@cortexkit/opencode-openai-auth 1.14.1 → 1.15.1
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/dist/bin.js +9 -9
- package/dist/{codex-request-Cl6zEfsR.d.ts → codex-request-DYqSE0ti.d.ts} +373 -2
- package/dist/{index-wma2ggn9.js → index-14x5shtc.js} +2 -2
- package/dist/{index-gkye2xh1.js → index-1scvhkyy.js} +2 -2
- package/dist/index-1wcg7g9d.js +2 -0
- package/dist/index-50p400eg.js +8 -0
- package/dist/{index-k5av4j8p.js → index-dzpjz8hv.js} +2 -2
- package/dist/{index-cxnrd85v.js → index-f7kmsdfk.js} +1 -1
- package/dist/index-fkgd5yv6.js +76 -0
- package/dist/{index-z9cj13z7.js → index-hnfqppx9.js} +1 -1
- package/dist/index-j2qfnmth.js +64 -0
- package/dist/{index-2f330s35.js → index-m8m7rakq.js} +2 -2
- package/dist/{index-94b345dw.js → index-t65axpta.js} +1 -1
- package/dist/{index-yygzbxjk.js → index-tes6vn59.js} +1 -1
- package/dist/index-xjtbg7sx.js +101 -0
- package/dist/index.d.ts +1 -372
- package/dist/index.js +1 -1
- package/dist/openai-auth.schema.json +400 -0
- package/dist/rpc/port-file.js +1 -1
- package/dist/rpc/protocol.js +1 -1
- package/dist/rpc/rpc-client.js +1 -1
- package/dist/rpc/rpc-dir.js +1 -1
- package/dist/sidebar-state.js +1 -1
- package/dist/{token-sgwxn4xg.js → token-j34j1zhm.js} +1 -1
- package/dist/token-util-bf619nmg.js +1 -0
- package/dist/tui-preferences.js +3 -3
- package/dist/v2/server.d.ts +1 -1
- package/dist/v2/server.js +2 -2
- package/package.json +3 -3
- package/src/tui-compiled/raw/logger.js +8 -1
- package/src/tui-compiled/raw/shared/061e741c052de9c1-paths.js +48 -0
- package/src/tui-compiled/raw/shared/0766d78c6f481f18-watcher.js +41 -18
- package/src/tui-compiled/raw/shared/23300c068d0b2ffc-parse.js +65 -0
- package/src/tui-compiled/raw/shared/3000be4f79f20eb6-write.js +88 -0
- package/src/tui-compiled/raw/shared/44449e891fab9fbc-edit.js +229 -0
- package/src/tui-compiled/raw/shared/4825e03209d6ae3e-with-lock.js +1 -1
- package/src/tui-compiled/raw/shared/5803fadb30af0ec0-descriptors.js +81 -0
- package/src/tui-compiled/raw/shared/58bc35a3cd459085-define.js +52 -0
- package/src/tui-compiled/raw/shared/5f4bc45522ab9bc6-update.js +139 -0
- package/src/tui-compiled/raw/shared/6948d70969aa7a55-fs.js +19 -0
- package/src/tui-compiled/raw/shared/754d6017f8e419cf-builtins.js +28 -12
- package/src/tui-compiled/raw/shared/81758f9fe7c2a576-handle.js +228 -0
- package/src/tui-compiled/raw/shared/87f6e22d4fc34918-resolve.js +51 -0
- package/src/tui-compiled/raw/shared/8a18abe4fa2977c8-menu.js +1 -0
- package/src/tui-compiled/raw/shared/8ab1f5794e677151-move.js +351 -0
- package/src/tui-compiled/raw/shared/92e38944b51b2405-file-read-cache.js +9 -5
- package/src/tui-compiled/raw/shared/9865a4fa45882c6f-settings-decisions.js +13 -0
- package/src/tui-compiled/raw/shared/cc0f2d1963547799-index.js +7 -0
- package/src/tui-compiled/raw/shared/dbc61cd405941828-accounts.js +48 -5
- package/src/tui-compiled/raw/shared/e0ed4a490e4eaa4f-schema.js +81 -0
- package/src/tui-compiled/raw/shared/e8cd4929507508d8-refresh-file-lock.js +146 -2
- package/src/tui-compiled/raw/shared/eac0bed49f3d4110-lock-errors.js +15 -0
- package/src/tui-compiled/raw/shared/facfd8afc431d68a-refusal.js +15 -0
- package/src/tui-compiled/raw/shared/fc7db0c5aa764565-settings-install.js +85 -0
- package/src/tui-compiled/raw/shared/fcd26e77bc538838-snapshot.js +150 -0
- package/src/tui-compiled/runtime/logger.js +8 -1
- package/src/tui-compiled/runtime/shared/061e741c052de9c1-paths.js +48 -0
- package/src/tui-compiled/runtime/shared/0766d78c6f481f18-watcher.js +41 -18
- package/src/tui-compiled/runtime/shared/23300c068d0b2ffc-parse.js +65 -0
- package/src/tui-compiled/runtime/shared/3000be4f79f20eb6-write.js +88 -0
- package/src/tui-compiled/runtime/shared/44449e891fab9fbc-edit.js +229 -0
- package/src/tui-compiled/runtime/shared/4825e03209d6ae3e-with-lock.js +1 -1
- package/src/tui-compiled/runtime/shared/5803fadb30af0ec0-descriptors.js +81 -0
- package/src/tui-compiled/runtime/shared/58bc35a3cd459085-define.js +52 -0
- package/src/tui-compiled/runtime/shared/5f4bc45522ab9bc6-update.js +139 -0
- package/src/tui-compiled/runtime/shared/6948d70969aa7a55-fs.js +19 -0
- package/src/tui-compiled/runtime/shared/754d6017f8e419cf-builtins.js +28 -12
- package/src/tui-compiled/runtime/shared/81758f9fe7c2a576-handle.js +228 -0
- package/src/tui-compiled/runtime/shared/87f6e22d4fc34918-resolve.js +51 -0
- package/src/tui-compiled/runtime/shared/8a18abe4fa2977c8-menu.js +1 -0
- package/src/tui-compiled/runtime/shared/8ab1f5794e677151-move.js +351 -0
- package/src/tui-compiled/runtime/shared/92e38944b51b2405-file-read-cache.js +9 -5
- package/src/tui-compiled/runtime/shared/9865a4fa45882c6f-settings-decisions.js +13 -0
- package/src/tui-compiled/runtime/shared/cc0f2d1963547799-index.js +7 -0
- package/src/tui-compiled/runtime/shared/dbc61cd405941828-accounts.js +48 -5
- package/src/tui-compiled/runtime/shared/e0ed4a490e4eaa4f-schema.js +81 -0
- package/src/tui-compiled/runtime/shared/e8cd4929507508d8-refresh-file-lock.js +146 -2
- package/src/tui-compiled/runtime/shared/eac0bed49f3d4110-lock-errors.js +15 -0
- package/src/tui-compiled/runtime/shared/facfd8afc431d68a-refusal.js +15 -0
- package/src/tui-compiled/runtime/shared/fc7db0c5aa764565-settings-install.js +85 -0
- package/src/tui-compiled/runtime/shared/fcd26e77bc538838-snapshot.js +150 -0
- package/dist/index-3mbw88h7.js +0 -91
- package/dist/index-999nwa9p.js +0 -76
- package/dist/index-pc4vrrex.js +0 -2
- package/dist/index-snq7bwr6.js +0 -8
- package/dist/index-xk7f1aef.js +0 -64
- package/dist/token-util-8mrnmkge.js +0 -1
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { basename, dirname, join } from "node:path";
|
|
2
|
+
import { ensurePrivateDirectory } from "./375d9cca2def037d-private-files.js";
|
|
3
|
+
import { withLock } from "./4825e03209d6ae3e-with-lock.js";
|
|
4
|
+
import { applySettingsEdits, } from "./44449e891fab9fbc-edit.js";
|
|
5
|
+
import { SettingsFs } from "./6948d70969aa7a55-fs.js";
|
|
6
|
+
import { rethrowSettingsLockError } from "./eac0bed49f3d4110-lock-errors.js";
|
|
7
|
+
import { parseSettings } from "./23300c068d0b2ffc-parse.js";
|
|
8
|
+
import { SettingsRefusal } from "./facfd8afc431d68a-refusal.js";
|
|
9
|
+
import { writeSettings } from "./3000be4f79f20eb6-write.js";
|
|
10
|
+
const SETTINGS_WRITE_LOCK = Object.freeze({
|
|
11
|
+
name: 'settings-write',
|
|
12
|
+
ttlMs: 10_000,
|
|
13
|
+
timeoutMs: 2_000,
|
|
14
|
+
renew: true,
|
|
15
|
+
});
|
|
16
|
+
function unavailable(file, cause) {
|
|
17
|
+
throw new SettingsRefusal('settings-unavailable', `Settings file is unavailable: ${file}`, { cause });
|
|
18
|
+
}
|
|
19
|
+
async function writeTarget(file) {
|
|
20
|
+
try {
|
|
21
|
+
return await SettingsFs.realpath(file);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (error.code !== 'ENOENT')
|
|
25
|
+
unavailable(file, error);
|
|
26
|
+
try {
|
|
27
|
+
await SettingsFs.lstat(file);
|
|
28
|
+
}
|
|
29
|
+
catch (lstatError) {
|
|
30
|
+
if (lstatError.code !== 'ENOENT')
|
|
31
|
+
unavailable(file, lstatError);
|
|
32
|
+
try {
|
|
33
|
+
return join(await SettingsFs.realpath(dirname(file)), basename(file));
|
|
34
|
+
}
|
|
35
|
+
catch (parentError) {
|
|
36
|
+
unavailable(file, parentError);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// A directory entry whose realpath is missing is a dangling symlink.
|
|
40
|
+
unavailable(file, error);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Lock the canonical user file so symlink aliases serialize the same edits. */
|
|
44
|
+
export async function updateSettings(definition, options, edits, context) {
|
|
45
|
+
if (options.separateInstall) {
|
|
46
|
+
throw new SettingsRefusal('settings-unavailable', 'Settings are unavailable for a separate install');
|
|
47
|
+
}
|
|
48
|
+
const folder = dirname(options.file);
|
|
49
|
+
try {
|
|
50
|
+
if (!options.createMissing)
|
|
51
|
+
await SettingsFs.stat(folder);
|
|
52
|
+
else if (process.platform === 'win32')
|
|
53
|
+
await ensurePrivateDirectory(folder);
|
|
54
|
+
else
|
|
55
|
+
await SettingsFs.mkdir(folder, { recursive: true, mode: 0o700 });
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
unavailable(options.file, error);
|
|
59
|
+
}
|
|
60
|
+
const target = await writeTarget(options.file);
|
|
61
|
+
let settled;
|
|
62
|
+
try {
|
|
63
|
+
return await withLock(target, {
|
|
64
|
+
...SETTINGS_WRITE_LOCK,
|
|
65
|
+
timeoutMs: options.lockTimeoutMs ?? SETTINGS_WRITE_LOCK.timeoutMs,
|
|
66
|
+
}, async (lock) => {
|
|
67
|
+
try {
|
|
68
|
+
if ((await writeTarget(options.file)) !== target) {
|
|
69
|
+
throw new SettingsRefusal('settings-busy', `Settings target changed while locking: ${options.file}`);
|
|
70
|
+
}
|
|
71
|
+
let source;
|
|
72
|
+
let existingMode;
|
|
73
|
+
let missing = false;
|
|
74
|
+
try {
|
|
75
|
+
source = await SettingsFs.readFile(target, 'utf8');
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (error.code !== 'ENOENT' ||
|
|
79
|
+
!options.createMissing)
|
|
80
|
+
unavailable(target, error);
|
|
81
|
+
missing = true;
|
|
82
|
+
source = '';
|
|
83
|
+
}
|
|
84
|
+
if (!missing) {
|
|
85
|
+
try {
|
|
86
|
+
existingMode = (await SettingsFs.stat(target)).mode;
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
unavailable(target, error);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const parsed = parseSettings(source);
|
|
93
|
+
if (!parsed.parseable || parsed.hasDuplicates) {
|
|
94
|
+
throw new SettingsRefusal('settings-unparseable', 'Settings must be an object without duplicate keys');
|
|
95
|
+
}
|
|
96
|
+
const version = parsed.value.schema_version;
|
|
97
|
+
if (Number.isInteger(version) && version > 1) {
|
|
98
|
+
throw new SettingsRefusal('settings-newer-version', 'Settings were written by a newer version');
|
|
99
|
+
}
|
|
100
|
+
if (!parsed.tree) {
|
|
101
|
+
source = `${parsed.hasBom ? '\uFEFF' : ''}// ${definition.plugin} settings\n{\n "schema_version": 1\n}\n`;
|
|
102
|
+
}
|
|
103
|
+
const edited = applySettingsEdits(definition, source, edits, context);
|
|
104
|
+
const result = edited.outcome === 'unchanged'
|
|
105
|
+
? { outcome: 'unchanged' }
|
|
106
|
+
: await writeSettings({
|
|
107
|
+
target,
|
|
108
|
+
text: edited.text,
|
|
109
|
+
hasBom: edited.hasBom,
|
|
110
|
+
existingMode,
|
|
111
|
+
lock,
|
|
112
|
+
logger: options.log,
|
|
113
|
+
});
|
|
114
|
+
settled = { outcome: 'fulfilled', result };
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
settled = { outcome: 'rejected', error };
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
if (!settled)
|
|
125
|
+
rethrowSettingsLockError(error, target, SETTINGS_WRITE_LOCK.name);
|
|
126
|
+
// Release runs after the callback; it cannot undo a settings write or refusal.
|
|
127
|
+
if (settled.outcome === 'fulfilled' || error !== settled.error) {
|
|
128
|
+
try {
|
|
129
|
+
options.log.warn('Settings lock release failed', { target, error });
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
// A logger failure must not replace the callback's own outcome either.
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (settled.outcome === 'rejected')
|
|
136
|
+
throw settled.error;
|
|
137
|
+
return settled.result;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as fs from "node:fs/promises";
|
|
2
|
+
const defaults = {
|
|
3
|
+
readFile: fs.readFile,
|
|
4
|
+
stat: fs.stat,
|
|
5
|
+
lstat: fs.lstat,
|
|
6
|
+
realpath: fs.realpath,
|
|
7
|
+
open: fs.open,
|
|
8
|
+
rename: fs.rename,
|
|
9
|
+
link: fs.link,
|
|
10
|
+
unlink: fs.unlink,
|
|
11
|
+
mkdir: fs.mkdir,
|
|
12
|
+
readdir: fs.readdir,
|
|
13
|
+
};
|
|
14
|
+
// Keep one object so readers, writers and directory sync use the same filesystem.
|
|
15
|
+
export const SettingsFs = { ...defaults };
|
|
16
|
+
/** Internal test hook; omitted methods always use the native implementation. */
|
|
17
|
+
export function setSettingsFsForTest(overrides) {
|
|
18
|
+
Object.assign(SettingsFs, defaults, overrides);
|
|
19
|
+
}
|
|
@@ -65,15 +65,31 @@ function addedText(name, result) {
|
|
|
65
65
|
function failureMessage(error) {
|
|
66
66
|
return projectFailure(error).text;
|
|
67
67
|
}
|
|
68
|
+
/** The plugin's settings port, or one over the account store's own settings. */
|
|
69
|
+
function settingsPort(options) {
|
|
70
|
+
if (options.settings)
|
|
71
|
+
return options.settings;
|
|
72
|
+
const { store, extraLocks } = options;
|
|
73
|
+
const locks = extraLocks ? { extraLocks } : {};
|
|
74
|
+
return {
|
|
75
|
+
async read() {
|
|
76
|
+
const read = await store.readSettings();
|
|
77
|
+
return read.status === 'error' ? {} : read.settings;
|
|
78
|
+
},
|
|
79
|
+
async update(mutator) {
|
|
80
|
+
await store.updateSettings(mutator, locks);
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
68
84
|
async function snapshot(options) {
|
|
69
85
|
const { store } = options;
|
|
70
86
|
const load = await store.read();
|
|
71
|
-
const
|
|
87
|
+
const settings = await settingsPort(options).read();
|
|
72
88
|
const vault = await options.routing?.vaultOrder?.();
|
|
73
89
|
return {
|
|
74
90
|
load,
|
|
75
91
|
rows: distinctRows(load),
|
|
76
|
-
settings
|
|
92
|
+
settings,
|
|
77
93
|
...(vault ? { vault } : {}),
|
|
78
94
|
};
|
|
79
95
|
}
|
|
@@ -396,6 +412,7 @@ function vaultNames(vault) {
|
|
|
396
412
|
function routingSection(options, snap) {
|
|
397
413
|
const { store, extraLocks } = options;
|
|
398
414
|
const locks = extraLocks ? { extraLocks } : {};
|
|
415
|
+
const port = settingsPort(options);
|
|
399
416
|
const choices = [
|
|
400
417
|
{ value: 'ordered', label: 'Ordered (roster order)' },
|
|
401
418
|
...(options.routing?.orderedVariants ?? []),
|
|
@@ -428,10 +445,10 @@ function routingSection(options, snap) {
|
|
|
428
445
|
],
|
|
429
446
|
run: async ({ values }) => {
|
|
430
447
|
const mode = String(values.mode);
|
|
431
|
-
await
|
|
448
|
+
await port.update((settings) => {
|
|
432
449
|
settings.routing = { ...record(settings.routing), mode };
|
|
433
450
|
return undefined;
|
|
434
|
-
}
|
|
451
|
+
});
|
|
435
452
|
return `Routing mode set to ${choices.find((choice) => choice.value === mode)?.label ?? mode}.`;
|
|
436
453
|
},
|
|
437
454
|
},
|
|
@@ -495,13 +512,13 @@ function routingSection(options, snap) {
|
|
|
495
512
|
run: async ({ values }) => {
|
|
496
513
|
const value = String(values.maxSameAccountWait);
|
|
497
514
|
const maxSameAccountWait = value === 'max' ? 'max' : Number(value);
|
|
498
|
-
await
|
|
515
|
+
await port.update((settings) => {
|
|
499
516
|
settings.routing = {
|
|
500
517
|
...record(settings.routing),
|
|
501
518
|
maxSameAccountWait,
|
|
502
519
|
};
|
|
503
520
|
return undefined;
|
|
504
|
-
}
|
|
521
|
+
});
|
|
505
522
|
return `${waitLabel} set to ${waitName(value)}.`;
|
|
506
523
|
},
|
|
507
524
|
});
|
|
@@ -541,8 +558,7 @@ function floorNames(options, snap) {
|
|
|
541
558
|
return (label) => names.get(label) ?? label;
|
|
542
559
|
}
|
|
543
560
|
function limitsSection(options, snap) {
|
|
544
|
-
const
|
|
545
|
-
const locks = extraLocks ? { extraLocks } : {};
|
|
561
|
+
const port = settingsPort(options);
|
|
546
562
|
const killswitch = record(snap.settings.killswitch);
|
|
547
563
|
const enabled = killswitch.enabled === true;
|
|
548
564
|
const floors = record(killswitch.accounts);
|
|
@@ -577,7 +593,7 @@ function limitsSection(options, snap) {
|
|
|
577
593
|
: {}),
|
|
578
594
|
})),
|
|
579
595
|
run: async ({ values }) => {
|
|
580
|
-
await
|
|
596
|
+
await port.update((settings) => {
|
|
581
597
|
const next = record(settings.killswitch);
|
|
582
598
|
const accounts = record(next.accounts);
|
|
583
599
|
const mine = record(accounts[row.id]);
|
|
@@ -598,7 +614,7 @@ function limitsSection(options, snap) {
|
|
|
598
614
|
delete next.accounts;
|
|
599
615
|
settings.killswitch = next;
|
|
600
616
|
return undefined;
|
|
601
|
-
}
|
|
617
|
+
});
|
|
602
618
|
return `Floors updated for ${name}.`;
|
|
603
619
|
},
|
|
604
620
|
},
|
|
@@ -628,13 +644,13 @@ function limitsSection(options, snap) {
|
|
|
628
644
|
],
|
|
629
645
|
run: async ({ values }) => {
|
|
630
646
|
const on = values.enabled === true;
|
|
631
|
-
await
|
|
647
|
+
await port.update((settings) => {
|
|
632
648
|
settings.killswitch = {
|
|
633
649
|
...record(settings.killswitch),
|
|
634
650
|
enabled: on,
|
|
635
651
|
};
|
|
636
652
|
return undefined;
|
|
637
|
-
}
|
|
653
|
+
});
|
|
638
654
|
return `Killswitch ${on ? 'on' : 'off'}.`;
|
|
639
655
|
},
|
|
640
656
|
},
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { join, resolve } from "node:path";
|
|
2
|
+
import { FileReadCache } from "./92e38944b51b2405-file-read-cache.js";
|
|
3
|
+
import { watchSettings } from "./0766d78c6f481f18-watcher.js";
|
|
4
|
+
import { isJsonObject } from "./5803fadb30af0ec0-descriptors.js";
|
|
5
|
+
import { SettingsFs } from "./6948d70969aa7a55-fs.js";
|
|
6
|
+
import { parseSettings } from "./23300c068d0b2ffc-parse.js";
|
|
7
|
+
import { resolveSetting } from "./87f6e22d4fc34918-resolve.js";
|
|
8
|
+
import { buildSettingsSnapshot, } from "./fcd26e77bc538838-snapshot.js";
|
|
9
|
+
import { updateSettings } from "./5f4bc45522ab9bc6-update.js";
|
|
10
|
+
const reportedKey = Symbol.for('cortexkit.common-auth.settings.reported.v1');
|
|
11
|
+
function reportedEntries() {
|
|
12
|
+
let reported = Reflect.get(globalThis, reportedKey);
|
|
13
|
+
if (!reported) {
|
|
14
|
+
reported = new Set();
|
|
15
|
+
Reflect.set(globalThis, reportedKey, reported);
|
|
16
|
+
}
|
|
17
|
+
return reported;
|
|
18
|
+
}
|
|
19
|
+
/** Internal test hook; not part of the settings package exports. */
|
|
20
|
+
export function resetSettingsReportedForTest() {
|
|
21
|
+
reportedEntries().clear();
|
|
22
|
+
}
|
|
23
|
+
export function openSettings(definition, options) {
|
|
24
|
+
options = { createMissing: false, lockTimeoutMs: 2_000, ...options };
|
|
25
|
+
const userPath = resolve(options.file);
|
|
26
|
+
const cacheOptions = {
|
|
27
|
+
followSymlinks: true,
|
|
28
|
+
// Read SettingsFs at call time so setSettingsFsForTest reaches open handles.
|
|
29
|
+
stat: (path) => SettingsFs.stat(path),
|
|
30
|
+
lstat: (path) => SettingsFs.lstat(path),
|
|
31
|
+
};
|
|
32
|
+
const userCache = new FileReadCache(cacheOptions);
|
|
33
|
+
const projectCache = new FileReadCache(cacheOptions);
|
|
34
|
+
let user;
|
|
35
|
+
const projects = new Map();
|
|
36
|
+
// A project's report and resolution use only the source selected by its read.
|
|
37
|
+
const projectLevels = new Map();
|
|
38
|
+
const projectSequences = new Map();
|
|
39
|
+
let sequence = 0;
|
|
40
|
+
// Keep ordering metadata after eviction so an older in-flight read cannot win.
|
|
41
|
+
const committedSequences = new Map();
|
|
42
|
+
const projectPath = (project) => resolve(join(project, '.cortexkit', `${definition.plugin}.jsonc`));
|
|
43
|
+
// Windows reads every time (below), so remember each file's last bytes to
|
|
44
|
+
// tell a changed file from an unchanged one, as the cache does elsewhere.
|
|
45
|
+
const windows = process.platform === 'win32';
|
|
46
|
+
const windowsSeen = new Map();
|
|
47
|
+
const unreadFile = {
|
|
48
|
+
missing: Symbol('missing'),
|
|
49
|
+
unreadable: Symbol('unreadable'),
|
|
50
|
+
};
|
|
51
|
+
async function load(file, reader) {
|
|
52
|
+
const currentSequence = ++sequence;
|
|
53
|
+
const cache = file.kind === 'user' ? userCache : projectCache;
|
|
54
|
+
let loaded = false;
|
|
55
|
+
const seen = (path, content) => {
|
|
56
|
+
if (!windows) {
|
|
57
|
+
// The cache calls this reader only when the file changed.
|
|
58
|
+
loaded = true;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
loaded = windowsSeen.get(path) !== content;
|
|
62
|
+
windowsSeen.set(path, content);
|
|
63
|
+
};
|
|
64
|
+
const readFile = async ([path]) => {
|
|
65
|
+
let text;
|
|
66
|
+
try {
|
|
67
|
+
text = await SettingsFs.readFile(path, 'utf8');
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
const status = error.code === 'ENOENT'
|
|
71
|
+
? 'missing'
|
|
72
|
+
: 'unreadable';
|
|
73
|
+
seen(path, unreadFile[status]);
|
|
74
|
+
return { status };
|
|
75
|
+
}
|
|
76
|
+
seen(path, text);
|
|
77
|
+
let parsed;
|
|
78
|
+
if (reader) {
|
|
79
|
+
let value;
|
|
80
|
+
try {
|
|
81
|
+
value = reader(text);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return { status: 'unreadable' };
|
|
85
|
+
}
|
|
86
|
+
if (!isJsonObject(value))
|
|
87
|
+
return { status: 'unparseable' };
|
|
88
|
+
// Legacy readers return an object keyed by setting name, not JSONC text.
|
|
89
|
+
parsed = {
|
|
90
|
+
parseable: true,
|
|
91
|
+
text,
|
|
92
|
+
hasBom: false,
|
|
93
|
+
tree: undefined,
|
|
94
|
+
value,
|
|
95
|
+
duplicateKeyPaths: [],
|
|
96
|
+
hasDuplicates: false,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
parsed = parseSettings(text);
|
|
101
|
+
}
|
|
102
|
+
return parsed.parseable
|
|
103
|
+
? {
|
|
104
|
+
status: 'snapshot',
|
|
105
|
+
snapshot: buildSettingsSnapshot(definition, file, parsed),
|
|
106
|
+
}
|
|
107
|
+
: { status: 'unparseable' };
|
|
108
|
+
};
|
|
109
|
+
// On Windows a quick same-size rewrite can keep the size and timestamps
|
|
110
|
+
// the cache compares, so read the file every time instead of using the cache.
|
|
111
|
+
const outcome = process.platform === 'win32'
|
|
112
|
+
? await readFile([file.path])
|
|
113
|
+
: await cache.read([file.path], readFile);
|
|
114
|
+
const key = JSON.stringify([file.kind, file.path]);
|
|
115
|
+
if (currentSequence <= (committedSequences.get(key) ?? 0))
|
|
116
|
+
return;
|
|
117
|
+
committedSequences.set(key, currentSequence);
|
|
118
|
+
const previous = file.kind === 'user' ? user : projects.get(file.path);
|
|
119
|
+
const snapshot = outcome.status === 'snapshot'
|
|
120
|
+
? outcome.snapshot
|
|
121
|
+
: outcome.status === 'missing'
|
|
122
|
+
? undefined
|
|
123
|
+
: previous?.snapshot;
|
|
124
|
+
const entries = [...(snapshot?.entries ?? [])];
|
|
125
|
+
if (outcome.status === 'unparseable' || outcome.status === 'unreadable') {
|
|
126
|
+
entries.unshift({
|
|
127
|
+
file: { ...file },
|
|
128
|
+
keyPath: [],
|
|
129
|
+
reason: outcome.status,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
const state = { status: outcome.status, snapshot, entries };
|
|
133
|
+
if (file.kind === 'user') {
|
|
134
|
+
user = state;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
// Cache hits do not refresh insertion order; changed files do.
|
|
138
|
+
if (loaded)
|
|
139
|
+
projects.delete(file.path);
|
|
140
|
+
if (!projects.has(file.path) && projects.size >= 64) {
|
|
141
|
+
const oldest = projects.keys().next().value;
|
|
142
|
+
if (oldest !== undefined) {
|
|
143
|
+
projects.delete(oldest);
|
|
144
|
+
windowsSeen.delete(oldest);
|
|
145
|
+
for (const [project, selected] of projectLevels) {
|
|
146
|
+
if (selected === oldest)
|
|
147
|
+
projectLevels.delete(project);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
projects.set(file.path, state);
|
|
152
|
+
}
|
|
153
|
+
const reported = reportedEntries();
|
|
154
|
+
for (const entry of entries) {
|
|
155
|
+
const identity = JSON.stringify([
|
|
156
|
+
entry.file.path,
|
|
157
|
+
entry.keyPath,
|
|
158
|
+
entry.reason,
|
|
159
|
+
]);
|
|
160
|
+
if (reported.has(identity))
|
|
161
|
+
continue;
|
|
162
|
+
reported.add(identity);
|
|
163
|
+
options.log.warn(`Settings ${entry.reason}: ${entry.file.path} (${entry.keyPath.join('.') || '<root>'})`, structuredClone(entry));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
async function readProject(project) {
|
|
167
|
+
const path = projectPath(project);
|
|
168
|
+
const currentSequence = ++sequence;
|
|
169
|
+
await load({ kind: 'project', path });
|
|
170
|
+
if (currentSequence <= (projectSequences.get(path) ?? 0))
|
|
171
|
+
return;
|
|
172
|
+
let selected = path;
|
|
173
|
+
if (projects.get(path)?.status === 'missing') {
|
|
174
|
+
const source = options.legacyProject?.(project);
|
|
175
|
+
if (source) {
|
|
176
|
+
selected = resolve(source.path);
|
|
177
|
+
await load({ kind: 'legacy-project', path: selected }, (text) => source.read(text));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
// A slow legacy reader must not replace a newer read's project-file choice.
|
|
181
|
+
if (currentSequence <= (projectSequences.get(path) ?? 0))
|
|
182
|
+
return;
|
|
183
|
+
projectSequences.set(path, currentSequence);
|
|
184
|
+
projectLevels.set(path, selected);
|
|
185
|
+
}
|
|
186
|
+
const handle = {
|
|
187
|
+
async read({ project } = {}) {
|
|
188
|
+
if (options.separateInstall)
|
|
189
|
+
return;
|
|
190
|
+
await Promise.all([
|
|
191
|
+
load({ kind: 'user', path: userPath }),
|
|
192
|
+
...(project === undefined ? [] : [readProject(project)]),
|
|
193
|
+
]);
|
|
194
|
+
},
|
|
195
|
+
async get(name, getOptions) {
|
|
196
|
+
if (!Object.hasOwn(definition.settings, name)) {
|
|
197
|
+
throw new TypeError(`Undeclared setting: ${name}`);
|
|
198
|
+
}
|
|
199
|
+
await handle.read({ project: getOptions.project });
|
|
200
|
+
return resolveSetting(definition, name, getOptions, {
|
|
201
|
+
user: user?.snapshot,
|
|
202
|
+
project: getOptions.project === undefined
|
|
203
|
+
? undefined
|
|
204
|
+
: projects.get(projectLevels.get(projectPath(getOptions.project)) ?? '')?.snapshot,
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
report() {
|
|
208
|
+
return structuredClone([
|
|
209
|
+
...(user?.entries ?? []),
|
|
210
|
+
...Array.from(new Set(projectLevels.values())).flatMap((path) => projects.get(path)?.entries ?? []),
|
|
211
|
+
]);
|
|
212
|
+
},
|
|
213
|
+
update(edits, context) {
|
|
214
|
+
return updateSettings(definition, { ...options, file: userPath }, edits, context);
|
|
215
|
+
},
|
|
216
|
+
watch(onChange) {
|
|
217
|
+
if (options.separateInstall)
|
|
218
|
+
return () => { };
|
|
219
|
+
return watchSettings(userPath, onChange, {
|
|
220
|
+
fs: {
|
|
221
|
+
stat: (path) => SettingsFs.stat(path),
|
|
222
|
+
readFile: (path) => SettingsFs.readFile(path, 'utf8'),
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
return handle;
|
|
228
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { isJsonObject, } from "./5803fadb30af0ec0-descriptors.js";
|
|
2
|
+
import { defineSettingsKey } from "./23300c068d0b2ffc-parse.js";
|
|
3
|
+
function valueAtLevel(declaration, name, snapshot, options) {
|
|
4
|
+
if (!snapshot || !Object.hasOwn(snapshot.settings, name))
|
|
5
|
+
return undefined;
|
|
6
|
+
const value = snapshot.settings[name];
|
|
7
|
+
if (declaration.overrides === undefined || !isJsonObject(value))
|
|
8
|
+
return value;
|
|
9
|
+
const { model, host } = options;
|
|
10
|
+
if (declaration.overrides.model &&
|
|
11
|
+
model !== undefined &&
|
|
12
|
+
model !== 'default' &&
|
|
13
|
+
model !== 'opencode' &&
|
|
14
|
+
model !== 'pi' &&
|
|
15
|
+
Object.hasOwn(value, model)) {
|
|
16
|
+
return value[model];
|
|
17
|
+
}
|
|
18
|
+
if (declaration.overrides.host &&
|
|
19
|
+
(host === 'opencode' || host === 'pi') &&
|
|
20
|
+
Object.hasOwn(value, host)) {
|
|
21
|
+
return value[host];
|
|
22
|
+
}
|
|
23
|
+
return Object.hasOwn(value, 'default') ? value.default : undefined;
|
|
24
|
+
}
|
|
25
|
+
/** Snapshot validation has already removed unusable values and unknown fields. */
|
|
26
|
+
function resolveValue(descriptor, levels) {
|
|
27
|
+
if (descriptor.kind !== 'object') {
|
|
28
|
+
return levels.find((value) => value !== undefined);
|
|
29
|
+
}
|
|
30
|
+
const result = {};
|
|
31
|
+
for (const [key, field] of Object.entries(descriptor.fields)) {
|
|
32
|
+
const candidates = levels.map((value) => isJsonObject(value) && Object.hasOwn(value, key) ? value[key] : undefined);
|
|
33
|
+
defineSettingsKey(result, key, resolveValue(field, candidates));
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
/** Resolve sanitized snapshots without reading files or adding report entries. */
|
|
38
|
+
export function resolveSetting(definition, name, options, snapshots = {}) {
|
|
39
|
+
if (!Object.hasOwn(definition.settings, name)) {
|
|
40
|
+
throw new TypeError(`Undeclared setting: ${name}`);
|
|
41
|
+
}
|
|
42
|
+
const declaration = definition.settings[name];
|
|
43
|
+
const levels = [];
|
|
44
|
+
if (declaration.project === 'allowed' && options.project !== undefined) {
|
|
45
|
+
levels.push(valueAtLevel(declaration, name, snapshots.project, options));
|
|
46
|
+
}
|
|
47
|
+
levels.push(valueAtLevel(declaration, name, snapshots.user, options));
|
|
48
|
+
levels.push(declaration.default);
|
|
49
|
+
// Clone even a whole-list or whole-record winner so callers cannot mutate it.
|
|
50
|
+
return structuredClone(resolveValue(declaration.value, levels));
|
|
51
|
+
}
|
|
@@ -156,6 +156,7 @@ export function createCommandMenu(options) {
|
|
|
156
156
|
store,
|
|
157
157
|
now,
|
|
158
158
|
...(options.extraLocks ? { extraLocks: options.extraLocks } : {}),
|
|
159
|
+
...(options.settings ? { settings: options.settings } : {}),
|
|
159
160
|
...(options.accounts ? { accounts: options.accounts } : {}),
|
|
160
161
|
...(options.quota ? { quota: options.quota } : {}),
|
|
161
162
|
...(options.routing ? { routing: options.routing } : {}),
|