@oxap/sdk 0.0.0-stage → 0.1.0-alpha.33
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 +575 -2
- package/dist/src/abort_signal.d.ts +5 -0
- package/dist/src/abort_signal.js +52 -0
- package/dist/src/auth_protocol.d.ts +67 -0
- package/dist/src/auth_protocol.js +113 -0
- package/dist/src/binary_resolver.d.ts +29 -0
- package/dist/src/binary_resolver.js +207 -0
- package/dist/src/execution_client.d.ts +23 -0
- package/dist/src/execution_client.js +7 -0
- package/dist/src/execution_types.d.ts +204 -0
- package/dist/src/execution_types.js +28 -0
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.js +30 -0
- package/dist/src/logger.d.ts +8 -0
- package/dist/src/logger.js +16 -0
- package/dist/src/models_types.d.ts +75 -0
- package/dist/src/models_types.js +14 -0
- package/dist/src/oap_client.d.ts +64 -0
- package/dist/src/oap_client.js +1023 -0
- package/dist/src/timeout_diagnostics.d.ts +22 -0
- package/dist/src/timeout_diagnostics.js +44 -0
- package/package.json +43 -3
|
@@ -0,0 +1,1023 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OapStdioTransport = exports.OapUnsupportedFeatureError = exports.OAP_PROVIDER_PROFILE = exports.OAP_AGENT_PROFILE = exports.OAP_VERSION = exports.OAP_PROTOCOL = void 0;
|
|
4
|
+
exports.createOapClient = createOapClient;
|
|
5
|
+
const lossless_json_1 = require("lossless-json");
|
|
6
|
+
const node_child_process_1 = require("node:child_process");
|
|
7
|
+
const node_readline_1 = require("node:readline");
|
|
8
|
+
const ulid_1 = require("ulid");
|
|
9
|
+
const binary_resolver_1 = require("./binary_resolver");
|
|
10
|
+
const abort_signal_1 = require("./abort_signal");
|
|
11
|
+
const auth_protocol_1 = require("./auth_protocol");
|
|
12
|
+
const execution_types_1 = require("./execution_types");
|
|
13
|
+
const models_types_1 = require("./models_types");
|
|
14
|
+
exports.OAP_PROTOCOL = "open-agent-protocol";
|
|
15
|
+
exports.OAP_VERSION = "0.1";
|
|
16
|
+
exports.OAP_AGENT_PROFILE = "open-agent-protocol.agent-control-core";
|
|
17
|
+
exports.OAP_PROVIDER_PROFILE = "open-agent-protocol.model-provider-core";
|
|
18
|
+
class OapUnsupportedFeatureError extends Error {
|
|
19
|
+
feature;
|
|
20
|
+
code = "unsupported_feature";
|
|
21
|
+
constructor(feature) {
|
|
22
|
+
super(`OAP endpoint does not support ${feature}`);
|
|
23
|
+
this.feature = feature;
|
|
24
|
+
this.name = "OapUnsupportedFeatureError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.OapUnsupportedFeatureError = OapUnsupportedFeatureError;
|
|
28
|
+
class FrameQueue {
|
|
29
|
+
frames = [];
|
|
30
|
+
waiting = [];
|
|
31
|
+
closed;
|
|
32
|
+
push(frame) {
|
|
33
|
+
const next = this.waiting.shift();
|
|
34
|
+
if (next) {
|
|
35
|
+
clearTimeout(next.timer);
|
|
36
|
+
if (next.signal && next.onAbort)
|
|
37
|
+
next.signal.removeEventListener("abort", next.onAbort);
|
|
38
|
+
next.resolve(frame);
|
|
39
|
+
}
|
|
40
|
+
else
|
|
41
|
+
this.frames.push(frame);
|
|
42
|
+
}
|
|
43
|
+
next(timeoutMs, signal) {
|
|
44
|
+
if (signal?.aborted)
|
|
45
|
+
return Promise.reject(abortError());
|
|
46
|
+
const frame = this.frames.shift();
|
|
47
|
+
if (frame)
|
|
48
|
+
return Promise.resolve(frame);
|
|
49
|
+
if (this.closed)
|
|
50
|
+
return Promise.reject(this.closed);
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
const waiter = { resolve, reject, timer: undefined, signal, onAbort: undefined };
|
|
53
|
+
const remove = () => { const index = this.waiting.indexOf(waiter); if (index >= 0)
|
|
54
|
+
this.waiting.splice(index, 1); if (signal && waiter.onAbort)
|
|
55
|
+
signal.removeEventListener("abort", waiter.onAbort); };
|
|
56
|
+
if (timeoutMs !== undefined) {
|
|
57
|
+
waiter.timer = setTimeout(() => {
|
|
58
|
+
remove();
|
|
59
|
+
reject(new Error(`timed out waiting for OAP event after ${timeoutMs}ms`));
|
|
60
|
+
}, timeoutMs);
|
|
61
|
+
}
|
|
62
|
+
if (signal) {
|
|
63
|
+
waiter.onAbort = () => { clearTimeout(waiter.timer); remove(); reject(abortError()); };
|
|
64
|
+
signal.addEventListener("abort", waiter.onAbort, { once: true });
|
|
65
|
+
}
|
|
66
|
+
this.waiting.push(waiter);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
close(error) {
|
|
70
|
+
this.closed = error;
|
|
71
|
+
for (const waiter of this.waiting.splice(0)) {
|
|
72
|
+
clearTimeout(waiter.timer);
|
|
73
|
+
if (waiter.signal && waiter.onAbort)
|
|
74
|
+
waiter.signal.removeEventListener("abort", waiter.onAbort);
|
|
75
|
+
waiter.reject(error);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
class OapStdioTransport {
|
|
80
|
+
command;
|
|
81
|
+
args;
|
|
82
|
+
options;
|
|
83
|
+
child;
|
|
84
|
+
reader;
|
|
85
|
+
pending = new Map();
|
|
86
|
+
subscriptions = new Map();
|
|
87
|
+
orphaned = new Map();
|
|
88
|
+
closed;
|
|
89
|
+
agentRevision;
|
|
90
|
+
agentEndpoint = "";
|
|
91
|
+
agentFeatures = new Set();
|
|
92
|
+
agentSource = "";
|
|
93
|
+
servesProvider = false;
|
|
94
|
+
constructor(command, args, options) {
|
|
95
|
+
this.command = command;
|
|
96
|
+
this.args = args;
|
|
97
|
+
this.options = options;
|
|
98
|
+
}
|
|
99
|
+
async connect() {
|
|
100
|
+
this.child = (0, node_child_process_1.spawn)(this.command, this.args, { cwd: this.options.cwd, env: this.options.env, stdio: "pipe" });
|
|
101
|
+
this.child.on("error", (error) => this.fail(error));
|
|
102
|
+
this.child.on("exit", (code, signal) => this.fail(new Error(`OAP process exited (code=${code}, signal=${signal})`)));
|
|
103
|
+
this.child.stderr.resume();
|
|
104
|
+
this.reader = (0, node_readline_1.createInterface)({ input: this.child.stdout });
|
|
105
|
+
this.reader.on("line", (line) => this.receive(line));
|
|
106
|
+
try {
|
|
107
|
+
const initialized = await this.request(exports.OAP_AGENT_PROFILE, "protocol.initialize.request", {
|
|
108
|
+
protocol_versions: [exports.OAP_VERSION], profiles: [exports.OAP_AGENT_PROFILE], participant: { id: "sdk" },
|
|
109
|
+
}, {}, this.options.handshakeTimeoutMs);
|
|
110
|
+
if (initialized.type !== "protocol.initialize.response" || initialized.payload.protocol_version !== exports.OAP_VERSION || initialized.payload.profile !== exports.OAP_AGENT_PROFILE) {
|
|
111
|
+
throw new models_types_1.MakaiProtocolError("OAP agent initialization did not negotiate agent-control-core", "protocol_mismatch");
|
|
112
|
+
}
|
|
113
|
+
const capabilities = await this.request(exports.OAP_AGENT_PROFILE, "capabilities.request", {}, {}, this.options.handshakeTimeoutMs);
|
|
114
|
+
if (capabilities.type !== "capabilities.response")
|
|
115
|
+
throw new models_types_1.MakaiProtocolError("OAP agent did not return capabilities", "protocol_mismatch");
|
|
116
|
+
this.agentRevision = capabilities.capability_revision;
|
|
117
|
+
this.agentEndpoint = isRecord(capabilities.payload.endpoint) ? str(capabilities.payload.endpoint.id) : "";
|
|
118
|
+
this.agentFeatures = new Set(isRecord(capabilities.payload.features)
|
|
119
|
+
? Object.entries(capabilities.payload.features).filter(([, support]) => !(isRecord(support) && support.level === "unavailable")).map(([name]) => name)
|
|
120
|
+
: []);
|
|
121
|
+
this.agentSource = declaredToolSource(capabilities.payload.sources);
|
|
122
|
+
const described = await this.request(exports.OAP_PROVIDER_PROFILE, "provider.describe.request", {}, {}, this.options.handshakeTimeoutMs).catch((error) => {
|
|
123
|
+
const refused = error instanceof OapUnsupportedFeatureError || (error instanceof models_types_1.MakaiProtocolError && (error.code === "invalid_request" || error.code === "unsupported_request"));
|
|
124
|
+
if (refused && !this.closed)
|
|
125
|
+
return undefined;
|
|
126
|
+
throw error;
|
|
127
|
+
});
|
|
128
|
+
if (described && described.type !== "provider.describe.response")
|
|
129
|
+
throw new models_types_1.MakaiProtocolError("OAP provider did not return a descriptor", "protocol_mismatch");
|
|
130
|
+
this.servesProvider = described !== undefined;
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
await this.close();
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
request(profile, type, payload, scope = {}, timeoutMs = this.options.timeoutMs) {
|
|
138
|
+
const id = (0, ulid_1.ulid)();
|
|
139
|
+
return new Promise((resolve, reject) => {
|
|
140
|
+
const timer = setTimeout(() => {
|
|
141
|
+
this.pending.delete(id);
|
|
142
|
+
reject(new Error(`timed out waiting for ${type} response after ${timeoutMs}ms`));
|
|
143
|
+
}, timeoutMs);
|
|
144
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
145
|
+
try {
|
|
146
|
+
this.send(profile, type, payload, { ...scope, id });
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
clearTimeout(timer);
|
|
150
|
+
this.pending.delete(id);
|
|
151
|
+
reject(error);
|
|
152
|
+
}
|
|
153
|
+
}).then((response) => {
|
|
154
|
+
if (response.type === "error.response")
|
|
155
|
+
throw protocolError(response);
|
|
156
|
+
return response;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
send(profile, type, payload, scope = {}) {
|
|
160
|
+
if (!this.child || this.closed)
|
|
161
|
+
throw this.closed ?? new Error("OAP transport is not connected");
|
|
162
|
+
const id = scope.id ?? (0, ulid_1.ulid)();
|
|
163
|
+
const envelope = {
|
|
164
|
+
protocol: exports.OAP_PROTOCOL, version: exports.OAP_VERSION, profile, type, id, payload,
|
|
165
|
+
...(scope.inference_id ? { inference_id: scope.inference_id } : {}),
|
|
166
|
+
...(scope.session_id ? { session_id: scope.session_id } : {}),
|
|
167
|
+
...(scope.run_id ? { run_id: scope.run_id } : {}),
|
|
168
|
+
...(profile === exports.OAP_AGENT_PROFILE && this.agentRevision && type !== "protocol.initialize.request" && type !== "capabilities.request" ? { capability_revision: this.agentRevision } : {}),
|
|
169
|
+
};
|
|
170
|
+
this.child.stdin.write(`${JSON.stringify(envelope)}\n`);
|
|
171
|
+
return id;
|
|
172
|
+
}
|
|
173
|
+
subscribe(profile, kind, id) {
|
|
174
|
+
const key = `${profile}|${kind}|${id}`;
|
|
175
|
+
if (this.subscriptions.has(key))
|
|
176
|
+
throw new Error(`OAP route already subscribed: ${key}`);
|
|
177
|
+
const queue = new FrameQueue();
|
|
178
|
+
if (this.closed) {
|
|
179
|
+
queue.close(this.closed);
|
|
180
|
+
return queue;
|
|
181
|
+
}
|
|
182
|
+
this.subscriptions.set(key, queue);
|
|
183
|
+
for (const frame of this.orphaned.get(key) ?? [])
|
|
184
|
+
queue.push(frame);
|
|
185
|
+
this.orphaned.delete(key);
|
|
186
|
+
return queue;
|
|
187
|
+
}
|
|
188
|
+
unsubscribe(profile, kind, id) {
|
|
189
|
+
this.subscriptions.delete(`${profile}|${kind}|${id}`);
|
|
190
|
+
}
|
|
191
|
+
receive(line) {
|
|
192
|
+
let frame;
|
|
193
|
+
try {
|
|
194
|
+
const decoded = (0, lossless_json_1.parse)(line);
|
|
195
|
+
if (isRecord(decoded) && isRecord(decoded.payload) && Object.hasOwn(decoded.payload, "usage")) {
|
|
196
|
+
decoded.payload.usage = wireUsage(decoded.payload.usage);
|
|
197
|
+
}
|
|
198
|
+
frame = ordinaryNumbers(decoded);
|
|
199
|
+
if (frame.protocol !== exports.OAP_PROTOCOL || frame.version !== exports.OAP_VERSION || ![exports.OAP_AGENT_PROFILE, exports.OAP_PROVIDER_PROFILE].includes(frame.profile) || typeof frame.type !== "string" || typeof frame.id !== "string" || !isRecord(frame.payload)) {
|
|
200
|
+
throw new Error("invalid OAP envelope");
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
this.fail(error instanceof models_types_1.MakaiProtocolError ? error : new models_types_1.MakaiProtocolError("invalid OAP frame", "malformed_response"));
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (frame.in_reply_to) {
|
|
208
|
+
const pending = this.pending.get(frame.in_reply_to);
|
|
209
|
+
if (pending) {
|
|
210
|
+
this.pending.delete(frame.in_reply_to);
|
|
211
|
+
clearTimeout(pending.timer);
|
|
212
|
+
pending.resolve(frame);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const flowId = frame.profile === exports.OAP_AGENT_PROFILE ? str(frame.payload.flow_id) : "";
|
|
217
|
+
const kind = frame.inference_id ? "inference" : frame.session_id ? "session" : flowId ? "flow" : undefined;
|
|
218
|
+
const routeId = frame.inference_id ?? frame.session_id ?? flowId;
|
|
219
|
+
if (kind && routeId) {
|
|
220
|
+
const key = `${frame.profile}|${kind}|${routeId}`;
|
|
221
|
+
const queue = this.subscriptions.get(key);
|
|
222
|
+
if (queue)
|
|
223
|
+
queue.push(frame);
|
|
224
|
+
else if (this.orphaned.has(key) || this.orphaned.size < 64) {
|
|
225
|
+
const waiting = this.orphaned.get(key) ?? [];
|
|
226
|
+
if (waiting.length < 256)
|
|
227
|
+
waiting.push(frame);
|
|
228
|
+
this.orphaned.set(key, waiting);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
fail(error) {
|
|
233
|
+
if (this.closed)
|
|
234
|
+
return;
|
|
235
|
+
this.closed = error;
|
|
236
|
+
for (const pending of this.pending.values()) {
|
|
237
|
+
clearTimeout(pending.timer);
|
|
238
|
+
pending.reject(error);
|
|
239
|
+
}
|
|
240
|
+
this.pending.clear();
|
|
241
|
+
for (const queue of this.subscriptions.values())
|
|
242
|
+
queue.close(error);
|
|
243
|
+
}
|
|
244
|
+
async close() {
|
|
245
|
+
const child = this.child;
|
|
246
|
+
if (!child)
|
|
247
|
+
return;
|
|
248
|
+
this.child = undefined;
|
|
249
|
+
this.reader?.close();
|
|
250
|
+
this.fail(new Error("OAP transport closed"));
|
|
251
|
+
child.stdin.end();
|
|
252
|
+
if (child.exitCode === null && child.signalCode === null) {
|
|
253
|
+
await new Promise((resolve) => {
|
|
254
|
+
const timer = setTimeout(() => { child.kill(); resolve(); }, 500);
|
|
255
|
+
child.once("exit", () => { clearTimeout(timer); resolve(); });
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
exports.OapStdioTransport = OapStdioTransport;
|
|
261
|
+
function isRecord(value) { return value !== null && typeof value === "object" && !Array.isArray(value); }
|
|
262
|
+
function abortError() { const error = new Error("operation aborted"); error.name = "AbortError"; return error; }
|
|
263
|
+
function str(value) { return typeof value === "string" ? value : ""; }
|
|
264
|
+
function declaredToolSource(sources) {
|
|
265
|
+
if (!Array.isArray(sources))
|
|
266
|
+
return "";
|
|
267
|
+
let first = "";
|
|
268
|
+
for (const source of sources) {
|
|
269
|
+
if (!isRecord(source))
|
|
270
|
+
continue;
|
|
271
|
+
const id = str(source.id);
|
|
272
|
+
if (!id)
|
|
273
|
+
continue;
|
|
274
|
+
if (source.kind === "native")
|
|
275
|
+
return id;
|
|
276
|
+
if (!first)
|
|
277
|
+
first = id;
|
|
278
|
+
}
|
|
279
|
+
return first;
|
|
280
|
+
}
|
|
281
|
+
function payload(frame) { return frame.payload; }
|
|
282
|
+
function protocolError(frame) {
|
|
283
|
+
const error = isRecord(frame.payload.error) ? frame.payload.error : frame.payload;
|
|
284
|
+
if (error.code === "unsupported_feature")
|
|
285
|
+
return new OapUnsupportedFeatureError(str(error.message) || frame.type);
|
|
286
|
+
return new models_types_1.MakaiProtocolError(str(error.message) || `${frame.type} failed`, str(error.code) || undefined);
|
|
287
|
+
}
|
|
288
|
+
function unsupported(feature) { throw new OapUnsupportedFeatureError(feature); }
|
|
289
|
+
function authFailure(error) {
|
|
290
|
+
const code = error instanceof execution_types_1.MakaiStreamError || error instanceof models_types_1.MakaiProtocolError ? error.code : undefined;
|
|
291
|
+
return code === "auth_required" || code === "credential_missing" || code === "credential_expired" || code === "credential_rejected" || code === "auth_expired";
|
|
292
|
+
}
|
|
293
|
+
function providerFromRef(modelRef) {
|
|
294
|
+
const slash = modelRef.indexOf("/");
|
|
295
|
+
return slash > 0 ? modelRef.slice(0, slash) : undefined;
|
|
296
|
+
}
|
|
297
|
+
function toOapMessages(messages) {
|
|
298
|
+
return messages.map((message) => {
|
|
299
|
+
const content = typeof message.content === "string" ? message.content : message.content.map((part) => {
|
|
300
|
+
switch (part.type) {
|
|
301
|
+
case "text": return { type: "text", text: part.text };
|
|
302
|
+
case "thinking": return { type: "reasoning", reasoning: part.thinking, ...(part.thinking_signature ? { carry: part.thinking_signature } : {}) };
|
|
303
|
+
case "tool_call": return { type: "tool_call", tool_call_id: part.tool_call_id, name: part.name, arguments_json: JSON.parse(part.arguments_json), ...(part.carry ? { carry: part.carry } : {}) };
|
|
304
|
+
case "tool_result": return { type: "tool_result", tool_call_id: part.tool_call_id, result: part.content, is_error: part.is_error ?? false };
|
|
305
|
+
case "image": return { type: "image", image: { data: part.data, media_type: part.mime_type } };
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
if (message.role === "tool") {
|
|
309
|
+
if (!message.tool_call_id)
|
|
310
|
+
throw new models_types_1.MakaiProtocolError("tool message requires tool_call_id on OAP", "invalid_request");
|
|
311
|
+
if (Array.isArray(content) && content.length === 1 && content[0]?.type === "tool_result") {
|
|
312
|
+
if (content[0].tool_call_id !== message.tool_call_id)
|
|
313
|
+
throw new models_types_1.MakaiProtocolError("tool result id disagrees with message tool_call_id", "invalid_request");
|
|
314
|
+
return { role: "tool", content };
|
|
315
|
+
}
|
|
316
|
+
return { role: "tool", content: [{ type: "tool_result", tool_call_id: message.tool_call_id, result: content }] };
|
|
317
|
+
}
|
|
318
|
+
return { role: message.role, content };
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
function fromOapContent(content) {
|
|
322
|
+
if (typeof content === "string")
|
|
323
|
+
return content;
|
|
324
|
+
if (!Array.isArray(content))
|
|
325
|
+
return "";
|
|
326
|
+
return content.flatMap((raw) => {
|
|
327
|
+
if (!isRecord(raw))
|
|
328
|
+
return [];
|
|
329
|
+
if (raw.type === "text")
|
|
330
|
+
return [{ type: "text", text: str(raw.text) }];
|
|
331
|
+
if (raw.type === "reasoning")
|
|
332
|
+
return [{ type: "thinking", thinking: str(raw.reasoning), ...(typeof raw.carry === "string" ? { thinking_signature: raw.carry } : {}) }];
|
|
333
|
+
if (raw.type === "tool_call")
|
|
334
|
+
return [{ type: "tool_call", tool_call_id: str(raw.tool_call_id), name: str(raw.name), arguments_json: JSON.stringify(raw.arguments_json ?? {}), ...(typeof raw.carry === "string" ? { carry: raw.carry } : {}) }];
|
|
335
|
+
if (raw.type === "tool_result")
|
|
336
|
+
return [{ type: "tool_result", tool_call_id: str(raw.tool_call_id), tool_name: "", content: typeof raw.result === "string" ? raw.result : JSON.stringify(raw.result ?? ""), is_error: raw.is_error === true }];
|
|
337
|
+
if (raw.type === "image") {
|
|
338
|
+
const value = isRecord(raw.image) ? raw.image : {};
|
|
339
|
+
if (typeof value.data === "string" && typeof value.media_type === "string")
|
|
340
|
+
return [{ type: "image", data: value.data, mime_type: value.media_type }];
|
|
341
|
+
unsupported("URL image content in an OAP response");
|
|
342
|
+
}
|
|
343
|
+
unsupported(`OAP response content part ${str(raw.type) || "unknown"}`);
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
function parseInputSchema(raw) {
|
|
347
|
+
try {
|
|
348
|
+
return JSON.parse(raw);
|
|
349
|
+
}
|
|
350
|
+
catch {
|
|
351
|
+
throw new models_types_1.MakaiProtocolError("tool parameters_schema_json is not valid JSON", "invalid_request");
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
const USAGE_FIELDS = {
|
|
355
|
+
input_tokens: "input", output_tokens: "output", total_tokens: "total",
|
|
356
|
+
cache_read_input_tokens: "cache_read", cache_write_input_tokens: "cache_write", reasoning_tokens: "reasoning",
|
|
357
|
+
};
|
|
358
|
+
function wireUsage(value) {
|
|
359
|
+
if (!isRecord(value) || (0, lossless_json_1.isLosslessNumber)(value))
|
|
360
|
+
throw new models_types_1.MakaiProtocolError("OAP usage must be an object", "malformed_response");
|
|
361
|
+
const result = {};
|
|
362
|
+
const unrepresentable = [];
|
|
363
|
+
for (const field of Object.keys(USAGE_FIELDS)) {
|
|
364
|
+
if (!Object.hasOwn(value, field))
|
|
365
|
+
continue;
|
|
366
|
+
const raw = value[field];
|
|
367
|
+
if (!(0, lossless_json_1.isLosslessNumber)(raw) || !/^(0|[1-9][0-9]*)$/.test(raw.value) || BigInt(raw.value) > 18446744073709551615n) {
|
|
368
|
+
throw new models_types_1.MakaiProtocolError(`OAP usage ${field} must be an unsigned 64-bit integer`, "malformed_response");
|
|
369
|
+
}
|
|
370
|
+
if (BigInt(raw.value) > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
371
|
+
unrepresentable.push({ code: "unrepresentable_count", field, value: raw.value });
|
|
372
|
+
}
|
|
373
|
+
else
|
|
374
|
+
result[field] = Number(raw.value);
|
|
375
|
+
}
|
|
376
|
+
if (unrepresentable.length)
|
|
377
|
+
result.unrepresentable_counts = unrepresentable;
|
|
378
|
+
return result;
|
|
379
|
+
}
|
|
380
|
+
function ordinaryNumbers(value) {
|
|
381
|
+
if ((0, lossless_json_1.isLosslessNumber)(value))
|
|
382
|
+
return Number(value.value);
|
|
383
|
+
if (Array.isArray(value))
|
|
384
|
+
return value.map(ordinaryNumbers);
|
|
385
|
+
if (isRecord(value))
|
|
386
|
+
return Object.fromEntries(Object.entries(value).map(([key, member]) => [key, ordinaryNumbers(member)]));
|
|
387
|
+
return value;
|
|
388
|
+
}
|
|
389
|
+
function usage(value) {
|
|
390
|
+
if (!isRecord(value))
|
|
391
|
+
return undefined;
|
|
392
|
+
const result = {};
|
|
393
|
+
for (const [wire, field] of Object.entries(USAGE_FIELDS)) {
|
|
394
|
+
if (typeof value[wire] === "number")
|
|
395
|
+
result[field] = value[wire];
|
|
396
|
+
}
|
|
397
|
+
if (Array.isArray(value.unrepresentable_counts))
|
|
398
|
+
result.unrepresentable_counts = value.unrepresentable_counts;
|
|
399
|
+
return result;
|
|
400
|
+
}
|
|
401
|
+
function modelCost(raw) {
|
|
402
|
+
return {
|
|
403
|
+
...(typeof raw.input === "number" ? { input: raw.input } : {}),
|
|
404
|
+
...(typeof raw.output === "number" ? { output: raw.output } : {}),
|
|
405
|
+
...(typeof raw.cache_read === "number" ? { cache_read: raw.cache_read } : {}),
|
|
406
|
+
...(typeof raw.cache_write === "number" ? { cache_write: raw.cache_write } : {}),
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
function modelParts(modelRef) {
|
|
410
|
+
const match = /^([^/]+)\/([^@]+)@(.+)$/.exec(modelRef);
|
|
411
|
+
if (!match)
|
|
412
|
+
throw new TypeError("model_ref must be provider_id/wire@model_id");
|
|
413
|
+
return { provider_id: match[1], api: match[2], model_id: match[3] };
|
|
414
|
+
}
|
|
415
|
+
class OapProviderApi {
|
|
416
|
+
transport;
|
|
417
|
+
idleTimeoutMs;
|
|
418
|
+
authRetryPolicy;
|
|
419
|
+
auth;
|
|
420
|
+
authHandlers;
|
|
421
|
+
constructor(transport, idleTimeoutMs, authRetryPolicy, auth, authHandlers) {
|
|
422
|
+
this.transport = transport;
|
|
423
|
+
this.idleTimeoutMs = idleTimeoutMs;
|
|
424
|
+
this.authRetryPolicy = authRetryPolicy;
|
|
425
|
+
this.auth = auth;
|
|
426
|
+
this.authHandlers = authHandlers;
|
|
427
|
+
}
|
|
428
|
+
async complete(request) {
|
|
429
|
+
if (!this.transport.servesProvider)
|
|
430
|
+
unsupported("the model-provider-core profile");
|
|
431
|
+
let final;
|
|
432
|
+
let text = "";
|
|
433
|
+
for await (const event of this.stream(request)) {
|
|
434
|
+
if (event.type === "text_delta")
|
|
435
|
+
text += event.delta;
|
|
436
|
+
if (event.type === "message_end") {
|
|
437
|
+
const ref = modelParts(request.model_ref);
|
|
438
|
+
final = { message: event.message ?? { role: "assistant", content: text }, usage: event.usage, stop_reason: event.stop_reason, ...ref };
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (!final)
|
|
442
|
+
throw new execution_types_1.MakaiStreamError("inference ended without inference.completed", { kind: "transport_error" });
|
|
443
|
+
return final;
|
|
444
|
+
}
|
|
445
|
+
async *stream(request) {
|
|
446
|
+
let retried = false;
|
|
447
|
+
for (;;) {
|
|
448
|
+
let yieldedContent = false;
|
|
449
|
+
const preamble = [];
|
|
450
|
+
try {
|
|
451
|
+
for await (const event of this.streamOnce(request)) {
|
|
452
|
+
if (!yieldedContent && event.type === "message_start") {
|
|
453
|
+
preamble.push(event);
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
if (!yieldedContent) {
|
|
457
|
+
yieldedContent = true;
|
|
458
|
+
for (const earlier of preamble)
|
|
459
|
+
yield earlier;
|
|
460
|
+
}
|
|
461
|
+
yield event;
|
|
462
|
+
}
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
catch (error) {
|
|
466
|
+
if (authFailure(error)) {
|
|
467
|
+
const providerId = providerFromRef(request.model_ref);
|
|
468
|
+
if (!yieldedContent && !retried && providerId && (request.options?.auth_retry_policy ?? this.authRetryPolicy) === "auto_once" && this.auth) {
|
|
469
|
+
retried = true;
|
|
470
|
+
try {
|
|
471
|
+
await this.auth.login(providerId, this.authHandlers, { signal: request.options?.signal });
|
|
472
|
+
}
|
|
473
|
+
catch (loginError) {
|
|
474
|
+
if (request.options?.signal?.aborted)
|
|
475
|
+
throw new execution_types_1.MakaiStreamError("provider inference aborted", { kind: "aborted" });
|
|
476
|
+
throw new execution_types_1.MakaiAuthRequiredError(providerId, error instanceof Error ? error.message : "authentication required");
|
|
477
|
+
}
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
if (providerId)
|
|
481
|
+
throw new execution_types_1.MakaiAuthRequiredError(providerId, error instanceof Error ? error.message : "authentication required");
|
|
482
|
+
}
|
|
483
|
+
throw error;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
async *streamOnce(request) {
|
|
488
|
+
if (!this.transport.servesProvider)
|
|
489
|
+
unsupported("the model-provider-core profile");
|
|
490
|
+
const signal = request.options?.signal;
|
|
491
|
+
if (signal?.aborted)
|
|
492
|
+
throw abortError();
|
|
493
|
+
if (request.tools?.some((tool) => tool.execute))
|
|
494
|
+
unsupported("client-executed tools on direct provider inference");
|
|
495
|
+
const create = await this.transport.request(exports.OAP_PROVIDER_PROFILE, "inference.create.request", {
|
|
496
|
+
model_ref: request.model_ref, messages: toOapMessages(request.messages), stream: true,
|
|
497
|
+
...(request.tools?.length ? { tools: request.tools.map((tool) => ({ name: tool.name, description: tool.description, input_schema: parseInputSchema(tool.parameters_schema_json) })) } : {}),
|
|
498
|
+
...(request.options?.max_tokens !== undefined ? { max_output_tokens: request.options.max_tokens } : {}),
|
|
499
|
+
...(request.options?.temperature !== undefined ? { temperature: request.options.temperature } : {}),
|
|
500
|
+
...(request.options?.reasoning_effort && request.options.reasoning_effort !== "off" ? { reasoning: { enabled: true, effort: request.options.reasoning_effort } } : {}),
|
|
501
|
+
...(request.options?.metadata ? { metadata: request.options.metadata } : {}),
|
|
502
|
+
});
|
|
503
|
+
if (create.type !== "inference.create.response")
|
|
504
|
+
throw new models_types_1.MakaiProtocolError(`unexpected ${create.type}`, "malformed_response");
|
|
505
|
+
if (create.payload.accepted !== true) {
|
|
506
|
+
const err = isRecord(create.payload.error) ? create.payload.error : {};
|
|
507
|
+
throw new execution_types_1.MakaiStreamError(str(err.message) || "inference rejected", { kind: "provider_error", code: str(err.code) || undefined });
|
|
508
|
+
}
|
|
509
|
+
const inferenceId = create.inference_id;
|
|
510
|
+
if (!inferenceId)
|
|
511
|
+
throw new models_types_1.MakaiProtocolError("inference.create.response has no inference_id", "malformed_response");
|
|
512
|
+
const queue = this.transport.subscribe(exports.OAP_PROVIDER_PROFILE, "inference", inferenceId);
|
|
513
|
+
const ref = modelParts(request.model_ref);
|
|
514
|
+
const partKinds = new Map();
|
|
515
|
+
let settled = false;
|
|
516
|
+
try {
|
|
517
|
+
while (true) {
|
|
518
|
+
if (signal?.aborted)
|
|
519
|
+
throw abortError();
|
|
520
|
+
const frame = await queue.next(this.idleTimeoutMs, signal);
|
|
521
|
+
const data = payload(frame);
|
|
522
|
+
if (frame.type === "inference.started") {
|
|
523
|
+
yield { type: "message_start", ...ref };
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
if (frame.type === "inference.part.started") {
|
|
527
|
+
if (typeof data.part_index === "number")
|
|
528
|
+
partKinds.set(data.part_index, str(data.part_kind));
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
if (frame.type === "inference.part.delta") {
|
|
532
|
+
const kind = partKinds.get(typeof data.part_index === "number" ? data.part_index : -1);
|
|
533
|
+
if (kind === "reasoning")
|
|
534
|
+
yield { type: "thinking_delta", delta: str(data.delta) };
|
|
535
|
+
else if (kind === "text")
|
|
536
|
+
yield { type: "text_delta", delta: str(data.delta) };
|
|
537
|
+
else if (kind !== "tool_call")
|
|
538
|
+
throw new models_types_1.MakaiProtocolError("inference delta has no known part kind", "malformed_response");
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
if (frame.type === "inference.part.ended" && data.part_kind === "tool_call" && isRecord(data.tool_call)) {
|
|
542
|
+
yield { type: "tool_call", tool_call_id: str(data.tool_call.tool_call_id), name: str(data.tool_call.name), arguments_json: JSON.stringify(data.tool_call.arguments_json ?? {}) };
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
if (frame.type === "inference.completed") {
|
|
546
|
+
settled = true;
|
|
547
|
+
const finalMessage = isRecord(data.message) ? data.message : {};
|
|
548
|
+
yield { type: "message_end", usage: usage(data.usage), stop_reason: str(data.stop_reason), message: { role: "assistant", content: fromOapContent(finalMessage.content) } };
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
if (frame.type === "inference.failed") {
|
|
552
|
+
settled = true;
|
|
553
|
+
const err = isRecord(data.error) ? data.error : {};
|
|
554
|
+
throw new execution_types_1.MakaiStreamError(str(err.message) || "inference failed", { kind: "provider_error", code: str(err.code) || undefined });
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
finally {
|
|
559
|
+
this.transport.unsubscribe(exports.OAP_PROVIDER_PROFILE, "inference", inferenceId);
|
|
560
|
+
if (!settled) {
|
|
561
|
+
try {
|
|
562
|
+
this.transport.send(exports.OAP_PROVIDER_PROFILE, "inference.cancel.request", { reason: "client stopped" }, { inference_id: inferenceId });
|
|
563
|
+
}
|
|
564
|
+
catch { }
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
function oapModelLifecycle(value) {
|
|
570
|
+
if (value === undefined) {
|
|
571
|
+
return {};
|
|
572
|
+
}
|
|
573
|
+
if (value === "stable" || value === "preview" || value === "deprecated") {
|
|
574
|
+
return { lifecycle: value };
|
|
575
|
+
}
|
|
576
|
+
throw new models_types_1.MakaiProtocolError("provider model entry lifecycle must be stable, preview or deprecated when present", "malformed_response");
|
|
577
|
+
}
|
|
578
|
+
function oapModelAuthStatus(value) {
|
|
579
|
+
if (value === undefined) {
|
|
580
|
+
return { auth_status: "unknown" };
|
|
581
|
+
}
|
|
582
|
+
if (typeof value === "string" && auth_protocol_1.AUTH_STATUSES.includes(value)) {
|
|
583
|
+
return { auth_status: value };
|
|
584
|
+
}
|
|
585
|
+
throw new models_types_1.MakaiProtocolError("provider model entry auth_status must be one of authenticated, login_required, expired, refreshing, login_in_progress, failed or unknown when present", "malformed_response");
|
|
586
|
+
}
|
|
587
|
+
function oapModelSource(value) {
|
|
588
|
+
if (value === undefined) {
|
|
589
|
+
return {};
|
|
590
|
+
}
|
|
591
|
+
if (value === "discovered") {
|
|
592
|
+
return { source: "dynamic" };
|
|
593
|
+
}
|
|
594
|
+
if (value === "fallback") {
|
|
595
|
+
return { source: "static_fallback" };
|
|
596
|
+
}
|
|
597
|
+
throw new models_types_1.MakaiProtocolError("provider model entry source must be discovered or fallback when present", "malformed_response");
|
|
598
|
+
}
|
|
599
|
+
class OapModelsApi {
|
|
600
|
+
transport;
|
|
601
|
+
constructor(transport) {
|
|
602
|
+
this.transport = transport;
|
|
603
|
+
}
|
|
604
|
+
async list(request = {}) {
|
|
605
|
+
if (!this.transport.servesProvider)
|
|
606
|
+
unsupported("the model-provider-core profile");
|
|
607
|
+
const frame = await this.transport.request(exports.OAP_PROVIDER_PROFILE, "provider.models.list.request", request.provider_id ? { provider_id: request.provider_id } : {});
|
|
608
|
+
if (frame.type !== "provider.models.list.response" || !Array.isArray(frame.payload.models))
|
|
609
|
+
throw new models_types_1.MakaiProtocolError("invalid provider.models.list.response", "malformed_response");
|
|
610
|
+
const models = frame.payload.models.map((raw) => {
|
|
611
|
+
if (!isRecord(raw))
|
|
612
|
+
throw new models_types_1.MakaiProtocolError("provider model entry must be an object", "malformed_response");
|
|
613
|
+
return ({
|
|
614
|
+
model_ref: str(raw.model_ref), model_id: str(raw.model_id), display_name: str(raw.display_name) || str(raw.model_id),
|
|
615
|
+
provider_id: str(raw.provider_id), api: str(raw.wire), ...oapModelAuthStatus(raw.auth_status),
|
|
616
|
+
...oapModelLifecycle(raw.lifecycle),
|
|
617
|
+
capabilities: Array.isArray(raw.capabilities) ? raw.capabilities.filter((v) => typeof v === "string") : [],
|
|
618
|
+
...oapModelSource(raw.source),
|
|
619
|
+
...(typeof raw.context_window === "number" ? { context_window: raw.context_window } : {}),
|
|
620
|
+
...(typeof raw.max_output_tokens === "number" ? { max_output_tokens: raw.max_output_tokens } : {}),
|
|
621
|
+
...(isRecord(raw.cost) ? { cost: modelCost(raw.cost) } : {}),
|
|
622
|
+
...(Array.isArray(raw.input_modalities) ? { input_modalities: raw.input_modalities.filter((v) => typeof v === "string") } : {}),
|
|
623
|
+
...(Array.isArray(raw.output_modalities) ? { output_modalities: raw.output_modalities.filter((v) => typeof v === "string") } : {}),
|
|
624
|
+
...(Array.isArray(raw.reasoning_levels) ? { reasoning_levels: raw.reasoning_levels.filter((v) => typeof v === "string") } : {}),
|
|
625
|
+
...(typeof raw.release_date === "string" ? { release_date: raw.release_date } : {}),
|
|
626
|
+
...(typeof raw.family === "string" ? { family: raw.family } : {}),
|
|
627
|
+
});
|
|
628
|
+
}).filter((model) => (!request.api || model.api === request.api) && (!request.model_id || model.model_id === request.model_id) && (request.include_deprecated || model.lifecycle !== "deprecated") && (request.include_login_required || model.auth_status !== "login_required"));
|
|
629
|
+
const catalog = isRecord(frame.payload.catalog)
|
|
630
|
+
? {
|
|
631
|
+
...(typeof frame.payload.catalog.observed_at_ms === "number" ? { observed_at_ms: frame.payload.catalog.observed_at_ms } : {}),
|
|
632
|
+
...(typeof frame.payload.catalog.complete === "boolean" ? { complete: frame.payload.catalog.complete } : {}),
|
|
633
|
+
}
|
|
634
|
+
: undefined;
|
|
635
|
+
return { models, ...(catalog ? { catalog } : {}), fetched_at_ms: Date.now(), cache_max_age_ms: 0 };
|
|
636
|
+
}
|
|
637
|
+
async resolve(request) {
|
|
638
|
+
const listed = await this.list({ provider_id: request.provider_id, api: request.api, model_id: request.model_id, include_deprecated: true, include_login_required: true });
|
|
639
|
+
const model = listed.models[0];
|
|
640
|
+
if (!model)
|
|
641
|
+
throw new models_types_1.MakaiProtocolError("model not found", "model_not_found");
|
|
642
|
+
return { model };
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
class OapAuthApi {
|
|
646
|
+
transport;
|
|
647
|
+
timeoutMs;
|
|
648
|
+
defaults;
|
|
649
|
+
constructor(transport, timeoutMs, defaults) {
|
|
650
|
+
this.transport = transport;
|
|
651
|
+
this.timeoutMs = timeoutMs;
|
|
652
|
+
this.defaults = defaults;
|
|
653
|
+
}
|
|
654
|
+
async listProviders() {
|
|
655
|
+
const frame = await this.transport.request(exports.OAP_AGENT_PROFILE, "auth.providers.request", {});
|
|
656
|
+
if (frame.type !== "auth.providers.response" || !Array.isArray(frame.payload.providers)) {
|
|
657
|
+
throw new models_types_1.MakaiProtocolError("invalid auth.providers.response", "malformed_response");
|
|
658
|
+
}
|
|
659
|
+
return frame.payload.providers.map((raw) => {
|
|
660
|
+
if (!isRecord(raw) || !str(raw.id) || !str(raw.name) || !auth_protocol_1.AUTH_STATUSES.some((status) => status === raw.auth_status)) {
|
|
661
|
+
throw new models_types_1.MakaiProtocolError("invalid auth provider descriptor", "malformed_response");
|
|
662
|
+
}
|
|
663
|
+
const kinds = raw.auth_kinds;
|
|
664
|
+
return {
|
|
665
|
+
id: str(raw.id), name: str(raw.name),
|
|
666
|
+
auth_kinds: Array.isArray(kinds)
|
|
667
|
+
? kinds.filter((kind) => auth_protocol_1.AUTH_KINDS.includes(kind))
|
|
668
|
+
: [],
|
|
669
|
+
auth_status: raw.auth_status,
|
|
670
|
+
...(typeof raw.last_error === "string" ? { last_error: raw.last_error } : {}),
|
|
671
|
+
...(str(raw.override_host) ? { override_host: str(raw.override_host) } : {}),
|
|
672
|
+
};
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
async login(providerId, handlers, options) {
|
|
676
|
+
const signal = options?.signal;
|
|
677
|
+
if (signal?.aborted)
|
|
678
|
+
throw new auth_protocol_1.MakaiAuthError("auth login aborted", { kind: "cancelled" });
|
|
679
|
+
const started = await this.transport.request(exports.OAP_AGENT_PROFILE, "auth.login.start.request", { provider_id: providerId });
|
|
680
|
+
if (started.type !== "auth.login.start.response" || !str(started.payload.flow_id)) {
|
|
681
|
+
throw new models_types_1.MakaiProtocolError("invalid auth.login.start.response", "malformed_response");
|
|
682
|
+
}
|
|
683
|
+
const flowId = str(started.payload.flow_id);
|
|
684
|
+
const queue = this.transport.subscribe(exports.OAP_AGENT_PROFILE, "flow", flowId);
|
|
685
|
+
const effective = handlers ?? this.defaults;
|
|
686
|
+
const notify = (event) => {
|
|
687
|
+
try {
|
|
688
|
+
effective?.onEvent?.(event);
|
|
689
|
+
}
|
|
690
|
+
catch (error) {
|
|
691
|
+
throw new auth_protocol_1.MakaiAuthError(error instanceof Error ? error.message : String(error), { kind: "unknown" });
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
let settled = false;
|
|
695
|
+
let sequence = 0;
|
|
696
|
+
try {
|
|
697
|
+
while (true) {
|
|
698
|
+
let frame;
|
|
699
|
+
try {
|
|
700
|
+
frame = await queue.next(this.timeoutMs, signal);
|
|
701
|
+
}
|
|
702
|
+
catch (error) {
|
|
703
|
+
if ((0, abort_signal_1.isAbortError)(error))
|
|
704
|
+
throw new auth_protocol_1.MakaiAuthError("auth login aborted", { kind: "cancelled" });
|
|
705
|
+
throw error;
|
|
706
|
+
}
|
|
707
|
+
if (!Number.isInteger(frame.sequence) || frame.sequence !== sequence + 1) {
|
|
708
|
+
throw new models_types_1.MakaiProtocolError("auth flow sequence is not contiguous", "malformed_response");
|
|
709
|
+
}
|
|
710
|
+
sequence = frame.sequence;
|
|
711
|
+
const data = frame.payload;
|
|
712
|
+
if (str(data.flow_id) !== flowId || str(data.provider_id) !== providerId) {
|
|
713
|
+
throw new models_types_1.MakaiProtocolError("auth flow identity mismatch", "malformed_response");
|
|
714
|
+
}
|
|
715
|
+
if (frame.type === "auth.login.event") {
|
|
716
|
+
let event;
|
|
717
|
+
if (data.kind === "url" && str(data.url)) {
|
|
718
|
+
event = { type: "auth_url", flow_id: flowId, provider_id: providerId, url: str(data.url), ...(typeof data.instructions === "string" ? { instructions: data.instructions } : {}) };
|
|
719
|
+
}
|
|
720
|
+
else if (data.kind === "prompt") {
|
|
721
|
+
throw new auth_protocol_1.MakaiAuthError("manual login input cannot be sent over OAP", { kind: "provider_error", code: "auth_input_unavailable" });
|
|
722
|
+
}
|
|
723
|
+
else if (data.kind === "progress" && str(data.message)) {
|
|
724
|
+
event = { type: "progress", flow_id: flowId, provider_id: providerId, message: str(data.message) };
|
|
725
|
+
}
|
|
726
|
+
else {
|
|
727
|
+
throw new models_types_1.MakaiProtocolError("invalid auth.login.event", "malformed_response");
|
|
728
|
+
}
|
|
729
|
+
notify(event);
|
|
730
|
+
continue;
|
|
731
|
+
}
|
|
732
|
+
if (frame.type !== "auth.login.completed")
|
|
733
|
+
throw new models_types_1.MakaiProtocolError(`unexpected auth flow frame: ${frame.type}`, "malformed_response");
|
|
734
|
+
settled = true;
|
|
735
|
+
if (data.status === "success") {
|
|
736
|
+
notify({ type: "success", flow_id: flowId, provider_id: providerId });
|
|
737
|
+
return { status: "success" };
|
|
738
|
+
}
|
|
739
|
+
const err = isRecord(data.error) ? data.error : {};
|
|
740
|
+
const message = str(err.message) || (data.status === "cancelled" ? "auth login cancelled" : "auth login failed");
|
|
741
|
+
notify({ type: "error", flow_id: flowId, provider_id: providerId, message, ...(str(err.code) ? { code: str(err.code) } : {}) });
|
|
742
|
+
if (data.status === "cancelled")
|
|
743
|
+
throw new auth_protocol_1.MakaiAuthError(message, { kind: "cancelled", code: str(err.code) || undefined });
|
|
744
|
+
if (data.status === "failed")
|
|
745
|
+
throw new auth_protocol_1.MakaiAuthError(message, { kind: "provider_error", code: str(err.code) || undefined });
|
|
746
|
+
throw new models_types_1.MakaiProtocolError("invalid auth.login.completed status", "malformed_response");
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
finally {
|
|
750
|
+
this.transport.unsubscribe(exports.OAP_AGENT_PROFILE, "flow", flowId);
|
|
751
|
+
if (!settled) {
|
|
752
|
+
try {
|
|
753
|
+
this.transport.send(exports.OAP_AGENT_PROFILE, "auth.login.cancel.request", { flow_id: flowId });
|
|
754
|
+
}
|
|
755
|
+
catch { }
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
const SDK_PARTICIPANT = "sdk";
|
|
761
|
+
const OAPX_AGENT_ENDPOINT = "oapx.agent";
|
|
762
|
+
async function resolveProvidedCall(call, tools, sessionId, runId) {
|
|
763
|
+
const toolCallId = str(call.tool_call_id);
|
|
764
|
+
const toolName = str(call.name);
|
|
765
|
+
const argsJson = typeof call.arguments_json === "string" ? call.arguments_json : JSON.stringify(call.arguments_json ?? {});
|
|
766
|
+
const answer = {
|
|
767
|
+
interaction_id: str(call.interaction_id), session_id: sessionId, run_id: runId, tool_call_id: toolCallId,
|
|
768
|
+
requested_by: str(call.requested_by), responded_by: SDK_PARTICIPANT,
|
|
769
|
+
};
|
|
770
|
+
const tool = tools.find((candidate) => candidate.name === toolName);
|
|
771
|
+
if (!tool?.execute)
|
|
772
|
+
return { ...answer, error: { code: "tool_unavailable", message: `Tool '${toolName}' is not executable by this client` } };
|
|
773
|
+
try {
|
|
774
|
+
const parsed = argsJson ? JSON.parse(argsJson) : {};
|
|
775
|
+
if (!isRecord(parsed))
|
|
776
|
+
throw new Error("tool arguments must be a JSON object");
|
|
777
|
+
const result = await tool.execute(parsed, { tool_call_id: toolCallId, tool_name: toolName, args_json: argsJson });
|
|
778
|
+
return { ...answer, result: typeof result === "string" ? result : result.map((part) => part.text).join("") };
|
|
779
|
+
}
|
|
780
|
+
catch (error) {
|
|
781
|
+
const message = error instanceof Error ? error.message || error.name : String(error);
|
|
782
|
+
return { ...answer, error: { code: "tool_failed", message: message || "the tool failed" } };
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
function canonical(value) {
|
|
786
|
+
if (Array.isArray(value))
|
|
787
|
+
return value.map(canonical);
|
|
788
|
+
if (!isRecord(value))
|
|
789
|
+
return value;
|
|
790
|
+
return Object.fromEntries(Object.keys(value).sort().map((key) => [key, canonical(value[key])]));
|
|
791
|
+
}
|
|
792
|
+
function declaredTools(tools) {
|
|
793
|
+
return JSON.stringify((tools ?? [])
|
|
794
|
+
.map((tool) => [tool.name, tool.description, canonical(parseInputSchema(tool.parameters_schema_json))])
|
|
795
|
+
.sort((left, right) => String(left[0]).localeCompare(String(right[0]))));
|
|
796
|
+
}
|
|
797
|
+
class OapAgentApi {
|
|
798
|
+
transport;
|
|
799
|
+
idleTimeoutMs;
|
|
800
|
+
authRetryPolicy;
|
|
801
|
+
auth;
|
|
802
|
+
authHandlers;
|
|
803
|
+
models;
|
|
804
|
+
selectedModels = new Map();
|
|
805
|
+
openSessions = new Map();
|
|
806
|
+
constructor(transport, idleTimeoutMs, models, authRetryPolicy, auth, authHandlers) {
|
|
807
|
+
this.transport = transport;
|
|
808
|
+
this.idleTimeoutMs = idleTimeoutMs;
|
|
809
|
+
this.authRetryPolicy = authRetryPolicy;
|
|
810
|
+
this.auth = auth;
|
|
811
|
+
this.authHandlers = authHandlers;
|
|
812
|
+
this.models = models;
|
|
813
|
+
}
|
|
814
|
+
async run(request) {
|
|
815
|
+
let text = "";
|
|
816
|
+
let end;
|
|
817
|
+
for await (const event of this.stream(request)) {
|
|
818
|
+
if (event.type === "text_delta")
|
|
819
|
+
text += event.delta;
|
|
820
|
+
if (event.type === "agent_end")
|
|
821
|
+
end = event;
|
|
822
|
+
}
|
|
823
|
+
if (!end)
|
|
824
|
+
throw new execution_types_1.MakaiStreamError("agent run ended without run.completed", { kind: "transport_error" });
|
|
825
|
+
return { message: end.message ?? { role: "assistant", content: text }, usage: end.usage, stop_reason: end.stop_reason, ...modelParts(request.model_ref || end.model_id || "") };
|
|
826
|
+
}
|
|
827
|
+
async runSelected(sessionId, messages) {
|
|
828
|
+
return this.run({ model_ref: "", messages, options: { session_id: sessionId } });
|
|
829
|
+
}
|
|
830
|
+
streamSelected(sessionId, messages) {
|
|
831
|
+
return this.stream({ model_ref: "", messages, options: { session_id: sessionId } });
|
|
832
|
+
}
|
|
833
|
+
async *stream(request) {
|
|
834
|
+
let retried = false;
|
|
835
|
+
for (;;) {
|
|
836
|
+
let yieldedContent = false;
|
|
837
|
+
const progress = { toolsRan: false };
|
|
838
|
+
const preamble = [];
|
|
839
|
+
try {
|
|
840
|
+
for await (const event of this.streamOnce(request, progress)) {
|
|
841
|
+
if (!yieldedContent && (event.type === "agent_start" || event.type === "message_start" || event.type === "turn_start")) {
|
|
842
|
+
preamble.push(event);
|
|
843
|
+
continue;
|
|
844
|
+
}
|
|
845
|
+
if (!yieldedContent) {
|
|
846
|
+
yieldedContent = true;
|
|
847
|
+
for (const earlier of preamble)
|
|
848
|
+
yield earlier;
|
|
849
|
+
}
|
|
850
|
+
yield event;
|
|
851
|
+
}
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
catch (error) {
|
|
855
|
+
if (authFailure(error)) {
|
|
856
|
+
const modelRef = request.model_ref || this.selectedModels.get(request.options?.session_id ?? "") || "";
|
|
857
|
+
const providerId = providerFromRef(modelRef);
|
|
858
|
+
if (!yieldedContent && !progress.toolsRan && !retried && providerId && (request.options?.auth_retry_policy ?? this.authRetryPolicy) === "auto_once" && this.auth) {
|
|
859
|
+
retried = true;
|
|
860
|
+
try {
|
|
861
|
+
await this.auth.login(providerId, this.authHandlers, { signal: request.options?.signal });
|
|
862
|
+
}
|
|
863
|
+
catch {
|
|
864
|
+
if (request.options?.signal?.aborted)
|
|
865
|
+
throw new execution_types_1.MakaiStreamError("agent run aborted", { kind: "aborted" });
|
|
866
|
+
throw new execution_types_1.MakaiAuthRequiredError(providerId, error instanceof Error ? error.message : "authentication required");
|
|
867
|
+
}
|
|
868
|
+
continue;
|
|
869
|
+
}
|
|
870
|
+
if (providerId)
|
|
871
|
+
throw new execution_types_1.MakaiAuthRequiredError(providerId, error instanceof Error ? error.message : "authentication required");
|
|
872
|
+
}
|
|
873
|
+
throw error;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
async *streamOnce(request, progress = { toolsRan: false }) {
|
|
878
|
+
if (request.options?.temperature !== undefined)
|
|
879
|
+
unsupported("temperature on an agent run");
|
|
880
|
+
if (request.options?.reasoning_effort === "minimal")
|
|
881
|
+
unsupported("minimal reasoning, which the agent loop runs as low");
|
|
882
|
+
const maxTokens = request.options?.max_tokens;
|
|
883
|
+
if (maxTokens !== undefined && (!Number.isInteger(maxTokens) || maxTokens < 1 || maxTokens > 4_294_967_295))
|
|
884
|
+
throw new models_types_1.MakaiProtocolError("max_tokens must be an integer between 1 and 4294967295", "invalid_request");
|
|
885
|
+
if (request.tools?.length && !this.transport.agentFeatures.has("action.tools.provide"))
|
|
886
|
+
unsupported("client tools on an endpoint that does not advertise action.tools.provide");
|
|
887
|
+
const readsOapxSettings = this.transport.agentEndpoint === OAPX_AGENT_ENDPOINT;
|
|
888
|
+
if (maxTokens !== undefined && !readsOapxSettings)
|
|
889
|
+
unsupported("an output limit on an endpoint other than the oapx agent loop");
|
|
890
|
+
const sessionId = request.options?.session_id ?? (0, ulid_1.ulid)();
|
|
891
|
+
const reasoning = request.options?.reasoning_effort;
|
|
892
|
+
let session = this.openSessions.get(sessionId);
|
|
893
|
+
if (session) {
|
|
894
|
+
const changed = (request.tools !== undefined && declaredTools(request.tools) !== session.declared)
|
|
895
|
+
|| (reasoning !== undefined && reasoning !== session.reasoning)
|
|
896
|
+
|| (maxTokens !== undefined && maxTokens !== session.maxTokens);
|
|
897
|
+
if (changed)
|
|
898
|
+
unsupported("changing the tools, reasoning effort or output limit of a session already open");
|
|
899
|
+
if (request.tools !== undefined) {
|
|
900
|
+
session = { ...session, tools: request.tools };
|
|
901
|
+
this.openSessions.set(sessionId, session);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
else {
|
|
905
|
+
const opened = await this.transport.request(exports.OAP_AGENT_PROFILE, "session.open.request", {
|
|
906
|
+
session_id: sessionId,
|
|
907
|
+
...(request.tools?.length ? { tools: request.tools.map((tool) => ({ name: tool.name, description: tool.description, input_schema: parseInputSchema(tool.parameters_schema_json), execution_owner: SDK_PARTICIPANT, ...(this.transport.agentSource ? { source: this.transport.agentSource } : {}) })) } : {}),
|
|
908
|
+
...(request.options?.reasoning_effort ? { reasoning_level: request.options.reasoning_effort } : {}),
|
|
909
|
+
...(readsOapxSettings ? { metadata: { oapx: { user_input: false, ...(maxTokens !== undefined ? { output: maxTokens } : {}) } } } : {}),
|
|
910
|
+
}, { session_id: sessionId });
|
|
911
|
+
if (opened.type !== "session.open.response")
|
|
912
|
+
throw new models_types_1.MakaiProtocolError(`unexpected ${opened.type}`, "malformed_response");
|
|
913
|
+
session = { declared: declaredTools(request.tools), reasoning, maxTokens, tools: request.tools ?? [] };
|
|
914
|
+
this.openSessions.set(sessionId, session);
|
|
915
|
+
}
|
|
916
|
+
const tools = session.tools;
|
|
917
|
+
const queue = this.transport.subscribe(exports.OAP_AGENT_PROFILE, "session", sessionId);
|
|
918
|
+
let runId;
|
|
919
|
+
let settled = false;
|
|
920
|
+
try {
|
|
921
|
+
const submitted = await this.transport.request(exports.OAP_AGENT_PROFILE, "session.message.submit.request", {
|
|
922
|
+
session_id: sessionId, messages: toOapMessages(request.messages),
|
|
923
|
+
...(request.model_ref ? { model_id: request.model_ref } : {}), delivery: "auto",
|
|
924
|
+
}, { session_id: sessionId });
|
|
925
|
+
if (submitted.type !== "session.message.submit.response" || submitted.payload.accepted !== true)
|
|
926
|
+
throw new models_types_1.MakaiProtocolError(`unexpected ${submitted.type}`, "malformed_response");
|
|
927
|
+
runId = str(submitted.payload.run_id);
|
|
928
|
+
while (true) {
|
|
929
|
+
if (request.options?.signal?.aborted) {
|
|
930
|
+
throw abortError();
|
|
931
|
+
}
|
|
932
|
+
const frame = await queue.next(this.idleTimeoutMs, request.options?.signal);
|
|
933
|
+
if (runId && frame.run_id && frame.run_id !== runId)
|
|
934
|
+
continue;
|
|
935
|
+
const data = payload(frame);
|
|
936
|
+
if (frame.type === "run.started") {
|
|
937
|
+
yield { type: "agent_start", session_id: sessionId };
|
|
938
|
+
continue;
|
|
939
|
+
}
|
|
940
|
+
if (frame.type === "action.call.requested") {
|
|
941
|
+
if (str(data.execution_owner) === SDK_PARTICIPANT) {
|
|
942
|
+
progress.toolsRan = true;
|
|
943
|
+
const answer = await (0, abort_signal_1.raceWithAbort)(resolveProvidedCall(data, tools, sessionId, runId ?? ""), request.options?.signal, "agent run aborted")
|
|
944
|
+
.catch((error) => { throw (0, abort_signal_1.isAbortError)(error) ? abortError() : error; });
|
|
945
|
+
this.transport.send(exports.OAP_AGENT_PROFILE, "action.call.resolve.request", answer, { session_id: sessionId, run_id: runId ?? "" });
|
|
946
|
+
}
|
|
947
|
+
continue;
|
|
948
|
+
}
|
|
949
|
+
if (frame.type === "action.call.resolve.response") {
|
|
950
|
+
if (data.accepted === false && str(data.reason) !== "already_resolved")
|
|
951
|
+
throw new models_types_1.MakaiProtocolError(`the endpoint refused a tool result: ${str(data.reason)}`, "malformed_response");
|
|
952
|
+
continue;
|
|
953
|
+
}
|
|
954
|
+
if (frame.type === "action.call.started") {
|
|
955
|
+
yield { type: "tool_execution_start", tool_call_id: str(data.tool_call_id), tool_name: str(data.name) };
|
|
956
|
+
continue;
|
|
957
|
+
}
|
|
958
|
+
if (frame.type === "action.call.completed" || frame.type === "action.call.failed" || frame.type === "action.call.cancelled") {
|
|
959
|
+
yield {
|
|
960
|
+
type: "tool_execution_end", tool_call_id: str(data.tool_call_id),
|
|
961
|
+
is_error: frame.type !== "action.call.completed" || (data.name === "codex.command" && (!isRecord(data.result) || data.result.exitCode !== 0)),
|
|
962
|
+
...("result" in data ? { result: data.result } : {}),
|
|
963
|
+
};
|
|
964
|
+
continue;
|
|
965
|
+
}
|
|
966
|
+
if (frame.type === "content.delta" && isRecord(data.part)) {
|
|
967
|
+
if (data.part.type === "text")
|
|
968
|
+
yield { type: "text_delta", delta: str(data.part.text) };
|
|
969
|
+
if (data.part.type === "reasoning")
|
|
970
|
+
yield { type: "thinking_delta", delta: str(data.part.reasoning) };
|
|
971
|
+
continue;
|
|
972
|
+
}
|
|
973
|
+
if (frame.type === "run.completed") {
|
|
974
|
+
settled = true;
|
|
975
|
+
const finalMessage = isRecord(data.final_response) ? data.final_response : {};
|
|
976
|
+
yield { type: "agent_end", stop_reason: str(data.stop_reason), usage: usage(data.usage), model_id: str(data.model_id), message: { role: "assistant", content: fromOapContent(finalMessage.content) } };
|
|
977
|
+
return;
|
|
978
|
+
}
|
|
979
|
+
if (frame.type === "run.failed") {
|
|
980
|
+
settled = true;
|
|
981
|
+
const err = isRecord(data.error) ? data.error : {};
|
|
982
|
+
throw new execution_types_1.MakaiStreamError(str(err.message) || "agent run failed", { kind: "provider_error", code: str(err.code) || undefined });
|
|
983
|
+
}
|
|
984
|
+
if (frame.type === "run.cancelled") {
|
|
985
|
+
settled = true;
|
|
986
|
+
throw new execution_types_1.MakaiStreamError("agent run cancelled", { kind: "aborted" });
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
finally {
|
|
991
|
+
this.transport.unsubscribe(exports.OAP_AGENT_PROFILE, "session", sessionId);
|
|
992
|
+
if (runId && !settled) {
|
|
993
|
+
try {
|
|
994
|
+
this.transport.send(exports.OAP_AGENT_PROFILE, "run.cancel.request", { session_id: sessionId, run_id: runId }, { session_id: sessionId, run_id: runId });
|
|
995
|
+
}
|
|
996
|
+
catch { }
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
async switchModel(sessionId, modelId) {
|
|
1001
|
+
const response = await this.transport.request(exports.OAP_AGENT_PROFILE, "session.model.switch.request", { session_id: sessionId, model_id: modelId }, { session_id: sessionId });
|
|
1002
|
+
if (response.type !== "session.model.switch.response")
|
|
1003
|
+
throw new models_types_1.MakaiProtocolError(`unexpected ${response.type}`, "malformed_response");
|
|
1004
|
+
this.selectedModels.set(sessionId, modelId);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
async function createOapClient(options = {}) {
|
|
1008
|
+
const resolver = options.logger ? { ...options.resolver, logger: options.logger } : options.resolver ?? {};
|
|
1009
|
+
const command = options.command ?? await (0, binary_resolver_1.resolveMakaiBinary)(resolver);
|
|
1010
|
+
const transport = new OapStdioTransport(command, options.args ?? ["serve", "agent,provider", "--stdio"], {
|
|
1011
|
+
cwd: options.cwd, env: options.env, timeoutMs: options.responseTimeoutMs ?? options.frameTimeoutMs ?? 30_000,
|
|
1012
|
+
handshakeTimeoutMs: options.handshakeTimeoutMs ?? 10_000,
|
|
1013
|
+
});
|
|
1014
|
+
await transport.connect();
|
|
1015
|
+
const models = new OapModelsApi(transport);
|
|
1016
|
+
const auth = new OapAuthApi(transport, options.frameTimeoutMs ?? 30_000, options.auth?.handlers);
|
|
1017
|
+
return {
|
|
1018
|
+
auth, models,
|
|
1019
|
+
provider: new OapProviderApi(transport, options.idleTimeoutMs, options.auth?.auth_retry_policy, auth, options.auth?.handlers),
|
|
1020
|
+
agent: new OapAgentApi(transport, options.idleTimeoutMs, models, options.auth?.auth_retry_policy, auth, options.auth?.handlers),
|
|
1021
|
+
close: () => transport.close(),
|
|
1022
|
+
};
|
|
1023
|
+
}
|