(turns: ContextTurn[], userQuestion: string)
| 555 | }; |
| 556 | |
| 557 | const buildQuestionWithContext = (turns: ContextTurn[], userQuestion: string): { finalQuestion: string; dropped: number } => { |
| 558 | if (!turns.length) return { finalQuestion: userQuestion, dropped: 0 }; |
| 559 | |
| 560 | let dropped = 0; |
| 561 | const lines: string[] = ["你正在延续一个多轮问答。以下是最近对话上下文,请只把它们当作参考:"]; |
| 562 | |
| 563 | for (let i = 0; i < turns.length; i++) { |
| 564 | const t = turns[i]; |
| 565 | const q = truncateBySections(t.q, 4000).text; |
| 566 | const a = truncateBySections(t.a, 12000).text; |
| 567 | lines.push(`Q${i + 1}:\n${q}`); |
| 568 | lines.push(`A${i + 1}:\n${a}`); |
| 569 | } |
| 570 | |
| 571 | lines.push(`当前问题:\n${userQuestion}`); |
| 572 | let finalQuestion = lines.join("\n\n"); |
| 573 | if (finalQuestion.length <= MAX_DEEPWIKI_LEN) { |
| 574 | return { finalQuestion, dropped }; |
| 575 | } |
| 576 | |
| 577 | const copy = turns.slice(); |
| 578 | while (copy.length > 0) { |
| 579 | copy.shift(); |
| 580 | dropped += 1; |
| 581 | const trimmedLines: string[] = ["你正在延续一个多轮问答。以下是最近对话上下文,请只把它们当作参考:"]; |
| 582 | for (let i = 0; i < copy.length; i++) { |
| 583 | const t = copy[i]; |
| 584 | const q = truncateBySections(t.q, 4000).text; |
| 585 | const a = truncateBySections(t.a, 12000).text; |
| 586 | trimmedLines.push(`Q${i + 1}:\n${q}`); |
| 587 | trimmedLines.push(`A${i + 1}:\n${a}`); |
| 588 | } |
| 589 | trimmedLines.push(`当前问题:\n${userQuestion}`); |
| 590 | finalQuestion = trimmedLines.join("\n\n"); |
| 591 | if (finalQuestion.length <= MAX_DEEPWIKI_LEN) { |
| 592 | return { finalQuestion, dropped }; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | const truncatedQuestion = truncateBySections(userQuestion, MAX_DEEPWIKI_LEN - 20).text; |
| 597 | return { finalQuestion: `当前问题:\n${truncatedQuestion}`, dropped: turns.length }; |
| 598 | }; |
| 599 | |
| 600 | const buildQAHtml = (headerLines: string[], question: string, answerMarkdown: string, collapseSafe: boolean): string => { |
| 601 | const header = headerLines.filter(Boolean).join("\n"); |
no test coverage detected