@aigne/core 1.1.0-beta.5 → 1.3.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 +81 -0
- package/lib/cjs/agents/agent.d.ts +37 -33
- package/lib/cjs/agents/agent.js +68 -28
- package/lib/cjs/agents/ai-agent.d.ts +8 -12
- package/lib/cjs/agents/ai-agent.js +17 -35
- package/lib/cjs/agents/mcp-agent.d.ts +25 -9
- package/lib/cjs/agents/mcp-agent.js +61 -14
- package/lib/cjs/agents/memory.d.ts +26 -0
- package/lib/cjs/agents/memory.js +38 -0
- package/lib/cjs/agents/types.d.ts +4 -3
- package/lib/cjs/agents/types.js +11 -1
- package/lib/cjs/agents/user-agent.d.ts +24 -0
- package/lib/cjs/agents/user-agent.js +62 -0
- package/lib/cjs/execution-engine/context.d.ts +35 -5
- package/lib/cjs/execution-engine/execution-engine.d.ts +64 -0
- package/lib/cjs/execution-engine/execution-engine.js +136 -0
- package/lib/cjs/execution-engine/index.d.ts +4 -46
- package/lib/cjs/execution-engine/index.js +17 -193
- package/lib/cjs/execution-engine/message-queue.d.ts +17 -2
- package/lib/cjs/execution-engine/message-queue.js +37 -1
- package/lib/cjs/execution-engine/utils.d.ts +4 -0
- package/lib/cjs/execution-engine/utils.js +32 -0
- package/lib/cjs/index.d.ts +14 -11
- package/lib/cjs/index.js +14 -11
- package/lib/{dts/models/chat.d.ts → cjs/models/chat-model.d.ts} +15 -12
- package/lib/cjs/models/{chat.js → chat-model.js} +5 -5
- package/lib/cjs/models/claude-chat-model.d.ts +17 -0
- package/lib/cjs/models/claude-chat-model.js +199 -0
- package/lib/cjs/models/{chat-openai.d.ts → openai-chat-model.d.ts} +2 -2
- package/lib/cjs/models/{chat-openai.js → openai-chat-model.js} +8 -8
- package/lib/cjs/prompt/prompt-builder.d.ts +14 -14
- package/lib/cjs/prompt/prompt-builder.js +62 -67
- package/lib/cjs/prompt/template.d.ts +9 -21
- package/lib/cjs/prompt/template.js +3 -5
- package/lib/cjs/utils/json-schema.d.ts +3 -0
- package/lib/cjs/utils/json-schema.js +23 -0
- package/lib/cjs/utils/mcp-utils.d.ts +3 -2
- package/lib/cjs/utils/mcp-utils.js +29 -17
- package/lib/cjs/utils/run-chat-loop.d.ts +5 -4
- package/lib/cjs/utils/run-chat-loop.js +25 -4
- package/lib/cjs/utils/type-utils.d.ts +3 -0
- package/lib/cjs/utils/type-utils.js +8 -8
- package/lib/dts/agents/agent.d.ts +37 -33
- package/lib/dts/agents/ai-agent.d.ts +8 -12
- package/lib/dts/agents/mcp-agent.d.ts +25 -9
- package/lib/dts/agents/memory.d.ts +26 -0
- package/lib/dts/agents/types.d.ts +4 -3
- package/lib/dts/agents/user-agent.d.ts +24 -0
- package/lib/dts/execution-engine/context.d.ts +35 -5
- package/lib/dts/execution-engine/execution-engine.d.ts +64 -0
- package/lib/dts/execution-engine/index.d.ts +4 -46
- package/lib/dts/execution-engine/message-queue.d.ts +17 -2
- package/lib/dts/execution-engine/utils.d.ts +4 -0
- package/lib/dts/index.d.ts +14 -11
- package/lib/{esm/models/chat.d.ts → dts/models/chat-model.d.ts} +15 -12
- package/lib/dts/models/claude-chat-model.d.ts +17 -0
- package/lib/dts/models/{chat-openai.d.ts → openai-chat-model.d.ts} +2 -2
- package/lib/dts/prompt/prompt-builder.d.ts +14 -14
- package/lib/dts/prompt/template.d.ts +9 -21
- package/lib/dts/utils/json-schema.d.ts +3 -0
- package/lib/dts/utils/mcp-utils.d.ts +3 -2
- package/lib/dts/utils/run-chat-loop.d.ts +5 -4
- package/lib/dts/utils/type-utils.d.ts +3 -0
- package/lib/esm/agents/agent.d.ts +37 -33
- package/lib/esm/agents/agent.js +67 -24
- package/lib/esm/agents/ai-agent.d.ts +8 -12
- package/lib/esm/agents/ai-agent.js +13 -31
- package/lib/esm/agents/mcp-agent.d.ts +25 -9
- package/lib/esm/agents/mcp-agent.js +54 -8
- package/lib/esm/agents/memory.d.ts +26 -0
- package/lib/esm/agents/memory.js +34 -0
- package/lib/esm/agents/types.d.ts +4 -3
- package/lib/esm/agents/types.js +10 -1
- package/lib/esm/agents/user-agent.d.ts +24 -0
- package/lib/esm/agents/user-agent.js +58 -0
- package/lib/esm/execution-engine/context.d.ts +35 -5
- package/lib/esm/execution-engine/execution-engine.d.ts +64 -0
- package/lib/esm/execution-engine/execution-engine.js +129 -0
- package/lib/esm/execution-engine/index.d.ts +4 -46
- package/lib/esm/execution-engine/index.js +4 -188
- package/lib/esm/execution-engine/message-queue.d.ts +17 -2
- package/lib/esm/execution-engine/message-queue.js +37 -1
- package/lib/esm/execution-engine/utils.d.ts +4 -0
- package/lib/esm/execution-engine/utils.js +28 -0
- package/lib/esm/index.d.ts +14 -11
- package/lib/esm/index.js +14 -11
- package/lib/{cjs/models/chat.d.ts → esm/models/chat-model.d.ts} +15 -12
- package/lib/esm/models/{chat.js → chat-model.js} +4 -4
- package/lib/esm/models/claude-chat-model.d.ts +17 -0
- package/lib/esm/models/claude-chat-model.js +192 -0
- package/lib/esm/models/{chat-openai.d.ts → openai-chat-model.d.ts} +2 -2
- package/lib/esm/models/{chat-openai.js → openai-chat-model.js} +5 -5
- package/lib/esm/prompt/prompt-builder.d.ts +14 -14
- package/lib/esm/prompt/prompt-builder.js +56 -58
- package/lib/esm/prompt/template.d.ts +9 -21
- package/lib/esm/prompt/template.js +3 -5
- package/lib/esm/utils/json-schema.d.ts +3 -0
- package/lib/esm/utils/json-schema.js +20 -0
- package/lib/esm/utils/mcp-utils.d.ts +3 -2
- package/lib/esm/utils/mcp-utils.js +26 -15
- package/lib/esm/utils/run-chat-loop.d.ts +5 -4
- package/lib/esm/utils/run-chat-loop.js +25 -4
- package/lib/esm/utils/type-utils.d.ts +3 -0
- package/lib/esm/utils/type-utils.js +4 -2
- package/package.json +31 -30
|
@@ -0,0 +1,199 @@
|
|
|
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.ClaudeChatModel = void 0;
|
|
7
|
+
const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
|
|
8
|
+
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
9
|
+
const chat_model_js_1 = require("./chat-model.js");
|
|
10
|
+
const CHAT_MODEL_CLAUDE_DEFAULT_MODEL = "claude-3-7-sonnet-latest";
|
|
11
|
+
class ClaudeChatModel extends chat_model_js_1.ChatModel {
|
|
12
|
+
config;
|
|
13
|
+
constructor(config) {
|
|
14
|
+
super();
|
|
15
|
+
this.config = config;
|
|
16
|
+
}
|
|
17
|
+
_client;
|
|
18
|
+
get client() {
|
|
19
|
+
if (!this.config?.apiKey)
|
|
20
|
+
throw new Error("Api Key is required for ClaudeChatModel");
|
|
21
|
+
this._client ??= new sdk_1.default({ apiKey: this.config.apiKey });
|
|
22
|
+
return this._client;
|
|
23
|
+
}
|
|
24
|
+
async process(input) {
|
|
25
|
+
const model = this.config?.model || CHAT_MODEL_CLAUDE_DEFAULT_MODEL;
|
|
26
|
+
const body = {
|
|
27
|
+
model,
|
|
28
|
+
temperature: input.modelOptions?.temperature,
|
|
29
|
+
top_p: input.modelOptions?.topP,
|
|
30
|
+
// TODO: make dynamic based on model https://docs.anthropic.com/en/docs/about-claude/models/all-models
|
|
31
|
+
max_tokens: /claude-3-[5|7]/.test(model) ? 8192 : 4096,
|
|
32
|
+
...convertMessages(input),
|
|
33
|
+
...convertTools(input),
|
|
34
|
+
};
|
|
35
|
+
const stream = this.client.messages.stream({
|
|
36
|
+
...body,
|
|
37
|
+
stream: true,
|
|
38
|
+
});
|
|
39
|
+
const result = await this.extractResultFromClaudeStream(stream);
|
|
40
|
+
// Claude doesn't support json_schema response and tool calls in the same request,
|
|
41
|
+
// so we need to make a separate request for json_schema response when the tool calls is empty
|
|
42
|
+
if (!result.toolCalls?.length && input.responseFormat?.type === "json_schema") {
|
|
43
|
+
return this.requestStructuredOutput(body, input.responseFormat);
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
async extractResultFromClaudeStream(stream) {
|
|
48
|
+
let text = "";
|
|
49
|
+
const toolCalls = [];
|
|
50
|
+
for await (const chunk of stream) {
|
|
51
|
+
// handle streaming text
|
|
52
|
+
if (chunk.type === "content_block_delta" && chunk.delta.type === "text_delta") {
|
|
53
|
+
text += chunk.delta.text;
|
|
54
|
+
}
|
|
55
|
+
if (chunk.type === "content_block_start" && chunk.content_block.type === "tool_use") {
|
|
56
|
+
toolCalls[chunk.index] = {
|
|
57
|
+
type: "function",
|
|
58
|
+
id: chunk.content_block.id,
|
|
59
|
+
function: {
|
|
60
|
+
name: chunk.content_block.name,
|
|
61
|
+
arguments: {},
|
|
62
|
+
},
|
|
63
|
+
args: "",
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (chunk.type === "content_block_delta" && chunk.delta.type === "input_json_delta") {
|
|
67
|
+
const call = toolCalls[chunk.index];
|
|
68
|
+
if (!call)
|
|
69
|
+
throw new Error("Tool call not found");
|
|
70
|
+
call.args += chunk.delta.partial_json;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const result = { text };
|
|
74
|
+
if (toolCalls.length) {
|
|
75
|
+
result.toolCalls = toolCalls
|
|
76
|
+
.map(({ args, ...c }) => ({
|
|
77
|
+
...c,
|
|
78
|
+
function: { ...c.function, arguments: JSON.parse(args) },
|
|
79
|
+
}))
|
|
80
|
+
.filter(type_utils_js_1.isNonNullable);
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
async requestStructuredOutput(body, responseFormat) {
|
|
85
|
+
if (responseFormat?.type !== "json_schema") {
|
|
86
|
+
throw new Error("Expected json_schema response format");
|
|
87
|
+
}
|
|
88
|
+
const result = await this.client.messages.create({
|
|
89
|
+
...body,
|
|
90
|
+
tools: [
|
|
91
|
+
{
|
|
92
|
+
name: "generate_json",
|
|
93
|
+
description: "Generate a json result by given context",
|
|
94
|
+
input_schema: responseFormat.jsonSchema.schema,
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
tool_choice: {
|
|
98
|
+
type: "tool",
|
|
99
|
+
name: "generate_json",
|
|
100
|
+
disable_parallel_tool_use: true,
|
|
101
|
+
},
|
|
102
|
+
stream: false,
|
|
103
|
+
});
|
|
104
|
+
const jsonTool = result.content.find((i) => i.type === "tool_use" && i.name === "generate_json");
|
|
105
|
+
if (!jsonTool)
|
|
106
|
+
throw new Error("Json tool not found");
|
|
107
|
+
return {
|
|
108
|
+
json: jsonTool.input,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.ClaudeChatModel = ClaudeChatModel;
|
|
113
|
+
function convertMessages({ messages, responseFormat }) {
|
|
114
|
+
const systemMessages = [];
|
|
115
|
+
const msgs = [];
|
|
116
|
+
for (const msg of messages) {
|
|
117
|
+
if (msg.role === "system") {
|
|
118
|
+
if (typeof msg.content !== "string")
|
|
119
|
+
throw new Error("System message must have content");
|
|
120
|
+
systemMessages.push(msg.content);
|
|
121
|
+
}
|
|
122
|
+
else if (msg.role === "tool") {
|
|
123
|
+
if (!msg.toolCallId)
|
|
124
|
+
throw new Error("Tool message must have toolCallId");
|
|
125
|
+
if (typeof msg.content !== "string")
|
|
126
|
+
throw new Error("Tool message must have string content");
|
|
127
|
+
msgs.push({
|
|
128
|
+
role: "user",
|
|
129
|
+
content: [{ type: "tool_result", tool_use_id: msg.toolCallId, content: msg.content }],
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
else if (msg.role === "user") {
|
|
133
|
+
if (!msg.content)
|
|
134
|
+
throw new Error("User message must have content");
|
|
135
|
+
msgs.push({ role: "user", content: contentBlockParamFromContent(msg.content) });
|
|
136
|
+
}
|
|
137
|
+
else if (msg.role === "agent") {
|
|
138
|
+
if (msg.toolCalls?.length) {
|
|
139
|
+
msgs.push({
|
|
140
|
+
role: "assistant",
|
|
141
|
+
content: msg.toolCalls.map((i) => ({
|
|
142
|
+
type: "tool_use",
|
|
143
|
+
id: i.id,
|
|
144
|
+
name: i.function.name,
|
|
145
|
+
input: i.function.arguments,
|
|
146
|
+
})),
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
else if (msg.content) {
|
|
150
|
+
msgs.push({ role: "assistant", content: contentBlockParamFromContent(msg.content) });
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
throw new Error("Agent message must have content or toolCalls");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (responseFormat?.type === "json_schema") {
|
|
158
|
+
systemMessages.push(`You should provide a json response with schema: ${JSON.stringify(responseFormat.jsonSchema.schema)}`);
|
|
159
|
+
}
|
|
160
|
+
return { messages: msgs, system: systemMessages.join("\n").trim() || undefined };
|
|
161
|
+
}
|
|
162
|
+
function contentBlockParamFromContent(content) {
|
|
163
|
+
if (typeof content === "string")
|
|
164
|
+
return content;
|
|
165
|
+
if (Array.isArray(content)) {
|
|
166
|
+
return content.map((item) => item.type === "image_url"
|
|
167
|
+
? { type: "image", source: { type: "url", url: item.url } }
|
|
168
|
+
: { type: "text", text: item.text });
|
|
169
|
+
}
|
|
170
|
+
throw new Error("Invalid chat message content");
|
|
171
|
+
}
|
|
172
|
+
function convertTools({ tools, toolChoice, }) {
|
|
173
|
+
let choice;
|
|
174
|
+
if (typeof toolChoice === "object" && "type" in toolChoice && toolChoice.type === "function") {
|
|
175
|
+
choice = {
|
|
176
|
+
type: "tool",
|
|
177
|
+
name: toolChoice.function.name,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
else if (toolChoice === "required") {
|
|
181
|
+
choice = { type: "any" };
|
|
182
|
+
}
|
|
183
|
+
else if (toolChoice === "auto") {
|
|
184
|
+
choice = { type: "auto" };
|
|
185
|
+
}
|
|
186
|
+
else if (toolChoice === "none") {
|
|
187
|
+
choice = { type: "none" };
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
tools: tools?.length
|
|
191
|
+
? tools.map((i) => ({
|
|
192
|
+
name: i.function.name,
|
|
193
|
+
description: i.function.description,
|
|
194
|
+
input_schema: i.function.parameters,
|
|
195
|
+
}))
|
|
196
|
+
: undefined,
|
|
197
|
+
tool_choice: choice,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ChatModel, type ChatModelInput, type ChatModelOutput } from "./chat";
|
|
2
|
-
export declare class
|
|
1
|
+
import { ChatModel, type ChatModelInput, type ChatModelOutput } from "./chat-model.js";
|
|
2
|
+
export declare class OpenAIChatModel extends ChatModel {
|
|
3
3
|
config?: {
|
|
4
4
|
apiKey?: string;
|
|
5
5
|
model?: string;
|
|
@@ -3,13 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.OpenAIChatModel = void 0;
|
|
7
7
|
const nanoid_1 = require("nanoid");
|
|
8
8
|
const openai_1 = __importDefault(require("openai"));
|
|
9
|
-
const
|
|
10
|
-
const
|
|
9
|
+
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
10
|
+
const chat_model_js_1 = require("./chat-model.js");
|
|
11
11
|
const CHAT_MODEL_OPENAI_DEFAULT_MODEL = "gpt-4o-mini";
|
|
12
|
-
class
|
|
12
|
+
class OpenAIChatModel extends chat_model_js_1.ChatModel {
|
|
13
13
|
config;
|
|
14
14
|
constructor(config) {
|
|
15
15
|
super();
|
|
@@ -18,7 +18,7 @@ class ChatModelOpenAI extends chat_1.ChatModel {
|
|
|
18
18
|
_client;
|
|
19
19
|
get client() {
|
|
20
20
|
if (!this.config?.apiKey)
|
|
21
|
-
throw new Error("Api Key is required for
|
|
21
|
+
throw new Error("Api Key is required for OpenAIChatModel");
|
|
22
22
|
this._client ??= new openai_1.default({ apiKey: this.config.apiKey });
|
|
23
23
|
return this._client;
|
|
24
24
|
}
|
|
@@ -68,7 +68,7 @@ class ChatModelOpenAI extends chat_1.ChatModel {
|
|
|
68
68
|
text += choice.delta.content;
|
|
69
69
|
}
|
|
70
70
|
const result = {};
|
|
71
|
-
if (input.responseFormat?.type === "json_schema") {
|
|
71
|
+
if (input.responseFormat?.type === "json_schema" && text) {
|
|
72
72
|
result.json = JSON.parse(text);
|
|
73
73
|
}
|
|
74
74
|
else {
|
|
@@ -83,7 +83,7 @@ class ChatModelOpenAI extends chat_1.ChatModel {
|
|
|
83
83
|
return result;
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
-
exports.
|
|
86
|
+
exports.OpenAIChatModel = OpenAIChatModel;
|
|
87
87
|
const ROLE_MAP = {
|
|
88
88
|
system: "system",
|
|
89
89
|
user: "user",
|
|
@@ -107,7 +107,7 @@ async function contentsFromInputMessages(messages) {
|
|
|
107
107
|
};
|
|
108
108
|
}
|
|
109
109
|
})
|
|
110
|
-
.filter(
|
|
110
|
+
.filter(type_utils_js_1.isNonNullable),
|
|
111
111
|
tool_calls: i.toolCalls?.map((i) => ({
|
|
112
112
|
...i,
|
|
113
113
|
function: {
|
|
@@ -1,22 +1,24 @@
|
|
|
1
|
-
import type { GetPromptResult } from "@modelcontextprotocol/sdk/types";
|
|
2
|
-
import { Agent, type
|
|
3
|
-
import type { AIAgent } from "../agents/ai-agent";
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
export declare
|
|
9
|
-
export declare
|
|
1
|
+
import type { GetPromptResult } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
import { Agent, type AgentOptions, type Message } from "../agents/agent.js";
|
|
3
|
+
import type { AIAgent } from "../agents/ai-agent.js";
|
|
4
|
+
import type { AgentMemory } from "../agents/memory.js";
|
|
5
|
+
import type { Context } from "../execution-engine/context.js";
|
|
6
|
+
import type { ChatModel, ChatModelInput } from "../models/chat-model.js";
|
|
7
|
+
import { ChatMessagesTemplate } from "./template.js";
|
|
8
|
+
export declare const MESSAGE_KEY = "$message";
|
|
9
|
+
export declare const DEFAULT_MAX_HISTORY_MESSAGES = 10;
|
|
10
|
+
export declare function createMessage(message: string | object): Message;
|
|
11
|
+
export declare function getMessage(input: Message): string | undefined;
|
|
10
12
|
export interface PromptBuilderOptions {
|
|
11
13
|
instructions?: string | ChatMessagesTemplate;
|
|
12
14
|
}
|
|
13
15
|
export interface PromptBuilderBuildOptions {
|
|
14
|
-
|
|
15
|
-
maxHistoryMessages?: number;
|
|
16
|
+
memory?: AgentMemory;
|
|
16
17
|
context?: Context;
|
|
17
18
|
agent?: AIAgent;
|
|
18
|
-
input?:
|
|
19
|
+
input?: Message;
|
|
19
20
|
model?: ChatModel;
|
|
21
|
+
outputSchema?: AgentOptions["outputSchema"];
|
|
20
22
|
}
|
|
21
23
|
export declare class PromptBuilder {
|
|
22
24
|
static from(instructions: string): PromptBuilder;
|
|
@@ -31,8 +33,6 @@ export declare class PromptBuilder {
|
|
|
31
33
|
private static fromMCPPromptResult;
|
|
32
34
|
constructor(options?: PromptBuilderOptions);
|
|
33
35
|
instructions?: string | ChatMessagesTemplate;
|
|
34
|
-
histories: ChatModelInputMessage[];
|
|
35
|
-
addHistory(...messages: ChatModelInputMessage[]): void;
|
|
36
36
|
build(options: PromptBuilderBuildOptions): Promise<ChatModelInput & {
|
|
37
37
|
toolAgents?: Agent[];
|
|
38
38
|
}>;
|
|
@@ -1,39 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.PromptBuilder = exports.
|
|
7
|
-
exports.
|
|
8
|
-
exports.
|
|
3
|
+
exports.PromptBuilder = exports.DEFAULT_MAX_HISTORY_MESSAGES = exports.MESSAGE_KEY = void 0;
|
|
4
|
+
exports.createMessage = createMessage;
|
|
5
|
+
exports.getMessage = getMessage;
|
|
9
6
|
const promises_1 = require("node:fs/promises");
|
|
10
|
-
const
|
|
7
|
+
const lodash_es_1 = require("lodash-es");
|
|
11
8
|
const zod_1 = require("zod");
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
exports.
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
const agent_js_1 = require("../agents/agent.js");
|
|
10
|
+
const json_schema_js_1 = require("../utils/json-schema.js");
|
|
11
|
+
const template_js_1 = require("./template.js");
|
|
12
|
+
exports.MESSAGE_KEY = "$message";
|
|
13
|
+
exports.DEFAULT_MAX_HISTORY_MESSAGES = 10;
|
|
14
|
+
function createMessage(message) {
|
|
15
|
+
return { [exports.MESSAGE_KEY]: message };
|
|
18
16
|
}
|
|
19
|
-
function
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const messages = (0, template_1.parseChatMessages)(originalUserInputMessages);
|
|
27
|
-
if (messages)
|
|
28
|
-
newMessages.push(...messages);
|
|
29
|
-
else
|
|
30
|
-
newMessages.push({
|
|
31
|
-
role: "user",
|
|
32
|
-
content: JSON.stringify(originalUserInputMessages),
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
newMessages.push(...messages);
|
|
36
|
-
return { ...input, [exports.USER_INPUT_MESSAGE_KEY]: newMessages };
|
|
17
|
+
function getMessage(input) {
|
|
18
|
+
const userInputMessage = input[exports.MESSAGE_KEY];
|
|
19
|
+
if (typeof userInputMessage === "string")
|
|
20
|
+
return userInputMessage;
|
|
21
|
+
if (!(0, lodash_es_1.isNil)(userInputMessage))
|
|
22
|
+
return JSON.stringify(userInputMessage);
|
|
23
|
+
return undefined;
|
|
37
24
|
}
|
|
38
25
|
class PromptBuilder {
|
|
39
26
|
static from(instructions) {
|
|
@@ -51,13 +38,28 @@ class PromptBuilder {
|
|
|
51
38
|
}
|
|
52
39
|
static fromMCPPromptResult(result) {
|
|
53
40
|
return new PromptBuilder({
|
|
54
|
-
instructions:
|
|
55
|
-
|
|
41
|
+
instructions: template_js_1.ChatMessagesTemplate.from(result.messages.map((i) => {
|
|
42
|
+
let content;
|
|
43
|
+
if (i.content.type === "text")
|
|
44
|
+
content = i.content.text;
|
|
45
|
+
else if (i.content.type === "resource") {
|
|
46
|
+
const { resource } = i.content;
|
|
47
|
+
if (typeof resource.text === "string") {
|
|
48
|
+
content = resource.text;
|
|
49
|
+
}
|
|
50
|
+
else if (typeof resource.blob === "string") {
|
|
51
|
+
content = [{ type: "image_url", url: resource.blob }];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else if (i.content.type === "image") {
|
|
55
|
+
content = [{ type: "image_url", url: i.content.data }];
|
|
56
|
+
}
|
|
57
|
+
if (!content)
|
|
56
58
|
throw new Error(`Unsupported content type ${i.content.type}`);
|
|
57
59
|
if (i.role === "user")
|
|
58
|
-
return
|
|
60
|
+
return template_js_1.UserMessageTemplate.from(content);
|
|
59
61
|
if (i.role === "assistant")
|
|
60
|
-
return
|
|
62
|
+
return template_js_1.AgentMessageTemplate.from(content);
|
|
61
63
|
throw new Error(`Unsupported role ${i.role}`);
|
|
62
64
|
})),
|
|
63
65
|
});
|
|
@@ -66,10 +68,6 @@ class PromptBuilder {
|
|
|
66
68
|
this.instructions = options?.instructions;
|
|
67
69
|
}
|
|
68
70
|
instructions;
|
|
69
|
-
histories = [];
|
|
70
|
-
addHistory(...messages) {
|
|
71
|
-
this.histories.push(...messages);
|
|
72
|
-
}
|
|
73
71
|
async build(options) {
|
|
74
72
|
return {
|
|
75
73
|
messages: this.buildMessages(options),
|
|
@@ -80,37 +78,28 @@ class PromptBuilder {
|
|
|
80
78
|
buildMessages(options) {
|
|
81
79
|
const { input } = options;
|
|
82
80
|
const messages = (typeof this.instructions === "string"
|
|
83
|
-
?
|
|
81
|
+
? template_js_1.ChatMessagesTemplate.from([template_js_1.SystemMessageTemplate.from(this.instructions)])
|
|
84
82
|
: this.instructions)?.format(options.input) ?? [];
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
userMessages.push(template_1.UserMessageTemplate.from(userInputMessage).format());
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
const messages = (0, template_1.parseChatMessages)(userInputMessage);
|
|
99
|
-
if (messages)
|
|
100
|
-
userMessages.push(...messages.map((i) => i.format()));
|
|
101
|
-
else
|
|
102
|
-
userMessages.push(template_1.UserMessageTemplate.from(JSON.stringify(userInputMessage)).format());
|
|
103
|
-
}
|
|
83
|
+
const memory = options.memory ?? options.agent?.memory;
|
|
84
|
+
if (memory?.enabled) {
|
|
85
|
+
const k = memory.maxMemoriesInChat ?? exports.DEFAULT_MAX_HISTORY_MESSAGES;
|
|
86
|
+
const histories = memory.memories.slice(-k);
|
|
87
|
+
if (histories?.length)
|
|
88
|
+
messages.push(...histories.map((i) => ({
|
|
89
|
+
role: i.role,
|
|
90
|
+
content: convertMessageToContent(i.content),
|
|
91
|
+
name: i.source,
|
|
92
|
+
})));
|
|
104
93
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
messages.push(
|
|
94
|
+
const content = input && getMessage(input);
|
|
95
|
+
// add user input if it's not the same as the last message
|
|
96
|
+
if (content && messages.at(-1)?.content !== content) {
|
|
97
|
+
messages.push({ role: "user", content });
|
|
109
98
|
}
|
|
110
99
|
return messages;
|
|
111
100
|
}
|
|
112
101
|
buildResponseFormat(options) {
|
|
113
|
-
const outputSchema = options.agent?.outputSchema;
|
|
102
|
+
const outputSchema = options.outputSchema || options.agent?.outputSchema;
|
|
114
103
|
if (!outputSchema)
|
|
115
104
|
return undefined;
|
|
116
105
|
const isJsonOutput = !isEmptyObjectType(outputSchema);
|
|
@@ -119,7 +108,7 @@ class PromptBuilder {
|
|
|
119
108
|
type: "json_schema",
|
|
120
109
|
jsonSchema: {
|
|
121
110
|
name: "output",
|
|
122
|
-
schema: (0,
|
|
111
|
+
schema: (0, json_schema_js_1.outputSchemaToResponseFormatSchema)(outputSchema),
|
|
123
112
|
strict: true,
|
|
124
113
|
},
|
|
125
114
|
}
|
|
@@ -135,14 +124,16 @@ class PromptBuilder {
|
|
|
135
124
|
function: {
|
|
136
125
|
name: i.name,
|
|
137
126
|
description: i.description,
|
|
138
|
-
parameters: !isEmptyObjectType(i.inputSchema)
|
|
127
|
+
parameters: !isEmptyObjectType(i.inputSchema)
|
|
128
|
+
? (0, json_schema_js_1.outputSchemaToResponseFormatSchema)(i.inputSchema)
|
|
129
|
+
: {},
|
|
139
130
|
},
|
|
140
131
|
}));
|
|
141
132
|
let toolChoice;
|
|
142
133
|
// use manual choice if configured in the agent
|
|
143
134
|
const manualChoice = options.agent?.toolChoice;
|
|
144
135
|
if (manualChoice) {
|
|
145
|
-
if (manualChoice instanceof
|
|
136
|
+
if (manualChoice instanceof agent_js_1.Agent) {
|
|
146
137
|
toolChoice = {
|
|
147
138
|
type: "function",
|
|
148
139
|
function: {
|
|
@@ -179,3 +170,7 @@ function isFromPath(value) {
|
|
|
179
170
|
function isEmptyObjectType(schema) {
|
|
180
171
|
return schema instanceof zod_1.ZodObject && Object.keys(schema.shape).length === 0;
|
|
181
172
|
}
|
|
173
|
+
function convertMessageToContent(i) {
|
|
174
|
+
const str = i[exports.MESSAGE_KEY];
|
|
175
|
+
return !(0, lodash_es_1.isNil)(str) ? (typeof str === "string" ? str : JSON.stringify(str)) : JSON.stringify(i);
|
|
176
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ChatModelInputMessage, ChatModelOutputToolCall } from "../models/chat";
|
|
1
|
+
import type { ChatModelInputMessage, ChatModelInputMessageContent, ChatModelOutputToolCall } from "../models/chat-model.js";
|
|
2
2
|
export declare class PromptTemplate {
|
|
3
3
|
template: string;
|
|
4
4
|
static from(template: string): PromptTemplate;
|
|
@@ -16,22 +16,16 @@ export declare class SystemMessageTemplate extends ChatMessageTemplate {
|
|
|
16
16
|
static from(content: string, name?: string): SystemMessageTemplate;
|
|
17
17
|
}
|
|
18
18
|
export declare class UserMessageTemplate extends ChatMessageTemplate {
|
|
19
|
-
static from(template:
|
|
19
|
+
static from(template: ChatModelInputMessageContent, name?: string): UserMessageTemplate;
|
|
20
20
|
}
|
|
21
21
|
export declare class AgentMessageTemplate extends ChatMessageTemplate {
|
|
22
22
|
toolCalls?: ChatModelOutputToolCall[] | undefined;
|
|
23
|
-
static from(template
|
|
24
|
-
constructor(content?:
|
|
23
|
+
static from(template?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[], name?: string): AgentMessageTemplate;
|
|
24
|
+
constructor(content?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string);
|
|
25
25
|
format(variables?: Record<string, unknown>): {
|
|
26
26
|
toolCalls: ChatModelOutputToolCall[] | undefined;
|
|
27
|
-
role: import("../models/chat").Role;
|
|
28
|
-
content?:
|
|
29
|
-
type: "text";
|
|
30
|
-
text: string;
|
|
31
|
-
} | {
|
|
32
|
-
type: "image_url";
|
|
33
|
-
url: string;
|
|
34
|
-
})[];
|
|
27
|
+
role: import("../models/chat-model.js").Role;
|
|
28
|
+
content?: ChatModelInputMessageContent;
|
|
35
29
|
toolCallId?: string;
|
|
36
30
|
name?: string;
|
|
37
31
|
};
|
|
@@ -42,20 +36,14 @@ export declare class ToolMessageTemplate extends ChatMessageTemplate {
|
|
|
42
36
|
constructor(content: object | string, toolCallId: string, name?: string);
|
|
43
37
|
format(variables?: Record<string, unknown>): {
|
|
44
38
|
toolCallId: string;
|
|
45
|
-
role: import("../models/chat").Role;
|
|
46
|
-
content?:
|
|
47
|
-
type: "text";
|
|
48
|
-
text: string;
|
|
49
|
-
} | {
|
|
50
|
-
type: "image_url";
|
|
51
|
-
url: string;
|
|
52
|
-
})[];
|
|
39
|
+
role: import("../models/chat-model.js").Role;
|
|
40
|
+
content?: ChatModelInputMessageContent;
|
|
53
41
|
toolCalls?: {
|
|
54
42
|
id: string;
|
|
55
43
|
type: "function";
|
|
56
44
|
function: {
|
|
57
45
|
name: string;
|
|
58
|
-
arguments: import("
|
|
46
|
+
arguments: import("../index.js").Message;
|
|
59
47
|
};
|
|
60
48
|
}[];
|
|
61
49
|
name?: string;
|
|
@@ -67,10 +67,8 @@ class UserMessageTemplate extends ChatMessageTemplate {
|
|
|
67
67
|
exports.UserMessageTemplate = UserMessageTemplate;
|
|
68
68
|
class AgentMessageTemplate extends ChatMessageTemplate {
|
|
69
69
|
toolCalls;
|
|
70
|
-
static from(template, name) {
|
|
71
|
-
return
|
|
72
|
-
? new AgentMessageTemplate(template, undefined, name)
|
|
73
|
-
: new AgentMessageTemplate(undefined, template, name);
|
|
70
|
+
static from(template, toolCalls, name) {
|
|
71
|
+
return new AgentMessageTemplate(template, toolCalls, name);
|
|
74
72
|
}
|
|
75
73
|
constructor(content, toolCalls, name) {
|
|
76
74
|
super("agent", content, name);
|
|
@@ -164,7 +162,7 @@ function parseChatMessages(messages) {
|
|
|
164
162
|
case "user":
|
|
165
163
|
return UserMessageTemplate.from(message.content, message.name);
|
|
166
164
|
case "agent":
|
|
167
|
-
return
|
|
165
|
+
return AgentMessageTemplate.from(message.content, message.toolCalls, message.name);
|
|
168
166
|
case "tool":
|
|
169
167
|
return ToolMessageTemplate.from(message.content, message.toolCallId, message.name);
|
|
170
168
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.outputSchemaToResponseFormatSchema = outputSchemaToResponseFormatSchema;
|
|
4
|
+
const lodash_es_1 = require("lodash-es");
|
|
5
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
6
|
+
function outputSchemaToResponseFormatSchema(agentOutput) {
|
|
7
|
+
return setAdditionPropertiesDeep((0, zod_to_json_schema_1.zodToJsonSchema)(agentOutput), false);
|
|
8
|
+
}
|
|
9
|
+
function setAdditionPropertiesDeep(schema, additionalProperties) {
|
|
10
|
+
if (Array.isArray(schema)) {
|
|
11
|
+
return schema.map((s) => setAdditionPropertiesDeep(s, additionalProperties));
|
|
12
|
+
}
|
|
13
|
+
if ((0, lodash_es_1.isObject)(schema)) {
|
|
14
|
+
return Object.entries(schema).reduce((acc, [key, value]) => {
|
|
15
|
+
acc[key] = setAdditionPropertiesDeep(value, additionalProperties);
|
|
16
|
+
if (acc.type === "object") {
|
|
17
|
+
acc.additionalProperties = additionalProperties;
|
|
18
|
+
}
|
|
19
|
+
return acc;
|
|
20
|
+
}, {});
|
|
21
|
+
}
|
|
22
|
+
return schema;
|
|
23
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
-
import type
|
|
3
|
-
import { MCPPrompt, MCPTool } from "../agents/mcp-agent";
|
|
2
|
+
import { type ListPromptsResult, type ListResourceTemplatesResult, type ListResourcesResult, type ListToolsResult } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
+
import { MCPPrompt, MCPResource, MCPTool } from "../agents/mcp-agent.js";
|
|
4
4
|
export declare function toolFromMCPTool(client: Client, tool: ListToolsResult["tools"][number]): MCPTool;
|
|
5
5
|
export declare function promptFromMCPPrompt(client: Client, prompt: ListPromptsResult["prompts"][number]): MCPPrompt;
|
|
6
|
+
export declare function resourceFromMCPResource(client: Client, resource: ListResourcesResult["resources"][number] | ListResourceTemplatesResult["resourceTemplates"][number]): MCPResource;
|