← Files Knowledge for AgentsARCHIVED FILE

scripts/kfa-session-adapter.mjs

1.37 KB · Nov 11, 2026 · 12:32 UTC

↓ Download file

See the change to this file →

#!/usr/bin/env node
// Thin client adapter: normalize a lifecycle event into one shared policy decision.
import { readFileSync } from "node:fs";
import {
  decideCloseout,
  decideError,
  decideFetch,
  decideStartup,
} from "./companion/default-policy.mjs";

export function decideLifecycle({ agent, event, input = {} }) {
  if (!['codex', 'claude'].includes(agent)) throw Error('AGENT_REQUIRED');
  const decisions = {
    startup: decideStartup,
    error: decideError,
    fetch: decideFetch,
    closeout: decideCloseout,
  };
  if (!decisions[event]) throw Error('LIFECYCLE_EVENT_REQUIRED');
  return { agent, event, decision: decisions[event](input) };
}

if (import.meta.url === new URL(process.argv[1], 'file:').href) {
  const args = process.argv.slice(2);
  const flag = (name) => {
    const i = args.indexOf('--' + name);
    return i < 0 ? undefined : args[i + 1];
  };
  try {
    const file = flag('file');
    if (!file) throw Error('INPUT_FILE_REQUIRED');
    const input = JSON.parse(readFileSync(file === '-' ? 0 : file, 'utf8'));
    process.stdout.write(
      JSON.stringify(
        decideLifecycle({ agent: flag('agent'), event: flag('event'), input }),
        null,
        2,
      ) + '\n',
    );
  } catch (error) {
    process.stdout.write(
      JSON.stringify({ error: error instanceof Error ? error.message : 'REFUSED' }) + '\n',
    );
    process.exitCode = 1;
  }
}

SHA-256: bffd2d7a17e42a9f5e4fdba051675dff4afe9c109546b072d0d7850ec1c5f08d