@aigne/core 1.1.0-beta.6 → 1.3.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.
Files changed (105) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/lib/cjs/agents/agent.d.ts +37 -33
  3. package/lib/cjs/agents/agent.js +68 -28
  4. package/lib/cjs/agents/ai-agent.d.ts +8 -12
  5. package/lib/cjs/agents/ai-agent.js +17 -35
  6. package/lib/cjs/agents/mcp-agent.d.ts +25 -9
  7. package/lib/cjs/agents/mcp-agent.js +61 -14
  8. package/lib/cjs/agents/memory.d.ts +26 -0
  9. package/lib/cjs/agents/memory.js +38 -0
  10. package/lib/cjs/agents/types.d.ts +4 -3
  11. package/lib/cjs/agents/types.js +11 -1
  12. package/lib/cjs/agents/user-agent.d.ts +24 -0
  13. package/lib/cjs/agents/user-agent.js +62 -0
  14. package/lib/cjs/execution-engine/context.d.ts +35 -5
  15. package/lib/cjs/execution-engine/execution-engine.d.ts +64 -0
  16. package/lib/cjs/execution-engine/execution-engine.js +136 -0
  17. package/lib/cjs/execution-engine/index.d.ts +4 -46
  18. package/lib/cjs/execution-engine/index.js +17 -193
  19. package/lib/cjs/execution-engine/message-queue.d.ts +17 -2
  20. package/lib/cjs/execution-engine/message-queue.js +37 -1
  21. package/lib/cjs/execution-engine/utils.d.ts +4 -0
  22. package/lib/cjs/execution-engine/utils.js +32 -0
  23. package/lib/cjs/index.d.ts +14 -12
  24. package/lib/cjs/index.js +14 -12
  25. package/lib/{dts/models/chat.d.ts → cjs/models/chat-model.d.ts} +15 -12
  26. package/lib/cjs/models/{chat.js → chat-model.js} +5 -5
  27. package/lib/cjs/models/claude-chat-model.d.ts +17 -0
  28. package/lib/cjs/models/claude-chat-model.js +199 -0
  29. package/lib/cjs/models/{chat-openai.d.ts → openai-chat-model.d.ts} +2 -2
  30. package/lib/cjs/models/{chat-openai.js → openai-chat-model.js} +8 -8
  31. package/lib/cjs/prompt/prompt-builder.d.ts +14 -14
  32. package/lib/cjs/prompt/prompt-builder.js +62 -67
  33. package/lib/cjs/prompt/template.d.ts +9 -21
  34. package/lib/cjs/prompt/template.js +3 -5
  35. package/lib/cjs/utils/json-schema.d.ts +3 -0
  36. package/lib/cjs/utils/json-schema.js +23 -0
  37. package/lib/cjs/utils/mcp-utils.d.ts +3 -2
  38. package/lib/cjs/utils/mcp-utils.js +29 -17
  39. package/lib/cjs/utils/run-chat-loop.d.ts +5 -4
  40. package/lib/cjs/utils/run-chat-loop.js +25 -4
  41. package/lib/cjs/utils/type-utils.d.ts +3 -0
  42. package/lib/cjs/utils/type-utils.js +8 -8
  43. package/lib/dts/agents/agent.d.ts +37 -33
  44. package/lib/dts/agents/ai-agent.d.ts +8 -12
  45. package/lib/dts/agents/mcp-agent.d.ts +25 -9
  46. package/lib/dts/agents/memory.d.ts +26 -0
  47. package/lib/dts/agents/types.d.ts +4 -3
  48. package/lib/dts/agents/user-agent.d.ts +24 -0
  49. package/lib/dts/execution-engine/context.d.ts +35 -5
  50. package/lib/dts/execution-engine/execution-engine.d.ts +64 -0
  51. package/lib/dts/execution-engine/index.d.ts +4 -46
  52. package/lib/dts/execution-engine/message-queue.d.ts +17 -2
  53. package/lib/dts/execution-engine/utils.d.ts +4 -0
  54. package/lib/dts/index.d.ts +14 -12
  55. package/lib/{esm/models/chat.d.ts → dts/models/chat-model.d.ts} +15 -12
  56. package/lib/dts/models/claude-chat-model.d.ts +17 -0
  57. package/lib/dts/models/{chat-openai.d.ts → openai-chat-model.d.ts} +2 -2
  58. package/lib/dts/prompt/prompt-builder.d.ts +14 -14
  59. package/lib/dts/prompt/template.d.ts +9 -21
  60. package/lib/dts/utils/json-schema.d.ts +3 -0
  61. package/lib/dts/utils/mcp-utils.d.ts +3 -2
  62. package/lib/dts/utils/run-chat-loop.d.ts +5 -4
  63. package/lib/dts/utils/type-utils.d.ts +3 -0
  64. package/lib/esm/agents/agent.d.ts +37 -33
  65. package/lib/esm/agents/agent.js +67 -24
  66. package/lib/esm/agents/ai-agent.d.ts +8 -12
  67. package/lib/esm/agents/ai-agent.js +13 -31
  68. package/lib/esm/agents/mcp-agent.d.ts +25 -9
  69. package/lib/esm/agents/mcp-agent.js +54 -8
  70. package/lib/esm/agents/memory.d.ts +26 -0
  71. package/lib/esm/agents/memory.js +34 -0
  72. package/lib/esm/agents/types.d.ts +4 -3
  73. package/lib/esm/agents/types.js +10 -1
  74. package/lib/esm/agents/user-agent.d.ts +24 -0
  75. package/lib/esm/agents/user-agent.js +58 -0
  76. package/lib/esm/execution-engine/context.d.ts +35 -5
  77. package/lib/esm/execution-engine/execution-engine.d.ts +64 -0
  78. package/lib/esm/execution-engine/execution-engine.js +129 -0
  79. package/lib/esm/execution-engine/index.d.ts +4 -46
  80. package/lib/esm/execution-engine/index.js +4 -188
  81. package/lib/esm/execution-engine/message-queue.d.ts +17 -2
  82. package/lib/esm/execution-engine/message-queue.js +37 -1
  83. package/lib/esm/execution-engine/utils.d.ts +4 -0
  84. package/lib/esm/execution-engine/utils.js +28 -0
  85. package/lib/esm/index.d.ts +14 -12
  86. package/lib/esm/index.js +14 -12
  87. package/lib/{cjs/models/chat.d.ts → esm/models/chat-model.d.ts} +15 -12
  88. package/lib/esm/models/{chat.js → chat-model.js} +4 -4
  89. package/lib/esm/models/claude-chat-model.d.ts +17 -0
  90. package/lib/esm/models/claude-chat-model.js +192 -0
  91. package/lib/esm/models/{chat-openai.d.ts → openai-chat-model.d.ts} +2 -2
  92. package/lib/esm/models/{chat-openai.js → openai-chat-model.js} +5 -5
  93. package/lib/esm/prompt/prompt-builder.d.ts +14 -14
  94. package/lib/esm/prompt/prompt-builder.js +56 -58
  95. package/lib/esm/prompt/template.d.ts +9 -21
  96. package/lib/esm/prompt/template.js +3 -5
  97. package/lib/esm/utils/json-schema.d.ts +3 -0
  98. package/lib/esm/utils/json-schema.js +20 -0
  99. package/lib/esm/utils/mcp-utils.d.ts +3 -2
  100. package/lib/esm/utils/mcp-utils.js +26 -15
  101. package/lib/esm/utils/run-chat-loop.d.ts +5 -4
  102. package/lib/esm/utils/run-chat-loop.js +25 -4
  103. package/lib/esm/utils/type-utils.d.ts +3 -0
  104. package/lib/esm/utils/type-utils.js +4 -2
  105. package/package.json +31 -30
