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,209 @@
|
|
|
1
|
+
// Built-in RWKV <-> DSH tool mappings (`agent-tool-v3` wire shapes and
|
|
2
|
+
// bidirectional argument converters).
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
normalizeWorklistStatus,
|
|
6
|
+
parsePlanItems,
|
|
7
|
+
TODO_PLAN_DESCRIPTION,
|
|
8
|
+
TODO_PLAN_TOOL,
|
|
9
|
+
TODO_UPDATE_DESCRIPTION,
|
|
10
|
+
TODO_UPDATE_TOOL,
|
|
11
|
+
} from './todo-state.ts'
|
|
12
|
+
|
|
13
|
+
export interface DshToolSchema {
|
|
14
|
+
readonly name: string
|
|
15
|
+
readonly description: string
|
|
16
|
+
readonly parameters: Record<string, unknown>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type Args = Record<string, unknown>
|
|
20
|
+
|
|
21
|
+
export interface WireTool {
|
|
22
|
+
/** Name the model sees and calls. */
|
|
23
|
+
readonly name: string
|
|
24
|
+
readonly description: string
|
|
25
|
+
/** Flat placeholder JSON object (or JSON string) shown in the catalog. */
|
|
26
|
+
readonly arguments: string
|
|
27
|
+
/** DSH tool that executes the call. */
|
|
28
|
+
readonly dshName: string
|
|
29
|
+
/** JSON Schema used for lenient scalar coercion of model arguments. */
|
|
30
|
+
readonly schema?: Record<string, unknown>
|
|
31
|
+
/** Shown under its DSH name with DSH arguments (names: 'dsh'). */
|
|
32
|
+
readonly native?: boolean
|
|
33
|
+
toDsh(args: Args): Args
|
|
34
|
+
fromDsh(args: Args): Args
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ToolMapping {
|
|
38
|
+
readonly wireName: string
|
|
39
|
+
readonly description: string
|
|
40
|
+
readonly arguments: string
|
|
41
|
+
readonly schema: Record<string, unknown>
|
|
42
|
+
toDsh(args: Args): Args
|
|
43
|
+
fromDsh(args: Args): Args
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const nonEmptyStr = (value: unknown): string | undefined =>
|
|
47
|
+
typeof value === 'string' && value.trim() !== '' ? value : undefined
|
|
48
|
+
|
|
49
|
+
export const finiteInt = (value: unknown): number | undefined =>
|
|
50
|
+
typeof value === 'number' && Number.isFinite(value) ? Math.trunc(value) : undefined
|
|
51
|
+
|
|
52
|
+
function stripUndefined(args: Args): Args {
|
|
53
|
+
return Object.fromEntries(Object.entries(args).filter(([, value]) => value !== undefined))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Workspace-relative path; models trained on a sandbox emit /workspace/... and fabricated home paths. */
|
|
57
|
+
export function workspacePath(value: unknown): string | undefined {
|
|
58
|
+
const path = nonEmptyStr(value)
|
|
59
|
+
if (path === undefined) return undefined
|
|
60
|
+
const index = path.lastIndexOf('workspace/')
|
|
61
|
+
if (path.startsWith('/') && index >= 0) return path.slice(index + 'workspace/'.length) || '.'
|
|
62
|
+
if (path === '/workspace') return '.'
|
|
63
|
+
return path
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function escapeRegex(text: string): string {
|
|
67
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function unescapeRegex(text: string): string {
|
|
71
|
+
return text.replace(/\\([.*+?^${}()|[\]\\])/g, '$1')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Built-in mappings keyed by DSH tool name. Descriptions and placeholders are
|
|
76
|
+
* the agent-tool-v3 strings the RWKV-Agent corpora were rendered with.
|
|
77
|
+
*/
|
|
78
|
+
export const BUILTIN_MAPPINGS: Readonly<Record<string, ToolMapping>> = {
|
|
79
|
+
read: {
|
|
80
|
+
wireName: 'read_file',
|
|
81
|
+
description: 'Read a UTF-8 text file in the workspace. Lines come numbered. Without start_line/end_line it returns the start of the file.',
|
|
82
|
+
arguments: '{"path":"relative file path","start_line":"optional integer >= 1","end_line":"optional integer >= start_line"}',
|
|
83
|
+
schema: { properties: { path: { type: 'string' }, start_line: { type: 'integer' }, end_line: { type: 'integer' } } },
|
|
84
|
+
toDsh(args) {
|
|
85
|
+
const start = finiteInt(args.start_line)
|
|
86
|
+
const end = finiteInt(args.end_line)
|
|
87
|
+
return stripUndefined({
|
|
88
|
+
file_path: workspacePath(args.path),
|
|
89
|
+
offset: start,
|
|
90
|
+
limit: end !== undefined ? Math.max(1, end - (start ?? 1) + 1) : undefined,
|
|
91
|
+
})
|
|
92
|
+
},
|
|
93
|
+
fromDsh(args) {
|
|
94
|
+
const offset = finiteInt(args.offset)
|
|
95
|
+
const limit = finiteInt(args.limit)
|
|
96
|
+
return stripUndefined({
|
|
97
|
+
path: args.file_path,
|
|
98
|
+
start_line: offset,
|
|
99
|
+
end_line: limit !== undefined ? (offset ?? 1) + limit - 1 : undefined,
|
|
100
|
+
})
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
write: {
|
|
104
|
+
wireName: 'write_file',
|
|
105
|
+
description: 'Create or overwrite a workspace text file with the given content. The content becomes the whole file.',
|
|
106
|
+
arguments: '{"path":"relative file path","content":"full file content"}',
|
|
107
|
+
schema: { properties: { path: { type: 'string' }, content: { type: 'string' } } },
|
|
108
|
+
toDsh: args => ({ file_path: workspacePath(args.path), content: args.content ?? '' }),
|
|
109
|
+
fromDsh: args => ({ path: args.file_path, content: args.content }),
|
|
110
|
+
},
|
|
111
|
+
edit: {
|
|
112
|
+
wireName: 'edit_file',
|
|
113
|
+
description: 'Replace one exact piece of text in a workspace text file. old_text must appear exactly once; copy it from read_file without the line numbers.',
|
|
114
|
+
arguments: '{"path":"relative file path","old_text":"exact text to replace","new_text":"replacement text"}',
|
|
115
|
+
schema: { properties: { path: { type: 'string' }, old_text: { type: 'string' }, new_text: { type: 'string' } } },
|
|
116
|
+
toDsh: args => ({ file_path: workspacePath(args.path), old_string: args.old_text ?? '', new_string: args.new_text ?? '' }),
|
|
117
|
+
fromDsh: args => ({ path: args.file_path, old_text: args.old_string, new_text: args.new_string }),
|
|
118
|
+
},
|
|
119
|
+
// Line-range edit (src/line-edit.ts); offered only where that plugin is mounted.
|
|
120
|
+
edit_lines: {
|
|
121
|
+
wireName: 'edit_lines',
|
|
122
|
+
description: 'Replace a range of lines in a workspace text file, numbered as read_file shows them. end_line = start_line - 1 inserts before start_line.',
|
|
123
|
+
arguments: '{"path":"relative file path","start_line":"first line to replace","end_line":"last line to replace","new_text":"replacement lines without line numbers"}',
|
|
124
|
+
schema: { properties: { path: { type: 'string' }, start_line: { type: 'integer' }, end_line: { type: 'integer' }, new_text: { type: 'string' } } },
|
|
125
|
+
toDsh: args => ({ file_path: workspacePath(args.path), start_line: args.start_line, end_line: args.end_line, new_text: args.new_text ?? '' }),
|
|
126
|
+
fromDsh: args => ({ path: args.file_path, start_line: args.start_line, end_line: args.end_line, new_text: args.new_text }),
|
|
127
|
+
},
|
|
128
|
+
glob: {
|
|
129
|
+
wireName: 'list_files',
|
|
130
|
+
description: 'List files below a workspace-relative path.',
|
|
131
|
+
arguments: '{"path":"optional relative directory","pattern":"optional glob such as *.ts"}',
|
|
132
|
+
schema: { properties: { path: { type: 'string' }, pattern: { type: 'string' } } },
|
|
133
|
+
toDsh: args => stripUndefined({ pattern: nonEmptyStr(args.pattern) ?? '*', path: workspacePath(args.path) }),
|
|
134
|
+
fromDsh: args => stripUndefined({ path: args.path, pattern: args.pattern === '*' ? undefined : args.pattern }),
|
|
135
|
+
},
|
|
136
|
+
grep: {
|
|
137
|
+
wireName: 'search_text',
|
|
138
|
+
description: 'Search literal text in workspace files. Returns matching lines with line numbers.',
|
|
139
|
+
arguments: '{"query":"literal text","path":"optional relative path"}',
|
|
140
|
+
schema: { properties: { query: { type: 'string' }, path: { type: 'string' } } },
|
|
141
|
+
toDsh: args => stripUndefined({ pattern: escapeRegex(String(args.query ?? '')), path: workspacePath(args.path) }),
|
|
142
|
+
fromDsh: args => stripUndefined({ query: unescapeRegex(String(args.pattern ?? '')), path: args.path }),
|
|
143
|
+
},
|
|
144
|
+
bash: {
|
|
145
|
+
wireName: 'bash',
|
|
146
|
+
description: 'Run a bash command in the workspace and return stdout and stderr. Each call starts in the workspace and does not keep cd or variables from earlier calls.',
|
|
147
|
+
arguments: '{"command":"bash command line"}',
|
|
148
|
+
schema: { properties: { command: { type: 'string' } } },
|
|
149
|
+
toDsh(args) {
|
|
150
|
+
const command = String(args.command ?? '')
|
|
151
|
+
const firstLine = command.split('\n')[0] ?? ''
|
|
152
|
+
return { description: firstLine.length > 60 ? `${firstLine.slice(0, 57)}...` : firstLine || 'Run command', command }
|
|
153
|
+
},
|
|
154
|
+
fromDsh: args => ({ command: args.command }),
|
|
155
|
+
},
|
|
156
|
+
web_search: {
|
|
157
|
+
wireName: 'web_search',
|
|
158
|
+
description: 'Search the public web and return titles, URLs, snippets, and source IDs.',
|
|
159
|
+
arguments: '{"query":"search query"}',
|
|
160
|
+
schema: { properties: { query: { type: 'string' } } },
|
|
161
|
+
toDsh: args => ({ queries: [String(args.query ?? '')] }),
|
|
162
|
+
fromDsh: args => ({ query: Array.isArray(args.queries) ? args.queries.join(' ') : args.queries }),
|
|
163
|
+
},
|
|
164
|
+
web_fetch: {
|
|
165
|
+
wireName: 'web_fetch',
|
|
166
|
+
description: 'Fetch the readable content of one public web page.',
|
|
167
|
+
arguments: '{"url":"https://example.com/page"}',
|
|
168
|
+
schema: { properties: { url: { type: 'string' } } },
|
|
169
|
+
toDsh(args) {
|
|
170
|
+
const url = nonEmptyStr(args.url) ?? (Array.isArray(args.urls) ? nonEmptyStr(args.urls[0]) : undefined)
|
|
171
|
+
return { url: url ?? '' }
|
|
172
|
+
},
|
|
173
|
+
fromDsh: args => ({ url: args.url }),
|
|
174
|
+
},
|
|
175
|
+
// Stateful work list (src/todo.ts); offered when historyMode === 'worklist'.
|
|
176
|
+
[TODO_PLAN_TOOL]: {
|
|
177
|
+
wireName: TODO_PLAN_TOOL,
|
|
178
|
+
description: TODO_PLAN_DESCRIPTION,
|
|
179
|
+
arguments: '{"items":"array of string"}',
|
|
180
|
+
schema: { properties: { items: { type: 'array', items: { type: 'string' } } } },
|
|
181
|
+
toDsh: args => ({ items: parsePlanItems(args.items) ?? args.items }),
|
|
182
|
+
fromDsh: args => ({ items: args.items }),
|
|
183
|
+
},
|
|
184
|
+
[TODO_UPDATE_TOOL]: {
|
|
185
|
+
wireName: TODO_UPDATE_TOOL,
|
|
186
|
+
description: TODO_UPDATE_DESCRIPTION,
|
|
187
|
+
arguments: '{"id":"integer","status":"in_progress|completed|dropped","note":"optional Concrete findings, exact file paths/lines, applied edits, or test outcomes","content":"optional Refined title for this item if the initial wording was inaccurate"}',
|
|
188
|
+
schema: {
|
|
189
|
+
properties: {
|
|
190
|
+
id: { type: 'integer' },
|
|
191
|
+
status: { type: 'string', enum: ['in_progress', 'completed', 'dropped'] },
|
|
192
|
+
note: { type: 'string' },
|
|
193
|
+
content: { type: 'string' },
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
toDsh: args => stripUndefined({
|
|
197
|
+
id: finiteInt(typeof args.id === 'string' ? Number(args.id.trim()) : args.id) ?? args.id,
|
|
198
|
+
status: normalizeWorklistStatus(args.status) ?? args.status,
|
|
199
|
+
note: nonEmptyStr(args.note),
|
|
200
|
+
content: nonEmptyStr(args.content),
|
|
201
|
+
}),
|
|
202
|
+
fromDsh: args => stripUndefined({
|
|
203
|
+
id: args.id,
|
|
204
|
+
status: args.status,
|
|
205
|
+
note: args.note,
|
|
206
|
+
content: args.content,
|
|
207
|
+
}),
|
|
208
|
+
},
|
|
209
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// Lenient tool-name resolution, argument key aliases, scalar coercion, and
|
|
2
|
+
// DSH native value shape repairs for 7B outputs.
|
|
3
|
+
|
|
4
|
+
import { editLinesAliasesFor } from './edit-modes/index.ts'
|
|
5
|
+
import { normalizeLineEditArgs } from './edit-modes/line-edit.ts'
|
|
6
|
+
import {
|
|
7
|
+
normalizeWorklistStatus,
|
|
8
|
+
parsePlanItems,
|
|
9
|
+
TODO_PLAN_TOOL,
|
|
10
|
+
TODO_UPDATE_TOOL,
|
|
11
|
+
} from './todo-state.ts'
|
|
12
|
+
import { BUILTIN_MAPPINGS, finiteInt, nonEmptyStr, type Args, type WireTool } from './tool-mappings.ts'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Other names the model uses for the wire tools: DSH's own names, Claude Code
|
|
16
|
+
* / OpenAI-agent habits, and capitalised forms ("Bash"). Matched after an
|
|
17
|
+
* exact, then a case-insensitive, lookup fails.
|
|
18
|
+
*/
|
|
19
|
+
const TOOL_NAME_ALIASES: Readonly<Record<string, string>> = {
|
|
20
|
+
read: 'read_file', view: 'read_file', cat: 'read_file', open_file: 'read_file',
|
|
21
|
+
write: 'write_file', create_file: 'write_file', save_file: 'write_file',
|
|
22
|
+
edit: 'edit_file', str_replace: 'edit_file', replace: 'edit_file', replace_in_file: 'edit_file',
|
|
23
|
+
glob: 'list_files', ls: 'list_files', list_dir: 'list_files', list_directory: 'list_files',
|
|
24
|
+
grep: 'search_text', search: 'search_text', search_files: 'search_text',
|
|
25
|
+
shell: 'bash', run: 'bash', terminal: 'bash', execute_command: 'bash', run_command: 'bash',
|
|
26
|
+
fetch: 'web_fetch', webfetch: 'web_fetch', search_web: 'web_search', websearch: 'web_search',
|
|
27
|
+
worklist_plan: TODO_PLAN_TOOL, plan_todo: TODO_PLAN_TOOL,
|
|
28
|
+
worklist_update: TODO_UPDATE_TOOL, update_todo: TODO_UPDATE_TOOL,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The offered tool a model-written name refers to, and whether an alias was needed. */
|
|
32
|
+
export function resolveTool(tools: readonly WireTool[], name: string): { tool: WireTool; aliased: boolean } | undefined {
|
|
33
|
+
const exact = tools.find(tool => tool.name === name)
|
|
34
|
+
if (exact) return { tool: exact, aliased: false }
|
|
35
|
+
const lower = name.trim().toLowerCase().replace(/^functions\./, '')
|
|
36
|
+
const target = tools.find(tool => tool.name.toLowerCase() === lower)
|
|
37
|
+
?? tools.find(tool => tool.name === TOOL_NAME_ALIASES[lower])
|
|
38
|
+
?? tools.find(tool => tool.dshName.toLowerCase() === lower)
|
|
39
|
+
// DSH-named catalog: the trained names (read_file, ...) and their aliases.
|
|
40
|
+
?? tools.find(tool => BUILTIN_MAPPINGS[tool.dshName]?.wireName === (TOOL_NAME_ALIASES[lower] ?? lower))
|
|
41
|
+
// Cross-harness compatibility: when `edit_lines` replaces `edit`, route `edit`/`edit_file` calls to `edit_lines`.
|
|
42
|
+
?? ((lower === 'edit' || lower === 'edit_file' || TOOL_NAME_ALIASES[lower] === 'edit_file')
|
|
43
|
+
? tools.find(tool => tool.dshName === 'edit_lines')
|
|
44
|
+
: undefined)
|
|
45
|
+
return target ? { tool: target, aliased: true } : undefined
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const PATH_ALIASES = ['file_path', 'filepath', 'file', 'filename', 'file_name']
|
|
49
|
+
const NATIVE_PATH_ALIASES = ['path', 'filepath', 'file', 'filename', 'file_name']
|
|
50
|
+
|
|
51
|
+
/** Canonical argument key -> other keys the model writes for it, per wire tool. */
|
|
52
|
+
const ARGUMENT_ALIASES: Readonly<Record<string, Readonly<Record<string, readonly string[]>>>> = {
|
|
53
|
+
read_file: { path: PATH_ALIASES, start_line: ['line_start', 'from_line'], end_line: ['line_end', 'to_line'] },
|
|
54
|
+
write_file: { path: PATH_ALIASES, content: ['text', 'file_text', 'contents', 'data', 'code'] },
|
|
55
|
+
edit_file: {
|
|
56
|
+
path: PATH_ALIASES,
|
|
57
|
+
old_text: ['old_string', 'old_str', 'old', 'search', 'find', 'original'],
|
|
58
|
+
new_text: ['new_string', 'new_str', 'new', 'replace', 'replacement', 'updated'],
|
|
59
|
+
},
|
|
60
|
+
edit_lines: {
|
|
61
|
+
path: PATH_ALIASES,
|
|
62
|
+
start_line: ['line_start', 'from_line', 'start', 'line'],
|
|
63
|
+
end_line: ['line_end', 'to_line', 'end'],
|
|
64
|
+
new_text: ['new_string', 'new_str', 'text', 'content', 'replacement', 'lines'],
|
|
65
|
+
},
|
|
66
|
+
list_files: { path: ['directory', 'dir', 'folder', 'dir_path'], pattern: ['glob'] },
|
|
67
|
+
search_text: { query: ['pattern', 'text', 'search', 'keyword', 'regex'], path: ['file_path', 'directory', 'dir'] },
|
|
68
|
+
bash: { command: ['cmd', 'script', 'code', 'shell'] },
|
|
69
|
+
web_fetch: { url: ['link', 'href', 'page'] },
|
|
70
|
+
web_search: { query: ['q', 'keywords', 'search', 'search_query'] },
|
|
71
|
+
[TODO_PLAN_TOOL]: { items: ['todos', 'tasks', 'steps', 'plan'] },
|
|
72
|
+
[TODO_UPDATE_TOOL]: {
|
|
73
|
+
id: ['index', 'item_id', 'task_id', 'step'],
|
|
74
|
+
note: ['notes', 'summary', 'finding', 'findings', 'conclusion', 'result'],
|
|
75
|
+
content: ['title', 'task', 'text', 'description'],
|
|
76
|
+
},
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The same table for DSH-named tools: trained and Claude-style keys -> DSH keys. */
|
|
80
|
+
const NATIVE_ARGUMENT_ALIASES: Readonly<Record<string, Readonly<Record<string, readonly string[]>>>> = {
|
|
81
|
+
read: { file_path: NATIVE_PATH_ALIASES, offset: ['start_line', 'line_start', 'from_line'] },
|
|
82
|
+
write: { file_path: NATIVE_PATH_ALIASES, content: ['text', 'file_text', 'contents', 'data', 'code'] },
|
|
83
|
+
edit: {
|
|
84
|
+
file_path: NATIVE_PATH_ALIASES,
|
|
85
|
+
old_string: ['old_text', 'old_str', 'old', 'search', 'find', 'original'],
|
|
86
|
+
new_string: ['new_text', 'new_str', 'new', 'replace', 'replacement', 'updated'],
|
|
87
|
+
},
|
|
88
|
+
edit_lines: {
|
|
89
|
+
file_path: NATIVE_PATH_ALIASES,
|
|
90
|
+
start_line: ['line_start', 'from_line', 'start', 'line', 'offset'],
|
|
91
|
+
end_line: ['line_end', 'to_line', 'end'],
|
|
92
|
+
new_text: ['new_string', 'new_str', 'text', 'content', 'replacement', 'lines'],
|
|
93
|
+
},
|
|
94
|
+
glob: { pattern: ['glob'], path: ['directory', 'dir', 'folder', 'dir_path'] },
|
|
95
|
+
grep: { pattern: ['query', 'text', 'search', 'keyword', 'regex'], path: ['file_path', 'directory', 'dir'] },
|
|
96
|
+
bash: { command: ['cmd', 'script', 'code', 'shell'] },
|
|
97
|
+
web_fetch: { url: ['link', 'href', 'page'] },
|
|
98
|
+
web_search: { queries: ['query', 'q', 'keywords', 'search', 'search_query'] },
|
|
99
|
+
[TODO_PLAN_TOOL]: ARGUMENT_ALIASES[TODO_PLAN_TOOL]!,
|
|
100
|
+
[TODO_UPDATE_TOOL]: ARGUMENT_ALIASES[TODO_UPDATE_TOOL]!,
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function renameAliases(args: Args, aliases: Readonly<Record<string, readonly string[]>>): Args | undefined {
|
|
104
|
+
let out: Args | undefined
|
|
105
|
+
for (const [canonical, others] of Object.entries(aliases)) {
|
|
106
|
+
if (args[canonical] !== undefined && args[canonical] !== '') continue
|
|
107
|
+
const found = others.find(key => args[key] !== undefined && args[key] !== '')
|
|
108
|
+
if (found === undefined) continue
|
|
109
|
+
out ??= { ...args }
|
|
110
|
+
out[canonical] = args[found]
|
|
111
|
+
delete out[found]
|
|
112
|
+
}
|
|
113
|
+
return out
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const looseInt = (value: unknown) => finiteInt(typeof value === 'string' ? Number(value.trim()) : value)
|
|
117
|
+
|
|
118
|
+
/** DSH-named tools: alias keys, end_line -> limit, a lone query string -> queries, a bash description. */
|
|
119
|
+
function normalizeNativeArguments(tool: WireTool, args: Args): Args {
|
|
120
|
+
const aliases = tool.dshName === 'edit_lines'
|
|
121
|
+
? editLinesAliasesFor({ native: true })
|
|
122
|
+
: NATIVE_ARGUMENT_ALIASES[tool.dshName]
|
|
123
|
+
if (aliases === undefined) return args
|
|
124
|
+
let out = renameAliases(args, aliases)
|
|
125
|
+
const current = out ?? args
|
|
126
|
+
if (tool.dshName === 'edit_lines' && current.edits !== undefined) {
|
|
127
|
+
out = normalizeLineEditArgs(current)
|
|
128
|
+
}
|
|
129
|
+
if (tool.dshName === 'read' && current.end_line !== undefined) {
|
|
130
|
+
const start = looseInt(current.offset) ?? 1
|
|
131
|
+
const end = looseInt(current.end_line)
|
|
132
|
+
out ??= { ...args }
|
|
133
|
+
delete out.end_line
|
|
134
|
+
if (end !== undefined && current.limit === undefined) out.limit = Math.max(1, end - start + 1)
|
|
135
|
+
}
|
|
136
|
+
if (tool.dshName === 'web_search' && typeof current.queries === 'string') {
|
|
137
|
+
out ??= { ...args }
|
|
138
|
+
out.queries = [current.queries]
|
|
139
|
+
}
|
|
140
|
+
if (tool.dshName === 'bash' && nonEmptyStr(current.description) === undefined && nonEmptyStr(current.command) !== undefined) {
|
|
141
|
+
const firstLine = String(current.command).split('\n')[0] ?? ''
|
|
142
|
+
out ??= { ...args }
|
|
143
|
+
out.description = firstLine.length > 60 ? `${firstLine.slice(0, 57)}...` : firstLine
|
|
144
|
+
}
|
|
145
|
+
return out ?? args
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Renames alias keys to the tool's own keys when the own key is absent
|
|
150
|
+
* (edit_file with Claude's old_string/new_string, read_file with file_path,
|
|
151
|
+
* ...). read_file's DSH-style offset/limit become start_line/end_line.
|
|
152
|
+
* Returns the args unchanged (same object) when nothing applied.
|
|
153
|
+
*/
|
|
154
|
+
export function normalizeArguments(tool: WireTool, args: Args): Args {
|
|
155
|
+
if (tool.native === true) return normalizeNativeArguments(tool, args)
|
|
156
|
+
const aliases = tool.name === 'edit_lines'
|
|
157
|
+
? editLinesAliasesFor({ native: false })
|
|
158
|
+
: ARGUMENT_ALIASES[tool.name]
|
|
159
|
+
if (aliases === undefined) return args
|
|
160
|
+
let out = renameAliases(args, aliases)
|
|
161
|
+
if (tool.name === 'edit_lines' && (out ?? args).edits !== undefined) {
|
|
162
|
+
out = normalizeLineEditArgs(out ?? args)
|
|
163
|
+
}
|
|
164
|
+
if (tool.name === 'read_file' && args.start_line === undefined && args.offset !== undefined) {
|
|
165
|
+
const start = looseInt(args.offset)
|
|
166
|
+
const limit = looseInt(args.limit)
|
|
167
|
+
out ??= { ...args }
|
|
168
|
+
delete out.offset
|
|
169
|
+
delete out.limit
|
|
170
|
+
if (start !== undefined) out.start_line = start
|
|
171
|
+
if (start !== undefined && limit !== undefined) out.end_line = start + limit - 1
|
|
172
|
+
}
|
|
173
|
+
return out ?? args
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Lenient scalars: "10" for an integer, "true" for a boolean, "6,412" with
|
|
178
|
+
* thousands separators. Structural mismatches are left for the tool to reject.
|
|
179
|
+
*/
|
|
180
|
+
export function coerceArguments(args: Args, schema: Record<string, unknown> | undefined): Args {
|
|
181
|
+
const properties = (schema?.properties ?? {}) as Record<string, Record<string, unknown>>
|
|
182
|
+
const result: Args = {}
|
|
183
|
+
for (const [key, value] of Object.entries(args)) {
|
|
184
|
+
const type = properties[key]?.type
|
|
185
|
+
if (value === null) continue
|
|
186
|
+
if ((type === 'integer' || type === 'number') && typeof value === 'string') {
|
|
187
|
+
const parsed = Number(value.replace(/,/g, '').trim())
|
|
188
|
+
result[key] = value.trim() !== '' && Number.isFinite(parsed) ? parsed : value
|
|
189
|
+
} else if (type === 'boolean' && typeof value === 'string' && /^(true|false)$/i.test(value.trim())) {
|
|
190
|
+
result[key] = value.trim().toLowerCase() === 'true'
|
|
191
|
+
} else if (type === 'string' && (typeof value === 'number' || typeof value === 'boolean')) {
|
|
192
|
+
result[key] = String(value)
|
|
193
|
+
} else {
|
|
194
|
+
result[key] = value
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return result
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* DSH-named tools, after scalar coercion: value shapes 7B writes from other
|
|
202
|
+
* harnesses' habits that DSH would reject. Keys outside the DSH schema
|
|
203
|
+
* (Claude Code's grep output_mode, ...) are dropped; glob without a pattern
|
|
204
|
+
* (list_files{path}) lists everything; read offset 0 (0-based habit) is line 1;
|
|
205
|
+
* a pattern array becomes one pattern. The same object when nothing applied.
|
|
206
|
+
*/
|
|
207
|
+
export function repairNativeValues(tool: WireTool, args: Args): { args: Args; repairs: string[] } {
|
|
208
|
+
if (tool.native !== true || tool.schema === undefined) return { args, repairs: [] }
|
|
209
|
+
const properties = (tool.schema.properties ?? {}) as Record<string, unknown>
|
|
210
|
+
const repairs: string[] = []
|
|
211
|
+
let out: Args | undefined
|
|
212
|
+
const edit = () => (out ??= { ...args })
|
|
213
|
+
const unknown = Object.keys(args).filter(key => !(key in properties))
|
|
214
|
+
if (unknown.length > 0) {
|
|
215
|
+
for (const key of unknown) delete edit()[key]
|
|
216
|
+
repairs.push('unknown_arguments_dropped')
|
|
217
|
+
}
|
|
218
|
+
const current = () => out ?? args
|
|
219
|
+
if ((tool.dshName === 'grep' || tool.dshName === 'glob') && Array.isArray(current().pattern)) {
|
|
220
|
+
const parts = (current().pattern as unknown[]).filter((part): part is string => typeof part === 'string' && part !== '')
|
|
221
|
+
if (parts.length > 0) {
|
|
222
|
+
edit().pattern = tool.dshName === 'grep' ? parts.join('|') : parts[0]
|
|
223
|
+
repairs.push('pattern_array')
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (tool.dshName === 'glob' && nonEmptyStr(current().pattern) === undefined) {
|
|
227
|
+
edit().pattern = '*'
|
|
228
|
+
repairs.push('glob_default_pattern')
|
|
229
|
+
}
|
|
230
|
+
if (tool.dshName === 'read') {
|
|
231
|
+
for (const key of ['offset', 'limit']) {
|
|
232
|
+
const value = current()[key]
|
|
233
|
+
if (typeof value === 'number' && value < 1) {
|
|
234
|
+
if (key === 'offset') edit().offset = 1
|
|
235
|
+
else delete edit().limit
|
|
236
|
+
repairs.push(`${key}_below_one`)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (tool.dshName === TODO_PLAN_TOOL && current().items !== undefined) {
|
|
241
|
+
const parsed = parsePlanItems(current().items)
|
|
242
|
+
if (parsed !== undefined && JSON.stringify(parsed) !== JSON.stringify(current().items)) {
|
|
243
|
+
edit().items = parsed
|
|
244
|
+
repairs.push('plan_items_coerced')
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (tool.dshName === TODO_UPDATE_TOOL && typeof current().status === 'string') {
|
|
248
|
+
const normalized = normalizeWorklistStatus(current().status)
|
|
249
|
+
if (normalized !== undefined && normalized !== current().status) {
|
|
250
|
+
edit().status = normalized
|
|
251
|
+
repairs.push('worklist_status_alias')
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return { args: out ?? args, repairs }
|
|
255
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// Tool catalog assembly for the RWKV wire (built-in mappings live in
|
|
2
|
+
// `tool-mappings.ts`; name/argument repairs live in `tool-repairs.ts`;
|
|
3
|
+
// stateful worklist schemas/helpers live in `todo-state.ts`).
|
|
4
|
+
|
|
5
|
+
import { editLinesTrainedWireSpec, LINE_EDIT_DSH_WIRE_ARGUMENTS } from './edit-modes/index.ts'
|
|
6
|
+
import { TODO_PLAN_TOOL } from './todo-state.ts'
|
|
7
|
+
import { BUILTIN_MAPPINGS, workspacePath, type DshToolSchema, type WireTool } from './tool-mappings.ts'
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
isWorklistTool,
|
|
11
|
+
normalizeWorklistStatus,
|
|
12
|
+
parsePlanItems,
|
|
13
|
+
sanitizeWorklistField,
|
|
14
|
+
TODO_PLAN_DESCRIPTION,
|
|
15
|
+
TODO_PLAN_TOOL,
|
|
16
|
+
TODO_UPDATE_DESCRIPTION,
|
|
17
|
+
TODO_UPDATE_TOOL,
|
|
18
|
+
WORKLIST_TOOLS,
|
|
19
|
+
type WorklistStatus,
|
|
20
|
+
} from './todo-state.ts'
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
BUILTIN_MAPPINGS,
|
|
24
|
+
workspacePath,
|
|
25
|
+
type Args,
|
|
26
|
+
type DshToolSchema,
|
|
27
|
+
type ToolMapping,
|
|
28
|
+
type WireTool,
|
|
29
|
+
} from './tool-mappings.ts'
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
coerceArguments,
|
|
33
|
+
normalizeArguments,
|
|
34
|
+
repairNativeValues,
|
|
35
|
+
resolveTool,
|
|
36
|
+
} from './tool-repairs.ts'
|
|
37
|
+
|
|
38
|
+
export const EXIT_TOOL_NAME = 'no_tool'
|
|
39
|
+
|
|
40
|
+
export const EXIT_TOOL_ENTRY = {
|
|
41
|
+
name: EXIT_TOOL_NAME,
|
|
42
|
+
description: 'Indicate that none of the offered tools is needed. Put a brief, complete user-facing response in reason; it becomes the final reply.',
|
|
43
|
+
arguments: '{"reason":"brief complete user-facing response"}',
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type ToolMode = 'mapped' | 'passthrough'
|
|
47
|
+
|
|
48
|
+
/** trained: the agent-tool-v3 names and arguments (read_file, ...); dsh: DSH's own names, descriptions and arguments. */
|
|
49
|
+
export type ToolNames = 'trained' | 'dsh'
|
|
50
|
+
|
|
51
|
+
export interface CatalogOptions {
|
|
52
|
+
/** mapped: only DSH tools with a built-in RWKV mapping; passthrough: every offered tool, unmapped ones with a flat schema. */
|
|
53
|
+
readonly mode: ToolMode
|
|
54
|
+
/** Defaults to trained. */
|
|
55
|
+
readonly names?: ToolNames
|
|
56
|
+
/** Wire or DSH names to hide; applied after the mode filter. */
|
|
57
|
+
readonly exclude?: readonly string[]
|
|
58
|
+
/** Experiment-only: replace a tool's description and/or argument placeholders, keyed by wire or DSH name. */
|
|
59
|
+
readonly overrides?: Readonly<Record<string, ToolOverride>>
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Replacement text for one catalog entry; unset fields keep the default. */
|
|
63
|
+
export interface ToolOverride {
|
|
64
|
+
readonly description?: string
|
|
65
|
+
/** Argument placeholder text by argument key; keys the tool does not have are ignored. */
|
|
66
|
+
readonly arguments?: Readonly<Record<string, string>>
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function applyOverride(tool: WireTool, override: ToolOverride | undefined): WireTool {
|
|
70
|
+
if (override === undefined) return tool
|
|
71
|
+
let args = tool.arguments
|
|
72
|
+
if (override.arguments !== undefined) {
|
|
73
|
+
const parsed = JSON.parse(args) as Record<string, string>
|
|
74
|
+
for (const [key, text] of Object.entries(override.arguments)) {
|
|
75
|
+
if (key in parsed) parsed[key] = text
|
|
76
|
+
}
|
|
77
|
+
args = JSON.stringify(parsed)
|
|
78
|
+
}
|
|
79
|
+
return { ...tool, description: override.description ?? tool.description, arguments: args }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function firstSentence(text: string): string {
|
|
83
|
+
const sentence = text.split(/(?<=\.)\s/)[0] ?? text
|
|
84
|
+
return sentence.length > 80 ? `${sentence.slice(0, 77)}...` : sentence.replace(/\.$/, '')
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Flat placeholder object for an unmapped DSH tool's JSON Schema. */
|
|
88
|
+
export function flatPlaceholder(parameters: Record<string, unknown>): string {
|
|
89
|
+
const properties = (parameters.properties ?? {}) as Record<string, Record<string, unknown>>
|
|
90
|
+
const required = new Set(Array.isArray(parameters.required) ? parameters.required as string[] : [])
|
|
91
|
+
const flat: Record<string, string> = {}
|
|
92
|
+
for (const [key, schema] of Object.entries(properties)) {
|
|
93
|
+
let shape: string
|
|
94
|
+
if (Array.isArray(schema.enum)) shape = schema.enum.join('|')
|
|
95
|
+
else if (schema.type === 'array') {
|
|
96
|
+
const items = (schema.items ?? {}) as Record<string, unknown>
|
|
97
|
+
shape = `array of ${String(items.type ?? 'value')}`
|
|
98
|
+
} else if (typeof schema.description === 'string' && schema.type === 'string') {
|
|
99
|
+
shape = firstSentence(schema.description)
|
|
100
|
+
} else shape = String(Array.isArray(schema.type) ? schema.type.join('|') : schema.type ?? 'value')
|
|
101
|
+
flat[key] = required.has(key) ? shape : `optional ${shape}`
|
|
102
|
+
}
|
|
103
|
+
return JSON.stringify(flat)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function buildCatalog(offered: readonly DshToolSchema[], options: CatalogOptions): WireTool[] {
|
|
107
|
+
const exclude = new Set(options.exclude ?? [])
|
|
108
|
+
// The line-range edit replaces the literal one wherever it is mounted and active.
|
|
109
|
+
if (offered.some(tool => tool.name === 'edit_lines') && !exclude.has('edit_lines')) exclude.add('edit')
|
|
110
|
+
// Stateful worklist replaces DSH's whole-list `todo_write` when offered and active.
|
|
111
|
+
if (offered.some(tool => tool.name === TODO_PLAN_TOOL) && !exclude.has(TODO_PLAN_TOOL)) exclude.add('todo_write')
|
|
112
|
+
const tools: WireTool[] = []
|
|
113
|
+
for (const tool of offered) {
|
|
114
|
+
const mapping = BUILTIN_MAPPINGS[tool.name]
|
|
115
|
+
if (mapping !== undefined && options.names === 'dsh') {
|
|
116
|
+
if (exclude.has(tool.name)) continue
|
|
117
|
+
tools.push({
|
|
118
|
+
name: tool.name,
|
|
119
|
+
description: tool.description,
|
|
120
|
+
arguments: tool.name === 'edit_lines' ? LINE_EDIT_DSH_WIRE_ARGUMENTS : flatPlaceholder(tool.parameters),
|
|
121
|
+
dshName: tool.name,
|
|
122
|
+
schema: tool.parameters,
|
|
123
|
+
native: true,
|
|
124
|
+
toDsh: args => args,
|
|
125
|
+
fromDsh: args => args,
|
|
126
|
+
})
|
|
127
|
+
} else if (mapping !== undefined) {
|
|
128
|
+
const effective = tool.name === 'edit_lines'
|
|
129
|
+
? editLinesTrainedWireSpec(workspacePath)
|
|
130
|
+
: mapping
|
|
131
|
+
if (exclude.has(tool.name) || exclude.has(effective.wireName)) continue
|
|
132
|
+
tools.push({
|
|
133
|
+
name: effective.wireName,
|
|
134
|
+
description: effective.description,
|
|
135
|
+
arguments: effective.arguments,
|
|
136
|
+
dshName: tool.name,
|
|
137
|
+
schema: effective.schema,
|
|
138
|
+
toDsh: effective.toDsh,
|
|
139
|
+
fromDsh: effective.fromDsh,
|
|
140
|
+
})
|
|
141
|
+
} else if (options.mode === 'passthrough' && !exclude.has(tool.name) && tool.name !== EXIT_TOOL_NAME) {
|
|
142
|
+
tools.push({
|
|
143
|
+
name: tool.name,
|
|
144
|
+
description: firstSentence(tool.description),
|
|
145
|
+
arguments: flatPlaceholder(tool.parameters),
|
|
146
|
+
dshName: tool.name,
|
|
147
|
+
schema: tool.parameters,
|
|
148
|
+
toDsh: args => args,
|
|
149
|
+
fromDsh: args => args,
|
|
150
|
+
})
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const overrides = options.overrides ?? {}
|
|
154
|
+
const final = tools.map(tool => applyOverride(tool, overrides[tool.name] ?? overrides[tool.dshName]))
|
|
155
|
+
// Stable alphabetical order, as in the RWKV-Agent catalog.
|
|
156
|
+
return final.sort((a, b) => a.name.localeCompare(b.name))
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function toolAccessDescription(tools: readonly WireTool[]): string {
|
|
160
|
+
const mutating = tools.some(tool => ['write', 'edit', 'edit_lines', 'bash'].includes(tool.dshName))
|
|
161
|
+
return mutating ? 'isolated tools, including the explicitly listed mutation tools' : 'read-only tools'
|
|
162
|
+
}
|