@rpcbase/agents-sdk 0.8.0 → 0.10.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/models.d.ts +1 -0
- package/dist/api/chat/models.d.ts.map +1 -1
- package/dist/api/chat/runtimeConfig.d.ts +4 -0
- package/dist/api/chat/runtimeConfig.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/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-C06ydMqf.js → handler-DE8agLBS.js} +313 -304
- package/dist/{handler-DGbBgIt2.js → handler-DSncMeo4.js} +207 -198
- 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 +1 -1
- package/dist/routes.js +1 -1
- 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
|
@@ -1,64 +1,64 @@
|
|
|
1
1
|
import { r as e } from "./openai-reasoning-kFq60GX9.js";
|
|
2
2
|
import { t } from "./apiErrors-CFUx-UrE.js";
|
|
3
3
|
import { i as n, r, t as i } from "./chatCtx-DSgD1fix.js";
|
|
4
|
-
import { _ as a, a as o, b as s, g as c, h as l, l as u, m as d, n as f, p, s as m, v as h, x as g, y as
|
|
5
|
-
import { n as
|
|
6
|
-
import { Output as
|
|
7
|
-
import { getTenantFilesystemDb as
|
|
8
|
-
import { createHash as
|
|
9
|
-
import { GridFSBucket as
|
|
10
|
-
import { createGoogleGenerativeAI as
|
|
11
|
-
import { openai as
|
|
12
|
-
import { createXai as
|
|
13
|
-
import { z as
|
|
4
|
+
import { _ as a, a as o, b as s, g as c, h as l, l as u, m as d, n as f, p, s as m, v as h, x as g, y as ee } from "./chatDb-YfeABtn-.js";
|
|
5
|
+
import { n as _, t as v } from "./resumableSse-BRFonw_y.js";
|
|
6
|
+
import { Output as y, convertToModelMessages as te, createUIMessageStream as ne, extractReasoningMiddleware as b, generateText as re, smoothStream as ie, stepCountIs as ae, streamObject as oe, streamText as se, tool as x, wrapLanguageModel as ce } from "ai";
|
|
7
|
+
import { getTenantFilesystemDb as S } from "@rpcbase/db";
|
|
8
|
+
import { createHash as C, randomUUID as w } from "node:crypto";
|
|
9
|
+
import { GridFSBucket as T, ObjectId as le } from "mongodb";
|
|
10
|
+
import { createGoogleGenerativeAI as E } from "@ai-sdk/google";
|
|
11
|
+
import { openai as D } from "@ai-sdk/openai";
|
|
12
|
+
import { createXai as O } from "@ai-sdk/xai";
|
|
13
|
+
import { z as k } from "zod";
|
|
14
14
|
//#region src/api/chat/models.ts
|
|
15
|
-
var
|
|
15
|
+
var A = /-thinking$/, j = "openai/", M = "google/", N = "xai/", P = new Set([
|
|
16
16
|
"openai/gpt-5.4-mini",
|
|
17
17
|
"openai/gpt-5.5",
|
|
18
18
|
"google/gemini-3.5-flash",
|
|
19
19
|
"xai/grok-4.3-thinking"
|
|
20
|
-
]),
|
|
20
|
+
]), F = (e) => e.startsWith(j), ue = (e) => e.startsWith(M), de = (e) => e.startsWith(N), I = (e) => e.endsWith("-thinking"), fe = (e) => e.includes("/") ? F(e) || ue(e) || de(e) : !1, L = O({
|
|
21
21
|
baseURL: process.env.XAI_BASE_URL ?? "https://api.x.ai/v1",
|
|
22
22
|
apiKey: process.env.XAI_API_KEY
|
|
23
|
-
}),
|
|
24
|
-
if (
|
|
25
|
-
if (
|
|
26
|
-
let t = e.slice(
|
|
27
|
-
return
|
|
23
|
+
}), R = E({ apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY ?? process.env.GOOGLE_API_KEY }), pe = (e) => {
|
|
24
|
+
if (F(e)) return D.responses(e.slice(j.length));
|
|
25
|
+
if (ue(e)) {
|
|
26
|
+
let t = e.slice(M.length).replace(A, ""), n = R.chat(t);
|
|
27
|
+
return I(e) ? ce({
|
|
28
28
|
model: n,
|
|
29
|
-
middleware:
|
|
29
|
+
middleware: b({ tagName: "thinking" })
|
|
30
30
|
}) : n;
|
|
31
31
|
}
|
|
32
|
-
if (
|
|
33
|
-
let t = e.slice(
|
|
34
|
-
return
|
|
32
|
+
if (de(e)) {
|
|
33
|
+
let t = e.slice(N.length).replace(A, "");
|
|
34
|
+
return I(e) ? L.responses(t) : L.chat(t);
|
|
35
35
|
}
|
|
36
36
|
throw Error(`Unsupported modelId: ${e}`);
|
|
37
|
-
}, me = () =>
|
|
37
|
+
}, me = () => D.chat("gpt-5.4-mini"), z = () => D.chat(process.env.AGENT_SDK_ARTIFACT_MODEL ?? "gpt-5.4-mini"), he = "\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) => `\
|
|
38
38
|
About the origin of user's request:
|
|
39
39
|
- lat: ${e.latitude ?? "unknown"}
|
|
40
40
|
- lon: ${e.longitude ?? "unknown"}
|
|
41
41
|
- city: ${e.city ?? "unknown"}
|
|
42
42
|
- country: ${e.country ?? "unknown"}
|
|
43
|
-
`,
|
|
43
|
+
`, ge = ({ selectedChatModel: e, requestHints: t, agentInstructions: n, knowledge: r }) => {
|
|
44
44
|
let i = V(t), a = [n?.trim() ? `Agent instructions:\n${n.trim()}` : "", r?.trim() ? `Knowledge context:\n${r.trim()}` : ""].filter(Boolean).join("\n\n");
|
|
45
45
|
if (e.includes("reasoning") || e.includes("thinking")) {
|
|
46
46
|
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}`;
|
|
47
47
|
return a ? `${t}\n\n${a}` : t;
|
|
48
48
|
}
|
|
49
|
-
let o = `${B}\n\n${i}\n\n${
|
|
49
|
+
let o = `${B}\n\n${i}\n\n${he}`;
|
|
50
50
|
return a ? `${o}\n\n${a}` : o;
|
|
51
|
-
}, H = "\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",
|
|
51
|
+
}, H = "\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", _e = "\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", U = (e, t) => {
|
|
52
52
|
let n = "document";
|
|
53
53
|
return t === "code" ? n = "script" : t === "sheet" && (n = "spreadsheet"), `Rewrite the following ${n} based on the given prompt.
|
|
54
54
|
|
|
55
55
|
${e}`;
|
|
56
|
-
},
|
|
57
|
-
let t =
|
|
56
|
+
}, ve = "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", ye = 24, be = 8e4, xe = 16e3, Se = "agent-runtime-history-compaction-summary", W = (e) => !!e && typeof e == "object" && !Array.isArray(e), G = (e) => typeof e == "string" && e.trim() ? e.trim() : null, Ce = (e) => {
|
|
57
|
+
let t = G((W(e.metadata) ? e.metadata : null)?.createdAt);
|
|
58
58
|
if (!t) return null;
|
|
59
59
|
let n = new Date(t);
|
|
60
60
|
return Number.isNaN(n.getTime()) ? null : t;
|
|
61
|
-
},
|
|
61
|
+
}, K = (e, t = 2e3) => {
|
|
62
62
|
if (typeof e == "string") return e.length > t ? `${e.slice(0, t)}...` : e;
|
|
63
63
|
if (typeof e == "number" || typeof e == "boolean") return String(e);
|
|
64
64
|
if (!e || typeof e != "object") return "";
|
|
@@ -68,52 +68,52 @@ ${e}`;
|
|
|
68
68
|
} catch {
|
|
69
69
|
return "[unserializable content]";
|
|
70
70
|
}
|
|
71
|
-
},
|
|
72
|
-
if (!
|
|
73
|
-
let t =
|
|
71
|
+
}, we = (e) => (Array.isArray(e.parts) ? e.parts : []).flatMap((e) => {
|
|
72
|
+
if (!W(e)) {
|
|
73
|
+
let t = K(e);
|
|
74
74
|
return t ? [t] : [];
|
|
75
75
|
}
|
|
76
|
-
let t =
|
|
76
|
+
let t = G(e.type) ?? "part", n = K(e.text);
|
|
77
77
|
if (n) return [`${t}: ${n}`];
|
|
78
|
-
let r =
|
|
78
|
+
let r = K(e.output);
|
|
79
79
|
if (r) return [`${t}: ${r}`];
|
|
80
|
-
let i =
|
|
80
|
+
let i = K(e.result);
|
|
81
81
|
if (i) return [`${t}: ${i}`];
|
|
82
|
-
let a =
|
|
82
|
+
let a = K(e.content);
|
|
83
83
|
if (a) return [`${t}: ${a}`];
|
|
84
|
-
let o =
|
|
84
|
+
let o = K(e, 1e3);
|
|
85
85
|
return o ? [`${t}: ${o}`] : [];
|
|
86
|
-
}).join("\n").trim(),
|
|
86
|
+
}).join("\n").trim(), Te = (e) => e.reduce((e, t) => e + we(t).length, 0), Ee = (e, t) => {
|
|
87
87
|
if (e.length <= t) return e;
|
|
88
88
|
if (t <= 32) return e.slice(0, t);
|
|
89
89
|
let n = Math.floor(t * .7), r = Math.max(0, t - n - 24);
|
|
90
90
|
return `${e.slice(0, n)}\n...[compacted]...\n${e.slice(e.length - r)}`;
|
|
91
|
-
},
|
|
92
|
-
maxMessages: Number.isFinite(e.maxMessages) && Number(e.maxMessages) > 0 ? Math.floor(Number(e.maxMessages)) :
|
|
93
|
-
maxEstimatedChars: Number.isFinite(e.maxEstimatedChars) && Number(e.maxEstimatedChars) > 0 ? Math.floor(Number(e.maxEstimatedChars)) :
|
|
94
|
-
maxSummaryChars: Number.isFinite(e.maxSummaryChars) && Number(e.maxSummaryChars) > 0 ? Math.floor(Number(e.maxSummaryChars)) :
|
|
91
|
+
}, De = (e) => e ? {
|
|
92
|
+
maxMessages: Number.isFinite(e.maxMessages) && Number(e.maxMessages) > 0 ? Math.floor(Number(e.maxMessages)) : ye,
|
|
93
|
+
maxEstimatedChars: Number.isFinite(e.maxEstimatedChars) && Number(e.maxEstimatedChars) > 0 ? Math.floor(Number(e.maxEstimatedChars)) : be,
|
|
94
|
+
maxSummaryChars: Number.isFinite(e.maxSummaryChars) && Number(e.maxSummaryChars) > 0 ? Math.floor(Number(e.maxSummaryChars)) : xe
|
|
95
95
|
} : {
|
|
96
|
-
maxMessages:
|
|
97
|
-
maxEstimatedChars:
|
|
98
|
-
maxSummaryChars:
|
|
99
|
-
},
|
|
96
|
+
maxMessages: ye,
|
|
97
|
+
maxEstimatedChars: be,
|
|
98
|
+
maxSummaryChars: xe
|
|
99
|
+
}, q = (e) => W(e) && e.type === "agent-runtime-history-compaction" && e.version === 1 && typeof e.summary == "string", Oe = (e) => q(e) ? e : W(e) && q(e.historyCompaction) ? e.historyCompaction : null, ke = (e, t) => t ? W(e) && !q(e) ? {
|
|
100
100
|
...e,
|
|
101
101
|
historyCompaction: t
|
|
102
|
-
} : { historyCompaction: t } : e ?? null,
|
|
103
|
-
let n =
|
|
104
|
-
return n ? e.findIndex((e) =>
|
|
105
|
-
},
|
|
102
|
+
} : { historyCompaction: t } : e ?? null, Ae = (e) => G(e.id), je = (e, t) => {
|
|
103
|
+
let n = G(t?.compactedThroughMessageId);
|
|
104
|
+
return n ? e.findIndex((e) => Ae(e) === n) : -1;
|
|
105
|
+
}, Me = (e, t) => {
|
|
106
106
|
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);
|
|
107
|
-
for (; i < e.length - r &&
|
|
107
|
+
for (; i < e.length - r && Te(e.slice(i)) > t.maxEstimatedChars;) i += 1;
|
|
108
108
|
return i;
|
|
109
|
-
},
|
|
110
|
-
let t =
|
|
109
|
+
}, Ne = (e) => {
|
|
110
|
+
let t = G(e.role) ?? "message", n = Ce(e), r = we(e);
|
|
111
111
|
return `${n ? `${t} (${n})` : t}:\n${r || "[non-text content]"}`;
|
|
112
|
-
},
|
|
113
|
-
let r = e?.summary?.trim(), i = t.map(
|
|
114
|
-
return
|
|
115
|
-
},
|
|
116
|
-
id: `${
|
|
112
|
+
}, Pe = ({ previousContext: e, messagesToCompact: t, limits: n }) => {
|
|
113
|
+
let r = e?.summary?.trim(), i = t.map(Ne).join("\n\n").trim();
|
|
114
|
+
return Ee([r ? `Earlier summary:\n${r}` : null, i ? `Compacted transcript:\n${i}` : null].filter((e) => !!e).join("\n\n"), n.maxSummaryChars).trim();
|
|
115
|
+
}, Fe = (e) => ({
|
|
116
|
+
id: `${Se}:${G(e.compactedThroughMessageId) ?? "unknown"}`,
|
|
117
117
|
role: "user",
|
|
118
118
|
parts: [{
|
|
119
119
|
type: "text",
|
|
@@ -129,42 +129,42 @@ ${e}`;
|
|
|
129
129
|
transient: !0,
|
|
130
130
|
historyCompaction: !0
|
|
131
131
|
}
|
|
132
|
-
}),
|
|
133
|
-
let r =
|
|
134
|
-
if (!(i.length > n.maxMessages ||
|
|
135
|
-
messages:
|
|
132
|
+
}), J = (e, t) => t?.summary.trim() ? [Fe(t), ...e] : e, Ie = ({ messages: e, previousContext: t, limits: n }) => {
|
|
133
|
+
let r = je(e, t), i = r >= 0 ? e.slice(r + 1) : e;
|
|
134
|
+
if (!(i.length > n.maxMessages || Te(i) > n.maxEstimatedChars)) return {
|
|
135
|
+
messages: J(i, t),
|
|
136
136
|
context: t
|
|
137
137
|
};
|
|
138
|
-
let a =
|
|
138
|
+
let a = Me(i, n);
|
|
139
139
|
if (a <= 0) return {
|
|
140
|
-
messages:
|
|
140
|
+
messages: J(i, t),
|
|
141
141
|
context: t
|
|
142
142
|
};
|
|
143
143
|
let o = i.slice(0, a), s = i.slice(a), c = o.at(-1) ?? null, l = {
|
|
144
144
|
type: "agent-runtime-history-compaction",
|
|
145
145
|
version: 1,
|
|
146
|
-
summary:
|
|
146
|
+
summary: Pe({
|
|
147
147
|
previousContext: t,
|
|
148
148
|
messagesToCompact: o,
|
|
149
149
|
limits: n
|
|
150
150
|
}),
|
|
151
|
-
compactedThroughMessageId: c ?
|
|
152
|
-
compactedThroughMessageCreatedAt: c ?
|
|
151
|
+
compactedThroughMessageId: c ? Ae(c) : t?.compactedThroughMessageId ?? null,
|
|
152
|
+
compactedThroughMessageCreatedAt: c ? Ce(c) : t?.compactedThroughMessageCreatedAt ?? null,
|
|
153
153
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
154
154
|
messageCount: e.length
|
|
155
155
|
};
|
|
156
156
|
return {
|
|
157
|
-
messages:
|
|
157
|
+
messages: J(s, l),
|
|
158
158
|
context: l
|
|
159
159
|
};
|
|
160
|
-
},
|
|
160
|
+
}, Le = async ({ ctx: e, chatId: t, agent: n, selectedChatModel: i, mode: a, messages: o, lastContext: s }) => {
|
|
161
161
|
let c = r().historyCompaction;
|
|
162
162
|
if (c === !1 || c && c.enabled === !1) return {
|
|
163
163
|
messages: o,
|
|
164
164
|
lastContext: s ?? null,
|
|
165
165
|
lastContextChanged: !1
|
|
166
166
|
};
|
|
167
|
-
let l =
|
|
167
|
+
let l = De(c), u = Oe(s);
|
|
168
168
|
try {
|
|
169
169
|
let r = typeof c == "object" && typeof c.compact == "function" ? await c.compact({
|
|
170
170
|
ctx: e,
|
|
@@ -175,14 +175,14 @@ ${e}`;
|
|
|
175
175
|
messages: o,
|
|
176
176
|
previousContext: u,
|
|
177
177
|
limits: l
|
|
178
|
-
}) :
|
|
178
|
+
}) : Ie({
|
|
179
179
|
messages: o,
|
|
180
180
|
previousContext: u,
|
|
181
181
|
limits: l
|
|
182
182
|
}), d = Array.isArray(r.messages) ? r.messages : o, f = r.context === void 0 ? u : r.context;
|
|
183
183
|
return {
|
|
184
184
|
messages: d,
|
|
185
|
-
lastContext:
|
|
185
|
+
lastContext: ke(s, f ?? null),
|
|
186
186
|
lastContextChanged: f !== u
|
|
187
187
|
};
|
|
188
188
|
} catch (e) {
|
|
@@ -192,11 +192,11 @@ ${e}`;
|
|
|
192
192
|
lastContextChanged: !1
|
|
193
193
|
};
|
|
194
194
|
}
|
|
195
|
-
},
|
|
195
|
+
}, Re = [
|
|
196
196
|
"text",
|
|
197
197
|
"code",
|
|
198
198
|
"sheet"
|
|
199
|
-
],
|
|
199
|
+
], ze = (e) => e ? e === "1" || e.toLowerCase() === "true" || e.toLowerCase() === "yes" : !1, Y = () => !ze(process.env.AGENT_SDK_LIVE_TOOLS) && (ze(process.env.PLAYWRIGHT) || process.env.NODE_ENV === "test"), Be = async ({ system: e, prompt: t, dataStream: n, providerOptions: r }) => {
|
|
200
200
|
if (Y()) {
|
|
201
201
|
let e = `Stub document: ${t}`;
|
|
202
202
|
return n.write({
|
|
@@ -205,8 +205,8 @@ ${e}`;
|
|
|
205
205
|
transient: !0
|
|
206
206
|
}), e;
|
|
207
207
|
}
|
|
208
|
-
let i = "", { fullStream: a } =
|
|
209
|
-
model:
|
|
208
|
+
let i = "", { fullStream: a } = se({
|
|
209
|
+
model: z(),
|
|
210
210
|
system: e,
|
|
211
211
|
experimental_transform: ie({ chunking: "word" }),
|
|
212
212
|
prompt: t,
|
|
@@ -218,7 +218,7 @@ ${e}`;
|
|
|
218
218
|
transient: !0
|
|
219
219
|
}));
|
|
220
220
|
return i;
|
|
221
|
-
},
|
|
221
|
+
}, Ve = async ({ system: e, prompt: t, dataStream: n }) => {
|
|
222
222
|
if (Y()) {
|
|
223
223
|
let e = `print("Stub code for: ${t}")\n`;
|
|
224
224
|
return n.write({
|
|
@@ -227,11 +227,11 @@ ${e}`;
|
|
|
227
227
|
transient: !0
|
|
228
228
|
}), e;
|
|
229
229
|
}
|
|
230
|
-
let r = "", { fullStream: i } =
|
|
231
|
-
model:
|
|
230
|
+
let r = "", { fullStream: i } = oe({
|
|
231
|
+
model: z(),
|
|
232
232
|
system: e,
|
|
233
233
|
prompt: t,
|
|
234
|
-
schema:
|
|
234
|
+
schema: k.object({ code: k.string() })
|
|
235
235
|
});
|
|
236
236
|
for await (let e of i) {
|
|
237
237
|
if (e.type !== "object") continue;
|
|
@@ -243,7 +243,7 @@ ${e}`;
|
|
|
243
243
|
}), r = t);
|
|
244
244
|
}
|
|
245
245
|
return r;
|
|
246
|
-
},
|
|
246
|
+
}, He = async ({ system: e, prompt: t, dataStream: n }) => {
|
|
247
247
|
if (Y()) {
|
|
248
248
|
let e = `name,value\n${t.replaceAll(",", " ")},1\n`;
|
|
249
249
|
return n.write({
|
|
@@ -252,11 +252,11 @@ ${e}`;
|
|
|
252
252
|
transient: !0
|
|
253
253
|
}), e;
|
|
254
254
|
}
|
|
255
|
-
let r = "", { fullStream: i } =
|
|
256
|
-
model:
|
|
255
|
+
let r = "", { fullStream: i } = oe({
|
|
256
|
+
model: z(),
|
|
257
257
|
system: e,
|
|
258
258
|
prompt: t,
|
|
259
|
-
schema:
|
|
259
|
+
schema: k.object({ csv: k.string().describe("CSV data") })
|
|
260
260
|
});
|
|
261
261
|
for await (let e of i) {
|
|
262
262
|
if (e.type !== "object") continue;
|
|
@@ -272,7 +272,7 @@ ${e}`;
|
|
|
272
272
|
data: r,
|
|
273
273
|
transient: !0
|
|
274
274
|
}), r;
|
|
275
|
-
},
|
|
275
|
+
}, Ue = async (e) => {
|
|
276
276
|
try {
|
|
277
277
|
let t = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(e)}&count=1&language=en&format=json`);
|
|
278
278
|
if (!t.ok) return null;
|
|
@@ -286,22 +286,22 @@ ${e}`;
|
|
|
286
286
|
} catch {
|
|
287
287
|
return null;
|
|
288
288
|
}
|
|
289
|
-
},
|
|
289
|
+
}, We = ({ ctx: e, dataStream: t, requestHints: n }) => {
|
|
290
290
|
let r = i(e), a = async (t) => {
|
|
291
291
|
let n = await m(e, t);
|
|
292
292
|
return n.length > 0 ? n.at(-1) ?? null : null;
|
|
293
|
-
}, o =
|
|
293
|
+
}, o = x({
|
|
294
294
|
description: "Get the current weather at a location. You can provide either coordinates or a city name.",
|
|
295
|
-
inputSchema:
|
|
296
|
-
latitude:
|
|
297
|
-
longitude:
|
|
298
|
-
city:
|
|
295
|
+
inputSchema: k.object({
|
|
296
|
+
latitude: k.number().optional(),
|
|
297
|
+
longitude: k.number().optional(),
|
|
298
|
+
city: k.string().describe("City name (e.g., 'San Francisco', 'New York', 'London')").optional()
|
|
299
299
|
}),
|
|
300
300
|
needsApproval: !0,
|
|
301
301
|
execute: async (e) => {
|
|
302
302
|
let t, n;
|
|
303
303
|
if (e.city) {
|
|
304
|
-
let r = await
|
|
304
|
+
let r = await Ue(e.city);
|
|
305
305
|
if (!r) return { error: `Could not find coordinates for "${e.city}". Please check the city name.` };
|
|
306
306
|
t = r.latitude, n = r.longitude;
|
|
307
307
|
} else if (e.latitude !== void 0 && e.longitude !== void 0) t = e.latitude, n = e.longitude;
|
|
@@ -309,14 +309,14 @@ ${e}`;
|
|
|
309
309
|
let r = await (await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${t}&longitude=${n}¤t=temperature_2m&hourly=temperature_2m&daily=sunrise,sunset&timezone=auto`)).json();
|
|
310
310
|
return "city" in e && (r.cityName = e.city), r;
|
|
311
311
|
}
|
|
312
|
-
}), s =
|
|
312
|
+
}), s = x({
|
|
313
313
|
description: "Create an artifact for writing or content creation. Use kind 'code' for programming, 'text' for writing, and 'sheet' for spreadsheets or tabular data.",
|
|
314
|
-
inputSchema:
|
|
315
|
-
title:
|
|
316
|
-
kind:
|
|
314
|
+
inputSchema: k.object({
|
|
315
|
+
title: k.string().describe("The title of the artifact"),
|
|
316
|
+
kind: k.enum(Re).describe("Use 'code' for programming, 'text' for prose, and 'sheet' for CSV/tabular data")
|
|
317
317
|
}),
|
|
318
318
|
execute: async ({ title: n, kind: r }) => {
|
|
319
|
-
let i =
|
|
319
|
+
let i = w();
|
|
320
320
|
t.write({
|
|
321
321
|
type: "data-kind",
|
|
322
322
|
data: r,
|
|
@@ -335,16 +335,16 @@ ${e}`;
|
|
|
335
335
|
transient: !0
|
|
336
336
|
});
|
|
337
337
|
let a = "";
|
|
338
|
-
return r === "text" ? a = await
|
|
338
|
+
return r === "text" ? a = await Be({
|
|
339
339
|
system: "Write about the given topic. Markdown is supported. Use headings wherever appropriate.",
|
|
340
340
|
prompt: n,
|
|
341
341
|
dataStream: t
|
|
342
|
-
}) : r === "code" ? a = await
|
|
342
|
+
}) : r === "code" ? a = await Ve({
|
|
343
343
|
system: H,
|
|
344
344
|
prompt: n,
|
|
345
345
|
dataStream: t
|
|
346
|
-
}) : r === "sheet" && (a = await
|
|
347
|
-
system:
|
|
346
|
+
}) : r === "sheet" && (a = await He({
|
|
347
|
+
system: _e,
|
|
348
348
|
prompt: n,
|
|
349
349
|
dataStream: t
|
|
350
350
|
})), await d(e, {
|
|
@@ -363,37 +363,37 @@ ${e}`;
|
|
|
363
363
|
content: r === "code" ? "A script was created and is now visible to the user." : "A document was created and is now visible to the user."
|
|
364
364
|
};
|
|
365
365
|
}
|
|
366
|
-
}), l =
|
|
366
|
+
}), l = x({
|
|
367
367
|
description: "Fully rewrite an existing artifact with the given description. Use editDocument instead for targeted find-and-replace changes.",
|
|
368
|
-
inputSchema:
|
|
369
|
-
id:
|
|
370
|
-
description:
|
|
368
|
+
inputSchema: k.object({
|
|
369
|
+
id: k.string().describe("The ID of the artifact to rewrite"),
|
|
370
|
+
description: k.string().default("Improve the content").describe("The description of changes that need to be made")
|
|
371
371
|
}),
|
|
372
372
|
execute: async ({ id: n, description: r }) => {
|
|
373
373
|
let i = await a(n);
|
|
374
374
|
if (!i) return { error: "Document not found" };
|
|
375
375
|
let o = i.kind;
|
|
376
|
-
if (!
|
|
376
|
+
if (!Re.includes(o)) return { error: `Unsupported document kind: ${i.kind}` };
|
|
377
377
|
t.write({
|
|
378
378
|
type: "data-clear",
|
|
379
379
|
data: null,
|
|
380
380
|
transient: !0
|
|
381
381
|
});
|
|
382
382
|
let s = "";
|
|
383
|
-
return o === "text" ? s = await
|
|
384
|
-
system:
|
|
383
|
+
return o === "text" ? s = await Be({
|
|
384
|
+
system: U(i.content, i.kind),
|
|
385
385
|
prompt: r,
|
|
386
386
|
dataStream: t,
|
|
387
387
|
providerOptions: { openai: { prediction: {
|
|
388
388
|
type: "content",
|
|
389
389
|
content: i.content
|
|
390
390
|
} } }
|
|
391
|
-
}) : o === "code" ? s = await
|
|
392
|
-
system:
|
|
391
|
+
}) : o === "code" ? s = await Ve({
|
|
392
|
+
system: U(i.content, i.kind),
|
|
393
393
|
prompt: r,
|
|
394
394
|
dataStream: t
|
|
395
|
-
}) : o === "sheet" && (s = await
|
|
396
|
-
system:
|
|
395
|
+
}) : o === "sheet" && (s = await He({
|
|
396
|
+
system: U(i.content, i.kind),
|
|
397
397
|
prompt: r,
|
|
398
398
|
dataStream: t
|
|
399
399
|
})), await d(e, {
|
|
@@ -416,20 +416,20 @@ ${e}`;
|
|
|
416
416
|
return {
|
|
417
417
|
getWeather: o,
|
|
418
418
|
createDocument: s,
|
|
419
|
-
editDocument:
|
|
419
|
+
editDocument: x({
|
|
420
420
|
description: "Make a targeted edit to an existing artifact by replacing an exact string. Prefer this over updateDocument for small changes, fixes, renames, and isolated edits.",
|
|
421
|
-
inputSchema:
|
|
422
|
-
id:
|
|
423
|
-
old_string:
|
|
424
|
-
new_string:
|
|
425
|
-
replace_all:
|
|
421
|
+
inputSchema: k.object({
|
|
422
|
+
id: k.string().describe("The ID of the artifact to edit"),
|
|
423
|
+
old_string: k.string().min(1).describe("Exact string to find. Include enough surrounding context to make the match unique."),
|
|
424
|
+
new_string: k.string().describe("Replacement string"),
|
|
425
|
+
replace_all: k.boolean().optional().describe("Replace all occurrences instead of only the first one")
|
|
426
426
|
}),
|
|
427
427
|
execute: async ({ id: n, old_string: r, new_string: i, replace_all: o }) => {
|
|
428
428
|
let s = await a(n);
|
|
429
429
|
if (!s) return { error: "Document not found" };
|
|
430
430
|
if (!s.content) return { error: "Document has no content" };
|
|
431
431
|
let c = s.kind;
|
|
432
|
-
if (!
|
|
432
|
+
if (!Re.includes(c)) return { error: `Unsupported document kind: ${s.kind}` };
|
|
433
433
|
if (!s.content.includes(r)) return { error: "old_string not found in document" };
|
|
434
434
|
let l = o ? s.content.replaceAll(r, i) : s.content.replace(r, i);
|
|
435
435
|
return await d(e, {
|
|
@@ -466,9 +466,9 @@ ${e}`;
|
|
|
466
466
|
}
|
|
467
467
|
}),
|
|
468
468
|
updateDocument: l,
|
|
469
|
-
requestSuggestions:
|
|
469
|
+
requestSuggestions: x({
|
|
470
470
|
description: "Request writing suggestions for an existing document artifact. Only use this when the user explicitly asks to improve or get suggestions for a document they have already created. Never use for general questions.",
|
|
471
|
-
inputSchema:
|
|
471
|
+
inputSchema: k.object({ documentId: k.string().describe("The UUID of an existing document artifact that was previously created with createDocument") }),
|
|
472
472
|
execute: async ({ documentId: n }) => {
|
|
473
473
|
let i = await a(n);
|
|
474
474
|
if (!i?.content) return { error: "Document not found" };
|
|
@@ -477,7 +477,7 @@ ${e}`;
|
|
|
477
477
|
originalText: "Stub original sentence.",
|
|
478
478
|
suggestedText: "Stub suggested sentence.",
|
|
479
479
|
description: "Stub suggestion",
|
|
480
|
-
id:
|
|
480
|
+
id: w(),
|
|
481
481
|
documentId: n,
|
|
482
482
|
documentCreatedAt: i.createdAt,
|
|
483
483
|
isResolved: !1,
|
|
@@ -498,14 +498,14 @@ ${e}`;
|
|
|
498
498
|
message: "Suggestions have been added to the document"
|
|
499
499
|
};
|
|
500
500
|
}
|
|
501
|
-
let o = [], { partialOutputStream: s } =
|
|
502
|
-
model:
|
|
501
|
+
let o = [], { partialOutputStream: s } = se({
|
|
502
|
+
model: z(),
|
|
503
503
|
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.",
|
|
504
504
|
prompt: i.content,
|
|
505
|
-
output:
|
|
506
|
-
originalSentence:
|
|
507
|
-
suggestedSentence:
|
|
508
|
-
description:
|
|
505
|
+
output: y.array({ element: k.object({
|
|
506
|
+
originalSentence: k.string().describe("The original sentence"),
|
|
507
|
+
suggestedSentence: k.string().describe("The suggested sentence"),
|
|
508
|
+
description: k.string().describe("The description of the suggestion")
|
|
509
509
|
}) })
|
|
510
510
|
}), l = 0;
|
|
511
511
|
for await (let e of s) if (e) for (let a = l; a < e.length; a++) {
|
|
@@ -515,7 +515,7 @@ ${e}`;
|
|
|
515
515
|
originalText: s.originalSentence,
|
|
516
516
|
suggestedText: s.suggestedSentence,
|
|
517
517
|
description: s.description,
|
|
518
|
-
id:
|
|
518
|
+
id: w(),
|
|
519
519
|
documentId: n,
|
|
520
520
|
documentCreatedAt: i.createdAt,
|
|
521
521
|
isResolved: !1,
|
|
@@ -539,7 +539,7 @@ ${e}`;
|
|
|
539
539
|
};
|
|
540
540
|
}
|
|
541
541
|
}),
|
|
542
|
-
webSearch:
|
|
542
|
+
webSearch: D.tools.webSearch({
|
|
543
543
|
externalWebAccess: !0,
|
|
544
544
|
searchContextSize: "medium",
|
|
545
545
|
userLocation: {
|
|
@@ -549,34 +549,43 @@ ${e}`;
|
|
|
549
549
|
}
|
|
550
550
|
})
|
|
551
551
|
};
|
|
552
|
-
},
|
|
552
|
+
}, Ge = (e) => e ? !e.trim() : !0, Ke = (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(), qe = (e) => {
|
|
553
553
|
let t = e.trim();
|
|
554
554
|
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;
|
|
555
|
-
},
|
|
555
|
+
}, Je = (e) => {
|
|
556
556
|
let t = e.trim().toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, "").replace(/\s+/g, " ").trim();
|
|
557
557
|
return t === "hi" || t === "hello" || t === "hey" || t === "salut" || t === "bonjour";
|
|
558
558
|
}, X = (e) => {
|
|
559
559
|
let t = e.trim();
|
|
560
|
-
return !t ||
|
|
561
|
-
},
|
|
562
|
-
|
|
563
|
-
var
|
|
564
|
-
let n =
|
|
560
|
+
return !t || Je(t) ? "" : qe(t.replaceAll("\n", " ").replaceAll("\r", " ").split(/\s+/).filter((e) => e.trim() !== "").slice(0, 5).join(" "));
|
|
561
|
+
}, Ye = "/api/rb/agents/chat";
|
|
562
|
+
k.any(), k.any();
|
|
563
|
+
var Xe = 1024 * 1024 * 20, Ze = 1e5, Qe = /^\/api\/rb\/files\/([^/]+)\/?$/, $e = "test-model", et = "openai/gpt-5.4-mini", tt = async (e) => new Promise((t) => setTimeout(t, e)), nt = async (e, t) => {
|
|
564
|
+
let n = _(t);
|
|
565
565
|
n && !n.isDone || await s(e, {
|
|
566
566
|
chatId: t,
|
|
567
567
|
isStreaming: !1
|
|
568
568
|
});
|
|
569
|
-
},
|
|
569
|
+
}, rt = (e) => e ? e === "1" || e.toLowerCase() === "true" || e.toLowerCase() === "yes" : !1, Z = (e) => typeof e == "string" && e.trim() || null, it = () => {
|
|
570
|
+
let e = r().chatModels;
|
|
571
|
+
if (!e?.length) return P;
|
|
572
|
+
let t = /* @__PURE__ */ new Set();
|
|
573
|
+
for (let n of e) {
|
|
574
|
+
let e = Z(typeof n == "string" ? n : n.id);
|
|
575
|
+
e && t.add(e);
|
|
576
|
+
}
|
|
577
|
+
return t;
|
|
578
|
+
}, at = (e) => {
|
|
570
579
|
if (typeof e == "boolean") return e;
|
|
571
580
|
let t = Z(e)?.toLowerCase();
|
|
572
581
|
return t ? t === "true" ? !0 : t === "false" ? !1 : null : null;
|
|
573
|
-
},
|
|
582
|
+
}, ot = (e) => {
|
|
574
583
|
let t = e.req?.session?.user?.currentTenantId;
|
|
575
584
|
return Z(t);
|
|
576
|
-
},
|
|
585
|
+
}, st = (e) => {
|
|
577
586
|
let t = e.req?.session?.user?.id;
|
|
578
587
|
return Z(t);
|
|
579
|
-
},
|
|
588
|
+
}, ct = () => Z(process.env.RB_FILESYSTEM_BUCKET_NAME) ?? "fs", lt = (e) => C("sha256").update(Buffer.from(e)).digest("hex"), ut = (e) => {
|
|
580
589
|
let t = e.trim();
|
|
581
590
|
if (!t) return null;
|
|
582
591
|
let n;
|
|
@@ -589,36 +598,36 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
589
598
|
return null;
|
|
590
599
|
}
|
|
591
600
|
}
|
|
592
|
-
let r =
|
|
601
|
+
let r = Qe.exec(n.pathname);
|
|
593
602
|
if (!r) return null;
|
|
594
603
|
let i = Z(decodeURIComponent(r[1]));
|
|
595
604
|
return i ? {
|
|
596
605
|
fileId: i,
|
|
597
606
|
uploadKey: Z(n.searchParams.get("rb-upload-key"))
|
|
598
607
|
} : null;
|
|
599
|
-
},
|
|
600
|
-
let r =
|
|
608
|
+
}, dt = async ({ ctx: e, fileId: t, uploadKey: n }) => {
|
|
609
|
+
let r = ot(e);
|
|
601
610
|
if (!r) throw Error("tenant_missing");
|
|
602
611
|
let i;
|
|
603
612
|
try {
|
|
604
|
-
i = new
|
|
613
|
+
i = new le(t);
|
|
605
614
|
} catch {
|
|
606
615
|
throw Error("invalid_file_id");
|
|
607
616
|
}
|
|
608
|
-
let a = (await
|
|
617
|
+
let a = (await S(r)).db;
|
|
609
618
|
if (!a) throw Error("filesystem_db_unavailable");
|
|
610
|
-
let o = new
|
|
619
|
+
let o = new T(a, { bucketName: ct() }), [s] = await o.find({ _id: i }).limit(1).toArray();
|
|
611
620
|
if (!s) throw Error("file_not_found");
|
|
612
|
-
let c =
|
|
621
|
+
let c = at(s?.metadata?.isPublic) ?? !1, l = st(e), u = Z(s?.metadata?.userId), d = Z(s?.metadata?.ownerKeyHash);
|
|
613
622
|
if (!c) {
|
|
614
|
-
let e = !!(l && u && l === u), t = !!(n && d &&
|
|
623
|
+
let e = !!(l && u && l === u), t = !!(n && d && lt(n) === d);
|
|
615
624
|
if (!e && !t) throw Error("unauthorized");
|
|
616
625
|
}
|
|
617
626
|
let f = Z(s?.metadata?.mimeType), p = [], m = 0;
|
|
618
627
|
return await new Promise((e, t) => {
|
|
619
628
|
let n = o.openDownloadStream(i);
|
|
620
629
|
n.on("data", (e) => {
|
|
621
|
-
if (m += e.length, m >
|
|
630
|
+
if (m += e.length, m > Xe) {
|
|
622
631
|
n.destroy(/* @__PURE__ */ Error("attachment_too_large"));
|
|
623
632
|
return;
|
|
624
633
|
}
|
|
@@ -628,16 +637,16 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
628
637
|
data: Buffer.concat(p, m),
|
|
629
638
|
mimeType: f
|
|
630
639
|
};
|
|
631
|
-
},
|
|
640
|
+
}, ft = async (e, t) => {
|
|
632
641
|
let n = Array.isArray(t) ? t : [], r = [];
|
|
633
642
|
for (let t of n) {
|
|
634
643
|
if (!t || typeof t != "object" || !("role" in t) || t.role !== "user") continue;
|
|
635
644
|
let n = Array.isArray(t.content) ? t.content : null;
|
|
636
645
|
if (n) for (let t of n) {
|
|
637
646
|
if (!t || typeof t != "object" || t.type !== "file" || typeof t.data != "string") continue;
|
|
638
|
-
let n =
|
|
647
|
+
let n = ut(t.data);
|
|
639
648
|
n && r.push((async () => {
|
|
640
|
-
let { data: r, mimeType: i } = await
|
|
649
|
+
let { data: r, mimeType: i } = await dt({
|
|
641
650
|
ctx: e,
|
|
642
651
|
...n
|
|
643
652
|
});
|
|
@@ -646,7 +655,7 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
646
655
|
}
|
|
647
656
|
}
|
|
648
657
|
r.length > 0 && await Promise.all(r);
|
|
649
|
-
},
|
|
658
|
+
}, pt = (e) => {
|
|
650
659
|
let t = Array.isArray(e) ? e : [];
|
|
651
660
|
for (let e of t) {
|
|
652
661
|
if (!e || typeof e != "object" || !("role" in e) || e.role !== "user") continue;
|
|
@@ -659,7 +668,7 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
659
668
|
let i = Z(n.filename) ?? Z(n.name) ?? `attachment-${e + 1}`, a = n.data;
|
|
660
669
|
if (r?.startsWith("text/") && a instanceof Uint8Array) {
|
|
661
670
|
let n = (Buffer.isBuffer(a) ? a : Buffer.from(a)).toString("utf8");
|
|
662
|
-
n.length >
|
|
671
|
+
n.length > Ze && (n = `${n.slice(0, Ze)}\n\n[Truncated]`);
|
|
663
672
|
let o = r === "text/csv" || i.toLowerCase().endsWith(".csv") ? "csv" : "";
|
|
664
673
|
t[e] = {
|
|
665
674
|
type: "text",
|
|
@@ -674,14 +683,14 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
674
683
|
};
|
|
675
684
|
}
|
|
676
685
|
}
|
|
677
|
-
},
|
|
678
|
-
if (e.message?.role === "user") return
|
|
686
|
+
}, mt = async (e, t, n) => {
|
|
687
|
+
if (e.message?.role === "user") return Ke(e.message.parts);
|
|
679
688
|
if (Array.isArray(e.messages)) {
|
|
680
689
|
let t = null;
|
|
681
690
|
for (let n = e.messages.length - 1; n >= 0; --n) {
|
|
682
691
|
let r = e.messages[n];
|
|
683
692
|
if (r?.role !== "user") continue;
|
|
684
|
-
let i =
|
|
693
|
+
let i = Ke(r.parts);
|
|
685
694
|
if (i) {
|
|
686
695
|
if (X(i)) return i;
|
|
687
696
|
t ??= i;
|
|
@@ -693,40 +702,40 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
693
702
|
for (let e = r.length - 1; e >= 0; --e) {
|
|
694
703
|
let t = r[e];
|
|
695
704
|
if (t.role !== "user") continue;
|
|
696
|
-
let n =
|
|
705
|
+
let n = Ke(t.parts);
|
|
697
706
|
if (n) {
|
|
698
707
|
if (X(n)) return n;
|
|
699
708
|
i ??= n;
|
|
700
709
|
}
|
|
701
710
|
}
|
|
702
711
|
return i ?? "";
|
|
703
|
-
},
|
|
712
|
+
}, ht = (e) => {
|
|
704
713
|
let t = Array.isArray(e.messages) && e.messages.length > 0 ? e.messages.at(-1) : e.message;
|
|
705
714
|
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();
|
|
706
|
-
},
|
|
715
|
+
}, gt = (e) => {
|
|
707
716
|
let t = e.id;
|
|
708
717
|
return typeof t == "string" && t.trim() !== "" ? t : null;
|
|
709
|
-
},
|
|
718
|
+
}, _t = (e) => {
|
|
710
719
|
let t = e.selectedChatModel;
|
|
711
720
|
return typeof t == "string" && t.trim() !== "" ? t : null;
|
|
712
|
-
},
|
|
721
|
+
}, vt = (e) => {
|
|
713
722
|
let t = e.openaiReasoningEffort;
|
|
714
723
|
return typeof t == "string" && t.trim() !== "" ? t.trim() : null;
|
|
715
|
-
},
|
|
724
|
+
}, yt = (e) => {
|
|
716
725
|
if (!e || typeof e != "object") return !1;
|
|
717
726
|
let t = e.state, n = e.toolCallId;
|
|
718
727
|
return typeof n == "string" && n.trim() !== "" && (t === "approval-responded" || t === "output-denied");
|
|
719
|
-
},
|
|
728
|
+
}, bt = (e) => {
|
|
720
729
|
let t = Array.isArray(e.messages) ? e.messages : [];
|
|
721
|
-
return t.length === 0 || t.at(-1)?.role === "user" ? !1 : t.some((e) => (Array.isArray(e?.parts) ? e.parts : []).some((e) =>
|
|
722
|
-
},
|
|
730
|
+
return t.length === 0 || t.at(-1)?.role === "user" ? !1 : t.some((e) => (Array.isArray(e?.parts) ? e.parts : []).some((e) => yt(e)));
|
|
731
|
+
}, xt = (e) => {
|
|
723
732
|
let t = /* @__PURE__ */ new Map();
|
|
724
733
|
for (let n of e) {
|
|
725
734
|
let e = Array.isArray(n?.parts) ? n.parts : [];
|
|
726
|
-
for (let n of e)
|
|
735
|
+
for (let n of e) yt(n) && t.set(n.toolCallId, n);
|
|
727
736
|
}
|
|
728
737
|
return t;
|
|
729
|
-
},
|
|
738
|
+
}, St = (e, t) => {
|
|
730
739
|
let n = [];
|
|
731
740
|
return {
|
|
732
741
|
changedMessages: n,
|
|
@@ -749,21 +758,21 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
749
758
|
return n.push(a), a;
|
|
750
759
|
})
|
|
751
760
|
};
|
|
752
|
-
},
|
|
761
|
+
}, Ct = (e) => {
|
|
753
762
|
if (!e || typeof e != "object" || !("createdAt" in e)) return null;
|
|
754
763
|
let t = e.createdAt;
|
|
755
764
|
if (typeof t != "string") return null;
|
|
756
765
|
let n = new Date(t);
|
|
757
766
|
return Number.isNaN(n.getTime()) ? null : n;
|
|
758
|
-
},
|
|
767
|
+
}, wt = (e) => ({
|
|
759
768
|
latitude: null,
|
|
760
769
|
longitude: null,
|
|
761
770
|
city: null,
|
|
762
771
|
country: null
|
|
763
|
-
}),
|
|
772
|
+
}), Tt = (e) => {
|
|
764
773
|
let [t] = e.split("/");
|
|
765
774
|
return t && t !== e ? t : "unknown";
|
|
766
|
-
}, Q = (e) => typeof e == "number" && Number.isFinite(e) ? e : void 0,
|
|
775
|
+
}, Q = (e) => typeof e == "number" && Number.isFinite(e) ? e : void 0, Et = (e) => e instanceof Error && e.message.trim() ? e.message : "Unknown error", Dt = (e) => {
|
|
767
776
|
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 : {};
|
|
768
777
|
return {
|
|
769
778
|
inputTokens: Q(t.inputTokens),
|
|
@@ -773,13 +782,13 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
773
782
|
reasoningTokens: Q(r.reasoningTokens) ?? Q(t.reasoningTokens),
|
|
774
783
|
rawUsage: Object.keys(t).length > 0 ? t.raw ?? t : void 0
|
|
775
784
|
};
|
|
776
|
-
},
|
|
785
|
+
}, Ot = () => ({
|
|
777
786
|
inputTokens: 0,
|
|
778
787
|
outputTokens: 0,
|
|
779
788
|
totalTokens: 0,
|
|
780
789
|
cachedInputTokens: 0,
|
|
781
790
|
reasoningTokens: 0
|
|
782
|
-
}),
|
|
791
|
+
}), kt = async ({ ctx: e, chatId: t, agent: n, selectedChatModel: i, mode: a }) => {
|
|
783
792
|
try {
|
|
784
793
|
return { workspaceId: Z((await r().resolveUsageContext?.({
|
|
785
794
|
ctx: e,
|
|
@@ -792,19 +801,19 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
792
801
|
return console.warn("Failed to resolve agent usage context.", e), { workspaceId: void 0 };
|
|
793
802
|
}
|
|
794
803
|
}, $ = async ({ ctx: e, usageContext: t, agent: n, chatId: r, mode: i, feature: o, modelId: s, status: c, billable: l, usage: u, finishReason: d, error: f, startedAt: p, finishedAt: m = /* @__PURE__ */ new Date() }) => {
|
|
795
|
-
let h = u ?
|
|
796
|
-
id:
|
|
804
|
+
let h = u ? Dt(u) : {}, g = {
|
|
805
|
+
id: w(),
|
|
797
806
|
workspaceId: t.workspaceId,
|
|
798
807
|
agentId: n.id,
|
|
799
808
|
chatId: r,
|
|
800
809
|
mode: i,
|
|
801
810
|
feature: o,
|
|
802
|
-
provider:
|
|
811
|
+
provider: Tt(s),
|
|
803
812
|
model: s,
|
|
804
813
|
billable: l,
|
|
805
814
|
status: c,
|
|
806
815
|
finishReason: Z(d) ?? void 0,
|
|
807
|
-
errorMessage: f ?
|
|
816
|
+
errorMessage: f ? Et(f) : void 0,
|
|
808
817
|
...h,
|
|
809
818
|
startedAt: p,
|
|
810
819
|
finishedAt: m,
|
|
@@ -815,12 +824,12 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
815
824
|
} catch (e) {
|
|
816
825
|
console.error("Failed to save agents usage event.", e);
|
|
817
826
|
}
|
|
818
|
-
},
|
|
827
|
+
}, At = (e) => `agents-sdk:chat:${e}:stream`, jt = async ({ ctx: e, chatId: t, selectedChatModel: i }) => await r().resolveAgent?.({
|
|
819
828
|
ctx: e,
|
|
820
829
|
chatId: t,
|
|
821
830
|
selectedChatModel: i
|
|
822
|
-
}) ?? n(),
|
|
823
|
-
let a = i(e), o =
|
|
831
|
+
}) ?? n(), Mt = ({ ctx: e, chatId: t, mode: n, streamTransport: r }) => {
|
|
832
|
+
let a = i(e), o = At(t), s = v({
|
|
824
833
|
chatId: t,
|
|
825
834
|
abortWhenNoSubscribersMs: n === "headless" || r ? 0 : void 0
|
|
826
835
|
});
|
|
@@ -848,95 +857,95 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
848
857
|
});
|
|
849
858
|
}
|
|
850
859
|
};
|
|
851
|
-
},
|
|
860
|
+
}, Nt = ({ ctx: e, chatId: t, streamTransport: n, message: r }) => {
|
|
852
861
|
if (!n) return;
|
|
853
862
|
let a = i(e);
|
|
854
863
|
n.publish({
|
|
855
864
|
tenantId: a.tenantId,
|
|
856
865
|
userId: a.userId,
|
|
857
|
-
channel:
|
|
866
|
+
channel: At(t),
|
|
858
867
|
payload: {
|
|
859
868
|
chatId: t,
|
|
860
869
|
message: r
|
|
861
870
|
}
|
|
862
871
|
});
|
|
863
|
-
},
|
|
864
|
-
let f =
|
|
872
|
+
}, Pt = async ({ payload: n, ctx: a, mode: c = "interactive", streamTransport: d = null }) => {
|
|
873
|
+
let f = gt(n);
|
|
865
874
|
if (!f) return t(a, "bad_request:api", "Parameter id is required.");
|
|
866
|
-
let m = n,
|
|
867
|
-
if (!
|
|
868
|
-
if (
|
|
869
|
-
let
|
|
870
|
-
if (
|
|
871
|
-
if (!
|
|
872
|
-
if (!
|
|
873
|
-
if (!
|
|
874
|
-
if (!
|
|
875
|
-
let
|
|
875
|
+
let m = n, _ = _t(m), v = rt(process.env.PLAYWRIGHT), y = !rt(process.env.AGENT_SDK_LIVE_CHAT) && v || process.env.NODE_ENV === "test";
|
|
876
|
+
if (!_) return t(a, "bad_request:api", "Parameter selectedChatModel is required.");
|
|
877
|
+
if (_ === $e && (y = !0), _ !== $e && (!it().has(_) || !fe(_))) return t(a, "bad_request:api", `Unsupported selectedChatModel: "${_}".`);
|
|
878
|
+
let b = process.env.OPENAI_API_KEY, ie = process.env.GOOGLE_GENERATIVE_AI_API_KEY ?? process.env.GOOGLE_API_KEY, oe = process.env.XAI_API_KEY, x = F(_), ce = ue(_), S = de(_), C = vt(m), T = C && C !== "default" && C !== "off" ? C : null, le = e(_);
|
|
879
|
+
if (T && !le.some((e) => e === T)) return t(a, "bad_request:api", `Unsupported reasoning effort "${T}" for selectedChatModel: "${_}".`);
|
|
880
|
+
if (!y && x && !b) return t(a, "bad_request:provider", "Missing OPENAI_API_KEY.");
|
|
881
|
+
if (!y && ce && !ie) return t(a, "bad_request:provider", "Missing GOOGLE_GENERATIVE_AI_API_KEY (or GOOGLE_API_KEY).");
|
|
882
|
+
if (!y && S && !oe) return t(a, "bad_request:provider", "Missing XAI_API_KEY.");
|
|
883
|
+
if (!y && !x && !ce && !S) return t(a, "bad_request:provider", `Unsupported selectedChatModel: "${_}".`);
|
|
884
|
+
let E = await jt({
|
|
876
885
|
ctx: a,
|
|
877
886
|
chatId: f,
|
|
878
|
-
selectedChatModel:
|
|
879
|
-
}),
|
|
887
|
+
selectedChatModel: _
|
|
888
|
+
}), D = await r().resolveKnowledge?.({
|
|
880
889
|
ctx: a,
|
|
881
890
|
chatId: f,
|
|
882
|
-
agent:
|
|
883
|
-
selectedChatModel:
|
|
884
|
-
}) ?? null,
|
|
891
|
+
agent: E,
|
|
892
|
+
selectedChatModel: _
|
|
893
|
+
}) ?? null, O = await kt({
|
|
885
894
|
ctx: a,
|
|
886
895
|
chatId: f,
|
|
887
|
-
agent:
|
|
888
|
-
selectedChatModel:
|
|
896
|
+
agent: E,
|
|
897
|
+
selectedChatModel: _,
|
|
889
898
|
mode: c
|
|
890
|
-
}),
|
|
891
|
-
if (
|
|
892
|
-
|
|
899
|
+
}), k = i(a), A = await o(a, f, { includeDeleted: !0 });
|
|
900
|
+
if (A?.deletedAt) return t(a, "not_found:chat");
|
|
901
|
+
A ? A.selectedChatModel !== _ && await ee(a, {
|
|
893
902
|
chatId: f,
|
|
894
|
-
selectedChatModel:
|
|
903
|
+
selectedChatModel: _
|
|
895
904
|
}) : await p(a, {
|
|
896
905
|
id: f,
|
|
897
906
|
createdAt: /* @__PURE__ */ new Date(),
|
|
898
|
-
userId:
|
|
907
|
+
userId: k.userId,
|
|
899
908
|
title: "",
|
|
900
909
|
isStreaming: !1,
|
|
901
|
-
selectedChatModel:
|
|
910
|
+
selectedChatModel: _,
|
|
902
911
|
lastContext: null
|
|
903
912
|
});
|
|
904
|
-
let
|
|
905
|
-
if ((typeof
|
|
913
|
+
let j = m.message, M = typeof j?.id == "string" ? j.id : w(), N = Array.isArray(j?.parts) ? j?.parts : [];
|
|
914
|
+
if ((typeof j?.role == "string" ? j.role : null) === "user") {
|
|
906
915
|
let e = /* @__PURE__ */ new Date();
|
|
907
916
|
await l(a, [{
|
|
908
|
-
id:
|
|
917
|
+
id: M,
|
|
909
918
|
chatId: f,
|
|
910
919
|
role: "user",
|
|
911
|
-
parts:
|
|
920
|
+
parts: N,
|
|
912
921
|
attachments: [],
|
|
913
922
|
createdAt: e
|
|
914
|
-
}]),
|
|
923
|
+
}]), Nt({
|
|
915
924
|
ctx: a,
|
|
916
925
|
chatId: f,
|
|
917
926
|
streamTransport: d,
|
|
918
927
|
message: {
|
|
919
|
-
id:
|
|
928
|
+
id: M,
|
|
920
929
|
role: "user",
|
|
921
|
-
parts:
|
|
930
|
+
parts: N,
|
|
922
931
|
metadata: { createdAt: e.toISOString() }
|
|
923
932
|
}
|
|
924
933
|
});
|
|
925
934
|
}
|
|
926
|
-
if (
|
|
935
|
+
if (y) {
|
|
927
936
|
await s(a, {
|
|
928
937
|
chatId: f,
|
|
929
938
|
isStreaming: !0
|
|
930
939
|
});
|
|
931
|
-
let e =
|
|
940
|
+
let e = Mt({
|
|
932
941
|
ctx: a,
|
|
933
942
|
chatId: f,
|
|
934
943
|
mode: c,
|
|
935
944
|
streamTransport: d
|
|
936
945
|
});
|
|
937
946
|
try {
|
|
938
|
-
let t = X(await
|
|
939
|
-
n &&
|
|
947
|
+
let t = X(await mt(m, a, f)), n = await o(a, f);
|
|
948
|
+
n && Ge(n.title) && t && t !== n.title && (await g(a, {
|
|
940
949
|
chatId: f,
|
|
941
950
|
title: t
|
|
942
951
|
}), e.publishJson({
|
|
@@ -944,8 +953,8 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
944
953
|
data: t,
|
|
945
954
|
transient: !0
|
|
946
955
|
}));
|
|
947
|
-
let r =
|
|
948
|
-
|
|
956
|
+
let r = w();
|
|
957
|
+
v && !e.abortController.signal.aborted && await tt(600), e.publishJson({
|
|
949
958
|
type: "start",
|
|
950
959
|
messageId: r
|
|
951
960
|
});
|
|
@@ -954,18 +963,18 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
954
963
|
type: "text-start",
|
|
955
964
|
id: i
|
|
956
965
|
});
|
|
957
|
-
let s =
|
|
966
|
+
let s = ht(m), u = s ? `Echo: ${s}` : "Hello from agents-sdk", d = Date.now(), p = u.split(/(\s+)/).filter((e) => e.length > 0);
|
|
958
967
|
for (let t of p) {
|
|
959
968
|
if (e.abortController.signal.aborted) break;
|
|
960
969
|
e.publishJson({
|
|
961
970
|
type: "text-delta",
|
|
962
971
|
id: i,
|
|
963
972
|
delta: t
|
|
964
|
-
}), await
|
|
973
|
+
}), await tt(v ? 120 : 80);
|
|
965
974
|
}
|
|
966
|
-
if (
|
|
975
|
+
if (v && !e.abortController.signal.aborted) {
|
|
967
976
|
let e = Date.now() - d, t = 1200;
|
|
968
|
-
e < t && await
|
|
977
|
+
e < t && await tt(t - e);
|
|
969
978
|
}
|
|
970
979
|
return e.abortController.signal.aborted ? {} : (e.publishJson({
|
|
971
980
|
type: "text-end",
|
|
@@ -985,23 +994,23 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
985
994
|
createdAt: /* @__PURE__ */ new Date()
|
|
986
995
|
}]), await $({
|
|
987
996
|
ctx: a,
|
|
988
|
-
usageContext:
|
|
989
|
-
agent:
|
|
997
|
+
usageContext: O,
|
|
998
|
+
agent: E,
|
|
990
999
|
chatId: f,
|
|
991
1000
|
mode: c,
|
|
992
1001
|
feature: "chat",
|
|
993
|
-
modelId:
|
|
1002
|
+
modelId: _,
|
|
994
1003
|
status: "success",
|
|
995
1004
|
billable: !1,
|
|
996
|
-
usage:
|
|
1005
|
+
usage: Ot(),
|
|
997
1006
|
finishReason: "stop",
|
|
998
1007
|
startedAt: new Date(d)
|
|
999
1008
|
}), {});
|
|
1000
1009
|
} finally {
|
|
1001
|
-
await
|
|
1010
|
+
await nt(a, f);
|
|
1002
1011
|
}
|
|
1003
1012
|
}
|
|
1004
|
-
let
|
|
1013
|
+
let P = wt(a), L = bt(m);
|
|
1005
1014
|
if (Array.isArray(m.messages) && !L) {
|
|
1006
1015
|
let e = m.messages.flatMap((e) => {
|
|
1007
1016
|
let t = typeof e?.id == "string" ? e.id : null, n = typeof e?.role == "string" ? e.role : null, r = Array.isArray(e?.parts) ? e.parts : [];
|
|
@@ -1011,7 +1020,7 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
1011
1020
|
role: n,
|
|
1012
1021
|
parts: r,
|
|
1013
1022
|
attachments: [],
|
|
1014
|
-
createdAt:
|
|
1023
|
+
createdAt: Ct(e?.metadata) ?? /* @__PURE__ */ new Date()
|
|
1015
1024
|
}];
|
|
1016
1025
|
});
|
|
1017
1026
|
e.length > 0 && await l(a, e);
|
|
@@ -1021,7 +1030,7 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
1021
1030
|
let e = await u(a, f);
|
|
1022
1031
|
if (e.length === 0) R = m.messages;
|
|
1023
1032
|
else {
|
|
1024
|
-
let { changedMessages: t, updatedMessages: n } =
|
|
1033
|
+
let { changedMessages: t, updatedMessages: n } = St(e, xt(m.messages));
|
|
1025
1034
|
t.length > 0 && await l(a, t), R = n.map((e) => ({
|
|
1026
1035
|
id: e.id,
|
|
1027
1036
|
role: e.role,
|
|
@@ -1037,16 +1046,16 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
1037
1046
|
}));
|
|
1038
1047
|
let z = L ? {
|
|
1039
1048
|
messages: R,
|
|
1040
|
-
lastContext:
|
|
1049
|
+
lastContext: A?.lastContext ?? null,
|
|
1041
1050
|
lastContextChanged: !1
|
|
1042
|
-
} : await
|
|
1051
|
+
} : await Le({
|
|
1043
1052
|
ctx: a,
|
|
1044
1053
|
chatId: f,
|
|
1045
|
-
agent:
|
|
1046
|
-
selectedChatModel:
|
|
1054
|
+
agent: E,
|
|
1055
|
+
selectedChatModel: _,
|
|
1047
1056
|
mode: c,
|
|
1048
1057
|
messages: R,
|
|
1049
|
-
lastContext:
|
|
1058
|
+
lastContext: A?.lastContext ?? null
|
|
1050
1059
|
});
|
|
1051
1060
|
if (!L && z.lastContextChanged) try {
|
|
1052
1061
|
await h(a, {
|
|
@@ -1056,56 +1065,56 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
1056
1065
|
} catch (e) {
|
|
1057
1066
|
console.warn("Failed to save compacted agent chat history context.", e);
|
|
1058
1067
|
}
|
|
1059
|
-
let
|
|
1068
|
+
let he = z.messages;
|
|
1060
1069
|
await s(a, {
|
|
1061
1070
|
chatId: f,
|
|
1062
1071
|
isStreaming: !0
|
|
1063
1072
|
});
|
|
1064
|
-
let
|
|
1073
|
+
let B = Mt({
|
|
1065
1074
|
ctx: a,
|
|
1066
1075
|
chatId: f,
|
|
1067
1076
|
mode: c,
|
|
1068
1077
|
streamTransport: d
|
|
1069
|
-
}),
|
|
1078
|
+
}), V = "", H = "";
|
|
1070
1079
|
try {
|
|
1071
|
-
|
|
1080
|
+
V = await mt(m, a, f), H = X(V);
|
|
1072
1081
|
let e = await o(a, f);
|
|
1073
|
-
e &&
|
|
1082
|
+
e && Ge(e.title) && H && H !== e.title && (await g(a, {
|
|
1074
1083
|
chatId: f,
|
|
1075
|
-
title:
|
|
1076
|
-
}),
|
|
1084
|
+
title: H
|
|
1085
|
+
}), B.publishJson({
|
|
1077
1086
|
type: "data-chat-title",
|
|
1078
|
-
data:
|
|
1087
|
+
data: H,
|
|
1079
1088
|
transient: !0
|
|
1080
1089
|
}));
|
|
1081
1090
|
} catch (e) {
|
|
1082
|
-
console.error(e),
|
|
1091
|
+
console.error(e), B.publishJson({
|
|
1083
1092
|
type: "error",
|
|
1084
1093
|
errorText: "Something went wrong."
|
|
1085
|
-
}),
|
|
1094
|
+
}), B.publishDone();
|
|
1086
1095
|
try {
|
|
1087
|
-
await
|
|
1096
|
+
await nt(a, f);
|
|
1088
1097
|
} catch (e) {
|
|
1089
1098
|
console.error("Failed to update chat streaming state.", e);
|
|
1090
1099
|
}
|
|
1091
1100
|
return {};
|
|
1092
1101
|
}
|
|
1093
|
-
|
|
1102
|
+
V && H && b && (async () => {
|
|
1094
1103
|
let e = /* @__PURE__ */ new Date();
|
|
1095
1104
|
try {
|
|
1096
1105
|
let { text: t } = await re({
|
|
1097
1106
|
model: me(),
|
|
1098
|
-
system:
|
|
1099
|
-
prompt:
|
|
1107
|
+
system: ve,
|
|
1108
|
+
prompt: V,
|
|
1100
1109
|
onFinish: async (t) => {
|
|
1101
1110
|
await $({
|
|
1102
1111
|
ctx: a,
|
|
1103
|
-
usageContext:
|
|
1104
|
-
agent:
|
|
1112
|
+
usageContext: O,
|
|
1113
|
+
agent: E,
|
|
1105
1114
|
chatId: f,
|
|
1106
1115
|
mode: c,
|
|
1107
1116
|
feature: "title",
|
|
1108
|
-
modelId:
|
|
1117
|
+
modelId: et,
|
|
1109
1118
|
status: "success",
|
|
1110
1119
|
billable: !0,
|
|
1111
1120
|
usage: t.totalUsage ?? t.usage,
|
|
@@ -1113,14 +1122,14 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
1113
1122
|
startedAt: e
|
|
1114
1123
|
});
|
|
1115
1124
|
}
|
|
1116
|
-
}), n =
|
|
1125
|
+
}), n = qe(t);
|
|
1117
1126
|
if (!n) return;
|
|
1118
1127
|
let r = await o(a, f);
|
|
1119
1128
|
if (!r) return;
|
|
1120
1129
|
r.title !== n && (await g(a, {
|
|
1121
1130
|
chatId: f,
|
|
1122
1131
|
title: n
|
|
1123
|
-
}),
|
|
1132
|
+
}), B.publishJson({
|
|
1124
1133
|
type: "data-chat-title",
|
|
1125
1134
|
data: n,
|
|
1126
1135
|
transient: !0
|
|
@@ -1128,12 +1137,12 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
1128
1137
|
} catch (t) {
|
|
1129
1138
|
await $({
|
|
1130
1139
|
ctx: a,
|
|
1131
|
-
usageContext:
|
|
1132
|
-
agent:
|
|
1140
|
+
usageContext: O,
|
|
1141
|
+
agent: E,
|
|
1133
1142
|
chatId: f,
|
|
1134
1143
|
mode: c,
|
|
1135
1144
|
feature: "title",
|
|
1136
|
-
modelId:
|
|
1145
|
+
modelId: et,
|
|
1137
1146
|
status: "error",
|
|
1138
1147
|
billable: !1,
|
|
1139
1148
|
error: t,
|
|
@@ -1141,13 +1150,13 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
1141
1150
|
}), console.warn("Failed to generate chat title.", t);
|
|
1142
1151
|
}
|
|
1143
1152
|
})();
|
|
1144
|
-
let
|
|
1153
|
+
let _e = ne({
|
|
1145
1154
|
originalMessages: R,
|
|
1146
1155
|
onError: (e) => (console.error(e), "Something went wrong."),
|
|
1147
1156
|
onFinish: async ({ responseMessage: e }) => {
|
|
1148
1157
|
let t = Array.isArray(e.parts) ? e.parts : [];
|
|
1149
1158
|
await l(a, [{
|
|
1150
|
-
id: e.id ??
|
|
1159
|
+
id: e.id ?? w(),
|
|
1151
1160
|
chatId: f,
|
|
1152
1161
|
role: e.role ?? "assistant",
|
|
1153
1162
|
parts: t,
|
|
@@ -1156,10 +1165,10 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
1156
1165
|
}]);
|
|
1157
1166
|
},
|
|
1158
1167
|
execute: async ({ writer: e }) => {
|
|
1159
|
-
let t =
|
|
1168
|
+
let t = We({
|
|
1160
1169
|
ctx: a,
|
|
1161
1170
|
dataStream: e,
|
|
1162
|
-
requestHints:
|
|
1171
|
+
requestHints: P
|
|
1163
1172
|
}), n = [
|
|
1164
1173
|
"getWeather",
|
|
1165
1174
|
"createDocument",
|
|
@@ -1173,17 +1182,17 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
1173
1182
|
"editDocument",
|
|
1174
1183
|
"updateDocument",
|
|
1175
1184
|
"requestSuggestions"
|
|
1176
|
-
], o =
|
|
1185
|
+
], o = I(_), s = o ? [] : x ? n : i, l = t, u = s, d = await r().resolveTools?.({
|
|
1177
1186
|
ctx: a,
|
|
1178
1187
|
dataStream: e,
|
|
1179
|
-
requestHints:
|
|
1180
|
-
agent:
|
|
1181
|
-
selectedChatModel:
|
|
1188
|
+
requestHints: P,
|
|
1189
|
+
agent: E,
|
|
1190
|
+
selectedChatModel: _,
|
|
1182
1191
|
availableTools: t,
|
|
1183
1192
|
defaultActiveTools: s
|
|
1184
1193
|
});
|
|
1185
1194
|
d && (l = d.tools, u = d.activeTools ?? Object.keys(d.tools));
|
|
1186
|
-
let p = await
|
|
1195
|
+
let p = await te(he.map((e) => ({
|
|
1187
1196
|
role: e?.role,
|
|
1188
1197
|
parts: e?.parts,
|
|
1189
1198
|
metadata: e?.metadata
|
|
@@ -1191,105 +1200,105 @@ var Ye = 1024 * 1024 * 20, Xe = 1e5, Ze = /^\/api\/rb\/files\/([^/]+)\/?$/, Qe =
|
|
|
1191
1200
|
tools: l,
|
|
1192
1201
|
ignoreIncompleteToolCalls: !0
|
|
1193
1202
|
});
|
|
1194
|
-
await
|
|
1203
|
+
await ft(a, p), x && pt(p);
|
|
1195
1204
|
let h = typeof m.openaiReasoningSummary == "string" ? m.openaiReasoningSummary : null, g;
|
|
1196
|
-
|
|
1197
|
-
reasoningEffort:
|
|
1205
|
+
x && T ? g = { openai: {
|
|
1206
|
+
reasoningEffort: T,
|
|
1198
1207
|
reasoningSummary: h ?? void 0
|
|
1199
|
-
} } :
|
|
1200
|
-
let
|
|
1201
|
-
|
|
1208
|
+
} } : S && o && T && (g = { xai: { reasoningEffort: T } });
|
|
1209
|
+
let ee = !1, v = /* @__PURE__ */ new Date(), y = async ({ status: e, usage: t, finishReason: n, error: r }) => {
|
|
1210
|
+
ee || (ee = !0, await $({
|
|
1202
1211
|
ctx: a,
|
|
1203
|
-
usageContext:
|
|
1204
|
-
agent:
|
|
1212
|
+
usageContext: O,
|
|
1213
|
+
agent: E,
|
|
1205
1214
|
chatId: f,
|
|
1206
1215
|
mode: c,
|
|
1207
1216
|
feature: "chat",
|
|
1208
|
-
modelId:
|
|
1217
|
+
modelId: _,
|
|
1209
1218
|
status: e,
|
|
1210
1219
|
billable: e === "success",
|
|
1211
1220
|
usage: t,
|
|
1212
1221
|
finishReason: n,
|
|
1213
1222
|
error: r,
|
|
1214
|
-
startedAt:
|
|
1223
|
+
startedAt: v
|
|
1215
1224
|
}));
|
|
1216
|
-
},
|
|
1217
|
-
model: pe(
|
|
1218
|
-
system:
|
|
1219
|
-
selectedChatModel:
|
|
1220
|
-
requestHints:
|
|
1221
|
-
agentInstructions:
|
|
1222
|
-
knowledge:
|
|
1225
|
+
}, ne = se({
|
|
1226
|
+
model: pe(_),
|
|
1227
|
+
system: ge({
|
|
1228
|
+
selectedChatModel: _,
|
|
1229
|
+
requestHints: P,
|
|
1230
|
+
agentInstructions: E.instructions,
|
|
1231
|
+
knowledge: D
|
|
1223
1232
|
}),
|
|
1224
1233
|
messages: p,
|
|
1225
1234
|
tools: l,
|
|
1226
1235
|
stopWhen: ae(5),
|
|
1227
|
-
abortSignal:
|
|
1236
|
+
abortSignal: B.abortController.signal,
|
|
1228
1237
|
providerOptions: g,
|
|
1229
1238
|
activeTools: u,
|
|
1230
1239
|
onFinish: async (e) => {
|
|
1231
|
-
await
|
|
1240
|
+
await y({
|
|
1232
1241
|
status: "success",
|
|
1233
1242
|
usage: e.totalUsage ?? e.usage,
|
|
1234
1243
|
finishReason: e.finishReason
|
|
1235
1244
|
});
|
|
1236
1245
|
},
|
|
1237
1246
|
onError: async (e) => {
|
|
1238
|
-
await
|
|
1247
|
+
await y({
|
|
1239
1248
|
status: "error",
|
|
1240
1249
|
error: e?.error ?? e
|
|
1241
1250
|
});
|
|
1242
1251
|
},
|
|
1243
1252
|
onAbort: async (e) => {
|
|
1244
|
-
await
|
|
1253
|
+
await y({
|
|
1245
1254
|
status: "aborted",
|
|
1246
1255
|
usage: e?.totalUsage ?? e?.usage,
|
|
1247
1256
|
finishReason: e?.finishReason
|
|
1248
1257
|
});
|
|
1249
1258
|
}
|
|
1250
1259
|
});
|
|
1251
|
-
e.merge(
|
|
1260
|
+
e.merge(ne.toUIMessageStream({
|
|
1252
1261
|
originalMessages: R,
|
|
1253
|
-
sendReasoning: o || !!(
|
|
1262
|
+
sendReasoning: o || !!(x && T)
|
|
1254
1263
|
}));
|
|
1255
1264
|
}
|
|
1256
1265
|
});
|
|
1257
1266
|
return (async () => {
|
|
1258
1267
|
let e = null;
|
|
1259
1268
|
try {
|
|
1260
|
-
let t =
|
|
1269
|
+
let t = _e.getReader();
|
|
1261
1270
|
for (e = () => t.releaseLock();;) {
|
|
1262
1271
|
let { value: e, done: n } = await t.read();
|
|
1263
1272
|
if (n) break;
|
|
1264
|
-
|
|
1273
|
+
B.publishJson(e);
|
|
1265
1274
|
}
|
|
1266
|
-
|
|
1275
|
+
B.publishDone();
|
|
1267
1276
|
} catch (e) {
|
|
1268
|
-
console.error(e),
|
|
1277
|
+
console.error(e), B.publishJson({
|
|
1269
1278
|
type: "error",
|
|
1270
1279
|
errorText: "Something went wrong."
|
|
1271
|
-
}),
|
|
1280
|
+
}), B.publishDone();
|
|
1272
1281
|
} finally {
|
|
1273
1282
|
e?.();
|
|
1274
1283
|
try {
|
|
1275
|
-
await
|
|
1284
|
+
await nt(a, f);
|
|
1276
1285
|
} catch (e) {
|
|
1277
1286
|
console.error("Failed to update chat streaming state.", e);
|
|
1278
1287
|
}
|
|
1279
1288
|
}
|
|
1280
1289
|
})(), {};
|
|
1281
|
-
},
|
|
1290
|
+
}, Ft = async (e, t) => Pt({
|
|
1282
1291
|
payload: e,
|
|
1283
1292
|
ctx: t,
|
|
1284
1293
|
mode: "interactive"
|
|
1285
|
-
}),
|
|
1294
|
+
}), It = async (e, n) => {
|
|
1286
1295
|
let r = n.req.query.id, i = Array.isArray(r) ? r[0] : r;
|
|
1287
1296
|
return typeof i != "string" || i.trim() === "" ? t(n, "bad_request:api") : f(n, i);
|
|
1288
|
-
},
|
|
1289
|
-
e.get(
|
|
1297
|
+
}, Lt = (e) => {
|
|
1298
|
+
e.get(Ye, (async (e, n) => {
|
|
1290
1299
|
let r = n.req.query.id, i = Array.isArray(r) ? r[0] : r;
|
|
1291
1300
|
return typeof i != "string" || i.trim() === "" ? t(n, "bad_request:api", "Parameter id is required.") : await o(n, i) || t(n, "not_found:chat");
|
|
1292
|
-
})), e.post(
|
|
1301
|
+
})), e.post(Ye, Ft), e.delete(Ye, It);
|
|
1293
1302
|
};
|
|
1294
1303
|
//#endregion
|
|
1295
|
-
export {
|
|
1304
|
+
export { Lt as default, Pt as runAgentLoop };
|