@@ -1,196 +1,20 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
4
15
  };
5
16
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.UserAgent = exports.ExecutionEngine = void 0;
7
- const node_events_1 = __importDefault(require("node:events"));
8
- const nanoid_1 = require("nanoid");
9
- const agent_1 = require("../agents/agent");
10
- const types_1 = require("../agents/types");
11
- const prompt_builder_1 = require("../prompt/prompt-builder");
12
- const type_utils_1 = require("../utils/type-utils");
13
- const message_queue_1 = require("./message-queue");
14
- class ExecutionEngine extends node_events_1.default {
15
- constructor(options) {
16
- super();
17
- this.model = options?.model;
18
- this.tools = options?.tools ?? [];
19
- if (options?.agents?.length)
20
- this.addAgent(...options.agents);
21
- this.initProcessExitHandler();
22
- }
23
- messageQueue = new message_queue_1.MessageQueue();
24
- model;
25
- tools;
26
- agents = [];
27
- agentListeners = new Map();
28
- addAgent(...agents) {
29
- for (const agent of agents) {
30
- this.agents.push(agent);
31
- this.attachAgentSubscriptions(agent);
32
- }
33
- }
34
- attachAgentSubscriptions(agent) {
35
- for (const topic of (0, type_utils_1.orArrayToArray)(agent.subscribeTopic)) {
36
- const listener = async (input) => {
37
- try {
38
- const { output } = await this.callAgent(input, agent);
39
- const topics = typeof agent.publishTopic === "function"
40
- ? await agent.publishTopic(output)
41
- : agent.publishTopic;
42
- for (const topic of (0, type_utils_1.orArrayToArray)(topics)) {
43
- this.publish(topic, output);
44
- }
45
- }
46
- catch (error) {
47
- this.emit("error", error);
48
- }
49
- };
50
- this.subscribe(topic, listener);
51
- const listeners = this.agentListeners.get(agent) || [];
52
- listeners.push({ topic, listener });
53
- this.agentListeners.set(agent, listeners);
54
- }
55
- }
56
- publish(topic, message) {
57
- this.messageQueue.emit(topic, message);
58
- }
59
- subscribe(topic, listener) {
60
- this.messageQueue.on(topic, listener);
61
- }
62
- unsubscribe(topic, listener) {
63
- this.messageQueue.off(topic, listener);
64
- }
65
- async run(_input, _options, ..._agents) {
66
- if (_input instanceof agent_1.Agent)
67
- return this.runChatLoop(_input);
68
- const [options, agents] = this.splitOptionsAndAgents(_options, ..._agents);
69
- const input = typeof _input === "string" ? (0, prompt_builder_1.userInput)(_input) : _input;
70
- if (agents.length === 0)
71
- return this.publishUserInputTopic(input);
72
- if (options?.concurrency)
73
- return this.runParallel(input, ...agents);
74
- return this.runSequential(input, ...agents);
75
- }
76
- splitOptionsAndAgents(options, ...agents) {
77
- if (options instanceof agent_1.Agent) {
78
- return [{}, [options, ...agents]];
79
- }
80
- return [options, agents];
81
- }
82
- async runSequential(input, ...agents) {
83
- const output = {};
84
- for (const agent of agents.flat()) {
85
- const { output: o } = await this.callAgent({ ...input, ...output }, agent);
86
- Object.assign(output, o);
87
- }
88
- return output;
89
- }
90
- async runParallel(input, ...agents) {
91
- const outputs = await Promise.all(agents.map((agent) => this.callAgent(input, agent).then((res) => res.output)));
92
- return Object.assign({}, ...outputs);
93
- }
94
- async publishUserInputTopic(input) {
95
- this.publish(message_queue_1.UserInputTopic, input);
96
- // TODO: 处理超时、错误、无限循环、饿死等情况
97
- const result = await new Promise((resolve) => {
98
- this.messageQueue.on(message_queue_1.UserOutputTopic, (result) => resolve(result));
99
- });
100
- return result;
101
- }
102
- async runChatLoop(agent) {
103
- const inputStream = new TransformStream({});
104
- const inputWriter = inputStream.writable.getWriter();
105
- const userAgent = new UserAgent({
106
- async run(input) {
107
- const wait = Promise.withResolvers();
108
- inputWriter.write({ ...wait, input });
109
- return wait.promise;
110
- },
111
- });
112
- // Run the loop in a separate async function, so that we can return the userAgent
113
- (async () => {
114
- let activeAgent = agent;
115
- const reader = inputStream.readable.getReader();
116
- for (;;) {
117
- const { value, done } = await reader.read();
118
- if (done)
119
- break;
120
- const { input, resolve } = value;
121
- const { agent, output } = await this.callAgent(input, activeAgent);
122
- if (agent)
123
- activeAgent = agent;
124
- resolve(output);
125
- }
126
- })();
127
- return userAgent;
128
- }
129
- async callAgent(input, agent) {
130
- let activeAgent = agent;
131
- let output;
132
- for (;;) {
133
- output = await activeAgent.call(input, this);
134
- if ((0, types_1.isTransferAgentOutput)(output)) {
135
- // TODO: 不要修改原始对象,可能被外部丢弃
136
- const transferToolCallId = (0, nanoid_1.nanoid)();
137
- Object.assign(input, (0, prompt_builder_1.addMessagesToInput)(input, [
138
- {
139
- role: "agent",
140
- name: agent.name,
141
- toolCalls: [
142
- {
143
- id: transferToolCallId,
144
- type: "function",
145
- function: {
146
- name: "transfer_to_agent",
147
- arguments: {
148
- to: output[types_1.transferAgentOutputKey].agent.name,
149
- },
150
- },
151
- },
152
- ],
153
- },
154
- {
155
- role: "tool",
156
- toolCallId: transferToolCallId,
157
- content: `Transferred to ${output[types_1.transferAgentOutputKey].agent.name}. Adopt persona immediately.`,
158
- },
159
- ]));
160
- activeAgent = output[types_1.transferAgentOutputKey].agent;
161
- }
162
- else {
163
- break;
164
- }
165
- }
166
- return {
167
- agent: activeAgent,
168
- output,
169
- };
170
- }
171
- async shutdown() {
172
- for (const tool of this.tools) {
173
- await tool.shutdown();
174
- }
175
- for (const agent of this.agents) {
176
- await agent.shutdown();
177
- }
178
- }
179
- initProcessExitHandler() {
180
- const shutdownAndExit = () => this.shutdown().finally(() => process.exit(0));
181
- process.on("SIGINT", shutdownAndExit);
182
- process.on("exit", shutdownAndExit);
183
- }
184
- }
185
- exports.ExecutionEngine = ExecutionEngine;
186
- class UserAgent extends agent_1.Agent {
187
- options;
188
- constructor(options) {
189
- super({ ...options, disableLogging: true });
190
- this.options = options;
191
- }
192
- process(input) {
193
- return this.options.run(input);
194
- }
195
- }
196
- exports.UserAgent = UserAgent;
17
+ __exportStar(require("./context.js"), exports);
18
+ __exportStar(require("./execution-engine.js"), exports);
19
+ __exportStar(require("./message-queue.js"), exports);
20
+ __exportStar(require("./utils.js"), exports);
@@ -1,5 +1,20 @@
1
- import { EventEmitter } from "node:events";
1
+ import type { Message } from "../agents/agent.js";
2
2
  export declare const UserInputTopic = "UserInputTopic";
