Skip to content

Commit bd4ac1a

Browse files
committed
Fix OpenCode event ingestion compatibility
1 parent ea20e80 commit bd4ac1a

2 files changed

Lines changed: 348 additions & 14 deletions

File tree

‎apps/server/src/provider/Layers/OpenCodeAdapter.test.ts‎

Lines changed: 249 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,14 @@ const runtimeMock = {
6060
revertCalls: [] as Array<{ sessionID: string; messageID?: string }>,
6161
promptCalls: [] as Array<unknown>,
6262
promptAsyncError: null as Error | null,
63+
globalEventError: null as unknown,
6364
closeError: null as Error | null,
6465
messages: [] as MessageEntry[],
6566
subscribedEvents: [] as unknown[],
67+
subscribedEventDirectory: process.cwd(),
68+
keepSubscriptionOpen: false,
69+
globalEventCalls: 0,
70+
legacySubscribeCalls: 0,
6671
},
6772
reset() {
6873
this.state.startCalls.length = 0;
@@ -73,9 +78,14 @@ const runtimeMock = {
7378
this.state.revertCalls.length = 0;
7479
this.state.promptCalls.length = 0;
7580
this.state.promptAsyncError = null;
81+
this.state.globalEventError = null;
7682
this.state.closeError = null;
7783
this.state.messages = [];
7884
this.state.subscribedEvents = [];
85+
this.state.subscribedEventDirectory = process.cwd();
86+
this.state.keepSubscriptionOpen = false;
87+
this.state.globalEventCalls = 0;
88+
this.state.legacySubscribeCalls = 0;
7989
},
8090
};
8191

@@ -159,13 +169,52 @@ const OpenCodeRuntimeTestDouble: OpenCodeRuntimeShape = {
159169
},
160170
},
161171
event: {
162-
subscribe: async () => ({
163-
stream: (async function* () {
164-
for (const event of runtimeMock.state.subscribedEvents) {
165-
yield event;
166-
}
167-
})(),
168-
}),
172+
subscribe: async (_parameters?: unknown, options?: { readonly signal?: AbortSignal }) => {
173+
runtimeMock.state.legacySubscribeCalls += 1;
174+
return {
175+
stream: (async function* () {
176+
let index = 0;
177+
while (options?.signal?.aborted !== true) {
178+
if (index < runtimeMock.state.subscribedEvents.length) {
179+
yield runtimeMock.state.subscribedEvents[index];
180+
index += 1;
181+
continue;
182+
}
183+
if (!runtimeMock.state.keepSubscriptionOpen) {
184+
return;
185+
}
186+
await Effect.runPromise(Effect.sleep("5 millis"));
187+
}
188+
})(),
189+
};
190+
},
191+
},
192+
global: {
193+
event: async (options?: { readonly signal?: AbortSignal }) => {
194+
runtimeMock.state.globalEventCalls += 1;
195+
if (runtimeMock.state.globalEventError) {
196+
throw runtimeMock.state.globalEventError;
197+
}
198+
return {
199+
stream: (async function* () {
200+
let index = 0;
201+
while (options?.signal?.aborted !== true) {
202+
if (index < runtimeMock.state.subscribedEvents.length) {
203+
yield {
204+
directory: runtimeMock.state.subscribedEventDirectory,
205+
payload: runtimeMock.state.subscribedEvents[index],
206+
};
207+
index += 1;
208+
continue;
209+
}
210+
if (!runtimeMock.state.keepSubscriptionOpen) {
211+
return;
212+
}
213+
await Effect.runPromise(Effect.sleep("5 millis"));
214+
}
215+
})(),
216+
};
217+
},
169218
},
170219
}) as unknown as ReturnType<OpenCodeRuntimeShape["createOpenCodeSdkClient"]>,
171220
loadOpenCodeInventory: () =>
@@ -395,6 +444,199 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
395444
}),
396445
);
397446

