marona 0.12.1 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +15 -4
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +430 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export const packageVersion = "0.
|
|
1
|
+
export const packageVersion = "0.13.0";
|
|
2
|
+
const discoverCapabilitiesTool = "marona__discover_capabilities";
|
|
3
|
+
const callCapabilityTool = "marona__call_capability";
|
|
2
4
|
const standardInterfaces = ["api", "web", "mobile_app", "desktop", "whatsapp"];
|
|
3
5
|
const interfaceNamePattern = "^[a-z][a-z0-9_-]{0,63}$";
|
|
4
6
|
const standardExecutionModes = ["online", "hybrid", "offline"];
|
|
@@ -372,18 +374,140 @@ class ModelManager {
|
|
|
372
374
|
};
|
|
373
375
|
}
|
|
374
376
|
}
|
|
377
|
+
function neutralGatewayTools() {
|
|
378
|
+
return [
|
|
379
|
+
{
|
|
380
|
+
name: discoverCapabilitiesTool,
|
|
381
|
+
description: "Discover connected Marona Apps, Skills, and exact capabilities relevant to the current request. Call once with the request, then again with only the selected App slugs or Skill names returned by discovery.",
|
|
382
|
+
inputSchema: {
|
|
383
|
+
type: "object",
|
|
384
|
+
properties: {
|
|
385
|
+
request: { type: "string", description: "The user's complete current request." },
|
|
386
|
+
app_slugs: {
|
|
387
|
+
type: "array",
|
|
388
|
+
items: { type: "string" },
|
|
389
|
+
description: "Exact App slugs returned by prior discovery.",
|
|
390
|
+
},
|
|
391
|
+
skill_names: {
|
|
392
|
+
type: "array",
|
|
393
|
+
items: { type: "string" },
|
|
394
|
+
description: "Exact Skill names returned by prior discovery.",
|
|
395
|
+
},
|
|
396
|
+
},
|
|
397
|
+
required: ["request"],
|
|
398
|
+
additionalProperties: false,
|
|
399
|
+
},
|
|
400
|
+
annotations: { marona_gateway: "capability_discovery" },
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
name: callCapabilityTool,
|
|
404
|
+
description: "Execute one exact Marona capability returned by capability discovery. Marona enforces authorization, permissions, approvals, and execution.",
|
|
405
|
+
inputSchema: {
|
|
406
|
+
type: "object",
|
|
407
|
+
properties: {
|
|
408
|
+
name: { type: "string", description: "Exact capability name returned by discovery." },
|
|
409
|
+
arguments: {
|
|
410
|
+
type: "object",
|
|
411
|
+
description: "Arguments matching the discovered input schema.",
|
|
412
|
+
additionalProperties: true,
|
|
413
|
+
},
|
|
414
|
+
},
|
|
415
|
+
required: ["name", "arguments"],
|
|
416
|
+
additionalProperties: false,
|
|
417
|
+
},
|
|
418
|
+
annotations: { marona_gateway: "capability_execution" },
|
|
419
|
+
},
|
|
420
|
+
];
|
|
421
|
+
}
|
|
422
|
+
function gatewayDiscoveryResult(payload, argumentsValue) {
|
|
423
|
+
const request = stringValue(argumentsValue.request);
|
|
424
|
+
if (!request)
|
|
425
|
+
throw new EdgeRuntimeError("Marona capability discovery requires the current request.");
|
|
426
|
+
const selectedApps = new Set((Array.isArray(argumentsValue.app_slugs) ? argumentsValue.app_slugs : [])
|
|
427
|
+
.map(stringValue).filter((value) => Boolean(value)));
|
|
428
|
+
const selectedSkills = new Set((Array.isArray(argumentsValue.skill_names) ? argumentsValue.skill_names : [])
|
|
429
|
+
.map(stringValue).filter((value) => Boolean(value)));
|
|
430
|
+
const capabilities = normalizeConnectedTools((Array.isArray(payload.connections) ? payload.connections : [])
|
|
431
|
+
.map(asRecord).filter((value) => Boolean(value))).map((descriptor) => {
|
|
432
|
+
const fn = asRecord(descriptor.function) ?? {};
|
|
433
|
+
return {
|
|
434
|
+
name: stringValue(fn.name) ?? "marona_tool",
|
|
435
|
+
description: stringValue(fn.description) ?? stringValue(fn.name) ?? "Marona tool",
|
|
436
|
+
inputSchema: asRecord(fn.parameters) ?? { type: "object", properties: {} },
|
|
437
|
+
annotations: asRecord(descriptor.marona) ?? {},
|
|
438
|
+
};
|
|
439
|
+
});
|
|
440
|
+
let structured;
|
|
441
|
+
if (selectedApps.size || selectedSkills.size) {
|
|
442
|
+
structured = {
|
|
443
|
+
status: "completed",
|
|
444
|
+
request,
|
|
445
|
+
capabilities: capabilities.filter((capability) => {
|
|
446
|
+
const app = stringValue(capability.annotations.app_slug);
|
|
447
|
+
const skill = stringValue(capability.annotations.skill_name);
|
|
448
|
+
return Boolean((app && selectedApps.has(app)) || (skill && selectedSkills.has(skill)));
|
|
449
|
+
}),
|
|
450
|
+
instruction: "Call marona__call_capability with one exact capability name and arguments matching its inputSchema.",
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
const appDescriptions = new Map();
|
|
455
|
+
const skillDescriptions = new Map();
|
|
456
|
+
for (const value of Array.isArray(payload.apps) ? payload.apps : []) {
|
|
457
|
+
const app = asRecord(value);
|
|
458
|
+
const slug = stringValue(app?.slug ?? app?.app_slug);
|
|
459
|
+
if (slug)
|
|
460
|
+
appDescriptions.set(slug, stringValue(app?.description ?? app?.summary ?? app?.name) ?? slug);
|
|
461
|
+
}
|
|
462
|
+
for (const value of Array.isArray(payload.skills) ? payload.skills : []) {
|
|
463
|
+
const skill = asRecord(value);
|
|
464
|
+
const name = stringValue(skill?.name ?? skill?.slug);
|
|
465
|
+
if (name)
|
|
466
|
+
skillDescriptions.set(name, stringValue(skill?.description ?? skill?.summary) ?? name);
|
|
467
|
+
}
|
|
468
|
+
for (const capability of capabilities) {
|
|
469
|
+
const app = stringValue(capability.annotations.app_slug);
|
|
470
|
+
const skill = stringValue(capability.annotations.skill_name);
|
|
471
|
+
if (app && !appDescriptions.has(app))
|
|
472
|
+
appDescriptions.set(app, capability.description);
|
|
473
|
+
if (skill && !skillDescriptions.has(skill))
|
|
474
|
+
skillDescriptions.set(skill, capability.description);
|
|
475
|
+
}
|
|
476
|
+
structured = {
|
|
477
|
+
status: "selection_required",
|
|
478
|
+
request,
|
|
479
|
+
apps: [...appDescriptions].sort(([left], [right]) => left.localeCompare(right))
|
|
480
|
+
.map(([slug, description]) => ({ slug, description })),
|
|
481
|
+
skills: [...skillDescriptions].sort(([left], [right]) => left.localeCompare(right))
|
|
482
|
+
.map(([name, description]) => ({ name, description })),
|
|
483
|
+
instruction: "Reason from the current request, select only the relevant exact App slugs or Skill names, then call marona__discover_capabilities again.",
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
return {
|
|
487
|
+
content: [{ type: "text", text: JSON.stringify(structured) }],
|
|
488
|
+
structuredContent: structured,
|
|
489
|
+
isError: false,
|
|
490
|
+
};
|
|
491
|
+
}
|
|
375
492
|
export class MCPConnection {
|
|
376
493
|
payload;
|
|
377
494
|
executor;
|
|
378
|
-
|
|
495
|
+
gatewayEnabled;
|
|
496
|
+
constructor(payload, executor, gatewayEnabled = false) {
|
|
379
497
|
this.payload = payload;
|
|
380
498
|
this.executor = executor;
|
|
499
|
+
this.gatewayEnabled = gatewayEnabled;
|
|
381
500
|
}
|
|
382
501
|
/** @internal */
|
|
383
502
|
agentRuntime() {
|
|
384
503
|
return this.executor?.runtime;
|
|
385
504
|
}
|
|
386
505
|
listTools() {
|
|
506
|
+
if (this.gatewayEnabled)
|
|
507
|
+
return neutralGatewayTools();
|
|
508
|
+
return this.listCapabilities();
|
|
509
|
+
}
|
|
510
|
+
listCapabilities() {
|
|
387
511
|
return normalizeConnectedTools(this.descriptors()).map((descriptor) => {
|
|
388
512
|
const fn = asRecord(descriptor.function) ?? {};
|
|
389
513
|
return {
|
|
@@ -395,6 +519,16 @@ export class MCPConnection {
|
|
|
395
519
|
});
|
|
396
520
|
}
|
|
397
521
|
async callTool(name, argumentsValue = {}, options = {}) {
|
|
522
|
+
if (this.gatewayEnabled && name === discoverCapabilitiesTool) {
|
|
523
|
+
return gatewayDiscoveryResult(this.payload, argumentsValue);
|
|
524
|
+
}
|
|
525
|
+
if (this.gatewayEnabled && name === callCapabilityTool) {
|
|
526
|
+
const capability = stringValue(argumentsValue.name);
|
|
527
|
+
if (!capability) {
|
|
528
|
+
throw new EdgeRuntimeError("Marona capability execution requires an exact name.");
|
|
529
|
+
}
|
|
530
|
+
return this.callTool(capability, asRecord(argumentsValue.arguments) ?? {}, options);
|
|
531
|
+
}
|
|
398
532
|
const descriptor = this.descriptors().find((candidate) => {
|
|
399
533
|
const normalized = normalizeConnectedTools([candidate])[0];
|
|
400
534
|
return stringValue(asRecord(normalized?.function)?.name) === name;
|
|
@@ -900,16 +1034,113 @@ export class Runner {
|
|
|
900
1034
|
}
|
|
901
1035
|
}
|
|
902
1036
|
static async *stream(agent, input, options = {}) {
|
|
1037
|
+
const userId = cleanString(options.userId);
|
|
1038
|
+
const sessionId = cleanString(options.sessionId) ?? "default";
|
|
1039
|
+
const context = agentContext(agent, userId, sessionId);
|
|
1040
|
+
await runGuardrails(agent, "input", context, input);
|
|
1041
|
+
await agent.hooks?.onStart?.(context, input);
|
|
1042
|
+
await resolveAgentPeers(agent);
|
|
1043
|
+
const graph = agentGraph(agent, context, options);
|
|
1044
|
+
const connection = agentConnection(agent);
|
|
1045
|
+
const runtime = connection?.agentRuntime() ?? agent.runtime ?? activeRuntime;
|
|
1046
|
+
if (!runtime)
|
|
1047
|
+
throw new EdgeRuntimeError("Create Marona before running an Agent without tools.");
|
|
1048
|
+
const developerId = connection ? developerIdForAgent(connection) : "authenticated";
|
|
1049
|
+
const conversationId = [developerId, userId ?? "anonymous", sessionId]
|
|
1050
|
+
.map((value) => encodeURIComponent(value))
|
|
1051
|
+
.join(":");
|
|
1052
|
+
const output = normalizeAgentOutputRequest(agent, options.output);
|
|
1053
|
+
const runtimeInput = agent.inputSchema ? JSON.stringify(input) : input;
|
|
1054
|
+
validateAgentSchema(agent.inputSchema, input, "input", agent.name);
|
|
1055
|
+
const inputs = inputModalities(runtimeInput);
|
|
1056
|
+
const unsupportedInputs = [...inputs].filter((value) => !new Set(agent.modalities ?? [...supportedModalities]).has(value));
|
|
1057
|
+
if (unsupportedInputs.length) {
|
|
1058
|
+
throw new EdgeRuntimeError(`Agent input does not allow: ${unsupportedInputs.join(", ")}`);
|
|
1059
|
+
}
|
|
903
1060
|
yield { type: "run.started" };
|
|
904
|
-
yield { type: "agent.started" };
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
1061
|
+
yield { type: "agent.started", data: { agent_name: agent.name } };
|
|
1062
|
+
let response;
|
|
1063
|
+
try {
|
|
1064
|
+
for await (const event of new ResponsesClient(runtime).stream({
|
|
1065
|
+
model: agent.model,
|
|
1066
|
+
input: runtimeInput,
|
|
1067
|
+
userId,
|
|
1068
|
+
conversationId,
|
|
1069
|
+
developerInstructions: resolveAgentInstructions(agent, context) || undefined,
|
|
1070
|
+
tools: agentRuntimeTools(agent),
|
|
1071
|
+
routing: agent.routing,
|
|
1072
|
+
output,
|
|
1073
|
+
toolChoice: agent.toolChoice,
|
|
1074
|
+
metadata: {
|
|
1075
|
+
marona_agent: {
|
|
1076
|
+
input_modalities: [...inputs],
|
|
1077
|
+
allowed_output_modalities: agent.outputModalities ?? [...supportedModalities],
|
|
1078
|
+
output_modalities: output.modalities,
|
|
1079
|
+
tools_enabled: Boolean(agent.tools) && agent.toolChoice !== "none",
|
|
1080
|
+
voice: agent.voice ?? {},
|
|
1081
|
+
},
|
|
1082
|
+
marona_multi_agent: graph,
|
|
1083
|
+
marona_tool_context: {
|
|
1084
|
+
session_id: sessionId,
|
|
1085
|
+
developer_id: developerId,
|
|
1086
|
+
permissions: Array.isArray(agent.metadata.permissions) ? agent.metadata.permissions : [],
|
|
1087
|
+
approved_tools: Array.isArray(agent.metadata.approved_tools) ? agent.metadata.approved_tools : [],
|
|
1088
|
+
},
|
|
1089
|
+
},
|
|
1090
|
+
})) {
|
|
1091
|
+
if (event.type === "run.started")
|
|
1092
|
+
continue;
|
|
1093
|
+
if (event.type === "run.failed") {
|
|
1094
|
+
throw new EdgeRuntimeError(cleanString(asRecord(event.data)?.message) ?? "Agent stream failed.");
|
|
1095
|
+
}
|
|
1096
|
+
if (event.type === "run.completed") {
|
|
1097
|
+
const payload = asRecord(event.output);
|
|
1098
|
+
if (payload)
|
|
1099
|
+
response = parseRuntimeMessageResponse(payload);
|
|
1100
|
+
continue;
|
|
1101
|
+
}
|
|
1102
|
+
yield {
|
|
1103
|
+
type: String(event.type ?? "event"),
|
|
1104
|
+
delta: cleanString(event.delta),
|
|
1105
|
+
output: event.output,
|
|
1106
|
+
data: asRecord(event.data),
|
|
1107
|
+
};
|
|
1108
|
+
}
|
|
1109
|
+
if (!response)
|
|
1110
|
+
throw new EdgeRuntimeError("Agent stream completed without a final response.");
|
|
1111
|
+
raiseAgentInterruption(response);
|
|
908
1112
|
}
|
|
909
|
-
|
|
910
|
-
|
|
1113
|
+
catch (error) {
|
|
1114
|
+
await agent.hooks?.onError?.(context, error);
|
|
1115
|
+
throw error;
|
|
911
1116
|
}
|
|
912
|
-
|
|
1117
|
+
const metadata = response.agent ?? asRecord(response.rawJson.agent) ?? {};
|
|
1118
|
+
const activeAgent = agentByName(agent, cleanString(metadata.active_agent) ?? agent.name);
|
|
1119
|
+
const lastAgent = agentByName(agent, cleanString(metadata.last_agent) ?? activeAgent.name);
|
|
1120
|
+
const parsedOutput = parseTypedOutput(activeAgent, response.output);
|
|
1121
|
+
const activeContext = agentContext(activeAgent, userId, sessionId);
|
|
1122
|
+
await runGuardrails(activeAgent, "output", activeContext, parsedOutput);
|
|
1123
|
+
const result = {
|
|
1124
|
+
output: parsedOutput,
|
|
1125
|
+
lastAgent,
|
|
1126
|
+
activeAgent,
|
|
1127
|
+
agentRuns: normalizeAgentRuns(metadata, agent),
|
|
1128
|
+
usage: asRecord(response.rawJson.usage) ?? {},
|
|
1129
|
+
routing: response.routing,
|
|
1130
|
+
traceId: cleanString(metadata.trace_id),
|
|
1131
|
+
};
|
|
1132
|
+
for (const run of result.agentRuns.slice(1)) {
|
|
1133
|
+
if (run.relationship === "delegation")
|
|
1134
|
+
await agent.hooks?.onToolCall?.(context, run);
|
|
1135
|
+
if (run.relationship === "handoff")
|
|
1136
|
+
await agent.hooks?.onHandoff?.(context, run);
|
|
1137
|
+
}
|
|
1138
|
+
await activeAgent.hooks?.onComplete?.(activeContext, result);
|
|
1139
|
+
yield {
|
|
1140
|
+
type: "run.completed",
|
|
1141
|
+
output: result.output,
|
|
1142
|
+
data: { usage: result.usage, routing: result.routing, trace_id: result.traceId },
|
|
1143
|
+
};
|
|
913
1144
|
}
|
|
914
1145
|
}
|
|
915
1146
|
export class RealtimeSession {
|
|
@@ -1192,7 +1423,7 @@ async function executeRealtimeGateway(agent, name, argumentsValue, options) {
|
|
|
1192
1423
|
const query = cleanString(argumentsValue.request) ?? cleanString(argumentsValue.query);
|
|
1193
1424
|
if (!query)
|
|
1194
1425
|
throw new EdgeRuntimeError("Realtime capability discovery requires a request.");
|
|
1195
|
-
return { status: "completed", capabilities: realtimeCapabilities(connection.
|
|
1426
|
+
return { status: "completed", capabilities: realtimeCapabilities(connection.listCapabilities(), query) };
|
|
1196
1427
|
}
|
|
1197
1428
|
if (name === "marona__call_capability") {
|
|
1198
1429
|
const capability = cleanString(argumentsValue.name);
|
|
@@ -1892,7 +2123,7 @@ class HubClient {
|
|
|
1892
2123
|
}
|
|
1893
2124
|
async connect(options = {}) {
|
|
1894
2125
|
const payload = await this.connectResult(options);
|
|
1895
|
-
return new MCPConnection(payload, this.toolExecutor);
|
|
2126
|
+
return new MCPConnection(payload, this.toolExecutor, options.apps === undefined && !(options.skills?.length));
|
|
1896
2127
|
}
|
|
1897
2128
|
async connectResult(options = {}) {
|
|
1898
2129
|
const payload = await this.requestJson("POST", "/hub/connect", {
|
|
@@ -2610,6 +2841,52 @@ class RuntimeTransport {
|
|
|
2610
2841
|
message(text, options = {}) {
|
|
2611
2842
|
return this.postRuntimeMessage(new RuntimeMessageRequest(text, options));
|
|
2612
2843
|
}
|
|
2844
|
+
async *streamMessage(text, options = {}) {
|
|
2845
|
+
const response = await this.fetchImpl(new URL("/runtime/message/stream", `${this.baseUrl}/`).toString(), {
|
|
2846
|
+
method: "POST",
|
|
2847
|
+
headers: {
|
|
2848
|
+
...this.defaultHeaders,
|
|
2849
|
+
Accept: "text/event-stream",
|
|
2850
|
+
"Content-Type": "application/json",
|
|
2851
|
+
},
|
|
2852
|
+
body: JSON.stringify(new RuntimeMessageRequest(text, options).toJson()),
|
|
2853
|
+
signal: AbortSignal.timeout(this.timeoutMs),
|
|
2854
|
+
});
|
|
2855
|
+
if (!response.ok) {
|
|
2856
|
+
throw new EdgeRuntimeError(`Edge runtime request failed with HTTP ${response.status}.`, { statusCode: response.status, payload: parseMaybeJson(await response.text()) });
|
|
2857
|
+
}
|
|
2858
|
+
if (!response.body) {
|
|
2859
|
+
throw new EdgeRuntimeError("Edge runtime returned an empty event stream.");
|
|
2860
|
+
}
|
|
2861
|
+
const reader = response.body.getReader();
|
|
2862
|
+
const decoder = new TextDecoder();
|
|
2863
|
+
let buffer = "";
|
|
2864
|
+
try {
|
|
2865
|
+
while (true) {
|
|
2866
|
+
const { done, value } = await reader.read();
|
|
2867
|
+
buffer += decoder.decode(value, { stream: !done });
|
|
2868
|
+
const lines = buffer.split(/\r?\n/);
|
|
2869
|
+
buffer = lines.pop() ?? "";
|
|
2870
|
+
for (const line of lines) {
|
|
2871
|
+
if (!line.startsWith("data:"))
|
|
2872
|
+
continue;
|
|
2873
|
+
const event = asRecord(parseMaybeJson(line.slice(5).trim()));
|
|
2874
|
+
if (event)
|
|
2875
|
+
yield event;
|
|
2876
|
+
}
|
|
2877
|
+
if (done)
|
|
2878
|
+
break;
|
|
2879
|
+
}
|
|
2880
|
+
if (buffer.startsWith("data:")) {
|
|
2881
|
+
const event = asRecord(parseMaybeJson(buffer.slice(5).trim()));
|
|
2882
|
+
if (event)
|
|
2883
|
+
yield event;
|
|
2884
|
+
}
|
|
2885
|
+
}
|
|
2886
|
+
finally {
|
|
2887
|
+
reader.releaseLock();
|
|
2888
|
+
}
|
|
2889
|
+
}
|
|
2613
2890
|
async createRealtimeSession(request) {
|
|
2614
2891
|
const sessionRequest = request instanceof RealtimeSessionRequest ? request : new RealtimeSessionRequest(request);
|
|
2615
2892
|
const payload = await this.requestJson("POST", "/runtime/realtime/session", {
|
|
@@ -2922,6 +3199,103 @@ class ResponsesClient {
|
|
|
2922
3199
|
}
|
|
2923
3200
|
throw new EdgeRuntimeError("Function tool execution exceeded 8 model rounds.");
|
|
2924
3201
|
}
|
|
3202
|
+
async *stream(options) {
|
|
3203
|
+
const toolChoice = normalizeToolChoice(options.toolChoice);
|
|
3204
|
+
if (toolChoice === "required" && !options.tools) {
|
|
3205
|
+
throw new EdgeRuntimeError("toolChoice='required' needs a Hub connection.");
|
|
3206
|
+
}
|
|
3207
|
+
const values = options.tools === undefined
|
|
3208
|
+
? []
|
|
3209
|
+
: Array.isArray(options.tools) ? options.tools : [options.tools];
|
|
3210
|
+
const functions = new Map();
|
|
3211
|
+
const descriptors = [];
|
|
3212
|
+
for (const value of values) {
|
|
3213
|
+
if (value instanceof MCPConnection)
|
|
3214
|
+
descriptors.push(...value.descriptors());
|
|
3215
|
+
else if (value instanceof FunctionTool) {
|
|
3216
|
+
if (functions.has(value.name))
|
|
3217
|
+
throw new EdgeRuntimeError(`Duplicate function tool name '${value.name}'.`);
|
|
3218
|
+
functions.set(value.name, value);
|
|
3219
|
+
descriptors.push(value.maronaDescriptor);
|
|
3220
|
+
}
|
|
3221
|
+
}
|
|
3222
|
+
const tools = normalizeConnectedTools(descriptors);
|
|
3223
|
+
const metadata = {
|
|
3224
|
+
...(options.metadata ?? {}),
|
|
3225
|
+
marona_agent: {
|
|
3226
|
+
...(asRecord(options.metadata?.marona_agent) ?? {}),
|
|
3227
|
+
tool_choice: toolChoice,
|
|
3228
|
+
tools_enabled: Boolean(options.tools) && toolChoice !== "none",
|
|
3229
|
+
},
|
|
3230
|
+
};
|
|
3231
|
+
if (functions.size) {
|
|
3232
|
+
metadata.marona_function_tools = [...functions.values()].map((tool) => ({
|
|
3233
|
+
name: tool.name,
|
|
3234
|
+
description: tool.description,
|
|
3235
|
+
input_schema: tool.inputSchema,
|
|
3236
|
+
output_schema: tool.outputSchema,
|
|
3237
|
+
}));
|
|
3238
|
+
}
|
|
3239
|
+
let results = [];
|
|
3240
|
+
for (let round = 0; round < 8; round += 1) {
|
|
3241
|
+
let response;
|
|
3242
|
+
for await (const event of this.runtime.clientStream({
|
|
3243
|
+
...options,
|
|
3244
|
+
model: options.model ?? "marona/gpt-5.6",
|
|
3245
|
+
metadata: {
|
|
3246
|
+
...metadata,
|
|
3247
|
+
...(results.length ? { marona_function_tool_results: results } : {}),
|
|
3248
|
+
},
|
|
3249
|
+
tools: toolChoice === "none" ? [] : tools,
|
|
3250
|
+
localFunctionTools: [...functions.values()],
|
|
3251
|
+
})) {
|
|
3252
|
+
if (event.type === "run.completed") {
|
|
3253
|
+
const payload = asRecord(event.output);
|
|
3254
|
+
if (payload)
|
|
3255
|
+
response = parseRuntimeMessageResponse(payload);
|
|
3256
|
+
continue;
|
|
3257
|
+
}
|
|
3258
|
+
yield event;
|
|
3259
|
+
}
|
|
3260
|
+
if (!response)
|
|
3261
|
+
throw new EdgeRuntimeError("Streaming runtime returned no completed response.");
|
|
3262
|
+
const calls = Array.isArray(response.agent.function_tool_calls)
|
|
3263
|
+
? response.agent.function_tool_calls
|
|
3264
|
+
: [];
|
|
3265
|
+
if (!calls.length) {
|
|
3266
|
+
yield { type: "run.completed", output: response.rawJson };
|
|
3267
|
+
return;
|
|
3268
|
+
}
|
|
3269
|
+
results = [];
|
|
3270
|
+
for (const raw of calls) {
|
|
3271
|
+
const call = asRecord(raw);
|
|
3272
|
+
const name = cleanString(call?.name);
|
|
3273
|
+
const tool = name ? functions.get(name) : undefined;
|
|
3274
|
+
if (!call || !name || !tool)
|
|
3275
|
+
throw new EdgeRuntimeError(`Function tool '${name ?? ""}' is not available in this SDK process.`);
|
|
3276
|
+
const callId = cleanString(call.call_id) ?? "";
|
|
3277
|
+
yield { type: "tool.started", data: { tool_name: name, call_id: callId } };
|
|
3278
|
+
const baseContext = asRecord(metadata.marona_tool_context) ?? {};
|
|
3279
|
+
const context = {
|
|
3280
|
+
userId: options.userId,
|
|
3281
|
+
sessionId: cleanString(baseContext.session_id) ?? options.conversationId ?? "default",
|
|
3282
|
+
developerId: cleanString(baseContext.developer_id),
|
|
3283
|
+
permissions: new Set(Array.isArray(baseContext.permissions) ? baseContext.permissions.map(String) : []),
|
|
3284
|
+
approvedTools: new Set(Array.isArray(baseContext.approved_tools) ? baseContext.approved_tools.map(String) : []),
|
|
3285
|
+
metadata: {},
|
|
3286
|
+
};
|
|
3287
|
+
const argumentsValue = asRecord(call.arguments) ?? {};
|
|
3288
|
+
results.push({
|
|
3289
|
+
call_id: callId,
|
|
3290
|
+
name,
|
|
3291
|
+
arguments: argumentsValue,
|
|
3292
|
+
output: await tool.invoke(argumentsValue, context),
|
|
3293
|
+
});
|
|
3294
|
+
yield { type: "tool.completed", data: { tool_name: name, call_id: callId } };
|
|
3295
|
+
}
|
|
3296
|
+
}
|
|
3297
|
+
throw new EdgeRuntimeError("Function tool execution exceeded 8 model rounds.");
|
|
3298
|
+
}
|
|
2925
3299
|
}
|
|
2926
3300
|
class RuntimeEngine extends RuntimeTransport {
|
|
2927
3301
|
hub;
|
|
@@ -3116,6 +3490,38 @@ class RuntimeEngine extends RuntimeTransport {
|
|
|
3116
3490
|
attachments: [...normalized.attachments, ...(attachments ?? [])],
|
|
3117
3491
|
});
|
|
3118
3492
|
}
|
|
3493
|
+
async *clientStream(options) {
|
|
3494
|
+
const { input, attachments, ...messageOptions } = options;
|
|
3495
|
+
const normalized = runtimeMessageFromInput(input);
|
|
3496
|
+
const modelRoute = cleanString(messageOptions.model ?? this.defaultModelRoute)
|
|
3497
|
+
?? "marona/gpt-5.6";
|
|
3498
|
+
const model = this.resolveModel(modelRoute);
|
|
3499
|
+
if (model) {
|
|
3500
|
+
const response = await this.client(options);
|
|
3501
|
+
yield { type: "run.started" };
|
|
3502
|
+
if (response.reply)
|
|
3503
|
+
yield { type: "output_text.delta", delta: response.reply };
|
|
3504
|
+
yield { type: "run.completed", output: runtimeResponsePayload(response) };
|
|
3505
|
+
return;
|
|
3506
|
+
}
|
|
3507
|
+
const developerInstructions = combineText([
|
|
3508
|
+
normalized.developerInstructions,
|
|
3509
|
+
this.developerInstructions,
|
|
3510
|
+
messageOptions.developerInstructions ?? messageOptions.developer_instructions,
|
|
3511
|
+
]);
|
|
3512
|
+
for await (const event of super.streamMessage(normalized.text, {
|
|
3513
|
+
...messageOptions,
|
|
3514
|
+
model: modelRoute,
|
|
3515
|
+
developerInstructions,
|
|
3516
|
+
attachments: [...normalized.attachments, ...(attachments ?? [])],
|
|
3517
|
+
metadata: {
|
|
3518
|
+
...(messageOptions.metadata ?? {}),
|
|
3519
|
+
...(messageOptions.tools ? { tools: messageOptions.tools } : {}),
|
|
3520
|
+
},
|
|
3521
|
+
})) {
|
|
3522
|
+
yield event;
|
|
3523
|
+
}
|
|
3524
|
+
}
|
|
3119
3525
|
async modelMessage(model, text, options) {
|
|
3120
3526
|
const messages = [
|
|
3121
3527
|
{
|
|
@@ -3554,6 +3960,19 @@ function parseRuntimeMessageResponse(payload) {
|
|
|
3554
3960
|
agent: asRecord(payload.agent) ?? {},
|
|
3555
3961
|
};
|
|
3556
3962
|
}
|
|
3963
|
+
function runtimeResponsePayload(response) {
|
|
3964
|
+
return {
|
|
3965
|
+
...response.rawJson,
|
|
3966
|
+
status: response.status,
|
|
3967
|
+
reply: response.reply,
|
|
3968
|
+
interface: response.interfaceName,
|
|
3969
|
+
conversation_id: response.conversationId,
|
|
3970
|
+
artifacts: response.artifacts,
|
|
3971
|
+
model: response.model,
|
|
3972
|
+
routing: response.routing,
|
|
3973
|
+
agent: response.agent,
|
|
3974
|
+
};
|
|
3975
|
+
}
|
|
3557
3976
|
function normalizeRouting(value) {
|
|
3558
3977
|
if (!value)
|
|
3559
3978
|
return undefined;
|