3
3
  export declare const UserOutputTopic = "UserOutputTopic";
4
- export declare class MessageQueue extends EventEmitter {
4
+ export interface MessagePayload {
5
+ role: "user" | "agent";
6
+ source?: string;
7
+ message: Message;
8
+ }
9
+ export type MessageQueueListener = (message: MessagePayload) => void;
10
+ export type MessageRequest = MessagePayload;
11
+ export type Unsubscribe = () => void;
12
+ export declare class MessageQueue {
13
+ private events;
14
+ publish(topic: string | string[], message: MessageRequest): void;
15
+ error(error: Error): void;
16
+ subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
17
+ subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
18
+ subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
19
+ unsubscribe(topic: string, listener: MessageQueueListener): void;
5
20
  }
@@ -2,8 +2,44 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MessageQueue = exports.UserOutputTopic = exports.UserInputTopic = void 0;
4
4
  const node_events_1 = require("node:events");
5
+ const type_utils_js_1 = require("../utils/type-utils.js");
5
6
  exports.UserInputTopic = "UserInputTopic";
6
7
  exports.UserOutputTopic = "UserOutputTopic";
7
- class MessageQueue extends node_events_1.EventEmitter {
8
+ class MessageQueue {
9
+ events = new node_events_1.EventEmitter();
10
+ publish(topic, message) {
11
+ for (const t of (0, type_utils_js_1.orArrayToArray)(topic)) {
12
+ this.events.emit(t, message);
13
+ }
14
+ }
15
+ error(error) {
16
+ this.events.emit("error", error);
17
+ }
18
+ subscribe(topic, listener) {
19
+ if (!listener) {
20
+ return new Promise((resolve, reject) => {
21
+ const unsubscribe1 = once(this.events, topic, (message) => {
22
+ unsubscribe2();
23
+ resolve(message);
24
+ });
25
+ const unsubscribe2 = once(this.events, "error", (error) => {
26
+ unsubscribe1();
27
+ reject(error);
28
+ });
29
+ });
30
+ }
31
+ return on(this.events, topic, listener);
32
+ }
33
+ unsubscribe(topic, listener) {
34
+ this.events.off(topic, listener);
35
+ }
8
36
  }
9
37
  exports.MessageQueue = MessageQueue;
38
+ function on(events, event, listener) {
39
+ events.on(event, listener);
40
+ return () => events.off(event, listener);
41
+ }
42
+ function once(events, event, listener) {
43
+ events.once(event, listener);
44
+ return () => events.off(event, listener);
45
+ }
@@ -0,0 +1,4 @@
1
+ import type { FunctionAgentFn } from "../agents/agent.js";
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;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sequential = sequential;
4
+ exports.parallel = parallel;
5
+ function sequential(..._agents) {
6
+ let agents = [..._agents];
7
+ return async (input, context) => {
8
+ if (!context)
9
+ throw new Error("Context is required for executing sequential agents. Please provide a valid context.");
10
+ const output = {};
11
+ // Clone the agents to run, so that we can update the agents list during the loop
12
+ const agentsToRun = [...agents];
13
+ agents = [];
14
+ for (const agent of agentsToRun) {
15
+ const [o, transferToAgent] = await context.call(agent, { ...input, ...output }, { returnActiveAgent: true });
16
+ Object.assign(output, o);
17
+ agents.push(transferToAgent);
18
+ }
19
+ return output;
20
+ };
21
+ }
22
+ function parallel(..._agents) {
23
+ let agents = [..._agents];
24
+ return async (input, context) => {
25
+ if (!context)
26
+ throw new Error("Context is required for executing parallel agents. Please provide a valid context.");
27
+ const result = await Promise.all(agents.map((agent) => context.call(agent, input, { returnActiveAgent: true })));
28
+ agents = result.map((i) => i[1]);
29
+ const outputs = result.map((i) => i[0]);
30
+ return Object.assign({}, ...outputs);
31
+ };
32
+ }
@@ -1,12 +1,14 @@
1
- export * from "./agents/agent";
2
- export * from "./agents/ai-agent";
3
- export * from "./agents/mcp-agent";
4
- export * from "./execution-engine";
5
- export * from "./execution-engine/context";
6
- export * from "./execution-engine/message-queue";
7
- export * from "./models/chat";
8
- export * from "./models/chat-openai";
9
- export * from "./prompt/prompt-builder";
10
- export * from "./prompt/template";
11
- export * from "./utils/logger";
12
- export * from "./utils/run-chat-loop";
1
+ export * from "./agents/agent.js";
2
+ export * from "./agents/ai-agent.js";
3
+ export * from "./agents/mcp-agent.js";
4
+ export * from "./agents/memory.js";
5
+ export * from "./agents/types.js";
6
+ export * from "./agents/user-agent.js";
7
+ export * from "./execution-engine/index.js";
8
+ export * from "./models/chat-model.js";
9
+ export * from "./models/claude-chat-model.js";
10
+ export * from "./models/openai-chat-model.js";
11
+ export * from "./prompt/prompt-builder.js";
12
+ export * from "./prompt/template.js";
13
+ export * from "./utils/logger.js";
14
+ export * from "./utils/run-chat-loop.js";
package/lib/cjs/index.js CHANGED
@@ -14,15 +14,17 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./agents/agent"), exports);
18
- __exportStar(require("./agents/ai-agent"), exports);
19
- __exportStar(require("./agents/mcp-agent"), exports);
20
- __exportStar(require("./execution-engine"), exports);
21
- __exportStar(require("./execution-engine/context"), exports);
22
- __exportStar(require("./execution-engine/message-queue"), exports);
23
- __exportStar(require("./models/chat"), exports);
24
- __exportStar(require("./models/chat-openai"), exports);
25
- __exportStar(require("./prompt/prompt-builder"), exports);
26
- __exportStar(require("./prompt/template"), exports);
27
- __exportStar(require("./utils/logger"), exports);
28
- __exportStar(require("./utils/run-chat-loop"), exports);
17
+ __exportStar(require("./agents/agent.js"), exports);
18
+ __exportStar(require("./agents/ai-agent.js"), exports);
19
+ __exportStar(require("./agents/mcp-agent.js"), exports);
20
+ __exportStar(require("./agents/memory.js"), exports);
21
+ __exportStar(require("./agents/types.js"), exports);
22
+ __exportStar(require("./agents/user-agent.js"), exports);
23
+ __exportStar(require("./execution-engine/index.js"), exports);
24
+ __exportStar(require("./models/chat-model.js"), exports);
25
+ __exportStar(require("./models/claude-chat-model.js"), exports);
26
+ __exportStar(require("./models/openai-chat-model.js"), exports);
27
+ __exportStar(require("./prompt/prompt-builder.js"), exports);
28
+ __exportStar(require("./prompt/template.js"), exports);
29
+ __exportStar(require("./utils/logger.js"), exports);
30
+ __exportStar(require("./utils/run-chat-loop.js"), exports);
@@ -1,8 +1,8 @@
1
- import { Agent, type AgentInput, type AgentOutput } from "../agents/agent";
1
+ import { Agent, type Message } from "../agents/agent.js";
2
2
  export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelOutput> {
3
3
  constructor();
4
4
  }
5
- export interface ChatModelInput extends AgentInput {
5
+ export interface ChatModelInput extends Message {
6
6
  messages: ChatModelInputMessage[];
7
7
  responseFormat?: ChatModelInputResponseFormat;
8
8
  tools?: ChatModelInputTool[];
@@ -12,24 +12,27 @@ export interface ChatModelInput extends AgentInput {
12
12
  export type Role = "system" | "user" | "agent" | "tool";
13
13
  export interface ChatModelInputMessage {
14
14
  role: Role;
15
- content?: string | ({
16
- type: "text";
17
- text: string;
18
- } | {
19
- type: "image_url";
20
- url: string;
21
- })[];
15
+ content?: ChatModelInputMessageContent;
22
16
  toolCalls?: {
23
17
  id: string;
24
18
  type: "function";
25
19
  function: {
26
20
  name: string;
27
- arguments: AgentInput;
21
+ arguments: Message;
28
22
  };
29
23
  }[];
30
24
  toolCallId?: string;
31
25
  name?: string;
32
26
  }
27
+ export type ChatModelInputMessageContent = string | (TextContent | ImageUrlContent)[];
28
+ export type TextContent = {
29
+ type: "text";
30
+ text: string;
31
+ };
32
+ export type ImageUrlContent = {
33
+ type: "image_url";
34
+ url: string;
35
+ };
33
36
  export type ChatModelInputResponseFormat = {
34
37
  type: "text";
35
38
  } | {
@@ -63,7 +66,7 @@ export interface ChatModelOptions {
63
66
  frequencyPenalty?: number;
64
67
  presencePenalty?: number;
65
68
  }
66
- export interface ChatModelOutput extends AgentOutput {
69
+ export interface ChatModelOutput extends Message {
67
70
  text?: string;
68
71
  json?: object;
69
72
  toolCalls?: ChatModelOutputToolCall[];
@@ -73,6 +76,6 @@ export interface ChatModelOutputToolCall {
73
76
  type: "function";
74
77
  function: {
75
78
  name: string;
76
- arguments: AgentInput;
79
+ arguments: Message;
77
80
  };
78
81
  }
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ChatModel = void 0;
4
4
  const zod_1 = require("zod");
5
- const agent_1 = require("../agents/agent");
6
- class ChatModel extends agent_1.Agent {
5
+ const agent_js_1 = require("../agents/agent.js");
6
+ class ChatModel extends agent_js_1.Agent {
7
7
  constructor() {
8
8
  super({
9
9
  inputSchema: chatModelInputSchema,
@@ -36,8 +36,8 @@ const chatModelInputMessageSchema = zod_1.z.object({
36
36
  toolCallId: zod_1.z.string().optional(),
37
37
  name: zod_1.z.string().optional(),
38
38
  });
39
- const chatModelInputResponseFormatSchema = zod_1.z.union([
40
- zod_1.z.literal("text"),
39
+ const chatModelInputResponseFormatSchema = zod_1.z.discriminatedUnion("type", [
40
+ zod_1.z.object({ type: zod_1.z.literal("text") }),
41
41
  zod_1.z.object({
42
42
  type: zod_1.z.literal("json_schema"),
43
43
  jsonSchema: zod_1.z.object({
@@ -86,6 +86,6 @@ const chatModelOutputToolCallSchema = zod_1.z.object({
86
86
  });
87
87
  const chatModelOutputSchema = zod_1.z.object({
88
88
  text: zod_1.z.string().optional(),
89
- json: zod_1.z.unknown().optional(),
89
+ json: zod_1.z.record(zod_1.z.unknown()).optional(),
90
90
  toolCalls: zod_1.z.array(chatModelOutputToolCallSchema).optional(),
91
91
  });
@@ -0,0 +1,17 @@
1
+ import Anthropic from "@anthropic-ai/sdk";
2
+ import { ChatModel, type ChatModelInput, type ChatModelOutput } from "./chat-model.js";
3
+ export declare class ClaudeChatModel extends ChatModel {
4
+ config?: {
5
+ apiKey?: string;
6
+ model?: string;
7
+ } | undefined;
8
+ constructor(config?: {
9
+ apiKey?: string;
10
+ model?: string;
11
+ } | undefined);
12
+ private _client?;
13
+ get client(): Anthropic;
14
+ process(input: ChatModelInput): Promise<ChatModelOutput>;
15
+ private extractResultFromClaudeStream;
16
+ private requestStructuredOutput;
17
+ }