@llmguard/opencode 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MANUAL-TEST-G.md +167 -0
- package/README.md +334 -2
- package/RELEASE.md +122 -0
- package/bin/darwin-amd64/helper +0 -0
- package/bin/darwin-arm64/helper +0 -0
- package/bin/linux-amd64/helper +0 -0
- package/bin/linux-arm64/helper +0 -0
- package/bin/manifest.json +51 -0
- package/bin/windows-amd64/helper.exe +0 -0
- package/bin/windows-arm64/helper.exe +0 -0
- package/bridge/client.d.ts +55 -0
- package/bridge/client.js +425 -0
- package/bridge/framing.d.ts +19 -0
- package/bridge/framing.js +110 -0
- package/bridge/process.d.ts +38 -0
- package/bridge/process.js +378 -0
- package/bridge/protocol.d.ts +184 -0
- package/bridge/protocol.js +683 -0
- package/connection-catalog.d.ts +69 -0
- package/connection-catalog.js +287 -0
- package/connections-tui.d.ts +11 -0
- package/connections-tui.js +394 -0
- package/demasking.d.ts +2 -0
- package/demasking.js +445 -0
- package/gui-experimental.cjs +33 -0
- package/hook-trace.d.ts +2 -0
- package/hook-trace.js +35 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/licenses/Go-LICENSE +27 -0
- package/messages.d.ts +230 -0
- package/messages.js +231 -0
- package/native/index.d.ts +1 -0
- package/native/index.js +1 -0
- package/native/tui.d.ts +1 -0
- package/native/tui.js +1 -0
- package/package.json +49 -4
- package/plugin-runtime.d.ts +3 -0
- package/plugin-runtime.js +3 -0
- package/plugin-settings.d.ts +77 -0
- package/plugin-settings.js +487 -0
- package/plugin.d.ts +41 -0
- package/plugin.js +10 -0
- package/response-diagnostics.d.ts +13 -0
- package/response-diagnostics.js +142 -0
- package/runtime/attachments.d.ts +4 -0
- package/runtime/attachments.js +19 -0
- package/runtime/errors.d.ts +18 -0
- package/runtime/errors.js +26 -0
- package/runtime/json.d.ts +14 -0
- package/runtime/json.js +243 -0
- package/runtime/limits.d.ts +2 -0
- package/runtime/limits.js +3 -0
- package/runtime/native-edit.d.ts +14 -0
- package/runtime/native-edit.js +73 -0
- package/runtime/runtime.d.ts +43 -0
- package/runtime/runtime.js +345 -0
- package/runtime/session-state.d.ts +13 -0
- package/runtime/session-state.js +73 -0
- package/runtime/text-layout.d.ts +9 -0
- package/runtime/text-layout.js +47 -0
- package/server.d.ts +1 -0
- package/server.js +1 -0
- package/settings-rpc.d.ts +511 -0
- package/settings-rpc.js +197 -0
- package/tui-messages.d.ts +1 -0
- package/tui-messages.js +84 -0
- package/tui.d.ts +17 -0
- package/tui.js +427 -0
- package/v1/hooks.d.ts +81 -0
- package/v1/hooks.js +97 -0
- package/v1/projection.d.ts +32 -0
- package/v1/projection.js +217 -0
- package/v1.d.ts +2 -0
- package/v1.js +2 -0
- package/v2/projection.d.ts +36 -0
- package/v2/projection.js +374 -0
- package/v2/server.d.ts +6 -0
- package/v2/server.js +633 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { ConnectionProfile, SettingsStatus } from "./plugin-settings.js";
|
|
2
|
+
export interface SavedConnection extends ConnectionProfile {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
}
|
|
6
|
+
export interface ConnectionDocument {
|
|
7
|
+
version: 1;
|
|
8
|
+
revision: number;
|
|
9
|
+
items: SavedConnection[];
|
|
10
|
+
/** Private server storage only; never included in ConnectionList. */
|
|
11
|
+
credentials?: Record<string, string>;
|
|
12
|
+
}
|
|
13
|
+
export interface ConnectionList {
|
|
14
|
+
revision: number;
|
|
15
|
+
items: (SavedConnection & {
|
|
16
|
+
tokenPresent: boolean;
|
|
17
|
+
tokenHint?: string;
|
|
18
|
+
active: boolean;
|
|
19
|
+
})[];
|
|
20
|
+
enabled: boolean;
|
|
21
|
+
configured: boolean;
|
|
22
|
+
activeLabel: string;
|
|
23
|
+
environmentOverride: boolean;
|
|
24
|
+
}
|
|
25
|
+
export declare class ConnectionCatalog {
|
|
26
|
+
#private;
|
|
27
|
+
readonly host: {
|
|
28
|
+
read(): Promise<unknown>;
|
|
29
|
+
write(value: ConnectionDocument): Promise<void>;
|
|
30
|
+
current(): ConnectionProfile & {
|
|
31
|
+
token: string;
|
|
32
|
+
connectionID?: string;
|
|
33
|
+
};
|
|
34
|
+
status(): SettingsStatus;
|
|
35
|
+
generation(): number;
|
|
36
|
+
validate(profile: ConnectionProfile, token: string): Promise<boolean>;
|
|
37
|
+
probe(profile: ConnectionProfile, token: string, signal?: AbortSignal): Promise<{
|
|
38
|
+
connected: boolean;
|
|
39
|
+
code: string;
|
|
40
|
+
decision: string | null;
|
|
41
|
+
}>;
|
|
42
|
+
apply(profile: SavedConnection, token: string, generation: number): Promise<void>;
|
|
43
|
+
};
|
|
44
|
+
constructor(host: {
|
|
45
|
+
read(): Promise<unknown>;
|
|
46
|
+
write(value: ConnectionDocument): Promise<void>;
|
|
47
|
+
current(): ConnectionProfile & {
|
|
48
|
+
token: string;
|
|
49
|
+
connectionID?: string;
|
|
50
|
+
};
|
|
51
|
+
status(): SettingsStatus;
|
|
52
|
+
generation(): number;
|
|
53
|
+
validate(profile: ConnectionProfile, token: string): Promise<boolean>;
|
|
54
|
+
probe(profile: ConnectionProfile, token: string, signal?: AbortSignal): Promise<{
|
|
55
|
+
connected: boolean;
|
|
56
|
+
code: string;
|
|
57
|
+
decision: string | null;
|
|
58
|
+
}>;
|
|
59
|
+
apply(profile: SavedConnection, token: string, generation: number): Promise<void>;
|
|
60
|
+
});
|
|
61
|
+
list(): Promise<ConnectionList>;
|
|
62
|
+
save(input: unknown): Promise<ConnectionList>;
|
|
63
|
+
test(input: unknown, connect?: boolean, signal?: AbortSignal): Promise<{
|
|
64
|
+
connected: boolean;
|
|
65
|
+
code: string;
|
|
66
|
+
decision: string | null;
|
|
67
|
+
}>;
|
|
68
|
+
dispose(): void;
|
|
69
|
+
}
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
export class ConnectionCatalog {
|
|
2
|
+
host;
|
|
3
|
+
#tail = Promise.resolve();
|
|
4
|
+
#keys = new Map();
|
|
5
|
+
// Serialized form of the last document whose items and credentials passed
|
|
6
|
+
// host.validate; an unchanged document is not validated again.
|
|
7
|
+
#validated;
|
|
8
|
+
#disposed = false;
|
|
9
|
+
constructor(host) {
|
|
10
|
+
this.host = host;
|
|
11
|
+
}
|
|
12
|
+
#run(action) {
|
|
13
|
+
const result = this.#tail.then(() => {
|
|
14
|
+
if (this.#disposed) {
|
|
15
|
+
throw new Error("Connections unavailable");
|
|
16
|
+
}
|
|
17
|
+
return action();
|
|
18
|
+
});
|
|
19
|
+
this.#tail = result
|
|
20
|
+
.catch(() => { })
|
|
21
|
+
.finally(() => {
|
|
22
|
+
if (this.#disposed) {
|
|
23
|
+
this.#keys.clear();
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
async #read() {
|
|
29
|
+
const raw = await this.host.read();
|
|
30
|
+
if (raw === undefined) {
|
|
31
|
+
const current = this.host.current();
|
|
32
|
+
const items = current.baseURL &&
|
|
33
|
+
current.projectSID &&
|
|
34
|
+
(await this.host.validate(current, "validation"))
|
|
35
|
+
? [
|
|
36
|
+
{
|
|
37
|
+
id: "legacy",
|
|
38
|
+
name: "OpenCode",
|
|
39
|
+
baseURL: current.baseURL,
|
|
40
|
+
projectSID: current.projectSID,
|
|
41
|
+
allowLoopbackHTTP: current.allowLoopbackHTTP,
|
|
42
|
+
},
|
|
43
|
+
]
|
|
44
|
+
: [];
|
|
45
|
+
if (items.length &&
|
|
46
|
+
current.token &&
|
|
47
|
+
(!current.connectionID || current.connectionID === "legacy") &&
|
|
48
|
+
!this.#keys.has("legacy")) {
|
|
49
|
+
this.#keys.set("legacy", current.token);
|
|
50
|
+
}
|
|
51
|
+
return { version: 1, revision: 0, items };
|
|
52
|
+
}
|
|
53
|
+
const doc = raw;
|
|
54
|
+
if (!doc ||
|
|
55
|
+
doc.version !== 1 ||
|
|
56
|
+
!Number.isSafeInteger(doc.revision) ||
|
|
57
|
+
doc.revision < 0 ||
|
|
58
|
+
!Array.isArray(doc.items) ||
|
|
59
|
+
doc.items.length > 100) {
|
|
60
|
+
throw new Error("Connections unavailable");
|
|
61
|
+
}
|
|
62
|
+
const signature = JSON.stringify(doc);
|
|
63
|
+
const known = signature === this.#validated;
|
|
64
|
+
const ids = new Set();
|
|
65
|
+
for (const item of doc.items) {
|
|
66
|
+
if (!item ||
|
|
67
|
+
Object.keys(item).sort().join(",") !==
|
|
68
|
+
"allowLoopbackHTTP,baseURL,id,name,projectSID" ||
|
|
69
|
+
typeof item.id !== "string" ||
|
|
70
|
+
!/^[a-zA-Z0-9-]{1,64}$/.test(item.id) ||
|
|
71
|
+
ids.has(item.id) ||
|
|
72
|
+
typeof item.name !== "string" ||
|
|
73
|
+
!item.name.trim() ||
|
|
74
|
+
item.name.length > 100 ||
|
|
75
|
+
[...item.name].some((c) => c.charCodeAt(0) < 32 || c.charCodeAt(0) === 127) ||
|
|
76
|
+
typeof item.baseURL !== "string" ||
|
|
77
|
+
typeof item.projectSID !== "string" ||
|
|
78
|
+
typeof item.allowLoopbackHTTP !== "boolean" ||
|
|
79
|
+
!(known || (await this.host.validate(item, "validation")))) {
|
|
80
|
+
throw new Error("Connections unavailable");
|
|
81
|
+
}
|
|
82
|
+
ids.add(item.id);
|
|
83
|
+
}
|
|
84
|
+
if (doc.credentials !== undefined) {
|
|
85
|
+
if (!doc.credentials ||
|
|
86
|
+
typeof doc.credentials !== "object" ||
|
|
87
|
+
Array.isArray(doc.credentials)) {
|
|
88
|
+
throw new Error("Connections unavailable");
|
|
89
|
+
}
|
|
90
|
+
const restored = new Map();
|
|
91
|
+
for (const [id, token] of Object.entries(doc.credentials)) {
|
|
92
|
+
const item = doc.items.find((entry) => entry.id === id);
|
|
93
|
+
if (!item ||
|
|
94
|
+
typeof token !== "string" ||
|
|
95
|
+
!token ||
|
|
96
|
+
!(known || (await this.host.validate(item, token)))) {
|
|
97
|
+
throw new Error("Connections unavailable");
|
|
98
|
+
}
|
|
99
|
+
restored.set(id, token);
|
|
100
|
+
}
|
|
101
|
+
this.#keys = restored;
|
|
102
|
+
}
|
|
103
|
+
this.#validated = signature;
|
|
104
|
+
// Only the migrated endpoint may inherit an environment key after restart.
|
|
105
|
+
const current = this.host.current();
|
|
106
|
+
const legacy = doc.items.find((item) => item.id === "legacy" &&
|
|
107
|
+
item.baseURL === current.baseURL &&
|
|
108
|
+
item.projectSID === current.projectSID &&
|
|
109
|
+
item.allowLoopbackHTTP === current.allowLoopbackHTTP);
|
|
110
|
+
if (legacy &&
|
|
111
|
+
current.token &&
|
|
112
|
+
(!current.connectionID || current.connectionID === legacy.id) &&
|
|
113
|
+
!this.#keys.has(legacy.id)) {
|
|
114
|
+
this.#keys.set(legacy.id, current.token);
|
|
115
|
+
}
|
|
116
|
+
return structuredClone(doc);
|
|
117
|
+
}
|
|
118
|
+
#view(doc) {
|
|
119
|
+
const status = this.host.status(), current = this.host.current();
|
|
120
|
+
const matches = (item) => item.baseURL === current.baseURL &&
|
|
121
|
+
item.projectSID === current.projectSID &&
|
|
122
|
+
item.allowLoopbackHTTP === current.allowLoopbackHTTP &&
|
|
123
|
+
!!current.token &&
|
|
124
|
+
this.#keys.get(item.id) === current.token;
|
|
125
|
+
// Old snapshots have no ID: choose at most one deterministic legacy match.
|
|
126
|
+
// New activations persist their ID with the runtime endpoint snapshot.
|
|
127
|
+
const active = current.connectionID
|
|
128
|
+
? doc.items.find((item) => item.id === current.connectionID && matches(item))
|
|
129
|
+
: doc.items.find(matches);
|
|
130
|
+
return {
|
|
131
|
+
revision: doc.revision,
|
|
132
|
+
enabled: status.enabled,
|
|
133
|
+
configured: status.configured,
|
|
134
|
+
activeLabel: active?.name ?? status.baseURL ?? "",
|
|
135
|
+
environmentOverride: status.environmentOverride,
|
|
136
|
+
items: doc.items.map((item) => ({
|
|
137
|
+
...item,
|
|
138
|
+
tokenPresent: this.#keys.has(item.id),
|
|
139
|
+
...(this.#keys.has(item.id)
|
|
140
|
+
? {
|
|
141
|
+
tokenHint: this.#keys.get(item.id).length >= 10
|
|
142
|
+
? `${this.#keys.get(item.id).slice(0, 4)}…${this.#keys.get(item.id).slice(-2)}`
|
|
143
|
+
: "••••",
|
|
144
|
+
}
|
|
145
|
+
: {}),
|
|
146
|
+
active: item.id === active?.id,
|
|
147
|
+
})),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
list() {
|
|
151
|
+
return this.#run(async () => this.#view(await this.#read()));
|
|
152
|
+
}
|
|
153
|
+
save(input) {
|
|
154
|
+
return this.#run(async () => {
|
|
155
|
+
if (this.host.status().environmentOverride) {
|
|
156
|
+
throw new Error("Settings controlled by environment");
|
|
157
|
+
}
|
|
158
|
+
if (!input || typeof input !== "object") {
|
|
159
|
+
throw new Error("Invalid connection");
|
|
160
|
+
}
|
|
161
|
+
const value = input;
|
|
162
|
+
if (Object.keys(value).some((k) => ![
|
|
163
|
+
"revision",
|
|
164
|
+
"id",
|
|
165
|
+
"name",
|
|
166
|
+
"baseURL",
|
|
167
|
+
"projectSID",
|
|
168
|
+
"allowLoopbackHTTP",
|
|
169
|
+
"token",
|
|
170
|
+
].includes(k))) {
|
|
171
|
+
throw new Error("Invalid connection");
|
|
172
|
+
}
|
|
173
|
+
const doc = await this.#read();
|
|
174
|
+
if (value.revision !== doc.revision) {
|
|
175
|
+
throw new Error("Connections changed");
|
|
176
|
+
}
|
|
177
|
+
const existing = doc.items.find((item) => item.id === value.id);
|
|
178
|
+
if (value.id !== undefined && !existing) {
|
|
179
|
+
throw new Error("Connection missing");
|
|
180
|
+
}
|
|
181
|
+
if (!existing && doc.items.length >= 100) {
|
|
182
|
+
throw new Error("Connection limit");
|
|
183
|
+
}
|
|
184
|
+
if (typeof value.name !== "string" ||
|
|
185
|
+
!value.name.trim() ||
|
|
186
|
+
value.name.length > 100 ||
|
|
187
|
+
[...value.name].some((c) => c.charCodeAt(0) < 32 || c.charCodeAt(0) === 127) ||
|
|
188
|
+
typeof value.baseURL !== "string" ||
|
|
189
|
+
typeof value.projectSID !== "string" ||
|
|
190
|
+
typeof value.allowLoopbackHTTP !== "boolean" ||
|
|
191
|
+
(value.token !== undefined && typeof value.token !== "string")) {
|
|
192
|
+
throw new Error("Invalid connection");
|
|
193
|
+
}
|
|
194
|
+
const item = {
|
|
195
|
+
id: existing?.id ?? crypto.randomUUID(),
|
|
196
|
+
name: value.name.trim(),
|
|
197
|
+
baseURL: value.baseURL.trim(),
|
|
198
|
+
projectSID: value.projectSID.trim(),
|
|
199
|
+
allowLoopbackHTTP: value.allowLoopbackHTTP,
|
|
200
|
+
};
|
|
201
|
+
const token = typeof value.token === "string" && value.token.length
|
|
202
|
+
? value.token
|
|
203
|
+
: this.#keys.get(item.id);
|
|
204
|
+
if (!(await this.host.validate(item, token ?? "validation"))) {
|
|
205
|
+
throw new Error("Invalid connection");
|
|
206
|
+
}
|
|
207
|
+
const next = {
|
|
208
|
+
version: 1,
|
|
209
|
+
revision: doc.revision + 1,
|
|
210
|
+
items: existing
|
|
211
|
+
? doc.items.map((p) => (p.id === item.id ? item : p))
|
|
212
|
+
: [...doc.items, item],
|
|
213
|
+
};
|
|
214
|
+
const keys = new Map(this.#keys);
|
|
215
|
+
if (token) {
|
|
216
|
+
keys.set(item.id, token);
|
|
217
|
+
}
|
|
218
|
+
next.credentials = Object.fromEntries(keys);
|
|
219
|
+
// Persist credentials with metadata atomically; public projections omit them.
|
|
220
|
+
await this.host.write(next);
|
|
221
|
+
if (this.#disposed) {
|
|
222
|
+
throw new Error("Connections unavailable");
|
|
223
|
+
}
|
|
224
|
+
if (token) {
|
|
225
|
+
this.#keys.set(item.id, token);
|
|
226
|
+
}
|
|
227
|
+
return this.#view(next);
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
test(input, connect = false, signal) {
|
|
231
|
+
return this.#run(async () => {
|
|
232
|
+
const doc = await this.#read();
|
|
233
|
+
const value = input;
|
|
234
|
+
if (!value ||
|
|
235
|
+
value.revision !== doc.revision ||
|
|
236
|
+
typeof value.id !== "string") {
|
|
237
|
+
throw new Error("Connections changed");
|
|
238
|
+
}
|
|
239
|
+
const item = doc.items.find((p) => p.id === value.id);
|
|
240
|
+
const token = item
|
|
241
|
+
? this.#keys.get(item.id)
|
|
242
|
+
: undefined;
|
|
243
|
+
if (!item || !token) {
|
|
244
|
+
return {
|
|
245
|
+
connected: false,
|
|
246
|
+
code: "key_required",
|
|
247
|
+
decision: null,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
if (connect && this.host.status().environmentOverride) {
|
|
251
|
+
throw new Error("Settings controlled by environment");
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
item,
|
|
255
|
+
token,
|
|
256
|
+
revision: doc.revision,
|
|
257
|
+
generation: this.host.generation(),
|
|
258
|
+
};
|
|
259
|
+
}).then(async (selected) => {
|
|
260
|
+
if (!("item" in selected)) {
|
|
261
|
+
return selected;
|
|
262
|
+
}
|
|
263
|
+
// The probe runs outside the queue so list() and save() are not blocked
|
|
264
|
+
// behind a slow Guard; apply re-enters it and rechecks what was probed.
|
|
265
|
+
const { item, token, revision, generation } = selected;
|
|
266
|
+
const result = await this.host.probe(item, token, signal);
|
|
267
|
+
if (connect && result.connected) {
|
|
268
|
+
await this.#run(async () => {
|
|
269
|
+
if (this.#disposed || signal?.aborted) {
|
|
270
|
+
throw new Error("Cancelled");
|
|
271
|
+
}
|
|
272
|
+
const current = await this.#read();
|
|
273
|
+
if (current.revision !== revision ||
|
|
274
|
+
this.#keys.get(item.id) !== token) {
|
|
275
|
+
throw new Error("Connections changed");
|
|
276
|
+
}
|
|
277
|
+
await this.host.apply(item, token, generation);
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
return result;
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
dispose() {
|
|
284
|
+
this.#disposed = true;
|
|
285
|
+
this.#keys.clear();
|
|
286
|
+
}
|
|
287
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode/plugin/tui";
|
|
2
|
+
type Context = Pick<Plugin.Context, "keymap" | "ui"> & Partial<Pick<Plugin.Context, "renderer">>;
|
|
3
|
+
export interface ConnectionsAPI {
|
|
4
|
+
connections(): Promise<unknown>;
|
|
5
|
+
saveConnection(input: unknown): Promise<unknown>;
|
|
6
|
+
connect(input: unknown): Promise<unknown>;
|
|
7
|
+
testConnection(input: unknown): Promise<unknown>;
|
|
8
|
+
setEnabled(input: unknown): Promise<unknown>;
|
|
9
|
+
}
|
|
10
|
+
export declare function registerConnectionsTUI(context: Context, api: ConnectionsAPI, locale?: string): () => void;
|
|
11
|
+
export {};
|