@aigne/core 1.15.0 → 1.16.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 (141) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +9 -7
  3. package/README.zh.md +9 -7
  4. package/lib/cjs/agents/agent.d.ts +129 -6
  5. package/lib/cjs/agents/agent.js +112 -20
  6. package/lib/cjs/agents/ai-agent.d.ts +3 -2
  7. package/lib/cjs/agents/ai-agent.js +10 -6
  8. package/lib/{esm/models → cjs/agents}/chat-model.d.ts +24 -13
  9. package/lib/cjs/{models → agents}/chat-model.js +48 -7
  10. package/lib/cjs/agents/guide-rail-agent.d.ts +62 -0
  11. package/lib/cjs/agents/guide-rail-agent.js +14 -0
  12. package/lib/cjs/agents/mcp-agent.js +9 -9
  13. package/lib/cjs/aigne/aigne.d.ts +3 -2
  14. package/lib/cjs/aigne/aigne.js +2 -2
  15. package/lib/cjs/aigne/context.d.ts +2 -1
  16. package/lib/cjs/aigne/context.js +7 -0
  17. package/lib/cjs/index.d.ts +1 -1
  18. package/lib/cjs/index.js +1 -1
  19. package/lib/cjs/loader/agent-yaml.d.ts +1 -1
  20. package/lib/cjs/loader/index.d.ts +18 -11
  21. package/lib/cjs/loader/index.js +8 -27
  22. package/lib/cjs/memory/retriever.d.ts +2 -2
  23. package/lib/cjs/prompt/prompt-builder.d.ts +3 -3
  24. package/lib/cjs/prompt/template.d.ts +3 -3
  25. package/lib/cjs/utils/json-schema.js +1 -1
  26. package/lib/cjs/utils/logger.d.ts +33 -8
  27. package/lib/cjs/utils/logger.js +63 -5
  28. package/lib/cjs/utils/model-utils.d.ts +1 -1
  29. package/lib/cjs/utils/stream-utils.d.ts +3 -1
  30. package/lib/cjs/utils/stream-utils.js +31 -1
  31. package/lib/cjs/utils/type-utils.d.ts +5 -0
  32. package/lib/dts/agents/agent.d.ts +129 -6
  33. package/lib/dts/agents/ai-agent.d.ts +3 -2
  34. package/lib/{cjs/models → dts/agents}/chat-model.d.ts +24 -13
  35. package/lib/dts/agents/guide-rail-agent.d.ts +62 -0
  36. package/lib/dts/aigne/aigne.d.ts +3 -2
  37. package/lib/dts/aigne/context.d.ts +2 -1
  38. package/lib/dts/index.d.ts +1 -1
  39. package/lib/dts/loader/agent-yaml.d.ts +1 -1
  40. package/lib/dts/loader/index.d.ts +18 -11
  41. package/lib/dts/memory/retriever.d.ts +2 -2
  42. package/lib/dts/prompt/prompt-builder.d.ts +3 -3
  43. package/lib/dts/prompt/template.d.ts +3 -3
  44. package/lib/dts/utils/logger.d.ts +33 -8
  45. package/lib/dts/utils/model-utils.d.ts +1 -1
  46. package/lib/dts/utils/stream-utils.d.ts +3 -1
  47. package/lib/dts/utils/type-utils.d.ts +5 -0
  48. package/lib/esm/agents/agent.d.ts +129 -6
  49. package/lib/esm/agents/agent.js +112 -20
  50. package/lib/esm/agents/ai-agent.d.ts +3 -2
  51. package/lib/esm/agents/ai-agent.js +10 -6
  52. package/lib/{dts/models → esm/agents}/chat-model.d.ts +24 -13
  53. package/lib/esm/{models → agents}/chat-model.js +48 -7
  54. package/lib/esm/agents/guide-rail-agent.d.ts +62 -0
  55. package/lib/esm/agents/guide-rail-agent.js +11 -0
  56. package/lib/esm/agents/mcp-agent.js +9 -9
  57. package/lib/esm/aigne/aigne.d.ts +3 -2
  58. package/lib/esm/aigne/aigne.js +2 -2
  59. package/lib/esm/aigne/context.d.ts +2 -1
  60. package/lib/esm/aigne/context.js +7 -0
  61. package/lib/esm/index.d.ts +1 -1
  62. package/lib/esm/index.js +1 -1
  63. package/lib/esm/loader/agent-yaml.d.ts +1 -1
  64. package/lib/esm/loader/index.d.ts +18 -11
  65. package/lib/esm/loader/index.js +8 -27
  66. package/lib/esm/memory/retriever.d.ts +2 -2
  67. package/lib/esm/prompt/prompt-builder.d.ts +3 -3
  68. package/lib/esm/prompt/template.d.ts +3 -3
  69. package/lib/esm/utils/json-schema.js +1 -1
  70. package/lib/esm/utils/logger.d.ts +33 -8
  71. package/lib/esm/utils/logger.js +61 -4
  72. package/lib/esm/utils/model-utils.d.ts +1 -1
  73. package/lib/esm/utils/stream-utils.d.ts +3 -1
  74. package/lib/esm/utils/stream-utils.js +29 -1
  75. package/lib/esm/utils/type-utils.d.ts +5 -0
  76. package/package.json +1 -20
  77. package/lib/cjs/client/client.d.ts +0 -97
  78. package/lib/cjs/client/client.js +0 -87
  79. package/lib/cjs/client/index.d.ts +0 -1
  80. package/lib/cjs/client/index.js +0 -17
  81. package/lib/cjs/models/bedrock-chat-model.d.ts +0 -79
  82. package/lib/cjs/models/bedrock-chat-model.js +0 -303
  83. package/lib/cjs/models/claude-chat-model.d.ts +0 -114
  84. package/lib/cjs/models/claude-chat-model.js +0 -317
  85. package/lib/cjs/models/deepseek-chat-model.d.ts +0 -23
  86. package/lib/cjs/models/deepseek-chat-model.js +0 -35
  87. package/lib/cjs/models/gemini-chat-model.d.ts +0 -23
  88. package/lib/cjs/models/gemini-chat-model.js +0 -35
  89. package/lib/cjs/models/ollama-chat-model.d.ts +0 -22
  90. package/lib/cjs/models/ollama-chat-model.js +0 -34
  91. package/lib/cjs/models/open-router-chat-model.d.ts +0 -22
  92. package/lib/cjs/models/open-router-chat-model.js +0 -34
  93. package/lib/cjs/models/openai-chat-model.d.ts +0 -166
  94. package/lib/cjs/models/openai-chat-model.js +0 -415
  95. package/lib/cjs/models/xai-chat-model.d.ts +0 -21
  96. package/lib/cjs/models/xai-chat-model.js +0 -33
  97. package/lib/cjs/server/error.d.ts +0 -15
  98. package/lib/cjs/server/error.js +0 -22
  99. package/lib/cjs/server/index.d.ts +0 -2
  100. package/lib/cjs/server/index.js +0 -18
  101. package/lib/cjs/server/server.d.ts +0 -135
  102. package/lib/cjs/server/server.js +0 -187
  103. package/lib/dts/client/client.d.ts +0 -97
  104. package/lib/dts/client/index.d.ts +0 -1
  105. package/lib/dts/models/bedrock-chat-model.d.ts +0 -79
  106. package/lib/dts/models/claude-chat-model.d.ts +0 -114
  107. package/lib/dts/models/deepseek-chat-model.d.ts +0 -23
  108. package/lib/dts/models/gemini-chat-model.d.ts +0 -23
  109. package/lib/dts/models/ollama-chat-model.d.ts +0 -22
  110. package/lib/dts/models/open-router-chat-model.d.ts +0 -22
  111. package/lib/dts/models/openai-chat-model.d.ts +0 -166
  112. package/lib/dts/models/xai-chat-model.d.ts +0 -21
  113. package/lib/dts/server/error.d.ts +0 -15
  114. package/lib/dts/server/index.d.ts +0 -2
  115. package/lib/dts/server/server.d.ts +0 -135
  116. package/lib/esm/client/client.d.ts +0 -97
  117. package/lib/esm/client/client.js +0 -83
  118. package/lib/esm/client/index.d.ts +0 -1
  119. package/lib/esm/client/index.js +0 -1
  120. package/lib/esm/models/bedrock-chat-model.d.ts +0 -79
  121. package/lib/esm/models/bedrock-chat-model.js +0 -298
  122. package/lib/esm/models/claude-chat-model.d.ts +0 -114
  123. package/lib/esm/models/claude-chat-model.js +0 -310
  124. package/lib/esm/models/deepseek-chat-model.d.ts +0 -23
  125. package/lib/esm/models/deepseek-chat-model.js +0 -31
  126. package/lib/esm/models/gemini-chat-model.d.ts +0 -23
  127. package/lib/esm/models/gemini-chat-model.js +0 -31
  128. package/lib/esm/models/ollama-chat-model.d.ts +0 -22
  129. package/lib/esm/models/ollama-chat-model.js +0 -30
  130. package/lib/esm/models/open-router-chat-model.d.ts +0 -22
  131. package/lib/esm/models/open-router-chat-model.js +0 -30
  132. package/lib/esm/models/openai-chat-model.d.ts +0 -166
  133. package/lib/esm/models/openai-chat-model.js +0 -405
  134. package/lib/esm/models/xai-chat-model.d.ts +0 -21
  135. package/lib/esm/models/xai-chat-model.js +0 -29
  136. package/lib/esm/server/error.d.ts +0 -15
  137. package/lib/esm/server/error.js +0 -18
  138. package/lib/esm/server/index.d.ts +0 -2
  139. package/lib/esm/server/index.js +0 -2
  140. package/lib/esm/server/server.d.ts +0 -135
  141. package/lib/esm/server/server.js +0 -180
