p-api-agent 0.0.6 → 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 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
- result.result = raw;
166
- return result;
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,117 +273,16 @@ ${tool_schemas}`;
261
273
  return [...input];
262
274
  }
263
275
  parse_command(raw) {
264
- const normalized = this.normalize_llm_output(raw);
265
- const json = this.safe_extract_json(normalized);
266
- if (!json) return null;
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
- const json_candidate = this.find_first_json_block(content);
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 start = content.indexOf("{");
297
- if (start < 0) return null;
298
- let depth = 0;
299
- let inString = false;
300
- let escaped = false;
301
- for (let i = start; i < content.length; i++) {
302
- const ch = content[i];
303
- if (inString) {
304
- if (escaped) {
305
- escaped = false;
306
- continue;
307
- }
308
- if (ch === "\\") {
309
- escaped = true;
310
- continue;
311
- }
312
- if (ch === '"') {
313
- inString = false;
314
- }
315
- continue;
316
- }
317
- if (ch === '"') {
318
- inString = true;
319
- continue;
320
- }
321
- if (ch === "{") depth++;
322
- if (ch === "}") {
323
- depth--;
324
- if (depth === 0) {
325
- return content.slice(start, i + 1);
326
- }
327
- }
328
- }
329
- return null;
330
- }
331
- /** 兼容常见 Function Calling 输出结构并转换为内部指令 */
332
- adapt_common_tool_call(json) {
333
- if (typeof json?.name === "string") {
334
- const params = this.normalize_params(json.params ?? json.arguments ?? {});
335
- return {
336
- command: "use_tool",
337
- tool_name: json.name,
338
- params,
339
- reasoning: typeof json.reasoning === "string" ? json.reasoning : void 0
340
- };
341
- }
342
- if (typeof json?.function?.name === "string") {
343
- const params = this.normalize_params(json.function.arguments ?? {});
344
- return {
345
- command: "use_tool",
346
- tool_name: json.function.name,
347
- params,
348
- reasoning: typeof json.reasoning === "string" ? json.reasoning : void 0
349
- };
350
- }
351
- const first_tool_call = Array.isArray(json?.tool_calls) ? json.tool_calls[0] : null;
352
- if (typeof first_tool_call?.function?.name === "string") {
353
- const params = this.normalize_params(first_tool_call.function.arguments ?? {});
354
- return {
355
- command: "use_tool",
356
- tool_name: first_tool_call.function.name,
357
- params,
358
- reasoning: typeof json.reasoning === "string" ? json.reasoning : void 0
359
- };
360
- }
361
- return null;
362
- }
363
- normalize_params(params) {
364
- if (params == null) return {};
365
- if (typeof params === "string") {
366
- try {
367
- const parsed = LLM_Utils.parse_json(params);
368
- return typeof parsed === "object" && parsed ? parsed : { value: parsed };
369
- } catch {
370
- return { value: params };
371
- }
284
+ return null;
372
285
  }
373
- if (typeof params === "object") return params;
374
- return { value: params };
375
286
  }
376
287
  async call_llm_with_retry(input) {
377
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
- result.result = raw;
137
- return result;
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,117 +244,16 @@ ${tool_schemas}`;
232
244
  return [...input];
233
245
  }
234
246
  parse_command(raw) {
235
- const normalized = this.normalize_llm_output(raw);
236
- const json = this.safe_extract_json(normalized);
237
- if (!json) return null;
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
- const json_candidate = this.find_first_json_block(content);
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 start = content.indexOf("{");
268
- if (start < 0) return null;
269
- let depth = 0;
270
- let inString = false;
271
- let escaped = false;
272
- for (let i = start; i < content.length; i++) {
273
- const ch = content[i];
274
- if (inString) {
275
- if (escaped) {
276
- escaped = false;
277
- continue;
278
- }
279
- if (ch === "\\") {
280
- escaped = true;
281
- continue;
282
- }
283
- if (ch === '"') {
284
- inString = false;
285
- }
286
- continue;
287
- }
288
- if (ch === '"') {
289
- inString = true;
290
- continue;
291
- }
292
- if (ch === "{") depth++;
293
- if (ch === "}") {
294
- depth--;
295
- if (depth === 0) {
296
- return content.slice(start, i + 1);
297
- }
298
- }
299
- }
300
- return null;
301
- }
302
- /** 兼容常见 Function Calling 输出结构并转换为内部指令 */
303
- adapt_common_tool_call(json) {
304
- if (typeof json?.name === "string") {
305
- const params = this.normalize_params(json.params ?? json.arguments ?? {});
306
- return {
307
- command: "use_tool",
308
- tool_name: json.name,
309
- params,
310
- reasoning: typeof json.reasoning === "string" ? json.reasoning : void 0
311
- };
312
- }
313
- if (typeof json?.function?.name === "string") {
314
- const params = this.normalize_params(json.function.arguments ?? {});
315
- return {
316
- command: "use_tool",
317
- tool_name: json.function.name,
318
- params,
319
- reasoning: typeof json.reasoning === "string" ? json.reasoning : void 0
320
- };
321
- }
322
- const first_tool_call = Array.isArray(json?.tool_calls) ? json.tool_calls[0] : null;
323
- if (typeof first_tool_call?.function?.name === "string") {
324
- const params = this.normalize_params(first_tool_call.function.arguments ?? {});
325
- return {
326
- command: "use_tool",
327
- tool_name: first_tool_call.function.name,
328
- params,
329
- reasoning: typeof json.reasoning === "string" ? json.reasoning : void 0
330
- };
331
- }
332
- return null;
333
- }
334
- normalize_params(params) {
335
- if (params == null) return {};
336
- if (typeof params === "string") {
337
- try {
338
- const parsed = LLM_Utils.parse_json(params);
339
- return typeof parsed === "object" && parsed ? parsed : { value: parsed };
340
- } catch {
341
- return { value: params };
342
- }
255
+ return null;
343
256
  }
344
- if (typeof params === "object") return params;
345
- return { value: params };
346
257
  }
347
258
  async call_llm_with_retry(input) {
348
259
  let last_error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "p-api-agent",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "main": "./dist/index.cjs",
5
5
  "module": "./dist/index.mjs",
6
6
  "files": [