openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
102 lines (101 loc) • 3.71 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { n as validateDecisionResultV2, t as validateDecisionBatchV2 } from "./validation-v2-60LJzO-m.mjs";
import "./string-coerce-runtime-BwAZZDf5.mjs";
import { t as decodeSystemOneResponse } from "./decisions-Cw6RZ9do.mjs";
//#region extensions/google/decision-codec.ts
const QUESTION_KEY = /^[A-Za-z0-9_-]{1,64}$/;
function text(e) {
return typeof e === "string" ? e : e == null ? "" : JSON.stringify(e);
}
/**
* Gemini requires non-empty instructions, but OpenClaw makes them optional when the
* criteria already define the judgment. A neutral prompt asks the rubric itself.
*/
function instructionsFor(e) {
const t = text(e.instructions);
if (t) return t;
if (e.type === "choice") return "Which option best fits the input?";
if (e.type === "score") return "Which level best describes the input?";
return e.type === "boolean" && (e.criteria?.true != null || e.criteria?.false != null) ? "Does the true description apply to the input?" : "";
}
/** Admit only batches the native wire can represent; malformed caller data still throws. */
function supportsGeminiDecisionBatch(e) {
if (!validateDecisionBatchV2(e) || e.state.type === "list") return false;
if (e.state.type === "text" && !e.state.text) return false;
return Object.entries(e.questions).every(([e, t]) => QUESTION_KEY.test(e) && instructionsFor(t) !== "" && (t.type === "boolean" || t.type === "choice" || t.type === "score" && t.criteria.every((e) => text(e) !== "")));
}
function encodeInput(e) {
if (e.type === "text") return e.text;
if (e.type === "json") return JSON.stringify(e.value);
const [t = "", n = ""] = e.dataUri.split(",", 2);
const r = {
type: "image",
data: n,
mime_type: t.slice(5).split(";", 1)[0]
};
return e.text ? [{
type: "text",
text: e.text
}, r] : [r];
}
function encodeQuestions(e) {
return Object.fromEntries(Object.entries(e.questions).map(([e, t]) => {
const n = instructionsFor(t);
if (t.type === "boolean") {
const r = t.criteria;
return [e, {
type: "noul",
instructions: n,
...r && (r.true != null || r.false != null) ? { criteria: {
...r.true != null ? { true_description: text(r.true) } : {},
...r.false != null ? { false_description: text(r.false) } : {}
} } : {}
}];
}
if (t.type === "choice") return [e, {
type: "choice",
instructions: n,
criteria: Object.fromEntries(Object.entries(t.criteria).map(([e, t]) => [e, t == null ? null : text(t)]))
}];
if (t.type !== "score") throw new Error("Unsupported Gemini decision question");
return [e, {
type: "score",
instructions: n,
criteria: t.criteria.map(text)
}];
}));
}
function encodeGeminiDecisionRequest(e, t) {
return JSON.stringify({
model: t,
input: encodeInput(e.state),
questions: encodeQuestions(e)
});
}
function decodeGeminiDecisionResult(e, t, n, r) {
const i = {
status: "unavailable",
reason: "invalid-response"
};
const { answers: a, usage: o } = t;
if (!isRecord(a) || !isRecord(o) || o.total_output_tokens !== 0) return i;
for (const [t, n] of Object.entries(e.questions)) {
if (n.type !== "score") continue;
const e = isRecord(a[t]) ? a[t].legend : void 0;
if (!isRecord(e) || Object.keys(e).length !== n.criteria.length || n.criteria.some((t, n) => e[String(n)] !== text(t))) return i;
}
const s = decodeSystemOneResponse(e, {
model: n,
answers: a,
usage: {
input_tokens: o.total_input_tokens,
output_tokens: o.total_output_tokens
}
});
return validateDecisionResultV2(e, s, r) ? {
status: "ok",
result: s
} : i;
}
//#endregion
export { encodeGeminiDecisionRequest as n, supportsGeminiDecisionBatch as r, decodeGeminiDecisionResult as t };