@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,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,405 +0,0 @@
1
- import { nanoid } from "nanoid";
2
- import OpenAI from "openai";
3
- import { z } from "zod";
4
- import { parseJSON } from "../utils/json-schema.js";
5
- import { mergeUsage } from "../utils/model-utils.js";
6
- import { getJsonOutputPrompt } from "../utils/prompts.js";
7
- import { agentResponseStreamToObject } from "../utils/stream-utils.js";
8
- import { checkArguments, isNonNullable } from "../utils/type-utils.js";
9
- import { ChatModel, } from "./chat-model.js";
10
- const CHAT_MODEL_OPENAI_DEFAULT_MODEL = "gpt-4o-mini";
11
- const OPENAI_CHAT_MODEL_CAPABILITIES = {
12
- "o4-mini": { supportsParallelToolCalls: false, supportsTemperature: false },
13
- "o3-mini": { supportsParallelToolCalls: false, supportsTemperature: false },
14
- };
15
- /**
16
- * @hidden
17
- */
18
- export const openAIChatModelOptionsSchema = z.object({
19
- apiKey: z.string().optional(),
20
- baseURL: z.string().optional(),
21
- model: z.string().optional(),
22
- modelOptions: z
23
- .object({
24
- model: z.string().optional(),
25
- temperature: z.number().optional(),
26
- topP: z.number().optional(),
27
- frequencyPenalty: z.number().optional(),
28
- presencePenalty: z.number().optional(),
29
- parallelToolCalls: z.boolean().optional().default(true),
30
- })
31
- .optional(),
32
- });
33
- /**
34
- * Implementation of the ChatModel interface for OpenAI's API
35
- *
36
- * This model provides access to OpenAI's capabilities including:
37
- * - Text generation
38
- * - Tool use with parallel tool calls
39
- * - JSON structured output
40
- * - Image understanding
41
- *
42
- * Default model: 'gpt-4o-mini'
43
- *
44
- * @example
45
- * Here's how to create and use an OpenAI chat model:
46
- * {@includeCode ../../test/models/openai-chat-model.test.ts#example-openai-chat-model}
47
- *
48
- * @example
49
- * Here's an example with streaming response:
50
- * {@includeCode ../../test/models/openai-chat-model.test.ts#example-openai-chat-model-streaming}
51
- */
52
- export class OpenAIChatModel extends ChatModel {
53
- options;
54
- constructor(options) {
55
- super();
56
- this.options = options;
57
- if (options)
58
- checkArguments(this.name, openAIChatModelOptionsSchema, options);
59
- const preset = options?.model ? OPENAI_CHAT_MODEL_CAPABILITIES[options.model] : undefined;
60
- Object.assign(this, preset);
61
- }
62
- /**
63
- * @hidden
64
- */
65
- _client;
66
- apiKeyEnvName = "OPENAI_API_KEY";
67
- apiKeyDefault;
68
- supportsNativeStructuredOutputs = true;
69
- supportsEndWithSystemMessage = true;
70
- supportsToolsUseWithJsonSchema = true;
71
- supportsParallelToolCalls = true;
72
- supportsToolsEmptyParameters = true;
73
- supportsToolStreaming = true;
74
- supportsTemperature = true;
75
- get client() {
76
- const apiKey = this.options?.apiKey || process.env[this.apiKeyEnvName] || this.apiKeyDefault;
77
- if (!apiKey)
78
- throw new Error(`Api Key is required for ${this.name}`);
79
- this._client ??= new OpenAI({
80
- baseURL: this.options?.baseURL,
81
- apiKey,
82
- });
83
- return this._client;
84
- }
85
- get modelOptions() {
86
- return this.options?.modelOptions;
87
- }
88
- /**
89
- * Process the input and generate a response
90
- * @param input The input to process
91
- * @returns The generated response
92
- */
93
- process(input) {
94
- return this._process(input);
95
- }
96
- async _process(input) {
97
- const messages = await this.getRunMessages(input);
98
- const body = {
99
- model: this.options?.model || CHAT_MODEL_OPENAI_DEFAULT_MODEL,
100
- temperature: this.supportsTemperature
101
- ? (input.modelOptions?.temperature ?? this.modelOptions?.temperature)
102
- : undefined,
103
- top_p: input.modelOptions?.topP ?? this.modelOptions?.topP,
104
- frequency_penalty: input.modelOptions?.frequencyPenalty ?? this.modelOptions?.frequencyPenalty,
105
- presence_penalty: input.modelOptions?.presencePenalty ?? this.modelOptions?.presencePenalty,
106
- messages,
107
- stream_options: {
108
- include_usage: true,
109
- },
110
- stream: true,
111
- };
112
- const { jsonMode, responseFormat } = await this.getRunResponseFormat(input);
113
- const stream = await this.client.chat.completions.create({
114
- ...body,
115
- tools: toolsFromInputTools(input.tools, {
116
- addTypeToEmptyParameters: !this.supportsToolsEmptyParameters,
117
- }),
118
- tool_choice: input.toolChoice,
119
- parallel_tool_calls: this.getParallelToolCalls(input),
120
- response_format: responseFormat,
121
- });
122
- if (input.responseFormat?.type !== "json_schema") {
123
- return await this.extractResultFromStream(stream, false, true);
124
- }
125
- const result = await this.extractResultFromStream(stream, jsonMode);
126
- if (!this.supportsToolsUseWithJsonSchema &&
127
- !result.toolCalls?.length &&
128
- input.responseFormat?.type === "json_schema" &&
129
- result.text) {
130
- const output = await this.requestStructuredOutput(body, input.responseFormat);
131
- return { ...output, usage: mergeUsage(result.usage, output.usage) };
132
- }
133
- return result;
134
- }
135
- getParallelToolCalls(input) {
136
- if (!this.supportsParallelToolCalls)
137
- return undefined;
138
- if (!input.tools?.length)
139
- return undefined;
140
- return input.modelOptions?.parallelToolCalls ?? this.modelOptions?.parallelToolCalls;
141
- }
142
- async getRunMessages(input) {
143
- const messages = await contentsFromInputMessages(input.messages);
144
- if (!this.supportsEndWithSystemMessage && messages.at(-1)?.role !== "user") {
145
- messages.push({ role: "user", content: "" });
146
- }
147
- if (!this.supportsToolsUseWithJsonSchema && input.tools?.length)
148
- return messages;
149
- if (this.supportsNativeStructuredOutputs)
150
- return messages;
151
- if (input.responseFormat?.type === "json_schema") {
152
- messages.unshift({
153
- role: "system",
154
- content: getJsonOutputPrompt(input.responseFormat.jsonSchema.schema),
155
- });
156
- }
157
- return messages;
158
- }
159
- async getRunResponseFormat(input) {
160
- if (!this.supportsToolsUseWithJsonSchema && input.tools?.length)
161
- return { jsonMode: false, responseFormat: undefined };
162
- if (!this.supportsNativeStructuredOutputs) {
163
- const jsonMode = input.responseFormat?.type === "json_schema";
164
- return { jsonMode, responseFormat: jsonMode ? { type: "json_object" } : undefined };
165
- }
166
- if (input.responseFormat?.type === "json_schema") {
167
- return {
168
- jsonMode: true,
169
- responseFormat: {
170
- type: "json_schema",
171
- json_schema: {
172
- ...input.responseFormat.jsonSchema,
173
- schema: jsonSchemaToOpenAIJsonSchema(input.responseFormat.jsonSchema.schema),
174
- },
175
- },
176
- };
177
- }
178
- return { jsonMode: false, responseFormat: undefined };
179
- }
180
- async requestStructuredOutput(body, responseFormat) {
181
- if (responseFormat?.type !== "json_schema") {
182
- throw new Error("Expected json_schema response format");
183
- }
184
- const { jsonMode, responseFormat: resolvedResponseFormat } = await this.getRunResponseFormat({
185
- responseFormat,
186
- });
187
- const res = await this.client.chat.completions.create({
188
- ...body,
189
- response_format: resolvedResponseFormat,
190
- });
191
- return this.extractResultFromStream(res, jsonMode);
192
- }
193
- async extractResultFromStream(stream, jsonMode, streaming) {
194
- const result = new ReadableStream({
195
- start: async (controller) => {
196
- try {
197
- let text = "";
198
- let refusal = "";
199
- const toolCalls = [];
200
- let model;
201
- for await (const chunk of stream) {
202
- const choice = chunk.choices?.[0];
203
- if (!model) {
204
- model = chunk.model;
205
- controller.enqueue({
206
- delta: {
207
- json: {
208
- model,
209
- },
210
- },
211
- });
212
- }
213
- if (choice?.delta.tool_calls?.length) {
214
- for (const call of choice.delta.tool_calls) {
215
- if (this.supportsToolStreaming && call.index !== undefined) {
216
- handleToolCallDelta(toolCalls, call);
217
- }
218
- else {
219
- handleCompleteToolCall(toolCalls, call);
220
- }
221
- }
222
- }
223
- if (choice?.delta.content) {
224
- text += choice.delta.content;
225
- if (!jsonMode) {
226
- controller.enqueue({
227
- delta: {
228
- text: {
229
- text: choice.delta.content,
230
- },
231
- },
232
- });
233
- }
234
- }
235
- if (choice?.delta.refusal) {
236
- refusal += choice.delta.refusal;
237
- if (!jsonMode) {
238
- controller.enqueue({
239
- delta: {
240
- text: { text: choice.delta.refusal },
241
- },
242
- });
243
- }
244
- }
245
- if (chunk.usage) {
246
- controller.enqueue({
247
- delta: {
248
- json: {
249
- usage: {
250
- inputTokens: chunk.usage.prompt_tokens,
251
- outputTokens: chunk.usage.completion_tokens,
252
- },
253
- },
254
- },
255
- });
256
- }
257
- }
258
- text = text || refusal;
259
- if (jsonMode && text) {
260
- controller.enqueue({
261
- delta: {
262
- json: {
263
- json: parseJSON(text),
264
- },
265
- },
266
- });
267
- }
268
- if (toolCalls.length) {
269
- controller.enqueue({
270
- delta: {
271
- json: {
272
- toolCalls: toolCalls.map(({ args, ...c }) => ({
273
- ...c,
274
- function: { ...c.function, arguments: parseJSON(args) },
275
- })),
276
- },
277
- },
278
- });
279
- }
280
- controller.close();
281
- }
282
- catch (error) {
283
- controller.error(error);
284
- }
285
- },
286
- });
287
- return streaming ? result : await agentResponseStreamToObject(result);
288
- }
289
- }
290
- /**
291
- * @hidden
292
- */
293
- export const ROLE_MAP = {
294
- system: "system",
295
- user: "user",
296
- agent: "assistant",
297
- tool: "tool",
298
- };
299
- /**
300
- * @hidden
301
- */
302
- export async function contentsFromInputMessages(messages) {
303
- return messages.map((i) => ({
304
- role: ROLE_MAP[i.role],
305
- content: typeof i.content === "string"
306
- ? i.content
307
- : i.content
308
- ?.map((c) => {
309
- if (c.type === "text") {
310
- return { type: "text", text: c.text };
311
- }
312
- if (c.type === "image_url") {
313
- return {
314
- type: "image_url",
315
- image_url: { url: c.url },
316
- };
317
- }
318
- })
319
- .filter(isNonNullable),
320
- tool_calls: i.toolCalls?.map((i) => ({
321
- ...i,
322
- function: {
323
- ...i.function,
324
- arguments: JSON.stringify(i.function.arguments),
325
- },
326
- })),
327
- tool_call_id: i.toolCallId,
328
- name: i.name,
329
- }));
330
- }
331
- /**
332
- * @hidden
333
- */
334
- export function toolsFromInputTools(tools, options) {
335
- return tools?.length
336
- ? tools.map((i) => {
337
- const parameters = i.function.parameters;
338
- if (options?.addTypeToEmptyParameters && Object.keys(parameters).length === 0) {
339
- parameters.type = "object";
340
- }
341
- return {
342
- type: "function",
343
- function: {
344
- name: i.function.name,
345
- description: i.function.description,
346
- parameters,
347
- },
348
- };
349
- })
350
- : undefined;
351
- }
352
- /**
353
- * @hidden
354
- */
355
- export function jsonSchemaToOpenAIJsonSchema(schema) {
356
- if (schema?.type === "object") {
357
- const { required, properties } = schema;
358
- return {
359
- ...schema,
360
- properties: Object.fromEntries(Object.entries(properties).map(([key, value]) => {
361
- const valueSchema = jsonSchemaToOpenAIJsonSchema(value);
362
- // NOTE: All fields must be required https://platform.openai.com/docs/guides/structured-outputs/all-fields-must-be-required
363
- return [
364
- key,
365
- required?.includes(key) ? valueSchema : { anyOf: [valueSchema, { type: ["null"] }] },
366
- ];
367
- })),
368
- required: Object.keys(properties),
369
- };
370
- }
371
- if (schema?.type === "array") {
372
- const { items } = schema;
373
- return {
374
- ...schema,
375
- items: jsonSchemaToOpenAIJsonSchema(items),
376
- };
377
- }
378
- return schema;
379
- }
380
- function handleToolCallDelta(toolCalls, call) {
381
- toolCalls[call.index] ??= {
382
- id: call.id || nanoid(),
383
- type: "function",
384
- function: { name: "", arguments: {} },
385
- args: "",
386
- };
387
- const c = toolCalls[call.index];
388
- if (!c)
389
- throw new Error("Tool call not found");
390
- if (call.type)
391
- c.type = call.type;
392
- c.function.name = c.function.name + (call.function?.name || "");
393
- c.args = c.args.concat(call.function?.arguments || "");
394
- }
395
- function handleCompleteToolCall(toolCalls, call) {
396
- toolCalls.push({
397
- id: call.id || nanoid(),
398
- type: "function",
399
- function: {
400
- name: call.function?.name || "",
401
- arguments: parseJSON(call.function?.arguments || "{}"),
402
- },
403
- args: call.function?.arguments || "",
404
- });
405
- }
@@ -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,29 +0,0 @@
1
- import { OpenAIChatModel } from "./openai-chat-model.js";
2
- const XAI_DEFAULT_CHAT_MODEL = "grok-2-latest";
3
- const XAI_BASE_URL = "https://api.x.ai/v1";
4
- /**
5
- * Implementation of the ChatModel interface for X.AI's API (Grok)
6
- *
7
- * This model uses OpenAI-compatible API format to interact with X.AI models,
8
- * providing access to models like Grok.
9
- *
10
- * Default model: 'grok-2-latest'
11
- *
12
- * @example
13
- * Here's how to create and use an X.AI chat model:
14
- * {@includeCode ../../test/models/xai-chat-model.test.ts#example-xai-chat-model}
15
- *
16
- * @example
17
- * Here's an example with streaming response:
18
- * {@includeCode ../../test/models/xai-chat-model.test.ts#example-xai-chat-model-streaming}
19
- */
20
- export class XAIChatModel extends OpenAIChatModel {
21
- constructor(options) {
22
- super({
23
- ...options,
24
- model: options?.model || XAI_DEFAULT_CHAT_MODEL,
25
- baseURL: options?.baseURL || XAI_BASE_URL,
26
- });
27
- }
28
- apiKeyEnvName = "XAI_API_KEY";
29
- }
@@ -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,18 +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 class ServerError extends Error {
7
- status;
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, message) {
15
- super(message);
16
- this.status = status;
17
- }
18
- }
@@ -1,2 +0,0 @@
1
- export * from "./error.js";
2
- export * from "./server.js";
@@ -1,2 +0,0 @@
1
- export * from "./error.js";
2
- export * from "./server.js";