@aigne/core 1.4.0 → 1.4.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 (41) hide show
  1. package/lib/cjs/agents/ai-agent.d.ts +49 -1
  2. package/lib/cjs/agents/ai-agent.js +27 -1
  3. package/lib/cjs/agents/mcp-agent.js +30 -4
  4. package/lib/cjs/agents/memory.js +7 -0
  5. package/lib/cjs/execution-engine/execution-engine.d.ts +1 -1
  6. package/lib/cjs/execution-engine/execution-engine.js +48 -0
  7. package/lib/cjs/execution-engine/utils.d.ts +1 -1
  8. package/lib/cjs/execution-engine/utils.js +6 -0
  9. package/lib/cjs/index.d.ts +1 -0
  10. package/lib/cjs/index.js +1 -0
  11. package/lib/cjs/models/claude-chat-model.d.ts +49 -0
  12. package/lib/cjs/models/claude-chat-model.js +18 -1
  13. package/lib/cjs/models/openai-chat-model.d.ts +52 -0
  14. package/lib/cjs/models/openai-chat-model.js +19 -1
  15. package/lib/cjs/utils/type-utils.d.ts +2 -0
  16. package/lib/cjs/utils/type-utils.js +44 -0
  17. package/lib/dts/agents/ai-agent.d.ts +49 -1
  18. package/lib/dts/execution-engine/execution-engine.d.ts +1 -1
  19. package/lib/dts/execution-engine/utils.d.ts +1 -1
  20. package/lib/dts/index.d.ts +1 -0
  21. package/lib/dts/models/claude-chat-model.d.ts +49 -0
  22. package/lib/dts/models/openai-chat-model.d.ts +52 -0
  23. package/lib/dts/utils/type-utils.d.ts +2 -0
  24. package/lib/esm/agents/ai-agent.d.ts +49 -1
  25. package/lib/esm/agents/ai-agent.js +26 -0
  26. package/lib/esm/agents/mcp-agent.js +31 -5
  27. package/lib/esm/agents/memory.js +8 -1
  28. package/lib/esm/execution-engine/execution-engine.d.ts +1 -1
  29. package/lib/esm/execution-engine/execution-engine.js +48 -0
  30. package/lib/esm/execution-engine/utils.d.ts +1 -1
  31. package/lib/esm/execution-engine/utils.js +6 -0
  32. package/lib/esm/index.d.ts +1 -0
  33. package/lib/esm/index.js +1 -0
  34. package/lib/esm/models/claude-chat-model.d.ts +49 -0
  35. package/lib/esm/models/claude-chat-model.js +18 -1
  36. package/lib/esm/models/openai-chat-model.d.ts +52 -0
  37. package/lib/esm/models/openai-chat-model.js +19 -1
  38. package/lib/esm/utils/type-utils.d.ts +2 -0
  39. package/lib/esm/utils/type-utils.js +43 -0
  40. package/package.json +8 -6
  41. package/pkce-challenge-4.1.0.tgz +0 -0
@@ -1,5 +1,6 @@
1
+ import { z } from "zod";
1
2
  import type { Context } from "../execution-engine/context.js";
2
- import type { ChatModel } from "../models/chat-model.js";
3
+ import { ChatModel } from "../models/chat-model.js";
3
4
  import { PromptBuilder } from "../prompt/prompt-builder.js";
4
5
  import { Agent, type AgentOptions, type Message } from "./agent.js";
5
6
  export interface AIAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
@@ -9,6 +10,53 @@ export interface AIAgentOptions<I extends Message = Message, O extends Message =
9
10
  toolChoice?: AIAgentToolChoice;
10
11
  }
11
12
  export type AIAgentToolChoice = "auto" | "none" | "required" | "router" | Agent;
