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