-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode.ts
More file actions
144 lines (129 loc) · 3.37 KB
/
opencode.ts
File metadata and controls
144 lines (129 loc) · 3.37 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
import {
type AssistantMessage,
createOpencode,
type Part,
type Session,
type TextPartInput,
} from "@opencode-ai/sdk/v2";
export interface OpencodeSessionClient {
create(parameters: { directory?: string; title?: string }): Promise<Session>;
prompt(parameters: {
sessionID: string;
directory?: string;
agent?: string;
model?: {
providerID: string;
modelID: string;
};
system?: string;
variant?: string;
parts?: Array<TextPartInput>;
}): Promise<{ info: AssistantMessage; parts: Part[] }>;
abort(parameters: {
sessionID: string;
directory?: string;
}): Promise<unknown>;
}
export interface OpencodeRuntimeClient {
session: OpencodeSessionClient;
instance: {
dispose(): Promise<unknown>;
};
}
export interface ManagedOpencodeRuntime {
client: OpencodeRuntimeClient;
server: {
url: string;
close(): void;
};
}
export interface OpencodeRuntimeManager {
ensureStarted(instanceId: string): Promise<ManagedOpencodeRuntime>;
get(instanceId: string): ManagedOpencodeRuntime | undefined;
isRunning(instanceId: string): boolean;
stop(instanceId: string): Promise<void>;
stopAll(): Promise<void>;
}
interface RuntimeEntry {
runtime?: ManagedOpencodeRuntime;
starting?: Promise<ManagedOpencodeRuntime>;
}
export class OpencodeRegistry implements OpencodeRuntimeManager {
private readonly runtimes = new Map<string, RuntimeEntry>();
async ensureStarted(instanceId: string): Promise<ManagedOpencodeRuntime> {
const entry = this.runtimes.get(instanceId);
if (entry?.runtime) {
return entry.runtime;
}
if (entry?.starting) {
return entry.starting;
}
const starting = createOpencode().then(async ({ client, server }) => {
const runtime: ManagedOpencodeRuntime = {
client: {
instance: {
dispose: () => client.instance.dispose(),
},
session: {
create: async (parameters) => {
const res = await client.session.create(parameters, {
throwOnError: true,
responseStyle: "data",
});
return res as unknown as Session;
},
prompt: async (parameters) => {
const res = await client.session.prompt(parameters, {
throwOnError: true,
responseStyle: "data",
});
return res as unknown as {
info: AssistantMessage;
parts: Part[];
};
},
abort: (parameters) =>
client.session.abort(parameters, {
throwOnError: true,
}),
},
},
server,
};
this.runtimes.set(instanceId, { runtime });
return runtime;
});
this.runtimes.set(instanceId, { starting });
try {
return await starting;
} catch (error) {
this.runtimes.delete(instanceId);
throw error;
}
}
get(instanceId: string): ManagedOpencodeRuntime | undefined {
return this.runtimes.get(instanceId)?.runtime;
}
isRunning(instanceId: string): boolean {
return this.runtimes.has(instanceId) && Boolean(this.get(instanceId));
}
async stop(instanceId: string): Promise<void> {
const entry = this.runtimes.get(instanceId);
if (!entry) {
return;
}
try {
const runtime =
entry.runtime ?? (entry.starting ? await entry.starting : undefined);
await runtime?.client.instance.dispose();
runtime?.server.close();
} finally {
this.runtimes.delete(instanceId);
}
}
async stopAll(): Promise<void> {
await Promise.allSettled(
[...this.runtimes.keys()].map((instanceId) => this.stop(instanceId)),
);
}
}