MCPcopy
hub / github.com/afar1/fieldtheory-cli / invokeEngine

Function invokeEngine

src/engine.ts:359–415  ·  view source on GitHub ↗
(engine: ResolvedEngine, prompt: string, opts: InvokeOptions = {})

Source from the content-addressed store, hash-verified

357 * EOF immediately so the child proceeds with just the prompt arg.
358 */
359export function invokeEngine(engine: ResolvedEngine, prompt: string, opts: InvokeOptions = {}): string {
360 const { bin, args } = engine.config;
361 const timeout = opts.timeout ?? DEFAULT_TIMEOUT;
362 const maxBuffer = opts.maxBuffer ?? DEFAULT_MAXBUF;
363
364 const result = spawnSync(bin, args(prompt, engine), {
365 input: '', // EOF on stdin — do not inherit parent stdin
366 timeout,
367 maxBuffer,
368 encoding: 'buffer',
369 });
370
371 const stderrBuf = result.stderr ?? Buffer.alloc(0);
372 const stderr = redactSecrets(tailString(stderrBuf, STDERR_TAIL_BYTES));
373
374 if (result.error) {
375 const anyErr = result.error as NodeJS.ErrnoException & { code?: string };
376 if (anyErr.code === 'ETIMEDOUT') {
377 throw new EngineInvocationError({
378 engine: engine.name, bin, stderr,
379 killed: true, code: null, signal: 'SIGTERM', reason: 'timeout',
380 message: buildMessage(engine.name, 'timeout', stderr, null, 'SIGTERM', timeout),
381 });
382 }
383 if (anyErr.code === 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER') {
384 throw new EngineInvocationError({
385 engine: engine.name, bin, stderr,
386 killed: true, code: null, signal: null, reason: 'maxbuffer',
387 message: buildMessage(engine.name, 'maxbuffer', stderr, null, null, timeout),
388 });
389 }
390 throw new EngineInvocationError({
391 engine: engine.name, bin,
392 stderr: '', killed: false, code: null, signal: null, reason: 'spawn',
393 message: buildMessage(engine.name, 'spawn', anyErr.message ?? '', null, null, timeout),
394 });
395 }
396
397 if (result.signal === 'SIGTERM' && (result.status === null || result.status === 143)) {
398 // spawnSync sets .signal='SIGTERM' when the timeout kills the child.
399 throw new EngineInvocationError({
400 engine: engine.name, bin, stderr,
401 killed: true, code: result.status, signal: result.signal, reason: 'timeout',
402 message: buildMessage(engine.name, 'timeout', stderr, result.status, result.signal, timeout),
403 });
404 }
405
406 if (result.status !== 0) {
407 throw new EngineInvocationError({
408 engine: engine.name, bin, stderr,
409 killed: false, code: result.status, signal: result.signal, reason: 'exit',
410 message: buildMessage(engine.name, 'exit', stderr, result.status, result.signal, timeout),
411 });
412 }
413
414 return (result.stdout ?? Buffer.alloc(0)).toString('utf-8').trim();
415}
416

Callers 3

classifyWithLlmFunction · 0.85
classifyDomainsWithLlmFunction · 0.85
engine.test.tsFile · 0.85

Calls 3

redactSecretsFunction · 0.85
tailStringFunction · 0.85
buildMessageFunction · 0.85

Tested by

no test coverage detected