async function SubProcess() {
console.log("Starting subprocess...");
const EXE: string = "./katago/katago.exe";
const MODEL: string = "./katago/model.bin.gz";
const CONFIG: string = "./katago/config.cfg";
const command = new Deno.Command(EXE, {
args: ["analysis", "-model", MODEL, "-config", CONFIG],
stdin: "piped",
stdout: "piped",
stderr: "piped",
});
const process = command.spawn();
console.log("Subprocess spawned.");
return process;
}
async function sendQuery(process: Deno.ChildProcess, query: Query) {
console.log(`Sending query with id: ${query.id}`);
const writer = process.stdin.getWriter();
const data = new TextEncoder().encode(JSON.stringify(query) + "\n");
await writer.write(data);
writer.releaseLock();
console.log(`Query ${query.id} sent.`);
}
async function readOutput(process: Deno.ChildProcess) {
console.log("Reading output from subprocess...");
const decoder = new TextDecoder();
const reader = process.stdout.getReader();
let output = "";
try {
while (true) {
const { value, done } = await reader.read();
if (done) {
break;
}
output += decoder.decode(value, { stream: true });
// Assuming that each response from Katago ends with a newline
if (output.includes("\n")) {
break;
}
}
} catch (error) {
console.error("Error reading subprocess output: ", error);
} finally {
reader.releaseLock();
}
const AIData: AIObject = JSON.parse(output) as AIObject
return AIData;
}