@deepagents/context 0.17.0 → 0.18.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/index.js CHANGED
@@ -1,7 +1,5 @@
1
1
  // packages/context/src/lib/agent.ts
2
- import { groq } from "@ai-sdk/groq";
3
2
  import {
4
- NoSuchToolError,
5
3
  Output,
6
4
  convertToModelMessages,
7
5
  createUIMessageStream,
@@ -12,8 +10,7 @@ import {
12
10
  streamText
13
11
  } from "ai";
14
12
  import chalk from "chalk";
15
- import "zod";
16
- import "@deepagents/agent";
13
+ import { createRepairToolCall } from "@deepagents/agent";
17
14
 
18
15
  // packages/context/src/lib/fragments.ts
19
16
  import { generateId } from "ai";
@@ -169,7 +166,7 @@ var Agent = class _Agent {
169
166
  stopWhen: stepCountIs(25),
170
167
  tools: this.#options.tools,
171
168
  experimental_context: contextVariables,
172
- experimental_repairToolCall: repairToolCall,
169
+ experimental_repairToolCall: createRepairToolCall(this.#options.model),
173
170
  toolChoice: this.#options.toolChoice,
174
171
  onStepFinish: (step) => {
175
172
  const toolCall = step.toolCalls.at(-1);
@@ -225,7 +222,7 @@ var Agent = class _Agent {
225
222
  model: this.#options.model,
226
223
  system: systemPrompt,
227
224
  messages: await convertToModelMessages(messages),
228
- experimental_repairToolCall: repairToolCall,
225
+ experimental_repairToolCall: createRepairToolCall(this.#options.model),
229
226
  stopWhen: stepCountIs(50),
230
227
  experimental_transform: config?.transform ?? smoothStream(),
231
228
  tools: this.#options.tools,
@@ -365,7 +362,7 @@ function structuredOutput(options) {
365
362
  system: systemPrompt,
366
363
  messages: await convertToModelMessages(messages),
367
364
  stopWhen: stepCountIs(25),
368
- experimental_repairToolCall: repairToolCall,
365
+ experimental_repairToolCall: createRepairToolCall(options.model),
369
366
  experimental_context: contextVariables,
370
367
  output: Output.object({ schema: options.schema }),
371
368
  tools: options.tools
@@ -387,7 +384,7 @@ function structuredOutput(options) {
387
384
  providerOptions: options.providerOptions,
388
385
  model: options.model,
389
386
  system: systemPrompt,
390
- experimental_repairToolCall: repairToolCall,
387
+ experimental_repairToolCall: createRepairToolCall(options.model),
391
388
  messages: await convertToModelMessages(messages),
392
389
  stopWhen: stepCountIs(50),
393
390
  experimental_transform: config?.transform ?? smoothStream(),
@@ -398,34 +395,6 @@ function structuredOutput(options) {
398
395
  }
399
396
  };
400
397
  }
401
- var repairToolCall = async ({
402
- toolCall,
403
- tools,
404
- inputSchema,
405
- error
406
- }) => {
407
- console.log(
408
- `Debug: ${chalk.yellow("RepairingToolCall")}: ${chalk.bgYellow(toolCall.toolName)}`,
409
- error.name,
410
- JSON.stringify(toolCall)
411
- );
412
- if (NoSuchToolError.isInstance(error)) {
413
- return null;
414
- }
415
- const tool = tools[toolCall.toolName];
416
- const { output } = await generateText({
417
- model: groq("openai/gpt-oss-20b"),
418
- output: Output.object({ schema: tool.inputSchema }),
419
- prompt: [
420
- `The model tried to call the tool "${toolCall.toolName}" with the following inputs:`,
421
- JSON.stringify(toolCall.input),
422
- `The tool accepts the following schema:`,
423
- JSON.stringify(inputSchema(toolCall)),
424
- "Please fix the inputs."
425
- ].join("\n")
426
- });
427
- return { ...toolCall, input: JSON.stringify(output) };
428
- };
429
398
  function writeText(writer, text) {
430
399
  const feedbackPartId = generateId2();
431
400
  writer.write({
@@ -1593,6 +1562,13 @@ var ContextEngine = class {
1593
1562
  get branch() {
1594
1563
  return this.#branchName;
1595
1564
  }
1565
+ /**
1566
+ * Get the current branch head message ID.
1567
+ * Returns undefined if no messages have been saved yet.
1568
+ */
1569
+ get headMessageId() {
1570
+ return this.#branch?.headMessageId ?? void 0;
1571
+ }
1596
1572
  /**
1597
1573
  * Get metadata for the current chat.
1598
1574
  * Returns null if the chat hasn't been initialized yet.
@@ -1699,7 +1675,7 @@ var ContextEngine = class {
1699
1675
  async save(options) {
1700
1676
  await this.#ensureInitialized();
1701
1677
  if (this.#pendingMessages.length === 0) {
1702
- return;
1678
+ return { headMessageId: this.#branch?.headMessageId ?? void 0 };
1703
1679
  }
1704
1680
  const shouldBranch = options?.branch ?? true;
1705
1681
  for (let i = 0; i < this.#pendingMessages.length; i++) {
@@ -1751,6 +1727,7 @@ var ContextEngine = class {
1751
1727
  await this.#store.updateBranchHead(this.#branch.id, parentId);
1752
1728
  this.#branch.headMessageId = parentId;
1753
1729
  this.#pendingMessages = [];
1730
+ return { headMessageId: this.#branch.headMessageId ?? void 0 };
1754
1731
  }
1755
1732
  /**
1756
1733
  * Resolve a lazy fragment by finding the appropriate ID.
@@ -5699,6 +5676,47 @@ var SqliteStreamStore = class extends StreamStore {
5699
5676
  async deleteStream(streamId) {
5700
5677
  this.#stmt("DELETE FROM streams WHERE id = ?").run(streamId);
5701
5678
  }
5679
+ async reopenStream(streamId) {
5680
+ return this.#transaction(() => {
5681
+ const row = this.#stmt("SELECT * FROM streams WHERE id = ?").get(
5682
+ streamId
5683
+ );
5684
+ if (!row) {
5685
+ throw new Error(`Stream "${streamId}" not found`);
5686
+ }
5687
+ if (row.status !== "completed" && row.status !== "failed" && row.status !== "cancelled") {
5688
+ throw new Error(
5689
+ `Cannot reopen stream "${streamId}" with status "${row.status}". Only terminal streams can be reopened.`
5690
+ );
5691
+ }
5692
+ this.#stmt("DELETE FROM streams WHERE id = ?").run(streamId);
5693
+ const now = Date.now();
5694
+ this.#stmt(
5695
+ `INSERT INTO streams (id, status, createdAt, startedAt, finishedAt, cancelRequestedAt, error)
5696
+ VALUES (?, ?, ?, ?, ?, ?, ?)`
5697
+ ).run(streamId, "queued", now, null, null, null, null);
5698
+ return {
5699
+ id: streamId,
5700
+ status: "queued",
5701
+ createdAt: now,
5702
+ startedAt: null,
5703
+ finishedAt: null,
5704
+ cancelRequestedAt: null,
5705
+ error: null
5706
+ };
5707
+ });
5708
+ }
5709
+ #transaction(callback) {
5710
+ try {
5711
+ this.#db.exec("BEGIN IMMEDIATE");
5712
+ const result = callback();
5713
+ this.#db.exec("COMMIT");
5714
+ return result;
5715
+ } catch (error) {
5716
+ this.#db.exec("ROLLBACK");
5717
+ throw error;
5718
+ }
5719
+ }
5702
5720
  };
5703
5721
 
5704
5722
  // packages/context/src/lib/stream/stream-manager.ts
@@ -5823,6 +5841,10 @@ var StreamManager = class {
5823
5841
  }
5824
5842
  });
5825
5843
  }
5844
+ async reopen(streamId) {
5845
+ const stream = await this.#store.reopenStream(streamId);
5846
+ return { stream, created: true };
5847
+ }
5826
5848
  async cleanup(streamId) {
5827
5849
  await this.#store.deleteStream(streamId);
5828
5850
  }