@sema-agent/core
Version:
Stateless, task-oriented AI agent core
72 lines (71 loc) • 2.65 kB
JavaScript
import { writtenAssignmentPrefix } from "../tools/fs/bash-program-position.js";
import { isSensitiveRecordKey } from "./credential-vocabulary.js";
import { egressText } from "./untrusted-egress.js";
const ACTIVITY_ARG_MAX = 80;
function truncCodePoints(s) {
const cp = Array.from(s);
return cp.length > ACTIVITY_ARG_MAX ? cp.slice(0, ACTIVITY_ARG_MAX).join("") + "…" : s;
}
const basename = (p) => p.split(/[/\\]/).filter((x) => x.length > 0).pop() ?? p;
export function primaryActivityArg(args) {
if (!args || typeof args !== "object")
return undefined;
const a = args;
const str = (k) => typeof a[k] === "string" && a[k].length > 0 ? a[k] : undefined;
let val;
const command = str("command");
const path = str("file_path") ?? str("filePath") ?? str("path") ?? str("notebook_path");
const url = str("url");
if (command !== undefined) {
const tokens = command.trim().split(/\s+/);
let i = 0;
while (i < tokens.length && writtenAssignmentPrefix(tokens[i]) !== undefined) {
if (/["'\\]/.test(tokens[i]))
return undefined;
i++;
}
if (i >= tokens.length)
return undefined;
const name = basename(tokens[i]);
val = tokens.length > i + 1 ? `${name} …` : name;
}
else if (path !== undefined) {
val = basename(path);
}
else if (url !== undefined) {
const parsed = (() => { try {
return new URL(url);
}
catch {
return undefined;
} })();
if (parsed !== undefined && parsed.host !== "") {
val = `${parsed.origin}${parsed.pathname}`;
}
else {
const head = url.split(/[?#]/, 1)[0] ?? url;
const sep = head.indexOf("://");
const scheme = sep > 0 && /^[a-z][a-z0-9+.-]*$/i.test(head.slice(0, sep)) ? head.slice(0, sep + 3) : "";
const rest = head.slice(scheme.length);
const at = rest.lastIndexOf("@");
val = scheme + (at === -1 ? rest : rest.slice(at + 1));
}
}
else {
val = str("pattern") ?? str("query") ?? str("description");
if (val === undefined) {
for (const k of Object.keys(a)) {
if (isSensitiveRecordKey(k))
continue;
const v = a[k];
if (typeof v === "string" && v.length > 0) {
val = v;
break;
}
}
}
}
if (val === undefined)
return undefined;
return truncCodePoints(egressText("same-principal", val));
}