one-model-router 0.0.10 → 0.0.11
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 -21
- package/package.json +1 -1
- package/src/admin.js +941 -941
- package/src/balances.js +61 -61
- package/src/boot.js +117 -117
- package/src/channels/buddy.js +2031 -2031
- package/src/channels/cline.js +753 -753
- package/src/channels/codearts.js +2051 -2051
- package/src/channels/comate.js +830 -830
- package/src/channels/doubaowork.js +1784 -1784
- package/src/channels/freebuff.js +1316 -1316
- package/src/channels/gemini.js +1721 -1721
- package/src/channels/index.js +270 -270
- package/src/channels/joycode.js +936 -936
- package/src/channels/kimi.js +572 -572
- package/src/channels/lobsterai.js +1226 -1226
- package/src/channels/loomy.js +1032 -1032
- package/src/channels/miaoshou.js +1396 -1396
- package/src/channels/mimo.js +474 -474
- package/src/channels/minimax.js +1451 -1451
- package/src/channels/qoder-wasm.js +434 -434
- package/src/channels/qoder.js +1619 -1619
- package/src/channels/raccoon.js +1842 -1842
- package/src/channels/tabbit.js +1457 -1457
- package/src/channels/trae.js +1405 -1405
- package/src/channels/zcode-global.js +35 -35
- package/src/channels/zcode.js +2085 -2085
- package/src/checkin.js +496 -496
- package/src/credits.js +464 -464
- package/src/crypto.js +132 -132
- package/src/free-models.js +683 -683
- package/src/gateway.js +596 -578
- package/src/health.js +86 -86
- package/src/model-key.js +35 -35
- package/src/paths.js +96 -96
- package/src/probe.js +318 -318
- package/src/providers/builtin.js +626 -626
- package/src/providers/index.js +483 -457
- package/src/providers/kilo.js +111 -111
- package/src/providers/last-good.js +69 -69
- package/src/providers/modelsdev.js +242 -242
- package/src/providers/openai-compat.js +312 -312
- package/src/providers/opencode/bun-fetch.js +226 -226
- package/src/providers/opencode/lane.js +186 -186
- package/src/providers/opencode/o2a/LICENSE.dsh-our-free-model +21 -21
- package/src/providers/opencode/o2a/asset-source.js +43 -43
- package/src/providers/opencode/o2a/assets/opencode_prompt_v2.txt +96 -96
- package/src/providers/opencode/o2a/assets/opencode_tools.json +327 -327
- package/src/providers/opencode/o2a/entry.js +17 -17
- package/src/providers/opencode/o2a/gateway-body.js +42 -42
- package/src/providers/opencode/o2a/headers.js +133 -133
- package/src/providers/opencode/o2a/identity.js +148 -148
- package/src/providers/opencode/o2a/official-prompt.js +191 -191
- package/src/providers/opencode/o2a/official-tools.js +110 -110
- package/src/providers/opencode/o2a/tool-renames.js +217 -217
- package/src/providers/opencode/o2a/toolshape.js +418 -418
- package/src/providers/opencode/o2a/version.js +37 -37
- package/src/providers/opencode/upstream.js +403 -403
- package/src/providers/opencode/wire.js +496 -496
- package/src/providers/opencode.js +652 -652
- package/src/router.js +629 -629
- package/src/server.js +33 -33
- package/src/store.js +467 -467
- package/src/sync/index.js +1529 -1529
- package/src/sync/login-preload.cjs +52 -52
- package/src/sync/noise.js +141 -141
- package/src/sync/relay-http.js +176 -176
- package/src/sync/remote-host.js +818 -818
- package/src/sync/remote-http.js +112 -112
- package/src/sync/remote-login.js +657 -657
- package/src/sync/remote-pairing.js +204 -204
- package/src/sync/remote-protocol.js +321 -321
- package/src/sync/snapshot.js +211 -211
- package/src/util.js +334 -334
- package/web/app.js +3972 -3972
- package/web/index.html +32 -32
- package/web/style.css +971 -971
|
@@ -1,191 +1,191 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The official CLI system prompt the free tier's abuse checks expect.
|
|
3
|
-
*
|
|
4
|
-
* The free tier accepts only agent-shaped requests, and its checks validate
|
|
5
|
-
* the system prompt the official CLI sends. `assets/opencode_prompt_v2.txt` is
|
|
6
|
-
* the CLI's own wording, and the
|
|
7
|
-
* environment block plus the built-in skill listing are appended exactly as the
|
|
8
|
-
* CLI assembles them. The marker (`opencodePromptMarker`) keeps a client that
|
|
9
|
-
* already sends the prompt from getting a duplicate.
|
|
10
|
-
*
|
|
11
|
-
* Injection per wire (`injectOfficialPrompt` → the three injectors):
|
|
12
|
-
* - Responses: the prompt is prepended to `instructions`.
|
|
13
|
-
* - Anthropic: the prompt is prepended to the top-level `system`.
|
|
14
|
-
* - Chat: the prompt is prepended to the first system/developer message.
|
|
15
|
-
*
|
|
16
|
-
* One documented deviation, in the Chat branch: folding every system/developer
|
|
17
|
-
* message's text into one rebuilt
|
|
18
|
-
* message would drop block objects — and the caller must preserve a
|
|
19
|
-
* caller's blocks (their identity and any `cache_control` marking survive the
|
|
20
|
-
* round trip). Here the prompt is prepended to the first system/developer
|
|
21
|
-
* message — a new text block for block content, a join for string content —
|
|
22
|
-
* and leaves additional system messages in place. The marker check upstream
|
|
23
|
-
* reads the prompt text, not the message count, so the gate is satisfied the
|
|
24
|
-
* same way.
|
|
25
|
-
*
|
|
26
|
-
* @module src/o2a/official-prompt.js
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
import { officialPromptText } from './asset-source.js'
|
|
30
|
-
|
|
31
|
-
let promptCache
|
|
32
|
-
|
|
33
|
-
/** The CLI's base prompt, read once. */
|
|
34
|
-
export function officialPrompt() {
|
|
35
|
-
if (promptCache !== undefined) return promptCache
|
|
36
|
-
promptCache = officialPromptText()
|
|
37
|
-
return promptCache
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Identifies a body that already carries the official prompt. */
|
|
41
|
-
export const OFFICIAL_PROMPT_MARKER = 'You are opencode, an interactive CLI tool'
|
|
42
|
-
|
|
43
|
-
/** The built-in skill listing the CLI appends after its environment block. */
|
|
44
|
-
export const OFFICIAL_SKILLS = `Skills provide specialized instructions and workflows for specific tasks.
|
|
45
|
-
Use the skill tool to load a skill when a task matches its description.
|
|
46
|
-
<available_skills>
|
|
47
|
-
<skill>
|
|
48
|
-
<name>customize-opencode</name>
|
|
49
|
-
<description>Use ONLY when the user is editing or creating opencode's own configuration: opencode.json, opencode.jsonc, files under .opencode/, or files under ~/.config/opencode/. Also use when creating or fixing opencode agents, subagents, skills, plugins, MCP servers, or permission rules. Do not use for the user's own application code, or for any project that is not configuring opencode itself.</description>
|
|
50
|
-
<location><built-in></location>
|
|
51
|
-
</skill>
|
|
52
|
-
</available_skills>`
|
|
53
|
-
|
|
54
|
-
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
|
55
|
-
const WEEKDAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
|
|
56
|
-
|
|
57
|
-
// The working directory is resolved once per process; a
|
|
58
|
-
// platform without one falls back to the official container path.
|
|
59
|
-
let cachedWorkDir
|
|
60
|
-
|
|
61
|
-
function workDir() {
|
|
62
|
-
if (cachedWorkDir !== undefined) return cachedWorkDir
|
|
63
|
-
let directory = ''
|
|
64
|
-
try { directory = process.cwd() } catch { directory = '' }
|
|
65
|
-
directory = String(directory).trim()
|
|
66
|
-
if (directory === '' || directory === '.') directory = '/workspace'
|
|
67
|
-
cachedWorkDir = directory
|
|
68
|
-
return directory
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** The `<env>` block the CLI puts after its base prompt. */
|
|
72
|
-
export function officialEnvironment(model, directory = workDir()) {
|
|
73
|
-
const apiModel = String(model ?? '').replace(/^opencode\//, '').trim() || 'unknown'
|
|
74
|
-
const platform = process.platform === 'win32' ? 'win32' : process.platform
|
|
75
|
-
const now = new Date()
|
|
76
|
-
const date = `${WEEKDAYS[now.getUTCDay()]} ${MONTHS[now.getUTCMonth()]} ${String(now.getUTCDate()).padStart(2, '0')} ${now.getUTCFullYear()}`
|
|
77
|
-
return [
|
|
78
|
-
`You are powered by the model named ${apiModel}. The exact model ID is opencode/${apiModel}`,
|
|
79
|
-
'Here is some useful information about the environment you are running in:',
|
|
80
|
-
'<env>',
|
|
81
|
-
` Working directory: ${directory}`,
|
|
82
|
-
` Workspace root folder: ${directory}`,
|
|
83
|
-
' Is directory a git repo: yes',
|
|
84
|
-
` Platform: ${platform}`,
|
|
85
|
-
` Today's date: ${date}`,
|
|
86
|
-
'</env>',
|
|
87
|
-
].join('\n')
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** The full CLI system prompt for `model`. */
|
|
91
|
-
export function officialSystemPrompt(model, directory) {
|
|
92
|
-
const base = officialPrompt()
|
|
93
|
-
if (base === '') return ''
|
|
94
|
-
return `${base}${officialEnvironment(model, directory)}\n${OFFICIAL_SKILLS}`
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** Flatten a value into its text fragments: string, block list, or block object. */
|
|
98
|
-
export function textFragments(value) {
|
|
99
|
-
if (typeof value === 'string') return value.trim() === '' ? [] : [value]
|
|
100
|
-
if (Array.isArray(value)) return value.flatMap(item => textFragments(item))
|
|
101
|
-
if (value && typeof value === 'object') {
|
|
102
|
-
if (typeof value.text === 'string') return textFragments(value.text)
|
|
103
|
-
if (value.content !== undefined) return textFragments(value.content)
|
|
104
|
-
}
|
|
105
|
-
return []
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** Whether any text inside `value` already contains the marker. */
|
|
109
|
-
export function carriesOpenCodePrompt(value) {
|
|
110
|
-
return textFragments(value).some(part => part.includes(OFFICIAL_PROMPT_MARKER))
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Prepend the official CLI system prompt to a shaped payload.
|
|
115
|
-
* Reports whether the payload changed; a body that
|
|
116
|
-
* already carries the marker is left untouched, so repeated shaping cannot
|
|
117
|
-
* duplicate the prompt. See the module header for the Chat-branch deviation.
|
|
118
|
-
*
|
|
119
|
-
* @param {object} payload - request body, mutated in place
|
|
120
|
-
* @param {'chat'|'flat'|'claude'} shape - the wire shape the body speaks
|
|
121
|
-
* @param {object} [options]
|
|
122
|
-
* @param {string} [options.workDir] - working directory for the env block
|
|
123
|
-
* @param {string} [options.model] - model for the env block; defaults to payload.model
|
|
124
|
-
* @returns {boolean}
|
|
125
|
-
*/
|
|
126
|
-
export function injectOfficialPrompt(payload, shape, { workDir, model } = {}) {
|
|
127
|
-
if (!payload || typeof payload !== 'object') return false
|
|
128
|
-
if (shape !== 'chat' && shape !== 'flat' && shape !== 'claude') return false
|
|
129
|
-
const prompt = officialSystemPrompt(model ?? payload.model, workDir)
|
|
130
|
-
if (prompt === '') return false
|
|
131
|
-
if (shape === 'claude') return injectAnthropicPrompt(payload, prompt)
|
|
132
|
-
if (shape === 'flat') return injectResponsesPrompt(payload, prompt)
|
|
133
|
-
return injectChatPrompt(payload, prompt)
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/** Prepend the prompt to the first system/developer message. */
|
|
137
|
-
function injectChatPrompt(payload, prompt) {
|
|
138
|
-
const messages = Array.isArray(payload.messages) ? payload.messages : []
|
|
139
|
-
let anchorIndex = -1
|
|
140
|
-
for (let i = 0; i < messages.length; i += 1) {
|
|
141
|
-
const message = messages[i]
|
|
142
|
-
if (!message || typeof message !== 'object') continue
|
|
143
|
-
const role = message.role
|
|
144
|
-
if (role !== 'system' && role !== 'developer') continue
|
|
145
|
-
if (carriesOpenCodePrompt(message.content)) return false
|
|
146
|
-
if (anchorIndex === -1) anchorIndex = i
|
|
147
|
-
}
|
|
148
|
-
if (anchorIndex === -1) {
|
|
149
|
-
payload.messages = [{ role: 'system', content: prompt }, ...messages]
|
|
150
|
-
return true
|
|
151
|
-
}
|
|
152
|
-
const anchor = messages[anchorIndex]
|
|
153
|
-
if (Array.isArray(anchor.content)) {
|
|
154
|
-
messages[anchorIndex] = { ...anchor, content: [{ type: 'text', text: prompt }, ...anchor.content] }
|
|
155
|
-
} else if (typeof anchor.content === 'string' && anchor.content.trim() !== '') {
|
|
156
|
-
messages[anchorIndex] = { ...anchor, content: `${prompt}\n\n${anchor.content}` }
|
|
157
|
-
} else {
|
|
158
|
-
messages[anchorIndex] = { ...anchor, content: prompt }
|
|
159
|
-
}
|
|
160
|
-
return true
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** Prepend the prompt to the top-level system value. */
|
|
164
|
-
function injectAnthropicPrompt(payload, prompt) {
|
|
165
|
-
const exists = payload.system !== undefined
|
|
166
|
-
if (exists && carriesOpenCodePrompt(payload.system)) return false
|
|
167
|
-
if (!exists) {
|
|
168
|
-
payload.system = prompt
|
|
169
|
-
return true
|
|
170
|
-
}
|
|
171
|
-
if (Array.isArray(payload.system)) {
|
|
172
|
-
payload.system = [{ type: 'text', text: prompt }, ...payload.system]
|
|
173
|
-
} else if (typeof payload.system === 'string' && payload.system.trim() !== '') {
|
|
174
|
-
payload.system = `${prompt}\n\n${payload.system}`
|
|
175
|
-
} else {
|
|
176
|
-
payload.system = prompt
|
|
177
|
-
}
|
|
178
|
-
return true
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/** Prepend the prompt to the Responses instructions. */
|
|
182
|
-
function injectResponsesPrompt(payload, prompt) {
|
|
183
|
-
const exists = payload.instructions !== undefined
|
|
184
|
-
if (exists && carriesOpenCodePrompt(payload.instructions)) return false
|
|
185
|
-
if (exists && typeof payload.instructions === 'string' && payload.instructions.trim() !== '') {
|
|
186
|
-
payload.instructions = `${prompt}\n\n${payload.instructions}`
|
|
187
|
-
return true
|
|
188
|
-
}
|
|
189
|
-
payload.instructions = prompt
|
|
190
|
-
return true
|
|
191
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The official CLI system prompt the free tier's abuse checks expect.
|
|
3
|
+
*
|
|
4
|
+
* The free tier accepts only agent-shaped requests, and its checks validate
|
|
5
|
+
* the system prompt the official CLI sends. `assets/opencode_prompt_v2.txt` is
|
|
6
|
+
* the CLI's own wording, and the
|
|
7
|
+
* environment block plus the built-in skill listing are appended exactly as the
|
|
8
|
+
* CLI assembles them. The marker (`opencodePromptMarker`) keeps a client that
|
|
9
|
+
* already sends the prompt from getting a duplicate.
|
|
10
|
+
*
|
|
11
|
+
* Injection per wire (`injectOfficialPrompt` → the three injectors):
|
|
12
|
+
* - Responses: the prompt is prepended to `instructions`.
|
|
13
|
+
* - Anthropic: the prompt is prepended to the top-level `system`.
|
|
14
|
+
* - Chat: the prompt is prepended to the first system/developer message.
|
|
15
|
+
*
|
|
16
|
+
* One documented deviation, in the Chat branch: folding every system/developer
|
|
17
|
+
* message's text into one rebuilt
|
|
18
|
+
* message would drop block objects — and the caller must preserve a
|
|
19
|
+
* caller's blocks (their identity and any `cache_control` marking survive the
|
|
20
|
+
* round trip). Here the prompt is prepended to the first system/developer
|
|
21
|
+
* message — a new text block for block content, a join for string content —
|
|
22
|
+
* and leaves additional system messages in place. The marker check upstream
|
|
23
|
+
* reads the prompt text, not the message count, so the gate is satisfied the
|
|
24
|
+
* same way.
|
|
25
|
+
*
|
|
26
|
+
* @module src/o2a/official-prompt.js
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { officialPromptText } from './asset-source.js'
|
|
30
|
+
|
|
31
|
+
let promptCache
|
|
32
|
+
|
|
33
|
+
/** The CLI's base prompt, read once. */
|
|
34
|
+
export function officialPrompt() {
|
|
35
|
+
if (promptCache !== undefined) return promptCache
|
|
36
|
+
promptCache = officialPromptText()
|
|
37
|
+
return promptCache
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Identifies a body that already carries the official prompt. */
|
|
41
|
+
export const OFFICIAL_PROMPT_MARKER = 'You are opencode, an interactive CLI tool'
|
|
42
|
+
|
|
43
|
+
/** The built-in skill listing the CLI appends after its environment block. */
|
|
44
|
+
export const OFFICIAL_SKILLS = `Skills provide specialized instructions and workflows for specific tasks.
|
|
45
|
+
Use the skill tool to load a skill when a task matches its description.
|
|
46
|
+
<available_skills>
|
|
47
|
+
<skill>
|
|
48
|
+
<name>customize-opencode</name>
|
|
49
|
+
<description>Use ONLY when the user is editing or creating opencode's own configuration: opencode.json, opencode.jsonc, files under .opencode/, or files under ~/.config/opencode/. Also use when creating or fixing opencode agents, subagents, skills, plugins, MCP servers, or permission rules. Do not use for the user's own application code, or for any project that is not configuring opencode itself.</description>
|
|
50
|
+
<location><built-in></location>
|
|
51
|
+
</skill>
|
|
52
|
+
</available_skills>`
|
|
53
|
+
|
|
54
|
+
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
|
55
|
+
const WEEKDAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
|
|
56
|
+
|
|
57
|
+
// The working directory is resolved once per process; a
|
|
58
|
+
// platform without one falls back to the official container path.
|
|
59
|
+
let cachedWorkDir
|
|
60
|
+
|
|
61
|
+
function workDir() {
|
|
62
|
+
if (cachedWorkDir !== undefined) return cachedWorkDir
|
|
63
|
+
let directory = ''
|
|
64
|
+
try { directory = process.cwd() } catch { directory = '' }
|
|
65
|
+
directory = String(directory).trim()
|
|
66
|
+
if (directory === '' || directory === '.') directory = '/workspace'
|
|
67
|
+
cachedWorkDir = directory
|
|
68
|
+
return directory
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The `<env>` block the CLI puts after its base prompt. */
|
|
72
|
+
export function officialEnvironment(model, directory = workDir()) {
|
|
73
|
+
const apiModel = String(model ?? '').replace(/^opencode\//, '').trim() || 'unknown'
|
|
74
|
+
const platform = process.platform === 'win32' ? 'win32' : process.platform
|
|
75
|
+
const now = new Date()
|
|
76
|
+
const date = `${WEEKDAYS[now.getUTCDay()]} ${MONTHS[now.getUTCMonth()]} ${String(now.getUTCDate()).padStart(2, '0')} ${now.getUTCFullYear()}`
|
|
77
|
+
return [
|
|
78
|
+
`You are powered by the model named ${apiModel}. The exact model ID is opencode/${apiModel}`,
|
|
79
|
+
'Here is some useful information about the environment you are running in:',
|
|
80
|
+
'<env>',
|
|
81
|
+
` Working directory: ${directory}`,
|
|
82
|
+
` Workspace root folder: ${directory}`,
|
|
83
|
+
' Is directory a git repo: yes',
|
|
84
|
+
` Platform: ${platform}`,
|
|
85
|
+
` Today's date: ${date}`,
|
|
86
|
+
'</env>',
|
|
87
|
+
].join('\n')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The full CLI system prompt for `model`. */
|
|
91
|
+
export function officialSystemPrompt(model, directory) {
|
|
92
|
+
const base = officialPrompt()
|
|
93
|
+
if (base === '') return ''
|
|
94
|
+
return `${base}${officialEnvironment(model, directory)}\n${OFFICIAL_SKILLS}`
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Flatten a value into its text fragments: string, block list, or block object. */
|
|
98
|
+
export function textFragments(value) {
|
|
99
|
+
if (typeof value === 'string') return value.trim() === '' ? [] : [value]
|
|
100
|
+
if (Array.isArray(value)) return value.flatMap(item => textFragments(item))
|
|
101
|
+
if (value && typeof value === 'object') {
|
|
102
|
+
if (typeof value.text === 'string') return textFragments(value.text)
|
|
103
|
+
if (value.content !== undefined) return textFragments(value.content)
|
|
104
|
+
}
|
|
105
|
+
return []
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Whether any text inside `value` already contains the marker. */
|
|
109
|
+
export function carriesOpenCodePrompt(value) {
|
|
110
|
+
return textFragments(value).some(part => part.includes(OFFICIAL_PROMPT_MARKER))
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Prepend the official CLI system prompt to a shaped payload.
|
|
115
|
+
* Reports whether the payload changed; a body that
|
|
116
|
+
* already carries the marker is left untouched, so repeated shaping cannot
|
|
117
|
+
* duplicate the prompt. See the module header for the Chat-branch deviation.
|
|
118
|
+
*
|
|
119
|
+
* @param {object} payload - request body, mutated in place
|
|
120
|
+
* @param {'chat'|'flat'|'claude'} shape - the wire shape the body speaks
|
|
121
|
+
* @param {object} [options]
|
|
122
|
+
* @param {string} [options.workDir] - working directory for the env block
|
|
123
|
+
* @param {string} [options.model] - model for the env block; defaults to payload.model
|
|
124
|
+
* @returns {boolean}
|
|
125
|
+
*/
|
|
126
|
+
export function injectOfficialPrompt(payload, shape, { workDir, model } = {}) {
|
|
127
|
+
if (!payload || typeof payload !== 'object') return false
|
|
128
|
+
if (shape !== 'chat' && shape !== 'flat' && shape !== 'claude') return false
|
|
129
|
+
const prompt = officialSystemPrompt(model ?? payload.model, workDir)
|
|
130
|
+
if (prompt === '') return false
|
|
131
|
+
if (shape === 'claude') return injectAnthropicPrompt(payload, prompt)
|
|
132
|
+
if (shape === 'flat') return injectResponsesPrompt(payload, prompt)
|
|
133
|
+
return injectChatPrompt(payload, prompt)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Prepend the prompt to the first system/developer message. */
|
|
137
|
+
function injectChatPrompt(payload, prompt) {
|
|
138
|
+
const messages = Array.isArray(payload.messages) ? payload.messages : []
|
|
139
|
+
let anchorIndex = -1
|
|
140
|
+
for (let i = 0; i < messages.length; i += 1) {
|
|
141
|
+
const message = messages[i]
|
|
142
|
+
if (!message || typeof message !== 'object') continue
|
|
143
|
+
const role = message.role
|
|
144
|
+
if (role !== 'system' && role !== 'developer') continue
|
|
145
|
+
if (carriesOpenCodePrompt(message.content)) return false
|
|
146
|
+
if (anchorIndex === -1) anchorIndex = i
|
|
147
|
+
}
|
|
148
|
+
if (anchorIndex === -1) {
|
|
149
|
+
payload.messages = [{ role: 'system', content: prompt }, ...messages]
|
|
150
|
+
return true
|
|
151
|
+
}
|
|
152
|
+
const anchor = messages[anchorIndex]
|
|
153
|
+
if (Array.isArray(anchor.content)) {
|
|
154
|
+
messages[anchorIndex] = { ...anchor, content: [{ type: 'text', text: prompt }, ...anchor.content] }
|
|
155
|
+
} else if (typeof anchor.content === 'string' && anchor.content.trim() !== '') {
|
|
156
|
+
messages[anchorIndex] = { ...anchor, content: `${prompt}\n\n${anchor.content}` }
|
|
157
|
+
} else {
|
|
158
|
+
messages[anchorIndex] = { ...anchor, content: prompt }
|
|
159
|
+
}
|
|
160
|
+
return true
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Prepend the prompt to the top-level system value. */
|
|
164
|
+
function injectAnthropicPrompt(payload, prompt) {
|
|
165
|
+
const exists = payload.system !== undefined
|
|
166
|
+
if (exists && carriesOpenCodePrompt(payload.system)) return false
|
|
167
|
+
if (!exists) {
|
|
168
|
+
payload.system = prompt
|
|
169
|
+
return true
|
|
170
|
+
}
|
|
171
|
+
if (Array.isArray(payload.system)) {
|
|
172
|
+
payload.system = [{ type: 'text', text: prompt }, ...payload.system]
|
|
173
|
+
} else if (typeof payload.system === 'string' && payload.system.trim() !== '') {
|
|
174
|
+
payload.system = `${prompt}\n\n${payload.system}`
|
|
175
|
+
} else {
|
|
176
|
+
payload.system = prompt
|
|
177
|
+
}
|
|
178
|
+
return true
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Prepend the prompt to the Responses instructions. */
|
|
182
|
+
function injectResponsesPrompt(payload, prompt) {
|
|
183
|
+
const exists = payload.instructions !== undefined
|
|
184
|
+
if (exists && carriesOpenCodePrompt(payload.instructions)) return false
|
|
185
|
+
if (exists && typeof payload.instructions === 'string' && payload.instructions.trim() !== '') {
|
|
186
|
+
payload.instructions = `${prompt}\n\n${payload.instructions}`
|
|
187
|
+
return true
|
|
188
|
+
}
|
|
189
|
+
payload.instructions = prompt
|
|
190
|
+
return true
|
|
191
|
+
}
|
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The official OpenCode
|
|
3
|
-
* toolset that makes a shaped body read as a genuine CLI session.
|
|
4
|
-
*
|
|
5
|
-
* The free tier validates agent-shaped traffic per lane generation. The
|
|
6
|
-
* **complete** official tool set of the 2.x client
|
|
7
|
-
* (13 tools, captured verbatim in `assets/opencode_tools.json`) is appended
|
|
8
|
-
* after renaming every client tool. An earlier two-tool gate
|
|
9
|
-
* (`read` + `shell` only) is a strict subset:
|
|
10
|
-
* those two definitions in the captured file are byte-identical to the ones
|
|
11
|
-
* that were hardcoded.
|
|
12
|
-
*
|
|
13
|
-
* Rendering per wire shape (`officialTool`): the 2.x Chat shape marks every
|
|
14
|
-
* tool `strict: false`, as the client does; the flat Responses shape and the
|
|
15
|
-
* Anthropic Messages shape carry the captured definitions verbatim. The shell
|
|
16
|
-
* description names the shell the host actually has — a Windows host says
|
|
17
|
-
* powershell where a Unix host says bash (`v2ShellDescription`).
|
|
18
|
-
*
|
|
19
|
-
* @module src/o2a/official-tools.js
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
import { officialToolsText } from './asset-source.js'
|
|
23
|
-
|
|
24
|
-
/** Tool order of the captured file, made explicit because JSON key order is not guaranteed. */
|
|
25
|
-
export const V2_OFFICIAL_TOOL_ORDER = Object.freeze([
|
|
26
|
-
'edit', 'execute', 'glob', 'grep', 'question', 'read', 'shell',
|
|
27
|
-
'skill', 'subagent', 'webfetch', 'websearch', 'write', 'patch',
|
|
28
|
-
])
|
|
29
|
-
|
|
30
|
-
let officialTools
|
|
31
|
-
|
|
32
|
-
function loadOfficialTools() {
|
|
33
|
-
if (officialTools !== undefined) return officialTools
|
|
34
|
-
officialTools = new Map()
|
|
35
|
-
try {
|
|
36
|
-
const parsed = JSON.parse(officialToolsText())
|
|
37
|
-
for (const [name, definition] of Object.entries(parsed)) {
|
|
38
|
-
if (definition && typeof definition.description === 'string' && definition.parameters) {
|
|
39
|
-
officialTools.set(name, definition)
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
} catch {
|
|
43
|
-
// An unreadable asset degrades every definition to the placeholder below;
|
|
44
|
-
// the names are still appended, which is what the gate checks first.
|
|
45
|
-
}
|
|
46
|
-
return officialTools
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** The captured definition for one official name. */
|
|
50
|
-
export function officialToolDefinition(name) {
|
|
51
|
-
return loadOfficialTools().get(name)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Render the shell tool description for the host platform.
|
|
56
|
-
* The 2.x client describes the shell it actually has.
|
|
57
|
-
*/
|
|
58
|
-
export function v2ShellDescription(base, platform = process.platform) {
|
|
59
|
-
if (platform !== 'win32') return base
|
|
60
|
-
return base.replace('on Linux using bash', 'on Windows using powershell')
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** Render one official definition in the wire shape the protocol expects. */
|
|
64
|
-
export function officialTool(shape, name) {
|
|
65
|
-
let description = `Agent tool ${name}`
|
|
66
|
-
let parameters = { type: 'object', properties: {} }
|
|
67
|
-
const definition = officialToolDefinition(name)
|
|
68
|
-
if (definition !== undefined) {
|
|
69
|
-
description = definition.description
|
|
70
|
-
if (definition.parameters != null) parameters = structuredClone(definition.parameters)
|
|
71
|
-
}
|
|
72
|
-
if (name === 'shell') description = v2ShellDescription(description)
|
|
73
|
-
if (shape === 'claude') {
|
|
74
|
-
return { name, description, input_schema: parameters }
|
|
75
|
-
}
|
|
76
|
-
if (shape === 'flat') {
|
|
77
|
-
return { type: 'function', name, description, parameters }
|
|
78
|
-
}
|
|
79
|
-
return {
|
|
80
|
-
type: 'function',
|
|
81
|
-
function: { name, description, parameters, strict: false },
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** The full official set for a wire shape, in captured order. */
|
|
86
|
-
export function officialToolset(shape) {
|
|
87
|
-
return V2_OFFICIAL_TOOL_ORDER.map(name => officialTool(shape, name))
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* The client tool name of one definition, or null when the entry is a built-in
|
|
92
|
-
* the shaper must leave alone: Responses built-in tools
|
|
93
|
-
* (web search and similar) and Anthropic server tools carry a `type` outside
|
|
94
|
-
* the function/custom spellings and have no client name.
|
|
95
|
-
*/
|
|
96
|
-
export function renamableToolName(shape, entry) {
|
|
97
|
-
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) return null
|
|
98
|
-
if (shape === 'chat') {
|
|
99
|
-
const name = entry.function?.name
|
|
100
|
-
return typeof name === 'string' && name !== '' ? name : null
|
|
101
|
-
}
|
|
102
|
-
const type = typeof entry.type === 'string' ? entry.type : ''
|
|
103
|
-
if (shape === 'flat') {
|
|
104
|
-
if (type !== '' && type !== 'function' && type !== 'custom') return null
|
|
105
|
-
} else if (shape === 'claude') {
|
|
106
|
-
if (type !== '' && type !== 'custom') return null
|
|
107
|
-
}
|
|
108
|
-
const name = entry.name
|
|
109
|
-
return typeof name === 'string' && name !== '' ? name : null
|
|
110
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The official OpenCode
|
|
3
|
+
* toolset that makes a shaped body read as a genuine CLI session.
|
|
4
|
+
*
|
|
5
|
+
* The free tier validates agent-shaped traffic per lane generation. The
|
|
6
|
+
* **complete** official tool set of the 2.x client
|
|
7
|
+
* (13 tools, captured verbatim in `assets/opencode_tools.json`) is appended
|
|
8
|
+
* after renaming every client tool. An earlier two-tool gate
|
|
9
|
+
* (`read` + `shell` only) is a strict subset:
|
|
10
|
+
* those two definitions in the captured file are byte-identical to the ones
|
|
11
|
+
* that were hardcoded.
|
|
12
|
+
*
|
|
13
|
+
* Rendering per wire shape (`officialTool`): the 2.x Chat shape marks every
|
|
14
|
+
* tool `strict: false`, as the client does; the flat Responses shape and the
|
|
15
|
+
* Anthropic Messages shape carry the captured definitions verbatim. The shell
|
|
16
|
+
* description names the shell the host actually has — a Windows host says
|
|
17
|
+
* powershell where a Unix host says bash (`v2ShellDescription`).
|
|
18
|
+
*
|
|
19
|
+
* @module src/o2a/official-tools.js
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { officialToolsText } from './asset-source.js'
|
|
23
|
+
|
|
24
|
+
/** Tool order of the captured file, made explicit because JSON key order is not guaranteed. */
|
|
25
|
+
export const V2_OFFICIAL_TOOL_ORDER = Object.freeze([
|
|
26
|
+
'edit', 'execute', 'glob', 'grep', 'question', 'read', 'shell',
|
|
27
|
+
'skill', 'subagent', 'webfetch', 'websearch', 'write', 'patch',
|
|
28
|
+
])
|
|
29
|
+
|
|
30
|
+
let officialTools
|
|
31
|
+
|
|
32
|
+
function loadOfficialTools() {
|
|
33
|
+
if (officialTools !== undefined) return officialTools
|
|
34
|
+
officialTools = new Map()
|
|
35
|
+
try {
|
|
36
|
+
const parsed = JSON.parse(officialToolsText())
|
|
37
|
+
for (const [name, definition] of Object.entries(parsed)) {
|
|
38
|
+
if (definition && typeof definition.description === 'string' && definition.parameters) {
|
|
39
|
+
officialTools.set(name, definition)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} catch {
|
|
43
|
+
// An unreadable asset degrades every definition to the placeholder below;
|
|
44
|
+
// the names are still appended, which is what the gate checks first.
|
|
45
|
+
}
|
|
46
|
+
return officialTools
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The captured definition for one official name. */
|
|
50
|
+
export function officialToolDefinition(name) {
|
|
51
|
+
return loadOfficialTools().get(name)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Render the shell tool description for the host platform.
|
|
56
|
+
* The 2.x client describes the shell it actually has.
|
|
57
|
+
*/
|
|
58
|
+
export function v2ShellDescription(base, platform = process.platform) {
|
|
59
|
+
if (platform !== 'win32') return base
|
|
60
|
+
return base.replace('on Linux using bash', 'on Windows using powershell')
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Render one official definition in the wire shape the protocol expects. */
|
|
64
|
+
export function officialTool(shape, name) {
|
|
65
|
+
let description = `Agent tool ${name}`
|
|
66
|
+
let parameters = { type: 'object', properties: {} }
|
|
67
|
+
const definition = officialToolDefinition(name)
|
|
68
|
+
if (definition !== undefined) {
|
|
69
|
+
description = definition.description
|
|
70
|
+
if (definition.parameters != null) parameters = structuredClone(definition.parameters)
|
|
71
|
+
}
|
|
72
|
+
if (name === 'shell') description = v2ShellDescription(description)
|
|
73
|
+
if (shape === 'claude') {
|
|
74
|
+
return { name, description, input_schema: parameters }
|
|
75
|
+
}
|
|
76
|
+
if (shape === 'flat') {
|
|
77
|
+
return { type: 'function', name, description, parameters }
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
type: 'function',
|
|
81
|
+
function: { name, description, parameters, strict: false },
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The full official set for a wire shape, in captured order. */
|
|
86
|
+
export function officialToolset(shape) {
|
|
87
|
+
return V2_OFFICIAL_TOOL_ORDER.map(name => officialTool(shape, name))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The client tool name of one definition, or null when the entry is a built-in
|
|
92
|
+
* the shaper must leave alone: Responses built-in tools
|
|
93
|
+
* (web search and similar) and Anthropic server tools carry a `type` outside
|
|
94
|
+
* the function/custom spellings and have no client name.
|
|
95
|
+
*/
|
|
96
|
+
export function renamableToolName(shape, entry) {
|
|
97
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) return null
|
|
98
|
+
if (shape === 'chat') {
|
|
99
|
+
const name = entry.function?.name
|
|
100
|
+
return typeof name === 'string' && name !== '' ? name : null
|
|
101
|
+
}
|
|
102
|
+
const type = typeof entry.type === 'string' ? entry.type : ''
|
|
103
|
+
if (shape === 'flat') {
|
|
104
|
+
if (type !== '' && type !== 'function' && type !== 'custom') return null
|
|
105
|
+
} else if (shape === 'claude') {
|
|
106
|
+
if (type !== '' && type !== 'custom') return null
|
|
107
|
+
}
|
|
108
|
+
const name = entry.name
|
|
109
|
+
return typeof name === 'string' && name !== '' ? name : null
|
|
110
|
+
}
|