openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
45 lines (44 loc) • 1.97 kB
JavaScript
import { a as memoizeFrozenPlainDataProjection } from "./immutable-data-Y7xVjZoy.mjs";
import { m as readRuntimePromptMediaFacts, p as readPersistedMediaFacts, r as attachRuntimePromptMediaFacts, s as isImageMediaFact } from "./media-facts-B-0Dpo5L.mjs";
import { o as convertToLlm$1 } from "./messages-MSizso-z.mjs";
//#region src/agents/sessions/messages.ts
/** Session message conversion and image policy, shared by normal turns and fork reconstruction. */
const convertMessage = memoizeFrozenPlainDataProjection((e) => convertToLlm$1([e]));
function convertToLlm(e) {
const t = [];
e.forEach((e) => {
if (e.role === "user" || e.role === "assistant" || e.role === "toolResult") t.push(e);
else t.push(...convertMessage(e));
});
return t;
}
/** Apply the session's image policy before either inline or deferred media reaches a provider. */
function convertToLlmWithBlockImages(e, t) {
const n = convertToLlm(e);
if (!t) return n;
return n.map(blockMessageImages);
}
const blockMessageImages = memoizeFrozenPlainDataProjection((e) => {
if (e.role !== "user" && e.role !== "toolResult") return e;
const t = readRuntimePromptMediaFacts(e) ?? readPersistedMediaFacts(e);
const n = t?.some(isImageMediaFact) === true;
const r = Array.isArray(e.content) ? e.content : [{
type: "text",
text: e.content
}];
const i = r.some((e) => e.type === "image");
if (!i && !n) return e;
const a = {
type: "text",
text: "Image reading is disabled."
};
const o = r.map((e) => e.type === "image" ? a : e);
if (!i && n) o.push(a);
const s = Object.assign({}, e, { content: o.filter((e, t, n) => {
const r = n.at(t - 1);
return !(t > 0 && e.type === "text" && e.text === a.text && r?.type === "text" && r.text === a.text);
}) });
return n && t ? attachRuntimePromptMediaFacts(s, t.map((e) => isImageMediaFact(e) ? Object.assign({}, e, { hydrationSuppressed: true }) : e)) : s;
});
//#endregion
export { convertToLlmWithBlockImages as n, convertToLlm as t };