13
+ export declare const aiAgentToolChoiceSchema: z.ZodUnion<[z.ZodLiteral<"auto">, z.ZodLiteral<"none">, z.ZodLiteral<"required">, z.ZodLiteral<"router">, z.ZodType<Agent<Message, Message>, z.ZodTypeDef, Agent<Message, Message>>]>;
14
+ export declare const aiAgentOptionsSchema: z.ZodObject<{
15
+ model: z.ZodOptional<z.ZodType<ChatModel, z.ZodTypeDef, ChatModel>>;
16
+ instructions: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodType<PromptBuilder, z.ZodTypeDef, PromptBuilder>]>>;
17
+ outputKey: z.ZodOptional<z.ZodString>;
18
+ toolChoice: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"auto">, z.ZodLiteral<"none">, z.ZodLiteral<"required">, z.ZodLiteral<"router">, z.ZodType<Agent<Message, Message>, z.ZodTypeDef, Agent<Message, Message>>]>>;
19
+ enableHistory: z.ZodOptional<z.ZodBoolean>;
20
+ maxHistoryMessages: z.ZodOptional<z.ZodNumber>;
21
+ includeInputInOutput: z.ZodOptional<z.ZodBoolean>;
22
+ subscribeTopic: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
23
+ publishTopic: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">, z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>]>>;
24
+ name: z.ZodOptional<z.ZodString>;
25
+ description: z.ZodOptional<z.ZodString>;
26
+ tools: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodType<Agent<Message, Message>, z.ZodTypeDef, Agent<Message, Message>>, z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>]>, "many">>;
27
+ disableLogging: z.ZodOptional<z.ZodBoolean>;
28
+ memory: z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodAny, z.ZodAny]>>;
29
+ }, "strip", z.ZodTypeAny, {
30
+ tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
31
+ toolChoice?: "auto" | "none" | "required" | Agent<Message, Message> | "router" | undefined;
32
+ name?: string | undefined;
33
+ description?: string | undefined;
34
+ model?: ChatModel | undefined;
35
+ instructions?: string | PromptBuilder | undefined;
36
+ outputKey?: string | undefined;
37
+ enableHistory?: boolean | undefined;
38
+ maxHistoryMessages?: number | undefined;
39
+ includeInputInOutput?: boolean | undefined;
40
+ subscribeTopic?: string | string[] | undefined;
41
+ publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
42
+ disableLogging?: boolean | undefined;
43
+ memory?: any;
44
+ }, {
45
+ tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
46
+ toolChoice?: "auto" | "none" | "required" | Agent<Message, Message> | "router" | undefined;
47
+ name?: string | undefined;
48
+ description?: string | undefined;
49
+ model?: ChatModel | undefined;
50
+ instructions?: string | PromptBuilder | undefined;
51
+ outputKey?: string | undefined;
52
+ enableHistory?: boolean | undefined;
53
+ maxHistoryMessages?: number | undefined;
54
+ includeInputInOutput?: boolean | undefined;
55
+ subscribeTopic?: string | string[] | undefined;
56
+ publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
57
+ disableLogging?: boolean | undefined;
58
+ memory?: any;
59
+ }>;
12
60
  export declare class AIAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
13
61
  static from<I extends Message, O extends Message>(options: AIAgentOptions<I, O>): AIAgent<I, O>;
14
62
  constructor(options: AIAgentOptions<I, O>);
@@ -1,15 +1,41 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AIAgent = void 0;
3
+ exports.AIAgent = exports.aiAgentOptionsSchema = exports.aiAgentToolChoiceSchema = void 0;
4
+ const zod_1 = require("zod");
5
+ const chat_model_js_1 = require("../models/chat-model.js");
4
6
  const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
5
7
  const template_js_1 = require("../prompt/template.js");
6
8
  const agent_js_1 = require("./agent.js");
7
9
  const types_js_1 = require("./types.js");
