@aigne/core 1.5.0 → 1.5.1-1

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.
Files changed (73) hide show
  1. package/lib/cjs/agents/agent.d.ts +8 -4
  2. package/lib/cjs/agents/agent.js +69 -8
  3. package/lib/cjs/agents/ai-agent.d.ts +5 -5
  4. package/lib/cjs/agents/ai-agent.js +1 -3
  5. package/lib/cjs/agents/memory.d.ts +1 -1
  6. package/lib/cjs/agents/user-agent.d.ts +4 -3
  7. package/lib/cjs/agents/user-agent.js +5 -5
  8. package/lib/cjs/execution-engine/context.d.ts +58 -5
  9. package/lib/cjs/execution-engine/context.js +169 -0
  10. package/lib/cjs/execution-engine/execution-engine.d.ts +10 -13
  11. package/lib/cjs/execution-engine/execution-engine.js +7 -96
  12. package/lib/cjs/execution-engine/message-queue.d.ts +2 -0
  13. package/lib/cjs/execution-engine/utils.d.ts +2 -2
  14. package/lib/cjs/execution-engine/utils.js +11 -15
  15. package/lib/cjs/models/chat-model.d.ts +8 -0
  16. package/lib/cjs/models/chat-model.js +21 -0
  17. package/lib/cjs/models/claude-chat-model.js +24 -4
  18. package/lib/cjs/models/openai-chat-model.d.ts +2 -1
  19. package/lib/cjs/models/openai-chat-model.js +13 -1
  20. package/lib/cjs/models/xai-chat-model.d.ts +1 -2
  21. package/lib/cjs/models/xai-chat-model.js +0 -2
  22. package/lib/cjs/prompt/prompt-builder.js +3 -3
  23. package/lib/cjs/utils/json-schema.js +1 -2
  24. package/lib/cjs/utils/logger.d.ts +1 -1
  25. package/lib/cjs/utils/logger.js +1 -1
  26. package/lib/cjs/utils/model-utils.d.ts +3 -0
  27. package/lib/cjs/utils/model-utils.js +9 -0
  28. package/lib/cjs/utils/type-utils.d.ts +2 -3
  29. package/lib/cjs/utils/type-utils.js +16 -11
  30. package/lib/dts/agents/agent.d.ts +8 -4
  31. package/lib/dts/agents/ai-agent.d.ts +5 -5
  32. package/lib/dts/agents/memory.d.ts +1 -1
  33. package/lib/dts/agents/user-agent.d.ts +4 -3
  34. package/lib/dts/execution-engine/context.d.ts +58 -5
  35. package/lib/dts/execution-engine/execution-engine.d.ts +10 -13
  36. package/lib/dts/execution-engine/message-queue.d.ts +2 -0
  37. package/lib/dts/execution-engine/utils.d.ts +2 -2
  38. package/lib/dts/models/chat-model.d.ts +8 -0
  39. package/lib/dts/models/openai-chat-model.d.ts +2 -1
  40. package/lib/dts/models/xai-chat-model.d.ts +1 -2
  41. package/lib/dts/utils/logger.d.ts +1 -1
  42. package/lib/dts/utils/model-utils.d.ts +3 -0
  43. package/lib/dts/utils/type-utils.d.ts +2 -3
  44. package/lib/esm/agents/agent.d.ts +8 -4
  45. package/lib/esm/agents/agent.js +36 -8
  46. package/lib/esm/agents/ai-agent.d.ts +5 -5
  47. package/lib/esm/agents/ai-agent.js +1 -3
  48. package/lib/esm/agents/memory.d.ts +1 -1
  49. package/lib/esm/agents/user-agent.d.ts +4 -3
  50. package/lib/esm/agents/user-agent.js +5 -5
  51. package/lib/esm/execution-engine/context.d.ts +58 -5
  52. package/lib/esm/execution-engine/context.js +164 -1
  53. package/lib/esm/execution-engine/execution-engine.d.ts +10 -13
  54. package/lib/esm/execution-engine/execution-engine.js +7 -96
  55. package/lib/esm/execution-engine/message-queue.d.ts +2 -0
  56. package/lib/esm/execution-engine/utils.d.ts +2 -2
  57. package/lib/esm/execution-engine/utils.js +11 -15
  58. package/lib/esm/models/chat-model.d.ts +8 -0
  59. package/lib/esm/models/chat-model.js +21 -0
  60. package/lib/esm/models/claude-chat-model.js +24 -4
  61. package/lib/esm/models/openai-chat-model.d.ts +2 -1
  62. package/lib/esm/models/openai-chat-model.js +13 -1
  63. package/lib/esm/models/xai-chat-model.d.ts +1 -2
  64. package/lib/esm/models/xai-chat-model.js +0 -2
  65. package/lib/esm/prompt/prompt-builder.js +1 -1
  66. package/lib/esm/utils/json-schema.js +1 -2
  67. package/lib/esm/utils/logger.d.ts +1 -1
  68. package/lib/esm/utils/logger.js +1 -1
  69. package/lib/esm/utils/model-utils.d.ts +3 -0
  70. package/lib/esm/utils/model-utils.js +6 -0
  71. package/lib/esm/utils/type-utils.d.ts +2 -3
  72. package/lib/esm/utils/type-utils.js +12 -8
  73. package/package.json +4 -5
