@auipage/model 0.1.0-alpha.4 → 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.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! @auipage/model v0.1.0-alpha.4 */
1
+ /*! @auipage/model 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> 形式的内容从 delta.content 中剥离出来
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: thinkPart[1] + (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
- if (logback) logback(delta.content);
326
- state.content += delta.content;
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/model",
3
- "version": "0.1.0-alpha.4",
3
+ "version": "0.1.0-alpha.5",
4
4
  "description": "@auipage/model",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "homepage": "https://github.com/handless-ui/auipage#readme",
32
32
  "dependencies": {
33
- "@auipage/fetch": "^0.1.0-alpha.4",
34
- "@auipage/types": "^0.1.0-alpha.4"
33
+ "@auipage/fetch": "^0.1.0-alpha.5",
34
+ "@auipage/types": "^0.1.0-alpha.5"
35
35
  }
36
36
  }
@@ -1,6 +1,6 @@
1
1
  import { fetch } from "@auipage/fetch/src/index.js";
2
2
  import parseString from "./parseString.js";
3
- import handleChoices from "./streamHandler.js";
3
+ import handleChoices, { flushThinkBuffer } from "./streamHandler.js";
4
4
 
5
5
  export default function completions(params, logback, thinkback) {
6
6
  return new Promise((resolve, reject) => {
@@ -50,6 +50,8 @@ export default function completions(params, logback, thinkback) {
50
50
  state
51
51
  );
52
52
  }
53
+ // 冲刷 <think> 标签截断残留
54
+ flushThinkBuffer(state, callbacks);
53
55
  resolve({ content: state.content, tool_calls: state.tool_calls });
54
56
  });
55
57
 
package/src/reasoning.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // 解析 LLM 返回的推理内容(reasoning / reasoning_content)
2
- // 将 <think>...</think> 形式的内容从 delta.content 中剥离出来
2
+ // 将 <think>...</think> 形式的内容从 reasoning 字段中剥离出来
3
3
  export default function parseReasoning(reasoning, content) {
4
4
  if (!/<\/think>/.test(reasoning)) return reasoning;
5
5
 
@@ -7,6 +7,72 @@ export default function parseReasoning(reasoning, content) {
7
7
  const thinkPart = parts[0].replace("<think>", "");
8
8
  return {
9
9
  reasoning: thinkPart,
10
- content: thinkPart[1] + (content || "")
10
+ content: parts[1] + (content || "")
11
11
  };
12
12
  }
13
+
14
+ const OPEN = "<think>";
15
+ const CLOSE = "</think>";
16
+
17
+ // 计算文本尾部与标签前缀的最长匹配长度
18
+ // 用于标签可能被截断在 chunk 边界的情况(如收到 "</thi")
19
+ function partialTagLength(text, tag) {
20
+ const max = Math.min(tag.length - 1, text.length);
21
+ for (let len = max; len > 0; len--) {
22
+ if (text.endsWith(tag.slice(0, len))) return len;
23
+ }
24
+ return 0;
25
+ }
26
+
27
+ // 部分模型(如 MiniMax-M3)会把思考过程以 <think>...</think> 形式混在 content 中
28
+ // 这里把它剥离出来:思考片段归 thinkback,其余归正文
29
+ // 流式时标签可能分散在多个 chunk,依赖 state.inThink / state.thinkBuf 跨 chunk 记录状态
30
+ // 返回 { thinking, content }
31
+ export function extractThink(content, state) {
32
+ const buf = (state.thinkBuf || "") + content;
33
+ state.thinkBuf = "";
34
+
35
+ // 正处于思考块内:寻找 </think>
36
+ if (state.inThink) {
37
+ const end = buf.indexOf(CLOSE);
38
+ if (end === -1) {
39
+ const hold = partialTagLength(buf, CLOSE);
40
+ state.thinkBuf = buf.slice(buf.length - hold);
41
+ return { thinking: buf.slice(0, buf.length - hold), content: "" };
42
+ }
43
+ state.inThink = false;
44
+ const rest = extractThink(buf.slice(end + CLOSE.length), state);
45
+ return { thinking: buf.slice(0, end) + rest.thinking, content: rest.content };
46
+ }
47
+
48
+ // 正常正文:寻找 <think>
49
+ const start = buf.indexOf(OPEN);
50
+ if (start === -1) {
51
+ const hold = partialTagLength(buf, OPEN);
52
+ state.thinkBuf = buf.slice(buf.length - hold);
53
+ return { thinking: "", content: buf.slice(0, buf.length - hold) };
54
+ }
55
+
56
+ const end = buf.indexOf(CLOSE, start);
57
+ if (end !== -1) {
58
+ const rest = extractThink(buf.slice(end + CLOSE.length), state);
59
+ return {
60
+ thinking: buf.slice(start + OPEN.length, end) + rest.thinking,
61
+ content: buf.slice(0, start) + rest.content
62
+ };
63
+ }
64
+
65
+ // 有 <think> 无 </think>:进入思考态
66
+ state.inThink = true;
67
+ const body = buf.slice(start + OPEN.length);
68
+ const hold = partialTagLength(body, CLOSE);
69
+ state.thinkBuf = body.slice(body.length - hold);
70
+ return { thinking: body.slice(0, body.length - hold), content: buf.slice(0, start) };
71
+ }
72
+
73
+ // 流结束时调用:冲刷 extractThink 留下的截断残留,按当前所处模式归属输出
74
+ export function flushThink(state) {
75
+ const buf = state.thinkBuf || "";
76
+ state.thinkBuf = "";
77
+ return state.inThink ? { thinking: buf, content: "" } : { thinking: "", content: buf };
78
+ }
@@ -1,4 +1,4 @@
1
- import parseReasoning from "./reasoning.js";
1
+ import parseReasoning, { extractThink, flushThink } from "./reasoning.js";
2
2
 
3
3
  // 处理单批 choices 数据:累加 content、拼装 tool_calls、回调 thinkback / logback
4
4
  // 返回累积的 { content, tool_calls }
@@ -6,7 +6,7 @@ export default function handleChoices(choices, { logback, thinkback }, state) {
6
6
  for (let i = 0; i < choices.length; i++) {
7
7
  const delta = choices[i].delta;
8
8
 
9
- // 推理内容
9
+ // 推理内容(reasoning / reasoning_content 字段)
10
10
  let reasoning = delta.reasoning || delta.reasoning_content;
11
11
  if (reasoning) {
12
12
  const parsed = parseReasoning(reasoning, delta.content);
@@ -17,10 +17,14 @@ export default function handleChoices(choices, { logback, thinkback }, state) {
17
17
  if (thinkback) thinkback(reasoning);
18
18
  }
19
19
 
20
- // 正文内容
20
+ // 正文内容:剥离混在 content 中的 <think>...</think> 思考片段
21
21
  if (delta.content) {
22
- if (logback) logback(delta.content);
23
- state.content += delta.content;
22
+ const extracted = extractThink(delta.content, state);
23
+ if (extracted.thinking && thinkback) thinkback(extracted.thinking);
24
+ if (extracted.content) {
25
+ if (logback) logback(extracted.content);
26
+ state.content += extracted.content;
27
+ }
24
28
  }
25
29
 
26
30
  // 工具调用
@@ -35,3 +39,13 @@ export default function handleChoices(choices, { logback, thinkback }, state) {
35
39
  }
36
40
  }
37
41
  }
42
+
43
+ // 流结束时调用:把 <think> 标签截断残留冲刷到对应回调
44
+ export function flushThinkBuffer(state, { logback, thinkback }) {
45
+ const flushed = flushThink(state);
46
+ if (flushed.thinking && thinkback) thinkback(flushed.thinking);
47
+ if (flushed.content) {
48
+ if (logback) logback(flushed.content);
49
+ state.content += flushed.content;
50
+ }
51
+ }