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