@agent-native/core 0.12.24 → 0.12.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/production-agent.d.ts +9 -2
- package/dist/agent/production-agent.d.ts.map +1 -1
- package/dist/agent/production-agent.js +36 -35
- package/dist/agent/production-agent.js.map +1 -1
- package/dist/agent/thread-data-builder.d.ts +16 -1
- package/dist/agent/thread-data-builder.d.ts.map +1 -1
- package/dist/agent/thread-data-builder.js +132 -0
- package/dist/agent/thread-data-builder.js.map +1 -1
- package/dist/client/agent-chat-adapter.d.ts.map +1 -1
- package/dist/client/agent-chat-adapter.js +15 -3
- package/dist/client/agent-chat-adapter.js.map +1 -1
- package/dist/client/progress/RunsTray.d.ts.map +1 -1
- package/dist/client/progress/RunsTray.js +3 -1
- package/dist/client/progress/RunsTray.js.map +1 -1
- package/dist/client/sse-event-processor.d.ts.map +1 -1
- package/dist/client/sse-event-processor.js +4 -1
- package/dist/client/sse-event-processor.js.map +1 -1
- package/dist/onboarding/default-steps.d.ts.map +1 -1
- package/dist/onboarding/default-steps.js +8 -1
- package/dist/onboarding/default-steps.js.map +1 -1
- package/dist/server/agent-chat-plugin.d.ts.map +1 -1
- package/dist/server/agent-chat-plugin.js +53 -2
- package/dist/server/agent-chat-plugin.js.map +1 -1
- package/dist/server/credential-provider.d.ts +2 -2
- package/dist/server/credential-provider.d.ts.map +1 -1
- package/dist/server/credential-provider.js +22 -1
- package/dist/server/credential-provider.js.map +1 -1
- package/dist/server/google-oauth.js +1 -1
- package/dist/server/google-oauth.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RunEvent } from "./types.js";
|
|
1
|
+
import type { AgentChatAttachment, RunEvent } from "./types.js";
|
|
2
2
|
interface ContentPart {
|
|
3
3
|
type: string;
|
|
4
4
|
text?: string;
|
|
@@ -12,6 +12,7 @@ interface BuildAssistantMessageOptions {
|
|
|
12
12
|
suppressInternalContinuation?: boolean;
|
|
13
13
|
}
|
|
14
14
|
type AssistantMessage = NonNullable<ReturnType<typeof buildAssistantMessage>>;
|
|
15
|
+
type UserMessage = ReturnType<typeof buildUserMessage>;
|
|
15
16
|
/**
|
|
16
17
|
* Reconstruct an assistant-ui message from raw agent run events.
|
|
17
18
|
* Mirrors the client-side processEvent logic so the server can persist
|
|
@@ -41,6 +42,20 @@ export declare function buildAssistantMessage(events: RunEvent[], runId?: string
|
|
|
41
42
|
* accepting client-only messages and metadata.
|
|
42
43
|
*/
|
|
43
44
|
export declare function mergeThreadDataForClientSave(existingRepo: any, incomingRepo: any): any;
|
|
45
|
+
export declare function buildUserMessage(opts: {
|
|
46
|
+
text: string;
|
|
47
|
+
attachments?: AgentChatAttachment[];
|
|
48
|
+
runId?: string;
|
|
49
|
+
createdAt?: Date;
|
|
50
|
+
}): {
|
|
51
|
+
id: string;
|
|
52
|
+
createdAt: Date;
|
|
53
|
+
role: "user";
|
|
54
|
+
content: ContentPart[];
|
|
55
|
+
attachments?: any[];
|
|
56
|
+
metadata: Record<string, unknown>;
|
|
57
|
+
};
|
|
58
|
+
export declare function upsertUserMessage(repo: any, userMsg: UserMessage): any;
|
|
44
59
|
/**
|
|
45
60
|
* Merge the server-reconstructed assistant message into persisted
|
|
46
61
|
* assistant-ui thread data.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"thread-data-builder.d.ts","sourceRoot":"","sources":["../../src/agent/thread-data-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"thread-data-builder.d.ts","sourceRoot":"","sources":["../../src/agent/thread-data-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEhE,UAAU,WAAW;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;I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IACF,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,EAAE,IAAI,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,WAAW,CAAC,EAAE,GAAG,EAAE,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC,CAcA;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,WAAW,GAAG,GAAG,CAoBtE;AA+BD;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,GAAG,EACT,YAAY,EAAE,gBAAgB,GAC7B,GAAG,CA+BL;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,GAAG,GAAG;IAC5C,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;CACjB,CAyBA"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const MAX_STORED_ATTACHMENT_CHARS = 60_000;
|
|
1
2
|
function isInternalContinuationError(event) {
|
|
2
3
|
const code = String(event.errorCode ?? "").toLowerCase();
|
|
3
4
|
const msg = event.error.toLowerCase();
|
|
@@ -174,6 +175,15 @@ function isTerminalAssistantStatus(status) {
|
|
|
174
175
|
const type = status?.type;
|
|
175
176
|
return type === "complete" || type === "incomplete";
|
|
176
177
|
}
|
|
178
|
+
function normalizeAttachmentIdentity(attachments) {
|
|
179
|
+
if (!Array.isArray(attachments))
|
|
180
|
+
return undefined;
|
|
181
|
+
return attachments.map((att) => ({
|
|
182
|
+
type: att?.type,
|
|
183
|
+
name: att?.name,
|
|
184
|
+
contentType: att?.contentType,
|
|
185
|
+
}));
|
|
186
|
+
}
|
|
177
187
|
function messageIdentityKeys(message) {
|
|
178
188
|
const keys = [];
|
|
179
189
|
if (typeof message?.id === "string" && message.id) {
|
|
@@ -192,8 +202,24 @@ function messageIdentityKeys(message) {
|
|
|
192
202
|
catch {
|
|
193
203
|
// Best effort. id/runId usually exist for persisted assistant-ui rows.
|
|
194
204
|
}
|
|
205
|
+
if (message?.role === "user") {
|
|
206
|
+
try {
|
|
207
|
+
keys.push(`user-fingerprint:${JSON.stringify({
|
|
208
|
+
role: message.role,
|
|
209
|
+
content: message.content,
|
|
210
|
+
attachments: normalizeAttachmentIdentity(message.attachments),
|
|
211
|
+
})}`);
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
// Same best-effort behavior as the full fingerprint.
|
|
215
|
+
}
|
|
216
|
+
}
|
|
195
217
|
return keys;
|
|
196
218
|
}
|
|
219
|
+
function messagesMatch(a, b) {
|
|
220
|
+
const bKeys = new Set(messageIdentityKeys(b));
|
|
221
|
+
return messageIdentityKeys(a).some((key) => bKeys.has(key));
|
|
222
|
+
}
|
|
197
223
|
function chooseMergedMessageEntry(existingEntry, incomingEntry) {
|
|
198
224
|
const existing = getStoredMessage(existingEntry);
|
|
199
225
|
const incoming = getStoredMessage(incomingEntry);
|
|
@@ -250,6 +276,112 @@ export function mergeThreadDataForClientSave(existingRepo, incomingRepo) {
|
|
|
250
276
|
merged.messages = nextMessages;
|
|
251
277
|
return merged;
|
|
252
278
|
}
|
|
279
|
+
function escapeAttachmentAttribute(value) {
|
|
280
|
+
return value.replace(/&/g, "&").replace(/"/g, """);
|
|
281
|
+
}
|
|
282
|
+
function unwrapTextAttachmentEnvelope(text) {
|
|
283
|
+
const match = text.match(/^<attachment\b[^>]*>\n?([\s\S]*?)\n?<\/attachment>$/);
|
|
284
|
+
return match ? match[1] : text;
|
|
285
|
+
}
|
|
286
|
+
function truncateStoredAttachment(text) {
|
|
287
|
+
const unwrapped = unwrapTextAttachmentEnvelope(text);
|
|
288
|
+
if (unwrapped.length <= MAX_STORED_ATTACHMENT_CHARS)
|
|
289
|
+
return unwrapped;
|
|
290
|
+
const omitted = unwrapped.length - MAX_STORED_ATTACHMENT_CHARS;
|
|
291
|
+
return `${unwrapped.slice(0, MAX_STORED_ATTACHMENT_CHARS)}\n\n[Attachment truncated after ${MAX_STORED_ATTACHMENT_CHARS.toLocaleString()} characters; ${omitted.toLocaleString()} characters omitted from persisted chat history.]`;
|
|
292
|
+
}
|
|
293
|
+
function textAttachmentEnvelope(att, text) {
|
|
294
|
+
const attrs = [
|
|
295
|
+
`name="${escapeAttachmentAttribute(att.name || "attachment")}"`,
|
|
296
|
+
att.contentType
|
|
297
|
+
? `contentType="${escapeAttachmentAttribute(att.contentType)}"`
|
|
298
|
+
: null,
|
|
299
|
+
att.type ? `type="${escapeAttachmentAttribute(att.type)}"` : null,
|
|
300
|
+
].filter(Boolean);
|
|
301
|
+
return `<attachment ${attrs.join(" ")}>\n${truncateStoredAttachment(text)}\n</attachment>`;
|
|
302
|
+
}
|
|
303
|
+
function buildStoredAttachments(attachments, runId) {
|
|
304
|
+
return (attachments ?? [])
|
|
305
|
+
.map((att, index) => {
|
|
306
|
+
const id = `server-${runId ?? Date.now()}-attachment-${index}`;
|
|
307
|
+
if (att.type === "image" && att.data) {
|
|
308
|
+
return {
|
|
309
|
+
id,
|
|
310
|
+
type: "image",
|
|
311
|
+
name: att.name,
|
|
312
|
+
contentType: att.contentType,
|
|
313
|
+
status: { type: "complete" },
|
|
314
|
+
content: [{ type: "image", image: att.data }],
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
if (att.data) {
|
|
318
|
+
return {
|
|
319
|
+
id,
|
|
320
|
+
type: "file",
|
|
321
|
+
name: att.name,
|
|
322
|
+
contentType: att.contentType,
|
|
323
|
+
status: { type: "complete" },
|
|
324
|
+
content: [
|
|
325
|
+
{
|
|
326
|
+
type: "file",
|
|
327
|
+
data: att.data,
|
|
328
|
+
mimeType: att.contentType,
|
|
329
|
+
filename: att.name,
|
|
330
|
+
},
|
|
331
|
+
],
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
if (typeof att.text === "string" && att.text.length > 0) {
|
|
335
|
+
return {
|
|
336
|
+
id,
|
|
337
|
+
type: "file",
|
|
338
|
+
name: att.name,
|
|
339
|
+
contentType: att.contentType,
|
|
340
|
+
status: { type: "complete" },
|
|
341
|
+
content: [
|
|
342
|
+
{ type: "text", text: textAttachmentEnvelope(att, att.text) },
|
|
343
|
+
],
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
return null;
|
|
347
|
+
})
|
|
348
|
+
.filter(Boolean);
|
|
349
|
+
}
|
|
350
|
+
export function buildUserMessage(opts) {
|
|
351
|
+
const attachments = buildStoredAttachments(opts.attachments, opts.runId);
|
|
352
|
+
return {
|
|
353
|
+
id: `server-user-${opts.runId ?? Date.now()}`,
|
|
354
|
+
createdAt: opts.createdAt ?? new Date(),
|
|
355
|
+
role: "user",
|
|
356
|
+
content: [{ type: "text", text: opts.text }],
|
|
357
|
+
...(attachments.length > 0 ? { attachments } : {}),
|
|
358
|
+
metadata: {
|
|
359
|
+
custom: {
|
|
360
|
+
submittedRunId: opts.runId,
|
|
361
|
+
},
|
|
362
|
+
},
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
export function upsertUserMessage(repo, userMsg) {
|
|
366
|
+
const nextRepo = repo && typeof repo === "object" ? repo : {};
|
|
367
|
+
if (!Array.isArray(nextRepo.messages))
|
|
368
|
+
nextRepo.messages = [];
|
|
369
|
+
const lastIndex = nextRepo.messages.length - 1;
|
|
370
|
+
const lastEntry = lastIndex >= 0 ? nextRepo.messages[lastIndex] : undefined;
|
|
371
|
+
const lastMsg = getStoredMessage(lastEntry);
|
|
372
|
+
if (lastMsg?.role === "user" && messagesMatch(lastMsg, userMsg)) {
|
|
373
|
+
return nextRepo;
|
|
374
|
+
}
|
|
375
|
+
const isWrapped = Boolean(lastEntry && "message" in lastEntry);
|
|
376
|
+
if (isWrapped) {
|
|
377
|
+
const parentId = lastIndex >= 0 ? (getStoredMessage(lastEntry)?.id ?? null) : null;
|
|
378
|
+
nextRepo.messages.push({ message: userMsg, parentId });
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
nextRepo.messages.push(userMsg);
|
|
382
|
+
}
|
|
383
|
+
return nextRepo;
|
|
384
|
+
}
|
|
253
385
|
function shouldReplaceLastAssistant(lastMessage, assistantMsg) {
|
|
254
386
|
const lastContent = lastMessage?.content;
|
|
255
387
|
if (messageContentIsEmpty(lastContent))
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"thread-data-builder.js","sourceRoot":"","sources":["../../src/agent/thread-data-builder.ts"],"names":[],"mappings":"AAkBA,SAAS,2BAA2B,CAAC,KAIpC;IACC,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IACzD,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC;IACtC,OAAO,CACL,KAAK,CAAC,WAAW,KAAK,IAAI;QAC1B,IAAI,KAAK,uBAAuB;QAChC,IAAI,KAAK,yBAAyB;QAClC,IAAI,KAAK,WAAW;QACpB,IAAI,KAAK,SAAS;QAClB,IAAI,KAAK,eAAe;QACxB,IAAI,KAAK,UAAU;QACnB,IAAI,KAAK,UAAU;QACnB,IAAI,KAAK,UAAU;QACnB,IAAI,KAAK,UAAU;QACnB,IAAI,KAAK,UAAU;QACnB,IAAI,KAAK,UAAU;QACnB,IAAI,KAAK,cAAc;QACvB,IAAI,KAAK,8BAA8B;QACvC,IAAI,KAAK,kBAAkB;QAC3B,GAAG,CAAC,QAAQ,CAAC,SAAS,CAAC;QACvB,GAAG,CAAC,QAAQ,CAAC,eAAe,CAAC;QAC7B,GAAG,CAAC,QAAQ,CAAC,iBAAiB,CAAC;QAC/B,GAAG,CAAC,QAAQ,CAAC,oBAAoB,CAAC;QAClC,GAAG,CAAC,QAAQ,CAAC,cAAc,CAAC;QAC5B,GAAG,CAAC,QAAQ,CAAC,eAAe,CAAC;QAC7B,GAAG,CAAC,QAAQ,CAAC,yBAAyB,CAAC;QACvC,GAAG,CAAC,QAAQ,CAAC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,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;QAC3C,CAAC;IACH,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AAC5B,CAAC","sourcesContent":["import type { RunEvent } from \"./types.js\";\n\ninterface ContentPart {\n type: string;\n text?: string;\n toolCallId?: string;\n toolName?: string;\n argsText?: string;\n args?: Record<string, string>;\n result?: string;\n}\n\ninterface BuildAssistantMessageOptions {\n suppressInternalContinuation?: boolean;\n}\n\ntype AssistantMessage = NonNullable<ReturnType<typeof buildAssistantMessage>>;\n\nfunction isInternalContinuationError(event: {\n error: string;\n errorCode?: string;\n recoverable?: boolean;\n}): boolean {\n const code = String(event.errorCode ?? \"\").toLowerCase();\n const msg = event.error.toLowerCase();\n return (\n event.recoverable === true ||\n code === \"builder_gateway_error\" ||\n code === \"builder_gateway_timeout\" ||\n code === \"stale_run\" ||\n code === \"timeout\" ||\n code === \"timeout_error\" ||\n code === \"http_408\" ||\n code === \"http_429\" ||\n code === \"http_500\" ||\n code === \"http_502\" ||\n code === \"http_503\" ||\n code === \"http_504\" ||\n code === \"rate_limited\" ||\n code === \"too_many_concurrent_requests\" ||\n code === \"overloaded_error\" ||\n msg.includes(\"timeout\") ||\n msg.includes(\"gateway error\") ||\n msg.includes(\"gateway timeout\") ||\n msg.includes(\"inactivity timeout\") ||\n msg.includes(\"stream ended\") ||\n msg.includes(\"stream closed\") ||\n msg.includes(\"temporarily unavailable\") ||\n msg.includes(\"502\") ||\n msg.includes(\"503\") ||\n msg.includes(\"504\") ||\n msg.includes(\"529\")\n );\n}\n\n/**\n * Reconstruct an assistant-ui message from raw agent run events.\n * Mirrors the client-side processEvent logic so the server can persist\n * the assistant's response even if the frontend is disconnected.\n */\nexport function buildAssistantMessage(\n events: RunEvent[],\n runId?: string,\n options: BuildAssistantMessageOptions = {},\n): {\n id: string;\n createdAt: Date;\n role: \"assistant\";\n content: ContentPart[];\n status:\n | { type: \"complete\"; reason: \"stop\" }\n | { type: \"incomplete\"; reason: \"error\" };\n metadata: Record<string, unknown>;\n} | null {\n const content: ContentPart[] = [];\n let toolCallCounter = 0;\n let runError: {\n message: string;\n errorCode?: string;\n details?: string;\n recoverable?: boolean;\n } | null = null;\n let endedAtInternalContinuationBoundary = false;\n\n const appendText = (text: string) => {\n const last = content[content.length - 1];\n if (last && last.type === \"text\") {\n last.text = (last.text ?? \"\") + text;\n } else {\n content.push({ type: \"text\", text });\n }\n };\n\n for (const { event } of events) {\n if (event.type === \"clear\") {\n content.length = 0;\n toolCallCounter = 0;\n continue;\n }\n\n if (event.type === \"text\") {\n appendText(event.text ?? \"\");\n continue;\n }\n\n if (event.type === \"tool_start\") {\n const toolCallId = `tc_${++toolCallCounter}`;\n const args = (event.input ?? {}) as Record<string, string>;\n content.push({\n type: \"tool-call\",\n toolCallId,\n toolName: event.tool ?? \"unknown\",\n argsText: JSON.stringify(args),\n args,\n });\n continue;\n }\n\n if (event.type === \"tool_done\") {\n for (let i = content.length - 1; i >= 0; i--) {\n const part = content[i];\n if (\n part.type === \"tool-call\" &&\n part.toolName === event.tool &&\n part.result === undefined\n ) {\n part.result = event.result ?? \"\";\n break;\n }\n }\n continue;\n }\n\n if (event.type === \"loop_limit\") {\n // Older servers emitted this as a user-visible terminal event. Treat it\n // as an internal continuation boundary when rebuilding persisted turns.\n if (options.suppressInternalContinuation) {\n endedAtInternalContinuationBoundary = true;\n }\n continue;\n }\n\n if (event.type === \"auto_continue\") {\n if (options.suppressInternalContinuation) {\n endedAtInternalContinuationBoundary = true;\n }\n continue;\n }\n\n if (event.type === \"error\") {\n if (\n options.suppressInternalContinuation &&\n isInternalContinuationError(event)\n ) {\n endedAtInternalContinuationBoundary = true;\n continue;\n }\n if (event.errorCode === \"run_timeout\" && event.recoverable) {\n continue;\n }\n runError = {\n message: event.error,\n ...(event.errorCode ? { errorCode: event.errorCode } : {}),\n ...(event.details ? { details: event.details } : {}),\n ...(event.recoverable ? { recoverable: event.recoverable } : {}),\n };\n appendText(`${content.length > 0 ? \"\\n\\n\" : \"\"}Error: ${event.error}`);\n continue;\n }\n\n // done, missing_api_key — terminal signals, not content\n }\n\n if (content.length === 0 || endedAtInternalContinuationBoundary) return null;\n\n const metadata: Record<string, unknown> = {};\n if (runId) metadata.runId = runId;\n if (runError) {\n metadata.custom = {\n runError: {\n ...runError,\n ...(runId ? { runId } : {}),\n },\n };\n }\n\n return {\n id: `server-${runId ?? Date.now()}`,\n createdAt: new Date(),\n role: \"assistant\",\n content,\n status: runError\n ? { type: \"incomplete\" as const, reason: \"error\" as const }\n : { type: \"complete\" as const, reason: \"stop\" as const },\n metadata,\n };\n}\n\nfunction getStoredMessage(entry: any): any {\n return entry?.message ?? entry;\n}\n\nfunction getMessageRunId(message: any): string | undefined {\n const meta = message?.metadata;\n const direct = meta?.runId;\n const custom = meta?.custom?.runId;\n const errorRun = meta?.custom?.runError?.runId ?? meta?.runError?.runId;\n if (typeof direct === \"string\") return direct;\n if (typeof custom === \"string\") return custom;\n if (typeof errorRun === \"string\") return errorRun;\n return undefined;\n}\n\nfunction messageContentIsEmpty(content: unknown): boolean {\n if (Array.isArray(content)) return content.length === 0;\n return content == null || content === \"\";\n}\n\nfunction messageText(content: unknown): string {\n if (typeof content === \"string\") return content;\n if (!Array.isArray(content)) return \"\";\n return content\n .filter(\n (part: any) => part?.type === \"text\" && typeof part.text === \"string\",\n )\n .map((part: any) => part.text)\n .join(\"\");\n}\n\nfunction isTerminalAssistantStatus(status: unknown): boolean {\n const type = (status as { type?: unknown } | undefined)?.type;\n return type === \"complete\" || type === \"incomplete\";\n}\n\nfunction messageIdentityKeys(message: any): string[] {\n const keys: string[] = [];\n if (typeof message?.id === \"string\" && message.id) {\n keys.push(`id:${message.id}`);\n }\n const runId = getMessageRunId(message);\n if (runId) keys.push(`run:${runId}`);\n\n try {\n keys.push(\n `fingerprint:${JSON.stringify({\n role: message?.role,\n content: message?.content,\n attachments: message?.attachments,\n })}`,\n );\n } catch {\n // Best effort. id/runId usually exist for persisted assistant-ui rows.\n }\n return keys;\n}\n\nfunction chooseMergedMessageEntry(existingEntry: any, incomingEntry: any): any {\n const existing = getStoredMessage(existingEntry);\n const incoming = getStoredMessage(incomingEntry);\n if (\n existing?.role === \"assistant\" &&\n incoming?.role === \"assistant\" &&\n isTerminalAssistantStatus(existing?.status) &&\n !isTerminalAssistantStatus(incoming?.status)\n ) {\n return existingEntry;\n }\n return incomingEntry;\n}\n\n/**\n * Merge an incoming client-side full-thread save over the current SQL copy.\n *\n * The browser exports and PUTs the whole assistant-ui repository. If a server\n * completion save lands first, an older browser export can otherwise replace\n * `thread_data` wholesale and delete the assistant message the server just\n * reconstructed from run events. Preserve server-only messages while still\n * accepting client-only messages and metadata.\n */\nexport function mergeThreadDataForClientSave(\n existingRepo: any,\n incomingRepo: any,\n) {\n const merged =\n incomingRepo && typeof incomingRepo === \"object\" ? incomingRepo : {};\n if (\n existingRepo &&\n typeof existingRepo === \"object\" &&\n existingRepo.queuedMessages !== undefined &&\n merged.queuedMessages === undefined\n ) {\n merged.queuedMessages = existingRepo.queuedMessages;\n }\n\n const existingMessages = Array.isArray(existingRepo?.messages)\n ? existingRepo.messages\n : null;\n const incomingMessages = Array.isArray(merged.messages)\n ? merged.messages\n : null;\n if (!existingMessages || !incomingMessages) return merged;\n\n const incomingKeySets = incomingMessages.map(\n (entry: any) => new Set(messageIdentityKeys(getStoredMessage(entry))),\n );\n const usedIncoming = new Set<number>();\n const nextMessages: any[] = [];\n\n for (const existingEntry of existingMessages) {\n const existingKeys = messageIdentityKeys(getStoredMessage(existingEntry));\n const incomingIndex = incomingKeySets.findIndex(\n (keys, index) =>\n !usedIncoming.has(index) && existingKeys.some((key) => keys.has(key)),\n );\n\n if (incomingIndex === -1) {\n nextMessages.push(existingEntry);\n continue;\n }\n\n usedIncoming.add(incomingIndex);\n nextMessages.push(\n chooseMergedMessageEntry(existingEntry, incomingMessages[incomingIndex]),\n );\n }\n\n for (let index = 0; index < incomingMessages.length; index++) {\n if (!usedIncoming.has(index)) nextMessages.push(incomingMessages[index]);\n }\n\n merged.messages = nextMessages;\n return merged;\n}\n\nfunction shouldReplaceLastAssistant(\n lastMessage: any,\n assistantMsg: AssistantMessage,\n): boolean {\n const lastContent = lastMessage?.content;\n if (messageContentIsEmpty(lastContent)) return true;\n\n const lastRunId = getMessageRunId(lastMessage);\n const nextRunId = getMessageRunId(assistantMsg);\n if (lastRunId && nextRunId && lastRunId === nextRunId) return true;\n if (lastRunId && nextRunId && lastRunId !== nextRunId) return false;\n\n const lastStatus = lastMessage?.status;\n if (lastStatus && !isTerminalAssistantStatus(lastStatus)) return true;\n\n try {\n if (JSON.stringify(lastContent) === JSON.stringify(assistantMsg.content)) {\n return true;\n }\n } catch {\n // Fall through to the text-prefix check.\n }\n\n const lastText = messageText(lastContent).trim();\n const nextText = messageText(assistantMsg.content).trim();\n if (isTerminalAssistantStatus(lastStatus)) return false;\n return Boolean(lastText && nextText && nextText.startsWith(lastText));\n}\n\n/**\n * Merge the server-reconstructed assistant message into persisted\n * assistant-ui thread data.\n *\n * The browser periodically saves thread data while a run is still streaming.\n * That can leave the last assistant message non-empty but partial/pending.\n * Completion must replace that same-run partial message instead of treating\n * any assistant content as proof that the frontend already saved the final\n * turn.\n */\nexport function upsertAssistantMessage(\n repo: any,\n assistantMsg: AssistantMessage,\n): any {\n const nextRepo = repo && typeof repo === \"object\" ? repo : {};\n if (!Array.isArray(nextRepo.messages)) nextRepo.messages = [];\n\n const lastIndex = nextRepo.messages.length - 1;\n const lastEntry = lastIndex >= 0 ? nextRepo.messages[lastIndex] : undefined;\n const lastMsg = getStoredMessage(lastEntry);\n const lastRole = lastMsg?.role;\n const isWrapped = Boolean(lastEntry && \"message\" in lastEntry);\n\n if (\n lastRole === \"assistant\" &&\n shouldReplaceLastAssistant(lastMsg, assistantMsg)\n ) {\n nextRepo.messages[lastIndex] = isWrapped\n ? { ...lastEntry, message: assistantMsg }\n : assistantMsg;\n return nextRepo;\n }\n\n if (isWrapped) {\n const parentId =\n nextRepo.messages.length > 0\n ? (getStoredMessage(nextRepo.messages[nextRepo.messages.length - 1])\n ?.id ?? null)\n : null;\n nextRepo.messages.push({ message: assistantMsg, parentId });\n } else {\n nextRepo.messages.push(assistantMsg);\n }\n return nextRepo;\n}\n\n/**\n * Extract title and preview from a thread runtime export.\n * Isomorphic — works on both server and client.\n */\nexport function extractThreadMeta(repo: any): {\n title: string;\n preview: string;\n} {\n const msgs = repo?.messages;\n if (!Array.isArray(msgs) || msgs.length === 0)\n return { title: \"\", preview: \"\" };\n\n let title = \"\";\n let preview = \"\";\n for (const entry of msgs) {\n // Support both wrapped ({ message: { role, content } }) and flat ({ role, content }) formats\n const msg = entry?.message ?? entry;\n if (msg.role !== \"user\") continue;\n const textParts = Array.isArray(msg.content)\n ? msg.content\n .filter((p: any) => p.type === \"text\")\n .map((p: any) => p.text)\n .join(\" \")\n : typeof msg.content === \"string\"\n ? msg.content\n : \"\";\n if (textParts.trim()) {\n if (!title) title = textParts.trim().slice(0, 80);\n preview = textParts.trim().slice(0, 120);\n }\n }\n return { title, preview };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"thread-data-builder.js","sourceRoot":"","sources":["../../src/agent/thread-data-builder.ts"],"names":[],"mappings":"AAmBA,MAAM,2BAA2B,GAAG,MAAM,CAAC;AAE3C,SAAS,2BAA2B,CAAC,KAIpC;IACC,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IACzD,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC;IACtC,OAAO,CACL,KAAK,CAAC,WAAW,KAAK,IAAI;QAC1B,IAAI,KAAK,uBAAuB;QAChC,IAAI,KAAK,yBAAyB;QAClC,IAAI,KAAK,WAAW;QACpB,IAAI,KAAK,SAAS;QAClB,IAAI,KAAK,eAAe;QACxB,IAAI,KAAK,UAAU;QACnB,I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GAAG,CAAC,CAAC;QAC3C,CAAC;IACH,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AAC5B,CAAC","sourcesContent":["import type { AgentChatAttachment, RunEvent } from \"./types.js\";\n\ninterface ContentPart {\n type: string;\n text?: string;\n toolCallId?: string;\n toolName?: string;\n argsText?: string;\n args?: Record<string, string>;\n result?: string;\n}\n\ninterface BuildAssistantMessageOptions {\n suppressInternalContinuation?: boolean;\n}\n\ntype AssistantMessage = NonNullable<ReturnType<typeof buildAssistantMessage>>;\ntype UserMessage = ReturnType<typeof buildUserMessage>;\n\nconst MAX_STORED_ATTACHMENT_CHARS = 60_000;\n\nfunction isInternalContinuationError(event: {\n error: string;\n errorCode?: string;\n recoverable?: boolean;\n}): boolean {\n const code = String(event.errorCode ?? \"\").toLowerCase();\n const msg = event.error.toLowerCase();\n return (\n event.recoverable === true ||\n code === \"builder_gateway_error\" ||\n code === \"builder_gateway_timeout\" ||\n code === \"stale_run\" ||\n code === \"timeout\" ||\n code === \"timeout_error\" ||\n code === \"http_408\" ||\n code === \"http_429\" ||\n code === \"http_500\" ||\n code === \"http_502\" ||\n code === \"http_503\" ||\n code === \"http_504\" ||\n code === \"rate_limited\" ||\n code === \"too_many_concurrent_requests\" ||\n code === \"overloaded_error\" ||\n msg.includes(\"timeout\") ||\n msg.includes(\"gateway error\") ||\n msg.includes(\"gateway timeout\") ||\n msg.includes(\"inactivity timeout\") ||\n msg.includes(\"stream ended\") ||\n msg.includes(\"stream closed\") ||\n msg.includes(\"temporarily unavailable\") ||\n msg.includes(\"502\") ||\n msg.includes(\"503\") ||\n msg.includes(\"504\") ||\n msg.includes(\"529\")\n );\n}\n\n/**\n * Reconstruct an assistant-ui message from raw agent run events.\n * Mirrors the client-side processEvent logic so the server can persist\n * the assistant's response even if the frontend is disconnected.\n */\nexport function buildAssistantMessage(\n events: RunEvent[],\n runId?: string,\n options: BuildAssistantMessageOptions = {},\n): {\n id: string;\n createdAt: Date;\n role: \"assistant\";\n content: ContentPart[];\n status:\n | { type: \"complete\"; reason: \"stop\" }\n | { type: \"incomplete\"; reason: \"error\" };\n metadata: Record<string, unknown>;\n} | null {\n const content: ContentPart[] = [];\n let toolCallCounter = 0;\n let runError: {\n message: string;\n errorCode?: string;\n details?: string;\n recoverable?: boolean;\n } | null = null;\n let endedAtInternalContinuationBoundary = false;\n\n const appendText = (text: string) => {\n const last = content[content.length - 1];\n if (last && last.type === \"text\") {\n last.text = (last.text ?? \"\") + text;\n } else {\n content.push({ type: \"text\", text });\n }\n };\n\n for (const { event } of events) {\n if (event.type === \"clear\") {\n content.length = 0;\n toolCallCounter = 0;\n continue;\n }\n\n if (event.type === \"text\") {\n appendText(event.text ?? \"\");\n continue;\n }\n\n if (event.type === \"tool_start\") {\n const toolCallId = `tc_${++toolCallCounter}`;\n const args = (event.input ?? {}) as Record<string, string>;\n content.push({\n type: \"tool-call\",\n toolCallId,\n toolName: event.tool ?? \"unknown\",\n argsText: JSON.stringify(args),\n args,\n });\n continue;\n }\n\n if (event.type === \"tool_done\") {\n for (let i = content.length - 1; i >= 0; i--) {\n const part = content[i];\n if (\n part.type === \"tool-call\" &&\n part.toolName === event.tool &&\n part.result === undefined\n ) {\n part.result = event.result ?? \"\";\n break;\n }\n }\n continue;\n }\n\n if (event.type === \"loop_limit\") {\n // Older servers emitted this as a user-visible terminal event. Treat it\n // as an internal continuation boundary when rebuilding persisted turns.\n if (options.suppressInternalContinuation) {\n endedAtInternalContinuationBoundary = true;\n }\n continue;\n }\n\n if (event.type === \"auto_continue\") {\n if (options.suppressInternalContinuation) {\n endedAtInternalContinuationBoundary = true;\n }\n continue;\n }\n\n if (event.type === \"error\") {\n if (\n options.suppressInternalContinuation &&\n isInternalContinuationError(event)\n ) {\n endedAtInternalContinuationBoundary = true;\n continue;\n }\n if (event.errorCode === \"run_timeout\" && event.recoverable) {\n continue;\n }\n runError = {\n message: event.error,\n ...(event.errorCode ? { errorCode: event.errorCode } : {}),\n ...(event.details ? { details: event.details } : {}),\n ...(event.recoverable ? { recoverable: event.recoverable } : {}),\n };\n appendText(`${content.length > 0 ? \"\\n\\n\" : \"\"}Error: ${event.error}`);\n continue;\n }\n\n // done, missing_api_key — terminal signals, not content\n }\n\n if (content.length === 0 || endedAtInternalContinuationBoundary) return null;\n\n const metadata: Record<string, unknown> = {};\n if (runId) metadata.runId = runId;\n if (runError) {\n metadata.custom = {\n runError: {\n ...runError,\n ...(runId ? { runId } : {}),\n },\n };\n }\n\n return {\n id: `server-${runId ?? Date.now()}`,\n createdAt: new Date(),\n role: \"assistant\",\n content,\n status: runError\n ? { type: \"incomplete\" as const, reason: \"error\" as const }\n : { type: \"complete\" as const, reason: \"stop\" as const },\n metadata,\n };\n}\n\nfunction getStoredMessage(entry: any): any {\n return entry?.message ?? entry;\n}\n\nfunction getMessageRunId(message: any): string | undefined {\n const meta = message?.metadata;\n const direct = meta?.runId;\n const custom = meta?.custom?.runId;\n const errorRun = meta?.custom?.runError?.runId ?? meta?.runError?.runId;\n if (typeof direct === \"string\") return direct;\n if (typeof custom === \"string\") return custom;\n if (typeof errorRun === \"string\") return errorRun;\n return undefined;\n}\n\nfunction messageContentIsEmpty(content: unknown): boolean {\n if (Array.isArray(content)) return content.length === 0;\n return content == null || content === \"\";\n}\n\nfunction messageText(content: unknown): string {\n if (typeof content === \"string\") return content;\n if (!Array.isArray(content)) return \"\";\n return content\n .filter(\n (part: any) => part?.type === \"text\" && typeof part.text === \"string\",\n )\n .map((part: any) => part.text)\n .join(\"\");\n}\n\nfunction isTerminalAssistantStatus(status: unknown): boolean {\n const type = (status as { type?: unknown } | undefined)?.type;\n return type === \"complete\" || type === \"incomplete\";\n}\n\nfunction normalizeAttachmentIdentity(attachments: unknown): unknown {\n if (!Array.isArray(attachments)) return undefined;\n return attachments.map((att: any) => ({\n type: att?.type,\n name: att?.name,\n contentType: att?.contentType,\n }));\n}\n\nfunction messageIdentityKeys(message: any): string[] {\n const keys: string[] = [];\n if (typeof message?.id === \"string\" && message.id) {\n keys.push(`id:${message.id}`);\n }\n const runId = getMessageRunId(message);\n if (runId) keys.push(`run:${runId}`);\n\n try {\n keys.push(\n `fingerprint:${JSON.stringify({\n role: message?.role,\n content: message?.content,\n attachments: message?.attachments,\n })}`,\n );\n } catch {\n // Best effort. id/runId usually exist for persisted assistant-ui rows.\n }\n if (message?.role === \"user\") {\n try {\n keys.push(\n `user-fingerprint:${JSON.stringify({\n role: message.role,\n content: message.content,\n attachments: normalizeAttachmentIdentity(message.attachments),\n })}`,\n );\n } catch {\n // Same best-effort behavior as the full fingerprint.\n }\n }\n return keys;\n}\n\nfunction messagesMatch(a: any, b: any): boolean {\n const bKeys = new Set(messageIdentityKeys(b));\n return messageIdentityKeys(a).some((key) => bKeys.has(key));\n}\n\nfunction chooseMergedMessageEntry(existingEntry: any, incomingEntry: any): any {\n const existing = getStoredMessage(existingEntry);\n const incoming = getStoredMessage(incomingEntry);\n if (\n existing?.role === \"assistant\" &&\n incoming?.role === \"assistant\" &&\n isTerminalAssistantStatus(existing?.status) &&\n !isTerminalAssistantStatus(incoming?.status)\n ) {\n return existingEntry;\n }\n return incomingEntry;\n}\n\n/**\n * Merge an incoming client-side full-thread save over the current SQL copy.\n *\n * The browser exports and PUTs the whole assistant-ui repository. If a server\n * completion save lands first, an older browser export can otherwise replace\n * `thread_data` wholesale and delete the assistant message the server just\n * reconstructed from run events. Preserve server-only messages while still\n * accepting client-only messages and metadata.\n */\nexport function mergeThreadDataForClientSave(\n existingRepo: any,\n incomingRepo: any,\n) {\n const merged =\n incomingRepo && typeof incomingRepo === \"object\" ? incomingRepo : {};\n if (\n existingRepo &&\n typeof existingRepo === \"object\" &&\n existingRepo.queuedMessages !== undefined &&\n merged.queuedMessages === undefined\n ) {\n merged.queuedMessages = existingRepo.queuedMessages;\n }\n\n const existingMessages = Array.isArray(existingRepo?.messages)\n ? existingRepo.messages\n : null;\n const incomingMessages = Array.isArray(merged.messages)\n ? merged.messages\n : null;\n if (!existingMessages || !incomingMessages) return merged;\n\n const incomingKeySets = incomingMessages.map(\n (entry: any) => new Set(messageIdentityKeys(getStoredMessage(entry))),\n );\n const usedIncoming = new Set<number>();\n const nextMessages: any[] = [];\n\n for (const existingEntry of existingMessages) {\n const existingKeys = messageIdentityKeys(getStoredMessage(existingEntry));\n const incomingIndex = incomingKeySets.findIndex(\n (keys, index) =>\n !usedIncoming.has(index) && existingKeys.some((key) => keys.has(key)),\n );\n\n if (incomingIndex === -1) {\n nextMessages.push(existingEntry);\n continue;\n }\n\n usedIncoming.add(incomingIndex);\n nextMessages.push(\n chooseMergedMessageEntry(existingEntry, incomingMessages[incomingIndex]),\n );\n }\n\n for (let index = 0; index < incomingMessages.length; index++) {\n if (!usedIncoming.has(index)) nextMessages.push(incomingMessages[index]);\n }\n\n merged.messages = nextMessages;\n return merged;\n}\n\nfunction escapeAttachmentAttribute(value: string): string {\n return value.replace(/&/g, \"&\").replace(/\"/g, \""\");\n}\n\nfunction unwrapTextAttachmentEnvelope(text: string): string {\n const match = text.match(\n /^<attachment\\b[^>]*>\\n?([\\s\\S]*?)\\n?<\\/attachment>$/,\n );\n return match ? match[1] : text;\n}\n\nfunction truncateStoredAttachment(text: string): string {\n const unwrapped = unwrapTextAttachmentEnvelope(text);\n if (unwrapped.length <= MAX_STORED_ATTACHMENT_CHARS) return unwrapped;\n const omitted = unwrapped.length - MAX_STORED_ATTACHMENT_CHARS;\n return `${unwrapped.slice(0, MAX_STORED_ATTACHMENT_CHARS)}\\n\\n[Attachment truncated after ${MAX_STORED_ATTACHMENT_CHARS.toLocaleString()} characters; ${omitted.toLocaleString()} characters omitted from persisted chat history.]`;\n}\n\nfunction textAttachmentEnvelope(\n att: AgentChatAttachment,\n text: string,\n): string {\n const attrs = [\n `name=\"${escapeAttachmentAttribute(att.name || \"attachment\")}\"`,\n att.contentType\n ? `contentType=\"${escapeAttachmentAttribute(att.contentType)}\"`\n : null,\n att.type ? `type=\"${escapeAttachmentAttribute(att.type)}\"` : null,\n ].filter(Boolean);\n return `<attachment ${attrs.join(\" \")}>\\n${truncateStoredAttachment(text)}\\n</attachment>`;\n}\n\nfunction buildStoredAttachments(\n attachments: AgentChatAttachment[] | undefined,\n runId: string | undefined,\n): any[] {\n return (attachments ?? [])\n .map((att, index) => {\n const id = `server-${runId ?? Date.now()}-attachment-${index}`;\n if (att.type === \"image\" && att.data) {\n return {\n id,\n type: \"image\",\n name: att.name,\n contentType: att.contentType,\n status: { type: \"complete\" },\n content: [{ type: \"image\", image: att.data }],\n };\n }\n if (att.data) {\n return {\n id,\n type: \"file\",\n name: att.name,\n contentType: att.contentType,\n status: { type: \"complete\" },\n content: [\n {\n type: \"file\",\n data: att.data,\n mimeType: att.contentType,\n filename: att.name,\n },\n ],\n };\n }\n if (typeof att.text === \"string\" && att.text.length > 0) {\n return {\n id,\n type: \"file\",\n name: att.name,\n contentType: att.contentType,\n status: { type: \"complete\" },\n content: [\n { type: \"text\", text: textAttachmentEnvelope(att, att.text) },\n ],\n };\n }\n return null;\n })\n .filter(Boolean);\n}\n\nexport function buildUserMessage(opts: {\n text: string;\n attachments?: AgentChatAttachment[];\n runId?: string;\n createdAt?: Date;\n}): {\n id: string;\n createdAt: Date;\n role: \"user\";\n content: ContentPart[];\n attachments?: any[];\n metadata: Record<string, unknown>;\n} {\n const attachments = buildStoredAttachments(opts.attachments, opts.runId);\n return {\n id: `server-user-${opts.runId ?? Date.now()}`,\n createdAt: opts.createdAt ?? new Date(),\n role: \"user\",\n content: [{ type: \"text\", text: opts.text }],\n ...(attachments.length > 0 ? { attachments } : {}),\n metadata: {\n custom: {\n submittedRunId: opts.runId,\n },\n },\n };\n}\n\nexport function upsertUserMessage(repo: any, userMsg: UserMessage): any {\n const nextRepo = repo && typeof repo === \"object\" ? repo : {};\n if (!Array.isArray(nextRepo.messages)) nextRepo.messages = [];\n\n const lastIndex = nextRepo.messages.length - 1;\n const lastEntry = lastIndex >= 0 ? nextRepo.messages[lastIndex] : undefined;\n const lastMsg = getStoredMessage(lastEntry);\n if (lastMsg?.role === \"user\" && messagesMatch(lastMsg, userMsg)) {\n return nextRepo;\n }\n\n const isWrapped = Boolean(lastEntry && \"message\" in lastEntry);\n if (isWrapped) {\n const parentId =\n lastIndex >= 0 ? (getStoredMessage(lastEntry)?.id ?? null) : null;\n nextRepo.messages.push({ message: userMsg, parentId });\n } else {\n nextRepo.messages.push(userMsg);\n }\n return nextRepo;\n}\n\nfunction shouldReplaceLastAssistant(\n lastMessage: any,\n assistantMsg: AssistantMessage,\n): boolean {\n const lastContent = lastMessage?.content;\n if (messageContentIsEmpty(lastContent)) return true;\n\n const lastRunId = getMessageRunId(lastMessage);\n const nextRunId = getMessageRunId(assistantMsg);\n if (lastRunId && nextRunId && lastRunId === nextRunId) return true;\n if (lastRunId && nextRunId && lastRunId !== nextRunId) return false;\n\n const lastStatus = lastMessage?.status;\n if (lastStatus && !isTerminalAssistantStatus(lastStatus)) return true;\n\n try {\n if (JSON.stringify(lastContent) === JSON.stringify(assistantMsg.content)) {\n return true;\n }\n } catch {\n // Fall through to the text-prefix check.\n }\n\n const lastText = messageText(lastContent).trim();\n const nextText = messageText(assistantMsg.content).trim();\n if (isTerminalAssistantStatus(lastStatus)) return false;\n return Boolean(lastText && nextText && nextText.startsWith(lastText));\n}\n\n/**\n * Merge the server-reconstructed assistant message into persisted\n * assistant-ui thread data.\n *\n * The browser periodically saves thread data while a run is still streaming.\n * That can leave the last assistant message non-empty but partial/pending.\n * Completion must replace that same-run partial message instead of treating\n * any assistant content as proof that the frontend already saved the final\n * turn.\n */\nexport function upsertAssistantMessage(\n repo: any,\n assistantMsg: AssistantMessage,\n): any {\n const nextRepo = repo && typeof repo === \"object\" ? repo : {};\n if (!Array.isArray(nextRepo.messages)) nextRepo.messages = [];\n\n const lastIndex = nextRepo.messages.length - 1;\n const lastEntry = lastIndex >= 0 ? nextRepo.messages[lastIndex] : undefined;\n const lastMsg = getStoredMessage(lastEntry);\n const lastRole = lastMsg?.role;\n const isWrapped = Boolean(lastEntry && \"message\" in lastEntry);\n\n if (\n lastRole === \"assistant\" &&\n shouldReplaceLastAssistant(lastMsg, assistantMsg)\n ) {\n nextRepo.messages[lastIndex] = isWrapped\n ? { ...lastEntry, message: assistantMsg }\n : assistantMsg;\n return nextRepo;\n }\n\n if (isWrapped) {\n const parentId =\n nextRepo.messages.length > 0\n ? (getStoredMessage(nextRepo.messages[nextRepo.messages.length - 1])\n ?.id ?? null)\n : null;\n nextRepo.messages.push({ message: assistantMsg, parentId });\n } else {\n nextRepo.messages.push(assistantMsg);\n }\n return nextRepo;\n}\n\n/**\n * Extract title and preview from a thread runtime export.\n * Isomorphic — works on both server and client.\n */\nexport function extractThreadMeta(repo: any): {\n title: string;\n preview: string;\n} {\n const msgs = repo?.messages;\n if (!Array.isArray(msgs) || msgs.length === 0)\n return { title: \"\", preview: \"\" };\n\n let title = \"\";\n let preview = \"\";\n for (const entry of msgs) {\n // Support both wrapped ({ message: { role, content } }) and flat ({ role, content }) formats\n const msg = entry?.message ?? entry;\n if (msg.role !== \"user\") continue;\n const textParts = Array.isArray(msg.content)\n ? msg.content\n .filter((p: any) => p.type === \"text\")\n .map((p: any) => p.text)\n .join(\" \")\n : typeof msg.content === \"string\"\n ? msg.content\n : \"\";\n if (textParts.trim()) {\n if (!title) title = textParts.trim().slice(0, 80);\n preview = textParts.trim().slice(0, 120);\n }\n }\n return { title, preview };\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-chat-adapter.d.ts","sourceRoot":"","sources":["../../src/client/agent-chat-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAsB,MAAM,qBAAqB,CAAC;AAehF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"agent-chat-adapter.d.ts","sourceRoot":"","sources":["../../src/client/agent-chat-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAsB,MAAM,qBAAqB,CAAC;AAehF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAsgBrE;;;;GAIG;AACH;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,CAAC,EAAE;IAC/C,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE;QAAE,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAC;IAC3C,SAAS,CAAC,EAAE;QAAE,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAC;IAC5C,SAAS,CAAC,EAAE;QAAE,OAAO,EAAE,eAAe,GAAG,SAAS,CAAA;KAAE,CAAC;IACrD,WAAW,CAAC,EAAE;QAAE,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAAA;KAAE,CAAC;CACzD,GAAG,gBAAgB,CA80BnB"}
|
|
@@ -115,10 +115,10 @@ function extractAttachmentsFromMessage(message) {
|
|
|
115
115
|
name: att.name,
|
|
116
116
|
contentType,
|
|
117
117
|
...(decodedText !== null
|
|
118
|
-
? { text: decodedText }
|
|
118
|
+
? { text: truncateOutboundAttachment(decodedText) }
|
|
119
119
|
: part.data.startsWith("data:")
|
|
120
120
|
? { data: part.data }
|
|
121
|
-
: { text: part.data }),
|
|
121
|
+
: { text: truncateOutboundAttachment(part.data) }),
|
|
122
122
|
});
|
|
123
123
|
}
|
|
124
124
|
else if (part.type === "text" && typeof part.text === "string") {
|
|
@@ -126,7 +126,7 @@ function extractAttachmentsFromMessage(message) {
|
|
|
126
126
|
type: "file",
|
|
127
127
|
name: att.name,
|
|
128
128
|
contentType: att.contentType,
|
|
129
|
-
text: unwrapAttachmentEnvelope(part.text),
|
|
129
|
+
text: truncateOutboundAttachment(unwrapAttachmentEnvelope(part.text)),
|
|
130
130
|
});
|
|
131
131
|
}
|
|
132
132
|
}
|
|
@@ -139,6 +139,12 @@ function truncateHistoryAttachment(text) {
|
|
|
139
139
|
const omitted = text.length - MAX_HISTORY_ATTACHMENT_CHARS;
|
|
140
140
|
return `${text.slice(0, MAX_HISTORY_ATTACHMENT_CHARS)}\n\n[Attachment truncated after ${MAX_HISTORY_ATTACHMENT_CHARS.toLocaleString()} characters; ${omitted.toLocaleString()} characters omitted from prior chat history.]`;
|
|
141
141
|
}
|
|
142
|
+
function truncateOutboundAttachment(text) {
|
|
143
|
+
if (text.length <= MAX_HISTORY_ATTACHMENT_CHARS)
|
|
144
|
+
return text;
|
|
145
|
+
const omitted = text.length - MAX_HISTORY_ATTACHMENT_CHARS;
|
|
146
|
+
return `${text.slice(0, MAX_HISTORY_ATTACHMENT_CHARS)}\n\n[Attachment truncated after ${MAX_HISTORY_ATTACHMENT_CHARS.toLocaleString()} characters; ${omitted.toLocaleString()} characters omitted from the submitted attachment.]`;
|
|
147
|
+
}
|
|
142
148
|
function attachmentHistoryText(attachment) {
|
|
143
149
|
if (typeof attachment.text === "string" && attachment.text.length > 0) {
|
|
144
150
|
const attrs = [
|
|
@@ -473,6 +479,7 @@ export function createAgentChatAdapter(options) {
|
|
|
473
479
|
let currentStructuredHistory = structuredHistory;
|
|
474
480
|
let includeAttachments = attachments.length > 0;
|
|
475
481
|
let includeReferences = Boolean(runConfig?.custom?.references);
|
|
482
|
+
let internalContinuationRequest = false;
|
|
476
483
|
let startupRecoveryAttempts = 0;
|
|
477
484
|
let staleRunContinuationAttempts = 0;
|
|
478
485
|
let stalledTransientContinuationAttempts = 0;
|
|
@@ -761,6 +768,7 @@ export function createAgentChatAdapter(options) {
|
|
|
761
768
|
// with nothing attached after a stale run or reconnect recovery.
|
|
762
769
|
includeAttachments = attachments.length > 0;
|
|
763
770
|
includeReferences = Boolean(runConfig?.custom?.references);
|
|
771
|
+
internalContinuationRequest = true;
|
|
764
772
|
startupRecoveryAttempts = 0;
|
|
765
773
|
clearActiveRun();
|
|
766
774
|
if (!isTransient) {
|
|
@@ -780,9 +788,13 @@ export function createAgentChatAdapter(options) {
|
|
|
780
788
|
headers,
|
|
781
789
|
body: JSON.stringify({
|
|
782
790
|
message: currentMessageText,
|
|
791
|
+
displayMessage: userMessageText,
|
|
783
792
|
history: currentHistory,
|
|
784
793
|
structuredHistory: currentStructuredHistory,
|
|
785
794
|
...(threadId ? { threadId } : {}),
|
|
795
|
+
...(internalContinuationRequest
|
|
796
|
+
? { internalContinuation: true }
|
|
797
|
+
: {}),
|
|
786
798
|
...(requestMode ? { mode: requestMode } : {}),
|
|
787
799
|
...(modelRef?.current ? { model: modelRef.current } : {}),
|
|
788
800
|
...(engineRef?.current ? { engine: engineRef.current } : {}),
|