UNPKG

akm-cli

Version:

akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur

43 lines (42 loc) • 1.95 kB
// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. import fs from "node:fs"; import path from "node:path"; import { stdout } from "../core/stdio.js"; /** * The single stdout writer for result documents. * * WHY NOT `console.log`: on Bun, once `process.stdout` has been materialized * as a Node-compat stream — which writing a result through it does — * `console.log(bigString)` issues a SINGLE `write(2)` against a * non-blocking fd 1 and silently discards whatever the kernel refused. A pipe * accepts at most one buffer (65,536 bytes on Linux), so `akm show … | * python3` lost everything past 64 KiB while `--output <file>` wrote the * whole document. `process.stdout.write` handles the short write correctly on * both runtimes, and the queued remainder keeps the event loop alive until it * drains, so the natural exit at the end of `runCli` still flushes it. * * The trailing newline is UNCONDITIONAL because that is exactly what * `console.log` appended: this is a transport fix, not a format change, and * `tests/integration/output-baseline.test.ts` pins the bytes either way. */ export function writeStdout(document) { stdout().write(stdoutText(document)); } /** The exact bytes {@link writeStdout} writes for `document`; in-process callers return this instead of writing. */ export function stdoutText(document) { return `${document}\n`; } /** * Deliver a rendered document: write to `outputPath` when set (`--output`), * otherwise print to stdout. */ export function deliverRendered(content, outputPath) { if (outputPath) { fs.mkdirSync(path.dirname(path.resolve(outputPath)), { recursive: true }); fs.writeFileSync(outputPath, content.endsWith("\n") ? content : `${content}\n`); return; } writeStdout(content); }