{"version":3,"file":"pi-text-caller.d.ts","sourceRoot":"","sources":["../src/pi-text-caller.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,UAAU;IAC1B,IAAI,CAAC,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAChE;AAyBD,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,UAAU,CAe3E","sourcesContent":["import { spawn } from \"node:child_process\";\n\nexport interface TextCaller {\n\tcall(systemPrompt: string, userPrompt: string): Promise<string>;\n}\n\nfunction spawnOnce(piBin: string, model: string, system: string, user: string): Promise<string> {\n\treturn new Promise((resolve, reject) => {\n\t\tconst child = spawn(\n\t\t\tpiBin,\n\t\t\t[\"--print\", \"--no-session\", \"--model\", model, `Instructions:\\n${system}\\n\\nTask:\\n${user}`],\n\t\t\t{ stdio: [\"ignore\", \"pipe\", \"pipe\"] },\n\t\t);\n\t\tlet stdout = \"\";\n\t\tlet stderr = \"\";\n\t\tchild.stdout.on(\"data\", (chunk: Buffer) => (stdout += chunk.toString(\"utf8\")));\n\t\tchild.stderr.on(\"data\", (chunk: Buffer) => (stderr += chunk.toString(\"utf8\")));\n\t\tchild.on(\"error\", (error) => reject(new Error(`pi failed to start: ${error.message}`)));\n\t\tchild.on(\"exit\", (code) => {\n\t\t\tconst text = stdout.trim();\n\t\t\tif (text) {\n\t\t\t\tresolve(text);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\treject(new Error(`pi exited with code ${code} and no output: ${stderr.trim().slice(-300)}`));\n\t\t});\n\t});\n}\n\nexport function createPiTextCaller(piBin: string, model: string): TextCaller {\n\treturn {\n\t\tasync call(system, user) {\n\t\t\ttry {\n\t\t\t\treturn await spawnOnce(piBin, model, system, user);\n\t\t\t} catch (first) {\n\t\t\t\t// Transient empty responses happen; retry once before failing.\n\t\t\t\ttry {\n\t\t\t\t\treturn await spawnOnce(piBin, model, system, user);\n\t\t\t\t} catch {\n\t\t\t\t\tthrow first;\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t};\n}\n"]}