@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
106 lines • 4.24 kB
JavaScript
import { redactAcrossBoundary } from "./share-entropy.js";
import { count, redactShareText } from "./share-redaction.js";
import { isSensitiveKeyName } from "./share-sensitive-key.js";
/** Any provider's image block: pi `{type:"image"}`, Anthropic `{type:"image", source}`, OpenAI `image_url`/`input_image`. */
function isImageBlock(value) {
if (typeof value.type === "string" && /image/i.test(value.type))
return true;
const source = value.source;
return typeof source === "object" && source !== null && Reflect.get(source, "type") === "base64";
}
const SCHEMA_TYPES = new Set(["string", "number", "integer", "boolean", "object", "array", "null"]);
const SCHEMA_KEYWORDS = new Set([
"type",
"description",
"title",
"format",
"enum",
"items",
"properties",
"required",
"minLength",
"maxLength",
"pattern",
"additionalProperties",
"nullable",
]);
/** A JSON-schema node (a tool parameter named `apiKey`) describes a credential; it does not hold one. */
function isSchemaNode(value) {
if (value === null || typeof value !== "object" || Array.isArray(value))
return false;
const type = Reflect.get(value, "type");
if (typeof type !== "string" || !SCHEMA_TYPES.has(type))
return false;
return Object.keys(value).every((key) => SCHEMA_KEYWORDS.has(key));
}
/** Every string under a sensitive key is a credential, whatever the nesting (`{"Authorization": ["Bearer x"]}`). */
function dropAllStrings(value, tally) {
if (typeof value === "string") {
if (value.length === 0)
return value;
if (value !== "<redacted>")
count(tally, "credential");
return "<redacted>";
}
if (Array.isArray(value))
return value.map((item) => dropAllStrings(item, tally));
if (value === null || typeof value !== "object")
return value;
return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, dropAllStrings(child, tally)]));
}
/**
* Redacts every string reachable from `value` (keys stay, they are schema);
* every value under a sensitive-named key is dropped whole, and image payloads
* are replaced by a text note because their pixels cannot be checked.
*/
export function redactShareValue(value, context, tally) {
return redactNode(value, context, tally);
}
function redactNode(value, context, tally) {
if (typeof value === "string")
return redactShareText(value, context, tally);
if (Array.isArray(value))
return joinAdjacentText(value.map((item) => redactNode(item, context, tally)), tally);
if (value === null || typeof value !== "object")
return value;
const record = value;
if (isImageBlock(record)) {
count(tally, "image");
return { type: "text", text: "[image omitted from share]" };
}
const out = {};
for (const [key, child] of Object.entries(record)) {
out[key] =
isSensitiveKeyName(key) && !isSchemaNode(child)
? dropAllStrings(child, tally)
: redactNode(child, context, tally);
}
return out;
}
function textOf(item) {
if (typeof item === "string")
return item;
if (item === null || typeof item !== "object" || Reflect.get(item, "type") !== "text")
return undefined;
const text = Reflect.get(item, "text");
return typeof text === "string" ? text : undefined;
}
function withText(item, text) {
return typeof item === "string" ? text : { ...item, text };
}
/** A token split across two adjacent text blocks or strings is one value; both halves go. */
function joinAdjacentText(items, tally) {
for (let index = 0; index + 1 < items.length; index++) {
const left = textOf(items[index]);
const right = textOf(items[index + 1]);
if (left === undefined || right === undefined)
continue;
const [nextLeft, nextRight] = redactAcrossBoundary(left, right, () => count(tally, "credential"));
if (nextLeft !== left) {
items[index] = withText(items[index], nextLeft);
items[index + 1] = withText(items[index + 1], nextRight);
}
}
return items;
}
//# sourceMappingURL=share-redaction-value.js.map