File size: 5,066 Bytes
76b5743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import * as d3 from 'd3';
import type { SimpleEventHandler } from '../../shared/core/SimpleEventHandler';
import { GLTR_Mode, GLTR_Text_Box } from '../../shared/vis/GLTR_Text_Box';
import { CHAT_SURPRISAL_COLOR_MAP_MAX } from '../../shared/cross/SurprisalColorConfig';
import { buildCompletionDisplayResult } from './buildCompletionDisplayResult';
import type { ChatDisplaySegment } from './chatSegments';

/** 展示用:前块为 output 且末尾无 \\n 时,去掉 input 前导 \\n(块边界已承担该换行,避免双空行)。 */
function inputSegmentTextForDisplay(
    text: string,
    prev: ChatDisplaySegment | undefined
): string {
    if (
        prev?.kind === 'output' &&
        text.startsWith('\n') &&
        !prev.text.endsWith('\n')
    ) {
        return text.slice(1);
    }
    return text;
}

const GLTR_OPTIONS = {
    gltrMode: GLTR_Mode.fract_p,
    enableRenderAnimation: false,
    enableMinimap: false,
    overlayTokenRenderStyle: 'classic' as const,
    overlayIgnoreGlobalInfoDensityDisable: true,
    surprisalColorMax: CHAT_SURPRISAL_COLOR_MAP_MAX,
};

export class ChatTurnsView {
    private readonly container: d3.Selection<HTMLElement, unknown, null, undefined>;
    private readonly eventHandler: SimpleEventHandler;
    private gltrBoxes: GLTR_Text_Box[] = [];
    private lastSegments: ChatDisplaySegment[] = [];
    /** 最近点击的 output 段索引;-1 表示默认最后一轮 */
    private activeOutputIndex = -1;

    constructor(
        container: HTMLElement,
        eventHandler: SimpleEventHandler
    ) {
        this.container = d3.select(container);
        this.eventHandler = eventHandler;
    }

    clear(): void {
        for (const box of this.gltrBoxes) {
            box.destroy?.();
        }
        this.gltrBoxes = [];
        this.activeOutputIndex = -1;
        this.container.selectAll('*').remove();
    }

    rerender(): void {
        if (this.lastSegments.length > 0) {
            this.render(this.lastSegments);
        }
    }

    private resolvedOutputIndex(): number {
        if (this.activeOutputIndex >= 0 && this.activeOutputIndex < this.gltrBoxes.length) {
            return this.activeOutputIndex;
        }
        return Math.max(0, this.gltrBoxes.length - 1);
    }

    getActiveAnalyzeResult() {
        const box = this.gltrBoxes[this.resolvedOutputIndex()];
        return box?.getCurrentAnalyzeResult() ?? null;
    }

    getPromptPrefixForSidebar(): string {
        const outIdx = this.resolvedOutputIndex();
        let outputCount = 0;
        let legacyPrefix = '';
        for (const seg of this.lastSegments) {
            if (seg.kind === 'output') {
                if (outputCount === outIdx) {
                    return seg.promptUsed ?? legacyPrefix;
                }
                outputCount++;
            }
            legacyPrefix += seg.text;
        }
        return '';
    }

    getFullTextForCopy(): string {
        return this.lastSegments.map((s) => s.text).join('');
    }

    render(segments: ChatDisplaySegment[]): void {
        this.lastSegments = segments;
        const prevActive = this.activeOutputIndex;
        this.clear();
        let outputIndex = 0;
        let prev: ChatDisplaySegment | undefined;
        for (const seg of segments) {
            if (seg.kind === 'input') {
                const block = this.container
                    .append('div')
                    .attr('class', 'chat-segment chat-segment-input');
                block
                    .append('pre')
                    .attr(
                        'class',
                        seg.pending
                            ? 'chat-segment-input-text tool-calling-pending-text'
                            : 'chat-segment-input-text',
                    )
                    .text(inputSegmentTextForDisplay(seg.text, prev));
            } else {
                const block = this.container
                    .append('div')
                    .attr('class', 'chat-segment chat-segment-output');
                const outHost = block.append('div').attr('class', 'chat-segment-output-host');
                const box = new GLTR_Text_Box(outHost, this.eventHandler);
                box.updateOptions(GLTR_OPTIONS, true);
                const display = buildCompletionDisplayResult(
                    seg.text,
                    seg.modelName,
                    seg.response.info_radar?.bpe_strings ?? null
                );
                box.update(display);
                const capturedIndex = outputIndex;
                block.node()?.addEventListener(
                    'click',
                    () => {
                        this.activeOutputIndex = capturedIndex;
                    },
                    true
                );
                if (capturedIndex === prevActive) {
                    this.activeOutputIndex = capturedIndex;
                }
                this.gltrBoxes.push(box);
                outputIndex++;
            }
            prev = seg;
        }
    }

}