@deepagents/agent 4.3.0 → 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 +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +73 -42
- package/dist/index.js.map +4 -4
- package/dist/lib/agent.d.ts +455 -13
- package/dist/lib/agent.d.ts.map +1 -1
- package/dist/lib/models.d.ts +2 -2
- package/dist/lib/repair.d.ts +2 -2
- package/dist/lib/stream_utils.d.ts +7 -4
- package/dist/lib/stream_utils.d.ts.map +1 -1
- package/dist/lib/swarm.d.ts +2 -2
- package/dist/lib/swarm.d.ts.map +1 -1
- package/dist/lib/tool-output.d.ts +11 -0
- package/dist/lib/tool-output.d.ts.map +1 -0
- package/package.json +8 -7
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,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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
472
|
-
stopWhen:
|
|
473
|
-
tools
|
|
477
|
+
repairToolCall: agent2.repairToolCall(config?.abortSignal),
|
|
478
|
+
stopWhen: isStepCount(25),
|
|
479
|
+
tools,
|
|
474
480
|
activeTools: agent2.toolsNames,
|
|
475
|
-
|
|
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
|
-
//
|
|
479
|
-
|
|
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
|
-
//
|
|
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
|
-
|
|
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:
|
|
512
|
+
stopWhen: isStepCount(25),
|
|
505
513
|
experimental_transform: config?.transform ?? smoothStream(),
|
|
506
|
-
tools
|
|
514
|
+
tools,
|
|
507
515
|
activeTools: agent2.toolsNames,
|
|
508
|
-
|
|
516
|
+
runtimeContext: contextVariables,
|
|
517
|
+
toolsContext: toToolsContext(tools, contextVariables),
|
|
509
518
|
toolChoice: agent2.toolChoice,
|
|
510
|
-
|
|
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
|
-
//
|
|
522
|
-
|
|
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
|
-
//
|
|
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
|
-
|
|
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.
|
|
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
|
-
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
774
|
-
|
|
775
|
-
|
|
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
|
-
|
|
812
|
+
onStepEnd: (step) => {
|
|
780
813
|
const toolCall = step.toolCalls.at(-1);
|
|
781
814
|
if (toolCall && this.logging) {
|
|
782
815
|
console.log(
|
|
@@ -784,11 +817,7 @@ ${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);
|
|
@@ -1069,7 +1098,7 @@ function pipe(state, ...processes) {
|
|
|
1069
1098
|
result.toUIMessageStream({
|
|
1070
1099
|
generateMessageId: generateId3,
|
|
1071
1100
|
originalMessages: state.messages,
|
|
1072
|
-
|
|
1101
|
+
onEnd: async ({
|
|
1073
1102
|
responseMessage
|
|
1074
1103
|
}) => {
|
|
1075
1104
|
state.messages.push(responseMessage);
|
|
@@ -1145,6 +1174,8 @@ export {
|
|
|
1145
1174
|
thirdPersonPrompt,
|
|
1146
1175
|
toOutput,
|
|
1147
1176
|
toState,
|
|
1148
|
-
|
|
1177
|
+
toToolsContext,
|
|
1178
|
+
user,
|
|
1179
|
+
withHostOnlyToolMetadata
|
|
1149
1180
|
};
|
|
1150
1181
|
//# sourceMappingURL=index.js.map
|