forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtanstack.ts
More file actions
451 lines (420 loc) · 14.6 KB
/
Copy pathtanstack.ts
File metadata and controls
451 lines (420 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import {
BaseEvent,
EventType,
RunAgentInput,
Message,
TextMessageChunkEvent,
ToolCallArgsEvent,
ToolCallEndEvent,
ToolCallStartEvent,
ToolCallResultEvent,
StateSnapshotEvent,
StateDeltaEvent,
ReasoningStartEvent,
ReasoningMessageStartEvent,
ReasoningMessageContentEvent,
ReasoningMessageEndEvent,
ReasoningEndEvent,
} from "@ag-ui/client";
import { randomUUID } from "@copilotkit/shared";
type ContentPartSource =
| { type: "data"; value: string; mimeType: string }
| { type: "url"; value: string; mimeType?: string };
/**
* A TanStack AI content part (text, image, audio, video, or document).
*/
export type TanStackContentPart =
| { type: "text"; content: string }
| { type: "image"; source: ContentPartSource }
| { type: "audio"; source: ContentPartSource }
| { type: "video"; source: ContentPartSource }
| { type: "document"; source: ContentPartSource };
/**
* Message format expected by TanStack AI's `chat()`.
*
* Content is typed as `any[]` for the multimodal case so messages are directly
* passable to any adapter without casts — different adapters constrain which
* modalities they accept (e.g. OpenAI only allows text + image).
* Use `TanStackContentPart` to inspect individual parts if needed.
*/
export interface TanStackChatMessage {
role: "user" | "assistant" | "tool";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
content: string | null | any[];
name?: string;
toolCalls?: Array<{
id: string;
type: "function";
function: { name: string; arguments: string };
}>;
toolCallId?: string;
}
/**
* Result of converting RunAgentInput to TanStack AI format.
*/
export interface TanStackInputResult {
/** Chat messages (only user/assistant/tool roles; all others excluded) */
messages: TanStackChatMessage[];
/** System prompts extracted from system/developer messages, context, and state */
systemPrompts: string[];
}
/**
* Converts AG-UI user message content to TanStack AI format.
* Handles plain strings, multimodal parts (image/audio/video/document),
* and legacy BinaryInputContent for backward compatibility.
*/
function convertUserContent(
content: unknown,
): string | null | TanStackContentPart[] {
if (!content) return null;
if (typeof content === "string") return content;
if (!Array.isArray(content)) return null;
if (content.length === 0) return "";
const parts: TanStackContentPart[] = [];
for (const part of content) {
if (!part || typeof part !== "object" || !("type" in part)) continue;
switch ((part as { type: string }).type) {
case "text": {
const text = (part as { text?: string }).text;
if (text != null) parts.push({ type: "text", content: text });
break;
}
case "image":
case "audio":
case "video":
case "document": {
const source = (part as { source?: any }).source;
if (!source) break;
const partType = (part as { type: string }).type as
| "image"
| "audio"
| "video"
| "document";
if (source.type === "data") {
parts.push({
type: partType,
source: {
type: "data",
value: source.value,
mimeType: source.mimeType,
},
});
} else if (source.type === "url") {
parts.push({
type: partType,
source: {
type: "url",
value: source.value,
...(source.mimeType ? { mimeType: source.mimeType } : {}),
},
});
}
break;
}
// Legacy BinaryInputContent backward compatibility
case "binary": {
const legacy = part as {
mimeType?: string;
data?: string;
url?: string;
};
const mimeType = legacy.mimeType ?? "application/octet-stream";
const isImage = mimeType.startsWith("image/");
if (legacy.data) {
const partType = isImage ? "image" : "document";
parts.push({
type: partType,
source: { type: "data", value: legacy.data, mimeType },
});
} else if (legacy.url) {
const partType = isImage ? "image" : "document";
parts.push({
type: partType,
source: { type: "url", value: legacy.url, mimeType },
});
}
break;
}
}
}
return parts.length > 0 ? parts : "";
}
/**
* Converts a RunAgentInput into the format expected by TanStack AI's `chat()`.
*
* - Keeps only user/assistant/tool messages (activity, reasoning, and other roles are also excluded)
* - Extracts system/developer messages into `systemPrompts`
* - Appends context entries and application state to `systemPrompts`
* - Preserves tool calls on assistant messages and toolCallId on tool messages
*/
export function convertInputToTanStackAI(
input: RunAgentInput,
): TanStackInputResult {
// Allowlist: only pass user/assistant/tool messages to TanStack.
// Other roles (system, developer, activity, reasoning) are either
// extracted into systemPrompts or not applicable.
const chatRoles = new Set(["user", "assistant", "tool"]);
const messages: TanStackChatMessage[] = input.messages
.filter((m: Message) => chatRoles.has(m.role))
.map((m: Message): TanStackChatMessage => {
const msg: TanStackChatMessage = {
role: m.role as "user" | "assistant" | "tool",
content:
m.role === "user"
? convertUserContent(m.content)
: typeof m.content === "string"
? m.content
: null,
};
if (m.role === "assistant" && "toolCalls" in m && m.toolCalls) {
msg.toolCalls = m.toolCalls.map((tc) => ({
id: tc.id,
type: "function" as const,
function: {
name: tc.function.name,
arguments: tc.function.arguments,
},
}));
}
if (m.role === "tool" && "toolCallId" in m) {
msg.toolCallId = (m as Record<string, unknown>).toolCallId as string;
}
return msg;
});
const systemPrompts: string[] = [];
for (const m of input.messages) {
if ((m.role === "system" || m.role === "developer") && m.content) {
systemPrompts.push(
typeof m.content === "string" ? m.content : JSON.stringify(m.content),
);
}
}
if (input.context?.length) {
for (const ctx of input.context) {
systemPrompts.push(`${ctx.description}:\n${ctx.value}`);
}
}
if (
input.state !== undefined &&
input.state !== null &&
typeof input.state === "object" &&
Object.keys(input.state).length > 0
) {
systemPrompts.push(
`Application State:\n\`\`\`json\n${JSON.stringify(input.state, null, 2)}\n\`\`\``,
);
}
return { messages, systemPrompts };
}
/**
* Converts a TanStack AI stream into AG-UI `BaseEvent` objects.
*
* This is a pure converter — it does NOT emit lifecycle events
* (RUN_STARTED / RUN_FINISHED / RUN_ERROR). The caller (Agent class)
* is responsible for those.
*/
export async function* convertTanStackStream(
stream: AsyncIterable<unknown>,
abortSignal: AbortSignal,
): AsyncGenerator<BaseEvent> {
const messageId = randomUUID();
const toolNamesById = new Map<string, string>();
// Track the reasoning lifecycle at two granularities so closeReasoningIfOpen
// emits exactly the events still owed. A single boolean conflates the run
// (REASONING_START → REASONING_END) with the message
// (REASONING_MESSAGE_START → REASONING_MESSAGE_END) and produces a duplicate
// REASONING_MESSAGE_END when upstream emits MSG_END but not END before
// text/tools resume.
let reasoningRunOpen = false;
let reasoningMessageOpen = false;
let reasoningMessageId = randomUUID();
function* closeReasoningIfOpen(): Generator<BaseEvent> {
if (reasoningMessageOpen) {
reasoningMessageOpen = false;
const msgEnd: ReasoningMessageEndEvent = {
type: EventType.REASONING_MESSAGE_END,
messageId: reasoningMessageId,
};
yield msgEnd;
}
if (reasoningRunOpen) {
reasoningRunOpen = false;
const end: ReasoningEndEvent = {
type: EventType.REASONING_END,
messageId: reasoningMessageId,
};
yield end;
}
}
// TanStack's chat() engine runs a multi-turn agent loop: after the model
// returns tool calls, the engine tries to execute them and re-prompt. This
// produces a second round of TOOL_CALL_START / TOOL_CALL_END events that
// duplicate the ones from the first streaming pass. The CopilotKit runtime
// handles tool execution externally (via the frontend SDK), so we must stop
// converting events once the TanStack adapter signals the first turn is
// complete with RUN_FINISHED.
let runFinished = false;
for await (const chunk of stream) {
if (abortSignal.aborted) break;
const raw = chunk as Record<string, unknown>;
const type = raw.type as string;
// Stop converting after the first RUN_FINISHED — any subsequent events
// come from TanStack's internal tool-execution loop and would produce
// duplicate TOOL_CALL_END events that violate the ag-ui verify middleware.
if (type === "RUN_FINISHED") {
runFinished = true;
continue;
}
if (runFinished) continue;
if (type === "TEXT_MESSAGE_CONTENT" && raw.delta != null) {
yield* closeReasoningIfOpen();
const textEvent: TextMessageChunkEvent = {
type: EventType.TEXT_MESSAGE_CHUNK,
role: "assistant",
messageId,
delta: raw.delta as string,
};
yield textEvent;
} else if (type === "TOOL_CALL_START") {
yield* closeReasoningIfOpen();
toolNamesById.set(raw.toolCallId as string, raw.toolCallName as string);
const startEvent: ToolCallStartEvent = {
type: EventType.TOOL_CALL_START,
parentMessageId: messageId,
toolCallId: raw.toolCallId as string,
toolCallName: raw.toolCallName as string,
};
yield startEvent;
} else if (type === "TOOL_CALL_ARGS") {
yield* closeReasoningIfOpen();
const argsEvent: ToolCallArgsEvent = {
type: EventType.TOOL_CALL_ARGS,
toolCallId: raw.toolCallId as string,
delta: raw.delta as string,
};
yield argsEvent;
} else if (type === "TOOL_CALL_END") {
yield* closeReasoningIfOpen();
const endEvent: ToolCallEndEvent = {
type: EventType.TOOL_CALL_END,
toolCallId: raw.toolCallId as string,
};
yield endEvent;
} else if (type === "TOOL_CALL_RESULT") {
yield* closeReasoningIfOpen();
const toolCallId = raw.toolCallId as string;
const toolName = toolNamesById.get(toolCallId);
// Accept the payload from either `content` (canonical TanStack shape)
// or `result` (alternate shape used by some adapters / tests). Both
// state-tool detection and the final TOOL_CALL_RESULT serialization
// must read the same field, otherwise STATE_SNAPSHOT/STATE_DELTA can
// be silently dropped when upstream uses `result`.
const rawPayload = raw.content ?? raw.result;
const parsedContent =
typeof rawPayload === "string" ? safeParse(rawPayload) : rawPayload;
if (
toolName === "AGUISendStateSnapshot" &&
parsedContent &&
typeof parsedContent === "object" &&
"snapshot" in parsedContent
) {
const stateSnapshotEvent: StateSnapshotEvent = {
type: EventType.STATE_SNAPSHOT,
snapshot: (parsedContent as Record<string, unknown>).snapshot,
};
yield stateSnapshotEvent;
}
if (
toolName === "AGUISendStateDelta" &&
parsedContent &&
typeof parsedContent === "object" &&
"delta" in parsedContent
) {
const stateDeltaEvent: StateDeltaEvent = {
type: EventType.STATE_DELTA,
delta: (parsedContent as Record<string, unknown>).delta as never,
};
yield stateDeltaEvent;
}
let serializedContent: string;
if (typeof rawPayload === "string") {
serializedContent = rawPayload;
} else {
try {
serializedContent = JSON.stringify(rawPayload ?? null);
} catch {
serializedContent = "[Unserializable tool result]";
}
}
const resultEvent: ToolCallResultEvent = {
type: EventType.TOOL_CALL_RESULT,
role: "tool",
messageId: randomUUID(),
toolCallId,
content: serializedContent,
};
yield resultEvent;
toolNamesById.delete(toolCallId);
} else if (type === "REASONING_START") {
// If a prior reasoning run is still open (no REASONING_END before this
// new START), close it cleanly first so MSG_END / END pair correctly.
yield* closeReasoningIfOpen();
reasoningRunOpen = true;
reasoningMessageId = (raw.messageId as string) ?? randomUUID();
const startEvt: ReasoningStartEvent = {
type: EventType.REASONING_START,
messageId: reasoningMessageId,
};
yield startEvt;
} else if (type === "REASONING_MESSAGE_START") {
reasoningMessageOpen = true;
const evt: ReasoningMessageStartEvent = {
type: EventType.REASONING_MESSAGE_START,
messageId: reasoningMessageId,
role: "reasoning",
};
yield evt;
} else if (type === "REASONING_MESSAGE_CONTENT") {
const evt: ReasoningMessageContentEvent = {
type: EventType.REASONING_MESSAGE_CONTENT,
messageId: reasoningMessageId,
delta: raw.delta as string,
};
yield evt;
} else if (type === "REASONING_MESSAGE_END") {
reasoningMessageOpen = false;
const evt: ReasoningMessageEndEvent = {
type: EventType.REASONING_MESSAGE_END,
messageId: reasoningMessageId,
};
yield evt;
} else if (type === "REASONING_END") {
// If upstream sends REASONING_END while a message is still open, emit
// the missing REASONING_MESSAGE_END FIRST so the closing pair stays in
// order (MSG_END before END). Otherwise the next non-reasoning chunk
// would trigger closeReasoningIfOpen and emit MSG_END after END.
if (reasoningMessageOpen) {
reasoningMessageOpen = false;
const msgEnd: ReasoningMessageEndEvent = {
type: EventType.REASONING_MESSAGE_END,
messageId: reasoningMessageId,
};
yield msgEnd;
}
reasoningRunOpen = false;
const evt: ReasoningEndEvent = {
type: EventType.REASONING_END,
messageId: reasoningMessageId,
};
yield evt;
}
}
yield* closeReasoningIfOpen();
}
function safeParse(value: string): unknown {
try {
return JSON.parse(value);
} catch {
return value;
}
}