@aigne/core 1.5.1-1 → 1.6.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 (71) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/lib/cjs/agents/mcp-agent.d.ts +34 -7
  3. package/lib/cjs/agents/mcp-agent.js +55 -8
  4. package/lib/cjs/agents/user-agent.d.ts +1 -0
  5. package/lib/cjs/agents/user-agent.js +2 -1
  6. package/lib/cjs/execution-engine/execution-engine.d.ts +13 -2
  7. package/lib/cjs/execution-engine/execution-engine.js +20 -3
  8. package/lib/cjs/loader/agent-js.d.ts +17 -0
  9. package/lib/cjs/loader/agent-js.js +67 -0
  10. package/lib/cjs/loader/agent-yaml.d.ts +24 -0
  11. package/lib/cjs/loader/agent-yaml.js +58 -0
  12. package/lib/cjs/loader/index.d.ts +35 -0
  13. package/lib/cjs/loader/index.js +128 -0
  14. package/lib/cjs/loader/schema.d.ts +17 -0
  15. package/lib/cjs/loader/schema.js +10 -0
  16. package/lib/cjs/models/claude-chat-model.d.ts +1 -1
  17. package/lib/cjs/models/claude-chat-model.js +3 -2
  18. package/lib/cjs/models/openai-chat-model.d.ts +1 -1
  19. package/lib/cjs/models/openai-chat-model.js +4 -3
  20. package/lib/cjs/models/xai-chat-model.d.ts +2 -0
  21. package/lib/cjs/models/xai-chat-model.js +14 -0
  22. package/lib/cjs/utils/json-schema.js +1 -1
  23. package/lib/cjs/utils/logger.d.ts +3 -1
  24. package/lib/cjs/utils/logger.js +13 -10
  25. package/lib/cjs/utils/mcp-utils.d.ts +4 -5
  26. package/lib/cjs/utils/mcp-utils.js +6 -6
  27. package/lib/cjs/utils/run-chat-loop.js +3 -1
  28. package/lib/cjs/utils/type-utils.d.ts +2 -0
  29. package/lib/cjs/utils/type-utils.js +38 -1
  30. package/lib/dts/agents/mcp-agent.d.ts +34 -7
  31. package/lib/dts/agents/user-agent.d.ts +1 -0
  32. package/lib/dts/execution-engine/execution-engine.d.ts +13 -2
  33. package/lib/dts/loader/agent-js.d.ts +17 -0
  34. package/lib/dts/loader/agent-yaml.d.ts +24 -0
  35. package/lib/dts/loader/index.d.ts +35 -0
  36. package/lib/dts/loader/schema.d.ts +17 -0
  37. package/lib/dts/models/claude-chat-model.d.ts +1 -1
  38. package/lib/dts/models/openai-chat-model.d.ts +1 -1
  39. package/lib/dts/models/xai-chat-model.d.ts +2 -0
  40. package/lib/dts/utils/logger.d.ts +3 -1
  41. package/lib/dts/utils/mcp-utils.d.ts +4 -5
  42. package/lib/dts/utils/type-utils.d.ts +2 -0
  43. package/lib/esm/agents/mcp-agent.d.ts +34 -7
  44. package/lib/esm/agents/mcp-agent.js +52 -8
  45. package/lib/esm/agents/user-agent.d.ts +1 -0
  46. package/lib/esm/agents/user-agent.js +1 -0
  47. package/lib/esm/execution-engine/execution-engine.d.ts +13 -2
  48. package/lib/esm/execution-engine/execution-engine.js +21 -4
  49. package/lib/esm/loader/agent-js.d.ts +17 -0
  50. package/lib/esm/loader/agent-js.js +31 -0
  51. package/lib/esm/loader/agent-yaml.d.ts +24 -0
  52. package/lib/esm/loader/agent-yaml.js +55 -0
  53. package/lib/esm/loader/index.d.ts +35 -0
  54. package/lib/esm/loader/index.js +123 -0
  55. package/lib/esm/loader/schema.d.ts +17 -0
  56. package/lib/esm/loader/schema.js +7 -0
  57. package/lib/esm/models/claude-chat-model.d.ts +1 -1
  58. package/lib/esm/models/claude-chat-model.js +3 -2
  59. package/lib/esm/models/openai-chat-model.d.ts +1 -1
  60. package/lib/esm/models/openai-chat-model.js +4 -3
  61. package/lib/esm/models/xai-chat-model.d.ts +2 -0
  62. package/lib/esm/models/xai-chat-model.js +11 -0
  63. package/lib/esm/utils/json-schema.js +1 -1
  64. package/lib/esm/utils/logger.d.ts +3 -1
  65. package/lib/esm/utils/logger.js +13 -10
  66. package/lib/esm/utils/mcp-utils.d.ts +4 -5
  67. package/lib/esm/utils/mcp-utils.js +6 -6
  68. package/lib/esm/utils/run-chat-loop.js +3 -1
  69. package/lib/esm/utils/type-utils.d.ts +2 -0
  70. package/lib/esm/utils/type-utils.js +36 -1
  71. package/package.json +15 -9
