@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
117 lines • 5.78 kB
JavaScript
import { PROVIDER_CALL_ERROR_CODES } from "./wire.js";
export const REQUEST_CHUNK_BYTES = 96 * 1024;
const pad = (index) => String(index).padStart(6, "0");
export const callKey = (callId) => `egress_call/${encodeURIComponent(callId)}`;
export const requestPrefix = (callId) => `egress_call_request/${encodeURIComponent(callId)}/`;
export const requestChunkKey = (callId, index) => `${requestPrefix(callId)}${pad(index)}`;
export const resultPrefix = (callId) => `egress_call_result/${encodeURIComponent(callId)}/`;
export const resultChunkKey = (callId, index) => `${resultPrefix(callId)}${pad(index)}`;
export const stepIndexPrefix = (stepId) => `egress_step/${encodeURIComponent(stepId)}/`;
export const stepIndexKey = (stepId, attempt) => `${stepIndexPrefix(stepId)}${pad(attempt)}`;
export const cancelledKey = (stepId) => `session/step.cancelled/${encodeURIComponent(stepId)}`;
export const variantPrefix = (providerId) => `session/variants/${encodeURIComponent(providerId)}/`;
export const variantKey = (providerId, deviceId) => `${variantPrefix(providerId)}${encodeURIComponent(deviceId)}`;
/** Splits UTF-8 text into chunks of at most `REQUEST_CHUNK_BYTES` bytes, never inside a code point. */
export function splitUtf8(text) {
const bytes = new TextEncoder().encode(text);
const decoder = new TextDecoder();
const chunks = [];
for (let start = 0; start < bytes.length;) {
let end = Math.min(start + REQUEST_CHUNK_BYTES, bytes.length);
while (end < bytes.length && ((bytes[end] ?? 0) & 0xc0) === 0x80)
end--;
chunks.push(decoder.decode(bytes.subarray(start, end)));
start = end;
}
return chunks.length > 0 ? chunks : [""];
}
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
const isErrorCode = (value) => PROVIDER_CALL_ERROR_CODES.some((code) => code === value);
const isState = (value) => value === "dispatched" || value === "ended" || value === "superseded" || value === "cancelled" || value === "closed";
function decodeCall(value) {
if (!isObject(value))
return undefined;
if (value.kind === "step" && typeof value.stepId === "string" && typeof value.attempt === "number")
return { kind: "step", stepId: value.stepId, attempt: value.attempt };
const { auxId, streamKind, auxKind } = value;
if (value.kind !== "aux" || typeof auxId !== "string" || typeof streamKind !== "string")
return undefined;
if (auxKind !== "stream" && auxKind !== "warm" && auxKind !== "compact-endpoint")
return undefined;
return { kind: "aux", auxId, streamKind, auxKind };
}
/** The ledger's own result JSON; the shape check guards against a truncated or foreign document. */
function isCallResult(value) {
if (typeof value !== "object" || value === null || !("kind" in value))
return false;
if (value.kind === "message")
return "chunks" in value && typeof value.chunks === "number" && "stopReason" in value && "usage" in value;
return value.kind === "error" && "code" in value && isErrorCode(value.code) && "message" in value;
}
/** The stored message text is the ledger's own JSON; the shape check guards against a truncated or foreign chunk set. */
export function isStoredMessage(value) {
if (typeof value !== "object" || value === null || !("role" in value) || value.role !== "assistant")
return false;
return "content" in value && Array.isArray(value.content) && "stopReason" in value && "usage" in value;
}
export function encodeCallRow(row) {
const { deviceId, result, ...fields } = row;
return {
...fields,
...(deviceId === undefined ? {} : { deviceId }),
...(result === undefined ? {} : { result: JSON.stringify(result) }),
};
}
export function decodeCallRow(value) {
if (!isObject(value))
return undefined;
const { callId, sessionId, route, deviceId, providerId, modelId, epoch, capability, hooksApplied, state } = value;
const { requestChunks, result } = value;
const call = decodeCall(value.call);
if (typeof callId !== "string" || typeof sessionId !== "string" || !call)
return undefined;
if ((route !== "sealed" && route !== "device") || typeof providerId !== "string" || typeof modelId !== "string")
return undefined;
if (typeof epoch !== "number" || typeof hooksApplied !== "boolean" || typeof requestChunks !== "number")
return undefined;
if ((capability !== "callback-stream" && capability !== "stream") || !isState(state))
return undefined;
const parsed = typeof result === "string" ? JSON.parse(result) : undefined;
if (result !== undefined && !isCallResult(parsed))
return undefined;
return {
callId,
sessionId,
call,
route,
providerId,
modelId,
epoch,
capability,
hooksApplied,
state,
requestChunks,
...(typeof deviceId === "string" ? { deviceId } : {}),
...(isCallResult(parsed) ? { result: parsed } : {}),
};
}
export function decodeVariant(value) {
if (!isObject(value))
return undefined;
const { headDigest, liveAtMismatch, mismatches, stepId } = value;
if (typeof headDigest !== "string" || typeof mismatches !== "number" || typeof stepId !== "string")
return undefined;
return {
headDigest,
liveAtMismatch: typeof liveAtMismatch === "string" ? liveAtMismatch : undefined,
mismatches,
stepId,
};
}
export const encodeVariant = (variant) => ({
headDigest: variant.headDigest,
liveAtMismatch: variant.liveAtMismatch ?? null,
mismatches: variant.mismatches,
stepId: variant.stepId,
});
//# sourceMappingURL=ledger-schema.js.map