@@ -1,135 +0,0 @@
1
- import { IncomingMessage, ServerResponse } from "node:http";
2
- import { z } from "zod";
3
- import type { AIGNE } from "../aigne/aigne.js";
4
- /**
5
- * Schema for validating agent invocation payloads.
6
- * Defines the expected structure for requests to invoke an agent.
7
- *
8
- * @hidden
9
- */
10
- export declare const invokePayloadSchema: z.ZodObject<{
11
- agent: z.ZodString;
12
- input: z.ZodRecord<z.ZodString, z.ZodUnknown>;
13
- options: z.ZodOptional<z.ZodNullable<z.ZodObject<{
14
- streaming: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
15
- }, "strip", z.ZodTypeAny, {
16
- streaming?: boolean | null | undefined;
17
- }, {
18
- streaming?: boolean | null | undefined;
19
- }>>>;
20
- }, "strip", z.ZodTypeAny, {
21
- agent: string;
22
- input: Record<string, unknown>;
23
- options?: {
24
- streaming?: boolean | null | undefined;
25
- } | null | undefined;
26
- }, {
27
- agent: string;
28
- input: Record<string, unknown>;
29
- options?: {
30
- streaming?: boolean | null | undefined;
31
- } | null | undefined;
32
- }>;
33
- /**
34
- * Configuration options for the AIGNEServer.
35
- * These options control various aspects of server behavior including
36
- * request parsing, payload limits, and response handling.
37
- */
38
- export interface AIGNEServerOptions {
39
- /**
40
- * Maximum body size for incoming HTTP requests.
41
- * This controls the upper limit of request payload size when parsing raw HTTP requests.
42
- * Only used when working with Node.js IncomingMessage objects that don't already have
43
- * a pre-parsed body property (e.g., when not using Express middleware).
44
- *
45
- * @default "4mb"
46
- */
47
- maximumBodySize?: string;
48
- }
49
- /**
50
- * AIGNEServer provides HTTP API access to AIGNE capabilities.
51
- * It handles requests to invoke agents, manages response streaming,
52
- * and supports multiple HTTP server frameworks including Node.js http,
53
- * Express, and Fetch API compatible environments.
54
- *
55
- * @example
56
- * Here's a simple example of how to use AIGNEServer with express:
57
- * {@includeCode ../../test/server/server.test.ts#example-aigne-server-express}
58
- *
59
- * @example
60
- * Here's an example of how to use AIGNEServer with Hono:
61
- * {@includeCode ../../test/server/server.test.ts#example-aigne-server-hono}
62
- */
63
- export declare class AIGNEServer {
64
- engine: AIGNE;
65
- options?: AIGNEServerOptions | undefined;
66
- /**
67
- * Creates a new AIGNEServer instance.
68
- *
69
- * @param engine - The AIGNE engine instance that will process agent invocations
70
- * @param options - Configuration options for the server
71
- */
72
- constructor(engine: AIGNE, options?: AIGNEServerOptions | undefined);
73
- /**
74
- * Invokes an agent with the provided input and returns a standard web Response.
75
- * This method serves as the primary API endpoint for agent invocation.
76
- *
77
- * The request can be provided in various formats to support different integration scenarios:
78
- * - As a pre-parsed JavaScript object
79
- * - As a Fetch API Request instance (for modern web frameworks)
80
- * - As a Node.js IncomingMessage (for Express, Fastify, etc.)
81
- *
82
- * @param request - The agent invocation request in any supported format
83
- * @returns A web standard Response object that can be returned directly in frameworks
84
- * like Hono, Next.js, or any Fetch API compatible environment
85
- *
86
- * @example
87
- * Here's a simple example of how to use AIGNEServer with Hono:
88
- * {@includeCode ../../test/server/server.test.ts#example-aigne-server-hono}
89
- */
90
- invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
91
- /**
92
- * Invokes an agent with the provided input and streams the response to a Node.js ServerResponse.
93
- * This overload is specifically designed for Node.js HTTP server environments.
94
- *
95
- * The method handles both regular JSON responses and streaming Server-Sent Events (SSE)
96
- * responses based on the options specified in the request.
97
- *
98
- * @param request - The agent invocation request in any supported format
99
- * @param response - The Node.js ServerResponse object to write the response to
100
- *
101
- * @example
102
- * Here's a simple example of how to use AIGNEServer with express:
103
- * {@includeCode ../../test/server/server.test.ts#example-aigne-server-express}
104
- */
105
- invoke(request: Record<string, unknown> | Request | IncomingMessage, response: ServerResponse): Promise<void>;
106
- /**
107
- * Internal method that handles the core logic of processing an agent invocation request.
108
- * Validates the request payload, finds the requested agent, and processes the invocation
109
- * with either streaming or non-streaming response handling.
110
- *
111
- * @param request - The parsed or raw request to process
112
- * @returns A standard Response object with the invocation result
113
- * @private
114
- */
115
- _invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
116
- /**
117
- * Prepares and normalizes the input from various request types.
118
- * Handles different request formats (Node.js IncomingMessage, Fetch API Request,
119
- * or already parsed object) and extracts the JSON payload.
120
- *
121
- * @param request - The request object in any supported format
122
- * @returns The normalized payload as a JavaScript object
123
- * @private
124
- */
125
- _prepareInput(request: Record<string, unknown> | Request | IncomingMessage): Promise<Record<string, unknown>>;
126
- /**
127
- * Writes a web standard Response object to a Node.js ServerResponse.
128
- * Handles streaming responses and error conditions appropriately.
129
- *
130
- * @param response - The web standard Response object to write
131
- * @param res - The Node.js ServerResponse to write to
132
- * @private
133
- */
134
- _writeResponse(response: Response, res: ServerResponse): Promise<void>;
135
- }
@@ -1,180 +0,0 @@
1
- import { IncomingMessage, ServerResponse } from "node:http";
2
- import contentType from "content-type";
3
- import getRawBody from "raw-body";
4
- import { z } from "zod";
5
- import { AgentResponseStreamSSE } from "../utils/event-stream.js";
6
- import { checkArguments, isRecord, tryOrThrow } from "../utils/type-utils.js";
7
- import { ServerError } from "./error.js";
8
- /**
9
- * Default maximum allowed size for request bodies when parsing raw HTTP requests.
10
- * This limits the amount of data that can be uploaded to protect against denial of service attacks.
11
- * Can be overridden via AIGNEServerOptions.
12
- * @internal
13
- */
14
- const DEFAULT_MAXIMUM_BODY_SIZE = "4mb";
15
- /**
16
- * Schema for validating agent invocation payloads.
17
- * Defines the expected structure for requests to invoke an agent.
18
- *
19
- * @hidden
20
- */
21
- export const invokePayloadSchema = z.object({
22
- agent: z.string(),
23
- input: z.record(z.string(), z.unknown()),
24
- options: z
25
- .object({
26
- streaming: z.boolean().nullish(),
27
- })
28
- .nullish(),
29
- });
30
- /**
31
- * AIGNEServer provides HTTP API access to AIGNE capabilities.
32
- * It handles requests to invoke agents, manages response streaming,
33
- * and supports multiple HTTP server frameworks including Node.js http,
34
- * Express, and Fetch API compatible environments.
35
- *
36
- * @example
37
- * Here's a simple example of how to use AIGNEServer with express:
38
- * {@includeCode ../../test/server/server.test.ts#example-aigne-server-express}
39
- *
40
- * @example
41
- * Here's an example of how to use AIGNEServer with Hono:
42
- * {@includeCode ../../test/server/server.test.ts#example-aigne-server-hono}
43
- */
44
- export class AIGNEServer {
45
- engine;
46
- options;
47
- /**
48
- * Creates a new AIGNEServer instance.
49
- *
50
- * @param engine - The AIGNE engine instance that will process agent invocations
51
- * @param options - Configuration options for the server
52
- */
53
- constructor(engine, options) {
54
- this.engine = engine;
55
- this.options = options;
56
- }
57
- async invoke(request, response) {
58
- const result = await this._invoke(request);
59
- if (response instanceof ServerResponse) {
60
- await this._writeResponse(result, response);
61
- return;
62
- }
63
- return result;
64
- }
65
- /**
66
- * Internal method that handles the core logic of processing an agent invocation request.
67
- * Validates the request payload, finds the requested agent, and processes the invocation
68
- * with either streaming or non-streaming response handling.
69
- *
70
- * @param request - The parsed or raw request to process
71
- * @returns A standard Response object with the invocation result
72
- * @private
73
- */
74
- async _invoke(request) {
75
- const { engine } = this;
76
- try {
77
- const payload = await this._prepareInput(request);
78
- const { agent: agentName, input, options, } = tryOrThrow(() => checkArguments(`Invoke agent ${payload.agent}`, invokePayloadSchema, payload), (error) => new ServerError(400, error.message));
79
- const agent = engine.agents[agentName];
80
- if (!agent)
81
- throw new ServerError(404, `Agent ${agentName} not found`);
82
- if (!options?.streaming) {
83
- const result = await engine.invoke(agent, input);
84
- return new Response(JSON.stringify(result), {
85
- headers: { "Content-Type": "application/json" },
86
- });
87
- }
88
- const stream = await engine.invoke(agent, input, { streaming: true });
89
- return new Response(new AgentResponseStreamSSE(stream), {
90
- headers: {
91
- "Content-Type": "text/event-stream",
92
- "Cache-Control": "no-cache",
93
- "X-Accel-Buffering": "no",
94
- },
95
- });
96
- }
97
- catch (error) {
98
- return new Response(JSON.stringify({ error: { message: error.message } }), {
99
- status: error instanceof ServerError ? error.status : 500,
100
- headers: { "Content-Type": "application/json" },
101
- });
102
- }
103
- }
104
- /**
105
- * Prepares and normalizes the input from various request types.
106
- * Handles different request formats (Node.js IncomingMessage, Fetch API Request,
107
- * or already parsed object) and extracts the JSON payload.
108
- *
109
- * @param request - The request object in any supported format
110
- * @returns The normalized payload as a JavaScript object
111
- * @private
112
- */
113
- async _prepareInput(request) {
114
- const contentTypeError = new ServerError(415, "Unsupported Media Type: Content-Type must be application/json");
115
- if (request instanceof IncomingMessage) {
116
- // Support for express with json() middleware
117
- if ("body" in request && typeof request.body === "object") {
118
- if (!isRecord(request.body))
119
- throw contentTypeError;
120
- return request.body;
121
- }
122
- // Support vanilla nodejs http server
123
- const maximumBodySize = this.options?.maximumBodySize || DEFAULT_MAXIMUM_BODY_SIZE;
124
- const ct = request.headers["content-type"];
125
- if (!ct || !ct.includes("application/json"))
126
- throw contentTypeError;
127
- const parsedCt = contentType.parse(ct);
128
- const raw = await getRawBody(request, {
129
- limit: maximumBodySize,
130
- encoding: parsedCt.parameters.charset ?? "utf-8",
131
- });
132
- return tryOrThrow(() => JSON.parse(raw.toString()), (error) => new ServerError(400, `Parse request body to json error: ${error.message}`));
133
- }
134
- if (request instanceof Request) {
135
- if (!request.headers.get("content-type")?.includes("application/json")) {
136
- throw contentTypeError;
137
- }
138
- return await request.json();
139
- }
140
- if (!isRecord(request))
141
- throw contentTypeError;
142
- return request;
143
- }
144
- /**
145
- * Writes a web standard Response object to a Node.js ServerResponse.
146
- * Handles streaming responses and error conditions appropriately.
147
- *
148
- * @param response - The web standard Response object to write
149
- * @param res - The Node.js ServerResponse to write to
150
- * @private
151
- */
152
- async _writeResponse(response, res) {
153
- try {
154
- res.writeHead(response.status, Object.fromEntries(response.headers.entries()));
155
- res.flushHeaders();
156
- if (!response.body)
157
- throw new Error("Response body is empty");
158
- for await (const chunk of response.body) {
159
- res.write(chunk);
160
- // Support for express with compression middleware
161
- if ("flush" in res && typeof res.flush === "function") {
162
- res.flush();
163
- }
164
- }
165
- }
166
- catch (error) {
167
- if (!res.headersSent) {
168
- res.writeHead(error instanceof ServerError ? error.status : 500, {
169
- "Content-Type": "application/json",
170
- });
171
- }
172
- if (res.writable) {
173
- res.write(JSON.stringify({ error: { message: error.message } }));
174
- }
175
- }
176
- finally {
177
- res.end();
178
- }
179
- }
180
- }