@minniexcode/codex-switch 0.0.9 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.AI.md +52 -13
  2. package/README.CN.md +94 -39
  3. package/README.md +75 -33
  4. package/dist/app/add-provider.js +29 -26
  5. package/dist/app/bridge.js +15 -15
  6. package/dist/app/edit-provider.js +2 -18
  7. package/dist/app/get-status.js +35 -13
  8. package/dist/app/import-providers.js +1 -1
  9. package/dist/app/init-codex.js +13 -14
  10. package/dist/app/list-providers.js +0 -1
  11. package/dist/app/remove-provider.js +1 -1
  12. package/dist/app/run-doctor.js +21 -39
  13. package/dist/app/run-mutation.js +3 -2
  14. package/dist/app/setup-codex.js +30 -18
  15. package/dist/app/show-config.js +1 -5
  16. package/dist/app/switch-provider.js +16 -33
  17. package/dist/cli/output.js +4 -6
  18. package/dist/cli.js +35 -3
  19. package/dist/commands/args.js +2 -2
  20. package/dist/commands/dispatch.js +40 -0
  21. package/dist/commands/handlers.js +202 -84
  22. package/dist/commands/help.js +2 -0
  23. package/dist/commands/registry.js +33 -12
  24. package/dist/domain/backups.js +4 -4
  25. package/dist/domain/config.js +102 -61
  26. package/dist/domain/providers.js +12 -5
  27. package/dist/domain/runtime-state.js +81 -4
  28. package/dist/domain/setup.js +58 -3
  29. package/dist/interaction/add-interactive.js +55 -1
  30. package/dist/interaction/interactive.js +1 -5
  31. package/dist/runtime/copilot-adapter.js +56 -13
  32. package/dist/runtime/copilot-bridge.js +392 -44
  33. package/dist/runtime/copilot-cli.js +142 -0
  34. package/dist/runtime/copilot-installer.js +59 -11
  35. package/dist/runtime/copilot-sdk-loader.js +5 -5
  36. package/dist/storage/auth-repo.js +28 -77
  37. package/dist/storage/backup-repo.js +4 -4
  38. package/dist/storage/codex-paths.js +34 -8
  39. package/dist/storage/config-repo.js +1 -36
  40. package/dist/storage/lock-repo.js +2 -4
  41. package/dist/storage/runtime-state-repo.js +43 -10
  42. package/dist/storage/tool-config-repo.js +111 -0
  43. package/docs/Design/codex-switch-copilot-integration-design.md +517 -0
  44. package/docs/Design/codex-switch-v0.0.10-design.md +669 -0
  45. package/docs/Design/codex-switch-v0.0.11-design.md +824 -0
  46. package/docs/PRD/codex-switch-prd-v0.0.10.md +406 -0
  47. package/docs/PRD/codex-switch-prd-v0.0.11.md +577 -0
  48. package/docs/cli-usage.md +166 -271
  49. package/docs/codex-switch-product-overview.md +2 -2
  50. package/docs/codex-switch-technical-architecture.md +6 -5
  51. package/package.json +1 -1
@@ -10,8 +10,8 @@ const copilot_installer_1 = require("./copilot-installer");
10
10
  /**
11
11
  * Probes whether the optional Copilot SDK runtime is installed and loadable.
12
12
  */