@@ -0,0 +1,17 @@
1
+ import { z } from "zod";
2
+ export declare const inputOutputSchema: z.ZodObject<{
3
+ type: z.ZodLiteral<"object">;
4
+ properties: z.ZodRecord<z.ZodString, z.ZodAny>;
5
+ required: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
6
+ additionalProperties: z.ZodOptional<z.ZodBoolean>;
7
+ }, "strip", z.ZodTypeAny, {
8
+ type: "object";
9
+ properties: Record<string, any>;
10
+ required?: string[] | undefined;
11
+ additionalProperties?: boolean | undefined;
12
+ }, {
13
+ type: "object";
14
+ properties: Record<string, any>;
15
+ required?: string[] | undefined;
16
+ additionalProperties?: boolean | undefined;
17
+ }>;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.inputOutputSchema = void 0;
4
+ const zod_1 = require("zod");
5
+ exports.inputOutputSchema = zod_1.z.object({
6
+ type: zod_1.z.literal("object"),
7
+ properties: zod_1.z.record(zod_1.z.any()),
8
+ required: zod_1.z.array(zod_1.z.string()).optional(),
9
+ additionalProperties: zod_1.z.boolean().optional(),
10
+ });
@@ -57,7 +57,7 @@ export declare const claudeChatModelOptionsSchema: z.ZodObject<{
57
57
  export declare class ClaudeChatModel extends ChatModel {
58
58
  options?: ClaudeChatModelOptions | undefined;
59
59
  constructor(options?: ClaudeChatModelOptions | undefined);
60
- private _client?;
60
+ protected _client?: Anthropic;
61
61
  get client(): Anthropic;
62
62
  get modelOptions(): ChatModelOptions | undefined;
63
63
  process(input: ChatModelInput): Promise<ChatModelOutput>;
@@ -36,9 +36,10 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
36
36
  }
37
37
  _client;
38
38
  get client() {
39
- if (!this.options?.apiKey)
39
+ const apiKey = this.options?.apiKey || process.env.CLAUDE_API_KEY;
40
+ if (!apiKey)
40
41
  throw new Error("Api Key is required for ClaudeChatModel");
41
- this._client ??= new sdk_1.default({ apiKey: this.options.apiKey });
42
+ this._client ??= new sdk_1.default({ apiKey });
42
43
  return this._client;
43
44
  }
44
45
  get modelOptions() {
@@ -61,7 +61,7 @@ export declare const openAIChatModelOptionsSchema: z.ZodObject<{
61
61
  export declare class OpenAIChatModel extends ChatModel {
62
62
  options?: OpenAIChatModelOptions | undefined;
63
63
  constructor(options?: OpenAIChatModelOptions | undefined);
64
- private _client?;
64
+ protected _client?: OpenAI;
65
65
  get client(): OpenAI;
66
66
  get modelOptions(): ChatModelOptions | undefined;
67
67
  process(input: ChatModelInput): Promise<ChatModelOutput>;
@@ -36,11 +36,12 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
36
36
  }
37
37
  _client;
38
38
  get client() {
39
- if (!this.options?.apiKey)
39
+ const apiKey = this.options?.apiKey || process.env.OPENAI_API_KEY;
40
+ if (!apiKey)
40
41
  throw new Error("Api Key is required for OpenAIChatModel");
41
42
  this._client ??= new openai_1.default({
42
- baseURL: this.options.baseURL,
43
- apiKey: this.options.apiKey,
43
+ baseURL: this.options?.baseURL,
44
+ apiKey,
44
45
  });
45
46
  return this._client;
46
47
  }
@@ -1,3 +1,4 @@
1
+ import OpenAI from "openai";
1
2
  import type { ChatModelOptions } from "./chat-model.js";
2
3
  import { OpenAIChatModel } from "./openai-chat-model.js";
3
4
  export interface XAIChatModelOptions {
@@ -8,4 +9,5 @@ export interface XAIChatModelOptions {
8
9
  }
9
10
  export declare class XAIChatModel extends OpenAIChatModel {
10
11
  constructor(options?: XAIChatModelOptions);
12
+ get client(): OpenAI;
11
13
  }
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.XAIChatModel = void 0;
7
+ const openai_1 = __importDefault(require("openai"));
4
8
  const openai_chat_model_js_1 = require("./openai-chat-model.js");
5
9
  const XAI_DEFAULT_CHAT_MODEL = "grok-2-latest";
6
10
  const XAI_BASE_URL = "https://api.x.ai/v1";
@@ -12,5 +16,15 @@ class XAIChatModel extends openai_chat_model_js_1.OpenAIChatModel {
12
16
  baseURL: options?.baseURL || XAI_BASE_URL,
13
17
  });
14
18
  }
19
+ get client() {
20
+ const apiKey = this.options?.apiKey || process.env.XAI_API_KEY;
21
+ if (!apiKey)
22
+ throw new Error("Api Key is required for XAIChatModel");
23
+ this._client ??= new openai_1.default({
24
+ baseURL: this.options?.baseURL,
25
+ apiKey,
26
+ });
27
+ return this._client;
28
+ }
15
29
  }
16
30
  exports.XAIChatModel = XAIChatModel;
@@ -12,7 +12,7 @@ function setAdditionPropertiesDeep(schema, additionalProperties) {
12
12
  if (Array.isArray(schema)) {
13
13
  return schema.map((s) => setAdditionPropertiesDeep(s, additionalProperties));
14
14
  }
15
- if (schema && typeof schema === "object" && !Array.isArray(schema)) {
15
+ if (schema !== null && typeof schema === "object" && !Array.isArray(schema)) {
16
16
  return Object.entries(schema).reduce((acc, [key, value]) => {
17
17
  acc[key] = setAdditionPropertiesDeep(value, additionalProperties);
18
18
  if (acc.type === "object") {
@@ -1,4 +1,5 @@
1
1
  import debug, { type Debugger } from "debug";
2
+ import type { Ora } from "ora";
2
3
  interface DebugWithSpinner extends Debugger {
3
4
  spinner<T>(promise: Promise<T>, message?: string, callback?: (result: T) => void, options?: {
4
5
  disabled?: boolean;
@@ -10,9 +11,10 @@ export declare const logger: debug.Debug & {
10
11
  debug: debug.Debug;
11
12
  default: debug.Debug;
12
13
  } & {
13
- globalSpinner: import("@aigne/ora").Ora;
14
+ globalSpinner: Ora | undefined;
14
15
  base: DebugWithSpinner;
15
16
  debug: DebugWithSpinner;
16
17
  spinner: typeof spinner;
18
+ setSpinner: (spinner: Ora) => void;
17
19
  };
18
20
  export {};
@@ -4,15 +4,14 @@ 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"));
8
7
  const debug_1 = __importDefault(require("debug"));
9
8
  debug_1.default.log = (...args) => {
10
- const { isSpinning } = globalSpinner;
9
+ const { isSpinning } = globalSpinner ?? {};
11
10
  if (isSpinning)
12
- globalSpinner.stop();
11
+ globalSpinner?.stop();
13
12
  console.log(...args);
14
13
  if (isSpinning)
15
- globalSpinner.start();
14
+ globalSpinner?.start();
16
15
  };
17
16
  function createDebugger(namespace) {
18
17
  const i = (0, debug_1.default)(namespace);
@@ -32,12 +31,12 @@ function createDebugger(namespace) {
32
31
  overrideExtend(i);
33
32
  return i;
34
33
  }
35
- const globalSpinner = (0, ora_1.default)();
34
+ let globalSpinner;
36
35
  const globalSpinnerTasks = [];
37
36
  async function spinner(promise, message, callback) {
38
37
  const task = { promise, message, callback };
39
38
  globalSpinnerTasks.push(task);
40
- globalSpinner.start(message || " ");
39
+ globalSpinner?.start(message || " ");
41
40
  await promise
42
41
  .then((result) => {
43
42
  task.result = result;
@@ -54,18 +53,18 @@ async function spinner(promise, message, callback) {
54
53
  break;
55
54
  // Recover spinner state for last running task
56
55
  if (!task.status) {
57
- globalSpinner.start(task.message || " ");
56
+ globalSpinner?.start(task.message || " ");
58
57
  break;
59
58
  }
60
59
  globalSpinnerTasks.pop();
61
60
  if (task.message) {
62
61
  if (task.status === "fail")
63
- globalSpinner.fail(task.message);
62
+ globalSpinner?.fail(task.message);
64
63
  else
65
- globalSpinner.succeed(task.message);
64
+ globalSpinner?.succeed(task.message);
66
65
  }
67
66
  else {
68
- globalSpinner.stop();
67
+ globalSpinner?.stop();
69
68
  }
70
69
  // NOTE: This is a workaround to make sure the spinner stops spinning before the next tick
71
70
  await new Promise((resolve) => setTimeout(resolve, 10));
@@ -80,4 +79,8 @@ exports.logger = Object.assign(debug_1.default, {
80
79
  base,
81
80
  debug: base.extend("core"),
82
81
  spinner,
82
+ setSpinner: (spinner) => {
83
+ globalSpinner = spinner;
84
+ exports.logger.globalSpinner = spinner;
85
+ },
83
86
  });
@@ -1,6 +1,5 @@
1
- import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
1
  import { type ListPromptsResult, type ListResourceTemplatesResult, type ListResourcesResult, type ListToolsResult } from "@modelcontextprotocol/sdk/types.js";
3
- import { MCPPrompt, MCPResource, MCPTool } from "../agents/mcp-agent.js";
4
- export declare function toolFromMCPTool(client: Client, tool: ListToolsResult["tools"][number]): MCPTool;
5
- export declare function promptFromMCPPrompt(client: Client, prompt: ListPromptsResult["prompts"][number]): MCPPrompt;
6
- export declare function resourceFromMCPResource(client: Client, resource: ListResourcesResult["resources"][number] | ListResourceTemplatesResult["resourceTemplates"][number]): MCPResource;
2
+ import { type MCPBaseOptions, MCPPrompt, MCPResource, MCPTool } from "../agents/mcp-agent.js";
3
+ export declare function toolFromMCPTool(tool: ListToolsResult["tools"][number], options: MCPBaseOptions): MCPTool;
4
+ export declare function promptFromMCPPrompt(prompt: ListPromptsResult["prompts"][number], options: MCPBaseOptions): MCPPrompt;
5
+ export declare function resourceFromMCPResource(resource: ListResourcesResult["resources"][number] | ListResourceTemplatesResult["resourceTemplates"][number], options: MCPBaseOptions): MCPResource;
@@ -8,18 +8,18 @@ const uriTemplate_js_1 = require("@modelcontextprotocol/sdk/shared/uriTemplate.j
8
8
  const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
9
9
  const zod_1 = require("zod");
10
10
  const mcp_agent_js_1 = require("../agents/mcp-agent.js");
11
- function toolFromMCPTool(client, tool) {
11
+ function toolFromMCPTool(tool, options) {
12
12
  return new mcp_agent_js_1.MCPTool({
13
- client,
13
+ ...options,
14
14
  name: tool.name,
15
15
  description: tool.description,
16
16
  inputSchema: (0, json_schema_to_zod_1.jsonSchemaToZod)(tool.inputSchema),
17
17
  outputSchema: types_js_1.CallToolResultSchema,
18
18
  });
19
19
  }
20
- function promptFromMCPPrompt(client, prompt) {
20
+ function promptFromMCPPrompt(prompt, options) {
21
21
  return new mcp_agent_js_1.MCPPrompt({
22
- client,
22
+ ...options,
23
23
  name: prompt.name,
24
24
  description: prompt.description,
25
25
  inputSchema: (0, json_schema_to_zod_1.jsonSchemaToZod)({
@@ -31,7 +31,7 @@ function promptFromMCPPrompt(client, prompt) {
31
31
  outputSchema: types_js_1.GetPromptResultSchema,
32
32
  });
33
33
  }
34
- function resourceFromMCPResource(client, resource) {
34
+ function resourceFromMCPResource(resource, options) {
35
35
  const [uri, variables] = isResourceTemplate(resource)
36
36
  ? [
37
37
  resource.uriTemplate,
@@ -40,7 +40,7 @@ function resourceFromMCPResource(client, resource) {
40
40
  ]
41
41
  : [resource.uri, []];
42
42
  return new mcp_agent_js_1.MCPResource({
43
- client,
43
+ ...options,
44
44
  name: resource.name,
45
45
  uri,
46
46
  description: resource.description,
@@ -5,8 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.runChatLoopInTerminal = runChatLoopInTerminal;
7
7
  const inquirer_1 = __importDefault(require("inquirer"));
8
+ const ora_1 = __importDefault(require("ora"));
8
9
  const logger_js_1 = require("./logger.js");
9
10
  async function runChatLoopInTerminal(userAgent, { log = console.log.bind(console), ...options } = {}) {
11
+ logger_js_1.logger.setSpinner((0, ora_1.default)());
10
12
  let isLoopExited = false;
11
13
  let prompt;
12
14
  if (options?.welcome)
@@ -19,7 +21,7 @@ async function runChatLoopInTerminal(userAgent, { log = console.log.bind(console
19
21
  if (isLoopExited)
20
22
  return;
21
23
  if (options?.onResponse)
22
- options.onResponse(output);
24
+ options.onResponse(output.message);
23
25
  else
24
26
  log(output);
25
27
  prompt?.ui.close();
@@ -5,8 +5,10 @@ export declare function isNil(value: unknown): value is null | undefined;
5
5
  export declare function isEmpty(obj: unknown): boolean;
6
6
  export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
7
7
  export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
8
+ export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[];
8
9
  export declare function orArrayToArray<T>(value?: T | T[]): T[];
9
10
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
10
11
  [key: string]: T;
11
12
  };
12
13
  export declare function checkArguments<T>(prefix: string, schema: ZodType<T>, args: T): void;
14
+ export declare function tryOrThrow<P extends PromiseOrValue<unknown>>(fn: () => P, error: string | Error | ((error: Error) => Error)): P;
@@ -4,9 +4,11 @@ exports.isNil = isNil;
4
4
  exports.isEmpty = isEmpty;
5
5
  exports.isNonNullable = isNonNullable;
6
6
  exports.isNotEmpty = isNotEmpty;
7
+ exports.duplicates = duplicates;
7
8
  exports.orArrayToArray = orArrayToArray;
8
9
  exports.createAccessorArray = createAccessorArray;
9
10
  exports.checkArguments = checkArguments;
11
+ exports.tryOrThrow = tryOrThrow;
10
12
  const zod_1 = require("zod");
11
13
  function isNil(value) {
12
14
  return value === null || value === undefined;
@@ -17,7 +19,7 @@ function isEmpty(obj) {
17
19
  if (typeof obj === "string" || Array.isArray(obj))
18
20
  return obj.length === 0;
19
21
  if (typeof obj === "object")
20
- Object.keys(obj).length === 0;
22
+ return Object.keys(obj).length === 0;
21
23
  return false;
22
24
  }
23
25
  function isNonNullable(value) {
@@ -26,6 +28,20 @@ function isNonNullable(value) {
26
28
  function isNotEmpty(arr) {
27
29
  return arr.length > 0;
28
30
  }
31
+ function duplicates(arr, key = (item) => item) {
32
+ const seen = new Set();
33
+ const duplicates = new Set();
34
+ for (const item of arr) {
35
+ const k = key(item);
36
+ if (seen.has(k)) {
37
+ duplicates.add(item);
38
+ }
39
+ else {
40
+ seen.add(k);
41
+ }
42
+ }
43
+ return Array.from(duplicates);
44
+ }
29
45
  function orArrayToArray(value) {
30
46
  if (isNil(value))
31
47
  return [];
@@ -78,3 +94,24 @@ function checkArguments(prefix, schema, args) {
78
94
  throw error;
79
95
  }
80
96
  }
97
+ function tryOrThrow(fn, error) {
98
+ const createError = (e) => {
99
+ return typeof error === "function"
100
+ ? error(e)
101
+ : typeof error === "string"
102
+ ? new Error(error)
103
+ : error;
104
+ };
105
+ try {
106
+ const result = fn();
107
+ if (result instanceof Promise) {
108
+ return result.catch((e) => {
109
+ throw createError(e);
110
+ });
111
+ }
112
+ return result;
113
+ }
114
+ catch (e) {
115
+ throw createError(e);
116
+ }
117
+ }
@@ -1,7 +1,11 @@
1
- import { Client } from "@modelcontextprotocol/sdk/client/index.js";
1
+ import { Client, type ClientOptions } from "@modelcontextprotocol/sdk/client/index.js";
2
2
  import { type StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js";
3
- import type { CallToolResult, GetPromptResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
3
+ import type { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";
4
+ import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
5
+ import type { CallToolResult, GetPromptResult, Implementation, ReadResourceResult, Request } from "@modelcontextprotocol/sdk/types.js";
6
+ import { type ZodType, z } from "zod";
4
7
  import type { Context } from "../execution-engine/context.js";
8
+ import { type PromiseOrValue } from "../utils/type-utils.js";
5
9
  import { Agent, type AgentOptions, type Message } from "./agent.js";
6
10
  export interface MCPAgentOptions extends AgentOptions {
7
11
  client: Client;
@@ -11,6 +15,16 @@ export interface MCPAgentOptions extends AgentOptions {
11
15
  export type MCPServerOptions = SSEServerParameters | StdioServerParameters;
12
16
  export type SSEServerParameters = {
13
17
  url: string;
18
+ /**
19
+ * Whether to automatically reconnect to the server if the connection is lost.
20
+ * @default 10 set to 0 to disable automatic reconnection
21
+ */
22
+ maxReconnects?: number;
23
+ /**
24
+ * A function that determines whether to reconnect to the server based on the error.
25
+ * default to reconnect on all errors.
26
+ */
27
+ shouldReconnect?: (error: Error) => boolean;
14
28
  };
15
29
  export declare class MCPAgent extends Agent {
16
30
  static from(options: MCPServerOptions): Promise<MCPAgent>;
@@ -28,12 +42,24 @@ export declare class MCPAgent extends Agent {
28
42
  process(_input: Message, _context?: Context): Promise<Message>;
29
43
  shutdown(): Promise<void>;
30
44
  }
31
- export interface MCPToolBaseOptions<I extends Message, O extends Message> extends AgentOptions<I, O> {
32
- client: Client;
45
+ export interface ClientWithReconnectOptions {
46
+ transportCreator?: () => PromiseOrValue<Transport>;
47
+ maxReconnects?: number;
48
+ shouldReconnect?: (error: Error) => boolean;
49
+ }
50
+ declare class ClientWithReconnect extends Client {
51
+ private reconnectOptions?;
52
+ constructor(info: Implementation, options?: ClientOptions, reconnectOptions?: ClientWithReconnectOptions | undefined);
53
+ private shouldReconnect;
54
+ private reconnect;
55
+ request<T extends ZodType<object>>(request: Request, resultSchema: T, options?: RequestOptions): Promise<z.infer<T>>;
56
+ }
57
+ export interface MCPBaseOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
58
+ client: ClientWithReconnect;
33
59
  }
34
60
  export declare abstract class MCPBase<I extends Message, O extends Message> extends Agent<I, O> {
35
- constructor(options: MCPToolBaseOptions<I, O>);
36
- protected client: Client;
61
+ constructor(options: MCPBaseOptions<I, O>);
62
+ protected client: ClientWithReconnect;
37
63
  protected get mcpServer(): string | undefined;
38
64
  }
39
65
  export declare class MCPTool extends MCPBase<Message, CallToolResult> {
@@ -45,7 +71,7 @@ export interface MCPPromptInput extends Message {
45
71
  export declare class MCPPrompt extends MCPBase<MCPPromptInput, GetPromptResult> {
46
72
  process(input: MCPPromptInput): Promise<GetPromptResult>;
47
73
  }
48
- export interface MCPResourceOptions extends MCPToolBaseOptions<MCPPromptInput, ReadResourceResult> {
74
+ export interface MCPResourceOptions extends MCPBaseOptions<MCPPromptInput, ReadResourceResult> {
49
75
  uri: string;
50
76
  }
51
77
  export declare class MCPResource extends MCPBase<MCPPromptInput, ReadResourceResult> {
@@ -53,3 +79,4 @@ export declare class MCPResource extends MCPBase<MCPPromptInput, ReadResourceRes
53
79
  uri: string;
54
80
  process(input: MCPPromptInput): Promise<ReadResourceResult>;
55
81
  }
82
+ export {};
@@ -1,3 +1,4 @@
1
+ import { ReadableStream } from "node:stream/web";
1
2
  import type { Context } from "../execution-engine/context.js";
2
3
  import type { MessagePayload, MessageQueueListener, Unsubscribe } from "../execution-engine/message-queue.js";
3
4
  import { type PromiseOrValue } from "../utils/type-utils.js";
@@ -5,17 +5,28 @@ import { ChatModel } from "../models/chat-model.js";
5
5
  import { type ContextLimits, ExecutionContext, type Runnable } from "./context.js";
6
6
  import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
7
7
  export interface ExecutionEngineOptions {
8
+ name?: string;
9
+ description?: string;
8
10
  model?: ChatModel;
9
11
  tools?: Agent[];
10
12
  agents?: Agent[];
11
13
  limits?: ContextLimits;
12
14
  }
13
15
  export declare class ExecutionEngine extends EventEmitter {
16
+ static load({ path, ...options }: {
17
+ path: string;
18
+ } & ExecutionEngineOptions): Promise<ExecutionEngine>;
14
19
  constructor(options?: ExecutionEngineOptions);
20
+ name?: string;
21
+ description?: string;
15
22
  readonly messageQueue: MessageQueue;
16
23
  model?: ChatModel;
17
- tools: Agent[];
18
- private agents;
24
+ readonly tools: Agent<Message, Message>[] & {
25
+ [key: string]: Agent<Message, Message>;
26
+ };
27
+ readonly agents: Agent<Message, Message>[] & {
28
+ [key: string]: Agent<Message, Message>;
29
+ };
19
30
  limits?: ContextLimits;
20
31
  addAgent(...agents: Agent[]): void;
21
32
  newContext(): ExecutionContext;
@@ -0,0 +1,17 @@
1
+ import { type ZodObject, type ZodType, z } from "zod";
2
+ import type { Message } from "../agents/agent.js";
3
+ export declare function loadAgentFromJsFile(path: string): Promise<{
4
+ name: string;
5
+ fn: (args_0: Message) => Message;
6
+ description?: string | undefined;
7
+ input_schema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
8
+ [x: string]: any;
9
+ }, {
10
+ [x: string]: any;
11
+ }> | undefined;
12
+ output_schema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
13
+ [x: string]: any;
14
+ }, {
15
+ [x: string]: any;
16
+ }> | undefined;
17
+ }>;
@@ -0,0 +1,24 @@
1
+ import { type ZodObject, type ZodType, z } from "zod";
2
+ export declare function loadAgentFromYamlFile(path: string): Promise<{
3
+ type: "ai";
4
+ name: string;
5
+ description?: string | undefined;
6
+ tools?: string[] | undefined;
7
+ instructions?: string | undefined;
8
+ input_schema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
9
+ [x: string]: any;
10
+ }, {
11
+ [x: string]: any;
12
+ }> | undefined;
13
+ output_schema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
14
+ [x: string]: any;
15
+ }, {
16
+ [x: string]: any;
17
+ }> | undefined;
18
+ output_key?: string | undefined;
19
+ } | {
20
+ type: "mcp";
21
+ url?: string | undefined;
22
+ command?: string | undefined;
23
+ args?: string[] | undefined;
24
+ }>;
@@ -0,0 +1,35 @@
1
+ import { type Agent } from "../agents/agent.js";
2
+ import type { ChatModel } from "../models/chat-model.js";
3
+ export interface LoadOptions {
4
+ path: string;
5
+ }
6
+ export declare function load(options: LoadOptions): Promise<{
7
+ model: ChatModel | undefined;
8
+ agents: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[];
9
+ tools: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[];
10
+ description?: string | null | undefined;
11
+ name?: string | null | undefined;
12
+ chat_model?: {
13
+ name?: string | null | undefined;
14
+ temperature?: number | null | undefined;
15
+ provider?: string | null | undefined;
16
+ top_p?: number | null | undefined;
17
+ frequent_penalty?: number | null | undefined;
18
+ presence_penalty?: number | null | undefined;
19
+ } | null | undefined;
20
+ }>;
21
+ export declare function loadAgent(path: string): Promise<Agent>;
22
+ export declare function loadAIGNEFile(path: string): Promise<{
23
+ description?: string | null | undefined;
24
+ tools?: string[] | null | undefined;
25
+ name?: string | null | undefined;
26
+ chat_model?: {
27
+ name?: string | null | undefined;
28
+ temperature?: number | null | undefined;
29
+ provider?: string | null | undefined;
30
+ top_p?: number | null | undefined;
31
+ frequent_penalty?: number | null | undefined;
32
+ presence_penalty?: number | null | undefined;
33
+ } | null | undefined;
34
+ agents?: string[] | null | undefined;
35
+ }>;
@@ -0,0 +1,17 @@
1
+ import { z } from "zod";
2
+ export declare const inputOutputSchema: z.ZodObject<{
3
+ type: z.ZodLiteral<"object">;
4
+ properties: z.ZodRecord<z.ZodString, z.ZodAny>;
5
+ required: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
6
+ additionalProperties: z.ZodOptional<z.ZodBoolean>;
7
+ }, "strip", z.ZodTypeAny, {
8
+ type: "object";
9
+ properties: Record<string, any>;
10
+ required?: string[] | undefined;
11
+ additionalProperties?: boolean | undefined;
12
+ }, {
13
+ type: "object";
14
+ properties: Record<string, any>;
15
+ required?: string[] | undefined;
16
+ additionalProperties?: boolean | undefined;
17
+ }>;
@@ -57,7 +57,7 @@ export declare const claudeChatModelOptionsSchema: z.ZodObject<{
57
57
  export declare class ClaudeChatModel extends ChatModel {
58
58
  options?: ClaudeChatModelOptions | undefined;
59
59
  constructor(options?: ClaudeChatModelOptions | undefined);
60
- private _client?;
60
+ protected _client?: Anthropic;
61
61
  get client(): Anthropic;
62
62
  get modelOptions(): ChatModelOptions | undefined;
63
63
  process(input: ChatModelInput): Promise<ChatModelOutput>;
@@ -61,7 +61,7 @@ export declare const openAIChatModelOptionsSchema: z.ZodObject<{
61
61
  export declare class OpenAIChatModel extends ChatModel {
62
62
  options?: OpenAIChatModelOptions | undefined;
63
63
  constructor(options?: OpenAIChatModelOptions | undefined);
64
- private _client?;
64
+ protected _client?: OpenAI;
65
65
  get client(): OpenAI;
66
66
  get modelOptions(): ChatModelOptions | undefined;
67
67
  process(input: ChatModelInput): Promise<ChatModelOutput>;
@@ -1,3 +1,4 @@
1
+ import OpenAI from "openai";
1
2
  import type { ChatModelOptions } from "./chat-model.js";
2
3
  import { OpenAIChatModel } from "./openai-chat-model.js";
3
4
  export interface XAIChatModelOptions {
@@ -8,4 +9,5 @@ export interface XAIChatModelOptions {
8
9
  }
9
10
  export declare class XAIChatModel extends OpenAIChatModel {
10
11
  constructor(options?: XAIChatModelOptions);
12
+ get client(): OpenAI;
11
13
  }
@@ -1,4 +1,5 @@
1
1
  import debug, { type Debugger } from "debug";
2
+ import type { Ora } from "ora";
2
3
  interface DebugWithSpinner extends Debugger {
3
4
  spinner<T>(promise: Promise<T>, message?: string, callback?: (result: T) => void, options?: {
4
5
  disabled?: boolean;
@@ -10,9 +11,10 @@ export declare const logger: debug.Debug & {
10
11
  debug: debug.Debug;
11
12
  default: debug.Debug;
12
13
  } & {
13
- globalSpinner: import("@aigne/ora").Ora;
14
+ globalSpinner: Ora | undefined;
14
15
  base: DebugWithSpinner;
15
16
  debug: DebugWithSpinner;
16
17
  spinner: typeof spinner;
18
+ setSpinner: (spinner: Ora) => void;
17
19
  };
18
20
  export {};
@@ -1,6 +1,5 @@
1
- import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
1
  import { type ListPromptsResult, type ListResourceTemplatesResult, type ListResourcesResult, type ListToolsResult } from "@modelcontextprotocol/sdk/types.js";
3
- import { MCPPrompt, MCPResource, MCPTool } from "../agents/mcp-agent.js";
4
- export declare function toolFromMCPTool(client: Client, tool: ListToolsResult["tools"][number]): MCPTool;
5
- export declare function promptFromMCPPrompt(client: Client, prompt: ListPromptsResult["prompts"][number]): MCPPrompt;
6
- export declare function resourceFromMCPResource(client: Client, resource: ListResourcesResult["resources"][number] | ListResourceTemplatesResult["resourceTemplates"][number]): MCPResource;
2
+ import { type MCPBaseOptions, MCPPrompt, MCPResource, MCPTool } from "../agents/mcp-agent.js";
3
+ export declare function toolFromMCPTool(tool: ListToolsResult["tools"][number], options: MCPBaseOptions): MCPTool;
4
+ export declare function promptFromMCPPrompt(prompt: ListPromptsResult["prompts"][number], options: MCPBaseOptions): MCPPrompt;
5
+ export declare function resourceFromMCPResource(resource: ListResourcesResult["resources"][number] | ListResourceTemplatesResult["resourceTemplates"][number], options: MCPBaseOptions): MCPResource;
@@ -5,8 +5,10 @@ export declare function isNil(value: unknown): value is null | undefined;
5
5
  export declare function isEmpty(obj: unknown): boolean;
6
6
  export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
7
7
  export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
8
+ export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[];
8
9
  export declare function orArrayToArray<T>(value?: T | T[]): T[];
9
10
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
10
11
  [key: string]: T;
11
12
  };
12
13
  export declare function checkArguments<T>(prefix: string, schema: ZodType<T>, args: T): void;
14
+ export declare function tryOrThrow<P extends PromiseOrValue<unknown>>(fn: () => P, error: string | Error | ((error: Error) => Error)): P;