@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,8 +1,33 @@
1
- import debug from "debug";
2
- export declare const logger: debug.Debug & {
3
- debug: debug.Debug;
4
- default: debug.Debug;
5
- } & {
6
- core: debug.Debugger;
7
- mcp: debug.Debugger;
8
- };
1
+ export declare enum LogLevel {
2
+ ERROR = "error",
3
+ WARN = "warn",
4
+ INFO = "info",
5
+ DEBUG = "debug"
6
+ }
7
+ export declare class Logger {
8
+ constructor(options: {
9
+ level: LogLevel;
10
+ ns: string;
11
+ });
12
+ level: LogLevel;
13
+ private debugLogger;
14
+ private infoLogger;
15
+ private warnLogger;
16
+ private errorLogger;
17
+ logMessage: {
18
+ (...data: any[]): void;
19
+ (...data: any[]): void;
20
+ (message?: any, ...optionalParams: any[]): void;
21
+ };
22
+ logError: {
23
+ (...data: any[]): void;
24
+ (...data: any[]): void;
25
+ (message?: any, ...optionalParams: any[]): void;
26
+ };
27
+ debug(message: string, ...args: unknown[]): void;
28
+ info(message: string, ...args: unknown[]): void;
29
+ warn(message: string, ...args: unknown[]): void;
30
+ error(message: string, ...args: unknown[]): void;
31
+ enabled(level: LogLevel): boolean;
32
+ }
33
+ export declare const logger: Logger;
@@ -1,6 +1,63 @@
1
+ import { isatty } from "node:tty";
1
2
  import debug from "debug";
