openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
145 lines (144 loc) • 4.29 kB
JavaScript
import { n as validateJsonSchemaValue } from "../../schema-validator-NvoOmaWr.mjs";
import { b as createAssistantMessageEventStream } from "../../llm-Bp_VCUmP.mjs";
import "../../json-schema-runtime-OTpXIHVF.mjs";
import { r as quoteUnsafeIntegerLiterals } from "../../json-unsafe-integers-DcKagBUk.mjs";
import { t as buildAssistantMessage } from "../../stream-message-shared-CBUzFwdv.mjs";
import { f as createEmptyTransportUsage, i as applyProviderPayloadHook, y as failTransportStream } from "../../provider-transport-runtime-Kmp3OdV6.mjs";
//#region extensions/apple-fm/stream.ts
/** Native tools propose calls; the OpenClaw agent loop validates and executes them. */
function createAppleFmStream(e) {
return (t, n, r) => {
const i = createAssistantMessageEventStream();
const a = buildAssistantMessage({
model: t,
content: [],
usage: createEmptyTransportUsage(),
stopReason: "stop"
});
i.push({
type: "start",
partial: a
});
(async () => {
try {
r?.signal?.throwIfAborted();
const o = r?.responseFormat;
const s = {
systemPrompt: n.systemPrompt,
messages: n.messages,
tools: n.tools ?? [],
maxTokens: r?.maxTokens ?? t.maxTokens,
temperature: r?.temperature,
responseFormat: o ? structuredClone(o) : void 0
};
const c = await applyProviderPayloadHook(r, s, t) ?? s;
if (!c || typeof c !== "object") throw new Error("Apple Foundation Models requires an object request.");
r?.signal?.throwIfAborted();
const l = await e.run(c, { signal: r?.signal });
r?.signal?.throwIfAborted();
if (o && l.toolCalls.length === 0) {
const e = "Apple Foundation Models returned an invalid structured response: an unsafe numeric value cannot be validated without losing precision.";
if (quoteUnsafeIntegerLiterals(l.text) !== l.text) throw new Error(e);
let t;
try {
t = JSON.parse(l.text, (t, n) => {
if (typeof n === "number" && (!Number.isFinite(n) || Number.isInteger(n) && !Number.isSafeInteger(n))) throw new Error(e);
return n;
});
} catch (e) {
if (!(e instanceof SyntaxError)) throw e;
}
if (t === void 0) throw new Error("Apple Foundation Models returned an invalid structured response: malformed JSON.");
const n = validateJsonSchemaValue({
schema: o,
cacheKey: "apple-fm.structured-response",
value: t,
applyDefaults: false
});
if (!n.ok) throw new Error(`Apple Foundation Models returned an invalid structured response at ${n.errors.map((e) => e.path).join(", ")}.`);
}
a.usage.input = l.inputTokens;
a.usage.output = l.outputTokens;
a.usage.totalTokens = l.inputTokens + l.outputTokens;
a.usage.cacheTelemetry = { state: "unavailable" };
a.usage.contextUsage = {
state: "available",
promptTokens: l.inputTokens,
totalTokens: a.usage.totalTokens
};
if (l.text) {
a.content.push({
type: "text",
text: l.text
});
i.push({
type: "text_start",
contentIndex: 0,
partial: a
});
i.push({
type: "text_delta",
contentIndex: 0,
delta: l.text,
partial: a
});
i.push({
type: "text_end",
contentIndex: 0,
content: l.text,
partial: a
});
}
for (const e of l.toolCalls) {
const t = {
type: "toolCall",
...e
};
const n = {
...t,
partialJson: JSON.stringify(e.arguments)
};
const r = a.content.length;
a.content.push(t);
i.push({
type: "toolcall_start",
contentIndex: r,
partial: a
});
i.push({
type: "toolcall_delta",
contentIndex: r,
delta: n.partialJson,
partial: {
...a,
content: [...a.content.slice(0, -1), n]
}
});
i.push({
type: "toolcall_end",
contentIndex: r,
toolCall: t,
partial: a
});
}
a.stopReason = l.toolCalls.length ? "toolUse" : "stop";
i.push({
type: "done",
reason: a.stopReason,
message: a
});
i.end(a);
} catch (e) {
failTransportStream({
stream: i,
output: a,
error: e,
signal: r?.signal
});
}
})();
return i;
};
}
//#endregion
export { createAppleFmStream };