Skip to content

Commit

Permalink
feat: split oauth prompts into their own messages (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhopperlowe authored Oct 19, 2024
1 parent 75b7fc4 commit 6d20913
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
11 changes: 9 additions & 2 deletions ui/admin/app/components/chat/ChatContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ChatEvent, combineChatEvents } from "~/lib/model/chatEvents";
import {
Message,
chatEventsToMessages,
promptMessage,
toolCallMessage,
} from "~/lib/model/messages";
import { InvokeService } from "~/lib/service/api/invokeService";
Expand Down Expand Up @@ -149,7 +150,8 @@ export function ChatProvider({
onChunk: (chunk) =>
// use a transition for performance
startTransition(() => {
const { content, toolCall, runID, input } = chunk;
const { content, toolCall, runID, input, prompt } =
chunk;

generatingRunIdRef.current = runID;

Expand All @@ -165,9 +167,14 @@ export function ChatProvider({

return;
}

isRunningToolCall.current = false;

if (prompt) {
insertGeneratingMessage(runID);
insertMessage(promptMessage(prompt, runID));
return;
}

if (content && !input) {
appendToGeneratingMessage(content);
}
Expand Down
4 changes: 1 addition & 3 deletions ui/admin/app/components/ui/scroll-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ const ScrollArea = React.forwardRef<
}
}, [startScrollAt]);

const deferredChildren = React.useDeferredValue(children);

React.useEffect(() => {
if (shouldStickToBottom && enableScrollStick === "bottom") {
viewportRef.current?.scrollTo({
top: viewportRef.current.scrollHeight,
behavior: "instant",
});
}
}, [shouldStickToBottom, enableScrollStick, deferredChildren]);
}, [enableScrollStick, shouldStickToBottom, children]);

return (
<ScrollAreaPrimitive.Root
Expand Down
21 changes: 19 additions & 2 deletions ui/admin/app/lib/model/chatEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ export type ToolCall = {
};
};

type PromptOAuthMeta = {
authType: string;
authURL: string;
toolContext: string;
toolDisplayName: string;
};

export type Prompt = {
id?: string;
time?: Date;
message?: string;
fields?: string[];
sensitive?: boolean;
metadata?: PromptOAuthMeta;
};

// note(ryanhopperlowe) renaming this to ChatEvent to differentiate itself specifically for a chat with an agent
// we should create a separate type for WorkflowEvents and leverage Unions to differentiate between them
export type ChatEvent = {
Expand All @@ -24,6 +40,7 @@ export type ChatEvent = {
waitingOnModel?: boolean;
toolInput?: ToolInput;
toolCall?: ToolCall;
prompt?: Prompt;
};

export function combineChatEvents(events: ChatEvent[]): ChatEvent[] {
Expand All @@ -39,10 +56,10 @@ export function combineChatEvents(events: ChatEvent[]): ChatEvent[] {
};

for (const event of events) {
const { content, input, error, runID, toolCall } = event;
const { content, input, error, runID, toolCall, prompt } = event;

// signals the end of a content block
if (error || toolCall || input) {
if (error || toolCall || input || prompt) {
insertBuildingEvent();

combinedEvents.push(event);
Expand Down
29 changes: 19 additions & 10 deletions ui/admin/app/lib/model/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChatEvent, ToolCall } from "./chatEvents";
import { ChatEvent, Prompt, ToolCall } from "./chatEvents";
import { Run } from "./runs";

export interface Message {
Expand Down Expand Up @@ -32,21 +32,24 @@ export const runsToMessages = (runs: Run[]) => {
return messages;
};

export const toolCallMessage = (toolCall: ToolCall) => {
return {
sender: "agent",
text: `Tool call: ${[toolCall.metadata?.category, toolCall.name].filter((x) => !!x).join(" - ")}`,
tools: [toolCall],
} as Message;
};
export const toolCallMessage = (toolCall: ToolCall): Message => ({
sender: "agent",
text: `Tool call: ${[toolCall.metadata?.category, toolCall.name].filter((x) => !!x).join(" - ")}`,
tools: [toolCall],
});

export const promptMessage = (prompt: Prompt, runID: string): Message => ({
sender: "agent",
text: prompt.message || "",
runId: runID,
});

export const chatEventsToMessages = (events: ChatEvent[]) => {
const messages: Message[] = [];

for (const event of events) {
const { content, input, toolCall, runID, error } = event;
const { content, input, toolCall, runID, error, prompt } = event;

// skip errors and tool inputs with no content
if (error) {
messages.push({
sender: "agent",
Expand All @@ -71,6 +74,12 @@ export const chatEventsToMessages = (events: ChatEvent[]) => {
continue;
}

// note(ryanhopperlowe) this just splits out a new message. In the future we will want to create a custom prompt message
if (prompt) {
messages.push(promptMessage(prompt, runID));
continue;
}

if (content) {
messages.push({
sender: "agent",
Expand Down

0 comments on commit 6d20913

Please sign in to comment.