All files / core/blong-gogo/src/agent agentSim.ts

100% Statements 92/92
100% Branches 16/16
100% Functions 5/5
100% Lines 92/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 931x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 4x 4x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x  
/**
 * `agent.sim` — the scripted kind, and the one implementation the base imports itself.
 *
 * A model kind is reached only through `loadKind` in `adapter/server/engine.ts`, on the first turn
 * an account of that kind answers; this one is small enough that the base imports it directly,
 * because every test and every deployment without a key runs it (`@@T-593`).
 */
import type {
    IAgentScriptEntry,
    IAgentSessionRequest,
    IAgentSessionResult,
    IAgentStepRequest,
    IAgentStepResult,
} from '@feasibleone/blong/types';
 
/**
 * The scripted engine kind.
 *
 * It exists so the realm's tests and a deployment without a model key still run the whole loop: an
 * account whose `kind` is `sim` replays its `script` one entry per turn, and `session` replays the
 * whole of it as one native run (`@@D-708`). Nothing here talks to a network or reads a key, which is
 * what makes the fixture the default in `integration`.
 *
 * The cursor is per account and per workspace, because one process may run two simulated sessions at
 * once and each has to start at the top of its own script.
 */
const cursors = new Map<string, number>();
 
const cursorKey = (account: string, workspacePath?: string): string =>
    `${account}|${workspacePath ?? ''}`;
 
/** How far a simulated dialogue has got, for a test that wants to assert the replay. */
export const simCursor = (account: string, workspacePath?: string): number =>
    cursors.get(cursorKey(account, workspacePath)) ?? 0;
 
/** Forget a dialogue, which is what `engine.session.close` means for this kind. */
export const simReset = (account: string, workspacePath?: string): void => {
    cursors.delete(cursorKey(account, workspacePath));
};
 
/** One turn: the next script line, as either an answer or a call to run. */
export const simStep = (
    script: IAgentScriptEntry[],
    account: string,
    _request: IAgentStepRequest,
    workspacePath?: string,
): IAgentStepResult => {
    const key = cursorKey(account, workspacePath);
    const index = cursors.get(key) ?? 0;
    const entry = script[index];
    cursors.set(key, index + 1);
    if (!entry) return {text: 'Nothing left to do.', toolCalls: [], finishReason: 'stop'};
    if ('toolCall' in entry)
        return {
            ...(entry.text !== undefined && {text: entry.text}),
            toolCalls: [
                {
                    toolName: entry.toolCall.toolName,
                    ...(entry.toolCall.args !== undefined && {args: entry.toolCall.args}),
                    ...(entry.toolCall.toolCallId !== undefined && {
                        toolCallId: entry.toolCall.toolCallId,
                    }),
                },
            ],
            finishReason: 'tool-calls',
        };
    return {text: entry.text, toolCalls: [], finishReason: entry.finishReason ?? 'stop'};
};
 
/** A native run: the whole script, reported as the events the realm records. */
export const simSession = (
    script: IAgentScriptEntry[],
    account: string,
    request: IAgentSessionRequest,
): IAgentSessionResult => {
    const events: Array<{kind: string; payload?: unknown}> = [];
    let text: string | undefined;
    for (const entry of script) {
        if ('toolCall' in entry) {
            events.push({kind: 'tool', payload: entry.toolCall});
            continue;
        }
        text = entry.text;
        events.push({kind: 'text', payload: {text: entry.text}});
    }
    return {
        text,
        sessionKey: `sim-${cursorKey(account, request.workspacePath)}`,
        finishReason: 'stop',
        events,
    };
};