@gobing-ai/ts-ai-runner 0.5.32 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/dist/ai-runner.d.ts +12 -0
- package/dist/ai-runner.d.ts.map +1 -1
- package/dist/ai-runner.js +36 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/testing/fake-turn-agent.d.ts +2 -0
- package/dist/testing/fake-turn-agent.d.ts.map +1 -0
- package/dist/testing/fake-turn-agent.js +114 -0
- package/dist/testing/fake-turn-driver.d.ts +22 -0
- package/dist/testing/fake-turn-driver.d.ts.map +1 -0
- package/dist/testing/fake-turn-driver.js +66 -0
- package/dist/turn-session.d.ts +201 -0
- package/dist/turn-session.d.ts.map +1 -0
- package/dist/turn-session.js +452 -0
- package/package.json +8 -4
- package/src/ai-runner.ts +55 -0
- package/src/index.ts +1 -0
- package/src/testing/fake-turn-agent.ts +123 -0
- package/src/testing/fake-turn-driver.ts +99 -0
- package/src/turn-session.ts +627 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fake turn-session driver for tests (task 1284). Spawns a REAL child process
|
|
3
|
+
* (`fake-turn-agent.ts`) that replays a scripted JSONL protocol across the process boundary,
|
|
4
|
+
* so sessions are exercised end-to-end — never against an in-process stub of the transport.
|
|
5
|
+
* Exported from the `./testing` subpath only; never imported by library code.
|
|
6
|
+
*/
|
|
7
|
+
import type { TurnBinary, TurnHandshake, TurnSessionDriver, TurnSessionPolicy, TurnWireEvent } from '../turn-session';
|
|
8
|
+
import { TurnProtocolError } from '../turn-session';
|
|
9
|
+
|
|
10
|
+
/** One scripted child action (see `fake-turn-agent.ts` for the wire semantics). */
|
|
11
|
+
export interface FakeTurnAction {
|
|
12
|
+
op: 'ready' | 'accept' | 'output' | 'terminal' | 'stray' | 'sleep' | 'ignore-cancel' | 'flood' | 'exit';
|
|
13
|
+
nativeSessionId?: string;
|
|
14
|
+
data?: string;
|
|
15
|
+
kind?: 'success' | 'failure';
|
|
16
|
+
nativeTurnId?: string;
|
|
17
|
+
finalText?: string;
|
|
18
|
+
line?: Record<string, unknown>;
|
|
19
|
+
ms?: number;
|
|
20
|
+
code?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Path of the child script: source tree in dev/checkouts, compiled sibling in built packages. */
|
|
24
|
+
function fakeTurnAgentPath(): string {
|
|
25
|
+
// No node:path/node:fs (runtime-boundaries rule): import.meta.dir is already absolute, so a
|
|
26
|
+
// separator-safe marker check picks the compiled sibling in built packages.
|
|
27
|
+
const isBuilt = import.meta.dir.replace(/\\/g, '/').includes('/dist/');
|
|
28
|
+
return `${import.meta.dir}/fake-turn-agent.${isBuilt ? 'js' : 'ts'}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface FakeFrame {
|
|
32
|
+
ctl?: string;
|
|
33
|
+
turn?: string | null;
|
|
34
|
+
type?: string;
|
|
35
|
+
data?: unknown;
|
|
36
|
+
kind?: string;
|
|
37
|
+
nativeTurnId?: string;
|
|
38
|
+
nativeSessionId?: string;
|
|
39
|
+
finalText?: string;
|
|
40
|
+
reason?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Build a turn-session driver over the scripted child. `overrides` tailors qualify/agent for refusal tests. */
|
|
44
|
+
export function createFakeTurnSessionDriver(
|
|
45
|
+
actions: FakeTurnAction[],
|
|
46
|
+
overrides: Partial<Pick<TurnSessionDriver, 'qualify' | 'agent'>> = {},
|
|
47
|
+
): TurnSessionDriver {
|
|
48
|
+
const scriptJson = JSON.stringify(actions);
|
|
49
|
+
const driver: TurnSessionDriver = {
|
|
50
|
+
agent: overrides.agent ?? 'pi',
|
|
51
|
+
qualify: overrides.qualify ?? (() => ({ qualified: true })),
|
|
52
|
+
spawnArgv: (_binary: TurnBinary, _policy: TurnSessionPolicy) => ({
|
|
53
|
+
command: process.execPath,
|
|
54
|
+
args: [fakeTurnAgentPath(), scriptJson],
|
|
55
|
+
}),
|
|
56
|
+
handshake: async (pull: (timeoutMs: number) => Promise<string | null>): Promise<TurnHandshake> => {
|
|
57
|
+
const raw = await pull(2_000);
|
|
58
|
+
if (raw === null) return null; // EOF or timeout before ready
|
|
59
|
+
const frame = JSON.parse(raw) as FakeFrame;
|
|
60
|
+
if (frame.ctl !== 'ready') return null;
|
|
61
|
+
return frame.nativeSessionId !== undefined ? { nativeSessionId: frame.nativeSessionId } : null;
|
|
62
|
+
},
|
|
63
|
+
encodeTurn: (input) => `${JSON.stringify({ type: 'turn', ...input })}\n`,
|
|
64
|
+
decode: (line: string): TurnWireEvent[] => {
|
|
65
|
+
let frame: FakeFrame & { ctl?: string };
|
|
66
|
+
try {
|
|
67
|
+
frame = JSON.parse(line) as FakeFrame & { ctl?: string };
|
|
68
|
+
} catch {
|
|
69
|
+
throw new TurnProtocolError(`malformed frame: ${line.slice(0, 80)}`);
|
|
70
|
+
}
|
|
71
|
+
if (frame.ctl !== undefined) return []; // control frames (ready) are handshake-only
|
|
72
|
+
if (frame.type !== 'accepted' && frame.type !== 'output' && frame.type !== 'terminal') {
|
|
73
|
+
throw new TurnProtocolError(`unknown frame type: ${String(frame.type)}`);
|
|
74
|
+
}
|
|
75
|
+
return [
|
|
76
|
+
{
|
|
77
|
+
turnId: frame.turn ?? null,
|
|
78
|
+
type: frame.type,
|
|
79
|
+
...(frame.data !== undefined ? { data: frame.data } : {}),
|
|
80
|
+
...(frame.type === 'terminal'
|
|
81
|
+
? {
|
|
82
|
+
terminal: {
|
|
83
|
+
kind: frame.kind === 'failure' ? 'failure' : 'success',
|
|
84
|
+
...(frame.nativeTurnId !== undefined ? { nativeTurnId: frame.nativeTurnId } : {}),
|
|
85
|
+
...(frame.nativeSessionId !== undefined
|
|
86
|
+
? { nativeSessionId: frame.nativeSessionId }
|
|
87
|
+
: {}),
|
|
88
|
+
...(frame.finalText !== undefined ? { finalText: frame.finalText } : {}),
|
|
89
|
+
...(frame.reason !== undefined ? { reason: frame.reason } : {}),
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
: {}),
|
|
93
|
+
},
|
|
94
|
+
];
|
|
95
|
+
},
|
|
96
|
+
encodeCancel: (reason: string) => `${JSON.stringify({ type: 'cancel', reason })}\n`,
|
|
97
|
+
};
|
|
98
|
+
return driver;
|
|
99
|
+
}
|