modelos-secure 0.3.9__py3-none-win_amd64.whl
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.
- modelos_secure/__init__.py +0 -0
- modelos_secure/_cli/README.md +63 -0
- modelos_secure/_cli/assets/login-D0M0qeoI.js +1 -0
- modelos_secure/_cli/assets/login-UmFsH3I5.js +936 -0
- modelos_secure/_cli/cli.js +23653 -0
- modelos_secure/_cli/hook/clients.mjs +278 -0
- modelos_secure/_cli/hook/edge.mjs +411 -0
- modelos_secure/_cli/hook/hook.mjs +338 -0
- modelos_secure/_cli/hook/hosts.d.mts +2 -0
- modelos_secure/_cli/hook/hosts.mjs +26 -0
- modelos_secure/_cli/hook/loader.mjs +12 -0
- modelos_secure/_cli/hook/rewrite.mjs +95 -0
- modelos_secure/_cli/node.exe +4 -0
- modelos_secure/_cli/python/modelos_secure/__init__.py +0 -0
- modelos_secure/_cli/python/modelos_secure/clients.py +457 -0
- modelos_secure/_cli/python/modelos_secure/edge.py +365 -0
- modelos_secure/_cli/python/modelos_secure/hook.py +574 -0
- modelos_secure/_cli/python/requirements.txt +0 -0
- modelos_secure/_cli/python/sitecustomize.py +13 -0
- modelos_secure/_cli/wasm/tree-sitter-javascript.wasm +0 -0
- modelos_secure/_cli/wasm/tree-sitter-python.wasm +0 -0
- modelos_secure/_cli/wasm/tree-sitter-tsx.wasm +0 -0
- modelos_secure/_cli/wasm/tree-sitter-typescript.wasm +0 -0
- modelos_secure/_cli/wasm/web-tree-sitter.wasm +0 -0
- modelos_secure/_launcher.py +11 -0
- modelos_secure/clients.py +457 -0
- modelos_secure/edge.py +365 -0
- modelos_secure/hook.py +574 -0
- modelos_secure-0.3.9.dist-info/METADATA +71 -0
- modelos_secure-0.3.9.dist-info/RECORD +32 -0
- modelos_secure-0.3.9.dist-info/WHEEL +4 -0
- modelos_secure-0.3.9.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// The plain model clients: the prompt canary going in, and what the model asked for coming out.
|
|
2
|
+
// Whatever happens here, the app's own call goes ahead as if the hook were not there.
|
|
3
|
+
|
|
4
|
+
import { sdkContext } from "./edge.mjs";
|
|
5
|
+
|
|
6
|
+
function safely(work, fallback) {
|
|
7
|
+
try {
|
|
8
|
+
return work();
|
|
9
|
+
} catch {
|
|
10
|
+
return fallback;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function parseArgs(text) {
|
|
15
|
+
if (text && typeof text === "object") return text;
|
|
16
|
+
const parsed = safely(() => JSON.parse(text), undefined);
|
|
17
|
+
if (parsed && typeof parsed === "object") return parsed;
|
|
18
|
+
return parsed === undefined ? { raw: String(text) } : { value: parsed };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Adds a line to a message's content, whether it is text or a list of parts. */
|
|
22
|
+
function withLine(content, line) {
|
|
23
|
+
if (typeof content === "string") return `${content}\n${line}`;
|
|
24
|
+
if (Array.isArray(content)) return [...content, { type: "text", text: line }];
|
|
25
|
+
return content;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function canaryInMessages(messages, line) {
|
|
29
|
+
const index = messages.findIndex(
|
|
30
|
+
(m) => m?.role === "system" || m?.role === "developer",
|
|
31
|
+
);
|
|
32
|
+
if (index === -1) return messages;
|
|
33
|
+
const copy = [...messages];
|
|
34
|
+
copy[index] = {
|
|
35
|
+
...copy[index],
|
|
36
|
+
content: withLine(copy[index].content, line),
|
|
37
|
+
};
|
|
38
|
+
return copy;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function chatWithCanary(body, line) {
|
|
42
|
+
if (!line || !Array.isArray(body?.messages)) return body;
|
|
43
|
+
return { ...body, messages: canaryInMessages(body.messages, line) };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function responsesWithCanary(body, line) {
|
|
47
|
+
if (!line) return body;
|
|
48
|
+
if (typeof body?.instructions === "string")
|
|
49
|
+
return { ...body, instructions: `${body.instructions}\n${line}` };
|
|
50
|
+
if (Array.isArray(body?.input))
|
|
51
|
+
return { ...body, input: canaryInMessages(body.input, line) };
|
|
52
|
+
return body;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function readChat(completion) {
|
|
56
|
+
const calls = (completion.choices ?? []).flatMap((choice) =>
|
|
57
|
+
(choice.message?.tool_calls ?? []).flatMap((call) =>
|
|
58
|
+
call.function
|
|
59
|
+
? [
|
|
60
|
+
{
|
|
61
|
+
tool: call.function.name,
|
|
62
|
+
args: parseArgs(call.function.arguments),
|
|
63
|
+
},
|
|
64
|
+
]
|
|
65
|
+
: [],
|
|
66
|
+
),
|
|
67
|
+
);
|
|
68
|
+
return {
|
|
69
|
+
calls,
|
|
70
|
+
inputTokens: completion.usage?.prompt_tokens,
|
|
71
|
+
outputTokens: completion.usage?.completion_tokens,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function chatStreamReader() {
|
|
76
|
+
const calls = new Map();
|
|
77
|
+
let usage;
|
|
78
|
+
return {
|
|
79
|
+
add(chunk) {
|
|
80
|
+
usage = chunk.usage ?? usage;
|
|
81
|
+
for (const choice of chunk.choices ?? []) {
|
|
82
|
+
for (const delta of choice.delta?.tool_calls ?? []) {
|
|
83
|
+
const call = calls.get(delta.index) ?? { tool: "", text: "" };
|
|
84
|
+
call.tool += delta.function?.name ?? "";
|
|
85
|
+
call.text += delta.function?.arguments ?? "";
|
|
86
|
+
calls.set(delta.index, call);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
finish: () => ({
|
|
91
|
+
calls: [...calls.values()].map((c) => ({
|
|
92
|
+
tool: c.tool,
|
|
93
|
+
args: parseArgs(c.text),
|
|
94
|
+
})),
|
|
95
|
+
inputTokens: usage?.prompt_tokens,
|
|
96
|
+
outputTokens: usage?.completion_tokens,
|
|
97
|
+
}),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function readResponse(response) {
|
|
102
|
+
const calls = (response.output ?? [])
|
|
103
|
+
.filter((item) => item.type === "function_call")
|
|
104
|
+
.map((item) => ({ tool: item.name, args: parseArgs(item.arguments) }));
|
|
105
|
+
return {
|
|
106
|
+
calls,
|
|
107
|
+
inputTokens: response.usage?.input_tokens,
|
|
108
|
+
outputTokens: response.usage?.output_tokens,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function responsesStreamReader() {
|
|
113
|
+
let found = { calls: [] };
|
|
114
|
+
return {
|
|
115
|
+
add(event) {
|
|
116
|
+
if (event.type === "response.completed")
|
|
117
|
+
found = readResponse(event.response);
|
|
118
|
+
},
|
|
119
|
+
finish: () => found,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function genAiWithCanary(params, line) {
|
|
124
|
+
const instruction = params?.config?.systemInstruction;
|
|
125
|
+
if (!line || !instruction) return params;
|
|
126
|
+
let changed = instruction;
|
|
127
|
+
if (typeof instruction === "string") changed = `${instruction}\n${line}`;
|
|
128
|
+
else if (Array.isArray(instruction)) changed = [...instruction, line];
|
|
129
|
+
else if (Array.isArray(instruction.parts))
|
|
130
|
+
changed = { ...instruction, parts: [...instruction.parts, { text: line }] };
|
|
131
|
+
else if (typeof instruction.text === "string")
|
|
132
|
+
changed = { ...instruction, text: `${instruction.text}\n${line}` };
|
|
133
|
+
return {
|
|
134
|
+
...params,
|
|
135
|
+
config: { ...params.config, systemInstruction: changed },
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function readGenAi(response) {
|
|
140
|
+
const calls = (response.candidates?.[0]?.content?.parts ?? []).flatMap(
|
|
141
|
+
(part) =>
|
|
142
|
+
part.functionCall
|
|
143
|
+
? [{ tool: part.functionCall.name, args: part.functionCall.args ?? {} }]
|
|
144
|
+
: [],
|
|
145
|
+
);
|
|
146
|
+
return {
|
|
147
|
+
calls,
|
|
148
|
+
inputTokens: response.usageMetadata?.promptTokenCount,
|
|
149
|
+
outputTokens: response.usageMetadata?.candidatesTokenCount,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function genAiStreamReader() {
|
|
154
|
+
const calls = [];
|
|
155
|
+
let inputTokens;
|
|
156
|
+
let outputTokens;
|
|
157
|
+
return {
|
|
158
|
+
add(chunk) {
|
|
159
|
+
const found = readGenAi(chunk);
|
|
160
|
+
calls.push(...found.calls);
|
|
161
|
+
inputTokens = found.inputTokens ?? inputTokens;
|
|
162
|
+
outputTokens = found.outputTokens ?? outputTokens;
|
|
163
|
+
},
|
|
164
|
+
finish: () => ({ calls, inputTokens, outputTokens }),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function clientPatches({ canaryLine, seeModelCall }) {
|
|
169
|
+
const report = (model, found) =>
|
|
170
|
+
seeModelCall(model, found.calls, {
|
|
171
|
+
inputTokens: found.inputTokens,
|
|
172
|
+
outputTokens: found.outputTokens,
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
/** The app reads the stream as usual; what the model asked for is known once it has been read. */
|
|
176
|
+
function watchStream(stream, model, reader) {
|
|
177
|
+
// Taken once, because a generator's iterator is the generator itself: replacing its iterator
|
|
178
|
+
// method and then asking it again would call the replacement for ever.
|
|
179
|
+
const inner = stream[Symbol.asyncIterator]();
|
|
180
|
+
const watched = {
|
|
181
|
+
async next(...args) {
|
|
182
|
+
const step = await inner.next(...args);
|
|
183
|
+
if (step.done) safely(() => report(model, reader.finish()));
|
|
184
|
+
else safely(() => reader.add(step.value));
|
|
185
|
+
return step;
|
|
186
|
+
},
|
|
187
|
+
return: (...args) => inner.return(...args),
|
|
188
|
+
throw: (...args) => inner.throw(...args),
|
|
189
|
+
[Symbol.asyncIterator]() {
|
|
190
|
+
return watched;
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
if (typeof stream.next === "function") return watched;
|
|
194
|
+
stream[Symbol.asyncIterator] = () => watched;
|
|
195
|
+
return stream;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function patchOpenAiCreate(prototype, withCanary, read, streamReader) {
|
|
199
|
+
if (!prototype?.create) return;
|
|
200
|
+
const original = prototype.create;
|
|
201
|
+
prototype.create = function (body, options) {
|
|
202
|
+
const prepared = safely(() => withCanary(body, canaryLine()), body);
|
|
203
|
+
const result = sdkContext.run(true, () =>
|
|
204
|
+
original.call(this, prepared, options),
|
|
205
|
+
);
|
|
206
|
+
const see = (data) => {
|
|
207
|
+
if (body?.stream && data?.[Symbol.asyncIterator])
|
|
208
|
+
return safely(
|
|
209
|
+
() => watchStream(data, body.model, streamReader()),
|
|
210
|
+
data,
|
|
211
|
+
);
|
|
212
|
+
safely(() => report(body?.model, read(data)));
|
|
213
|
+
return data;
|
|
214
|
+
};
|
|
215
|
+
// The client's own promise sends nothing until asked, and has methods of its own, such as
|
|
216
|
+
// withResponse. Its chaining method keeps all of that, where a plain then would not.
|
|
217
|
+
if (typeof result?._thenUnwrap === "function")
|
|
218
|
+
return result._thenUnwrap(see);
|
|
219
|
+
return typeof result?.then === "function"
|
|
220
|
+
? result.then(see)
|
|
221
|
+
: see(result);
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function patchGenAi(Models) {
|
|
226
|
+
const prototype = Models?.prototype;
|
|
227
|
+
// generateContent is set up in the constructor. These two are what it calls, once for each
|
|
228
|
+
// real model call, so automatic function calling is counted call by call.
|
|
229
|
+
if (prototype?.generateContentInternal) {
|
|
230
|
+
const original = prototype.generateContentInternal;
|
|
231
|
+
prototype.generateContentInternal = async function (params) {
|
|
232
|
+
const prepared = safely(
|
|
233
|
+
() => genAiWithCanary(params, canaryLine()),
|
|
234
|
+
params,
|
|
235
|
+
);
|
|
236
|
+
const response = await sdkContext.run(true, () =>
|
|
237
|
+
original.call(this, prepared),
|
|
238
|
+
);
|
|
239
|
+
safely(() => report(params?.model, readGenAi(response)));
|
|
240
|
+
return response;
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
if (prototype?.generateContentStreamInternal) {
|
|
244
|
+
const original = prototype.generateContentStreamInternal;
|
|
245
|
+
prototype.generateContentStreamInternal = async function (params) {
|
|
246
|
+
const prepared = safely(
|
|
247
|
+
() => genAiWithCanary(params, canaryLine()),
|
|
248
|
+
params,
|
|
249
|
+
);
|
|
250
|
+
const stream = await sdkContext.run(true, () =>
|
|
251
|
+
original.call(this, prepared),
|
|
252
|
+
);
|
|
253
|
+
return safely(
|
|
254
|
+
() => watchStream(stream, params?.model, genAiStreamReader()),
|
|
255
|
+
stream,
|
|
256
|
+
);
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
patchOpenAiChat: (Completions) =>
|
|
263
|
+
patchOpenAiCreate(
|
|
264
|
+
Completions?.prototype,
|
|
265
|
+
chatWithCanary,
|
|
266
|
+
readChat,
|
|
267
|
+
chatStreamReader,
|
|
268
|
+
),
|
|
269
|
+
patchOpenAiResponses: (Responses) =>
|
|
270
|
+
patchOpenAiCreate(
|
|
271
|
+
Responses?.prototype,
|
|
272
|
+
responsesWithCanary,
|
|
273
|
+
readResponse,
|
|
274
|
+
responsesStreamReader,
|
|
275
|
+
),
|
|
276
|
+
patchGenAi,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
// The edge: every framework ends at an HTTP call to a model, so the hook watches there too, below
|
|
2
|
+
// whatever framework the app uses. During an attempt it sees each model call (the tools offered, the
|
|
3
|
+
// tool calls the model makes and their arguments), adds the prompt canary, plants attacker text into
|
|
4
|
+
// a tool result, and stops every other call that leaves the machine, answering it as if it worked and
|
|
5
|
+
// recording it. A tool known to run, such as a read against a staging copy, keeps its calls. Outside
|
|
6
|
+
// an attempt it does nothing. No dependencies, Node 20 and later. ADR 0062.
|
|
7
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
8
|
+
import http from "node:http";
|
|
9
|
+
import https from "node:https";
|
|
10
|
+
import { URL } from "node:url";
|
|
11
|
+
import { allowedHost } from "./hosts.mjs";
|
|
12
|
+
|
|
13
|
+
/** Which tool is running, so its own calls can be told apart from calls nobody declared. */
|
|
14
|
+
export const toolContext = new AsyncLocalStorage();
|
|
15
|
+
/** Set while a model client the hook already patches is calling, so the call is counted once. */
|
|
16
|
+
export const sdkContext = new AsyncLocalStorage();
|
|
17
|
+
|
|
18
|
+
const MODEL_PATHS = [
|
|
19
|
+
[/\/chat\/completions$/, "chat"],
|
|
20
|
+
[/\/responses$/, "responses"],
|
|
21
|
+
[/\/messages$/, "anthropic"],
|
|
22
|
+
[/:(stream)?[gG]enerateContent$/, "gemini"],
|
|
23
|
+
[/\/api\/chat$/, "ollama"],
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
function safely(work, fallback) {
|
|
27
|
+
try {
|
|
28
|
+
return work();
|
|
29
|
+
} catch {
|
|
30
|
+
return fallback;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const parseArgs = (value) =>
|
|
35
|
+
value && typeof value === "object"
|
|
36
|
+
? value
|
|
37
|
+
: safely(() => JSON.parse(value), { raw: String(value ?? "") });
|
|
38
|
+
|
|
39
|
+
/** The API a request speaks, from its path and body, or null when it is not a model call. */
|
|
40
|
+
export function modelShape(url, body) {
|
|
41
|
+
if (!body || typeof body !== "object") return null;
|
|
42
|
+
for (const [pattern, shape] of MODEL_PATHS)
|
|
43
|
+
if (pattern.test(url.pathname)) return shape;
|
|
44
|
+
if (Array.isArray(body.messages) && typeof body.model === "string")
|
|
45
|
+
return "chat";
|
|
46
|
+
if (Array.isArray(body.contents)) return "gemini";
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const withLine = (content, line) =>
|
|
51
|
+
typeof content === "string"
|
|
52
|
+
? `${content}\n${line}`
|
|
53
|
+
: Array.isArray(content)
|
|
54
|
+
? [...content, { type: "text", text: line }]
|
|
55
|
+
: content;
|
|
56
|
+
|
|
57
|
+
/** The canary goes into the system prompt, whatever the API calls it, once. */
|
|
58
|
+
function addCanary(shape, body, line) {
|
|
59
|
+
if (!line || JSON.stringify(body).includes(line)) return body;
|
|
60
|
+
if (shape === "chat" || shape === "ollama") {
|
|
61
|
+
const i = body.messages.findIndex(
|
|
62
|
+
(m) => m?.role === "system" || m?.role === "developer",
|
|
63
|
+
);
|
|
64
|
+
if (i === -1) return body;
|
|
65
|
+
const messages = [...body.messages];
|
|
66
|
+
messages[i] = {
|
|
67
|
+
...messages[i],
|
|
68
|
+
content: withLine(messages[i].content, line),
|
|
69
|
+
};
|
|
70
|
+
return { ...body, messages };
|
|
71
|
+
}
|
|
72
|
+
if (shape === "responses" && typeof body.instructions === "string")
|
|
73
|
+
return { ...body, instructions: `${body.instructions}\n${line}` };
|
|
74
|
+
if (shape === "anthropic" && body.system)
|
|
75
|
+
return {
|
|
76
|
+
...body,
|
|
77
|
+
system:
|
|
78
|
+
typeof body.system === "string"
|
|
79
|
+
? `${body.system}\n${line}`
|
|
80
|
+
: [...body.system, { type: "text", text: line }],
|
|
81
|
+
};
|
|
82
|
+
if (shape === "gemini") {
|
|
83
|
+
const instruction = body.systemInstruction ?? body.system_instruction;
|
|
84
|
+
if (!instruction?.parts) return body;
|
|
85
|
+
return {
|
|
86
|
+
...body,
|
|
87
|
+
systemInstruction: {
|
|
88
|
+
...instruction,
|
|
89
|
+
parts: [...instruction.parts, { text: line }],
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return body;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The tool results in a request, with the tool each answers, so a plant lands where it reads. */
|
|
97
|
+
function plantIntoResults(shape, body, tool, text) {
|
|
98
|
+
let delivered = false;
|
|
99
|
+
const named = new Map();
|
|
100
|
+
const plantText = (value) => {
|
|
101
|
+
delivered = true;
|
|
102
|
+
return typeof value === "string"
|
|
103
|
+
? `${value}\n\n${text}`
|
|
104
|
+
: Array.isArray(value)
|
|
105
|
+
? [...value, { type: "text", text }]
|
|
106
|
+
: { ...value, note: text };
|
|
107
|
+
};
|
|
108
|
+
if (shape === "chat" || shape === "ollama") {
|
|
109
|
+
for (const m of body.messages)
|
|
110
|
+
for (const call of m?.tool_calls ?? [])
|
|
111
|
+
named.set(call.id, call.function?.name);
|
|
112
|
+
const messages = body.messages.map((m) =>
|
|
113
|
+
m?.role === "tool" &&
|
|
114
|
+
(named.get(m.tool_call_id) === tool || m.name === tool)
|
|
115
|
+
? { ...m, content: plantText(m.content) }
|
|
116
|
+
: m,
|
|
117
|
+
);
|
|
118
|
+
return { body: { ...body, messages }, delivered };
|
|
119
|
+
}
|
|
120
|
+
if (shape === "anthropic") {
|
|
121
|
+
for (const m of body.messages ?? [])
|
|
122
|
+
for (const part of Array.isArray(m.content) ? m.content : [])
|
|
123
|
+
if (part.type === "tool_use") named.set(part.id, part.name);
|
|
124
|
+
const messages = (body.messages ?? []).map((m) =>
|
|
125
|
+
Array.isArray(m.content)
|
|
126
|
+
? {
|
|
127
|
+
...m,
|
|
128
|
+
content: m.content.map((part) =>
|
|
129
|
+
part.type === "tool_result" &&
|
|
130
|
+
named.get(part.tool_use_id) === tool
|
|
131
|
+
? { ...part, content: plantText(part.content) }
|
|
132
|
+
: part,
|
|
133
|
+
),
|
|
134
|
+
}
|
|
135
|
+
: m,
|
|
136
|
+
);
|
|
137
|
+
return { body: { ...body, messages }, delivered };
|
|
138
|
+
}
|
|
139
|
+
if (shape === "gemini") {
|
|
140
|
+
const contents = body.contents.map((c) => ({
|
|
141
|
+
...c,
|
|
142
|
+
parts: (c.parts ?? []).map((p) =>
|
|
143
|
+
p.functionResponse?.name === tool
|
|
144
|
+
? {
|
|
145
|
+
...p,
|
|
146
|
+
functionResponse: {
|
|
147
|
+
...p.functionResponse,
|
|
148
|
+
response: plantText(p.functionResponse.response ?? {}),
|
|
149
|
+
},
|
|
150
|
+
}
|
|
151
|
+
: p,
|
|
152
|
+
),
|
|
153
|
+
}));
|
|
154
|
+
return { body: { ...body, contents }, delivered };
|
|
155
|
+
}
|
|
156
|
+
if (shape === "responses" && Array.isArray(body.input)) {
|
|
157
|
+
for (const item of body.input)
|
|
158
|
+
if (item.type === "function_call") named.set(item.call_id, item.name);
|
|
159
|
+
const input = body.input.map((item) =>
|
|
160
|
+
item.type === "function_call_output" && named.get(item.call_id) === tool
|
|
161
|
+
? { ...item, output: plantText(item.output) }
|
|
162
|
+
: item,
|
|
163
|
+
);
|
|
164
|
+
return { body: { ...body, input }, delivered };
|
|
165
|
+
}
|
|
166
|
+
return { body, delivered };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** The tools offered and the calls asked for, in any of the APIs above. */
|
|
170
|
+
export function readModel(shape, body, answer) {
|
|
171
|
+
const offered = [
|
|
172
|
+
...(body.tools ?? []).map((t) => t.function?.name ?? t.name),
|
|
173
|
+
...(body.tools ?? []).flatMap((t) =>
|
|
174
|
+
(t.functionDeclarations ?? t.function_declarations ?? []).map(
|
|
175
|
+
(d) => d.name,
|
|
176
|
+
),
|
|
177
|
+
),
|
|
178
|
+
].filter(Boolean);
|
|
179
|
+
const calls = [];
|
|
180
|
+
for (const choice of answer.choices ?? [])
|
|
181
|
+
for (const call of choice.message?.tool_calls ?? [])
|
|
182
|
+
calls.push({
|
|
183
|
+
tool: call.function?.name,
|
|
184
|
+
args: parseArgs(call.function?.arguments),
|
|
185
|
+
});
|
|
186
|
+
for (const call of answer.message?.tool_calls ?? [])
|
|
187
|
+
calls.push({
|
|
188
|
+
tool: call.function?.name,
|
|
189
|
+
args: parseArgs(call.function?.arguments),
|
|
190
|
+
});
|
|
191
|
+
for (const item of answer.output ?? [])
|
|
192
|
+
if (item.type === "function_call")
|
|
193
|
+
calls.push({ tool: item.name, args: parseArgs(item.arguments) });
|
|
194
|
+
for (const part of Array.isArray(answer.content) ? answer.content : [])
|
|
195
|
+
if (part.type === "tool_use")
|
|
196
|
+
calls.push({ tool: part.name, args: part.input ?? {} });
|
|
197
|
+
for (const candidate of answer.candidates ?? [])
|
|
198
|
+
for (const part of candidate.content?.parts ?? [])
|
|
199
|
+
if (part.functionCall)
|
|
200
|
+
calls.push({
|
|
201
|
+
tool: part.functionCall.name,
|
|
202
|
+
args: part.functionCall.args ?? {},
|
|
203
|
+
});
|
|
204
|
+
const usage = answer.usage ?? answer.usageMetadata ?? {};
|
|
205
|
+
return {
|
|
206
|
+
model: body.model ?? answer.model,
|
|
207
|
+
offered,
|
|
208
|
+
calls: calls.filter((c) => c.tool),
|
|
209
|
+
inputTokens:
|
|
210
|
+
usage.prompt_tokens ?? usage.input_tokens ?? usage.promptTokenCount ?? 0,
|
|
211
|
+
outputTokens:
|
|
212
|
+
usage.completion_tokens ??
|
|
213
|
+
usage.output_tokens ??
|
|
214
|
+
usage.candidatesTokenCount ??
|
|
215
|
+
0,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** A streamed answer, read event by event into the same shape as a whole one. */
|
|
220
|
+
export function readStream(shape, body, text) {
|
|
221
|
+
const events = text
|
|
222
|
+
.split("\n")
|
|
223
|
+
.filter((l) => l.startsWith("data:"))
|
|
224
|
+
.map((l) => safely(() => JSON.parse(l.slice(5).trim()), null))
|
|
225
|
+
.filter(Boolean);
|
|
226
|
+
const pieces = new Map();
|
|
227
|
+
const whole = { choices: [], content: [], candidates: [], output: [] };
|
|
228
|
+
for (const event of events) {
|
|
229
|
+
for (const choice of event.choices ?? [])
|
|
230
|
+
for (const delta of choice.delta?.tool_calls ?? []) {
|
|
231
|
+
const piece = pieces.get(`c${delta.index}`) ?? { name: "", args: "" };
|
|
232
|
+
piece.name += delta.function?.name ?? "";
|
|
233
|
+
piece.args += delta.function?.arguments ?? "";
|
|
234
|
+
pieces.set(`c${delta.index}`, piece);
|
|
235
|
+
}
|
|
236
|
+
if (
|
|
237
|
+
event.type === "content_block_start" &&
|
|
238
|
+
event.content_block?.type === "tool_use"
|
|
239
|
+
)
|
|
240
|
+
pieces.set(`a${event.index}`, {
|
|
241
|
+
name: event.content_block.name,
|
|
242
|
+
args: "",
|
|
243
|
+
});
|
|
244
|
+
if (event.delta?.type === "input_json_delta") {
|
|
245
|
+
const piece = pieces.get(`a${event.index}`);
|
|
246
|
+
if (piece) piece.args += event.delta.partial_json ?? "";
|
|
247
|
+
}
|
|
248
|
+
if (event.candidates) whole.candidates.push(...event.candidates);
|
|
249
|
+
if (event.type === "response.completed")
|
|
250
|
+
whole.output = event.response?.output ?? [];
|
|
251
|
+
if (event.usage || event.usageMetadata)
|
|
252
|
+
whole.usage = event.usage ?? event.usageMetadata;
|
|
253
|
+
}
|
|
254
|
+
for (const piece of pieces.values())
|
|
255
|
+
whole.choices.push({
|
|
256
|
+
message: {
|
|
257
|
+
tool_calls: [{ function: { name: piece.name, arguments: piece.args } }],
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
return readModel(shape, body, whole);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Installs the edge. `hook` gives the attempt's state, the config's tools and ways in, what the
|
|
265
|
+
* person allowed through, and where a held call is recorded.
|
|
266
|
+
*/
|
|
267
|
+
export function installEdge(hook) {
|
|
268
|
+
const allowed = (host) => allowedHost(host, hook.allow);
|
|
269
|
+
/** Inside a tool the person said may run, its calls go through, to a staging copy. */
|
|
270
|
+
const insideRunningTool = () => hook.mode(toolContext.getStore()) === "run";
|
|
271
|
+
|
|
272
|
+
function hold(method, url, bytes) {
|
|
273
|
+
hook
|
|
274
|
+
.state()
|
|
275
|
+
.edge.push({ method, host: url.hostname, path: url.pathname, bytes });
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function seeModel(shape, body, answer, streamed, counted) {
|
|
279
|
+
const read = streamed
|
|
280
|
+
? readStream(shape, body, answer)
|
|
281
|
+
: readModel(shape, body, answer);
|
|
282
|
+
const state = hook.state();
|
|
283
|
+
if (!counted)
|
|
284
|
+
state.modelCalls.push({
|
|
285
|
+
model: read.model,
|
|
286
|
+
toolCalls: read.calls,
|
|
287
|
+
offered: read.offered,
|
|
288
|
+
inputTokens: read.inputTokens,
|
|
289
|
+
outputTokens: read.outputTokens,
|
|
290
|
+
edge: true,
|
|
291
|
+
});
|
|
292
|
+
// A call to a tool marked held that no function-level hook caught: what the model asked for
|
|
293
|
+
// is the evidence, and the edge stops what the tool does.
|
|
294
|
+
for (const call of read.calls)
|
|
295
|
+
if (
|
|
296
|
+
hook.mode(call.tool) === "hold" &&
|
|
297
|
+
!state.held.some((h) => h.tool === call.tool)
|
|
298
|
+
)
|
|
299
|
+
state.held.push({ tool: call.tool, args: call.args, at: "edge" });
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** What a model call carries in, during an attempt: the canary, and a plant at a tool result. */
|
|
303
|
+
function prepare(shape, body) {
|
|
304
|
+
const state = hook.state();
|
|
305
|
+
let prepared = addCanary(shape, body, hook.canaryLine());
|
|
306
|
+
const plant = state.attempt?.plant;
|
|
307
|
+
const way = plant && hook.wayIn(plant.wayIn);
|
|
308
|
+
if (plant && way?.function && !state.plantDelivered) {
|
|
309
|
+
const planted = plantIntoResults(
|
|
310
|
+
shape,
|
|
311
|
+
prepared,
|
|
312
|
+
way.function,
|
|
313
|
+
plant.text,
|
|
314
|
+
);
|
|
315
|
+
prepared = planted.body;
|
|
316
|
+
if (planted.delivered) state.plantDelivered = true;
|
|
317
|
+
}
|
|
318
|
+
return prepared;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const original = globalThis.fetch;
|
|
322
|
+
if (original)
|
|
323
|
+
globalThis.fetch = async function modelosSecureFetch(input, init) {
|
|
324
|
+
if (!hook.state().attempt) return original(input, init);
|
|
325
|
+
const url = new URL(
|
|
326
|
+
typeof input === "string" || input instanceof URL ? input : input.url,
|
|
327
|
+
);
|
|
328
|
+
const text = typeof init?.body === "string" ? init.body : undefined;
|
|
329
|
+
const body = text ? safely(() => JSON.parse(text), null) : null;
|
|
330
|
+
const shape = modelShape(url, body);
|
|
331
|
+
if (shape) {
|
|
332
|
+
const counted = Boolean(sdkContext.getStore());
|
|
333
|
+
const failed = (why) =>
|
|
334
|
+
hook.state().modelFailures?.push({ host: url.hostname, why });
|
|
335
|
+
const response = await original(input, {
|
|
336
|
+
...init,
|
|
337
|
+
body: JSON.stringify(prepare(shape, body)),
|
|
338
|
+
}).catch((error) => {
|
|
339
|
+
failed("no answer");
|
|
340
|
+
throw error;
|
|
341
|
+
});
|
|
342
|
+
if (!response.ok) failed(`status ${String(response.status)}`);
|
|
343
|
+
const streamed =
|
|
344
|
+
body.stream === true || /stream/i.test(url.pathname + url.search);
|
|
345
|
+
void response
|
|
346
|
+
.clone()
|
|
347
|
+
.text()
|
|
348
|
+
.then((answer) =>
|
|
349
|
+
safely(() =>
|
|
350
|
+
seeModel(
|
|
351
|
+
shape,
|
|
352
|
+
body,
|
|
353
|
+
streamed ? answer : JSON.parse(answer),
|
|
354
|
+
streamed,
|
|
355
|
+
counted,
|
|
356
|
+
),
|
|
357
|
+
),
|
|
358
|
+
)
|
|
359
|
+
.catch(() => undefined);
|
|
360
|
+
return response;
|
|
361
|
+
}
|
|
362
|
+
if (allowed(url.hostname) || insideRunningTool())
|
|
363
|
+
return original(input, init);
|
|
364
|
+
hold(init?.method ?? "GET", url, text?.length ?? 0);
|
|
365
|
+
return new globalThis.Response(
|
|
366
|
+
JSON.stringify({ status: "ok", id: `held-${Date.now()}` }),
|
|
367
|
+
{
|
|
368
|
+
status: 200,
|
|
369
|
+
headers: { "content-type": "application/json" },
|
|
370
|
+
},
|
|
371
|
+
);
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
// Clients built on http and https, such as axios and older SDKs: a call that would leave goes to
|
|
375
|
+
// the hook's own port instead, which answers as if it worked.
|
|
376
|
+
for (const module of [http, https]) {
|
|
377
|
+
for (const name of ["request", "get"]) {
|
|
378
|
+
const real = module[name];
|
|
379
|
+
module[name] = function modelosSecureRequest(...args) {
|
|
380
|
+
const state = hook.state();
|
|
381
|
+
const port = hook.controlPort();
|
|
382
|
+
if (!state.attempt || !port) return real.apply(this, args);
|
|
383
|
+
const target = safely(() => requestUrl(module, args), null);
|
|
384
|
+
if (!target || allowed(target.hostname) || insideRunningTool())
|
|
385
|
+
return real.apply(this, args);
|
|
386
|
+
hold(requestMethod(args), target, 0);
|
|
387
|
+
return http[name](
|
|
388
|
+
`http://127.0.0.1:${port}/held`,
|
|
389
|
+
...args.filter((a) => typeof a === "function"),
|
|
390
|
+
);
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function requestUrl(module, args) {
|
|
397
|
+
const [first, second] = args;
|
|
398
|
+
if (typeof first === "string" || first instanceof URL) return new URL(first);
|
|
399
|
+
const options = first ?? second ?? {};
|
|
400
|
+
const protocol = options.protocol ?? (module === https ? "https:" : "http:");
|
|
401
|
+
return new URL(
|
|
402
|
+
`${protocol}//${options.hostname ?? options.host ?? "localhost"}${options.path ?? "/"}`,
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function requestMethod(args) {
|
|
407
|
+
const options = args.find(
|
|
408
|
+
(a) => a && typeof a === "object" && !(a instanceof URL),
|
|
409
|
+
);
|
|
410
|
+
return options?.method ?? "GET";
|
|
411
|
+
}
|