threecode/actions

Tool dispatch: translate model tool_call JSON into Actions, then execute.

Two stages, intentionally separated:

Parse: dispatchGlm, dispatchGptOss, etc. accept raw args JSON and return an Action value. Each family gets its own dispatcher that accepts only the tools it was offered in prompts.nim. A name outside that set returns an akError action with a plain message back to the model - no crash, no silent reinterpretation across families.

Execute: runAction drives akBash (subprocess), akRead (file read with optional window), akWrite/akPatch (file mutations, read-cache update), akWebSearch/akWebFetch (native HTTP), and akClear (returns a sentinel that triggers a context wipe in the outer loop).

The read cache tracks the last-seen mtime and size of every read so patch can reject stale edits when the file changed since last read.

Procs

proc bannerFor(act: Action): string {....raises: [], tags: [], forbids: [].}
Returns the parameter portion of the tool banner (no icon/prefix).
proc computeDiff(before, after, label: string): string {....raises: [], tags: [],
    forbids: [].}
Native unified-diff producer. Shelling out to the external diff binary breaks on Windows (no diff on stock installs) and on systems where it isn't on PATH, so we compute the hunks in-process via LCS. Output is consumed by the green/red painter in display.nim and by tests that assert changed text appears in the result.
proc fileSig(path: string): (Time, int) {....raises: [], tags: [ReadIOEffect],
    forbids: [].}
proc newReadCache(): ReadCache {....raises: [], tags: [], forbids: [].}
proc parseActions(text: string): seq[Action] {....raises: [], tags: [], forbids: [].}
Text-mode parser. Recognises three fenced-block forms: bash fence -> akBash <path> then code fence -> akWrite <path> then SEARCH/REPLACE fence -> akPatch Multiple SEARCH/REPLACE pairs in one fenced block become one akPatch with multiple edits. Thin wrapper over parseActionsChecked that drops the issue list — used in spots where we only want the actions.
proc parseActionsChecked(text: string): tuple[actions: seq[Action],
    issues: seq[ParseIssue]] {....raises: [], tags: [], forbids: [].}
Text-mode parser with syntax-fail detection. Same recognised forms as parseActions, but additionally flags unterminated fences, orphan code-fence blocks (no bash tag and no preceding path), and malformed SEARCH/REPLACE markers inside a patch. The harness bounces issues back to the model rather than silently dropping the action.
proc previewCmd(body: string; width = 64): string {....raises: [], tags: [],
    forbids: [].}
proc runAction(act: Action; cache: ReadCache = nil): tuple[output: string,
    code: int, diff: string] {....raises: [Exception, KeyError, Exception,
                                        Exception], tags: [RootEffect,
    ReadEnvEffect, ReadIOEffect, ReadDirEffect, WriteDirEffect, WriteIOEffect,
    TimeEffect], forbids: [].}
proc runActionStreaming(act: Action; cache: ReadCache = nil;
                        onLine: proc (line: string) = nil): tuple[
    output: string, code: int, diff: string] {....raises: [Exception, KeyError,
    OSError, IOError, ResourceExhaustedError, ValueError], tags: [RootEffect,
    ReadEnvEffect, ReadIOEffect, ReadDirEffect, WriteDirEffect, WriteIOEffect,
    TimeEffect, ExecIOEffect], forbids: [].}
proc stripActions(text: string): string {....raises: [], tags: [], forbids: [].}
Mirror of parseActions: returns prose with every action block elided, collapsing the resulting blank-line runs and trimming leading/trailing blank lines. Used to suppress the fenced blocks from any post-turn reprint of the assistant message (the streamer already showed them).
proc toolCallToAction(family, name: string; args: JsonNode): Action {.
    ...raises: [IOError], tags: [RootEffect, WriteIOEffect], forbids: [].}
Routes a tool_call to the dispatcher for the active family. The family label comes from Profile.family ("glm" / "qwen" / "gpt-oss"); the case statement below mirrors setup in prompts.nim.