diff --git a/.devops/cann.Dockerfile b/.devops/cann.Dockerfile index 791b583ca6a5..83beced0add1 100644 --- a/.devops/cann.Dockerfile +++ b/.devops/cann.Dockerfile @@ -25,10 +25,11 @@ COPY ui/apps/chat/package.json ui/apps/chat/package-lock.json ./ RUN npm ci COPY ui/apps/chat/ ./ -# the app compiles Llama UI and Llama Voice from their sources next to it +# the app compiles Llama UI, Llama Voice and the harness from their sources next to it COPY ui/lib/ui/package.json /app/ui/lib/ui/package.json COPY ui/lib/ui/src/lib/ /app/ui/lib/ui/src/lib/ COPY ui/lib/voice/src/ /app/ui/lib/voice/src/ +COPY ui/lib/harness/src/ /app/ui/lib/harness/src/ RUN LLAMA_BUILD_NUMBER="$APP_VERSION" npm run build FROM ${CANN_BASE_IMAGE} AS build diff --git a/.devops/cpu.Dockerfile b/.devops/cpu.Dockerfile index f63e005ec930..fb3b5e9d9744 100644 --- a/.devops/cpu.Dockerfile +++ b/.devops/cpu.Dockerfile @@ -15,10 +15,11 @@ COPY ui/apps/chat/package.json ui/apps/chat/package-lock.json ./ RUN npm ci COPY ui/apps/chat/ ./ -# the app compiles Llama UI and Llama Voice from their sources next to it +# the app compiles Llama UI, Llama Voice and the harness from their sources next to it COPY ui/lib/ui/package.json /app/ui/lib/ui/package.json COPY ui/lib/ui/src/lib/ /app/ui/lib/ui/src/lib/ COPY ui/lib/voice/src/ /app/ui/lib/voice/src/ +COPY ui/lib/harness/src/ /app/ui/lib/harness/src/ RUN LLAMA_BUILD_NUMBER="$APP_VERSION" npm run build FROM docker.io/ubuntu:$UBUNTU_VERSION AS build diff --git a/.devops/cuda.Dockerfile b/.devops/cuda.Dockerfile index cf1715d43a4c..058212e2c2a8 100644 --- a/.devops/cuda.Dockerfile +++ b/.devops/cuda.Dockerfile @@ -23,10 +23,11 @@ COPY ui/apps/chat/package.json ui/apps/chat/package-lock.json ./ RUN npm ci COPY ui/apps/chat/ ./ -# the app compiles Llama UI and Llama Voice from their sources next to it +# the app compiles Llama UI, Llama Voice and the harness from their sources next to it COPY ui/lib/ui/package.json /app/ui/lib/ui/package.json COPY ui/lib/ui/src/lib/ /app/ui/lib/ui/src/lib/ COPY ui/lib/voice/src/ /app/ui/lib/voice/src/ +COPY ui/lib/harness/src/ /app/ui/lib/harness/src/ RUN LLAMA_BUILD_NUMBER="$APP_VERSION" npm run build FROM ${BASE_CUDA_DEV_CONTAINER} AS build diff --git a/.devops/intel.Dockerfile b/.devops/intel.Dockerfile index 0783ef738c9f..5a4ddabe5796 100644 --- a/.devops/intel.Dockerfile +++ b/.devops/intel.Dockerfile @@ -17,10 +17,11 @@ COPY ui/apps/chat/package.json ui/apps/chat/package-lock.json ./ RUN npm ci COPY ui/apps/chat/ ./ -# the app compiles Llama UI and Llama Voice from their sources next to it +# the app compiles Llama UI, Llama Voice and the harness from their sources next to it COPY ui/lib/ui/package.json /app/ui/lib/ui/package.json COPY ui/lib/ui/src/lib/ /app/ui/lib/ui/src/lib/ COPY ui/lib/voice/src/ /app/ui/lib/voice/src/ +COPY ui/lib/harness/src/ /app/ui/lib/harness/src/ RUN LLAMA_BUILD_NUMBER="$APP_VERSION" npm run build FROM docker.io/intel/deep-learning-essentials:$ONEAPI_VERSION AS build diff --git a/.devops/musa.Dockerfile b/.devops/musa.Dockerfile index 3ae62a676e4b..66568885590b 100644 --- a/.devops/musa.Dockerfile +++ b/.devops/musa.Dockerfile @@ -21,10 +21,11 @@ COPY ui/apps/chat/package.json ui/apps/chat/package-lock.json ./ RUN npm ci COPY ui/apps/chat/ ./ -# the app compiles Llama UI and Llama Voice from their sources next to it +# the app compiles Llama UI, Llama Voice and the harness from their sources next to it COPY ui/lib/ui/package.json /app/ui/lib/ui/package.json COPY ui/lib/ui/src/lib/ /app/ui/lib/ui/src/lib/ COPY ui/lib/voice/src/ /app/ui/lib/voice/src/ +COPY ui/lib/harness/src/ /app/ui/lib/harness/src/ RUN LLAMA_BUILD_NUMBER="$APP_VERSION" npm run build FROM ${BASE_MUSA_DEV_CONTAINER} AS build diff --git a/.devops/nix/package.nix b/.devops/nix/package.nix index 4ac844ff959d..887270fb23bd 100644 --- a/.devops/nix/package.nix +++ b/.devops/nix/package.nix @@ -182,7 +182,7 @@ effectiveStdenv.mkDerivation (finalAttrs: { webui = stdenvNoCC.mkDerivation { pname = "webui"; version = llamaVersion; - # the app compiles Llama UI from its source in ../../lib/ui, so the source is ui/ as a whole + # the app compiles Llama UI and the harness from their sources in ../../lib, so the source is ui/ as a whole src = lib.cleanSource ../../ui; sourceRoot = "source/apps/chat"; diff --git a/.devops/openvino.Dockerfile b/.devops/openvino.Dockerfile index fb8cb4c61f66..1509796dd225 100644 --- a/.devops/openvino.Dockerfile +++ b/.devops/openvino.Dockerfile @@ -34,10 +34,11 @@ COPY ui/apps/chat/package.json ui/apps/chat/package-lock.json ./ RUN npm ci COPY ui/apps/chat/ ./ -# the app compiles Llama UI and Llama Voice from their sources next to it +# the app compiles Llama UI, Llama Voice and the harness from their sources next to it COPY ui/lib/ui/package.json /app/ui/lib/ui/package.json COPY ui/lib/ui/src/lib/ /app/ui/lib/ui/src/lib/ COPY ui/lib/voice/src/ /app/ui/lib/voice/src/ +COPY ui/lib/harness/src/ /app/ui/lib/harness/src/ RUN LLAMA_BUILD_NUMBER="$APP_VERSION" npm run build ## Build Image diff --git a/.devops/rocm.Dockerfile b/.devops/rocm.Dockerfile index ba9e85a0ed19..b7e5938d935a 100644 --- a/.devops/rocm.Dockerfile +++ b/.devops/rocm.Dockerfile @@ -23,10 +23,11 @@ COPY ui/apps/chat/package.json ui/apps/chat/package-lock.json ./ RUN npm ci COPY ui/apps/chat/ ./ -# the app compiles Llama UI and Llama Voice from their sources next to it +# the app compiles Llama UI, Llama Voice and the harness from their sources next to it COPY ui/lib/ui/package.json /app/ui/lib/ui/package.json COPY ui/lib/ui/src/lib/ /app/ui/lib/ui/src/lib/ COPY ui/lib/voice/src/ /app/ui/lib/voice/src/ +COPY ui/lib/harness/src/ /app/ui/lib/harness/src/ RUN LLAMA_BUILD_NUMBER="$APP_VERSION" npm run build ### Build image diff --git a/.devops/vulkan.Dockerfile b/.devops/vulkan.Dockerfile index 023847628366..4e97422d6d07 100644 --- a/.devops/vulkan.Dockerfile +++ b/.devops/vulkan.Dockerfile @@ -15,10 +15,11 @@ COPY ui/apps/chat/package.json ui/apps/chat/package-lock.json ./ RUN npm ci COPY ui/apps/chat/ ./ -# the app compiles Llama UI and Llama Voice from their sources next to it +# the app compiles Llama UI, Llama Voice and the harness from their sources next to it COPY ui/lib/ui/package.json /app/ui/lib/ui/package.json COPY ui/lib/ui/src/lib/ /app/ui/lib/ui/src/lib/ COPY ui/lib/voice/src/ /app/ui/lib/voice/src/ +COPY ui/lib/harness/src/ /app/ui/lib/harness/src/ RUN LLAMA_BUILD_NUMBER="$APP_VERSION" npm run build FROM docker.io/ubuntu:$UBUNTU_VERSION AS build diff --git a/.devops/zendnn.Dockerfile b/.devops/zendnn.Dockerfile index e49df81ca45f..da2fea2dfc58 100644 --- a/.devops/zendnn.Dockerfile +++ b/.devops/zendnn.Dockerfile @@ -15,10 +15,11 @@ COPY ui/apps/chat/package.json ui/apps/chat/package-lock.json ./ RUN npm ci COPY ui/apps/chat/ ./ -# the app compiles Llama UI and Llama Voice from their sources next to it +# the app compiles Llama UI, Llama Voice and the harness from their sources next to it COPY ui/lib/ui/package.json /app/ui/lib/ui/package.json COPY ui/lib/ui/src/lib/ /app/ui/lib/ui/src/lib/ COPY ui/lib/voice/src/ /app/ui/lib/voice/src/ +COPY ui/lib/harness/src/ /app/ui/lib/harness/src/ RUN LLAMA_BUILD_NUMBER="$APP_VERSION" npm run build FROM docker.io/ubuntu:$UBUNTU_VERSION AS build diff --git a/.github/workflows/ui-self-hosted.yml b/.github/workflows/ui-self-hosted.yml index 4967de07ca21..f254f3b0c7d2 100644 --- a/.github/workflows/ui-self-hosted.yml +++ b/.github/workflows/ui-self-hosted.yml @@ -20,6 +20,7 @@ on: 'ui/apps/chat/**.*', 'ui/lib/ui/**.*', 'ui/lib/voice/**.*', + 'ui/lib/harness/**.*', 'tools/server/tests/**.*' ] pull_request: @@ -30,6 +31,7 @@ on: 'ui/apps/chat/**.*', 'ui/lib/ui/**.*', 'ui/lib/voice/**.*', + 'ui/lib/harness/**.*', 'tools/server/tests/**.*' ] diff --git a/.github/workflows/ui.yml b/.github/workflows/ui.yml index 65e5c36ffdbf..bba032416bfb 100644 --- a/.github/workflows/ui.yml +++ b/.github/workflows/ui.yml @@ -181,6 +181,37 @@ jobs: run: npm run prepack working-directory: ui/lib/voice + harness-checks: + name: Harness checks + runs-on: ubuntu-24.04 + steps: + - name: Checkout code + uses: actions/checkout@v6 + with: + ref: ${{ github.event.inputs.sha || github.event.pull_request.head.sha || github.sha || github.head_ref || github.ref_name }} + + - name: Setup Node.js + uses: actions/setup-node@v6 + with: + node-version: "24" + package-manager-cache: false + + - name: Install dependencies + run: npm ci + working-directory: ui/lib/harness + + - name: Run type checking + run: npm run check + working-directory: ui/lib/harness + + - name: Run tests + run: npm test + working-directory: ui/lib/harness + + - name: Build the package + run: npm run prepack + working-directory: ui/lib/harness + e2e-tests: name: E2E Tests needs: ui-build diff --git a/scripts/ui-assets.cmake b/scripts/ui-assets.cmake index a3b4f16d7b68..8079c1c09651 100644 --- a/scripts/ui-assets.cmake +++ b/scripts/ui-assets.cmake @@ -21,11 +21,13 @@ set(DIST_DIR "${UI_BINARY_DIR}/dist") set(SRC_DIST_DIR "${UI_SOURCE_DIR}/dist") # The stage keeps the layout of ui/: the app in apps/chat, with the sources of # Llama UI that it compiles next to it in lib/ui (see ui/apps/chat/scripts/llama-ui.js), -# and those of Llama Voice in lib/voice (see ui/apps/chat/vite.config.ts). +# and those of Llama Voice and the harness in lib/voice and lib/harness (see +# ui/apps/chat/vite.config.ts). set(STAGE_DIR "${UI_BINARY_DIR}/ui-src") set(WORK_DIR "${STAGE_DIR}/apps/chat") set(UI_LIB_DIR "${UI_SOURCE_DIR}/../../lib/ui") set(UI_VOICE_DIR "${UI_SOURCE_DIR}/../../lib/voice") +set(HARNESS_DIR "${UI_SOURCE_DIR}/../../lib/harness") set(STAMP_FILE "${UI_BINARY_DIR}/.ui-stamp") set(EMBED_STAMP "${UI_BINARY_DIR}/.ui-embed.sha256") set(UI_CPP "${UI_BINARY_DIR}/ui.cpp") @@ -325,7 +327,7 @@ function(npm_build_should_skip out_var) endfunction() function(stage_sources) - # everything but the app goes: the library, restaged below, and what a stage + # everything but the app goes: the packages, restaged below, and what a stage # from before this layout left at the top, its node_modules included if(EXISTS "${STAGE_DIR}") file(GLOB staged RELATIVE "${STAGE_DIR}" "${STAGE_DIR}/*") @@ -349,7 +351,8 @@ function(stage_sources) PATTERN "node_modules" EXCLUDE ) - # what the app reads of the library: its sources and the dependency list + # what the app reads of the packages: Llama UI's sources and dependency list, + # and the harness's sources file(COPY "${UI_LIB_DIR}/package.json" DESTINATION "${STAGE_DIR}/lib/ui" NO_SOURCE_PERMISSIONS @@ -364,6 +367,10 @@ function(stage_sources) DESTINATION "${STAGE_DIR}/lib/voice" NO_SOURCE_PERMISSIONS ) + file(COPY "${HARNESS_DIR}/src" + DESTINATION "${STAGE_DIR}/lib/harness" + NO_SOURCE_PERMISSIONS + ) endfunction() function(npm_build out_var) diff --git a/ui/README.md b/ui/README.md index 0f210a40c80c..3d6521b58ac4 100644 --- a/ui/README.md +++ b/ui/README.md @@ -5,10 +5,11 @@ The UI of llama.cpp: the packages in `lib` and the apps built on them. ```text ui/ ├── lib/ -│ ├── ui/ @ggml-org/llama-ui: design tokens, fonts, surfaces and components -│ └── voice/ @ggml-org/llama-voice: microphone capture, transcription and dictation +│ ├── ui/ @ggml-org/llama-ui: design tokens, fonts, surfaces and components +│ ├── voice/ @ggml-org/llama-voice: microphone capture, transcription and dictation +│ └── harness/ @ggml-org/llama-harness: the agentic loop, headless └── apps/ - └── chat/ Llama Chat, the chat app that llama-server serves + └── chat/ Llama Chat, the chat app that llama-server serves ``` - [`lib/ui`](lib/ui/README.md) draws the parts and handles interaction, with no business logic. It is built with @@ -17,12 +18,18 @@ ui/ - [`lib/voice`](lib/voice/README.md) records the microphone and turns speech into text through llama-server, with no UI: Llama UI draws the voice parts. It is plain TypeScript built with `tsc`, plus a Svelte entry (`@ggml-org/llama-voice/svelte`). Apps in this repo compile its sources too. It is not published to npm yet. -- [`apps/chat`](apps/chat/README.md) talks to llama-server, stores conversations and runs the agentic loop. CMake - builds it into llama-server (see `scripts/ui-assets.cmake`), and it is published to npm as `@ggml-org/llama-chat` - (with `llama-chat` as its short name), so `npx llama-chat` runs it against any llama-server. +- [`lib/harness`](lib/harness/README.md) is the agentic loop: plain TypeScript with no UI and no dependencies. An app + gives it an inference client and tools, and reads its events. +- [`apps/chat`](apps/chat/README.md) talks to llama-server, stores conversations and runs the agentic loop through the + harness. CMake builds it into llama-server (see `scripts/ui-assets.cmake`), and it is published to npm as + `@ggml-org/llama-chat` (with `llama-chat` as its short name), so `npx llama-chat` runs it against any llama-server. Each folder is its own npm package with its own `package-lock.json`: run `npm ci` in the one you work on. Building, -checking and testing `apps/chat` needs no install in `lib/ui` or `lib/voice`. +checking and testing `apps/chat` needs no install in `lib/ui`, `lib/voice` or `lib/harness`. + +The split between library and app is not finished. Llama Chat takes its tokens, fonts, surfaces and base components +from `lib/ui`, but still has its own versions of what the library holds above those (ActionIcon, SearchInput, Logo, +SyntaxHighlightedCode and the Model UI), and `DESIGN.md`, `BRAND.md` and the full Storybook still sit beside the app. ## Releases diff --git a/ui/apps/chat/AGENTS.md b/ui/apps/chat/AGENTS.md index 32e6a009a2c9..0b4884de3167 100644 --- a/ui/apps/chat/AGENTS.md +++ b/ui/apps/chat/AGENTS.md @@ -1,6 +1,6 @@ # Instructions for ui/apps/chat -These add to the repository's [AGENTS.md](../../../AGENTS.md) for work in `ui/apps/chat`: **Llama Chat**, the chat client that llama-server bundles. **Llama UI**, the library, is `ui/lib/ui` (`@ggml-org/llama-ui`): Chat imports its tokens, fonts and base components from there (`@ggml-org/llama-ui/ui/`) and keeps no copy of them. `DESIGN.md` and the Storybook live here; the brand book, `BRAND.md`, and the brand and communications pages are in [ggml-org/llama-brand](https://github.com/ggml-org/llama-brand), with their own Storybook. +These add to the repository's [AGENTS.md](../../../AGENTS.md) for work in `ui/apps/chat`: **Llama Chat**, the chat client that llama-server bundles. **Llama UI**, the library, is `ui/lib/ui` (`@ggml-org/llama-ui`): Chat imports its tokens, fonts and base components from there (`@ggml-org/llama-ui/ui/`) and keeps no copy of them. `DESIGN.md` and the Storybook live here; the brand book, `BRAND.md`, and the brand and communications pages are in [ggml-org/llama-brand](https://github.com/ggml-org/llama-brand), with their own Storybook. The agentic loop is `ui/lib/harness` (`@ggml-org/llama-harness`). - **Design:** [DESIGN.md](DESIGN.md) holds the tokens, roles and rules, written for agents. Follow it for anything visual. Its token sections (`colors`, `typography`, `rounded`) are generated from the CSS with `npm run design:tokens`, and CI checks they are current; the rest is written by hand, and when it and the CSS disagree, the CSS wins and DESIGN.md is updated. - **Stories:** a story lives with the code it shows. Llama UI's (its foundations, base components, app components and Model UI) are in `ui/lib/ui/tests/stories`, titled `Llama UI/...`, and run as the library's tests; this app's Storybook shows them too. Llama Chat's are in `tests/stories/`, one folder per `src/lib/components/app/` (`chat/` has one per Chat folder), titled `Llama Chat//`. An accessibility story sits next to its component's, as `.a11y.stories.svelte`, titled `...//Accessibility`. The story helpers both Storybooks use (the Editorial the prose pages are set in, `StateGallery`) live with the library's stories and are imported here as `$llama-ui-stories/...`; reuse them rather than copying them here. diff --git a/ui/apps/chat/README.md b/ui/apps/chat/README.md index abee5d43bb0d..891fe6b2a422 100644 --- a/ui/apps/chat/README.md +++ b/ui/apps/chat/README.md @@ -84,16 +84,17 @@ Llama Chat is the first app on Llama UI, and the reference for the line between The app imports the library as `@ggml-org/llama-ui/ui/` and compiles its sources in its own build, with no install of the library (see `scripts/vite-plugin-llama-ui.ts`). Data flows one way, through these layers: -| Layer | Lives in | What it holds | -| -------------- | ------------------------- | --------------------------------------------------------------------------------------------------- | -| Routes | `src/routes` | `/`, `/chat/[id]`, `/models`, `/mcp-servers`, `/settings`, `/search` | -| App components | `src/lib/components/app` | Chat, navigation, models, MCP, settings and dialogs, grouped by area | -| UI components | `ui/lib/ui` | shadcn-svelte on bits-ui, from Llama UI | -| Voice | `ui/lib/voice` | Microphone capture, transcription and dictation, from Llama Voice (`@ggml-org/llama-voice`) | -| Hooks | `src/lib/hooks` | View logic between components and stores: scroll, drag and drop, pickers, keyboard | -| Stores | `src/lib/stores` | Application state as Svelte 5 runes: chat, conversations, the agentic loop, tools, models, settings | -| Services | `src/lib/services` | Stateless I/O: the llama-server API, IndexedDB, MCP, the Hugging Face Hub, the tool sandbox | -| Storage + API | the browser, llama-server | IndexedDB and localStorage; llama-server's `/v1`, `/props`, `/models`, `/tools`, `/slots` | +| Layer | Lives in | What it holds | +| -------------- | ------------------------- | ---------------------------------------------------------------------------------------------------- | +| Routes | `src/routes` | `/`, `/chat/[id]`, `/models`, `/mcp-servers`, `/settings`, `/search` | +| App components | `src/lib/components/app` | Chat, navigation, models, MCP, settings and dialogs, grouped by area | +| UI components | `ui/lib/ui` | shadcn-svelte on bits-ui, from Llama UI | +| Voice | `ui/lib/voice` | Microphone capture, transcription and dictation, from Llama Voice (`@ggml-org/llama-voice`) | +| Hooks | `src/lib/hooks` | View logic between components and stores: scroll, drag and drop, pickers, keyboard | +| Stores | `src/lib/stores` | Application state as Svelte 5 runes: chat, conversations, the agentic loop, tools, models, settings | +| Agentic loop | `ui/lib/harness` | Llama Harness: streams a turn, runs the tools the model called, repeats until it answers without one | +| Services | `src/lib/services` | Stateless I/O: the llama-server API, IndexedDB, MCP, the Hugging Face Hub, the tool sandbox | +| Storage + API | the browser, llama-server | IndexedDB and localStorage; llama-server's `/v1`, `/props`, `/models`, `/tools`, `/slots` | ```mermaid flowchart LR diff --git a/ui/apps/chat/eslint.config.js b/ui/apps/chat/eslint.config.js index 330551d9d8d1..edbc71c487a3 100644 --- a/ui/apps/chat/eslint.config.js +++ b/ui/apps/chat/eslint.config.js @@ -266,7 +266,6 @@ export default ts.config( 'simple-import-sort/imports': ['error', { groups: [['.*']] }], 'svelte/no-at-html-tags': 'off', // This app uses hash-based routing (#/) where resolve() from $app/paths does not apply - 'svelte/no-navigation-without-resolve': 'off', // Sort HTML attributes alphabetically in the markup. The Svelte directives // (bind:/use:/animate:/style:/in:/out:/transition:/class:) sort first, @@ -321,6 +320,12 @@ export default ts.config( } } }, + { + // type information for the app's own modules too, so svelte/no-navigation-without-resolve + // accepts a URL typed as ResolvedPathname, such as RouterService.chat(id) + files: ['src/**/*.ts'], + languageOptions: { parserOptions: { projectService: true } } + }, { // Exclude generated build output and Storybook files from ESLint ignores: [ diff --git a/ui/apps/chat/sources.cmake b/ui/apps/chat/sources.cmake index d3daee6c9d37..f12daa8134c1 100644 --- a/ui/apps/chat/sources.cmake +++ b/ui/apps/chat/sources.cmake @@ -3,10 +3,11 @@ set(UI_SOURCE_GLOBS src/* static/* - # Llama UI, which the app compiles from source + # Llama UI and the harness, which the app compiles from source ../../lib/ui/src/lib/* # Llama Voice, also compiled from source ../../lib/voice/src/* + ../../lib/harness/src/* ) set(UI_SOURCE_FILES diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessage.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessage.svelte index 0086107eed80..f4172daaf969 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessage.svelte +++ b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessage.svelte @@ -30,6 +30,7 @@ import { parseFilesToMessageExtras } from '#lib/utils/browser-only.ts'; import { deriveAgenticSections } from '#lib/utils/index.ts'; import { goto } from '$app/navigation'; + import { resolve } from '$app/paths'; interface Props { class?: string; @@ -241,7 +242,7 @@ const conversationDeleted = await chatStore.removeSystemPromptPlaceholder(message.id); if (conversationDeleted) { - goto(ROUTES.START); + goto(resolve(ROUTES.START)); } return; @@ -282,7 +283,7 @@ const conversationDeleted = await chatStore.removeSystemPromptPlaceholder(message.id); if (conversationDeleted) { - goto(ROUTES.START); + goto(resolve(ROUTES.START)); } } else { chatActions.delete(message); @@ -362,7 +363,7 @@ isEditing = false; if (conversationDeleted) { - goto(ROUTES.START); + goto(resolve(ROUTES.START)); } else { focusMainChatForm(); } diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/ChatMessageToolCallBlockDefault.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/ChatMessageToolCallBlockDefault.svelte index 0cbe86535a22..9485dc8f11cf 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/ChatMessageToolCallBlockDefault.svelte +++ b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/ChatMessageToolCallBlockDefault.svelte @@ -3,9 +3,9 @@ // Renders section.toolArgs / section.toolResult directly using the // shared chrome shell. - import { simpleToolArgFields } from './parsers/tool-call-phrasing'; import ToolCallBlock from './ToolCallBlock.svelte'; import ToolCallTitle from './ToolCallTitle.svelte'; + import { simpleToolArgFields } from '@ggml-org/llama-harness'; import { Loader2 } from '@lucide/svelte'; import { MarkdownContent, SyntaxHighlightedCode } from '#lib/components/app/index.ts'; import { MAX_HEIGHT_CODE_BLOCK } from '#lib/constants/index.ts'; diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/ChatMessageToolCallBlockSearchResults.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/ChatMessageToolCallBlockSearchResults.svelte index efbbde97446a..1a8491d3aa7d 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/ChatMessageToolCallBlockSearchResults.svelte +++ b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/ChatMessageToolCallBlockSearchResults.svelte @@ -83,7 +83,7 @@ {#if faviconUrl} @@ -108,7 +108,7 @@ {result.title} diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/ToolCallBlock.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/ToolCallBlock.svelte index f8f9ee05be45..2f45f9a37ca3 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/ToolCallBlock.svelte +++ b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/ToolCallBlock.svelte @@ -8,17 +8,18 @@ // Components supply only their `meta`, a title snippet, and a body // snippet - everything around them is this single source of truth. - import { - describeToolCall, - formatToolCallLine, - isToolCallRunning - } from './parsers/tool-call-phrasing'; + import { describeToolCall } from './parsers/tool-call-phrasing'; import { useToolCallElapsed } from './tool-call-elapsed'; import ToolCallElapsed from './ToolCallElapsed.svelte'; + import { + formatToolCallLine, + isToolCallRunning, + type ToolCallDescription + } from '@ggml-org/llama-harness'; import { CollapsibleContentBlock } from '#lib/components/app/index.ts'; import { AgenticSectionType } from '#lib/enums/index.ts'; import { settingsStore } from '#lib/stores/index.ts'; - import type { AgenticSection, ToolCallDescription } from '#lib/types/index.ts'; + import type { AgenticSection } from '#lib/types/index.ts'; import { imageExtras, toolCallShownArg } from '#lib/utils/index.ts'; import type { Snippet } from 'svelte'; import { fade } from 'svelte/transition'; diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/_shared.ts b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/_shared.ts index e11a1fa4d336..122d70b540fb 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/_shared.ts +++ b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/_shared.ts @@ -4,14 +4,10 @@ // args-present check, JSON parse), plus the same path aliases - keeping // them here lets each parser stay focused on its own format quirks. -import { - FILE_PATH_SEPARATOR_REGEX, - TOOL_ARG_PATH_KEYS, - TOOL_ARG_STRING_FIELD_PATTERN_TEMPLATE -} from '#lib/constants/index.ts'; +import { parsePartialJsonArgs } from '@ggml-org/llama-harness'; +import { TOOL_ARG_PATH_KEYS } from '#lib/constants/index.ts'; import { BuiltInTool } from '#lib/enums/index.ts'; import type { AgenticSection } from '#lib/types/agentic.js'; -import { parsePartialJsonArgs } from '#lib/utils/parse-partial-json-args.ts'; /** * Strict (final-state) JSON parser for a tool-args blob. Mirrors the @@ -33,55 +29,6 @@ function parseFinalToolArgs(blob: string): Record | null { } } -// Compiled per key on first use; the key set is tiny and fixed. -const toolArgStringRegexes = new Map(); - -/** - * Extract a string field from a JSON tool-args blob without parsing the - * whole document. write_file and edit_file args embed full file contents, - * yet the block title needs only the path; a targeted key match plus a - * JSON.parse of the captured string literal alone keeps title rendering - * O(path) instead of O(blob). Returns undefined when the key is missing - * or its value is not a string; callers fall back to the full parse. - */ -export function extractToolArgString( - toolArgs: string, - keys: readonly string[] -): string | undefined { - for (const key of keys) { - let pattern = toolArgStringRegexes.get(key); - - if (!pattern) { - pattern = new RegExp(TOOL_ARG_STRING_FIELD_PATTERN_TEMPLATE.replace('{key}', key)); - toolArgStringRegexes.set(key, pattern); - } - - const match = pattern.exec(toolArgs); - - if (!match) continue; - - try { - const value: unknown = JSON.parse(`"${match[1]}"`); - - if (typeof value === 'string') return value; - } catch { - // fall through to the next key; the full parse is the fallback - } - } - - return undefined; -} - -/** - * File name shown in a block title, from a tool-call path. Undefined when - * the call carries no path; the title then shows the tool label alone. - */ -export function fileNameFromPath(filePath: string | undefined): string | undefined { - if (!filePath) return undefined; - - return filePath.split(FILE_PATH_SEPARATOR_REGEX).pop() || filePath; -} - /** * Read the path argument out of an already parsed args object, checking * the aliases in `TOOL_ARG_PATH_KEYS`. Returns undefined when the call diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/edit-file.ts b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/edit-file.ts index 72c1306a71af..d36979f82321 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/edit-file.ts +++ b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/edit-file.ts @@ -4,12 +4,8 @@ // `error` fields. The path is display-only (block title), so a call // that has not streamed one still shows its diffs. -import { - extractToolArgPath, - extractToolArgString, - fileNameFromPath, - parseToolArgs -} from './_shared'; +import { extractToolArgPath, parseToolArgs } from './_shared'; +import { extractToolArgString, fileNameFromPath } from '@ggml-org/llama-harness'; import { TOOL_ARG_PATH_KEYS } from '#lib/constants/index.ts'; import { BuiltInTool, DiffLineKind } from '#lib/enums/index.ts'; import type { diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/tool-call-phrasing.ts b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/tool-call-phrasing.ts index 8792bc90aac4..91d864e60cf1 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/tool-call-phrasing.ts +++ b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/tool-call-phrasing.ts @@ -1,44 +1,24 @@ -// Plain-language lines for tool calls: what one call did ("Read a file -// sheet_new.jpg"), what it is doing now ("Reading package.json") and what -// a run of calls did ("Ran 3 commands, read 2 files"). A call that has the -// arg the settings pick (`description` by default) shows it as the model -// wrote it. Otherwise built-in tools use TOOL_CALL_PHRASING, and MCP and -// unknown tools read as "Used " with their main argument. +// Plain-language lines for the tool calls of a trace. The phrasing itself is +// Llama Harness's (`createTracePhraser`), which knows llama-server's built-in +// tools; this module adds Llama Chat's own: the browser tools, web search and +// tool_search. -import { extractToolArgString, fileNameFromPath } from './_shared'; import { - TOOL_ARG_PATH_KEYS, - TOOL_CALL_MAIN_ARG_KEYS, + createTracePhraser, + fileNameFromPath, + toolCallPath, + type ToolCallResolver +} from '@ggml-org/llama-harness'; +import { TOOL_CALL_PHRASING, - TOOL_CALL_SHOWN_ARG_DEFAULT, - TOOL_CALL_SIMPLE_ARG_MAX_CHARS, - TOOL_CALL_SUMMARY, - TOOL_CALL_TARGET_MAX_CHARS, - TOOL_CALL_THINKING_LABEL, TOOL_SEARCH_LOAD_PHRASING, TOOL_SEARCH_PHRASING_NAME, TOOL_SEARCH_PHRASING_SELECT_PREFIX, TOOL_SEARCH_QUERY_PHRASING, WEB_SEARCH_PHRASING } from '#lib/constants/index.ts'; -import { AgenticSectionType, BuiltInTool, ToolCallSummaryKind } from '#lib/enums/index.ts'; -import type { AgenticSection, ToolCallDescription, ToolCallPhrasing } from '#lib/types/index.ts'; -import { isWebSearchToolName, memoizeBounded, parsePartialJsonArgs } from '#lib/utils/index.ts'; - -/** How many results each cache below holds. */ -const CACHE_MAX_SIZE = 256; -/** Args longer than this ( whole files ) are not worth holding as cache keys. */ -const CACHE_MAX_ARGS_CHARS = 2000; - -type ToolArgField = { label: string; value: string }; - -function shorten(text: string): string { - const oneLine = text.replace(/\s+/g, ' ').trim(); - - return oneLine.length > TOOL_CALL_TARGET_MAX_CHARS - ? `${oneLine.slice(0, TOOL_CALL_TARGET_MAX_CHARS - 3)}...` - : oneLine; -} +import { BuiltInTool } from '#lib/enums/index.ts'; +import { isWebSearchToolName } from '#lib/utils/index.ts'; function stringArg(args: Record | null, key: string): string | undefined { const value = args?.[key]; @@ -46,92 +26,25 @@ function stringArg(args: Record | null, key: string): string | return typeof value === 'string' && value.trim() ? value : undefined; } -function genericMainArg(args: Record | null): string | undefined { - if (!args) return undefined; - - for (const key of TOOL_CALL_MAIN_ARG_KEYS) { - const value = stringArg(args, key); - - if (value) return value; - } - - for (const value of Object.values(args)) { - if (typeof value === 'number') return String(value); - - if (typeof value === 'string' && value.trim() && value.length <= TOOL_CALL_TARGET_MAX_CHARS) { - return value; - } - } - - return undefined; -} - -function genericPhrasing(toolName: string): ToolCallPhrasing { - return { - object: toolName, - past: 'Used', - present: 'Using', - summary: ToolCallSummaryKind.OTHER, - targetObject: toolName - }; -} - -interface ResolvedCall { - phrasing: ToolCallPhrasing; - target?: string; - count?: number; -} - -function resolveCall(toolName: string, toolArgs: string): ResolvedCall { - // write_file and edit_file args hold whole files: read the path with a - // targeted match instead of parsing the blob - if ( - toolName === BuiltInTool.SERVER_READ_FILE || - toolName === BuiltInTool.SERVER_WRITE_FILE || - toolName === BuiltInTool.SERVER_EDIT_FILE || - toolName === BuiltInTool.BROWSER_READ_MEDIA - ) { - const path = - extractToolArgString(toolArgs, TOOL_ARG_PATH_KEYS) ?? - stringArg(parsePartialJsonArgs(toolArgs), 'path'); - - return { phrasing: TOOL_CALL_PHRASING[toolName], target: fileNameFromPath(path) }; - } - - const args = toolArgs ? parsePartialJsonArgs(toolArgs) : null; - - if (toolName === BuiltInTool.SERVER_EXEC_SHELL_COMMAND) { - // the targeted match keeps the command in the line while a later arg - // is still streaming and the JSON does not parse - return { - phrasing: TOOL_CALL_PHRASING[toolName], - target: stringArg(args, 'command') ?? extractToolArgString(toolArgs, ['command']) - }; - } - - switch (toolName) { - case BuiltInTool.SERVER_GREP_SEARCH: - return { phrasing: TOOL_CALL_PHRASING[toolName], target: stringArg(args, 'pattern') }; - - case BuiltInTool.SERVER_FILE_GLOB_SEARCH: { - const include = stringArg(args, 'include'); - +const chatToolResolver: ToolCallResolver = (call) => { + switch (call.name) { + case BuiltInTool.BROWSER_READ_MEDIA: return { - phrasing: TOOL_CALL_PHRASING[toolName], - target: include && include !== '**' ? include : undefined + phrasing: TOOL_CALL_PHRASING[call.name], + target: fileNameFromPath(toolCallPath(call)) }; - } case BuiltInTool.BROWSER_SEARCH_CHATS: { - const matches = Array.isArray(args?.by_matches) - ? args.by_matches.filter((m): m is string => typeof m === 'string' && m.trim() !== '') + const byMatches = call.args()?.by_matches; + const matches = Array.isArray(byMatches) + ? byMatches.filter((m): m is string => typeof m === 'string' && m.trim() !== '') : []; - return { phrasing: TOOL_CALL_PHRASING[toolName], target: matches.join(', ') || undefined }; + return { phrasing: TOOL_CALL_PHRASING[call.name], target: matches.join(', ') || undefined }; } case TOOL_SEARCH_PHRASING_NAME: { - const query = stringArg(args, 'query')?.trim(); + const query = stringArg(call.args(), 'query')?.trim(); if (query?.startsWith(TOOL_SEARCH_PHRASING_SELECT_PREFIX)) { const names = query @@ -151,295 +64,14 @@ function resolveCall(toolName: string, toolArgs: string): ResolvedCall { } } - if (isWebSearchToolName(toolName)) { - return { phrasing: WEB_SEARCH_PHRASING, target: stringArg(args, 'query') }; - } - - const known = TOOL_CALL_PHRASING[toolName]; - - if (known) return { phrasing: known }; - - return { phrasing: genericPhrasing(toolName), target: genericMainArg(args) }; -} - -/** An arg name the settings can pick: a plain key, so it is safe in a pattern. */ -const SHOWN_ARG_PATTERN = /^[\w-]+$/; - -/** - * The value of the arg a call's line shows, when the call has it. Whole-file - * args are not parsed for it: the targeted match reads it instead. - */ -function shownArgValue(toolArgs: string, shownArg: string | null): string | undefined { - if (!shownArg || !toolArgs || !SHOWN_ARG_PATTERN.test(shownArg)) return undefined; - - const value = - toolArgs.length <= CACHE_MAX_ARGS_CHARS - ? stringArg(parsePartialJsonArgs(toolArgs), shownArg) - : extractToolArgString(toolArgs, [shownArg]); - - return value?.trim() || undefined; -} - -/** - * Describe one tool call as a short sentence, in the past tense once it is - * done and the present tense while `running`. `shownArg` names the arg - * shown instead when the call has it ( null for none ). - */ -export function describeToolCall( - toolName: string | undefined, - toolArgs: string | undefined, - running = false, - shownArg: string | null = TOOL_CALL_SHOWN_ARG_DEFAULT -): ToolCallDescription { - return describeCached(toolName || 'a tool', toolArgs ?? '', running, shownArg ?? ''); -} - -// Sections are rebuilt on every streamed chunk, so the same finished call is -// described again and again: remember the answer for its args. Every caller -// of one call reads the same frozen object, so a title cannot edit the line -// out for the others. -const describeCached = memoizeBounded( - (name: string, args: string, running: boolean, shownArg: string) => - Object.freeze(buildDescription(name, args, running, shownArg || null)), - (name, args, running, shownArg) => - args.length <= CACHE_MAX_ARGS_CHARS - ? `${running ? 1 : 0}\0${shownArg}\0${name}\0${args}` - : null, - CACHE_MAX_SIZE -); - -function buildDescription( - name: string, - args: string, - running: boolean, - shownArg: string | null -): ToolCallDescription { - const { count = 1, phrasing, target } = resolveCall(name, args); - const shown = shownArgValue(args, shownArg); - // an arg the row already shows as its target ( a shell command ) would - // only repeat it - const described = shown && shown !== target?.trim() ? shown : undefined; - - if (described) { - return { - count, - described: true, - // shown as the model wrote it: rewriting its tense by rule mangles - // anything that does not start with a plain verb - label: shorten(described).replace(/[.\s]+$/, ''), - quoted: false, - summary: phrasing.summary - }; - } - - const verb = running ? phrasing.present : phrasing.past; - const shortTarget = target ? shorten(target) : undefined; - const object = shortTarget ? phrasing.targetObject : phrasing.object; - - return { - count, - label: object ? `${verb} ${object}` : verb, - quoted: Boolean(phrasing.quoteTarget && shortTarget), - summary: phrasing.summary, - target: shortTarget - }; -} - -/** One line of plain text for a description, e.g. `Searched for 'issue'`. */ -export function formatToolCallLine(description: ToolCallDescription): string { - if (!description.target) return description.label; - - const target = description.quoted ? `'${description.target}'` : description.target; - - return `${description.label} ${target}`; -} - -export function isToolCallSection(section: AgenticSection): boolean { - return ( - section.type === AgenticSectionType.TOOL_CALL || - section.type === AgenticSectionType.TOOL_CALL_PENDING || - section.type === AgenticSectionType.TOOL_CALL_STREAMING - ); -} - -/** True while the call has no result yet and the reply is still streaming. */ -export function isToolCallRunning(section: AgenticSection, isStreaming: boolean): boolean { - return ( - isStreaming && - (section.type === AgenticSectionType.TOOL_CALL_PENDING || - section.type === AgenticSectionType.TOOL_CALL_STREAMING) - ); -} - -/** - * Summarize a run of tool calls, e.g. "Ran 3 commands, read 2 files". - * Kinds keep the order they first appear in; other tools come last. - */ -export function summarizeToolCalls(sections: readonly AgenticSection[]): string { - const counts = new Map(); - - for (const section of sections) { - if (!isToolCallSection(section)) continue; - - const { count, summary } = describeToolCall(section.toolName, section.toolArgs); - - counts.set(summary, (counts.get(summary) ?? 0) + count); - } - - const other = counts.get(ToolCallSummaryKind.OTHER); - - if (other !== undefined) { - counts.delete(ToolCallSummaryKind.OTHER); - counts.set(ToolCallSummaryKind.OTHER, other); - } - - const clauses = [...counts].map(([kind, n]) => TOOL_CALL_SUMMARY[kind](n)); - const text = clauses.join(', '); - - return text.charAt(0).toUpperCase() + text.slice(1); -} - -/** - * What a run of calls is doing now, in user words ("Running a command", - * "Thinking"): the model thinking at its end, or the last call still - * running; null when neither is. - */ -export function describeToolActivity( - sections: readonly AgenticSection[], - isStreaming: boolean, - executingToolCallId?: string | null, - shownArgFor: (toolName: string | undefined) => string | null = () => TOOL_CALL_SHOWN_ARG_DEFAULT -): string | null { - // the model thinking at the end of the run, between calls or before its answer - if (isStreaming && sections.at(-1)?.type === AgenticSectionType.REASONING_PENDING) { - return TOOL_CALL_THINKING_LABEL; - } - - for (let i = sections.length - 1; i >= 0; i--) { - const section = sections[i]; - const executing = - section.toolCallId !== undefined && section.toolCallId === executingToolCallId; - - if (executing || isToolCallRunning(section, isStreaming)) { - return formatToolCallLine( - describeToolCall(section.toolName, section.toolArgs, true, shownArgFor(section.toolName)) - ); - } - } - - return null; -} - -/** - * Tool args as labelled fields when every value is a short, one-line - * scalar; null when the args need the JSON view instead. Memoized like - * describeToolCall: the row that shows them re-renders on every chunk. - */ -export function simpleToolArgFields(toolArgs: string | undefined): ToolArgField[] | null { - return toolArgs ? argFieldsCached(toolArgs) : null; -} - -const argFieldsCached = memoizeBounded( - buildSimpleToolArgFields, - (toolArgs) => (toolArgs.length <= CACHE_MAX_ARGS_CHARS ? toolArgs : null), - CACHE_MAX_SIZE -); - -function buildSimpleToolArgFields(toolArgs: string): ToolArgField[] | null { - let parsed: unknown; - - try { - parsed = JSON.parse(toolArgs); - } catch { - return null; - } - - if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return null; - - const entries = Object.entries(parsed as Record); - - if (entries.length === 0) return null; - - const fields: ToolArgField[] = []; - - for (const [key, value] of entries) { - if (value !== null && typeof value === 'object') return null; - - const text = String(value); - - if (text.length > TOOL_CALL_SIMPLE_ARG_MAX_CHARS || text.includes('\n')) return null; - - fields.push({ label: humanizeArgKey(key), value: text }); + if (isWebSearchToolName(call.name)) { + return { phrasing: WEB_SEARCH_PHRASING, target: stringArg(call.args(), 'query') }; } - return fields; -} - -/** `max_results` / `maxResults` -> `Max results`. */ -export function humanizeArgKey(key: string): string { - const words = key - .replace(/([a-z0-9])([A-Z])/g, '$1 $2') - .replace(/[_-]+/g, ' ') - .trim() - .toLowerCase(); - - return words.charAt(0).toUpperCase() + words.slice(1); -} - -export function isReasoningSection(section: AgenticSection): boolean { - return ( - section.type === AgenticSectionType.REASONING || - section.type === AgenticSectionType.REASONING_PENDING - ); -} - -/** - * Split section positions into single items and runs of tool work, e.g. - * [0, [1, 2, 3], 4]. A run is the calls and all the reasoning around them, - * up to the next text, so the answer follows the work as one row: it is - * grouped once it holds `minCalls` calls, or one call with reasoning beside - * it. Reasoning with no call in reach, and text, stay apart. - */ -export function groupToolCallRuns( - sections: readonly AgenticSection[], - indices: readonly number[], - minCalls: number -): (number | number[])[] { - const items: (number | number[])[] = []; - - let run: number[] = []; - let calls = 0; - // reasoning seen since the last call: it ends the run if text follows, - // and leads into it if another call does - let thinking: number[] = []; - - const flush = () => { - const whole = [...run, ...thinking]; - - if (calls >= minCalls || (calls > 0 && whole.length > calls)) items.push(whole); - else items.push(...whole); - - run = []; - thinking = []; - calls = 0; - }; - - for (const index of indices) { - const section = sections[index]; - - if (isToolCallSection(section)) { - run.push(...thinking, index); - thinking = []; - calls++; - } else if (isReasoningSection(section)) { - thinking.push(index); - } else { - flush(); - items.push(index); - } - } - - flush(); + return undefined; +}; - return items; -} +export const { describeToolActivity, describeToolCall, summarizeToolCalls } = createTracePhraser({ + phrasings: TOOL_CALL_PHRASING, + resolvers: [chatToolResolver] +}); diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/write-file.ts b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/write-file.ts index 3a88fb5f9f21..63557211c5c4 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/write-file.ts +++ b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/write-file.ts @@ -4,12 +4,8 @@ // result blob. The path is display-only (title + highlight language), // so a call that has not streamed one still shows its content. -import { - extractToolArgPath, - extractToolArgString, - fileNameFromPath, - parseToolArgs -} from './_shared'; +import { extractToolArgPath, parseToolArgs } from './_shared'; +import { extractToolArgString, fileNameFromPath } from '@ggml-org/llama-harness'; import { CODE_BLOCK, TOOL_ARG_PATH_KEYS } from '#lib/constants/index.ts'; import { BuiltInTool } from '#lib/enums/index.ts'; import type { AgenticSection, WriteFileMeta, WriteFileTitleMeta } from '#lib/types/index.ts'; diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessageAgentTrace.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessageAgentTrace.svelte index f0e7bffde8dd..988cb11d9afa 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessageAgentTrace.svelte +++ b/ui/apps/chat/src/lib/components/app/chat/ChatMessages/ChatMessageAgentTrace.svelte @@ -2,12 +2,9 @@ import ChatMessageToolCallBlock from './ChatMessage/ChatMessageToolCall/ChatMessageToolCallBlock.svelte'; import ChatMessageToolCallGroup from './ChatMessage/ChatMessageToolCall/ChatMessageToolCallGroup.svelte'; import { parseExecShellCommandMeta } from './ChatMessage/ChatMessageToolCall/parsers/exec-shell-command'; - import { - groupToolCallRuns, - isToolCallSection - } from './ChatMessage/ChatMessageToolCall/parsers/tool-call-phrasing'; import { setToolCallElapsedContext } from './ChatMessage/ChatMessageToolCall/tool-call-elapsed'; import ChatMessageReasoningBlock from './ChatMessageReasoningBlock.svelte'; + import { groupToolCallRuns, isToolCallStep } from '@ggml-org/llama-harness'; import { ChatMessageActionCardContinueRequest, ChatMessageActionCardPermissionRequest, @@ -168,7 +165,7 @@ result.push(groupToolCallRuns(sections, indices, TOOL_CALL_GROUP_MIN_CALLS)); for (let i = 0; i < sections.length; i++) { - const isTool = isToolCallSection(sections[i]); + const isTool = isToolCallStep(sections[i]); // a turn ends where its tool calls do if (!isTool && prevWasTool && indices.length > 0) { @@ -202,7 +199,7 @@ return toolCallId ? { startedAt: taskStartedAt, toolCallId } : null; }); - const lastToolIndex = $derived(sections.findLastIndex((section) => isToolCallSection(section))); + const lastToolIndex = $derived(sections.findLastIndex((section) => isToolCallStep(section))); // The status row shows only while nothing else in the reply is moving: a // running row, a permission card or streaming text already says what is diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatScreen/ChatScreen.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatScreen/ChatScreen.svelte index 7cf2366e304e..df28b777a86b 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatScreen/ChatScreen.svelte +++ b/ui/apps/chat/src/lib/components/app/chat/ChatScreen/ChatScreen.svelte @@ -372,7 +372,7 @@ {/if}
- {#if stick.farFromBottom && page.url.hash.includes(ROUTES.CHAT) && page.params.id} + {#if stick.farFromBottom && page.route.id === ROUTES.CHAT && page.params.id} stick.scrollToBottom('smooth')} /> {/if}
diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatTabs/ChatTabsItem.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatTabs/ChatTabsItem.svelte index c01af20d6f4b..d5c5509568c2 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatTabs/ChatTabsItem.svelte +++ b/ui/apps/chat/src/lib/components/app/chat/ChatTabs/ChatTabsItem.svelte @@ -10,10 +10,11 @@ TAB_SURFACE_IDLE_CLASS, UI_DATA_ATTRS } from '#lib/constants/index.ts'; + import type { ResolvedPathname } from '$app/types'; import type { Component } from 'svelte'; interface Tab { - href: string; + href: ResolvedPathname; /** Marks the new-chat tab. */ icon?: Component; id: string; diff --git a/ui/apps/chat/src/lib/components/app/dialogs/DialogModelNotAvailable.svelte b/ui/apps/chat/src/lib/components/app/dialogs/DialogModelNotAvailable.svelte index 5a7a6ba4d40a..818bf5779527 100644 --- a/ui/apps/chat/src/lib/components/app/dialogs/DialogModelNotAvailable.svelte +++ b/ui/apps/chat/src/lib/components/app/dialogs/DialogModelNotAvailable.svelte @@ -26,6 +26,7 @@ url.searchParams.set(URL_PARAMS.MODEL, model); handleOpenChange(false); + // eslint-disable-next-line svelte/no-navigation-without-resolve -- the current page's own URL with its query changed, not a link to resolve goto(url.toString()); } diff --git a/ui/apps/chat/src/lib/components/app/mcp/McpServerIdentity.svelte b/ui/apps/chat/src/lib/components/app/mcp/McpServerIdentity.svelte index c716652b0556..256baa2dd1f4 100644 --- a/ui/apps/chat/src/lib/components/app/mcp/McpServerIdentity.svelte +++ b/ui/apps/chat/src/lib/components/app/mcp/McpServerIdentity.svelte @@ -54,7 +54,7 @@ class="shrink-0 text-muted-foreground hover:text-foreground" href={safeWebsiteUrl} onclick={(e) => e.stopPropagation()} - rel="noopener noreferrer" + rel="external noopener noreferrer" target="_blank" > diff --git a/ui/apps/chat/src/lib/components/app/models/discover/ModelsDiscoverDetails/ModelsDiscoverDetailsHeader.svelte b/ui/apps/chat/src/lib/components/app/models/discover/ModelsDiscoverDetails/ModelsDiscoverDetailsHeader.svelte index 64bdbd5f62ed..26db7f7a9c7f 100644 --- a/ui/apps/chat/src/lib/components/app/models/discover/ModelsDiscoverDetails/ModelsDiscoverDetailsHeader.svelte +++ b/ui/apps/chat/src/lib/components/app/models/discover/ModelsDiscoverDetails/ModelsDiscoverDetailsHeader.svelte @@ -73,7 +73,7 @@ aria-label="View base model on HuggingFace" class="shrink-0 text-muted-foreground transition-colors hover:text-foreground" href={HuggingFaceService.getModelUrl(baseModels[0])} - rel="noopener noreferrer" + rel="external noopener noreferrer" target="_blank" > @@ -86,7 +86,7 @@ diff --git a/ui/apps/chat/src/routes/+layout.svelte b/ui/apps/chat/src/routes/+layout.svelte index 8fcef551be52..5f45697132d6 100644 --- a/ui/apps/chat/src/routes/+layout.svelte +++ b/ui/apps/chat/src/routes/+layout.svelte @@ -9,7 +9,6 @@ FAVICON_PATHS, FAVICON_SELECTORS, HEADERS, - MODELS_ROUTE_ID, NEW_CHAT_TAB_ID, ROUTES, SETTINGS_KEYS, @@ -173,9 +172,9 @@ untrack(() => { if ( - (page.route.id === '/(chat)' || - page.route.id === '/(chat)/chat/[id]' || - page.route.id === MODELS_ROUTE_ID) && + (page.route.id === ROUTES.START || + page.route.id === ROUTES.CHAT || + page.route.id === ROUTES.MODELS) && page.status !== 401 && page.status !== 403 ) { @@ -384,7 +383,7 @@ - {#if deviceStore.isMobile && !page.url.hash.includes(ROUTES.SEARCH) && !uiStore.isComposerExpanded} + {#if deviceStore.isMobile && page.route.id !== ROUTES.SEARCH && !uiStore.isComposerExpanded}
{ if (browser && !deviceStore.isMobile) { - goto(ROUTES.START, { replace: true }); + goto(resolve(ROUTES.START), { replace: true }); } }); diff --git a/ui/apps/chat/tests/client/nested-surfaces.svelte.test.ts b/ui/apps/chat/tests/client/nested-surfaces.svelte.test.ts index 302c1b6f35fb..dd92bca23d25 100644 --- a/ui/apps/chat/tests/client/nested-surfaces.svelte.test.ts +++ b/ui/apps/chat/tests/client/nested-surfaces.svelte.test.ts @@ -2,7 +2,6 @@ // group inset in the sidebar and a row in the group. This holds the rule app.css states for // them, on what the browser actually renders: a row's corners are concentric with its // group's, so its radius is the group's less the padding between their edges. - import '../../src/app.css'; import SidebarNavigationActions from '#lib/components/app/navigation/SidebarNavigation/SidebarNavigationActions.svelte'; import SidebarNavigationFooter from '#lib/components/app/navigation/SidebarNavigation/SidebarNavigationFooter.svelte'; diff --git a/ui/apps/chat/tests/stories/chat/tabs/ChatTabsItem.stories.svelte b/ui/apps/chat/tests/stories/chat/tabs/ChatTabsItem.stories.svelte index c51ee56c32fd..95a5a5b360e5 100644 --- a/ui/apps/chat/tests/stories/chat/tabs/ChatTabsItem.stories.svelte +++ b/ui/apps/chat/tests/stories/chat/tabs/ChatTabsItem.stories.svelte @@ -2,7 +2,9 @@ import { SquarePen } from '@lucide/svelte'; import { defineMeta } from '@storybook/addon-svelte-csf'; import ChatTabsItem from '#lib/components/app/chat/ChatTabs/ChatTabsItem.svelte'; - import { NEW_CHAT_LABEL, NEW_CHAT_TAB_ID } from '#lib/constants/index.ts'; + import { NEW_CHAT_LABEL, NEW_CHAT_TAB_ID, ROUTES } from '#lib/constants/index.ts'; + import { RouterService } from '#lib/services/router.service.ts'; + import { resolve } from '$app/paths'; import StateGallery from '$llama-ui-stories/helpers/StateGallery.svelte'; import StateGalleryItem from '$llama-ui-stories/helpers/StateGalleryItem.svelte'; @@ -14,7 +16,7 @@ title: 'Llama Chat/Chat/ChatTabs/ChatTabsItem' }); - const tab = { href: '#/chat/conv-1', id: 'conv-1', name: 'Rust borrow checker' }; + const tab = { href: RouterService.chat('conv-1'), id: 'conv-1', name: 'Rust borrow checker' }; const handlers = { onActivate: (id: string) => console.log('activate', id), @@ -37,7 +39,12 @@ diff --git a/ui/apps/chat/tests/unit/agentic-flow.test.ts b/ui/apps/chat/tests/unit/agentic-flow.test.ts new file mode 100644 index 000000000000..1003f8fcb871 --- /dev/null +++ b/ui/apps/chat/tests/unit/agentic-flow.test.ts @@ -0,0 +1,373 @@ +// The agentic flow end to end, with the server and the tools mocked: what the +// model is sent each turn, and the order the flow's callbacks fire in. + +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +vi.mock('#lib/services/database.service.ts', () => ({ + DatabaseService: {} +})); + +import { BuiltInTool, MessageRole, ToolPermissionDecision, ToolSource } from '#lib/enums/index.ts'; +import { ChatService } from '#lib/services/chat.service.ts'; +import { ToolsService } from '#lib/services/tools.service.ts'; +import { agenticStore } from '#lib/stores/agentic/index.svelte.ts'; +import { modelsStore } from '#lib/stores/models/index.svelte.ts'; +import { permissionsStore } from '#lib/stores/permissions.svelte.ts'; +import { toolsStore } from '#lib/stores/tools.svelte.ts'; +import type { AgenticFlowCallbacks } from '#lib/types/agentic.js'; +import type { ApiChatMessageData } from '#lib/types/api.js'; +import type { ChatMessageTimings } from '#lib/types/chat.js'; +import type { DatabaseMessage } from '#lib/types/database.js'; + +const CONV_ID = 'conv-1'; +const USER_MESSAGES: ApiChatMessageData[] = [{ content: 'hi', role: MessageRole.USER }]; +const IMAGE_DATA_URI = 'data:image/png;base64,AAAA'; + +type StreamOptions = { + onChunk?: (chunk: string) => void; + onReasoningChunk?: (chunk: string) => void; + onTimings?: (timings?: ChatMessageTimings) => void; + onToolCallChunk?: (serialized: string) => void; +}; +type Turn = (options: StreamOptions) => void | Promise; + +function toolCalls(...names: string[]): string { + return JSON.stringify( + names.map((name, index) => ({ function: { arguments: '{}', name }, id: `call_${index}` })) + ); +} + +// each request plays the next scripted turn; returns what every request was sent +function mockTurns(...turns: Turn[]): ApiChatMessageData[][] { + const requests: ApiChatMessageData[][] = []; + + vi.spyOn(ChatService, 'sendMessage').mockImplementation(async (messages, options) => { + requests.push(structuredClone(messages) as ApiChatMessageData[]); + await turns[requests.length - 1]?.(options as StreamOptions); + }); + + return requests; +} + +function mockTools(source: ToolSource, ...names: string[]) { + vi.spyOn(toolsStore, 'getEnabledToolsForLLM').mockReturnValue( + names.map((name) => ({ function: { name, parameters: {} }, type: 'function' })) + ); + vi.spyOn(toolsStore, 'getToolSource').mockReturnValue(source); +} + +// callbacks that log their calls in order +function recorder() { + const calls: unknown[][] = []; + const log = + (name: string) => + (...args: unknown[]) => { + calls.push([name, ...args]); + }; + + let nextMessageId = 0; + + const createMessage = + (name: string) => + async (...args: unknown[]) => { + calls.push([name, ...args]); + + return { id: `msg_${nextMessageId++}` } as DatabaseMessage; + }; + const callbacks: AgenticFlowCallbacks = { + createAssistantMessage: createMessage('createAssistantMessage'), + createToolResultMessage: createMessage('createToolResultMessage'), + onAssistantTurnComplete: async (...args) => { + calls.push(['onAssistantTurnComplete', ...args]); + }, + onAttachments: log('onAttachments'), + onChunk: log('onChunk'), + onError: log('onError'), + onFlowComplete: log('onFlowComplete'), + onReasoningChunk: log('onReasoningChunk'), + onToolCallsStreaming: log('onToolCallsStreaming'), + onTurnComplete: log('onTurnComplete'), + updateToolResultMessage: async (...args) => { + calls.push(['updateToolResultMessage', ...args]); + } + }; + + return { + callbacks, + calls, + named: (name: string) => calls.filter((call) => call[0] === name), + names: () => calls.map((call) => call[0]) + }; +} + +function runFlow(callbacks: AgenticFlowCallbacks, signal?: AbortSignal) { + return agenticStore.runAgenticFlow({ + callbacks, + conversationId: CONV_ID, + messages: USER_MESSAGES, + signal + }); +} + +beforeEach(() => { + vi.restoreAllMocks(); + agenticStore.clearSession(CONV_ID); + vi.spyOn(console, 'log').mockImplementation(() => {}); + vi.spyOn(ChatService, 'normalizeMessagesForApi').mockImplementation( + async (messages) => messages as ApiChatMessageData[] + ); + vi.spyOn(toolsStore, 'fetchServerTools').mockResolvedValue(); + vi.spyOn(toolsStore, 'getToolServerLabel').mockReturnValue('Browser'); + vi.spyOn(toolsStore, 'getPermissionKey').mockReturnValue('tool-key'); + vi.spyOn(permissionsStore, 'hasTool').mockReturnValue(true); +}); + +describe('agentic flow', () => { + it('is not handled when no tool is enabled', async () => { + vi.spyOn(toolsStore, 'getEnabledToolsForLLM').mockReturnValue([]); + + const send = vi.spyOn(ChatService, 'sendMessage'); + + expect(await runFlow(recorder().callbacks)).toEqual({ handled: false }); + expect(send).not.toHaveBeenCalled(); + }); + + it('runs a tool call, then lets the model answer', async () => { + mockTools(ToolSource.BROWSER, BuiltInTool.BROWSER_GET_DATETIME); + + const requests = mockTurns( + (options) => { + options.onChunk?.('checking'); + options.onToolCallChunk?.(toolCalls(BuiltInTool.BROWSER_GET_DATETIME)); + options.onTimings?.({ predicted_ms: 10, predicted_n: 4, prompt_ms: 20, prompt_n: 8 }); + }, + (options) => { + options.onReasoningChunk?.('hm'); + options.onChunk?.('it is noon'); + options.onTimings?.({ predicted_ms: 5, predicted_n: 2, prompt_ms: 1, prompt_n: 1 }); + } + ); + const { callbacks, named, names } = recorder(); + + expect(await runFlow(callbacks)).toEqual({ handled: true }); + expect(names()).toEqual([ + 'onChunk', + 'onToolCallsStreaming', + 'onAssistantTurnComplete', + 'createToolResultMessage', + 'onTurnComplete', + 'createAssistantMessage', + 'onReasoningChunk', + 'onChunk', + 'onAssistantTurnComplete', + 'onTurnComplete', + 'onFlowComplete' + ]); + + const [toolTurn, finalTurn] = named('onAssistantTurnComplete'); + const call = { + function: { arguments: '{}', name: BuiltInTool.BROWSER_GET_DATETIME }, + id: 'call_0', + type: 'function' + }; + + expect(toolTurn.slice(1)).toEqual([ + 'checking', + undefined, + { predicted_ms: 10, predicted_n: 4, prompt_ms: 20, prompt_n: 8 }, + [call] + ]); + expect(finalTurn.slice(1, 3)).toEqual(['it is noon', 'hm']); + expect(finalTurn[4]).toBeUndefined(); + + const [, toolCallId, toolResult, extras] = named('createToolResultMessage')[0]; + + expect([toolCallId, extras]).toEqual(['call_0', undefined]); + expect(requests[1].slice(1)).toEqual([ + { content: 'checking', role: 'assistant', tool_calls: [call] }, + { content: toolResult, role: 'tool', tool_call_id: 'call_0' } + ]); + expect(named('onFlowComplete')[0][1]).toMatchObject({ + agentic: { + llm: { predicted_ms: 15, predicted_n: 6, prompt_ms: 21, prompt_n: 9 }, + perTurn: [{ turn: 1 }, { turn: 2 }], + toolCalls: [{ name: BuiltInTool.BROWSER_GET_DATETIME, success: true }], + toolCallsCount: 1, + turns: 2 + }, + predicted_n: 2 + }); + expect(agenticStore.getTotalToolCalls(CONV_ID)).toBe(1); + expect(agenticStore.isRunning(CONV_ID)).toBe(false); + }); + + it('waits for permission and tells the model when it is denied', async () => { + mockTools(ToolSource.BROWSER, BuiltInTool.BROWSER_GET_DATETIME); + vi.mocked(permissionsStore.hasTool).mockReturnValue(false); + + const requests = mockTurns( + (options) => options.onToolCallChunk?.(toolCalls(BuiltInTool.BROWSER_GET_DATETIME)), + (options) => options.onChunk?.('ok') + ); + const { callbacks, named } = recorder(); + const flow = runFlow(callbacks); + + await vi.waitFor(() => + expect(agenticStore.getPendingPermissionRequest(CONV_ID)).toEqual({ + serverLabel: 'Browser', + toolCallId: 'call_0', + toolName: BuiltInTool.BROWSER_GET_DATETIME + }) + ); + agenticStore.resolvePermission(CONV_ID, ToolPermissionDecision.DENY); + await flow; + + expect(named('createToolResultMessage')[0].slice(1)).toEqual([ + 'call_0', + 'Tool execution was denied by the user.', + undefined + ]); + expect(requests[1][2]).toMatchObject({ content: 'Tool execution was denied by the user.' }); + expect(named('onFlowComplete')[0][1]).toMatchObject({ + agentic: { toolCalls: [{ success: false }] } + }); + }); + + it('reports a failing tool to the model and goes on', async () => { + mockTools(ToolSource.SERVER, 'read_file'); + vi.spyOn(ToolsService, 'executeTool').mockRejectedValue(new Error('no such file')); + + const requests = mockTurns( + (options) => options.onToolCallChunk?.(toolCalls('read_file')), + (options) => options.onChunk?.('sorry') + ); + const { callbacks } = recorder(); + + expect(await runFlow(callbacks)).toEqual({ handled: true }); + expect(requests[1][2]).toMatchObject({ content: 'Error: no such file', role: 'tool' }); + }); + + it('shows the output of a streaming tool while it runs', async () => { + mockTools(ToolSource.SERVER, BuiltInTool.SERVER_EXEC_SHELL_COMMAND); + vi.spyOn(ToolsService, 'streamTool').mockImplementation(async function* () { + yield { chunk: 'one\n', done: false }; + await new Promise((resolve) => setTimeout(resolve, 0)); + yield { chunk: 'two\n', done: false }; + await new Promise((resolve) => setTimeout(resolve, 0)); + yield { chunk: null, done: true }; + }); + + const requests = mockTurns( + (options) => options.onToolCallChunk?.(toolCalls(BuiltInTool.SERVER_EXEC_SHELL_COMMAND)), + (options) => options.onChunk?.('done') + ); + const { callbacks, named } = recorder(); + + await runFlow(callbacks); + + // created empty when the tool starts, then rewritten in place: never created twice + expect(named('createToolResultMessage').map((call) => call.slice(1))).toEqual([ + ['call_0', '', undefined, undefined] + ]); + expect(named('updateToolResultMessage').map((call) => call.slice(1))).toEqual([ + ['msg_0', 'one\n'], + ['msg_0', 'one\ntwo\n'] + ]); + expect(requests[1][2]).toMatchObject({ content: 'one\ntwo\n', role: 'tool' }); + }); + + it('takes an image out of a tool result and sends it to a model with vision', async () => { + mockTools(ToolSource.SERVER, 'screenshot'); + vi.spyOn(ToolsService, 'executeTool').mockResolvedValue({ + content: `saved\n${IMAGE_DATA_URI}`, + isError: false + }); + vi.spyOn(modelsStore.props, 'modelSupportsVision').mockReturnValue(true); + + const requests = mockTurns( + (options) => options.onToolCallChunk?.(toolCalls('screenshot')), + (options) => options.onChunk?.('here') + ); + const { callbacks, named } = recorder(); + + await runFlow(callbacks); + + const [, , content, extras] = named('createToolResultMessage')[0]; + + expect(content).toMatch(/^saved\n\[Attachment saved: .+\.png\]$/); + expect(extras).toMatchObject([{ base64Url: IMAGE_DATA_URI, type: 'IMAGE' }]); + expect(named('onAttachments')[0].slice(1)).toEqual(['msg_0', extras]); + expect(requests[1][2]).toEqual({ + content: [ + { text: content, type: 'text' }, + { image_url: { url: IMAGE_DATA_URI }, type: 'image_url' } + ], + role: 'tool', + tool_call_id: 'call_0' + }); + }); + + it('leaves the flow when a steering message is queued, skipping the tool calls left', async () => { + mockTools(ToolSource.SERVER, 'first', 'second'); + vi.spyOn(ToolsService, 'executeTool').mockImplementation(async () => { + agenticStore.injectSteeringMessage(CONV_ID, 'wait'); + + return { content: 'first ran', isError: false }; + }); + + const requests = mockTurns((options) => + options.onToolCallChunk?.(toolCalls('first', 'second')) + ); + const { callbacks, named, names } = recorder(); + + expect(await runFlow(callbacks)).toEqual({ handled: true }); + expect(named('createToolResultMessage').map((call) => call.slice(1, 3))).toEqual([ + ['call_0', 'first ran'], + ['call_1', 'Tool execution was interrupted by a new user message.'] + ]); + expect(names().slice(-2)).toEqual(['onTurnComplete', 'onFlowComplete']); + expect(requests).toHaveLength(1); + expect(agenticStore.consumePendingSteeringMessage(CONV_ID)).toMatchObject({ content: 'wait' }); + }); + + it('keeps the partial turn and reports the error when the stream fails', async () => { + mockTools(ToolSource.BROWSER, BuiltInTool.BROWSER_GET_DATETIME); + mockTurns((options) => { + options.onChunk?.('par'); + + throw new Error('HTTP 500'); + }); + + const { callbacks, named, names } = recorder(); + const result = await runFlow(callbacks); + + expect(result).toMatchObject({ error: { message: 'HTTP 500' }, handled: true }); + expect(names()).toEqual(['onChunk', 'onAssistantTurnComplete', 'onFlowComplete', 'onError']); + expect(named('onAssistantTurnComplete')[0].slice(1)).toEqual([ + 'par', + undefined, + undefined, + undefined + ]); + expect(agenticStore.getLastError(CONV_ID)?.message).toBe('HTTP 500'); + }); + + it('stops without a tool result when the user aborts during a tool call', async () => { + const controller = new AbortController(); + + mockTools(ToolSource.SERVER, 'slow'); + vi.spyOn(ToolsService, 'executeTool').mockImplementation(async () => { + controller.abort(); + + return { content: 'late', isError: false }; + }); + + const requests = mockTurns((options) => options.onToolCallChunk?.(toolCalls('slow'))); + const { callbacks, named, names } = recorder(); + + expect(await runFlow(callbacks, controller.signal)).toEqual({ handled: true }); + expect(named('createToolResultMessage')).toEqual([]); + expect(names().slice(-1)).toEqual(['onFlowComplete']); + expect(requests).toHaveLength(1); + }); +}); diff --git a/ui/apps/chat/tests/unit/agentic-hotpath.bench.ts b/ui/apps/chat/tests/unit/agentic-hotpath.bench.ts index c46f3fe4bf68..0da763bd3d76 100644 --- a/ui/apps/chat/tests/unit/agentic-hotpath.bench.ts +++ b/ui/apps/chat/tests/unit/agentic-hotpath.bench.ts @@ -6,6 +6,7 @@ // // Run: npx vitest bench --project=unit tests/unit/agentic-hotpath.bench.ts +import { parsePartialJsonArgs } from '@ggml-org/llama-harness'; import { classifyToolResult, parseToolResultWithMedia } from '#lib/utils/agentic.ts'; import { detectIncompleteCodeBlock, @@ -14,7 +15,6 @@ import { } from '#lib/utils/code.ts'; import { computeLineDiff } from '#lib/utils/compute-line-diff.ts'; import { preprocessLaTeX } from '#lib/utils/latex-protection.ts'; -import { parsePartialJsonArgs } from '#lib/utils/parse-partial-json-args.ts'; import { extractSearchQuery, extractSearchResults } from '#lib/utils/search-results.ts'; import { all as lowlightAll } from 'lowlight'; import rehypeHighlight from 'rehype-highlight'; diff --git a/ui/apps/chat/tests/unit/async-queue.test.ts b/ui/apps/chat/tests/unit/async-queue.test.ts new file mode 100644 index 000000000000..4856f74d3ba6 --- /dev/null +++ b/ui/apps/chat/tests/unit/async-queue.test.ts @@ -0,0 +1,42 @@ +import { createAsyncQueue } from '#lib/utils/async-queue.ts'; +import { describe, expect, it } from 'vitest'; + +async function collect(queue: AsyncIterable): Promise { + const items: T[] = []; + + for await (const item of queue) items.push(item); + + return items; +} + +describe('createAsyncQueue', () => { + it('yields the items pushed before and during the iteration, then ends on close', async () => { + const queue = createAsyncQueue(); + + queue.push(1); + + const collected = collect(queue); + + await Promise.resolve(); + queue.push(2); + queue.push(3); + queue.close(); + + expect(await collected).toEqual([1, 2, 3]); + }); + + it('throws the failure after the pushed items are read', async () => { + const queue = createAsyncQueue(); + const seen: number[] = []; + + queue.push(1); + queue.fail(new Error('cut')); + + await expect( + (async () => { + for await (const item of queue) seen.push(item); + })() + ).rejects.toThrow('cut'); + expect(seen).toEqual([1]); + }); +}); diff --git a/ui/apps/chat/tests/unit/tool-call-phrasing.test.ts b/ui/apps/chat/tests/unit/tool-call-phrasing.test.ts index 1433d32250cd..459ff227f4dd 100644 --- a/ui/apps/chat/tests/unit/tool-call-phrasing.test.ts +++ b/ui/apps/chat/tests/unit/tool-call-phrasing.test.ts @@ -1,10 +1,12 @@ import { - describeToolActivity, - describeToolCall, formatToolCallLine, groupToolCallRuns, humanizeArgKey, - simpleToolArgFields, + simpleToolArgFields +} from '@ggml-org/llama-harness'; +import { + describeToolActivity, + describeToolCall, summarizeToolCalls } from '#lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/tool-call-phrasing.ts'; import { AgenticSectionType, BuiltInTool } from '#lib/enums/index.ts'; diff --git a/ui/apps/chat/tsconfig.json b/ui/apps/chat/tsconfig.json index 74af0cce0360..fed5bbe9d198 100644 --- a/ui/apps/chat/tsconfig.json +++ b/ui/apps/chat/tsconfig.json @@ -47,7 +47,8 @@ "tw-animate-css/*": ["./node_modules/tw-animate-css/*"], "vaul-svelte": ["./node_modules/vaul-svelte"], "vaul-svelte/*": ["./node_modules/vaul-svelte/*"], - "@lucide/svelte/icons/*": ["./node_modules/@lucide/svelte/dist/icons/*"] + "@lucide/svelte/icons/*": ["./node_modules/@lucide/svelte/dist/icons/*"], + "@ggml-org/llama-harness": ["../../lib/harness/src/index.ts"] }, "sourceMap": true, "strict": true diff --git a/ui/apps/chat/vite.config.ts b/ui/apps/chat/vite.config.ts index fb9c666a9577..4608938f35f0 100644 --- a/ui/apps/chat/vite.config.ts +++ b/ui/apps/chat/vite.config.ts @@ -13,10 +13,12 @@ import { playwright } from '@vitest/browser-playwright'; import { mdsvex } from 'mdsvex'; import { dirname, resolve } from 'path'; import { fileURLToPath } from 'url'; -import { defineConfig, loadEnv, searchForWorkspaceRoot } from 'vite'; +import { defineConfig, loadEnv, type ProxyOptions, searchForWorkspaceRoot } from 'vite'; const __dirname = dirname(fileURLToPath(import.meta.url)); const LLAMA_VOICE_SOURCE = resolve(__dirname, '../../lib/voice/src'); +// the harness has no build step in the repo: the app compiles it from source +const LLAMA_HARNESS_ENTRY = resolve(__dirname, '../../lib/harness/src/index.ts'); // eslint-disable-next-line @typescript-eslint/no-explicit-any const browserBaseConfig: any = { enabled: true, @@ -36,6 +38,18 @@ const outDir = process.env.LLAMA_UI_OUT_DIR ?? './dist'; export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd(), 'VITE_PUBLIC_'); const SERVER_ORIGIN = env.VITE_PUBLIC_SERVER_ORIGIN || 'http://localhost:9931'; + // llama-server takes cross-origin browser requests only from --cors-origins, so the proxy + // calls it as its own page: the Host of the server and no Origin. Sec-Fetch-Site still + // passes through, so the server keeps refusing requests from other sites. + const toServer = (ws = false): ProxyOptions => ({ + changeOrigin: true, + configure: (proxy) => { + proxy.on('proxyReq', (req) => req.removeHeader('origin')); + proxy.on('proxyReqWs', (req) => req.removeHeader('origin')); + }, + target: SERVER_ORIGIN, + ws + }); return { build: { @@ -85,6 +99,7 @@ export default defineConfig(({ mode }) => { replacement: resolve(LLAMA_VOICE_SOURCE, 'svelte/index.ts') }, { find: /^@ggml-org\/llama-voice$/, replacement: resolve(LLAMA_VOICE_SOURCE, 'index.ts') }, + { find: /^@ggml-org\/llama-harness$/, replacement: LLAMA_HARNESS_ENTRY }, { find: 'katex-fonts', replacement: resolve('node_modules/katex/dist/fonts') } ] }, @@ -94,7 +109,8 @@ export default defineConfig(({ mode }) => { allow: [ searchForWorkspaceRoot(process.cwd()), resolve(__dirname, 'tests'), - LLAMA_VOICE_SOURCE + LLAMA_VOICE_SOURCE, + resolve(__dirname, '../../lib/harness') ] }, headers: { @@ -102,13 +118,13 @@ export default defineConfig(({ mode }) => { 'Cross-Origin-Opener-Policy': 'same-origin' }, proxy: { - '/cors-proxy': SERVER_ORIGIN, - '/models': SERVER_ORIGIN, - '/props': SERVER_ORIGIN, - '/slots': SERVER_ORIGIN, - '/tools': SERVER_ORIGIN, + '/cors-proxy': toServer(), + '/models': toServer(), + '/props': toServer(), + '/slots': toServer(), + '/tools': toServer(), // ws: realtime transcription (/v1/realtime) is a WebSocket - '/v1': { target: SERVER_ORIGIN, ws: true } + '/v1': toServer(true) } }, diff --git a/ui/lib/harness/.gitignore b/ui/lib/harness/.gitignore new file mode 100644 index 000000000000..16339ab2e87f --- /dev/null +++ b/ui/lib/harness/.gitignore @@ -0,0 +1,6 @@ +node_modules +/dist + +# OS +.DS_Store +Thumbs.db diff --git a/ui/lib/harness/.npmrc b/ui/lib/harness/.npmrc new file mode 100644 index 000000000000..b6f27f135954 --- /dev/null +++ b/ui/lib/harness/.npmrc @@ -0,0 +1 @@ +engine-strict=true diff --git a/ui/lib/harness/LICENSE b/ui/lib/harness/LICENSE new file mode 100644 index 000000000000..50c59c51c158 --- /dev/null +++ b/ui/lib/harness/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) ggml + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/ui/lib/harness/README.md b/ui/lib/harness/README.md new file mode 100644 index 000000000000..140a06d9e58b --- /dev/null +++ b/ui/lib/harness/README.md @@ -0,0 +1,83 @@ +# @ggml-org/llama-harness + +Llama Harness, the agentic loop of Llama Chat, as a package: it streams a turn, runs the tools the model called, gives it the results, and repeats until the model answers without a tool call. It is headless TypeScript with no UI, no DOM and no dependencies, so the same loop runs in a browser, in Node and in a worker. It lives in llama.cpp at `ui/lib/harness`. + +It talks to llama-server's chat completions endpoint (`/v1/chat/completions`) and reads the timings and prompt progress llama-server adds. + +## Use + +```ts +import { createHarness, createOpenAIClient, type Tool } from "@ggml-org/llama-harness"; + +const harness = createHarness(createOpenAIClient({ baseUrl: "http://localhost:8080" })); + +const getTime: Tool = { + definition: { + type: "function", + function: { + name: "get_time", + description: "The current time", + parameters: { type: "object", properties: {} }, + }, + }, + execute: async () => ({ content: new Date().toISOString() }), +}; + +for await (const event of harness.run([{ role: "user", content: "What time is it?" }], [getTime])) { + if (event.type === "text") process.stdout.write(event.delta); +} +``` + +## The contract + +```ts +interface Harness { + run(messages, tools, signal?, options?): AsyncIterable; +} +``` + +- **Messages and tools** use the OpenAI shapes. A tool is its definition plus an `execute` function. +- **Events:** `turn_start`, `text`, `reasoning`, `tool_call_delta`, `timings`, `model`, `completion_id`, `message` (a turn's assistant message), `tool_call`, `tool_update` (a streaming tool's output so far), `tool_result`, `turn_end`, and last `done` or `error`. `run` never throws. +- **The loop waits for you.** It does not go on until you ask for the next event, so you can store each message before the next step runs. +- **It keeps nothing.** No conversation state, no storage: the app owns the history and passes it in. +- **Stopping:** `signal` stops at once. `options.interrupted` ends the run at the next safe point, e.g. when the user sent a new message. `options.maxTurns` with `options.onTurnLimit` asks before it goes past a turn limit. + +An app can put another loop behind the same `Harness` interface. What sits above the loop is the same for every loop: the tools, and whatever an app wraps around `execute`, such as a permission prompt. + +## Traces + +A run's steps also read as an agent trace: what each tool call did, in plain words. That part is headless too, so any app on the harness can show the same lines. + +```ts +import { createTracePhraser, formatToolCallLine, groupToolCallRuns } from "@ggml-org/llama-harness"; + +const { describeToolCall, summarizeToolCalls, describeToolActivity } = createTracePhraser(); + +formatToolCallLine(describeToolCall("read_file", '{"path":"src/app.ts"}')); // "Read app.ts" +summarizeToolCalls(steps); // "Ran 3 commands, read 2 files" +describeToolActivity(steps, true); // "Running npm test", or "Thinking" +``` + +- **Steps** are anything with a `type` from `TraceStepType` (text, reasoning, or a tool call that is done, pending or still streaming) plus the call's name and arguments. +- **Phrasing** knows llama-server's built-in tools. An app adds its own tools with `resolvers` (a function per tool or family of tools) and `phrasings` (a table by name); any other tool reads as "Used " with its main argument. A call whose arguments carry `description` shows the model's own words instead. +- **Streaming:** arguments are read while they stream (`parsePartialJsonArgs`), and arguments that hold whole files are read for their path only (`extractToolArgString`). +- **Grouping:** `groupToolCallRuns` folds the calls and the reasoning around them into one row, up to the next text. + +## What the app keeps + +Llama Chat (`ui/apps/chat`) is the first app on it. Its agentic store gives the harness: + +- **An inference client:** its own chat service, which knows the servers, the settings and how to resume a stream. +- **The tools:** built-in tools, llama-server tools and MCP tools, each behind the permission gate. +- **Storage:** it turns the events into messages in its database. + +## Develop + +```sh +npm ci +npm run check # type check +npm test # unit tests +npm run prepack # build dist/ and lint the package +``` + +Llama Chat does not need a build of this package: it compiles `src/` directly, through the `@ggml-org/llama-harness` alias in its `vite.config.ts`. diff --git a/ui/lib/harness/package-lock.json b/ui/lib/harness/package-lock.json new file mode 100644 index 000000000000..952987aff8df --- /dev/null +++ b/ui/lib/harness/package-lock.json @@ -0,0 +1,1255 @@ +{ + "name": "@ggml-org/llama-harness", + "version": "0.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "@ggml-org/llama-harness", + "version": "0.1.0", + "license": "MIT", + "devDependencies": { + "@types/node": "^24", + "publint": "^0.3.21", + "typescript": "^6.0.3", + "vitest": "5.0.5" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.6.0.tgz", + "integrity": "sha512-T7jf+5zgsZHwNJ4lvQ7/aezbyk0nNX+zJVWpmHA7VYsEx7a7qr5Rg5IbtJFqkgze5Y2sruq1RUY8Q837Od7iFw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.31", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.31.tgz", + "integrity": "sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.1.0", + "@jridgewell/sourcemap-codec": "^1.4.14" + } + }, + "node_modules/@oxc-project/types": { + "version": "0.156.0", + "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.156.0.tgz", + "integrity": "sha512-1Duyg48TdJNdyhEB8Xn1L36bT9Pv6GUtlKjHxOys8xkaF/rQq3XJE+bAurSK/AgIz5mGFsb77cn9Pb20+WCScw==", + "dev": true, + "license": "MIT", + "peer": true, + "funding": { + "url": "https://github.com/sponsors/oxc-project" + } + }, + "node_modules/@publint/pack": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/@publint/pack/-/pack-0.1.7.tgz", + "integrity": "sha512-4EDEmvxWtgsCnnVeBvtFIFZtUhPPt1+bA9JrSwU4Sa//6oKtzCSlGGXYJr44OD9aGISymbieJ4mCKHUygUDU+g==", + "dev": true, + "license": "MIT", + "dependencies": { + "tinyexec": "^1.3.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://bjornlu.com/sponsor" + } + }, + "node_modules/@rolldown/binding-android-arm-eabi": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm-eabi/-/binding-android-arm-eabi-1.2.18.tgz", + "integrity": "sha512-ubTG+4KlidvioxjOFdsdEa2F6JVGZb8m2ByJUqQ7fYzUeT1cEn194POcsQ10PKH4p//q0YQn4SB3YpSuzcA1Yg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-android-arm64": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm64/-/binding-android-arm64-1.2.18.tgz", + "integrity": "sha512-2Xa1t/JeFGTZn5TbzgPppcCftF8sCVnTNvrIvoSdcZhoFzAuVy1L3xxQUQ4sfWDtwVzg1u6Yi1R3SlJugRNV2Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-arm64": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-arm64/-/binding-darwin-arm64-1.2.18.tgz", + "integrity": "sha512-iUzw9HshVvrWci9gVXjxWS7/M/IcLvNo67vu2c/w9zKBz4hIvPRixdUpIW/iCfhldLaq9ER2hecsI9UY5z0/yA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-x64": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-x64/-/binding-darwin-x64-1.2.18.tgz", + "integrity": "sha512-XuDHFzbFEtZl0QGJi92+fmG67tfnp1iZ9VOeDkwasGyTq2VG3/5PnJnvAr+MTzFh/rNScJ3VhN+XpZyyGsN7Jw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-freebsd-x64": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-freebsd-x64/-/binding-freebsd-x64-1.2.18.tgz", + "integrity": "sha512-MM8opLfsLK15oEXDz6iBc0t3ahu+ew9jsAmha+ovZ33hSwYgqo7YmpRITqFTfW7xA71BLEQ3jJdTsaCmhB1c9w==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm-gnueabihf": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-1.2.18.tgz", + "integrity": "sha512-J6EoCWiy5RnFQzHrKyHXXevZ9lukQfGpnchX/zG/k/WC2nc9O9XyS1JgA8kI9VxMFggMbCPjpv7LcikriXfehw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-gnu": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.2.18.tgz", + "integrity": "sha512-HIkFuLF7CdDeki+1aCZLLbLq0hk/PHntb+z4/YCTo47JbksTD6jJEELIz92tYROmDmvMrNqUMqBi71BkJLDR+A==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-musl": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.2.18.tgz", + "integrity": "sha512-4oqWItLLYt7IceOnElCct8T2ek5p7PmD7LN04TRJRXGo5f3HtKTgGH6PhYRFK4C2ICkq/LMUU0BVPvvlAKhm8g==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-ppc64-gnu": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-ppc64-gnu/-/binding-linux-ppc64-gnu-1.2.18.tgz", + "integrity": "sha512-+AkRly5SrCEgnPhGRfub7pucASYEC2fTsxZg7VCcnozpA2+Wn/gCYy1t7LuJS67/xyJu/7noYBJncDUkbwgw2A==", + "cpu": [ + "ppc64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-s390x-gnu": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-s390x-gnu/-/binding-linux-s390x-gnu-1.2.18.tgz", + "integrity": "sha512-Ttg1r9vVykMnQrE/GwYO9icKtXiuiMfRe/w74xJkeoVuvhRG+WtT6LEMOf58Yl63yd77lhHzO8Y55R8CC3XALA==", + "cpu": [ + "s390x" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-gnu": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.2.18.tgz", + "integrity": "sha512-xX1Tt4o5jUMma0vJsXp5eRZA9nOuyigb2dKJMMzZRmzMxLYGE9PaKKSqHPzPvSQgM8ZOEacSTQwDQcwWfh0xmQ==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-musl": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-musl/-/binding-linux-x64-musl-1.2.18.tgz", + "integrity": "sha512-4VVlGI5K2kociZlW3lSN8VjG3TW5yQIlAcfCdBXMgGjJxZll15Fa4ocRHXHvIVVnwlibCw/NQajJrNDiqqpiiw==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-openharmony-arm64": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-openharmony-arm64/-/binding-openharmony-arm64-1.2.18.tgz", + "integrity": "sha512-PApTsgH1jLYuyy3tSCz9De82Gq5qXwi7vImVZhEzTgX1C4150Cp5M2ZfL3pLvbCvbEHL0oJeC6pd6podtv8Y5w==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-win32-arm64-msvc": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.2.18.tgz", + "integrity": "sha512-J2XjWjx/SP+4x0H4r6dz3Vzb5WVkg7re40Do9K7HJ3sK2/xcmZrWwakXlSwaMvOkvruLJCD4CL/3wqsIR/thgg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-win32-x64-msvc": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.2.18.tgz", + "integrity": "sha512-XwRJxRQvkuEtS0hIIt29QrqPJ+JzMJSNYHJw4ae/mc6uEarfRG1ceyg4HmmWiBdL/YCb6g7N5BLbSvmRJRYZHw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/pluginutils": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.1.0.tgz", + "integrity": "sha512-K+NU2HDCEem4sClgHH4ZZXwdJeNFkJo+hRk479DIwdIviAPN2hFJH1KHHwg8CxHQXvi0M5XY4deTXINfIiSG6w==", + "dev": true, + "license": "MIT", + "peer": true + }, + "node_modules/@types/chai": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/@types/chai/-/chai-5.2.3.tgz", + "integrity": "sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/deep-eql": "*", + "assertion-error": "^2.0.1" + } + }, + "node_modules/@types/deep-eql": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/deep-eql/-/deep-eql-4.0.2.tgz", + "integrity": "sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/estree": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.10.tgz", + "integrity": "sha512-lTyOMnOcmRBzdCv6/4zUZCrEhV0xrqQiUZBZtVcbmLHBXKLnN8q89v/wKRF/Q0HbwVsqbIG3o5HDoFl9sduM+g==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/node": { + "version": "24.19.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-24.19.2.tgz", + "integrity": "sha512-93d49wCWJkCyN87MxLpNN6KXPCwFw7bVUg79mGUV1OS40JGWwVCr8n61DXCSqz7CV6/VfPkb7xec7uP2M+QHWQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": ">=7.24.0 <7.24.7" + } + }, + "node_modules/@vitest/mocker": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-5.0.5.tgz", + "integrity": "sha512-xLL3lkDegJZw+MGoGaCCsFayDZthGgpAtr3Vziyfc4p7xH89K7KRPcRBBh/kEYm0Aqkt5DtTmkYpBtRV/FrHZg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/trace-mapping": "0.3.31", + "@vitest/spy": "5.0.5", + "estree-walker": "^3.0.3", + "magic-string": "^1.4.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "msw": "^2.4.9 || ^3.0.0", + "vite": "^6.0.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "msw": { + "optional": true + }, + "vite": { + "optional": true + } + } + }, + "node_modules/@vitest/spy": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-5.0.5.tgz", + "integrity": "sha512-lwJRExuH417257Q5wv9QYugN9IMJkoxcrKrYxGPQkBzl7wlNpTUxD/5yNX4ESKVUIShKPgkGchYPFMWOOk0wzw==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/assertion-error": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz", + "integrity": "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + } + }, + "node_modules/chai": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-6.3.0.tgz", + "integrity": "sha512-XWAtwJ6OHO+tj0EKCs0Y2UamnyOxseZWltU4x2U2wh8g4AigdjwvtUjvLP2tqkA/avxHEtzxNaqGq/YGNwckKg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "dev": true, + "license": "Apache-2.0", + "peer": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/es-module-lexer": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-2.3.2.tgz", + "integrity": "sha512-poHGpORABojJJucnV9KbOavETW8lBVnphkW77ER5/BQ5Fz7oXSoCNek7IH3vR5nRjdsEz926ibFYX8KtLQmdyw==", + "dev": true, + "license": "MIT" + }, + "node_modules/estree-walker": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz", + "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "^1.0.0" + } + }, + "node_modules/expect-type": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz", + "integrity": "sha512-KfYbmpRm0VbLjEvVa9yGwCi9GI34xvi7A/HXYWQO65CSD2u3MczUJSuwXKFIxlGsgBQizV9q5J9NHj4VG0n+pA==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/fdir": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz", + "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12.0.0" + }, + "peerDependencies": { + "picomatch": "^3 || ^4" + }, + "peerDependenciesMeta": { + "picomatch": { + "optional": true + } + } + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "peer": true, + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/lightningcss": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.33.0.tgz", + "integrity": "sha512-WkUDrojuJs0xkgGf2udWxa3yGBRxPtxUkB79i6aCZLRgc7PM8fZe9TosfPDcvEpQZbuFASnHYmRLBLUbmLOIIA==", + "dev": true, + "license": "MPL-2.0", + "peer": true, + "dependencies": { + "detect-libc": "^2.0.3" + }, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + }, + "optionalDependencies": { + "lightningcss-android-arm64": "1.33.0", + "lightningcss-darwin-arm64": "1.33.0", + "lightningcss-darwin-x64": "1.33.0", + "lightningcss-freebsd-x64": "1.33.0", + "lightningcss-linux-arm-gnueabihf": "1.33.0", + "lightningcss-linux-arm64-gnu": "1.33.0", + "lightningcss-linux-arm64-musl": "1.33.0", + "lightningcss-linux-x64-gnu": "1.33.0", + "lightningcss-linux-x64-musl": "1.33.0", + "lightningcss-win32-arm64-msvc": "1.33.0", + "lightningcss-win32-x64-msvc": "1.33.0" + } + }, + "node_modules/lightningcss-android-arm64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-android-arm64/-/lightningcss-android-arm64-1.33.0.tgz", + "integrity": "sha512-gEpRTalKdosp4Bb8qWtc2iOgE5SeIHlpS1up9bFq2wAyYhl1UdTObYiHe98zEM9SQvSoqQZ1IQD0JNpg3Ml5pg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "android" + ], + "peer": true, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-arm64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-arm64/-/lightningcss-darwin-arm64-1.33.0.tgz", + "integrity": "sha512-Sciaz8eenNTKn9b3t7+xr0ipTp9YxKQY4npwQ3mrRuL0BAVHBLyZxofhaKBAVtzmtRZ/zTyo0/to4B1uWG/Djg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "peer": true, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-x64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-x64/-/lightningcss-darwin-x64-1.33.0.tgz", + "integrity": "sha512-Z5UPAxzrjlWNNyGy6i65cJzzvgJ5D3T6wMvs+gWpY9d7qRhANrxqAp6LhxIgZhWEw18RfJTGcRxjuLIBr+m8XQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "peer": true, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-freebsd-x64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-freebsd-x64/-/lightningcss-freebsd-x64-1.33.0.tgz", + "integrity": "sha512-QQM/Ti/hQajJwCY+RiWuCZ9sdtI/XQk7nDK5vC8kkdwixezOlDgvDx7+RT+QjK6FcFT4MpsuoBnHIo/O3StRRg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "peer": true, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm-gnueabihf": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm-gnueabihf/-/lightningcss-linux-arm-gnueabihf-1.33.0.tgz", + "integrity": "sha512-N7FVBe6iS24MlM6R/4RBTxGhQheZGs7tiQ9U32UtF75NzP5Q7xWPRqLBCKxlRQRk3rY1jCIPLzx7WzOhuUIRLQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-gnu": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-gnu/-/lightningcss-linux-arm64-gnu-1.33.0.tgz", + "integrity": "sha512-j2v/itmy4HlNxlc6voKXYgBqNi0Ng2LShg4z7GufpEgs05P+2suBVyi9I6YHq5uoVFx9ETin3eCEhLVyXGQnKg==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-musl": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-musl/-/lightningcss-linux-arm64-musl-1.33.0.tgz", + "integrity": "sha512-yiO5ROMuYQgXbC60yjZU5CYSFZGKXL0HFATXt9mHJn1+zW55oCtMI9NfcVhYLMFDL7gV7oBPon/EmMMGg2OvtQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-gnu": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-gnu/-/lightningcss-linux-x64-gnu-1.33.0.tgz", + "integrity": "sha512-ar+Ju7LmcN0Jo4FpL4hpFybwNG9/3A/Br5KW2n2jyODg3MEZXaDYADdemoNS+BDNfMgKvylJLj4S5tyRActuAg==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-musl": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-musl/-/lightningcss-linux-x64-musl-1.33.0.tgz", + "integrity": "sha512-RYiYbkokw0trfKqqzfF55lginwEPrD3OJDfTuJzFs1MK6iFnDenaz1fqLLtX4ITG3OktJQXOeTaw1awrBAlZPw==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-arm64-msvc": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-arm64-msvc/-/lightningcss-win32-arm64-msvc-1.33.0.tgz", + "integrity": "sha512-1K+MPfLSFVpphzpdbfkhlWk6wBrTObBzS2T6db10PNOZgR9GoVsAWzwNyuhUYYbTp23j+4RrncfujZ4uAzXvwA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "peer": true, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-x64-msvc": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-x64-msvc/-/lightningcss-win32-x64-msvc-1.33.0.tgz", + "integrity": "sha512-OlEICDx/Xl0FqSp4bry8zFnCvGpig3Gl4gCquvYwHuqJKEC1+n9NgDniFvqHGmMv1ZkqDJrDqKKSykTDX+ehuA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "peer": true, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/magic-string": { + "version": "1.4.5", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-1.4.5.tgz", + "integrity": "sha512-oLLC2+YvQUfAa+vxoufE/yc1icb87M9iXIRqAoR/2EhE/8cb3SF/+Gi0c2By1B+5Vz2e2vQtUvHiu1+63Znh+w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.6.0" + } + }, + "node_modules/mri": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/mri/-/mri-1.2.0.tgz", + "integrity": "sha512-tzzskb3bG8LvYGFF/mDTpq3jpI6Q9wc3LEmBaghu+DdCssd1FakN7Bc0hVNmEyGq1bq3RgfkCb3cmQLpNPOroA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=4" + } + }, + "node_modules/nanoid": { + "version": "3.3.20", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.20.tgz", + "integrity": "sha512-uKdg2G3GNCKQn9byYOpxbGqrT2fGO5KRt5J/8b3pok8rT6qxGWF6hxMyJiEYtAf+FVyYuD9hRaDqX5uPFYJ4ZQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "peer": true, + "bin": { + "nanoid": "bin/nanoid.cjs" + }, + "engines": { + "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" + } + }, + "node_modules/obug": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/obug/-/obug-3.0.0.tgz", + "integrity": "sha512-5vvB5+W7ePv+p3uqxi+RcW1XAzLW0/hxt3/4X4Lc4qHudzOhmBiBwOY6DRob4WnanAEGvNcLjF+KNOufrUoEQw==", + "dev": true, + "funding": [ + "https://github.com/sponsors/sxzz", + "https://opencollective.com/debug" + ], + "license": "MIT", + "engines": { + "node": ">=12.20.0" + } + }, + "node_modules/package-manager-detector": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/package-manager-detector/-/package-manager-detector-1.9.0.tgz", + "integrity": "sha512-zAMfbta9nPA8AKkA4WdmNAT8R8s6Dk0/xwPsqNnRidMFcLo1YkLuPDTgs2f64XdzS8vbLNnoJ+uUIkgbz7oRHA==", + "dev": true, + "license": "MIT" + }, + "node_modules/picocolors": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", + "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", + "dev": true, + "license": "ISC" + }, + "node_modules/picomatch": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.7.tgz", + "integrity": "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/postcss": { + "version": "8.5.32", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.32.tgz", + "integrity": "sha512-LJlKCCAUFKOuZxipUdYw1NzIITEsPcgyOG9LkvRJvuKj/bRzbuRY0ARXvL0pWGpsZvn8jku+LMep2eU0seHp8g==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/postcss" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "peer": true, + "dependencies": { + "nanoid": "^3.3.20", + "picocolors": "^1.1.1", + "source-map-js": "^1.2.2" + }, + "engines": { + "node": "^10 || ^12 || >=14" + } + }, + "node_modules/publint": { + "version": "0.3.25", + "resolved": "https://registry.npmjs.org/publint/-/publint-0.3.25.tgz", + "integrity": "sha512-WMMbn/d7JqRlfXQL/JUI4n5skC8cJ5WVr8/wT5mmiYWkUdENkZr1I5rgs2+dQPVRgoJd+JRYtB46MTsuDylw9g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@publint/pack": "^0.1.7", + "package-manager-detector": "^1.8.0", + "picocolors": "^1.1.1", + "sade": "^1.8.1" + }, + "bin": { + "publint": "src/cli.js" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://bjornlu.com/sponsor" + } + }, + "node_modules/rolldown": { + "version": "1.2.18", + "resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.2.18.tgz", + "integrity": "sha512-C1uuT3UR60SShuCaPQFAAZyefJCWZlAKB02BV3BuPE2Rs4fevRQltEKoFO2tV5W9/eJhU5RieV6vJj3euulfNQ==", + "dev": true, + "license": "MIT", + "peer": true, + "dependencies": { + "@oxc-project/types": "=0.156.0", + "@rolldown/pluginutils": "^1.0.0" + }, + "bin": { + "rolldown": "bin/cli.mjs" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "optionalDependencies": { + "@rolldown/binding-android-arm-eabi": "1.2.18", + "@rolldown/binding-android-arm64": "1.2.18", + "@rolldown/binding-darwin-arm64": "1.2.18", + "@rolldown/binding-darwin-x64": "1.2.18", + "@rolldown/binding-freebsd-x64": "1.2.18", + "@rolldown/binding-linux-arm-gnueabihf": "1.2.18", + "@rolldown/binding-linux-arm64-gnu": "1.2.18", + "@rolldown/binding-linux-arm64-musl": "1.2.18", + "@rolldown/binding-linux-ppc64-gnu": "1.2.18", + "@rolldown/binding-linux-s390x-gnu": "1.2.18", + "@rolldown/binding-linux-x64-gnu": "1.2.18", + "@rolldown/binding-linux-x64-musl": "1.2.18", + "@rolldown/binding-openharmony-arm64": "1.2.18", + "@rolldown/binding-win32-arm64-msvc": "1.2.18", + "@rolldown/binding-win32-x64-msvc": "1.2.18" + } + }, + "node_modules/sade": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/sade/-/sade-1.8.1.tgz", + "integrity": "sha512-xal3CZX1Xlo/k4ApwCFrHVACi9fBqJ7V+mwhBsuf/1IOKbBy098Fex+Wa/5QMubw09pSZ/u8EY8PWgevJsXp1A==", + "dev": true, + "license": "MIT", + "dependencies": { + "mri": "^1.1.0" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/source-map-js": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.2.tgz", + "integrity": "sha512-KGj/8Y43x35aZVDtt+J4mK1hoLGHULMYfSkODJNQjNDC3oW1PqPoxMwo0pLUsWM/UEGzON/NxeHywEfNXNP3Vw==", + "dev": true, + "license": "BSD-3-Clause", + "peer": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/std-env": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/std-env/-/std-env-4.3.0.tgz", + "integrity": "sha512-OtU/EgQ1kIm5KwqQpBC6ZEMXrZRui11w8zgfTWp8cdO9B8OaPsbA8bTHO2P+HNo1VlUTGMVBwPhydu6poeXiag==", + "dev": true, + "license": "MIT" + }, + "node_modules/tinybench": { + "version": "6.2.3", + "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-6.2.3.tgz", + "integrity": "sha512-Al01C2JIBnO2CXCAPbpAl6sUiOZHeThGtHYNTa51JRw9PyWOCdr+9Ong05R4qg/Sk3+H645E8ov1h2c58ityNw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/tinyexec": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.3.1.tgz", + "integrity": "sha512-GCvB3aoys96IuDFBMcTB46JOR6mdMtAToqwiW8JlWhsoh1mhHi/xn9ss/Dg7N555GiJyEt2qzoG/NHCwM6h1EA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/tinyglobby": { + "version": "0.2.17", + "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.17.tgz", + "integrity": "sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g==", + "dev": true, + "license": "MIT", + "dependencies": { + "fdir": "^6.5.0", + "picomatch": "^4.0.4" + }, + "engines": { + "node": ">=12.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/SuperchupuDev" + } + }, + "node_modules/typescript": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-6.0.3.tgz", + "integrity": "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/undici-types": { + "version": "7.24.6", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.24.6.tgz", + "integrity": "sha512-WRNW+sJgj5OBN4/0JpHFqtqzhpbnV0GuB+OozA9gCL7a993SmU+1JBZCzLNxYsbMfIeDL+lTsphD5jN5N+n0zg==", + "dev": true, + "license": "MIT" + }, + "node_modules/vite": { + "version": "8.3.7", + "resolved": "https://registry.npmjs.org/vite/-/vite-8.3.7.tgz", + "integrity": "sha512-oxh0dMxrayBr33ScJvvBJ2HBQ2jc1TBrXKR2NyL/MJ9WPuX19mN2DXWUmywKdyw5RFVQBo8hmtb2FMC0LHrnng==", + "dev": true, + "license": "MIT", + "peer": true, + "dependencies": { + "lightningcss": "^1.33.0", + "picomatch": "^4.0.7", + "postcss": "^8.5.32", + "rolldown": "~1.2.18", + "tinyglobby": "^0.2.17" + }, + "bin": { + "vite": "bin/vite.js" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "funding": { + "url": "https://github.com/vitejs/vite?sponsor=1" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + }, + "peerDependencies": { + "@types/node": "^20.19.0 || >=22.12.0", + "@vitejs/devtools": "^0.7.1 || ^0.8.0", + "esbuild": "^0.27.0 || ^0.28.0", + "jiti": ">=1.21.0", + "less": "^4.0.0", + "sass": "^1.70.0", + "sass-embedded": "^1.70.0", + "stylus": ">=0.54.8", + "sugarss": "^5.0.0", + "terser": "^5.16.0", + "tsx": "^4.8.1", + "yaml": "^2.4.2" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + }, + "@vitejs/devtools": { + "optional": true + }, + "esbuild": { + "optional": true + }, + "jiti": { + "optional": true + }, + "less": { + "optional": true + }, + "sass": { + "optional": true + }, + "sass-embedded": { + "optional": true + }, + "stylus": { + "optional": true + }, + "sugarss": { + "optional": true + }, + "terser": { + "optional": true + }, + "tsx": { + "optional": true + }, + "yaml": { + "optional": true + } + } + }, + "node_modules/vitest": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-5.0.5.tgz", + "integrity": "sha512-cPODOMF8YnkiiYtDdHo6n4wWmBrOBBbOIYH2d33M4l3CdNn72h+qMmO6uXffUN6A5AZFXNND/d+krS9mgkXRfg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/chai": "^5.2.2", + "@vitest/mocker": "5.0.5", + "chai": "^6.3.0", + "es-module-lexer": "^2.3.2", + "expect-type": "^1.4.0", + "magic-string": "^1.4.3", + "obug": "^3.0.0", + "picomatch": "^4.0.7", + "std-env": "^4.3.0", + "tinybench": "^6.2.2", + "tinyexec": "^1.3.1", + "tinyglobby": "^0.2.17", + "why-is-node-running": "3.2.1" + }, + "bin": { + "vitest": "vitest.mjs" + }, + "engines": { + "node": "^22.12.0 || ^24.0.0 || >=26.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@edge-runtime/vm": "*", + "@opentelemetry/api": "^1.9.0", + "@types/node": "^22.0.0 || >=24.0.0", + "@vitest/browser-playwright": "5.0.5", + "@vitest/browser-preview": "5.0.5", + "@vitest/browser-webdriverio": "^5.0.0-beta.5 || >=5.0.0", + "@vitest/coverage-istanbul": "5.0.5", + "@vitest/coverage-v8": "5.0.5", + "@vitest/ui": "5.0.5", + "happy-dom": "*", + "jsdom": "*", + "vite": "^6.4.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "@edge-runtime/vm": { + "optional": true + }, + "@opentelemetry/api": { + "optional": true + }, + "@types/node": { + "optional": true + }, + "@vitest/browser-playwright": { + "optional": true + }, + "@vitest/browser-preview": { + "optional": true + }, + "@vitest/browser-webdriverio": { + "optional": true + }, + "@vitest/coverage-istanbul": { + "optional": true + }, + "@vitest/coverage-v8": { + "optional": true + }, + "@vitest/ui": { + "optional": true + }, + "happy-dom": { + "optional": true + }, + "jsdom": { + "optional": true + }, + "vite": { + "optional": false + } + } + }, + "node_modules/why-is-node-running": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-3.2.1.tgz", + "integrity": "sha512-Tb2FUhB4vUsGQlfSquQLYkApkuPAFQXGFzxWKHHumVz2dK+X1RUm/HnID4+TfIGYJ1kTcwOaCk/buYCEJr6YjQ==", + "dev": true, + "license": "MIT", + "bin": { + "why-is-node-running": "cli.js" + }, + "engines": { + "node": ">=20.11" + } + } + } +} diff --git a/ui/lib/harness/package.json b/ui/lib/harness/package.json new file mode 100644 index 000000000000..b93deffb207d --- /dev/null +++ b/ui/lib/harness/package.json @@ -0,0 +1,39 @@ +{ + "name": "@ggml-org/llama-harness", + "version": "0.1.0", + "description": "Headless agentic loop for llama.cpp frontend apps.", + "homepage": "https://github.com/ggml-org/llama.cpp/tree/master/ui/lib/harness", + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/ggml-org/llama.cpp.git", + "directory": "ui/lib/harness" + }, + "files": [ + "dist" + ], + "type": "module", + "sideEffects": false, + "types": "./dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + } + }, + "scripts": { + "build": "tsc -p tsconfig.build.json", + "check": "tsc --noEmit", + "prepack": "npm run build && publint", + "test": "vitest --run" + }, + "devDependencies": { + "@types/node": "^24", + "publint": "^0.3.21", + "typescript": "^6.0.3", + "vitest": "5.0.5" + }, + "engines": { + "node": ">=20" + } +} diff --git a/ui/lib/harness/src/abort.ts b/ui/lib/harness/src/abort.ts new file mode 100644 index 000000000000..636dd21ed718 --- /dev/null +++ b/ui/lib/harness/src/abort.ts @@ -0,0 +1,17 @@ +const ABORT_ERROR_NAME = "AbortError"; +// TypeErrors a browser throws when a fetch stream is cut by page unload, navigation or a network drop +const ABORT_LIKE_MESSAGE_PATTERNS = [ + /input stream/i, + /network connection was lost/i, + /load failed/i, +]; + +export function isAbortError(error: unknown): boolean { + if (!(error instanceof Error)) return false; + + if (error.name === ABORT_ERROR_NAME) return true; + + return ( + error instanceof TypeError && ABORT_LIKE_MESSAGE_PATTERNS.some((re) => re.test(error.message)) + ); +} diff --git a/ui/lib/harness/src/harness.ts b/ui/lib/harness/src/harness.ts new file mode 100644 index 000000000000..e58e7ea25188 --- /dev/null +++ b/ui/lib/harness/src/harness.ts @@ -0,0 +1,375 @@ +import { isAbortError } from "./abort.js"; +import type { + DoneReason, + Harness, + HarnessEvent, + InferenceClient, + LlmTimings, + LlmTotals, + Message, + PartialToolCall, + RunOptions, + RunTimings, + Timings, + Tool, + ToolCall, + ToolResult, + TurnStats, +} from "./types.js"; + +/** The result of a tool call that did not run because the run was interrupted. */ +export const INTERRUPTED_TOOL_RESULT = "Tool execution was interrupted by a new user message."; + +function emptyTotals(): LlmTotals { + return { predicted_ms: 0, predicted_n: 0, prompt_ms: 0, prompt_n: 0 }; +} + +function addTimings(totals: LlmTotals, timings: LlmTimings): LlmTotals { + return { + predicted_ms: totals.predicted_ms + (timings.predicted_ms ?? 0), + predicted_n: totals.predicted_n + (timings.predicted_n ?? 0), + prompt_ms: totals.prompt_ms + (timings.prompt_ms ?? 0), + prompt_n: totals.prompt_n + (timings.prompt_n ?? 0), + }; +} + +function toToolCalls(partial: PartialToolCall[]): ToolCall[] { + return partial.map((call, index) => ({ + function: { + arguments: call?.function?.arguments ?? "", + name: call?.function?.name ?? "", + }, + id: call?.id ?? `tool_${index}`, + type: "function", + })); +} + +function errorMessage(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} + +/** + * The default loop: stream a turn, run the tools the model called, give it the results, and repeat until it answers + * without a tool call. It keeps no state between runs and stores nothing: the app owns the conversation. + */ +export function createHarness(client: InferenceClient): Harness { + return { + run: (messages, tools, signal, options) => run(client, messages, tools, signal, options), + }; +} + +async function* run( + client: InferenceClient, + messages: Message[], + tools: Tool[], + signal?: AbortSignal, + options: RunOptions = {}, +): AsyncGenerator { + const { interrupted = () => false, maxTurns = Infinity, onTurnLimit } = options; + const history = [...messages]; + const definitions = tools.map((tool) => tool.definition); + const toolsByName = new Map(tools.map((tool) => [tool.definition.function.name, tool])); + const totals: RunTimings = { + llm: emptyTotals(), + perTurn: [], + toolCalls: [], + toolCallsCount: 0, + toolsMs: 0, + turns: 0, + }; + + let lastTimings: LlmTimings | undefined; + let turn = 0; + + const runTimings = (): Timings | undefined => { + if (totals.toolCallsCount === 0) return lastTimings; + + return { + agentic: totals, + cache_n: lastTimings?.cache_n, + predicted_ms: lastTimings?.predicted_ms, + predicted_n: lastTimings?.predicted_n, + prompt_ms: lastTimings?.prompt_ms, + prompt_n: lastTimings?.prompt_n, + }; + }; + const done = (reason: DoneReason): HarnessEvent => ({ + reason, + timings: runTimings(), + type: "done", + }); + + while (true) { + if (turn >= maxTurns) { + const more = onTurnLimit ? await onTurnLimit(signal) : false; + + if (!more || signal?.aborted) { + yield done(signal?.aborted ? "aborted" : "turn_limit"); + + return; + } + + turn = 0; + } + + totals.turns = turn + 1; + + if (signal?.aborted) { + yield done("aborted"); + + return; + } + + yield { turn: turn + 1, type: "turn_start" }; + + let content = ""; + let reasoning = ""; + let partialCalls: PartialToolCall[] = []; + let turnTimings: LlmTimings | undefined; + + const stats: TurnStats = { llm: emptyTotals(), toolCalls: [], toolsMs: 0, turn: turn + 1 }; + // the turn's message when the run ends after it: what streamed so far, with the run's timings + const lastMessage = (toolCalls?: ToolCall[]): HarnessEvent => ({ + content, + final: true, + reasoning: reasoning || undefined, + timings: runTimings(), + toolCalls, + type: "message", + }); + + try { + const stream = client.stream({ + messages: history, + signal, + tools: definitions.length > 0 ? definitions : undefined, + }); + + for await (const event of stream) { + if (event.type === "text") { + content += event.delta; + yield event; + } else if (event.type === "reasoning") { + reasoning += event.delta; + yield event; + } else if (event.type === "tool_calls") { + partialCalls = event.toolCalls; + yield { toolCalls: partialCalls, type: "tool_call_delta" }; + } else if (event.type === "timings") { + if (event.timings) { + lastTimings = event.timings; + turnTimings = event.timings; + } + + yield { ...event, llm: event.timings && addTimings(totals.llm, event.timings) }; + } else { + yield event; + } + } + + if (turnTimings) { + totals.llm = addTimings(totals.llm, turnTimings); + stats.llm = addTimings(stats.llm, turnTimings); + } + } catch (error) { + // keep the partial output in both cases + yield lastMessage(); + + if (signal?.aborted) { + yield done("aborted"); + } else { + yield { + error: error instanceof Error ? error : new Error("LLM stream error"), + timings: runTimings(), + type: "error", + }; + } + + return; + } + + // a client can swallow the abort and end the stream normally: do not take a cut tool call for a complete one + if (signal?.aborted) { + yield lastMessage(); + yield done("aborted"); + + return; + } + + if (interrupted()) { + yield lastMessage(partialCalls.length > 0 ? toToolCalls(partialCalls) : undefined); + yield done("interrupted"); + + return; + } + + if (partialCalls.length === 0) { + totals.perTurn.push(stats); + + const timings = runTimings(); + + yield lastMessage(); + + if (timings) yield { timings, type: "turn_end" }; + + yield done("complete"); + + return; + } + + const toolCalls = toToolCalls(partialCalls); + + yield { + content, + final: false, + reasoning: reasoning || undefined, + timings: turnTimings, + toolCalls, + type: "message", + }; + + history.push({ + content: content || undefined, + reasoning_content: reasoning || undefined, + role: "assistant", + tool_calls: toolCalls, + }); + + for (let i = 0; i < toolCalls.length; i++) { + const toolCall = toolCalls[i]; + + if (signal?.aborted) { + yield done("aborted"); + + return; + } + + if (interrupted()) { + for (const skippedCall of toolCalls.slice(i)) { + yield { + result: { content: INTERRUPTED_TOOL_RESULT }, + skipped: true, + toolCall: skippedCall, + type: "tool_result", + }; + + history.push({ + content: INTERRUPTED_TOOL_RESULT, + role: "tool", + tool_call_id: skippedCall.id, + }); + } + + break; + } + + yield { toolCall, type: "tool_call" }; + + const tool = toolsByName.get(toolCall.function.name); + const startTime = performance.now(); + + let result: ToolResult; + + if (!tool) { + result = { content: `Error: Unknown tool: ${toolCall.function.name}`, isError: true }; + } else { + // only the latest update matters, so a slow consumer skips the ones in between + let pendingUpdate: string | undefined; + let settled = false; + let wake: (() => void) | undefined; + + const notify = () => { + wake?.(); + wake = undefined; + }; + const execution = Promise.resolve() + .then(() => + tool.execute(toolCall, { + signal, + update: (updated) => { + pendingUpdate = updated; + notify(); + }, + }), + ) + .finally(() => { + settled = true; + notify(); + }); + + // the rejection is handled below, after the updates are drained + execution.catch(() => {}); + + while (true) { + if (pendingUpdate !== undefined) { + const updated = pendingUpdate; + + pendingUpdate = undefined; + yield { content: updated, toolCall, type: "tool_update" }; + } else if (settled) { + break; + } else { + await new Promise((resolve) => (wake = resolve)); + } + } + + try { + result = await execution; + } catch (error) { + if (isAbortError(error)) { + yield done("aborted"); + + return; + } + + result = { content: `Error: ${errorMessage(error)}`, isError: true }; + } + } + + const durationMs = Math.round(result.durationMs ?? performance.now() - startTime); + const timing = { + duration_ms: durationMs, + name: toolCall.function.name, + success: !result.isError, + }; + + totals.toolCalls.push(timing); + totals.toolCallsCount++; + totals.toolsMs += durationMs; + stats.toolCalls.push(timing); + stats.toolsMs += durationMs; + + if (signal?.aborted) { + yield done("aborted"); + + return; + } + + yield { result, skipped: false, toolCall, type: "tool_result" }; + + history.push({ + content: result.parts?.length + ? [{ text: result.content, type: "text" }, ...result.parts] + : result.content, + role: "tool", + tool_call_id: toolCall.id, + }); + } + + if (stats.toolCalls.length > 0) { + totals.perTurn.push(stats); + + const timings = runTimings(); + + if (timings) yield { timings, type: "turn_end" }; + } + + if (interrupted()) { + yield done("interrupted"); + + return; + } + + turn++; + } +} diff --git a/ui/lib/harness/src/index.ts b/ui/lib/harness/src/index.ts new file mode 100644 index 000000000000..1cbaf8aded43 --- /dev/null +++ b/ui/lib/harness/src/index.ts @@ -0,0 +1,5 @@ +export { isAbortError } from "./abort.js"; +export { createHarness, INTERRUPTED_TOOL_RESULT } from "./harness.js"; +export { createOpenAIClient, type OpenAIClientOptions } from "./openai-client.js"; +export type * from "./types.js"; +export * from "./trace/index.js"; diff --git a/ui/lib/harness/src/openai-client.ts b/ui/lib/harness/src/openai-client.ts new file mode 100644 index 000000000000..c1c45d9b840d --- /dev/null +++ b/ui/lib/harness/src/openai-client.ts @@ -0,0 +1,143 @@ +import type { InferenceClient, InferenceEvent, PartialToolCall } from "./types.js"; + +const CHAT_COMPLETIONS_PATH = "/v1/chat/completions"; +const SSE_DATA_PREFIX = "data:"; +const SSE_DONE = "[DONE]"; + +export interface OpenAIClientOptions { + /** Server root, e.g. `http://localhost:8080`. */ + baseUrl: string; + apiKey?: string; + model?: string; + /** Extra fields of the request body, e.g. `temperature`. */ + params?: Record; + headers?: Record; + /** Default: the global fetch. */ + fetch?: typeof fetch; +} + +interface ToolCallDelta extends PartialToolCall { + index?: number; +} + +function mergeToolCallDeltas(toolCalls: PartialToolCall[], deltas: ToolCallDelta[]): void { + for (const delta of deltas) { + // without an index, a delta with an id opens a call and any other one continues the last call + const index = + delta.index ?? (delta.id || toolCalls.length === 0 ? toolCalls.length : toolCalls.length - 1); + const call = (toolCalls[index] ??= { function: { arguments: "", name: "" } }); + + if (delta.id) call.id = delta.id; + + if (delta.type) call.type = delta.type; + + call.function = { + arguments: (call.function?.arguments ?? "") + (delta.function?.arguments ?? ""), + name: (call.function?.name ?? "") + (delta.function?.name ?? ""), + }; + } +} + +async function errorFromResponse(response: Response): Promise { + const body = await response.text().catch(() => ""); + + let message = body; + + try { + message = JSON.parse(body).error?.message ?? body; + } catch { + // not JSON: keep the body text + } + + return new Error(`HTTP ${response.status}${message ? `: ${message}` : ""}`); +} + +/** + * An inference client for llama-server's chat completions endpoint. It reads the `timings` and + * `prompt_progress` fields llama-server adds. + */ +export function createOpenAIClient(options: OpenAIClientOptions): InferenceClient { + const { apiKey, baseUrl, headers, model, params } = options; + const url = baseUrl.replace(/\/+$/, "") + CHAT_COMPLETIONS_PATH; + + return { + async *stream({ messages, signal, tools }): AsyncGenerator { + const response = await (options.fetch ?? fetch)(url, { + body: JSON.stringify({ ...params, messages, model, stream: true, tools }), + headers: { + "Content-Type": "application/json", + ...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}), + ...headers, + }, + method: "POST", + signal, + }); + + if (!response.ok) throw await errorFromResponse(response); + + if (!response.body) throw new Error("The response has no body"); + + const reader = response.body.getReader(); + const decoder = new TextDecoder(); + const toolCalls: PartialToolCall[] = []; + + let buffer = ""; + let completionId: string | undefined; + let modelName: string | undefined; + + try { + while (true) { + const { done, value } = await reader.read(); + + if (done) return; + + buffer += decoder.decode(value, { stream: true }); + + const lines = buffer.split("\n"); + + buffer = lines.pop() ?? ""; + + for (const line of lines) { + if (!line.startsWith(SSE_DATA_PREFIX)) continue; + + const data = line.slice(SSE_DATA_PREFIX.length).trim(); + + if (data === SSE_DONE) return; + + const chunk = JSON.parse(data); + + if (chunk.error) throw new Error(chunk.error.message ?? "Stream error"); + + if (chunk.id && chunk.id !== completionId) { + completionId = chunk.id; + yield { id: chunk.id, type: "completion_id" }; + } + + if (chunk.model && chunk.model !== modelName) { + modelName = chunk.model; + yield { model: chunk.model, type: "model" }; + } + + const delta = chunk.choices?.[0]?.delta; + const reasoning = delta?.reasoning_content ?? delta?.reasoning; + + if (reasoning) yield { delta: reasoning, type: "reasoning" }; + + if (delta?.content) yield { delta: delta.content, type: "text" }; + + if (delta?.tool_calls?.length) { + mergeToolCallDeltas(toolCalls, delta.tool_calls); + yield { toolCalls: toolCalls.map((call) => ({ ...call })), type: "tool_calls" }; + } + + if (chunk.timings || chunk.prompt_progress) { + yield { progress: chunk.prompt_progress, timings: chunk.timings, type: "timings" }; + } + } + } + } finally { + reader.cancel().catch(() => {}); + } + }, + }; +} diff --git a/ui/lib/harness/src/trace/args.ts b/ui/lib/harness/src/trace/args.ts new file mode 100644 index 000000000000..7349ee49579d --- /dev/null +++ b/ui/lib/harness/src/trace/args.ts @@ -0,0 +1,158 @@ +// Reading tool-call arguments while they stream: a lenient parse for cut-off +// JSON, and a targeted read of one string field for arguments that hold +// whole files. + +const PARTIAL_JSON_CACHE_MAX_SIZE = 32; +const partialJsonCache = new Map | null>(); + +function asObject(value: unknown): Record | null { + return value && typeof value === "object" && !Array.isArray(value) + ? (value as Record) + : null; +} + +/** + * Parse tool-call arguments that may be cut short mid-stream: an open string + * and open containers are closed so the keys received so far can be read. + * Null when the arguments are not an object. Memoized, since a trace parses the + * same arguments on every streamed chunk. + */ +export function parsePartialJsonArgs(args: string): Record | null { + const cached = partialJsonCache.get(args); + + if (cached !== undefined) return cached; + + let result: Record | null; + + try { + result = asObject(JSON.parse(args)); + } catch { + result = scanPartialJson(args); + } + + if (partialJsonCache.size >= PARTIAL_JSON_CACHE_MAX_SIZE) { + partialJsonCache.delete(partialJsonCache.keys().next().value!); + } + + partialJsonCache.set(args, result); + + return result; +} + +function scanPartialJson(args: string): Record | null { + let inString = false; + let escape = false; + + const stack: ("{" | "[")[] = []; + + for (const ch of args) { + if (escape) { + escape = false; + + continue; + } + + if (ch === "\\" && inString) { + escape = true; + + continue; + } + + if (ch === '"') { + inString = !inString; + + continue; + } + + if (inString) continue; + + if (ch === "{" || ch === "[") stack.push(ch); + else if (ch === "}" || ch === "]") { + if (stack.pop() !== (ch === "}" ? "{" : "[")) return null; + } + } + + let completed = args; + + // a dangling escape: keep the backslash as a literal so the string can close + if (escape) completed += "\\"; + + if (inString) completed += '"'; + else completed = completed.replace(/,?\s*$/, ""); + + for (let i = stack.length - 1; i >= 0; i--) completed += stack[i] === "{" ? "}" : "]"; + + try { + return asObject(JSON.parse(completed)); + } catch { + return null; + } +} + +const fieldPatterns = new Map(); + +/** + * Read one string field out of tool-call arguments without parsing them all: + * write and edit tools carry whole files, and a trace line needs only the + * path. The first of `keys` that holds a string wins; undefined when none does. + */ +export function extractToolArgString(args: string, keys: readonly string[]): string | undefined { + for (const key of keys) { + let pattern = fieldPatterns.get(key); + + if (!pattern) { + pattern = new RegExp(`"${key}"\\s*:\\s*"((?:[^"\\\\]|\\\\.)*)"`); + fieldPatterns.set(key, pattern); + } + + const match = pattern.exec(args); + + if (!match) continue; + + try { + const value: unknown = JSON.parse(`"${match[1]}"`); + + if (typeof value === "string") return value; + } catch { + // the next key, then the caller's full parse + } + } + + return undefined; +} + +/** The file name of a path, for a trace line; undefined without a path. */ +export function fileNameFromPath(path: string | undefined): string | undefined { + if (!path) return undefined; + + return path.split(/[\\/]/).pop() || path; +} + +/** + * Memoize `fn` by a string key, holding at most `maxSize` results; a full cache + * is emptied, which suits values that are cheap to rebuild. `keyOf` returns + * null for a call not worth caching. + */ +export function memoizeBounded( + fn: (...args: Args) => Result, + keyOf: (...args: Args) => string | null, + maxSize: number, +): (...args: Args) => Result { + const cache = new Map(); + + return (...args) => { + const key = keyOf(...args); + + if (key === null) return fn(...args); + + if (cache.has(key)) return cache.get(key) as Result; + + const result = fn(...args); + + if (cache.size >= maxSize) cache.clear(); + + cache.set(key, result); + + return result; + }; +} diff --git a/ui/lib/harness/src/trace/index.ts b/ui/lib/harness/src/trace/index.ts new file mode 100644 index 000000000000..75dfc649e255 --- /dev/null +++ b/ui/lib/harness/src/trace/index.ts @@ -0,0 +1,33 @@ +export { extractToolArgString, fileNameFromPath, parsePartialJsonArgs } from "./args.js"; +export { + createTracePhraser, + formatToolCallLine, + groupToolCallRuns, + humanizeArgKey, + isReasoningStep, + isToolCallRunning, + isToolCallStep, + LLAMA_SERVER_TOOL_PHRASING, + llamaServerToolResolver, + simpleToolArgFields, + TOOL_ARG_PATH_KEYS, + TOOL_CALL_MAIN_ARG_KEYS, + TOOL_CALL_SHOWN_ARG_DEFAULT, + TOOL_CALL_SIMPLE_ARG_MAX_CHARS, + TOOL_CALL_SUMMARY, + TOOL_CALL_TARGET_MAX_CHARS, + TOOL_CALL_THINKING_LABEL, + toolCallPath, + type TracePhraser, + type TracePhraserOptions, +} from "./phrasing.js"; +export { ToolCallSummaryKind, TraceStepType } from "./types.js"; +export type { + ResolvedToolCall, + ToolArgField, + ToolCallDescription, + ToolCallInput, + ToolCallPhrasing, + ToolCallResolver, + TraceStep, +} from "./types.js"; diff --git a/ui/lib/harness/src/trace/phrasing.ts b/ui/lib/harness/src/trace/phrasing.ts new file mode 100644 index 000000000000..1cbd02508574 --- /dev/null +++ b/ui/lib/harness/src/trace/phrasing.ts @@ -0,0 +1,497 @@ +// Plain-language lines for the tool calls of an agent trace: what one call did +// ("Read a file sheet_new.jpg"), what it is doing now ("Reading package.json") +// and what a run of calls did ("Ran 3 commands, read 2 files"). A call that +// has the argument the app picks (`description` by default) shows it as the +// model wrote it. Otherwise the app's resolvers phrase it, then llama-server's +// built-in tools, then a phrasing table, and any other tool reads as +// "Used " with its main argument. + +import { extractToolArgString, fileNameFromPath, memoizeBounded, parsePartialJsonArgs } from "./args.js"; +import { + type ResolvedToolCall, + type ToolArgField, + type ToolCallDescription, + type ToolCallInput, + type ToolCallPhrasing, + type ToolCallResolver, + ToolCallSummaryKind, + type TraceStep, + TraceStepType, +} from "./types.js"; + +/** Longest target shown after the verb before it is cut with "...". */ +export const TOOL_CALL_TARGET_MAX_CHARS = 60; + +/** Arguments of a short, one-line value read as fields instead of JSON. */ +export const TOOL_CALL_SIMPLE_ARG_MAX_CHARS = 200; + +/** The argument a call's line shows when the call has it: the model's own words for it. */ +export const TOOL_CALL_SHOWN_ARG_DEFAULT = "description"; + +/** Live title of a run of calls while the model thinks inside it. */ +export const TOOL_CALL_THINKING_LABEL = "Thinking"; + +/** Arguments a tool without a phrasing is described by; the first one present wins. */ +export const TOOL_CALL_MAIN_ARG_KEYS: readonly string[] = [ + "query", + "q", + "path", + "file_path", + "filePath", + "url", + "name", + "command", + "pattern", + "prompt", + "text", + "id", +]; + +/** Argument names a path can come under. */ +export const TOOL_ARG_PATH_KEYS: readonly string[] = ["path", "file_path", "filePath"]; + +function counted(n: number, singular: string, plural: string): string { + return `${n} ${n === 1 ? singular : plural}`; +} + +/** The group summary's clause per kind, in lower case; the summary capitalizes the first. */ +export const TOOL_CALL_SUMMARY: Readonly string>> = { + [ToolCallSummaryKind.COMMAND]: (n) => `ran ${counted(n, "command", "commands")}`, + [ToolCallSummaryKind.EDIT]: (n) => `edited ${counted(n, "file", "files")}`, + [ToolCallSummaryKind.LOAD_TOOLS]: (n) => `loaded ${counted(n, "tool", "tools")}`, + [ToolCallSummaryKind.OTHER]: (n) => `used ${counted(n, "tool", "tools")}`, + [ToolCallSummaryKind.READ]: (n) => `read ${counted(n, "file", "files")}`, + [ToolCallSummaryKind.SCRIPT]: (n) => `ran ${counted(n, "script", "scripts")}`, + [ToolCallSummaryKind.SEARCH]: (n) => (n === 1 ? "searched once" : `searched ${n} times`), + [ToolCallSummaryKind.VIEW]: (n) => `viewed ${counted(n, "media file", "media files")}`, + [ToolCallSummaryKind.WRITE]: (n) => `wrote ${counted(n, "file", "files")}`, +}; + +/** How llama-server's built-in tools read (`--tools`). */ +export const LLAMA_SERVER_TOOL_PHRASING: Readonly> = { + edit_file: { object: "a file", past: "Edited", present: "Editing", summary: ToolCallSummaryKind.EDIT }, + exec_shell_command: { + object: "a command", + past: "Ran", + present: "Running", + summary: ToolCallSummaryKind.COMMAND, + }, + file_glob_search: { + object: "files", + past: "Listed", + present: "Listing", + quoteTarget: true, + summary: ToolCallSummaryKind.SEARCH, + targetObject: "files matching", + }, + get_info: { object: "runtime info", past: "Checked", present: "Checking", summary: ToolCallSummaryKind.OTHER }, + grep_search: { + object: "files", + past: "Searched", + present: "Searching", + quoteTarget: true, + summary: ToolCallSummaryKind.SEARCH, + targetObject: "for", + }, + read_file: { object: "a file", past: "Read", present: "Reading", summary: ToolCallSummaryKind.READ }, + write_file: { object: "a file", past: "Wrote", present: "Writing", summary: ToolCallSummaryKind.WRITE }, +}; + +function stringArg(args: Record | null, key: string): string | undefined { + const value = args?.[key]; + + return typeof value === "string" && value.trim() ? value : undefined; +} + +/** The path of a call, read without parsing arguments that may hold a whole file. */ +export function toolCallPath(call: ToolCallInput): string | undefined { + return extractToolArgString(call.rawArgs, TOOL_ARG_PATH_KEYS) ?? stringArg(call.args(), "path"); +} + +/** Phrases llama-server's built-in tools, with what each acted on. */ +export const llamaServerToolResolver: ToolCallResolver = (call) => { + const phrasing = LLAMA_SERVER_TOOL_PHRASING[call.name]; + + if (!phrasing) return undefined; + + switch (call.name) { + case "read_file": + case "write_file": + case "edit_file": + return { phrasing, target: fileNameFromPath(toolCallPath(call)) }; + + case "exec_shell_command": + // the targeted read keeps the command in the line while a later + // argument still streams and the JSON does not parse + return { + phrasing, + target: stringArg(call.args(), "command") ?? extractToolArgString(call.rawArgs, ["command"]), + }; + + case "grep_search": + return { phrasing, target: stringArg(call.args(), "pattern") }; + + case "file_glob_search": { + const include = stringArg(call.args(), "include"); + + return { phrasing, target: include && include !== "**" ? include : undefined }; + } + } + + return { phrasing }; +}; + +function shorten(text: string): string { + const oneLine = text.replace(/\s+/g, " ").trim(); + + return oneLine.length > TOOL_CALL_TARGET_MAX_CHARS + ? `${oneLine.slice(0, TOOL_CALL_TARGET_MAX_CHARS - 3)}...` + : oneLine; +} + +function genericMainArg(args: Record | null): string | undefined { + if (!args) return undefined; + + for (const key of TOOL_CALL_MAIN_ARG_KEYS) { + const value = stringArg(args, key); + + if (value) return value; + } + + for (const value of Object.values(args)) { + if (typeof value === "number") return String(value); + + if (typeof value === "string" && value.trim() && value.length <= TOOL_CALL_TARGET_MAX_CHARS) { + return value; + } + } + + return undefined; +} + +function genericPhrasing(toolName: string): ToolCallPhrasing { + return { + object: toolName, + past: "Used", + present: "Using", + summary: ToolCallSummaryKind.OTHER, + targetObject: toolName, + }; +} + +/** Results each cache below holds. */ +const CACHE_MAX_SIZE = 256; +/** Arguments longer than this (whole files) are not worth holding as cache keys. */ +const CACHE_MAX_ARGS_CHARS = 2000; + +/** An argument name an app can pick: a plain key, so it is safe in a pattern. */ +const SHOWN_ARG_PATTERN = /^[\w-]+$/; + +/** The value of the argument a call's line shows, when the call has it. */ +function shownArgValue(toolArgs: string, shownArg: string | null): string | undefined { + if (!shownArg || !toolArgs || !SHOWN_ARG_PATTERN.test(shownArg)) return undefined; + + const value = + toolArgs.length <= CACHE_MAX_ARGS_CHARS + ? stringArg(parsePartialJsonArgs(toolArgs), shownArg) + : extractToolArgString(toolArgs, [shownArg]); + + return value?.trim() || undefined; +} + +/** One line of plain text for a description, e.g. `Searched for 'issue'`. */ +export function formatToolCallLine(description: ToolCallDescription): string { + if (!description.target) return description.label; + + const target = description.quoted ? `'${description.target}'` : description.target; + + return `${description.label} ${target}`; +} + +export function isToolCallStep(step: TraceStep): boolean { + return ( + step.type === TraceStepType.TOOL_CALL || + step.type === TraceStepType.TOOL_CALL_PENDING || + step.type === TraceStepType.TOOL_CALL_STREAMING + ); +} + +export function isReasoningStep(step: TraceStep): boolean { + return step.type === TraceStepType.REASONING || step.type === TraceStepType.REASONING_PENDING; +} + +/** True while the call has no result yet and the reply is still streaming. */ +export function isToolCallRunning(step: TraceStep, isStreaming: boolean): boolean { + return ( + isStreaming && + (step.type === TraceStepType.TOOL_CALL_PENDING || step.type === TraceStepType.TOOL_CALL_STREAMING) + ); +} + +export interface TracePhraserOptions { + /** The app's own tools, tried in order before llama-server's built-in ones. */ + resolvers?: readonly ToolCallResolver[]; + /** Phrasing for tools whose line names no target, by tool name. */ + phrasings?: Readonly>; +} + +export interface TracePhraser { + /** + * One tool call as a short sentence, in the past tense once it is done and + * the present tense while `running`. `shownArg` names the argument shown + * instead when the call has it (null for none). + */ + describeToolCall( + toolName: string | undefined, + toolArgs: string | undefined, + running?: boolean, + shownArg?: string | null, + ): ToolCallDescription; + /** A run of calls in one line, e.g. "Ran 3 commands, read 2 files". */ + summarizeToolCalls(steps: readonly TraceStep[]): string; + /** + * What a run of calls is doing now ("Running a command", "Thinking"): the + * model thinking at its end, or the last call still running; null when neither. + */ + describeToolActivity( + steps: readonly TraceStep[], + isStreaming: boolean, + executingToolCallId?: string | null, + shownArgFor?: (toolName: string | undefined) => string | null, + ): string | null; +} + +/** Phrasing for an app's traces: its own tools first, then llama-server's, then any tool. */ +export function createTracePhraser(options: TracePhraserOptions = {}): TracePhraser { + const resolvers = [...(options.resolvers ?? []), llamaServerToolResolver]; + const phrasings = options.phrasings ?? {}; + + function resolveCall(name: string, rawArgs: string): ResolvedToolCall { + let parsed: Record | null | undefined; + + const call: ToolCallInput = { + args: () => (parsed === undefined ? (parsed = rawArgs ? parsePartialJsonArgs(rawArgs) : null) : parsed), + name, + rawArgs, + }; + + for (const resolve of resolvers) { + const resolved = resolve(call); + + if (resolved) return resolved; + } + + const known = phrasings[name]; + + if (known) return { phrasing: known }; + + return { phrasing: genericPhrasing(name), target: genericMainArg(call.args()) }; + } + + function buildDescription( + name: string, + args: string, + running: boolean, + shownArg: string | null, + ): ToolCallDescription { + const { count = 1, phrasing, target } = resolveCall(name, args); + const shown = shownArgValue(args, shownArg); + // an argument the line already shows as its target (a shell command) would only repeat it + const described = shown && shown !== target?.trim() ? shown : undefined; + + if (described) { + return { + count, + described: true, + // as the model wrote it: rewriting its tense by rule mangles anything + // that does not start with a plain verb + label: shorten(described).replace(/[.\s]+$/, ""), + quoted: false, + summary: phrasing.summary, + }; + } + + const verb = running ? phrasing.present : phrasing.past; + const shortTarget = target ? shorten(target) : undefined; + const object = shortTarget ? phrasing.targetObject : phrasing.object; + + return { + count, + label: object ? `${verb} ${object}` : verb, + quoted: Boolean(phrasing.quoteTarget && shortTarget), + summary: phrasing.summary, + target: shortTarget, + }; + } + + // A trace is rebuilt on every streamed chunk, so the same finished call is + // described again and again: remember the answer for its arguments. Every + // caller of one call reads the same frozen object. + const describeCached = memoizeBounded( + (name: string, args: string, running: boolean, shownArg: string) => + Object.freeze(buildDescription(name, args, running, shownArg || null)), + (name, args, running, shownArg) => + args.length <= CACHE_MAX_ARGS_CHARS ? `${running ? 1 : 0}\0${shownArg}\0${name}\0${args}` : null, + CACHE_MAX_SIZE, + ); + + const describeToolCall: TracePhraser["describeToolCall"] = ( + toolName, + toolArgs, + running = false, + shownArg = TOOL_CALL_SHOWN_ARG_DEFAULT, + ) => describeCached(toolName || "a tool", toolArgs ?? "", running, shownArg ?? ""); + + return { + describeToolActivity(steps, isStreaming, executingToolCallId, shownArgFor = () => TOOL_CALL_SHOWN_ARG_DEFAULT) { + // the model thinking at the end of the run, between calls or before its answer + if (isStreaming && steps.at(-1)?.type === TraceStepType.REASONING_PENDING) { + return TOOL_CALL_THINKING_LABEL; + } + + for (let i = steps.length - 1; i >= 0; i--) { + const step = steps[i]; + const executing = step.toolCallId !== undefined && step.toolCallId === executingToolCallId; + + if (executing || isToolCallRunning(step, isStreaming)) { + return formatToolCallLine(describeToolCall(step.toolName, step.toolArgs, true, shownArgFor(step.toolName))); + } + } + + return null; + }, + + describeToolCall, + + summarizeToolCalls(steps) { + const counts = new Map(); + + for (const step of steps) { + if (!isToolCallStep(step)) continue; + + const { count, summary } = describeToolCall(step.toolName, step.toolArgs); + + counts.set(summary, (counts.get(summary) ?? 0) + count); + } + + // kinds keep the order they first appear in; other tools come last + const other = counts.get(ToolCallSummaryKind.OTHER); + + if (other !== undefined) { + counts.delete(ToolCallSummaryKind.OTHER); + counts.set(ToolCallSummaryKind.OTHER, other); + } + + const text = [...counts].map(([kind, n]) => TOOL_CALL_SUMMARY[kind](n)).join(", "); + + return text.charAt(0).toUpperCase() + text.slice(1); + }, + }; +} + +/** + * Tool arguments as labelled fields when every value is a short, one-line + * scalar; null when they need the JSON view instead. Memoized: the row that + * shows them re-renders on every chunk. + */ +export function simpleToolArgFields(toolArgs: string | undefined): ToolArgField[] | null { + return toolArgs ? argFieldsCached(toolArgs) : null; +} + +const argFieldsCached = memoizeBounded( + buildSimpleToolArgFields, + (toolArgs) => (toolArgs.length <= CACHE_MAX_ARGS_CHARS ? toolArgs : null), + CACHE_MAX_SIZE, +); + +function buildSimpleToolArgFields(toolArgs: string): ToolArgField[] | null { + let parsed: unknown; + + try { + parsed = JSON.parse(toolArgs); + } catch { + return null; + } + + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null; + + const entries = Object.entries(parsed as Record); + + if (entries.length === 0) return null; + + const fields: ToolArgField[] = []; + + for (const [key, value] of entries) { + if (value !== null && typeof value === "object") return null; + + const text = String(value); + + if (text.length > TOOL_CALL_SIMPLE_ARG_MAX_CHARS || text.includes("\n")) return null; + + fields.push({ label: humanizeArgKey(key), value: text }); + } + + return fields; +} + +/** `max_results` / `maxResults` -> `Max results`. */ +export function humanizeArgKey(key: string): string { + const words = key + .replace(/([a-z0-9])([A-Z])/g, "$1 $2") + .replace(/[_-]+/g, " ") + .trim() + .toLowerCase(); + + return words.charAt(0).toUpperCase() + words.slice(1); +} + +/** + * Split step positions into single items and runs of tool work, e.g. + * [0, [1, 2, 3], 4]. A run is the calls and all the reasoning around them, up + * to the next text, so the answer follows the work as one row: it is grouped + * once it holds `minCalls` calls, or one call with reasoning beside it. + * Reasoning with no call in reach, and text, stay apart. + */ +export function groupToolCallRuns( + steps: readonly TraceStep[], + indices: readonly number[], + minCalls: number, +): (number | number[])[] { + const items: (number | number[])[] = []; + + let run: number[] = []; + let calls = 0; + // reasoning seen since the last call: it ends the run if text follows, and + // leads into it if another call does + let thinking: number[] = []; + + const flush = () => { + const whole = [...run, ...thinking]; + + if (calls >= minCalls || (calls > 0 && whole.length > calls)) items.push(whole); + else items.push(...whole); + + run = []; + thinking = []; + calls = 0; + }; + + for (const index of indices) { + const step = steps[index]; + + if (isToolCallStep(step)) { + run.push(...thinking, index); + thinking = []; + calls++; + } else if (isReasoningStep(step)) { + thinking.push(index); + } else { + flush(); + items.push(index); + } + } + + flush(); + + return items; +} diff --git a/ui/lib/harness/src/trace/types.ts b/ui/lib/harness/src/trace/types.ts new file mode 100644 index 000000000000..f445e6805427 --- /dev/null +++ b/ui/lib/harness/src/trace/types.ts @@ -0,0 +1,94 @@ +/** + * What a step of an agent trace is: the model's text, its reasoning, or a tool + * call, finished or still in flight. Apps keep their own step type; anything + * with these values in `type` reads as a trace step. + */ +export const TraceStepType = { + REASONING: "reasoning", + REASONING_PENDING: "reasoning_pending", + TEXT: "text", + TOOL_CALL: "tool_call", + TOOL_CALL_PENDING: "tool_call_pending", + TOOL_CALL_STREAMING: "tool_call_streaming", +} as const; + +export type TraceStepType = (typeof TraceStepType)[keyof typeof TraceStepType]; + +/** One step of an agent trace, as much of it as the phrasing reads. */ +export interface TraceStep { + /** A TraceStepType value. */ + type: string; + toolCallId?: string; + toolName?: string; + /** The call's arguments as streamed: JSON, possibly cut short. */ + toolArgs?: string; +} + +/** Which clause a call counts under in a group summary ("ran 3 commands"). */ +export const ToolCallSummaryKind = { + COMMAND: "command", + EDIT: "edit", + LOAD_TOOLS: "load_tools", + OTHER: "other", + READ: "read", + SCRIPT: "script", + SEARCH: "search", + VIEW: "view", + WRITE: "write", +} as const; + +export type ToolCallSummaryKind = (typeof ToolCallSummaryKind)[keyof typeof ToolCallSummaryKind]; + +/** How a tool call reads in a trace. */ +export interface ToolCallPhrasing { + /** Verb once the call is done, e.g. "Read". */ + past: string; + /** Verb while the call runs, e.g. "Reading". */ + present: string; + /** Words after the verb when the call has no target, e.g. "a file". */ + object: string; + /** Words between the verb and the target, e.g. "for". */ + targetObject?: string; + /** Show the target in quotes, for search terms. */ + quoteTarget?: boolean; + summary: ToolCallSummaryKind; +} + +/** A tool call as one plain sentence: label, then an optional target. */ +export interface ToolCallDescription { + /** Verb and object, e.g. "Read a file" or "Searched for". */ + label: string; + /** What the call acted on, e.g. a file name or a search term. */ + target?: string; + quoted: boolean; + summary: ToolCallSummaryKind; + /** How many things the call counts as in the group summary. */ + count: number; + /** True when the label is the model's own description of the call. */ + described?: boolean; +} + +/** A tool call as a resolver sees it. */ +export interface ToolCallInput { + name: string; + /** The arguments as streamed. Whole files for write tools: read them with extractToolArgString. */ + rawArgs: string; + /** The arguments parsed leniently, cut-off JSON included; parsed on first call. */ + args(): Record | null; +} + +/** How one call reads: its phrasing, what it acted on, and how many things it counts as. */ +export interface ResolvedToolCall { + phrasing: ToolCallPhrasing; + target?: string; + count?: number; +} + +/** Phrases the calls of one tool, or of a family of tools; undefined passes the call on. */ +export type ToolCallResolver = (call: ToolCallInput) => ResolvedToolCall | undefined; + +/** A tool argument shown as a labelled field. */ +export interface ToolArgField { + label: string; + value: string; +} diff --git a/ui/lib/harness/src/types.ts b/ui/lib/harness/src/types.ts new file mode 100644 index 000000000000..9167e37ca111 --- /dev/null +++ b/ui/lib/harness/src/types.ts @@ -0,0 +1,209 @@ +// Messages and tools use the OpenAI chat-completions shapes, so they go to the endpoint as they are. + +export interface TextPart { + type: "text"; + text: string; +} + +export interface ImagePart { + type: "image_url"; + image_url: { url: string }; +} + +export interface AudioPart { + type: "input_audio"; + input_audio: { data: string; format: string }; +} + +export type ContentPart = TextPart | ImagePart | AudioPart; + +export interface ToolCall { + id: string; + type: "function"; + function: { name: string; arguments: string }; +} + +/** A tool call as it streams in: any field can still be missing. */ +export interface PartialToolCall { + id?: string; + type?: string; + function?: { name?: string; arguments?: string }; +} + +export type Message = + | { role: "system" | "user"; content: string | ContentPart[] } + | { + role: "assistant"; + content?: string | ContentPart[]; + reasoning_content?: string; + tool_calls?: ToolCall[]; + } + | { role: "tool"; tool_call_id: string; content: string | ContentPart[] }; + +export interface ToolDefinition { + type: "function"; + function: { + name: string; + description?: string; + parameters?: Record; + }; +} + +export interface ToolContext { + signal?: AbortSignal; + /** Report the output so far of a tool that streams. Each call replaces the previous content. */ + update(content: string): void; +} + +export interface ToolResult { + /** Text the model reads. */ + content: string; + /** Media the model reads after the text, e.g. an image the tool produced. */ + parts?: ContentPart[]; + isError?: boolean; + /** Set it when the tool times itself, e.g. to leave out a wait for approval. Default: the time execute took. */ + durationMs?: number; + /** App data the loop does not read. It comes back on the tool_result event. */ + details?: unknown; +} + +export interface Tool { + definition: ToolDefinition; + /** Run the call. Reject with an AbortError to end the run, with any other error to report it to the model. */ + execute(call: ToolCall, context: ToolContext): Promise; +} + +/** Token counts and times of one request, as llama-server reports them. */ +export interface LlmTimings { + cache_n?: number; + predicted_ms?: number; + predicted_n?: number; + prompt_ms?: number; + prompt_n?: number; +} + +export interface LlmTotals { + predicted_ms: number; + predicted_n: number; + prompt_ms: number; + prompt_n: number; +} + +export interface ToolCallTiming { + name: string; + duration_ms: number; + success: boolean; +} + +export interface TurnStats { + turn: number; + llm: LlmTotals; + toolCalls: ToolCallTiming[]; + toolsMs: number; +} + +/** Totals of a run: every turn's requests and tool calls. */ +export interface RunTimings { + turns: number; + toolCallsCount: number; + toolsMs: number; + toolCalls: ToolCallTiming[]; + perTurn: TurnStats[]; + llm: LlmTotals; +} + +/** The last request's timings, with the run's totals once a tool has run. */ +export interface Timings extends LlmTimings { + agentic?: RunTimings; +} + +export interface PromptProgress { + cache: number; + processed: number; + time_ms: number; + total: number; +} + +export interface InferenceRequest { + messages: Message[]; + tools?: ToolDefinition[]; + signal?: AbortSignal; +} + +export type InferenceEvent = + | { type: "text"; delta: string } + | { type: "reasoning"; delta: string } + /** Every tool call of the response so far, not only the new part. */ + | { type: "tool_calls"; toolCalls: PartialToolCall[] } + | { type: "timings"; timings?: LlmTimings; progress?: PromptProgress } + | { type: "model"; model: string } + | { type: "completion_id"; id: string }; + +/** + * One streamed chat completion. The loop needs nothing else from llama-server. + * Timings, model and completion id are optional extras. + */ +export interface InferenceClient { + stream(request: InferenceRequest): AsyncIterable; +} + +export type DoneReason = + /** The model answered without calling a tool. */ + | "complete" + | "aborted" + /** `interrupted` returned true. */ + | "interrupted" + /** The turn limit was reached and `onTurnLimit` did not grant more turns. */ + | "turn_limit"; + +export type HarnessEvent = + /** `turn` starts at 1, and again at 1 once `onTurnLimit` grants more turns. */ + | { type: "turn_start"; turn: number } + | { type: "text"; delta: string } + | { type: "reasoning"; delta: string } + | { type: "tool_call_delta"; toolCalls: PartialToolCall[] } + /** `llm` is the run's totals including the request in flight. */ + | { type: "timings"; timings?: LlmTimings; progress?: PromptProgress; llm?: LlmTotals } + | { type: "model"; model: string } + | { type: "completion_id"; id: string } + /** The assistant message of a turn. `final` is true when the run ends after it and none of its tool calls runs. */ + | { + type: "message"; + content: string; + reasoning?: string; + toolCalls?: ToolCall[]; + timings?: Timings; + final: boolean; + } + | { type: "tool_call"; toolCall: ToolCall } + | { type: "tool_update"; toolCall: ToolCall; content: string } + /** `skipped` is true when the call did not run because the run was interrupted. */ + | { type: "tool_result"; toolCall: ToolCall; result: ToolResult; skipped: boolean } + | { type: "turn_end"; timings: Timings } + | { type: "done"; reason: DoneReason; timings?: Timings } + | { type: "error"; error: Error; timings?: Timings }; + +export interface RunOptions { + /** Turns before `onTurnLimit` is asked. Default: no limit. */ + maxTurns?: number; + /** Resolve true to run `maxTurns` more turns. Default: the run ends. */ + onTurnLimit?: (signal?: AbortSignal) => Promise; + /** + * Return true to end the run at the next safe point, e.g. when the user sent a new message. Tool calls that did + * not run yet get a result that says so. Use `signal` to stop at once. + */ + interrupted?: () => boolean; +} + +/** + * The contract an app runs a loop through. `run` never throws: it ends with a `done` or an `error` event. It waits + * for the consumer between events, so the consumer can store each one before the loop goes on. + */ +export interface Harness { + run( + messages: Message[], + tools: Tool[], + signal?: AbortSignal, + options?: RunOptions, + ): AsyncIterable; +} diff --git a/ui/lib/harness/tests/harness.test.ts b/ui/lib/harness/tests/harness.test.ts new file mode 100644 index 000000000000..4e302a14c5fe --- /dev/null +++ b/ui/lib/harness/tests/harness.test.ts @@ -0,0 +1,316 @@ +import { describe, expect, it } from "vitest"; +import { + createHarness, + INTERRUPTED_TOOL_RESULT, + type HarnessEvent, + type InferenceClient, + type InferenceEvent, + type InferenceRequest, + type Message, + type RunOptions, + type Tool, + type ToolResult, +} from "../src/index.js"; + +const USER: Message[] = [{ content: "hi", role: "user" }]; + +function callEvent(...names: string[]): InferenceEvent { + return { + toolCalls: names.map((name, index) => ({ + function: { arguments: "{}", name }, + id: `call_${index}`, + })), + type: "tool_calls", + }; +} + +// a client that plays one scripted turn per request and records what it was sent +function scriptedClient(turns: Array) { + const requests: InferenceRequest[] = []; + const client: InferenceClient = { + async *stream(request) { + requests.push({ ...request, messages: [...request.messages] }); + + const turn = turns[requests.length - 1] ?? []; + + if (turn instanceof Error) throw turn; + + yield* turn; + }, + }; + + return { client, requests }; +} + +function tool( + name: string, + execute: Tool["execute"] = async () => ({ content: `${name} ran` }), +): Tool { + return { definition: { function: { name }, type: "function" }, execute }; +} + +async function collect( + client: InferenceClient, + tools: Tool[] = [], + options?: RunOptions, + signal?: AbortSignal, +): Promise { + const events: HarnessEvent[] = []; + + for await (const event of createHarness(client).run(USER, tools, signal, options)) + events.push(event); + + return events; +} + +function types(events: HarnessEvent[]): string[] { + return events.map((event) => event.type); +} + +function last(events: HarnessEvent[]): HarnessEvent { + return events[events.length - 1]; +} + +function results(events: HarnessEvent[]) { + return events.filter((event) => event.type === "tool_result"); +} + +describe("createHarness", () => { + it("ends after a turn without tool calls", async () => { + const { client, requests } = scriptedClient([ + [ + { delta: "think", type: "reasoning" }, + { delta: "Hel", type: "text" }, + { delta: "lo", type: "text" }, + ], + ]); + const events = await collect(client); + + expect(types(events)).toEqual(["turn_start", "reasoning", "text", "text", "message", "done"]); + expect(events[4]).toMatchObject({ content: "Hello", final: true, reasoning: "think" }); + expect(last(events)).toMatchObject({ reason: "complete" }); + expect(requests[0].tools).toBeUndefined(); + }); + + it("runs the called tool and sends its result with the next request", async () => { + const { client, requests } = scriptedClient([ + [{ delta: "let me look", type: "text" }, callEvent("read")], + [{ delta: "done", type: "text" }], + ]); + const events = await collect(client, [tool("read")]); + + expect(types(events)).toEqual([ + "turn_start", + "text", + "tool_call_delta", + "message", + "tool_call", + "tool_result", + "turn_end", + "turn_start", + "text", + "message", + "turn_end", + "done", + ]); + expect(events[3]).toMatchObject({ + final: false, + toolCalls: [{ id: "call_0", type: "function" }], + }); + expect(requests[1].messages.slice(1)).toEqual([ + { + content: "let me look", + reasoning_content: undefined, + role: "assistant", + tool_calls: [ + { function: { arguments: "{}", name: "read" }, id: "call_0", type: "function" }, + ], + }, + { content: "read ran", role: "tool", tool_call_id: "call_0" }, + ]); + expect(requests[0].tools).toEqual([{ function: { name: "read" }, type: "function" }]); + }); + + it("sends a result's media parts after its text", async () => { + const image = { image_url: { url: "data:image/png;base64,AAAA" }, type: "image_url" } as const; + const { client, requests } = scriptedClient([[callEvent("shot")], []]); + + await collect(client, [tool("shot", async () => ({ content: "saved", parts: [image] }))]); + + expect(requests[1].messages[2]).toEqual({ + content: [{ text: "saved", type: "text" }, image], + role: "tool", + tool_call_id: "call_0", + }); + }); + + it("reports an unknown tool and a tool that throws to the model", async () => { + const { client } = scriptedClient([[callEvent("missing", "broken")], []]); + const events = await collect(client, [ + tool("broken", async () => { + throw new Error("boom"); + }), + ]); + + expect(results(events).map((event) => event.result)).toEqual([ + { content: "Error: Unknown tool: missing", isError: true }, + { content: "Error: boom", isError: true }, + ]); + expect(last(events)).toMatchObject({ + timings: { + agentic: { toolCalls: [{ success: false }, { success: false }], toolCallsCount: 2 }, + }, + }); + }); + + it("emits a streaming tool's updates before its result", async () => { + const { client } = scriptedClient([[callEvent("shell")], []]); + const events = await collect(client, [ + tool("shell", async (_call, { update }) => { + update(""); + await Promise.resolve(); + update("a"); + update("ab"); + + return { content: "ab" }; + }), + ]); + const updates = events.filter((event) => event.type === "tool_update"); + + // "a" was replaced before the consumer asked for it + expect(updates.map((event) => event.content)).toEqual(["", "ab"]); + expect(types(events).indexOf("tool_result")).toBeGreaterThan( + types(events).lastIndexOf("tool_update"), + ); + }); + + it("uses the duration a tool reports", async () => { + const { client } = scriptedClient([[callEvent("slow")], []]); + const events = await collect(client, [ + tool("slow", async () => ({ content: "", durationMs: 1234 })), + ]); + + expect(last(events)).toMatchObject({ timings: { agentic: { toolsMs: 1234 } } }); + }); + + it("ends without a result when a tool rejects with an AbortError", async () => { + const { client, requests } = scriptedClient([[callEvent("gated")], []]); + const events = await collect(client, [ + tool("gated", async () => { + throw new DOMException("Operation was aborted", "AbortError"); + }), + ]); + + expect(results(events)).toEqual([]); + expect(last(events)).toMatchObject({ reason: "aborted" }); + expect(requests).toHaveLength(1); + }); + + it("keeps the partial output when the signal aborts mid-stream", async () => { + const controller = new AbortController(); + const client: InferenceClient = { + async *stream() { + yield { delta: "par", type: "text" }; + controller.abort(); + throw new DOMException("Operation was aborted", "AbortError"); + }, + }; + const events = await collect(client, [], undefined, controller.signal); + + expect(types(events)).toEqual(["turn_start", "text", "message", "done"]); + expect(events[2]).toMatchObject({ content: "par", final: true }); + expect(last(events)).toMatchObject({ reason: "aborted" }); + }); + + it("ends with an error event when the stream fails", async () => { + const { client } = scriptedClient([new Error("HTTP 500")]); + const events = await collect(client); + + expect(types(events)).toEqual(["turn_start", "message", "error"]); + expect(last(events)).toMatchObject({ error: { message: "HTTP 500" } }); + }); + + it("keeps the tool calls of the turn it is interrupted after, and runs none", async () => { + const { client, requests } = scriptedClient([[callEvent("read")], []]); + const events = await collect(client, [tool("read")], { interrupted: () => true }); + + expect(types(events)).toEqual(["turn_start", "tool_call_delta", "message", "done"]); + expect(events[2]).toMatchObject({ final: true, toolCalls: [{ id: "call_0" }] }); + expect(last(events)).toMatchObject({ reason: "interrupted" }); + expect(requests).toHaveLength(1); + }); + + it("skips the tool calls left when it is interrupted between two", async () => { + let interrupted = false; + + const { client, requests } = scriptedClient([[callEvent("first", "second", "third")], []]); + const events = await collect( + client, + [ + tool("first", async (): Promise => { + interrupted = true; + + return { content: "first ran" }; + }), + tool("second"), + tool("third"), + ], + { interrupted: () => interrupted }, + ); + + expect(results(events).map((event) => [event.result.content, event.skipped])).toEqual([ + ["first ran", false], + [INTERRUPTED_TOOL_RESULT, true], + [INTERRUPTED_TOOL_RESULT, true], + ]); + expect(last(events)).toMatchObject({ reason: "interrupted" }); + expect(requests).toHaveLength(1); + }); + + it("asks at the turn limit and starts the turn count again when it goes on", async () => { + const answers = [true, false]; + const { client } = scriptedClient([[callEvent("read")], [callEvent("read")], []]); + const events = await collect(client, [tool("read")], { + maxTurns: 1, + onTurnLimit: async () => answers.shift() ?? false, + }); + const turns = events.filter((event) => event.type === "turn_start"); + + expect(turns.map((event) => event.turn)).toEqual([1, 1]); + expect(last(events)).toMatchObject({ reason: "turn_limit" }); + }); + + it("adds up the timings of every turn", async () => { + const timings = (n: number): InferenceEvent => ({ + timings: { cache_n: 1, predicted_ms: n, predicted_n: n, prompt_ms: n, prompt_n: n }, + type: "timings", + }); + const { client } = scriptedClient([[timings(10), callEvent("read")], [timings(5)]]); + const events = await collect(client, [tool("read")]); + const live = events + .filter((event) => event.type === "timings") + .map((event) => event.llm?.predicted_n); + + expect(live).toEqual([10, 15]); + expect(last(events)).toMatchObject({ + timings: { + agentic: { + llm: { predicted_ms: 15, predicted_n: 15, prompt_ms: 15, prompt_n: 15 }, + perTurn: [{ turn: 1 }, { turn: 2 }], + toolCallsCount: 1, + turns: 2, + }, + cache_n: 1, + predicted_n: 5, + }, + }); + }); + + it("reports the last request's timings alone when no tool ran", async () => { + const { client } = scriptedClient([[{ timings: { predicted_n: 3 }, type: "timings" }]]); + const events = await collect(client); + + expect(types(events)).toEqual(["turn_start", "timings", "message", "turn_end", "done"]); + expect(last(events)).toEqual({ reason: "complete", timings: { predicted_n: 3 }, type: "done" }); + }); +}); diff --git a/ui/lib/harness/tests/openai-client.test.ts b/ui/lib/harness/tests/openai-client.test.ts new file mode 100644 index 000000000000..fedcb59a0a8c --- /dev/null +++ b/ui/lib/harness/tests/openai-client.test.ts @@ -0,0 +1,130 @@ +import { describe, expect, it } from "vitest"; +import { createOpenAIClient, type InferenceEvent } from "../src/index.js"; + +function sse(...chunks: unknown[]): string { + return chunks.map((chunk) => `data: ${JSON.stringify(chunk)}\n\n`).join("") + "data: [DONE]\n\n"; +} + +// answers every request with the given body, cut into pieces to cross read boundaries +function fakeFetch(body: string, init: ResponseInit = {}, pieceLength = 7) { + const calls: Array<{ url: string; init: RequestInit }> = []; + const fetch = (async (url: string, requestInit: RequestInit) => { + calls.push({ init: requestInit, url }); + + const bytes = new TextEncoder().encode(body); + const stream = new ReadableStream({ + start(controller) { + for (let i = 0; i < bytes.length; i += pieceLength) { + controller.enqueue(bytes.slice(i, i + pieceLength)); + } + + controller.close(); + }, + }); + + return new Response(stream, init); + }) as typeof globalThis.fetch; + + return { calls, fetch }; +} + +async function collect(stream: AsyncIterable): Promise { + const events: InferenceEvent[] = []; + + for await (const event of stream) events.push(event); + + return events; +} + +const delta = (fields: object, rest: object = {}) => ({ + choices: [{ delta: fields }], + id: "cmpl-1", + model: "m", + ...rest, +}); + +describe("createOpenAIClient", () => { + it("posts a streaming request to the chat completions endpoint", async () => { + const { calls, fetch } = fakeFetch(sse()); + const client = createOpenAIClient({ + apiKey: "key", + baseUrl: "http://localhost:8080/", + fetch, + model: "m", + params: { temperature: 0 }, + }); + + await collect(client.stream({ messages: [{ content: "hi", role: "user" }] })); + + expect(calls[0].url).toBe("http://localhost:8080/v1/chat/completions"); + expect(calls[0].init.headers).toMatchObject({ Authorization: "Bearer key" }); + expect(JSON.parse(calls[0].init.body as string)).toEqual({ + messages: [{ content: "hi", role: "user" }], + model: "m", + stream: true, + temperature: 0, + }); + }); + + it("turns the chunks into events", async () => { + const timings = { predicted_n: 2 }; + const { fetch } = fakeFetch( + sse( + delta({ reasoning_content: "hm" }), + delta({ content: "Hel" }), + delta({ content: "lo" }), + delta({}, { timings }), + ), + ); + const events = await collect( + createOpenAIClient({ baseUrl: "http://x", fetch }).stream({ messages: [] }), + ); + + expect(events).toEqual([ + { id: "cmpl-1", type: "completion_id" }, + { model: "m", type: "model" }, + { delta: "hm", type: "reasoning" }, + { delta: "Hel", type: "text" }, + { delta: "lo", type: "text" }, + { progress: undefined, timings, type: "timings" }, + ]); + }); + + it("merges tool call deltas by index", async () => { + const { fetch } = fakeFetch( + sse( + delta({ + tool_calls: [ + { function: { arguments: "", name: "read" }, id: "a", index: 0, type: "function" }, + ], + }), + delta({ tool_calls: [{ function: { arguments: '{"path"' }, index: 0 }] }), + delta({ + tool_calls: [ + { function: { arguments: ':"x"}' }, index: 0 }, + { function: { arguments: "{}", name: "list" }, id: "b", index: 1, type: "function" }, + ], + }), + ), + ); + const events = await collect( + createOpenAIClient({ baseUrl: "http://x", fetch }).stream({ messages: [] }), + ); + const toolCalls = events.filter((event) => event.type === "tool_calls"); + + expect(toolCalls).toHaveLength(3); + expect(toolCalls[2].toolCalls).toEqual([ + { function: { arguments: '{"path":"x"}', name: "read" }, id: "a", type: "function" }, + { function: { arguments: "{}", name: "list" }, id: "b", type: "function" }, + ]); + }); + + it("throws the server's error message", async () => { + const { fetch } = fakeFetch(JSON.stringify({ error: { message: "no model" } }), { + status: 400, + }); + const stream = createOpenAIClient({ baseUrl: "http://x", fetch }).stream({ messages: [] }); + + await expect(collect(stream)).rejects.toThrow("HTTP 400: no model"); + }); +}); diff --git a/ui/lib/harness/tests/trace.test.ts b/ui/lib/harness/tests/trace.test.ts new file mode 100644 index 000000000000..093c0366e0a6 --- /dev/null +++ b/ui/lib/harness/tests/trace.test.ts @@ -0,0 +1,123 @@ +import { describe, expect, it } from "vitest"; +import { + createTracePhraser, + extractToolArgString, + formatToolCallLine, + groupToolCallRuns, + humanizeArgKey, + parsePartialJsonArgs, + simpleToolArgFields, + ToolCallSummaryKind, + type TraceStep, + TraceStepType, +} from "../src/index.js"; + +function call(toolName: string, args: object | string, type: string = TraceStepType.TOOL_CALL): TraceStep { + return { toolArgs: typeof args === "string" ? args : JSON.stringify(args), toolName, type }; +} + +const phraser = createTracePhraser(); +const line = (name: string, args: object | string, running = false) => + formatToolCallLine(phraser.describeToolCall(name, typeof args === "string" ? args : JSON.stringify(args), running)); + +describe("parsePartialJsonArgs", () => { + it("reads the keys of arguments cut off mid-stream", () => { + expect(parsePartialJsonArgs('{"path": "src/a.ts", "content": "line')).toEqual({ + content: "line", + path: "src/a.ts", + }); + expect(parsePartialJsonArgs('{"items": [1, 2,')).toEqual({ items: [1, 2] }); + expect(parsePartialJsonArgs("[1, 2]")).toBeNull(); + }); + + it("reads one field of a whole-file argument without parsing it", () => { + expect(extractToolArgString('{"content": "x\\"y", "path": "a\\\\b.ts"}', ["file_path", "path"])).toBe("a\\b.ts"); + }); +}); + +describe("llama-server tools", () => { + it("say what each call acted on, in the past or present tense", () => { + expect(line("read_file", { path: "/repo/src/app.ts" })).toBe("Read app.ts"); + expect(line("read_file", { path: "/repo/src/app.ts" }, true)).toBe("Reading app.ts"); + expect(line("exec_shell_command", { command: "npm test" })).toBe("Ran npm test"); + expect(line("grep_search", { pattern: "TODO" })).toBe("Searched for 'TODO'"); + expect(line("file_glob_search", { include: "**" })).toBe("Listed files"); + }); + + it("keep the command in the line while a later argument still streams", () => { + expect(line("exec_shell_command", '{"command": "ls -la", "timeout": 1')).toBe("Ran ls -la"); + }); +}); + +describe("createTracePhraser", () => { + it("shows the model's own description of a call as it wrote it", () => { + expect(line("exec_shell_command", { command: "npm test", description: "Run the tests." })).toBe("Run the tests"); + }); + + it("reads any other tool as Used, with its main argument", () => { + expect(line("create_issue", { title: "Bug", query: "crash" })).toBe("Used create_issue crash"); + }); + + it("asks the app's resolvers first, then its phrasing table", () => { + const app = createTracePhraser({ + phrasings: { + get_datetime: { object: "the time", past: "Checked", present: "Checking", summary: ToolCallSummaryKind.OTHER }, + }, + resolvers: [ + (c) => + c.name === "read_file" + ? { phrasing: { object: "notes", past: "Opened", present: "Opening", summary: ToolCallSummaryKind.READ } } + : undefined, + ], + }); + + expect(formatToolCallLine(app.describeToolCall("read_file", '{"path":"a"}'))).toBe("Opened notes"); + expect(formatToolCallLine(app.describeToolCall("get_datetime", "{}"))).toBe("Checked the time"); + }); + + it("summarizes a run by kind, other tools last", () => { + const steps = [ + call("create_issue", {}), + call("exec_shell_command", { command: "a" }), + call("read_file", { path: "x" }), + call("exec_shell_command", { command: "b" }), + { type: TraceStepType.TEXT }, + ]; + + expect(phraser.summarizeToolCalls(steps)).toBe("Ran 2 commands, read 1 file, used 1 tool"); + }); + + it("describes what is running now, or the model thinking", () => { + const steps = [call("read_file", { path: "a.ts" }), call("grep_search", { pattern: "x" }, TraceStepType.TOOL_CALL_PENDING)]; + + expect(phraser.describeToolActivity(steps, true)).toBe("Searching for 'x'"); + expect(phraser.describeToolActivity(steps, false)).toBeNull(); + expect(phraser.describeToolActivity([{ type: TraceStepType.REASONING_PENDING }], true)).toBe("Thinking"); + }); +}); + +describe("groupToolCallRuns", () => { + it("groups calls with the reasoning around them, up to the next text", () => { + const steps: TraceStep[] = [ + { type: TraceStepType.REASONING }, + call("read_file", {}), + { type: TraceStepType.REASONING }, + call("grep_search", {}), + { type: TraceStepType.TEXT }, + call("read_file", {}), + ]; + + expect(groupToolCallRuns(steps, [0, 1, 2, 3, 4, 5], 2)).toEqual([[0, 1, 2, 3], 4, 5]); + }); +}); + +describe("tool arguments", () => { + it("read as fields only when every value is short and on one line", () => { + expect(simpleToolArgFields('{"max_results": 5, "query": "llama"}')).toEqual([ + { label: "Max results", value: "5" }, + { label: "Query", value: "llama" }, + ]); + expect(simpleToolArgFields('{"body": "a\\nb"}')).toBeNull(); + expect(humanizeArgKey("filePath")).toBe("File path"); + }); +}); diff --git a/ui/lib/harness/tsconfig.build.json b/ui/lib/harness/tsconfig.build.json new file mode 100644 index 000000000000..cb6a3121d48a --- /dev/null +++ b/ui/lib/harness/tsconfig.build.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "dist", + "declaration": true, + "sourceMap": true + }, + "include": ["src"] +} diff --git a/ui/lib/harness/tsconfig.json b/ui/lib/harness/tsconfig.json new file mode 100644 index 000000000000..caa8927c4fc5 --- /dev/null +++ b/ui/lib/harness/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "ES2022", + "lib": ["ES2022", "DOM", "DOM.Iterable"], + "module": "NodeNext", + "moduleResolution": "NodeNext", + "strict": true, + "verbatimModuleSyntax": true, + "skipLibCheck": true, + "types": ["node"] + }, + "include": ["src", "tests"] +}