@@ -5,14 +5,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.ExecutionEngine = void 0;
7
7
  const node_events_1 = __importDefault(require("node:events"));
8
- const lodash_es_1 = require("lodash-es");
9
8
  const zod_1 = require("zod");
10
9
  const agent_js_1 = require("../agents/agent.js");
11
- const types_js_1 = require("../agents/types.js");
12
- const user_agent_js_1 = require("../agents/user-agent.js");
13
10
  const chat_model_js_1 = require("../models/chat-model.js");
14
- const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
15
11
  const type_utils_js_1 = require("../utils/type-utils.js");
12
+ const context_js_1 = require("./context.js");
16
13
  const message_queue_js_1 = require("./message-queue.js");
17
14
  class ExecutionEngine extends node_events_1.default {
18
15
  constructor(options) {
@@ -21,6 +18,7 @@ class ExecutionEngine extends node_events_1.default {
21
18
  super();
22
19
  this.model = options?.model;
23
20
  this.tools = options?.tools ?? [];
21
+ this.limits = options?.limits;
24
22
  if (options?.agents?.length)
25
23
  this.addAgent(...options.agents);
26
24
  this.initProcessExitHandler();
@@ -29,6 +27,7 @@ class ExecutionEngine extends node_events_1.default {
29
27
  model;
30
28
  tools;
31
29
  agents = [];
30
+ limits;
32
31
  addAgent(...agents) {
33
32
  (0, type_utils_js_1.checkArguments)("ExecutionEngine.addAgent", executionEngineAddAgentArgsSchema, agents);
34
33
  for (const agent of agents) {
@@ -36,8 +35,8 @@ class ExecutionEngine extends node_events_1.default {
36
35
  agent.attach(this);
37
36
  }
38
37
  }
39
- createMessageFromInput(message) {
40
- return typeof message === "string" ? (0, prompt_builder_js_1.createMessage)(message) : message;
38
+ newContext() {
39
+ return new context_js_1.ExecutionContext(this);
41
40
  }
42
41
  /**
43
42
  * Publish a message to a topic, the engine will call the listeners of the topic
@@ -46,56 +45,10 @@ class ExecutionEngine extends node_events_1.default {
46
45
  * @param from the agent who publish the message, if not provided, it will be treated as a user message
47
46
  */
48
47
  publish(topic, message, from) {
49
- (0, type_utils_js_1.checkArguments)("ExecutionEngine.publish", executionEnginePublishArgsSchema, {
50
- topic,
51
- message,
52
- from,
53
- });
54
- const request = {
55
- role: !from || from instanceof user_agent_js_1.UserAgent ? "user" : "agent",
56
- source: from?.name,
57
- message: this.createMessageFromInput(message),
58
- };
59
- this.messageQueue.publish(topic, request);
48
+ return new context_js_1.ExecutionContext(this).publish(topic, message, from);
60
49
  }
61
50
  call(agent, message, options) {
62
- (0, type_utils_js_1.checkArguments)("ExecutionEngine.call", executionEngineCallArgsSchema, {
63
- agent,
64
- message,
65
- options,
66
- });
67
- if ((0, lodash_es_1.isNil)(message)) {
68
- let activeAgent = agent;
69
- return user_agent_js_1.UserAgent.from({
70
- context: this,
71
- process: async (input, context) => {
72
- const [output, agent] = await context.call(activeAgent, input, {
73
- returnActiveAgent: true,
74
- });
75
- activeAgent = agent;
76
- return output;
77
- },
78
- });
79
- }
80
- return this._call(agent, this.createMessageFromInput(message)).then((result) => options?.returnActiveAgent ? [result.output, result.agent] : result.output);
81
- }
82
- /**
83
- * Call an agent with a message and return the output and the active agent
84
- * @param agent Agent to call
85
- * @param message Message to pass to the agent
86
- * @returns the output of the agent and the final active agent
87
- */
88
- async _call(agent, message) {
89
- const { output, agent: activeAgent } = await this.callAgent(agent, message);
90
- if (activeAgent instanceof agent_js_1.Agent) {
91
- const publishTopics = typeof activeAgent.publishTopic === "function"
92
- ? await activeAgent.publishTopic(output)
93
- : activeAgent.publishTopic;
94
- if (publishTopics?.length) {
95
- this.publish(publishTopics, output, activeAgent);
96
- }
97
- }
98
- return { output, agent: activeAgent };
51
+ return new context_js_1.ExecutionContext(this).call(agent, message, options);
99
52
  }
100
53
  subscribe(topic, listener) {
101
54
  (0, type_utils_js_1.checkArguments)("ExecutionEngine.subscribe", executionEngineSubscribeArgsSchema, {
@@ -111,38 +64,6 @@ class ExecutionEngine extends node_events_1.default {
111
64
  });
112
65
  this.messageQueue.unsubscribe(topic, listener);
113
66
  }
114
- async callAgent(agent, input) {
115
- let activeAgent = agent;
116
- let output;
117
- for (;;) {
118
- let result;
119
- if (typeof activeAgent === "function") {
120
- result = await activeAgent(input, this);
121
- }
122
- else {
123
- result = await activeAgent.call(input, this);
124
- }
125
- if (!(result instanceof agent_js_1.Agent))
126
- output = result;
127
- const transferToAgent = result instanceof agent_js_1.Agent
128
- ? result
129
- : (0, types_js_1.isTransferAgentOutput)(result)
130
- ? result[types_js_1.transferAgentOutputKey].agent
131
- : undefined;
132
- if (transferToAgent) {
133
- activeAgent = transferToAgent;
134
- }
135
- else {
136
- break;
137
- }
138
- }
139
- if (!output)
140
- throw new Error("Unexpected empty output");
141
- return {
142
- agent: activeAgent,
143
- output,
144
- };
145
- }
146
67
  async shutdown() {
147
68
  for (const tool of this.tools) {
148
69
  await tool.shutdown();
@@ -164,16 +85,6 @@ const executionEngineOptionsSchema = zod_1.z.object({
164
85
  agents: zod_1.z.array(zod_1.z.instanceof(agent_js_1.Agent)).optional(),
165
86
  });
166
87
  const executionEngineAddAgentArgsSchema = zod_1.z.array(zod_1.z.instanceof(agent_js_1.Agent));
167
- const executionEnginePublishArgsSchema = zod_1.z.object({
168
- topic: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]),
169
- message: zod_1.z.union([zod_1.z.string(), zod_1.z.record(zod_1.z.unknown())]),
170
- from: zod_1.z.instanceof(agent_js_1.Agent).optional(),
171
- });
172
- const executionEngineCallArgsSchema = zod_1.z.object({
173
- agent: zod_1.z.union([zod_1.z.function(), zod_1.z.instanceof(agent_js_1.Agent)]),
174
- message: zod_1.z.union([zod_1.z.record(zod_1.z.unknown()), zod_1.z.string()]).optional(),
175
- options: zod_1.z.object({ returnActiveAgent: zod_1.z.boolean().optional() }).optional(),
176
- });
177
88
  const executionEngineSubscribeArgsSchema = zod_1.z.object({
178
89
  topic: zod_1.z.string(),
179
90
  listener: zod_1.z.function(zod_1.z.tuple([zod_1.z.any()]), zod_1.z.any()).optional(),
@@ -1,10 +1,12 @@
1
1
  import type { Message } from "../agents/agent.js";
2
+ import type { Context } from "./context.js";
2
3
  export declare const UserInputTopic = "UserInputTopic";
3
4
  export declare const UserOutputTopic = "UserOutputTopic";
4
5
  export interface MessagePayload {
5
6
  role: "user" | "agent";
6
7
  source?: string;
7
8
  message: Message;
9
+ context: Context;
8
10
  }
9
11
  export type MessageQueueListener = (message: MessagePayload) => void;
10
12
  export type MessageRequest = MessagePayload;
@@ -1,4 +1,4 @@
1
1
  import { type FunctionAgentFn } from "../agents/agent.js";
2
2
  import type { Runnable } from "./context.js";
3
- export declare function sequential(..._agents: [Runnable, ...Runnable[]]): FunctionAgentFn;
4
- export declare function parallel(..._agents: [Runnable, ...Runnable[]]): FunctionAgentFn;
3
+ export declare function sequential(...agents: [Runnable, ...Runnable[]]): FunctionAgentFn;
4
+ export declare function parallel(...agents: [Runnable, ...Runnable[]]): FunctionAgentFn;
@@ -5,32 +5,28 @@ exports.parallel = parallel;
5
5
  const zod_1 = require("zod");
6
6
  const agent_js_1 = require("../agents/agent.js");
7
7
  const type_utils_js_1 = require("../utils/type-utils.js");
8
- function sequential(..._agents) {
9
- (0, type_utils_js_1.checkArguments)("sequential", agentArraySchema, _agents);
10
- let agents = [..._agents];
8
+ function sequential(...agents) {
9
+ (0, type_utils_js_1.checkArguments)("sequential", agentArraySchema, agents);
10
+ let _agents = [...agents];
11
11
  return async (input, context) => {
12
- if (!context)
13
- throw new Error("Context is required for executing sequential agents. Please provide a valid context.");
14
12
  const output = {};
15
13
  // Clone the agents to run, so that we can update the agents list during the loop
16
- const agentsToRun = [...agents];
17
- agents = [];
14
+ const agentsToRun = [..._agents];
15
+ _agents = [];
18
16
  for (const agent of agentsToRun) {
19
17
  const [o, transferToAgent] = await context.call(agent, { ...input, ...output }, { returnActiveAgent: true });
20
18
  Object.assign(output, o);
21
- agents.push(transferToAgent);
19
+ _agents.push(transferToAgent);
22
20
  }
23
21
  return output;
24
22
  };
25
23
  }
26
- function parallel(..._agents) {
27
- (0, type_utils_js_1.checkArguments)("parallel", agentArraySchema, _agents);
28
- let agents = [..._agents];
24
+ function parallel(...agents) {
25
+ (0, type_utils_js_1.checkArguments)("parallel", agentArraySchema, agents);
26
+ let _agents = [...agents];
29
27
  return async (input, context) => {
30
- if (!context)
31
- throw new Error("Context is required for executing parallel agents. Please provide a valid context.");
32
- const result = await Promise.all(agents.map((agent) => context.call(agent, input, { returnActiveAgent: true })));
33
- agents = result.map((i) => i[1]);
28
+ const result = await Promise.all(_agents.map((agent) => context.call(agent, input, { returnActiveAgent: true })));
29
+ _agents = result.map((i) => i[1]);
34
30
  const outputs = result.map((i) => i[0]);
35
31
  return Object.assign({}, ...outputs);
36
32
  };
@@ -1,6 +1,9 @@
1
1
  import { Agent, type Message } from "../agents/agent.js";
2
+ import type { Context } from "../execution-engine/context.js";
2
3
  export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelOutput> {
3
4
  constructor();
5
+ protected preprocess(input: ChatModelInput, context: Context): void;
6
+ protected postprocess(input: ChatModelInput, output: ChatModelOutput, context: Context): void;
4
7
  }
5
8
  export interface ChatModelInput extends Message {
6
9
  messages: ChatModelInputMessage[];
@@ -71,6 +74,7 @@ export interface ChatModelOutput extends Message {
71
74
  text?: string;
72
75
  json?: object;
73
76
  toolCalls?: ChatModelOutputToolCall[];
77
+ usage?: ChatModelOutputUsage;
74
78
  }
75
79
  export interface ChatModelOutputToolCall {
76
80
  id: string;
@@ -80,3 +84,7 @@ export interface ChatModelOutputToolCall {
80
84
  arguments: Message;
81
85
  };
82
86
  }
87
+ export interface ChatModelOutputUsage {
88
+ promptTokens: number;
89
+ completionTokens: number;
90
+ }
@@ -10,6 +10,22 @@ class ChatModel extends agent_js_1.Agent {
10
10
  outputSchema: chatModelOutputSchema,
11
11
  });
12
12
  }
13
+ preprocess(input, context) {
14
+ super.preprocess(input, context);
15
+ const { limits, usage } = context;
16
+ const usedTokens = usage.completionTokens + usage.promptTokens;
17
+ if (limits?.maxTokens && usedTokens >= limits.maxTokens) {
18
+ throw new Error(`Exceeded max tokens ${usedTokens}/${limits.maxTokens}`);
19
+ }
20
+ }
21
+ postprocess(input, output, context) {
22
+ super.postprocess(input, output, context);
23
+ const { usage } = output;
24
+ if (usage) {
25
+ context.usage.completionTokens += usage.completionTokens;
26
+ context.usage.promptTokens += usage.promptTokens;
27
+ }
28
+ }
13
29
  }
14
30
  exports.ChatModel = ChatModel;
15
31
  const chatModelInputMessageSchema = zod_1.z.object({
@@ -85,8 +101,13 @@ const chatModelOutputToolCallSchema = zod_1.z.object({
85
101
  arguments: zod_1.z.record(zod_1.z.unknown()),
86
102
  }),
87
103
  });
104
+ const chatModelOutputUsageSchema = zod_1.z.object({
105
+ promptTokens: zod_1.z.number(),
106
+ completionTokens: zod_1.z.number(),
107
+ });
88
108
  const chatModelOutputSchema = zod_1.z.object({
89
109
  text: zod_1.z.string().optional(),
90
110
  json: zod_1.z.record(zod_1.z.unknown()).optional(),
91
111
  toolCalls: zod_1.z.array(chatModelOutputToolCallSchema).optional(),
112
+ usage: chatModelOutputUsageSchema.optional(),
92
113
  });
@@ -5,10 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.ClaudeChatModel = exports.claudeChatModelOptionsSchema = void 0;
7
7
  const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
8
- const lodash_es_1 = require("lodash-es");
9
8
  const zod_1 = require("zod");
10
9
  const json_schema_js_1 = require("../utils/json-schema.js");
11
10
  const logger_js_1 = require("../utils/logger.js");
11
+ const model_utils_js_1 = require("../utils/model-utils.js");
12
12
  const type_utils_js_1 = require("../utils/type-utils.js");
13
13
  const chat_model_js_1 = require("./chat-model.js");
14
14
  const CHAT_MODEL_CLAUDE_DEFAULT_MODEL = "claude-3-7-sonnet-latest";
@@ -65,7 +65,12 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
65
65
  // Claude doesn't support json_schema response and tool calls in the same request,
66
66
  // so we need to make a separate request for json_schema response when the tool calls is empty
67
67
  if (!result.toolCalls?.length && input.responseFormat?.type === "json_schema") {
68
- return this.requestStructuredOutput(body, input.responseFormat);
68
+ const output = await this.requestStructuredOutput(body, input.responseFormat);
69
+ return {
70
+ ...output,
71
+ // merge usage from both requests
72
+ usage: (0, model_utils_js_1.mergeUsage)(result.usage, output.usage),
73
+ };
69
74
  }
70
75
  return result;
71
76
  }
@@ -74,7 +79,18 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
74
79
  try {
75
80
  let text = "";
76
81
  const toolCalls = [];
82
+ let usage;
77
83
  for await (const chunk of stream) {
84
+ if (chunk.type === "message_start") {
85
+ const { input_tokens, output_tokens } = chunk.message.usage;
86
+ usage = {
87
+ promptTokens: input_tokens,
88
+ completionTokens: output_tokens,
89
+ };
90
+ }
91
+ if (chunk.type === "message_delta" && usage) {
92
+ usage.completionTokens = chunk.usage.output_tokens;
93
+ }
78
94
  logs.push(JSON.stringify(chunk));
79
95
  // handle streaming text
80
96
  if (chunk.type === "content_block_delta" && chunk.delta.type === "text_delta") {
@@ -98,7 +114,7 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
98
114
  call.args += chunk.delta.partial_json;
99
115
  }
100
116
  }
101
- const result = { text };
117
+ const result = { usage, text };
102
118
  if (toolCalls.length) {
103
119
  result.toolCalls = toolCalls
104
120
  .map(({ args, ...c }) => ({
@@ -143,6 +159,10 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
143
159
  throw new Error("Json tool not found");
144
160
  return {
145
161
  json: jsonTool.input,
162
+ usage: {
163
+ promptTokens: result.usage.input_tokens,
164
+ completionTokens: result.usage.output_tokens,
165
+ },
146
166
  };
147
167
  }
148
168
  }
@@ -236,7 +256,7 @@ function convertTools({ tools, toolChoice, disableParallelToolUse, }) {
236
256
  ? tools.map((i) => ({
237
257
  name: i.function.name,
238
258
  description: i.function.description,
239
- input_schema: (0, lodash_es_1.isEmpty)(i.function.parameters)
259
+ input_schema: (0, type_utils_js_1.isEmpty)(i.function.parameters)
240
260
  ? { type: "object" }
241
261
  : i.function.parameters,
242
262
  }))
@@ -1,3 +1,4 @@
1
+ import OpenAI from "openai";
1
2
  import { z } from "zod";
2
3
  import { ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "./chat-model.js";
3
4
  export interface OpenAIChatModelOptions {
@@ -61,7 +62,7 @@ export declare class OpenAIChatModel extends ChatModel {
61
62
  options?: OpenAIChatModelOptions | undefined;
62
63
  constructor(options?: OpenAIChatModelOptions | undefined);
63
64
  private _client?;
64
- private get client();
65
+ get client(): OpenAI;
65
66
  get modelOptions(): ChatModelOptions | undefined;
66
67
  process(input: ChatModelInput): Promise<ChatModelOutput>;
67
68
  }
@@ -69,10 +69,14 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
69
69
  },
70
70
  }
71
71
  : undefined,
72
+ stream_options: {
73
+ include_usage: true,
74
+ },
72
75
  stream: true,
73
76
  });
74
77
  let text = "";
75
78
  const toolCalls = [];
79
+ let usage;
76
80
  for await (const chunk of res) {
77
81
  const choice = chunk.choices?.[0];
78
82
  if (choice?.delta.tool_calls?.length) {
@@ -94,8 +98,16 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
94
98
  }
95
99
  if (choice?.delta.content)
96
100
  text += choice.delta.content;
101
+ if (chunk.usage) {
102
+ usage = {
103
+ promptTokens: chunk.usage.prompt_tokens,
104
+ completionTokens: chunk.usage.completion_tokens,
105
+ };
106
+ }
97
107
  }
98
- const result = {};
108
+ const result = {
109
+ usage,
110
+ };
99
111
  if (input.responseFormat?.type === "json_schema" && text) {
100
112
  result.json = (0, json_schema_js_1.parseJSON)(text);
101
113
  }
@@ -7,6 +7,5 @@ export interface XAIChatModelOptions {
7
7
  baseURL?: string;
8
8
  }
9
9
  export declare class XAIChatModel extends OpenAIChatModel {
10
- options?: XAIChatModelOptions | undefined;
11
- constructor(options?: XAIChatModelOptions | undefined);
10
+ constructor(options?: XAIChatModelOptions);
12
11
  }
@@ -5,14 +5,12 @@ const openai_chat_model_js_1 = require("./openai-chat-model.js");
5
5
  const XAI_DEFAULT_CHAT_MODEL = "grok-2-latest";
6
6
  const XAI_BASE_URL = "https://api.x.ai/v1";
7
7
  class XAIChatModel extends openai_chat_model_js_1.OpenAIChatModel {
8
- options;
9
8
  constructor(options) {
10
9
  super({
11
10
  ...options,
12
11
  model: options?.model || XAI_DEFAULT_CHAT_MODEL,
13
12
  baseURL: options?.baseURL || XAI_BASE_URL,
14
13
  });
15
- this.options = options;
16
14
  }
17
15
  }
18
16
  exports.XAIChatModel = XAIChatModel;
@@ -4,10 +4,10 @@ exports.PromptBuilder = exports.DEFAULT_MAX_HISTORY_MESSAGES = exports.MESSAGE_K
4
4
  exports.createMessage = createMessage;
5
5
  exports.getMessage = getMessage;
6
6
  const promises_1 = require("node:fs/promises");
7
- const lodash_es_1 = require("lodash-es");
8
7
  const zod_1 = require("zod");
9
8
  const agent_js_1 = require("../agents/agent.js");
10
9
  const json_schema_js_1 = require("../utils/json-schema.js");
10
+ const type_utils_js_1 = require("../utils/type-utils.js");
11
11
  const template_js_1 = require("./template.js");
12
12
  exports.MESSAGE_KEY = "$message";
13
13
  exports.DEFAULT_MAX_HISTORY_MESSAGES = 10;
@@ -18,7 +18,7 @@ function getMessage(input) {
18
18
  const userInputMessage = input[exports.MESSAGE_KEY];
19
19
  if (typeof userInputMessage === "string")
20
20
  return userInputMessage;
21
- if (!(0, lodash_es_1.isNil)(userInputMessage))
21
+ if (!(0, type_utils_js_1.isNil)(userInputMessage))
22
22
  return JSON.stringify(userInputMessage);
23
23
  return undefined;
24
24
  }
@@ -172,5 +172,5 @@ function isEmptyObjectType(schema) {
172
172
  }
173
173
  function convertMessageToContent(i) {
174
174
  const str = i[exports.MESSAGE_KEY];
175
- return !(0, lodash_es_1.isNil)(str) ? (typeof str === "string" ? str : JSON.stringify(str)) : JSON.stringify(i);
175
+ return !(0, type_utils_js_1.isNil)(str) ? (typeof str === "string" ? str : JSON.stringify(str)) : JSON.stringify(i);
176
176
  }
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.outputSchemaToResponseFormatSchema = outputSchemaToResponseFormatSchema;
4
4
  exports.parseJSON = parseJSON;
5
5
  exports.ensureZodUnionArray = ensureZodUnionArray;
6
- const lodash_es_1 = require("lodash-es");
7
6
  const zod_to_json_schema_1 = require("zod-to-json-schema");
8
7
  const logger_js_1 = require("./logger.js");
9
8
  function outputSchemaToResponseFormatSchema(agentOutput) {
@@ -13,7 +12,7 @@ function setAdditionPropertiesDeep(schema, additionalProperties) {
13
12
  if (Array.isArray(schema)) {
14
13
  return schema.map((s) => setAdditionPropertiesDeep(s, additionalProperties));
15
14
  }
16
- if ((0, lodash_es_1.isObject)(schema)) {
15
+ if (schema && typeof schema === "object" && !Array.isArray(schema)) {
17
16
  return Object.entries(schema).reduce((acc, [key, value]) => {
18
17
  acc[key] = setAdditionPropertiesDeep(value, additionalProperties);
19
18
  if (acc.type === "object") {
@@ -10,7 +10,7 @@ export declare const logger: debug.Debug & {
10
10
  debug: debug.Debug;
11
11
  default: debug.Debug;
12
12
  } & {
13
- globalSpinner: import("ora").Ora;
13
+ globalSpinner: import("@aigne/ora").Ora;
14
14
  base: DebugWithSpinner;
15
15
  debug: DebugWithSpinner;
16
16
  spinner: typeof spinner;
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.logger = void 0;
7
+ const ora_1 = __importDefault(require("@aigne/ora"));
7
8
  const debug_1 = __importDefault(require("debug"));
8
- const ora_1 = __importDefault(require("ora"));
9
9
  debug_1.default.log = (...args) => {
10
10
  const { isSpinning } = globalSpinner;
11
11
  if (isSpinning)
@@ -0,0 +1,3 @@
1
+ import type { ChatModelOutputUsage } from "../models/chat-model.js";
2
+ import type { Nullish } from "./type-utils.js";
3
+ export declare function mergeUsage(...usages: Nullish<ChatModelOutputUsage>[]): ChatModelOutputUsage;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mergeUsage = mergeUsage;
4
+ function mergeUsage(...usages) {
5
+ return {
6
+ promptTokens: usages.reduce((acc, usage) => (usage ? acc + usage.promptTokens : acc), 0),
7
+ completionTokens: usages.reduce((acc, usage) => (usage ? acc + usage.completionTokens : acc), 0),
8
+ };
9
+ }
@@ -1,12 +1,11 @@
1
1
  import { type ZodType } from "zod";
2
2
  export type PromiseOrValue<T> = T | Promise<T>;
3
3
  export type Nullish<T> = T | null | undefined;
4
+ export declare function isNil(value: unknown): value is null | undefined;
5
+ export declare function isEmpty(obj: unknown): boolean;
4
6
  export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
5
7
  export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
6
8
  export declare function orArrayToArray<T>(value?: T | T[]): T[];
7
- export declare function get(obj: unknown, path: string | string[], type?: undefined): unknown | undefined;
8
- export declare function get(obj: unknown, path: string | string[], type: "string"): string | undefined;
9
- export declare function get(obj: unknown, path: string | string[], type: "number"): number | undefined;
10
9
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
11
10
  [key: string]: T;
12
11
  };
@@ -1,31 +1,36 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isNil = isNil;
4
+ exports.isEmpty = isEmpty;
3
5
  exports.isNonNullable = isNonNullable;
4
6
  exports.isNotEmpty = isNotEmpty;
5
7
  exports.orArrayToArray = orArrayToArray;
6
- exports.get = get;
7
8
  exports.createAccessorArray = createAccessorArray;
8
9
  exports.checkArguments = checkArguments;
9
- const lodash_es_1 = require("lodash-es");
10
10
  const zod_1 = require("zod");
11
+ function isNil(value) {
12
+ return value === null || value === undefined;
13
+ }
14
+ function isEmpty(obj) {
15
+ if (isNil(obj))
16
+ return true;
17
+ if (typeof obj === "string" || Array.isArray(obj))
18
+ return obj.length === 0;
19
+ if (typeof obj === "object")
20
+ Object.keys(obj).length === 0;
21
+ return false;
22
+ }
11
23
  function isNonNullable(value) {
12
- return !(0, lodash_es_1.isNil)(value);
24
+ return !isNil(value);
13
25
  }
14
26
  function isNotEmpty(arr) {
15
27
  return arr.length > 0;
16
28
  }
17
29
  function orArrayToArray(value) {
18
- if ((0, lodash_es_1.isNil)(value))
30
+ if (isNil(value))
19
31
  return [];
20
32
  return Array.isArray(value) ? value : [value];
21
33
  }
22
- function get(obj, path, type) {
23
- const v = (0, lodash_es_1.get)(obj, path);
24
- if (type === "string" && typeof v === "string")
25
- return v;
26
- if (type === "number" && typeof v === "number")
27
- return v;
28
- }
29
34
  function createAccessorArray(array, accessor) {
30
35
  return new Proxy(array, {
31
36
  get: (t, p, r) => Reflect.get(t, p, r) ?? accessor(array, p),
@@ -44,11 +44,15 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
44
44
  * - subscribe to memory topic if memory is enabled
45
45
  * @param context Context to attach
46
46
  */
47
- attach(context: Context): void;
47
+ attach(context: Pick<Context, "subscribe">): void;
48
48
  addTool<I extends Message, O extends Message>(tool: Agent<I, O> | FunctionAgentFn<I, O>): void;
49
49
  get isCallable(): boolean;
50
+ private checkContextStatus;
51
+ private newDefaultContext;
50
52
  call(input: I | string, context?: Context): Promise<O>;
51
- abstract process(input: I, context?: Context): Promise<O | TransferAgentOutput>;
53
+ protected preprocess(_: I, context: Context): void;
54
+ protected postprocess(input: I, output: O, context: Context): void;
55
+ abstract process(input: I, context: Context): Promise<O | TransferAgentOutput>;
52
56
  shutdown(): Promise<void>;
53
57
  }
54
58
  export type AgentInputOutputSchema<I extends Message = Message> = ZodType<I> | ((agent: Agent) => ZodType<I>);
@@ -59,6 +63,6 @@ export declare class FunctionAgent<I extends Message = Message, O extends Messag
59
63
  static from<I extends Message, O extends Message>(options: FunctionAgentOptions<I, O> | FunctionAgentFn<I, O>): FunctionAgent<I, O>;
60
64
  constructor(options: FunctionAgentOptions<I, O>);
61
65
  fn: FunctionAgentFn<I, O>;
62
- process(input: I, context?: Context): Promise<TransferAgentOutput | O>;
66
+ process(input: I, context: Context): Promise<TransferAgentOutput | O>;
63
67
  }
64
- export type FunctionAgentFn<I extends Message = Message, O extends Message = Message> = (input: I, context?: Context) => O | Promise<O> | Agent | Promise<Agent>;
68
+ export type FunctionAgentFn<I extends Message = Message, O extends Message = Message> = (input: I, context: Context) => O | Promise<O> | Agent | Promise<Agent>;
@@ -27,10 +27,10 @@ export declare const aiAgentOptionsSchema: z.ZodObject<{
27
27
  disableLogging: z.ZodOptional<z.ZodBoolean>;
28
28
  memory: z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodAny, z.ZodAny]>>;
29
29
  }, "strip", z.ZodTypeAny, {
30
+ description?: string | undefined;
30
31
  tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
31
- toolChoice?: "auto" | "none" | "required" | Agent<Message, Message> | "router" | undefined;
32
+ toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
32
33
  name?: string | undefined;
33
- description?: string | undefined;
34
34
  model?: ChatModel | undefined;
35
35
  instructions?: string | PromptBuilder | undefined;
36
36
  outputKey?: string | undefined;
@@ -42,10 +42,10 @@ export declare const aiAgentOptionsSchema: z.ZodObject<{
42
42
  disableLogging?: boolean | undefined;
43
43
  memory?: any;
44
44
  }, {
45
+ description?: string | undefined;
45
46
  tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
46
- toolChoice?: "auto" | "none" | "required" | Agent<Message, Message> | "router" | undefined;
47
+ toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
47
48
  name?: string | undefined;
48
- description?: string | undefined;
49
49
  model?: ChatModel | undefined;
50
50
  instructions?: string | PromptBuilder | undefined;
51
51
  outputKey?: string | undefined;
@@ -64,5 +64,5 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
64
64
  instructions: PromptBuilder;
65
65
  outputKey?: string;
66
66
  toolChoice?: AIAgentToolChoice;
67
- process(input: I, context?: Context): Promise<import("./types.js").TransferAgentOutput | O>;
67
+ process(input: I, context: Context): Promise<import("./types.js").TransferAgentOutput | O>;
68
68
  }
@@ -21,6 +21,6 @@ export declare class AgentMemory {
21
21
  memories: Memory[];
22
22
  private subscriptions;
23
23
  addMemory(memory: Memory): void;
24
- attach(context: Context): void;
24
+ attach(context: Pick<Context, "subscribe">): void;
25
25
  detach(): void;
26
26
  }