@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,22 +0,0 @@
1
- import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
2
- /**
3
- * Implementation of the ChatModel interface for Ollama
4
- *
5
- * This model allows you to run open-source LLMs locally using Ollama,
6
- * with an OpenAI-compatible API interface.
7
- *
8
- * Default model: 'llama3.2'
9
- *
10
- * @example
11
- * Here's how to create and use an Ollama chat model:
12
- * {@includeCode ../../test/models/ollama-chat-model.test.ts#example-ollama-chat-model}
13
- *
14
- * @example
15
- * Here's an example with streaming response:
16
- * {@includeCode ../../test/models/ollama-chat-model.test.ts#example-ollama-chat-model-streaming}
17
- */
18
- export declare class OllamaChatModel extends OpenAIChatModel {
19
- constructor(options?: OpenAIChatModelOptions);
20
- protected apiKeyEnvName: string;
21
- protected apiKeyDefault: string;
22
- }
@@ -1,22 +0,0 @@
1
- import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
2
- /**
3
- * Implementation of the ChatModel interface for OpenRouter service
4
- *
5
- * OpenRouter provides access to a variety of large language models through a unified API.
6
- * This implementation uses the OpenAI-compatible interface to connect to OpenRouter's service.
7
- *
8
- * Default model: 'openai/gpt-4o'
9
- *
10
- * @example
11
- * Here's how to create and use an OpenRouter chat model:
12
- * {@includeCode ../../test/models/open-router-chat-model.test.ts#example-openrouter-chat-model}
13
- *
14
- * @example
15
- * Here's an example with streaming response:
16
- * {@includeCode ../../test/models/open-router-chat-model.test.ts#example-openrouter-chat-model-streaming}
17
- */
18
- export declare class OpenRouterChatModel extends OpenAIChatModel {
19
- constructor(options?: OpenAIChatModelOptions);
20
- protected apiKeyEnvName: string;
21
- protected supportsParallelToolCalls: boolean;
22
- }
@@ -1,166 +0,0 @@
1
- import OpenAI from "openai";
2
- import type { ChatCompletionMessageParam, ChatCompletionTool } from "openai/resources";
3
- import { z } from "zod";
4
- import type { AgentProcessResult } from "../agents/agent.js";
5
- import { type PromiseOrValue } from "../utils/type-utils.js";
6
- import { ChatModel, type ChatModelInput, type ChatModelInputMessage, type ChatModelInputTool, type ChatModelOptions, type ChatModelOutput, type Role } from "./chat-model.js";
7
- export interface OpenAIChatModelCapabilities {
8
- supportsNativeStructuredOutputs: boolean;
9
- supportsEndWithSystemMessage: boolean;
10
- supportsToolsUseWithJsonSchema: boolean;
11
- supportsParallelToolCalls: boolean;
12
- supportsToolsEmptyParameters: boolean;
13
- supportsToolStreaming: boolean;
14
- supportsTemperature: boolean;
15
- }
16
- /**
17
- * Configuration options for OpenAI Chat Model
18
- */
19
- export interface OpenAIChatModelOptions {
20
- /**
21
- * API key for OpenAI API
22
- *
23
- * If not provided, will look for OPENAI_API_KEY in environment variables
24
- */
25
- apiKey?: string;
26
- /**
27
- * Base URL for OpenAI API
28
- *
29
- * Useful for proxies or alternate endpoints
30
- */
31
- baseURL?: string;
32
- /**
33
- * OpenAI model to use
34
- *
35
- * Defaults to 'gpt-4o-mini'
36
- */
37
- model?: string;
38
- /**
39
- * Additional model options to control behavior
40
- */
41
- modelOptions?: ChatModelOptions;
42
- }
43
- /**
44
- * @hidden
45
- */
46
- export declare const openAIChatModelOptionsSchema: z.ZodObject<{
47
- apiKey: z.ZodOptional<z.ZodString>;
48
- baseURL: z.ZodOptional<z.ZodString>;
49
- model: z.ZodOptional<z.ZodString>;
50
- modelOptions: z.ZodOptional<z.ZodObject<{
51
- model: z.ZodOptional<z.ZodString>;
52
- temperature: z.ZodOptional<z.ZodNumber>;
53
- topP: z.ZodOptional<z.ZodNumber>;
54
- frequencyPenalty: z.ZodOptional<z.ZodNumber>;
55
- presencePenalty: z.ZodOptional<z.ZodNumber>;
56
- parallelToolCalls: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
57
- }, "strip", z.ZodTypeAny, {
58
- parallelToolCalls: boolean;
59
- model?: string | undefined;
60
- temperature?: number | undefined;
61
- topP?: number | undefined;
62
- frequencyPenalty?: number | undefined;
63
- presencePenalty?: number | undefined;
64
- }, {
65
- model?: string | undefined;
66
- temperature?: number | undefined;
67
- topP?: number | undefined;
68
- frequencyPenalty?: number | undefined;
69
- presencePenalty?: number | undefined;
70
- parallelToolCalls?: boolean | undefined;
71
- }>>;
72
- }, "strip", z.ZodTypeAny, {
73
- modelOptions?: {
74
- parallelToolCalls: boolean;
75
- model?: string | undefined;
76
- temperature?: number | undefined;
77
- topP?: number | undefined;
78
- frequencyPenalty?: number | undefined;
79
- presencePenalty?: number | undefined;
80
- } | undefined;
81
- model?: string | undefined;
82
- apiKey?: string | undefined;
83
- baseURL?: string | undefined;
84
- }, {
85
- modelOptions?: {
86
- model?: string | undefined;
87
- temperature?: number | undefined;
88
- topP?: number | undefined;
89
- frequencyPenalty?: number | undefined;
90
- presencePenalty?: number | undefined;
91
- parallelToolCalls?: boolean | undefined;
92
- } | undefined;
93
- model?: string | undefined;
94
- apiKey?: string | undefined;
95
- baseURL?: string | undefined;
96
- }>;
97
- /**
98
- * Implementation of the ChatModel interface for OpenAI's API
99
- *
100
- * This model provides access to OpenAI's capabilities including:
101
- * - Text generation
102
- * - Tool use with parallel tool calls
103
- * - JSON structured output
104
- * - Image understanding
105
- *
106
- * Default model: 'gpt-4o-mini'
107
- *
108
- * @example
109
- * Here's how to create and use an OpenAI chat model:
110
- * {@includeCode ../../test/models/openai-chat-model.test.ts#example-openai-chat-model}
111
- *
112
- * @example
113
- * Here's an example with streaming response:
114
- * {@includeCode ../../test/models/openai-chat-model.test.ts#example-openai-chat-model-streaming}
115
- */
116
- export declare class OpenAIChatModel extends ChatModel {
117
- options?: OpenAIChatModelOptions | undefined;
118
- constructor(options?: OpenAIChatModelOptions | undefined);
119
- /**
120
- * @hidden
121
- */
122
- protected _client?: OpenAI;
123
- protected apiKeyEnvName: string;
124
- protected apiKeyDefault: string | undefined;
125
- protected supportsNativeStructuredOutputs: boolean;
126
- protected supportsEndWithSystemMessage: boolean;
127
- protected supportsToolsUseWithJsonSchema: boolean;
128
- protected supportsParallelToolCalls: boolean;
129
- protected supportsToolsEmptyParameters: boolean;
130
- protected supportsToolStreaming: boolean;
131
- protected supportsTemperature: boolean;
132
- get client(): OpenAI;
133
- get modelOptions(): ChatModelOptions | undefined;
134
- /**
135
- * Process the input and generate a response
136
- * @param input The input to process
137
- * @returns The generated response
138
- */
139
- process(input: ChatModelInput): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
140
- private _process;
141
- private getParallelToolCalls;
142
- private getRunMessages;
143
- private getRunResponseFormat;
144
- private requestStructuredOutput;
145
- private extractResultFromStream;
146
- }
147
- /**
148
- * @hidden
149
- */
150
- export declare const ROLE_MAP: {
151
- [key in Role]: ChatCompletionMessageParam["role"];
152
- };
153
- /**
154
- * @hidden
155
- */
156
- export declare function contentsFromInputMessages(messages: ChatModelInputMessage[]): Promise<ChatCompletionMessageParam[]>;
157
- /**
158
- * @hidden
159
- */
160
- export declare function toolsFromInputTools(tools?: ChatModelInputTool[], options?: {
161
- addTypeToEmptyParameters?: boolean;
162
- }): ChatCompletionTool[] | undefined;
163
- /**
164
- * @hidden
165
- */
166
- export declare function jsonSchemaToOpenAIJsonSchema(schema: Record<string, unknown>): Record<string, unknown>;
@@ -1,21 +0,0 @@
1
- import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
2
- /**
3
- * Implementation of the ChatModel interface for X.AI's API (Grok)
4
- *
5
- * This model uses OpenAI-compatible API format to interact with X.AI models,
6
- * providing access to models like Grok.
7
- *
8
- * Default model: 'grok-2-latest'
9
- *
10
- * @example
11
- * Here's how to create and use an X.AI chat model:
12
- * {@includeCode ../../test/models/xai-chat-model.test.ts#example-xai-chat-model}
13
- *
14
- * @example
15
- * Here's an example with streaming response:
16
- * {@includeCode ../../test/models/xai-chat-model.test.ts#example-xai-chat-model-streaming}
17
- */
18
- export declare class XAIChatModel extends OpenAIChatModel {
19
- constructor(options?: OpenAIChatModelOptions);
20
- protected apiKeyEnvName: string;
21
- }
@@ -1,15 +0,0 @@
1
- /**
2
- * Custom error class for AIGNEServer HTTP-related errors.
3
- * Extends the standard Error class with an HTTP status code property.
4
- * This allows error responses to include appropriate HTTP status codes.
5
- */
6
- export declare class ServerError extends Error {
7
- status: number;
8
- /**
9
- * Creates a new ServerError instance.
10
- *
11
- * @param status - The HTTP status code for this error (e.g., 400, 404, 500)
12
- * @param message - The error message describing what went wrong
13
- */
14
- constructor(status: number, message: string);
15
- }
@@ -1,2 +0,0 @@
1
- export * from "./error.js";
2
- export * from "./server.js";
@@ -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,97 +0,0 @@
1
- /**
2
- * Client module used to interact with the AIGNE framework.
3
- */
4
- import type { AgentInvokeOptions, AgentResponse, AgentResponseStream, Message } from "../agents/agent.js";
5
- /**
6
- * Configuration options for the AIGNEClient.
7
- */
8
- export interface AIGNEClientOptions {
9
- /**
10
- * The URL of the AIGNE server to connect to.
11
- * This should point to the base endpoint where the AIGNEServer is hosted.
12
- */
13
- url: string;
14
- }
15
- /**
16
- * Options for invoking an agent through the AIGNEClient.
17
- * Extends the standard AgentInvokeOptions with client-specific options.
18
- */
19
- export interface AIGNEClientInvokeOptions extends AgentInvokeOptions {
20
- /**
21
- * Additional fetch API options to customize the HTTP request.
22
- * These options will be merged with the default options used by the client.
23
- */
24
- fetchOptions?: Partial<RequestInit>;
25
- }
26
- /**
27
- * Client for interacting with a remote AIGNE server.
28
- * AIGNEClient provides a client-side interface that matches the AIGNE API,
29
- * allowing applications to invoke agents and receive responses from a remote AIGNE instance.
30
- *
31
- * @example
32
- * Here's a simple example of how to use AIGNEClient:
33
- * {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
34
- *
35
- * @example
36
- * Here's an example of how to use AIGNEClient with streaming response:
37
- * {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
38
- */
39
- export declare class AIGNEClient {
40
- options: AIGNEClientOptions;
41
- /**
42
- * Creates a new AIGNEClient instance.
43
- *
44
- * @param options - Configuration options for connecting to the AIGNE server
45
- */
46
- constructor(options: AIGNEClientOptions);
47
- /**
48
- * Invokes an agent in non-streaming mode and returns the complete response.
49
- *
50
- * @param agent - Name of the agent to invoke
51
- * @param input - Input message for the agent
52
- * @param options - Options with streaming mode explicitly set to false or omitted
53
- * @returns The complete agent response
54
- *
55
- * @example
56
- * Here's a simple example of how to use AIGNEClient:
57
- * {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
58
- */
59
- invoke<I extends Message, O extends Message>(agent: string, input: I, options?: AIGNEClientInvokeOptions & {
60
- streaming?: false;
61
- }): Promise<O>;
62
- /**
63
- * Invokes an agent with streaming mode enabled and returns a stream of response chunks.
64
- *
65
- * @param agent - Name of the agent to invoke
66
- * @param input - Input message for the agent
67
- * @param options - Options with streaming mode explicitly set to true
68
- * @returns A stream of agent response chunks
69
- *
70
- * @example
71
- * Here's an example of how to use AIGNEClient with streaming response:
72
- * {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
73
- */
74
- invoke<I extends Message, O extends Message>(agent: string, input: I, options: AIGNEClientInvokeOptions & {
75
- streaming: true;
76
- }): Promise<AgentResponseStream<O>>;
77
- /**
78
- * Invokes an agent with the given input and options.
79
- *
80
- * @param agent - Name of the agent to invoke
81
- * @param input - Input message for the agent
82
- * @param options - Options for the invocation
83
- * @returns Either a complete response or a response stream depending on the streaming option
84
- */
85
- invoke<I extends Message, O extends Message>(agent: string, input: I, options?: AIGNEClientInvokeOptions): Promise<AgentResponse<O>>;
86
- /**
87
- * Enhanced fetch method that handles error responses from the AIGNE server.
88
- * This method wraps the standard fetch API to provide better error handling and reporting.
89
- *
90
- * @param args - Standard fetch API arguments (url and options)
91
- * @returns A Response object if the request was successful
92
- * @throws Error with detailed information if the request failed
93
- *
94
- * @private
95
- */
96
- fetch(...args: Parameters<typeof globalThis.fetch>): Promise<Response>;
97
- }
@@ -1,83 +0,0 @@
1
- /**
2
- * Client module used to interact with the AIGNE framework.
3
- */
4
- import { AgentResponseStreamParser, EventStreamParser } from "../utils/event-stream.js";
5
- import { tryOrThrow } from "../utils/type-utils.js";
6
- /**
7
- * Client for interacting with a remote AIGNE server.
8
- * AIGNEClient provides a client-side interface that matches the AIGNE API,
9
- * allowing applications to invoke agents and receive responses from a remote AIGNE instance.
10
- *
11
- * @example
12
- * Here's a simple example of how to use AIGNEClient:
13
- * {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
14
- *
15
- * @example
16
- * Here's an example of how to use AIGNEClient with streaming response:
17
- * {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
18
- */
19
- export class AIGNEClient {
20
- options;
21
- /**
22
- * Creates a new AIGNEClient instance.
23
- *
24
- * @param options - Configuration options for connecting to the AIGNE server
25
- */
26
- constructor(options) {
27
- this.options = options;
28
- }
29
- async invoke(agent, input, options) {
30
- // Send the agent invocation request to the AIGNE server
31
- const response = await this.fetch(this.options.url, {
32
- ...options?.fetchOptions,
33
- method: "POST",
34
- headers: {
35
- "Content-Type": "application/json",
36
- ...options?.fetchOptions?.headers,
37
- },
38
- body: JSON.stringify({ agent, input, options }),
39
- });
40
- // For non-streaming responses, simply parse the JSON response and return it
41
- if (!options?.streaming) {
42
- return await response.json();
43
- }
44
- // For streaming responses, set up the streaming pipeline
45
- const stream = response.body;
46
- if (!stream)
47
- throw new Error("Response body is not a stream");
48
- // Process the stream through a series of transforms:
49
- // 1. Convert bytes to text
50
- // 2. Parse SSE format into structured events
51
- // 3. Convert events into a standardized agent response stream
52
- return stream
53
- .pipeThrough(new TextDecoderStream())
54
- .pipeThrough(new EventStreamParser())
55
- .pipeThrough(new AgentResponseStreamParser());
56
- }
57
- /**
58
- * Enhanced fetch method that handles error responses from the AIGNE server.
59
- * This method wraps the standard fetch API to provide better error handling and reporting.
60
- *
61
- * @param args - Standard fetch API arguments (url and options)
62
- * @returns A Response object if the request was successful
63
- * @throws Error with detailed information if the request failed
64
- *
65
- * @private
66
- */
67
- async fetch(...args) {
68
- const result = await globalThis.fetch(...args);
69
- if (!result.ok) {
70
- let message;
71
- try {
72
- const text = await result.text();
73
- const json = tryOrThrow(() => JSON.parse(text));
74
- message = json?.error?.message || text;
75
- }
76
- catch {
77
- // ignore
78
- }
79
- throw new Error(`Failed to fetch url ${args[0]} with status ${result.status}: ${message}`);
80
- }
81
- return result;
82
- }
83
- }
@@ -1 +0,0 @@
1
- export * from "./client.js";
@@ -1 +0,0 @@
1
- export * from "./client.js";
@@ -1,79 +0,0 @@
1
- import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime";
2
- import { z } from "zod";
3
- import type { AgentResponse } from "../agents/agent.js";
4
- import { ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "./chat-model.js";
5
- /**
6
- * @hidden
7
- */
8
- export declare function extractLastJsonObject(text: string): string | null;
9
- export interface BedrockChatModelOptions {
10
- accessKeyId?: string;
11
- secretAccessKey?: string;
12
- region?: string;
13
- model?: string;
14
- modelOptions?: ChatModelOptions;
15
- }
16
- /**
17
- * @hidden
18
- */
19
- export declare const bedrockChatModelOptionsSchema: z.ZodObject<{
20
- region: z.ZodOptional<z.ZodString>;
21
- model: z.ZodOptional<z.ZodString>;
22
- modelOptions: z.ZodOptional<z.ZodObject<{
23
- model: z.ZodOptional<z.ZodString>;
24
- temperature: z.ZodOptional<z.ZodNumber>;
25
- topP: z.ZodOptional<z.ZodNumber>;
26
- frequencyPenalty: z.ZodOptional<z.ZodNumber>;
27
- presencePenalty: z.ZodOptional<z.ZodNumber>;
28
- parallelToolCalls: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
29
- }, "strip", z.ZodTypeAny, {
30
- parallelToolCalls: boolean;
31
- model?: string | undefined;
32
- temperature?: number | undefined;
33
- topP?: number | undefined;
34
- frequencyPenalty?: number | undefined;
35
- presencePenalty?: number | undefined;
36
- }, {
37
- model?: string | undefined;
38
- temperature?: number | undefined;
39
- topP?: number | undefined;
40
- frequencyPenalty?: number | undefined;
41
- presencePenalty?: number | undefined;
42
- parallelToolCalls?: boolean | undefined;
43
- }>>;
44
- }, "strip", z.ZodTypeAny, {
45
- modelOptions?: {
46
- parallelToolCalls: boolean;
47
- model?: string | undefined;
48
- temperature?: number | undefined;
49
- topP?: number | undefined;
50
- frequencyPenalty?: number | undefined;
51
- presencePenalty?: number | undefined;
52
- } | undefined;
53
- model?: string | undefined;
54
- region?: string | undefined;
55
- }, {
56
- modelOptions?: {
57
- model?: string | undefined;
58
- temperature?: number | undefined;
59
- topP?: number | undefined;
60
- frequencyPenalty?: number | undefined;
61
- presencePenalty?: number | undefined;
62
- parallelToolCalls?: boolean | undefined;
63
- } | undefined;
64
- model?: string | undefined;
65
- region?: string | undefined;
66
- }>;
67
- export declare class BedrockChatModel extends ChatModel {
68
- options?: BedrockChatModelOptions | undefined;
69
- constructor(options?: BedrockChatModelOptions | undefined);
70
- /**
71
- * @hidden
72
- */
73
- protected _client?: BedrockRuntimeClient;
74
- get client(): BedrockRuntimeClient;
75
- get modelOptions(): ChatModelOptions | undefined;
76
- process(input: ChatModelInput): Promise<AgentResponse<ChatModelOutput>>;
77
- private extractResultFromStream;
78
- private requestStructuredOutput;
79
- }