Skip to content

ui: move the agentic loop into @ggml-org/llama-harness, in ui/lib/harness - #55

Closed
allozaur wants to merge 6 commits into
masterfrom
allozaur/refactor/llama-harness-package
Closed

allozaur wants to merge 6 commits into
masterfrom
allozaur/refactor/llama-harness-package

Conversation

@allozaur

@allozaur allozaur commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Overview

Llama Chat's agentic loop becomes a headless package of its own, Llama Harness (@ggml-org/llama-harness), together with the plain-language phrasing of agent traces, so the loop and its traces can run without the UI and other apps can use them. Llama Chat works as before.

flowchart LR
  chat["<b>Llama Chat</b><br/>UI · storage · permission prompts"]
  other["<b>Other apps</b><br/>Node scripts · workers · editors"]
  subgraph harness["@ggml-org/llama-harness"]
    direction TB
    loop["agentic loop<br/>turns · tool calls · events"]
    trace["trace phrasing<br/>what each call did"]
  end
  server["llama-server"]

  chat --> harness
  other -.-> harness
  loop --> server
Loading

Additional information

Bottom of the harness stack on the fork (stack allozaur/llama.cpp#114), rebased onto master at 12d47ba on 24 November. It also carries two fork-only commits every layer above builds on: the dev-proxy commit and typed routes (resolve() with ROUTES as route ids). The upstream PR is the harness commits alone, on master.

The package is at ui/lib/harness, beside the library in ui/lib/ui: ui/lib holds the packages, one folder each.

Known gaps: the Docker and Nix builds were not run locally. The harness is not published to npm yet, and Llama Chat still uses its own chat service as the inference client, not the package's.

Architecture

Where things live. ui/lib holds the packages, one folder each, and ui/apps/chat compiles them from source. The harness is the only part that knows the loop; Llama Chat supplies everything around it.

flowchart LR
  subgraph lib["ui/lib (packages)"]
    direction TB
    harness["<b>@ggml-org/llama-harness</b><br/>ui/lib/harness<br/>loop · OpenAI client · trace phrasing"]
    llamaui["<b>@ggml-org/llama-ui</b><br/>ui/lib/ui<br/>components · tokens"]
  end
  subgraph chat["ui/apps/chat (Llama Chat)"]
    direction TB
    store["agenticStore<br/>(permission gate, sessions)"]
    service["ChatService<br/>(servers, settings, resume)"]
    traceui["Agent Trace components<br/>(Svelte)"]
    db[("IndexedDB")]
  end
  server["llama-server<br/>/v1/chat/completions · /tools"]

  store -- "run(messages, tools)" --> harness
  harness -- "InferenceClient.stream()" --> service
  service --> server
  store -- "events → messages" --> db
  traceui -- "createTracePhraser()" --> harness
  traceui --> llamaui
Loading

What the harness is made of. Three parts, no dependencies, no DOM. The loop only talks to an InferenceClient and to Tools; it keeps no state.

flowchart TB
  subgraph pkg["@ggml-org/llama-harness"]
    direction LR
    loop["<b>createHarness(client)</b><br/>run() → a stream of HarnessEvents"]
    client["<b>createOpenAIClient</b><br/>llama-server's chat completions,<br/>with its timings"]
    trace["<b>trace</b><br/>createTracePhraser · groupToolCallRuns<br/>parsePartialJsonArgs"]
  end
  iface(["InferenceClient<br/>stream(request)"])
  tool(["Tool<br/>definition + execute()"])
  loop --> iface
  loop --> tool
  client -. implements .-> iface
Loading

One run. The loop streams a turn, runs the tools the model called, hands back the results and goes again, until the model answers without a tool call. It waits for the consumer between events, so the app can store each message before the next step.

sequenceDiagram
  autonumber
  participant App as App (agenticStore)
  participant H as Harness
  participant C as InferenceClient
  participant T as Tool

  App->>H: run(messages, tools, signal, options)
  loop each turn
    H-->>App: turn_start
    H->>C: stream({ messages, tools })
    C-->>H: text · reasoning · tool_call deltas · timings
    H-->>App: text / reasoning / tool_call_delta / timings
    H-->>App: message (the turn's assistant message)
    alt the model called tools
      loop each tool call
        H-->>App: tool_call
        H->>T: execute(call, { signal, update })
        T-->>H: update(output so far)
        H-->>App: tool_update
        T-->>H: result
        H-->>App: tool_result
      end
      H-->>App: turn_end
    else plain answer
      H-->>App: done (complete)
    end
  end
  Note over App,H: signal → done(aborted) at once · interrupted() → done(interrupted) at the next safe point<br/>maxTurns + onTurnLimit() → ask before going on · a failure → error, run() never throws
Loading

How a run ends.

stateDiagram-v2
  [*] --> Streaming: turn_start
  Streaming --> Tools: message with tool calls
  Streaming --> Done: message, no tool calls
  Tools --> Streaming: turn_end, next turn
  Tools --> TurnLimit: maxTurns reached
  TurnLimit --> Streaming: onTurnLimit() → true
  TurnLimit --> Done: onTurnLimit() → false
  Streaming --> Done: signal / interrupted()
  Tools --> Done: signal / interrupted()
  Streaming --> Error: stream fails
  Done --> [*]: done(complete · aborted · interrupted · turn_limit)
  Error --> [*]: error
Loading

How a trace reads. The steps of a reply (text, reasoning, tool calls) go through the phraser. Each call is phrased by the first source that knows it; any step with a TraceStepType value works, so Llama Chat passes its own sections as they are.

flowchart TB
  steps["trace steps: text · reasoning · tool calls"] --> group["groupToolCallRuns: calls + reasoning → one row"]
  steps --> r1
  subgraph phr["createTracePhraser: the first source that knows the call"]
    direction TB
    r1["1. app resolvers (Llama Chat: browser tools, web search, tool_search)"]
    r2["2. llama-server tools (read_file, exec_shell_command, …)"]
    r3["3. phrasing table"]
    r4["4. any other tool: Used + its main argument"]
    r1 -- "not mine" --> r2 -- "not mine" --> r3 -- "not listed" --> r4
  end
  phr --> line["Read app.ts · Running npm test · Ran 3 commands, read 2 files"]
Loading

What Llama Chat keeps. The store wraps each enabled tool in the permission gate and turns events into the callbacks the chat store already had, so nothing above it changed.

flowchart LR
  tools["built-in · llama-server · MCP tools"] --> gate["permission gate<br/>(asks, remembers)"] --> htools["harness Tools"]
  htools --> run["harness.run()"]
  run --> cb["callbacks: onChunk · onAssistantTurnComplete ·<br/>createToolResultMessage · onFlowComplete"]
  cb --> db[("messages in IndexedDB")]
  cb --> ui["Agent Trace view"]
Loading
What changed
  • ui/lib/harness holds @ggml-org/llama-harness: plain TypeScript, no UI, no DOM, no dependencies.
    • createHarness(client).run(messages, tools, signal, options) returns the run as an async stream of events: turn_start, text, reasoning, tool_call_delta, timings, message, tool_call, tool_update, tool_result, turn_end, then done or error. It never throws, and it waits for the consumer between events, so an app can store each message before the loop goes on.
    • Messages and tools use the OpenAI shapes. A tool is its definition plus execute. The loop keeps no state and stores nothing.
    • signal stops at once, interrupted ends the run at the next safe point, and maxTurns with onTurnLimit asks before going past the limit.
    • createOpenAIClient is an inference client for llama-server's chat completions endpoint. It reads llama-server's timings and prompt_progress.
  • The package also phrases agent traces, the headless part of the Agent Trace view:
    • createTracePhraser turns tool calls into lines ("Read app.ts", "Running npm test", "Ran 3 commands, read 2 files"). It knows llama-server's built-in tools; an app adds its own through resolvers and a phrasing table, and any other tool reads as "Used " with its main argument.
    • groupToolCallRuns folds calls and the reasoning around them into one row, and the lenient parse of streamed tool arguments comes along.
    • Llama Chat keeps the Svelte components and phrases only its own tools: the browser tools, web search and tool_search. Its functions keep their names and signatures, so nothing that renders a trace changes.
  • Llama Chat's agenticStore no longer has the loop. It gives the harness ChatService as the inference client and the enabled tools, each behind the permission gate, and it turns the events into the same callbacks as before. Its public API, the gates and the chat store are unchanged.
  • Chat compiles the harness from source through a @ggml-org/llama-harness alias in vite.config.ts (a Vite alias plus a TypeScript path), so it needs no build of the package. The builds that copy or isolate ui/apps/chat bring ui/lib/harness along the same way as ui/lib/ui: CMake stages its sources next to the app, the Dockerfiles add a COPY, the Nix derivation takes ui/ as its source, and sources.cmake watches the harness sources.
  • The UI workflow has a job that type-checks, tests and packs the harness.

Three behaviours differ from before:

  • A call to a tool that was not offered to the model gets an Unknown tool error as its result. Before, a tool that exists but is disabled could still run after the permission prompt.
  • The output of a streaming tool (exec_shell_command) is coalesced: when the UI is slower than the stream, it gets the latest output, not every chunk.
  • The live "streaming tool call" state is also cleared when a turn ends with an error or is stopped.

One existing behaviour is kept as it is and looks like a bug: after Continue at the turn limit, the turn count starts again at 1, so no new assistant message is created for the next turn and it is written into the previous one.

Testing
  • ui/lib/harness: npm run check, npm test (29 tests of the loop, the client and the trace phrasing) and npm run prepack (build and publint) pass.
  • ui/apps/chat: npm run build, npm run check, npm run lint, npm run check:design, the 924 unit tests and the 347 client tests pass.
  • A unit test, agentic-flow.test.ts, runs the flow end to end with the server and the tools mocked: a tool turn, a denied permission, a failing tool, a streaming tool, an image in a tool result, a steering message, a stream error and an abort. The same test passes on the store as it was before this PR.
  • Llama Chat's existing trace phrasing tests pass unchanged on top of the harness phraser, and ui/lib/harness/tests/trace.test.ts covers the phraser on its own.
  • Before the trace phrasing moved in, the CMake asset script (scripts/ui-assets.cmake) staged the app with the harness beside it, built it with neither package installed and embedded the assets.
  • By hand, before the trace phrasing moved in, with a local llama-server: in Llama Chat, a get_datetime call shows the permission prompt, runs and the model answers with the result. From Node, the built package runs a tool call and a final answer against the same server, with no browser.

Requirements

@github-actions github-actions Bot added documentation Improvements or additions to documentation devops nix labels Nov 10, 2026
@allozaur
allozaur added this pull request to stack #53 November 10, 2026 23:09
@allozaur allozaur changed the title ui: move the agentic loop into @ggml/llama-harness, in ui/lib/harness ui: move the agentic loop into @ggml/llama-harness, in ui/harness Nov 11, 2026
@allozaur
allozaur force-pushed the allozaur/refactor/llama-harness-package branch from f34d624 to 7409202 Compare November 12, 2026 00:00
@allozaur
allozaur force-pushed the allozaur/refactor/ui-folders branch from b39a865 to 47e6a83 Compare November 12, 2026 00:09
@allozaur
allozaur force-pushed the allozaur/refactor/llama-harness-package branch from 7409202 to af2c4ab Compare November 12, 2026 00:09
@allozaur allozaur changed the title ui: move the agentic loop into @ggml/llama-harness, in ui/harness ui: move the agentic loop into @ggml/llama-harness, in ui/lib/harness Nov 12, 2026
@allozaur
allozaur force-pushed the allozaur/refactor/ui-folders branch from 47e6a83 to ff3daab Compare November 12, 2026 00:49
@allozaur
allozaur force-pushed the allozaur/refactor/llama-harness-package branch from af2c4ab to dff7cd3 Compare November 12, 2026 00:49
@allozaur
allozaur force-pushed the allozaur/refactor/ui-folders branch from ff3daab to 3226e0f Compare November 12, 2026 01:27
@allozaur
allozaur removed this pull request from stack #53 November 12, 2026 01:28
@allozaur
allozaur changed the base branch from allozaur/refactor/ui-folders to allozaur/style/glass-surfaces November 12, 2026 01:28
@allozaur
allozaur force-pushed the allozaur/style/glass-surfaces branch from 455ed4b to a50bb30 Compare November 12, 2026 01:29
@allozaur
allozaur force-pushed the allozaur/refactor/llama-harness-package branch from dff7cd3 to 7fd2f56 Compare November 12, 2026 01:29
@allozaur
allozaur added this pull request to stack #58 November 12, 2026 01:29
@allozaur
allozaur force-pushed the allozaur/refactor/llama-harness-package branch 3 times, most recently from c1882f0 to be6fe36 Compare November 12, 2026 14:29
@allozaur
allozaur removed this pull request from stack #58 November 12, 2026 20:54
@allozaur
allozaur force-pushed the allozaur/style/glass-surfaces branch from 482e1bc to bf4e58a Compare November 12, 2026 20:54
@allozaur
allozaur changed the base branch from allozaur/style/glass-surfaces to allozaur/feat/prompt-library November 12, 2026 20:54
@allozaur
allozaur force-pushed the allozaur/feat/prompt-library branch from 12fb4f6 to cb9c4bd Compare November 12, 2026 20:55
@allozaur
allozaur force-pushed the allozaur/refactor/llama-harness-package branch from be6fe36 to a48ffd4 Compare November 12, 2026 20:55
@allozaur
allozaur added this pull request to stack #59 November 12, 2026 20:55
@allozaur
allozaur force-pushed the allozaur/refactor/llama-harness-package branch from a48ffd4 to 8f887f0 Compare November 12, 2026 22:59
@allozaur
allozaur removed this pull request from stack #59 November 12, 2026 22:59
@allozaur
allozaur changed the base branch from allozaur/feat/prompt-library to allozaur/style/glass-surfaces November 12, 2026 22:59
@allozaur
allozaur added this pull request to stack #60 November 12, 2026 23:00
@allozaur
allozaur force-pushed the allozaur/refactor/llama-harness-package branch from 23f5ffa to 64799b7 Compare November 16, 2026 16:12
@allozaur
allozaur force-pushed the allozaur/docs/llama-ui-storybook branch from e53c531 to 33cdd56 Compare November 16, 2026 16:12
@allozaur
allozaur changed the base branch from allozaur/docs/llama-ui-storybook to allozaur/feat/llama-chat-npx November 16, 2026 16:13
@allozaur
allozaur added this pull request to stack #71 November 16, 2026 16:13
@allozaur
allozaur removed this pull request from stack #71 November 16, 2026 16:49
@allozaur
allozaur force-pushed the allozaur/refactor/llama-harness-package branch from 64799b7 to b285cda Compare November 16, 2026 16:50
@allozaur
allozaur force-pushed the allozaur/feat/llama-chat-npx branch from f96dd32 to 7499bd4 Compare November 16, 2026 16:50
@allozaur
allozaur added this pull request to stack #72 November 16, 2026 16:50
@allozaur
allozaur force-pushed the allozaur/refactor/llama-harness-package branch from b285cda to f98f578 Compare November 16, 2026 17:36
@allozaur
allozaur force-pushed the allozaur/feat/llama-chat-npx branch from 4484a36 to 33387cb Compare November 16, 2026 18:58
@allozaur
allozaur force-pushed the allozaur/refactor/llama-harness-package branch from f98f578 to 432435d Compare November 16, 2026 18:58
@allozaur
allozaur force-pushed the allozaur/refactor/llama-harness-package branch from 432435d to f69ce45 Compare November 17, 2026 05:21
@allozaur
allozaur force-pushed the allozaur/feat/llama-chat-npx branch from 21c2294 to a75153d Compare November 17, 2026 05:55
llama-server refuses browser requests whose Origin does not match its own
host unless --cors-origins allows it, and the Vite proxy forwarded the
page's Origin (localhost:5173, or a LAN or Tailscale address). Loading a
model, saving tools and /v1 calls failed with 403 under `vite dev`.

The proxy now calls the server with its own Host and no Origin, WebSocket
upgrades included. Sec-Fetch-Site still passes through, so requests from
other sites stay refused.

Assisted-by: Claude (Opus 5.5 Medium)
ROUTES holds the app's route ids, as page.route.id reports them, in
place of hash strings and the separate *_ROUTE_ID constants. Every link
and goto() goes through resolve() from $app/paths, which checks the
route exists and returns its URL (#/models with the hash router).
RouterService resolves the routes that take params: a chat, a project,
a project's chat or new chat, and an extension's page. Tab links,
sidebar items and extension navigation items carry the resolved URL's
type, and checks for the current page compare page.route.id instead of
the hash.

svelte/no-navigation-without-resolve is on again, with type information
for the app's modules so a URL typed as ResolvedPathname passes. Links
that leave the app say so with rel="external"; the deep-link cleanups,
which change the current page's own query, are marked as such.

Assisted-by: Claude (Opus 5.5 Medium)
…/lib/harness

Llama Harness is the agentic loop as headless TypeScript with no UI, no DOM and no dependencies. It streams a turn, runs
the tools the model called, gives it the results, and repeats until the model answers without a tool call. An app gives
it an inference client and tools, and reads the run as a stream of events: run(messages, tools, signal). It ships a
client for any OpenAI-compatible endpoint, so it also runs on its own in Node. The UI workflow type-checks, tests and
packs it.

Assisted-by: Claude (Fable 5.2)
The loop leaves the agentic store, which now gives the harness what the app owns: ChatService as its inference client,
the enabled tools behind the permission gate, and the callbacks that store each turn. Its API and the flow's callbacks
are unchanged. A unit test runs the flow end to end with the server and the tools mocked.

Chat compiles the harness from source through an alias, so the builds that copy or isolate ui/apps/chat now bring
ui/lib/harness along: CMake and Nix through LLAMA_HARNESS_DIR, the Dockerfiles with a COPY.

Assisted-by: Claude (Fable 5.2)
The layout card no longer calls the move planned, the harness is in the
pages, and they say that Llama Chat does not import @ggml-org/llama-ui yet.

Assisted-by: Claude (Opus 5.5 Medium)
The plain-language lines of an agent trace move out of Llama Chat into the
harness, as a headless trace module any app on it can use: what one tool call
did ("Read app.ts"), what it is doing now ("Running npm test", "Thinking"),
what a run of calls did ("Ran 3 commands, read 2 files"), and how calls fold
into one row with the reasoning around them.

createTracePhraser knows llama-server's built-in tools. An app adds its own
with resolvers and a phrasing table; any other tool reads as "Used <tool>"
with its main argument, and a call with a description shows the model's own
words. The lenient parse of streamed arguments and the targeted read of a
path in a whole-file argument move with it.

Llama Chat keeps its Svelte components and phrases only its own tools: the
browser tools, web search and tool_search. Its functions keep their names and
signatures, so nothing that renders a trace changes.

Assisted-by: Claude (Opus 5.5 Medium)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

devops documentation Improvements or additions to documentation nix server/ui

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant