A high-performance inference engine for AI models
Pi extension (#963)
uuuvn455f290eb14a
diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.ymlindex cddd42ae2..4bd5c84e2 100644--- a/.github/actions/setup/action.yml+++ b/.github/actions/setup/action.yml@@ -26,6 +26,10 @@ inputs:description: "Install bindings requirements"required: falsedefault: 'false'+ node-version:+ description: "Node.js version for bindings and apps"+ required: false+ default: '20'runs:using: composite@@ -90,7 +94,7 @@ runs:- if: inputs.bindings == 'true'uses: actions/setup-node@v4with:- node-version: "20"+ node-version: ${{ inputs.node-version }}registry-url: "https://registry.npmjs.org"- if: inputs.bindings == 'true'diff --git a/.github/workflows/release-uzu-pi-extension.yml b/.github/workflows/release-uzu-pi-extension.ymlnew file mode 100644index 000000000..b6a34dcca--- /dev/null+++ b/.github/workflows/release-uzu-pi-extension.yml@@ -0,0 +1,129 @@+name: "Build / release uzu-pi-extension"++on:+ workflow_dispatch:+ release:+ types: [published]++permissions:+ contents: read++concurrency:+ group: uzu-pi-extension-${{ github.event_name }}+ cancel-in-progress: false++defaults:+ run:+ shell: bash++jobs:+ decide:+ name: decide+ if: ${{ github.event_name == 'workflow_dispatch' || !github.event.release.prerelease }}+ runs-on: ubuntu-latest+ outputs:+ build: ${{ steps.decide.outputs.build }}+ version: ${{ steps.decide.outputs.version }}+ steps:+ - uses: actions/checkout@v6+ with:+ ref: ${{ github.sha }}+ - id: decide+ env:+ TAG: ${{ github.event.release.tag_name }}+ run: |+ node --input-type=module <<'JS'+ import fs from 'node:fs';+ const version = JSON.parse(fs.readFileSync('apps/uzu-pi-extension/package.json')).version;+ fs.appendFileSync(process.env.GITHUB_OUTPUT, `version=${version}\n`);+ let build = true;+ if (process.env.GITHUB_EVENT_NAME === 'release') {+ if (!/^\d+\.\d+\.\d+$/.test(process.env.TAG)) build = false;+ else {+ if (version !== process.env.TAG) throw new Error('Extension version does not match release tag; run cargo tools sync');+ const response = await fetch(`https://registry.npmjs.org/@trymirai%2fuzu-pi-extension/${version}`);+ if (response.ok) build = false;+ else if (response.status !== 404) throw new Error(`npm registry returned ${response.status}`);+ }+ }+ fs.appendFileSync(process.env.GITHUB_OUTPUT, `build=${build}\n`);+ JS++ build:+ name: Build extension package+ needs: decide+ if: ${{ needs.decide.outputs.build == 'true' }}+ runs-on: prefect-github-actions+ outputs:+ artifact-id: ${{ steps.artifacts.outputs.artifact-id }}+ defaults:+ run:+ working-directory: apps/uzu-pi-extension+ env:+ APP_VERSION: ${{ needs.decide.outputs.version }}+ steps:+ - uses: actions/checkout@v6+ with:+ ref: ${{ github.sha }}+ persist-credentials: false+ - uses: ./.github/actions/setup+ with:+ metal: true+ bindings: true+ node-version: '24'++ - name: Verify matching npm release+ if: ${{ github.event_name == 'release' }}+ run: test "$(npm view "@trymirai/uzu@$APP_VERSION" version)" = "$APP_VERSION"++ - name: Build monorepo bindings+ working-directory: crates/legacy/uzu/bindings/typescript+ env:+ MIRAI_DISABLE_DENO_BUILD: '1'+ run: |+ pnpm install --frozen-lockfile+ pnpm exec napi build --manifest-path ../../Cargo.toml --no-default-features --features backend-metal,capability-grammar,bindings-napi --platform --output-dir src/napi+ pnpm build++ - name: Install dependencies+ run: pnpm install --frozen-lockfile++ - name: Check and build extension+ run: |+ pnpm check+ pnpm --dir dist pack+ node scripts/check-package.mjs++ - name: Verify released dependency+ if: ${{ github.event_name == 'release' }}+ working-directory: apps/uzu-pi-extension/dist+ run: |+ npm install --ignore-scripts --omit=dev+ node --input-type=module -e 'import { Engine, ChatModelInfo, ToolCallArgumentStream } from "@trymirai/uzu"; if (!Engine.prototype.chatModelInfo || !Engine.prototype.refreshModels || !ChatModelInfo || !ToolCallArgumentStream) throw new Error("Released Uzu lacks the required API"); await import("./index.js");'++ - uses: actions/upload-artifact@v4+ id: artifacts+ with:+ name: uzu-pi-extension-${{ needs.decide.outputs.version }}+ path: apps/uzu-pi-extension/dist/*.tgz+ if-no-files-found: error++ publish:+ name: Publish npm package+ needs: [decide, build]+ if: ${{ github.event_name == 'release' }}+ runs-on: ubuntu-latest+ steps:+ - uses: actions/download-artifact@v4+ with:+ artifact-ids: ${{ needs.build.outputs.artifact-id }}+ merge-multiple: true+ - uses: actions/setup-node@v4+ with:+ node-version: '24'+ registry-url: 'https://registry.npmjs.org'+ - name: Publish+ env:+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}+ APP_VERSION: ${{ needs.decide.outputs.version }}+ run: npm publish "trymirai-uzu-pi-extension-${APP_VERSION}.tgz" --access publicdiff --git a/.github/workflows/tests.yml b/.github/workflows/tests.ymlindex fa5101cd6..8d79301c0 100644--- a/.github/workflows/tests.yml+++ b/.github/workflows/tests.yml@@ -259,10 +259,53 @@ jobs:path: apps/mirai-gui/desktop/target/aarch64-apple-darwin/release/bundle/dmg/*.dmgif-no-files-found: error+ build-uzu-pi-extension:+ name: check and build (uzu-pi-extension)+ runs-on: prefect-github-actions++ defaults:+ run:+ working-directory: apps/uzu-pi-extension++ steps:+ - uses: actions/checkout@v6+ - uses: ./.github/actions/setup+ with:+ metal: true+ bindings: true+ node-version: '24'++ - name: Build monorepo bindings+ working-directory: crates/legacy/uzu/bindings/typescript+ env:+ MIRAI_DISABLE_DENO_BUILD: '1'+ run: |+ pnpm install --frozen-lockfile+ pnpm exec napi build --manifest-path ../../Cargo.toml --no-default-features --features backend-metal,capability-grammar,bindings-napi --platform --output-dir src/napi+ pnpm build++ - name: Install dependencies+ run: pnpm install --frozen-lockfile++ - name: Typecheck, test and build+ run: pnpm check++ - name: Check package+ run: |+ pnpm --dir dist pack+ node scripts/check-package.mjs++ - name: Upload extension package+ uses: actions/upload-artifact@v4+ with:+ name: uzu-pi-extension+ path: apps/uzu-pi-extension/dist/*.tgz+ if-no-files-found: error+completed:name: completedruns-on: ubuntu-latest- needs: [linters, tests, validate-bindings, linters-mirai-gui, tests-mirai-gui, build-mirai-gui]+ needs: [linters, tests, validate-bindings, linters-mirai-gui, tests-mirai-gui, build-mirai-gui, build-uzu-pi-extension]if: ${{ always() }}steps:@@ -273,3 +316,4 @@ jobs:[ "${{ needs.linters-mirai-gui.result }}" = success ][ "${{ needs.tests-mirai-gui.result }}" = success ][ "${{ needs.build-mirai-gui.result }}" = success ]+ [ "${{ needs.build-uzu-pi-extension.result }}" = success ]diff --git a/.gitignore b/.gitignoreindex 1cedb0c35..ec7aca85c 100644--- a/.gitignore+++ b/.gitignore@@ -19,6 +19,9 @@ result# Python*__pycache__+# Node package cache+.pnpm-store/+# AI tools.cursor.claude/settings.local.jsondiff --git a/apps/mirai-gui/desktop/src/chat/transcript.rs b/apps/mirai-gui/desktop/src/chat/transcript.rsindex d87355bf2..084202fcc 100644--- a/apps/mirai-gui/desktop/src/chat/transcript.rs+++ b/apps/mirai-gui/desktop/src/chat/transcript.rs@@ -486,6 +486,7 @@ mod tests {reply(initial.with_block(ChatContentBlock::ToolCallCandidate {value: serde_json::json!(candidate).into(),+ identifier: None,}),false,),diff --git a/apps/uzu-pi-extension/.gitignore b/apps/uzu-pi-extension/.gitignorenew file mode 100644index 000000000..1ab415f57--- /dev/null+++ b/apps/uzu-pi-extension/.gitignore@@ -0,0 +1,3 @@+node_modules/+dist/+*.tgzdiff --git a/apps/uzu-pi-extension/README.md b/apps/uzu-pi-extension/README.mdnew file mode 100644index 000000000..51cbd9955--- /dev/null+++ b/apps/uzu-pi-extension/README.md@@ -0,0 +1,20 @@+# Uzu for Pi++Run local language models in Pi with uzu engine.++Requires Apple Silicon macOS, Pi 1.1 or later, and Node.js 22.19.0 or newer.++## Install++```sh+pi install npm:@trymirai/uzu-pi-extension+```++## Use++Open `/model` and select a model under the `uzu` provider. The picker shows download+status in the model details. Qwen3.8 27B Mirai Medium Quantization is recommended.++Uzu manages downloaded models in its own cache. To use manually converted models, set+`LOCAL_PATH` to a model directory or a directory containing models before starting+Pi.diff --git a/apps/uzu-pi-extension/package.json b/apps/uzu-pi-extension/package.jsonnew file mode 100644index 000000000..2ab207ffb--- /dev/null+++ b/apps/uzu-pi-extension/package.json@@ -0,0 +1,56 @@+{+ "name": "@trymirai/uzu-pi-extension",+ "version": "0.6.7",+ "description": "On-device Uzu models for the Pi coding agent",+ "type": "module",+ "license": "MIT",+ "repository": {+ "type": "git",+ "url": "https://github.com/trymirai/uzu.git",+ "directory": "apps/uzu-pi-extension"+ },+ "keywords": [+ "pi-package",+ "uzu",+ "local-models"+ ],+ "engines": {+ "node": ">=22.19.0"+ },+ "os": [+ "darwin"+ ],+ "cpu": [+ "arm64"+ ],+ "packageManager": "[email protected]",+ "pi": {+ "extensions": [+ "./src/index.ts"+ ]+ },+ "publishConfig": {+ "access": "public",+ "directory": "dist"+ },+ "scripts": {+ "build": "tsc -p tsconfig.build.json && node scripts/package.mjs",+ "typecheck": "tsc --noEmit",+ "test": "tsx --test tests/*.test.ts",+ "check": "pnpm typecheck && pnpm test && pnpm build"+ },+ "dependencies": {+ "@trymirai/uzu": "link:../../crates/legacy/uzu/bindings/typescript"+ },+ "peerDependencies": {+ "@earendil-works/pi-ai": "^1.1.0",+ "@earendil-works/pi-coding-agent": "^1.1.0"+ },+ "devDependencies": {+ "@earendil-works/pi-ai": "1.1.0",+ "@earendil-works/pi-coding-agent": "1.1.0",+ "@types/node": "^24.0.0",+ "tsx": "^4.20.0",+ "typescript": "~5.8.3"+ }+}diff --git a/apps/uzu-pi-extension/pnpm-lock.yaml b/apps/uzu-pi-extension/pnpm-lock.yamlnew file mode 100644index 000000000..d0425d374--- /dev/null+++ b/apps/uzu-pi-extension/pnpm-lock.yaml@@ -0,0 +1,1456 @@+lockfileVersion: '9.0'++settings:+ autoInstallPeers: true+ excludeLinksFromLockfile: false++importers:++ .:+ dependencies:+ '@trymirai/uzu':+ specifier: link:../../crates/legacy/uzu/bindings/typescript+ version: link:../../crates/legacy/uzu/bindings/typescript+ devDependencies:+ '@earendil-works/pi-ai':+ specifier: 1.1.0+ version: 1.1.0(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])+ '@earendil-works/pi-coding-agent':+ specifier: 1.1.0+ '@types/node':+ specifier: ^24.0.0+ version: 24.19.2+ tsx:+ specifier: ^4.20.0+ version: 4.23.15+ typescript:+ specifier: ~5.8.3+ version: 5.8.3+ publishDirectory: dist++packages:++ '@anthropic-ai/[email protected]':+ resolution: {integrity: sha512-MH7LB20kNpGLUpPTe2OG5XvL/KhX8HUO9XyBaFjgFk6TLS4Xjt0VPIuMKywe7POKKPslX+QxSlVok9e+8kG5Nw==}+ hasBin: true+ peerDependencies:+ zod: ^3.25.0 || ^4.0.0+ peerDependenciesMeta:+ zod:+ optional: true++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-IDl/lrPb90aH+pZFHGNDmgH9nAUQj5PlZH1sJ3w7RikctyjHSnY3oNjZhrLoaBoQn/rNK0zsP6OHEqEhj2tdLA==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-nslpzUCTI9b2IQMWSd6mSAXldBexPc8rr0d3dXUutXYFLFaTdsHa2GD7sJdsn3PhyjK0O5K8ypd6zi+RXXPVlg==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-rsRJcW2iFUhCgclyD6OSbMEaPl8Eu0qSeRZb2uuCK1ViZ66hl5wOe/p6Vr6MO3b+ydyKgFeqza2p6tZThvILlw==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-pnKdh0Br7OJPj+7qKjzgTfDTFW0RhEMKXNDR2jv9CAM4oHuG/4z3hRh8KtutLfpmItuRtDbKBmwWkDqHwwAWXw==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-kcjvAxqQEV8yuwkX58RelNn3ne65o7JgRWf3Z/gVvbi7Xuw1qSDLJlatrLD76+40cmRxmNkLxBoyp/vAQChQ5g==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-CeQHSbE2CnVdaHIJ/NayRaCecIER+Pm9faUPkmRATO54rCoMTEPSbVieGHzEwvm1dZM2HIs6qQZCznhayNTWJQ==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-WNttSyr6e8innPtBSty//CdnqJcgZN6eAbtw3v/CR2IObz6MYZEo8eusdtuCHJ2ujwFqC2bSPHVyhXGHx8HruQ==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-Yj6kZFJQdbR+PygTkqdVLPHHpdDpOS9GCYVcDCuweCRRig9LF24ELQBMfyUw1vspvrZJUfnOGVyMjhaL094ahw==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-FnpPhdY4qUGtX/8X6eKxh+uD+9Y19i9FYibgk4SBe0M935s9vocHkHzaxaMNk+YkHmvzgdu8cHA5SCoeU+kkew==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-TJF25Ndwy2HxcVj6oxMgp8qVlJAawlExHYBXqhuC6Zt2m+A7WpdkjBD6QglNbiArlteYljZHEY3R7KKmno6ISA==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-HFaKXA6ugPVvpR7W/3UT3hylWIPndqGMwduwNVrmGsRT5+OESHN8/ven4eeofraQF5Kxp8A8QgGVrv7iQsTRKQ==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-4yvdTVwPGKCnrwuwlYEmwLqQXKwk0unx3f/FvBi4FA9ruK/WGoPJyqfK7hOYurheIyQL44FR0ovgOHqvkfdpAQ==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-32d9EnnNM5UMwjVp/qnL0K4XYiZv+YlSbULI3BaoBdJvebAS6cD0bKfPJ7lzovN0qrelvmSh5jo/3tk0KoTuGQ==}+ engines: {node: '>= 14.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-B01jdXJtAW/XOiUzGdI2mz67WyEHbGEc9X6Euzw3bjLAa+txTqdZDER4X2FsIEcXzV2ABqudVZ5thali8tCCzQ==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-cFDXfEbXaGwbnpvR/DyqfxfjxpjXVRVthQLgkwvcfDS5u3+8G9WSj1X1Ag/pTRg/QOeOno1Tq3ycEdrKsCUcOA==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-Dv2TMWBshJ+tF6ahs2Sy5bh4Iabsd4GAQqVvE9XZmYmnoaVbpS2QKIKE/HRacc7bTtbjEEvP+laGzHvHlf1CiQ==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-wDU36ofhabZYu+d2KSriyDbwzxNoaOp+4dG0p3qkabk7HeALR5nIWyQbjoyJZLn3iqWkv/xNdyUfbfevDVDzzg==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-v/clNZzZnDxGyvpHMOGpJKVXFAExJzUNAAjaWGdcx8QAcXLGwTaOkw33p5SHAi0YAioK32xB3hWwOekRVfmfKg==}+ engines: {node: '>=20.0.0'}++ '@aws-sdk/[email protected]':+ resolution: {integrity: sha512-ctjVSyCMegrWfXlx6VqzSBFI6UqmQ5ZlnfMhdLIiWmhoH8UAQxSCP5N3OpG7X3k4LnS7ou74C4mt20+bfTW2aQ==}+ engines: {node: '>=20.0.0'}++ '@aws/[email protected]':+ resolution: {integrity: sha512-sl4Bm6yiMNYrZKkqqDFWN0UfnWhlS8ivKxrYl+6t0gCLrqr8y3B2IqZZbFRkfaVVp7C/baApyh71P+LeE1A2sQ==}+ engines: {node: '>=18.0.0'}++ '@babel/[email protected]':+ resolution: {integrity: sha512-NpugTzbKgGrNe45Ui32cN8vCFFMxVtP/9rtfwv0nP5FRDAjvYBxJcLVpYA9mr1jxP6cnIT7x9LUzuDTbWYYNRA==}+ engines: {node: '>=6.9.0'}++ '@earendil-works/[email protected]':+ resolution: {integrity: sha512-Xh3M5B4D9YotOyBXyVbZ/5WfoUwP6Kt+TheCIDLxLCUx72UExuxZF//hNmIQGD13EkAHf2svKpXr/rCCT1gXTg==}+ engines: {node: '>=22.19.0'}++ '@earendil-works/[email protected]':+ resolution: {integrity: sha512-pUR0vYmGhHkBQGeCeWjwVIA+l1+MrDu6m6VGuysjh6KBzBpovTV/MlcVrEIOaM8jv/slpAyUmuCEQl1jOBqHJQ==}+ engines: {node: '>=22.19.0'}++ '@earendil-works/[email protected]':+ resolution: {integrity: sha512-1T7LAkc/5Bvc0v6w4vAGVdCrli0o/E0pEmYKTnixu95vSFArBjvbhS/G4ZwI0RUePgf0Imcu0VyqlM4EcXxqfw==}+ engines: {node: '>=22.19.0'}+ hasBin: true++ '@earendil-works/[email protected]':+ resolution: {integrity: sha512-U5/1zXqfV1on7/9sbez03tT4Ru49Lvfb4+zXSDK3DBKF5cRk6KxvAOTOY3HJ4jimlZth+rs7vbJqN6iBQAoNuQ==}+ engines: {node: '>=22.19.0'}+ hasBin: true++ '@earendil-works/[email protected]':+ resolution: {integrity: sha512-UZF7IN0IIrf3bop2wTxrwoAakGctH6d3zdPwhPN+QHMECfoI5E6B6l6I17YGFZPj8cmGex6xX7mLCNJXRf7wsw==}+ engines: {node: '>=22.19.0'}++ '@earendil-works/[email protected]':+ resolution: {integrity: sha512-SeEi/4hdcHNgA9UWlefZl7ZZpm3dzi2OoxNjDHsBJ9o298LNOtbL4DGKgitlEj6uCTccvtw6f2hlCkTPVJ2RXg==}+ engines: {node: '>=22.19.0'}+ hasBin: true++ '@earendil-works/[email protected]':+ resolution: {integrity: sha512-ertUv40rUBgssTzw2lIPlejjIBdhvdg4uw+whWCDJYKYHU+ldp1ezhbB0BzwpBfnwef6/YN+m6eKLscj+r3p7w==}+ engines: {node: '>=22.19.0'}++ '@earendil-works/[email protected]':+ resolution: {integrity: sha512-NZ3aJE6dr7aTgzzXDe53OW7BsAsF8ZUKHPJTtnjaQcxtlagkUT0Ew8Qas0EvYQsZqR/KpF1lB+z209XBzIC7xg==}+ engines: {node: '>=22.19.0'}++ '@earendil-works/[email protected]':+ resolution: {integrity: sha512-OdJCiYklkL3TqmdmGKUflHoLy9zw18GBYO0DPASZvGlP4RkDXAZzLLDt7cidJ6wUR7DBrKb13rrekDsXgMF60g==}+ engines: {node: '>=22.19.0'}++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==}+ engines: {node: '>=18'}+ cpu: [ppc64]+ os: [aix]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==}+ engines: {node: '>=18'}+ cpu: [arm64]+ os: [android]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==}+ engines: {node: '>=18'}+ cpu: [arm]+ os: [android]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==}+ engines: {node: '>=18'}+ cpu: [x64]+ os: [android]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==}+ engines: {node: '>=18'}+ cpu: [arm64]+ os: [darwin]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==}+ engines: {node: '>=18'}+ cpu: [x64]+ os: [darwin]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==}+ engines: {node: '>=18'}+ cpu: [arm64]+ os: [freebsd]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==}+ engines: {node: '>=18'}+ cpu: [x64]+ os: [freebsd]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==}+ engines: {node: '>=18'}+ cpu: [arm64]+ os: [linux]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==}+ engines: {node: '>=18'}+ cpu: [arm]+ os: [linux]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==}+ engines: {node: '>=18'}+ cpu: [ia32]+ os: [linux]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==}+ engines: {node: '>=18'}+ cpu: [loong64]+ os: [linux]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==}+ engines: {node: '>=18'}+ cpu: [mips64el]+ os: [linux]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==}+ engines: {node: '>=18'}+ cpu: [ppc64]+ os: [linux]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==}+ engines: {node: '>=18'}+ cpu: [riscv64]+ os: [linux]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==}+ engines: {node: '>=18'}+ cpu: [s390x]+ os: [linux]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==}+ engines: {node: '>=18'}+ cpu: [x64]+ os: [linux]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==}+ engines: {node: '>=18'}+ cpu: [arm64]+ os: [netbsd]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==}+ engines: {node: '>=18'}+ cpu: [x64]+ os: [netbsd]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==}+ engines: {node: '>=18'}+ cpu: [arm64]+ os: [openbsd]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==}+ engines: {node: '>=18'}+ cpu: [x64]+ os: [openbsd]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==}+ engines: {node: '>=18'}+ cpu: [arm64]+ os: [openharmony]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==}+ engines: {node: '>=18'}+ cpu: [x64]+ os: [sunos]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==}+ engines: {node: '>=18'}+ cpu: [arm64]+ os: [win32]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==}+ engines: {node: '>=18'}+ cpu: [ia32]+ os: [win32]++ '@esbuild/[email protected]':+ resolution: {integrity: sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==}+ engines: {node: '>=18'}+ cpu: [x64]+ os: [win32]++ '@google/[email protected]':+ resolution: {integrity: sha512-+PDtco2/Z0ONdzCGekCoCT+O1VJS9xJQNN4XzQpXG/t3El/SWWMkCWlFRO1KmivOHPa4Q0VjUYu1HBKCZ/v33Q==}+ engines: {node: '>=20.0.0'}+ peerDependencies:+ '@modelcontextprotocol/sdk': ^1.25.2+ peerDependenciesMeta:+ '@modelcontextprotocol/sdk':+ optional: true++ '@protobufjs/[email protected]':+ resolution: {integrity: sha512-j+gKExEuLmKwvz3OgROXtrJ2UG2x8Ch2YZUxahh+s1F2HZ+wAceUNLkvy6zKCPVRkU++ZWQrdxsUeQXmcg4uoQ==}++ '@protobufjs/[email protected]':+ resolution: {integrity: sha512-AZkcAA5vnN/v4PDqKyMR5lx7hZttPDgClv83E//FMNhR2TMcLUhfRUBHCmSl0oi9zMgDDqRUJkSxO3wm85+XLg==}++ '@protobufjs/[email protected]':+ resolution: {integrity: sha512-zgXFLzW3Ap33e6d0Wlj4MGIm6Ce8O89n/apUaGNB/jx+hw+ruWEp7EwGUshdLKVRCxZW12fp9r40E1mQrf/34g==}++ '@protobufjs/[email protected]':+ resolution: {integrity: sha512-vW1GmwMZNnL+gMRaovlh9yZX74kc+TTU3FObkkurpMaRtBfLP3ldjS9KQWlwZgraRE0+dheEEoAxdzcJQ8eXZg==}++ '@protobufjs/[email protected]':+ resolution: {integrity: sha512-GpptLrs57adMSuHi3VNj0mAF8dwh36LMaYF6XyJ6JMWlVsc+t42tm1HSEDmOs3A8fC9yyeisgLhsTVQokOZ0zw==}++ '@protobufjs/[email protected]':+ resolution: {integrity: sha512-Ddb+kVXlXst9d+R9PfTIxh1EdNkgoRe5tOX6t01f1lYWOvJnSPDBlG241QLzcyPdoNTsblLUdujGSE4RzrTZGQ==}++ '@protobufjs/[email protected]':+ resolution: {integrity: sha512-6JOcJ5Tm08dOHAbdR3GrvP+yUUfkjG5ePsHYczMFLq3ZmMkAD98cDgcT2iA1lJ9NVwFd4tH/iSSoe44YWkltEA==}++ '@protobufjs/[email protected]':+ resolution: {integrity: sha512-0kELaGSIDBKvcgS4zkjz1PeddatrjYcmMWOlAuAPwAeccUrPHdUqo/J6LiymHHEiJT5NrF1UVwxY14f+fy4WQw==}++ '@protobufjs/[email protected]':+ resolution: {integrity: sha512-b1UQwcEZ4yCnMCD8DAL1VlbvBJE9/IX4FTIp7BG1xYpf29SLazLSrqUkj4w7Y5y7cCVP6E5tcqqcI0xemPkHug==}++ '@silvia-odwyer/[email protected]':+ resolution: {integrity: sha512-bnly4BKB3KDTFxrUIcgCLbaeVVS8lrAkri1pEzskpmxu9MdfGQTy8b8EgcD83ywD3RPMsIulY8xJH5Awa+t9fA==}++ '@smithy/[email protected]':+ resolution: {integrity: sha512-3hJiwG1F5t1SoXqp/yOCmYXziyx7ZVqF63S5W6YU201e/qwVaHPex+E/3vvyY9a3ORq/b+BydeNZpP2saQ9X2g==}+ engines: {node: '>=18.0.0'}++ '@smithy/[email protected]':+ resolution: {integrity: sha512-A9uSdn72ozbRUSit0eib0TW7nXuNPlaeM0zcGkJ+nE6tFcSDbnmtwoxbTCFBukVQcszDAyvsd7+rTduPTXpygg==}+ engines: {node: '>=18.0.0'}++ '@smithy/[email protected]':+ resolution: {integrity: sha512-ycSJu3tFAQ4v04CBB0agqFMVsSQ1iG3yw+SpgxRqKfaURpQD4CZ8Wn0zPMmSnOuTpTh65Vz+EA0rMrw089wvkA==}+ engines: {node: '>=18.0.0'}++ '@smithy/[email protected]':+ resolution: {integrity: sha512-ThMkboGeONWXAelq9FvGsuJC4rOi+qyC4/zhUF58xYpxUg5sQKx2VXZYJmtNjr4dSuBJ1HeJXETQILCz3wOHvw==}+ engines: {node: '>=18.0.0'}++ '@smithy/[email protected]':+ resolution: {integrity: sha512-tHy0K0VtqNd5Y7Y41h0a0Lhh0L1GzC08dTWg0F7vRJWFtTENg7IZikf3wQkanYIRdb7ngoIPMTmqgUi401fEeQ==}+ engines: {node: '>=18.0.0'}++ '@smithy/[email protected]':+ resolution: {integrity: sha512-r7jh49VJxGerfAcTQA6gXcKc+98zOp/tqRwzYjgOE+iSQsP6cEU1hq2QzbuipmP68QtYdY9wKEhiCQZIzHgZ4Q==}+ engines: {node: '>=18.0.0'}++ '@stablelib/[email protected]':+ resolution: {integrity: sha512-1bnPQqSxSuc3Ii6MhBysoWCg58j97aUjuCSZrGSmDxNqtytIi0k8utUenAwTZN4V5mXXYGsVUI9zeBqy+jBOSQ==}++ '@types/[email protected]':+ resolution: {integrity: sha512-93d49wCWJkCyN87MxLpNN6KXPCwFw7bVUg79mGUV1OS40JGWwVCr8n61DXCSqz7CV6/VfPkb7xec7uP2M+QHWQ==}++ '@types/[email protected]':+ resolution: {integrity: sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==}++ resolution: {integrity: sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ==}+ engines: {node: '>= 14'}++ resolution: {integrity: sha512-TQf59BsZnytt8GdJKLPfUZ54g/iaUL2OWDSFCCvMOhsHduDQxO8xC4PNeyIkVcA5KwL2phPSv0douC0fgWzmnA==}+ engines: {node: '>= 20'}++ resolution: {integrity: sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA==}+ engines: {node: 18 || 20 || >=22}++ resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==}++ resolution: {integrity: sha512-Ko0uX15oIUS7wJ3Rb30Fs6SkVbLmPBAKdlm7q9+ak9bbIeFf0MwuBsQV6z7+X768/cHsfg+WlysDWJcmthjsjQ==}++ resolution: {integrity: sha512-tzPjzCxygAKWFOJP011oxFHs57HzIhOEracIgAePE4pqB3LikALKnSzUyU4MGs9/iCEUuHlAJTjTc5M+u7YEGg==}++ resolution: {integrity: sha512-YovQ3rzhaLMIrDjNDMkNS01tea93qhEhG5xy8f6+R0l+dw3Ki+5sCoIoI942iuLZTHWogWktgwVDhU09iNEimQ==}+ engines: {node: 20 || >=22}++ resolution: {integrity: sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==}++ resolution: {integrity: sha512-2uNTXIuTTxk7ciZgAU1BQcgnchcG0xXnrs6jzkQfj9SsRa9M2s5zE8WT96hS6KmG4MzWHSrvH43DF1m4XRkrFg==}+ engines: {node: '>=22'}++ resolution: {integrity: sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==}+ engines: {node: '>= 8'}++ resolution: {integrity: sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==}+ engines: {node: '>= 12'}++ resolution: {integrity: sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==}+ engines: {node: '>=6.0'}+ peerDependencies:+ supports-color: '*'+ peerDependenciesMeta:+ supports-color:+ optional: true++ resolution: {integrity: sha512-DPi0FmjiSU5EvQV0++GFDOJ9ASQUVFh5kD+OzOnYdi7n3Wpm9hWWGfB/O2blfHcMVTL5WkQXSnRiK9makhrcnw==}+ engines: {node: '>=0.3.1'}++ resolution: {integrity: sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==}++ resolution: {integrity: sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==}+ engines: {node: '>=18'}+ hasBin: true++ resolution: {integrity: sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==}++ resolution: {integrity: sha512-n11RGP/lrWEFI/bWdygLxhI+pVeo1ZYIVwvvPkW7azl/rOy+F3HYRZ2K5zeE9mmkhQppyv9sQFx0JM9UabnpPQ==}++ resolution: {integrity: sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==}+ engines: {node: ^12.20 || >= 14.13}++ resolution: {integrity: sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==}+ engines: {node: '>=12.20.0'}++ resolution: {integrity: sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==}+ engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0}+ os: [darwin]++ resolution: {integrity: sha512-kB3rzJV7d9juLZh8/56QTXCwQfxyhdOMdyYk1HdQKFtF8TJTDTZQJtixWIwXdE9Jji91mC41DUNpjleo4L4eAQ==}+ engines: {node: '>=18'}++ resolution: {integrity: sha512-zV/5HKTfCeKWnxG0Dmrw51hEWFGfcF2xiXqcA3+J90WDuP0SvoiSO5ORvcBsifmx/FoIjgQN3oNOGaQ5PhLFkg==}+ engines: {node: '>=18'}++ resolution: {integrity: sha512-QRbvDIbx6YklUe6RxeTeleMR0yv3cYH6PsPZHcnVn7xv7zO1BHN8r0XETu8n6Ye3Q+ahtSarc3WgtNWmehIBfA==}+ engines: {node: '>=18'}++ resolution: {integrity: sha512-i1ydyHrqcIxXkWh/uBmVkzCvIuq5yiK2ATndIe5XxKholrG/MTYP9xGYka4sQhrbIAgGjL2B6NOE7rFaiF3fXw==}+ engines: {node: '>=18'}++ resolution: {integrity: sha512-eAmLkjDjAFCVXg7A1unxHsLf961m6y17QFqXqAXGj/gVkKFrEICfStRfwUlGNfeCEjNRa32JEWOUTlYXPyyKvA==}+ engines: {node: '>=14'}++ resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==}++ resolution: {integrity: sha512-/4KopAbsjvuRP9MdPtlDjOHUmUVEohOX73JNcsWpzAtFxh+bq5+Dhb6gzvRieLDwIPQIR3/vy8V1NNTuz4Zsmg==}+ engines: {node: '>=18'}++ resolution: {integrity: sha512-tzcUFauisWKNHaRkN4Wjl/ZA07gENAjFl3J/c480dprkGTg5EQstgaNFqBfUqCq54kZRIEcreTsAgF/m2quD7A==}++ resolution: {integrity: sha512-Hc+ghLoSt6QaYZUv0WBiIvmMDZuZZ7oaDvdH8MbfOO4lOsxdXLEvuC6ePoGs9H1X9oCLyq6+NVN0MKqD+ydxyg==}+ engines: {node: ^20.17.0 || >=22.9.0}++ resolution: {integrity: sha512-2NxoveTT58mjYT4n3RPTEfCZGLMbidoO8XEieXfpSYxu+PQJ1qpx4ypwH6N+uF9twBPIvRRgvkvW5HUTYWENig==}+ engines: {node: '>= 20'}++ resolution: {integrity: sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==}+ engines: {node: '>= 14'}++ resolution: {integrity: sha512-ag87y7cJJ9/3+GxFr8Oy4O5faDsGRGnBGsJj/YjOSsSx/5eadKLYTMPlzuR6obgoCDDm0abAAZitXXQkMOPSpA==}+ engines: {node: '>= 20'}++ resolution: {integrity: sha512-YYNsSlXBjMk92SKnkwvB5LOVSa6OznlFUGcsvrFgNJbJCd0M1XKeFVRc8ZByeCqz32FivYNHJVooLmdqrmvp/Q==}+ engines: {node: '>= 4'}++ resolution: {integrity: sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==}++ resolution: {integrity: sha512-AC/7JofJvZGrrneWNaEnJeOLUx+JlGt7tNa0wZiRPT4MY1wmfKjt2+6O2p2uz2+skll8OZZmJMNqeke7kKbNgQ==}+ hasBin: true++ resolution: {integrity: sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==}++ resolution: {integrity: sha512-+DWg8jCJG2TEnpy7kOm/7/AxaYoaRbjVB4LFZLySZlWn8exGs3A4OLJR966cVvU26N7X9TWxl+Jsw7dzAqKT6g==}+ engines: {node: '>=16'}++ resolution: {integrity: sha512-hRF04fqJIP8Abbkq5NKGN0Bbr3JxlQ+qhZufXVr0DvujKy93ZCbXZMHDL4EOtodSbCWxOqR8MS1tXA5hwqCXDg==}++ resolution: {integrity: sha512-EKI/M/yqPncGUUh44xz0PxSidXFr/+r0pA70+gIYhjv+et7yxM+s29Y+VGDkovRofQem0fs7Uvf4+YmAdyRduA==}++ resolution: {integrity: sha512-mNAgZ1GmyNhD7AuqnTG3/VQ26o760+ZYBPKjPvugO8+nLbYfX6TVpJPseBvopbdY+qpZ/lKUnmEc1LeZYS3QAA==}++ resolution: {integrity: sha512-U4N8FgzmWxc8k1VH8Kr6lQg18U7Fjvby6wXHVRX/ZZ7IwWbRMgrRbP0Wrb5q5NVinryp4SQampHKdvtecItxUg==}+ engines: {node: 20 || >=22}++ resolution: {integrity: sha512-HnslJfsZkRPBDJRHvVtAaWlZHEpSu7u8LgQuJCELjRKuWR+hpq4A7sLq3p8HaI9ypVoXDXxV34CsQJEe1+J5Aw==}+ engines: {node: '>= 20'}+ hasBin: true++ resolution: {integrity: sha512-vpLQEs+VLCr1nU0BXS07maYoFwlDAH0gngQuuttxIwutDFEMHq2blX+8vpgxDdK3J1PwjCJiep77OitTZ4Ll1A==}+ engines: {node: 18 || 20 || >=22}++ resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==}++ resolution: {integrity: sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==}+ engines: {node: '>=10.5.0'}+ deprecated: Use your platform's native DOMException instead++ resolution: {integrity: sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==}+ engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}++ resolution: {integrity: sha512-MX2s3u2L5racTO0CC/SWpCOasJQBCJrqLKXK+l82cAhdeF8mPMBEe/gxMm0ZFa2xpKpOFLRjxv5afYEZbBXmbQ==}+ engines: {node: '>=22.0.0'}+ peerDependencies:+ '@aws-sdk/credential-provider-node': '>=3.972.0 <4'+ '@smithy/hash-node': '>=4.3.0 <5'+ '@smithy/signature-v4': '>=5.4.0 <6'+ undici: '>=5 <9'+ ws: ^8.21.0+ zod: ^3.25 || ^4.0+ peerDependenciesMeta:+ '@aws-sdk/credential-provider-node':+ optional: true+ '@smithy/hash-node':+ optional: true+ '@smithy/signature-v4':+ optional: true+ undici:+ optional: true+ ws:+ optional: true+ zod:+ optional: true++ resolution: {integrity: sha512-312Id396EbJdvRONlngUx0NydfrIQ5lsYu0znKVUzVvArzEIt08V1qhtyESbGVd1FGX7UKtiFp5uwKZdM8wIuQ==}+ engines: {node: '>=8'}++ resolution: {integrity: sha512-Njv/59hHaokb/hRUjce3Hdv12wd60MtM9Z5Olmn+nehe0QDAsRtRbJPvJ0Z91TusF0SuZRIvnM+S4l6EIP8leA==}++ resolution: {integrity: sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==}+ engines: {node: '>=8'}++ resolution: {integrity: sha512-TjNPblN4BwAWMXU8s9AEz4JmQxnD1NNL7bNOY/AKUzyamc379FWASUhc/K1pL2noVb+XmZKLL68cjzLsiOAMaA==}++ resolution: {integrity: sha512-dYDWdjSl5RNb7SgPxGQcRU+GtvP7s2fpkrY0r432PcOIaZ0/rBcxEZnQN67iJhFuQiVw754JDoPruPCNdGsbjg==}+ engines: {node: '>=12.0.0'}++ resolution: {integrity: sha512-N8IBcM3UgCVzz2L2Lqv8DVntDnnC8/hiV4nEDUPkqq72TPUgYWjQc+bdZlBPZK9LzPAvOY//gAt0S0DApoOXWQ==}+ engines: {node: '>= 20'}+ peerDependencies:+ kerberos: ^2.0.0+ peerDependenciesMeta:+ kerberos:+ optional: true++ resolution: {integrity: sha512-FCqGtGOrMgzUiIrMNMA2YnsOxCNwo31dzqXvclXUC6xeT35NJLKXQJsvbeCTjvoFAwZgEAPg8U6+KAPDGXn8Mg==}++ resolution: {integrity: sha512-9LkiTwjUh6rT555DtE9rTX+BKByPfrMzEAtnlEtdEwr3Nkffwiihqe2bWADg+OQRjt9gl6ICdmB/ZFDCGAtSow==}+ engines: {node: '>= 4'}++ resolution: {integrity: sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg==}+ engines: {node: '>= 4'}++ resolution: {integrity: sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==}++ resolution: {integrity: sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==}+ engines: {node: '>=10'}+ hasBin: true++ resolution: {integrity: sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==}+ engines: {node: '>=8'}++ resolution: {integrity: sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==}+ engines: {node: '>=8'}++ resolution: {integrity: sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==}++ resolution: {integrity: sha512-bCbX9ZEyFkWPsRz7Bl3NuQUJohmwGSev/yhr7vhaGPlc4AfIrspIRa6cPTBuI1ItmrTDJ4d/S2hCsfe4+vQGnQ==}++ resolution: {integrity: sha512-FPAhNPFMrkwz76P7cdjdmiShwMynZYN6SgOujD1urY4oNm80Ou9oMdmbR45LotcKOXoy7wSmHkRFE6Mxbrhefw==}++ resolution: {integrity: sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==}++ resolution: {integrity: sha512-Yiex1Ovn8z2xPpOWckIiysV1SSyRMY9BkLF++q0yKiDxCqRhosKfMg3janKkiLBwZ5c/YryloKwGZcrEmtwxKw==}+ engines: {node: '>=18.0.0'}+ hasBin: true++ resolution: {integrity: sha512-zu+jc1pcy4UiNThxikUr36f0Rybk9PEeCg/NE6adeWr/SKsdNO4EzZHYRDlv2YCVAfj3Odq3dESSo/jNyoBXzA==}++ resolution: {integrity: sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ==}+ engines: {node: '>=14.17'}+ hasBin: true++ resolution: {integrity: sha512-WRNW+sJgj5OBN4/0JpHFqtqzhpbnV0GuB+OozA9gCL7a993SmU+1JBZCzLNxYsbMfIeDL+lTsphD5jN5N+n0zg==}++ resolution: {integrity: sha512-/y4/bH9YNU5hi9NIrpOuvGXFcxrj3CMrV+/AYpowAYTpHn8gX/XPFjNy766FPoYY0miQhdW977JFWKGNhBdwyQ==}+ engines: {node: '>=22.19.0'}++ resolution: {integrity: sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==}+ engines: {node: '>= 8'}++ resolution: {integrity: sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==}+ engines: {node: '>= 8'}+ hasBin: true++ resolution: {integrity: sha512-Ydggc987+RO0AnWtZ/7Wq9FtNvcrL1b/RO0ud9mWjUPgDrsAAwQSF51sm2hm1XofbU/4jkpGEsLFsZZxU+1DOg==}+ engines: {node: '>=10.0.0'}+ peerDependencies:+ bufferutil: ^4.0.1+ utf-8-validate: '>=5.0.2'+ peerDependenciesMeta:+ bufferutil:+ optional: true+ utf-8-validate:+ optional: true++ resolution: {integrity: sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA==}+ engines: {node: '>= 14.6'}+ hasBin: true++snapshots:++ '@anthropic-ai/[email protected]':+ dependencies:+ json-schema-to-ts: 3.1.1+ standardwebhooks: 1.1.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/core': 3.978.3+ '@aws-sdk/credential-provider-node': 3.972.86+ '@aws-sdk/eventstream-handler-node': 3.972.36+ '@aws-sdk/middleware-eventstream': 3.972.31+ '@aws-sdk/middleware-websocket': 3.972.56+ '@aws-sdk/token-providers': 3.1127.0+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/fetch-http-handler': 5.8.0+ '@smithy/node-http-handler': 4.12.1+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/types': 3.974.6+ '@aws-sdk/xml-builder': 3.972.41+ '@aws/lambda-invoke-store': 0.3.0+ '@smithy/core': 3.36.0+ '@smithy/signature-v4': 5.7.4+ '@smithy/types': 4.19.0+ bowser: 2.14.1+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/core': 3.978.3+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/core': 3.978.3+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/fetch-http-handler': 5.8.0+ '@smithy/node-http-handler': 4.12.1+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/core': 3.978.3+ '@aws-sdk/credential-provider-env': 3.972.74+ '@aws-sdk/credential-provider-http': 3.972.76+ '@aws-sdk/credential-provider-login': 3.972.81+ '@aws-sdk/credential-provider-process': 3.972.74+ '@aws-sdk/credential-provider-sso': 3.973.18+ '@aws-sdk/credential-provider-web-identity': 3.972.80+ '@aws-sdk/nested-clients': 3.997.48+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/credential-provider-imds': 4.5.2+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/core': 3.978.3+ '@aws-sdk/nested-clients': 3.997.48+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/credential-provider-env': 3.972.74+ '@aws-sdk/credential-provider-http': 3.972.76+ '@aws-sdk/credential-provider-ini': 3.973.19+ '@aws-sdk/credential-provider-process': 3.972.74+ '@aws-sdk/credential-provider-sso': 3.973.18+ '@aws-sdk/credential-provider-web-identity': 3.972.80+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/credential-provider-imds': 4.5.2+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/core': 3.978.3+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/core': 3.978.3+ '@aws-sdk/nested-clients': 3.997.48+ '@aws-sdk/token-providers': 3.1150.0+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/core': 3.978.3+ '@aws-sdk/nested-clients': 3.997.48+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/core': 3.978.3+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/fetch-http-handler': 5.8.0+ '@smithy/signature-v4': 5.7.4+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/core': 3.978.3+ '@aws-sdk/signature-v4-multi-region': 3.996.48+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/fetch-http-handler': 5.8.0+ '@smithy/node-http-handler': 4.12.1+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/types': 3.974.6+ '@smithy/signature-v4': 5.7.4+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/core': 3.978.3+ '@aws-sdk/nested-clients': 3.997.48+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@aws-sdk/core': 3.978.3+ '@aws-sdk/nested-clients': 3.997.48+ '@aws-sdk/types': 3.974.6+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws-sdk/[email protected]':+ dependencies:+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@aws/[email protected]': {}++ '@babel/[email protected]': {}++ '@earendil-works/[email protected]':+ dependencies:+ typebox: 1.3.27++ '@earendil-works/[email protected](@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])':+ dependencies:+ '@earendil-works/pi-ai': 1.2.0(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])+ typebox: 1.3.27+ transitivePeerDependencies:+ - '@aws-sdk/credential-provider-node'+ - '@modelcontextprotocol/sdk'+ - '@smithy/hash-node'+ - '@smithy/signature-v4'+ - bufferutil+ - kerberos+ - supports-color+ - undici+ - utf-8-validate+ - ws+ - zod++ '@earendil-works/[email protected](@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])':+ dependencies:+ '@anthropic-ai/sdk': 0.129.0+ '@aws-sdk/client-bedrock-runtime': 3.1127.0+ '@earendil-works/pi-telemetry': 1.2.0+ '@google/genai': 2.21.0+ '@smithy/node-http-handler': 4.12.1+ http-proxy-agent: 9.1.0+ https-proxy-agent: 9.1.0+ openai: 7.19.0(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])+ partial-json: 0.1.7+ typebox: 1.3.27+ transitivePeerDependencies:+ - '@aws-sdk/credential-provider-node'+ - '@modelcontextprotocol/sdk'+ - '@smithy/hash-node'+ - '@smithy/signature-v4'+ - bufferutil+ - kerberos+ - supports-color+ - undici+ - utf-8-validate+ - ws+ - zod++ '@earendil-works/[email protected](@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])':+ dependencies:+ '@anthropic-ai/sdk': 0.129.0+ '@aws-sdk/client-bedrock-runtime': 3.1127.0+ '@earendil-works/pi-telemetry': 1.2.0+ '@google/genai': 2.21.0+ '@smithy/node-http-handler': 4.12.1+ http-proxy-agent: 9.1.0+ https-proxy-agent: 9.1.0+ openai: 7.19.0(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])+ partial-json: 0.1.7+ typebox: 1.3.27+ transitivePeerDependencies:+ - '@aws-sdk/credential-provider-node'+ - '@modelcontextprotocol/sdk'+ - '@smithy/hash-node'+ - '@smithy/signature-v4'+ - bufferutil+ - kerberos+ - supports-color+ - undici+ - utf-8-validate+ - ws+ - zod++ '@earendil-works/[email protected]':+ dependencies:+ quickjs-wasi: 3.6.2++ '@earendil-works/[email protected](@aws-sdk/[email protected])(@smithy/[email protected])([email protected])':+ dependencies:+ '@earendil-works/chord': 1.2.0+ '@earendil-works/pi-agent-core': 1.2.0(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])+ '@earendil-works/pi-ai': 1.1.0(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])+ '@earendil-works/pi-codemode': 1.2.0+ '@earendil-works/pi-mcp': 1.2.0+ '@earendil-works/pi-tui': 1.2.0+ '@silvia-odwyer/photon-node': 0.3.4+ brace-expansion: 5.0.12+ chalk: 6.0.0+ cross-spawn: 7.0.6+ diff: 8.0.4+ grok-mermaid: 0.2.3+ highlight.js: 10.7.3+ hosted-git-info: 9.0.3+ ignore: 7.0.8+ jiti: 2.7.0+ minimatch: 10.2.6+ proper-lockfile: 4.1.2+ quickjs-wasi: 3.6.2+ semver: 7.8.5+ typebox: 1.3.27+ undici: 8.10.2+ yaml: 2.9.0+ transitivePeerDependencies:+ - '@aws-sdk/credential-provider-node'+ - '@modelcontextprotocol/sdk'+ - '@smithy/hash-node'+ - '@smithy/signature-v4'+ - bufferutil+ - kerberos+ - supports-color+ - utf-8-validate+ - ws+ - zod++ '@earendil-works/[email protected]':+ dependencies:+ cross-spawn: 7.0.6++ '@earendil-works/[email protected]': {}++ '@earendil-works/[email protected]':+ dependencies:+ get-east-asian-width: 1.6.0+ marked: 18.0.11++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@esbuild/[email protected]':+ optional: true++ '@google/[email protected]':+ dependencies:+ google-auth-library: 10.9.1+ p-retry: 4.6.2+ protobufjs: 7.6.6+ ws: 8.22.0+ transitivePeerDependencies:+ - bufferutil+ - supports-color+ - utf-8-validate++ '@protobufjs/[email protected]': {}++ '@protobufjs/[email protected]': {}++ '@protobufjs/[email protected]': {}++ '@protobufjs/[email protected]': {}++ '@protobufjs/[email protected]':+ dependencies:+ '@protobufjs/aspromise': 1.1.2++ '@protobufjs/[email protected]': {}++ '@protobufjs/[email protected]': {}++ '@protobufjs/[email protected]': {}++ '@protobufjs/[email protected]': {}++ '@silvia-odwyer/[email protected]': {}++ '@smithy/[email protected]':+ dependencies:+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@smithy/[email protected]':+ dependencies:+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@smithy/[email protected]':+ dependencies:+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@smithy/[email protected]':+ dependencies:+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@smithy/[email protected]':+ dependencies:+ '@smithy/core': 3.36.0+ '@smithy/types': 4.19.0+ tslib: 2.8.1++ '@smithy/[email protected]':+ dependencies:+ tslib: 2.8.1++ '@stablelib/[email protected]': {}++ '@types/[email protected]':+ dependencies:+ undici-types: 7.24.6++ '@types/[email protected]': {}++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ dependencies:+ balanced-match: 4.0.4++ [email protected]: {}++ [email protected]: {}++ dependencies:+ path-key: 3.1.1+ shebang-command: 2.0.0+ which: 2.0.2++ [email protected]: {}++ dependencies:+ ms: 2.1.3++ [email protected]: {}++ dependencies:+ safe-buffer: 5.2.1++ optionalDependencies:+ '@esbuild/aix-ppc64': 0.28.2+ '@esbuild/android-arm': 0.28.2+ '@esbuild/android-arm64': 0.28.2+ '@esbuild/android-x64': 0.28.2+ '@esbuild/darwin-arm64': 0.28.2+ '@esbuild/darwin-x64': 0.28.2+ '@esbuild/freebsd-arm64': 0.28.2+ '@esbuild/freebsd-x64': 0.28.2+ '@esbuild/linux-arm': 0.28.2+ '@esbuild/linux-arm64': 0.28.2+ '@esbuild/linux-ia32': 0.28.2+ '@esbuild/linux-loong64': 0.28.2+ '@esbuild/linux-mips64el': 0.28.2+ '@esbuild/linux-ppc64': 0.28.2+ '@esbuild/linux-riscv64': 0.28.2+ '@esbuild/linux-s390x': 0.28.2+ '@esbuild/linux-x64': 0.28.2+ '@esbuild/netbsd-arm64': 0.28.2+ '@esbuild/netbsd-x64': 0.28.2+ '@esbuild/openbsd-arm64': 0.28.2+ '@esbuild/openbsd-x64': 0.28.2+ '@esbuild/openharmony-arm64': 0.28.2+ '@esbuild/sunos-x64': 0.28.2+ '@esbuild/win32-arm64': 0.28.2+ '@esbuild/win32-ia32': 0.28.2+ '@esbuild/win32-x64': 0.28.2++ [email protected]: {}++ [email protected]: {}++ dependencies:+ node-domexception: 1.0.0+ web-streams-polyfill: 3.3.3++ dependencies:+ fetch-blob: 3.2.0++ optional: true++ dependencies:+ extend: 3.0.2+ https-proxy-agent: 7.0.6+ node-fetch: 3.3.2+ transitivePeerDependencies:+ - supports-color++ dependencies:+ gaxios: 7.3.1+ google-logging-utils: 1.1.3+ json-bigint: 1.0.0+ transitivePeerDependencies:+ - supports-color++ [email protected]: {}++ dependencies:+ base64-js: 1.5.1+ ecdsa-sig-formatter: 1.0.11+ gaxios: 7.3.1+ gcp-metadata: 8.1.2+ google-logging-utils: 1.1.3+ jws: 4.0.1+ transitivePeerDependencies:+ - supports-color++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ dependencies:+ lru-cache: 11.5.3++ dependencies:+ agent-base: 9.0.0+ debug: 4.4.3+ proxy-agent-negotiate: 1.1.0+ transitivePeerDependencies:+ - kerberos+ - supports-color++ dependencies:+ agent-base: 7.1.4+ debug: 4.4.3+ transitivePeerDependencies:+ - supports-color++ dependencies:+ agent-base: 9.0.0+ debug: 4.4.3+ proxy-agent-negotiate: 1.1.0+ transitivePeerDependencies:+ - kerberos+ - supports-color++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ dependencies:+ bignumber.js: 9.3.1++ dependencies:+ '@babel/runtime': 7.29.10+ ts-algebra: 2.0.0++ dependencies:+ buffer-equal-constant-time: 1.0.1+ ecdsa-sig-formatter: 1.0.11+ safe-buffer: 5.2.1++ dependencies:+ jwa: 2.0.1+ safe-buffer: 5.2.1++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ dependencies:+ brace-expansion: 5.0.12++ [email protected]: {}++ [email protected]: {}++ dependencies:+ data-uri-to-buffer: 4.0.1+ fetch-blob: 3.2.0+ formdata-polyfill: 4.0.10++ [email protected](@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected]):+ optionalDependencies:+ '@aws-sdk/credential-provider-node': 3.972.86+ '@smithy/signature-v4': 5.7.4+ undici: 8.10.2+ ws: 8.22.0++ dependencies:+ '@types/retry': 0.12.0+ retry: 0.13.1++ [email protected]: {}++ [email protected]: {}++ dependencies:+ graceful-fs: 4.2.11+ retry: 0.12.0+ signal-exit: 3.0.7++ dependencies:+ '@protobufjs/aspromise': 1.1.2+ '@protobufjs/base64': 1.1.2+ '@protobufjs/codegen': 2.0.5+ '@protobufjs/eventemitter': 1.1.1+ '@protobufjs/fetch': 1.1.1+ '@protobufjs/float': 1.0.2+ '@protobufjs/path': 1.1.2+ '@protobufjs/pool': 1.1.0+ '@protobufjs/utf8': 1.1.2+ '@types/node': 24.19.2+ long: 5.3.2++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ dependencies:+ shebang-regex: 3.0.0++ [email protected]: {}++ [email protected]: {}++ dependencies:+ '@stablelib/base64': 1.0.1+ fast-sha256: 1.3.0++ [email protected]: {}++ [email protected]: {}++ dependencies:+ esbuild: 0.28.2+ optionalDependencies:+ fsevents: 2.3.3++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ [email protected]: {}++ dependencies:+ isexe: 2.0.0++ [email protected]: {}++ [email protected]: {}diff --git a/apps/uzu-pi-extension/scripts/check-package.mjs b/apps/uzu-pi-extension/scripts/check-package.mjsnew file mode 100644index 000000000..4bb3a2213--- /dev/null+++ b/apps/uzu-pi-extension/scripts/check-package.mjs@@ -0,0 +1,41 @@+import assert from "node:assert/strict";+import { readFile } from "node:fs/promises";+import { execFileSync } from "node:child_process";++const root = new URL("../", import.meta.url);+const manifest = JSON.parse(+ await readFile(new URL("dist/package.json", root), "utf8"),+);+assert.equal(manifest.dependencies["@trymirai/uzu"], manifest.version);+assert.deepEqual(manifest.pi, { extensions: ["./index.js"] });+assert.equal(manifest.devDependencies, undefined);+assert.equal(manifest.publishConfig, undefined);+assert.equal(JSON.stringify(manifest).includes("link:"), false);+const archive = new URL(+ `dist/trymirai-uzu-pi-extension-${manifest.version}.tgz`,+ root,+);+const files = execFileSync("tar", ["-tzf", archive.pathname], {+ encoding: "utf8",+})+ .trim()+ .split("\n");+assert.ok(files.includes("package/index.js"));+assert.ok(files.includes("package/LICENSE"));+assert.ok(+ files.every((file) =>+ /^package\/(?:[^/]+\.(?:js|ts)|README\.md|LICENSE|package\.json)$/.test(+ file,+ ),+ ),+ files.join("\n"),+);+const packed = JSON.parse(+ execFileSync("tar", ["-xOf", archive.pathname, "package/package.json"], {+ encoding: "utf8",+ }),+);+assert.deepEqual(packed.dependencies, manifest.dependencies);+console.log(+ `Verified ${manifest.name}@${manifest.version} with @trymirai/uzu@${manifest.version}`,+);diff --git a/apps/uzu-pi-extension/scripts/package.mjs b/apps/uzu-pi-extension/scripts/package.mjsnew file mode 100644index 000000000..274f9daaa--- /dev/null+++ b/apps/uzu-pi-extension/scripts/package.mjs@@ -0,0 +1,28 @@+import { readFile, writeFile, copyFile } from "node:fs/promises";++const readJson = async (path) =>+ JSON.parse(await readFile(new URL(path, import.meta.url), "utf8"));+const source = await readJson("../package.json");+const uzu = await readJson(+ "../../../crates/legacy/uzu/bindings/typescript/package.json",+);+if (source.version !== uzu.version) {+ throw new Error("Extension and Uzu versions differ; run cargo tools sync");+}+const { scripts, devDependencies, publishConfig, packageManager, ...manifest } =+ source;+manifest.dependencies = { "@trymirai/uzu": uzu.version };+manifest.pi = { extensions: ["./index.js"] };+manifest.files = ["*.js", "*.d.ts", "README.md", "LICENSE"];+await writeFile(+ new URL("../dist/package.json", import.meta.url),+ `${JSON.stringify(manifest, null, 2)}\n`,+);+await copyFile(+ new URL("../README.md", import.meta.url),+ new URL("../dist/README.md", import.meta.url),+);+await copyFile(+ new URL("../../../LICENSE", import.meta.url),+ new URL("../dist/LICENSE", import.meta.url),+);diff --git a/apps/uzu-pi-extension/src/abort.ts b/apps/uzu-pi-extension/src/abort.tsnew file mode 100644index 000000000..b8d1187a1--- /dev/null+++ b/apps/uzu-pi-extension/src/abort.ts@@ -0,0 +1,17 @@+export async function abortable<T>(+ pending: Promise<T>,+ signal?: AbortSignal,+): Promise<T> {+ if (!signal) return pending;+ let abort!: () => void;+ const cancelled = new Promise<never>((_resolve, reject) => {+ abort = () => reject(signal.reason);+ if (signal.aborted) abort();+ else signal.addEventListener("abort", abort, { once: true });+ });+ try {+ return await Promise.race([cancelled, pending]);+ } finally {+ signal.removeEventListener("abort", abort);+ }+}diff --git a/apps/uzu-pi-extension/src/cache.ts b/apps/uzu-pi-extension/src/cache.tsnew file mode 100644index 000000000..51570a493--- /dev/null+++ b/apps/uzu-pi-extension/src/cache.ts@@ -0,0 +1,123 @@+import {+ ChatConfig,+ ContextLengthMaximal,+ type ChatInstance,+ type ChatSession,+ type Engine,+ type Model,+} from "@trymirai/uzu";+import { abortable } from "./abort.js";+import {+ assistantInput,+ nativeMessage,+ nativePreamble,+ type Input,+} from "./messages.js";+import type { AssistantMessage } from "@earendil-works/pi-ai";++// A single resident model and KV cache bound memory use. Requests serialize, including+// cancellation cleanup, so another turn can never race a native session still generating.+export class SessionCache {+ private tail: Promise<void> = Promise.resolve();+ private instance?: ChatInstance;+ private session?: ChatSession;+ private modelKey?: string;+ private ownerSessionId?: string;+ private sessionId?: string;+ private settings?: string;+ private history: string[] = [];++ constructor(+ private readonly engine: Pick<Engine, "chatInstance" | "chatWithInstance">,+ private readonly getOwnerSessionId?: () => string | undefined,+ ) {}++ async exclusive<T>(+ signal: AbortSignal | undefined,+ run: () => Promise<T>,+ ): Promise<T> {+ const previous = this.tail;+ let release!: () => void;+ const finished = new Promise<void>((resolve) => {+ release = resolve;+ });+ // A cancelled waiter can finish immediately, but its successor must still+ // wait for the active request to finish using native state.+ this.tail = previous.then(() => finished);+ try {+ await abortable(previous, signal);+ signal?.throwIfAborted();+ return await run();+ } finally {+ release();+ }+ }++ async prepare(+ model: Model,+ sessionId: string | undefined,+ input: Input,+ retain: boolean,+ ) {+ const key = model.cacheIdentifier;+ if (key !== this.modelKey) {+ this.clear();+ this.instance = await this.engine.chatInstance(+ model,+ ChatConfig.create().withContextLength(new ContextLengthMaximal()),+ );+ this.modelKey = key;+ }+ // Summaries use temporary request IDs; weights still belong to the Pi session.+ this.ownerSessionId =+ this.getOwnerSessionId?.() ?? sessionId ?? this.ownerSessionId;+ const settings = JSON.stringify([+ input.system,+ input.tools,+ input.reasoning,+ ]);+ const history = input.messages.map((message) => JSON.stringify(message));+ const continues =+ retain &&+ sessionId !== undefined &&+ sessionId === this.sessionId &&+ settings === this.settings &&+ this.history.length <= history.length &&+ this.history.every((message, index) => message === history[index]);+ if (!this.session)+ this.session = await this.engine.chatWithInstance(this.instance!);+ else if (!continues) await this.session.reset();+ const offset = continues ? this.history.length : 0;+ const messages = input.messages.slice(offset).map(nativeMessage);+ if (!continues) messages.unshift(...nativePreamble(input));+ this.sessionId = retain ? sessionId : undefined;+ this.settings = settings;+ this.history = history;+ return { session: this.session, messages };+ }++ complete(message: AssistantMessage) {+ this.history.push(JSON.stringify(assistantInput(message)));+ }++ invalidate() {+ this.session = undefined;+ this.sessionId = undefined;+ this.settings = undefined;+ this.history = [];+ }++ clear() {+ this.invalidate();+ this.instance = undefined;+ this.modelKey = undefined;+ this.ownerSessionId = undefined;+ }++ release(sessionId?: string) {+ return this.exclusive(undefined, async () => {+ if (sessionId === undefined || sessionId === this.ownerSessionId)+ this.clear();+ });+ }+}diff --git a/apps/uzu-pi-extension/src/download.ts b/apps/uzu-pi-extension/src/download.tsnew file mode 100644index 000000000..66359bdbb--- /dev/null+++ b/apps/uzu-pi-extension/src/download.ts@@ -0,0 +1,65 @@+import {+ DownloadPhaseDownloaded,+ DownloadPhaseError,+ DownloadPhaseLocked,+ DownloadPhasePaused,+ type Engine,+ type Model,+} from "@trymirai/uzu";+import { abortable } from "./abort.js";++export async function prepareModel(+ engine: Pick<+ Engine,+ "modelPath" | "downloader" | "download" | "chatModelInfo"+ >,+ model: Model,+ signal?: AbortSignal,+ progress: (message?: string) => void = () => {},+) {+ signal?.throwIfAborted();+ if (!(await engine.modelPath(model))) {+ if (!model.isDownloadable)+ throw new Error(`Uzu model files are unavailable: ${model.name}`);+ const downloader = engine.downloader(model);+ if ((await downloader.state)?.phase instanceof DownloadPhaseLocked)+ throw new Error(`Another process is downloading ${model.name}`);+ signal?.throwIfAborted();+ progress(`Downloading ${model.name}`);+ try {+ // Await startup before pausing a cancelled request, so resume cannot race pause.+ const stream = await engine.download(model);+ signal?.throwIfAborted();+ while (true) {+ const update = await abortable(stream.next(), signal);+ if (!update) break;+ if (update.phase instanceof DownloadPhaseError)+ throw new Error(update.phase.message);+ if (update.phase instanceof DownloadPhaseLocked)+ throw new Error(`Another process is downloading ${model.name}`);+ if (update.phase instanceof DownloadPhasePaused)+ throw new Error(`Download paused: ${model.name}`);+ progress(+ `Downloading ${model.name}: ${Math.round(update.progress * 100)}%`,+ );+ }+ signal?.throwIfAborted();+ const state = await downloader.state;+ if (state?.phase instanceof DownloadPhaseError)+ throw new Error(state.phase.message);+ if (!(state?.phase instanceof DownloadPhaseDownloaded))+ throw new Error(`Download did not complete: ${model.name}`);+ } catch (error) {+ if (!((await downloader.state)?.phase instanceof DownloadPhaseLocked))+ await downloader.pause();+ throw error;+ } finally {+ progress();+ }+ }+ signal?.throwIfAborted();+ const info = await engine.chatModelInfo(model);+ signal?.throwIfAborted();+ if (!info) throw new Error(`Unsupported Uzu chat model: ${model.name}`);+ return info;+}diff --git a/apps/uzu-pi-extension/src/index.ts b/apps/uzu-pi-extension/src/index.tsnew file mode 100644index 000000000..97220bdff--- /dev/null+++ b/apps/uzu-pi-extension/src/index.ts@@ -0,0 +1,110 @@+import { isDeepStrictEqual } from "node:util";+import {+ registerSessionResourceCleanup,+ type Provider,+} from "@earendil-works/pi-ai";+import type {+ ExtensionAPI,+ ExtensionUIContext,+} from "@earendil-works/pi-coding-agent";+import { Engine, EngineConfig } from "@trymirai/uzu";+import { SessionCache } from "./cache.js";+import { abortable } from "./abort.js";+import { prepareModel } from "./download.js";+import { api, discoverModels, modelDefinition, providerId } from "./models.js";+import { createStream } from "./stream.js";++export default async function uzuExtension(pi: ExtensionAPI) {+ const config = EngineConfig.create()+ .withAllowOllamaUsage(false)+ .withAllowLmstudioUsage(false);+ // This provider owns local inference; Pi already owns cloud provider credentials.+ config.openaiApiKey =+ config.anthropicApiKey =+ config.geminiApiKey =+ config.xaiApiKey =+ undefined;+ config.basetenApiKey = config.openrouterApiKey = undefined;+ const engine = await Engine.create(config);+ await registerUzuProvider(pi, engine);+}++export async function registerUzuProvider(pi: ExtensionAPI, engine: Engine) {+ let ownerSessionId: string | undefined;+ const cache = new SessionCache(engine, () => ownerSessionId);+ // Engine creation exposes the cached catalog while its refresh continues.+ let catalog = await discoverModels(engine);+ let ui: ExtensionUIContext | undefined;+ const stream = createStream(cache, async (id, signal) => {+ const entry = catalog.entries.get(id);+ if (!entry) return undefined;+ const info = await prepareModel(engine, entry.model, signal, (message) =>+ ui?.setStatus("uzu", message),+ );+ const definition = modelDefinition(entry.model, info, true);+ const next = { model: entry.model, info, definition };+ catalog.entries.set(id, next);+ // Pi refreshes its active model through the registry, preserving models.json overrides.+ if (!isDeepStrictEqual(definition, entry.definition))+ pi.registerProvider(provider);+ return next;+ });+ const provider: Provider<typeof api> = {+ id: providerId,+ name: "Uzu",+ auth: {+ apiKey: {+ name: "On-device Uzu",+ async resolve() {+ return { auth: {}, source: "On-device Uzu" };+ },+ },+ },+ getModels: () =>+ [...catalog.entries.values()].map((entry) => entry.definition),+ async refreshModels(context) {+ if (context.allowNetwork)+ await abortable(engine.refreshModels(), context.signal);+ const next = await discoverModels(engine, context.signal);+ await context.publish({+ update: () => {+ catalog = next;+ },+ });+ },+ stream,+ streamSimple: stream,+ };+ pi.registerProvider(provider);+ const unregisterCleanup = registerSessionResourceCleanup((sessionId) => {+ void cache.release(sessionId);+ });+ pi.on("session_shutdown", async (event) => {+ if (event.reason === "quit" || event.reason === "reload")+ unregisterCleanup();+ await cache.release();+ ownerSessionId = undefined;+ ui = undefined;+ });+ pi.on("before_provider_request", (_event, ctx) => {+ ownerSessionId = ctx.sessionManager.getSessionId();+ });+ pi.on("model_select", async (event, ctx) => {+ ui = ctx.ui;+ if (+ event.previousModel?.provider === providerId &&+ (event.model.provider !== providerId ||+ event.model.id !== event.previousModel.id)+ )+ await cache.release();+ });+ pi.on("session_start", async (_event, ctx) => {+ ownerSessionId = ctx.sessionManager.getSessionId();+ ui = ctx.ui;+ if (catalog.errors.length)+ ctx.ui.notify(+ `Some Uzu models could not be read:\n${catalog.errors.join("\n")}`,+ "warning",+ );+ });+}diff --git a/apps/uzu-pi-extension/src/messages.ts b/apps/uzu-pi-extension/src/messages.tsnew file mode 100644index 000000000..4702f91db--- /dev/null+++ b/apps/uzu-pi-extension/src/messages.ts@@ -0,0 +1,207 @@+import {+ getCurrentSystemPrompt,+ getCurrentTools,+ type AssistantMessage,+ type JsonObject,+ type SimpleStreamOptions,+ type TranscriptContext,+} from "@earendil-works/pi-ai";+import {+ ChatContentBlockToolCallResult,+ ChatMessage,+ ReasoningEffort,+ ToolCall,+ ToolDescriptionFunction,+ ToolFunction,+ ToolNamespace,+ Value,+ type ChatModelInfo,+} from "@trymirai/uzu";++export type InputBlock =+ | { type: "text"; text: string }+ | { type: "thinking"; thinking: string }+ | { type: "toolCall"; id: string; name: string; arguments: JsonObject }+ | { type: "toolResult"; id: string; name: string; text: string };+export interface InputMessage {+ role: "system" | "user" | "assistant" | "tool";+ content: InputBlock[];+}+export interface Input {+ system: string;+ tools: { name: string; description: string; parameters: unknown }[];+ reasoning?: ReasoningEffort;+ messages: InputMessage[];+}++export function assistantInput(message: AssistantMessage): InputMessage {+ return {+ role: "assistant",+ content: message.content.flatMap((block): InputBlock[] => {+ switch (block.type) {+ case "text":+ return [{ type: "text", text: block.text }];+ case "thinking":+ return block.redacted+ ? []+ : [{ type: "thinking", thinking: block.thinking }];+ case "toolCall":+ return [+ {+ type: "toolCall",+ id: block.id,+ name: block.name,+ arguments: block.arguments,+ },+ ];+ }+ }),+ };+}++export function prepareInput(+ context: TranscriptContext,+ info: ChatModelInfo,+ options: SimpleStreamOptions,+): Input {+ const tools =+ options.toolChoice === "none" ? [] : getCurrentTools(context.messages);+ if (tools.length && !info.capabilities.supportsTools)+ throw new Error("This Uzu model does not support tools");+ if (!tools.length && info.capabilities.requiresTools)+ throw new Error("This Uzu model requires tool declarations");+ const levels: Record<string, ReasoningEffort> = {+ minimal: ReasoningEffort.Low,+ low: ReasoningEffort.Low,+ medium: ReasoningEffort.Medium,+ high: ReasoningEffort.High,+ xhigh: ReasoningEffort.XHigh,+ max: ReasoningEffort.XHigh,+ };+ const requested = options.reasoning ? levels[options.reasoning] : undefined;+ const reasoning = !info.capabilities.supportsReasoning+ ? undefined+ : !requested && info.capabilities.supportsDisableReasoning+ ? ReasoningEffort.Disabled+ : requested && info.capabilities.reasoningEfforts.includes(requested)+ ? requested+ : (info.defaultReasoningEffort ?? ReasoningEffort.Default);+ const messages: InputMessage[] = [];+ const ignoredToolCalls = new Set<string>();+ for (const message of context.messages) {+ if (message.role === "system") continue;+ if (message.role === "assistant") {+ // Failed requests may contain incomplete tool calls that never ran.+ if (message.stopReason === "error" || message.stopReason === "aborted") {+ for (const block of message.content) {+ if (block.type === "toolCall") ignoredToolCalls.add(block.id);+ }+ } else messages.push(assistantInput(message));+ } else if (message.role === "user") {+ const content =+ typeof message.content === "string"+ ? [{ type: "text" as const, text: message.content }]+ : message.content;+ messages.push({+ role: "user",+ content: content.map((block) => {+ if (block.type !== "text")+ throw new Error(+ "Uzu currently supports text input only; remove image attachments",+ );+ return { type: "text", text: block.text };+ }),+ });+ } else {+ if (ignoredToolCalls.has(message.toolCallId)) continue;+ const text = message.content+ .map((block) => {+ if (block.type !== "text")+ throw new Error("Uzu currently supports text tool results only");+ return block.text;+ })+ .join("\n");+ messages.push({+ role: "tool",+ content: [+ {+ type: "toolResult",+ id: message.toolCallId,+ name: message.toolName,+ text,+ },+ ],+ });+ }+ }+ return {+ system: getCurrentSystemPrompt(context.messages),+ tools,+ reasoning,+ messages,+ };+}++export function nativePreamble(input: Input): ChatMessage[] {+ let message = ChatMessage.system().withText(input.system);+ if (input.reasoning !== undefined)+ message = message.withReasoningEffort(input.reasoning);+ const messages = [message];+ if (input.tools.length)+ messages.push(+ ChatMessage.developer().withToolNamespaces(nativeTools(input)),+ );+ return messages;+}++export function nativeTools(input: Input): ToolNamespace[] {+ return [+ new ToolNamespace(+ "functions",+ undefined,+ input.tools.map(+ (tool) =>+ new ToolDescriptionFunction(+ new ToolFunction(+ tool.name,+ tool.description,+ new Value(JSON.stringify(tool.parameters)),+ ),+ ),+ ),+ ),+ ];+}++export function nativeMessage(message: InputMessage): ChatMessage {+ let native = ChatMessage[message.role]();+ for (const block of message.content) {+ switch (block.type) {+ case "text":+ native = native.withText(block.text);+ break;+ case "thinking":+ native = native.withReasoning(block.thinking);+ break;+ case "toolCall":+ native = native.withToolCall(+ new ToolCall(+ block.id,+ block.name,+ new Value(JSON.stringify(block.arguments)),+ ),+ );+ break;+ case "toolResult":+ native = native.withBlock(+ new ChatContentBlockToolCallResult(+ block.id,+ block.name,+ new Value(JSON.stringify(block.text)),+ ),+ );+ break;+ }+ }+ return native;+}diff --git a/apps/uzu-pi-extension/src/models.ts b/apps/uzu-pi-extension/src/models.tsnew file mode 100644index 000000000..c1e1ccbe2--- /dev/null+++ b/apps/uzu-pi-extension/src/models.ts@@ -0,0 +1,104 @@+import type { Model, ThinkingLevelMap } from "@earendil-works/pi-ai";+import {+ DownloadPhaseDownloaded,+ ReasoningEffort,+ type ChatModelInfo,+ type Engine,+ type Model as UzuModel,+} from "@trymirai/uzu";+import type { ModelEntry } from "./stream.js";++export const providerId = "uzu";+export const api = "uzu-local";++export function modelDefinition(+ model: UzuModel,+ info: ChatModelInfo,+ downloaded: boolean,+): Model<typeof api> {+ // Zero denotes an unknown limit; downloaded models without one are unbounded.+ const contextWindow = info.contextLength ?? (downloaded ? Infinity : 0);+ const thinkingLevelMap: ThinkingLevelMap = { minimal: null, max: null };+ const efforts = info.capabilities.reasoningEfforts;+ const fallback = info.defaultReasoningEffort ?? ReasoningEffort.Default;+ const hasLevels = efforts.some((effort) =>+ [+ ReasoningEffort.Low,+ ReasoningEffort.Medium,+ ReasoningEffort.High,+ ReasoningEffort.XHigh,+ ].includes(effort),+ );+ const levels = {+ low: ReasoningEffort.Low,+ medium: ReasoningEffort.Medium,+ high: ReasoningEffort.High,+ xhigh: ReasoningEffort.XHigh,+ } as const;+ thinkingLevelMap.off =+ info.capabilities.supportsDisableReasoning ||+ !info.capabilities.supportsReasoning+ ? ReasoningEffort.Disabled+ : null;+ for (const [level, effort] of Object.entries(levels)) {+ thinkingLevelMap[level as keyof typeof levels] = !info.capabilities+ .supportsReasoning+ ? null+ : efforts.includes(effort)+ ? effort+ : !hasLevels && level === "medium"+ ? fallback+ : null;+ }+ return {+ id: model.repoIds[0] ?? model.identifier,+ name: downloaded ? model.name : `${model.name} (not downloaded)`,+ api,+ provider: providerId,+ baseUrl: "",+ input: ["text"],+ reasoning: info.capabilities.supportsReasoning,+ thinkingLevelMap,+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },+ contextWindow,+ maxTokens: info.contextLength ?? 0,+ };+}++export async function discoverModels(+ engine: Pick<Engine, "modelsOnDevice" | "chatModelInfo" | "downloadStates">,+ signal?: AbortSignal,+) {+ const entries = new Map<string, ModelEntry>();+ const errors: string[] = [];+ const states = await engine.downloadStates;+ for (const model of await engine.modelsOnDevice) {+ signal?.throwIfAborted();+ if (!model.isChatCapable) continue;+ const size = model.properties?.size;+ if (size !== undefined && size < 9_000_000_000) continue;+ try {+ const info = await engine.chatModelInfo(model);+ if (info?.capabilities.supportsTools) {+ const state = states[model.identifier];+ const downloaded =+ !!model.filesystemPath ||+ state?.phase instanceof DownloadPhaseDownloaded;+ const definition = modelDefinition(model, info, downloaded);+ if (!downloaded && state)+ definition.name = `${model.name} (${state.name.toLowerCase()})`;+ entries.set(definition.id, {+ model,+ info,+ definition,+ });+ }+ } catch (error) {+ errors.push(+ `${model.name}: ${error instanceof Error ? error.message : String(error)}`,+ );+ }+ }+ signal?.throwIfAborted();+ return { entries, errors };+}diff --git a/apps/uzu-pi-extension/src/stream.ts b/apps/uzu-pi-extension/src/stream.tsnew file mode 100644index 000000000..a08f8bf82--- /dev/null+++ b/apps/uzu-pi-extension/src/stream.ts@@ -0,0 +1,459 @@+import {+ createAssistantMessageEventStream,+ getCurrentTools,+ parseStreamingJson,+ type AssistantMessage,+ type AssistantMessageEventStream,+ type Model,+ type SimpleStreamOptions,+ type TranscriptContext,+} from "@earendil-works/pi-ai";+import {+ ChatContentBlockToolCallCandidate,+ ChatContentBlockToolCall,+ ChatReplyConfig,+ ChatReplyFinishReason,+ ChatSessionStreamChunkError,+ ChatSessionStreamChunkReplies,+ SamplingMethodGreedy,+ SamplingMethodStochastic,+ ToolCallArgumentStream,+ type ToolNamespace,+ type ChatModelInfo,+ type ChatReply,+ type ChatSessionStream,+ type Model as UzuModel,+} from "@trymirai/uzu";+import { SessionCache } from "./cache.js";+import { nativeTools, prepareInput, type Input } from "./messages.js";+import type { api } from "./models.js";++export function emptyMessage(model: Model<string>): AssistantMessage {+ return {+ role: "assistant",+ content: [],+ api: model.api,+ provider: model.provider,+ model: model.id,+ stopReason: "pending",+ timestamp: Date.now(),+ usage: {+ input: 0,+ output: 0,+ cacheRead: 0,+ cacheWrite: 0,+ totalTokens: 0,+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },+ },+ };+}++export class ReplyEvents {+ private thinkingIndex?: number;+ private textIndex?: number;+ private calls = new Map<+ string,+ {+ index?: number;+ stream: ToolCallArgumentStream;+ json: string;+ complete: boolean;+ source?: string;+ }+ >();++ constructor(+ readonly message: AssistantMessage,+ private readonly stream: AssistantMessageEventStream,+ private readonly tools: ToolNamespace[],+ ) {}++ update(reply: ChatReply) {+ if (reply.finishReason === ChatReplyFinishReason.ContextLimitReached)+ throw new Error("context_length_exceeded: Uzu context limit reached");+ this.text("thinking", reply.message.reasoning ?? "");+ this.text("text", reply.message.text ?? "");+ for (const content of reply.message.content) {+ if (+ !(content instanceof ChatContentBlockToolCall) &&+ !(content instanceof ChatContentBlockToolCallCandidate)+ )+ continue;+ const id =+ content instanceof ChatContentBlockToolCall+ ? content.value.identifier+ : content.identifier;+ if (!id) throw new Error("Uzu returned a tool call without an ID");+ let call = this.calls.get(id);+ if (!call) {+ call = {+ stream: ToolCallArgumentStream.create(this.tools),+ json: "",+ complete: false,+ };+ this.calls.set(id, call);+ }+ if (call.complete) {+ if (+ !(content instanceof ChatContentBlockToolCall) ||+ call.source !==+ JSON.stringify([content.value.name, content.value.arguments.json])+ )+ throw new Error("Uzu revised a completed tool call");+ continue;+ }+ let delta;+ if (content instanceof ChatContentBlockToolCallCandidate) {+ const value: unknown = JSON.parse(content.value.json);+ delta = call.stream.update(+ typeof value === "string" ? value : content.value.json,+ );+ } else {+ delta = call.stream.finish(content.value);+ call.complete = true;+ call.source = JSON.stringify([+ content.value.name,+ content.value.arguments.json,+ ]);+ }+ if (!delta.name) continue;+ if (call.index === undefined) {+ call.index = this.message.content.length;+ this.message.content.push({+ type: "toolCall",+ id,+ name: delta.name,+ arguments: {},+ });+ this.stream.push({+ type: "toolcall_start",+ contentIndex: call.index,+ partial: this.message,+ });+ }+ const block = this.message.content[call.index];+ if (block?.type !== "toolCall")+ throw new Error("Invalid tool content index");+ call.json += delta.arguments;+ block.arguments = call.complete+ ? JSON.parse(call.json)+ : parseStreamingJson(call.json);+ if (!delta.arguments) continue;+ this.stream.push({+ type: "toolcall_delta",+ contentIndex: call.index,+ delta: delta.arguments,+ partial: this.message,+ });+ }+ const cached = reply.stats.tokensCountInputCached ?? 0;+ const input = reply.stats.tokensCountInput ?? 0;+ this.message.usage.input = input;+ this.message.usage.cacheRead = cached;+ this.message.usage.output = reply.stats.tokensCountOutput ?? 0;+ this.message.usage.totalTokens =+ this.message.usage.input + cached + this.message.usage.output;+ }++ private text(kind: "text" | "thinking", value: string) {+ let index = kind === "text" ? this.textIndex : this.thinkingIndex;+ if (index === undefined) {+ if (!value) return;+ index = this.message.content.length;+ if (kind === "text") {+ this.textIndex = index;+ this.message.content.push({ type: "text", text: "" });+ this.stream.push({+ type: "text_start",+ contentIndex: index,+ partial: this.message,+ });+ } else {+ this.thinkingIndex = index;+ this.message.content.push({ type: "thinking", thinking: "" });+ this.stream.push({+ type: "thinking_start",+ contentIndex: index,+ partial: this.message,+ });+ }+ }+ const block = this.message.content[index]!;+ if (block.type === "toolCall") throw new Error("Invalid content index");+ const previous = block.type === "text" ? block.text : block.thinking;+ if (!value.startsWith(previous))+ throw new Error(`Uzu revised streamed ${kind}`);+ const delta = value.slice(previous.length);+ if (!delta) return;+ if (block.type === "text") {+ block.text = value;+ this.stream.push({+ type: "text_delta",+ contentIndex: index,+ delta,+ partial: this.message,+ });+ } else {+ block.thinking = value;+ this.stream.push({+ type: "thinking_delta",+ contentIndex: index,+ delta,+ partial: this.message,+ });+ }+ }++ finish(reply: ChatReply) {+ switch (reply.finishReason) {+ case ChatReplyFinishReason.Stop:+ this.message.stopReason = "stop";+ break;+ case ChatReplyFinishReason.Length:+ this.message.stopReason = "length";+ break;+ case ChatReplyFinishReason.ToolCalls:+ if (!this.calls.size)+ throw new Error("Uzu returned no complete tool calls");+ this.message.stopReason = "toolUse";+ break;+ case ChatReplyFinishReason.ContextLimitReached:+ throw new Error("context_length_exceeded: Uzu context limit reached");+ case ChatReplyFinishReason.Cancelled:+ throw new DOMException("Uzu generation cancelled", "AbortError");+ case ChatReplyFinishReason.Rejected:+ throw new Error("Uzu rejected the request");+ default:+ throw new Error("Uzu stream ended without a finish reason");+ }+ if (+ reply.message.content.some(+ (block) => block instanceof ChatContentBlockToolCallCandidate,+ )+ ) {+ throw new Error("Uzu returned an unfinished tool call");+ }+ if (reply.message.toolCalls.length !== this.calls.size)+ throw new Error("Uzu removed a streamed tool call");+ if (+ this.calls.size &&+ reply.finishReason !== ChatReplyFinishReason.ToolCalls+ )+ throw new Error("Uzu stopped before completing its tool calls");+ this.message.content.forEach((block, index) => {+ if (block.type === "text")+ this.stream.push({+ type: "text_end",+ contentIndex: index,+ content: block.text,+ partial: this.message,+ });+ else if (block.type === "thinking")+ this.stream.push({+ type: "thinking_end",+ contentIndex: index,+ content: block.thinking,+ partial: this.message,+ });+ else {+ this.stream.push({+ type: "toolcall_end",+ contentIndex: index,+ toolCall: block,+ partial: this.message,+ });+ }+ });+ }+}++export interface ModelEntry {+ model: UzuModel;+ info: ChatModelInfo;+ definition: Model<typeof api>;+}++export function createStream(+ cache: SessionCache,+ resolveEntry: (+ id: string,+ signal?: AbortSignal,+ ) => Promise<ModelEntry | undefined>,+) {+ return (+ model: Model<string>,+ context: TranscriptContext,+ options: SimpleStreamOptions = {},+ ): AssistantMessageEventStream => {+ const stream = createAssistantMessageEventStream();+ const message = emptyMessage(model);+ void cache+ .exclusive(options.signal, async () => {+ let native: ChatSessionStream | undefined;+ const abort = () => native?.cancelToken.cancel();+ options.signal?.addEventListener("abort", abort, { once: true });+ try {+ if (options.deferred)+ throw new Error("Uzu does not support deferred generation");+ if (options.fetch)+ throw new Error(+ "Uzu runs locally and cannot use a custom fetch implementation",+ );+ if (+ options.toolChoice !== undefined &&+ options.toolChoice !== "auto" &&+ options.toolChoice !== "none"+ )+ throw new Error('Uzu supports only toolChoice "auto" and "none"');+ if (options.toolChoice !== "none") {+ for (const tool of getCurrentTools(context.messages)) {+ if (+ tool.constrainedSampling &&+ tool.constrainedSampling.type === "json_schema" &&+ tool.constrainedSampling.strict === "require"+ )+ throw new Error(+ `Tool "${tool.name}" requires JSON-schema constrained sampling, which this Uzu adapter does not support`,+ );+ }+ }+ const tokenLimit = options.maxTokens;+ if (+ tokenLimit !== undefined &&+ (!Number.isSafeInteger(tokenLimit) ||+ tokenLimit < 1 ||+ tokenLimit > 0xffffffff)+ )+ throw new Error(+ "maxTokens must be an integer between 1 and 4294967295",+ );+ const entry = await resolveEntry(model.id, options.signal);+ if (!entry) throw new Error(`Uzu model is unavailable: ${model.id}`);+ let input = prepareInput(context, entry.info, options);+ let config = ChatReplyConfig.create()+ .withToolTurnLimit(0)+ .withTokenLimit(tokenLimit);+ if (options.temperature !== undefined) {+ if (+ !Number.isFinite(options.temperature) ||+ options.temperature < 0+ )+ throw new Error("Invalid temperature");+ config = config.withSamplingMethod(+ options.temperature === 0+ ? new SamplingMethodGreedy()+ : new SamplingMethodStochastic(options.temperature),+ );+ }+ const replacement = await options.onPayload?.(+ { input, config },+ model,+ );+ if (replacement !== undefined) {+ if (+ !replacement ||+ typeof replacement !== "object" ||+ !("input" in replacement) ||+ !("config" in replacement) ||+ !(replacement.config instanceof ChatReplyConfig)+ )+ throw new Error(+ "onPayload must return { input, config: ChatReplyConfig }",+ );+ input = replacement.input as Input;+ config = replacement.config;+ }+ options.signal?.throwIfAborted();+ const events = new ReplyEvents(message, stream, nativeTools(input));+ const prepared = await cache.prepare(+ entry.model,+ options.sessionId,+ input,+ options.cacheRetention !== "none",+ );+ options.signal?.throwIfAborted();+ native = await prepared.session.replyWithStream(+ prepared.messages,+ config,+ );+ if (options.signal?.aborted) abort();+ stream.push({ type: "start", partial: message });+ let last: ChatReply | undefined;+ for await (const chunk of native) {+ await options.onProviderStreamEvent?.(chunk, model);+ options.signal?.throwIfAborted();+ if (chunk instanceof ChatSessionStreamChunkError) {+ if (+ chunk.error.type === "Backend" &&+ chunk.error.message === "Context overflow"+ )+ throw new Error(+ "context_length_exceeded: Uzu input exceeds the context window",+ );+ throw new Error(JSON.stringify(chunk.error));+ }+ if (chunk instanceof ChatSessionStreamChunkReplies) {+ if (chunk.replies.length !== 1)+ throw new Error(+ "Expected one Uzu reply with automatic tools disabled",+ );+ last = chunk.replies[0]!;+ events.update(last);+ } else+ throw new Error("Unexpected automatic tool execution in Uzu");+ }+ options.signal?.throwIfAborted();+ if (!last) throw new Error("Uzu returned no response");+ if (+ last.finishReason === ChatReplyFinishReason.ToolCalls &&+ !entry.info.capabilities.supportsMultipleToolCalls &&+ last.message.toolCalls.length > 1+ )+ throw new Error(+ "This Uzu model returned more tool calls than its encoding supports",+ );+ events.finish(last);+ cache.complete(message);+ if (+ options.cacheRetention === "none" ||+ options.sessionId === undefined+ )+ cache.invalidate();+ stream.push({+ type: "done",+ reason: message.stopReason as "stop" | "length" | "toolUse",+ message,+ });+ } catch (error) {+ abort();+ // Drain cancellation before releasing the lock or model. Native tasks own+ // their state until the stream closes, even after an observer throws.+ if (native) {+ try {+ while ((await native.next()) !== null) {}+ } catch {}+ }+ cache.invalidate();+ throw error;+ } finally {+ options.signal?.removeEventListener("abort", abort);+ }+ })+ .catch((error: unknown) => {+ message.stopReason =+ options.signal?.aborted ||+ (error instanceof Error && error.name === "AbortError")+ ? "aborted"+ : "error";+ message.errorMessage =+ error instanceof Error ? error.message : String(error);+ stream.push({+ type: "error",+ reason: message.stopReason,+ error: message,+ });+ })+ .finally(() => stream.end());+ return stream;+ };+}diff --git a/apps/uzu-pi-extension/tests/download.test.ts b/apps/uzu-pi-extension/tests/download.test.tsnew file mode 100644index 000000000..a52be97d5--- /dev/null+++ b/apps/uzu-pi-extension/tests/download.test.ts@@ -0,0 +1,254 @@+import assert from "node:assert/strict";+import { test } from "node:test";+import {+ ChatModelCapabilities,+ ChatModelInfo,+ DownloadPhaseDownloaded,+ DownloadPhaseDownloading,+ DownloadPhaseError,+ DownloadPhaseLocked,+ DownloadPhaseNotDownloaded,+ DownloadPhasePaused,+ DownloadState,+ Model,+ ModelAccessibilityOnDevice,+ ModelSourceRegistry,+ ModelSpecializationChat,+ type DownloaderStream,+} from "@trymirai/uzu";+import { abortable } from "../src/abort.js";+import { prepareModel } from "../src/download.js";++const model = Model.external(+ "downloadable",+ "test",+ "Test",+ "uzu",+ "Uzu",+ "1",+ [new ModelSpecializationChat()],+ new ModelAccessibilityOnDevice(+ new ModelSourceRegistry("1", undefined, undefined, []),+ ),+);+const info = new ChatModelInfo(+ 65536,+ new ChatModelCapabilities(false, false, [], true, true, false),+);++function deferred<T>() {+ let resolve!: (value: T | PromiseLike<T>) => void;+ const promise = new Promise<T>((done) => {+ resolve = done;+ });+ return { promise, resolve };+}++function fixture() {+ let state = new DownloadState(100, 0, new DownloadPhaseNotDownloaded());+ let updates: DownloadState[] = [];+ const events: string[] = [];+ const progress: (string | undefined)[] = [];+ let start = async () => {};+ let next = async () => updates.shift() ?? null;+ const stream: DownloaderStream = {+ async next() {+ const update = await next();+ if (update) state = update;+ return update;+ },+ async *[Symbol.asyncIterator]() {+ let update;+ while ((update = await this.next()) !== null) yield update;+ },+ };+ const downloader = {+ get state() {+ return Promise.resolve(state);+ },+ async resume() {+ throw new Error("unused");+ },+ async pause() {+ events.push("pause");+ state = new DownloadState(+ 100,+ state.downloadedBytes,+ new DownloadPhasePaused(),+ );+ },+ async delete() {+ throw new Error("unused");+ },+ async progress() {+ return stream;+ },+ };+ const engine = {+ async modelPath() {+ return state.phase instanceof DownloadPhaseDownloaded ? "/model" : null;+ },+ downloader() {+ return downloader;+ },+ async download() {+ events.push("start");+ await start();+ events.push("started");+ return stream;+ },+ async chatModelInfo() {+ events.push("metadata");+ assert.ok(state.phase instanceof DownloadPhaseDownloaded);+ return info;+ },+ };+ return {+ engine,+ events,+ progress,+ run: (signal?: AbortSignal) =>+ prepareModel(engine, model, signal, (message) => progress.push(message)),+ setState(value: DownloadState) {+ state = value;+ },+ setUpdates(value: DownloadState[]) {+ updates = value;+ },+ setStart(value: typeof start) {+ start = value;+ },+ setNext(value: typeof next) {+ next = value;+ },+ };+}++test("downloads through the library and reads metadata only after completion", async () => {+ const f = fixture();+ f.setUpdates([+ new DownloadState(100, 25, new DownloadPhaseDownloading()),+ new DownloadState(100, 100, new DownloadPhaseDownloaded()),+ ]);+ assert.equal(await f.run(), info);+ assert.deepEqual(f.events, ["start", "started", "metadata"]);+ assert.deepEqual(f.progress, [+ `Downloading ${model.name}`,+ `Downloading ${model.name}: 25%`,+ `Downloading ${model.name}: 100%`,+ undefined,+ ]);+ f.events.length = 0;+ assert.equal(await f.run(), info);+ assert.deepEqual(f.events, ["metadata"]);+});++test("a download that finishes during startup still succeeds with an empty progress stream", async () => {+ const f = fixture();+ f.setStart(async () =>+ f.setState(new DownloadState(100, 100, new DownloadPhaseDownloaded())),+ );+ assert.equal(await f.run(), info);+});++test("failed and incomplete downloads never proceed to metadata or inference", async () => {+ for (const failure of [+ new DownloadPhaseError("network failed"),+ new DownloadPhasePaused(),+ new DownloadPhaseNotDownloaded(),+ ]) {+ const f = fixture();+ f.setUpdates([new DownloadState(100, 25, failure)]);+ await assert.rejects(+ f.run(),+ /network failed|Download paused|Download did not complete/,+ );+ assert.deepEqual(f.events, ["start", "started", "pause"]);+ assert.equal(f.progress.at(-1), undefined);+ }+ const f = fixture();+ f.setStart(async () =>+ f.setState(+ new DownloadState(100, 0, new DownloadPhaseError("startup failed")),+ ),+ );+ await assert.rejects(f.run(), /startup failed/);+});++test("cancellation pauses a pending download and a later request resumes it", async () => {+ const f = fixture();+ const waiting = deferred<void>();+ const pending = deferred<DownloadState | null>();+ f.setNext(async () => {+ waiting.resolve();+ return pending.promise;+ });+ const controller = new AbortController();+ const request = f.run(controller.signal);+ await waiting.promise;+ controller.abort();+ await assert.rejects(request, { name: "AbortError" });+ assert.deepEqual(f.events, ["start", "started", "pause"]);+ assert.equal(f.progress.at(-1), undefined);+ pending.resolve(null);+ let done = false;+ f.setNext(async () => {+ if (done) return null;+ done = true;+ return new DownloadState(100, 100, new DownloadPhaseDownloaded());+ });+ assert.equal(await f.run(), info);+ assert.deepEqual(f.events, [+ "start",+ "started",+ "pause",+ "start",+ "started",+ "metadata",+ ]);+});++test("cancellation waits for startup before pausing, so resume cannot restart a cancelled download", async () => {+ const f = fixture();+ const starting = deferred<void>();+ const started = deferred<void>();+ f.setStart(async () => {+ starting.resolve();+ await started.promise;+ });+ const controller = new AbortController();+ const request = f.run(controller.signal);+ await starting.promise;+ controller.abort();+ assert.deepEqual(f.events, ["start"]);+ started.resolve();+ await assert.rejects(request, { name: "AbortError" });+ assert.deepEqual(f.events, ["start", "started", "pause"]);+});++test("a download owned by another process is never paused", async () => {+ const locked = new DownloadState(100, 0, new DownloadPhaseLocked("other"));+ const f = fixture();+ f.setState(locked);+ await assert.rejects(f.run(), /Another process/);+ assert.deepEqual(f.events, []);++ const racing = fixture();+ racing.setUpdates([locked]);+ await assert.rejects(racing.run(), /Another process/);+ assert.deepEqual(racing.events, ["start", "started"]);+});++test("an already cancelled request starts no download and still observes pending rejection", async () => {+ const signal = AbortSignal.abort();+ const f = fixture();+ await assert.rejects(f.run(signal), { name: "AbortError" });+ assert.deepEqual(f.events, []);+ await assert.rejects(+ abortable(Promise.reject(new Error("late failure")), signal),+ { name: "AbortError" },+ );+ await assert.rejects(abortable(Promise.resolve(), signal), {+ name: "AbortError",+ });+});diff --git a/apps/uzu-pi-extension/tests/lifecycle.test.ts b/apps/uzu-pi-extension/tests/lifecycle.test.tsnew file mode 100644index 000000000..b75e55fbd--- /dev/null+++ b/apps/uzu-pi-extension/tests/lifecycle.test.ts@@ -0,0 +1,191 @@+import assert from "node:assert/strict";+import { test } from "node:test";+import {+ cleanupSessionResources,+ getSupportedThinkingLevels,+ normalizeContext,+ type Provider,+} from "@earendil-works/pi-ai";+import type {+ ExtensionAPI,+ ExtensionContext,+ ExtensionEvent,+} from "@earendil-works/pi-coding-agent";+import {+ ChatModelCapabilities,+ ChatModelInfo,+ Model,+ ModelAccessibilityOnDevice,+ ModelSourceFilesystem,+ ModelSpecializationChat,+ ReasoningEffort,+ type Engine,+} from "@trymirai/uzu";+import { SessionCache } from "../src/cache.js";+import { registerUzuProvider } from "../src/index.js";++const model = Model.external(+ "test",+ "local",+ "Local",+ "uzu",+ "Uzu",+ "1",+ [new ModelSpecializationChat()],+ new ModelAccessibilityOnDevice(new ModelSourceFilesystem("/test/model")),+);+const info = new ChatModelInfo(+ 65536,+ new ChatModelCapabilities(false, true, [], true, true, false),+ ReasoningEffort.Default,+);++function fixture() {+ type Handler = (event: ExtensionEvent, ctx: ExtensionContext) => unknown;+ const handlers = new Map<string, Handler>();+ let provider: Provider;+ let loads = 0;+ const pi = {+ registerProvider(value: Provider) {+ provider = value;+ },+ on(event: string, handler: Handler) {+ handlers.set(event, handler);+ return () => handlers.delete(event);+ },+ } as unknown as ExtensionAPI;+ const engine = {+ modelsOnDevice: Promise.resolve([model]),+ downloadStates: Promise.resolve({}),+ async chatModelInfo() {+ return info;+ },+ async modelPath() {+ return "/test/model";+ },+ async chatInstance() {+ loads++;+ return {};+ },+ async chatWithInstance() {+ throw new Error("Stop before inference");+ },+ // A registry that never responds must not delay cached-model registration.+ refreshModels: () => new Promise<Model[]>(() => {}),+ } as unknown as Engine;+ return {+ pi,+ engine,+ get provider() {+ return provider;+ },+ get loads() {+ return loads;+ },+ async emit(event: ExtensionEvent, sessionId = "session") {+ const ctx = {+ sessionManager: { getSessionId: () => sessionId },+ ui: { notify() {} },+ } as unknown as ExtensionContext;+ await handlers.get(event.type)?.(event, ctx);+ },+ };+}++test("cached models register without waiting for registry refresh", {+ timeout: 2000,+}, async (t) => {+ const f = fixture();+ const refresh = t.mock.method(f.engine, "refreshModels");+ await registerUzuProvider(f.pi, f.engine);+ t.after(() => f.emit({ type: "session_shutdown", reason: "quit" }));+ assert.equal(refresh.mock.callCount(), 0);+ assert.equal(f.provider.getModels().length, 1);+ assert.equal(f.provider.getModels()[0]?.name, model.name);++ // Pi's offline refresh also remains usable while networking is unavailable.+ let publications = 0;+ await f.provider.refreshModels!({+ allowNetwork: false,+ signal: new AbortController().signal,+ publish: async ({ update }) => {+ update?.();+ publications++;+ return true;+ },+ });+ assert.equal(publications, 1);+ assert.equal(refresh.mock.callCount(), 0);+});++test("provider cleanup uses the Pi session even before session_start", async (t) => {+ const f = fixture();+ await registerUzuProvider(f.pi, f.engine);+ t.after(() => f.emit({ type: "session_shutdown", reason: "quit" }));+ const definition = f.provider.getModels()[0]!;+ const context = normalizeContext({+ messages: [{ role: "user", content: "hello", timestamp: 1 }],+ });+ for (const owner of ["first", "second"]) {+ const response = await f.provider.streamSimple(definition, context, {+ sessionId: "temporary-summary-id",+ cacheRetention: "none",+ onPayload: async (payload) => {+ await f.emit({ type: "before_provider_request", payload }, owner);+ },+ }).result();+ assert.equal(response.errorMessage, "Stop before inference");+ cleanupSessionResources(owner);+ }+ assert.equal(f.loads, 2);+});++test("capability changes refresh Pi's model even when its name and context stay the same", async (t) => {+ const f = fixture();+ const registration = t.mock.method(f.pi, "registerProvider");+ await registerUzuProvider(f.pi, f.engine);+ t.after(() => f.emit({ type: "session_shutdown", reason: "quit" }));+ const definition = f.provider.getModels()[0]!;+ assert.deepEqual(getSupportedThinkingLevels(definition), ["off"]);+ t.mock.method(f.engine, "chatModelInfo", async () => new ChatModelInfo(+ info.contextLength,+ new ChatModelCapabilities(true, true, [ReasoningEffort.Disabled, ReasoningEffort.Default], true, true, false),+ ReasoningEffort.Default,+ ));+ await f.provider.streamSimple(definition, normalizeContext({+ messages: [{ role: "user", content: "hello", timestamp: 1 }],+ })).result();+ assert.equal(registration.mock.callCount(), 2);+ const refreshed = f.provider.getModels()[0]!;+ assert.equal(refreshed.name, definition.name);+ assert.equal(refreshed.contextWindow, definition.contextWindow);+ assert.deepEqual(getSupportedThinkingLevels(refreshed), ["off", "medium"]);+});++test("session cleanup survives replacements and unregisters on quit or reload", async (t) => {+ const releases: (string | undefined)[] = [];+ t.mock.method(SessionCache.prototype, "release", async (sessionId?: string) => {+ releases.push(sessionId);+ });+ for (const finalReason of ["quit", "reload"] as const) {+ const f = fixture();+ await registerUzuProvider(f.pi, f.engine);+ try {+ await f.emit({ type: "session_start", reason: "startup" });+ for (const reason of ["new", "resume", "fork"] as const) {+ releases.length = 0;+ await f.emit({ type: "session_shutdown", reason });+ cleanupSessionResources("old");+ await f.emit({ type: "session_start", reason }, "next");+ cleanupSessionResources("next");+ assert.deepEqual(releases, [undefined, "old", "next"]);+ }+ await f.emit({ type: "session_shutdown", reason: finalReason });+ releases.length = 0;+ cleanupSessionResources("disposed");+ assert.deepEqual(releases, []);+ } finally {+ await f.emit({ type: "session_shutdown", reason: "quit" });+ }+ }+});diff --git a/apps/uzu-pi-extension/tests/provider.test.ts b/apps/uzu-pi-extension/tests/provider.test.tsnew file mode 100644index 000000000..82ea0bd63--- /dev/null+++ b/apps/uzu-pi-extension/tests/provider.test.ts@@ -0,0 +1,1188 @@+import assert from "node:assert/strict";+import { test } from "node:test";+import {+ clampThinkingLevel,+ createAssistantMessageEventStream,+ getSupportedThinkingLevels,+ isContextOverflow,+ normalizeContext,+ Type,+ type AssistantMessageEvent,+ type ModelThinkingLevel,+ type SimpleStreamOptions,+ type TranscriptContext,+} from "@earendil-works/pi-ai";+import {+ CancelToken,+ ChatConfig,+ ChatContentBlockToolCallCandidate,+ ChatMessage,+ ChatModelCapabilities,+ ChatModelInfo,+ ChatReply,+ ChatReplyFinishReason,+ ChatReplyStats,+ ChatReplyConfig,+ ChatRoleDeveloper,+ ChatRoleSystem,+ ChatRoleUser,+ ChatSessionState,+ ChatSessionStreamChunkError,+ ChatSessionStreamChunkReplies,+ ContextLengthMaximal,+ DownloadPhaseDownloaded,+ DownloadPhaseNotDownloaded,+ DownloadState,+ Metadata,+ Model,+ ModelAccessibilityOnDevice,+ ModelProperties,+ ModelSourceFilesystem,+ ModelSourceRegistry,+ ModelSpecializationChat,+ ReasoningEffort,+ Repository,+ ToolCall,+ ToolDescriptionFunction,+ Value,+ type ChatInstance,+ type ChatSession,+ type ChatSessionStream,+ type ChatSessionStreamChunk,+} from "@trymirai/uzu";+import {+ AssistantMessageFrameEncoder,+ reduceAssistantMessageFrames,+ type AssistantMessageFrame,+} from "@earendil-works/pi-ai/utils/assistant-message-frame";+import { SessionCache } from "../src/cache.js";+import { prepareInput } from "../src/messages.js";+import { discoverModels, modelDefinition } from "../src/models.js";+import { createStream, emptyMessage } from "../src/stream.js";++const model = Model.external(+ "test",+ "local",+ "Local",+ "uzu",+ "Uzu",+ "1",+ [new ModelSpecializationChat()],+ new ModelAccessibilityOnDevice(new ModelSourceFilesystem("/test/model")),+);+const info = new ChatModelInfo(+ 65536,+ new ChatModelCapabilities(+ true,+ true,+ [ReasoningEffort.Default, ReasoningEffort.Disabled],+ true,+ true,+ false,+ ),+ ReasoningEffort.Default,+);+const definition = modelDefinition(model, info, true);+const context = (): TranscriptContext =>+ normalizeContext({+ systemPrompt: "Help",+ tools: [+ {+ name: "read",+ description: "Read",+ parameters: Type.Object({ path: Type.String() }),+ },+ ],+ messages: [{ role: "user", content: "hello", timestamp: 1 }],+ });+const reply = (message: ChatMessage, finish?: ChatReplyFinishReason) =>+ new ChatReply(+ message,+ new ChatReplyStats(1, undefined, undefined, undefined, 8, 32, 5),+ finish,+ );+const candidate = (id: string, raw: string) =>+ new ChatContentBlockToolCallCandidate(new Value(JSON.stringify(raw)), id);+const call = (id: string, name: string, args: unknown) =>+ new ToolCall(id, name, new Value(JSON.stringify(args)));++async function collect(+ stream: ReturnType<typeof createAssistantMessageEventStream>,+) {+ const events: AssistantMessageEvent[] = [];+ for await (const event of stream) events.push(structuredClone(event));+ return { events, message: await stream.result() };+}++function fixture(getOwnerSessionId?: () => string | undefined) {+ let resolutions = 0;+ let loads = 0;+ let resets = 0;+ let sessions = 0;+ const inputs: ChatMessage[][] = [];+ const instanceConfigs: ChatConfig[] = [];+ const replyConfigs: ChatReplyConfig[] = [];+ let chunks: ChatSessionStreamChunk[] = [];+ let onNext: (() => Promise<void>) | undefined;+ let token: CancelToken;+ const engine = {+ async chatInstance(+ _model: Model,+ config: ChatConfig,+ ): Promise<ChatInstance> {+ instanceConfigs.push(config);+ loads++;+ return { model };+ },+ async chatWithInstance(): Promise<ChatSession> {+ sessions++;+ return {+ state: Promise.resolve(ChatSessionState.Idle),+ messages: Promise.resolve([]),+ supportsToolCalls: Promise.resolve(true),+ samplingDefaults: Promise.resolve(null),+ async addTool() {+ return undefined;+ },+ async addTools() {+ return undefined;+ },+ async reset() {+ resets++;+ },+ async reply() {+ throw new Error("unused");+ },+ async replyWithStream(messages, config): Promise<ChatSessionStream> {+ replyConfigs.push(config);+ inputs.push(messages);+ token = CancelToken.create();+ const next = async () => {+ await new Promise<void>((resolve) => setImmediate(resolve));+ await onNext?.();+ return token.isCancelled ? null : (chunks.shift() ?? null);+ };+ return {+ cancelToken: token,+ next,+ async *[Symbol.asyncIterator]() {+ let chunk;+ while ((chunk = await next()) !== null) yield chunk;+ },+ };+ },+ };+ },+ };+ const cache = new SessionCache(engine, getOwnerSessionId);+ const stream = createStream(cache, async () => {+ resolutions++;+ return { model, info, definition };+ });+ return {+ cache,+ stream,+ inputs,+ instanceConfigs,+ replyConfigs,+ get resolutions() {+ return resolutions;+ },+ get loads() {+ return loads;+ },+ get resets() {+ return resets;+ },+ get sessions() {+ return sessions;+ },+ setReplies(replies: ChatReply[]) {+ chunks = replies.map(+ (value) => new ChatSessionStreamChunkReplies([value]),+ );+ },+ setChunks(value: ChatSessionStreamChunk[]) {+ chunks = value;+ },+ setOnNext(callback: () => Promise<void>) {+ onNext = callback;+ },+ get token() {+ return token;+ },+ };+}++test("streams reasoning, Unicode text and tool arguments through Pi frames", async () => {+ const f = fixture();+ f.setReplies([+ reply(+ ChatMessage.assistant()+ .withReasoning("think")+ .withText("🙂")+ .withBlock(candidate("id", "<function=read>\n<parameter=path>\n/")),+ ),+ reply(+ ChatMessage.assistant()+ .withReasoning("thinking")+ .withText("🙂 ok")+ .withToolCall(call("id", "read", { path: "/tmp" })),+ ChatReplyFinishReason.ToolCalls,+ ),+ ]);+ const result = await collect(+ f.stream(definition, context(), { sessionId: "s" }),+ );+ assert.equal(result.message.stopReason, "toolUse");+ assert.deepEqual(+ result.events+ .filter((e) => e.type === "toolcall_delta")+ .map((e) => e.delta),+ ['{"path":"/', 'tmp"}'],+ );+ const encoder = new AssistantMessageFrameEncoder();+ const frames: AssistantMessageFrame[] = [];+ let liveArguments: unknown;+ for (const event of result.events) {+ const frame = encoder.encode(event);+ if (frame) frames.push(frame);+ if (event.type === "toolcall_delta" && liveArguments === undefined) {+ liveArguments = reduceAssistantMessageFrames(frames)?.content.find(+ (block) => block.type === "toolCall",+ )?.arguments;+ }+ }+ assert.deepEqual(liveArguments, { path: "/" });+ const snapshot = result.events.find((e) => e.type === "toolcall_delta")!;+ assert.equal(snapshot.type, "toolcall_delta");+ if (snapshot.type === "toolcall_delta")+ assert.deepEqual(snapshot.partial.content[2], {+ type: "toolCall",+ id: "id",+ name: "read",+ arguments: { path: "/" },+ });+ assert.equal(+ result.events.filter((e) => e.type === "toolcall_start").length,+ 1,+ );+ assert.equal(+ result.events.filter((e) => e.type === "toolcall_end").length,+ 1,+ );+ assert.equal(result.events.filter((e) => e.type === "done").length, 1);+ assert.deepEqual(result.message.usage, {+ input: 8,+ cacheRead: 32,+ output: 5,+ cacheWrite: 0,+ totalTokens: 45,+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },+ });+});++test("tool declarations use the developer role while instructions and reasoning stay in system", async () => {+ const f = fixture();+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, context(), { sessionId: "s" }).result();+ const messages = f.inputs[0]!;+ assert.equal(messages.length, 3);+ assert.ok(messages[0]!.role instanceof ChatRoleSystem);+ assert.equal(messages[0]!.text, "Help");+ assert.equal(messages[0]!.reasoningEffort, ReasoningEffort.Disabled);+ assert.deepEqual(messages[0]!.toolNamespaces, []);+ assert.ok(messages[1]!.role instanceof ChatRoleDeveloper);+ assert.equal(messages[1]!.toolNamespaces[0]!.name, "functions");+ const tool = messages[1]!.toolNamespaces[0]!.tools[0]!;+ assert.ok(tool instanceof ToolDescriptionFunction);+ assert.equal(tool.toolFunction.name, "read");+ assert.deepEqual(JSON.parse(tool.toolFunction.parameters.json), {+ type: "object",+ properties: { path: { type: "string" } },+ required: ["path"],+ });+ assert.ok(messages[2]!.role instanceof ChatRoleUser);++ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f+ .stream(definition, context(), { sessionId: "s", toolChoice: "none" })+ .result();+ assert.equal(f.resets, 1);+ assert.equal(f.inputs[1]!.length, 2);+ assert.ok(f.inputs[1]![0]!.role instanceof ChatRoleSystem);+ assert.ok(f.inputs[1]![1]!.role instanceof ChatRoleUser);+ assert.deepEqual(+ f.inputs[1]!.flatMap((message) => message.toolNamespaces),+ [],+ );+});++test("tool round trips append only results, keep weights and KV, and reset on history or settings changes", async () => {+ const f = fixture();+ const firstContext = context();+ f.setReplies([+ reply(+ ChatMessage.assistant().withToolCall(call("id", "read", { path: "a" })),+ ChatReplyFinishReason.ToolCalls,+ ),+ ]);+ const first = await f+ .stream(definition, firstContext, { sessionId: "s" })+ .result();+ const next = normalizeContext({+ messages: [+ ...firstContext.messages,+ first,+ {+ role: "toolResult",+ toolCallId: "id",+ toolName: "read",+ content: [{ type: "text", text: "file" }],+ isError: false,+ timestamp: 2,+ },+ ],+ });+ f.setReplies([+ reply(ChatMessage.assistant().withText("done"), ChatReplyFinishReason.Stop),+ ]);+ const second = await f.stream(definition, next, { sessionId: "s" }).result();+ assert.equal(second.stopReason, "stop");+ assert.equal(f.inputs[0]?.length, 3);+ assert.equal(f.inputs[1]?.length, 1);+ assert.equal(+ f.inputs[1]?.[0]?.content[0]?.constructor.name,+ "ChatContentBlockToolCallResult",+ );+ assert.equal(f.loads, 1);+ assert.equal(f.resets, 0);+ f.setReplies([+ reply(+ ChatMessage.assistant().withText("branch"),+ ChatReplyFinishReason.Stop,+ ),+ ]);+ await f.stream(definition, firstContext, { sessionId: "s" }).result();+ assert.equal(f.resets, 1);+ f.setReplies([+ reply(+ ChatMessage.assistant().withText("reason"),+ ChatReplyFinishReason.Stop,+ ),+ ]);+ await f+ .stream(definition, firstContext, { sessionId: "s", reasoning: "low" })+ .result();+ assert.equal(f.resets, 2);+ assert.equal(f.loads, 1);+ await f.cache.release();+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, firstContext, { sessionId: "other" }).result();+ assert.equal(f.loads, 2);+});++test("a different session, changed tools and cacheRetention none never reuse prior KV state", async () => {+ const f = fixture();+ for (const sessionId of ["one", "two", "two"]) {+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ const input = context();+ if (f.inputs.length === 2)+ input.messages.unshift({+ role: "system",+ content: "changed",+ timestamp: 2,+ });+ await f.stream(definition, input, { sessionId }).result();+ }+ assert.equal(f.resets, 2);+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f+ .stream(definition, context(), { sessionId: "two", cacheRetention: "none" })+ .result();+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, context(), { sessionId: "two" }).result();+ assert.equal(f.sessions, 2);+ assert.equal(f.loads, 1);+});++test("malformed, unterminated and overflowing streams fail instead of executing partial calls", async () => {+ for (const value of [+ reply(+ ChatMessage.assistant().withBlock(+ new ChatContentBlockToolCallCandidate(new Value('"unfinished"')),+ ),+ ChatReplyFinishReason.ToolCalls,+ ),+ reply(+ ChatMessage.assistant().withToolCall(+ call("id", "read", { __uzu_unparsed_arguments: "bad" }),+ ),+ ChatReplyFinishReason.ToolCalls,+ ),+ reply(ChatMessage.assistant().withText("unfinished")),+ reply(ChatMessage.assistant(), ChatReplyFinishReason.ContextLimitReached),+ reply(+ ChatMessage.assistant().withToolCall(+ call("id", "read", { __uzu_unparsed_arguments: "partial" }),+ ),+ ChatReplyFinishReason.ContextLimitReached,+ ),+ ]) {+ const f = fixture();+ f.setReplies([value]);+ const { events, message } = await collect(+ f.stream(definition, context(), { sessionId: "s" }),+ );+ assert.equal(message.stopReason, "error");+ assert.equal(events.filter((e) => e.type === "error").length, 1);+ assert.equal(+ events.filter((e) => e.type === "done" || e.type === "toolcall_end")+ .length,+ 0,+ );+ if (value.finishReason === ChatReplyFinishReason.ContextLimitReached)+ assert.match(message.errorMessage!, /context_length_exceeded/);+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, context(), { sessionId: "s" }).result();+ assert.equal(f.sessions, 2);+ }+});++test("native input overflow triggers Pi's compaction detector without relabeling other errors", async () => {+ for (const error of [+ { type: "Backend", message: "Context overflow" },+ { type: "Backend", message: "Unable to allocate buffer" },+ { type: "Loading", message: "Context overflow" },+ ] as const) {+ const f = fixture();+ f.setChunks([new ChatSessionStreamChunkError(error)]);+ const { message, events } = await collect(+ f.stream(definition, context(), { sessionId: "s" }),+ );+ const overflow =+ error.type === "Backend" && error.message === "Context overflow";+ assert.equal(message.stopReason, "error");+ assert.equal(isContextOverflow(message), overflow);+ if (!overflow) assert.equal(message.errorMessage, JSON.stringify(error));+ assert.equal(events.filter((event) => event.type === "error").length, 1);+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, context(), { sessionId: "s" }).result();+ assert.equal(f.sessions, 2);+ }+});++test("unsupported required tool constraints fail before download or inference", async () => {+ for (const choice of ["any", { type: "tool", name: "read" }]) {+ const f = fixture();+ f.setReplies([+ reply(+ ChatMessage.assistant().withText("hello"),+ ChatReplyFinishReason.Stop,+ ),+ ]);+ // Runtime callers can supply values outside Pi 1.1's auto/none TypeScript union.+ const options = { toolChoice: choice } as SimpleStreamOptions;+ const result = await f.stream(definition, context(), options).result();+ assert.equal(result.stopReason, "error");+ assert.match(result.errorMessage!, /toolChoice.*auto.*none/);+ assert.equal(f.resolutions, 0);+ assert.equal(f.loads, 0);+ }++ for (const strict of ["require", "prefer"] as const) {+ const f = fixture();+ const input = normalizeContext({+ tools: [+ {+ name: "read",+ description: "Read",+ parameters: Type.Object({ path: Type.String() }),+ constrainedSampling: { type: "json_schema", strict },+ },+ ],+ messages: [{ role: "user", content: "hello", timestamp: 1 }],+ });+ f.setReplies([+ reply(+ ChatMessage.assistant().withText("hello"),+ ChatReplyFinishReason.Stop,+ ),+ ]);+ const result = await f.stream(definition, input).result();+ if (strict === "require") {+ assert.equal(result.stopReason, "error");+ assert.match(+ result.errorMessage!,+ /read.*requires JSON-schema constrained sampling/,+ );+ assert.equal(f.resolutions, 0);+ assert.equal(f.loads, 0);+ const withoutTools = await f+ .stream(definition, input, { toolChoice: "none" })+ .result();+ assert.equal(withoutTools.stopReason, "stop");+ } else {+ assert.equal(result.stopReason, "stop");+ assert.equal(f.loads, 1);+ }+ }+});++test("session cleanup releases weights after failure, cancellation, or disabled KV retention", async () => {+ for (const outcome of ["failure", "cancel", "no-cache"] as const) {+ const f = fixture();+ const controller = new AbortController();+ if (outcome === "failure")+ f.setChunks([+ new ChatSessionStreamChunkError({ type: "Backend", message: "failed" }),+ ]);+ else+ f.setReplies([+ reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop),+ ]);+ if (outcome === "cancel") f.setOnNext(async () => controller.abort());+ const result = await f+ .stream(definition, context(), {+ sessionId: "owner",+ signal: controller.signal,+ cacheRetention: outcome === "no-cache" ? "none" : undefined,+ })+ .result();+ assert.equal(+ result.stopReason,+ outcome === "failure"+ ? "error"+ : outcome === "cancel"+ ? "aborted"+ : "stop",+ );+ await f.cache.release("unrelated");+ await f.cache.release("owner");+ f.setOnNext(async () => {});+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, context(), { sessionId: "next" }).result();+ assert.equal(f.loads, 2, outcome);+ }+});++test("cleanup from a previous owner does not release another session's retained weights", async () => {+ const f = fixture();+ for (const sessionId of ["first", "second"]) {+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f+ .stream(definition, context(), { sessionId, cacheRetention: "none" })+ .result();+ }+ await f.cache.release("first");+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, context(), { sessionId: "second" }).result();+ assert.equal(f.loads, 1);+ await f.cache.release("second");+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, context(), { sessionId: "third" }).result();+ assert.equal(f.loads, 2);+});++test("temporary requests keep their owning Pi session for cleanup", async () => {+ for (const sessionId of [undefined, "summary-request"]) {+ let owner = "parent";+ const f = fixture(() => owner);+ for (const options of [+ { sessionId: owner },+ { sessionId, cacheRetention: "none" as const },+ ]) {+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ assert.equal(+ (await f.stream(definition, context(), options).result()).stopReason,+ "stop",+ );+ }+ await f.cache.release("summary-request");+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f+ .stream(definition, context(), { sessionId, cacheRetention: "none" })+ .result();+ assert.equal(f.loads, 1);+ await f.cache.release(owner);+ owner = "next";+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, context(), { sessionId: owner }).result();+ assert.equal(f.loads, 2);+ }+});++test("an anonymous request does not erase an existing cache owner", async () => {+ const f = fixture();+ for (const sessionId of ["owner", undefined]) {+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, context(), { sessionId }).result();+ }+ await f.cache.release("owner");+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, context(), { sessionId: "next" }).result();+ assert.equal(f.loads, 2);+});++test("cancellation drains the native stream, releases the lock and discards the session", async () => {+ const f = fixture();+ const abort = new AbortController();+ f.setOnNext(async () => {+ abort.abort();+ });+ f.setReplies([+ reply(+ ChatMessage.assistant().withText("ignored"),+ ChatReplyFinishReason.Stop,+ ),+ ]);+ const result = await collect(+ f.stream(definition, context(), { signal: abort.signal, sessionId: "s" }),+ );+ assert.equal(result.message.stopReason, "aborted");+ assert.equal(f.token.isCancelled, true);+ f.setOnNext(async () => {});+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ assert.equal(+ (await f.stream(definition, context(), { sessionId: "s" }).result())+ .stopReason,+ "stop",+ );+ assert.equal(f.sessions, 2);+});++test("a queued request aborts immediately without letting later requests overtake the active request", async () => {+ const f = fixture();+ let release!: () => void;+ let entered!: () => void;+ const started = new Promise<void>((resolve) => {+ entered = resolve;+ });+ const held = f.cache.exclusive(+ undefined,+ () =>+ new Promise<void>((resolve) => {+ release = resolve;+ entered();+ }),+ );+ await started;+ const controller = new AbortController();+ const waiting = f.stream(definition, context(), {+ signal: controller.signal,+ });+ controller.abort();+ let followingRan = false;+ const following = f.cache.exclusive(undefined, async () => {+ followingRan = true;+ });+ try {+ const result = await Promise.race([+ waiting.result(),+ new Promise<undefined>((resolve) =>+ setImmediate(() => resolve(undefined)),+ ),+ ]);+ assert.equal(+ result?.stopReason,+ "aborted",+ "cancellation must finish while the first request still holds the lock",+ );+ assert.equal(followingRan, false);+ assert.equal(f.loads, 0);+ } finally {+ release();+ await held;+ await following;+ }+ assert.equal(followingRan, true);+ assert.equal((await waiting.result()).stopReason, "aborted");+ const cancelled = await f+ .stream(definition, context(), { signal: AbortSignal.abort() })+ .result();+ assert.equal(cancelled.stopReason, "aborted");+ assert.equal(f.loads, 0);+});++test("cancellation after the final chunk still discards the session", async () => {+ const f = fixture();+ const controller = new AbortController();+ let reads = 0;+ f.setOnNext(async () => {+ if (++reads === 2) controller.abort();+ });+ f.setReplies([+ reply(ChatMessage.assistant().withText("done"), ChatReplyFinishReason.Stop),+ ]);+ const result = await collect(+ f.stream(definition, context(), {+ sessionId: "s",+ signal: controller.signal,+ }),+ );+ assert.equal(result.message.stopReason, "aborted");+ assert.equal(+ result.events.filter((event) => event.type === "done").length,+ 0,+ );+ assert.equal(f.token.isCancelled, true);+});++test("partial tool arguments can become valid, and multiple calls retain separate IDs", async () => {+ const f = fixture();+ f.setReplies([+ reply(+ ChatMessage.assistant().withBlock(+ candidate("first", "<function=read>\n<parameter=path>\na"),+ ),+ ),+ reply(+ ChatMessage.assistant()+ .withToolCall(call("first", "read", { path: "a" }))+ .withToolCall(call("second", "read", { path: "b" })),+ ChatReplyFinishReason.ToolCalls,+ ),+ ]);+ const result = await collect(f.stream(definition, context()));+ assert.equal(result.message.stopReason, "toolUse");+ assert.deepEqual(+ result.events+ .filter((event) => event.type === "toolcall_end")+ .map((event) => event.toolCall.id),+ ["first", "second"],+ );+ assert.deepEqual(result.message.content, [+ { type: "toolCall", id: "first", name: "read", arguments: { path: "a" } },+ { type: "toolCall", id: "second", name: "read", arguments: { path: "b" } },+ ]);+});++for (const format of ["Qwen XML", "Muse Glimmer ATEM"]) {+ test(`${format} streams escaped strings through Pi frames and preserves declared argument types`, async () => {+ const f = fixture();+ const text = 'London — 🙂\n"quoted"\\path\n</parametric>\n';+ const raw = format === "Qwen XML"+ ? `<function=write>\n<parameter=path>\n/tmp/a\n</parameter>\n<parameter=content>\n${text}\n</parameter>\n<parameter=count>\n3\n</parameter>\n</function>`+ : `<atem:function_calls>\n<atem:invoke name="write">\n<atem:parameter name="path">/tmp/a</atem:parameter>\n<atem:parameter name="content">${text}</atem:parameter>\n<atem:parameter name="count">3</atem:parameter>\n</atem:invoke>\n</atem:function_calls>`;+ let prefix = "";+ const replies: ChatReply[] = [];+ for (const character of raw) {+ prefix += character;+ replies.push(+ reply(ChatMessage.assistant().withBlock(candidate("write-id", prefix))),+ );+ }+ replies.push(+ reply(+ ChatMessage.assistant().withToolCall(+ call("write-id", "write", {+ path: "/tmp/a",+ content: text,+ count: "3",+ }),+ ),+ ChatReplyFinishReason.ToolCalls,+ ),+ );+ f.setReplies(replies);+ const input = normalizeContext({+ tools: [+ {+ name: "write",+ description: "Write",+ parameters: Type.Object({+ path: Type.String(),+ content: Type.String(),+ count: Type.Integer(),+ }),+ },+ ],+ messages: [{ role: "user", content: "Write", timestamp: 1 }],+ });+ const encoder = new AssistantMessageFrameEncoder();+ const frames: AssistantMessageFrame[] = [];+ const liveContents: string[] = [];+ const stream = f.stream(definition, input);+ for await (const event of stream) {+ const frame = encoder.encode(event);+ if (frame) frames.push(frame);+ if (event.type === "toolcall_delta") {+ const block = reduceAssistantMessageFrames(frames)?.content[0];+ if (+ block?.type === "toolCall" &&+ typeof block.arguments.content === "string"+ )+ liveContents.push(block.arguments.content);+ }+ }+ const result = await stream.result();+ assert.equal(result.stopReason, "toolUse", result.errorMessage);+ assert.ok(liveContents.includes("London"));+ assert.ok(liveContents.every((value) => text.startsWith(value)));+ assert.deepEqual(result.content, [+ {+ type: "toolCall",+ id: "write-id",+ name: "write",+ arguments: { path: "/tmp/a", content: text, count: 3 },+ },+ ]);+ assert.deepEqual(+ reduceAssistantMessageFrames(frames)?.content,+ result.content,+ );+ });+}++test("observer errors cancel generation and payload hooks can replace the native request", async () => {+ const f = fixture();+ f.setReplies([+ reply(ChatMessage.assistant().withText("x"), ChatReplyFinishReason.Stop),+ ]);+ const failed = await f+ .stream(definition, context(), {+ onProviderStreamEvent() {+ throw new Error("observer failed");+ },+ })+ .result();+ assert.equal(failed.errorMessage, "observer failed");+ assert.equal(f.token.isCancelled, true);+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ const success = await f+ .stream(definition, context(), {+ onPayload(payload) {+ assert.ok(+ payload &&+ typeof payload === "object" &&+ "input" in payload &&+ "config" in payload,+ );+ return {+ ...payload,+ input: {+ ...prepareInput(context(), info, {}),+ messages: [+ { role: "user", content: [{ type: "text", text: "changed" }] },+ ],+ },+ };+ },+ })+ .result();+ assert.equal(success.stopReason, "stop");+ assert.equal(f.inputs.at(-1)?.at(-1)?.text, "changed");+});++test("advertised thinking levels match native efforts without duplicate aliases", () => {+ const profiles: {+ name: string;+ capabilities: ChatModelCapabilities;+ defaultEffort?: ReasoningEffort;+ expected: [ModelThinkingLevel, ReasoningEffort | undefined][];+ }[] = [+ {+ name: "Qwen3.8",+ capabilities: new ChatModelCapabilities(true, true, [+ ReasoningEffort.Disabled, ReasoningEffort.Default,+ ReasoningEffort.Low, ReasoningEffort.Medium, ReasoningEffort.XHigh,+ ], true, true, false),+ defaultEffort: ReasoningEffort.XHigh,+ expected: [+ ["off", ReasoningEffort.Disabled],+ ["low", ReasoningEffort.Low],+ ["medium", ReasoningEffort.Medium],+ ["xhigh", ReasoningEffort.XHigh],+ ],+ },+ {+ name: "Muse Glimmer",+ capabilities: new ChatModelCapabilities(true, false, [+ ReasoningEffort.Default, ReasoningEffort.Low, ReasoningEffort.Medium,+ ReasoningEffort.High, ReasoningEffort.XHigh,+ ], true, true, false),+ defaultEffort: ReasoningEffort.High,+ expected: [+ ["low", ReasoningEffort.Low],+ ["medium", ReasoningEffort.Medium],+ ["high", ReasoningEffort.High],+ ["xhigh", ReasoningEffort.XHigh],+ ],+ },+ {+ name: "GPT-OSS",+ capabilities: new ChatModelCapabilities(true, false, [+ ReasoningEffort.Low, ReasoningEffort.Medium, ReasoningEffort.High,+ ], true, false, false),+ defaultEffort: ReasoningEffort.Medium,+ expected: [+ ["low", ReasoningEffort.Low],+ ["medium", ReasoningEffort.Medium],+ ["high", ReasoningEffort.High],+ ],+ },+ {+ name: "thinking toggle",+ capabilities: info.capabilities,+ defaultEffort: ReasoningEffort.Default,+ expected: [["off", ReasoningEffort.Disabled], ["medium", ReasoningEffort.Default]],+ },+ {+ name: "always thinking",+ capabilities: new ChatModelCapabilities(true, false, [], true, true, false),+ expected: [["medium", ReasoningEffort.Default]],+ },+ {+ name: "no thinking",+ capabilities: new ChatModelCapabilities(false, false, [], true, true, false),+ expected: [["off", undefined]],+ },+ ];+ for (const profile of profiles) {+ const metadata = new ChatModelInfo(65536, profile.capabilities, profile.defaultEffort);+ const definition = modelDefinition(model, metadata, true);+ assert.equal(definition.reasoning, profile.capabilities.supportsReasoning, profile.name);+ assert.deepEqual(+ getSupportedThinkingLevels(definition),+ profile.expected.map(([level]) => level),+ profile.name,+ );+ assert.equal(definition.thinkingLevelMap?.minimal, null, profile.name);+ assert.equal(definition.thinkingLevelMap?.max, null, profile.name);+ for (const [level, effort] of profile.expected) {+ const options = { reasoning: level === "off" ? undefined : level };+ assert.equal(prepareInput(context(), metadata, options).reasoning, effort, `${profile.name}: ${level}`);+ }+ for (const requested of ["off", "minimal", "low", "medium", "high", "xhigh", "max"] as const) {+ const clamped = clampThinkingLevel(definition, requested);+ assert.ok(profile.expected.some(([level]) => level === clamped), `${profile.name}: ${requested}`);+ }+ }+});++test("model metadata keeps the model's full context and lists models before download", async () => {+ assert.equal(definition.contextWindow, 65536);+ assert.equal(definition.maxTokens, definition.contextWindow);+ assert.equal(definition.reasoning, true);+ assert.deepEqual(getSupportedThinkingLevels(definition), ["off", "medium"]);+ assert.deepEqual(definition.input, ["text"]);+ assert.equal(definition.id, model.identifier);+ const downloadable = Model.external(+ "alibaba:qwen3.5:9b:mirai:mirai-m:4",+ "test",+ "Test",+ "uzu",+ "Uzu",+ "1",+ [new ModelSpecializationChat()],+ new ModelAccessibilityOnDevice(+ new ModelSourceRegistry(+ "1",+ new Repository("trymirai/Qwen3.5-9B-M"),+ new Repository("Qwen/Qwen3.5-9B"),+ [],+ ),+ ),+ );+ const pendingInfo = new ChatModelInfo(+ undefined,+ info.capabilities,+ info.defaultReasoningEffort,+ );+ const result = await discoverModels({+ modelsOnDevice: Promise.resolve([model, downloadable]),+ downloadStates: Promise.resolve({+ [downloadable.identifier]: new DownloadState(+ 100,+ 0,+ new DownloadPhaseNotDownloaded(),+ ),+ }),+ async chatModelInfo(value) {+ if (value.identifier === downloadable.identifier) return pendingInfo;+ return info;+ },+ });+ assert.equal(result.entries.size, 2);+ assert.equal(+ result.entries.get(model.identifier)?.definition.contextWindow,+ 65536,+ );+ const entry = result.entries.get("trymirai/Qwen3.5-9B-M")!;+ assert.equal(entry.model.identifier, downloadable.identifier);+ const pending = entry.definition;+ assert.equal(pending.id, "trymirai/Qwen3.5-9B-M");+ assert.match(pending.name, /not downloaded/);+ assert.equal(pending.contextWindow, 0);+ assert.equal(pending.reasoning, true);+ assert.equal(+ modelDefinition(model, pendingInfo, true).contextWindow,+ Infinity,+ );+ assert.equal(modelDefinition(model, pendingInfo, true).maxTokens, 0);+ const downloaded = await discoverModels({+ modelsOnDevice: Promise.resolve([downloadable]),+ downloadStates: Promise.resolve({+ [downloadable.identifier]: new DownloadState(+ 100,+ 100,+ new DownloadPhaseDownloaded(),+ ),+ }),+ async chatModelInfo() {+ return info;+ },+ });+ assert.equal(+ downloaded.entries.get(pending.id)?.definition.name,+ downloadable.name,+ );+ assert.equal(+ downloaded.entries.get(pending.id)?.definition.contextWindow,+ 65536,+ );+ const invalid = await discoverModels({+ modelsOnDevice: Promise.resolve([model]),+ downloadStates: Promise.resolve({}),+ async chatModelInfo() {+ throw new Error("bad config");+ },+ });+ assert.match(invalid.errors[0]!, /bad config/);+});++test("model discovery filters tool support and size for downloaded and missing models", async () => {+ const candidates = [+ { id: "small", size: 8_999_999_999, supportsTools: true },+ { id: "minimum", size: 9_000_000_000, supportsTools: true },+ { id: "large", size: 27_000_000_000, supportsTools: true },+ { id: "no-tools", size: 9_000_000_000, supportsTools: false },+ { id: "unknown-size", size: undefined, supportsTools: true },+ { id: "unknown-size-no-tools", size: undefined, supportsTools: false },+ ];+ for (const downloaded of [false, true]) {+ const models = candidates.map(({ id, size }) => {+ const candidate = Model.external(+ id,+ "test",+ "Test",+ "uzu",+ "Uzu",+ "1",+ [new ModelSpecializationChat()],+ new ModelAccessibilityOnDevice(+ new ModelSourceRegistry(+ "1", new Repository(`test/${id}`), undefined, [],+ ),+ ),+ );+ if (size !== undefined)+ candidate.properties = new ModelProperties(+ id,+ size,+ undefined,+ Metadata.external(id),+ );+ return candidate;+ });+ const catalog = await discoverModels({+ modelsOnDevice: Promise.resolve(models),+ downloadStates: Promise.resolve(+ Object.fromEntries(+ models.map((model) => [+ model.identifier,+ new DownloadState(+ 100,+ downloaded ? 100 : 0,+ downloaded+ ? new DownloadPhaseDownloaded()+ : new DownloadPhaseNotDownloaded(),+ ),+ ]),+ ),+ ),+ async chatModelInfo(model) {+ const capabilities = info.capabilities;+ capabilities.supportsTools = candidates.find(+ ({ id }) => id === model.identifier,+ )!.supportsTools;+ return new ChatModelInfo(65536, capabilities, info.defaultReasoningEffort);+ },+ });+ assert.deepEqual([...catalog.entries.keys()], [+ "test/minimum",+ "test/large",+ "test/unknown-size",+ ]);+ assert.deepEqual(catalog.errors, []);+ }+});++test("sessions use Uzu's maximal context and only explicit requests limit output", async () => {+ const f = fixture();+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, context()).result();+ assert.ok(+ f.instanceConfigs[0]?.contextLength instanceof ContextLengthMaximal,+ );+ assert.equal(f.replyConfigs[0]?.tokenLimit, null);+ f.setReplies([reply(ChatMessage.assistant(), ChatReplyFinishReason.Stop)]);+ await f.stream(definition, context(), { maxTokens: 12345 }).result();+ assert.equal(f.replyConfigs[1]?.tokenLimit, 12345);+});++test("images fail explicitly and foreign provider tool calls retain their IDs and arguments", () => {+ assert.throws(+ () =>+ prepareInput(+ normalizeContext({+ messages: [+ {+ role: "user",+ timestamp: 1,+ content: [{ type: "image", mimeType: "image/png", data: "AA==" }],+ },+ ],+ }),+ info,+ {},+ ),+ /text input only/,+ );+ const foreign = emptyMessage({ ...definition, provider: "foreign" });+ foreign.stopReason = "toolUse";+ foreign.content = [+ {+ type: "toolCall",+ id: "foreign-id",+ name: "read",+ arguments: { path: "x" },+ },+ ];+ assert.deepEqual(+ prepareInput(normalizeContext({ messages: [foreign] }), info, {})+ .messages[0]?.content,+ foreign.content,+ );+ foreign.stopReason = "aborted";+ assert.deepEqual(+ prepareInput(+ normalizeContext({+ messages: [+ foreign,+ {+ role: "toolResult",+ toolCallId: "foreign-id",+ toolName: "read",+ content: [{ type: "text", text: "Cancelled" }],+ isError: true,+ timestamp: 1,+ },+ ],+ }),+ info,+ {},+ ).messages,+ [],+ );+});diff --git a/apps/uzu-pi-extension/tsconfig.build.json b/apps/uzu-pi-extension/tsconfig.build.jsonnew file mode 100644index 000000000..5079b9f52--- /dev/null+++ b/apps/uzu-pi-extension/tsconfig.build.json@@ -0,0 +1,5 @@+{+ "extends": "./tsconfig.json",+ "compilerOptions": { "rootDir": "src" },+ "include": ["src/**/*.ts"]+}diff --git a/apps/uzu-pi-extension/tsconfig.json b/apps/uzu-pi-extension/tsconfig.jsonnew file mode 100644index 000000000..a1405e1c3--- /dev/null+++ b/apps/uzu-pi-extension/tsconfig.json@@ -0,0 +1,14 @@+{+ "compilerOptions": {+ "target": "ES2023",+ "module": "NodeNext",+ "moduleResolution": "NodeNext",+ "strict": true,+ "noUncheckedIndexedAccess": true,+ "skipLibCheck": true,+ "declaration": true,+ "outDir": "dist",+ "types": ["node"]+ },+ "include": ["src/**/*.ts", "tests/**/*.ts"]+}diff --git a/crates/legacy/backend-remote/src/openai/tool_call_state.rs b/crates/legacy/backend-remote/src/openai/tool_call_state.rsindex b93793472..522f06352 100644--- a/crates/legacy/backend-remote/src/openai/tool_call_state.rs+++ b/crates/legacy/backend-remote/src/openai/tool_call_state.rs@@ -41,6 +41,10 @@ impl ToolCallState {},});}- OutputToolCallState::Candidate(self.arguments.clone())+ OutputToolCallState::Candidate(format!(+ "{{\"name\":{},\"arguments\":{}",+ serde_json::to_string(&self.name).expect("tool name serializes"),+ self.arguments,+ ))}}diff --git a/crates/legacy/cli-tools/src/sync/mod.rs b/crates/legacy/cli-tools/src/sync/mod.rsindex 02bec92cf..bcd42ae3a 100644--- a/crates/legacy/cli-tools/src/sync/mod.rs+++ b/crates/legacy/cli-tools/src/sync/mod.rs@@ -5,6 +5,7 @@ mod jsr;mod license;mod mirai_gui;mod package_json;+mod pi_extension;mod playground;mod pyproject;mod readme;@@ -102,6 +103,13 @@ pub fn run_sync(check: bool) -> Result<()> {PlaygroundSyncTask.run(&platforms, &workspace, &root_path.join("apps/playground/Project.swift"), check)?;+ pi_extension::PiExtensionSyncTask.run(+ &platforms,+ &workspace,+ &root_path.join("apps/uzu-pi-extension/package.json"),+ check,+ )?;+let mirai_gui_path = root_path.join("apps/mirai-gui");MiraiGuiSyncTask::PackageJson.run(&platforms, &workspace, &mirai_gui_path.join("package.json"), check)?;MiraiGuiSyncTask::CargoToml.run(&platforms, &workspace, &mirai_gui_path.join("desktop/Cargo.toml"), check)?;diff --git a/crates/legacy/cli-tools/src/sync/pi_extension.rs b/crates/legacy/cli-tools/src/sync/pi_extension.rsnew file mode 100644index 000000000..8571da276--- /dev/null+++ b/crates/legacy/cli-tools/src/sync/pi_extension.rs@@ -0,0 +1,22 @@+use anyhow::Result;+use regex::Regex;++use crate::{+ configs::{PlatformsConfig, WorkspaceManifest},+ sync::SyncTask,+};++pub struct PiExtensionSyncTask;++impl SyncTask for PiExtensionSyncTask {+ fn process(+ &self,+ _platforms: &PlatformsConfig,+ workspace: &WorkspaceManifest,+ input: &str,+ ) -> Result<String> {+ let regex = Regex::new(r#"("version"\s*:\s*)"[^"]*""#)?;+ let version = serde_json::to_string(&workspace.workspace.package.version)?;+ Ok(regex.replace(input, format!("$1{version}").as_str()).into_owned())+ }+}diff --git a/crates/legacy/cli/src/server/chat_completions.rs b/crates/legacy/cli/src/server/chat_completions.rsindex 87c472330..e18752555 100644--- a/crates/legacy/cli/src/server/chat_completions.rs+++ b/crates/legacy/cli/src/server/chat_completions.rs@@ -33,7 +33,7 @@ use crate::{chat_tool_calls::{OaiTool, OaiToolCall, ToolCallStreamer, ToolParameterTypes, backfill_tool_result_names, choose_tools,coerce_tool_call, insert_tools_message, oai_tool_call, parse_parameter_text, reply_tool_calls,- to_tool_call, tool_call_result_block, withhold_stream_text,+ to_tool_call, tool_call_result_block, tool_parameter_types, withhold_stream_text,},request_info::RequestInfo,request_log::RequestLog,@@ -1011,19 +1011,32 @@ async fn run_stream(let candidates = reply.message.content.iter().filter_map(|block| match block {ChatContentBlock::ToolCallCandidate {value,+ ..} => Some(value),_ => None,});for (offset, value) in candidates.enumerate() {let index = finished_count + offset;- // candidates carry their partial text as a JSON string document, except- // object-valued ones (e.g. muse-glimmer's name-only progress objects)+ // Candidates carry partial markup as a JSON string document or a JSON object.let raw = serde_json::from_str::<String>(&value.json).unwrap_or_else(|_| value.json.clone());let deltas = tool_call_streamers.entry(index).or_insert_with(ToolCallStreamer::new).update(index,&raw,¶meter_types,);+ let deltas = match deltas {+ Ok(deltas) => deltas,+ Err(error) => {+ let message = error.to_string();+ let _ = sender.send(Event::data(+ serde_json::to_string(&oai_error_body(Status::BadRequest, "backend_error", &message))+ .unwrap_or_default(),+ ));+ cancel_and_drain(&stream).await;+ log.fail(&message);+ return;+ },+ };for delta in deltas {let sent = sender.send(Event::data(chunk_json(&id,@@ -1053,7 +1066,23 @@ async fn run_stream(}let call = coerce_tool_call(&tool_calls[index], ¶meter_types);let delta = match tool_call_streamers.remove(&index) {- Some(mut streamer) => streamer.finish(index, &call),+ Some(mut streamer) => match streamer.finish(index, &call) {+ Ok(delta) => delta,+ Err(error) => {+ let message = error.to_string();+ let _ = sender.send(Event::data(+ serde_json::to_string(&oai_error_body(+ Status::BadRequest,+ "backend_error",+ &message,+ ))+ .unwrap_or_default(),+ ));+ cancel_and_drain(&stream).await;+ log.fail(&message);+ return;+ },+ },None => oai_tool_call(Some(index), &call),};let sent = sender.send(Event::data(chunk_json(@@ -1224,7 +1253,7 @@ pub async fn handle_chat_completions(// The parser keeps every argument the text the model wrote; the declared// schemas restore the types when replies cross back into the OpenAI wire format.- let parameter_types = ToolParameterTypes::from_tools(request.tools.as_deref());+ let parameter_types = tool_parameter_types(request.tools.as_deref());let id = request_info.id.as_str().to_owned();if is_stream {diff --git a/crates/legacy/cli/src/server/chat_tool_calls.rs b/crates/legacy/cli/src/server/chat_tool_calls.rsindex 49fb765ce..cdf972049 100644--- a/crates/legacy/cli/src/server/chat_tool_calls.rs+++ b/crates/legacy/cli/src/server/chat_tool_calls.rs@@ -1,11 +1,12 @@-use std::collections::{HashMap, HashSet};+use std::collections::HashMap;use serde::{Deserialize, Serialize};use uuid::Uuid;+pub use uzu::session::chat::tool_call::{ToolParameterTypes, coerce_tool_call, parse_parameter_text};use uzu::{- session::chat::normalize_tool_call_arguments,+ session::chat::{ChatSessionError, normalize_tool_call_arguments, tool_call::ToolCallArgumentStream},types::{- basic::{ToolCall, ToolDescription, ToolFunction, ToolNamespace, Value, parse_lenient_json},+ basic::{ToolCall, ToolDescription, ToolFunction, ToolNamespace, Value},session::chat::{ChatContentBlock, ChatMessage, ChatRole},},};@@ -92,193 +93,10 @@ pub fn choose_tools<'t>(}}-/// Declared JSON-Schema types of each tool parameter, keyed by function name.-/// Tool-call markup cannot carry types — the parser keeps every parameter the-/// text the model wrote — so the declared schema is what restores the wire-/// types clients validate against.-#[derive(Clone, Default)]-pub struct ToolParameterTypes(HashMap<String, HashMap<String, Vec<String>>>);--impl ToolParameterTypes {- pub fn from_tools(tools: Option<&[OaiTool]>) -> Self {- let mut functions = HashMap::new();- for tool in tools.unwrap_or_default() {- let Some(parameters) = &tool.function.parameters else {- continue;- };- let Ok(schema) = serde_json::from_str::<serde_json::Value>(¶meters.json) else {- continue;- };- let Some(properties) = schema.get("properties").and_then(serde_json::Value::as_object) else {- continue;- };- let parameters = properties- .iter()- .filter_map(|(name, property)| {- let types = declared_types(property, &schema, 8);- (!types.is_empty()).then(|| (name.clone(), types))- })- .collect();- functions.insert(tool.function.name.clone(), parameters);- }- Self(functions)- }-- fn declared(- &self,- function: &str,- parameter: &str,- ) -> Option<&[String]> {- Some(self.0.get(function)?.get(parameter)?.as_slice())- }-}--// Per parameter: count each queued schema and each entry in a type array.-const MAX_SCHEMA_TRAVERSAL_WORK: usize = 1024;--/// The JSON-Schema types a property schema declares: its `type` keyword, or the-/// union of its `anyOf`/`oneOf` branches, following local `$ref`s (the shape-/// pydantic and schemars clients emit for optional and nested objects).-fn declared_types(- property: &serde_json::Value,- root: &serde_json::Value,- depth: u32,-) -> Vec<String> {- let mut pending = vec![(property, depth)];- let mut visited = HashMap::new();- let mut types = HashSet::new();- let mut remaining_work = MAX_SCHEMA_TRAVERSAL_WORK - 1;- while let Some((property, depth)) = pending.pop() {- if depth == 0 {- continue;- }- let property = match property.get("$ref").and_then(serde_json::Value::as_str) {- Some(reference) => {- reference.strip_prefix('#').and_then(|pointer| root.pointer(pointer)).unwrap_or(property)- },- None => property,- };- // Track identity without hashing the schema's contents. Revisit a shared- // node only when a shorter path leaves more depth to discover its types.- let identity = std::ptr::from_ref(property);- if visited.get(&identity).is_some_and(|seen_depth| *seen_depth >= depth) {- continue;- }- visited.insert(identity, depth);- match property.get("type") {- Some(serde_json::Value::String(kind)) => {- types.insert(kind.as_str());- },- Some(serde_json::Value::Array(kinds)) => {- let Some(remaining) = remaining_work.checked_sub(kinds.len()) else {- return Vec::new();- };- remaining_work = remaining;- types.extend(kinds.iter().filter_map(serde_json::Value::as_str));- },- _ if depth > 1 => {- for branches in- ["anyOf", "oneOf"].iter().filter_map(|key| property.get(*key).and_then(serde_json::Value::as_array))- {- // Charge work before enqueueing, bounding both visits and- // pending allocations even for a wide, nonrecursive union.- let Some(remaining) = remaining_work.checked_sub(branches.len()) else {- // Partial types may omit a string branch and cause an- // incorrect coercion, so leave the parameter unchanged.- return Vec::new();- };- remaining_work = remaining;- pending.extend(branches.iter().map(|branch| (branch, depth - 1)));- }- },- _ => {},- }- }- let mut types: Vec<String> = types.into_iter().map(str::to_string).collect();- types.sort_unstable();- types-}--fn matches_declared_type(- value: &serde_json::Value,- declared: &[String],-) -> bool {- declared.iter().any(|kind| match kind.as_str() {- "integer" => value.as_i64().is_some() || value.as_u64().is_some(),- "number" => value.is_number(),- "boolean" => value.is_boolean(),- "null" => value.is_null(),- "object" => value.is_object(),- "array" => value.is_array(),- "string" => value.is_string(),- _ => false,- })-}--/// Parameter text read as JSON: strict first, then with the bounded repair of-/// control characters and bare quotes models leave inside string values, plus-/// the Python-style booleans some models emit in tool markup (qwen3.5 writes-/// `True`/`False`).-pub fn parse_parameter_text(text: &str) -> Option<serde_json::Value> {- if let Some(parsed) = parse_lenient_json(text) {- return Some(parsed);- }- match text.trim() {- text if text.eq_ignore_ascii_case("true") => Some(serde_json::Value::Bool(true)),- text if text.eq_ignore_ascii_case("false") => Some(serde_json::Value::Bool(false)),- _ => None,- }-}--fn coerce_parameter_value(- value: &serde_json::Value,- declared: &[String],-) -> Option<serde_json::Value> {- let declares_string = declared.iter().any(|kind| kind == "string");- match value {- // The parser kept the markup text as a string; restore the declared- // type when the text reads as it. A union that includes "string" stays- // a string: the text is already schema-valid and the intended type is- // unknowable.- serde_json::Value::String(text) if !declares_string => {- let parsed = parse_parameter_text(text)?;- matches_declared_type(&parsed, declared).then_some(parsed)- },- // A JSON-format parser typed a value although the parameter is a- // plain string.- value if declared == ["string"] && !value.is_string() => Some(serde_json::Value::String(value.to_string())),- _ => None,- }-}--/// Restores the declared types the markup could not carry. Applied only at-/// the OpenAI boundary: the session keeps the parser's values so its stored-/// history stays consistent with what the template renders.-pub fn coerce_tool_call(- tool_call: &ToolCall,- types: &ToolParameterTypes,-) -> ToolCall {- let Ok(serde_json::Value::Object(mut object)) = serde_json::from_str(&tool_call.arguments.json) else {- return tool_call.clone();- };- let mut changed = false;- for (parameter, value) in object.iter_mut() {- if let Some(declared) = types.declared(&tool_call.name, parameter)- && let Some(coerced) = coerce_parameter_value(value, declared)- {- *value = coerced;- changed = true;- }- }- if !changed {- return tool_call.clone();- }- ToolCall {- arguments: Value {- json: serde_json::Value::Object(object).to_string(),- },- ..tool_call.clone()- }+pub fn tool_parameter_types(tools: Option<&[OaiTool]>) -> ToolParameterTypes {+ ToolParameterTypes::from_parameters(tools.unwrap_or_default().iter().filter_map(|tool| {+ tool.function.parameters.as_ref().map(|parameters| (tool.function.name.as_str(), parameters))+ }))}pub fn to_tool_call(tool_call: &OaiToolCall) -> ToolCall {@@ -402,165 +220,18 @@ pub fn withhold_stream_text(has_tools && (trimmed.is_empty() || trimmed.starts_with('{'))}-// Streaming state for one in-progress tool call.-//-// OpenAI clients assemble `function.arguments` by concatenating delta fragments and parsing the-// result when the call ends, so every fragment must extend one string. Candidate content comes in-// two shapes:-// - framed (qwen3.5-style): the candidate value is the model's raw `<function=name>` /-// `<parameter=key>` markup, growing token by token. Arguments are synthesized into compact JSON;-// the currently open parameter's value streams as escaped string content.-// - bare JSON (llama-3-style): the candidate value is partial JSON text; only the name is-// announced, since the raw text is not a prefix of the canonical arguments string.pub struct ToolCallStreamer {id: String,- name_announced: bool,- key_order: Vec<String>,- open_text: String,- final_candidate: Option<String>,-}--enum FramedParam {- Complete(String),- Open(String),-}--struct FramedCall {- name: Option<String>,- params: Vec<(String, FramedParam)>,-}--fn parse_framed_call(raw: &str) -> FramedCall {- let name = raw.find("<function=").and_then(|start| {- let rest = &raw[start + "<function=".len()..];- rest.find('>').map(|end| rest[..end].to_string())- });- let mut params = Vec::new();- let mut rest = raw;- while let Some(start) = rest.find("<parameter=") {- rest = &rest[start + "<parameter=".len()..];- let Some(tag_end) = rest.find('>') else {- break;- };- let key = rest[..tag_end].to_string();- let value_part = &rest[tag_end + 1..];- let value_part = value_part.strip_prefix('\n').unwrap_or(value_part);- match value_part.find("\n</parameter>") {- Some(end) => {- params.push((key, FramedParam::Complete(value_part[..end].to_string())));- rest = &value_part[end + "\n</parameter>".len()..];- },- None => {- params.push((key, FramedParam::Open(value_part.to_string())));- break;- },- }- }- FramedCall {- name,- params,- }-}--// A completed parameter serializes exactly as coerce_tool_call serializes it in-// the final call, so the streamed prefix is a prefix of the final text.-fn serialize_param_value(- value: &str,- declared: Option<&[String]>,-) -> String {- let text = serde_json::Value::String(value.to_string());- declared.and_then(|declared| coerce_parameter_value(&text, declared)).unwrap_or(text).to_string()-}--// The longest suffix of `content` that could be the start of the parameter close tag is-// ambiguous until more tokens arrive, so it is withheld from streaming.-const PARAMETER_CLOSE_SEQUENCE: &str = "\n</parameter>";--fn withhold_ambiguous_tail(content: &str) -> &str {- // the leftmost char boundary whose suffix is a prefix of the close sequence- // gives the longest ambiguous tail- for (index, _) in content.char_indices() {- let suffix = &content[index..];- if suffix.len() <= PARAMETER_CLOSE_SEQUENCE.len() && PARAMETER_CLOSE_SEQUENCE.starts_with(suffix) {- return &content[..index];- }- }- content-}--// The arguments text with no closing brace: completed parameters are frozen, the open-// parameter's value streams as it grows.-fn open_arguments_text(- params: &[(String, FramedParam)],- function: Option<&str>,- types: &ToolParameterTypes,-) -> Option<String> {- let mut entries: Vec<(String, String)> = Vec::new();- for (key, param) in params {- let declared = function.and_then(|function| types.declared(function, key));- let serialized = match param {- FramedParam::Complete(value) => serialize_param_value(value, declared),- FramedParam::Open(content) => {- // an empty value has nothing to emit yet, and a declared non-string- // value takes its final form (a bare literal) only once complete:- // either way nothing about the parameter may be emitted- let streamable = withhold_ambiguous_tail(content);- let declared_non_string =- declared.is_some_and(|declared| !declared.iter().any(|kind| kind == "string"));- if streamable.is_empty() || declared_non_string {- break;- }- let quoted = serde_json::Value::String(streamable.to_string()).to_string();- quoted[..quoted.len() - 1].to_string()- },- };- entries.push((key.clone(), serialized));- }- if entries.is_empty() {- return None;- }- let mut out = String::from("{");- for (index, (key, serialized)) in entries.iter().enumerate() {- if index > 0 {- out.push(',');- }- out.push_str(&serde_json::to_string(key).expect("key serializes"));- out.push(':');- out.push_str(serialized);- }- Some(out)-}--fn json_candidate_name(raw: &str) -> Option<String> {- let rest = &raw[raw.find("\"name\"")? + 6..];- let rest = rest[rest.find(':')? + 1..].trim_start();- let rest = rest.strip_prefix('"')?;- let mut name = String::new();- let mut chars = rest.chars();- while let Some(c) = chars.next() {- match c {- '"' => return Some(name),- '\\' => match chars.next() {- Some('n') => name.push('\n'),- Some(other) => name.push(other),- None => return None,- },- c => name.push(c),- }- }- None+ announced: bool,+ arguments: Option<ToolCallArgumentStream>,}impl ToolCallStreamer {pub fn new() -> Self {Self {- // The id is assigned by the server at announcement time: clients key- // in-progress tool call UI by id, so it must never change mid-call.id: Uuid::new_v4().to_string(),- name_announced: false,- key_order: Vec::new(),- open_text: String::new(),- final_candidate: None,+ announced: false,+ arguments: None,}}@@ -569,121 +240,67 @@ impl ToolCallStreamer {index: usize,raw: &str,types: &ToolParameterTypes,- ) -> Vec<OaiToolCall> {+ ) -> Result<Vec<OaiToolCall>, ChatSessionError> {+ let stream = self.arguments.get_or_insert_with(|| ToolCallArgumentStream::new(types.clone()));+ let delta = stream.update(raw.to_string())?;+ if delta.name.is_empty() {+ return Ok(Vec::new());+ }let mut deltas = Vec::new();- let name;- if raw.trim_start().starts_with('<') {- let call = parse_framed_call(raw);- name = call.name.filter(|name| !name.is_empty());- self.key_order = call.params.iter().map(|(key, _)| key.clone()).collect();- if let Some(open_text) = open_arguments_text(&call.params, name.as_deref(), types) {- if call.params.iter().all(|(_, param)| matches!(param, FramedParam::Complete(_))) {- self.final_candidate = Some(format!("{open_text}}}"));- }- if open_text.len() > self.open_text.len() && open_text.starts_with(&self.open_text) {- deltas.push(OaiToolCall {- index: Some(index),- id: String::new(),- kind: String::new(),- function: OaiFunctionCall {- name: String::new(),- arguments: open_text[self.open_text.len()..].to_string(),- },- });- self.open_text = open_text;- }- }- } else {- name = json_candidate_name(raw);+ if !self.announced {+ self.announced = true;+ deltas.push(OaiToolCall {+ index: Some(index),+ id: self.id.clone(),+ kind: "function".into(),+ function: OaiFunctionCall {+ name: delta.name,+ arguments: String::new(),+ },+ });}- if let Some(name) = name- && !name.is_empty()- && !self.name_announced- {- self.name_announced = true;- deltas.insert(- 0,- OaiToolCall {- index: Some(index),- id: self.id.clone(),- kind: "function".to_string(),- function: OaiFunctionCall {- name,- arguments: String::new(),- },+ if !delta.arguments.is_empty() {+ deltas.push(OaiToolCall {+ index: Some(index),+ id: String::new(),+ kind: String::new(),+ function: OaiFunctionCall {+ name: String::new(),+ arguments: delta.arguments,},- );+ });}- deltas+ Ok(deltas)}pub fn finish(&mut self,index: usize,call: &ToolCall,- ) -> OaiToolCall {- let arguments = if self.open_text.is_empty() {- call.arguments.json.clone()- } else {- self.closing_fragment(call)- };- let (id, kind, name) = if self.name_announced {- (String::new(), String::new(), String::new())- } else {- (self.id.clone(), "function".to_string(), call.name.clone())- };- OaiToolCall {+ ) -> Result<OaiToolCall, ChatSessionError> {+ let stream = self.arguments.get_or_insert_with(|| ToolCallArgumentStream::new(ToolParameterTypes::default()));+ let delta = stream.finish(call.clone())?;+ Ok(OaiToolCall {index: Some(index),- id,- kind,- function: OaiFunctionCall {- name,- arguments,+ id: if self.announced {+ String::new()+ } else {+ self.id.clone()},- }- }-- fn closing_fragment(- &self,- call: &ToolCall,- ) -> String {- // Rebuild the final arguments with keys in streamed order so the closing fragment- // completes the exact string the earlier fragments started.- let final_text = match serde_json::from_str::<serde_json::Value>(&call.arguments.json) {- Ok(serde_json::Value::Object(map)) => {- let mut keys: Vec<&str> =- self.key_order.iter().map(String::as_str).filter(|key| map.contains_key(*key)).collect();- for key in map.keys() {- if !keys.contains(&key.as_str()) {- keys.push(key);- }- }- let entries = keys- .iter()- .map(|key| format!("{}:{}", serde_json::to_string(key).expect("key serializes"), map[*key]))- .collect::<Vec<_>>();- format!("{{{}}}", entries.join(","))+ kind: if self.announced {+ String::new()+ } else {+ "function".into()},- _ => call.arguments.json.clone(),- };- match final_text.strip_prefix(&self.open_text) {- Some(suffix) => suffix.to_string(),- None => {- if let Some(candidate) = self.final_candidate.as_deref()- && serde_json::from_str::<serde_json::Value>(candidate).is_ok()- && let Some(suffix) = candidate.strip_prefix(&self.open_text)- {- eprintln!(- "[server] tool call stream diverged from the final call; using the structurally parsed candidate"- );- return suffix.to_string();- }- eprintln!(- "[server] tool call stream diverged from the final call; the client may reject the assembled arguments"- );- final_text+ function: OaiFunctionCall {+ name: if self.announced {+ String::new()+ } else {+ delta.name+ },+ arguments: delta.arguments,},- }+ })}}diff --git a/crates/legacy/cli/unit/server/chat_tool_calls_test.rs b/crates/legacy/cli/unit/server/chat_tool_calls_test.rsindex 3fafcc13b..ae0622d14 100644--- a/crates/legacy/cli/unit/server/chat_tool_calls_test.rs+++ b/crates/legacy/cli/unit/server/chat_tool_calls_test.rs@@ -170,10 +170,11 @@ fn reply_tool_calls_serialize_in_openai_format() {#[test]fn json_candidate_announces_name_once_it_is_complete() {let mut streamer = ToolCallStreamer::new();- assert!(streamer.update(0, r#"{"name": "wr"#, &ToolParameterTypes::default()).is_empty());- assert!(streamer.update(0, r#"{"arguments": {}}"#, &ToolParameterTypes::default()).is_empty());+ assert!(streamer.update(0, r#"{"name": "wr"#, &ToolParameterTypes::default()).unwrap().is_empty());+ assert!(streamer.update(0, r#"{"arguments": {}}"#, &ToolParameterTypes::default()).unwrap().is_empty());- let deltas = streamer.update(1, r#"{"name": "write", "arguments": {"path":"#, &ToolParameterTypes::default());+ let deltas =+ streamer.update(1, r#"{"name": "write", "arguments": {"path":"#, &ToolParameterTypes::default()).unwrap();assert_eq!(deltas.len(), 1);assert_eq!(deltas[0].index, Some(1));assert_eq!(deltas[0].function.name, "write");@@ -189,7 +190,7 @@ fn json_candidate_announces_name_once_it_is_complete() {},};- let finish = serde_json::to_value(streamer.finish(1, &call)).expect("serializable final delta");+ let finish = serde_json::to_value(streamer.finish(1, &call).unwrap()).expect("serializable final delta");assert_eq!(finish,serde_json::json!({@@ -198,15 +199,14 @@ fn json_candidate_announces_name_once_it_is_complete() {}));- assert!(- streamer.update(1, r#"{"name": "write", "arguments": {"path": "/"#, &ToolParameterTypes::default()).is_empty()- );+ assert!(streamer.update(1, r#"{"name": "write"}"#, &ToolParameterTypes::default()).is_err());}#[test]fn framed_argument_deltas_do_not_repeat_announced_metadata() {let mut streamer = ToolCallStreamer::new();- let deltas = streamer.update(0, "<function=write>\n<parameter=path>\n/tmp/a", &ToolParameterTypes::default());+ let deltas =+ streamer.update(0, "<function=write>\n<parameter=path>\n/tmp/a", &ToolParameterTypes::default()).unwrap();assert_eq!(deltas.len(), 2);let announcement = serde_json::to_value(&deltas[0]).expect("serializable announcement");@@ -234,7 +234,7 @@ fn finish_emits_metadata_when_no_announcement_was_possible() {},};- let finish = serde_json::to_value(streamer.finish(0, &call)).expect("serializable final delta");+ let finish = serde_json::to_value(streamer.finish(0, &call).unwrap()).expect("serializable final delta");assert_eq!(finish["type"], "function");assert_eq!(finish["function"]["name"], "write");assert!(!finish["id"].as_str().expect("string id").is_empty());@@ -258,7 +258,7 @@ fn framed_tool_call_streams_arguments_incrementally() {let chars = final_markup.chars().collect::<Vec<_>>();for i in 0..=chars.len() {let partial: String = chars[..i].iter().collect();- for delta in streamer.update(0, &partial, &ToolParameterTypes::default()) {+ for delta in streamer.update(0, &partial, &ToolParameterTypes::default()).unwrap() {if !delta.function.name.is_empty() {announcements += 1;assert_eq!(delta.function.name, "write");@@ -267,7 +267,7 @@ fn framed_tool_call_streams_arguments_incrementally() {}}assert_eq!(announcements, 1);- fragments.push_str(&streamer.finish(0, &call).function.arguments);+ fragments.push_str(&streamer.finish(0, &call).unwrap().function.arguments);let parsed: serde_json::Value = serde_json::from_str(&fragments).expect("assembled arguments parse");assert_eq!(parsed, final_arguments);@@ -281,11 +281,12 @@ fn framed_tool_call_withholds_typed_parameter_until_complete() {"parameters":{"type":"object","properties":{"options":{"type":"object"}}}}}]"#,);let mut streamer = ToolCallStreamer::new();- let deltas = streamer.update(0, "\n<function=read>\n<parameter=options>\n{\"a\"", &types);+ let deltas = streamer.update(0, "\n<function=read>\n<parameter=options>\n{\"a\"", &types).unwrap();assert!(deltas.iter().all(|delta| delta.function.arguments.is_empty()));- let deltas =- streamer.update(0, "\n<function=read>\n<parameter=options>\n{\"a\": 1}\n</parameter>\n</function>\n", &types);+ let deltas = streamer+ .update(0, "\n<function=read>\n<parameter=options>\n{\"a\": 1}\n</parameter>\n</function>\n", &types)+ .unwrap();let fragment: String = deltas.iter().map(|delta| delta.function.arguments.as_str()).collect();assert_eq!(fragment, r#"{"options":{"a":1}"#);@@ -296,7 +297,7 @@ fn framed_tool_call_withholds_typed_parameter_until_complete() {json: r#"{"options":{"a":1}}"#.to_string(),},};- assert_eq!(streamer.finish(0, &call).function.arguments, "}");+ assert_eq!(streamer.finish(0, &call).unwrap().function.arguments, "}");}#[test]@@ -315,17 +316,17 @@ fn framed_tool_call_streams_multibyte_content_without_panicking() {let chars = final_markup.chars().collect::<Vec<_>>();for i in 0..=chars.len() {let partial: String = chars[..i].iter().collect();- for delta in streamer.update(0, &partial, &ToolParameterTypes::default()) {+ for delta in streamer.update(0, &partial, &ToolParameterTypes::default()).unwrap() {fragments.push_str(&delta.function.arguments);}}- fragments.push_str(&streamer.finish(0, &call).function.arguments);+ fragments.push_str(&streamer.finish(0, &call).unwrap().function.arguments);let parsed: serde_json::Value = serde_json::from_str(&fragments).expect("assembled arguments parse");assert_eq!(parsed, serde_json::json!({ "content": "London — a city" }));}#[test]-fn framed_tool_call_uses_valid_candidate_when_final_call_diverges() {+fn framed_tool_call_rejects_divergent_final_arguments() {let markup = "<function=write_file>\n<parameter=path>\nrepro.rs\n</parameter>\n<parameter=content>\nlet x = \"hello\";\n</parameter>\n</function>";let corrupted_call = ToolCall {identifier: None,@@ -337,13 +338,10 @@ fn framed_tool_call_uses_valid_candidate_when_final_call_diverges() {let mut streamer = ToolCallStreamer::new();let mut fragments = String::new();- for delta in streamer.update(0, markup, &ToolParameterTypes::default()) {+ for delta in streamer.update(0, markup, &ToolParameterTypes::default()).unwrap() {fragments.push_str(&delta.function.arguments);}- fragments.push_str(&streamer.finish(0, &corrupted_call).function.arguments);-- let parsed: serde_json::Value = serde_json::from_str(&fragments).expect("assembled arguments parse");- assert_eq!(parsed, serde_json::json!({"path": "repro.rs", "content": "let x = \"hello\";"}));+ assert!(streamer.finish(0, &corrupted_call).is_err());}#[test]@@ -373,12 +371,12 @@ fn framed_tool_call_with_array_parameter_assembles_exactly() {let chars = final_markup.chars().collect::<Vec<_>>();for i in 0..=chars.len() {let partial: String = chars[..i].iter().collect();- for delta in streamer.update(0, &partial, &types) {+ for delta in streamer.update(0, &partial, &types).unwrap() {fragment_count += 1;fragments.push_str(&delta.function.arguments);}}- fragments.push_str(&streamer.finish(0, &call).function.arguments);+ fragments.push_str(&streamer.finish(0, &call).unwrap().function.arguments);assert!(fragment_count > 2, "fragments: {fragments:?}");let parsed: serde_json::Value = serde_json::from_str(&fragments).expect("assembled arguments parse");@@ -416,14 +414,14 @@ fn framed_string_parameter_streams_json_shaped_text_verbatim() {let chars = final_markup.chars().collect::<Vec<_>>();for i in 0..=chars.len() {let partial: String = chars[..i].iter().collect();- for delta in streamer.update(0, &partial, &types) {+ for delta in streamer.update(0, &partial, &types).unwrap() {if streamed.contains("\"content\":") && !delta.function.arguments.is_empty() {content_fragments += 1;}streamed.push_str(&delta.function.arguments);}}- let closing = streamer.finish(0, &call).function.arguments;+ let closing = streamer.finish(0, &call).unwrap().function.arguments;// the coerced final call, not a hand-written literal: a stream that disagreed with it would fall back// to the streamer's own candidate and still close with "}", so equality with the call is the check@@ -468,75 +466,9 @@ fn declared_types_follow_refs_and_unions() {assert_eq!(coerced["shape"], serde_json::json!("{\"r\": 1}"));}-#[test]-fn declared_types_stop_recursive_union_expansion() {- for keyword in ["anyOf", "oneOf"] {- let mut branches = vec![serde_json::json!({"type": "object"})];- branches.extend(std::iter::repeat_n(serde_json::json!({"$ref": "#"}), 32));- let schema = serde_json::json!({keyword: branches});-- assert_eq!(declared_types(&schema, &schema, 8), ["object"]);- }-}--#[test]-fn declared_types_reuse_shared_union_nodes() {- let mut definitions = serde_json::Map::new();- definitions.insert("0".to_string(), serde_json::json!({"type": ["object", "object", "null"]}));- for level in 1..=6 {- let branches = vec![serde_json::json!({"$ref": format!("#/$defs/{}", level - 1)}); 32];- definitions.insert(level.to_string(), serde_json::json!({"anyOf": branches}));- }- let schema = serde_json::json!({"$ref": "#/$defs/6", "$defs": definitions});-- assert_eq!(declared_types(&schema, &schema, 8), ["null", "object"]);-}--#[test]-fn declared_types_revisit_shared_nodes_with_more_depth() {- let mut nested = serde_json::json!({"$ref": "#/$defs/Shared"});- for _ in 0..6 {- nested = serde_json::json!({"anyOf": [nested]});- }- let schema = serde_json::json!({- "$defs": {"Shared": {"anyOf": [{"type": "string"}]}},- "anyOf": [{"type": "object"}, {"$ref": "#/$defs/Shared"}, nested]- });-- assert_eq!(declared_types(&schema, &schema, 8), ["object", "string"]);-}--#[test]-fn declared_types_discard_partial_results_when_work_is_exhausted() {- let leaves = vec![serde_json::json!({"type": "object"}); 32];- let mut branches = vec![serde_json::json!({"type": "string"})];- branches.extend(std::iter::repeat_n(serde_json::json!({"anyOf": leaves}), 32));- let mut kinds = vec!["object"; MAX_SCHEMA_TRAVERSAL_WORK];- kinds.push("string");-- for property in [serde_json::json!({"anyOf": branches}), serde_json::json!({"type": kinds})] {- let tools = serde_json::json!([{"function": {- "name": "read",- "parameters": {"properties": {"options": property}}- }}]);- let types = parameter_types(&tools.to_string());- assert!(types.declared("read", "options").is_none());-- let call = ToolCall {- identifier: None,- name: "read".to_string(),- arguments: Value {- json: serde_json::json!({"options": "{\"a\": 1}"}).to_string(),- },- };- assert_eq!(coerce_tool_call(&call, &types).arguments.json, call.arguments.json);- assert_eq!(serialize_param_value("{\"a\": 1}", types.declared("read", "options")), r#""{\"a\": 1}""#);- }-}-fn parameter_types(tools_json: &str) -> ToolParameterTypes {let tools: Vec<OaiTool> = serde_json::from_str(tools_json).expect("valid tools json");- ToolParameterTypes::from_tools(Some(&tools))+ tool_parameter_types(Some(&tools))}const SEARCH_TOOL: &str = r#"[{"type":"function","function":{"name":"search","description":"Search",@@ -624,11 +556,11 @@ fn framed_streamer_agrees_with_coerced_boolean_finish() {let chars = final_markup.chars().collect::<Vec<_>>();for i in 0..=chars.len() {let partial: String = chars[..i].iter().collect();- for delta in streamer.update(0, &partial, &types) {+ for delta in streamer.update(0, &partial, &types).unwrap() {fragments.push_str(&delta.function.arguments);}}- fragments.push_str(&streamer.finish(0, &call).function.arguments);+ fragments.push_str(&streamer.finish(0, &call).unwrap().function.arguments);let parsed: serde_json::Value = serde_json::from_str(&fragments).expect("assembled arguments parse");assert_eq!(parsed, serde_json::json!({"query": "cats", "metric": true}));@@ -694,11 +626,11 @@ fn framed_streamer_agrees_with_coerced_finish() {let chars = final_markup.chars().collect::<Vec<_>>();for i in 0..=chars.len() {let partial: String = chars[..i].iter().collect();- for delta in streamer.update(0, &partial, &types) {+ for delta in streamer.update(0, &partial, &types).unwrap() {fragments.push_str(&delta.function.arguments);}}- fragments.push_str(&streamer.finish(0, &call).function.arguments);+ fragments.push_str(&streamer.finish(0, &call).unwrap().function.arguments);let parsed: serde_json::Value = serde_json::from_str(&fragments).expect("assembled arguments parse");assert_eq!(parsed, serde_json::json!({"query": "cats", "limit": 5}));diff --git a/crates/legacy/hanashi/configs/parsing/muse-glimmer.json b/crates/legacy/hanashi/configs/parsing/muse-glimmer.jsonindex 3afbc093b..11c7c40b2 100644--- a/crates/legacy/hanashi/configs/parsing/muse-glimmer.json+++ b/crates/legacy/hanashi/configs/parsing/muse-glimmer.json@@ -439,25 +439,11 @@],"value": [{- "type": "object",- "fields": {- "name": [- {- "type": "get",- "path": [- "$value",- "role"- ]- },- {- "type": "regex_find_all",- "pattern": "^assistant to=(\\S+)"- },- {- "type": "first"- }- ]- }+ "type": "get",+ "path": [+ "$value",+ "content"+ ]}]}@@ -557,35 +543,8 @@],"parse_atem_arguments": [{- "type": "regex_find_all",- "pattern": "<atem:parameter name=\"[^\"]+\">[\\s\\S]*?</atem:parameter>"- },- {- "type": "each",- "apply": [- {- "type": "regex_replace",- "pattern": "<atem:parameter name=\"([^\"]+)\">([{\\[][\\s\\S]*[}\\]])</atem:parameter>",- "template": "\"$1\": $2"- },- {- "type": "regex_replace",- "pattern": "<atem:parameter name=\"([^\"]+)\">([\\s\\S]*?)</atem:parameter>",- "template": "\"$1\": \"$2\""- }- ]- },- {- "type": "join",- "separator": ", "- },- {- "type": "format",- "template": "{ {} }"- },- {- "type": "parse_json",- "repair": true+ "type": "regex_capture_object",+ "pattern": "<atem:parameter name=\"([^\"]+)\">([\\s\\S]*?)</atem:parameter>"}],"parse_tool_output_value": [diff --git a/crates/legacy/hanashi/src/chat/hanashi/messages/canonical/message.rs b/crates/legacy/hanashi/src/chat/hanashi/messages/canonical/message.rsindex fb44d50a7..5514bebaa 100644--- a/crates/legacy/hanashi/src/chat/hanashi/messages/canonical/message.rs+++ b/crates/legacy/hanashi/src/chat/hanashi/messages/canonical/message.rs@@ -129,6 +129,7 @@ impl Message {} => serde_json::to_value(value.clone()),OriginalContentBlock::ToolCallCandidate {value,+ ..} => serde_json::to_value(value.clone()),OriginalContentBlock::ToolCallResult {identifier: _,diff --git a/crates/legacy/hanashi/src/chat/hanashi/messages/streamed/content.rs b/crates/legacy/hanashi/src/chat/hanashi/messages/streamed/content.rsindex 81b985793..8daf0432f 100644--- a/crates/legacy/hanashi/src/chat/hanashi/messages/streamed/content.rs+++ b/crates/legacy/hanashi/src/chat/hanashi/messages/streamed/content.rs@@ -68,6 +68,7 @@ impl Content {} else {tool_calls.push(ChatContentBlock::ToolCallCandidate {value: value.into(),+ identifier: None,});}},@@ -78,6 +79,7 @@ impl Content {} => {tool_calls.push(ChatContentBlock::ToolCallCandidate {value: Value::Null.into(),+ identifier: None,});},Section::ToolCallResult {diff --git a/crates/legacy/hanashi/src/chat/harmony/bridging/message.rs b/crates/legacy/hanashi/src/chat/harmony/bridging/message.rsindex 48b85d7ea..12928430a 100644--- a/crates/legacy/hanashi/src/chat/harmony/bridging/message.rs+++ b/crates/legacy/hanashi/src/chat/harmony/bridging/message.rs@@ -477,7 +477,11 @@ pub fn bridge_messages_from_harmony(messages: &[ExternalMessage]) -> Result<Vec<},Err(_) => {content.push(ChatContentBlock::ToolCallCandidate {- value: Value::from(serde_json::Value::String(text)),+ value: Value::from(serde_json::Value::String(format!(+ "{{\"name\":{},\"arguments\":{text}",+ serde_json::to_string(name).expect("tool name serializes")+ ))),+ identifier: None,});},}diff --git a/crates/legacy/hanashi/tests/test_continuation.rs b/crates/legacy/hanashi/tests/test_continuation.rsindex 6dfd6d112..193412725 100644--- a/crates/legacy/hanashi/tests/test_continuation.rs+++ b/crates/legacy/hanashi/tests/test_continuation.rs@@ -111,6 +111,36 @@ fn add_tool_result(}));}+#[test]+fn muse_glimmer_tool_arguments_survive_continuation() {+ let config = HanashiConfig::MuseGlimmer;+ for options in [+ "printf '%s\\n' \"hello\", \"world\"\n",+ "héllo 🙂\n\"quoted\"\\path\n</atem:parametric>\n",+ "{\n \"b\": 2,\n \"a\": [1, true, null]\n}",+ ] {+ let mut encoding = encoding(config.clone());+ let mut history = history();+ history[0] = ChatMessage::system().with_reasoning_effort(ReasoningEffort::High);+ encoding.encode(history.clone()).unwrap();+ decode(+ &mut encoding,+ &format!(+ " to=inspect<|message|><atem:function_calls>\n<atem:invoke name=\"inspect\">\n\+ <atem:parameter name=\"options\">{options}</atem:parameter>\n\+ </atem:invoke>\n</atem:function_calls><|eot|>"+ ),+ );+ let sampled = encoding.state().tokens.clone();+ add_tool_result(&encoding, &mut history, options);+ let suffix = encoding.try_append(&history).unwrap().expect("must retain the sampled tool call");+ assert!(!suffix.is_empty());+ assert_eq!(&encoding.state().tokens[..sampled.len()], sampled);+ // A fresh session must be able to replay the same call as well.+ assert!(canonical_text(config.clone(), history).contains(options));+ }+}+#[test]fn formatting_and_sampled_token_ids_survive_repeated_tool_continuations() {for config in [HanashiConfig::Qwen35, HanashiConfig::Qwen36, HanashiConfig::Qwen38] {diff --git a/crates/legacy/hanashi/unit/chat/hanashi/messages/streamed/content.rs b/crates/legacy/hanashi/unit/chat/hanashi/messages/streamed/content.rsindex 60a0f29a3..a7555312c 100644--- a/crates/legacy/hanashi/unit/chat/hanashi/messages/streamed/content.rs+++ b/crates/legacy/hanashi/unit/chat/hanashi/messages/streamed/content.rs@@ -1,5 +1,69 @@use super::*;+#[test]+fn qwen38_exposes_tool_candidates_before_the_call_closes() {+ use token_stream_parser::{Parser, token_stream::TokenStreamParser, types::Token};++ use crate::chat::hanashi::{config::HanashiConfig, messages::streamed::Message};+ let mut parser = TokenStreamParser::new(HanashiConfig::Qwen38.resolve().unwrap().parsing).unwrap();+ for value in ["<|im_start|>", "assistant", "\n", "<tool_call>"] {+ parser+ .push(&Token {+ id: 0,+ value: value.into(),+ is_special: false,+ })+ .unwrap();+ }+ let text = "\n<function=write>\n<parameter=content>\nhello";+ for value in text.chars() {+ parser+ .push(&Token {+ id: 0,+ value: value.to_string(),+ is_special: false,+ })+ .unwrap();+ }+ let messages: Vec<Message> = serde_json::from_value(parser.state().value.clone()).unwrap();+ let blocks = messages.into_iter().last().unwrap().content.unwrap().blocks(&ChatRole::Assistant {});+ let candidate = blocks+ .iter()+ .find_map(|block| match block {+ ChatContentBlock::ToolCallCandidate {+ value,+ ..+ } => Some(value),+ _ => None,+ })+ .expect("Qwen must expose the unfinished tool call");+ let raw: String = serde_json::from_str(&candidate.json).unwrap();+ assert!(raw.contains("<function=write>"));+ assert!(raw.ends_with("hello"));+ parser+ .push(&Token {+ id: 0,+ value: "\n</parameter>\n</function>\n".into(),+ is_special: false,+ })+ .unwrap();+ parser+ .push(&Token {+ id: 0,+ value: "</tool_call>".into(),+ is_special: false,+ })+ .unwrap();+ let messages: Vec<Message> = serde_json::from_value(parser.state().value.clone()).unwrap();+ let message: shoji::types::session::chat::ChatMessage = messages.into_iter().last().unwrap().into();+ let calls = message.tool_calls();+ assert_eq!(calls[0].name, "write");+ assert_eq!(+ serde_json::from_str::<Value>(&calls[0].arguments.json).unwrap(),+ serde_json::json!({"content": "hello"})+ );+}+#[test]fn preserves_reasoning_whitespace_but_normalizes_text() {let blocks = Content::Sections(vec![diff --git a/crates/legacy/nagare/src/chat/mod.rs b/crates/legacy/nagare/src/chat/mod.rsindex e0e78ebfc..204db501e 100644--- a/crates/legacy/nagare/src/chat/mod.rs+++ b/crates/legacy/nagare/src/chat/mod.rs@@ -6,6 +6,7 @@ mod chat_instance;mod error;pub mod message;pub mod token;+pub mod tool_call;#[cfg(test)]mod tool_cancellation_tests;@@ -659,27 +660,26 @@ fn build_message(message = message.with_text(text.clone());}for (index, tool_call) in output.tool_calls.iter().enumerate() {+ let supplied_identifier = match tool_call {+ ToolCallState::Finished(call) => call.identifier.clone(),+ ToolCallState::Candidate(_) => None,+ };+ if generated_tool_call_identifiers.len() <= index {+ generated_tool_call_identifiers.resize(index + 1, None);+ }+ let identifier = generated_tool_call_identifiers[index]+ .get_or_insert_with(|| supplied_identifier.unwrap_or_else(|| Uuid::new_v4().to_string()))+ .clone();message = match tool_call {- ToolCallState::Candidate(candidate) => {- message.with_tool_call_candidate(Value::from(serde_json::Value::String(candidate.clone())))- },- ToolCallState::Finished(tool_call) => {- let identifier = tool_call.identifier.clone().or_else(|| {- if generated_tool_call_identifiers.len() <= index {- generated_tool_call_identifiers.resize(index + 1, None);- }- Some(- generated_tool_call_identifiers[index]- .get_or_insert_with(|| Uuid::new_v4().to_string())- .clone(),- )- });- message.with_tool_call(ToolCall {- identifier,- name: tool_call.name.clone(),- arguments: normalize_tool_call_arguments(tool_call.arguments.clone()),- })- },+ ToolCallState::Candidate(candidate) => message.with_block(ChatContentBlock::ToolCallCandidate {+ value: Value::from(serde_json::Value::String(candidate.clone())),+ identifier: Some(identifier),+ }),+ ToolCallState::Finished(tool_call) => message.with_tool_call(ToolCall {+ identifier: Some(identifier),+ name: tool_call.name.clone(),+ arguments: normalize_tool_call_arguments(tool_call.arguments.clone()),+ }),};}messagediff --git a/crates/legacy/nagare/src/chat/token.rs b/crates/legacy/nagare/src/chat/token.rsindex 35cbb634b..9e3e06e6a 100644--- a/crates/legacy/nagare/src/chat/token.rs+++ b/crates/legacy/nagare/src/chat/token.rs@@ -316,9 +316,10 @@ impl Session {} => Some(ToolCallState::Finished(value.clone())),ChatContentBlock::ToolCallCandidate {value,+ ..} => Some(ToolCallState::Candidate(// the block stores the candidate text as a JSON string document- serde_json::from_str::<String>(&value.json).unwrap_or_default(),+ serde_json::from_str::<String>(&value.json).unwrap_or_else(|_| value.json.clone()),)),_ => None,})diff --git a/crates/legacy/nagare/src/chat/tool_call.rs b/crates/legacy/nagare/src/chat/tool_call.rsnew file mode 100644index 000000000..a3b404867--- /dev/null+++ b/crates/legacy/nagare/src/chat/tool_call.rs@@ -0,0 +1,463 @@+use std::{+ collections::{HashMap, HashSet},+ sync::{Arc, Mutex},+};++use shoji::types::basic::{ToolCall, ToolDescription, ToolNamespace, Value, parse_lenient_json};++use super::ChatSessionError;++/// Declared JSON-Schema types of each tool parameter, keyed by function name.+/// Tool-call markup cannot carry types — the parser keeps every parameter the+/// text the model wrote — so the declared schema is what restores the wire+/// types clients validate against.+#[derive(Clone, Default)]+pub struct ToolParameterTypes(HashMap<String, HashMap<String, Vec<String>>>);++impl ToolParameterTypes {+ pub fn from_parameters<'a>(parameters: impl IntoIterator<Item = (&'a str, &'a Value)>) -> Self {+ let mut functions = HashMap::new();+ for (name, parameters) in parameters {+ let Ok(schema) = serde_json::from_str::<serde_json::Value>(¶meters.json) else {+ continue;+ };+ let Some(properties) = schema.get("properties").and_then(serde_json::Value::as_object) else {+ continue;+ };+ let parameters = properties+ .iter()+ .filter_map(|(name, property)| {+ let types = declared_types(property, &schema, 8);+ (!types.is_empty()).then(|| (name.clone(), types))+ })+ .collect();+ functions.insert(name.to_string(), parameters);+ }+ Self(functions)+ }++ fn declared(+ &self,+ function: &str,+ parameter: &str,+ ) -> Option<&[String]> {+ Some(self.0.get(function)?.get(parameter)?.as_slice())+ }+}++// Per parameter: count each queued schema and each entry in a type array.+const MAX_SCHEMA_TRAVERSAL_WORK: usize = 1024;++/// The JSON-Schema types a property schema declares: its `type` keyword, or the+/// union of its `anyOf`/`oneOf` branches, following local `$ref`s (the shape+/// pydantic and schemars clients emit for optional and nested objects).+fn declared_types(+ property: &serde_json::Value,+ root: &serde_json::Value,+ depth: u32,+) -> Vec<String> {+ let mut pending = vec![(property, depth)];+ let mut visited = HashMap::new();+ let mut types = HashSet::new();+ let mut remaining_work = MAX_SCHEMA_TRAVERSAL_WORK - 1;+ while let Some((property, depth)) = pending.pop() {+ if depth == 0 {+ continue;+ }+ let property = match property.get("$ref").and_then(serde_json::Value::as_str) {+ Some(reference) => {+ reference.strip_prefix('#').and_then(|pointer| root.pointer(pointer)).unwrap_or(property)+ },+ None => property,+ };+ // Track identity without hashing the schema's contents. Revisit a shared+ // node only when a shorter path leaves more depth to discover its types.+ let identity = std::ptr::from_ref(property);+ if visited.get(&identity).is_some_and(|seen_depth| *seen_depth >= depth) {+ continue;+ }+ visited.insert(identity, depth);+ match property.get("type") {+ Some(serde_json::Value::String(kind)) => {+ types.insert(kind.as_str());+ },+ Some(serde_json::Value::Array(kinds)) => {+ let Some(remaining) = remaining_work.checked_sub(kinds.len()) else {+ return Vec::new();+ };+ remaining_work = remaining;+ types.extend(kinds.iter().filter_map(serde_json::Value::as_str));+ },+ _ if depth > 1 => {+ for branches in+ ["anyOf", "oneOf"].iter().filter_map(|key| property.get(*key).and_then(serde_json::Value::as_array))+ {+ // Charge work before enqueueing, bounding both visits and+ // pending allocations even for a wide, nonrecursive union.+ let Some(remaining) = remaining_work.checked_sub(branches.len()) else {+ // Partial types may omit a string branch and cause an+ // incorrect coercion, so leave the parameter unchanged.+ return Vec::new();+ };+ remaining_work = remaining;+ pending.extend(branches.iter().map(|branch| (branch, depth - 1)));+ }+ },+ _ => {},+ }+ }+ let mut types: Vec<String> = types.into_iter().map(str::to_string).collect();+ types.sort_unstable();+ types+}++fn matches_declared_type(+ value: &serde_json::Value,+ declared: &[String],+) -> bool {+ declared.iter().any(|kind| match kind.as_str() {+ "integer" => value.as_i64().is_some() || value.as_u64().is_some(),+ "number" => value.is_number(),+ "boolean" => value.is_boolean(),+ "null" => value.is_null(),+ "object" => value.is_object(),+ "array" => value.is_array(),+ "string" => value.is_string(),+ _ => false,+ })+}++/// Parameter text read as JSON: strict first, then with the bounded repair of+/// control characters and bare quotes models leave inside string values, plus+/// the Python-style booleans some models emit in tool markup (qwen3.5 writes+/// `True`/`False`).+pub fn parse_parameter_text(text: &str) -> Option<serde_json::Value> {+ if let Some(parsed) = parse_lenient_json(text) {+ return Some(parsed);+ }+ match text.trim() {+ text if text.eq_ignore_ascii_case("true") => Some(serde_json::Value::Bool(true)),+ text if text.eq_ignore_ascii_case("false") => Some(serde_json::Value::Bool(false)),+ _ => None,+ }+}++fn coerce_parameter_value(+ value: &serde_json::Value,+ declared: &[String],+) -> Option<serde_json::Value> {+ let declares_string = declared.iter().any(|kind| kind == "string");+ match value {+ // The parser kept the markup text as a string; restore the declared+ // type when the text reads as it. A union that includes "string" stays+ // a string: the text is already schema-valid and the intended type is+ // unknowable.+ serde_json::Value::String(text) if !declares_string => {+ let parsed = parse_parameter_text(text)?;+ matches_declared_type(&parsed, declared).then_some(parsed)+ },+ // A JSON-format parser typed a value although the parameter is a+ // plain string.+ value if declared == ["string"] && !value.is_string() => Some(serde_json::Value::String(value.to_string())),+ _ => None,+ }+}++/// Restores the declared types the markup could not carry. Applied only at+/// protocol boundaries: the session keeps the parser's values so its stored+/// history stays consistent with what the template renders.+pub fn coerce_tool_call(+ tool_call: &ToolCall,+ types: &ToolParameterTypes,+) -> ToolCall {+ let Ok(serde_json::Value::Object(mut object)) = serde_json::from_str(&tool_call.arguments.json) else {+ return tool_call.clone();+ };+ let mut changed = false;+ for (parameter, value) in object.iter_mut() {+ if let Some(declared) = types.declared(&tool_call.name, parameter)+ && let Some(coerced) = coerce_parameter_value(value, declared)+ {+ *value = coerced;+ changed = true;+ }+ }+ if !changed {+ return tool_call.clone();+ }+ ToolCall {+ arguments: Value {+ json: serde_json::Value::Object(object).to_string(),+ },+ ..tool_call.clone()+ }+}++enum FramedParam {+ Complete(String),+ Open(String),+}++struct FramedCall {+ name: Option<String>,+ params: Vec<(String, FramedParam)>,+ parameter_close: &'static str,+}++fn parse_framed_call(raw: &str) -> FramedCall {+ let atem = raw.trim_start().starts_with("<atem:");+ let (function_open, parameter_open, name_close, parameter_close) = if atem {+ ("<atem:invoke name=\"", "<atem:parameter name=\"", "\">", "</atem:parameter>")+ } else {+ ("<function=", "<parameter=", ">", "\n</parameter>")+ };+ let name = raw.find(function_open).and_then(|start| {+ let rest = &raw[start + function_open.len()..];+ rest.find(name_close).map(|end| rest[..end].to_string())+ });+ let mut params = Vec::new();+ let mut rest = raw;+ while let Some(start) = rest.find(parameter_open) {+ rest = &rest[start + parameter_open.len()..];+ let Some(tag_end) = rest.find(name_close) else {+ break;+ };+ let key = rest[..tag_end].to_string();+ let value_part = &rest[tag_end + name_close.len()..];+ let value_part = if atem {+ value_part+ } else {+ value_part.strip_prefix('\n').unwrap_or(value_part)+ };+ match value_part.find(parameter_close) {+ Some(end) => {+ params.push((key, FramedParam::Complete(value_part[..end].to_string())));+ rest = &value_part[end + parameter_close.len()..];+ },+ None => {+ params.push((key, FramedParam::Open(value_part.to_string())));+ break;+ },+ }+ }+ FramedCall {+ name,+ params,+ parameter_close,+ }+}++// A completed parameter serializes exactly as coerce_tool_call serializes it in+// the final call, so the streamed prefix is a prefix of the final text.+fn serialize_param_value(+ value: &str,+ declared: Option<&[String]>,+) -> String {+ let text = serde_json::Value::String(value.to_string());+ declared.and_then(|declared| coerce_parameter_value(&text, declared)).unwrap_or(text).to_string()+}++// The longest suffix of `content` that could be the start of the parameter close tag is+// ambiguous until more tokens arrive, so it is withheld from streaming.+fn withhold_ambiguous_tail<'a>(+ content: &'a str,+ close_sequence: &str,+) -> &'a str {+ // the leftmost char boundary whose suffix is a prefix of the close sequence+ // gives the longest ambiguous tail+ let mut end = content.len();+ for (index, _) in content.char_indices().rev().take(close_sequence.len()) {+ let suffix = &content[index..];+ if suffix.len() <= close_sequence.len() && close_sequence.starts_with(suffix) {+ end = index;+ }+ }+ &content[..end]+}++// The arguments text with no closing brace: completed parameters are frozen, the open+// parameter's value streams as it grows.+fn open_arguments_text(+ call: &FramedCall,+ types: &ToolParameterTypes,+) -> Option<String> {+ let mut entries: Vec<(String, String)> = Vec::new();+ for (key, param) in &call.params {+ let declared = call.name.as_deref().and_then(|function| types.declared(function, key));+ let serialized = match param {+ FramedParam::Complete(value) => serialize_param_value(value, declared),+ FramedParam::Open(content) => {+ // an empty value has nothing to emit yet, and a declared non-string+ // value takes its final form (a bare literal) only once complete:+ // either way nothing about the parameter may be emitted+ let streamable = withhold_ambiguous_tail(content, call.parameter_close);+ let declared_non_string =+ declared.is_some_and(|declared| !declared.iter().any(|kind| kind == "string"));+ if streamable.is_empty() || declared_non_string {+ break;+ }+ let quoted = serde_json::Value::String(streamable.to_string()).to_string();+ quoted[..quoted.len() - 1].to_string()+ },+ };+ entries.push((key.clone(), serialized));+ }+ if entries.is_empty() {+ return None;+ }+ let mut out = String::from("{");+ for (index, (key, serialized)) in entries.iter().enumerate() {+ if index > 0 {+ out.push(',');+ }+ out.push_str(&serde_json::to_string(key).expect("key serializes"));+ out.push(':');+ out.push_str(serialized);+ }+ Some(out)+}++fn json_candidate_name(raw: &str) -> Option<String> {+ let mut rest = raw.trim_start().strip_prefix('{')?;+ loop {+ let mut key = serde_json::Deserializer::from_str(rest).into_iter::<String>();+ let name = key.next()?.ok()?;+ rest = rest[key.byte_offset()..].trim_start().strip_prefix(':')?.trim_start();+ if name == "name" {+ return serde_json::Deserializer::from_str(rest).into_iter::<String>().next()?.ok();+ }+ let mut value = serde_json::Deserializer::from_str(rest).into_iter::<serde::de::IgnoredAny>();+ value.next()?.ok()?;+ rest = rest[value.byte_offset()..].trim_start().strip_prefix(',')?;+ }+}++/// Converts model tool-call candidates into append-only JSON argument fragments.+#[bindings::export(Class)]+#[derive(Clone)]+pub struct ToolCallArgumentStream {+ state: Arc<Mutex<ArgumentState>>,+ types: ToolParameterTypes,+}++#[derive(Default)]+struct ArgumentState {+ name: String,+ key_order: Vec<String>,+ open_text: String,+ finished: bool,+}++#[bindings::export(Structure(Class))]+#[derive(Debug, Clone)]+pub struct ToolCallArgumentDelta {+ pub name: String,+ pub arguments: String,+}++impl ToolCallArgumentStream {+ pub fn new(types: ToolParameterTypes) -> Self {+ Self {+ state: Arc::new(Mutex::new(ArgumentState::default())),+ types,+ }+ }+}++#[bindings::export(Implementation)]+impl ToolCallArgumentStream {+ #[bindings::export(Method(Factory))]+ pub fn create(tools: Vec<ToolNamespace>) -> Self {+ let functions = tools.iter().flat_map(|namespace| &namespace.tools).filter_map(|tool| match tool {+ ToolDescription::Function {+ tool_function,+ } => tool_function.parameters.as_ref().map(|parameters| (tool_function.name.as_str(), parameters)),+ });+ Self::new(ToolParameterTypes::from_parameters(functions))+ }++ #[bindings::export(Method)]+ pub fn update(+ &self,+ raw: String,+ ) -> Result<ToolCallArgumentDelta, ChatSessionError> {+ let mut state = self.state.lock().expect("tool argument stream mutex poisoned");+ if state.finished {+ return Err(stream_error("Tool arguments changed after completion"));+ }+ let (name, next) = if raw.trim_start().starts_with('<') {+ let call = parse_framed_call(&raw);+ state.key_order = call.params.iter().map(|(key, _)| key.clone()).collect();+ let next = open_arguments_text(&call, &self.types);+ (call.name, next)+ } else {+ (json_candidate_name(&raw), None)+ };+ if let Some(name) = name.filter(|name| !name.is_empty()) {+ if !state.name.is_empty() && state.name != name {+ return Err(stream_error("Uzu revised a streamed tool name"));+ }+ state.name = name;+ }+ let mut arguments = String::new();+ if let Some(next) = next {+ let suffix = next+ .strip_prefix(&state.open_text)+ .ok_or_else(|| stream_error("Uzu revised streamed tool arguments"))?;+ arguments.push_str(suffix);+ state.open_text = next;+ }+ Ok(ToolCallArgumentDelta {+ name: state.name.clone(),+ arguments,+ })+ }++ #[bindings::export(Method)]+ pub fn finish(+ &self,+ call: ToolCall,+ ) -> Result<ToolCallArgumentDelta, ChatSessionError> {+ let mut state = self.state.lock().expect("tool argument stream mutex poisoned");+ if state.finished || call.name.is_empty() || (!state.name.is_empty() && state.name != call.name) {+ return Err(stream_error("Uzu revised a completed tool call"));+ }+ let call = coerce_tool_call(&call, &self.types);+ let map = serde_json::from_str::<serde_json::Value>(&call.arguments.json)+ .ok()+ .and_then(|value| value.as_object().cloned())+ .filter(|map| !map.contains_key(super::UNPARSED_ARGUMENTS_KEY))+ .ok_or_else(|| stream_error("Uzu returned invalid tool arguments"))?;+ let mut keys = Vec::new();+ let mut seen = HashSet::new();+ for key in state.key_order.iter().chain(map.keys()) {+ if map.contains_key(key) && seen.insert(key) {+ keys.push(key);+ }+ }+ let entries = keys+ .iter()+ .map(|key| format!("{}:{}", serde_json::to_string(key).expect("key serializes"), map[*key]))+ .collect::<Vec<_>>();+ let final_text = format!("{{{}}}", entries.join(","));+ let arguments = final_text+ .strip_prefix(&state.open_text)+ .ok_or_else(|| stream_error("Uzu revised streamed tool arguments"))?+ .to_string();+ state.finished = true;+ Ok(ToolCallArgumentDelta {+ name: call.name,+ arguments,+ })+ }+}++fn stream_error(message: &str) -> ChatSessionError {+ ChatSessionError::Backend {+ message: message.to_string(),+ }+}++#[cfg(test)]+#[path = "tool_call_tests.rs"]+mod tests;diff --git a/crates/legacy/nagare/src/chat/tool_call_tests.rs b/crates/legacy/nagare/src/chat/tool_call_tests.rsnew file mode 100644index 000000000..064ea05aa--- /dev/null+++ b/crates/legacy/nagare/src/chat/tool_call_tests.rs@@ -0,0 +1,166 @@+use super::*;++#[test]+fn json_candidates_use_only_the_top_level_tool_name() {+ assert_eq!(json_candidate_name(r#"{"arguments":{"name":"wrong"},"name":"right"}"#).as_deref(), Some("right"));+ assert_eq!(json_candidate_name(r#"{"name":"wr\u0069te","arguments":{"name":"wrong""#).as_deref(), Some("write"));+ assert_eq!(json_candidate_name(r#"{"arguments":{"name":"wrong""#), None);+}++#[test]+fn candidates_and_completed_calls_keep_the_same_identifier() {+ use shoji::{+ traits::backend::chat_message::{Output, ToolCallState},+ types::session::chat::ChatContentBlock,+ };+ let mut identifiers = Vec::new();+ let candidate = super::super::build_message(+ &Output {+ tool_calls: vec![ToolCallState::Candidate("<function=write>".into())],+ ..Default::default()+ },+ &mut identifiers,+ );+ let ChatContentBlock::ToolCallCandidate {+ identifier: Some(identifier),+ ..+ } = &candidate.content[0]+ else {+ panic!("candidate must have an identifier");+ };+ let finished = super::super::build_message(+ &Output {+ tool_calls: vec![ToolCallState::Finished(ToolCall {+ identifier: None,+ name: "write".into(),+ arguments: Value::from(serde_json::json!({"path": "a"})),+ })],+ ..Default::default()+ },+ &mut identifiers,+ );+ assert_eq!(finished.tool_calls()[0].identifier.as_ref(), Some(identifier));+}++#[test]+fn xml_stream_preserves_escapes_and_closing_tag_prefixes() {+ let text = "héllo 🙂\n\"quoted\"\\\n</parametric>\n";+ let raw = format!("<function=write>\n<parameter=content>\n{text}\n</parameter>\n</function>");+ let stream = ToolCallArgumentStream::create(Vec::new());+ let mut json = String::new();+ for (index, _) in raw.char_indices() {+ json.push_str(&stream.update(raw[..index].into()).unwrap().arguments);+ }+ json.push_str(&stream.update(raw).unwrap().arguments);+ assert!(json.contains("héllo"), "arguments must appear before completion");+ json.push_str(+ &stream+ .finish(ToolCall {+ identifier: None,+ name: "write".into(),+ arguments: Value::from(serde_json::json!({"content": text})),+ })+ .unwrap()+ .arguments,+ );+ assert_eq!(serde_json::from_str::<serde_json::Value>(&json).unwrap(), serde_json::json!({"content": text}));+}++#[test]+fn atem_stream_preserves_parameter_text_and_closing_tag_prefixes() {+ for text in ["\nhéllo 🙂\n\"quoted\"\\\n</atem:parametric>\n", "{\n \"a\": 1, \"b\": [true, null]\n}"] {+ let raw = format!(+ "<atem:function_calls>\n<atem:invoke name=\"write\">\n\+ <atem:parameter name=\"content\">{text}</atem:parameter>\n\+ </atem:invoke>\n</atem:function_calls>"+ );+ let stream = ToolCallArgumentStream::create(Vec::new());+ let mut json = String::new();+ for (index, _) in raw.char_indices() {+ json.push_str(&stream.update(raw[..index].into()).unwrap().arguments);+ if index == raw.find("</atem:parameter>").unwrap() {+ let quoted = serde_json::to_string(text).unwrap();+ assert_eq!(json, format!("{{\"content\":{}", "ed[..quoted.len() - 1]));+ }+ }+ json.push_str(&stream.update(raw).unwrap().arguments);+ json.push_str(+ &stream+ .finish(ToolCall {+ identifier: None,+ name: "write".into(),+ arguments: Value::from(serde_json::json!({"content": text})),+ })+ .unwrap()+ .arguments,+ );+ assert_eq!(serde_json::from_str::<serde_json::Value>(&json).unwrap(), serde_json::json!({"content": text}));+ }+}++#[test]+fn declared_types_stop_recursive_union_expansion() {+ for keyword in ["anyOf", "oneOf"] {+ let mut branches = vec![serde_json::json!({"type": "object"})];+ branches.extend(std::iter::repeat_n(serde_json::json!({"$ref": "#"}), 32));+ let schema = serde_json::json!({keyword: branches});++ assert_eq!(declared_types(&schema, &schema, 8), ["object"]);+ }+}++#[test]+fn declared_types_reuse_shared_union_nodes() {+ let mut definitions = serde_json::Map::new();+ definitions.insert("0".to_string(), serde_json::json!({"type": ["object", "object", "null"]}));+ for level in 1..=6 {+ let branches = vec![serde_json::json!({"$ref": format!("#/$defs/{}", level - 1)}); 32];+ definitions.insert(level.to_string(), serde_json::json!({"anyOf": branches}));+ }+ let schema = serde_json::json!({"$ref": "#/$defs/6", "$defs": definitions});++ assert_eq!(declared_types(&schema, &schema, 8), ["null", "object"]);+}++#[test]+fn declared_types_revisit_shared_nodes_with_more_depth() {+ let mut nested = serde_json::json!({"$ref": "#/$defs/Shared"});+ for _ in 0..6 {+ nested = serde_json::json!({"anyOf": [nested]});+ }+ let schema = serde_json::json!({+ "$defs": {"Shared": {"anyOf": [{"type": "string"}]}},+ "anyOf": [{"type": "object"}, {"$ref": "#/$defs/Shared"}, nested]+ });++ assert_eq!(declared_types(&schema, &schema, 8), ["object", "string"]);+}++#[test]+fn declared_types_discard_partial_results_when_work_is_exhausted() {+ let leaves = vec![serde_json::json!({"type": "object"}); 32];+ let mut branches = vec![serde_json::json!({"type": "string"})];+ branches.extend(std::iter::repeat_n(serde_json::json!({"anyOf": leaves}), 32));+ let mut kinds = vec!["object"; MAX_SCHEMA_TRAVERSAL_WORK];+ kinds.push("string");++ for property in [serde_json::json!({"anyOf": branches}), serde_json::json!({"type": kinds})] {+ let tools = serde_json::json!([{"function": {+ "name": "read",+ "parameters": {"properties": {"options": property}}+ }}]);+ let parameters = Value::from(tools[0]["function"]["parameters"].clone());+ let types = ToolParameterTypes::from_parameters([("read", ¶meters)]);+ assert!(types.declared("read", "options").is_none());++ let call = ToolCall {+ identifier: None,+ name: "read".to_string(),+ arguments: Value {+ json: serde_json::json!({"options": "{\"a\": 1}"}).to_string(),+ },+ };+ assert_eq!(coerce_tool_call(&call, &types).arguments.json, call.arguments.json);+ assert_eq!(serialize_param_value("{\"a\": 1}", types.declared("read", "options")), r#""{\"a\": 1}""#);+ }+}diff --git a/crates/legacy/shoji/src/types/session/chat/content_block.rs b/crates/legacy/shoji/src/types/session/chat/content_block.rsindex 4de09a3b3..b4c24799b 100644--- a/crates/legacy/shoji/src/types/session/chat/content_block.rs+++ b/crates/legacy/shoji/src/types/session/chat/content_block.rs@@ -145,6 +145,9 @@ pub enum ChatContentBlock {},ToolCallCandidate {value: Value,+ /// Assigned by the session and retained when the candidate becomes a tool call.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ identifier: Option<String>,},ToolCallResult {#[serde(rename = "id")]diff --git a/crates/legacy/shoji/src/types/session/chat/message.rs b/crates/legacy/shoji/src/types/session/chat/message.rsindex 7ac585e41..82127bd72 100644--- a/crates/legacy/shoji/src/types/session/chat/message.rs+++ b/crates/legacy/shoji/src/types/session/chat/message.rs@@ -142,6 +142,7 @@ impl ChatMessage {) -> Self {self.with_block(ChatContentBlock::ToolCallCandidate {value,+ identifier: None,})}}diff --git a/crates/legacy/token-stream-parser/tests/test_token_stream_parser_muse_glimmer.rs b/crates/legacy/token-stream-parser/tests/test_token_stream_parser_muse_glimmer.rsindex 6cd689b3a..57245bab9 100644--- a/crates/legacy/token-stream-parser/tests/test_token_stream_parser_muse_glimmer.rs+++ b/crates/legacy/token-stream-parser/tests/test_token_stream_parser_muse_glimmer.rs@@ -244,6 +244,26 @@ mod hermetic {let section = &assistant["content"][0];assert_eq!(section["type"], "tool_call");assert!(section["value"].get("arguments").is_none(), "an unfinished call must not expose arguments: {section}");+ assert_eq!(section["value"], "<", "preserve the raw markup for argument streaming");+ }++ #[test]+ fn tool_call_candidates_preserve_partial_parameter_text() {+ let mut parser = parser();+ let mut next_id = 0;+ feed(&mut parser, "<|start|>user<|message|>hi<|eot|><|start|>assistant to=bash<|message|>", &mut next_id);+ let raw = concat!(+ "<atem:function_calls>\n<atem:invoke name=\"bash\">\n",+ "<atem:parameter name=\"command\">printf '%s\\n' \"hello"+ );+ let mut prefix = String::new();+ for character in raw.chars() {+ prefix.push(character);+ feed(&mut parser, &character.to_string(), &mut next_id);+ let section = &parser.state().value[1]["content"][0];+ assert_eq!(section["type"], "tool_call");+ assert_eq!(section["value"], prefix);+ }}#[test]diff --git a/crates/legacy/uzu/bindings/python/uzu/__init__.pyi b/crates/legacy/uzu/bindings/python/uzu/__init__.pyiindex 3fad9fb92..9fe887149 100644--- a/crates/legacy/uzu/bindings/python/uzu/__init__.pyi+++ b/crates/legacy/uzu/bindings/python/uzu/__init__.pyi@@ -16,6 +16,7 @@ __all__ = ["ChatMessage","ChatMessageMetadata","ChatModelCapabilities",+ "ChatModelInfo","ChatReply","ChatReplyConfig","ChatReplyEnergy",@@ -84,6 +85,8 @@ __all__ = ["TextToSpeechStats","Token","ToolCall",+ "ToolCallArgumentDelta",+ "ToolCallArgumentStream","ToolDescription","ToolFunction","ToolNamespace",@@ -208,10 +211,15 @@ class ChatContentBlock:@typing.finalclass ToolCallCandidate(ChatContentBlock):- __match_args__ = ("value",)+ __match_args__ = ("value", "identifier",)@propertydef value(self) -> Value: ...- def __new__(cls, value: Value) -> ChatContentBlock.ToolCallCandidate: ...+ @property+ def identifier(self) -> typing.Optional[builtins.str]:+ r"""+ Assigned by the session and retained when the candidate becomes a tool call.+ """+ def __new__(cls, value: Value, identifier: typing.Optional[builtins.str]) -> ChatContentBlock.ToolCallCandidate: ...@typing.finalclass ToolCallResult(ChatContentBlock):@@ -315,6 +323,22 @@ class ChatModelCapabilities:def requires_tools(self) -> builtins.bool: ...def __new__(cls, supports_reasoning: builtins.bool, supports_disable_reasoning: builtins.bool, reasoning_efforts: typing.Sequence[ReasoningEffort], supports_tools: builtins.bool, supports_multiple_tool_calls: builtins.bool, requires_tools: builtins.bool) -> ChatModelCapabilities: ...+class ChatModelInfo:+ r"""+ Chat metadata available without loading model weights or allocating a session.+ """+ @property+ def context_length(self) -> typing.Optional[builtins.int]:+ r"""+ Architectural limit; absent before download or for models without a finite limit.+ """+ @property+ def capabilities(self) -> ChatModelCapabilities: ...+ @property+ def default_reasoning_effort(self) -> typing.Optional[ReasoningEffort]: ...+ def __new__(cls, context_length: typing.Optional[builtins.int], capabilities: ChatModelCapabilities, default_reasoning_effort: typing.Optional[ReasoningEffort]) -> ChatModelInfo: ...+@typing.finalclass ChatReply:@property@@ -702,6 +726,7 @@ class Engine:def remove_registry(self, registry_identifier: builtins.str) -> collections.abc.Awaitable[None]: ...def remove_backend(self, identifier: builtins.str) -> collections.abc.Awaitable[None]: ...def models(self) -> collections.abc.Awaitable[builtins.list[Model]]: ...+ def refresh_models(self) -> collections.abc.Awaitable[builtins.list[Model]]: ...def models_on_device(self) -> collections.abc.Awaitable[builtins.list[Model]]: ...def models_remote(self) -> collections.abc.Awaitable[builtins.list[Model]]: ...def models_downloadable(self) -> collections.abc.Awaitable[builtins.list[Model]]: ...@@ -730,6 +755,7 @@ class Engine:def classification(self, model: Model) -> collections.abc.Awaitable[ClassificationSession]: ...def text_to_speech(self, model: Model) -> collections.abc.Awaitable[TextToSpeechSession]: ...def settings(self) -> collections.abc.Awaitable[Settings]: ...+ def chat_model_info(self, model: Model) -> collections.abc.Awaitable[ChatModelInfo | None]: ...@typing.finalclass EngineCallback:@@ -1244,6 +1270,24 @@ class ToolCall:def arguments(self) -> Value: ...def __new__(cls, identifier: typing.Optional[builtins.str], name: builtins.str, arguments: Value) -> ToolCall: ...+class ToolCallArgumentDelta:+ @property+ def name(self) -> builtins.str: ...+ @property+ def arguments(self) -> builtins.str: ...+ def __new__(cls, name: builtins.str, arguments: builtins.str) -> ToolCallArgumentDelta: ...++class ToolCallArgumentStream:+ r"""+ Converts model tool-call candidates into append-only JSON argument fragments.+ """+ @staticmethod+ def create(tools: typing.Sequence[ToolNamespace]) -> ToolCallArgumentStream: ...+ def update(self, raw: builtins.str) -> ToolCallArgumentDelta: ...+ def finish(self, call: ToolCall) -> ToolCallArgumentDelta: ...+class ToolDescription:@typing.finalclass Function(ToolDescription):diff --git a/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/Extensions.swift b/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/Extensions.swiftindex 4f261b000..7efadfa9c 100644--- a/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/Extensions.swift+++ b/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/Extensions.swift@@ -108,6 +108,12 @@ extension Player {}}+extension ToolCallArgumentStream {+ public static func create(tools: [ToolNamespace]) -> ToolCallArgumentStream {+ toolCallArgumentStreamCreate(tools: tools)+ }+}+extension EngineCallback {public typealias Closure = @Sendable () -> Voiddiff --git a/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/nagare.swift b/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/nagare.swiftindex 3fe96ba56..7c6b2ea65 100644--- a/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/nagare.swift+++ b/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/nagare.swift@@ -1609,6 +1609,194 @@ public func FfiConverterTypeTextToSpeechSessionStream_lower(_ value: TextToSpeec+++/**+ * Converts model tool-call candidates into append-only JSON argument fragments.+ */+public protocol ToolCallArgumentStreamProtocol: AnyObject, Sendable {++ func finish(call: ToolCall) throws -> ToolCallArgumentDelta++ func update(raw: String) throws -> ToolCallArgumentDelta++}+/**+ * Converts model tool-call candidates into append-only JSON argument fragments.+ */+open class ToolCallArgumentStream: ToolCallArgumentStreamProtocol, @unchecked Sendable {+ fileprivate let handle: UInt64++ /// Used to instantiate a [FFIObject] without an actual handle, for fakes in tests, mostly.+#if swift(>=5.8)+ @_documentation(visibility: private)+#endif+ public struct NoHandle {+ public init() {}+ }++ // TODO: We'd like this to be `private` but for Swifty reasons,+ // we can't implement `FfiConverter` without making this `required` and we can't+ // make it `required` without making it `public`.+#if swift(>=5.8)+ @_documentation(visibility: private)+#endif+ required public init(unsafeFromHandle handle: UInt64) {+ self.handle = handle+ }++ // This constructor can be used to instantiate a fake object.+ // - Parameter noHandle: Placeholder value so we can have a constructor separate from the default empty one that may be implemented for classes extending [FFIObject].+ //+ // - Warning:+ // Any object instantiated with this constructor cannot be passed to an actual Rust-backed object. Since there isn't a backing handle the FFI lower functions will crash.+#if swift(>=5.8)+ @_documentation(visibility: private)+#endif+ public init(noHandle: NoHandle) {+ self.handle = 0+ }++#if swift(>=5.8)+ @_documentation(visibility: private)+#endif+ public func uniffiCloneHandle() -> UInt64 {+ return try! rustCall { uniffi_nagare_fn_clone_toolcallargumentstream(self.handle, $0) }+ }+ // No primary constructor declared for this class.++ deinit {+ if handle == 0 {+ // Mock objects have handle=0 don't try to free them+ return+ }++ try! rustCall { uniffi_nagare_fn_free_toolcallargumentstream(handle, $0) }+ }+++++open func finish(call: ToolCall)throws -> ToolCallArgumentDelta {+ return try FfiConverterTypeToolCallArgumentDelta_lift(try rustCallWithError(FfiConverterTypeChatSessionError_lift) {+ uniffi_nagare_fn_method_toolcallargumentstream_finish(+ self.uniffiCloneHandle(),+ FfiConverterTypeToolCall_lower(call),$0+ )+})+}++open func update(raw: String)throws -> ToolCallArgumentDelta {+ return try FfiConverterTypeToolCallArgumentDelta_lift(try rustCallWithError(FfiConverterTypeChatSessionError_lift) {+ uniffi_nagare_fn_method_toolcallargumentstream_update(+ self.uniffiCloneHandle(),+ FfiConverterString.lower(raw),$0+ )+})+}++++}+++#if swift(>=5.8)+@_documentation(visibility: private)+#endif+public struct FfiConverterTypeToolCallArgumentStream: FfiConverter {+ typealias FfiType = UInt64+ typealias SwiftType = ToolCallArgumentStream++ public static func lift(_ handle: UInt64) throws -> ToolCallArgumentStream {+ return ToolCallArgumentStream(unsafeFromHandle: handle)+ }++ public static func lower(_ value: ToolCallArgumentStream) -> UInt64 {+ return value.uniffiCloneHandle()+ }++ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ToolCallArgumentStream {+ let handle: UInt64 = try readInt(&buf)+ return try lift(handle)+ }++ public static func write(_ value: ToolCallArgumentStream, into buf: inout [UInt8]) {+ writeInt(&buf, lower(value))+ }+}+++#if swift(>=5.8)+@_documentation(visibility: private)+#endif+public func FfiConverterTypeToolCallArgumentStream_lift(_ handle: UInt64) throws -> ToolCallArgumentStream {+ return try FfiConverterTypeToolCallArgumentStream.lift(handle)+}++#if swift(>=5.8)+@_documentation(visibility: private)+#endif+public func FfiConverterTypeToolCallArgumentStream_lower(_ value: ToolCallArgumentStream) -> UInt64 {+ return FfiConverterTypeToolCallArgumentStream.lower(value)+}+++++public struct ToolCallArgumentDelta: Equatable, Hashable, Codable {+ public var name: String+ public var arguments: String++ // Default memberwise initializers are never public by default, so we+ // declare one manually.+ public init(name: String, arguments: String) {+ self.name = name+ self.arguments = arguments+ }+++++}++#if compiler(>=6)+extension ToolCallArgumentDelta: Sendable {}+#endif++#if swift(>=5.8)+@_documentation(visibility: private)+#endif+public struct FfiConverterTypeToolCallArgumentDelta: FfiConverterRustBuffer {+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ToolCallArgumentDelta {+ return+ try ToolCallArgumentDelta(+ name: FfiConverterString.read(from: &buf),+ arguments: FfiConverterString.read(from: &buf)+ )+ }++ public static func write(_ value: ToolCallArgumentDelta, into buf: inout [UInt8]) {+ FfiConverterString.write(value.name, into: &buf)+ FfiConverterString.write(value.arguments, into: &buf)+ }+}+++#if swift(>=5.8)+@_documentation(visibility: private)+#endif+public func FfiConverterTypeToolCallArgumentDelta_lift(_ buf: RustBuffer) throws -> ToolCallArgumentDelta {+ return try FfiConverterTypeToolCallArgumentDelta.lift(buf)+}++#if swift(>=5.8)+@_documentation(visibility: private)+#endif+public func FfiConverterTypeToolCallArgumentDelta_lower(_ value: ToolCallArgumentDelta) -> RustBuffer {+ return FfiConverterTypeToolCallArgumentDelta.lower(value)+}++public enum ChatSessionError: Swift.Error, Equatable, Hashable, Codable, Foundation.LocalizedError {@@ -2663,6 +2851,31 @@ fileprivate struct FfiConverterSequenceTypeClassificationMessage: FfiConverterRureturn seq}}++#if swift(>=5.8)+@_documentation(visibility: private)+#endif+fileprivate struct FfiConverterSequenceTypeToolNamespace: FfiConverterRustBuffer {+ typealias SwiftType = [ToolNamespace]++ public static func write(_ value: [ToolNamespace], into buf: inout [UInt8]) {+ let len = Int32(value.count)+ writeInt(&buf, len)+ for item in value {+ FfiConverterTypeToolNamespace.write(item, into: &buf)+ }+ }++ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> [ToolNamespace] {+ let len: Int32 = try readInt(&buf)+ var seq = [ToolNamespace]()+ seq.reserveCapacity(Int(len))+ for _ in 0 ..< len {+ seq.append(try FfiConverterTypeToolNamespace.read(from: &buf))+ }+ return seq+ }+}private let UNIFFI_RUST_FUTURE_POLL_READY: Int8 = 0private let UNIFFI_RUST_FUTURE_POLL_WAKE: Int8 = 1@@ -2801,6 +3014,13 @@ private func uniffiForeignFutureDroppedCallback(handle: UInt64) {public func uniffiForeignFutureHandleCountNagare() -> Int {UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.count}+public func toolCallArgumentStreamCreate(tools: [ToolNamespace]) -> ToolCallArgumentStream {+ return try! FfiConverterTypeToolCallArgumentStream_lift(try! rustCall() {+ uniffi_nagare_fn_func_tool_call_argument_stream_create(+ FfiConverterSequenceTypeToolNamespace.lower(tools),$0+ )+})+}private enum InitializationResult {case ok@@ -2817,6 +3037,9 @@ private let initializationResult: InitializationResult = {if bindings_contract_version != scaffolding_contract_version {return InitializationResult.contractVersionMismatch}+ if (uniffi_nagare_checksum_func_tool_call_argument_stream_create() != 13199) {+ return InitializationResult.apiChecksumMismatch+ }if (uniffi_nagare_checksum_method_chatsession_add_foreign_tool() != 48906) {return InitializationResult.apiChecksumMismatch}@@ -2853,6 +3076,12 @@ private let initializationResult: InitializationResult = {if (uniffi_nagare_checksum_method_chatinstance_model() != 52758) {return InitializationResult.apiChecksumMismatch}+ if (uniffi_nagare_checksum_method_toolcallargumentstream_finish() != 39556) {+ return InitializationResult.apiChecksumMismatch+ }+ if (uniffi_nagare_checksum_method_toolcallargumentstream_update() != 26013) {+ return InitializationResult.apiChecksumMismatch+ }if (uniffi_nagare_checksum_method_classificationsession_classify() != 62366) {return InitializationResult.apiChecksumMismatch}diff --git a/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/shoji.swift b/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/shoji.swiftindex 079faabc1..6cec313b8 100644--- a/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/shoji.swift+++ b/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/shoji.swift@@ -3225,7 +3225,10 @@ public enum ChatContentBlock: Equatable, Hashable, Codable {)case toolCall(value: ToolCall)- case toolCallCandidate(value: Value+ case toolCallCandidate(value: Value,+ /**+ * Assigned by the session and retained when the candidate becomes a tool call.+ */identifier: String?)case toolCallResult(identifier: String?, name: String?, value: Value)@@ -3293,7 +3296,7 @@ public struct FfiConverterTypeChatContentBlock: FfiConverterRustBuffer {case 13: return .toolCall(value: try FfiConverterTypeToolCall.read(from: &buf))- case 14: return .toolCallCandidate(value: try FfiConverterTypeValue.read(from: &buf)+ case 14: return .toolCallCandidate(value: try FfiConverterTypeValue.read(from: &buf), identifier: try FfiConverterOptionString.read(from: &buf))case 15: return .toolCallResult(identifier: try FfiConverterOptionString.read(from: &buf), name: try FfiConverterOptionString.read(from: &buf), value: try FfiConverterTypeValue.read(from: &buf)@@ -3378,9 +3381,10 @@ public struct FfiConverterTypeChatContentBlock: FfiConverterRustBuffer {FfiConverterTypeToolCall.write(value, into: &buf)- case let .toolCallCandidate(value):+ case let .toolCallCandidate(value,identifier):writeInt(&buf, Int32(14))FfiConverterTypeValue.write(value, into: &buf)+ FfiConverterOptionString.write(identifier, into: &buf)case let .toolCallResult(identifier,name,value):diff --git a/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/uzu.swift b/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/uzu.swiftindex e023ba256..c9c0b3f64 100644--- a/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/uzu.swift+++ b/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/uzu.swift@@ -422,6 +422,22 @@ private let UNIFFI_CALLBACK_SUCCESS: Int32 = 0private let UNIFFI_CALLBACK_ERROR: Int32 = 1private let UNIFFI_CALLBACK_UNEXPECTED_ERROR: Int32 = 2+#if swift(>=5.8)+@_documentation(visibility: private)+#endif+fileprivate struct FfiConverterUInt32: FfiConverterPrimitive {+ typealias FfiType = UInt32+ typealias SwiftType = UInt32++ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UInt32 {+ return try lift(readInt(&buf))+ }++ public static func write(_ value: SwiftType, into buf: inout [UInt8]) {+ writeInt(&buf, lower(value))+ }+}+#if swift(>=5.8)@_documentation(visibility: private)#endif@@ -890,6 +906,12 @@ public protocol EngineProtocol: AnyObject, Sendable {func modelsRemote() async throws -> [Model]+ /**+ * Refresh registries and wait until the catalog has been published to storage.+ * Registries that fail to refresh retain their last known entries.+ */+ func refreshModels() async throws -> [Model]+func registerCallback(callback: EngineCallback) async throwsfunc removeBackend(identifier: String) async@@ -900,6 +922,13 @@ public protocol EngineProtocol: AnyObject, Sendable {func textToSpeech(model: Model) async throws -> TextToSpeechSession+ /**+ * Returns metadata for a Uzu chat model, including models not yet downloaded.+ * Encoding capabilities come from the catalog; the context limit needs local configuration.+ * Remote and non-chat models return `None`.+ */+ func chatModelInfo(model: Model) async throws -> ChatModelInfo?+}open class Engine: EngineProtocol, @unchecked Sendable {fileprivate let handle: UInt64@@ -1408,6 +1437,27 @@ open func modelsRemote()async throws -> [Model] {)}+ /**+ * Refresh registries and wait until the catalog has been published to storage.+ * Registries that fail to refresh retain their last known entries.+ */+open func refreshModels()async throws -> [Model] {+ return+ try await uniffiRustCallAsync(+ rustFutureFunc: {+ uniffi_uzu_fn_method_engine_refresh_models(+ self.uniffiCloneHandle()++ )+ },+ pollFunc: ffi_uzu_rust_future_poll_rust_buffer,+ completeFunc: ffi_uzu_rust_future_complete_rust_buffer,+ freeFunc: ffi_uzu_rust_future_free_rust_buffer,+ liftFunc: FfiConverterSequenceTypeModel.lift,+ errorHandler: FfiConverterTypeEngineError_lift+ )+}+open func registerCallback(callback: EngineCallback)async throws {returntry await uniffiRustCallAsync(@@ -1494,6 +1544,28 @@ open func textToSpeech(model: Model)async throws -> TextToSpeechSession {)}+ /**+ * Returns metadata for a Uzu chat model, including models not yet downloaded.+ * Encoding capabilities come from the catalog; the context limit needs local configuration.+ * Remote and non-chat models return `None`.+ */+open func chatModelInfo(model: Model)async throws -> ChatModelInfo? {+ return+ try await uniffiRustCallAsync(+ rustFutureFunc: {+ uniffi_uzu_fn_method_engine_chat_model_info(+ self.uniffiCloneHandle(),+ FfiConverterTypeModel_lower(model)+ )+ },+ pollFunc: ffi_uzu_rust_future_poll_rust_buffer,+ completeFunc: ffi_uzu_rust_future_complete_rust_buffer,+ freeFunc: ffi_uzu_rust_future_free_rust_buffer,+ liftFunc: FfiConverterOptionTypeChatModelInfo.lift,+ errorHandler: FfiConverterTypeEngineError_lift+ )+}+}@@ -1920,6 +1992,73 @@ public func FfiConverterTypeSettings_lower(_ value: Settings) -> UInt64 {+/**+ * Chat metadata available without loading model weights or allocating a session.+ */+public struct ChatModelInfo: Equatable, Hashable, Codable {+ /**+ * Architectural limit; absent before download or for models without a finite limit.+ */+ public var contextLength: UInt32?+ public var capabilities: ChatModelCapabilities+ public var defaultReasoningEffort: ReasoningEffort?++ // Default memberwise initializers are never public by default, so we+ // declare one manually.+ public init(+ /**+ * Architectural limit; absent before download or for models without a finite limit.+ */contextLength: UInt32?, capabilities: ChatModelCapabilities, defaultReasoningEffort: ReasoningEffort?) {+ self.contextLength = contextLength+ self.capabilities = capabilities+ self.defaultReasoningEffort = defaultReasoningEffort+ }+++++}++#if compiler(>=6)+extension ChatModelInfo: Sendable {}+#endif++#if swift(>=5.8)+@_documentation(visibility: private)+#endif+public struct FfiConverterTypeChatModelInfo: FfiConverterRustBuffer {+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ChatModelInfo {+ return+ try ChatModelInfo(+ contextLength: FfiConverterOptionUInt32.read(from: &buf),+ capabilities: FfiConverterTypeChatModelCapabilities.read(from: &buf),+ defaultReasoningEffort: FfiConverterOptionTypeReasoningEffort.read(from: &buf)+ )+ }++ public static func write(_ value: ChatModelInfo, into buf: inout [UInt8]) {+ FfiConverterOptionUInt32.write(value.contextLength, into: &buf)+ FfiConverterTypeChatModelCapabilities.write(value.capabilities, into: &buf)+ FfiConverterOptionTypeReasoningEffort.write(value.defaultReasoningEffort, into: &buf)+ }+}+++#if swift(>=5.8)+@_documentation(visibility: private)+#endif+public func FfiConverterTypeChatModelInfo_lift(_ buf: RustBuffer) throws -> ChatModelInfo {+ return try FfiConverterTypeChatModelInfo.lift(buf)+}++#if swift(>=5.8)+@_documentation(visibility: private)+#endif+public func FfiConverterTypeChatModelInfo_lower(_ value: ChatModelInfo) -> RustBuffer {+ return FfiConverterTypeChatModelInfo.lower(value)+}++public struct Device: Equatable, Hashable, Codable {public var osName: String?public var cpuName: String?@@ -2983,6 +3122,30 @@ public func FfiConverterCallbackInterfaceEngineCallbackHandler_lower(_ v: Enginereturn FfiConverterCallbackInterfaceEngineCallbackHandler.lower(v)}+#if swift(>=5.8)+@_documentation(visibility: private)+#endif+fileprivate struct FfiConverterOptionUInt32: FfiConverterRustBuffer {+ typealias SwiftType = UInt32?++ public static func write(_ value: SwiftType, into buf: inout [UInt8]) {+ guard let value = value else {+ writeInt(&buf, Int8(0))+ return+ }+ writeInt(&buf, Int8(1))+ FfiConverterUInt32.write(value, into: &buf)+ }++ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {+ switch try readInt(&buf) as Int8 {+ case 0: return nil+ case 1: return try FfiConverterUInt32.read(from: &buf)+ default: throw UniffiInternalError.unexpectedOptionalTag+ }+ }+}+#if swift(>=5.8)@_documentation(visibility: private)#endif@@ -3055,6 +3218,54 @@ fileprivate struct FfiConverterOptionTypeModel: FfiConverterRustBuffer {}}+#if swift(>=5.8)+@_documentation(visibility: private)+#endif+fileprivate struct FfiConverterOptionTypeChatModelInfo: FfiConverterRustBuffer {+ typealias SwiftType = ChatModelInfo?++ public static func write(_ value: SwiftType, into buf: inout [UInt8]) {+ guard let value = value else {+ writeInt(&buf, Int8(0))+ return+ }+ writeInt(&buf, Int8(1))+ FfiConverterTypeChatModelInfo.write(value, into: &buf)+ }++ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {+ switch try readInt(&buf) as Int8 {+ case 0: return nil+ case 1: return try FfiConverterTypeChatModelInfo.read(from: &buf)+ default: throw UniffiInternalError.unexpectedOptionalTag+ }+ }+}++#if swift(>=5.8)+@_documentation(visibility: private)+#endif+fileprivate struct FfiConverterOptionTypeReasoningEffort: FfiConverterRustBuffer {+ typealias SwiftType = ReasoningEffort?++ public static func write(_ value: SwiftType, into buf: inout [UInt8]) {+ guard let value = value else {+ writeInt(&buf, Int8(0))+ return+ }+ writeInt(&buf, Int8(1))+ FfiConverterTypeReasoningEffort.write(value, into: &buf)+ }++ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {+ switch try readInt(&buf) as Int8 {+ case 0: return nil+ case 1: return try FfiConverterTypeReasoningEffort.read(from: &buf)+ default: throw UniffiInternalError.unexpectedOptionalTag+ }+ }+}+#if swift(>=5.8)@_documentation(visibility: private)#endif@@ -3377,6 +3588,9 @@ private let initializationResult: InitializationResult = {if (uniffi_uzu_checksum_method_engine_models_remote() != 36115) {return InitializationResult.apiChecksumMismatch}+ if (uniffi_uzu_checksum_method_engine_refresh_models() != 4090) {+ return InitializationResult.apiChecksumMismatch+ }if (uniffi_uzu_checksum_method_engine_register_callback() != 18383) {return InitializationResult.apiChecksumMismatch}@@ -3392,6 +3606,9 @@ private let initializationResult: InitializationResult = {if (uniffi_uzu_checksum_method_engine_text_to_speech() != 62782) {return InitializationResult.apiChecksumMismatch}+ if (uniffi_uzu_checksum_method_engine_chat_model_info() != 60496) {+ return InitializationResult.apiChecksumMismatch+ }if (uniffi_uzu_checksum_method_downloader_delete() != 65010) {return InitializationResult.apiChecksumMismatch}diff --git a/crates/legacy/uzu/bindings/typescript/src/napi/index.d.mts b/crates/legacy/uzu/bindings/typescript/src/napi/index.d.mtsindex 9b8ceb87f..664c2e0ba 100644--- a/crates/legacy/uzu/bindings/typescript/src/napi/index.d.mts+++ b/crates/legacy/uzu/bindings/typescript/src/napi/index.d.mts@@ -139,6 +139,19 @@ export declare class TextToSpeechSessionStreamChunkOutput {constructor(output: TextToSpeechOutput)}+export declare class ToolCallArgumentDelta {+ name: string+ arguments: string+ constructor(name: string, arguments: string)+}++/** Converts model tool-call candidates into append-only JSON argument fragments. */+export declare class ToolCallArgumentStream {+ update(raw: string): ToolCallArgumentDelta+ finish(call: ToolCall): ToolCallArgumentDelta+ static create(tools: Array<ToolNamespace>): ToolCallArgumentStream+}+export type ChatSessionError =| { type: 'Backend', message: string }| { type: 'Loading', message: string }@@ -259,7 +272,8 @@ export declare class ChatContentBlockToolCall {export declare class ChatContentBlockToolCallCandidate {value: any- constructor(value: any)+ identifier?: string+ constructor(value: any, identifier?: string)}export declare class ChatContentBlockToolCallResult {@@ -912,6 +926,15 @@ export type ToolDescription =export type TranslationPayload =TranslationPayloadText | TranslationPayloadImage+/** Chat metadata available without loading model weights or allocating a session. */+export declare class ChatModelInfo {+ /** Architectural limit; absent before download or for models without a finite limit. */+ contextLength?: number+ capabilities: ChatModelCapabilities+ defaultReasoningEffort?: ReasoningEffort+ constructor(contextLength?: number, capabilities: ChatModelCapabilities, defaultReasoningEffort?: ReasoningEffort)+}+export declare class Device {osName?: stringcpuName?: string@@ -942,11 +965,23 @@ export declare class DownloaderStream {}export declare class Engine {+ /**+ * Returns metadata for a Uzu chat model, including models not yet downloaded.+ * Encoding capabilities come from the catalog; the context limit needs local configuration.+ * Remote and non-chat models return `None`.+ */+ chatModelInfo(model: Model): Promise<ChatModelInfo | null>+static create(config: EngineConfig): Promise<Engine>registerCallback(callback: EngineCallback): Promise<void>removeRegistry(registryIdentifier: string): Promise<void>removeBackend(identifier: string): Promise<void>get models(): Promise<Array<Model>>+ /**+ * Refresh registries and wait until the catalog has been published to storage.+ * Registries that fail to refresh retain their last known entries.+ */+ refreshModels(): Promise<Array<Model>>get modelsOnDevice(): Promise<Array<Model>>get modelsRemote(): Promise<Array<Model>>get modelsDownloadable(): Promise<Array<Model>>diff --git a/crates/legacy/uzu/bindings/typescript/src/napi/index.d.ts b/crates/legacy/uzu/bindings/typescript/src/napi/index.d.tsindex 9b8ceb87f..664c2e0ba 100644--- a/crates/legacy/uzu/bindings/typescript/src/napi/index.d.ts+++ b/crates/legacy/uzu/bindings/typescript/src/napi/index.d.ts@@ -139,6 +139,19 @@ export declare class TextToSpeechSessionStreamChunkOutput {constructor(output: TextToSpeechOutput)}+export declare class ToolCallArgumentDelta {+ name: string+ arguments: string+ constructor(name: string, arguments: string)+}++/** Converts model tool-call candidates into append-only JSON argument fragments. */+export declare class ToolCallArgumentStream {+ update(raw: string): ToolCallArgumentDelta+ finish(call: ToolCall): ToolCallArgumentDelta+ static create(tools: Array<ToolNamespace>): ToolCallArgumentStream+}+export type ChatSessionError =| { type: 'Backend', message: string }| { type: 'Loading', message: string }@@ -259,7 +272,8 @@ export declare class ChatContentBlockToolCall {export declare class ChatContentBlockToolCallCandidate {value: any- constructor(value: any)+ identifier?: string+ constructor(value: any, identifier?: string)}export declare class ChatContentBlockToolCallResult {@@ -912,6 +926,15 @@ export type ToolDescription =export type TranslationPayload =TranslationPayloadText | TranslationPayloadImage+/** Chat metadata available without loading model weights or allocating a session. */+export declare class ChatModelInfo {+ /** Architectural limit; absent before download or for models without a finite limit. */+ contextLength?: number+ capabilities: ChatModelCapabilities+ defaultReasoningEffort?: ReasoningEffort+ constructor(contextLength?: number, capabilities: ChatModelCapabilities, defaultReasoningEffort?: ReasoningEffort)+}+export declare class Device {osName?: stringcpuName?: string@@ -942,11 +965,23 @@ export declare class DownloaderStream {}export declare class Engine {+ /**+ * Returns metadata for a Uzu chat model, including models not yet downloaded.+ * Encoding capabilities come from the catalog; the context limit needs local configuration.+ * Remote and non-chat models return `None`.+ */+ chatModelInfo(model: Model): Promise<ChatModelInfo | null>+static create(config: EngineConfig): Promise<Engine>registerCallback(callback: EngineCallback): Promise<void>removeRegistry(registryIdentifier: string): Promise<void>removeBackend(identifier: string): Promise<void>get models(): Promise<Array<Model>>+ /**+ * Refresh registries and wait until the catalog has been published to storage.+ * Registries that fail to refresh retain their last known entries.+ */+ refreshModels(): Promise<Array<Model>>get modelsOnDevice(): Promise<Array<Model>>get modelsRemote(): Promise<Array<Model>>get modelsDownloadable(): Promise<Array<Model>>diff --git a/crates/legacy/uzu/bindings/typescript/src/napi/index.js b/crates/legacy/uzu/bindings/typescript/src/napi/index.jsindex ce8eacae0..eb62fa4f7 100644--- a/crates/legacy/uzu/bindings/typescript/src/napi/index.js+++ b/crates/legacy/uzu/bindings/typescript/src/napi/index.js@@ -529,6 +529,8 @@ module.exports.TextToSpeechSession = nativeBinding.TextToSpeechSessionmodule.exports.TextToSpeechSessionStream = nativeBinding.TextToSpeechSessionStreammodule.exports.TextToSpeechSessionStreamChunkError = nativeBinding.TextToSpeechSessionStreamChunkErrormodule.exports.TextToSpeechSessionStreamChunkOutput = nativeBinding.TextToSpeechSessionStreamChunkOutput+module.exports.ToolCallArgumentDelta = nativeBinding.ToolCallArgumentDelta+module.exports.ToolCallArgumentStream = nativeBinding.ToolCallArgumentStreammodule.exports.ChatSessionState = nativeBinding.ChatSessionStatemodule.exports.ClassificationSessionState = nativeBinding.ClassificationSessionStatemodule.exports.TextToSpeechSessionState = nativeBinding.TextToSpeechSessionState@@ -624,6 +626,7 @@ module.exports.HashMethod = nativeBinding.HashMethodmodule.exports.ImageFormat = nativeBinding.ImageFormatmodule.exports.ImageTheme = nativeBinding.ImageThememodule.exports.ReasoningEffort = nativeBinding.ReasoningEffort+module.exports.ChatModelInfo = nativeBinding.ChatModelInfomodule.exports.Device = nativeBinding.Devicemodule.exports.Downloader = nativeBinding.Downloadermodule.exports.DownloaderStream = nativeBinding.DownloaderStreamdiff --git a/crates/legacy/uzu/bindings/typescript/src/napi/index.mjs b/crates/legacy/uzu/bindings/typescript/src/napi/index.mjsindex 7f4fb5da0..30424c3e3 100644--- a/crates/legacy/uzu/bindings/typescript/src/napi/index.mjs+++ b/crates/legacy/uzu/bindings/typescript/src/napi/index.mjs@@ -26,6 +26,8 @@ export const TextToSpeechSession = cjs.TextToSpeechSession;export const TextToSpeechSessionStream = cjs.TextToSpeechSessionStream;export const TextToSpeechSessionStreamChunkError = cjs.TextToSpeechSessionStreamChunkError;export const TextToSpeechSessionStreamChunkOutput = cjs.TextToSpeechSessionStreamChunkOutput;+export const ToolCallArgumentDelta = cjs.ToolCallArgumentDelta;+export const ToolCallArgumentStream = cjs.ToolCallArgumentStream;export const ChatSessionState = cjs.ChatSessionState;export const ClassificationSessionState = cjs.ClassificationSessionState;export const TextToSpeechSessionState = cjs.TextToSpeechSessionState;@@ -121,6 +123,7 @@ export const HashMethod = cjs.HashMethod;export const ImageFormat = cjs.ImageFormat;export const ImageTheme = cjs.ImageTheme;export const ReasoningEffort = cjs.ReasoningEffort;+export const ChatModelInfo = cjs.ChatModelInfo;export const Device = cjs.Device;export const Downloader = cjs.Downloader;export const DownloaderStream = cjs.DownloaderStream;diff --git a/crates/legacy/uzu/src/engine/catalog_tests.rs b/crates/legacy/uzu/src/engine/catalog_tests.rsindex cb0d29852..e14ffa41f 100644--- a/crates/legacy/uzu/src/engine/catalog_tests.rs+++ b/crates/legacy/uzu/src/engine/catalog_tests.rs@@ -113,6 +113,120 @@ async fn engine() -> (Engine, TempDir) {)}+#[tokio::test]+async fn refresh_models_waits_for_in_flight_catalog_publication() {+ let (engine, _directory) = engine().await;+ let (registry, complete, started) = registry("refresh", Some(vec![model("cached")]));+ engine.start_registry(registry).await.unwrap();+ started.notified().await;+ let refresh = tokio::spawn({+ let engine = engine.clone();+ async move { engine.refresh_models().await }+ });+ tokio::task::yield_now().await;+ assert!(!refresh.is_finished());+ complete.send(Ok(vec![model("updated")])).unwrap();+ let models = tokio::time::timeout(Duration::from_secs(1), refresh).await.unwrap().unwrap().unwrap();+ assert_eq!(models, vec![model("updated")]);+ assert!(!engine.catalog_is_refreshing());+}++#[tokio::test]+async fn chat_model_info_reads_capabilities_and_context_without_weights() {+ use shoji::types::{+ basic::Value,+ model::{ModelSource, ModelSpecialization},+ };++ let (engine, directory) = engine().await;+ let norm = serde_json::json!({+ "epsilon": 0.00001, "scale_offset": null, "upcast_mode": "only_normalization",+ "subtract_mean": false, "has_scale": true, "has_biases": false,+ });+ let layer = |length| {+ serde_json::json!({+ "pre_mixer_norm_config": null, "post_mixer_norm_config": null,+ "mixer_conv_config": null, "mlp_conv_config": null, "post_mlp_norm_config": null,+ "hidden_dim": null, "ple_config": null, "has_post_layer_scalar": false,+ "kv_source_layer_index": null, "pre_mlp_norm_config": norm,+ "rope_config": { "type": "UnscaledRoPEConfig", "base": 10000.0, "max_sequence_length": length, "head_dim": 4 },+ "mixer_config": {+ "type": "AttentionConfig", "qkvg_projection_config": {}, "out_projection_config": {},+ "query_norm_config": null, "key_norm_config": null, "num_heads": 1, "num_groups": 1,+ "head_dim": 4, "is_causal": true, "scale": null, "sliding_window_size": null,+ "logit_soft_cap": null, "has_sinks": false, "has_qkvg_biases": false,+ "has_out_biases": false, "has_gate": false, "normalize_values": false, "is_kv_sharing": false,+ },+ "mlp_config": {+ "type": "DenseMLPConfig", "linear_config": {}, "activation": { "type": "SiLU", "alpha": 1.0 },+ "has_up_biases": false, "has_down_biases": false, "gate_clipping": null, "up_clipping": null,+ },+ })+ };+ let config = serde_json::json!({+ "type": "LanguageModelConfig", "token_codec_config": { "type": "RawTextCodecConfig" },+ "decoder_config": {+ "embedding_config": { "type": "TiedEmbeddingConfig", "input_scale": null, "logit_soft_cap": null, "logit_scale": null },+ "transformer_config": { "layer_configs": [layer(32768), layer(16384)], "model_dim": 4, "hidden_dim": 4, "output_norm_config": norm },+ "vocab_size": 4, "ple_model_config": null, "embedding_norm_config": null,+ },+ "generation_config": {+ "stop_token_ids": [], "temperature": null, "top_k": null, "top_p": null, "min_p": null,+ "banned_tokens": null, "repetition_penalty": null, "presence_penalty": null,+ "frequency_penalty": null, "suffix_repetition_length": null,+ },+ });+ std::fs::write(directory.path().join("config.json"), config.to_string()).unwrap();+ let model = Model::external(+ "metadata-test".into(),+ "local".into(),+ "Local".into(),+ "uzu".into(),+ "Uzu".into(),+ "1".into(),+ vec![ModelSpecialization::Chat {}],+ ModelAccessibility::OnDevice {+ source: ModelSource::Filesystem {+ path: directory.path().to_string_lossy().into_owned(),+ },+ },+ Some(Value::from(serde_json::json!({ "type": "hanashi", "name": "qwen3.5" }))),+ );+ let info = engine.chat_model_info(&model).await.unwrap().unwrap();+ assert_eq!(info.context_length, Some(16384));+ assert!(info.capabilities.supports_tools);+ assert!(info.capabilities.supports_reasoning);+ assert!(info.capabilities.supports_disable_reasoning);+ let mut unlimited = config;+ unlimited["decoder_config"]["transformer_config"]["layer_configs"] = serde_json::json!([]);+ std::fs::write(directory.path().join("config.json"), unlimited.to_string()).unwrap();+ assert_eq!(engine.chat_model_info(&model).await.unwrap().unwrap().context_length, None);++ let downloadable = Model {+ identifier: "not-downloaded".into(),+ accessibility: ModelAccessibility::OnDevice {+ source: ModelSource::Registry {+ toolchain_version: "1".into(),+ repository: None,+ source_repository: None,+ files: vec![],+ },+ },+ ..model.clone()+ };+ let pending = engine.chat_model_info(&downloadable).await.unwrap().unwrap();+ assert_eq!(pending.context_length, None);+ assert_eq!(pending.capabilities, info.capabilities);+ assert_eq!(pending.default_reasoning_effort, info.default_reasoning_effort);+ let remote = Model {+ accessibility: ModelAccessibility::Remote {+ repository: None,+ },+ ..model+ };+ assert!(engine.chat_model_info(&remote).await.unwrap().is_none());+}+#[tokio::test]async fn a_stalled_registry_does_not_delay_a_healthy_registry() {let (engine, _directory) = engine().await;diff --git a/crates/legacy/uzu/src/engine/mod.rs b/crates/legacy/uzu/src/engine/mod.rsindex fc0bea215..0e8638f32 100644--- a/crates/legacy/uzu/src/engine/mod.rs+++ b/crates/legacy/uzu/src/engine/mod.rs@@ -6,6 +6,7 @@ pub mod config;mod downloader;mod downloader_stream;mod error;+mod model_info;mod sampling;mod shorthand;@@ -27,6 +28,7 @@ pub use error::EngineError;use futures_util::stream::FuturesUnordered;use indexmap::IndexSet;use kiban::rt::RuntimeHandle;+pub use model_info::ChatModelInfo;use nagare::{chat::{ChatInstance, ChatSession},classification::ClassificationSession,@@ -340,6 +342,20 @@ impl Engine {Ok(self.catalog_snapshot().await?.0)}+ /// Refresh registries and wait until the catalog has been published to storage.+ /// Registries that fail to refresh retain their last known entries.+ #[bindings::export(Method)]+ pub async fn refresh_models(&self) -> Result<Vec<Model>, EngineError> {+ let mut updates = self.catalog_subscribe();+ self.refresh_catalog().await?;+ while self.catalog_is_refreshing() {+ if updates.next().await.is_none() {+ break;+ }+ }+ self.models().await+ }+#[bindings::export(Method(Getter))]pub async fn models_on_device(&self) -> Result<Vec<Model>, EngineError> {Ok(self.models().await?.into_iter().filter(|model| model.is_on_device()).collect())diff --git a/crates/legacy/uzu/src/engine/model_info.rs b/crates/legacy/uzu/src/engine/model_info.rsnew file mode 100644index 000000000..b811b8b0f--- /dev/null+++ b/crates/legacy/uzu/src/engine/model_info.rs@@ -0,0 +1,63 @@+use std::path::Path;++use hanashi::chat::EncodingConfig;+use shoji::types::{basic::ReasoningEffort, model::Model, session::chat::ChatModelCapabilities};++use super::{Engine, EngineError};++/// Chat metadata available without loading model weights or allocating a session.+#[bindings::export(Structure(Class))]+#[derive(Debug, Clone, PartialEq)]+pub struct ChatModelInfo {+ /// Architectural limit; absent before download or for models without a finite limit.+ pub context_length: Option<u32>,+ pub capabilities: ChatModelCapabilities,+ pub default_reasoning_effort: Option<ReasoningEffort>,+}++#[bindings::export(Implementation)]+impl Engine {+ /// Returns metadata for a Uzu chat model, including models not yet downloaded.+ /// Encoding capabilities come from the catalog; the context limit needs local configuration.+ /// Remote and non-chat models return `None`.+ #[bindings::export(Method)]+ pub async fn chat_model_info(+ &self,+ model: &Model,+ ) -> Result<Option<ChatModelInfo>, EngineError> {+ if !model.is_on_device()+ || !model.is_chat_capable()+ || !model.backends.iter().any(|backend| backend.identifier == "uzu")+ {+ return Ok(None);+ }+ let path = self.model_path(model).await;+ let encoding = model.encoding.clone();+ tokio::task::spawn_blocking(move || {+ let encoding = encoding.ok_or_else(|| EngineError::ModelConfig {+ message: "Chat model has no supported encoding".into(),+ })?;+ let config: EncodingConfig = serde_json::from_str(&encoding.json).map_err(config_error)?;+ Ok(Some(ChatModelInfo {+ context_length: match path {+ Some(path) => {+ uzu_engine::engine::resolve_model_context_length(Path::new(&path)).map_err(config_error)?+ },+ None => None,+ },+ capabilities: config.capabilities().map_err(config_error)?,+ default_reasoning_effort: config.default_reasoning_effort().map_err(config_error)?,+ }))+ })+ .await+ .map_err(|error| EngineError::TokioError {+ message: error.to_string(),+ })?+ }+}++fn config_error(error: impl std::fmt::Display) -> EngineError {+ EngineError::ModelConfig {+ message: error.to_string(),+ }+}diff --git a/crates/uzu-engine/src/engine/mod.rs b/crates/uzu-engine/src/engine/mod.rsindex c62f5bca7..338d6ade9 100644--- a/crates/uzu-engine/src/engine/mod.rs+++ b/crates/uzu-engine/src/engine/mod.rs@@ -8,7 +8,7 @@ use crate::{Backend, Context,capture_matcher::{CAPTURE_MATCHER, CaptureError, CaptureMatcherSpan},},- config::model::AnyModelConfig,+ config::{model::AnyModelConfig, token_mixer::AnyTokenMixerConfig},encodable_block::sampling::SamplingMethod,};@@ -72,6 +72,19 @@ pub fn resolve_model_sampling_method(model_path: &Path) -> Result<Option<Samplin})}+pub fn resolve_model_context_length(model_path: &Path) -> Result<Option<u32>, ResolveModelTypeError> {+ let transformer = match read_model_config(model_path)? {+ AnyModelConfig::LanguageModelConfig(config) => config.decoder_config.transformer_config,+ AnyModelConfig::ClassifierModelConfig(_) => return Ok(None),+ };+ Ok(transformer+ .layer_configs+ .iter()+ .filter(|layer| matches!(layer.mixer_config, AnyTokenMixerConfig::AttentionConfig(_)))+ .filter_map(|layer| layer.rope_config.as_ref().map(|rope| *rope.max_sequence_length()))+ .min())+}+fn read_model_config(model_path: &Path) -> Result<AnyModelConfig, ResolveModelTypeError> {let file = File::open(model_path.join("config.json"))?;Ok(serde_json::from_reader(BufReader::new(file))?)