@deepagents/agent 4.2.1 → 5.0.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.d.ts CHANGED
@@ -5,4 +5,5 @@ export * from './lib/prompts.ts';
5
5
  export * from './lib/repair.ts';
6
6
  export * from './lib/stream_utils.ts';
7
7
  export * from './lib/swarm.ts';
8
+ export * from './lib/tool-output.ts';
8
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC"}
package/dist/index.js CHANGED
@@ -4,8 +4,8 @@ import {
4
4
  Output as Output3,
5
5
  dynamicTool,
6
6
  generateText as generateText3,
7
+ isStepCount as isStepCount2,
7
8
  jsonSchema,
8
- stepCountIs as stepCountIs2,
9
9
  tool
10
10
  } from "ai";
11
11
  import chalk3 from "chalk";
@@ -308,7 +308,7 @@ async function streamWrite(response) {
308
308
  response.consumeStream();
309
309
  const writeStream = createWriteStream("blog_writer_output.md");
310
310
  Readable.fromWeb(response.textStream).pipe(writeStream);
311
- console.log(await response.totalUsage);
311
+ console.log(await response.usage);
312
312
  }
313
313
  function printChunk(chunk, options, state) {
314
314
  const {
@@ -375,7 +375,7 @@ var printer = {
375
375
  state
376
376
  );
377
377
  }
378
- console.log(await response.totalUsage);
378
+ console.log(await response.usage);
379
379
  },
380
380
  mermaid: flow(visualizeMermaid, console.log),
381
381
  semantic: flow(visualizeSemantic, console.log),
@@ -442,7 +442,12 @@ function toOutput(result) {
442
442
  return isPromise(result) ? result.then((res) => res.output) : last(result.partialOutputStream);
443
443
  }
444
444
  function toState(options) {
445
- return options.experimental_context;
445
+ return options.context;
446
+ }
447
+ function toToolsContext(tools, context) {
448
+ return Object.fromEntries(
449
+ Object.keys(tools).map((toolName) => [toolName, context])
450
+ );
446
451
  }
447
452
 
448
453
  // packages/agent/src/lib/swarm.ts
@@ -452,31 +457,33 @@ import {
452
457
  createUIMessageStream,
453
458
  generateId as generateId2,
454
459
  generateText as generateText2,
460
+ isStepCount,
455
461
  smoothStream,
456
- stepCountIs,
457
462
  streamText
458
463
  } from "ai";
459
464
  import chalk2 from "chalk";
460
465
  import dedent2 from "dedent";
