@sema-agent/core
Version:
Stateless, task-oriented AI agent core
56 lines (55 loc) • 1.59 kB
JavaScript
export function valueTypeLabel(value) {
if (value === null)
return "null";
switch (typeof value) {
case "string":
return "a string";
case "undefined":
return "undefined";
case "number":
return "a number";
case "boolean":
return "a boolean";
case "bigint":
return "a bigint";
case "symbol":
return "a symbol";
case "function":
return "a function";
default:
try {
return Array.isArray(value) ? "an array" : "an object";
}
catch {
return "an object";
}
}
}
const isList = (value) => Array.isArray(value);
export const UNREADABLE_LIST = Object.freeze({});
export function readToolNameList(value) {
if (value === UNREADABLE_LIST)
return { kind: "unreadable" };
let list;
try {
list = isList(value) ? value : undefined;
}
catch {
return { kind: "not_a_list", type: "an object" };
}
if (list === undefined)
return { kind: "not_a_list", type: valueTypeLabel(value) };
try {
const entries = [];
for (let i = 0; i < list.length; i++)
entries.push(list[i]);
return { kind: "entries", entries };
}
catch {
return { kind: "unreadable" };
}
}
export function snapshotToolNameList(value) {
const read = readToolNameList(value);
return read.kind === "entries" ? read.entries : read.kind === "unreadable" ? UNREADABLE_LIST : value;
}