openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
104 lines (103 loc) • 3.66 kB
JavaScript
import { o as resolveFreshSessionTotalTokens } from "./types-BUko5WqP.mjs";
import crypto from "node:crypto";
//#region src/config/sessions/goals-transitions.ts
var SessionGoalTransitionError = class extends Error {
constructor(...e) {
super(...e);
this.name = "SessionGoalTransitionError";
}
};
function normalizeTokenCount(e) {
return typeof e === "number" && Number.isFinite(e) && e >= 0 ? Math.floor(e) : void 0;
}
function resolveEntryFreshTotalTokens(e) {
return normalizeTokenCount(resolveFreshSessionTotalTokens(e));
}
function normalizeTokenBudget(e) {
const t = normalizeTokenCount(e);
return t && t > 0 ? t : void 0;
}
function accountSessionGoalUsage(e, t, n) {
const r = e.goal;
if (!r) return;
const i = resolveEntryFreshTotalTokens(e);
const a = r.tokenStartFresh !== false;
const o = !a && n?.adoptFreshBaseline === false;
const s = !o && i !== void 0 && !a;
const c = s ? i : normalizeTokenCount(r.tokenStart) ?? i ?? 0;
const l = i === void 0 || s || o ? r.tokensUsed : Math.max(r.tokensUsed, Math.max(0, i - c));
const u = {
...r,
tokenStart: c,
tokenStartFresh: a || s,
tokensUsed: l
};
if (u.status === "active" && u.tokenBudget !== void 0 && l >= u.tokenBudget) {
u.status = "budget_limited";
u.budgetLimitedAt = t;
u.updatedAt = t;
}
return u;
}
function buildCreatedSessionGoal(e, t, n) {
const r = t.objective;
if (!r.trim()) throw new SessionGoalTransitionError("objective required");
if (e.goal) throw new SessionGoalTransitionError("goal already exists");
const i = normalizeTokenBudget(t.tokenBudget);
const a = resolveEntryFreshTotalTokens(e);
return {
schemaVersion: 1,
id: crypto.randomUUID(),
objective: r,
status: "active",
createdAt: n,
updatedAt: n,
tokenStart: a ?? 0,
tokenStartFresh: a !== void 0,
tokensUsed: 0,
...i ? { tokenBudget: i } : {},
continuationTurns: 0
};
}
function buildUpdatedSessionGoalStatus(e, t, n) {
const r = accountSessionGoalUsage(e, n);
if (!r) throw new SessionGoalTransitionError("goal not found");
if (r.status === "complete" && t.status !== "complete" && t.status !== "active") throw new SessionGoalTransitionError(`goal is already ${r.status}`);
const i = t.status === "active" && (r.status === "budget_limited" || r.status === "usage_limited" || r.tokenBudget !== void 0 && r.tokensUsed >= r.tokenBudget);
const a = i ? resolveEntryFreshTotalTokens(e) : void 0;
const o = {
...r,
status: t.status,
updatedAt: n,
...t.note ? { lastStatusNote: t.note } : {},
...t.status === "paused" ? { pausedAt: n } : {},
...t.status === "blocked" ? { blockedAt: n } : {},
...t.status === "complete" ? { completedAt: r.completedAt ?? n } : {}
};
if (r.status === "complete" && t.status === "active") delete o.completedAt;
if (i) {
o.tokenStart = a ?? 0;
o.tokenStartFresh = a !== void 0;
o.tokensUsed = 0;
delete o.budgetLimitedAt;
delete o.usageLimitedAt;
}
if (o.status === "active" && o.tokenBudget !== void 0 && o.tokensUsed >= o.tokenBudget) {
o.status = "budget_limited";
o.budgetLimitedAt = n;
}
return o;
}
function buildUpdatedSessionGoalObjective(e, t, n) {
if (!t.trim()) throw new SessionGoalTransitionError("objective required");
const r = accountSessionGoalUsage(e, n);
if (!r) throw new SessionGoalTransitionError("goal not found");
if (r.status === "complete") throw new SessionGoalTransitionError(`goal is already ${r.status}`);
return {
...r,
objective: t,
updatedAt: n
};
}
//#endregion
export { buildUpdatedSessionGoalStatus as a, buildUpdatedSessionGoalObjective as i, accountSessionGoalUsage as n, buildCreatedSessionGoal as r, SessionGoalTransitionError as t };