447+
it.effect("uses global wrapped events for assistant message projection", () =>
448+
Effect.gen(function* () {
449+
const adapter = yield* OpenCodeAdapter;
450+
const threadId = asThreadId("thread-global-assistant-message");
451+
runtimeMock.state.subscribedEvents = [
452+
{
453+
type: "message.updated",
454+
properties: {
455+
sessionID: "http://127.0.0.1:9999/session",
456+
info: { id: "msg-global-assistant", role: "assistant" },
457+
},
458+
},
459+
{
460+
type: "message.part.updated",
461+
properties: {
462+
sessionID: "http://127.0.0.1:9999/session",
463+
part: {
464+
id: "part-global-assistant",
465+
sessionID: "http://127.0.0.1:9999/session",
466+
messageID: "msg-global-assistant",
467+
type: "text",
468+
text: "Hello from global events",
469+
time: { start: 1, end: 2 },
470+
},
471+
time: 2,
472+
},
473+
},
474+
];
475+
const eventsFiber = yield* adapter.streamEvents.pipe(
476+
Stream.filter((event) => event.threadId === threadId),
477+
Stream.take(4),
478+
Stream.runCollect,
479+
Effect.forkChild,
480+
);
481+
482+
yield* adapter.startSession({
483+
provider: ProviderDriverKind.make("opencode"),
484+
threadId,
485+
runtimeMode: "full-access",
486+
});
487+
488+
const events = Array.from(yield* Fiber.join(eventsFiber).pipe(Effect.timeout("1 second")));
489+
assert.equal(runtimeMock.state.globalEventCalls > 0, true);
490+
assert.equal(runtimeMock.state.legacySubscribeCalls, 0);
491+
assert.deepEqual(
492+
events.map((event) => event.type),
493+
["session.started", "thread.started", "content.delta", "item.completed"],
494+
);
495+
assert.equal(
496+
events.some(
497+
(event) =>
498+
event.type === "content.delta" && event.payload.delta === "Hello from global events",
499+
),
500+
true,
501+
);
502+
}),
503+
);
504+
505+
it.effect("ignores OpenCode global events from a different directory", () =>
506+
Effect.gen(function* () {
507+
const adapter = yield* OpenCodeAdapter;
508+
const threadId = asThreadId("thread-global-other-directory");
509+
runtimeMock.state.keepSubscriptionOpen = true;
510+
runtimeMock.state.subscribedEventDirectory = "/tmp/not-this-project";
511+
const eventsFiber = yield* adapter.streamEvents.pipe(
512+
Stream.filter((event) => event.threadId === threadId),
513+
Stream.take(3),
514+
Stream.runCollect,
515+
Effect.forkChild,
516+
);
517+
518+
yield* adapter.startSession({
519+
provider: ProviderDriverKind.make("opencode"),
520+
threadId,
521+
runtimeMode: "full-access",
522+
});
523+
yield* adapter.sendTurn({
524+
threadId,
525+
input: "Finish cleanly",
526+
modelSelection: {
527+
instanceId: ProviderInstanceId.make("opencode"),
528+
model: "openai/gpt-5",
529+
},
530+
});
531+
runtimeMock.state.subscribedEvents.push({
532+
type: "session.status",
533+
properties: {
534+
sessionID: "http://127.0.0.1:9999/session",
535+
status: { type: "idle" },
536+
},
537+
});
538+
runtimeMock.state.keepSubscriptionOpen = false;
539+
540+
const events = Array.from(yield* Fiber.join(eventsFiber).pipe(Effect.timeout("1 second")));
541+
assert.deepEqual(
542+
events.map((event) => event.type),
543+
["session.started", "thread.started", "turn.started"],
544+
);
545+
}),
546+
);
547+
548+
it.effect("ignores malformed OpenCode global payloads before idle completion", () =>
549+
Effect.gen(function* () {
550+
const adapter = yield* OpenCodeAdapter;
551+
const threadId = asThreadId("thread-global-malformed-payload");
552+
runtimeMock.state.keepSubscriptionOpen = true;
553+
const eventsFiber = yield* adapter.streamEvents.pipe(
554+
Stream.filter((event) => event.threadId === threadId),
555+
Stream.take(4),
556+
Stream.runCollect,
557+
Effect.forkChild,
558+
);
559+
560+
yield* adapter.startSession({
561+
provider: ProviderDriverKind.make("opencode"),
562+
threadId,
563+
runtimeMode: "full-access",
564+
});
565+
const started = yield* adapter.sendTurn({
566+
threadId,
567+
input: "Finish cleanly",
568+
modelSelection: {
569+
instanceId: ProviderInstanceId.make("opencode"),
570+
model: "openai/gpt-5",
571+
},
572+
});
573+
runtimeMock.state.subscribedEvents.push(
574+
{ type: "session.status" },
575+
{
576+
type: "session.status",
577+
properties: {
578+
sessionID: "http://127.0.0.1:9999/session",
579+
status: { type: "idle" },
580+
},
581+
},
582+
);
583+
runtimeMock.state.keepSubscriptionOpen = false;
584+
585+
const events = Array.from(yield* Fiber.join(eventsFiber).pipe(Effect.timeout("1 second")));
586+
assert.deepEqual(
587+
events.map((event) => event.type),
588+
["session.started", "thread.started", "turn.started", "turn.completed"],
589+
);
590+
assert.equal(events.at(-1)?.turnId, started.turnId);
591+
}),
592+
);
593+
594+
it.effect("falls back to legacy event.subscribe when global.event is unavailable", () =>
595+
Effect.gen(function* () {
596+
const adapter = yield* OpenCodeAdapter;
597+
const threadId = asThreadId("thread-global-event-fallback");
598+
runtimeMock.state.keepSubscriptionOpen = true;
599+
runtimeMock.state.globalEventError = { response: { status: 404 } };
600+
const eventsFiber = yield* adapter.streamEvents.pipe(
601+
Stream.filter((event) => event.threadId === threadId),
602+
Stream.take(4),
603+
Stream.runCollect,
604+
Effect.forkChild,
605+
);
606+
607+
yield* adapter.startSession({
608+
provider: ProviderDriverKind.make("opencode"),
609+
threadId,
610+
runtimeMode: "full-access",
611+
});
612+
const started = yield* adapter.sendTurn({
613+
threadId,
614+
input: "Finish through legacy events",
615+
modelSelection: {
616+
instanceId: ProviderInstanceId.make("opencode"),
617+
model: "openai/gpt-5",
618+
},
619+
});
620+
runtimeMock.state.subscribedEvents.push({
621+
type: "session.status",
622+
properties: {
623+
sessionID: "http://127.0.0.1:9999/session",
624+
status: { type: "idle" },
625+
},
626+
});
627+
runtimeMock.state.keepSubscriptionOpen = false;
628+
629+
const events = Array.from(yield* Fiber.join(eventsFiber).pipe(Effect.timeout("1 second")));
630+
assert.equal(runtimeMock.state.globalEventCalls, 1);
631+
assert.equal(runtimeMock.state.legacySubscribeCalls, 1);
632+
assert.deepEqual(
633+
events.map((event) => event.type),
634+
["session.started", "thread.started", "turn.started", "turn.completed"],
635+
);
636+
assert.equal(events.at(-1)?.turnId, started.turnId);
637+
}),
638+
);
639+
398640
it.effect("passes agent and variant options for the adapter's bound custom instance id", () => {
399641
const instanceId = ProviderInstanceId.make("opencode_zen");
400642
const adapterLayer = Layer.effect(

0 commit comments

Comments
 (0)