@deepagents/context 0.19.0 → 0.22.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/README.md +33 -0
- package/dist/browser.d.ts +22 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +1701 -0
- package/dist/browser.js.map +7 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +335 -154
- package/dist/index.js.map +4 -4
- package/dist/lib/agent.d.ts +3 -2
- package/dist/lib/agent.d.ts.map +1 -1
- package/dist/lib/chat.d.ts +20 -0
- package/dist/lib/chat.d.ts.map +1 -0
- package/dist/lib/fragments/message/user.d.ts +62 -0
- package/dist/lib/fragments/message/user.d.ts.map +1 -0
- package/dist/lib/fragments.d.ts +0 -41
- package/dist/lib/fragments.d.ts.map +1 -1
- package/package.json +7 -2
package/dist/index.js
CHANGED
|
@@ -29,113 +29,6 @@ function fragment(name, ...children) {
|
|
|
29
29
|
data: children
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
|
-
var SYSTEM_REMINDER_OPEN_TAG = "<system-reminder>";
|
|
33
|
-
var SYSTEM_REMINDER_CLOSE_TAG = "</system-reminder>";
|
|
34
|
-
function isRecord(value) {
|
|
35
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
36
|
-
}
|
|
37
|
-
function assertReminderText(text) {
|
|
38
|
-
if (text.trim().length === 0) {
|
|
39
|
-
throw new Error("Reminder text must not be empty");
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
function formatTaggedReminder(text) {
|
|
43
|
-
return `${SYSTEM_REMINDER_OPEN_TAG}${text}${SYSTEM_REMINDER_CLOSE_TAG}`;
|
|
44
|
-
}
|
|
45
|
-
function findLastTextPartIndex(message2) {
|
|
46
|
-
for (let i = message2.parts.length - 1; i >= 0; i--) {
|
|
47
|
-
if (message2.parts[i].type === "text") {
|
|
48
|
-
return i;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return void 0;
|
|
52
|
-
}
|
|
53
|
-
function ensureTextPart(message2) {
|
|
54
|
-
const existingIndex = findLastTextPartIndex(message2);
|
|
55
|
-
if (existingIndex !== void 0) {
|
|
56
|
-
return existingIndex;
|
|
57
|
-
}
|
|
58
|
-
const reminderPart = {
|
|
59
|
-
type: "text",
|
|
60
|
-
text: ""
|
|
61
|
-
};
|
|
62
|
-
message2.parts.push(reminderPart);
|
|
63
|
-
return message2.parts.length - 1;
|
|
64
|
-
}
|
|
65
|
-
function applyInlineReminder(message2, value) {
|
|
66
|
-
const partIndex = ensureTextPart(message2);
|
|
67
|
-
const textPart = message2.parts[partIndex];
|
|
68
|
-
if (textPart.type !== "text") {
|
|
69
|
-
throw new Error("Failed to resolve text part for inline reminder");
|
|
70
|
-
}
|
|
71
|
-
const reminderText = formatTaggedReminder(value);
|
|
72
|
-
const start = textPart.text.length;
|
|
73
|
-
const updatedText = `${textPart.text}${reminderText}`;
|
|
74
|
-
message2.parts[partIndex] = { ...textPart, text: updatedText };
|
|
75
|
-
return {
|
|
76
|
-
id: generateId(),
|
|
77
|
-
text: value,
|
|
78
|
-
partIndex,
|
|
79
|
-
start,
|
|
80
|
-
end: start + reminderText.length,
|
|
81
|
-
mode: "inline"
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
function applyPartReminder(message2, value) {
|
|
85
|
-
const part = { type: "text", text: value };
|
|
86
|
-
message2.parts.push(part);
|
|
87
|
-
const partIndex = message2.parts.length - 1;
|
|
88
|
-
return {
|
|
89
|
-
id: generateId(),
|
|
90
|
-
text: value,
|
|
91
|
-
partIndex,
|
|
92
|
-
start: 0,
|
|
93
|
-
end: value.length,
|
|
94
|
-
mode: "part"
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
function reminder(text, options) {
|
|
98
|
-
assertReminderText(text);
|
|
99
|
-
return {
|
|
100
|
-
text,
|
|
101
|
-
asPart: options?.asPart ?? false
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
function user(content, ...reminders) {
|
|
105
|
-
const message2 = typeof content === "string" ? {
|
|
106
|
-
id: generateId(),
|
|
107
|
-
role: "user",
|
|
108
|
-
parts: [{ type: "text", text: content }]
|
|
109
|
-
} : { ...content, role: "user", parts: [...content.parts] };
|
|
110
|
-
if (reminders.length > 0) {
|
|
111
|
-
const addedReminders = [];
|
|
112
|
-
for (const item of reminders) {
|
|
113
|
-
assertReminderText(item.text);
|
|
114
|
-
addedReminders.push(
|
|
115
|
-
item.asPart ? applyPartReminder(message2, item.text) : applyInlineReminder(message2, item.text)
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
const metadata = isRecord(message2.metadata) ? { ...message2.metadata } : {};
|
|
119
|
-
const existingReminders = Array.isArray(metadata.reminders) ? metadata.reminders : [];
|
|
120
|
-
metadata.reminders = [...existingReminders, ...addedReminders];
|
|
121
|
-
message2.metadata = metadata;
|
|
122
|
-
}
|
|
123
|
-
return {
|
|
124
|
-
id: message2.id,
|
|
125
|
-
name: "user",
|
|
126
|
-
data: "content",
|
|
127
|
-
type: "message",
|
|
128
|
-
persist: true,
|
|
129
|
-
codec: {
|
|
130
|
-
decode() {
|
|
131
|
-
return message2;
|
|
132
|
-
},
|
|
133
|
-
encode() {
|
|
134
|
-
return message2;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
32
|
function assistant(message2) {
|
|
140
33
|
return {
|
|
141
34
|
id: message2.id,
|
|
@@ -227,9 +120,11 @@ var Agent = class _Agent {
|
|
|
227
120
|
#options;
|
|
228
121
|
#guardrails = [];
|
|
229
122
|
tools;
|
|
123
|
+
context;
|
|
230
124
|
constructor(options) {
|
|
231
125
|
this.#options = options;
|
|
232
126
|
this.tools = options.tools || {};
|
|
127
|
+
this.context = options.context;
|
|
233
128
|
this.#guardrails = options.guardrails || [];
|
|
234
129
|
}
|
|
235
130
|
async generate(contextVariables, config) {
|
|
@@ -297,17 +192,25 @@ var Agent = class _Agent {
|
|
|
297
192
|
* Create a raw stream without guardrail processing.
|
|
298
193
|
*/
|
|
299
194
|
async #createRawStream(contextVariables, config) {
|
|
300
|
-
const
|
|
195
|
+
const context = this.#options.context;
|
|
196
|
+
if (!context) {
|
|
197
|
+
throw new Error(`Agent ${this.#options.name} is missing a context.`);
|
|
198
|
+
}
|
|
199
|
+
const model = this.#options.model;
|
|
200
|
+
if (!model) {
|
|
201
|
+
throw new Error(`Agent ${this.#options.name} is missing a model.`);
|
|
202
|
+
}
|
|
203
|
+
const { messages, systemPrompt } = await context.resolve({
|
|
301
204
|
renderer: new XmlRenderer()
|
|
302
205
|
});
|
|
303
206
|
const runId = generateId2();
|
|
304
207
|
return streamText({
|
|
305
208
|
abortSignal: config?.abortSignal,
|
|
306
209
|
providerOptions: this.#options.providerOptions,
|
|
307
|
-
model
|
|
210
|
+
model,
|
|
308
211
|
system: systemPrompt,
|
|
309
212
|
messages: await convertToModelMessages(messages),
|
|
310
|
-
experimental_repairToolCall: createRepairToolCall(
|
|
213
|
+
experimental_repairToolCall: createRepairToolCall(model),
|
|
311
214
|
stopWhen: stepCountIs(50),
|
|
312
215
|
experimental_transform: config?.transform ?? smoothStream(),
|
|
313
216
|
tools: this.#options.tools,
|
|
@@ -334,6 +237,9 @@ var Agent = class _Agent {
|
|
|
334
237
|
#wrapWithGuardrails(result, contextVariables, config) {
|
|
335
238
|
const maxRetries = config?.maxRetries ?? this.#options.maxGuardrailRetries ?? 3;
|
|
336
239
|
const context = this.#options.context;
|
|
240
|
+
if (!context) {
|
|
241
|
+
throw new Error(`Agent ${this.#options.name} is missing a context.`);
|
|
242
|
+
}
|
|
337
243
|
const originalToUIMessageStream = result.toUIMessageStream.bind(result);
|
|
338
244
|
result.toUIMessageStream = (options) => {
|
|
339
245
|
return createUIMessageStream({
|
|
@@ -497,6 +403,101 @@ function writeText(writer, text) {
|
|
|
497
403
|
});
|
|
498
404
|
}
|
|
499
405
|
|
|
406
|
+
// packages/context/src/lib/chat.ts
|
|
407
|
+
import {
|
|
408
|
+
APICallError,
|
|
409
|
+
InvalidToolInputError,
|
|
410
|
+
NoSuchToolError,
|
|
411
|
+
ToolCallRepairError,
|
|
412
|
+
createUIMessageStream as createUIMessageStream2,
|
|
413
|
+
generateId as generateId3
|
|
414
|
+
} from "ai";
|
|
415
|
+
async function chat(agent2, messages, options) {
|
|
416
|
+
const context = agent2.context;
|
|
417
|
+
if (!context) {
|
|
418
|
+
throw new Error(
|
|
419
|
+
"Agent is missing a context. Provide context when creating the agent."
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
if (messages.length === 0) {
|
|
423
|
+
throw new Error("messages must not be empty");
|
|
424
|
+
}
|
|
425
|
+
const lastMessage = messages[messages.length - 1];
|
|
426
|
+
let assistantMsgId;
|
|
427
|
+
if (lastMessage.role === "assistant") {
|
|
428
|
+
context.set(message(lastMessage));
|
|
429
|
+
await context.save({ branch: false });
|
|
430
|
+
assistantMsgId = lastMessage.id;
|
|
431
|
+
} else {
|
|
432
|
+
context.set(message(lastMessage));
|
|
433
|
+
await context.save();
|
|
434
|
+
assistantMsgId = options?.generateMessageId?.() ?? generateId3();
|
|
435
|
+
}
|
|
436
|
+
const streamContextVariables = options?.contextVariables === void 0 ? {} : options.contextVariables;
|
|
437
|
+
const result = await agent2.stream(streamContextVariables, {
|
|
438
|
+
transform: options?.transform
|
|
439
|
+
});
|
|
440
|
+
const uiStream = result.toUIMessageStream({
|
|
441
|
+
onError: options?.onError ?? formatChatError,
|
|
442
|
+
sendStart: true,
|
|
443
|
+
sendFinish: true,
|
|
444
|
+
sendReasoning: true,
|
|
445
|
+
sendSources: true,
|
|
446
|
+
originalMessages: messages,
|
|
447
|
+
generateMessageId: () => assistantMsgId,
|
|
448
|
+
messageMetadata: options?.messageMetadata
|
|
449
|
+
});
|
|
450
|
+
return createUIMessageStream2({
|
|
451
|
+
originalMessages: messages,
|
|
452
|
+
generateId: () => assistantMsgId,
|
|
453
|
+
onStepFinish: async ({ responseMessage }) => {
|
|
454
|
+
const normalizedMessage = {
|
|
455
|
+
...responseMessage,
|
|
456
|
+
id: assistantMsgId
|
|
457
|
+
};
|
|
458
|
+
context.set(assistant(normalizedMessage));
|
|
459
|
+
await context.save({ branch: false });
|
|
460
|
+
},
|
|
461
|
+
onFinish: async ({ responseMessage }) => {
|
|
462
|
+
const normalizedMessage = {
|
|
463
|
+
...responseMessage,
|
|
464
|
+
id: assistantMsgId
|
|
465
|
+
};
|
|
466
|
+
const finalMetadata = await options?.finalAssistantMetadata?.(normalizedMessage);
|
|
467
|
+
const finalMessage = finalMetadata === void 0 ? normalizedMessage : {
|
|
468
|
+
...normalizedMessage,
|
|
469
|
+
metadata: {
|
|
470
|
+
...normalizedMessage.metadata ?? {},
|
|
471
|
+
...finalMetadata
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
context.set(assistant(finalMessage));
|
|
475
|
+
await context.save({ branch: false });
|
|
476
|
+
const totalUsage = await result.totalUsage;
|
|
477
|
+
await context.trackUsage(totalUsage);
|
|
478
|
+
},
|
|
479
|
+
execute: async ({ writer }) => {
|
|
480
|
+
writer.merge(uiStream);
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
function formatChatError(error) {
|
|
485
|
+
if (NoSuchToolError.isInstance(error)) {
|
|
486
|
+
return "The model tried to call an unknown tool.";
|
|
487
|
+
}
|
|
488
|
+
if (InvalidToolInputError.isInstance(error)) {
|
|
489
|
+
return "The model called a tool with invalid arguments.";
|
|
490
|
+
}
|
|
491
|
+
if (ToolCallRepairError.isInstance(error)) {
|
|
492
|
+
return "The model tried to call a tool with invalid arguments, but it was repaired.";
|
|
493
|
+
}
|
|
494
|
+
if (APICallError.isInstance(error)) {
|
|
495
|
+
console.error("Upstream API call failed:", error);
|
|
496
|
+
return `Upstream API call failed with status ${error.statusCode}: ${error.message}`;
|
|
497
|
+
}
|
|
498
|
+
return JSON.stringify(error);
|
|
499
|
+
}
|
|
500
|
+
|
|
500
501
|
// packages/context/src/lib/engine.ts
|
|
501
502
|
import { mergeWith } from "lodash-es";
|
|
502
503
|
|
|
@@ -2362,6 +2363,182 @@ function policy(input) {
|
|
|
2362
2363
|
};
|
|
2363
2364
|
}
|
|
2364
2365
|
|
|
2366
|
+
// packages/context/src/lib/fragments/message/user.ts
|
|
2367
|
+
import { generateId as generateId4 } from "ai";
|
|
2368
|
+
var SYSTEM_REMINDER_OPEN_TAG = "<system-reminder>";
|
|
2369
|
+
var SYSTEM_REMINDER_CLOSE_TAG = "</system-reminder>";
|
|
2370
|
+
function getReminderRanges(metadata) {
|
|
2371
|
+
return metadata?.reminders ?? [];
|
|
2372
|
+
}
|
|
2373
|
+
function stripTextByRanges(text, ranges) {
|
|
2374
|
+
if (ranges.length === 0) {
|
|
2375
|
+
return text;
|
|
2376
|
+
}
|
|
2377
|
+
const normalized = ranges.map((range) => ({
|
|
2378
|
+
start: Math.max(0, Math.min(text.length, range.start)),
|
|
2379
|
+
end: Math.max(0, Math.min(text.length, range.end))
|
|
2380
|
+
})).filter((range) => range.end > range.start).sort((a, b) => a.start - b.start);
|
|
2381
|
+
if (normalized.length === 0) {
|
|
2382
|
+
return text;
|
|
2383
|
+
}
|
|
2384
|
+
let cursor = 0;
|
|
2385
|
+
let output = "";
|
|
2386
|
+
for (const range of normalized) {
|
|
2387
|
+
if (range.start < cursor) {
|
|
2388
|
+
if (range.end > cursor) {
|
|
2389
|
+
cursor = range.end;
|
|
2390
|
+
}
|
|
2391
|
+
continue;
|
|
2392
|
+
}
|
|
2393
|
+
output += text.slice(cursor, range.start);
|
|
2394
|
+
cursor = range.end;
|
|
2395
|
+
}
|
|
2396
|
+
output += text.slice(cursor);
|
|
2397
|
+
return output.trimEnd();
|
|
2398
|
+
}
|
|
2399
|
+
function stripReminders(message2) {
|
|
2400
|
+
const reminderRanges = getReminderRanges(
|
|
2401
|
+
isRecord(message2.metadata) ? message2.metadata : void 0
|
|
2402
|
+
);
|
|
2403
|
+
const rangesByPartIndex = /* @__PURE__ */ new Map();
|
|
2404
|
+
for (const range of reminderRanges) {
|
|
2405
|
+
const partRanges = rangesByPartIndex.get(range.partIndex) ?? [];
|
|
2406
|
+
partRanges.push({ start: range.start, end: range.end });
|
|
2407
|
+
rangesByPartIndex.set(range.partIndex, partRanges);
|
|
2408
|
+
}
|
|
2409
|
+
const strippedParts = message2.parts.flatMap((part, partIndex) => {
|
|
2410
|
+
const clonedPart = { ...part };
|
|
2411
|
+
const ranges = rangesByPartIndex.get(partIndex);
|
|
2412
|
+
if (clonedPart.type !== "text" || ranges === void 0) {
|
|
2413
|
+
return [clonedPart];
|
|
2414
|
+
}
|
|
2415
|
+
const strippedText = stripTextByRanges(clonedPart.text, ranges);
|
|
2416
|
+
if (strippedText.length === 0) {
|
|
2417
|
+
return [];
|
|
2418
|
+
}
|
|
2419
|
+
return [{ ...clonedPart, text: strippedText }];
|
|
2420
|
+
});
|
|
2421
|
+
const nextMessage = {
|
|
2422
|
+
...message2,
|
|
2423
|
+
parts: strippedParts
|
|
2424
|
+
};
|
|
2425
|
+
if (isRecord(message2.metadata)) {
|
|
2426
|
+
const metadata = { ...message2.metadata };
|
|
2427
|
+
delete metadata.reminders;
|
|
2428
|
+
if (Object.keys(metadata).length > 0) {
|
|
2429
|
+
nextMessage.metadata = metadata;
|
|
2430
|
+
} else {
|
|
2431
|
+
delete nextMessage.metadata;
|
|
2432
|
+
}
|
|
2433
|
+
}
|
|
2434
|
+
return nextMessage;
|
|
2435
|
+
}
|
|
2436
|
+
function isRecord(value) {
|
|
2437
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2438
|
+
}
|
|
2439
|
+
function assertReminderText(text) {
|
|
2440
|
+
if (text.trim().length === 0) {
|
|
2441
|
+
throw new Error("Reminder text must not be empty");
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2444
|
+
function formatTaggedReminder(text) {
|
|
2445
|
+
return `${SYSTEM_REMINDER_OPEN_TAG}${text}${SYSTEM_REMINDER_CLOSE_TAG}`;
|
|
2446
|
+
}
|
|
2447
|
+
function findLastTextPartIndex(message2) {
|
|
2448
|
+
for (let i = message2.parts.length - 1; i >= 0; i--) {
|
|
2449
|
+
if (message2.parts[i].type === "text") {
|
|
2450
|
+
return i;
|
|
2451
|
+
}
|
|
2452
|
+
}
|
|
2453
|
+
return void 0;
|
|
2454
|
+
}
|
|
2455
|
+
function ensureTextPart(message2) {
|
|
2456
|
+
const existingIndex = findLastTextPartIndex(message2);
|
|
2457
|
+
if (existingIndex !== void 0) {
|
|
2458
|
+
return existingIndex;
|
|
2459
|
+
}
|
|
2460
|
+
const reminderPart = {
|
|
2461
|
+
type: "text",
|
|
2462
|
+
text: ""
|
|
2463
|
+
};
|
|
2464
|
+
message2.parts.push(reminderPart);
|
|
2465
|
+
return message2.parts.length - 1;
|
|
2466
|
+
}
|
|
2467
|
+
function applyInlineReminder(message2, value) {
|
|
2468
|
+
const partIndex = ensureTextPart(message2);
|
|
2469
|
+
const textPart = message2.parts[partIndex];
|
|
2470
|
+
if (textPart.type !== "text") {
|
|
2471
|
+
throw new Error("Failed to resolve text part for inline reminder");
|
|
2472
|
+
}
|
|
2473
|
+
const reminderText = formatTaggedReminder(value);
|
|
2474
|
+
const start = textPart.text.length;
|
|
2475
|
+
const updatedText = `${textPart.text}${reminderText}`;
|
|
2476
|
+
message2.parts[partIndex] = { ...textPart, text: updatedText };
|
|
2477
|
+
return {
|
|
2478
|
+
id: generateId4(),
|
|
2479
|
+
text: value,
|
|
2480
|
+
partIndex,
|
|
2481
|
+
start,
|
|
2482
|
+
end: start + reminderText.length,
|
|
2483
|
+
mode: "inline"
|
|
2484
|
+
};
|
|
2485
|
+
}
|
|
2486
|
+
function applyPartReminder(message2, value) {
|
|
2487
|
+
const part = { type: "text", text: value };
|
|
2488
|
+
message2.parts.push(part);
|
|
2489
|
+
const partIndex = message2.parts.length - 1;
|
|
2490
|
+
return {
|
|
2491
|
+
id: generateId4(),
|
|
2492
|
+
text: value,
|
|
2493
|
+
partIndex,
|
|
2494
|
+
start: 0,
|
|
2495
|
+
end: value.length,
|
|
2496
|
+
mode: "part"
|
|
2497
|
+
};
|
|
2498
|
+
}
|
|
2499
|
+
function reminder(text, options) {
|
|
2500
|
+
assertReminderText(text);
|
|
2501
|
+
return {
|
|
2502
|
+
text,
|
|
2503
|
+
asPart: options?.asPart ?? false
|
|
2504
|
+
};
|
|
2505
|
+
}
|
|
2506
|
+
function user(content, ...reminders) {
|
|
2507
|
+
const message2 = typeof content === "string" ? {
|
|
2508
|
+
id: generateId4(),
|
|
2509
|
+
role: "user",
|
|
2510
|
+
parts: [{ type: "text", text: content }]
|
|
2511
|
+
} : { ...content, role: "user", parts: [...content.parts] };
|
|
2512
|
+
if (reminders.length > 0) {
|
|
2513
|
+
const addedReminders = [];
|
|
2514
|
+
for (const item of reminders) {
|
|
2515
|
+
assertReminderText(item.text);
|
|
2516
|
+
addedReminders.push(
|
|
2517
|
+
item.asPart ? applyPartReminder(message2, item.text) : applyInlineReminder(message2, item.text)
|
|
2518
|
+
);
|
|
2519
|
+
}
|
|
2520
|
+
const metadata = isRecord(message2.metadata) ? { ...message2.metadata } : {};
|
|
2521
|
+
const existingReminders = Array.isArray(metadata.reminders) ? metadata.reminders : [];
|
|
2522
|
+
metadata.reminders = [...existingReminders, ...addedReminders];
|
|
2523
|
+
message2.metadata = metadata;
|
|
2524
|
+
}
|
|
2525
|
+
return {
|
|
2526
|
+
id: message2.id,
|
|
2527
|
+
name: "user",
|
|
2528
|
+
data: "content",
|
|
2529
|
+
type: "message",
|
|
2530
|
+
persist: true,
|
|
2531
|
+
codec: {
|
|
2532
|
+
decode() {
|
|
2533
|
+
return message2;
|
|
2534
|
+
},
|
|
2535
|
+
encode() {
|
|
2536
|
+
return message2;
|
|
2537
|
+
}
|
|
2538
|
+
}
|
|
2539
|
+
};
|
|
2540
|
+
}
|
|
2541
|
+
|
|
2365
2542
|
// packages/context/src/lib/fragments/user.ts
|
|
2366
2543
|
function identity(input) {
|
|
2367
2544
|
return {
|
|
@@ -3487,22 +3664,22 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
3487
3664
|
// ==========================================================================
|
|
3488
3665
|
// Chat Operations
|
|
3489
3666
|
// ==========================================================================
|
|
3490
|
-
async createChat(
|
|
3667
|
+
async createChat(chat2) {
|
|
3491
3668
|
return this.#useTransaction(() => {
|
|
3492
3669
|
const row = this.#db.prepare(
|
|
3493
3670
|
`INSERT INTO chats (id, userId, title, metadata)
|
|
3494
3671
|
VALUES (?, ?, ?, ?)
|
|
3495
3672
|
RETURNING *`
|
|
3496
3673
|
).get(
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
|
|
3674
|
+
chat2.id,
|
|
3675
|
+
chat2.userId,
|
|
3676
|
+
chat2.title ?? null,
|
|
3677
|
+
chat2.metadata ? JSON.stringify(chat2.metadata) : null
|
|
3501
3678
|
);
|
|
3502
3679
|
this.#db.prepare(
|
|
3503
3680
|
`INSERT INTO branches (id, chatId, name, headMessageId, isActive, createdAt)
|
|
3504
3681
|
VALUES (?, ?, 'main', NULL, 1, ?)`
|
|
3505
|
-
).run(crypto.randomUUID(),
|
|
3682
|
+
).run(crypto.randomUUID(), chat2.id, Date.now());
|
|
3506
3683
|
return {
|
|
3507
3684
|
id: row.id,
|
|
3508
3685
|
userId: row.userId,
|
|
@@ -3513,7 +3690,7 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
3513
3690
|
};
|
|
3514
3691
|
});
|
|
3515
3692
|
}
|
|
3516
|
-
async upsertChat(
|
|
3693
|
+
async upsertChat(chat2) {
|
|
3517
3694
|
return this.#useTransaction(() => {
|
|
3518
3695
|
const row = this.#db.prepare(
|
|
3519
3696
|
`INSERT INTO chats (id, userId, title, metadata)
|
|
@@ -3521,15 +3698,15 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
3521
3698
|
ON CONFLICT(id) DO UPDATE SET id = excluded.id
|
|
3522
3699
|
RETURNING *`
|
|
3523
3700
|
).get(
|
|
3524
|
-
|
|
3525
|
-
|
|
3526
|
-
|
|
3527
|
-
|
|
3701
|
+
chat2.id,
|
|
3702
|
+
chat2.userId,
|
|
3703
|
+
chat2.title ?? null,
|
|
3704
|
+
chat2.metadata ? JSON.stringify(chat2.metadata) : null
|
|
3528
3705
|
);
|
|
3529
3706
|
this.#db.prepare(
|
|
3530
3707
|
`INSERT OR IGNORE INTO branches (id, chatId, name, headMessageId, isActive, createdAt)
|
|
3531
3708
|
VALUES (?, ?, 'main', NULL, 1, ?)`
|
|
3532
|
-
).run(crypto.randomUUID(),
|
|
3709
|
+
).run(crypto.randomUUID(), chat2.id, Date.now());
|
|
3533
3710
|
return {
|
|
3534
3711
|
id: row.id,
|
|
3535
3712
|
userId: row.userId,
|
|
@@ -3725,8 +3902,8 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
3725
3902
|
return row.hasChildren === 1;
|
|
3726
3903
|
}
|
|
3727
3904
|
async getMessages(chatId) {
|
|
3728
|
-
const
|
|
3729
|
-
if (!
|
|
3905
|
+
const chat2 = await this.getChat(chatId);
|
|
3906
|
+
if (!chat2) {
|
|
3730
3907
|
throw new Error(`Chat "${chatId}" not found`);
|
|
3731
3908
|
}
|
|
3732
3909
|
const activeBranch = await this.getActiveBranch(chatId);
|
|
@@ -4150,24 +4327,24 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
4150
4327
|
// ==========================================================================
|
|
4151
4328
|
// Chat Operations
|
|
4152
4329
|
// ==========================================================================
|
|
4153
|
-
async createChat(
|
|
4330
|
+
async createChat(chat2) {
|
|
4154
4331
|
return this.#useTransaction(async (client) => {
|
|
4155
4332
|
const result = await client.query(
|
|
4156
4333
|
`INSERT INTO ${this.#t("chats")} (id, userId, title, metadata)
|
|
4157
4334
|
VALUES ($1, $2, $3, $4)
|
|
4158
4335
|
RETURNING *`,
|
|
4159
4336
|
[
|
|
4160
|
-
|
|
4161
|
-
|
|
4162
|
-
|
|
4163
|
-
|
|
4337
|
+
chat2.id,
|
|
4338
|
+
chat2.userId,
|
|
4339
|
+
chat2.title ?? null,
|
|
4340
|
+
chat2.metadata ? JSON.stringify(chat2.metadata) : null
|
|
4164
4341
|
]
|
|
4165
4342
|
);
|
|
4166
4343
|
const row = result.rows[0];
|
|
4167
4344
|
await client.query(
|
|
4168
4345
|
`INSERT INTO ${this.#t("branches")} (id, chatId, name, headMessageId, isActive, createdAt)
|
|
4169
4346
|
VALUES ($1, $2, 'main', NULL, TRUE, $3)`,
|
|
4170
|
-
[crypto.randomUUID(),
|
|
4347
|
+
[crypto.randomUUID(), chat2.id, Date.now()]
|
|
4171
4348
|
);
|
|
4172
4349
|
return {
|
|
4173
4350
|
id: row.id,
|
|
@@ -4179,7 +4356,7 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
4179
4356
|
};
|
|
4180
4357
|
});
|
|
4181
4358
|
}
|
|
4182
|
-
async upsertChat(
|
|
4359
|
+
async upsertChat(chat2) {
|
|
4183
4360
|
return this.#useTransaction(async (client) => {
|
|
4184
4361
|
const result = await client.query(
|
|
4185
4362
|
`INSERT INTO ${this.#t("chats")} (id, userId, title, metadata)
|
|
@@ -4187,10 +4364,10 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
4187
4364
|
ON CONFLICT(id) DO UPDATE SET id = EXCLUDED.id
|
|
4188
4365
|
RETURNING *`,
|
|
4189
4366
|
[
|
|
4190
|
-
|
|
4191
|
-
|
|
4192
|
-
|
|
4193
|
-
|
|
4367
|
+
chat2.id,
|
|
4368
|
+
chat2.userId,
|
|
4369
|
+
chat2.title ?? null,
|
|
4370
|
+
chat2.metadata ? JSON.stringify(chat2.metadata) : null
|
|
4194
4371
|
]
|
|
4195
4372
|
);
|
|
4196
4373
|
const row = result.rows[0];
|
|
@@ -4198,7 +4375,7 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
4198
4375
|
`INSERT INTO ${this.#t("branches")} (id, chatId, name, headMessageId, isActive, createdAt)
|
|
4199
4376
|
VALUES ($1, $2, 'main', NULL, TRUE, $3)
|
|
4200
4377
|
ON CONFLICT(chatId, name) DO NOTHING`,
|
|
4201
|
-
[crypto.randomUUID(),
|
|
4378
|
+
[crypto.randomUUID(), chat2.id, Date.now()]
|
|
4202
4379
|
);
|
|
4203
4380
|
return {
|
|
4204
4381
|
id: row.id,
|
|
@@ -4404,8 +4581,8 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
4404
4581
|
return rows[0].exists;
|
|
4405
4582
|
}
|
|
4406
4583
|
async getMessages(chatId) {
|
|
4407
|
-
const
|
|
4408
|
-
if (!
|
|
4584
|
+
const chat2 = await this.getChat(chatId);
|
|
4585
|
+
if (!chat2) {
|
|
4409
4586
|
throw new Error(`Chat "${chatId}" not found`);
|
|
4410
4587
|
}
|
|
4411
4588
|
const activeBranch = await this.getActiveBranch(chatId);
|
|
@@ -4909,17 +5086,17 @@ var SqlServerContextStore = class _SqlServerContextStore extends ContextStore {
|
|
|
4909
5086
|
// ==========================================================================
|
|
4910
5087
|
// Chat Operations
|
|
4911
5088
|
// ==========================================================================
|
|
4912
|
-
async createChat(
|
|
5089
|
+
async createChat(chat2) {
|
|
4913
5090
|
return this.#useTransaction(async (transaction) => {
|
|
4914
5091
|
const mssql = _SqlServerContextStore.#requireMssql();
|
|
4915
5092
|
const request = transaction.request();
|
|
4916
|
-
request.input("p0", mssql.NVarChar,
|
|
4917
|
-
request.input("p1", mssql.NVarChar,
|
|
4918
|
-
request.input("p2", mssql.NVarChar,
|
|
5093
|
+
request.input("p0", mssql.NVarChar, chat2.id);
|
|
5094
|
+
request.input("p1", mssql.NVarChar, chat2.userId);
|
|
5095
|
+
request.input("p2", mssql.NVarChar, chat2.title ?? null);
|
|
4919
5096
|
request.input(
|
|
4920
5097
|
"p3",
|
|
4921
5098
|
mssql.NVarChar,
|
|
4922
|
-
|
|
5099
|
+
chat2.metadata ? JSON.stringify(chat2.metadata) : null
|
|
4923
5100
|
);
|
|
4924
5101
|
const result = await request.query(`
|
|
4925
5102
|
INSERT INTO ${this.#t("chats")} (id, userId, title, metadata)
|
|
@@ -4929,7 +5106,7 @@ var SqlServerContextStore = class _SqlServerContextStore extends ContextStore {
|
|
|
4929
5106
|
const row = result.recordset[0];
|
|
4930
5107
|
const branchRequest = transaction.request();
|
|
4931
5108
|
branchRequest.input("p0", mssql.NVarChar, crypto.randomUUID());
|
|
4932
|
-
branchRequest.input("p1", mssql.NVarChar,
|
|
5109
|
+
branchRequest.input("p1", mssql.NVarChar, chat2.id);
|
|
4933
5110
|
branchRequest.input("p2", mssql.BigInt, Date.now());
|
|
4934
5111
|
await branchRequest.query(`
|
|
4935
5112
|
INSERT INTO ${this.#t("branches")} (id, chatId, name, headMessageId, isActive, createdAt)
|
|
@@ -4945,17 +5122,17 @@ var SqlServerContextStore = class _SqlServerContextStore extends ContextStore {
|
|
|
4945
5122
|
};
|
|
4946
5123
|
});
|
|
4947
5124
|
}
|
|
4948
|
-
async upsertChat(
|
|
5125
|
+
async upsertChat(chat2) {
|
|
4949
5126
|
return this.#useTransaction(async (transaction) => {
|
|
4950
5127
|
const mssql = _SqlServerContextStore.#requireMssql();
|
|
4951
5128
|
const request = transaction.request();
|
|
4952
|
-
request.input("p0", mssql.NVarChar,
|
|
4953
|
-
request.input("p1", mssql.NVarChar,
|
|
4954
|
-
request.input("p2", mssql.NVarChar,
|
|
5129
|
+
request.input("p0", mssql.NVarChar, chat2.id);
|
|
5130
|
+
request.input("p1", mssql.NVarChar, chat2.userId);
|
|
5131
|
+
request.input("p2", mssql.NVarChar, chat2.title ?? null);
|
|
4955
5132
|
request.input(
|
|
4956
5133
|
"p3",
|
|
4957
5134
|
mssql.NVarChar,
|
|
4958
|
-
|
|
5135
|
+
chat2.metadata ? JSON.stringify(chat2.metadata) : null
|
|
4959
5136
|
);
|
|
4960
5137
|
request.input("p4", mssql.BigInt, BigInt(Date.now()));
|
|
4961
5138
|
const result = await request.query(`
|
|
@@ -4972,7 +5149,7 @@ var SqlServerContextStore = class _SqlServerContextStore extends ContextStore {
|
|
|
4972
5149
|
const row = result.recordset[0];
|
|
4973
5150
|
const branchRequest = transaction.request();
|
|
4974
5151
|
branchRequest.input("p0", mssql.NVarChar, crypto.randomUUID());
|
|
4975
|
-
branchRequest.input("p1", mssql.NVarChar,
|
|
5152
|
+
branchRequest.input("p1", mssql.NVarChar, chat2.id);
|
|
4976
5153
|
branchRequest.input("p2", mssql.BigInt, Date.now());
|
|
4977
5154
|
await branchRequest.query(`
|
|
4978
5155
|
IF NOT EXISTS (SELECT 1 FROM ${this.#t("branches")} WHERE chatId = @p1 AND name = 'main')
|
|
@@ -5193,8 +5370,8 @@ var SqlServerContextStore = class _SqlServerContextStore extends ContextStore {
|
|
|
5193
5370
|
return rows[0].hasChildren === 1;
|
|
5194
5371
|
}
|
|
5195
5372
|
async getMessages(chatId) {
|
|
5196
|
-
const
|
|
5197
|
-
if (!
|
|
5373
|
+
const chat2 = await this.getChat(chatId);
|
|
5374
|
+
if (!chat2) {
|
|
5198
5375
|
throw new Error(`Chat "${chatId}" not found`);
|
|
5199
5376
|
}
|
|
5200
5377
|
const activeBranch = await this.getActiveBranch(chatId);
|
|
@@ -5909,7 +6086,7 @@ var SqliteStreamStore = class extends StreamStore {
|
|
|
5909
6086
|
};
|
|
5910
6087
|
|
|
5911
6088
|
// packages/context/src/lib/stream/stream-manager.ts
|
|
5912
|
-
import { createUIMessageStream as
|
|
6089
|
+
import { createUIMessageStream as createUIMessageStream3 } from "ai";
|
|
5913
6090
|
import { setTimeout } from "node:timers/promises";
|
|
5914
6091
|
function isTerminal(status) {
|
|
5915
6092
|
return status !== "queued" && status !== "running";
|
|
@@ -5990,7 +6167,7 @@ var StreamManager = class {
|
|
|
5990
6167
|
}
|
|
5991
6168
|
})();
|
|
5992
6169
|
let pw;
|
|
5993
|
-
const sink =
|
|
6170
|
+
const sink = createUIMessageStream3({
|
|
5994
6171
|
execute: async ({ writer }) => {
|
|
5995
6172
|
pw = await persistedWriter({
|
|
5996
6173
|
writer,
|
|
@@ -6307,6 +6484,7 @@ export {
|
|
|
6307
6484
|
analogy,
|
|
6308
6485
|
assistant,
|
|
6309
6486
|
assistantText,
|
|
6487
|
+
chat,
|
|
6310
6488
|
clarification,
|
|
6311
6489
|
correction,
|
|
6312
6490
|
createAdaptivePollingState,
|
|
@@ -6322,6 +6500,7 @@ export {
|
|
|
6322
6500
|
fail,
|
|
6323
6501
|
fragment,
|
|
6324
6502
|
getModelsRegistry,
|
|
6503
|
+
getReminderRanges,
|
|
6325
6504
|
glossary,
|
|
6326
6505
|
guardrail,
|
|
6327
6506
|
hint,
|
|
@@ -6354,6 +6533,8 @@ export {
|
|
|
6354
6533
|
skills,
|
|
6355
6534
|
soul,
|
|
6356
6535
|
stop,
|
|
6536
|
+
stripReminders,
|
|
6537
|
+
stripTextByRanges,
|
|
6357
6538
|
structuredOutput,
|
|
6358
6539
|
styleGuide,
|
|
6359
6540
|
term,
|