13
- function probeCopilotSdkRuntime() {
14
- const status = (0, copilot_installer_1.probeCopilotSdkInstall)();
13
+ function probeCopilotSdkRuntime(runtimesDir) {
14
+ const status = (0, copilot_installer_1.probeCopilotSdkInstall)(runtimesDir);
15
15
  if (!status.installed) {
16
16
  return {
17
17
  ok: false,
@@ -37,18 +37,18 @@ function probeCopilotSdkRuntime() {
37
37
  /**
38
38
  * Loads the lazily installed Copilot SDK and returns the module.
39
39
  */
40
- async function requireCopilotSdk() {
41
- return (0, copilot_sdk_loader_1.loadCopilotSdk)();
40
+ async function requireCopilotSdk(runtimesDir) {
41
+ return (0, copilot_sdk_loader_1.loadCopilotSdk)(runtimesDir);
42
42
  }
43
43
  /**
44
44
  * Probes whether the lazily installed Copilot SDK can create a usable session.
45
45
  */
46
- async function readCopilotAuthState() {
47
- const runtime = probeCopilotSdkRuntime();
46
+ async function readCopilotAuthState(runtimesDir) {
47
+ const runtime = probeCopilotSdkRuntime(runtimesDir);
48
48
  if (!runtime.ok) {
49
49
  throw (0, errors_1.cliError)("COPILOT_SDK_MISSING", "The optional Copilot SDK runtime is not installed.", runtime.details);
50
50
  }
51
- const { client, session } = await createCopilotSession();
51
+ const { client, session } = await createCopilotSession(runtimesDir);
52
52
  await stopCopilotClient(client);
53
53
  return {
54
54
  ready: Boolean(session),
@@ -60,7 +60,7 @@ async function readCopilotAuthState() {
60
60
  * Executes a single chat-completions style request through the optional Copilot SDK when available.
61
61
  */
62
62
  async function sendCopilotChatCompletion(args) {
63
- const { client, session, sdk } = await createCopilotSession();
63
+ const { client, session, sdk } = await createCopilotSession(args.runtimesDir);
64
64
  try {
65
65
  const sendAndWait = resolveCallable(session, "sendAndWait") ?? resolveCallable(sdk, "sendAndWait");
66
66
  if (!sendAndWait) {
@@ -106,15 +106,15 @@ async function sendCopilotChatCompletion(args) {
106
106
  await stopCopilotClient(client);
107
107
  }
108
108
  }
109
- async function createCopilotSession() {
110
- const sdk = (await requireCopilotSdk());
109
+ async function createCopilotSession(runtimesDir) {
110
+ const sdk = (await requireCopilotSdk(runtimesDir));
111
111
  const client = createCopilotClient(sdk);
112
112
  const createSession = resolveCallable(client ? client : null, "createSession") ?? resolveCallable(sdk, "createSession");
113
113
  if (!createSession) {
114
114
  throw (0, errors_1.cliError)("COPILOT_SDK_UNSUPPORTED", "The installed Copilot SDK does not expose a supported createSession API.", {});
115
115
  }
116
116
  try {
117
- const session = (await Promise.resolve(createSession({})));
117
+ const session = (await Promise.resolve(createSession(createSessionOptions(sdk))));
118
118
  return {
119
119
  sdk,
120
120
  client,
@@ -122,11 +122,30 @@ async function createCopilotSession() {
122
122
  };
123
123
  }
124
124
  catch (error) {
125
+ if (classifyCopilotSessionError(error) === "unsupported") {
126
+ throw (0, errors_1.cliError)("COPILOT_SDK_UNSUPPORTED", "The installed Copilot SDK does not expose a compatible permission-handling session API.", {
127
+ cause: error instanceof Error ? error.message : String(error),
128
+ });
129
+ }
125
130
  throw (0, errors_1.cliError)("COPILOT_AUTH_REQUIRED", "Copilot authentication is required before the local bridge can be used.", {
126
131
  cause: error instanceof Error ? error.message : String(error),
127
132
  });
128
133
  }
129
134
  }
135
+ /**
136
+ * Builds the session options used consistently across auth probes and request execution.
137
+ */
138
+ function createSessionOptions(sdk) {
139
+ const approveAll = resolveApproveAll(sdk);
140
+ if (approveAll) {
141
+ return {
142
+ onPermissionRequest: (request) => approveAll(request),
143
+ };
144
+ }
145
+ return {
146
+ onPermissionRequest: () => true,
147
+ };
148
+ }
130
149
  function createCopilotClient(sdk) {
131
150
  const ClientCtor = resolveConstructor(sdk, "CopilotClient");
132
151
  if (!ClientCtor) {
@@ -146,17 +165,27 @@ async function stopCopilotClient(client) {
146
165
  await Promise.resolve(client.stop());
147
166
  }
148
167
  }
168
+ /**
169
+ * Distinguishes true auth failures from SDK API-shape mismatches.
170
+ */
171
+ function classifyCopilotSessionError(error) {
172
+ const message = error instanceof Error ? error.message : String(error);
173
+ if (/onPermissionRequest/i.test(message) || /permission/i.test(message)) {
174
+ return "unsupported";
175
+ }
176
+ return "auth";
177
+ }
149
178
  function resolveCallable(target, name) {
150
179
  if (!target) {
151
180
  return null;
152
181
  }
153
182
  const direct = target[name];
154
183
  if (typeof direct === "function") {
155
- return direct;
184
+ return direct.bind(target);
156
185
  }
157
186
  const nestedDefault = target.default;
158
187
  if (nestedDefault && typeof nestedDefault[name] === "function") {
159
- return nestedDefault[name];
188
+ return nestedDefault[name].bind(nestedDefault);
160
189
  }
161
190
  return null;
162
191
  }
@@ -171,3 +200,17 @@ function resolveConstructor(target, name) {
171
200
  }
172
201
  return null;
173
202
  }
203
+ /**
204
+ * Resolves the SDK-provided permission helper when available.
205
+ */
206
+ function resolveApproveAll(target) {
207
+ const direct = target.approveAll;
208
+ if (typeof direct === "function") {
209
+ return direct;
210
+ }
211
+ const nestedDefault = target.default;
212
+ if (nestedDefault && typeof nestedDefault.approveAll === "function") {
213
+ return nestedDefault.approveAll;
214
+ }
215
+ return null;
216
+ }
@@ -45,11 +45,13 @@ exports.stopCopilotBridge = stopCopilotBridge;
45
45
  const http = __importStar(require("node:http"));
46
46
  const net = __importStar(require("node:net"));
47
47
  const node_child_process_1 = require("node:child_process");
48
+ const fs = __importStar(require("node:fs"));
48
49
  const path = __importStar(require("node:path"));
49
50
  const providers_1 = require("../domain/providers");
50
51
  const errors_1 = require("../domain/errors");
51
52
  const runtime_state_repo_1 = require("../storage/runtime-state-repo");
52
53
  let spawnImplementation = node_child_process_1.spawn;
54
+ let cachedBridgeWorkerBuildId = null;
53
55
  /**
54
56
  * Overrides the spawn implementation for bridge runtime tests.
55
57
  */
@@ -65,8 +67,8 @@ function resetCopilotBridgeSpawnImplementation() {
65
67
  /**
66
68
  * Returns the last known Copilot bridge runtime status.
67
69
  */
68
- async function probeCopilotBridgeRuntime(provider) {
69
- const state = (0, runtime_state_repo_1.readCopilotBridgeState)();
70
+ async function probeCopilotBridgeRuntime(provider, persistedState, runtimeDir) {
71
+ const state = persistedState === undefined ? (0, runtime_state_repo_1.readCopilotBridgeState)(runtimeDir) : persistedState;
70
72
  if (state && (!provider || !(0, providers_1.isCopilotBridgeProvider)(provider))) {
71
73
  return {
72
74
  ok: false,
@@ -126,7 +128,7 @@ async function probeCopilotBridgeRuntime(provider) {
126
128
  (0, runtime_state_repo_1.writeCopilotBridgeState)({
127
129
  ...state,
128
130
  lastHealthcheckAt: new Date().toISOString(),
129
- });
131
+ }, runtimeDir);
130
132
  return {
131
133
  ok: true,
132
134
  runtime: "copilot-bridge",
@@ -136,13 +138,13 @@ async function probeCopilotBridgeRuntime(provider) {
136
138
  /**
137
139
  * Starts or reuses a Copilot bridge worker, then verifies its health before returning.
138
140
  */
139
- async function ensureCopilotBridge(providerName, provider) {
140
- return startOrReuseCopilotBridge(providerName, provider);
141
+ async function ensureCopilotBridge(providerName, provider, runtimeDir) {
142
+ return startOrReuseCopilotBridge(providerName, provider, runtimeDir);
141
143
  }
142
144
  /**
143
145
  * Starts or reuses a Copilot bridge worker and reports the chosen port.
144
146
  */
145
- async function startOrReuseCopilotBridge(providerName, provider) {
147
+ async function startOrReuseCopilotBridge(providerName, provider, runtimeDir) {
146
148
  if (!(0, providers_1.isCopilotBridgeProvider)(provider)) {
147
149
  throw (0, errors_1.cliError)("RUNTIME_PROVIDER_INVALID", "Provider is not backed by a Copilot bridge runtime.", {
148
150
  provider: providerName,
@@ -155,27 +157,40 @@ async function startOrReuseCopilotBridge(providerName, provider) {
155
157
  });
156
158
  }
157
159
  const expectedBaseUrl = (0, providers_1.buildCopilotBridgeBaseUrl)(runtime);
158
- const current = (0, runtime_state_repo_1.readCopilotBridgeState)();
160
+ const current = (0, runtime_state_repo_1.readCopilotBridgeState)(runtimeDir);
161
+ const workerBuildId = getCopilotBridgeWorkerBuildId();
159
162
  let replaced = false;
160
163
  if (current && current.provider === providerName && current.baseUrl === expectedBaseUrl) {
161
- const healthy = await healthcheckCopilotBridge(current.host, current.port);
162
- if (healthy.ok) {
163
- (0, runtime_state_repo_1.writeCopilotBridgeState)({
164
- ...current,
165
- lastHealthcheckAt: new Date().toISOString(),
166
- });
167
- return {
168
- baseUrl: expectedBaseUrl,
169
- host: current.host,
170
- port: current.port,
171
- reused: true,
172
- portChanged: false,
173
- replaced: false,
174
- };
164
+ if (current.workerBuildId === workerBuildId) {
165
+ const healthy = await healthcheckCopilotBridge(current.host, current.port);
166
+ if (healthy.ok) {
167
+ const compatible = await verifyCopilotBridgeAuthorization(current.host, current.port, provider.apiKey);
168
+ if (compatible.ok) {
169
+ (0, runtime_state_repo_1.writeCopilotBridgeState)({
170
+ ...current,
171
+ lastHealthcheckAt: new Date().toISOString(),
172
+ workerBuildId,
173
+ }, runtimeDir);
174
+ return {
175
+ baseUrl: expectedBaseUrl,
176
+ host: current.host,
177
+ port: current.port,
178
+ reused: true,
179
+ portChanged: false,
180
+ replaced: false,
181
+ };
182
+ }
183
+ }
184
+ stopCopilotBridge(runtimeDir);
185
+ replaced = true;
186
+ }
187
+ else {
188
+ stopCopilotBridge(runtimeDir);
189
+ replaced = true;
175
190
  }
176
191
  }
177
192
  if (current && current.provider !== providerName) {
178
- stopCopilotBridge();
193
+ stopCopilotBridge(runtimeDir);
179
194
  replaced = true;
180
195
  }
181
196
  const selectedPort = await selectBridgePort(runtime.bridgeHost, runtime.bridgePort);
@@ -208,7 +223,7 @@ async function startOrReuseCopilotBridge(providerName, provider) {
208
223
  const startedAt = new Date().toISOString();
209
224
  const healthy = await waitForCopilotBridgeStartup(child, runtime.bridgeHost, selectedPort, 15, 200);
210
225
  if (!healthy.ok) {
211
- (0, runtime_state_repo_1.clearCopilotBridgeState)();
226
+ (0, runtime_state_repo_1.clearCopilotBridgeState)(runtimeDir);
212
227
  if (healthy.reason === "start-failed") {
213
228
  throw (0, errors_1.cliError)("BRIDGE_START_FAILED", "Copilot bridge worker exited before becoming healthy.", {
214
229
  provider: providerName,
@@ -232,8 +247,9 @@ async function startOrReuseCopilotBridge(providerName, provider) {
232
247
  baseUrl: selectedBaseUrl,
233
248
  startedAt,
234
249
  lastHealthcheckAt: new Date().toISOString(),
250
+ workerBuildId,
235
251
  };
236
- (0, runtime_state_repo_1.writeCopilotBridgeState)(state);
252
+ (0, runtime_state_repo_1.writeCopilotBridgeState)(state, runtimeDir);
237
253
  return {
238
254
  baseUrl: selectedBaseUrl,
239
255
  host: runtime.bridgeHost,
@@ -266,34 +282,329 @@ function createCopilotBridgeRequestHandler(context) {
266
282
  response.end(JSON.stringify({ object: "list", data: [] }));
267
283
  return;
268
284
  }
269
- if (method !== "POST" || url !== "/v1/chat/completions") {
270
- response.writeHead(404, { "content-type": "application/json" });
271
- response.end(JSON.stringify({ error: { message: "Not found" } }));
285
+ if (method === "POST" && url === "/v1/chat/completions") {
286
+ const body = await readJsonBody(request);
287
+ const stream = Boolean(body.stream);
288
+ const payload = await context.executeChatCompletion(body);
289
+ if (stream) {
290
+ response.writeHead(200, {
291
+ "content-type": "text/event-stream",
292
+ "cache-control": "no-cache",
293
+ connection: "keep-alive",
294
+ });
295
+ response.write(`data: ${JSON.stringify(payload)}\n\n`);
296
+ response.write("data: [DONE]\n\n");
297
+ response.end();
298
+ return;
299
+ }
300
+ response.writeHead(200, { "content-type": "application/json" });
301
+ response.end(JSON.stringify(payload));
272
302
  return;
273
303
  }
274
- const body = await readJsonBody(request);
275
- const stream = Boolean(body.stream);
276
- const payload = await context.executeChatCompletion(body);
277
- if (stream) {
278
- response.writeHead(200, {
279
- "content-type": "text/event-stream",
280
- "cache-control": "no-cache",
281
- connection: "keep-alive",
304
+ if (method === "POST" && url === "/v1/responses") {
305
+ const body = await readJsonBody(request);
306
+ const normalized = normalizeResponsesRequest(body);
307
+ const payload = await context.executeChatCompletion({
308
+ model: normalized.model,
309
+ messages: normalized.messages,
282
310
  });
283
- response.write(`data: ${JSON.stringify(payload)}\n\n`);
284
- response.write("data: [DONE]\n\n");
285
- response.end();
311
+ if (normalized.stream) {
312
+ response.writeHead(200, {
313
+ "content-type": "text/event-stream",
314
+ "cache-control": "no-cache",
315
+ connection: "keep-alive",
316
+ });
317
+ writeResponsesStream(response, payload);
318
+ response.end();
319
+ return;
320
+ }
321
+ response.writeHead(200, { "content-type": "application/json" });
322
+ response.end(JSON.stringify(buildResponsesPayload(payload)));
323
+ return;
324
+ }
325
+ if (method !== "POST") {
326
+ response.writeHead(404, { "content-type": "application/json" });
327
+ response.end(JSON.stringify({ error: { message: "Not found" } }));
286
328
  return;
287
329
  }
288
- response.writeHead(200, { "content-type": "application/json" });
289
- response.end(JSON.stringify(payload));
330
+ response.writeHead(404, { "content-type": "application/json" });
331
+ response.end(JSON.stringify({ error: { message: "Not found" } }));
290
332
  }
291
333
  catch (error) {
292
- response.writeHead(500, { "content-type": "application/json" });
334
+ const statusCode = isCliError(error) && error.code === "BRIDGE_UNSUPPORTED_REQUEST" ? 400 : 500;
335
+ response.writeHead(statusCode, { "content-type": "application/json" });
293
336
  response.end(JSON.stringify({ error: { message: error instanceof Error ? error.message : String(error) } }));
294
337
  }
295
338
  };
296
339
  }
340
+ /**
341
+ * Converts one minimal Responses API payload into the existing chat-completions bridge call shape.
342
+ */
343
+ function normalizeResponsesRequest(body) {
344
+ const payload = body;
345
+ if (typeof payload.model !== "string" || payload.model.trim() === "") {
346
+ throw (0, errors_1.cliError)("BRIDGE_UNSUPPORTED_REQUEST", "Copilot bridge /v1/responses requires a non-empty string model.");
347
+ }
348
+ const messages = normalizeResponsesInput(payload.input);
349
+ if (messages.length === 0) {
350
+ throw (0, errors_1.cliError)("BRIDGE_UNSUPPORTED_REQUEST", "Copilot bridge /v1/responses requires at least one input message.");
351
+ }
352
+ return {
353
+ model: payload.model,
354
+ messages,
355
+ stream: payload.stream === true,
356
+ };
357
+ }
358
+ function normalizeResponsesInput(input) {
359
+ if (typeof input === "string") {
360
+ return [{ role: "user", content: input }];
361
+ }
362
+ if (!Array.isArray(input)) {
363
+ throw (0, errors_1.cliError)("BRIDGE_UNSUPPORTED_REQUEST", "Copilot bridge /v1/responses expects input as a string or message array.");
364
+ }
365
+ if (input.length === 0) {
366
+ return [];
367
+ }
368
+ const entryKinds = input.map(classifyResponsesInputEntry);
369
+ const hasMessages = entryKinds.includes("message");
370
+ const hasContentItems = entryKinds.includes("content-item");
371
+ if (hasMessages && hasContentItems) {
372
+ throw (0, errors_1.cliError)("BRIDGE_UNSUPPORTED_REQUEST", "Copilot bridge /v1/responses input array must contain either message objects or content items, not both.");
373
+ }
374
+ if (hasContentItems) {
375
+ return [
376
+ {
377
+ role: "user",
378
+ content: extractResponsesTextContent(input, 0),
379
+ },
380
+ ];
381
+ }
382
+ return input.map((entry, index) => normalizeResponsesMessage(entry, index));
383
+ }
384
+ function normalizeResponsesMessage(entry, index) {
385
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
386
+ throw (0, errors_1.cliError)("BRIDGE_UNSUPPORTED_REQUEST", `Copilot bridge /v1/responses input[${String(index)}] must be an object message.`);
387
+ }
388
+ const message = entry;
389
+ const role = typeof message.role === "string" && message.role.trim() !== "" ? message.role : "user";
390
+ const content = extractResponsesTextContent(message.content, index);
391
+ return { role, content };
392
+ }
393
+ /**
394
+ * Classifies one top-level Responses input entry so mixed array shapes can be rejected clearly.
395
+ */
396
+ function classifyResponsesInputEntry(entry) {
397
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
398
+ throw (0, errors_1.cliError)("BRIDGE_UNSUPPORTED_REQUEST", "Copilot bridge /v1/responses input entries must be objects.");
399
+ }
400
+ const record = entry;
401
+ if ("content" in record || "role" in record) {
402
+ return "message";
403
+ }
404
+ if (typeof record.type === "string") {
405
+ return "content-item";
406
+ }
407
+ throw (0, errors_1.cliError)("BRIDGE_UNSUPPORTED_REQUEST", "Copilot bridge /v1/responses input entries must be message objects or typed content items.");
408
+ }
409
+ function extractResponsesTextContent(content, index) {
410
+ if (typeof content === "string") {
411
+ return content;
412
+ }
413
+ if (!Array.isArray(content)) {
414
+ throw (0, errors_1.cliError)("BRIDGE_UNSUPPORTED_REQUEST", `Copilot bridge /v1/responses input[${String(index)}].content must be a string or content item array.`);
415
+ }
416
+ const parts = content.map((item, itemIndex) => {
417
+ if (!item || typeof item !== "object" || Array.isArray(item)) {
418
+ throw (0, errors_1.cliError)("BRIDGE_UNSUPPORTED_REQUEST", `Copilot bridge /v1/responses input[${String(index)}].content[${String(itemIndex)}] must be an object item.`);
419
+ }
420
+ return renderResponsesContentItem(item);
421
+ });
422
+ return parts.join("\n");
423
+ }
424
+ /**
425
+ * Converts one Responses content item into the text-only prompt representation required by the Copilot SDK bridge.
426
+ */
427
+ function renderResponsesContentItem(item) {
428
+ const type = typeof item.type === "string" ? item.type : null;
429
+ const text = typeof item.text === "string" ? item.text : null;
430
+ if ((type === "input_text" || type === "text" || type === "output_text") && text !== null) {
431
+ return text;
432
+ }
433
+ if (type === "input_image") {
434
+ return buildResponsesPlaceholder("input_image", item.image_url, item.file_id);
435
+ }
436
+ if (type === "input_file") {
437
+ return buildResponsesPlaceholder("input_file", item.filename, item.file_id);
438
+ }
439
+ if (type !== null) {
440
+ return `[unsupported content type: ${type}]`;
441
+ }
442
+ throw (0, errors_1.cliError)("BRIDGE_UNSUPPORTED_REQUEST", "Copilot bridge /v1/responses content items must declare a string type.");
443
+ }
444
+ /**
445
+ * Builds a readable placeholder for non-text Responses content items preserved as text-only context.
446
+ */
447
+ function buildResponsesPlaceholder(type, ...candidates) {
448
+ for (const candidate of candidates) {
449
+ if (typeof candidate === "string" && candidate.trim() !== "") {
450
+ return `[${type}: ${candidate}]`;
451
+ }
452
+ }
453
+ return `[${type} omitted]`;
454
+ }
455
+ /**
456
+ * Converts the existing chat-completions response into a minimal Responses API payload.
457
+ */
458
+ function buildResponsesPayload(payload) {
459
+ const firstChoice = Array.isArray(payload.choices) ? payload.choices[0] : null;
460
+ const outputText = firstChoice?.message?.content ?? "";
461
+ return {
462
+ id: payload.id ?? `resp_${Date.now()}`,
463
+ object: "response",
464
+ created_at: payload.created ?? Math.floor(Date.now() / 1000),
465
+ model: payload.model ?? "copilot",
466
+ status: "completed",
467
+ output: [
468
+ {
469
+ type: "message",
470
+ id: `${payload.id ?? "resp"}_msg_0`,
471
+ role: "assistant",
472
+ content: [
473
+ {
474
+ type: "output_text",
475
+ text: outputText,
476
+ },
477
+ ],
478
+ },
479
+ ],
480
+ output_text: outputText,
481
+ };
482
+ }
483
+ /**
484
+ * Emits a minimal OpenAI-compatible Responses API event stream.
485
+ */
486
+ function writeResponsesStream(response, payload) {
487
+ const responsePayload = buildResponsesPayload(payload);
488
+ const responseId = typeof responsePayload.id === "string" ? responsePayload.id : `resp_${Date.now()}`;
489
+ const messageId = buildResponsesMessageId(responseId);
490
+ const outputText = typeof responsePayload.output_text === "string" ? responsePayload.output_text : "";
491
+ const inProgressResponse = {
492
+ ...responsePayload,
493
+ status: "in_progress",
494
+ output: [],
495
+ };
496
+ const completedMessage = {
497
+ id: messageId,
498
+ type: "message",
499
+ status: "completed",
500
+ role: "assistant",
501
+ content: [
502
+ {
503
+ type: "output_text",
504
+ text: outputText,
505
+ annotations: [],
506
+ },
507
+ ],
508
+ };
509
+ writeSseEvent(response, "response.created", {
510
+ type: "response.created",
511
+ response: inProgressResponse,
512
+ });
513
+ writeSseEvent(response, "response.in_progress", {
514
+ type: "response.in_progress",
515
+ response: inProgressResponse,
516
+ });
517
+ writeSseEvent(response, "response.output_item.added", {
518
+ type: "response.output_item.added",
519
+ output_index: 0,
520
+ item: {
521
+ id: messageId,
522
+ type: "message",
523
+ status: "in_progress",
524
+ role: "assistant",
525
+ content: [],
526
+ },
527
+ });
528
+ writeSseEvent(response, "response.content_part.added", {
529
+ type: "response.content_part.added",
530
+ item_id: messageId,
531
+ output_index: 0,
532
+ content_index: 0,
533
+ part: {
534
+ type: "output_text",
535
+ text: "",
536
+ annotations: [],
537
+ },
538
+ });
539
+ writeSseEvent(response, "response.output_text.delta", {
540
+ type: "response.output_text.delta",
541
+ item_id: messageId,
542
+ output_index: 0,
543
+ content_index: 0,
544
+ delta: outputText,
545
+ });
546
+ writeSseEvent(response, "response.output_text.done", {
547
+ type: "response.output_text.done",
548
+ item_id: messageId,
549
+ output_index: 0,
550
+ content_index: 0,
551
+ text: outputText,
552
+ });
553
+ writeSseEvent(response, "response.content_part.done", {
554
+ type: "response.content_part.done",
555
+ item_id: messageId,
556
+ output_index: 0,
557
+ content_index: 0,
558
+ part: {
559
+ type: "output_text",
560
+ text: outputText,
561
+ annotations: [],
562
+ },
563
+ });
564
+ writeSseEvent(response, "response.output_item.done", {
565
+ type: "response.output_item.done",
566
+ output_index: 0,
567
+ item: completedMessage,
568
+ });
569
+ writeSseEvent(response, "response.completed", {
570
+ type: "response.completed",
571
+ response: {
572
+ ...responsePayload,
573
+ output: [completedMessage],
574
+ },
575
+ });
576
+ }
577
+ /**
578
+ * Formats and writes one server-sent event frame.
579
+ */
580
+ function writeSseEvent(response, eventName, data) {
581
+ response.write(`event: ${eventName}\n`);
582
+ response.write(`data: ${JSON.stringify(data)}\n\n`);
583
+ }
584
+ /**
585
+ * Derives a stable message identifier for synthesized Responses output items.
586
+ */
587
+ function buildResponsesMessageId(responseId) {
588
+ if (responseId.startsWith("resp_")) {
589
+ return `msg_${responseId.slice("resp_".length)}_0`;
590
+ }
591
+ return `${responseId}_msg_0`;
592
+ }
593
+ function isCliError(error) {
594
+ return Boolean(error && typeof error === "object" && typeof error.code === "string");
595
+ }
596
+ /**
597
+ * Returns a stable build identifier for the compiled bridge worker bundle.
598
+ */
599
+ function getCopilotBridgeWorkerBuildId() {
600
+ if (cachedBridgeWorkerBuildId) {
601
+ return cachedBridgeWorkerBuildId;
602
+ }
603
+ const workerPath = path.join(__dirname, "copilot-bridge-worker.js");
604
+ const stats = fs.statSync(workerPath);
605
+ cachedBridgeWorkerBuildId = `${stats.size}:${stats.mtimeMs}`;
606
+ return cachedBridgeWorkerBuildId;
607
+ }
297
608
  /**
298
609
  * Starts an in-process local bridge server. Primarily used by the worker entrypoint and tests.
299
610
  */
@@ -329,8 +640,8 @@ async function waitForCopilotBridgeHealth(host, port, attempts = 10, delayMs = 1
329
640
  /**
330
641
  * Stops the currently persisted Copilot bridge worker when possible.
331
642
  */
332
- function stopCopilotBridge() {
333
- const state = (0, runtime_state_repo_1.readCopilotBridgeState)();
643
+ function stopCopilotBridge(runtimeDir) {
644
+ const state = (0, runtime_state_repo_1.readCopilotBridgeState)(runtimeDir);
334
645
  if (state?.pid) {
335
646
  try {
336
647
  process.kill(state.pid);
@@ -339,7 +650,7 @@ function stopCopilotBridge() {
339
650
  // Ignore best-effort bridge cleanup failures.
340
651
  }
341
652
  }
342
- (0, runtime_state_repo_1.clearCopilotBridgeState)();
653
+ (0, runtime_state_repo_1.clearCopilotBridgeState)(runtimeDir);
343
654
  }
344
655
  async function checkPortAvailability(host, port) {
345
656
  return new Promise((resolve) => {
@@ -457,6 +768,43 @@ async function healthcheckCopilotBridge(host, port) {
457
768
  request.end();
458
769
  });
459
770
  }
771
+ /**
772
+ * Checks whether a healthy bridge still accepts the provider's current bearer secret.
773
+ */
774
+ async function verifyCopilotBridgeAuthorization(host, port, apiKey) {
775
+ return new Promise((resolve) => {
776
+ const request = http.request({
777
+ host,
778
+ port,
779
+ method: "GET",
780
+ path: "/v1/models",
781
+ timeout: 1000,
782
+ headers: {
783
+ authorization: `Bearer ${apiKey}`,
784
+ },
785
+ }, (response) => {
786
+ response.resume();
787
+ if (response.statusCode === 200) {
788
+ resolve({ ok: true });
789
+ return;
790
+ }
791
+ resolve({
792
+ ok: false,
793
+ cause: `Authorization probe returned status ${String(response.statusCode ?? 0)}.`,
794
+ });
795
+ });
796
+ request.on("error", (error) => {
797
+ resolve({
798
+ ok: false,
799
+ cause: error.message,
800
+ });
801
+ });
802
+ request.on("timeout", () => {
803
+ request.destroy(new Error("Authorization probe timed out."));
804
+ });
805
+ request.end();
806
+ });
807
+ }
460
808
  async function readJsonBody(request) {
461
809
  const chunks = [];
462
810
  for await (const chunk of request) {