cordisx 0.1.0-beta.43 → 0.1.0-beta.45
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 +52 -15
- package/dist/assets/launcher/native-provider-errors.mjs +12 -0
- package/dist/assets/launcher/native-provider-transport.mjs +309 -0
- package/dist/codex-adapter/adapter.d.mts +1 -0
- package/dist/codex-adapter/adapter.mjs +86 -3
- package/dist/codex-adapter/index.d.ts +1 -0
- package/dist/codex-adapter/index.js +1 -0
- package/dist/codex-adapter/native-chunk-structure.d.ts +5 -0
- package/dist/codex-adapter/native-chunk-structure.js +72 -0
- package/dist/codex-adapter/native-submission-structure.js +25 -3
- package/dist/native/native-helper-artifact.json +1 -1
- package/dist/src/cli/production-host-bootstrap.d.ts.map +1 -1
- package/dist/src/cli/production-host-bootstrap.js +13 -6
- package/dist/src/cli/production-host-bootstrap.js.map +1 -1
- package/dist/src/launcher/builtin-skill.d.ts.map +1 -1
- package/dist/src/launcher/builtin-skill.js +6 -3
- package/dist/src/launcher/builtin-skill.js.map +1 -1
- package/dist/src/launcher/cdp.d.ts +1 -1
- package/dist/src/launcher/cdp.d.ts.map +1 -1
- package/dist/src/launcher/cdp.js +18 -2
- package/dist/src/launcher/cdp.js.map +1 -1
- package/dist/src/launcher/configured-encrypted-replay.d.ts.map +1 -1
- package/dist/src/launcher/configured-encrypted-replay.js +7 -1
- package/dist/src/launcher/configured-encrypted-replay.js.map +1 -1
- package/dist/src/launcher/native-agent-models.d.ts +1 -1
- package/dist/src/launcher/native-agent-models.d.ts.map +1 -1
- package/dist/src/launcher/native-agent-models.js +1 -1
- package/dist/src/launcher/native-agent-models.js.map +1 -1
- package/dist/src/launcher/native-app-resources.d.ts.map +1 -1
- package/dist/src/launcher/native-app-resources.js +8 -2
- package/dist/src/launcher/native-app-resources.js.map +1 -1
- package/dist/src/launcher/native-submission-cdp-channel.d.ts +1 -1
- package/dist/src/launcher/native-submission-cdp-channel.d.ts.map +1 -1
- package/dist/src/launcher/native-submission-cdp-channel.js +81 -4
- package/dist/src/launcher/native-submission-cdp-channel.js.map +1 -1
- package/dist/src/launcher/native-submission-controller-contracts.d.ts +5 -0
- 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 +41 -8
- package/dist/src/launcher/native-submission-controller.js.map +1 -1
- package/dist/src/renderer/adapter/native-model-provider-interaction.d.ts.map +1 -1
- package/dist/src/renderer/adapter/native-model-provider-interaction.js +3 -0
- package/dist/src/renderer/adapter/native-model-provider-interaction.js.map +1 -1
- package/dist/src/renderer/adapter/native-model-provider-seat.d.ts.map +1 -1
- package/dist/src/renderer/adapter/native-model-provider-seat.js +7 -2
- package/dist/src/renderer/adapter/native-model-provider-seat.js.map +1 -1
- package/package.json +1 -1
|
@@ -6,12 +6,30 @@ import { NativeTurnDiagnostics } from './native-turn-diagnostics.mjs'
|
|
|
6
6
|
import { NativeRequestTracker } from './native-request-tracker.mjs'
|
|
7
7
|
import { prepareManagedResume } from './native-managed-resume.mjs'
|
|
8
8
|
import { nativeInput } from './native-app-server-input.mjs'
|
|
9
|
+
import { createNativeProviderTransport } from './native-provider-transport.mjs'
|
|
10
|
+
import { nativeProviderFailure } from './native-provider-errors.mjs'
|
|
9
11
|
|
|
10
12
|
const socketPath = process.env.CORDISX_NATIVE_CONTROL_SOCKET
|
|
11
13
|
const nonce = process.env.CORDISX_NATIVE_CONTROL_NONCE
|
|
12
14
|
const executable = process.env.CORDISX_NATIVE_REAL_CODEX_PATH
|
|
13
15
|
const agentModelsBinding = process.env.CORDISX_NATIVE_AGENT_MODELS_BINDING
|
|
14
16
|
const marker = 'cordisx.operation_token'
|
|
17
|
+
function configuredOverrides(existing, overrides) {
|
|
18
|
+
const merged = { ...existing, ...overrides }
|
|
19
|
+
for (const [name, definition] of Object.entries(overrides)) {
|
|
20
|
+
if (!name.startsWith('model_providers.') || !object(definition)) continue
|
|
21
|
+
const id = name.slice('model_providers.'.length)
|
|
22
|
+
merged[name] = {
|
|
23
|
+
...definition,
|
|
24
|
+
...object(object(existing?.model_providers)?.[id]),
|
|
25
|
+
...object(existing?.[name]),
|
|
26
|
+
base_url: definition.base_url,
|
|
27
|
+
wire_api: definition.wire_api,
|
|
28
|
+
}
|
|
29
|
+
if (Object.hasOwn(merged, `${name}.base_url`)) merged[`${name}.base_url`] = definition.base_url
|
|
30
|
+
}
|
|
31
|
+
return merged
|
|
32
|
+
}
|
|
15
33
|
// Native history and plugin catalogs can exceed the small private control-message budget.
|
|
16
34
|
const maxNativeLineBytes = 64 * 1024 * 1024
|
|
17
35
|
const maxControlLineBytes = 8 * 1024 * 1024
|
|
@@ -48,6 +66,7 @@ if (agentModelsBinding && nativeArgs.includes('app-server')) {
|
|
|
48
66
|
)
|
|
49
67
|
}
|
|
50
68
|
const child = spawn(executable, nativeArgs, { stdio: ['pipe', 'pipe', 'inherit'], env: process.env })
|
|
69
|
+
const nativeTransport = createNativeProviderTransport(child, { executable, args: nativeArgs, env: process.env })
|
|
51
70
|
const control = net.connect(socketPath)
|
|
52
71
|
const controlCalls = new Map()
|
|
53
72
|
const privateCalls = new Map()
|
|
@@ -154,7 +173,7 @@ const write = (stream, value) =>
|
|
|
154
173
|
reject(new Error('Native transport is closed'))
|
|
155
174
|
return
|
|
156
175
|
}
|
|
157
|
-
if (stream ===
|
|
176
|
+
if (stream === nativeTransport.stdin) {
|
|
158
177
|
try {
|
|
159
178
|
const message = typeof value === 'string' ? JSON.parse(value) : value
|
|
160
179
|
if (message?.method === 'initialize') {
|
|
@@ -195,7 +214,8 @@ function stop(reason = 'transport-closed') {
|
|
|
195
214
|
agentFirstTurns.clear()
|
|
196
215
|
control.destroy()
|
|
197
216
|
nativeInput.destroy()
|
|
198
|
-
|
|
217
|
+
nativeTransport.stdin.destroy()
|
|
218
|
+
nativeTransport.close()
|
|
199
219
|
if (child.exitCode === null) child.kill('SIGTERM')
|
|
200
220
|
}
|
|
201
221
|
function call(stream, calls, request, timeout = 30_000) {
|
|
@@ -218,7 +238,7 @@ function controlCall(type, input = {}) {
|
|
|
218
238
|
return call(control, controlCalls, { type, ...input })
|
|
219
239
|
}
|
|
220
240
|
function nativeCall(method, params) {
|
|
221
|
-
return call(
|
|
241
|
+
return call(nativeTransport.stdin, privateCalls, { method, params })
|
|
222
242
|
}
|
|
223
243
|
function settle(calls, message, isControl) {
|
|
224
244
|
const pending = calls.get(message.id)
|
|
@@ -235,6 +255,8 @@ function settle(calls, message, isControl) {
|
|
|
235
255
|
}
|
|
236
256
|
|
|
237
257
|
function missingManagedProvider(message) {
|
|
258
|
+
const structured = nativeProviderFailure(message)
|
|
259
|
+
if (structured !== undefined) return structured
|
|
238
260
|
if (message?.error?.code !== -32600 || typeof message.error.message !== 'string') return undefined
|
|
239
261
|
const match = /^failed to load configuration: Model provider `([^`\0\r\n]{1,128})` not found$/u.exec(
|
|
240
262
|
message.error.message,
|
|
@@ -298,7 +320,7 @@ child.on('error', error => {
|
|
|
298
320
|
nativeLifecycle('child-spawn-failed', { errorCode: nativeErrorCode(error) })
|
|
299
321
|
stop(error)
|
|
300
322
|
})
|
|
301
|
-
|
|
323
|
+
nativeTransport.stdin.on('error', error => {
|
|
302
324
|
nativeLifecycle('child-input-error', { errorCode: nativeErrorCode(error) })
|
|
303
325
|
stop(error)
|
|
304
326
|
})
|
|
@@ -320,7 +342,7 @@ async function forward(line) {
|
|
|
320
342
|
try {
|
|
321
343
|
message = JSON.parse(line)
|
|
322
344
|
} catch {
|
|
323
|
-
await write(
|
|
345
|
+
await write(nativeTransport.stdin, line)
|
|
324
346
|
return
|
|
325
347
|
}
|
|
326
348
|
const params = object(message?.params)
|
|
@@ -433,7 +455,7 @@ async function forward(line) {
|
|
|
433
455
|
if (prepared) {
|
|
434
456
|
preparedResumes.set(message.id, { threadId: prepared.threadId, resumeToken: prepared.resumeToken })
|
|
435
457
|
// A failed native write must not cause a second attempt with the original request.
|
|
436
|
-
await write(
|
|
458
|
+
await write(nativeTransport.stdin, prepared.message)
|
|
437
459
|
return
|
|
438
460
|
}
|
|
439
461
|
}
|
|
@@ -448,13 +470,13 @@ async function forward(line) {
|
|
|
448
470
|
...(typeof providerId === 'string' ? { providerId } : {}),
|
|
449
471
|
})
|
|
450
472
|
if (object(overrides) && Object.keys(overrides).length) {
|
|
451
|
-
params.config =
|
|
452
|
-
await write(
|
|
473
|
+
params.config = configuredOverrides(object(params.config), overrides)
|
|
474
|
+
await write(nativeTransport.stdin, message)
|
|
453
475
|
return
|
|
454
476
|
}
|
|
455
477
|
} catch {}
|
|
456
478
|
}
|
|
457
|
-
await write(
|
|
479
|
+
await write(nativeTransport.stdin, line)
|
|
458
480
|
return
|
|
459
481
|
}
|
|
460
482
|
let admitted = false
|
|
@@ -480,7 +502,9 @@ async function forward(line) {
|
|
|
480
502
|
if (message.method === 'thread/start') {
|
|
481
503
|
params.modelProvider = dispatch.providerId
|
|
482
504
|
params.model = dispatch.model
|
|
483
|
-
params.config =
|
|
505
|
+
params.config = dispatch.providerSource === 'config'
|
|
506
|
+
? configuredOverrides(nextConfig, dispatch.configOverrides)
|
|
507
|
+
: { ...nextConfig, ...dispatch.configOverrides }
|
|
484
508
|
if (typeof dispatch.reasoningEffort === 'string') {
|
|
485
509
|
params.config.model_reasoning_effort = dispatch.reasoningEffort
|
|
486
510
|
}
|
|
@@ -511,7 +535,7 @@ async function forward(line) {
|
|
|
511
535
|
firstTurns.delete(params.threadId)
|
|
512
536
|
agentFirstTurns.delete(params.threadId)
|
|
513
537
|
}
|
|
514
|
-
await write(
|
|
538
|
+
await write(nativeTransport.stdin, JSON.stringify(message))
|
|
515
539
|
} catch {
|
|
516
540
|
desktopCalls.delete(message?.id)
|
|
517
541
|
if (admitted) {
|
|
@@ -583,7 +607,7 @@ async function recoverResume(id, params, providerId, failureLine) {
|
|
|
583
607
|
|
|
584
608
|
// A persisted history can exist after its executable session has been unloaded.
|
|
585
609
|
// Replay only the exact missing-thread rejection, never a timeout or failed turn.
|
|
586
|
-
async function recoverUnloadedTurn(id, params, failureLine) {
|
|
610
|
+
async function recoverUnloadedTurn(id, params, failureLine, expectedProvider) {
|
|
587
611
|
const threadId = params.threadId
|
|
588
612
|
if (recoveringTurns.has(threadId) || switching.has(threadId) || lifecyclePending(threadId)) {
|
|
589
613
|
await write(process.stdout, failureLine)
|
|
@@ -597,9 +621,14 @@ async function recoverUnloadedTurn(id, params, failureLine) {
|
|
|
597
621
|
const read = await nativeCall('thread/read', { threadId, includeTurns: false })
|
|
598
622
|
const thread = object(read?.thread)
|
|
599
623
|
if (
|
|
600
|
-
thread?.id !== threadId
|
|
624
|
+
thread?.id !== threadId
|
|
625
|
+
|| !(expectedProvider === undefined ? ['notLoaded'] : ['idle', 'notLoaded', 'systemError']).includes(
|
|
626
|
+
thread.status?.type,
|
|
627
|
+
)
|
|
601
628
|
|| typeof thread.modelProvider !== 'string' || thread.modelProvider === 'openai'
|
|
629
|
+
|| (expectedProvider !== undefined && expectedProvider !== thread.modelProvider)
|
|
602
630
|
|| typeof thread.model !== 'string' || thread.model === ''
|
|
631
|
+
|| thread.ephemeral === true
|
|
603
632
|
) throw new Error('Missing thread is not an unloaded managed session')
|
|
604
633
|
const prepared = await controlCall('resume', {
|
|
605
634
|
threadId,
|
|
@@ -610,6 +639,9 @@ async function recoverUnloadedTurn(id, params, failureLine) {
|
|
|
610
639
|
throw new Error('Managed recovery rejected')
|
|
611
640
|
}
|
|
612
641
|
resumeToken = prepared.resumeToken
|
|
642
|
+
if (expectedProvider !== undefined && thread.status.type !== 'notLoaded') {
|
|
643
|
+
await nativeCall('thread/unsubscribe', { threadId })
|
|
644
|
+
}
|
|
613
645
|
const resumed = await nativeCall('thread/resume', {
|
|
614
646
|
threadId,
|
|
615
647
|
modelProvider: thread.modelProvider,
|
|
@@ -672,10 +704,10 @@ readLines(nativeInput, line => {
|
|
|
672
704
|
})
|
|
673
705
|
nativeInput.once('end', () => {
|
|
674
706
|
nativeLifecycle('desktop-input-ended')
|
|
675
|
-
void inputQueue.finally(() =>
|
|
707
|
+
void inputQueue.finally(() => nativeTransport.stdin.end())
|
|
676
708
|
})
|
|
677
709
|
|
|
678
|
-
readLines(
|
|
710
|
+
readLines(nativeTransport.stdout, line => {
|
|
679
711
|
let message
|
|
680
712
|
try {
|
|
681
713
|
message = JSON.parse(line)
|
|
@@ -694,6 +726,11 @@ readLines(child.stdout, line => {
|
|
|
694
726
|
if (message.method === undefined) lifecycleCalls.delete(message.id)
|
|
695
727
|
const turnParams = message.method === undefined ? unmarkedTurns.take(message.id) : undefined
|
|
696
728
|
if (turnParams !== undefined) {
|
|
729
|
+
const providerId = nativeProviderFailure(message)
|
|
730
|
+
if (providerId !== undefined && providerId !== 'openai') {
|
|
731
|
+
void recoverUnloadedTurn(message.id, turnParams, line, providerId).catch(stop)
|
|
732
|
+
return
|
|
733
|
+
}
|
|
697
734
|
if (message.error?.code === -32600 && message.error.message === `thread not found: ${turnParams.threadId}`) {
|
|
698
735
|
void recoverUnloadedTurn(message.id, turnParams, line).catch(stop)
|
|
699
736
|
return
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Native structured provider failures occur before a model turn is accepted. */
|
|
2
|
+
export function nativeProviderFailure(message) {
|
|
3
|
+
if (message?.error?.code !== -32600) return undefined
|
|
4
|
+
const data = message.error.data
|
|
5
|
+
if (!['modelProviderNotConfigured', 'modelProviderMismatch'].includes(data?.type)) return undefined
|
|
6
|
+
if (typeof data.modelProvider !== 'string' || !/^[a-zA-Z0-9_.:-]{1,128}$/u.test(data.modelProvider)) return undefined
|
|
7
|
+
if (
|
|
8
|
+
data.type === 'modelProviderMismatch'
|
|
9
|
+
&& (typeof data.currentModelProvider !== 'string' || data.currentModelProvider === data.modelProvider)
|
|
10
|
+
) return undefined
|
|
11
|
+
return data.modelProvider
|
|
12
|
+
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { randomUUID } from 'node:crypto'
|
|
3
|
+
import { PassThrough, Writable } from 'node:stream'
|
|
4
|
+
|
|
5
|
+
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value) ? value : undefined
|
|
6
|
+
const provider = value => typeof value === 'string' && /^[a-zA-Z0-9_.:-]{1,128}$/u.test(value) ? value : undefined
|
|
7
|
+
const key = id => JSON.stringify([typeof id, id])
|
|
8
|
+
const MAX_FRAME = 64 * 1024 * 1024
|
|
9
|
+
const MAX_WORKERS = 128
|
|
10
|
+
|
|
11
|
+
/** Multiplex native connections after the Host has admitted the original request.
|
|
12
|
+
* Credentials and endpoints remain in that request's config, never argv or disk.
|
|
13
|
+
* The inert process default satisfies native provider identity checks and cannot
|
|
14
|
+
* silently send a request when a per-thread override has been lost.
|
|
15
|
+
*/
|
|
16
|
+
export function createNativeProviderTransport(primary, { executable, args, env, initialize } = {}) {
|
|
17
|
+
const stdout = new PassThrough()
|
|
18
|
+
const workers = new Map()
|
|
19
|
+
const owners = new Map()
|
|
20
|
+
const serverRequests = new Map()
|
|
21
|
+
const primaryWrite = primary.stdin.write.bind(primary.stdin)
|
|
22
|
+
let initializeParams = initialize
|
|
23
|
+
let disposed = false
|
|
24
|
+
const emit = (message, original) => {
|
|
25
|
+
if (!disposed) stdout.write((original ?? JSON.stringify(message)) + '\n')
|
|
26
|
+
}
|
|
27
|
+
const send = (write, message) =>
|
|
28
|
+
new Promise((resolve, reject) => {
|
|
29
|
+
write(JSON.stringify(message) + '\n', error => error ? reject(error) : resolve())
|
|
30
|
+
})
|
|
31
|
+
const receive = (connection, message, original) => {
|
|
32
|
+
if (connection !== primary && connection.initializeId === message.id && message.method === undefined) {
|
|
33
|
+
message.error
|
|
34
|
+
? connection.rejectReady(new Error('Native provider initialization rejected'))
|
|
35
|
+
: connection.resolveReady()
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
if (message.method === undefined) {
|
|
39
|
+
const pending = connection.pending?.get(key(message.id))
|
|
40
|
+
if (pending) {
|
|
41
|
+
connection.pending.delete(key(message.id))
|
|
42
|
+
const thread = message.result?.thread
|
|
43
|
+
if (!message.error && thread?.id && ['thread/start', 'thread/resume'].includes(pending.method)) {
|
|
44
|
+
owners.set(thread.id, {
|
|
45
|
+
connection,
|
|
46
|
+
status: thread.status,
|
|
47
|
+
model: thread.model,
|
|
48
|
+
providerId: message.result.modelProvider ?? thread.modelProvider,
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
if (!message.error && pending.method === 'thread/unsubscribe') owners.delete(pending.threadId)
|
|
52
|
+
if (!message.error && pending.method === 'turn/start' && message.result?.turn?.id) {
|
|
53
|
+
const owner = owners.get(pending.threadId)
|
|
54
|
+
if (owner?.connection === connection) owner.turn = message.result.turn
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(message.result?.data)) {
|
|
58
|
+
for (const thread of message.result.data) {
|
|
59
|
+
const owner = owners.get(thread.id)
|
|
60
|
+
if (owner?.connection !== primary && owner?.status) {
|
|
61
|
+
thread.status = owner.status
|
|
62
|
+
original = undefined
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} else if (message.id !== undefined && connection !== primary) {
|
|
67
|
+
const id = `cordisx-provider-request-${randomUUID()}`
|
|
68
|
+
serverRequests.set(id, { connection, id: message.id, threadId: message.params?.threadId })
|
|
69
|
+
message = { ...message, id }
|
|
70
|
+
original = undefined
|
|
71
|
+
} else if (connection !== primary) {
|
|
72
|
+
if (message.method === 'serverRequest/resolved') {
|
|
73
|
+
const requestId = message.params?.requestId
|
|
74
|
+
const request = [...serverRequests].find(([, entry]) =>
|
|
75
|
+
entry.connection === connection && key(entry.id) === key(requestId)
|
|
76
|
+
)
|
|
77
|
+
if (!request) return
|
|
78
|
+
serverRequests.delete(request[0])
|
|
79
|
+
message = { ...message, params: { ...message.params, requestId: request[0] } }
|
|
80
|
+
original = undefined
|
|
81
|
+
}
|
|
82
|
+
const threadId = message.params?.threadId ?? message.params?.thread?.id
|
|
83
|
+
// Global account/config notifications still belong to the primary Host.
|
|
84
|
+
if (typeof threadId !== 'string' && message.method !== 'serverRequest/resolved') return
|
|
85
|
+
const owner = owners.get(threadId)
|
|
86
|
+
if (owner?.connection === connection) {
|
|
87
|
+
if (message.method === 'thread/status/changed') owner.status = message.params.status
|
|
88
|
+
if (message.method === 'turn/started') owner.turn = message.params.turn
|
|
89
|
+
if (message.method === 'turn/completed') delete owner.turn
|
|
90
|
+
if (message.method === 'thread/closed') owners.delete(threadId)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
emit(message, original)
|
|
94
|
+
}
|
|
95
|
+
const attach = (stream, connection) => {
|
|
96
|
+
let buffer = ''
|
|
97
|
+
stream.setEncoding('utf8')
|
|
98
|
+
stream.on('data', chunk => {
|
|
99
|
+
buffer += chunk
|
|
100
|
+
let boundary
|
|
101
|
+
while ((boundary = buffer.indexOf('\n')) >= 0) {
|
|
102
|
+
const line = buffer.slice(0, boundary)
|
|
103
|
+
buffer = buffer.slice(boundary + 1)
|
|
104
|
+
if (Buffer.byteLength(line) > MAX_FRAME) {
|
|
105
|
+
connection.child?.kill('SIGTERM')
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
receive(connection, JSON.parse(line), line)
|
|
110
|
+
} catch {
|
|
111
|
+
if (connection === primary && !disposed) stdout.write(line + '\n')
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (Buffer.byteLength(buffer) > MAX_FRAME) connection.child?.kill('SIGTERM')
|
|
115
|
+
})
|
|
116
|
+
}
|
|
117
|
+
primary.pending = new Map()
|
|
118
|
+
attach(primary.stdout, primary)
|
|
119
|
+
|
|
120
|
+
const workerFor = async id => {
|
|
121
|
+
let worker = workers.get(id)
|
|
122
|
+
if (!worker) {
|
|
123
|
+
if (workers.size >= MAX_WORKERS) throw new Error('Native provider connection limit reached')
|
|
124
|
+
const insertion = args.indexOf('app-server')
|
|
125
|
+
if (insertion < 0 || !initializeParams) throw new Error('Native provider launch unavailable')
|
|
126
|
+
const overrides = [
|
|
127
|
+
'-c',
|
|
128
|
+
`model_provider=${JSON.stringify(id)}`,
|
|
129
|
+
'-c',
|
|
130
|
+
`model_providers={${
|
|
131
|
+
JSON.stringify(id)
|
|
132
|
+
}={name="CordisX provider transport",base_url="http://127.0.0.1:0/v1",wire_api="responses",requires_openai_auth=false}}`,
|
|
133
|
+
]
|
|
134
|
+
const child = spawn(executable, [...args.slice(0, insertion), ...overrides, ...args.slice(insertion)], {
|
|
135
|
+
env,
|
|
136
|
+
stdio: ['pipe', 'pipe', 'inherit'],
|
|
137
|
+
})
|
|
138
|
+
worker = {
|
|
139
|
+
child,
|
|
140
|
+
providerId: id,
|
|
141
|
+
pending: new Map(),
|
|
142
|
+
write: child.stdin.write.bind(child.stdin),
|
|
143
|
+
initializeId: `cordisx-provider-initialize-${randomUUID()}`,
|
|
144
|
+
}
|
|
145
|
+
worker.ready = new Promise((resolve, reject) => {
|
|
146
|
+
const timer = setTimeout(() => {
|
|
147
|
+
worker.rejectReady(new Error('Native provider initialization timed out'))
|
|
148
|
+
child.kill('SIGTERM')
|
|
149
|
+
}, 15000)
|
|
150
|
+
worker.resolveReady = () => {
|
|
151
|
+
clearTimeout(timer)
|
|
152
|
+
resolve()
|
|
153
|
+
}
|
|
154
|
+
worker.rejectReady = error => {
|
|
155
|
+
clearTimeout(timer)
|
|
156
|
+
reject(error)
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
workers.set(id, worker)
|
|
160
|
+
attach(child.stdout, worker)
|
|
161
|
+
const failed = () => {
|
|
162
|
+
if (worker.closed) return
|
|
163
|
+
worker.closed = true
|
|
164
|
+
if (workers.get(id) === worker) workers.delete(id)
|
|
165
|
+
worker.rejectReady(new Error('Native provider connection closed'))
|
|
166
|
+
for (const pending of worker.pending.values()) {
|
|
167
|
+
emit({
|
|
168
|
+
id: pending.id,
|
|
169
|
+
error: {
|
|
170
|
+
code: -32000,
|
|
171
|
+
message: 'Native provider connection closed. Check thread history before retrying.',
|
|
172
|
+
},
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
worker.pending.clear()
|
|
176
|
+
for (const [threadId, owner] of owners) {
|
|
177
|
+
if (owner.connection !== worker) continue
|
|
178
|
+
if (owner.turn?.id) {
|
|
179
|
+
emit({
|
|
180
|
+
method: 'turn/completed',
|
|
181
|
+
params: {
|
|
182
|
+
threadId,
|
|
183
|
+
turn: {
|
|
184
|
+
...owner.turn,
|
|
185
|
+
status: 'failed',
|
|
186
|
+
error: { message: 'Native provider connection closed. Check thread history before retrying.' },
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
})
|
|
190
|
+
}
|
|
191
|
+
emit({ method: 'thread/status/changed', params: { threadId, status: { type: 'systemError' } } })
|
|
192
|
+
owners.delete(threadId)
|
|
193
|
+
}
|
|
194
|
+
for (const [requestId, request] of serverRequests) {
|
|
195
|
+
if (request.connection !== worker) continue
|
|
196
|
+
emit({ method: 'serverRequest/resolved', params: { requestId, threadId: request.threadId } })
|
|
197
|
+
serverRequests.delete(requestId)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
child.once('error', failed)
|
|
201
|
+
child.once('exit', failed)
|
|
202
|
+
child.stdin.on('error', failed)
|
|
203
|
+
void send(worker.write, { id: worker.initializeId, method: 'initialize', params: initializeParams })
|
|
204
|
+
.catch(worker.rejectReady)
|
|
205
|
+
}
|
|
206
|
+
await worker.ready
|
|
207
|
+
if (worker.closed || disposed) throw new Error('Native provider connection closed')
|
|
208
|
+
if (!worker.initialized) {
|
|
209
|
+
worker.initialized = true
|
|
210
|
+
await send(worker.write, { method: 'initialized', params: {} })
|
|
211
|
+
}
|
|
212
|
+
return worker
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const route = async (message, original) => {
|
|
216
|
+
if (message.method === 'initialize') initializeParams = message.params
|
|
217
|
+
if (message.method === undefined && serverRequests.has(message.id)) {
|
|
218
|
+
const request = serverRequests.get(message.id)
|
|
219
|
+
serverRequests.delete(message.id)
|
|
220
|
+
return await send(request.connection.write, { ...message, id: request.id })
|
|
221
|
+
}
|
|
222
|
+
if (
|
|
223
|
+
message.method === undefined && typeof message.id === 'string'
|
|
224
|
+
&& message.id.startsWith('cordisx-provider-request-')
|
|
225
|
+
) return
|
|
226
|
+
const params = object(message.params)
|
|
227
|
+
let connection = owners.get(params?.threadId)?.connection ?? primary
|
|
228
|
+
if (initializeParams && ['thread/start', 'thread/resume'].includes(message.method)) {
|
|
229
|
+
const config = object(params?.config)
|
|
230
|
+
const tables = config ? Object.keys(config).filter(name => name.startsWith('model_providers.')) : []
|
|
231
|
+
const id = provider(params?.modelProvider) ?? provider(config?.model_provider)
|
|
232
|
+
?? (tables.length === 1 ? provider(tables[0].slice('model_providers.'.length)) : undefined)
|
|
233
|
+
const table = id && (object(config?.[`model_providers.${id}`]) ?? object(config?.model_providers?.[id]))
|
|
234
|
+
if (id && id !== 'openai' && table?.wire_api === 'responses') connection = await workerFor(id)
|
|
235
|
+
else if (id === 'openai') connection = primary
|
|
236
|
+
}
|
|
237
|
+
if (disposed || connection.closed) throw new Error('Native provider connection closed')
|
|
238
|
+
if (message.id !== undefined && message.method !== undefined) {
|
|
239
|
+
connection.pending.set(key(message.id), { id: message.id, method: message.method, threadId: params?.threadId })
|
|
240
|
+
}
|
|
241
|
+
const write = connection === primary ? primaryWrite : connection.write
|
|
242
|
+
await new Promise((resolve, reject) => write(original + '\n', error => error ? reject(error) : resolve()))
|
|
243
|
+
}
|
|
244
|
+
let buffer = ''
|
|
245
|
+
const stdin = new Writable({
|
|
246
|
+
write(chunk, _encoding, callback) {
|
|
247
|
+
buffer += chunk.toString('utf8')
|
|
248
|
+
const frames = []
|
|
249
|
+
let boundary
|
|
250
|
+
while ((boundary = buffer.indexOf('\n')) >= 0) {
|
|
251
|
+
const line = buffer.slice(0, boundary)
|
|
252
|
+
buffer = buffer.slice(boundary + 1)
|
|
253
|
+
if (Buffer.byteLength(line) > MAX_FRAME) {
|
|
254
|
+
callback(new Error('Native frame exceeds limit'))
|
|
255
|
+
return
|
|
256
|
+
}
|
|
257
|
+
try {
|
|
258
|
+
frames.push({ message: JSON.parse(line), original: line })
|
|
259
|
+
} catch {
|
|
260
|
+
primaryWrite(line + '\n')
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (Buffer.byteLength(buffer) > MAX_FRAME) {
|
|
264
|
+
callback(new Error('Native frame exceeds limit'))
|
|
265
|
+
return
|
|
266
|
+
}
|
|
267
|
+
void (async () => {
|
|
268
|
+
for (const { message: frame, original } of frames) {
|
|
269
|
+
try {
|
|
270
|
+
await route(frame, original)
|
|
271
|
+
} catch {
|
|
272
|
+
if (frame.id !== undefined) {
|
|
273
|
+
emit({
|
|
274
|
+
id: frame.id,
|
|
275
|
+
error: {
|
|
276
|
+
code: -32000,
|
|
277
|
+
message: 'Native provider transport unavailable. Check thread history before retrying.',
|
|
278
|
+
},
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
})().then(() => callback(), callback)
|
|
284
|
+
},
|
|
285
|
+
final(callback) {
|
|
286
|
+
primary.stdin.end(callback)
|
|
287
|
+
},
|
|
288
|
+
})
|
|
289
|
+
const close = () => {
|
|
290
|
+
if (disposed) return
|
|
291
|
+
disposed = true
|
|
292
|
+
for (const worker of workers.values()) {
|
|
293
|
+
worker.rejectReady(new Error('Native provider transport disposed'))
|
|
294
|
+
if (worker.child.exitCode === null) worker.child.kill('SIGTERM')
|
|
295
|
+
}
|
|
296
|
+
workers.clear()
|
|
297
|
+
owners.clear()
|
|
298
|
+
serverRequests.clear()
|
|
299
|
+
stdin.destroy()
|
|
300
|
+
stdout.end()
|
|
301
|
+
}
|
|
302
|
+
primary.once('exit', close)
|
|
303
|
+
return {
|
|
304
|
+
stdin,
|
|
305
|
+
stdout,
|
|
306
|
+
close,
|
|
307
|
+
workers: () => [...workers.values()].map(w => ({ providerId: w.providerId, pid: w.child.pid })),
|
|
308
|
+
}
|
|
309
|
+
}
|
|
@@ -5872,6 +5872,62 @@ function nativeLazyRequestSource(factory, factoryReturn, reference) {
|
|
|
5872
5872
|
return binding.init;
|
|
5873
5873
|
}
|
|
5874
5874
|
|
|
5875
|
+
// src/native-chunk-structure.ts
|
|
5876
|
+
function nativeChunkPaths(source) {
|
|
5877
|
+
if (!source.includes("assets/async/")) return [];
|
|
5878
|
+
const paths = /* @__PURE__ */ new Set();
|
|
5879
|
+
visitSyntax(parseNativeSource(source), (node) => {
|
|
5880
|
+
if (node.type !== "AssignmentExpression" || !member(node.left, "u")) return;
|
|
5881
|
+
const fn = node.right;
|
|
5882
|
+
if (fn.type !== "ArrowFunctionExpression" || fn.params.length !== 1 || fn.params[0].type !== "Identifier") return;
|
|
5883
|
+
const parameter = fn.params[0].name;
|
|
5884
|
+
const keys = /* @__PURE__ */ new Set();
|
|
5885
|
+
const tables = /* @__PURE__ */ new Map();
|
|
5886
|
+
visitSyntax(fn.body, (child) => {
|
|
5887
|
+
if (child.type === "MemberExpression" && child.computed && child.object.type === "ObjectExpression" && child.property.type === "Identifier" && child.property.name === parameter) {
|
|
5888
|
+
const table = new Map([...properties(child.object)].map(([key, value]) => [String(key), value]));
|
|
5889
|
+
tables.set(child.object, table);
|
|
5890
|
+
for (const key of table.keys()) keys.add(key);
|
|
5891
|
+
}
|
|
5892
|
+
});
|
|
5893
|
+
const evaluate = (expression, key) => {
|
|
5894
|
+
const value = literal2(expression);
|
|
5895
|
+
if (typeof value === "string" || typeof value === "number") return value;
|
|
5896
|
+
if (expression.type === "Identifier" && expression.name === parameter) return key;
|
|
5897
|
+
if (expression.type === "BinaryExpression" && expression.operator === "+") {
|
|
5898
|
+
const left = evaluate(expression.left, key);
|
|
5899
|
+
const right = evaluate(expression.right, key);
|
|
5900
|
+
if (left !== void 0 && right !== void 0) return String(left) + String(right);
|
|
5901
|
+
}
|
|
5902
|
+
if (expression.type === "MemberExpression" && expression.computed && expression.object.type === "ObjectExpression" && expression.property.name === parameter) {
|
|
5903
|
+
const entry = tables.get(expression.object)?.get(key);
|
|
5904
|
+
if (entry) return evaluate(entry, key);
|
|
5905
|
+
}
|
|
5906
|
+
return void 0;
|
|
5907
|
+
};
|
|
5908
|
+
for (const key of keys) {
|
|
5909
|
+
const value = evaluate(fn.body, key);
|
|
5910
|
+
if (typeof value === "string" && /^assets\/async\/[A-Za-z0-9._-]+\.js$/u.test(value)) paths.add(value);
|
|
5911
|
+
}
|
|
5912
|
+
});
|
|
5913
|
+
return [...paths];
|
|
5914
|
+
}
|
|
5915
|
+
function nativeModuleFactories(ast) {
|
|
5916
|
+
const result = /* @__PURE__ */ new Map();
|
|
5917
|
+
for (const statement of ast.body) {
|
|
5918
|
+
if (statement.type !== "ExportNamedDeclaration" || statement.declaration?.type !== "VariableDeclaration") continue;
|
|
5919
|
+
for (const declaration of statement.declaration.declarations) {
|
|
5920
|
+
if (declaration.id.name !== "__webpack_modules__" || declaration.init?.type !== "ObjectExpression") continue;
|
|
5921
|
+
for (const [id, factory] of properties(declaration.init)) {
|
|
5922
|
+
if (factory.type !== "FunctionExpression" || factory.params.length !== 3) continue;
|
|
5923
|
+
if (result.has(factory)) throw new Error("Duplicate native module factory");
|
|
5924
|
+
result.set(factory, String(id));
|
|
5925
|
+
}
|
|
5926
|
+
}
|
|
5927
|
+
}
|
|
5928
|
+
return result;
|
|
5929
|
+
}
|
|
5930
|
+
|
|
5875
5931
|
// src/native-submission-structure.ts
|
|
5876
5932
|
var TOKEN = "cordisx.operation_token";
|
|
5877
5933
|
var carrier = "__cordisxOperationToken";
|
|
@@ -5903,8 +5959,13 @@ var ResourcePlan = class {
|
|
|
5903
5959
|
constructor(resource) {
|
|
5904
5960
|
this.resource = resource;
|
|
5905
5961
|
this.ast = parseNativeSource(resource.source);
|
|
5962
|
+
const factories = nativeModuleFactories(this.ast);
|
|
5906
5963
|
visitSyntax(this.ast, (node, parents) => {
|
|
5907
|
-
if (isFunction2(node))
|
|
5964
|
+
if (isFunction2(node)) {
|
|
5965
|
+
this.functions.push(node);
|
|
5966
|
+
const factory = parents.find((parent) => factories.has(parent));
|
|
5967
|
+
if (factory) this.owners.set(node, factories.get(factory));
|
|
5968
|
+
}
|
|
5908
5969
|
if (node.type !== "VariableDeclarator") return;
|
|
5909
5970
|
const fn = parents.findLast(isFunction2);
|
|
5910
5971
|
if (!fn) return;
|
|
@@ -5919,6 +5980,17 @@ var ResourcePlan = class {
|
|
|
5919
5980
|
capabilities = [];
|
|
5920
5981
|
functions = [];
|
|
5921
5982
|
declarations = /* @__PURE__ */ new Map();
|
|
5983
|
+
owners = /* @__PURE__ */ new Map();
|
|
5984
|
+
capabilityOwners = /* @__PURE__ */ new Map();
|
|
5985
|
+
claimCapability(capability, fn) {
|
|
5986
|
+
if (this.capabilityOwners.has(capability)) {
|
|
5987
|
+
throw new Error(`Native capability ${capability}: duplicate structure`);
|
|
5988
|
+
}
|
|
5989
|
+
this.capabilityOwners.set(
|
|
5990
|
+
capability,
|
|
5991
|
+
this.owners.has(fn) ? `factory:${this.owners.get(fn)}` : this.resource.url
|
|
5992
|
+
);
|
|
5993
|
+
}
|
|
5922
5994
|
text(node) {
|
|
5923
5995
|
return this.resource.source.slice(node.start, node.end);
|
|
5924
5996
|
}
|
|
@@ -6181,11 +6253,13 @@ function modelCompletion(plan) {
|
|
|
6181
6253
|
throw new Error("Native capability model-completion: native update completion is unavailable");
|
|
6182
6254
|
}
|
|
6183
6255
|
plan.capabilities.push("model-completion");
|
|
6256
|
+
plan.claimCapability("model-completion", fn);
|
|
6184
6257
|
}
|
|
6185
6258
|
function analyzeNativeSubmissionTransforms(resources) {
|
|
6186
6259
|
const plans = resources.map((resource) => new ResourcePlan(resource));
|
|
6187
6260
|
for (const plan of plans) {
|
|
6188
6261
|
for (const fn of plan.functions) {
|
|
6262
|
+
const before = plan.capabilities.length;
|
|
6189
6263
|
if (inputPattern(plan, fn, ["submitTarget", "isResponseInProgress", "options"])) {
|
|
6190
6264
|
submitGuard(plan, fn);
|
|
6191
6265
|
}
|
|
@@ -6219,6 +6293,9 @@ function analyzeNativeSubmissionTransforms(resources) {
|
|
|
6219
6293
|
"attachments"
|
|
6220
6294
|
])
|
|
6221
6295
|
)) firstTurn(plan, fn);
|
|
6296
|
+
for (const capability of plan.capabilities.slice(before)) {
|
|
6297
|
+
plan.claimCapability(capability, fn);
|
|
6298
|
+
}
|
|
6222
6299
|
}
|
|
6223
6300
|
if (plan.resource.source.includes("onSelectModelOption")) modelCompletion(plan);
|
|
6224
6301
|
}
|
|
@@ -6233,7 +6310,11 @@ function analyzeNativeSubmissionTransforms(resources) {
|
|
|
6233
6310
|
"first-turn"
|
|
6234
6311
|
];
|
|
6235
6312
|
for (const capability of required) {
|
|
6236
|
-
one(
|
|
6313
|
+
one([
|
|
6314
|
+
...new Set(plans.flatMap(
|
|
6315
|
+
(plan) => plan.capabilities.filter((c) => c === capability).map((c) => plan.capabilityOwners.get(c) ?? plan.resource.url)
|
|
6316
|
+
))
|
|
6317
|
+
], capability);
|
|
6237
6318
|
}
|
|
6238
6319
|
const transforms = plans.filter((plan) => plan.edits.length > 0).map((plan) => {
|
|
6239
6320
|
const sha256 = hash(plan.resource.source);
|
|
@@ -6243,7 +6324,7 @@ function analyzeNativeSubmissionTransforms(resources) {
|
|
|
6243
6324
|
return Object.freeze({
|
|
6244
6325
|
url: plan.resource.url,
|
|
6245
6326
|
sha256,
|
|
6246
|
-
requiredForDocumentReady: plan.capabilities.includes("submit-guard"),
|
|
6327
|
+
requiredForDocumentReady: plan.capabilities.includes("submit-guard") && plan.owners.size === 0,
|
|
6247
6328
|
transform: (observed) => {
|
|
6248
6329
|
if (hash(observed) !== sha256) {
|
|
6249
6330
|
throw new Error(`Native resource changed after capability discovery: ${plan.resource.url}`);
|
|
@@ -7233,7 +7314,9 @@ export {
|
|
|
7233
7314
|
member,
|
|
7234
7315
|
nativeAgentTaskTransforms,
|
|
7235
7316
|
nativeBrowserSessionTransforms,
|
|
7317
|
+
nativeChunkPaths,
|
|
7236
7318
|
nativeLazyRequestSource,
|
|
7319
|
+
nativeModuleFactories,
|
|
7237
7320
|
nativeSideChatTransforms,
|
|
7238
7321
|
one,
|
|
7239
7322
|
parseNativeSource,
|