p-api-agent 0.0.2 → 0.0.3
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/dist/index.cjs +372 -0
- package/dist/index.d.cts +148 -0
- package/dist/index.d.ts +68 -38
- package/dist/index.js +188 -245
- package/package.json +3 -3
- package/dist/index.d.mts +0 -118
- package/dist/index.mjs +0 -369
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var index_exports = {};
|
|
33
|
+
__export(index_exports, {
|
|
34
|
+
Agent: () => Agent,
|
|
35
|
+
FunctionCall: () => FunctionCall,
|
|
36
|
+
LLM_Utils: () => LLM_Utils
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
|
|
40
|
+
// src/agent/index.ts
|
|
41
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
42
|
+
|
|
43
|
+
// src/utils/llm_utils.ts
|
|
44
|
+
var import_jsonrepair = require("jsonrepair");
|
|
45
|
+
var _LLM_Utils = class {
|
|
46
|
+
/**
|
|
47
|
+
* 提取大模型返回的markdown
|
|
48
|
+
*/
|
|
49
|
+
extract_markdown(content) {
|
|
50
|
+
const regex = /```markdown\n(.*?)\n```/s;
|
|
51
|
+
const match = content.match(regex);
|
|
52
|
+
if (!match) return content;
|
|
53
|
+
return match[1];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 提取大模型的json格式
|
|
57
|
+
*/
|
|
58
|
+
extract_json(content) {
|
|
59
|
+
const regex = /```json\n(.*?)\n```/s;
|
|
60
|
+
const match = content.match(regex);
|
|
61
|
+
let json_content = match ? match[1] : content;
|
|
62
|
+
try {
|
|
63
|
+
return JSON.parse(json_content);
|
|
64
|
+
} catch (err) {
|
|
65
|
+
return JSON.parse((0, import_jsonrepair.jsonrepair)(json_content));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 提取mermaid格式
|
|
70
|
+
*/
|
|
71
|
+
extract_mermaid(content) {
|
|
72
|
+
const regex = /```mermaid\n(.*?)\n```/s;
|
|
73
|
+
const match = content.match(regex);
|
|
74
|
+
if (!match) return content;
|
|
75
|
+
return match[1];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* 提取svg格式
|
|
79
|
+
*/
|
|
80
|
+
extract_svg(content) {
|
|
81
|
+
const regex = /```svg\n(.*?)\n```/s;
|
|
82
|
+
const match = content.match(regex);
|
|
83
|
+
if (!match) return content;
|
|
84
|
+
return match[1];
|
|
85
|
+
}
|
|
86
|
+
parse_json(content) {
|
|
87
|
+
if (typeof content === "object") {
|
|
88
|
+
return content;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
return JSON.parse(content);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
return JSON.parse((0, import_jsonrepair.jsonrepair)(content));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
var LLM_Utils = new _LLM_Utils();
|
|
98
|
+
|
|
99
|
+
// src/agent/index.ts
|
|
100
|
+
var import_path = __toESM(require("path"), 1);
|
|
101
|
+
var Agent = class {
|
|
102
|
+
constructor(options = {}) {
|
|
103
|
+
__publicField(this, "function_call", null);
|
|
104
|
+
__publicField(this, "llm_chat_func", null);
|
|
105
|
+
__publicField(this, "max_loop");
|
|
106
|
+
__publicField(this, "retry_times");
|
|
107
|
+
__publicField(this, "custom_system_prompt", null);
|
|
108
|
+
this.max_loop = options.maxLoop ?? 20;
|
|
109
|
+
this.retry_times = options.retryTimes ?? 2;
|
|
110
|
+
if (options.customSystemPrompt) {
|
|
111
|
+
this.custom_system_prompt = options.customSystemPrompt;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// ─────────── Registration ───────────
|
|
115
|
+
/** 注册工具函数集合,支持链式调用 */
|
|
116
|
+
register_function_call(fc) {
|
|
117
|
+
this.function_call = fc;
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
/** 注册 LLM 文字能力,支持链式调用 */
|
|
121
|
+
register_llm_text_ability(func) {
|
|
122
|
+
this.llm_chat_func = func;
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
/** 覆盖系统提示词 */
|
|
126
|
+
set_system_prompt(prompt) {
|
|
127
|
+
this.custom_system_prompt = prompt;
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
/** 动态修改最大循环次数 */
|
|
131
|
+
set_max_loop(n) {
|
|
132
|
+
this.max_loop = n;
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
// ─────────── Main ReAct Loop ───────────
|
|
136
|
+
/**
|
|
137
|
+
* 发起对话。
|
|
138
|
+
*
|
|
139
|
+
* 架构:ReAct 单调用循环
|
|
140
|
+
* 每轮只调用一次 LLM,LLM 在拥有完整工具 schema 的系统提示词下
|
|
141
|
+
* 直接输出 end / use_tool / no_ability 三种指令,
|
|
142
|
+
* 彻底消除了旧版 "判断→查文档→生成参数→再生成答案" 的多次调用开销。
|
|
143
|
+
*/
|
|
144
|
+
async create_chat(user_input) {
|
|
145
|
+
if (!this.llm_chat_func) {
|
|
146
|
+
return { result: "\u672A\u6CE8\u518CLLM\u80FD\u529B", use_tools: [] };
|
|
147
|
+
}
|
|
148
|
+
const history = this.normalize_input(user_input);
|
|
149
|
+
const system_msg = {
|
|
150
|
+
role: "system",
|
|
151
|
+
content: [{ type: "text", text: this.build_system_prompt() }]
|
|
152
|
+
};
|
|
153
|
+
const result = { result: "", use_tools: [] };
|
|
154
|
+
const tool_fail_count = {};
|
|
155
|
+
for (let turn = 0; turn < this.max_loop; turn++) {
|
|
156
|
+
let raw;
|
|
157
|
+
try {
|
|
158
|
+
raw = await this.call_llm_with_retry([system_msg, ...history]);
|
|
159
|
+
} catch (err) {
|
|
160
|
+
result.result = `LLM\u8C03\u7528\u5931\u8D25: ${err?.message ?? String(err)}`;
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
const cmd = this.parse_command(raw);
|
|
164
|
+
if (!cmd) {
|
|
165
|
+
result.result = raw;
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
if (cmd.command === "end") {
|
|
169
|
+
result.result = cmd.result;
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
if (cmd.command === "no_ability") {
|
|
173
|
+
result.result = cmd.result || "\u65E0\u6CD5\u6267\u884C\u6B64\u64CD\u4F5C";
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
if (cmd.command === "use_tool") {
|
|
177
|
+
const { tool_name, params } = cmd;
|
|
178
|
+
if (!this.function_call) {
|
|
179
|
+
result.result = "\u672A\u6CE8\u518C\u5DE5\u5177\u51FD\u6570\u96C6\u5408\uFF0C\u65E0\u6CD5\u6267\u884C\u5DE5\u5177\u8C03\u7528";
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
history.push({
|
|
183
|
+
role: "assistant",
|
|
184
|
+
content: [{ type: "text", text: raw }]
|
|
185
|
+
});
|
|
186
|
+
let tool_result;
|
|
187
|
+
try {
|
|
188
|
+
tool_result = await this.function_call.exec_function(tool_name, params);
|
|
189
|
+
} catch (err) {
|
|
190
|
+
const err_msg = err?.message ?? String(err);
|
|
191
|
+
tool_fail_count[tool_name] = (tool_fail_count[tool_name] ?? 0) + 1;
|
|
192
|
+
history.push({
|
|
193
|
+
role: "user",
|
|
194
|
+
content: [{
|
|
195
|
+
type: "text",
|
|
196
|
+
text: `[\u5DE5\u5177\u6267\u884C\u5931\u8D25] ${tool_name}
|
|
197
|
+
\u9519\u8BEF\u4FE1\u606F: ${err_msg}
|
|
198
|
+
\u8BF7\u6839\u636E\u9519\u8BEF\u8C03\u6574\u7B56\u7565\u540E\u7EE7\u7EED`
|
|
199
|
+
}]
|
|
200
|
+
});
|
|
201
|
+
if (tool_fail_count[tool_name] >= 2) {
|
|
202
|
+
result.result = `\u5DE5\u5177 "${tool_name}" \u8FDE\u7EED\u8C03\u7528\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5`;
|
|
203
|
+
return result;
|
|
204
|
+
}
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
history.push({
|
|
208
|
+
role: "user",
|
|
209
|
+
content: [{
|
|
210
|
+
type: "text",
|
|
211
|
+
text: `[\u5DE5\u5177\u8C03\u7528\u7ED3\u679C] ${tool_name}
|
|
212
|
+
${JSON.stringify(tool_result, null, 2)}`
|
|
213
|
+
}]
|
|
214
|
+
});
|
|
215
|
+
result.use_tools.push({ tool_name, params, exec_result: tool_result });
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
result.result = "\u8D85\u51FA\u6700\u5927\u63A8\u7406\u8F6E\u6B21\uFF0C\u8BF7\u7CBE\u7B80\u95EE\u9898\u540E\u91CD\u8BD5";
|
|
220
|
+
return result;
|
|
221
|
+
}
|
|
222
|
+
// ─────────── System Prompt ───────────
|
|
223
|
+
/**
|
|
224
|
+
* 构建系统提示词。
|
|
225
|
+
* 将所有工具的完整 schema(含参数类型、必填项、示例值)
|
|
226
|
+
* 直接注入系统提示词,让 LLM 在单次调用中就能做出准确的工具选择和参数填写。
|
|
227
|
+
*/
|
|
228
|
+
build_system_prompt() {
|
|
229
|
+
if (this.custom_system_prompt) return this.custom_system_prompt;
|
|
230
|
+
const base = this.load_preset_prompt();
|
|
231
|
+
if (!this.function_call) return base;
|
|
232
|
+
const tool_schemas = this.function_call.get_tools_with_schema().map((t) => {
|
|
233
|
+
const props = Object.entries(t.input_schema.properties).map(([k, v]) => {
|
|
234
|
+
const req = t.input_schema.required?.includes(k) ? "\u5FC5\u586B" : "\u53EF\u9009";
|
|
235
|
+
const ex = v.examples?.length ? `\uFF0C\u793A\u4F8B: ${v.examples.join(" / ")}` : "";
|
|
236
|
+
return ` - ${k} (${v.type}, ${req}): ${v.description}${ex}`;
|
|
237
|
+
}).join("\n");
|
|
238
|
+
return `### ${t.name}
|
|
239
|
+
\u63CF\u8FF0: ${t.description}
|
|
240
|
+
\u53C2\u6570:
|
|
241
|
+
${props}`;
|
|
242
|
+
}).join("\n\n");
|
|
243
|
+
return `${base}
|
|
244
|
+
|
|
245
|
+
## \u53EF\u7528\u5DE5\u5177\u5217\u8868
|
|
246
|
+
|
|
247
|
+
${tool_schemas}`;
|
|
248
|
+
}
|
|
249
|
+
load_preset_prompt() {
|
|
250
|
+
try {
|
|
251
|
+
return import_fs.default.readFileSync(import_path.default.join(__dirname, "./preset_prompt.md"), "utf-8");
|
|
252
|
+
} catch {
|
|
253
|
+
return "";
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
// ─────────── Helpers ───────────
|
|
257
|
+
normalize_input(input) {
|
|
258
|
+
if (typeof input === "string") {
|
|
259
|
+
return [{ role: "user", content: [{ type: "text", text: input }] }];
|
|
260
|
+
}
|
|
261
|
+
return [...input];
|
|
262
|
+
}
|
|
263
|
+
parse_command(raw) {
|
|
264
|
+
const json = this.safe_extract_json(raw);
|
|
265
|
+
if (!json || typeof json.command !== "string") return null;
|
|
266
|
+
return json;
|
|
267
|
+
}
|
|
268
|
+
safe_extract_json(content) {
|
|
269
|
+
try {
|
|
270
|
+
return LLM_Utils.extract_json(content);
|
|
271
|
+
} catch {
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
async call_llm_with_retry(input) {
|
|
276
|
+
let last_error;
|
|
277
|
+
for (let i = 0; i <= this.retry_times; i++) {
|
|
278
|
+
try {
|
|
279
|
+
const res = await this.llm_chat_func(input);
|
|
280
|
+
if (typeof res === "string" && res.trim()) return res;
|
|
281
|
+
throw new Error("\u7A7A\u54CD\u5E94");
|
|
282
|
+
} catch (err) {
|
|
283
|
+
last_error = err;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
throw last_error ?? new Error("LLM\u8C03\u7528\u5931\u8D25");
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// src/function_call/index.ts
|
|
291
|
+
var import_path2 = __toESM(require("path"), 1);
|
|
292
|
+
var import_fs2 = __toESM(require("fs"), 1);
|
|
293
|
+
var FunctionCall = class {
|
|
294
|
+
constructor(tool_path) {
|
|
295
|
+
this.tool_path = tool_path;
|
|
296
|
+
__publicField(this, "tools", {});
|
|
297
|
+
this.init();
|
|
298
|
+
}
|
|
299
|
+
init() {
|
|
300
|
+
for (const file_name of import_fs2.default.readdirSync(this.tool_path)) {
|
|
301
|
+
const route_path = import_path2.default.join(this.tool_path, file_name);
|
|
302
|
+
const mcp = require(route_path);
|
|
303
|
+
const info = mcp.register();
|
|
304
|
+
this.tools[info.name] = info;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* 生成完整的工具函数说明文档
|
|
309
|
+
*/
|
|
310
|
+
gen_tool_doc(name) {
|
|
311
|
+
const info = this.tools[name];
|
|
312
|
+
if (!info) {
|
|
313
|
+
throw `${name}\u5DE5\u5177\u51FD\u6570\u4E0D\u5B58\u5728`;
|
|
314
|
+
}
|
|
315
|
+
const schema = info.input_schema;
|
|
316
|
+
const schemaDesc = `\u7C7B\u578B: ${schema.type}`;
|
|
317
|
+
const params = Object.keys(schema.properties).map((key) => {
|
|
318
|
+
const item = schema.properties[key];
|
|
319
|
+
const required = schema.required?.includes(key) ? "\u5FC5\u586B" : "\u53EF\u9009";
|
|
320
|
+
const examples = item.examples && item.examples.length > 0 ? `\u793A\u4F8B\u503C: ${item.examples.join(", ")}` : "\u65E0\u793A\u4F8B";
|
|
321
|
+
return ` - ${key} (${item.type}, ${required})
|
|
322
|
+
\u8BF4\u660E: ${item.description}
|
|
323
|
+
${examples}`;
|
|
324
|
+
}).join("\n");
|
|
325
|
+
return `\u5DE5\u5177\u51FD\u6570\u540D\u79F0: ${info.name}
|
|
326
|
+
\u529F\u80FD\u63CF\u8FF0: ${info.description}
|
|
327
|
+
\u8F93\u5165\u7ED3\u6784: ${schemaDesc}
|
|
328
|
+
\u53C2\u6570\u5217\u8868:
|
|
329
|
+
${params}`;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* 获取工具函数列表(仅名称 + 描述)
|
|
333
|
+
*/
|
|
334
|
+
get_tools_list() {
|
|
335
|
+
return Object.keys(this.tools).map((key) => {
|
|
336
|
+
const info = this.tools[key];
|
|
337
|
+
return {
|
|
338
|
+
name: info.name,
|
|
339
|
+
description: info.description
|
|
340
|
+
};
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* 获取工具函数列表(含完整 input_schema),供 Agent 注入系统提示词
|
|
345
|
+
*/
|
|
346
|
+
get_tools_with_schema() {
|
|
347
|
+
return Object.keys(this.tools).map((key) => {
|
|
348
|
+
const info = this.tools[key];
|
|
349
|
+
return {
|
|
350
|
+
name: info.name,
|
|
351
|
+
description: info.description,
|
|
352
|
+
input_schema: info.input_schema
|
|
353
|
+
};
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* 执行工具函数
|
|
358
|
+
*/
|
|
359
|
+
async exec_function(name, params) {
|
|
360
|
+
const info = this.tools[name];
|
|
361
|
+
if (!info) {
|
|
362
|
+
throw `\u5DE5\u5177\u51FD\u6570${name}\u4E0D\u5B58\u5728`;
|
|
363
|
+
}
|
|
364
|
+
return await info.register_func(params);
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
368
|
+
0 && (module.exports = {
|
|
369
|
+
Agent,
|
|
370
|
+
FunctionCall,
|
|
371
|
+
LLM_Utils
|
|
372
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
interface RegisterInfo {
|
|
2
|
+
name: string;
|
|
3
|
+
description: string;
|
|
4
|
+
input_schema: {
|
|
5
|
+
type: string;
|
|
6
|
+
properties: {
|
|
7
|
+
[key: string]: {
|
|
8
|
+
type: string;
|
|
9
|
+
description: string;
|
|
10
|
+
examples?: string[];
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
required?: string[];
|
|
14
|
+
};
|
|
15
|
+
register_func: (...args: any[]) => Promise<any>;
|
|
16
|
+
}
|
|
17
|
+
declare class FunctionCall {
|
|
18
|
+
tool_path: string;
|
|
19
|
+
private tools;
|
|
20
|
+
constructor(tool_path: string);
|
|
21
|
+
private init;
|
|
22
|
+
/**
|
|
23
|
+
* 生成完整的工具函数说明文档
|
|
24
|
+
*/
|
|
25
|
+
gen_tool_doc(name: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* 获取工具函数列表(仅名称 + 描述)
|
|
28
|
+
*/
|
|
29
|
+
get_tools_list(): {
|
|
30
|
+
name: string;
|
|
31
|
+
description: string;
|
|
32
|
+
}[];
|
|
33
|
+
/**
|
|
34
|
+
* 获取工具函数列表(含完整 input_schema),供 Agent 注入系统提示词
|
|
35
|
+
*/
|
|
36
|
+
get_tools_with_schema(): {
|
|
37
|
+
name: string;
|
|
38
|
+
description: string;
|
|
39
|
+
input_schema: {
|
|
40
|
+
type: string;
|
|
41
|
+
properties: {
|
|
42
|
+
[key: string]: {
|
|
43
|
+
type: string;
|
|
44
|
+
description: string;
|
|
45
|
+
examples?: string[];
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
required?: string[];
|
|
49
|
+
};
|
|
50
|
+
}[];
|
|
51
|
+
/**
|
|
52
|
+
* 执行工具函数
|
|
53
|
+
*/
|
|
54
|
+
exec_function(name: string, params: any): Promise<any>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface TextContent {
|
|
58
|
+
type: 'text';
|
|
59
|
+
text: string;
|
|
60
|
+
}
|
|
61
|
+
interface ImageContent {
|
|
62
|
+
type: 'image_url';
|
|
63
|
+
image_url: {
|
|
64
|
+
url: string;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
type Content = TextContent | ImageContent;
|
|
68
|
+
type Message = {
|
|
69
|
+
role: 'system' | 'user' | 'assistant';
|
|
70
|
+
content: Content[];
|
|
71
|
+
};
|
|
72
|
+
type UserChatInput = Message[];
|
|
73
|
+
interface ToolRecord {
|
|
74
|
+
tool_name: string;
|
|
75
|
+
params: any;
|
|
76
|
+
exec_result: any;
|
|
77
|
+
}
|
|
78
|
+
interface CreateChatResult {
|
|
79
|
+
result: string;
|
|
80
|
+
use_tools: ToolRecord[];
|
|
81
|
+
}
|
|
82
|
+
interface AgentOptions {
|
|
83
|
+
/** 最大推理循环次数,默认 20 */
|
|
84
|
+
maxLoop?: number;
|
|
85
|
+
/** LLM 失败重试次数,默认 2 */
|
|
86
|
+
retryTimes?: number;
|
|
87
|
+
/** 完全覆盖系统提示词 */
|
|
88
|
+
customSystemPrompt?: string;
|
|
89
|
+
}
|
|
90
|
+
declare class Agent {
|
|
91
|
+
private function_call;
|
|
92
|
+
private llm_chat_func;
|
|
93
|
+
private max_loop;
|
|
94
|
+
private retry_times;
|
|
95
|
+
private custom_system_prompt;
|
|
96
|
+
constructor(options?: AgentOptions);
|
|
97
|
+
/** 注册工具函数集合,支持链式调用 */
|
|
98
|
+
register_function_call(fc: FunctionCall): this;
|
|
99
|
+
/** 注册 LLM 文字能力,支持链式调用 */
|
|
100
|
+
register_llm_text_ability(func: (input: UserChatInput) => Promise<string>): this;
|
|
101
|
+
/** 覆盖系统提示词 */
|
|
102
|
+
set_system_prompt(prompt: string): this;
|
|
103
|
+
/** 动态修改最大循环次数 */
|
|
104
|
+
set_max_loop(n: number): this;
|
|
105
|
+
/**
|
|
106
|
+
* 发起对话。
|
|
107
|
+
*
|
|
108
|
+
* 架构:ReAct 单调用循环
|
|
109
|
+
* 每轮只调用一次 LLM,LLM 在拥有完整工具 schema 的系统提示词下
|
|
110
|
+
* 直接输出 end / use_tool / no_ability 三种指令,
|
|
111
|
+
* 彻底消除了旧版 "判断→查文档→生成参数→再生成答案" 的多次调用开销。
|
|
112
|
+
*/
|
|
113
|
+
create_chat(user_input: string | UserChatInput): Promise<CreateChatResult>;
|
|
114
|
+
/**
|
|
115
|
+
* 构建系统提示词。
|
|
116
|
+
* 将所有工具的完整 schema(含参数类型、必填项、示例值)
|
|
117
|
+
* 直接注入系统提示词,让 LLM 在单次调用中就能做出准确的工具选择和参数填写。
|
|
118
|
+
*/
|
|
119
|
+
private build_system_prompt;
|
|
120
|
+
private load_preset_prompt;
|
|
121
|
+
private normalize_input;
|
|
122
|
+
private parse_command;
|
|
123
|
+
private safe_extract_json;
|
|
124
|
+
private call_llm_with_retry;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
declare class _LLM_Utils {
|
|
128
|
+
/**
|
|
129
|
+
* 提取大模型返回的markdown
|
|
130
|
+
*/
|
|
131
|
+
extract_markdown(content: any): any;
|
|
132
|
+
/**
|
|
133
|
+
* 提取大模型的json格式
|
|
134
|
+
*/
|
|
135
|
+
extract_json(content: any): any;
|
|
136
|
+
/**
|
|
137
|
+
* 提取mermaid格式
|
|
138
|
+
*/
|
|
139
|
+
extract_mermaid(content: any): any;
|
|
140
|
+
/**
|
|
141
|
+
* 提取svg格式
|
|
142
|
+
*/
|
|
143
|
+
extract_svg(content: any): any;
|
|
144
|
+
parse_json(content: string | object): any;
|
|
145
|
+
}
|
|
146
|
+
declare const LLM_Utils: _LLM_Utils;
|
|
147
|
+
|
|
148
|
+
export { Agent, type AgentOptions, type CreateChatResult, FunctionCall, LLM_Utils, type Message, type RegisterInfo, type ToolRecord, type UserChatInput };
|
package/dist/index.d.ts
CHANGED
|
@@ -24,12 +24,30 @@ declare class FunctionCall {
|
|
|
24
24
|
*/
|
|
25
25
|
gen_tool_doc(name: string): string;
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
27
|
+
* 获取工具函数列表(仅名称 + 描述)
|
|
28
28
|
*/
|
|
29
29
|
get_tools_list(): {
|
|
30
30
|
name: string;
|
|
31
31
|
description: string;
|
|
32
32
|
}[];
|
|
33
|
+
/**
|
|
34
|
+
* 获取工具函数列表(含完整 input_schema),供 Agent 注入系统提示词
|
|
35
|
+
*/
|
|
36
|
+
get_tools_with_schema(): {
|
|
37
|
+
name: string;
|
|
38
|
+
description: string;
|
|
39
|
+
input_schema: {
|
|
40
|
+
type: string;
|
|
41
|
+
properties: {
|
|
42
|
+
[key: string]: {
|
|
43
|
+
type: string;
|
|
44
|
+
description: string;
|
|
45
|
+
examples?: string[];
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
required?: string[];
|
|
49
|
+
};
|
|
50
|
+
}[];
|
|
33
51
|
/**
|
|
34
52
|
* 执行工具函数
|
|
35
53
|
*/
|
|
@@ -47,51 +65,63 @@ interface ImageContent {
|
|
|
47
65
|
};
|
|
48
66
|
}
|
|
49
67
|
type Content = TextContent | ImageContent;
|
|
50
|
-
type
|
|
68
|
+
type Message = {
|
|
51
69
|
role: 'system' | 'user' | 'assistant';
|
|
52
70
|
content: Content[];
|
|
53
|
-
}
|
|
71
|
+
};
|
|
72
|
+
type UserChatInput = Message[];
|
|
73
|
+
interface ToolRecord {
|
|
74
|
+
tool_name: string;
|
|
75
|
+
params: any;
|
|
76
|
+
exec_result: any;
|
|
77
|
+
}
|
|
78
|
+
interface CreateChatResult {
|
|
79
|
+
result: string;
|
|
80
|
+
use_tools: ToolRecord[];
|
|
81
|
+
}
|
|
82
|
+
interface AgentOptions {
|
|
83
|
+
/** 最大推理循环次数,默认 20 */
|
|
84
|
+
maxLoop?: number;
|
|
85
|
+
/** LLM 失败重试次数,默认 2 */
|
|
86
|
+
retryTimes?: number;
|
|
87
|
+
/** 完全覆盖系统提示词 */
|
|
88
|
+
customSystemPrompt?: string;
|
|
89
|
+
}
|
|
54
90
|
declare class Agent {
|
|
55
91
|
private function_call;
|
|
56
|
-
private function_call_doc;
|
|
57
92
|
private llm_chat_func;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
register_function_call(
|
|
64
|
-
/**
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
|
|
70
|
-
*/
|
|
71
|
-
register_llm_image_ability(): boolean;
|
|
72
|
-
/**
|
|
73
|
-
* 判断是否需要调用function_call, 以及调用哪个工具函数
|
|
74
|
-
*/
|
|
75
|
-
check_whether_use_function_call(user_input: UserChatInput): Promise<any>;
|
|
76
|
-
/**
|
|
77
|
-
* 创建聊天
|
|
78
|
-
*/
|
|
79
|
-
create_chat(user_input: string | UserChatInput): Promise<{
|
|
80
|
-
result: string;
|
|
81
|
-
use_tools: any[];
|
|
82
|
-
}>;
|
|
83
|
-
/**
|
|
84
|
-
* agent预置提示词
|
|
85
|
-
*/
|
|
86
|
-
get_preset_prompt(): string;
|
|
93
|
+
private max_loop;
|
|
94
|
+
private retry_times;
|
|
95
|
+
private custom_system_prompt;
|
|
96
|
+
constructor(options?: AgentOptions);
|
|
97
|
+
/** 注册工具函数集合,支持链式调用 */
|
|
98
|
+
register_function_call(fc: FunctionCall): this;
|
|
99
|
+
/** 注册 LLM 文字能力,支持链式调用 */
|
|
100
|
+
register_llm_text_ability(func: (input: UserChatInput) => Promise<string>): this;
|
|
101
|
+
/** 覆盖系统提示词 */
|
|
102
|
+
set_system_prompt(prompt: string): this;
|
|
103
|
+
/** 动态修改最大循环次数 */
|
|
104
|
+
set_max_loop(n: number): this;
|
|
87
105
|
/**
|
|
88
|
-
*
|
|
106
|
+
* 发起对话。
|
|
107
|
+
*
|
|
108
|
+
* 架构:ReAct 单调用循环
|
|
109
|
+
* 每轮只调用一次 LLM,LLM 在拥有完整工具 schema 的系统提示词下
|
|
110
|
+
* 直接输出 end / use_tool / no_ability 三种指令,
|
|
111
|
+
* 彻底消除了旧版 "判断→查文档→生成参数→再生成答案" 的多次调用开销。
|
|
89
112
|
*/
|
|
90
|
-
|
|
113
|
+
create_chat(user_input: string | UserChatInput): Promise<CreateChatResult>;
|
|
91
114
|
/**
|
|
92
|
-
*
|
|
115
|
+
* 构建系统提示词。
|
|
116
|
+
* 将所有工具的完整 schema(含参数类型、必填项、示例值)
|
|
117
|
+
* 直接注入系统提示词,让 LLM 在单次调用中就能做出准确的工具选择和参数填写。
|
|
93
118
|
*/
|
|
94
|
-
private
|
|
119
|
+
private build_system_prompt;
|
|
120
|
+
private load_preset_prompt;
|
|
121
|
+
private normalize_input;
|
|
122
|
+
private parse_command;
|
|
123
|
+
private safe_extract_json;
|
|
124
|
+
private call_llm_with_retry;
|
|
95
125
|
}
|
|
96
126
|
|
|
97
127
|
declare class _LLM_Utils {
|
|
@@ -115,4 +145,4 @@ declare class _LLM_Utils {
|
|
|
115
145
|
}
|
|
116
146
|
declare const LLM_Utils: _LLM_Utils;
|
|
117
147
|
|
|
118
|
-
export { Agent, FunctionCall, LLM_Utils, type RegisterInfo };
|
|
148
|
+
export { Agent, type AgentOptions, type CreateChatResult, FunctionCall, LLM_Utils, type Message, type RegisterInfo, type ToolRecord, type UserChatInput };
|