@llmguard/opencode 0.0.0-stage → 0.1.1
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/README.md +53 -2
- 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 +47 -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
package/v2/server.js
ADDED
|
@@ -0,0 +1,633 @@
|
|
|
1
|
+
import { blocked, OpenCodeGuardError } from "../runtime/errors.js";
|
|
2
|
+
import { settingsRPC } from "../settings-rpc.js";
|
|
3
|
+
import { PluginSettings } from "../plugin-settings.js";
|
|
4
|
+
import { argumentProjection, layoutField, modelProjection, } from "./projection.js";
|
|
5
|
+
import { createHookTracer } from "../hook-trace.js";
|
|
6
|
+
import { modelField, modelName } from "../runtime/text-layout.js";
|
|
7
|
+
import { attachmentsForCheck } from "../runtime/attachments.js";
|
|
8
|
+
// Location-specific or reloaded module imports share live server state.
|
|
9
|
+
const sharedKey = Symbol.for("llmguard.opencode.server-settings.v1");
|
|
10
|
+
const registry = globalThis;
|
|
11
|
+
const routeNoticeAcknowledgment = ": llmguard-route-notice-shown\n\n";
|
|
12
|
+
function observeRouteNotice(response, shown) {
|
|
13
|
+
if (!response.body) {
|
|
14
|
+
return response;
|
|
15
|
+
}
|
|
16
|
+
const decoder = new TextDecoder();
|
|
17
|
+
let tail = "";
|
|
18
|
+
let acknowledged = false;
|
|
19
|
+
const body = response.body.pipeThrough(new TransformStream({
|
|
20
|
+
transform(chunk, controller) {
|
|
21
|
+
controller.enqueue(chunk);
|
|
22
|
+
if (acknowledged) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const text = tail + decoder.decode(chunk, { stream: true });
|
|
26
|
+
if (text.includes(routeNoticeAcknowledgment)) {
|
|
27
|
+
acknowledged = true;
|
|
28
|
+
shown();
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
tail = text.slice(-(routeNoticeAcknowledgment.length - 1));
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
}));
|
|
35
|
+
return new Response(body, {
|
|
36
|
+
status: response.status,
|
|
37
|
+
statusText: response.statusText,
|
|
38
|
+
headers: response.headers,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export default {
|
|
42
|
+
id: "llmguard",
|
|
43
|
+
async setup(ctx) {
|
|
44
|
+
// Hook diagnostics live in one local file: kind and reason codes only,
|
|
45
|
+
// never message content or credentials.
|
|
46
|
+
const trace = createHookTracer();
|
|
47
|
+
if (!registry[sharedKey]) {
|
|
48
|
+
const contexts = new Set([ctx]);
|
|
49
|
+
const storage = () => {
|
|
50
|
+
const active = contexts.values().next().value;
|
|
51
|
+
if (!active) {
|
|
52
|
+
throw new Error("LLMGuard: server plugin unavailable");
|
|
53
|
+
}
|
|
54
|
+
return active.storage;
|
|
55
|
+
};
|
|
56
|
+
const settings = new PluginSettings({
|
|
57
|
+
get: (key) => storage().get(key),
|
|
58
|
+
set: (key, value) => storage().set(key, typeof value === "boolean"
|
|
59
|
+
? value
|
|
60
|
+
: "items" in value
|
|
61
|
+
? {
|
|
62
|
+
...value,
|
|
63
|
+
items: value.items.map((item) => ({ ...item })),
|
|
64
|
+
}
|
|
65
|
+
: { ...value }),
|
|
66
|
+
}, ctx.location.directory, process.env);
|
|
67
|
+
registry[sharedKey] = { settings, ready: settings.load(), contexts };
|
|
68
|
+
}
|
|
69
|
+
const owner = registry[sharedKey];
|
|
70
|
+
owner.contexts.add(ctx);
|
|
71
|
+
const settings = owner.settings;
|
|
72
|
+
let disposed = false;
|
|
73
|
+
const localSessions = new Set();
|
|
74
|
+
const pendingRedirectNotices = new Set();
|
|
75
|
+
const routeLeases = new Map();
|
|
76
|
+
const routeLeaseRequests = new WeakMap();
|
|
77
|
+
async function routeLeaseAction(lease, action) {
|
|
78
|
+
const started = performance.now();
|
|
79
|
+
const validFor = await lease.action(action);
|
|
80
|
+
if (action === "renew") {
|
|
81
|
+
if (lease.closed) {
|
|
82
|
+
throw new Error("route lease closed");
|
|
83
|
+
}
|
|
84
|
+
lease.validUntil = started + validFor;
|
|
85
|
+
const remaining = lease.validUntil - performance.now();
|
|
86
|
+
if (remaining <= 0) {
|
|
87
|
+
throw new Error("route lease expired");
|
|
88
|
+
}
|
|
89
|
+
clearTimeout(lease.validity);
|
|
90
|
+
lease.validity = setTimeout(() => {
|
|
91
|
+
lease.abort.abort();
|
|
92
|
+
void finishRouteLease(lease.session, "lease_expired", lease);
|
|
93
|
+
}, remaining);
|
|
94
|
+
lease.validity.unref?.();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function scheduleRouteLeaseRenewal(session, lease) {
|
|
98
|
+
lease.timer = setTimeout(async () => {
|
|
99
|
+
if (lease.closed || routeLeases.get(session) !== lease) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
await routeLeaseAction(lease, "renew");
|
|
104
|
+
if (!lease.closed && routeLeases.get(session) === lease) {
|
|
105
|
+
scheduleRouteLeaseRenewal(session, lease);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
lease.abort.abort();
|
|
110
|
+
await finishRouteLease(session, "renew_failed", lease);
|
|
111
|
+
}
|
|
112
|
+
}, 8000);
|
|
113
|
+
lease.timer.unref?.();
|
|
114
|
+
}
|
|
115
|
+
async function startRouteLease(session, token, action, requestSignal) {
|
|
116
|
+
if (routeLeases.has(session)) {
|
|
117
|
+
const unclaimed = {
|
|
118
|
+
session,
|
|
119
|
+
token,
|
|
120
|
+
action,
|
|
121
|
+
validUntil: 0,
|
|
122
|
+
abort: new AbortController(),
|
|
123
|
+
closed: true,
|
|
124
|
+
};
|
|
125
|
+
await routeLeaseAction(unclaimed, "release").catch(() => { });
|
|
126
|
+
blocked();
|
|
127
|
+
}
|
|
128
|
+
const lease = {
|
|
129
|
+
session,
|
|
130
|
+
token,
|
|
131
|
+
action,
|
|
132
|
+
validUntil: 0,
|
|
133
|
+
abort: new AbortController(),
|
|
134
|
+
requestSignal,
|
|
135
|
+
closed: false,
|
|
136
|
+
};
|
|
137
|
+
routeLeases.set(session, lease);
|
|
138
|
+
lease.expiry = setTimeout(() => {
|
|
139
|
+
lease.abort.abort();
|
|
140
|
+
void finishRouteLease(session, "request_timeout", lease);
|
|
141
|
+
}, 180_000);
|
|
142
|
+
lease.expiry.unref?.();
|
|
143
|
+
lease.onRequestAbort = () => {
|
|
144
|
+
void finishRouteLease(session, "request_aborted", lease);
|
|
145
|
+
};
|
|
146
|
+
requestSignal.addEventListener("abort", lease.onRequestAbort, {
|
|
147
|
+
once: true,
|
|
148
|
+
});
|
|
149
|
+
if (requestSignal.aborted) {
|
|
150
|
+
await finishRouteLease(session, "request_aborted", lease);
|
|
151
|
+
blocked();
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
await routeLeaseAction(lease, "renew");
|
|
155
|
+
if (lease.closed ||
|
|
156
|
+
lease.abort.signal.aborted ||
|
|
157
|
+
requestSignal.aborted ||
|
|
158
|
+
performance.now() >= lease.validUntil) {
|
|
159
|
+
blocked();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
lease.abort.abort();
|
|
164
|
+
await finishRouteLease(session, "initial_renew_failed", lease);
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
scheduleRouteLeaseRenewal(session, lease);
|
|
168
|
+
return lease;
|
|
169
|
+
}
|
|
170
|
+
async function finishRouteLease(session, reason, expected) {
|
|
171
|
+
const lease = routeLeases.get(session);
|
|
172
|
+
if (!lease || lease.closed || (expected && lease !== expected)) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
lease.closed = true;
|
|
176
|
+
if (lease.timer) {
|
|
177
|
+
clearTimeout(lease.timer);
|
|
178
|
+
}
|
|
179
|
+
if (lease.expiry) {
|
|
180
|
+
clearTimeout(lease.expiry);
|
|
181
|
+
}
|
|
182
|
+
if (lease.validity) {
|
|
183
|
+
clearTimeout(lease.validity);
|
|
184
|
+
}
|
|
185
|
+
if (lease.onRequestAbort) {
|
|
186
|
+
lease.requestSignal?.removeEventListener("abort", lease.onRequestAbort);
|
|
187
|
+
}
|
|
188
|
+
routeLeases.delete(session);
|
|
189
|
+
try {
|
|
190
|
+
await routeLeaseAction(lease, "release");
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
trace(`route lease release_failed reason=${reason}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
async function release() {
|
|
197
|
+
if (disposed) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
disposed = true;
|
|
201
|
+
await Promise.all([...routeLeases.entries()].map(([session, lease]) => {
|
|
202
|
+
lease.abort.abort();
|
|
203
|
+
return finishRouteLease(session, "plugin_disposed", lease);
|
|
204
|
+
}));
|
|
205
|
+
owner.contexts.delete(ctx);
|
|
206
|
+
if (![...owner.contexts].some((other) => other.location.directory === ctx.location.directory)) {
|
|
207
|
+
await settings.releaseRuntime(ctx.location.directory);
|
|
208
|
+
}
|
|
209
|
+
if (!owner.contexts.size) {
|
|
210
|
+
if (registry[sharedKey] === owner) {
|
|
211
|
+
delete registry[sharedKey];
|
|
212
|
+
}
|
|
213
|
+
await settings.dispose();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
await owner.ready;
|
|
218
|
+
await ctx.rpc.register(settingsRPC, {
|
|
219
|
+
connections: async () => ({ ...(await settings.connections.list()) }),
|
|
220
|
+
saveConnection: async (input) => ({
|
|
221
|
+
...(await settings.connections.save(input)),
|
|
222
|
+
}),
|
|
223
|
+
testConnection: async (input, context) => settings.connections.test(input, false, context.signal),
|
|
224
|
+
connect: async (input, context) => settings.connections.test(input, true, context.signal),
|
|
225
|
+
status: async () => ({ ...settings.status() }),
|
|
226
|
+
setEnabled: async (input) => ({
|
|
227
|
+
...(await settings.setEnabled(input)),
|
|
228
|
+
}),
|
|
229
|
+
save: async (input) => ({ ...(await settings.save(input)) }),
|
|
230
|
+
setKey: async (input) => ({ ...(await settings.setKey(input)) }),
|
|
231
|
+
files: async (_input, context) => ({
|
|
232
|
+
...(await settings.files(context.signal)),
|
|
233
|
+
}),
|
|
234
|
+
setFiles: async (input, context) => ({
|
|
235
|
+
...(await settings.setFiles(input, context.signal)),
|
|
236
|
+
}),
|
|
237
|
+
test: async (_input, context) => settings.test(context.signal),
|
|
238
|
+
});
|
|
239
|
+
async function guard(action) {
|
|
240
|
+
if (disposed || !settings.enforcing()) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
try {
|
|
244
|
+
await action();
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
if (!disposed && settings.enforcing()) {
|
|
248
|
+
trace(`guard blocked code=${error instanceof OpenCodeGuardError ? error.code : "unknown"}`);
|
|
249
|
+
throw error;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
async function lookupRoute(session) {
|
|
254
|
+
if (!/^[A-Za-z0-9_.:-]{1,128}$/.test(session)) {
|
|
255
|
+
blocked();
|
|
256
|
+
}
|
|
257
|
+
const connection = settings.gatewayConnection();
|
|
258
|
+
const base = connection.baseURL.replace(/\/+$/, "");
|
|
259
|
+
const endpoint = `${base}/v1/projects/${encodeURIComponent(connection.projectSID)}`;
|
|
260
|
+
const data = await settings
|
|
261
|
+
.runtime(ctx.location.directory)
|
|
262
|
+
.routeStatus(session);
|
|
263
|
+
return {
|
|
264
|
+
connection,
|
|
265
|
+
endpoint,
|
|
266
|
+
...data,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
for (const kind of [
|
|
270
|
+
"context",
|
|
271
|
+
"compaction",
|
|
272
|
+
"generate",
|
|
273
|
+
"title",
|
|
274
|
+
]) {
|
|
275
|
+
await ctx.session.hook(kind, (event) => guard(async () => {
|
|
276
|
+
const started = performance.now();
|
|
277
|
+
const sessionID = event.sessionID;
|
|
278
|
+
// A result supplied by another plugin never came from a provider
|
|
279
|
+
// response; restore bound tokens before it is persisted.
|
|
280
|
+
const result = event.result;
|
|
281
|
+
if (kind === "title" && typeof result === "string") {
|
|
282
|
+
event.result = settings
|
|
283
|
+
.runtime(ctx.location.directory)
|
|
284
|
+
.restoreText(event.sessionID, result);
|
|
285
|
+
}
|
|
286
|
+
else if (kind === "compaction" &&
|
|
287
|
+
!!result &&
|
|
288
|
+
typeof result === "object" &&
|
|
289
|
+
typeof result.summary === "string") {
|
|
290
|
+
const summary = result;
|
|
291
|
+
summary.summary = settings
|
|
292
|
+
.runtime(ctx.location.directory)
|
|
293
|
+
.restoreText(event.sessionID, summary.summary);
|
|
294
|
+
}
|
|
295
|
+
const route = await lookupRoute(sessionID);
|
|
296
|
+
if (route.local || route.gateway_required) {
|
|
297
|
+
if (route.local) {
|
|
298
|
+
localSessions.add(sessionID);
|
|
299
|
+
}
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const projection = modelProjection(event, true);
|
|
303
|
+
const runtime = settings.runtime(ctx.location.directory);
|
|
304
|
+
const deadline = runtime.deadline(started);
|
|
305
|
+
// Attachments are inspected only when the helper's policy says so;
|
|
306
|
+
// otherwise they stay opaque, as they always were.
|
|
307
|
+
const attachments = projection.attachments.length
|
|
308
|
+
? await attachmentsForCheck(runtime, projection.attachments, deadline)
|
|
309
|
+
: undefined;
|
|
310
|
+
const texts = attachments ? projection.allTexts : projection.texts;
|
|
311
|
+
if (!texts.length && !attachments) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
// Structure-only diagnostics distinguish a missing layout from a
|
|
315
|
+
// genuinely flattened host request without logging any content.
|
|
316
|
+
const roles = ["system", "user", "assistant", "tool"];
|
|
317
|
+
const counts = roles.map((role) => `${role}=${event.messages.filter((message) => message.role === role).length}`);
|
|
318
|
+
trace(`projection name=session.${kind} texts=${texts.length} attachments=${attachments?.length ?? 0} layout=${projection.layout ? "bound" : "legacy"} ${counts.join(" ")}`);
|
|
319
|
+
// Bound role metadata lets Guard history render the latest user
|
|
320
|
+
// input as the active message instead of instructions or context.
|
|
321
|
+
const modelId = modelName(event.model);
|
|
322
|
+
const sourcePayload = projection.layout || modelId
|
|
323
|
+
? {
|
|
324
|
+
...(projection.layout
|
|
325
|
+
? {
|
|
326
|
+
[layoutField]: {
|
|
327
|
+
version: 1,
|
|
328
|
+
items: projection.layout,
|
|
329
|
+
},
|
|
330
|
+
}
|
|
331
|
+
: {}),
|
|
332
|
+
...(modelId ? { [modelField]: modelId } : {}),
|
|
333
|
+
}
|
|
334
|
+
: undefined;
|
|
335
|
+
await runtime.checkModelTexts(`session.${kind}`, texts, event.sessionID, sourcePayload, (candidate, replacements) => {
|
|
336
|
+
if (disposed ||
|
|
337
|
+
!settings.enforcing() ||
|
|
338
|
+
event.sessionID !== sessionID) {
|
|
339
|
+
blocked();
|
|
340
|
+
}
|
|
341
|
+
projection.apply(candidate, replacements);
|
|
342
|
+
}, deadline, attachments);
|
|
343
|
+
const routeAfterCheck = await runtime.routeStatus(sessionID);
|
|
344
|
+
if (!route.local && routeAfterCheck.local) {
|
|
345
|
+
localSessions.add(sessionID);
|
|
346
|
+
pendingRedirectNotices.add(sessionID);
|
|
347
|
+
}
|
|
348
|
+
}));
|
|
349
|
+
}
|
|
350
|
+
await ctx.session.hook("http.request", (event) => guard(async () => {
|
|
351
|
+
const session = event.sessionID;
|
|
352
|
+
const started = performance.now();
|
|
353
|
+
const { connection, endpoint, local, gateway_required } = await lookupRoute(session);
|
|
354
|
+
if (!local && localSessions.has(session)) {
|
|
355
|
+
blocked();
|
|
356
|
+
}
|
|
357
|
+
if (!local && !gateway_required) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (local) {
|
|
361
|
+
localSessions.add(session);
|
|
362
|
+
}
|
|
363
|
+
const original = event.request;
|
|
364
|
+
const url = new URL(original.url);
|
|
365
|
+
const geminiMatch = url.pathname.match(/\/models\/([A-Za-z0-9._-]{1,128}):(generateContent|streamGenerateContent)$/);
|
|
366
|
+
const apiPath = url.pathname.endsWith("/chat/completions")
|
|
367
|
+
? "chat/completions"
|
|
368
|
+
: url.pathname.endsWith("/responses")
|
|
369
|
+
? "responses"
|
|
370
|
+
: url.pathname.endsWith("/messages")
|
|
371
|
+
? "messages"
|
|
372
|
+
: geminiMatch
|
|
373
|
+
? "gemini"
|
|
374
|
+
: undefined;
|
|
375
|
+
if (!apiPath) {
|
|
376
|
+
return blocked();
|
|
377
|
+
}
|
|
378
|
+
if (apiPath === "gemini" &&
|
|
379
|
+
(geminiMatch === null ||
|
|
380
|
+
geminiMatch[1] === "." ||
|
|
381
|
+
geminiMatch[1] === "..")) {
|
|
382
|
+
return blocked();
|
|
383
|
+
}
|
|
384
|
+
if (original.method !== "POST" ||
|
|
385
|
+
!original.headers
|
|
386
|
+
.get("content-type")
|
|
387
|
+
?.toLowerCase()
|
|
388
|
+
.startsWith("application/json")) {
|
|
389
|
+
blocked();
|
|
390
|
+
}
|
|
391
|
+
const body = await original.clone().arrayBuffer();
|
|
392
|
+
if (body.byteLength > 28 * 1024 * 1024 ||
|
|
393
|
+
disposed ||
|
|
394
|
+
!settings.enforcing() ||
|
|
395
|
+
event.sessionID !== session) {
|
|
396
|
+
blocked();
|
|
397
|
+
}
|
|
398
|
+
if (!local) {
|
|
399
|
+
const runtime = settings.runtime(ctx.location.directory);
|
|
400
|
+
const routePath = apiPath === "gemini"
|
|
401
|
+
? `/gemini/${geminiMatch[1]}/${geminiMatch[2]}`
|
|
402
|
+
: `/${apiPath}`;
|
|
403
|
+
const checked = await runtime.checkProviderRequest(session, routePath, new Uint8Array(body), runtime.deadline(started));
|
|
404
|
+
if (disposed ||
|
|
405
|
+
!settings.enforcing() ||
|
|
406
|
+
event.sessionID !== session ||
|
|
407
|
+
event.request !== original) {
|
|
408
|
+
if (checked.routeLease) {
|
|
409
|
+
await checked.leaseAction("release").catch(() => { });
|
|
410
|
+
}
|
|
411
|
+
blocked();
|
|
412
|
+
}
|
|
413
|
+
if (!checked.local) {
|
|
414
|
+
const routeLease = checked.routeLease
|
|
415
|
+
? await startRouteLease(session, checked.routeLease, checked.leaseAction, original.signal)
|
|
416
|
+
: undefined;
|
|
417
|
+
if (routeLease &&
|
|
418
|
+
(routeLease.closed ||
|
|
419
|
+
routeLease.abort.signal.aborted ||
|
|
420
|
+
performance.now() >= routeLease.validUntil)) {
|
|
421
|
+
await finishRouteLease(session, "lease_expired_before_dispatch", routeLease);
|
|
422
|
+
blocked();
|
|
423
|
+
}
|
|
424
|
+
const headers = new Headers(original.headers);
|
|
425
|
+
headers.delete("content-length");
|
|
426
|
+
headers.delete("content-md5");
|
|
427
|
+
headers.delete("digest");
|
|
428
|
+
try {
|
|
429
|
+
event.request = new Request(original, {
|
|
430
|
+
headers,
|
|
431
|
+
body: new Uint8Array(checked.body),
|
|
432
|
+
...(routeLease
|
|
433
|
+
? {
|
|
434
|
+
signal: AbortSignal.any([
|
|
435
|
+
original.signal,
|
|
436
|
+
routeLease.abort.signal,
|
|
437
|
+
]),
|
|
438
|
+
}
|
|
439
|
+
: {}),
|
|
440
|
+
});
|
|
441
|
+
if (routeLease) {
|
|
442
|
+
routeLeaseRequests.set(event.request, routeLease);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
catch (error) {
|
|
446
|
+
if (routeLease) {
|
|
447
|
+
await finishRouteLease(session, "request_rewrite_failed", routeLease);
|
|
448
|
+
}
|
|
449
|
+
throw error;
|
|
450
|
+
}
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
localSessions.add(session);
|
|
454
|
+
if (!local) {
|
|
455
|
+
pendingRedirectNotices.add(session);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
if (apiPath === "gemini" &&
|
|
459
|
+
(geminiMatch === null ||
|
|
460
|
+
(geminiMatch[2] === "generateContent"
|
|
461
|
+
? url.search !== ""
|
|
462
|
+
: url.search !== "?alt=sse"))) {
|
|
463
|
+
return blocked();
|
|
464
|
+
}
|
|
465
|
+
const headers = new Headers({
|
|
466
|
+
Authorization: `Bearer ${connection.token}`,
|
|
467
|
+
"Content-Type": "application/json",
|
|
468
|
+
"X-LLMGuard-Session-ID": session,
|
|
469
|
+
"X-LLMGuard-Local-Only": "1",
|
|
470
|
+
});
|
|
471
|
+
if (pendingRedirectNotices.has(session)) {
|
|
472
|
+
headers.set("X-LLMGuard-Route-Notice", "local");
|
|
473
|
+
}
|
|
474
|
+
if (apiPath === "messages") {
|
|
475
|
+
for (const name of ["anthropic-version", "anthropic-beta"]) {
|
|
476
|
+
const value = original.headers.get(name);
|
|
477
|
+
if (value) {
|
|
478
|
+
headers.set(name, value);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
const localPath = apiPath === "gemini"
|
|
483
|
+
? `gemini/${geminiMatch[1]}/${geminiMatch[2]}${url.search}`
|
|
484
|
+
: apiPath === "chat/completions"
|
|
485
|
+
? "local/chat/completions"
|
|
486
|
+
: apiPath;
|
|
487
|
+
event.request = new Request(`${endpoint}/${localPath}`, {
|
|
488
|
+
method: "POST",
|
|
489
|
+
headers,
|
|
490
|
+
body,
|
|
491
|
+
signal: original.signal,
|
|
492
|
+
redirect: "error",
|
|
493
|
+
credentials: "omit",
|
|
494
|
+
});
|
|
495
|
+
}));
|
|
496
|
+
await ctx.session.hook("experimental.ws.handshake", (event) => guard(async () => {
|
|
497
|
+
const route = await lookupRoute(event.sessionID);
|
|
498
|
+
if (route.local ||
|
|
499
|
+
route.gateway_required ||
|
|
500
|
+
localSessions.has(event.sessionID)) {
|
|
501
|
+
blocked();
|
|
502
|
+
}
|
|
503
|
+
}));
|
|
504
|
+
await ctx.session.hook("http.response", async (event) => {
|
|
505
|
+
const lease = routeLeaseRequests.get(event.request);
|
|
506
|
+
if (lease) {
|
|
507
|
+
await finishRouteLease(event.sessionID, "provider_response", lease);
|
|
508
|
+
}
|
|
509
|
+
// Restoring masked model output never blocks. A response that cannot be
|
|
510
|
+
// restored passes through with its markers visible. The trace contains
|
|
511
|
+
// no content or credentials; it only shows whether and why restoration
|
|
512
|
+
// applied or passed through.
|
|
513
|
+
const responseID = crypto.randomUUID();
|
|
514
|
+
const reason = (detail) => trace(`http.response response=${responseID} kind=${event.kind} ${detail}`);
|
|
515
|
+
if (disposed || !settings.enforcing()) {
|
|
516
|
+
reason("disabled");
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
if (event.response.status >= 200 && event.response.status < 300) {
|
|
520
|
+
const notice = event.response.headers.get("x-llmguard-route-notice");
|
|
521
|
+
if (notice === "shown") {
|
|
522
|
+
pendingRedirectNotices.delete(event.sessionID);
|
|
523
|
+
}
|
|
524
|
+
else if (notice === "pending" &&
|
|
525
|
+
event.response.headers
|
|
526
|
+
.get("content-type")
|
|
527
|
+
?.toLowerCase()
|
|
528
|
+
.startsWith("text/event-stream")) {
|
|
529
|
+
event.response = observeRouteNotice(event.response, () => {
|
|
530
|
+
pendingRedirectNotices.delete(event.sessionID);
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
try {
|
|
535
|
+
const previous = event.response;
|
|
536
|
+
event.response = await settings
|
|
537
|
+
.runtime(ctx.location.directory)
|
|
538
|
+
.demaskResponse(event.sessionID, event.response, reason);
|
|
539
|
+
if (event.response === previous) {
|
|
540
|
+
reason("unchanged");
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
catch {
|
|
544
|
+
reason("hook_error");
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
await ctx.tool.hook("execute.before", (event) => guard(async () => {
|
|
548
|
+
const started = performance.now();
|
|
549
|
+
const input = event.input;
|
|
550
|
+
const sessionID = event.sessionID;
|
|
551
|
+
const callID = event.id;
|
|
552
|
+
const tool = event.tool;
|
|
553
|
+
const holder = { input };
|
|
554
|
+
const selected = argumentProjection(holder);
|
|
555
|
+
if (!selected.texts.length) {
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
const runtime = settings.runtime(ctx.location.directory);
|
|
559
|
+
const deadline = runtime.deadline(started);
|
|
560
|
+
await runtime.checkToolTexts({ tool, sessionID, callID }, selected.texts, (texts) => {
|
|
561
|
+
if (disposed ||
|
|
562
|
+
!settings.enforcing() ||
|
|
563
|
+
event.input !== input ||
|
|
564
|
+
event.sessionID !== sessionID ||
|
|
565
|
+
event.id !== callID ||
|
|
566
|
+
event.tool !== tool) {
|
|
567
|
+
blocked();
|
|
568
|
+
}
|
|
569
|
+
const descriptor = Object.getOwnPropertyDescriptor(event, "input");
|
|
570
|
+
if (!descriptor ||
|
|
571
|
+
!("value" in descriptor) ||
|
|
572
|
+
!descriptor.writable) {
|
|
573
|
+
blocked();
|
|
574
|
+
}
|
|
575
|
+
selected.apply(texts);
|
|
576
|
+
event.input = holder.input;
|
|
577
|
+
}, deadline);
|
|
578
|
+
}));
|
|
579
|
+
await ctx.tool.hook("execute.after", (event) => guard(async () => {
|
|
580
|
+
const tool = {
|
|
581
|
+
tool: event.tool,
|
|
582
|
+
sessionID: event.sessionID,
|
|
583
|
+
callID: event.id,
|
|
584
|
+
};
|
|
585
|
+
const runtime = settings.runtime(ctx.location.directory);
|
|
586
|
+
// Observation only: the action already executed. Guard records the
|
|
587
|
+
// result and the next model request masks it; deny cannot roll back.
|
|
588
|
+
if (event.status === "error") {
|
|
589
|
+
await runtime.observeToolAfter(tool, event.error.message);
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
const content = event.result.content;
|
|
593
|
+
const items = typeof content === "string"
|
|
594
|
+
? [content]
|
|
595
|
+
: (content
|
|
596
|
+
?.filter((part) => part.type === "text")
|
|
597
|
+
.map((part) => part.text) ?? []);
|
|
598
|
+
for (const text of items) {
|
|
599
|
+
await runtime.observeToolAfter(tool, text);
|
|
600
|
+
}
|
|
601
|
+
}));
|
|
602
|
+
const controller = new AbortController();
|
|
603
|
+
void (async () => {
|
|
604
|
+
try {
|
|
605
|
+
for await (const event of ctx.event.subscribe({
|
|
606
|
+
signal: controller.signal,
|
|
607
|
+
})) {
|
|
608
|
+
if (controller.signal.aborted) {
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
if (event.type === "session.deleted") {
|
|
612
|
+
localSessions.delete(event.data.sessionID);
|
|
613
|
+
await settings.forgetSession(event.data.sessionID, ctx.location.directory);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
catch {
|
|
618
|
+
/* No event payload or exception details go to logs. */
|
|
619
|
+
}
|
|
620
|
+
})();
|
|
621
|
+
return async () => {
|
|
622
|
+
controller.abort();
|
|
623
|
+
await release();
|
|
624
|
+
// OpenCode 2.1.4 scopes the subscription to the plugin and can ignore
|
|
625
|
+
// this AbortSignal. Waiting for that stream here deadlocks scope teardown.
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
catch (error) {
|
|
629
|
+
await release();
|
|
630
|
+
throw error;
|
|
631
|
+
}
|
|
632
|
+
},
|
|
633
|
+
};
|