Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

signals.ts

83 lines2.9 KB

1import { EXIT } from './print'
2import type { HarnessSession } from '@tanstack/ai-harness'
3​
4/** The signals that stop the CLI. */
5export type StopSignal = 'SIGINT' | 'SIGTERM'
6​
7/**
8 * Where the CLI hears SIGINT and SIGTERM. `process` is one. An
9 * `EventEmitter` is one too, so a test can emit the signals.
10 */
11export interface SignalSource {
12 on: (signal: StopSignal, handler: () => void) => unknown
13 off: (signal: StopSignal, handler: () => void) => unknown
15​
16/** The exit code of a process that a signal stopped: 128 plus its number. */
17const EXIT_CODES = { SIGINT: EXIT.cancelled, SIGTERM: 143 }
18​
19/**
20 * Listen for SIGINT and SIGTERM on `source` while a CLI mode runs.
21 *
22 * A signal first calls `cancel`, which stops the running turn and returns
23 * `true`. When `cancel` returns `false` (no turn runs, or a signal cancelled
24 * this turn already), the signal stops the mode: the listeners go, `signal`
25 * aborts, and `stopped` resolves with the exit code, 130 for SIGINT or 143
26 * for SIGTERM. A signal after that gets the default action of `process`, so
27 * a close that does not end can still be killed.
28 */
29export function stopOnSignals(source: SignalSource, cancel: () => boolean) {
30 const controller = new AbortController()
31 let resolveStopped: (code: number) => void = () => {}
32 const stopped = new Promise<number>((resolve) => {
33 resolveStopped = resolve
34 })
35 let last: StopSignal | undefined
36 const receive = (signal: StopSignal) => {
37 if (controller.signal.aborted) return
38 last = signal
39 if (cancel()) return
40 dispose()
41 controller.abort()
42 resolveStopped(EXIT_CODES[signal])
43 }
44 const onInterrupt = () => receive('SIGINT')
45 const onTerminate = () => receive('SIGTERM')
46 function dispose() {
47 source.off('SIGINT', onInterrupt)
48 source.off('SIGTERM', onTerminate)
49 }
50 source.on('SIGINT', onInterrupt)
51 source.on('SIGTERM', onTerminate)
52 return {
53 /** Aborts when a signal stops the mode. */
54 signal: controller.signal,
55 /** Resolves with the exit code when a signal stops the mode. */
56 stopped,
57 /** The exit code of the last signal. `undefined` before a signal. */
58 exitCode: () => (last === undefined ? undefined : EXIT_CODES[last]),
59 /** Handle `signal` as if `source` sent it. */
60 receive,
61 /** Stop listening. */
62 dispose,
63 }
65​
66/**
67 * The `cancel` of `stopOnSignals` for a session: cancel its running turn.
68 * Returns `false` when no turn runs, or when it cancelled this turn
69 * already, so a second signal stops a turn that does not end.
70 */
71export function cancelTurn(session: HarnessSession) {
72 let cancelled: string | undefined
73 return () => {
74 const turn = session
75 .snapshot()
76 .activeOperations.find((operation) => operation.kind === 'chat')
77 if (turn === undefined || turn.id === cancelled) return false
78 cancelled = turn.id
79 // When the cancel fails, the turn runs on, and the next signal closes.
80 void session.cancel({ operationId: turn.id }).catch(() => {})
81 return true
82 }