@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
118 lines • 4.64 kB
JavaScript
import { diagnostic } from "./diagnostics.js";
export function stopOutputRecords(result) {
return result.summaries.flatMap((summary) => {
const fields = stopOutputFieldPaths(summary);
if (fields.length === 0)
return [];
return [
{
event: "Stop",
exitCode: summary.run.exitCode,
fields,
sourcePath: summary.handler.source.sourcePath,
},
];
});
}
export function unsupportedStopOutputDiagnostics(result) {
const diagnostics = [];
for (const summary of result.summaries) {
diagnostics.push(...unsupportedSummaryDiagnostics(summary));
}
return diagnostics;
}
function unsupportedSummaryDiagnostics(summary) {
const diagnostics = [];
if (summary.output.systemMessage !== undefined) {
diagnostics.push(unsupportedField(summary, "stdout.systemMessage", "Stop does not support systemMessage."));
}
if (summary.output.suppressOutput !== undefined) {
diagnostics.push(unsupportedField(summary, "stdout.suppressOutput", "Stop does not support suppressOutput."));
}
if (summary.output.stopReason !== undefined) {
diagnostics.push(unsupportedField(summary, "stdout.stopReason", "Stop output stopReason is diagnostic-only."));
}
if (summary.output.updatedInput !== undefined) {
diagnostics.push(unsupportedField(summary, "stdout.updatedInput", "Stop does not support updatedInput."));
}
if (summary.output.updatedToolOutput !== undefined) {
diagnostics.push(unsupportedField(summary, "stdout.updatedToolOutput", "Stop does not support updatedToolOutput."));
}
for (const path of rawUnsupportedStopFieldPaths(summary)) {
diagnostics.push(unsupportedField(summary, path, `Stop does not support ${path.replace("stdout.", "")}.`));
}
return diagnostics;
}
function stopOutputFieldPaths(summary) {
const fields = [];
if (summary.output.decision !== undefined)
fields.push("stdout.decision");
if (summary.output.reason !== undefined)
fields.push("stdout.reason");
if (summary.output.additionalContext !== undefined) {
const raw = rawStdoutObject(summary.run.stdout);
const specific = raw === undefined ? undefined : rawHookSpecificOutput(raw);
fields.push(specific !== undefined && Object.hasOwn(specific, "additionalContext")
? "stdout.hookSpecificOutput.additionalContext"
: "stdout.additionalContext");
}
if (summary.output.continue !== undefined)
fields.push("stdout.continue");
if (summary.output.stopReason !== undefined)
fields.push("stdout.stopReason");
if (summary.output.suppressOutput !== undefined)
fields.push("stdout.suppressOutput");
if (summary.output.systemMessage !== undefined)
fields.push("stdout.systemMessage");
fields.push(...rawUnsupportedStopFieldPaths(summary));
return fields;
}
function rawUnsupportedStopFieldPaths(summary) {
const raw = rawStdoutObject(summary.run.stdout);
if (raw === undefined)
return [];
const fields = [];
pushFieldIfPresent(fields, raw, "updatedInput", "stdout.updatedInput");
pushFieldIfPresent(fields, raw, "updatedToolOutput", "stdout.updatedToolOutput");
const specific = rawHookSpecificOutput(raw);
if (specific !== undefined) {
pushFieldIfPresent(fields, specific, "updatedInput", "stdout.hookSpecificOutput.updatedInput");
pushFieldIfPresent(fields, specific, "updatedToolOutput", "stdout.hookSpecificOutput.updatedToolOutput");
}
return fields;
}
function pushFieldIfPresent(fields, record, key, path) {
if (Object.hasOwn(record, key))
fields.push(path);
}
function rawStdoutObject(stdout) {
const trimmed = stdout.trim();
if (trimmed.length === 0)
return undefined;
try {
const parsed = JSON.parse(trimmed);
return isRecord(parsed) ? parsed : undefined;
}
catch (error) {
if (error instanceof SyntaxError)
return undefined;
throw error;
}
}
function rawHookSpecificOutput(raw) {
const value = raw.hookSpecificOutput;
return isRecord(value) ? value : undefined;
}
function unsupportedField(summary, path, message) {
return diagnostic({
code: "unsupported_field",
event: "Stop",
message,
path,
severity: "warning",
}, summary.handler.source);
}
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
//# sourceMappingURL=stop-output.js.map