@rpcbase/agents-sdk 0.7.0 → 0.9.0
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/api/chat/handler.d.ts.map +1 -1
- package/dist/api/chat/historyCompaction.d.ts +29 -0
- package/dist/api/chat/historyCompaction.d.ts.map +1 -0
- package/dist/api/chat/models.d.ts +1 -0
- package/dist/api/chat/models.d.ts.map +1 -1
- package/dist/api/chat/runtimeConfig.d.ts +43 -0
- package/dist/api/chat/runtimeConfig.d.ts.map +1 -1
- package/dist/api/chatDb.d.ts +4 -0
- package/dist/api/chatDb.d.ts.map +1 -1
- package/dist/chat-models.browser.js +2 -2
- package/dist/chat-models.d.ts +1 -1
- package/dist/chat-models.d.ts.map +1 -1
- package/dist/chat-models.js +2 -2
- package/dist/{chatDb-DdX0Vtyt.js → chatDb-BSiHIyn9.js} +25 -18
- package/dist/{chatDb-CcAjAGX7.js → chatDb-YfeABtn-.js} +25 -18
- package/dist/client-routes.browser.js +94 -75
- package/dist/client-routes.d.ts +7 -1
- package/dist/client-routes.d.ts.map +1 -1
- package/dist/client-routes.js +94 -75
- package/dist/components/agents-chat-route-layout.d.ts +5 -2
- package/dist/components/agents-chat-route-layout.d.ts.map +1 -1
- package/dist/components/artifact-messages.d.ts +3 -2
- package/dist/components/artifact-messages.d.ts.map +1 -1
- package/dist/components/artifact.d.ts +6 -2
- package/dist/components/artifact.d.ts.map +1 -1
- package/dist/components/chat.d.ts +6 -2
- package/dist/components/chat.d.ts.map +1 -1
- package/dist/components/message.d.ts +3 -2
- package/dist/components/message.d.ts.map +1 -1
- package/dist/components/messages.d.ts +3 -2
- package/dist/components/messages.d.ts.map +1 -1
- package/dist/components/multimodal-input.d.ts +4 -1
- package/dist/components/multimodal-input.d.ts.map +1 -1
- package/dist/components/sidebar-history-item.d.ts +4 -5
- package/dist/components/sidebar-history-item.d.ts.map +1 -1
- package/dist/components/sidebar-history.d.ts +6 -3
- package/dist/components/sidebar-history.d.ts.map +1 -1
- package/dist/{data-stream-handler-BW4eCG7d.js → data-stream-handler-DXAHFC5k.js} +462 -427
- package/dist/{data-stream-handler-Bd_oM4r9.js → data-stream-handler-lr4wJ3vt.js} +462 -427
- package/dist/{handler-D7f8iAy5.js → handler-5n-YwiF3.js} +1 -1
- package/dist/{handler-CGV7uuUA.js → handler-B3uUDuTR.js} +1 -1
- package/dist/{handler-BLG0AYVC.js → handler-BMeUL2iE.js} +1 -1
- package/dist/{handler-C6u7vLAb.js → handler-BgYHL3BZ.js} +9 -9
- package/dist/{handler-dVlpopfq.js → handler-By86dyTJ.js} +1 -1
- package/dist/{handler-BeWk8pDD.js → handler-C0MO52d9.js} +1 -1
- package/dist/{handler-mbDBRYl8.js → handler-C8gaYgmy.js} +1 -1
- package/dist/{handler-ClWOGjB_.js → handler-DE8agLBS.js} +438 -268
- package/dist/{handler-CEk1hut1.js → handler-DMwBT4bK.js} +1 -1
- package/dist/{handler-DPuJK6ft.js → handler-DSncMeo4.js} +352 -182
- package/dist/{handler-BY5bgBEh.js → handler-DdKB5P3H.js} +1 -1
- package/dist/{handler-BBCbPLLC.js → handler-Dy536I3-.js} +1 -1
- package/dist/{handler-BKT8Hl0a.js → handler-Jua6SU39.js} +9 -9
- package/dist/{handler-CZSMkZnQ.js → handler-YmuAR8ei.js} +1 -1
- package/dist/{handler-P40GjJry.js → handler-hah8tVEE.js} +1 -1
- package/dist/{handler-DbjPd3w4.js → handler-svaSKC2q.js} +1 -1
- package/dist/index.browser.js +4 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/lib/ai/models.d.ts +6 -0
- package/dist/lib/ai/models.d.ts.map +1 -1
- package/dist/lib/types.d.ts +23 -0
- package/dist/lib/types.d.ts.map +1 -1
- package/dist/{models-DUt7W9lV.js → models-DphUIx22.js} +13 -2
- package/dist/pages/ChatNewPage.d.ts +6 -1
- package/dist/pages/ChatNewPage.d.ts.map +1 -1
- package/dist/pages/ChatPage.d.ts +6 -1
- package/dist/pages/ChatPage.d.ts.map +1 -1
- package/dist/routes.browser.js +8 -8
- package/dist/routes.js +8 -8
- package/dist/runtime.d.ts +2 -2
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +1 -1
- package/dist/tailwind-source.js +1 -1
- package/package.json +1 -1
|
@@ -2,64 +2,203 @@ import { c as e } from "./chunk-BO2N2NFS-BDR8fnNb.js";
|
|
|
2
2
|
import { r as t } from "./openai-reasoning-kFq60GX9.js";
|
|
3
3
|
import { a as n, i as r, t as i } from "./chatCtx-BWOFI93u.js";
|
|
4
4
|
import { t as a } from "./apiErrors-FdS4Eh1H.js";
|
|
5
|
-
import { _ as o, a as s, b as c, g as l, h as u, l as d, m as f, n as p, p as m, s as h, v as g,
|
|
5
|
+
import { _ as o, a as s, b as c, g as l, h as u, l as d, m as f, n as p, p as m, s as h, v as g, x as _, y as ee } from "./chatDb-BSiHIyn9.js";
|
|
6
6
|
import { n as v, t as y } from "./resumableSse-BprFOR5Q.js";
|
|
7
|
-
import { Output as b, convertToModelMessages as
|
|
7
|
+
import { Output as b, convertToModelMessages as te, createUIMessageStream as ne, extractReasoningMiddleware as re, generateText as ie, smoothStream as ae, stepCountIs as oe, streamObject as x, streamText as S, tool as C, wrapLanguageModel as w } from "ai";
|
|
8
8
|
import { getTenantFilesystemDb as T } from "@rpcbase/db";
|
|
9
9
|
import { createHash as E, randomUUID as D } from "node:crypto";
|
|
10
|
-
import { GridFSBucket as
|
|
11
|
-
import { createGoogleGenerativeAI as
|
|
10
|
+
import { GridFSBucket as se, ObjectId as O } from "mongodb";
|
|
11
|
+
import { createGoogleGenerativeAI as ce } from "@ai-sdk/google";
|
|
12
12
|
import { openai as k } from "@ai-sdk/openai";
|
|
13
|
-
import { createXai as
|
|
13
|
+
import { createXai as le } from "@ai-sdk/xai";
|
|
14
14
|
import { z as A } from "zod";
|
|
15
15
|
//#region src/api/chat/models.ts
|
|
16
|
-
var j = /-thinking$/, M = "openai/", N = "google/", P = "xai/",
|
|
16
|
+
var j = /-thinking$/, M = "openai/", N = "google/", P = "xai/", F = new Set([
|
|
17
17
|
"openai/gpt-5.4-mini",
|
|
18
18
|
"openai/gpt-5.5",
|
|
19
19
|
"google/gemini-3.5-flash",
|
|
20
20
|
"xai/grok-4.3-thinking"
|
|
21
|
-
]),
|
|
21
|
+
]), ue = (e) => e.startsWith(M), de = (e) => e.startsWith(N), fe = (e) => e.startsWith(P), pe = (e) => e.endsWith("-thinking"), me = (e) => e.includes("/") ? ue(e) || de(e) || fe(e) : !1, I = le({
|
|
22
22
|
baseURL: process.env.XAI_BASE_URL ?? "https://api.x.ai/v1",
|
|
23
23
|
apiKey: process.env.XAI_API_KEY
|
|
24
|
-
}),
|
|
25
|
-
if (
|
|
26
|
-
if (
|
|
27
|
-
let t = e.slice(N.length).replace(j, ""), n =
|
|
28
|
-
return
|
|
24
|
+
}), L = ce({ apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY ?? process.env.GOOGLE_API_KEY }), he = (e) => {
|
|
25
|
+
if (ue(e)) return k.responses(e.slice(M.length));
|
|
26
|
+
if (de(e)) {
|
|
27
|
+
let t = e.slice(N.length).replace(j, ""), n = L.chat(t);
|
|
28
|
+
return pe(e) ? w({
|
|
29
29
|
model: n,
|
|
30
|
-
middleware:
|
|
30
|
+
middleware: re({ tagName: "thinking" })
|
|
31
31
|
}) : n;
|
|
32
32
|
}
|
|
33
|
-
if (
|
|
33
|
+
if (fe(e)) {
|
|
34
34
|
let t = e.slice(P.length).replace(j, "");
|
|
35
|
-
return
|
|
35
|
+
return pe(e) ? I.responses(t) : I.chat(t);
|
|
36
36
|
}
|
|
37
37
|
throw Error(`Unsupported modelId: ${e}`);
|
|
38
|
-
},
|
|
38
|
+
}, ge = () => k.chat("gpt-5.4-mini"), R = () => k.chat(process.env.AGENT_SDK_ARTIFACT_MODEL ?? "gpt-5.4-mini"), z = "\nArtifacts is a special user interface mode that helps users with writing, editing, and other content creation tasks. When artifact is open, it is on the right side of the screen, while the conversation is on the left side. When creating, editing, or updating documents, changes are reflected in real-time on the artifacts and visible to the user.\n\nWhen asked to write code, always use artifacts. When writing code, specify the language in the backticks, e.g. ```python`code here```. The default language is Python. Other languages are not yet supported, so let the user know if they request a different language.\n\nCritical rules:\n- Only call one artifact tool per response.\n- Do not update or edit a document immediately after creating it. Wait for user feedback or an explicit request to modify it.\n- After creating, editing, or updating an artifact, do not repeat the full artifact content in chat. The user can already see it.\n\nThis is a guide for using artifacts tools: `createDocument`, `editDocument`, `updateDocument`, and `requestSuggestions`, which render content on artifacts beside the conversation.\n\n**When to use `createDocument`:**\n- For substantial content (>10 lines) or code\n- For content users will likely save/reuse (emails, code, essays, etc.)\n- When explicitly requested to create a document\n- For when content contains a single code snippet\n- Use kind \"code\" for programming, \"text\" for writing, and \"sheet\" for tabular/CSV data\n\n**When NOT to use `createDocument`:**\n- For informational/explanatory content\n- For conversational responses\n- When asked to keep it in chat\n\n**Using `editDocument`:**\n- Prefer this for targeted edits to an existing artifact\n- For scripts: fixing bugs, adding/removing lines, renaming variables, adding logs\n- For documents: fixing typos, rewording paragraphs, inserting or replacing sections\n- Provide an exact `old_string` and `new_string`\n- Include enough surrounding context in `old_string` to make the match unique\n- Use `replace_all: true` only for deliberate repeated replacements, like renaming a variable everywhere\n\n**Using `updateDocument`:**\n- Default to full document rewrites for major changes\n- Use `editDocument` instead for specific, isolated changes\n- Follow user instructions for which parts to modify\n\n**When NOT to use `editDocument` or `updateDocument`:**\n- Immediately after creating a document\n- Without an explicit user request to modify the artifact\n\n**Using `requestSuggestions`:**\n- ONLY use when the user explicitly asks for suggestions on an existing document\n- Requires a valid document ID from a previously created document\n- Never use for general questions or information requests\n", B = "You are a friendly assistant! Keep your responses concise and helpful.\n\nWhen asked to write, create, or help with something, just do it directly. Don't ask clarifying questions unless absolutely necessary - make reasonable assumptions and proceed with the task.", V = (e) => `\
|
|
39
39
|
About the origin of user's request:
|
|
40
40
|
- lat: ${e.latitude ?? "unknown"}
|
|
41
41
|
- lon: ${e.longitude ?? "unknown"}
|
|
42
42
|
- city: ${e.city ?? "unknown"}
|
|
43
43
|
- country: ${e.country ?? "unknown"}
|
|
44
|
-
`,
|
|
45
|
-
let i =
|
|
44
|
+
`, _e = ({ selectedChatModel: e, requestHints: t, agentInstructions: n, knowledge: r }) => {
|
|
45
|
+
let i = V(t), a = [n?.trim() ? `Agent instructions:\n${n.trim()}` : "", r?.trim() ? `Knowledge context:\n${r.trim()}` : ""].filter(Boolean).join("\n\n");
|
|
46
46
|
if (e.includes("reasoning") || e.includes("thinking")) {
|
|
47
|
-
let t = e.startsWith("google/") && e.includes("thinking") ? `${
|
|
47
|
+
let t = e.startsWith("google/") && e.includes("thinking") ? `${B}\n\nWhen you reason, put your reasoning inside <thinking>...</thinking> tags. Then provide your final answer outside of those tags.\n\n${i}` : `${B}\n\n${i}`;
|
|
48
48
|
return a ? `${t}\n\n${a}` : t;
|
|
49
49
|
}
|
|
50
|
-
let o = `${
|
|
50
|
+
let o = `${B}\n\n${i}\n\n${z}`;
|
|
51
51
|
return a ? `${o}\n\n${a}` : o;
|
|
52
|
-
},
|
|
52
|
+
}, ve = "\nYou are a Python code generator that creates self-contained, executable code snippets. When writing code:\n\n1. Each snippet should be complete and runnable on its own\n2. Prefer using print() statements to display outputs\n3. Include helpful comments explaining the code\n4. Keep snippets concise (generally under 15 lines)\n5. Avoid external dependencies - use Python standard library\n6. Handle potential errors gracefully\n7. Return meaningful output that demonstrates the code's functionality\n8. Don't use input() or other interactive functions\n9. Don't access files or network resources\n10. Don't use infinite loops\n\nExamples of good snippets:\n\n# Calculate factorial iteratively\ndef factorial(n):\n result = 1\n for i in range(1, n + 1):\n result *= i\n return result\n\nprint(f\"Factorial of 5 is: {factorial(5)}\")\n", ye = "\nYou are a spreadsheet creation assistant. Create a spreadsheet in csv format based on the given prompt. The spreadsheet should contain meaningful column headers and data.\n", be = (e, t) => {
|
|
53
53
|
let n = "document";
|
|
54
54
|
return t === "code" ? n = "script" : t === "sheet" && (n = "spreadsheet"), `Rewrite the following ${n} based on the given prompt.
|
|
55
55
|
|
|
56
56
|
${e}`;
|
|
57
|
-
},
|
|
57
|
+
}, xe = "Generate a short chat title (2-5 words) summarizing the user's message.\n\nOutput ONLY the title text. No prefixes, no formatting.\n\nExamples:\n- \"what's the weather in nyc\" → Weather in NYC\n- \"help me write an essay about space\" → Space Essay Help\n- \"hi\" → (empty string)\n- \"debug my python code\" → Python Debugging\n\nBad outputs (never do this):\n- \"# Space Essay\" (no hashtags)\n- \"Title: Weather\" (no prefixes)\n- \"\"NYC Weather\"\" (no quotes)\n\nRules:\n- Maximum 30 characters\n- No quotes, colons, hashtags, or markdown\n- Just the topic/intent, not a full sentence", Se = 24, Ce = 8e4, we = 16e3, Te = "agent-runtime-history-compaction-summary", H = (e) => !!e && typeof e == "object" && !Array.isArray(e), U = (e) => typeof e == "string" && e.trim() ? e.trim() : null, Ee = (e) => {
|
|
58
|
+
let t = U((H(e.metadata) ? e.metadata : null)?.createdAt);
|
|
59
|
+
if (!t) return null;
|
|
60
|
+
let n = new Date(t);
|
|
61
|
+
return Number.isNaN(n.getTime()) ? null : t;
|
|
62
|
+
}, W = (e, t = 2e3) => {
|
|
63
|
+
if (typeof e == "string") return e.length > t ? `${e.slice(0, t)}...` : e;
|
|
64
|
+
if (typeof e == "number" || typeof e == "boolean") return String(e);
|
|
65
|
+
if (!e || typeof e != "object") return "";
|
|
66
|
+
try {
|
|
67
|
+
let n = JSON.stringify(e);
|
|
68
|
+
return n.length > t ? `${n.slice(0, t)}...` : n;
|
|
69
|
+
} catch {
|
|
70
|
+
return "[unserializable content]";
|
|
71
|
+
}
|
|
72
|
+
}, De = (e) => (Array.isArray(e.parts) ? e.parts : []).flatMap((e) => {
|
|
73
|
+
if (!H(e)) {
|
|
74
|
+
let t = W(e);
|
|
75
|
+
return t ? [t] : [];
|
|
76
|
+
}
|
|
77
|
+
let t = U(e.type) ?? "part", n = W(e.text);
|
|
78
|
+
if (n) return [`${t}: ${n}`];
|
|
79
|
+
let r = W(e.output);
|
|
80
|
+
if (r) return [`${t}: ${r}`];
|
|
81
|
+
let i = W(e.result);
|
|
82
|
+
if (i) return [`${t}: ${i}`];
|
|
83
|
+
let a = W(e.content);
|
|
84
|
+
if (a) return [`${t}: ${a}`];
|
|
85
|
+
let o = W(e, 1e3);
|
|
86
|
+
return o ? [`${t}: ${o}`] : [];
|
|
87
|
+
}).join("\n").trim(), Oe = (e) => e.reduce((e, t) => e + De(t).length, 0), ke = (e, t) => {
|
|
88
|
+
if (e.length <= t) return e;
|
|
89
|
+
if (t <= 32) return e.slice(0, t);
|
|
90
|
+
let n = Math.floor(t * .7), r = Math.max(0, t - n - 24);
|
|
91
|
+
return `${e.slice(0, n)}\n...[compacted]...\n${e.slice(e.length - r)}`;
|
|
92
|
+
}, Ae = (e) => e ? {
|
|
93
|
+
maxMessages: Number.isFinite(e.maxMessages) && Number(e.maxMessages) > 0 ? Math.floor(Number(e.maxMessages)) : Se,
|
|
94
|
+
maxEstimatedChars: Number.isFinite(e.maxEstimatedChars) && Number(e.maxEstimatedChars) > 0 ? Math.floor(Number(e.maxEstimatedChars)) : Ce,
|
|
95
|
+
maxSummaryChars: Number.isFinite(e.maxSummaryChars) && Number(e.maxSummaryChars) > 0 ? Math.floor(Number(e.maxSummaryChars)) : we
|
|
96
|
+
} : {
|
|
97
|
+
maxMessages: Se,
|
|
98
|
+
maxEstimatedChars: Ce,
|
|
99
|
+
maxSummaryChars: we
|
|
100
|
+
}, G = (e) => H(e) && e.type === "agent-runtime-history-compaction" && e.version === 1 && typeof e.summary == "string", je = (e) => G(e) ? e : H(e) && G(e.historyCompaction) ? e.historyCompaction : null, Me = (e, t) => t ? H(e) && !G(e) ? {
|
|
101
|
+
...e,
|
|
102
|
+
historyCompaction: t
|
|
103
|
+
} : { historyCompaction: t } : e ?? null, Ne = (e) => U(e.id), Pe = (e, t) => {
|
|
104
|
+
let n = U(t?.compactedThroughMessageId);
|
|
105
|
+
return n ? e.findIndex((e) => Ne(e) === n) : -1;
|
|
106
|
+
}, Fe = (e, t) => {
|
|
107
|
+
let n = Math.max(1, t.maxMessages), r = Math.max(1, Math.min(e.length, Math.ceil(n / 3))), i = Math.max(0, e.length - n);
|
|
108
|
+
for (; i < e.length - r && Oe(e.slice(i)) > t.maxEstimatedChars;) i += 1;
|
|
109
|
+
return i;
|
|
110
|
+
}, Ie = (e) => {
|
|
111
|
+
let t = U(e.role) ?? "message", n = Ee(e), r = De(e);
|
|
112
|
+
return `${n ? `${t} (${n})` : t}:\n${r || "[non-text content]"}`;
|
|
113
|
+
}, Le = ({ previousContext: e, messagesToCompact: t, limits: n }) => {
|
|
114
|
+
let r = e?.summary?.trim(), i = t.map(Ie).join("\n\n").trim();
|
|
115
|
+
return ke([r ? `Earlier summary:\n${r}` : null, i ? `Compacted transcript:\n${i}` : null].filter((e) => !!e).join("\n\n"), n.maxSummaryChars).trim();
|
|
116
|
+
}, Re = (e) => ({
|
|
117
|
+
id: `${Te}:${U(e.compactedThroughMessageId) ?? "unknown"}`,
|
|
118
|
+
role: "user",
|
|
119
|
+
parts: [{
|
|
120
|
+
type: "text",
|
|
121
|
+
text: [
|
|
122
|
+
"Context from earlier messages in this chat has been compacted for the model.",
|
|
123
|
+
"Use this summary as durable conversation memory, but prefer the recent messages when they conflict.",
|
|
124
|
+
"",
|
|
125
|
+
e.summary
|
|
126
|
+
].join("\n")
|
|
127
|
+
}],
|
|
128
|
+
metadata: {
|
|
129
|
+
createdAt: e.compactedThroughMessageCreatedAt ?? e.updatedAt,
|
|
130
|
+
transient: !0,
|
|
131
|
+
historyCompaction: !0
|
|
132
|
+
}
|
|
133
|
+
}), K = (e, t) => t?.summary.trim() ? [Re(t), ...e] : e, ze = ({ messages: e, previousContext: t, limits: n }) => {
|
|
134
|
+
let r = Pe(e, t), i = r >= 0 ? e.slice(r + 1) : e;
|
|
135
|
+
if (!(i.length > n.maxMessages || Oe(i) > n.maxEstimatedChars)) return {
|
|
136
|
+
messages: K(i, t),
|
|
137
|
+
context: t
|
|
138
|
+
};
|
|
139
|
+
let a = Fe(i, n);
|
|
140
|
+
if (a <= 0) return {
|
|
141
|
+
messages: K(i, t),
|
|
142
|
+
context: t
|
|
143
|
+
};
|
|
144
|
+
let o = i.slice(0, a), s = i.slice(a), c = o.at(-1) ?? null, l = {
|
|
145
|
+
type: "agent-runtime-history-compaction",
|
|
146
|
+
version: 1,
|
|
147
|
+
summary: Le({
|
|
148
|
+
previousContext: t,
|
|
149
|
+
messagesToCompact: o,
|
|
150
|
+
limits: n
|
|
151
|
+
}),
|
|
152
|
+
compactedThroughMessageId: c ? Ne(c) : t?.compactedThroughMessageId ?? null,
|
|
153
|
+
compactedThroughMessageCreatedAt: c ? Ee(c) : t?.compactedThroughMessageCreatedAt ?? null,
|
|
154
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
155
|
+
messageCount: e.length
|
|
156
|
+
};
|
|
157
|
+
return {
|
|
158
|
+
messages: K(s, l),
|
|
159
|
+
context: l
|
|
160
|
+
};
|
|
161
|
+
}, Be = async ({ ctx: e, chatId: t, agent: n, selectedChatModel: i, mode: a, messages: o, lastContext: s }) => {
|
|
162
|
+
let c = r().historyCompaction;
|
|
163
|
+
if (c === !1 || c && c.enabled === !1) return {
|
|
164
|
+
messages: o,
|
|
165
|
+
lastContext: s ?? null,
|
|
166
|
+
lastContextChanged: !1
|
|
167
|
+
};
|
|
168
|
+
let l = Ae(c), u = je(s);
|
|
169
|
+
try {
|
|
170
|
+
let r = typeof c == "object" && typeof c.compact == "function" ? await c.compact({
|
|
171
|
+
ctx: e,
|
|
172
|
+
chatId: t,
|
|
173
|
+
agent: n,
|
|
174
|
+
selectedChatModel: i,
|
|
175
|
+
mode: a,
|
|
176
|
+
messages: o,
|
|
177
|
+
previousContext: u,
|
|
178
|
+
limits: l
|
|
179
|
+
}) : ze({
|
|
180
|
+
messages: o,
|
|
181
|
+
previousContext: u,
|
|
182
|
+
limits: l
|
|
183
|
+
}), d = Array.isArray(r.messages) ? r.messages : o, f = r.context === void 0 ? u : r.context;
|
|
184
|
+
return {
|
|
185
|
+
messages: d,
|
|
186
|
+
lastContext: Me(s, f ?? null),
|
|
187
|
+
lastContextChanged: f !== u
|
|
188
|
+
};
|
|
189
|
+
} catch (e) {
|
|
190
|
+
return console.warn("Failed to compact agent chat history.", e), {
|
|
191
|
+
messages: o,
|
|
192
|
+
lastContext: s ?? null,
|
|
193
|
+
lastContextChanged: !1
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}, q = [
|
|
58
197
|
"text",
|
|
59
198
|
"code",
|
|
60
199
|
"sheet"
|
|
61
|
-
],
|
|
62
|
-
if (
|
|
200
|
+
], Ve = (e) => e ? e === "1" || e.toLowerCase() === "true" || e.toLowerCase() === "yes" : !1, J = () => !Ve(process.env.AGENT_SDK_LIVE_TOOLS) && (Ve(process.env.PLAYWRIGHT) || process.env.NODE_ENV === "test"), He = async ({ system: e, prompt: t, dataStream: n, providerOptions: r }) => {
|
|
201
|
+
if (J()) {
|
|
63
202
|
let e = `Stub document: ${t}`;
|
|
64
203
|
return n.write({
|
|
65
204
|
type: "data-textDelta",
|
|
@@ -68,9 +207,9 @@ ${e}`;
|
|
|
68
207
|
}), e;
|
|
69
208
|
}
|
|
70
209
|
let i = "", { fullStream: a } = S({
|
|
71
|
-
model:
|
|
210
|
+
model: R(),
|
|
72
211
|
system: e,
|
|
73
|
-
experimental_transform:
|
|
212
|
+
experimental_transform: ae({ chunking: "word" }),
|
|
74
213
|
prompt: t,
|
|
75
214
|
providerOptions: r
|
|
76
215
|
});
|
|
@@ -80,8 +219,8 @@ ${e}`;
|
|
|
80
219
|
transient: !0
|
|
81
220
|
}));
|
|
82
221
|
return i;
|
|
83
|
-
},
|
|
84
|
-
if (
|
|
222
|
+
}, Ue = async ({ system: e, prompt: t, dataStream: n }) => {
|
|
223
|
+
if (J()) {
|
|
85
224
|
let e = `print("Stub code for: ${t}")\n`;
|
|
86
225
|
return n.write({
|
|
87
226
|
type: "data-codeDelta",
|
|
@@ -90,7 +229,7 @@ ${e}`;
|
|
|
90
229
|
}), e;
|
|
91
230
|
}
|
|
92
231
|
let r = "", { fullStream: i } = x({
|
|
93
|
-
model:
|
|
232
|
+
model: R(),
|
|
94
233
|
system: e,
|
|
95
234
|
prompt: t,
|
|
96
235
|
schema: A.object({ code: A.string() })
|
|
@@ -105,8 +244,8 @@ ${e}`;
|
|
|
105
244
|
}), r = t);
|
|
106
245
|
}
|
|
107
246
|
return r;
|
|
108
|
-
},
|
|
109
|
-
if (
|
|
247
|
+
}, We = async ({ system: e, prompt: t, dataStream: n }) => {
|
|
248
|
+
if (J()) {
|
|
110
249
|
let e = `name,value\n${t.replaceAll(",", " ")},1\n`;
|
|
111
250
|
return n.write({
|
|
112
251
|
type: "data-sheetDelta",
|
|
@@ -115,7 +254,7 @@ ${e}`;
|
|
|
115
254
|
}), e;
|
|
116
255
|
}
|
|
117
256
|
let r = "", { fullStream: i } = x({
|
|
118
|
-
model:
|
|
257
|
+
model: R(),
|
|
119
258
|
system: e,
|
|
120
259
|
prompt: t,
|
|
121
260
|
schema: A.object({ csv: A.string().describe("CSV data") })
|
|
@@ -134,7 +273,7 @@ ${e}`;
|
|
|
134
273
|
data: r,
|
|
135
274
|
transient: !0
|
|
136
275
|
}), r;
|
|
137
|
-
},
|
|
276
|
+
}, Ge = async (e) => {
|
|
138
277
|
try {
|
|
139
278
|
let t = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(e)}&count=1&language=en&format=json`);
|
|
140
279
|
if (!t.ok) return null;
|
|
@@ -148,7 +287,7 @@ ${e}`;
|
|
|
148
287
|
} catch {
|
|
149
288
|
return null;
|
|
150
289
|
}
|
|
151
|
-
},
|
|
290
|
+
}, Ke = ({ ctx: e, dataStream: t, requestHints: n }) => {
|
|
152
291
|
let r = i(e), a = async (t) => {
|
|
153
292
|
let n = await h(e, t);
|
|
154
293
|
return n.length > 0 ? n.at(-1) ?? null : null;
|
|
@@ -163,7 +302,7 @@ ${e}`;
|
|
|
163
302
|
execute: async (e) => {
|
|
164
303
|
let t, n;
|
|
165
304
|
if (e.city) {
|
|
166
|
-
let r = await
|
|
305
|
+
let r = await Ge(e.city);
|
|
167
306
|
if (!r) return { error: `Could not find coordinates for "${e.city}". Please check the city name.` };
|
|
168
307
|
t = r.latitude, n = r.longitude;
|
|
169
308
|
} else if (e.latitude !== void 0 && e.longitude !== void 0) t = e.latitude, n = e.longitude;
|
|
@@ -175,7 +314,7 @@ ${e}`;
|
|
|
175
314
|
description: "Create an artifact for writing or content creation. Use kind 'code' for programming, 'text' for writing, and 'sheet' for spreadsheets or tabular data.",
|
|
176
315
|
inputSchema: A.object({
|
|
177
316
|
title: A.string().describe("The title of the artifact"),
|
|
178
|
-
kind: A.enum(
|
|
317
|
+
kind: A.enum(q).describe("Use 'code' for programming, 'text' for prose, and 'sheet' for CSV/tabular data")
|
|
179
318
|
}),
|
|
180
319
|
execute: async ({ title: n, kind: r }) => {
|
|
181
320
|
let i = D();
|
|
@@ -197,16 +336,16 @@ ${e}`;
|
|
|
197
336
|
transient: !0
|
|
198
337
|
});
|
|
199
338
|
let a = "";
|
|
200
|
-
return r === "text" ? a = await
|
|
339
|
+
return r === "text" ? a = await He({
|
|
201
340
|
system: "Write about the given topic. Markdown is supported. Use headings wherever appropriate.",
|
|
202
341
|
prompt: n,
|
|
203
342
|
dataStream: t
|
|
204
|
-
}) : r === "code" ? a = await
|
|
205
|
-
system:
|
|
343
|
+
}) : r === "code" ? a = await Ue({
|
|
344
|
+
system: ve,
|
|
206
345
|
prompt: n,
|
|
207
346
|
dataStream: t
|
|
208
|
-
}) : r === "sheet" && (a = await
|
|
209
|
-
system:
|
|
347
|
+
}) : r === "sheet" && (a = await We({
|
|
348
|
+
system: ye,
|
|
210
349
|
prompt: n,
|
|
211
350
|
dataStream: t
|
|
212
351
|
})), await f(e, {
|
|
@@ -235,27 +374,27 @@ ${e}`;
|
|
|
235
374
|
let i = await a(n);
|
|
236
375
|
if (!i) return { error: "Document not found" };
|
|
237
376
|
let o = i.kind;
|
|
238
|
-
if (!
|
|
377
|
+
if (!q.includes(o)) return { error: `Unsupported document kind: ${i.kind}` };
|
|
239
378
|
t.write({
|
|
240
379
|
type: "data-clear",
|
|
241
380
|
data: null,
|
|
242
381
|
transient: !0
|
|
243
382
|
});
|
|
244
383
|
let s = "";
|
|
245
|
-
return o === "text" ? s = await
|
|
246
|
-
system:
|
|
384
|
+
return o === "text" ? s = await He({
|
|
385
|
+
system: be(i.content, i.kind),
|
|
247
386
|
prompt: r,
|
|
248
387
|
dataStream: t,
|
|
249
388
|
providerOptions: { openai: { prediction: {
|
|
250
389
|
type: "content",
|
|
251
390
|
content: i.content
|
|
252
391
|
} } }
|
|
253
|
-
}) : o === "code" ? s = await
|
|
254
|
-
system:
|
|
392
|
+
}) : o === "code" ? s = await Ue({
|
|
393
|
+
system: be(i.content, i.kind),
|
|
255
394
|
prompt: r,
|
|
256
395
|
dataStream: t
|
|
257
|
-
}) : o === "sheet" && (s = await
|
|
258
|
-
system:
|
|
396
|
+
}) : o === "sheet" && (s = await We({
|
|
397
|
+
system: be(i.content, i.kind),
|
|
259
398
|
prompt: r,
|
|
260
399
|
dataStream: t
|
|
261
400
|
})), await f(e, {
|
|
@@ -291,7 +430,7 @@ ${e}`;
|
|
|
291
430
|
if (!s) return { error: "Document not found" };
|
|
292
431
|
if (!s.content) return { error: "Document has no content" };
|
|
293
432
|
let c = s.kind;
|
|
294
|
-
if (!
|
|
433
|
+
if (!q.includes(c)) return { error: `Unsupported document kind: ${s.kind}` };
|
|
295
434
|
if (!s.content.includes(r)) return { error: "old_string not found in document" };
|
|
296
435
|
let l = o ? s.content.replaceAll(r, i) : s.content.replace(r, i);
|
|
297
436
|
return await f(e, {
|
|
@@ -334,7 +473,7 @@ ${e}`;
|
|
|
334
473
|
execute: async ({ documentId: n }) => {
|
|
335
474
|
let i = await a(n);
|
|
336
475
|
if (!i?.content) return { error: "Document not found" };
|
|
337
|
-
if (
|
|
476
|
+
if (J()) {
|
|
338
477
|
let a = {
|
|
339
478
|
originalText: "Stub original sentence.",
|
|
340
479
|
suggestedText: "Stub suggested sentence.",
|
|
@@ -361,7 +500,7 @@ ${e}`;
|
|
|
361
500
|
};
|
|
362
501
|
}
|
|
363
502
|
let o = [], { partialOutputStream: s } = S({
|
|
364
|
-
model:
|
|
503
|
+
model: R(),
|
|
365
504
|
system: "You are a help writing assistant. Given a piece of writing, please offer suggestions to improve the piece of writing and describe the change. It is very important for the edits to contain full sentences instead of just words. Max 5 suggestions.",
|
|
366
505
|
prompt: i.content,
|
|
367
506
|
output: b.array({ element: A.object({
|
|
@@ -411,39 +550,48 @@ ${e}`;
|
|
|
411
550
|
}
|
|
412
551
|
})
|
|
413
552
|
};
|
|
414
|
-
},
|
|
553
|
+
}, qe = (e) => e ? !e.trim() : !0, Je = (e) => (Array.isArray(e) ? e : []).filter((e) => !!e && typeof e == "object" && !!e && "type" in e && "text" in e && e.type === "text" && typeof e.text == "string").map((e) => e.text).join("").trim(), Ye = (e) => {
|
|
415
554
|
let t = e.trim();
|
|
416
555
|
return t = t.replaceAll("\n", " "), t = t.replaceAll("\r", " "), t = t.replace(/\s+/g, " ").trim(), t = t.replace(/^(?:chat\s+)?title\s*[:\-–—]\s*/i, ""), t = t.replace(/^[#*\-•>"'`\s]+/, ""), t = t.replace(/[#:]/g, " "), t = t.replace(/[`"'*_~[\]()\\]/g, ""), t = t.replace(/\s+/g, " ").trim(), t.length > 30 && (t = t.slice(0, 30).trimEnd()), t;
|
|
417
|
-
},
|
|
556
|
+
}, Xe = (e) => {
|
|
418
557
|
let t = e.trim().toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, "").replace(/\s+/g, " ").trim();
|
|
419
558
|
return t === "hi" || t === "hello" || t === "hey" || t === "salut" || t === "bonjour";
|
|
420
|
-
},
|
|
559
|
+
}, Y = (e) => {
|
|
421
560
|
let t = e.trim();
|
|
422
|
-
return !t ||
|
|
423
|
-
},
|
|
561
|
+
return !t || Xe(t) ? "" : Ye(t.replaceAll("\n", " ").replaceAll("\r", " ").split(/\s+/).filter((e) => e.trim() !== "").slice(0, 5).join(" "));
|
|
562
|
+
}, Ze = "/api/rb/agents/chat";
|
|
424
563
|
A.any(), A.any();
|
|
425
564
|
//#endregion
|
|
426
565
|
//#region src/api/chat/handler.ts
|
|
427
|
-
var
|
|
428
|
-
default: () =>
|
|
566
|
+
var Qe = /* @__PURE__ */ e({
|
|
567
|
+
default: () => zt,
|
|
429
568
|
runAgentLoop: () => $
|
|
430
|
-
}),
|
|
569
|
+
}), $e = 1024 * 1024 * 20, et = 1e5, tt = /^\/api\/rb\/files\/([^/]+)\/?$/, nt = "test-model", rt = "openai/gpt-5.4-mini", it = async (e) => new Promise((t) => setTimeout(t, e)), at = async (e, t) => {
|
|
431
570
|
let n = v(t);
|
|
432
|
-
n && !n.isDone || await
|
|
571
|
+
n && !n.isDone || await c(e, {
|
|
433
572
|
chatId: t,
|
|
434
573
|
isStreaming: !1
|
|
435
574
|
});
|
|
436
|
-
},
|
|
575
|
+
}, ot = (e) => e ? e === "1" || e.toLowerCase() === "true" || e.toLowerCase() === "yes" : !1, X = (e) => typeof e == "string" && e.trim() || null, st = () => {
|
|
576
|
+
let e = r().chatModels;
|
|
577
|
+
if (!e?.length) return F;
|
|
578
|
+
let t = /* @__PURE__ */ new Set();
|
|
579
|
+
for (let n of e) {
|
|
580
|
+
let e = X(typeof n == "string" ? n : n.id);
|
|
581
|
+
e && t.add(e);
|
|
582
|
+
}
|
|
583
|
+
return t;
|
|
584
|
+
}, ct = (e) => {
|
|
437
585
|
if (typeof e == "boolean") return e;
|
|
438
586
|
let t = X(e)?.toLowerCase();
|
|
439
587
|
return t ? t === "true" ? !0 : t === "false" ? !1 : null : null;
|
|
440
|
-
},
|
|
588
|
+
}, lt = (e) => {
|
|
441
589
|
let t = e.req?.session?.user?.currentTenantId;
|
|
442
590
|
return X(t);
|
|
443
|
-
},
|
|
591
|
+
}, ut = (e) => {
|
|
444
592
|
let t = e.req?.session?.user?.id;
|
|
445
593
|
return X(t);
|
|
446
|
-
},
|
|
594
|
+
}, dt = () => X(process.env.RB_FILESYSTEM_BUCKET_NAME) ?? "fs", ft = (e) => E("sha256").update(Buffer.from(e)).digest("hex"), pt = (e) => {
|
|
447
595
|
let t = e.trim();
|
|
448
596
|
if (!t) return null;
|
|
449
597
|
let n;
|
|
@@ -456,15 +604,15 @@ var Oe = /* @__PURE__ */ e({
|
|
|
456
604
|
return null;
|
|
457
605
|
}
|
|
458
606
|
}
|
|
459
|
-
let r =
|
|
607
|
+
let r = tt.exec(n.pathname);
|
|
460
608
|
if (!r) return null;
|
|
461
609
|
let i = X(decodeURIComponent(r[1]));
|
|
462
610
|
return i ? {
|
|
463
611
|
fileId: i,
|
|
464
612
|
uploadKey: X(n.searchParams.get("rb-upload-key"))
|
|
465
613
|
} : null;
|
|
466
|
-
},
|
|
467
|
-
let r =
|
|
614
|
+
}, mt = async ({ ctx: e, fileId: t, uploadKey: n }) => {
|
|
615
|
+
let r = lt(e);
|
|
468
616
|
if (!r) throw Error("tenant_missing");
|
|
469
617
|
let i;
|
|
470
618
|
try {
|
|
@@ -474,18 +622,18 @@ var Oe = /* @__PURE__ */ e({
|
|
|
474
622
|
}
|
|
475
623
|
let a = (await T(r)).db;
|
|
476
624
|
if (!a) throw Error("filesystem_db_unavailable");
|
|
477
|
-
let o = new
|
|
625
|
+
let o = new se(a, { bucketName: dt() }), [s] = await o.find({ _id: i }).limit(1).toArray();
|
|
478
626
|
if (!s) throw Error("file_not_found");
|
|
479
|
-
let c =
|
|
627
|
+
let c = ct(s?.metadata?.isPublic) ?? !1, l = ut(e), u = X(s?.metadata?.userId), d = X(s?.metadata?.ownerKeyHash);
|
|
480
628
|
if (!c) {
|
|
481
|
-
let e = !!(l && u && l === u), t = !!(n && d &&
|
|
629
|
+
let e = !!(l && u && l === u), t = !!(n && d && ft(n) === d);
|
|
482
630
|
if (!e && !t) throw Error("unauthorized");
|
|
483
631
|
}
|
|
484
632
|
let f = X(s?.metadata?.mimeType), p = [], m = 0;
|
|
485
633
|
return await new Promise((e, t) => {
|
|
486
634
|
let n = o.openDownloadStream(i);
|
|
487
635
|
n.on("data", (e) => {
|
|
488
|
-
if (m += e.length, m >
|
|
636
|
+
if (m += e.length, m > $e) {
|
|
489
637
|
n.destroy(/* @__PURE__ */ Error("attachment_too_large"));
|
|
490
638
|
return;
|
|
491
639
|
}
|
|
@@ -495,16 +643,16 @@ var Oe = /* @__PURE__ */ e({
|
|
|
495
643
|
data: Buffer.concat(p, m),
|
|
496
644
|
mimeType: f
|
|
497
645
|
};
|
|
498
|
-
},
|
|
646
|
+
}, ht = async (e, t) => {
|
|
499
647
|
let n = Array.isArray(t) ? t : [], r = [];
|
|
500
648
|
for (let t of n) {
|
|
501
649
|
if (!t || typeof t != "object" || !("role" in t) || t.role !== "user") continue;
|
|
502
650
|
let n = Array.isArray(t.content) ? t.content : null;
|
|
503
651
|
if (n) for (let t of n) {
|
|
504
652
|
if (!t || typeof t != "object" || t.type !== "file" || typeof t.data != "string") continue;
|
|
505
|
-
let n =
|
|
653
|
+
let n = pt(t.data);
|
|
506
654
|
n && r.push((async () => {
|
|
507
|
-
let { data: r, mimeType: i } = await
|
|
655
|
+
let { data: r, mimeType: i } = await mt({
|
|
508
656
|
ctx: e,
|
|
509
657
|
...n
|
|
510
658
|
});
|
|
@@ -513,7 +661,7 @@ var Oe = /* @__PURE__ */ e({
|
|
|
513
661
|
}
|
|
514
662
|
}
|
|
515
663
|
r.length > 0 && await Promise.all(r);
|
|
516
|
-
},
|
|
664
|
+
}, gt = (e) => {
|
|
517
665
|
let t = Array.isArray(e) ? e : [];
|
|
518
666
|
for (let e of t) {
|
|
519
667
|
if (!e || typeof e != "object" || !("role" in e) || e.role !== "user") continue;
|
|
@@ -526,7 +674,7 @@ var Oe = /* @__PURE__ */ e({
|
|
|
526
674
|
let i = X(n.filename) ?? X(n.name) ?? `attachment-${e + 1}`, a = n.data;
|
|
527
675
|
if (r?.startsWith("text/") && a instanceof Uint8Array) {
|
|
528
676
|
let n = (Buffer.isBuffer(a) ? a : Buffer.from(a)).toString("utf8");
|
|
529
|
-
n.length >
|
|
677
|
+
n.length > et && (n = `${n.slice(0, et)}\n\n[Truncated]`);
|
|
530
678
|
let o = r === "text/csv" || i.toLowerCase().endsWith(".csv") ? "csv" : "";
|
|
531
679
|
t[e] = {
|
|
532
680
|
type: "text",
|
|
@@ -541,16 +689,16 @@ var Oe = /* @__PURE__ */ e({
|
|
|
541
689
|
};
|
|
542
690
|
}
|
|
543
691
|
}
|
|
544
|
-
},
|
|
545
|
-
if (e.message?.role === "user") return
|
|
692
|
+
}, _t = async (e, t, n) => {
|
|
693
|
+
if (e.message?.role === "user") return Je(e.message.parts);
|
|
546
694
|
if (Array.isArray(e.messages)) {
|
|
547
695
|
let t = null;
|
|
548
696
|
for (let n = e.messages.length - 1; n >= 0; --n) {
|
|
549
697
|
let r = e.messages[n];
|
|
550
698
|
if (r?.role !== "user") continue;
|
|
551
|
-
let i =
|
|
699
|
+
let i = Je(r.parts);
|
|
552
700
|
if (i) {
|
|
553
|
-
if (
|
|
701
|
+
if (Y(i)) return i;
|
|
554
702
|
t ??= i;
|
|
555
703
|
}
|
|
556
704
|
}
|
|
@@ -560,40 +708,40 @@ var Oe = /* @__PURE__ */ e({
|
|
|
560
708
|
for (let e = r.length - 1; e >= 0; --e) {
|
|
561
709
|
let t = r[e];
|
|
562
710
|
if (t.role !== "user") continue;
|
|
563
|
-
let n =
|
|
711
|
+
let n = Je(t.parts);
|
|
564
712
|
if (n) {
|
|
565
|
-
if (
|
|
713
|
+
if (Y(n)) return n;
|
|
566
714
|
i ??= n;
|
|
567
715
|
}
|
|
568
716
|
}
|
|
569
717
|
return i ?? "";
|
|
570
|
-
},
|
|
718
|
+
}, vt = (e) => {
|
|
571
719
|
let t = Array.isArray(e.messages) && e.messages.length > 0 ? e.messages.at(-1) : e.message;
|
|
572
720
|
return !t || t.role !== "user" ? "" : (Array.isArray(t.parts) ? t.parts : []).filter((e) => !!e && typeof e == "object" && !!e && "type" in e && "text" in e && e.type === "text" && typeof e.text == "string").map((e) => e.text).join("\n").trim();
|
|
573
|
-
},
|
|
721
|
+
}, yt = (e) => {
|
|
574
722
|
let t = e.id;
|
|
575
723
|
return typeof t == "string" && t.trim() !== "" ? t : null;
|
|
576
|
-
},
|
|
724
|
+
}, bt = (e) => {
|
|
577
725
|
let t = e.selectedChatModel;
|
|
578
726
|
return typeof t == "string" && t.trim() !== "" ? t : null;
|
|
579
|
-
},
|
|
727
|
+
}, xt = (e) => {
|
|
580
728
|
let t = e.openaiReasoningEffort;
|
|
581
729
|
return typeof t == "string" && t.trim() !== "" ? t.trim() : null;
|
|
582
|
-
},
|
|
730
|
+
}, St = (e) => {
|
|
583
731
|
if (!e || typeof e != "object") return !1;
|
|
584
732
|
let t = e.state, n = e.toolCallId;
|
|
585
733
|
return typeof n == "string" && n.trim() !== "" && (t === "approval-responded" || t === "output-denied");
|
|
586
|
-
},
|
|
734
|
+
}, Ct = (e) => {
|
|
587
735
|
let t = Array.isArray(e.messages) ? e.messages : [];
|
|
588
|
-
return t.length === 0 || t.at(-1)?.role === "user" ? !1 : t.some((e) => (Array.isArray(e?.parts) ? e.parts : []).some((e) =>
|
|
589
|
-
},
|
|
736
|
+
return t.length === 0 || t.at(-1)?.role === "user" ? !1 : t.some((e) => (Array.isArray(e?.parts) ? e.parts : []).some((e) => St(e)));
|
|
737
|
+
}, wt = (e) => {
|
|
590
738
|
let t = /* @__PURE__ */ new Map();
|
|
591
739
|
for (let n of e) {
|
|
592
740
|
let e = Array.isArray(n?.parts) ? n.parts : [];
|
|
593
|
-
for (let n of e)
|
|
741
|
+
for (let n of e) St(n) && t.set(n.toolCallId, n);
|
|
594
742
|
}
|
|
595
743
|
return t;
|
|
596
|
-
},
|
|
744
|
+
}, Tt = (e, t) => {
|
|
597
745
|
let n = [];
|
|
598
746
|
return {
|
|
599
747
|
changedMessages: n,
|
|
@@ -616,21 +764,21 @@ var Oe = /* @__PURE__ */ e({
|
|
|
616
764
|
return n.push(a), a;
|
|
617
765
|
})
|
|
618
766
|
};
|
|
619
|
-
},
|
|
767
|
+
}, Et = (e) => {
|
|
620
768
|
if (!e || typeof e != "object" || !("createdAt" in e)) return null;
|
|
621
769
|
let t = e.createdAt;
|
|
622
770
|
if (typeof t != "string") return null;
|
|
623
771
|
let n = new Date(t);
|
|
624
772
|
return Number.isNaN(n.getTime()) ? null : n;
|
|
625
|
-
},
|
|
773
|
+
}, Dt = (e) => ({
|
|
626
774
|
latitude: null,
|
|
627
775
|
longitude: null,
|
|
628
776
|
city: null,
|
|
629
777
|
country: null
|
|
630
|
-
}),
|
|
778
|
+
}), Ot = (e) => {
|
|
631
779
|
let [t] = e.split("/");
|
|
632
780
|
return t && t !== e ? t : "unknown";
|
|
633
|
-
}, Z = (e) => typeof e == "number" && Number.isFinite(e) ? e : void 0,
|
|
781
|
+
}, Z = (e) => typeof e == "number" && Number.isFinite(e) ? e : void 0, kt = (e) => e instanceof Error && e.message.trim() ? e.message : "Unknown error", At = (e) => {
|
|
634
782
|
let t = e && typeof e == "object" ? e : {}, n = t.inputTokenDetails && typeof t.inputTokenDetails == "object" ? t.inputTokenDetails : {}, r = t.outputTokenDetails && typeof t.outputTokenDetails == "object" ? t.outputTokenDetails : {};
|
|
635
783
|
return {
|
|
636
784
|
inputTokens: Z(t.inputTokens),
|
|
@@ -640,13 +788,13 @@ var Oe = /* @__PURE__ */ e({
|
|
|
640
788
|
reasoningTokens: Z(r.reasoningTokens) ?? Z(t.reasoningTokens),
|
|
641
789
|
rawUsage: Object.keys(t).length > 0 ? t.raw ?? t : void 0
|
|
642
790
|
};
|
|
643
|
-
},
|
|
791
|
+
}, jt = () => ({
|
|
644
792
|
inputTokens: 0,
|
|
645
793
|
outputTokens: 0,
|
|
646
794
|
totalTokens: 0,
|
|
647
795
|
cachedInputTokens: 0,
|
|
648
796
|
reasoningTokens: 0
|
|
649
|
-
}),
|
|
797
|
+
}), Mt = async ({ ctx: e, chatId: t, agent: n, selectedChatModel: i, mode: a }) => {
|
|
650
798
|
try {
|
|
651
799
|
return { workspaceId: X((await r().resolveUsageContext?.({
|
|
652
800
|
ctx: e,
|
|
@@ -659,19 +807,19 @@ var Oe = /* @__PURE__ */ e({
|
|
|
659
807
|
return console.warn("Failed to resolve agent usage context.", e), { workspaceId: void 0 };
|
|
660
808
|
}
|
|
661
809
|
}, Q = async ({ ctx: e, usageContext: t, agent: n, chatId: r, mode: i, feature: a, modelId: s, status: c, billable: l, usage: u, finishReason: d, error: f, startedAt: p, finishedAt: m = /* @__PURE__ */ new Date() }) => {
|
|
662
|
-
let h = u ?
|
|
810
|
+
let h = u ? At(u) : {}, g = {
|
|
663
811
|
id: D(),
|
|
664
812
|
workspaceId: t.workspaceId,
|
|
665
813
|
agentId: n.id,
|
|
666
814
|
chatId: r,
|
|
667
815
|
mode: i,
|
|
668
816
|
feature: a,
|
|
669
|
-
provider:
|
|
817
|
+
provider: Ot(s),
|
|
670
818
|
model: s,
|
|
671
819
|
billable: l,
|
|
672
820
|
status: c,
|
|
673
821
|
finishReason: X(d) ?? void 0,
|
|
674
|
-
errorMessage: f ?
|
|
822
|
+
errorMessage: f ? kt(f) : void 0,
|
|
675
823
|
...h,
|
|
676
824
|
startedAt: p,
|
|
677
825
|
finishedAt: m,
|
|
@@ -682,12 +830,12 @@ var Oe = /* @__PURE__ */ e({
|
|
|
682
830
|
} catch (e) {
|
|
683
831
|
console.error("Failed to save agents usage event.", e);
|
|
684
832
|
}
|
|
685
|
-
},
|
|
833
|
+
}, Nt = (e) => `agents-sdk:chat:${e}:stream`, Pt = async ({ ctx: e, chatId: t, selectedChatModel: i }) => await r().resolveAgent?.({
|
|
686
834
|
ctx: e,
|
|
687
835
|
chatId: t,
|
|
688
836
|
selectedChatModel: i
|
|
689
|
-
}) ?? n(),
|
|
690
|
-
let a = i(e), o =
|
|
837
|
+
}) ?? n(), Ft = ({ ctx: e, chatId: t, mode: n, streamTransport: r }) => {
|
|
838
|
+
let a = i(e), o = Nt(t), s = y({
|
|
691
839
|
chatId: t,
|
|
692
840
|
abortWhenNoSubscribersMs: n === "headless" || r ? 0 : void 0
|
|
693
841
|
});
|
|
@@ -715,54 +863,54 @@ var Oe = /* @__PURE__ */ e({
|
|
|
715
863
|
});
|
|
716
864
|
}
|
|
717
865
|
};
|
|
718
|
-
},
|
|
866
|
+
}, It = ({ ctx: e, chatId: t, streamTransport: n, message: r }) => {
|
|
719
867
|
if (!n) return;
|
|
720
868
|
let a = i(e);
|
|
721
869
|
n.publish({
|
|
722
870
|
tenantId: a.tenantId,
|
|
723
871
|
userId: a.userId,
|
|
724
|
-
channel:
|
|
872
|
+
channel: Nt(t),
|
|
725
873
|
payload: {
|
|
726
874
|
chatId: t,
|
|
727
875
|
message: r
|
|
728
876
|
}
|
|
729
877
|
});
|
|
730
878
|
}, $ = async ({ payload: e, ctx: n, mode: o = "interactive", streamTransport: l = null }) => {
|
|
731
|
-
let f =
|
|
879
|
+
let f = yt(e);
|
|
732
880
|
if (!f) return a(n, "bad_request:api", "Parameter id is required.");
|
|
733
|
-
let p = e, h =
|
|
881
|
+
let p = e, h = bt(p), v = ot(process.env.PLAYWRIGHT), y = !ot(process.env.AGENT_SDK_LIVE_CHAT) && v || process.env.NODE_ENV === "test";
|
|
734
882
|
if (!h) return a(n, "bad_request:api", "Parameter selectedChatModel is required.");
|
|
735
|
-
if (h ===
|
|
736
|
-
let b = process.env.OPENAI_API_KEY,
|
|
737
|
-
if (E && !
|
|
883
|
+
if (h === nt && (y = !0), h !== nt && (!st().has(h) || !me(h))) return a(n, "bad_request:api", `Unsupported selectedChatModel: "${h}".`);
|
|
884
|
+
let b = process.env.OPENAI_API_KEY, re = process.env.GOOGLE_GENERATIVE_AI_API_KEY ?? process.env.GOOGLE_API_KEY, ae = process.env.XAI_API_KEY, x = ue(h), C = de(h), w = fe(h), T = xt(p), E = T && T !== "default" && T !== "off" ? T : null, se = t(h);
|
|
885
|
+
if (E && !se.some((e) => e === E)) return a(n, "bad_request:api", `Unsupported reasoning effort "${E}" for selectedChatModel: "${h}".`);
|
|
738
886
|
if (!y && x && !b) return a(n, "bad_request:provider", "Missing OPENAI_API_KEY.");
|
|
739
|
-
if (!y && C && !
|
|
740
|
-
if (!y && w && !
|
|
887
|
+
if (!y && C && !re) return a(n, "bad_request:provider", "Missing GOOGLE_GENERATIVE_AI_API_KEY (or GOOGLE_API_KEY).");
|
|
888
|
+
if (!y && w && !ae) return a(n, "bad_request:provider", "Missing XAI_API_KEY.");
|
|
741
889
|
if (!y && !x && !C && !w) return a(n, "bad_request:provider", `Unsupported selectedChatModel: "${h}".`);
|
|
742
|
-
let O = await
|
|
890
|
+
let O = await Pt({
|
|
743
891
|
ctx: n,
|
|
744
892
|
chatId: f,
|
|
745
893
|
selectedChatModel: h
|
|
746
|
-
}),
|
|
894
|
+
}), ce = await r().resolveKnowledge?.({
|
|
747
895
|
ctx: n,
|
|
748
896
|
chatId: f,
|
|
749
897
|
agent: O,
|
|
750
898
|
selectedChatModel: h
|
|
751
|
-
}) ?? null, k = await
|
|
899
|
+
}) ?? null, k = await Mt({
|
|
752
900
|
ctx: n,
|
|
753
901
|
chatId: f,
|
|
754
902
|
agent: O,
|
|
755
903
|
selectedChatModel: h,
|
|
756
904
|
mode: o
|
|
757
|
-
}),
|
|
905
|
+
}), le = i(n), A = await s(n, f, { includeDeleted: !0 });
|
|
758
906
|
if (A?.deletedAt) return a(n, "not_found:chat");
|
|
759
|
-
A ? A.selectedChatModel !== h && await
|
|
907
|
+
A ? A.selectedChatModel !== h && await ee(n, {
|
|
760
908
|
chatId: f,
|
|
761
909
|
selectedChatModel: h
|
|
762
910
|
}) : await m(n, {
|
|
763
911
|
id: f,
|
|
764
912
|
createdAt: /* @__PURE__ */ new Date(),
|
|
765
|
-
userId:
|
|
913
|
+
userId: le.userId,
|
|
766
914
|
title: "",
|
|
767
915
|
isStreaming: !1,
|
|
768
916
|
selectedChatModel: h,
|
|
@@ -778,7 +926,7 @@ var Oe = /* @__PURE__ */ e({
|
|
|
778
926
|
parts: N,
|
|
779
927
|
attachments: [],
|
|
780
928
|
createdAt: e
|
|
781
|
-
}]),
|
|
929
|
+
}]), It({
|
|
782
930
|
ctx: n,
|
|
783
931
|
chatId: f,
|
|
784
932
|
streamTransport: l,
|
|
@@ -791,19 +939,19 @@ var Oe = /* @__PURE__ */ e({
|
|
|
791
939
|
});
|
|
792
940
|
}
|
|
793
941
|
if (y) {
|
|
794
|
-
await
|
|
942
|
+
await c(n, {
|
|
795
943
|
chatId: f,
|
|
796
944
|
isStreaming: !0
|
|
797
945
|
});
|
|
798
|
-
let e =
|
|
946
|
+
let e = Ft({
|
|
799
947
|
ctx: n,
|
|
800
948
|
chatId: f,
|
|
801
949
|
mode: o,
|
|
802
950
|
streamTransport: l
|
|
803
951
|
});
|
|
804
952
|
try {
|
|
805
|
-
let t =
|
|
806
|
-
r &&
|
|
953
|
+
let t = Y(await _t(p, n, f)), r = await s(n, f);
|
|
954
|
+
r && qe(r.title) && t && t !== r.title && (await _(n, {
|
|
807
955
|
chatId: f,
|
|
808
956
|
title: t
|
|
809
957
|
}), e.publishJson({
|
|
@@ -812,7 +960,7 @@ var Oe = /* @__PURE__ */ e({
|
|
|
812
960
|
transient: !0
|
|
813
961
|
}));
|
|
814
962
|
let i = D();
|
|
815
|
-
v && !e.abortController.signal.aborted && await
|
|
963
|
+
v && !e.abortController.signal.aborted && await it(600), e.publishJson({
|
|
816
964
|
type: "start",
|
|
817
965
|
messageId: i
|
|
818
966
|
});
|
|
@@ -821,18 +969,18 @@ var Oe = /* @__PURE__ */ e({
|
|
|
821
969
|
type: "text-start",
|
|
822
970
|
id: a
|
|
823
971
|
});
|
|
824
|
-
let
|
|
825
|
-
for (let t of
|
|
972
|
+
let c = vt(p), l = c ? `Echo: ${c}` : "Hello from agents-sdk", d = Date.now(), m = l.split(/(\s+)/).filter((e) => e.length > 0);
|
|
973
|
+
for (let t of m) {
|
|
826
974
|
if (e.abortController.signal.aborted) break;
|
|
827
975
|
e.publishJson({
|
|
828
976
|
type: "text-delta",
|
|
829
977
|
id: a,
|
|
830
978
|
delta: t
|
|
831
|
-
}), await
|
|
979
|
+
}), await it(v ? 120 : 80);
|
|
832
980
|
}
|
|
833
981
|
if (v && !e.abortController.signal.aborted) {
|
|
834
|
-
let e = Date.now() -
|
|
835
|
-
e < t && await
|
|
982
|
+
let e = Date.now() - d, t = 1200;
|
|
983
|
+
e < t && await it(t - e);
|
|
836
984
|
}
|
|
837
985
|
return e.abortController.signal.aborted ? {} : (e.publishJson({
|
|
838
986
|
type: "text-end",
|
|
@@ -846,7 +994,7 @@ var Oe = /* @__PURE__ */ e({
|
|
|
846
994
|
role: "assistant",
|
|
847
995
|
parts: [{
|
|
848
996
|
type: "text",
|
|
849
|
-
text:
|
|
997
|
+
text: l
|
|
850
998
|
}],
|
|
851
999
|
attachments: [],
|
|
852
1000
|
createdAt: /* @__PURE__ */ new Date()
|
|
@@ -860,16 +1008,16 @@ var Oe = /* @__PURE__ */ e({
|
|
|
860
1008
|
modelId: h,
|
|
861
1009
|
status: "success",
|
|
862
1010
|
billable: !1,
|
|
863
|
-
usage:
|
|
1011
|
+
usage: jt(),
|
|
864
1012
|
finishReason: "stop",
|
|
865
|
-
startedAt: new Date(
|
|
1013
|
+
startedAt: new Date(d)
|
|
866
1014
|
}), {});
|
|
867
1015
|
} finally {
|
|
868
|
-
await
|
|
1016
|
+
await at(n, f);
|
|
869
1017
|
}
|
|
870
1018
|
}
|
|
871
|
-
let P =
|
|
872
|
-
if (Array.isArray(p.messages) && !
|
|
1019
|
+
let P = Dt(n), F = Ct(p);
|
|
1020
|
+
if (Array.isArray(p.messages) && !F) {
|
|
873
1021
|
let e = p.messages.flatMap((e) => {
|
|
874
1022
|
let t = typeof e?.id == "string" ? e.id : null, n = typeof e?.role == "string" ? e.role : null, r = Array.isArray(e?.parts) ? e.parts : [];
|
|
875
1023
|
return !t || !n ? [] : [{
|
|
@@ -878,44 +1026,66 @@ var Oe = /* @__PURE__ */ e({
|
|
|
878
1026
|
role: n,
|
|
879
1027
|
parts: r,
|
|
880
1028
|
attachments: [],
|
|
881
|
-
createdAt:
|
|
1029
|
+
createdAt: Et(e?.metadata) ?? /* @__PURE__ */ new Date()
|
|
882
1030
|
}];
|
|
883
1031
|
});
|
|
884
1032
|
e.length > 0 && await u(n, e);
|
|
885
1033
|
}
|
|
886
|
-
let
|
|
887
|
-
if (Array.isArray(p.messages) && p.messages.length > 0 &&
|
|
1034
|
+
let I;
|
|
1035
|
+
if (Array.isArray(p.messages) && p.messages.length > 0 && F) {
|
|
888
1036
|
let e = await d(n, f);
|
|
889
|
-
if (e.length === 0)
|
|
1037
|
+
if (e.length === 0) I = p.messages;
|
|
890
1038
|
else {
|
|
891
|
-
let { changedMessages: t, updatedMessages: r } =
|
|
892
|
-
t.length > 0 && await u(n, t),
|
|
1039
|
+
let { changedMessages: t, updatedMessages: r } = Tt(e, wt(p.messages));
|
|
1040
|
+
t.length > 0 && await u(n, t), I = r.map((e) => ({
|
|
893
1041
|
id: e.id,
|
|
894
1042
|
role: e.role,
|
|
895
1043
|
parts: e.parts,
|
|
896
1044
|
metadata: { createdAt: e.createdAt.toISOString() }
|
|
897
1045
|
}));
|
|
898
1046
|
}
|
|
899
|
-
} else
|
|
1047
|
+
} else I = Array.isArray(p.messages) && p.messages.length > 0 ? p.messages : (await d(n, f)).map((e) => ({
|
|
900
1048
|
id: e.id,
|
|
901
1049
|
role: e.role,
|
|
902
1050
|
parts: e.parts,
|
|
903
1051
|
metadata: { createdAt: e.createdAt.toISOString() }
|
|
904
1052
|
}));
|
|
905
|
-
|
|
1053
|
+
let L = F ? {
|
|
1054
|
+
messages: I,
|
|
1055
|
+
lastContext: A?.lastContext ?? null,
|
|
1056
|
+
lastContextChanged: !1
|
|
1057
|
+
} : await Be({
|
|
1058
|
+
ctx: n,
|
|
1059
|
+
chatId: f,
|
|
1060
|
+
agent: O,
|
|
1061
|
+
selectedChatModel: h,
|
|
1062
|
+
mode: o,
|
|
1063
|
+
messages: I,
|
|
1064
|
+
lastContext: A?.lastContext ?? null
|
|
1065
|
+
});
|
|
1066
|
+
if (!F && L.lastContextChanged) try {
|
|
1067
|
+
await g(n, {
|
|
1068
|
+
chatId: f,
|
|
1069
|
+
lastContext: L.lastContext
|
|
1070
|
+
});
|
|
1071
|
+
} catch (e) {
|
|
1072
|
+
console.warn("Failed to save compacted agent chat history context.", e);
|
|
1073
|
+
}
|
|
1074
|
+
let R = L.messages;
|
|
1075
|
+
await c(n, {
|
|
906
1076
|
chatId: f,
|
|
907
1077
|
isStreaming: !0
|
|
908
1078
|
});
|
|
909
|
-
let z =
|
|
1079
|
+
let z = Ft({
|
|
910
1080
|
ctx: n,
|
|
911
1081
|
chatId: f,
|
|
912
1082
|
mode: o,
|
|
913
1083
|
streamTransport: l
|
|
914
1084
|
}), B = "", V = "";
|
|
915
1085
|
try {
|
|
916
|
-
B = await
|
|
1086
|
+
B = await _t(p, n, f), V = Y(B);
|
|
917
1087
|
let e = await s(n, f);
|
|
918
|
-
e &&
|
|
1088
|
+
e && qe(e.title) && V && V !== e.title && (await _(n, {
|
|
919
1089
|
chatId: f,
|
|
920
1090
|
title: V
|
|
921
1091
|
}), z.publishJson({
|
|
@@ -929,7 +1099,7 @@ var Oe = /* @__PURE__ */ e({
|
|
|
929
1099
|
errorText: "Something went wrong."
|
|
930
1100
|
}), z.publishDone();
|
|
931
1101
|
try {
|
|
932
|
-
await
|
|
1102
|
+
await at(n, f);
|
|
933
1103
|
} catch (e) {
|
|
934
1104
|
console.error("Failed to update chat streaming state.", e);
|
|
935
1105
|
}
|
|
@@ -938,9 +1108,9 @@ var Oe = /* @__PURE__ */ e({
|
|
|
938
1108
|
B && V && b && (async () => {
|
|
939
1109
|
let e = /* @__PURE__ */ new Date();
|
|
940
1110
|
try {
|
|
941
|
-
let { text: t } = await
|
|
942
|
-
model:
|
|
943
|
-
system:
|
|
1111
|
+
let { text: t } = await ie({
|
|
1112
|
+
model: ge(),
|
|
1113
|
+
system: xe,
|
|
944
1114
|
prompt: B,
|
|
945
1115
|
onFinish: async (t) => {
|
|
946
1116
|
await Q({
|
|
@@ -950,7 +1120,7 @@ var Oe = /* @__PURE__ */ e({
|
|
|
950
1120
|
chatId: f,
|
|
951
1121
|
mode: o,
|
|
952
1122
|
feature: "title",
|
|
953
|
-
modelId:
|
|
1123
|
+
modelId: rt,
|
|
954
1124
|
status: "success",
|
|
955
1125
|
billable: !0,
|
|
956
1126
|
usage: t.totalUsage ?? t.usage,
|
|
@@ -958,11 +1128,11 @@ var Oe = /* @__PURE__ */ e({
|
|
|
958
1128
|
startedAt: e
|
|
959
1129
|
});
|
|
960
1130
|
}
|
|
961
|
-
}), r =
|
|
1131
|
+
}), r = Ye(t);
|
|
962
1132
|
if (!r) return;
|
|
963
1133
|
let i = await s(n, f);
|
|
964
1134
|
if (!i) return;
|
|
965
|
-
i.title !== r && (await
|
|
1135
|
+
i.title !== r && (await _(n, {
|
|
966
1136
|
chatId: f,
|
|
967
1137
|
title: r
|
|
968
1138
|
}), z.publishJson({
|
|
@@ -978,7 +1148,7 @@ var Oe = /* @__PURE__ */ e({
|
|
|
978
1148
|
chatId: f,
|
|
979
1149
|
mode: o,
|
|
980
1150
|
feature: "title",
|
|
981
|
-
modelId:
|
|
1151
|
+
modelId: rt,
|
|
982
1152
|
status: "error",
|
|
983
1153
|
billable: !1,
|
|
984
1154
|
error: t,
|
|
@@ -986,8 +1156,8 @@ var Oe = /* @__PURE__ */ e({
|
|
|
986
1156
|
}), console.warn("Failed to generate chat title.", t);
|
|
987
1157
|
}
|
|
988
1158
|
})();
|
|
989
|
-
let
|
|
990
|
-
originalMessages:
|
|
1159
|
+
let ve = ne({
|
|
1160
|
+
originalMessages: I,
|
|
991
1161
|
onError: (e) => (console.error(e), "Something went wrong."),
|
|
992
1162
|
onFinish: async ({ responseMessage: e }) => {
|
|
993
1163
|
let t = Array.isArray(e.parts) ? e.parts : [];
|
|
@@ -1001,7 +1171,7 @@ var Oe = /* @__PURE__ */ e({
|
|
|
1001
1171
|
}]);
|
|
1002
1172
|
},
|
|
1003
1173
|
execute: async ({ writer: e }) => {
|
|
1004
|
-
let t =
|
|
1174
|
+
let t = Ke({
|
|
1005
1175
|
ctx: n,
|
|
1006
1176
|
dataStream: e,
|
|
1007
1177
|
requestHints: P
|
|
@@ -1018,7 +1188,7 @@ var Oe = /* @__PURE__ */ e({
|
|
|
1018
1188
|
"editDocument",
|
|
1019
1189
|
"updateDocument",
|
|
1020
1190
|
"requestSuggestions"
|
|
1021
|
-
], s =
|
|
1191
|
+
], s = pe(h), c = s ? [] : x ? i : a, l = t, u = c, d = await r().resolveTools?.({
|
|
1022
1192
|
ctx: n,
|
|
1023
1193
|
dataStream: e,
|
|
1024
1194
|
requestHints: P,
|
|
@@ -1028,7 +1198,7 @@ var Oe = /* @__PURE__ */ e({
|
|
|
1028
1198
|
defaultActiveTools: c
|
|
1029
1199
|
});
|
|
1030
1200
|
d && (l = d.tools, u = d.activeTools ?? Object.keys(d.tools));
|
|
1031
|
-
let m = await
|
|
1201
|
+
let m = await te(R.map((e) => ({
|
|
1032
1202
|
role: e?.role,
|
|
1033
1203
|
parts: e?.parts,
|
|
1034
1204
|
metadata: e?.metadata
|
|
@@ -1036,14 +1206,14 @@ var Oe = /* @__PURE__ */ e({
|
|
|
1036
1206
|
tools: l,
|
|
1037
1207
|
ignoreIncompleteToolCalls: !0
|
|
1038
1208
|
});
|
|
1039
|
-
await
|
|
1209
|
+
await ht(n, m), x && gt(m);
|
|
1040
1210
|
let g = typeof p.openaiReasoningSummary == "string" ? p.openaiReasoningSummary : null, _;
|
|
1041
1211
|
x && E ? _ = { openai: {
|
|
1042
1212
|
reasoningEffort: E,
|
|
1043
1213
|
reasoningSummary: g ?? void 0
|
|
1044
1214
|
} } : w && s && E && (_ = { xai: { reasoningEffort: E } });
|
|
1045
|
-
let
|
|
1046
|
-
|
|
1215
|
+
let ee = !1, v = /* @__PURE__ */ new Date(), y = async ({ status: e, usage: t, finishReason: r, error: i }) => {
|
|
1216
|
+
ee || (ee = !0, await Q({
|
|
1047
1217
|
ctx: n,
|
|
1048
1218
|
usageContext: k,
|
|
1049
1219
|
agent: O,
|
|
@@ -1056,45 +1226,45 @@ var Oe = /* @__PURE__ */ e({
|
|
|
1056
1226
|
usage: t,
|
|
1057
1227
|
finishReason: r,
|
|
1058
1228
|
error: i,
|
|
1059
|
-
startedAt:
|
|
1229
|
+
startedAt: v
|
|
1060
1230
|
}));
|
|
1061
|
-
},
|
|
1062
|
-
model:
|
|
1063
|
-
system:
|
|
1231
|
+
}, b = S({
|
|
1232
|
+
model: he(h),
|
|
1233
|
+
system: _e({
|
|
1064
1234
|
selectedChatModel: h,
|
|
1065
1235
|
requestHints: P,
|
|
1066
1236
|
agentInstructions: O.instructions,
|
|
1067
|
-
knowledge:
|
|
1237
|
+
knowledge: ce
|
|
1068
1238
|
}),
|
|
1069
1239
|
messages: m,
|
|
1070
1240
|
tools: l,
|
|
1071
|
-
stopWhen:
|
|
1241
|
+
stopWhen: oe(5),
|
|
1072
1242
|
abortSignal: z.abortController.signal,
|
|
1073
1243
|
providerOptions: _,
|
|
1074
1244
|
activeTools: u,
|
|
1075
1245
|
onFinish: async (e) => {
|
|
1076
|
-
await
|
|
1246
|
+
await y({
|
|
1077
1247
|
status: "success",
|
|
1078
1248
|
usage: e.totalUsage ?? e.usage,
|
|
1079
1249
|
finishReason: e.finishReason
|
|
1080
1250
|
});
|
|
1081
1251
|
},
|
|
1082
1252
|
onError: async (e) => {
|
|
1083
|
-
await
|
|
1253
|
+
await y({
|
|
1084
1254
|
status: "error",
|
|
1085
1255
|
error: e?.error ?? e
|
|
1086
1256
|
});
|
|
1087
1257
|
},
|
|
1088
1258
|
onAbort: async (e) => {
|
|
1089
|
-
await
|
|
1259
|
+
await y({
|
|
1090
1260
|
status: "aborted",
|
|
1091
1261
|
usage: e?.totalUsage ?? e?.usage,
|
|
1092
1262
|
finishReason: e?.finishReason
|
|
1093
1263
|
});
|
|
1094
1264
|
}
|
|
1095
1265
|
});
|
|
1096
|
-
e.merge(
|
|
1097
|
-
originalMessages:
|
|
1266
|
+
e.merge(b.toUIMessageStream({
|
|
1267
|
+
originalMessages: I,
|
|
1098
1268
|
sendReasoning: s || !!(x && E)
|
|
1099
1269
|
}));
|
|
1100
1270
|
}
|
|
@@ -1102,7 +1272,7 @@ var Oe = /* @__PURE__ */ e({
|
|
|
1102
1272
|
return (async () => {
|
|
1103
1273
|
let e = null;
|
|
1104
1274
|
try {
|
|
1105
|
-
let t =
|
|
1275
|
+
let t = ve.getReader();
|
|
1106
1276
|
for (e = () => t.releaseLock();;) {
|
|
1107
1277
|
let { value: e, done: n } = await t.read();
|
|
1108
1278
|
if (n) break;
|
|
@@ -1117,24 +1287,24 @@ var Oe = /* @__PURE__ */ e({
|
|
|
1117
1287
|
} finally {
|
|
1118
1288
|
e?.();
|
|
1119
1289
|
try {
|
|
1120
|
-
await
|
|
1290
|
+
await at(n, f);
|
|
1121
1291
|
} catch (e) {
|
|
1122
1292
|
console.error("Failed to update chat streaming state.", e);
|
|
1123
1293
|
}
|
|
1124
1294
|
}
|
|
1125
1295
|
})(), {};
|
|
1126
|
-
},
|
|
1296
|
+
}, Lt = async (e, t) => $({
|
|
1127
1297
|
payload: e,
|
|
1128
1298
|
ctx: t,
|
|
1129
1299
|
mode: "interactive"
|
|
1130
|
-
}),
|
|
1300
|
+
}), Rt = async (e, t) => {
|
|
1131
1301
|
let n = t.req.query.id, r = Array.isArray(n) ? n[0] : n;
|
|
1132
1302
|
return typeof r != "string" || r.trim() === "" ? a(t, "bad_request:api") : p(t, r);
|
|
1133
|
-
},
|
|
1134
|
-
e.get(
|
|
1303
|
+
}, zt = (e) => {
|
|
1304
|
+
e.get(Ze, (async (e, t) => {
|
|
1135
1305
|
let n = t.req.query.id, r = Array.isArray(n) ? n[0] : n;
|
|
1136
1306
|
return typeof r != "string" || r.trim() === "" ? a(t, "bad_request:api", "Parameter id is required.") : await s(t, r) || a(t, "not_found:chat");
|
|
1137
|
-
})), e.post(
|
|
1307
|
+
})), e.post(Ze, Lt), e.delete(Ze, Rt);
|
|
1138
1308
|
};
|
|
1139
1309
|
//#endregion
|
|
1140
|
-
export { $ as n,
|
|
1310
|
+
export { $ as n, Qe as t };
|