10
+ exports.aiAgentToolChoiceSchema = zod_1.z.union([
11
+ zod_1.z.literal("auto"),
12
+ zod_1.z.literal("none"),
13
+ zod_1.z.literal("required"),
14
+ zod_1.z.literal("router"),
15
+ zod_1.z.instanceof(agent_js_1.Agent),
16
+ ], { message: "aiAgentToolChoice must be 'auto', 'none', 'required', 'router', or an Agent" });
17
+ exports.aiAgentOptionsSchema = zod_1.z.object({
18
+ model: zod_1.z.instanceof(chat_model_js_1.ChatModel).optional(),
19
+ instructions: zod_1.z.union([zod_1.z.string(), zod_1.z.instanceof(prompt_builder_js_1.PromptBuilder)]).optional(),
20
+ outputKey: zod_1.z.string().optional(),
21
+ toolChoice: exports.aiAgentToolChoiceSchema.optional(),
22
+ enableHistory: zod_1.z.boolean().optional(),
23
+ maxHistoryMessages: zod_1.z.number().optional(),
24
+ includeInputInOutput: zod_1.z.boolean().optional(),
25
+ subscribeTopic: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]).optional(),
26
+ publishTopic: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string()), zod_1.z.function()]).optional(),
27
+ name: zod_1.z.string().optional(),
28
+ description: zod_1.z.string().optional(),
29
+ tools: zod_1.z.array(zod_1.z.union([zod_1.z.instanceof(agent_js_1.Agent), zod_1.z.function()])).optional(),
30
+ disableLogging: zod_1.z.boolean().optional(),
31
+ memory: zod_1.z.union([zod_1.z.boolean(), zod_1.z.any(), zod_1.z.any()]).optional(),
32
+ });
8
33
  class AIAgent extends agent_js_1.Agent {
9
34
  static from(options) {
10
35
  return new AIAgent(options);
11
36
  }
12
37
  constructor(options) {
38
+ exports.aiAgentOptionsSchema.parse(options);
13
39
  super(options);
14
40
  this.model = options.model;
15
41
  this.instructions =
@@ -5,6 +5,7 @@ const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js");
5
5
  const sse_js_1 = require("@modelcontextprotocol/sdk/client/sse.js");
6
6
  const stdio_js_1 = require("@modelcontextprotocol/sdk/client/stdio.js");
7
7
  const uriTemplate_js_1 = require("@modelcontextprotocol/sdk/shared/uriTemplate.js");
8
+ const zod_1 = require("zod");
8
9
  const logger_js_1 = require("../utils/logger.js");
9
10
  const mcp_utils_js_1 = require("../utils/mcp-utils.js");
10
11
  const type_utils_js_1 = require("../utils/type-utils.js");
@@ -18,11 +19,21 @@ function isSSEServerParameters(options) {
18
19
  function isStdioServerParameters(options) {
19
20
  return "command" in options && typeof options.command === "string";
20
21
  }
22
+ function getMCPServerString(options) {
23
+ if (isSSEServerParameters(options)) {
24
+ return options.url;
25
+ }
26
+ if (isStdioServerParameters(options)) {
27
+ return `${options.command} ${options.args?.join(" ") || ""}`;
28
+ }
29
+ return "unknown";
30
+ }
21
31
  class MCPAgent extends agent_js_1.Agent {
22
32
  static from(options) {
33
+ (0, type_utils_js_1.checkArguments)("MCPAgent.from", mcpAgentOptionsSchema, options);
23
34
  if (isSSEServerParameters(options)) {
24
35
  const transport = new sse_js_1.SSEClientTransport(new URL(options.url));
25
- return MCPAgent.fromTransport(transport);
36
+ return MCPAgent.fromTransport(transport, options);
26
37
  }
27
38
  if (isStdioServerParameters(options)) {
28
39
  const transport = new stdio_js_1.StdioClientTransport({
@@ -33,16 +44,16 @@ class MCPAgent extends agent_js_1.Agent {
33
44
  },
34
45
  stderr: "pipe",
35
46
  });
36
- return MCPAgent.fromTransport(transport);
47
+ return MCPAgent.fromTransport(transport, options);
37
48
  }
38
49
  return new MCPAgent(options);
39
50
  }
40
- static async fromTransport(transport) {
51
+ static async fromTransport(transport, options) {
41
52
  const client = new index_js_1.Client({
42
53
  name: MCP_AGENT_CLIENT_NAME,
43
54
  version: MCP_AGENT_CLIENT_VERSION,
44
55
  });
45
- await debug.spinner(client.connect(transport), "Connecting to MCP server");
56
+ await debug.spinner(client.connect(transport), `Connecting to MCP server: ${getMCPServerString(options)}`);
46
57
  const mcpServer = getMCPServerName(client);
47
58
  const { tools: isToolsAvailable, prompts: isPromptsAvailable, resources: isResourcesAvailable, } = client.getServerCapabilities() ?? {};
48
59
  const tools = isToolsAvailable
@@ -142,3 +153,18 @@ function getMCPServerName(client) {
142
153
  const { name, version } = info;
143
154
  return `${name}@${version}`;
144
155
  }
156
+ const mcpAgentOptionsSchema = zod_1.z.union([
157
+ zod_1.z.object({
158
+ client: zod_1.z.instanceof(index_js_1.Client),
159
+ prompts: zod_1.z.array(zod_1.z.instanceof(MCPPrompt)).optional(),
160
+ resources: zod_1.z.array(zod_1.z.instanceof(MCPResource)).optional(),
161
+ }),
162
+ zod_1.z.object({
163
+ url: zod_1.z.string(),
164
+ }),
165
+ zod_1.z.object({
166
+ command: zod_1.z.string(),
167
+ args: zod_1.z.array(zod_1.z.string()).optional(),
168
+ env: zod_1.z.record(zod_1.z.string()).optional(),
169
+ }),
170
+ ]);
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AgentMemory = void 0;
4
+ const zod_1 = require("zod");
4
5
  const type_utils_js_1 = require("../utils/type-utils.js");
5
6
  class AgentMemory {
6
7
  constructor(options) {
8
+ (0, type_utils_js_1.checkArguments)("AgentMemory", agentMemoryOptionsSchema, options);
7
9
  this.enabled = options.enabled ?? true;
8
10
  this.subscribeTopic = options.subscribeTopic;
9
11
  this.maxMemoriesInChat = options.maxMemoriesInChat;
@@ -36,3 +38,8 @@ class AgentMemory {
36
38
  }
37
39
  }
38
40
  exports.AgentMemory = AgentMemory;
41
+ const agentMemoryOptionsSchema = zod_1.z.object({
42
+ enabled: zod_1.z.boolean().optional(),
43
+ subscribeTopic: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]).optional(),
44
+ maxMemoriesInChat: zod_1.z.number().optional(),
45
+ });
@@ -1,7 +1,7 @@
1
1
  import EventEmitter from "node:events";
2
2
  import { Agent, type Message } from "../agents/agent.js";
3
3
  import { UserAgent } from "../agents/user-agent.js";
4
- import type { ChatModel } from "../models/chat-model.js";
4
+ import { ChatModel } from "../models/chat-model.js";
5
5
  import type { Context, Runnable } from "./context.js";
6
6
  import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
7
7
  export interface ExecutionEngineOptions {
@@ -6,13 +6,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.ExecutionEngine = void 0;
7
7
  const node_events_1 = __importDefault(require("node:events"));
8
8
  const lodash_es_1 = require("lodash-es");
9
+ const zod_1 = require("zod");
9
10
  const agent_js_1 = require("../agents/agent.js");
10
11
  const types_js_1 = require("../agents/types.js");
11
12
  const user_agent_js_1 = require("../agents/user-agent.js");
13
+ const chat_model_js_1 = require("../models/chat-model.js");
12
14
  const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
15
+ const type_utils_js_1 = require("../utils/type-utils.js");
13
16
  const message_queue_js_1 = require("./message-queue.js");
14
17
  class ExecutionEngine extends node_events_1.default {
15
18
  constructor(options) {
19
+ if (options)
20
+ (0, type_utils_js_1.checkArguments)("ExecutionEngine", executionEngineOptionsSchema, options);
16
21
  super();
17
22
  this.model = options?.model;
18
23
  this.tools = options?.tools ?? [];
@@ -25,6 +30,7 @@ class ExecutionEngine extends node_events_1.default {
25
30
  tools;
26
31
  agents = [];
27
32
  addAgent(...agents) {
33
+ (0, type_utils_js_1.checkArguments)("ExecutionEngine.addAgent", executionEngineAddAgentArgsSchema, agents);
28
34
  for (const agent of agents) {
29
35
  this.agents.push(agent);
30
36
  agent.attach(this);
@@ -40,6 +46,11 @@ class ExecutionEngine extends node_events_1.default {
40
46
  * @param from the agent who publish the message, if not provided, it will be treated as a user message
41
47
  */
42
48
  publish(topic, message, from) {
49
+ (0, type_utils_js_1.checkArguments)("ExecutionEngine.publish", executionEnginePublishArgsSchema, {
50
+ topic,
51
+ message,
52
+ from,
53
+ });
43
54
  const request = {
44
55
  role: !from || from instanceof user_agent_js_1.UserAgent ? "user" : "agent",
45
56
  source: from?.name,
@@ -48,6 +59,11 @@ class ExecutionEngine extends node_events_1.default {
48
59
  this.messageQueue.publish(topic, request);
49
60
  }
50
61
  call(agent, message, options) {
62
+ (0, type_utils_js_1.checkArguments)("ExecutionEngine.call", executionEngineCallArgsSchema, {
63
+ agent,
64
+ message,
65
+ options,
66
+ });
51
67
  if ((0, lodash_es_1.isNil)(message)) {
52
68
  let activeAgent = agent;
53
69
  return user_agent_js_1.UserAgent.from({
@@ -82,9 +98,17 @@ class ExecutionEngine extends node_events_1.default {
82
98
  return { output, agent: activeAgent };
83
99
  }
84
100
  subscribe(topic, listener) {
101
+ (0, type_utils_js_1.checkArguments)("ExecutionEngine.subscribe", executionEngineSubscribeArgsSchema, {
102
+ topic,
103
+ listener,
104
+ });
85
105
  return this.messageQueue.subscribe(topic, listener);
86
106
  }
87
107
  unsubscribe(topic, listener) {
108
+ (0, type_utils_js_1.checkArguments)("ExecutionEngine.unsubscribe", executionEngineUnsubscribeArgsSchema, {
109
+ topic,
110
+ listener,
111
+ });
88
112
  this.messageQueue.unsubscribe(topic, listener);
89
113
  }
90
114
  async callAgent(agent, input) {
@@ -134,3 +158,27 @@ class ExecutionEngine extends node_events_1.default {
134
158
  }
135
159
  }
136
160
  exports.ExecutionEngine = ExecutionEngine;
161
+ const executionEngineOptionsSchema = zod_1.z.object({
162
+ model: zod_1.z.instanceof(chat_model_js_1.ChatModel).optional(),
163
+ tools: zod_1.z.array(zod_1.z.instanceof(agent_js_1.Agent)).optional(),
164
+ agents: zod_1.z.array(zod_1.z.instanceof(agent_js_1.Agent)).optional(),
165
+ });
166
+ 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
+ const executionEngineSubscribeArgsSchema = zod_1.z.object({
178
+ topic: zod_1.z.string(),
179
+ listener: zod_1.z.function(zod_1.z.tuple([zod_1.z.any()]), zod_1.z.any()).optional(),
180
+ });
181
+ const executionEngineUnsubscribeArgsSchema = zod_1.z.object({
182
+ topic: zod_1.z.string(),
183
+ listener: zod_1.z.function(zod_1.z.tuple([zod_1.z.any()]), zod_1.z.any()),
184
+ });
@@ -1,4 +1,4 @@
1
- import type { FunctionAgentFn } from "../agents/agent.js";
1
+ import { type FunctionAgentFn } from "../agents/agent.js";
2
2
  import type { Runnable } from "./context.js";
3
3
  export declare function sequential(..._agents: [Runnable, ...Runnable[]]): FunctionAgentFn;
4
4
  export declare function parallel(..._agents: [Runnable, ...Runnable[]]): FunctionAgentFn;
@@ -2,7 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.sequential = sequential;
4
4
  exports.parallel = parallel;
5
+ const zod_1 = require("zod");
6
+ const agent_js_1 = require("../agents/agent.js");
7
+ const type_utils_js_1 = require("../utils/type-utils.js");
5
8
  function sequential(..._agents) {
9
+ (0, type_utils_js_1.checkArguments)("sequential", agentArraySchema, _agents);
6
10
  let agents = [..._agents];
7
11
  return async (input, context) => {
8
12
  if (!context)
@@ -20,6 +24,7 @@ function sequential(..._agents) {
20
24
  };
21
25
  }
22
26
  function parallel(..._agents) {
27
+ (0, type_utils_js_1.checkArguments)("parallel", agentArraySchema, _agents);
23
28
  let agents = [..._agents];
24
29
  return async (input, context) => {
25
30
  if (!context)
@@ -30,3 +35,4 @@ function parallel(..._agents) {
30
35
  return Object.assign({}, ...outputs);
31
36
  };
32
37
  }
38
+ const agentArraySchema = zod_1.z.array(zod_1.z.union([zod_1.z.function(), zod_1.z.instanceof(agent_js_1.Agent)]));
@@ -14,3 +14,4 @@ export * from "./prompt/template.js";
14
14
  export * from "./utils/json-schema.js";
15
15
  export * from "./utils/logger.js";
16
16
  export * from "./utils/run-chat-loop.js";
17
+ export * from "./utils/type-utils.js";
package/lib/cjs/index.js CHANGED
@@ -30,3 +30,4 @@ __exportStar(require("./prompt/template.js"), exports);
30
30
  __exportStar(require("./utils/json-schema.js"), exports);
31
31
  __exportStar(require("./utils/logger.js"), exports);
32
32
  __exportStar(require("./utils/run-chat-loop.js"), exports);
33
+ __exportStar(require("./utils/type-utils.js"), exports);
@@ -1,10 +1,59 @@
1
1
  import Anthropic from "@anthropic-ai/sdk";
2
+ import { z } from "zod";
2
3
  import { ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "./chat-model.js";
3
4
  export interface ClaudeChatModelOptions {
4
5
  apiKey?: string;
5
6
  model?: string;
6
7
  modelOptions?: ChatModelOptions;
7
8
  }
9
+ export declare const claudeChatModelOptionsSchema: z.ZodObject<{
10
+ apiKey: z.ZodOptional<z.ZodString>;
11
+ model: z.ZodOptional<z.ZodString>;
12
+ modelOptions: z.ZodOptional<z.ZodObject<{
13
+ model: z.ZodOptional<z.ZodString>;
14
+ temperature: z.ZodOptional<z.ZodNumber>;
15
+ topP: z.ZodOptional<z.ZodNumber>;
16
+ frequencyPenalty: z.ZodOptional<z.ZodNumber>;
17
+ presencePenalty: z.ZodOptional<z.ZodNumber>;
18
+ parallelToolCalls: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
19
+ }, "strip", z.ZodTypeAny, {
20
+ parallelToolCalls: boolean;
21
+ model?: string | undefined;
22
+ temperature?: number | undefined;
23
+ topP?: number | undefined;
24
+ frequencyPenalty?: number | undefined;
25
+ presencePenalty?: number | undefined;
26
+ }, {
27
+ model?: string | undefined;
28
+ temperature?: number | undefined;
29
+ topP?: number | undefined;
30
+ frequencyPenalty?: number | undefined;
31
+ presencePenalty?: number | undefined;
32
+ parallelToolCalls?: boolean | undefined;
33
+ }>>;
34
+ }, "strip", z.ZodTypeAny, {
35
+ modelOptions?: {
36
+ parallelToolCalls: boolean;
37
+ model?: string | undefined;
38
+ temperature?: number | undefined;
39
+ topP?: number | undefined;
40
+ frequencyPenalty?: number | undefined;
41
+ presencePenalty?: number | undefined;
42
+ } | undefined;
43
+ model?: string | undefined;
44
+ apiKey?: string | undefined;
45
+ }, {
46
+ modelOptions?: {
47
+ model?: string | undefined;
48
+ temperature?: number | undefined;
49
+ topP?: number | undefined;
50
+ frequencyPenalty?: number | undefined;
51
+ presencePenalty?: number | undefined;
52
+ parallelToolCalls?: boolean | undefined;
53
+ } | undefined;
54
+ model?: string | undefined;
55
+ apiKey?: string | undefined;
56
+ }>;
8
57
  export declare class ClaudeChatModel extends ChatModel {
9
58
  options?: ClaudeChatModelOptions | undefined;
10
59
  constructor(options?: ClaudeChatModelOptions | undefined);
@@ -3,17 +3,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ClaudeChatModel = void 0;
6
+ exports.ClaudeChatModel = exports.claudeChatModelOptionsSchema = void 0;
7
7
  const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
8
8
  const lodash_es_1 = require("lodash-es");
9
+ const zod_1 = require("zod");
9
10
  const json_schema_js_1 = require("../utils/json-schema.js");
10
11
  const logger_js_1 = require("../utils/logger.js");
11
12
  const type_utils_js_1 = require("../utils/type-utils.js");
12
13
  const chat_model_js_1 = require("./chat-model.js");
13
14
  const CHAT_MODEL_CLAUDE_DEFAULT_MODEL = "claude-3-7-sonnet-latest";
15
+ exports.claudeChatModelOptionsSchema = zod_1.z.object({
16
+ apiKey: zod_1.z.string().optional(),
17
+ model: zod_1.z.string().optional(),
18
+ modelOptions: zod_1.z
19
+ .object({
20
+ model: zod_1.z.string().optional(),
21
+ temperature: zod_1.z.number().optional(),
22
+ topP: zod_1.z.number().optional(),
23
+ frequencyPenalty: zod_1.z.number().optional(),
24
+ presencePenalty: zod_1.z.number().optional(),
25
+ parallelToolCalls: zod_1.z.boolean().optional().default(true),
26
+ })
27
+ .optional(),
28
+ });
14
29
  class ClaudeChatModel extends chat_model_js_1.ChatModel {
15
30
  options;
16
31
  constructor(options) {
32
+ if (options)
33
+ (0, type_utils_js_1.checkArguments)("ClaudeChatModel", exports.claudeChatModelOptionsSchema, options);
17
34
  super();
18
35
  this.options = options;
19
36
  }
@@ -1,3 +1,4 @@
1
+ import { z } from "zod";
1
2
  import { ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "./chat-model.js";
2
3
  export interface OpenAIChatModelOptions {
3
4
  apiKey?: string;
@@ -5,6 +6,57 @@ export interface OpenAIChatModelOptions {
5
6
  model?: string;
6
7
  modelOptions?: ChatModelOptions;
7
8
  }
9
+ export declare const openAIChatModelOptionsSchema: z.ZodObject<{
10
+ apiKey: z.ZodOptional<z.ZodString>;
11
+ baseURL: z.ZodOptional<z.ZodString>;
12
+ model: z.ZodOptional<z.ZodString>;
13
+ modelOptions: z.ZodOptional<z.ZodObject<{
14
+ model: z.ZodOptional<z.ZodString>;
15
+ temperature: z.ZodOptional<z.ZodNumber>;
16
+ topP: z.ZodOptional<z.ZodNumber>;
17
+ frequencyPenalty: z.ZodOptional<z.ZodNumber>;
18
+ presencePenalty: z.ZodOptional<z.ZodNumber>;
19
+ parallelToolCalls: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
20
+ }, "strip", z.ZodTypeAny, {
21
+ parallelToolCalls: boolean;
22
+ model?: string | undefined;
23
+ temperature?: number | undefined;
24
+ topP?: number | undefined;
25
+ frequencyPenalty?: number | undefined;
26
+ presencePenalty?: number | undefined;
27
+ }, {
28
+ model?: string | undefined;
29
+ temperature?: number | undefined;
30
+ topP?: number | undefined;
31
+ frequencyPenalty?: number | undefined;
32
+ presencePenalty?: number | undefined;
33
+ parallelToolCalls?: boolean | undefined;
34
+ }>>;
35
+ }, "strip", z.ZodTypeAny, {
36
+ modelOptions?: {
37
+ parallelToolCalls: boolean;
38
+ model?: string | undefined;
39
+ temperature?: number | undefined;
40
+ topP?: number | undefined;
41
+ frequencyPenalty?: number | undefined;
42
+ presencePenalty?: number | undefined;
43
+ } | undefined;
44
+ model?: string | undefined;
45
+ apiKey?: string | undefined;
46
+ baseURL?: string | undefined;
47
+ }, {
48
+ modelOptions?: {
49
+ model?: string | undefined;
50
+ temperature?: number | undefined;
51
+ topP?: number | undefined;
52
+ frequencyPenalty?: number | undefined;
53
+ presencePenalty?: number | undefined;
54
+ parallelToolCalls?: boolean | undefined;
55
+ } | undefined;
56
+ model?: string | undefined;
57
+ apiKey?: string | undefined;
58
+ baseURL?: string | undefined;
59
+ }>;
8
60
  export declare class OpenAIChatModel extends ChatModel {
9
61
  options?: OpenAIChatModelOptions | undefined;
10
62
  constructor(options?: OpenAIChatModelOptions | undefined);
@@ -3,16 +3,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.OpenAIChatModel = void 0;
6
+ exports.OpenAIChatModel = exports.openAIChatModelOptionsSchema = void 0;
7
7
  const nanoid_1 = require("nanoid");
8
8
  const openai_1 = __importDefault(require("openai"));
9
+ const zod_1 = require("zod");
9
10
  const json_schema_js_1 = require("../utils/json-schema.js");
10
11
  const type_utils_js_1 = require("../utils/type-utils.js");
11
12
  const chat_model_js_1 = require("./chat-model.js");
12
13
  const CHAT_MODEL_OPENAI_DEFAULT_MODEL = "gpt-4o-mini";
14
+ exports.openAIChatModelOptionsSchema = zod_1.z.object({
15
+ apiKey: zod_1.z.string().optional(),
16
+ baseURL: zod_1.z.string().optional(),
17
+ model: zod_1.z.string().optional(),
18
+ modelOptions: zod_1.z
19
+ .object({
20
+ model: zod_1.z.string().optional(),
21
+ temperature: zod_1.z.number().optional(),
22
+ topP: zod_1.z.number().optional(),
23
+ frequencyPenalty: zod_1.z.number().optional(),
24
+ presencePenalty: zod_1.z.number().optional(),
25
+ parallelToolCalls: zod_1.z.boolean().optional().default(true),
26
+ })
27
+ .optional(),
28
+ });
13
29
  class OpenAIChatModel extends chat_model_js_1.ChatModel {
14
30
  options;
15
31
  constructor(options) {
32
+ if (options)
33
+ (0, type_utils_js_1.checkArguments)("OpenAIChatModel", exports.openAIChatModelOptionsSchema, options);
16
34
  super();
17
35
  this.options = options;
18
36
  }
@@ -1,3 +1,4 @@
1
+ import { type ZodType } from "zod";
1
2
  export type PromiseOrValue<T> = T | Promise<T>;
2
3
  export type Nullish<T> = T | null | undefined;
3
4
  export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
@@ -9,3 +10,4 @@ export declare function get(obj: unknown, path: string | string[], type: "number
9
10
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
10
11
  [key: string]: T;
11
12
  };
13
+ export declare function checkArguments<T>(prefix: string, schema: ZodType<T>, args: T): void;
@@ -5,7 +5,9 @@ exports.isNotEmpty = isNotEmpty;
5
5
  exports.orArrayToArray = orArrayToArray;
6
6
  exports.get = get;
7
7
  exports.createAccessorArray = createAccessorArray;
8
+ exports.checkArguments = checkArguments;
8
9
  const lodash_es_1 = require("lodash-es");
10
+ const zod_1 = require("zod");
9
11
  function isNonNullable(value) {
10
12
  return !(0, lodash_es_1.isNil)(value);
11
13
  }
@@ -29,3 +31,45 @@ function createAccessorArray(array, accessor) {
29
31
  get: (t, p, r) => Reflect.get(t, p, r) ?? accessor(array, p),
30
32
  });
31
33
  }
34
+ function checkArguments(prefix, schema, args) {
35
+ try {
36
+ schema.parse(args, {
37
+ errorMap: (issue, ctx) => {
38
+ if (issue.code === "invalid_union") {
39
+ // handle all issues that are not invalid_type
40
+ const otherQuestions = issue.unionErrors
41
+ .map(({ issues: [issue] }) => {
42
+ if (issue && issue.code !== "invalid_type") {
43
+ return issue.message || zod_1.z.defaultErrorMap(issue, ctx).message;
44
+ }
45
+ })
46
+ .filter(isNonNullable);
47
+ if (otherQuestions.length) {
48
+ return { message: otherQuestions.join(", ") };
49
+ }
50
+ // handle invalid_type issues
51
+ const expected = issue.unionErrors
52
+ .map(({ issues: [issue] }) => {
53
+ if (issue?.code === "invalid_type") {
54
+ return issue;
55
+ }
56
+ })
57
+ .filter(isNonNullable);
58
+ if (expected.length) {
59
+ return {
60
+ message: `Expected ${expected.map((i) => i.expected).join(" or ")}, received ${expected[0]?.received}`,
61
+ };
62
+ }
63
+ }
64
+ return { message: ctx.defaultError };
65
+ },
66
+ });
67
+ }
68
+ catch (error) {
69
+ if (error instanceof zod_1.z.ZodError) {
70
+ const message = error.issues.map((i) => `${i.path}: ${i.message}`).join(", ");
71
+ throw new Error(`${prefix} check arguments error: ${message}`);
72
+ }
73
+ throw error;
74
+ }
75
+ }