dsh-rwkv-plugin 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +50 -2
- package/cordis.patch.yml +59 -0
- package/lib/client.js +3148 -0
- package/lib/client.js.map +7 -0
- package/lib/index.js +6294 -0
- package/lib/index.js.map +7 -0
- package/lib/line-edit.js +1423 -0
- package/lib/line-edit.js.map +7 -0
- package/lib/todo.js +610 -0
- package/lib/todo.js.map +7 -0
- package/package.json +72 -4
- package/scripts/build.mjs +77 -0
- package/scripts/headless.ts +279 -0
- package/scripts/link-dsh.sh +31 -0
- package/scripts/relay-e2e.mjs +157 -0
- package/scripts/smoke.ts +116 -0
- package/scripts/state.ts +39 -0
- package/scripts/tokenizer-parity.ts +107 -0
- package/scripts/trace-stats.ts +33 -0
- package/src/adapter.ts +388 -0
- package/src/backend/generator.ts +221 -0
- package/src/backend/irwkv.ts +117 -0
- package/src/backend/lightning.ts +228 -0
- package/src/backend/stream.ts +97 -0
- package/src/client/AdapterPicker.tsx +264 -0
- package/src/client/DeveloperPanel.tsx +403 -0
- package/src/client/FoldNode.tsx +82 -0
- package/src/client/ModelCard.tsx +230 -0
- package/src/client/RwkvSettings.tsx +265 -0
- package/src/client/Segment.tsx +15 -0
- package/src/client/StatePicker.tsx +248 -0
- package/src/client/TraceFrames.tsx +462 -0
- package/src/client/TraceView.tsx +77 -0
- package/src/client/WirePanel.tsx +566 -0
- package/src/client/fields.tsx +362 -0
- package/src/client/index.tsx +109 -0
- package/src/client/styles.ts +277 -0
- package/src/client/traces-api.ts +222 -0
- package/src/client/url.ts +20 -0
- package/src/core/agent.ts +586 -0
- package/src/core/defaults.ts +51 -0
- package/src/core/dsh-prompt.ts +32 -0
- package/src/core/edit-modes/index.ts +108 -0
- package/src/core/edit-modes/line-edit.ts +793 -0
- package/src/core/edit-modes/literal.ts +26 -0
- package/src/core/edit-modes/shared.ts +213 -0
- package/src/core/edit-modes/tolerant.ts +558 -0
- package/src/core/fold-pairs.ts +407 -0
- package/src/core/fold-window.ts +563 -0
- package/src/core/line-edit.ts +33 -0
- package/src/core/parse.ts +290 -0
- package/src/core/policy.ts +500 -0
- package/src/core/prompt.ts +220 -0
- package/src/core/relay.ts +400 -0
- package/src/core/step-gates.ts +371 -0
- package/src/core/time-zone.ts +32 -0
- package/src/core/todo-state.ts +227 -0
- package/src/core/tolerant-edit.ts +14 -0
- package/src/core/tool-mappings.ts +209 -0
- package/src/core/tool-repairs.ts +255 -0
- package/src/core/tools.ts +162 -0
- package/src/core/worklist.ts +606 -0
- package/src/dsh-host.ts +48 -0
- package/src/edit-modes/folded-read-guard.ts +32 -0
- package/src/edit-modes/index.ts +26 -0
- package/src/edit-modes/line-edit.ts +154 -0
- package/src/edit-modes/literal.ts +9 -0
- package/src/edit-modes/observations.ts +234 -0
- package/src/edit-modes/syntax-check.ts +226 -0
- package/src/edit-modes/tolerant.ts +232 -0
- package/src/folded-read-guard.ts +6 -0
- package/src/history.ts +163 -0
- package/src/index.ts +287 -0
- package/src/line-edit.ts +15 -0
- package/src/live-wire.ts +133 -0
- package/src/paths.ts +91 -0
- package/src/relay-store.ts +59 -0
- package/src/result-budget.ts +225 -0
- package/src/routes.ts +324 -0
- package/src/session-setup.ts +150 -0
- package/src/syntax-check.ts +2 -0
- package/src/todo.ts +328 -0
- package/src/tokenizer.ts +48 -0
- package/src/tolerant-edit.ts +2 -0
- package/src/trace-stats.ts +225 -0
- package/src/traces.ts +289 -0
- package/vendor/rwkv-tokenizer/NOTICE.md +26 -0
- package/vendor/rwkv-tokenizer/package.json +5 -0
- package/vendor/rwkv-tokenizer/rwkv_tokenizer.d.ts +11 -0
- package/vendor/rwkv-tokenizer/rwkv_tokenizer.js +280 -0
- package/vendor/rwkv-tokenizer/rwkv_tokenizer_bg.wasm +0 -0
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
// Action parser with the RWKV-Agent repairs, ported from internal/agent
|
|
2
|
+
// (protocol_g1.go Parse, protocol_g1_experiments.go recover-preamble and
|
|
3
|
+
// normalizeG1CallPayload, wire/framing.go).
|
|
4
|
+
|
|
5
|
+
import { EXIT_TOOL_NAME, type Args } from './tools.ts'
|
|
6
|
+
|
|
7
|
+
export type FinishReason = 'stop' | 'length' | 'cancelled' | 'unknown'
|
|
8
|
+
|
|
9
|
+
export type ParseErrorKind = 'unclosed-think' | 'length' | 'decode' | 'shape' | 'empty'
|
|
10
|
+
|
|
11
|
+
export type Action =
|
|
12
|
+
| { readonly type: 'tool'; readonly name: string; readonly args: Args; readonly repairs: readonly string[] }
|
|
13
|
+
| { readonly type: 'exit'; readonly answer: string; readonly repairs: readonly string[] }
|
|
14
|
+
| { readonly type: 'final'; readonly text: string }
|
|
15
|
+
| { readonly type: 'error'; readonly kind: ParseErrorKind; readonly message: string }
|
|
16
|
+
|
|
17
|
+
const LEADING_THINK = /^\s*(?:<think>[\s\S]*?<\/think>\s*)+/
|
|
18
|
+
|
|
19
|
+
/** Removes complete leading think blocks; an unterminated block stays for the caller to judge. */
|
|
20
|
+
export function stripLeadingThinkBlocks(text: string): string {
|
|
21
|
+
const match = LEADING_THINK.exec(text)
|
|
22
|
+
return match ? text.slice(match[0].length).trim() : text
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Drops the lone ">" that closes a withheld think prefix when a tool envelope follows. */
|
|
26
|
+
export function trimWithheldOpening(text: string): string {
|
|
27
|
+
if (!text.startsWith('>')) return text
|
|
28
|
+
const rest = text.slice(1).trim()
|
|
29
|
+
return rest.startsWith('<tool_call') ? rest : text
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Reasoning text of a leading think block, open or closed. */
|
|
33
|
+
export function extractThinking(text: string): string {
|
|
34
|
+
const trimmed = text.trimStart()
|
|
35
|
+
if (!trimmed.startsWith('<think>')) return ''
|
|
36
|
+
const end = trimmed.indexOf('</think>')
|
|
37
|
+
return (end < 0 ? trimmed.slice(7) : trimmed.slice(7, end)).trim()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function envelopeContent(candidate: string, open: string, close: string): { content: string; closed: boolean } {
|
|
41
|
+
const content = candidate.slice(open.length)
|
|
42
|
+
if (content.trimEnd().endsWith(close)) {
|
|
43
|
+
return { content: content.trimEnd().slice(0, -close.length).trim(), closed: true }
|
|
44
|
+
}
|
|
45
|
+
return { content: content.trim(), closed: false }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
49
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function tryParse(text: string): unknown {
|
|
53
|
+
try {
|
|
54
|
+
return JSON.parse(text)
|
|
55
|
+
} catch {
|
|
56
|
+
return undefined
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Deliberately narrow: extra closing braces after a complete object only. */
|
|
61
|
+
export function repairExtraBraces(value: string): string {
|
|
62
|
+
let trimmed = value.trim()
|
|
63
|
+
while (trimmed.endsWith('}')) {
|
|
64
|
+
trimmed = trimmed.slice(0, -1).trim()
|
|
65
|
+
if (tryParse(trimmed) !== undefined) return trimmed
|
|
66
|
+
}
|
|
67
|
+
return value
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface Call {
|
|
71
|
+
name: string
|
|
72
|
+
args: unknown
|
|
73
|
+
repairs: string[]
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Decodes one call payload, accepting the near-misses G1 checkpoints emit:
|
|
78
|
+
* stray closing braces, the OpenAI {"type":"function","function":{...}}
|
|
79
|
+
* wrapper, stringified arguments, key aliases, arguments lifted to top level.
|
|
80
|
+
*/
|
|
81
|
+
export function decodeCallPayload(payload: string): Call | undefined {
|
|
82
|
+
const repairs: string[] = []
|
|
83
|
+
let parsed = tryParse(payload)
|
|
84
|
+
if (parsed === undefined) {
|
|
85
|
+
const fixed = repairExtraBraces(payload)
|
|
86
|
+
if (fixed !== payload) {
|
|
87
|
+
parsed = tryParse(fixed)
|
|
88
|
+
if (parsed !== undefined) repairs.push('json_repaired')
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (Array.isArray(parsed) && parsed.length === 1) {
|
|
92
|
+
parsed = parsed[0]
|
|
93
|
+
repairs.push('array_unwrapped')
|
|
94
|
+
}
|
|
95
|
+
if (!isObject(parsed)) return undefined
|
|
96
|
+
let object = parsed
|
|
97
|
+
if (object.name === undefined && isObject(object.function)) {
|
|
98
|
+
object = object.function
|
|
99
|
+
repairs.push('function_wrapper')
|
|
100
|
+
}
|
|
101
|
+
let name = object.name ?? object.tool ?? object.cmd ?? object.function
|
|
102
|
+
if (object.name === undefined && name !== undefined) repairs.push('name_alias')
|
|
103
|
+
let args: unknown = object.arguments ?? object.args ?? object.parameters ?? object.input
|
|
104
|
+
if (object.arguments === undefined && args !== undefined) repairs.push('arguments_alias')
|
|
105
|
+
if (args === undefined && typeof name === 'string') {
|
|
106
|
+
const lifted = Object.fromEntries(Object.entries(object).filter(([key]) =>
|
|
107
|
+
!['name', 'tool', 'cmd', 'function', 'type', 'id'].includes(key)))
|
|
108
|
+
if (Object.keys(lifted).length > 0) {
|
|
109
|
+
args = lifted
|
|
110
|
+
repairs.push('arguments_lifted')
|
|
111
|
+
} else {
|
|
112
|
+
args = {}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (name === undefined && typeof object.path === 'string') {
|
|
116
|
+
name = 'read_file'
|
|
117
|
+
args = { path: object.path }
|
|
118
|
+
repairs.push('path_argument')
|
|
119
|
+
}
|
|
120
|
+
if (typeof args === 'string') {
|
|
121
|
+
const inner = tryParse(args)
|
|
122
|
+
if (isObject(inner)) {
|
|
123
|
+
args = inner
|
|
124
|
+
repairs.push('stringified_arguments')
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (typeof name !== 'string') return undefined
|
|
128
|
+
let resolved = name.trim()
|
|
129
|
+
if (resolved === 'reader' || resolved === 'file_reader') {
|
|
130
|
+
resolved = 'read_file'
|
|
131
|
+
repairs.push('tool_renamed')
|
|
132
|
+
}
|
|
133
|
+
return { name: resolved, args, repairs }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function exitAnswer(args: Record<string, unknown>): string {
|
|
137
|
+
for (const key of ['reason', 'answer', 'response', 'text']) {
|
|
138
|
+
const value = args[key]
|
|
139
|
+
if (typeof value === 'string' && value.trim() !== '') return value.trim()
|
|
140
|
+
}
|
|
141
|
+
return ''
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function callAction(call: Call, extra: string[] = []): Action {
|
|
145
|
+
if (call.name === '' || !isObject(call.args)) {
|
|
146
|
+
return { type: 'error', kind: 'shape', message: 'invalid G1 tool call' }
|
|
147
|
+
}
|
|
148
|
+
const repairs = [...call.repairs, ...extra]
|
|
149
|
+
if (call.name === EXIT_TOOL_NAME) return { type: 'exit', answer: exitAnswer(call.args), repairs }
|
|
150
|
+
return { type: 'tool', name: call.name, args: call.args, repairs }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* recover-preamble (harness default since v25): one sentence, a newline, then
|
|
155
|
+
* one complete call. Only a single standalone call at the tail is accepted;
|
|
156
|
+
* quoted examples, multiple calls, fenced text and truncated output are not.
|
|
157
|
+
*/
|
|
158
|
+
function recoverPreamble(value: string, finish: FinishReason): Action | undefined {
|
|
159
|
+
if (finish === 'length') return undefined
|
|
160
|
+
const candidate = trimWithheldOpening(stripLeadingThinkBlocks(value.trim()))
|
|
161
|
+
if (candidate.startsWith('<think>')) return undefined
|
|
162
|
+
const opens = candidate.split('<tool_call>').length - 1
|
|
163
|
+
if (opens !== 1) return undefined
|
|
164
|
+
const index = candidate.indexOf('<tool_call>')
|
|
165
|
+
if (index === 0 || candidate[index - 1] !== '\n') return undefined
|
|
166
|
+
if (candidate.slice(0, index).includes('```')) return undefined
|
|
167
|
+
const { content } = envelopeContent(candidate.slice(index), '<tool_call>', '</tool_call>')
|
|
168
|
+
const call = decodeCallPayload(content)
|
|
169
|
+
if (call === undefined || call.name === '' || call.name === 'TOOL_NAME' || !isObject(call.args)) return undefined
|
|
170
|
+
return callAction(call, ['preamble_stripped'])
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Compact self-closing call some checkpoints emit: <read_file path="a.txt"/>. */
|
|
174
|
+
function parseLegacyXMLCall(value: string): Action | undefined {
|
|
175
|
+
let candidate = value.trim()
|
|
176
|
+
if (candidate.startsWith('>')) candidate = candidate.slice(1).trim()
|
|
177
|
+
const match = /^<([A-Za-z_][\w-]*)((?:\s+[A-Za-z_][\w-]*="[^"]*")*)\s*\/>$/.exec(candidate)
|
|
178
|
+
if (!match) return undefined
|
|
179
|
+
const args: Record<string, string> = {}
|
|
180
|
+
for (const attribute of match[2]!.matchAll(/([A-Za-z_][\w-]*)="([^"]*)"/g)) {
|
|
181
|
+
args[attribute[1]!] = attribute[2]!
|
|
182
|
+
}
|
|
183
|
+
const name = match[1]!
|
|
184
|
+
if (name === 'read_file' && args.file_path !== undefined && args.path === undefined) {
|
|
185
|
+
args.path = args.file_path
|
|
186
|
+
delete args.file_path
|
|
187
|
+
}
|
|
188
|
+
return callAction({ name, args, repairs: ['legacy_xml_call'] })
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export interface ParseOptions {
|
|
192
|
+
/**
|
|
193
|
+
* Accept only the exact protocol form: no near-miss repairs. A call that
|
|
194
|
+
* needs one, or a final answer that still carries a tool envelope, is a
|
|
195
|
+
* shape error.
|
|
196
|
+
*/
|
|
197
|
+
readonly strict?: boolean
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function parseAction(value: string, finish: FinishReason, options: ParseOptions = {}): Action {
|
|
201
|
+
const action = parseLenient(value, finish)
|
|
202
|
+
if (options.strict !== true) return action
|
|
203
|
+
if ((action.type === 'tool' || action.type === 'exit') && action.repairs.length > 0) {
|
|
204
|
+
return { type: 'error', kind: 'shape', message: `tool call needs repair: ${action.repairs.join(',')}` }
|
|
205
|
+
}
|
|
206
|
+
if (action.type === 'final' && /<tool_calls?>/.test(action.text)) {
|
|
207
|
+
return { type: 'error', kind: 'shape', message: 'tool call not at the start of the output' }
|
|
208
|
+
}
|
|
209
|
+
return action
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function parseLenient(value: string, finish: FinishReason): Action {
|
|
213
|
+
const recovered = recoverPreamble(value, finish)
|
|
214
|
+
if (recovered !== undefined) return recovered
|
|
215
|
+
let candidate = stripLeadingThinkBlocks(value.trim())
|
|
216
|
+
if (finish === 'length') {
|
|
217
|
+
return candidate.startsWith('<think>')
|
|
218
|
+
? { type: 'error', kind: 'unclosed-think', message: 'reasoning never closed' }
|
|
219
|
+
: { type: 'error', kind: 'length', message: 'output token limit' }
|
|
220
|
+
}
|
|
221
|
+
if (candidate.startsWith('<think>')) {
|
|
222
|
+
return { type: 'error', kind: 'unclosed-think', message: 'reasoning never closed' }
|
|
223
|
+
}
|
|
224
|
+
candidate = trimWithheldOpening(candidate)
|
|
225
|
+
if (candidate.startsWith('<tool_call>')) {
|
|
226
|
+
const { content, closed } = envelopeContent(candidate, '<tool_call>', '</tool_call>')
|
|
227
|
+
if (!closed && finish !== 'stop') {
|
|
228
|
+
return { type: 'error', kind: 'decode', message: 'incomplete tool call envelope' }
|
|
229
|
+
}
|
|
230
|
+
const call = decodeCallPayload(content)
|
|
231
|
+
if (call === undefined) return { type: 'error', kind: 'decode', message: 'tool call JSON did not decode' }
|
|
232
|
+
return callAction(call)
|
|
233
|
+
}
|
|
234
|
+
if (candidate.startsWith('<answer>')) {
|
|
235
|
+
const { content } = envelopeContent(candidate, '<answer>', '</answer>')
|
|
236
|
+
return content === ''
|
|
237
|
+
? { type: 'error', kind: 'empty', message: 'empty answer' }
|
|
238
|
+
: { type: 'final', text: content }
|
|
239
|
+
}
|
|
240
|
+
if (candidate === '') return { type: 'error', kind: 'empty', message: 'empty model response' }
|
|
241
|
+
if (candidate.startsWith('</tool_call>')) {
|
|
242
|
+
return { type: 'error', kind: 'shape', message: 'unexpected tool call closing tag' }
|
|
243
|
+
}
|
|
244
|
+
// The model may echo the result envelope around its own action.
|
|
245
|
+
for (const open of ['<tool_response>', '<tool_result>']) {
|
|
246
|
+
if (candidate.startsWith(open)) {
|
|
247
|
+
const { content } = envelopeContent(candidate, open, `</${open.slice(1)}`)
|
|
248
|
+
if (content !== '') {
|
|
249
|
+
const inner = parseLenient(content, finish)
|
|
250
|
+
if (inner.type === 'tool' || inner.type === 'exit') {
|
|
251
|
+
return { ...inner, repairs: ['envelope_recovered', ...inner.repairs] }
|
|
252
|
+
}
|
|
253
|
+
return inner
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (candidate.includes('<tool_calls>')) {
|
|
258
|
+
const { content } = envelopeContent(candidate.slice(candidate.indexOf('<tool_calls>')), '<tool_calls>', '</tool_calls>')
|
|
259
|
+
const call = decodeCallPayload(content)
|
|
260
|
+
if (call !== undefined) return callAction(call, ['envelope_recovered'])
|
|
261
|
+
}
|
|
262
|
+
const legacy = parseLegacyXMLCall(candidate)
|
|
263
|
+
if (legacy !== undefined) return legacy
|
|
264
|
+
// JSON outside a <tool_call> envelope is text: a user who asks for JSON gets it.
|
|
265
|
+
return { type: 'final', text: candidate }
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Whether text after the last <tool_call> is a real call rather than a quoted template. */
|
|
269
|
+
export function endsWithCompleteCall(text: string): boolean {
|
|
270
|
+
const index = text.lastIndexOf('<tool_call>')
|
|
271
|
+
if (index < 0) return false
|
|
272
|
+
const call = decodeCallPayload(text.slice(index + '<tool_call>'.length).trim())
|
|
273
|
+
return call !== undefined && call.name !== '' && call.name !== 'TOOL_NAME' && call.name !== '...' && isObject(call.args)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export function thinkOpen(text: string): boolean {
|
|
277
|
+
return (text.match(/<think/g) ?? []).length > (text.match(/<\/think>/g) ?? []).length
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Final-answer clean-up: leftover think block, trailing role labels the stop did not catch. */
|
|
281
|
+
export function cleanFinalText(text: string): string {
|
|
282
|
+
let result = stripLeadingThinkBlocks(text.trim())
|
|
283
|
+
if (result.includes('</think>') && !result.includes('<think')) {
|
|
284
|
+
const afterOrphanClose = result.slice(result.lastIndexOf('</think>') + '</think>'.length).trim()
|
|
285
|
+
if (afterOrphanClose !== '') result = afterOrphanClose
|
|
286
|
+
}
|
|
287
|
+
const role = /\n\s*(?:User|System|Assistant|Tool)\s*[::]/.exec(result)
|
|
288
|
+
if (role) result = result.slice(0, role.index)
|
|
289
|
+
return result.replace(/✿.*$/s, '').trim()
|
|
290
|
+
}
|