-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathopencodeRuntime.ts
More file actions
555 lines (505 loc) · 18.9 KB
/
Copy pathopencodeRuntime.ts
File metadata and controls
555 lines (505 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import { pathToFileURL } from "node:url";
import type { ChatAttachment, ProviderApprovalDecision, RuntimeMode } from "@t3tools/contracts";
import {
createOpencodeClient,
type Agent,
type FilePartInput,
type OpencodeClient,
type PermissionRuleset,
type ProviderListResponse,
type QuestionAnswer,
type QuestionRequest,
} from "@opencode-ai/sdk/v2";
import * as Cause from "effect/Cause";
import * as Context from "effect/Context";
import * as Deferred from "effect/Deferred";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Ref from "effect/Ref";
import * as Result from "effect/Result";
import * as Scope from "effect/Scope";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import { isWindowsCommandNotFound } from "../processRunner.ts";
import { collectStreamAsString } from "./providerSnapshot.ts";
import * as NetService from "@t3tools/shared/Net";
const encodeUnknownJsonStringExit = Schema.encodeUnknownExit(Schema.UnknownFromJsonString);
const OPENCODE_EMPTY_CONFIG_CONTENT = "{}";
const OPENCODE_SERVER_READY_PREFIX = "opencode server listening";
const DEFAULT_OPENCODE_SERVER_TIMEOUT = Duration.seconds(5);
const DEFAULT_HOSTNAME = "127.0.0.1";
export interface OpenCodeServerProcess {
readonly url: string;
readonly exitCode: Effect.Effect<number, never>;
}
export interface OpenCodeServerConnection {
readonly url: string;
readonly exitCode: Effect.Effect<number, never> | null;
readonly external: boolean;
}
export class OpenCodeRuntimeError extends Schema.TaggedErrorClass<OpenCodeRuntimeError>()(
"OpenCodeRuntimeError",
{
operation: Schema.String,
detail: Schema.String,
cause: Schema.optional(Schema.Defect),
},
) {
override get message() {
return this.detail;
}
}
export const isOpenCodeRuntimeError = Schema.is(OpenCodeRuntimeError);
function encodeJsonStringForDiagnostics(input: unknown): string | undefined {
const result = encodeUnknownJsonStringExit(input);
return Exit.isSuccess(result) ? result.value : undefined;
}
export function openCodeRuntimeErrorDetail(cause: unknown): string {
if (isOpenCodeRuntimeError(cause)) return cause.detail;
if (cause instanceof Error && cause.message.trim().length > 0) return cause.message.trim();
if (cause && typeof cause === "object") {
// SDK v2 throws { response, request, error? } shapes — extract what's useful
const anyCause = cause as Record<string, unknown>;
const status = (anyCause.response as { status?: number } | undefined)?.status;
const body = anyCause.error ?? anyCause.data ?? anyCause.body;
const encodedBody = encodeJsonStringForDiagnostics(body ?? cause);
if (encodedBody) {
return `status=${status ?? "?"} body=${encodedBody}`;
}
}
return String(cause);
}
export const runOpenCodeSdk = <A>(
operation: string,
fn: () => Promise<A>,
): Effect.Effect<A, OpenCodeRuntimeError> =>
Effect.tryPromise({
try: fn,
catch: (cause) =>
new OpenCodeRuntimeError({ operation, detail: openCodeRuntimeErrorDetail(cause), cause }),
}).pipe(Effect.withSpan(`opencode.${operation}`));
export interface OpenCodeCommandResult {
readonly stdout: string;
readonly stderr: string;
readonly code: number;
}
export interface OpenCodeInventory {
readonly providerList: ProviderListResponse;
readonly agents: ReadonlyArray<Agent>;
}
export interface ParsedOpenCodeModelSlug {
readonly providerID: string;
readonly modelID: string;
}
export interface OpenCodeRuntimeShape {
/**
* Spawns a local OpenCode server process. Its lifetime is bound to the caller's
* `Scope.Scope` — the child is killed automatically when that scope closes.
* Consumers that want a long-lived server must create and hold a scope explicitly
* (see {@link Scope.make}) and close it when done.
*/
readonly startOpenCodeServerProcess: (input: {
readonly binaryPath: string;
readonly environment?: NodeJS.ProcessEnv;
readonly port?: number;
readonly hostname?: string;
readonly timeout?: Duration.Input;
}) => Effect.Effect<OpenCodeServerProcess, OpenCodeRuntimeError, Scope.Scope>;
/**
* Returns a handle to either an externally-managed OpenCode server (when
* `serverUrl` is provided — no lifetime is attached to the caller's scope) or a
* freshly spawned local server whose lifetime is bound to the caller's scope.
*/
readonly connectToOpenCodeServer: (input: {
readonly binaryPath: string;
readonly serverUrl?: string | null;
readonly environment?: NodeJS.ProcessEnv;
readonly port?: number;
readonly hostname?: string;
readonly timeout?: Duration.Input;
}) => Effect.Effect<OpenCodeServerConnection, OpenCodeRuntimeError, Scope.Scope>;
readonly runOpenCodeCommand: (input: {
readonly binaryPath: string;
readonly args: ReadonlyArray<string>;
readonly environment?: NodeJS.ProcessEnv;
}) => Effect.Effect<OpenCodeCommandResult, OpenCodeRuntimeError>;
readonly createOpenCodeSdkClient: (input: {
readonly baseUrl: string;
readonly directory: string;
readonly serverPassword?: string;
}) => OpencodeClient;
readonly loadOpenCodeInventory: (
client: OpencodeClient,
) => Effect.Effect<OpenCodeInventory, OpenCodeRuntimeError>;
}
function parseServerUrlFromOutput(output: string): string | null {
for (const line of output.split("\n")) {
if (!line.startsWith(OPENCODE_SERVER_READY_PREFIX)) {
continue;
}
const match = line.match(/on\s+(https?:\/\/[^\s]+)/);
return match?.[1] ?? null;
}
return null;
}
export function parseOpenCodeModelSlug(
slug: string | null | undefined,
): ParsedOpenCodeModelSlug | null {
if (typeof slug !== "string") {
return null;
}
const trimmed = slug.trim();
const separator = trimmed.indexOf("/");
if (separator <= 0 || separator === trimmed.length - 1) {
return null;
}
return {
providerID: trimmed.slice(0, separator),
modelID: trimmed.slice(separator + 1),
};
}
export function openCodeQuestionId(
index: number,
question: QuestionRequest["questions"][number],
): string {
const header = question.header
.trim()
.toLowerCase()
.replace(/[^a-z0-9_-]+/g, "-");
return header.length > 0 ? `question-${index}-${header}` : `question-${index}`;
}
export function toOpenCodeFileParts(input: {
readonly attachments: ReadonlyArray<ChatAttachment> | undefined;
readonly resolveAttachmentPath: (attachment: ChatAttachment) => string | null;
}): Array<FilePartInput> {
const parts: Array<FilePartInput> = [];
for (const attachment of input.attachments ?? []) {
const attachmentPath = input.resolveAttachmentPath(attachment);
if (!attachmentPath) {
continue;
}
parts.push({
type: "file",
mime: attachment.mimeType,
filename: attachment.name,
url: pathToFileURL(attachmentPath).href,
});
}
return parts;
}
export function buildOpenCodePermissionRules(runtimeMode: RuntimeMode): PermissionRuleset {
if (runtimeMode === "full-access") {
return [{ permission: "*", pattern: "*", action: "allow" }];
}
return [
{ permission: "*", pattern: "*", action: "ask" },
{ permission: "bash", pattern: "*", action: "ask" },
{ permission: "edit", pattern: "*", action: "ask" },
{ permission: "webfetch", pattern: "*", action: "ask" },
{ permission: "websearch", pattern: "*", action: "ask" },
{ permission: "codesearch", pattern: "*", action: "ask" },
{ permission: "external_directory", pattern: "*", action: "ask" },
{ permission: "doom_loop", pattern: "*", action: "ask" },
{ permission: "question", pattern: "*", action: "allow" },
];
}
export function toOpenCodePermissionReply(
decision: ProviderApprovalDecision,
): "once" | "always" | "reject" {
switch (decision) {
case "accept":
return "once";
case "acceptForSession":
return "always";
case "decline":
case "cancel":
default:
return "reject";
}
}
export function toOpenCodeQuestionAnswers(
request: QuestionRequest,
answers: Record<string, unknown>,
): Array<QuestionAnswer> {
return request.questions.map((question, index) => {
const raw =
answers[openCodeQuestionId(index, question)] ??
answers[question.header] ??
answers[question.question];
if (Array.isArray(raw)) {
return raw.filter((value): value is string => typeof value === "string");
}
if (typeof raw === "string") {
return raw.trim().length > 0 ? [raw] : [];
}
return [];
});
}
function ensureRuntimeError(
operation: OpenCodeRuntimeError["operation"],
detail: string,
cause: unknown,
): OpenCodeRuntimeError {
return isOpenCodeRuntimeError(cause)
? cause
: new OpenCodeRuntimeError({ operation, detail, cause });
}
const makeOpenCodeRuntime = Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const netService = yield* NetService.NetService;
const runOpenCodeCommand: OpenCodeRuntimeShape["runOpenCodeCommand"] = (input) =>
Effect.gen(function* () {
const child = yield* spawner.spawn(
ChildProcess.make(input.binaryPath, [...input.args], {
shell: process.platform === "win32",
env: input.environment ?? process.env,
}),
);
const [stdout, stderr, code] = yield* Effect.all(
[collectStreamAsString(child.stdout), collectStreamAsString(child.stderr), child.exitCode],
{ concurrency: "unbounded" },
);
const exitCode = Number(code);
if (isWindowsCommandNotFound(exitCode, stderr)) {
return yield* new OpenCodeRuntimeError({
operation: "runOpenCodeCommand",
detail: `spawn ${input.binaryPath} ENOENT`,
});
}
return {
stdout,
stderr,
code: exitCode,
} satisfies OpenCodeCommandResult;
}).pipe(
Effect.scoped,
Effect.mapError((cause) =>
ensureRuntimeError(
"runOpenCodeCommand",
`Failed to execute '${input.binaryPath} ${input.args.join(" ")}': ${openCodeRuntimeErrorDetail(cause)}`,
cause,
),
),
);
const startOpenCodeServerProcess: OpenCodeRuntimeShape["startOpenCodeServerProcess"] = (input) =>
Effect.gen(function* () {
// Bind this server's lifetime to the caller's scope. When the caller's
// scope closes, the spawned child is killed and all associated fibers
// are interrupted automatically — no `close()` method needed.
const runtimeScope = yield* Scope.Scope;
const hostname = input.hostname ?? DEFAULT_HOSTNAME;
const port =
input.port ??
(yield* netService.findAvailablePort(0).pipe(
Effect.mapError(
(cause) =>
new OpenCodeRuntimeError({
operation: "startOpenCodeServerProcess",
detail: `Failed to find available port: ${openCodeRuntimeErrorDetail(cause)}`,
cause,
}),
),
));
const timeout = Duration.fromInputUnsafe(input.timeout ?? DEFAULT_OPENCODE_SERVER_TIMEOUT);
const args = ["serve", `--hostname=${hostname}`, `--port=${port}`];
const child = yield* spawner
.spawn(
ChildProcess.make(input.binaryPath, args, {
detached: process.platform !== "win32",
shell: process.platform === "win32",
env: {
...(input.environment ?? process.env),
OPENCODE_CONFIG_CONTENT: OPENCODE_EMPTY_CONFIG_CONTENT,
},
}),
)
.pipe(
Effect.provideService(Scope.Scope, runtimeScope),
Effect.mapError(
(cause) =>
new OpenCodeRuntimeError({
operation: "startOpenCodeServerProcess",
detail: `Failed to spawn OpenCode server process: ${openCodeRuntimeErrorDetail(cause)}`,
cause,
}),
),
);
const killOpenCodeProcessGroup = (signal: NodeJS.Signals) =>
process.platform === "win32"
? child.kill({ killSignal: signal, forceKillAfter: "1 second" }).pipe(Effect.asVoid)
: Effect.sync(() => {
try {
process.kill(-Number(child.pid), signal);
} catch {
// The direct child may already have exited after starting the
// server; the process group kill is best-effort cleanup for
// any serve process left in that group.
}
});
const terminateChild = killOpenCodeProcessGroup("SIGTERM").pipe(
Effect.andThen(Effect.sleep("1 second")),
Effect.andThen(killOpenCodeProcessGroup("SIGKILL")),
Effect.ignore,
);
yield* Scope.addFinalizer(runtimeScope, terminateChild);
const stdoutRef = yield* Ref.make("");
const stderrRef = yield* Ref.make("");
const readyDeferred = yield* Deferred.make<string, OpenCodeRuntimeError>();
const setReadyFromStdoutChunk = (chunk: string) =>
Ref.updateAndGet(stdoutRef, (stdout) => `${stdout}${chunk}`).pipe(
Effect.flatMap((nextStdout) => {
const parsed = parseServerUrlFromOutput(nextStdout);
return parsed
? Deferred.succeed(readyDeferred, parsed).pipe(Effect.ignore)
: Effect.void;
}),
);
const stdoutFiber = yield* child.stdout.pipe(
Stream.decodeText(),
Stream.runForEach(setReadyFromStdoutChunk),
Effect.ignore,
Effect.forkIn(runtimeScope),
);
const stderrFiber = yield* child.stderr.pipe(
Stream.decodeText(),
Stream.runForEach((chunk) => Ref.update(stderrRef, (stderr) => `${stderr}${chunk}`)),
Effect.ignore,
Effect.forkIn(runtimeScope),
);
const exitFiber = yield* child.exitCode.pipe(
Effect.flatMap((code) =>
Effect.gen(function* () {
const stdout = yield* Ref.get(stdoutRef);
const stderr = yield* Ref.get(stderrRef);
const exitCode = Number(code);
yield* Deferred.fail(
readyDeferred,
new OpenCodeRuntimeError({
operation: "startOpenCodeServerProcess",
detail: [
`OpenCode server exited before startup completed (code: ${String(exitCode)}).`,
stdout.trim() ? `stdout:\n${stdout.trim()}` : null,
stderr.trim() ? `stderr:\n${stderr.trim()}` : null,
]
.filter(Boolean)
.join("\n\n"),
cause: { exitCode, stdout, stderr },
}),
).pipe(Effect.ignore);
}),
),
Effect.ignore,
Effect.forkIn(runtimeScope),
);
const readyExit = yield* Effect.exit(
Deferred.await(readyDeferred).pipe(Effect.timeoutOption(timeout)),
);
// Startup-time fibers are no longer needed once ready has resolved (either
// way). The exit fiber is only interrupted on failure; on success it keeps
// the caller's `exitCode` effect observable until the scope closes.
yield* Fiber.interrupt(stdoutFiber).pipe(Effect.ignore);
yield* Fiber.interrupt(stderrFiber).pipe(Effect.ignore);
if (Exit.isFailure(readyExit)) {
yield* Fiber.interrupt(exitFiber).pipe(Effect.ignore);
const squashed = Cause.squash(readyExit.cause);
return yield* ensureRuntimeError(
"startOpenCodeServerProcess",
`Failed while waiting for OpenCode server startup: ${openCodeRuntimeErrorDetail(squashed)}`,
squashed,
);
}
const readyOption = readyExit.value;
if (Option.isNone(readyOption)) {
yield* Fiber.interrupt(exitFiber).pipe(Effect.ignore);
return yield* new OpenCodeRuntimeError({
operation: "startOpenCodeServerProcess",
detail: `Timed out waiting for OpenCode server start after ${Duration.format(timeout)}.`,
});
}
return {
url: readyOption.value,
exitCode: child.exitCode.pipe(
Effect.map(Number),
Effect.orElseSucceed(() => 0),
),
} satisfies OpenCodeServerProcess;
});
const connectToOpenCodeServer: OpenCodeRuntimeShape["connectToOpenCodeServer"] = (input) => {
const serverUrl = input.serverUrl?.trim();
if (serverUrl) {
// We don't own externally-configured servers — no scope interaction.
return Effect.succeed({
url: serverUrl,
exitCode: null,
external: true,
});
}
return startOpenCodeServerProcess({
binaryPath: input.binaryPath,
...(input.environment !== undefined ? { environment: input.environment } : {}),
...(input.port !== undefined ? { port: input.port } : {}),
...(input.hostname !== undefined ? { hostname: input.hostname } : {}),
...(input.timeout !== undefined ? { timeout: input.timeout } : {}),
}).pipe(
Effect.map((server) => ({
url: server.url,
exitCode: server.exitCode,
external: false,
})),
);
};
const createOpenCodeSdkClient: OpenCodeRuntimeShape["createOpenCodeSdkClient"] = (input) =>
createOpencodeClient({
baseUrl: input.baseUrl,
directory: input.directory,
...(input.serverPassword
? {
headers: {
Authorization: `Basic ${Buffer.from(`opencode:${input.serverPassword}`, "utf8").toString("base64")}`,
},
}
: {}),
throwOnError: true,
});
const loadProviders = (client: OpencodeClient) =>
runOpenCodeSdk("provider.list", () => client.provider.list()).pipe(
Effect.filterMapOrFail(
(list) =>
list.data
? Result.succeed(list.data)
: Result.fail(
new OpenCodeRuntimeError({
operation: "provider.list",
detail: "OpenCode provider list was empty.",
}),
),
(result) => result,
),
);
const loadAgents = (client: OpencodeClient) =>
runOpenCodeSdk("app.agents", () => client.app.agents()).pipe(
Effect.map((result) => result.data ?? []),
);
const loadOpenCodeInventory: OpenCodeRuntimeShape["loadOpenCodeInventory"] = (client) =>
Effect.all([loadProviders(client), loadAgents(client)], { concurrency: "unbounded" }).pipe(
Effect.map(([providerList, agents]) => ({ providerList, agents })),
);
return {
startOpenCodeServerProcess,
connectToOpenCodeServer,
runOpenCodeCommand,
createOpenCodeSdkClient,
loadOpenCodeInventory,
} satisfies OpenCodeRuntimeShape;
});
export class OpenCodeRuntime extends Context.Service<OpenCodeRuntime, OpenCodeRuntimeShape>()(
"t3/provider/opencodeRuntime",
) {}
export const OpenCodeRuntimeLive = Layer.effect(OpenCodeRuntime, makeOpenCodeRuntime).pipe(
Layer.provide(NetService.layer),
);