2
- const base = debug("aigne");
3
- export const logger = Object.assign(debug, {
4
- core: base.extend("core"),
5
- mcp: base.extend("mcp"),
3
+ export var LogLevel;
4
+ (function (LogLevel) {
5
+ LogLevel["ERROR"] = "error";
6
+ LogLevel["WARN"] = "warn";
7
+ LogLevel["INFO"] = "info";
8
+ LogLevel["DEBUG"] = "debug";
9
+ })(LogLevel || (LogLevel = {}));
10
+ const levels = Object.values(LogLevel);
11
+ export class Logger {
12
+ constructor(options) {
13
+ this.level = options.level;
14
+ this.debugLogger = debug(`${options.ns}:debug`);
15
+ this.infoLogger = debug(`${options.ns}:info`);
16
+ this.warnLogger = debug(`${options.ns}:warn`);
17
+ this.errorLogger = debug(`${options.ns}:error`);
18
+ for (const logger of [this.debugLogger, this.infoLogger, this.warnLogger]) {
19
+ // @ts-ignore
20
+ logger.useColors = isatty(process.stdout.fd);
21
+ logger.enabled = true;
22
+ logger.log = (...args) => this.logMessage(...args);
23
+ }
24
+ this.errorLogger.log = (...args) => this.logError(...args);
25
+ // @ts-ignore
26
+ this.errorLogger.useColors = isatty(process.stderr.fd);
27
+ this.errorLogger.enabled = true;
28
+ }
29
+ level;
30
+ debugLogger;
31
+ infoLogger;
32
+ warnLogger;
33
+ errorLogger;
34
+ logMessage = console.log;
35
+ logError = console.error;
36
+ debug(message, ...args) {
37
+ if (this.enabled(LogLevel.DEBUG)) {
38
+ this.debugLogger(message, ...args);
39
+ }
40
+ }
41
+ info(message, ...args) {
42
+ if (this.enabled(LogLevel.INFO)) {
43
+ this.infoLogger(message, ...args);
44
+ }
45
+ }
46
+ warn(message, ...args) {
47
+ if (this.enabled(LogLevel.WARN)) {
48
+ this.warnLogger(message, ...args);
49
+ }
50
+ }
51
+ error(message, ...args) {
52
+ if (this.enabled(LogLevel.ERROR)) {
53
+ this.errorLogger(message, ...args);
54
+ }
55
+ }
56
+ enabled(level) {
57
+ return levels.indexOf(this.level) >= levels.indexOf(level);
58
+ }
59
+ }
60
+ export const logger = new Logger({
61
+ ns: "aigne:core",
62
+ level: LogLevel.INFO,
6
63
  });
@@ -1,3 +1,3 @@
1
- import type { ChatModelOutputUsage } from "../models/chat-model.js";
1
+ import type { ChatModelOutputUsage } from "../agents/chat-model.js";
2
2
  import type { Nullish } from "./type-utils.js";
3
3
  export declare function mergeUsage(...usages: Nullish<ChatModelOutputUsage>[]): ChatModelOutputUsage;
@@ -6,7 +6,7 @@ export declare function mergeAgentResponseChunk<T extends Message>(output: T, ch
6
6
  export declare function agentResponseStreamToObject<T extends Message>(stream: AgentResponseStream<T> | AgentProcessAsyncGenerator<T>): Promise<T>;
7
7
  export declare function asyncGeneratorToReadableStream<T extends Message>(generator: AgentProcessAsyncGenerator<T>): AgentResponseStream<T>;
8
8
  export declare function onAgentResponseStreamEnd<T extends Message>(stream: AgentResponseStream<T>, callback: (result: T) => PromiseOrValue<Partial<T> | void>, options?: {
9
- errorCallback?: (error: Error) => Error;
9
+ errorCallback?: (error: Error) => PromiseOrValue<Error>;
10
10
  processChunk?: (chunk: AgentResponseChunk<T>) => AgentResponseChunk<T>;
11
11
  }): ReadableStream<any>;
12
12
  export declare function isAsyncGenerator<T extends AsyncGenerator>(value: AsyncGenerator | unknown): value is T;
@@ -18,5 +18,6 @@ export declare function readableStreamToArray<T>(stream: ReadableStream<T>, opti
18
18
  export declare function readableStreamToArray<T>(stream: ReadableStream<T>, options?: {
19
19
  catchError?: false;
20
20
  }): Promise<T[]>;
21
- export declare function readableStreamToAsyncIterator<T>(stream: ReadableStream<T>): AsyncIterable<T>;
22
21
  export declare function stringToAgentResponseStream(str: string, key?: "text" | typeof MESSAGE_KEY | string): AgentResponseStream<Message>;
22
+ export declare function toReadableStream(stream: NodeJS.ReadStream): ReadableStream<Uint8Array<ArrayBufferLike>>;
23
+ export declare function readAllString(stream: NodeJS.ReadStream | ReadableStream): Promise<string>;
@@ -26,7 +26,7 @@ export function mergeAgentResponseChunk(output, chunk) {
26
26
  export async function agentResponseStreamToObject(stream) {
27
27
  const json = {};
28
28
  if (stream instanceof ReadableStream) {
29
- for await (const value of readableStreamToAsyncIterator(stream)) {
29
+ for await (const value of stream) {
30
30
  mergeAgentResponseChunk(json, value);
31
31
  }
32
32
  }
@@ -76,25 +76,29 @@ export function onAgentResponseStreamEnd(stream, callback, options) {
76
76
  return new ReadableStream({
77
77
  async pull(controller) {
78
78
  try {
79
- const { value, done } = await reader.read();
80
- if (!done) {
79
+ while (true) {
80
+ const { value, done } = await reader.read();
81
+ if (done) {
82
+ const result = await callback(json);
83
+ if (result && !equal(result, json)) {
84
+ let chunk = { delta: { json: result } };
85
+ if (options?.processChunk)
86
+ chunk = options.processChunk(chunk);
87
+ controller.enqueue(chunk);
88
+ }
89
+ controller.close();
90
+ return;
91
+ }
92
+ mergeAgentResponseChunk(json, value);
81
93
  const chunk = options?.processChunk ? options.processChunk(value) : value;
82
- if (!isEmptyChunk(chunk))
94
+ if (!isEmptyChunk(chunk)) {
83
95
  controller.enqueue(chunk);
84
- mergeAgentResponseChunk(json, value);
85
- return;
86
- }
87
- const result = await callback(json);
88
- if (result && !equal(result, json)) {
89
- let chunk = { delta: { json: result } };
90
- if (options?.processChunk)
91
- chunk = options.processChunk(chunk);
92
- controller.enqueue(chunk);
96
+ break;
97
+ }
93
98
  }
94
- controller.close();
95
99
  }
96
100
  catch (error) {
97
- controller.error(options?.errorCallback?.(error) ?? error);
101
+ controller.error((await options?.errorCallback?.(error)) ?? error);
98
102
  }
99
103
  },
100
104
  });
@@ -131,7 +135,7 @@ export function arrayToReadableStream(chunks) {
131
135
  export async function readableStreamToArray(stream, options) {
132
136
  const result = [];
133
137
  try {
134
- for await (const value of readableStreamToAsyncIterator(stream)) {
138
+ for await (const value of stream) {
135
139
  result.push(value);
136
140
  }
137
141
  }
@@ -142,17 +146,36 @@ export async function readableStreamToArray(stream, options) {
142
146
  }
143
147
  return result;
144
148
  }
145
- export async function* readableStreamToAsyncIterator(stream) {
146
- const reader = stream.getReader();
147
- while (true) {
148
- const { value, done } = await reader.read();
149
- if (done)
150
- break;
151
- yield value;
152
- }
153
- }
154
149
  export function stringToAgentResponseStream(str, key = "text") {
155
150
  const segmenter = new Intl.Segmenter(undefined, { granularity: "word" });
156
151
  const segments = segmenter.segment(str);
157
152
  return arrayToReadableStream(Array.from(segments).map((segment) => ({ delta: { text: { [key]: segment.segment } } })));
158
153
  }
154
+ export function toReadableStream(stream) {
155
+ return new ReadableStream({
156
+ start(controller) {
157
+ const onData = (chunk) => {
158
+ controller.enqueue(new Uint8Array(chunk));
159
+ };
160
+ const onEnd = () => {
161
+ cleanup();
162
+ controller.close();
163
+ };
164
+ const onError = (err) => {
165
+ cleanup();
166
+ controller.error(err);
167
+ };
168
+ function cleanup() {
169
+ stream.off("data", onData);
170
+ stream.off("end", onEnd);
171
+ stream.off("error", onError);
172
+ }
173
+ stream.on("data", onData);
174
+ stream.on("end", onEnd);
175
+ stream.on("error", onError);
176
+ },
177
+ });
178
+ }
179
+ export async function readAllString(stream) {
180
+ return (await readableStreamToArray((stream instanceof ReadableStream ? stream : toReadableStream(stream)).pipeThrough(new TextDecoderStream()))).join("");
181
+ }
@@ -2,6 +2,11 @@ import { type ZodType } from "zod";
2
2
  export type PromiseOrValue<T> = T | Promise<T>;
3
3
  export type Nullish<T> = T | null | undefined | void;
4
4
  export type OmitPropertiesFromArrayFirstElement<T extends unknown[], K extends string | number | symbol> = T extends [infer U, ...infer Rest] ? [Omit<U, K>, ...Rest] : never;
5
+ export type XOr<T, K extends keyof T, O extends keyof T> = (Omit<T, O> & {
6
+ [key in O]?: undefined;
7
+ }) | (Omit<T, K> & {
8
+ [key in K]?: undefined;
9
+ });
5
10
  export declare function isNil(value: unknown): value is null | undefined;
6
11
  export declare function isRecord<T>(value: unknown): value is Record<string, T>;
7
12
  export declare function isEmpty(obj: unknown): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.14.0",
3
+ "version": "1.16.0",
4
4
  "description": "AIGNE core library for building AI-powered applications",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -50,9 +50,6 @@
50
50
  "loader/*": [
51
51
  "./lib/dts/loader/*"
52
52
  ],
53
- "models/*": [
54
- "./lib/dts/models/*"
55
- ],
56
53
  "memory/*": [
57
54
  "./lib/dts/memory/*"
58
55
  ],
@@ -61,12 +58,6 @@
61
58
  ],
62
59
  "utils/*": [
63
60
  "./lib/dts/utils/*"
64
- ],
65
- "client/*": [
66
- "./lib/dts/client/*"
67
- ],
68
- "server/*": [
69
- "./lib/dts/server/*"
70
61
  ]
71
62
  }
72
63
  },
@@ -91,15 +82,7 @@
91
82
  "zod": "^3.24.4",
92
83
  "zod-to-json-schema": "^3.24.5"
93
84
  },
94
- "peerDependencies": {
95
- "@anthropic-ai/sdk": "^0.39.0",
96
- "@aws-sdk/client-bedrock-runtime": "^3.796.0",
97
- "@google/generative-ai": "^0.24.0",
98
- "openai": "^4.87.3"
99
- },
100
85
  "devDependencies": {
101
- "@anthropic-ai/sdk": "^0.41.0",
102
- "@google/generative-ai": "^0.24.1",
103
86
  "@types/bun": "^1.2.12",
104
87
  "@types/compression": "^1.7.5",
105
88
  "@types/content-type": "^1.1.8",
@@ -109,9 +92,7 @@
109
92
  "compression": "^1.8.0",
110
93
  "detect-port": "^2.1.0",
111
94
  "express": "^5.1.0",
112
- "hono": "^4.7.8",
113
95
  "npm-run-all": "^4.1.5",
114
- "openai": "^4.97.0",
115
96
  "rimraf": "^6.0.1",
116
97
  "typescript": "^5.8.3"
117
98
  },
@@ -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,87 +0,0 @@
1
- "use strict";
2
- /**
3
- * Client module used to interact with the AIGNE framework.
4
- */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.AIGNEClient = void 0;
7
- const event_stream_js_1 = require("../utils/event-stream.js");
8
- const type_utils_js_1 = require("../utils/type-utils.js");
9
- /**
10
- * Client for interacting with a remote AIGNE server.
11
- * AIGNEClient provides a client-side interface that matches the AIGNE API,
12
- * allowing applications to invoke agents and receive responses from a remote AIGNE instance.
13
- *
14
- * @example
15
- * Here's a simple example of how to use AIGNEClient:
16
- * {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
17
- *
18
- * @example
19
- * Here's an example of how to use AIGNEClient with streaming response:
20
- * {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
21
- */
22
- class AIGNEClient {
23
- options;
24
- /**
25
- * Creates a new AIGNEClient instance.
26
- *
27
- * @param options - Configuration options for connecting to the AIGNE server
28
- */
29
- constructor(options) {
30
- this.options = options;
31
- }
32
- async invoke(agent, input, options) {
33
- // Send the agent invocation request to the AIGNE server
34
- const response = await this.fetch(this.options.url, {
35
- ...options?.fetchOptions,
36
- method: "POST",
37
- headers: {
38
- "Content-Type": "application/json",
39
- ...options?.fetchOptions?.headers,
40
- },
41
- body: JSON.stringify({ agent, input, options }),
42
- });
43
- // For non-streaming responses, simply parse the JSON response and return it
44
- if (!options?.streaming) {
45
- return await response.json();
46
- }
47
- // For streaming responses, set up the streaming pipeline
48
- const stream = response.body;
49
- if (!stream)
50
- throw new Error("Response body is not a stream");
51
- // Process the stream through a series of transforms:
52
- // 1. Convert bytes to text
53
- // 2. Parse SSE format into structured events
54
- // 3. Convert events into a standardized agent response stream
55
- return stream
56
- .pipeThrough(new TextDecoderStream())
57
- .pipeThrough(new event_stream_js_1.EventStreamParser())
58
- .pipeThrough(new event_stream_js_1.AgentResponseStreamParser());
59
- }
60
- /**
61
- * Enhanced fetch method that handles error responses from the AIGNE server.
62
- * This method wraps the standard fetch API to provide better error handling and reporting.
63
- *
64
- * @param args - Standard fetch API arguments (url and options)
65
- * @returns A Response object if the request was successful
66
- * @throws Error with detailed information if the request failed
67
- *
68
- * @private
69
- */
70
- async fetch(...args) {
71
- const result = await globalThis.fetch(...args);
72
- if (!result.ok) {
73
- let message;
74
- try {
75
- const text = await result.text();
76
- const json = (0, type_utils_js_1.tryOrThrow)(() => JSON.parse(text));
77
- message = json?.error?.message || text;
78
- }
79
- catch {
80
- // ignore
81
- }
82
- throw new Error(`Failed to fetch url ${args[0]} with status ${result.status}: ${message}`);
83
- }
84
- return result;
85
- }
86
- }
87
- exports.AIGNEClient = AIGNEClient;
@@ -1 +0,0 @@
1
- export * from "./client.js";
@@ -1,17 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./client.js"), exports);
@@ -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
- }