xonecas.io
5414
Sa

sat / files

sat / src / lib / chat.ts

5.1 KB
1import {
2  AIMessage,
3  AIMessageChunk,
4  type BaseMessage,
5  HumanMessage,
6  ToolMessage,
7} from "@langchain/core/messages";
8import { concat } from "@langchain/core/utils/stream";
9import type { TurnWithToolCalls } from "@/db/schema";
10import { getAgent } from "@/lib/agent";
11
12export type StreamEvent =
13  | { type: "token"; content: string }
14  | { type: "reasoning"; content: string }
15  | {
16      type: "tool_call";
17      id: string;
18      name: string;
19      args: Record<string, unknown>;
20    }
21  | { type: "tool_result"; id: string; result: string };
22
23function historyToMessages(history: TurnWithToolCalls[]): BaseMessage[] {
24  const messages: BaseMessage[] = [];
25
26  for (const turn of history) {
27    if (turn.role === "user") {
28      messages.push(new HumanMessage(turn.content));
29    } else {
30      if (turn.toolCalls?.length) {
31        messages.push(
32          new AIMessage({
33            content: turn.content,
34            tool_calls: turn.toolCalls.map((tc) => ({
35              id: tc.id,
36              name: tc.name,
37              args: tc.args,
38            })),
39          }),
40        );
41        // Add corresponding ToolMessages
42        for (const tc of turn.toolCalls) {
43          if (tc.result) {
44            messages.push(
45              new ToolMessage({
46                tool_call_id: tc.id,
47                content: tc.result,
48              }),
49            );
50          }
51        }
52      } else {
53        messages.push(new AIMessage(turn.content));
54      }
55    }
56  }
57
58  return messages;
59}
60
61export async function chat(
62  userInput: string,
63  onEvent: (event: StreamEvent) => void,
64  history?: TurnWithToolCalls[],
65  conversationId?: string,
66) {
67  const agent = getAgent();
68  const threadId = conversationId ?? "default";
69  const config = { configurable: { thread_id: threadId } };
70
71  // Check if this thread has existing state in the checkpointer
72  // If not, we need to populate it from PostgreSQL history
73  if (history && history.length > 0) {
74    try {
75      const existingState = (await agent.getState(config)) as {
76        values?: { messages?: unknown[] };
77      } | null;
78      const stateValues = existingState?.values;
79
80      // If no messages in checkpointer state, populate from DB history
81      const hasMessages =
82        stateValues?.messages &&
83        Array.isArray(stateValues.messages) &&
84        stateValues.messages.length > 0;
85
86      if (!hasMessages) {
87        const historyMessages = historyToMessages(history);
88        // Use updateState to populate the checkpointer
89        await agent.updateState(config, { messages: historyMessages });
90      }
91    } catch (error) {
92      console.error("[chat] Error checking/populating state:", error);
93    }
94  }
95
96  // Stream with just the new user message
97  const stream = await agent.stream(
98    {
99      messages: [{ role: "user", content: userInput }],
100    },
101    { ...config, streamMode: ["messages", "updates"] },
102  );
103
104  // Track emitted tool calls to avoid duplicates
105  const emittedToolCallIds = new Set<string>();
106  let aggregatedAiChunk: AIMessageChunk | undefined;
107
108  for await (const [mode, chunk] of stream) {
109    // Handle token-by-token streaming from "messages" mode
110    if (mode === "messages") {
111      const [message, _metadata] = chunk as [unknown, unknown];
112
113      if (message instanceof AIMessageChunk) {
114        // Stream content tokens immediately
115        if (typeof message.content === "string" && message.content) {
116          onEvent({ type: "token", content: message.content });
117        }
118
119        // Aggregate for tool call detection within messages mode
120        aggregatedAiChunk =
121          aggregatedAiChunk === undefined
122            ? message
123            : concat(aggregatedAiChunk, message);
124      }
125    }
126
127    // Handle state updates from "updates" mode (tool calls and reasoning)
128    if (mode === "updates") {
129      const update = chunk as Record<string, { messages?: unknown[] }>;
130
131      for (const [_node, state] of Object.entries(update)) {
132        if (!state.messages) continue;
133
134        for (const msg of state.messages) {
135          // Handle AI messages with tool calls
136          if (AIMessage.isInstance(msg)) {
137            if (msg.tool_calls) {
138              for (const tc of msg.tool_calls) {
139                if (tc.id && tc.name && !emittedToolCallIds.has(tc.id)) {
140                  emittedToolCallIds.add(tc.id);
141                  onEvent({
142                    type: "tool_call",
143                    id: tc.id,
144                    name: tc.name,
145                    args: tc.args ?? {},
146                  });
147                }
148              }
149            }
150
151            // Handle reasoning from updates
152            const reasoning = msg.additional_kwargs?.reasoning_content;
153            if (typeof reasoning === "string" && reasoning) {
154              onEvent({ type: "reasoning", content: reasoning });
155            }
156          }
157
158          // Handle tool results
159          if (ToolMessage.isInstance(msg)) {
160            onEvent({
161              type: "tool_result",
162              id: msg.tool_call_id,
163              result:
164                typeof msg.content === "string"
165                  ? msg.content
166                  : JSON.stringify(msg.content),
167            });
168          }
169        }
170      }
171    }
172  }
173}