omnicell-router 0.8.1 → 0.8.4
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/backend/dist/auth/api-keys.js +11 -0
- package/backend/dist/auth/key-models.d.ts +36 -0
- package/backend/dist/auth/key-models.js +99 -0
- package/backend/dist/cli/clients/activity.d.ts +35 -0
- package/backend/dist/cli/clients/activity.js +69 -0
- package/backend/dist/cli/clients/codex.d.ts +70 -0
- package/backend/dist/cli/clients/codex.js +470 -57
- package/backend/dist/cli/clients/follow-catalog-sync.d.ts +62 -0
- package/backend/dist/cli/clients/follow-catalog-sync.js +435 -0
- package/backend/dist/cli/clients/index.js +7 -1
- package/backend/dist/cli/clients/opencode.js +3 -2
- package/backend/dist/cli/clients/reserved.js +2 -0
- package/backend/dist/cli/types.d.ts +18 -0
- package/backend/dist/config/comments-en.js +5 -1
- package/backend/dist/config/comments-zh.js +5 -1
- package/backend/dist/config/defaults.d.ts +98 -1
- package/backend/dist/config/defaults.js +98 -1
- package/backend/dist/config/index.d.ts +15 -1
- package/backend/dist/config/index.js +35 -1
- package/backend/dist/config/migrations/v4-to-v5.d.ts +36 -0
- package/backend/dist/config/migrations/v4-to-v5.js +42 -0
- package/backend/dist/config/migrations/v5-to-v6.d.ts +27 -0
- package/backend/dist/config/migrations/v5-to-v6.js +29 -0
- package/backend/dist/config/schema.d.ts +3 -3
- package/backend/dist/config/schema.js +8 -4
- package/backend/dist/config/types.d.ts +54 -3
- package/backend/dist/forward-proxy/egress.js +13 -0
- package/backend/dist/forward-proxy/h2-forward.js +6 -5
- package/backend/dist/forward-proxy/index.js +5 -0
- package/backend/dist/forward-proxy/server.d.ts +12 -0
- package/backend/dist/forward-proxy/server.js +113 -15
- package/backend/dist/index.js +6 -1
- package/backend/dist/ir/adapters/openai-chat.js +5 -3
- package/backend/dist/ir/adapters/openai-responses-output.js +2 -2
- package/backend/dist/ir/adapters/openai-responses.d.ts +33 -0
- package/backend/dist/ir/adapters/openai-responses.js +44 -2
- package/backend/dist/ir/capabilities.js +4 -0
- package/backend/dist/ir/validate.d.ts +24 -0
- package/backend/dist/ir/validate.js +113 -63
- package/backend/dist/opencode/catalog/auto-pull.js +8 -7
- package/backend/dist/opencode/catalog/ocr-store.d.ts +7 -6
- package/backend/dist/opencode/catalog/ocr-store.js +2 -2
- package/backend/dist/opencode/catalog/overrides-store.d.ts +5 -3
- package/backend/dist/opencode/catalog/overrides-store.js +22 -19
- package/backend/dist/opencode/catalog/repository.js +3 -3
- package/backend/dist/opencode/catalog/sources/models-dev.d.ts +3 -3
- package/backend/dist/opencode/catalog/sources/models-dev.js +29 -11
- package/backend/dist/opencode/catalog/types.d.ts +7 -9
- package/backend/dist/opencode/credentials/def-materialize.js +27 -21
- package/backend/dist/opencode/migrate/engine.d.ts +20 -0
- package/backend/dist/opencode/migrate/engine.js +71 -0
- package/backend/dist/opencode/migrate/migrator.d.ts +11 -0
- package/backend/dist/opencode/migrate/migrator.js +6 -0
- package/backend/dist/opencode/migrate/steps/overrides-v1-to-v2.d.ts +6 -0
- package/backend/dist/opencode/migrate/steps/overrides-v1-to-v2.js +31 -0
- package/backend/dist/opencode/migrate/steps/providers-v1-to-v2.d.ts +6 -0
- package/backend/dist/opencode/migrate/steps/providers-v1-to-v2.js +62 -0
- package/backend/dist/opencode/provider-def-adapters.d.ts +12 -11
- package/backend/dist/opencode/provider-def-adapters.js +43 -27
- package/backend/dist/opencode/provider-defs.d.ts +10 -16
- package/backend/dist/opencode/provider-defs.js +45 -45
- package/backend/dist/opencode/user-config.d.ts +23 -26
- package/backend/dist/opencode/user-config.js +95 -77
- package/backend/dist/pipeline/orchestrator.js +17 -7
- package/backend/dist/plugins/builtin/model-gateway.js +38 -12
- package/backend/dist/plugins/host.d.ts +2 -2
- package/backend/dist/plugins/host.js +15 -12
- package/backend/dist/providers/boot-direct.js +32 -8
- package/backend/dist/providers/dial-wire.d.ts +17 -0
- package/backend/dist/providers/dial-wire.js +49 -0
- package/backend/dist/providers/protocols-field.d.ts +24 -0
- package/backend/dist/providers/protocols-field.js +66 -0
- package/backend/dist/providers/registry.d.ts +3 -1
- package/backend/dist/providers/registry.js +23 -7
- package/backend/dist/quota/cli-login.js +3 -1
- package/backend/dist/routes/console.js +120 -95
- package/backend/dist/server.js +99 -2
- package/backend/dist/surfaces/anthropic.js +4 -1
- package/backend/dist/surfaces/body-admission.d.ts +279 -0
- package/backend/dist/surfaces/body-admission.js +613 -0
- package/backend/dist/surfaces/transport-node-http.d.ts +44 -5
- package/backend/dist/surfaces/transport-node-http.js +86 -13
- package/backend/dist/utils/server-liveness.d.ts +27 -0
- package/backend/dist/utils/server-liveness.js +31 -0
- package/frontend/dist/assets/{index-Cn3QMOQx.js → index-Dk76uc7e.js} +398 -363
- package/frontend/dist/assets/{index-DxZ9MHKB.css → index-KI1OW5x6.css} +1 -1
- package/frontend/dist/assets/workbuddy.color-DOMpQkxJ.svg +29 -0
- package/frontend/dist/index.html +2 -2
- package/package.json +1 -1
|
@@ -1,39 +1,118 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import os from 'node:os';
|
|
3
3
|
import fs from 'node:fs';
|
|
4
|
-
import {
|
|
4
|
+
import { spawnSync } from 'node:child_process';
|
|
5
|
+
import TOML from '@iarna/toml';
|
|
6
|
+
import { createBackup, describeBackup, restoreBackup, defaultGatewayPort, } from './base.js';
|
|
7
|
+
import { renameWithRetry } from '../../utils/atomic-write.js';
|
|
8
|
+
import { VIRTUAL_AUTO_IDS } from '../types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Default loader: `codex debug models` in a throwaway CODEX_HOME with only
|
|
11
|
+
* the built-in openai provider. Failures surface as an empty list so setup
|
|
12
|
+
* still writes the OCR block (stock is best-effort).
|
|
13
|
+
*/
|
|
14
|
+
export function loadCodexStockCatalog() {
|
|
15
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ocr-codex-stock-'));
|
|
16
|
+
try {
|
|
17
|
+
fs.writeFileSync(path.join(tmp, 'config.toml'), ['model = "gpt-5.4"', 'model_provider = "openai"', ''].join('\n'));
|
|
18
|
+
const res = spawnSync('codex', ['debug', 'models'], {
|
|
19
|
+
encoding: 'utf8',
|
|
20
|
+
env: { ...process.env, CODEX_HOME: tmp },
|
|
21
|
+
maxBuffer: 20 * 1024 * 1024,
|
|
22
|
+
windowsHide: true,
|
|
23
|
+
});
|
|
24
|
+
if (res.status !== 0 || !res.stdout)
|
|
25
|
+
return [];
|
|
26
|
+
const parsed = JSON.parse(res.stdout);
|
|
27
|
+
return Array.isArray(parsed.models)
|
|
28
|
+
? parsed.models.filter((m) => typeof m?.slug === 'string' && m.slug)
|
|
29
|
+
: [];
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
try {
|
|
36
|
+
fs.rmSync(tmp, { recursive: true, force: true });
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
/* ignore */
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Hook target: OpenAI Codex CLI and ChatGPT Desktop — both read
|
|
45
|
+
* `~/.codex/config.toml`. One write shape for both:
|
|
46
|
+
*
|
|
47
|
+
* - keep built-in `model_provider = "openai"`
|
|
48
|
+
* - point `openai_base_url` at the gateway
|
|
49
|
+
* - pin `model_catalog_json` = OCR selection first, then remaining stock
|
|
50
|
+
* OpenAI entries (Desktop’s allowlist still shows stock slugs; CLI `/model`
|
|
51
|
+
* shows the full merged list)
|
|
52
|
+
* - gateway key in `auth.json` (`OPENAI_API_KEY`)
|
|
53
|
+
*
|
|
54
|
+
* Legacy `omnicell-router` provider marks are stripped on setup/teardown so a
|
|
55
|
+
* previous CLI-only hook does not linger. TOML has no comment-preserving
|
|
56
|
+
* rewrite; the pre-write backup is the safety net.
|
|
57
|
+
*/
|
|
5
58
|
export class CodexClientAdapter {
|
|
6
59
|
name = 'codex';
|
|
7
60
|
hookKind = 'config';
|
|
8
|
-
|
|
61
|
+
/** One hook covers Codex CLI and ChatGPT Desktop (shared ~/.codex). */
|
|
62
|
+
displayName = 'Codex / ChatGPT';
|
|
63
|
+
shortName = 'Codex / ChatGPT';
|
|
64
|
+
/** Retired custom-provider id — still stripped for upgrade cleanliness. */
|
|
65
|
+
static LEGACY_PROVIDER_ID = 'omnicell-router';
|
|
66
|
+
static PROFILE_PREFIX = 'ocr-';
|
|
67
|
+
static CATALOG_FILE = 'omnicell-router-models.json';
|
|
68
|
+
/** Previous Desktop-only catalog name — cleared on setup/teardown. */
|
|
69
|
+
static LEGACY_DESKTOP_CATALOG = 'chatgpt-ocr-models.json';
|
|
70
|
+
static AUTH_MODE_API = 'apikey';
|
|
71
|
+
static CATALOG_INSTRUCTIONS = 'You are a coding agent running in the Codex CLI, a terminal-based coding assistant. You help the user with software engineering tasks.';
|
|
72
|
+
/** Override in tests to supply a fixture stock catalog. */
|
|
73
|
+
stockCatalogLoader = loadCodexStockCatalog;
|
|
9
74
|
getConfigPath() {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return candidateCodex;
|
|
15
|
-
if (fs.existsSync(candidateOpenAi))
|
|
16
|
-
return candidateOpenAi;
|
|
17
|
-
return candidateCodex;
|
|
75
|
+
return path.join(os.homedir(), '.codex', 'config.toml');
|
|
76
|
+
}
|
|
77
|
+
authPath() {
|
|
78
|
+
return path.join(path.dirname(this.getConfigPath()), 'auth.json');
|
|
18
79
|
}
|
|
19
80
|
async getStatus() {
|
|
20
81
|
const configPath = this.getConfigPath();
|
|
82
|
+
const configDir = path.dirname(configPath);
|
|
21
83
|
const exists = fs.existsSync(configPath);
|
|
22
84
|
const backupExists = fs.existsSync(`${configPath}.bak.ocr`);
|
|
23
85
|
let hooked = false;
|
|
24
|
-
let details = 'Direct OpenAI
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
86
|
+
let details = 'Direct OpenAI / ChatGPT connection';
|
|
87
|
+
let mainValue;
|
|
88
|
+
let extras;
|
|
89
|
+
if (exists) {
|
|
90
|
+
const data = this.readToml(configPath);
|
|
91
|
+
if (data) {
|
|
92
|
+
const gwPort = defaultGatewayPort();
|
|
93
|
+
if (this.detectHook(data, configDir, gwPort)) {
|
|
94
|
+
hooked = true;
|
|
95
|
+
details = `Routed to OmniCellRouter gateway (:${gwPort}/v1 via openai_base_url)`;
|
|
96
|
+
mainValue = typeof data.model === 'string' ? data.model : undefined;
|
|
97
|
+
// Extras echo from managed profiles ONLY. The merged catalog also
|
|
98
|
+
// carries remaining stock OpenAI rows (Desktop allowlist / CLI
|
|
99
|
+
// `/model`), and reading those as extras made the Clients "自定义"
|
|
100
|
+
// chip wall claim the operator had selected every gpt-* slug.
|
|
101
|
+
// Profiles are rewritten on setup as [...virtuals, ...extras];
|
|
102
|
+
// virtual ids are stripped below so the console sees the real pick.
|
|
103
|
+
extras = [
|
|
104
|
+
...new Set(this.listManagedProfiles(configDir)
|
|
105
|
+
.map((p) => p.model)
|
|
106
|
+
.filter((id) => id && id !== mainValue && !VIRTUAL_AUTO_IDS.has(id))),
|
|
107
|
+
];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
details = 'Config exists but could not be parsed as TOML — fix it manually, then re-apply';
|
|
33
112
|
}
|
|
34
113
|
}
|
|
35
114
|
else {
|
|
36
|
-
details = 'No Codex
|
|
115
|
+
details = 'No Codex config file detected';
|
|
37
116
|
}
|
|
38
117
|
return {
|
|
39
118
|
name: this.name,
|
|
@@ -43,67 +122,401 @@ export class CodexClientAdapter {
|
|
|
43
122
|
hooked,
|
|
44
123
|
backupExists,
|
|
45
124
|
details,
|
|
46
|
-
modelSlots: [{ key: 'main', value: hooked
|
|
125
|
+
modelSlots: [{ key: 'main', value: hooked ? mainValue : undefined, default: 'auto' }],
|
|
126
|
+
extraModels: extras,
|
|
47
127
|
};
|
|
48
128
|
}
|
|
49
129
|
async setup(options) {
|
|
50
130
|
const configPath = this.getConfigPath();
|
|
131
|
+
const configDir = path.dirname(configPath);
|
|
51
132
|
const port = options?.port || defaultGatewayPort();
|
|
52
133
|
const targetUrl = `http://127.0.0.1:${port}/v1`;
|
|
53
|
-
|
|
134
|
+
const apiKey = options?.apiKey?.trim();
|
|
135
|
+
if (!apiKey) {
|
|
136
|
+
return {
|
|
137
|
+
success: false,
|
|
138
|
+
message: 'Codex takeover requires a gateway API key (written into auth.json as OPENAI_API_KEY for the OCR hop)',
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
let data = {};
|
|
54
142
|
if (fs.existsSync(configPath)) {
|
|
143
|
+
const parsed = this.readToml(configPath);
|
|
144
|
+
if (parsed === null) {
|
|
145
|
+
return { success: false, message: `Could not parse TOML at ${configPath}; refusing to overwrite` };
|
|
146
|
+
}
|
|
147
|
+
data = parsed;
|
|
55
148
|
createBackup(configPath);
|
|
56
149
|
}
|
|
57
|
-
|
|
58
|
-
|
|
150
|
+
const main = options?.models?.main?.trim() || 'auto';
|
|
151
|
+
const extras = (options?.extraModels || [])
|
|
152
|
+
.map((id) => (id || '').trim())
|
|
153
|
+
.filter((id) => id && id !== main && !VIRTUAL_AUTO_IDS.has(id));
|
|
154
|
+
const virtuals = Array.isArray(options?.virtualModels)
|
|
155
|
+
? [...VIRTUAL_AUTO_IDS].filter((id) => options.virtualModels.includes(id) && id !== main)
|
|
156
|
+
: [...VIRTUAL_AUTO_IDS].filter((id) => id !== main);
|
|
157
|
+
this.clearLegacyProvider(data, configDir);
|
|
158
|
+
if (typeof data.openai_base_url === 'string' && data.openai_base_url && !data._ocr_previous_openai_base_url) {
|
|
159
|
+
data._ocr_previous_openai_base_url = data.openai_base_url;
|
|
59
160
|
}
|
|
60
|
-
data.
|
|
61
|
-
data.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
161
|
+
data.openai_base_url = targetUrl;
|
|
162
|
+
data.model_provider = 'openai';
|
|
163
|
+
data.model = main;
|
|
164
|
+
delete data.profile;
|
|
165
|
+
if (data.profiles)
|
|
166
|
+
delete data.profiles;
|
|
167
|
+
const ocrIds = [main, ...virtuals, ...extras];
|
|
168
|
+
const { path: catalogPath, count: catalogCount } = this.writeMergedCatalog(configDir, ocrIds);
|
|
169
|
+
data.model_catalog_json = catalogPath.replace(/\\/g, '/');
|
|
170
|
+
const authNote = this.writeAuthApiKey(apiKey);
|
|
171
|
+
this.writeToml(configPath, data);
|
|
172
|
+
this.rewriteManagedProfiles(configDir, [...virtuals, ...extras]);
|
|
71
173
|
return {
|
|
72
174
|
success: true,
|
|
73
|
-
message: `Codex configured to route via OCR (${targetUrl}, model: ${
|
|
175
|
+
message: `Codex configured to route via OCR (${targetUrl}, openai_base_url, ${catalogCount} picker entr${catalogCount === 1 ? 'y' : 'ies'} = OCR + stock OpenAI, model: ${main}). Fully quit and relaunch Codex CLI / ChatGPT Desktop so the picker reloads.${describeBackup(configPath)}${authNote}`,
|
|
74
176
|
};
|
|
75
177
|
}
|
|
76
178
|
async teardown() {
|
|
77
179
|
const configPath = this.getConfigPath();
|
|
78
|
-
|
|
180
|
+
const configDir = path.dirname(configPath);
|
|
181
|
+
const authPath = this.authPath();
|
|
182
|
+
if (!fs.existsSync(configPath) &&
|
|
183
|
+
this.listManagedProfiles(configDir).length === 0 &&
|
|
184
|
+
!fs.existsSync(this.catalogPath(configDir)) &&
|
|
185
|
+
!fs.existsSync(path.join(configDir, CodexClientAdapter.LEGACY_DESKTOP_CATALOG)) &&
|
|
186
|
+
!fs.existsSync(`${authPath}.bak.ocr`)) {
|
|
79
187
|
return { success: false, message: `Configuration file not found at ${configPath}` };
|
|
80
188
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
189
|
+
const restored = fs.existsSync(configPath) ? restoreBackup(configPath) : false;
|
|
190
|
+
if (fs.existsSync(configPath)) {
|
|
191
|
+
const data = this.readToml(configPath);
|
|
192
|
+
if (data === null) {
|
|
193
|
+
return { success: false, message: `Could not parse Codex config at ${configPath}` };
|
|
194
|
+
}
|
|
195
|
+
this.strip(data, configDir);
|
|
196
|
+
this.writeToml(configPath, data);
|
|
197
|
+
}
|
|
198
|
+
this.clearManagedProfiles(configDir);
|
|
199
|
+
this.clearCatalog(configDir);
|
|
200
|
+
this.clearLegacyDesktopCatalog(configDir);
|
|
201
|
+
const authRestored = this.restoreAuthBackup();
|
|
202
|
+
return {
|
|
203
|
+
success: true,
|
|
204
|
+
message: restored
|
|
205
|
+
? `Codex configuration restored from backup (${configPath}.bak.ocr)${authRestored ? '; auth.json restored' : ''}`
|
|
206
|
+
: `Codex configuration decoupled from OCR.${authRestored ? ' auth.json restored.' : ''}`,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
detectHook(data, configDir, gwPort) {
|
|
210
|
+
const base = typeof data.openai_base_url === 'string' ? data.openai_base_url : '';
|
|
211
|
+
const viaBase = base.includes(`127.0.0.1:${gwPort}`) &&
|
|
212
|
+
(data.model_provider === 'openai' || data.model_provider == null);
|
|
213
|
+
const catalogPinned = this.isOurCatalogPath(data.model_catalog_json, configDir);
|
|
214
|
+
// Legacy CLI hook (pre-unification) still counts as hooked until re-setup.
|
|
215
|
+
const legacy = this.detectLegacyProvider(data, gwPort);
|
|
216
|
+
return viaBase || catalogPinned || legacy;
|
|
217
|
+
}
|
|
218
|
+
detectLegacyProvider(data, gwPort) {
|
|
219
|
+
const provider = data.model_providers?.[CodexClientAdapter.LEGACY_PROVIDER_ID];
|
|
220
|
+
const providerRouted = typeof provider?.base_url === 'string' && provider.base_url.includes(`127.0.0.1:${gwPort}`);
|
|
221
|
+
return providerRouted || data.model_provider === CodexClientAdapter.LEGACY_PROVIDER_ID;
|
|
222
|
+
}
|
|
223
|
+
strip(data, configDir) {
|
|
224
|
+
this.clearLegacyProviderKeys(data);
|
|
225
|
+
const previous = data._ocr_previous_openai_base_url;
|
|
226
|
+
if (typeof previous === 'string' && previous) {
|
|
227
|
+
data.openai_base_url = previous;
|
|
86
228
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return { success: false, message: `Could not parse Codex config at ${configPath}` };
|
|
229
|
+
else if (typeof data.openai_base_url === 'string' && data.openai_base_url.includes('127.0.0.1:')) {
|
|
230
|
+
delete data.openai_base_url;
|
|
90
231
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
delete data._ocr_previous_base_url;
|
|
232
|
+
delete data._ocr_previous_openai_base_url;
|
|
233
|
+
if (this.isOurCatalogPath(data.model_catalog_json, configDir)) {
|
|
234
|
+
delete data.model_catalog_json;
|
|
95
235
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
236
|
+
const legacyCat = path.join(configDir, CodexClientAdapter.LEGACY_DESKTOP_CATALOG);
|
|
237
|
+
if (typeof data.model_catalog_json === 'string') {
|
|
238
|
+
const norm = (p) => path.normalize(p.replace(/\//g, path.sep)).toLowerCase();
|
|
239
|
+
if (norm(data.model_catalog_json) === norm(legacyCat))
|
|
240
|
+
delete data.model_catalog_json;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
clearLegacyProvider(data, configDir) {
|
|
244
|
+
this.clearLegacyProviderKeys(data);
|
|
245
|
+
this.clearLegacyDesktopCatalog(configDir);
|
|
246
|
+
// Drop leftover helper profiles from the old Desktop-only prefix.
|
|
247
|
+
if (fs.existsSync(configDir)) {
|
|
248
|
+
let names;
|
|
249
|
+
try {
|
|
250
|
+
names = fs.readdirSync(configDir);
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
names = [];
|
|
254
|
+
}
|
|
255
|
+
for (const name of names) {
|
|
256
|
+
if (!name.startsWith('chatgpt-') || !name.endsWith('.config.toml'))
|
|
257
|
+
continue;
|
|
258
|
+
try {
|
|
259
|
+
fs.unlinkSync(path.join(configDir, name));
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
/* ignore */
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
clearLegacyProviderKeys(data) {
|
|
268
|
+
const providers = data.model_providers;
|
|
269
|
+
if (providers?.[CodexClientAdapter.LEGACY_PROVIDER_ID]) {
|
|
270
|
+
delete providers[CodexClientAdapter.LEGACY_PROVIDER_ID];
|
|
271
|
+
if (Object.keys(providers).length === 0)
|
|
272
|
+
delete data.model_providers;
|
|
273
|
+
}
|
|
274
|
+
if (data.model_provider === CodexClientAdapter.LEGACY_PROVIDER_ID) {
|
|
275
|
+
data.model_provider = 'openai';
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Picker catalog: OCR-selected ids first (cloned stock template when the id
|
|
280
|
+
* is official, else a synthetic row), then every remaining stock OpenAI
|
|
281
|
+
* entry so Desktop’s allowlist and CLI `/model` both stay populated.
|
|
282
|
+
*/
|
|
283
|
+
writeMergedCatalog(configDir, ocrIds) {
|
|
284
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
285
|
+
const filePath = this.catalogPath(configDir);
|
|
286
|
+
const stock = this.stockCatalogLoader();
|
|
287
|
+
const stockBySlug = new Map(stock.map((m) => [String(m.slug), m]));
|
|
288
|
+
const models = [];
|
|
289
|
+
const seen = new Set();
|
|
290
|
+
let priority = 0;
|
|
291
|
+
for (const slug of ocrIds) {
|
|
292
|
+
if (!slug || seen.has(slug))
|
|
293
|
+
continue;
|
|
294
|
+
seen.add(slug);
|
|
295
|
+
const template = stockBySlug.get(slug);
|
|
296
|
+
if (template) {
|
|
297
|
+
models.push({
|
|
298
|
+
...template,
|
|
299
|
+
priority: priority++,
|
|
300
|
+
visibility: 'list',
|
|
301
|
+
hidden: false,
|
|
302
|
+
supported_in_api: true,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
models.push(this.syntheticCatalogEntry(slug, priority++));
|
|
307
|
+
}
|
|
99
308
|
}
|
|
100
|
-
|
|
101
|
-
|
|
309
|
+
for (const row of stock) {
|
|
310
|
+
const slug = String(row.slug);
|
|
311
|
+
if (!slug || seen.has(slug))
|
|
312
|
+
continue;
|
|
313
|
+
if (row.visibility != null && row.visibility !== 'list')
|
|
314
|
+
continue;
|
|
315
|
+
seen.add(slug);
|
|
316
|
+
models.push({
|
|
317
|
+
...row,
|
|
318
|
+
priority: priority++,
|
|
319
|
+
visibility: 'list',
|
|
320
|
+
hidden: false,
|
|
321
|
+
supported_in_api: true,
|
|
322
|
+
});
|
|
102
323
|
}
|
|
103
|
-
|
|
324
|
+
const tmp = `${filePath}.tmp.ocr`;
|
|
325
|
+
fs.writeFileSync(tmp, JSON.stringify({ models }, null, 2));
|
|
326
|
+
renameWithRetry(tmp, filePath);
|
|
327
|
+
this.clearLegacyDesktopCatalog(configDir);
|
|
328
|
+
return { path: filePath, count: models.length };
|
|
329
|
+
}
|
|
330
|
+
syntheticCatalogEntry(slug, priority) {
|
|
104
331
|
return {
|
|
105
|
-
|
|
106
|
-
|
|
332
|
+
slug,
|
|
333
|
+
display_name: slug,
|
|
334
|
+
description: `OmniCellRouter: ${slug}`,
|
|
335
|
+
supported_reasoning_levels: [
|
|
336
|
+
{ effort: 'low', description: 'Fast responses with lighter reasoning' },
|
|
337
|
+
{ effort: 'medium', description: 'Balances speed and reasoning depth' },
|
|
338
|
+
{ effort: 'high', description: 'Greater reasoning depth for complex problems' },
|
|
339
|
+
],
|
|
340
|
+
shell_type: 'shell_command',
|
|
341
|
+
visibility: 'list',
|
|
342
|
+
hidden: false,
|
|
343
|
+
supported_in_api: true,
|
|
344
|
+
priority,
|
|
345
|
+
availability_nux: null,
|
|
346
|
+
upgrade: null,
|
|
347
|
+
base_instructions: CodexClientAdapter.CATALOG_INSTRUCTIONS,
|
|
348
|
+
supports_reasoning_summaries: true,
|
|
349
|
+
support_verbosity: false,
|
|
350
|
+
default_verbosity: null,
|
|
351
|
+
apply_patch_tool_type: 'freeform',
|
|
352
|
+
truncation_policy: { mode: 'bytes', limit: 10_000 },
|
|
353
|
+
supports_parallel_tool_calls: true,
|
|
354
|
+
experimental_supported_tools: [],
|
|
355
|
+
context_window: 262_144,
|
|
356
|
+
max_context_window: 262_144,
|
|
357
|
+
input_modalities: ['text', 'image'],
|
|
107
358
|
};
|
|
108
359
|
}
|
|
360
|
+
writeAuthApiKey(apiKey) {
|
|
361
|
+
const authPath = this.authPath();
|
|
362
|
+
const backupPath = `${authPath}.bak.ocr`;
|
|
363
|
+
let data = {};
|
|
364
|
+
if (fs.existsSync(authPath)) {
|
|
365
|
+
try {
|
|
366
|
+
data = JSON.parse(fs.readFileSync(authPath, 'utf8'));
|
|
367
|
+
}
|
|
368
|
+
catch {
|
|
369
|
+
return ' (auth.json unreadable — gateway key not written; set OPENAI_API_KEY manually)';
|
|
370
|
+
}
|
|
371
|
+
if (!fs.existsSync(backupPath)) {
|
|
372
|
+
try {
|
|
373
|
+
fs.copyFileSync(authPath, backupPath);
|
|
374
|
+
}
|
|
375
|
+
catch {
|
|
376
|
+
/* ignore */
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
data.OPENAI_API_KEY = apiKey;
|
|
381
|
+
data.auth_mode = CodexClientAdapter.AUTH_MODE_API;
|
|
382
|
+
delete data.tokens;
|
|
383
|
+
try {
|
|
384
|
+
fs.mkdirSync(path.dirname(authPath), { recursive: true });
|
|
385
|
+
const tmp = `${authPath}.tmp.ocr`;
|
|
386
|
+
fs.writeFileSync(tmp, JSON.stringify(data, null, 2));
|
|
387
|
+
renameWithRetry(tmp, authPath);
|
|
388
|
+
return fs.existsSync(backupPath) ? ` auth.json updated (backup ${backupPath}).` : ' auth.json updated.';
|
|
389
|
+
}
|
|
390
|
+
catch (err) {
|
|
391
|
+
return ` (auth.json write failed: ${err instanceof Error ? err.message : String(err)})`;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
restoreAuthBackup() {
|
|
395
|
+
const authPath = this.authPath();
|
|
396
|
+
const backupPath = `${authPath}.bak.ocr`;
|
|
397
|
+
if (!fs.existsSync(backupPath))
|
|
398
|
+
return false;
|
|
399
|
+
try {
|
|
400
|
+
fs.copyFileSync(backupPath, authPath);
|
|
401
|
+
fs.unlinkSync(backupPath);
|
|
402
|
+
return true;
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
rewriteManagedProfiles(configDir, modelIds) {
|
|
409
|
+
this.clearManagedProfiles(configDir);
|
|
410
|
+
if (modelIds.length === 0)
|
|
411
|
+
return;
|
|
412
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
413
|
+
const used = new Set();
|
|
414
|
+
for (const modelId of modelIds) {
|
|
415
|
+
const stem = this.uniqueProfileStem(modelId, used);
|
|
416
|
+
used.add(stem);
|
|
417
|
+
const filePath = path.join(configDir, `${stem}.config.toml`);
|
|
418
|
+
const body = {
|
|
419
|
+
model: modelId,
|
|
420
|
+
model_provider: 'openai',
|
|
421
|
+
};
|
|
422
|
+
const tmp = `${filePath}.tmp.ocr`;
|
|
423
|
+
fs.writeFileSync(tmp, TOML.stringify(body));
|
|
424
|
+
renameWithRetry(tmp, filePath);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
clearManagedProfiles(configDir) {
|
|
428
|
+
for (const entry of this.listManagedProfiles(configDir)) {
|
|
429
|
+
try {
|
|
430
|
+
fs.unlinkSync(entry.filePath);
|
|
431
|
+
}
|
|
432
|
+
catch {
|
|
433
|
+
/* ignore */
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Profiles this adapter owns: `ocr-*.config.toml` whose body points at
|
|
439
|
+
* openai (unified) or the retired omnicell-router provider.
|
|
440
|
+
*/
|
|
441
|
+
listManagedProfiles(configDir) {
|
|
442
|
+
if (!fs.existsSync(configDir))
|
|
443
|
+
return [];
|
|
444
|
+
let names;
|
|
445
|
+
try {
|
|
446
|
+
names = fs.readdirSync(configDir);
|
|
447
|
+
}
|
|
448
|
+
catch {
|
|
449
|
+
return [];
|
|
450
|
+
}
|
|
451
|
+
const out = [];
|
|
452
|
+
for (const name of names) {
|
|
453
|
+
if (!name.startsWith(CodexClientAdapter.PROFILE_PREFIX) || !name.endsWith('.config.toml'))
|
|
454
|
+
continue;
|
|
455
|
+
const filePath = path.join(configDir, name);
|
|
456
|
+
const data = this.readToml(filePath);
|
|
457
|
+
if (!data)
|
|
458
|
+
continue;
|
|
459
|
+
if (data.model_provider !== 'openai' &&
|
|
460
|
+
data.model_provider !== CodexClientAdapter.LEGACY_PROVIDER_ID) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
if (typeof data.model !== 'string' || !data.model)
|
|
464
|
+
continue;
|
|
465
|
+
out.push({ filePath, model: data.model });
|
|
466
|
+
}
|
|
467
|
+
return out;
|
|
468
|
+
}
|
|
469
|
+
uniqueProfileStem(modelId, used) {
|
|
470
|
+
const safe = modelId.replace(/[^a-zA-Z0-9_-]+/g, '_').replace(/^_+|_+$/g, '') || 'model';
|
|
471
|
+
let stem = `${CodexClientAdapter.PROFILE_PREFIX}${safe}`;
|
|
472
|
+
if (!used.has(stem))
|
|
473
|
+
return stem;
|
|
474
|
+
let n = 2;
|
|
475
|
+
while (used.has(`${stem}-${n}`))
|
|
476
|
+
n += 1;
|
|
477
|
+
return `${stem}-${n}`;
|
|
478
|
+
}
|
|
479
|
+
clearCatalog(configDir) {
|
|
480
|
+
try {
|
|
481
|
+
const p = this.catalogPath(configDir);
|
|
482
|
+
if (fs.existsSync(p))
|
|
483
|
+
fs.unlinkSync(p);
|
|
484
|
+
}
|
|
485
|
+
catch {
|
|
486
|
+
/* ignore */
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
clearLegacyDesktopCatalog(configDir) {
|
|
490
|
+
try {
|
|
491
|
+
const p = path.join(configDir, CodexClientAdapter.LEGACY_DESKTOP_CATALOG);
|
|
492
|
+
if (fs.existsSync(p))
|
|
493
|
+
fs.unlinkSync(p);
|
|
494
|
+
}
|
|
495
|
+
catch {
|
|
496
|
+
/* ignore */
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
catalogPath(configDir) {
|
|
500
|
+
return path.join(configDir, CodexClientAdapter.CATALOG_FILE);
|
|
501
|
+
}
|
|
502
|
+
isOurCatalogPath(value, configDir) {
|
|
503
|
+
if (typeof value !== 'string' || !value)
|
|
504
|
+
return false;
|
|
505
|
+
const norm = (p) => path.normalize(p.replace(/\//g, path.sep)).toLowerCase();
|
|
506
|
+
return norm(value) === norm(this.catalogPath(configDir));
|
|
507
|
+
}
|
|
508
|
+
readToml(configPath) {
|
|
509
|
+
try {
|
|
510
|
+
return TOML.parse(fs.readFileSync(configPath, 'utf8'));
|
|
511
|
+
}
|
|
512
|
+
catch {
|
|
513
|
+
return null;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
writeToml(configPath, data) {
|
|
517
|
+
fs.mkdirSync(path.dirname(configPath), { recursive: true });
|
|
518
|
+
const tmp = `${configPath}.tmp.ocr`;
|
|
519
|
+
fs.writeFileSync(tmp, TOML.stringify(data));
|
|
520
|
+
renameWithRetry(tmp, configPath);
|
|
521
|
+
}
|
|
109
522
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ClientProfileMemoryEntry } from '../../config/types.js';
|
|
2
|
+
import type { ProviderRegistry } from '../../providers/registry.js';
|
|
3
|
+
export type FollowCatalogSyncReason = 'key' | 'pool';
|
|
4
|
+
export type FollowCatalogSyncRequest = {
|
|
5
|
+
reason: FollowCatalogSyncReason;
|
|
6
|
+
keyId?: string;
|
|
7
|
+
};
|
|
8
|
+
export type FollowCatalogSyncError = {
|
|
9
|
+
target: string;
|
|
10
|
+
reason: string;
|
|
11
|
+
};
|
|
12
|
+
export type FollowCatalogSyncRun = {
|
|
13
|
+
at: number;
|
|
14
|
+
reason: FollowCatalogSyncReason;
|
|
15
|
+
keyId?: string;
|
|
16
|
+
synced: number;
|
|
17
|
+
skipped: number;
|
|
18
|
+
failed: number;
|
|
19
|
+
errors: FollowCatalogSyncError[];
|
|
20
|
+
};
|
|
21
|
+
/** Per-target issue until a successful rewrite clears it (in-process; restart resets). */
|
|
22
|
+
export type FollowCatalogIssue = {
|
|
23
|
+
id: string;
|
|
24
|
+
kind: 'client' | 'gateway';
|
|
25
|
+
/** pending = waiting (usually ide_running); failed = hard error. */
|
|
26
|
+
status: 'pending' | 'failed';
|
|
27
|
+
reason: string;
|
|
28
|
+
apiKeyId?: string;
|
|
29
|
+
at: number;
|
|
30
|
+
};
|
|
31
|
+
export type FollowCatalogSyncState = {
|
|
32
|
+
enabled: boolean;
|
|
33
|
+
/** Ids still waiting (subset of issues with status pending). */
|
|
34
|
+
pending: string[];
|
|
35
|
+
/** Ids with a hard failure (subset of issues with status failed). */
|
|
36
|
+
failed: string[];
|
|
37
|
+
/** Full issue list for console cards / banner. */
|
|
38
|
+
issues: FollowCatalogIssue[];
|
|
39
|
+
lastRun?: FollowCatalogSyncRun;
|
|
40
|
+
};
|
|
41
|
+
export declare function setFollowCatalogSyncRegistry(registry: ProviderRegistry | undefined): void;
|
|
42
|
+
/** Resolve the kill switch: explicit false disables; absent → default ON. */
|
|
43
|
+
export declare function isFollowCatalogAutoResyncEnabled(): boolean;
|
|
44
|
+
export declare function getFollowCatalogSyncState(): FollowCatalogSyncState;
|
|
45
|
+
export declare function isFollowCatalogPending(clientOrPluginId: string): boolean;
|
|
46
|
+
/** Issue for a client name or its catalogPlugin id (card hint). */
|
|
47
|
+
export declare function getFollowCatalogIssue(clientOrPluginId: string): FollowCatalogIssue | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Schedule a sync pass. Key edits coalesce into a Set (multi-key → one pool
|
|
50
|
+
* pass so no earlier keyId is dropped); pool rebuilds share one 2s timer.
|
|
51
|
+
*/
|
|
52
|
+
export declare function requestFollowCatalogSync(req: FollowCatalogSyncRequest): void;
|
|
53
|
+
/** Retry every open issue (pending + failed). May pass acknowledgeRunning. */
|
|
54
|
+
export declare function retryFollowCatalogPending(opts?: {
|
|
55
|
+
acknowledgeRunning?: boolean;
|
|
56
|
+
}): Promise<FollowCatalogSyncRun>;
|
|
57
|
+
/** Persist modelMode / apiKeyId after a successful console setup (authoritative index). */
|
|
58
|
+
export declare function stampClientProfileMemory(clientName: string, patch: Partial<ClientProfileMemoryEntry>): void;
|
|
59
|
+
/** Drop follow index on teardown so unloaded clients are not auto-synced. */
|
|
60
|
+
export declare function clearClientProfileMemory(clientName: string): void;
|
|
61
|
+
/** Test helpers — reset in-process state between cases. */
|
|
62
|
+
export declare function _resetFollowCatalogSyncForTests(): void;
|