@voltagent/core 0.1.3 → 0.1.4

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
@@ -1148,22 +1148,30 @@ type AgentHandoffOptions = {
1148
1148
  parentHistoryEntryId?: string;
1149
1149
  };
1150
1150
  /**
1151
- * Agent handoff result
1151
+ * Result of a handoff to another agent
1152
1152
  */
1153
- type AgentHandoffResult = {
1153
+ interface AgentHandoffResult {
1154
1154
  /**
1155
- * The result of the handoff operation
1155
+ * Result text from the agent
1156
1156
  */
1157
1157
  result: string;
1158
1158
  /**
1159
- * The conversation ID of the handoff
1159
+ * Conversation ID used for the interaction
1160
1160
  */
1161
1161
  conversationId: string;
1162
1162
  /**
1163
- * The messages exchanged during the handoff
1163
+ * Messages exchanged during the handoff
1164
1164
  */
1165
1165
  messages: BaseMessage[];
1166
- };
1166
+ /**
1167
+ * Status of the handoff operation
1168
+ */
1169
+ status?: "success" | "error";
1170
+ /**
1171
+ * Error information if the handoff failed
1172
+ */
1173
+ error?: Error | string;
1174
+ }
1167
1175
  /**
1168
1176
  * Operation context to isolate state for concurrent operations
1169
1177
  * Prevents race conditions when the same agent instance is used concurrently
@@ -1553,8 +1561,9 @@ declare class SubAgentManager {
1553
1561
  /**
1554
1562
  * Generate enhanced system message for supervisor role
1555
1563
  * @param baseDescription - The base description of the agent
1564
+ * @param agentsMemory - Optional string containing formatted memory from previous agent interactions
1556
1565
  */
1557
- generateSupervisorSystemMessage(baseDescription: string): string;
1566
+ generateSupervisorSystemMessage(baseDescription: string, agentsMemory?: string): string;
1558
1567
  /**
1559
1568
  * Check if the agent has sub-agents
1560
1569
  */
@@ -1841,10 +1850,16 @@ declare class Agent<TProvider extends {
1841
1850
  /**
1842
1851
  * Get the system message for the agent
1843
1852
  */
1844
- protected getSystemMessage({ input, historyEntryId, }: {
1853
+ protected getSystemMessage({ input, historyEntryId, contextMessages, }: {
1845
1854
  input?: string | BaseMessage[];
1846
1855
  historyEntryId: string;
1856
+ contextMessages: BaseMessage[];
1847
1857
  }): Promise<BaseMessage>;
1858
+ /**
1859
+ * Prepare agents memory for the supervisor system message
1860
+ * This fetches and formats recent interactions with sub-agents
1861
+ */
1862
+ private prepareAgentsMemory;
1848
1863
  /**
1849
1864
  * Add input to messages array based on type
1850
1865
  */
@@ -1911,7 +1926,7 @@ declare class Agent<TProvider extends {
1911
1926
  */
1912
1927
  private createStandardTimelineEvent;
1913
1928
  /**
1914
- * Tool event creator
1929
+ * Fix delete operator usage for better performance
1915
1930
  */
1916
1931
  private addToolEvent;
1917
1932
  /**
@@ -2039,7 +2054,7 @@ interface ToolCall {
2039
2054
  declare function zodSchemaToJsonUI(schema: any): any;
2040
2055
 
2041
2056
  /**
2042
- * Ajanlar, araçlar ve diğer bileşenler için node tipleri
2057
+ * Node types for agents, tools, and other components
2043
2058
  */
2044
2059
  declare enum NodeType {
2045
2060
  AGENT = "agent",
@@ -2051,17 +2066,17 @@ declare enum NodeType {
2051
2066
  RETRIEVER = "retriever"
2052
2067
  }
2053
2068
  /**
2054
- * Standart node ID oluşturma fonksiyonu
2055
- * @param type Node tipi
2056
- * @param name Ana tanımlayıcı (tool adı, agent adı vb.)
2057
- * @param ownerId Sahibinin ID'si (isteğe bağlı)
2058
- * @returns Standart formatlanmış node ID
2069
+ * Standard node ID creation function
2070
+ * @param type Node type
2071
+ * @param name Main identifier (tool name, agent name, etc.)
2072
+ * @param ownerId Owner ID (optional)
2073
+ * @returns Standard formatted node ID
2059
2074
  */
2060
2075
  declare const createNodeId: (type: NodeType, name: string, ownerId?: string) => string;
2061
2076
  /**
2062
- * NodeID'den node tipini çıkaran fonksiyon
2077
+ * Function to extract node type from NodeID
2063
2078
  * @param nodeId Node ID
2064
- * @returns NodeType veya null (tip bulunamazsa)
2079
+ * @returns NodeType or null (if type cannot be found)
2065
2080
  */
2066
2081
  declare const getNodeTypeFromNodeId: (nodeId: string) => NodeType | null;
2067
2082
 
package/dist/index.js CHANGED
@@ -3716,17 +3716,24 @@ var SubAgentManager = class {
3716
3716
  /**
3717
3717
  * Generate enhanced system message for supervisor role
3718
3718
  * @param baseDescription - The base description of the agent
3719
+ * @param agentsMemory - Optional string containing formatted memory from previous agent interactions
3719
3720
  */
3720
- generateSupervisorSystemMessage(baseDescription) {
3721
+ generateSupervisorSystemMessage(baseDescription, agentsMemory = "") {
3721
3722
  if (this.subAgents.length === 0) {
3722
3723
  return baseDescription;
3723
3724
  }
3724
3725
  const subAgentList = this.subAgents.map((agent) => `- ${agent.name}: ${agent.description}`).join("\n");
3725
- return `You are a supervisor agent that coordinates between specialized agents:
3726
+ return `
3727
+ You are a supervisor agent that coordinates between specialized agents:
3726
3728
 
3729
+ <specialized_agents>
3727
3730
  ${subAgentList}
3731
+ </specialized_agents>
3732
+
3733
+ <instructions>
3734
+ ${baseDescription}
3735
+ </instructions>
3728
3736
 
3729
- When communicating with other agents, including the User, please follow these guidelines:
3730
3737
  <guidelines>
3731
3738
  - Provide a final answer to the User when you have a response from all agents.
3732
3739
  - Do not mention the name of any agent in your response.
@@ -3749,7 +3756,7 @@ When communicating with other agents, including the User, please follow these gu
3749
3756
  </guidelines>
3750
3757
 
3751
3758
  <agents_memory>
3752
- {{AGENTS_MEMORY}}
3759
+ ${agentsMemory || "No previous agent interactions available."}
3753
3760
  </agents_memory>
3754
3761
  `;
3755
3762
  }
@@ -3776,27 +3783,45 @@ When communicating with other agents, including the User, please follow these gu
3776
3783
  parentHistoryEntryId
3777
3784
  } = options;
3778
3785
  const handoffConversationId = conversationId || crypto.randomUUID();
3779
- if (sourceAgent && targetAgent.hooks) {
3780
- yield (_b = (_a = targetAgent.hooks).onHandoff) == null ? void 0 : _b.call(_a, targetAgent, sourceAgent);
3781
- }
3782
- const sharedContext = options.sharedContext || [];
3783
- const handoffMessage = {
3784
- role: "system",
3785
- content: `Task handed off from ${(sourceAgent == null ? void 0 : sourceAgent.name) || this.agentName} to ${targetAgent.name}:
3786
+ try {
3787
+ if (sourceAgent && targetAgent.hooks) {
3788
+ yield (_b = (_a = targetAgent.hooks).onHandoff) == null ? void 0 : _b.call(_a, targetAgent, sourceAgent);
3789
+ }
3790
+ const sharedContext = options.sharedContext || [];
3791
+ const handoffMessage = {
3792
+ role: "system",
3793
+ content: `Task handed off from ${(sourceAgent == null ? void 0 : sourceAgent.name) || this.agentName} to ${targetAgent.name}:
3786
3794
  ${task}
3787
3795
  Context: ${JSON.stringify(context)}`
3788
- };
3789
- const response = yield targetAgent.generateText([handoffMessage, ...sharedContext], {
3790
- conversationId: handoffConversationId,
3791
- userId,
3792
- parentAgentId: (sourceAgent == null ? void 0 : sourceAgent.id) || parentAgentId,
3793
- parentHistoryEntryId
3794
- });
3795
- return {
3796
- result: response.text,
3797
- conversationId: handoffConversationId,
3798
- messages: [handoffMessage, { role: "assistant", content: response.text }]
3799
- };
3796
+ };
3797
+ const response = yield targetAgent.generateText([handoffMessage, ...sharedContext], {
3798
+ conversationId: handoffConversationId,
3799
+ userId,
3800
+ parentAgentId: (sourceAgent == null ? void 0 : sourceAgent.id) || parentAgentId,
3801
+ parentHistoryEntryId
3802
+ });
3803
+ return {
3804
+ result: response.text,
3805
+ conversationId: handoffConversationId,
3806
+ messages: [handoffMessage, { role: "assistant", content: response.text }],
3807
+ status: "success"
3808
+ };
3809
+ } catch (error) {
3810
+ console.error(`Error in handoffTask to ${targetAgent.name}:`, error);
3811
+ const errorMessage = error instanceof Error ? error.message : String(error);
3812
+ return {
3813
+ result: `Error in delegating task to ${targetAgent.name}: ${errorMessage}`,
3814
+ conversationId: handoffConversationId,
3815
+ messages: [
3816
+ {
3817
+ role: "system",
3818
+ content: `Error occurred during task handoff: ${errorMessage}`
3819
+ }
3820
+ ],
3821
+ status: "error",
3822
+ error: error instanceof Error ? error : String(error)
3823
+ };
3824
+ }
3800
3825
  });
3801
3826
  }
3802
3827
  /**
@@ -3805,16 +3830,35 @@ Context: ${JSON.stringify(context)}`
3805
3830
  handoffToMultiple(options) {
3806
3831
  return __async(this, null, function* () {
3807
3832
  const _a = options, { targetAgents, conversationId, parentAgentId, parentHistoryEntryId } = _a, restOptions = __objRest(_a, ["targetAgents", "conversationId", "parentAgentId", "parentHistoryEntryId"]);
3808
- return Promise.all(
3809
- targetAgents.map(
3810
- (agent) => this.handoffTask(__spreadProps(__spreadValues({}, restOptions), {
3811
- targetAgent: agent,
3812
- conversationId,
3813
- parentAgentId,
3814
- parentHistoryEntryId
3815
- }))
3816
- )
3833
+ const handoffConversationId = conversationId || crypto.randomUUID();
3834
+ const results = yield Promise.all(
3835
+ targetAgents.map((agent) => __async(this, null, function* () {
3836
+ try {
3837
+ return yield this.handoffTask(__spreadProps(__spreadValues({}, restOptions), {
3838
+ targetAgent: agent,
3839
+ conversationId: handoffConversationId,
3840
+ parentAgentId,
3841
+ parentHistoryEntryId
3842
+ }));
3843
+ } catch (error) {
3844
+ console.error(`Error in handoffToMultiple for agent ${agent.name}:`, error);
3845
+ const errorMessage = error instanceof Error ? error.message : String(error);
3846
+ return {
3847
+ result: `Error in delegating task to ${agent.name}: ${errorMessage}`,
3848
+ conversationId: handoffConversationId,
3849
+ messages: [
3850
+ {
3851
+ role: "system",
3852
+ content: `Error occurred during task handoff: ${errorMessage}`
3853
+ }
3854
+ ],
3855
+ status: "error",
3856
+ error: error instanceof Error ? error : String(error)
3857
+ };
3858
+ }
3859
+ }))
3817
3860
  );
3861
+ return results;
3818
3862
  });
3819
3863
  }
3820
3864
  /**
@@ -3830,26 +3874,56 @@ Context: ${JSON.stringify(context)}`
3830
3874
  context: import_zod.z.record(import_zod.z.unknown()).optional().describe("Additional context for the task")
3831
3875
  }),
3832
3876
  execute: (_0) => __async(this, [_0], function* ({ task, targetAgents, context = {} }) {
3833
- const agents = targetAgents.map((name) => this.subAgents.find((a) => a.name === name)).filter((agent) => agent !== void 0);
3834
- if (agents.length === 0) {
3835
- throw new Error("No valid target agents found");
3877
+ try {
3878
+ if (!task || task.trim() === "") {
3879
+ throw new Error("Task cannot be empty");
3880
+ }
3881
+ if (!targetAgents || !Array.isArray(targetAgents) || targetAgents.length === 0) {
3882
+ throw new Error("At least one target agent must be specified");
3883
+ }
3884
+ const agents = targetAgents.map((name) => {
3885
+ const agent = this.subAgents.find((a) => a.name === name);
3886
+ if (!agent) {
3887
+ console.warn(
3888
+ `Agent "${name}" not found. Available agents: ${this.subAgents.map((a) => a.name).join(", ")}`
3889
+ );
3890
+ }
3891
+ return agent;
3892
+ }).filter((agent) => agent !== void 0);
3893
+ if (agents.length === 0) {
3894
+ throw new Error(
3895
+ `No valid target agents found. Available agents: ${this.subAgents.map((a) => a.name).join(", ")}`
3896
+ );
3897
+ }
3898
+ const sourceAgent = options.sourceAgent;
3899
+ const currentHistoryEntryId = options.currentHistoryEntryId;
3900
+ const results = yield this.handoffToMultiple(__spreadValues({
3901
+ task,
3902
+ targetAgents: agents,
3903
+ context,
3904
+ sourceAgent,
3905
+ // Pass parent context for event propagation
3906
+ parentAgentId: sourceAgent == null ? void 0 : sourceAgent.id,
3907
+ parentHistoryEntryId: currentHistoryEntryId
3908
+ }, options));
3909
+ return results.map((result, index) => {
3910
+ const status = result.status || "success";
3911
+ const errorInfo = status === "error" && result.error ? typeof result.error === "string" ? result.error : result.error.message : void 0;
3912
+ return {
3913
+ agentName: agents[index].name,
3914
+ response: result.result,
3915
+ conversationId: result.conversationId,
3916
+ status,
3917
+ error: errorInfo
3918
+ };
3919
+ });
3920
+ } catch (error) {
3921
+ console.error("Error in delegate_task tool execution:", error);
3922
+ return {
3923
+ error: `Failed to delegate task: ${error instanceof Error ? error.message : String(error)}`,
3924
+ status: "error"
3925
+ };
3836
3926
  }
3837
- const sourceAgent = options.sourceAgent;
3838
- const currentHistoryEntryId = options.currentHistoryEntryId;
3839
- const results = yield this.handoffToMultiple(__spreadValues({
3840
- task,
3841
- targetAgents: agents,
3842
- context,
3843
- sourceAgent,
3844
- // Pass parent context for event propagation
3845
- parentAgentId: sourceAgent == null ? void 0 : sourceAgent.id,
3846
- parentHistoryEntryId: currentHistoryEntryId
3847
- }, options));
3848
- return results.map((result, index) => ({
3849
- agentName: agents[index].name,
3850
- response: result.result,
3851
- conversationId: result.conversationId
3852
- }));
3853
3927
  })
3854
3928
  };
3855
3929
  }
@@ -3915,25 +3989,25 @@ var Agent = class {
3915
3989
  }
3916
3990
  }, "createStandardTimelineEvent");
3917
3991
  /**
3918
- * Tool event creator
3992
+ * Fix delete operator usage for better performance
3919
3993
  */
3920
3994
  this.addToolEvent = /* @__PURE__ */ __name((_0, _1, _2, _3, ..._4) => __async(this, [_0, _1, _2, _3, ..._4], function* (context, eventName, toolName, status, data = {}) {
3921
3995
  const toolNodeId = createNodeId("tool" /* TOOL */, toolName, this.id);
3922
3996
  const metadata = __spreadValues({
3923
3997
  toolName
3924
3998
  }, data.metadata || {});
3925
- if (data.toolId) {
3926
- metadata.toolId = data.toolId;
3927
- delete data.toolId;
3999
+ const { toolId, input, output, error, errorMessage } = data;
4000
+ if (toolId) {
4001
+ metadata.toolId = toolId;
3928
4002
  }
3929
4003
  const eventData = {
3930
4004
  affectedNodeId: toolNodeId,
3931
4005
  status,
3932
4006
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
3933
- input: data.input,
3934
- output: data.output,
3935
- error: data.error,
3936
- errorMessage: data.errorMessage,
4007
+ input,
4008
+ output,
4009
+ error,
4010
+ errorMessage,
3937
4011
  metadata
3938
4012
  };
3939
4013
  metadata.sourceAgentId = this.id;
@@ -3971,11 +4045,11 @@ var Agent = class {
3971
4045
  */
3972
4046
  this.addAgentEvent = /* @__PURE__ */ __name((context, eventName, status, data = {}) => {
3973
4047
  const metadata = __spreadValues({}, data.metadata || {});
3974
- if (data.usage) {
3975
- metadata.usage = data.usage;
3976
- delete data.usage;
4048
+ const _a = data, { usage } = _a, standardData = __objRest(_a, ["usage"]);
4049
+ if (usage) {
4050
+ metadata.usage = usage;
3977
4051
  }
3978
- const standardData = __spreadProps(__spreadValues({}, data), {
4052
+ const eventData = __spreadProps(__spreadValues({}, standardData), {
3979
4053
  metadata
3980
4054
  });
3981
4055
  this.createStandardTimelineEvent(
@@ -3984,7 +4058,7 @@ var Agent = class {
3984
4058
  status,
3985
4059
  "agent" /* AGENT */,
3986
4060
  this.id,
3987
- standardData,
4061
+ eventData,
3988
4062
  "agent",
3989
4063
  context
3990
4064
  );
@@ -4016,7 +4090,8 @@ var Agent = class {
4016
4090
  getSystemMessage(_0) {
4017
4091
  return __async(this, arguments, function* ({
4018
4092
  input,
4019
- historyEntryId
4093
+ historyEntryId,
4094
+ contextMessages
4020
4095
  }) {
4021
4096
  let description = this.description;
4022
4097
  if (this.retriever && input && historyEntryId) {
@@ -4037,7 +4112,7 @@ var Agent = class {
4037
4112
  });
4038
4113
  try {
4039
4114
  const context = yield this.retriever.retrieve(input);
4040
- if (context && context.trim()) {
4115
+ if (context == null ? void 0 : context.trim()) {
4041
4116
  description = `${description}
4042
4117
 
4043
4118
  Relevant Context:
@@ -4062,7 +4137,12 @@ ${context}`;
4062
4137
  }
4063
4138
  }
4064
4139
  if (this.subAgentManager.hasSubAgents()) {
4065
- description = this.subAgentManager.generateSupervisorSystemMessage(description);
4140
+ const agentsMemory = yield this.prepareAgentsMemory(contextMessages);
4141
+ description = this.subAgentManager.generateSupervisorSystemMessage(description, agentsMemory);
4142
+ return {
4143
+ role: "system",
4144
+ content: description
4145
+ };
4066
4146
  }
4067
4147
  return {
4068
4148
  role: "system",
@@ -4070,6 +4150,26 @@ ${context}`;
4070
4150
  };
4071
4151
  });
4072
4152
  }
4153
+ /**
4154
+ * Prepare agents memory for the supervisor system message
4155
+ * This fetches and formats recent interactions with sub-agents
4156
+ */
4157
+ prepareAgentsMemory(contextMessages) {
4158
+ return __async(this, null, function* () {
4159
+ try {
4160
+ const subAgents = this.subAgentManager.getSubAgents();
4161
+ if (subAgents.length === 0)
4162
+ return "";
4163
+ const formattedMemory = contextMessages.filter((p) => p.role !== "system").filter((p) => p.role === "assistant" && !p.content.toString().includes("toolCallId")).map((message) => {
4164
+ return `${message.role}: ${message.content}`;
4165
+ }).join("\n\n");
4166
+ return formattedMemory || "No previous agent interactions found.";
4167
+ } catch (error) {
4168
+ console.warn("Error preparing agents memory:", error);
4169
+ return "Error retrieving agent history.";
4170
+ }
4171
+ });
4172
+ }
4073
4173
  /**
4074
4174
  * Add input to messages array based on type
4075
4175
  */
@@ -4242,7 +4342,8 @@ ${context}`;
4242
4342
  );
4243
4343
  const systemMessage = yield this.getSystemMessage({
4244
4344
  input,
4245
- historyEntryId: context.historyEntry.id
4345
+ historyEntryId: context.historyEntry.id,
4346
+ contextMessages
4246
4347
  });
4247
4348
  let messages = [systemMessage, ...contextMessages];
4248
4349
  messages = yield this.formatInputMessages(messages, input);
@@ -4365,7 +4466,8 @@ ${context}`;
4365
4466
  );
4366
4467
  const systemMessage = yield this.getSystemMessage({
4367
4468
  input,
4368
- historyEntryId: context.historyEntry.id
4469
+ historyEntryId: context.historyEntry.id,
4470
+ contextMessages
4369
4471
  });
4370
4472
  let messages = [systemMessage, ...contextMessages];
4371
4473
  messages = yield this.formatInputMessages(messages, input);
@@ -4505,7 +4607,8 @@ ${context}`;
4505
4607
  );
4506
4608
  const systemMessage = yield this.getSystemMessage({
4507
4609
  input,
4508
- historyEntryId: context.historyEntry.id
4610
+ historyEntryId: context.historyEntry.id,
4611
+ contextMessages
4509
4612
  });
4510
4613
  let messages = [systemMessage, ...contextMessages];
4511
4614
  messages = yield this.formatInputMessages(messages, input);
@@ -4583,7 +4686,8 @@ ${context}`;
4583
4686
  );
4584
4687
  const systemMessage = yield this.getSystemMessage({
4585
4688
  input,
4586
- historyEntryId: context.historyEntry.id
4689
+ historyEntryId: context.historyEntry.id,
4690
+ contextMessages
4587
4691
  });
4588
4692
  let messages = [systemMessage, ...contextMessages];
4589
4693
  messages = yield this.formatInputMessages(messages, input);