@aigne/core 1.13.0 → 1.14.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.
- package/CHANGELOG.md +15 -0
- package/README.md +13 -26
- package/README.zh.md +24 -37
- package/lib/cjs/agents/agent.d.ts +522 -15
- package/lib/cjs/agents/agent.js +357 -36
- package/lib/cjs/agents/ai-agent.d.ts +210 -52
- package/lib/cjs/agents/ai-agent.js +182 -24
- package/lib/cjs/agents/mcp-agent.d.ts +112 -0
- package/lib/cjs/agents/mcp-agent.js +79 -1
- package/lib/cjs/agents/team-agent.d.ts +99 -0
- package/lib/cjs/agents/team-agent.js +94 -0
- package/lib/cjs/agents/user-agent.d.ts +6 -4
- package/lib/cjs/agents/user-agent.js +16 -5
- package/lib/cjs/aigne/aigne.d.ts +263 -16
- package/lib/cjs/aigne/aigne.js +130 -20
- package/lib/cjs/aigne/context.d.ts +24 -8
- package/lib/cjs/aigne/context.js +8 -22
- package/lib/cjs/aigne/message-queue.d.ts +26 -4
- package/lib/cjs/aigne/message-queue.js +42 -7
- package/lib/cjs/aigne/usage.d.ts +9 -0
- package/lib/cjs/aigne/usage.js +3 -0
- package/lib/cjs/client/client.d.ts +81 -3
- package/lib/cjs/client/client.js +38 -0
- package/lib/cjs/client/index.d.ts +1 -0
- package/lib/cjs/client/index.js +17 -0
- package/lib/cjs/index.d.ts +0 -1
- package/lib/cjs/index.js +0 -1
- package/lib/cjs/loader/agent-js.d.ts +1 -1
- package/lib/cjs/loader/agent-js.js +2 -2
- package/lib/cjs/loader/agent-yaml.d.ts +3 -2
- package/lib/cjs/loader/agent-yaml.js +2 -1
- package/lib/cjs/loader/index.d.ts +4 -4
- package/lib/cjs/memory/default-memory.d.ts +16 -0
- package/lib/cjs/memory/default-memory.js +70 -0
- package/lib/cjs/memory/index.d.ts +3 -0
- package/lib/cjs/memory/index.js +19 -0
- package/lib/cjs/memory/memory.d.ts +89 -0
- package/lib/cjs/memory/memory.js +132 -0
- package/lib/cjs/memory/recorder.d.ts +86 -0
- package/lib/cjs/memory/recorder.js +50 -0
- package/lib/cjs/memory/retriever.d.ts +99 -0
- package/lib/cjs/memory/retriever.js +51 -0
- package/lib/cjs/models/bedrock-chat-model.d.ts +12 -3
- package/lib/cjs/models/bedrock-chat-model.js +54 -24
- package/lib/cjs/models/chat-model.d.ts +278 -1
- package/lib/cjs/models/chat-model.js +54 -0
- package/lib/cjs/models/claude-chat-model.d.ts +49 -3
- package/lib/cjs/models/claude-chat-model.js +34 -2
- package/lib/cjs/models/deepseek-chat-model.d.ts +16 -0
- package/lib/cjs/models/deepseek-chat-model.js +16 -0
- package/lib/cjs/models/gemini-chat-model.d.ts +14 -0
- package/lib/cjs/models/gemini-chat-model.js +14 -0
- package/lib/cjs/models/ollama-chat-model.d.ts +16 -0
- package/lib/cjs/models/ollama-chat-model.js +16 -0
- package/lib/cjs/models/open-router-chat-model.d.ts +16 -0
- package/lib/cjs/models/open-router-chat-model.js +16 -0
- package/lib/cjs/models/openai-chat-model.d.ts +67 -3
- package/lib/cjs/models/openai-chat-model.js +47 -2
- package/lib/cjs/models/xai-chat-model.d.ts +16 -0
- package/lib/cjs/models/xai-chat-model.js +16 -0
- package/lib/cjs/prompt/prompt-builder.d.ts +4 -4
- package/lib/cjs/prompt/prompt-builder.js +19 -18
- package/lib/cjs/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/cjs/prompt/prompts/memory-message-template.js +10 -0
- package/lib/cjs/prompt/template.js +5 -1
- package/lib/cjs/server/error.d.ts +11 -0
- package/lib/cjs/server/error.js +11 -0
- package/lib/cjs/server/index.d.ts +2 -0
- package/lib/cjs/server/index.js +18 -0
- package/lib/cjs/server/server.d.ts +89 -8
- package/lib/cjs/server/server.js +58 -0
- package/lib/cjs/utils/fs.d.ts +2 -0
- package/lib/cjs/utils/fs.js +25 -0
- package/lib/cjs/utils/prompts.d.ts +1 -0
- package/lib/cjs/utils/prompts.js +11 -2
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/utils/type-utils.js +14 -0
- package/lib/dts/agents/agent.d.ts +522 -15
- package/lib/dts/agents/ai-agent.d.ts +210 -52
- package/lib/dts/agents/mcp-agent.d.ts +112 -0
- package/lib/dts/agents/team-agent.d.ts +99 -0
- package/lib/dts/agents/user-agent.d.ts +6 -4
- package/lib/dts/aigne/aigne.d.ts +263 -16
- package/lib/dts/aigne/context.d.ts +24 -8
- package/lib/dts/aigne/message-queue.d.ts +26 -4
- package/lib/dts/aigne/usage.d.ts +9 -0
- package/lib/dts/client/client.d.ts +81 -3
- package/lib/dts/client/index.d.ts +1 -0
- package/lib/dts/index.d.ts +0 -1
- package/lib/dts/loader/agent-js.d.ts +1 -1
- package/lib/dts/loader/agent-yaml.d.ts +3 -2
- package/lib/dts/loader/index.d.ts +4 -4
- package/lib/dts/memory/default-memory.d.ts +16 -0
- package/lib/dts/memory/index.d.ts +3 -0
- package/lib/dts/memory/memory.d.ts +89 -0
- package/lib/dts/memory/recorder.d.ts +86 -0
- package/lib/dts/memory/retriever.d.ts +99 -0
- package/lib/dts/models/bedrock-chat-model.d.ts +12 -3
- package/lib/dts/models/chat-model.d.ts +278 -1
- package/lib/dts/models/claude-chat-model.d.ts +49 -3
- package/lib/dts/models/deepseek-chat-model.d.ts +16 -0
- package/lib/dts/models/gemini-chat-model.d.ts +14 -0
- package/lib/dts/models/ollama-chat-model.d.ts +16 -0
- package/lib/dts/models/open-router-chat-model.d.ts +16 -0
- package/lib/dts/models/openai-chat-model.d.ts +67 -3
- package/lib/dts/models/xai-chat-model.d.ts +16 -0
- package/lib/dts/prompt/prompt-builder.d.ts +4 -4
- package/lib/dts/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/dts/server/error.d.ts +11 -0
- package/lib/dts/server/index.d.ts +2 -0
- package/lib/dts/server/server.d.ts +89 -8
- package/lib/dts/utils/fs.d.ts +2 -0
- package/lib/dts/utils/prompts.d.ts +1 -0
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/esm/agents/agent.d.ts +522 -15
- package/lib/esm/agents/agent.js +351 -35
- package/lib/esm/agents/ai-agent.d.ts +210 -52
- package/lib/esm/agents/ai-agent.js +183 -25
- package/lib/esm/agents/mcp-agent.d.ts +112 -0
- package/lib/esm/agents/mcp-agent.js +79 -1
- package/lib/esm/agents/team-agent.d.ts +99 -0
- package/lib/esm/agents/team-agent.js +94 -0
- package/lib/esm/agents/user-agent.d.ts +6 -4
- package/lib/esm/agents/user-agent.js +17 -6
- package/lib/esm/aigne/aigne.d.ts +263 -16
- package/lib/esm/aigne/aigne.js +132 -22
- package/lib/esm/aigne/context.d.ts +24 -8
- package/lib/esm/aigne/context.js +9 -22
- package/lib/esm/aigne/message-queue.d.ts +26 -4
- package/lib/esm/aigne/message-queue.js +42 -8
- package/lib/esm/aigne/usage.d.ts +9 -0
- package/lib/esm/aigne/usage.js +3 -0
- package/lib/esm/client/client.d.ts +81 -3
- package/lib/esm/client/client.js +38 -0
- package/lib/esm/client/index.d.ts +1 -0
- package/lib/esm/client/index.js +1 -0
- package/lib/esm/index.d.ts +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/loader/agent-js.d.ts +1 -1
- package/lib/esm/loader/agent-js.js +2 -2
- package/lib/esm/loader/agent-yaml.d.ts +3 -2
- package/lib/esm/loader/agent-yaml.js +2 -1
- package/lib/esm/loader/index.d.ts +4 -4
- package/lib/esm/memory/default-memory.d.ts +16 -0
- package/lib/esm/memory/default-memory.js +63 -0
- package/lib/esm/memory/index.d.ts +3 -0
- package/lib/esm/memory/index.js +3 -0
- package/lib/esm/memory/memory.d.ts +89 -0
- package/lib/esm/memory/memory.js +127 -0
- package/lib/esm/memory/recorder.d.ts +86 -0
- package/lib/esm/memory/recorder.js +46 -0
- package/lib/esm/memory/retriever.d.ts +99 -0
- package/lib/esm/memory/retriever.js +47 -0
- package/lib/esm/models/bedrock-chat-model.d.ts +12 -3
- package/lib/esm/models/bedrock-chat-model.js +56 -26
- package/lib/esm/models/chat-model.d.ts +278 -1
- package/lib/esm/models/chat-model.js +54 -0
- package/lib/esm/models/claude-chat-model.d.ts +49 -3
- package/lib/esm/models/claude-chat-model.js +35 -3
- package/lib/esm/models/deepseek-chat-model.d.ts +16 -0
- package/lib/esm/models/deepseek-chat-model.js +16 -0
- package/lib/esm/models/gemini-chat-model.d.ts +14 -0
- package/lib/esm/models/gemini-chat-model.js +14 -0
- package/lib/esm/models/ollama-chat-model.d.ts +16 -0
- package/lib/esm/models/ollama-chat-model.js +16 -0
- package/lib/esm/models/open-router-chat-model.d.ts +16 -0
- package/lib/esm/models/open-router-chat-model.js +16 -0
- package/lib/esm/models/openai-chat-model.d.ts +67 -3
- package/lib/esm/models/openai-chat-model.js +47 -2
- package/lib/esm/models/xai-chat-model.d.ts +16 -0
- package/lib/esm/models/xai-chat-model.js +16 -0
- package/lib/esm/prompt/prompt-builder.d.ts +4 -4
- package/lib/esm/prompt/prompt-builder.js +20 -19
- package/lib/esm/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/esm/prompt/prompts/memory-message-template.js +7 -0
- package/lib/esm/prompt/template.js +5 -1
- package/lib/esm/server/error.d.ts +11 -0
- package/lib/esm/server/error.js +11 -0
- package/lib/esm/server/index.d.ts +2 -0
- package/lib/esm/server/index.js +2 -0
- package/lib/esm/server/server.d.ts +89 -8
- package/lib/esm/server/server.js +58 -0
- package/lib/esm/utils/fs.d.ts +2 -0
- package/lib/esm/utils/fs.js +21 -0
- package/lib/esm/utils/prompts.d.ts +1 -0
- package/lib/esm/utils/prompts.js +10 -2
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/type-utils.js +13 -0
- package/package.json +14 -11
- package/lib/cjs/agents/memory.d.ts +0 -26
- package/lib/cjs/agents/memory.js +0 -45
- package/lib/dts/agents/memory.d.ts +0 -26
- package/lib/esm/agents/memory.d.ts +0 -26
- package/lib/esm/agents/memory.js +0 -41
- /package/{LICENSE → LICENSE.md} +0 -0
|
@@ -1,4 +1,20 @@
|
|
|
1
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
|
+
*/
|
|
2
18
|
export declare class OllamaChatModel extends OpenAIChatModel {
|
|
3
19
|
constructor(options?: OpenAIChatModelOptions);
|
|
4
20
|
protected apiKeyEnvName: string;
|
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import { OpenAIChatModel } from "./openai-chat-model.js";
|
|
2
2
|
const OLLAMA_DEFAULT_BASE_URL = "http://localhost:11434/v1";
|
|
3
3
|
const OLLAMA_DEFAULT_CHAT_MODEL = "llama3.2";
|
|
4
|
+
/**
|
|
5
|
+
* Implementation of the ChatModel interface for Ollama
|
|
6
|
+
*
|
|
7
|
+
* This model allows you to run open-source LLMs locally using Ollama,
|
|
8
|
+
* with an OpenAI-compatible API interface.
|
|
9
|
+
*
|
|
10
|
+
* Default model: 'llama3.2'
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* Here's how to create and use an Ollama chat model:
|
|
14
|
+
* {@includeCode ../../test/models/ollama-chat-model.test.ts#example-ollama-chat-model}
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* Here's an example with streaming response:
|
|
18
|
+
* {@includeCode ../../test/models/ollama-chat-model.test.ts#example-ollama-chat-model-streaming}
|
|
19
|
+
*/
|
|
4
20
|
export class OllamaChatModel extends OpenAIChatModel {
|
|
5
21
|
constructor(options) {
|
|
6
22
|
super({
|
|
@@ -1,4 +1,20 @@
|
|
|
1
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
|
+
*/
|
|
2
18
|
export declare class OpenRouterChatModel extends OpenAIChatModel {
|
|
3
19
|
constructor(options?: OpenAIChatModelOptions);
|
|
4
20
|
protected apiKeyEnvName: string;
|
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import { OpenAIChatModel } from "./openai-chat-model.js";
|
|
2
2
|
const OPEN_ROUTER_DEFAULT_CHAT_MODEL = "openai/gpt-4o";
|
|
3
3
|
const OPEN_ROUTER_BASE_URL = "https://openrouter.ai/api/v1";
|
|
4
|
+
/**
|
|
5
|
+
* Implementation of the ChatModel interface for OpenRouter service
|
|
6
|
+
*
|
|
7
|
+
* OpenRouter provides access to a variety of large language models through a unified API.
|
|
8
|
+
* This implementation uses the OpenAI-compatible interface to connect to OpenRouter's service.
|
|
9
|
+
*
|
|
10
|
+
* Default model: 'openai/gpt-4o'
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* Here's how to create and use an OpenRouter chat model:
|
|
14
|
+
* {@includeCode ../../test/models/open-router-chat-model.test.ts#example-openrouter-chat-model}
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* Here's an example with streaming response:
|
|
18
|
+
* {@includeCode ../../test/models/open-router-chat-model.test.ts#example-openrouter-chat-model-streaming}
|
|
19
|
+
*/
|
|
4
20
|
export class OpenRouterChatModel extends OpenAIChatModel {
|
|
5
21
|
constructor(options) {
|
|
6
22
|
super({
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
2
|
import type { ChatCompletionMessageParam, ChatCompletionTool } from "openai/resources";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import type {
|
|
5
|
-
import type
|
|
4
|
+
import type { AgentProcessResult } from "../agents/agent.js";
|
|
5
|
+
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
6
6
|
import { ChatModel, type ChatModelInput, type ChatModelInputMessage, type ChatModelInputTool, type ChatModelOptions, type ChatModelOutput, type Role } from "./chat-model.js";
|
|
7
7
|
export interface OpenAIChatModelCapabilities {
|
|
8
8
|
supportsNativeStructuredOutputs: boolean;
|
|
@@ -13,12 +13,36 @@ export interface OpenAIChatModelCapabilities {
|
|
|
13
13
|
supportsToolStreaming: boolean;
|
|
14
14
|
supportsTemperature: boolean;
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Configuration options for OpenAI Chat Model
|
|
18
|
+
*/
|
|
16
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
|
+
*/
|
|
17
25
|
apiKey?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Base URL for OpenAI API
|
|
28
|
+
*
|
|
29
|
+
* Useful for proxies or alternate endpoints
|
|
30
|
+
*/
|
|
18
31
|
baseURL?: string;
|
|
32
|
+
/**
|
|
33
|
+
* OpenAI model to use
|
|
34
|
+
*
|
|
35
|
+
* Defaults to 'gpt-4o-mini'
|
|
36
|
+
*/
|
|
19
37
|
model?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Additional model options to control behavior
|
|
40
|
+
*/
|
|
20
41
|
modelOptions?: ChatModelOptions;
|
|
21
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* @hidden
|
|
45
|
+
*/
|
|
22
46
|
export declare const openAIChatModelOptionsSchema: z.ZodObject<{
|
|
23
47
|
apiKey: z.ZodOptional<z.ZodString>;
|
|
24
48
|
baseURL: z.ZodOptional<z.ZodString>;
|
|
@@ -70,9 +94,31 @@ export declare const openAIChatModelOptionsSchema: z.ZodObject<{
|
|
|
70
94
|
apiKey?: string | undefined;
|
|
71
95
|
baseURL?: string | undefined;
|
|
72
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
|
+
*/
|
|
73
116
|
export declare class OpenAIChatModel extends ChatModel {
|
|
74
117
|
options?: OpenAIChatModelOptions | undefined;
|
|
75
118
|
constructor(options?: OpenAIChatModelOptions | undefined);
|
|
119
|
+
/**
|
|
120
|
+
* @hidden
|
|
121
|
+
*/
|
|
76
122
|
protected _client?: OpenAI;
|
|
77
123
|
protected apiKeyEnvName: string;
|
|
78
124
|
protected apiKeyDefault: string | undefined;
|
|
@@ -85,18 +131,36 @@ export declare class OpenAIChatModel extends ChatModel {
|
|
|
85
131
|
protected supportsTemperature: boolean;
|
|
86
132
|
get client(): OpenAI;
|
|
87
133
|
get modelOptions(): ChatModelOptions | undefined;
|
|
88
|
-
|
|
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;
|
|
89
141
|
private getParallelToolCalls;
|
|
90
142
|
private getRunMessages;
|
|
91
143
|
private getRunResponseFormat;
|
|
92
144
|
private requestStructuredOutput;
|
|
93
145
|
private extractResultFromStream;
|
|
94
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* @hidden
|
|
149
|
+
*/
|
|
95
150
|
export declare const ROLE_MAP: {
|
|
96
151
|
[key in Role]: ChatCompletionMessageParam["role"];
|
|
97
152
|
};
|
|
153
|
+
/**
|
|
154
|
+
* @hidden
|
|
155
|
+
*/
|
|
98
156
|
export declare function contentsFromInputMessages(messages: ChatModelInputMessage[]): Promise<ChatCompletionMessageParam[]>;
|
|
157
|
+
/**
|
|
158
|
+
* @hidden
|
|
159
|
+
*/
|
|
99
160
|
export declare function toolsFromInputTools(tools?: ChatModelInputTool[], options?: {
|
|
100
161
|
addTypeToEmptyParameters?: boolean;
|
|
101
162
|
}): ChatCompletionTool[] | undefined;
|
|
163
|
+
/**
|
|
164
|
+
* @hidden
|
|
165
|
+
*/
|
|
102
166
|
export declare function jsonSchemaToOpenAIJsonSchema(schema: Record<string, unknown>): Record<string, unknown>;
|
|
@@ -12,6 +12,9 @@ const OPENAI_CHAT_MODEL_CAPABILITIES = {
|
|
|
12
12
|
"o4-mini": { supportsParallelToolCalls: false, supportsTemperature: false },
|
|
13
13
|
"o3-mini": { supportsParallelToolCalls: false, supportsTemperature: false },
|
|
14
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* @hidden
|
|
17
|
+
*/
|
|
15
18
|
export const openAIChatModelOptionsSchema = z.object({
|
|
16
19
|
apiKey: z.string().optional(),
|
|
17
20
|
baseURL: z.string().optional(),
|
|
@@ -27,6 +30,25 @@ export const openAIChatModelOptionsSchema = z.object({
|
|
|
27
30
|
})
|
|
28
31
|
.optional(),
|
|
29
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
|
+
*/
|
|
30
52
|
export class OpenAIChatModel extends ChatModel {
|
|
31
53
|
options;
|
|
32
54
|
constructor(options) {
|
|
@@ -37,6 +59,9 @@ export class OpenAIChatModel extends ChatModel {
|
|
|
37
59
|
const preset = options?.model ? OPENAI_CHAT_MODEL_CAPABILITIES[options.model] : undefined;
|
|
38
60
|
Object.assign(this, preset);
|
|
39
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* @hidden
|
|
64
|
+
*/
|
|
40
65
|
_client;
|
|
41
66
|
apiKeyEnvName = "OPENAI_API_KEY";
|
|
42
67
|
apiKeyDefault;
|
|
@@ -60,7 +85,15 @@ export class OpenAIChatModel extends ChatModel {
|
|
|
60
85
|
get modelOptions() {
|
|
61
86
|
return this.options?.modelOptions;
|
|
62
87
|
}
|
|
63
|
-
|
|
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) {
|
|
64
97
|
const messages = await this.getRunMessages(input);
|
|
65
98
|
const body = {
|
|
66
99
|
model: this.options?.model || CHAT_MODEL_OPENAI_DEFAULT_MODEL,
|
|
@@ -86,7 +119,7 @@ export class OpenAIChatModel extends ChatModel {
|
|
|
86
119
|
parallel_tool_calls: this.getParallelToolCalls(input),
|
|
87
120
|
response_format: responseFormat,
|
|
88
121
|
});
|
|
89
|
-
if (
|
|
122
|
+
if (input.responseFormat?.type !== "json_schema") {
|
|
90
123
|
return await this.extractResultFromStream(stream, false, true);
|
|
91
124
|
}
|
|
92
125
|
const result = await this.extractResultFromStream(stream, jsonMode);
|
|
@@ -254,12 +287,18 @@ export class OpenAIChatModel extends ChatModel {
|
|
|
254
287
|
return streaming ? result : await agentResponseStreamToObject(result);
|
|
255
288
|
}
|
|
256
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* @hidden
|
|
292
|
+
*/
|
|
257
293
|
export const ROLE_MAP = {
|
|
258
294
|
system: "system",
|
|
259
295
|
user: "user",
|
|
260
296
|
agent: "assistant",
|
|
261
297
|
tool: "tool",
|
|
262
298
|
};
|
|
299
|
+
/**
|
|
300
|
+
* @hidden
|
|
301
|
+
*/
|
|
263
302
|
export async function contentsFromInputMessages(messages) {
|
|
264
303
|
return messages.map((i) => ({
|
|
265
304
|
role: ROLE_MAP[i.role],
|
|
@@ -289,6 +328,9 @@ export async function contentsFromInputMessages(messages) {
|
|
|
289
328
|
name: i.name,
|
|
290
329
|
}));
|
|
291
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* @hidden
|
|
333
|
+
*/
|
|
292
334
|
export function toolsFromInputTools(tools, options) {
|
|
293
335
|
return tools?.length
|
|
294
336
|
? tools.map((i) => {
|
|
@@ -307,6 +349,9 @@ export function toolsFromInputTools(tools, options) {
|
|
|
307
349
|
})
|
|
308
350
|
: undefined;
|
|
309
351
|
}
|
|
352
|
+
/**
|
|
353
|
+
* @hidden
|
|
354
|
+
*/
|
|
310
355
|
export function jsonSchemaToOpenAIJsonSchema(schema) {
|
|
311
356
|
if (schema?.type === "object") {
|
|
312
357
|
const { required, properties } = schema;
|
|
@@ -1,4 +1,20 @@
|
|
|
1
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
|
+
*/
|
|
2
18
|
export declare class XAIChatModel extends OpenAIChatModel {
|
|
3
19
|
constructor(options?: OpenAIChatModelOptions);
|
|
4
20
|
protected apiKeyEnvName: string;
|
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import { OpenAIChatModel } from "./openai-chat-model.js";
|
|
2
2
|
const XAI_DEFAULT_CHAT_MODEL = "grok-2-latest";
|
|
3
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
|
+
*/
|
|
4
20
|
export class XAIChatModel extends OpenAIChatModel {
|
|
5
21
|
constructor(options) {
|
|
6
22
|
super({
|
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
import type { GetPromptResult } from "@modelcontextprotocol/sdk/types.js";
|
|
2
2
|
import { Agent, type Message } from "../agents/agent.js";
|
|
3
3
|
import type { AIAgent } from "../agents/ai-agent.js";
|
|
4
|
-
import type { AgentMemory } from "../agents/memory.js";
|
|
5
4
|
import type { Context } from "../aigne/context.js";
|
|
5
|
+
import type { MemoryAgent } from "../memory/memory.js";
|
|
6
6
|
import type { ChatModel, ChatModelInput } from "../models/chat-model.js";
|
|
7
7
|
import { ChatMessagesTemplate } from "./template.js";
|
|
8
8
|
export declare const MESSAGE_KEY = "$message";
|
|
9
|
-
export declare const DEFAULT_MAX_HISTORY_MESSAGES = 10;
|
|
10
9
|
export declare function createMessage<I extends Message>(message: string | I): I;
|
|
11
10
|
export declare function getMessage(input: Message): string | undefined;
|
|
12
11
|
export interface PromptBuilderOptions {
|
|
13
12
|
instructions?: string | ChatMessagesTemplate;
|
|
14
13
|
}
|
|
15
14
|
export interface PromptBuilderBuildOptions {
|
|
16
|
-
memory?:
|
|
17
|
-
context
|
|
15
|
+
memory?: MemoryAgent | MemoryAgent[];
|
|
16
|
+
context: Context;
|
|
18
17
|
agent?: AIAgent;
|
|
19
18
|
input?: Message;
|
|
20
19
|
model?: ChatModel;
|
|
@@ -37,6 +36,7 @@ export declare class PromptBuilder {
|
|
|
37
36
|
toolAgents?: Agent[];
|
|
38
37
|
}>;
|
|
39
38
|
private buildMessages;
|
|
39
|
+
private convertMemoriesToMessages;
|
|
40
40
|
private buildResponseFormat;
|
|
41
41
|
private buildTools;
|
|
42
42
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { stringify } from "yaml";
|
|
2
3
|
import { ZodObject } from "zod";
|
|
3
4
|
import { Agent } from "../agents/agent.js";
|
|
4
5
|
import { outputSchemaToResponseFormatSchema } from "../utils/json-schema.js";
|
|
5
|
-
import { isNil, unique } from "../utils/type-utils.js";
|
|
6
|
-
import {
|
|
6
|
+
import { isNil, orArrayToArray, unique } from "../utils/type-utils.js";
|
|
7
|
+
import { MEMORY_MESSAGE_TEMPLATE } from "./prompts/memory-message-template.js";
|
|
8
|
+
import { AgentMessageTemplate, ChatMessagesTemplate, PromptTemplate, SystemMessageTemplate, UserMessageTemplate, } from "./template.js";
|
|
7
9
|
export const MESSAGE_KEY = "$message";
|
|
8
|
-
export const DEFAULT_MAX_HISTORY_MESSAGES = 10;
|
|
9
10
|
export function createMessage(message) {
|
|
10
11
|
return typeof message === "string"
|
|
11
12
|
? { [MESSAGE_KEY]: message }
|
|
@@ -67,26 +68,20 @@ export class PromptBuilder {
|
|
|
67
68
|
instructions;
|
|
68
69
|
async build(options) {
|
|
69
70
|
return {
|
|
70
|
-
messages: this.buildMessages(options),
|
|
71
|
+
messages: await this.buildMessages(options),
|
|
71
72
|
responseFormat: this.buildResponseFormat(options),
|
|
72
73
|
...this.buildTools(options),
|
|
73
74
|
};
|
|
74
75
|
}
|
|
75
|
-
buildMessages(options) {
|
|
76
|
+
async buildMessages(options) {
|
|
76
77
|
const { input } = options;
|
|
77
78
|
const messages = (typeof this.instructions === "string"
|
|
78
79
|
? ChatMessagesTemplate.from([SystemMessageTemplate.from(this.instructions)])
|
|
79
80
|
: this.instructions)?.format(options.input) ?? [];
|
|
80
|
-
const memory
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (histories?.length)
|
|
85
|
-
messages.push(...histories.map((i) => ({
|
|
86
|
-
role: i.role,
|
|
87
|
-
content: convertMessageToContent(i.content),
|
|
88
|
-
name: i.source,
|
|
89
|
-
})));
|
|
81
|
+
for (const memory of orArrayToArray(options.memory ?? options.agent?.memories)) {
|
|
82
|
+
const memories = (await memory.retrieve({ search: input && getMessage(input) }, options.context))?.memories;
|
|
83
|
+
if (memories?.length)
|
|
84
|
+
messages.push(...this.convertMemoriesToMessages(memories, options));
|
|
90
85
|
}
|
|
91
86
|
const content = input && getMessage(input);
|
|
92
87
|
// add user input if it's not the same as the last message
|
|
@@ -95,6 +90,15 @@ export class PromptBuilder {
|
|
|
95
90
|
}
|
|
96
91
|
return messages;
|
|
97
92
|
}
|
|
93
|
+
convertMemoriesToMessages(memories, options) {
|
|
94
|
+
const str = stringify(memories.map((i) => i.content));
|
|
95
|
+
return [
|
|
96
|
+
{
|
|
97
|
+
role: "system",
|
|
98
|
+
content: PromptTemplate.from(options.agent?.memoryPromptTemplate || MEMORY_MESSAGE_TEMPLATE).format({ memories: str }),
|
|
99
|
+
},
|
|
100
|
+
];
|
|
101
|
+
}
|
|
98
102
|
buildResponseFormat(options) {
|
|
99
103
|
const outputSchema = options.outputSchema || options.agent?.outputSchema;
|
|
100
104
|
if (!outputSchema)
|
|
@@ -114,6 +118,7 @@ export class PromptBuilder {
|
|
|
114
118
|
buildTools(options) {
|
|
115
119
|
const toolAgents = unique((options.context?.skills ?? [])
|
|
116
120
|
.concat(options.agent?.skills ?? [])
|
|
121
|
+
.concat(options.agent?.memoryAgentsAsTools ? options.agent.memories : [])
|
|
117
122
|
// TODO: support nested tools?
|
|
118
123
|
.flatMap((i) => (i.isInvokable ? i.skills.concat(i) : i.skills)), (i) => i.name);
|
|
119
124
|
const tools = toolAgents.map((i) => ({
|
|
@@ -169,7 +174,3 @@ function isFromPath(value) {
|
|
|
169
174
|
function isEmptyObjectType(schema) {
|
|
170
175
|
return schema instanceof ZodObject && Object.keys(schema.shape).length === 0;
|
|
171
176
|
}
|
|
172
|
-
function convertMessageToContent(i) {
|
|
173
|
-
const str = i[MESSAGE_KEY];
|
|
174
|
-
return !isNil(str) ? (typeof str === "string" ? str : JSON.stringify(str)) : JSON.stringify(i);
|
|
175
|
-
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const MEMORY_MESSAGE_TEMPLATE = "Here are some useful memories to consider:\n\n<memories>\n{{memories}}\n</memories>\n";
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { inspect } from "node:util";
|
|
1
2
|
import Mustache from "mustache";
|
|
2
3
|
import { z } from "zod";
|
|
4
|
+
import { tryOrThrow } from "../utils/type-utils.js";
|
|
3
5
|
export class PromptTemplate {
|
|
4
6
|
template;
|
|
5
7
|
static from(template) {
|
|
@@ -76,7 +78,9 @@ export class ToolMessageTemplate extends ChatMessageTemplate {
|
|
|
76
78
|
return new ToolMessageTemplate(content, toolCallId, name);
|
|
77
79
|
}
|
|
78
80
|
constructor(content, toolCallId, name) {
|
|
79
|
-
super("tool", typeof content === "string"
|
|
81
|
+
super("tool", typeof content === "string"
|
|
82
|
+
? content
|
|
83
|
+
: tryOrThrow(() => JSON.stringify(content), `Failed to stringify tool content. toolCallId: ${toolCallId}, content: ${inspect(content)}`), name);
|
|
80
84
|
this.toolCallId = toolCallId;
|
|
81
85
|
}
|
|
82
86
|
format(variables) {
|
|
@@ -1,4 +1,15 @@
|
|
|
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
|
+
*/
|
|
1
6
|
export declare class ServerError extends Error {
|
|
2
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
|
+
*/
|
|
3
14
|
constructor(status: number, message: string);
|
|
4
15
|
}
|
package/lib/esm/server/error.js
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
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
|
+
*/
|
|
1
6
|
export class ServerError extends Error {
|
|
2
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
|
+
*/
|
|
3
14
|
constructor(status, message) {
|
|
4
15
|
super(message);
|
|
5
16
|
this.status = status;
|