@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
69 lines • 3.14 kB
JavaScript
import { WIRE_STREAM_OPTION_KEYS } from "./wire.js";
function copyWireKey(from, to, key) {
const value = from[key];
if (value !== undefined)
to[key] = value;
}
/** Only allowlisted, serializable request shaping crosses to the hand: never `commitStep`, secrets, headers or callbacks. */
function toWireOptions(options) {
const wire = {};
if (options)
for (const key of WIRE_STREAM_OPTION_KEYS)
copyWireKey(options, wire, key);
return wire;
}
export const admittedRequestJson = (context, options) => JSON.stringify({ context, options: toWireOptions(options) });
const isRequest = (value) => typeof value === "object" &&
value !== null &&
(("context" in value && "options" in value) || "compactBody" in value);
const toWireRequest = (request) => "context" in request
? { context: request.context, options: toWireOptions(request.options) }
: { compactBody: request.compactBody };
/** The JSON text the ledger stores for a request that came back from a hook (`hooked`, V5). */
export const storedRequestJson = (request) => JSON.stringify(toWireRequest(request));
export function parseStoredRequest(json) {
const parsed = JSON.parse(json);
if (!isRequest(parsed))
throw new Error("stored egress request is not a provider-call request");
return toWireRequest(parsed);
}
/**
* The hook rules of 4.5: to the hand, `hooks` is `enabled` exactly when the stored request does not carry them yet;
* to any other server, a request-hooked provider without applied hooks runs them on the hand first and stores the
* post-hook request with `hooksApplied: true`; everything else goes with `hooks: "disabled"`.
*/
export async function planDispatch(decision, stored, runRequestHooks) {
if (decision.executorHooks === "enabled")
return { ...stored, hooks: stored.hooksApplied ? "disabled" : "enabled" };
if (!stored.hooksApplied && decision.requestHooksOn !== undefined) {
const hooked = await runRequestHooks(decision.requestHooksOn, parseStoredRequest(stored.requestJson));
return { requestJson: storedRequestJson(hooked), hooksApplied: true, hooks: "disabled" };
}
return { ...stored, hooks: "disabled" };
}
/** A fresh object per message: `Usage` is mutable and consumers update it in place. */
const noUsage = () => ({
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
});
/** The terminal message the loop receives when the hand refused or failed the call, or the step was aborted. */
export function failureMessage(model, stopReason, errorMessage, usage = noUsage()) {
return {
role: "assistant",
content: [],
api: model.api,
provider: model.provider,
model: model.id,
usage,
stopReason,
errorMessage,
timestamp: Date.now(),
};
}
export const ABORT_TEXT = "Request was aborted";
export const idleTimeoutText = (idleMs) => `Idle timeout waiting for provider stream after ${idleMs}ms`;
//# sourceMappingURL=device-proxy-request.js.map