@ai-sdk/harness-opencode 1.0.165 → 1.0.167
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/CHANGELOG.md +21 -0
- package/README.md +9 -20
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24 -146
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/opencode-harness.ts +34 -239
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/harness-opencode",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.167",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@ai-sdk/harness": "1.0.
|
|
30
|
-
"@ai-sdk/provider": "4.0.
|
|
31
|
-
"@ai-sdk/provider-utils": "5.0.
|
|
29
|
+
"@ai-sdk/harness": "1.0.165",
|
|
30
|
+
"@ai-sdk/provider": "4.0.34",
|
|
31
|
+
"@ai-sdk/provider-utils": "5.0.69",
|
|
32
32
|
"ws": "8.21.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
package/src/opencode-harness.ts
CHANGED
|
@@ -12,7 +12,6 @@ import {
|
|
|
12
12
|
type HarnessV1CredentialForwarding,
|
|
13
13
|
type HarnessV1DebugConfig,
|
|
14
14
|
type HarnessV1MintBridgeTokenCallback,
|
|
15
|
-
type HarnessV1NetworkSandboxSession,
|
|
16
15
|
type HarnessV1PermissionMode,
|
|
17
16
|
type HarnessV1PromptControl,
|
|
18
17
|
type HarnessV1ResponseFormat,
|
|
@@ -30,6 +29,7 @@ import {
|
|
|
30
29
|
classifyDiskLog,
|
|
31
30
|
resolveSandboxCredentialEnvironment,
|
|
32
31
|
createBridgeToken,
|
|
32
|
+
createBridgeWebSocketConnect,
|
|
33
33
|
experimental_createBridgeUserMessageSubmitter,
|
|
34
34
|
createBridgeErrorHandler,
|
|
35
35
|
createBridgeStartupError,
|
|
@@ -38,18 +38,19 @@ import {
|
|
|
38
38
|
getRestrictedSandboxSession,
|
|
39
39
|
extractHarnessPromptContent,
|
|
40
40
|
markBridgeStarting,
|
|
41
|
+
resolveBridgeEndpoint,
|
|
42
|
+
resolveBridgePort,
|
|
41
43
|
resolveSandboxHomeDir,
|
|
42
44
|
SandboxChannel,
|
|
43
45
|
shellQuote,
|
|
46
|
+
validateBridgeSandboxSettings,
|
|
44
47
|
warnCredentialBrokeringUnavailable,
|
|
45
48
|
waitForBridgeReady,
|
|
46
|
-
withBridgeToken,
|
|
47
49
|
writeSkills,
|
|
48
50
|
type SandboxChannelReconnectOptions,
|
|
49
51
|
type WriteSkillsResult,
|
|
50
52
|
} from '@ai-sdk/harness/utils';
|
|
51
53
|
import {
|
|
52
|
-
safeParseJSON,
|
|
53
54
|
tool,
|
|
54
55
|
type Experimental_SandboxProcess,
|
|
55
56
|
type Experimental_SandboxSession as SandboxSession,
|
|
@@ -293,18 +294,13 @@ export function createOpenCode(
|
|
|
293
294
|
const toolSafeSandboxSession =
|
|
294
295
|
getRestrictedSandboxSession(sandboxSession);
|
|
295
296
|
const sandboxId = 'id' in sandboxSession ? sandboxSession.id : undefined;
|
|
296
|
-
|
|
297
|
+
validateBridgeSandboxSettings({
|
|
297
298
|
sandboxSession,
|
|
298
|
-
|
|
299
|
-
|
|
299
|
+
sandboxId,
|
|
300
|
+
portOverride: settings.port,
|
|
301
|
+
portEndpointOverride: settings.portEndpoint,
|
|
302
|
+
mintBridgeToken: settings.mintBridgeToken,
|
|
300
303
|
});
|
|
301
|
-
if (settings.mintBridgeToken != null && sandboxId == null) {
|
|
302
|
-
throw new HarnessCapabilityUnsupportedError({
|
|
303
|
-
harnessId: 'opencode',
|
|
304
|
-
message:
|
|
305
|
-
'The OpenCode harness cannot use `mintBridgeToken` with a sandbox session that does not expose an id.',
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
304
|
const lifecycleState = startOpts.continueFrom ?? startOpts.resumeFrom;
|
|
309
305
|
const isResume = lifecycleState != null;
|
|
310
306
|
const isContinue = startOpts.continueFrom != null;
|
|
@@ -447,26 +443,23 @@ export function createOpenCode(
|
|
|
447
443
|
|
|
448
444
|
if (coords) {
|
|
449
445
|
try {
|
|
450
|
-
const
|
|
446
|
+
const attachEndpoint = await resolveBridgeEndpoint({
|
|
451
447
|
sandboxSession,
|
|
452
|
-
override: settings.portEndpoint,
|
|
453
448
|
port: coords.port,
|
|
454
|
-
|
|
455
|
-
const attachEndpoint = withBridgeToken({
|
|
456
|
-
endpoint,
|
|
449
|
+
portEndpointOverride: settings.portEndpoint,
|
|
457
450
|
token: coords.token,
|
|
458
451
|
});
|
|
459
452
|
let supportsUserMessageResponses = false;
|
|
460
453
|
const attachChannel: OpenCodeChannel = new SandboxChannel({
|
|
461
|
-
connect: ({
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
454
|
+
connect: createBridgeWebSocketConnect({
|
|
455
|
+
endpoint: attachEndpoint,
|
|
456
|
+
webSocketConstructor: WebSocket,
|
|
457
|
+
onHello: hello => {
|
|
458
|
+
supportsUserMessageResponses =
|
|
459
|
+
hello.capabilities?.experimental_userMessageResponses ===
|
|
460
|
+
true;
|
|
461
|
+
},
|
|
462
|
+
}),
|
|
470
463
|
outboundSchema: outboundMessageSchema,
|
|
471
464
|
initialLastSeenEventId: coords.lastSeenEventId,
|
|
472
465
|
onDiagnostic,
|
|
@@ -519,7 +512,7 @@ export function createOpenCode(
|
|
|
519
512
|
|
|
520
513
|
const port = resolveBridgePort({
|
|
521
514
|
sandboxSession,
|
|
522
|
-
|
|
515
|
+
portOverride: settings.port,
|
|
523
516
|
});
|
|
524
517
|
const token =
|
|
525
518
|
settings.mintBridgeToken == null
|
|
@@ -624,24 +617,25 @@ export function createOpenCode(
|
|
|
624
617
|
});
|
|
625
618
|
void drainBridgeProcessStream(proc.stdout);
|
|
626
619
|
|
|
627
|
-
const
|
|
620
|
+
const bridgeEndpoint = await resolveBridgeEndpoint({
|
|
628
621
|
sandboxSession,
|
|
629
|
-
override: settings.portEndpoint,
|
|
630
622
|
port: boundPort,
|
|
623
|
+
portEndpointOverride: settings.portEndpoint,
|
|
624
|
+
token,
|
|
631
625
|
});
|
|
632
|
-
const bridgeEndpoint = withBridgeToken({ endpoint, token });
|
|
633
626
|
let supportsUserMessageResponses = false;
|
|
634
627
|
|
|
635
628
|
const channel: OpenCodeChannel = new SandboxChannel({
|
|
636
|
-
connect: ({
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
}
|
|
629
|
+
connect: createBridgeWebSocketConnect({
|
|
630
|
+
endpoint: bridgeEndpoint,
|
|
631
|
+
webSocketConstructor: WebSocket,
|
|
632
|
+
openRetryTimeoutMs: timeoutMs,
|
|
633
|
+
startAbortSignal: startOpts.abortSignal,
|
|
634
|
+
onHello: hello => {
|
|
635
|
+
supportsUserMessageResponses =
|
|
636
|
+
hello.capabilities?.experimental_userMessageResponses === true;
|
|
637
|
+
},
|
|
638
|
+
}),
|
|
645
639
|
outboundSchema: outboundMessageSchema,
|
|
646
640
|
onDiagnostic,
|
|
647
641
|
onBridgeError,
|
|
@@ -707,205 +701,6 @@ function resolveInactiveBuiltinToolNames(
|
|
|
707
701
|
);
|
|
708
702
|
}
|
|
709
703
|
|
|
710
|
-
function resolveBridgePort({
|
|
711
|
-
sandboxSession,
|
|
712
|
-
override,
|
|
713
|
-
}: {
|
|
714
|
-
sandboxSession: HarnessV1NetworkSandboxSession | SandboxSession;
|
|
715
|
-
override: number | undefined;
|
|
716
|
-
}): number {
|
|
717
|
-
if (override !== undefined) return override;
|
|
718
|
-
if ('ports' in sandboxSession && sandboxSession.ports.length > 0) {
|
|
719
|
-
return sandboxSession.ports[0];
|
|
720
|
-
}
|
|
721
|
-
throw new HarnessCapabilityUnsupportedError({
|
|
722
|
-
harnessId: 'opencode',
|
|
723
|
-
message:
|
|
724
|
-
'The OpenCode harness needs a TCP port exposed by the sandbox. ' +
|
|
725
|
-
'Create the sandbox with `ports: [<port>]` or pass `createOpenCode({ port })`.',
|
|
726
|
-
});
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
function validateBasicSandboxSettings({
|
|
730
|
-
sandboxSession,
|
|
731
|
-
port,
|
|
732
|
-
portEndpoint,
|
|
733
|
-
}: {
|
|
734
|
-
sandboxSession: HarnessV1NetworkSandboxSession | SandboxSession;
|
|
735
|
-
port: number | undefined;
|
|
736
|
-
portEndpoint: HarnessV1PortEndpoint | undefined;
|
|
737
|
-
}): void {
|
|
738
|
-
if ('getPortEndpoint' in sandboxSession) return;
|
|
739
|
-
if (port == null) {
|
|
740
|
-
throw new HarnessCapabilityUnsupportedError({
|
|
741
|
-
harnessId: 'opencode',
|
|
742
|
-
message:
|
|
743
|
-
'The OpenCode harness requires an explicit `port` when using a basic sandbox session.',
|
|
744
|
-
});
|
|
745
|
-
}
|
|
746
|
-
if (portEndpoint == null) {
|
|
747
|
-
throw new HarnessCapabilityUnsupportedError({
|
|
748
|
-
harnessId: 'opencode',
|
|
749
|
-
message:
|
|
750
|
-
'The OpenCode harness requires an explicit `portEndpoint` when using a basic sandbox session.',
|
|
751
|
-
});
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
async function resolveBridgeEndpoint({
|
|
756
|
-
sandboxSession,
|
|
757
|
-
override,
|
|
758
|
-
port,
|
|
759
|
-
}: {
|
|
760
|
-
sandboxSession: HarnessV1NetworkSandboxSession | SandboxSession;
|
|
761
|
-
override: HarnessV1PortEndpoint | undefined;
|
|
762
|
-
port: number;
|
|
763
|
-
}): Promise<HarnessV1PortEndpoint> {
|
|
764
|
-
if (override != null) return override;
|
|
765
|
-
if ('getPortEndpoint' in sandboxSession) {
|
|
766
|
-
return sandboxSession.getPortEndpoint({ port, protocol: 'ws' });
|
|
767
|
-
}
|
|
768
|
-
throw new HarnessCapabilityUnsupportedError({
|
|
769
|
-
harnessId: 'opencode',
|
|
770
|
-
message:
|
|
771
|
-
'The OpenCode harness requires an explicit `portEndpoint` when using a basic sandbox session.',
|
|
772
|
-
});
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
function openWebSocket({
|
|
776
|
-
endpoint,
|
|
777
|
-
helloTimeoutMs,
|
|
778
|
-
abortSignal,
|
|
779
|
-
onHello,
|
|
780
|
-
}: {
|
|
781
|
-
endpoint: HarnessV1PortEndpoint;
|
|
782
|
-
helloTimeoutMs: number;
|
|
783
|
-
abortSignal: AbortSignal;
|
|
784
|
-
onHello(supportsUserMessageResponses: boolean): void;
|
|
785
|
-
}): Promise<WebSocket> {
|
|
786
|
-
return new Promise((resolve, reject) => {
|
|
787
|
-
const abortReason = () =>
|
|
788
|
-
abortSignal.reason ?? new Error('WebSocket connection aborted');
|
|
789
|
-
if (abortSignal.aborted) {
|
|
790
|
-
reject(abortReason());
|
|
791
|
-
return;
|
|
792
|
-
}
|
|
793
|
-
|
|
794
|
-
const ws = new WebSocket(endpoint.url, {
|
|
795
|
-
headers: endpoint.headers == null ? undefined : { ...endpoint.headers },
|
|
796
|
-
});
|
|
797
|
-
let opened = false;
|
|
798
|
-
let receivedHello = false;
|
|
799
|
-
let settled = false;
|
|
800
|
-
let helloTimer: ReturnType<typeof setTimeout> | undefined;
|
|
801
|
-
let onAbort: (() => void) | undefined;
|
|
802
|
-
const cleanup = () => {
|
|
803
|
-
clearTimeout(helloTimer);
|
|
804
|
-
ws.off('open', onOpen);
|
|
805
|
-
ws.off('message', onMessage);
|
|
806
|
-
ws.off('close', onClose);
|
|
807
|
-
ws.off('error', onError);
|
|
808
|
-
if (onAbort != null) {
|
|
809
|
-
abortSignal.removeEventListener('abort', onAbort);
|
|
810
|
-
}
|
|
811
|
-
};
|
|
812
|
-
const settle = (error?: unknown) => {
|
|
813
|
-
if (settled) return;
|
|
814
|
-
settled = true;
|
|
815
|
-
cleanup();
|
|
816
|
-
if (error == null) {
|
|
817
|
-
resolve(ws);
|
|
818
|
-
} else {
|
|
819
|
-
const suppressError = () => {};
|
|
820
|
-
const socketWithOnce = ws as WebSocket & {
|
|
821
|
-
once?: (event: string, listener: () => void) => void;
|
|
822
|
-
};
|
|
823
|
-
socketWithOnce.once?.('error', suppressError);
|
|
824
|
-
try {
|
|
825
|
-
ws.terminate();
|
|
826
|
-
} catch {
|
|
827
|
-
try {
|
|
828
|
-
ws.close();
|
|
829
|
-
} catch {
|
|
830
|
-
// best-effort
|
|
831
|
-
}
|
|
832
|
-
}
|
|
833
|
-
reject(error);
|
|
834
|
-
}
|
|
835
|
-
};
|
|
836
|
-
const tryResolve = () => {
|
|
837
|
-
if (opened && receivedHello) settle();
|
|
838
|
-
};
|
|
839
|
-
const onOpen = () => {
|
|
840
|
-
opened = true;
|
|
841
|
-
tryResolve();
|
|
842
|
-
};
|
|
843
|
-
const onMessage = (raw: unknown) => {
|
|
844
|
-
void (async () => {
|
|
845
|
-
const parsed = await safeParseJSON({
|
|
846
|
-
text: webSocketMessageToString(raw),
|
|
847
|
-
});
|
|
848
|
-
if (!parsed.success) return;
|
|
849
|
-
const value = parsed.value;
|
|
850
|
-
if (
|
|
851
|
-
typeof value !== 'object' ||
|
|
852
|
-
value == null ||
|
|
853
|
-
Array.isArray(value) ||
|
|
854
|
-
(value as { type?: unknown }).type !== 'bridge-hello'
|
|
855
|
-
) {
|
|
856
|
-
return;
|
|
857
|
-
}
|
|
858
|
-
const capabilities = (
|
|
859
|
-
value as {
|
|
860
|
-
capabilities?: { experimental_userMessageResponses?: unknown };
|
|
861
|
-
}
|
|
862
|
-
).capabilities;
|
|
863
|
-
onHello(capabilities?.experimental_userMessageResponses === true);
|
|
864
|
-
receivedHello = true;
|
|
865
|
-
tryResolve();
|
|
866
|
-
})();
|
|
867
|
-
};
|
|
868
|
-
const onClose = () =>
|
|
869
|
-
settle(new Error('OpenCode bridge closed before sending bridge-hello.'));
|
|
870
|
-
const onError = (err: Error) => {
|
|
871
|
-
settle(err);
|
|
872
|
-
};
|
|
873
|
-
onAbort = () => settle(abortReason());
|
|
874
|
-
ws.on('open', onOpen);
|
|
875
|
-
ws.on('message', onMessage);
|
|
876
|
-
ws.on('close', onClose);
|
|
877
|
-
ws.on('error', onError);
|
|
878
|
-
abortSignal.addEventListener('abort', onAbort, { once: true });
|
|
879
|
-
if (abortSignal.aborted) {
|
|
880
|
-
onAbort();
|
|
881
|
-
return;
|
|
882
|
-
}
|
|
883
|
-
helloTimer = setTimeout(
|
|
884
|
-
() =>
|
|
885
|
-
settle(
|
|
886
|
-
new Error(
|
|
887
|
-
`OpenCode bridge did not send bridge-hello within ${helloTimeoutMs}ms.`,
|
|
888
|
-
),
|
|
889
|
-
),
|
|
890
|
-
helloTimeoutMs,
|
|
891
|
-
);
|
|
892
|
-
helloTimer.unref?.();
|
|
893
|
-
});
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
function webSocketMessageToString(raw: unknown): string {
|
|
897
|
-
if (typeof raw === 'string') return raw;
|
|
898
|
-
if (Buffer.isBuffer(raw)) return raw.toString('utf8');
|
|
899
|
-
if (Array.isArray(raw)) return Buffer.concat(raw).toString('utf8');
|
|
900
|
-
if (raw instanceof ArrayBuffer) return Buffer.from(raw).toString('utf8');
|
|
901
|
-
if (ArrayBuffer.isView(raw)) {
|
|
902
|
-
return Buffer.from(raw.buffer, raw.byteOffset, raw.byteLength).toString(
|
|
903
|
-
'utf8',
|
|
904
|
-
);
|
|
905
|
-
}
|
|
906
|
-
return String(raw);
|
|
907
|
-
}
|
|
908
|
-
|
|
909
704
|
function createSession({
|
|
910
705
|
sessionId,
|
|
911
706
|
channel,
|