opencode-codebuddy-quota 0.1.0 → 0.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/README.md +1 -0
- package/config.default.json +41 -38
- package/package.json +2 -1
- package/rpc.ts +92 -92
- package/server.ts +133 -19
- package/src/gui/battery.tsx +81 -81
- package/src/gui/pace.ts +76 -76
- package/src/gui/quota.ts +198 -198
- package/src/gui/store.ts +181 -181
- package/src/gui.tsx +111 -111
- package/src/server/catalog.ts +264 -264
- package/src/server/config-writer.ts +622 -621
- package/src/server/config.ts +215 -195
- package/src/server/embedding-local.ts +166 -0
- package/src/server/embedding-mode.ts +14 -0
- package/src/server/gateway-owner.ts +306 -0
- package/src/server/gateway-setup.ts +74 -0
- package/src/server/logger.ts +26 -26
- package/src/server/model-cache.ts +136 -0
- package/src/server/model-info.ts +109 -109
- package/src/server/model-prewarm.ts +123 -0
- package/src/server/models-dev.ts +77 -77
- package/src/server/models-endpoint.ts +74 -0
- package/src/server/package-spec.ts +42 -42
- package/src/server/package-version.ts +51 -51
- package/src/server/pricing.ts +169 -169
- package/src/server/proxy.ts +379 -8
- package/src/server/stream-collect.ts +166 -0
- package/src/server/wire.ts +67 -67
- package/src/shared.ts +3 -0
|
@@ -1,621 +1,622 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync } from "node:fs"
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
*
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
| { status: "
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
let
|
|
82
|
-
let
|
|
83
|
-
let
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
//
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
entry.
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (value
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
* `
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
//
|
|
243
|
-
|
|
244
|
-
const
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
const
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
.
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
//
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
let
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
//
|
|
295
|
-
//
|
|
296
|
-
//
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
*
|
|
374
|
-
*
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
* the
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
const
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
const
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
else i
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
*
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
const
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
const
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
//
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
const
|
|
548
|
-
const
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
const
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
//
|
|
561
|
-
|
|
562
|
-
const
|
|
563
|
-
|
|
564
|
-
//
|
|
565
|
-
//
|
|
566
|
-
|
|
567
|
-
const
|
|
568
|
-
const
|
|
569
|
-
|
|
570
|
-
`${eol}${indent}
|
|
571
|
-
`${eol}${indent} "
|
|
572
|
-
`${eol}${indent} "
|
|
573
|
-
`${eol}${indent} "
|
|
574
|
-
`${eol}${indent} "
|
|
575
|
-
`${eol}${indent}
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
//
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
if (final.status === "
|
|
602
|
-
|
|
603
|
-
//
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
1
|
+
import { readFileSync, writeFileSync } from "node:fs"
|
|
2
|
+
import { PROXY_PORT } from "../shared"
|
|
3
|
+
import type { ImportedModel } from "./model-info"
|
|
4
|
+
import { withVersion } from "./package-version"
|
|
5
|
+
import { isVersion, packageSpec, PROVIDER_IDS } from "./package-spec"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Writes the imported model list into the user's `opencode.jsonc`.
|
|
9
|
+
*
|
|
10
|
+
* Why the config file and not `ctx.provider.transform`: in OpenCode 2.2.5 the
|
|
11
|
+
* provider records handed to a transform are already frozen — every write path
|
|
12
|
+
* (`add`, `models.set`, `update`) throws `Attempted to assign to readonly
|
|
13
|
+
* property`. The config file is read before providers are resolved, so it is
|
|
14
|
+
* the only place a plugin can add models that actually reach the model picker.
|
|
15
|
+
*
|
|
16
|
+
* The file is JSONC, so this edits text rather than re-serializing: comments and
|
|
17
|
+
* key order survive, and an unrelated field is never reformatted. Failure to
|
|
18
|
+
* parse means the file is left untouched.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export interface WriteModelsDeps {
|
|
22
|
+
jsoncPath: string
|
|
23
|
+
/** Which provider's `models` block to edit. Defaults to the legacy provider. */
|
|
24
|
+
providerId?: string
|
|
25
|
+
models: readonly ImportedModel[]
|
|
26
|
+
/** When set, the provider's `package` version is moved to this value. */
|
|
27
|
+
packageVersion?: string | null
|
|
28
|
+
readFile?: (path: string) => string
|
|
29
|
+
writeFile?: (path: string, content: string) => void
|
|
30
|
+
/** Reads the file from disk for the concurrent-write guard. Defaults to readFileSync. */
|
|
31
|
+
diskRead?: (path: string) => string
|
|
32
|
+
/** Called with the original text right before it is replaced. */
|
|
33
|
+
backup?: (path: string, content: string) => void
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Refuse to write if another process changed the file since we read it. When
|
|
38
|
+
* `writeFile` is injected (tests / in-memory use) there is no real disk file to
|
|
39
|
+
* guard, so this is a no-op unless an explicit `diskRead` is supplied.
|
|
40
|
+
*/
|
|
41
|
+
function fileChangedOnDisk(deps: WriteModelsDeps, original: string): boolean {
|
|
42
|
+
if (!deps.diskRead && deps.writeFile) return false
|
|
43
|
+
try {
|
|
44
|
+
const diskRead = deps.diskRead ?? ((p: string) => readFileSync(p, "utf8"))
|
|
45
|
+
return diskRead(deps.jsoncPath) !== original
|
|
46
|
+
} catch {
|
|
47
|
+
// A file that vanished/changed unreadably is unsafe to overwrite blindly.
|
|
48
|
+
return true
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Three outcomes, not two booleans: the file was rewritten, it already matched
|
|
54
|
+
* (so nothing was touched), or it was left alone because something about it
|
|
55
|
+
* could not be understood.
|
|
56
|
+
*/
|
|
57
|
+
export type WriteModelsResult =
|
|
58
|
+
| {
|
|
59
|
+
status: "written"
|
|
60
|
+
/** The `package` version moved. False when it already matched or was not a version. */
|
|
61
|
+
packageUpdated: boolean
|
|
62
|
+
/**
|
|
63
|
+
* Set when a `package` key exists but updating it failed — the models still
|
|
64
|
+
* land, but the caller should say why the version did not move.
|
|
65
|
+
*/
|
|
66
|
+
packageWarning?: string
|
|
67
|
+
}
|
|
68
|
+
| { status: "unchanged" }
|
|
69
|
+
| { status: "skipped"; reason: string }
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Blanks `//` and leading-`*` comments plus trailing commas, so `JSON.parse`
|
|
73
|
+
* can read the text.
|
|
74
|
+
*
|
|
75
|
+
* Deleted characters are replaced by spaces rather than removed: every caller
|
|
76
|
+
* splices the original text using offsets taken from this result, and a changed
|
|
77
|
+
* length would silently cut a different span. Newlines survive so line offsets
|
|
78
|
+
* and error messages stay meaningful.
|
|
79
|
+
*/
|
|
80
|
+
export function stripComments(text: string): string {
|
|
81
|
+
let out = ""
|
|
82
|
+
let inString = false
|
|
83
|
+
let inLine = false
|
|
84
|
+
let inBlock = false
|
|
85
|
+
|
|
86
|
+
for (let i = 0; i < text.length; i++) {
|
|
87
|
+
const ch = text[i]
|
|
88
|
+
const next = text[i + 1]
|
|
89
|
+
|
|
90
|
+
if (inLine) {
|
|
91
|
+
if (ch === "\n") {
|
|
92
|
+
inLine = false
|
|
93
|
+
out += "\n"
|
|
94
|
+
} else {
|
|
95
|
+
out += " "
|
|
96
|
+
}
|
|
97
|
+
continue
|
|
98
|
+
}
|
|
99
|
+
if (inBlock) {
|
|
100
|
+
if (ch === "*" && next === "/") {
|
|
101
|
+
inBlock = false
|
|
102
|
+
out += " "
|
|
103
|
+
i++
|
|
104
|
+
} else {
|
|
105
|
+
out += ch === "\n" ? "\n" : " "
|
|
106
|
+
}
|
|
107
|
+
continue
|
|
108
|
+
}
|
|
109
|
+
if (inString) {
|
|
110
|
+
out += ch
|
|
111
|
+
if (ch === "\\") {
|
|
112
|
+
out += next ?? ""
|
|
113
|
+
i++
|
|
114
|
+
} else if (ch === "\"") {
|
|
115
|
+
inString = false
|
|
116
|
+
}
|
|
117
|
+
continue
|
|
118
|
+
}
|
|
119
|
+
if (ch === "\"") {
|
|
120
|
+
inString = true
|
|
121
|
+
out += ch
|
|
122
|
+
continue
|
|
123
|
+
}
|
|
124
|
+
if (ch === "/" && next === "/") {
|
|
125
|
+
inLine = true
|
|
126
|
+
out += " "
|
|
127
|
+
i++
|
|
128
|
+
continue
|
|
129
|
+
}
|
|
130
|
+
if (ch === "/" && next === "*") {
|
|
131
|
+
inBlock = true
|
|
132
|
+
out += " "
|
|
133
|
+
i++
|
|
134
|
+
continue
|
|
135
|
+
}
|
|
136
|
+
out += ch
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return blankTrailingCommas(out)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** JSONC allows a comma before a closing brace/bracket; JSON does not. */
|
|
143
|
+
function blankTrailingCommas(text: string): string {
|
|
144
|
+
// Code units, not code points: the caller relies on a 1:1 index mapping back
|
|
145
|
+
// into `text`, which a `[...text]` spread would break for astral characters.
|
|
146
|
+
const chars = text.split("")
|
|
147
|
+
let inString = false
|
|
148
|
+
for (let i = 0; i < text.length; i++) {
|
|
149
|
+
const ch = text[i]
|
|
150
|
+
if (inString) {
|
|
151
|
+
if (ch === "\\") i++
|
|
152
|
+
else if (ch === "\"") inString = false
|
|
153
|
+
continue
|
|
154
|
+
}
|
|
155
|
+
if (ch === "\"") {
|
|
156
|
+
inString = true
|
|
157
|
+
continue
|
|
158
|
+
}
|
|
159
|
+
if (ch !== ",") continue
|
|
160
|
+
// Only a comma whose next non-space character closes a container is trailing.
|
|
161
|
+
let j = i + 1
|
|
162
|
+
while (j < text.length && /\s/.test(text[j])) j++
|
|
163
|
+
if (j < text.length && (text[j] === "}" || text[j] === "]")) chars[i] = " "
|
|
164
|
+
}
|
|
165
|
+
return chars.join("")
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** A model as it must appear in `opencode.jsonc`, matching the existing hand-written entries. */
|
|
169
|
+
export function toConfigModel(model: ImportedModel): Record<string, unknown> {
|
|
170
|
+
const cost = model.cost[0]
|
|
171
|
+
const entry: Record<string, unknown> = {
|
|
172
|
+
name: model.name,
|
|
173
|
+
capabilities: {
|
|
174
|
+
tools: true,
|
|
175
|
+
input: model.imageInput ? ["text", "image"] : ["text"],
|
|
176
|
+
output: ["text"],
|
|
177
|
+
},
|
|
178
|
+
limit: { context: model.limit.context, output: model.limit.output },
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (cost) {
|
|
182
|
+
entry.cost = {
|
|
183
|
+
input: cost.input,
|
|
184
|
+
output: cost.output,
|
|
185
|
+
cache: { read: cost.cache.read, write: cost.cache.write },
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const variants = model.efforts.map((effort) => ({
|
|
190
|
+
id: effort,
|
|
191
|
+
settings: effort === "off" ? {} : { reasoningEffort: effort },
|
|
192
|
+
}))
|
|
193
|
+
if (variants.length > 0) {
|
|
194
|
+
entry.variants = variants
|
|
195
|
+
entry.settings = { reasoningEffort: model.defaultEffort ?? model.efforts[model.efforts.length - 1] }
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return entry
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Serializes one property value on a single line: `{ ... }` with spaces inside
|
|
203
|
+
* the braces and `[a, b]` for arrays, which is how these config files are
|
|
204
|
+
* normally hand-written. Keeping it compact matters — a fully expanded entry
|
|
205
|
+
* costs ~40 lines, so ten models would turn a 190-line file into 500.
|
|
206
|
+
*/
|
|
207
|
+
function compactJson(value: unknown): string {
|
|
208
|
+
if (Array.isArray(value)) return `[${value.map(compactJson).join(", ")}]`
|
|
209
|
+
if (value && typeof value === "object") {
|
|
210
|
+
const entries = Object.entries(value as Record<string, unknown>)
|
|
211
|
+
if (entries.length === 0) return "{}"
|
|
212
|
+
return `{ ${entries.map(([k, v]) => `${JSON.stringify(k)}: ${compactJson(v)}`).join(", ")} }`
|
|
213
|
+
}
|
|
214
|
+
return JSON.stringify(value)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The line ending the file actually uses, so a rewritten block does not mix them. */
|
|
218
|
+
function newlineOf(text: string): string {
|
|
219
|
+
return text.includes("\r\n") ? "\r\n" : "\n"
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function indentOf(text: string, at: number): string {
|
|
223
|
+
const lineStart = text.lastIndexOf("\n", at) + 1
|
|
224
|
+
const prefix = text.slice(lineStart, at)
|
|
225
|
+
const match = /^[ \t]*/.exec(prefix)
|
|
226
|
+
return match ? match[0] : " "
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Brings `providers.codebuddy` in line with what the plugin computed: the
|
|
231
|
+
* `models` block is replaced, and the provider `package` version is moved
|
|
232
|
+
* forward when a newer one was found.
|
|
233
|
+
*
|
|
234
|
+
* The whole block is compared by content, not just by the set of model ids, so
|
|
235
|
+
* a price or reasoning-level change from models.dev also lands. Nothing is
|
|
236
|
+
* written when the rendered text already matches what is on disk.
|
|
237
|
+
*
|
|
238
|
+
* Any malformed input ends in `skipped` rather than risk corrupting the user's
|
|
239
|
+
* configuration.
|
|
240
|
+
*/
|
|
241
|
+
export function writeModels(deps: WriteModelsDeps): WriteModelsResult {
|
|
242
|
+
// Default to the first NEW provider, never the legacy `codebuddy`: a future
|
|
243
|
+
// missed providerId must not rewrite the old provider block.
|
|
244
|
+
const providerId = deps.providerId ?? PROVIDER_IDS[0]
|
|
245
|
+
const read = deps.readFile ?? ((p: string) => readFileSync(p, "utf8"))
|
|
246
|
+
const write = deps.writeFile ?? ((p: string, c: string) => writeFileSync(p, c, "utf8"))
|
|
247
|
+
|
|
248
|
+
let original: string
|
|
249
|
+
try {
|
|
250
|
+
original = read(deps.jsoncPath)
|
|
251
|
+
} catch {
|
|
252
|
+
return { status: "skipped", reason: "config file could not be read" }
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const bare = stripComments(original)
|
|
256
|
+
if (!isJsonObject(bare)) {
|
|
257
|
+
return { status: "skipped", reason: "config file is not valid JSON/JSONC" }
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const provider = locateProvider(bare, providerId)
|
|
261
|
+
if (!provider) {
|
|
262
|
+
return { status: "skipped", reason: `could not locate providers.${providerId} in the config text` }
|
|
263
|
+
}
|
|
264
|
+
const modelsRange = locateModelsBlock(bare, provider)
|
|
265
|
+
if (!modelsRange) {
|
|
266
|
+
return { status: "skipped", reason: `could not locate providers.${providerId}.models in the config text` }
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const modelsById: Record<string, unknown> = {}
|
|
270
|
+
for (const model of deps.models) modelsById[model.id] = toConfigModel(model)
|
|
271
|
+
|
|
272
|
+
const modelsIndent = indentOf(original, modelsRange.open)
|
|
273
|
+
const eol = newlineOf(original)
|
|
274
|
+
const members = Object.entries(modelsById)
|
|
275
|
+
.map(([id, value]) => `${modelsIndent} ${JSON.stringify(id)}: ${compactJson(value)}`)
|
|
276
|
+
.join(`,${eol}`)
|
|
277
|
+
const body = members ? `${eol}${members}${eol}${modelsIndent}` : ""
|
|
278
|
+
let next = `${original.slice(0, modelsRange.open + 1)}${body}${original.slice(modelsRange.close)}`
|
|
279
|
+
|
|
280
|
+
// A version that is not a version (a whole file, an empty string, a URL) is
|
|
281
|
+
// never written into the `package` spec; the models still land.
|
|
282
|
+
const packageVersion = isVersion(deps.packageVersion) ? deps.packageVersion : null
|
|
283
|
+
let packageUpdated = false
|
|
284
|
+
let packageWarning: string | undefined
|
|
285
|
+
if (packageVersion) {
|
|
286
|
+
const retargeted = retargetPackage({ text: next, provider, version: packageVersion })
|
|
287
|
+
next = retargeted.text
|
|
288
|
+
packageUpdated = retargeted.applied
|
|
289
|
+
packageWarning = retargeted.problem
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (next === original) return { status: "unchanged" }
|
|
293
|
+
|
|
294
|
+
// Last gate before touching the user's file: a splice that landed on the
|
|
295
|
+
// wrong offset produces text that no longer parses, and writing that would
|
|
296
|
+
// destroy the config. Refuse instead. Comments are still in `next`, so it is
|
|
297
|
+
// parsed the same way the input was.
|
|
298
|
+
const rendered = parseJsonc(next)
|
|
299
|
+
if (!rendered) {
|
|
300
|
+
return { status: "skipped", reason: "rendered config would not parse; refusing to write" }
|
|
301
|
+
}
|
|
302
|
+
const renderedProvider = (rendered as { providers?: Record<string, unknown> }).providers?.[providerId] as
|
|
303
|
+
| { models?: unknown; package?: unknown }
|
|
304
|
+
| undefined
|
|
305
|
+
const renderedIds = Object.keys((renderedProvider?.models as Record<string, unknown>) ?? {})
|
|
306
|
+
if (renderedIds.join("\u0000") !== Object.keys(modelsById).join("\u0000")) {
|
|
307
|
+
return { status: "skipped", reason: "model block did not land under the expected provider; refusing to write" }
|
|
308
|
+
}
|
|
309
|
+
if (packageVersion) {
|
|
310
|
+
const expected = packageSpec(packageVersion)
|
|
311
|
+
if (renderedProvider?.package !== undefined && renderedProvider.package !== expected && !packageWarning) {
|
|
312
|
+
return { status: "skipped", reason: `package is ${JSON.stringify(renderedProvider.package)} after the rewrite; refusing to write` }
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (fileChangedOnDisk(deps, original)) {
|
|
317
|
+
return { status: "skipped", reason: "config file changed on disk while editing; refusing to overwrite" }
|
|
318
|
+
}
|
|
319
|
+
deps.backup?.(deps.jsoncPath, original)
|
|
320
|
+
write(deps.jsoncPath, next)
|
|
321
|
+
return packageWarning
|
|
322
|
+
? { status: "written", packageUpdated, packageWarning }
|
|
323
|
+
: { status: "written", packageUpdated }
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function isJsonObject(text: string): boolean {
|
|
327
|
+
try {
|
|
328
|
+
const value: unknown = JSON.parse(text)
|
|
329
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
330
|
+
} catch {
|
|
331
|
+
return false
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** The config as a value, comments and trailing commas tolerated. Null if it is not parseable. */
|
|
336
|
+
function parseJsonc(text: string): unknown | null {
|
|
337
|
+
try {
|
|
338
|
+
return JSON.parse(stripComments(text))
|
|
339
|
+
} catch {
|
|
340
|
+
return null
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** Offsets of an object literal, `open` being its `{` and `close` its matching `}`. */
|
|
345
|
+
interface ObjectRange {
|
|
346
|
+
open: number
|
|
347
|
+
close: number
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Finds `providers.<providerId>`, walking the structure instead of searching
|
|
352
|
+
* for the first `"codebuddy"`: that literal also appears inside other
|
|
353
|
+
* providers' string values, and splicing there would drop the models into the
|
|
354
|
+
* wrong provider.
|
|
355
|
+
*/
|
|
356
|
+
function locateProvider(bare: string, providerId: string = PROVIDER_IDS[0]): ObjectRange | null {
|
|
357
|
+
const root = objectAt(bare, skipSpace(bare, 0))
|
|
358
|
+
if (!root) return null
|
|
359
|
+
const providers = memberObject(bare, root.open, "providers")
|
|
360
|
+
return providers && memberObject(bare, providers.open, providerId)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Finds the `{`/`}` of the provider's models object. Duplicate keys are
|
|
365
|
+
* refused: text-level editing cannot tell which copy the parser would read, so
|
|
366
|
+
* a wrong guess would edit the one that is never used.
|
|
367
|
+
*/
|
|
368
|
+
function locateModelsBlock(bare: string, provider: ObjectRange): ObjectRange | null {
|
|
369
|
+
return memberObject(bare, provider.open, "models")
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Retargets the `package` directly under `providers.codebuddy` to `version`.
|
|
374
|
+
*
|
|
375
|
+
* Only that level is searched, so a `"package"` nested in `settings` or any
|
|
376
|
+
* other sibling is never touched. `problem` explains a rewrite that was wanted
|
|
377
|
+
* but could not be made, so the caller can say so instead of silently leaving
|
|
378
|
+
* the version behind.
|
|
379
|
+
*/
|
|
380
|
+
function retargetPackage(deps: {
|
|
381
|
+
text: string
|
|
382
|
+
provider: ObjectRange
|
|
383
|
+
version: string
|
|
384
|
+
}): { text: string; applied: boolean; problem?: string } {
|
|
385
|
+
const { text, provider } = deps
|
|
386
|
+
const bare = stripComments(text)
|
|
387
|
+
|
|
388
|
+
const member = findMember(bare, provider.open, "package")
|
|
389
|
+
if (!member) return { text, applied: false, problem: "no package key in this provider" }
|
|
390
|
+
|
|
391
|
+
const spec = scalarAt(bare, member.valueAt)
|
|
392
|
+
if (!spec?.[0].startsWith('"')) return { text, applied: false, problem: "package is not a string" }
|
|
393
|
+
|
|
394
|
+
const current = JSON.parse(spec[0]) as string
|
|
395
|
+
const replacement = withVersion(current, deps.version)
|
|
396
|
+
if (!replacement) {
|
|
397
|
+
return { text, applied: false, problem: `package ${JSON.stringify(current)} is not a known @ai-sdk spec` }
|
|
398
|
+
}
|
|
399
|
+
if (current === replacement) return { text, applied: false }
|
|
400
|
+
|
|
401
|
+
return {
|
|
402
|
+
text: `${text.slice(0, member.valueAt)}${JSON.stringify(replacement)}${text.slice(member.valueAt + spec[0].length)}`,
|
|
403
|
+
applied: true,
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function skipSpace(text: string, at: number): number {
|
|
408
|
+
let i = at
|
|
409
|
+
while (i < text.length && " \t\n\r".includes(text[i])) i++
|
|
410
|
+
return i
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** The end of the JSON string starting at `at` (just past the closing quote), or -1. */
|
|
414
|
+
function stringEnd(text: string, at: number): number {
|
|
415
|
+
let i = at + 1
|
|
416
|
+
while (i < text.length) {
|
|
417
|
+
if (text[i] === "\\") i += 2
|
|
418
|
+
else if (text[i] === '"') return i + 1
|
|
419
|
+
else i++
|
|
420
|
+
}
|
|
421
|
+
return -1
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** Just past a number or literal starting at `at`, or -1. */
|
|
425
|
+
function scalarEnd(text: string, at: number): number {
|
|
426
|
+
if (text[at] === '"') return stringEnd(text, at)
|
|
427
|
+
let i = at
|
|
428
|
+
while (i < text.length && !",}] \t\n\r".includes(text[i])) i++
|
|
429
|
+
return i === at ? -1 : i
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function scalarAt(text: string, at: number): [string, number] | null {
|
|
433
|
+
const end = scalarEnd(text, at)
|
|
434
|
+
return end < 0 ? null : [text.slice(at, end), end]
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* The closing offset of the container opening at `at`, skipping nested
|
|
439
|
+
* containers and strings. [close, next] where `next` is just past the container.
|
|
440
|
+
*/
|
|
441
|
+
function containerEnd(text: string, at: number): [number, number] | null {
|
|
442
|
+
const open = text[at]
|
|
443
|
+
const close = open === "{" ? "}" : open === "[" ? "]" : null
|
|
444
|
+
if (!close) return null
|
|
445
|
+
let depth = 0
|
|
446
|
+
for (let i = at; i < text.length; ) {
|
|
447
|
+
const ch = text[i]
|
|
448
|
+
if (ch === '"') {
|
|
449
|
+
const end = stringEnd(text, i)
|
|
450
|
+
if (end < 0) return null
|
|
451
|
+
i = end
|
|
452
|
+
continue
|
|
453
|
+
}
|
|
454
|
+
if (ch === open) depth++
|
|
455
|
+
else if (ch === close) {
|
|
456
|
+
depth--
|
|
457
|
+
if (depth === 0) return [i, i + 1]
|
|
458
|
+
}
|
|
459
|
+
i++
|
|
460
|
+
}
|
|
461
|
+
return null
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/** Just past whatever value starts at `at`: a string, a number/literal, an `{}` or a `[]`. */
|
|
465
|
+
function valueEnd(text: string, at: number): number {
|
|
466
|
+
const container = containerEnd(text, at)
|
|
467
|
+
return container ? container[1] : scalarEnd(text, at)
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** The `{`/`}` of the object starting at `at`. */
|
|
471
|
+
function objectAt(text: string, at: number): ObjectRange | null {
|
|
472
|
+
const end = text[at] === "{" ? containerEnd(text, at) : null
|
|
473
|
+
return end ? { open: at, close: end[0] } : null
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** A top-level member of the object literal whose `{` is at `at`. */
|
|
477
|
+
interface Member {
|
|
478
|
+
/** Where the member's value starts. */
|
|
479
|
+
valueAt: number
|
|
480
|
+
/** True when that value is an object, for callers that need the braces. */
|
|
481
|
+
isObject: boolean
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/** Walks the object literal at `at` and returns its member named `key`, or null. */
|
|
485
|
+
function findMember(text: string, at: number, key: string): Member | null {
|
|
486
|
+
const body = objectAt(text, at)
|
|
487
|
+
if (!body) return null
|
|
488
|
+
let i = skipSpace(text, body.open + 1)
|
|
489
|
+
while (i < body.close && text[i] === '"') {
|
|
490
|
+
const keyEnd = stringEnd(text, i)
|
|
491
|
+
if (keyEnd < 0) return null
|
|
492
|
+
const colon = skipSpace(text, keyEnd)
|
|
493
|
+
if (text[colon] !== ":") return null
|
|
494
|
+
const valueAt = skipSpace(text, colon + 1)
|
|
495
|
+
if (JSON.parse(text.slice(i, keyEnd)) === key) {
|
|
496
|
+
return { valueAt, isObject: text[valueAt] === "{" }
|
|
497
|
+
}
|
|
498
|
+
const end = valueEnd(text, valueAt)
|
|
499
|
+
if (end < 0) return null
|
|
500
|
+
i = skipSpace(text, end)
|
|
501
|
+
if (text[i] !== ",") return null
|
|
502
|
+
i = skipSpace(text, i + 1)
|
|
503
|
+
}
|
|
504
|
+
return null
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/** The range of the object directly under `key`, or null when the member is missing or not an object. */
|
|
508
|
+
function memberObject(text: string, at: number, key: string): ObjectRange | null {
|
|
509
|
+
const member = findMember(text, at, key)
|
|
510
|
+
return member?.isObject ? { open: member.valueAt, close: containerEnd(text, member.valueAt)![0] } : null
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
export interface WriteProviderDeps extends WriteModelsDeps {
|
|
514
|
+
providerId: "codebuddy-cn" | "codebuddy-intl"
|
|
515
|
+
/** Port the local proxy listens on; baked into the provider's baseURL. */
|
|
516
|
+
proxyPort?: number
|
|
517
|
+
backup?: (path: string, content: string) => void
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Create (or update) a v2 provider. When the block is absent, a complete provider
|
|
522
|
+
* object is inserted into `providers` — baseURL points at the local proxy path for
|
|
523
|
+
* this site — then the models are written by the same text-level path as before.
|
|
524
|
+
* The legacy `codebuddy` block is never located or edited.
|
|
525
|
+
*/
|
|
526
|
+
export function writeProvider(deps: WriteProviderDeps): WriteModelsResult {
|
|
527
|
+
const read = deps.readFile ?? ((p: string) => readFileSync(p, "utf8"))
|
|
528
|
+
const write = deps.writeFile ?? ((p: string, c: string) => writeFileSync(p, c, "utf8"))
|
|
529
|
+
let original: string
|
|
530
|
+
try {
|
|
531
|
+
original = read(deps.jsoncPath)
|
|
532
|
+
} catch {
|
|
533
|
+
return { status: "skipped", reason: "config file could not be read" }
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
const bare = stripComments(original)
|
|
537
|
+
if (!isJsonObject(bare)) {
|
|
538
|
+
return { status: "skipped", reason: "config file is not valid JSON/JSONC" }
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// An explicitly provided package version must be valid; never silently fall
|
|
542
|
+
// back, or a stray value could reach the provider's `package` spec downstream.
|
|
543
|
+
if (deps.packageVersion !== undefined && deps.packageVersion !== null && !isVersion(deps.packageVersion)) {
|
|
544
|
+
return { status: "skipped", reason: "invalid package version" }
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
const port = deps.proxyPort ?? PROXY_PORT
|
|
548
|
+
const site = deps.providerId === "codebuddy-intl" ? "intl" : "cn"
|
|
549
|
+
const baseURL = `http://127.0.0.1:${port}/${site}/v2`
|
|
550
|
+
|
|
551
|
+
let text = original
|
|
552
|
+
if (!locateProvider(bare, deps.providerId)) {
|
|
553
|
+
// Locate the `providers` object and insert a new member just inside it.
|
|
554
|
+
const root = objectAt(bare, skipSpace(bare, 0))
|
|
555
|
+
const providers = root && memberObject(bare, root.open, "providers")
|
|
556
|
+
if (!providers) {
|
|
557
|
+
return { status: "skipped", reason: "could not locate providers object in the config text" }
|
|
558
|
+
}
|
|
559
|
+
const eol = newlineOf(original)
|
|
560
|
+
// Indent relative to the `providers` line, not the (possibly empty) line
|
|
561
|
+
// just after `{`, so a freshly inserted block is never flush-left.
|
|
562
|
+
const indent = `${indentOf(original, providers.open)} `
|
|
563
|
+
const pkg = isVersion(deps.packageVersion) ? packageSpec(deps.packageVersion) : packageSpec("2.0.90")
|
|
564
|
+
// Only append a comma when the object already has a member; appending one
|
|
565
|
+
// to an empty `{}` yields `},}}` which is only "valid" because trailing
|
|
566
|
+
// commas get blanked out — two bugs cancelling each other.
|
|
567
|
+
const inner = bare.slice(providers.open + 1, providers.close)
|
|
568
|
+
const hadMembers = skipSpace(bare, providers.open + 1) < providers.close && inner.trim().length > 0
|
|
569
|
+
const block =
|
|
570
|
+
`${eol}${indent}${JSON.stringify(deps.providerId)}: {` +
|
|
571
|
+
`${eol}${indent} "name": ${JSON.stringify(deps.providerId)},` +
|
|
572
|
+
`${eol}${indent} "package": ${JSON.stringify(pkg)},` +
|
|
573
|
+
`${eol}${indent} "compatibility": { "reasoningField": "reasoning_content" },` +
|
|
574
|
+
`${eol}${indent} "settings": { "baseURL": ${JSON.stringify(baseURL)}, "apiKey": "" },` +
|
|
575
|
+
`${eol}${indent} "models": {}` +
|
|
576
|
+
`${eol}${indent}}${hadMembers ? "," : ""}`
|
|
577
|
+
text = `${original.slice(0, providers.open + 1)}${block}${original.slice(providers.open + 1)}`
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
if (text === original) {
|
|
581
|
+
// Provider exists: delegate straight to the models writer.
|
|
582
|
+
return writeModels({
|
|
583
|
+
...deps,
|
|
584
|
+
providerId: deps.providerId,
|
|
585
|
+
readFile: () => original,
|
|
586
|
+
writeFile: deps.writeFile,
|
|
587
|
+
backup: deps.backup,
|
|
588
|
+
})
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// Freshly inserted provider: write its models into the intermediate text, then
|
|
592
|
+
// commit once. The writer callback captures the rendered text.
|
|
593
|
+
let committed = ""
|
|
594
|
+
const final = writeModels({
|
|
595
|
+
...deps,
|
|
596
|
+
providerId: deps.providerId,
|
|
597
|
+
readFile: () => text,
|
|
598
|
+
writeFile: (_p, c) => { committed = c },
|
|
599
|
+
backup: undefined,
|
|
600
|
+
})
|
|
601
|
+
if (final.status === "skipped") return final
|
|
602
|
+
if (final.status === "written") {
|
|
603
|
+
// Final gate: the new provider must land under `providers` alongside the
|
|
604
|
+
// originals — never somewhere else in the file.
|
|
605
|
+
const originalKeys = Object.keys(
|
|
606
|
+
(((JSON.parse(stripComments(original)) as { providers?: Record<string, unknown> }).providers) ?? {}),
|
|
607
|
+
)
|
|
608
|
+
const renderedKeys = Object.keys(
|
|
609
|
+
(((JSON.parse(stripComments(committed)) as { providers?: Record<string, unknown> }).providers) ?? {}),
|
|
610
|
+
)
|
|
611
|
+
const expected = [...originalKeys, deps.providerId].sort().join("\u0000")
|
|
612
|
+
if (renderedKeys.sort().join("\u0000") !== expected) {
|
|
613
|
+
return { status: "skipped", reason: "provider block did not land under providers; refusing to write" }
|
|
614
|
+
}
|
|
615
|
+
if (fileChangedOnDisk(deps, original)) {
|
|
616
|
+
return { status: "skipped", reason: "config file changed on disk while editing; refusing to overwrite" }
|
|
617
|
+
}
|
|
618
|
+
deps.backup?.(deps.jsoncPath, original)
|
|
619
|
+
write(deps.jsoncPath, committed)
|
|
620
|
+
}
|
|
621
|
+
return final
|
|
622
|
+
}
|