@deepagents/context 0.20.0 → 0.23.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 +2 -0
- package/dist/browser.js +38 -0
- package/dist/browser.js.map +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +216 -52
- 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 +24 -0
- package/dist/lib/chat.d.ts.map +1 -0
- package/dist/lib/fragments/message/user.d.ts +12 -2
- package/dist/lib/fragments/message/user.d.ts.map +1 -1
- package/dist/lib/fragments.d.ts +13 -4
- package/dist/lib/fragments.d.ts.map +1 -1
- package/dist/lib/store/sqlite.store.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iCAAiC,CAAC;AAChD,cAAc,yBAAyB,CAAC;AACxC,cAAc,oBAAoB,CAAC;AACnC,cAAc,8CAA8C,CAAC;AAC7D,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iBAAiB,CAAC;AAChC,cAAc,sCAAsC,CAAC;AACrD,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC;AACvC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,qCAAqC,CAAC;AACpD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iCAAiC,CAAC;AAChD,cAAc,yBAAyB,CAAC;AACxC,cAAc,oBAAoB,CAAC;AACnC,cAAc,8CAA8C,CAAC;AAC7D,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iBAAiB,CAAC;AAChC,cAAc,sCAAsC,CAAC;AACrD,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC;AACvC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,qCAAqC,CAAC;AACpD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -120,9 +120,11 @@ var Agent = class _Agent {
|
|
|
120
120
|
#options;
|
|
121
121
|
#guardrails = [];
|
|
122
122
|
tools;
|
|
123
|
+
context;
|
|
123
124
|
constructor(options) {
|
|
124
125
|
this.#options = options;
|
|
125
126
|
this.tools = options.tools || {};
|
|
127
|
+
this.context = options.context;
|
|
126
128
|
this.#guardrails = options.guardrails || [];
|
|
127
129
|
}
|
|
128
130
|
async generate(contextVariables, config) {
|
|
@@ -190,17 +192,25 @@ var Agent = class _Agent {
|
|
|
190
192
|
* Create a raw stream without guardrail processing.
|
|
191
193
|
*/
|
|
192
194
|
async #createRawStream(contextVariables, config) {
|
|
193
|
-
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({
|
|
194
204
|
renderer: new XmlRenderer()
|
|
195
205
|
});
|
|
196
206
|
const runId = generateId2();
|
|
197
207
|
return streamText({
|
|
198
208
|
abortSignal: config?.abortSignal,
|
|
199
209
|
providerOptions: this.#options.providerOptions,
|
|
200
|
-
model
|
|
210
|
+
model,
|
|
201
211
|
system: systemPrompt,
|
|
202
212
|
messages: await convertToModelMessages(messages),
|
|
203
|
-
experimental_repairToolCall: createRepairToolCall(
|
|
213
|
+
experimental_repairToolCall: createRepairToolCall(model),
|
|
204
214
|
stopWhen: stepCountIs(50),
|
|
205
215
|
experimental_transform: config?.transform ?? smoothStream(),
|
|
206
216
|
tools: this.#options.tools,
|
|
@@ -227,6 +237,9 @@ var Agent = class _Agent {
|
|
|
227
237
|
#wrapWithGuardrails(result, contextVariables, config) {
|
|
228
238
|
const maxRetries = config?.maxRetries ?? this.#options.maxGuardrailRetries ?? 3;
|
|
229
239
|
const context = this.#options.context;
|
|
240
|
+
if (!context) {
|
|
241
|
+
throw new Error(`Agent ${this.#options.name} is missing a context.`);
|
|
242
|
+
}
|
|
230
243
|
const originalToUIMessageStream = result.toUIMessageStream.bind(result);
|
|
231
244
|
result.toUIMessageStream = (options) => {
|
|
232
245
|
return createUIMessageStream({
|
|
@@ -390,6 +403,116 @@ function writeText(writer, text) {
|
|
|
390
403
|
});
|
|
391
404
|
}
|
|
392
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
|
+
function toMessageFragment(item) {
|
|
416
|
+
if (isFragment(item) && isMessageFragment(item)) {
|
|
417
|
+
return item;
|
|
418
|
+
}
|
|
419
|
+
return message(item);
|
|
420
|
+
}
|
|
421
|
+
function chatMessageToUIMessage(item) {
|
|
422
|
+
if (isFragment(item) && isMessageFragment(item)) {
|
|
423
|
+
return item.codec.decode();
|
|
424
|
+
}
|
|
425
|
+
return item;
|
|
426
|
+
}
|
|
427
|
+
async function chat(agent2, messages, options) {
|
|
428
|
+
const context = agent2.context;
|
|
429
|
+
if (!context) {
|
|
430
|
+
throw new Error(
|
|
431
|
+
"Agent is missing a context. Provide context when creating the agent."
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
if (messages.length === 0) {
|
|
435
|
+
throw new Error("messages must not be empty");
|
|
436
|
+
}
|
|
437
|
+
const lastItem = messages[messages.length - 1];
|
|
438
|
+
const lastFragment = toMessageFragment(lastItem);
|
|
439
|
+
const lastUIMessage = chatMessageToUIMessage(lastItem);
|
|
440
|
+
let assistantMsgId;
|
|
441
|
+
if (lastUIMessage.role === "assistant") {
|
|
442
|
+
context.set(lastFragment);
|
|
443
|
+
await context.save({ branch: false });
|
|
444
|
+
assistantMsgId = lastUIMessage.id;
|
|
445
|
+
} else {
|
|
446
|
+
context.set(lastFragment);
|
|
447
|
+
await context.save();
|
|
448
|
+
assistantMsgId = options?.generateMessageId?.() ?? generateId3();
|
|
449
|
+
}
|
|
450
|
+
const uiMessages = messages.map(chatMessageToUIMessage);
|
|
451
|
+
const streamContextVariables = options?.contextVariables === void 0 ? {} : options.contextVariables;
|
|
452
|
+
const result = await agent2.stream(streamContextVariables, {
|
|
453
|
+
transform: options?.transform
|
|
454
|
+
});
|
|
455
|
+
const uiStream = result.toUIMessageStream({
|
|
456
|
+
onError: options?.onError ?? formatChatError,
|
|
457
|
+
sendStart: true,
|
|
458
|
+
sendFinish: true,
|
|
459
|
+
sendReasoning: true,
|
|
460
|
+
sendSources: true,
|
|
461
|
+
originalMessages: uiMessages,
|
|
462
|
+
generateMessageId: () => assistantMsgId,
|
|
463
|
+
messageMetadata: options?.messageMetadata
|
|
464
|
+
});
|
|
465
|
+
return createUIMessageStream2({
|
|
466
|
+
originalMessages: uiMessages,
|
|
467
|
+
generateId: () => assistantMsgId,
|
|
468
|
+
onStepFinish: async ({ responseMessage }) => {
|
|
469
|
+
const normalizedMessage = {
|
|
470
|
+
...responseMessage,
|
|
471
|
+
id: assistantMsgId
|
|
472
|
+
};
|
|
473
|
+
context.set(assistant(normalizedMessage));
|
|
474
|
+
await context.save({ branch: false });
|
|
475
|
+
},
|
|
476
|
+
onFinish: async ({ responseMessage }) => {
|
|
477
|
+
const normalizedMessage = {
|
|
478
|
+
...responseMessage,
|
|
479
|
+
id: assistantMsgId
|
|
480
|
+
};
|
|
481
|
+
const finalMetadata = await options?.finalAssistantMetadata?.(normalizedMessage);
|
|
482
|
+
const finalMessage = finalMetadata === void 0 ? normalizedMessage : {
|
|
483
|
+
...normalizedMessage,
|
|
484
|
+
metadata: {
|
|
485
|
+
...normalizedMessage.metadata ?? {},
|
|
486
|
+
...finalMetadata
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
context.set(assistant(finalMessage));
|
|
490
|
+
await context.save({ branch: false });
|
|
491
|
+
const totalUsage = await result.totalUsage;
|
|
492
|
+
await context.trackUsage(totalUsage);
|
|
493
|
+
},
|
|
494
|
+
execute: async ({ writer }) => {
|
|
495
|
+
writer.merge(uiStream);
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
function formatChatError(error) {
|
|
500
|
+
if (NoSuchToolError.isInstance(error)) {
|
|
501
|
+
return "The model tried to call an unknown tool.";
|
|
502
|
+
}
|
|
503
|
+
if (InvalidToolInputError.isInstance(error)) {
|
|
504
|
+
return "The model called a tool with invalid arguments.";
|
|
505
|
+
}
|
|
506
|
+
if (ToolCallRepairError.isInstance(error)) {
|
|
507
|
+
return "The model tried to call a tool with invalid arguments, but it was repaired.";
|
|
508
|
+
}
|
|
509
|
+
if (APICallError.isInstance(error)) {
|
|
510
|
+
console.error("Upstream API call failed:", error);
|
|
511
|
+
return `Upstream API call failed with status ${error.statusCode}: ${error.message}`;
|
|
512
|
+
}
|
|
513
|
+
return JSON.stringify(error);
|
|
514
|
+
}
|
|
515
|
+
|
|
393
516
|
// packages/context/src/lib/engine.ts
|
|
394
517
|
import { mergeWith } from "lodash-es";
|
|
395
518
|
|
|
@@ -2256,7 +2379,7 @@ function policy(input) {
|
|
|
2256
2379
|
}
|
|
2257
2380
|
|
|
2258
2381
|
// packages/context/src/lib/fragments/message/user.ts
|
|
2259
|
-
import { generateId as
|
|
2382
|
+
import { generateId as generateId4 } from "ai";
|
|
2260
2383
|
var SYSTEM_REMINDER_OPEN_TAG = "<system-reminder>";
|
|
2261
2384
|
var SYSTEM_REMINDER_CLOSE_TAG = "</system-reminder>";
|
|
2262
2385
|
function getReminderRanges(metadata) {
|
|
@@ -2288,6 +2411,43 @@ function stripTextByRanges(text, ranges) {
|
|
|
2288
2411
|
output += text.slice(cursor);
|
|
2289
2412
|
return output.trimEnd();
|
|
2290
2413
|
}
|
|
2414
|
+
function stripReminders(message2) {
|
|
2415
|
+
const reminderRanges = getReminderRanges(
|
|
2416
|
+
isRecord(message2.metadata) ? message2.metadata : void 0
|
|
2417
|
+
);
|
|
2418
|
+
const rangesByPartIndex = /* @__PURE__ */ new Map();
|
|
2419
|
+
for (const range of reminderRanges) {
|
|
2420
|
+
const partRanges = rangesByPartIndex.get(range.partIndex) ?? [];
|
|
2421
|
+
partRanges.push({ start: range.start, end: range.end });
|
|
2422
|
+
rangesByPartIndex.set(range.partIndex, partRanges);
|
|
2423
|
+
}
|
|
2424
|
+
const strippedParts = message2.parts.flatMap((part, partIndex) => {
|
|
2425
|
+
const clonedPart = { ...part };
|
|
2426
|
+
const ranges = rangesByPartIndex.get(partIndex);
|
|
2427
|
+
if (clonedPart.type !== "text" || ranges === void 0) {
|
|
2428
|
+
return [clonedPart];
|
|
2429
|
+
}
|
|
2430
|
+
const strippedText = stripTextByRanges(clonedPart.text, ranges);
|
|
2431
|
+
if (strippedText.length === 0) {
|
|
2432
|
+
return [];
|
|
2433
|
+
}
|
|
2434
|
+
return [{ ...clonedPart, text: strippedText }];
|
|
2435
|
+
});
|
|
2436
|
+
const nextMessage = {
|
|
2437
|
+
...message2,
|
|
2438
|
+
parts: strippedParts
|
|
2439
|
+
};
|
|
2440
|
+
if (isRecord(message2.metadata)) {
|
|
2441
|
+
const metadata = { ...message2.metadata };
|
|
2442
|
+
delete metadata.reminders;
|
|
2443
|
+
if (Object.keys(metadata).length > 0) {
|
|
2444
|
+
nextMessage.metadata = metadata;
|
|
2445
|
+
} else {
|
|
2446
|
+
delete nextMessage.metadata;
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
return nextMessage;
|
|
2450
|
+
}
|
|
2291
2451
|
function isRecord(value) {
|
|
2292
2452
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2293
2453
|
}
|
|
@@ -2330,7 +2490,7 @@ function applyInlineReminder(message2, value) {
|
|
|
2330
2490
|
const updatedText = `${textPart.text}${reminderText}`;
|
|
2331
2491
|
message2.parts[partIndex] = { ...textPart, text: updatedText };
|
|
2332
2492
|
return {
|
|
2333
|
-
id:
|
|
2493
|
+
id: generateId4(),
|
|
2334
2494
|
text: value,
|
|
2335
2495
|
partIndex,
|
|
2336
2496
|
start,
|
|
@@ -2343,7 +2503,7 @@ function applyPartReminder(message2, value) {
|
|
|
2343
2503
|
message2.parts.push(part);
|
|
2344
2504
|
const partIndex = message2.parts.length - 1;
|
|
2345
2505
|
return {
|
|
2346
|
-
id:
|
|
2506
|
+
id: generateId4(),
|
|
2347
2507
|
text: value,
|
|
2348
2508
|
partIndex,
|
|
2349
2509
|
start: 0,
|
|
@@ -2360,7 +2520,7 @@ function reminder(text, options) {
|
|
|
2360
2520
|
}
|
|
2361
2521
|
function user(content, ...reminders) {
|
|
2362
2522
|
const message2 = typeof content === "string" ? {
|
|
2363
|
-
id:
|
|
2523
|
+
id: generateId4(),
|
|
2364
2524
|
role: "user",
|
|
2365
2525
|
parts: [{ type: "text", text: content }]
|
|
2366
2526
|
} : { ...content, role: "user", parts: [...content.parts] };
|
|
@@ -3519,22 +3679,22 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
3519
3679
|
// ==========================================================================
|
|
3520
3680
|
// Chat Operations
|
|
3521
3681
|
// ==========================================================================
|
|
3522
|
-
async createChat(
|
|
3682
|
+
async createChat(chat2) {
|
|
3523
3683
|
return this.#useTransaction(() => {
|
|
3524
3684
|
const row = this.#db.prepare(
|
|
3525
3685
|
`INSERT INTO chats (id, userId, title, metadata)
|
|
3526
3686
|
VALUES (?, ?, ?, ?)
|
|
3527
3687
|
RETURNING *`
|
|
3528
3688
|
).get(
|
|
3529
|
-
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3689
|
+
chat2.id,
|
|
3690
|
+
chat2.userId,
|
|
3691
|
+
chat2.title ?? null,
|
|
3692
|
+
chat2.metadata ? JSON.stringify(chat2.metadata) : null
|
|
3533
3693
|
);
|
|
3534
3694
|
this.#db.prepare(
|
|
3535
3695
|
`INSERT INTO branches (id, chatId, name, headMessageId, isActive, createdAt)
|
|
3536
3696
|
VALUES (?, ?, 'main', NULL, 1, ?)`
|
|
3537
|
-
).run(crypto.randomUUID(),
|
|
3697
|
+
).run(crypto.randomUUID(), chat2.id, Date.now());
|
|
3538
3698
|
return {
|
|
3539
3699
|
id: row.id,
|
|
3540
3700
|
userId: row.userId,
|
|
@@ -3545,7 +3705,7 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
3545
3705
|
};
|
|
3546
3706
|
});
|
|
3547
3707
|
}
|
|
3548
|
-
async upsertChat(
|
|
3708
|
+
async upsertChat(chat2) {
|
|
3549
3709
|
return this.#useTransaction(() => {
|
|
3550
3710
|
const row = this.#db.prepare(
|
|
3551
3711
|
`INSERT INTO chats (id, userId, title, metadata)
|
|
@@ -3553,15 +3713,15 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
3553
3713
|
ON CONFLICT(id) DO UPDATE SET id = excluded.id
|
|
3554
3714
|
RETURNING *`
|
|
3555
3715
|
).get(
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3716
|
+
chat2.id,
|
|
3717
|
+
chat2.userId,
|
|
3718
|
+
chat2.title ?? null,
|
|
3719
|
+
chat2.metadata ? JSON.stringify(chat2.metadata) : null
|
|
3560
3720
|
);
|
|
3561
3721
|
this.#db.prepare(
|
|
3562
3722
|
`INSERT OR IGNORE INTO branches (id, chatId, name, headMessageId, isActive, createdAt)
|
|
3563
3723
|
VALUES (?, ?, 'main', NULL, 1, ?)`
|
|
3564
|
-
).run(crypto.randomUUID(),
|
|
3724
|
+
).run(crypto.randomUUID(), chat2.id, Date.now());
|
|
3565
3725
|
return {
|
|
3566
3726
|
id: row.id,
|
|
3567
3727
|
userId: row.userId,
|
|
@@ -3757,8 +3917,8 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
3757
3917
|
return row.hasChildren === 1;
|
|
3758
3918
|
}
|
|
3759
3919
|
async getMessages(chatId) {
|
|
3760
|
-
const
|
|
3761
|
-
if (!
|
|
3920
|
+
const chat2 = await this.getChat(chatId);
|
|
3921
|
+
if (!chat2) {
|
|
3762
3922
|
throw new Error(`Chat "${chatId}" not found`);
|
|
3763
3923
|
}
|
|
3764
3924
|
const activeBranch = await this.getActiveBranch(chatId);
|
|
@@ -3961,7 +4121,7 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
3961
4121
|
).all(chatId);
|
|
3962
4122
|
const nodes = messageRows.map((row) => {
|
|
3963
4123
|
const data = JSON.parse(row.data);
|
|
3964
|
-
const content = typeof data === "string" ? data : JSON.stringify(data);
|
|
4124
|
+
const content = typeof data === "string" ? data : Array.isArray(data.parts) ? data.parts.filter((p) => p.type === "text").map((p) => p.text).join(" ") : JSON.stringify(data);
|
|
3965
4125
|
return {
|
|
3966
4126
|
id: row.id,
|
|
3967
4127
|
parentId: row.parentId,
|
|
@@ -4182,24 +4342,24 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
4182
4342
|
// ==========================================================================
|
|
4183
4343
|
// Chat Operations
|
|
4184
4344
|
// ==========================================================================
|
|
4185
|
-
async createChat(
|
|
4345
|
+
async createChat(chat2) {
|
|
4186
4346
|
return this.#useTransaction(async (client) => {
|
|
4187
4347
|
const result = await client.query(
|
|
4188
4348
|
`INSERT INTO ${this.#t("chats")} (id, userId, title, metadata)
|
|
4189
4349
|
VALUES ($1, $2, $3, $4)
|
|
4190
4350
|
RETURNING *`,
|
|
4191
4351
|
[
|
|
4192
|
-
|
|
4193
|
-
|
|
4194
|
-
|
|
4195
|
-
|
|
4352
|
+
chat2.id,
|
|
4353
|
+
chat2.userId,
|
|
4354
|
+
chat2.title ?? null,
|
|
4355
|
+
chat2.metadata ? JSON.stringify(chat2.metadata) : null
|
|
4196
4356
|
]
|
|
4197
4357
|
);
|
|
4198
4358
|
const row = result.rows[0];
|
|
4199
4359
|
await client.query(
|
|
4200
4360
|
`INSERT INTO ${this.#t("branches")} (id, chatId, name, headMessageId, isActive, createdAt)
|
|
4201
4361
|
VALUES ($1, $2, 'main', NULL, TRUE, $3)`,
|
|
4202
|
-
[crypto.randomUUID(),
|
|
4362
|
+
[crypto.randomUUID(), chat2.id, Date.now()]
|
|
4203
4363
|
);
|
|
4204
4364
|
return {
|
|
4205
4365
|
id: row.id,
|
|
@@ -4211,7 +4371,7 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
4211
4371
|
};
|
|
4212
4372
|
});
|
|
4213
4373
|
}
|
|
4214
|
-
async upsertChat(
|
|
4374
|
+
async upsertChat(chat2) {
|
|
4215
4375
|
return this.#useTransaction(async (client) => {
|
|
4216
4376
|
const result = await client.query(
|
|
4217
4377
|
`INSERT INTO ${this.#t("chats")} (id, userId, title, metadata)
|
|
@@ -4219,10 +4379,10 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
4219
4379
|
ON CONFLICT(id) DO UPDATE SET id = EXCLUDED.id
|
|
4220
4380
|
RETURNING *`,
|
|
4221
4381
|
[
|
|
4222
|
-
|
|
4223
|
-
|
|
4224
|
-
|
|
4225
|
-
|
|
4382
|
+
chat2.id,
|
|
4383
|
+
chat2.userId,
|
|
4384
|
+
chat2.title ?? null,
|
|
4385
|
+
chat2.metadata ? JSON.stringify(chat2.metadata) : null
|
|
4226
4386
|
]
|
|
4227
4387
|
);
|
|
4228
4388
|
const row = result.rows[0];
|
|
@@ -4230,7 +4390,7 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
4230
4390
|
`INSERT INTO ${this.#t("branches")} (id, chatId, name, headMessageId, isActive, createdAt)
|
|
4231
4391
|
VALUES ($1, $2, 'main', NULL, TRUE, $3)
|
|
4232
4392
|
ON CONFLICT(chatId, name) DO NOTHING`,
|
|
4233
|
-
[crypto.randomUUID(),
|
|
4393
|
+
[crypto.randomUUID(), chat2.id, Date.now()]
|
|
4234
4394
|
);
|
|
4235
4395
|
return {
|
|
4236
4396
|
id: row.id,
|
|
@@ -4436,8 +4596,8 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
4436
4596
|
return rows[0].exists;
|
|
4437
4597
|
}
|
|
4438
4598
|
async getMessages(chatId) {
|
|
4439
|
-
const
|
|
4440
|
-
if (!
|
|
4599
|
+
const chat2 = await this.getChat(chatId);
|
|
4600
|
+
if (!chat2) {
|
|
4441
4601
|
throw new Error(`Chat "${chatId}" not found`);
|
|
4442
4602
|
}
|
|
4443
4603
|
const activeBranch = await this.getActiveBranch(chatId);
|
|
@@ -4941,17 +5101,17 @@ var SqlServerContextStore = class _SqlServerContextStore extends ContextStore {
|
|
|
4941
5101
|
// ==========================================================================
|
|
4942
5102
|
// Chat Operations
|
|
4943
5103
|
// ==========================================================================
|
|
4944
|
-
async createChat(
|
|
5104
|
+
async createChat(chat2) {
|
|
4945
5105
|
return this.#useTransaction(async (transaction) => {
|
|
4946
5106
|
const mssql = _SqlServerContextStore.#requireMssql();
|
|
4947
5107
|
const request = transaction.request();
|
|
4948
|
-
request.input("p0", mssql.NVarChar,
|
|
4949
|
-
request.input("p1", mssql.NVarChar,
|
|
4950
|
-
request.input("p2", mssql.NVarChar,
|
|
5108
|
+
request.input("p0", mssql.NVarChar, chat2.id);
|
|
5109
|
+
request.input("p1", mssql.NVarChar, chat2.userId);
|
|
5110
|
+
request.input("p2", mssql.NVarChar, chat2.title ?? null);
|
|
4951
5111
|
request.input(
|
|
4952
5112
|
"p3",
|
|
4953
5113
|
mssql.NVarChar,
|
|
4954
|
-
|
|
5114
|
+
chat2.metadata ? JSON.stringify(chat2.metadata) : null
|
|
4955
5115
|
);
|
|
4956
5116
|
const result = await request.query(`
|
|
4957
5117
|
INSERT INTO ${this.#t("chats")} (id, userId, title, metadata)
|
|
@@ -4961,7 +5121,7 @@ var SqlServerContextStore = class _SqlServerContextStore extends ContextStore {
|
|
|
4961
5121
|
const row = result.recordset[0];
|
|
4962
5122
|
const branchRequest = transaction.request();
|
|
4963
5123
|
branchRequest.input("p0", mssql.NVarChar, crypto.randomUUID());
|
|
4964
|
-
branchRequest.input("p1", mssql.NVarChar,
|
|
5124
|
+
branchRequest.input("p1", mssql.NVarChar, chat2.id);
|
|
4965
5125
|
branchRequest.input("p2", mssql.BigInt, Date.now());
|
|
4966
5126
|
await branchRequest.query(`
|
|
4967
5127
|
INSERT INTO ${this.#t("branches")} (id, chatId, name, headMessageId, isActive, createdAt)
|
|
@@ -4977,17 +5137,17 @@ var SqlServerContextStore = class _SqlServerContextStore extends ContextStore {
|
|
|
4977
5137
|
};
|
|
4978
5138
|
});
|
|
4979
5139
|
}
|
|
4980
|
-
async upsertChat(
|
|
5140
|
+
async upsertChat(chat2) {
|
|
4981
5141
|
return this.#useTransaction(async (transaction) => {
|
|
4982
5142
|
const mssql = _SqlServerContextStore.#requireMssql();
|
|
4983
5143
|
const request = transaction.request();
|
|
4984
|
-
request.input("p0", mssql.NVarChar,
|
|
4985
|
-
request.input("p1", mssql.NVarChar,
|
|
4986
|
-
request.input("p2", mssql.NVarChar,
|
|
5144
|
+
request.input("p0", mssql.NVarChar, chat2.id);
|
|
5145
|
+
request.input("p1", mssql.NVarChar, chat2.userId);
|
|
5146
|
+
request.input("p2", mssql.NVarChar, chat2.title ?? null);
|
|
4987
5147
|
request.input(
|
|
4988
5148
|
"p3",
|
|
4989
5149
|
mssql.NVarChar,
|
|
4990
|
-
|
|
5150
|
+
chat2.metadata ? JSON.stringify(chat2.metadata) : null
|
|
4991
5151
|
);
|
|
4992
5152
|
request.input("p4", mssql.BigInt, BigInt(Date.now()));
|
|
4993
5153
|
const result = await request.query(`
|
|
@@ -5004,7 +5164,7 @@ var SqlServerContextStore = class _SqlServerContextStore extends ContextStore {
|
|
|
5004
5164
|
const row = result.recordset[0];
|
|
5005
5165
|
const branchRequest = transaction.request();
|
|
5006
5166
|
branchRequest.input("p0", mssql.NVarChar, crypto.randomUUID());
|
|
5007
|
-
branchRequest.input("p1", mssql.NVarChar,
|
|
5167
|
+
branchRequest.input("p1", mssql.NVarChar, chat2.id);
|
|
5008
5168
|
branchRequest.input("p2", mssql.BigInt, Date.now());
|
|
5009
5169
|
await branchRequest.query(`
|
|
5010
5170
|
IF NOT EXISTS (SELECT 1 FROM ${this.#t("branches")} WHERE chatId = @p1 AND name = 'main')
|
|
@@ -5225,8 +5385,8 @@ var SqlServerContextStore = class _SqlServerContextStore extends ContextStore {
|
|
|
5225
5385
|
return rows[0].hasChildren === 1;
|
|
5226
5386
|
}
|
|
5227
5387
|
async getMessages(chatId) {
|
|
5228
|
-
const
|
|
5229
|
-
if (!
|
|
5388
|
+
const chat2 = await this.getChat(chatId);
|
|
5389
|
+
if (!chat2) {
|
|
5230
5390
|
throw new Error(`Chat "${chatId}" not found`);
|
|
5231
5391
|
}
|
|
5232
5392
|
const activeBranch = await this.getActiveBranch(chatId);
|
|
@@ -5941,7 +6101,7 @@ var SqliteStreamStore = class extends StreamStore {
|
|
|
5941
6101
|
};
|
|
5942
6102
|
|
|
5943
6103
|
// packages/context/src/lib/stream/stream-manager.ts
|
|
5944
|
-
import { createUIMessageStream as
|
|
6104
|
+
import { createUIMessageStream as createUIMessageStream3 } from "ai";
|
|
5945
6105
|
import { setTimeout } from "node:timers/promises";
|
|
5946
6106
|
function isTerminal(status) {
|
|
5947
6107
|
return status !== "queued" && status !== "running";
|
|
@@ -6022,7 +6182,7 @@ var StreamManager = class {
|
|
|
6022
6182
|
}
|
|
6023
6183
|
})();
|
|
6024
6184
|
let pw;
|
|
6025
|
-
const sink =
|
|
6185
|
+
const sink = createUIMessageStream3({
|
|
6026
6186
|
execute: async ({ writer }) => {
|
|
6027
6187
|
pw = await persistedWriter({
|
|
6028
6188
|
writer,
|
|
@@ -6339,6 +6499,8 @@ export {
|
|
|
6339
6499
|
analogy,
|
|
6340
6500
|
assistant,
|
|
6341
6501
|
assistantText,
|
|
6502
|
+
chat,
|
|
6503
|
+
chatMessageToUIMessage,
|
|
6342
6504
|
clarification,
|
|
6343
6505
|
correction,
|
|
6344
6506
|
createAdaptivePollingState,
|
|
@@ -6387,10 +6549,12 @@ export {
|
|
|
6387
6549
|
skills,
|
|
6388
6550
|
soul,
|
|
6389
6551
|
stop,
|
|
6552
|
+
stripReminders,
|
|
6390
6553
|
stripTextByRanges,
|
|
6391
6554
|
structuredOutput,
|
|
6392
6555
|
styleGuide,
|
|
6393
6556
|
term,
|
|
6557
|
+
toMessageFragment,
|
|
6394
6558
|
useSandbox,
|
|
6395
6559
|
user,
|
|
6396
6560
|
userContext,
|