MCPcopy Create free account
hub / github.com/TraderAlice/OpenAlice / makeSessionIdScanner

Function makeSessionIdScanner

src/workspaces/headless-task.ts:109–134  ·  view source on GitHub ↗

* Newline-buffered scanner over a byte stream: feeds COMPLETE lines to * `extract` until it returns an id, then goes inert. A pathological "line" * exceeding the cap without a newline is discarded (the id announcement is a * small JSONL event in the first lines of every adapter's headless output)

(
  extract: (line: string) => string | null,
  onFound: (id: string) => void,
)

Source from the content-addressed store, hash-verified

107 * small JSONL event in the first lines of every adapter's headless output).
108 */
109function makeSessionIdScanner(
110 extract: (line: string) => string | null,
111 onFound: (id: string) => void,
112): { push(c: Buffer): void } {
113 let buf = '';
114 let done = false;
115 return {
116 push(c) {
117 if (done) return;
118 buf += c.toString('utf8');
119 let nl: number;
120 while ((nl = buf.indexOf('\n')) !== -1) {
121 const line = buf.slice(0, nl).trim();
122 buf = buf.slice(nl + 1);
123 if (!line) continue;
124 const id = extract(line);
125 if (id) {
126 done = true;
127 onFound(id);
128 return;
129 }
130 }
131 if (buf.length > SCAN_LINE_MAX_BYTES) buf = '';
132 },
133 };
134}
135
136/** Open a log write-stream, creating the parent dir; null (+ warn) on failure. */
137async function openLogStream(path: string, logger: Logger, name: string): Promise<WriteStream | null> {

Callers 1

runHeadlessTaskFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected