@voltagent/core 0.1.3 → 0.1.5

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
@@ -1874,7 +1874,8 @@ var LibSQLStorage = class {
1874
1874
  * @param data Additional data to log
1875
1875
  */
1876
1876
  debug(message, data) {
1877
- if (this.options.debug) {
1877
+ var _a;
1878
+ if ((_a = this.options) == null ? void 0 : _a.debug) {
1878
1879
  console.log(`[LibSQLStorage] ${message}`, data || "");
1879
1880
  }
1880
1881
  }
@@ -3716,17 +3717,24 @@ var SubAgentManager = class {
3716
3717
  /**
3717
3718
  * Generate enhanced system message for supervisor role
3718
3719
  * @param baseDescription - The base description of the agent
3720
+ * @param agentsMemory - Optional string containing formatted memory from previous agent interactions
3719
3721
  */
3720
- generateSupervisorSystemMessage(baseDescription) {
3722
+ generateSupervisorSystemMessage(baseDescription, agentsMemory = "") {
3721
3723
  if (this.subAgents.length === 0) {
3722
3724
  return baseDescription;
3723
3725
  }
3724
3726
  const subAgentList = this.subAgents.map((agent) => `- ${agent.name}: ${agent.description}`).join("\n");
3725
- return `You are a supervisor agent that coordinates between specialized agents:
3727
+ return `
3728
+ You are a supervisor agent that coordinates between specialized agents:
3726
3729
 
3730
+ <specialized_agents>
3727
3731
  ${subAgentList}
3732
+ </specialized_agents>
3733
+
3734
+ <instructions>
3735
+ ${baseDescription}
3736
+ </instructions>
3728
3737
 
3729
- When communicating with other agents, including the User, please follow these guidelines:
3730
3738
  <guidelines>
3731
3739
  - Provide a final answer to the User when you have a response from all agents.
3732
3740
  - Do not mention the name of any agent in your response.
@@ -3749,7 +3757,7 @@ When communicating with other agents, including the User, please follow these gu
3749
3757
  </guidelines>
3750
3758
 
3751
3759
  <agents_memory>
3752
- {{AGENTS_MEMORY}}
3760
+ ${agentsMemory || "No previous agent interactions available."}
3753
3761
  </agents_memory>
3754
3762
  `;
3755
3763
  }
@@ -3776,27 +3784,45 @@ When communicating with other agents, including the User, please follow these gu
3776
3784
  parentHistoryEntryId
3777
3785
  } = options;
3778
3786
  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}:
3787
+ try {
3788
+ if (sourceAgent && targetAgent.hooks) {
3789
+ yield (_b = (_a = targetAgent.hooks).onHandoff) == null ? void 0 : _b.call(_a, targetAgent, sourceAgent);
3790
+ }
3791
+ const sharedContext = options.sharedContext || [];
3792
+ const handoffMessage = {
3793
+ role: "system",
3794
+ content: `Task handed off from ${(sourceAgent == null ? void 0 : sourceAgent.name) || this.agentName} to ${targetAgent.name}:
3786
3795
  ${task}
3787
3796
  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
- };
3797
+ };
3798
+ const response = yield targetAgent.generateText([handoffMessage, ...sharedContext], {
3799
+ conversationId: handoffConversationId,
3800
+ userId,
3801
+ parentAgentId: (sourceAgent == null ? void 0 : sourceAgent.id) || parentAgentId,
3802
+ parentHistoryEntryId
3803
+ });
3804
+ return {
3805
+ result: response.text,
3806
+ conversationId: handoffConversationId,
3807
+ messages: [handoffMessage, { role: "assistant", content: response.text }],
3808
+ status: "success"
3809
+ };
3810
+ } catch (error) {
3811
+ console.error(`Error in handoffTask to ${targetAgent.name}:`, error);
3812
+ const errorMessage = error instanceof Error ? error.message : String(error);
3813
+ return {
3814
+ result: `Error in delegating task to ${targetAgent.name}: ${errorMessage}`,
3815
+ conversationId: handoffConversationId,
3816
+ messages: [
3817
+ {
3818
+ role: "system",
3819
+ content: `Error occurred during task handoff: ${errorMessage}`
3820
+ }
3821
+ ],
3822
+ status: "error",
3823
+ error: error instanceof Error ? error : String(error)
3824
+ };
3825
+ }
3800
3826
  });
3801
3827
  }
3802
3828
  /**
@@ -3805,16 +3831,35 @@ Context: ${JSON.stringify(context)}`
3805
3831
  handoffToMultiple(options) {
3806
3832
  return __async(this, null, function* () {
3807
3833
  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
- )
3834
+ const handoffConversationId = conversationId || crypto.randomUUID();
3835
+ const results = yield Promise.all(
3836
+ targetAgents.map((agent) => __async(this, null, function* () {
3837
+ try {
3838
+ return yield this.handoffTask(__spreadProps(__spreadValues({}, restOptions), {
3839
+ targetAgent: agent,
3840
+ conversationId: handoffConversationId,
3841
+ parentAgentId,
3842
+ parentHistoryEntryId
3843
+ }));
3844
+ } catch (error) {
3845
+ console.error(`Error in handoffToMultiple for agent ${agent.name}:`, error);
3846
+ const errorMessage = error instanceof Error ? error.message : String(error);
3847
+ return {
3848
+ result: `Error in delegating task to ${agent.name}: ${errorMessage}`,
3849
+ conversationId: handoffConversationId,
3850
+ messages: [
3851
+ {
3852
+ role: "system",
3853
+ content: `Error occurred during task handoff: ${errorMessage}`
3854
+ }
3855
+ ],
3856
+ status: "error",
3857
+ error: error instanceof Error ? error : String(error)
3858
+ };
3859
+ }
3860
+ }))
3817
3861
  );
3862
+ return results;
3818
3863
  });
3819
3864
  }
3820
3865
  /**
@@ -3830,26 +3875,56 @@ Context: ${JSON.stringify(context)}`
3830
3875
  context: import_zod.z.record(import_zod.z.unknown()).optional().describe("Additional context for the task")
3831
3876
  }),
3832
3877
  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");
3878
+ try {
3879
+ if (!task || task.trim() === "") {
3880
+ throw new Error("Task cannot be empty");
3881
+ }
3882
+ if (!targetAgents || !Array.isArray(targetAgents) || targetAgents.length === 0) {
3883
+ throw new Error("At least one target agent must be specified");
3884
+ }
3885
+ const agents = targetAgents.map((name) => {
3886
+ const agent = this.subAgents.find((a) => a.name === name);
3887
+ if (!agent) {
3888
+ console.warn(
3889
+ `Agent "${name}" not found. Available agents: ${this.subAgents.map((a) => a.name).join(", ")}`
3890
+ );
3891
+ }
3892
+ return agent;
3893
+ }).filter((agent) => agent !== void 0);
3894
+ if (agents.length === 0) {
3895
+ throw new Error(
3896
+ `No valid target agents found. Available agents: ${this.subAgents.map((a) => a.name).join(", ")}`
3897
+ );
3898
+ }
3899
+ const sourceAgent = options.sourceAgent;
3900
+ const currentHistoryEntryId = options.currentHistoryEntryId;
3901
+ const results = yield this.handoffToMultiple(__spreadValues({
3902
+ task,
3903
+ targetAgents: agents,
3904
+ context,
3905
+ sourceAgent,
3906
+ // Pass parent context for event propagation
3907
+ parentAgentId: sourceAgent == null ? void 0 : sourceAgent.id,
3908
+ parentHistoryEntryId: currentHistoryEntryId
3909
+ }, options));
3910
+ return results.map((result, index) => {
3911
+ const status = result.status || "success";
3912
+ const errorInfo = status === "error" && result.error ? typeof result.error === "string" ? result.error : result.error.message : void 0;
3913
+ return {
3914
+ agentName: agents[index].name,
3915
+ response: result.result,
3916
+ conversationId: result.conversationId,
3917
+ status,
3918
+ error: errorInfo
3919
+ };
3920
+ });
3921
+ } catch (error) {
3922
+ console.error("Error in delegate_task tool execution:", error);
3923
+ return {
3924
+ error: `Failed to delegate task: ${error instanceof Error ? error.message : String(error)}`,
3925
+ status: "error"
3926
+ };
3836
3927
  }
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
3928
  })
3854
3929
  };
3855
3930
  }
@@ -3915,25 +3990,25 @@ var Agent = class {
3915
3990
  }
3916
3991
  }, "createStandardTimelineEvent");
3917
3992
  /**
3918
- * Tool event creator
3993
+ * Fix delete operator usage for better performance
3919
3994
  */
3920
3995
  this.addToolEvent = /* @__PURE__ */ __name((_0, _1, _2, _3, ..._4) => __async(this, [_0, _1, _2, _3, ..._4], function* (context, eventName, toolName, status, data = {}) {
3921
3996
  const toolNodeId = createNodeId("tool" /* TOOL */, toolName, this.id);
3922
3997
  const metadata = __spreadValues({
3923
3998
  toolName
3924
3999
  }, data.metadata || {});
3925
- if (data.toolId) {
3926
- metadata.toolId = data.toolId;
3927
- delete data.toolId;
4000
+ const { toolId, input, output, error, errorMessage } = data;
4001
+ if (toolId) {
4002
+ metadata.toolId = toolId;
3928
4003
  }
3929
4004
  const eventData = {
3930
4005
  affectedNodeId: toolNodeId,
3931
4006
  status,
3932
4007
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
3933
- input: data.input,
3934
- output: data.output,
3935
- error: data.error,
3936
- errorMessage: data.errorMessage,
4008
+ input,
4009
+ output,
4010
+ error,
4011
+ errorMessage,
3937
4012
  metadata
3938
4013
  };
3939
4014
  metadata.sourceAgentId = this.id;
@@ -3971,11 +4046,11 @@ var Agent = class {
3971
4046
  */
3972
4047
  this.addAgentEvent = /* @__PURE__ */ __name((context, eventName, status, data = {}) => {
3973
4048
  const metadata = __spreadValues({}, data.metadata || {});
3974
- if (data.usage) {
3975
- metadata.usage = data.usage;
3976
- delete data.usage;
4049
+ const _a = data, { usage } = _a, standardData = __objRest(_a, ["usage"]);
4050
+ if (usage) {
4051
+ metadata.usage = usage;
3977
4052
  }
3978
- const standardData = __spreadProps(__spreadValues({}, data), {
4053
+ const eventData = __spreadProps(__spreadValues({}, standardData), {
3979
4054
  metadata
3980
4055
  });
3981
4056
  this.createStandardTimelineEvent(
@@ -3984,7 +4059,7 @@ var Agent = class {
3984
4059
  status,
3985
4060
  "agent" /* AGENT */,
3986
4061
  this.id,
3987
- standardData,
4062
+ eventData,
3988
4063
  "agent",
3989
4064
  context
3990
4065
  );
@@ -4016,7 +4091,8 @@ var Agent = class {
4016
4091
  getSystemMessage(_0) {
4017
4092
  return __async(this, arguments, function* ({
4018
4093
  input,
4019
- historyEntryId
4094
+ historyEntryId,
4095
+ contextMessages
4020
4096
  }) {
4021
4097
  let description = this.description;
4022
4098
  if (this.retriever && input && historyEntryId) {
@@ -4037,7 +4113,7 @@ var Agent = class {
4037
4113
  });
4038
4114
  try {
4039
4115
  const context = yield this.retriever.retrieve(input);
4040
- if (context && context.trim()) {
4116
+ if (context == null ? void 0 : context.trim()) {
4041
4117
  description = `${description}
4042
4118
 
4043
4119
  Relevant Context:
@@ -4062,7 +4138,12 @@ ${context}`;
4062
4138
  }
4063
4139
  }
4064
4140
  if (this.subAgentManager.hasSubAgents()) {
4065
- description = this.subAgentManager.generateSupervisorSystemMessage(description);
4141
+ const agentsMemory = yield this.prepareAgentsMemory(contextMessages);
4142
+ description = this.subAgentManager.generateSupervisorSystemMessage(description, agentsMemory);
4143
+ return {
4144
+ role: "system",
4145
+ content: description
4146
+ };
4066
4147
  }
4067
4148
  return {
4068
4149
  role: "system",
@@ -4070,6 +4151,26 @@ ${context}`;
4070
4151
  };
4071
4152
  });
4072
4153
  }
4154
+ /**
4155
+ * Prepare agents memory for the supervisor system message
4156
+ * This fetches and formats recent interactions with sub-agents
4157
+ */
4158
+ prepareAgentsMemory(contextMessages) {
4159
+ return __async(this, null, function* () {
4160
+ try {
4161
+ const subAgents = this.subAgentManager.getSubAgents();
4162
+ if (subAgents.length === 0)
4163
+ return "";
4164
+ const formattedMemory = contextMessages.filter((p) => p.role !== "system").filter((p) => p.role === "assistant" && !p.content.toString().includes("toolCallId")).map((message) => {
4165
+ return `${message.role}: ${message.content}`;
4166
+ }).join("\n\n");
4167
+ return formattedMemory || "No previous agent interactions found.";
4168
+ } catch (error) {
4169
+ console.warn("Error preparing agents memory:", error);
4170
+ return "Error retrieving agent history.";
4171
+ }
4172
+ });
4173
+ }
4073
4174
  /**
4074
4175
  * Add input to messages array based on type
4075
4176
  */
@@ -4242,7 +4343,8 @@ ${context}`;
4242
4343
  );
4243
4344
  const systemMessage = yield this.getSystemMessage({
4244
4345
  input,
4245
- historyEntryId: context.historyEntry.id
4346
+ historyEntryId: context.historyEntry.id,
4347
+ contextMessages
4246
4348
  });
4247
4349
  let messages = [systemMessage, ...contextMessages];
4248
4350
  messages = yield this.formatInputMessages(messages, input);
@@ -4365,7 +4467,8 @@ ${context}`;
4365
4467
  );
4366
4468
  const systemMessage = yield this.getSystemMessage({
4367
4469
  input,
4368
- historyEntryId: context.historyEntry.id
4470
+ historyEntryId: context.historyEntry.id,
4471
+ contextMessages
4369
4472
  });
4370
4473
  let messages = [systemMessage, ...contextMessages];
4371
4474
  messages = yield this.formatInputMessages(messages, input);
@@ -4505,7 +4608,8 @@ ${context}`;
4505
4608
  );
4506
4609
  const systemMessage = yield this.getSystemMessage({
4507
4610
  input,
4508
- historyEntryId: context.historyEntry.id
4611
+ historyEntryId: context.historyEntry.id,
4612
+ contextMessages
4509
4613
  });
4510
4614
  let messages = [systemMessage, ...contextMessages];
4511
4615
  messages = yield this.formatInputMessages(messages, input);
@@ -4583,7 +4687,8 @@ ${context}`;
4583
4687
  );
4584
4688
  const systemMessage = yield this.getSystemMessage({
4585
4689
  input,
4586
- historyEntryId: context.historyEntry.id
4690
+ historyEntryId: context.historyEntry.id,
4691
+ contextMessages
4587
4692
  });
4588
4693
  let messages = [systemMessage, ...contextMessages];
4589
4694
  messages = yield this.formatInputMessages(messages, input);