p-api-agent 0.0.7 → 0.0.8
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 +18 -123
- package/dist/index.d.cts +0 -10
- package/dist/index.d.ts +0 -10
- package/dist/index.js +18 -123
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -152,6 +152,7 @@ var Agent = class {
|
|
|
152
152
|
};
|
|
153
153
|
const result = { result: "", use_tools: [] };
|
|
154
154
|
const tool_fail_count = {};
|
|
155
|
+
let format_fail_count = 0;
|
|
155
156
|
for (let turn = 0; turn < this.max_loop; turn++) {
|
|
156
157
|
let raw;
|
|
157
158
|
try {
|
|
@@ -162,8 +163,19 @@ var Agent = class {
|
|
|
162
163
|
}
|
|
163
164
|
const cmd = this.parse_command(raw);
|
|
164
165
|
if (!cmd) {
|
|
165
|
-
|
|
166
|
-
|
|
166
|
+
format_fail_count++;
|
|
167
|
+
history.push({
|
|
168
|
+
role: "user",
|
|
169
|
+
content: [{
|
|
170
|
+
type: "text",
|
|
171
|
+
text: '\u4F60\u7684\u4E0A\u4E00\u6761\u8F93\u51FA\u683C\u5F0F\u4E0D\u5408\u6CD5\u3002\u8BF7\u4E25\u683C\u53EA\u8FD4\u56DE\u4E00\u6761 JSON \u6307\u4EE4\uFF0C\u4E14\u5FC5\u987B\u4F7F\u7528 {"command":"end"|"use_tool"|"no_ability", ...} \u7ED3\u6784\uFF0C\u4E0D\u8981\u8F93\u51FA <|FunctionCallBegin|>\u3001tool_calls\u3001\u6570\u7EC4\u5305\u88F9\u6216\u4EFB\u4F55\u989D\u5916\u6587\u672C\u3002'
|
|
172
|
+
}]
|
|
173
|
+
});
|
|
174
|
+
if (format_fail_count >= 2) {
|
|
175
|
+
result.result = "LLM\u8F93\u51FA\u683C\u5F0F\u4E0D\u7B26\u5408\u534F\u8BAE\uFF0C\u8BF7\u68C0\u67E5\u7CFB\u7EDF\u63D0\u793A\u8BCD\u6216\u6A21\u578B\u914D\u7F6E";
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
continue;
|
|
167
179
|
}
|
|
168
180
|
if (cmd.command === "end") {
|
|
169
181
|
result.result = cmd.result;
|
|
@@ -261,133 +273,16 @@ ${tool_schemas}`;
|
|
|
261
273
|
return [...input];
|
|
262
274
|
}
|
|
263
275
|
parse_command(raw) {
|
|
264
|
-
const
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
if (typeof json.command === "string") {
|
|
268
|
-
return json;
|
|
269
|
-
}
|
|
270
|
-
return this.adapt_common_tool_call(json);
|
|
276
|
+
const json = this.safe_extract_json(raw);
|
|
277
|
+
if (!json || typeof json.command !== "string") return null;
|
|
278
|
+
return json;
|
|
271
279
|
}
|
|
272
280
|
safe_extract_json(content) {
|
|
273
281
|
try {
|
|
274
282
|
return LLM_Utils.extract_json(content);
|
|
275
283
|
} catch {
|
|
276
|
-
|
|
277
|
-
if (!json_candidate) return null;
|
|
278
|
-
try {
|
|
279
|
-
return LLM_Utils.parse_json(json_candidate);
|
|
280
|
-
} catch {
|
|
281
|
-
return null;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* 兼容部分模型返回的函数调用包裹标记,例如:
|
|
287
|
-
* <|FunctionCallBegin|> ... <|FunctionCallEnd|>
|
|
288
|
-
*/
|
|
289
|
-
normalize_llm_output(raw) {
|
|
290
|
-
return raw.replace(/<\|FunctionCallBegin\|>/gi, "").replace(/<\|FunctionCallEnd\|>/gi, "").replace(/<\|tool_call\|>/gi, "").replace(/<tool_call>/gi, "").replace(/<\/tool_call>/gi, "").trim();
|
|
291
|
-
}
|
|
292
|
-
/** 从混杂文本中提取第一个 JSON 对象字符串 */
|
|
293
|
-
find_first_json_block(content) {
|
|
294
|
-
const fenced = content.match(/```json\s*([\s\S]*?)\s*```/i);
|
|
295
|
-
if (fenced?.[1]) return fenced[1];
|
|
296
|
-
const objectStart = content.indexOf("{");
|
|
297
|
-
const arrayStart = content.indexOf("[");
|
|
298
|
-
let start = -1;
|
|
299
|
-
let openChar = "{";
|
|
300
|
-
let closeChar = "}";
|
|
301
|
-
if (objectStart >= 0 && (arrayStart < 0 || objectStart < arrayStart)) {
|
|
302
|
-
start = objectStart;
|
|
303
|
-
openChar = "{";
|
|
304
|
-
closeChar = "}";
|
|
305
|
-
} else if (arrayStart >= 0) {
|
|
306
|
-
start = arrayStart;
|
|
307
|
-
openChar = "[";
|
|
308
|
-
closeChar = "]";
|
|
309
|
-
}
|
|
310
|
-
if (start < 0) return null;
|
|
311
|
-
let depth = 0;
|
|
312
|
-
let inString = false;
|
|
313
|
-
let escaped = false;
|
|
314
|
-
for (let i = start; i < content.length; i++) {
|
|
315
|
-
const ch = content[i];
|
|
316
|
-
if (inString) {
|
|
317
|
-
if (escaped) {
|
|
318
|
-
escaped = false;
|
|
319
|
-
continue;
|
|
320
|
-
}
|
|
321
|
-
if (ch === "\\") {
|
|
322
|
-
escaped = true;
|
|
323
|
-
continue;
|
|
324
|
-
}
|
|
325
|
-
if (ch === '"') {
|
|
326
|
-
inString = false;
|
|
327
|
-
}
|
|
328
|
-
continue;
|
|
329
|
-
}
|
|
330
|
-
if (ch === '"') {
|
|
331
|
-
inString = true;
|
|
332
|
-
continue;
|
|
333
|
-
}
|
|
334
|
-
if (ch === openChar) depth++;
|
|
335
|
-
if (ch === closeChar) {
|
|
336
|
-
depth--;
|
|
337
|
-
if (depth === 0) {
|
|
338
|
-
return content.slice(start, i + 1);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
return null;
|
|
343
|
-
}
|
|
344
|
-
/** 兼容常见 Function Calling 输出结构并转换为内部指令 */
|
|
345
|
-
adapt_common_tool_call(json) {
|
|
346
|
-
if (Array.isArray(json) && json.length > 0) {
|
|
347
|
-
return this.adapt_common_tool_call(json[0]);
|
|
348
|
-
}
|
|
349
|
-
if (typeof json?.name === "string") {
|
|
350
|
-
const params = this.normalize_params(json.params ?? json.arguments ?? {});
|
|
351
|
-
return {
|
|
352
|
-
command: "use_tool",
|
|
353
|
-
tool_name: json.name,
|
|
354
|
-
params,
|
|
355
|
-
reasoning: typeof json.reasoning === "string" ? json.reasoning : void 0
|
|
356
|
-
};
|
|
357
|
-
}
|
|
358
|
-
if (typeof json?.function?.name === "string") {
|
|
359
|
-
const params = this.normalize_params(json.function.arguments ?? {});
|
|
360
|
-
return {
|
|
361
|
-
command: "use_tool",
|
|
362
|
-
tool_name: json.function.name,
|
|
363
|
-
params,
|
|
364
|
-
reasoning: typeof json.reasoning === "string" ? json.reasoning : void 0
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
const first_tool_call = Array.isArray(json?.tool_calls) ? json.tool_calls[0] : null;
|
|
368
|
-
if (typeof first_tool_call?.function?.name === "string") {
|
|
369
|
-
const params = this.normalize_params(first_tool_call.function.arguments ?? {});
|
|
370
|
-
return {
|
|
371
|
-
command: "use_tool",
|
|
372
|
-
tool_name: first_tool_call.function.name,
|
|
373
|
-
params,
|
|
374
|
-
reasoning: typeof json.reasoning === "string" ? json.reasoning : void 0
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
return null;
|
|
378
|
-
}
|
|
379
|
-
normalize_params(params) {
|
|
380
|
-
if (params == null) return {};
|
|
381
|
-
if (typeof params === "string") {
|
|
382
|
-
try {
|
|
383
|
-
const parsed = LLM_Utils.parse_json(params);
|
|
384
|
-
return typeof parsed === "object" && parsed ? parsed : { value: parsed };
|
|
385
|
-
} catch {
|
|
386
|
-
return { value: params };
|
|
387
|
-
}
|
|
284
|
+
return null;
|
|
388
285
|
}
|
|
389
|
-
if (typeof params === "object") return params;
|
|
390
|
-
return { value: params };
|
|
391
286
|
}
|
|
392
287
|
async call_llm_with_retry(input) {
|
|
393
288
|
let last_error;
|
package/dist/index.d.cts
CHANGED
|
@@ -121,16 +121,6 @@ declare class Agent {
|
|
|
121
121
|
private normalize_input;
|
|
122
122
|
private parse_command;
|
|
123
123
|
private safe_extract_json;
|
|
124
|
-
/**
|
|
125
|
-
* 兼容部分模型返回的函数调用包裹标记,例如:
|
|
126
|
-
* <|FunctionCallBegin|> ... <|FunctionCallEnd|>
|
|
127
|
-
*/
|
|
128
|
-
private normalize_llm_output;
|
|
129
|
-
/** 从混杂文本中提取第一个 JSON 对象字符串 */
|
|
130
|
-
private find_first_json_block;
|
|
131
|
-
/** 兼容常见 Function Calling 输出结构并转换为内部指令 */
|
|
132
|
-
private adapt_common_tool_call;
|
|
133
|
-
private normalize_params;
|
|
134
124
|
private call_llm_with_retry;
|
|
135
125
|
}
|
|
136
126
|
|
package/dist/index.d.ts
CHANGED
|
@@ -121,16 +121,6 @@ declare class Agent {
|
|
|
121
121
|
private normalize_input;
|
|
122
122
|
private parse_command;
|
|
123
123
|
private safe_extract_json;
|
|
124
|
-
/**
|
|
125
|
-
* 兼容部分模型返回的函数调用包裹标记,例如:
|
|
126
|
-
* <|FunctionCallBegin|> ... <|FunctionCallEnd|>
|
|
127
|
-
*/
|
|
128
|
-
private normalize_llm_output;
|
|
129
|
-
/** 从混杂文本中提取第一个 JSON 对象字符串 */
|
|
130
|
-
private find_first_json_block;
|
|
131
|
-
/** 兼容常见 Function Calling 输出结构并转换为内部指令 */
|
|
132
|
-
private adapt_common_tool_call;
|
|
133
|
-
private normalize_params;
|
|
134
124
|
private call_llm_with_retry;
|
|
135
125
|
}
|
|
136
126
|
|
package/dist/index.js
CHANGED
|
@@ -123,6 +123,7 @@ var Agent = class {
|
|
|
123
123
|
};
|
|
124
124
|
const result = { result: "", use_tools: [] };
|
|
125
125
|
const tool_fail_count = {};
|
|
126
|
+
let format_fail_count = 0;
|
|
126
127
|
for (let turn = 0; turn < this.max_loop; turn++) {
|
|
127
128
|
let raw;
|
|
128
129
|
try {
|
|
@@ -133,8 +134,19 @@ var Agent = class {
|
|
|
133
134
|
}
|
|
134
135
|
const cmd = this.parse_command(raw);
|
|
135
136
|
if (!cmd) {
|
|
136
|
-
|
|
137
|
-
|
|
137
|
+
format_fail_count++;
|
|
138
|
+
history.push({
|
|
139
|
+
role: "user",
|
|
140
|
+
content: [{
|
|
141
|
+
type: "text",
|
|
142
|
+
text: '\u4F60\u7684\u4E0A\u4E00\u6761\u8F93\u51FA\u683C\u5F0F\u4E0D\u5408\u6CD5\u3002\u8BF7\u4E25\u683C\u53EA\u8FD4\u56DE\u4E00\u6761 JSON \u6307\u4EE4\uFF0C\u4E14\u5FC5\u987B\u4F7F\u7528 {"command":"end"|"use_tool"|"no_ability", ...} \u7ED3\u6784\uFF0C\u4E0D\u8981\u8F93\u51FA <|FunctionCallBegin|>\u3001tool_calls\u3001\u6570\u7EC4\u5305\u88F9\u6216\u4EFB\u4F55\u989D\u5916\u6587\u672C\u3002'
|
|
143
|
+
}]
|
|
144
|
+
});
|
|
145
|
+
if (format_fail_count >= 2) {
|
|
146
|
+
result.result = "LLM\u8F93\u51FA\u683C\u5F0F\u4E0D\u7B26\u5408\u534F\u8BAE\uFF0C\u8BF7\u68C0\u67E5\u7CFB\u7EDF\u63D0\u793A\u8BCD\u6216\u6A21\u578B\u914D\u7F6E";
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
continue;
|
|
138
150
|
}
|
|
139
151
|
if (cmd.command === "end") {
|
|
140
152
|
result.result = cmd.result;
|
|
@@ -232,133 +244,16 @@ ${tool_schemas}`;
|
|
|
232
244
|
return [...input];
|
|
233
245
|
}
|
|
234
246
|
parse_command(raw) {
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
if (typeof json.command === "string") {
|
|
239
|
-
return json;
|
|
240
|
-
}
|
|
241
|
-
return this.adapt_common_tool_call(json);
|
|
247
|
+
const json = this.safe_extract_json(raw);
|
|
248
|
+
if (!json || typeof json.command !== "string") return null;
|
|
249
|
+
return json;
|
|
242
250
|
}
|
|
243
251
|
safe_extract_json(content) {
|
|
244
252
|
try {
|
|
245
253
|
return LLM_Utils.extract_json(content);
|
|
246
254
|
} catch {
|
|
247
|
-
|
|
248
|
-
if (!json_candidate) return null;
|
|
249
|
-
try {
|
|
250
|
-
return LLM_Utils.parse_json(json_candidate);
|
|
251
|
-
} catch {
|
|
252
|
-
return null;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* 兼容部分模型返回的函数调用包裹标记,例如:
|
|
258
|
-
* <|FunctionCallBegin|> ... <|FunctionCallEnd|>
|
|
259
|
-
*/
|
|
260
|
-
normalize_llm_output(raw) {
|
|
261
|
-
return raw.replace(/<\|FunctionCallBegin\|>/gi, "").replace(/<\|FunctionCallEnd\|>/gi, "").replace(/<\|tool_call\|>/gi, "").replace(/<tool_call>/gi, "").replace(/<\/tool_call>/gi, "").trim();
|
|
262
|
-
}
|
|
263
|
-
/** 从混杂文本中提取第一个 JSON 对象字符串 */
|
|
264
|
-
find_first_json_block(content) {
|
|
265
|
-
const fenced = content.match(/```json\s*([\s\S]*?)\s*```/i);
|
|
266
|
-
if (fenced?.[1]) return fenced[1];
|
|
267
|
-
const objectStart = content.indexOf("{");
|
|
268
|
-
const arrayStart = content.indexOf("[");
|
|
269
|
-
let start = -1;
|
|
270
|
-
let openChar = "{";
|
|
271
|
-
let closeChar = "}";
|
|
272
|
-
if (objectStart >= 0 && (arrayStart < 0 || objectStart < arrayStart)) {
|
|
273
|
-
start = objectStart;
|
|
274
|
-
openChar = "{";
|
|
275
|
-
closeChar = "}";
|
|
276
|
-
} else if (arrayStart >= 0) {
|
|
277
|
-
start = arrayStart;
|
|
278
|
-
openChar = "[";
|
|
279
|
-
closeChar = "]";
|
|
280
|
-
}
|
|
281
|
-
if (start < 0) return null;
|
|
282
|
-
let depth = 0;
|
|
283
|
-
let inString = false;
|
|
284
|
-
let escaped = false;
|
|
285
|
-
for (let i = start; i < content.length; i++) {
|
|
286
|
-
const ch = content[i];
|
|
287
|
-
if (inString) {
|
|
288
|
-
if (escaped) {
|
|
289
|
-
escaped = false;
|
|
290
|
-
continue;
|
|
291
|
-
}
|
|
292
|
-
if (ch === "\\") {
|
|
293
|
-
escaped = true;
|
|
294
|
-
continue;
|
|
295
|
-
}
|
|
296
|
-
if (ch === '"') {
|
|
297
|
-
inString = false;
|
|
298
|
-
}
|
|
299
|
-
continue;
|
|
300
|
-
}
|
|
301
|
-
if (ch === '"') {
|
|
302
|
-
inString = true;
|
|
303
|
-
continue;
|
|
304
|
-
}
|
|
305
|
-
if (ch === openChar) depth++;
|
|
306
|
-
if (ch === closeChar) {
|
|
307
|
-
depth--;
|
|
308
|
-
if (depth === 0) {
|
|
309
|
-
return content.slice(start, i + 1);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
return null;
|
|
314
|
-
}
|
|
315
|
-
/** 兼容常见 Function Calling 输出结构并转换为内部指令 */
|
|
316
|
-
adapt_common_tool_call(json) {
|
|
317
|
-
if (Array.isArray(json) && json.length > 0) {
|
|
318
|
-
return this.adapt_common_tool_call(json[0]);
|
|
319
|
-
}
|
|
320
|
-
if (typeof json?.name === "string") {
|
|
321
|
-
const params = this.normalize_params(json.params ?? json.arguments ?? {});
|
|
322
|
-
return {
|
|
323
|
-
command: "use_tool",
|
|
324
|
-
tool_name: json.name,
|
|
325
|
-
params,
|
|
326
|
-
reasoning: typeof json.reasoning === "string" ? json.reasoning : void 0
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
if (typeof json?.function?.name === "string") {
|
|
330
|
-
const params = this.normalize_params(json.function.arguments ?? {});
|
|
331
|
-
return {
|
|
332
|
-
command: "use_tool",
|
|
333
|
-
tool_name: json.function.name,
|
|
334
|
-
params,
|
|
335
|
-
reasoning: typeof json.reasoning === "string" ? json.reasoning : void 0
|
|
336
|
-
};
|
|
337
|
-
}
|
|
338
|
-
const first_tool_call = Array.isArray(json?.tool_calls) ? json.tool_calls[0] : null;
|
|
339
|
-
if (typeof first_tool_call?.function?.name === "string") {
|
|
340
|
-
const params = this.normalize_params(first_tool_call.function.arguments ?? {});
|
|
341
|
-
return {
|
|
342
|
-
command: "use_tool",
|
|
343
|
-
tool_name: first_tool_call.function.name,
|
|
344
|
-
params,
|
|
345
|
-
reasoning: typeof json.reasoning === "string" ? json.reasoning : void 0
|
|
346
|
-
};
|
|
347
|
-
}
|
|
348
|
-
return null;
|
|
349
|
-
}
|
|
350
|
-
normalize_params(params) {
|
|
351
|
-
if (params == null) return {};
|
|
352
|
-
if (typeof params === "string") {
|
|
353
|
-
try {
|
|
354
|
-
const parsed = LLM_Utils.parse_json(params);
|
|
355
|
-
return typeof parsed === "object" && parsed ? parsed : { value: parsed };
|
|
356
|
-
} catch {
|
|
357
|
-
return { value: params };
|
|
358
|
-
}
|
|
255
|
+
return null;
|
|
359
256
|
}
|
|
360
|
-
if (typeof params === "object") return params;
|
|
361
|
-
return { value: params };
|
|
362
257
|
}
|
|
363
258
|
async call_llm_with_retry(input) {
|
|
364
259
|
let last_error;
|