Repository navigation
Expand file tree
/
Copy pathacp.ts
More file actions
61 lines (60 loc) · 2.45 KB
/
Copy pathacp.ts
File metadata and controls
61 lines (60 loc) · 2.45 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
import { MessageTooLargeError, ndJsonStream } from "@agentclientprotocol/sdk"
import { OpenCode } from "@opencode/client/effect"
import { Service } from "@opencode/client/effect/service"
import { CrossSpawnSpawner } from "@opencode/util/cross-spawn-spawner"
import { Effect, Option } from "effect"
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http"
import { Writable } from "node:stream"
import { ACP } from "../../acp/agent"
import { Commands } from "../commands"
import { Runtime } from "../../framework/runtime"
import { Standalone } from "../../services/standalone"
export default Runtime.handler(
Commands.commands.acp,
Effect.fn("cli.acp")(function* (input) {
if (input.login) {
const login = yield* Effect.promise(() => import("./auth/login"))
return yield* login.default({
target: Option.none(),
method: Option.none(),
answer: [],
server: Option.none(),
standalone: false,
})
}
process.env.OPENCODE_CLIENT = "acp"
const endpoint = yield* Standalone.start()
const client = yield* OpenCode.make({ baseUrl: endpoint.url }).pipe(
Effect.provideServiceEffect(
HttpClient.HttpClient,
HttpClient.HttpClient.pipe(
Effect.map(HttpClient.mapRequest(HttpClientRequest.setHeaders(Service.headers(endpoint) ?? {}))),
),
),
Effect.provide(FetchHttpClient.layer),
)
const connection = yield* ACP.connect(client, ndJsonStream(Writable.toWeb(process.stdout), Bun.stdin.stream()))
const failure = yield* Effect.raceFirst(
Effect.promise(() => connection.closed).pipe(
Effect.map(() =>
connection.signal.reason instanceof MessageTooLargeError
? `incoming message exceeded the ${connection.signal.reason.maxMessageBytes / 1024 / 1024} MiB limit`
: undefined,
),
),
endpoint.exited.pipe(
Effect.match({
onSuccess: (code) => `code ${code}`,
onFailure: (error) =>
error.cause instanceof CrossSpawnSpawner.KilledBySignal ? `signal ${error.cause.signal}` : error.message,
}),
Effect.map((reason) => `server exited unexpectedly (${reason})`),
),
)
// Exit directly: closing the scope would wait on the server's graceful shutdown, and its lease pipe ends it anyway.
yield* Effect.sync(() => {
if (failure) process.stderr.write(`opencode acp: ${failure}\n`)
process.exit(failure ? 1 : 0)
})
}),
)