UNPKG

@posthog/agent

Version:

TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog

46 lines (44 loc) 1.58 kB
/** * Creates a WritableStream wrapper that taps all newline-delimited messages, * forwarding each complete line for persistence. * * This aligns with ACP's transport model - all messages flow through * newline-delimited JSON-RPC streams, so we intercept at the transport layer * and persist everything. */ function createTappedWritableStream(underlying, options) { const { onMessage, logger } = options; const decoder = new TextDecoder(); let buffer = ""; return new WritableStream({ async write(chunk) { // Decode and buffer buffer += decoder.decode(chunk, { stream: true }); // Process complete lines (newline-delimited) const lines = buffer.split("\n"); buffer = lines.pop() ?? ""; for (const line of lines) { if (!line.trim()) continue; onMessage(line); } // Forward to underlying stream const writer = underlying.getWriter(); await writer.write(chunk); writer.releaseLock(); }, async close() { const writer = underlying.getWriter(); await writer.close(); writer.releaseLock(); }, async abort(reason) { logger?.warn("Tapped stream aborted", { reason }); const writer = underlying.getWriter(); await writer.abort(reason); writer.releaseLock(); }, }); } export { createTappedWritableStream }; //# sourceMappingURL=tapped-stream.js.map