dsh-plugin-thinking-guard 0.0.0-stage → 0.4.0
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/CHANGELOG.md +88 -0
- package/LICENSE +21 -0
- package/README.en.md +231 -0
- package/README.md +213 -2
- package/cordis.patch.yml +43 -0
- package/icon.svg +19 -0
- package/index.js +48 -0
- package/lib/guard.js +645 -0
- package/lib/lead-wrapper.js +209 -0
- package/lib/tool-markup.js +885 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +61 -4
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lead-wrapper guard: a text block wrapped whole in a tag the model made up.
|
|
3
|
+
*
|
|
4
|
+
* Besides `<thinking>`, the same relays let the model wrap a progress note in
|
|
5
|
+
* an invented tag, in any language:
|
|
6
|
+
*
|
|
7
|
+
* <概述>
|
|
8
|
+
* 用户想新建一个任务……我已经检查了重复项。现在开始创建。
|
|
9
|
+
* </概述>
|
|
10
|
+
* [tool call]
|
|
11
|
+
*
|
|
12
|
+
* The tag name carries no reliable meaning, so the decision rests on shape and
|
|
13
|
+
* on what follows the block:
|
|
14
|
+
* - the block opens with an unknown tag and that tag closes the whole block;
|
|
15
|
+
* - a tool call or another block follows -> it was a preamble: reasoning
|
|
16
|
+
* (dropped in `strip` mode);
|
|
17
|
+
* - the response ends there -> it is the answer: the tags are removed and the
|
|
18
|
+
* content stays visible text;
|
|
19
|
+
* - text follows the closing tag in the same block -> the tags are removed.
|
|
20
|
+
*
|
|
21
|
+
* HTML element names, capitalized JSX components, tags with attributes and
|
|
22
|
+
* anything not at the start of a block are left alone.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** Element names that legitimately open an answer written as HTML. */
|
|
26
|
+
const HTML = new Set([
|
|
27
|
+
'a', 'abbr', 'address', 'article', 'aside', 'audio', 'b', 'blockquote', 'body', 'br', 'button', 'canvas',
|
|
28
|
+
'caption', 'center', 'cite', 'code', 'col', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em',
|
|
29
|
+
'figcaption', 'figure', 'font', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hr',
|
|
30
|
+
'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'label', 'li', 'main', 'mark', 'nav', 'ol', 'p',
|
|
31
|
+
'picture', 'pre', 'q', 's', 'samp', 'script', 'section', 'select', 'small', 'span', 'strong', 'style', 'sub',
|
|
32
|
+
'summary', 'sup', 'svg', 'table', 'tbody', 'td', 'template', 'textarea', 'tfoot', 'th', 'thead', 'time',
|
|
33
|
+
'title', 'tr', 'u', 'ul', 'var', 'video',
|
|
34
|
+
])
|
|
35
|
+
|
|
36
|
+
const OPEN = /^\s{0,64}<([\p{L}_][\p{L}\p{N}_-]{0,23})>/u
|
|
37
|
+
const MAY_BECOME_OPEN = /^\s{0,64}(?:<(?:[\p{L}_][\p{L}\p{N}_-]{0,23})?)?$/u
|
|
38
|
+
/** A wrapper longer than this is released untouched. */
|
|
39
|
+
const MAX_WRAPPED = 8000
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {string} name - candidate tag name.
|
|
43
|
+
* @returns {boolean} whether the name can be an invented wrapper.
|
|
44
|
+
*/
|
|
45
|
+
function isWrapperName(name) {
|
|
46
|
+
if (/^[A-Z]/.test(name)) return false
|
|
47
|
+
return !HTML.has(name.toLowerCase())
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Rewrites text blocks that are wrapped whole in an invented tag.
|
|
52
|
+
*/
|
|
53
|
+
export class LeadWrapperGuard {
|
|
54
|
+
/**
|
|
55
|
+
* @param {object} [options] - configuration.
|
|
56
|
+
* @param {'split'|'strip'} [options.mode] - keep a preamble as reasoning, or drop it.
|
|
57
|
+
* @param {boolean} [options.debug] - log each rewrite.
|
|
58
|
+
*/
|
|
59
|
+
constructor(options = {}) {
|
|
60
|
+
this.mode = options.mode === 'strip' ? 'strip' : 'split'
|
|
61
|
+
this.debug = options.debug === true
|
|
62
|
+
/** @type {Map<number, any>} */
|
|
63
|
+
this.blocks = new Map()
|
|
64
|
+
/** A wrapped block waiting to learn what follows it. */
|
|
65
|
+
this.pending = null
|
|
66
|
+
/** Chunks held behind the pending block (usage). */
|
|
67
|
+
this.held = []
|
|
68
|
+
this.out = []
|
|
69
|
+
this.rewritten = 0
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Feed one chunk.
|
|
74
|
+
* @param {object} chunk - a StreamChunk.
|
|
75
|
+
* @returns {object[]} chunks to emit.
|
|
76
|
+
*/
|
|
77
|
+
push(chunk) {
|
|
78
|
+
this.out = []
|
|
79
|
+
if (this.pending !== null) {
|
|
80
|
+
if (chunk.type === 'usage') {
|
|
81
|
+
this.held.push(chunk)
|
|
82
|
+
return this.out
|
|
83
|
+
}
|
|
84
|
+
// Anything but the end of the response means the wrapper was a preamble.
|
|
85
|
+
this.resolvePending(chunk.type === 'finish' ? 'text' : 'reasoning')
|
|
86
|
+
}
|
|
87
|
+
this.route(chunk)
|
|
88
|
+
return this.out
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* End of stream.
|
|
93
|
+
* @returns {object[]} remaining chunks.
|
|
94
|
+
*/
|
|
95
|
+
flush() {
|
|
96
|
+
this.out = []
|
|
97
|
+
for (const state of this.blocks.values()) {
|
|
98
|
+
if (!state.done) this.endBlock(state, { type: 'block-end', index: state.index, block: { type: 'text', text: state.buffer } })
|
|
99
|
+
}
|
|
100
|
+
if (this.pending !== null) this.resolvePending('text')
|
|
101
|
+
return this.out
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
route(chunk) {
|
|
105
|
+
if (chunk.type === 'block-start' && chunk.blockType === 'text') {
|
|
106
|
+
this.blocks.set(chunk.index, { index: chunk.index, phase: 'lead', buffer: '', start: chunk, done: false, altered: false, emitted: '' })
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
if (chunk.type === 'text-delta') {
|
|
110
|
+
const state = this.blocks.get(chunk.index)
|
|
111
|
+
if (state === undefined || state.phase === 'pass') {
|
|
112
|
+
if (state !== undefined) state.emitted += chunk.text
|
|
113
|
+
this.out.push(chunk)
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
state.buffer += chunk.text
|
|
117
|
+
this.advance(state)
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
if (chunk.type === 'block-end') {
|
|
121
|
+
const state = this.blocks.get(chunk.index)
|
|
122
|
+
if (state !== undefined && !state.done) {
|
|
123
|
+
this.endBlock(state, chunk)
|
|
124
|
+
return
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
this.out.push(chunk)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Release a block as ordinary text from here on. */
|
|
131
|
+
release(state) {
|
|
132
|
+
state.phase = 'pass'
|
|
133
|
+
state.emitted = state.buffer
|
|
134
|
+
this.out.push(state.start)
|
|
135
|
+
if (state.buffer.length > 0) this.out.push({ type: 'text-delta', index: state.index, text: state.buffer })
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
advance(state) {
|
|
139
|
+
if (state.phase === 'lead') {
|
|
140
|
+
const open = OPEN.exec(state.buffer)
|
|
141
|
+
if (open !== null && isWrapperName(open[1])) {
|
|
142
|
+
state.phase = 'inside'
|
|
143
|
+
state.name = open[1]
|
|
144
|
+
state.openEnd = open[0].length
|
|
145
|
+
state.close = new RegExp(`</${open[1].replace(/[-]/g, '\\-')}\\s*>`, 'iu')
|
|
146
|
+
} else if (open !== null || !MAY_BECOME_OPEN.test(state.buffer)) {
|
|
147
|
+
this.release(state)
|
|
148
|
+
return
|
|
149
|
+
} else {
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (state.phase === 'inside') {
|
|
154
|
+
const close = state.close.exec(state.buffer.slice(state.openEnd))
|
|
155
|
+
if (close !== null) {
|
|
156
|
+
state.phase = 'after'
|
|
157
|
+
state.closeStart = state.openEnd + close.index
|
|
158
|
+
state.closeEnd = state.closeStart + close[0].length
|
|
159
|
+
} else if (state.buffer.length > MAX_WRAPPED) {
|
|
160
|
+
this.release(state)
|
|
161
|
+
return
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (state.phase === 'after' && state.buffer.slice(state.closeEnd).trim().length > 0) {
|
|
165
|
+
// Text after the wrapper: only remove the tags, from here on stream through.
|
|
166
|
+
const content = state.buffer.slice(state.openEnd, state.closeStart).replace(/^\s+/, '')
|
|
167
|
+
state.buffer = content + state.buffer.slice(state.closeEnd)
|
|
168
|
+
state.altered = true
|
|
169
|
+
this.note('unwrapped')
|
|
170
|
+
this.release(state)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
endBlock(state, chunk) {
|
|
175
|
+
state.done = true
|
|
176
|
+
if (state.phase === 'pass') {
|
|
177
|
+
// The assembler trusts block-end text over deltas: restate it after a rewrite.
|
|
178
|
+
this.out.push(state.altered ? { ...chunk, block: { type: 'text', text: state.emitted } } : chunk)
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
if (state.phase !== 'after') {
|
|
182
|
+
// Never closed, or never a wrapper: unchanged.
|
|
183
|
+
this.release(state)
|
|
184
|
+
this.out.push(chunk)
|
|
185
|
+
return
|
|
186
|
+
}
|
|
187
|
+
// Wrapped whole: what follows decides between preamble and answer.
|
|
188
|
+
this.pending = { index: state.index, content: state.buffer.slice(state.openEnd, state.closeStart).trim() }
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
resolvePending(kind) {
|
|
192
|
+
const { index, content } = this.pending
|
|
193
|
+
this.pending = null
|
|
194
|
+
const held = this.held
|
|
195
|
+
this.held = []
|
|
196
|
+
if (content.length > 0 && !(kind === 'reasoning' && this.mode === 'strip')) {
|
|
197
|
+
this.out.push({ type: 'block-start', index, blockType: kind })
|
|
198
|
+
this.out.push({ type: kind === 'reasoning' ? 'reasoning-delta' : 'text-delta', index, text: content })
|
|
199
|
+
this.out.push({ type: 'block-end', index, block: { type: kind, text: content } })
|
|
200
|
+
}
|
|
201
|
+
this.note(kind === 'reasoning' ? 'preamble' : 'answer')
|
|
202
|
+
this.out.push(...held)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
note(what) {
|
|
206
|
+
this.rewritten += 1
|
|
207
|
+
if (this.debug) console.warn(`[dsh-thinking-guard] rewrote a lead wrapper tag (${what})`)
|
|
208
|
+
}
|
|
209
|
+
}
|