Retain complete authenticated application Evidence when the repeated MCP representation exceeds the default receive frame or Node string limit. Return a typed transport constraint, keep useful followup queries available, and stream full canonical bundles to atomic destinations. Verify complete Notion and Obsidian workflows on Linux and native Windows, full CLI/Evidence parity, and real large Chrome screenshot recovery. Preserve complete success contracts and caller-selected evidence paths. Refs #1065, #1053.
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import type { Writable } from "node:stream";
|
|
|
|
import { bufferedJsonParts, jsonParts } from "../domain/jsonSerialization.js";
|
|
|
|
type JsonOutputFormat = "json" | "jsonl";
|
|
|
|
/** Write ordinary JSON without constructing a document-sized string. */
|
|
export const writeJsonOutput = async (
|
|
value: unknown,
|
|
destination: Writable,
|
|
format: JsonOutputFormat = "json",
|
|
): Promise<void> => {
|
|
for (const part of bufferedJsonParts(documentParts(value, format)))
|
|
await writePart(destination, part);
|
|
};
|
|
|
|
const writePart = (destination: Writable, part: string): Promise<void> =>
|
|
new Promise((resolve, reject) => {
|
|
const onError = (cause: unknown): void => reject(cause);
|
|
destination.once("error", onError);
|
|
try {
|
|
destination.write(part, (cause) => {
|
|
if (cause) {
|
|
// A Writable can emit its error after invoking the write callback.
|
|
setImmediate(() => destination.off("error", onError));
|
|
reject(cause);
|
|
} else {
|
|
destination.off("error", onError);
|
|
resolve();
|
|
}
|
|
});
|
|
} catch (cause: unknown) {
|
|
destination.off("error", onError);
|
|
reject(cause);
|
|
}
|
|
});
|
|
|
|
function* documentParts(
|
|
value: unknown,
|
|
format: JsonOutputFormat,
|
|
): Generator<string> {
|
|
if (format === "jsonl" && Array.isArray(value)) {
|
|
for (const item of value) {
|
|
yield* jsonParts(item);
|
|
yield "\n";
|
|
}
|
|
return;
|
|
}
|
|
yield* jsonParts(value, format === "json");
|
|
yield "\n";
|
|
}
|