461
466
  async function generate(agent2, messages, contextVariables, config) {
467
+ const tools = agent2.toToolset();
462
468
  return generateText2({
463
469
  abortSignal: config?.abortSignal,
464
470
  providerOptions: agent2.providerOptions ?? config?.providerOptions,
465
471
  model: agent2.model,
466
- system: agent2.instructions(contextVariables),
472
+ instructions: agent2.instructions(contextVariables),
467
473
  messages: await convertToModelMessages(
468
474
  Array.isArray(messages) ? messages : [user(messages)],
469
- { ignoreIncompleteToolCalls: true }
475
+ { ignoreIncompleteToolCalls: true, tools }
470
476
  ),
471
- experimental_repairToolCall: agent2.repairToolCall(config?.abortSignal),
472
- stopWhen: stepCountIs(25),
473
- tools: agent2.toToolset(),
477
+ repairToolCall: agent2.repairToolCall(config?.abortSignal),
478
+ stopWhen: isStepCount(25),
479
+ tools,
474
480
  activeTools: agent2.toolsNames,
475
- experimental_context: contextVariables,
481
+ runtimeContext: contextVariables,
482
+ toolsContext: toToolsContext(tools, contextVariables),
476
483
  toolChoice: agent2.toolChoice,
477
484
  output: agent2.output ? Output2.object({ schema: agent2.output }) : void 0,
478
- // onStepFinish: (step) => tagAgents(step, agent.handoff.name),
479
- onStepFinish: (step) => {
485
+ // onStepEnd: (step) => tagAgents(step, agent.handoff.name),
486
+ onStepEnd: (step) => {
480
487
  const toolCall = step.toolCalls.at(-1);
481
488
  if (toolCall) {
482
489
  console.log(
@@ -485,29 +492,31 @@ async function generate(agent2, messages, contextVariables, config) {
485
492
  }
486
493
  },
487
494
  prepareStep: prepareStep(agent2, agent2.model, contextVariables)
488
- // onFinish: (result) => {
495
+ // onEnd: (result) => {
489
496
  // (contextVariables as any).content = result.content;
490
497
  // },
491
498
  });
492
499
  }
493
500
  async function execute(agent2, messages, contextVariables, config) {
494
501
  const runId = generateId2();
502
+ const tools = agent2.toToolset();
495
503
  const stream2 = streamText({
496
504
  abortSignal: config?.abortSignal,
497
505
  providerOptions: config?.providerOptions,
498
506
  model: agent2.model,
499
- system: agent2.instructions(contextVariables),
507
+ instructions: agent2.instructions(contextVariables),
500
508
  messages: await convertToModelMessages(
501
509
  Array.isArray(messages) ? messages : [user(messages)],
502
- { ignoreIncompleteToolCalls: true }
510
+ { ignoreIncompleteToolCalls: true, tools }
503
511
  ),
504
- stopWhen: stepCountIs(25),
512
+ stopWhen: isStepCount(25),
505
513
  experimental_transform: config?.transform ?? smoothStream(),
506
- tools: agent2.toToolset(),
514
+ tools,
507
515
  activeTools: agent2.toolsNames,
508
- experimental_context: contextVariables,
516
+ runtimeContext: contextVariables,
517
+ toolsContext: toToolsContext(tools, contextVariables),
509
518
  toolChoice: agent2.toolChoice,
510
- experimental_repairToolCall: agent2.repairToolCall(config?.abortSignal),
519
+ repairToolCall: agent2.repairToolCall(config?.abortSignal),
511
520
  onError: (error) => {
512
521
  console.error(
513
522
  chalk2.red(
@@ -518,8 +527,8 @@ async function execute(agent2, messages, contextVariables, config) {
518
527
  console.dir(error, { depth: null });
519
528
  },
520
529
  output: agent2.output ? Output2.object({ schema: agent2.output }) : void 0,
521
- // onStepFinish: (step) => tagAgents(step, agent.handoff.name),
522
- onStepFinish: (step) => {
530
+ // onStepEnd: (step) => tagAgents(step, agent.handoff.name),
531
+ onStepEnd: (step) => {
523
532
  const toolCall = step.toolCalls.at(-1);
524
533
  if (toolCall) {
525
534
  console.log(
@@ -528,7 +537,7 @@ async function execute(agent2, messages, contextVariables, config) {
528
537
  }
529
538
  },
530
539
  prepareStep: prepareStep(agent2, agent2.model, contextVariables)
531
- // onFinish: (result) => {
540
+ // onEnd: (result) => {
532
541
  // (contextVariables as any).content = result.content;
533
542
  // },
534
543
  });
@@ -574,13 +583,13 @@ function swarm(agent2, messages, contextVariables, abortSignal) {
574
583
  stream2.toUIMessageStream({
575
584
  sendFinish: false,
576
585
  sendStart: true,
577
- onFinish: (event) => {
586
+ onEnd: (event) => {
578
587
  parts.push(...event.responseMessage.parts);
579
588
  }
580
589
  })
581
590
  );
582
591
  await stream2.consumeStream({ onError: console.error });
583
- await last(stream2.fullStream);
592
+ await last(stream2.stream);
584
593
  if (!agent2.prepareEnd) return;
585
594
  while (true) {
586
595
  const state = contextVariables;
@@ -609,13 +618,13 @@ function swarm(agent2, messages, contextVariables, abortSignal) {
609
618
  stream3.toUIMessageStream({
610
619
  sendFinish: false,
611
620
  sendStart: false,
612
- onFinish: (event) => {
621
+ onEnd: (event) => {
613
622
  parts.push(...event.responseMessage.parts);
614
623
  }
615
624
  })
616
625
  );
617
626
  await stream3.consumeStream({ onError: console.error });
618
- await last(stream3.fullStream);
627
+ await last(stream3.stream);
619
628
  }
620
629
  writer.write({ type: "finish" });
621
630
  }
@@ -626,7 +635,7 @@ async function prepareAgent(defaultModel, agent2, messages, contextVariables) {
626
635
  await agent2.prepareHandoff?.(messages);
627
636
  const stepModel = agent2.model ?? defaultModel;
628
637
  return {
629
- system: agent2.instructions(contextVariables),
638
+ instructions: agent2.instructions(contextVariables),
630
639
  activeTools: agent2.toolsNames,
631
640
  model: stepModel,
632
641
  messages,
@@ -640,6 +649,25 @@ function findAgent(agent2, agentName) {
640
649
  );
641
650
  }
642
651
 
652
+ // packages/agent/src/lib/tool-output.ts
653
+ function withHostOnlyToolMetadata(tools) {
654
+ const wrapped = {};
655
+ for (const [name, tool2] of Object.entries(tools)) {
656
+ wrapped[name] = tool2.toModelOutput !== void 0 ? tool2 : {
657
+ ...tool2,
658
+ toModelOutput: ({ output }) => defaultToolModelOutput(output)
659
+ };
660
+ }
661
+ return wrapped;
662
+ }
663
+ function defaultToolModelOutput(output) {
664
+ if (typeof output === "object" && output !== null && !Array.isArray(output) && Object.hasOwn(output, "meta")) {
665
+ const { meta: _meta, ...visible } = output;
666
+ return { type: "json", value: visible };
667
+ }
668
+ return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: output ?? null };
669
+ }
670
+
643
671
  // packages/agent/src/lib/agent.ts
644
672
  function agent(config) {
645
673
  return new Agent(config);
@@ -671,7 +699,7 @@ var Agent = class _Agent {
671
699
  this.handoff = {
672
700
  name: this.internalName,
673
701
  instructions: config.prompt,
674
- tools: config.tools ?? {},
702
+ tools: withHostOnlyToolMetadata(config.tools ?? {}),
675
703
  handoffDescription: config.handoffDescription
676
704
  };
677
705
  this.handoffToolName = `transfer_to_${this.internalName}`;
@@ -756,9 +784,10 @@ var Agent = class _Agent {
756
784
  }),
757
785
  execute: async ({ input: input2, output }, options) => {
758
786
  try {
787
+ const contextVariables = options.context;
759
788
  const result = await generateText3({
760
789
  model: this.model,
761
- system: this.#prepareInstructions(),
790
+ instructions: this.#prepareInstructions(),
762
791
  prompt: `
763
792
  <Input>
764
793
  ${input2}
@@ -770,13 +799,17 @@ ${output}
770
799
  temperature: 0,
771
800
  tools: this.handoff.tools,
772
801
  abortSignal: options.abortSignal,
773
- stopWhen: stepCountIs2(25),
774
- experimental_repairToolCall: this.repairToolCall(
775
- options.abortSignal
802
+ stopWhen: isStepCount2(25),
803
+ repairToolCall: this.repairToolCall(options.abortSignal),
804
+ runtimeContext: contextVariables,
805
+ toolsContext: Object.fromEntries(
806
+ Object.keys(this.handoff.tools).map((toolName) => [
807
+ toolName,
808
+ contextVariables
809
+ ])
776
810
  ),
777
- experimental_context: options.experimental_context,
778
811
  output: this.output ? Output3.object({ schema: this.output }) : void 0,
779
- onStepFinish: (step) => {
812
+ onStepEnd: (step) => {
780
813
  const toolCall = step.toolCalls.at(-1);
781
814
  if (toolCall && this.logging) {
782
815
  console.log(
@@ -784,21 +817,21 @@ ${output}
784
817
  );
785
818
  }
786
819
  },
787
- prepareStep: prepareStep(
788
- this,
789
- this.model,
790
- options.experimental_context
791
- )
820
+ prepareStep: prepareStep(this, this.model, contextVariables)
792
821
  });
793
822
  if (props?.outputExtractor) {
794
823
  return await props.outputExtractor(result);
795
824
  }
796
- return result.steps.map((it) => it.toolResults).flat();
825
+ return result.text;
797
826
  } catch (error) {
827
+ if (error instanceof Error && error.name === "AbortError") {
828
+ throw error;
829
+ }
798
830
  console.error(error);
831
+ const details = error instanceof Error ? error.message : JSON.stringify(error);
799
832
  return `An error thrown from a tool call.
800
833
  <ErrorDetails>
801
- ${JSON.stringify(error)}
834
+ ${details}
802
835
  </ErrorDetails>`;
803
836
  }
804
837
  }
@@ -1065,7 +1098,7 @@ function pipe(state, ...processes) {
1065
1098
  result.toUIMessageStream({
1066
1099
  generateMessageId: generateId3,
1067
1100
  originalMessages: state.messages,
1068
- onFinish: async ({
1101
+ onEnd: async ({
1069
1102
  responseMessage
1070
1103
  }) => {
1071
1104
  state.messages.push(responseMessage);
@@ -1141,6 +1174,8 @@ export {
1141
1174
  thirdPersonPrompt,
1142
1175
  toOutput,
1143
1176
  toState,
1144
- user
1177
+ toToolsContext,
1178
+ user,
1179
+ withHostOnlyToolMetadata
1145
1180
  };
1146
1181
  //# sourceMappingURL=index.js.map