Skip to content

Commit a2ca40a

Browse files
feat(server): OpenCode 2 text generation, MCP injection and restart reconciliation (#14386)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent b08e051 commit a2ca40a

35 files changed

Lines changed: 3249 additions & 214 deletions

‎apps/server/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@opencode-ai/sdk": "^1.3.15",
3838
"@opencode/client": "2.0.18",
3939
"@opencode/protocol": "2.0.18",
40+
"@opencode/schema": "2.0.18",
4041
"diff": "8.0.3",
4142
"effect": "catalog:",
4243
"jose": "catalog:",

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts‎

Lines changed: 647 additions & 23 deletions
Large diffs are not rendered by default.

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.testkit.ts‎

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ const operationOf = (
6767
if (method === "POST" && path === "/api/session") return { type: "session.create", input: body };
6868
if (method === "GET" && path === "/api/session/active") return { type: "session.active" };
6969
if (method === "GET" && path === "/api/command") return { type: "command.list", input: query };
70+
const mcp = /^\/api\/experimental\/mcp\/([^/]+)$/.exec(path);
71+
if (mcp !== null && method === "PUT") {
72+
return { type: "mcp.add", input: { server: mcp[1], ...query, ...(body as object) } };
73+
}
74+
if (mcp !== null && method === "DELETE") {
75+
return { type: "mcp.remove", input: { server: mcp[1], ...query } };
76+
}
77+
const entry = /^\/api\/experimental\/session\/([^/]+)\/instructions\/entries\/([^/]+)$/.exec(
78+
path,
79+
);
80+
if (entry !== null && method === "PUT") {
81+
return {
82+
type: "session.instructions.entry.put",
83+
input: { sessionID: entry[1], key: entry[2], ...(body as object) },
84+
};
85+
}
7086
if (method === "GET" && path === "/api/skill") return { type: "skill.list", input: query };
7187
if (session !== null) {
7288
const [, sessionID, rest = ""] = session;
@@ -76,6 +92,7 @@ const operationOf = (
7692
);
7793
const input = { sessionID, ...query, ...Object.fromEntries(fields) };
7894
if (method === "GET" && rest === "") return { type: "session.get", input };
95+
if (method === "DELETE" && rest === "") return { type: "session.remove", input };
7996
if (method === "PATCH" && rest === "") return { type: "session.update", input };
8097
if (method === "POST" && rest === "/prompt") return { type: "session.prompt", input };
8198
if (method === "POST" && rest === "/command") return { type: "session.command", input };
@@ -118,14 +135,23 @@ const operationOf = (
118135
return { type: `${method} ${path}`, input: { ...query, body } };
119136
};
120137

121-
/** An `HttpClient` that answers every request from the transcript. */
138+
/**
139+
* An `HttpClient` that answers every request from the transcript. A successful
140+
* `runtime_exit` ends the event stream: the server has stopped. When entries
141+
* follow it, the server was restarted and the next `event.subscribe` opens its
142+
* new stream; a transcript that ends there stays down, so every later request
143+
* fails to connect as it would against a stopped server.
144+
*/
122145
const replayHttpClient = (
123146
controller: OpenCodeReplayController,
124147
replayGate: ProviderReplayGate | undefined,
125148
) =>
126149
HttpClient.make((request, url) =>
127150
Effect.tryPromise({
128151
try: async () => {
152+
if (controller.exited && controller.finished) {
153+
throw new Error("The replayed OpenCode server has exited.");
154+
}
129155
const raw =
130156
request.body._tag === "Uint8Array"
131157
? new TextDecoder().decode(request.body.body)
@@ -142,6 +168,7 @@ const replayHttpClient = (
142168
if (operation.type !== "event.subscribe") await controller.untilInboundDelivered();
143169
await controller.expectOutbound(operation);
144170
if (operation.type === "event.subscribe") {
171+
controller.exited = false;
145172
const encoder = new TextEncoder();
146173
const events = controller.events(undefined, replayGate?.beforeEmit);
147174
const frames = events[Symbol.asyncIterator]();
@@ -175,10 +202,18 @@ const replayHttpClient = (
175202
}).pipe(Effect.map((response) => HttpClientResponse.fromWeb(request, response))),
176203
);
177204

178-
/** The 2.x adapter over a replayed server, checking at scope close that the transcript ran out. */
179-
const makeReplayAdapter = (
205+
/**
206+
* An OpenCode 2 server that answers from `transcript`, checking at scope close
207+
* that the transcript ran out.
208+
*/
209+
export const replayServer = (
180210
transcript: ProviderReplayTranscript,
181-
options?: { readonly external?: boolean; readonly replayGate?: ProviderReplayGate },
211+
options?: {
212+
readonly external?: boolean;
213+
readonly replayGate?: ProviderReplayGate;
214+
/** Counts the connections currently lent out, as the server owner's borrowers. */
215+
readonly borrowers?: { current: number };
216+
},
182217
) =>
183218
Effect.gen(function* () {
184219
const controller = new OpenCodeReplayController(transcript);
@@ -200,11 +235,38 @@ const makeReplayAdapter = (
200235
version: transcript.version,
201236
external: options?.external ?? false,
202237
};
238+
const borrowers = options?.borrowers;
239+
return OpenCode2Server.OpenCode2Server.of({
240+
withConnection: (use) =>
241+
borrowers === undefined
242+
? use(connection)
243+
: Effect.acquireUseRelease(
244+
Effect.sync(() => {
245+
borrowers.current += 1;
246+
}),
247+
() => use(connection),
248+
() =>
249+
Effect.sync(() => {
250+
borrowers.current -= 1;
251+
}),
252+
),
253+
});
254+
});
255+
256+
/** The 2.x adapter over a replayed server. */
257+
const makeReplayAdapter = (
258+
transcript: ProviderReplayTranscript,
259+
options?: {
260+
readonly external?: boolean;
261+
readonly replayGate?: ProviderReplayGate;
262+
/** Counts the connections currently lent out, as the server owner's borrowers. */
263+
readonly borrowers?: { current: number };
264+
},
265+
) =>
266+
Effect.gen(function* () {
267+
const server = yield* replayServer(transcript, options);
203268
return yield* OpenCode2AdapterV2.make(ProviderInstanceId.make("opencode")).pipe(
204-
Effect.provideService(
205-
OpenCode2Server.OpenCode2Server,
206-
OpenCode2Server.OpenCode2Server.of({ withConnection: (use) => use(connection) }),
207-
),
269+
Effect.provideService(OpenCode2Server.OpenCode2Server, server),
208270
);
209271
});
210272

@@ -230,7 +292,7 @@ function makeRegistryLayer(
230292
*/
231293
export const openCode2ReplayRuntime = (
232294
entries: ReadonlyArray<ProviderReplayEntry>,
233-
options?: { readonly external?: boolean },
295+
options?: { readonly external?: boolean; readonly borrowers?: { current: number } },
234296
) =>
235297
Effect.gen(function* () {
236298
const adapter = yield* makeReplayAdapter(

0 commit comments

Comments
 (0)