@auipage/agent 0.1.0-alpha.3 → 0.1.0-alpha.5
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.d.ts +1 -1
- package/dist/index.js +89 -7
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '../types/index
|
|
1
|
+
export * from '../types/index'
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! @auipage/agent v0.1.0-alpha.
|
|
1
|
+
/*! @auipage/agent v0.1.0-alpha.5 */
|
|
2
2
|
(function (global, factory) {
|
|
3
3
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
4
4
|
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
@@ -291,7 +291,7 @@
|
|
|
291
291
|
}
|
|
292
292
|
|
|
293
293
|
// 解析 LLM 返回的推理内容(reasoning / reasoning_content)
|
|
294
|
-
// 将 <think>...</think> 形式的内容从
|
|
294
|
+
// 将 <think>...</think> 形式的内容从 reasoning 字段中剥离出来
|
|
295
295
|
function parseReasoning(reasoning, content) {
|
|
296
296
|
if (!/<\/think>/.test(reasoning)) return reasoning;
|
|
297
297
|
|
|
@@ -299,17 +299,83 @@
|
|
|
299
299
|
const thinkPart = parts[0].replace("<think>", "");
|
|
300
300
|
return {
|
|
301
301
|
reasoning: thinkPart,
|
|
302
|
-
content:
|
|
302
|
+
content: parts[1] + (content || "")
|
|
303
303
|
};
|
|
304
304
|
}
|
|
305
305
|
|
|
306
|
+
const OPEN = "<think>";
|
|
307
|
+
const CLOSE = "</think>";
|
|
308
|
+
|
|
309
|
+
// 计算文本尾部与标签前缀的最长匹配长度
|
|
310
|
+
// 用于标签可能被截断在 chunk 边界的情况(如收到 "</thi")
|
|
311
|
+
function partialTagLength(text, tag) {
|
|
312
|
+
const max = Math.min(tag.length - 1, text.length);
|
|
313
|
+
for (let len = max; len > 0; len--) {
|
|
314
|
+
if (text.endsWith(tag.slice(0, len))) return len;
|
|
315
|
+
}
|
|
316
|
+
return 0;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// 部分模型(如 MiniMax-M3)会把思考过程以 <think>...</think> 形式混在 content 中
|
|
320
|
+
// 这里把它剥离出来:思考片段归 thinkback,其余归正文
|
|
321
|
+
// 流式时标签可能分散在多个 chunk,依赖 state.inThink / state.thinkBuf 跨 chunk 记录状态
|
|
322
|
+
// 返回 { thinking, content }
|
|
323
|
+
function extractThink(content, state) {
|
|
324
|
+
const buf = (state.thinkBuf || "") + content;
|
|
325
|
+
state.thinkBuf = "";
|
|
326
|
+
|
|
327
|
+
// 正处于思考块内:寻找 </think>
|
|
328
|
+
if (state.inThink) {
|
|
329
|
+
const end = buf.indexOf(CLOSE);
|
|
330
|
+
if (end === -1) {
|
|
331
|
+
const hold = partialTagLength(buf, CLOSE);
|
|
332
|
+
state.thinkBuf = buf.slice(buf.length - hold);
|
|
333
|
+
return { thinking: buf.slice(0, buf.length - hold), content: "" };
|
|
334
|
+
}
|
|
335
|
+
state.inThink = false;
|
|
336
|
+
const rest = extractThink(buf.slice(end + CLOSE.length), state);
|
|
337
|
+
return { thinking: buf.slice(0, end) + rest.thinking, content: rest.content };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// 正常正文:寻找 <think>
|
|
341
|
+
const start = buf.indexOf(OPEN);
|
|
342
|
+
if (start === -1) {
|
|
343
|
+
const hold = partialTagLength(buf, OPEN);
|
|
344
|
+
state.thinkBuf = buf.slice(buf.length - hold);
|
|
345
|
+
return { thinking: "", content: buf.slice(0, buf.length - hold) };
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const end = buf.indexOf(CLOSE, start);
|
|
349
|
+
if (end !== -1) {
|
|
350
|
+
const rest = extractThink(buf.slice(end + CLOSE.length), state);
|
|
351
|
+
return {
|
|
352
|
+
thinking: buf.slice(start + OPEN.length, end) + rest.thinking,
|
|
353
|
+
content: buf.slice(0, start) + rest.content
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// 有 <think> 无 </think>:进入思考态
|
|
358
|
+
state.inThink = true;
|
|
359
|
+
const body = buf.slice(start + OPEN.length);
|
|
360
|
+
const hold = partialTagLength(body, CLOSE);
|
|
361
|
+
state.thinkBuf = body.slice(body.length - hold);
|
|
362
|
+
return { thinking: body.slice(0, body.length - hold), content: buf.slice(0, start) };
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// 流结束时调用:冲刷 extractThink 留下的截断残留,按当前所处模式归属输出
|
|
366
|
+
function flushThink(state) {
|
|
367
|
+
const buf = state.thinkBuf || "";
|
|
368
|
+
state.thinkBuf = "";
|
|
369
|
+
return state.inThink ? { thinking: buf, content: "" } : { thinking: "", content: buf };
|
|
370
|
+
}
|
|
371
|
+
|
|
306
372
|
// 处理单批 choices 数据:累加 content、拼装 tool_calls、回调 thinkback / logback
|
|
307
373
|
// 返回累积的 { content, tool_calls }
|
|
308
374
|
function handleChoices(choices, { logback, thinkback }, state) {
|
|
309
375
|
for (let i = 0; i < choices.length; i++) {
|
|
310
376
|
const delta = choices[i].delta;
|
|
311
377
|
|
|
312
|
-
//
|
|
378
|
+
// 推理内容(reasoning / reasoning_content 字段)
|
|
313
379
|
let reasoning = delta.reasoning || delta.reasoning_content;
|
|
314
380
|
if (reasoning) {
|
|
315
381
|
const parsed = parseReasoning(reasoning, delta.content);
|
|
@@ -320,10 +386,14 @@
|
|
|
320
386
|
if (thinkback) thinkback(reasoning);
|
|
321
387
|
}
|
|
322
388
|
|
|
323
|
-
//
|
|
389
|
+
// 正文内容:剥离混在 content 中的 <think>...</think> 思考片段
|
|
324
390
|
if (delta.content) {
|
|
325
|
-
|
|
326
|
-
|
|
391
|
+
const extracted = extractThink(delta.content, state);
|
|
392
|
+
if (extracted.thinking && thinkback) thinkback(extracted.thinking);
|
|
393
|
+
if (extracted.content) {
|
|
394
|
+
if (logback) logback(extracted.content);
|
|
395
|
+
state.content += extracted.content;
|
|
396
|
+
}
|
|
327
397
|
}
|
|
328
398
|
|
|
329
399
|
// 工具调用
|
|
@@ -339,6 +409,16 @@
|
|
|
339
409
|
}
|
|
340
410
|
}
|
|
341
411
|
|
|
412
|
+
// 流结束时调用:把 <think> 标签截断残留冲刷到对应回调
|
|
413
|
+
function flushThinkBuffer(state, { logback, thinkback }) {
|
|
414
|
+
const flushed = flushThink(state);
|
|
415
|
+
if (flushed.thinking && thinkback) thinkback(flushed.thinking);
|
|
416
|
+
if (flushed.content) {
|
|
417
|
+
if (logback) logback(flushed.content);
|
|
418
|
+
state.content += flushed.content;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
342
422
|
function completions(params, logback, thinkback) {
|
|
343
423
|
return new Promise((resolve, reject) => {
|
|
344
424
|
|
|
@@ -387,6 +467,8 @@
|
|
|
387
467
|
state
|
|
388
468
|
);
|
|
389
469
|
}
|
|
470
|
+
// 冲刷 <think> 标签截断残留
|
|
471
|
+
flushThinkBuffer(state, callbacks);
|
|
390
472
|
resolve({ content: state.content, tool_calls: state.tool_calls });
|
|
391
473
|
});
|
|
392
474
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@auipage/agent",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.5",
|
|
4
4
|
"description": "@auipage/agent",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/handless-ui/auipage#readme",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@auipage/model": "^0.1.0-alpha.
|
|
34
|
-
"@auipage/types": "^0.1.0-alpha.
|
|
33
|
+
"@auipage/model": "^0.1.0-alpha.5",
|
|
34
|
+
"@auipage/types": "^0.1.0-alpha.5",
|
|
35
35
|
"oipage": "^2.1.0"
|
|
36
36
|
}
|
|
37
37
|
}
|