@osovv/vv-opencode 2.1.1 → 2.1.3
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 +19 -0
- package/README.md +6 -2
- package/dist/commands/sync.js +18 -3
- package/dist/commands/sync.js.map +1 -1
- package/dist/commands/upgrade.d.ts +5 -0
- package/dist/commands/upgrade.js +64 -5
- package/dist/commands/upgrade.js.map +1 -1
- package/dist/lib/opencode/agent-registrations.js +2 -2
- package/dist/lib/opencode/agent-registrations.js.map +1 -1
- package/dist/lib/opencode/config-migration.d.ts +24 -0
- package/dist/lib/opencode/config-migration.js +764 -0
- package/dist/lib/opencode/config-migration.js.map +1 -0
- package/dist/lib/opencode.d.ts +2 -0
- package/dist/lib/opencode.js +4 -2
- package/dist/lib/opencode.js.map +1 -1
- package/package.json +2 -3
- package/schemas/vvoc/v3.json +1 -1
- package/templates/skills/vv-execute/references/tool-contracts.md +1 -1
|
@@ -0,0 +1,764 @@
|
|
|
1
|
+
// FILE: src/lib/opencode/config-migration.ts
|
|
2
|
+
// VERSION: 1.0.0
|
|
3
|
+
// START_MODULE_CONTRACT
|
|
4
|
+
// PURPOSE: Materialize a legacy V1 OpenCode configuration into the native V2 shape described by the official V1-to-V2 migration guide, with a timestamped backup and a validate-before-write guarantee.
|
|
5
|
+
// SCOPE: V1-shape classification, guide-aligned top-level and nested transformation planning with wholly-or-not-at-all nested entries, report-only field detection, vvoc role-reference removal, native codec validation, and atomic backup-plus-write orchestration.
|
|
6
|
+
// DEPENDS: [jsonc-parser, node:path, src/lib/opencode/shared-utils.ts, src/lib/opencode/paths.ts]
|
|
7
|
+
// LINKS: [M-CLI-CONFIG, V-M-CLI-CONFIG]
|
|
8
|
+
// ROLE: RUNTIME
|
|
9
|
+
// MAP_MODE: EXPORTS
|
|
10
|
+
// END_MODULE_CONTRACT
|
|
11
|
+
//
|
|
12
|
+
// START_MODULE_MAP
|
|
13
|
+
// MigrationShape - Classification result tag for an OpenCode document.
|
|
14
|
+
// MigrationClassification - Shape plus the V1 signals that produced it.
|
|
15
|
+
// OpenCodeConfigMigrationPlan - Planned native document, change flag, and report-only/unmappable field lists.
|
|
16
|
+
// OpenCodeConfigMigrationResult - Outcome of a migration attempt against a resolved path bundle.
|
|
17
|
+
// classifyOpenCodeConfig - Classifies a document as native-clean or V1-shaped.
|
|
18
|
+
// planOpenCodeConfigMigration - Builds the native target document and the report-only/unmappable lists.
|
|
19
|
+
// migrateOpenCodeConfig - Reads, plans, validates, backs up, and writes the migrated OpenCode config.
|
|
20
|
+
// END_MODULE_MAP
|
|
21
|
+
//
|
|
22
|
+
// START_CHANGE_SUMMARY
|
|
23
|
+
// LAST_CHANGE: [C-V1-OPENCODE-CONFIG-MIGRATION T-001 - Added the guide-aligned V1-to-V2 OpenCode config materializer with backup and validate-before-write.]
|
|
24
|
+
// END_CHANGE_SUMMARY
|
|
25
|
+
import { applyEdits, modify } from "jsonc-parser";
|
|
26
|
+
import { dirname, join } from "node:path";
|
|
27
|
+
import { assertNativeOpenCodeDocument, ensureTrailingNewline, isJsonObject, normalizeNativeModelSelection, OPENCODE_SCHEMA_URL, parseObjectDocument, readOptionalText, writeText, } from "./shared-utils.js";
|
|
28
|
+
const TOP_LEVEL_V1_KEYS = [
|
|
29
|
+
"plugin",
|
|
30
|
+
"agent",
|
|
31
|
+
"mode",
|
|
32
|
+
"provider",
|
|
33
|
+
"command",
|
|
34
|
+
"small_model",
|
|
35
|
+
"tools",
|
|
36
|
+
"permission",
|
|
37
|
+
"reference",
|
|
38
|
+
"snapshot",
|
|
39
|
+
"attachment",
|
|
40
|
+
"autoshare",
|
|
41
|
+
"autoupdate",
|
|
42
|
+
];
|
|
43
|
+
/** V1 top-level fields that have no supported native behavior and are reported, never converted. */
|
|
44
|
+
const REPORT_ONLY_TOP_LEVEL = [
|
|
45
|
+
"enabled_providers",
|
|
46
|
+
"disabled_providers",
|
|
47
|
+
"logLevel",
|
|
48
|
+
"server",
|
|
49
|
+
"subagent_depth",
|
|
50
|
+
"batch_tool",
|
|
51
|
+
"openTelemetry",
|
|
52
|
+
"primary_tools",
|
|
53
|
+
"continue_loop_on_deny",
|
|
54
|
+
];
|
|
55
|
+
const PROVIDER_ID_RENAMES = {
|
|
56
|
+
"azure-cognitive-services": "azure",
|
|
57
|
+
"google-vertex-anthropic": "google-vertex",
|
|
58
|
+
};
|
|
59
|
+
const PERMISSION_ACTION_RENAMES = {
|
|
60
|
+
bash: "shell",
|
|
61
|
+
task: "subagent",
|
|
62
|
+
write: "edit",
|
|
63
|
+
patch: "edit",
|
|
64
|
+
};
|
|
65
|
+
/** V1 provider fields without a native equivalent. */
|
|
66
|
+
const PROVIDER_REPORT_ONLY_FIELDS = ["id", "whitelist", "blacklist"];
|
|
67
|
+
/** V1 provider-model fields without a native equivalent. */
|
|
68
|
+
const PROVIDER_MODEL_LEGACY_FIELDS = [
|
|
69
|
+
"release_date",
|
|
70
|
+
"attachment",
|
|
71
|
+
"reasoning",
|
|
72
|
+
"temperature",
|
|
73
|
+
"experimental",
|
|
74
|
+
"interleaved",
|
|
75
|
+
];
|
|
76
|
+
const JSON_FORMAT = { insertSpaces: true, tabSize: 2, eol: "\n" };
|
|
77
|
+
function isRoleReference(value) {
|
|
78
|
+
return typeof value === "string" && value.trim().startsWith("vv-role:");
|
|
79
|
+
}
|
|
80
|
+
function cloneJson(value) {
|
|
81
|
+
if (Array.isArray(value))
|
|
82
|
+
return value.map((entry) => cloneJson(entry));
|
|
83
|
+
if (isJsonObject(value)) {
|
|
84
|
+
const out = {};
|
|
85
|
+
for (const [key, entry] of Object.entries(value))
|
|
86
|
+
out[key] = cloneJson(entry);
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
function asObject(value) {
|
|
92
|
+
return isJsonObject(value) ? value : {};
|
|
93
|
+
}
|
|
94
|
+
// START_CONTRACT: classifyOpenCodeConfig
|
|
95
|
+
// PURPOSE: Determine whether a parsed OpenCode document still carries V1 shapes that need materialization.
|
|
96
|
+
// INPUTS: { document: JsonObject - parsed OpenCode config document. }
|
|
97
|
+
// OUTPUTS: { MigrationClassification - shape plus the V1 signals observed. }
|
|
98
|
+
// SIDE_EFFECTS: none
|
|
99
|
+
// LINKS: [fn-planOpenCodeConfigMigration]
|
|
100
|
+
// END_CONTRACT: classifyOpenCodeConfig
|
|
101
|
+
export function classifyOpenCodeConfig(document) {
|
|
102
|
+
const reasons = [];
|
|
103
|
+
for (const key of TOP_LEVEL_V1_KEYS) {
|
|
104
|
+
if (Object.hasOwn(document, key))
|
|
105
|
+
reasons.push(`top-level V1 field "${key}"`);
|
|
106
|
+
}
|
|
107
|
+
for (const key of REPORT_ONLY_TOP_LEVEL) {
|
|
108
|
+
if (Object.hasOwn(document, key))
|
|
109
|
+
reasons.push(`report-only field "${key}"`);
|
|
110
|
+
}
|
|
111
|
+
if (isRoleReference(document.model))
|
|
112
|
+
reasons.push('role reference in "model"');
|
|
113
|
+
if (Object.hasOwn(document, "small_model"))
|
|
114
|
+
reasons.push('top-level V1 field "small_model"');
|
|
115
|
+
const agents = asObject(document.agents);
|
|
116
|
+
for (const [name, entry] of Object.entries(agents)) {
|
|
117
|
+
if (!isJsonObject(entry))
|
|
118
|
+
continue;
|
|
119
|
+
if (Object.hasOwn(entry, "prompt") ||
|
|
120
|
+
Object.hasOwn(entry, "permission") ||
|
|
121
|
+
Object.hasOwn(entry, "tools")) {
|
|
122
|
+
reasons.push(`V1 agent field in "agents.${name}"`);
|
|
123
|
+
}
|
|
124
|
+
if (isRoleReference(entry.model))
|
|
125
|
+
reasons.push(`role reference in "agents.${name}.model"`);
|
|
126
|
+
}
|
|
127
|
+
if (isJsonObject(document.skills) &&
|
|
128
|
+
(Object.hasOwn(document.skills, "paths") || Object.hasOwn(document.skills, "urls"))) {
|
|
129
|
+
reasons.push('V1 "skills" object form');
|
|
130
|
+
}
|
|
131
|
+
return { shape: reasons.length > 0 ? "v1" : "native-clean", reasons };
|
|
132
|
+
}
|
|
133
|
+
function convertPermissionMap(permission, collector) {
|
|
134
|
+
const entries = [];
|
|
135
|
+
if (permission === undefined)
|
|
136
|
+
return entries;
|
|
137
|
+
if (!isJsonObject(permission)) {
|
|
138
|
+
collector.unmappable.push('"permission" is not an object');
|
|
139
|
+
return entries;
|
|
140
|
+
}
|
|
141
|
+
for (const [rawAction, value] of Object.entries(permission)) {
|
|
142
|
+
const action = PERMISSION_ACTION_RENAMES[rawAction] ?? rawAction;
|
|
143
|
+
if (isJsonObject(value)) {
|
|
144
|
+
for (const [resource, effect] of Object.entries(value)) {
|
|
145
|
+
if (typeof effect !== "string") {
|
|
146
|
+
collector.unmappable.push(`"permission.${rawAction}.${resource}" effect is not a string`);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
entries.push({ action, resource, effect });
|
|
150
|
+
}
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (typeof value === "string") {
|
|
154
|
+
entries.push({ action, resource: "*", effect: value });
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
collector.unmappable.push(`"permission.${rawAction}" is neither an effect string nor an object`);
|
|
158
|
+
}
|
|
159
|
+
return entries;
|
|
160
|
+
}
|
|
161
|
+
function convertToolsMap(tools, collector) {
|
|
162
|
+
const entries = [];
|
|
163
|
+
if (tools === undefined)
|
|
164
|
+
return entries;
|
|
165
|
+
if (!isJsonObject(tools)) {
|
|
166
|
+
collector.unmappable.push('"tools" is not an object');
|
|
167
|
+
return entries;
|
|
168
|
+
}
|
|
169
|
+
for (const [rawAction, value] of Object.entries(tools)) {
|
|
170
|
+
if (typeof value !== "boolean") {
|
|
171
|
+
collector.unmappable.push(`"tools.${rawAction}" is not a boolean`);
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
entries.push({
|
|
175
|
+
action: PERMISSION_ACTION_RENAMES[rawAction] ?? rawAction,
|
|
176
|
+
resource: "*",
|
|
177
|
+
effect: value ? "allow" : "deny",
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return entries;
|
|
181
|
+
}
|
|
182
|
+
function convertAgentEntry(name, entry, collector) {
|
|
183
|
+
if (!isJsonObject(entry)) {
|
|
184
|
+
collector.unmappable.push(`"agents.${name}" is not an object`);
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
const out = {};
|
|
188
|
+
let variant;
|
|
189
|
+
for (const [key, value] of Object.entries(entry)) {
|
|
190
|
+
if (key === "prompt") {
|
|
191
|
+
out.system = value;
|
|
192
|
+
}
|
|
193
|
+
else if (key === "disable") {
|
|
194
|
+
out.disabled = value;
|
|
195
|
+
}
|
|
196
|
+
else if (key === "maxSteps") {
|
|
197
|
+
out.steps = value;
|
|
198
|
+
}
|
|
199
|
+
else if (key === "variant") {
|
|
200
|
+
if (typeof value === "string")
|
|
201
|
+
variant = value;
|
|
202
|
+
else
|
|
203
|
+
collector.unmappable.push(`"agents.${name}.variant" is not a string`);
|
|
204
|
+
}
|
|
205
|
+
else if (key === "temperature" || key === "top_p" || key === "options") {
|
|
206
|
+
const request = asObject(out.request);
|
|
207
|
+
const body = asObject(request.body);
|
|
208
|
+
if (key === "options" && isJsonObject(value)) {
|
|
209
|
+
request.body = { ...body, ...value };
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
body[key] = value;
|
|
213
|
+
request.body = body;
|
|
214
|
+
}
|
|
215
|
+
out.request = request;
|
|
216
|
+
}
|
|
217
|
+
else if (key === "permission") {
|
|
218
|
+
const converted = convertPermissionMap(value, collector);
|
|
219
|
+
if (converted.length > 0)
|
|
220
|
+
out.permissions = converted;
|
|
221
|
+
}
|
|
222
|
+
else if (key === "tools") {
|
|
223
|
+
const converted = convertToolsMap(value, collector);
|
|
224
|
+
if (converted.length > 0)
|
|
225
|
+
out.permissions = [
|
|
226
|
+
...(Array.isArray(out.permissions) ? out.permissions : []),
|
|
227
|
+
...converted,
|
|
228
|
+
];
|
|
229
|
+
}
|
|
230
|
+
else if (key === "model") {
|
|
231
|
+
if (isRoleReference(value)) {
|
|
232
|
+
// vvoc role intent; rebuilt from vvoc config by the existing pipeline.
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
out.model = value;
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
out[key] = value;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (variant !== undefined && typeof out.model === "string" && !out.model.includes("#")) {
|
|
242
|
+
out.model = `${out.model}#${variant}`;
|
|
243
|
+
}
|
|
244
|
+
else if (variant !== undefined && out.model === undefined) {
|
|
245
|
+
collector.unmappable.push(`"agents.${name}.variant" has no model to join`);
|
|
246
|
+
}
|
|
247
|
+
else if (variant !== undefined) {
|
|
248
|
+
collector.unmappable.push(`"agents.${name}.variant" cannot join a non-string model`);
|
|
249
|
+
}
|
|
250
|
+
return out;
|
|
251
|
+
}
|
|
252
|
+
function convertCommandEntry(name, entry, collector) {
|
|
253
|
+
if (!isJsonObject(entry)) {
|
|
254
|
+
collector.unmappable.push(`"commands.${name}" is not an object`);
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
const out = {};
|
|
258
|
+
let variant;
|
|
259
|
+
for (const [key, value] of Object.entries(entry)) {
|
|
260
|
+
if (key === "subtask") {
|
|
261
|
+
out.subagent = value;
|
|
262
|
+
}
|
|
263
|
+
else if (key === "variant") {
|
|
264
|
+
if (typeof value === "string")
|
|
265
|
+
variant = value;
|
|
266
|
+
else
|
|
267
|
+
collector.unmappable.push(`"commands.${name}.variant" is not a string`);
|
|
268
|
+
}
|
|
269
|
+
else if (key === "model") {
|
|
270
|
+
out.model = value;
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
out[key] = value;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (variant !== undefined && typeof out.model === "string" && !out.model.includes("#")) {
|
|
277
|
+
out.model = `${out.model}#${variant}`;
|
|
278
|
+
}
|
|
279
|
+
else if (variant !== undefined) {
|
|
280
|
+
collector.unmappable.push(`"commands.${name}.variant" cannot join a non-string model`);
|
|
281
|
+
}
|
|
282
|
+
return out;
|
|
283
|
+
}
|
|
284
|
+
function convertProviderModel(id, entry, collector) {
|
|
285
|
+
if (!isJsonObject(entry)) {
|
|
286
|
+
collector.unmappable.push(`"providers.${id}.models" entry is not an object`);
|
|
287
|
+
return undefined;
|
|
288
|
+
}
|
|
289
|
+
const out = {};
|
|
290
|
+
for (const [key, value] of Object.entries(entry)) {
|
|
291
|
+
if (key === "id") {
|
|
292
|
+
out.modelID = value;
|
|
293
|
+
}
|
|
294
|
+
else if (key === "tool_call") {
|
|
295
|
+
out.capabilities = { ...asObject(out.capabilities), tools: value };
|
|
296
|
+
}
|
|
297
|
+
else if (key === "modalities") {
|
|
298
|
+
if (isJsonObject(value)) {
|
|
299
|
+
out.capabilities = { ...asObject(out.capabilities), ...value };
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
collector.unmappable.push(`"providers.${id}.models.modalities" is not an object`);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
else if (key === "status") {
|
|
306
|
+
if (value === "deprecated")
|
|
307
|
+
out.disabled = true;
|
|
308
|
+
else
|
|
309
|
+
collector.reportOnly.push(`"providers.${id}.models.status" non-deprecated value`);
|
|
310
|
+
}
|
|
311
|
+
else if (key === "cache_read") {
|
|
312
|
+
out.cache = { ...asObject(out.cache), read: value };
|
|
313
|
+
}
|
|
314
|
+
else if (key === "cache_write") {
|
|
315
|
+
out.cache = { ...asObject(out.cache), write: value };
|
|
316
|
+
}
|
|
317
|
+
else if (key === "options") {
|
|
318
|
+
out.settings = { ...asObject(out.settings), ...asObject(value) };
|
|
319
|
+
}
|
|
320
|
+
else if (key === "variants") {
|
|
321
|
+
if (isJsonObject(value)) {
|
|
322
|
+
out.variants = Object.entries(value).map(([variantId, settings]) => ({
|
|
323
|
+
id: variantId,
|
|
324
|
+
settings: asObject(settings),
|
|
325
|
+
}));
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
collector.unmappable.push(`"providers.${id}.models.variants" is not an object`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
else if (PROVIDER_MODEL_LEGACY_FIELDS.includes(key)) {
|
|
332
|
+
collector.reportOnly.push(`"providers.${id}.models.${key}"`);
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
out[key] = value;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return out;
|
|
339
|
+
}
|
|
340
|
+
function convertProviderEntry(id, entry, collector) {
|
|
341
|
+
if (!isJsonObject(entry)) {
|
|
342
|
+
collector.unmappable.push(`"providers.${id}" is not an object`);
|
|
343
|
+
return undefined;
|
|
344
|
+
}
|
|
345
|
+
const out = {};
|
|
346
|
+
for (const [key, value] of Object.entries(entry)) {
|
|
347
|
+
if (key === "npm") {
|
|
348
|
+
const spec = typeof value === "string" ? value : undefined;
|
|
349
|
+
if (spec === undefined)
|
|
350
|
+
collector.unmappable.push(`"providers.${id}.npm" is not a string`);
|
|
351
|
+
else
|
|
352
|
+
out.package = spec.includes(":") ? spec : `aisdk:${spec}`;
|
|
353
|
+
}
|
|
354
|
+
else if (key === "api") {
|
|
355
|
+
out.settings = { ...asObject(out.settings), baseURL: value };
|
|
356
|
+
}
|
|
357
|
+
else if (key === "options") {
|
|
358
|
+
out.settings = { ...asObject(out.settings), ...asObject(value) };
|
|
359
|
+
}
|
|
360
|
+
else if (key === "models") {
|
|
361
|
+
if (isJsonObject(value)) {
|
|
362
|
+
const models = {};
|
|
363
|
+
for (const [modelId, modelEntry] of Object.entries(value)) {
|
|
364
|
+
const converted = convertProviderModel(`${id}.${modelId}`, modelEntry, collector);
|
|
365
|
+
if (converted !== undefined)
|
|
366
|
+
models[modelId] = converted;
|
|
367
|
+
}
|
|
368
|
+
out.models = models;
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
collector.unmappable.push(`"providers.${id}.models" is not an object`);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
else if (PROVIDER_REPORT_ONLY_FIELDS.includes(key)) {
|
|
375
|
+
collector.reportOnly.push(`"providers.${id}.${key}"`);
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
out[key] = value;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return out;
|
|
382
|
+
}
|
|
383
|
+
const MCP_OAUTH_SNAKE_CASE = {
|
|
384
|
+
clientId: "client_id",
|
|
385
|
+
clientSecret: "client_secret",
|
|
386
|
+
callbackPort: "callback_port",
|
|
387
|
+
redirectUri: "redirect_uri",
|
|
388
|
+
};
|
|
389
|
+
function convertMcpServer(name, entry, collector) {
|
|
390
|
+
if (!isJsonObject(entry)) {
|
|
391
|
+
collector.unmappable.push(`"mcp.servers.${name}" is not an object`);
|
|
392
|
+
return undefined;
|
|
393
|
+
}
|
|
394
|
+
if (!Object.hasOwn(entry, "type") && Object.hasOwn(entry, "enabled")) {
|
|
395
|
+
collector.reportOnly.push(`"mcp.${name}" enabled-only entry without a type`);
|
|
396
|
+
return undefined;
|
|
397
|
+
}
|
|
398
|
+
const out = {};
|
|
399
|
+
for (const [key, value] of Object.entries(entry)) {
|
|
400
|
+
if (key === "enabled") {
|
|
401
|
+
out.disabled = value === true ? false : true;
|
|
402
|
+
}
|
|
403
|
+
else if (key === "timeout") {
|
|
404
|
+
out.timeout = typeof value === "number" ? { catalog: value, execution: value } : value;
|
|
405
|
+
}
|
|
406
|
+
else if (MCP_OAUTH_SNAKE_CASE[key] !== undefined) {
|
|
407
|
+
out[MCP_OAUTH_SNAKE_CASE[key]] = value;
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
out[key] = value;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return out;
|
|
414
|
+
}
|
|
415
|
+
function convertMcp(mcp, collector) {
|
|
416
|
+
if (!isJsonObject(mcp)) {
|
|
417
|
+
collector.unmappable.push('"mcp" is not an object');
|
|
418
|
+
return undefined;
|
|
419
|
+
}
|
|
420
|
+
if (isJsonObject(mcp.servers)) {
|
|
421
|
+
// Already native.
|
|
422
|
+
return mcp;
|
|
423
|
+
}
|
|
424
|
+
const servers = {};
|
|
425
|
+
for (const [name, entry] of Object.entries(mcp)) {
|
|
426
|
+
const converted = convertMcpServer(name, entry, collector);
|
|
427
|
+
if (converted !== undefined)
|
|
428
|
+
servers[name] = converted;
|
|
429
|
+
}
|
|
430
|
+
return { servers };
|
|
431
|
+
}
|
|
432
|
+
function convertCompaction(compaction, collector) {
|
|
433
|
+
if (!isJsonObject(compaction)) {
|
|
434
|
+
collector.unmappable.push('"compaction" is not an object');
|
|
435
|
+
return undefined;
|
|
436
|
+
}
|
|
437
|
+
const out = {};
|
|
438
|
+
for (const [key, value] of Object.entries(compaction)) {
|
|
439
|
+
if (key === "preserve_recent_tokens") {
|
|
440
|
+
out.keep = { ...asObject(out.keep), tokens: value };
|
|
441
|
+
}
|
|
442
|
+
else if (key === "reserved") {
|
|
443
|
+
out.buffer = value;
|
|
444
|
+
}
|
|
445
|
+
else if (key === "tail_turns" || key === "prune") {
|
|
446
|
+
collector.reportOnly.push(`"compaction.${key}"`);
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
out[key] = value;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return out;
|
|
453
|
+
}
|
|
454
|
+
function convertSkills(skills, collector) {
|
|
455
|
+
if (Array.isArray(skills))
|
|
456
|
+
return skills.slice();
|
|
457
|
+
if (!isJsonObject(skills)) {
|
|
458
|
+
collector.unmappable.push('"skills" is neither an array nor an object');
|
|
459
|
+
return undefined;
|
|
460
|
+
}
|
|
461
|
+
const result = [];
|
|
462
|
+
for (const key of ["paths", "urls"]) {
|
|
463
|
+
const value = skills[key];
|
|
464
|
+
if (value === undefined)
|
|
465
|
+
continue;
|
|
466
|
+
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string")) {
|
|
467
|
+
collector.unmappable.push(`"skills.${key}" is not an array of strings`);
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
result.push(...value);
|
|
471
|
+
}
|
|
472
|
+
return result;
|
|
473
|
+
}
|
|
474
|
+
function convertAutoshare(value) {
|
|
475
|
+
if (value === true)
|
|
476
|
+
return "auto";
|
|
477
|
+
if (value === false)
|
|
478
|
+
return "disabled";
|
|
479
|
+
return undefined;
|
|
480
|
+
}
|
|
481
|
+
function convertAutoupdate(value) {
|
|
482
|
+
if (value === true)
|
|
483
|
+
return "auto";
|
|
484
|
+
if (value === false)
|
|
485
|
+
return "disable";
|
|
486
|
+
if (value === "notify")
|
|
487
|
+
return "notify";
|
|
488
|
+
return undefined;
|
|
489
|
+
}
|
|
490
|
+
// START_CONTRACT: planOpenCodeConfigMigration
|
|
491
|
+
// PURPOSE: Build the native V2 target document from a V1 document, collecting report-only and unmappable fields.
|
|
492
|
+
// INPUTS: { document: JsonObject - parsed OpenCode config document. }
|
|
493
|
+
// OUTPUTS: { OpenCodeConfigMigrationPlan - native target, change flag, and report-only/unmappable lists. }
|
|
494
|
+
// SIDE_EFFECTS: none
|
|
495
|
+
// LINKS: [fn-classifyOpenCodeConfig, fn-migrateOpenCodeConfig]
|
|
496
|
+
// END_CONTRACT: planOpenCodeConfigMigration
|
|
497
|
+
export function planOpenCodeConfigMigration(document) {
|
|
498
|
+
const collector = { reportOnly: [], unmappable: [] };
|
|
499
|
+
const out = {};
|
|
500
|
+
const handled = new Set([...TOP_LEVEL_V1_KEYS, ...REPORT_ONLY_TOP_LEVEL, "model"]);
|
|
501
|
+
// Copy unrelated fields first.
|
|
502
|
+
for (const [key, value] of Object.entries(document)) {
|
|
503
|
+
if (!handled.has(key))
|
|
504
|
+
out[key] = cloneJson(value);
|
|
505
|
+
}
|
|
506
|
+
for (const key of REPORT_ONLY_TOP_LEVEL) {
|
|
507
|
+
if (Object.hasOwn(document, key))
|
|
508
|
+
collector.reportOnly.push(`"${key}"`);
|
|
509
|
+
}
|
|
510
|
+
out.$schema = OPENCODE_SCHEMA_URL;
|
|
511
|
+
// plugins: rename plugin and convert V1 tuples to {package, options} objects.
|
|
512
|
+
const rawPlugins = document.plugins ?? document.plugin;
|
|
513
|
+
if (rawPlugins !== undefined) {
|
|
514
|
+
if (!Array.isArray(rawPlugins)) {
|
|
515
|
+
collector.unmappable.push('"plugins" is not an array');
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
const plugins = [];
|
|
519
|
+
for (const entry of rawPlugins) {
|
|
520
|
+
if (typeof entry === "string") {
|
|
521
|
+
plugins.push(entry);
|
|
522
|
+
}
|
|
523
|
+
else if (Array.isArray(entry) && entry.length === 2 && typeof entry[0] === "string") {
|
|
524
|
+
plugins.push(isJsonObject(entry[1])
|
|
525
|
+
? { package: entry[0], options: entry[1] }
|
|
526
|
+
: { package: entry[0] });
|
|
527
|
+
}
|
|
528
|
+
else if (isJsonObject(entry)) {
|
|
529
|
+
plugins.push(entry);
|
|
530
|
+
}
|
|
531
|
+
else {
|
|
532
|
+
collector.unmappable.push('"plugins" entry is neither a string, tuple, nor object');
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
out.plugins = plugins;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
// agents: merge agent and mode maps, converting each entry wholly.
|
|
539
|
+
const sourceAgents = asObject(document.agents);
|
|
540
|
+
const legacyAgentMap = document.agent;
|
|
541
|
+
const legacyModeMap = document.mode;
|
|
542
|
+
if (legacyAgentMap !== undefined && !isJsonObject(legacyAgentMap))
|
|
543
|
+
collector.unmappable.push('"agent" is not an object');
|
|
544
|
+
if (legacyModeMap !== undefined && !isJsonObject(legacyModeMap))
|
|
545
|
+
collector.unmappable.push('"mode" is not an object');
|
|
546
|
+
const agents = {};
|
|
547
|
+
const mergeAgentMap = (map, forcePrimary) => {
|
|
548
|
+
if (!isJsonObject(map))
|
|
549
|
+
return;
|
|
550
|
+
for (const [name, entry] of Object.entries(map)) {
|
|
551
|
+
const converted = convertAgentEntry(name, entry, collector);
|
|
552
|
+
if (converted === undefined)
|
|
553
|
+
continue;
|
|
554
|
+
if (forcePrimary && !Object.hasOwn(converted, "mode"))
|
|
555
|
+
converted.mode = "primary";
|
|
556
|
+
agents[name] = converted;
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
mergeAgentMap(sourceAgents, false);
|
|
560
|
+
mergeAgentMap(legacyAgentMap, false);
|
|
561
|
+
mergeAgentMap(legacyModeMap, true);
|
|
562
|
+
// small_model / title agent.
|
|
563
|
+
if (Object.hasOwn(document, "small_model")) {
|
|
564
|
+
const small = document.small_model;
|
|
565
|
+
if (isRoleReference(small)) {
|
|
566
|
+
// dropped; rebuilt as modelIntent
|
|
567
|
+
}
|
|
568
|
+
else {
|
|
569
|
+
const selection = normalizeNativeModelSelection(small, "small_model");
|
|
570
|
+
if (selection !== undefined) {
|
|
571
|
+
const title = asObject(agents.title);
|
|
572
|
+
agents.title = { ...title, model: selection };
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
if (Object.keys(agents).length > 0)
|
|
577
|
+
out.agents = agents;
|
|
578
|
+
// commands
|
|
579
|
+
const legacyCommands = document.command;
|
|
580
|
+
if (legacyCommands !== undefined) {
|
|
581
|
+
if (!isJsonObject(legacyCommands)) {
|
|
582
|
+
collector.unmappable.push('"command" is not an object');
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
const commands = {};
|
|
586
|
+
for (const [name, entry] of Object.entries(legacyCommands)) {
|
|
587
|
+
const converted = convertCommandEntry(name, entry, collector);
|
|
588
|
+
if (converted !== undefined)
|
|
589
|
+
commands[name] = converted;
|
|
590
|
+
}
|
|
591
|
+
out.commands = commands;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
// providers
|
|
595
|
+
const legacyProviders = document.provider;
|
|
596
|
+
if (legacyProviders !== undefined) {
|
|
597
|
+
if (!isJsonObject(legacyProviders)) {
|
|
598
|
+
collector.unmappable.push('"provider" is not an object');
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
const providers = {};
|
|
602
|
+
for (const [id, entry] of Object.entries(legacyProviders)) {
|
|
603
|
+
const canonical = PROVIDER_ID_RENAMES[id] ?? id;
|
|
604
|
+
const converted = convertProviderEntry(canonical, entry, collector);
|
|
605
|
+
if (converted !== undefined)
|
|
606
|
+
providers[canonical] = converted;
|
|
607
|
+
}
|
|
608
|
+
out.providers = providers;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
// permissions from permission and tools
|
|
612
|
+
const permissions = [
|
|
613
|
+
...convertPermissionMap(document.permission, collector),
|
|
614
|
+
...convertToolsMap(document.tools, collector),
|
|
615
|
+
];
|
|
616
|
+
if (permissions.length > 0) {
|
|
617
|
+
const existing = Array.isArray(out.permissions) ? out.permissions : [];
|
|
618
|
+
out.permissions = [...existing, ...permissions];
|
|
619
|
+
}
|
|
620
|
+
// skills
|
|
621
|
+
if (document.skills !== undefined) {
|
|
622
|
+
const skills = convertSkills(document.skills, collector);
|
|
623
|
+
if (skills !== undefined)
|
|
624
|
+
out.skills = skills;
|
|
625
|
+
}
|
|
626
|
+
// mcp
|
|
627
|
+
if (document.mcp !== undefined) {
|
|
628
|
+
const mcp = convertMcp(document.mcp, collector);
|
|
629
|
+
if (mcp !== undefined)
|
|
630
|
+
out.mcp = mcp;
|
|
631
|
+
}
|
|
632
|
+
// compaction
|
|
633
|
+
if (document.compaction !== undefined) {
|
|
634
|
+
const compaction = convertCompaction(document.compaction, collector);
|
|
635
|
+
if (compaction !== undefined)
|
|
636
|
+
out.compaction = compaction;
|
|
637
|
+
}
|
|
638
|
+
// simple renames
|
|
639
|
+
if (document.reference !== undefined)
|
|
640
|
+
out.references = cloneJson(document.reference);
|
|
641
|
+
if (document.snapshot !== undefined)
|
|
642
|
+
out.snapshots = document.snapshot;
|
|
643
|
+
if (document.attachment !== undefined)
|
|
644
|
+
out.media = cloneJson(document.attachment);
|
|
645
|
+
if (document.autoshare !== undefined) {
|
|
646
|
+
const share = convertAutoshare(document.autoshare);
|
|
647
|
+
if (share === undefined)
|
|
648
|
+
collector.reportOnly.push('"autoshare"');
|
|
649
|
+
else
|
|
650
|
+
out.share = share;
|
|
651
|
+
}
|
|
652
|
+
if (document.autoupdate !== undefined) {
|
|
653
|
+
const update = convertAutoupdate(document.autoupdate);
|
|
654
|
+
if (update === undefined)
|
|
655
|
+
collector.reportOnly.push('"autoupdate"');
|
|
656
|
+
else
|
|
657
|
+
out.update = update;
|
|
658
|
+
}
|
|
659
|
+
// root model role reference is dropped (rebuilt as modelIntent); native model strings are preserved.
|
|
660
|
+
if (document.model !== undefined && !isRoleReference(document.model)) {
|
|
661
|
+
out.model = cloneJson(document.model);
|
|
662
|
+
}
|
|
663
|
+
const changed = JSON.stringify(out) !== JSON.stringify(document);
|
|
664
|
+
return {
|
|
665
|
+
document: out,
|
|
666
|
+
changed,
|
|
667
|
+
reportOnly: collector.reportOnly,
|
|
668
|
+
unmappable: collector.unmappable,
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
function renderMigratedText(text, before, after) {
|
|
672
|
+
// Edit top-level keys individually so comments and formatting attached to
|
|
673
|
+
// unchanged keys survive; only changed keys and removed V1 keys are touched.
|
|
674
|
+
let next = text;
|
|
675
|
+
for (const key of Object.keys(before)) {
|
|
676
|
+
if (!Object.hasOwn(after, key)) {
|
|
677
|
+
next = applyEdits(next, modify(next, [key], undefined, { formattingOptions: JSON_FORMAT }));
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
for (const [key, value] of Object.entries(after)) {
|
|
681
|
+
if (Object.hasOwn(before, key) && JSON.stringify(before[key]) === JSON.stringify(value))
|
|
682
|
+
continue;
|
|
683
|
+
next = applyEdits(next, modify(next, [key], value, { formattingOptions: JSON_FORMAT }));
|
|
684
|
+
}
|
|
685
|
+
return ensureTrailingNewline(next);
|
|
686
|
+
}
|
|
687
|
+
async function detectLegacyTuiPaths(opencodeConfigPath) {
|
|
688
|
+
const baseDir = dirname(opencodeConfigPath);
|
|
689
|
+
const found = [];
|
|
690
|
+
for (const name of ["tui.json", "tui.jsonc"]) {
|
|
691
|
+
const candidate = join(baseDir, name);
|
|
692
|
+
if ((await readOptionalText(candidate)) !== undefined)
|
|
693
|
+
found.push(candidate);
|
|
694
|
+
}
|
|
695
|
+
return found;
|
|
696
|
+
}
|
|
697
|
+
// START_CONTRACT: migrateOpenCodeConfig
|
|
698
|
+
// PURPOSE: Read an OpenCode config, materialize the native V2 document when V1-shaped, back it up, and write it only when valid.
|
|
699
|
+
// INPUTS: { paths: ResolvedPaths - resolved OpenCode config path bundle. }
|
|
700
|
+
// OUTPUTS: { OpenCodeConfigMigrationResult - action, backup path, and field lists. }
|
|
701
|
+
// SIDE_EFFECTS: Writes a timestamped sibling backup and the migrated config atomically; never touches a legacy tui.json(c).
|
|
702
|
+
// LINKS: [fn-classifyOpenCodeConfig, fn-planOpenCodeConfigMigration]
|
|
703
|
+
// END_CONTRACT: migrateOpenCodeConfig
|
|
704
|
+
export async function migrateOpenCodeConfig(paths) {
|
|
705
|
+
const legacyTuiPaths = await detectLegacyTuiPaths(paths.opencodeConfigPath);
|
|
706
|
+
const currentText = await readOptionalText(paths.opencodeConfigPath);
|
|
707
|
+
if (currentText === undefined || !currentText.trim()) {
|
|
708
|
+
return {
|
|
709
|
+
path: paths.opencodeConfigPath,
|
|
710
|
+
action: "kept",
|
|
711
|
+
reportOnly: [],
|
|
712
|
+
unmappable: [],
|
|
713
|
+
legacyTuiPaths,
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
const document = parseObjectDocument(currentText, "OpenCode config");
|
|
717
|
+
const classification = classifyOpenCodeConfig(document);
|
|
718
|
+
if (classification.shape === "native-clean") {
|
|
719
|
+
return {
|
|
720
|
+
path: paths.opencodeConfigPath,
|
|
721
|
+
action: "kept",
|
|
722
|
+
reportOnly: [],
|
|
723
|
+
unmappable: [],
|
|
724
|
+
legacyTuiPaths,
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
const plan = planOpenCodeConfigMigration(document);
|
|
728
|
+
if (plan.unmappable.length > 0 || plan.reportOnly.length > 0) {
|
|
729
|
+
return {
|
|
730
|
+
path: paths.opencodeConfigPath,
|
|
731
|
+
action: "aborted",
|
|
732
|
+
reportOnly: plan.reportOnly,
|
|
733
|
+
unmappable: plan.unmappable,
|
|
734
|
+
legacyTuiPaths,
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
// Validate the planned document against the pinned native codec before any write.
|
|
738
|
+
const nextText = renderMigratedText(currentText, document, plan.document);
|
|
739
|
+
const reparsed = parseObjectDocument(nextText, "OpenCode config");
|
|
740
|
+
assertNativeOpenCodeDocument(reparsed, "OpenCode config", {
|
|
741
|
+
configDir: dirname(paths.opencodeConfigPath),
|
|
742
|
+
});
|
|
743
|
+
if (nextText === currentText) {
|
|
744
|
+
return {
|
|
745
|
+
path: paths.opencodeConfigPath,
|
|
746
|
+
action: "kept",
|
|
747
|
+
reportOnly: [],
|
|
748
|
+
unmappable: [],
|
|
749
|
+
legacyTuiPaths,
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
const backupPath = `${paths.opencodeConfigPath}.vvoc-backup-${Date.now()}`;
|
|
753
|
+
await writeText(backupPath, currentText);
|
|
754
|
+
await writeText(paths.opencodeConfigPath, nextText);
|
|
755
|
+
return {
|
|
756
|
+
path: paths.opencodeConfigPath,
|
|
757
|
+
action: "migrated",
|
|
758
|
+
backupPath,
|
|
759
|
+
reportOnly: [],
|
|
760
|
+
unmappable: [],
|
|
761
|
+
legacyTuiPaths,
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
//# sourceMappingURL=config-migration.js.map
|