@voltagent/core 0.1.44 → 0.1.46
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 +17 -12
- package/dist/index.js +55 -48
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +55 -48
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { Span } from '@opentelemetry/api';
|
|
3
|
-
import { Simplify, LiteralUnion } from 'type-fest';
|
|
3
|
+
import { Simplify, LiteralUnion, MergeDeep } from 'type-fest';
|
|
4
4
|
import { Context } from 'hono';
|
|
5
5
|
import { SpanExporter } from '@opentelemetry/sdk-trace-base';
|
|
6
6
|
import { ClientCapabilities } from '@modelcontextprotocol/sdk/types.js';
|
|
@@ -790,8 +790,8 @@ type InferStreamEventBase<TStreamPart extends StreamPart> = {
|
|
|
790
790
|
type InferStreamEventData<TStreamPart extends StreamPart> = Simplify<Omit<TStreamPart, "type" | "subAgentId" | "subAgentName">>;
|
|
791
791
|
|
|
792
792
|
interface StreamEventForwarderOptions {
|
|
793
|
-
forwarder
|
|
794
|
-
|
|
793
|
+
forwarder: (event: StreamEvent) => Promise<void>;
|
|
794
|
+
types: Array<LiteralUnion<StreamEventType, string>> | ReadonlyArray<LiteralUnion<StreamEventType, string>>;
|
|
795
795
|
addSubAgentPrefix?: boolean;
|
|
796
796
|
}
|
|
797
797
|
/**
|
|
@@ -799,13 +799,13 @@ interface StreamEventForwarderOptions {
|
|
|
799
799
|
* @param event - The SubAgent event to forward
|
|
800
800
|
* @param options - Configuration options for forwarding
|
|
801
801
|
*/
|
|
802
|
-
declare function streamEventForwarder(event: StreamEvent, options
|
|
802
|
+
declare function streamEventForwarder(event: StreamEvent, options: StreamEventForwarderOptions): Promise<void>;
|
|
803
803
|
/**
|
|
804
804
|
* Creates a configured streamEventForwarder function
|
|
805
805
|
* @param options - Configuration options
|
|
806
806
|
* @returns A configured forwarder function
|
|
807
807
|
*/
|
|
808
|
-
declare function createStreamEventForwarder(options
|
|
808
|
+
declare function createStreamEventForwarder(options: StreamEventForwarderOptions): (event: StreamEvent) => Promise<void>;
|
|
809
809
|
|
|
810
810
|
/**
|
|
811
811
|
* Options object for dynamic value resolution
|
|
@@ -1051,15 +1051,15 @@ type AgentHandoffOptions = {
|
|
|
1051
1051
|
* Parent history entry ID
|
|
1052
1052
|
*/
|
|
1053
1053
|
parentHistoryEntryId?: string;
|
|
1054
|
-
/**
|
|
1055
|
-
* Optional user-defined context to be passed from the supervisor agent
|
|
1056
|
-
*/
|
|
1057
|
-
userContext?: Map<string | symbol, unknown>;
|
|
1058
1054
|
/**
|
|
1059
1055
|
* Optional real-time event forwarder function
|
|
1060
1056
|
* Used to forward SubAgent events to parent stream in real-time
|
|
1061
1057
|
*/
|
|
1062
1058
|
forwardEvent?: (event: StreamEvent) => Promise<void>;
|
|
1059
|
+
/**
|
|
1060
|
+
* Parent's operation context to merge SubAgent steps into
|
|
1061
|
+
*/
|
|
1062
|
+
parentOperationContext?: OperationContext;
|
|
1063
1063
|
};
|
|
1064
1064
|
/**
|
|
1065
1065
|
* Result of a handoff to another agent
|
|
@@ -2873,12 +2873,17 @@ declare class SubAgentManager {
|
|
|
2873
2873
|
*/
|
|
2874
2874
|
handoffToMultiple(options: Omit<AgentHandoffOptions, "targetAgent"> & {
|
|
2875
2875
|
targetAgents: Agent<any>[];
|
|
2876
|
-
userContext?: Map<string | symbol, unknown>;
|
|
2877
2876
|
}): Promise<AgentHandoffResult[]>;
|
|
2878
2877
|
/**
|
|
2879
|
-
*
|
|
2878
|
+
* Creates a tool that allows the supervisor agent to delegate a
|
|
2879
|
+
* task to one or more specialized agents
|
|
2880
2880
|
*/
|
|
2881
|
-
createDelegateTool(options
|
|
2881
|
+
createDelegateTool(options: MergeDeep<{
|
|
2882
|
+
sourceAgent: Agent<any>;
|
|
2883
|
+
currentHistoryEntryId?: string;
|
|
2884
|
+
operationContext?: OperationContext;
|
|
2885
|
+
forwardEvent?: (event: StreamEvent) => Promise<void>;
|
|
2886
|
+
}, Record<string, any>>): BaseTool;
|
|
2882
2887
|
/**
|
|
2883
2888
|
* Get sub-agent details for API exposure
|
|
2884
2889
|
*/
|
package/dist/index.js
CHANGED
|
@@ -3199,8 +3199,8 @@ __name(serializeValueForDebug, "serializeValueForDebug");
|
|
|
3199
3199
|
|
|
3200
3200
|
// src/utils/streams/stream-event-forwarder.ts
|
|
3201
3201
|
var import_dev5 = require("@voltagent/internal/dev");
|
|
3202
|
-
async function streamEventForwarder(event, options
|
|
3203
|
-
const { forwarder,
|
|
3202
|
+
async function streamEventForwarder(event, options) {
|
|
3203
|
+
const { forwarder, types } = options;
|
|
3204
3204
|
try {
|
|
3205
3205
|
if (!event || typeof event !== "object") {
|
|
3206
3206
|
import_dev5.devLogger.warn("[StreamEventForwarder] Invalid event structure:", event);
|
|
@@ -3214,7 +3214,7 @@ async function streamEventForwarder(event, options = {}) {
|
|
|
3214
3214
|
});
|
|
3215
3215
|
return;
|
|
3216
3216
|
}
|
|
3217
|
-
if (
|
|
3217
|
+
if (!types.includes(event.type)) {
|
|
3218
3218
|
import_dev5.devLogger.info(
|
|
3219
3219
|
"[StreamEventForwarder] Filtered out",
|
|
3220
3220
|
event.type,
|
|
@@ -3223,9 +3223,6 @@ async function streamEventForwarder(event, options = {}) {
|
|
|
3223
3223
|
);
|
|
3224
3224
|
return;
|
|
3225
3225
|
}
|
|
3226
|
-
if (!forwarder) {
|
|
3227
|
-
return;
|
|
3228
|
-
}
|
|
3229
3226
|
await forwarder(formatEvent(event, options));
|
|
3230
3227
|
import_dev5.devLogger.info(
|
|
3231
3228
|
"[StreamEventForwarder] Forwarded",
|
|
@@ -3238,7 +3235,7 @@ async function streamEventForwarder(event, options = {}) {
|
|
|
3238
3235
|
}
|
|
3239
3236
|
}
|
|
3240
3237
|
__name(streamEventForwarder, "streamEventForwarder");
|
|
3241
|
-
function createStreamEventForwarder(options
|
|
3238
|
+
function createStreamEventForwarder(options) {
|
|
3242
3239
|
return (event) => streamEventForwarder(event, options);
|
|
3243
3240
|
}
|
|
3244
3241
|
__name(createStreamEventForwarder, "createStreamEventForwarder");
|
|
@@ -6898,15 +6895,15 @@ ${agentsMemory || "No previous agent interactions available."}
|
|
|
6898
6895
|
async handoffTask(options) {
|
|
6899
6896
|
const {
|
|
6900
6897
|
task,
|
|
6901
|
-
targetAgent,
|
|
6902
|
-
context = {},
|
|
6903
6898
|
conversationId,
|
|
6904
6899
|
userId,
|
|
6905
|
-
sourceAgent,
|
|
6906
6900
|
parentAgentId,
|
|
6907
6901
|
parentHistoryEntryId,
|
|
6908
|
-
|
|
6902
|
+
parentOperationContext
|
|
6909
6903
|
} = options;
|
|
6904
|
+
const context = options.context;
|
|
6905
|
+
const sourceAgent = options.sourceAgent;
|
|
6906
|
+
const targetAgent = options.targetAgent;
|
|
6910
6907
|
const handoffConversationId = conversationId || crypto.randomUUID();
|
|
6911
6908
|
try {
|
|
6912
6909
|
if (sourceAgent && targetAgent.hooks) {
|
|
@@ -6922,15 +6919,17 @@ ${task}
|
|
|
6922
6919
|
Context: ${JSON.stringify(context, null, 2)}`;
|
|
6923
6920
|
}
|
|
6924
6921
|
const taskMessage = {
|
|
6925
|
-
role: "
|
|
6922
|
+
role: "user",
|
|
6926
6923
|
content: taskContent
|
|
6927
6924
|
};
|
|
6928
6925
|
const streamResponse = await targetAgent.streamText([...sharedContext, taskMessage], {
|
|
6929
6926
|
conversationId: handoffConversationId,
|
|
6930
6927
|
userId,
|
|
6928
|
+
// TODO: Fix the types here
|
|
6929
|
+
// @ts-expect-error - bad types
|
|
6931
6930
|
parentAgentId: sourceAgent?.id || parentAgentId,
|
|
6932
6931
|
parentHistoryEntryId,
|
|
6933
|
-
|
|
6932
|
+
parentOperationContext
|
|
6934
6933
|
});
|
|
6935
6934
|
let finalText = "";
|
|
6936
6935
|
if (streamResponse.fullStream) {
|
|
@@ -7021,8 +7020,8 @@ Context: ${JSON.stringify(context, null, 2)}`;
|
|
|
7021
7020
|
const eventData = {
|
|
7022
7021
|
type: "error",
|
|
7023
7022
|
data: {
|
|
7023
|
+
// @ts-expect-error - fix bad type
|
|
7024
7024
|
error: part.error?.message || "Stream error occurred",
|
|
7025
|
-
// @ts-expect-error - code is not part of the StreamEventError type currently
|
|
7026
7025
|
code: "STREAM_ERROR"
|
|
7027
7026
|
},
|
|
7028
7027
|
timestamp,
|
|
@@ -7073,7 +7072,7 @@ Context: ${JSON.stringify(context, null, 2)}`;
|
|
|
7073
7072
|
conversationId,
|
|
7074
7073
|
parentAgentId,
|
|
7075
7074
|
parentHistoryEntryId,
|
|
7076
|
-
|
|
7075
|
+
parentOperationContext,
|
|
7077
7076
|
...restOptions
|
|
7078
7077
|
} = options;
|
|
7079
7078
|
const handoffConversationId = conversationId || crypto.randomUUID();
|
|
@@ -7086,7 +7085,7 @@ Context: ${JSON.stringify(context, null, 2)}`;
|
|
|
7086
7085
|
conversationId: handoffConversationId,
|
|
7087
7086
|
parentAgentId,
|
|
7088
7087
|
parentHistoryEntryId,
|
|
7089
|
-
|
|
7088
|
+
parentOperationContext
|
|
7090
7089
|
});
|
|
7091
7090
|
} catch (error) {
|
|
7092
7091
|
import_dev13.devLogger.error(`Error in handoffToMultiple for agent ${agent.name}:`, error);
|
|
@@ -7109,9 +7108,11 @@ Context: ${JSON.stringify(context, null, 2)}`;
|
|
|
7109
7108
|
return results;
|
|
7110
7109
|
}
|
|
7111
7110
|
/**
|
|
7112
|
-
*
|
|
7111
|
+
* Creates a tool that allows the supervisor agent to delegate a
|
|
7112
|
+
* task to one or more specialized agents
|
|
7113
7113
|
*/
|
|
7114
|
-
createDelegateTool(options
|
|
7114
|
+
createDelegateTool(options) {
|
|
7115
|
+
const { sourceAgent, forwardEvent, operationContext, currentHistoryEntryId, ...restOptions } = options;
|
|
7115
7116
|
return createTool({
|
|
7116
7117
|
id: "delegate_task",
|
|
7117
7118
|
name: "delegate_task",
|
|
@@ -7143,11 +7144,6 @@ Context: ${JSON.stringify(context, null, 2)}`;
|
|
|
7143
7144
|
`No valid target agents found. Available agents: ${this.subAgents.map((a) => a.name).join(", ")}`
|
|
7144
7145
|
);
|
|
7145
7146
|
}
|
|
7146
|
-
const sourceAgent = options.sourceAgent;
|
|
7147
|
-
const operationContext = options.operationContext;
|
|
7148
|
-
const supervisorUserContext = operationContext?.userContext;
|
|
7149
|
-
const currentHistoryEntryId = options.currentHistoryEntryId;
|
|
7150
|
-
const forwardEvent = options.forwardEvent;
|
|
7151
7147
|
const results = await this.handoffToMultiple({
|
|
7152
7148
|
task,
|
|
7153
7149
|
targetAgents: agents,
|
|
@@ -7155,12 +7151,13 @@ Context: ${JSON.stringify(context, null, 2)}`;
|
|
|
7155
7151
|
sourceAgent,
|
|
7156
7152
|
// Pass parent context for event propagation
|
|
7157
7153
|
parentAgentId: sourceAgent?.id,
|
|
7154
|
+
// Get current history entry ID for parent context
|
|
7155
|
+
// This is passed from the Agent class via options when the tool is called
|
|
7158
7156
|
parentHistoryEntryId: currentHistoryEntryId,
|
|
7159
|
-
|
|
7160
|
-
userContext: supervisorUserContext,
|
|
7157
|
+
parentOperationContext: operationContext,
|
|
7161
7158
|
// Pass the real-time event forwarder
|
|
7162
7159
|
forwardEvent,
|
|
7163
|
-
...
|
|
7160
|
+
...restOptions
|
|
7164
7161
|
});
|
|
7165
7162
|
const structuredResults = results.map((result, index) => {
|
|
7166
7163
|
const status = result.status || "success";
|
|
@@ -7322,36 +7319,36 @@ var Agent = class {
|
|
|
7322
7319
|
/**
|
|
7323
7320
|
* Resolve dynamic instructions based on user context
|
|
7324
7321
|
*/
|
|
7325
|
-
|
|
7322
|
+
async resolveInstructions(options) {
|
|
7326
7323
|
if (!this.dynamicInstructions)
|
|
7327
7324
|
return this.instructions;
|
|
7328
7325
|
if (typeof this.dynamicInstructions === "function") {
|
|
7329
7326
|
return await this.dynamicInstructions(options);
|
|
7330
7327
|
}
|
|
7331
7328
|
return this.dynamicInstructions;
|
|
7332
|
-
}
|
|
7329
|
+
}
|
|
7333
7330
|
/**
|
|
7334
7331
|
* Resolve dynamic model based on user context
|
|
7335
7332
|
*/
|
|
7336
|
-
|
|
7333
|
+
async resolveModel(options) {
|
|
7337
7334
|
if (!this.dynamicModel)
|
|
7338
7335
|
return this.model;
|
|
7339
7336
|
if (typeof this.dynamicModel === "function") {
|
|
7340
7337
|
return await this.dynamicModel(options);
|
|
7341
7338
|
}
|
|
7342
7339
|
return this.dynamicModel;
|
|
7343
|
-
}
|
|
7340
|
+
}
|
|
7344
7341
|
/**
|
|
7345
7342
|
* Resolve dynamic tools based on user context
|
|
7346
7343
|
*/
|
|
7347
|
-
|
|
7344
|
+
async resolveTools(options) {
|
|
7348
7345
|
if (!this.dynamicTools)
|
|
7349
7346
|
return [];
|
|
7350
7347
|
if (typeof this.dynamicTools === "function") {
|
|
7351
7348
|
return await this.dynamicTools(options);
|
|
7352
7349
|
}
|
|
7353
7350
|
return this.dynamicTools;
|
|
7354
|
-
}
|
|
7351
|
+
}
|
|
7355
7352
|
/**
|
|
7356
7353
|
* Get the system message for the agent
|
|
7357
7354
|
*/
|
|
@@ -7602,12 +7599,13 @@ ${context}`;
|
|
|
7602
7599
|
import_dev14.devLogger.info(
|
|
7603
7600
|
`[Agent ${this.id}] Received SubAgent event: ${event.type} from ${event.subAgentName}`
|
|
7604
7601
|
);
|
|
7605
|
-
|
|
7606
|
-
|
|
7607
|
-
|
|
7608
|
-
|
|
7609
|
-
|
|
7610
|
-
|
|
7602
|
+
if (internalStreamForwarder) {
|
|
7603
|
+
await streamEventForwarder(event, {
|
|
7604
|
+
forwarder: internalStreamForwarder,
|
|
7605
|
+
types: ["tool-call", "tool-result"],
|
|
7606
|
+
addSubAgentPrefix: true
|
|
7607
|
+
});
|
|
7608
|
+
}
|
|
7611
7609
|
}, "forwardEvent");
|
|
7612
7610
|
const delegateTool = this.subAgentManager.createDelegateTool({
|
|
7613
7611
|
sourceAgent: this,
|
|
@@ -7663,13 +7661,14 @@ ${context}`;
|
|
|
7663
7661
|
});
|
|
7664
7662
|
const opContext = {
|
|
7665
7663
|
operationId: historyEntry.id,
|
|
7666
|
-
userContext: options.userContext ?? /* @__PURE__ */ new Map(),
|
|
7664
|
+
userContext: (options.parentOperationContext?.userContext || options.userContext) ?? /* @__PURE__ */ new Map(),
|
|
7667
7665
|
historyEntry,
|
|
7668
7666
|
isActive: true,
|
|
7669
7667
|
parentAgentId: options.parentAgentId,
|
|
7670
7668
|
parentHistoryEntryId: options.parentHistoryEntryId,
|
|
7671
7669
|
otelSpan,
|
|
7672
|
-
conversationSteps
|
|
7670
|
+
// Use parent's conversationSteps if available (for SubAgents), otherwise create new array
|
|
7671
|
+
conversationSteps: options.parentOperationContext?.conversationSteps || []
|
|
7673
7672
|
};
|
|
7674
7673
|
return opContext;
|
|
7675
7674
|
}
|
|
@@ -7733,7 +7732,7 @@ ${context}`;
|
|
|
7733
7732
|
/**
|
|
7734
7733
|
* Fix delete operator usage for better performance
|
|
7735
7734
|
*/
|
|
7736
|
-
addToolEvent
|
|
7735
|
+
addToolEvent(context, toolName, status, data = {}) {
|
|
7737
7736
|
if (!context.toolSpans) {
|
|
7738
7737
|
context.toolSpans = /* @__PURE__ */ new Map();
|
|
7739
7738
|
}
|
|
@@ -7753,11 +7752,11 @@ ${context}`;
|
|
|
7753
7752
|
context.toolSpans.set(toolCallId, toolSpan);
|
|
7754
7753
|
}
|
|
7755
7754
|
}
|
|
7756
|
-
}
|
|
7755
|
+
}
|
|
7757
7756
|
/**
|
|
7758
7757
|
* Agent event creator (update)
|
|
7759
7758
|
*/
|
|
7760
|
-
addAgentEvent
|
|
7759
|
+
addAgentEvent(context, eventName, status, data = {}) {
|
|
7761
7760
|
const otelSpan = context.otelSpan;
|
|
7762
7761
|
if (otelSpan) {
|
|
7763
7762
|
endOperationSpan({
|
|
@@ -7770,7 +7769,7 @@ ${context}`;
|
|
|
7770
7769
|
`OpenTelemetry span not found in OperationContext for agent event ${eventName} (Operation ID: ${context.operationId})`
|
|
7771
7770
|
);
|
|
7772
7771
|
}
|
|
7773
|
-
}
|
|
7772
|
+
}
|
|
7774
7773
|
/**
|
|
7775
7774
|
* Helper method to enrich and end an OpenTelemetry span associated with a tool call.
|
|
7776
7775
|
*/
|
|
@@ -7850,6 +7849,7 @@ ${context}`;
|
|
|
7850
7849
|
conversationId: initialConversationId,
|
|
7851
7850
|
parentAgentId,
|
|
7852
7851
|
parentHistoryEntryId,
|
|
7852
|
+
parentOperationContext,
|
|
7853
7853
|
contextLimit = 10,
|
|
7854
7854
|
userContext
|
|
7855
7855
|
} = internalOptions;
|
|
@@ -7859,7 +7859,8 @@ ${context}`;
|
|
|
7859
7859
|
operationName: "generateText",
|
|
7860
7860
|
userContext,
|
|
7861
7861
|
userId,
|
|
7862
|
-
conversationId: initialConversationId
|
|
7862
|
+
conversationId: initialConversationId,
|
|
7863
|
+
parentOperationContext
|
|
7863
7864
|
});
|
|
7864
7865
|
const { messages: contextMessages, conversationId: finalConversationId } = await this.memoryManager.prepareConversationContext(
|
|
7865
7866
|
operationContext,
|
|
@@ -8194,6 +8195,7 @@ ${context}`;
|
|
|
8194
8195
|
conversationId: initialConversationId,
|
|
8195
8196
|
parentAgentId,
|
|
8196
8197
|
parentHistoryEntryId,
|
|
8198
|
+
parentOperationContext,
|
|
8197
8199
|
contextLimit = 10,
|
|
8198
8200
|
userContext
|
|
8199
8201
|
} = internalOptions;
|
|
@@ -8203,7 +8205,8 @@ ${context}`;
|
|
|
8203
8205
|
operationName: "streamText",
|
|
8204
8206
|
userContext,
|
|
8205
8207
|
userId,
|
|
8206
|
-
conversationId: initialConversationId
|
|
8208
|
+
conversationId: initialConversationId,
|
|
8209
|
+
parentOperationContext
|
|
8207
8210
|
});
|
|
8208
8211
|
const { messages: contextMessages, conversationId: finalConversationId } = await this.memoryManager.prepareConversationContext(
|
|
8209
8212
|
operationContext,
|
|
@@ -8637,6 +8640,7 @@ ${context}`;
|
|
|
8637
8640
|
conversationId: initialConversationId,
|
|
8638
8641
|
parentAgentId,
|
|
8639
8642
|
parentHistoryEntryId,
|
|
8643
|
+
parentOperationContext,
|
|
8640
8644
|
contextLimit = 10,
|
|
8641
8645
|
userContext
|
|
8642
8646
|
} = internalOptions;
|
|
@@ -8646,7 +8650,8 @@ ${context}`;
|
|
|
8646
8650
|
operationName: "generateObject",
|
|
8647
8651
|
userContext,
|
|
8648
8652
|
userId,
|
|
8649
|
-
conversationId: initialConversationId
|
|
8653
|
+
conversationId: initialConversationId,
|
|
8654
|
+
parentOperationContext
|
|
8650
8655
|
});
|
|
8651
8656
|
const { messages: contextMessages, conversationId: finalConversationId } = await this.memoryManager.prepareConversationContext(
|
|
8652
8657
|
operationContext,
|
|
@@ -8866,6 +8871,7 @@ ${context}`;
|
|
|
8866
8871
|
conversationId: initialConversationId,
|
|
8867
8872
|
parentAgentId,
|
|
8868
8873
|
parentHistoryEntryId,
|
|
8874
|
+
parentOperationContext,
|
|
8869
8875
|
provider,
|
|
8870
8876
|
contextLimit = 10,
|
|
8871
8877
|
userContext
|
|
@@ -8876,7 +8882,8 @@ ${context}`;
|
|
|
8876
8882
|
operationName: "streamObject",
|
|
8877
8883
|
userContext,
|
|
8878
8884
|
userId,
|
|
8879
|
-
conversationId: initialConversationId
|
|
8885
|
+
conversationId: initialConversationId,
|
|
8886
|
+
parentOperationContext
|
|
8880
8887
|
});
|
|
8881
8888
|
const { messages: contextMessages, conversationId: finalConversationId } = await this.memoryManager.prepareConversationContext(
|
|
8882
8889
|
operationContext,
|