cordisx 0.1.0-beta.45 → 0.1.0-beta.47
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/assets/launcher/native-app-server-protocol.mjs +135 -44
- package/dist/assets/launcher/native-managed-resume.mjs +3 -2
- package/dist/assets/launcher/native-provider-continuation.mjs +109 -0
- package/dist/assets/launcher/native-provider-transport.mjs +2 -2
- package/dist/assets/launcher/native-thread-switch.mjs +112 -0
- package/dist/codex-adapter/adapter.mjs +141 -28
- package/dist/codex-adapter/index.js +1 -1
- package/dist/codex-adapter/native-provider-continuation-structure.d.ts +16 -0
- package/dist/codex-adapter/native-provider-continuation-structure.js +100 -0
- package/dist/codex-adapter/native-submission-structure.js +32 -6
- package/dist/native/native-helper-artifact.json +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/help.d.ts.map +1 -1
- package/dist/src/cli/help.js +2 -1
- package/dist/src/cli/help.js.map +1 -1
- package/dist/src/cli/parse.d.ts +1 -0
- package/dist/src/cli/parse.d.ts.map +1 -1
- package/dist/src/cli/parse.js +9 -1
- package/dist/src/cli/parse.js.map +1 -1
- package/dist/src/cli/production-managed-services.d.ts +1 -0
- package/dist/src/cli/production-managed-services.d.ts.map +1 -1
- package/dist/src/cli/production-managed-services.js +1 -0
- package/dist/src/cli/production-managed-services.js.map +1 -1
- package/dist/src/cli/production-plugin-management.d.ts +1 -0
- package/dist/src/cli/production-plugin-management.d.ts.map +1 -1
- package/dist/src/cli/production-plugin-management.js +5 -1
- package/dist/src/cli/production-plugin-management.js.map +1 -1
- package/dist/src/cli/run-command-dispatch.d.ts.map +1 -1
- package/dist/src/cli/run-command-dispatch.js +10 -1
- package/dist/src/cli/run-command-dispatch.js.map +1 -1
- package/dist/src/cli/run-command.d.ts +0 -1
- package/dist/src/cli/run-command.d.ts.map +1 -1
- package/dist/src/cli/run-command.js +28 -18
- package/dist/src/cli/run-command.js.map +1 -1
- package/dist/src/cli/service-owner.d.ts +4 -0
- package/dist/src/cli/service-owner.d.ts.map +1 -0
- package/dist/src/cli/service-owner.js +154 -0
- package/dist/src/cli/service-owner.js.map +1 -0
- package/dist/src/cli/start-mode.d.ts +4 -1
- package/dist/src/cli/start-mode.d.ts.map +1 -1
- package/dist/src/cli/start-mode.js +9 -6
- package/dist/src/cli/start-mode.js.map +1 -1
- package/dist/src/cli/supervisor-command.d.ts +1 -0
- package/dist/src/cli/supervisor-command.d.ts.map +1 -1
- package/dist/src/cli/supervisor-command.js +42 -11
- package/dist/src/cli/supervisor-command.js.map +1 -1
- package/dist/src/cli/supervisor-runtime.d.ts +1 -0
- package/dist/src/cli/supervisor-runtime.d.ts.map +1 -1
- package/dist/src/cli/supervisor-runtime.js +15 -1
- package/dist/src/cli/supervisor-runtime.js.map +1 -1
- package/dist/src/cli/supervisor-state.d.ts +2 -0
- package/dist/src/cli/supervisor-state.d.ts.map +1 -1
- package/dist/src/cli/supervisor-state.js +2 -0
- package/dist/src/cli/supervisor-state.js.map +1 -1
- package/dist/src/diagnostics/journal.d.ts.map +1 -1
- package/dist/src/diagnostics/journal.js +1 -0
- package/dist/src/diagnostics/journal.js.map +1 -1
- package/dist/src/launcher/agent-bridge.d.ts.map +1 -1
- package/dist/src/launcher/agent-bridge.js +7 -1
- package/dist/src/launcher/agent-bridge.js.map +1 -1
- package/dist/src/launcher/existing-model-provider-display.d.ts +5 -0
- package/dist/src/launcher/existing-model-provider-display.d.ts.map +1 -0
- package/dist/src/launcher/existing-model-provider-display.js +54 -0
- package/dist/src/launcher/existing-model-provider-display.js.map +1 -0
- package/dist/src/launcher/existing-terminal-provider-rpc.d.ts.map +1 -1
- package/dist/src/launcher/existing-terminal-provider-rpc.js +20 -1
- package/dist/src/launcher/existing-terminal-provider-rpc.js.map +1 -1
- package/dist/src/launcher/managed-service-candidate.d.ts +1 -0
- package/dist/src/launcher/managed-service-candidate.d.ts.map +1 -1
- package/dist/src/launcher/managed-service-candidate.js.map +1 -1
- package/dist/src/launcher/managed-service-node-host.js +1 -1
- package/dist/src/launcher/managed-service-node-host.js.map +1 -1
- package/dist/src/launcher/managed-service-process.d.ts +1 -1
- package/dist/src/launcher/managed-service-process.d.ts.map +1 -1
- package/dist/src/launcher/managed-service-process.js +12 -2
- package/dist/src/launcher/managed-service-process.js.map +1 -1
- package/dist/src/launcher/managed-service-runtime-process.d.ts.map +1 -1
- package/dist/src/launcher/managed-service-runtime-process.js +21 -2
- package/dist/src/launcher/managed-service-runtime-process.js.map +1 -1
- package/dist/src/launcher/managed-service-runtime.d.ts +2 -0
- package/dist/src/launcher/managed-service-runtime.d.ts.map +1 -1
- package/dist/src/launcher/managed-service-runtime.js +8 -2
- package/dist/src/launcher/managed-service-runtime.js.map +1 -1
- package/dist/src/launcher/managed-service-startup-diagnostic.d.ts +2 -1
- package/dist/src/launcher/managed-service-startup-diagnostic.d.ts.map +1 -1
- package/dist/src/launcher/managed-service-startup-diagnostic.js.map +1 -1
- package/dist/src/launcher/management-rpc.d.ts.map +1 -1
- package/dist/src/launcher/management-rpc.js +6 -0
- package/dist/src/launcher/management-rpc.js.map +1 -1
- package/dist/src/launcher/model-api-server.d.ts +34 -0
- package/dist/src/launcher/model-api-server.d.ts.map +1 -0
- package/dist/src/launcher/model-api-server.js +444 -0
- package/dist/src/launcher/model-api-server.js.map +1 -0
- package/dist/src/launcher/native-model-provider-catalog.d.ts +4 -0
- package/dist/src/launcher/native-model-provider-catalog.d.ts.map +1 -1
- package/dist/src/launcher/native-model-provider-catalog.js.map +1 -1
- package/dist/src/launcher/native-remote-provider-source.d.ts +3 -3
- package/dist/src/launcher/native-remote-provider-source.d.ts.map +1 -1
- package/dist/src/launcher/native-remote-provider-source.js +18 -4
- package/dist/src/launcher/native-remote-provider-source.js.map +1 -1
- package/dist/src/launcher/native-submission-cdp-channel.d.ts +2 -0
- package/dist/src/launcher/native-submission-cdp-channel.d.ts.map +1 -1
- package/dist/src/launcher/native-submission-cdp-channel.js +98 -6
- package/dist/src/launcher/native-submission-cdp-channel.js.map +1 -1
- package/dist/src/launcher/native-submission-composition.d.ts.map +1 -1
- package/dist/src/launcher/native-submission-composition.js +122 -1
- package/dist/src/launcher/native-submission-composition.js.map +1 -1
- package/dist/src/launcher/native-submission-continuation.d.ts +17 -0
- package/dist/src/launcher/native-submission-continuation.d.ts.map +1 -0
- package/dist/src/launcher/native-submission-continuation.js +56 -0
- package/dist/src/launcher/native-submission-continuation.js.map +1 -0
- package/dist/src/launcher/native-submission-control-server.d.ts.map +1 -1
- package/dist/src/launcher/native-submission-control-server.js +121 -6
- package/dist/src/launcher/native-submission-control-server.js.map +1 -1
- package/dist/src/launcher/native-submission-controller-contracts.d.ts +39 -3
- package/dist/src/launcher/native-submission-controller-contracts.d.ts.map +1 -1
- package/dist/src/launcher/native-submission-controller.d.ts.map +1 -1
- package/dist/src/launcher/native-submission-controller.js +79 -45
- package/dist/src/launcher/native-submission-controller.js.map +1 -1
- package/dist/src/launcher/native-submission-records.d.ts +50 -0
- package/dist/src/launcher/native-submission-records.d.ts.map +1 -0
- package/dist/src/launcher/native-submission-records.js +30 -0
- package/dist/src/launcher/native-submission-records.js.map +1 -0
- package/dist/src/launcher/native-thread-switch-error.d.ts +8 -0
- package/dist/src/launcher/native-thread-switch-error.d.ts.map +1 -0
- package/dist/src/launcher/native-thread-switch-error.js +12 -0
- package/dist/src/launcher/native-thread-switch-error.js.map +1 -0
- package/dist/src/launcher/plugin-lifecycle-model.d.ts +2 -0
- package/dist/src/launcher/plugin-lifecycle-model.d.ts.map +1 -1
- package/dist/src/launcher/plugin-lifecycle-model.js.map +1 -1
- package/dist/src/launcher/plugin-lifecycle-transaction.js +2 -2
- package/dist/src/launcher/plugin-lifecycle-transaction.js.map +1 -1
- package/dist/src/launcher/profile-runtime-owner.d.ts +3 -0
- package/dist/src/launcher/profile-runtime-owner.d.ts.map +1 -0
- package/dist/src/launcher/profile-runtime-owner.js +17 -0
- package/dist/src/launcher/profile-runtime-owner.js.map +1 -0
- package/dist/src/launcher/remote-terminal-provider-client.d.ts.map +1 -1
- package/dist/src/launcher/remote-terminal-provider-client.js +4 -0
- package/dist/src/launcher/remote-terminal-provider-client.js.map +1 -1
- package/dist/src/launcher/responses-encrypted-replay.js +1 -1
- package/dist/src/launcher/service-model-api.d.ts +25 -0
- package/dist/src/launcher/service-model-api.d.ts.map +1 -0
- package/dist/src/launcher/service-model-api.js +84 -0
- package/dist/src/launcher/service-model-api.js.map +1 -0
- package/dist/src/launcher/service-plugin-runtime.d.ts +5 -0
- package/dist/src/launcher/service-plugin-runtime.d.ts.map +1 -0
- package/dist/src/launcher/service-plugin-runtime.js +90 -0
- package/dist/src/launcher/service-plugin-runtime.js.map +1 -0
- package/dist/src/launcher/terminal-provider.d.ts +3 -0
- package/dist/src/launcher/terminal-provider.d.ts.map +1 -1
- package/dist/src/launcher/terminal-provider.js +17 -6
- package/dist/src/launcher/terminal-provider.js.map +1 -1
- package/dist/src/management/contracts.d.ts +2 -1
- package/dist/src/management/contracts.d.ts.map +1 -1
- package/dist/src/management/service.d.ts +1 -0
- package/dist/src/management/service.d.ts.map +1 -1
- package/dist/src/management/service.js +9 -4
- package/dist/src/management/service.js.map +1 -1
- package/dist/src/model-provider-display.d.ts +13 -0
- package/dist/src/model-provider-display.d.ts.map +1 -0
- package/dist/src/model-provider-display.js +36 -0
- package/dist/src/model-provider-display.js.map +1 -0
- package/dist/src/renderer/adapter/model-provider-display.d.ts +8 -0
- package/dist/src/renderer/adapter/model-provider-display.d.ts.map +1 -0
- package/dist/src/renderer/adapter/model-provider-display.js +46 -0
- package/dist/src/renderer/adapter/model-provider-display.js.map +1 -0
- package/dist/src/renderer/adapter/native-composer-creation.d.ts +23 -0
- package/dist/src/renderer/adapter/native-composer-creation.d.ts.map +1 -0
- package/dist/src/renderer/adapter/native-composer-creation.js +129 -0
- package/dist/src/renderer/adapter/native-composer-creation.js.map +1 -0
- package/dist/src/renderer/adapter/native-submission-action-normalizer.d.ts +1 -0
- package/dist/src/renderer/adapter/native-submission-action-normalizer.d.ts.map +1 -1
- package/dist/src/renderer/adapter/native-submission-action-normalizer.js +3 -0
- package/dist/src/renderer/adapter/native-submission-action-normalizer.js.map +1 -1
- package/dist/src/renderer/codex-router-history.d.ts +13 -15
- package/dist/src/renderer/codex-router-history.d.ts.map +1 -1
- package/dist/src/renderer/codex-router-history.js +56 -78
- package/dist/src/renderer/codex-router-history.js.map +1 -1
- package/dist/src/renderer/codex-router-navigator.d.ts +22 -0
- package/dist/src/renderer/codex-router-navigator.d.ts.map +1 -0
- package/dist/src/renderer/codex-router-navigator.js +82 -0
- package/dist/src/renderer/codex-router-navigator.js.map +1 -0
- package/dist/src/renderer/host-ui/AgentTaskDetailsNavigator.js +2 -2
- package/dist/src/renderer/host-ui/AgentTaskDetailsNavigator.js.map +1 -1
- package/dist/src/renderer/install-model-provider-selector.d.ts +1 -1
- package/dist/src/renderer/install-model-provider-selector.d.ts.map +1 -1
- package/dist/src/renderer/install-model-provider-selector.js +12 -4
- package/dist/src/renderer/install-model-provider-selector.js.map +1 -1
- package/dist/src/renderer/model-provider-copy.d.ts +4 -0
- package/dist/src/renderer/model-provider-copy.d.ts.map +1 -1
- package/dist/src/renderer/model-provider-copy.js +18 -0
- package/dist/src/renderer/model-provider-copy.js.map +1 -1
- package/dist/src/renderer/model-provider-preferences.d.ts +1 -0
- package/dist/src/renderer/model-provider-preferences.d.ts.map +1 -1
- package/dist/src/renderer/model-provider-preferences.js +4 -0
- package/dist/src/renderer/model-provider-preferences.js.map +1 -1
- package/dist/src/renderer/model-provider-selection.d.ts +41 -0
- package/dist/src/renderer/model-provider-selection.d.ts.map +1 -0
- package/dist/src/renderer/model-provider-selection.js +141 -0
- package/dist/src/renderer/model-provider-selection.js.map +1 -0
- package/dist/src/renderer/model-provider-selector.d.ts +5 -4
- package/dist/src/renderer/model-provider-selector.d.ts.map +1 -1
- package/dist/src/renderer/model-provider-selector.js +24 -102
- package/dist/src/renderer/model-provider-selector.js.map +1 -1
- package/dist/src/renderer/model-providers.d.ts +3 -0
- package/dist/src/renderer/model-providers.d.ts.map +1 -1
- package/dist/src/renderer/model-providers.js +10 -3
- package/dist/src/renderer/model-providers.js.map +1 -1
- package/dist/src/renderer/native-composer-creation-handoff.d.ts +25 -0
- package/dist/src/renderer/native-composer-creation-handoff.d.ts.map +1 -0
- package/dist/src/renderer/native-composer-creation-handoff.js +147 -0
- package/dist/src/renderer/native-composer-creation-handoff.js.map +1 -0
- package/dist/src/renderer/native-composer-refresh.d.ts +33 -0
- package/dist/src/renderer/native-composer-refresh.d.ts.map +1 -0
- package/dist/src/renderer/native-composer-refresh.js +105 -0
- package/dist/src/renderer/native-composer-refresh.js.map +1 -0
- package/dist/src/renderer/native-composer-request-observer.d.ts +34 -0
- package/dist/src/renderer/native-composer-request-observer.d.ts.map +1 -0
- package/dist/src/renderer/native-composer-request-observer.js +169 -0
- package/dist/src/renderer/native-composer-request-observer.js.map +1 -0
- package/dist/src/renderer/native-composer-submission-router.d.ts +7 -7
- package/dist/src/renderer/native-composer-submission-router.d.ts.map +1 -1
- package/dist/src/renderer/native-composer-submission-router.js +21 -1
- package/dist/src/renderer/native-composer-submission-router.js.map +1 -1
- package/dist/src/renderer/native-model-provider-transport.d.ts +4 -7
- package/dist/src/renderer/native-model-provider-transport.d.ts.map +1 -1
- package/dist/src/renderer/native-model-provider-transport.js +108 -235
- package/dist/src/renderer/native-model-provider-transport.js.map +1 -1
- package/dist/src/renderer/native-provider-continuation.d.ts +29 -0
- package/dist/src/renderer/native-provider-continuation.d.ts.map +1 -0
- package/dist/src/renderer/native-provider-continuation.js +64 -0
- package/dist/src/renderer/native-provider-continuation.js.map +1 -0
- package/dist/src/renderer/native-provider-selection-client.d.ts +16 -2
- package/dist/src/renderer/native-provider-selection-client.d.ts.map +1 -1
- package/dist/src/renderer/native-provider-selection-client.js +83 -8
- package/dist/src/renderer/native-provider-selection-client.js.map +1 -1
- package/dist/src/renderer/native-provider-submission-policy.d.ts +2 -0
- package/dist/src/renderer/native-provider-submission-policy.d.ts.map +1 -1
- package/dist/src/renderer/native-provider-submission-policy.js.map +1 -1
- package/dist/src/renderer/navigation-registry-base.js +7 -7
- package/dist/src/renderer/navigation-registry-base.js.map +1 -1
- package/dist/src/renderer/runtime-agent-composition.d.ts.map +1 -1
- package/dist/src/renderer/runtime-agent-composition.js +6 -1
- package/dist/src/renderer/runtime-agent-composition.js.map +1 -1
- package/package.json +1 -1
|
@@ -8,6 +8,8 @@ import { prepareManagedResume } from './native-managed-resume.mjs'
|
|
|
8
8
|
import { nativeInput } from './native-app-server-input.mjs'
|
|
9
9
|
import { createNativeProviderTransport } from './native-provider-transport.mjs'
|
|
10
10
|
import { nativeProviderFailure } from './native-provider-errors.mjs'
|
|
11
|
+
import { switchNativeThread } from './native-thread-switch.mjs'
|
|
12
|
+
import { createNativeProviderContinuation, dispatchNativeSelectionMutation } from './native-provider-continuation.mjs'
|
|
11
13
|
|
|
12
14
|
const socketPath = process.env.CORDISX_NATIVE_CONTROL_SOCKET
|
|
13
15
|
const nonce = process.env.CORDISX_NATIVE_CONTROL_NONCE
|
|
@@ -76,6 +78,8 @@ const preparedResumes = new Map()
|
|
|
76
78
|
const unmarkedTurns = new NativeRequestTracker()
|
|
77
79
|
// An unacknowledged lifecycle mutation stays uncertain; retain its fence, not its payload.
|
|
78
80
|
const lifecycleCalls = new Map()
|
|
81
|
+
const uncertainSwitches = new Map()
|
|
82
|
+
const switchReconciliations = new NativeRequestTracker()
|
|
79
83
|
// If the bounded map loses a mutation, its safety fence lasts until this process restarts.
|
|
80
84
|
// Ordinary native requests still pass through; only automatic recovery/Host switching stops.
|
|
81
85
|
let lifecycleUncertain = false
|
|
@@ -166,7 +170,9 @@ const nativeErrorCode = error =>
|
|
|
166
170
|
['ENOENT', 'EACCES', 'EPERM', 'EPIPE', 'ECONNRESET', 'ECONNREFUSED'].includes(error?.code) ? error.code : 'unknown'
|
|
167
171
|
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value) ? value : undefined
|
|
168
172
|
const validToken = value => typeof value === 'string' && /^[^\0\r\n]{16,256}$/u.test(value)
|
|
169
|
-
const lifecyclePending = threadId =>
|
|
173
|
+
const lifecyclePending = threadId =>
|
|
174
|
+
lifecycleUncertain || uncertainSwitches.has(threadId)
|
|
175
|
+
|| [...lifecycleCalls.values()].includes(threadId)
|
|
170
176
|
const write = (stream, value) =>
|
|
171
177
|
new Promise((resolve, reject) => {
|
|
172
178
|
if (stream.destroyed) {
|
|
@@ -210,6 +216,9 @@ function stop(reason = 'transport-closed') {
|
|
|
210
216
|
recoveringTurns.clear()
|
|
211
217
|
cancelledRecovery.clear()
|
|
212
218
|
lifecycleCalls.clear()
|
|
219
|
+
uncertainSwitches.clear()
|
|
220
|
+
switchReconciliations.clear()
|
|
221
|
+
switchRequests.clear()
|
|
213
222
|
firstTurns.clear()
|
|
214
223
|
agentFirstTurns.clear()
|
|
215
224
|
control.destroy()
|
|
@@ -240,6 +249,18 @@ function controlCall(type, input = {}) {
|
|
|
240
249
|
function nativeCall(method, params) {
|
|
241
250
|
return call(nativeTransport.stdin, privateCalls, { method, params })
|
|
242
251
|
}
|
|
252
|
+
function acknowledgedSwitchResume(message, expected) {
|
|
253
|
+
return expected && message.error === undefined && message.result?.thread?.id === expected.threadId
|
|
254
|
+
&& message.result.thread.status?.type === 'idle' && message.result.modelProvider === expected.providerId
|
|
255
|
+
&& message.result.model === expected.model
|
|
256
|
+
}
|
|
257
|
+
function reconcileSwitch(message, expected, hostAdmitted = false) {
|
|
258
|
+
if (
|
|
259
|
+
acknowledgedSwitchResume(message, expected) && uncertainSwitches.get(expected.threadId) === expected
|
|
260
|
+
&& (expected.providerId === 'openai' || hostAdmitted) && !lifecycleUncertain
|
|
261
|
+
&& ![...lifecycleCalls.values()].includes(expected.threadId)
|
|
262
|
+
) uncertainSwitches.delete(expected.threadId)
|
|
263
|
+
}
|
|
243
264
|
function settle(calls, message, isControl) {
|
|
244
265
|
const pending = calls.get(message.id)
|
|
245
266
|
if (!pending) return false
|
|
@@ -382,10 +403,24 @@ async function forward(line) {
|
|
|
382
403
|
const managed = config !== undefined && Object.hasOwn(config, marker)
|
|
383
404
|
if (message?.method === 'turn/interrupt' && typeof params?.threadId === 'string') {
|
|
384
405
|
unmarkedTurns.retireThread(params.threadId)
|
|
385
|
-
if (recoveringTurns.has(params.threadId)) cancelledRecovery.add(params.threadId)
|
|
406
|
+
if (recoveringTurns.has(params.threadId) || switching.has(params.threadId)) cancelledRecovery.add(params.threadId)
|
|
386
407
|
}
|
|
387
408
|
const mutatesThread = ['turn/start', 'turn/addUserMessage', 'turn/steer', 'thread/resume', 'thread/unsubscribe']
|
|
388
409
|
.includes(message?.method)
|
|
410
|
+
if (
|
|
411
|
+
uncertainSwitches.has(params?.threadId)
|
|
412
|
+
&& ['thread/resume', 'thread/unsubscribe', 'turn/interrupt'].includes(message?.method)
|
|
413
|
+
) uncertainSwitches.set(params.threadId, undefined)
|
|
414
|
+
if (
|
|
415
|
+
uncertainSwitches.has(params?.threadId)
|
|
416
|
+
&& ['turn/start', 'turn/addUserMessage', 'turn/steer'].includes(message?.method)
|
|
417
|
+
) {
|
|
418
|
+
await write(process.stdout, {
|
|
419
|
+
id: message.id,
|
|
420
|
+
error: { code: -32000, message: 'Provider switch status is uncertain. Reopen this conversation before sending.' },
|
|
421
|
+
})
|
|
422
|
+
return
|
|
423
|
+
}
|
|
389
424
|
if (
|
|
390
425
|
mutatesThread && (recoveringTurns.has(params?.threadId) || switching.has(params?.threadId))
|
|
391
426
|
) {
|
|
@@ -428,7 +463,37 @@ async function forward(line) {
|
|
|
428
463
|
if (
|
|
429
464
|
message?.method === 'thread/resume' && typeof params?.threadId === 'string'
|
|
430
465
|
&& ['string', 'number'].includes(typeof message.id)
|
|
431
|
-
)
|
|
466
|
+
) {
|
|
467
|
+
trackedResume = resumeCalls.track(message.id, params)
|
|
468
|
+
if (uncertainSwitches.has(params.threadId)) {
|
|
469
|
+
let expected
|
|
470
|
+
try {
|
|
471
|
+
const before = await nativeCall('thread/read', { threadId: params.threadId, includeTurns: false })
|
|
472
|
+
const thread = before?.thread
|
|
473
|
+
if (
|
|
474
|
+
thread?.id === params.threadId && typeof thread.modelProvider === 'string'
|
|
475
|
+
&& typeof thread.model === 'string'
|
|
476
|
+
&& ['idle', 'notLoaded', 'systemError'].includes(thread.status?.type)
|
|
477
|
+
&& params.path == null && params.history == null
|
|
478
|
+
&& (params.modelProvider == null || params.modelProvider === thread.modelProvider)
|
|
479
|
+
&& (params.config?.model_provider === undefined || params.config.model_provider === thread.modelProvider)
|
|
480
|
+
&& (params.model == null || params.model === thread.model)
|
|
481
|
+
) expected = { threadId: params.threadId, providerId: thread.modelProvider, model: thread.model }
|
|
482
|
+
} catch {}
|
|
483
|
+
if (!expected || !switchReconciliations.track(message.id, expected)) {
|
|
484
|
+
resumeCalls.take(message.id)
|
|
485
|
+
await write(process.stdout, {
|
|
486
|
+
id: message.id,
|
|
487
|
+
error: {
|
|
488
|
+
code: -32000,
|
|
489
|
+
message: 'Provider switch status is uncertain. Reopen the original conversation and service.',
|
|
490
|
+
},
|
|
491
|
+
})
|
|
492
|
+
return
|
|
493
|
+
}
|
|
494
|
+
uncertainSwitches.set(params.threadId, expected)
|
|
495
|
+
}
|
|
496
|
+
}
|
|
432
497
|
if (
|
|
433
498
|
message?.method === 'turn/start' && typeof params?.threadId === 'string'
|
|
434
499
|
&& ['string', 'number'].includes(typeof message.id)
|
|
@@ -450,7 +515,11 @@ async function forward(line) {
|
|
|
450
515
|
) {
|
|
451
516
|
let prepared
|
|
452
517
|
try {
|
|
453
|
-
prepared = await prepareManagedResume(message, {
|
|
518
|
+
prepared = await prepareManagedResume(message, {
|
|
519
|
+
nativeCall,
|
|
520
|
+
controlCall,
|
|
521
|
+
allowConfigured: uncertainSwitches.has(params.threadId),
|
|
522
|
+
})
|
|
454
523
|
} catch {}
|
|
455
524
|
if (prepared) {
|
|
456
525
|
preparedResumes.set(message.id, { threadId: prepared.threadId, resumeToken: prepared.resumeToken })
|
|
@@ -459,6 +528,21 @@ async function forward(line) {
|
|
|
459
528
|
return
|
|
460
529
|
}
|
|
461
530
|
}
|
|
531
|
+
const reconciliation = uncertainSwitches.get(params?.threadId)
|
|
532
|
+
if (message?.method === 'thread/resume' && reconciliation && reconciliation.providerId !== 'openai') {
|
|
533
|
+
resumeCalls.take(message.id)
|
|
534
|
+
switchReconciliations.take(message.id)
|
|
535
|
+
lifecycleCalls.delete(message.id)
|
|
536
|
+
uncertainSwitches.set(params.threadId, undefined)
|
|
537
|
+
await write(process.stdout, {
|
|
538
|
+
id: message.id,
|
|
539
|
+
error: {
|
|
540
|
+
code: -32000,
|
|
541
|
+
message: 'Provider switch status is uncertain. Original service preparation was not admitted.',
|
|
542
|
+
},
|
|
543
|
+
})
|
|
544
|
+
return
|
|
545
|
+
}
|
|
462
546
|
if (['thread/start', 'thread/resume'].includes(message?.method) && params) {
|
|
463
547
|
try {
|
|
464
548
|
let providerId = params.modelProvider
|
|
@@ -535,6 +619,7 @@ async function forward(line) {
|
|
|
535
619
|
firstTurns.delete(params.threadId)
|
|
536
620
|
agentFirstTurns.delete(params.threadId)
|
|
537
621
|
}
|
|
622
|
+
if (message.method === 'turn/start') continuation.dispatched(token, params.threadId)
|
|
538
623
|
await write(nativeTransport.stdin, JSON.stringify(message))
|
|
539
624
|
} catch {
|
|
540
625
|
desktopCalls.delete(message?.id)
|
|
@@ -724,6 +809,7 @@ readLines(nativeTransport.stdout, line => {
|
|
|
724
809
|
// Late private responses must never escape into the Desktop's request table.
|
|
725
810
|
if (typeof message?.id === 'string' && message.id.startsWith(privatePrefix)) return
|
|
726
811
|
if (message.method === undefined) lifecycleCalls.delete(message.id)
|
|
812
|
+
const reconciliation = message.method === undefined ? switchReconciliations.take(message.id) : undefined
|
|
727
813
|
const turnParams = message.method === undefined ? unmarkedTurns.take(message.id) : undefined
|
|
728
814
|
if (turnParams !== undefined) {
|
|
729
815
|
const providerId = nativeProviderFailure(message)
|
|
@@ -741,8 +827,14 @@ readLines(nativeTransport.stdout, line => {
|
|
|
741
827
|
preparedResumes.delete(message.id)
|
|
742
828
|
resumeCalls.take(message.id)
|
|
743
829
|
const succeeded = message.error === undefined && message.result?.thread?.id === preparedResume.threadId
|
|
830
|
+
&& (reconciliation === undefined
|
|
831
|
+
|| (acknowledgedSwitchResume(message, reconciliation)
|
|
832
|
+
&& uncertainSwitches.get(reconciliation.threadId) === reconciliation))
|
|
744
833
|
void controlCall('resume-complete', { ...preparedResume, succeeded })
|
|
745
|
-
.then(
|
|
834
|
+
.then(completed => {
|
|
835
|
+
if (completed === true) reconcileSwitch(message, reconciliation, true)
|
|
836
|
+
return write(process.stdout, line)
|
|
837
|
+
}).catch(stop)
|
|
746
838
|
return
|
|
747
839
|
}
|
|
748
840
|
const resumeParams = message.method === undefined ? resumeCalls.take(message?.id) : undefined
|
|
@@ -753,6 +845,7 @@ readLines(nativeTransport.stdout, line => {
|
|
|
753
845
|
return
|
|
754
846
|
}
|
|
755
847
|
}
|
|
848
|
+
reconcileSwitch(message, reconciliation)
|
|
756
849
|
const operation = desktopCalls.get(message?.id)
|
|
757
850
|
if (!operation || message.method !== undefined) {
|
|
758
851
|
void write(process.stdout, line).catch(stop)
|
|
@@ -780,6 +873,12 @@ readLines(nativeTransport.stdout, line => {
|
|
|
780
873
|
})
|
|
781
874
|
|
|
782
875
|
const switching = new Set()
|
|
876
|
+
const switchRequests = new Map()
|
|
877
|
+
const continuation = createNativeProviderContinuation({
|
|
878
|
+
nativeCall,
|
|
879
|
+
lifecyclePending,
|
|
880
|
+
current: threadId => !closed && !cancelledRecovery.has(threadId) && !lifecyclePending(threadId),
|
|
881
|
+
})
|
|
783
882
|
const BRIDGE_NATIVE_METHODS = new Set([
|
|
784
883
|
'thread/list',
|
|
785
884
|
'thread/read',
|
|
@@ -798,6 +897,14 @@ readLines(control, line => {
|
|
|
798
897
|
return
|
|
799
898
|
}
|
|
800
899
|
if (settle(controlCalls, message, true)) return
|
|
900
|
+
if (
|
|
901
|
+
message.type === 'switch-cancel' && typeof message.switchId === 'string'
|
|
902
|
+
&& switchRequests.get(message.switchId) === message.threadId
|
|
903
|
+
) {
|
|
904
|
+
cancelledRecovery.add(message.threadId)
|
|
905
|
+
uncertainSwitches.set(message.threadId, undefined)
|
|
906
|
+
return
|
|
907
|
+
}
|
|
801
908
|
if (message.type === 'task-create' && typeof message.id === 'string') {
|
|
802
909
|
void nativeCall('mcpServer/tool/call', {
|
|
803
910
|
server: 'codex_app',
|
|
@@ -821,6 +928,13 @@ readLines(control, line => {
|
|
|
821
928
|
void write(control, { id: message.id, ok: false }).catch(stop)
|
|
822
929
|
return
|
|
823
930
|
}
|
|
931
|
+
if (uncertainSwitches.has(params.threadId) && message.method === 'turn/start') {
|
|
932
|
+
void write(control, { id: message.id, ok: false, reason: 'provider-switch-reconciliation-required' }).catch(stop)
|
|
933
|
+
return
|
|
934
|
+
}
|
|
935
|
+
if (
|
|
936
|
+
uncertainSwitches.has(params.threadId) && ['thread/resume', 'turn/interrupt'].includes(message.method)
|
|
937
|
+
) uncertainSwitches.set(params.threadId, undefined)
|
|
824
938
|
void nativeCall(message.method, params)
|
|
825
939
|
.then(value => write(control, { id: message.id, ok: true, value }))
|
|
826
940
|
.catch(() => write(control, { id: message.id, ok: false }).catch(stop))
|
|
@@ -837,43 +951,20 @@ readLines(control, line => {
|
|
|
837
951
|
}).catch(() => write(control, { id: message.id, ok: false }).catch(stop))
|
|
838
952
|
return
|
|
839
953
|
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
// Ephemeral side chats have no rollout to resume. Unsubscribe would destroy
|
|
859
|
-
// the only live session even when the following resume fails.
|
|
860
|
-
const current = await nativeCall('thread/read', { threadId: message.threadId, includeTurns: false })
|
|
861
|
-
if (current?.thread?.ephemeral === true) throw new Error('Ephemeral provider switching requires a new side chat')
|
|
862
|
-
await nativeCall('thread/unsubscribe', { threadId: message.threadId })
|
|
863
|
-
const result = await nativeCall('thread/resume', {
|
|
864
|
-
threadId: message.threadId,
|
|
865
|
-
model: message.model,
|
|
866
|
-
modelProvider: message.providerId,
|
|
867
|
-
config: message.config,
|
|
868
|
-
excludeTurns: true,
|
|
869
|
-
})
|
|
870
|
-
const ok = result?.thread?.id === message.threadId && result?.modelProvider === message.providerId
|
|
871
|
-
&& result?.model === message.model
|
|
872
|
-
await write(control, { id: message.id, ok })
|
|
873
|
-
} catch {
|
|
874
|
-
await write(control, { id: message.id, ok: false })
|
|
875
|
-
} finally {
|
|
876
|
-
switching.delete(message.threadId)
|
|
877
|
-
}
|
|
878
|
-
})().catch(stop)
|
|
954
|
+
dispatchNativeSelectionMutation(message, {
|
|
955
|
+
switching,
|
|
956
|
+
switchRequests,
|
|
957
|
+
recoveringTurns,
|
|
958
|
+
lifecyclePending,
|
|
959
|
+
cancelledRecovery,
|
|
960
|
+
uncertainSwitches,
|
|
961
|
+
nativeCall,
|
|
962
|
+
controlCall,
|
|
963
|
+
switchNativeThread,
|
|
964
|
+
continuation,
|
|
965
|
+
write,
|
|
966
|
+
control,
|
|
967
|
+
closed: () => closed,
|
|
968
|
+
stop,
|
|
969
|
+
})
|
|
879
970
|
}, maxControlLineBytes)
|
|
@@ -2,7 +2,7 @@ const object = value => value !== null && typeof value === 'object' && !Array.is
|
|
|
2
2
|
const validToken = value => typeof value === 'string' && /^[^\0\r\n]{16,256}$/u.test(value)
|
|
3
3
|
|
|
4
4
|
/** Refresh recognized persisted managed routes without changing the selected provider or inventing a Send. */
|
|
5
|
-
export async function prepareManagedResume(message, { nativeCall, controlCall }) {
|
|
5
|
+
export async function prepareManagedResume(message, { nativeCall, controlCall, allowConfigured = false }) {
|
|
6
6
|
const params = message.params
|
|
7
7
|
// Native path/history overrides can select history other than the named thread.
|
|
8
8
|
if (params.path != null || params.history != null) return undefined
|
|
@@ -15,7 +15,8 @@ export async function prepareManagedResume(message, { nativeCall, controlCall })
|
|
|
15
15
|
|| (params.modelProvider != null && params.modelProvider !== thread.modelProvider)
|
|
16
16
|
|| (params.config?.model_provider !== undefined && params.config.model_provider !== thread.modelProvider)
|
|
17
17
|
) return undefined
|
|
18
|
-
|
|
18
|
+
// Only an exact persisted-pair reconciliation uses ordinary configured-provider admission.
|
|
19
|
+
const prepared = await controlCall(allowConfigured ? 'resume' : 'managed-resume', {
|
|
19
20
|
threadId: params.threadId,
|
|
20
21
|
providerId: thread.modelProvider,
|
|
21
22
|
model: typeof params.model === 'string' ? params.model : thread.model,
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
const valid = (value, max = 512) =>
|
|
2
|
+
typeof value === 'string' && value.length > 0 && value.length <= max && !/[\0\r\n]/u.test(value)
|
|
3
|
+
|
|
4
|
+
/** Owns only normal forks created for a Host-admitted Composer Send. */
|
|
5
|
+
export function createNativeProviderContinuation({ nativeCall, lifecyclePending, current }) {
|
|
6
|
+
const forks = new Map()
|
|
7
|
+
const discard = async (operationToken, threadId) => {
|
|
8
|
+
const fork = forks.get(operationToken)
|
|
9
|
+
if (!fork || fork.threadId !== threadId || fork.dispatched) return false
|
|
10
|
+
const read = await nativeCall('thread/read', { threadId, includeTurns: false })
|
|
11
|
+
if (
|
|
12
|
+
read?.thread?.id !== threadId || read.thread.modelProvider !== fork.providerId
|
|
13
|
+
|| !['idle', 'notLoaded'].includes(read.thread.status?.type) || lifecyclePending(threadId)
|
|
14
|
+
) return false
|
|
15
|
+
await nativeCall('thread/archive', { threadId })
|
|
16
|
+
forks.delete(operationToken)
|
|
17
|
+
return true
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
dispatched(operationToken, threadId) {
|
|
21
|
+
const fork = forks.get(operationToken)
|
|
22
|
+
if (fork?.threadId === threadId) {
|
|
23
|
+
fork.dispatched = true
|
|
24
|
+
forks.delete(operationToken)
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
async handle(message) {
|
|
28
|
+
if (!valid(message.operationToken, 256)) return { ok: false }
|
|
29
|
+
if (message.type === 'continuation-discard') {
|
|
30
|
+
return { ok: true, value: await discard(message.operationToken, message.threadId) }
|
|
31
|
+
}
|
|
32
|
+
if (!valid(message.threadId) || !valid(message.providerId, 128) || !valid(message.model)) {
|
|
33
|
+
return { ok: false }
|
|
34
|
+
}
|
|
35
|
+
if (forks.has(message.operationToken) || forks.size >= 32) return { ok: false }
|
|
36
|
+
const source = await nativeCall('thread/read', { threadId: message.threadId, includeTurns: false })
|
|
37
|
+
if (
|
|
38
|
+
!current(message.threadId) || source?.thread?.id !== message.threadId
|
|
39
|
+
|| !['idle', 'notLoaded', 'systemError'].includes(source.thread.status?.type)
|
|
40
|
+
) return { ok: false }
|
|
41
|
+
if (source.thread.ephemeral === true) return { ok: false, reason: 'provider-switch-unsupported' }
|
|
42
|
+
try {
|
|
43
|
+
const result = await nativeCall('thread/fork', {
|
|
44
|
+
threadId: message.threadId,
|
|
45
|
+
modelProvider: message.providerId,
|
|
46
|
+
model: message.model,
|
|
47
|
+
config: message.config,
|
|
48
|
+
ephemeral: false,
|
|
49
|
+
excludeTurns: true,
|
|
50
|
+
deferGoalContinuation: true,
|
|
51
|
+
})
|
|
52
|
+
const thread = result?.thread
|
|
53
|
+
if (
|
|
54
|
+
!valid(thread?.id) || thread.id === message.threadId || thread.ephemeral === true
|
|
55
|
+
|| result.modelProvider !== message.providerId || result.model !== message.model
|
|
56
|
+
|| thread.status?.type !== 'idle'
|
|
57
|
+
) return { ok: false, reason: 'provider-switch-reconciliation-required' }
|
|
58
|
+
forks.set(message.operationToken, { threadId: thread.id, providerId: message.providerId, dispatched: false })
|
|
59
|
+
if (!current(message.threadId)) {
|
|
60
|
+
await discard(message.operationToken, thread.id)
|
|
61
|
+
return { ok: false, reason: 'provider-switch-reconciliation-required' }
|
|
62
|
+
}
|
|
63
|
+
return { ok: true, value: { threadId: thread.id } }
|
|
64
|
+
} catch (error) {
|
|
65
|
+
return error.nativeError === undefined
|
|
66
|
+
? { ok: false, reason: 'provider-switch-reconciliation-required' }
|
|
67
|
+
: { ok: false }
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Shared switch/fork scheduling preserves the existing lifecycle fence. */
|
|
74
|
+
export function dispatchNativeSelectionMutation(message, ports) {
|
|
75
|
+
const { switching, switchRequests, recoveringTurns, lifecyclePending, cancelledRecovery, uncertainSwitches } = ports
|
|
76
|
+
const { nativeCall, controlCall, switchNativeThread, continuation, write, control, closed, stop } = ports
|
|
77
|
+
if (!['switch', 'continuation-fork', 'continuation-discard'].includes(message.type)) return false
|
|
78
|
+
if (
|
|
79
|
+
typeof message.id !== 'string' || switching.has(message.threadId) || recoveringTurns.has(message.threadId)
|
|
80
|
+
|| lifecyclePending(message.threadId)
|
|
81
|
+
) {
|
|
82
|
+
void write(control, { id: message.id, ok: false }).catch(stop)
|
|
83
|
+
return true
|
|
84
|
+
}
|
|
85
|
+
switching.add(message.threadId)
|
|
86
|
+
switchRequests.set(message.id, message.threadId)
|
|
87
|
+
void (async () => {
|
|
88
|
+
try {
|
|
89
|
+
const result = message.type === 'switch'
|
|
90
|
+
? await switchNativeThread(message, {
|
|
91
|
+
nativeCall,
|
|
92
|
+
controlCall,
|
|
93
|
+
current: () => !closed() && !cancelledRecovery.has(message.threadId) && !lifecyclePending(message.threadId),
|
|
94
|
+
})
|
|
95
|
+
: await continuation.handle(message)
|
|
96
|
+
if (result.reason === 'provider-switch-reconciliation-required') {
|
|
97
|
+
uncertainSwitches.set(message.threadId, undefined)
|
|
98
|
+
}
|
|
99
|
+
await write(control, { id: message.id, ...result })
|
|
100
|
+
} catch {
|
|
101
|
+
await write(control, { id: message.id, ok: false })
|
|
102
|
+
} finally {
|
|
103
|
+
switching.delete(message.threadId)
|
|
104
|
+
switchRequests.delete(message.id)
|
|
105
|
+
cancelledRecovery.delete(message.threadId)
|
|
106
|
+
}
|
|
107
|
+
})().catch(stop)
|
|
108
|
+
return true
|
|
109
|
+
}
|
|
@@ -40,7 +40,7 @@ export function createNativeProviderTransport(primary, { executable, args, env,
|
|
|
40
40
|
if (pending) {
|
|
41
41
|
connection.pending.delete(key(message.id))
|
|
42
42
|
const thread = message.result?.thread
|
|
43
|
-
if (!message.error && thread?.id && ['thread/start', 'thread/resume'].includes(pending.method)) {
|
|
43
|
+
if (!message.error && thread?.id && ['thread/start', 'thread/resume', 'thread/fork'].includes(pending.method)) {
|
|
44
44
|
owners.set(thread.id, {
|
|
45
45
|
connection,
|
|
46
46
|
status: thread.status,
|
|
@@ -225,7 +225,7 @@ export function createNativeProviderTransport(primary, { executable, args, env,
|
|
|
225
225
|
) return
|
|
226
226
|
const params = object(message.params)
|
|
227
227
|
let connection = owners.get(params?.threadId)?.connection ?? primary
|
|
228
|
-
if (initializeParams && ['thread/start', 'thread/resume'].includes(message.method)) {
|
|
228
|
+
if (initializeParams && ['thread/start', 'thread/resume', 'thread/fork'].includes(message.method)) {
|
|
229
229
|
const config = object(params?.config)
|
|
230
230
|
const tables = config ? Object.keys(config).filter(name => name.startsWith('model_providers.')) : []
|
|
231
231
|
const id = provider(params?.modelProvider) ?? provider(config?.model_provider)
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { nativeProviderFailure } from './native-provider-errors.mjs'
|
|
2
|
+
|
|
3
|
+
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value) ? value : undefined
|
|
4
|
+
const valid = (value, max = 512) =>
|
|
5
|
+
typeof value === 'string' && value.length > 0 && value.length <= max
|
|
6
|
+
&& !/[\0\r\n]/u.test(value)
|
|
7
|
+
const acknowledged = (result, threadId, providerId, model) =>
|
|
8
|
+
result?.thread?.id === threadId
|
|
9
|
+
&& result.thread.status?.type === 'idle' && result.modelProvider === providerId && result.model === model
|
|
10
|
+
|
|
11
|
+
/** A rejected provider change restores only the exact previous native session, never a Send. */
|
|
12
|
+
export async function switchNativeThread(message, { nativeCall, controlCall, current }) {
|
|
13
|
+
const threadId = message.threadId
|
|
14
|
+
const before = await nativeCall('thread/read', { threadId, includeTurns: false })
|
|
15
|
+
if (message.requireIdle === true && (before?.thread?.id !== threadId || before.thread.status?.type !== 'idle')) {
|
|
16
|
+
return { ok: true, value: { status: 'rejected', reason: 'thread-not-idle' } }
|
|
17
|
+
}
|
|
18
|
+
const prior = before?.thread
|
|
19
|
+
if (prior?.ephemeral === true) return { ok: false, reason: 'provider-switch-unsupported', restored: true }
|
|
20
|
+
let unsubscribeAttempted = false
|
|
21
|
+
let unsubscribed = false
|
|
22
|
+
try {
|
|
23
|
+
if (!current()) throw new Error('Native switch cancelled')
|
|
24
|
+
unsubscribeAttempted = true
|
|
25
|
+
await nativeCall('thread/unsubscribe', { threadId })
|
|
26
|
+
unsubscribed = true
|
|
27
|
+
if (!current()) throw new Error('Native switch cancelled')
|
|
28
|
+
const result = await nativeCall('thread/resume', {
|
|
29
|
+
threadId,
|
|
30
|
+
model: message.model,
|
|
31
|
+
modelProvider: message.providerId,
|
|
32
|
+
config: message.config,
|
|
33
|
+
excludeTurns: true,
|
|
34
|
+
})
|
|
35
|
+
return current() && acknowledged(result, threadId, message.providerId, message.model)
|
|
36
|
+
? { ok: true }
|
|
37
|
+
: { ok: false, reason: 'provider-switch-reconciliation-required' }
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (!unsubscribed) {
|
|
40
|
+
return unsubscribeAttempted && !object(error.nativeError)
|
|
41
|
+
? { ok: false, reason: 'provider-switch-reconciliation-required' }
|
|
42
|
+
: { ok: false }
|
|
43
|
+
}
|
|
44
|
+
// A timeout or uncertain acknowledgement cannot authorize another lifecycle mutation.
|
|
45
|
+
if (!object(error.nativeError) || !current()) {
|
|
46
|
+
return { ok: false, reason: 'provider-switch-reconciliation-required' }
|
|
47
|
+
}
|
|
48
|
+
const failureProvider = nativeProviderFailure({ error: error.nativeError })
|
|
49
|
+
const unsupported = prior?.modelProvider !== message.providerId
|
|
50
|
+
&& [prior?.modelProvider, message.providerId].includes(failureProvider)
|
|
51
|
+
&& failureProvider !== undefined
|
|
52
|
+
&& (error.nativeError.data?.type === 'modelProviderMismatch'
|
|
53
|
+
|| failureProvider === prior?.modelProvider)
|
|
54
|
+
let resumeToken
|
|
55
|
+
let completed = false
|
|
56
|
+
try {
|
|
57
|
+
if (
|
|
58
|
+
prior?.id !== threadId || !valid(prior.modelProvider, 128) || !valid(prior.model)
|
|
59
|
+
|| !['idle', 'notLoaded', 'systemError'].includes(prior.status?.type)
|
|
60
|
+
) throw new Error('Prior native session is unknown')
|
|
61
|
+
const read = await nativeCall('thread/read', { threadId, includeTurns: false })
|
|
62
|
+
if (
|
|
63
|
+
!current() || read?.thread?.id !== threadId || read.thread.modelProvider !== prior.modelProvider
|
|
64
|
+
|| read.thread.model !== prior.model || read.thread.status?.type !== 'notLoaded'
|
|
65
|
+
) throw new Error('Native session changed during switch')
|
|
66
|
+
const prepared = await controlCall('switch-resume', {
|
|
67
|
+
switchId: message.id,
|
|
68
|
+
threadId,
|
|
69
|
+
providerId: prior.modelProvider,
|
|
70
|
+
model: prior.model,
|
|
71
|
+
})
|
|
72
|
+
resumeToken = prepared?.resumeToken
|
|
73
|
+
if (
|
|
74
|
+
!current() || !['resume', 'native'].includes(prepared?.kind) || !object(prepared.configOverrides)
|
|
75
|
+
|| (prepared.kind === 'resume' && !valid(prepared.resumeToken, 256))
|
|
76
|
+
) throw new Error('Native restoration was not admitted')
|
|
77
|
+
const restored = await nativeCall('thread/resume', {
|
|
78
|
+
threadId,
|
|
79
|
+
modelProvider: prior.modelProvider,
|
|
80
|
+
model: prior.model,
|
|
81
|
+
config: prepared.configOverrides,
|
|
82
|
+
excludeTurns: true,
|
|
83
|
+
})
|
|
84
|
+
if (
|
|
85
|
+
!current() || !acknowledged(restored, threadId, prior.modelProvider, prior.model)
|
|
86
|
+
) throw new Error('Native restoration differed')
|
|
87
|
+
completed = await controlCall('switch-resume-complete', {
|
|
88
|
+
switchId: message.id,
|
|
89
|
+
threadId,
|
|
90
|
+
providerId: prior.modelProvider,
|
|
91
|
+
model: prior.model,
|
|
92
|
+
...(resumeToken === undefined ? {} : { resumeToken }),
|
|
93
|
+
succeeded: true,
|
|
94
|
+
}) === true
|
|
95
|
+
if (!completed || !current()) throw new Error('Native restoration completion was stale')
|
|
96
|
+
return { ok: false, ...(unsupported ? { reason: 'provider-switch-unsupported' } : {}), restored: true }
|
|
97
|
+
} catch {
|
|
98
|
+
return { ok: false, reason: 'provider-switch-reconciliation-required' }
|
|
99
|
+
} finally {
|
|
100
|
+
if (resumeToken !== undefined && !completed) {
|
|
101
|
+
await controlCall('switch-resume-complete', {
|
|
102
|
+
switchId: message.id,
|
|
103
|
+
threadId,
|
|
104
|
+
providerId: prior.modelProvider,
|
|
105
|
+
model: prior.model,
|
|
106
|
+
resumeToken,
|
|
107
|
+
succeeded: false,
|
|
108
|
+
}).catch(() => undefined)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|