A high-performance inference engine for AI models
Mac app fixes and improvements (#918)
uuuvn03825dc22d6d
diff --git a/.github/workflows/release-mirai-gui.yml b/.github/workflows/release-mirai-gui.ymlindex 3af03c788..582406de1 100644--- a/.github/workflows/release-mirai-gui.yml+++ b/.github/workflows/release-mirai-gui.yml@@ -1,16 +1,17 @@-name: "Release mirai-gui"+name: "Build / release mirai-gui"# The app ships with every engine release under the engine's version, stamped# by `cargo tools sync`; the engine's GitHub Release is also the update feed.on:+ workflow_dispatch:release:types: [published]permissions:- contents: write+ contents: readconcurrency:- group: release-mirai-gui+ group: mirai-gui-${{ github.event_name }}cancel-in-progress: falsedefaults:@@ -20,13 +21,16 @@ defaults:jobs:decide:name: decide- if: ${{ !github.event.release.prerelease }}+ if: ${{ github.event_name == 'workflow_dispatch' || !github.event.release.prerelease }}runs-on: ubuntu-latestoutputs:- publish: ${{ steps.decide.outputs.publish }}+ build: ${{ steps.decide.outputs.build }}+ version: ${{ steps.decide.outputs.version }}steps:- uses: actions/checkout@v6+ with:+ ref: ${{ github.sha }}- id: decideenv:@@ -34,12 +38,17 @@ jobs:GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}run: |set -euo pipefail+ STAMPED="$(node -p "require('./apps/mirai-gui/package.json').version")"+ echo "version=${STAMPED}" >> "$GITHUB_OUTPUT"+ if [ "${GITHUB_EVENT_NAME}" = workflow_dispatch ]; then+ echo "build=true" >> "$GITHUB_OUTPUT"+ exit 0+ fiif [[ ! "${TAG}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; thenecho "::notice::${TAG} is not an engine release tag; nothing to publish"- echo "publish=false" >> "$GITHUB_OUTPUT"+ echo "build=false" >> "$GITHUB_OUTPUT"exit 0fi- STAMPED="$(node -p "require('./apps/mirai-gui/package.json').version")"if [ "${STAMPED}" != "${TAG}" ]; thenecho "apps/mirai-gui/package.json is at ${STAMPED}, tag is ${TAG}: cargo tools sync did not run before tagging" >&2exit 1@@ -48,16 +57,18 @@ jobs:ASSETS="$(gh release view "${TAG}" --json assets --jq '.assets[].name')"if grep -qxF latest.json <<< "${ASSETS}"; thenecho "::notice::${TAG} already carries the app; nothing to do"- echo "publish=false" >> "$GITHUB_OUTPUT"+ echo "build=false" >> "$GITHUB_OUTPUT"exit 0fi- echo "publish=true" >> "$GITHUB_OUTPUT"+ echo "build=true" >> "$GITHUB_OUTPUT"- release:- name: release-mirai-gui+ build:+ name: Build signed macOS appneeds: decide- if: ${{ needs.decide.outputs.publish == 'true' }}+ if: ${{ needs.decide.outputs.build == 'true' }}runs-on: macos-26+ outputs:+ artifact-id: ${{ steps.artifacts.outputs.artifact-id }}defaults:run:@@ -65,6 +76,7 @@ jobs:env:TAG: ${{ github.event.release.tag_name }}+ APP_VERSION: ${{ needs.decide.outputs.version }}GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}APPLE_ID: ${{ secrets.APPLE_ID }}APPLE_PASSWORD: ${{ secrets.APPLE_APP_SPECIFIC_PASSWORD }}@@ -77,6 +89,9 @@ jobs:steps:- uses: actions/checkout@v6+ with:+ ref: ${{ github.sha }}+ persist-credentials: false- uses: ./.github/actions/setupwith:metal: true@@ -96,6 +111,7 @@ jobs:# Already signed and notarized by the engine's release job; Tauri leaves# files under Resources unsigned.- name: Take the mirai CLI from the engine release+ if: ${{ github.event_name == 'release' }}run: |set -euo pipefailgh release download "${TAG}" --dir "${RUNNER_TEMP}" --pattern mirai-aarch64-apple-darwin.tar.gz@@ -106,6 +122,31 @@ jobs:-R="anchor apple generic and certificate leaf[subject.OU] = \"${APPLE_TEAM_ID}\"" \desktop/resources/cli/mirai+ - name: Build the mirai CLI from the selected commit+ if: ${{ github.event_name == 'workflow_dispatch' }}+ working-directory: .+ run: |+ set -euo pipefail+ uv run --no-project --python 3.12 python - <<'PY'+ import os+ import subprocess+ import tomllib+ from pathlib import Path++ config = tomllib.loads(Path('.config/platforms.toml').read_text())+ target = 'aarch64-apple-darwin'+ platform = config['targets'][target]+ features = [f"backend-{platform['backend']}"]+ features += [f'capability-{cap}' for cap in platform['capabilities_default']]+ env = os.environ | {key: config['envs'][key] for key in platform['required_envs']}+ subprocess.run([+ 'cargo', 'build', '--locked', '--release', '-p', config['binaries']['mirai']['crate'],+ '--target', target, '--no-default-features', '--features', ','.join(features),+ ], env=env, check=True)+ PY+ install -d apps/mirai-gui/desktop/resources/cli+ install -m 755 target/aarch64-apple-darwin/release/cli apps/mirai-gui/desktop/resources/cli/mirai+- name: Import signing certificate and read its identityrun: |set -euo pipefail@@ -121,16 +162,32 @@ jobs:-P "${APPLE_DEVELOPER_ID_PASSWORD}" \-T /usr/bin/codesign -T /usr/bin/securitysecurity set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "${KEYCHAIN_PW}" "${KEYCHAIN}" >/dev/null- security list-keychains -d user -s "${KEYCHAIN}" $(security list-keychains -d user | tr -d '"')+ KEYCHAINS=()+ while IFS='"' read -r _ EXISTING_KEYCHAIN _; do+ KEYCHAINS+=("${EXISTING_KEYCHAIN}")+ done < <(security list-keychains -d user)+ security list-keychains -d user -s "${KEYCHAIN}" "${KEYCHAINS[@]}"IDENTITY="$(security find-identity -v -p codesigning "${KEYCHAIN}" | grep -m1 'Developer ID Application' | awk -F'"' '{print $2}')"if [ -z "${IDENTITY}" ]; thenecho "No Developer ID Application identity found in the imported certificate" >&2exit 1firm -f "${RUNNER_TEMP}/app_cert.p12"- echo "APPLE_SIGNING_IDENTITY=${IDENTITY}" >> $GITHUB_ENV+ echo "APPLE_SIGNING_IDENTITY=${IDENTITY}" >> "$GITHUB_ENV"echo "::add-mask::${IDENTITY}"+ - name: Sign and notarize the locally built CLI+ if: ${{ github.event_name == 'workflow_dispatch' }}+ run: |+ set -euo pipefail+ codesign --force --options runtime --timestamp --sign "${APPLE_SIGNING_IDENTITY}" desktop/resources/cli/mirai+ codesign --verify --strict --verbose=2 \+ -R="anchor apple generic and certificate leaf[subject.OU] = \"${APPLE_TEAM_ID}\"" \+ desktop/resources/cli/mirai+ ditto -c -k --sequesterRsrc --keepParent desktop/resources/cli/mirai "${RUNNER_TEMP}/mirai-cli.zip"+ xcrun notarytool submit "${RUNNER_TEMP}/mirai-cli.zip" \+ --apple-id "${APPLE_ID}" --team-id "${APPLE_TEAM_ID}" --password "${APPLE_PASSWORD}" --wait+- name: Build and sign Tauri apprun: |set -euo pipefail@@ -151,7 +208,7 @@ jobs:test -e "${p}" || { echo "Missing bundle artifact: ${p}" >&2; ls -la "${BUNDLE_DIR}/macos" >&2; exit 1; }done# public.api matches ^mirai-<version>-aarch64-apple-darwin.dmg$ exactly.- DMG_NAME="mirai-${TAG}-aarch64-apple-darwin.dmg"+ DMG_NAME="mirai-${APP_VERSION}-aarch64-apple-darwin.dmg"DMG_TARGET="${BUNDLE_DIR}/dmg/${DMG_NAME}"mkdir -p "${BUNDLE_DIR}/dmg"DMG_APP_PATH="${APP_PATH}" \@@ -173,8 +230,8 @@ jobs:base64 -d < "${SIG_PATH}" > "${RUNNER_TEMP}/updater.minisig"minisign -Vm "${TARGZ_PATH}" -p "${RUNNER_TEMP}/updater.pub" -x "${RUNNER_TEMP}/updater.minisig"BUILT="$(defaults read "${APP_PATH}/Contents/Info" CFBundleShortVersionString)"- if [ "${BUILT}" != "${TAG}" ]; then- echo "Built app is ${BUILT}, the manifest would say ${TAG}: every client would update forever" >&2+ if [ "${BUILT}" != "${APP_VERSION}" ]; then+ echo "Built app is ${BUILT}, expected ${APP_VERSION}" >&2exit 1fi# Installed /usr/local/bin wrappers point at this path.@@ -194,12 +251,54 @@ jobs:--waitxcrun stapler staple -v "${DMG_TARGET}"+ - name: Stage downloadable app artifacts+ run: |+ set -euo pipefail+ mkdir -p "${RUNNER_TEMP}/mirai-gui-artifacts"+ cp "${DMG_TARGET}" "${RUNNER_TEMP}/mirai-gui-artifacts/"+ cp "${TARGZ_PATH}" "${RUNNER_TEMP}/mirai-gui-artifacts/mirai-${APP_VERSION}.app.tar.gz"+ cp "${SIG_PATH}" "${RUNNER_TEMP}/mirai-gui-artifacts/mirai-${APP_VERSION}.app.tar.gz.sig"++ - uses: actions/upload-artifact@v4+ id: artifacts+ with:+ name: mirai-macos-${{ github.sha }}-${{ github.run_attempt }}+ path: ${{ runner.temp }}/mirai-gui-artifacts/+ if-no-files-found: error+ compression-level: 0+ retention-days: 14++ - name: Remove signing credentials+ if: ${{ always() }}+ run: |+ rm -f "${RUNNER_TEMP}/app_cert.p12"+ if [ -f "${RUNNER_TEMP}/mirai-signing.keychain-db" ]; then+ security delete-keychain "${RUNNER_TEMP}/mirai-signing.keychain-db"+ fi++ publish:+ name: Publish release assets+ needs: [decide, build]+ if: ${{ github.event_name == 'release' }}+ runs-on: ubuntu-latest+ permissions:+ contents: write+ env:+ TAG: ${{ github.event.release.tag_name }}+ GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}+ GH_REPO: ${{ github.repository }}+ DMG_TARGET: mirai-${{ needs.decide.outputs.version }}-aarch64-apple-darwin.dmg+ TARGZ_NAME: mirai-${{ needs.decide.outputs.version }}.app.tar.gz++ steps:+ - uses: actions/download-artifact@v4+ with:+ artifact-ids: ${{ needs.build.outputs.artifact-id }}+ merge-multiple: true+- name: Build the updater manifestrun: |set -euo pipefail- TARGZ_NAME="mirai-${TAG}.app.tar.gz"- cp "${TARGZ_PATH}" "${TARGZ_NAME}"- cp "${SIG_PATH}" "${TARGZ_NAME}.sig"jq -n \--arg version "${TAG}" \--arg pub_date "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \@@ -207,7 +306,6 @@ jobs:--arg url "https://github.com/${GITHUB_REPOSITORY}/releases/download/${TAG}/${TARGZ_NAME}" \'{version: $version, pub_date: $pub_date, platforms: {"darwin-aarch64": {signature: $signature, url: $url}}}' \> latest.json- echo "TARGZ_NAME=${TARGZ_NAME}" >> "$GITHUB_ENV"# Clients read latest.json as soon as it is attached.- name: Attach the release assetsdiff --git a/.github/workflows/tests.yml b/.github/workflows/tests.ymlindex a634f1e8c..1c874249c 100644--- a/.github/workflows/tests.yml+++ b/.github/workflows/tests.yml@@ -151,7 +151,7 @@ jobs:cargo tools test typescriptlinters-mirai-gui:- name: linters-mirai-gui+ name: linters (mirai-gui)runs-on: [self-hosted, macos, arm64]defaults:@@ -205,7 +205,7 @@ jobs:run: cargo deny check -D warningstests-mirai-gui:- name: tests-mirai-gui+ name: tests (mirai-gui)runs-on: [self-hosted, macos, arm64]defaults:@@ -232,7 +232,7 @@ jobs:run: pnpm test:rustbuild-mirai-gui:- name: build-mirai-gui+ name: build (mirai-gui)runs-on: [self-hosted, macos, arm64]defaults:diff --git a/Cargo.lock b/Cargo.lockindex eee205fe4..f97405426 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1767,12 +1767,10 @@ name = "download-manager"version = "0.6.2"dependencies = ["async-trait",- "base64 0.22.1","bindings","bindings-types","block2","bon",- "crc32c","futures-util","inventory","kiban",@@ -1791,8 +1789,6 @@ dependencies = ["serde","serde-wasm-bindgen","serde_json",- "sha1",- "sha2","tempfile","thiserror 2.0.18","tokio",@@ -3729,7 +3725,6 @@ dependencies = ["bindings","bindings-types","bon",- "chrono","futures","hanashi","indexmap 2.14.0",diff --git a/Cargo.toml b/Cargo.tomlindex 759972957..eaa41ea96 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -143,8 +143,6 @@ reqwest = { version = "0.13.2", features = ["query",] }crc32c = "0.6.8"-sha1 = "0.10.6"-sha2 = "0.10.9"base64 = "0.22.1"bitflags = "2.11.1"bon = "3.10.0"diff --git a/README.md b/README.mdindex 1efe4a7f9..50e108411 100644--- a/README.md+++ b/README.md@@ -281,6 +281,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {let mut last_message: Option<ChatMessage> = None;while let Some(chunk) = stream.next().await {match chunk {+ ChatSessionStreamChunk::ToolResults {+ ..+ } => {},ChatSessionStreamChunk::Replies {replies,} => {@@ -396,6 +399,8 @@ public func runChat() async throws {print("Generated tokens: \(reply?.stats.tokensCountOutput ?? 0)")case .error(let error):print("Error: \(error)")+ case .toolResults:+ break}}print("Reasoning: \(message?.reasoning() ?? "empty")")diff --git a/apps/mirai-gui/README.md b/apps/mirai-gui/README.mdindex 89bb2cc59..059e2be70 100644--- a/apps/mirai-gui/README.md+++ b/apps/mirai-gui/README.md@@ -23,6 +23,22 @@ pnpm web:dev # frontend in a browser without the engine: no models, no chats`pnpm check` runs typecheck, ESLint, client tests, clippy and Rust tests;`pnpm format` runs Prettier.+## Signed builds without a release++In GitHub Actions, open **Build / release mirai-gui**, choose **Run workflow**,+and select the branch to build. GitHub requires repository write access to run+it manually; the workflow must be on the default branch for the button to appear.++Download the `mirai-macos-…` artifact from the completed run. It contains the+Apple-signed app in a notarized DMG, plus the app archive and updater signature, and+is retained for 14 days. The app and bundled CLI are built from the selected+commit using its existing version. Manual runs do not create a release or tag,+change versions, publish an updater manifest, or upload to the website bucket.++These builds use the normal Mirai app identity, local data, and release update+feed; installing one replaces the regular app. Published engine releases still+use this workflow to build and publish the app automatically.+## Layout```textdiff --git a/apps/mirai-gui/client/index.html b/apps/mirai-gui/client/index.htmlindex f7f0200d0..4a13eb47f 100644--- a/apps/mirai-gui/client/index.html+++ b/apps/mirai-gui/client/index.html@@ -7,12 +7,20 @@<link rel="icon" type="image/png" href="/icons/icon_mirai_app.png" /><script>{- let isDark = true;+ let theme = "system";try {- isDark = JSON.parse(localStorage.getItem("mirai-store") ?? "{}")?.state?.isDarkMode !== false;- } catch {- isDark = true;- }+ const stored = JSON.parse(+ localStorage.getItem("mirai-store") || localStorage.getItem("mirai-electron-store") || "{}",+ )?.state;+ theme = ["system", "light", "dark"].includes(stored?.theme)+ ? stored.theme+ : stored?.isDarkMode === false+ ? "light"+ : "system";+ } catch {}+ const isDark =+ theme === "dark" ||+ (theme === "system" && (window.matchMedia?.("(prefers-color-scheme: dark)").matches ?? false));document.documentElement.classList.toggle("dark", isDark);}</script>diff --git a/apps/mirai-gui/client/src/components/error-page.tsx b/apps/mirai-gui/client/src/components/error-page.tsxindex 1784f7cd2..8eae43cef 100644--- a/apps/mirai-gui/client/src/components/error-page.tsx+++ b/apps/mirai-gui/client/src/components/error-page.tsx@@ -16,12 +16,12 @@ export function ErrorPage() {};return (- <div className="min-h-screen min-w-[360px] w-full flex flex-col bg-background text-label-title">- <div className="flex px-8 py-8 justify-center">+ <div className="h-full min-h-0 min-w-[360px] w-full flex flex-col bg-background text-label-title">+ <div className="shrink-0 flex px-8 py-8 justify-center">{isDarkMode ? <Logo width={66} height={57} /> : <LogoLightMode width={48} height={24} />}</div>- <div className="flex-1 flex items-center justify-center px-4">- <div className="flex flex-col items-center gap-10">+ <div className="flex-1 min-h-0 overflow-y-auto overscroll-y-contain px-4">+ <div className="flex min-h-full flex-col items-center justify-center gap-10 py-4"><ErrorIcon color="#FF2020" /><div className="flex flex-col items-center justify-center gap-3 text-center"><h1 className="text-2xl font-bold">Page Not Found</h1>diff --git a/apps/mirai-gui/client/src/components/footer-bar/index.tsx b/apps/mirai-gui/client/src/components/footer-bar/index.tsxindex b9f4fb7f3..bd2c83b06 100644--- a/apps/mirai-gui/client/src/components/footer-bar/index.tsx+++ b/apps/mirai-gui/client/src/components/footer-bar/index.tsx@@ -17,7 +17,7 @@ export const FooterBar = React.memo(({ className }: { className?: string }) => {className,)}>- <div className="flex items-center gap-2 overflow-hidden">+ <div className="flex min-w-0 items-center gap-2 overflow-clip">{footerModel.visible && (<>{footerModel.status === "loading" || footerModel.status === "ejecting" ? (diff --git a/apps/mirai-gui/client/src/components/icons/discord-icon.tsx b/apps/mirai-gui/client/src/components/icons/discord-icon.tsxdeleted file mode 100644index d2624004f..000000000--- a/apps/mirai-gui/client/src/components/icons/discord-icon.tsx+++ /dev/null@@ -1,12 +0,0 @@-import React from "react";--const DiscordIcon: React.FC<React.SVGProps<SVGSVGElement>> = (props) => (- <svg xmlns="http://www.w3.org/2000/svg" width="19" height="15" viewBox="0 0 19 15" fill="none" {...props}>- <path- d="M15.3202 1.6725C14.1495 1.12475 12.8977 0.72667 11.589 0.5C11.4283 0.790583 11.2405 1.18142 11.1111 1.49234C9.71987 1.28312 8.34148 1.28312 6.97588 1.49234C6.84644 1.18142 6.6544 0.790583 6.49224 0.5C5.18213 0.72667 3.92891 1.12621 2.7582 1.6754C0.39686 5.24369 -0.243259 8.72334 0.0768009 12.1536C1.64296 13.3232 3.16076 14.0336 4.65295 14.4986C5.02138 13.9915 5.34997 13.4525 5.63304 12.8844C5.09392 12.6795 4.57756 12.4267 4.08966 12.1333C4.21909 12.0374 4.34571 11.9371 4.46803 11.834C7.44387 13.2258 10.6772 13.2258 13.6175 11.834C13.7412 11.9371 13.8678 12.0374 13.9959 12.1333C13.5065 12.4282 12.9887 12.681 12.4496 12.8859C12.7327 13.4525 13.0599 13.993 13.4297 14.5C14.9233 14.0351 16.4425 13.3246 18.0087 12.1536C18.3843 8.17706 17.3672 4.72936 15.3202 1.6725ZM6.03846 10.044C5.14514 10.044 4.41254 9.21005 4.41254 8.19449C4.41254 7.17892 5.12949 6.34352 6.03846 6.34352C6.94744 6.34352 7.68001 7.17746 7.66437 8.19449C7.66578 9.21005 6.94744 10.044 6.03846 10.044ZM12.0471 10.044C11.1537 10.044 10.4212 9.21005 10.4212 8.19449C10.4212 7.17892 11.1381 6.34352 12.0471 6.34352C12.956 6.34352 13.6886 7.17746 13.673 8.19449C13.673 9.21005 12.956 10.044 12.0471 10.044Z"- fill="currentColor"- />- </svg>-);--export default DiscordIcon;diff --git a/apps/mirai-gui/client/src/components/icons/github-icon.tsx b/apps/mirai-gui/client/src/components/icons/github-icon.tsxdeleted file mode 100644index e021f3fe3..000000000--- a/apps/mirai-gui/client/src/components/icons/github-icon.tsx+++ /dev/null@@ -1,14 +0,0 @@-import React from "react";--const GithubIcon: React.FC<React.SVGProps<SVGSVGElement>> = (props) => (- <svg xmlns="http://www.w3.org/2000/svg" width="17" height="17" viewBox="0 0 17 17" fill="none" {...props}>- <path- fillRule="evenodd"- clipRule="evenodd"- d="M8.33987 0.5C3.91242 0.5 0.333252 4.16665 0.333252 8.7028C0.333252 12.3288 2.62655 15.3981 5.80795 16.4844C6.20571 16.5661 6.35141 16.3079 6.35141 16.0908C6.35141 15.9006 6.3383 15.2488 6.3383 14.5696C4.11105 15.0586 3.64725 13.5918 3.64725 13.5918C3.28931 12.6411 2.75897 12.3968 2.75897 12.3968C2.02999 11.8943 2.81207 11.8943 2.81207 11.8943C3.6207 11.9486 4.045 12.7363 4.045 12.7363C4.76071 13.9856 5.91399 13.6326 6.37796 13.4153C6.44417 12.8856 6.65641 12.5189 6.88175 12.3153C5.10536 12.1251 3.23638 11.419 3.23638 8.26813C3.23638 7.3718 3.55432 6.63847 4.05812 6.06814C3.97863 5.86448 3.70018 5.02231 4.13776 3.89515C4.13776 3.89515 4.81381 3.67782 6.33813 4.73715C6.99075 4.55759 7.66379 4.46625 8.33987 4.46548C9.01591 4.46548 9.70506 4.56065 10.3414 4.73715C11.8659 3.67782 12.542 3.89515 12.542 3.89515C12.9796 5.02231 12.7009 5.86448 12.6215 6.06814C13.1385 6.63847 13.4434 7.3718 13.4434 8.26813C13.4434 11.419 11.5744 12.1115 9.78471 12.3153C10.0764 12.5733 10.3282 13.0621 10.3282 13.8363C10.3282 14.9363 10.3151 15.8191 10.3151 16.0906C10.3151 16.3079 10.4609 16.5661 10.8585 16.4846C14.0399 15.3979 16.3332 12.3288 16.3332 8.7028C16.3463 4.16665 12.7541 0.5 8.33987 0.5Z"- fill="currentColor"- />- </svg>-);--export default GithubIcon;diff --git a/apps/mirai-gui/client/src/components/icons/model-menu-icon.tsx b/apps/mirai-gui/client/src/components/icons/model-menu-icon.tsxdeleted file mode 100644index 5609af286..000000000--- a/apps/mirai-gui/client/src/components/icons/model-menu-icon.tsx+++ /dev/null@@ -1,13 +0,0 @@-import React from "react";--export const ModelMenuIcon: React.FC = () => (- <svg xmlns="http://www.w3.org/2000/svg" width="16" height="17" viewBox="0 0 16 17" fill="none">- <path- d="M10.9092 9.96376C12.3137 9.54601 13.2364 8.83503 13.2364 8.02747C13.2364 6.74165 10.8917 5.7002 8.00008 5.7002C5.10845 5.7002 2.76372 6.74165 2.76372 8.02747C2.76372 9.31329 5.10845 10.3547 8.00008 10.3547M8.00008 10.3547L6.25463 8.60929M8.00008 10.3547L6.25463 12.1002"- stroke="#A8A8B3"- strokeWidth="1.2"- strokeLinecap="round"- strokeLinejoin="round"- />- </svg>-);diff --git a/apps/mirai-gui/client/src/components/icons/performance-icon.tsx b/apps/mirai-gui/client/src/components/icons/performance-icon.tsxdeleted file mode 100644index 162cf90ae..000000000--- a/apps/mirai-gui/client/src/components/icons/performance-icon.tsx+++ /dev/null@@ -1,13 +0,0 @@-import React from "react";--export const PerformanceIcon: React.FC = () => (- <svg xmlns="http://www.w3.org/2000/svg" width="13" height="8" viewBox="0 0 13 8" fill="none">- <path- d="M1.97777 5.9998L5.44444 1.95536L8.33333 4.84425L11.2222 1.37758M7.75555 4.84425C7.75555 4.99749 7.81642 5.14445 7.92478 5.2528C8.03313 5.36115 8.18009 5.42203 8.33333 5.42203C8.48656 5.42203 8.63352 5.36115 8.74188 5.2528C8.85023 5.14445 8.91111 4.99749 8.91111 4.84425C8.91111 4.69101 8.85023 4.54405 8.74188 4.4357C8.63352 4.32734 8.48656 4.26647 8.33333 4.26647C8.18009 4.26647 8.03313 4.32734 7.92478 4.4357C7.81642 4.54405 7.75555 4.69101 7.75555 4.84425ZM4.86666 1.95536C4.86666 2.1086 4.92753 2.25556 5.03589 2.36391C5.14424 2.47227 5.2912 2.53314 5.44444 2.53314C5.59767 2.53314 5.74463 2.47227 5.85299 2.36391C5.96134 2.25556 6.02222 2.1086 6.02222 1.95536C6.02222 1.80212 5.96134 1.65516 5.85299 1.54681C5.74463 1.43846 5.59767 1.37758 5.44444 1.37758C5.2912 1.37758 5.14424 1.43846 5.03589 1.54681C4.92753 1.65516 4.86666 1.80212 4.86666 1.95536ZM1.39999 5.9998C1.39999 6.15304 1.46087 6.3 1.56922 6.40836C1.67758 6.51671 1.82454 6.57758 1.97777 6.57758C2.13101 6.57758 2.27797 6.51671 2.38632 6.40836C2.49468 6.3 2.55555 6.15304 2.55555 5.9998C2.55555 5.84657 2.49468 5.69961 2.38632 5.59125C2.27797 5.4829 2.13101 5.42203 1.97777 5.42203C1.82454 5.42203 1.67758 5.4829 1.56922 5.59125C1.46087 5.69961 1.39999 5.84657 1.39999 5.9998ZM10.6444 1.37758C10.6444 1.53082 10.7053 1.67778 10.8137 1.78613C10.922 1.89449 11.069 1.95536 11.2222 1.95536C11.3755 1.95536 11.5224 1.89449 11.6308 1.78613C11.7391 1.67778 11.8 1.53082 11.8 1.37758C11.8 1.22435 11.7391 1.07739 11.6308 0.969032C11.5224 0.860678 11.3755 0.799805 11.2222 0.799805C11.069 0.799805 10.922 0.860678 10.8137 0.969032C10.7053 1.07739 10.6444 1.22435 10.6444 1.37758Z"- stroke="#A8A8B3"- strokeWidth="1.2"- strokeLinecap="round"- strokeLinejoin="round"- />- </svg>-);diff --git a/apps/mirai-gui/client/src/components/icons/x-icon.tsx b/apps/mirai-gui/client/src/components/icons/x-icon.tsxdeleted file mode 100644index e17b71436..000000000--- a/apps/mirai-gui/client/src/components/icons/x-icon.tsx+++ /dev/null@@ -1,14 +0,0 @@-import React from "react";--const XIcon: React.FC<React.SVGProps<SVGSVGElement>> = (props) => (- <svg xmlns="http://www.w3.org/2000/svg" width="17" height="17" viewBox="0 0 17 17" fill="none" {...props}>- <path- fillRule="evenodd"- clipRule="evenodd"- d="M0.762431 0.636564C0.815065 0.711665 2.03403 2.4859 3.47124 4.5793C4.90846 6.67269 6.23774 8.60863 6.42523 8.88141C6.6127 9.15419 6.7661 9.38284 6.7661 9.38953C6.7661 9.39623 6.69628 9.48231 6.61096 9.58081C6.52564 9.67931 6.2853 9.95815 6.07692 10.2004C5.86851 10.4427 5.5172 10.8511 5.29621 11.1079C5.07524 11.3648 4.68648 11.8167 4.4323 12.1123C4.17813 12.4079 3.72106 12.9394 3.41658 13.2933C2.47489 14.388 2.33899 14.5461 1.62215 15.3811C1.23938 15.8269 0.872334 16.2531 0.806501 16.3283C0.740669 16.4035 0.686801 16.4729 0.686801 16.4825C0.686801 16.4938 0.927224 16.5 1.36418 16.5H2.04153L2.78622 15.6322C3.19581 15.1548 3.59984 14.685 3.68407 14.5881C3.86622 14.3786 5.25529 12.7623 5.37205 12.6241C5.41676 12.5711 5.48169 12.4958 5.51635 12.4567C5.55103 12.4175 5.82432 12.1 6.12366 11.7511C6.423 11.4022 6.67614 11.1082 6.68617 11.0977C6.69621 11.0873 6.85468 10.9029 7.03833 10.6881C7.22197 10.4732 7.3779 10.2974 7.38483 10.2974C7.39177 10.2974 8.33514 11.6632 9.48123 13.3326C10.6273 15.002 11.5855 16.3974 11.6105 16.4335L11.656 16.4992L13.9794 16.4996C15.8902 16.4999 16.3013 16.4959 16.294 16.4769C16.2863 16.4566 15.1731 14.8329 12.3172 10.6762C10.2613 7.68386 9.98791 7.28174 9.99575 7.26134C10.0034 7.2415 10.2834 6.91397 12.1244 4.77167C12.4402 4.40419 12.8756 3.89736 13.0918 3.64537C13.3081 3.39339 13.5264 3.13965 13.5771 3.0815C13.6277 3.02335 13.8938 2.7141 14.1684 2.39427C14.443 2.07445 14.9099 1.53101 15.2059 1.18664C15.5019 0.842273 15.755 0.546908 15.7682 0.530256C15.791 0.501762 15.7523 0.5 15.1044 0.5H14.4164L14.1104 0.856828C13.7031 1.33172 12.9669 2.18809 12.7578 2.43008C12.6655 2.537 12.5502 2.67138 12.5018 2.72869C12.4533 2.78599 12.3577 2.89674 12.2893 2.9748C12.2209 3.05286 11.8763 3.45374 11.5234 3.86564C11.1706 4.27753 10.877 4.6185 10.8711 4.62335C10.8652 4.62819 10.7896 4.71589 10.7031 4.81825C10.5518 4.99729 10.3978 5.17649 9.69168 5.99542C9.38173 6.35489 9.36583 6.37033 9.3397 6.33729C9.32467 6.31825 8.41632 4.99715 7.32116 3.40152L5.32997 0.50037L2.99836 0.500176L0.666748 0.5L0.762431 0.636564ZM2.58222 1.58796C2.59974 1.6147 3.03536 2.23921 3.55031 2.97577C4.52692 4.3727 8.36463 9.86539 10.8327 13.3987C11.6214 14.5278 12.2763 15.4615 12.2881 15.4737C12.3049 15.4911 12.5345 15.4949 13.3611 15.4913L14.4127 15.4868L11.6608 11.5485C10.1473 9.38238 7.95588 6.24626 6.79108 4.5793L4.67327 1.54846L3.61184 1.54391L2.55041 1.53937L2.58222 1.58796Z"- fill="currentColor"- />- </svg>-);--export default XIcon;diff --git a/apps/mirai-gui/client/src/components/left-bar/index.tsx b/apps/mirai-gui/client/src/components/left-bar/index.tsxindex cf3c8d125..5577a3828 100644--- a/apps/mirai-gui/client/src/components/left-bar/index.tsx+++ b/apps/mirai-gui/client/src/components/left-bar/index.tsx@@ -2,10 +2,8 @@ import { useIsMobile } from "@/hooks/use-media-query";import { platformInfo } from "@/platform/platform-info";import { useChatStore } from "@/stores/use-chat-store";import { useSidebarStore } from "@/stores/use-sidebar-store";-import { Transition } from "@headlessui/react";import { useLocation, useNavigate } from "@tanstack/react-router";import { Plus } from "lucide-react";-import { useCallback, useEffect } from "react";import { twMerge } from "tailwind-merge";import { v4 as uuidv4 } from "uuid";import { ChatsIcon } from "../icons/chats-icon";@@ -23,25 +21,25 @@ function LeftBar() {const isSidebarOpen = useSidebarStore((s) => s.isOpen);const toggleSidebar = useSidebarStore((s) => s.toggle);- const setMobile = useSidebarStore((s) => s.setMobile);const closeOnMobile = useSidebarStore((s) => s.closeOnMobile);- useEffect(() => {- setMobile(isMobile);- }, [isMobile, setMobile]);-- const setSidebarWidth = useCallback(- (open: boolean) => {- document.documentElement.style.setProperty("--sidebar-width", open && !isMobile ? "200px" : "0px");- },- [isMobile],- );-- useEffect(() => {- setSidebarWidth(useSidebarStore.getState().isOpen);- }, [setSidebarWidth]);-const mainItems = [+ ...(platformInfo.features.localModelDownloads+ ? [+ {+ icon: ModelsIcon,+ title: "Models",+ url: "/local-models",+ isActive: location.pathname.startsWith("/local-models"),+ },+ ]+ : []),+ {+ icon: ChatsIcon,+ title: "Chats",+ url: "/chats",+ isActive: location.pathname.startsWith("/chats"),+ },{icon: Plus,title: "New Chat",@@ -56,29 +54,17 @@ function LeftBar() {closeOnMobile();},},- {- icon: ChatsIcon,- title: "Chats",- url: "/chats",- isActive: location.pathname.startsWith("/chats"),- },- ...(platformInfo.features.localModelDownloads- ? [- {- icon: ModelsIcon,- title: "Local Models",- url: "/local-models",- isActive: location.pathname.startsWith("/local-models"),- },- ]- : []),];const toggleSidebarButton = (<button+ type="button"+ aria-label={isSidebarOpen ? "Close sidebar" : "Open sidebar"}+ aria-expanded={isSidebarOpen}+ aria-controls="sidebar"onClick={toggleSidebar}className={twMerge(- "overlay-button p-1 rounded-lg hover:bg-bg-hover transition-all duration-300 ease-in-out absolute top-[10px]",+ "overlay-button p-1 rounded-lg hover:bg-bg-hover transition-colors duration-300 ease-in-out motion-reduce:transition-none absolute top-[10px]",isMobile && platformInfo.features.nativeTitleBar? "left-20 translate-x-0": isMobile@@ -93,60 +79,56 @@ function LeftBar() {);return (- <div className="relative z-30">+ <div+ className={twMerge(+ "relative z-30 h-full min-h-0 shrink-0 transition-[width] duration-300 ease-in-out motion-reduce:transition-none",+ !isMobile && isSidebarOpen ? "w-[200px]" : "w-0",+ )}+ >{toggleSidebarButton}{isMobile && isSidebarOpen && (<div className="fixed top-0 right-0 bottom-0 left-[280px] bg-black/50 z-40 md:hidden" onClick={closeOnMobile} />)}- <Transition- show={isSidebarOpen}- enter="transition-all duration-300 ease-out"- enterFrom="opacity-0 -translate-x-full"- enterTo="opacity-100 translate-x-0"- leave="transition-all duration-300 ease-in"- leaveFrom="opacity-100 translate-x-0"- leaveTo="opacity-0 -translate-x-full"- beforeEnter={() => setSidebarWidth(true)}- beforeLeave={() => setSidebarWidth(true)}- afterLeave={() => setSidebarWidth(false)}+ <div+ id="sidebar"+ ref={(element) => {+ if (element) element.inert = !isSidebarOpen;+ }}+ aria-hidden={!isSidebarOpen}+ data-tauri-drag-region+ className={twMerge(+ "relative min-h-0 bg-bg-sidebar border-[1px] border-cell-border border-b-0 border-t-0 flex flex-col py-3 overflow-clip transition-transform duration-300 ease-in-out motion-reduce:transition-none",+ isMobile ? "fixed top-0 left-0 h-dvh w-[280px] z-50" : "h-full w-[200px]",+ isSidebarOpen ? "translate-x-0" : "-translate-x-full",+ )}>- <div- data-tauri-drag-region- className={twMerge(- "relative min-h-screen bg-bg-sidebar border-[1px] border-cell-border border-b-0 border-t-0 h-screen flex flex-col py-3 overflow-hidden",- isMobile ? "fixed top-0 left-0 w-[280px] z-50" : "w-[200px]",- )}- >- <div className="flex-1 flex flex-col overflow-hidden mt-10">- <div className="flex-shrink-0">- <nav className="flex flex-col gap-2">- {mainItems.map((item, index) => (- <MenuItem- key={index}- icon={item.icon}- title={item.title}- url={item.url}- isActive={item.isActive}- onClick={item.onClick}- />- ))}- </nav>+ <div className="flex-1 min-h-0 flex flex-col overflow-clip mt-10">+ <div className="flex-shrink-0">+ <nav className="flex flex-col gap-2">+ {mainItems.map((item) => (+ <MenuItem+ key={item.title}+ icon={item.icon}+ title={item.title}+ url={item.url}+ isActive={item.isActive}+ onClick={item.onClick}+ />+ ))}+ </nav>- <div className="mt-3 mb-2 border-t border-cell-border" />- </div>+ <div className="mt-3 mb-2 border-t border-cell-border" />+ </div>- <div className="flex-1 overflow-y-auto min-h-0 scrollbar-hide">- <SavedChats />- </div>+ <SavedChats />- <div>- <Settings />- </div>+ <div className="shrink-0">+ <Settings /></div></div>- </Transition>+ </div></div>);}diff --git a/apps/mirai-gui/client/src/components/left-bar/menu-item.tsx b/apps/mirai-gui/client/src/components/left-bar/menu-item.tsxindex 7634f7aeb..89ef0aa94 100644--- a/apps/mirai-gui/client/src/components/left-bar/menu-item.tsx+++ b/apps/mirai-gui/client/src/components/left-bar/menu-item.tsx@@ -25,7 +25,7 @@ function MenuItem({ icon: Icon, title, url, onClick, isActive }: MenuItemProps)const content = (<div className="flex px-2"><div- className={`flex items-center gap-2 px-2 py-[6px] w-full rounded-md hover:bg-bg-hover ${isActive ? "bg-bg-hover dark:bg-bg-hover" : ""}`}+ className={`flex items-center gap-2 px-2 py-[6px] w-full rounded-md transition-colors duration-150 ${isActive ? "bg-sidebar-chat-selected" : "hover:bg-sidebar-chat-hover"}`}><Icon className="w-5 h-5 flex-shrink-0 text-label-muted" /><p className="text-[13px] font-[350] leading-[150%] text-label-title truncate min-w-0">{title}</p>diff --git a/apps/mirai-gui/client/src/components/left-bar/settings.tsx b/apps/mirai-gui/client/src/components/left-bar/settings.tsxindex de2390150..cf0f3360d 100644--- a/apps/mirai-gui/client/src/components/left-bar/settings.tsx+++ b/apps/mirai-gui/client/src/components/left-bar/settings.tsx@@ -1,27 +1,25 @@import { Link } from "@tanstack/react-router";-import Socials from "./socials";+import { Settings as SettingsIcon } from "lucide-react";function Settings() {return (<div><div className="border-t border-cell-border mb-2" />- <div className="flex flex-col gap-2">- <Link- to="/settings"- search={{ tab: "general" }}- className="flex px-2"- activeOptions={{ includeSearch: true, exact: true }}- >- <div className="flex items-center gap-2 px-2 py-[6px] w-full rounded-md hover:bg-bg-hover">+ <Link+ to="/settings"+ search={{ tab: "general" }}+ className="flex px-2"+ activeOptions={{ includeSearch: false, exact: true }}+ >+ {({ isActive }) => (+ <div+ className={`flex items-center gap-2 px-2 py-[6px] w-full rounded-md transition-colors duration-150 ${isActive ? "bg-sidebar-chat-selected" : "hover:bg-sidebar-chat-hover"}`}+ >+ <SettingsIcon aria-hidden="true" className="size-5 shrink-0 text-label-muted" /><span className="text-[13px] font-[350] leading-[150%] text-label-title">Settings</span></div>- </Link>- <div className="flex px-2">- <div className="px-2 py-[6px]">- <Socials />- </div>- </div>- </div>+ )}+ </Link></div>);}diff --git a/apps/mirai-gui/client/src/components/left-bar/socials.tsx b/apps/mirai-gui/client/src/components/left-bar/socials.tsxdeleted file mode 100644index 6a8dae7fb..000000000--- a/apps/mirai-gui/client/src/components/left-bar/socials.tsx+++ /dev/null@@ -1,36 +0,0 @@-import DiscordIcon from "../icons/discord-icon";-import GithubIcon from "../icons/github-icon";-import XIcon from "../icons/x-icon";--function Socials() {- return (- <div className="flex items-center justify-start space-x-4">- <a- href="https://github.com/trymirai"- target="_blank"- rel="noopener noreferrer"- className="text-label-muted hover:text-label-title"- >- <GithubIcon className="w-5 h-5" />- </a>- <a- href="https://x.com/trymirai"- target="_blank"- rel="noopener noreferrer"- className="text-label-muted hover:text-label-title"- >- <XIcon className="w-4 h-4" />- </a>- <a- href="https://discord.gg/trymirai"- target="_blank"- rel="noopener noreferrer"- className="text-label-muted hover:text-label-title"- >- <DiscordIcon className="w-5 h-5" />- </a>- </div>- );-}--export default Socials;diff --git a/apps/mirai-gui/client/src/components/page-header.tsx b/apps/mirai-gui/client/src/components/page-header.tsxindex f010c525f..6196e5f15 100644--- a/apps/mirai-gui/client/src/components/page-header.tsx+++ b/apps/mirai-gui/client/src/components/page-header.tsx@@ -9,13 +9,8 @@ export const PageHeader = ({ title }: { title: ReactElement }) => {return (<div- style={- !isSidebarOpen- ? {- paddingLeft: `clamp(0px, calc(${HEADER_LEFT_RESERVE_PX}px - var(--sidebar-width, 0px)), ${HEADER_LEFT_RESERVE_PX}px)`,- }- : undefined- }+ className="transition-[padding-left] duration-300 ease-in-out motion-reduce:transition-none"+ style={{ paddingLeft: isSidebarOpen ? 0 : HEADER_LEFT_RESERVE_PX }}>{title}</div>diff --git a/apps/mirai-gui/client/src/components/ui/card-container.tsx b/apps/mirai-gui/client/src/components/ui/card-container.tsxindex 68779e77a..bf6dfc293 100644--- a/apps/mirai-gui/client/src/components/ui/card-container.tsx+++ b/apps/mirai-gui/client/src/components/ui/card-container.tsx@@ -7,7 +7,7 @@ type CardContainerProps = {export function CardContainer({ children, className, ...props }: CardContainerProps) {return (- <div className={twMerge("bg-bg-modal border border-cell-border rounded-lg overflow-hidden", className)} {...props}>+ <div className={twMerge("bg-bg-modal border border-cell-border rounded-lg overflow-clip", className)} {...props}>{children}</div>);diff --git a/apps/mirai-gui/client/src/components/ui/group-button.tsx b/apps/mirai-gui/client/src/components/ui/group-button.tsxindex 36acdf298..2e718b88b 100644--- a/apps/mirai-gui/client/src/components/ui/group-button.tsx+++ b/apps/mirai-gui/client/src/components/ui/group-button.tsx@@ -19,7 +19,7 @@ const Base: React.FC<GroupButtonProps> = ({ className, children }) => {return (<divclassName={twMerge(- "h-8 flex items-center rounded-lg overflow-hidden border border-cell-border bg-transparent",+ "h-8 flex items-center rounded-lg overflow-clip border border-cell-border bg-transparent",className,)}>diff --git a/apps/mirai-gui/client/src/components/ui/segmented-control.tsx b/apps/mirai-gui/client/src/components/ui/segmented-control.tsxindex 45578f5b2..0dd3a0175 100644--- a/apps/mirai-gui/client/src/components/ui/segmented-control.tsx+++ b/apps/mirai-gui/client/src/components/ui/segmented-control.tsx@@ -17,7 +17,7 @@ export type SegmentedControlProps = {};const CONTAINER_CLASSES =- "inline-flex w-fit h-7 items-center rounded-lg overflow-hidden border border-border-default bg-transparent";+ "inline-flex w-fit h-7 items-center rounded-lg overflow-clip border border-border-default bg-transparent";export function SegmentedControl({ value, onChange, options, ariaLabel }: SegmentedControlProps) {const handleSelect = (option: SegmentedControlOption) => {diff --git a/apps/mirai-gui/client/src/components/ui/select/constants.ts b/apps/mirai-gui/client/src/components/ui/select/constants.tsindex 77dca1632..53fa0f138 100644--- a/apps/mirai-gui/client/src/components/ui/select/constants.ts+++ b/apps/mirai-gui/client/src/components/ui/select/constants.ts@@ -1,3 +1,4 @@export const SELECT_ROW_BASE_CLASSNAME = "text-xs leading-[130%] cursor-pointer select-none rounded-md p-2";-export const SELECT_ROW_ACTIVE_CLASSNAME = "bg-surface-tertiary";+export const SELECT_ROW_ACTIVE_CLASSNAME = "bg-selection-hover hover:bg-selection-hover";+export const SELECT_ROW_SELECTED_CLASSNAME = "bg-selection-selected hover:bg-selection-selected";diff --git a/apps/mirai-gui/client/src/components/ui/select/selection-item/index.tsx b/apps/mirai-gui/client/src/components/ui/select/selection-item/index.tsxindex 473381065..18a2b8719 100644--- a/apps/mirai-gui/client/src/components/ui/select/selection-item/index.tsx+++ b/apps/mirai-gui/client/src/components/ui/select/selection-item/index.tsx@@ -1,6 +1,6 @@import { forwardRef, type ElementType } from "react";import { twMerge } from "tailwind-merge";-import { SELECT_ROW_ACTIVE_CLASSNAME, SELECT_ROW_BASE_CLASSNAME } from "../constants";+import { SELECT_ROW_ACTIVE_CLASSNAME, SELECT_ROW_BASE_CLASSNAME, SELECT_ROW_SELECTED_CLASSNAME } from "../constants";import type { SelectionItemProps } from "./types";function SelectionItemInner(@@ -16,7 +16,7 @@ function SelectionItemInner(className={twMerge(SELECT_ROW_BASE_CLASSNAME,className,- active || selected ? SELECT_ROW_ACTIVE_CLASSNAME : undefined,+ selected ? SELECT_ROW_SELECTED_CLASSNAME : active ? SELECT_ROW_ACTIVE_CLASSNAME : "hover:bg-selection-hover",disabled ? "opacity-50 cursor-not-allowed" : undefined,)}{...resolvedProps}diff --git a/apps/mirai-gui/client/src/components/ui/text-area/index.tsx b/apps/mirai-gui/client/src/components/ui/text-area/index.tsxindex cc8fca1ff..cd10d7acc 100644--- a/apps/mirai-gui/client/src/components/ui/text-area/index.tsx+++ b/apps/mirai-gui/client/src/components/ui/text-area/index.tsx@@ -20,7 +20,7 @@ export const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(function<textarearef={setRef}className={twMerge(- "w-full resize-none outline-hidden thin-scrollbar text-text-primary placeholder:text-text-muted",+ "w-full resize-none outline-hidden overscroll-y-auto thin-scrollbar text-text-primary placeholder:text-text-muted",className,)}value={resolvedValue}diff --git a/apps/mirai-gui/client/src/features/chat-history/components/chat-card.tsx b/apps/mirai-gui/client/src/features/chat-history/components/chat-card.tsxindex 20160fd7d..35a1b94d9 100644--- a/apps/mirai-gui/client/src/features/chat-history/components/chat-card.tsx+++ b/apps/mirai-gui/client/src/features/chat-history/components/chat-card.tsx@@ -68,7 +68,7 @@ const ChatCard: React.FC<ChatCardProps> = ({)}</div><div className="min-w-0">- <h3 className="text-[15px] leading-[150%] font-[350] text-label-title overflow-hidden text-ellipsis whitespace-nowrap">+ <h3 className="text-[15px] leading-[150%] font-[350] text-label-title overflow-clip text-ellipsis whitespace-nowrap">{title}</h3></div>diff --git a/apps/mirai-gui/client/src/features/chat-history/components/chat-history-page.tsx b/apps/mirai-gui/client/src/features/chat-history/components/chat-history-page.tsxindex ee8819086..2f8f34163 100644--- a/apps/mirai-gui/client/src/features/chat-history/components/chat-history-page.tsx+++ b/apps/mirai-gui/client/src/features/chat-history/components/chat-history-page.tsx@@ -7,17 +7,19 @@ import GlobalInstructions from "./global-instructions";import { GroupButton } from "@/components/ui/group-button";import { useNavigate } from "@tanstack/react-router";import { X } from "lucide-react";-import { useEffect, useMemo, useState } from "react";+import { type CSSProperties, useEffect, useMemo, useState } from "react";import { Button } from "@/components/ui/button";import { Checkbox } from "@/components/ui/checkbox";import { SearchInput } from "@/components/ui/search-input";import { useToast } from "@/components/ui/toast/use-toast";import { useChatStore } from "@/stores/use-chat-store";+import { useAppStore } from "@/stores/use-app-store";import { useGlobalInstructionsStore } from "@/stores/use-global-instructions-store";export function ChatHistoryPage() {const navigate = useNavigate();const toast = useToast();+ const chatWidth = useAppStore((s) => s.chatWidth);const [searchQuery, setSearchQuery] = useState("");const savedChats = useChatStore((s) => s.savedChats);const loadSavedChats = useChatStore((s) => s.loadSavedChats);@@ -89,23 +91,23 @@ export function ChatHistoryPage() {const labelText = isAllSelected ? "All selected" : selectedCount === 0 ? "Select all" : `${selectedCount} selected`;return (- <>- <div className="sticky top-0 z-10">+ <div className="flex h-full min-h-0 flex-col" style={{ "--chat-width": `${chatWidth}px` } as CSSProperties}>+ <div className="shrink-0 z-10"><div className="w-full bg-bg-modal">- <div className="pt-7 pb-7 flex flex-col justify-center px-5 lg:px-0 lg:max-w-[800px] mx-auto gap-6 lg:gap-0">+ <div className="pt-7 pb-7 flex flex-col justify-center px-5 w-full max-w-[calc(var(--chat-width)+2.5rem)] mx-auto gap-6 lg:gap-0"><h1 className="leading-[130%] text-xl font-medium text-label-title text-center lg:text-left">Chat history</h1></div>- <div className="px-5 lg:px-0 pt-4 pb-7 lg:max-w-[800px] mx-auto">+ <div className="px-5 pt-4 pb-7 w-full max-w-[calc(var(--chat-width)+2.5rem)] mx-auto"><GlobalInstructions instructions={instructions} onSave={saveInstructions} /></div><div className="h-[1px] bg-cell-border" /></div><div className="w-full bg-background">- <div className="px-5 lg:px-0 pt-5 pb-2.5 flex items-center justify-between lg:max-w-[800px] mx-auto">+ <div className="px-5 pt-5 pb-2.5 flex items-center justify-between w-full max-w-[calc(var(--chat-width)+2.5rem)] mx-auto"><h2 className="text-sm font-[350] text-label-title">Your chats</h2><div className="flex items-center gap-2"><SearchInput@@ -174,8 +176,8 @@ export function ChatHistoryPage() {</div></div>- <div className="w-full lg:max-w-[808px] pl-2 overflow-auto mx-auto bg-background">- <div className="px-5 lg:px-0 overflow-visible w-full">+ <div className="flex-1 min-h-0 w-full overflow-y-auto overscroll-y-contain thin-scrollbar bg-background">+ <div className="px-5 overflow-visible w-full max-w-[calc(var(--chat-width)+2.5rem)] mx-auto">{filteredChats.length === 0 ? (<div className="text-center py-8"><p className="text-label-muted">@@ -222,6 +224,6 @@ export function ChatHistoryPage() {currentName={singleSelectedChat.title}/>)}- </>+ </div>);}diff --git a/apps/mirai-gui/client/src/features/chat-history/components/global-instructions.tsx b/apps/mirai-gui/client/src/features/chat-history/components/global-instructions.tsxindex 44f4588ff..07cebc421 100644--- a/apps/mirai-gui/client/src/features/chat-history/components/global-instructions.tsx+++ b/apps/mirai-gui/client/src/features/chat-history/components/global-instructions.tsx@@ -129,7 +129,7 @@ const GlobalInstructions: React.FC<GlobalInstructionsProps> = ({ instructions, o</CardContainer><CardContainer- className={`overflow-hidden transition-all duration-300 ease-in-out ${isExpanded ? "max-h-96 opacity-100 mt-3" : "max-h-0 opacity-0"}`}+ className={`overflow-clip transition-all duration-300 ease-in-out ${isExpanded ? "max-h-96 opacity-100 mt-3" : "max-h-0 opacity-0"}`}><div className="p-3"><HeadlessTextarea@@ -138,7 +138,7 @@ const GlobalInstructions: React.FC<GlobalInstructionsProps> = ({ instructions, oonChange={handleChange}onBlur={flushSave}placeholder="Add instructions to all chats"- className="w-full p-1 bg-bg-modal rounded-md text-sm leading-[150%] text-label-title placeholder:text-label-muted focus:outline-hidden focus:border-primary resize-none thin-scrollbar"+ className="w-full p-1 bg-bg-modal rounded-md text-sm leading-[150%] text-label-title placeholder:text-label-muted focus:outline-hidden focus:border-primary resize-none overscroll-y-auto thin-scrollbar"rows={7}/></div>diff --git a/apps/mirai-gui/client/src/features/chat-history/components/saved-chats.test.tsx b/apps/mirai-gui/client/src/features/chat-history/components/saved-chats.test.tsxnew file mode 100644index 000000000..75d8d2dd9--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat-history/components/saved-chats.test.tsx@@ -0,0 +1,115 @@+import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import SavedChats from "./saved-chats";++const { chatStore, navigate } = vi.hoisted(() => ({+ chatStore: {+ savedChats: [{ id: "saved-chat", title: "Saved chat", createdAt: 0, updatedAt: 0, messageCount: 2 }],+ loadSavedChats: vi.fn(),+ deleteChat: vi.fn(async () => {}),+ updateChatTitle: vi.fn(),+ },+ navigate: vi.fn(),+}));++vi.mock("@/stores/use-chat-store", () => ({+ useChatStore: (selector: (state: typeof chatStore) => unknown) => selector(chatStore),+}));++vi.mock("@tanstack/react-router", () => ({+ useNavigate: () => navigate,+ useParams: () => ({ chatId: "another-chat" }),+}));++vi.mock("./chat-delete-modal", () => ({+ ChatDeleteModal: ({ isOpen, onConfirm }: { isOpen: boolean; onConfirm: () => void }) =>+ isOpen ? (+ <div role="dialog" aria-label="Delete chat">+ <button onClick={onConfirm}>Confirm deletion</button>+ </div>+ ) : null,+}));++beforeEach(() => {+ vi.clearAllMocks();+ vi.stubGlobal(+ "ResizeObserver",+ class {+ observe() {}+ unobserve() {}+ disconnect() {}+ },+ );+});++afterEach(() => {+ cleanup();+ fireEvent.keyUp(window, { key: "Shift" });+ vi.unstubAllGlobals();+});++async function openMenu() {+ render(<SavedChats />);+ fireEvent.click(screen.getByRole("button", { name: "Options for Saved chat" }));+ return screen.findByRole("menu");+}++it("keeps the open menu mounted while Shift is pressed and released", async () => {+ const menu = await openMenu();++ fireEvent.keyDown(menu, { key: "Shift", shiftKey: true });+ expect(screen.getByRole("menu")).toBe(menu);+ expect(screen.getByRole("menuitem", { name: "Delete" })).toBeDefined();++ fireEvent.keyUp(menu, { key: "Shift" });+ expect(screen.getByRole("menu")).toBe(menu);+ expect(chatStore.deleteChat).not.toHaveBeenCalled();+ expect(navigate).not.toHaveBeenCalled();+});++it("deletes without confirmation when Shift-clicking Delete in an open menu", async () => {+ const menu = await openMenu();+ fireEvent.keyDown(menu, { key: "Shift", shiftKey: true });+ fireEvent.click(screen.getByRole("menuitem", { name: "Delete" }), { shiftKey: true });++ await waitFor(() => expect(chatStore.deleteChat).toHaveBeenCalledWith("saved-chat"));+ expect(screen.queryByRole("dialog")).toBeNull();+ expect(navigate).not.toHaveBeenCalled();+});++it("deletes without confirmation when activating Delete with Shift+Enter", async () => {+ const menu = await openMenu();+ fireEvent.keyDown(menu, { key: "End" });+ const deleteItem = screen.getByRole("menuitem", { name: "Delete" });+ await waitFor(() => expect(menu.getAttribute("aria-activedescendant")).toBe(deleteItem.id));++ fireEvent.keyDown(menu, { key: "Shift", shiftKey: true });+ fireEvent.keyDown(menu, { key: "Enter", shiftKey: true });++ await waitFor(() => expect(chatStore.deleteChat).toHaveBeenCalledWith("saved-chat"));+ expect(screen.queryByRole("dialog")).toBeNull();+ expect(navigate).not.toHaveBeenCalled();+});++it("asks for confirmation when deleting after releasing Shift", async () => {+ const menu = await openMenu();+ fireEvent.keyDown(menu, { key: "Shift", shiftKey: true });+ fireEvent.keyUp(menu, { key: "Shift" });+ fireEvent.click(screen.getByRole("menuitem", { name: "Delete" }));++ expect(screen.getByRole("dialog", { name: "Delete chat" })).toBeDefined();+ expect(chatStore.deleteChat).not.toHaveBeenCalled();+ fireEvent.click(screen.getByRole("button", { name: "Confirm deletion" }));+ await waitFor(() => expect(chatStore.deleteChat).toHaveBeenCalledWith("saved-chat"));+});++it("keeps Shift quick-delete available while the menu is closed", async () => {+ render(<SavedChats />);+ fireEvent.keyDown(window, { key: "Shift", shiftKey: true });+ fireEvent.click(screen.getByRole("button", { name: "Delete Saved chat" }), { shiftKey: true });++ await waitFor(() => expect(chatStore.deleteChat).toHaveBeenCalledWith("saved-chat"));+ expect(screen.queryByRole("dialog")).toBeNull();+ expect(screen.queryByRole("menu")).toBeNull();+ expect(navigate).not.toHaveBeenCalled();+});diff --git a/apps/mirai-gui/client/src/features/chat-history/components/saved-chats.tsx b/apps/mirai-gui/client/src/features/chat-history/components/saved-chats.tsxindex aa55fe920..33c1b0519 100644--- a/apps/mirai-gui/client/src/features/chat-history/components/saved-chats.tsx+++ b/apps/mirai-gui/client/src/features/chat-history/components/saved-chats.tsx@@ -3,11 +3,14 @@ import { useNavigate, useParams } from "@tanstack/react-router";import { Edit3, MoreHorizontal, Trash2 } from "lucide-react";import { useEffect, useRef, useState } from "react";import { useChatStore } from "@/stores/use-chat-store";+import { useSidebarStore } from "@/stores/use-sidebar-store";import { CHAT_TITLE_MAX_LENGTH, CHAT_TITLE_MIN_LENGTH } from "@/constants/chat";import { useShiftHeld } from "@/hooks/use-shift-held";import { ChatDeleteModal } from "@/features/chat-history/components/chat-delete-modal";import type { ChatMetadata } from "@/platform/services/storage";+const VISIBILITY_THRESHOLDS = Array.from({ length: 101 }, (_, index) => index / 100);+export default function SavedChats() {const navigate = useNavigate();const { chatId: currentChatId } = useParams({ strict: false }) as {@@ -17,6 +20,33 @@ export default function SavedChats() {const loadSavedChats = useChatStore((s) => s.loadSavedChats);const deleteChat = useChatStore((s) => s.deleteChat);const updateChatTitle = useChatStore((s) => s.updateChatTitle);+ const setChatVisibility = useSidebarStore((s) => s.setChatVisibility);+ const scrollViewportRef = useRef<HTMLDivElement>(null);+ const currentChatRowRef = useRef<HTMLDivElement>(null);+ const currentChatExists = savedChats.some((chat) => chat.id === currentChatId);++ // Keep the measurement while collapsed so the title and sidebar start+ // their transitions together when reopened.+ useEffect(() => {+ setChatVisibility(null);+ const root = scrollViewportRef.current;+ const row = currentChatRowRef.current;+ if (!currentChatId || !currentChatExists || !root || !row || typeof IntersectionObserver === "undefined") return;+ let active = true;+ const observer = new IntersectionObserver(+ ([entry]) => {+ if (!active) return;+ setChatVisibility({ chatId: currentChatId, ratio: entry?.isIntersecting ? entry.intersectionRatio : 0 });+ },+ { root, threshold: VISIBILITY_THRESHOLDS },+ );+ observer.observe(row);+ return () => {+ active = false;+ observer.disconnect();+ setChatVisibility(null);+ };+ }, [currentChatId, currentChatExists, setChatVisibility]);const [showDeleteModal, setShowDeleteModal] = useState(false);const [selectedChat, setSelectedChat] = useState<ChatMetadata | null>(null);@@ -106,138 +136,142 @@ export default function SavedChats() {};return (- <div className="flex flex-col gap-2">- {savedChats.length === 0 ? (- <div className="px-4 py-2 text-[13px] font-[350] leading-[150%] text-label-muted">No chats yet</div>- ) : (- savedChats.map((chat: ChatMetadata) => (- <div- key={chat.id}- role="button"- tabIndex={0}- aria-label={chat.title}- onClick={() => {- if (editingChatId !== chat.id) handleChatClick(chat.id);- }}- onKeyDown={(e) => {- if (e.target !== e.currentTarget) return;- if (e.key === "Enter" || e.key === " ") {- e.preventDefault();+ <div ref={scrollViewportRef} className="flex-1 overflow-y-auto overscroll-y-contain min-h-0 scrollbar-hide">+ <div className="flex flex-col gap-2">+ {savedChats.length === 0 ? (+ <div className="px-4 py-2 text-[13px] font-[350] leading-[150%] text-label-muted">No chats yet</div>+ ) : (+ savedChats.map((chat: ChatMetadata) => (+ <div+ key={chat.id}+ ref={chat.id === currentChatId ? currentChatRowRef : undefined}+ role="button"+ tabIndex={0}+ aria-label={chat.title}+ aria-current={chat.id === currentChatId ? "page" : undefined}+ onClick={() => {if (editingChatId !== chat.id) handleChatClick(chat.id);- }- }}- className="flex px-2"- >- <Menu>- {({ open }) => (- <div- onDoubleClick={(e) => {- e.stopPropagation();- startRename(chat);- }}- className={`flex items-center gap-3 py-[6px] px-2 w-full rounded-md group ${currentChatId === chat.id || editingChatId === chat.id || open ? "bg-bg-hover dark:bg-bg-hover" : "hover:bg-bg-hover hover:dark:bg-bg-hover"}`}- >- <div className="flex-1 min-w-0">- {editingChatId === chat.id ? (- <input- ref={renameInputRef}- value={draftTitle}- maxLength={CHAT_TITLE_MAX_LENGTH}- onChange={(e) => setDraftTitle(e.target.value)}- onClick={(e) => e.stopPropagation()}- onKeyDown={(e) => {+ }}+ onKeyDown={(e) => {+ if (e.target !== e.currentTarget) return;+ if (e.key === "Enter" || e.key === " ") {+ e.preventDefault();+ if (editingChatId !== chat.id) handleChatClick(chat.id);+ }+ }}+ className="flex px-2"+ >+ <Menu>+ {({ open }) => (+ <div+ onDoubleClick={(e) => {+ e.stopPropagation();+ startRename(chat);+ }}+ className={`flex items-center gap-3 py-[6px] px-2 w-full rounded-md group transition-colors duration-150 ${currentChatId === chat.id ? "bg-sidebar-chat-selected" : editingChatId === chat.id || open ? "bg-sidebar-chat-hover" : "hover:bg-sidebar-chat-hover"}`}+ >+ <div className="flex-1 min-w-0">+ {editingChatId === chat.id ? (+ <input+ ref={renameInputRef}+ value={draftTitle}+ maxLength={CHAT_TITLE_MAX_LENGTH}+ onChange={(e) => setDraftTitle(e.target.value)}+ onClick={(e) => e.stopPropagation()}+ onKeyDown={(e) => {+ e.stopPropagation();+ if (e.key === "Enter") void commitRename();+ if (e.key === "Escape") cancelRename();+ }}+ onBlur={() => void commitRename()}+ className="block w-full text-left text-[13px] font-[350] leading-[150%] text-label-title bg-transparent outline-hidden border-none p-0"+ />+ ) : (+ <p className="w-full text-left text-[13px] font-[350] leading-[150%] text-label-title truncate">+ {chat.title}+ </p>+ )}+ </div>+ {shiftHeld && !open ? (+ <button+ type="button"+ aria-label={`Delete ${chat.title}`}+ onClick={(e) => {e.stopPropagation();- if (e.key === "Enter") void commitRename();- if (e.key === "Escape") cancelRename();+ void performDelete(chat);}}- onBlur={() => void commitRename()}- className="block w-full text-left text-[13px] font-[350] leading-[150%] text-label-title bg-transparent outline-hidden border-none p-0"- />+ onDoubleClick={(e) => e.stopPropagation()}+ className="opacity-0 group-hover:opacity-100 hover:bg-bg-hover p-1 rounded transition-opacity"+ >+ <Trash2 className="w-3 h-3 text-error" />+ </button>) : (- <p className="w-full text-left text-[13px] font-[350] leading-[150%] text-label-title truncate">- {chat.title}- </p>+ <>+ <MenuButton+ aria-label={`Options for ${chat.title}`}+ onClick={(e) => e?.stopPropagation()}+ onDoubleClick={(e) => e.stopPropagation()}+ className={`${open ? "opacity-100 bg-bg-hover dark:bg-bg-hover" : "opacity-0 group-hover:opacity-100 data-[focus]:opacity-100"} hover:bg-bg-hover p-1 rounded transition-opacity outline-hidden data-[focus]:shadow-focus`}+ >+ <MoreHorizontal className="w-3 h-3 text-label-muted" />+ </MenuButton>+ <MenuItems+ anchor="bottom end"+ className="w-32 rounded-lg border border-cell-border bg-bg-modal [--anchor-gap:4px] outline-hidden focus:outline-hidden focus:ring-0 z-50 translate-x-2"+ >+ <MenuItem>+ <button+ onClick={(e) => {+ e.stopPropagation();+ startRename(chat);+ }}+ className="group/item flex w-full p-1 outline-hidden"+ >+ <div className="flex grow rounded-md py-1.5 px-2 gap-2 w-full items-center hover:bg-card-hover group-data-[focus]/item:bg-card-hover transition-colors">+ <Edit3 className="w-3 h-3 text-label-title" />+ <span className="text-xs text-label-title">Rename</span>+ </div>+ </button>+ </MenuItem>+ <MenuItem>+ <button+ onClick={(e) => {+ e.stopPropagation();+ handleDeleteChat(chat, e.shiftKey || shiftHeld);+ }}+ className="group/item flex w-full p-1 outline-hidden"+ >+ <div className="flex grow rounded-md py-1.5 px-2 gap-2 w-full items-center hover:bg-card-hover group-data-[focus]/item:bg-card-hover transition-colors">+ <Trash2 className="w-3 h-3 text-error" />+ <span className="text-xs text-error">Delete</span>+ </div>+ </button>+ </MenuItem>+ </MenuItems>+ </>)}</div>- {shiftHeld ? (- <button- type="button"- aria-label={`Delete ${chat.title}`}- onClick={(e) => {- e.stopPropagation();- void performDelete(chat);- }}- onDoubleClick={(e) => e.stopPropagation()}- className="opacity-0 group-hover:opacity-100 hover:bg-bg-hover p-1 rounded transition-opacity"- >- <Trash2 className="w-3 h-3 text-error" />- </button>- ) : (- <>- <MenuButton- aria-label={`Options for ${chat.title}`}- onClick={(e) => e?.stopPropagation()}- onDoubleClick={(e) => e.stopPropagation()}- className={`${open ? "opacity-100 bg-bg-hover dark:bg-bg-hover" : "opacity-0 group-hover:opacity-100 data-[focus]:opacity-100"} hover:bg-bg-hover p-1 rounded transition-opacity outline-hidden data-[focus]:shadow-focus`}- >- <MoreHorizontal className="w-3 h-3 text-label-muted" />- </MenuButton>- <MenuItems- anchor="bottom end"- className="w-32 rounded-lg border border-cell-border bg-bg-modal [--anchor-gap:4px] outline-hidden focus:outline-hidden focus:ring-0 z-50 translate-x-2"- >- <MenuItem>- <button- onClick={(e) => {- e.stopPropagation();- startRename(chat);- }}- className="group/item flex w-full p-1 outline-hidden"- >- <div className="flex grow rounded-md py-1.5 px-2 gap-2 w-full items-center hover:bg-card-hover group-data-[focus]/item:bg-card-hover transition-colors">- <Edit3 className="w-3 h-3 text-label-title" />- <span className="text-xs text-label-title">Rename</span>- </div>- </button>- </MenuItem>- <MenuItem>- <button- onClick={(e) => {- e.stopPropagation();- handleDeleteChat(chat, e.shiftKey);- }}- className="group/item flex w-full p-1 outline-hidden"- >- <div className="flex grow rounded-md py-1.5 px-2 gap-2 w-full items-center hover:bg-card-hover group-data-[focus]/item:bg-card-hover transition-colors">- <Trash2 className="w-3 h-3 text-error" />- <span className="text-xs text-error">Delete</span>- </div>- </button>- </MenuItem>- </MenuItems>- </>- )}- </div>- )}- </Menu>- </div>- ))- )}-- {selectedChat && (- <ChatDeleteModal- isOpen={showDeleteModal}- onClose={() => {- setShowDeleteModal(false);- setSelectedChat(null);- }}- onConfirm={async () => {- if (selectedChat) await performDelete(selectedChat);- }}- chatName={selectedChat.title}- isDeleting={isDeleting}- />- )}+ )}+ </Menu>+ </div>+ ))+ )}++ {selectedChat && (+ <ChatDeleteModal+ isOpen={showDeleteModal}+ onClose={() => {+ setShowDeleteModal(false);+ setSelectedChat(null);+ }}+ onConfirm={async () => {+ if (selectedChat) await performDelete(selectedChat);+ }}+ chatName={selectedChat.title}+ isDeleting={isDeleting}+ />+ )}+ </div></div>);}diff --git a/apps/mirai-gui/client/src/features/chat/components/chat-header.test.tsx b/apps/mirai-gui/client/src/features/chat/components/chat-header.test.tsxnew file mode 100644index 000000000..556002cff--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/components/chat-header.test.tsx@@ -0,0 +1,179 @@+import { act, cleanup, render, screen } from "@testing-library/react";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { useSidebarStore } from "@/stores/use-sidebar-store";+import { useChatStore } from "@/stores/use-chat-store";+import SavedChats from "@/features/chat-history/components/saved-chats";+import { ChatHeader } from "./chat-header";+import { UNTITLED_CHAT_TITLE } from "@/constants/chat";++const route = vi.hoisted(() => ({ chatId: "first" }));+vi.mock("@tanstack/react-router", () => ({+ useParams: () => route,+ useNavigate: () => vi.fn(),+}));++const chatDefaults = useChatStore.getState();+const sidebarDefaults = useSidebarStore.getState();+const observers: Observer[] = [];++class Observer {+ observed: Element | undefined;+ disconnect = vi.fn();+ constructor(+ readonly callback: IntersectionObserverCallback,+ readonly options: IntersectionObserverInit,+ ) {+ observers.push(this);+ }+ observe(element: Element) {+ this.observed = element;+ }+ emit(ratio: number) {+ this.callback(+ [{ target: this.observed, isIntersecting: ratio > 0, intersectionRatio: ratio } as IntersectionObserverEntry],+ this as unknown as IntersectionObserver,+ );+ }+}++const Harness = () => {+ const isOpen = useSidebarStore((state) => state.isOpen);+ return (+ <>+ <SavedChats />+ <ChatHeader title={`Chat ${route.chatId}`} isSidebarOpen={isOpen} />+ </>+ );+};++const titleWrapper = () => screen.getByRole("heading", { hidden: true }).parentElement!;++beforeEach(() => {+ observers.length = 0;+ route.chatId = "first";+ vi.stubGlobal("IntersectionObserver", Observer);+ useSidebarStore.setState({ ...sidebarDefaults, isOpen: true, chatVisibility: null }, true);+ useChatStore.setState(+ {+ ...chatDefaults,+ loadSavedChats: vi.fn(async () => {}),+ savedChats: ["first", "second"].map((id) => ({+ id,+ title: `Chat ${id}`,+ createdAt: 1,+ updatedAt: 1,+ messageCount: 2,+ })),+ },+ true,+ );+});++afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+ useChatStore.setState(chatDefaults, true);+ useSidebarStore.setState(sidebarDefaults, true);+});++it("fades the title smoothly between 15% and 75% of the selected row being clipped", () => {+ render(<Harness />);+ const row = screen.getByRole("button", { name: "Chat first" });+ const observer = observers[0]!;+ expect(observer.observed).toBe(row);+ expect(observer.options.root).toBe(row.parentElement!.parentElement);+ expect(observer.options.threshold).toEqual(Array.from({ length: 101 }, (_, index) => index / 100));+ expect(observer.options.root).toHaveProperty("className", expect.stringContaining("overflow-y-auto"));+ const heading = screen.getByRole("heading");+ const wrapper = titleWrapper();+ expect(wrapper.style.opacity).toBe("1");++ act(() => observer.emit(1));+ expect(wrapper.style.opacity).toBe("0");+ expect(wrapper.getAttribute("aria-hidden")).toBe("true");+ expect(screen.getByRole("heading", { hidden: true })).toBe(heading);+ expect(wrapper.className).toContain("transition-[padding-left,opacity]");+ expect(wrapper.className).toContain("duration-300");+ expect(wrapper.className).toContain("motion-reduce:transition-none");++ for (const ratio of [0.9, 0.85]) {+ act(() => observer.emit(ratio));+ expect(wrapper.style.opacity).toBe("0");+ expect(wrapper.getAttribute("aria-hidden")).toBe("true");+ }+ for (const [ratio, opacity] of [+ [0.7, 0.15625],+ [0.55, 0.5],+ [0.4, 0.84375],+ ] as const) {+ act(() => observer.emit(ratio));+ expect(Number(wrapper.style.opacity)).toBeCloseTo(opacity);+ }+ expect(wrapper.getAttribute("aria-hidden")).toBe("false");+ act(() => observer.emit(0.25));+ expect(wrapper.style.opacity).toBe("1");+ act(() => observer.emit(0));+ expect(wrapper.style.opacity).toBe("1");+ act(() => observer.emit(1));+ expect(wrapper.style.opacity).toBe("0");+});++it("retains row visibility while closed so reopening starts the title fade with the sidebar", () => {+ render(<Harness />);+ const observer = observers[0]!;+ act(() => observer.emit(1));+ expect(titleWrapper().style.opacity).toBe("0");+ act(() => useSidebarStore.getState().toggle());+ expect(observer.disconnect).not.toHaveBeenCalled();+ expect(titleWrapper().style.opacity).toBe("1");+ expect(useSidebarStore.getState().chatVisibility).toEqual({ chatId: "first", ratio: 1 });++ act(() => useSidebarStore.getState().toggle());+ expect(observers).toHaveLength(1);+ expect(titleWrapper().style.opacity).toBe("0");++ act(() => useSidebarStore.getState().toggle());+ act(() => observer.emit(0.55));+ expect(titleWrapper().style.opacity).toBe("1");+ act(() => useSidebarStore.getState().toggle());+ expect(Number(titleWrapper().style.opacity)).toBeCloseTo(0.5);+});++it("tracks the new active row on chat switches and clears visibility when it disappears", () => {+ const view = render(<Harness />);+ const old = observers[0]!;+ act(() => old.emit(1));+ route.chatId = "second";+ view.rerender(<Harness />);+ expect(titleWrapper().style.opacity).toBe("1");+ expect(observers.at(-1)!.observed).toBe(screen.getByRole("button", { name: "Chat second" }));+ act(() => old.emit(1));+ expect(useSidebarStore.getState().chatVisibility).toBeNull();+ act(() => observers.at(-1)!.emit(1));+ expect(titleWrapper().style.opacity).toBe("0");++ act(() => useChatStore.setState({ savedChats: [] }));+ expect(titleWrapper().style.opacity).toBe("1");+ expect(useSidebarStore.getState().chatVisibility).toBeNull();+});++it("keeps the title visible when its row is not mounted or observation is unavailable", () => {+ route.chatId = "unsaved";+ const view = render(<Harness />);+ expect(observers).toHaveLength(0);+ expect(titleWrapper().style.opacity).toBe("1");+ vi.stubGlobal("IntersectionObserver", undefined);+ route.chatId = "first";+ view.rerender(<Harness />);+ expect(observers).toHaveLength(0);+ expect(titleWrapper().style.opacity).toBe("1");+});++it("keeps the new-chat placeholder hidden until the chat has a name", () => {+ const view = render(<ChatHeader title={UNTITLED_CHAT_TITLE} isSidebarOpen={false} />);+ expect(titleWrapper().getAttribute("aria-hidden")).toBe("true");+ expect(titleWrapper().style.opacity).toBe("0");+ view.rerender(<ChatHeader title="Cities" isSidebarOpen={false} />);+ expect(titleWrapper().getAttribute("aria-hidden")).toBe("false");+ expect(titleWrapper().style.opacity).toBe("1");+});diff --git a/apps/mirai-gui/client/src/features/chat/components/chat-header.tsx b/apps/mirai-gui/client/src/features/chat/components/chat-header.tsxindex 1db9f001e..f341ab9dc 100644--- a/apps/mirai-gui/client/src/features/chat/components/chat-header.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/chat-header.tsx@@ -1,6 +1,9 @@import { twMerge } from "tailwind-merge";import { LoaderIcon } from "@/components/loader";import { platformInfo } from "@/platform/platform-info";+import { useSidebarStore } from "@/stores/use-sidebar-store";+import { useParams } from "@tanstack/react-router";+import { UNTITLED_CHAT_TITLE } from "@/constants/chat";const HEADER_LEFT_RESERVE_PX = platformInfo.features.nativeTitleBar ? 120 : 56;@@ -15,20 +18,22 @@ export const ChatHeader = ({isTitleGenerating?: boolean;className?: string;}) => {+ const { chatId } = useParams({ strict: false }) as { chatId?: string };+ const chatVisibility = useSidebarStore((s) => s.chatVisibility);+ const sidebarVisibility =+ isSidebarOpen && chatVisibility && chatVisibility.chatId === chatId ? chatVisibility.ratio : 0;+ // Fade smoothly between 15% and 75% clipped, staying flat at either end.+ const fadeProgress = Math.max(0, Math.min(1, (0.85 - sidebarVisibility) / 0.6));+ const opacity = title === UNTITLED_CHAT_TITLE ? 0 : fadeProgress * fadeProgress * (3 - 2 * fadeProgress);return (<divdata-tauri-drag-region="deep"className={twMerge("flex items-center justify-between pb-4 text-center lg:text-left", className)}><div- className="flex items-center gap-1 w-auto lg:w-auto justify-center lg:justify-start min-w-0"- style={- !isSidebarOpen- ? {- paddingLeft: `clamp(0px, calc(${HEADER_LEFT_RESERVE_PX}px - var(--sidebar-width, 0px)), ${HEADER_LEFT_RESERVE_PX}px)`,- }- : undefined- }+ aria-hidden={opacity === 0}+ className="flex items-center gap-1 w-auto lg:w-auto justify-center lg:justify-start min-w-0 transition-[padding-left,opacity] duration-300 ease-in-out motion-reduce:transition-none"+ style={{ paddingLeft: isSidebarOpen ? 0 : HEADER_LEFT_RESERVE_PX, opacity }}><h1className={twMerge(diff --git a/apps/mirai-gui/client/src/features/chat/components/chat-page.tsx b/apps/mirai-gui/client/src/features/chat/components/chat-page.tsxindex c7edeaf5f..0c9b4a099 100644--- a/apps/mirai-gui/client/src/features/chat/components/chat-page.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/chat-page.tsx@@ -1,4 +1,4 @@-import { useCallback, useEffect, useState } from "react";+import { useCallback, useEffect, useState, type CSSProperties } from "react";import { useParams, useSearch } from "@tanstack/react-router";import { ChatHeader } from "./chat-header";@@ -7,6 +7,8 @@ import { useToast } from "@/components/ui/toast/use-toast";import { useRuntimeSessionStore } from "@/stores/use-runtime-session-store";import { useChatSessionStore } from "@/stores/use-chat-session-store";import { useChatStore } from "@/stores/use-chat-store";+import { useModelsStore } from "@/stores/use-models-store";+import { useAppStore } from "@/stores/use-app-store";import { useGlobalInstructionsStore } from "@/stores/use-global-instructions-store";import { useSidebarStore } from "@/stores/use-sidebar-store";import { isChatGenerating } from "@/features/runtime/runtime-busy";@@ -40,10 +42,14 @@ export function ChatPage() {const toast = useToast();const isSidebarOpen = useSidebarStore((s) => s.isOpen);+ const chatWidth = useAppStore((s) => s.chatWidth);const globalInstructions = useGlobalInstructionsStore((s) => s.instructions);const loadInstructions = useGlobalInstructionsStore((s) => s.loadInstructions);const residentSession = useRuntimeSessionStore((s) => s.residentSession);const isModelLoading = useChatSessionStore((s) => s.isModelLoading);+ const isSessionBusy = useChatSessionStore(+ (s) => s.operationState !== "idle" || s.isGenerating || s.isTitleGenerating || s.isModelLoading || s.isEjecting,+ );const isTitleGenerating = useChatSessionStore((s) => s.isTitleGenerating);const titleGenChatId = useChatSessionStore((s) => s.titleGenChatId);const isTitleGeneratingForChat = isTitleGenerating && titleGenChatId === chatId;@@ -112,6 +118,11 @@ export function ChatPage() {startStream,});+ const handleEditMessage = useCallback(+ (messageId: string, text: string, onSaved: () => void) => handleSendMessage(text, { messageId, onSaved }),+ [handleSendMessage],+ );+const handleChatModelSelect = useModelSwitch({chatId,toast,@@ -132,19 +143,26 @@ export function ChatPage() {const selectedModelParams = useModelParamsStore((s) =>effectiveSelectedModelId ? s.paramsByRepoId[effectiveSelectedModelId] : undefined,);- const globalReasoningEnabled = useModelParamsStore((s) => s.globalReasoningEnabled);- const modelParamsModified = isCustomParams(selectedModelParams, globalReasoningEnabled);+ const globalModelChatNamingEnabled = useModelParamsStore((s) => s.globalModelChatNamingEnabled);+ const selectedModelSize = useModelsStore(+ (s) => s.models.find((model) => model.repoId === effectiveSelectedModelId)?.paramSize,+ );+ const modelParamsModified = isCustomParams(selectedModelParams, globalModelChatNamingEnabled, selectedModelSize);return (- <div className="flex flex-col h-[calc(100vh-24px)] bg-background pt-4 pb-5 px-5">- <div className="w-full flex flex-col h-full">+ <div+ className="flex flex-col h-full min-h-0 bg-background pt-4 pb-5 px-5"+ style={{ "--chat-width": `${chatWidth}px` } as CSSProperties}+ >+ <div className="w-full flex flex-col h-full min-h-0"><ChatHeader+ className="shrink-0"title={currentChatTitle}isSidebarOpen={isSidebarOpen}isTitleGenerating={isTitleGeneratingForChat}/>- <div className="flex-1 overflow-hidden min-h-0 w-full lg:max-w-[800px] mx-auto">+ <div className="flex-1 overflow-clip min-h-0 w-full max-w-[var(--chat-width)] mx-auto"><ChatMessageListisNewChat={!!search.isNew}isChatStreaming={isChatStreaming}@@ -157,10 +175,12 @@ export function ChatPage() {loadingMessageId={loadingMessageId}canceledMessageId={canceledMessageId}onMessageModelSelect={handleMessageModelSelect}+ onEditMessage={handleEditMessage}+ canEditMessages={runStatus.canRun && !isSessionBusy}/></div>- <div className="flex-shrink-0 w-full lg:max-w-[800px] mx-auto">+ <div className="flex-shrink-0 w-full max-w-[var(--chat-width)] mx-auto"><ChatComposerchatId={chatId}isChatStreaming={isChatStreaming}diff --git a/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/index.tsx b/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/index.tsxindex dca9411ab..887f9b278 100644--- a/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/index.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/index.tsx@@ -5,6 +5,7 @@ import { TextArea } from "@/components/ui/text-area";import { AttachMenu } from "./attach-menu";import { FileChip } from "./file-chip";import { ModelPicker } from "./model-picker";+import { ReasoningSelector } from "./reasoning-selector";import { SendButton } from "./send-button";import { CHAT_INPUT_TEXTAREA_CLASSNAME, CHAT_INPUT_TEXTAREA_STYLE } from "./constants";import type { ChatInputProps } from "./types";@@ -18,7 +19,7 @@ export function ChatInput({onBlockedSend,onAttach,attachAccept = ".txt,.md,.json,.csv,.yaml,.yml",- placeholder = "Send a message to the local model…",+ placeholder = "Send a message to the model…",files,onRemoveFile,models,@@ -83,7 +84,7 @@ export function ChatInput({style={{ ...CHAT_INPUT_TEXTAREA_STYLE, minHeight: 23, maxHeight: 320 }}/>- <div className="flex items-center justify-between mt-4">+ <div className="flex items-center justify-between gap-2 mt-4"><AttachMenu onSelectFile={handleFileSelect} /><div className="flex items-center gap-2 min-w-0">@@ -94,6 +95,9 @@ export function ChatInput({moreModelsLink={moreModelsLink}disabled={modelPickerDisabled}/>+ {activeModelId ? (+ <ReasoningSelector key={activeModelId} repoId={activeModelId} disabled={modelPickerDisabled} />+ ) : null}{onModelSettingsClick ? (<span className="relative inline-flex shrink-0"><buttondiff --git a/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/model-picker.tsx b/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/model-picker.tsxindex 349831bd4..81c50aeed 100644--- a/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/model-picker.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/model-picker.tsx@@ -39,7 +39,7 @@ const SelectionRow = forwardRef<HTMLElement, SelectionRowProps>(function Selectiref={ref}active={active}selected={selected}- className="flex items-center gap-2 w-full border-none outline-hidden bg-transparent text-text-primary hover:bg-surface-tertiary"+ className="flex items-center gap-2 w-full border-none outline-hidden bg-transparent text-text-primary"{...rest}onClick={onClick}>@@ -93,23 +93,24 @@ export function ModelPicker({<Transitionas={Fragment}- enter="transition-[opacity,transform] duration-[120ms] ease-spring"- enterFrom="transform opacity-0 scale-90"- enterTo="transform opacity-100 scale-100"- leave="transition-[opacity,transform] duration-[80ms] ease-spring"- leaveFrom="transform opacity-100 scale-100"- leaveTo="transform opacity-0 scale-90"+ enter="transition-[opacity,scale] duration-[120ms] ease-spring"+ enterFrom="opacity-0 scale-90"+ enterTo="opacity-100 scale-100"+ leave="transition-[opacity,scale] duration-[80ms] ease-spring"+ leaveFrom="opacity-100 scale-100"+ leaveTo="opacity-0 scale-90"><divclassName={menuPlacement === "up"- ? "absolute right-0 bottom-full pb-1 z-50"- : "absolute right-0 top-full pt-1 z-50"+ ? "absolute right-0 bottom-full pb-1 z-50 origin-bottom-right"+ : "absolute right-0 top-full pt-1 z-50 origin-top-right"}><MenuItems className="outline-hidden"><SelectionPanel className="pt-1.5 min-w-[240px]">- <SelectionSection className="max-h-[280px] overflow-y-auto thin-scrollbar">+ {/* Reserve the scrollbar width before WebKit measures the popup. */}+ <SelectionSection className="max-h-[280px] overflow-y-scroll overscroll-y-contain thin-scrollbar">{models?.map((model) => (<MenuItem key={model.id}>{({ focus }) => (@@ -150,10 +151,10 @@ export function ModelPicker({moreModelsLink.onClick?.();close();}}- className="flex items-center w-full bg-transparent text-text-primary hover:bg-surface-tertiary"+ className="flex items-center w-full bg-transparent text-text-primary"><Text as="span" color="primary" opticalSize={14} className={TEXT_13_CLASSNAME}>- Download more local models+ Download more models</Text></SelectionItem>)}diff --git a/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/reasoning-selector.test.tsx b/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/reasoning-selector.test.tsxnew file mode 100644index 000000000..24c908d00--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/reasoning-selector.test.tsx@@ -0,0 +1,202 @@+import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { useModelParamsStore } from "@/stores/use-model-params-store";+import { useModelsStore } from "@/stores/use-models-store";+import { ModelKind, type PlatformModel } from "@/types/models";+import type { ReasoningSupport } from "@/types/sampling";+import { ChatInput } from ".";+import { ReasoningSelector } from "./reasoning-selector";++const settings = vi.hoisted(() => ({ setModelParams: vi.fn(async () => {}) }));+vi.mock("@/platform/platform-singleton", () => ({ getPlatform: () => ({ settings }) }));++const paramsDefaults = useModelParamsStore.getState();+const modelsDefaults = useModelsStore.getState();+const model: PlatformModel = {+ repoId: "model",+ name: "Model",+ vendor: "Vendor",+ kind: ModelKind.Text,+ reasoning: { kind: "toggle", defaultEffort: "default" },+ supportsTools: true,+};++beforeEach(() => {+ vi.clearAllMocks();+ vi.useFakeTimers({ shouldAdvanceTime: true });+ vi.stubGlobal(+ "ResizeObserver",+ class {+ observe() {}+ unobserve() {}+ disconnect() {}+ },+ );+ useModelParamsStore.setState(paramsDefaults, true);+ useModelsStore.setState({ ...modelsDefaults, models: [model] }, true);+});++afterEach(async () => {+ cleanup();+ await vi.runAllTimersAsync();+ vi.useRealTimers();+ vi.unstubAllGlobals();+});++it.each<ReasoningSupport>([+ { kind: "unsupported" },+ { kind: "alwaysOn" },+ { kind: "levels", efforts: [] },+ { kind: "levels", efforts: ["high"], defaultEffort: "high" },+])("hides a selector with no meaningful choice: %j", (reasoning) => {+ useModelsStore.setState({ models: [{ ...model, reasoning }] });+ render(<ReasoningSelector repoId="model" />);+ expect(screen.queryByRole("button")).toBeNull();+});++it("sits between the model picker and settings and follows the picker busy state", () => {+ const props = {+ value: "",+ onChange: vi.fn(),+ models: [{ id: model.repoId, name: model.name, logo: null }],+ activeModelId: model.repoId,+ onModelSettingsClick: vi.fn(),+ };+ const { rerender } = render(<ChatInput {...props} />);+ const modelButton = screen.getByRole("button", { name: model.name });+ const reasoningButton = screen.getByRole("button", { name: /^Reasoning:/ });+ const settingsButton = screen.getByRole("button", { name: "Model settings" });+ expect(modelButton.compareDocumentPosition(reasoningButton) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();+ expect(reasoningButton.compareDocumentPosition(settingsButton) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();++ rerender(<ChatInput {...props} modelPickerDisabled />);+ expect((reasoningButton as HTMLButtonElement).disabled).toBe(true);+ fireEvent.click(reasoningButton);+ expect(screen.queryByRole("menu")).toBeNull();+});++it("cannot change reasoning if generation starts while the menu is already open", async () => {+ const { rerender } = render(<ReasoningSelector repoId="model" />);+ fireEvent.click(screen.getByRole("button", { name: /^Reasoning:/ }));+ await screen.findByRole("menu");+ rerender(<ReasoningSelector repoId="model" disabled />);+ const off = screen.getByRole("menuitem", { name: "Off" });+ expect(off.getAttribute("aria-disabled")).toBe("true");+ fireEvent.click(off);+ expect(useModelParamsStore.getState().getParams("model").reasoningEffort).toBeUndefined();+});++it("shows actual levels, marks the model default, and clears only its reasoning override when selected", async () => {+ const otherParams = {+ sampling: { type: "Greedy" as const },+ dateTimeToolEnabled: false,+ modelChatNamingEnabled: false,+ };+ useModelsStore.setState({+ models: [+ { ...model, reasoning: { kind: "levels", efforts: ["low", "medium", "high", "high"], defaultEffort: "medium" } },+ ],+ });+ useModelParamsStore.setState({ paramsByRepoId: { model: { ...otherParams, reasoningEffort: "high" } } });+ render(<ReasoningSelector repoId="model" />);+ const trigger = screen.getByRole("button", { name: "Reasoning: High" });+ expect(useModelParamsStore.getState().getParams("model").reasoningEffort).toBe("high");+ expect(settings.setModelParams).not.toHaveBeenCalled();++ fireEvent.click(trigger);+ await screen.findByRole("menu");+ expect(screen.getAllByRole("menuitem").map((item) => item.getAttribute("aria-label"))).toEqual([+ "High",+ "Medium, model default",+ "Low",+ ]);+ expect(screen.queryByRole("menuitem", { name: "Default" })).toBeNull();+ expect(screen.queryByRole("menuitem", { name: "Off" })).toBeNull();+ expect(screen.getByTitle("Model default").textContent).toBe("default");+ fireEvent.click(screen.getByRole("menuitem", { name: "Medium, model default" }));+ expect(useModelParamsStore.getState().getParams("model")).toEqual(otherParams);+ expect(screen.getByRole("button", { name: "Reasoning: Medium, model default" })).toBeTruthy();++ fireEvent.click(trigger);+ await screen.findByRole("menu");+ fireEvent.click(screen.getByRole("menuitem", { name: "Low" }));+ expect(useModelParamsStore.getState().getParams("model")).toEqual({ ...otherParams, reasoningEffort: "low" });+});++it("does not invent a selected level or default marker when the model default is unknown", async () => {+ useModelsStore.setState({ models: [{ ...model, reasoning: { kind: "levels", efforts: ["low", "high"] } }] });+ render(<ReasoningSelector repoId="model" />);+ fireEvent.click(screen.getByRole("button", { name: "Reasoning" }));+ await screen.findByRole("menu");+ expect(screen.getAllByRole("menuitem").every((item) => !item.hasAttribute("aria-current"))).toBe(true);+ expect(screen.queryByTitle("Model default")).toBeNull();+ fireEvent.click(screen.getByRole("menuitem", { name: "High" }));+ expect(screen.getByRole("button", { name: "Reasoning: High" })).toBeTruthy();+ expect(useModelParamsStore.getState().getParams("model").reasoningEffort).toBe("high");+});++it("switches choices and stored selection with the active model without changing another model's parameters", async () => {+ useModelsStore.setState({+ models: [+ model,+ { ...model, repoId: "levels", reasoning: { kind: "levels", efforts: ["low", "high"], defaultEffort: "low" } },+ ],+ });+ useModelParamsStore.setState({+ paramsByRepoId: {+ model: { sampling: { type: "Greedy" }, reasoningEffort: "disabled" },+ levels: { sampling: { type: "Default" }, reasoningEffort: "high" },+ },+ });+ const { rerender } = render(<ReasoningSelector repoId="levels" />);+ expect(screen.getByRole("button", { name: "Reasoning: High" })).toBeTruthy();+ rerender(<ReasoningSelector repoId="model" />);+ fireEvent.click(screen.getByRole("button", { name: "Reasoning: Off" }));+ await screen.findByRole("menu");+ expect(screen.getAllByRole("menuitem").map((item) => item.getAttribute("aria-label"))).toEqual([+ "Thinking, model default",+ "Off",+ ]);+ fireEvent.click(screen.getByRole("menuitem", { name: "Thinking, model default" }));+ expect(useModelParamsStore.getState().getParams("model")).toEqual({ sampling: { type: "Greedy" } });+ expect(useModelParamsStore.getState().getParams("levels").reasoningEffort).toBe("high");+});++it("keeps keyboard navigation and activation working through the selection rows", async () => {+ render(<ReasoningSelector repoId="model" />);+ const trigger = screen.getByRole("button", { name: "Reasoning: Thinking, model default" });+ act(() => trigger.focus());+ fireEvent.keyDown(trigger, { key: "ArrowDown" });+ const menu = await screen.findByRole("menu");+ fireEvent.keyDown(menu, { key: "End" });+ const off = screen.getByRole("menuitem", { name: "Off" });+ await waitFor(() => expect(menu.getAttribute("aria-activedescendant")).toBe(off.id));+ fireEvent.keyDown(menu, { key: "Enter" });+ expect(useModelParamsStore.getState().getParams("model").reasoningEffort).toBe("disabled");+ expect(screen.getByRole("button", { name: "Reasoning: Off" })).toBeTruthy();+});++it("orders all supported reasoning levels from highest to lowest with Off last", async () => {+ useModelsStore.setState({+ models: [+ {+ ...model,+ reasoning: {+ kind: "levels",+ efforts: ["disabled", "medium", "xhigh", "low", "high", "medium"],+ defaultEffort: "medium",+ },+ },+ ],+ });+ render(<ReasoningSelector repoId="model" />);+ fireEvent.click(screen.getByRole("button", { name: "Reasoning: Medium, model default" }));+ await screen.findByRole("menu");+ expect(screen.getAllByRole("menuitem").map((item) => item.getAttribute("aria-label"))).toEqual([+ "XHigh",+ "High",+ "Medium, model default",+ "Low",+ "Off",+ ]);+});diff --git a/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/reasoning-selector.tsx b/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/reasoning-selector.tsxnew file mode 100644index 000000000..f02ca6973--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/components/composer/chat-input/reasoning-selector.tsx@@ -0,0 +1,123 @@+import { Menu, MenuButton, MenuItem, MenuItems, Transition } from "@headlessui/react";+import { Brain, ChevronDown } from "lucide-react";+import { Fragment } from "react";+import { SelectionItem } from "@/components/ui/select/selection-item";+import { SelectionPanel } from "@/components/ui/select/selection-panel";+import { SelectionSection } from "@/components/ui/select/selection-section";+import { useModelParamsStore } from "@/stores/use-model-params-store";+import { useModelsStore } from "@/stores/use-models-store";+import type { ReasoningEffort, ReasoningSupport } from "@/types/sampling";++const EFFORT_LABELS: Record<ReasoningEffort, string> = {+ default: "Thinking",+ disabled: "Off",+ low: "Low",+ medium: "Medium",+ high: "High",+ xhigh: "XHigh",+};++const EFFORT_ORDER = ["xhigh", "high", "medium", "low", "disabled"] as const;++const availableEfforts = (support: ReasoningSupport | undefined): ReasoningEffort[] => {+ switch (support?.kind) {+ case "toggle":+ return ["default", "disabled"];+ case "levels":+ return EFFORT_ORDER.filter((effort) => support.efforts.includes(effort));+ default:+ return [];+ }+};++type ReasoningSelectorProps = {+ repoId: string;+ disabled?: boolean;+};++export const ReasoningSelector = ({ repoId, disabled = false }: ReasoningSelectorProps) => {+ const support = useModelsStore((s) => s.models.find((model) => model.repoId === repoId)?.reasoning);+ const effort = useModelParamsStore((s) => s.paramsByRepoId[repoId]?.reasoningEffort ?? "default");+ const options = availableEfforts(support);+ if (options.length <= 1) return null;++ const defaultEffort = support?.kind === "toggle" || support?.kind === "levels" ? support.defaultEffort : undefined;+ const selected =+ effort !== "default" && options.includes(effort)+ ? effort+ : defaultEffort && options.includes(defaultEffort)+ ? defaultEffort+ : undefined;+ const label = selected ? EFFORT_LABELS[selected] : "Reasoning";+ const isDefault = selected !== undefined && selected === defaultEffort;+ const onChange = (nextEffort: ReasoningEffort) => {+ const { getParams, setParams } = useModelParamsStore.getState();+ const next = { ...getParams(repoId) };+ if (nextEffort === defaultEffort) delete next.reasoningEffort;+ else next.reasoningEffort = nextEffort;+ setParams(repoId, next);+ };++ return (+ <Menu as="div" className="relative shrink-0">+ <MenuButton+ type="button"+ disabled={disabled}+ aria-label={selected ? `Reasoning: ${label}${isDefault ? ", model default" : ""}` : "Reasoning"}+ title={selected ? `Reasoning: ${label}` : "Reasoning"}+ className="flex h-7 items-center gap-1.5 rounded-md bg-surface-tertiary px-2 text-text-muted text-[13px] font-[450] leading-[1.3] outline-hidden hover:bg-control-surface-hover hover:text-text-primary data-[headlessui-state~=open]:bg-control-surface-active data-[headlessui-state~=open]:text-text-primary data-[focus]:bg-control-surface-hover data-[focus]:text-text-primary data-[focus]:shadow-focus disabled:opacity-40 disabled:cursor-not-allowed disabled:hover:bg-surface-tertiary"+ >+ <Brain size={14} aria-hidden="true" />+ <span>{label}</span>+ <ChevronDown size={16} aria-hidden="true" />+ </MenuButton>+ <Transition+ as={Fragment}+ enter="transition-[opacity,scale] duration-[120ms] ease-spring"+ enterFrom="opacity-0 scale-90"+ enterTo="opacity-100 scale-100"+ leave="transition-[opacity,scale] duration-[80ms] ease-spring"+ leaveFrom="opacity-100 scale-100"+ leaveTo="opacity-0 scale-90"+ >+ <div className="absolute right-0 bottom-full pb-1 z-50 origin-bottom-right">+ <MenuItems aria-label="Reasoning" className="outline-hidden">+ <SelectionPanel className="min-w-[160px] pt-1.5">+ <SelectionSection>+ {options.map((option) => (+ <MenuItem key={option} disabled={disabled}>+ {({ focus }) => (+ <SelectionItem+ as="button"+ type="button"+ active={focus}+ selected={option === selected}+ aria-current={option === selected ? "true" : undefined}+ aria-label={`${EFFORT_LABELS[option]}${option === defaultEffort ? ", model default" : ""}`}+ onClick={() => onChange(option)}+ className="flex w-full items-center border-none bg-transparent text-left text-text-primary text-[13px] font-[450] leading-[1.3] outline-hidden"+ >+ {EFFORT_LABELS[option]}+ {option === defaultEffort && <DefaultMarker />}+ </SelectionItem>+ )}+ </MenuItem>+ ))}+ </SelectionSection>+ </SelectionPanel>+ </MenuItems>+ </div>+ </Transition>+ </Menu>+ );+};++const DefaultMarker = () => (+ <span+ title="Model default"+ aria-hidden="true"+ className="ml-auto pl-4 text-[11px] font-normal text-text-muted opacity-60"+ >+ default+ </span>+);diff --git a/apps/mirai-gui/client/src/features/chat/components/composer/model-params-controls.test.tsx b/apps/mirai-gui/client/src/features/chat/components/composer/model-params-controls.test.tsxnew file mode 100644index 000000000..9df2a7c43--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/components/composer/model-params-controls.test.tsx@@ -0,0 +1,445 @@+import { act, cleanup, fireEvent, render, screen, within } from "@testing-library/react";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { useModelParamsStore } from "@/stores/use-model-params-store";+import { useModelsStore } from "@/stores/use-models-store";+import { useRuntimeSessionStore } from "@/stores/use-runtime-session-store";+import { ModelKind, type PlatformModel } from "@/types/models";+import { ModelParamsControls } from "./model-params-controls";+import { ModelParamsDrawer } from "./model-params-drawer";+import type { SamplingDefaults } from "@/platform/services/chat";++const mocks = vi.hoisted(() => ({+ setModelParams: vi.fn(async () => {}),+ getSamplingDefaults: vi.fn<() => Promise<SamplingDefaults | null>>(),+}));+vi.mock("@/platform/platform-singleton", () => ({ getPlatform: () => ({ settings: mocks, chat: mocks }) }));+const samplingDefaults = {+ type: "Stochastic",+ temperature: 0.6,+ topK: null,+ topP: null,+ minP: 0.05,+ repetitionPenalty: 1.1,+ suffixRepetitionLength: 64,+} as const;+const paramsDefaults = useModelParamsStore.getState();+const modelsDefaults = useModelsStore.getState();+const runtimeDefaults = useRuntimeSessionStore.getState();+const model: PlatformModel = {+ repoId: "model",+ name: "Model",+ vendor: "Vendor",+ kind: ModelKind.Text,+ reasoning: { kind: "toggle", defaultEffort: "default" },+ supportsTools: true,+ paramSize: 2_000_000_000,+};+const parameterRow = (name: string) => within(screen.getByRole("textbox", { name }).parentElement!.parentElement!);++beforeEach(() => {+ vi.useFakeTimers();+ vi.clearAllMocks();+ mocks.getSamplingDefaults.mockResolvedValue(samplingDefaults);+ useModelParamsStore.setState(paramsDefaults, true);+ useModelsStore.setState({ ...modelsDefaults, models: [model] }, true);+ useRuntimeSessionStore.setState(runtimeDefaults, true);+});++it("displays the model's resolved defaults without creating a sampling override", async () => {+ render(<ModelParamsControls repoId="model" samplingDefaults={samplingDefaults} />);+ await act(async () => {});+ const options = screen.getAllByRole("radio");+ expect(options.map((option) => option.getAttribute("aria-label"))).toEqual(["Stochastic", "Greedy"]);+ expect(options[0]!.getAttribute("aria-checked")).toBe("true");+ expect(screen.queryByRole("radio", { name: "Default" })).toBeNull();+ expect((screen.getByRole("textbox", { name: "Temperature" }) as HTMLInputElement).value).toBe("0.6");+ expect((screen.getByRole("textbox", { name: "Top K" }) as HTMLInputElement).disabled).toBe(true);+ expect((screen.getByRole("textbox", { name: "Top P" }) as HTMLInputElement).disabled).toBe(true);+ expect(screen.queryByTitle("Changed from default")).toBeNull();+ expect(screen.queryByRole("button", { name: "Reset sampling to defaults" })).toBeNull();+ expect(screen.queryByRole("button", { name: "Reset tools to defaults" })).toBeNull();+ expect(screen.getAllByRole("switch").map((element) => element.getAttribute("aria-label"))).toEqual([+ "Current date and time",+ "Draw charts",+ "Name chat using a tool",+ ]);+ expect(screen.getByRole("switch", { name: "Draw charts" }).getAttribute("aria-checked")).toBe("true");+ expect(useModelParamsStore.getState().paramsByRepoId).toEqual({});+});++it("preserves disabled filters and repetition settings when editing a resolved default", async () => {+ render(<ModelParamsControls repoId="model" samplingDefaults={samplingDefaults} />);+ await act(async () => {});+ fireEvent.change(screen.getByRole("textbox", { name: "Temperature" }), { target: { value: "0.8" } });+ expect(useModelParamsStore.getState().getParams("model").sampling).toEqual({ ...samplingDefaults, temperature: 0.8 });+ expect(parameterRow("Temperature").getByTitle("Changed from default").getAttribute("aria-hidden")).toBe("true");+ expect(screen.getByRole("button", { name: "Reset sampling to defaults" })).toBeTruthy();+ expect(within(screen.getByRole("radio", { name: "Stochastic" })).queryByTitle("Changed from default")).toBeNull();+ fireEvent.change(screen.getByRole("textbox", { name: "Temperature" }), { target: { value: "0.6" } });+ expect(useModelParamsStore.getState().getParams("model").sampling).toEqual({ type: "Default" });+ expect(screen.queryByTitle("Changed from default")).toBeNull();+ expect(screen.queryByRole("button", { name: "Reset sampling to defaults" })).toBeNull();++ fireEvent.click(screen.getByRole("checkbox", { name: "Enable Top K" }));+ expect(useModelParamsStore.getState().getParams("model").sampling).toEqual({ ...samplingDefaults, topK: 40 });+ expect(parameterRow("Top K").getByTitle("Changed from default")).toBeTruthy();+ fireEvent.click(screen.getByRole("checkbox", { name: "Enable Top K" }));+ expect(useModelParamsStore.getState().getParams("model").sampling).toEqual({ type: "Default" });+ expect(screen.queryByTitle("Changed from default")).toBeNull();+ expect(screen.queryByRole("button", { name: "Reset sampling to defaults" })).toBeNull();+});++it("marks changes to enabled parameters and disabled filters until sampling is reset", async () => {+ render(<ModelParamsControls repoId="model" samplingDefaults={samplingDefaults} />);+ await act(async () => {});++ fireEvent.click(screen.getByRole("checkbox", { name: "Enable Top P" }));+ fireEvent.click(screen.getByRole("checkbox", { name: "Enable Min P" }));+ fireEvent.change(screen.getByRole("textbox", { name: "Repetition penalty" }), { target: { value: "1.2" } });+ fireEvent.change(screen.getByRole("textbox", { name: "Suffix repetition length" }), { target: { value: "96" } });+ for (const label of ["Top P", "Min P", "Repetition penalty", "Suffix repetition length"]) {+ expect(parameterRow(label).getByTitle("Changed from default")).toBeTruthy();+ }+ expect(screen.getAllByTitle("Changed from default")).toHaveLength(4);+ expect((screen.getByRole("textbox", { name: "Min P" }) as HTMLInputElement).disabled).toBe(true);++ fireEvent.click(screen.getByRole("button", { name: "Reset sampling to defaults" }));+ expect(useModelParamsStore.getState().getParams("model").sampling).toEqual({ type: "Default" });+ expect(screen.queryByTitle("Changed from default")).toBeNull();+ expect((screen.getByRole("textbox", { name: "Min P" }) as HTMLInputElement).disabled).toBe(false);+ expect((screen.getByRole("textbox", { name: "Suffix repetition length" }) as HTMLInputElement).value).toBe("64");+});++it("does not mark explicit matching defaults or distinguish null from omitted disabled filters", async () => {+ useModelParamsStore.setState({+ paramsByRepoId: {+ model: {+ sampling: { ...samplingDefaults, temperature: 1, topK: undefined, topP: undefined },+ modelChatNamingEnabled: true,+ dateTimeToolEnabled: true,+ chartToolEnabled: true,+ },+ },+ });+ render(<ModelParamsControls repoId="model" samplingDefaults={{ ...samplingDefaults, temperature: null }} />);+ await act(async () => {});+ expect(screen.queryByTitle("Changed from default")).toBeNull();+ expect(screen.queryByRole("button", { name: "Reset sampling to defaults" })).toBeNull();+ expect(screen.queryByRole("button", { name: "Reset tools to defaults" })).toBeNull();+});++it("waits for defaults before switching from greedy to stochastic", async () => {+ let resolveDefaults!: (defaults: SamplingDefaults) => void;+ mocks.getSamplingDefaults.mockReturnValue(new Promise((resolve) => (resolveDefaults = resolve)));+ useModelParamsStore.setState({ paramsByRepoId: { model: { sampling: { type: "Greedy" } } } });+ render(<ModelParamsDrawer open chatId="chat" repoId="model" onClose={() => {}} />);+ expect(screen.getByText("Loading model sampling settings…")).toBeTruthy();+ expect(screen.queryByRole("radio", { name: "Stochastic" })).toBeNull();+ expect(useModelParamsStore.getState().getParams("model").sampling).toEqual({ type: "Greedy" });++ await act(async () => resolveDefaults(samplingDefaults));+ const stochastic = screen.getByRole("radio", { name: "Stochastic" }) as HTMLButtonElement;+ expect(stochastic.disabled).toBe(false);+ expect(within(screen.getByRole("radio", { name: "Greedy" })).getByTitle("Changed from default")).toBeTruthy();+ fireEvent.click(stochastic);+ expect(useModelParamsStore.getState().getParams("model").sampling).toEqual({ type: "Default" });+ expect((screen.getByRole("textbox", { name: "Temperature" }) as HTMLInputElement).value).toBe("0.6");+ expect(screen.queryByTitle("Changed from default")).toBeNull();+ expect(screen.queryByRole("button", { name: "Reset sampling to defaults" })).toBeNull();+});++it.each([null, 0.01, 1.5])(+ "shows the actual model temperature %s without clamping or inventing greedy sampling",+ async (temperature) => {+ render(<ModelParamsControls repoId="model" samplingDefaults={{ ...samplingDefaults, temperature }} />);+ await act(async () => {});+ const input = screen.getByRole("textbox", { name: "Temperature" }) as HTMLInputElement;+ expect(input.value).toBe(String(temperature ?? 1));+ expect(screen.getByRole("radio", { name: "Stochastic" }).getAttribute("aria-checked")).toBe("true");+ fireEvent.blur(input);+ expect(useModelParamsStore.getState().paramsByRepoId).toEqual({});+ },+);++it("shows no invented default while metadata is unavailable", async () => {+ render(<ModelParamsControls repoId="model" samplingDefaults={null} />);+ await act(async () => {});+ expect(screen.getByText("Model sampling settings are unavailable.")).toBeTruthy();+ expect(screen.getAllByRole("radio").every((option) => option.getAttribute("aria-checked") === "false")).toBe(true);+ expect(screen.queryByRole("textbox", { name: "Temperature" })).toBeNull();+ fireEvent.click(screen.getByRole("radio", { name: "Greedy" }));+ expect(useModelParamsStore.getState().getParams("model").sampling).toEqual({ type: "Greedy" });+ expect(screen.queryByTitle("Changed from default")).toBeNull();+ fireEvent.click(screen.getByRole("button", { name: "Reset sampling to defaults" }));+ expect(useModelParamsStore.getState().getParams("model").sampling).toEqual({ type: "Default" });+ expect(screen.queryByRole("button", { name: "Reset sampling to defaults" })).toBeNull();+});++it("allows resetting explicit parameters with unknown defaults without inventing per-value markers", async () => {+ useModelParamsStore.setState({+ paramsByRepoId: { model: { sampling: { type: "Stochastic", temperature: 0.8, topK: 20 } } },+ });+ render(<ModelParamsControls repoId="model" samplingDefaults={null} />);+ await act(async () => {});+ expect((screen.getByRole("textbox", { name: "Temperature" }) as HTMLInputElement).value).toBe("0.8");+ expect(screen.queryByTitle("Changed from default")).toBeNull();+ fireEvent.click(screen.getByRole("button", { name: "Reset sampling to defaults" }));+ expect(useModelParamsStore.getState().getParams("model").sampling).toEqual({ type: "Default" });+ expect(screen.getByText("Model sampling settings are unavailable.")).toBeTruthy();+ expect(screen.queryByRole("button", { name: "Reset sampling to defaults" })).toBeNull();+});+afterEach(async () => {+ cleanup();+ await vi.runAllTimersAsync();+ vi.useRealTimers();+});++it("hides chat naming while globally disabled and restores a model's opt-out when re-enabled", async () => {+ useModelParamsStore.setState({ globalModelChatNamingEnabled: false });+ render(<ModelParamsControls repoId="model" samplingDefaults={samplingDefaults} />);+ await act(async () => {});+ expect(screen.queryByRole("switch", { name: "Name chat using a tool" })).toBeNull();+ expect(screen.queryByText("Name chat")).toBeNull();+ expect(screen.getByRole("switch", { name: "Current date and time" }).getAttribute("aria-checked")).toBe("true");+ expect(screen.queryByRole("button", { name: "Reset tools to defaults" })).toBeNull();++ act(() => useModelParamsStore.getState().setGlobalModelChatNamingEnabled(true));+ const naming = screen.getByRole("switch", { name: "Name chat using a tool" });+ expect(naming.getAttribute("aria-checked")).toBe("true");+ fireEvent.click(naming);+ expect(useModelParamsStore.getState().getParams("model").modelChatNamingEnabled).toBe(false);+ expect(within(screen.getByText("Name chat")).getByTitle("Changed from default")).toBeTruthy();+ expect(screen.getByRole("button", { name: "Reset tools to defaults" })).toBeTruthy();++ act(() => useModelParamsStore.getState().setGlobalModelChatNamingEnabled(false));+ expect(screen.queryByRole("switch", { name: "Name chat using a tool" })).toBeNull();+ expect(screen.queryByRole("button", { name: "Reset tools to defaults" })).toBeNull();+ expect(useModelParamsStore.getState().getParams("model").modelChatNamingEnabled).toBe(false);++ act(() => useModelParamsStore.getState().setGlobalModelChatNamingEnabled(true));+ expect(screen.getByRole("switch", { name: "Name chat using a tool" }).getAttribute("aria-checked")).toBe("false");+ expect(within(screen.getByText("Name chat")).getByTitle("Changed from default")).toBeTruthy();++ fireEvent.click(screen.getByRole("button", { name: "Reset tools to defaults" }));+ expect(useModelParamsStore.getState().getParams("model").modelChatNamingEnabled).toBeUndefined();+ expect(screen.getByRole("switch", { name: "Name chat using a tool" }).getAttribute("aria-checked")).toBe("true");+ expect(screen.queryByRole("button", { name: "Reset tools to defaults" })).toBeNull();+});++it("resetting visible tools leaves a hidden naming opt-out unchanged", async () => {+ useModelParamsStore.setState({+ globalModelChatNamingEnabled: false,+ paramsByRepoId: {+ model: {+ sampling: { type: "Default" },+ modelChatNamingEnabled: false,+ dateTimeToolEnabled: false,+ chartToolEnabled: false,+ },+ },+ });+ render(<ModelParamsControls repoId="model" samplingDefaults={samplingDefaults} />);+ await act(async () => {});+ expect(screen.queryByText("Name chat")).toBeNull();+ fireEvent.click(screen.getByRole("button", { name: "Reset tools to defaults" }));+ expect(useModelParamsStore.getState().getParams("model")).toEqual({+ sampling: { type: "Default" },+ modelChatNamingEnabled: false,+ });+ expect(screen.queryByRole("button", { name: "Reset tools to defaults" })).toBeNull();+});++it("marks tool overrides until each switch returns to its default", async () => {+ useModelParamsStore.setState({+ paramsByRepoId: { model: { sampling: { type: "Default" }, reasoningEffort: "disabled" } },+ });+ render(<ModelParamsControls repoId="model" samplingDefaults={samplingDefaults} />);+ await act(async () => {});+ expect(screen.queryByRole("switch", { name: "Reasoning" })).toBeNull();+ fireEvent.click(screen.getByRole("switch", { name: "Current date and time" }));+ fireEvent.click(screen.getByRole("switch", { name: "Name chat using a tool" }));+ fireEvent.click(screen.getByRole("radio", { name: "Greedy" }));+ expect(useModelParamsStore.getState().getParams("model")).toEqual({+ sampling: { type: "Greedy" },+ reasoningEffort: "disabled",+ modelChatNamingEnabled: false,+ dateTimeToolEnabled: false,+ });+ expect(within(screen.getByText("Current date and time")).getByTitle("Changed from default")).toBeTruthy();+ expect(within(screen.getByText("Name chat")).getByTitle("Changed from default")).toBeTruthy();+ fireEvent.click(screen.getByRole("switch", { name: "Current date and time" }));+ expect(within(screen.getByText("Current date and time")).queryByTitle("Changed from default")).toBeNull();+ expect(screen.getByRole("button", { name: "Reset tools to defaults" })).toBeTruthy();+ fireEvent.click(screen.getByRole("switch", { name: "Name chat using a tool" }));+ expect(within(screen.getByText("Name chat")).queryByTitle("Changed from default")).toBeNull();+ expect(screen.queryByRole("button", { name: "Reset tools to defaults" })).toBeNull();+ expect(useModelParamsStore.getState().getParams("model")).toEqual({+ sampling: { type: "Greedy" },+ reasoningEffort: "disabled",+ });+ expect(screen.getByRole("switch", { name: "Name chat using a tool" }).getAttribute("aria-checked")).toBe("true");+ expect(screen.getByRole("switch", { name: "Current date and time" }).getAttribute("aria-checked")).toBe("true");+});++it("disables tools for a model without tool support", () => {+ useModelsStore.setState({ models: [{ ...model, supportsTools: false }] });+ render(<ModelParamsControls repoId="model" samplingDefaults={samplingDefaults} />);+ expect(screen.getByText("This model does not support tool calls.")).toBeTruthy();+ expect((screen.getByRole("switch", { name: "Name chat using a tool" }) as HTMLButtonElement).disabled).toBe(true);+ expect((screen.getByRole("switch", { name: "Current date and time" }) as HTMLButtonElement).disabled).toBe(true);+ expect((screen.getByRole("switch", { name: "Draw charts" }) as HTMLButtonElement).disabled).toBe(true);+ expect(screen.getByRole("switch", { name: "Draw charts" }).getAttribute("aria-checked")).toBe("false");+});++it("defaults all tools off below 2B while allowing per-model opt-ins and reset", () => {+ useModelsStore.setState({ models: [{ ...model, paramSize: 1_999_999_999 }] });+ render(<ModelParamsControls repoId="model" samplingDefaults={samplingDefaults} />);+ expect(screen.queryByRole("button", { name: "Reset tools to defaults" })).toBeNull();+ const labels = ["Current date and time", "Draw charts", "Name chat using a tool"];+ for (const name of labels) {+ const toggle = screen.getByRole("switch", { name }) as HTMLButtonElement;+ expect(toggle.disabled).toBe(false);+ expect(toggle.getAttribute("aria-checked")).toBe("false");+ fireEvent.click(toggle);+ expect(toggle.getAttribute("aria-checked")).toBe("true");+ }+ expect(useModelParamsStore.getState().getParams("model")).toEqual({+ sampling: { type: "Default" },+ dateTimeToolEnabled: true,+ chartToolEnabled: true,+ modelChatNamingEnabled: true,+ });+ fireEvent.click(screen.getByRole("button", { name: "Reset tools to defaults" }));+ expect(useModelParamsStore.getState().getParams("model")).toEqual({ sampling: { type: "Default" } });+ for (const name of labels) expect(screen.getByRole("switch", { name }).getAttribute("aria-checked")).toBe("false");+ expect(screen.queryByRole("button", { name: "Reset tools to defaults" })).toBeNull();+});++it("keeps chart opt-outs per model and resets them to the inherited enabled default", () => {+ useModelsStore.setState({ models: [model, { ...model, repoId: "other" }] });+ const { rerender } = render(<ModelParamsControls repoId="model" samplingDefaults={samplingDefaults} />);+ fireEvent.click(screen.getByRole("switch", { name: "Draw charts" }));+ expect(useModelParamsStore.getState().getParams("model").chartToolEnabled).toBe(false);+ expect(within(screen.getByText("Draw charts")).getByTitle("Changed from default")).toBeTruthy();++ rerender(<ModelParamsControls repoId="other" samplingDefaults={samplingDefaults} />);+ expect(screen.getByRole("switch", { name: "Draw charts" }).getAttribute("aria-checked")).toBe("true");+ expect(screen.queryByRole("button", { name: "Reset tools to defaults" })).toBeNull();++ rerender(<ModelParamsControls repoId="model" samplingDefaults={samplingDefaults} />);+ expect(screen.getByRole("switch", { name: "Draw charts" }).getAttribute("aria-checked")).toBe("false");+ fireEvent.click(screen.getByRole("button", { name: "Reset tools to defaults" }));+ expect(useModelParamsStore.getState().getParams("model").chartToolEnabled).toBeUndefined();+ expect(screen.getByRole("switch", { name: "Draw charts" }).getAttribute("aria-checked")).toBe("true");+ expect(within(screen.getByText("Draw charts")).queryByTitle("Changed from default")).toBeNull();+});++it("prefetches defaults while closed and opens ready controls without refetching or expanding them", async () => {+ const { rerender } = render(<ModelParamsDrawer open={false} chatId="chat" repoId="model" onClose={() => {}} />);+ await act(async () => {});+ expect(mocks.getSamplingDefaults).toHaveBeenCalledExactlyOnceWith("model");+ expect(screen.queryByRole("textbox", { name: "Temperature" })).toBeNull();++ rerender(<ModelParamsDrawer open chatId="chat" repoId="model" onClose={() => {}} />);+ const temperature = screen.getByRole("textbox", { name: "Temperature" }) as HTMLInputElement;+ expect(temperature.value).toBe("0.6");+ const panel = temperature.closest<HTMLElement>("[style]")!;+ expect(panel.style.height).toBe("auto");+ expect(panel.style.opacity).toBe("1");+ expect(screen.queryByText("Loading model sampling settings…")).toBeNull();+ expect(mocks.getSamplingDefaults).toHaveBeenCalledTimes(1);+ expect(useModelParamsStore.getState().paramsByRepoId).toEqual({});+});++it("mounts controls at their final height when defaults arrive after the drawer opens", async () => {+ let resolveDefaults!: (defaults: SamplingDefaults) => void;+ mocks.getSamplingDefaults.mockReturnValue(new Promise((resolve) => (resolveDefaults = resolve)));+ render(<ModelParamsDrawer open chatId="chat" repoId="model" onClose={() => {}} />);+ expect(screen.getByText("Loading model sampling settings…")).toBeTruthy();+ expect(screen.queryByRole("textbox", { name: "Temperature" })).toBeNull();++ await act(async () => resolveDefaults(samplingDefaults));+ const temperature = screen.getByRole("textbox", { name: "Temperature" }) as HTMLInputElement;+ expect(temperature.value).toBe("0.6");+ const panel = temperature.closest<HTMLElement>("[style]")!;+ expect(panel.style.height).toBe("auto");+ expect(panel.style.opacity).toBe("1");+ expect(screen.queryByText("Loading model sampling settings…")).toBeNull();+});++it("retries unavailable prefetched defaults on opening and mounts recovered controls without expanding", async () => {+ let resolveRetry!: (defaults: SamplingDefaults) => void;+ mocks.getSamplingDefaults+ .mockResolvedValueOnce(null)+ .mockReturnValueOnce(new Promise((resolve) => (resolveRetry = resolve)));+ const { rerender } = render(<ModelParamsDrawer open={false} chatId="chat" repoId="model" onClose={() => {}} />);+ await act(async () => {});+ expect(mocks.getSamplingDefaults).toHaveBeenCalledTimes(1);++ rerender(<ModelParamsDrawer open chatId="chat" repoId="model" onClose={() => {}} />);+ expect(mocks.getSamplingDefaults).toHaveBeenCalledTimes(2);+ await act(async () => resolveRetry(samplingDefaults));+ const temperature = screen.getByRole("textbox", { name: "Temperature" }) as HTMLInputElement;+ expect(temperature.value).toBe("0.6");+ const panel = temperature.closest<HTMLElement>("[style]")!;+ expect(panel.style.height).toBe("auto");+ expect(panel.style.opacity).toBe("1");+ expect(mocks.getSamplingDefaults).toHaveBeenCalledTimes(2);+});++it("ignores stale defaults when the selected model changes during a request", async () => {+ let resolveOldDefaults!: (defaults: SamplingDefaults) => void;+ let resolveNewDefaults!: (defaults: SamplingDefaults) => void;+ mocks.getSamplingDefaults+ .mockReturnValueOnce(new Promise((resolve) => (resolveOldDefaults = resolve)))+ .mockReturnValueOnce(new Promise((resolve) => (resolveNewDefaults = resolve)));+ const { rerender } = render(<ModelParamsDrawer open chatId="chat" repoId="model" onClose={() => {}} />);+ rerender(<ModelParamsDrawer open chatId="chat" repoId="other-model" onClose={() => {}} />);+ await act(async () => resolveOldDefaults(samplingDefaults));+ expect(screen.getByText("Loading model sampling settings…")).toBeTruthy();+ expect(screen.queryByRole("textbox", { name: "Temperature" })).toBeNull();++ await act(async () => resolveNewDefaults({ type: "Greedy" }));+ expect(screen.getByRole("radio", { name: "Greedy" }).getAttribute("aria-checked")).toBe("true");+ expect(screen.queryByRole("textbox", { name: "Temperature" })).toBeNull();+ expect(mocks.getSamplingDefaults.mock.calls).toEqual([["model"], ["other-model"]]);+});++it("resets sampling and tools independently while preserving the composer reasoning choice", async () => {+ useModelParamsStore.setState({+ paramsByRepoId: {+ model: {+ sampling: { type: "Greedy" },+ reasoningEffort: "disabled",+ dateTimeToolEnabled: false,+ chartToolEnabled: false,+ modelChatNamingEnabled: false,+ },+ },+ });+ render(<ModelParamsDrawer open chatId="chat" repoId="model" onClose={() => {}} />);+ await act(async () => {});+ expect(screen.queryByRole("button", { name: "Reset to defaults" })).toBeNull();+ fireEvent.click(screen.getByRole("button", { name: "Reset sampling to defaults" }));+ expect(useModelParamsStore.getState().getParams("model")).toEqual({+ sampling: { type: "Default" },+ reasoningEffort: "disabled",+ dateTimeToolEnabled: false,+ chartToolEnabled: false,+ modelChatNamingEnabled: false,+ });+ expect(screen.queryByRole("button", { name: "Reset sampling to defaults" })).toBeNull();+ expect(screen.getByRole("button", { name: "Reset tools to defaults" })).toBeTruthy();++ fireEvent.change(screen.getByRole("textbox", { name: "Temperature" }), { target: { value: "0.8" } });+ fireEvent.click(screen.getByRole("button", { name: "Reset tools to defaults" }));+ expect(useModelParamsStore.getState().getParams("model")).toEqual({+ sampling: { ...samplingDefaults, temperature: 0.8 },+ reasoningEffort: "disabled",+ });+ expect(screen.queryByRole("button", { name: "Reset tools to defaults" })).toBeNull();+ expect(screen.getByRole("button", { name: "Reset sampling to defaults" })).toBeTruthy();+ expect(screen.getByRole("switch", { name: "Name chat using a tool" }).getAttribute("aria-checked")).toBe("true");+ expect(screen.getByRole("switch", { name: "Current date and time" }).getAttribute("aria-checked")).toBe("true");+ expect(screen.getByRole("switch", { name: "Draw charts" }).getAttribute("aria-checked")).toBe("true");+});diff --git a/apps/mirai-gui/client/src/features/chat/components/composer/model-params-controls.tsx b/apps/mirai-gui/client/src/features/chat/components/composer/model-params-controls.tsxindex 5bf81e10e..3719ad540 100644--- a/apps/mirai-gui/client/src/features/chat/components/composer/model-params-controls.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/composer/model-params-controls.tsx@@ -1,20 +1,14 @@-import { getPlatform } from "@/platform/platform-singleton";import type { SamplingDefaults } from "@/platform/services/chat";-import { STOCHASTIC_SEED, defaultReasoningEffort, useModelParamsStore } from "@/stores/use-model-params-store";+import { resolveModelTools, useModelParamsStore } from "@/stores/use-model-params-store";import { useModelsStore } from "@/stores/use-models-store";-import { useRuntimeSessionStore } from "@/stores/use-runtime-session-store";-import {- TEMPERATURE_MIN,- type ReasoningEffort,- type ReasoningSupport,- type SamplingPolicyPayload,-} from "@/types/sampling";+import { TEMPERATURE_MIN, type SamplingPolicyPayload } from "@/types/sampling";import { Checkbox } from "@/components/ui/checkbox";import { Input } from "@/components/ui/input";import { SegmentedControl } from "@/components/ui/segmented-control";import { AnimatePresence, motion } from "motion/react";import { useEffect, useState, useId } from "react";import { Toggle } from "@/components/ui/toggle";+import { RotateCcw } from "lucide-react";const TOP_K_MIN = 1;const TOP_K_MAX = 4096;@@ -28,71 +22,95 @@ const SUFFIX_REPETITION_LENGTH_SEED = 32;type ModelParamsControlsProps = {repoId: string;+ samplingDefaults: SamplingDefaults | null;};const MODE_OPTIONS = [- { value: "Default", label: "Default" },- { value: "Argmax", label: "Argmax" },{ value: "Stochastic", label: "Stochastic" },+ { value: "Greedy", label: "Greedy" },] as const;-const EFFORT_LABELS: Record<ReasoningEffort, string> = {- disabled: "Off",- default: "Default",- low: "Low",- medium: "Medium",- high: "High",- xhigh: "XHigh",-};+type StochasticSampling = Extract<SamplingDefaults, { type: "Stochastic" }>;+type SamplingField = Exclude<keyof StochasticSampling, "type">;++const samplingValue = (sampling: StochasticSampling, field: SamplingField) =>+ sampling[field] ?? (field === "temperature" ? 1 : null);++const sameSampling = (left: SamplingDefaults, right: SamplingDefaults) =>+ left.type === right.type &&+ (left.type === "Greedy" ||+ (right.type === "Stochastic" &&+ (["temperature", "topK", "topP", "minP", "repetitionPenalty", "suffixRepetitionLength"] as const).every(+ (field) => samplingValue(left, field) === samplingValue(right, field),+ )));-const sectionLabel = "text-[12px] font-[450] text-label-muted";+const ChangedMarker = () => (+ <span aria-hidden="true" title="Changed from default" className="ml-0.5 text-red-500">+ *+ </span>+);-export const ModelParamsControls = ({ repoId }: ModelParamsControlsProps) => {+const SectionHeading = ({ name, changed, onReset }: { name: string; changed: boolean; onReset: () => void }) => (+ <div className="flex h-5 items-center gap-1">+ <span className="text-[12px] font-[450] text-label-muted">{name}</span>+ {changed && (+ <button+ type="button"+ onClick={onReset}+ aria-label={`Reset ${name.toLowerCase()} to defaults`}+ title={`Reset ${name.toLowerCase()} to defaults`}+ className="inline-flex size-5 items-center justify-center rounded text-red-500 transition-colors hover:bg-red-500/10 outline-hidden focus-visible:shadow-focus"+ >+ <RotateCcw aria-hidden="true" className="size-3" />+ </button>+ )}+ </div>+);++export const ModelParamsControls = ({ repoId, samplingDefaults: modelDefaults }: ModelParamsControlsProps) => {const model = useModelsStore((s) => s.models.find((m) => m.repoId === repoId));const params = useModelParamsStore((s) => s.paramsByRepoId[repoId] ?? null);- const globalReasoningEnabled = useModelParamsStore((s) => s.globalReasoningEnabled);+ const globalModelChatNamingEnabled = useModelParamsStore((s) => s.globalModelChatNamingEnabled);const setParams = useModelParamsStore((s) => s.setParams);const sampling: SamplingPolicyPayload = params?.sampling ?? { type: "Default" };- const reasoningEffort = params?.reasoningEffort ?? defaultReasoningEffort(globalReasoningEnabled);- const reasoning: ReasoningSupport = model?.reasoning ?? { kind: "unsupported" };- const stochastic = sampling.type === "Stochastic" ? sampling : null;+ const tools = resolveModelTools(params ?? undefined, globalModelChatNamingEnabled, model?.paramSize);+ const defaultTools = resolveModelTools(undefined, globalModelChatNamingEnabled, model?.paramSize);+ const supportsTools = model?.supportsTools === true;- const isResident = useRuntimeSessionStore((s) => s.residentSession?.repoId === repoId);- const [modelDefaults, setModelDefaults] = useState<SamplingDefaults | null>(null);- useEffect(() => setModelDefaults(null), [repoId]);- useEffect(() => {- if (!isResident) return;- let alive = true;- void getPlatform()- .chat.getSamplingDefaults(repoId)- .then((defaults) => {- if (alive) setModelDefaults(defaults);- });- return () => {- alive = false;- };- }, [repoId, isResident]);+ const resolvedSampling = sampling.type === "Default" ? modelDefaults : sampling;+ const stochastic = resolvedSampling?.type === "Stochastic" ? resolvedSampling : null;+ const defaultStochastic = modelDefaults?.type === "Stochastic" ? modelDefaults : null;+ const samplingChanged = sampling.type !== "Default" && (!modelDefaults || !sameSampling(sampling, modelDefaults));+ const modeChanged = modelDefaults !== null && resolvedSampling?.type !== modelDefaults.type;+ const fieldChanged = (field: SamplingField) =>+ stochastic !== null &&+ defaultStochastic !== null &&+ samplingValue(stochastic, field) !== samplingValue(defaultStochastic, field);+ const dateTimeChanged = tools.dateTimeToolEnabled !== defaultTools.dateTimeToolEnabled;+ const chartChanged = tools.chartToolEnabled !== defaultTools.chartToolEnabled;+ const chatNamingChanged = tools.modelChatNamingEnabled !== defaultTools.modelChatNamingEnabled;+ // Seeds are only for explicitly enabling a filter; missing parameters stay disabled.const seed = {- temperature: Math.max(TEMPERATURE_MIN, modelDefaults?.temperature ?? STOCHASTIC_SEED.temperature),- topK: modelDefaults?.topK ?? STOCHASTIC_SEED.topK,- topP: modelDefaults?.topP ?? STOCHASTIC_SEED.topP,- minP: modelDefaults?.minP ?? STOCHASTIC_SEED.minP,- repetitionPenalty: modelDefaults?.repetitionPenalty ?? REPETITION_PENALTY_SEED,- suffixRepetitionLength: modelDefaults?.suffixRepetitionLength ?? SUFFIX_REPETITION_LENGTH_SEED,+ topK: defaultStochastic?.topK ?? 40,+ topP: defaultStochastic?.topP ?? 0.95,+ minP: defaultStochastic?.minP ?? 0.05,+ repetitionPenalty: defaultStochastic?.repetitionPenalty ?? REPETITION_PENALTY_SEED,+ suffixRepetitionLength: defaultStochastic?.suffixRepetitionLength ?? SUFFIX_REPETITION_LENGTH_SEED,};- const update = (next: SamplingPolicyPayload) => {- setParams(repoId, { ...params, sampling: next });+ const update = (next: SamplingDefaults) => {+ setParams(repoId, {+ ...params,+ sampling: modelDefaults && sameSampling(next, modelDefaults) ? { type: "Default" } : next,+ });};const onModeChange = (value: string) => {if (value === "Stochastic") {- update({ type: "Stochastic", temperature: seed.temperature, topK: seed.topK, topP: seed.topP, minP: seed.minP });- } else if (value === "Argmax") {- update({ type: "Argmax" });- } else {- update({ type: "Default" });+ update(defaultStochastic ?? { type: "Stochastic" });+ } else if (value === "Greedy") {+ update({ type: "Greedy" });}};@@ -107,19 +125,10 @@ export const ModelParamsControls = ({ repoId }: ModelParamsControlsProps) => {}>,) => {const base = stochastic ?? { type: "Stochastic" as const };- update({- type: "Stochastic",- temperature: base.temperature ?? seed.temperature,- topK: base.topK ?? seed.topK,- ...(base.topP !== undefined ? { topP: base.topP } : {}),- ...(base.minP !== undefined ? { minP: base.minP } : {}),- ...(base.repetitionPenalty !== undefined ? { repetitionPenalty: base.repetitionPenalty } : {}),- ...(base.suffixRepetitionLength !== undefined ? { suffixRepetitionLength: base.suffixRepetitionLength } : {}),- ...patch,- });+ update({ ...base, ...patch });};- const onToggleOptional = (field: "topP" | "minP" | "repetitionPenalty", on: boolean) => {+ const onToggleOptional = (field: "topK" | "topP" | "minP" | "repetitionPenalty", on: boolean) => {if (on) {const patch =field === "repetitionPenalty"@@ -134,28 +143,45 @@ export const ModelParamsControls = ({ repoId }: ModelParamsControlsProps) => {}};- const onReasoningChange = (effort: ReasoningEffort) => {- const override = effort === defaultReasoningEffort(globalReasoningEnabled) ? undefined : effort;- setParams(repoId, { sampling, ...(override ? { reasoningEffort: override } : {}) });+ const onToolChange = (+ field: "modelChatNamingEnabled" | "dateTimeToolEnabled" | "chartToolEnabled",+ enabled: boolean,+ ) => {+ const next = { ...params, sampling };+ if (enabled === defaultTools[field]) delete next[field];+ else next[field] = enabled;+ setParams(repoId, next);};- const levelOptions =- reasoning.kind === "levels"- ? reasoning.efforts.map((effort) => ({ value: effort, label: EFFORT_LABELS[effort] }))- : [];- const levelValue =- reasoning.kind === "levels"- ? reasoning.efforts.includes(reasoningEffort)- ? reasoningEffort- : (reasoning.efforts.find((effort) => effort === "default") ??- reasoning.efforts.find((effort) => effort !== "disabled"))- : undefined;+ const resetSampling = () => setParams(repoId, { ...params, sampling: { type: "Default" } });+ const resetTools = () => {+ const next = { ...params, sampling };+ delete next.dateTimeToolEnabled;+ delete next.chartToolEnabled;+ if (globalModelChatNamingEnabled) delete next.modelChatNamingEnabled;+ setParams(repoId, next);+ };return (<div className="flex flex-col gap-3"><div className="flex flex-col gap-1.5">- <span className={sectionLabel}>Sampling</span>- <SegmentedControl ariaLabel="Sampling" value={sampling.type} onChange={onModeChange} options={MODE_OPTIONS} />+ <SectionHeading name="Sampling" changed={samplingChanged} onReset={resetSampling} />+ <SegmentedControl+ ariaLabel="Sampling"+ value={resolvedSampling?.type ?? ""}+ onChange={onModeChange}+ options={MODE_OPTIONS.map((option) => ({+ ...option,+ ariaLabel: option.label,+ label: (+ <span>+ {option.label}+ {modeChanged && option.value === resolvedSampling?.type && <ChangedMarker />}+ </span>+ ),+ }))}+ />+ {!resolvedSampling && <p className="text-[12px] text-label-muted">Model sampling settings are unavailable.</p>}</div><AnimatePresence initial={false}>@@ -166,66 +192,80 @@ export const ModelParamsControls = ({ repoId }: ModelParamsControlsProps) => {animate={{ height: "auto", opacity: 1 }}exit={{ height: 0, opacity: 0 }}transition={{ duration: 0.2, ease: "easeInOut" }}- className="overflow-hidden"+ className="overflow-clip"><div className="flex flex-col gap-3 pt-3"><NumberSliderRowlabel="Temperature"- min={TEMPERATURE_MIN}- max={1}+ changed={fieldChanged("temperature")}+ min={Math.min(TEMPERATURE_MIN, stochastic.temperature ?? 1)}+ max={Math.max(2, stochastic.temperature ?? 1)}step={0.01}decimals={2}- value={stochastic.temperature ?? seed.temperature}+ value={stochastic.temperature ?? 1}onChange={(v) => onStochasticChange({ temperature: v })}/><NumberSliderRowlabel="Top K"+ changed={fieldChanged("topK")}min={TOP_K_MIN}- max={TOP_K_MAX}+ max={Math.max(TOP_K_MAX, stochastic.topK ?? 0)}step={1}decimals={0}noSlidervalue={stochastic.topK ?? seed.topK}+ enabled={stochastic.topK != null}+ onToggle={(on) => onToggleOptional("topK", on)}onChange={(v) => onStochasticChange({ topK: v })}/><NumberSliderRowlabel="Top P"+ changed={fieldChanged("topP")}min={0}max={1}step={0.01}decimals={2}value={stochastic.topP ?? seed.topP}- enabled={stochastic.topP !== undefined}+ enabled={stochastic.topP != null}onToggle={(on) => onToggleOptional("topP", on)}onChange={(v) => onStochasticChange({ topP: v })}/><NumberSliderRowlabel="Min P"+ changed={fieldChanged("minP")}min={0}max={1}step={0.01}decimals={2}value={stochastic.minP ?? seed.minP}- enabled={stochastic.minP !== undefined}+ enabled={stochastic.minP != null}onToggle={(on) => onToggleOptional("minP", on)}onChange={(v) => onStochasticChange({ minP: v })}/><NumberSliderRowlabel="Repetition penalty"- min={REPETITION_PENALTY_MIN}- max={REPETITION_PENALTY_MAX}+ changed={fieldChanged("repetitionPenalty")}+ min={Math.min(REPETITION_PENALTY_MIN, stochastic.repetitionPenalty ?? REPETITION_PENALTY_MIN)}+ max={Math.max(REPETITION_PENALTY_MAX, stochastic.repetitionPenalty ?? REPETITION_PENALTY_MAX)}step={0.05}decimals={2}value={stochastic.repetitionPenalty ?? seed.repetitionPenalty}- enabled={stochastic.repetitionPenalty !== undefined}+ enabled={stochastic.repetitionPenalty != null}onToggle={(on) => onToggleOptional("repetitionPenalty", on)}onChange={(v) => onStochasticChange({ repetitionPenalty: v })}/>- {stochastic.repetitionPenalty !== undefined && (+ {stochastic.repetitionPenalty != null && (<NumberSliderRowlabel="Suffix repetition length"- min={SUFFIX_REPETITION_LENGTH_MIN}- max={SUFFIX_REPETITION_LENGTH_MAX}+ changed={fieldChanged("suffixRepetitionLength")}+ min={Math.min(+ SUFFIX_REPETITION_LENGTH_MIN,+ stochastic.suffixRepetitionLength ?? SUFFIX_REPETITION_LENGTH_MIN,+ )}+ max={Math.max(+ SUFFIX_REPETITION_LENGTH_MAX,+ stochastic.suffixRepetitionLength ?? SUFFIX_REPETITION_LENGTH_MAX,+ )}step={SUFFIX_REPETITION_LENGTH_STEP}decimals={0}noSlider@@ -238,34 +278,65 @@ export const ModelParamsControls = ({ repoId }: ModelParamsControlsProps) => {)}</AnimatePresence>- {reasoning.kind === "toggle" && (- <div className="flex items-center justify-between gap-3 border-t border-cell-border pt-3">- <span className="text-[13px] text-label-title">Reasoning</span>+ <div className="flex flex-col gap-3 border-t border-cell-border pt-3">+ <SectionHeading+ name="Tools"+ changed={dateTimeChanged || chartChanged || chatNamingChanged}+ onReset={resetTools}+ />+ {!supportsTools && (+ <p className="text-[12px] leading-relaxed text-label-muted">This model does not support tool calls.</p>+ )}+ <div className="flex items-start justify-between gap-3">+ <div className="flex flex-col gap-1">+ <span className="text-[13px] text-label-title">+ Current date and time{dateTimeChanged && <ChangedMarker />}+ </span>+ <p className="text-[12px] leading-relaxed text-label-muted">Look up the current local date and time.</p>+ </div><Toggle- label="Reasoning"- checked={reasoningEffort !== "disabled"}- onChange={(checked) => onReasoningChange(checked ? "default" : "disabled")}+ label="Current date and time"+ checked={supportsTools && tools.dateTimeToolEnabled}+ disabled={!supportsTools}+ onChange={(enabled) => onToolChange("dateTimeToolEnabled", enabled)}/></div>- )}-- {reasoning.kind === "levels" && levelValue !== undefined && (- <div className="flex flex-col gap-1.5 border-t border-cell-border pt-3">- <span className={sectionLabel}>Reasoning</span>- <SegmentedControl- ariaLabel="Reasoning"- value={levelValue}- onChange={(value) => onReasoningChange(value as ReasoningEffort)}- options={levelOptions}+ <div className="flex items-start justify-between gap-3">+ <div className="flex flex-col gap-1">+ <span className="text-[13px] text-label-title">Draw charts{chartChanged && <ChangedMarker />}</span>+ <p className="text-[12px] leading-relaxed text-label-muted">Show charts in replies.</p>+ </div>+ <Toggle+ label="Draw charts"+ checked={supportsTools && tools.chartToolEnabled}+ disabled={!supportsTools}+ onChange={(enabled) => onToolChange("chartToolEnabled", enabled)}/></div>- )}+ {globalModelChatNamingEnabled && (+ <div className="flex items-start justify-between gap-3">+ <div className="flex flex-col gap-1">+ <span className="text-[13px] text-label-title">Name chat{chatNamingChanged && <ChangedMarker />}</span>+ <p className="text-[12px] leading-relaxed text-label-muted">+ Name and rename the chat using a tool call.+ </p>+ </div>+ <Toggle+ label="Name chat using a tool"+ checked={supportsTools && tools.modelChatNamingEnabled}+ disabled={!supportsTools}+ onChange={(enabled) => onToolChange("modelChatNamingEnabled", enabled)}+ />+ </div>+ )}+ </div></div>);};type NumberSliderRowProps = {label: string;+ changed: boolean;min: number;max: number;step: number;@@ -281,6 +352,7 @@ const fmt = (value: number, decimals: number) => (decimals > 0 ? String(value) :const NumberSliderRow = ({label,+ changed,min,max,step,@@ -318,13 +390,16 @@ const NumberSliderRow = ({return (<div className="flex flex-col gap-1.5"><div className="flex items-center justify-between gap-3">- <div className="flex items-center gap-2">- <span id={labelId} className="text-[13px] text-label-title">- {label}- </span>- {optional && (- <Checkbox checked={active} onChange={(on) => onToggle?.(on)} size="sm" aria-label={`Enable ${label}`} />- )}+ <div className="flex items-center">+ <div className="flex items-center gap-2">+ <span id={labelId} className="text-[13px] text-label-title">+ {label}+ </span>+ {optional && (+ <Checkbox checked={active} onChange={(on) => onToggle?.(on)} size="sm" aria-label={`Enable ${label}`} />+ )}+ </div>+ {changed && <ChangedMarker />}</div><Inputsize="sm"diff --git a/apps/mirai-gui/client/src/features/chat/components/composer/model-params-drawer.tsx b/apps/mirai-gui/client/src/features/chat/components/composer/model-params-drawer.tsxindex abdabea95..1b8e5ed77 100644--- a/apps/mirai-gui/client/src/features/chat/components/composer/model-params-drawer.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/composer/model-params-drawer.tsx@@ -3,8 +3,10 @@ import { useChatStore } from "@/stores/use-chat-store";import { Dialog, DialogPanel, DialogTitle, Transition, TransitionChild } from "@headlessui/react";import { ModelPicker } from "./chat-input/model-picker";import { X } from "lucide-react";-import { Fragment } from "react";+import { Fragment, useEffect, useState } from "react";import { ModelParamsControls } from "./model-params-controls";+import { getPlatform } from "@/platform/platform-singleton";+import type { SamplingDefaults } from "@/platform/services/chat";type ModelParamsDrawerProps = {open: boolean;@@ -16,6 +18,26 @@ type ModelParamsDrawerProps = {export const ModelParamsDrawer = ({ open, chatId, repoId, onClose }: ModelParamsDrawerProps) => {const setChatModel = useChatStore((s) => s.setChatModel);const pickerModels = useInstalledPickerModels();+ const [defaults, setDefaults] = useState<{ repoId: string; sampling: SamplingDefaults | null } | null>(null);+ const samplingReady = defaults?.repoId === repoId && defaults.sampling !== null;++ // This component stays mounted while the drawer is closed. Read defaults on+ // model selection so its controls are ready before the opening transition.+ useEffect(() => {+ if (!repoId || samplingReady) return;+ let alive = true;+ void getPlatform()+ .chat.getSamplingDefaults(repoId)+ .then((sampling) => {+ if (alive) setDefaults({ repoId, sampling });+ })+ .catch(() => {+ if (alive) setDefaults({ repoId, sampling: null });+ });+ return () => {+ alive = false;+ };+ }, [repoId, open, samplingReady]);const onPickModel = (modelId: string) => {const model = pickerModels.find((m) => m.id === modelId);@@ -71,8 +93,17 @@ export const ModelParamsDrawer = ({ open, chatId, repoId, onClose }: ModelParams/></div>- <div className="mt-4 flex-1 overflow-y-auto">- {repoId ? <ModelParamsControls repoId={repoId} /> : null}+ <div className="mt-4 flex-1 min-h-0 overflow-y-auto overscroll-y-contain">+ {repoId &&+ (defaults?.repoId === repoId ? (+ <ModelParamsControls+ key={`${repoId}:${samplingReady}`}+ repoId={repoId}+ samplingDefaults={defaults.sampling}+ />+ ) : (+ <p className="text-[12px] text-label-muted">Loading model sampling settings…</p>+ ))}</div></DialogPanel></TransitionChild>diff --git a/apps/mirai-gui/client/src/features/chat/components/message/chart-message.test.tsx b/apps/mirai-gui/client/src/features/chat/components/message/chart-message.test.tsxnew file mode 100644index 000000000..2c095105e--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/components/message/chart-message.test.tsx@@ -0,0 +1,114 @@+import { act, cleanup, render, screen } from "@testing-library/react";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { useAppStore } from "@/stores/use-app-store";+import { CHART_TYPES, type ChartSpec } from "@/types/chart";+import ChartMessage, { chartConfiguration } from "./chart-message";++const drawing = vi.hoisted(() => ({ create: vi.fn(), destroy: vi.fn() }));+vi.mock("chart.js/auto", () => ({+ default: class {+ constructor(canvas: unknown, config: unknown) {+ drawing.create(canvas, config);+ }+ destroy() {+ drawing.destroy();+ }+ },+}));++const chart: ChartSpec = {+ type: "bar",+ title: "<img src=x onerror=alert(1)>",+ labels: ["Rent", "Food"],+ datasets: [{ label: "Monthly spending", data: [900, 350] }],+};+beforeEach(() => {+ vi.clearAllMocks();+ useAppStore.setState({ isDarkMode: true });+});+afterEach(cleanup);++it("renders text safely and keeps completed charts stable through later streaming snapshots", () => {+ const view = render(<ChartMessage chart={chart} />);+ expect(screen.getByText(chart.title)).toBeTruthy();+ expect(view.container.querySelector("img")).toBeNull();+ expect(screen.getByRole("img").tagName).toBe("CANVAS");+ expect(drawing.create).toHaveBeenCalledTimes(1);+ view.rerender(<ChartMessage chart={structuredClone(chart)} />);+ expect(drawing.create).toHaveBeenCalledTimes(1);+ act(() => useAppStore.setState({ isDarkMode: false }));+ expect(drawing.create).toHaveBeenCalledTimes(2);+ expect(drawing.destroy).toHaveBeenCalledTimes(1);+ view.unmount();+ expect(drawing.destroy).toHaveBeenCalledTimes(2);+});++it("refuses arbitrary options even if a malformed chart reaches the renderer", () => {+ render(<ChartMessage chart={{ ...chart, options: { onClick: "alert(1)" } } as ChartSpec} />);+ expect(drawing.create).not.toHaveBeenCalled();+ expect(screen.getByText("This chart could not be displayed.")).toBeTruthy();+});++it("defaults charts to medium and applies each model-selected height", () => {+ const view = render(<ChartMessage chart={chart} />);+ expect(screen.getByRole("img").parentElement?.style.height).toBe("480px");+ for (const [height, pixels] of [+ ["small", 320],+ ["medium", 480],+ ["big", 640],+ ] as const) {+ view.rerender(<ChartMessage chart={{ ...chart, height }} />);+ expect(screen.getByRole("img").parentElement?.style.height).toBe(`${pixels}px`);+ }+ view.rerender(<ChartMessage chart={chart} />);+ expect(screen.getByRole("img").parentElement?.style.height).toBe("480px");+});++it.each(["pie", "doughnut", "polarArea"] as const)(+ "explains an all-zero %s while preserving its canvas and data",+ (type) => {+ const zeroChart: ChartSpec = {+ ...chart,+ type,+ datasets: [+ { label: "First", data: [0, 0] },+ { label: "Second", data: [0, 0] },+ ],+ };+ const view = render(<ChartMessage chart={zeroChart} />);+ const explanation = screen.getByText("All values are zero, so there are no segments to show.");+ expect(screen.getByRole("img").getAttribute("aria-describedby")).toBe(explanation.id);+ expect(screen.getAllByRole("cell").filter((cell) => cell.textContent === "0")).toHaveLength(4);+ expect(drawing.create.mock.calls[0]?.[1].data.datasets.map((dataset: { data: number[] }) => dataset.data)).toEqual([+ [0, 0],+ [0, 0],+ ]);++ view.rerender(+ <ChartMessage chart={{ ...zeroChart, datasets: [zeroChart.datasets[0]!, { label: "Second", data: [0, 1] }] }} />,+ );+ expect(screen.queryByText(explanation.textContent!)).toBeNull();+ expect(screen.getByRole("img").hasAttribute("aria-describedby")).toBe(false);+ },+);++it.each(["bar", "line", "radar"] as const)("does not treat valid zero values in a %s as missing segments", (type) => {+ render(<ChartMessage chart={{ ...chart, type, datasets: [{ label: "Data", data: [0, 0] }] }} />);+ expect(screen.queryByText("All values are zero, so there are no segments to show.")).toBeNull();+ expect(drawing.create).toHaveBeenCalledOnce();+});++it.each(CHART_TYPES)("builds %s without sharing mutable dataset objects with the saved transcript", (type) => {+ const xy = type === "scatter" || type === "bubble";+ const spec: ChartSpec = {+ ...chart,+ type,+ labels: xy ? undefined : chart.labels,+ datasets: [{ label: "Data", data: xy ? [{ x: 2, y: 4, ...(type === "bubble" ? { r: 8 } : {}) }] : [1, 2] }],+ };+ const config = chartConfiguration(spec, true);+ expect(config.type).toBe(type);+ expect(config.data.datasets[0]?.data).toEqual(spec.datasets[0]?.data);+ expect(config.data.datasets[0]?.data).not.toBe(spec.datasets[0]?.data);+ if (xy) expect(config.data.datasets[0]?.data[0]).not.toBe(spec.datasets[0]?.data[0]);+});diff --git a/apps/mirai-gui/client/src/features/chat/components/message/chart-message.tsx b/apps/mirai-gui/client/src/features/chat/components/message/chart-message.tsxnew file mode 100644index 000000000..e6a49be6c--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/components/message/chart-message.tsx@@ -0,0 +1,172 @@+import Chart from "chart.js/auto";+import type { ChartConfiguration } from "chart.js";+import { memo, useEffect, useId, useRef } from "react";+import { useAppStore } from "@/stores/use-app-store";+import { isChartSpec, type ChartSpec } from "@/types/chart";+import { chartTable } from "@/utils/chart-data";++const CHART_HEIGHT_PX = { small: 320, medium: 480, big: 640 };++export function chartConfiguration(spec: ChartSpec, dark: boolean): ChartConfiguration {+ const colors = dark+ ? ["#74a9ff", "#f4ab72", "#80c99a", "#b59aef", "#ed94b9", "#69cbd2", "#d6ca73", "#abb8d1"]+ : ["#3676cf", "#bd6828", "#32824e", "#7950b6", "#b84477", "#25828a", "#8f7c1d", "#60718e"];+ const foreground = dark ? "#c5c7cb" : "#43464b";+ const grid = dark ? "#ffffff18" : "#00000018";+ const font = { family: "InterVariable, Inter, system-ui, sans-serif", size: 12 };+ const tooltipBackground = dark ? "#191919" : "#f0f0f0";+ const tooltipForeground = dark ? "#eeeeee" : "#202020";+ const segments = spec.type === "pie" || spec.type === "doughnut" || spec.type === "polarArea";+ const radial = spec.type === "radar" || spec.type === "polarArea";+ const cartesian = !segments && !radial;+ return {+ type: spec.type,+ data: {+ labels: spec.labels ? [...spec.labels] : undefined,+ datasets: spec.datasets.map((dataset, index) => {+ const color = colors[index % colors.length]!;+ return {+ label: dataset.label,+ // Copy only known data fields. No model-supplied options or callbacks+ // are passed into Chart.js, and Chart.js cannot mutate saved data.+ data: dataset.data.map((point) =>+ typeof point === "number"+ ? point+ : { x: point.x, y: point.y, ...(point.r !== undefined ? { r: point.r } : {}) },+ ),+ borderColor: segments ? colors : color,+ backgroundColor: segments ? colors.map((entry) => `${entry}b3`) : `${color}66`,+ borderWidth: 2,+ pointRadius: spec.type === "line" ? 2 : undefined,+ fill: spec.type === "radar",+ };+ }),+ },+ options: {+ responsive: true,+ maintainAspectRatio: false,+ animation: false,+ color: foreground,+ font,+ plugins: {+ colors: { enabled: false },+ legend: {+ display: segments || spec.datasets.length > 1,+ position: "bottom",+ labels: { color: foreground, font, boxWidth: 12, boxHeight: 12, padding: 16 },+ },+ tooltip: {+ backgroundColor: tooltipBackground,+ titleColor: tooltipForeground,+ bodyColor: tooltipForeground,+ footerColor: tooltipForeground,+ titleFont: { ...font, weight: "bold" },+ bodyFont: font,+ footerFont: { ...font, weight: "bold" },+ borderColor: dark ? "#ffffff1a" : "#0000000f",+ borderWidth: 1,+ multiKeyBackground: tooltipBackground,+ },+ },+ ...(cartesian+ ? {+ scales: {+ x: {+ grid: { color: grid },+ border: { color: grid },+ ticks: { color: foreground, font },+ title: { display: !!spec.xLabel, text: spec.xLabel, color: foreground, font },+ },+ y: {+ beginAtZero: spec.type === "bar",+ grid: { color: grid },+ border: { color: grid },+ ticks: { color: foreground, font },+ title: { display: !!spec.yLabel, text: spec.yLabel, color: foreground, font },+ },+ },+ }+ : radial+ ? {+ scales: {+ r: {+ grid: { color: grid },+ angleLines: { color: grid },+ pointLabels: { color: foreground, font: { ...font, size: 10 } },+ ticks: { color: foreground, font, backdropColor: "transparent" },+ },+ },+ }+ : {}),+ },+ };+}++function ChartMessage({ chart }: { chart: ChartSpec }) {+ const canvasRef = useRef<HTMLCanvasElement>(null);+ const emptyDescriptionId = useId();+ const dark = useAppStore((state) => state.isDarkMode);+ const valid = isChartSpec(chart);+ useEffect(() => {+ if (!valid || !canvasRef.current) return;+ const instance = new Chart(canvasRef.current, chartConfiguration(chart, dark));+ return () => instance.destroy();+ }, [chart, dark, valid]);++ if (!valid) return <p className="my-3 text-[13px] text-label-muted">This chart could not be displayed.</p>;+ const table = chartTable(chart);+ const allSegmentsZero =+ (chart.type === "pie" || chart.type === "doughnut" || chart.type === "polarArea") &&+ chart.datasets.every((dataset) => dataset.data.every((value) => value === 0));+ return (+ <figure className="my-3 min-w-0 rounded-lg border border-border-default bg-surface-elevated p-4">+ <figcaption className="mb-3 text-[14px] font-medium text-text-primary [overflow-wrap:anywhere]">+ {chart.title}+ </figcaption>+ <div className="relative min-w-0" style={{ height: CHART_HEIGHT_PX[chart.height ?? "medium"] }}>+ <canvas+ ref={canvasRef}+ role="img"+ aria-label={`${chart.title} (${chart.type} chart)`}+ aria-describedby={allSegmentsZero ? emptyDescriptionId : undefined}+ >+ <table>+ <thead>+ <tr>+ {table.headers.map((header, index) => (+ <th key={index}>{header}</th>+ ))}+ </tr>+ </thead>+ <tbody>+ {table.rows.map((row, index) => (+ <tr key={index}>+ {row.map((cell, column) => (+ <td key={column}>{cell}</td>+ ))}+ </tr>+ ))}+ </tbody>+ </table>+ </canvas>+ {allSegmentsZero && (+ <div className="pointer-events-none absolute inset-0 flex items-center justify-center p-4">+ <p+ id={emptyDescriptionId}+ className="rounded-md bg-surface-elevated px-3 py-2 text-center text-[13px] text-text-secondary"+ >+ All values are zero, so there are no segments to show.+ </p>+ </div>+ )}+ </div>+ </figure>+ );+}++// Streaming snapshots repeat completed charts. Do not redraw them for every+// following token; theme changes still update through the store subscription.+export default memo(+ ChartMessage,+ (previous, next) => previous.chart === next.chart || JSON.stringify(previous.chart) === JSON.stringify(next.chart),+);diff --git a/apps/mirai-gui/client/src/features/chat/components/message/chat-message-list.tsx b/apps/mirai-gui/client/src/features/chat/components/message/chat-message-list.tsxindex 656e1a1da..520cec491 100644--- a/apps/mirai-gui/client/src/features/chat/components/message/chat-message-list.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/message/chat-message-list.tsx@@ -12,6 +12,8 @@ type ChatMessageListProps = {loadingMessageId: string | null;canceledMessageId: string | null;onMessageModelSelect: (messageId: string, modelId: string, modelName: string) => void;+ onEditMessage: (messageId: string, text: string, onSaved: () => void) => Promise<void>;+ canEditMessages: boolean;};export const ChatMessageList = ({@@ -26,6 +28,8 @@ export const ChatMessageList = ({loadingMessageId,canceledMessageId,onMessageModelSelect,+ onEditMessage,+ canEditMessages,}: ChatMessageListProps) => (<MessageListisNew={isNewChat}@@ -34,6 +38,8 @@ export const ChatMessageList = ({onScrolled={onScrolled}isUiStreaming={isChatStreaming}onMessageModelSelect={onMessageModelSelect}+ onEditMessage={onEditMessage}+ canEditMessages={canEditMessages}loadingMessageId={loadingMessageId}canceledMessageId={canceledMessageId}isModelLoading={isModelLoading}diff --git a/apps/mirai-gui/client/src/features/chat/components/message/markdown-renderer.tsx b/apps/mirai-gui/client/src/features/chat/components/message/markdown-renderer.tsxindex 270758922..b33d1f371 100644--- a/apps/mirai-gui/client/src/features/chat/components/message/markdown-renderer.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/message/markdown-renderer.tsx@@ -64,7 +64,7 @@ const MarkdownTable = ({ children, ...props }: MarkdownComponentProps<"table">)<div className="absolute right-1 top-[10px] z-10"><CopyButton className="!min-w-6 !min-h-6" onCopy={handleTableCopy} /></div>- <div className="overflow-x-auto thin-scrollbar">+ <div className="overflow-x-auto overscroll-x-contain overscroll-y-auto thin-scrollbar"><table ref={tableRef} className="min-w-full" {...props}>{children}</table>diff --git a/apps/mirai-gui/client/src/features/chat/components/message/message-list.tsx b/apps/mirai-gui/client/src/features/chat/components/message/message-list.tsxindex 4dc192c8a..dbc04e060 100644--- a/apps/mirai-gui/client/src/features/chat/components/message/message-list.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/message/message-list.tsx@@ -12,6 +12,8 @@ type MessageListProps = {onScrolled?: () => void;isUiStreaming?: boolean;onMessageModelSelect?: (messageId: string, modelId: string, modelName: string) => void;+ onEditMessage?: (messageId: string, text: string, onSaved: () => void) => Promise<void>;+ canEditMessages?: boolean;loadingMessageId?: string | null;canceledMessageId?: string | null;isModelLoading?: boolean;@@ -25,6 +27,8 @@ const MessageListComponent: React.FC<MessageListProps> = ({onScrolled,isUiStreaming,onMessageModelSelect,+ onEditMessage,+ canEditMessages,loadingMessageId,canceledMessageId,isModelLoading,@@ -163,8 +167,11 @@ const MessageListComponent: React.FC<MessageListProps> = ({const loaderText = showWaiting ? "Waiting for the model to be ready…" : "Generating reply…";return (- <div className="relative flex flex-1 flex-col overflow-hidden h-full">- <div ref={scrollContainerRef} className="absolute inset-0 flex flex-col gap-4 overflow-y-auto scrollbar-hide">+ <div className="relative flex flex-1 flex-col overflow-clip h-full">+ <div+ ref={scrollContainerRef}+ className="absolute inset-0 flex flex-col overflow-y-auto overscroll-y-contain scrollbar-hide"+ >{messages.length > 0 ? (messages.map((message, idx) => {const isLastAssistant = idx === lastAssistantIndex;@@ -176,10 +183,12 @@ const MessageListComponent: React.FC<MessageListProps> = ({: undefined;const activeParsed = hasVersions ? activeVersion?.output?.text?.parsed : message.output?.text?.parsed;const activeText = hasVersions ? activeVersion?.text : message.text;+ const activeTranscript = hasVersions ? activeVersion?.output?.transcript : message.output?.transcript;const hasVisibleParsed = Boolean((activeParsed?.chainOfThought && activeParsed.chainOfThought.length > 0) ||(activeParsed?.response && activeParsed.response.length > 0) ||+ activeTranscript?.length ||(activeText && activeText.length > 0),);@@ -200,7 +209,10 @@ const MessageListComponent: React.FC<MessageListProps> = ({ref={(el) => {itemRefs.current[message.id] = el;}}- className={isLastAssistant ? "flex flex-col min-h-8" : undefined}+ className={[+ isLastAssistant ? "flex flex-col min-h-8" : "",+ idx > 0 ? (message.sender === "user" ? "mt-3" : "mt-1") : "",+ ].join(" ")}>{showAssistantLoader ? (<div className="flex items-center gap-2 text-left text-[15px] leading-[18px] text-label-muted">@@ -220,6 +232,8 @@ const MessageListComponent: React.FC<MessageListProps> = ({isUiStreaming={Boolean(isUiStreaming && isLastAssistant)}isCanceled={canceledMessageId === message.id}onModelSelect={onMessageModelSelect}+ onEdit={onEditMessage}+ canEdit={canEditMessages}{...message}/></div>@@ -241,11 +255,11 @@ const MessageListComponent: React.FC<MessageListProps> = ({</div>)}{lastAssistantIndex === -1 && isBusy && !loadingMessageId && (- <div className="flex items-center gap-2 text-left text-[15px] leading-[18px] text-label-muted">+ <div className="mt-1 flex items-center gap-2 text-left text-[15px] leading-[18px] text-label-muted"><Loader text={loaderText} /></div>)}- <div ref={messagesEndRef} />+ <div ref={messagesEndRef} className="h-4 shrink-0" /></div></div>);diff --git a/apps/mirai-gui/client/src/features/chat/components/message/message-version-controls.tsx b/apps/mirai-gui/client/src/features/chat/components/message/message-version-controls.tsxindex fdd9f504c..7e5259898 100644--- a/apps/mirai-gui/client/src/features/chat/components/message/message-version-controls.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/message/message-version-controls.tsx@@ -43,6 +43,8 @@ export const MessageVersionControls: React.FC<MessageVersionControlsProps> = ({<buttononClick={handlePrevious}disabled={!canGoPrevious}+ aria-label="Previous response"+ title="Previous response"className={twMerge("p-1 rounded transition-colors",canGoPrevious ? "text-label-muted hover:text-label-title" : "text-label-muted/50 cursor-not-allowed",@@ -51,16 +53,15 @@ export const MessageVersionControls: React.FC<MessageVersionControlsProps> = ({<ChevronLeft className="w-4 h-4" /></button>- <span className="text-sm text-label-muted">+ <span className="text-sm text-label-muted" title={currentModelName || undefined}>{currentVersion + 1}/{totalVersions}</span>- {currentModelName && (- <span className="text-[13px] leading-[130%] text-label-muted truncate max-w-[220px]">{currentModelName}</span>- )}<buttononClick={handleNext}disabled={!canGoNext}+ aria-label="Next response"+ title="Next response"className={twMerge("p-1 rounded transition-colors",canGoNext ? "text-label-muted hover:text-label-title" : "text-label-muted/50 cursor-not-allowed",diff --git a/apps/mirai-gui/client/src/features/chat/components/message/message.test.tsx b/apps/mirai-gui/client/src/features/chat/components/message/message.test.tsxnew file mode 100644index 000000000..00579df84--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/components/message/message.test.tsx@@ -0,0 +1,320 @@+import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { useChatSessionStore } from "@/stores/use-chat-session-store";+import { useChatStore } from "@/stores/use-chat-store";+import type { Message as StoredMessage } from "@/types/message";+import type { TranscriptItem } from "@/types/llm-stream";+import { Message } from "./message";++const clipboard = vi.hoisted(() => ({ write: vi.fn<(items: ClipboardItem[]) => Promise<void>>(async () => {}) }));+vi.mock("@/utils/clipboard", () => ({ writeItemsWithFocus: clipboard.write }));+vi.mock("chart.js/auto", () => ({+ default: class {+ destroy() {}+ },+}));++vi.mock("./markdown-renderer", () => ({+ MarkdownRenderer: ({ content }: { content: string }) => <div>{content}</div>,+}));+vi.mock("./model-selector", () => ({ ModelSelector: () => null }));+vi.mock("./performance-dropdown", () => ({ PerformanceDropdown: () => null }));++const sessionDefaults = useChatSessionStore.getState();+const chatDefaults = useChatStore.getState();+const message: StoredMessage = {+ id: "answer",+ sender: "assistant",+ timestamp: 1,+ text: "Response",+ output: { text: { parsed: { chainOfThought: "Reasoning", response: "Response" } } },+};++beforeEach(() => {+ useChatSessionStore.setState(sessionDefaults, true);+ useChatStore.setState({ ...chatDefaults, messages: [message] }, true);+});+afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+});++it("copies chart-only replies as readable data instead of an empty canvas", async () => {+ class ClipboardEntry {+ constructor(public items: Record<string, Blob>) {}+ }+ vi.stubGlobal("ClipboardItem", ClipboardEntry);+ clipboard.write.mockClear();+ const chartMessage: StoredMessage = {+ ...message,+ text: "",+ output: {+ transcript: [+ {+ type: "chart",+ chart: {+ type: "bar",+ title: "Budget",+ labels: ["Rent", "Food"],+ datasets: [{ label: "Monthly spending", data: [900, 350] }],+ },+ },+ ],+ },+ };+ useChatStore.setState({ messages: [chartMessage] });+ render(<Message {...chartMessage} chatId="chat" />);+ await screen.findByRole("img", { name: "Budget (bar chart)" });+ fireEvent.click(screen.getByRole("button", { name: "Copy" }));+ await waitFor(() => expect(clipboard.write).toHaveBeenCalledOnce());+ const copied = clipboard.write.mock.calls[0]![0][0] as unknown as ClipboardEntry;+ const read = (kind: string) =>+ new Promise<string>((resolve) => {+ const reader = new FileReader();+ reader.onload = () => resolve(reader.result as string);+ reader.readAsText(copied.items[kind]!);+ });+ const html = await read("text/html");+ expect(html).toContain("<table>");+ expect(html).toContain("Monthly spending");+ expect(html).toContain("900");+ expect(html).not.toContain("<canvas");+ expect(await read("text/plain")).toContain("Category\tMonthly spending\nRent\t900\nFood\t350");+});++it("changes Thinking... to Thinking when this response finishes", () => {+ useChatSessionStore.setState({+ isGenerating: true,+ activeGeneratingChatId: "chat",+ activeAssistantMessageId: message.id,+ });+ render(<Message {...message} chatId="chat" isLast isUiStreaming />);+ expect(screen.getByText("Thinking...")).toBeTruthy();++ act(() => useChatSessionStore.getState().setGenerating(false));+ expect(screen.getByText("Thinking")).toBeTruthy();+});++it("tracks the actual message when regenerating an earlier response", () => {+ const later = { ...message, id: "later-answer" };+ useChatStore.setState({ messages: [message, later] });+ useChatSessionStore.setState({+ isGenerating: true,+ activeGeneratingChatId: "chat",+ activeAssistantMessageId: message.id,+ });+ render(+ <>+ <Message {...message} chatId="chat" />+ <Message {...later} chatId="chat" isLast isUiStreaming />+ </>,+ );+ expect(screen.getAllByText(/^Thinking/).map((element) => element.textContent)).toEqual(["Thinking...", "Thinking"]);+});++it("keeps an older version and other chats marked finished", () => {+ const versions = ["old", "new"].map((id) => ({ ...message, id, modelId: "model", modelName: "Model" }));+ useChatStore.setState({ messages: [{ ...message, versions, currentVersionIndex: 0 }] });+ useChatSessionStore.setState({+ isGenerating: true,+ activeGeneratingChatId: "chat",+ activeAssistantMessageId: message.id,+ });+ const view = render(<Message {...message} chatId="chat" isLast isUiStreaming />);+ expect(screen.getByText("Thinking")).toBeTruthy();++ act(() => useChatStore.setState({ messages: [message] }));+ view.rerender(<Message {...message} chatId="other-chat" isLast isUiStreaming />);+ expect(screen.getByText("Thinking")).toBeTruthy();+});++const renderThinkingMessage = () => {+ const thinkingMessage: StoredMessage = {+ ...message,+ text: "",+ output: { text: { parsed: { chainOfThought: "Reasoning", response: "" } } },+ };+ useChatStore.setState({ messages: [thinkingMessage] });+ useChatSessionStore.setState({+ isGenerating: true,+ activeGeneratingChatId: "chat",+ activeAssistantMessageId: message.id,+ });+ const view = render(<Message {...thinkingMessage} chatId="chat" isLast isUiStreaming />);+ return { view, thinkingMessage, toggle: screen.getByRole("button", { name: /^Thinking/ }) };+};++it("opens during reasoning, collapses when the answer starts, and allows reopening", () => {+ const { toggle } = renderThinkingMessage();+ expect(toggle.getAttribute("aria-expanded")).toBe("true");++ act(() => useChatStore.getState().updateMessage(message.id, message));+ expect(toggle.getAttribute("aria-expanded")).toBe("false");++ fireEvent.click(toggle);+ expect(toggle.getAttribute("aria-expanded")).toBe("true");++ act(() => useChatSessionStore.getState().setGenerating(false));+ expect(toggle.getAttribute("aria-expanded")).toBe("true");++ act(() =>+ useChatStore.setState((state) => ({ messages: [...state.messages, { ...message, id: "another-answer" }] })),+ );+ expect(toggle.getAttribute("aria-expanded")).toBe("true");+});++it.each(["finish", "cancel", "error"] as const)(+ "collapses when reasoning ends through %s, then stays open if reopened",+ (outcome) => {+ const { view, thinkingMessage, toggle } = renderThinkingMessage();+ expect(toggle.getAttribute("aria-expanded")).toBe("true");++ if (outcome === "finish") {+ act(() => useChatSessionStore.getState().setGenerating(false));+ } else if (outcome === "cancel") {+ view.rerender(<Message {...thinkingMessage} chatId="chat" isLast isUiStreaming isCanceled />);+ } else {+ act(() => useChatStore.getState().updateMessage(message.id, { error: "Generation failed" }));+ }+ expect(toggle.getAttribute("aria-expanded")).toBe("false");++ fireEvent.click(toggle);+ expect(toggle.getAttribute("aria-expanded")).toBe("true");++ act(() => useChatSessionStore.getState().clearActiveGenerating());+ expect(toggle.getAttribute("aria-expanded")).toBe("true");+ },+);++it("keeps reasoning closed when the user closes it while more reasoning arrives", () => {+ const { toggle } = renderThinkingMessage();+ expect(toggle.getAttribute("aria-expanded")).toBe("true");++ fireEvent.click(toggle);+ expect(toggle.getAttribute("aria-expanded")).toBe("false");++ act(() =>+ useChatStore.getState().updateMessage(message.id, {+ output: { text: { parsed: { chainOfThought: "Reasoning continues", response: "" } } },+ }),+ );+ expect(toggle.getAttribute("aria-expanded")).toBe("false");+});++it("renders each reasoning, text and tool turn in order and collapses reasoning independently", () => {+ const first: TranscriptItem = { type: "thinking", text: "First reasoning", completed: false };+ const update = (transcript: TranscriptItem[]) =>+ act(() => useChatStore.getState().updateMessage(message.id, { output: { transcript } }));+ update([first]);+ useChatSessionStore.setState({+ isGenerating: true,+ activeGeneratingChatId: "chat",+ activeAssistantMessageId: message.id,+ });+ const view = render(<Message {...message} chatId="chat" isLast isUiStreaming />);+ const firstToggle = screen.getByRole("button", { name: /^Thinking/ });+ expect(firstToggle.getAttribute("aria-expanded")).toBe("true");++ const firstTurn: TranscriptItem[] = [+ { ...first, completed: true },+ { type: "text", text: "Checking the time." },+ { type: "toolCall", name: "get_current_date_time", called: false },+ ];+ update(firstTurn);+ expect(firstToggle.getAttribute("aria-expanded")).toBe("false");+ expect(view.container.textContent).toContain("Checking the date and time...");+ expect(view.container.textContent).not.toContain("Renaming the chat");+ expect(view.container.textContent).not.toContain("Response");+ const answerNode = screen.getByText("Checking the time.");++ const nextTurn: TranscriptItem[] = [+ ...firstTurn.slice(0, -1),+ { type: "toolCall", name: "get_current_date_time", called: true },+ { type: "thinking", text: "Second reasoning", completed: false },+ ];+ update(nextTurn);+ expect(screen.getByText("Checking the time.")).toBe(answerNode);+ expect(view.container.textContent).toMatch(/Checking the time\..*Checked the date and time.*Thinking\.\.\./s);+ const secondToggle = screen.getAllByRole("button", { name: /^Thinking/ })[1]!;+ expect(secondToggle.getAttribute("aria-expanded")).toBe("true");+ fireEvent.click(firstToggle);+ expect(firstToggle.getAttribute("aria-expanded")).toBe("true");++ update([+ ...nextTurn.slice(0, -1),+ { type: "thinking", text: "Second reasoning", completed: true },+ { type: "text", text: "It is noon." },+ ]);+ expect(firstToggle.getAttribute("aria-expanded")).toBe("true");+ expect(secondToggle.getAttribute("aria-expanded")).toBe("false");+ fireEvent.click(secondToggle);+ expect(secondToggle.getAttribute("aria-expanded")).toBe("true");+ act(() => useChatSessionStore.getState().setGenerating(false));+ expect(secondToggle.getAttribute("aria-expanded")).toBe("true");+});++it("keeps one reasoning block across hidden housekeeping and preserves manual toggles", () => {+ useChatStore.getState().updateMessage(message.id, {+ output: { transcript: [{ type: "thinking", text: "Choose a title", completed: false }] },+ });+ useChatSessionStore.setState({+ isGenerating: true,+ activeGeneratingChatId: "chat",+ activeAssistantMessageId: message.id,+ });+ render(<Message {...message} chatId="chat" isLast isUiStreaming />);+ const toggle = screen.getByRole("button", { name: /^Thinking/ });+ expect(toggle.getAttribute("aria-expanded")).toBe("true");+ fireEvent.click(toggle);+ expect(toggle.getAttribute("aria-expanded")).toBe("false");+ act(() =>+ useChatStore.getState().updateMessage(message.id, {+ output: {+ transcript: [{ type: "thinking", text: "Choose a title\n\nAnswer the question", completed: false }],+ },+ }),+ );+ expect(screen.getAllByRole("button", { name: /^Thinking/ })).toEqual([toggle]);+ expect(toggle.getAttribute("aria-expanded")).toBe("false");+ fireEvent.click(toggle);+ expect(toggle.getAttribute("aria-expanded")).toBe("true");+ act(() =>+ useChatStore.getState().updateMessage(message.id, {+ output: {+ transcript: [+ { type: "thinking", text: "Choose a title\n\nAnswer the question", completed: true },+ { type: "text", text: "The answer" },+ ],+ },+ }),+ );+ expect(toggle.getAttribute("aria-expanded")).toBe("false");+ expect(screen.getByText("Thinking")).toBeTruthy();+});++it("shows a chat naming tool requested explicitly by the user", () => {+ useChatStore.getState().updateMessage(message.id, {+ output: { transcript: [{ type: "toolCall", name: "set_chat_name", called: true }] },+ });+ const view = render(<Message {...message} chatId="chat" />);+ expect(view.container.textContent).toContain("Renamed the chat");+ expect(view.container.textContent).not.toContain("set_chat_name");+});++it("keeps interrupted tool calls visible without claiming they are still running", () => {+ useChatStore.getState().updateMessage(message.id, {+ output: { transcript: [{ type: "toolCall", name: "get_current_date_time", called: false }] },+ });+ const view = render(<Message {...message} chatId="chat" isCanceled />);+ expect(view.container.textContent).toContain("Stopped checking the date and time");+ expect(view.container.textContent).not.toContain("Checking the date and time...");+});++it("does not claim a failed tool call succeeded", () => {+ useChatStore.getState().updateMessage(message.id, {+ output: { transcript: [{ type: "toolCall", name: "set_chat_name", called: true, failed: true }] },+ });+ render(<Message {...message} chatId="chat" />);+ expect(screen.getByText("Couldn't rename the chat")).toBeTruthy();+ expect(screen.queryByText("Renamed the chat")).toBeNull();+});diff --git a/apps/mirai-gui/client/src/features/chat/components/message/message.tsx b/apps/mirai-gui/client/src/features/chat/components/message/message.tsxindex c90f78da2..f893f1bbe 100644--- a/apps/mirai-gui/client/src/features/chat/components/message/message.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/message/message.tsx@@ -1,21 +1,68 @@import { useChatSessionStore } from "@/stores/use-chat-session-store";import { isOtherChatGenerating } from "@/features/runtime/runtime-busy";import { writeItemsWithFocus } from "@/utils/clipboard";-import { Transition } from "@headlessui/react";-import { ChevronDownIcon } from "lucide-react";-import React, { useEffect, useRef, useState } from "react";-import { twMerge } from "tailwind-merge";+import { WrenchIcon } from "lucide-react";+import React, { lazy, Suspense, useRef } from "react";import type { MessageVersion } from "@/types/message";+import type { TranscriptItem } from "@/types/llm-stream";import { useChatStore } from "@/stores/use-chat-store";-import { attachmentStorage } from "../../services/attachment-storage";-import { ModelMenuIcon } from "@/components/icons/model-menu-icon";-import { ThinkingBubbleIcon } from "@/components/icons/thinking-bubble";-import { AttachedFilesDisplay } from "../composer/attached-files-display";import { CopyButton } from "@/components/ui/copy-button";import { MarkdownRenderer } from "./markdown-renderer";+import { ThinkingBlock } from "./thinking-block";import { MessageVersionControls } from "./message-version-controls";import { ModelSelector } from "./model-selector";import { PerformanceDropdown } from "./performance-dropdown";+import { UserMessage, type UserMessageProps } from "./user-message";++const ChartMessage = lazy(() => import("./chart-message"));++const toolLabels = new Map([+ [+ "show_chart",+ {+ running: "Drawing a chart...",+ done: "Drew a chart",+ failed: "Couldn't draw the chart",+ stopped: "Stopped drawing the chart",+ },+ ],+ [+ "get_current_date_time",+ {+ running: "Checking the date and time...",+ done: "Checked the date and time",+ failed: "Couldn't check the date and time",+ stopped: "Stopped checking the date and time",+ },+ ],+ [+ "set_chat_name",+ {+ running: "Renaming the chat...",+ done: "Renamed the chat",+ failed: "Couldn't rename the chat",+ stopped: "Stopped renaming the chat",+ },+ ],+]);++function toolCallLabel(tool: Extract<TranscriptItem, { type: "toolCall" }>, isRunning: boolean): string {+ const state = tool.failed ? "failed" : tool.called ? "done" : isRunning ? "running" : "stopped";+ const labels = toolLabels.get(tool.name);+ if (labels) return labels[state];++ const name =+ tool.name+ .replace(/([a-z0-9])([A-Z])/g, "$1 $2")+ .replace(/[_-]+/g, " ")+ .trim() || "tool";+ return {+ running: `Using ${name}...`,+ done: `Used ${name}`,+ failed: `Couldn't use ${name}`,+ stopped: `Stopped using ${name}`,+ }[state];+}export type MessageType = {text: string;@@ -36,21 +83,10 @@ type MessageProps = MessageType & {isUiStreaming?: boolean;isCanceled?: boolean;onModelSelect?: (messageId: string, modelId: string, modelName: string) => void;+ canEdit?: boolean;+ onEdit?: UserMessageProps["onEdit"];};-const UserMessage: React.FC<Pick<MessageProps, "text" | "attachmentIds">> = ({ text, attachmentIds }) => (- <>- {attachmentIds && attachmentIds.length > 0 && (- <div className="mb-3 ml-auto w-fit">- <AttachedFilesDisplay files={attachmentStorage.getFiles(attachmentIds)} />- </div>- )}- <div className="font-[350] text-label-title relative w-fit max-w-full rounded-[5px] text-[15px] leading-[140%] px-2.5 py-[6px] bg-bg-hover ml-auto [&>div>*:first-child]:mt-0 [&>div>*:last-child]:mb-0">- <MarkdownRenderer content={text} />- </div>- </>-);-const AssistantMessage: React.FC<MessageProps> = ({text,id,@@ -74,76 +110,26 @@ const AssistantMessage: React.FC<MessageProps> = ({const hasActiveVersions = Array.isArray(messageFromStore?.versions) && (messageFromStore?.versions?.length || 0) > 0;const parsed = hasActiveVersions ? currentMsgVersion?.output?.text?.parsed : messageFromStore?.output?.text?.parsed;+ const transcript = hasActiveVersions ? currentMsgVersion?.output?.transcript : messageFromStore?.output?.transcript;const visibleChainOfThought = parsed?.chainOfThought;const visibleMessageText = hasActiveVersions ? (currentMsgVersion?.text ?? text) : text;const visibleResponseText = parsed?.response ?? visibleMessageText ?? "";const hasError = hasActiveVersions ? !!currentMsgVersion?.error : !!messageFromStore?.error;const errorText = hasActiveVersions ? currentMsgVersion?.error : messageFromStore?.error;+ const isGeneratingThisMessage = useChatSessionStore(+ (s) => s.isGenerating && s.activeGeneratingChatId === chatId && s.activeAssistantMessageId === id,+ );+ const isThinking =+ isGeneratingThisMessage &&+ !isCanceled &&+ !hasError &&+ (!hasActiveVersions || currentMsgVersion === messageFromStore?.versions?.at(-1));const streamInProgress = !!(isLast && (isLoading || isUiStreaming));-- const [isReasoningVisible, setIsReasoningVisible] = useState(false);- const autoOpenedByStreamRef = useRef(false);- const userToggledRef = useRef(false);+ const reasoningInProgress = isThinking && !visibleResponseText;const messageRef = useRef<HTMLDivElement>(null);- const reasoningScrollRef = useRef<HTMLDivElement>(null);-- const reasoningPinnedRef = useRef(true);-- const reasoningAutoScrollingRef = useRef(false);-- const onReasoningScroll = (e: React.UIEvent<HTMLDivElement>) => {- if (reasoningAutoScrollingRef.current) return;- const el = e.currentTarget;- reasoningPinnedRef.current = el.scrollHeight - el.scrollTop - el.clientHeight < 24;- };-- useEffect(() => {- setIsReasoningVisible(false);- autoOpenedByStreamRef.current = false;- userToggledRef.current = false;- reasoningPinnedRef.current = true;- }, [currentMsgVersion?.id]);-- const stickReasoningToBottom = () => {- const el = reasoningScrollRef.current;- if (!el || !reasoningPinnedRef.current) return;- reasoningAutoScrollingRef.current = true;- el.scrollTop = el.scrollHeight;-- requestAnimationFrame(() => {- requestAnimationFrame(() => {- reasoningAutoScrollingRef.current = false;- });- });- };-- useEffect(() => {- if (isReasoningVisible) stickReasoningToBottom();- }, [visibleChainOfThought, isReasoningVisible]);-- useEffect(() => {- const el = reasoningScrollRef.current;- if (!isReasoningVisible || !el || typeof ResizeObserver === "undefined") return;- const observer = new ResizeObserver(() => stickReasoningToBottom());- if (el.firstElementChild) observer.observe(el.firstElementChild);- return () => observer.disconnect();- }, [isReasoningVisible]);-- useEffect(() => {- const shouldAutoOpen =- !!visibleChainOfThought &&- !visibleResponseText &&- !!isUiStreaming &&- !isReasoningVisible &&- !userToggledRef.current;- if (shouldAutoOpen) {- setIsReasoningVisible(true);- autoOpenedByStreamRef.current = true;- }- }, [visibleChainOfThought, visibleResponseText, isUiStreaming, isReasoningVisible]);const handleMessageCopy = async () => {if (!messageRef.current) {@@ -152,13 +138,23 @@ const AssistantMessage: React.FC<MessageProps> = ({const messageClone = messageRef.current.cloneNode(true) as HTMLDivElement;- const buttons = messageClone.querySelectorAll('button, .copy-button, [class*="copy"]');+ const buttons = messageClone.querySelectorAll('button, .copy-button, [class*="copy"], [data-tool-call]');buttons.forEach((button) => button.remove());+ // A cloned canvas has no bitmap. Copy the chart's readable data instead.+ messageClone.querySelectorAll("canvas").forEach((canvas) => {+ const table = canvas.querySelector("table");+ if (table) canvas.replaceWith(table);+ });+ const plainClone = messageClone.cloneNode(true) as HTMLDivElement;+ plainClone.querySelectorAll("table").forEach((table) => {+ const rows = Array.from(table.rows, (row) => Array.from(row.cells, (cell) => cell.textContent).join("\t"));+ table.replaceWith(document.createTextNode(`\n${rows.join("\n")}\n`));+ });await writeItemsWithFocus([new ClipboardItem({"text/html": new Blob([messageClone.innerHTML], { type: "text/html" }),- "text/plain": new Blob([messageClone.textContent || ""], {+ "text/plain": new Blob([plainClone.textContent || ""], {type: "text/plain",}),}),@@ -175,13 +171,6 @@ const AssistantMessage: React.FC<MessageProps> = ({const anyStreamInProgress = !!(isLoading || isUiStreaming);const isStreamFinished = !(isLast && isUiStreaming);- const modelMenuContent = (- <>- <ModelMenuIcon />- <span className="text-[13px]">Model</span>- </>- );-const totalVersions = versions ? versions.length : 1;const hasVersions = totalVersions > 1;const effectivePerf = hasVersions ? versions?.[currentVersionIndex || 0]?.perf : messageFromStore?.perf;@@ -203,93 +192,86 @@ const AssistantMessage: React.FC<MessageProps> = ({return (<div className="pb-3 text-label-title relative w-full rounded-[5px] text-[15px] leading-[140%]">- {visibleChainOfThought && (- <button- onClick={() => {- userToggledRef.current = true;- setIsReasoningVisible((v) => !v);- }}- className={twMerge(- "group mb-1 px-4 pt-4 border border-cell-border w-full rounded-[8px] transition-colors duration-150",- "hover:[background-color:rgba(0,0,0,0.01)] dark:hover:[background-color:rgba(255,255,255,0.01)]",- )}- >- <div className="flex pb-4 items-center justify-between p-0 group hover:bg-transparent text-label-title">- <span className="flex items-center gap-2">- <ThinkingBubbleIcon className="w-[14px] h-[14px]" />-- <span className="text-[13px] font-[400] leading-[130%]">thinking...</span>- </span>- <ChevronDownIcon- className={twMerge(- "w-[12px] h-[12px] text-label-muted transition-transform transition-colors duration-150",- isReasoningVisible && "rotate-180",- )}- />- </div>- <Transition- show={isReasoningVisible}- appear- enter="transition-all duration-200 ease-out"- enterFrom="opacity-0 -translate-y-1"- enterTo="opacity-100 translate-y-0"- leave="transition-all duration-150 ease-in"- leaveFrom="opacity-100 translate-y-0"- leaveTo="opacity-0 -translate-y-1"- >- <div className="relative contain-layout">- <div- ref={reasoningScrollRef}- onScroll={onReasoningScroll}- className="pb-4 text-left max-h-[180px] overflow-y-auto thin-scrollbar"- >- <MarkdownRenderer- content={visibleChainOfThought || ""}- useOneFontSize={true}- className="text-[12px] leading-[150%] font-mono text-label-muted [&_p]:mb-1 [&_p]:mt-1"- />- </div>- <div className="group-hover:pointer-events-none opacity-100 group-hover:opacity-0 transition-opacity duration-150 absolute bottom-0 left-0 right-0 h-[96px] z-10 [background:linear-gradient(180deg,rgba(255,255,255,0)_0%,#FFFFFF_100%)] dark:[background:linear-gradient(180deg,rgba(10,10,10,0)_0%,#0A0A0A_100%)]" />- </div>- </Transition>- </button>- )}<div ref={messageRef}>+ {transcript !== undefined ? (+ transcript.map((item, index) => {+ const key = `${currentMsgVersion?.id ?? id}:${index}`;+ switch (item.type) {+ case "thinking":+ return (+ <ThinkingBlock+ key={key}+ text={item.text}+ reasoningInProgress={isThinking && !item.completed && index === transcript.length - 1}+ />+ );+ case "text":+ return (+ <MarkdownRenderer+ key={key}+ content={item.text}+ streaming={isThinking && index === transcript.length - 1}+ />+ );+ case "toolCall":+ return (+ <div+ key={key}+ data-tool-call+ className="mt-1 mb-2 flex items-center gap-2 pl-1 text-[13px] text-label-muted"+ >+ <WrenchIcon className="size-3.5 shrink-0" />+ <span>{toolCallLabel(item, isThinking)}</span>+ </div>+ );+ case "chart":+ return (+ <Suspense key={key} fallback={<div className="my-3 h-96 text-label-muted">Loading chart...</div>}>+ <ChartMessage chart={item.chart} />+ </Suspense>+ );+ }+ })+ ) : (+ <>+ {visibleChainOfThought && (+ <ThinkingBlock+ key={currentMsgVersion?.id ?? id}+ text={visibleChainOfThought}+ reasoningInProgress={reasoningInProgress}+ isThinking={isThinking}+ />+ )}+ <MarkdownRenderer content={visibleResponseText} streaming={streamInProgress} />+ </>+ )}{hasError && (- <div className="p-3 rounded-md border border-error/30 bg-error/10">+ <div role="alert" className="mt-4 p-3 rounded-md border border-error/30 bg-error/10"><span className="text-[13px] leading-[150%] text-error">{errorText}</span></div>)}- <MarkdownRenderer content={visibleResponseText} streaming={streamInProgress} /></div>{(hasVersions ||(!streamInProgress && (isStreamFinished || isCanceled || text.length === 0 || !!visibleChainOfThought))) && (- <div className="flex justify-between items-center mt-6 pr-6">- <div className="flex items-center gap-1">- {hasVersions && (- <MessageVersionControls- currentVersion={currentVersionIndex}- totalVersions={totalVersions}- onVersionChange={handleVersionChange}- disabledPrevious={anyStreamInProgress}- currentModelName={currentVersionModelName || effectiveModelName || ""}- />- )}- {(visibleResponseText.length !== 0 || hasError) && (- <CopyButton className="!min-w-6 !min-h-6" onCopy={handleMessageCopy} />- )}- </div>- <div className="flex items-center gap-4">- <ModelSelector- selectedModel={effectiveModelId || ""}- onModelSelect={handleModelSelect}- menuContent={modelMenuContent}- disabled={(anyStreamInProgress && hasVersions) || isEjecting || blockedByOtherChat}+ <div className="flex flex-wrap items-center gap-1 mt-4">+ {(visibleResponseText.length !== 0 || hasError || transcript?.some((item) => item.type === "chart")) && (+ <CopyButton className="[&_svg]:size-4" onCopy={handleMessageCopy} />+ )}+ <ModelSelector+ selectedModel={effectiveModelId || ""}+ onModelSelect={handleModelSelect}+ disabled={(anyStreamInProgress && hasVersions) || isEjecting || blockedByOtherChat}+ />+ {shouldShowPerf && <PerformanceDropdown perf={effectivePerf} disabled={anyStreamInProgress && hasVersions} />}+ {hasVersions && (+ <MessageVersionControls+ currentVersion={currentVersionIndex}+ totalVersions={totalVersions}+ onVersionChange={handleVersionChange}+ disabledPrevious={anyStreamInProgress}+ currentModelName={currentVersionModelName || effectiveModelName || ""}/>- {shouldShowPerf && (- <PerformanceDropdown perf={effectivePerf} disabled={anyStreamInProgress && hasVersions} />- )}- </div>+ )}</div>)}</div>@@ -298,7 +280,13 @@ const AssistantMessage: React.FC<MessageProps> = ({const MessageComponent: React.FC<MessageProps> = (props) =>props.sender === "user" ? (- <UserMessage text={props.text} attachmentIds={props.attachmentIds} />+ <UserMessage+ id={props.id}+ text={props.text}+ attachmentIds={props.attachmentIds}+ canEdit={props.canEdit}+ onEdit={props.onEdit}+ />) : (<AssistantMessage {...props} />);diff --git a/apps/mirai-gui/client/src/features/chat/components/message/model-selector.tsx b/apps/mirai-gui/client/src/features/chat/components/message/model-selector.tsxindex cb2ab4e00..b90e6757d 100644--- a/apps/mirai-gui/client/src/features/chat/components/message/model-selector.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/message/model-selector.tsx@@ -1,5 +1,5 @@import { Listbox, ListboxButton, ListboxOption, ListboxOptions, Transition } from "@headlessui/react";-import { ChevronRight } from "lucide-react";+import { ChevronRight, RefreshCw } from "lucide-react";import { useNavigate } from "@tanstack/react-router";import React from "react";import { twMerge } from "tailwind-merge";@@ -10,16 +10,10 @@ import { ModelVendorIcon } from "@/components/model-vendor-icon";type ModelSelectorProps = {selectedModel?: string;onModelSelect?: (modelId: string, modelName: string) => void;- menuContent: React.ReactNode;disabled?: boolean;};-export const ModelSelector: React.FC<ModelSelectorProps> = ({- selectedModel,- onModelSelect,- menuContent,- disabled = false,-}) => {+export const ModelSelector: React.FC<ModelSelectorProps> = ({ selectedModel, onModelSelect, disabled = false }) => {const navigate = useNavigate();const models = useModelsStore((s) => s.models);@@ -39,23 +33,16 @@ export const ModelSelector: React.FC<ModelSelectorProps> = ({navigate({ to: "/local-models" });};- const variantClasses = disabled- ? "text-label-muted rounded-md py-1"- : "text-label-muted group-hover:text-label-title group-hover:bg-bg-hover rounded-md py-1";-return (- <div className="group w-fit">+ <div className="w-fit"><Listbox value={selectedModel || ""} onChange={handleModelSelect} disabled={disabled}><div><ListboxButton- className={twMerge(- "flex items-center px-[6px] transition-colors focus:outline-hidden focus:ring-0",- "gap-2",- variantClasses,- disabled ? "opacity-60 cursor-not-allowed" : undefined,- )}+ aria-label="Regenerate with model"+ title="Regenerate with model"+ className="flex size-8 items-center justify-center rounded-md text-label-muted transition-colors enabled:hover:bg-bg-hover enabled:hover:text-label-title focus:outline-hidden focus-visible:shadow-focus disabled:opacity-60 disabled:cursor-not-allowed">- {menuContent}+ <RefreshCw className="size-4" aria-hidden="true" /></ListboxButton><Transition@@ -67,8 +54,8 @@ export const ModelSelector: React.FC<ModelSelectorProps> = ({leaveTo="transform scale-95 opacity-0"><ListboxOptions- anchor="bottom end"- className="thin-scrollbar [--anchor-gap:8px] [--anchor-max-height:360px] bg-bg-modal border border-cell-border rounded-[6px] z-50 pointer-events-auto focus:outline-hidden focus:ring-0"+ anchor="bottom start"+ className="overscroll-y-contain thin-scrollbar [--anchor-gap:8px] [--anchor-max-height:360px] bg-bg-modal border border-cell-border rounded-[6px] z-50 pointer-events-auto focus:outline-hidden focus:ring-0"><div className="p-[6px] flex flex-col gap-2">{loadingModels ? (@@ -105,7 +92,7 @@ export const ModelSelector: React.FC<ModelSelectorProps> = ({onClick={handleMoreModelsClick}className="w-full flex items-center justify-between px-[14px] py-2 rounded-md hover:bg-bg-hover transition-colors text-label-title focus:outline-hidden focus:ring-0">- <span className="text-sm text-label-title">More local models</span>+ <span className="text-sm text-label-title">More models</span><ChevronRight className="h-4 w-4 text-label-muted" /></button></div>diff --git a/apps/mirai-gui/client/src/features/chat/components/message/performance-dropdown.tsx b/apps/mirai-gui/client/src/features/chat/components/message/performance-dropdown.tsxindex 22979c806..551ee9d81 100644--- a/apps/mirai-gui/client/src/features/chat/components/message/performance-dropdown.tsx+++ b/apps/mirai-gui/client/src/features/chat/components/message/performance-dropdown.tsx@@ -1,6 +1,5 @@import { Popover, PopoverButton, PopoverPanel, Transition } from "@headlessui/react";-import { twMerge } from "tailwind-merge";-import { PerformanceIcon } from "@/components/icons/performance-icon";+import { Activity } from "lucide-react";import type { PerfStats } from "@/types/message";type PerformanceDropdownProps = {@@ -14,11 +13,6 @@ const formatSeconds = (seconds?: number): string =>const formatTps = (tps?: number): string =>typeof tps === "number" && Number.isFinite(tps) ? `${Math.round(tps)}` : "—";-const BUTTON_BASE =- "flex items-center px-[6px] transition-colors focus:outline-hidden focus:ring-0 gap-2 text-label-muted rounded-md py-1";-const BUTTON_ENABLED = "group-hover:text-label-title group-hover:bg-bg-hover";-const BUTTON_DISABLED = "opacity-60 cursor-not-allowed";-function PerfStat({ value, label }: { value: string; label: string }) {return (<div className="flex flex-col items-center">@@ -30,10 +24,14 @@ function PerfStat({ value, label }: { value: string; label: string }) {export function PerformanceDropdown({ perf, disabled = false }: PerformanceDropdownProps) {return (- <Popover className="group w-fit">- <PopoverButton disabled={disabled} className={twMerge(BUTTON_BASE, disabled ? BUTTON_DISABLED : BUTTON_ENABLED)}>- <PerformanceIcon />- <span className="text-[13px]">Performance</span>+ <Popover className="w-fit">+ <PopoverButton+ disabled={disabled}+ aria-label="Performance"+ title="Performance"+ className="flex size-8 items-center justify-center rounded-md text-label-muted transition-colors enabled:hover:bg-bg-hover enabled:hover:text-label-title focus:outline-hidden focus-visible:shadow-focus disabled:opacity-60 disabled:cursor-not-allowed"+ >+ <Activity className="size-4" aria-hidden="true" /></PopoverButton><Transitionenter="transition duration-100 ease-out"@@ -44,7 +42,7 @@ export function PerformanceDropdown({ perf, disabled = false }: PerformanceDropdleaveTo="transform scale-95 opacity-0"><PopoverPanel- anchor="bottom end"+ anchor="top start"className="[--anchor-gap:8px] bg-bg-modal border border-cell-border rounded-[6px] z-50 pointer-events-auto focus:outline-hidden focus:ring-0"><div className="p-[10px]">diff --git a/apps/mirai-gui/client/src/features/chat/components/message/thinking-block.test.tsx b/apps/mirai-gui/client/src/features/chat/components/message/thinking-block.test.tsxnew file mode 100644index 000000000..86e0b830f--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/components/message/thinking-block.test.tsx@@ -0,0 +1,155 @@+import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { ThinkingBlock } from "./thinking-block";++vi.mock("./markdown-renderer", () => ({+ MarkdownRenderer: ({ content }: { content: string }) => <div>{content}</div>,+}));++let observers: Set<{ elements: Set<Element>; callback: () => void }>;+const originalGetAnimations = Object.getOwnPropertyDescriptor(Element.prototype, "getAnimations");++beforeEach(() => {+ Object.defineProperty(Element.prototype, "getAnimations", { configurable: true, value: () => [] });+ observers = new Set();+ vi.stubGlobal(+ "ResizeObserver",+ class {+ elements = new Set<Element>();+ constructor(public callback: () => void) {+ observers.add(this);+ }+ observe(element: Element) {+ this.elements.add(element);+ }+ disconnect() {+ observers.delete(this);+ }+ },+ );+});+afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+ if (originalGetAnimations) Object.defineProperty(Element.prototype, "getAnimations", originalGetAnimations);+ else delete (Element.prototype as Partial<Element>).getAnimations;+});++const resize = (element: Element) =>+ act(() => {+ observers.forEach((observer) => {+ if (observer.elements.has(element)) observer.callback();+ });+ });++const settleScrolling = () =>+ act(+ () =>+ new Promise<void>((resolve) => {+ requestAnimationFrame(() => requestAnimationFrame(() => resolve()));+ }),+ );++const measure = (element: HTMLDivElement, height: number, contentHeight: number) => {+ const geometry = { height, contentHeight, top: 0 };+ Object.defineProperties(element, {+ clientHeight: { get: () => geometry.height },+ scrollHeight: { get: () => geometry.contentHeight },+ scrollTop: {+ get: () => geometry.top,+ set: (value: number) => {+ geometry.top = Math.max(0, Math.min(value, geometry.contentHeight - geometry.height));+ },+ },+ });+ return geometry;+};++const renderBlock = async (height = 180, contentHeight = 600) => {+ const view = render(<ThinkingBlock text="Reasoning" reasoningInProgress />);+ const scroller = view.container.querySelector<HTMLDivElement>(".overflow-y-auto")!;+ const geometry = measure(scroller, height, contentHeight);+ resize(scroller);+ await settleScrolling();+ const fade = (edge: "top" | "bottom") => {+ const overlay = view.container.querySelector<HTMLDivElement>(`[data-reasoning-fade="${edge}"]`)!;+ return { height: Number.parseFloat(overlay.style.height), opacity: Number.parseFloat(overlay.style.opacity) };+ };+ const scroll = (top: number) => {+ scroller.scrollTop = top;+ fireEvent.scroll(scroller);+ };+ return { view, scroller, geometry, fade, scroll };+};++it("keeps a subtle end fade for fully visible text without fading the top", async () => {+ const { fade } = await renderBlock(100, 100);+ expect(fade("top")).toEqual({ height: 0, opacity: 0 });+ expect(fade("bottom").height).toBe(21);+ expect(fade("bottom").opacity).toBeCloseTo(1 / 3);+});++it("ramps each clipped edge over 32 hidden pixels and saturates at 42 px and two-thirds opacity", async () => {+ const { fade, scroll } = await renderBlock();+ expect(fade("top").height).toBe(42);+ expect(fade("top").opacity).toBeCloseTo(2 / 3);+ expect(fade("bottom").height).toBe(21);+ expect(fade("bottom").opacity).toBeCloseTo(1 / 3);++ scroll(0);+ expect(fade("top")).toEqual({ height: 0, opacity: 0 });+ expect(fade("bottom").height).toBe(42);+ expect(fade("bottom").opacity).toBeCloseTo(2 / 3);+ scroll(16);+ expect(fade("top").height).toBe(21);+ expect(fade("top").opacity).toBeCloseTo(1 / 3);+ scroll(404);+ expect(fade("bottom")).toEqual({ height: 31.5, opacity: 0.5 });+});++it("follows appended reasoning only while pinned and refreshes fades when unpinned", async () => {+ const { view, scroller, geometry, fade, scroll } = await renderBlock();+ geometry.contentHeight = 700;+ view.rerender(<ThinkingBlock text="More reasoning" reasoningInProgress />);+ expect(scroller.scrollTop).toBe(520);+ expect(fade("bottom").height).toBe(21);+ await settleScrolling();++ scroll(496);+ expect(fade("bottom").height).toBe(36.75);+ geometry.contentHeight = 800;+ view.rerender(<ThinkingBlock text="Further reasoning" reasoningInProgress />);+ expect(scroller.scrollTop).toBe(496);+ expect(fade("bottom").height).toBe(42);+ expect(fade("bottom").opacity).toBeCloseTo(2 / 3);+});++it("tracks content and viewport resizes and caps each fade at half the viewport", async () => {+ const { scroller, geometry, fade, scroll } = await renderBlock();+ geometry.contentHeight = 700;+ resize(scroller.firstElementChild!);+ expect(scroller.scrollTop).toBe(520);+ expect(fade("bottom").height).toBe(21);+ await settleScrolling();++ scroll(100);+ geometry.height = 40;+ resize(scroller);+ expect(scroller.scrollTop).toBe(100);+ expect(fade("top").height).toBe(20);+ expect(fade("bottom").height).toBe(20);+});++it("recalculates fades after a completed block is reopened", async () => {+ const { view, scroll, fade } = await renderBlock();+ scroll(0);+ view.rerender(<ThinkingBlock text="Reasoning" reasoningInProgress={false} />);+ await waitFor(() => expect(view.container.querySelector(".overflow-y-auto")).toBeNull());+ fireEvent.click(screen.getByRole("button", { name: "Thinking" }));+ const scroller = view.container.querySelector<HTMLDivElement>(".overflow-y-auto")!;+ measure(scroller, 100, 240);+ resize(scroller);+ expect(fade("top")).toEqual({ height: 0, opacity: 0 });+ expect(fade("bottom").height).toBe(42);+ expect(fade("bottom").opacity).toBeCloseTo(2 / 3);+});diff --git a/apps/mirai-gui/client/src/features/chat/components/message/thinking-block.tsx b/apps/mirai-gui/client/src/features/chat/components/message/thinking-block.tsxnew file mode 100644index 000000000..453496c4c--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/components/message/thinking-block.tsx@@ -0,0 +1,158 @@+import { Transition } from "@headlessui/react";+import { ChevronDownIcon } from "lucide-react";+import { useCallback, useEffect, useRef, useState, type UIEvent } from "react";+import { twMerge } from "tailwind-merge";+import { ThinkingBubbleIcon } from "@/components/icons/thinking-bubble";+import { MarkdownRenderer } from "./markdown-renderer";++type Props = { text: string; reasoningInProgress: boolean; isThinking?: boolean };++const FADE_RAMP_PX = 32;+const MAX_FADE_HEIGHT_PX = 42;+const END_FADE_HEIGHT_PX = MAX_FADE_HEIGHT_PX / 2;++export const ThinkingBlock = ({ text, reasoningInProgress, isThinking = reasoningInProgress }: Props) => {+ const [isReasoningVisible, setIsReasoningVisible] = useState(false);+ const wasReasoningInProgressRef = useRef(false);+ const userToggledRef = useRef(false);++ const [reasoningScrollElement, setReasoningScrollElement] = useState<HTMLDivElement | null>(null);++ const reasoningPinnedRef = useRef(true);++ const reasoningAutoScrollingRef = useRef(false);+ const [fades, setFades] = useState({ top: 0, bottom: 0, viewportHeight: 0 });++ const updateFades = useCallback((el: HTMLDivElement) => {+ const viewportHeight = el.clientHeight;+ const overflow = Math.max(0, el.scrollHeight - viewportHeight);+ const hiddenTop = Math.max(0, Math.min(el.scrollTop, overflow));+ const top = Math.min(hiddenTop / FADE_RAMP_PX, 1);+ const bottom = Math.min((overflow - hiddenTop) / FADE_RAMP_PX, 1);+ setFades((current) =>+ current.top === top && current.bottom === bottom && current.viewportHeight === viewportHeight+ ? current+ : { top, bottom, viewportHeight },+ );+ }, []);++ const onReasoningScroll = (e: UIEvent<HTMLDivElement>) => {+ const el = e.currentTarget;+ updateFades(el);+ if (reasoningAutoScrollingRef.current) return;+ reasoningPinnedRef.current = el.scrollHeight - el.scrollTop - el.clientHeight < 24;+ };++ const updateReasoningScroll = useCallback(() => {+ const el = reasoningScrollElement;+ if (!el) return;+ if (reasoningPinnedRef.current) {+ reasoningAutoScrollingRef.current = true;+ el.scrollTop = el.scrollHeight;++ requestAnimationFrame(() => {+ requestAnimationFrame(() => {+ reasoningAutoScrollingRef.current = false;+ });+ });+ }+ updateFades(el);+ }, [reasoningScrollElement, updateFades]);++ useEffect(() => {+ if (isReasoningVisible) updateReasoningScroll();+ }, [text, isReasoningVisible, updateReasoningScroll]);++ useEffect(() => {+ const el = reasoningScrollElement;+ if (!isReasoningVisible || !el || typeof ResizeObserver === "undefined") return;+ const observer = new ResizeObserver(updateReasoningScroll);+ observer.observe(el);+ if (el.firstElementChild) observer.observe(el.firstElementChild);+ return () => observer.disconnect();+ }, [isReasoningVisible, reasoningScrollElement, updateReasoningScroll]);++ useEffect(() => {+ if (wasReasoningInProgressRef.current && !reasoningInProgress) {+ setIsReasoningVisible(false);+ } else if (reasoningInProgress && text && !userToggledRef.current) {+ setIsReasoningVisible(true);+ }+ wasReasoningInProgressRef.current = reasoningInProgress;+ }, [reasoningInProgress, text]);++ return (+ <button+ data-message-reasoning+ aria-expanded={isReasoningVisible}+ onClick={() => {+ userToggledRef.current = true;+ setIsReasoningVisible((v) => !v);+ }}+ className={twMerge(+ "group mb-1 px-3 pt-2 border border-cell-border w-full rounded-[8px] transition-colors duration-150",+ "hover:[background-color:rgba(0,0,0,0.01)] dark:hover:[background-color:rgba(255,255,255,0.01)]",+ )}+ >+ <div className="flex pb-2 items-center justify-between p-0 group hover:bg-transparent text-label-title">+ <span className="flex items-center gap-2">+ <ThinkingBubbleIcon className="w-[14px] h-[14px]" />++ <span className="text-[13px] font-[400] leading-[130%]">{isThinking ? "Thinking..." : "Thinking"}</span>+ </span>+ <ChevronDownIcon+ className={twMerge(+ "w-[12px] h-[12px] text-label-muted transition-transform transition-colors duration-150",+ isReasoningVisible && "rotate-180",+ )}+ />+ </div>+ <Transition+ show={isReasoningVisible}+ appear+ enter="transition-[grid-template-rows] duration-200 ease-in-out motion-reduce:transition-none"+ enterFrom="grid-rows-[0fr]"+ enterTo="grid-rows-[1fr]"+ leave="transition-[grid-template-rows] duration-200 ease-in-out motion-reduce:transition-none"+ leaveFrom="grid-rows-[1fr]"+ leaveTo="grid-rows-[0fr]"+ >+ <div className="grid">+ <div className="min-h-0 overflow-clip">+ <div className="relative contain-layout">+ <div+ ref={setReasoningScrollElement}+ onScroll={onReasoningScroll}+ className="pb-2 text-left max-h-[180px] overflow-y-auto overscroll-y-auto thin-scrollbar"+ >+ <MarkdownRenderer+ content={text || ""}+ useOneFontSize={true}+ className="text-[12px] leading-[150%] font-mono text-label-muted [&_p]:mb-1 [&_p]:mt-1"+ />+ </div>+ <div+ data-reasoning-fade="top"+ aria-hidden+ style={{+ height: Math.min(MAX_FADE_HEIGHT_PX * fades.top, fades.viewportHeight / 2),+ opacity: (2 / 3) * fades.top,+ }}+ className="pointer-events-none absolute top-0 inset-x-0 z-10 [background:linear-gradient(180deg,#FFFFFF_0%,rgba(255,255,255,0)_100%)] dark:[background:linear-gradient(180deg,#0A0A0A_0%,rgba(10,10,10,0)_100%)]"+ />+ <div+ data-reasoning-fade="bottom"+ aria-hidden+ style={{+ height: Math.min(END_FADE_HEIGHT_PX + END_FADE_HEIGHT_PX * fades.bottom, fades.viewportHeight / 2),+ opacity: (1 + fades.bottom) / 3,+ }}+ className="pointer-events-none absolute bottom-0 inset-x-0 z-10 [background:linear-gradient(180deg,rgba(255,255,255,0)_0%,#FFFFFF_100%)] dark:[background:linear-gradient(180deg,rgba(10,10,10,0)_0%,#0A0A0A_100%)]"+ />+ </div>+ </div>+ </div>+ </Transition>+ </button>+ );+};diff --git a/apps/mirai-gui/client/src/features/chat/components/message/user-message.test.tsx b/apps/mirai-gui/client/src/features/chat/components/message/user-message.test.tsxnew file mode 100644index 000000000..4b648bd64--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/components/message/user-message.test.tsx@@ -0,0 +1,157 @@+import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import type { AttachedFile } from "@/types/files";+import { UserMessage, type UserMessageProps } from "./user-message";++const mocks = vi.hoisted(() => ({+ writeItemsWithFocus: vi.fn<(items: ClipboardItem[]) => Promise<void>>(async () => {}),+ getFiles: vi.fn<() => AttachedFile[]>(() => []),+}));+vi.mock("@/utils/clipboard", () => ({ writeItemsWithFocus: mocks.writeItemsWithFocus }));+vi.mock("../../services/attachment-storage", () => ({ attachmentStorage: { getFiles: mocks.getFiles } }));++class TestClipboardItem {+ constructor(public items: Record<string, Blob>) {}+}++const originalText = " **original**\n\n[link](https://example.com) ";+const onEdit = vi.fn<NonNullable<UserMessageProps["onEdit"]>>(async (_id, _text, onSaved) => onSaved());+const props = { id: "user-message", text: originalText, canEdit: true, onEdit };+const edit = () => fireEvent.click(screen.getByRole("button", { name: "Edit message" }));+const draft = () => screen.getByRole("textbox", { name: "Edit message" }) as HTMLTextAreaElement;+const save = () => fireEvent.click(screen.getByRole("button", { name: "Save & send" }));++beforeEach(() => {+ vi.clearAllMocks();+ vi.stubGlobal("ClipboardItem", TestClipboardItem);+ mocks.getFiles.mockReturnValue([]);+ onEdit.mockImplementation(async (_id, _text, onSaved) => onSaved());+});+afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+});++it("renders pasted tables, spacing, and Markdown syntax as unchanged plain text", () => {+ const text =+ " Model Prefill Decode\n Qwen 123.4 56.7\n\n\t**Raw** | `text`\n <b>literal</b> \n";+ render(<UserMessage {...props} text={text} />);++ const message = screen.getByText(text, { normalizer: (value) => value });+ expect(message.textContent).toBe(text);+ expect(message.childNodes).toHaveLength(1);+ expect(message.firstChild?.nodeType).toBe(Node.TEXT_NODE);+});++it("copies the exact original text, including Markdown and whitespace", async () => {+ render(<UserMessage {...props} />);+ fireEvent.click(screen.getByRole("button", { name: "Copy" }));+ await waitFor(() => expect(mocks.writeItemsWithFocus).toHaveBeenCalledOnce());+ const item = mocks.writeItemsWithFocus.mock.calls[0]?.[0][0] as unknown as TestClipboardItem;+ expect(Object.keys(item.items)).toEqual(["text/plain"]);+ const copiedText = await new Promise<string>((resolve) => {+ const reader = new FileReader();+ reader.onload = () => resolve(reader.result as string);+ reader.readAsText(item.items["text/plain"]!);+ });+ expect(copiedText).toBe(originalText);+});++it("edits raw text, preserves attachments, and discards the draft on Cancel", () => {+ mocks.getFiles.mockReturnValue([+ { id: "file", name: "notes.txt", extension: "txt", content: "notes", mimeType: "text/plain", size: 5 },+ ]);+ render(<UserMessage {...props} attachmentIds={["file"]} />);+ expect(screen.getByText("notes")).toBeTruthy();+ edit();+ expect(draft().value).toBe(originalText);+ expect(document.activeElement).toBe(draft());+ expect(screen.getByText("notes")).toBeTruthy();+ expect(screen.getByText("Saving removes all later messages.")).toBeTruthy();+ fireEvent.change(draft(), { target: { value: "Discard this" } });+ fireEvent.click(screen.getByRole("button", { name: "Cancel" }));+ expect(screen.queryByRole("textbox")).toBeNull();+ expect(onEdit).not.toHaveBeenCalled();+ edit();+ expect(draft().value).toBe(originalText);+});++it("cancels on Escape without saving", () => {+ render(<UserMessage {...props} />);+ edit();+ fireEvent.keyDown(draft(), { key: "Escape" });+ expect(screen.queryByRole("textbox")).toBeNull();+ expect(onEdit).not.toHaveBeenCalled();+});++it.each(["metaKey", "ctrlKey"])("saves exact edited text with %s + Enter", async (modifier) => {+ render(<UserMessage {...props} />);+ edit();+ fireEvent.change(draft(), { target: { value: " revised\nquestion " } });+ fireEvent.keyDown(draft(), { key: "Enter" });+ expect(onEdit).not.toHaveBeenCalled();+ fireEvent.keyDown(draft(), { key: "Enter", [modifier]: true });+ await waitFor(() => expect(screen.queryByRole("textbox")).toBeNull());+ expect(onEdit).toHaveBeenCalledWith(props.id, " revised\nquestion ", expect.any(Function));+});++it("prevents duplicate submissions and closes after storage succeeds while generation continues", async () => {+ let finishGeneration!: () => void;+ let markSaved!: () => void;+ onEdit.mockImplementation((_id, _text, onSaved) => {+ markSaved = onSaved;+ return new Promise((resolve) => {+ finishGeneration = resolve;+ });+ });+ render(<UserMessage {...props} />);+ edit();+ save();+ expect((screen.getByRole("button", { name: "Saving..." }) as HTMLButtonElement).disabled).toBe(true);+ expect((screen.getByRole("button", { name: "Cancel" }) as HTMLButtonElement).disabled).toBe(true);+ fireEvent.keyDown(draft(), { key: "Enter", metaKey: true });+ expect(onEdit).toHaveBeenCalledOnce();+ act(markSaved);+ expect(screen.queryByRole("textbox")).toBeNull();+ await act(async () => finishGeneration());+});++it("retains the draft after a save failure and allows retrying", async () => {+ onEdit.mockRejectedValueOnce(new Error("Disk full"));+ render(<UserMessage {...props} />);+ edit();+ fireEvent.change(draft(), { target: { value: "Keep this draft" } });+ save();+ await waitFor(() => expect(screen.getByRole("alert").textContent).toBe("Disk full"));+ expect(draft().value).toBe("Keep this draft");+ save();+ await waitFor(() => expect(screen.queryByRole("textbox")).toBeNull());+ expect(onEdit).toHaveBeenCalledTimes(2);+});++it("rejects blank text unless the message retains attachments", async () => {+ const view = render(<UserMessage {...props} />);+ edit();+ fireEvent.change(draft(), { target: { value: " \n " } });+ expect((screen.getByRole("button", { name: "Save & send" }) as HTMLButtonElement).disabled).toBe(true);+ fireEvent.keyDown(draft(), { key: "Enter", ctrlKey: true });+ expect(onEdit).not.toHaveBeenCalled();+ view.rerender(<UserMessage {...props} attachmentIds={["file"]} />);+ expect((screen.getByRole("button", { name: "Save & send" }) as HTMLButtonElement).disabled).toBe(false);+ save();+ await waitFor(() => expect(onEdit).toHaveBeenCalledWith(props.id, " \n ", expect.any(Function)));+});++it("disables editing and saving when another operation prevents sending", () => {+ const view = render(<UserMessage {...props} canEdit={false} />);+ expect((screen.getByRole("button", { name: "Edit message" }) as HTMLButtonElement).disabled).toBe(true);+ expect((screen.getByRole("button", { name: "Copy" }) as HTMLButtonElement).disabled).toBe(false);+ view.rerender(<UserMessage {...props} />);+ edit();+ view.rerender(<UserMessage {...props} canEdit={false} />);+ expect((screen.getByRole("button", { name: "Save & send" }) as HTMLButtonElement).disabled).toBe(true);+ fireEvent.keyDown(draft(), { key: "Enter", metaKey: true });+ expect(onEdit).not.toHaveBeenCalled();+ fireEvent.click(screen.getByRole("button", { name: "Cancel" }));+ expect(screen.queryByRole("textbox")).toBeNull();+});diff --git a/apps/mirai-gui/client/src/features/chat/components/message/user-message.tsx b/apps/mirai-gui/client/src/features/chat/components/message/user-message.tsxnew file mode 100644index 000000000..2fafa429a--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/components/message/user-message.tsx@@ -0,0 +1,136 @@+import { CopyButton } from "@/components/ui/copy-button";+import { Button } from "@/components/ui/button";+import { TextArea } from "@/components/ui/text-area";+import { writeItemsWithFocus } from "@/utils/clipboard";+import { Pencil } from "lucide-react";+import { useId, useRef, useState } from "react";+import { attachmentStorage } from "../../services/attachment-storage";+import { AttachedFilesDisplay } from "../composer/attached-files-display";++export type UserMessageProps = {+ id: string;+ text: string;+ attachmentIds?: string[];+ canEdit?: boolean;+ onEdit?: (messageId: string, text: string, onSaved: () => void) => Promise<void>;+};++export const UserMessage = ({ id, text, attachmentIds, canEdit = false, onEdit }: UserMessageProps) => {+ const [editing, setEditing] = useState(false);+ const [draft, setDraft] = useState(text);+ const [saving, setSaving] = useState(false);+ const [error, setError] = useState<string | null>(null);+ const saveInFlight = useRef(false);+ const descriptionId = useId();+ const editButtonRef = useRef<HTMLButtonElement>(null);+ const hasAttachments = (attachmentIds?.length ?? 0) > 0;+ const canSave = canEdit && !!onEdit && !saving && (draft.trim().length > 0 || hasAttachments);++ const copy = () =>+ writeItemsWithFocus([new ClipboardItem({ "text/plain": new Blob([text], { type: "text/plain" }) })]);++ const startEditing = () => {+ if (!canEdit || saveInFlight.current) return;+ setDraft(text);+ setError(null);+ setEditing(true);+ };++ const cancel = () => {+ if (saveInFlight.current) return;+ setEditing(false);+ requestAnimationFrame(() => editButtonRef.current?.focus());+ };++ const save = async () => {+ if (!canSave || !onEdit || saveInFlight.current) return;+ saveInFlight.current = true;+ setSaving(true);+ setError(null);+ let saved = false;+ try {+ await onEdit(id, draft, () => {+ saved = true;+ setEditing(false);+ setSaving(false);+ });+ } catch (cause) {+ if (!saved) setError(cause instanceof Error ? cause.message : "Could not save changes. Please try again.");+ } finally {+ saveInFlight.current = false;+ setSaving(false);+ }+ };++ return (+ <div className="group/user">+ {hasAttachments && (+ <div className="mb-3 ml-auto w-fit">+ <AttachedFilesDisplay files={attachmentStorage.getFiles(attachmentIds ?? [])} />+ </div>+ )}+ {editing ? (+ <div className="ml-auto flex w-full flex-col gap-3 rounded-lg bg-bg-hover p-3">+ <TextArea+ aria-label="Edit message"+ aria-describedby={descriptionId}+ autoFocus+ value={draft}+ onChange={setDraft}+ disabled={saving}+ maxHeightPx={320}+ className="bg-transparent text-[15px] leading-[140%]"+ onKeyDown={(event) => {+ if (event.nativeEvent.isComposing) return;+ if (event.key === "Escape") {+ event.preventDefault();+ cancel();+ } else if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {+ event.preventDefault();+ void save();+ }+ }}+ />+ <p id={descriptionId} className="text-[12px] text-label-muted">+ Saving removes all later messages.+ </p>+ {error && (+ <p role="alert" className="text-[13px] text-error">+ {error}+ </p>+ )}+ <div className="flex justify-end gap-2">+ <Button type="button" size="sm" kind="ghost" disabled={saving} onClick={cancel}>+ Cancel+ </Button>+ <Button type="button" size="sm" disabled={!canSave} onClick={() => void save()}>+ {saving ? "Saving..." : "Save & send"}+ </Button>+ </div>+ </div>+ ) : (+ <>+ <div className="font-[350] text-label-title relative w-fit max-w-full whitespace-pre-wrap [overflow-wrap:anywhere] rounded-[5px] text-[15px] leading-[140%] px-2.5 py-[6px] bg-bg-hover ml-auto">+ {text}+ </div>+ <div className="mt-1 flex h-8 items-center justify-end gap-1 opacity-0 transition-opacity group-hover/user:opacity-100 group-focus-within/user:opacity-100 [@media(hover:none)]:opacity-100">+ <CopyButton className="[&_svg]:size-4" onCopy={copy} />+ {onEdit && (+ <button+ ref={editButtonRef}+ type="button"+ aria-label="Edit message"+ title="Edit message"+ disabled={!canEdit}+ onClick={startEditing}+ className="flex min-h-8 min-w-8 items-center justify-center rounded-[5px] p-[6px] text-label-muted outline-hidden transition-colors hover:bg-card-hover hover:text-label-title focus-visible:shadow-focus disabled:opacity-50"+ >+ <Pencil className="size-4" />+ </button>+ )}+ </div>+ </>+ )}+ </div>+ );+};diff --git a/apps/mirai-gui/client/src/features/chat/hooks/reopen-mid-stream.test.tsx b/apps/mirai-gui/client/src/features/chat/hooks/reopen-mid-stream.test.tsxindex 4879207c5..b5f947229 100644--- a/apps/mirai-gui/client/src/features/chat/hooks/reopen-mid-stream.test.tsx+++ b/apps/mirai-gui/client/src/features/chat/hooks/reopen-mid-stream.test.tsx@@ -12,6 +12,7 @@ import { useLlmStream, type StartStreamOptions } from "./use-llm-stream";const mocks = vi.hoisted(() => ({ chats: new Map<string, ChatData>() }));vi.mock("@/platform/platform-singleton", () => ({getPlatform: () => ({+ settings: { getModelChatNamingEnabled: async () => true },storage: {loadChat: async (id: string) => mocks.chats.get(id) ?? null,listChats: async () => [...mocks.chats.values()].map((c) => c.metadata),@@ -87,17 +88,34 @@ it("keeps streaming into the message after leaving and reopening the chat", asynvoid useChatSessionStore.getState().withOperation("running", () => page.result.current.startStream(options));});await waitFor(() => expect(page.result.current.isStreaming).toBe(true));- act(() => emit({ type: "chunk", delta: "Hello" }));+ act(() => {+ emit({ type: "transcript", items: [{ type: "text", text: "Hel" }] });+ emit({ type: "transcriptDelta", index: 0, delta: "lo" });+ });await waitFor(() => expect(useChatStore.getState().messages.at(-1)?.text).toBe("Hello"));page.rerender({ chatId: CHAT_B });await waitFor(() => expect(useChatStore.getState().currentChatId).toBe(CHAT_B));- act(() => emit({ type: "chunk", delta: " world" }));+ const transcript = [+ { type: "thinking" as const, text: "Check the clock", completed: true },+ { type: "text" as const, text: "Hello" },+ { type: "toolCall" as const, name: "get_current_date_time", called: true },+ { type: "text" as const, text: "world" },+ ];+ act(() => {+ emit({ type: "transcript", items: [...transcript.slice(0, -1), { type: "text", text: "wor" }] });+ emit({ type: "transcriptDelta", index: 3, delta: "ld" });+ });page.rerender({ chatId: CHAT_A });await waitFor(() => expect(useChatStore.getState().messages.map((m) => m.id)).toEqual(["user-1", MESSAGE_ID]));- await waitFor(() => expect(useChatStore.getState().messages.at(-1)?.text).toBe("Hello world"));+ await waitFor(() => expect(useChatStore.getState().messages.at(-1)?.text).toBe("Hello\n\nworld"));+ await waitFor(() => expect(useChatStore.getState().messages.at(-1)?.output?.transcript).toEqual(transcript));+ expect(useChatStore.getState().messages.at(-1)?.output?.text?.parsed).toEqual({+ response: "Hello\n\nworld",+ chainOfThought: "Check the clock",+ });- act(() => emit({ type: "chunk", delta: "!" }));- await waitFor(() => expect(useChatStore.getState().messages.at(-1)?.text).toBe("Hello world!"));+ act(() => emit({ type: "transcriptDelta", index: 3, delta: "!" }));+ await waitFor(() => expect(useChatStore.getState().messages.at(-1)?.text).toBe("Hello\n\nworld!"));});diff --git a/apps/mirai-gui/client/src/features/chat/hooks/use-active-assistant-buffer.ts b/apps/mirai-gui/client/src/features/chat/hooks/use-active-assistant-buffer.tsindex b35c16ca9..a2745e393 100644--- a/apps/mirai-gui/client/src/features/chat/hooks/use-active-assistant-buffer.ts+++ b/apps/mirai-gui/client/src/features/chat/hooks/use-active-assistant-buffer.ts@@ -10,19 +10,24 @@ export const useActiveAssistantBuffer = (chatId: string): (() => void) => {const generatingHere = useChatSessionStore((s) => isChatGenerating(s, chatId));const activeAssistantMessageId = useChatSessionStore((s) => s.activeAssistantMessageId);const activeAssistantMessageText = useChatSessionStore((s) => s.activeAssistantMessageText);+ const activeAssistantMessageOutput = useChatSessionStore((s) => s.activeAssistantMessageOutput);const apply = useCallback(() => {- if (!chatId || !generatingHere || !activeAssistantMessageId || typeof activeAssistantMessageText !== "string")- return;+ if (!chatId || !generatingHere || !activeAssistantMessageId) return;+ if (activeAssistantMessageText === null && activeAssistantMessageOutput === null) return;const s = useChatStore.getState();const msg = s.messages.find((m) => m.id === activeAssistantMessageId);if (!msg) return;const hasVersions = Array.isArray(msg.versions) && msg.versions.length > 0;+ const patch = {+ ...(activeAssistantMessageText !== null ? { text: activeAssistantMessageText } : {}),+ ...(activeAssistantMessageOutput !== null ? { output: activeAssistantMessageOutput } : {}),+ };s.updateMessage(activeAssistantMessageId, {- text: activeAssistantMessageText,- ...(hasVersions ? { versions: patchActiveVersion(msg.versions, { text: activeAssistantMessageText }) } : {}),+ ...patch,+ ...(hasVersions ? { versions: patchActiveVersion(msg.versions, patch) } : {}),});- }, [chatId, generatingHere, activeAssistantMessageId, activeAssistantMessageText]);+ }, [chatId, generatingHere, activeAssistantMessageId, activeAssistantMessageText, activeAssistantMessageOutput]);useEffect(() => {apply();diff --git a/apps/mirai-gui/client/src/features/chat/hooks/use-chat-model-selector.test.tsx b/apps/mirai-gui/client/src/features/chat/hooks/use-chat-model-selector.test.tsxnew file mode 100644index 000000000..d07d9648a--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/hooks/use-chat-model-selector.test.tsx@@ -0,0 +1,69 @@+import { act, cleanup, renderHook } from "@testing-library/react";+import { afterEach, beforeEach, expect, it } from "vitest";+import { useChatStore } from "@/stores/use-chat-store";+import { useModelsStore } from "@/stores/use-models-store";+import { useRuntimeSessionStore } from "@/stores/use-runtime-session-store";+import { modelDownloadPhases } from "@/types/model-manager";+import { ModelKind, type PlatformModel } from "@/types/models";+import { useChatModelSelector } from "./use-chat-model-selector";++const chatDefaults = useChatStore.getState();+const modelsDefaults = useModelsStore.getState();+const runtimeDefaults = useRuntimeSessionStore.getState();+const model: PlatformModel = {+ repoId: "vendor/model",+ name: "Model",+ vendor: "Vendor",+ kind: ModelKind.Text,+ reasoning: { kind: "unsupported" },+ supportsTools: false,+};++beforeEach(() => {+ useChatStore.setState({ ...chatDefaults, lastUsedModel: { modelId: model.repoId, modelName: model.name } }, true);+ useModelsStore.setState({ ...modelsDefaults, initialized: true, hasLoadedModels: true }, true);+ useRuntimeSessionStore.setState(runtimeDefaults, true);+});+afterEach(cleanup);++it("preserves the last used model through partial catalog and storage initialization", () => {+ const { result } = renderHook(() => useChatModelSelector({ chatId: "chat" }));+ expect(result.current.currentModelId).toBe(model.repoId);+ expect(result.current.hasAvailableModels).toBe(false);++ act(() =>+ useModelsStore.setState({+ models: [model],+ catalogComplete: true,+ modelPhasesById: { [model.repoId]: modelDownloadPhases.initializing },+ }),+ );+ expect(result.current.currentModelId).toBe(model.repoId);+ expect(result.current.hasAvailableModels).toBe(false);++ act(() => useModelsStore.setState({ modelPhasesById: { [model.repoId]: modelDownloadPhases.downloaded } }));+ expect(result.current.currentModelId).toBe(model.repoId);+ expect(result.current.hasAvailableModels).toBe(true);+});++it("clears a missing model only once the catalog is complete", () => {+ const { result } = renderHook(() => useChatModelSelector({ chatId: "chat" }));+ expect(result.current.currentModelId).toBe(model.repoId);++ act(() => useModelsStore.setState({ catalogComplete: true }));+ expect(result.current.currentModelId).toBeUndefined();+});++it("clears a selected model after initialization confirms it is not downloaded", () => {+ useModelsStore.setState({ models: [model], modelPhasesById: { [model.repoId]: modelDownloadPhases.initializing } });+ const { result } = renderHook(() => useChatModelSelector({ chatId: "chat" }));+ expect(result.current.currentModelId).toBe(model.repoId);++ act(() =>+ useModelsStore.setState({+ catalogComplete: true,+ modelPhasesById: { [model.repoId]: modelDownloadPhases.notDownloaded },+ }),+ );+ expect(result.current.currentModelId).toBeUndefined();+});diff --git a/apps/mirai-gui/client/src/features/chat/hooks/use-chat-model-selector.ts b/apps/mirai-gui/client/src/features/chat/hooks/use-chat-model-selector.tsindex de8d719e6..850c7f06f 100644--- a/apps/mirai-gui/client/src/features/chat/hooks/use-chat-model-selector.ts+++ b/apps/mirai-gui/client/src/features/chat/hooks/use-chat-model-selector.ts@@ -27,6 +27,7 @@ export function useChatModelSelector(props: UseChatModelSelectorProps): UseChatMconst models = useModelsStore((s) => s.models);const modelPhasesById = useModelsStore((s) => s.modelPhasesById);const localsLoaded = useModelsStore((s) => s.hasLoadedModels);+ const catalogComplete = useModelsStore((s) => s.catalogComplete);const setChatModel = useChatStore((s) => s.setChatModel);const selectedChatModel = useChatStore(@@ -54,9 +55,19 @@ export function useChatModelSelector(props: UseChatModelSelectorProps): UseChatMconst exists = allAvailableModels.some((model) => model.repoId === selectedId);if (exists) return;+ if (modelPhasesById[selectedId] === modelDownloadPhases.initializing || !catalogComplete) return;setChatModel(chatId, "", "");- }, [selectedChatModel.modelId, searchModel, allAvailableModels, chatId, setChatModel, localsLoaded]);+ }, [+ selectedChatModel.modelId,+ searchModel,+ allAvailableModels,+ chatId,+ setChatModel,+ localsLoaded,+ catalogComplete,+ modelPhasesById,+ ]);useEffect(() => {if (autoSelectSuppressed) return;@@ -77,7 +88,12 @@ export function useChatModelSelector(props: UseChatModelSelectorProps): UseChatMif (lastUsedModel?.modelId) {const isAvailable = allAvailableModels.some((model) => model.repoId === lastUsedModel.modelId);- if (!localsLoaded || isAvailable) {+ if (+ !localsLoaded ||+ !catalogComplete ||+ modelPhasesById[lastUsedModel.modelId] === modelDownloadPhases.initializing ||+ isAvailable+ ) {setChatModel(chatId, lastUsedModel.modelId, lastUsedModel.modelName);}}@@ -93,6 +109,8 @@ export function useChatModelSelector(props: UseChatModelSelectorProps): UseChatMlastUsedModel?.modelId,lastUsedModel?.modelName,localsLoaded,+ catalogComplete,+ modelPhasesById,]);const currentModelId = selectedChatModel.modelId || searchModel;diff --git a/apps/mirai-gui/client/src/features/chat/hooks/use-llm-stream.test.tsx b/apps/mirai-gui/client/src/features/chat/hooks/use-llm-stream.test.tsxindex be49d3e0d..594c35054 100644--- a/apps/mirai-gui/client/src/features/chat/hooks/use-llm-stream.test.tsx+++ b/apps/mirai-gui/client/src/features/chat/hooks/use-llm-stream.test.tsx@@ -4,14 +4,29 @@ import { runLlmStream, type RunEvent, type RunTransport } from "@/platform/serviimport { useChatSessionStore } from "@/stores/use-chat-session-store";import { useChatStore } from "@/stores/use-chat-store";import { Roles } from "@/types/chat";-import type { SessionOutputStats } from "@/types/llm-stream";-import { STALL_TIMEOUT_MS, useLlmStream, type StartStreamOptions } from "./use-llm-stream";--const mocks = vi.hoisted(() => ({ cancelRun: vi.fn(async () => {}), updateStoredMessage: vi.fn(async () => {}) }));+import type { SessionOutputStats, TranscriptItem } from "@/types/llm-stream";+import type { Message } from "@/types/message";+import { useLlmStream, type StartStreamOptions } from "./use-llm-stream";++const mocks = vi.hoisted(() => ({+ getModelChatNamingEnabled: vi.fn(async () => true),+ cancelRun: vi.fn(async () => {}),+ updateStoredMessage: vi.fn(async () => {}),+ updateChatTitle: vi.fn<(id: string, name: string, expected?: string) => Promise<void>>(async () => {}),+ loadChat: vi.fn<() => Promise<{ metadata: { title: string }; messages?: Message[] }>>(async () => ({+ metadata: { title: "Untitled" },+ })),+}));vi.mock("@/platform/platform-singleton", () => ({getPlatform: () => ({chat: { cancelRun: mocks.cancelRun },- storage: { updateStoredMessage: mocks.updateStoredMessage },+ settings: { getModelChatNamingEnabled: mocks.getModelChatNamingEnabled },+ storage: {+ updateStoredMessage: mocks.updateStoredMessage,+ loadChat: mocks.loadChat,+ updateChatTitle: mocks.updateChatTitle,+ listChats: async () => [],+ },}),}));@@ -80,10 +95,67 @@ const messageText = () => useChatStore.getState().messages.find((m) => m.id ===describe("useLlmStream", () => {beforeEach(() => {+ vi.clearAllMocks();+ mocks.getModelChatNamingEnabled.mockResolvedValue(true);+ mocks.loadChat.mockResolvedValue({ metadata: { title: "Untitled" } });useChatSessionStore.setState(sessionDefaults, true);useChatStore.setState(chatDefaults, true);});+ it("enforces the global naming switch even with an explicit stream opt-in", async () => {+ mocks.getModelChatNamingEnabled.mockResolvedValue(false);+ const { hook, start, options, emit } = setup();+ const runChatStream = vi.spyOn(useChatStore.getState(), "runChatStream");+ let run!: Promise<unknown>;+ act(() => {+ run = start({ ...options, modelChatNamingEnabled: true });+ });+ await waitFor(() => expect(hook.result.current.isStreaming).toBe(true));+ expect(runChatStream).toHaveBeenCalledWith(expect.objectContaining({ modelChatNamingEnabled: false }));+ expect(mocks.loadChat).not.toHaveBeenCalled();+ act(() => {+ emit({ type: "chatName", name: "Unexpected name" });+ emit({ type: "done", text: "Answer", stats, finishReason: "Stop" });+ });+ await act(() => run);+ expect(mocks.updateChatTitle).not.toHaveBeenCalled();+ });++ it("does not start a native run when its chat operation is canceled while loading the saved title", async () => {+ const { hook, options, callbacks, runId } = setup();+ const runChatStream = vi.spyOn(useChatStore.getState(), "runChatStream");+ let resolveTitle!: (value: { metadata: { title: string } }) => void;+ mocks.loadChat.mockReturnValueOnce(+ new Promise((resolve) => {+ resolveTitle = resolve;+ }),+ );+ let run!: Promise<unknown>;+ act(() => {+ run = useChatSessionStore+ .getState()+ .withOperation("running", (signal) => hook.result.current.startStream({ ...options, signal }), CHAT_ID);+ });+ await waitFor(() => expect(mocks.loadChat).toHaveBeenCalled());+ expect(useChatSessionStore.getState().activeRunId).toBeNull();+ await act(() => useChatSessionStore.getState().cancelActiveRunForChat(CHAT_ID));+ await act(async () => {+ resolveTitle({ metadata: { title: "Untitled" } });+ await run;+ });++ expect(runChatStream).not.toHaveBeenCalled();+ expect(runId()).toBe("");+ expect(callbacks.onDone).toHaveBeenCalledOnce();+ expect(callbacks.onError).not.toHaveBeenCalled();+ expect(hook.result.current.isStreaming).toBe(false);+ expect(useChatSessionStore.getState()).toMatchObject({+ isGenerating: false,+ activeRunId: null,+ operationState: "idle",+ });+ });+it("reveals streamed text, records perf and finalizes the message", async () => {const { hook, start, options, callbacks, finalizeAssistantMessage, emit } = setup();@@ -108,9 +180,15 @@ describe("useLlmStream", () => {const message = useChatStore.getState().messages.find((m) => m.id === MESSAGE_ID);expect(message?.output?.text?.parsed).toEqual({ chainOfThought: "let me think", response: "Hello world" });expect(message?.perf).toEqual({ ttftSec: 0.25, totalSec: 2.25, tps: 20, tokensOut: 40 });- expect(finalizeAssistantMessage).toHaveBeenCalledWith(CHAT_ID, MESSAGE_ID, "Hello world", {- response: "Hello world",- });+ expect(finalizeAssistantMessage).toHaveBeenCalledWith(+ CHAT_ID,+ MESSAGE_ID,+ "Hello world",+ {+ response: "Hello world",+ },+ undefined,+ );expect(callbacks.onFinishReason).toHaveBeenCalledWith("Stop");expect(callbacks.onDone).toHaveBeenCalledTimes(1);expect(callbacks.onError).not.toHaveBeenCalled();@@ -123,6 +201,169 @@ describe("useLlmStream", () => {});});+ it("keeps a tool-only partial reply when canceled and forwards tool preferences", async () => {+ const { hook, start, options, emit } = setup();+ const runChatStream = vi.spyOn(useChatStore.getState(), "runChatStream");+ let run!: Promise<unknown>;+ act(() => {+ run = start({ ...options, dateTimeToolEnabled: false, chartToolEnabled: false });+ });+ await waitFor(() => expect(hook.result.current.isStreaming).toBe(true));+ const transcript: TranscriptItem[] = [{ type: "toolCall", name: "get_current_date_time", called: true }];+ act(() => emit({ type: "transcript", items: transcript }));+ expect(hook.result.current.isLoading).toBe(false);+ expect(useChatStore.getState().messages[0]?.output?.transcript).toEqual(transcript);+ expect(useChatSessionStore.getState().activeAssistantMessageOutput?.transcript).toEqual(transcript);+ await act(() => hook.result.current.cancel());+ await act(() => run);+ expect(runChatStream).toHaveBeenCalledWith(+ expect.objectContaining({ dateTimeToolEnabled: false, chartToolEnabled: false }),+ );+ expect(mocks.updateStoredMessage).toHaveBeenCalledWith(+ CHAT_ID,+ MESSAGE_ID,+ expect.objectContaining({ output: expect.objectContaining({ transcript }) }),+ );+ });++ it.each(["cancel", "error"] as const)(+ "saves incrementally streamed text and reasoning on %s without legacy chunks",+ async (end) => {+ const { hook, start, options, emit } = setup();+ let run!: Promise<unknown>;+ act(() => {+ run = start({+ ...options,+ onError: (id, error) => useChatStore.getState().updateMessage(id, { text: "", error }),+ });+ });+ await waitFor(() => expect(hook.result.current.isStreaming).toBe(true));+ const transcript: TranscriptItem[] = [+ { type: "thinking", text: "Check the clock 🕰️", completed: true },+ { type: "text", text: "Before the tool." },+ { type: "toolCall", name: "get_current_date_time", called: true },+ { type: "text", text: "A long response. ".repeat(100) },+ ];+ const fullText = transcript.flatMap((item) => (item.type === "text" ? [item.text] : [])).join("\n\n");+ act(() => {+ emit({ type: "transcript", items: [{ type: "thinking", text: "Check" }] });+ emit({ type: "transcriptDelta", index: 0, delta: " the clock 🕰️" });+ emit({ type: "transcript", items: [...transcript.slice(0, -1), { type: "text", text: "" }] });+ emit({ type: "transcriptDelta", index: 3, delta: "A long response. ".repeat(100) });+ });+ expect(messageText()).toBe(fullText);+ expect(useChatSessionStore.getState().activeAssistantMessageText).toBe(fullText);+ if (end === "cancel") await act(() => hook.result.current.cancel());+ else act(() => emit({ type: "error", error: "Model failed" }));+ await act(() => run);+ expect(messageText()).toBe(fullText);+ expect(mocks.updateStoredMessage).toHaveBeenCalledWith(+ CHAT_ID,+ MESSAGE_ID,+ expect.objectContaining({+ text: fullText,+ output: expect.objectContaining({+ transcript,+ text: { parsed: { response: fullText, chainOfThought: "Check the clock 🕰️" } },+ }),+ }),+ );+ },+ );++ it("clears superseded parsed reasoning when a snapshot promotes it to visible text", async () => {+ const { hook, start, options, emit } = setup();+ let run!: Promise<unknown>;+ act(() => {+ run = start(options);+ });+ await waitFor(() => expect(hook.result.current.isStreaming).toBe(true));+ act(() => {+ emit({ type: "transcript", items: [{ type: "thinking", text: "Visible" }] });+ emit({ type: "transcriptDelta", index: 0, delta: " answer" });+ });+ expect(useChatStore.getState().messages[0]?.output?.text?.parsed).toEqual({+ response: "",+ chainOfThought: "Visible answer",+ });+ act(() => emit({ type: "transcript", items: [{ type: "text", text: "Visible answer" }] }));+ expect(useChatStore.getState().messages[0]?.output?.text?.parsed).toEqual({+ response: "Visible answer",+ chainOfThought: "",+ });+ await act(() => hook.result.current.cancel());+ await act(() => run);+ });++ it("finalizes the complete transcript even after leaving the chat", async () => {+ const { hook, start, options, emit, finalizeAssistantMessage } = setup();+ let run!: Promise<unknown>;+ act(() => {+ run = start(options);+ });+ await waitFor(() => expect(hook.result.current.isStreaming).toBe(true));+ act(() => useChatStore.setState({ currentChatId: "another-chat", messages: [] }));+ const transcript: TranscriptItem[] = [+ { type: "text", text: "Before" },+ { type: "toolCall", name: "get_current_date_time", called: true },+ { type: "text", text: "After" },+ ];+ act(() => emit({ type: "done", text: "Before\n\nAfter", stats, transcript }));+ await act(() => run);+ expect(finalizeAssistantMessage).toHaveBeenCalledWith(+ CHAT_ID,+ MESSAGE_ID,+ "Before\n\nAfter",+ undefined,+ transcript,+ );+ expect(useChatStore.getState().messages).toEqual([]);+ });++ it("saves the active version's visible text and transcript when stopped from another chat", async () => {+ const { hook, start, options, emit } = setup();+ const message: Message = {+ id: MESSAGE_ID,+ text: "",+ sender: "assistant",+ timestamp: 1,+ versions: [+ { id: "old", text: "Previous response", modelId: "model", modelName: "Model", timestamp: 1 },+ { id: "new", text: "", modelId: "model", modelName: "Model", timestamp: 2 },+ ],+ currentVersionIndex: 1,+ };+ useChatStore.setState({ messages: [message] });+ mocks.loadChat.mockResolvedValue({ metadata: { title: "Untitled" }, messages: [message] });+ let run!: Promise<unknown>;+ act(() => {+ run = start(options);+ });+ await waitFor(() => expect(hook.result.current.isStreaming).toBe(true));+ act(() => useChatStore.setState({ currentChatId: "other", messages: [] }));+ const transcript: TranscriptItem[] = [+ { type: "toolCall", name: "get_current_date_time", called: true },+ { type: "text", text: "New visible response" },+ ];+ act(() => {+ emit({ type: "transcript", items: [transcript[0]!, { type: "text", text: "New visible" }] });+ emit({ type: "transcriptDelta", index: 1, delta: " response" });+ });+ await act(() => hook.result.current.cancel());+ await act(() => run);+ expect(mocks.updateStoredMessage).toHaveBeenCalledWith(+ CHAT_ID,+ MESSAGE_ID,+ expect.objectContaining({+ text: "New visible response",+ versions: [+ message.versions![0],+ expect.objectContaining({ text: "New visible response", output: expect.objectContaining({ transcript }) }),+ ],+ }),+ );+ });+it("reports a backend error and does not finalize", async () => {const { hook, start, options, callbacks, finalizeAssistantMessage, emit } = setup();@@ -207,24 +448,31 @@ describe("useLlmStream", () => {expect(hook.result.current.isStreaming).toBe(false);});- it("reports a stalled stream once", async () => {+ it("keeps a quiet generation running until the backend finishes", async () => {vi.useFakeTimers();try {- const { start, options, callbacks, emit } = setup();-+ const { hook, start, options, callbacks, emit, transport } = setup();+ let run!: Promise<unknown>;act(() => {- void start(options);+ run = start(options);});await act(async () => {await vi.advanceTimersByTimeAsync(0);});act(() => emit({ type: "chunk", delta: "partial" }));await act(async () => {- await vi.advanceTimersByTimeAsync(STALL_TIMEOUT_MS + 1);+ await vi.advanceTimersByTimeAsync(120_000);});- expect(callbacks.onError).toHaveBeenCalledWith(MESSAGE_ID, "Error: Stream timeout");+ expect(hook.result.current.isStreaming).toBe(true);+ expect(callbacks.onError).not.toHaveBeenCalled();+ expect(callbacks.onDone).not.toHaveBeenCalled();+ expect(transport.cancel).not.toHaveBeenCalled();++ act(() => emit({ type: "done", text: "partial", stats, finishReason: "Stop" }));+ await act(() => run);expect(callbacks.onDone).toHaveBeenCalledTimes(1);+ expect(hook.result.current.isStreaming).toBe(false);} finally {vi.useRealTimers();}@@ -240,4 +488,85 @@ describe("useLlmStream", () => {expect(finalizeAssistantMessage).not.toHaveBeenCalled();expect(useChatSessionStore.getState().isGenerating).toBe(false);});++ it("saves model names to the originating chat even after navigation and a later stream error", async () => {+ const { hook, start, options, emit } = setup();+ let run!: Promise<unknown>;+ act(() => {+ run = start(options);+ });+ await waitFor(() => expect(hook.result.current.isStreaming).toBe(true));+ act(() => useChatStore.getState().createNewChat("another-chat"));++ act(() => emit({ type: "chatName", name: "First name" }));+ await waitFor(() => expect(mocks.updateChatTitle).toHaveBeenCalledWith(CHAT_ID, "First name", "Untitled"));+ act(() => {+ emit({ type: "chatName", name: "Changed topic" });+ emit({ type: "error", error: "later generation failure" });+ });+ await act(() => run);++ expect(mocks.updateChatTitle).toHaveBeenLastCalledWith(CHAT_ID, "Changed topic", "First name");+ expect(useChatStore.getState().currentChatId).toBe("another-chat");+ });++ it("keeps a manual rename made during a run", async () => {+ let storedName = "Untitled";+ mocks.updateChatTitle.mockImplementationOnce(async (_id, name, expected) => {+ if (storedName === expected) storedName = name;+ });+ const { hook, start, options, emit } = setup();+ let run!: Promise<unknown>;+ act(() => {+ run = start(options);+ });+ await waitFor(() => expect(hook.result.current.isStreaming).toBe(true));+ storedName = "My chosen name";++ act(() => {+ emit({ type: "chatName", name: "Automatic name" });+ emit({ type: "done", text: "Answer", stats, finishReason: "Stop" });+ });+ await act(() => run);++ expect(storedName).toBe("My chosen name");+ });++ it("reports a failed name save without losing the response", async () => {+ mocks.updateChatTitle.mockRejectedValueOnce(new Error("disk full"));+ const logged = vi.spyOn(console, "error").mockImplementation(() => {});+ try {+ const { hook, start, options, callbacks, finalizeAssistantMessage, emit } = setup();+ let run!: Promise<unknown>;+ act(() => {+ run = start(options);+ });+ await waitFor(() => expect(hook.result.current.isStreaming).toBe(true));+ act(() => {+ emit({ type: "chatName", name: "Automatic name" });+ emit({ type: "done", text: "Answer", stats, finishReason: "Stop" });+ });+ await act(() => run);++ expect(useChatStore.getState().saveFailureCount).toBe(1);+ expect(callbacks.onError).not.toHaveBeenCalled();+ expect(finalizeAssistantMessage).toHaveBeenCalledWith(CHAT_ID, MESSAGE_ID, "Answer", undefined, undefined);+ } finally {+ logged.mockRestore();+ }+ });++ it("keeps a name already set when the response is canceled", async () => {+ const { hook, start, options, emit } = setup();+ let run!: Promise<unknown>;+ act(() => {+ run = start(options);+ });+ await waitFor(() => expect(hook.result.current.isStreaming).toBe(true));+ act(() => emit({ type: "chatName", name: "Accepted name" }));+ await act(() => hook.result.current.cancel());+ await act(() => run);++ expect(mocks.updateChatTitle).toHaveBeenCalledWith(CHAT_ID, "Accepted name", "Untitled");+ });});diff --git a/apps/mirai-gui/client/src/features/chat/hooks/use-llm-stream.ts b/apps/mirai-gui/client/src/features/chat/hooks/use-llm-stream.tsindex c08abd61e..07f36dfca 100644--- a/apps/mirai-gui/client/src/features/chat/hooks/use-llm-stream.ts+++ b/apps/mirai-gui/client/src/features/chat/hooks/use-llm-stream.ts@@ -1,12 +1,13 @@import { useChatSessionStore } from "@/stores/use-chat-session-store";import { useChatStore } from "@/stores/use-chat-store";+import { getPlatform } from "@/platform/platform-singleton";import type { Message } from "@/types/message";import { withParsedOutput } from "@/stores/chat/message-patches";import { getRunMetrics } from "../services/run-metrics";import type {LlmAsyncStream,LlmRunParams,- ParsedPatch,+ OutputShape,SessionOutputFinishReason,SessionOutputStats,} from "@/types/llm-stream";@@ -16,6 +17,10 @@ import { createRevealLoop } from "../services/reveal-loop";export type StartStreamOptions = {repoId: string;messages: LlmRunParams["messages"];+ modelChatNamingEnabled?: boolean;+ dateTimeToolEnabled?: boolean;+ chartToolEnabled?: boolean;+ signal?: AbortSignal;messageId: string;chatId: string;updateText: (messageId: string, updatedText: string) => void;@@ -24,23 +29,20 @@ export type StartStreamOptions = {onFinishReason?: (reason: SessionOutputFinishReason) => void;};-export const STALL_TIMEOUT_MS = 15000;-const activeVersionIndex = (message: Message | undefined): number | undefined =>message?.versions?.length ? (message.currentVersionIndex ?? message.versions.length - 1) : undefined;-const applyParsed = (messageId: string, patch: ParsedPatch): void => {+const outputPatch = (message: Message, output: OutputShape, text?: string): Partial<Message> => {+ const versionIndex = activeVersionIndex(message);+ const patch = { output, ...(text !== undefined ? { text } : {}) };+ if (versionIndex === undefined) return patch;+ return { versions: message.versions?.map((v, i) => (i === versionIndex ? { ...v, ...patch } : v)) };+};++const applyOutput = (messageId: string, output: OutputShape): void => {const store = useChatStore.getState();const target = store.messages.find((m) => m.id === messageId);- const versionIndex = activeVersionIndex(target);- if (versionIndex === undefined) {- store.updateMessage(messageId, { output: withParsedOutput(target?.output, patch) });- return;- }- const versions = (target?.versions || []).map((v, i) =>- i === versionIndex ? { ...v, output: withParsedOutput(v.output, patch) } : v,- );- store.updateMessage(messageId, { versions });+ if (target) store.updateMessage(messageId, outputPatch(target, output));};const applyPerf = (messageId: string, stats: SessionOutputStats): void => {@@ -64,29 +66,17 @@ export const useLlmStream = (chatId: string) => {const activeRunIdRef = useRef<string | null>(null);const canceledRunIdRef = useRef<string | null>(null);- const stallTimeoutRef = useRef<number | null>(null);-- const clearStallTimeout = useCallback(() => {- if (stallTimeoutRef.current === null) return;- window.clearTimeout(stallTimeoutRef.current);- stallTimeoutRef.current = null;+ const finalize = useCallback((onDone?: () => void) => {+ setIsStreaming(false);+ setIsLoading(false);+ currentLlmRef.current = null;+ currentReaderRef.current = null;+ currentRevealCancelRef.current = null;+ useChatSessionStore.getState().setGenerating(false);+ useChatSessionStore.getState().clearActiveGenerating();+ onDone?.();}, []);- const finalize = useCallback(- (onDone?: () => void) => {- clearStallTimeout();- setIsStreaming(false);- setIsLoading(false);- currentLlmRef.current = null;- currentReaderRef.current = null;- currentRevealCancelRef.current = null;- useChatSessionStore.getState().setGenerating(false);- useChatSessionStore.getState().clearActiveGenerating();- onDone?.();- },- [clearStallTimeout],- );-const releaseCurrentRun = useCallback(async () => {currentRevealCancelRef.current?.();currentRevealCancelRef.current = null;@@ -126,7 +116,25 @@ export const useLlmStream = (chatId: string) => {if (session.operationState !== "running") throw new Error("startStream called without the run operation");+ const stopIfAborted = () => {+ if (!options.signal?.aborted) return false;+ options.onDone?.();+ return true;+ };+ if (stopIfAborted()) return;+await releaseCurrentRun();+ if (stopIfAborted()) return;++ const modelChatNamingEnabled =+ options.modelChatNamingEnabled !== false && (await getPlatform().settings.getModelChatNamingEnabled());+ if (stopIfAborted()) return;+ // Compare against the title at the start of this run so a manual rename+ // made while the model thinks wins over its automatic naming.+ let expectedTitle = modelChatNamingEnabled+ ? (await getPlatform().storage.loadChat(chatId))?.metadata.title+ : undefined;+ if (stopIfAborted()) return;session.setGenerating(true);session.setActiveGenerating(chatId, messageId);@@ -137,7 +145,13 @@ export const useLlmStream = (chatId: string) => {return;}- const llm = useChatStore.getState().runChatStream({ repoId, messages: options.messages });+ const llm = useChatStore.getState().runChatStream({+ repoId,+ messages: options.messages,+ modelChatNamingEnabled,+ dateTimeToolEnabled: options.dateTimeToolEnabled,+ chartToolEnabled: options.chartToolEnabled,+ });const runId = llm.runId;const reader = llm.stream.getReader();currentLlmRef.current = llm;@@ -146,21 +160,9 @@ export const useLlmStream = (chatId: string) => {session.setActiveRunId(runId);const isCurrentRun = () => activeRunIdRef.current === runId;- // Reasoning arrives as parsed patches before any text does.- let hasContent = false;- const offParsed = llm.onParsed((patch) => {- if (!hasContent && (patch.chainOfThought || patch.response)) {- hasContent = true;- setIsLoading(false);- }- applyParsed(messageId, patch);- });-- setIsStreaming(true);- setIsLoading(true);-const storeText = useChatStore.getState().messages.find((m) => m.id === messageId)?.text || "";const bufferText = useChatSessionStore.getState().activeAssistantMessageText || "";+ let transcriptText: string | undefined;const reveal = createRevealLoop({baseText: bufferText.length >= storeText.length ? bufferText : storeText,isActive: isCurrentRun,@@ -171,29 +173,72 @@ export const useLlmStream = (chatId: string) => {});currentRevealCancelRef.current = reveal.cancel;+ let titleUpdates = Promise.resolve();+ const offChatName = llm.onChatName((name) => {+ if (!modelChatNamingEnabled) return;+ titleUpdates = titleUpdates.then(async () => {+ if (expectedTitle === undefined) return;+ try {+ await useChatStore.getState().updateChatTitle(chatId, name, expectedTitle);+ expectedTitle = name;+ } catch (error) {+ console.error("[storage] failed to save model chat name", { chatId }, error);+ useChatStore.setState((s) => ({ saveFailureCount: s.saveFailureCount + 1 }));+ }+ });+ });++ // Reasoning arrives as parsed patches before any text does.+ let hasContent = false;+ let output: OutputShape = {};+ const updateOutput = (next: OutputShape) => {+ output = next;+ applyOutput(messageId, output);+ session.setActiveAssistantMessageOutput(output);+ };+ const offParsed = llm.onParsed((patch) => {+ if (!hasContent && (patch.chainOfThought || patch.response)) {+ hasContent = true;+ setIsLoading(false);+ }+ updateOutput(withParsedOutput(output, patch));+ });+ const offTranscript = llm.onTranscript((items) => {+ if (items.length > 0) {+ hasContent = true;+ setIsLoading(false);+ }+ // The transcript is already visible. Keep the saved text and subsequent+ // model history at the same point, without waiting for the legacy pacer.+ reveal.cancel();+ transcriptText = items+ .flatMap((item) => (item.type === "text" && item.text.length > 0 ? [item.text] : []))+ .join("\n\n");+ const chainOfThought = items+ .flatMap((item) => (item.type === "thinking" && item.text.length > 0 ? [item.text] : []))+ .join("\n\n");+ options.updateText(messageId, transcriptText);+ session.setActiveAssistantMessageText(transcriptText);+ updateOutput({ ...withParsedOutput(output, { response: transcriptText, chainOfThought }), transcript: items });+ });++ setIsStreaming(true);+ setIsLoading(!hasContent);+const fail = (message: string) => {// The pump may still be awaiting a read that resolves after this;// retiring the run id keeps it from finalizing a second time.activeRunIdRef.current = null;options.onError(messageId, `Error: ${message}`);+ if (transcriptText !== undefined) options.updateText(messageId, transcriptText);reveal.cancel();offParsed();+ offChatName();+ offTranscript();finalize(options.onDone);};let hasText = false;- const armStallTimeout = () => {- if (!hasText) return;- clearStallTimeout();- stallTimeoutRef.current = window.setTimeout(() => {- if (!isCurrentRun()) return;- canceledRunIdRef.current = runId;- void reader.cancel();- void llm.cancel();- fail("Stream timeout");- }, STALL_TIMEOUT_MS);- };-const pump = async (): Promise<void> => {for (;;) {const { value, done } = await reader.read();@@ -203,8 +248,7 @@ export const useLlmStream = (chatId: string) => {return;}if (done) {- clearStallTimeout();- await reveal.drain();+ if (output.transcript === undefined) await reveal.drain();offParsed();if (!isCurrentRun()) return;session.setActiveAssistantMessageText(null);@@ -214,9 +258,8 @@ export const useLlmStream = (chatId: string) => {if (value) {if (!hasText) setIsLoading(false);hasText = true;- reveal.append(value);+ if (output.transcript === undefined) reveal.append(value);}- armStallTimeout();}};@@ -234,30 +277,39 @@ export const useLlmStream = (chatId: string) => {}const result = await llm.result;- if (result.error) return;+ offChatName();+ offTranscript();+ await titleUpdates;// Stop pressed after leaving and reopening the chat: the stop handler saved// the text at that moment, and this stream kept printing after it.const wasCanceled = canceledRunIdRef.current === runId || result.finishReason === "Cancelled";- if (wasCanceled) {+ if (wasCanceled || result.error) {canceledRunIdRef.current = null;- const current = useChatStore.getState().messages.find((m) => m.id === messageId);- if (isCurrentRun() && current) {- void useChatStore.getState().persistMessagePatch(chatId, messageId, {- text: current.text,+ const current =+ useChatStore.getState().messages.find((m) => m.id === messageId) ??+ (await getPlatform().storage.loadChat(chatId))?.messages?.find((m) => m.id === messageId);+ if (current) {+ const patch = {+ text: transcriptText ?? current.text,versions: current.versions,error: current.error,output: current.output,- });+ ...(Object.keys(output).length > 0 ? outputPatch(current, output, transcriptText) : {}),+ };+ await useChatStore.getState().persistMessagePatch(chatId, messageId, patch);}- return;+ return result;}if (!isCurrentRun()) return;applyPerf(messageId, result.stats);- await useChatStore.getState().finalizeAssistantMessage(chatId, messageId, result.text || "", result.parsed);+ await useChatStore+ .getState()+ .finalizeAssistantMessage(chatId, messageId, result.text || "", result.parsed, result.transcript);if (result.finishReason) options.onFinishReason?.(result.finishReason);+ return result;},- [clearStallTimeout, finalize, releaseCurrentRun],+ [finalize, releaseCurrentRun],);return { isStreaming, isLoading, cancel, startStream };diff --git a/apps/mirai-gui/client/src/features/chat/hooks/use-regenerate-message.test.tsx b/apps/mirai-gui/client/src/features/chat/hooks/use-regenerate-message.test.tsxnew file mode 100644index 000000000..5bb62e254--- /dev/null+++ b/apps/mirai-gui/client/src/features/chat/hooks/use-regenerate-message.test.tsx@@ -0,0 +1,146 @@+import { act, cleanup, renderHook, waitFor } from "@testing-library/react";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { useChatStore } from "@/stores/use-chat-store";+import { useChatSessionStore } from "@/stores/use-chat-session-store";+import { useModelParamsStore } from "@/stores/use-model-params-store";+import { useModelsStore } from "@/stores/use-models-store";+import { ModelKind } from "@/types/models";+import { useRuntimeSessionStore } from "@/stores/use-runtime-session-store";+import type { ToastApi } from "@/components/ui/toast/use-toast";+import { Roles } from "@/types/chat";+import { useRegenerateMessage } from "./use-regenerate-message";++const settings = vi.hoisted(() => ({ getModelChatNamingEnabled: vi.fn(async () => true) }));+vi.mock("@/platform/platform-singleton", () => ({ getPlatform: () => ({ settings }) }));+const chatDefaults = useChatStore.getState();+const sessionDefaults = useChatSessionStore.getState();+const paramsDefaults = useModelParamsStore.getState();+const runtimeDefaults = useRuntimeSessionStore.getState();+const startStream = vi.fn(async () => {});+const toast = { error: vi.fn(), info: vi.fn(), warning: vi.fn() } as unknown as ToastApi;++beforeEach(() => {+ vi.clearAllMocks();+ useChatStore.setState(+ {+ ...chatDefaults,+ currentChatId: "chat",+ messages: [+ { id: "user", sender: Roles.User, text: "Question", timestamp: 1 },+ {+ id: "answer",+ sender: Roles.Assistant,+ text: "Answer",+ timestamp: 2,+ modelId: "original",+ modelName: "Original",+ },+ ],+ persistMessagePatch: vi.fn(async () => {}),+ },+ true,+ );+ useChatSessionStore.setState(sessionDefaults, true);+ useModelParamsStore.setState(paramsDefaults, true);+ useModelsStore.setState({ models: [] });+ useRuntimeSessionStore.setState(runtimeDefaults, true);+});+afterEach(cleanup);++it("regenerates with the selected model's tool overrides", async () => {+ useModelParamsStore.setState({+ paramsByRepoId: {+ original: { sampling: { type: "Default" }, modelChatNamingEnabled: true },+ replacement: {+ sampling: { type: "Default" },+ modelChatNamingEnabled: false,+ dateTimeToolEnabled: false,+ chartToolEnabled: false,+ },+ },+ });+ const hook = renderHook(() => useRegenerateMessage({ chatId: "chat", globalInstructions: "", toast, startStream }));+ await act(() => hook.result.current("answer", "replacement", "Replacement"));+ expect(startStream).toHaveBeenCalledWith(+ expect.objectContaining({+ repoId: "replacement",+ signal: expect.any(AbortSignal),+ modelChatNamingEnabled: false,+ dateTimeToolEnabled: false,+ chartToolEnabled: false,+ }),+ );+});++it.each(["save", "settings"] as const)("does not start regeneration canceled during %s preparation", async (stage) => {+ let resume!: () => void;+ const pending = new Promise<void>((resolve) => {+ resume = resolve;+ });+ const persist = vi.mocked(useChatStore.getState().persistMessagePatch);+ if (stage === "save") persist.mockReturnValueOnce(pending);+ else+ settings.getModelChatNamingEnabled.mockImplementationOnce(async () => {+ await pending;+ return true;+ });+ const hook = renderHook(() => useRegenerateMessage({ chatId: "chat", globalInstructions: "", toast, startStream }));+ let operation!: Promise<void>;+ act(() => {+ operation = hook.result.current("answer", "replacement", "Replacement");+ });+ await waitFor(() => expect(stage === "save" ? persist : settings.getModelChatNamingEnabled).toHaveBeenCalledOnce());+ await act(() => useChatSessionStore.getState().cancelActiveRunForChat("chat"));+ await act(async () => {+ resume();+ await operation;+ });++ expect(startStream).not.toHaveBeenCalled();+ expect(useChatSessionStore.getState()).toMatchObject({ loadingMessage: null, operationState: "idle" });+});++it("regenerates with current global naming and default tool settings when there is no override", async () => {+ settings.getModelChatNamingEnabled.mockResolvedValueOnce(false);+ const hook = renderHook(() => useRegenerateMessage({ chatId: "chat", globalInstructions: "", toast, startStream }));+ await act(() => hook.result.current("answer", "replacement", "Replacement"));+ expect(startStream).toHaveBeenCalledWith(+ expect.objectContaining({ modelChatNamingEnabled: false, dateTimeToolEnabled: true, chartToolEnabled: true }),+ );+});++it.each([false, true])("regenerates on a small model with tools enabled only after opt-in=%s", async (enabled) => {+ useModelsStore.setState({+ models: [+ {+ repoId: "replacement",+ name: "Replacement",+ vendor: "Vendor",+ kind: ModelKind.Text,+ reasoning: { kind: "unsupported" },+ supportsTools: true,+ paramSize: 1_000_000_000,+ },+ ],+ });+ if (enabled)+ useModelParamsStore.setState({+ paramsByRepoId: {+ replacement: {+ sampling: { type: "Default" },+ modelChatNamingEnabled: true,+ dateTimeToolEnabled: true,+ chartToolEnabled: true,+ },+ },+ });+ const hook = renderHook(() => useRegenerateMessage({ chatId: "chat", globalInstructions: "", toast, startStream }));+ await act(() => hook.result.current("answer", "replacement", "Replacement"));+ expect(startStream).toHaveBeenCalledWith(+ expect.objectContaining({+ modelChatNamingEnabled: enabled,+ dateTimeToolEnabled: enabled,+ chartToolEnabled: enabled,+ }),+ );+});diff --git a/apps/mirai-gui/client/src/features/chat/hooks/use-regenerate-message.ts b/apps/mirai-gui/client/src/features/chat/hooks/use-regenerate-message.tsindex 5dfdd0309..fdc17a806 100644--- a/apps/mirai-gui/client/src/features/chat/hooks/use-regenerate-message.ts+++ b/apps/mirai-gui/client/src/features/chat/hooks/use-regenerate-message.ts@@ -1,6 +1,10 @@import { useCallback } from "react";import { useChatSessionStore } from "@/stores/use-chat-session-store";import { useChatStore } from "@/stores/use-chat-store";+import { resolveModelTools, useModelParamsStore } from "@/stores/use-model-params-store";+import { useModelsStore } from "@/stores/use-models-store";+import { getPlatform } from "@/platform/platform-singleton";+import type { LlmRunResult } from "@/types/llm-stream";import { Roles } from "../types";import { attachmentStorage } from "../services/attachment-storage";import { getChatRunBlockMessage, prepareChatModelForRun } from "../services/chat-run-preflight";@@ -13,7 +17,7 @@ type UseRegenerateMessageParams = {chatId: string;globalInstructions: string | null | undefined;toast: ToastApi;- startStream: (options: StartStreamOptions) => Promise<void | null>;+ startStream: (options: StartStreamOptions) => Promise<LlmRunResult | void | null>;};export const useRegenerateMessage = ({@@ -55,64 +59,83 @@ export const useRegenerateMessage = ({globalInstructions,});- const accepted = await useChatSessionStore.getState().withOperation("running", async () => {- const newVersions = projectAssistantVersion(targetMessage, modelId, modelName, userMessage.attachmentIds);- const regeneratePatch = {- text: "",- modelId,- modelName,- versions: newVersions,- currentVersionIndex: newVersions.length - 1,- };- state.updateMessage(messageId, regeneratePatch);- await state.persistMessagePatch(chatId, messageId, regeneratePatch);- const { setLoadingMessage } = useChatSessionStore.getState();- setLoadingMessage(chatId, messageId);+ const accepted = await useChatSessionStore.getState().withOperation(+ "running",+ async (signal) => {+ const canceled = () => {+ if (!signal.aborted) return false;+ useChatSessionStore.getState().setLoadingMessage(chatId, null);+ return true;+ };+ const newVersions = projectAssistantVersion(targetMessage, modelId, modelName, userMessage.attachmentIds);+ const regeneratePatch = {+ text: "",+ modelId,+ modelName,+ versions: newVersions,+ currentVersionIndex: newVersions.length - 1,+ };+ state.updateMessage(messageId, regeneratePatch);+ await state.persistMessagePatch(chatId, messageId, regeneratePatch);+ if (canceled()) return;+ const { setLoadingMessage } = useChatSessionStore.getState();+ setLoadingMessage(chatId, messageId);- const updateText = (id: string, updatedText: string) => {- const current = useChatStore.getState().messages.find((m) => m.id === id);- useChatStore.getState().updateMessage(id, {- text: updatedText,- versions: patchActiveVersion(current?.versions, {+ const updateText = (id: string, updatedText: string) => {+ const current = useChatStore.getState().messages.find((m) => m.id === id);+ useChatStore.getState().updateMessage(id, {text: updatedText,- attachmentIds: userMessage.attachmentIds,- }),- });- };+ versions: patchActiveVersion(current?.versions, {+ text: updatedText,+ attachmentIds: userMessage.attachmentIds,+ }),+ });+ };- const failVersion = (errorText: string) => {- const current = useChatStore.getState().messages.find((m) => m.id === messageId);- const nextVersions = patchActiveVersion(current?.versions, { text: "", error: errorText });- useChatStore- .getState()- .updateMessage(- messageId,- nextVersions.length > 0 ? { text: "", versions: nextVersions } : { text: "", error: errorText },- );- setLoadingMessage(chatId, null);- };+ const failVersion = (errorText: string) => {+ if (canceled()) return;+ const current = useChatStore.getState().messages.find((m) => m.id === messageId);+ const nextVersions = patchActiveVersion(current?.versions, { text: "", error: errorText });+ useChatStore+ .getState()+ .updateMessage(+ messageId,+ nextVersions.length > 0 ? { text: "", versions: nextVersions } : { text: "", error: errorText },+ );+ setLoadingMessage(chatId, null);+ };- try {- await startStream({- repoId: modelId,- messages: messagesForRun,- messageId,- chatId,- onDone: () => setLoadingMessage(chatId, null),- updateText,- onError: (_id, errorText) => failVersion(errorText),- onFinishReason: (reason) => {- if (reason === "ContextLimitReached") {- toast.warning("Conversation reached the model's context limit; reply may be truncated");- } else if (reason === "Length") {- toast.warning("Reply hit the output length limit and may be truncated");- }- },- });- } catch (e) {- failVersion(`Error: ${e instanceof Error ? e.message : String(e)}`);- }- });+ try {+ const tools = resolveModelTools(+ useModelParamsStore.getState().getParams(modelId),+ await getPlatform().settings.getModelChatNamingEnabled(),+ useModelsStore.getState().models.find((model) => model.repoId === modelId)?.paramSize,+ );+ if (canceled()) return;+ await startStream({+ signal,+ repoId: modelId,+ messages: messagesForRun,+ messageId,+ chatId,+ ...tools,+ onDone: () => setLoadingMessage(chatId, null),+ updateText,+ onError: (_id, errorText) => failVersion(errorText),+ onFinishReason: (reason) => {+ if (reason === "ContextLimitReached") {+ toast.warning("Conversation reached the model's context limit; reply may be truncated");+ } else if (reason === "Length") {+ toast.warning("Reply hit the output length limit and may be truncated");+ }+ },+ });+ } catch (e) {+ failVersion(`Error: ${e instanceof Error ? e.message : String(e)}`);+ }+ },+ chatId,+ );if (accepted === null) toast.error("Model is not ready yet, please wait");},[chatId, globalInstructions, toast, startStream],diff --git a/apps/mirai-gui/client/src/features/chat/hooks/use-send-chat-message.test.tsx b/apps/mirai-gui/client/src/features/chat/hooks/use-send-chat-message.test.tsxindex 021985578..4e6da65c1 100644--- a/apps/mirai-gui/client/src/features/chat/hooks/use-send-chat-message.test.tsx+++ b/apps/mirai-gui/client/src/features/chat/hooks/use-send-chat-message.test.tsx@@ -3,13 +3,19 @@ import { afterEach, beforeEach, expect, it, vi } from "vitest";import { ChatNotFoundError, type ChatData, type ChatMetadata } from "@/platform/services/storage";import { useChatSessionStore } from "@/stores/use-chat-session-store";import { useChatStore } from "@/stores/use-chat-store";+import { useModelParamsStore } from "@/stores/use-model-params-store";+import { useModelsStore } from "@/stores/use-models-store";+import { ModelKind } from "@/types/models";import { useRuntimeSessionStore } from "@/stores/use-runtime-session-store";import { Roles } from "@/types/chat";import type { ToastApi } from "@/components/ui/toast/use-toast";import type { StartStreamOptions } from "./use-llm-stream";+import type { LlmRunResult } from "@/types/llm-stream";+import { emptyStats } from "@/platform/services/chat/empty-stats";import { useSendChatMessage } from "./use-send-chat-message";import { useStopChatRun } from "./use-stop-chat-run";import type { ChatComposerState } from "./use-chat-composer-state";+import { attachmentStorage } from "../services/attachment-storage";const mocks = vi.hoisted(() => ({ platform: {} as Record<string, unknown> }));vi.mock("@/platform/platform-singleton", () => ({ getPlatform: () => mocks.platform }));@@ -19,6 +25,7 @@ const CHAT_B = "b";const chatDefaults = useChatStore.getState();const sessionDefaults = useChatSessionStore.getState();const runtimeDefaults = useRuntimeSessionStore.getState();+const paramsDefaults = useModelParamsStore.getState();const metadata: ChatMetadata = {id: CHAT_A,title: "Untitled",@@ -29,7 +36,7 @@ const metadata: ChatMetadata = {modelName: "Model",};-const startStream = vi.fn<(options: StartStreamOptions) => Promise<void>>(async () => {+const startStream = vi.fn<(options: StartStreamOptions) => Promise<LlmRunResult | void>>(async () => {expect(useChatSessionStore.getState().operationState).toBe("running");});const composer = { attachedFiles: [], clear: vi.fn() } as unknown as ChatComposerState;@@ -57,7 +64,19 @@ const renderSend = (chatId = CHAT_A) =>const generateTitle = vi.fn();const cancelTitleGen = vi.fn();const ejectSession = vi.fn(async () => {});+const getModelChatNamingEnabled = vi.fn(async () => false);let chats = new Map<string, ChatData>();+const editUserMessage = vi.fn(async (id: string, messageId: string, text: string) => {+ const chat = structuredClone(chats.get(id));+ if (!chat) throw new ChatNotFoundError(id);+ const index = chat.messages.findIndex((m) => m.id === messageId && m.sender === Roles.User);+ if (index < 0) throw new Error("User message not found");+ chat.messages = chat.messages.slice(0, index + 1);+ chat.messages[index] = { ...chat.messages[index]!, text };+ chat.metadata.messageCount = chat.messages.length;+ chats.set(id, chat);+ return structuredClone(chat);+});const updateChatTitle = vi.fn(async (id: string, title: string, expectedTitle?: string) => {const chat = chats.get(id);if (!chat) return;@@ -80,10 +99,14 @@ beforeEach(() => {useChatStore.setState(chatDefaults, true);useChatSessionStore.setState(sessionDefaults, true);useRuntimeSessionStore.setState(runtimeDefaults, true);+ useModelParamsStore.setState(paramsDefaults, true);+ useModelsStore.setState({ models: [] });useChatStore.getState().createNewChat(CHAT_A);useChatStore.getState().setChatModel(CHAT_A, "model", "Model");chats = new Map<string, ChatData>();generateTitle.mockResolvedValue("Title");+ getModelChatNamingEnabled.mockResolvedValue(false);+ composer.attachedFiles = [];cancelTitleGen.mockResolvedValue(undefined);mocks.platform = {storage: {@@ -98,7 +121,14 @@ beforeEach(() => {loadChat: vi.fn(async (id: string) => structuredClone(chats.get(id) ?? null)),listChats: vi.fn(async () => [...chats.values()].map((c) => c.metadata)),updateChatTitle,- updateStoredMessage: vi.fn(async () => {}),+ updateStoredMessage: vi.fn(+ async (id: string, messageId: string, patch: Partial<ChatData["messages"][number]>) => {+ const chat = chats.get(id);+ if (chat)+ chat.messages = chat.messages.map((m) => (m.id === messageId ? { ...m, ...structuredClone(patch) } : m));+ },+ ),+ editUserMessage,removeMessage: vi.fn(async (id: string, messageId: string) => {const chat = chats.get(id);if (chat) chat.messages = chat.messages.filter((m) => m.id !== messageId);@@ -108,6 +138,7 @@ beforeEach(() => {}),},chat: { generateTitle, cancelTitleGen },+ settings: { getModelChatNamingEnabled },session: { ejectSession },};});@@ -380,3 +411,357 @@ it("turns the placeholder into the error bubble when the first write fails", asyexpect(chats.get(CHAT_A)?.messages.map((m) => m.id)).toEqual(messages.map((m) => m.id));expect(useChatSessionStore.getState()).toMatchObject({ loadingMessage: null, operationState: "idle" });});++it("skips separate title generation when the model named the first turn", async () => {+ getModelChatNamingEnabled.mockResolvedValue(true);+ startStream.mockResolvedValueOnce({ text: "Answer", stats: emptyStats(), finishReason: "Stop", chatName: "A name" });+ const hook = renderSend();++ await act(() => hook.result.current.send("question"));++ expect(startStream.mock.calls[0]?.[0].modelChatNamingEnabled).toBe(true);+ expect(startStream.mock.calls[0]?.[0].dateTimeToolEnabled).toBe(true);+ expect(startStream.mock.calls[0]?.[0].chartToolEnabled).toBe(true);+ expect(generateTitle).not.toHaveBeenCalled();+});++it.each([false, true])("sends a small model with tools enabled only after opt-in=%s", async (enabled) => {+ getModelChatNamingEnabled.mockResolvedValue(true);+ useModelsStore.setState({+ models: [+ {+ repoId: "model",+ name: "Model",+ vendor: "Vendor",+ kind: ModelKind.Text,+ reasoning: { kind: "unsupported" },+ supportsTools: true,+ paramSize: 1_000_000_000,+ },+ ],+ });+ if (enabled)+ useModelParamsStore.setState({+ paramsByRepoId: {+ model: {+ sampling: { type: "Default" },+ modelChatNamingEnabled: true,+ dateTimeToolEnabled: true,+ chartToolEnabled: true,+ },+ },+ });+ const hook = renderSend();+ await act(() => hook.result.current.send("question"));+ expect(startStream).toHaveBeenCalledWith(+ expect.objectContaining({+ modelChatNamingEnabled: enabled,+ dateTimeToolEnabled: enabled,+ chartToolEnabled: enabled,+ }),+ );+});++it("ignores an old per-model naming opt-in while the global feature is off", async () => {+ useModelParamsStore.setState({+ paramsByRepoId: {+ model: {+ sampling: { type: "Default" },+ modelChatNamingEnabled: true,+ dateTimeToolEnabled: false,+ chartToolEnabled: false,+ },+ },+ });+ startStream.mockResolvedValueOnce({ text: "Answer", stats: emptyStats(), finishReason: "Stop", chatName: "A name" });+ const hook = renderSend();+ await act(() => hook.result.current.send("question"));+ expect(startStream.mock.calls[0]?.[0]).toMatchObject({+ modelChatNamingEnabled: false,+ dateTimeToolEnabled: false,+ chartToolEnabled: false,+ });+ expect(generateTitle).toHaveBeenCalledTimes(1);+});++it("uses separate title generation when the per-model naming tool is turned off", async () => {+ getModelChatNamingEnabled.mockResolvedValue(true);+ useModelParamsStore.setState({+ paramsByRepoId: { model: { sampling: { type: "Default" }, modelChatNamingEnabled: false } },+ });+ const hook = renderSend();+ await act(() => hook.result.current.send("question"));+ expect(startStream.mock.calls[0]?.[0]).toMatchObject({ modelChatNamingEnabled: false, dateTimeToolEnabled: true });+ expect(generateTitle).toHaveBeenCalledTimes(1);+});++it("waits for the first reply before falling back when no model name was provided", async () => {+ getModelChatNamingEnabled.mockResolvedValue(true);+ const reply = deferred<LlmRunResult>();+ startStream.mockReturnValueOnce(reply.promise);+ const hook = renderSend();+ let sending!: Promise<void>;+ act(() => {+ sending = hook.result.current.send("question");+ });+ await waitFor(() => expect(startStream).toHaveBeenCalled());+ expect(generateTitle).not.toHaveBeenCalled();++ await act(async () => {+ reply.resolve({ text: "Answer", stats: emptyStats(), finishReason: "Stop" });+ await sending;+ });++ expect(generateTitle).toHaveBeenCalledTimes(1);+ expect(chats.get(CHAT_A)?.metadata.title).toBe("Title");+});++it("does not retry the fallback on later turns of an untitled chat", async () => {+ getModelChatNamingEnabled.mockResolvedValue(true);+ useChatStore.getState().addMessageTo(CHAT_A, { sender: Roles.User, text: "earlier" });+ useChatStore.getState().addMessageTo(CHAT_A, { sender: Roles.Assistant, text: "earlier answer" });+ startStream.mockResolvedValueOnce({ text: "Answer", stats: emptyStats(), finishReason: "Stop" });+ const hook = renderSend();++ await act(() => hook.result.current.send("next question"));++ expect(generateTitle).not.toHaveBeenCalled();+});++it.each(["Cancelled", "Length", "ContextLimitReached"] as const)(+ "does not run fallback after %s",+ async (finishReason) => {+ getModelChatNamingEnabled.mockResolvedValue(true);+ startStream.mockResolvedValueOnce({ text: "Partial answer", stats: emptyStats(), finishReason });+ const hook = renderSend();++ await act(() => hook.result.current.send("question"));++ expect(generateTitle).not.toHaveBeenCalled();+ },+);++it("keeps a completed reply when fallback title generation fails", async () => {+ getModelChatNamingEnabled.mockResolvedValue(true);+ startStream.mockImplementationOnce(async (options) => {+ options.updateText(options.messageId, "Completed answer");+ return { text: "Completed answer", stats: emptyStats(), finishReason: "Stop" };+ });+ generateTitle.mockRejectedValueOnce(new Error("title failed"));+ const hook = renderSend();++ await act(() => hook.result.current.send("question"));++ expect(useChatStore.getState().messages.at(-1)).toMatchObject({ text: "Completed answer" });+ expect(useChatStore.getState().messages.at(-1)?.error).toBeUndefined();+ expect(toast.error).toHaveBeenCalledWith("Could not name the chat: title failed");+});++it("keeps the reply when Stop cancels the fallback title request", async () => {+ getModelChatNamingEnabled.mockResolvedValue(true);+ startStream.mockImplementationOnce(async (options) => {+ options.updateText(options.messageId, "Completed answer");+ return { text: "Completed answer", stats: emptyStats(), finishReason: "Stop" };+ });+ const title = deferred<string>();+ generateTitle.mockReturnValueOnce(title.promise);+ cancelTitleGen.mockImplementationOnce(async () => {+ title.resolve("");+ });+ const hook = renderSend();+ let sending!: Promise<void>;+ act(() => {+ sending = hook.result.current.send("question");+ });+ await waitFor(() => expect(generateTitle).toHaveBeenCalled());++ await act(async () => {+ await hook.result.current.stop();+ await sending;+ });++ expect(useChatStore.getState().messages.at(-1)?.text).toBe("Completed answer");+ expect(chats.get(CHAT_A)?.messages).toHaveLength(2);+ expect(useChatSessionStore.getState().titleGenAbortChatId).toBeNull();+ expect(updateChatTitle).not.toHaveBeenCalled();+});++const seedEditableChat = () => {+ const messages: ChatData["messages"] = [+ { id: "u1", sender: Roles.User, text: "first question", timestamp: 1 },+ { id: "a1", sender: Roles.Assistant, text: "first answer", timestamp: 2 },+ { id: "u2", sender: Roles.User, text: "old question", timestamp: 3, attachmentIds: ["original-file"] },+ { id: "a2", sender: Roles.Assistant, text: "discarded answer", timestamp: 4 },+ { id: "u3", sender: Roles.User, text: "discarded follow-up", timestamp: 5 },+ ];+ const data = { metadata: { ...metadata, title: "Chosen title", messageCount: messages.length }, messages };+ chats.set(CHAT_A, structuredClone(data));+ useChatStore.setState({ messages, savedChats: [data.metadata] });+ attachmentStorage.saveFile({+ id: "original-file",+ name: "notes.txt",+ mimeType: "text/plain",+ size: 16,+ extension: "txt",+ content: "original notes",+ });+ return data;+};++it("replaces an earlier prompt and regenerates with only its retained history and attachments", async () => {+ seedEditableChat();+ composer.attachedFiles = [+ { id: "draft-file", name: "draft.txt", mimeType: "text/plain", size: 5, extension: "txt", content: "unsent draft" },+ ];+ const onSaved = vi.fn();+ const hook = renderSend();++ await act(() => hook.result.current.send("edited question", { messageId: "u2", onSaved }));++ expect(startStream).toHaveBeenCalledTimes(1);+ expect(startStream.mock.calls[0]?.[0].messages).toEqual([+ { role: Roles.User, content: "first question" },+ { role: Roles.Assistant, content: "first answer" },+ { role: Roles.User, content: "edited question\n\n```txt\noriginal notes\n```" },+ ]);+ const stored = chats.get(CHAT_A)!;+ expect(stored.messages.map((m) => m.text)).toEqual(["first question", "first answer", "edited question", ""]);+ expect(stored.messages[2]).toMatchObject({ id: "u2", timestamp: 3, attachmentIds: ["original-file"] });+ expect(stored.messages[3]?.versions).toBeUndefined();+ expect(stored.metadata.title).toBe("Chosen title");+ expect(useChatStore.getState().messages).toEqual(stored.messages);+ expect(composer.clear).not.toHaveBeenCalled();+ expect(composer.attachedFiles[0]?.id).toBe("draft-file");+ expect(onSaved).toHaveBeenCalledOnce();+});++it("leaves the entire conversation intact and does not generate when saving an edit fails", async () => {+ const original = seedEditableChat();+ editUserMessage.mockRejectedValueOnce(new Error("disk full"));+ const onSaved = vi.fn();+ const hook = renderSend();++ await act(async () => {+ await expect(hook.result.current.send("edited", { messageId: "u2", onSaved })).rejects.toThrow("disk full");+ });++ expect(chats.get(CHAT_A)).toEqual(original);+ expect(useChatStore.getState().messages).toEqual(original.messages);+ expect(useChatSessionStore.getState().operationState).toBe("idle");+ expect(startStream).not.toHaveBeenCalled();+ expect(onSaved).not.toHaveBeenCalled();+});++it("refuses edits until the previous operation, including final persistence, finishes", async () => {+ const original = seedEditableChat();+ useChatSessionStore.setState({ operationState: "running", isGenerating: false });+ const hook = renderSend();+ await act(() => hook.result.current.send("edited", { messageId: "u2", onSaved: noop }));++ expect(editUserMessage).not.toHaveBeenCalled();+ expect(chats.get(CHAT_A)).toEqual(original);+ expect(startStream).not.toHaveBeenCalled();+ expect(toast.error).toHaveBeenCalledWith("A response is already generating.");+});++it("closes the editor only after the edit reaches storage, before the new reply finishes", async () => {+ const original = seedEditableChat();+ const save = deferred<ChatData>();+ const response = deferred<void>();+ editUserMessage.mockReturnValueOnce(save.promise);+ startStream.mockReturnValueOnce(response.promise);+ const onSaved = vi.fn();+ const hook = renderSend();+ let sending!: Promise<void>;+ act(() => {+ sending = hook.result.current.send("edited", { messageId: "u2", onSaved });+ });+ await waitFor(() => expect(editUserMessage).toHaveBeenCalled());+ expect(onSaved).not.toHaveBeenCalled();+ expect(useChatStore.getState().messages).toEqual(original.messages);++ const edited = structuredClone(original);+ edited.messages = [...edited.messages.slice(0, 2), { ...edited.messages[2]!, text: "edited" }];+ await act(async () => {+ chats.set(CHAT_A, edited);+ save.resolve(structuredClone(edited));+ });+ await waitFor(() => expect(startStream).toHaveBeenCalled());+ expect(onSaved).toHaveBeenCalledOnce();+ expect(useChatSessionStore.getState().operationState).toBe("running");+ await act(async () => {+ response.resolve();+ await sending;+ });+});++it("keeps edits and their replacement reply in the original chat when navigating during the save", async () => {+ const original = seedEditableChat();+ const save = deferred<ChatData>();+ editUserMessage.mockReturnValueOnce(save.promise);+ const hook = renderSend();+ let sending!: Promise<void>;+ act(() => {+ sending = hook.result.current.send("edited", { messageId: "u2", onSaved: noop });+ });+ await waitFor(() => expect(editUserMessage).toHaveBeenCalled());+ useChatStore.getState().createNewChat(CHAT_B);+ useChatStore.getState().addMessageTo(CHAT_B, { sender: Roles.User, text: "private history B" });+ const edited = structuredClone(original);+ edited.messages = [...edited.messages.slice(0, 2), { ...edited.messages[2]!, text: "edited" }];+ await act(async () => {+ chats.set(CHAT_A, edited);+ save.resolve(structuredClone(edited));+ await sending;+ });++ expect(useChatStore.getState().messages.map((m) => m.text)).toEqual(["private history B"]);+ expect(chats.get(CHAT_A)?.messages.map((m) => m.text)).toEqual(["first question", "first answer", "edited", ""]);+ expect(JSON.stringify(startStream.mock.calls)).not.toContain("private history B");+ expect(startStream.mock.calls[0]?.[0].chatId).toBe(CHAT_A);+});++it("does not recreate a chat deleted while its edit is being saved", async () => {+ const original = seedEditableChat();+ const save = deferred<ChatData>();+ editUserMessage.mockReturnValueOnce(save.promise);+ const hook = renderSend();+ const onSaved = vi.fn();+ let sending!: Promise<void>;+ act(() => {+ sending = hook.result.current.send("edited", { messageId: "u2", onSaved });+ });+ await waitFor(() => expect(editUserMessage).toHaveBeenCalled());+ await act(() => useChatStore.getState().deleteChat(CHAT_A));+ await act(async () => {+ save.resolve(original);+ await sending;+ });++ expect(chats.has(CHAT_A)).toBe(false);+ expect(useChatStore.getState().savedChats).toEqual([]);+ expect(useChatStore.getState().messages).toEqual([]);+ expect(startStream).not.toHaveBeenCalled();+ expect(onSaved).not.toHaveBeenCalled();+ expect(useChatSessionStore.getState().operationState).toBe("idle");+});++it("retains the selected earlier response version when editing a later prompt", async () => {+ const data = seedEditableChat();+ const answer = data.messages[1]!;+ answer.versions = [+ { id: "v0", text: "first answer", modelId: "model", modelName: "Model", timestamp: 2 },+ { id: "v1", text: "chosen answer", modelId: "model", modelName: "Model", timestamp: 3 },+ ];+ answer.currentVersionIndex = 0;+ chats.set(CHAT_A, structuredClone(data));+ useChatStore.setState({ messages: structuredClone(data.messages) });+ useChatStore.getState().switchMessageVersion("a1", 1);+ const hook = renderSend();++ await act(() => hook.result.current.send("edited", { messageId: "u2", onSaved: noop }));++ expect(startStream.mock.calls[0]?.[0].messages[1]).toEqual({ role: Roles.Assistant, content: "chosen answer" });+ expect(chats.get(CHAT_A)?.messages[1]?.currentVersionIndex).toBe(1);+ expect(useChatStore.getState().messages[1]?.currentVersionIndex).toBe(1);+});diff --git a/apps/mirai-gui/client/src/features/chat/hooks/use-send-chat-message.ts b/apps/mirai-gui/client/src/features/chat/hooks/use-send-chat-message.tsindex d38a2ad9a..9be9b3eed 100644--- a/apps/mirai-gui/client/src/features/chat/hooks/use-send-chat-message.ts+++ b/apps/mirai-gui/client/src/features/chat/hooks/use-send-chat-message.ts@@ -1,7 +1,12 @@import { useCallback } from "react";import { useChatSessionStore } from "@/stores/use-chat-session-store";import { useChatStore } from "@/stores/use-chat-store";+import { resolveModelTools, useModelParamsStore } from "@/stores/use-model-params-store";+import { useModelsStore } from "@/stores/use-models-store";+import { getPlatform } from "@/platform/platform-singleton";+import type { LlmRunResult } from "@/types/llm-stream";import type { AttachedFile } from "@/types/files";+import type { Message } from "@/types/message";import { Roles } from "../types";import { attachmentStorage } from "../services/attachment-storage";import { ChatRunBlockReason } from "../types";@@ -18,15 +23,21 @@ type UseSendChatMessageParams = {runStatusBlockReason: ChatRunBlockReason | null;toast: ToastApi;setScrollTargetId: (id: string | null) => void;- startStream: (options: StartStreamOptions) => Promise<void | null>;+ startStream: (options: StartStreamOptions) => Promise<LlmRunResult | void | null>;+};++type MessageEdit = {+ messageId: string;+ onSaved: () => void;};export const useSendChatMessage = (params: UseSendChatMessageParams) => {const { chatId, composer, globalInstructions, runStatusBlockReason, toast, setScrollTargetId, startStream } = params;return useCallback(- async (message: string): Promise<void> => {+ async (message: string, edit?: MessageEdit): Promise<void> => {const session = useChatSessionStore.getState();+ if (edit && useChatStore.getState().currentChatId !== chatId) return;useChatStore.getState().setCurrentChatId(chatId);const storeState = useChatStore.getState();@@ -35,7 +46,12 @@ export const useSendChatMessage = (params: UseSendChatMessageParams) => {// Captured before any await: the user may open another chat while this// one waits for an eject or a write, and the store then holds that chat.- const history = storeState.messages;+ let history = storeState.messages;+ if (edit) {+ const original = history.find((m) => m.id === edit.messageId && m.sender === Roles.User);+ if (!original) throw new Error("The message is no longer in this chat");+ if (!message.trim() && !original.attachmentIds?.length) return;+ }if (runStatusBlockReason !== null) {if (runStatusBlockReason === ChatRunBlockReason.OtherChatGenerating) {@@ -59,100 +75,153 @@ export const useSendChatMessage = (params: UseSendChatMessageParams) => {return;}- const accepted = await session.withOperation("running", async () => {- const fileIds = composer.attachedFiles.map((file: AttachedFile) => {- attachmentStorage.saveFile(file);- return file.id;- });- const attachmentIds = fileIds.length > 0 ? fileIds : undefined;-- const userMessage = useChatStore.getState().addMessageTo(chatId, {- text: message,- sender: Roles.User,- attachmentIds,- });- const filesToSend = [...composer.attachedFiles];- composer.clear(message);- setScrollTargetId(userMessage.id);-- // Created before the first await so Stop during title generation has a reply to remove.- const placeholder = useChatStore.getState().addMessageTo(chatId, {- text: "",- sender: Roles.Assistant,- modelId: modelId || undefined,- modelName: storeState.chatModels[chatId]?.modelName || undefined,- perf: {},- attachmentIds,- });- session.setLoadingMessage(chatId, placeholder.id);-- const failReply = async (errText: string) => {- session.setLoadingMessage(chatId, null);- useChatStore.getState().updateMessage(placeholder.id, { text: "", error: errText, attachmentIds });- await useChatStore- .getState()- .persistMessage(chatId, placeholder.id, { ...placeholder, text: "", error: errText })- .catch((err) => console.error("[storage] failed to persist error message", err));- };-- try {- await useChatStore.getState().persistMessage(chatId, userMessage.id, userMessage);- await useChatStore.getState().persistMessage(chatId, placeholder.id, placeholder);-- const { messages: messagesForRun } = buildChatRunInput({- history,- prompt: message,- attachments: filesToSend,- globalInstructions,- });-- const titleGenResult = await useChatStore.getState().generateChatTitle(chatId, message);-- if (useChatSessionStore.getState().consumeTitleGenAbort(chatId)) {- // The placeholder is already on disk; left there it would come back- // as a blank bubble after a reload.- session.setCanceledMessage(chatId, null);+ const accepted = await session.withOperation(+ "running",+ async (signal) => {+ const canceled = () => {+ if (!signal.aborted) return false;session.setLoadingMessage(chatId, null);- await useChatStore.getState().discardMessage(chatId, placeholder.id);- return;+ return true;+ };+ let userMessage: Message;+ let filesToSend: AttachedFile[];+ if (edit) {+ // Commit the replacement and truncation together before changing the+ // view. Use the saved history even if the user navigates while saving.+ const messages = await useChatStore.getState().editUserMessage(chatId, edit.messageId, message, signal);+ if (canceled()) return;+ userMessage = messages[messages.length - 1]!;+ history = messages.slice(0, -1);+ filesToSend = attachmentStorage.getFiles(userMessage.attachmentIds ?? []);+ edit.onSaved();+ } else {+ filesToSend = [...composer.attachedFiles];+ const fileIds = filesToSend.map((file) => {+ attachmentStorage.saveFile(file);+ return file.id;+ });+ userMessage = useChatStore.getState().addMessageTo(chatId, {+ text: message,+ sender: Roles.User,+ attachmentIds: fileIds.length > 0 ? fileIds : undefined,+ });+ composer.clear(message);}+ const attachmentIds = userMessage.attachmentIds;+ if (useChatStore.getState().currentChatId === chatId) setScrollTargetId(userMessage.id);++ // Created before the first await so Stop during title generation has a reply to remove.+ const placeholder = useChatStore.getState().addMessageTo(chatId, {+ text: "",+ sender: Roles.Assistant,+ modelId: modelId || undefined,+ modelName: storeState.chatModels[chatId]?.modelName || undefined,+ perf: {},+ attachmentIds,+ });+ session.setLoadingMessage(chatId, placeholder.id);- if (!titleGenResult.ok && titleGenResult.error) {- await failReply(`Error: ${titleGenResult.error}`);- return;- }+ const failReply = async (errText: string) => {+ if (canceled()) return;+ session.setLoadingMessage(chatId, null);+ useChatStore.getState().updateMessage(placeholder.id, { text: "", error: errText, attachmentIds });+ await useChatStore+ .getState()+ .persistMessage(chatId, placeholder.id, { ...placeholder, text: "", error: errText })+ .catch((err) => console.error("[storage] failed to persist error message", err));+ };++ try {+ if (!edit) await useChatStore.getState().persistMessage(chatId, userMessage.id, userMessage);+ if (canceled()) return;+ await useChatStore.getState().persistMessage(chatId, placeholder.id, placeholder);+ if (canceled()) return;++ const { messages: messagesForRun } = buildChatRunInput({+ history,+ prompt: message,+ attachments: filesToSend,+ globalInstructions,+ });++ const { modelChatNamingEnabled, dateTimeToolEnabled, chartToolEnabled } = resolveModelTools(+ useModelParamsStore.getState().getParams(modelId),+ await getPlatform().settings.getModelChatNamingEnabled(),+ useModelsStore.getState().models.find((model) => model.repoId === modelId)?.paramSize,+ );+ if (canceled()) return;++ if (!modelChatNamingEnabled) {+ const titleGenResult = await useChatStore.getState().generateChatTitle(chatId, message);++ if (useChatSessionStore.getState().consumeTitleGenAbort(chatId)) {+ // The placeholder is already on disk; remove it after Stop so it+ // cannot return as a blank bubble after a reload.+ session.setCanceledMessage(chatId, null);+ session.setLoadingMessage(chatId, null);+ await useChatStore.getState().discardMessage(chatId, placeholder.id);+ return;+ }- await startStream({- repoId: modelId,- messages: messagesForRun,- messageId: placeholder.id,- chatId,- updateText: (id: string, updatedText: string) => {- useChatStore.getState().updateMessage(id, { text: updatedText, attachmentIds });- },- onDone: () => session.setLoadingMessage(chatId, null),- onError: (id, errorText) => {- const current = useChatStore.getState().messages.find((m) => m.id === id);- useChatStore.getState().updateMessage(id, {- text: "",- error: errorText,- attachmentIds: current?.attachmentIds,- });- void useChatStore.getState().persistMessageError(chatId, id, "", errorText, current?.attachmentIds);- session.setLoadingMessage(chatId, null);- },- onFinishReason: (reason) => {- if (reason === "ContextLimitReached") {- toast.warning("Conversation reached the model's context limit; reply may be truncated");- } else if (reason === "Length") {- toast.warning("Reply hit the output length limit and may be truncated");+ if (!titleGenResult.ok && titleGenResult.error) {+ await failReply(`Error: ${titleGenResult.error}`);+ return;}- },- });- } catch (e) {- await failReply(`Error: ${e instanceof Error ? e.message : String(e)}`);- }- });+ }+ if (canceled()) return;++ const result = await startStream({+ signal,+ repoId: modelId,+ messages: messagesForRun,+ modelChatNamingEnabled,+ dateTimeToolEnabled,+ chartToolEnabled,+ messageId: placeholder.id,+ chatId,+ updateText: (id: string, updatedText: string) => {+ useChatStore.getState().updateMessage(id, { text: updatedText, attachmentIds });+ },+ onDone: () => session.setLoadingMessage(chatId, null),+ onError: (id, errorText) => {+ const current = useChatStore.getState().messages.find((m) => m.id === id);+ useChatStore.getState().updateMessage(id, {+ text: "",+ error: errorText,+ attachmentIds: current?.attachmentIds,+ });+ void useChatStore.getState().persistMessageError(chatId, id, "", errorText, current?.attachmentIds);+ session.setLoadingMessage(chatId, null);+ },+ onFinishReason: (reason) => {+ if (reason === "ContextLimitReached") {+ toast.warning("Conversation reached the model's context limit; reply may be truncated");+ } else if (reason === "Length") {+ toast.warning("Reply hit the output length limit and may be truncated");+ }+ },+ });+ if (canceled()) return;++ if (+ modelChatNamingEnabled &&+ !history.some((m) => m.sender === Roles.User) &&+ result?.finishReason === "Stop" &&+ !result.error &&+ !result.chatName+ ) {+ const titleGenResult = await useChatStore.getState().generateChatTitle(chatId, message);+ session.consumeTitleGenAbort(chatId);+ if (!titleGenResult.ok && titleGenResult.error) {+ toast.error(`Could not name the chat: ${titleGenResult.error}`);+ }+ }+ } catch (e) {+ await failReply(`Error: ${e instanceof Error ? e.message : String(e)}`);+ }+ },+ chatId,+ );if (accepted === null) toast.error("Model is not ready yet, please wait");},[chatId, composer, globalInstructions, runStatusBlockReason, toast, setScrollTargetId, startStream],diff --git a/apps/mirai-gui/client/src/features/chat/services/regenerate-versions.ts b/apps/mirai-gui/client/src/features/chat/services/regenerate-versions.tsindex b99d960c3..713027bfc 100644--- a/apps/mirai-gui/client/src/features/chat/services/regenerate-versions.ts+++ b/apps/mirai-gui/client/src/features/chat/services/regenerate-versions.ts@@ -33,7 +33,7 @@ export const projectAssistantVersion = (...(target.perf ? { perf: target.perf } : {}),...(target.stats ? { stats: target.stats } : {}),...(target.error ? { error: target.error } : {}),- ...(target.output?.text?.parsed ? { output: { text: { parsed: target.output.text.parsed } } } : {}),+ ...(target.output ? { output: target.output } : {}),};return [frozenOriginal, newVersion];diff --git a/apps/mirai-gui/client/src/features/local-models/components/family-card.tsx b/apps/mirai-gui/client/src/features/local-models/components/family-card.tsxindex fd8ddf8e0..be26c8fcc 100644--- a/apps/mirai-gui/client/src/features/local-models/components/family-card.tsx+++ b/apps/mirai-gui/client/src/features/local-models/components/family-card.tsx@@ -1,17 +1,13 @@import { ChevronRight } from "lucide-react";import type { ReactNode } from "react";import { twMerge } from "tailwind-merge";-import { Tooltip } from "@/components/ui/tooltip";import { Text } from "@/components/ui/typography";export type FamilyCardBadgeTone = "neutral" | "success";-export type FamilyCardBadgeVariant = "outline" | "filled";export type FamilyCardBadge = {label: ReactNode;- tooltip?: ReactNode;tone?: FamilyCardBadgeTone;- variant?: FamilyCardBadgeVariant;};export type FamilyCardProps = {@@ -23,29 +19,26 @@ export type FamilyCardProps = {compact?: boolean;};-const TONE_CLASSES: Record<FamilyCardBadgeTone, { outline: string; filled: string; text: string }> = {+const TONE_CLASSES: Record<FamilyCardBadgeTone, { border: string; text: string }> = {neutral: {- outline: "border-border-outlined bg-transparent",- filled: "border-border-outlined bg-surface-secondary",+ border: "border-border-outlined",text: "text-text-muted",},success: {- outline: "border-success-border bg-transparent",- filled: "border-success-border bg-success-bg",+ border: "border-success-border",text: "text-success",},};function Chip({ badge }: { badge: FamilyCardBadge }) {- const { label, tooltip, tone = "neutral", variant = "outline" } = badge;+ const { label, tone = "neutral" } = badge;const tones = TONE_CLASSES[tone];- const surface = variant === "filled" ? tones.filled : tones.outline;- const chip = (+ return (<divclassName={twMerge(- "flex items-center justify-center h-7 px-2 py-0.5 rounded border-[0.5px] cursor-default",- surface,+ "flex items-center justify-center h-7 px-2 py-0.5 rounded border-[0.5px] bg-transparent cursor-default",+ tones.border,)}><Text@@ -57,15 +50,6 @@ function Chip({ badge }: { badge: FamilyCardBadge }) {</Text></div>);-- if (tooltip) {- return (- <Tooltip content={tooltip} side="top">- {chip}- </Tooltip>- );- }- return chip;}export function FamilyCard({ vendorIcon, familyName, vendorName, badges, onClick, compact = false }: FamilyCardProps) {diff --git a/apps/mirai-gui/client/src/features/local-models/components/family-detail-page.tsx b/apps/mirai-gui/client/src/features/local-models/components/family-detail-page.tsxindex 8f116df32..33f9de2c2 100644--- a/apps/mirai-gui/client/src/features/local-models/components/family-detail-page.tsx+++ b/apps/mirai-gui/client/src/features/local-models/components/family-detail-page.tsx@@ -5,7 +5,6 @@ import { formatModelSize } from "@/utils/format";import { ModelVendorIcon } from "@/components/model-vendor-icon";import { ModelCard } from "./model-card";import { ModelTable } from "./model-table";-import { useShiftHeld } from "@/hooks/use-shift-held";import { useMemo, type MouseEvent } from "react";import { buildFamilyDetailView, formatQuantization, toModelCardState, type FamilyEntry } from "../lib/view";@@ -33,7 +32,6 @@ export function FamilyDetailPage({onOpen,}: FamilyDetailPageProps) {const isMobile = useIsMobile();- const shiftHeld = useShiftHeld();const models = useModelsStore((s) => s.models);const modelStatesById = useModelsStore((s) => s.modelStatesById);const modelPhasesById = useModelsStore((s) => s.modelPhasesById);@@ -68,7 +66,6 @@ export function FamilyDetailPage({onCancel={() => void onCancel(model)}onRetry={() => void onDownload(model)}onDelete={(event) => onDelete(model, event)}- quickDelete={shiftHeld}onOpen={() => onOpen(model)}/>);@@ -89,7 +86,7 @@ export function FamilyDetailPage({return (<div className="flex flex-col gap-6">{detail.installed.length > 0 && (- <ModelTable title="Installed" compact={isMobile}>+ <ModelTable title="Downloaded" compact={isMobile}>{detail.installed.map((m) => renderRow(m))}</ModelTable>)}diff --git a/apps/mirai-gui/client/src/features/local-models/components/local-models-page/index.tsx b/apps/mirai-gui/client/src/features/local-models/components/local-models-page/index.tsxindex 612aaa709..4fd16e0e2 100644--- a/apps/mirai-gui/client/src/features/local-models/components/local-models-page/index.tsx+++ b/apps/mirai-gui/client/src/features/local-models/components/local-models-page/index.tsx@@ -9,14 +9,13 @@ import { createNewestTextModelComparator, createSizeAccessor } from "../../lib/mimport { useNavigate } from "@tanstack/react-router";import { Button } from "@/components/ui/button";import { FamilyCard, type FamilyCardBadge } from "../family-card";-import { HoverPopoverProvider } from "@/components/ui/popover/hover-popover/provider";import { Modal } from "@/components/ui/modal";import { Text } from "@/components/ui/typography";import { TooltipProvider } from "@/components/ui/tooltip/provider";import { RefreshCcw } from "lucide-react";import { useCallback, useEffect, useMemo, useState } from "react";import { v4 as uuidv4 } from "uuid";-import { buildFamiliesView, formatParamRange, isPartnerFamily, type FamilyEntry } from "../../lib/view";+import { buildFamiliesView, formatParamRange, type FamilyEntry } from "../../lib/view";import { FamilyDetailPage } from "../family-detail-page";import { LocalModelsHeader } from "./local-models-header";import { useModelDeletion } from "./use-model-deletion";@@ -30,7 +29,6 @@ export function LocalModelsPage() {const createNewChat = useChatStore((s) => s.createNewChat);const { handleDownload, handlePause, handleResume, handleCancel } = useDownloadManager();- const [loading, setLoading] = useState(true);const [searchQuery, setSearchQuery] = useState("");const [modelsSearch, setModelsSearch] = useState("");const [selectedFamilyId, setSelectedFamilyId] = useState<string | null>(null);@@ -44,8 +42,11 @@ export function LocalModelsPage() {const models = useModelsStore((s) => s.models);const loadingModels = useModelsStore((s) => s.loading);const modelsInitialized = useModelsStore((s) => s.initialized);+ const catalogComplete = useModelsStore((s) => s.catalogComplete);+ const catalogRefreshing = useModelsStore((s) => s.catalogRefreshing);const modelsError = useModelsStore((s) => s.error);const fetchModels = useModelsStore((s) => s.fetchModels);+ const refreshModels = useModelsStore((s) => s.refreshModels);const getModelState = useModelsStore((s) => s.getModelState);const modelPhasesById = useModelsStore((s) => s.modelPhasesById);const installedAtById = useModelsStore((s) => s.installedAtById);@@ -54,15 +55,7 @@ export function LocalModelsPage() {useModelDeletion(fetchModels);useEffect(() => {- const loadPage = async () => {- try {- await fetchModels();- } finally {- setLoading(false);- }- };-- void loadPage();+ void fetchModels();}, [fetchModels]);const getSize = useMemo(() => createSizeAccessor(getModelState), [getModelState]);@@ -113,15 +106,17 @@ export function LocalModelsPage() {const handleRefresh = useCallback(async () => {try {setRefreshing(true);- await fetchModels();+ await refreshModels();} finally {setRefreshing(false);}- }, [fetchModels]);+ }, [refreshModels]);++ const incompleteCatalog = modelsInitialized && !catalogComplete && !catalogRefreshing;- if (loading || loadingModels || !modelsInitialized) {+ if (models.length === 0 && (loadingModels || !modelsInitialized)) {return (- <div className="flex h-[calc(100vh-24px)] items-center justify-center">+ <div className="flex h-full items-center justify-center"><Loader /></div>);@@ -129,134 +124,129 @@ export function LocalModelsPage() {return (<TooltipProvider>- <HoverPopoverProvider>- <div className="flex h-[calc(100vh-24px)] flex-col bg-background">- <div className="w-full flex h-full overflow-hidden bg-surface-elevated">- <div className="flex-1 min-w-0 flex flex-col">- <LocalModelsHeader- selectedFamily={selectedFamily}- isMobile={isMobile}- searchQuery={searchQuery}- modelsSearch={modelsSearch}- onSearchQueryChange={setSearchQuery}- onModelsSearchChange={setModelsSearch}- onBack={() => selectFamily(null)}- />- <div style={{ borderBottom: "0.5px solid var(--color-border-default)" }}></div>-- <div className="flex-1 overflow-y-auto scrollbar-hide">- <div className="px-4 py-4">- {modelsError ? (- <div className="my-4">- <div className="flex items-start gap-2 rounded-md border border-red-200 bg-red-50 p-3 dark:border-red-800 dark:bg-red-900/20">- <div className="mt-[6px] h-2 w-2 rounded-full bg-red-500" />- <div className="flex-1">- <div className="text-sm text-red-700 dark:text-red-300">Failed to load models</div>+ <div className="flex h-full min-h-0 flex-col bg-background">+ <div className="w-full flex h-full min-h-0 overflow-clip bg-surface-elevated">+ <div className="flex-1 min-w-0 min-h-0 flex flex-col">+ <LocalModelsHeader+ selectedFamily={selectedFamily}+ isMobile={isMobile}+ searchQuery={searchQuery}+ modelsSearch={modelsSearch}+ onSearchQueryChange={setSearchQuery}+ onModelsSearchChange={setModelsSearch}+ onBack={() => selectFamily(null)}+ />+ <div style={{ borderBottom: "0.5px solid var(--color-border-default)" }}></div>++ <div className="flex-1 min-h-0 overflow-y-auto overscroll-y-contain scrollbar-hide">+ <div className="px-4 py-4">+ {(modelsError || incompleteCatalog) && (+ <div className="my-4">+ <div className="flex items-start gap-2 rounded-md border border-red-200 bg-red-50 p-3 dark:border-red-800 dark:bg-red-900/20">+ <div className="mt-[6px] h-2 w-2 rounded-full bg-red-500" />+ <div className="flex-1">+ <div className="text-sm text-red-700 dark:text-red-300">+ {models.length > 0 ? "Some models could not be loaded" : "Could not load models"}+ </div>+ {modelsError && (<div className="mt-1 break-all text-xs text-red-700/80 dark:text-red-300/80">{modelsError}</div>- </div>- <div className="shrink-0">- <Button- icon={<RefreshCcw size={16} />}- kind="secondary"- size="sm"- loading={refreshing}- disabled={refreshing}- onClick={handleRefresh}- >- {refreshing ? "Retrying…" : "Retry"}- </Button>- </div>+ )}+ </div>+ <div className="shrink-0">+ <Button+ icon={<RefreshCcw size={16} />}+ kind="secondary"+ size="sm"+ loading={refreshing || catalogRefreshing}+ disabled={refreshing || catalogRefreshing}+ onClick={handleRefresh}+ >+ {refreshing || catalogRefreshing ? "Retrying…" : "Retry"}+ </Button></div></div>- ) : models.length === 0 ? (+ </div>+ )}+ {models.length === 0 ? (+ modelsError || incompleteCatalog ? null : (<div className="flex min-h-[320px] items-center justify-center"><span className={`${TEXT_14} text-text-muted`} style={TEXT_14_STYLE}>- No models available for this device- </span>- </div>- ) : selectedFamily ? (- <FamilyDetailPage- family={selectedFamily}- searchQuery={modelsSearch}- sortModels={sortModels}- onDownload={handleDownload}- onPause={handlePause}- onResume={handleResume}- onCancel={handleCancel}- onDelete={requestDeleteModel}- onOpen={handleInstalledModelClick}- />- ) : families.length === 0 ? (- <div className="flex min-h-[200px] items-center justify-center">- <span className="text-[13px] text-text-muted">- {searchQuery ? `No families matching "${searchQuery}"` : "No model families"}+ {catalogRefreshing ? "Loading models…" : "No models available for this device"}</span></div>- ) : (- <div className="flex flex-col gap-1.5">- {families.map((family) => {- const sizeRange = formatParamRange(family.paramRange?.min, family.paramRange?.max);- const badges: FamilyCardBadge[] = [- ...(isPartnerFamily(family.familyName)- ? [- {- label: "Partner",- tooltip: "Partner of Mirai — co-developed and verified to work on your hardware",- tone: "success" as const,- variant: "filled" as const,- },- ]- : []),- {- label: `${family.totalCount} ${family.totalCount === 1 ? "model" : "models"}`,- },- ...(family.installedCount > 0- ? [- {- label: `${family.installedCount} installed ${family.installedCount === 1 ? "model" : "models"}`,- tone: "success" as const,- },- ]- : []),- ...(sizeRange ? [{ label: sizeRange }] : []),- ];- return (- <FamilyCard- key={family.familyIdentifier}- compact={isMobile}- vendorIcon={<ModelVendorIcon vendor={family.vendor} size={16} className="h-4 w-4" />}- familyName={family.familyName}- vendorName={family.vendor}- badges={badges}- onClick={() => selectFamily(family.familyIdentifier)}- />- );- })}- </div>- )}- </div>+ )+ ) : selectedFamily ? (+ <FamilyDetailPage+ family={selectedFamily}+ searchQuery={modelsSearch}+ sortModels={sortModels}+ onDownload={handleDownload}+ onPause={handlePause}+ onResume={handleResume}+ onCancel={handleCancel}+ onDelete={requestDeleteModel}+ onOpen={handleInstalledModelClick}+ />+ ) : families.length === 0 ? (+ <div className="flex min-h-[200px] items-center justify-center">+ <span className="text-[13px] text-text-muted">+ {searchQuery ? `No families matching "${searchQuery}"` : "No model families"}+ </span>+ </div>+ ) : (+ <div className="flex flex-col gap-1.5">+ {families.map((family) => {+ const sizeRange = formatParamRange(family.paramRange?.min, family.paramRange?.max);+ const badges: FamilyCardBadge[] = [+ ...(family.installedCount > 0+ ? [+ {+ label: `${family.installedCount} downloaded ${family.installedCount === 1 ? "model" : "models"}`,+ tone: "success" as const,+ },+ ]+ : []),+ {+ label: `${family.totalCount} ${family.totalCount === 1 ? "model" : "models"}`,+ },+ ...(sizeRange ? [{ label: sizeRange }] : []),+ ];+ return (+ <FamilyCard+ key={family.familyIdentifier}+ compact={isMobile}+ vendorIcon={<ModelVendorIcon vendor={family.vendor} size={16} className="h-4 w-4" />}+ familyName={family.familyName}+ vendorName={family.vendor}+ badges={badges}+ onClick={() => selectFamily(family.familyIdentifier)}+ />+ );+ })}+ </div>+ )}</div></div></div>- <Modal- open={pendingDeleteModel != null}- onClose={closeDeleteModal}- title="Delete model"- primaryLabel="Delete"- primaryKind="danger"- primaryDisabled={isDeletingModel}- onPrimary={() => {- void confirmDeleteModel();- }}- >- <Text as="p" color="secondary" opticalSize={20} className="text-[15px] font-[450] leading-[1.6]">- Are you sure you want to delete model "{pendingDeleteModel?.repoId ?? ""}"? This action cannot be undone.- </Text>- </Modal></div>- </HoverPopoverProvider>+ <Modal+ open={pendingDeleteModel != null}+ onClose={closeDeleteModal}+ title="Delete model"+ primaryLabel="Delete"+ primaryKind="danger"+ primaryDisabled={isDeletingModel}+ onPrimary={() => {+ void confirmDeleteModel();+ }}+ >+ <Text as="p" color="secondary" opticalSize={20} className="text-[15px] font-[450] leading-[1.6]">+ Are you sure you want to delete model "{pendingDeleteModel?.repoId ?? ""}"? This action cannot be undone.+ </Text>+ </Modal>+ </div></TooltipProvider>);}diff --git a/apps/mirai-gui/client/src/features/local-models/components/local-models-page/local-models-header.tsx b/apps/mirai-gui/client/src/features/local-models/components/local-models-page/local-models-header.tsxindex 5cb66427d..2fb63abbf 100644--- a/apps/mirai-gui/client/src/features/local-models/components/local-models-page/local-models-header.tsx+++ b/apps/mirai-gui/client/src/features/local-models/components/local-models-page/local-models-header.tsx@@ -58,7 +58,7 @@ export function LocalModelsHeader({<><MonitorSmartphone size={16} className="text-text-primary shrink-0" /><span className={`${TEXT_14} text-text-primary truncate`} style={TEXT_14_STYLE}>- Choose local model to chat+ Choose a model to chat</span></>)}diff --git a/apps/mirai-gui/client/src/features/local-models/components/local-models-page/use-model-deletion.ts b/apps/mirai-gui/client/src/features/local-models/components/local-models-page/use-model-deletion.tsindex 3c869e542..e965a2b3b 100644--- a/apps/mirai-gui/client/src/features/local-models/components/local-models-page/use-model-deletion.ts+++ b/apps/mirai-gui/client/src/features/local-models/components/local-models-page/use-model-deletion.ts@@ -27,7 +27,7 @@ export function useModelDeletion(fetchModels: () => Promise<void>): ModelDeletio}await useModelsStore.getState().deleteLocalModel(model.repoId);- toast.success(`Uninstalled ${model.name}`);+ toast.success(`Deleted ${model.name}`);} catch (error) {toast.error(`Failed to uninstall ${model.name}`);console.error(error);diff --git a/apps/mirai-gui/client/src/features/local-models/components/model-card/download-actions.tsx b/apps/mirai-gui/client/src/features/local-models/components/model-card/download-actions.tsxindex 8ea9b4274..191c0345c 100644--- a/apps/mirai-gui/client/src/features/local-models/components/model-card/download-actions.tsx+++ b/apps/mirai-gui/client/src/features/local-models/components/model-card/download-actions.tsx@@ -1,4 +1,4 @@-import { Download, RefreshCw, X } from "lucide-react";+import { Download, LoaderCircle, RefreshCw, X } from "lucide-react";import { IconAction } from "@/components/ui/icon-action";import { IconPauseFilled } from "@/components/icons/pause-filled-icon";import { IconPlayFilled } from "@/components/icons/play-filled-icon";@@ -31,6 +31,16 @@ export function DownloadActions({}: DownloadActionsProps) {return (<>+ {status === "initializing" && (+ <span+ role="status"+ aria-label="Checking model"+ title="Checking model"+ className="flex h-7 w-7 items-center justify-center text-text-muted"+ >+ <LoaderCircle size={16} className="animate-spin motion-reduce:animate-none" />+ </span>+ )}{isError && onRetry && (<Tooltip content="Retry" side="top"><span>diff --git a/apps/mirai-gui/client/src/features/local-models/components/model-card/model-card-row.tsx b/apps/mirai-gui/client/src/features/local-models/components/model-card/model-card-row.tsxindex 72f6d26e7..cbf477d4c 100644--- a/apps/mirai-gui/client/src/features/local-models/components/model-card/model-card-row.tsx+++ b/apps/mirai-gui/client/src/features/local-models/components/model-card/model-card-row.tsx@@ -1,4 +1,5 @@import type { KeyboardEvent } from "react";+import { Trash2 } from "lucide-react";import { twMerge } from "tailwind-merge";import { IconCheckStroke } from "@/components/icons/check-stroke-icon";import { Tooltip } from "@/components/ui/tooltip";@@ -8,7 +9,6 @@ import { deriveModelCardState } from "./model-card-state";import { DownloadActions } from "./download-actions";import { ModelBadges } from "./model-badges";import { ModelHeading } from "./model-heading";-import { ModelOptions } from "./model-options";export function ModelCardRow({name,@@ -23,7 +23,6 @@ export function ModelCardRow({onRetry,onOpen,compact = false,- quickDelete = false,}: ModelCardProps) {const { status, isDownloading, isDownloaded, isPaused, isError, progress } = deriveModelCardState(state);@@ -108,14 +107,23 @@ export function ModelCardRow({/>{isDownloaded && (<>- <Tooltip content="Installed" side="top">+ <Tooltip content="Downloaded" side="top"><div className="flex items-center justify-center h-7 w-7 rounded border-[0.5px] border-border-outlined text-success"><IconCheckStroke /></div></Tooltip>- <div className="relative" onClick={(event) => event.stopPropagation()}>- <ModelOptions quickDelete={quickDelete} onDelete={onDelete} />- </div>+ <button+ type="button"+ aria-label={`Delete ${name}`}+ title="Delete model (hold Shift to skip confirmation)"+ onClick={(event) => {+ event.stopPropagation();+ onDelete?.(event);+ }}+ className="flex items-center justify-center h-7 w-7 rounded cursor-pointer text-text-muted hover:text-danger bg-transparent hover:border-border-outlined-hover border-[0.5px] border-border-outlined outline-hidden focus-visible:shadow-focus"+ >+ <Trash2 size={14} />+ </button></>)}</div>@@ -129,7 +137,7 @@ export function ModelCardRow({{isDownloading && (<div className="mt-2.5 pl-4 pr-3">- <div className="h-0.5 bg-border-default rounded overflow-hidden">+ <div className="h-0.5 bg-border-default rounded overflow-clip"><div className="h-full bg-primary transition-[width] duration-200" style={{ width: `${progress}%` }} /></div></div>diff --git a/apps/mirai-gui/client/src/features/local-models/components/model-card/model-options.tsx b/apps/mirai-gui/client/src/features/local-models/components/model-card/model-options.tsxdeleted file mode 100644index cd4ae274a..000000000--- a/apps/mirai-gui/client/src/features/local-models/components/model-card/model-options.tsx+++ /dev/null@@ -1,48 +0,0 @@-import { Ellipsis, Trash2 } from "lucide-react";-import type { MouseEvent } from "react";-import { HoverPopover } from "@/components/ui/popover/hover-popover";--type ModelOptionsProps = {- quickDelete?: boolean;- onDelete?: (event: MouseEvent<HTMLElement>) => void;-};--export function ModelOptions({ quickDelete = false, onDelete }: ModelOptionsProps) {- if (quickDelete) {- return (- <button- type="button"- aria-label="Delete model"- onClick={onDelete}- className="flex items-center justify-center h-7 w-7 rounded cursor-pointer text-danger bg-transparent hover:border-border-outlined-hover border-[0.5px] border-border-outlined outline-hidden focus-visible:shadow-focus"- >- <Trash2 size={14} />- </button>- );- }-- return (- <HoverPopover- side="top"- align="end"- trigger={- <button- type="button"- aria-label="Model options"- className="group/chip flex items-center justify-center h-7 w-7 rounded cursor-pointer text-text-muted hover:text-text-primary bg-transparent hover:border-border-outlined-hover border-[0.5px] border-border-outlined outline-hidden focus-visible:shadow-focus"- >- <Ellipsis size={16} />- </button>- }- items={[- {- id: "delete",- label: "Delete",- icon: <Trash2 size={14} />,- danger: true,- onClick: onDelete,- },- ]}- />- );-}diff --git a/apps/mirai-gui/client/src/features/local-models/components/model-card/types.ts b/apps/mirai-gui/client/src/features/local-models/components/model-card/types.tsindex 027f73adc..97eb79cc9 100644--- a/apps/mirai-gui/client/src/features/local-models/components/model-card/types.ts+++ b/apps/mirai-gui/client/src/features/local-models/components/model-card/types.ts@@ -1,6 +1,7 @@import type { MouseEvent, ReactNode } from "react";export type ModelCardState =+ | { status: "initializing" }| { status: "available" }| { status: "downloading"; progress: number }| { status: "paused"; progress: number }@@ -19,7 +20,6 @@ export type ModelCardProps = {onPause?: () => void;onCancel?: () => void;onDelete?: (event: MouseEvent<HTMLElement>) => void;- quickDelete?: boolean;onRetry?: () => void;onOpen?: () => void;compact?: boolean;diff --git a/apps/mirai-gui/client/src/features/local-models/components/model-table.tsx b/apps/mirai-gui/client/src/features/local-models/components/model-table.tsxindex e4d462149..d89400750 100644--- a/apps/mirai-gui/client/src/features/local-models/components/model-table.tsx+++ b/apps/mirai-gui/client/src/features/local-models/components/model-table.tsx@@ -24,7 +24,7 @@ export function ModelTable({ title, children, compact = false }: ModelTableProps<span aria-hidden /></div>- <div className="rounded-md border-[0.5px] border-border-default overflow-hidden divide-y-[0.5px] divide-border-default bg-surface-elevated">+ <div className="rounded-md border-[0.5px] border-border-default overflow-clip divide-y-[0.5px] divide-border-default bg-surface-elevated">{children}</div></div>diff --git a/apps/mirai-gui/client/src/features/local-models/lib/view.ts b/apps/mirai-gui/client/src/features/local-models/lib/view.tsindex cee8a47ea..1fc0162d2 100644--- a/apps/mirai-gui/client/src/features/local-models/lib/view.ts+++ b/apps/mirai-gui/client/src/features/local-models/lib/view.ts@@ -8,6 +8,7 @@ export function toModelCardState(phase?: string, downloadedKbytes?: number, totatotalKbytes && totalKbytes > 0 ? Math.min(100, Math.round(((downloadedKbytes ?? 0) / totalKbytes) * 100)) : 0;const mappedState: Record<string, ModelCardState> = {+ [modelDownloadPhases.initializing]: { status: "initializing" },[modelDownloadPhases.downloaded]: { status: "downloaded" },[modelDownloadPhases.paused]: { status: "paused", progress },[modelDownloadPhases.downloading]: { status: "downloading", progress },@@ -70,12 +71,6 @@ function familyKey(model: PlatformModel): string {return model.familyIdentifier ?? model.family ?? model.vendor ?? model.repoId;}-const PARTNER_FAMILIES = new Set<string>(["LFM2.5"]);--export function isPartnerFamily(familyName: string): boolean {- return PARTNER_FAMILIES.has(familyName.trim());-}-export function buildFamiliesView(params: {models: PlatformModel[];modelPhasesById: Record<string, string | undefined>;diff --git a/apps/mirai-gui/client/src/features/settings/components/about-tab.tsx b/apps/mirai-gui/client/src/features/settings/components/about-tab.tsxindex 73cf99ef6..582ce8147 100644--- a/apps/mirai-gui/client/src/features/settings/components/about-tab.tsx+++ b/apps/mirai-gui/client/src/features/settings/components/about-tab.tsx@@ -1,4 +1,5 @@import { ChevronRight, Heart } from "lucide-react";+import { Button } from "@/components/ui/button";const links = [{title: "Website",@@ -27,16 +28,37 @@ const productLinks = [},];+const legalLinks = [+ {+ title: "Terms of Service",+ url: "https://artifacts.trymirai.com/legal/Mirai_Tech_Terms_of_Use.pdf",+ },+ {+ title: "Privacy Policy",+ url: "https://artifacts.trymirai.com/legal/Mirai_Tech_Privacy_Policy.pdf",+ },+];+export default function AboutTab() {return (<div className="h-full flex flex-col justify-between"><div className="flex flex-col"><div className="flex flex-col gap-4 px-5 lg:max-w-[800px] mx-auto w-full"><Heart className="w-6 h-6 text-blue" />- <h3 className="text-[18px] font-medium leading-[130%] tracking-[0.2px] text-label-title truncate">- Built by a team that shares a vision for <br />- accessible and powerful local AI.- </h3>+ <div className="flex flex-wrap items-center justify-between gap-4">+ <h3 className="text-[18px] font-medium leading-[130%] tracking-[0.2px] text-label-title">+ Built by a team that shares a vision for <br />+ accessible and powerful local AI.+ </h3>+ <Button+ size="sm"+ href="https://discord.gg/trymirai"+ target="_blank"+ className="bg-blue/10 text-blue hover:bg-blue/20 active:bg-blue/25"+ >+ Join our Discord+ </Button>+ </div></div><div className="mt-5 h-[1px] bg-cell-border" />@@ -49,7 +71,9 @@ export default function AboutTab() {rel="noopener noreferrer"className="hover:bg-bg-sub flex items-center justify-between px-5 py-5 border-r last:border-r-0 border-cell-border">- <h4 className="text-[15px] font-[350] leading-[150%] text-label-title overflow-hidden">{link.title}</h4>+ <h4 className="min-w-0 text-[15px] font-[350] leading-[150%] text-label-title overflow-clip">+ {link.title}+ </h4><ChevronRight className="w-[14px] h-[14px] text-label-muted" /></a>))}@@ -72,9 +96,7 @@ export default function AboutTab() {className="lg:max-w-[800px] mx-auto w-full hover:bg-bg-sub flex items-center justify-between px-5 py-5"><div className="flex flex-col gap-1">- <h4 className="text-[15px] font-[350] leading-[150%] text-label-title overflow-hidden">- {link.title}- </h4>+ <h4 className="text-[15px] font-[350] leading-[150%] text-label-title overflow-clip">{link.title}</h4><p className="text-[13px] font-[350] leading-[150%] text-label-muted">{link.description}</p></div>@@ -84,6 +106,22 @@ export default function AboutTab() {</div>))}</div>++ <div className="h-[1px] bg-cell-border" />+ <div className="grid grid-cols-2 lg:max-w-[800px] mx-auto w-full">+ {legalLinks.map((link) => (+ <a+ key={link.url}+ href={link.url}+ target="_blank"+ rel="noopener noreferrer"+ className="hover:bg-bg-sub flex items-center justify-between px-5 py-5 border-r last:border-r-0 border-cell-border"+ >+ <h4 className="text-[15px] font-[350] leading-[150%] text-label-title">{link.title}</h4>+ <ChevronRight className="w-[14px] h-[14px] text-label-muted" />+ </a>+ ))}+ </div></div></div>);diff --git a/apps/mirai-gui/client/src/features/settings/components/general-tab.test.tsx b/apps/mirai-gui/client/src/features/settings/components/general-tab.test.tsxindex c2f57e439..5bd7e86a4 100644--- a/apps/mirai-gui/client/src/features/settings/components/general-tab.test.tsx+++ b/apps/mirai-gui/client/src/features/settings/components/general-tab.test.tsx@@ -1,5 +1,7 @@-import { fireEvent, render, screen } from "@testing-library/react";-import { describe, expect, it, vi } from "vitest";+import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { useAppStore } from "@/stores/use-app-store";+import { APP_STORE_KEY } from "@/stores/migrate-app-storage";import GeneralTab from "./general-tab";vi.mock("@/platform/platform-singleton", () => ({@@ -7,47 +9,183 @@ vi.mock("@/platform/platform-singleton", () => ({}));vi.mock("@/platform/platform-info", () => ({- platformInfo: { isTauri: true, features: { globalShortcut: true } },+ platformInfo: { isTauri: true, features: { logExport: true } },}));const settings = {- enableThinking: true,- runOnStartup: false,- quickEntryShortcut: "CommandOrControl+Shift+M",+ analyticsEnabled: false,+ modelChatNamingEnabled: true,autoEjectEnabled: true,autoEjectMinutes: 7,fetch: vi.fn(),- fetchDesktopSettings: vi.fn(() => Promise.resolve()),- setEnableThinking: vi.fn(),- setRunOnStartup: vi.fn(),- registerQuickEntryShortcut: vi.fn(() => Promise.resolve(true)),- unregisterQuickEntryShortcut: vi.fn(() => Promise.resolve()),+ setAnalyticsEnabled: vi.fn(),+ setModelChatNamingEnabled: vi.fn(),setAutoEjectEnabled: vi.fn(),setAutoEjectMinutes: vi.fn(),+ exportLogs: vi.fn(async () => "ok"),};vi.mock("@/stores/use-settings-store", () => ({ useSettingsStore: () => settings }));+const exportChats = vi.hoisted(() => vi.fn(async () => true));+vi.mock("@/features/chat/services/export-chats", () => ({ exportAllChatsZip: exportChats }));+vi.mock("./clear-data-dialog", () => ({+ ClearDataDialog: ({ open }: { open: boolean }) => (open ? <div role="dialog">Clear data confirmation</div> : null),+}));+vi.mock("@/stores/use-global-instructions-store", () => ({useGlobalInstructionsStore: (selector: (s: unknown) => unknown) =>selector({ instructions: "", loadInstructions: vi.fn(), saveInstructions: vi.fn() }),}));-vi.mock("@/stores/use-app-store", () => ({- useAppStore: (selector: (s: unknown) => unknown) => selector({ isDarkMode: true, setDarkMode: vi.fn() }),-}));+const appDefaults = useAppStore.getState();++beforeEach(() => {+ vi.clearAllMocks();+ vi.stubGlobal(+ "ResizeObserver",+ class {+ observe() {}+ unobserve() {}+ disconnect() {}+ },+ );+ useAppStore.setState(appDefaults, true);+ localStorage.clear();+});++afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+});++it("lets users disable model-provided chat names", () => {+ render(<GeneralTab />);+ const toggle = screen.getByRole("switch", { name: "Let models name chats via tool call" });+ expect(toggle.getAttribute("aria-checked")).toBe("true");++ fireEvent.click(toggle);+ expect(settings.setModelChatNamingEnabled).toHaveBeenCalledWith(false);+});++it("leaves usage analytics off until the user opts in", () => {+ render(<GeneralTab />);+ const toggle = screen.getByRole("switch", { name: "Share usage analytics" });+ expect(toggle.getAttribute("aria-checked")).toBe("false");+ expect(settings.setAnalyticsEnabled).not.toHaveBeenCalled();++ fireEvent.click(toggle);+ expect(settings.setAnalyticsEnabled).toHaveBeenCalledWith(true);+});-describe("quick entry shortcut capture", () => {- it("registers a combination with a system modifier and ignores one without", async () => {- render(<GeneralTab />);- fireEvent.click(screen.getByRole("button", { name: /⌘ ⇧ M/ }));- await new Promise((r) => setTimeout(r, 0));+it("opens analytics details in a popover that dismisses with Escape or an outside click", async () => {+ render(<GeneralTab />);+ const more = screen.getByRole("button", { name: "See more" });+ expect(more.getAttribute("aria-expanded")).toBe("false");+ expect(screen.queryByRole("region", { name: "Usage analytics details" })).toBeNull();- fireEvent.keyDown(window, { key: "k", code: "KeyK", metaKey: true, shiftKey: true });- expect(settings.registerQuickEntryShortcut).toHaveBeenCalledWith("CommandOrControl+Shift+K");+ fireEvent.click(more);+ const details = await screen.findByRole("region", { name: "Usage analytics details" });+ expect(more.getAttribute("aria-expanded")).toBe("true");+ expect(more.getAttribute("aria-controls")).toBe(details.id);+ expect(within(details).getAllByRole("listitem")).toHaveLength(5);+ expect(within(details).getByText(/Chat content, attachments, chat names, local paths/).textContent).toContain(+ "connection’s IP address",+ );+ expect(settings.setAnalyticsEnabled).not.toHaveBeenCalled();- settings.registerQuickEntryShortcut.mockClear();- fireEvent.keyDown(window, { key: "j", code: "KeyJ", shiftKey: true });- expect(settings.registerQuickEntryShortcut).not.toHaveBeenCalled();+ await waitFor(() => expect(details.contains(document.activeElement)).toBe(true));+ fireEvent.keyDown(document.activeElement!, { key: "Escape" });+ await waitFor(() => expect(screen.queryByRole("region", { name: "Usage analytics details" })).toBeNull());+ expect(more.getAttribute("aria-expanded")).toBe("false");+ expect(document.activeElement).toBe(more);+ expect(more.hasAttribute("data-focus")).toBe(true);++ fireEvent.click(more);+ await screen.findByRole("region", { name: "Usage analytics details" });+ const outside = screen.getByRole("heading", { name: "Theme" });+ fireEvent.pointerDown(outside);+ fireEvent.pointerUp(outside);+ fireEvent.click(outside);+ await waitFor(() => expect(screen.queryByRole("region", { name: "Usage analytics details" })).toBeNull());+ expect(more.getAttribute("aria-expanded")).toBe("false");+});++it("keeps focus without a keyboard ring when See more is closed by clicking it again", async () => {+ render(<GeneralTab />);+ const more = screen.getByRole("button", { name: "See more" });+ const clickWithMouse = () => {+ fireEvent.pointerDown(more, { pointerType: "mouse" });+ fireEvent.mouseDown(more);+ fireEvent.pointerUp(more, { pointerType: "mouse" });+ fireEvent.mouseUp(more);+ fireEvent.click(more, { detail: 1 });+ };++ clickWithMouse();+ const details = await screen.findByRole("region", { name: "Usage analytics details" });+ await waitFor(() => expect(details.contains(document.activeElement)).toBe(true));++ clickWithMouse();+ await waitFor(() => expect(screen.queryByRole("region", { name: "Usage analytics details" })).toBeNull());+ expect(document.activeElement).toBe(more);+ expect(more.hasAttribute("data-focus")).toBe(false);+});++it("makes exports and data cleanup available in General settings", async () => {+ render(<GeneralTab />);+ const chatsRow = screen.getByRole("heading", { name: "Export all your chats" }).closest(".justify-between")!;+ const logsRow = screen.getByRole("heading", { name: "Export logs" }).closest(".justify-between")!;+ fireEvent.click(within(chatsRow as HTMLElement).getByRole("button", { name: "Export" }));+ fireEvent.click(within(logsRow as HTMLElement).getByRole("button", { name: "Export" }));+ await waitFor(() => {+ expect(exportChats).toHaveBeenCalledOnce();+ expect(settings.exportLogs).toHaveBeenCalledOnce();});+ expect(screen.queryByRole("dialog")).toBeNull();+ fireEvent.click(screen.getByRole("button", { name: "Clear data" }));+ expect(screen.getByRole("dialog").textContent).toBe("Clear data confirmation");+});++it("updates and persists chat width from General settings", () => {+ render(<GeneralTab />);+ expect(screen.getByRole("radio", { name: "Medium" }).getAttribute("aria-checked")).toBe("true");++ for (const [label, width] of [+ ["Narrow", 800],+ ["Wide", 1200],+ ["Medium", 1000],+ ] as const) {+ const option = screen.getByRole("radio", { name: label });+ fireEvent.click(option);+ expect(option.getAttribute("aria-checked")).toBe("true");+ expect(useAppStore.getState().chatWidth).toBe(width);+ expect(JSON.parse(localStorage.getItem(APP_STORE_KEY)!).state.chatWidth).toBe(width);+ }+});++it("persists System, Light, and Dark choices through the custom theme selector", async () => {+ render(<GeneralTab />);+ let selectedLabel = "System";+ for (const [mode, label] of [+ ["light", "Light"],+ ["dark", "Dark"],+ ["system", "System"],+ ] as const) {+ fireEvent.click(screen.getByRole("button", { name: `Theme: ${selectedLabel}` }));+ const popup = await screen.findByRole("listbox", { name: `Theme: ${selectedLabel}` });+ expect(screen.getByRole("option", { name: selectedLabel }).getAttribute("aria-selected")).toBe("true");+ const option = screen.getByRole("option", { name: label });+ if (mode === "dark") {+ fireEvent.keyDown(popup, { key: "End" });+ await waitFor(() => expect(popup.getAttribute("aria-activedescendant")).toBe(option.id));+ fireEvent.keyDown(popup, { key: "Enter" });+ } else {+ fireEvent.click(option);+ }+ expect(screen.getByRole("button", { name: `Theme: ${label}` })).toBeTruthy();+ expect(useAppStore.getState().theme).toBe(mode);+ expect(JSON.parse(localStorage.getItem(APP_STORE_KEY)!).state.theme).toBe(mode);+ selectedLabel = label;+ }});diff --git a/apps/mirai-gui/client/src/features/settings/components/general-tab.tsx b/apps/mirai-gui/client/src/features/settings/components/general-tab.tsxindex b772ad0bb..5d536d1e9 100644--- a/apps/mirai-gui/client/src/features/settings/components/general-tab.tsx+++ b/apps/mirai-gui/client/src/features/settings/components/general-tab.tsx@@ -1,29 +1,27 @@import GlobalInstructions from "@/features/chat-history/components/global-instructions";import { Toggle } from "@/components/ui/toggle";+import { SegmentedControl } from "@/components/ui/segmented-control";import { platformInfo } from "@/platform/platform-info";import { useAppStore } from "@/stores/use-app-store";import { useGlobalInstructionsStore } from "@/stores/use-global-instructions-store";import { useSettingsStore } from "@/stores/use-settings-store";import { useEffect } from "react";+import { AnalyticsSetting } from "./general/analytics-setting";import { AutoEjectSetting } from "./general/auto-eject-setting";import { CliInstallSetting } from "./general/cli-install-setting";-import { FeedbackFooter } from "./general/feedback-footer";-import { QuickEntryShortcutSetting } from "./general/quick-entry-shortcut-setting";+import { DataSettings } from "./general/data-settings";import { SettingDivider, SettingRow } from "./general/setting-row";+import { ThemeSelector } from "./general/theme-selector";export default function GeneralTab() {const {- enableThinking,- runOnStartup,- quickEntryShortcut,+ analyticsEnabled,+ modelChatNamingEnabled,autoEjectEnabled,autoEjectMinutes,fetch: fetchSettings,- fetchDesktopSettings,- setEnableThinking,- setRunOnStartup,- registerQuickEntryShortcut,- unregisterQuickEntryShortcut,+ setAnalyticsEnabled,+ setModelChatNamingEnabled,setAutoEjectEnabled,setAutoEjectMinutes,} = useSettingsStore();@@ -32,14 +30,15 @@ export default function GeneralTab() {const loadInstructions = useGlobalInstructionsStore((s) => s.loadInstructions);const saveInstructions = useGlobalInstructionsStore((s) => s.saveInstructions);- const isDarkMode = useAppStore((s) => s.isDarkMode);- const setDarkMode = useAppStore((s) => s.setDarkMode);+ const theme = useAppStore((s) => s.theme);+ const setTheme = useAppStore((s) => s.setTheme);+ const chatWidth = useAppStore((s) => s.chatWidth);+ const setChatWidth = useAppStore((s) => s.setChatWidth);useEffect(() => {loadInstructions();fetchSettings();- fetchDesktopSettings().catch(() => {});- }, [loadInstructions, fetchSettings, fetchDesktopSettings]);+ }, [loadInstructions, fetchSettings]);return (<div className="h-full flex flex-col">@@ -50,46 +49,42 @@ export default function GeneralTab() {<SettingDivider /><SettingRow- title="Dark mode"- description="Use the dark appearance across the app"- control={<Toggle label="Dark mode" checked={isDarkMode} onChange={() => setDarkMode(!isDarkMode)} />}+ title="Theme"+ description="Choose an appearance or follow your system settings"+ control={<ThemeSelector value={theme} onChange={setTheme} />}/>- {platformInfo.features.startupLaunch && (- <>- <SettingDivider />- <SettingRow- title="Run on startup"- description="Automatically start Mirai when you log in to your computer"- control={- <Toggle label="Run on startup" checked={runOnStartup} onChange={() => setRunOnStartup(!runOnStartup)} />- }- />- </>- )}-- {platformInfo.features.globalShortcut && (- <>- <SettingDivider />- <QuickEntryShortcutSetting- quickEntryShortcut={quickEntryShortcut}- registerQuickEntryShortcut={registerQuickEntryShortcut}- unregisterQuickEntryShortcut={unregisterQuickEntryShortcut}- />- </>- )}+ <SettingDivider />+ <SettingRow+ title="Chat width"+ description="Maximum width of messages, the message box, and chat history. Adapts to fit smaller windows."+ control={+ <div className="shrink-0">+ <SegmentedControl+ ariaLabel="Chat width"+ value={String(chatWidth)}+ onChange={(value) => setChatWidth(Number(value))}+ options={[+ { value: "800", label: "Narrow" },+ { value: "1000", label: "Medium" },+ { value: "1200", label: "Wide" },+ ]}+ />+ </div>+ }+ /><CliInstallSetting /><SettingDivider /><SettingRow- title="Default reasoning mode"- description="Let reasoning models think by default. You can override this for each model."+ title="Let models name chats via tool call"+ description="Enabled by default for supported models with 2B or more parameters. Otherwise, the same model is asked to name the chat separately."control={<Toggle- label="Default reasoning mode"- checked={enableThinking}- onChange={() => setEnableThinking(!enableThinking)}+ label="Let models name chats via tool call"+ checked={modelChatNamingEnabled}+ onChange={setModelChatNamingEnabled}/>}/>@@ -105,9 +100,12 @@ export default function GeneralTab() {/></>)}- </div>- <FeedbackFooter />+ <SettingDivider />+ <AnalyticsSetting enabled={analyticsEnabled} onChange={setAnalyticsEnabled} />++ <DataSettings />+ </div></div>);}diff --git a/apps/mirai-gui/client/src/features/settings/components/general/analytics-setting.tsx b/apps/mirai-gui/client/src/features/settings/components/general/analytics-setting.tsxnew file mode 100644index 000000000..183f88d55--- /dev/null+++ b/apps/mirai-gui/client/src/features/settings/components/general/analytics-setting.tsx@@ -0,0 +1,60 @@+import { Popover, PopoverButton, PopoverPanel } from "@headlessui/react";+import { Toggle } from "@/components/ui/toggle";+import { SettingRow } from "./setting-row";++export function AnalyticsSetting({ enabled, onChange }: { enabled: boolean; onChange: (enabled: boolean) => void }) {+ return (+ <SettingRow+ title="Share usage analytics"+ description={+ <>+ Send model activity, performance measurements, OS/CPU/RAM, app versions, and a temporary session ID to Mirai.{" "}+ <Popover as="span">+ <PopoverButton+ type="button"+ className="rounded-sm underline underline-offset-2 decoration-label-muted/50 hover:text-label-title transition-colors outline-hidden data-[focus]:shadow-focus"+ >+ See more+ </PopoverButton>+ <PopoverPanel+ anchor="bottom end"+ focus+ transition+ role="region"+ aria-label="Usage analytics details"+ className="z-50 w-[min(28rem,calc(100vw-2rem))] rounded-lg bg-surface-elevated shadow-sm text-[13px] leading-[150%] text-label-muted outline-hidden [--anchor-gap:8px] [--anchor-padding:16px] [--anchor-max-height:min(70vh,36rem)] origin-top-right transition-[opacity,transform] duration-[120ms] ease-spring data-[closed]:opacity-0 data-[closed]:scale-95"+ >+ <div+ tabIndex={0}+ className="max-h-[inherit] overflow-y-auto overscroll-y-contain thin-scrollbar rounded-lg p-4 outline-hidden"+ >+ <ul className="list-disc space-y-2 pl-4">+ <li>OS name and version, CPU model, total RAM, and app, uzu engine, and model toolchain versions.</li>+ <li>+ Model identifiers, download and generation start/finish events, UTC timestamps, and a generic+ generation-failed event.+ </li>+ <li>+ Reply duration, time to first token, input-processing and generation speeds,+ input/cached-input/output token counts, memory used, tokens per decoding pass, and decoding pass+ count, when available.+ </li>+ <li>+ Input/output energy usage and joules per token, including CPU, GPU, Neural Engine, and RAM+ breakdowns when available.+ </li>+ <li>A random session ID, renewed on app restart or when analytics is re-enabled.</li>+ </ul>+ <p className="mt-3">+ Chat content, attachments, chat names, local paths, and detailed error messages are not sent. Mirai’s+ server also sees your connection’s IP address.+ </p>+ </div>+ </PopoverPanel>+ </Popover>+ </>+ }+ control={<Toggle label="Share usage analytics" checked={enabled} onChange={onChange} />}+ />+ );+}diff --git a/apps/mirai-gui/client/src/features/settings/components/general/data-settings.tsx b/apps/mirai-gui/client/src/features/settings/components/general/data-settings.tsxnew file mode 100644index 000000000..a21bad6a2--- /dev/null+++ b/apps/mirai-gui/client/src/features/settings/components/general/data-settings.tsx@@ -0,0 +1,94 @@+import { Download } from "lucide-react";+import { useState } from "react";+import { Button } from "@/components/ui/button";+import { useToast } from "@/components/ui/toast/use-toast";+import { exportAllChatsZip } from "@/features/chat/services/export-chats";+import { platformInfo } from "@/platform/platform-info";+import { useSettingsStore } from "@/stores/use-settings-store";+import { ClearDataDialog } from "../clear-data-dialog";+import { SettingDivider, SettingRow } from "./setting-row";++export function DataSettings() {+ const [isExporting, setIsExporting] = useState(false);+ const [isExportingLogs, setIsExportingLogs] = useState(false);+ const [clearDataOpen, setClearDataOpen] = useState(false);+ const toast = useToast();+ const { exportLogs } = useSettingsStore();++ const handleExportChats = async () => {+ if (isExporting) return;+ setIsExporting(true);+ try {+ const ok = await exportAllChatsZip();+ if (ok) toast.success("Exported chats to ZIP");+ else toast.info("Nothing to export");+ } catch {+ toast.error("Export failed");+ } finally {+ setIsExporting(false);+ }+ };++ const handleExportLogs = async () => {+ if (isExportingLogs) return;+ setIsExportingLogs(true);+ const result = await exportLogs();+ setIsExportingLogs(false);+ if (result === "ok") toast.success("Exported logs");+ else if (result === "error") toast.error("Failed to export logs");+ };++ return (+ <>+ <SettingDivider />+ <SettingRow+ title="Export all your chats"+ description="As Markdown files in a ZIP archive"+ control={+ <Button+ kind="primary"+ size="sm"+ loading={isExporting}+ icon={<Download size={16} />}+ onClick={handleExportChats}+ disabled={isExporting}+ >+ Export+ </Button>+ }+ />+ {platformInfo.features.logExport && (+ <>+ <SettingDivider />+ <SettingRow+ title="Export logs"+ description="Save the current Mirai log file for debugging"+ control={+ <Button+ kind="primary"+ size="sm"+ loading={isExportingLogs}+ icon={<Download size={16} />}+ onClick={handleExportLogs}+ disabled={isExportingLogs}+ >+ Export+ </Button>+ }+ />+ </>+ )}+ <SettingDivider />+ <SettingRow+ title="Clear data"+ description="Delete chats, models, or logs"+ control={+ <Button kind="secondary" size="sm" onClick={() => setClearDataOpen(true)}>+ Clear data+ </Button>+ }+ />+ <ClearDataDialog open={clearDataOpen} onClose={() => setClearDataOpen(false)} />+ </>+ );+}diff --git a/apps/mirai-gui/client/src/features/settings/components/general/feedback-footer.tsx b/apps/mirai-gui/client/src/features/settings/components/general/feedback-footer.tsxdeleted file mode 100644index 36042711b..000000000--- a/apps/mirai-gui/client/src/features/settings/components/general/feedback-footer.tsx+++ /dev/null@@ -1,20 +0,0 @@-import { Button } from "@/components/ui/button";-import { Heart } from "lucide-react";-import { SettingDivider } from "./setting-row";--export function FeedbackFooter() {- return (- <div className="mt-auto sticky bottom-0 z-10">- <SettingDivider />- <div className="bg-background/80 backdrop-blur flex items-center justify-between px-5 py-3 lg:max-w-[800px] mx-auto w-full gap-3">- <div className="flex items-center gap-2 text-[13px] text-label-muted max-w-[210px] lg:max-w-none">- <Heart className="min-w-4 min-h-4 text-blue" />- <span>Let us know your feedback or request a new feature</span>- </div>- <Button kind="primary" size="xs" href="https://discord.gg/trymirai" target="_blank">- Give Feedback- </Button>- </div>- </div>- );-}diff --git a/apps/mirai-gui/client/src/features/settings/components/general/quick-entry-shortcut-setting.tsx b/apps/mirai-gui/client/src/features/settings/components/general/quick-entry-shortcut-setting.tsxdeleted file mode 100644index dc5e2fa22..000000000--- a/apps/mirai-gui/client/src/features/settings/components/general/quick-entry-shortcut-setting.tsx+++ /dev/null@@ -1,57 +0,0 @@-import { Button } from "@/components/ui/button";-import { X as XIcon } from "lucide-react";-import { SettingRow } from "./setting-row";-import { useShortcutCapture } from "./use-shortcut-capture";--export function QuickEntryShortcutSetting({- quickEntryShortcut,- registerQuickEntryShortcut,- unregisterQuickEntryShortcut,-}: {- quickEntryShortcut: string | null;- registerQuickEntryShortcut: (accelerator: string) => Promise<boolean>;- unregisterQuickEntryShortcut: () => Promise<void>;-}) {- const { isCapturing, setIsCapturing, buttonRef, shortcutDisplay } = useShortcutCapture(- quickEntryShortcut,- registerQuickEntryShortcut,- );- const capturingRing = isCapturing ? "ring-2 ring-blue ring-offset-2 ring-offset-background" : "";-- return (- <SettingRow- title="Quick entry keyboard shortcut"- description={isCapturing ? "Hold ⌘, ⌃ or ⌥, then a key" : "Open Mirai from anywhere with a shortcut"}- control={- quickEntryShortcut ? (- <Button- kind="primary"- size="xs"- icon={<XIcon width={16} height={16} />}- iconPosition="right"- className={capturingRing}- ref={buttonRef}- onClick={async () => {- await unregisterQuickEntryShortcut();- setIsCapturing(true);- }}- >- {shortcutDisplay}- </Button>- ) : (- <Button- kind="primary"- size="xs"- className={capturingRing}- ref={buttonRef}- onClick={() => {- setIsCapturing(true);- }}- >- Set shortcut- </Button>- )- }- />- );-}diff --git a/apps/mirai-gui/client/src/features/settings/components/general/setting-row.tsx b/apps/mirai-gui/client/src/features/settings/components/general/setting-row.tsxindex d97a016e2..aef9ca95f 100644--- a/apps/mirai-gui/client/src/features/settings/components/general/setting-row.tsx+++ b/apps/mirai-gui/client/src/features/settings/components/general/setting-row.tsx@@ -19,7 +19,7 @@ export function SettingRow({<div className="px-5 lg:max-w-[800px] mx-auto w-full"><div className="flex items-center justify-between gap-3"><div className="flex flex-col gap-1">- <h4 className="text-[15px] font-[350] leading-[150%] text-label-title overflow-hidden">{title}</h4>+ <h4 className="text-[15px] font-[350] leading-[150%] text-label-title overflow-clip">{title}</h4><p className="text-[13px] text-label-muted">{description}</p></div>{control}diff --git a/apps/mirai-gui/client/src/features/settings/components/general/theme-selector.tsx b/apps/mirai-gui/client/src/features/settings/components/general/theme-selector.tsxnew file mode 100644index 000000000..09fc6824f--- /dev/null+++ b/apps/mirai-gui/client/src/features/settings/components/general/theme-selector.tsx@@ -0,0 +1,65 @@+import { Listbox, ListboxButton, ListboxOption, ListboxOptions, Transition } from "@headlessui/react";+import { Check, ChevronDown } from "lucide-react";+import { Fragment } from "react";+import { SelectionItem } from "@/components/ui/select/selection-item";+import { SelectionPanel } from "@/components/ui/select/selection-panel";+import { SelectionSection } from "@/components/ui/select/selection-section";+import type { ThemeMode } from "@/stores/use-app-store";++const THEMES: { value: ThemeMode; label: string }[] = [+ { value: "system", label: "System" },+ { value: "light", label: "Light" },+ { value: "dark", label: "Dark" },+];++type Props = { value: ThemeMode; onChange: (theme: ThemeMode) => void };++export const ThemeSelector = ({ value, onChange }: Props) => {+ const label = THEMES.find((theme) => theme.value === value)?.label ?? "System";+ return (+ <Listbox value={value} onChange={onChange} as="div" className="relative shrink-0">+ <ListboxButton+ type="button"+ aria-label={`Theme: ${label}`}+ className="flex h-8 min-w-[110px] items-center justify-between gap-3 rounded-md bg-surface-tertiary px-2.5 text-[13px] font-[450] leading-[1.3] text-text-muted outline-hidden hover:bg-control-surface-hover hover:text-text-primary data-[headlessui-state~=open]:bg-control-surface-active data-[headlessui-state~=open]:text-text-primary data-[focus]:bg-control-surface-hover data-[focus]:text-text-primary data-[focus]:shadow-focus"+ >+ <span>{label}</span>+ <ChevronDown size={16} aria-hidden="true" />+ </ListboxButton>+ <Transition+ as={Fragment}+ enter="transition-[opacity,transform] duration-[120ms] ease-spring"+ enterFrom="transform opacity-0 scale-90"+ enterTo="transform opacity-100 scale-100"+ leave="transition-[opacity,transform] duration-[80ms] ease-spring"+ leaveFrom="transform opacity-100 scale-100"+ leaveTo="transform opacity-0 scale-90"+ >+ <div className="absolute right-0 top-full z-50 origin-top-right pt-1">+ <ListboxOptions className="outline-hidden">+ <SelectionPanel className="min-w-[160px] origin-top-right py-1.5">+ <SelectionSection>+ {THEMES.map((theme) => (+ <ListboxOption key={theme.value} value={theme.value} as={Fragment}>+ {({ focus, selected }) => (+ <SelectionItem+ as="button"+ type="button"+ active={focus}+ selected={selected}+ className="flex w-full items-center justify-between gap-4 border-none bg-transparent text-left text-[13px] font-[450] leading-[1.3] text-text-primary outline-hidden"+ >+ {theme.label}+ {selected && <Check size={14} aria-hidden="true" />}+ </SelectionItem>+ )}+ </ListboxOption>+ ))}+ </SelectionSection>+ </SelectionPanel>+ </ListboxOptions>+ </div>+ </Transition>+ </Listbox>+ );+};diff --git a/apps/mirai-gui/client/src/features/settings/components/general/use-shortcut-capture.ts b/apps/mirai-gui/client/src/features/settings/components/general/use-shortcut-capture.tsdeleted file mode 100644index 62c346cc6..000000000--- a/apps/mirai-gui/client/src/features/settings/components/general/use-shortcut-capture.ts+++ /dev/null@@ -1,132 +0,0 @@-import { useEffect, useRef, useState } from "react";-import { useToast } from "@/components/ui/toast/use-toast";--const DISPLAY_KEYS: Record<string, string> = {- Meta: "⌘",- Control: "⌃",- Alt: "⌥",- Option: "⌥",- Shift: "⇧",- " ": "Space",-};--const CODE_KEYS: Record<string, string> = {- Minus: "-",- Equal: "=",- BracketLeft: "[",- BracketRight: "]",- Semicolon: ";",- Quote: "'",- Backquote: "`",- Backslash: "\\",- Comma: ",",- Period: ".",- Slash: "/",- Space: "Space",-};--function formatKeys(keys: string[]): string {- return keys- .map((k) => DISPLAY_KEYS[k] || k)- .join(" ")- .replace(" Space", " Space");-}--function acceleratorFrom(event: KeyboardEvent): string | null {- const modifiers: string[] = [];- if (event.metaKey) modifiers.push("Meta");- if (event.ctrlKey) modifiers.push("Control");- if (event.altKey) modifiers.push("Alt");- if (event.shiftKey) modifiers.push("Shift");-- if (["Meta", "Control", "Alt", "Shift"].includes(event.key)) return null;-- const mappedCode = CODE_KEYS[event.code];- let primary: string | null = null;- if (event.code.startsWith("Key")) {- primary = event.code.slice(3).toUpperCase();- } else if (event.code.startsWith("Digit")) {- primary = event.code.slice(5);- } else if (mappedCode) {- primary = mappedCode;- } else if (/^F\d{1,2}$/.test(event.key)) {- primary = event.key;- } else if (event.key === " ") {- primary = "Space";- }- if (!primary) return null;-- // Shift alone won't fire a global hotkey on macOS; keep capturing until- // a system modifier is added rather than store a dead shortcut.- if (!modifiers.some((m) => m === "Meta" || m === "Control" || m === "Alt")) return null;-- return [...modifiers]- .map((p) => (p === "Meta" ? "CommandOrControl" : p))- .concat(primary)- .join("+");-}--export function useShortcutCapture(- quickEntryShortcut: string | null,- registerQuickEntryShortcut: (accelerator: string) => Promise<boolean>,-) {- const toast = useToast();- const [isCapturing, setIsCapturing] = useState(false);- const buttonRef = useRef<HTMLButtonElement | null>(null);-- useEffect(() => {- if (!isCapturing) return;-- function onKeyDown(e: KeyboardEvent) {- e.preventDefault();- if (e.key === "Escape") {- setIsCapturing(false);- return;- }- const accelerator = acceleratorFrom(e);- if (!accelerator) return;- void registerQuickEntryShortcut(accelerator).then((ok) => {- setIsCapturing(false);- if (!ok) toast.error("This shortcut can't be registered", { id: "shortcut-register-failed" });- });- }-- window.addEventListener("keydown", onKeyDown, { capture: true });- return () => window.removeEventListener("keydown", onKeyDown, true);- }, [isCapturing, registerQuickEntryShortcut, toast]);-- useEffect(() => {- if (!isCapturing) return;-- const handlePossibleOutside = (event: Event) => {- const buttonEl = buttonRef.current;- if (!buttonEl) {- setIsCapturing(false);- return;- }- if (!(event.target instanceof Node) || !buttonEl.contains(event.target)) {- setIsCapturing(false);- }- };-- const handleWindowBlur = () => setIsCapturing(false);-- document.addEventListener("mousedown", handlePossibleOutside);- document.addEventListener("touchstart", handlePossibleOutside);- document.addEventListener("contextmenu", handlePossibleOutside);- window.addEventListener("blur", handleWindowBlur);-- return () => {- document.removeEventListener("mousedown", handlePossibleOutside);- document.removeEventListener("touchstart", handlePossibleOutside);- document.removeEventListener("contextmenu", handlePossibleOutside);- window.removeEventListener("blur", handleWindowBlur);- };- }, [isCapturing]);-- const shortcutDisplay = quickEntryShortcut- ? formatKeys(quickEntryShortcut.split("+").map((p) => (p === "CommandOrControl" ? "Meta" : p)))- : "";-- return { isCapturing, setIsCapturing, buttonRef, shortcutDisplay };-}diff --git a/apps/mirai-gui/client/src/features/settings/components/privacy-tab.tsx b/apps/mirai-gui/client/src/features/settings/components/privacy-tab.tsxdeleted file mode 100644index 39030ce08..000000000--- a/apps/mirai-gui/client/src/features/settings/components/privacy-tab.tsx+++ /dev/null@@ -1,150 +0,0 @@-import { ChevronRight, Download, Shield } from "lucide-react";-import { Button } from "@/components/ui/button";-import { useToast } from "@/components/ui/toast/use-toast";-import { useEffect, useState } from "react";-import { ClearDataDialog } from "./clear-data-dialog";-import { FeedbackFooter } from "./general/feedback-footer";-import { SettingDivider, SettingRow } from "./general/setting-row";-import { platformInfo } from "@/platform/platform-info";-import { useSettingsStore } from "@/stores/use-settings-store";-import { exportAllChatsZip } from "@/features/chat/services/export-chats";--const links = [- {- title: "Terms of Service",- url: "https://artifacts.trymirai.com/legal/Mirai_Tech_Terms_of_Use.pdf",- },- {- title: "Privacy Policy",- url: "https://artifacts.trymirai.com/legal/Mirai_Tech_Privacy_Policy.pdf",- },-];--export default function PrivacyTab() {- const [isExporting, setIsExporting] = useState(false);- const [isExportingLogs, setIsExportingLogs] = useState(false);- const [clearDataOpen, setClearDataOpen] = useState(false);- const toast = useToast();- const { fetch: fetchSettings, exportLogs } = useSettingsStore();-- useEffect(() => {- fetchSettings();- }, [fetchSettings]);-- const handleExportChats = async () => {- if (isExporting) return;- setIsExporting(true);- try {- const ok = await exportAllChatsZip();- if (ok) toast.success("Exported chats to ZIP");- else toast.info("Nothing to export");- } catch {- toast.error("Export failed");- } finally {- setIsExporting(false);- }- };-- const handleExportLogs = async () => {- if (isExportingLogs) return;- setIsExportingLogs(true);- const result = await exportLogs();- setIsExportingLogs(false);- if (result === "ok") toast.success("Exported logs");- else if (result === "error") toast.error("Failed to export logs");- };-- return (- <div className="h-full flex flex-col">- <div className="flex flex-col">- <div className="flex flex-col gap-4 px-5 lg:max-w-[800px] mx-auto w-full">- <Shield className="w-6 h-6 text-blue" />- <div className="flex flex-col gap-1">- <h3 className="text-[18px] font-medium leading-[130%] tracking-[0.2px] text-label-title break-words">- All data is processed and stored locally on your device.- </h3>- <p className="text-[13px] font-[350] leading-[150%] text-label-muted">- Your privacy is built into the core of how Mirai runs.- </p>- </div>- </div>- <div className="mt-5">- <SettingDivider />- </div>-- <div className="flex flex-col">- {links.map((link, index) => (- <div key={link.url} className="w-full">- <a- href={link.url}- target="_blank"- rel="noopener noreferrer"- className="lg:max-w-[800px] mx-auto w-full hover:bg-bg-sub flex items-center justify-between px-5 py-5"- >- <h4 className="text-[15px] font-[350] leading-[150%] text-label-title">{link.title}</h4>-- <ChevronRight className="w-[14px] h-[14px] text-label-muted" />- </a>- {index < links.length - 1 && <SettingDivider />}- </div>- ))}- </div>-- <div className="flex flex-col gap-5">- <SettingDivider />- <SettingRow- title="Export all your chats"- description="As Markdown files in .zip archive"- control={- <Button- kind="primary"- size="sm"- loading={isExporting}- icon={<Download size={16} />}- onClick={handleExportChats}- disabled={isExporting}- >- Export- </Button>- }- />- {platformInfo.features.logExport && (- <>- <SettingDivider />- <SettingRow- title="Export logs"- description="Save the current Mirai log file for debugging"- control={- <Button- kind="primary"- size="sm"- loading={isExportingLogs}- icon={<Download size={16} />}- onClick={handleExportLogs}- disabled={isExportingLogs}- >- Export- </Button>- }- />- </>- )}- <SettingDivider />- <SettingRow- title="Clear data"- description="Delete dialogs, models, or logs"- control={- <Button kind="secondary" size="sm" onClick={() => setClearDataOpen(true)}>- Clear data- </Button>- }- />- <SettingDivider />- </div>- </div>- <ClearDataDialog open={clearDataOpen} onClose={() => setClearDataOpen(false)} />-- <FeedbackFooter />- </div>- );-}diff --git a/apps/mirai-gui/client/src/features/settings/components/settings-page.tsx b/apps/mirai-gui/client/src/features/settings/components/settings-page.tsxindex d6893b39a..f567e07b8 100644--- a/apps/mirai-gui/client/src/features/settings/components/settings-page.tsx+++ b/apps/mirai-gui/client/src/features/settings/components/settings-page.tsx@@ -1,46 +1,34 @@import { Link, useSearch } from "@tanstack/react-router";import GeneralTab from "./general-tab";-import PrivacyTab from "./privacy-tab";import AboutTab from "./about-tab";-import { useSidebarStore } from "@/stores/use-sidebar-store";+import { PageHeader } from "@/components/page-header";import { Button } from "@/components/ui/button";import { twMerge } from "tailwind-merge";-export type SettingsTab = "general" | "privacy" | "about";+export type SettingsTab = "general" | "about";export function SettingsPage() {const { tab } = useSearch({ from: "/settings" });const sections: { key: SettingsTab; label: string }[] = [{ key: "general", label: "General" },- { key: "privacy", label: "Privacy" },- { key: "about", label: "About Mirai" },+ { key: "about", label: "About" },];const tabComponents: Record<SettingsTab, React.ReactNode> = {general: <GeneralTab />,- privacy: <PrivacyTab />,about: <AboutTab />,};- const isSidebarOpen = useSidebarStore((s) => s.isOpen);-return (- <div className="h-[calc(100vh-24px)] w-full bg-background text-label-title pt-4">- <div className="h-full flex flex-col">- <div className={twMerge("px-5 text-center lg:text-left sticky top-0 z-10 bg-background")}>- <h1- className={twMerge(- "text-[15px] leading-[150%] font-medium mb-4 transition-all duration-300 ease-in-out",- !isSidebarOpen && "text-center",- )}- >- Settings- </h1>+ <div className="h-full min-h-0 w-full bg-background text-label-title pt-4">+ <div className="h-full min-h-0 flex flex-col">+ <div className="px-5 shrink-0 z-10 bg-background">+ <PageHeader title={<h1 className="text-[15px] leading-[150%] font-medium mb-4">Settings</h1>} /></div>- <div className="h-[1px] bg-cell-border" />+ <div className="h-[1px] shrink-0 bg-cell-border" />- <div className="lg:hidden px-5 py-4 sticky top-[52px] z-10 bg-background">- <div className="flex gap-2 overflow-x-auto thin-scrollbar">+ <div className="lg:hidden px-5 py-4 shrink-0 z-10 bg-background">+ <div className="flex gap-2 overflow-x-auto overscroll-x-contain overscroll-y-auto thin-scrollbar">{sections.map((s) => (<Link key={s.key} to="/settings" search={{ tab: s.key }} className="flex-shrink-0"><Button kind={s.key === tab ? "primary" : "ghost"} size="sm" className="whitespace-nowrap">@@ -52,7 +40,7 @@ export function SettingsPage() {</div><div className="flex flex-1 min-h-0">- <aside className="hidden lg:block w-[152px] flex-shrink-0 h-full pt-2 overflow-auto thin-scrollbar border-r border-cell-border">+ <aside className="hidden lg:block w-[152px] flex-shrink-0 h-full pt-2 overflow-y-auto thin-scrollbar border-r border-cell-border"><nav className="flex flex-col gap-2">{sections.map((s) => (<Link key={s.key} to="/settings" search={{ tab: s.key }} className="flex px-3">@@ -68,7 +56,7 @@ export function SettingsPage() {<sectionclassName={twMerge(- "flex-1 lg:px-0 h-full overflow-auto thin-scrollbar [scrollbar-gutter:stable]",+ "flex-1 min-w-0 lg:px-0 h-full overflow-y-auto overscroll-y-contain thin-scrollbar [scrollbar-gutter:stable]","pt-5 lg:pt-5",)}>diff --git a/apps/mirai-gui/client/src/hooks/use-app-initialization.ts b/apps/mirai-gui/client/src/hooks/use-app-initialization.tsindex a198b2f93..cd112b563 100644--- a/apps/mirai-gui/client/src/hooks/use-app-initialization.ts+++ b/apps/mirai-gui/client/src/hooks/use-app-initialization.ts@@ -1,5 +1,4 @@import { useAppStore } from "@/stores/use-app-store";-import { getPlatform } from "@/platform/platform-singleton";import { platformInfo } from "@/platform/platform-info";import { useModelParamsStore } from "@/stores/use-model-params-store";import { useModelsStore } from "@/stores/use-models-store";@@ -8,8 +7,10 @@ import { useEffect } from "react";import { useSessionWiring } from "./session-wiring";import { useDownloadWiring } from "./use-download-wiring";import { useUpdateInitialization } from "./use-update-initialization";+import { useAppTheme } from "./use-app-theme";export const useAppInitialization = (enabled: boolean = true) => {+ useAppTheme();useSessionWiring(enabled);useDownloadWiring(enabled);useUpdateInitialization(enabled && platformInfo.features.autoUpdate);@@ -21,9 +22,6 @@ export const useAppInitialization = (enabled: boolean = true) => {void useModelsStore.getState().fetchModels();if (platformInfo.isTauri) {void useAppStore.getState().fetchVersion();- void getPlatform()- .systemUi.setWindowTheme(useAppStore.getState().isDarkMode)- .catch(() => {});}}, [enabled]);};diff --git a/apps/mirai-gui/client/src/hooks/use-app-theme.test.tsx b/apps/mirai-gui/client/src/hooks/use-app-theme.test.tsxnew file mode 100644index 000000000..ee6423daf--- /dev/null+++ b/apps/mirai-gui/client/src/hooks/use-app-theme.test.tsx@@ -0,0 +1,90 @@+import { act, cleanup, renderHook } from "@testing-library/react";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { useAppStore } from "@/stores/use-app-store";+import { useAppTheme } from "./use-app-theme";+import { useAppInitialization } from "./use-app-initialization";++const setWindowTheme = vi.hoisted(() => vi.fn(async () => true));+vi.mock("@/platform/platform-singleton", () => ({ getPlatform: () => ({ systemUi: { setWindowTheme } }) }));+vi.mock("./session-wiring", () => ({ useSessionWiring: vi.fn() }));+vi.mock("./use-download-wiring", () => ({ useDownloadWiring: vi.fn() }));+vi.mock("./use-update-initialization", () => ({ useUpdateInitialization: vi.fn() }));+const defaults = useAppStore.getState();+let systemDark = false;+let listeners: Set<(event: { matches: boolean }) => void>;++beforeEach(() => {+ vi.clearAllMocks();+ systemDark = false;+ listeners = new Set();+ vi.stubGlobal(+ "matchMedia",+ vi.fn(() => ({+ get matches() {+ return systemDark;+ },+ addEventListener: (_name: string, listener: (event: { matches: boolean }) => void) => listeners.add(listener),+ removeEventListener: (_name: string, listener: (event: { matches: boolean }) => void) =>+ listeners.delete(listener),+ })),+ );+ useAppStore.setState({ ...defaults, theme: "system", isDarkMode: false }, true);+ document.documentElement.classList.remove("dark");+});+afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+ document.documentElement.classList.remove("dark");+});++const changeSystemTheme = (dark: boolean) =>+ act(() => {+ systemDark = dark;+ listeners.forEach((listener) => listener({ matches: dark }));+ });++it("follows live OS changes by default and leaves the native window in System mode", () => {+ const hook = renderHook(useAppTheme);+ expect(useAppStore.getState().theme).toBe("system");+ expect(document.documentElement.classList.contains("dark")).toBe(false);+ expect(setWindowTheme).toHaveBeenCalledWith("system");++ changeSystemTheme(true);+ expect(document.documentElement.classList.contains("dark")).toBe(true);+ expect(useAppStore.getState().isDarkMode).toBe(true);+ changeSystemTheme(false);+ expect(document.documentElement.classList.contains("dark")).toBe(false);+ expect(useAppStore.getState().isDarkMode).toBe(false);+ expect(setWindowTheme).toHaveBeenCalledOnce();+ hook.unmount();+ expect(listeners.size).toBe(0);+});++it("keeps explicit Light and Dark modes through OS changes, then resumes System mode", () => {+ renderHook(useAppTheme);+ act(() => useAppStore.getState().setTheme("dark"));+ expect(document.documentElement.classList.contains("dark")).toBe(true);+ expect(setWindowTheme).toHaveBeenLastCalledWith("dark");+ changeSystemTheme(true);+ changeSystemTheme(false);+ expect(useAppStore.getState().isDarkMode).toBe(true);++ act(() => useAppStore.getState().setTheme("light"));+ changeSystemTheme(true);+ expect(document.documentElement.classList.contains("dark")).toBe(false);+ expect(useAppStore.getState().isDarkMode).toBe(false);+ expect(setWindowTheme).toHaveBeenLastCalledWith("light");++ act(() => useAppStore.getState().setTheme("system"));+ expect(document.documentElement.classList.contains("dark")).toBe(true);+ expect(useAppStore.getState().isDarkMode).toBe(true);+ expect(setWindowTheme).toHaveBeenLastCalledWith("system");+});++it("still follows the system on the welcome screen while app initialization is disabled", () => {+ renderHook(() => useAppInitialization(false));+ changeSystemTheme(true);+ expect(document.documentElement.classList.contains("dark")).toBe(true);+ expect(useAppStore.getState().isDarkMode).toBe(true);+ expect(setWindowTheme).toHaveBeenCalledWith("system");+});diff --git a/apps/mirai-gui/client/src/hooks/use-app-theme.ts b/apps/mirai-gui/client/src/hooks/use-app-theme.tsnew file mode 100644index 000000000..3c9484074--- /dev/null+++ b/apps/mirai-gui/client/src/hooks/use-app-theme.ts@@ -0,0 +1,21 @@+import { getPlatform } from "@/platform/platform-singleton";+import { isDarkTheme, useAppStore } from "@/stores/use-app-store";+import { useEffect, useLayoutEffect } from "react";+import { useMediaQuery } from "./use-media-query";++export const useAppTheme = () => {+ const theme = useAppStore((state) => state.theme);+ const systemDark = useMediaQuery("(prefers-color-scheme: dark)");++ useLayoutEffect(() => {+ const isDarkMode = isDarkTheme(theme, systemDark);+ document.documentElement.classList.toggle("dark", isDarkMode);+ if (useAppStore.getState().isDarkMode !== isDarkMode) useAppStore.setState({ isDarkMode });+ }, [theme, systemDark]);++ useEffect(() => {+ void getPlatform()+ .systemUi.setWindowTheme(theme)+ .catch(() => {});+ }, [theme]);+};diff --git a/apps/mirai-gui/client/src/hooks/use-download-wiring.ts b/apps/mirai-gui/client/src/hooks/use-download-wiring.tsindex e8e95840b..bbc2f05e6 100644--- a/apps/mirai-gui/client/src/hooks/use-download-wiring.ts+++ b/apps/mirai-gui/client/src/hooks/use-download-wiring.ts@@ -5,6 +5,12 @@ import { useEffect } from "react";export const useDownloadWiring = (enabled: boolean = true) => {useEffect(() => {if (!enabled) return;- return getPlatform().models.onDownloadEvent((event) => useModelsStore.getState().applyDownloadEvent(event));+ const models = getPlatform().models;+ const stopDownloads = models.onDownloadEvent((event) => useModelsStore.getState().applyDownloadEvent(event));+ const stopCatalog = models.onModelsChanged(() => void useModelsStore.getState().fetchModels());+ return () => {+ stopDownloads();+ stopCatalog();+ };}, [enabled]);};diff --git a/apps/mirai-gui/client/src/hooks/use-save-failure-toast.ts b/apps/mirai-gui/client/src/hooks/use-save-failure-toast.tsindex 3acdaf83f..3ecc2ead3 100644--- a/apps/mirai-gui/client/src/hooks/use-save-failure-toast.ts+++ b/apps/mirai-gui/client/src/hooks/use-save-failure-toast.ts@@ -10,6 +10,6 @@ export const useSaveFailureToast = (): void => {useEffect(() => {if (saveFailureCount === seenRef.current) return;seenRef.current = saveFailureCount;- toast.error("Failed to save the message. It may be missing after a restart.", { id: "chat-save-failed" });+ toast.error("Failed to save chat changes. They may be missing after a restart.", { id: "chat-save-failed" });}, [saveFailureCount, toast]);};diff --git a/apps/mirai-gui/client/src/index.css b/apps/mirai-gui/client/src/index.cssindex b9d1fb8bd..031193e81 100644--- a/apps/mirai-gui/client/src/index.css+++ b/apps/mirai-gui/client/src/index.css@@ -27,6 +27,13 @@@source "../../node_modules/streamdown/dist";@custom-variant dark (&:where(.dark, .dark *));+/* Ellipsis should clip labels, not create hidden scroll containers that swallow+ wheel gestures before they reach the surrounding list. */+@utility truncate {+ min-width: 0;+ overflow: clip;+}+@utility thin-scrollbar {&::-webkit-scrollbar {width: 6px;@@ -42,6 +49,12 @@}@layer base {+ /* App chrome must stay still. Content scrollers opt into overscroll on their+ scrolling axis; independent panes use contain to stop scroll chaining. */+ * {+ overscroll-behavior: none;+ }+/* Tailwind 4 defaults borders to currentColor, buttons to cursor: default andplaceholders to a translucent currentColor; the app was designed on theTailwind 3 defaults, kept here. */@@ -77,6 +90,7 @@body,#root {height: 100%;+ overflow: clip;}body {@@ -117,7 +131,7 @@}[data-streamdown="code-block"] {- @apply w-full overflow-hidden border border-cell-border bg-bg-modal;+ @apply w-full border border-cell-border bg-bg-modal;/* The 200px placeholder flashes a scrollbar as the block scrolls into view. */content-visibility: visible !important;}@@ -139,6 +153,8 @@[data-streamdown="code-block-body"] {@apply p-4;+ /* Horizontal code scrolling stays local; vertical gestures scroll the chat. */+ overscroll-behavior: contain auto;background: transparent !important;border: 0 !important;font-size: 12px !important;@@ -176,6 +192,14 @@}}+@layer utilities {+ /* Streamdown supplies overflow-hidden on this rounded wrapper. Its body is+ the scroll owner; the wrapper only clips the corners. */+ [data-streamdown="code-block"] {+ overflow: clip;+ }+}+@layer components {.scrollbar-hide {scrollbar-width: none;diff --git a/apps/mirai-gui/client/src/index.tsx b/apps/mirai-gui/client/src/index.tsxindex d4a739a98..8af534c3d 100644--- a/apps/mirai-gui/client/src/index.tsx+++ b/apps/mirai-gui/client/src/index.tsx@@ -10,27 +10,15 @@ import { initPlatform } from "./platform/platform-singleton";import { TauriPlatformClient } from "./platform/tauri-platform-client";import { WebPlatformClient } from "./platform/web-platform-client";import { platformInfo } from "./platform/platform-info";-import { APP_STORE_KEY } from "./stores/migrate-app-storage";+import { useAppStore } from "./stores/use-app-store";RuntimeLoader.setWasmUrl(riveWasmUrl);-type PersistedAppState = { isDarkMode?: unknown; skipWelcome?: unknown };--const readPersistedAppState = (): PersistedAppState => {- try {- const stored = localStorage.getItem(APP_STORE_KEY);- return stored ? (JSON.parse(stored)?.state ?? {}) : {};- } catch {- return {};- }-};--const persistedAppState = readPersistedAppState();--document.documentElement.classList.toggle("dark", persistedAppState.isDarkMode !== false);+const appState = useAppStore.getState();+document.documentElement.classList.toggle("dark", appState.isDarkMode);const getInitialPath = (): string => {- if (!persistedAppState.skipWelcome) return "/welcome";+ if (!appState.skipWelcome) return "/welcome";return platformInfo.isTauri ? "/local-models" : "/chats";};diff --git a/apps/mirai-gui/client/src/platform/platform-info.ts b/apps/mirai-gui/client/src/platform/platform-info.tsindex e0435159d..5b406cfca 100644--- a/apps/mirai-gui/client/src/platform/platform-info.ts+++ b/apps/mirai-gui/client/src/platform/platform-info.ts@@ -6,8 +6,6 @@ export const platformInfo = {fileSystem: __PLATFORM__ === "tauri",logExport: __PLATFORM__ === "tauri",localModelDownloads: __PLATFORM__ === "tauri",- startupLaunch: __PLATFORM__ === "tauri",- globalShortcut: __PLATFORM__ === "tauri",autoEject: __PLATFORM__ === "tauri",cliInstall: __PLATFORM__ === "tauri",// Overlay title bar with in-content macOS traffic lights: UI must reservediff --git a/apps/mirai-gui/client/src/platform/services/chat/index.ts b/apps/mirai-gui/client/src/platform/services/chat/index.tsindex 5f1e25e8c..2d54acb1d 100644--- a/apps/mirai-gui/client/src/platform/services/chat/index.ts+++ b/apps/mirai-gui/client/src/platform/services/chat/index.ts@@ -6,13 +6,13 @@ export type TitleGenParams = {messages: Array<{ role: "system" | "user" | "assistant"; content: string }>;};-export type SamplingDefaults = Omit<Extract<SamplingPolicyPayload, { type: "Stochastic" }>, "type">;+export type SamplingDefaults = Exclude<SamplingPolicyPayload, { type: "Default" }>;export type ChatService = {runStream(params: LlmRunParams): LlmAsyncStream;cancelRun(runId: string): Promise<void>;generateTitle(params: TitleGenParams): Promise<string>;cancelTitleGen(): Promise<void>;- /** Model's own generation defaults; null until that model's session is loaded. */+ /** Model's actual sampling method and parameters, read without loading weights. */getSamplingDefaults(repoId: string): Promise<SamplingDefaults | null>;};diff --git a/apps/mirai-gui/client/src/platform/services/chat/run-stream.test.ts b/apps/mirai-gui/client/src/platform/services/chat/run-stream.test.tsindex 4780daf66..fc14e324e 100644--- a/apps/mirai-gui/client/src/platform/services/chat/run-stream.test.ts+++ b/apps/mirai-gui/client/src/platform/services/chat/run-stream.test.ts@@ -1,5 +1,5 @@import { describe, expect, it, vi } from "vitest";-import type { LlmRunParams, SessionOutputStats } from "@/types/llm-stream";+import type { LlmRunParams, SessionOutputStats, TranscriptItem } from "@/types/llm-stream";import { runLlmStream, type RunEvent, type RunTransport } from "./run-stream";const params: LlmRunParams = { repoId: "vendor/model", messages: [{ role: "user", content: "hi" }] };@@ -37,6 +37,130 @@ const readAll = async (stream: ReadableStream<string>): Promise<string> => {};describe("runLlmStream", () => {+ it("replays ordered transcript snapshots including explicit naming calls", async () => {+ const initial: TranscriptItem[] = [+ { type: "thinking", text: "Check the clock", completed: true },+ { type: "toolCall", name: "set_chat_name", called: true, failed: true },+ { type: "toolCall", name: "get_current_date_time", called: false },+ ];+ const { transport } = fakeTransport(async (_id, _params, emit) => {+ emit({ type: "transcript", items: initial });+ });+ const run = runLlmStream(transport, params);+ const listener = vi.fn();+ run.onTranscript(listener);+ expect(listener).toHaveBeenCalledExactlyOnceWith(initial);+ await run.cancel();+ expect((await run.result).transcript).toEqual(initial);+ });++ it("appends Unicode text and reasoning without changing previously published snapshots", async () => {+ const { transport, emit } = fakeTransport();+ const run = runLlmStream(transport, params);+ const initial: TranscriptItem[] = [+ { type: "thinking", text: "考" },+ { type: "toolCall", name: "get_current_date_time", called: true },+ { type: "text", text: "Hello" },+ ];+ const snapshots: TranscriptItem[][] = [];+ run.onTranscript((items) => snapshots.push(items));+ emit({ type: "transcript", items: initial });+ emit({ type: "transcriptDelta", index: 0, delta: "える 🧠" });+ emit({ type: "transcriptDelta", index: 2, delta: ", თბილისი 👋" });+ emit({ type: "done", text: "Hello, თბილისი 👋", stats });++ expect(snapshots).toEqual([+ initial,+ [{ type: "thinking", text: "考える 🧠" }, initial[1], initial[2]],+ [{ type: "thinking", text: "考える 🧠" }, initial[1], { type: "text", text: "Hello, თბილისი 👋" }],+ ]);+ expect(initial[0]).toEqual({ type: "thinking", text: "考" });+ expect(initial[2]).toEqual({ type: "text", text: "Hello" });+ expect(snapshots[1]?.[1]).toBe(initial[1]);+ expect(snapshots[2]?.[0]).toBe(snapshots[1]?.[0]);+ expect((await run.result).transcript).toBe(snapshots[2]);+ expect(await readAll(run.stream)).toBe("");+ });++ it("replays deltas received before subscription as one complete snapshot", async () => {+ const { transport } = fakeTransport(async (_id, _params, emit) => {+ emit({ type: "transcript", items: [{ type: "thinking", text: "Let me" }] });+ emit({ type: "transcriptDelta", index: 0, delta: " think" });+ });+ const run = runLlmStream(transport, params);+ const listener = vi.fn();+ run.onTranscript(listener);+ expect(listener).toHaveBeenCalledExactlyOnceWith([{ type: "thinking", text: "Let me think" }]);+ await run.cancel();+ });++ it("replaces delta state with structural and final snapshots", async () => {+ const { transport, emit } = fakeTransport();+ const run = runLlmStream(transport, params);+ const listener = vi.fn();+ run.onTranscript(listener);+ emit({ type: "transcript", items: [{ type: "thinking", text: "Draft" }] });+ emit({ type: "transcriptDelta", index: 0, delta: " reasoning" });+ emit({+ type: "transcript",+ items: [+ { type: "thinking", text: "Draft reasoning", completed: true },+ { type: "toolCall", name: "get_current_date_time", called: false },+ ],+ });+ emit({ type: "transcript", items: [{ type: "text", text: "Revised" }] });+ emit({ type: "transcriptDelta", index: 0, delta: " answer" });+ expect(listener).toHaveBeenLastCalledWith([{ type: "text", text: "Revised answer" }]);+ const final: TranscriptItem[] = [{ type: "text", text: "Final answer" }];+ emit({ type: "done", text: "Final answer", stats, transcript: final });+ expect(listener).toHaveBeenLastCalledWith(final);+ expect((await run.result).transcript).toEqual(final);+ });++ it.each([undefined, [{ type: "toolCall", name: "get_current_date_time", called: false }]] as const)(+ "reports an invalid delta target instead of silently losing text (%j)",+ async (initial) => {+ const { transport, emit } = fakeTransport();+ const run = runLlmStream(transport, params);+ if (initial) emit({ type: "transcript", items: [...initial] });+ emit({ type: "transcriptDelta", index: 0, delta: "Lost text" });+ await expect(readAll(run.stream)).rejects.toThrow("Invalid transcript delta at index 0");+ expect((await run.result).error).toBe("Invalid transcript delta at index 0");+ expect((await run.result).transcript).toEqual(initial);+ },+ );++ it.each(["done", "error", "cancel"] as const)(+ "preserves an incrementally assembled transcript through %s and ignores late updates",+ async (end) => {+ const { transport, emit } = fakeTransport();+ const run = runLlmStream(transport, params);+ const items: TranscriptItem[] = [+ { type: "text", text: "Checking." },+ { type: "toolCall", name: "get_current_date_time", called: true },+ { type: "text", text: "It is noon." },+ ];+ const listener = vi.fn();+ run.onTranscript(listener);+ emit({ type: "transcript", items: [...items.slice(0, -1), { type: "text", text: "It is" }] });+ emit({ type: "transcriptDelta", index: 2, delta: " noon." });+ expect(listener).toHaveBeenLastCalledWith(items);+ listener.mockClear();+ if (end === "done") emit({ type: "done", text: "Checking.\n\nIt is noon.", stats });+ else {+ if (end === "cancel") await run.cancel();+ else {+ emit({ type: "error", error: "Stopped" });+ await expect(readAll(run.stream)).rejects.toThrow("Stopped");+ }+ }+ emit({ type: "transcript", items: [] });+ emit({ type: "transcriptDelta", index: 2, delta: " Ignore this." });+ expect(listener).not.toHaveBeenCalled();+ expect((await run.result).transcript).toEqual(items);+ },+ );+it("streams chunks, closes on done and resolves the result", async () => {const { transport, emit } = fakeTransport();const run = runLlmStream(transport, params);@@ -122,4 +246,72 @@ describe("runLlmStream", () => {expect(await run.result).toMatchObject({ text: "Done", finishReason: "Stop" });});++ describe("chat names", () => {+ it("emits names separately from text and deduplicates the done summary", async () => {+ const { transport, emit } = fakeTransport();+ const run = runLlmStream(transport, params);+ const named = vi.fn();+ run.onChatName(named);++ emit({ type: "chatName", name: "First name" });+ emit({ type: "chunk", delta: "Hello" });+ emit({ type: "chatName", name: "Updated name" });+ emit({ type: "done", text: "Hello", stats, chatName: "Updated name" });++ expect(named.mock.calls).toEqual([["First name"], ["Updated name"]]);+ expect(await readAll(run.stream)).toBe("Hello");+ expect(await run.result).toMatchObject({ text: "Hello", chatName: "Updated name" });+ });++ it("notifies listeners when the name only arrives in the done summary", async () => {+ const { transport, emit } = fakeTransport();+ const run = runLlmStream(transport, params);+ const named = vi.fn();+ run.onChatName(named);++ emit({ type: "done", text: "Hello", stats, chatName: "A greeting" });++ expect(named).toHaveBeenCalledExactlyOnceWith("A greeting");+ expect(await run.result).toMatchObject({ chatName: "A greeting" });+ });++ it("replays a name emitted before the listener could subscribe", async () => {+ const { transport } = fakeTransport(async (_runId, _params, emit) => {+ emit({ type: "chatName", name: "A greeting" });+ emit({ type: "done", text: "Hello", stats, chatName: "A greeting" });+ });+ const run = runLlmStream(transport, params);+ const named = vi.fn();+ run.onChatName(named);++ expect(named).toHaveBeenCalledExactlyOnceWith("A greeting");+ expect(await run.result).toMatchObject({ chatName: "A greeting" });+ });++ it.each(["done", "error", "cancel"] as const)(+ "preserves the last name after %s and ignores later names",+ async (end) => {+ const { transport, emit } = fakeTransport();+ const run = runLlmStream(transport, params);+ const named = vi.fn();+ run.onChatName(named);+ emit({ type: "chatName", name: "Keep this name" });++ if (end === "cancel") {+ await run.cancel();+ } else if (end === "error") {+ emit({ type: "error", error: "generation failed" });+ await expect(readAll(run.stream)).rejects.toThrow("generation failed");+ } else {+ emit({ type: "done", text: "Hello", stats });+ }+ emit({ type: "chatName", name: "Ignore this name" });+ emit({ type: "done", text: "Late result", stats, chatName: "Ignore this summary" });++ expect(named).toHaveBeenCalledExactlyOnceWith("Keep this name");+ expect(await run.result).toMatchObject({ chatName: "Keep this name" });+ },+ );+ });});diff --git a/apps/mirai-gui/client/src/platform/services/chat/run-stream.ts b/apps/mirai-gui/client/src/platform/services/chat/run-stream.tsindex a4224395f..d71316199 100644--- a/apps/mirai-gui/client/src/platform/services/chat/run-stream.ts+++ b/apps/mirai-gui/client/src/platform/services/chat/run-stream.ts@@ -5,18 +5,24 @@ import type {ParsedPatch,SessionOutputFinishReason,SessionOutputStats,+ TranscriptItem,} from "@/types/llm-stream";import { v4 as uuidv4 } from "uuid";import { emptyStats } from "./empty-stats";export type RunEvent =| { type: "chunk"; delta: string; parsed?: ParsedPatch }+ | { type: "chatName"; name: string }+ | { type: "transcript"; items: TranscriptItem[] }+ | { type: "transcriptDelta"; index: number; delta: string }| {type: "done";text: string;stats: SessionOutputStats;finishReason?: SessionOutputFinishReason;parsed?: ParsedPatch;+ chatName?: string;+ transcript?: TranscriptItem[];}| { type: "error"; error?: string };@@ -29,6 +35,10 @@ export type RunTransport = {export function runLlmStream(transport: RunTransport, params: LlmRunParams): LlmAsyncStream {const runId = uuidv4();const parsedListeners = new Set<(patch: ParsedPatch) => void>();+ const chatNameListeners = new Set<(name: string) => void>();+ const transcriptListeners = new Set<(items: TranscriptItem[]) => void>();+ let chatName: string | undefined;+ let transcript: TranscriptItem[] | undefined;let settled = false;let resolveResult!: (result: LlmRunResult) => void;const result = new Promise<LlmRunResult>((resolve) => {@@ -39,7 +49,11 @@ export function runLlmStream(transport: RunTransport, params: LlmRunParams): Llmconst settle = (outcome: LlmRunResult): boolean => {if (settled) return false;settled = true;- resolveResult(outcome);+ resolveResult({+ ...outcome,+ ...(chatName !== undefined ? { chatName } : {}),+ ...(transcript !== undefined ? { transcript } : {}),+ });return true;};@@ -50,6 +64,17 @@ export function runLlmStream(transport: RunTransport, params: LlmRunParams): Llmif (patch) parsedListeners.forEach((listener) => listener(patch));};+ const notifyChatName = (name: string) => {+ if (name === chatName) return;+ chatName = name;+ chatNameListeners.forEach((listener) => listener(name));+ };++ const notifyTranscript = (items: TranscriptItem[]) => {+ transcript = items;+ transcriptListeners.forEach((listener) => listener(items));+ };+const stream = new ReadableStream<string>({start(controller) {const fail = (message: string) => {@@ -64,7 +89,26 @@ export function runLlmStream(transport: RunTransport, params: LlmRunParams): Llmcontroller.enqueue(event.delta ?? "");notifyParsed(event.parsed);return;+ case "chatName":+ notifyChatName(event.name);+ return;+ case "transcript":+ notifyTranscript(event.items);+ return;+ case "transcriptDelta": {+ const item = transcript?.[event.index];+ if (!transcript || !item || (item.type !== "text" && item.type !== "thinking")) {+ fail(`Invalid transcript delta at index ${event.index}`);+ return;+ }+ const items = [...transcript];+ items[event.index] = { ...item, text: item.text + event.delta };+ notifyTranscript(items);+ return;+ }case "done":+ if (event.chatName !== undefined) notifyChatName(event.chatName);+ if (event.transcript !== undefined) notifyTranscript(event.transcript);settle({text: event.text,stats: event.stats,@@ -103,5 +147,15 @@ export function runLlmStream(transport: RunTransport, params: LlmRunParams): LlmparsedListeners.add(listener);return () => parsedListeners.delete(listener);},+ onChatName: (listener) => {+ chatNameListeners.add(listener);+ if (chatName !== undefined) listener(chatName);+ return () => chatNameListeners.delete(listener);+ },+ onTranscript: (listener) => {+ transcriptListeners.add(listener);+ if (transcript !== undefined) listener(transcript);+ return () => transcriptListeners.delete(listener);+ },};}diff --git a/apps/mirai-gui/client/src/platform/services/chat/tauri.test.ts b/apps/mirai-gui/client/src/platform/services/chat/tauri.test.tsnew file mode 100644index 000000000..1038be1f6--- /dev/null+++ b/apps/mirai-gui/client/src/platform/services/chat/tauri.test.ts@@ -0,0 +1,23 @@+import { beforeEach, expect, it, vi } from "vitest";+import { tauriChat } from "./tauri";+import type { LlmRunParams } from "@/types/llm-stream";++const mocks = vi.hoisted(() => ({ invoke: vi.fn(async () => {}) }));+vi.mock("../shared/invoke", () => ({ invoke: mocks.invoke }));+vi.mock("@tauri-apps/api/core", () => ({ Channel: class {} }));++beforeEach(() => vi.clearAllMocks());++it.each([true, false])("sends chartToolEnabled=%s through the native run payload", async (chartToolEnabled) => {+ const params: LlmRunParams = {+ repoId: "model",+ messages: [{ role: "user", content: "Draw a chart" }],+ chartToolEnabled,+ };+ const run = tauriChat.runStream(params);+ expect(mocks.invoke).toHaveBeenCalledWith("run_stream", {+ payload: { runId: run.runId, ...params },+ onEvent: expect.any(Object),+ });+ await run.cancel();+});diff --git a/apps/mirai-gui/client/src/platform/services/chat/web.ts b/apps/mirai-gui/client/src/platform/services/chat/web.tsindex d41c58d83..dc5f100ff 100644--- a/apps/mirai-gui/client/src/platform/services/chat/web.ts+++ b/apps/mirai-gui/client/src/platform/services/chat/web.ts@@ -15,7 +15,15 @@ export const webChat: ChatService = {error: "Chat not available on web",stats: emptyStats(),});- return { runId: "", stream, result, cancel: () => Promise.resolve(), onParsed: noopUnsubscribe };+ return {+ runId: "",+ stream,+ result,+ cancel: () => Promise.resolve(),+ onParsed: noopUnsubscribe,+ onChatName: noopUnsubscribe,+ onTranscript: noopUnsubscribe,+ };},cancelRun: () => Promise.resolve(),generateTitle: () => Promise.reject(new Error("Not available on web")),diff --git a/apps/mirai-gui/client/src/platform/services/models/index.ts b/apps/mirai-gui/client/src/platform/services/models/index.tsindex 7ec7e671e..30418a3c6 100644--- a/apps/mirai-gui/client/src/platform/services/models/index.ts+++ b/apps/mirai-gui/client/src/platform/services/models/index.ts@@ -1,6 +1,7 @@-import type { EngineModel } from "@/types/model-manager";+import type { ModelCatalog, ModelDownloadPhase } from "@/types/model-manager";export type DownloadEvent = { identifier: string; seq: number } & (+ | { kind: "state"; phase: ModelDownloadPhase; completedBytes: number; totalBytes: number; error: string | null }| { kind: "progress"; completedBytes: number; totalBytes: number | null }| { kind: "error"; error: string }| { kind: "locked"; lockedBy: string }@@ -8,10 +9,12 @@ export type DownloadEvent = { identifier: string; seq: number } & ();export type ModelsService = {- getModels(): Promise<EngineModel[]>;+ getModels(): Promise<ModelCatalog>;+ refreshModels(): Promise<void>;startDownload(identifier: string): Promise<void>;pauseDownload(identifier: string): Promise<void>;resumeDownload(identifier: string): Promise<void>;deleteModel(identifier: string): Promise<void>;onDownloadEvent(listener: (event: DownloadEvent) => void): () => void;+ onModelsChanged(listener: () => void): () => void;};diff --git a/apps/mirai-gui/client/src/platform/services/models/tauri.ts b/apps/mirai-gui/client/src/platform/services/models/tauri.tsindex 1c106847d..6966a318b 100644--- a/apps/mirai-gui/client/src/platform/services/models/tauri.ts+++ b/apps/mirai-gui/client/src/platform/services/models/tauri.ts@@ -1,29 +1,46 @@import { listen } from "@tauri-apps/api/event";import { invoke } from "../shared/invoke";-import type { EngineModel } from "@/types/model-manager";+import type { ModelCatalog } from "@/types/model-manager";import type { DownloadEvent, ModelsService } from ".";// One backend subscription for the app's lifetime, fanned out to listeners.const listeners = new Set<(event: DownloadEvent) => void>();-let listening = false;+const catalogListeners = new Set<() => void>();+let listening: Promise<void> | null = null;-const ensureListening = (): void => {- if (listening) return;- listening = true;- void listen<DownloadEvent>("download-state", ({ payload: event }) => {- listeners.forEach((listener) => {- try {- listener(event);- } catch (e) {- // State wiring and toasts are independent; one failing must not starve the other.- console.error("[downloads] listener failed", { kind: event.kind, identifier: event.identifier }, e);- }+const ensureListening = (): Promise<void> => {+ if (listening) return listening;+ listening = (async () => {+ const stopDownloads = await listen<DownloadEvent>("download-state", ({ payload: event }) => {+ listeners.forEach((listener) => {+ try {+ listener(event);+ } catch (e) {+ // State wiring and toasts are independent; one failing must not starve the other.+ console.error("[downloads] listener failed", { kind: event.kind, identifier: event.identifier }, e);+ }+ });});+ try {+ await listen("models-changed", () => catalogListeners.forEach((listener) => listener()));+ } catch (error) {+ stopDownloads();+ throw error;+ }+ })().catch((error) => {+ listening = null;+ throw error;});+ return listening;};export const tauriModels: ModelsService = {- getModels: () => invoke<EngineModel[]>("chat_models_get"),+ getModels: async () => {+ // Subscribe before taking a snapshot so readiness updates cannot fall in between.+ await ensureListening();+ return invoke<ModelCatalog>("chat_models_get");+ },+ refreshModels: () => invoke("chat_models_refresh"),startDownload: (repoId) => invoke("download_resume", { repoId }),pauseDownload: (repoId) => invoke("download_pause", { repoId }),@@ -31,8 +48,14 @@ export const tauriModels: ModelsService = {deleteModel: (repoId) => invoke("download_delete", { repoId }),onDownloadEvent: (listener) => {- ensureListening();listeners.add(listener);+ void ensureListening().catch((error) => console.error("[downloads] subscription failed", error));return () => listeners.delete(listener);},++ onModelsChanged: (listener) => {+ catalogListeners.add(listener);+ void ensureListening().catch((error) => console.error("[models] subscription failed", error));+ return () => catalogListeners.delete(listener);+ },};diff --git a/apps/mirai-gui/client/src/platform/services/models/web.ts b/apps/mirai-gui/client/src/platform/services/models/web.tsindex 132fb28d5..3e85c573d 100644--- a/apps/mirai-gui/client/src/platform/services/models/web.ts+++ b/apps/mirai-gui/client/src/platform/services/models/web.ts@@ -4,10 +4,12 @@ import { noopUnsubscribe } from "../shared/noop";const unavailable = () => Promise.reject(new Error("Not available on web"));export const webModels: ModelsService = {- getModels: () => Promise.resolve([]),+ getModels: () => Promise.resolve({ models: [], complete: true, refreshing: false }),+ refreshModels: () => Promise.resolve(),startDownload: unavailable,pauseDownload: unavailable,resumeDownload: unavailable,deleteModel: unavailable,onDownloadEvent: noopUnsubscribe,+ onModelsChanged: noopUnsubscribe,};diff --git a/apps/mirai-gui/client/src/platform/services/settings/index.ts b/apps/mirai-gui/client/src/platform/services/settings/index.tsindex dacfe4a0b..4096ba2f3 100644--- a/apps/mirai-gui/client/src/platform/services/settings/index.ts+++ b/apps/mirai-gui/client/src/platform/services/settings/index.ts@@ -1,8 +1,12 @@import type { ModelParams } from "@/types/sampling";+export const DEFAULT_AUTO_EJECT_MINUTES = 15;+export type SettingsService = {- getEnableThinking(): Promise<boolean>;- setEnableThinking(enabled: boolean): Promise<boolean>;+ getAnalyticsEnabled(): Promise<boolean>;+ setAnalyticsEnabled(enabled: boolean): Promise<void>;+ getModelChatNamingEnabled(): Promise<boolean>;+ setModelChatNamingEnabled(enabled: boolean): Promise<void>;getModelParams(): Promise<Record<string, ModelParams>>;setModelParams(repoId: string, params: ModelParams | null): Promise<void>;getAutoEjectEnabled(): Promise<boolean>;diff --git a/apps/mirai-gui/client/src/platform/services/settings/settings.test.ts b/apps/mirai-gui/client/src/platform/services/settings/settings.test.tsnew file mode 100644index 000000000..3d4763961--- /dev/null+++ b/apps/mirai-gui/client/src/platform/services/settings/settings.test.ts@@ -0,0 +1,84 @@+import { beforeEach, describe, expect, it, vi } from "vitest";+import { tauriSettings } from "./tauri";+import { webSettings } from "./web";+import { invoke } from "../shared/invoke";++vi.mock("../shared/invoke", () => ({ invoke: vi.fn() }));++beforeEach(() => {+ vi.resetAllMocks();+ localStorage.clear();+});++describe("analytics consent", () => {+ it.each([undefined, null, false, "true", 1])("requires an explicit desktop opt-in, not %s", async (value) => {+ vi.mocked(invoke).mockResolvedValue({ analyticsEnabled: value });+ expect(await tauriSettings.getAnalyticsEnabled()).toBe(false);+ });++ it("loads and patches explicit desktop consent", async () => {+ vi.mocked(invoke).mockResolvedValue({ analyticsEnabled: true });+ expect(await tauriSettings.getAnalyticsEnabled()).toBe(true);++ await tauriSettings.setAnalyticsEnabled(false);+ expect(invoke).toHaveBeenLastCalledWith("settings_patch", { patch: { analyticsEnabled: false } });+ });++ it("defaults off in the browser and preserves other preferences when changed", async () => {+ localStorage.setItem("mirai.web.settings", JSON.stringify({ modelChatNamingEnabled: false }));+ expect(await webSettings.getAnalyticsEnabled()).toBe(false);++ await webSettings.setAnalyticsEnabled(true);+ expect(await webSettings.getAnalyticsEnabled()).toBe(true);+ expect(await webSettings.getModelChatNamingEnabled()).toBe(false);+ await webSettings.setAnalyticsEnabled(false);+ expect(await webSettings.getAnalyticsEnabled()).toBe(false);+ });+});++describe("model chat naming preference", () => {+ it("defaults on for existing desktop settings", async () => {+ vi.mocked(invoke).mockResolvedValue({ enableThinking: false });+ expect(await tauriSettings.getModelChatNamingEnabled()).toBe(true);+ });++ it("loads and patches the disabled desktop preference", async () => {+ vi.mocked(invoke).mockResolvedValue({ modelChatNamingEnabled: false });+ expect(await tauriSettings.getModelChatNamingEnabled()).toBe(false);++ await tauriSettings.setModelChatNamingEnabled(false);+ expect(invoke).toHaveBeenLastCalledWith("settings_patch", { patch: { modelChatNamingEnabled: false } });+ });++ it("defaults on in the browser and persists disabling without replacing other settings", async () => {+ localStorage.setItem("mirai.web.settings", JSON.stringify({ unrelatedPreference: true }));+ expect(await webSettings.getModelChatNamingEnabled()).toBe(true);++ await webSettings.setModelChatNamingEnabled(false);+ expect(await webSettings.getModelChatNamingEnabled()).toBe(false);+ expect(JSON.parse(localStorage.getItem("mirai.web.settings")!)).toMatchObject({ unrelatedPreference: true });+ });+});++describe("auto-eject defaults", () => {+ it("defaults to fifteen minutes on desktop and web", async () => {+ vi.mocked(invoke).mockResolvedValue({});+ expect(await tauriSettings.getAutoEjectMinutes()).toBe(15);+ expect(await webSettings.getAutoEjectMinutes()).toBe(15);+ expect(await tauriSettings.getAutoEjectEnabled()).toBe(true);+ });++ it.each([2, 7, 30])("preserves a saved interval of %s minutes", async (minutes) => {+ vi.mocked(invoke).mockResolvedValue({ autoEjectMinutes: minutes, autoEjectEnabled: false });+ expect(await tauriSettings.getAutoEjectMinutes()).toBe(minutes);+ expect(await tauriSettings.getAutoEjectEnabled()).toBe(false);+ });++ it.each([null, "7", 0, -1, Number.NaN, Number.POSITIVE_INFINITY])(+ "uses the default for invalid interval %s",+ async (minutes) => {+ vi.mocked(invoke).mockResolvedValue({ autoEjectMinutes: minutes });+ expect(await tauriSettings.getAutoEjectMinutes()).toBe(15);+ },+ );+});diff --git a/apps/mirai-gui/client/src/platform/services/settings/tauri.ts b/apps/mirai-gui/client/src/platform/services/settings/tauri.tsindex 4c413dc7d..3112dd0e3 100644--- a/apps/mirai-gui/client/src/platform/services/settings/tauri.ts+++ b/apps/mirai-gui/client/src/platform/services/settings/tauri.ts@@ -1,12 +1,13 @@import { invoke } from "../shared/invoke";import type { ModelParams } from "@/types/sampling";-import type { SettingsService } from ".";+import { DEFAULT_AUTO_EJECT_MINUTES, type SettingsService } from ".";// Keys are read by every installed version; renaming one resets that// preference on upgrade.type StoredSettings = {- enableThinking?: boolean;+ analyticsEnabled?: boolean;+ modelChatNamingEnabled?: boolean;autoEjectEnabled?: boolean;autoEjectMinutes?: number;modelParams?: Record<string, ModelParams>;@@ -18,14 +19,15 @@ const patch = (values: Record<string, unknown>): Promise<void> => invoke("settinconst autoEjectEnabledOf = (s: StoredSettings): boolean => s.autoEjectEnabled !== false;const autoEjectMinutesOf = (s: StoredSettings): number =>- typeof s.autoEjectMinutes === "number" && Number.isFinite(s.autoEjectMinutes) ? s.autoEjectMinutes : 2;+ typeof s.autoEjectMinutes === "number" && Number.isFinite(s.autoEjectMinutes) && s.autoEjectMinutes > 0+ ? s.autoEjectMinutes+ : DEFAULT_AUTO_EJECT_MINUTES;export const tauriSettings: SettingsService = {- getEnableThinking: async () => (await load()).enableThinking !== false,- setEnableThinking: async (enabled) => {- await patch({ enableThinking: enabled });- return enabled;- },+ getAnalyticsEnabled: async () => (await load()).analyticsEnabled === true,+ setAnalyticsEnabled: (enabled) => patch({ analyticsEnabled: enabled }),+ getModelChatNamingEnabled: async () => (await load()).modelChatNamingEnabled !== false,+ setModelChatNamingEnabled: (enabled) => patch({ modelChatNamingEnabled: enabled }),getModelParams: async () => (await load()).modelParams ?? {},setModelParams: (repoId, params) => invoke("model_params_set", { repoId, params }),getAutoEjectEnabled: async () => autoEjectEnabledOf(await load()),diff --git a/apps/mirai-gui/client/src/platform/services/settings/web.ts b/apps/mirai-gui/client/src/platform/services/settings/web.tsindex b5af5a070..c64aee16a 100644--- a/apps/mirai-gui/client/src/platform/services/settings/web.ts+++ b/apps/mirai-gui/client/src/platform/services/settings/web.ts@@ -1,22 +1,27 @@import type { ModelParams } from "@/types/sampling";-import type { SettingsService } from ".";+import { DEFAULT_AUTO_EJECT_MINUTES, type SettingsService } from ".";import { readJson, writeJson } from "../shared/browser-store";const SETTINGS_KEY = "mirai.web.settings";const MODEL_PARAMS_KEY = "mirai.web.modelParams";-type StoredSettings = { enableThinking: boolean };+type StoredSettings = { analyticsEnabled: boolean; modelChatNamingEnabled: boolean };-const DEFAULTS: StoredSettings = { enableThinking: false };+const DEFAULTS: StoredSettings = { analyticsEnabled: false, modelChatNamingEnabled: true };const load = (): StoredSettings => ({ ...DEFAULTS, ...readJson<Partial<StoredSettings>>(SETTINGS_KEY, {}) });export const webSettings: SettingsService = {- getEnableThinking: async () => load().enableThinking,+ getAnalyticsEnabled: async () => load().analyticsEnabled === true,- setEnableThinking: async (enabled) => {- writeJson(SETTINGS_KEY, { ...load(), enableThinking: enabled });- return enabled;+ setAnalyticsEnabled: async (enabled) => {+ writeJson(SETTINGS_KEY, { ...load(), analyticsEnabled: enabled });+ },++ getModelChatNamingEnabled: async () => load().modelChatNamingEnabled !== false,++ setModelChatNamingEnabled: async (enabled) => {+ writeJson(SETTINGS_KEY, { ...load(), modelChatNamingEnabled: enabled });},getModelParams: async () => readJson<Record<string, ModelParams>>(MODEL_PARAMS_KEY, {}),@@ -31,6 +36,6 @@ export const webSettings: SettingsService = {// No resident engine in the browser, so auto-eject is inert.getAutoEjectEnabled: async () => true,setAutoEjectEnabled: async (enabled) => enabled,- getAutoEjectMinutes: async () => 2,+ getAutoEjectMinutes: async () => DEFAULT_AUTO_EJECT_MINUTES,setAutoEjectMinutes: async () => true,};diff --git a/apps/mirai-gui/client/src/platform/services/storage/chat-repository.test.ts b/apps/mirai-gui/client/src/platform/services/storage/chat-repository.test.tsnew file mode 100644index 000000000..f1f1c91e7--- /dev/null+++ b/apps/mirai-gui/client/src/platform/services/storage/chat-repository.test.ts@@ -0,0 +1,134 @@+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { ChatNotFoundError, type ChatData } from ".";+import { serializeToMarkdown } from "./markdown/serialize";++const files = vi.hoisted(() => ({ load: vi.fn(), save: vi.fn(), list: vi.fn(), delete: vi.fn() }));+vi.mock("./chat-files", () => ({+ loadChatFile: files.load,+ saveChatFile: files.save,+ listChatFiles: files.list,+ deleteChatFile: files.delete,+}));++import { chatRepository } from "./chat-repository";+import { webStorage } from "./web";++const timestamp = 1_700_000_000_000;+const now = timestamp + 100_000;+const original: ChatData = {+ metadata: {+ id: "chat",+ title: "Existing title",+ modelId: "vendor/model",+ modelName: "Model",+ createdAt: timestamp,+ updatedAt: timestamp,+ messageCount: 4,+ },+ messages: [+ { id: "u1", sender: "user", text: "First question", timestamp },+ { id: "a1", sender: "assistant", text: "First answer", timestamp: timestamp + 1_000 },+ {+ id: "u2",+ sender: "user",+ text: "Old question",+ timestamp: timestamp + 2_000,+ attachmentIds: ["image-id", "document-id"],+ },+ { id: "a2", sender: "assistant", text: "Old answer", timestamp: timestamp + 3_000 },+ ],+};+let disk: Map<string, string>;++beforeEach(() => {+ vi.resetAllMocks();+ vi.spyOn(Date, "now").mockReturnValue(now);+ disk = new Map([["chat.md", serializeToMarkdown(original)]]);+ files.load.mockImplementation(async (path: string) => disk.get(path) ?? null);+ files.save.mockImplementation(async (path: string, content: string) => {+ disk.set(path, content);+ });+});++afterEach(() => vi.restoreAllMocks());++describe("editUserMessage", () => {+ it("saves the edited prompt and truncates later messages while preserving attachments and metadata", async () => {+ const before = (await chatRepository.loadChat("chat"))!;+ const saved = await chatRepository.editUserMessage("chat", "u2", "Edited question\nwith a second line");++ expect(saved.messages.map((message) => message.id)).toEqual(["u1", "a1", "u2"]);+ expect(saved.messages.slice(0, 2)).toEqual(before.messages.slice(0, 2));+ expect(saved.messages[2]).toMatchObject({+ id: "u2",+ sender: "user",+ text: "Edited question\nwith a second line",+ timestamp: before.messages[2]!.timestamp,+ attachmentIds: ["image-id", "document-id"],+ });+ expect(saved.metadata).toEqual({ ...before.metadata, messageCount: 3, updatedAt: now });+ expect(await chatRepository.loadChat("chat")).toEqual(saved);+ expect(files.save).toHaveBeenCalledTimes(1);+ });++ it("loads the latest metadata inside the same queue as other chat mutations", async () => {+ const renamed = chatRepository.updateChatTitle("chat", "New title");+ const edited = chatRepository.editUserMessage("chat", "u1", "New first question");+ await renamed;+ const saved = await edited;+ expect(saved.metadata.title).toBe("New title");+ expect(saved.metadata.messageCount).toBe(1);+ expect(saved.messages.map((message) => message.text)).toEqual(["New first question"]);+ expect(await chatRepository.loadChat("chat")).toEqual(saved);+ });++ it.each([+ ["missing-chat", "u2", "Chat missing-chat not found"],+ ["chat", "missing-message", "Message missing-message not found"],+ ["chat", "a1", "Only user messages can be edited"],+ ])("rejects invalid target %s/%s without writing", async (chatId, messageId, error) => {+ const before = disk.get("chat.md");+ await expect(chatRepository.editUserMessage(chatId, messageId, "Edit")).rejects.toThrow(error);+ expect(files.save).not.toHaveBeenCalled();+ expect(disk.get("chat.md")).toBe(before);+ });++ it("rejects a failed write without losing the old conversation and allows retry", async () => {+ const before = disk.get("chat.md");+ files.save.mockRejectedValueOnce(new Error("disk full"));+ await expect(chatRepository.editUserMessage("chat", "u2", "Edit")).rejects.toThrow("disk full");+ expect(disk.get("chat.md")).toBe(before);+ expect((await chatRepository.loadChat("chat"))?.messages.map((message) => message.text)).toEqual(+ original.messages.map((message) => message.text),+ );+ const retried = await chatRepository.editUserMessage("chat", "u2", "Retried edit");+ expect(retried.messages.at(-1)?.text).toBe("Retried edit");+ expect(await chatRepository.loadChat("chat")).toEqual(retried);+ });++ it("waits for the save to succeed before returning the replacement chat", async () => {+ let finishSave!: () => void;+ files.save.mockImplementationOnce(+ (path: string, content: string) =>+ new Promise<void>((resolve) => {+ finishSave = () => {+ disk.set(path, content);+ resolve();+ };+ }),+ );+ const settled = vi.fn();+ const edited = chatRepository.editUserMessage("chat", "u2", "Edit").then(settled);+ await vi.waitFor(() => expect(files.save).toHaveBeenCalled());+ expect(settled).not.toHaveBeenCalled();+ expect((await chatRepository.loadChat("chat"))?.messages).toHaveLength(4);+ finishSave();+ await edited;+ expect(settled).toHaveBeenCalledOnce();+ expect((await chatRepository.loadChat("chat"))?.messages).toHaveLength(3);+ });++ it("rejects edits in the browser provider, which has no stored chats", async () => {+ await expect(webStorage.editUserMessage("chat", "u2", "Edit")).rejects.toBeInstanceOf(ChatNotFoundError);+ });+});diff --git a/apps/mirai-gui/client/src/platform/services/storage/chat-repository.ts b/apps/mirai-gui/client/src/platform/services/storage/chat-repository.tsindex 1f8d862b8..04adf4310 100644--- a/apps/mirai-gui/client/src/platform/services/storage/chat-repository.ts+++ b/apps/mirai-gui/client/src/platform/services/storage/chat-repository.ts@@ -14,6 +14,7 @@ type ChatRepository = Pick<| "removeMessage"| "appendMessage"| "updateStoredMessage"+ | "editUserMessage"| "updateChatTitle"| "deleteChat">;@@ -87,6 +88,41 @@ export const chatRepository: ChatRepository = {});},+ editUserMessage(chatId, messageId, text) {+ return withChatLock(chatId, async () => {+ const existing = await loadChat(chatId);+ if (!existing) throw new ChatNotFoundError(chatId);+ const index = existing.messages.findIndex((message) => message.id === messageId);+ const original = existing.messages[index];+ if (!original) throw new Error(`Message ${messageId} not found in chat ${chatId}`);+ if (original.sender !== "user") throw new Error("Only user messages can be edited");+ const edited: Message = {+ ...original,+ text,+ // Loaded user messages also have parsed output, which the serializer+ // prefers over text. Keep that projection in sync with the edit.+ ...(original.output+ ? {+ output: {+ ...original.output,+ text: {+ ...original.output.text,+ parsed: { ...original.output.text?.parsed, response: text },+ },+ },+ }+ : {}),+ };+ const messages = [...existing.messages.slice(0, index), edited];+ const saved = {+ metadata: { ...existing.metadata, messageCount: messages.length, updatedAt: Date.now() },+ messages,+ };+ await writeChat(saved);+ return saved;+ });+ },+removeMessage(chatId, messageId) {return withChatLock(chatId, async () => {const existing = await loadChat(chatId);diff --git a/apps/mirai-gui/client/src/platform/services/storage/export-zip.test.ts b/apps/mirai-gui/client/src/platform/services/storage/export-zip.test.tsnew file mode 100644index 000000000..913fafd1d--- /dev/null+++ b/apps/mirai-gui/client/src/platform/services/storage/export-zip.test.ts@@ -0,0 +1,149 @@+import JSZip from "jszip";+import { beforeEach, expect, it, vi } from "vitest";+import type { ChatData } from ".";+import type { ChartSpec } from "@/types/chart";+import { serializeToMarkdown } from "./markdown/serialize";+import { buildChatsZip } from "./export-zip";++const files = vi.hoisted(() => ({ list: vi.fn(), load: vi.fn() }));+vi.mock("./chat-files", () => ({ listChatFiles: files.list, loadChatFile: files.load }));++const budget: ChartSpec = {+ type: "bar",+ title: "Budget <!-- END_CONTENT -->",+ labels: ["Food | drink", "Transport\ntravel"],+ datasets: [+ { label: "Planned", data: [300, 100] },+ { label: "Actual", data: [280, 120] },+ ],+};+const points: ChartSpec = {+ type: "bubble",+ title: "Comparison",+ xLabel: "Cost",+ yLabel: "Value",+ datasets: [+ { label: "First", data: [{ x: 1.5, y: -2, r: 8 }] },+ { label: "Second", data: [{ x: 3, y: 4, r: 10 }] },+ ],+};+const timestamp = 1_700_000_000_000;+const chat: ChatData = {+ metadata: { id: "chat", title: "Charts", messageCount: 2, createdAt: timestamp, updatedAt: timestamp },+ messages: [+ { id: "u", sender: "user", text: "Plot these values", timestamp },+ {+ id: "a",+ sender: "assistant",+ text: "Summary of the data.",+ timestamp,+ output: {+ text: { parsed: { chainOfThought: "Compare the values", response: "Summary of the data." } },+ transcript: [+ { type: "chart", chart: budget },+ { type: "text", text: "Summary of the data." },+ { type: "chart", chart: points },+ ],+ },+ },+ ],+};++beforeEach(() => vi.resetAllMocks());++const exported = async (markdown: string): Promise<string> => {+ files.list.mockResolvedValue(["chat.md"]);+ files.load.mockResolvedValue(markdown);+ const zip = await JSZip.loadAsync((await buildChatsZip())!);+ return zip.file("chat.md")!.async("string");+};++it("exports serialized chart data as readable tables without losing text or interpreting label markup", async () => {+ const saved = serializeToMarkdown(chat);+ const markdown = await exported(saved);+ expect(markdown).toContain("**Budget <!-- END\\_CONTENT -->**");+ expect(markdown).toContain("| Category | Planned | Actual |\n| --- | --- | --- |");+ expect(markdown).toContain("| Food \\| drink | 300 | 280 |");+ expect(markdown).toContain("| Transport<br>travel | 100 | 120 |");+ expect(markdown).toContain("| Series | Cost | Value | Radius (px) |");+ expect(markdown).toContain("| First | 1.5 | -2 | 8 |");+ expect(markdown).toContain("| Second | 3 | 4 | 10 |");+ expect(markdown.indexOf("**Budget")).toBeLessThan(markdown.indexOf("**Comparison"));+ expect(markdown).toContain("Plot these values");+ expect(markdown).toContain("<summary>Thinking</summary>");+ expect(markdown).toContain("Compare the values");+ expect(markdown.match(/Summary of the data\./g)).toHaveLength(1);+ expect(markdown).not.toContain("<!--");+ expect(saved).toBe(serializeToMarkdown(chat));+});++it("exports charts in saved response versions and marks invalid chart data visibly", async () => {+ const assistant = chat.messages[1]!;+ const versioned: ChatData = {+ ...chat,+ messages: [+ {+ ...assistant,+ currentVersionIndex: 1,+ versions: [+ {+ id: "v1",+ modelId: "model",+ modelName: "Model",+ timestamp,+ text: "First version",+ output: { transcript: [{ type: "chart", chart: budget }] },+ },+ {+ id: "v2",+ modelId: "model",+ modelName: "Model",+ timestamp,+ text: "Second version",+ output: { transcript: [{ type: "chart", chart: points }] },+ },+ ],+ },+ ],+ };+ const markdown = await exported(serializeToMarkdown(versioned));+ expect(markdown.indexOf("**Budget")).toBeLessThan(markdown.indexOf("#### Version 2"));+ expect(markdown.indexOf("**Comparison")).toBeGreaterThan(markdown.indexOf("#### Version 2"));+ const corrupted = serializeToMarkdown(chat).replace('"type":"bar"', '"type":"unsupported"');+ expect(await exported(corrupted)).toContain("This chart could not be exported: invalid data.");+});++it.each(["response", "reasoning", "error"] as const)(+ "does not export chart metadata examples in %s or version content as actual charts",+ async (location) => {+ const example = `Example metadata:\n<!-- TRANSCRIPT: ${JSON.stringify([{ type: "chart", chart: points }])} -->`;+ const text = location === "response" ? example : "Original response";+ const output =+ location === "reasoning" ? { text: { parsed: { response: text, chainOfThought: example } } } : undefined;+ const exampleMessage = {+ id: "example",+ sender: "assistant" as const,+ timestamp,+ text,+ output,+ ...(location === "error" ? { error: example } : {}),+ };+ const saved = serializeToMarkdown({+ ...chat,+ messages: [+ exampleMessage,+ {+ ...exampleMessage,+ id: "versioned",+ currentVersionIndex: 0,+ versions: [{ ...exampleMessage, modelId: "model", modelName: "Model" }],+ },+ { ...exampleMessage, id: "actual-chart", output: { transcript: [{ type: "chart", chart: budget }] } },+ ],+ });+ const markdown = await exported(saved);+ expect(markdown).not.toContain("**Comparison**");+ expect(markdown).not.toContain("| First | 1.5 | -2 | 8 |");+ expect(markdown.match(/\*\*Budget/g)).toHaveLength(1);+ },+);diff --git a/apps/mirai-gui/client/src/platform/services/storage/export-zip.ts b/apps/mirai-gui/client/src/platform/services/storage/export-zip.tsindex 01f2b453b..e25d3348d 100644--- a/apps/mirai-gui/client/src/platform/services/storage/export-zip.ts+++ b/apps/mirai-gui/client/src/platform/services/storage/export-zip.ts@@ -1,6 +1,32 @@import JSZip from "jszip";import { listChatFiles, loadChatFile } from "./chat-files";import { stripHtmlComments, wrapCotInDetails } from "./markdown/parse";+import { isChartSpec } from "@/types/chart";+import { chartToMarkdown } from "@/utils/chart-data";+import { transcriptMetadata } from "./markdown/transcript-metadata";++const includeChartTables = (markdown: string): string => {+ let offset = 0;+ let result = "";+ for (const { start, end, json } of transcriptMetadata(markdown)) {+ result += markdown.slice(offset, start);+ offset = end;+ try {+ const items: unknown = JSON.parse(json);+ if (!Array.isArray(items)) continue;+ const charts = items+ .filter((item) => item?.type === "chart")+ .map((item) =>+ isChartSpec(item.chart) ? chartToMarkdown(item.chart) : "_This chart could not be exported: invalid data._",+ )+ .join("\n\n");+ if (charts) result += `\n${charts}\n`;+ } catch {+ // Invalid metadata was previously stripped with the other HTML comments.+ }+ }+ return result + markdown.slice(offset);+};export const buildChatsZip = async (): Promise<Uint8Array | null> => {const zip = new JSZip();@@ -12,7 +38,7 @@ export const buildChatsZip = async (): Promise<Uint8Array | null> => {const content = await loadChatFile(file);if (!content) continue;const normalized = content.replace(/\*\*TTFB:\*\*/g, "**TTFT:**");- zip.file(file, stripHtmlComments(wrapCotInDetails(normalized)));+ zip.file(file, stripHtmlComments(wrapCotInDetails(includeChartTables(normalized))));} catch {continue;}diff --git a/apps/mirai-gui/client/src/platform/services/storage/index.ts b/apps/mirai-gui/client/src/platform/services/storage/index.tsindex 546ea25dd..8191f1d01 100644--- a/apps/mirai-gui/client/src/platform/services/storage/index.ts+++ b/apps/mirai-gui/client/src/platform/services/storage/index.ts@@ -35,6 +35,8 @@ export type StorageService = {createOrReplaceChat(chat: ChatData): Promise<void>;appendMessage(chatId: string, message: Message): Promise<void>;updateStoredMessage(chatId: string, messageId: string, patch: Partial<Message>): Promise<void>;+ /** Edits a user message and removes all later messages in one stored update. */+ editUserMessage(chatId: string, messageId: string, text: string): Promise<ChatData>;removeMessage(chatId: string, messageId: string): Promise<void>;/** With `expectedTitle`, writes only while the stored title is still that one. */updateChatTitle(chatId: string, title: string, expectedTitle?: string): Promise<void>;diff --git a/apps/mirai-gui/client/src/platform/services/storage/markdown/parse.ts b/apps/mirai-gui/client/src/platform/services/storage/markdown/parse.tsindex ebb1ea694..b0d60168a 100644--- a/apps/mirai-gui/client/src/platform/services/storage/markdown/parse.ts+++ b/apps/mirai-gui/client/src/platform/services/storage/markdown/parse.ts@@ -1,6 +1,40 @@import type { Message, PerfStats, MessageVersion as StoreMessageVersion } from "@/types/message";import { parse } from "date-fns";import type { ChatMetadata } from "..";+import type { TranscriptItem } from "@/types/llm-stream";+import { isChartSpec } from "@/types/chart";+import { transcriptMetadata } from "./transcript-metadata";++const extractTranscript = (text: string): TranscriptItem[] | undefined => {+ const json = transcriptMetadata(text).next().value?.json;+ if (json === undefined) return undefined;+ try {+ const items: unknown = JSON.parse(json);+ if (+ !Array.isArray(items) ||+ !items.every((item) => {+ if (!item || typeof item !== "object") return false;+ if (item.type === "toolCall")+ return (+ typeof item.name === "string" &&+ typeof item.called === "boolean" &&+ (item.failed === undefined || typeof item.failed === "boolean")+ );+ if (item.type === "text") return typeof item.text === "string";+ if (item.type === "chart") return isChartSpec(item.chart);+ return (+ item.type === "thinking" &&+ typeof item.text === "string" &&+ (item.completed === undefined || typeof item.completed === "boolean")+ );+ })+ )+ return undefined;+ return items;+ } catch {+ return undefined;+ }+};const extractContent = (text: string): string => {const match = text.match(/<!-- START_CONTENT -->\n([\s\S]*?)\n<!-- END_CONTENT -->/);@@ -63,9 +97,11 @@ const parseVersions = (block: string): { versions: StoreMessageVersion[]; currenconst cot = extractChainOfThought(content);const versionError = extractError(content);const versionPerf = parsePerf(content);+ const transcript = extractTranscript(content);const outputParsed =- cot || versionText+ cot || versionText || transcript !== undefined? {+ ...(transcript !== undefined ? { transcript } : {}),text: {parsed: { ...(cot ? { chainOfThought: cot } : {}), ...(versionText ? { response: versionText } : {}) },},@@ -122,9 +158,13 @@ export const parseMessage = (block: string): Message | null => {const resp = extractContent(block);const errorText = extractError(block);const perf = parsePerf(block);+ const transcript = extractTranscript(block);const outputParsed =- cot || resp- ? { text: { parsed: { ...(cot ? { chainOfThought: cot } : {}), ...(resp ? { response: resp } : {}) } } }+ cot || resp || transcript !== undefined+ ? {+ ...(transcript !== undefined ? { transcript } : {}),+ text: { parsed: { ...(cot ? { chainOfThought: cot } : {}), ...(resp ? { response: resp } : {}) } },+ }: undefined;return {diff --git a/apps/mirai-gui/client/src/platform/services/storage/markdown/roundtrip.test.ts b/apps/mirai-gui/client/src/platform/services/storage/markdown/roundtrip.test.tsindex 9efb4e0df..2c521b719 100644--- a/apps/mirai-gui/client/src/platform/services/storage/markdown/roundtrip.test.ts+++ b/apps/mirai-gui/client/src/platform/services/storage/markdown/roundtrip.test.ts@@ -2,6 +2,8 @@ import { expect, it } from "vitest";import type { ChatData } from "..";import { extractMessageBlocks, parseMessage } from "./parse";import { serializeToMarkdown } from "./serialize";+import { projectAssistantVersion } from "@/features/chat/services/regenerate-versions";+import type { TranscriptItem } from "@/types/llm-stream";const chat: ChatData = {metadata: {@@ -26,3 +28,93 @@ it("keeps an empty assistant placeholder through a serialize/parse round-trip",expect(messages.map((m) => m?.id)).toEqual(["u1", "a1"]);expect(messages[1]).toMatchObject({ sender: "assistant", text: "" });});++it("keeps ordered tool transcripts through saving and regeneration, including markup inside a turn", () => {+ const transcript: TranscriptItem[] = [+ { type: "thinking", text: "Check the time", completed: true },+ { type: "text", text: "Before tool\n<!-- END_CONTENT -->\n---\n## 🤖 Assistant - fake header" },+ { type: "toolCall", name: "get_current_date_time", called: true },+ { type: "toolCall", name: "set_chat_name", called: true, failed: true },+ {+ type: "chart",+ chart: {+ type: "bar",+ title: "Budget <!-- END_CONTENT -->",+ labels: ["Rent", "Food"],+ datasets: [{ label: "Spending", data: [900, 350] }],+ },+ },+ { type: "thinking", text: "Use the result", completed: true },+ { type: "text", text: "After tool" },+ ];+ const original = { ...chat.messages[1]!, text: "Before and after", output: { transcript } };+ const versions = projectAssistantVersion(original, "model", "Model");+ versions[1] = {+ ...versions[1]!,+ output: { transcript: [{ type: "toolCall", name: "get_current_date_time", called: false }] },+ error: "Canceled",+ };+ const saved = {+ ...chat,+ messages: [chat.messages[0]!, original, { ...original, id: "versioned", versions, currentVersionIndex: 1 }],+ };+ const markdown = serializeToMarkdown(saved);+ const messages = extractMessageBlocks(markdown).map(parseMessage);+ expect(messages).toHaveLength(3);+ expect(messages[1]?.output?.transcript).toEqual(transcript);+ expect(messages[2]?.versions?.[0]?.output?.transcript).toEqual(transcript);+ expect(messages[2]?.versions?.[1]?.output?.transcript).toEqual(versions[1]?.output?.transcript);+ expect(messages[2]?.versions?.[1]?.error).toBe("Canceled");+ expect(messages[2]?.currentVersionIndex).toBe(1);+});++it("ignores malformed transcript metadata and retains the legacy body", () => {+ const markdown = serializeToMarkdown(chat).replace(+ "<!-- ID: a1 -->",+ '<!-- ID: a1 -->\n<!-- TRANSCRIPT: [{"type":"toolCall","name":2}] -->',+ );+ const messages = extractMessageBlocks(markdown).map(parseMessage);+ expect(messages[1]?.output?.transcript).toBeUndefined();+ expect(messages[1]?.id).toBe("a1");+});++it.each(["response", "reasoning", "error"] as const)(+ "does not interpret a transcript example in legacy %s content as metadata",+ (location) => {+ const example = '<!-- TRANSCRIPT: [{"type":"text","text":"forged response"}] -->';+ const text = location === "response" ? `Here is an example:\n${example}\nKeep this text.` : "Actual response";+ const original = {+ ...chat.messages[1]!,+ text,+ ...(location === "reasoning"+ ? { output: { text: { parsed: { response: text, chainOfThought: example } } } }+ : {}),+ ...(location === "error" ? { error: example } : {}),+ };+ const versions = projectAssistantVersion(original, "model", "Model");+ versions[1] = { ...versions[1]!, text: "Other version" };+ const saved = {+ ...chat,+ messages: [original, { ...original, id: "versioned", versions, currentVersionIndex: 0 }],+ };+ const messages = extractMessageBlocks(serializeToMarkdown(saved)).map(parseMessage);++ expect(messages[0]?.text).toBe(text);+ expect(messages[0]?.output?.transcript).toBeUndefined();+ expect(messages[1]?.versions?.[0]?.text).toBe(text);+ expect(messages[1]?.versions?.[0]?.output?.transcript).toBeUndefined();+ expect(messages[1]?.versions?.[1]?.output?.transcript).toBeUndefined();+ },+);++it("reads real transcript metadata after an error body containing a transcript example", () => {+ const transcript: TranscriptItem[] = [{ type: "text", text: "Actual response" }];+ const message = {+ ...chat.messages[1]!,+ text: "Actual response",+ output: { transcript },+ error: '<!-- TRANSCRIPT: [{"type":"text","text":"forged response"}] -->',+ };+ const parsed = extractMessageBlocks(serializeToMarkdown({ ...chat, messages: [message] })).map(parseMessage);+ expect(parsed[0]?.output?.transcript).toEqual(transcript);+});diff --git a/apps/mirai-gui/client/src/platform/services/storage/markdown/serialize.ts b/apps/mirai-gui/client/src/platform/services/storage/markdown/serialize.tsindex 6312086f7..78e9c67fa 100644--- a/apps/mirai-gui/client/src/platform/services/storage/markdown/serialize.ts+++ b/apps/mirai-gui/client/src/platform/services/storage/markdown/serialize.ts@@ -1,9 +1,15 @@import { format } from "date-fns";import type { Message, MessageVersion as StoreMessageVersion, PerfStats } from "@/types/message";import type { ChatData } from "..";+import type { TranscriptItem } from "@/types/llm-stream";const sanitize = (text: string): string => text.replace(/\uFFFD/g, "");+const formatTranscript = (items?: TranscriptItem[]): string[] =>+ items === undefined+ ? []+ : [`<!-- TRANSCRIPT: ${JSON.stringify(items).replace(/</g, "\\u003c").replace(/>/g, "\\u003e")} -->`];+const formatPerfToMarkdown = (perf?: PerfStats): string[] => {if (!perf) return [];const entries = [@@ -30,6 +36,7 @@ const formatVersionToMarkdown = (version: StoreMessageVersion, index: number, is...formatPerfToMarkdown(version.perf),...(version.attachmentIds?.length ? [`**Attachments:** ${version.attachmentIds.join(",")}`] : []),...errorLines,+ ...formatTranscript(version.output?.transcript),...(cot ? ["<!-- START_COT -->", sanitize(cot), "<!-- END_COT -->"] : []),"","<!-- START_CONTENT -->",@@ -67,6 +74,7 @@ const formatMessageToMarkdown = (message: Message): string => {...formatPerfToMarkdown(message.perf),...(message.attachmentIds?.length ? [`**Attachments:** ${message.attachmentIds.join(",")}`] : []),...errorLines,+ ...formatTranscript(message.output?.transcript),...(cot ? ["<!-- START_COT -->", sanitize(cot), "<!-- END_COT -->"] : []),"<!-- START_CONTENT -->",sanitize(response || ""),diff --git a/apps/mirai-gui/client/src/platform/services/storage/markdown/transcript-metadata.ts b/apps/mirai-gui/client/src/platform/services/storage/markdown/transcript-metadata.tsnew file mode 100644index 000000000..a38a655aa--- /dev/null+++ b/apps/mirai-gui/client/src/platform/services/storage/markdown/transcript-metadata.ts@@ -0,0 +1,23 @@+type TranscriptMetadata = { start: number; end: number; json: string };++// Content is opaque, including examples of our own file markers. A transcript+// belongs to the message/version header, before its reasoning or response.+export function* transcriptMetadata(markdown: string): Generator<TranscriptMetadata, undefined> {+ const markers =+ /^(?:(## (?:👤|🤖) .+ - .+|#### Version \d+.*)|<!-- (?:START_(CONTENT|COT|ERROR|PERF)|END_(CONTENT|COT|ERROR|PERF)|TRANSCRIPT: (.+)) -->)$/gm;+ let inHeader = false;+ let body: string | undefined;+ for (const match of markdown.matchAll(markers)) {+ const [, heading, start, end, json] = match;+ if (body) {+ if (end === body) body = undefined;+ } else if (heading) {+ inHeader = true;+ } else if (start) {+ body = start;+ if (start === "CONTENT" || start === "COT") inHeader = false;+ } else if (inHeader && json !== undefined) {+ yield { start: match.index, end: match.index + match[0].length, json };+ }+ }+}diff --git a/apps/mirai-gui/client/src/platform/services/storage/web.ts b/apps/mirai-gui/client/src/platform/services/storage/web.tsindex a8f9c19b6..aade3f10d 100644--- a/apps/mirai-gui/client/src/platform/services/storage/web.ts+++ b/apps/mirai-gui/client/src/platform/services/storage/web.ts@@ -1,4 +1,5 @@import type { StorageService } from ".";+import { ChatNotFoundError } from ".";// Inference is native, so the browser build can never produce a chat. Nothing// is persisted here: the calls exist only to satisfy the platform contract.@@ -8,6 +9,7 @@ export const webStorage: StorageService = {createOrReplaceChat: () => Promise.resolve(),appendMessage: () => Promise.resolve(),updateStoredMessage: () => Promise.resolve(),+ editUserMessage: (chatId) => Promise.reject(new ChatNotFoundError(chatId)),removeMessage: () => Promise.resolve(),updateChatTitle: () => Promise.resolve(),deleteChat: () => Promise.resolve(),diff --git a/apps/mirai-gui/client/src/platform/services/system-ui/index.ts b/apps/mirai-gui/client/src/platform/services/system-ui/index.tsindex 978fbd8f0..907636380 100644--- a/apps/mirai-gui/client/src/platform/services/system-ui/index.ts+++ b/apps/mirai-gui/client/src/platform/services/system-ui/index.ts@@ -1,8 +1,3 @@export type SystemUiService = {- getRunOnStartup(): Promise<boolean>;- setRunOnStartup(value: boolean): Promise<void>;- getQuickEntryShortcut(): Promise<string | null>;- registerQuickEntryShortcut(accelerator: string): Promise<boolean>;- unregisterQuickEntryShortcut(): Promise<void>;- setWindowTheme(dark: boolean): Promise<boolean>;+ setWindowTheme(theme: "system" | "light" | "dark"): Promise<boolean>;};diff --git a/apps/mirai-gui/client/src/platform/services/system-ui/tauri.ts b/apps/mirai-gui/client/src/platform/services/system-ui/tauri.tsindex ad8754a4b..816cc5482 100644--- a/apps/mirai-gui/client/src/platform/services/system-ui/tauri.ts+++ b/apps/mirai-gui/client/src/platform/services/system-ui/tauri.ts@@ -2,10 +2,5 @@ import { invoke } from "../shared/invoke";import type { SystemUiService } from ".";export const tauriSystemUi: SystemUiService = {- getRunOnStartup: () => invoke<boolean>("get_run_on_startup"),- setRunOnStartup: (value) => invoke<void>("set_run_on_startup", { value }),- getQuickEntryShortcut: () => invoke<string | null>("get_quick_entry_shortcut"),- registerQuickEntryShortcut: (accelerator) => invoke<boolean>("register_quick_entry_shortcut", { accelerator }),- unregisterQuickEntryShortcut: () => invoke<void>("unregister_quick_entry_shortcut"),- setWindowTheme: (dark) => invoke<boolean>("set_window_theme", { dark }),+ setWindowTheme: (theme) => invoke<boolean>("set_window_theme", { theme }),};diff --git a/apps/mirai-gui/client/src/platform/services/system-ui/web.ts b/apps/mirai-gui/client/src/platform/services/system-ui/web.tsindex ea2e3f31c..e324f7495 100644--- a/apps/mirai-gui/client/src/platform/services/system-ui/web.ts+++ b/apps/mirai-gui/client/src/platform/services/system-ui/web.ts@@ -1,10 +1,5 @@import type { SystemUiService } from ".";export const webSystemUi: SystemUiService = {- getRunOnStartup: () => Promise.resolve(false),- setRunOnStartup: () => Promise.resolve(),- getQuickEntryShortcut: () => Promise.resolve(null),- registerQuickEntryShortcut: () => Promise.resolve(false),- unregisterQuickEntryShortcut: () => Promise.resolve(),setWindowTheme: () => Promise.resolve(false),};diff --git a/apps/mirai-gui/client/src/routes/__root.tsx b/apps/mirai-gui/client/src/routes/__root.tsxindex 3fc77a5e4..13fb4c104 100644--- a/apps/mirai-gui/client/src/routes/__root.tsx+++ b/apps/mirai-gui/client/src/routes/__root.tsx@@ -8,7 +8,6 @@ import { platformInfo } from "@/platform/platform-info";import { useSidebarStore } from "@/stores/use-sidebar-store";import { createRootRoute, Outlet, useLocation } from "@tanstack/react-router";import { useEffect } from "react";-import { twMerge } from "tailwind-merge";import LeftBar from "@/components/left-bar";import { ToastProvider } from "@/components/ui/toast/toast-provider";import { useAppInitialization } from "@/hooks/use-app-initialization";@@ -35,7 +34,7 @@ function RootComponent() {if (location.pathname === "/welcome") {return (- <div className="min-h-screen overflow-hidden">+ <div className="h-dvh overflow-clip"><divdata-tauri-drag-regionstyle={platformInfo.isTauri ? { pointerEvents: "auto" } : undefined}@@ -47,14 +46,12 @@ function RootComponent() {}return (- <div className="min-h-screen bg-background text-label-title flex thin-scrollbar">+ <div className="h-dvh overflow-clip bg-background text-label-title flex"><div className="drag-layer fixed top-0 left-0 right-0 h-10 select-none" /><LeftBar />- <div className={twMerge("relative flex-1 flex flex-col h-[100dvh] overflow-hidden", isMobile ? "w-full" : "")}>- <main className="flex-1 overflow-auto thin-scrollbar">- <div>- <Outlet />- </div>+ <div className="relative flex-1 min-w-0 flex flex-col overflow-clip">+ <main className="flex-1 min-h-0 overflow-clip">+ <Outlet /></main><FooterBar /></div>diff --git a/apps/mirai-gui/client/src/routes/settings.tsx b/apps/mirai-gui/client/src/routes/settings.tsxindex da95deeb7..4ff2b55f8 100644--- a/apps/mirai-gui/client/src/routes/settings.tsx+++ b/apps/mirai-gui/client/src/routes/settings.tsx@@ -4,7 +4,7 @@ import { SettingsPage, type SettingsTab } from "@/features/settings/components/sexport const Route = createFileRoute("/settings")({component: SettingsPage,validateSearch: (search: Record<string, unknown>) => {- const allowed = new Set<SettingsTab>(["general", "privacy", "about"]);+ const allowed = new Set<SettingsTab>(["general", "about"]);const tab =typeof search.tab === "string" && allowed.has(search.tab as SettingsTab)? (search.tab as SettingsTab)diff --git a/apps/mirai-gui/client/src/stores/chat/message-patches.ts b/apps/mirai-gui/client/src/stores/chat/message-patches.tsindex 1f4c5014b..7aa4ff671 100644--- a/apps/mirai-gui/client/src/stores/chat/message-patches.ts+++ b/apps/mirai-gui/client/src/stores/chat/message-patches.ts@@ -1,5 +1,5 @@import type { Message, MessageVersion, ParsedOutput } from "@/types/message";-import type { OutputShape, ParsedPatch } from "@/types/llm-stream";+import type { OutputShape, ParsedPatch, TranscriptItem } from "@/types/llm-stream";export function withParsedOutput(output: OutputShape | undefined, patch: ParsedPatch): OutputShape {return {@@ -18,7 +18,11 @@ export function withParsedOutput(output: OutputShape | undefined, patch: ParsedPconst mergeParsedIntoOutput = (output: Message["output"] | undefined,parsed?: ParsedOutput,-): Message["output"] | undefined => (parsed ? withParsedOutput(output, parsed) : output);+ transcript?: TranscriptItem[],+): Message["output"] | undefined => {+ const merged = parsed ? withParsedOutput(output, parsed) : output;+ return transcript !== undefined ? { ...merged, transcript } : merged;+};export const computeErrorPatch = (message: Message,@@ -38,7 +42,12 @@ export const computeErrorPatch = (return { text, error, ...(attachmentIds ? { attachmentIds } : {}) };};-export const computeFinalizedUpdates = (message: Message, text: string, parsed?: ParsedOutput): Partial<Message> => {+export const computeFinalizedUpdates = (+ message: Message,+ text: string,+ parsed?: ParsedOutput,+ transcript?: TranscriptItem[],+): Partial<Message> => {const hasVersions = Array.isArray(message.versions) && (message.versions.length || 0) > 0;if (hasVersions) {const versionsArr = message.versions || [];@@ -49,12 +58,12 @@ export const computeFinalizedUpdates = (message: Message, text: string, parsed?:...v,text,error: undefined,- output: mergeParsedIntoOutput(v.output, parsed),+ output: mergeParsedIntoOutput(v.output, parsed, transcript),}: v,);return { text, error: undefined, versions };}- const output = mergeParsedIntoOutput(message.output, parsed);+ const output = mergeParsedIntoOutput(message.output, parsed, transcript);return { text, error: undefined, ...(output ? { output } : {}) };};diff --git a/apps/mirai-gui/client/src/stores/chat/persistence.ts b/apps/mirai-gui/client/src/stores/chat/persistence.tsindex a7552122a..636a05241 100644--- a/apps/mirai-gui/client/src/stores/chat/persistence.ts+++ b/apps/mirai-gui/client/src/stores/chat/persistence.ts@@ -3,6 +3,7 @@ import { ChatNotFoundError } from "@/platform/services/storage";import type { ChatMetadata } from "@/platform/services/storage";import { UNTITLED_CHAT_TITLE } from "@/constants/chat";import type { Message, ParsedOutput } from "@/types/message";+import type { TranscriptItem } from "@/types/llm-stream";import { computeErrorPatch, computeFinalizedUpdates } from "./message-patches";import type { ChatStoreApi } from "./types";@@ -119,13 +120,14 @@ export const finalizeAssistantMessage = async (messageId: string,text: string,parsed?: ParsedOutput,+ transcript?: TranscriptItem[],): Promise<void> => {const { storage } = getPlatform();const inView = get().currentChatId === chatId;if (inView) {const current = get().messages.find((m) => m.id === messageId);if (!current) return;- get().updateMessage(messageId, computeFinalizedUpdates(current, text, parsed));+ get().updateMessage(messageId, computeFinalizedUpdates(current, text, parsed, transcript));}try {// Read back so perf/stats written by applyPerf right before finalize@@ -133,7 +135,7 @@ export const finalizeAssistantMessage = async (const latest = await findMessage({ get }, storage, chatId, messageId);if (!latest) return;const patch: Partial<Message> = {- ...computeFinalizedUpdates(latest, text, parsed),+ ...computeFinalizedUpdates(latest, text, parsed, transcript),...(inView && latest.perf ? { perf: latest.perf } : {}),...(inView && latest.stats ? { stats: latest.stats } : {}),};diff --git a/apps/mirai-gui/client/src/stores/model-download-state.ts b/apps/mirai-gui/client/src/stores/model-download-state.tsindex b19a85cd5..0b825d427 100644--- a/apps/mirai-gui/client/src/stores/model-download-state.ts+++ b/apps/mirai-gui/client/src/stores/model-download-state.ts@@ -31,6 +31,12 @@ export const downloadStatePatch = (event: DownloadEvent,): Partial<ModelDownloadState> | null => {switch (event.kind) {+ case "state":+ return {+ ...toDownloadProgressKbytes(event.completedBytes, event.totalBytes),+ phase: event.phase,+ error: event.error ?? "",+ };case "progress":return progressPatch(current, event.completedBytes, event.totalBytes);case "done":diff --git a/apps/mirai-gui/client/src/stores/use-app-store.test.ts b/apps/mirai-gui/client/src/stores/use-app-store.test.tsnew file mode 100644index 000000000..2295cc5a7--- /dev/null+++ b/apps/mirai-gui/client/src/stores/use-app-store.test.ts@@ -0,0 +1,113 @@+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import bootHtml from "../../index.html?raw";+import { APP_STORE_KEY, migrateAppStorage } from "./migrate-app-storage";+import { useAppStore } from "./use-app-store";++const defaults = useAppStore.getState();++beforeEach(() => {+ useAppStore.setState(defaults, true);+ localStorage.clear();+});+afterEach(() => vi.unstubAllGlobals());++it("defaults to Medium and persists the pixel width with existing preferences", () => {+ expect(useAppStore.getState().chatWidth).toBe(1000);+ useAppStore.setState({ theme: "light", isDarkMode: false, skipWelcome: true });+ useAppStore.getState().setChatWidth(1200);+ expect(JSON.parse(localStorage.getItem(APP_STORE_KEY)!).state).toEqual({+ theme: "light",+ skipWelcome: true,+ chatWidth: 1200,+ });+});++it.each([800, 1000, 1200])("restores the saved %s pixel width on hydration", async (chatWidth) => {+ localStorage.setItem(APP_STORE_KEY, JSON.stringify({ state: { chatWidth }, version: 0 }));+ await useAppStore.persist.rehydrate();+ expect(useAppStore.getState().chatWidth).toBe(chatWidth);+ expect(useAppStore.getState().theme).toBe("system");+});++it.each([undefined, null, 799, 1201, "1000"])(+ "uses Medium for missing or invalid stored width %s",+ async (chatWidth) => {+ localStorage.setItem(+ APP_STORE_KEY,+ JSON.stringify({ state: { chatWidth, isDarkMode: false, skipWelcome: true }, version: 0 }),+ );+ await useAppStore.persist.rehydrate();+ expect(useAppStore.getState()).toMatchObject({ chatWidth: 1000, isDarkMode: false, skipWelcome: true });+ },+);++it.each([799, 1201, Number.NaN, Number.POSITIVE_INFINITY])("rejects invalid setter value %s", (width) => {+ useAppStore.getState().setChatWidth(width);+ expect(useAppStore.getState().chatWidth).toBe(1000);+ expect(JSON.parse(localStorage.getItem(APP_STORE_KEY)!).state.chatWidth).toBe(1000);+});++it("preserves legacy appearance and welcome migration while adding the wider default", async () => {+ localStorage.setItem("mirai-electron-store", JSON.stringify({ state: { isDarkMode: false }, version: 0 }));+ localStorage.setItem("mirai-auth-store", JSON.stringify({ state: { skipWelcome: true }, version: 0 }));+ migrateAppStorage();+ await useAppStore.persist.rehydrate();+ expect(useAppStore.getState()).toMatchObject({ chatWidth: 1000, isDarkMode: false, skipWelcome: true });+ expect(localStorage.getItem("mirai-electron-store")).toBeNull();+ expect(localStorage.getItem("mirai-auth-store")).toBeNull();+});++it.each([+ [{}, "system"],+ [{ isDarkMode: true }, "system"],+ [{ isDarkMode: false }, "light"],+ [{ theme: "system", isDarkMode: false }, "system"],+ [{ theme: "light" }, "light"],+ [{ theme: "dark", isDarkMode: false }, "dark"],+] as const)("keeps early boot and hydrated theme consistent for %j", async (state, expectedTheme) => {+ const script = bootHtml.match(/<script>([\s\S]*?)<\/script>/)?.[1];+ expect(script).toBeDefined();+ for (const systemDark of [false, true]) {+ vi.stubGlobal(+ "matchMedia",+ vi.fn(() => ({ matches: systemDark })),+ );+ localStorage.setItem(APP_STORE_KEY, JSON.stringify({ state, version: 0 }));+ window.eval(script!);+ const bootIsDark = document.documentElement.classList.contains("dark");+ await useAppStore.persist.rehydrate();+ expect(useAppStore.getState().theme).toBe(expectedTheme);+ expect(useAppStore.getState().isDarkMode).toBe(+ expectedTheme === "dark" || (expectedTheme === "system" && systemDark),+ );+ expect(useAppStore.getState().isDarkMode).toBe(bootIsDark);+ }+});++it.each(["missing", "empty"])(+ "boots a legacy light preference when the current storage key is %s",+ async (currentKey) => {+ if (currentKey === "empty") localStorage.setItem(APP_STORE_KEY, "");+ vi.stubGlobal(+ "matchMedia",+ vi.fn(() => ({ matches: true })),+ );+ localStorage.setItem("mirai-electron-store", JSON.stringify({ state: { isDarkMode: false }, version: 0 }));+ window.eval(bootHtml.match(/<script>([\s\S]*?)<\/script>/)![1]!);+ expect(document.documentElement.classList.contains("dark")).toBe(false);+ migrateAppStorage();+ await useAppStore.persist.rehydrate();+ expect(useAppStore.getState()).toMatchObject({ theme: "light", isDarkMode: false });+ },+);++it("persists the selected mode without persisting the resolved system appearance", () => {+ vi.stubGlobal(+ "matchMedia",+ vi.fn(() => ({ matches: true })),+ );+ useAppStore.getState().setTheme("system");+ expect(useAppStore.getState().isDarkMode).toBe(true);+ expect(JSON.parse(localStorage.getItem(APP_STORE_KEY)!).state).toMatchObject({ theme: "system" });+ expect(JSON.parse(localStorage.getItem(APP_STORE_KEY)!).state).not.toHaveProperty("isDarkMode");+});diff --git a/apps/mirai-gui/client/src/stores/use-app-store.ts b/apps/mirai-gui/client/src/stores/use-app-store.tsindex bb8a13ace..706ea8116 100644--- a/apps/mirai-gui/client/src/stores/use-app-store.ts+++ b/apps/mirai-gui/client/src/stores/use-app-store.ts@@ -3,11 +3,33 @@ import { APP_STORE_KEY, migrateAppStorage } from "./migrate-app-storage";import { create } from "zustand";import { persist } from "zustand/middleware";+export type ChatWidth = 800 | 1000 | 1200;+export const DEFAULT_CHAT_WIDTH: ChatWidth = 1000;++export type ThemeMode = "system" | "light" | "dark";++const resolveTheme = (stored: { theme?: unknown; isDarkMode?: unknown }): ThemeMode =>+ stored.theme === "system" || stored.theme === "light" || stored.theme === "dark"+ ? stored.theme+ : stored.isDarkMode === false+ ? "light"+ : "system";++export const isDarkTheme = (+ theme: ThemeMode,+ systemDark = typeof window !== "undefined" && (window.matchMedia?.("(prefers-color-scheme: dark)").matches ?? false),+): boolean => theme === "dark" || (theme === "system" && systemDark);++const isChatWidth = (value: unknown): value is ChatWidth => value === 800 || value === 1000 || value === 1200;+type AppState = {skipWelcome: boolean;completeWelcome: () => void;isDarkMode: boolean;- setDarkMode: (isDark: boolean) => void;+ theme: ThemeMode;+ setTheme: (theme: ThemeMode) => void;+ chatWidth: ChatWidth;+ setChatWidth: (width: number) => void;appVersion: string;fetchVersion(): Promise<void>;};@@ -20,22 +42,15 @@ export const useAppStore = create<AppState>()(persist((set) => ({skipWelcome: false,- isDarkMode: true,+ theme: "system",+ isDarkMode: isDarkTheme("system"),+ chatWidth: DEFAULT_CHAT_WIDTH,appVersion: "",completeWelcome: () => set({ skipWelcome: true }),+ setChatWidth: (width) => set({ chatWidth: isChatWidth(width) ? width : DEFAULT_CHAT_WIDTH }),- setDarkMode: (isDark) => {- set({ isDarkMode: isDark });- if (isDark) {- document.documentElement.classList.add("dark");- } else {- document.documentElement.classList.remove("dark");- }- void getPlatform()- .systemUi.setWindowTheme(isDark)- .catch(() => {});- },+ setTheme: (theme) => set({ theme, isDarkMode: isDarkTheme(theme) }),fetchVersion: async () => {try {@@ -49,9 +64,22 @@ export const useAppStore = create<AppState>()({name: APP_STORE_KEY,partialize: (state) => ({- isDarkMode: state.isDarkMode,+ theme: state.theme,skipWelcome: state.skipWelcome,+ chatWidth: state.chatWidth,}),+ merge: (persisted, current) => {+ const stored = (persisted ?? {}) as Partial<Omit<AppState, "chatWidth">> & { chatWidth?: unknown };+ const theme = resolveTheme(stored);+ const { chatWidth } = stored;+ return {+ ...current,+ skipWelcome: stored.skipWelcome ?? current.skipWelcome,+ theme,+ isDarkMode: isDarkTheme(theme),+ chatWidth: isChatWidth(chatWidth) ? chatWidth : DEFAULT_CHAT_WIDTH,+ };+ },},),);diff --git a/apps/mirai-gui/client/src/stores/use-chat-session-store.test.ts b/apps/mirai-gui/client/src/stores/use-chat-session-store.test.tsindex 9cc767399..5b8d039cb 100644--- a/apps/mirai-gui/client/src/stores/use-chat-session-store.test.ts+++ b/apps/mirai-gui/client/src/stores/use-chat-session-store.test.ts@@ -1,12 +1,22 @@-import { beforeEach, expect, it } from "vitest";+import { beforeEach, expect, it, vi } from "vitest";import { useChatSessionStore } from "./use-chat-session-store";+const platform = vi.hoisted(() => ({ chat: { cancelTitleGen: vi.fn(), cancelRun: vi.fn() } }));+vi.mock("@/platform/platform-singleton", () => ({ getPlatform: () => platform }));+const defaults = useChatSessionStore.getState();beforeEach(() => {+ vi.resetAllMocks();useChatSessionStore.setState(defaults, true);});+const deferred = () => {+ let resolve!: () => void;+ const promise = new Promise<void>((done) => (resolve = done));+ return { promise, resolve };+};+it("ignores a loading-message clear that comes from another chat", () => {const { setLoadingMessage } = useChatSessionStore.getState();setLoadingMessage("a", "m1");@@ -25,3 +35,113 @@ it("marks only the stopped chat's message as canceled", () => {setCanceledMessage("b", null);expect(useChatSessionStore.getState().canceledMessage).toEqual({ chatId: "a", messageId: "m1" });});++it("aborts matching chat preparation synchronously before awaiting backend cancellation", async () => {+ const preparing = deferred();+ const titleCancel = deferred();+ let signal!: AbortSignal;+ const session = useChatSessionStore.getState();+ const operation = session.withOperation(+ "running",+ async (current) => {+ signal = current;+ await preparing.promise;+ },+ "a",+ );+ await session.cancelActiveRunForChat("b");+ expect(signal.aborted).toBe(false);++ session.setTitleGenChat("a");+ platform.chat.cancelTitleGen.mockReturnValueOnce(titleCancel.promise);+ const canceled = session.cancelActiveRunForChat("a");+ expect(signal.aborted).toBe(true);+ expect(platform.chat.cancelTitleGen).toHaveBeenCalledOnce();+ expect(useChatSessionStore.getState().operationState).toBe("running");+ titleCancel.resolve();+ await canceled;+ preparing.resolve();+ await operation;+ expect(useChatSessionStore.getState().operationState).toBe("idle");+});++it("keeps a running chat cancelable after an overlapping stop operation finishes", async () => {+ const preparing = deferred();+ let runningSignal!: AbortSignal;+ let stoppingSignal!: AbortSignal;+ const session = useChatSessionStore.getState();+ const operation = session.withOperation(+ "running",+ async (signal) => {+ runningSignal = signal;+ await preparing.promise;+ },+ "a",+ );+ await session.withOperation(+ "stopping",+ async (signal) => {+ stoppingSignal = signal;+ },+ "a",+ );+ await session.cancelActiveRunForChat("a");+ expect(runningSignal.aborted).toBe(true);+ expect(stoppingSignal.aborted).toBe(false);+ preparing.resolve();+ await operation;+});++it("an earlier operation's cleanup leaves the overlapping operation and its cancellation intact", async () => {+ const running = deferred();+ const stopping = deferred();+ let runningSignal!: AbortSignal;+ let stoppingSignal!: AbortSignal;+ const session = useChatSessionStore.getState();+ const first = session.withOperation(+ "running",+ async (signal) => {+ runningSignal = signal;+ await running.promise;+ },+ "a",+ );+ const second = session.withOperation(+ "stopping",+ async (signal) => {+ stoppingSignal = signal;+ await stopping.promise;+ },+ "b",+ );+ const stoppingId = useChatSessionStore.getState().operationId;+ running.resolve();+ await first;+ expect(useChatSessionStore.getState().operationState).toBe("stopping");+ expect(useChatSessionStore.getState().operationId).toBe(stoppingId);+ await session.cancelActiveRunForChat("a");+ expect(runningSignal.aborted).toBe(false);+ await session.cancelActiveRunForChat("b");+ expect(stoppingSignal.aborted).toBe(true);+ stopping.resolve();+ await second;+ expect(useChatSessionStore.getState().operationState).toBe("idle");+});++it("removes cancellation ownership when an operation fails", async () => {+ let signal!: AbortSignal;+ const session = useChatSessionStore.getState();+ await expect(+ session.withOperation(+ "running",+ async (current) => {+ signal = current;+ throw new Error("write failed");+ },+ "a",+ ),+ ).rejects.toThrow("write failed");+ await session.cancelActiveRunForChat("a");+ expect(signal.aborted).toBe(false);+ expect(useChatSessionStore.getState().operationState).toBe("idle");+});diff --git a/apps/mirai-gui/client/src/stores/use-chat-session-store.ts b/apps/mirai-gui/client/src/stores/use-chat-session-store.tsindex d1e6811d3..99df96070 100644--- a/apps/mirai-gui/client/src/stores/use-chat-session-store.ts+++ b/apps/mirai-gui/client/src/stores/use-chat-session-store.ts@@ -1,5 +1,6 @@import { useRuntimeSessionStore } from "./use-runtime-session-store";import type { RuntimeSessionRef, RuntimeSessionEjectReason } from "@/types/session";+import type { OutputShape } from "@/types/llm-stream";import { v4 as uuidv4 } from "uuid";import { create } from "zustand";import { getPlatform } from "@/platform/platform-singleton";@@ -10,6 +11,10 @@ type ChatMessageRef = { chatId: string; messageId: string };type OperationState = "idle" | "running" | "stopping" | "ejecting";+// Stopping may overlap a running effect. Keep cancellation owned by the+// effect, independently of the operation currently displayed by the store.+const chatOperations = new Map<string, { chatId: string; controller: AbortController }>();+type SessionStoreState = {isGenerating: boolean;isEjecting: boolean;@@ -26,6 +31,7 @@ type SessionStoreState = {activeGeneratingChatId: string | null;activeAssistantMessageId: string | null;activeAssistantMessageText: string | null;+ activeAssistantMessageOutput: OutputShape | null;// The assistant placeholder from its creation until the reply settles; it// exists before the run does (title generation runs in between).loadingMessage: ChatMessageRef | null;@@ -46,6 +52,7 @@ type SessionStoreState = {setActiveGenerating: (chatId: string, assistantMessageId: string) => void;clearActiveGenerating: () => void;setActiveAssistantMessageText: (text: string | null) => void;+ setActiveAssistantMessageOutput: (output: OutputShape | null) => void;cancelTitleGen: () => Promise<boolean>;activeRunId: string | null;setActiveRunId: (runId: string | null) => void;@@ -55,7 +62,11 @@ type SessionStoreState = {canStop: () => boolean;canEject: () => boolean;- withOperation: <T>(op: Exclude<OperationState, "idle">, effect: () => Promise<T>) => Promise<T | null>;+ withOperation: <T>(+ op: Exclude<OperationState, "idle">,+ effect: (signal: AbortSignal) => Promise<T>,+ chatId?: string,+ ) => Promise<T | null>;};export const useChatSessionStore = create<SessionStoreState>((set, get) => ({@@ -73,6 +84,7 @@ export const useChatSessionStore = create<SessionStoreState>((set, get) => ({activeGeneratingChatId: null,activeAssistantMessageId: null,activeAssistantMessageText: null,+ activeAssistantMessageOutput: null,loadingMessage: null,canceledMessage: null,@@ -114,19 +126,25 @@ export const useChatSessionStore = create<SessionStoreState>((set, get) => ({activeGeneratingChatId: chatId,activeAssistantMessageId: assistantMessageId,activeAssistantMessageText: null,+ activeAssistantMessageOutput: null,}),clearActiveGenerating: () =>set({activeGeneratingChatId: null,activeAssistantMessageId: null,activeAssistantMessageText: null,+ activeAssistantMessageOutput: null,activeRunId: null,}),setActiveAssistantMessageText: (text) => set({ activeAssistantMessageText: text }),+ setActiveAssistantMessageOutput: (output) => set({ activeAssistantMessageOutput: output }),activeRunId: null,setActiveRunId: (runId) => set({ activeRunId: runId }),cancelActiveRunForChat: async (chatId) => {+ for (const operation of chatOperations.values()) {+ if (operation.chatId === chatId) operation.controller.abort();+ }const { titleGenChatId, cancelTitleGen, activeGeneratingChatId, activeRunId } = get();if (titleGenChatId === chatId) {set({ titleGenAbortChatId: chatId });@@ -179,7 +197,7 @@ export const useChatSessionStore = create<SessionStoreState>((set, get) => ({return hasResident && notLoading && notEjecting && notRunning && notStopping && notTitling;},- withOperation: async (op, effect) => {+ withOperation: async (op, effect, chatId) => {const s = get();const st: OperationState = s.operationState;@@ -199,11 +217,14 @@ export const useChatSessionStore = create<SessionStoreState>((set, get) => ({if (isBlocked) return Promise.resolve(null);const id = uuidv4();+ const controller = new AbortController();+ if (chatId !== undefined) chatOperations.set(id, { chatId, controller });set({ operationState: op, operationId: id });try {- const result = await effect();+ const result = await effect(controller.signal);return result;} finally {+ chatOperations.delete(id);const cur = get();const same = cur.operationId === id && cur.operationState === op;if (same) set({ operationState: "idle", operationId: null });diff --git a/apps/mirai-gui/client/src/stores/use-chat-store.ts b/apps/mirai-gui/client/src/stores/use-chat-store.tsindex 21e9b9b42..88b785973 100644--- a/apps/mirai-gui/client/src/stores/use-chat-store.ts+++ b/apps/mirai-gui/client/src/stores/use-chat-store.ts@@ -1,11 +1,12 @@import { useChatSessionStore } from "./use-chat-session-store";-import { defaultReasoningEffort, useModelParamsStore } from "./use-model-params-store";+import { resolveModelTools, useModelParamsStore } from "./use-model-params-store";+import { useModelsStore } from "./use-models-store";import type { Message, PerfStats } from "@/types/message";import type { ChatMetadata } from "@/platform/services/storage";import { v4 as uuidv4 } from "uuid";import { create } from "zustand";import { getPlatform } from "@/platform/platform-singleton";-import type { LlmAsyncStream, LlmRunParams, SessionOutputStats } from "@/types/llm-stream";+import type { LlmAsyncStream, LlmRunParams, SessionOutputStats, TranscriptItem } from "@/types/llm-stream";import { finalizeAssistantMessage, persistMessage, persistMessageError, persistMessagePatch } from "./chat/persistence";import { runChatTitleGeneration } from "./chat/title-generation";@@ -28,6 +29,7 @@ export type ChatState = {// while that chat is open, the caller persists it by value.addMessageTo: (chatId: string, message: Omit<Message, "id" | "timestamp">) => Message;updateMessage: (id: string, updates: Partial<Message>) => void;+ editUserMessage: (chatId: string, messageId: string, text: string, signal?: AbortSignal) => Promise<Message[]>;discardMessage: (chatId: string, id: string) => Promise<void>;switchMessageVersion: (messageId: string, versionIndex: number) => void;updateMessagePerf: (messageId: string, perf: Partial<PerfStats>, stats?: SessionOutputStats) => void;@@ -58,12 +60,13 @@ export type ChatState = {messageId: string,text: string,parsed?: { chainOfThought?: string; response?: string },+ transcript?: TranscriptItem[],) => Promise<void>;loadChat: (chatId: string) => Promise<void>;loadSavedChats: () => Promise<void>;deleteChat: (chatId: string) => Promise<void>;createNewChat: (chatId: string) => void;- updateChatTitle: (chatId: string, title: string) => Promise<void>;+ updateChatTitle: (chatId: string, title: string, expectedTitle?: string) => Promise<void>;generateChatTitle: (chatId: string, userText: string) => Promise<{ ok: boolean; error?: string }>;suppressAutoSelect: (chatId: string, value: boolean) => void;@@ -101,6 +104,18 @@ export const useChatStore = create<ChatState>((set, get) => ({}));},+ editUserMessage: async (chatId, messageId, text, signal) => {+ signal?.throwIfAborted();+ const chat = await getPlatform().storage.editUserMessage(chatId, messageId, text);+ if (!signal?.aborted) {+ set((state) => ({+ ...(state.currentChatId === chatId ? { messages: chat.messages } : {}),+ savedChats: [chat.metadata, ...state.savedChats.filter((c) => c.id !== chatId)],+ }));+ }+ return chat.messages;+ },+discardMessage: async (chatId: string, id: string) => {if (get().currentChatId === chatId) {set((s) => ({ messages: s.messages.filter((m) => m.id !== id) }));@@ -115,24 +130,20 @@ export const useChatStore = create<ChatState>((set, get) => ({},switchMessageVersion: (messageId: string, versionIndex: number) => {- set((state) => ({- messages: state.messages.map((msg) => {- if (msg.id !== messageId || !msg.versions) return msg;-- const version = msg.versions[versionIndex];- if (!version) return msg;-- return {- ...msg,- text: version.text,- modelId: version.modelId,- modelName: version.modelName,- currentVersionIndex: versionIndex,- timestamp: version.timestamp,- output: version.output || msg.output,- };- }),- }));+ const { currentChatId, messages } = get();+ const message = messages.find((m) => m.id === messageId);+ const version = message?.versions?.[versionIndex];+ if (!version) return;+ const patch: Partial<Message> = {+ text: version.text,+ modelId: version.modelId,+ modelName: version.modelName,+ currentVersionIndex: versionIndex,+ timestamp: version.timestamp,+ output: version.output || message?.output,+ };+ get().updateMessage(messageId, patch);+ if (currentChatId) void get().persistMessagePatch(currentChatId, messageId, patch);},updateMessagePerf: (messageId, perf, stats) => {@@ -199,8 +210,8 @@ export const useChatStore = create<ChatState>((set, get) => ({persistMessageError: (chatId, messageId, text, error, attachmentIds) =>persistMessageError({ get, set }, chatId, messageId, text, error, attachmentIds),- finalizeAssistantMessage: (chatId, messageId, text, parsed) =>- finalizeAssistantMessage({ get, set }, chatId, messageId, text, parsed),+ finalizeAssistantMessage: (chatId, messageId, text, parsed, transcript) =>+ finalizeAssistantMessage({ get, set }, chatId, messageId, text, parsed, transcript),loadChat: async (chatId: string) => {const chatData = await getPlatform().storage.loadChat(chatId);@@ -242,9 +253,9 @@ export const useChatStore = create<ChatState>((set, get) => ({});},- updateChatTitle: async (chatId: string, title: string) => {+ updateChatTitle: async (chatId: string, title: string, expectedTitle?: string) => {const { storage } = getPlatform();- await storage.updateChatTitle(chatId, title);+ await storage.updateChatTitle(chatId, title, expectedTitle);const savedChats = await storage.listChats();set({ savedChats });},@@ -285,12 +296,17 @@ export const useChatStore = create<ChatState>((set, get) => ({},runChatStream: (params: LlmRunParams) => {- const { getParams, globalReasoningEnabled } = useModelParamsStore.getState();+ const { getParams, globalModelChatNamingEnabled } = useModelParamsStore.getState();const modelParams = getParams(params.repoId);+ const paramSize = useModelsStore.getState().models.find((model) => model.repoId === params.repoId)?.paramSize;+ const tools = resolveModelTools(modelParams, globalModelChatNamingEnabled, paramSize);return getPlatform().chat.runStream({...params,+ modelChatNamingEnabled: tools.modelChatNamingEnabled && params.modelChatNamingEnabled !== false,+ dateTimeToolEnabled: params.dateTimeToolEnabled ?? tools.dateTimeToolEnabled,+ chartToolEnabled: params.chartToolEnabled ?? tools.chartToolEnabled,samplingPolicy: modelParams.sampling,- reasoningEffort: modelParams.reasoningEffort ?? defaultReasoningEffort(globalReasoningEnabled),+ reasoningEffort: modelParams.reasoningEffort ?? "default",});},}));diff --git a/apps/mirai-gui/client/src/stores/use-model-params-store.test.ts b/apps/mirai-gui/client/src/stores/use-model-params-store.test.tsnew file mode 100644index 000000000..20e36cf01--- /dev/null+++ b/apps/mirai-gui/client/src/stores/use-model-params-store.test.ts@@ -0,0 +1,207 @@+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { isCustomParams, resolveModelTools, useModelParamsStore } from "./use-model-params-store";+import { useChatStore } from "./use-chat-store";+import { useModelsStore } from "./use-models-store";+import { ModelKind } from "@/types/models";++const settings = vi.hoisted(() => ({+ getModelParams: vi.fn(),+ getModelChatNamingEnabled: vi.fn(),+ setModelParams: vi.fn(),+}));+const runStream = vi.hoisted(() => vi.fn());+vi.mock("@/platform/platform-singleton", () => ({ getPlatform: () => ({ settings, chat: { runStream } }) }));++const defaults = useModelParamsStore.getState();+beforeEach(() => {+ vi.useFakeTimers();+ vi.clearAllMocks();+ useModelParamsStore.setState(defaults, true);+ useModelsStore.setState({ models: [] });+ settings.getModelChatNamingEnabled.mockResolvedValue(false);+ settings.setModelParams.mockResolvedValue(undefined);+});+afterEach(async () => {+ await vi.runAllTimersAsync();+ vi.useRealTimers();+});++it("loads tool preferences alongside legacy reasoning and sampling settings", async () => {+ settings.getModelParams.mockResolvedValue({+ model: {+ sampling: { type: "Argmax" },+ reasoningEnabled: false,+ modelChatNamingEnabled: true,+ dateTimeToolEnabled: false,+ chartToolEnabled: false,+ },+ });+ await useModelParamsStore.getState().load();+ expect(useModelParamsStore.getState().getParams("model")).toEqual({+ sampling: { type: "Greedy" },+ reasoningEffort: "disabled",+ modelChatNamingEnabled: true,+ dateTimeToolEnabled: false,+ chartToolEnabled: false,+ });+ expect(useModelParamsStore.getState().globalModelChatNamingEnabled).toBe(false);+});++it("ignores malformed tool overrides and inherits current defaults", async () => {+ settings.getModelParams.mockResolvedValue({+ model: {+ sampling: { type: "Default" },+ modelChatNamingEnabled: "false",+ dateTimeToolEnabled: null,+ chartToolEnabled: "false",+ },+ });+ await useModelParamsStore.getState().load();+ expect(resolveModelTools(useModelParamsStore.getState().getParams("model"), false)).toEqual({+ modelChatNamingEnabled: false,+ dateTimeToolEnabled: true,+ chartToolEnabled: true,+ });+});++it("persists tool overrides and removes them when parameters are reset", async () => {+ const params = {+ sampling: { type: "Default" as const },+ modelChatNamingEnabled: true,+ dateTimeToolEnabled: false,+ chartToolEnabled: false,+ };+ useModelParamsStore.getState().setParams("model", params);+ await vi.runAllTimersAsync();+ expect(settings.setModelParams).toHaveBeenLastCalledWith("model", params);++ useModelParamsStore.getState().resetParams("model");+ await vi.runAllTimersAsync();+ expect(settings.setModelParams).toHaveBeenLastCalledWith("model", null);+ expect(resolveModelTools(useModelParamsStore.getState().getParams("model"), false)).toEqual({+ modelChatNamingEnabled: false,+ dateTimeToolEnabled: true,+ chartToolEnabled: true,+ });+});++it("marks parameters modified only when their effective tools differ from defaults", () => {+ const sampling = { type: "Default" as const };+ expect(isCustomParams({ sampling }, false)).toBe(false);+ expect(isCustomParams({ sampling, modelChatNamingEnabled: false, dateTimeToolEnabled: true }, false)).toBe(false);+ expect(isCustomParams({ sampling, modelChatNamingEnabled: true }, false)).toBe(false);+ expect(isCustomParams({ sampling, modelChatNamingEnabled: false }, true)).toBe(true);+ expect(isCustomParams({ sampling, dateTimeToolEnabled: false }, true)).toBe(true);+ expect(isCustomParams({ sampling, chartToolEnabled: true }, true)).toBe(false);+ expect(isCustomParams({ sampling, chartToolEnabled: false }, true)).toBe(true);+});++it.each([+ { paramSize: 1_999_999_999, expected: false },+ { paramSize: 2_000_000_000, expected: true },+ { paramSize: undefined, expected: true },+])("uses size-specific tool defaults for outgoing requests: %j", ({ paramSize, expected }) => {+ useModelsStore.setState({+ models: [+ {+ repoId: "model",+ name: "Model",+ vendor: "Vendor",+ kind: ModelKind.Text,+ reasoning: { kind: "unsupported" },+ supportsTools: true,+ paramSize,+ },+ ],+ });+ useChatStore.getState().runChatStream({ repoId: "model", messages: [] });+ expect(runStream).toHaveBeenLastCalledWith(+ expect.objectContaining({+ modelChatNamingEnabled: expected,+ dateTimeToolEnabled: expected,+ chartToolEnabled: expected,+ }),+ );+ expect(isCustomParams({ sampling: { type: "Default" } }, true, paramSize)).toBe(false);+});++it("preserves explicit opt-ins on small models while respecting the global naming switch", () => {+ const params = {+ sampling: { type: "Default" as const },+ modelChatNamingEnabled: true,+ dateTimeToolEnabled: true,+ chartToolEnabled: true,+ };+ expect(resolveModelTools(params, true, 1_000_000_000)).toEqual({+ modelChatNamingEnabled: true,+ dateTimeToolEnabled: true,+ chartToolEnabled: true,+ });+ expect(resolveModelTools(params, false, 1_000_000_000)).toEqual({+ modelChatNamingEnabled: false,+ dateTimeToolEnabled: true,+ chartToolEnabled: true,+ });+ expect(isCustomParams(params, true, 1_000_000_000)).toBe(true);+ expect(isCustomParams({ sampling: params.sampling, modelChatNamingEnabled: true }, false, 1_000_000_000)).toBe(false);+ expect(isCustomParams({ sampling: params.sampling, dateTimeToolEnabled: false }, true, 1_000_000_000)).toBe(false);+});++it.each([+ { model: undefined, request: undefined, expected: true },+ { model: false, request: undefined, expected: false },+ { model: true, request: false, expected: false },+ { model: false, request: true, expected: true },+])("resolves the model chart preference and explicit run override: %j", (test) => {+ useModelParamsStore.setState({+ paramsByRepoId: { model: { sampling: { type: "Default" }, chartToolEnabled: test.model } },+ });+ useChatStore.getState().runChatStream({ repoId: "model", messages: [], chartToolEnabled: test.request });+ expect(runStream).toHaveBeenLastCalledWith(expect.objectContaining({ chartToolEnabled: test.expected }));+});++it("preserves a model's naming opt-out while the global feature is disabled and re-enabled", () => {+ const params = { sampling: { type: "Default" as const }, modelChatNamingEnabled: false };+ useModelParamsStore.getState().setParams("model", params);++ for (const enabled of [false, true]) {+ useModelParamsStore.getState().setGlobalModelChatNamingEnabled(enabled);+ const saved = useModelParamsStore.getState().getParams("model");+ expect(saved).toEqual(params);+ expect(resolveModelTools(saved, enabled).modelChatNamingEnabled).toBe(false);+ }+});++it.each([+ { global: false, model: true, request: true, expected: false },+ { global: true, model: false, request: true, expected: false },+ { global: true, model: undefined, request: false, expected: false },+ { global: true, model: undefined, request: true, expected: true },+])("enforces the global and per-model naming gates on outgoing requests: %j", (test) => {+ useModelParamsStore.setState({+ globalModelChatNamingEnabled: test.global,+ paramsByRepoId: { model: { sampling: { type: "Default" }, modelChatNamingEnabled: test.model } },+ });+ useChatStore.getState().runChatStream({ repoId: "model", messages: [], modelChatNamingEnabled: test.request });+ expect(runStream).toHaveBeenLastCalledWith(expect.objectContaining({ modelChatNamingEnabled: test.expected }));+});++it("uses model-default reasoning for unset parameters while retaining explicit model overrides", async () => {+ settings.getModelParams.mockResolvedValue({+ legacyOn: { sampling: { type: "Default" }, reasoningEnabled: true },+ legacyOff: { sampling: { type: "Default" }, reasoningEnabled: false },+ explicit: { sampling: { type: "Default" }, reasoningEffort: "high", reasoningEnabled: false },+ });+ await useModelParamsStore.getState().load();+ for (const [repoId, reasoningEffort] of [+ ["unset", "default"],+ ["legacyOn", "default"],+ ["legacyOff", "disabled"],+ ["explicit", "high"],+ ]) {+ useChatStore.getState().runChatStream({ repoId: repoId!, messages: [] });+ expect(runStream).toHaveBeenLastCalledWith(expect.objectContaining({ repoId, reasoningEffort }));+ }+ expect(isCustomParams({ sampling: { type: "Default" }, reasoningEffort: "default" }, true)).toBe(false);+ expect(isCustomParams({ sampling: { type: "Default" }, reasoningEffort: "disabled" }, true)).toBe(false);+});diff --git a/apps/mirai-gui/client/src/stores/use-model-params-store.ts b/apps/mirai-gui/client/src/stores/use-model-params-store.tsindex fc4c2fba6..55c104ef3 100644--- a/apps/mirai-gui/client/src/stores/use-model-params-store.ts+++ b/apps/mirai-gui/client/src/stores/use-model-params-store.ts@@ -4,45 +4,74 @@ import { isReasoningEffort, normalizeSampling, type ModelParams, type ReasoningEconst DEFAULT_PARAMS: ModelParams = { sampling: { type: "Default" } };-export const STOCHASTIC_SEED = { temperature: 0.7, topK: 40, topP: 0.95, minP: 0.05 } as const;--export const defaultReasoningEffort = (globalReasoningEnabled: boolean): ReasoningEffort =>- globalReasoningEnabled ? "default" : "disabled";+export const resolveModelTools = (+ params: ModelParams | undefined,+ globalModelChatNamingEnabled: boolean,+ paramSize?: number,+) => {+ const enabledByDefault = paramSize === undefined || paramSize >= 2_000_000_000;+ return {+ modelChatNamingEnabled: globalModelChatNamingEnabled && (params?.modelChatNamingEnabled ?? enabledByDefault),+ dateTimeToolEnabled: params?.dateTimeToolEnabled ?? enabledByDefault,+ chartToolEnabled: params?.chartToolEnabled ?? enabledByDefault,+ };+};-export const isCustomParams = (params: ModelParams | undefined, globalReasoningEnabled: boolean): boolean => {+export const isCustomParams = (+ params: ModelParams | undefined,+ globalModelChatNamingEnabled: boolean,+ paramSize?: number,+): boolean => {if (!params) return false;const samplingChanged = params.sampling.type !== "Default";- const reasoningOverridden =- params.reasoningEffort !== undefined && params.reasoningEffort !== defaultReasoningEffort(globalReasoningEnabled);- return samplingChanged || reasoningOverridden;+ const tools = resolveModelTools(params, globalModelChatNamingEnabled, paramSize);+ const defaults = resolveModelTools(undefined, globalModelChatNamingEnabled, paramSize);+ return (+ samplingChanged ||+ tools.modelChatNamingEnabled !== defaults.modelChatNamingEnabled ||+ tools.dateTimeToolEnabled !== defaults.dateTimeToolEnabled ||+ tools.chartToolEnabled !== defaults.chartToolEnabled+ );};// Settings written before reasoning levels stored a boolean.const migrateParams = (raw: unknown): ModelParams | null => {if (!raw || typeof raw !== "object") return null;- const { sampling, reasoningEffort, reasoningEnabled } = raw as {- sampling?: ModelParams["sampling"];- reasoningEffort?: unknown;- reasoningEnabled?: unknown;- };+ const { sampling, reasoningEffort, reasoningEnabled, modelChatNamingEnabled, dateTimeToolEnabled, chartToolEnabled } =+ raw as {+ sampling?: ModelParams["sampling"] | { type: "Argmax" };+ reasoningEffort?: unknown;+ reasoningEnabled?: unknown;+ modelChatNamingEnabled?: unknown;+ dateTimeToolEnabled?: unknown;+ chartToolEnabled?: unknown;+ };if (!sampling) return null;const effort: ReasoningEffort | undefined = isReasoningEffort(reasoningEffort)? reasoningEffort: typeof reasoningEnabled === "boolean"- ? defaultReasoningEffort(reasoningEnabled)+ ? reasoningEnabled+ ? "default"+ : "disabled": undefined;- return { sampling: normalizeSampling(sampling), ...(effort !== undefined ? { reasoningEffort: effort } : {}) };+ return {+ sampling: normalizeSampling(sampling),+ ...(effort !== undefined ? { reasoningEffort: effort } : {}),+ ...(typeof modelChatNamingEnabled === "boolean" ? { modelChatNamingEnabled } : {}),+ ...(typeof dateTimeToolEnabled === "boolean" ? { dateTimeToolEnabled } : {}),+ ...(typeof chartToolEnabled === "boolean" ? { chartToolEnabled } : {}),+ };};type ModelParamsState = {paramsByRepoId: Record<string, ModelParams>;- globalReasoningEnabled: boolean;+ globalModelChatNamingEnabled: boolean;loaded: boolean;load: () => Promise<void>;getParams: (repoId: string) => ModelParams;setParams: (repoId: string, params: ModelParams) => void;resetParams: (repoId: string) => void;- setGlobalReasoningEnabled: (enabled: boolean) => void;+ setGlobalModelChatNamingEnabled: (enabled: boolean) => void;};const persistTimers = new Map<string, ReturnType<typeof setTimeout>>();@@ -63,14 +92,14 @@ const persist = (repoId: string, params: ModelParams | null): void => {export const useModelParamsStore = create<ModelParamsState>((set, get) => ({paramsByRepoId: {},- globalReasoningEnabled: true,+ globalModelChatNamingEnabled: true,loaded: false,load: async () => {try {- const [stored, globalReasoningEnabled] = await Promise.all([+ const [stored, globalModelChatNamingEnabled] = await Promise.all([getPlatform().settings.getModelParams(),- getPlatform().settings.getEnableThinking(),+ getPlatform().settings.getModelChatNamingEnabled(),]);const paramsByRepoId: Record<string, ModelParams> = {};for (const [repoId, raw] of Object.entries(stored ?? {})) {@@ -79,7 +108,7 @@ export const useModelParamsStore = create<ModelParamsState>((set, get) => ({}set({paramsByRepoId,- globalReasoningEnabled: typeof globalReasoningEnabled === "boolean" ? globalReasoningEnabled : true,+ globalModelChatNamingEnabled: globalModelChatNamingEnabled !== false,loaded: true,});} catch (e) {@@ -104,5 +133,5 @@ export const useModelParamsStore = create<ModelParamsState>((set, get) => ({persist(repoId, null);},- setGlobalReasoningEnabled: (enabled) => set({ globalReasoningEnabled: enabled }),+ setGlobalModelChatNamingEnabled: (enabled) => set({ globalModelChatNamingEnabled: enabled }),}));diff --git a/apps/mirai-gui/client/src/stores/use-models-store.test.ts b/apps/mirai-gui/client/src/stores/use-models-store.test.tsindex 4401b24f9..bd130604e 100644--- a/apps/mirai-gui/client/src/stores/use-models-store.test.ts+++ b/apps/mirai-gui/client/src/stores/use-models-store.test.ts@@ -1,10 +1,10 @@import { beforeEach, expect, it, vi } from "vitest";-import type { EngineModel } from "@/types/model-manager";+import type { EngineModel, ModelCatalog } from "@/types/model-manager";import { modelDownloadPhases } from "@/types/model-manager";import { useModelsStore } from "./use-models-store";-const mocks = vi.hoisted(() => ({ getModels: vi.fn() }));-vi.mock("@/platform/platform-singleton", () => ({ getPlatform: () => ({ models: { getModels: mocks.getModels } }) }));+const mocks = vi.hoisted(() => ({ getModels: vi.fn(), refreshModels: vi.fn() }));+vi.mock("@/platform/platform-singleton", () => ({ getPlatform: () => ({ models: mocks }) }));const REPO_ID = "vendor/model";const defaults = useModelsStore.getState();@@ -15,6 +15,7 @@ const snapshot = (phase: EngineModel["state"]["phase"], seq: number): EngineModevendor: "Vendor",name: "Model",reasoning: { kind: "unsupported" },+ supportsTools: false,state: { phase, totalKbytes: 100, downloadedKbytes: 50, seq },});@@ -24,7 +25,7 @@ beforeEach(() => {});it("keeps a download event that arrived while the catalog request was in flight", async () => {- let resolveModels!: (value: EngineModel[]) => void;+ let resolveModels!: (value: ModelCatalog) => void;mocks.getModels.mockImplementation(() =>new Promise((resolve) => {@@ -34,7 +35,7 @@ it("keeps a download event that arrived while the catalog request was in flight"const fetching = useModelsStore.getState().fetchModels();useModelsStore.getState().applyDownloadEvent({ kind: "done", identifier: REPO_ID, seq: 7 });- resolveModels([snapshot(modelDownloadPhases.downloading, 3)]);+ resolveModels({ models: [snapshot(modelDownloadPhases.downloading, 3)], complete: true, refreshing: false });await fetching;expect(useModelsStore.getState().modelStatesById[REPO_ID]?.phase).toBe(modelDownloadPhases.downloaded);@@ -42,7 +43,11 @@ it("keeps a download event that arrived while the catalog request was in flight"it("applies a snapshot that is newer than the last event", async () => {useModelsStore.getState().applyDownloadEvent({ kind: "paused", identifier: REPO_ID, seq: 2 });- mocks.getModels.mockResolvedValue([snapshot(modelDownloadPhases.downloaded, 5)]);+ mocks.getModels.mockResolvedValue({+ models: [snapshot(modelDownloadPhases.downloaded, 5)],+ complete: true,+ refreshing: false,+ });await useModelsStore.getState().fetchModels();@@ -55,3 +60,74 @@ it("ignores an event older than the state it already holds", () => {expect(useModelsStore.getState().modelStatesById[REPO_ID]?.phase).toBe(modelDownloadPhases.downloaded);});++it("refetches after catalog changes while a snapshot is in flight", async () => {+ let resolveModels!: (value: ModelCatalog) => void;+ mocks.getModels+ .mockImplementationOnce(+ () =>+ new Promise<ModelCatalog>((resolve) => {+ resolveModels = resolve;+ }),+ )+ .mockResolvedValueOnce({+ models: [snapshot(modelDownloadPhases.downloaded, 2)],+ complete: true,+ refreshing: false,+ });+ const fetching = useModelsStore.getState().fetchModels();+ void useModelsStore.getState().fetchModels();+ void useModelsStore.getState().fetchModels();+ resolveModels({ models: [], complete: false, refreshing: true });+ await fetching;++ expect(mocks.getModels).toHaveBeenCalledTimes(2);+ expect(useModelsStore.getState().catalogComplete).toBe(true);+ expect(useModelsStore.getState().models.map((model) => model.repoId)).toEqual([REPO_ID]);+});++it("settles errors and lets the next request retry", async () => {+ mocks.getModels.mockRejectedValueOnce(new Error("Catalog unavailable"));+ await useModelsStore.getState().fetchModels();+ expect(useModelsStore.getState()).toMatchObject({ initialized: true, loading: false, error: "Catalog unavailable" });++ mocks.getModels.mockResolvedValueOnce({ models: [], complete: true, refreshing: false });+ await useModelsStore.getState().fetchModels();+ expect(useModelsStore.getState()).toMatchObject({ hasLoadedModels: true, catalogComplete: true, error: null });+});++it("updates a pending model when its existing download is discovered", async () => {+ mocks.getModels.mockResolvedValueOnce({+ models: [snapshot(modelDownloadPhases.initializing, 0)],+ complete: false,+ refreshing: true,+ });+ await useModelsStore.getState().fetchModels();+ useModelsStore.getState().applyDownloadEvent({+ kind: "state",+ identifier: REPO_ID,+ seq: 1,+ phase: modelDownloadPhases.downloaded,+ completedBytes: 102400,+ totalBytes: 102400,+ error: null,+ });+ expect(useModelsStore.getState().modelStatesById[REPO_ID]).toMatchObject({+ phase: modelDownloadPhases.downloaded,+ downloadedKbytes: 100,+ totalKbytes: 100,+ seq: 1,+ });+});++it("distinguishes an incomplete failed catalog from a refresh in progress and explicitly retries it", async () => {+ mocks.getModels.mockResolvedValueOnce({ models: [], complete: false, refreshing: false });+ await useModelsStore.getState().fetchModels();+ expect(useModelsStore.getState()).toMatchObject({ catalogComplete: false, catalogRefreshing: false });++ mocks.refreshModels.mockResolvedValueOnce(undefined);+ mocks.getModels.mockResolvedValueOnce({ models: [], complete: false, refreshing: true });+ await useModelsStore.getState().refreshModels();+ expect(mocks.refreshModels).toHaveBeenCalledOnce();+ expect(useModelsStore.getState()).toMatchObject({ catalogComplete: false, catalogRefreshing: true, error: null });+});diff --git a/apps/mirai-gui/client/src/stores/use-models-store.ts b/apps/mirai-gui/client/src/stores/use-models-store.tsindex 17cf70f68..63c6b887c 100644--- a/apps/mirai-gui/client/src/stores/use-models-store.ts+++ b/apps/mirai-gui/client/src/stores/use-models-store.ts@@ -21,12 +21,15 @@ type ModelsState = {initialized: boolean;/** At least one fetch succeeded; gates logic that must trust `models`. */hasLoadedModels: boolean;+ catalogComplete: boolean;+ catalogRefreshing: boolean;error: string | null;modelStatesById: Record<string, ModelDownloadState>;modelPhasesById: Record<string, ModelDownloadPhase>;installedAtById: Record<string, number>;vendorIconsByName: Record<string, VendorIcons>;fetchModels: () => Promise<void>;+ refreshModels: () => Promise<void>;deleteLocalModel: (repoId: string) => Promise<void>;getModelState: (repoId: string) => ModelDownloadState | undefined;updateModelState: (repoId: string, patch: Partial<ModelDownloadState>) => void;@@ -34,6 +37,7 @@ type ModelsState = {};let fetchInFlight: Promise<void> | null = null;+let fetchAgain = false;export const useModelsStore = create<ModelsState>()(persist(@@ -42,6 +46,8 @@ export const useModelsStore = create<ModelsState>()(loading: false,initialized: false,hasLoadedModels: false,+ catalogComplete: false,+ catalogRefreshing: false,error: null,modelStatesById: {},modelPhasesById: {},@@ -49,90 +55,108 @@ export const useModelsStore = create<ModelsState>()(vendorIconsByName: {},fetchModels: async () => {- if (fetchInFlight) return fetchInFlight;+ if (fetchInFlight) {+ fetchAgain = true;+ return fetchInFlight;+ }fetchInFlight = (async () => {const firstLoad = get().initialized === false;set(firstLoad ? { loading: true, error: null } : { error: null });- try {- const rawModels = await getPlatform().models.getModels();-- const prevById = get().modelStatesById;- const prevInstalledAt = get().installedAtById;-- const entries = rawModels.map((m) => {- const repoId = m.repoId ?? m.identifier;- const prev = prevById[repoId];- const mergedState = prev && prev.seq > m.state.seq ? prev : preserveTotalKbytes(m.state, prev);- return { repoId, mergedState };- });-- const byId: Record<string, ModelDownloadState> = Object.fromEntries(- entries.map(({ repoId, mergedState }) => [repoId, mergedState]),- );- const phaseById: Record<string, ModelDownloadPhase> = Object.fromEntries(- entries.map(({ repoId, mergedState }) => [repoId, mergedState.phase]),- );-- const now = Date.now();- const newlyInstalledEntries = entries.flatMap(({ repoId, mergedState }) =>- mergedState.phase === modelDownloadPhases.downloaded && prevInstalledAt[repoId] === undefined- ? [[repoId, now] as const]- : [],- );- const installedAt: Record<string, number> =- newlyInstalledEntries.length > 0- ? { ...prevInstalledAt, ...Object.fromEntries(newlyInstalledEntries) }- : prevInstalledAt;-- const mapped: PlatformModel[] = rawModels.map((m, sourceIndex) => {- const repoId = m.repoId ?? m.identifier;- const mergedState = byId[repoId];- const totalBytes = (mergedState?.totalKbytes ?? 0) * 1024;- const familyName = m.familyName ?? extractFamily(m.name);- return {- vendor: m.vendor,- name: m.name,- quantization: m.quantization ?? null,- ...(typeof m.quantizationBits === "number" ? { quantizationBits: m.quantizationBits } : {}),- repoId,- kind: ModelKind.Text,- reasoning: m.reasoning,- size: totalBytes,- ...(typeof m.paramSize === "number" ? { paramSize: m.paramSize } : {}),- family: familyName,- ...(m.familyIdentifier ? { familyIdentifier: m.familyIdentifier } : {}),- sourceIndex,- };- });-- const vendorIconsByName: Record<string, VendorIcons> = Object.fromEntries(- rawModels.flatMap((m) => (m.vendorIcons ? [[m.vendor, m.vendorIcons] as const] : [])),- );-- set({- models: mapped,- modelStatesById: byId,- modelPhasesById: phaseById,- installedAtById: installedAt,- vendorIconsByName,- loading: false,- initialized: true,- hasLoadedModels: true,- error: null,- });- } catch (error) {- set({- loading: false,- initialized: true,- error: error instanceof Error ? error.message : "Failed to fetch models",- });- }+ do {+ fetchAgain = false;+ try {+ const { models: rawModels, complete, refreshing } = await getPlatform().models.getModels();++ const prevById = get().modelStatesById;+ const prevInstalledAt = get().installedAtById;++ const entries = rawModels.map((m) => {+ const repoId = m.repoId ?? m.identifier;+ const prev = prevById[repoId];+ const mergedState = prev && prev.seq > m.state.seq ? prev : preserveTotalKbytes(m.state, prev);+ return { repoId, mergedState };+ });++ const byId: Record<string, ModelDownloadState> = Object.fromEntries(+ entries.map(({ repoId, mergedState }) => [repoId, mergedState]),+ );+ const phaseById: Record<string, ModelDownloadPhase> = Object.fromEntries(+ entries.map(({ repoId, mergedState }) => [repoId, mergedState.phase]),+ );++ const now = Date.now();+ const newlyInstalledEntries = entries.flatMap(({ repoId, mergedState }) =>+ mergedState.phase === modelDownloadPhases.downloaded && prevInstalledAt[repoId] === undefined+ ? [[repoId, now] as const]+ : [],+ );+ const installedAt: Record<string, number> =+ newlyInstalledEntries.length > 0+ ? { ...prevInstalledAt, ...Object.fromEntries(newlyInstalledEntries) }+ : prevInstalledAt;++ const mapped: PlatformModel[] = rawModels.map((m, sourceIndex) => {+ const repoId = m.repoId ?? m.identifier;+ const mergedState = byId[repoId];+ const totalBytes = (mergedState?.totalKbytes ?? 0) * 1024;+ const familyName = m.familyName ?? extractFamily(m.name);+ return {+ vendor: m.vendor,+ name: m.name,+ quantization: m.quantization ?? null,+ ...(typeof m.quantizationBits === "number" ? { quantizationBits: m.quantizationBits } : {}),+ repoId,+ kind: ModelKind.Text,+ reasoning: m.reasoning,+ supportsTools: m.supportsTools,+ size: totalBytes,+ ...(typeof m.paramSize === "number" ? { paramSize: m.paramSize } : {}),+ family: familyName,+ ...(m.familyIdentifier ? { familyIdentifier: m.familyIdentifier } : {}),+ sourceIndex,+ };+ });++ const vendorIconsByName: Record<string, VendorIcons> = Object.fromEntries(+ rawModels.flatMap((m) => (m.vendorIcons ? [[m.vendor, m.vendorIcons] as const] : [])),+ );++ set({+ models: mapped,+ modelStatesById: byId,+ modelPhasesById: phaseById,+ installedAtById: installedAt,+ vendorIconsByName,+ loading: false,+ initialized: true,+ hasLoadedModels: true,+ catalogComplete: complete,+ catalogRefreshing: refreshing,+ error: null,+ });+ } catch (error) {+ set({+ loading: false,+ initialized: true,+ error: error instanceof Error ? error.message : "Failed to fetch models",+ });+ }+ } while (fetchAgain);})().finally(() => {fetchInFlight = null;});return fetchInFlight;},+ refreshModels: async () => {+ try {+ await getPlatform().models.refreshModels();+ await get().fetchModels();+ } catch (error) {+ set({ error: error instanceof Error ? error.message : "Failed to refresh models" });+ }+ },+deleteLocalModel: async (repoId: string) => {await getPlatform().models.deleteModel(repoId);useModelParamsStore.getState().resetParams(repoId);diff --git a/apps/mirai-gui/client/src/stores/use-settings-store.test.ts b/apps/mirai-gui/client/src/stores/use-settings-store.test.tsnew file mode 100644index 000000000..ef0f95c1c--- /dev/null+++ b/apps/mirai-gui/client/src/stores/use-settings-store.test.ts@@ -0,0 +1,80 @@+import { beforeEach, expect, it, vi } from "vitest";+import { useSettingsStore } from "./use-settings-store";++const settings = vi.hoisted(() => ({+ getAnalyticsEnabled: vi.fn(async () => false),+ setAnalyticsEnabled: vi.fn<(enabled: boolean) => Promise<void>>().mockResolvedValue(undefined),+ getModelChatNamingEnabled: vi.fn(async () => true),+ setModelChatNamingEnabled: vi.fn<(enabled: boolean) => Promise<void>>().mockResolvedValue(undefined),+ getAutoEjectEnabled: vi.fn(async () => true),+ getAutoEjectMinutes: vi.fn(async () => 15),+}));++vi.mock("@/platform/platform-singleton", () => ({ getPlatform: () => ({ settings }) }));++const defaults = useSettingsStore.getState();++beforeEach(() => {+ vi.clearAllMocks();+ useSettingsStore.setState(defaults, true);+});++it("starts with analytics disabled and stays disabled when settings cannot be read", async () => {+ expect(useSettingsStore.getState().analyticsEnabled).toBe(false);+ settings.getAnalyticsEnabled.mockRejectedValueOnce(new Error("read failed"));++ await useSettingsStore.getState().fetch();+ expect(useSettingsStore.getState().analyticsEnabled).toBe(false);+});++it("loads and saves explicit analytics consent", async () => {+ settings.getAnalyticsEnabled.mockResolvedValueOnce(true);+ await useSettingsStore.getState().fetch();+ expect(useSettingsStore.getState().analyticsEnabled).toBe(true);++ await useSettingsStore.getState().setAnalyticsEnabled(false);+ expect(settings.setAnalyticsEnabled).toHaveBeenCalledWith(false);+ expect(useSettingsStore.getState().analyticsEnabled).toBe(false);+});++it("does not show analytics as enabled when saving consent fails", async () => {+ settings.setAnalyticsEnabled.mockRejectedValueOnce(new Error("Could not save settings"));++ await useSettingsStore.getState().setAnalyticsEnabled(true);+ expect(useSettingsStore.getState().analyticsEnabled).toBe(false);+});++it("starts with chat naming enabled and loads a saved opt-out", async () => {+ expect(useSettingsStore.getState().modelChatNamingEnabled).toBe(true);+ settings.getModelChatNamingEnabled.mockResolvedValueOnce(false);++ await useSettingsStore.getState().fetch();+ expect(useSettingsStore.getState().modelChatNamingEnabled).toBe(false);+});++it("keeps chat naming disabled after successfully saving the preference", async () => {+ await useSettingsStore.getState().setModelChatNamingEnabled(false);++ expect(settings.setModelChatNamingEnabled).toHaveBeenCalledWith(false);+ expect(useSettingsStore.getState().modelChatNamingEnabled).toBe(false);+});++it("restores the previous preference if saving fails", async () => {+ settings.setModelChatNamingEnabled.mockRejectedValueOnce(new Error("Could not save settings"));++ await useSettingsStore.getState().setModelChatNamingEnabled(false);+ expect(useSettingsStore.getState().modelChatNamingEnabled).toBe(true);+});++it("defaults auto-eject to fifteen minutes, including when settings cannot be read", async () => {+ expect(useSettingsStore.getState().autoEjectMinutes).toBe(15);+ settings.getAutoEjectMinutes.mockRejectedValueOnce(new Error("read failed"));+ await useSettingsStore.getState().fetch();+ expect(useSettingsStore.getState().autoEjectMinutes).toBe(15);+});++it("loads an explicit auto-eject interval without replacing it with the default", async () => {+ settings.getAutoEjectMinutes.mockResolvedValueOnce(7);+ await useSettingsStore.getState().fetch();+ expect(useSettingsStore.getState().autoEjectMinutes).toBe(7);+});diff --git a/apps/mirai-gui/client/src/stores/use-settings-store.ts b/apps/mirai-gui/client/src/stores/use-settings-store.tsindex 17de96199..24ff2654d 100644--- a/apps/mirai-gui/client/src/stores/use-settings-store.ts+++ b/apps/mirai-gui/client/src/stores/use-settings-store.ts@@ -1,87 +1,63 @@import { create } from "zustand";import { getPlatform } from "@/platform/platform-singleton";+import { DEFAULT_AUTO_EJECT_MINUTES } from "@/platform/services/settings";import { useModelParamsStore } from "./use-model-params-store";type SettingsState = {- enableThinking: boolean;- runOnStartup: boolean;- quickEntryShortcut: string | null;+ analyticsEnabled: boolean;+ modelChatNamingEnabled: boolean;autoEjectEnabled: boolean;autoEjectMinutes: number;fetch: () => Promise<void>;- fetchDesktopSettings: () => Promise<void>;- setEnableThinking: (enabled: boolean) => Promise<void>;- setRunOnStartup: (value: boolean) => Promise<void>;- registerQuickEntryShortcut: (accelerator: string) => Promise<boolean>;- unregisterQuickEntryShortcut: () => Promise<void>;+ setAnalyticsEnabled: (enabled: boolean) => Promise<void>;+ setModelChatNamingEnabled: (enabled: boolean) => Promise<void>;setAutoEjectEnabled: (value: boolean) => Promise<void>;setAutoEjectMinutes: (minutes: number) => Promise<void>;exportLogs: () => Promise<"ok" | "cancelled" | "error">;};export const useSettingsStore = create<SettingsState>((set, get) => ({- enableThinking: true,- runOnStartup: false,- quickEntryShortcut: null,+ analyticsEnabled: false,+ modelChatNamingEnabled: true,autoEjectEnabled: true,- autoEjectMinutes: 2,+ autoEjectMinutes: DEFAULT_AUTO_EJECT_MINUTES,fetch: async () => {const { settings } = getPlatform();- const [thinking, aeEnabled, aeMinutes] = await Promise.all([- settings.getEnableThinking().catch(() => true),+ const [analyticsEnabled, modelChatNamingEnabled, aeEnabled, aeMinutes] = await Promise.all([+ settings.getAnalyticsEnabled().catch(() => false),+ settings.getModelChatNamingEnabled().catch(() => true),settings.getAutoEjectEnabled().catch(() => true),- settings.getAutoEjectMinutes().catch(() => 2),+ settings.getAutoEjectMinutes().catch(() => DEFAULT_AUTO_EJECT_MINUTES),]);set({- enableThinking: thinking,+ analyticsEnabled,+ modelChatNamingEnabled,autoEjectEnabled: aeEnabled,- autoEjectMinutes: Number.isFinite(aeMinutes) ? aeMinutes : 2,+ autoEjectMinutes: Number.isFinite(aeMinutes) && aeMinutes > 0 ? aeMinutes : DEFAULT_AUTO_EJECT_MINUTES,});+ useModelParamsStore.getState().setGlobalModelChatNamingEnabled(modelChatNamingEnabled);},- fetchDesktopSettings: async () => {- const { systemUi } = getPlatform();- const [startup, shortcut] = await Promise.all([- systemUi.getRunOnStartup().catch(() => false),- systemUi.getQuickEntryShortcut().catch(() => null),- ]);- set({ runOnStartup: startup, quickEntryShortcut: shortcut });- },-- setEnableThinking: async (enabled) => {- const { settings } = getPlatform();- set({ enableThinking: enabled });- useModelParamsStore.getState().setGlobalReasoningEnabled(enabled);- await settings.setEnableThinking(enabled).catch(() => {- set({ enableThinking: !enabled });- useModelParamsStore.getState().setGlobalReasoningEnabled(!enabled);- });- },-- setRunOnStartup: async (value) => {- set({ runOnStartup: value });+ setAnalyticsEnabled: async (enabled) => {+ const previous = get().analyticsEnabled;await getPlatform()- .systemUi.setRunOnStartup(value)- .catch(() => set({ runOnStartup: !value }));+ .settings.setAnalyticsEnabled(enabled)+ .then(() => set({ analyticsEnabled: enabled }))+ .catch(() => set({ analyticsEnabled: previous }));},- registerQuickEntryShortcut: async (accelerator) => {- const ok = await getPlatform()- .systemUi.registerQuickEntryShortcut(accelerator)- .catch(() => false);- if (ok) set({ quickEntryShortcut: accelerator });- return ok;- },-- unregisterQuickEntryShortcut: async () => {- try {- await getPlatform().systemUi.unregisterQuickEntryShortcut();- set({ quickEntryShortcut: null });- } catch (e) {- console.error("[settings] unregisterQuickEntryShortcut failed", e);- }+ setModelChatNamingEnabled: async (enabled) => {+ const previous = get().modelChatNamingEnabled;+ set({ modelChatNamingEnabled: enabled });+ useModelParamsStore.getState().setGlobalModelChatNamingEnabled(enabled);+ await getPlatform()+ .settings.setModelChatNamingEnabled(enabled)+ .catch(() => {+ set({ modelChatNamingEnabled: previous });+ useModelParamsStore.getState().setGlobalModelChatNamingEnabled(previous);+ });},setAutoEjectEnabled: async (value) => {diff --git a/apps/mirai-gui/client/src/stores/use-sidebar-store.ts b/apps/mirai-gui/client/src/stores/use-sidebar-store.tsindex fc6628b2f..e59812f7e 100644--- a/apps/mirai-gui/client/src/stores/use-sidebar-store.ts+++ b/apps/mirai-gui/client/src/stores/use-sidebar-store.ts@@ -4,6 +4,8 @@ import { persist } from "zustand/middleware";type SidebarState = {isOpen: boolean;isMobile: boolean;+ chatVisibility: { chatId: string; ratio: number } | null;+ setChatVisibility: (visibility: SidebarState["chatVisibility"]) => void;toggle: () => void;setMobile: (isMobile: boolean) => void;closeOnMobile: () => void;@@ -14,6 +16,8 @@ export const useSidebarStore = create<SidebarState>()((set, get) => ({isOpen: true,isMobile: false,+ chatVisibility: null,+ setChatVisibility: (chatVisibility) => set({ chatVisibility }),toggle: () => set((state) => ({ isOpen: !state.isOpen })),setMobile: (isMobile: boolean) => {const currentState = get();diff --git a/apps/mirai-gui/client/src/styles/theme.css b/apps/mirai-gui/client/src/styles/theme.cssindex d0017b5f2..2ef9f9ab5 100644--- a/apps/mirai-gui/client/src/styles/theme.css+++ b/apps/mirai-gui/client/src/styles/theme.css@@ -61,10 +61,14 @@--color-surface-secondary: var(--color-gray-100);--color-surface-tertiary: var(--color-gray-300);--color-surface-elevated: var(--color-gray-50);+ --color-selection-selected: #e1e1e1;+ --color-selection-hover: #ededed;--color-overlay: rgba(0, 0, 0, 0.4);--color-bg-sidebar: #f5f5f5;--color-bg-hover: #e5e5e5;+ --color-sidebar-chat-selected: #e1e1e1;+ --color-sidebar-chat-hover: #ededed;--color-bg-sub: #f4f5f5;--color-bg-modal: #ffffff;--color-card-hover: #f6f8f8;@@ -116,10 +120,14 @@--color-border-outlined-hover: rgb(from var(--color-white) r g b / 0.2);--color-surface-tertiary: var(--color-gray-200);--color-surface-elevated: var(--color-gray-100);+ --color-selection-selected: color-mix(in srgb, var(--color-surface-elevated), var(--color-text-primary) 8%);+ --color-selection-hover: color-mix(in srgb, var(--color-surface-elevated), var(--color-text-primary) 4%);--color-overlay: rgba(0, 0, 0, 0.7);--color-bg-sidebar: #050506;--color-bg-hover: #181a1b;+ --color-sidebar-chat-selected: #1c1e1f;+ --color-sidebar-chat-hover: #101213;--color-bg-sub: #1f1f1f;--color-bg-modal: #141414;--color-card-hover: #1f1f1f;diff --git a/apps/mirai-gui/client/src/types/chart.test.ts b/apps/mirai-gui/client/src/types/chart.test.tsnew file mode 100644index 000000000..e52e43e5b--- /dev/null+++ b/apps/mirai-gui/client/src/types/chart.test.ts@@ -0,0 +1,80 @@+import { expect, it } from "vitest";+import { CHART_HEIGHTS, CHART_TYPES, isChartSpec, type ChartSpec } from "./chart";++const chart: ChartSpec = {+ type: "bar",+ title: "Budget",+ labels: ["Rent", "Food"],+ datasets: [{ label: "Monthly spending", data: [900, 350] }],+};++it.each(CHART_TYPES)("accepts the data shape for %s charts", (type) => {+ const xy = type === "scatter" || type === "bubble";+ expect(+ isChartSpec({+ ...chart,+ type,+ labels: xy ? undefined : chart.labels,+ datasets: [{ label: "Example", data: xy ? [{ x: 2, y: 4, ...(type === "bubble" ? { r: 6 } : {}) }] : [1, 2] }],+ }),+ ).toBe(true);+});++it.each(CHART_HEIGHTS)("accepts the optional %s chart height", (height) => {+ expect(isChartSpec({ ...chart, height })).toBe(true);+});++it.each([+ { type: "custom" },+ { height: "large" },+ { height: "320px" },+ { height: 320 },+ { height: null },+ { options: { onClick: "alert(1)" } },+ { plugins: [{ id: "code", beforeDraw: "alert(1)" }] },+ { title: " " },+ { title: "x".repeat(201) },+ { labels: [] },+ { labels: ["Rent"] },+ { datasets: [{ label: "Bad", data: [NaN, 1] }] },+ { datasets: [{ label: "Bad", data: [Infinity, 1] }] },+ { datasets: [{ label: "Bad", data: ["1", 2] }] },+ { datasets: [{ label: "Bad", data: [1, 2], parsing: "anything" }] },+ {+ datasets: [+ {+ label: "Bad",+ data: [+ { x: 1, y: 2 },+ { x: 2, y: 3 },+ ],+ },+ ],+ },+ { type: "pie", datasets: [{ label: "Bad", data: [-1, 2] }] },+ { labels: Array(501).fill("x"), datasets: [{ label: "Too long", data: Array(501).fill(1) }] },+ { datasets: Array(9).fill(chart.datasets[0]) },+ { labels: Array(500).fill("x"), datasets: Array(5).fill({ label: "Too many", data: Array(500).fill(1) }) },+])("rejects unsupported configuration or invalid data: %j", (override) => {+ expect(isChartSpec({ ...chart, ...override })).toBe(false);+});++it("validates XY points and bubble radii without accepting point options", () => {+ const pointChart = (type: "bubble" | "scatter", point: unknown) => ({+ type,+ title: "Points",+ datasets: [{ label: "Series", data: [point] }],+ });+ expect(isChartSpec(pointChart("bubble", { x: 1, y: 2 }))).toBe(false);+ expect(isChartSpec(pointChart("bubble", { x: 1, y: 2, r: 101 }))).toBe(false);+ expect(isChartSpec(pointChart("bubble", { x: 1, y: 2, r: -1 }))).toBe(false);+ expect(isChartSpec(pointChart("scatter", { x: 1, y: 2, r: 1 }))).toBe(false);+ expect(isChartSpec(pointChart("scatter", { x: 1, y: 2, onClick: "code" }))).toBe(false);+ expect(isChartSpec({ ...pointChart("scatter", { x: 1, y: 2 }), labels: ["Unexpected"] })).toBe(false);+});++it("treats markup and code-looking labels as plain text, and counts Unicode characters consistently", () => {+ expect(isChartSpec({ ...chart, title: "<img src=x onerror=alert(1)>" })).toBe(true);+ expect(isChartSpec({ ...chart, title: "😀".repeat(200) })).toBe(true);+ expect(isChartSpec({ ...chart, title: "😀".repeat(201) })).toBe(false);+});diff --git a/apps/mirai-gui/client/src/types/chart.ts b/apps/mirai-gui/client/src/types/chart.tsnew file mode 100644index 000000000..ae4302bce--- /dev/null+++ b/apps/mirai-gui/client/src/types/chart.ts@@ -0,0 +1,66 @@+export const CHART_TYPES = ["bar", "line", "scatter", "bubble", "pie", "doughnut", "radar", "polarArea"] as const;+export type ChartKind = (typeof CHART_TYPES)[number];+export const CHART_HEIGHTS = ["small", "medium", "big"] as const;+export type ChartHeight = (typeof CHART_HEIGHTS)[number];+export type ChartPoint = { x: number; y: number; r?: number };+export type ChartSpec = {+ type: ChartKind;+ title: string;+ height?: ChartHeight;+ labels?: string[];+ datasets: Array<{ label: string; data: Array<number | ChartPoint> }>;+ xLabel?: string;+ yLabel?: string;+};++const objectWithKeys = (value: unknown, keys: string[]): value is Record<string, unknown> =>+ value !== null &&+ typeof value === "object" &&+ !Array.isArray(value) &&+ Object.keys(value).every((key) => keys.includes(key));++const text = (value: unknown): value is string =>+ typeof value === "string" && value.trim().length > 0 && Array.from(value).length <= 200;+const finite = (value: unknown): value is number => typeof value === "number" && Number.isFinite(value);++// Saved transcripts are untrusted input too. Keep this data-only schema and its+// limits aligned with desktop/src/chat/chart.rs; never accept Chart.js options.+export function isChartSpec(value: unknown): value is ChartSpec {+ if (!objectWithKeys(value, ["type", "title", "height", "labels", "datasets", "xLabel", "yLabel"])) return false;+ if (!(CHART_TYPES as readonly unknown[]).includes(value.type) || !text(value.title)) return false;+ if (value.height !== undefined && !(CHART_HEIGHTS as readonly unknown[]).includes(value.height)) return false;+ if ((value.xLabel !== undefined && !text(value.xLabel)) || (value.yLabel !== undefined && !text(value.yLabel)))+ return false;+ const xy = value.type === "scatter" || value.type === "bubble";+ if (xy) {+ if (value.labels !== undefined) return false;+ } else if (+ !Array.isArray(value.labels) ||+ value.labels.length < 1 ||+ value.labels.length > 500 ||+ !value.labels.every(text)+ ) {+ return false;+ }+ if (!Array.isArray(value.datasets) || value.datasets.length < 1 || value.datasets.length > 8) return false;+ let count = 0;+ const nonnegative = value.type === "pie" || value.type === "doughnut" || value.type === "polarArea";+ for (const dataset of value.datasets) {+ if (!objectWithKeys(dataset, ["label", "data"]) || !text(dataset.label)) return false;+ if (!Array.isArray(dataset.data) || dataset.data.length < 1 || dataset.data.length > 500) return false;+ count += dataset.data.length;+ if (count > 2_000) return false;+ if (!xy && dataset.data.length !== (value.labels as string[]).length) return false;+ for (const point of dataset.data) {+ if (!xy) {+ if (!finite(point) || (nonnegative && point < 0)) return false;+ } else {+ if (!objectWithKeys(point, ["x", "y", "r"]) || !finite(point.x) || !finite(point.y)) return false;+ if (value.type === "bubble") {+ if (!finite(point.r) || point.r < 0 || point.r > 100) return false;+ } else if (point.r !== undefined) return false;+ }+ }+ }+ return true;+}diff --git a/apps/mirai-gui/client/src/types/llm-stream.ts b/apps/mirai-gui/client/src/types/llm-stream.tsindex 1ca8cc7db..3d4dfb0c7 100644--- a/apps/mirai-gui/client/src/types/llm-stream.ts+++ b/apps/mirai-gui/client/src/types/llm-stream.ts@@ -1,4 +1,5 @@import type { ChatRole } from "@/types/chat";+import type { ChartSpec } from "@/types/chart";import type { ReasoningEffort, SamplingPolicyPayload } from "@/types/sampling";export type LlmRunParams = {@@ -6,6 +7,9 @@ export type LlmRunParams = {messages: Array<{ role: ChatRole; content: string; reasoningContent?: string }>;samplingPolicy?: SamplingPolicyPayload;reasoningEffort?: ReasoningEffort;+ modelChatNamingEnabled?: boolean;+ dateTimeToolEnabled?: boolean;+ chartToolEnabled?: boolean;};export type SessionOutputStats = {@@ -27,16 +31,24 @@ export type SessionOutputStats = {};export type ParsedPatch = { response?: string; chainOfThought?: string };-export type OutputShape = { text?: { parsed?: ParsedPatch; raw?: string } };+export type TranscriptItem =+ | { type: "thinking"; text: string; completed?: boolean }+ | { type: "text"; text: string }+ | { type: "chart"; chart: ChartSpec }+ | { type: "toolCall"; name: string; called: boolean; failed?: boolean };++export type OutputShape = { text?: { parsed?: ParsedPatch; raw?: string }; transcript?: TranscriptItem[] };export type SessionOutputFinishReason ="Stop" | "Length" | "Cancelled" | "ContextLimitReached" | "ToolCalls" | "Rejected";export type LlmRunResult = {text: string;+ chatName?: string;stats: SessionOutputStats;finishReason?: SessionOutputFinishReason;parsed?: { chainOfThought?: string; response?: string };+ transcript?: TranscriptItem[];error?: string;};@@ -46,4 +58,6 @@ export type LlmAsyncStream = {result: Promise<LlmRunResult>;cancel: () => Promise<void>;onParsed: (cb: (p: { chainOfThought?: string; response?: string }) => void) => () => void;+ onChatName: (cb: (name: string) => void) => () => void;+ onTranscript: (cb: (items: TranscriptItem[]) => void) => () => void;};diff --git a/apps/mirai-gui/client/src/types/message.ts b/apps/mirai-gui/client/src/types/message.tsindex a4577978d..ae4cd9823 100644--- a/apps/mirai-gui/client/src/types/message.ts+++ b/apps/mirai-gui/client/src/types/message.ts@@ -1,4 +1,4 @@-import type { SessionOutputStats } from "./llm-stream";+import type { OutputShape, SessionOutputStats } from "./llm-stream";import type { NonSystemRole } from "./chat";export type ParsedOutput = {@@ -22,7 +22,7 @@ export type MessageVersion = {perf?: PerfStats;stats?: SessionOutputStats;attachmentIds?: string[];- output?: { text?: { parsed?: ParsedOutput; raw?: string } };+ output?: OutputShape;error?: string;};@@ -38,6 +38,6 @@ export type Message = {perf?: PerfStats;stats?: SessionOutputStats;attachmentIds?: string[];- output?: { text?: { parsed?: ParsedOutput; raw?: string } };+ output?: OutputShape;error?: string;};diff --git a/apps/mirai-gui/client/src/types/model-manager.ts b/apps/mirai-gui/client/src/types/model-manager.tsindex b52fabec7..ce015c59e 100644--- a/apps/mirai-gui/client/src/types/model-manager.ts+++ b/apps/mirai-gui/client/src/types/model-manager.ts@@ -1,6 +1,7 @@import type { ReasoningSupport } from "./sampling";export const modelDownloadPhases = {+ initializing: "Initializing",notDownloaded: "NotDownloaded",downloading: "Downloading",paused: "Paused",@@ -32,7 +33,14 @@ export type EngineModel = {familyName?: string;paramSize?: number;reasoning: ReasoningSupport;+ supportsTools: boolean;quantization?: string | null;quantizationBits?: number;state: ModelDownloadState;};++export type ModelCatalog = {+ models: EngineModel[];+ complete: boolean;+ refreshing: boolean;+};diff --git a/apps/mirai-gui/client/src/types/models.ts b/apps/mirai-gui/client/src/types/models.tsindex a4722f2bd..8978c1693 100644--- a/apps/mirai-gui/client/src/types/models.ts+++ b/apps/mirai-gui/client/src/types/models.ts@@ -10,6 +10,7 @@ export type UzuModel = {name: string;vendor: string;reasoning: ReasoningSupport;+ supportsTools: boolean;quantization?: string | null;quantizationBits?: number;};diff --git a/apps/mirai-gui/client/src/types/sampling.test.ts b/apps/mirai-gui/client/src/types/sampling.test.tsindex cacdb4ccd..f41f4716f 100644--- a/apps/mirai-gui/client/src/types/sampling.test.ts+++ b/apps/mirai-gui/client/src/types/sampling.test.ts@@ -2,8 +2,17 @@ import { describe, expect, it } from "vitest";import { normalizeSampling } from "./sampling";describe("normalizeSampling", () => {- it("turns a saved zero temperature into argmax", () => {- expect(normalizeSampling({ type: "Stochastic", temperature: 0, topK: 40 })).toEqual({ type: "Argmax" });+ it("turns a saved zero temperature into greedy", () => {+ expect(normalizeSampling({ type: "Stochastic", temperature: 0, topK: 40 })).toEqual({ type: "Greedy" });+ });++ it("migrates the old Argmax name without mistaking disabled temperature scaling for greedy", () => {+ expect(normalizeSampling({ type: "Argmax" })).toEqual({ type: "Greedy" });+ expect(normalizeSampling({ type: "Stochastic", temperature: null, topK: null })).toEqual({+ type: "Stochastic",+ temperature: null,+ topK: null,+ });});it("keeps everything else as saved", () => {diff --git a/apps/mirai-gui/client/src/types/sampling.ts b/apps/mirai-gui/client/src/types/sampling.tsindex 00913e079..f572b4ddb 100644--- a/apps/mirai-gui/client/src/types/sampling.ts+++ b/apps/mirai-gui/client/src/types/sampling.ts@@ -1,22 +1,23 @@export type SamplingPolicyPayload =| { type: "Default" }- | { type: "Argmax" }+ | { type: "Greedy" }| {type: "Stochastic";- temperature?: number;- topK?: number;- topP?: number;- minP?: number;- repetitionPenalty?: number;- suffixRepetitionLength?: number;+ temperature?: number | null;+ topK?: number | null;+ topP?: number | null;+ minP?: number | null;+ repetitionPenalty?: number | null;+ suffixRepetitionLength?: number | null;};// Same floor as the CLI.export const TEMPERATURE_MIN = 0.05;-export const normalizeSampling = (sampling: SamplingPolicyPayload): SamplingPolicyPayload =>- sampling.type === "Stochastic" && sampling.temperature !== undefined && sampling.temperature <= 0- ? { type: "Argmax" }+export const normalizeSampling = (sampling: SamplingPolicyPayload | { type: "Argmax" }): SamplingPolicyPayload =>+ sampling.type === "Argmax" ||+ (sampling.type === "Stochastic" && sampling.temperature != null && sampling.temperature <= 0)+ ? { type: "Greedy" }: sampling;export type ReasoningEffort = "disabled" | "default" | "low" | "medium" | "high" | "xhigh";@@ -27,9 +28,19 @@ export const isReasoningEffort = (value: unknown): value is ReasoningEffort =>typeof value === "string" && (REASONING_EFFORTS as readonly string[]).includes(value);export type ReasoningSupport =- { kind: "unsupported" } | { kind: "alwaysOn" } | { kind: "toggle" } | { kind: "levels"; efforts: ReasoningEffort[] };+ | { kind: "unsupported" }+ | { kind: "alwaysOn" }+ | { kind: "toggle"; defaultEffort: "default" }+ | {+ kind: "levels";+ efforts: Exclude<ReasoningEffort, "default">[];+ defaultEffort?: Exclude<ReasoningEffort, "default">;+ };export type ModelParams = {sampling: SamplingPolicyPayload;reasoningEffort?: ReasoningEffort;+ modelChatNamingEnabled?: boolean;+ dateTimeToolEnabled?: boolean;+ chartToolEnabled?: boolean;};diff --git a/apps/mirai-gui/client/src/utils/chart-data.ts b/apps/mirai-gui/client/src/utils/chart-data.tsnew file mode 100644index 000000000..b149bf77e--- /dev/null+++ b/apps/mirai-gui/client/src/utils/chart-data.ts@@ -0,0 +1,43 @@+import type { ChartSpec } from "@/types/chart";++export function chartTable(chart: ChartSpec): { headers: string[]; rows: string[][] } {+ if (chart.type === "scatter" || chart.type === "bubble") {+ const bubble = chart.type === "bubble";+ return {+ headers: ["Series", chart.xLabel ?? "X", chart.yLabel ?? "Y", ...(bubble ? ["Radius (px)"] : [])],+ rows: chart.datasets.flatMap((dataset) =>+ dataset.data.flatMap((point) =>+ typeof point === "number"+ ? []+ : [[dataset.label, String(point.x), String(point.y), ...(bubble ? [String(point.r)] : [])]],+ ),+ ),+ };+ }+ return {+ headers: [+ chart.xLabel ?? "Category",+ ...chart.datasets.map((dataset) => (chart.yLabel ? `${dataset.label} (${chart.yLabel})` : dataset.label)),+ ],+ rows: (chart.labels ?? []).map((label, index) => [+ label,+ ...chart.datasets.map((dataset) => String(dataset.data[index])),+ ]),+ };+}++const markdownText = (value: string): string =>+ value+ .replace(/&/g, "&")+ .replace(/</g, "<")+ .replace(/>/g, ">")+ .replace(/[\\`*_[\]{}|]/g, "\\$&")+ .replace(/\r\n|\r|\n/g, "<br>");++export function chartToMarkdown(chart: ChartSpec): string {+ const { headers, rows } = chartTable(chart);+ const row = (cells: string[]) => `| ${cells.map(markdownText).join(" | ")} |`;+ return [`**${markdownText(chart.title)}**`, "", row(headers), row(headers.map(() => "---")), ...rows.map(row)].join(+ "\n",+ );+}diff --git a/apps/mirai-gui/desktop/Cargo.lock b/apps/mirai-gui/desktop/Cargo.lockindex a2481a32f..695d6edb2 100644--- a/apps/mirai-gui/desktop/Cargo.lock+++ b/apps/mirai-gui/desktop/Cargo.lock@@ -404,17 +404,6 @@ version = "1.1.2"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"-[[package]]-name = "auto-launch"-version = "0.5.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "1f012b8cc0c850f34117ec8252a44418f2e34a2cf501de89e29b241ae5f79471"-dependencies = [- "dirs 4.0.0",- "thiserror 1.0.69",- "winreg 0.10.1",-]-[[package]]name = "autocfg"version = "1.5.1"@@ -1211,15 +1200,6 @@ dependencies = ["libc",]-[[package]]-name = "crc32c"-version = "0.6.8"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "3a47af21622d091a8f0fb295b88bc886ac74efcc613efc19f5d0b21de5c89e47"-dependencies = [- "rustc_version",-]-[[package]]name = "crc32fast"version = "1.5.0"@@ -1530,22 +1510,13 @@ dependencies = ["crypto-common",]-[[package]]-name = "dirs"-version = "4.0.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"-dependencies = [- "dirs-sys 0.3.7",-]-[[package]]name = "dirs"version = "6.0.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e"dependencies = [- "dirs-sys 0.5.0",+ "dirs-sys",][[package]]@@ -1554,18 +1525,7 @@ version = "7.0.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "8d57d423b3c82e89b9a24ca3091fee61f456a26edbd28d26c65906f4bc1dcd8f"dependencies = [- "dirs-sys 0.5.0",-]--[[package]]-name = "dirs-sys"-version = "0.3.7"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"-dependencies = [- "libc",- "redox_users 0.4.6",- "winapi",+ "dirs-sys",][[package]]@@ -1576,7 +1536,7 @@ checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab"dependencies = ["libc","option-ext",- "redox_users 0.5.2",+ "redox_users","windows-sys 0.61.2",]@@ -1646,11 +1606,9 @@ name = "download-manager"version = "0.6.2"dependencies = ["async-trait",- "base64 0.22.1","bindings","block2","bon",- "crc32c","futures-util","kiban","objc2",@@ -1658,8 +1616,6 @@ dependencies = ["reqwest 0.13.4","serde","serde_json",- "sha1",- "sha2","thiserror 2.0.18","tokio","tokio-stream",@@ -1720,7 +1676,7 @@ dependencies = ["rustc_version","toml 1.1.2+spec-1.1.0","vswhom",- "winreg 0.55.0",+ "winreg",][[package]]@@ -2242,16 +2198,6 @@ dependencies = ["version_check",]-[[package]]-name = "gethostname"-version = "1.1.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "1bd49230192a3797a9a4d6abe9b3eed6f7fa4c8a8a4947977c6f80025f92cbd8"-dependencies = [- "rustix",- "windows-link 0.2.1",-]-[[package]]name = "getrandom"version = "0.2.17"@@ -2388,24 +2334,6 @@ version = "0.3.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280"-[[package]]-name = "global-hotkey"-version = "0.8.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "8c386b0a4a70cb2d39fffd74480f985b6f0bfbcb934b6a6b6b7e630e448f242e"-dependencies = [- "crossbeam-channel",- "keyboard-types 0.7.0",- "objc2",- "objc2-app-kit",- "once_cell",- "serde",- "thiserror 2.0.18",- "windows-sys 0.59.0",- "x11rb",- "xkeysym",-]-[[package]]name = "gobject-sys"version = "0.18.0"@@ -3133,17 +3061,6 @@ dependencies = ["thiserror 2.0.18",]-[[package]]-name = "keyboard-types"-version = "0.7.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "b750dcadc39a09dbadd74e118f6dd6598df77fa01df0cfcdc52c28dece74528a"-dependencies = [- "bitflags 2.13.0",- "serde",- "unicode-segmentation",-]-[[package]]name = "keyboard-types"version = "0.8.3"@@ -3517,17 +3434,18 @@ dependencies = ["chrono","futures","hanashi",+ "plist",+ "schemars 1.2.1","serde","serde_json","tauri","tauri-build",- "tauri-plugin-autostart","tauri-plugin-dialog",- "tauri-plugin-global-shortcut","tauri-plugin-notification","tauri-plugin-updater","thiserror 2.0.18","tokio",+ "uuid","uzu",]@@ -3587,7 +3505,7 @@ dependencies = ["crossbeam-channel","dpi","gtk",- "keyboard-types 0.8.3",+ "keyboard-types","objc2","objc2-app-kit","objc2-core-foundation",@@ -3605,7 +3523,6 @@ version = "0.6.2"dependencies = ["bindings","bon",- "chrono","futures","hanashi","indexmap 2.14.0",@@ -4800,17 +4717,6 @@ dependencies = ["bitflags 2.13.0",]-[[package]]-name = "redox_users"-version = "0.4.6"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43"-dependencies = [- "getrandom 0.2.17",- "libredox",- "thiserror 1.0.69",-]-[[package]]name = "redox_users"version = "0.5.2"@@ -6124,20 +6030,6 @@ dependencies = ["walkdir",]-[[package]]-name = "tauri-plugin-autostart"-version = "2.5.1"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "459383cebc193cdd03d1ba4acc40f2c408a7abce419d64bdcd2d745bc2886f70"-dependencies = [- "auto-launch",- "serde",- "serde_json",- "tauri",- "tauri-plugin",- "thiserror 2.0.18",-]-[[package]]name = "tauri-plugin-dialog"version = "2.8.1"@@ -6180,21 +6072,6 @@ dependencies = ["url",]-[[package]]-name = "tauri-plugin-global-shortcut"-version = "2.3.2"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "b4dd9f4c5136c09cd962da0c86dc4accd4666db2ea591cf16e6597435843bd2b"-dependencies = [- "global-hotkey",- "log",- "serde",- "serde_json",- "tauri",- "tauri-plugin",- "thiserror 2.0.18",-]-[[package]]name = "tauri-plugin-notification"version = "2.3.3"@@ -7925,15 +7802,6 @@ dependencies = ["memchr",]-[[package]]-name = "winreg"-version = "0.10.1"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"-dependencies = [- "winapi",-]-[[package]]name = "winreg"version = "0.55.0"@@ -8021,23 +7889,6 @@ dependencies = ["pkg-config",]-[[package]]-name = "x11rb"-version = "0.13.2"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "9993aa5be5a26815fe2c3eacfc1fde061fc1a1f094bf1ad2a18bf9c495dd7414"-dependencies = [- "gethostname",- "rustix",- "x11rb-protocol",-]--[[package]]-name = "x11rb-protocol"-version = "0.13.2"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "ea6fc2961e4ef194dcbfe56bb845534d0dc8098940c7e5c012a258bfec6701bd"-[[package]]name = "xattr"version = "1.6.1"@@ -8048,12 +7899,6 @@ dependencies = ["rustix",]-[[package]]-name = "xkeysym"-version = "0.2.1"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56"-[[package]]name = "xxhash-rust"version = "0.8.16"diff --git a/apps/mirai-gui/desktop/Cargo.toml b/apps/mirai-gui/desktop/Cargo.tomlindex f2b6cfc7a..e4a74d78f 100644--- a/apps/mirai-gui/desktop/Cargo.toml+++ b/apps/mirai-gui/desktop/Cargo.toml@@ -12,17 +12,18 @@ tauri-build = { version = "2", features = [] }[dependencies]tauri = { version = "2", features = [] }-tauri-plugin-autostart = "2"-tauri-plugin-global-shortcut = "2"tauri-plugin-dialog = "2"tauri-plugin-notification = "2"tauri-plugin-updater = "2"serde = { version = "1", features = ["derive"] }serde_json = "1"+plist = "1"+schemars = "1.2.1"thiserror = "2"tokio = { version = "1", features = ["rt-multi-thread", "sync", "time"] }futures = "0.3"chrono = { version = "0.4", default-features = false, features = ["clock"] }+uuid = { version = "1", features = ["v4"] }uzu = { path = "../../../crates/legacy/uzu", default-features = false, features = ["backend-metal"] }hanashi = { path = "../../../crates/legacy/hanashi", default-features = false }diff --git a/apps/mirai-gui/desktop/src/analytics.rs b/apps/mirai-gui/desktop/src/analytics.rsnew file mode 100644index 000000000..b4aaa8f4f--- /dev/null+++ b/apps/mirai-gui/desktop/src/analytics.rs@@ -0,0 +1,308 @@+use std::{sync::Mutex, time::Duration};++use serde::Serialize;+use serde_json::{Map, Value};+use tokio::sync::mpsc;+use uzu::{+ device::Device,+ engine::Engine,+ session::api::{Client, Endpoint, RetryConfig},+ types::session::chat::{ChatReplyJoulesPerToken, ChatReplyStats},+};++use crate::error::AppResult;++const BASE_URL: &str = "https://sdk.trymirai.com/api/v2";++#[derive(Serialize)]+#[serde(tag = "event_name", content = "payload", rename_all = "snake_case")]+pub enum Event {+ #[serde(rename = "model_download_started")]+ DownloadStarted {+ model_id: String,+ },+ #[serde(rename = "model_download_finished")]+ DownloadFinished {+ model_id: String,+ },+ #[serde(rename = "model_inference_started")]+ InferenceStarted {+ model_id: String,+ },+ #[serde(rename = "model_inference_finished")]+ InferenceFinished {+ model_id: String,+ stats: Box<Stats>,+ },+ // Do not serialize engine errors: they can contain user text or local paths.+ #[serde(rename = "model_inference_failed")]+ InferenceFailed {+ error: &'static str,+ },+}++#[derive(Serialize)]+pub struct Stats {+ #[serde(flatten)]+ stats: ChatReplyStats,+ input_joules_per_token: Option<ChatReplyJoulesPerToken>,+ output_joules_per_token: Option<ChatReplyJoulesPerToken>,+}++impl From<ChatReplyStats> for Stats {+ fn from(stats: ChatReplyStats) -> Self {+ Self {+ input_joules_per_token: stats.input_joules_per_token(),+ output_joules_per_token: stats.output_joules_per_token(),+ stats,+ }+ }+}++// Explicit fields keep local paths and other Device fields off the wire.+#[derive(Serialize)]+struct DeviceInfo {+ os_name: Option<String>,+ cpu_name: Option<String>,+ memory_total: i64,+}++#[derive(Serialize)]+struct Context {+ // Retain the server's field name; the ID now belongs only to this app's+ // opt-in period and is neither persisted nor shared with the engine.+ engine_session_id: String,+ app_version: &'static str,+ uzu_version: String,+ toolchain_version: String,+ device: DeviceInfo,+}++#[derive(Serialize)]+struct Record {+ event_time: String,+ #[serde(flatten)]+ event: Event,+}++#[derive(Serialize)]+struct Body<'a> {+ #[serde(flatten)]+ context: &'a Context,+ #[serde(flatten)]+ record: &'a Record,+}++struct Events;+impl Endpoint for Events {+ const PATH: &'static str = "telemetry/events";+ type Request = Value;+ type Response = ();+}++struct Worker {+ sender: mpsc::Sender<Record>,+ task: tauri::async_runtime::JoinHandle<()>,+}++impl Drop for Worker {+ fn drop(&mut self) {+ // Opting out discards queued records and cancels any pending send/retry.+ self.task.abort();+ }+}++impl Worker {+ fn start() -> Option<Self> {+ let device = Device::new().ok()?;+ let context = Context {+ engine_session_id: uuid::Uuid::new_v4().to_string(),+ app_version: env!("CARGO_PKG_VERSION"),+ uzu_version: Engine::version(),+ toolchain_version: Engine::toolchain_version(),+ device: DeviceInfo {+ os_name: device.os_name,+ cpu_name: device.cpu_name,+ memory_total: device.memory_total,+ },+ };+ let client = Client::builder()+ .base_url(BASE_URL)+ .retry(RetryConfig {+ max_attempts: 2,+ base_delay: Duration::from_secs(1),+ budget: Duration::from_secs(10),+ })+ .build()+ .ok()?;+ let (sender, mut receiver) = mpsc::channel::<Record>(64);+ let task = tauri::async_runtime::spawn(async move {+ while let Some(record) = receiver.recv().await {+ let Ok(body) = serde_json::to_value(Body {+ context: &context,+ record: &record,+ }) else {+ continue;+ };+ // Reporting must never block a chat, and failures are not persisted.+ let _ = client.send::<Events>(&body).await;+ }+ });+ Some(Self {+ sender,+ task,+ })+ }+}++#[derive(Default)]+pub struct AnalyticsState {+ worker: Mutex<Option<Worker>>,+}++fn enabled(settings: &Map<String, Value>) -> bool {+ settings.get("analyticsEnabled").and_then(Value::as_bool) == Some(true)+}++fn apply(+ worker: &mut Option<Worker>,+ enabled: bool,+) {+ if !enabled {+ *worker = None;+ } else if worker.is_none() {+ *worker = Worker::start();+ }+}++impl AnalyticsState {+ pub fn restore(&self) {+ let mut worker = self.worker.lock().expect("analytics mutex poisoned");+ let settings = crate::storage::settings_load().ok();+ apply(&mut worker, settings.as_ref().and_then(Value::as_object).is_some_and(enabled));+ }++ pub fn patch_settings(+ &self,+ patch: Map<String, Value>,+ ) -> AppResult<()> {+ // Serialize persistence and application so rapid toggles cannot apply+ // out of order or disagree with the preference read on the next launch.+ let mut worker = self.worker.lock().expect("analytics mutex poisoned");+ let settings = crate::storage::settings_merge(patch)?;+ apply(&mut worker, enabled(&settings));+ Ok(())+ }++ pub fn report(+ &self,+ event: impl FnOnce() -> Event,+ ) {+ if let Some(worker) = self.worker.lock().expect("analytics mutex poisoned").as_ref() {+ let _ = worker.sender.try_send(Record {+ event_time: chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true),+ event: event(),+ });+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn reporting_requires_explicit_opt_in() {+ for settings in [+ serde_json::json!({}),+ serde_json::json!({"analyticsEnabled": false}),+ serde_json::json!({"analyticsEnabled": "true"}),+ ] {+ assert!(!enabled(settings.as_object().unwrap()));+ }+ assert!(enabled(serde_json::json!({"analyticsEnabled": true}).as_object().unwrap()));+ AnalyticsState::default().report(|| panic!("must not collect events without consent"));+ }++ #[tokio::test]+ async fn opting_out_aborts_worker_and_drops_queued_records() {+ let (sender, receiver) = mpsc::channel(4);+ let (started, ready) = tokio::sync::oneshot::channel();+ let task = tauri::async_runtime::spawn(async move {+ let _receiver = receiver;+ let _ = started.send(());+ std::future::pending::<()>().await;+ });+ let state = AnalyticsState {+ worker: Mutex::new(Some(Worker {+ sender: sender.clone(),+ task,+ })),+ };+ ready.await.unwrap();+ state.report(|| Event::DownloadStarted {+ model_id: "model".into(),+ });+ assert_eq!(sender.capacity(), 3);+ apply(&mut state.worker.lock().unwrap(), false);+ tokio::time::timeout(Duration::from_secs(1), sender.closed()).await.unwrap();+ state.report(|| panic!("must stop collection immediately after opting out"));+ }++ #[test]+ fn payload_contains_only_declared_metadata_and_numeric_stats() {+ let context = Context {+ engine_session_id: "ephemeral-session".into(),+ app_version: "0.6.2",+ uzu_version: "0.6.2".into(),+ toolchain_version: "0.1".into(),+ device: DeviceInfo {+ os_name: Some("macOS".into()),+ cpu_name: Some("Apple".into()),+ memory_total: 64,+ },+ };+ let record = Record {+ event_time: "2026-10-09T00:00:00Z".into(),+ event: Event::InferenceFinished {+ model_id: "model".into(),+ stats: Box::new(ChatReplyStats::default().into()),+ },+ };+ let body = serde_json::to_value(Body {+ context: &context,+ record: &record,+ })+ .unwrap();+ assert_eq!(+ body,+ serde_json::json!({+ "engine_session_id": "ephemeral-session",+ "app_version": "0.6.2",+ "uzu_version": "0.6.2",+ "toolchain_version": "0.1",+ "device": {"os_name": "macOS", "cpu_name": "Apple", "memory_total": 64},+ "event_time": "2026-10-09T00:00:00Z",+ "event_name": "model_inference_finished",+ "payload": {+ "model_id": "model",+ "stats": {+ "duration": 0.0,+ "time_to_first_token": null,+ "prefill_tokens_per_second": null,+ "generate_tokens_per_second": null,+ "tokens_count_input": null,+ "tokens_count_input_cached": null,+ "tokens_count_output": null,+ "memory_used_bytes": null,+ "speculator_stats": null,+ "input_energy": null,+ "output_energy": null,+ "input_joules_per_token": null,+ "output_joules_per_token": null+ }+ }+ })+ );+ }+}diff --git a/apps/mirai-gui/desktop/src/chat/chart.rs b/apps/mirai-gui/desktop/src/chat/chart.rsnew file mode 100644index 000000000..0c0e9432e--- /dev/null+++ b/apps/mirai-gui/desktop/src/chat/chart.rs@@ -0,0 +1,381 @@+use schemars::JsonSchema;+use serde::{Deserialize, Serialize};+use uzu::session::tool::{func_def::ErrorFuture, uzu_tool_function};++/// Data for an inline chart. Only the fields in this schema are accepted.+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]+#[serde(rename_all = "camelCase", deny_unknown_fields)]+pub struct ChartSpec {+ #[serde(rename = "type")]+ pub kind: ChartType,+ /// A concise chart title, at most 200 characters.+ #[schemars(length(min = 1, max = 200))]+ pub title: String,+ /// Chart height: small (320 px), medium (480 px, the default), or big (640 px).+ #[serde(skip_serializing_if = "Option::is_none")]+ pub height: Option<ChartHeight>,+ /// Category names, required for every chart except scatter and bubble.+ #[serde(skip_serializing_if = "Option::is_none")]+ #[schemars(length(min = 1, max = 500))]+ pub labels: Option<Vec<String>>,+ /// One to eight series, with at most 2,000 data points in the whole chart.+ #[schemars(length(min = 1, max = 8))]+ pub datasets: Vec<ChartDataset>,+ /// Optional horizontal axis label, at most 200 characters.+ #[serde(skip_serializing_if = "Option::is_none")]+ #[schemars(length(min = 1, max = 200))]+ pub x_label: Option<String>,+ /// Optional vertical axis label, at most 200 characters.+ #[serde(skip_serializing_if = "Option::is_none")]+ #[schemars(length(min = 1, max = 200))]+ pub y_label: Option<String>,+}++#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+pub enum ChartType {+ Bar,+ Line,+ Scatter,+ Bubble,+ Pie,+ Doughnut,+ Radar,+ PolarArea,+}++#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]+#[serde(rename_all = "lowercase")]+pub enum ChartHeight {+ Small,+ Medium,+ Big,+}++#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]+#[serde(deny_unknown_fields)]+pub struct ChartDataset {+ /// A concise series name, at most 200 characters.+ #[schemars(length(min = 1, max = 200))]+ pub label: String,+ /// Numbers matching labels for categorical charts, or x/y points for scatter and bubble.+ #[schemars(length(min = 1, max = 500))]+ pub data: Vec<ChartDatum>,+}++#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]+#[serde(untagged)]+pub enum ChartDatum {+ Number(f64),+ Point(ChartPoint),+}++#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]+#[serde(deny_unknown_fields)]+pub struct ChartPoint {+ pub x: f64,+ pub y: f64,+ /// Bubble radius in pixels, from 0 to 100. Required for bubble, omitted for scatter.+ #[serde(skip_serializing_if = "Option::is_none")]+ #[schemars(range(min = 0, max = 100))]+ pub r: Option<f64>,+}++impl ChartSpec {+ pub(super) fn validate(&self) -> Result<(), ErrorFuture> {+ validate_text(&self.title)?;+ for text in self.x_label.iter().chain(self.y_label.iter()) {+ validate_text(text)?;+ }+ if !(1..=8).contains(&self.datasets.len()) {+ return Err("A chart must have 1 to 8 datasets".into());+ }+ let point_chart = matches!(self.kind, ChartType::Scatter | ChartType::Bubble);+ if point_chart {+ if self.labels.is_some() {+ return Err("Scatter and bubble charts use x/y points, not category labels".into());+ }+ } else {+ let labels = self.labels.as_ref().ok_or("This chart type requires category labels")?;+ if !(1..=500).contains(&labels.len()) {+ return Err("A chart must have 1 to 500 category labels".into());+ }+ for label in labels {+ validate_text(label)?;+ }+ }+ let mut total_points = 0;+ for dataset in &self.datasets {+ validate_text(&dataset.label)?;+ if !(1..=500).contains(&dataset.data.len()) {+ return Err("Each dataset must have 1 to 500 data points".into());+ }+ total_points += dataset.data.len();+ if total_points > 2_000 {+ return Err("A chart must have at most 2,000 data points".into());+ }+ if let Some(labels) = &self.labels+ && dataset.data.len() != labels.len()+ {+ return Err("Each dataset must have one value per category label".into());+ }+ for datum in &dataset.data {+ match datum {+ ChartDatum::Number(value) if !point_chart => {+ if !value.is_finite() {+ return Err("Chart values must be finite numbers".into());+ }+ if matches!(self.kind, ChartType::Pie | ChartType::Doughnut | ChartType::PolarArea)+ && *value < 0.0+ {+ return Err("Pie, doughnut, and polar area values must be nonnegative".into());+ }+ },+ ChartDatum::Point(point) if point_chart => {+ if !point.x.is_finite() || !point.y.is_finite() {+ return Err("Chart coordinates must be finite numbers".into());+ }+ match (self.kind, point.r) {+ (ChartType::Scatter, None) => {},+ (ChartType::Bubble, Some(r)) if r.is_finite() && (0.0..=100.0).contains(&r) => {},+ _ => {+ return Err(+ "Bubble points require radius r from 0 to 100; scatter points omit r".into()+ );+ },+ }+ },+ _ => {+ return Err(+ "Use numeric data for categorical charts and x/y points for scatter or bubble".into()+ );+ },+ }+ }+ }+ Ok(())+ }+}++fn validate_text(text: &str) -> Result<(), ErrorFuture> {+ if text.trim().is_empty() || text.chars().count() > 200 {+ Err("Chart titles and labels must contain 1 to 200 characters".into())+ } else {+ Ok(())+ }+}++/// Display a chart inline in the conversation. Supply chart data only, never code, callbacks, plugins,+/// URLs, or Chart.js options. Use numeric values and matching labels for categorical charts; use x/y+/// points for scatter and x/y/r points for bubble. Optional height is small (320 px), medium+/// (480 px, default), or big (640 px). The returned chart is displayed to the user.+#[uzu_tool_function]+pub(super) fn show_chart(chart: ChartSpec) -> Result<ChartSpec, ErrorFuture> {+ chart.validate()?;+ Ok(chart)+}++#[cfg(test)]+mod tests {+ use uzu::session::tool::func_def::ToolDescriptor;++ use super::*;++ fn category_chart(kind: &str) -> serde_json::Value {+ serde_json::json!({+ "type": kind,+ "title": "Monthly sales",+ "labels": ["January", "February"],+ "datasets": [{ "label": "Sales", "data": [10, 20] }],+ })+ }++ #[tokio::test]+ async fn tool_returns_validated_data_for_every_chart_type() {+ let tool: ToolDescriptor = show_chart.into();+ for kind in ["bar", "line", "pie", "doughnut", "radar", "polarArea", "scatter", "bubble"] {+ let mut chart = category_chart(kind);+ if matches!(kind, "scatter" | "bubble") {+ chart.as_object_mut().unwrap().remove("labels");+ chart["datasets"][0]["data"] = if kind == "bubble" {+ serde_json::json!([{ "x": 2, "y": 3, "r": 8 }])+ } else {+ serde_json::json!([{ "x": 2, "y": 3 }])+ };+ }+ // Markup models pass object parameters as JSON text.+ for argument in [chart.clone(), serde_json::Value::String(chart.to_string())] {+ let result = tool.execute(serde_json::json!({ "chart": argument }).into()).await.unwrap();+ let returned: ChartSpec = serde_json::from_str(&result.json).unwrap();+ assert_eq!(returned, serde_json::from_value(chart.clone()).unwrap());+ returned.validate().unwrap();+ let json: serde_json::Value = serde_json::from_str(&result.json).unwrap();+ assert!(json.get("height").is_none());+ assert!(json.get("xLabel").is_none());+ assert!(json.get("yLabel").is_none());+ if kind == "scatter" {+ assert!(json.get("labels").is_none());+ assert!(json["datasets"][0]["data"][0].get("r").is_none());+ }+ }+ }+ }++ #[tokio::test]+ async fn tool_coerces_quoted_numeric_data_through_the_datum_union() {+ let tool: ToolDescriptor = show_chart.into();+ let mut chart = category_chart("bar");+ chart["datasets"][0]["data"] = serde_json::json!(["97.42", "123"]);+ for argument in [chart.clone(), serde_json::Value::String(chart.to_string())] {+ let result = tool.execute(serde_json::json!({ "chart": argument }).into()).await.unwrap();+ let returned: ChartSpec = serde_json::from_str(&result.json).unwrap();+ assert_eq!(returned.datasets[0].data, vec![ChartDatum::Number(97.42), ChartDatum::Number(123.0)]);+ }+ }++ #[tokio::test]+ async fn tool_coerces_point_coordinates_through_the_datum_union() {+ let tool: ToolDescriptor = show_chart.into();+ for kind in ["scatter", "bubble"] {+ let mut chart = category_chart(kind);+ chart.as_object_mut().unwrap().remove("labels");+ let mut point = serde_json::json!({ "x": "1", "y": "2" });+ let radius = if kind == "bubble" {+ point["r"] = serde_json::json!("8");+ Some(8.0)+ } else {+ None+ };+ chart["datasets"][0]["data"] = serde_json::json!([point]);+ for argument in [chart.clone(), serde_json::Value::String(chart.to_string())] {+ let result = tool.execute(serde_json::json!({ "chart": argument }).into()).await.unwrap();+ let returned: ChartSpec = serde_json::from_str(&result.json).unwrap();+ assert_eq!(+ returned.datasets[0].data,+ vec![ChartDatum::Point(ChartPoint {+ x: 1.0,+ y: 2.0,+ r: radius,+ })]+ );+ }+ }+ }++ #[tokio::test]+ async fn tool_preserves_supported_heights_and_rejects_arbitrary_sizes() {+ let tool: ToolDescriptor = show_chart.into();+ for height in ["small", "medium", "big"] {+ let mut chart = category_chart("bar");+ chart["height"] = serde_json::json!(height);+ for argument in [chart.clone(), serde_json::Value::String(chart.to_string())] {+ let result = tool.execute(serde_json::json!({ "chart": argument }).into()).await.unwrap();+ let returned: serde_json::Value = serde_json::from_str(&result.json).unwrap();+ assert_eq!(returned["height"], height);+ let chart: ChartSpec = serde_json::from_value(returned).unwrap();+ chart.validate().unwrap();+ }+ }+ for height in [serde_json::json!("large"), serde_json::json!("320px"), serde_json::json!(320)] {+ let mut chart = category_chart("bar");+ chart["height"] = height;+ assert!(tool.execute(serde_json::json!({ "chart": chart }).into()).await.is_err());+ }+ }++ #[tokio::test]+ async fn schema_and_execution_reject_options_code_and_unknown_nested_fields() {+ let tool: ToolDescriptor = show_chart.into();+ let schema: serde_json::Value = tool.parameters.clone().unwrap().try_into().unwrap();+ let chart_schema = &schema["properties"]["chart"];+ assert_eq!(chart_schema["additionalProperties"], false);+ assert_eq!(chart_schema["properties"]["datasets"]["maxItems"], 8);+ assert_eq!(chart_schema["properties"]["datasets"]["items"]["additionalProperties"], false);+ for key in ["options", "plugins", "code", "url", "onClick"] {+ let mut chart = category_chart("bar");+ chart[key] = serde_json::json!("alert('not executable')");+ assert!(tool.execute(serde_json::json!({ "chart": chart }).into()).await.is_err());+ }+ let mut chart = category_chart("bar");+ chart["datasets"][0]["backgroundColor"] = serde_json::json!("url(https://example.com/image)");+ assert!(tool.execute(serde_json::json!({ "chart": chart }).into()).await.is_err());+ let chart = serde_json::json!({+ "type": "scatter", "title": "Points", "datasets": [{+ "label": "Points", "data": [{ "x": 1, "y": 2, "callback": "alert(1)" }]+ }]+ });+ assert!(tool.execute(serde_json::json!({ "chart": chart }).into()).await.is_err());+ }++ #[test]+ fn validation_enforces_shapes_sizes_and_finite_values() {+ let valid: ChartSpec = serde_json::from_value(category_chart("bar")).unwrap();+ let mut invalid = Vec::new();+ let mut chart = valid.clone();+ chart.title = " ".into();+ invalid.push(chart);+ let mut chart = valid.clone();+ chart.datasets[0].label = "x".repeat(201);+ invalid.push(chart);+ let mut chart = valid.clone();+ chart.labels = None;+ invalid.push(chart);+ let mut chart = valid.clone();+ chart.datasets[0].data.pop();+ invalid.push(chart);+ let mut chart = valid.clone();+ chart.datasets = vec![chart.datasets[0].clone(); 9];+ invalid.push(chart);+ let mut chart = valid.clone();+ chart.labels = Some(vec!["Point".into(); 500]);+ chart.datasets = vec![+ ChartDataset {+ label: "Series".into(),+ data: vec![ChartDatum::Number(1.0); 500]+ };+ 5+ ];+ invalid.push(chart);+ let mut chart = valid.clone();+ chart.datasets[0].data[0] = ChartDatum::Number(f64::INFINITY);+ invalid.push(chart);+ let mut chart = valid.clone();+ chart.kind = ChartType::Pie;+ chart.datasets[0].data[0] = ChartDatum::Number(-1.0);+ invalid.push(chart);+ let mut chart = valid.clone();+ chart.datasets[0].data[0] = ChartDatum::Point(ChartPoint {+ x: 1.0,+ y: 2.0,+ r: None,+ });+ invalid.push(chart);+ for chart in invalid {+ assert!(chart.validate().is_err(), "unexpectedly accepted {chart:?}");+ }+ for (kind, radius, accepted) in [+ (ChartType::Scatter, None, true),+ (ChartType::Scatter, Some(1.0), false),+ (ChartType::Bubble, None, false),+ (ChartType::Bubble, Some(0.0), true),+ (ChartType::Bubble, Some(100.0), true),+ (ChartType::Bubble, Some(101.0), false),+ (ChartType::Bubble, Some(f64::NAN), false),+ ] {+ let chart = ChartSpec {+ kind,+ labels: None,+ datasets: vec![ChartDataset {+ label: "Points".into(),+ data: vec![ChartDatum::Point(ChartPoint {+ x: 1.0,+ y: 2.0,+ r: radius,+ })],+ }],+ ..valid.clone()+ };+ assert_eq!(chart.validate().is_ok(), accepted);+ }+ }+}diff --git a/apps/mirai-gui/desktop/src/chat/mod.rs b/apps/mirai-gui/desktop/src/chat/mod.rsindex a4f0ccc20..4cd4f24c6 100644--- a/apps/mirai-gui/desktop/src/chat/mod.rs+++ b/apps/mirai-gui/desktop/src/chat/mod.rs@@ -1,9 +1,12 @@+mod chart;mod messages;+mod naming;mod payloads;mod session;mod session_events;mod stream;mod title;+mod transcript;use std::collections::HashMap;@@ -14,7 +17,7 @@ use stream::run_stream_inner;use tauri::{AppHandle, Manager, ipc::Channel};use title::{TITLE_GEN_RUN_ID, title_gen_inner};use uzu::{- session::chat::ChatSession,+ session::chat::{ChatInstance, ChatSession},types::basic::{CancelToken, ReasoningEffort},};@@ -22,14 +25,15 @@ use crate::{error::AppResult, models::ReasoningSupport};const MIN_AUTO_EJECT_IDLE_MS: u64 = 60_000;const MAX_AUTO_EJECT_IDLE_MS: u64 = 24 * 60 * 60_000;-const DEFAULT_AUTO_EJECT_IDLE_MS: u64 = 2 * 60_000;+const DEFAULT_AUTO_EJECT_IDLE_MS: u64 = 15 * 60_000;struct IdleConfig {enabled: bool,idle_ms: u64,}-// The exact message prefix currently represented by the session's KV cache.+// The client-visible prefix represented by the resident session. The session+// also retains internal tool calls and results that are absent from this projection.#[derive(Default)]struct SessionHistory {repo_id: String,@@ -41,10 +45,12 @@ struct ResidentSession {repo_id: String,identifier: String,session: ChatSession,+ instance: ChatInstance,+ model_chat_naming_enabled: bool,+ date_time_tool_enabled: bool,+ chart_tool_enabled: bool,+ naming: Option<naming::ChatNaming>,support: ReasoningSupport,- // Read right after load: uzu holds the instance lock for a whole reply, so- // asking the session later would block until generation ends.- sampling_defaults: Option<SamplingDefaults>,}// A run is registered before its model loads: a cold load takes tens of@@ -198,17 +204,11 @@ enum RunPlan {Replay,}-// try_lock: while a load holds the session lock there is nothing to report yet,-// and the client re-asks once the session becomes resident.#[tauri::command]-pub async fn chat_sampling_defaults(- state: tauri::State<'_, ChatState>,- repo_id: String,-) -> AppResult<Option<SamplingDefaults>> {- let Ok(guard) = state.session.try_lock() else {- return Ok(None);- };- Ok(guard.as_ref().filter(|resident| resident.repo_id == repo_id).and_then(|r| r.sampling_defaults.clone()))+pub async fn chat_sampling_defaults(repo_id: String) -> AppResult<Option<SamplingDefaults>> {+ let model = crate::models::find_chat_model(&repo_id).await?;+ let engine = crate::engine::engine().await?;+ Ok(engine.model_sampling_defaults(&model).await?.map(SamplingDefaults::from))}#[tauri::command]@@ -223,6 +223,9 @@ pub async fn run_stream(let result = run_stream_inner(&app, &state, &payload, &on_event).await;state.finish_run(&payload.run_id);if let Err(error) = result {+ app.state::<crate::analytics::AnalyticsState>().report(|| crate::analytics::Event::InferenceFailed {+ error: "generation_failed",+ });crate::logger::error("chat:run:error",Some(serde_json::json!({ "runId": payload.run_id, "repoId": payload.repo_id, "error": error })),@@ -279,13 +282,14 @@ pub async fn eject_session(Ok(())}-fn auto_eject_config_from(settings: &serde_json::Map<String, serde_json::Value>) -> (bool, Option<u64>) {+fn auto_eject_config_from(settings: &serde_json::Map<String, serde_json::Value>) -> (bool, u64) {let enabled = settings.get("autoEjectEnabled").and_then(|v| v.as_bool()).unwrap_or(true);let idle_ms = settings.get("autoEjectMinutes").and_then(|v| v.as_f64()).filter(|m| m.is_finite() && *m > 0.0)- .map(|minutes| ((minutes * 60_000.0) as u64).clamp(MIN_AUTO_EJECT_IDLE_MS, MAX_AUTO_EJECT_IDLE_MS));+ .map(|minutes| ((minutes * 60_000.0) as u64).clamp(MIN_AUTO_EJECT_IDLE_MS, MAX_AUTO_EJECT_IDLE_MS))+ .unwrap_or(DEFAULT_AUTO_EJECT_IDLE_MS);(enabled, idle_ms)}@@ -293,13 +297,11 @@ fn apply_auto_eject_config(app: &AppHandle,state: &ChatState,enabled: bool,- idle_ms: Option<u64>,+ idle_ms: u64,) {let mut cfg = state.idle_config.lock().expect("idle_config mutex poisoned");cfg.enabled = enabled;- if let Some(idle_ms) = idle_ms {- cfg.idle_ms = idle_ms;- }+ cfg.idle_ms = idle_ms;drop(cfg);if !enabled {state.cancel_idle_timer();@@ -356,9 +358,10 @@ mod tests {use uzu::types::session::chat::ChatRole;#[test]- fn auto_eject_defaults_to_enabled_with_no_interval_override() {+ fn auto_eject_defaults_to_enabled_after_fifteen_minutes() {let settings = serde_json::Map::new();- assert_eq!(auto_eject_config_from(&settings), (true, None));+ assert_eq!(auto_eject_config_from(&settings), (true, 15 * 60_000));+ assert_eq!(ChatState::default().idle_config.lock().unwrap().idle_ms, 15 * 60_000);}#[test]@@ -366,13 +369,22 @@ mod tests {let mut settings = serde_json::Map::new();settings.insert("autoEjectEnabled".to_string(), false.into());settings.insert("autoEjectMinutes".to_string(), 0.1.into());- assert_eq!(auto_eject_config_from(&settings), (false, Some(MIN_AUTO_EJECT_IDLE_MS)));+ assert_eq!(auto_eject_config_from(&settings), (false, MIN_AUTO_EJECT_IDLE_MS));settings.insert("autoEjectMinutes".to_string(), 100_000.into());- assert_eq!(auto_eject_config_from(&settings).1, Some(MAX_AUTO_EJECT_IDLE_MS));+ assert_eq!(auto_eject_config_from(&settings).1, MAX_AUTO_EJECT_IDLE_MS);settings.insert("autoEjectMinutes".to_string(), (-3).into());- assert_eq!(auto_eject_config_from(&settings).1, None);+ assert_eq!(auto_eject_config_from(&settings).1, DEFAULT_AUTO_EJECT_IDLE_MS);+ }++ #[test]+ fn auto_eject_keeps_saved_intervals() {+ for minutes in [2, 7, 30] {+ let mut settings = serde_json::Map::new();+ settings.insert("autoEjectMinutes".to_string(), minutes.into());+ assert_eq!(auto_eject_config_from(&settings), (true, minutes as u64 * 60_000));+ }}use super::{payloads::test_message, *};diff --git a/apps/mirai-gui/desktop/src/chat/naming.rs b/apps/mirai-gui/desktop/src/chat/naming.rsnew file mode 100644index 000000000..9daac6bf1--- /dev/null+++ b/apps/mirai-gui/desktop/src/chat/naming.rs@@ -0,0 +1,152 @@+use std::sync::{Arc, Mutex};++use tauri::ipc::Channel;+use uzu::{+ session::tool::{+ func_def::{ErrorFuture, ToolDescriptor},+ uzu_tool_closure,+ },+ types::session::chat::{ChatMessage, ChatRole},+};++use super::payloads::RunEvent;++const NAMING_INSTRUCTION: &str = "Use set_chat_name to give this chat a concise, descriptive name in your first turn. Update the name when the conversation's main topic changes or the user asks you to rename it. Choose a short name of 3 to 50 characters. Set hidden to true for autonomous naming housekeeping, and false when the user explicitly asks you to name or rename the chat. Answer the user's request normally; do not mention autonomous housekeeping or ask for permission to name the chat.";++#[derive(Default)]+struct ActiveRun {+ channel: Option<Channel<RunEvent>>,+ name: Option<String>,+}++#[derive(Clone, Default)]+pub(super) struct ChatNaming(Arc<Mutex<ActiveRun>>);++pub(super) struct NamingRun(Arc<Mutex<ActiveRun>>);++impl ChatNaming {+ pub(super) fn begin(+ &self,+ channel: Channel<RunEvent>,+ ) -> NamingRun {+ *self.0.lock().expect("chat naming mutex poisoned") = ActiveRun {+ channel: Some(channel),+ name: None,+ };+ NamingRun(self.0.clone())+ }++ pub(super) fn tool(&self) -> ToolDescriptor {+ let state = self.0.clone();+ uzu_tool_closure! {+ /// Set or update the name of the current chat.+ set_chat_name: async move |+ /// A concise, descriptive chat name of 3 to 50 characters.+ name: String,+ /// True for autonomous housekeeping; false when the user explicitly requests naming or renaming.+ hidden: bool+ | -> Result<String, ErrorFuture> {+ // The transcript reads visibility from the recorded call.+ let _ = hidden;+ let name = normalize_name(&name)?;+ let mut run = state.lock().expect("chat naming mutex poisoned");+ let channel = run.channel.as_ref().ok_or("No chat is currently running")?;+ channel.send(RunEvent::ChatName { name: name.clone() })?;+ run.name = Some(name);+ Ok("Chat name accepted.".to_string())+ }+ }+ }+}++impl NamingRun {+ pub(super) fn name(&self) -> Option<String> {+ self.0.lock().expect("chat naming mutex poisoned").name.clone()+ }+}++impl Drop for NamingRun {+ fn drop(&mut self) {+ self.0.lock().expect("chat naming mutex poisoned").channel = None;+ }+}++fn normalize_name(name: &str) -> Result<String, ErrorFuture> {+ let name = name.split_whitespace().collect::<Vec<_>>().join(" ");+ let name = name.trim_matches(['"', '\'', '`']).trim();+ // Match JavaScript's string length and the manual rename limit.+ if !(3..=50).contains(&name.encode_utf16().count()) || name.chars().any(char::is_control) {+ return Err("Chat names must contain 3 to 50 characters without control characters".into());+ }+ Ok(name.to_string())+}++pub(super) fn add_naming_instruction(messages: &mut Vec<ChatMessage>) {+ if let Some(system) = messages.iter_mut().find(|message| message.role == (ChatRole::System {})) {+ *system = system.with_text(format!("\n\n{NAMING_INSTRUCTION}"));+ } else {+ messages.insert(0, ChatMessage::system().with_text(NAMING_INSTRUCTION.to_string()));+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn normalizes_names_and_matches_manual_rename_limits() {+ assert_eq!(normalize_name(" `Exploring\n London` ").unwrap(), "Exploring London");+ assert!(normalize_name("\" \"").is_err());+ assert!(normalize_name("ab").is_err());+ assert!(normalize_name(&"x".repeat(51)).is_err());+ assert!(normalize_name(&"😀".repeat(25)).is_ok());+ assert!(normalize_name(&"😀".repeat(26)).is_err());+ assert!(normalize_name("bad\0name").is_err());+ }++ #[test]+ fn naming_instruction_keeps_the_existing_system_prompt() {+ let mut messages = vec![ChatMessage::system().with_text("Be helpful.".to_string())];+ add_naming_instruction(&mut messages);+ assert_eq!(messages.len(), 1);+ assert_eq!(messages[0].text().unwrap(), format!("Be helpful.\n\n{NAMING_INSTRUCTION}"));+ }++ #[tokio::test]+ async fn tool_only_names_the_active_run_and_rejects_invalid_names() {+ let naming = ChatNaming::default();+ let tool = naming.tool();+ let arguments = |name: &str| serde_json::json!({ "name": name, "hidden": true }).into();+ assert!(tool.execute(arguments("Before run")).await.is_err());+ let received = Arc::new(Mutex::new(Vec::new()));+ let events = received.clone();+ let run = naming.begin(Channel::new(move |body| {+ events.lock().unwrap().push(body);+ Ok(())+ }));+ assert!(tool.execute(arguments("x")).await.is_err());+ assert!(run.name().is_none());+ assert!(tool.execute(serde_json::json!({ "name": "Missing flag" }).into()).await.is_err());+ assert!(tool.execute(serde_json::json!({ "name": "Bad flag", "hidden": "maybe" }).into()).await.is_err());+ tool.execute(arguments(" First\nname ")).await.unwrap();+ assert_eq!(run.name().as_deref(), Some("First name"));+ tool.execute(serde_json::json!({ "name": "Changed name", "hidden": false }).into()).await.unwrap();+ assert_eq!(run.name().as_deref(), Some("Changed name"));+ assert_eq!(received.lock().unwrap().len(), 2);+ drop(run);+ assert!(tool.execute(arguments("After run")).await.is_err());+ let next = naming.begin(Channel::new(|_| Ok(())));+ assert!(next.name().is_none());+ }++ #[tokio::test]+ async fn naming_tool_requires_hidden_and_accepts_the_standard_boolean_coercion() {+ let naming = ChatNaming::default();+ let tool = naming.tool();+ let schema: serde_json::Value = tool.parameters.clone().unwrap().try_into().unwrap();+ assert!(schema["required"].as_array().unwrap().contains(&serde_json::json!("hidden")));+ let run = naming.begin(Channel::new(|_| Ok(())));+ tool.execute(serde_json::json!({ "name": "Markup name", "hidden": " TRUE " }).into()).await.unwrap();+ assert_eq!(run.name().as_deref(), Some("Markup name"));+ }+}diff --git a/apps/mirai-gui/desktop/src/chat/payloads.rs b/apps/mirai-gui/desktop/src/chat/payloads.rsindex 5624761aa..359a1b685 100644--- a/apps/mirai-gui/desktop/src/chat/payloads.rs+++ b/apps/mirai-gui/desktop/src/chat/payloads.rs@@ -4,6 +4,8 @@ use uzu::types::{session::chat::{ChatReply, ChatReplyFinishReason, ChatRole},};+use super::chart::ChartSpec;+#[derive(Deserialize, Clone)]#[serde(rename_all = "camelCase")]pub struct RunStreamPayload {@@ -15,17 +17,49 @@ pub struct RunStreamPayload {pub sampling_policy: Option<SamplingPolicyPayload>,#[serde(default)]pub reasoning_effort: Option<ReasoningEffort>,+ #[serde(default)]+ pub model_chat_naming_enabled: Option<bool>,+ #[serde(default)]+ pub date_time_tool_enabled: Option<bool>,+ #[serde(default)]+ pub chart_tool_enabled: Option<bool>,}-#[derive(Serialize, Clone)]-#[serde(rename_all = "camelCase")]-pub struct SamplingDefaults {- pub temperature: Option<f64>,- pub top_k: Option<i64>,- pub top_p: Option<f64>,- pub min_p: Option<f64>,- pub repetition_penalty: Option<f64>,- pub suffix_repetition_length: Option<i64>,+#[derive(Serialize)]+#[serde(tag = "type", rename_all_fields = "camelCase")]+pub enum SamplingDefaults {+ Greedy,+ Stochastic {+ temperature: Option<f64>,+ top_k: Option<i64>,+ top_p: Option<f64>,+ min_p: Option<f64>,+ repetition_penalty: Option<f64>,+ suffix_repetition_length: Option<i64>,+ },+}++impl From<SamplingMethod> for SamplingDefaults {+ fn from(method: SamplingMethod) -> Self {+ match method {+ SamplingMethod::Greedy {} => Self::Greedy,+ SamplingMethod::Stochastic {+ temperature,+ top_k,+ top_p,+ min_p,+ repetition_penalty,+ suffix_repetition_length,+ } => Self::Stochastic {+ temperature,+ top_k,+ top_p,+ min_p,+ repetition_penalty,+ suffix_repetition_length,+ },+ }+ }}#[derive(Deserialize, Clone, PartialEq)]@@ -40,7 +74,8 @@ pub struct MsgIn {#[serde(tag = "type")]pub enum SamplingPolicyPayload {Default,- Argmax,+ #[serde(alias = "Argmax")]+ Greedy,#[serde(rename_all = "camelCase")]Stochastic {temperature: Option<f64>,@@ -91,10 +126,15 @@ pub struct SessionOutputStats {#[serde(tag = "type", rename_all = "camelCase")]pub enum RunEvent {#[serde(rename_all = "camelCase")]- Chunk {+ ChatName {+ name: String,+ },+ Transcript {+ items: Vec<TranscriptItem>,+ },+ TranscriptDelta {+ index: usize,delta: String,- #[serde(skip_serializing_if = "Option::is_none")]- parsed: Option<Parsed>,},#[serde(rename_all = "camelCase")]Done {@@ -104,6 +144,9 @@ pub enum RunEvent {finish_reason: Option<ChatReplyFinishReason>,#[serde(skip_serializing_if = "Option::is_none")]parsed: Option<Parsed>,+ #[serde(skip_serializing_if = "Option::is_none")]+ chat_name: Option<String>,+ transcript: Vec<TranscriptItem>,},#[serde(rename_all = "camelCase")]Error {@@ -111,6 +154,27 @@ pub enum RunEvent {},}+#[derive(Debug, Serialize, Clone, PartialEq)]+#[serde(tag = "type", rename_all = "camelCase")]+pub enum TranscriptItem {+ Thinking {+ text: String,+ completed: bool,+ },+ Text {+ text: String,+ },+ Chart {+ chart: ChartSpec,+ },+ ToolCall {+ name: String,+ called: bool,+ #[serde(skip_serializing_if = "std::ops::Not::not")]+ failed: bool,+ },+}+pub(super) fn map_reply_stats(reply: Option<&ChatReply>) -> SessionOutputStats {let stats = reply.map(|r| &r.stats);let prefill_duration = stats.and_then(|s| s.time_to_first_token).unwrap_or(0.0);@@ -140,7 +204,7 @@ pub(super) fn map_reply_stats(reply: Option<&ChatReply>) -> SessionOutputStats {pub(super) fn sampling_policy(payload: &Option<SamplingPolicyPayload>) -> SamplingPolicy {match payload {None | Some(SamplingPolicyPayload::Default) => SamplingPolicy::Default {},- Some(SamplingPolicyPayload::Argmax) => SamplingPolicy::Custom {+ Some(SamplingPolicyPayload::Greedy) => SamplingPolicy::Custom {method: SamplingMethod::Greedy {},},Some(SamplingPolicyPayload::Stochastic {@@ -198,6 +262,33 @@ pub(super) fn test_message(mod tests {use super::*;+ #[test]+ fn omitted_tool_preferences_remain_distinct_from_explicit_choices() {+ let payload = |extra: serde_json::Value| {+ let mut value = serde_json::json!({ "runId": "run", "repoId": "model" });+ value.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());+ serde_json::from_value::<RunStreamPayload>(value).unwrap()+ };+ let defaults = payload(serde_json::json!({}));+ assert_eq!(defaults.model_chat_naming_enabled, None);+ assert_eq!(defaults.date_time_tool_enabled, None);+ assert_eq!(defaults.chart_tool_enabled, None);+ for enabled in [true, false] {+ let naming = payload(serde_json::json!({ "modelChatNamingEnabled": enabled }));+ assert_eq!(naming.model_chat_naming_enabled, Some(enabled));+ assert_eq!(naming.date_time_tool_enabled, None);+ assert_eq!(naming.chart_tool_enabled, None);+ let date = payload(serde_json::json!({ "dateTimeToolEnabled": enabled }));+ assert_eq!(date.model_chat_naming_enabled, None);+ assert_eq!(date.date_time_tool_enabled, Some(enabled));+ assert_eq!(date.chart_tool_enabled, None);+ let chart = payload(serde_json::json!({ "chartToolEnabled": enabled }));+ assert_eq!(chart.model_chat_naming_enabled, None);+ assert_eq!(chart.date_time_tool_enabled, None);+ assert_eq!(chart.chart_tool_enabled, Some(enabled));+ }+ }+#[test]fn deserializes_client_reasoning_content() {let message: MsgIn = serde_json::from_value(serde_json::json!({@@ -236,6 +327,43 @@ mod tests {));}+ #[test]+ fn greedy_accepts_the_previous_argmax_payload_name() {+ for name in ["Greedy", "Argmax"] {+ let payload = serde_json::from_value(serde_json::json!({ "type": name })).unwrap();+ assert!(matches!(payload, SamplingPolicyPayload::Greedy));+ assert!(matches!(+ sampling_policy(&Some(payload)),+ SamplingPolicy::Custom {+ method: SamplingMethod::Greedy {}+ }+ ));+ }+ }++ #[test]+ fn sampling_defaults_include_the_actual_method_and_keep_disabled_filters_null() {+ let defaults = SamplingDefaults::from(SamplingMethod::Stochastic {+ temperature: None,+ top_k: None,+ top_p: Some(0.95),+ min_p: None,+ repetition_penalty: None,+ suffix_repetition_length: None,+ });+ assert_eq!(+ serde_json::to_value(defaults).unwrap(),+ serde_json::json!({+ "type": "Stochastic", "temperature": null, "topK": null, "topP": 0.95,+ "minP": null, "repetitionPenalty": null, "suffixRepetitionLength": null,+ })+ );+ assert_eq!(+ serde_json::to_value(SamplingDefaults::from(SamplingMethod::Greedy {})).unwrap(),+ serde_json::json!({ "type": "Greedy" })+ );+ }+#[test]fn finish_reason_names_are_the_client_contract() {let names: Vec<String> = [diff --git a/apps/mirai-gui/desktop/src/chat/session.rs b/apps/mirai-gui/desktop/src/chat/session.rsindex 4cec06fa2..1fb83ebda 100644--- a/apps/mirai-gui/desktop/src/chat/session.rs+++ b/apps/mirai-gui/desktop/src/chat/session.rs@@ -1,9 +1,17 @@use tauri::{AppHandle, Manager};-use uzu::{session::chat::ChatSession, types::session::chat::ChatConfig};+use uzu::{+ engine::Engine,+ session::{+ chat::{ChatInstance, ChatSession},+ tool::uzu_tool_function,+ },+ types::session::chat::ChatConfig,+};use super::{ChatState, ResidentSession,- payloads::SamplingDefaults,+ chart::show_chart,+ naming::ChatNaming,session_events::{LoadingStatus, emit_session_loading, emit_session_state},};use crate::{@@ -39,20 +47,64 @@ pub(super) async fn ensure_session(app: &AppHandle,state: &ChatState,repo_id: &str,-) -> AppResult<(ChatSession, ReasoningSupport)> {+ model_chat_naming_enabled: Option<bool>,+ date_time_tool_enabled: Option<bool>,+ chart_tool_enabled: Option<bool>,+) -> AppResult<(ChatSession, ReasoningSupport, Option<ChatNaming>)> {let model = find_chat_model(repo_id).await?;+ let model_size = model.properties.as_ref().map(|properties| properties.size);+ let model_chat_naming_enabled = chat_naming_enabled(+ resolve_tool_enabled(model_chat_naming_enabled, model_size),+ &crate::storage::settings_load()?,+ );+ let date_time_tool_enabled = resolve_tool_enabled(date_time_tool_enabled, model_size);+ let chart_tool_enabled = resolve_tool_enabled(chart_tool_enabled, model_size);let engine = engine().await?;- let download_state = engine.download_state(&model).await;- let downloaded =- download_state.as_ref().is_some_and(|s| matches!(s.phase, uzu::storage::DownloadPhase::Downloaded {}));- if model.is_downloadable() && !downloaded {+ if model.is_downloadable()+ && !matches!(engine.ready_download_state(&model).await?.phase, uzu::storage::DownloadPhase::Downloaded {})+ {return Err(AppError::msg("Model is not downloaded"));}let mut guard = state.session.lock().await;if let Some(resident) = guard.as_ref()&& resident.identifier == model.identifier{- return Ok((resident.session.clone(), resident.support.clone()));+ if resident.model_chat_naming_enabled == model_chat_naming_enabled+ && resident.date_time_tool_enabled == date_time_tool_enabled+ && resident.chart_tool_enabled == chart_tool_enabled+ {+ return Ok((resident.session.clone(), resident.support.clone(), resident.naming.clone()));+ }+ // Tools survive reset(). Drop the old KV state before creating the+ // replacement session, keeping the loaded model weights in the instance.+ let resident = guard.take().expect("resident session");+ drop(resident.session);+ state.forget_history();+ let (session, naming) = match create_session(+ &engine,+ &resident.instance,+ model_chat_naming_enabled,+ date_time_tool_enabled,+ chart_tool_enabled,+ )+ .await+ {+ Ok(created) => created,+ Err(error) => {+ emit_session_state(app, false, repo_id, false);+ return Err(error);+ },+ };+ let support = resident.support.clone();+ *guard = Some(ResidentSession {+ session: session.clone(),+ naming: naming.clone(),+ model_chat_naming_enabled,+ date_time_tool_enabled,+ chart_tool_enabled,+ ..resident+ });+ return Ok((session, support, naming));}let support = reasoning_support(&model);*guard = None;@@ -61,30 +113,123 @@ pub(super) async fn ensure_session(// would flash the spinner and the resident-model chip.emit_session_loading(app, LoadingStatus::Start, repo_id, None);let config = ChatConfig::create();- let session = match engine.chat(model.clone(), config).await {- Ok(session) => session,+ let loaded = async {+ let instance = engine.chat_instance(model.clone(), config).await?;+ let (session, naming) =+ create_session(&engine, &instance, model_chat_naming_enabled, date_time_tool_enabled, chart_tool_enabled)+ .await?;+ Ok::<_, AppError>((instance, session, naming))+ }+ .await;+ let (instance, session, naming) = match loaded {+ Ok(loaded) => loaded,Err(error) => {emit_session_loading(app, LoadingStatus::Error, repo_id, Some(error.to_string()));- return Err(error.into());+ return Err(error);},};- // uzu stores these as f32; widening leaves 0.6 as 0.6000000238.- let round = |v: Option<f64>| v.map(|v| (v * 10_000.0).round() / 10_000.0);- let sampling_defaults = session.sampling_defaults().await.map(|p| SamplingDefaults {- temperature: round(p.temperature),- top_k: p.top_k,- top_p: round(p.top_p),- min_p: round(p.min_p),- repetition_penalty: round(p.repetition_penalty),- suffix_repetition_length: p.suffix_repetition_length,- });*guard = Some(ResidentSession {repo_id: repo_id.to_string(),identifier: model.identifier.clone(),session: session.clone(),+ instance,+ model_chat_naming_enabled,+ date_time_tool_enabled,+ chart_tool_enabled,+ naming: naming.clone(),support: support.clone(),- sampling_defaults,});emit_session_state(app, true, repo_id, false);- Ok((session, support))+ Ok((session, support, naming))+}++fn resolve_tool_enabled(+ requested: Option<bool>,+ model_size: Option<i64>,+) -> bool {+ requested.unwrap_or_else(|| model_size.is_none_or(|size| size >= 2_000_000_000))+}++fn chat_naming_enabled(+ requested: bool,+ settings: &serde_json::Value,+) -> bool {+ requested && settings.get("modelChatNamingEnabled").and_then(serde_json::Value::as_bool) != Some(false)+}++async fn create_session(+ engine: &Engine,+ instance: &ChatInstance,+ model_chat_naming_enabled: bool,+ date_time_tool_enabled: bool,+ chart_tool_enabled: bool,+) -> AppResult<(ChatSession, Option<ChatNaming>)> {+ let mut session = engine.chat_with_instance(instance).await?;+ let supports_tools = session.supports_tool_calls().await;+ if date_time_tool_enabled && supports_tools {+ session.add_tool(get_current_date_time).await?;+ }+ if chart_tool_enabled && supports_tools {+ session.add_tool(show_chart).await?;+ }+ let naming = if model_chat_naming_enabled && supports_tools {+ let naming = ChatNaming::default();+ session.add_tool(naming.tool()).await?;+ Some(naming)+ } else {+ None+ };+ Ok((session, naming))+}++/// Returns current date and time in RFC 3339 format: YYYY-MM-DDTHH:MM:SSZ+#[uzu_tool_function]+fn get_current_date_time() -> String {+ chrono::Local::now().to_rfc3339()+}++#[cfg(test)]+mod tests {+ use uzu::session::tool::func_def::ToolDescriptor;++ use super::*;++ #[test]+ fn tool_defaults_follow_model_size_without_overriding_explicit_choices() {+ for (size, default_enabled) in+ [(Some(1_999_999_999), false), (Some(2_000_000_000), true), (Some(27_000_000_000), true), (None, true)]+ {+ assert_eq!(resolve_tool_enabled(None, size), default_enabled);+ assert!(resolve_tool_enabled(Some(true), size));+ assert!(!resolve_tool_enabled(Some(false), size));+ }+ }++ #[test]+ fn global_naming_switch_overrides_request_without_overriding_per_model_disable() {+ let mut settings = serde_json::json!({+ "modelChatNamingEnabled": false,+ "modelParams": {"test/model": {"modelChatNamingEnabled": true}},+ });+ let explicit_small_model_opt_in = resolve_tool_enabled(Some(true), Some(1_200_000_000));+ assert!(!chat_naming_enabled(explicit_small_model_opt_in, &settings));++ settings["modelChatNamingEnabled"] = true.into();+ assert!(chat_naming_enabled(explicit_small_model_opt_in, &settings));+ assert!(!chat_naming_enabled(false, &settings));+ assert!(chat_naming_enabled(true, &serde_json::json!({})));+ }++ #[tokio::test]+ async fn date_time_tool_returns_the_current_local_rfc3339_timestamp() {+ let tool: ToolDescriptor = get_current_date_time.into();+ assert_eq!(tool.name, "get_current_date_time");+ let before = chrono::Local::now();+ let result = tool.execute(serde_json::json!({}).into()).await.unwrap();+ let after = chrono::Local::now();+ let value: serde_json::Value = result.try_into().unwrap();+ let date = chrono::DateTime::parse_from_rfc3339(value.as_str().unwrap()).unwrap();+ assert!(date >= before && date <= after);+ assert_eq!(date.offset(), before.offset());+ }}diff --git a/apps/mirai-gui/desktop/src/chat/stream.rs b/apps/mirai-gui/desktop/src/chat/stream.rsindex 13d0154c0..b365d16e2 100644--- a/apps/mirai-gui/desktop/src/chat/stream.rs+++ b/apps/mirai-gui/desktop/src/chat/stream.rs@@ -1,26 +1,97 @@-use tauri::{AppHandle, ipc::Channel};+use tauri::{AppHandle, Manager, ipc::Channel};use uzu::{session::chat::{ChatSessionError, ChatSessionStreamChunk},types::{basic::ReasoningEffort,- session::chat::{ChatReply, ChatReplyConfig, ChatReplyFinishReason},+ session::chat::{ChatContentBlock, ChatReply, ChatReplyConfig, ChatReplyFinishReason},},};use super::{ChatState, RunPlan,messages::{assistant_history_message, build_messages, build_tail_messages, parsed_from, sanitize},+ naming::add_naming_instruction,payloads::{Parsed, RunEvent, RunStreamPayload, map_reply_stats, sampling_policy},session::ensure_session,+ transcript::Transcript,+};+use crate::{+ analytics::{AnalyticsState, Event},+ error::{AppError, AppResult},};-use crate::error::{AppError, AppResult};--const DEFAULT_CHAT_TOKENS_LIMIT: u32 = 8_192;// uzu turns LanguageModelStreamError::ContextOverflow into Backend { message },// so the variant cannot tell an overflow from any other backend failure.const ENGINE_CONTEXT_OVERFLOW: &str = "Context overflow";+#[derive(Default)]+struct ReplyText {+ completed_text: String,+ completed_reasoning: String,+ text: String,+ reasoning: String,+ tool_turn_finished: bool,+}++fn join_turns(+ first: &str,+ second: &str,+) -> String {+ match (first.is_empty(), second.is_empty()) {+ (true, _) => second.to_string(),+ (_, true) => first.to_string(),+ _ => format!("{first}\n\n{second}"),+ }+}++impl ReplyText {+ fn update(+ &mut self,+ reply: &ChatReply,+ ) {+ // Each automatic tool continuation starts a new snapshot at zero.+ // Preserve visible content from the completed assistant turn first.+ if self.tool_turn_finished {+ self.completed_text = join_turns(&self.completed_text, &self.text);+ self.completed_reasoning = join_turns(&self.completed_reasoning, &self.reasoning);+ }+ self.text = reply.message.text().unwrap_or_default();+ self.reasoning = reply.message.reasoning().unwrap_or_default();+ self.tool_turn_finished = reply.finish_reason == Some(ChatReplyFinishReason::ToolCalls);+ }++ fn raw(&self) -> (String, String) {+ (join_turns(&self.completed_text, &self.text), join_turns(&self.completed_reasoning, &self.reasoning))+ }++ fn effective(+ &self,+ promote_reasoning: bool,+ ) -> (String, String) {+ if promote_reasoning {+ // Tool-call reasoning is internal even when a model ignored the+ // disabled-reasoning preference for its final answer.+ (join_turns(&self.completed_text, &self.reasoning), self.completed_reasoning.clone())+ } else {+ self.raw()+ }+ }+}++fn final_parsed(+ text: &str,+ reasoning: &str,+ promoted_reasoning: bool,+) -> Option<Parsed> {+ let mut parsed = parsed_from(text, reasoning);+ if promoted_reasoning && let Some(parsed) = parsed.as_mut() {+ // The frontend merges parsed patches. Explicitly clear reasoning that+ // was streamed before we knew it belonged in the final answer.+ parsed.chain_of_thought = Some(sanitize(reasoning.trim()));+ }+ parsed+}+fn stream_error(error: ChatSessionError) -> AppError {match &error {ChatSessionError::Backend {@@ -39,7 +110,15 @@ pub(super) async fn run_stream_inner(on_event: &Channel<RunEvent>,) -> AppResult<()> {let _run_guard = state.run_lock.lock().await;- let (session, support) = ensure_session(app, state, &payload.repo_id).await?;+ let (session, support, naming) = ensure_session(+ app,+ state,+ &payload.repo_id,+ payload.model_chat_naming_enabled,+ payload.date_time_tool_enabled,+ payload.chart_tool_enabled,+ )+ .await?;if state.cancel_requested(&payload.run_id) {crate::logger::info("chat:run:cancelled-before-start", Some(serde_json::json!({ "runId": payload.run_id })));let _ = on_event.send(RunEvent::Done {@@ -47,14 +126,14 @@ pub(super) async fn run_stream_inner(stats: map_reply_stats(None),finish_reason: Some(ChatReplyFinishReason::Cancelled),parsed: None,+ chat_name: None,+ transcript: Vec::new(),});return Ok(());}let effort = support.effective(payload.reasoning_effort);- let reply_config = ChatReplyConfig::create()- .with_token_limit(Some(DEFAULT_CHAT_TOKENS_LIMIT))- .with_sampling_policy(sampling_policy(&payload.sampling_policy));+ let reply_config = ChatReplyConfig::create().with_sampling_policy(sampling_policy(&payload.sampling_policy));let plan = state.plan_run(&payload.repo_id, &payload.messages, effort);crate::logger::info(@@ -79,87 +158,116 @@ pub(super) async fn run_stream_inner(state.evict(app, &payload.repo_id).await;return Err(error.into());}- build_messages(&payload.messages, effort)+ let mut messages = build_messages(&payload.messages, effort);+ if naming.is_some() {+ add_naming_instruction(&mut messages);+ }+ messages},};state.forget_history();-+ let naming_run = naming.as_ref().map(|naming| naming.begin(on_event.clone()));+ let (model_id, tools_enabled) = state+ .session+ .lock()+ .await+ .as_ref()+ .map(|resident| {+ (+ Some(resident.identifier.clone()),+ resident.model_chat_naming_enabled || resident.date_time_tool_enabled || resident.chart_tool_enabled,+ )+ })+ .unwrap_or_default();+ let tools_enabled = tools_enabled && session.supports_tool_calls().await;+ if let Some(model_id) = &model_id {+ app.state::<AnalyticsState>().report(|| Event::InferenceStarted {+ model_id: model_id.clone(),+ });+ }let stream = session.reply_with_stream(messages, reply_config).await;state.attach_cancel_token(&payload.run_id, stream.cancel_token());let should_promote_reasoning = effort == Some(ReasoningEffort::Disabled);- let effective_channels = |reply: Option<&ChatReply>| -> (String, String) {- let text = reply.and_then(|r| r.message.text()).unwrap_or_default();- let reasoning = reply.and_then(|r| r.message.reasoning()).unwrap_or_default();- if should_promote_reasoning && text.is_empty() && !reasoning.is_empty() {- (reasoning, String::new())- } else {- (text, reasoning)- }- };-let mut output: Option<ChatReply> = None;- let mut last_parsed: Option<Parsed> = None;- let mut prev_text_len = 0usize;- let mut prev_reasoning_len = 0usize;+ let mut reply_text = ReplyText::default();+ let mut transcript = Transcript::default();while let Some(chunk) = stream.next().await {match chunk {ChatSessionStreamChunk::Error {error,} => {+ transcript.flush_reply(output.as_ref());+ let _ = on_event.send(RunEvent::Transcript {+ items: transcript.items(true),+ });return Err(stream_error(error));},+ ChatSessionStreamChunk::ToolResults {+ messages,+ } => {+ transcript.tool_results(messages);+ let _ = on_event.send(RunEvent::Transcript {+ items: transcript.items(false),+ });+ },ChatSessionStreamChunk::Replies {replies,} => {- let reply = replies.into_iter().next();- if reply.is_some() {- output = reply;- }- let (text, reasoning) = effective_channels(output.as_ref());- last_parsed = parsed_from(&text, &reasoning);- let text_grew = text.len() > prev_text_len;- let reasoning_grew = reasoning.len() > prev_reasoning_len;- // A previous length off a char boundary means a re-sent snapshot; skip- // the delta, Done carries the full text.- let delta = if text_grew {- text.get(prev_text_len..).unwrap_or("")- } else {- ""- };- if text_grew {- prev_text_len = text.len();- }- if reasoning_grew {- prev_reasoning_len = reasoning.len();- }- if text_grew || reasoning_grew {- let parsed_patch = reasoning_grew.then(|| parsed_from("", &reasoning)).flatten();- let _ = on_event.send(RunEvent::Chunk {- delta: sanitize(delta),- parsed: parsed_patch,- });+ if let Some(reply) = replies.into_iter().next() {+ let has_text = reply+ .message+ .content+ .iter()+ .any(|block| matches!(block, ChatContentBlock::Text { value } if !value.is_empty()));+ let has_reasoning = reply+ .message+ .content+ .iter()+ .any(|block| matches!(block, ChatContentBlock::Reasoning { value } if !value.is_empty()));+ let promote_while_streaming =+ !tools_enabled && should_promote_reasoning && !has_text && has_reasoning;+ if let Some(event) = transcript.stream_update(output.as_ref(), &reply, promote_while_streaming) {+ let _ = on_event.send(event);+ }+ if reply.finish_reason.is_some() {+ reply_text.update(&reply);+ }+ output = Some(reply);}},}}- let raw_text = output.as_ref().and_then(|r| r.message.text()).unwrap_or_default();- let raw_reasoning = output.as_ref().and_then(|r| r.message.reasoning()).unwrap_or_default();- let promoted_reasoning = should_promote_reasoning && raw_text.is_empty() && !raw_reasoning.is_empty();- let (text, reasoning) = effective_channels(output.as_ref());+ transcript.flush_reply(output.as_ref());+ if let Some(reply) = output.as_ref().filter(|reply| reply.finish_reason.is_none()) {+ reply_text.update(reply);+ }let finish_reason = output.as_ref().and_then(|r| r.finish_reason.as_ref());+ let (raw_text, raw_reasoning) = reply_text.raw();+ let promoted_reasoning = should_promote_reasoning+ && reply_text.text.is_empty()+ && !reply_text.reasoning.is_empty()+ && finish_reason.is_some_and(|reason| *reason != ChatReplyFinishReason::ToolCalls);+ let (text, reasoning) = reply_text.effective(promoted_reasoning);// Cancelled or truncated replies may diverge from what the client persists.- if let (Some(ChatReplyFinishReason::Stop), Some(assistant)) =- (finish_reason, assistant_history_message(&raw_text, &raw_reasoning, promoted_reasoning))+ if !stream.cancel_token().is_cancelled()+ && let (Some(ChatReplyFinishReason::Stop), Some(assistant)) =+ (finish_reason, assistant_history_message(&raw_text, &raw_reasoning, promoted_reasoning)){let mut history = payload.messages.clone();history.push(assistant);state.remember_history(&payload.repo_id, history, effort);}- let finish_reason = finish_reason.cloned();+ // Cancellation between a tool result and the next token has no assistant+ // snapshot of its own; the last reply still says ToolCalls.+ let finish_reason = if stream.cancel_token().is_cancelled() {+ Some(ChatReplyFinishReason::Cancelled)+ } else {+ finish_reason.cloned()+ };crate::logger::info("chat:run:done",Some(serde_json::json!({@@ -171,17 +279,28 @@ pub(super) async fn run_stream_inner(})),);+ transcript.set_reasoning_promotion(promoted_reasoning);+ if let (Some(model_id), Some(reply)) = (model_id, &output) {+ app.state::<AnalyticsState>().report(|| Event::InferenceFinished {+ model_id,+ stats: Box::new(reply.stats.clone().into()),+ });+ }let _ = on_event.send(RunEvent::Done {text: sanitize(&text),stats: map_reply_stats(output.as_ref()),finish_reason,- parsed: last_parsed.or_else(|| parsed_from(&text, &reasoning)),+ parsed: final_parsed(&text, &reasoning, promoted_reasoning),+ chat_name: naming_run.as_ref().and_then(|run| run.name()),+ transcript: transcript.items(true),});Ok(())}#[cfg(test)]mod tests {+ use uzu::types::session::chat::ChatMessage;+use super::*;fn backend(message: &str) -> ChatSessionError {@@ -201,4 +320,66 @@ mod tests {let error = stream_error(backend("No seed token"));assert_eq!(error.to_string(), "Backend error: No seed token");}++ fn reply(+ text: &str,+ reasoning: &str,+ finish_reason: Option<ChatReplyFinishReason>,+ ) -> ChatReply {+ ChatReply {+ message: ChatMessage::assistant().with_text(text.to_string()).with_reasoning(reasoning.to_string()),+ stats: Default::default(),+ finish_reason,+ }+ }++ #[test]+ fn final_text_preserves_completed_tool_turns_and_partial_last_turn() {+ let snapshots = [+ reply("Let", "First thought", None),+ reply("Let me check.", "First thought", Some(ChatReplyFinishReason::ToolCalls)),+ reply("", "Second", None),+ reply("", "Second thought", Some(ChatReplyFinishReason::ToolCalls)),+ reply("The", "Final thought", None),+ reply("The answer.", "Final thought", Some(ChatReplyFinishReason::Stop)),+ ];+ let mut text = ReplyText::default();+ for snapshot in &snapshots {+ if snapshot.finish_reason.is_some() {+ text.update(snapshot);+ }+ }+ assert_eq!(+ text.raw(),+ (+ "Let me check.\n\nThe answer.".to_string(),+ "First thought\n\nSecond thought\n\nFinal thought".to_string()+ )+ );++ let mut interrupted = ReplyText::default();+ for snapshot in &snapshots[..5] {+ if snapshot.finish_reason.is_some() {+ interrupted.update(snapshot);+ }+ }+ interrupted.update(&snapshots[4]);+ assert_eq!(+ interrupted.raw(),+ ("Let me check.\n\nThe".to_string(), "First thought\n\nSecond thought\n\nFinal thought".to_string())+ );+ }++ #[test]+ fn reasoning_promotion_keeps_tool_reasoning_out_of_the_answer() {+ let mut text = ReplyText::default();+ text.update(&reply("", "Name the chat", Some(ChatReplyFinishReason::ToolCalls)));+ text.update(&reply("", "Actual answer", Some(ChatReplyFinishReason::Stop)));+ assert_eq!(text.effective(true), ("Actual answer".to_string(), "Name the chat".to_string()));+ let parsed = final_parsed("Actual answer", "Name the chat", true).unwrap();+ assert_eq!(parsed.chain_of_thought.as_deref(), Some("Name the chat"));+ let parsed = final_parsed("Actual answer", "", true).unwrap();+ assert_eq!(parsed.chain_of_thought.as_deref(), Some(""));+ assert_eq!(parsed.response.as_deref(), Some("Actual answer"));+ }}diff --git a/apps/mirai-gui/desktop/src/chat/title.rs b/apps/mirai-gui/desktop/src/chat/title.rsindex 4f128b974..84e7db630 100644--- a/apps/mirai-gui/desktop/src/chat/title.rs+++ b/apps/mirai-gui/desktop/src/chat/title.rs@@ -24,7 +24,8 @@ pub(super) async fn title_gen_inner(payload: &TitleGenPayload,) -> AppResult<String> {let _run_guard = state.run_lock.lock().await;- let (session, support) = ensure_session(app, state, &payload.repo_id).await?;+ let (session, support, _) =+ ensure_session(app, state, &payload.repo_id, Some(false), Some(false), Some(false)).await?;if state.cancel_requested(TITLE_GEN_RUN_ID) {return Ok(String::new());}@@ -54,6 +55,9 @@ pub(super) async fn title_gen_inner(let mut output: Option<ChatReply> = None;while let Some(chunk) = stream.next().await {match chunk {+ ChatSessionStreamChunk::ToolResults {+ ..+ } => {},ChatSessionStreamChunk::Error {error,} => return Err(error.into()),diff --git a/apps/mirai-gui/desktop/src/chat/transcript.rs b/apps/mirai-gui/desktop/src/chat/transcript.rsnew file mode 100644index 000000000..d87355bf2--- /dev/null+++ b/apps/mirai-gui/desktop/src/chat/transcript.rs@@ -0,0 +1,1018 @@+use uzu::types::{+ basic::ToolCall,+ session::chat::{ChatContentBlock, ChatMessage, ChatReply, ChatReplyFinishReason, ChatRole},+};++use super::{+ chart::ChartSpec,+ messages::sanitize,+ payloads::{RunEvent, TranscriptItem},+};++/// Only this run's messages: replayed history belongs to earlier UI messages.+/// Result events arrive before the next prefill, so completion never waits for+/// another generated token or the session's inference lock.+#[derive(Default)]+pub(super) struct Transcript {+ messages: Vec<ChatMessage>,+ finish_reason: Option<ChatReplyFinishReason>,+ promote_reasoning: bool,+ stream_snapshot_valid: bool,+ last_item_index: Option<usize>,+ reply_dirty: bool,+}++impl Transcript {+ /// Append to the last visible item while the current message keeps its+ /// structure. The caller owns the latest reply; materialize it only when a+ /// snapshot or final/error transcript needs the complete message history.+ pub(super) fn stream_update(+ &mut self,+ previous: Option<&ChatReply>,+ reply: &ChatReply,+ promote_reasoning: bool,+ ) -> Option<RunEvent> {+ if self.stream_snapshot_valid+ && self.promote_reasoning == promote_reasoning+ && reply.finish_reason.is_none()+ && let Some(previous) = previous.filter(|previous| previous.finish_reason.is_none())+ && let Some(delta) = visible_delta(&previous.message, &reply.message, promote_reasoning)+ && (delta.is_empty() || self.last_item_index.is_some())+ {+ self.reply_dirty = true;+ return (!delta.is_empty()).then(|| RunEvent::TranscriptDelta {+ index: self.last_item_index.expect("visible text item"),+ delta,+ });+ }++ self.update(reply);+ self.promote_reasoning = promote_reasoning;+ let items = self.items(false);+ self.last_item_index = items.len().checked_sub(1);+ self.stream_snapshot_valid = true;+ Some(RunEvent::Transcript {+ items,+ })+ }++ pub(super) fn flush_reply(+ &mut self,+ reply: Option<&ChatReply>,+ ) {+ if self.reply_dirty+ && let Some(reply) = reply+ {+ self.update(reply);+ }+ }++ pub(super) fn update(+ &mut self,+ reply: &ChatReply,+ ) {+ if self.finish_reason.is_some() || self.messages.is_empty() {+ self.messages.push(reply.message.clone());+ } else {+ *self.messages.last_mut().expect("current assistant message") = reply.message.clone();+ }+ self.finish_reason = reply.finish_reason.clone();+ self.reply_dirty = false;+ self.stream_snapshot_valid = false;+ }++ pub(super) fn tool_results(+ &mut self,+ messages: Vec<ChatMessage>,+ ) {+ self.messages.extend(messages);+ self.finish_reason = Some(ChatReplyFinishReason::ToolCalls);+ self.stream_snapshot_valid = false;+ }++ pub(super) fn set_reasoning_promotion(+ &mut self,+ enabled: bool,+ ) {+ if self.promote_reasoning != enabled {+ self.promote_reasoning = enabled;+ self.stream_snapshot_valid = false;+ }+ }++ pub(super) fn items(+ &self,+ finished: bool,+ ) -> Vec<TranscriptItem> {+ let last_assistant = self.messages.iter().rposition(|message| message.role == (ChatRole::Assistant {}));+ let mut items = Vec::new();+ let mut pending: Vec<(&ToolCall, Option<usize>)> = Vec::new();+ for (message_index, message) in self.messages.iter().enumerate() {+ if message.role == (ChatRole::Assistant {}) {+ let last = Some(message_index) == last_assistant;+ for block in &message.content {+ match block {+ ChatContentBlock::Reasoning {+ value,+ } if !value.trim().is_empty() => {+ if last && self.promote_reasoning {+ complete_thinking(&mut items);+ items.push(TranscriptItem::Text {+ text: sanitize(value),+ });+ } else {+ let text = sanitize(value.trim());+ if let Some(TranscriptItem::Thinking {+ text: previous,+ ..+ }) = items.last_mut()+ {+ previous.push_str("\n\n");+ previous.push_str(&text);+ } else {+ items.push(TranscriptItem::Thinking {+ text,+ completed: false,+ });+ }+ }+ },+ ChatContentBlock::Text {+ value,+ } if !value.trim().is_empty() => {+ complete_thinking(&mut items);+ items.push(TranscriptItem::Text {+ text: sanitize(value),+ });+ },+ ChatContentBlock::ToolCall {+ value,+ } => {+ let item_index = if hidden_call(value) {+ None+ } else {+ complete_thinking(&mut items);+ let index = items.len();+ items.push(TranscriptItem::ToolCall {+ name: value.name.clone(),+ called: false,+ failed: false,+ });+ Some(index)+ };+ // Hidden calls still consume their own results.+ pending.push((value, item_index));+ },+ _ => {},+ }+ }+ } else if message.role == (ChatRole::Tool {}) {+ for block in &message.content {+ let ChatContentBlock::ToolCallResult {+ identifier,+ name,+ value,+ } = block+ else {+ continue;+ };+ // IDs take precedence. When a template omits IDs, consume+ // just one matching name in call order, never every call.+ let exact = identifier+ .as_ref()+ .and_then(|id| pending.iter().position(|(call, _)| call.identifier.as_ref() == Some(id)));+ let matched = exact.or_else(|| {+ let name = name.as_ref()?;+ pending.iter().position(|(call, _)| {+ (identifier.is_none() || call.identifier.is_none()) && &call.name == name+ })+ });+ if let Some(index) = matched {+ let (call, item_index) = pending.remove(index);+ if let Some(index) = item_index {+ let result = serde_json::from_str::<serde_json::Value>(&value.json).ok();+ let failed = result.as_ref().is_some_and(|value| value.get("error").is_some());+ let chart = if call.name == "show_chart" && !failed {+ result+ .and_then(|value| serde_json::from_value::<ChartSpec>(value).ok())+ .filter(|chart| chart.validate().is_ok())+ } else {+ None+ };+ items[index] = match chart {+ Some(chart) => TranscriptItem::Chart {+ chart,+ },+ None => TranscriptItem::ToolCall {+ name: call.name.clone(),+ called: true,+ failed: failed || call.name == "show_chart",+ },+ };+ }+ }+ }+ }+ }+ // ToolCalls starts another assistant turn. Hidden housekeeping creates+ // no visible boundary: leave thinking open until content resumes or+ // the whole run ends, including cancellation and errors.+ if finished || self.finish_reason.as_ref().is_some_and(|reason| *reason != ChatReplyFinishReason::ToolCalls) {+ complete_thinking(&mut items);+ }+ items+ }+}++/// Return only an appended suffix, or require a snapshot if any visible block+/// changes shape. Invisible candidates and housekeeping calls do not interrupt+/// streaming. A decoder correction (including a replaced partial Unicode+/// character) falls back to a snapshot rather than using byte offsets blindly.+fn visible_delta(+ previous: &ChatMessage,+ current: &ChatMessage,+ promote_reasoning: bool,+) -> Option<String> {+ if previous.role != (ChatRole::Assistant {})+ || current.role != previous.role+ || current.metadata != previous.metadata+ {+ return None;+ }+ let visible = |block: &&ChatContentBlock| match block {+ ChatContentBlock::Reasoning {+ value,+ }+ | ChatContentBlock::Text {+ value,+ } => !value.trim().is_empty(),+ ChatContentBlock::ToolCall {+ value,+ } => !hidden_call(value),+ _ => false,+ };+ let mut previous = previous.content.iter().filter(visible).peekable();+ let mut current = current.content.iter().filter(visible).peekable();+ while let Some(before) = previous.next() {+ let after = current.next()?;+ let last = previous.peek().is_none();+ if last != current.peek().is_none() {+ return None;+ }+ if before == after {+ continue;+ }+ if !last {+ return None;+ }+ let (before, after) = match (before, after) {+ (+ ChatContentBlock::Text {+ value: before,+ },+ ChatContentBlock::Text {+ value: after,+ },+ ) => (before.as_str(), after.as_str()),+ (+ ChatContentBlock::Reasoning {+ value: before,+ },+ ChatContentBlock::Reasoning {+ value: after,+ },+ ) => {+ if promote_reasoning {+ (before.as_str(), after.as_str())+ } else {+ (before.trim(), after.trim())+ }+ },+ _ => return None,+ };+ return after.strip_prefix(before).map(sanitize);+ }+ current.next().is_none().then(String::new)+}++fn complete_thinking(items: &mut [TranscriptItem]) {+ if let Some(TranscriptItem::Thinking {+ completed,+ ..+ }) = items.last_mut()+ {+ *completed = true;+ }+}++fn hidden_call(call: &ToolCall) -> bool {+ if call.name != "set_chat_name" {+ return false;+ }+ let Ok(arguments) = serde_json::from_str::<serde_json::Value>(&call.arguments.json) else {+ return false;+ };+ // Match ToolDescriptor's boolean coercion for markup-based templates.+ match arguments.get("hidden") {+ Some(serde_json::Value::Bool(value)) => *value,+ Some(serde_json::Value::String(value)) => value.trim().eq_ignore_ascii_case("true"),+ _ => false,+ }+}++#[cfg(test)]+mod tests {+ use uzu::types::{basic::Value, session::chat::ChatReplyFinishReason};++ use super::*;++ fn call(+ name: &str,+ id: Option<&str>,+ ) -> ToolCall {+ ToolCall {+ identifier: id.map(str::to_string),+ name: name.to_string(),+ arguments: Value::from(serde_json::json!({})),+ }+ }++ fn naming_call(hidden: serde_json::Value) -> ToolCall {+ ToolCall {+ arguments: serde_json::json!({ "name": "A name", "hidden": hidden }).into(),+ ..call("set_chat_name", None)+ }+ }++ fn result(+ name: Option<&str>,+ id: Option<&str>,+ ) -> ChatMessage {+ ChatMessage::tool().with_block(ChatContentBlock::ToolCallResult {+ identifier: id.map(str::to_string),+ name: name.map(str::to_string),+ value: Value::from(serde_json::json!("result")),+ })+ }++ fn reply(+ message: ChatMessage,+ finished: bool,+ ) -> ChatReply {+ ChatReply {+ message,+ stats: Default::default(),+ finish_reason: finished.then_some(ChatReplyFinishReason::ToolCalls),+ }+ }++ #[derive(Default)]+ struct StreamOracle {+ transcript: Transcript,+ oracle: Transcript,+ displayed: Vec<TranscriptItem>,+ previous: Option<ChatReply>,+ wire_bytes: usize,+ }++ impl StreamOracle {+ fn send(+ &mut self,+ reply: ChatReply,+ promote_reasoning: bool,+ ) -> Option<serde_json::Value> {+ let event = self.transcript.stream_update(self.previous.as_ref(), &reply, promote_reasoning);+ self.oracle.update(&reply);+ self.oracle.set_reasoning_promotion(promote_reasoning);+ let serialized = event.as_ref().map(|event| {+ let json = serde_json::to_string(event).unwrap();+ self.wire_bytes += json.len();+ serde_json::from_str::<serde_json::Value>(&json).unwrap()+ });+ match event {+ Some(RunEvent::Transcript {+ items,+ }) => self.displayed = items,+ Some(RunEvent::TranscriptDelta {+ index,+ delta,+ }) => match &mut self.displayed[index] {+ TranscriptItem::Text {+ text,+ }+ | TranscriptItem::Thinking {+ text,+ ..+ } => text.push_str(&delta),+ item => panic!("delta targets a non-text item: {item:?}"),+ },+ None => {},+ _ => panic!("unexpected transcript event"),+ }+ assert_eq!(self.displayed, self.oracle.items(false));+ self.previous = Some(reply);+ serialized+ }++ fn tool_results(+ &mut self,+ results: Vec<ChatMessage>,+ ) {+ self.transcript.flush_reply(self.previous.as_ref());+ self.transcript.tool_results(results.clone());+ self.oracle.tool_results(results);+ self.displayed = self.transcript.items(false);+ assert_eq!(self.displayed, self.oracle.items(false));+ }++ fn finish(&mut self) {+ self.transcript.flush_reply(self.previous.as_ref());+ assert_eq!(self.transcript.items(true), self.oracle.items(true));+ }+ }++ #[test]+ fn stream_deltas_preserve_whitespace_unicode_and_decoder_corrections() {+ for reasoning in [false, true] {+ let mut stream = StreamOracle::default();+ for text in [+ "",+ " ",+ " A",+ " A ",+ " A \n",+ " A \nB",+ " A \nB�",+ " A \nB🙂",+ " A \nB🙂\u{200d}",+ " A \nB🙂\u{200d}↔\u{fe0f}",+ " A \nB🙂\u{200d}↔\u{fe0f}\u{0000}",+ "Replaced",+ "R",+ ] {+ let message = if reasoning {+ ChatMessage::assistant().with_reasoning(text.into())+ } else {+ ChatMessage::assistant().with_text(text.into())+ };+ stream.send(reply(message, false), false);+ }+ stream.finish();+ }+ }++ #[test]+ fn growing_text_keeps_the_owned_reply_only_at_snapshot_boundaries() {+ let mut stream = StreamOracle::default();+ let initial = ChatMessage::assistant().with_text("Hello".into());+ stream.send(reply(initial.clone(), false), false);+ let event = stream.send(reply(ChatMessage::assistant().with_text("Hello world".into()), false), false);+ assert_eq!(event.unwrap(), serde_json::json!({ "type": "transcriptDelta", "index": 0, "delta": " world" }));+ assert_eq!(stream.transcript.messages, [initial]);+ assert!(stream.transcript.reply_dirty);+ stream.finish();+ assert!(!stream.transcript.reply_dirty);+ }++ #[test]+ fn hidden_naming_candidates_and_merged_reasoning_stream_without_extra_snapshots() {+ let mut stream = StreamOracle::default();+ let initial = ChatMessage::assistant().with_reasoning("Before".into());+ stream.send(reply(initial.clone(), false), false);+ for candidate in ["<tool_call>", "<tool_call>set_chat_name"] {+ assert!(+ stream+ .send(+ reply(+ initial.with_block(ChatContentBlock::ToolCallCandidate {+ value: serde_json::json!(candidate).into(),+ }),+ false,+ ),+ false,+ )+ .is_none()+ );+ }+ stream.send(reply(initial.with_tool_call(naming_call(serde_json::json!(true))), true), false);+ stream.tool_results(vec![result(Some("set_chat_name"), None)]);+ stream.send(reply(ChatMessage::assistant().with_reasoning("After ".into()), false), false);+ let event = stream.send(reply(ChatMessage::assistant().with_reasoning("After naming".into()), false), false);+ assert_eq!(event.unwrap(), serde_json::json!({ "type": "transcriptDelta", "index": 0, "delta": " naming" }));+ assert_eq!(+ stream.displayed,+ [TranscriptItem::Thinking {+ text: "Before\n\nAfter naming".into(),+ completed: false,+ }]+ );+ stream.send(+ reply(ChatMessage::assistant().with_reasoning("After naming".into()).with_text("Answer".into()), false),+ false,+ );+ stream.finish();+ }++ #[test]+ fn structural_changes_promotion_and_terminal_metadata_require_snapshots() {+ let mut stream = StreamOracle::default();+ let message = ChatMessage::assistant().with_reasoning(" Answer ".into());+ stream.send(reply(message.clone(), false), false);+ let promoted = stream.send(reply(message, false), true).unwrap();+ assert_eq!(promoted["type"], "transcript");+ let message = ChatMessage::assistant().with_reasoning(" Answer now ".into());+ assert_eq!(stream.send(reply(message.clone(), false), true).unwrap()["type"], "transcriptDelta");+ assert_eq!(stream.send(reply(message, false), false).unwrap()["type"], "transcript");+ let message = ChatMessage::assistant().with_reasoning(" Answer now ".into()).with_text("Visible".into());+ assert_eq!(stream.send(reply(message, false), false).unwrap()["type"], "transcript");+ let mut message = ChatMessage::assistant().with_reasoning("Corrected".into()).with_text("Visible text".into());+ assert_eq!(stream.send(reply(message.clone(), false), false).unwrap()["type"], "transcript");+ message.metadata.values.insert("test".into(), serde_json::json!(true).into());+ assert_eq!(stream.send(reply(message.clone(), false), false).unwrap()["type"], "transcript");+ let mut terminal = reply(message, false);+ terminal.finish_reason = Some(ChatReplyFinishReason::Cancelled);+ assert_eq!(stream.send(terminal, false).unwrap()["type"], "transcript");+ stream.finish();++ // Cancellation and errors may arrive without a final reply. Flush the+ // latest streamed suffix before completing the current thinking item.+ for promote in [false, true] {+ let mut stream = StreamOracle::default();+ stream.send(reply(ChatMessage::assistant().with_reasoning("Partial".into()), false), promote);+ stream.send(reply(ChatMessage::assistant().with_reasoning("Partial answer".into()), false), promote);+ stream.finish();+ }+ }++ #[test]+ fn serialized_stream_grows_linearly_and_deltas_exclude_prior_charts() {+ let measure = |length: usize| {+ let mut stream = StreamOracle::default();+ stream.send(reply(ChatMessage::assistant().with_tool_call(call("show_chart", Some("chart"))), true), false);+ stream.tool_results(vec![+ ChatMessage::tool().with_block(ChatContentBlock::ToolCallResult {+ identifier: Some("chart".into()),+ name: Some("show_chart".into()),+ value: serde_json::json!({+ "type": "bar", "title": "Previously displayed chart", "labels": ["A"],+ "datasets": [{ "label": "Values", "data": [2] }],+ })+ .into(),+ }),+ ]);+ assert!(matches!(stream.displayed[0], TranscriptItem::Chart { .. }));+ let mut text = String::new();+ for index in 0..length {+ text.push('x');+ let event = stream.send(reply(ChatMessage::assistant().with_text(text.clone()), false), false).unwrap();+ if index > 0 {+ assert_eq!(event, serde_json::json!({ "type": "transcriptDelta", "index": 1, "delta": "x" }));+ }+ }+ stream.finish();+ stream.wire_bytes+ };+ let shorter = measure(512);+ let longer = measure(1024);+ assert!(longer < 2 * shorter);+ assert!(longer < 100 * 1024, "serialized {longer} bytes for 1024 generated characters");+ }++ #[test]+ fn chronological_transcript_completes_tools_before_another_assistant_token() {+ let mut transcript = Transcript::default();+ transcript.update(&reply(ChatMessage::assistant().with_reasoning("Need time".into()), false));+ assert_eq!(+ transcript.items(false),+ [TranscriptItem::Thinking {+ text: "Need time".into(),+ completed: false+ }]+ );+ transcript.update(&reply(+ ChatMessage::assistant()+ .with_reasoning("Need time".into())+ .with_text("Checking.".into())+ .with_tool_call(call("get_current_date_time", Some("date"))),+ true,+ ));+ let initial = transcript.items(false);+ assert_eq!(+ initial[0],+ TranscriptItem::Thinking {+ text: "Need time".into(),+ completed: true+ }+ );+ assert_eq!(+ initial[1],+ TranscriptItem::Text {+ text: "Checking.".into()+ }+ );+ assert_eq!(+ initial[2],+ TranscriptItem::ToolCall {+ name: "get_current_date_time".into(),+ called: false,+ failed: false+ }+ );+ transcript.tool_results(vec![result(Some("get_current_date_time"), Some("date"))]);+ assert_eq!(+ transcript.items(false)[2],+ TranscriptItem::ToolCall {+ name: "get_current_date_time".into(),+ called: true,+ failed: false+ }+ );+ transcript.update(&reply(ChatMessage::assistant().with_text("It is noon.".into()), false));+ let items = transcript.items(true);+ assert_eq!(items.len(), 4);+ assert_eq!(+ items[3],+ TranscriptItem::Text {+ text: "It is noon.".into()+ }+ );+ }++ #[test]+ fn hidden_naming_bridges_thinking_without_collapsing_between_turns() {+ let mut transcript = Transcript::default();+ let message = ChatMessage::assistant().with_reasoning("A name".into()).with_text("\n".into());+ transcript.update(&reply(message.with_tool_call_candidate(serde_json::json!("partial call").into()), false));+ let thinking = |text: &str, completed| {+ vec![TranscriptItem::Thinking {+ text: text.into(),+ completed,+ }]+ };+ assert_eq!(transcript.items(false), thinking("A name", false));+ transcript.update(&reply(message.with_tool_call(naming_call(serde_json::json!(true))), true));+ assert_eq!(transcript.items(false), thinking("A name", false));+ transcript.tool_results(vec![result(Some("set_chat_name"), None)]);+ assert_eq!(transcript.items(false), thinking("A name", false));+ assert_eq!(transcript.items(true), thinking("A name", true));+ transcript.update(&reply(ChatMessage::assistant().with_reasoning("Answer".into()), false));+ assert_eq!(transcript.items(false), thinking("A name\n\nAnswer", false));+ transcript.update(&reply(ChatMessage::assistant().with_reasoning("Answer reasoning".into()), false));+ assert_eq!(transcript.items(false), thinking("A name\n\nAnswer reasoning", false));+ transcript.update(&reply(+ ChatMessage::assistant().with_reasoning("Answer reasoning".into()).with_text("The answer.".into()),+ false,+ ));+ assert_eq!(+ transcript.items(false),+ [+ TranscriptItem::Thinking {+ text: "A name\n\nAnswer reasoning".into(),+ completed: true+ },+ TranscriptItem::Text {+ text: "The answer.".into()+ }+ ]+ );+ }++ #[test]+ fn explicit_naming_stays_visible_and_separates_thinking() {+ let mut transcript = Transcript::default();+ transcript.update(&reply(+ ChatMessage::assistant()+ .with_reasoning("Requested rename".into())+ .with_tool_call(naming_call(serde_json::json!(false))),+ true,+ ));+ transcript.tool_results(vec![result(Some("set_chat_name"), None)]);+ transcript.update(&reply(ChatMessage::assistant().with_reasoning("Now answer".into()), false));+ assert_eq!(+ transcript.items(false),+ [+ TranscriptItem::Thinking {+ text: "Requested rename".into(),+ completed: true+ },+ TranscriptItem::ToolCall {+ name: "set_chat_name".into(),+ called: true,+ failed: false+ },+ TranscriptItem::Thinking {+ text: "Now answer".into(),+ completed: false+ },+ ]+ );+ }++ #[test]+ fn only_true_hidden_values_hide_naming_calls() {+ let calls = [+ call("set_chat_name", None),+ naming_call(serde_json::json!(false)),+ naming_call(serde_json::json!("false")),+ naming_call(serde_json::json!("maybe")),+ naming_call(serde_json::json!(1)),+ naming_call(serde_json::Value::Null),+ ToolCall {+ arguments: Value {+ json: "not json".into(),+ },+ ..call("set_chat_name", None)+ },+ ToolCall {+ arguments: serde_json::json!({ "hidden": true }).into(),+ ..call("get_current_date_time", None)+ },+ ];+ for call in calls {+ assert!(!hidden_call(&call));+ let mut transcript = Transcript::default();+ transcript.update(&reply(ChatMessage::assistant().with_tool_call(call), true));+ assert!(matches!(+ transcript.items(false).as_slice(),+ [TranscriptItem::ToolCall {+ called: false,+ ..+ }]+ ));+ }+ assert!(hidden_call(&naming_call(serde_json::json!(true))));+ assert!(hidden_call(&naming_call(serde_json::json!(" TRUE "))));+ }++ #[test]+ fn terminal_assistant_reason_closes_thinking_without_visible_output() {+ let mut transcript = Transcript::default();+ let mut final_reply = reply(ChatMessage::assistant().with_reasoning("Finished".into()), false);+ final_reply.finish_reason = Some(ChatReplyFinishReason::Stop);+ transcript.update(&final_reply);+ assert_eq!(+ transcript.items(false),+ [TranscriptItem::Thinking {+ text: "Finished".into(),+ completed: true+ }]+ );+ }++ #[test]+ fn results_match_one_call_by_id_or_name_and_never_complete_unrelated_calls() {+ let mut transcript = Transcript::default();+ transcript.update(&reply(+ ChatMessage::assistant()+ .with_tool_call(naming_call(serde_json::json!(true)))+ .with_tool_call(call("get_current_date_time", None))+ .with_tool_call(call("get_current_date_time", None))+ .with_tool_call(call("get_current_date_time", Some("final"))),+ true,+ ));+ transcript.tool_results(vec![result(Some("set_chat_name"), None), result(Some("get_current_date_time"), None)]);+ let called = |transcript: &Transcript| {+ transcript+ .items(false)+ .iter()+ .map(|item| match item {+ TranscriptItem::ToolCall {+ called,+ ..+ } => *called,+ _ => panic!("only tool calls"),+ })+ .collect::<Vec<_>>()+ };+ assert_eq!(called(&transcript), [true, false, false]);+ transcript.tool_results(vec![result(None, Some("final"))]);+ assert_eq!(called(&transcript), [true, false, true]);+ transcript.tool_results(vec![result(None, None)]);+ assert_eq!(called(&transcript), [true, false, true]);+ transcript.tool_results(vec![result(Some("get_current_date_time"), None)]);+ assert_eq!(called(&transcript), [true, true, true]);+ }++ #[test]+ fn mismatched_ids_do_not_fall_back_to_a_shared_name() {+ let mut transcript = Transcript::default();+ transcript.update(&reply(ChatMessage::assistant().with_tool_call(call("date", Some("a"))), true));+ transcript.tool_results(vec![result(Some("date"), Some("b"))]);+ assert_eq!(+ transcript.items(false),+ [TranscriptItem::ToolCall {+ name: "date".into(),+ called: false,+ failed: false+ }]+ );+ }++ #[test]+ fn tool_errors_mark_only_the_matching_call_as_failed() {+ let mut transcript = Transcript::default();+ transcript.update(&reply(+ ChatMessage::assistant()+ .with_tool_call(call("set_chat_name", Some("failed")))+ .with_tool_call(call("set_chat_name", Some("succeeded")))+ .with_tool_call(call("set_chat_name", Some("pending"))),+ true,+ ));+ transcript.tool_results(vec![+ result(Some("set_chat_name"), Some("succeeded")),+ ChatMessage::tool().with_block(ChatContentBlock::ToolCallResult {+ identifier: Some("failed".into()),+ name: Some("set_chat_name".into()),+ value: serde_json::json!({ "error": "Chat names must contain 3 to 50 characters" }).into(),+ }),+ ]);+ let items = transcript.items(false);+ assert_eq!(+ items,+ [+ TranscriptItem::ToolCall {+ name: "set_chat_name".into(),+ called: true,+ failed: true,+ },+ TranscriptItem::ToolCall {+ name: "set_chat_name".into(),+ called: true,+ failed: false,+ },+ TranscriptItem::ToolCall {+ name: "set_chat_name".into(),+ called: false,+ failed: false,+ },+ ]+ );+ let serialized = serde_json::to_value(items).unwrap();+ assert_eq!(serialized[0]["failed"], true);+ assert!(serialized[1].get("failed").is_none());+ assert!(serialized[2].get("failed").is_none());+ }++ #[tokio::test]+ async fn completed_charts_replace_the_matching_call_in_transcript_order() {+ use uzu::session::tool::func_def::ToolDescriptor;++ let chart = |title: &str| {+ serde_json::json!({+ "type": "bar", "title": title, "labels": ["A"],+ "datasets": [{ "label": "Values", "data": [2] }],+ })+ };+ let first = chart("First chart");+ let second = chart("Second chart");+ let first_call = ToolCall {+ // The transcript must use the canonical result, including markup argument coercion.+ arguments: serde_json::json!({ "chart": first.to_string() }).into(),+ ..call("show_chart", Some("first"))+ };+ let second_call = ToolCall {+ arguments: serde_json::json!({ "chart": second }).into(),+ ..call("show_chart", Some("second"))+ };+ let tool: ToolDescriptor = super::super::chart::show_chart.into();+ let first_result = tool.execute(first_call.arguments.clone()).await.unwrap();+ let second_result = tool.execute(second_call.arguments.clone()).await.unwrap();+ let chart_result = |id: &str, value| {+ ChatMessage::tool().with_block(ChatContentBlock::ToolCallResult {+ identifier: Some(id.into()),+ name: Some("show_chart".into()),+ value,+ })+ };+ let mut transcript = Transcript::default();+ transcript.update(&reply(+ ChatMessage::assistant()+ .with_reasoning("Compare the data".into())+ .with_tool_call(first_call)+ .with_tool_call(call("get_current_date_time", Some("date")))+ .with_tool_call(second_call)+ .with_text("The comparison.".into()),+ true,+ ));+ transcript.tool_results(vec![chart_result("second", second_result)]);+ let items = transcript.items(false);+ assert!(matches!(+ items[1],+ TranscriptItem::ToolCall {+ called: false,+ ..+ }+ ));+ assert!(matches!(&items[3], TranscriptItem::Chart { chart } if chart.title == "Second chart"));+ transcript.tool_results(vec![+ chart_result("first", first_result),+ result(Some("get_current_date_time"), Some("date")),+ ]);+ let items = transcript.items(true);+ assert_eq!(items.len(), 5);+ assert!(matches!(+ items[0],+ TranscriptItem::Thinking {+ completed: true,+ ..+ }+ ));+ assert!(matches!(&items[1], TranscriptItem::Chart { chart } if chart.title == "First chart"));+ assert!(matches!(+ items[2],+ TranscriptItem::ToolCall {+ called: true,+ failed: false,+ ..+ }+ ));+ assert!(matches!(&items[3], TranscriptItem::Chart { chart } if chart.title == "Second chart"));+ assert!(matches!(&items[4], TranscriptItem::Text { text } if text == "The comparison."));+ let serialized = serde_json::to_value(&items[1]).unwrap();+ assert_eq!(serialized["type"], "chart");+ assert_eq!(serialized["chart"]["title"], "First chart");+ }++ #[test]+ fn chart_errors_and_invalid_results_remain_failed_tool_calls() {+ for value in [+ serde_json::json!({ "error": "Invalid chart" }),+ serde_json::json!("Chart displayed"),+ serde_json::json!({+ "type": "bar", "title": "Invalid", "labels": ["A", "B"],+ "datasets": [{ "label": "Values", "data": [1] }],+ }),+ serde_json::json!({+ "type": "bar", "title": "Invalid", "labels": ["A"],+ "datasets": [{ "label": "Values", "data": [1] }],+ "options": { "onClick": "alert(1)" },+ }),+ ] {+ let mut transcript = Transcript::default();+ transcript.update(&reply(ChatMessage::assistant().with_tool_call(call("show_chart", None)), true));+ transcript.tool_results(vec![ChatMessage::tool().with_block(ChatContentBlock::ToolCallResult {+ identifier: None,+ name: Some("show_chart".into()),+ value: value.into(),+ })]);+ assert_eq!(+ transcript.items(true),+ [TranscriptItem::ToolCall {+ name: "show_chart".into(),+ called: true,+ failed: true,+ }]+ );+ }+ }++ #[test]+ fn promotion_only_changes_the_current_assistant_turn_and_finishing_keeps_partial_activity() {+ let mut transcript = Transcript::default();+ transcript.update(&reply(+ ChatMessage::assistant().with_reasoning("Find time".into()).with_tool_call(call("date", None)),+ true,+ ));+ transcript.tool_results(vec![result(Some("date"), None)]);+ transcript.update(&reply(ChatMessage::assistant().with_reasoning("Actual answer".into()), false));+ transcript.set_reasoning_promotion(true);+ let items = transcript.items(true);+ assert_eq!(+ items[0],+ TranscriptItem::Thinking {+ text: "Find time".into(),+ completed: true+ }+ );+ assert_eq!(+ items[2],+ TranscriptItem::Text {+ text: "Actual answer".into()+ }+ );+ transcript.set_reasoning_promotion(false);+ assert_eq!(+ transcript.items(true)[2],+ TranscriptItem::Thinking {+ text: "Actual answer".into(),+ completed: true+ }+ );+ }++ #[test]+ fn error_keeps_reasoning_that_was_already_displayed_as_an_answer() {+ let mut transcript = Transcript::default();+ transcript.update(&reply(ChatMessage::assistant().with_reasoning("Partial answer".into()), false));+ transcript.set_reasoning_promotion(true);+ let displayed = transcript.items(false);+ assert_eq!(+ displayed,+ [TranscriptItem::Text {+ text: "Partial answer".into()+ }]+ );+ // An error finishes the existing view without reclassifying its text.+ assert_eq!(transcript.items(true), displayed);+ }+}diff --git a/apps/mirai-gui/desktop/src/cli_installer/mod.rs b/apps/mirai-gui/desktop/src/cli_installer/mod.rsindex 3371017a7..d778e108a 100644--- a/apps/mirai-gui/desktop/src/cli_installer/mod.rs+++ b/apps/mirai-gui/desktop/src/cli_installer/mod.rs@@ -111,7 +111,10 @@ fn install_declined() -> bool {}fn persist_declined(declined: bool) {- if let Err(e) = crate::storage::settings_patch(serde_json::json!({ "cliInstallDeclined": declined })) {+ if let Err(e) = crate::storage::settings_merge(serde_json::Map::from_iter([(+ "cliInstallDeclined".to_string(),+ declined.into(),+ )])) {crate::logger::warn("cli-installer:settings-write-failed",Some(serde_json::json!({ "declined": declined, "error": e })),diff --git a/apps/mirai-gui/desktop/src/downloads/events.rs b/apps/mirai-gui/desktop/src/downloads/events.rsindex 29b4de38e..9bffab5ba 100644--- a/apps/mirai-gui/desktop/src/downloads/events.rs+++ b/apps/mirai-gui/desktop/src/downloads/events.rs@@ -7,6 +7,12 @@ use crate::models::PhaseKind;#[derive(Serialize, Clone, PartialEq, Eq, Debug)]#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]pub(super) enum DownloadEvent {+ State {+ phase: PhaseKind,+ completed_bytes: i64,+ total_bytes: i64,+ error: Option<String>,+ },Progress {completed_bytes: i64,total_bytes: Option<i64>,@@ -36,7 +42,12 @@ pub(super) fn events_for(state: &DownloadState,previous: Option<PhaseKind>,) -> Vec<DownloadEvent> {+ // Initial inspection reports existing files without announcing a new download.+ if previous.is_none() || previous == Some(PhaseKind::Initializing) {+ return vec![snapshot(state)];+ }match &state.phase {+ DownloadPhase::Initializing {} => vec![snapshot(state)],DownloadPhase::Downloading {} => {let progress = DownloadEvent::Progress {completed_bytes: state.downloaded_bytes,@@ -79,6 +90,20 @@ pub(super) fn events_for(}}+fn snapshot(state: &DownloadState) -> DownloadEvent {+ DownloadEvent::State {+ phase: PhaseKind::from(&state.phase),+ completed_bytes: state.downloaded_bytes,+ total_bytes: state.total_bytes,+ error: match &state.phase {+ DownloadPhase::Error {+ message,+ } => Some(message.clone()),+ _ => None,+ },+ }+}+fn transition(previous: Option<PhaseKind>,entered: PhaseKind,@@ -113,7 +138,10 @@ pub(super) fn announce("download:state",Some(serde_json::json!({ "identifier": event.identifier, "kind": "locked", "lockedBy": locked_by })),),- DownloadEvent::Progress {+ DownloadEvent::State {+ ..+ }+ | DownloadEvent::Progress {..}| DownloadEvent::Paused@@ -213,9 +241,10 @@ mod tests {}#[test]- fn settled_phases_are_events_only_as_transitions() {+ fn initial_snapshots_are_quiet_and_later_transitions_are_announced() {let done = state(DownloadPhase::Downloaded {}, 100, 100);- assert_eq!(events_for(&done, None), vec![]);+ assert_eq!(events_for(&done, None), vec![snapshot(&done)]);+ assert_eq!(events_for(&done, Some(PhaseKind::Initializing)), vec![snapshot(&done)]);assert_eq!(events_for(&done, Some(PhaseKind::Downloaded)), vec![]);assert_eq!(events_for(&done, Some(PhaseKind::Downloading)), vec![DownloadEvent::Done]);@@ -226,7 +255,7 @@ mod tests {0,0,);- assert_eq!(events_for(&failed, None), vec![]);+ assert_eq!(events_for(&failed, None), vec![snapshot(&failed)]);assert_eq!(events_for(&failed, Some(PhaseKind::Downloading)),vec![DownloadEvent::Error {@@ -248,4 +277,12 @@ mod tests {}]);}++ #[test]+ fn reinitialization_between_downloaded_states_always_updates_the_client() {+ let initializing = state(DownloadPhase::Initializing {}, 0, 100);+ let done = state(DownloadPhase::Downloaded {}, 100, 100);+ assert_eq!(events_for(&initializing, Some(PhaseKind::Downloaded)), vec![snapshot(&initializing)]);+ assert_eq!(events_for(&done, Some(PhaseKind::Initializing)), vec![snapshot(&done)]);+ }}diff --git a/apps/mirai-gui/desktop/src/downloads/intent.rs b/apps/mirai-gui/desktop/src/downloads/intent.rsindex 5f1642bb4..edaf50601 100644--- a/apps/mirai-gui/desktop/src/downloads/intent.rs+++ b/apps/mirai-gui/desktop/src/downloads/intent.rs@@ -1,5 +1,6 @@-use std::collections::HashMap;+use std::collections::{HashMap, HashSet};+use futures::StreamExt;use tauri::{AppHandle, Manager};use uzu::{storage::DownloadPhase, types::model::Model};@@ -67,42 +68,66 @@ pub fn auto_resume_on_startup(app: AppHandle) {let Ok(engine) = engine().await else {return;};- let wanted: Vec<String> =+ let mut wanted: HashSet<String> =with_readiness(|map| map.iter().filter_map(|(k, v)| v.then_some(k.clone())).collect());if wanted.is_empty() {return;}- let Ok(models) = engine.models_for_chat().await else {- return;- };- for key in wanted {- let found = models.iter().filter(|m| m.is_on_device()).find(|m| m.repo_ids().contains(&key));- let Some(model) = found.cloned() else {- continue;+ let mut catalog_updates = engine.catalog_subscribe();+ let mut storage_updates = engine.storage_subscribe();+ loop {+ let Ok((models, complete)) = engine.catalog_snapshot().await else {+ return;};- let Some(state) = engine.download_state(&model).await else {- continue;- };- // NotDownloaded with intent set = the cache moved to a new- // checkpoint_version dir (engine update); re-fetch automatically.- if !matches!(- state.phase,- DownloadPhase::Paused {} | DownloadPhase::Downloading {} | DownloadPhase::NotDownloaded {}- ) {- continue;+ let mut waiting_for_storage = false;+ for key in wanted.clone() {+ if !with_readiness(|map| map.get(&key).copied().unwrap_or(false)) {+ wanted.remove(&key);+ continue;+ }+ let found = models.iter().filter(|m| m.is_on_device()).find(|m| m.repo_ids().contains(&key));+ let Some(model) = found.cloned() else {+ if complete {+ wanted.remove(&key);+ }+ continue;+ };+ let Some(state) = engine.download_state(&model).await else {+ continue;+ };+ if matches!(state.phase, DownloadPhase::Initializing {}) {+ waiting_for_storage = true;+ continue;+ }+ wanted.remove(&key);+ // NotDownloaded with intent set = the cache moved to a new+ // checkpoint_version dir (engine update); re-fetch automatically.+ if !matches!(+ state.phase,+ DownloadPhase::Paused {} | DownloadPhase::Downloading {} | DownloadPhase::NotDownloaded {}+ ) {+ continue;+ }+ let downloads = app.state::<DownloadsState>();+ downloads.remember_one(&model.identifier, &key).await;+ ensure_watcher(app.clone(), &downloads);+ match engine.downloader(&model).resume().await {+ Ok(()) => crate::logger::info(+ "download:auto-resume",+ Some(serde_json::json!({ "identifier": model.identifier })),+ ),+ Err(error) => crate::logger::warn(+ "download:auto-resume-failed",+ Some(serde_json::json!({ "identifier": model.identifier, "error": error.to_string() })),+ ),+ }+ }+ if wanted.is_empty() || (!engine.catalog_is_refreshing() && !waiting_for_storage) {+ break;}- let downloads = app.state::<DownloadsState>();- downloads.remember_one(&model.identifier, &key).await;- ensure_watcher(app.clone(), &downloads);- match engine.downloader(&model).resume().await {- Ok(()) => crate::logger::info(- "download:auto-resume",- Some(serde_json::json!({ "identifier": model.identifier })),- ),- Err(error) => crate::logger::warn(- "download:auto-resume-failed",- Some(serde_json::json!({ "identifier": model.identifier, "error": error.to_string() })),- ),+ tokio::select! {+ event = catalog_updates.next() => if event.is_none() { break; },+ event = storage_updates.next() => if event.is_none() { break; },}}});diff --git a/apps/mirai-gui/desktop/src/downloads/mod.rs b/apps/mirai-gui/desktop/src/downloads/mod.rsindex c92495fd9..cb5433d70 100644--- a/apps/mirai-gui/desktop/src/downloads/mod.rs+++ b/apps/mirai-gui/desktop/src/downloads/mod.rs@@ -15,6 +15,7 @@ use tauri::{AppHandle, Emitter, Manager};use uzu::types::model::Model;use crate::{+ analytics::{AnalyticsState, Event as AnalyticsEvent},engine::engine,error::AppResult,models::{PhaseKind, find_downloadable_model},@@ -78,11 +79,23 @@ impl DownloadsState {async fn repo_info(&self,identifier: &str,+ engine: &uzu::engine::Engine,) -> RepoInfo {- let map = self.repo_by_identifier.lock().await;- map.get(identifier).cloned().unwrap_or_else(|| RepoInfo {- event_key: identifier.to_string(),- })+ if let Some(info) = self.repo_by_identifier.lock().await.get(identifier).cloned() {+ return info;+ }+ // A model may finish its initial check before the UI fetches its catalog row.+ let event_key = engine+ .catalog_snapshot()+ .await+ .ok()+ .and_then(|(models, _)| models.into_iter().find(|model| model.identifier == identifier))+ .and_then(|model| model.repo_ids().first().cloned())+ .unwrap_or_else(|| identifier.to_string());+ self.remember_one(identifier, &event_key).await;+ RepoInfo {+ event_key,+ }}async fn remember_one(@@ -116,15 +129,41 @@ pub fn ensure_watcher(},};let mut stream = engine.storage_subscribe();- while let Some(item) = stream.next().await {+ let mut catalog = engine.catalog_subscribe();+ // Close the gap between the first command's snapshot and this task subscribing.+ let _ = app.emit("models-changed", ());+ loop {+ let item = tokio::select! {+ update = catalog.next() => {+ if update.is_none() { break; }+ let _ = app.emit("models-changed", ());+ continue;+ },+ item = stream.next() => {+ let Some(item) = item else { break; };+ item+ },+ };let Ok((identifier, download_state)) = item else {+ // A lagged storage subscriber recovers from a fresh snapshot.+ let _ = app.emit("models-changed", ());continue;};let downloads = app.state::<DownloadsState>();- let previous =- downloads.last_phase.lock().await.insert(identifier.clone(), PhaseKind::from(&download_state.phase));- let info = downloads.repo_info(&identifier).await;+ let phase = PhaseKind::from(&download_state.phase);+ let previous = downloads.last_phase.lock().await.insert(identifier.clone(), phase);+ let info = downloads.repo_info(&identifier, &engine).await;+ if phase == PhaseKind::Downloading && previous != Some(PhaseKind::Downloading) {+ app.state::<AnalyticsState>().report(|| AnalyticsEvent::DownloadStarted {+ model_id: identifier.clone(),+ });+ }for event in events_for(&download_state, previous) {+ if matches!(event, DownloadEvent::Done) {+ app.state::<AnalyticsState>().report(|| AnalyticsEvent::DownloadFinished {+ model_id: identifier.clone(),+ });+ }downloads.emit(&app, &info, event);}}diff --git a/apps/mirai-gui/desktop/src/lib.rs b/apps/mirai-gui/desktop/src/lib.rsindex a6791e8cf..1a400973b 100644--- a/apps/mirai-gui/desktop/src/lib.rs+++ b/apps/mirai-gui/desktop/src/lib.rs@@ -1,3 +1,4 @@+mod analytics;mod chat;mod cli_installer;mod downloads;@@ -46,13 +47,13 @@ fn install_panic_logger() {}pub fn run() {+ use tauri::Manager;+install_panic_logger();tauri::Builder::default()- .plugin(tauri_plugin_global_shortcut::Builder::new().build()).plugin(tauri_plugin_dialog::init()).plugin(tauri_plugin_notification::init()).plugin(tauri_plugin_updater::Builder::new().build())- .plugin(tauri_plugin_autostart::init(tauri_plugin_autostart::MacosLauncher::LaunchAgent, None)).setup(|app| {let window = tauri::WebviewWindowBuilder::new(app, "main", tauri::WebviewUrl::default()).title("Mirai")@@ -73,8 +74,7 @@ pub fn run() {false}).build()?;- // Closing hides the window but keeps the process alive so the global- // shortcut stays registered; ⌘Q quits.+ // Closing hides the window; the Dock reopens it and ⌘Q quits.let hide_target = window.clone();window.on_window_event(move |event| {if let tauri::WindowEvent::CloseRequested {@@ -87,11 +87,14 @@ pub fn run() {}});logger::info("app:start", Some(serde_json::json!({ "version": app.package_info().version.to_string() })));+ if let Err(error) = system_ui::remove_legacy_autostart() {+ logger::warn("autostart:cleanup", Some(serde_json::json!({ "error": error.to_string() })));+ }if let Err(error) = system_ui::setup_app_menu(app.handle()) {logger::error("menu:setup", Some(serde_json::json!({ "error": error.to_string() })));}- system_ui::restore_from_settings(app.handle());chat::restore_auto_eject_config(app.handle());+ app.state::<analytics::AnalyticsState>().restore();cli_installer::trigger_if_needed(app.handle());// Warm the engine so the first models/chat request doesn't pay init latency.tauri::async_runtime::spawn(async {@@ -101,6 +104,7 @@ pub fn run() {Ok(())}).manage(chat::ChatState::default())+ .manage(analytics::AnalyticsState::default()).manage(downloads::DownloadsState::default()).manage(updater::UpdaterState::default()).invoke_handler(tauri::generate_handler![@@ -108,6 +112,7 @@ pub fn run() {cli_installer::cli_install,cli_installer::cli_status,models::chat_models_get,+ models::chat_models_refresh,chat::run_stream,chat::title_gen,chat::cancel_run,@@ -132,11 +137,6 @@ pub fn run() {storage::model_params_set,storage::cleanup::cleanup_preview,storage::cleanup::cleanup_execute,- system_ui::get_run_on_startup,- system_ui::set_run_on_startup,- system_ui::register_quick_entry_shortcut,- system_ui::unregister_quick_entry_shortcut,- system_ui::get_quick_entry_shortcut,system_ui::set_window_theme,updater::update_check,updater::update_download,diff --git a/apps/mirai-gui/desktop/src/models.rs b/apps/mirai-gui/desktop/src/models.rsindex 9bad3ec04..9cd401757 100644--- a/apps/mirai-gui/desktop/src/models.rs+++ b/apps/mirai-gui/desktop/src/models.rs@@ -10,6 +10,7 @@ use uzu::{types::{basic::{Image, ImageFormat, ImageTheme, ReasoningEffort},model::{Model, ModelVendor},+ session::chat::ChatModelCapabilities,},};@@ -19,14 +20,18 @@ use crate::{};#[derive(Serialize, Clone, Debug, PartialEq, Default)]-#[serde(tag = "kind", rename_all = "camelCase")]+#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]pub enum ReasoningSupport {#[default]Unsupported,AlwaysOn,- Toggle,+ Toggle {+ default_effort: ReasoningEffort,+ },Levels {efforts: Vec<ReasoningEffort>,+ #[serde(skip_serializing_if = "Option::is_none")]+ default_effort: Option<ReasoningEffort>,},}@@ -37,36 +42,36 @@ impl ReasoningSupport {) -> Option<ReasoningEffort> {match self {ReasoningSupport::Unsupported | ReasoningSupport::AlwaysOn => None,- ReasoningSupport::Toggle => Some(if requested == Some(ReasoningEffort::Disabled) {- ReasoningEffort::Disabled- } else {- ReasoningEffort::Default- }),+ ReasoningSupport::Toggle {+ ..+ } => requested.filter(|effort| *effort == ReasoningEffort::Disabled),ReasoningSupport::Levels {efforts,+ ..} => requested- .filter(|effort| efforts.contains(effort))- .or_else(|| efforts.contains(&ReasoningEffort::Default).then_some(ReasoningEffort::Default))- // hanashi rejects an effort missing from the mapping, so never invent one.- .or_else(|| efforts.iter().copied().find(|effort| *effort != ReasoningEffort::Disabled)),+ // Leave default selection to the template, including mappings+ // whose default is not named as an explicit effort level.+ .filter(|effort| *effort != ReasoningEffort::Default && efforts.contains(effort)),}}pub fn cheapest(&self) -> Option<ReasoningEffort> {match self {ReasoningSupport::Unsupported | ReasoningSupport::AlwaysOn => None,- ReasoningSupport::Toggle => Some(ReasoningEffort::Disabled),+ ReasoningSupport::Toggle {+ ..+ } => Some(ReasoningEffort::Disabled),ReasoningSupport::Levels {efforts,+ ..} => efforts.iter().copied().find(|e| *e != ReasoningEffort::Default),}}}// uzu lists efforts in template-mapping order, which differs per model.-const EFFORT_ORDER: [ReasoningEffort; 6] = [+const EFFORT_ORDER: [ReasoningEffort; 5] = [ReasoningEffort::Disabled,- ReasoningEffort::Default,ReasoningEffort::Low,ReasoningEffort::Medium,ReasoningEffort::High,@@ -74,23 +79,29 @@ const EFFORT_ORDER: [ReasoningEffort; 6] = [];// Resolving an encoding parses four bundled configs; models share a handful of variants.-static ENCODING_SUPPORT: OnceLock<Mutex<HashMap<String, Option<ReasoningSupport>>>> = OnceLock::new();+#[derive(Clone)]+struct EncodingSupport {+ reasoning: ReasoningSupport,+ tools: bool,+}-fn reasoning_support_from_encoding(model: &Model) -> Option<ReasoningSupport> {+static ENCODING_SUPPORT: OnceLock<Mutex<HashMap<String, Option<EncodingSupport>>>> = OnceLock::new();++fn encoding_support(model: &Model) -> Option<EncodingSupport> {let encoding = model.encoding.as_ref()?;let cache = ENCODING_SUPPORT.get_or_init(Default::default);if let Some(cached) = cache.lock().expect("encoding cache poisoned").get(&encoding.json) {return cached.clone();}- let support = parse_reasoning_support(model, &encoding.json);+ let support = parse_encoding_support(model, &encoding.json);cache.lock().expect("encoding cache poisoned").insert(encoding.json.clone(), support.clone());support}-fn parse_reasoning_support(+fn parse_encoding_support(model: &Model,encoding_json: &str,-) -> Option<ReasoningSupport> {+) -> Option<EncodingSupport> {let config = match serde_json::from_str::<EncodingConfig>(encoding_json) {Ok(config) => config,Err(error) => {@@ -111,35 +122,58 @@ fn parse_reasoning_support(return None;},};- if !capabilities.supports_reasoning {- return Some(ReasoningSupport::Unsupported);- }+ Some(EncodingSupport {+ reasoning: classify_reasoning_support(&capabilities, config.default_reasoning_effort().ok().flatten()),+ tools: capabilities.supports_tools,+ })+}++fn classify_reasoning_support(+ capabilities: &ChatModelCapabilities,+ default_effort: Option<ReasoningEffort>,+) -> ReasoningSupport {let has_levels = capabilities.reasoning_efforts.iter().any(|effort| {matches!(effort,ReasoningEffort::Low | ReasoningEffort::Medium | ReasoningEffort::High | ReasoningEffort::XHigh)});- if has_levels {- let efforts =+ if !capabilities.supports_reasoning {+ ReasoningSupport::Unsupported+ } else if has_levels {+ let efforts: Vec<_> =EFFORT_ORDER.into_iter().filter(|effort| capabilities.reasoning_efforts.contains(effort)).collect();- Some(ReasoningSupport::Levels {+ ReasoningSupport::Levels {+ default_effort: default_effort.filter(|effort| efforts.contains(effort)),efforts,- })+ }} else if capabilities.supports_disable_reasoning {- Some(ReasoningSupport::Toggle)+ if default_effort != Some(ReasoningEffort::Default)+ || !capabilities.reasoning_efforts.contains(&ReasoningEffort::Default)+ {+ // Identical or unresolved modes do not prove an enabled choice.+ ReasoningSupport::Levels {+ efforts: vec![ReasoningEffort::Disabled],+ default_effort: default_effort.filter(|effort| *effort == ReasoningEffort::Disabled),+ }+ } else {+ ReasoningSupport::Toggle {+ default_effort: ReasoningEffort::Default,+ }+ }} else {- Some(ReasoningSupport::AlwaysOn)+ ReasoningSupport::AlwaysOn}}pub fn reasoning_support(model: &Model) -> ReasoningSupport {- reasoning_support_from_encoding(model).unwrap_or_default()+ encoding_support(model).map(|support| support.reasoning).unwrap_or_default()}// Serialized by variant name; the client matches on these strings.#[derive(Serialize, Clone, Copy, Debug, PartialEq, Eq)]pub enum PhaseKind {+ Initializing,NotDownloaded,Downloading,Paused,@@ -151,6 +185,7 @@ pub enum PhaseKind {impl From<&DownloadPhase> for PhaseKind {fn from(phase: &DownloadPhase) -> Self {match phase {+ DownloadPhase::Initializing {} => PhaseKind::Initializing,DownloadPhase::NotDownloaded {} => PhaseKind::NotDownloaded,DownloadPhase::Downloading {} => PhaseKind::Downloading,DownloadPhase::Paused {} => PhaseKind::Paused,@@ -194,6 +229,7 @@ pub struct EngineModel {#[serde(skip_serializing_if = "Option::is_none")]pub param_size: Option<i64>,pub reasoning: ReasoningSupport,+ pub supports_tools: bool,pub quantization: Option<String>,#[serde(skip_serializing_if = "Option::is_none")]pub quantization_bits: Option<u32>,@@ -227,7 +263,7 @@ pub fn map_download_state(return ModelDownloadState {total_kbytes: 0,downloaded_kbytes: 0,- phase: PhaseKind::NotDownloaded,+ phase: PhaseKind::Initializing,error: None,seq,};@@ -290,6 +326,7 @@ pub fn engine_model(family_name: model.family.as_ref().map(|f| f.name()),param_size: model.properties.as_ref().map(|p| p.size),reasoning,+ supports_tools: encoding_support(model).is_some_and(|support| support.tools),quantization: model.quantization.as_ref().map(|q| q.method.clone()),quantization_bits: model.quantization.as_ref().map(|q| q.bits_per_weight),state,@@ -351,21 +388,140 @@ pub async fn find_downloadable_model(key: &str) -> AppResult<Model> {pub async fn chat_models_get(app: tauri::AppHandle,state: tauri::State<'_, crate::downloads::DownloadsState>,-) -> AppResult<Vec<EngineModel>> {+) -> AppResult<ModelCatalog> {let engine = engine().await?;- let models = engine.models_for_chat().await?;+ crate::downloads::ensure_watcher(app, &state);+ let (models, complete) = engine.catalog_snapshot().await?;// Taken before the states: an event emitted while they are read then// carries a newer seq than this snapshot and wins on the client.let seq = state.event_seq();let states = engine.download_states().await;- let local: Vec<Model> = models.into_iter().filter(|m| m.is_on_device()).collect();+ let local: Vec<Model> = models.into_iter().filter(|m| m.is_on_device() && m.is_chat_capable()).collect();state.remember_repo_ids(&local).await;- crate::downloads::ensure_watcher(app, &state);let mut result = Vec::with_capacity(local.len());for model in &local {let reasoning = reasoning_support(model);let state = model_download_state(model, states.get(&model.identifier), seq);result.push(engine_model(model, state, reasoning));}- Ok(result)+ Ok(ModelCatalog {+ models: result,+ complete,+ refreshing: engine.catalog_is_refreshing(),+ })+}++#[tauri::command]+pub async fn chat_models_refresh() -> AppResult<()> {+ engine().await?.refresh_catalog().await?;+ Ok(())+}++#[derive(Serialize)]+pub struct ModelCatalog {+ pub models: Vec<EngineModel>,+ pub complete: bool,+ pub refreshing: bool,+}++#[cfg(test)]+mod tests {+ use super::*;++ fn support(json: &str) -> ReasoningSupport {+ let config: EncodingConfig = serde_json::from_str(json).unwrap();+ classify_reasoning_support(&config.capabilities().unwrap(), config.default_reasoning_effort().unwrap())+ }++ #[test]+ fn model_level_metadata_exposes_only_real_choices_and_actual_default() {+ assert_eq!(+ serde_json::to_value(support(r#"{"type":"hanashi","name":"qwen3.8"}"#)).unwrap(),+ serde_json::json!({"kind":"levels", "efforts":["disabled","low","medium","xhigh"], "defaultEffort":"xhigh"})+ );+ assert_eq!(+ serde_json::to_value(support(r#"{"type":"hanashi","name":"muse-glimmer"}"#)).unwrap(),+ serde_json::json!({"kind":"levels", "efforts":["low","medium","high","xhigh"], "defaultEffort":"high"})+ );+ for encoding in ["hanashi", "harmony"] {+ assert_eq!(+ serde_json::to_value(support(&format!(r#"{{"type":"{encoding}","name":"gpt-oss"}}"#))).unwrap(),+ serde_json::json!({"kind":"levels", "efforts":["low","medium","high"], "defaultEffort":"medium"})+ );+ }+ }++ #[test]+ fn model_toggle_metadata_uses_the_enabled_default() {+ for name in ["qwen3", "qwen3.5", "qwen3.6", "gemma-4"] {+ assert_eq!(+ serde_json::to_value(support(&format!(r#"{{"type":"hanashi","name":"{name}"}}"#))).unwrap(),+ serde_json::json!({"kind":"toggle", "defaultEffort":"default"})+ );+ }+ }++ #[test]+ fn identical_toggle_modes_do_not_advertise_a_switch() {+ let config: EncodingConfig = serde_json::from_str(r#"{"type":"hanashi","name":"qwen3.5"}"#).unwrap();+ for default in [None, Some(ReasoningEffort::Disabled)] {+ assert_eq!(+ classify_reasoning_support(&config.capabilities().unwrap(), default),+ ReasoningSupport::Levels {+ efforts: vec![ReasoningEffort::Disabled],+ default_effort: default+ }+ );+ }+ let mut capabilities = config.capabilities().unwrap();+ capabilities.reasoning_efforts.retain(|effort| *effort != ReasoningEffort::Default);+ assert_eq!(+ classify_reasoning_support(&capabilities, Some(ReasoningEffort::Default)),+ ReasoningSupport::Levels {+ efforts: vec![ReasoningEffort::Disabled],+ default_effort: None+ }+ );+ }++ #[test]+ fn level_mapping_without_default_keeps_the_template_default_unset() {+ let support = ReasoningSupport::Levels {+ efforts: vec![ReasoningEffort::Low, ReasoningEffort::Medium, ReasoningEffort::High],+ default_effort: None,+ };+ assert_eq!(support.effective(None), None);+ assert_eq!(support.effective(Some(ReasoningEffort::Default)), None);+ assert_eq!(support.effective(Some(ReasoningEffort::Medium)), Some(ReasoningEffort::Medium));+ assert_eq!(support.effective(Some(ReasoningEffort::Disabled)), None);+ assert_eq!(support.effective(Some(ReasoningEffort::XHigh)), None);+ }++ #[test]+ fn explicit_levels_are_retained_but_default_is_not_an_override() {+ let support = ReasoningSupport::Levels {+ efforts: vec![ReasoningEffort::Disabled, ReasoningEffort::XHigh],+ default_effort: Some(ReasoningEffort::XHigh),+ };+ assert_eq!(support.effective(None), None);+ assert_eq!(support.effective(Some(ReasoningEffort::Default)), None);+ assert_eq!(support.effective(Some(ReasoningEffort::Disabled)), Some(ReasoningEffort::Disabled));+ assert_eq!(support.effective(Some(ReasoningEffort::XHigh)), Some(ReasoningEffort::XHigh));+ }++ #[test]+ fn toggles_and_fixed_reasoning_modes_only_apply_supported_overrides() {+ let toggle = ReasoningSupport::Toggle {+ default_effort: ReasoningEffort::Default,+ };+ assert_eq!(toggle.effective(None), None);+ assert_eq!(toggle.effective(Some(ReasoningEffort::Default)), None);+ assert_eq!(toggle.effective(Some(ReasoningEffort::High)), None);+ assert_eq!(toggle.effective(Some(ReasoningEffort::Disabled)), Some(ReasoningEffort::Disabled));+ for fixed in [ReasoningSupport::Unsupported, ReasoningSupport::AlwaysOn] {+ assert_eq!(fixed.effective(None), None);+ assert_eq!(fixed.effective(Some(ReasoningEffort::Default)), None);+ assert_eq!(fixed.effective(Some(ReasoningEffort::Disabled)), None);+ }+ }}diff --git a/apps/mirai-gui/desktop/src/storage/cleanup.rs b/apps/mirai-gui/desktop/src/storage/cleanup.rsindex 8c6ae6ca2..2ae4ff31d 100644--- a/apps/mirai-gui/desktop/src/storage/cleanup.rs+++ b/apps/mirai-gui/desktop/src/storage/cleanup.rs@@ -168,7 +168,8 @@ pub async fn cleanup_execute(for (identifier, state) in states.iter() {let is_active = matches!(state.phase,- uzu::storage::DownloadPhase::Downloading {}+ uzu::storage::DownloadPhase::Initializing {}+ | uzu::storage::DownloadPhase::Downloading {}| uzu::storage::DownloadPhase::Paused {}| uzu::storage::DownloadPhase::Locked { .. });diff --git a/apps/mirai-gui/desktop/src/storage/mod.rs b/apps/mirai-gui/desktop/src/storage/mod.rsindex ba94efdbe..a2e183e5d 100644--- a/apps/mirai-gui/desktop/src/storage/mod.rs+++ b/apps/mirai-gui/desktop/src/storage/mod.rs@@ -172,11 +172,14 @@ pub fn settings_merge(}#[tauri::command]-pub fn settings_patch(patch: serde_json::Value) -> AppResult<()> {+pub fn settings_patch(+ analytics: tauri::State<'_, crate::analytics::AnalyticsState>,+ patch: serde_json::Value,+) -> AppResult<()> {let serde_json::Value::Object(patch) = patch else {return Err(AppError::msg("settings patch must be an object"));};- settings_merge(patch).map(|_| ())+ analytics.patch_settings(patch)}// One model's entry changes under the settings lock, so the other models'diff --git a/apps/mirai-gui/desktop/src/system_ui.rs b/apps/mirai-gui/desktop/src/system_ui.rsindex 0728bf15e..e409947a4 100644--- a/apps/mirai-gui/desktop/src/system_ui.rs+++ b/apps/mirai-gui/desktop/src/system_ui.rs@@ -1,15 +1,55 @@use tauri::{AppHandle, Emitter, Manager,- menu::{Menu, MenuItem, PredefinedMenuItem},+ menu::{AboutMetadata, HELP_SUBMENU_ID, MenuBuilder, MenuItem, SubmenuBuilder, WINDOW_SUBMENU_ID},};-use tauri_plugin_autostart::ManagerExt as AutostartExt;-use tauri_plugin_global_shortcut::GlobalShortcutExt;--use crate::error::{AppError, AppResult};const MENU_NEW_CHAT_ID: &str = "menu:new-chat";const MENU_SETTINGS_ID: &str = "menu:settings";+// Before the startup setting was removed, tauri-plugin-autostart used the+// product name (Mirai) and LaunchAgent mode. Retire only that registration for+// this executable; a similarly named agent may belong to something else.+pub fn remove_legacy_autostart() -> std::io::Result<()> {+ let Some(home) = std::env::var_os("HOME") else {+ return Ok(());+ };+ let path = std::path::PathBuf::from(home).join("Library/LaunchAgents/Mirai.plist");+ remove_legacy_autostart_at(&path, &std::env::current_exe()?.canonicalize()?)+}++fn remove_legacy_autostart_at(+ path: &std::path::Path,+ executable: &std::path::Path,+) -> std::io::Result<()> {+ use std::io::Read;++ let metadata = match std::fs::symlink_metadata(path) {+ Ok(metadata) => metadata,+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),+ Err(error) => return Err(error),+ };+ if !metadata.is_file() || metadata.len() > 16 * 1024 {+ return Ok(());+ }+ let Ok(entry) = plist::Value::from_reader_xml(std::fs::File::open(path)?.take(16 * 1024)) else {+ return Ok(());+ };+ let Some(entry) = entry.as_dictionary() else {+ return Ok(());+ };+ let arguments = entry.get("ProgramArguments").and_then(plist::Value::as_array);+ if entry.len() == 3+ && entry.get("Label").and_then(plist::Value::as_string) == Some("Mirai")+ && entry.get("RunAtLoad").and_then(plist::Value::as_boolean) == Some(true)+ && arguments.is_some_and(|arguments| {+ arguments.len() == 1 && arguments[0].as_string().map(std::path::Path::new) == Some(executable)+ })+ {+ std::fs::remove_file(path)?;+ }+ Ok(())+}+pub(crate) fn show_and_focus_main(app: &AppHandle) {if let Some(window) = app.get_webview_window("main") {let _ = window.show();@@ -18,42 +58,46 @@ pub(crate) fn show_and_focus_main(app: &AppHandle) {}}-#[tauri::command]-pub fn get_run_on_startup(app: AppHandle) -> bool {- app.autolaunch().is_enabled().unwrap_or(false)-}--#[tauri::command]-pub fn set_run_on_startup(- app: AppHandle,- value: bool,-) -> AppResult<()> {- let manager = app.autolaunch();- let result = if value {- manager.enable()- } else {- manager.disable()- };- result.map_err(AppError::msg)-}-pub fn setup_app_menu(app: &AppHandle) -> tauri::Result<()> {- let menu = Menu::default(app)?;- for item in menu.items()? {- let Some(submenu) = item.as_submenu() else {- continue;- };- if submenu.text().unwrap_or_default() == "File" {- submenu.insert_items(- &[- &MenuItem::with_id(app, MENU_NEW_CHAT_ID, "New Chat", true, Some("CmdOrCtrl+N"))?,- &MenuItem::with_id(app, MENU_SETTINGS_ID, "Settings", true, Some("CmdOrCtrl+,"))?,- &PredefinedMenuItem::separator(app)?,- ],- 0,- )?;- }- }+ let package = app.package_info();+ let bundle = &app.config().bundle;+ let app_menu = SubmenuBuilder::new(app, &package.name)+ .about(Some(AboutMetadata {+ name: Some(package.name.clone()),+ version: Some(package.version.to_string()),+ copyright: bundle.copyright.clone(),+ authors: bundle.publisher.clone().map(|publisher| vec![publisher]),+ ..Default::default()+ }))+ .separator()+ .item(&MenuItem::with_id(app, MENU_SETTINGS_ID, "Settings…", true, Some("CmdOrCtrl+,"))?)+ .separator()+ .services()+ .separator()+ .hide()+ .hide_others()+ .show_all()+ .separator()+ .quit()+ .build()?;+ let file_menu = SubmenuBuilder::new(app, "File")+ .item(&MenuItem::with_id(app, MENU_NEW_CHAT_ID, "New Chat", true, Some("CmdOrCtrl+N"))?)+ .separator()+ .close_window()+ .build()?;+ let edit_menu =+ SubmenuBuilder::new(app, "Edit").undo().redo().separator().cut().copy().paste().select_all().build()?;+ let view_menu = SubmenuBuilder::new(app, "View").fullscreen().build()?;+ let window_menu = SubmenuBuilder::with_id(app, WINDOW_SUBMENU_ID, "Window")+ .minimize()+ .maximize()+ .separator()+ .bring_all_to_front()+ .build()?;+ let help_menu = SubmenuBuilder::with_id(app, HELP_SUBMENU_ID, "Help").build()?;+ let menu = MenuBuilder::new(app)+ .items(&[&app_menu, &file_menu, &edit_menu, &view_menu, &window_menu, &help_menu])+ .build()?;app.set_menu(menu)?;app.on_menu_event(|app, event| match event.id().as_ref() {MENU_NEW_CHAT_ID => {@@ -69,85 +113,75 @@ pub fn setup_app_menu(app: &AppHandle) -> tauri::Result<()> {Ok(())}-fn apply_quick_entry_shortcut(- app: &AppHandle,- accelerator: &str,-) -> bool {- let shortcut = app.global_shortcut();- if shortcut.is_registered(accelerator) {- return true;- }- let handler_app = app.clone();- let registered = shortcut- .on_shortcut(accelerator, move |_app, _shortcut, event| {- if event.state() == tauri_plugin_global_shortcut::ShortcutState::Pressed {- show_and_focus_main(&handler_app);- }- })- .is_ok();- if registered && let Some(previous) = get_quick_entry_shortcut().filter(|p| p != accelerator) {- let _ = shortcut.unregister(previous.as_str());- }- registered-}--#[tauri::command]-pub fn register_quick_entry_shortcut(- app: AppHandle,- accelerator: String,-) -> bool {- let ok = apply_quick_entry_shortcut(&app, &accelerator);- if ok {- persist_quick_entry_accelerator(Some(&accelerator));- }- ok+#[derive(serde::Deserialize)]+#[serde(rename_all = "lowercase")]+pub enum WindowTheme {+ System,+ Light,+ Dark,}-#[tauri::command]-pub fn unregister_quick_entry_shortcut(app: AppHandle) -> AppResult<()> {- app.global_shortcut().unregister_all().map_err(AppError::msg)?;- persist_quick_entry_accelerator(None);- Ok(())-}--// The shortcut already works for this session; a failed write only loses it on-// the next launch, so log instead of failing the command.-fn persist_quick_entry_accelerator(accelerator: Option<&str>) {- if let Err(e) = crate::storage::settings_patch(serde_json::json!({ "quickEntryAccelerator": accelerator })) {- crate::logger::warn("shortcut:settings-write-failed", Some(serde_json::json!({ "error": e })));- }-}--// Sync the native window appearance to the app theme: otherwise the window-// keeps the system appearance and macOS paints the inactive traffic lights for-// the wrong one (grey lights vanish on a dark background).+// Follow the OS in system mode. Explicit app themes also set the native+// appearance so macOS paints the traffic lights for the matching background.#[tauri::command]pub fn set_window_theme(app: AppHandle,- dark: bool,+ theme: WindowTheme,) -> bool {- let theme = if dark {- tauri::Theme::Dark- } else {- tauri::Theme::Light+ let theme = match theme {+ WindowTheme::System => None,+ WindowTheme::Light => Some(tauri::Theme::Light),+ WindowTheme::Dark => Some(tauri::Theme::Dark),};- app.get_webview_window("main").map(|w| w.set_theme(Some(theme)).is_ok()).unwrap_or(false)+ app.get_webview_window("main").map(|w| w.set_theme(theme).is_ok()).unwrap_or(false)}-#[tauri::command]-pub fn get_quick_entry_shortcut() -> Option<String> {- let settings = crate::storage::settings_load().ok()?;- settings.get("quickEntryAccelerator").and_then(|v| v.as_str()).filter(|s| !s.is_empty()).map(str::to_string)-}+#[cfg(test)]+mod tests {+ use super::*;-pub fn restore_from_settings(app: &AppHandle) {- let settings = crate::storage::settings_load().unwrap_or_else(|_| serde_json::json!({}));- if let Some(accelerator) = settings.get("quickEntryAccelerator").and_then(|v| v.as_str())- && !accelerator.is_empty()- && !apply_quick_entry_shortcut(app, accelerator)- {- // Otherwise settings keep showing a shortcut that does not work.- crate::logger::warn("shortcut:restore-failed", Some(serde_json::json!({ "accelerator": accelerator })));- persist_quick_entry_accelerator(None);+ #[test]+ fn autostart_cleanup_only_removes_the_released_apps_own_registration() {+ let directory = std::env::temp_dir().join(format!("mirai-autostart-test-{}", uuid::Uuid::new_v4()));+ std::fs::create_dir(&directory).unwrap();+ let path = directory.join("Mirai.plist");+ let executable = std::path::Path::new("/Applications/Mirai.app/Contents/MacOS/Mirai");+ let original = format!(+ r#"<?xml version="1.0" encoding="UTF-8"?>+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">+<plist version="1.0"><dict>+<key>Label</key><string>Mirai</string>+<key>ProgramArguments</key><array><string>{}</string></array>+<key>RunAtLoad</key><true/>+</dict></plist>"#,+ executable.display()+ );+ remove_legacy_autostart_at(&path, executable).unwrap();+ for contents in [+ original.replace("<string>Mirai</string>", "<string>Other</string>"),+ original.replace("/Applications/Mirai.app", "/Applications/Other.app"),+ original.replace("</array>", "<string>--custom</string></array>"),+ original.replace("<true/>", "<false/>"),+ original.replace("</dict>", "<key>KeepAlive</key><true/></dict>"),+ "invalid plist".into(),+ " ".repeat(16 * 1024 + 1),+ ] {+ std::fs::write(&path, &contents).unwrap();+ remove_legacy_autostart_at(&path, executable).unwrap();+ assert_eq!(std::fs::read_to_string(&path).unwrap(), contents);+ }+ std::fs::write(&path, &original).unwrap();+ remove_legacy_autostart_at(&path, executable).unwrap();+ assert!(!path.exists());+ #[cfg(unix)]+ {+ let other = directory.join("other.plist");+ std::fs::write(&other, &original).unwrap();+ std::os::unix::fs::symlink(&other, &path).unwrap();+ remove_legacy_autostart_at(&path, executable).unwrap();+ assert_eq!(std::fs::read_to_string(&other).unwrap(), original);+ assert!(std::fs::symlink_metadata(&path).unwrap().is_symlink());+ }+ std::fs::remove_dir_all(&directory).unwrap();}}diff --git a/apps/mirai-gui/desktop/tauri.conf.json b/apps/mirai-gui/desktop/tauri.conf.jsonindex edbd6d85d..6d4940026 100644--- a/apps/mirai-gui/desktop/tauri.conf.json+++ b/apps/mirai-gui/desktop/tauri.conf.json@@ -8,7 +8,8 @@"beforeDevCommand": "pnpm run client:dev","devUrl": "http://localhost:3002","beforeBuildCommand": "pnpm run client:build",- "frontendDist": "../client/dist-tauri"+ "frontendDist": "../client/dist-tauri",+ "additionalWatchFolders": ["../../../crates"]},"app": {"windows": [],diff --git a/apps/mirai-gui/package.json b/apps/mirai-gui/package.jsonindex b4e070c96..40afd5b6c 100644--- a/apps/mirai-gui/package.json+++ b/apps/mirai-gui/package.json@@ -67,6 +67,7 @@"@tanstack/react-router": "^1.170.41","@tauri-apps/api": "^2.12.0","@tauri-apps/plugin-dialog": "^2.8.0",+ "chart.js": "^4.5.1","date-fns": "^4.4.0","jszip": "^3.10.2","katex": "^0.16.47",diff --git a/apps/mirai-gui/pnpm-lock.yaml b/apps/mirai-gui/pnpm-lock.yamlindex cbdd95e45..6057bd468 100644--- a/apps/mirai-gui/pnpm-lock.yaml+++ b/apps/mirai-gui/pnpm-lock.yaml@@ -38,6 +38,9 @@ importers:'@tauri-apps/plugin-dialog':specifier: ^2.8.0version: 2.8.0+ chart.js:+ specifier: ^4.5.1+ version: 4.5.1date-fns:specifier: ^4.4.0version: 4.4.0@@ -677,6 +680,9 @@ packages:'@keyv/[email protected]':resolution: {integrity: sha512-dXn3FZhPv0US+7dtJsIi2R+c7qWYiReoEh5zUntWCf4oSpMNib8FDhSoed6m3QyZdx5hK7iLFkYk3rNxwt8vTA==}+ '@kurkle/[email protected]':+ resolution: {integrity: sha512-M5UknZPHRu3DEDWoipU6sE8PdkZ6Z/S+v4dD+Ke8IaNlpdSQah50lz1KtcFBa2vsdOnwbbnxJwVM4wty6udA5w==}+'@napi-rs/[email protected]':resolution: {integrity: sha512-oTXEIha4SsuXdTA4Iyskj0kpdx2yVXdhd75c2v3xGrHFfVMsbhTPZU/nMPL4sWKo4pBHm3aucLaqGlF696dTyQ==}engines: {node: ^22.20 || ^24.12 || >=25}@@ -1408,6 +1414,10 @@ packages:resolution: {integrity: sha512-iBZ4F4wRbyORVsu0jPV7gXkOsGYjGHPmAyv+HiHG8gi5PtC9KI2j1+v8/tlibRvjoWX027ypmG/n0HtO5t7unw==}+ resolution: {integrity: sha512-GIjfiT9dbmHRiYi6Nl2yFCq7kkwdkp1W/lp2J99rX0yo9tgJGn3lKQATztIjb5tVtevcBtIdICNWqlq5+E8/Pw==}+ engines: {pnpm: '>=8'}+resolution: {integrity: sha512-PAJdDJusoxnwm1VwW07VWwUN1sl7smmC3OKggvndJFadxxDRyFJBX/ggnu/KE4kQAB7a3Dp8f/YXC1FlUprWmA==}engines: {node: '>= 16'}@@ -3232,6 +3242,8 @@ snapshots:'@keyv/[email protected]': {}+ '@kurkle/[email protected]': {}+'@napi-rs/[email protected]':optional: true@@ -3942,6 +3954,10 @@ snapshots:+ dependencies:+ '@kurkle/color': 0.3.4+diff --git a/crates/legacy/cli/src/interactive/components/selected_model.rs b/crates/legacy/cli/src/interactive/components/selected_model.rsindex e1362a947..de607bbad 100644--- a/crates/legacy/cli/src/interactive/components/selected_model.rs+++ b/crates/legacy/cli/src/interactive/components/selected_model.rs@@ -129,7 +129,7 @@ pub fn SelectedModel(} => {let _ = downloader.resume().await;},- DownloadPhase::Downloaded {} => {},+ DownloadPhase::Initializing {} | DownloadPhase::Downloaded {} => {},}},StorageAction::Delete => {diff --git a/crates/legacy/cli/src/interactive/flows/model_families.rs b/crates/legacy/cli/src/interactive/flows/model_families.rsindex affec6cc3..6726746ae 100644--- a/crates/legacy/cli/src/interactive/flows/model_families.rs+++ b/crates/legacy/cli/src/interactive/flows/model_families.rs@@ -1,6 +1,7 @@use indexmap::IndexSet;use iocraft::prelude::*;use shoji::types::model::ModelFamily;+use tokio_stream::StreamExt;use crate::interactive::{components::{ApplicationState, Loading, Selector, SelectorItem, SelectorStyle},@@ -46,22 +47,30 @@ fn ModelFamilies(let engine = state.read().engine.clone();let registry_id = registry_id.clone();async move {- let loaded = match registry_id {- Some(identifier) => {- let mut seen: IndexSet<String> = IndexSet::new();- engine- .models()- .await- .unwrap_or_default()- .into_iter()- .filter(|model| model.registry.identifier == identifier)- .filter_map(|model| model.family.clone())- .filter(|family| seen.insert(family.identifier.clone()))- .collect()- },- None => engine.model_families().await.unwrap_or_default(),- };- families.set(Some(loaded));+ let mut updates = engine.catalog_subscribe();+ loop {+ let loaded: Vec<ModelFamily> = match registry_id.clone() {+ Some(identifier) => {+ let mut seen: IndexSet<String> = IndexSet::new();+ engine+ .models()+ .await+ .unwrap_or_default()+ .into_iter()+ .filter(|model| model.registry.identifier == identifier)+ .filter_map(|model| model.family.clone())+ .filter(|family| seen.insert(family.identifier.clone()))+ .collect()+ },+ None => engine.model_families().await.unwrap_or_default(),+ };+ if !engine.catalog_is_refreshing() || !loaded.is_empty() {+ families.set(Some(loaded));+ }+ if updates.next().await.is_none() {+ break;+ }+ }}});diff --git a/crates/legacy/cli/src/interactive/flows/model_registries.rs b/crates/legacy/cli/src/interactive/flows/model_registries.rsindex 9a551e9ac..a71c688da 100644--- a/crates/legacy/cli/src/interactive/flows/model_registries.rs+++ b/crates/legacy/cli/src/interactive/flows/model_registries.rs@@ -2,6 +2,7 @@ use std::sync::Arc;use iocraft::prelude::*;use shoji::types::model::{Model, ModelRegistry};+use tokio_stream::StreamExt;use crate::interactive::{components::{ApplicationState, Loading, Selector, SelectorItem, SelectorStyle},@@ -66,11 +67,19 @@ fn ModelRegistries(let on_pick = on_pick.clone();let engine = state.read().engine.clone();async move {- let registries = engine.model_registries().await.unwrap_or_default();- if registries.len() == 1 {- on_pick(registries[0].clone());- } else {- registries_state.set(Some(registries));+ let mut updates = engine.catalog_subscribe();+ loop {+ let registries = engine.model_registries().await.unwrap_or_default();+ let refreshing = engine.catalog_is_refreshing();+ if registries.len() == 1 && !refreshing {+ on_pick(registries[0].clone());+ break;+ } else if !refreshing || !registries.is_empty() {+ registries_state.set(Some(registries));+ }+ if updates.next().await.is_none() {+ break;+ }}}});diff --git a/crates/legacy/cli/src/interactive/flows/models.rs b/crates/legacy/cli/src/interactive/flows/models.rsindex 28635a74d..04a3b69ae 100644--- a/crates/legacy/cli/src/interactive/flows/models.rs+++ b/crates/legacy/cli/src/interactive/flows/models.rs@@ -5,6 +5,7 @@ use shoji::types::{basic::SamplingParameters,model::{Model, ModelIdentifier},};+use tokio_stream::StreamExt;use uzu::storage::DownloadState;use crate::{@@ -60,22 +61,46 @@ fn Models(let registry_id = registry_id.clone();let family_id = family_id.clone();async move {- let models: Vec<Model> = engine- .models()- .await- .unwrap_or_default()- .into_iter()- .filter(|model| registry_id.as_ref().is_none_or(|id| &model.registry.identifier == id))- .filter(|model| {- family_id- .as_ref()- .is_none_or(|id| model.family.as_ref().is_some_and(|family| &family.identifier == id))- })- .collect();- let download_statuses = engine.download_states().await;-- models_state.set(Some(models));- model_download_statuses_state.set(Some(download_statuses));+ let mut catalog_updates = engine.catalog_subscribe();+ let mut storage_updates = engine.storage_subscribe();+ loop {+ let models: Vec<Model> = engine+ .models()+ .await+ .unwrap_or_default()+ .into_iter()+ .filter(|model| registry_id.as_ref().is_none_or(|id| &model.registry.identifier == id))+ .filter(|model| {+ family_id+ .as_ref()+ .is_none_or(|id| model.family.as_ref().is_some_and(|family| &family.identifier == id))+ })+ .collect();+ let download_statuses = engine.download_states().await;+ if !engine.catalog_is_refreshing() || !models.is_empty() {+ models_state.set(Some(models));+ }+ model_download_statuses_state.set(Some(download_statuses));+ loop {+ tokio::select! {+ update = catalog_updates.next() => {+ if update.is_none() { return; }+ break;+ },+ update = storage_updates.next() => {+ match update {+ Some(Ok((identifier, download_state))) => {+ if let Some(states) = model_download_statuses_state.write().as_mut() {+ states.insert(identifier, download_state);+ }+ },+ Some(Err(_)) => break,+ None => return,+ }+ },+ }+ }+ }}});diff --git a/crates/legacy/cli/src/interactive/mod.rs b/crates/legacy/cli/src/interactive/mod.rsindex c98d83516..de5cffd95 100644--- a/crates/legacy/cli/src/interactive/mod.rs+++ b/crates/legacy/cli/src/interactive/mod.rs@@ -4,6 +4,7 @@ use comfy_table::{presets::UTF8_FULL,};use shoji::types::basic::ReasoningEffort;+use tokio_stream::StreamExt;use uzu::engine::{Engine, EngineConfig};use crate::interactive::{@@ -40,7 +41,7 @@ pub async fn run_interactive(pub async fn run_list_models() -> anyhow::Result<()> {let engine_config = EngineConfig::default().with_application_identifier(APP_IDENTIFIER.to_string());let engine = Engine::new(engine_config).await?;- let models = engine.models().await?;+ let models = list_models(&engine).await?;if models.is_empty() {return Err(anyhow::anyhow!("No models to run"));}@@ -65,7 +66,7 @@ pub async fn run_list_models() -> anyhow::Result<()> {pub async fn run_list_checkpoints(model_id: String) -> anyhow::Result<()> {let engine_config = EngineConfig::default().with_application_identifier(APP_IDENTIFIER.to_string());let engine = Engine::new(engine_config).await?;- let models = engine.models().await?;+ let models = list_models(&engine).await?;let checkpoints = get_checkpoints(&models, &model_id);if checkpoints.is_empty() {return Err(anyhow::anyhow!("No checkpoints found for model: {model_id}"));@@ -86,3 +87,15 @@ pub async fn run_list_checkpoints(model_id: String) -> anyhow::Result<()> {Ok(())}++async fn list_models(engine: &Engine) -> anyhow::Result<Vec<shoji::types::model::Model>> {+ // These commands print once and exit; take the final snapshot rather than+ // whichever registry happened to answer first. Interactive views subscribe.+ let mut updates = engine.catalog_subscribe();+ while engine.catalog_is_refreshing() {+ if updates.next().await.is_none() {+ break;+ }+ }+ Ok(engine.models().await?)+}diff --git a/crates/legacy/cli/src/interactive/non_interactive.rs b/crates/legacy/cli/src/interactive/non_interactive.rsindex eda0697f8..db79225fd 100644--- a/crates/legacy/cli/src/interactive/non_interactive.rs+++ b/crates/legacy/cli/src/interactive/non_interactive.rs@@ -82,13 +82,16 @@ pub async fn run_non_interactive(if let Some(reply) = replies.last() {stats = Some(reply.stats.clone());}- let items = build_transcript(&session.messages().await, 0);- plain.write(&mut stdout, items, false)?;},+ ChatSessionStreamChunk::ToolResults {+ ..+ } => {},ChatSessionStreamChunk::Error {error,} => return Err(error.into()),}+ let items = build_transcript(&session.messages().await, 0);+ plain.write(&mut stdout, items, false)?;}let stats = stats.context("No response generated")?;diff --git a/crates/legacy/cli/src/interactive/sessions/chat.rs b/crates/legacy/cli/src/interactive/sessions/chat.rsindex dd9824498..ab058f165 100644--- a/crates/legacy/cli/src/interactive/sessions/chat.rs+++ b/crates/legacy/cli/src/interactive/sessions/chat.rs@@ -235,13 +235,10 @@ pub async fn run_session(if let Some(reply) = replies.last() {latest_stats = Some(reply.stats.clone());}- let items = build_transcript(&session.messages().await, history_offset);- let mut state = state.write();- if let Some(chat_state) = chat_state_mut(&mut state) {- chat_state.pending_items = items;- chat_state.pending_stats = latest_stats.clone();- }},+ ChatSessionStreamChunk::ToolResults {+ ..+ } => {},ChatSessionStreamChunk::Error {error,} => {@@ -251,6 +248,12 @@ pub async fn run_session(break;},}+ let items = build_transcript(&session.messages().await, history_offset);+ let mut state = state.write();+ if let Some(chat_state) = chat_state_mut(&mut state) {+ chat_state.pending_items = items;+ chat_state.pending_stats = latest_stats.clone();+ }}let final_items = build_transcript(&session.messages().await, history_offset);diff --git a/crates/legacy/cli/src/server/chat_completions.rs b/crates/legacy/cli/src/server/chat_completions.rsindex fcc03c5ca..43cfb8b25 100644--- a/crates/legacy/cli/src/server/chat_completions.rs+++ b/crates/legacy/cli/src/server/chat_completions.rs@@ -809,6 +809,9 @@ async fn run_blocking(} => {final_replies = replies;},+ ChatSessionStreamChunk::ToolResults {+ ..+ } => {},ChatSessionStreamChunk::Error {error,} => {@@ -1075,6 +1078,9 @@ async fn run_stream(.iter().any(|block| matches!(block, ChatContentBlock::ToolCallCandidate { .. }));},+ ChatSessionStreamChunk::ToolResults {+ ..+ } => {},ChatSessionStreamChunk::Error {error,} => {diff --git a/crates/legacy/cli/src/storage/app.rs b/crates/legacy/cli/src/storage/app.rsindex a080ceae4..a121aa803 100644--- a/crates/legacy/cli/src/storage/app.rs+++ b/crates/legacy/cli/src/storage/app.rs@@ -6,7 +6,7 @@ use ratatui::widgets::ListState;use shoji::types::model::{Model, ModelIdentifier};use tokio::{sync::Mutex as TokioMutex, task::JoinHandle};use uzu::{- engine::Engine,+ engine::{Engine, EngineError},storage::{DownloadPhase, DownloadState},};@@ -19,6 +19,28 @@ pub struct ModelWithState {pub state: DownloadState,}+async fn catalog_models(engine: &Engine) -> Result<HashMap<ModelIdentifier, ModelWithState>, EngineError> {+ let models = engine.models().await?;+ let states = engine.download_states().await;+ Ok(models+ .into_iter()+ .filter(|model| model.is_downloadable())+ .map(|model| {+ let state = states.get(&model.identifier).cloned().unwrap_or(DownloadState {+ phase: DownloadPhase::Initializing {},+ ..DownloadState::default()+ });+ (+ model.identifier.clone(),+ ModelWithState {+ model,+ state,+ },+ )+ })+ .collect())+}+pub struct App {pub engine: Arc<Engine>,pub models: Arc<TokioMutex<HashMap<ModelIdentifier, ModelWithState>>>,@@ -34,21 +56,7 @@ impl App {engine: Arc<Engine>,tokio_handle: tokio::runtime::Handle,) -> Self {- let models =- engine.models().await.unwrap().into_iter().filter(|model| model.is_downloadable()).collect::<Vec<_>>();-- // Fetch initial state for all models- let mut models_with_state = HashMap::new();- for model in models {- let state = engine.downloader(&model).state().await.unwrap();- models_with_state.insert(- model.identifier.clone(),- ModelWithState {- model,- state,- },- );- }+ let models_with_state = catalog_models(&engine).await.unwrap_or_default();let mut list_states = HashMap::new();for section in Section::all() {@@ -71,29 +79,34 @@ impl App {let models = Arc::clone(&self.models);let engine = Arc::clone(&self.engine);let mut updates = self.engine.storage_subscribe();+ let mut catalog_updates = self.engine.catalog_subscribe();let handle = self.tokio_handle.spawn(async move {- while let Some(Ok((model_id, state))) = updates.next().await {- let mut models_guard = models.lock().await;-- if let Some(model_with_state) = models_guard.get_mut(&model_id) {- // Update the cached state- model_with_state.state = state;- } else {- // Model not in local HashMap; fetch from storage and add it- drop(models_guard);- if let Some(fresh_model) = engine.model_by_identifier(model_id.clone()).await.unwrap() {- let state = engine.downloader(&fresh_model).state().await.unwrap();- let mut models_guard = models.lock().await;- models_guard.insert(- model_id,- ModelWithState {- model: fresh_model,- state,+ // Subscribe before refreshing to include changes since App::new.+ if let Ok(snapshot) = catalog_models(&engine).await {+ *models.lock().await = snapshot;+ }+ loop {+ tokio::select! {+ event = catalog_updates.next() => {+ if event.is_none() { break; }+ },+ event = updates.next() => {+ match event {+ Some(Ok((model_id, state))) => {+ if let Some(model) = models.lock().await.get_mut(&model_id) {+ model.state = state;+ continue;+ }},- );+ Some(Err(_)) => {},+ None => break,+ }}}+ if let Ok(snapshot) = catalog_models(&engine).await {+ *models.lock().await = snapshot;+ }}});@@ -192,7 +205,9 @@ impl App {if let Some(id) = model_id {let models_guard = self.models.lock().await;- if let Some(model_with_state) = models_guard.get(&id) {+ if let Some(model_with_state) = models_guard.get(&id)+ && !matches!(model_with_state.state.phase, DownloadPhase::Initializing {})+ {let _ = self.engine.downloader(&model_with_state.model).resume().await;}}@@ -238,6 +253,7 @@ impl App {if let Some(model_with_state) = self.get_selected_model_id(models).and_then(|id| models.get(&id)) {helpers.insert(0, "↑↓: Navigate".to_string());match &model_with_state.state.phase {+ DownloadPhase::Initializing {} => helpers.push("Checking model…".to_string()),DownloadPhase::NotDownloaded {} => helpers.push("d/Enter: Download".to_string()),DownloadPhase::Paused {}| DownloadPhase::Error {diff --git a/crates/legacy/cli/src/storage/sections.rs b/crates/legacy/cli/src/storage/sections.rsindex d70728d92..c6bdb7525 100644--- a/crates/legacy/cli/src/storage/sections.rs+++ b/crates/legacy/cli/src/storage/sections.rs@@ -46,7 +46,7 @@ impl Section {| DownloadPhase::Error {..} => Section::Downloading,- DownloadPhase::NotDownloaded {} => Section::Available,+ DownloadPhase::Initializing {} | DownloadPhase::NotDownloaded {} => Section::Available,}}diff --git a/crates/legacy/cli/src/storage/ui.rs b/crates/legacy/cli/src/storage/ui.rsindex 21190f556..3cd8ee85e 100644--- a/crates/legacy/cli/src/storage/ui.rs+++ b/crates/legacy/cli/src/storage/ui.rs@@ -66,6 +66,7 @@ fn render_section(let items: Vec<ListItem> = section_models.iter().map(|(id, model_with_state)| match &model_with_state.state.phase {+ DownloadPhase::Initializing {} => ListItem::new(format!("{} (Checking…)", id)),DownloadPhase::Downloaded {} => ListItem::new(format!("✓ {}", id)),DownloadPhase::NotDownloaded {} => {let size_mb = model_with_state.state.total_bytes as f64 / 1_000_000.0;@@ -183,6 +184,7 @@ fn render_downloading_model(progress * 100.0);let (info, gauge_color, selected_border) = match &state.phase {+ DownloadPhase::Initializing {} => (" [CHECKING]".to_string(), Color::Gray, Color::White),DownloadPhase::Downloading {} => (format!(" {bytes_info}"), Color::Cyan, Color::Blue),DownloadPhase::Paused {} => (format!(" [PAUSED] {bytes_info}"), Color::Yellow, Color::Yellow),DownloadPhase::Locked {diff --git a/crates/legacy/download-manager/Cargo.toml b/crates/legacy/download-manager/Cargo.tomlindex a78e94347..626a9ebb6 100644--- a/crates/legacy/download-manager/Cargo.toml+++ b/crates/legacy/download-manager/Cargo.toml@@ -52,10 +52,6 @@ serde.workspace = trueserde_json.workspace = trueasync-trait.workspace = truethiserror.workspace = true-crc32c.workspace = true-sha1.workspace = true-sha2.workspace = true-base64.workspace = truetracing.workspace = truereqwest.workspace = truetokio = { workspace = true, features = ["macros", "sync"] }diff --git a/crates/legacy/download-manager/examples/wasm/Cargo.lock b/crates/legacy/download-manager/examples/wasm/Cargo.lockindex 9f0d54a68..5b4be4b70 100644--- a/crates/legacy/download-manager/examples/wasm/Cargo.lock+++ b/crates/legacy/download-manager/examples/wasm/Cargo.lock@@ -50,7 +50,7 @@ checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"[[package]]name = "bindings"-version = "0.5.26"+version = "0.6.2"dependencies = ["heck","proc-macro2",@@ -58,10 +58,6 @@ dependencies = ["syn 2.0.118",]-[[package]]-name = "bindings-types"-version = "0.5.26"-[[package]]name = "bitflags"version = "2.13.0"@@ -211,15 +207,6 @@ dependencies = ["libc",]-[[package]]-name = "crc32c"-version = "0.6.8"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "3a47af21622d091a8f0fb295b88bc886ac74efcc613efc19f5d0b21de5c89e47"-dependencies = [- "rustc_version",-]-[[package]]name = "darling"version = "0.24.1"@@ -277,15 +264,12 @@ dependencies = [[[package]]name = "download-manager"-version = "0.5.26"+version = "0.6.2"dependencies = ["async-trait",- "base64","bindings",- "bindings-types","block2","bon",- "crc32c","futures-util","kiban","objc2",@@ -780,7 +764,7 @@ dependencies = [[[package]]name = "kiban"-version = "0.5.26"+version = "0.6.2"dependencies = ["async-trait","futures-util",diff --git a/crates/legacy/download-manager/examples/wasm/src/lib.rs b/crates/legacy/download-manager/examples/wasm/src/lib.rsindex 86d2d209c..643fa541a 100644--- a/crates/legacy/download-manager/examples/wasm/src/lib.rs+++ b/crates/legacy/download-manager/examples/wasm/src/lib.rs@@ -79,6 +79,7 @@ async fn download_internal(task.download().await?;while let Some(state) = progress.next().await {let (phase, message) = match &state.phase {+ DownloadPhase::Initializing {} => ("initializing", None),DownloadPhase::NotDownloaded {} => ("not_downloaded", None),DownloadPhase::Downloading {} => ("downloading", None),DownloadPhase::Paused {} => ("paused", None),diff --git a/crates/legacy/download-manager/src/backends/apple/apple_backend.rs b/crates/legacy/download-manager/src/backends/apple/apple_backend.rsindex 3115db83f..2e6b8ccb5 100644--- a/crates/legacy/download-manager/src/backends/apple/apple_backend.rs+++ b/crates/legacy/download-manager/src/backends/apple/apple_backend.rs@@ -2,6 +2,7 @@ use std::{path::Path,ptr::NonNull,sync::{Arc, Mutex, PoisonError},+ time::{Duration, Instant},};use block2::RcBlock;@@ -29,6 +30,7 @@ use crate::{const CALLER_SECURITY_SESSION: u32 = u32::MAX;#[cfg(target_os = "macos")]const SESSION_IS_ROOT: u32 = 0x0001;+const TASK_DISCOVERY_TIMEOUT: Duration = Duration::from_secs(5);#[cfg(target_os = "macos")]#[link(name = "Security", kind = "framework")]@@ -46,6 +48,7 @@ pub struct AppleBackend {event_registry: AppleEventRegistry,runtime_handle: RuntimeHandle,pending_tasks: TokioOnceCell<Mutex<Vec<Retained<NSURLSessionDownloadTask>>>>,+ pending_task_error: Mutex<Option<(Instant, String)>>,}unsafe impl Send for AppleBackend {}@@ -81,6 +84,7 @@ impl AppleBackend {event_registry,runtime_handle,pending_tasks: TokioOnceCell::new(),+ pending_task_error: Mutex::new(None),}}@@ -99,6 +103,14 @@ impl AppleBackend {async fn pending_tasks(&self) -> Result<&Mutex<Vec<Retained<NSURLSessionDownloadTask>>>, BackendError> {self.pending_tasks.get_or_try_init(|| async {+ // A failed lookup is shared briefly by concurrent initializers.+ // Otherwise each queued file waits through its own timeout.+ if let Some((when, error)) =+ self.pending_task_error.lock().unwrap_or_else(PoisonError::into_inner).as_ref()+ && when.elapsed() < TASK_DISCOVERY_TIMEOUT+ {+ return Err(AppleBackendError::TaskDiscovery(error.clone()).into());+ }let (tasks_sender, tasks_receiver) = tokio_oneshot_channel();{let tasks_sender = Mutex::new(Some(tasks_sender));@@ -115,7 +127,19 @@ impl AppleBackend {self.session.getTasksWithCompletionHandler(&handler);}}- let tasks = tasks_receiver.await.map_err(AppleBackendError::CallbackDropped)?;+ let result = tokio::time::timeout(TASK_DISCOVERY_TIMEOUT, tasks_receiver)+ .await+ .map_err(|_| "URLSession did not reply within 5 seconds".to_string())+ .and_then(|tasks| tasks.map_err(|error| error.to_string()));+ let tasks = match result {+ Ok(tasks) => tasks,+ Err(message) => {+ *self.pending_task_error.lock().unwrap_or_else(PoisonError::into_inner) =+ Some((Instant::now(), message.clone()));+ // Leave native tasks and their destination markers alone.+ return Err(AppleBackendError::TaskDiscovery(message).into());+ },+ };Ok(Mutex::new(tasks))}).await@@ -228,9 +252,7 @@ impl Backend for AppleBackend {if attached.is_none()&& Self::is_live(&task)&& AppleTaskDescription::of(&task).is_some_and(|description| {- description.download_id == config.download_id- && description.source_url == config.source_url- && description.checksum == config.expected_checksum+ description.download_id == config.download_id && description.source_url == config.source_url}){attached = Some(self.activate(task, &config, generation, events.clone()));diff --git a/crates/legacy/download-manager/src/backends/apple/apple_backend_error.rs b/crates/legacy/download-manager/src/backends/apple/apple_backend_error.rsindex 8a46d6643..e5b4e1709 100644--- a/crates/legacy/download-manager/src/backends/apple/apple_backend_error.rs+++ b/crates/legacy/download-manager/src/backends/apple/apple_backend_error.rs@@ -6,4 +6,6 @@ pub enum AppleBackendError {InvalidUrl(String),#[error("URLSession callback dropped: {0}")]CallbackDropped(#[from] RecvError),+ #[error("unable to inspect background downloads: {0}")]+ TaskDiscovery(String),}diff --git a/crates/legacy/download-manager/src/backends/apple/apple_task_description.rs b/crates/legacy/download-manager/src/backends/apple/apple_task_description.rsindex 0686cbed5..a1946adc9 100644--- a/crates/legacy/download-manager/src/backends/apple/apple_task_description.rs+++ b/crates/legacy/download-manager/src/backends/apple/apple_task_description.rs@@ -1,15 +1,13 @@use objc2_foundation::NSURLSessionDownloadTask;use serde::{Deserialize, Serialize};-use crate::{Checksum, DownloadId, file_download::DownloadConfig};+use crate::{DownloadId, file_download::DownloadConfig};#[derive(Serialize, Deserialize, Debug)]pub struct AppleTaskDescription {#[serde(default)]pub download_id: DownloadId,pub source_url: String,- #[serde(default)]- pub checksum: Option<Checksum>,}impl From<&DownloadConfig> for AppleTaskDescription {@@ -17,7 +15,6 @@ impl From<&DownloadConfig> for AppleTaskDescription {Self {download_id: config.download_id,source_url: config.source_url.clone(),- checksum: config.expected_checksum.clone(),}}}diff --git a/crates/legacy/download-manager/src/backends/backend.rs b/crates/legacy/download-manager/src/backends/backend.rsindex 69fcb29fe..8cd568fb0 100644--- a/crates/legacy/download-manager/src/backends/backend.rs+++ b/crates/legacy/download-manager/src/backends/backend.rs@@ -7,8 +7,7 @@ use kiban::fs;use crate::{DownloadPhase, DownloadState,- backends::{ActiveTask, BackendError, BackendEventSender, DownloadGeneration, VerifyError},- checksum_receipt::ChecksumReceipt,+ backends::{ActiveTask, BackendError, BackendEventSender, DownloadGeneration},file_download::DownloadConfig,locks::{DestinationLock, LockError},};@@ -55,14 +54,24 @@ pub trait Backend: Send + Sync {&self,config: &DownloadConfig,) -> Result<(DownloadState, Option<DestinationLock>), BackendError> {- let untouched = !fs::asyn::is_file(&config.destination).await+ let destination_exists = fs::asyn::is_file(&config.destination).await;+ let untouched = !destination_exists&& !fs::asyn::is_file(&config.resume_artifact_path).await- && !ChecksumReceipt::exists(&config.destination).await- && DestinationLock::foreign_owner(&config.destination, &config.owner).await.is_none();- let pending_task = self.has_pending_task(config).await?;- if untouched && !pending_task {+ && !DestinationLock::exists(&config.destination).await;+ // A download acquires its destination lock before creating a native+ // task. A process crash releases the OS lock but leaves the file behind.+ if untouched {return Ok((DownloadState::new(config, DownloadPhase::NotDownloaded {}, 0, None), None));}+ if let Some(owner) = DestinationLock::foreign_owner(&config.destination, &config.owner).await {+ return Ok((self.observe(config, Some(owner)).await, None));+ }+ // Completed files need only a size check, never native task discovery.+ let pending_task = if destination_exists && self.completed_size(config).await.is_ok() {+ false+ } else {+ self.has_pending_task(config).await?+ };let lock = match DestinationLock::acquire(&config.destination, &config.owner).await {Ok(lock) => lock,Err(LockError::LockedByOther {@@ -86,7 +95,7 @@ pub trait Backend: Send + Sync {None};let downloaded = if fs::asyn::is_file(&config.destination).await {- self.verify(config).await.ok()+ self.completed_size(config).await.ok()} else {None};@@ -95,47 +104,36 @@ pub trait Backend: Send + Sync {let _ = fs::asyn::remove_file(&config.resume_artifact_path).await;} else {let _ = fs::asyn::remove_file(&config.destination).await;- ChecksumReceipt::remove(&config.destination).await;}}let (phase, downloaded_bytes, total_bytes) = match (downloaded, resume_bytes, foreign_owner) {- (Some(size), _, _) => (DownloadPhase::Downloaded {}, size, Some(size)),- (None, downloaded_bytes, Some(manager_id)) => (+ (downloaded, downloaded_bytes, Some(manager_id)) => (DownloadPhase::Locked {manager_id,},- downloaded_bytes.unwrap_or(0),+ downloaded.or(downloaded_bytes).unwrap_or(0),None,),+ (Some(size), _, None) => (DownloadPhase::Downloaded {}, size, Some(size)),(None, Some(downloaded_bytes), None) => (DownloadPhase::Paused {}, downloaded_bytes, None),(None, None, None) => (DownloadPhase::NotDownloaded {}, 0, None),};DownloadState::new(config, phase, downloaded_bytes, total_bytes)}- async fn verify(+ async fn completed_size(&self,config: &DownloadConfig,- ) -> Result<u64, VerifyError> {+ ) -> Result<u64, BackendError> {let actual = fs::asyn::file_length(&config.destination).await?;if let Some(expected) = config.expected_bytes&& expected != actual{- return Err(VerifyError::Size {+ return Err(BackendError::Size {expected,actual,});}- let Some(checksum) = &config.expected_checksum else {- return Ok(actual);- };- if ChecksumReceipt::matches(&config.destination, checksum).await {- return Ok(actual);- }- if !checksum.verify(&config.destination).await? {- return Err(VerifyError::Checksum(checksum.algorithm()));- }- let _ = ChecksumReceipt::save(&config.destination, checksum).await;Ok(actual)}@@ -145,6 +143,100 @@ pub trait Backend: Send + Sync {) {let _ = fs::asyn::remove_file(&config.resume_artifact_path).await;let _ = fs::asyn::remove_file(&config.destination).await;- ChecksumReceipt::remove(&config.destination).await;+ }+}++#[cfg(test)]+mod tests {+ use uuid::Uuid;++ use super::*;+ use crate::locks::LockOwner;++ struct UnavailableNativeBackend;++ #[async_trait::async_trait]+ impl Backend for UnavailableNativeBackend {+ fn name(&self) -> &'static str {+ "unavailable"+ }+ fn resume_artifact_extension(&self) -> &'static str {+ "resume_data"+ }++ async fn start(+ &self,+ _config: Arc<DownloadConfig>,+ _generation: DownloadGeneration,+ _events: BackendEventSender,+ ) -> Result<Box<dyn ActiveTask>, BackendError> {+ unreachable!()+ }++ async fn read_resume_progress(+ &self,+ _path: &Path,+ ) -> u64 {+ 0+ }++ async fn has_pending_task(+ &self,+ _config: &DownloadConfig,+ ) -> Result<bool, BackendError> {+ Err(std::io::Error::other("native discovery unavailable").into())+ }++ async fn attach_pending_task(+ &self,+ _config: Arc<DownloadConfig>,+ _generation: DownloadGeneration,+ _events: BackendEventSender,+ ) -> Result<Option<Box<dyn ActiveTask>>, BackendError> {+ unreachable!()+ }+ }++ #[tokio::test]+ async fn startup_uses_metadata_and_preserves_native_recovery_markers() {+ let directory = tempfile::tempdir().unwrap();+ let destination = directory.path().join("large-model.bin");+ let config = DownloadConfig {+ download_id: Uuid::new_v4(),+ source_url: "https://example.invalid/model".to_string(),+ bearer_token: None,+ resume_artifact_path: destination.with_extension("bin.resume_data"),+ destination: destination.clone(),+ expected_bytes: Some(15_000_000_000),+ owner: LockOwner {+ manager_id: "test".to_string(),+ instance_id: Uuid::new_v4(),+ },+ };+ let backend = UnavailableNativeBackend;+ let (state, _) = backend.reconcile(&config).await.unwrap();+ assert_eq!(state.phase, DownloadPhase::NotDownloaded {});++ // This sparse file has no receipt; catalog initialization must never read its contents.+ tokio::fs::File::create(&destination).await.unwrap().set_len(config.expected_bytes.unwrap()).await.unwrap();+ let (state, _) =+ tokio::time::timeout(std::time::Duration::from_secs(2), backend.reconcile(&config)).await.unwrap().unwrap();+ assert_eq!(state.phase, DownloadPhase::Downloaded {});++ let foreign = LockOwner {+ manager_id: "other".to_string(),+ instance_id: Uuid::new_v4(),+ };+ let lock = DestinationLock::acquire(&destination, &foreign).await.unwrap();+ let (state, _) = backend.reconcile(&config).await.unwrap();+ assert!(matches!(state.phase, DownloadPhase::Locked { .. }));+ assert!(destination.is_file());+ drop(lock);++ tokio::fs::remove_file(&destination).await.unwrap();+ let marker = destination.with_extension("bin.lock");+ tokio::fs::write(&marker, b"stale marker from interrupted native download").await.unwrap();+ assert!(backend.reconcile(&config).await.is_err());+ assert!(marker.is_file(), "failed discovery must retain evidence of a possible background task");}}diff --git a/crates/legacy/download-manager/src/backends/backend_error.rs b/crates/legacy/download-manager/src/backends/backend_error.rsindex 94c4b760b..67182ac2c 100644--- a/crates/legacy/download-manager/src/backends/backend_error.rs+++ b/crates/legacy/download-manager/src/backends/backend_error.rs@@ -2,6 +2,11 @@ use crate::locks::LockError;#[derive(Debug, thiserror::Error)]pub enum BackendError {+ #[error("downloaded file is {actual} bytes but registry declared {expected}")]+ Size {+ expected: u64,+ actual: u64,+ },#[error(transparent)]Io(#[from] std::io::Error),#[error(transparent)]diff --git a/crates/legacy/download-manager/src/backends/mod.rs b/crates/legacy/download-manager/src/backends/mod.rsindex 2dad26391..9707491a4 100644--- a/crates/legacy/download-manager/src/backends/mod.rs+++ b/crates/legacy/download-manager/src/backends/mod.rs@@ -8,7 +8,6 @@ mod backend_event_sender;mod backend_progress;mod download_generation;mod universal;-mod verify_error;pub use active_task::ActiveTask;#[cfg(target_vendor = "apple")]@@ -20,4 +19,3 @@ pub use backend_event_sender::BackendEventSender;pub use backend_progress::BackendProgress;pub use download_generation::DownloadGeneration;pub use universal::UniversalBackend;-pub use verify_error::VerifyError;diff --git a/crates/legacy/download-manager/src/backends/verify_error.rs b/crates/legacy/download-manager/src/backends/verify_error.rsdeleted file mode 100644index 9058b6c78..000000000--- a/crates/legacy/download-manager/src/backends/verify_error.rs+++ /dev/null@@ -1,12 +0,0 @@-#[derive(Debug, thiserror::Error)]-pub enum VerifyError {- #[error(transparent)]- Io(#[from] std::io::Error),- #[error("downloaded file is {actual} bytes but registry declared {expected}")]- Size {- expected: u64,- actual: u64,- },- #[error("{0} verification failed")]- Checksum(&'static str),-}diff --git a/crates/legacy/download-manager/src/checksum.rs b/crates/legacy/download-manager/src/checksum.rsdeleted file mode 100644index 818dd2a56..000000000--- a/crates/legacy/download-manager/src/checksum.rs+++ /dev/null@@ -1,86 +0,0 @@-use std::{- io::{Error as IoError, ErrorKind},- path::Path,-};--use base64::Engine;-use kiban::fs;-use serde::{Deserialize, Serialize};-use sha1::Sha1;-use sha2::{Digest, Sha256};--const READ_CHUNK_SIZE: u64 = 8 * 1024 * 1024;--#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]-pub enum Checksum {- Crc32c(String),- Sha256(String),- GitBlobSha1(String),-}--impl Checksum {- pub fn algorithm(&self) -> &'static str {- match self {- Self::Crc32c(_) => "CRC32C",- Self::Sha256(_) => "SHA-256",- Self::GitBlobSha1(_) => "Git blob SHA-1",- }- }-- pub async fn verify(- &self,- path: &Path,- ) -> Result<bool, IoError> {- let length = fs::asyn::file_length(path).await?;- match self {- Self::Crc32c(expected) => {- let Some(expected) = base64::engine::general_purpose::STANDARD- .decode(expected)- .ok()- .and_then(|bytes| Some(u32::from_be_bytes(bytes.try_into().ok()?)))- else {- return Ok(false);- };- let actual = fold_chunks(path, length, 0_u32, |checksum, chunk| {- *checksum = ::crc32c::crc32c_append(*checksum, chunk)- })- .await?;- Ok(actual == expected)- },- Self::Sha256(expected) => {- let digest =- fold_chunks(path, length, Sha256::new(), |hasher, chunk| hasher.update(chunk)).await?.finalize();- Ok(hex(&digest) == expected.to_ascii_lowercase())- },- Self::GitBlobSha1(expected) => {- let mut hasher = Sha1::new();- hasher.update(format!("blob {length}\0"));- let digest = fold_chunks(path, length, hasher, |hasher, chunk| hasher.update(chunk)).await?.finalize();- Ok(hex(&digest) == expected.to_ascii_lowercase())- },- }- }-}--async fn fold_chunks<S>(- path: &Path,- length: u64,- mut state: S,- mut update: impl FnMut(&mut S, &[u8]),-) -> Result<S, IoError> {- let mut offset = 0_u64;- while offset < length {- let end = offset.saturating_add(READ_CHUNK_SIZE).min(length);- let chunk = fs::asyn::read_range(path, offset..end).await?;- if chunk.len() as u64 != end - offset {- return Err(IoError::new(ErrorKind::UnexpectedEof, "file changed during checksum verification"));- }- update(&mut state, &chunk);- offset = end;- }- Ok(state)-}--fn hex(bytes: &[u8]) -> String {- bytes.iter().map(|byte| format!("{byte:02x}")).collect()-}diff --git a/crates/legacy/download-manager/src/checksum_receipt.rs b/crates/legacy/download-manager/src/checksum_receipt.rsdeleted file mode 100644index 3f002c1f5..000000000--- a/crates/legacy/download-manager/src/checksum_receipt.rs+++ /dev/null@@ -1,71 +0,0 @@-use std::{- io,- path::{Path, PathBuf},-};--use kiban::{fs, time::SystemTime};-use serde::{Deserialize, Serialize};--use crate::Checksum;--#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]-pub struct ChecksumReceipt {- version: u8,- checksum: Checksum,- file_size: u64,- modified_unix_seconds: u64,- modified_nanos: u32,-}--impl ChecksumReceipt {- pub async fn matches(- destination: &Path,- checksum: &Checksum,- ) -> bool {- matches!(- (Self::load(destination).await, Self::for_file(destination, checksum).await),- (Some(saved), Some(current)) if saved == current- )- }-- pub async fn save(- destination: &Path,- checksum: &Checksum,- ) -> Result<(), io::Error> {- let Some(receipt) = Self::for_file(destination, checksum).await else {- return Ok(());- };- fs::asyn::write(Self::path_for(destination), serde_json::to_vec(&receipt).map_err(io::Error::other)?).await- }-- pub async fn exists(destination: &Path) -> bool {- fs::asyn::is_file(Self::path_for(destination)).await- }-- pub async fn remove(destination: &Path) {- let _ = fs::asyn::remove_file(Self::path_for(destination)).await;- }-- async fn load(destination: &Path) -> Option<Self> {- serde_json::from_str(&fs::asyn::read_to_string(Self::path_for(destination)).await.ok()?).ok()- }-- async fn for_file(- destination: &Path,- checksum: &Checksum,- ) -> Option<Self> {- let file_size = fs::asyn::file_length(destination).await.ok()?;- let modified = fs::asyn::file_modified(destination).await.ok()?.duration_since(SystemTime::UNIX_EPOCH).ok()?;- Some(Self {- version: 2,- checksum: checksum.clone(),- file_size,- modified_unix_seconds: modified.as_secs(),- modified_nanos: modified.subsec_nanos(),- })- }-- fn path_for(destination: &Path) -> PathBuf {- PathBuf::from(format!("{}.checksum", destination.display()))- }-}diff --git a/crates/legacy/download-manager/src/download_manager.rs b/crates/legacy/download-manager/src/download_manager.rsindex 0ea98ba47..ea2a21ea1 100644--- a/crates/legacy/download-manager/src/download_manager.rs+++ b/crates/legacy/download-manager/src/download_manager.rs@@ -131,7 +131,6 @@ impl DownloadManager {DownloadTaskKind::File {source_url,bearer_token,- expected_checksum,expected_bytes,} => {if bearer_token.is_some() && !carries_token_securely(source_url) {@@ -143,7 +142,6 @@ impl DownloadManager {bearer_token: bearer_token.clone(),destination: request.destination.clone(),resume_artifact_path: self.backend.resume_artifact_path(&request.destination),- expected_checksum: expected_checksum.clone(),expected_bytes: *expected_bytes,owner: self.owner.clone(),});diff --git a/crates/legacy/download-manager/src/download_phase.rs b/crates/legacy/download-manager/src/download_phase.rsindex 0dfff4b33..3a12ecd43 100644--- a/crates/legacy/download-manager/src/download_phase.rs+++ b/crates/legacy/download-manager/src/download_phase.rs@@ -13,10 +13,11 @@ pub enum DownloadPhase {Error {message: String,},+ Initializing {},}impl DownloadPhase {pub fn is_in_progress(&self) -> bool {- matches!(self, Self::Downloading {} | Self::Locked { .. })+ matches!(self, Self::Initializing {} | Self::Downloading {} | Self::Locked { .. })}}diff --git a/crates/legacy/download-manager/src/download_state.rs b/crates/legacy/download-manager/src/download_state.rsindex 9b6c4c0ef..50f3d0603 100644--- a/crates/legacy/download-manager/src/download_state.rs+++ b/crates/legacy/download-manager/src/download_state.rs@@ -43,12 +43,16 @@ impl DownloadState {#[bindings::export(Method(Getter))]pub fn can_delete(&self) -> bool {- !matches!(self.phase, DownloadPhase::NotDownloaded {} | DownloadPhase::Locked { .. })+ !matches!(+ self.phase,+ DownloadPhase::Initializing {} | DownloadPhase::NotDownloaded {} | DownloadPhase::Locked { .. }+ )}#[bindings::export(Method(Getter))]pub fn name(&self) -> String {match &self.phase {+ DownloadPhase::Initializing {} => "Loading".to_string(),DownloadPhase::NotDownloaded {} => "Not Downloaded".to_string(),DownloadPhase::Downloading {} => "Downloading".to_string(),DownloadPhase::Paused {} => "Paused".to_string(),diff --git a/crates/legacy/download-manager/src/download_task_kind.rs b/crates/legacy/download-manager/src/download_task_kind.rsindex 528e41d71..00eb17c34 100644--- a/crates/legacy/download-manager/src/download_task_kind.rs+++ b/crates/legacy/download-manager/src/download_task_kind.rs@@ -1,11 +1,10 @@-use crate::{BearerToken, Checksum, DownloadTaskRequest};+use crate::{BearerToken, DownloadTaskRequest};#[derive(Clone, Debug, PartialEq, Eq)]pub enum DownloadTaskKind {File {source_url: String,bearer_token: Option<BearerToken>,- expected_checksum: Option<Checksum>,expected_bytes: Option<u64>,},Group(Vec<DownloadTaskRequest>),diff --git a/crates/legacy/download-manager/src/download_task_request.rs b/crates/legacy/download-manager/src/download_task_request.rsindex efa5588d1..9695160af 100644--- a/crates/legacy/download-manager/src/download_task_request.rs+++ b/crates/legacy/download-manager/src/download_task_request.rs@@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};use bon::bon;use uuid::Uuid;-use crate::{BearerToken, Checksum, DownloadId, DownloadTaskKind};+use crate::{BearerToken, DownloadId, DownloadTaskKind};#[derive(Clone, Debug, PartialEq, Eq)]pub struct DownloadTaskRequest {@@ -18,7 +18,6 @@ impl DownloadTaskRequest {#[builder(into)] destination: PathBuf,#[builder(into)] source_url: String,#[builder(into)] bearer_token: Option<BearerToken>,- expected_checksum: Option<Checksum>,expected_bytes: Option<u64>,) -> Self {Self {@@ -26,7 +25,6 @@ impl DownloadTaskRequest {kind: DownloadTaskKind::File {source_url,bearer_token,- expected_checksum,expected_bytes,},}diff --git a/crates/legacy/download-manager/src/file_download/download_config.rs b/crates/legacy/download-manager/src/file_download/download_config.rsindex 52785e229..19d216a9b 100644--- a/crates/legacy/download-manager/src/file_download/download_config.rs+++ b/crates/legacy/download-manager/src/file_download/download_config.rs@@ -1,6 +1,6 @@use std::path::PathBuf;-use crate::{BearerToken, Checksum, DownloadId, locks::LockOwner};+use crate::{BearerToken, DownloadId, locks::LockOwner};pub struct DownloadConfig {pub download_id: DownloadId,@@ -8,7 +8,6 @@ pub struct DownloadConfig {pub bearer_token: Option<BearerToken>,pub destination: PathBuf,pub resume_artifact_path: PathBuf,- pub expected_checksum: Option<Checksum>,pub expected_bytes: Option<u64>,pub owner: LockOwner,}diff --git a/crates/legacy/download-manager/src/file_download/file_download_worker.rs b/crates/legacy/download-manager/src/file_download/file_download_worker.rsindex dfa248184..c93e4c5b2 100644--- a/crates/legacy/download-manager/src/file_download/file_download_worker.rs+++ b/crates/legacy/download-manager/src/file_download/file_download_worker.rs@@ -108,7 +108,8 @@ impl FileDownloadWorker {| DownloadPhase::Error {..} => self.start().await,- DownloadPhase::Downloading {}+ DownloadPhase::Initializing {}+ | DownloadPhase::Downloading {}| DownloadPhase::Downloaded {}| DownloadPhase::Locked {..@@ -208,7 +209,7 @@ impl FileDownloadWorker {}async fn complete(&mut self) {- match self.backend.verify(&self.config).await {+ match self.backend.completed_size(&self.config).await {Ok(total_bytes) => {let _ = fs::asyn::remove_file(&self.config.resume_artifact_path).await;self.set(DownloadPhase::Downloaded {}, total_bytes, Some(total_bytes));diff --git a/crates/legacy/download-manager/src/group_download_task.rs b/crates/legacy/download-manager/src/group_download_task.rsindex 867052a86..5565f24f3 100644--- a/crates/legacy/download-manager/src/group_download_task.rs+++ b/crates/legacy/download-manager/src/group_download_task.rs@@ -161,7 +161,7 @@ async fn download_in_order(children: &[Arc<DownloadTask>]) {let mut progress = child.progress();match child.state().phase {DownloadPhase::Downloaded {} => continue,- DownloadPhase::Downloading {} => {},+ DownloadPhase::Initializing {} | DownloadPhase::Downloading {} => {},_ => {if child.download().await.is_err() {return;diff --git a/crates/legacy/download-manager/src/lib.rs b/crates/legacy/download-manager/src/lib.rsindex 3e7b03d92..e40155bb4 100644--- a/crates/legacy/download-manager/src/lib.rs+++ b/crates/legacy/download-manager/src/lib.rs@@ -3,8 +3,6 @@ uniffi::setup_scaffolding!();mod backends;mod bearer_token;-mod checksum;-mod checksum_receipt;mod download_error;mod download_id;mod download_manager;@@ -22,7 +20,6 @@ mod locks;pub use backends::AppleBackendError;pub use backends::BackendError;pub use bearer_token::BearerToken;-pub use checksum::Checksum;pub use download_error::DownloadError;pub use download_id::DownloadId;pub use download_manager::DownloadManager;diff --git a/crates/legacy/download-manager/src/locks/destination_lock.rs b/crates/legacy/download-manager/src/locks/destination_lock.rsindex ae0ec3ec9..0a55cb443 100644--- a/crates/legacy/download-manager/src/locks/destination_lock.rs+++ b/crates/legacy/download-manager/src/locks/destination_lock.rs@@ -11,6 +11,10 @@ pub struct DestinationLock {}impl DestinationLock {+ pub(crate) async fn exists(destination: &Path) -> bool {+ fs::asyn::try_exists(Self::path_for(destination)).await.unwrap_or(false)+ }+pub async fn acquire(destination: &Path,owner: &LockOwner,@@ -48,7 +52,16 @@ impl DestinationLock {pub async fn held_within(directory: &Path) -> bool {let mut directories = vec![directory.to_path_buf()];while let Some(directory) = directories.pop() {- for path in fs::asyn::read_dir(&directory).await.unwrap_or_default() {+ let Ok(paths) = fs::asyn::read_dir(&directory).await else {+ return true;+ };+ for path in paths {+ // Cleanup must not follow links outside the checkpoint or infer+ // that unreadable contents are safe to delete.+ #[cfg(not(target_family = "wasm"))]+ if !tokio::fs::symlink_metadata(&path).await.is_ok_and(|metadata| !metadata.is_symlink()) {+ return true;+ }if !fs::asyn::is_file(&path).await {directories.push(path);} else if path.extension().is_some_and(|extension| extension == "lock")diff --git a/crates/legacy/download-manager/tests/common/helpers.rs b/crates/legacy/download-manager/tests/common/helpers.rsindex 8142980d3..b0b23f101 100644--- a/crates/legacy/download-manager/tests/common/helpers.rs+++ b/crates/legacy/download-manager/tests/common/helpers.rs@@ -1,6 +1,6 @@use std::{path::Path, time::Duration};-use download_manager::{Checksum, DestinationLock, DownloadState, DownloadTask, DownloadTaskRequest, LockOwner};+use download_manager::{DestinationLock, DownloadState, DownloadTask, DownloadTaskRequest, LockOwner};use kiban::stream::BoxStream;use mock_registry::MockRegistry;use tokio::time::timeout;@@ -10,13 +10,11 @@ use uuid::Uuid;pub fn file_request(source_url: &str,destination: &Path,- expected_crc32c: Option<String>,expected_bytes: Option<u64>,) -> DownloadTaskRequest {DownloadTaskRequest::file().destination(destination).source_url(source_url)- .maybe_expected_checksum(expected_crc32c.map(Checksum::Crc32c)).maybe_expected_bytes(expected_bytes).build()}@@ -24,19 +22,18 @@ pub fn file_request(pub fn model_request(registry: &MockRegistry,directory: &Path,-) -> Result<DownloadTaskRequest, Box<dyn std::error::Error>> {+) -> DownloadTaskRequest {let mut files = Vec::new();for served in registry.files.iter() {files.push(DownloadTaskRequest::file().destination(&served.file.name).source_url(&served.file.url)- .expected_checksum(Checksum::Crc32c(served.crc32c()?)).expected_bytes(served.file.size as u64).build(),);}- Ok(DownloadTaskRequest::group().destination(directory).subrequests(files).build())+ DownloadTaskRequest::group().destination(directory).subrequests(files).build()}pub async fn wait_for_state(diff --git a/crates/legacy/download-manager/tests/download_manager.rs b/crates/legacy/download-manager/tests/download_manager.rsindex 8ce80106f..026e3346c 100644--- a/crates/legacy/download-manager/tests/download_manager.rs+++ b/crates/legacy/download-manager/tests/download_manager.rs@@ -3,8 +3,8 @@ mod common;use std::{path::Path, sync::Arc, time::Duration};use download_manager::{- Checksum, DestinationLock, DownloadError, DownloadManager, DownloadManagerType, DownloadPhase, DownloadState,- DownloadTask, DownloadTaskRequest, LockError, LockOwner,+ DestinationLock, DownloadError, DownloadManager, DownloadManagerType, DownloadPhase, DownloadState, DownloadTask,+ DownloadTaskRequest, LockError, LockOwner,};use kiban::rt::RuntimeHandle;use rstest::rstest;@@ -18,8 +18,6 @@ use wiremock::{use crate::common::{Behavior, MockRegistry, artifact_path, file_request, foreign_lock, model_request, wait_for_state};const HELLO: &[u8] = b"hello\n";-const HELLO_SHA256: &str = "5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03";-const HELLO_GIT_BLOB_SHA1: &str = "ce013625030ba8dba906f756967f9e9ca394464a";fn manager(kind: DownloadManagerType) -> DownloadManager {DownloadManager::new(kind, RuntimeHandle::current())@@ -45,7 +43,7 @@ async fn model_lifecycle(#[case] kind: DownloadManagerType) -> Result<(), Box<dylet registry = MockRegistry::start_with(Behavior::THROTTLED).await?;let directory = tempfile::tempdir()?;let manager = manager(kind);- let request = model_request(®istry, directory.path())?;+ let request = model_request(®istry, directory.path());let total_bytes: i64 = registry.files.iter().map(|served| served.file.size).sum();let mut last_downloaded_bytes = 0;@@ -93,7 +91,7 @@ async fn model_lifecycle(#[case] kind: DownloadManagerType) -> Result<(), Box<dyfor served in registry.files.iter() {let destination = directory.path().join(&served.file.name);assert_eq!(tokio::fs::read(&destination).await?, served.bytes.to_vec());- assert!(artifact_path(&destination, "checksum").is_file());+ assert!(!artifact_path(&destination, "checksum").exists());}model.delete().await?;@@ -110,12 +108,11 @@ async fn model_lifecycle(#[case] kind: DownloadManagerType) -> Result<(), Box<dylet destination = directory.path().join(&first.file.name);let expected_bytes = Some(first.file.size as u64);let conflicting_url =- manager.download_task(file_request("http://example.invalid/other", &destination, None, expected_bytes)).await;+ manager.download_task(file_request("http://example.invalid/other", &destination, expected_bytes)).await;assert!(matches!(conflicting_url, Err(DownloadError::ConflictingConfig(_))));- let conflicting_size =- manager.download_task(file_request(&first.file.url, &destination, Some(first.crc32c()?), Some(1))).await;+ let conflicting_size = manager.download_task(file_request(&first.file.url, &destination, Some(1))).await;assert!(matches!(conflicting_size, Err(DownloadError::ConflictingConfig(_))));- let same = file_request(&first.file.url, &destination, Some(first.crc32c()?), expected_bytes);+ let same = file_request(&first.file.url, &destination, expected_bytes);let (task_a, task_b) = tokio::join!(manager.download_task(same.clone()), manager.download_task(same));assert!(Arc::ptr_eq(&task_a?, &task_b?));Ok(())@@ -131,10 +128,9 @@ async fn startup_reconciliation() -> Result<(), Box<dyn std::error::Error>> {let registry = MockRegistry::start().await?;let served = registry.file("config.json")?;- let crc = served.crc32c()?;let size = Some(served.file.size as u64);let manager = manager(kind);- let request = |destination: &Path| file_request(&served.file.url, destination, Some(crc.clone()), size);+ let request = |destination: &Path| file_request(&served.file.url, destination, size);let valid = tempfile::tempdir()?;let destination = valid.path().join(&served.file.name);@@ -144,26 +140,13 @@ async fn startup_reconciliation(let task = manager.download_task(request(&destination)).await?;assert_eq!(task.state().phase, DownloadPhase::Downloaded {});assert!(!artifact.exists());- let receipt: serde_json::Value =- serde_json::from_str(&tokio::fs::read_to_string(artifact_path(&destination, "checksum")).await?)?;- assert_eq!(receipt["version"].as_u64(), Some(2));- assert_eq!(receipt["checksum"]["Crc32c"].as_str(), Some(crc.as_str()));- assert_eq!(receipt["file_size"].as_u64(), size);-- let mut changed_bytes = served.bytes.to_vec();- changed_bytes[0] = changed_bytes[0].wrapping_add(1);- let stale_receipt = serde_json::to_vec(&serde_json::json!({- "version": 2,- "checksum": { "Crc32c": crc.clone() },- "file_size": served.file.size,- "modified_unix_seconds": 0,- "modified_nanos": 0,- }))?;- for cache in [crc.clone().into_bytes(), stale_receipt] {- let stale = tempfile::tempdir()?;- let destination = stale.path().join(&served.file.name);- tokio::fs::write(&destination, &changed_bytes).await?;- tokio::fs::write(artifact_path(&destination, "checksum"), cache).await?;+ assert!(!artifact_path(&destination, "checksum").exists());++ // Crash leftovers and truncated files cannot be loaded as completed models.+ for actual_size in [served.file.size - 1, served.file.size + 1] {+ let incomplete = tempfile::tempdir()?;+ let destination = incomplete.path().join(&served.file.name);+ tokio::fs::File::create(&destination).await?.set_len(actual_size as u64).await?;let task = manager.download_task(request(&destination)).await?;assert_eq!(task.state().phase, DownloadPhase::NotDownloaded {});assert!(!destination.exists());@@ -172,33 +155,26 @@ async fn startup_reconciliation(let folder = tempfile::tempdir()?;let destination = folder.path().join(&served.file.name);tokio::fs::create_dir(&destination).await?;- let task = manager.download_task(file_request(&served.file.url, &destination, None, None)).await?;+ let task = manager.download_task(file_request(&served.file.url, &destination, None)).await?;assert_eq!(task.state().phase, DownloadPhase::NotDownloaded {});let locked = tempfile::tempdir()?;let destination = locked.path().join(&served.file.name);let artifact = artifact_path(&destination, resume_artifact_extension);tokio::fs::write(&destination, b"corrupt").await?;- tokio::fs::write(artifact_path(&destination, "checksum"), &crc).await?;tokio::fs::write(&artifact, b"partial").await?;let lock = foreign_lock(&destination).await;let group = manager.download_task(DownloadTaskRequest::group().destination(locked.path())- .subrequests(vec![file_request(- &served.file.url,- Path::new(&served.file.name),- Some(crc.clone()),- size,- )])+ .subrequests(vec![file_request(&served.file.url, Path::new(&served.file.name), size)]).build(),).await?;assert!(matches!(group.state().phase, DownloadPhase::Locked { .. }));assert!(matches!(group.delete().await, Err(DownloadError::Lock(LockError::LockedByOther { .. }))));assert!(destination.exists());- assert!(artifact_path(&destination, "checksum").exists());assert!(artifact.exists());let mut progress = group.progress();if kind == DownloadManagerType::Universal {@@ -239,37 +215,13 @@ async fn startup_reconciliation(async fn failures(#[case] kind: DownloadManagerType) -> Result<(), Box<dyn std::error::Error>> {let manager = manager(kind);- let corrupt = MockRegistry::start_with(Behavior::CORRUPT_BODY).await?;- let served = corrupt.file("tokenizer.json")?;let directory = tempfile::tempdir()?;- let destination = directory.path().join(&served.file.name);- let task = manager- .download_task(file_request(- &served.file.url,- &destination,- Some(served.crc32c()?),- Some(served.file.size as u64),- ))- .await?;- let mut progress = task.progress();- task.download().await?;- let state = wait_for_state(&task, &mut progress, |state| matches!(state.phase, DownloadPhase::Error { .. })).await;- let DownloadPhase::Error {- message,- } = state.phase- else {- panic!("expected an error phase, got {:?}", state.phase)- };- assert!(message.contains("CRC"), "unexpected error: {message}");- task.delete().await?;- assert_eq!(task.state().phase, DownloadPhase::NotDownloaded {});let truncated = MockRegistry::start_with(Behavior::TRUNCATE_BODY).await?;let served = truncated.file("config.json")?;let destination = directory.path().join(&served.file.name);- let task = manager- .download_task(file_request(&served.file.url, &destination, None, Some(served.file.size as u64)))- .await?;+ let task =+ manager.download_task(file_request(&served.file.url, &destination, Some(served.file.size as u64))).await?;let mut progress = task.progress();task.download().await?;let state = wait_for_state(&task, &mut progress, |state| {@@ -284,6 +236,41 @@ async fn failures(#[case] kind: DownloadManagerType) -> Result<(), Box<dyn std::Ok(())}+#[rstest]+#[case::universal(DownloadManagerType::Universal)]+#[cfg_attr(target_vendor = "apple", case::native(DownloadManagerType::Native))]+#[tokio::test(flavor = "multi_thread")]+async fn declared_size_must_match(#[case] kind: DownloadManagerType) -> Result<(), Box<dyn std::error::Error>> {+ let manager = manager(kind);+ let server = MockServer::start().await;+ Mock::given(method("GET"))+ .and(path("/model.bin"))+ .respond_with(ResponseTemplate::new(200).set_body_bytes(HELLO))+ .mount(&server)+ .await;+ let directory = tempfile::tempdir()?;+ for expected in [HELLO.len() - 1, HELLO.len(), HELLO.len() + 1] {+ let destination = directory.path().join(expected.to_string());+ let task = manager+ .download_task(file_request(&format!("{}/model.bin", server.uri()), &destination, Some(expected as u64)))+ .await?;+ let mut progress = task.progress();+ task.download().await?;+ let state = wait_for_state(&task, &mut progress, |state| {+ matches!(state.phase, DownloadPhase::Downloaded {} | DownloadPhase::Error { .. })+ })+ .await;+ if expected == HELLO.len() {+ assert_eq!(state.phase, DownloadPhase::Downloaded {});+ assert_eq!(tokio::fs::read(&destination).await?, HELLO);+ } else {+ assert!(matches!(state.phase, DownloadPhase::Error { .. }));+ assert!(!destination.exists());+ }+ }+ Ok(())+}+#[rstest]#[case::universal(DownloadManagerType::Universal, "part")]#[cfg_attr(target_vendor = "apple", case::native(DownloadManagerType::Native, "resume_data"))]@@ -298,9 +285,7 @@ async fn shutdown_and_locks(let destination = directory.path().join(&served.file.name);let lock = artifact_path(&destination, "lock");let artifact = artifact_path(&destination, resume_artifact_extension);- let request = || {- file_request(&served.file.url, &destination, Some(served.crc32c().expect("crc")), Some(served.file.size as u64))- };+ let request = || file_request(&served.file.url, &destination, Some(served.file.size as u64));let manager_a = manager(kind);let task_a = manager_a.download_task(request()).await?;@@ -358,7 +343,6 @@ async fn universal_resume() -> Result<(), Box<dyn std::error::Error>> {.download_task(file_request(&format!("{}/model.bin", ignoring_range.uri()),&destination,- None,Some(full_bytes.len() as u64),)).await?;@@ -385,7 +369,6 @@ async fn universal_resume() -> Result<(), Box<dyn std::error::Error>> {.download_task(file_request(&format!("{}/model.bin", misaligned.uri()),&destination,- None,Some(full_bytes.len() as u64),)).await?;@@ -414,7 +397,7 @@ async fn locked_takeover(#[case] kind: DownloadManagerType) -> Result<(), Box<dylet manager = manager(kind);let first = registry.files.first().ok_or("mock registry must include files")?;let lock = foreign_lock(&directory.path().join(&first.file.name)).await;- let group = manager.download_task(model_request(®istry, directory.path())?).await?;+ let group = manager.download_task(model_request(®istry, directory.path())).await?;let mut progress = group.progress();group.download().await?;assert!(matches!(group.state().phase, DownloadPhase::Locked { .. }));@@ -434,65 +417,13 @@ async fn ancestor_destination_rejected(#[case] kind: DownloadManagerType) -> Reslet directory = tempfile::tempdir()?;let request = DownloadTaskRequest::group().destination(directory.path())- .subrequests(vec![file_request("http://127.0.0.1/unused", directory.path(), None, None)])+ .subrequests(vec![file_request("http://127.0.0.1/unused", directory.path(), None)]).build();let result = timeout(Duration::from_secs(5), manager(kind).download_task(request)).await?;assert!(matches!(result, Err(DownloadError::ConflictingConfig(_))));Ok(())}-#[rstest]-#[case::universal(DownloadManagerType::Universal)]-#[cfg_attr(target_vendor = "apple", case::native(DownloadManagerType::Native))]-#[tokio::test(flavor = "multi_thread")]-async fn checksums(#[case] kind: DownloadManagerType) -> Result<(), Box<dyn std::error::Error>> {- let manager = manager(kind);- let server = MockServer::start().await;- Mock::given(method("GET"))- .and(path("/hello"))- .respond_with(ResponseTemplate::new(200).set_body_bytes(HELLO))- .mount(&server)- .await;- Mock::given(method("GET"))- .and(path("/jello"))- .respond_with(ResponseTemplate::new(200).set_body_bytes(b"jello\n".as_slice()))- .mount(&server)- .await;- let directory = tempfile::tempdir()?;- for (checksum, algorithm) in [- (Checksum::Sha256(HELLO_SHA256.to_string()), "SHA-256"),- (Checksum::GitBlobSha1(HELLO_GIT_BLOB_SHA1.to_string()), "Git blob SHA-1"),- ] {- let request = |name: &str| {- DownloadTaskRequest::file()- .destination(directory.path().join(format!("{algorithm} {name}")))- .source_url(format!("{}/{name}", server.uri()))- .expected_checksum(checksum.clone())- .expected_bytes(HELLO.len() as u64)- .build()- };- let task = manager.download_task(request("hello")).await?;- let mut progress = task.progress();- task.download().await?;- wait_for_state(&task, &mut progress, |state| matches!(state.phase, DownloadPhase::Downloaded {})).await;- assert!(artifact_path(&task.request().destination, "checksum").is_file());-- let corrupt = manager.download_task(request("jello")).await?;- let mut progress = corrupt.progress();- corrupt.download().await?;- let state =- wait_for_state(&corrupt, &mut progress, |state| matches!(state.phase, DownloadPhase::Error { .. })).await;- let DownloadPhase::Error {- message,- } = state.phase- else {- panic!("expected an error phase, got {:?}", state.phase)- };- assert!(message.contains(algorithm), "unexpected error: {message}");- }- Ok(())-}-#[rstest]#[case::universal(DownloadManagerType::Universal)]#[cfg_attr(target_vendor = "apple", case::native(DownloadManagerType::Native))]diff --git a/crates/legacy/hanashi/configs/ordering/functiongemma.json b/crates/legacy/hanashi/configs/ordering/functiongemma.jsonindex e996cee25..52e03ac3c 100644--- a/crates/legacy/hanashi/configs/ordering/functiongemma.json+++ b/crates/legacy/hanashi/configs/ordering/functiongemma.json@@ -15,7 +15,8 @@],"tool": ["tool",- "assistant"+ "assistant",+ "user"]}-}\ No newline at end of file+}diff --git a/crates/legacy/hanashi/configs/rendering/functiongemma.json b/crates/legacy/hanashi/configs/rendering/functiongemma.jsonindex 27a240484..624d3499f 100644--- a/crates/legacy/hanashi/configs/rendering/functiongemma.json+++ b/crates/legacy/hanashi/configs/rendering/functiongemma.json@@ -1,6 +1,6 @@{"jinja": {- "template": "{%- macro format_parameters(properties, required) -%}\n {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in properties | dictsort -%}\n {%- if key not in standard_keys -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {{- key }}:{description:<escape>{{ value['description'] }}<escape>\n {%- if value['type'] | upper == 'STRING' -%}\n {%- if value['enum'] -%}\n ,enum:{{ format_argument(value['enum']) }}\n {%- endif -%}\n {%- elif value['type'] | upper == 'OBJECT' -%}\n ,properties:{\n {%- if value['properties'] is defined and value['properties'] is mapping -%}\n {{- format_parameters(value['properties'], value['required'] | default([])) -}}\n {%- elif value is mapping -%}\n {{- format_parameters(value, value['required'] | default([])) -}}\n {%- endif -%}\n }\n {%- if value['required'] -%}\n ,required:[\n {%- for item in value['required'] | default([]) -%}\n <escape>{{- item -}}<escape>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- endif -%}\n {%- elif value['type'] | upper == 'ARRAY' -%}\n {%- if value['items'] is mapping and value['items'] -%}\n ,items:{\n {%- set ns_items = namespace(found_first=false) -%}\n {%- for item_key, item_value in value['items'] | dictsort -%}\n {%- if item_value is not none -%}\n {%- if ns_items.found_first %},{% endif -%}\n {%- set ns_items.found_first = true -%}\n {%- if item_key == 'properties' -%}\n properties:{\n {%- if item_value is mapping -%}\n {{- format_parameters(item_value, value['items']['required'] | default([])) -}}\n {%- endif -%}\n }\n {%- elif item_key == 'required' -%}\n required:[\n {%- for req_item in item_value -%}\n <escape>{{- req_item -}}<escape>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- elif item_key == 'type' -%}\n {%- if item_value is string -%}\n type:{{ format_argument(item_value | upper) }}\n {%- else -%}\n type:{{ format_argument(item_value | map('upper') | list) }}\n {%- endif -%}\n {%- else -%}\n {{ item_key }}:{{ format_argument(item_value) }}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n }\n {%- endif -%}\n {%- endif -%}\n ,type:<escape>{{ value['type'] | upper }}<escape>}\n {%- endif -%}\n {%- endfor -%}\n{%- endmacro -%}\n{% macro format_function_declaration(tool_data) -%}\ndeclaration:{{- tool_data['function']['name'] -}}\n{description:<escape>{{- tool_data['function']['description'] -}}<escape>\n{%- set params = tool_data['function']['parameters'] -%}\n{%- if params -%}\n ,parameters:{\n {%- if params['properties'] -%}\n properties:{ {{- format_parameters(params['properties'], params['required']) -}} },\n {%- endif -%}\n {%- if params['required'] -%}\n required:[\n {%- for item in params['required'] -%}\n <escape>{{- item -}}<escape>\n {{- ',' if not loop.last -}}\n {%- endfor -%}\n ],\n {%- endif -%}\n {%- if params['type'] -%}\n type:<escape>{{- params['type'] | upper -}}<escape>}\n {%- endif -%}\n{%- endif -%}\n}\n{%- endmacro -%}\n{% macro format_argument(argument, escape_keys=True) -%}\n{%- if argument is string -%}\n {{- '<escape>' + argument + '<escape>' -}}\n{%- elif argument is boolean -%}\n {%- if argument -%}\n {{- 'true' -}}\n {%- else -%}\n {{- 'false' -}}\n {%- endif -%}\n{%- elif argument is mapping -%}\n {{- '{' -}}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in argument | dictsort -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {%- if escape_keys -%}\n {{- '<escape>' + key + '<escape>' -}}\n {%- else -%}\n {{- key -}}\n {%- endif -%}\n :{{- format_argument(value, escape_keys=escape_keys) -}}\n {%- endfor -%}\n {{- '}' -}}\n{%- elif argument is sequence -%}\n {{- '[' -}}\n {%- for item in argument -%}\n {{- format_argument(item, escape_keys=escape_keys) -}}\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n {{- ']' -}}\n{%- else -%}\n {{- argument -}}\n{%- endif -%}\n{%- endmacro -%}\n{{ bos_token }}\n{%- set ns = namespace(prev_message_type=None) -%}\n{#- Tool Declarations -#}\n{%- set loop_messages = messages -%}\n{%- if tools or messages[0]['role'] == 'system' or messages[0]['role'] == 'developer' -%}\n {{- '<start_of_turn>developer\\n' -}}\n {%- if messages[0]['role'] == 'system' or messages[0]['role'] == 'developer' -%}\n {%- if messages[0]['content'] is string -%}\n {{- messages[0]['content'] | trim -}}\n {%- elif messages[0]['content'] is sequence -%}\n {%- for item in messages[0]['content'] -%}\n {%- if item['type'] == 'text' -%}\n {{- item['text'] | trim -}}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- set loop_messages = messages[1:] -%}\n {%- endif -%}\n {%- if tools -%}\n {%- for tool in tools %}\n {{- '<start_function_declaration>' -}}\n {{- format_function_declaration(tool) | trim }}\n {{- '<end_function_declaration>' -}}\n {%- endfor %}\n {%- endif -%}\n {{- '<end_of_turn>\\n' }}\n{%- endif %}\n{#- Loop through messages. -#}\n{%- for message in loop_messages -%}\n {%- if (message['role'] == 'assistant') -%}\n {#- Rename \"assistant\" to \"model\". -#}\n {%- set role = \"model\" -%}\n {%- else -%}\n {%- set role = message['role'] -%}\n {%- endif -%}\n {%- if role != 'tool' -%}\n {%- if ns.prev_message_type != 'tool_response' -%}\n {{- '<start_of_turn>' + role + '\\n' }}\n {%- endif -%}\n {%- set ns.prev_message_type = None -%}\n {%- if 'content' in message and message['content'] is not none -%}\n {%- if message['content'] is string -%}\n {{ message['content'] | trim }}\n {%- elif message['content'] is sequence -%}\n {%- for item in message['content'] -%}\n {%- if item['type'] == 'image' -%}\n {{ '<start_of_image>' }}\n {%- elif item['type'] == 'text' -%}\n {{ item['text'] | trim }}\n {%- endif -%}\n {%- endfor -%}\n {%- else -%}\n {{ raise_exception(\"Invalid content type in user/assistant message\") }}\n {%- endif -%}\n {%- set ns.prev_message_type = 'content' -%}\n {%- endif -%}\n {%- if 'tool_calls' in message and message['tool_calls'] and message['tool_calls'] is iterable -%}\n {#- Tool Calls -#}\n {%- for tool_call in message['tool_calls'] -%}\n {% set function = tool_call['function'] if tool_call['function'] is defined else tool_call %}\n {{- '<start_function_call>call:' + function['name'] + '{' -}}\n {%- if 'arguments' in function -%}\n {%- if function['arguments'] is mapping -%}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in function['arguments'] | dictsort -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {%- elif function['arguments'] is string -%}\n {# This handles string-JSON, just in case #}\n {{ function['arguments'] }}\n {%- endif %}\n {%- endif -%}\n {{- '}<end_function_call>' -}}\n {%- endfor -%}\n {%- if loop.last -%}\n {{ '<start_function_response>' }}\n {%- endif -%}\n {%- set ns.prev_message_type = 'tool_call' -%}\n {%- endif -%}\n {%- else -%}\n {#- Tool Responses -#}\n {%- if 'content' in message and message['content'] -%}\n {%- if message['content'] is mapping -%}\n {%- if 'name' in message['content'] and 'response' in message['content'] -%}\n {{ '<start_function_response>response:' + message['content']['name'] | trim + '{' }}\n {%- set response_ns = namespace(found_first=false) -%}\n {%- for key, value in message['content']['response'] | dictsort -%}\n {%- if response_ns.found_first %},{% endif -%}\n {%- set response_ns.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {{- '}<end_function_response>' -}}\n {%- elif 'name' in message -%}\n {{ '<start_function_response>response:' + message['name'] | trim + '{' }}\n {%- set response_ns = namespace(found_first=false) -%}\n {%- for key, value in message['content'] | dictsort -%}\n {%- if response_ns.found_first %},{% endif -%}\n {%- set response_ns.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {{- '}<end_function_response>' -}}\n {%- else -%}\n {{ raise_exception(\"Invalid tool response mapping: must contain 'name' and 'response' keys, or 'name' must be in the message.\") }}\n {%- endif -%}\n {%- elif message['content'] is string -%}\n {%- if 'name' in message -%}\n {{ '<start_function_response>response:' + message['name'] | trim + '{value:' + format_argument(message['content'], escape_keys=False) + '}<end_function_response>' }}\n {%- else -%}\n {{ raise_exception(\"Invalid tool response: 'name' must be provided.\") }}\n {%- endif -%}\n {%- elif message['content'] is sequence -%}\n {%- for item in message['content'] -%}\n {%- if item is mapping -%}\n {%- if 'name' in item and 'response' in item -%}\n {{ '<start_function_response>response:' + item['name'] | trim + '{' }}\n {%- set response_ns = namespace(found_first=false) -%}\n {%- for key, value in item['response'] | dictsort -%}\n {%- if response_ns.found_first %},{% endif -%}\n {%- set response_ns.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {{- '}<end_function_response>' -}}\n {%- elif 'name' in message -%}\n {{ '<start_function_response>response:' + message['name'] | trim + '{' }}\n {%- set response_ns = namespace(found_first=false) -%}\n {%- for key, value in item | dictsort -%}\n {%- if response_ns.found_first %},{% endif -%}\n {%- set response_ns.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {{- '}<end_function_response>' -}}\n {%- else -%}\n {{ raise_exception(\"Invalid tool response mapping: must contain 'name' and 'response' keys, or 'name' must be in the message.\") }}\n {%- endif -%}\n {%- else -%}\n {{ raise_exception(\"Invalid tool response message: multiple responses must all be mappings\") }}\n {%- endif -%}\n {%- endfor -%}\n {%- else -%}\n {{ raise_exception(\"Invalid content type in tool message: must be mapping, sequence of mappings, or string.\") }}\n {%- endif -%}\n {%- endif -%}\n {%- set ns.prev_message_type = 'tool_response' -%}\n {%- endif -%}\n {%- if ns.prev_message_type not in ['tool_call', 'tool_response'] -%}\n {{ '<end_of_turn>\\n' }}\n {%- endif -%}\n{%- endfor -%}\n{%- if add_generation_prompt -%}\n {%- if ns.prev_message_type != 'tool_response' -%}\n {{- '<start_of_turn>model\\n' -}}\n {%- endif -%}\n{%- endif -%}\n",+ "template": "{%- macro format_parameters(properties, required) -%}\n {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in properties | dictsort -%}\n {%- if key not in standard_keys -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {{- key }}:{description:<escape>{{ value['description'] }}<escape>\n {%- if value['type'] | upper == 'STRING' -%}\n {%- if value['enum'] -%}\n ,enum:{{ format_argument(value['enum']) }}\n {%- endif -%}\n {%- elif value['type'] | upper == 'OBJECT' -%}\n ,properties:{\n {%- if value['properties'] is defined and value['properties'] is mapping -%}\n {{- format_parameters(value['properties'], value['required'] | default([])) -}}\n {%- elif value is mapping -%}\n {{- format_parameters(value, value['required'] | default([])) -}}\n {%- endif -%}\n }\n {%- if value['required'] -%}\n ,required:[\n {%- for item in value['required'] | default([]) -%}\n <escape>{{- item -}}<escape>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- endif -%}\n {%- elif value['type'] | upper == 'ARRAY' -%}\n {%- if value['items'] is mapping and value['items'] -%}\n ,items:{\n {%- set ns_items = namespace(found_first=false) -%}\n {%- for item_key, item_value in value['items'] | dictsort -%}\n {%- if item_value is not none -%}\n {%- if ns_items.found_first %},{% endif -%}\n {%- set ns_items.found_first = true -%}\n {%- if item_key == 'properties' -%}\n properties:{\n {%- if item_value is mapping -%}\n {{- format_parameters(item_value, value['items']['required'] | default([])) -}}\n {%- endif -%}\n }\n {%- elif item_key == 'required' -%}\n required:[\n {%- for req_item in item_value -%}\n <escape>{{- req_item -}}<escape>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- elif item_key == 'type' -%}\n {%- if item_value is string -%}\n type:{{ format_argument(item_value | upper) }}\n {%- else -%}\n type:{{ format_argument(item_value | map('upper') | list) }}\n {%- endif -%}\n {%- else -%}\n {{ item_key }}:{{ format_argument(item_value) }}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n }\n {%- endif -%}\n {%- endif -%}\n ,type:<escape>{{ value['type'] | upper }}<escape>}\n {%- endif -%}\n {%- endfor -%}\n{%- endmacro -%}\n{% macro format_function_declaration(tool_data) -%}\ndeclaration:{{- tool_data['function']['name'] -}}\n{description:<escape>{{- tool_data['function']['description'] -}}<escape>\n{%- set params = tool_data['function']['parameters'] -%}\n{%- if params -%}\n ,parameters:{\n {%- if params['properties'] -%}\n properties:{ {{- format_parameters(params['properties'], params['required']) -}} },\n {%- endif -%}\n {%- if params['required'] -%}\n required:[\n {%- for item in params['required'] -%}\n <escape>{{- item -}}<escape>\n {{- ',' if not loop.last -}}\n {%- endfor -%}\n ],\n {%- endif -%}\n {%- if params['type'] -%}\n type:<escape>{{- params['type'] | upper -}}<escape>}\n {%- endif -%}\n{%- endif -%}\n}\n{%- endmacro -%}\n{% macro format_argument(argument, escape_keys=True) -%}\n{%- if argument is string -%}\n {{- '<escape>' + argument + '<escape>' -}}\n{%- elif argument is boolean -%}\n {%- if argument -%}\n {{- 'true' -}}\n {%- else -%}\n {{- 'false' -}}\n {%- endif -%}\n{%- elif argument is mapping -%}\n {{- '{' -}}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in argument | dictsort -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {%- if escape_keys -%}\n {{- '<escape>' + key + '<escape>' -}}\n {%- else -%}\n {{- key -}}\n {%- endif -%}\n :{{- format_argument(value, escape_keys=escape_keys) -}}\n {%- endfor -%}\n {{- '}' -}}\n{%- elif argument is sequence -%}\n {{- '[' -}}\n {%- for item in argument -%}\n {{- format_argument(item, escape_keys=escape_keys) -}}\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n {{- ']' -}}\n{%- else -%}\n {{- argument -}}\n{%- endif -%}\n{%- endmacro -%}\n{{ bos_token }}\n{%- set ns = namespace(prev_message_type=None) -%}\n{#- Tool Declarations -#}\n{%- set loop_messages = messages -%}\n{%- if tools or messages[0]['role'] == 'system' or messages[0]['role'] == 'developer' -%}\n {{- '<start_of_turn>developer\\n' -}}\n {%- if messages[0]['role'] == 'system' or messages[0]['role'] == 'developer' -%}\n {%- if messages[0]['content'] is string -%}\n {{- messages[0]['content'] | trim -}}\n {%- elif messages[0]['content'] is sequence -%}\n {%- for item in messages[0]['content'] -%}\n {%- if item['type'] == 'text' -%}\n {{- item['text'] | trim -}}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- set loop_messages = messages[1:] -%}\n {%- endif -%}\n {%- if tools -%}\n {%- for tool in tools %}\n {{- '<start_function_declaration>' -}}\n {{- format_function_declaration(tool) | trim }}\n {{- '<end_function_declaration>' -}}\n {%- endfor %}\n {%- endif -%}\n {{- '<end_of_turn>\\n' }}\n{%- endif %}\n{#- Loop through messages. -#}\n{%- for message in loop_messages -%}\n {%- if (message['role'] == 'assistant') -%}\n {#- Rename \"assistant\" to \"model\". -#}\n {%- set role = \"model\" -%}\n {%- else -%}\n {%- set role = message['role'] -%}\n {%- endif -%}\n {%- if role != 'tool' -%}\n {#- A user can interrupt after a tool result, before the model continues. Close its open turn. -#}\n {%- if ns.prev_message_type == 'tool_response' and role != 'model' -%}\n {{- '<end_of_turn>\\n' }}\n {%- endif -%}\n {%- if ns.prev_message_type != 'tool_response' or role != 'model' -%}\n {{- '<start_of_turn>' + role + '\\n' }}\n {%- endif -%}\n {%- set ns.prev_message_type = None -%}\n {%- if 'content' in message and message['content'] is not none -%}\n {%- if message['content'] is string -%}\n {{ message['content'] | trim }}\n {%- elif message['content'] is sequence -%}\n {%- for item in message['content'] -%}\n {%- if item['type'] == 'image' -%}\n {{ '<start_of_image>' }}\n {%- elif item['type'] == 'text' -%}\n {{ item['text'] | trim }}\n {%- endif -%}\n {%- endfor -%}\n {%- else -%}\n {{ raise_exception(\"Invalid content type in user/assistant message\") }}\n {%- endif -%}\n {%- set ns.prev_message_type = 'content' -%}\n {%- endif -%}\n {%- if 'tool_calls' in message and message['tool_calls'] and message['tool_calls'] is iterable -%}\n {#- Tool Calls -#}\n {%- for tool_call in message['tool_calls'] -%}\n {% set function = tool_call['function'] if tool_call['function'] is defined else tool_call %}\n {{- '<start_function_call>call:' + function['name'] + '{' -}}\n {%- if 'arguments' in function -%}\n {%- if function['arguments'] is mapping -%}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in function['arguments'] | dictsort -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {%- elif function['arguments'] is string -%}\n {# This handles string-JSON, just in case #}\n {{ function['arguments'] }}\n {%- endif %}\n {%- endif -%}\n {{- '}<end_function_call>' -}}\n {%- endfor -%}\n {%- if loop.last -%}\n {{ '<start_function_response>' }}\n {%- endif -%}\n {%- set ns.prev_message_type = 'tool_call' -%}\n {%- endif -%}\n {%- else -%}\n {#- Tool Responses -#}\n {%- if 'content' in message and message['content'] -%}\n {%- if message['content'] is mapping -%}\n {%- if 'name' in message['content'] and 'response' in message['content'] -%}\n {{ '<start_function_response>response:' + message['content']['name'] | trim + '{' }}\n {%- set response_ns = namespace(found_first=false) -%}\n {%- for key, value in message['content']['response'] | dictsort -%}\n {%- if response_ns.found_first %},{% endif -%}\n {%- set response_ns.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {{- '}<end_function_response>' -}}\n {%- elif 'name' in message -%}\n {{ '<start_function_response>response:' + message['name'] | trim + '{' }}\n {%- set response_ns = namespace(found_first=false) -%}\n {%- for key, value in message['content'] | dictsort -%}\n {%- if response_ns.found_first %},{% endif -%}\n {%- set response_ns.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {{- '}<end_function_response>' -}}\n {%- else -%}\n {{ raise_exception(\"Invalid tool response mapping: must contain 'name' and 'response' keys, or 'name' must be in the message.\") }}\n {%- endif -%}\n {%- elif message['content'] is string -%}\n {%- if 'name' in message -%}\n {{ '<start_function_response>response:' + message['name'] | trim + '{value:' + format_argument(message['content'], escape_keys=False) + '}<end_function_response>' }}\n {%- else -%}\n {{ raise_exception(\"Invalid tool response: 'name' must be provided.\") }}\n {%- endif -%}\n {%- elif message['content'] is sequence -%}\n {%- for item in message['content'] -%}\n {%- if item is mapping -%}\n {%- if 'name' in item and 'response' in item -%}\n {{ '<start_function_response>response:' + item['name'] | trim + '{' }}\n {%- set response_ns = namespace(found_first=false) -%}\n {%- for key, value in item['response'] | dictsort -%}\n {%- if response_ns.found_first %},{% endif -%}\n {%- set response_ns.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {{- '}<end_function_response>' -}}\n {%- elif 'name' in message -%}\n {{ '<start_function_response>response:' + message['name'] | trim + '{' }}\n {%- set response_ns = namespace(found_first=false) -%}\n {%- for key, value in item | dictsort -%}\n {%- if response_ns.found_first %},{% endif -%}\n {%- set response_ns.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {{- '}<end_function_response>' -}}\n {%- else -%}\n {{ raise_exception(\"Invalid tool response mapping: must contain 'name' and 'response' keys, or 'name' must be in the message.\") }}\n {%- endif -%}\n {%- else -%}\n {{ raise_exception(\"Invalid tool response message: multiple responses must all be mappings\") }}\n {%- endif -%}\n {%- endfor -%}\n {%- else -%}\n {{ raise_exception(\"Invalid content type in tool message: must be mapping, sequence of mappings, or string.\") }}\n {%- endif -%}\n {%- endif -%}\n {%- set ns.prev_message_type = 'tool_response' -%}\n {%- endif -%}\n {%- if ns.prev_message_type not in ['tool_call', 'tool_response'] -%}\n {{ '<end_of_turn>\\n' }}\n {%- endif -%}\n{%- endfor -%}\n{%- if add_generation_prompt -%}\n {%- if ns.prev_message_type != 'tool_response' -%}\n {{- '<start_of_turn>model\\n' -}}\n {%- endif -%}\n{%- endif -%}\n","required_functions": [],"preamble_control_key": "add_generation_prompt","bos_token_key": "bos_token"diff --git a/crates/legacy/hanashi/configs/rendering/gpt-oss.json b/crates/legacy/hanashi/configs/rendering/gpt-oss.jsonindex 75df0dce1..86e0bf111 100644--- a/crates/legacy/hanashi/configs/rendering/gpt-oss.json+++ b/crates/legacy/hanashi/configs/rendering/gpt-oss.json@@ -1,4 +1,5 @@{+ "default_reasoning_effort": "medium","jinja": {"template": "{#-\n In addition to the normal inputs of `messages` and `tools`, this template also accepts the\n following kwargs:\n - \"builtin_tools\": A list, can contain \"browser\" and/or \"python\".\n - \"model_identity\": A string that optionally describes the model identity.\n - \"reasoning_effort\": A string that describes the reasoning effort, defaults to \"medium\".\n #}\n\n{#- Tool Definition Rendering ============================================== #}\n{%- macro render_typescript_type(param_spec, required_params, is_nullable=false) -%}\n {%- if param_spec.type == \"array\" -%}\n {%- if param_spec['items'] -%}\n {%- if param_spec['items']['type'] == \"string\" -%}\n {{- \"string[]\" }}\n {%- elif param_spec['items']['type'] == \"number\" -%}\n {{- \"number[]\" }}\n {%- elif param_spec['items']['type'] == \"integer\" -%}\n {{- \"number[]\" }}\n {%- elif param_spec['items']['type'] == \"boolean\" -%}\n {{- \"boolean[]\" }}\n {%- else -%}\n {%- set inner_type = render_typescript_type(param_spec['items'], required_params) -%}\n {%- if inner_type == \"object | object\" or inner_type|length > 50 -%}\n {{- \"any[]\" }}\n {%- else -%}\n {{- inner_type + \"[]\" }}\n {%- endif -%}\n {%- endif -%}\n {%- if param_spec.nullable -%}\n {{- \" | null\" }}\n {%- endif -%}\n {%- else -%}\n {{- \"any[]\" }}\n {%- if param_spec.nullable -%}\n {{- \" | null\" }}\n {%- endif -%}\n {%- endif -%}\n {%- elif param_spec.type is defined and param_spec.type is iterable and param_spec.type is not string and param_spec.type is not mapping and param_spec.type[0] is defined -%}\n {#- Handle array of types like [\"object\", \"object\"] from Union[dict, list] #}\n {%- if param_spec.type | length > 1 -%}\n {{- param_spec.type | join(\" | \") }}\n {%- else -%}\n {{- param_spec.type[0] }}\n {%- endif -%}\n {%- elif param_spec.oneOf -%}\n {#- Handle oneOf schemas - check for complex unions and fallback to any #}\n {%- set has_object_variants = false -%}\n {%- for variant in param_spec.oneOf -%}\n {%- if variant.type == \"object\" -%}\n {%- set has_object_variants = true -%}\n {%- endif -%}\n {%- endfor -%}\n {%- if has_object_variants and param_spec.oneOf|length > 1 -%}\n {{- \"any\" }}\n {%- else -%}\n {%- for variant in param_spec.oneOf -%}\n {{- render_typescript_type(variant, required_params) -}}\n {%- if variant.description %}\n {{- \"// \" + variant.description }}\n {%- endif -%}\n {%- if variant.default is defined %}\n {{ \"// default: \" + variant.default|tojson }}\n {%- endif -%}\n {%- if not loop.last %}\n {{- \" | \" }}\n {% endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- elif param_spec.type == \"string\" -%}\n {%- if param_spec.enum -%}\n {{- '\"' + param_spec.enum|join('\" | \"') + '\"' -}}\n {%- else -%}\n {{- \"string\" }}\n {%- if param_spec.nullable %}\n {{- \" | null\" }}\n {%- endif -%}\n {%- endif -%}\n {%- elif param_spec.type == \"number\" -%}\n {{- \"number\" }}\n {%- elif param_spec.type == \"integer\" -%}\n {{- \"number\" }}\n {%- elif param_spec.type == \"boolean\" -%}\n {{- \"boolean\" }}\n\n {%- elif param_spec.type == \"object\" -%}\n {%- if param_spec.properties -%}\n {{- \"{\\n\" }}\n {%- for prop_name, prop_spec in param_spec.properties.items() -%}\n {{- prop_name -}}\n {%- if prop_name not in (param_spec.required or []) -%}\n {{- \"?\" }}\n {%- endif -%}\n {{- \": \" }}\n {{ render_typescript_type(prop_spec, param_spec.required or []) }}\n {%- if not loop.last -%}\n {{-\", \" }}\n {%- endif -%}\n {%- endfor -%}\n {{- \"}\" }}\n {%- else -%}\n {{- \"object\" }}\n {%- endif -%}\n {%- else -%}\n {{- \"any\" }}\n {%- endif -%}\n{%- endmacro -%}\n\n{%- macro render_tool_namespace(namespace_name, tools) -%}\n {{- \"## \" + namespace_name + \"\\n\\n\" }}\n {{- \"namespace \" + namespace_name + \" {\\n\\n\" }}\n {%- for tool in tools %}\n {%- set tool = tool.function %}\n {{- \"// \" + tool.description + \"\\n\" }}\n {{- \"type \"+ tool.name + \" = \" }}\n {%- if tool.parameters and tool.parameters.properties %}\n {{- \"(_: {\\n\" }}\n {%- for param_name, param_spec in tool.parameters.properties.items() %}\n {%- if param_spec.description %}\n {{- \"// \" + param_spec.description + \"\\n\" }}\n {%- endif %}\n {{- param_name }}\n {%- if param_name not in (tool.parameters.required or []) -%}\n {{- \"?\" }}\n {%- endif -%}\n {{- \": \" }}\n {{- render_typescript_type(param_spec, tool.parameters.required or []) }}\n {%- if param_spec.default is defined -%}\n {%- if param_spec.enum %}\n {{- \", // default: \" + param_spec.default }}\n {%- elif param_spec.oneOf %}\n {{- \"// default: \" + param_spec.default }}\n {%- else %}\n {{- \", // default: \" + param_spec.default|tojson }}\n {%- endif -%}\n {%- endif -%}\n {%- if not loop.last %}\n {{- \",\\n\" }}\n {%- else %}\n {{- \",\\n\" }}\n {%- endif -%}\n {%- endfor %}\n {{- \"}) => any;\\n\\n\" }}\n {%- else -%}\n {{- \"() => any;\\n\\n\" }}\n {%- endif -%}\n {%- endfor %}\n {{- \"} // namespace \" + namespace_name }}\n{%- endmacro -%}\n\n{%- macro render_builtin_tools(browser_tool, python_tool) -%}\n {%- if browser_tool %}\n {{- \"## browser\\n\\n\" }}\n {{- \"// Tool for browsing.\\n\" }}\n {{- \"// The `cursor` appears in brackets before each browsing display: `[{cursor}]`.\\n\" }}\n {{- \"// Cite information from the tool using the following format:\\n\" }}\n {{- \"// `【{cursor}†L{line_start}(-L{line_end})?】`, for example: `【6†L9-L11】` or `【8†L3】`.\\n\" }}\n {{- \"// Do not quote more than 10 words directly from the tool output.\\n\" }}\n {{- \"// sources=web (default: web)\\n\" }}\n {{- \"namespace browser {\\n\\n\" }}\n {{- \"// Searches for information related to `query` and displays `topn` results.\\n\" }}\n {{- \"type search = (_: {\\n\" }}\n {{- \"query: string,\\n\" }}\n {{- \"topn?: number, // default: 10\\n\" }}\n {{- \"source?: string,\\n\" }}\n {{- \"}) => any;\\n\\n\" }}\n {{- \"// Opens the link `id` from the page indicated by `cursor` starting at line number `loc`, showing `num_lines` lines.\\n\" }}\n {{- \"// Valid link ids are displayed with the formatting: `【{id}†.*】`.\\n\" }}\n {{- \"// If `cursor` is not provided, the most recent page is implied.\\n\" }}\n {{- \"// If `id` is a string, it is treated as a fully qualified URL associated with `source`.\\n\" }}\n {{- \"// If `loc` is not provided, the viewport will be positioned at the beginning of the document or centered on the most relevant passage, if available.\\n\" }}\n {{- \"// Use this function without `id` to scroll to a new location of an opened page.\\n\" }}\n {{- \"type open = (_: {\\n\" }}\n {{- \"id?: number | string, // default: -1\\n\" }}\n {{- \"cursor?: number, // default: -1\\n\" }}\n {{- \"loc?: number, // default: -1\\n\" }}\n {{- \"num_lines?: number, // default: -1\\n\" }}\n {{- \"view_source?: boolean, // default: false\\n\" }}\n {{- \"source?: string,\\n\" }}\n {{- \"}) => any;\\n\\n\" }}\n {{- \"// Finds exact matches of `pattern` in the current page, or the page given by `cursor`.\\n\" }}\n {{- \"type find = (_: {\\n\" }}\n {{- \"pattern: string,\\n\" }}\n {{- \"cursor?: number, // default: -1\\n\" }}\n {{- \"}) => any;\\n\\n\" }}\n {{- \"} // namespace browser\\n\\n\" }}\n {%- endif -%}\n\n {%- if python_tool %}\n {{- \"## python\\n\\n\" }}\n {{- \"Use this tool to execute Python code in your chain of thought. The code will not be shown to the user. This tool should be used for internal reasoning, but not for code that is intended to be visible to the user (e.g. when creating plots, tables, or files).\\n\\n\" }}\n {{- \"When you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 120.0 seconds. The drive at '/mnt/data' can be used to save and persist user files. Internet access for this session is UNKNOWN. Depends on the cluster.\\n\\n\" }}\n {%- endif -%}\n{%- endmacro -%}\n\n{#- System Message Construction ============================================ #}\n{%- macro build_system_message() -%}\n {%- if model_identity is not defined %}\n {%- set model_identity = \"You are ChatGPT, a large language model trained by OpenAI.\" %}\n {%- endif %}\n {{- model_identity + \"\\n\" }}\n {{- \"Knowledge cutoff: 2024-06\\n\" }}\n {{- \"Current date: \" + strftime_now(\"%Y-%m-%d\") + \"\\n\\n\" }}\n {%- if reasoning_effort is not defined %}\n {%- set reasoning_effort = \"medium\" %}\n {%- endif %}\n {{- \"Reasoning: \" + reasoning_effort + \"\\n\\n\" }}\n {%- if builtin_tools %}\n {{- \"# Tools\\n\\n\" }}\n {%- set available_builtin_tools = namespace(browser=false, python=false) %}\n {%- for tool in builtin_tools %}\n {%- if tool == \"browser\" %}\n {%- set available_builtin_tools.browser = true %}\n {%- elif tool == \"python\" %}\n {%- set available_builtin_tools.python = true %}\n {%- endif %}\n {%- endfor %}\n {{- render_builtin_tools(available_builtin_tools.browser, available_builtin_tools.python) }}\n {%- endif -%}\n {{- \"# Valid channels: analysis, commentary, final. Channel must be included for every message.\" }}\n {%- if tools -%}\n {{- \"\\nCalls to these tools must go to the commentary channel: 'functions'.\" }}\n {%- endif -%}\n{%- endmacro -%}\n\n{#- Main Template Logic ================================================= #}\n{#- Set defaults #}\n\n{#- Render system message #}\n{{- \"<|start|>system<|message|>\" }}\n{{- build_system_message() }}\n{{- \"<|end|>\" }}\n\n{#- Extract developer message #}\n{%- if messages[0].role == \"developer\" or messages[0].role == \"system\" %}\n {%- set developer_message = messages[0].content %}\n {%- set loop_messages = messages[1:] %}\n{%- else %}\n {%- set developer_message = \"\" %}\n {%- set loop_messages = messages %}\n{%- endif %}\n\n{#- Render developer message #}\n{%- if developer_message or tools %}\n {{- \"<|start|>developer<|message|>\" }}\n {%- if developer_message %}\n {{- \"# Instructions\\n\\n\" }}\n {{- developer_message }}\n {{- \"\\n\\n\" }}\n {%- endif %}\n {%- if tools -%}\n {{- \"# Tools\\n\\n\" }}\n {{- render_tool_namespace(\"functions\", tools) }}\n {%- endif -%}\n {{- \"<|end|>\" }}\n{%- endif %}\n\n{#- Render messages #}\n{%- set last_tool_call = namespace(name=none) %}\n{%- for message in loop_messages -%}\n {#- At this point only assistant/user/tool messages should remain #}\n {%- if message.role == 'assistant' -%}\n {#- Checks to ensure the messages are being passed in the format we expect #}\n {%- if \"content\" in message %}\n {%- if \"<|channel|>analysis<|message|>\" in message.content or \"<|channel|>final<|message|>\" in message.content %}\n {{- raise_exception(\"You have passed a message containing <|channel|> tags in the content field. Instead of doing this, you should pass analysis messages (the string between '<|message|>' and '<|end|>') in the 'thinking' field, and final messages (the string between '<|message|>' and '<|end|>') in the 'content' field.\") }}\n {%- endif %}\n {%- endif %}\n {%- if \"thinking\" in message %}\n {%- if \"<|channel|>analysis<|message|>\" in message.thinking or \"<|channel|>final<|message|>\" in message.thinking %}\n {{- raise_exception(\"You have passed a message containing <|channel|> tags in the thinking field. Instead of doing this, you should pass analysis messages (the string between '<|message|>' and '<|end|>') in the 'thinking' field, and final messages (the string between '<|message|>' and '<|end|>') in the 'content' field.\") }}\n {%- endif %}\n {%- endif %}\n {%- if \"tool_calls\" in message %}\n {#- We need very careful handling here - we want to drop the tool call analysis message if the model #}\n {#- has output a later <|final|> message, but otherwise we want to retain it. This is the only case #}\n {#- when we render CoT/analysis messages in inference. #}\n {%- set future_final_message = namespace(found=false) %}\n {%- for future_message in loop_messages[loop.index:] %}\n {%- if future_message.role == 'assistant' and \"tool_calls\" not in future_message %}\n {%- set future_final_message.found = true %}\n {%- endif %}\n {%- endfor %}\n {#- We assume max 1 tool call per message, and so we infer the tool call name #}\n {#- in \"tool\" messages from the most recent assistant tool call name #}\n {%- set tool_call = message.tool_calls[0] %}\n {%- if tool_call.function %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {%- if message.content and message.thinking %}\n {{- raise_exception(\"Cannot pass both content and thinking in an assistant message with tool calls! Put the analysis message in one or the other, but not both.\") }}\n {%- elif message.content and not future_final_message.found %}\n {{- \"<|start|>assistant<|channel|>analysis<|message|>\" + message.content + \"<|end|>\" }}\n {%- elif message.thinking and not future_final_message.found %}\n {{- \"<|start|>assistant<|channel|>analysis<|message|>\" + message.thinking + \"<|end|>\" }}\n {%- endif %}\n {{- \"<|start|>assistant to=\" }}\n {{- \"functions.\" + tool_call.name + \"<|channel|>commentary \" }}\n {{- (tool_call.content_type if tool_call.content_type is defined else \"json\") + \"<|message|>\" }}\n {{- tool_call.arguments|tojson }}\n {{- \"<|call|>\" }}\n {%- set last_tool_call.name = tool_call.name %}\n {%- elif loop.last and not add_generation_prompt %}\n {#- Only render the CoT if the final turn is an assistant turn and add_generation_prompt is false #}\n {#- This is a situation that should only occur in training, never in inference. #}\n {%- if \"thinking\" in message %}\n {{- \"<|start|>assistant<|channel|>analysis<|message|>\" + message.thinking + \"<|end|>\" }}\n {%- endif %}\n {#- <|return|> indicates the end of generation, but <|end|> does not #}\n {#- <|return|> should never be an input to the model, but we include it as the final token #}\n {#- when training, so the model learns to emit it. #}\n {{- \"<|start|>assistant<|channel|>final<|message|>\" + message.content + \"<|return|>\" }}\n {%- else %}\n {#- CoT is dropped during all previous turns, so we never render it for inference #}\n {{- \"<|start|>assistant<|channel|>final<|message|>\" + message.content + \"<|end|>\" }}\n {%- set last_tool_call.name = none %}\n {%- endif %}\n {%- elif message.role == 'tool' -%}\n {%- if last_tool_call.name is none %}\n {{- raise_exception(\"Message has tool role, but there was no previous assistant message with a tool call!\") }}\n {%- endif %}\n {{- \"<|start|>functions.\" + last_tool_call.name }}\n {{- \" to=assistant<|channel|>commentary<|message|>\" + message.content|tojson + \"<|end|>\" }}\n {%- elif message.role == 'user' -%}\n {{- \"<|start|>user<|message|>\" + message.content + \"<|end|>\" }}\n {%- endif -%}\n{%- endfor -%}\n\n{#- Generation prompt #}\n{%- if add_generation_prompt -%}\n<|start|>assistant\n{%- endif -%}","required_functions": [@@ -104,4 +105,4 @@}}}-}\ No newline at end of file+}diff --git a/crates/legacy/hanashi/configs/rendering/muse-glimmer.json b/crates/legacy/hanashi/configs/rendering/muse-glimmer.jsonindex bdc0c1553..4d02410b2 100644--- a/crates/legacy/hanashi/configs/rendering/muse-glimmer.json+++ b/crates/legacy/hanashi/configs/rendering/muse-glimmer.json@@ -37,11 +37,11 @@"reasoning_strength": {"block": "reasoning_effort","mapping": {- "disabled": null,- "default": null,+ "default": "high","low": "low","medium": "medium",- "high": "high"+ "high": "high",+ "xhigh": "xhigh"}}}diff --git a/crates/legacy/hanashi/configs/rendering/qwen3-instruct.json b/crates/legacy/hanashi/configs/rendering/qwen3-instruct.jsonindex faf50df85..2d73545c3 100644--- a/crates/legacy/hanashi/configs/rendering/qwen3-instruct.json+++ b/crates/legacy/hanashi/configs/rendering/qwen3-instruct.json@@ -66,6 +66,7 @@"blocks": ["tool_call_result"],+ "as_text": true,"limit": 1}}diff --git a/crates/legacy/hanashi/configs/rendering/qwen3-thinking.json b/crates/legacy/hanashi/configs/rendering/qwen3-thinking.jsonindex 23af5a87b..dff3b6bd2 100644--- a/crates/legacy/hanashi/configs/rendering/qwen3-thinking.json+++ b/crates/legacy/hanashi/configs/rendering/qwen3-thinking.json@@ -69,6 +69,7 @@"blocks": ["tool_call_result"],+ "as_text": true,"limit": 1}}diff --git a/crates/legacy/hanashi/configs/rendering/qwen3.5.json b/crates/legacy/hanashi/configs/rendering/qwen3.5.jsonindex ad3ed5fec..2ee55d9d2 100644--- a/crates/legacy/hanashi/configs/rendering/qwen3.5.json+++ b/crates/legacy/hanashi/configs/rendering/qwen3.5.json@@ -77,6 +77,7 @@"blocks": ["tool_call_result"],+ "as_text": true,"limit": 1}}diff --git a/crates/legacy/hanashi/configs/rendering/qwen3.6.json b/crates/legacy/hanashi/configs/rendering/qwen3.6.jsonindex 677465e54..4cbfd4b86 100644--- a/crates/legacy/hanashi/configs/rendering/qwen3.6.json+++ b/crates/legacy/hanashi/configs/rendering/qwen3.6.json@@ -77,6 +77,7 @@"blocks": ["tool_call_result"],+ "as_text": true,"limit": 1}}diff --git a/crates/legacy/hanashi/configs/rendering/qwen3.8.json b/crates/legacy/hanashi/configs/rendering/qwen3.8.jsonindex 4476e53d3..456fe80d9 100644--- a/crates/legacy/hanashi/configs/rendering/qwen3.8.json+++ b/crates/legacy/hanashi/configs/rendering/qwen3.8.json@@ -90,6 +90,7 @@"blocks": ["tool_call_result"],+ "as_text": true,"limit": 1}}diff --git a/crates/legacy/hanashi/configs/rendering/qwen3.json b/crates/legacy/hanashi/configs/rendering/qwen3.jsonindex 7d097bbf3..a03f39d93 100644--- a/crates/legacy/hanashi/configs/rendering/qwen3.json+++ b/crates/legacy/hanashi/configs/rendering/qwen3.json@@ -77,6 +77,7 @@"blocks": ["tool_call_result"],+ "as_text": true,"limit": 1}}diff --git a/crates/legacy/hanashi/src/chat/config.rs b/crates/legacy/hanashi/src/chat/config.rsindex 441f8a302..ee9a74197 100644--- a/crates/legacy/hanashi/src/chat/config.rs+++ b/crates/legacy/hanashi/src/chat/config.rs@@ -1,5 +1,8 @@use serde::{Deserialize, Serialize};-use shoji::types::{basic::Value, session::chat::ChatModelCapabilities};+use shoji::types::{+ basic::{ReasoningEffort, Value},+ session::chat::ChatModelCapabilities,+};use crate::chat::{Error, hanashi::config::HanashiConfig, harmony::HarmonyConfig};@@ -17,6 +20,17 @@ pub enum EncodingConfig {}impl EncodingConfig {+ pub fn default_reasoning_effort(&self) -> Result<Option<ReasoningEffort>, Error> {+ match self {+ EncodingConfig::Hanashi {+ config,+ } => config.default_reasoning_effort().map_err(Error::from),+ EncodingConfig::Harmony {+ config,+ } => Ok(config.default_reasoning_effort()),+ }+ }+pub fn select(encodings: &[Value]) -> Option<Value> {let parse = |value: &Value| serde_json::from_str::<EncodingConfig>(&value.json).ok();encodingsdiff --git a/crates/legacy/hanashi/src/chat/hanashi/config/mod.rs b/crates/legacy/hanashi/src/chat/hanashi/config/mod.rsindex b04a542bf..ad4cdc1ec 100644--- a/crates/legacy/hanashi/src/chat/hanashi/config/mod.rs+++ b/crates/legacy/hanashi/src/chat/hanashi/config/mod.rs@@ -63,6 +63,10 @@ pub enum HanashiConfig {}impl HanashiConfig {+ pub fn default_reasoning_effort(&self) -> Result<Option<ReasoningEffort>, Error> {+ Ok(self.resolve()?.rendering.default_reasoning_effort())+ }+pub fn capabilities(&self) -> Result<ChatModelCapabilities, Error> {let resolved = self.resolve()?;let rendering = &resolved.rendering;diff --git a/crates/legacy/hanashi/src/chat/hanashi/messages/rendered/config.rs b/crates/legacy/hanashi/src/chat/hanashi/messages/rendered/config.rsindex 727fbd55e..7b1c0bf8b 100644--- a/crates/legacy/hanashi/src/chat/hanashi/messages/rendered/config.rs+++ b/crates/legacy/hanashi/src/chat/hanashi/messages/rendered/config.rs@@ -9,6 +9,9 @@ pub struct Field {pub required: bool,#[serde(default)]pub disable_raw: bool,+ /// Serialize non-string values as JSON text for templates expecting a text field.+ #[serde(default)]+ pub as_text: bool,#[serde(flatten)]pub config: FieldConfig,}diff --git a/crates/legacy/hanashi/src/chat/hanashi/messages/rendered/message.rs b/crates/legacy/hanashi/src/chat/hanashi/messages/rendered/message.rsindex da95766d0..ffc234cc1 100644--- a/crates/legacy/hanashi/src/chat/hanashi/messages/rendered/message.rs+++ b/crates/legacy/hanashi/src/chat/hanashi/messages/rendered/message.rs@@ -93,6 +93,19 @@ impl Message {}}+ // Adapt only the final template field; canonical values and chat history+ // stay structured. Only templates requiring text opt in.+ for (fields, destination) in destinations {+ for (field_name, field) in *fields {+ if field.as_text+ && let Some(value) = destination.get_mut(field_name)+ && !value.is_string()+ {+ *value = Value::String(value.to_string());+ }+ }+ }+// Check required fieldsfor (field_name, field) in config.message.iter().chain(config.context.iter()) {if field.required && !seen_fields.contains_key(field_name) {diff --git a/crates/legacy/hanashi/src/chat/hanashi/renderer/config/mod.rs b/crates/legacy/hanashi/src/chat/hanashi/renderer/config/mod.rsindex e31cc6e91..194fde38a 100644--- a/crates/legacy/hanashi/src/chat/hanashi/renderer/config/mod.rs+++ b/crates/legacy/hanashi/src/chat/hanashi/renderer/config/mod.rs@@ -5,7 +5,10 @@ use indexmap::IndexMap;pub use jinja::JinjaConfig;pub use jinja_function::JinjaFunction;use serde::{Deserialize, Serialize};-use shoji::types::session::chat::{ChatContentBlockType, ChatRole};+use shoji::types::{+ basic::ReasoningEffort,+ session::chat::{ChatContentBlockType, ChatRole},+};use crate::chat::hanashi::messages::{canonical::Config as CanonicalConfig,@@ -17,9 +20,67 @@ pub struct RendererConfig {pub jinja: JinjaConfig,pub canonization: CanonicalConfig,pub rendering: IndexMap<ChatRole, RenderedConfig>,+ // Needed when the default lives inside the template rather than its field mapping.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub default_reasoning_effort: Option<ReasoningEffort>,}impl RendererConfig {+ pub fn default_reasoning_effort(&self) -> Option<ReasoningEffort> {+ if let Some(effort) = self.default_reasoning_effort {+ return Some(effort);+ }++ let mappings: Option<Vec<_>> = self+ .rendering+ .values()+ .flat_map(|role| role.message.values().chain(role.context.values()))+ .filter_map(|field| match &field.config {+ FieldConfig::Unique {+ block: ChatContentBlockType::ReasoningEffort,+ mapping,+ ..+ } => Some(mapping.as_ref()),+ _ => None,+ })+ .collect();+ let mappings = mappings.filter(|mappings| !mappings.is_empty())?;++ // Null omits a template variable; matching two null mappings does not+ // identify the template's fallback as either of those named modes.+ if !mappings.iter().any(|mapping| matches!(mapping.get("default"), Some(Some(_)))) {+ return None;+ }++ // A control may span several fields (Qwen3.8 has both enable_thinking+ // and reasoning_effort). Only an exact match across all of them proves+ // that an explicit effort has the same meaning as the default.+ let mut matches = [+ ReasoningEffort::Disabled,+ ReasoningEffort::Low,+ ReasoningEffort::Medium,+ ReasoningEffort::High,+ ReasoningEffort::XHigh,+ ]+ .into_iter()+ .filter(|effort| {+ mappings.iter().all(|mapping| {+ mapping.get(&effort.to_string()).is_some_and(|value| Some(value) == mapping.get("default"))+ })+ });+ if let Some(effort) = matches.next() {+ return matches.next().is_none().then_some(effort);+ }++ // Toggle-only templates name their enabled mode "default". An omitted+ // or null default still depends on the template and cannot prove this.+ (mappings+ .iter()+ .all(|mapping| matches!(mapping.get("default"), Some(Some(_))) && mapping.contains_key("disabled"))+ && mappings.iter().any(|mapping| mapping.get("default") != mapping.get("disabled")))+ .then_some(ReasoningEffort::Default)+ }+pub fn get_role_by_name(&self,name: &str,@@ -63,3 +124,72 @@ impl RendererConfig {self.get_rendering_role_and_field_for_block_type(block_type).map(|(_, field)| field)}}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::chat::hanashi::config::HanashiConfig;++ #[test]+ fn omitted_default_and_disabled_do_not_prove_the_template_default_is_off() {+ let mut config = HanashiConfig::Qwen35.resolve().unwrap().rendering;+ let field = config.rendering.get_mut(&ChatRole::System {}).unwrap().context.get_mut("enable_thinking").unwrap();+ let FieldConfig::Unique {+ mapping: Some(mapping),+ ..+ } = &mut field.config+ else {+ panic!("expected a reasoning mapping");+ };+ mapping.insert("default".to_string(), None);+ mapping.insert("disabled".to_string(), None);+ assert_eq!(config.default_reasoning_effort(), None);+ }++ #[test]+ fn default_effort_compares_every_control_field() {+ let config = HanashiConfig::Qwen38.resolve().unwrap().rendering;+ assert_eq!(config.default_reasoning_effort(), Some(ReasoningEffort::XHigh));++ let mut ambiguous = config;+ ambiguous.rendering.get_mut(&ChatRole::System {}).unwrap().context.shift_remove("reasoning_effort");+ // The remaining boolean field matches low, medium, and xhigh equally.+ assert_eq!(ambiguous.default_reasoning_effort(), None);+ }++ #[test]+ fn muse_glimmer_default_is_high() {+ let config = HanashiConfig::MuseGlimmer.resolve().unwrap().rendering;+ assert_eq!(config.default_reasoning_effort(), Some(ReasoningEffort::High));+ }++ #[test]+ fn toggle_default_requires_a_distinct_declared_value() {+ let mut config = HanashiConfig::Qwen35.resolve().unwrap().rendering;+ assert_eq!(config.default_reasoning_effort(), Some(ReasoningEffort::Default));+ let set_default = |config: &mut RendererConfig, value| {+ let field =+ config.rendering.get_mut(&ChatRole::System {}).unwrap().context.get_mut("enable_thinking").unwrap();+ let FieldConfig::Unique {+ mapping: Some(mapping),+ ..+ } = &mut field.config+ else {+ panic!("expected mapped control")+ };+ mapping.insert("default".to_string(), value);+ };+ set_default(&mut config, None);+ assert_eq!(config.default_reasoning_effort(), None);+ set_default(&mut config, Some(false.into()));+ assert_eq!(config.default_reasoning_effort(), Some(ReasoningEffort::Disabled));+ }++ #[test]+ fn template_fallback_requires_declared_default_metadata() {+ let mut config = HanashiConfig::GptOss.resolve().unwrap().rendering;+ assert_eq!(config.default_reasoning_effort(), Some(ReasoningEffort::Medium));+ config.default_reasoning_effort = None;+ assert_eq!(config.default_reasoning_effort(), None);+ }+}diff --git a/crates/legacy/hanashi/src/chat/hanashi/renderer/mod.rs b/crates/legacy/hanashi/src/chat/hanashi/renderer/mod.rsindex 6d2566699..0b3dbada9 100644--- a/crates/legacy/hanashi/src/chat/hanashi/renderer/mod.rs+++ b/crates/legacy/hanashi/src/chat/hanashi/renderer/mod.rs@@ -177,6 +177,74 @@ mod tests {ChatMessage::system().with_reasoning_effort(effort)}+ fn tool_result_messages(value: serde_json::Value) -> Vec<ChatMessage> {+ vec![+ user_message(),+ ChatMessage::assistant().with_tool_call(ToolCall {+ identifier: Some("call_1".to_string()),+ name: "echo".to_string(),+ arguments: Value::from(serde_json::json!({})),+ }),+ ChatMessage::tool().with_block(ChatContentBlock::ToolCallResult {+ identifier: Some("call_1".to_string()),+ name: Some("echo".to_string()),+ value: Value::from(value),+ }),+ ]+ }++ #[test]+ fn qwen_tool_result_fields_render_json_as_text() {+ for config in [+ HanashiConfig::Qwen3,+ HanashiConfig::Qwen3Instruct,+ HanashiConfig::Qwen3Thinking,+ HanashiConfig::Qwen35,+ HanashiConfig::Qwen36,+ HanashiConfig::Qwen38,+ ] {+ let renderer = renderer(config);+ for result in [+ serde_json::json!({"datasets": [{"label": "Revenue", "data": [12.5, 15]}]}),+ serde_json::json!([1, "two", null]),+ serde_json::json!(42),+ serde_json::json!(true),+ serde_json::json!(null),+ serde_json::json!("ready"),+ ] {+ let prompt = render(&renderer, tool_result_messages(result.clone()));+ let response =+ prompt.split_once("<tool_response>\n").unwrap().1.split_once("\n</tool_response>").unwrap().0;+ let expected = result.as_str().map(str::to_owned).unwrap_or_else(|| result.to_string());+ assert_eq!(response, expected);+ }+ }+ }++ #[test]+ fn gpt_oss_template_serializes_tool_results_once() {+ // Its template calls tojson itself, so the adapter must keep JSON values.+ let renderer = renderer(HanashiConfig::GptOss);+ for result in [+ serde_json::json!({"datasets": [{"label": "Revenue", "data": [12.5, 15]}]}),+ serde_json::json!([1, "two", null]),+ serde_json::json!(42),+ serde_json::json!(true),+ serde_json::json!(null),+ serde_json::json!("ready"),+ ] {+ let prompt = render(&renderer, tool_result_messages(result.clone()));+ let response = prompt+ .split_once("<|start|>functions.echo to=assistant<|channel|>commentary<|message|>")+ .unwrap()+ .1+ .split_once("<|end|>")+ .unwrap()+ .0;+ assert_eq!(serde_json::from_str::<serde_json::Value>(response).unwrap(), result);+ }+ }+#[test]fn absent_reasoning_effort_renders_like_default() {let renderer = renderer(HanashiConfig::Qwen35);@@ -215,6 +283,47 @@ mod tests {render(&renderer, vec![system_message_with_effort(ReasoningEffort::Default), user_message()]);assert_eq!(without_block, with_default_block);+ let declared_effort = HanashiConfig::GptOss.default_reasoning_effort().unwrap().unwrap();+ assert_eq!(declared_effort, ReasoningEffort::Medium);+ let with_declared_effort = render(&renderer, vec![system_message_with_effort(declared_effort), user_message()]);+ assert_eq!(without_block, with_declared_effort);+ }++ #[test]+ fn muse_glimmer_levels_and_default_match_rendered_instructions() {+ let capabilities = HanashiConfig::MuseGlimmer.capabilities().unwrap();+ assert!(!capabilities.supports_disable_reasoning);+ assert!(!capabilities.reasoning_efforts.contains(&ReasoningEffort::Disabled));++ let renderer = renderer(HanashiConfig::MuseGlimmer);+ let render_effort = |effort: Option<ReasoningEffort>| {+ let mut messages = Vec::new();+ if let Some(effort) = effort {+ messages.push(system_message_with_effort(effort));+ }+ messages.push(user_message());+ renderer.render(+ &messages,+ true,+ Some(Token {+ id: 0,+ value: "<|begin_of_text|>".to_string(),+ is_special: true,+ }),+ None,+ None,+ )+ };+ let high = render_effort(Some(ReasoningEffort::High)).unwrap();+ assert_eq!(render_effort(None).unwrap(), high);+ assert_eq!(render_effort(Some(ReasoningEffort::Default)).unwrap(), high);++ for effort in [ReasoningEffort::Low, ReasoningEffort::Medium, ReasoningEffort::High, ReasoningEffort::XHigh] {+ assert!(capabilities.reasoning_efforts.contains(&effort));+ assert!(render_effort(Some(effort)).unwrap().contains(&format!("Reasoning strength: {effort}.")));+ }+ assert!(matches!(render_effort(Some(ReasoningEffort::Disabled)),+ Err(Error::Message(crate::chat::hanashi::messages::Error::UnmappedValue { value, .. })) if value == "disabled"));}#[test]diff --git a/crates/legacy/hanashi/src/chat/harmony/bridging/message.rs b/crates/legacy/hanashi/src/chat/harmony/bridging/message.rsindex e2d1b6952..48b85d7ea 100644--- a/crates/legacy/hanashi/src/chat/harmony/bridging/message.rs+++ b/crates/legacy/hanashi/src/chat/harmony/bridging/message.rs@@ -22,12 +22,21 @@ const RECIPIENT_ASSISTANT: &str = ROLE_ASSISTANT;const BUILTIN_BROWSER: &str = "browser";const BUILTIN_PYTHON: &str = "python";-pub fn bridge_messages_to_harmony(messages: &[ChatMessage]) -> Result<Vec<ExternalMessage>, Error> {+pub fn bridge_messages_to_harmony(+ messages: &[ChatMessage],+ is_initial: bool,+) -> Result<Vec<ExternalMessage>, Error> {let mut result = Vec::new();// Plain system text becomes developer `# Instructions`, matching the reference gpt-oss chat template.// The harmony system message always carries the meta preamble (identity, reasoning effort, valid channels) the model was trained to expect.let mut pending_instructions: Vec<String> = Vec::new();+ // Unset reasoning still uses the native system defaults. Only add this at+ // the beginning: incremental encodes must not repeat the system preamble.+ if is_initial && !messages.iter().any(|message| message.role == ChatRole::System {}) {+ result.push(system_message(ExternalSystemContent::default()));+ }+for message in messages {if !pending_instructions.is_empty() && !matches!(message.role, ChatRole::System {} | ChatRole::Developer {}) {result.push(developer_message(std::mem::take(&mut pending_instructions).join("")));@@ -92,18 +101,7 @@ pub fn bridge_messages_to_harmony(messages: &[ChatMessage]) -> Result<Vec<Extern}}- // the reference gpt-oss chat template always includes the current date, so default it here the same way it does (strftime_now("%Y-%m-%d"))- if system_content.conversation_start_date.is_none() {- system_content.conversation_start_date = Some(strftime_now("%Y-%m-%d".to_string()));- }-- result.push(ExternalMessage {- author: ExternalAuthor::from(ExternalRole::System),- recipient: None,- content: vec![ExternalContent::SystemContent(system_content)],- channel: None,- content_type: None,- });+ result.push(system_message(system_content));},ChatRole::Developer {} => {let mut developer_content = ExternalDeveloperContent::default();@@ -299,6 +297,20 @@ pub fn bridge_messages_to_harmony(messages: &[ChatMessage]) -> Result<Vec<ExternOk(result)}+fn system_message(mut content: ExternalSystemContent) -> ExternalMessage {+ // Match the reference template's current-date fallback.+ if content.conversation_start_date.is_none() {+ content.conversation_start_date = Some(strftime_now("%Y-%m-%d".to_string()));+ }+ ExternalMessage {+ author: ExternalAuthor::from(ExternalRole::System),+ recipient: None,+ content: vec![ExternalContent::SystemContent(content)],+ channel: None,+ content_type: None,+ }+}+fn developer_message(instructions: String) -> ExternalMessage {let developer_content = ExternalDeveloperContent {instructions: Some(instructions),@@ -541,3 +553,77 @@ fn extract_text_content(content: &[ExternalContent]) -> String {fn strip_namespace_prefix(qualified_name: &str) -> &str {qualified_name.strip_prefix(FUNCTIONS_NAMESPACE).and_then(|rest| rest.strip_prefix('.')).unwrap_or(qualified_name)}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn absent_system_instructions_use_the_same_native_defaults_as_medium() {+ let user = ChatMessage::user().with_text("Hello".to_string());+ let implicit = bridge_messages_to_harmony(std::slice::from_ref(&user), true).unwrap();+ let explicit = bridge_messages_to_harmony(+ &[ChatMessage::system().with_reasoning_effort(ReasoningEffort::Medium), user],+ true,+ )+ .unwrap();++ assert_eq!(implicit, explicit);+ assert_eq!(implicit[0].author.role, ExternalRole::System);+ let ExternalContent::SystemContent(content) = &implicit[0].content[0] else {+ panic!("expected a native system preamble");+ };+ assert_eq!(content.reasoning_effort, Some(openai_harmony::chat::ReasoningEffort::Medium));+ assert!(content.model_identity.is_some());+ assert!(content.conversation_start_date.is_some());+ assert!(content.channel_config.is_some());+ }++ #[test]+ fn explicit_reasoning_and_instructions_are_preserved_without_a_second_system_message() {+ let messages = bridge_messages_to_harmony(+ &[+ ChatMessage::system()+ .with_reasoning_effort(ReasoningEffort::Low)+ .with_text("Keep answers brief".to_string()),+ ChatMessage::user().with_text("Hello".to_string()),+ ],+ true,+ )+ .unwrap();++ assert_eq!(messages.iter().filter(|message| message.author.role == ExternalRole::System).count(), 1);+ let ExternalContent::SystemContent(content) = &messages[0].content[0] else {+ panic!("expected a native system preamble");+ };+ assert_eq!(content.reasoning_effort, Some(openai_harmony::chat::ReasoningEffort::Low));+ let ExternalContent::DeveloperContent(instructions) = &messages[1].content[0] else {+ panic!("expected the user's instructions");+ };+ assert_eq!(instructions.instructions.as_deref(), Some("Keep answers brief"));+ }++ #[test]+ fn later_turns_preserve_one_preamble_on_replay_and_do_not_repeat_it_on_append() {+ let history = [+ ChatMessage::user().with_text("Hello".to_string()),+ ChatMessage::assistant().with_text("Hi".to_string()),+ ChatMessage::user().with_text("Tell me more".to_string()),+ ];++ // The token session resets and re-encodes the full Harmony history.+ let replay = bridge_messages_to_harmony(&history, true).unwrap();+ let explicit_history: Vec<_> =+ std::iter::once(ChatMessage::system().with_reasoning_effort(ReasoningEffort::Medium))+ .chain(history.iter().cloned())+ .collect();+ assert_eq!(replay, bridge_messages_to_harmony(&explicit_history, true).unwrap());+ assert_eq!(replay.iter().filter(|message| message.author.role == ExternalRole::System).count(), 1);++ // Encoding also supports direct appends without resetting its state.+ let appended = bridge_messages_to_harmony(&history[2..], false).unwrap();+ assert_eq!(appended.len(), 1);+ assert_eq!(appended[0].author.role, ExternalRole::User);+ assert_eq!(appended[0], replay[3]);+ }+}diff --git a/crates/legacy/hanashi/src/chat/harmony/config.rs b/crates/legacy/hanashi/src/chat/harmony/config.rsindex 5d079c245..9f9df79c0 100644--- a/crates/legacy/hanashi/src/chat/harmony/config.rs+++ b/crates/legacy/hanashi/src/chat/harmony/config.rs@@ -1,5 +1,7 @@use serde::{Deserialize, Serialize};-use shoji::types::session::chat::ChatModelCapabilities;+use shoji::types::{basic::ReasoningEffort, session::chat::ChatModelCapabilities};++use crate::chat::harmony::bridging::FromHarmony;#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]#[serde(tag = "name", rename_all = "snake_case")]@@ -9,12 +11,20 @@ pub enum HarmonyConfig {}impl HarmonyConfig {+ pub fn default_reasoning_effort(&self) -> Option<ReasoningEffort> {+ match self {+ HarmonyConfig::GptOss => {+ openai_harmony::chat::SystemContent::default().reasoning_effort.map(ReasoningEffort::from_harmony)+ },+ }+ }+pub fn capabilities(&self) -> ChatModelCapabilities {match self {HarmonyConfig::GptOss => ChatModelCapabilities {supports_reasoning: true,supports_disable_reasoning: false,- reasoning_efforts: vec![],+ reasoning_efforts: vec![ReasoningEffort::Low, ReasoningEffort::Medium, ReasoningEffort::High],supports_tools: true,supports_multiple_tool_calls: false,requires_tools: false,@@ -22,3 +32,18 @@ impl HarmonyConfig {}}}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn gpt_oss_exposes_supported_efforts_and_native_default() {+ let config = HarmonyConfig::GptOss;+ assert_eq!(+ config.capabilities().reasoning_efforts,+ vec![ReasoningEffort::Low, ReasoningEffort::Medium, ReasoningEffort::High]+ );+ assert_eq!(config.default_reasoning_effort(), Some(ReasoningEffort::Medium));+ }+}diff --git a/crates/legacy/hanashi/src/chat/harmony/mod.rs b/crates/legacy/hanashi/src/chat/harmony/mod.rsindex 46a87c333..c13637517 100644--- a/crates/legacy/hanashi/src/chat/harmony/mod.rs+++ b/crates/legacy/hanashi/src/chat/harmony/mod.rs@@ -128,6 +128,7 @@ impl EncodingTrait for HarmonyEncodingImpl {&mut self,messages: Self::Input,) -> Result<(), Self::Error> {+ let is_initial = self.state.messages.is_empty();self.state.messages.extend(messages.clone());self.completion_message_start = self.state.messages.len();// Keep an empty completion in state until decoding replaces it. Cancellation@@ -138,7 +139,7 @@ impl EncodingTrait for HarmonyEncodingImpl {.map_err(|_| Error::UnableToLoadEncoding)?;self.header_buffer = Some(Vec::new());- let conversation = HarmonyConversation::from_messages(bridge_messages_to_harmony(&messages)?);+ let conversation = HarmonyConversation::from_messages(bridge_messages_to_harmony(&messages, is_initial)?);let token_ids = self.encoding.render_conversation_for_completion(&conversation, HarmonyRole::Assistant, None)diff --git a/crates/legacy/hanashi/tests/test_continuation.rs b/crates/legacy/hanashi/tests/test_continuation.rsindex ebc5cbc9a..6dfd6d112 100644--- a/crates/legacy/hanashi/tests/test_continuation.rs+++ b/crates/legacy/hanashi/tests/test_continuation.rs@@ -149,6 +149,64 @@ fn formatting_and_sampled_token_ids_survive_repeated_tool_continuations() {}}+#[test]+fn structured_tool_results_render_as_json_without_changing_history() {+ for result in [+ serde_json::json!({+ "type": "line",+ "title": "Revenue < forecast: \"2026\"",+ "labels": ["January", "February"],+ "datasets": [{"label": "Revenue", "data": [12.5, 15.0]}],+ }),+ serde_json::json!({"error": "Invalid chart", "retryable": true, "details": null}),+ serde_json::json!([{"x": 1, "y": 2}, null, "point"]),+ serde_json::json!(12.5),+ serde_json::json!(true),+ serde_json::Value::Null,+ serde_json::json!("done"),+ ] {+ let config = HanashiConfig::Qwen38;+ let mut live = encoding(config.clone());+ let mut history = history();+ live.encode(history.clone()).unwrap();+ let options = r#"{"a":1, "b": 2}"#;+ decode(&mut live, &(tool_call(options) + " <|im_end|>"));+ let sampled = live.state().tokens.clone();+ let sampled_text = live.state().text();+ add_tool_result(&live, &mut history, options);+ let result_index = history.len() - 1;+ let ChatContentBlock::ToolCallResult {+ value,+ ..+ } = &mut history[result_index].content[0]+ else {+ unreachable!();+ };+ *value = Value::from(result.clone());+ let original_result = history[result_index].clone();+ let text = result.as_str().map(str::to_owned).unwrap_or_else(|| result.to_string());+ let response = format!("<tool_response>\n{text}\n</tool_response>");++ live.try_append(&history).unwrap().expect("must continue after a structured tool result");++ assert_eq!(&live.state().tokens[..sampled.len()], sampled);+ assert_eq!(+ &live.state().text()[sampled_text.len()..],+ format!("\n<|im_start|>user\n{response}<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n")+ );+ assert_eq!(live.state().messages[result_index], original_result);+ assert_eq!(history[result_index], original_result);++ decode(&mut live, "Done.<|im_end|>");+ assert_eq!(live.state().messages[result_index], original_result);++ let mut replay = encoding(config);+ replay.encode(history).unwrap();+ assert!(replay.state().text().contains(&response));+ assert_eq!(replay.state().messages[result_index], original_result);+ }+}+#[test]fn edited_history_and_render_context_are_rejected_without_mutation() {let mut encoding = encoding(HanashiConfig::Qwen38);diff --git a/crates/legacy/hanashi/tests/test_encoding.rs b/crates/legacy/hanashi/tests/test_encoding.rsindex b27d6754d..cb996e5e1 100644--- a/crates/legacy/hanashi/tests/test_encoding.rs+++ b/crates/legacy/hanashi/tests/test_encoding.rs@@ -714,10 +714,12 @@ fn test_decoding_muse_glimmer_reasoning_and_tool_call() {}#[test]-fn test_rendering_functiongemma_non_object_tool_results() {+fn test_rendering_functiongemma_structured_tool_results() {let config = HanashiConfig::FunctionGemma.resolve().unwrap();let renderer = hanashi::chat::hanashi::renderer::Renderer::new(config.rendering);let cases = [+ (serde_json::json!({"count": 2, "series": [1, 2]}), "response:get_value{count:2,series:[1,2]}"),+ (serde_json::json!("ready"), "response:get_value{value:<escape>ready<escape>}"),(serde_json::json!(42), "response:get_value{value:42}"),(serde_json::json!(["first", "second"]),diff --git a/crates/legacy/nagare/Cargo.toml b/crates/legacy/nagare/Cargo.tomlindex bd396e55e..1aa2cf83d 100644--- a/crates/legacy/nagare/Cargo.toml+++ b/crates/legacy/nagare/Cargo.toml@@ -60,7 +60,6 @@ hanashi.workspace = trueserde.workspace = trueserde_json.workspace = trueschemars.workspace = true-chrono.workspace = truetracing.workspace = truethiserror.workspace = trueindexmap.workspace = truediff --git a/crates/legacy/nagare/src/chat/mod.rs b/crates/legacy/nagare/src/chat/mod.rsindex 8245abeea..e0e78ebfc 100644--- a/crates/legacy/nagare/src/chat/mod.rs+++ b/crates/legacy/nagare/src/chat/mod.rs@@ -6,6 +6,8 @@ mod chat_instance;mod error;pub mod message;pub mod token;+#[cfg(test)]+mod tool_cancellation_tests;use std::{panic::AssertUnwindSafe, sync::Arc};@@ -35,12 +37,7 @@ use uuid::Uuid;#[cfg(feature = "bindings-uniffi")]use crate::tool::bindings_uniffi::ForeignTool;-use crate::{- telemetry::{Telemetry, TelemetryEvent},- tool::{func_def::ToolDescriptor, registry::ToolRegistry},-};--const DEFAULT_TOOL_TURN_LIMIT: u32 = 10;+use crate::tool::{func_def::ToolDescriptor, registry::ToolRegistry};#[bindings::export(Enumeration)]#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]@@ -51,12 +48,15 @@ pub enum ChatSessionStreamChunk {Error {error: ChatSessionError,},+ ToolResults {+ messages: Vec<ChatMessage>,+ },}#[bindings::export(Class(Stream))]#[derive(Clone)]pub struct ChatSessionStream {- receiver: Arc<Mutex<mpsc::UnboundedReceiver<Result<Vec<ChatReply>, ChatSessionError>>>>,+ receiver: Arc<Mutex<mpsc::UnboundedReceiver<ChatSessionStreamChunk>>>,cancel_token: CancelToken,}@@ -64,15 +64,7 @@ pub struct ChatSessionStream {impl ChatSessionStream {#[bindings::export(Method(StreamNext))]pub async fn next(&self) -> Option<ChatSessionStreamChunk> {- match self.receiver.lock().await.recv().await {- Some(Ok(replies)) => Some(ChatSessionStreamChunk::Replies {- replies,- }),- Some(Err(error)) => Some(ChatSessionStreamChunk::Error {- error,- }),- None => None,- }+ self.receiver.lock().await.recv().await}#[bindings::export(Method(Getter))]@@ -131,8 +123,6 @@ pub struct ChatSession {instance: Arc<Mutex<Instance>>,state: Arc<Mutex<ChatSessionState>>,messages: Arc<Mutex<Vec<ChatMessage>>>,- model_id: String,- telemetry: Telemetry,tool_registry: Option<Arc<Mutex<ToolRegistry>>>,}@@ -142,18 +132,13 @@ impl ChatSession {config: ChatConfig,model: Model,path: Option<String>,- telemetry: Telemetry,) -> Result<Self, ChatSessionError> {let instance = ChatInstance::new(backend, config, model, path).await?;- Self::with_instance(&instance, telemetry).await+ Self::with_instance(&instance).await}- pub async fn with_instance(- instance: &ChatInstance,- telemetry: Telemetry,- ) -> Result<Self, ChatSessionError> {+ pub async fn with_instance(instance: &ChatInstance) -> Result<Self, ChatSessionError> {let model = instance.model();- let model_id = model.identifier.clone();let instance = tokio::spawn({let instance = instance.clone();@@ -178,8 +163,6 @@ impl ChatSession {instance: Arc::new(Mutex::new(instance)),state: Arc::new(Mutex::new(ChatSessionState::Idle)),messages: Arc::new(Mutex::new(Vec::new())),- model_id,- telemetry,tool_registry: supports_tool_calls.then(|| Arc::new(Mutex::new(ToolRegistry::new()))),})}@@ -259,7 +242,7 @@ impl ChatSession {/// Returns the turn's replies, or `None` when the turn must not continue (the stream errored or the receiver was dropped).async fn send_input(&self,- sender: &UnboundedSender<Result<Vec<ChatReply>, ChatSessionError>>,+ sender: &UnboundedSender<ChatSessionStreamChunk>,input: Vec<ChatMessage>,config: ChatReplyConfig,cancel_token: CancellationToken,@@ -281,12 +264,7 @@ impl ChatSession {messages_guard.clone()};- self.telemetry.report(TelemetryEvent::ModelInferenceStarted {- model_id: self.model_id.clone(),- });-let mut outputs: IndexMap<u32, ChatReply> = IndexMap::new();- let mut error_value: Option<serde_json::Value> = None;let mut interrupted = false;let mut generated_tool_call_identifiers: Vec<Option<String>> = Vec::new();let mut latest_stats: Option<ChatReplyStats> = None;@@ -320,7 +298,12 @@ impl ChatSession {drop(messages_guard);// send new output- if sender.send(Ok(outputs.values().cloned().collect())).is_err() {+ if sender+ .send(ChatSessionStreamChunk::Replies {+ replies: outputs.values().cloned().collect(),+ })+ .is_err()+ {interrupted = true;break;}@@ -332,8 +315,9 @@ impl ChatSession {}},Err(error) => {- error_value = Some(serde_json::json!({ "message": error.to_string() }));- let _ = sender.send(Err(error));+ let _ = sender.send(ChatSessionStreamChunk::Error {+ error,+ });interrupted = true;break;},@@ -342,18 +326,6 @@ impl ChatSession {drop(stream);drop(instance);- // telemetry report result- if let Some(error) = error_value {- self.telemetry.report(TelemetryEvent::ModelInferenceFailed {- error,- });- } else if let Some(stats) = latest_stats.as_ref() {- self.telemetry.report(TelemetryEvent::ModelInferenceFinished {- model_id: self.model_id.clone(),- stats: stats.clone().into(),- });- }-if !interrupted && let Some(stats) = latest_stats {completed_stats.push(stats);}@@ -363,18 +335,19 @@ impl ChatSession {async fn execute_turn(&self,- sender: UnboundedSender<Result<Vec<ChatReply>, ChatSessionError>>,+ sender: UnboundedSender<ChatSessionStreamChunk>,input: Vec<ChatMessage>,config: ChatReplyConfig,cancel_token: CancellationToken,) {// check stateif !self.try_transition(ChatSessionState::Idle, ChatSessionState::Generation).await {- let _ = sender.send(Err(ChatSessionError::UnableToPerformOperationInCurrentState {}));+ let _ = sender.send(ChatSessionStreamChunk::Error {+ error: ChatSessionError::UnableToPerformOperationInCurrentState {},+ });return;}- let tool_turn_limit = config.tool_turn_limit.unwrap_or(DEFAULT_TOOL_TURN_LIMIT);let mut tool_turns: u32 = 0;let mut next_input = input;let mut completed_stats = Vec::new();@@ -417,13 +390,17 @@ impl ChatSession {break;}- if tool_turns >= tool_turn_limit {- let _ = sender.send(Err(ChatSessionError::ToolTurnLimitExceeded {- limit: tool_turn_limit,- }));- break;+ if let Some(limit) = config.tool_turn_limit {+ if tool_turns >= limit {+ let _ = sender.send(ChatSessionStreamChunk::Error {+ error: ChatSessionError::ToolTurnLimitExceeded {+ limit,+ },+ });+ break;+ }+ tool_turns += 1;}- tool_turns += 1;if self.try_transition(ChatSessionState::Generation, ChatSessionState::ToolCalling).await {let tool_messages = tokio::select! {@@ -431,9 +408,25 @@ impl ChatSession {_ = cancel_token.cancelled() => break,};if self.try_transition(ChatSessionState::ToolCalling, ChatSessionState::Generation).await {- if !tool_messages.is_empty() && !cancel_token.is_cancelled() {- next_input = tool_messages;- } else {+ if tool_messages.is_empty() {+ break;+ }+ // Publish completed tools before the next prefill, and retain+ // their results even if cancellation arrives before it starts.+ self.messages.lock().await.extend(tool_messages.clone());+ if sender+ .send(ChatSessionStreamChunk::ToolResults {+ messages: tool_messages,+ })+ .is_err()+ || cancel_token.is_cancelled()+ {+ break;+ }+ // Native prefill can block the next poll. Let consumers+ // render the completed tool before starting that work.+ tokio::task::yield_now().await;+ if cancel_token.is_cancelled() || sender.is_closed() {break;}} else {@@ -624,6 +617,9 @@ impl ChatSession {ChatSessionStreamChunk::Error {error,} => return Err(error),+ ChatSessionStreamChunk::ToolResults {+ ..+ } => {},}}outputs.ok_or(ChatSessionError::NoResponse {})@@ -637,7 +633,7 @@ impl ChatSession {) -> ChatSessionStream {let cancel_token_to_return = CancelToken::new();let cancel_token = cancel_token_to_return.inner().clone();- let (sender, receiver) = mpsc::unbounded_channel::<Result<Vec<ChatReply>, ChatSessionError>>();+ let (sender, receiver) = mpsc::unbounded_channel();let session = self.clone();let turn = async move { session.execute_turn(sender, input, config, cancel_token).await }.in_current_span();#[cfg(feature = "bindings-pyo3")]@@ -930,8 +926,220 @@ fn merge_tool_namespaces(#[cfg(test)]mod tests {+ use std::{+ convert::Infallible,+ pin::Pin,+ sync::atomic::{AtomicUsize, Ordering},+ time::Duration,+ };++ use shoji::traits::backend::{Error, Instance as BackendInstance, InstanceStream, NoMetricsStream, State};+ use tokio::sync::Notify;+use super::*;+ struct EmptyState;+ impl State for EmptyState {}++ struct ToolsThenAnswer {+ rounds: usize,+ continue_answer: Option<Arc<Notify>>,+ }++ impl BackendInstance for ToolsThenAnswer {+ type StreamConfig = ChatReplyConfig;+ type StreamInput = Vec<ChatMessage>;+ type StreamOutput = BackendOutput;+ type StreamMetrics = Option<Infallible>;++ fn state(&self) -> Pin<Box<dyn Future<Output = Result<Box<dyn State>, Error>> + Send + '_>> {+ Box::pin(async { Ok(Box::new(EmptyState) as Box<dyn State>) })+ }++ fn stream<'a>(+ &'a self,+ input: &'a Self::StreamInput,+ _state: &'a mut dyn State,+ _config: Self::StreamConfig,+ _cancel_token: CancellationToken,+ ) -> Pin<Box<dyn InstanceStream<Item = Result<BackendOutput, Error>, Metrics = Self::StreamMetrics> + Send + 'a>>+ {+ Box::pin(NoMetricsStream::new(futures::stream::once(async move {+ let completed_rounds = input.iter().map(|message| message.tool_call_results().len()).sum::<usize>();+ if completed_rounds >= self.rounds {+ if let Some(continue_answer) = &self.continue_answer {+ continue_answer.notified().await;+ }+ Ok(BackendOutput {+ text: Some("Answer".to_string()),+ finish_reason: Some(ChatReplyFinishReason::Stop),+ ..Default::default()+ })+ } else {+ Ok(BackendOutput {+ tool_calls: vec![ToolCallState::Finished(ToolCall {+ identifier: Some(format!("call-{}", completed_rounds + 1)),+ name: "clock".to_string(),+ arguments: serde_json::json!({}).into(),+ })],+ finish_reason: Some(ChatReplyFinishReason::ToolCalls),+ ..Default::default()+ })+ }+ })))+ }++ fn peak_memory_usage(&self) -> Option<usize> {+ None+ }+ }++ async fn tool_session(+ rounds: usize,+ continue_answer: Option<Arc<Notify>>,+ ) -> ChatSession {+ let instance = message::Session::with_instance(Arc::new(ToolsThenAnswer {+ rounds,+ continue_answer,+ }))+ .await+ .unwrap();+ let mut session = ChatSession {+ instance: Arc::new(Mutex::new(Instance::Message(instance))),+ state: Arc::new(Mutex::new(ChatSessionState::Idle)),+ messages: Arc::new(Mutex::new(Vec::new())),+ tool_registry: Some(Arc::new(Mutex::new(ToolRegistry::new()))),+ };+ session+ .add_tool(ToolDescriptor::new(+ "clock".to_string(),+ "Return the time".to_string(),+ None,+ None,+ Box::new(|_| Box::new(async { Ok(serde_json::json!("noon").into()) })),+ ))+ .await+ .unwrap();+ session+ }++ #[tokio::test]+ async fn tool_results_arrive_before_the_next_answer_and_enter_history_once() {+ let continue_answer = Arc::new(Notify::new());+ let session = tool_session(1, Some(continue_answer.clone())).await;+ let stream = session+ .reply_with_stream(vec![ChatMessage::user().with_text("Time?".to_string())], ChatReplyConfig::default())+ .await;+ assert!(matches!(stream.next().await, Some(ChatSessionStreamChunk::Replies { .. })));+ let event = tokio::time::timeout(Duration::from_secs(1), stream.next()).await.unwrap().unwrap();+ let ChatSessionStreamChunk::ToolResults {+ messages,+ } = event+ else {+ panic!("Expected tool results before the paused answer");+ };+ assert_eq!(messages.len(), 1);+ assert_eq!(messages[0].tool_call_results()[0].0.as_deref(), Some("call-1"));+ let results_count =+ |messages: Vec<ChatMessage>| messages.iter().map(|m| m.tool_call_results().len()).sum::<usize>();+ assert_eq!(results_count(session.messages().await), 1);++ continue_answer.notify_one();+ assert!(+ matches!(stream.next().await, Some(ChatSessionStreamChunk::Replies { replies }) if replies[0].message.text().as_deref() == Some("Answer"))+ );+ assert!(stream.next().await.is_none());+ assert_eq!(results_count(session.messages().await), 1);+ }++ #[tokio::test]+ async fn automatic_tool_turns_are_unlimited_by_default() {+ let session = tool_session(12, None).await;+ let replies = tokio::time::timeout(+ Duration::from_secs(1),+ session.reply(vec![ChatMessage::user().with_text("Time?".to_string())], ChatReplyConfig::default()),+ )+ .await+ .unwrap()+ .unwrap();+ assert_eq!(replies[0].message.text().as_deref(), Some("Answer"));+ assert_eq!(session.messages().await.iter().map(|message| message.tool_call_results().len()).sum::<usize>(), 12);+ assert_eq!(session.state().await, ChatSessionState::Idle);+ }++ #[tokio::test]+ async fn explicit_tool_turn_limits_stop_at_the_requested_round() {+ for limit in [0, 3, 12] {+ let session = tool_session(13, None).await;+ let error = tokio::time::timeout(+ Duration::from_secs(1),+ session.reply(+ vec![ChatMessage::user().with_text("Time?".to_string())],+ ChatReplyConfig::default().with_tool_turn_limit(Some(limit)),+ ),+ )+ .await+ .unwrap()+ .unwrap_err();+ assert!(matches!(error, ChatSessionError::ToolTurnLimitExceeded { limit: actual } if actual == limit));+ assert_eq!(+ session.messages().await.iter().map(|message| message.tool_call_results().len()).sum::<usize>(),+ limit as usize+ );+ assert_eq!(session.state().await, ChatSessionState::Idle);+ }+ }++ #[tokio::test]+ async fn an_unlimited_tool_loop_can_be_cancelled_during_a_tool() {+ let mut session = tool_session(usize::MAX, None).await;+ let tool_started = Arc::new(Notify::new());+ let calls = Arc::new(AtomicUsize::new(0));+ session+ .add_tool(ToolDescriptor::new(+ "clock".to_string(),+ "Return the time".to_string(),+ None,+ None,+ Box::new({+ let calls = calls.clone();+ let tool_started = tool_started.clone();+ move |_| {+ let calls = calls.clone();+ let tool_started = tool_started.clone();+ Box::new(async move {+ if calls.fetch_add(1, Ordering::SeqCst) == 12 {+ tool_started.notify_one();+ std::future::pending::<()>().await;+ }+ Ok(serde_json::json!("noon").into())+ })+ }+ }),+ ))+ .await+ .unwrap();+ let stream = session+ .reply_with_stream(vec![ChatMessage::user().with_text("Time?".to_string())], ChatReplyConfig::default())+ .await;++ tokio::time::timeout(Duration::from_secs(1), tool_started.notified()).await.unwrap();+ stream.cancel_token().cancel();+ tokio::time::timeout(Duration::from_secs(1), async {+ while let Some(chunk) = stream.next().await {+ assert!(!matches!(chunk, ChatSessionStreamChunk::Error { .. }));+ }+ })+ .await+ .unwrap();++ assert_eq!(calls.load(Ordering::SeqCst), 13);+ assert_eq!(session.state().await, ChatSessionState::Idle);+ let messages = session.messages().await;+ assert_eq!(messages.iter().map(|message| message.tool_call_results().len()).sum::<usize>(), 12);+ assert_eq!(messages.last().unwrap().role, ChatRole::Tool {});+ }+#[test]fn normalize_tool_call_arguments_canonicalizes_and_unwraps_string_encoding() {let object = normalize_tool_call_arguments(Value {diff --git a/crates/legacy/nagare/src/chat/tool_cancellation_tests.rs b/crates/legacy/nagare/src/chat/tool_cancellation_tests.rsnew file mode 100644index 000000000..12e208319--- /dev/null+++ b/crates/legacy/nagare/src/chat/tool_cancellation_tests.rs@@ -0,0 +1,242 @@+use std::{+ pin::Pin,+ sync::{Arc, Mutex as StdMutex},+ time::Duration,+};++use hanashi::chat::{EncodingConfig, hanashi::config::HanashiConfig};+use shoji::{+ traits::backend::{+ Error, Instance as BackendInstance, InstanceStream, NoMetricsStream, State,+ chat_token::{Instance as TokenInstance, StreamInput, StreamOutput, TokenStreamMetrics},+ },+ types::{+ basic::SamplingParameters,+ model::{ModelAccessibility, ModelSource},+ },+};+use tokenizers::{AddedToken, Tokenizer, models::bpe::BPE, pre_tokenizers::byte_level::ByteLevel};++use super::*;++struct EmptyState;+impl State for EmptyState {}++// Exercise the real token session, including its cached prefix and FunctionGemma+// renderer/parser. Only the model's sampled tokens are scripted.+struct FunctionGemma {+ tokenizer: Arc<Tokenizer>,+ cached_tokens: StdMutex<Vec<u32>>,+ prompts: StdMutex<Vec<String>>,+}++impl BackendInstance for FunctionGemma {+ type StreamConfig = ChatReplyConfig;+ type StreamInput = StreamInput;+ type StreamOutput = StreamOutput;+ type StreamMetrics = Option<TokenStreamMetrics>;++ fn state(&self) -> Pin<Box<dyn Future<Output = Result<Box<dyn State>, Error>> + Send + '_>> {+ Box::pin(async {+ self.cached_tokens.lock().unwrap().clear();+ Ok(Box::new(EmptyState) as Box<dyn State>)+ })+ }++ fn stream<'a>(+ &'a self,+ input: &'a StreamInput,+ _state: &'a mut dyn State,+ _config: ChatReplyConfig,+ _cancel_token: CancellationToken,+ ) -> Pin<Box<dyn InstanceStream<Item = Result<StreamOutput, Error>, Metrics = Self::StreamMetrics> + Send + 'a>>+ {+ let mut cached = self.cached_tokens.lock().unwrap();+ cached.extend(input.iter().map(|id| *id as u32));+ let prompt = self.tokenizer.decode(&cached, false).unwrap();+ let output = if prompt.contains("Follow up") {+ "Answer<end_of_turn>"+ } else if prompt.contains("<end_function_response>") {+ "Continuation<end_of_turn>"+ } else {+ "<start_function_call>call:clock{}<end_function_call>"+ };+ self.prompts.lock().unwrap().push(prompt);+ let tokens = self.tokenizer.encode(output, false).unwrap().get_ids().to_vec();+ cached.extend(&tokens);+ Box::pin(NoMetricsStream::new(futures::stream::iter(+ tokens.into_iter().map(|id| Ok(StreamOutput::Token(u64::from(id)))),+ )))+ }++ fn peak_memory_usage(&self) -> Option<usize> {+ None+ }+}++impl TokenInstance for FunctionGemma {+ fn tokenizer(&self) -> Arc<Tokenizer> {+ self.tokenizer.clone()+ }++ fn max_context_length(&self) -> Option<usize> {+ None+ }++ fn stop_token_ids(&self) -> Option<Box<[u64]>> {+ Some(+ ["<end_function_call>", "<end_of_turn>"]+ .map(|token| u64::from(self.tokenizer.token_to_id(token).unwrap()))+ .into(),+ )+ }++ fn sampling_defaults(&self) -> SamplingParameters {+ SamplingParameters::default()+ }+}++async fn session() -> (ChatSession, Arc<FunctionGemma>) {+ let mut config = HanashiConfig::FunctionGemma.resolve().unwrap();+ let mut alphabet: Vec<_> = ByteLevel::alphabet().into_iter().collect();+ alphabet.sort_unstable();+ let vocabulary: [(String, u32); 256] = alphabet+ .into_iter()+ .enumerate()+ .map(|(index, value)| (value.to_string(), index as u32))+ .collect::<Vec<_>>()+ .try_into()+ .unwrap();+ let mut tokenizer = Tokenizer::new(BPE::builder().vocab_and_merges(vocabulary, Vec::new()).build().unwrap());+ tokenizer.with_pre_tokenizer(Some(ByteLevel::new(false, false, false)));+ tokenizer.with_decoder(Some(ByteLevel::new(false, false, false)));+ tokenizer.add_tokens(&["developer", "user", "model", "clock"].map(|value| AddedToken::from(value, false)));+ let mut markers = config.parsing.framing_config().tokens;+ markers.extend(["<bos>".to_string(), "<eos>".to_string()]);+ tokenizer.add_special_tokens(&markers.into_iter().map(|value| AddedToken::from(value, true)).collect::<Vec<_>>());+ config.tokens.bos_token_id = tokenizer.token_to_id("<bos>");+ config.tokens.eos_token_id = tokenizer.token_to_id("<eos>");+ let backend = Arc::new(FunctionGemma {+ tokenizer: Arc::new(tokenizer),+ cached_tokens: StdMutex::new(Vec::new()),+ prompts: StdMutex::new(Vec::new()),+ });+ let mut encoding = serde_json::to_value(EncodingConfig::Hanashi {+ config: HanashiConfig::Custom {+ config,+ },+ })+ .unwrap();+ // JinjaConfig omits empty lists on serialization but requires this key when read.+ encoding["rendering"]["jinja"]["required_functions"] = serde_json::json!([]);+ let model = Model::external(+ "test".into(),+ "test".into(),+ "Test".into(),+ "test".into(),+ "Test".into(),+ "1".into(),+ vec![],+ ModelAccessibility::OnDevice {+ source: ModelSource::Filesystem {+ path: String::new(),+ },+ },+ Some(encoding.into()),+ );+ let instance = token::Session::with_instance(backend.clone(), String::new(), &model).await.unwrap();+ let mut session = ChatSession {+ instance: Arc::new(Mutex::new(Instance::Token(instance))),+ state: Arc::new(Mutex::new(ChatSessionState::Idle)),+ messages: Arc::new(Mutex::new(Vec::new())),+ tool_registry: Some(Arc::new(Mutex::new(ToolRegistry::new()))),+ };+ session+ .add_tool(ToolDescriptor::new(+ "clock".into(),+ "Return the time".into(),+ None,+ None,+ Box::new(|_| Box::new(async { Ok(serde_json::json!("noon").into()) })),+ ))+ .await+ .unwrap();+ (session, backend)+}++async fn stop_after_tool_result(drop_receiver: bool) {+ let (session, backend) = session().await;+ let stream = session+ .reply_with_stream(vec![ChatMessage::user().with_text("Time?".into())], ChatReplyConfig::default())+ .await;+ let completed = tokio::time::timeout(Duration::from_secs(5), async {+ loop {+ match stream.next().await.unwrap() {+ ChatSessionStreamChunk::ToolResults {+ messages,+ } => break messages,+ ChatSessionStreamChunk::Error {+ error,+ } => panic!("first reply failed: {error}"),+ ChatSessionStreamChunk::Replies {+ ..+ } => {},+ }+ }+ })+ .await+ .unwrap();+ assert_eq!(completed.len(), 1);+ assert_eq!(completed[0].tool_call_results()[0].2.json, "\"noon\"");+ if drop_receiver {+ drop(stream);+ } else {+ stream.cancel_token().cancel();+ assert!(stream.next().await.is_none());+ }+ tokio::time::timeout(Duration::from_secs(5), async {+ while session.state().await != ChatSessionState::Idle {+ tokio::task::yield_now().await;+ }+ })+ .await+ .unwrap();+ assert_eq!(backend.prompts.lock().unwrap().len(), 1, "stopping must not start another prefill");+ let retained = session.messages().await;+ assert_eq!(retained.last(), completed.last());++ let replies = session+ .reply(vec![ChatMessage::user().with_text("Follow up".into())], ChatReplyConfig::default())+ .await+ .unwrap();+ assert_eq!(replies[0].message.text().as_deref(), Some("Answer"));+ let prompts = backend.prompts.lock().unwrap();+ let resumed = prompts.last().unwrap();+ assert!(resumed.contains(+ "<start_function_response>response:clock{value:<escape>noon<escape>}<end_function_response><end_of_turn>\n<start_of_turn>user\nFollow up<end_of_turn>\n<start_of_turn>model\n"+ ), "wrong interrupted-turn boundary: {resumed}");+ assert_eq!(resumed.matches("<start_function_call>").count(), 1);+ assert_eq!(resumed.matches("<start_function_response>").count(), 1);+}++#[tokio::test]+async fn cancellation_after_tool_results_keeps_history_and_allows_the_next_user_turn() {+ stop_after_tool_result(false).await;+}++#[tokio::test]+async fn dropped_receiver_after_tool_results_skips_prefill_and_allows_the_next_user_turn() {+ stop_after_tool_result(true).await;+}++#[tokio::test]+async fn normal_tool_continuation_keeps_the_existing_model_turn_open() {+ let (session, backend) = session().await;+ let replies =+ session.reply(vec![ChatMessage::user().with_text("Time?".into())], ChatReplyConfig::default()).await.unwrap();+ assert_eq!(replies[0].message.text().as_deref(), Some("Continuation"));+ let prompts = backend.prompts.lock().unwrap();+ assert_eq!(prompts.len(), 2);+ assert!(prompts[1].ends_with("<end_function_response>"));+ assert_eq!(prompts[1].matches("<start_of_turn>model\n").count(), 1);+}diff --git a/crates/legacy/nagare/src/lib.rs b/crates/legacy/nagare/src/lib.rsindex 9a74219af..bed08231a 100644--- a/crates/legacy/nagare/src/lib.rs+++ b/crates/legacy/nagare/src/lib.rs@@ -5,7 +5,6 @@ uniffi::setup_scaffolding!();pub mod api;pub mod chat;pub mod classification;-pub mod telemetry;pub mod text_to_speech;pub mod tool;mod util;diff --git a/crates/legacy/nagare/src/telemetry/body.rs b/crates/legacy/nagare/src/telemetry/body.rsdeleted file mode 100644index 9456282a3..000000000--- a/crates/legacy/nagare/src/telemetry/body.rs+++ /dev/null@@ -1,77 +0,0 @@-use serde_json::Value;--use super::{TelemetryContext, record::TelemetryRecord};--pub fn build_body(- context: &TelemetryContext,- record: &TelemetryRecord,-) -> Value {- let mut body = serde_json::to_value(&record.event).unwrap_or_else(|_| Value::Object(Default::default()));- if let Value::Object(map) = &mut body {- if let Ok(Value::Object(context)) = serde_json::to_value(context) {- for (key, value) in context {- map.insert(key, value);- }- }- map.insert("event_time".to_string(), Value::String(record.event_time.clone()));- }- body-}--#[cfg(test)]-mod tests {- use serde_json::json;-- use super::{- super::{TelemetryContext, TelemetryDevice, record::TelemetryRecord},- build_body,- };- use crate::telemetry::TelemetryEvent;-- fn context() -> TelemetryContext {- TelemetryContext {- engine_session_id: "session-1".to_string(),- uzu_version: "0.5.7".to_string(),- toolchain_version: "0.12.1".to_string(),- device: TelemetryDevice {- os_name: Some("macOS".to_string()),- cpu_name: None,- memory_total: 0,- is_environment_sandboxed: false,- },- }- }-- #[test]- fn build_body_nests_event_time_device_and_payload() {- let record = TelemetryRecord {- event_time: "2026-06-04T16:24:29Z".to_string(),- event: TelemetryEvent::ModelInferenceFinished {- model_id: "model-1".to_string(),- stats: Default::default(),- },- };- let body = build_body(&context(), &record);- assert_eq!(body["event_name"], json!("model_inference_finished"));- assert_eq!(body["event_time"], json!("2026-06-04T16:24:29Z"));- assert_eq!(body["engine_session_id"], json!("session-1"));- assert_eq!(body["uzu_version"], json!("0.5.7"));- assert_eq!(body["toolchain_version"], json!("0.12.1"));- assert_eq!(body["device"]["os_name"], json!("macOS"));- assert_eq!(body["payload"]["model_id"], json!("model-1"));- assert!(body["payload"]["stats"].is_object());- }-- #[test]- fn build_body_wraps_failure_error() {- let record = TelemetryRecord {- event_time: "2026-06-04T16:24:29Z".to_string(),- event: TelemetryEvent::ModelInferenceFailed {- error: json!({ "message": "boom" }),- },- };- let body = build_body(&context(), &record);- assert_eq!(body["event_name"], json!("model_inference_failed"));- assert_eq!(body["payload"]["error"]["message"], json!("boom"));- }-}diff --git a/crates/legacy/nagare/src/telemetry/context.rs b/crates/legacy/nagare/src/telemetry/context.rsdeleted file mode 100644index 3eb6f036f..000000000--- a/crates/legacy/nagare/src/telemetry/context.rs+++ /dev/null@@ -1,34 +0,0 @@-use serde::Serialize;--#[derive(Debug, Clone, Serialize)]-pub struct TelemetryDevice {- pub os_name: Option<String>,- pub cpu_name: Option<String>,- pub memory_total: i64,- pub is_environment_sandboxed: bool,-}--#[derive(Debug, Clone, Serialize)]-pub struct TelemetryContext {- pub engine_session_id: String,- pub uzu_version: String,- pub toolchain_version: String,- pub device: TelemetryDevice,-}--// The constructor lives only in non-wasm code so `uuid` is never referenced on wasm.-#[cfg(not(target_family = "wasm"))]-impl TelemetryContext {- pub fn new(- uzu_version: String,- toolchain_version: String,- device: TelemetryDevice,- ) -> Self {- Self {- engine_session_id: uuid::Uuid::new_v4().to_string(),- uzu_version,- toolchain_version,- device,- }- }-}diff --git a/crates/legacy/nagare/src/telemetry/event.rs b/crates/legacy/nagare/src/telemetry/event.rsdeleted file mode 100644index 9ffc394cc..000000000--- a/crates/legacy/nagare/src/telemetry/event.rs+++ /dev/null@@ -1,44 +0,0 @@-use serde::{Deserialize, Serialize};-use serde_json::Value;-use shoji::types::session::chat::{ChatReplyJoulesPerToken, ChatReplyStats};--/// Reply stats plus the metrics `ChatReplyStats` exposes as getters, which serde-/// would otherwise leave out of the reported payload.-#[derive(Debug, Clone, Default, Serialize, Deserialize)]-pub struct TelemetryStats {- #[serde(flatten)]- pub stats: ChatReplyStats,- pub input_joules_per_token: Option<ChatReplyJoulesPerToken>,- pub output_joules_per_token: Option<ChatReplyJoulesPerToken>,-}--impl From<ChatReplyStats> for TelemetryStats {- fn from(stats: ChatReplyStats) -> Self {- Self {- input_joules_per_token: stats.input_joules_per_token(),- output_joules_per_token: stats.output_joules_per_token(),- stats,- }- }-}--#[derive(Debug, Clone, Serialize, Deserialize)]-#[serde(tag = "event_name", content = "payload", rename_all = "snake_case")]-pub enum TelemetryEvent {- ModelDownloadStarted {- model_id: String,- },- ModelDownloadFinished {- model_id: String,- },- ModelInferenceStarted {- model_id: String,- },- ModelInferenceFinished {- model_id: String,- stats: TelemetryStats,- },- ModelInferenceFailed {- error: Value,- },-}diff --git a/crates/legacy/nagare/src/telemetry/events.rs b/crates/legacy/nagare/src/telemetry/events.rsdeleted file mode 100644index 92d81eef7..000000000--- a/crates/legacy/nagare/src/telemetry/events.rs+++ /dev/null@@ -1,12 +0,0 @@-use serde_json::Value;--use crate::api::Endpoint;--pub struct Events;--impl Endpoint for Events {- const PATH: &'static str = "telemetry/events";-- type Request = Value;- type Response = ();-}diff --git a/crates/legacy/nagare/src/telemetry/mod.rs b/crates/legacy/nagare/src/telemetry/mod.rsdeleted file mode 100644index 9f2f4d714..000000000--- a/crates/legacy/nagare/src/telemetry/mod.rs+++ /dev/null@@ -1,14 +0,0 @@-#[cfg(not(target_family = "wasm"))]-mod body;-mod context;-mod event;-#[cfg(not(target_family = "wasm"))]-mod events;-mod record;-mod telemetry;-#[cfg(not(target_family = "wasm"))]-mod worker;--pub use context::{TelemetryContext, TelemetryDevice};-pub use event::{TelemetryEvent, TelemetryStats};-pub use telemetry::Telemetry;diff --git a/crates/legacy/nagare/src/telemetry/record.rs b/crates/legacy/nagare/src/telemetry/record.rsdeleted file mode 100644index c918505bb..000000000--- a/crates/legacy/nagare/src/telemetry/record.rs+++ /dev/null@@ -1,34 +0,0 @@-use super::TelemetryEvent;--pub(super) struct TelemetryRecord {- pub(super) event_time: String,- pub(super) event: TelemetryEvent,-}--impl TelemetryRecord {- pub(super) fn new(event: TelemetryEvent) -> Self {- Self {- event_time: now_rfc3339(),- event,- }- }-}--fn now_rfc3339() -> String {- chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)-}--#[cfg(test)]-mod tests {- use super::TelemetryRecord;- use crate::telemetry::TelemetryEvent;-- #[test]- fn new_stamps_rfc3339_utc() {- let record = TelemetryRecord::new(TelemetryEvent::ModelDownloadFinished {- model_id: "model-1".to_string(),- });- assert!(record.event_time.ends_with('Z'), "expected Z suffix: {}", record.event_time);- assert!(chrono::DateTime::parse_from_rfc3339(&record.event_time).is_ok());- }-}diff --git a/crates/legacy/nagare/src/telemetry/telemetry.rs b/crates/legacy/nagare/src/telemetry/telemetry.rsdeleted file mode 100644index ed85be83c..000000000--- a/crates/legacy/nagare/src/telemetry/telemetry.rs+++ /dev/null@@ -1,67 +0,0 @@-#[cfg(not(target_family = "wasm"))]-use std::time::Duration;--#[cfg(not(target_family = "wasm"))]-use bon::bon;-use tokio::sync::mpsc;-#[cfg(not(target_family = "wasm"))]-use tokio::sync::mpsc::channel as TokioMpscChannel;--#[cfg(not(target_family = "wasm"))]-use super::TelemetryContext;-use super::{TelemetryEvent, record::TelemetryRecord};-#[cfg(not(target_family = "wasm"))]-use crate::api::{Client, RetryConfig};--#[cfg(not(target_family = "wasm"))]-const CAPACITY: usize = 256;--#[derive(Clone)]-pub struct Telemetry {- sender: Option<mpsc::Sender<TelemetryRecord>>,-}--impl Telemetry {- pub fn disabled() -> Self {- Self {- sender: None,- }- }-- pub fn report(- &self,- event: TelemetryEvent,- ) {- if let Some(sender) = &self.sender {- let _ = sender.try_send(TelemetryRecord::new(event));- }- }-}--#[cfg(not(target_family = "wasm"))]-#[bon]-impl Telemetry {- #[builder]- pub fn new(- #[builder(into)] base_url: String,- context: TelemetryContext,- ) -> Self {- let retry = RetryConfig {- max_attempts: 5,- base_delay: Duration::from_secs(1),- budget: Duration::from_secs(60),- };- let client = match Client::builder().base_url(base_url).retry(retry).build() {- Ok(client) => client,- Err(error) => {- tracing::warn!(%error, "telemetry disabled: failed to build client");- return Self::disabled();- },- };- let (sender, receiver) = TokioMpscChannel::<TelemetryRecord>(CAPACITY);- tokio::spawn(super::worker::run(client, context, receiver));- Self {- sender: Some(sender),- }- }-}diff --git a/crates/legacy/nagare/src/telemetry/worker.rs b/crates/legacy/nagare/src/telemetry/worker.rsdeleted file mode 100644index 1cb689537..000000000--- a/crates/legacy/nagare/src/telemetry/worker.rs+++ /dev/null@@ -1,16 +0,0 @@-use tokio::sync::mpsc;--use super::{TelemetryContext, body::build_body, events::Events, record::TelemetryRecord};-use crate::api::Client;--pub(super) async fn run(- client: Client,- context: TelemetryContext,- mut receiver: mpsc::Receiver<TelemetryRecord>,-) {- while let Some(record) = receiver.recv().await {- if let Err(error) = client.send::<Events>(&build_body(&context, &record)).await {- tracing::warn!(%error, "telemetry event dropped");- }- }-}diff --git a/crates/legacy/nagare/src/tool/func_def.rs b/crates/legacy/nagare/src/tool/func_def.rsindex ac6cd553b..b72153848 100644--- a/crates/legacy/nagare/src/tool/func_def.rs+++ b/crates/legacy/nagare/src/tool/func_def.rs@@ -98,7 +98,7 @@ fn coerce_to_schema(}fn coerce_to_schema_with_root(- value: serde_json::Value,+ mut value: serde_json::Value,schema: &serde_json::Value,root_schema: &serde_json::Value,) -> serde_json::Value {@@ -114,21 +114,51 @@ fn coerce_to_schema_with_root({return coerce_to_schema_with_root(value, non_null_schema, root_schema);}- // A union of shapes: container text goes to the first branch it parses as. Unlike the OpenAI boundary, which- // keeps the text when the union admits a string, a tool implementation deserializes what it declared, so the- // container branch wins here.- if let Json::String(text) = &value- && let Some(branches) = schema.get("anyOf").or_else(|| schema.get("oneOf")).and_then(Json::as_array)- {- for branch in branches {- let branch = resolve_local_schema(branch, root_schema);- let parses = match branch.get("type").and_then(Json::as_str) {- Some("object") => parse_lenient_json(text).is_some_and(|parsed| parsed.is_object()),- Some("array") => parse_lenient_json(text).is_some_and(|parsed| parsed.is_array()),- _ => false,- };- if parses {- return coerce_to_schema_with_root(value, branch, root_schema);+ if let Some(branches) = schema.get("anyOf").or_else(|| schema.get("oneOf")).and_then(Json::as_array) {+ let branches = branches.iter().map(|branch| resolve_local_schema(branch, root_schema)).collect::<Vec<_>>();+ // Container text still prefers its declared container shape over a string branch.+ // Native containers need the same traversal to coerce their nested fields.+ let parsed = value.as_str().and_then(parse_lenient_json);+ let container_type = match parsed.as_ref().unwrap_or(&value) {+ Json::Object(_) => Some("object"),+ Json::Array(_) => Some("array"),+ _ => None,+ };+ if let Some(container_type) = container_type {+ let mut matching =+ branches.iter().filter(|branch| branch.get("type").and_then(Json::as_str) == Some(container_type));+ if let Some(branch) = matching.next() {+ if matching.next().is_none() {+ return coerce_to_schema_with_root(value, branch, root_schema);+ }+ // Reading valid container JSON does not require choosing a+ // union branch. Keep its nested values unchanged when several+ // branches share this shape; serde can select the actual arm.+ if let Some(parsed) = parsed {+ value = parsed;+ }+ }+ }+ // Do not reinterpret scalar text if a branch permits strings or has an unknown shape.+ if value.is_string()+ && branches.iter().all(|branch| {+ matches!(+ branch.get("type").and_then(Json::as_str),+ Some("number" | "integer" | "boolean" | "object" | "array" | "null")+ )+ })+ {+ let mut converted = branches.iter().filter_map(|branch| {+ if !matches!(branch.get("type").and_then(Json::as_str), Some("number" | "integer" | "boolean")) {+ return None;+ }+ let converted = coerce_to_schema_with_root(value.clone(), branch, root_schema);+ (converted != value).then_some(converted)+ });+ if let Some(converted_value) = converted.next()+ && converted.next().is_none()+ {+ return converted_value;}}}@@ -246,6 +276,46 @@ fn resolve_local_schema<'a>(mod tests {use super::coerce_to_schema;+ #[derive(serde::Deserialize, schemars::JsonSchema)]+ #[serde(untagged)]+ enum Choice {+ First {+ first: u32,+ },+ Second {+ second: u32,+ },+ }++ #[crate::tool::uzu_tool_function]+ fn choose(request: Choice) -> u32 {+ match request {+ Choice::First {+ first,+ } => first,+ Choice::Second {+ second,+ } => second,+ }+ }++ #[tokio::test]+ async fn tool_accepts_markup_json_for_either_object_union_branch() {+ let tool: super::ToolDescriptor = choose.into();+ for request in [serde_json::json!({ "first": 1 }), serde_json::json!({ "second": 2 })] {+ let expected = request.as_object().unwrap().values().next().unwrap().clone();+ for argument in [request.clone(), serde_json::json!(request.to_string())] {+ let result = tool.execute(serde_json::json!({ "request": argument }).into()).await.unwrap();+ assert_eq!(serde_json::Value::try_from(result).unwrap(), expected);+ }+ }+ // Parsing the container must not guess which branch should coerce a+ // nested scalar, or make a malformed container acceptable.+ for request in [r#"{"first":"1"}"#, r#"{"first":1}}"#] {+ assert!(tool.execute(serde_json::json!({ "request": request }).into()).await.is_err());+ }+ }+#[test]fn coerce_to_schema_parses_container_text_and_keeps_string_text() {let schema = serde_json::json!({@@ -289,4 +359,72 @@ mod tests {coerce_to_schema(serde_json::json!({"child": "{\"n\": \"2\"}", "shape": "{\"r\": \"1.5\"}"}), &schema);assert_eq!(coerced, serde_json::json!({"child": {"n": 2}, "shape": {"r": 1.5}}));}++ #[test]+ fn coerce_to_schema_traverses_union_containers_and_scalar_strings() {+ for union in ["anyOf", "oneOf"] {+ let schema = serde_json::json!({+ "type": "array",+ "items": {(union): [{"type": "number"}, {"$ref": "#/$defs/Point"}]},+ "$defs": {"Point": {"type": "object", "properties": {+ "x": {"type": "number"}, "y": {"type": "number"}+ }}}+ });+ let coerced = coerce_to_schema(+ serde_json::json!(["97.42", {"x": "1", "y": "2"}, "{\"x\":\"3\",\"y\":\"4\"}"]),+ &schema,+ );+ assert_eq!(coerced, serde_json::json!([97.42, {"x": 1.0, "y": 2.0}, {"x": 3.0, "y": 4.0}]));++ let schema = serde_json::json!({+ (union): [{"type": "array", "items": {"type": "integer"}}, {"type": "boolean"}]+ });+ assert_eq!(coerce_to_schema(serde_json::json!(["1", "2"]), &schema), serde_json::json!([1, 2]));+ assert_eq!(coerce_to_schema(serde_json::json!("true"), &schema), serde_json::json!(true));+ }+ }++ #[test]+ fn coerce_to_schema_keeps_ambiguous_or_invalid_union_input() {+ for union in ["anyOf", "oneOf"] {+ for branches in [+ serde_json::json!([{"type": "number"}, {"type": "string"}]),+ serde_json::json!([{"type": "number"}, {"$ref": "#/$defs/Text"}]),+ serde_json::json!([{"type": "number"}, {"type": "integer"}]),+ serde_json::json!([{"type": "number"}, {"enum": ["other"]}]),+ ] {+ let schema = serde_json::json!({(union): branches, "$defs": {"Text": {"type": "string"}}});+ let input = serde_json::json!("2");+ assert_eq!(coerce_to_schema(input.clone(), &schema), input);+ }++ let schema = serde_json::json!({(union): [+ {"type": "object", "properties": {"x": {"type": "number"}}},+ {"type": "object", "properties": {"x": {"type": "integer"}}}+ ]});+ for input in [serde_json::json!({"x": "1"}), serde_json::json!("{\"x\":\"1\"}")] {+ assert_eq!(coerce_to_schema(input, &schema), serde_json::json!({"x": "1"}));+ }+ let schema = serde_json::json!({(union): [+ {"type": "array", "items": {"type": "number"}},+ {"type": "array", "items": {"type": "integer"}}+ ]});+ for input in [serde_json::json!(["1"]), serde_json::json!("[\"1\"]")] {+ assert_eq!(coerce_to_schema(input, &schema), serde_json::json!(["1"]));+ }++ let schema = serde_json::json!({(union): [+ {"type": "number"}, {"type": "object", "properties": {"x": {"type": "number"}}}+ ]});+ for input in [+ serde_json::json!("not a number"),+ serde_json::json!("NaN"),+ serde_json::json!("Infinity"),+ serde_json::json!({"x": "bad"}),+ serde_json::json!(null),+ ] {+ assert_eq!(coerce_to_schema(input.clone(), &schema), input);+ }+ }+ }}diff --git a/crates/legacy/shoji/src/traits/registry.rs b/crates/legacy/shoji/src/traits/registry.rsindex 2a7c29f41..86eb8846a 100644--- a/crates/legacy/shoji/src/traits/registry.rs+++ b/crates/legacy/shoji/src/traits/registry.rs@@ -1,4 +1,4 @@-use std::{error::Error, future::Future, pin::Pin};+use std::{error::Error, future::Future, pin::Pin, sync::Arc};use crate::types::model::{Model, ModelIdentifier};@@ -13,6 +13,20 @@ pub trait Registry: Send + Sync {Box::pin(async { Ok((self.models().await?, true)) })}+ /// A snapshot that can be read without waiting for network requests.+ /// `None` means no listing has loaded; an empty listing is still a snapshot.+ fn cached_listing(&self) -> Option<(Vec<Model>, bool)> {+ None+ }++ /// Refreshes the snapshot, notifying after intermediate results become readable.+ fn refresh_listing(+ &self,+ _on_update: Arc<dyn Fn() + Send + Sync>,+ ) -> Pin<Box<dyn Future<Output = Result<(Vec<Model>, bool), Self::Error>> + Send + '_>> {+ self.listing()+ }+fn model_by_identifier(&self,identifier: &ModelIdentifier,diff --git a/crates/legacy/shoji/src/types/session/chat/reply/config.rs b/crates/legacy/shoji/src/types/session/chat/reply/config.rsindex 6824b5c05..531e8d970 100644--- a/crates/legacy/shoji/src/types/session/chat/reply/config.rs+++ b/crates/legacy/shoji/src/types/session/chat/reply/config.rs@@ -9,7 +9,7 @@ pub struct ChatReplyConfig {pub sampling_policy: SamplingPolicy,pub grammar: Option<Grammar>,/// Maximum number of automatic tool-call turns per reply.- /// `None` falls back to the session default.+ /// `None` allows unlimited turns; `Some(0)` prevents automatic tool execution.pub tool_turn_limit: Option<u32>,}diff --git a/crates/legacy/uzu/bindings/python/uzu/__init__.pyi b/crates/legacy/uzu/bindings/python/uzu/__init__.pyiindex b4cebb0fb..3fad9fb92 100644--- a/crates/legacy/uzu/bindings/python/uzu/__init__.pyi+++ b/crates/legacy/uzu/bindings/python/uzu/__init__.pyi@@ -337,7 +337,7 @@ class ChatReplyConfig:def tool_turn_limit(self) -> typing.Optional[builtins.int]:r"""Maximum number of automatic tool-call turns per reply.- `None` falls back to the session default.+ `None` allows unlimited turns; `Some(0)` prevents automatic tool execution."""def __new__(cls, token_limit: typing.Optional[builtins.int], sampling_policy: SamplingPolicy, grammar: typing.Optional[Grammar], tool_turn_limit: typing.Optional[builtins.int]) -> ChatReplyConfig: ...@staticmethod@@ -507,6 +507,13 @@ class ChatSessionStreamChunk:def error(self) -> builtins.str: ...def __new__(cls, error: builtins.str) -> ChatSessionStreamChunk.Error: ...+ @typing.final+ class ToolResults(ChatSessionStreamChunk):+ __match_args__ = ("messages",)+ @property+ def messages(self) -> builtins.list[ChatMessage]: ...+ def __new__(cls, messages: typing.Sequence[ChatMessage]) -> ChatSessionStreamChunk.ToolResults: ...+...@typing.final@@ -645,6 +652,11 @@ class DownloadPhase:def message(self) -> builtins.str: ...def __new__(cls, message: builtins.str) -> DownloadPhase.Error: ...+ @typing.final+ class Initializing(DownloadPhase):+ __match_args__ = ()+ def __new__(cls) -> DownloadPhase.Initializing: ...+...@typing.finaldiff --git a/crates/legacy/uzu/bindings/swift/README.md b/crates/legacy/uzu/bindings/swift/README.mdindex 4a6e04f33..3f562d680 100644--- a/crates/legacy/uzu/bindings/swift/README.md+++ b/crates/legacy/uzu/bindings/swift/README.md@@ -110,6 +110,8 @@ public func runChat() async throws {print("Generated tokens: \(reply?.stats.tokensCountOutput ?? 0)")case .error(let error):print("Error: \(error)")+ case .toolResults:+ break}}print("Reasoning: \(message?.reasoning() ?? "empty")")diff --git a/crates/legacy/uzu/bindings/swift/Sources/Examples/Chat.swift b/crates/legacy/uzu/bindings/swift/Sources/Examples/Chat.swiftindex d2cfcce90..f0bb21c4e 100644--- a/crates/legacy/uzu/bindings/swift/Sources/Examples/Chat.swift+++ b/crates/legacy/uzu/bindings/swift/Sources/Examples/Chat.swift@@ -29,6 +29,8 @@ public func runChat() async throws {print("Generated tokens: \(reply?.stats.tokensCountOutput ?? 0)")case .error(let error):print("Error: \(error)")+ case .toolResults:+ break}}print("Reasoning: \(message?.reasoning() ?? "empty")")diff --git a/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/download_manager.swift b/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/download_manager.swiftindex d58420db1..a000b3ba6 100644--- a/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/download_manager.swift+++ b/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/download_manager.swift@@ -691,6 +691,7 @@ public enum DownloadPhase: Equatable, Hashable, Codable {)case error(message: String)+ case initializing@@ -726,6 +727,8 @@ public struct FfiConverterTypeDownloadPhase: FfiConverterRustBuffer {case 6: return .error(message: try FfiConverterString.read(from: &buf))+ case 7: return .initializing+default: throw UniffiInternalError.unexpectedEnumCase}}@@ -759,6 +762,10 @@ public struct FfiConverterTypeDownloadPhase: FfiConverterRustBuffer {writeInt(&buf, Int32(6))FfiConverterString.write(message, into: &buf)++ case .initializing:+ writeInt(&buf, Int32(7))+}}}diff --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 37afdf08a..3fe96ba56 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@@ -1810,6 +1810,8 @@ public enum ChatSessionStreamChunk: Equatable, Hashable, Codable {)case error(error: ChatSessionError)+ case toolResults(messages: [ChatMessage]+ )@@ -1837,6 +1839,9 @@ public struct FfiConverterTypeChatSessionStreamChunk: FfiConverterRustBuffer {case 2: return .error(error: try FfiConverterTypeChatSessionError.read(from: &buf))+ case 3: return .toolResults(messages: try FfiConverterSequenceTypeChatMessage.read(from: &buf)+ )+default: throw UniffiInternalError.unexpectedEnumCase}}@@ -1854,6 +1859,11 @@ public struct FfiConverterTypeChatSessionStreamChunk: FfiConverterRustBuffer {writeInt(&buf, Int32(2))FfiConverterTypeChatSessionError.write(error, into: &buf)++ case let .toolResults(messages):+ writeInt(&buf, Int32(3))+ FfiConverterSequenceTypeChatMessage.write(messages, into: &buf)+}}}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 5fc1fc62c..079faabc1 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@@ -1072,7 +1072,7 @@ public struct ChatReplyConfig: Equatable, Hashable, Codable {public var grammar: Grammar?/*** Maximum number of automatic tool-call turns per reply.- * `None` falls back to the session default.+ * `None` allows unlimited turns; `Some(0)` prevents automatic tool execution.*/public var toolTurnLimit: UInt32?@@ -1081,7 +1081,7 @@ public struct ChatReplyConfig: Equatable, Hashable, Codable {public init(tokenLimit: UInt32?, samplingPolicy: SamplingPolicy, grammar: Grammar?,/*** Maximum number of automatic tool-call turns per reply.- * `None` falls back to the session default.+ * `None` allows unlimited turns; `Some(0)` prevents automatic tool execution.*/toolTurnLimit: UInt32?) {self.tokenLimit = tokenLimitself.samplingPolicy = samplingPolicydiff --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 effafcf2c..e023ba256 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@@ -2290,6 +2290,8 @@ public enum EngineError: Swift.Error, Equatable, Hashable, Codable, Foundation.Lcase TextToSpeechSession(TextToSpeechSessionError)case SettingsNotAvailable+ case ModelConfig(message: String+ )@@ -2346,6 +2348,9 @@ public struct FfiConverterTypeEngineError: FfiConverterRustBuffer {try FfiConverterTypeTextToSpeechSessionError.read(from: &buf))case 11: return .SettingsNotAvailable+ case 12: return .ModelConfig(+ message: try FfiConverterString.read(from: &buf)+ )default: throw UniffiInternalError.unexpectedEnumCase}@@ -2409,6 +2414,11 @@ public struct FfiConverterTypeEngineError: FfiConverterRustBuffer {case .SettingsNotAvailable:writeInt(&buf, Int32(11))++ case let .ModelConfig(message):+ writeInt(&buf, Int32(12))+ FfiConverterString.write(message, into: &buf)+}}}@@ -2756,8 +2766,6 @@ public enum StorageError: Swift.Error, Equatable, Hashable, Codable, Foundation.)case DownloadManager(message: String)- case HashNotFound(identifier: String, name: String- )case ModelNotFound(identifier: String)case UnsupportedModel(identifier: String@@ -2797,14 +2805,10 @@ public struct FfiConverterTypeStorageError: FfiConverterRustBuffer {case 2: return .DownloadManager(message: try FfiConverterString.read(from: &buf))- case 3: return .HashNotFound(- identifier: try FfiConverterString.read(from: &buf),- name: try FfiConverterString.read(from: &buf)- )- case 4: return .ModelNotFound(+ case 3: return .ModelNotFound(identifier: try FfiConverterString.read(from: &buf))- case 5: return .UnsupportedModel(+ case 4: return .UnsupportedModel(identifier: try FfiConverterString.read(from: &buf))@@ -2829,19 +2833,13 @@ public struct FfiConverterTypeStorageError: FfiConverterRustBuffer {FfiConverterString.write(message, into: &buf)- case let .HashNotFound(identifier,name):- writeInt(&buf, Int32(3))- FfiConverterString.write(identifier, into: &buf)- FfiConverterString.write(name, into: &buf)--case let .ModelNotFound(identifier):- writeInt(&buf, Int32(4))+ writeInt(&buf, Int32(3))FfiConverterString.write(identifier, into: &buf)case let .UnsupportedModel(identifier):- writeInt(&buf, Int32(5))+ writeInt(&buf, Int32(4))FfiConverterString.write(identifier, into: &buf)}diff --git a/crates/legacy/uzu/bindings/typescript/src/napi/index.d.mts b/crates/legacy/uzu/bindings/typescript/src/napi/index.d.mtsindex 9e86c9e70..9b8ceb87f 100644--- a/crates/legacy/uzu/bindings/typescript/src/napi/index.d.mts+++ b/crates/legacy/uzu/bindings/typescript/src/napi/index.d.mts@@ -15,6 +15,11 @@ export declare class DownloadPhaseError {constructor(message: string)}+export declare class DownloadPhaseInitializing {++ constructor()+}+export declare class DownloadPhaseLocked {managerId: stringconstructor(managerId: string)@@ -48,7 +53,7 @@ export declare const enum DownloadManagerType {}export type DownloadPhase =- DownloadPhaseNotDownloaded | DownloadPhaseDownloading | DownloadPhasePaused | DownloadPhaseDownloaded | DownloadPhaseLocked | DownloadPhaseError+ DownloadPhaseNotDownloaded | DownloadPhaseDownloading | DownloadPhasePaused | DownloadPhaseDownloaded | DownloadPhaseLocked | DownloadPhaseError | DownloadPhaseInitializing/*** A loaded chat backend instance (model weights, tokenizer, configuration).* Cloning is cheap and shares the underlying instance, so multiple [`ChatSession`]s@@ -92,6 +97,11 @@ export declare class ChatSessionStreamChunkReplies {constructor(replies: Array<ChatReply>)}+export declare class ChatSessionStreamChunkToolResults {+ messages: Array<ChatMessage>+ constructor(messages: Array<ChatMessage>)+}+export declare class ClassificationSession {get state(): Promise<ClassificationSessionState>classify(input: Array<ClassificationMessage>): Promise<ClassificationOutput>@@ -145,7 +155,7 @@ export declare const enum ChatSessionState {}export type ChatSessionStreamChunk =- ChatSessionStreamChunkReplies | ChatSessionStreamChunkError+ ChatSessionStreamChunkReplies | ChatSessionStreamChunkError | ChatSessionStreamChunkToolResultsexport type ClassificationSessionError =| { type: 'Backend', message: string }@@ -325,7 +335,7 @@ export declare class ChatReplyConfig {grammar?: Grammar/*** Maximum number of automatic tool-call turns per reply.- * `None` falls back to the session default.+ * `None` allows unlimited turns; `Some(0)` prevents automatic tool execution.*/toolTurnLimit?: numberconstructor(tokenLimit?: number, samplingPolicy: SamplingPolicy, grammar?: Grammar, toolTurnLimit?: number)@@ -1030,6 +1040,7 @@ export type EngineError =| { type: 'ClassificationSession', field0: ClassificationSessionError }| { type: 'TextToSpeechSession', field0: TextToSpeechSessionError }| { type: 'SettingsNotAvailable' }+ | { type: 'ModelConfig', message: string }export type PlayerError =| { type: 'RodioError', message: string }@@ -1051,6 +1062,5 @@ export type SettingsError =export type StorageError =| { type: 'UnableToCreateDirectory', path: string }| { type: 'DownloadManager', message: string }- | { type: 'HashNotFound', identifier: ModelIdentifier, name: string }| { type: 'ModelNotFound', identifier: ModelIdentifier }| { type: 'UnsupportedModel', identifier: ModelIdentifier }diff --git a/crates/legacy/uzu/bindings/typescript/src/napi/index.d.ts b/crates/legacy/uzu/bindings/typescript/src/napi/index.d.tsindex 9e86c9e70..9b8ceb87f 100644--- a/crates/legacy/uzu/bindings/typescript/src/napi/index.d.ts+++ b/crates/legacy/uzu/bindings/typescript/src/napi/index.d.ts@@ -15,6 +15,11 @@ export declare class DownloadPhaseError {constructor(message: string)}+export declare class DownloadPhaseInitializing {++ constructor()+}+export declare class DownloadPhaseLocked {managerId: stringconstructor(managerId: string)@@ -48,7 +53,7 @@ export declare const enum DownloadManagerType {}export type DownloadPhase =- DownloadPhaseNotDownloaded | DownloadPhaseDownloading | DownloadPhasePaused | DownloadPhaseDownloaded | DownloadPhaseLocked | DownloadPhaseError+ DownloadPhaseNotDownloaded | DownloadPhaseDownloading | DownloadPhasePaused | DownloadPhaseDownloaded | DownloadPhaseLocked | DownloadPhaseError | DownloadPhaseInitializing/*** A loaded chat backend instance (model weights, tokenizer, configuration).* Cloning is cheap and shares the underlying instance, so multiple [`ChatSession`]s@@ -92,6 +97,11 @@ export declare class ChatSessionStreamChunkReplies {constructor(replies: Array<ChatReply>)}+export declare class ChatSessionStreamChunkToolResults {+ messages: Array<ChatMessage>+ constructor(messages: Array<ChatMessage>)+}+export declare class ClassificationSession {get state(): Promise<ClassificationSessionState>classify(input: Array<ClassificationMessage>): Promise<ClassificationOutput>@@ -145,7 +155,7 @@ export declare const enum ChatSessionState {}export type ChatSessionStreamChunk =- ChatSessionStreamChunkReplies | ChatSessionStreamChunkError+ ChatSessionStreamChunkReplies | ChatSessionStreamChunkError | ChatSessionStreamChunkToolResultsexport type ClassificationSessionError =| { type: 'Backend', message: string }@@ -325,7 +335,7 @@ export declare class ChatReplyConfig {grammar?: Grammar/*** Maximum number of automatic tool-call turns per reply.- * `None` falls back to the session default.+ * `None` allows unlimited turns; `Some(0)` prevents automatic tool execution.*/toolTurnLimit?: numberconstructor(tokenLimit?: number, samplingPolicy: SamplingPolicy, grammar?: Grammar, toolTurnLimit?: number)@@ -1030,6 +1040,7 @@ export type EngineError =| { type: 'ClassificationSession', field0: ClassificationSessionError }| { type: 'TextToSpeechSession', field0: TextToSpeechSessionError }| { type: 'SettingsNotAvailable' }+ | { type: 'ModelConfig', message: string }export type PlayerError =| { type: 'RodioError', message: string }@@ -1051,6 +1062,5 @@ export type SettingsError =export type StorageError =| { type: 'UnableToCreateDirectory', path: string }| { type: 'DownloadManager', message: string }- | { type: 'HashNotFound', identifier: ModelIdentifier, name: string }| { type: 'ModelNotFound', identifier: ModelIdentifier }| { type: 'UnsupportedModel', identifier: ModelIdentifier }diff --git a/crates/legacy/uzu/bindings/typescript/src/napi/index.js b/crates/legacy/uzu/bindings/typescript/src/napi/index.jsindex 323da747b..2853feea3 100644--- a/crates/legacy/uzu/bindings/typescript/src/napi/index.js+++ b/crates/legacy/uzu/bindings/typescript/src/napi/index.js@@ -511,6 +511,7 @@ module.exports = nativeBindingmodule.exports.DownloadPhaseDownloaded = nativeBinding.DownloadPhaseDownloadedmodule.exports.DownloadPhaseDownloading = nativeBinding.DownloadPhaseDownloadingmodule.exports.DownloadPhaseError = nativeBinding.DownloadPhaseError+module.exports.DownloadPhaseInitializing = nativeBinding.DownloadPhaseInitializingmodule.exports.DownloadPhaseLocked = nativeBinding.DownloadPhaseLockedmodule.exports.DownloadPhaseNotDownloaded = nativeBinding.DownloadPhaseNotDownloadedmodule.exports.DownloadPhasePaused = nativeBinding.DownloadPhasePaused@@ -521,6 +522,7 @@ module.exports.ChatSession = nativeBinding.ChatSessionmodule.exports.ChatSessionStream = nativeBinding.ChatSessionStreammodule.exports.ChatSessionStreamChunkError = nativeBinding.ChatSessionStreamChunkErrormodule.exports.ChatSessionStreamChunkReplies = nativeBinding.ChatSessionStreamChunkReplies+module.exports.ChatSessionStreamChunkToolResults = nativeBinding.ChatSessionStreamChunkToolResultsmodule.exports.ClassificationSession = nativeBinding.ClassificationSessionmodule.exports.NativeTool = nativeBinding.NativeToolmodule.exports.TextToSpeechSession = nativeBinding.TextToSpeechSessiondiff --git a/crates/legacy/uzu/bindings/typescript/src/napi/index.mjs b/crates/legacy/uzu/bindings/typescript/src/napi/index.mjsindex 18e55d21f..7f4fb5da0 100644--- a/crates/legacy/uzu/bindings/typescript/src/napi/index.mjs+++ b/crates/legacy/uzu/bindings/typescript/src/napi/index.mjs@@ -8,6 +8,7 @@ export default cjs;export const DownloadPhaseDownloaded = cjs.DownloadPhaseDownloaded;export const DownloadPhaseDownloading = cjs.DownloadPhaseDownloading;export const DownloadPhaseError = cjs.DownloadPhaseError;+export const DownloadPhaseInitializing = cjs.DownloadPhaseInitializing;export const DownloadPhaseLocked = cjs.DownloadPhaseLocked;export const DownloadPhaseNotDownloaded = cjs.DownloadPhaseNotDownloaded;export const DownloadPhasePaused = cjs.DownloadPhasePaused;@@ -18,6 +19,7 @@ export const ChatSession = cjs.ChatSession;export const ChatSessionStream = cjs.ChatSessionStream;export const ChatSessionStreamChunkError = cjs.ChatSessionStreamChunkError;export const ChatSessionStreamChunkReplies = cjs.ChatSessionStreamChunkReplies;+export const ChatSessionStreamChunkToolResults = cjs.ChatSessionStreamChunkToolResults;export const ClassificationSession = cjs.ClassificationSession;export const NativeTool = cjs.NativeTool;export const TextToSpeechSession = cjs.TextToSpeechSession;diff --git a/crates/legacy/uzu/examples/chat.rs b/crates/legacy/uzu/examples/chat.rsindex 3fafb4441..2546d71b0 100644--- a/crates/legacy/uzu/examples/chat.rs+++ b/crates/legacy/uzu/examples/chat.rs@@ -28,6 +28,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {let mut last_message: Option<ChatMessage> = None;while let Some(chunk) = stream.next().await {match chunk {+ ChatSessionStreamChunk::ToolResults {+ ..+ } => {},ChatSessionStreamChunk::Replies {replies,} => {diff --git a/crates/legacy/uzu/src/device/mod.rs b/crates/legacy/uzu/src/device/mod.rsindex 3989e159d..8e6c0ee57 100644--- a/crates/legacy/uzu/src/device/mod.rs+++ b/crates/legacy/uzu/src/device/mod.rs@@ -2,7 +2,7 @@ mod error;mod os;pub use error::DeviceError;-pub(crate) use os::{home_path, is_environment_sandboxed};+pub(crate) use os::home_path;use serde::{Deserialize, Serialize};use sysinfo::System;diff --git a/crates/legacy/uzu/src/device/os.rs b/crates/legacy/uzu/src/device/os.rsindex a6217d2cc..2ffba6db8 100644--- a/crates/legacy/uzu/src/device/os.rs+++ b/crates/legacy/uzu/src/device/os.rs@@ -26,16 +26,3 @@ pub fn home_path() -> Option<PathBuf> {}dirs::home_dir()}--#[allow(unreachable_code)]-pub fn is_environment_sandboxed() -> bool {- #[cfg(target_os = "ios")]- {- return true;- }- #[cfg(target_os = "macos")]- {- return std::env::var_os("APP_SANDBOX_CONTAINER_ID").is_some();- }- false-}diff --git a/crates/legacy/uzu/src/engine/catalog_tests.rs b/crates/legacy/uzu/src/engine/catalog_tests.rsnew file mode 100644index 000000000..cb0d29852--- /dev/null+++ b/crates/legacy/uzu/src/engine/catalog_tests.rs@@ -0,0 +1,302 @@+use std::{future::Future, pin::Pin, sync::Mutex, time::Duration};++use shoji::types::{basic::Repository, model::ModelAccessibility};+use tempfile::TempDir;+use tokio::sync::Notify;++use super::*;+use crate::storage::DownloadManagerType;++type RegistryResult = Result<Vec<Model>, RegistryError>;++struct ControlledRegistry {+ identifier: &'static str,+ cached: Option<Vec<Model>>,+ started: Arc<Notify>,+ result: Mutex<Option<oneshot::Receiver<RegistryResult>>>,+}++impl Registry for ControlledRegistry {+ type Error = RegistryError;++ fn identifier(&self) -> String {+ self.identifier.to_string()+ }++ fn cached_listing(&self) -> Option<(Vec<Model>, bool)> {+ self.cached.clone().map(|models| (models, true))+ }++ fn models(&self) -> Pin<Box<dyn Future<Output = RegistryResult> + Send + '_>> {+ Box::pin(async {+ let result = self.result.lock().unwrap().take().expect("registry fetched more than once");+ self.started.notify_one();+ result.await.expect("test dropped registry result")+ })+ }+}++fn registry(+ identifier: &'static str,+ cached: Option<Vec<Model>>,+) -> (Box<dyn Registry<Error = RegistryError>>, oneshot::Sender<RegistryResult>, Arc<Notify>) {+ let (sender, result) = oneshot::channel();+ let started = Arc::new(Notify::new());+ (+ Box::new(ControlledRegistry {+ identifier,+ cached,+ started: started.clone(),+ result: Mutex::new(Some(result)),+ }),+ sender,+ started,+ )+}++fn model(identifier: &str) -> Model {+ Model::external(+ identifier.to_string(),+ "test".to_string(),+ "Test".to_string(),+ "test".to_string(),+ "Test".to_string(),+ "1".to_string(),+ vec![],+ ModelAccessibility::Remote {+ repository: None,+ },+ None,+ )+}++fn model_with_repo(+ identifier: &str,+ repo_id: &str,+) -> Model {+ Model {+ accessibility: ModelAccessibility::Remote {+ repository: Some(Repository {+ identifier: repo_id.to_string(),+ commit_hash: None,+ paths: None,+ }),+ },+ ..model(identifier)+ }+}++async fn engine() -> (Engine, TempDir) {+ let directory = tempfile::tempdir().unwrap();+ let config = StorageConfig::new(+ Device::new().unwrap(),+ Some(directory.path().to_path_buf()),+ "catalog-test".to_string(),+ DownloadManagerType::Universal,+ "https://example.invalid".to_string(),+ None,+ );+ let storage = Arc::new(Storage::new(RuntimeHandle::current(), config).await.unwrap());+ (+ Engine {+ settings: SharedAccess::new(None),+ registry: SharedAccess::new(MergedRegistry::new(vec![])),+ storage,+ backends: SharedAccess::new(HashMap::new()),+ callback: SharedAccess::new(None),+ catalog_events: broadcast::channel(64).0,+ catalog_refreshes: Arc::new(AtomicUsize::new(0)),+ catalog_publish: Arc::new(tokio::sync::Mutex::new(())),+ published_catalog: Arc::new(tokio::sync::Mutex::new((Vec::new(), true))),+ },+ directory,+ )+}++#[tokio::test]+async fn a_stalled_registry_does_not_delay_a_healthy_registry() {+ let (engine, _directory) = engine().await;+ let (slow, slow_result, started) = registry("slow", None);+ let slow_ready = engine.start_registry(slow).await.unwrap();+ started.notified().await;++ let (healthy, healthy_result, _) = registry("healthy", None);+ healthy_result.send(Ok(vec![model("available")])).unwrap();+ tokio::time::timeout(Duration::from_secs(1), engine.add_registry(healthy)).await.unwrap().unwrap();+ assert_eq!(engine.models().await.unwrap(), vec![model("available")]);+ assert_eq!(engine.catalog_refreshes.load(Ordering::SeqCst), 1);++ slow_result+ .send(Err(RegistryError::UnableToGetModels {+ message: "offline".to_string(),+ }))+ .unwrap();+ // It may have signaled readiness from the healthy aggregate before its own error.+ let _ = tokio::time::timeout(Duration::from_secs(1), slow_ready).await.unwrap().unwrap();+ let mut events = engine.catalog_subscribe();+ while engine.catalog_refreshes.load(Ordering::SeqCst) != 0 {+ tokio::time::timeout(Duration::from_secs(1), events.next()).await.unwrap();+ }+ assert_eq!(engine.models().await.unwrap(), vec![model("available")]);+}++#[tokio::test]+async fn cached_models_and_lookups_remain_available_during_refresh() {+ let (engine, _directory) = engine().await;+ let (cached, result, started) = registry("cached", Some(vec![model("existing")]));+ let ready = engine.start_registry(cached).await.unwrap();+ started.notified().await;++ assert_eq!(+ tokio::time::timeout(Duration::from_secs(1), engine.models()).await.unwrap().unwrap(),+ vec![model("existing")]+ );+ let found =+ tokio::time::timeout(Duration::from_secs(1), engine.model("existing".to_string())).await.unwrap().unwrap();+ assert_eq!(found, Some(model("existing")));++ result.send(Ok(vec![model("existing"), model("new")])).unwrap();+ ready.await.unwrap().unwrap();+ let mut events = engine.catalog_subscribe();+ while engine.catalog_refreshes.load(Ordering::SeqCst) != 0 {+ tokio::time::timeout(Duration::from_secs(1), events.next()).await.unwrap();+ }+ assert_eq!(engine.models().await.unwrap(), vec![model("existing"), model("new")]);+}++#[tokio::test]+async fn missing_model_lookup_waits_for_its_registry_and_finishes_when_refresh_ends() {+ for result in [+ Ok(vec![model("later")]),+ Ok(vec![]),+ Err(RegistryError::UnableToGetModels {+ message: "offline".to_string(),+ }),+ ] {+ let (engine, _directory) = engine().await;+ let (pending, sender, started) = registry("pending", None);+ let ready = engine.start_registry(pending).await.unwrap();+ started.notified().await;+ let lookup = tokio::spawn({+ let engine = engine.clone();+ async move { engine.model("later".to_string()).await }+ });+ tokio::task::yield_now().await;+ assert!(!lookup.is_finished());++ let expected = result.as_ref().ok().and_then(|models| models.first()).cloned();+ sender.send(result).unwrap();+ let found = tokio::time::timeout(Duration::from_secs(1), lookup).await.unwrap().unwrap().unwrap();+ assert_eq!(found, expected);+ let _ = ready.await.unwrap();+ assert_eq!(engine.catalog_refreshes.load(Ordering::SeqCst), 0);+ }+}++#[tokio::test]+async fn exact_lookups_wait_for_discovery_and_finish_after_empty_or_failed_refreshes() {+ for by_repo_id in [false, true] {+ for result in [+ Ok(vec![model_with_repo("later", "org/later")]),+ Ok(vec![]),+ Err(RegistryError::UnableToGetModels {+ message: "offline".to_string(),+ }),+ ] {+ let (engine, _directory) = engine().await;+ let (pending, sender, started) = registry("pending", None);+ let ready = engine.start_registry(pending).await.unwrap();+ started.notified().await;+ let lookup = tokio::spawn({+ let engine = engine.clone();+ async move {+ if by_repo_id {+ engine.model_by_repo_id("org/later".into()).await+ } else {+ engine.model_by_identifier("later".into()).await+ }+ }+ });+ tokio::task::yield_now().await;+ assert!(!lookup.is_finished(), "exact lookup must wait for the pending registry");++ let expected = result.as_ref().ok().and_then(|models| models.first()).cloned();+ sender.send(result).unwrap();+ let found = tokio::time::timeout(Duration::from_secs(1), lookup).await.unwrap().unwrap().unwrap();+ assert_eq!(found, expected);+ let _ = ready.await.unwrap();+ }+ }+}++#[tokio::test]+async fn exact_lookups_return_cached_hits_without_confusing_identifiers_and_repo_ids() {+ let (engine, _directory) = engine().await;+ let models = vec![model_with_repo("first", "second"), model_with_repo("second", "third")];+ let (cached, result, started) = registry("cached", Some(models.clone()));+ let ready = engine.start_registry(cached).await.unwrap();+ started.notified().await;++ assert_eq!(+ tokio::time::timeout(Duration::from_secs(1), engine.model_by_identifier("second".into()))+ .await+ .unwrap()+ .unwrap(),+ Some(models[1].clone()),+ );+ assert_eq!(+ tokio::time::timeout(Duration::from_secs(1), engine.model_by_repo_id("second".into())).await.unwrap().unwrap(),+ Some(models[0].clone()),+ );+ result.send(Ok(models)).unwrap();+ ready.await.unwrap().unwrap();+}++#[tokio::test]+async fn discovered_downloadable_models_have_storage_entries_when_returned() {+ let (engine, _directory) = engine().await;+ let (pending, sender, started) = registry("pending", Some(vec![model("existing")]));+ let ready = engine.start_registry(pending).await.unwrap();+ started.notified().await;+ ready.await.unwrap().unwrap();+ // A previous publication can hold this lock while the registry finishes.+ let publication = engine.catalog_publish.lock().await;+ let expected = Model {+ accessibility: ModelAccessibility::OnDevice {+ source: shoji::types::model::ModelSource::Registry {+ toolchain_version: "1".into(),+ repository: None,+ source_repository: None,+ files: vec![],+ },+ },+ ..model("downloadable")+ };+ sender.send(Ok(vec![model("existing"), expected.clone()])).unwrap();+ tokio::time::timeout(Duration::from_secs(1), async {+ while engine.registry.lock().await.cached_listing().unwrap_or_default().0.len() != 2 {+ tokio::task::yield_now().await;+ }+ })+ .await+ .unwrap();+ assert_eq!(+ tokio::time::timeout(Duration::from_secs(1), engine.model_by_identifier("existing".into()))+ .await+ .unwrap()+ .unwrap(),+ Some(model("existing")),+ );+ let lookup = tokio::spawn({+ let engine = engine.clone();+ async move { engine.model_by_identifier("downloadable".into()).await }+ });+ tokio::task::yield_now().await;+ assert!(!lookup.is_finished(), "lookup must wait for the model's storage registration");+ assert!(engine.catalog_is_refreshing(), "refresh remains active until publication completes");+ drop(publication);+ let found = tokio::time::timeout(Duration::from_secs(1), lookup).await.unwrap().unwrap().unwrap().unwrap();+ assert_eq!(found, expected);+ assert!(engine.download_state(&found).await.is_some(), "lookup returned a model before its storage entry existed");+ assert!(!engine.catalog_is_refreshing());+}diff --git a/crates/legacy/uzu/src/engine/error.rs b/crates/legacy/uzu/src/engine/error.rsindex 51175fd74..453038811 100644--- a/crates/legacy/uzu/src/engine/error.rs+++ b/crates/legacy/uzu/src/engine/error.rs@@ -26,4 +26,8 @@ pub enum EngineError {TextToSpeechSession(#[from] nagare::text_to_speech::TextToSpeechSessionError),#[error("Settings not available")]SettingsNotAvailable,+ #[error("Unable to read model configuration: {message}")]+ ModelConfig {+ message: String,+ },}diff --git a/crates/legacy/uzu/src/engine/mod.rs b/crates/legacy/uzu/src/engine/mod.rsindex 7671a668c..fc0bea215 100644--- a/crates/legacy/uzu/src/engine/mod.rs+++ b/crates/legacy/uzu/src/engine/mod.rs@@ -1,12 +1,22 @@pub mod bridge;mod callback;+#[cfg(test)]+mod catalog_tests;pub mod config;mod downloader;mod downloader_stream;mod error;+mod sampling;mod shorthand;-use std::{collections::HashMap, path::Path, sync::Arc};+use std::{+ collections::HashMap,+ path::Path,+ sync::{+ Arc,+ atomic::{AtomicUsize, Ordering},+ },+};use backend_remote::openai::Backend as OpenAIBackend;pub use callback::{EngineCallback, EngineCallbackType};@@ -14,12 +24,12 @@ pub use config::EngineConfig;pub use downloader::Downloader;pub use downloader_stream::DownloaderStream;pub use error::EngineError;+use futures_util::stream::FuturesUnordered;use indexmap::IndexSet;use kiban::rt::RuntimeHandle;use nagare::{chat::{ChatInstance, ChatSession},classification::ClassificationSession,- telemetry::{Telemetry, TelemetryContext, TelemetryDevice, TelemetryEvent},text_to_speech::TextToSpeechSession,};use shoji::{@@ -30,6 +40,7 @@ use shoji::{},};use sysinfo::System;+use tokio::sync::{broadcast, oneshot, watch};use tokio_stream::{StreamExt, wrappers::BroadcastStream};use crate::{@@ -40,7 +51,7 @@ use crate::{registry::{CachedRegistry, MergedRegistry, RegistryError,local::{Config as LocalRegistryConfig, Registry as LocalRegistry},- mirai::{Backend as MiraiBackend, HUGGING_FACE_URL, Registry as MiraiRegistry, TELEMETRY_URL},+ mirai::{Backend as MiraiBackend, HUGGING_FACE_URL, Registry as MiraiRegistry},openai::{Config as OpenAIConfig, Registry as OpenAIRegistry},unique_model,},@@ -56,7 +67,10 @@ pub struct Engine {storage: Arc<Storage>,backends: SharedAccess<HashMap<String, Arc<dyn Backend>>>,callback: SharedAccess<Option<Arc<EngineCallback>>>,- telemetry: SharedAccess<Telemetry>,+ catalog_events: broadcast::Sender<()>,+ catalog_refreshes: Arc<AtomicUsize>,+ catalog_publish: Arc<tokio::sync::Mutex<()>>,+ published_catalog: Arc<tokio::sync::Mutex<(Vec<Model>, bool)>>,}impl Engine {@@ -77,20 +91,6 @@ impl Engine {let device = Device::new()?;- let telemetry = SharedAccess::new({- let context = TelemetryContext::new(- Self::version(),- Self::toolchain_version(),- TelemetryDevice {- os_name: device.os_name.clone(),- cpu_name: device.cpu_name.clone(),- memory_total: device.memory_total,- is_environment_sandboxed: crate::device::is_environment_sandboxed(),- },- );- Telemetry::builder().base_url(TELEMETRY_URL).context(context).build()- });-let registry = SharedAccess::new(MergedRegistry::new(vec![]));let huggingface_api_key = config.huggingface_api_key.map(BearerToken::from);let storage_config = StorageConfig::new(@@ -111,9 +111,13 @@ impl Engine {registry,backends: SharedAccess::new(HashMap::new()),callback: SharedAccess::new(None),- telemetry,+ catalog_events: broadcast::channel(64).0,+ catalog_refreshes: Arc::new(AtomicUsize::new(0)),+ catalog_publish: Arc::new(tokio::sync::Mutex::new(())),+ published_catalog: Arc::new(tokio::sync::Mutex::new((Vec::new(), true))),};- engine.spawn_storage_listener().await;+ engine.spawn_storage_listener();+ let mut initializations = FuturesUnordered::new();{let uzu_backend = UzuLlmBackend::new();@@ -134,7 +138,7 @@ impl Engine {);engine.add_backend(Arc::new(uzu_backend) as Arc<dyn Backend>).await;- engine.add_registry(mirai_registry).await?;+ initializations.push(engine.start_registry(mirai_registry).await?);if let Some(local_path) = config.local_path {let local_registry = LocalRegistry::new(LocalRegistryConfig::new(@@ -142,7 +146,7 @@ impl Engine {uzu_backend_version.clone(),local_path,))?;- engine.add_registry(Box::new(local_registry)).await?;+ initializations.push(engine.start_registry(Box::new(local_registry)).await?);}}@@ -180,10 +184,27 @@ impl Engine {for config in openai_configs {let registry = OpenAIRegistry::new(config.clone())?;let backend = OpenAIBackend::new(config.into()).map_err(|_| EngineError::UnableToCreateBackend {})?;- engine.add_registry(Box::new(registry)).await?;+ initializations.push(engine.start_registry(Box::new(registry)).await?);engine.add_backend(Arc::new(backend) as Arc<dyn Backend>).await;}+ let mut last_error = None;+ while engine.catalog_snapshot().await?.0.is_empty() {+ let Some(result) = initializations.next().await else {+ if let Some(error) = last_error {+ return Err(error);+ }+ break;+ };+ if let Err(error) = result+ .map_err(|error| EngineError::TokioError {+ message: error.to_string(),+ })+ .and_then(|result| result)+ {+ last_error = Some(error);+ }+ }Ok(engine)}@@ -221,11 +242,68 @@ impl Engine {&self,registry: Box<dyn Registry<Error = RegistryError>>,) -> Result<(), EngineError> {- self.registry.lock().await.add(Box::new(CachedRegistry::new(registry)))?;- self.handle_registry_refresh().await?;+ let first_update = self.start_registry(registry).await?;+ if self.catalog_snapshot().await?.0.is_empty() {+ first_update.await.map_err(|error| EngineError::TokioError {+ message: error.to_string(),+ })??;+ }Ok(())}+ async fn start_registry(+ &self,+ registry: Box<dyn Registry<Error = RegistryError>>,+ ) -> Result<oneshot::Receiver<Result<(), EngineError>>, EngineError> {+ let registry: Arc<dyn Registry<Error = RegistryError>> = Arc::new(CachedRegistry::new(registry));+ self.registry.lock().await.add(Arc::clone(®istry))?;+ self.start_registry_refresh(registry).await+ }++ async fn start_registry_refresh(+ &self,+ registry: Arc<dyn Registry<Error = RegistryError>>,+ ) -> Result<oneshot::Receiver<Result<(), EngineError>>, EngineError> {+ self.catalog_refreshes.fetch_add(1, Ordering::SeqCst);+ self.handle_registry_refresh(false).await?;+ let (ready, first_update) = oneshot::channel();+ let engine = self.clone();+ kiban::rt::spawn(async move {+ let (updates, mut updated) = watch::channel(());+ let publisher = updates.clone();+ let on_update: Arc<dyn Fn() + Send + Sync> = Arc::new(move || {+ publisher.send_replace(());+ });+ let refresh = registry.refresh_listing(on_update);+ tokio::pin!(refresh);+ let mut ready = Some(ready);+ loop {+ tokio::select! {+ result = &mut refresh => {+ let published = engine.handle_registry_refresh(true).await;+ if let Err(error) = &result {+ tracing::warn!(%error, registry = %registry.identifier(), "catalog refresh failed");+ }+ if let Some(ready) = ready.take() {+ let _ = ready.send(result.map(|_| ()).map_err(EngineError::from).and(published));+ }+ break;+ },+ update = updated.changed() => {+ if update.is_err() { break; }+ if let Err(error) = engine.handle_registry_refresh(false).await {+ tracing::warn!(%error, "unable to publish catalog update");+ }+ if ready.is_some() && engine.catalog_snapshot().await.is_ok_and(|(models, _)| !models.is_empty()) {+ let _ = ready.take().unwrap().send(Ok(()));+ }+ },+ }+ }+ });+ Ok(first_update)+ }+pub async fn add_backend(&self,backend: Arc<dyn Backend>,@@ -242,7 +320,7 @@ impl Engine {registry_identifier: String,) -> Result<(), EngineError> {self.registry.lock().await.remove(®istry_identifier)?;- self.handle_registry_refresh().await?;+ self.handle_registry_refresh(false).await?;Ok(())}@@ -259,7 +337,7 @@ impl Engine {impl Engine {#[bindings::export(Method(Getter))]pub async fn models(&self) -> Result<Vec<Model>, EngineError> {- self.registry.lock().await.models().await.map_err(EngineError::from)+ Ok(self.catalog_snapshot().await?.0)}#[bindings::export(Method(Getter))]@@ -370,6 +448,28 @@ impl Engine {}}+impl Engine {+ async fn find_catalog_model(+ &self,+ reference: &str,+ matches: impl Fn(&Model) -> bool,+ ) -> Result<Option<Model>, EngineError> {+ let mut updates = self.catalog_subscribe();+ loop {+ // Observe completion before taking the snapshot so a refresh that+ // finishes during the read cannot make a partial snapshot final.+ let refreshing = self.catalog_is_refreshing();+ let model = unique_model(reference, self.models().await?.into_iter().filter(&matches))?;+ if model.is_some() || !refreshing {+ return Ok(model);+ }+ if updates.next().await.is_none() {+ return Ok(None);+ }+ }+ }+}+#[bindings::export(Implementation)]impl Engine {#[bindings::export(Method)]@@ -377,8 +477,14 @@ impl Engine {&self,identifier: String,) -> Result<Option<Model>, EngineError> {- let registered = self.registry.lock().await.model(&identifier).await?;- if registered.is_some() && Path::new(&identifier).is_dir() {+ let is_directory = Path::new(&identifier).is_dir();+ let matches = |model: &Model| model.identifier == identifier || model.repo_ids().contains(&identifier);+ let registered = if is_directory {+ unique_model(&identifier, self.models().await?.into_iter().filter(matches))?+ } else {+ self.find_catalog_model(&identifier, matches).await?+ };+ if registered.is_some() && is_directory {return Err(RegistryError::UnableToGetModels {message: format!("Ambiguous model reference `{identifier}`: matches both a registered model and a directory"@@ -402,7 +508,7 @@ impl Engine {&self,identifier: ModelIdentifier,) -> Result<Option<Model>, EngineError> {- self.registry.lock().await.model_by_identifier(&identifier).await.map_err(EngineError::from)+ self.find_catalog_model(&identifier, |model| model.identifier == identifier).await}#[bindings::export(Method)]@@ -410,7 +516,7 @@ impl Engine {&self,repo_id: String,) -> Result<Option<Model>, EngineError> {- self.registry.lock().await.model_by_repo_id(&repo_id).await.map_err(EngineError::from)+ self.find_catalog_model(&repo_id, |model| model.repo_ids().contains(&repo_id)).await}#[bindings::export(Method)]@@ -421,6 +527,11 @@ impl Engine {let models = self.models().await?;let mut matches = Vec::new();for model in models {+ let candidate =+ model.filesystem_path().map(std::path::PathBuf::from).or_else(|| self.storage.cache_model_path(&model));+ if candidate.as_deref() != Some(Path::new(&path)) {+ continue;+ }if self.model_path(&model).await.is_some_and(|model_path| model_path == path) {matches.push(model);}@@ -451,7 +562,7 @@ impl Engine {if let Some(filesystem_path) = model.filesystem_path() {return Some(filesystem_path);}- let state = self.storage.state(&model.identifier).await.ok()?;+ let state = self.storage.ready_state(&model.identifier).await.ok()?;if !matches!(state.phase, DownloadPhase::Downloaded {}) {return None;}@@ -528,7 +639,7 @@ impl Engine {&self,instance: &ChatInstance,) -> Result<ChatSession, EngineError> {- let session = ChatSession::with_instance(instance, self.telemetry.lock().await.clone()).await?;+ let session = ChatSession::with_instance(instance).await?;Ok(session)}@@ -578,42 +689,71 @@ impl Engine {self.storage.subscribe()}- async fn handle_registry_refresh(&self) -> Result<(), EngineError> {- let (models, complete) = self.registry.lock().await.listing().await?;- self.storage.refresh(&models, complete).await?;+ pub async fn ready_download_state(+ &self,+ model: &Model,+ ) -> Result<DownloadState, EngineError> {+ Ok(self.storage.ready_state(&model.identifier).await?)+ }++ pub fn catalog_is_refreshing(&self) -> bool {+ self.catalog_refreshes.load(Ordering::SeqCst) != 0+ }++ pub async fn refresh_catalog(&self) -> Result<(), EngineError> {+ if !self.catalog_is_refreshing() {+ let registry = self.registry.lock().await.clone();+ let _ready = self.start_registry_refresh(Arc::new(registry)).await?;+ }+ Ok(())+ }++ pub fn catalog_subscribe(&self) -> BroadcastStream<()> {+ BroadcastStream::new(self.catalog_events.subscribe())+ }++ pub async fn catalog_snapshot(&self) -> Result<(Vec<Model>, bool), EngineError> {+ let (models, complete) = self.published_catalog.lock().await.clone();+ Ok((models, complete && self.catalog_refreshes.load(Ordering::SeqCst) == 0))+ }++ async fn handle_registry_refresh(+ &self,+ finished_refresh: bool,+ ) -> Result<(), EngineError> {+ let _publish = self.catalog_publish.lock().await;+ let registry = self.registry.lock().await.clone();+ let (models, complete) = registry.cached_listing().unwrap_or_default();+ let complete = complete && self.catalog_refreshes.load(Ordering::SeqCst) == usize::from(finished_refresh);+ // Register storage entries before exposing their descriptors. Individual+ // download verification continues asynchronously after registration.+ let result = self.storage.refresh(&models, complete).await;+ let mut published = self.published_catalog.lock().await;+ if result.is_ok() {+ *published = (models, complete);+ }+ // Completion and its snapshot become visible together: a lookup that+ // sees no active refresh must also read the final published snapshot.+ if finished_refresh {+ self.catalog_refreshes.fetch_sub(1, Ordering::SeqCst);+ }+ drop(published);+ let _ = self.catalog_events.send(());+ result?;if let Some(callback) = self.callback.lock().await.as_ref().cloned() {callback.on_event();};Ok(())}- async fn spawn_storage_listener(&self) {+ fn spawn_storage_listener(&self) {let mut stream = self.storage_subscribe();let callback = self.callback.clone();- let telemetry = self.telemetry.lock().await.clone();kiban::rt::spawn(async move {- let mut last_phase: HashMap<ModelIdentifier, DownloadPhase> = HashMap::new();while let Some(update) = stream.next().await {- let Ok((id, state)) = update else {+ let Ok(_) = update else {continue;};- let previous = last_phase.insert(id.clone(), state.phase.clone());- let event = match (&previous, &state.phase) {- (prev, DownloadPhase::Downloading {}) if !matches!(prev, Some(DownloadPhase::Downloading {})) => {- Some(TelemetryEvent::ModelDownloadStarted {- model_id: id,- })- },- (Some(DownloadPhase::Downloading {}), DownloadPhase::Downloaded {}) => {- Some(TelemetryEvent::ModelDownloadFinished {- model_id: id,- })- },- _ => None,- };- if let Some(event) = event {- telemetry.report(event);- }if let Some(callback) = callback.lock().await.as_ref().cloned() {callback.on_event();};diff --git a/crates/legacy/uzu/src/engine/sampling.rs b/crates/legacy/uzu/src/engine/sampling.rsnew file mode 100644index 000000000..b2fbb537d--- /dev/null+++ b/crates/legacy/uzu/src/engine/sampling.rs@@ -0,0 +1,113 @@+use std::path::Path;++use shoji::types::{basic::SamplingMethod, model::Model};+use uzu_engine::engine::{language_model::stream::SamplingMethod as UzuSamplingMethod, resolve_model_sampling_method};++use super::{Engine, EngineError};++impl Engine {+ /// Read a local model's sampling defaults without loading its weights or creating a session.+ pub async fn model_sampling_defaults(+ &self,+ model: &Model,+ ) -> Result<Option<SamplingMethod>, EngineError> {+ let Some(path) = self.model_path(model).await else {+ return Ok(None);+ };+ tokio::task::spawn_blocking(move || read_sampling_defaults(Path::new(&path))).await.map_err(|error| {+ EngineError::TokioError {+ message: error.to_string(),+ }+ })?+ }+}++fn read_sampling_defaults(model_path: &Path) -> Result<Option<SamplingMethod>, EngineError> {+ resolve_model_sampling_method(model_path).map(|method| method.map(sampling_method)).map_err(|error| {+ EngineError::ModelConfig {+ message: error.to_string(),+ }+ })+}++fn sampling_method(method: UzuSamplingMethod) -> SamplingMethod {+ match method {+ UzuSamplingMethod::Greedy => SamplingMethod::Greedy {},+ UzuSamplingMethod::Stochastic {+ temperature,+ top_k,+ top_p,+ min_p,+ repetition_penalty,+ suffix_repetition_length,+ } => SamplingMethod::Stochastic {+ temperature: temperature.map(sampling_float),+ top_k: top_k.map(i64::from),+ top_p: top_p.map(sampling_float),+ min_p: min_p.map(sampling_float),+ repetition_penalty: repetition_penalty.map(sampling_float),+ suffix_repetition_length: suffix_repetition_length.map(i64::from),+ },+ }+}++fn sampling_float(value: f32) -> f64 {+ // Keep the shortest decimal that round-trips to the engine's f32 value.+ // Fixed decimal rounding would turn sufficiently small positive temperatures into zero.+ value.to_string().parse().expect("an f32 decimal parses as f64")+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn reads_sampling_defaults_without_weights_and_preserves_disabled_filters() {+ let directory = tempfile::tempdir().unwrap();+ 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": [], "model_dim": 4, "hidden_dim": 4,+ "output_norm_config": {+ "epsilon": 0.00001, "scale_offset": null,+ "upcast_mode": "only_normalization", "subtract_mean": false,+ "has_scale": true, "has_biases": false,+ },+ },+ "vocab_size": 4, "ple_model_config": null, "embedding_norm_config": null,+ },+ "generation_config": {+ "stop_token_ids": [], "temperature": null, "top_k": 20, "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"), serde_json::to_vec(&config).unwrap()).unwrap();+ assert_eq!(+ read_sampling_defaults(directory.path()).unwrap(),+ Some(SamplingMethod::Stochastic {+ temperature: None,+ top_k: Some(20),+ top_p: None,+ min_p: None,+ repetition_penalty: None,+ suffix_repetition_length: None,+ })+ );+ }++ #[test]+ fn sampling_numbers_are_readable_and_round_trip_even_when_tiny() {+ assert_eq!(sampling_float(0.6), 0.6);+ assert_eq!(sampling_float(0.000_001), 0.000_001);+ for value in [f32::MIN_POSITIVE, f32::from_bits(1), 0.000_001, 0.6, 0.999_999_94, f32::MAX] {+ assert_eq!(sampling_float(value) as f32, value);+ }+ }+}diff --git a/crates/legacy/uzu/src/registry/cached.rs b/crates/legacy/uzu/src/registry/cached.rsindex d9ead9d92..257d1aa5d 100644--- a/crates/legacy/uzu/src/registry/cached.rs+++ b/crates/legacy/uzu/src/registry/cached.rs@@ -1,20 +1,25 @@-use std::{future::Future, pin::Pin};+use std::{+ future::Future,+ pin::Pin,+ sync::{Arc, Mutex},+};use shoji::{traits::Registry, types::model::Model};-use tokio::sync::Mutex;use crate::registry::RegistryError;pub struct CachedRegistry {- registry: Box<dyn Registry<Error = RegistryError>>,- listing: Mutex<Option<(Vec<Model>, bool)>>,+ registry: Arc<dyn Registry<Error = RegistryError>>,+ listing: Arc<Mutex<Option<(Vec<Model>, bool)>>>,+ refresh: tokio::sync::Mutex<()>,}impl CachedRegistry {pub fn new(registry: Box<dyn Registry<Error = RegistryError>>) -> Self {Self {- registry,- listing: Mutex::new(None),+ listing: Arc::new(Mutex::new(registry.cached_listing())),+ registry: registry.into(),+ refresh: tokio::sync::Mutex::new(()),}}}@@ -32,14 +37,48 @@ impl Registry for CachedRegistry {fn listing(&self) -> Pin<Box<dyn Future<Output = Result<(Vec<Model>, bool), RegistryError>> + Send + '_>> {Box::pin(async {- let mut cached_listing = self.listing.lock().await;- if let Some(cached_listing) = cached_listing.as_ref() {- Ok(cached_listing.clone())- } else {- let listing = self.registry.listing().await?;- *cached_listing = Some(listing.clone());- Ok(listing)+ if let Some(listing) = self.cached_listing() {+ return Ok(listing);}+ self.refresh_listing(Arc::new(|| {})).await+ })+ }++ fn cached_listing(&self) -> Option<(Vec<Model>, bool)> {+ self.listing.lock().expect("registry snapshot mutex poisoned").clone()+ }++ fn refresh_listing(+ &self,+ on_update: Arc<dyn Fn() + Send + Sync>,+ ) -> Pin<Box<dyn Future<Output = Result<(Vec<Model>, bool), RegistryError>> + Send + '_>> {+ Box::pin(async move {+ let _refresh = self.refresh.lock().await;+ let had_snapshot = {+ let mut snapshot = self.listing.lock().expect("registry snapshot mutex poisoned");+ if let Some((_, complete)) = snapshot.as_mut() {+ *complete = false;+ }+ snapshot.is_some()+ };+ if had_snapshot {+ on_update();+ }+ let registry = self.registry.clone();+ let snapshot = self.listing.clone();+ let notify = on_update.clone();+ let listing = self+ .registry+ .refresh_listing(Arc::new(move || {+ if let Some(listing) = registry.cached_listing() {+ *snapshot.lock().expect("registry snapshot mutex poisoned") = Some(listing);+ notify();+ }+ }))+ .await?;+ *self.listing.lock().expect("registry snapshot mutex poisoned") = Some(listing.clone());+ on_update();+ Ok(listing)})}}diff --git a/crates/legacy/uzu/src/registry/merged.rs b/crates/legacy/uzu/src/registry/merged.rsindex f83d6e973..69b04e347 100644--- a/crates/legacy/uzu/src/registry/merged.rs+++ b/crates/legacy/uzu/src/registry/merged.rs@@ -1,4 +1,4 @@-use std::{future::Future, pin::Pin};+use std::{future::Future, pin::Pin, sync::Arc};use shoji::{traits::Registry,@@ -7,20 +7,21 @@ use shoji::{use crate::registry::RegistryError;+#[derive(Clone)]pub struct MergedRegistry {- registries: Vec<Box<dyn Registry<Error = RegistryError>>>,+ registries: Vec<Arc<dyn Registry<Error = RegistryError>>>,}impl MergedRegistry {pub fn new(registries: Vec<Box<dyn Registry<Error = RegistryError>>>) -> Self {Self {- registries,+ registries: registries.into_iter().map(Arc::from).collect(),}}pub fn add(&mut self,- registry: Box<dyn Registry<Error = RegistryError>>,+ registry: Arc<dyn Registry<Error = RegistryError>>,) -> Result<(), RegistryError> {if self.registries.iter().any(|current_registry| current_registry.identifier() == registry.identifier()) {return Err(RegistryError::UnableToAddRegistry {@@ -98,7 +99,39 @@ impl Registry for MergedRegistry {fn listing(&self) -> Pin<Box<dyn Future<Output = Result<(Vec<Model>, bool), RegistryError>> + Send + '_>> {Box::pin(async {- let results = futures::future::join_all(self.registries.iter().map(|registry| registry.listing())).await;+ if let Some(listing) = self.cached_listing() {+ return Ok(listing);+ }+ self.refresh_listing(Arc::new(|| {})).await+ })+ }++ fn cached_listing(&self) -> Option<(Vec<Model>, bool)> {+ let mut models = Vec::new();+ let mut complete = true;+ let mut loaded = self.registries.is_empty();+ for registry in &self.registries {+ match registry.cached_listing() {+ Some((snapshot, registry_complete)) => {+ loaded = true;+ models.extend(snapshot);+ complete &= registry_complete;+ },+ None => complete = false,+ }+ }+ loaded.then_some((models, complete))+ }++ fn refresh_listing(+ &self,+ on_update: Arc<dyn Fn() + Send + Sync>,+ ) -> Pin<Box<dyn Future<Output = Result<(Vec<Model>, bool), RegistryError>> + Send + '_>> {+ Box::pin(async move {+ let results = futures::future::join_all(+ self.registries.iter().map(|registry| registry.refresh_listing(on_update.clone())),+ )+ .await;let mut models = Vec::new();let mut complete = true;@@ -110,6 +143,9 @@ impl Registry for MergedRegistry {},Err(error) => {complete = false;+ if let Some((cached, _)) = registry.cached_listing() {+ models.extend(cached);+ }tracing::warn!(?error, registry = %registry.identifier(), "skipping registry that failed to list models");},}diff --git a/crates/legacy/uzu/src/registry/mirai/api.rs b/crates/legacy/uzu/src/registry/mirai/api.rsindex c8e378bc0..5ddafd03d 100644--- a/crates/legacy/uzu/src/registry/mirai/api.rs+++ b/crates/legacy/uzu/src/registry/mirai/api.rs@@ -1,3 +1,2 @@pub const REGISTRY_URL: &str = "https://sdk.trymirai.com/api/v1";-pub const TELEMETRY_URL: &str = "https://sdk.trymirai.com/api/v2";pub const HUGGING_FACE_URL: &str = "https://huggingface.co";diff --git a/crates/legacy/uzu/src/registry/mirai/fetch_models.rs b/crates/legacy/uzu/src/registry/mirai/fetch_models.rsindex f5b0224dc..aca969513 100644--- a/crates/legacy/uzu/src/registry/mirai/fetch_models.rs+++ b/crates/legacy/uzu/src/registry/mirai/fetch_models.rs@@ -7,7 +7,7 @@ use crate::device::Device;#[derive(Serialize, Builder)]pub struct FetchModels {- device: Device,+ device: CatalogDevice,backends: Vec<Backend>,#[builder(default)]include_traces: bool,@@ -15,9 +15,58 @@ pub struct FetchModels {show_all: bool,}+#[derive(Clone, Serialize)]+pub struct CatalogDevice {+ os_name: Option<String>,+ cpu_name: Option<String>,+ memory_total: i64,+}++impl From<&Device> for CatalogDevice {+ fn from(device: &Device) -> Self {+ Self {+ os_name: device.os_name.clone(),+ cpu_name: device.cpu_name.clone(),+ memory_total: device.memory_total,+ }+ }+}+impl Endpoint for FetchModels {const PATH: &'static str = "fetch/models";type Request = Self;type Response = Response;}++#[cfg(test)]+mod tests {+ use serde_json::json;++ use super::*;++ #[test]+ fn catalog_request_contains_hardware_metadata_but_no_local_paths() {+ let device = Device {+ os_name: Some("macOS".to_string()),+ cpu_name: Some("Apple silicon".to_string()),+ memory_total: 32_000_000_000,+ home_path: "/Users/private-user".to_string(),+ };+ let request = FetchModels::builder().device(CatalogDevice::from(&device)).backends(vec![]).build();++ assert_eq!(+ serde_json::to_value(request).unwrap(),+ json!({+ "device": {+ "os_name": "macOS",+ "cpu_name": "Apple silicon",+ "memory_total": 32_000_000_000i64,+ },+ "backends": [],+ "include_traces": false,+ "show_all": false,+ })+ );+ }+}diff --git a/crates/legacy/uzu/src/registry/mirai/hugging_face.rs b/crates/legacy/uzu/src/registry/mirai/hugging_face.rsindex 03ffa1505..872846901 100644--- a/crates/legacy/uzu/src/registry/mirai/hugging_face.rs+++ b/crates/legacy/uzu/src/registry/mirai/hugging_face.rs@@ -98,9 +98,9 @@ impl HuggingFace {}),),};- let (Some(size), Some(hash)) = (size, hash) else {+ let Some(size) = size else {return Err(RegistryError::UnableToGetModels {- message: format!("Hugging Face is missing size or digest for {name}"),+ message: format!("Hugging Face is missing size for {name}"),});};Ok(File {@@ -111,7 +111,7 @@ impl HuggingFace {size: i64::try_from(size).map_err(|_| RegistryError::UnableToGetModels {message: format!("file size overflow for {name}"),})?,- hashes: vec![hash],+ hashes: hash.into_iter().collect(),})}).collect::<Result<Vec<_>, _>>()?;diff --git a/crates/legacy/uzu/src/registry/mirai/mod.rs b/crates/legacy/uzu/src/registry/mirai/mod.rsindex 33589c372..514f3ee3d 100644--- a/crates/legacy/uzu/src/registry/mirai/mod.rs+++ b/crates/legacy/uzu/src/registry/mirai/mod.rs@@ -8,7 +8,7 @@ mod hugging_face_model;mod registry;mod types;-pub use api::{HUGGING_FACE_URL, TELEMETRY_URL};+pub use api::HUGGING_FACE_URL;pub use backend::Backend;pub use hugging_face::HuggingFace;pub use registry::Registry;diff --git a/crates/legacy/uzu/src/registry/mirai/registry.rs b/crates/legacy/uzu/src/registry/mirai/registry.rsindex f00bea2ca..d6c06168e 100644--- a/crates/legacy/uzu/src/registry/mirai/registry.rs+++ b/crates/legacy/uzu/src/registry/mirai/registry.rs@@ -1,15 +1,16 @@use std::{- fs::{read_to_string, write},- future::{Future, ready},+ fs::{read_to_string, remove_file, rename, write},+ future::Future,io,- path::PathBuf,+ path::{Path, PathBuf},pin::Pin,+ sync::{Arc, Mutex},};use bon::bon;use download_manager::BearerToken;use futures::{StreamExt, stream};-use nagare::api::{Client, Error as ApiError, IsTransient};+use nagare::api::{Client, Error as ApiError};use shoji::{traits::Registry as RegistryTrait,types::model::{Model, ModelAccessibility, ModelSource},@@ -18,7 +19,7 @@ use shoji::{use super::{api::{HUGGING_FACE_URL, REGISTRY_URL},backend::Backend,- fetch_models::FetchModels,+ fetch_models::{CatalogDevice, FetchModels},hugging_face::{HuggingFace, is_lower_hex},};use crate::{device::Device, registry::RegistryError};@@ -26,12 +27,13 @@ use crate::{device::Device, registry::RegistryError};const CONCURRENT_RESOLUTIONS: usize = 8;pub struct Registry {- device: Device,+ device: CatalogDevice,backends: Vec<Backend>,include_traces: bool,cache_path: PathBuf,client: Client,hugging_face: HuggingFace,+ snapshot: Mutex<Option<(Vec<Model>, bool)>>,}#[bon]@@ -54,14 +56,16 @@ impl Registry {})?;let hugging_face =HuggingFace::builder().endpoint(hugging_face_url).maybe_token(huggingface_api_key).build()?;+ let snapshot = Self::load_registry(&cache_path).ok().map(|models| (models, false));Ok(Self {- device,+ device: (&device).into(),backends,include_traces,cache_path,client,hugging_face,+ snapshot: Mutex::new(snapshot),})}}@@ -79,32 +83,35 @@ impl RegistryTrait for Registry {fn listing(&self) -> Pin<Box<dyn Future<Output = Result<(Vec<Model>, bool), RegistryError>> + Send + '_>> {Box::pin(async {- let cached = self.load_registry();- match self.fetch_models().await {- Ok(models) => {- let listed = models.len();- let models = self.resolve(models, cached.unwrap_or_default()).await;- let saved = serde_json::to_vec_pretty(&models)- .map_err(io::Error::other)- .and_then(|contents| write(self.registry_path(), contents));- if let Err(error) = saved {- tracing::warn!(?error, "failed to save Mirai registry");- }- let complete = models.len() == listed;- Ok((models, complete))- },- Err(error) => {- if error.is_transient()- && let Ok(models) = cached- {- tracing::warn!(?error, "serving cached Mirai registry after fetch failure");- return Ok((models, false));- }- Err(RegistryError::UnableToGetModels {- message: error.to_string(),- })- },+ if let Some(listing) = self.cached_listing() {+ return Ok(listing);+ }+ self.refresh_listing(Arc::new(|| {})).await+ })+ }++ fn cached_listing(&self) -> Option<(Vec<Model>, bool)> {+ self.snapshot.lock().expect("Mirai registry snapshot mutex poisoned").clone()+ }++ fn refresh_listing(+ &self,+ on_update: Arc<dyn Fn() + Send + Sync>,+ ) -> Pin<Box<dyn Future<Output = Result<(Vec<Model>, bool), RegistryError>> + Send + '_>> {+ Box::pin(async move {+ let cached = self.cached_listing().map(|(models, _)| models).unwrap_or_default();+ if !cached.is_empty() {+ self.publish((cached.clone(), false), &on_update);}+ let models = self.fetch_models().await.map_err(|error| RegistryError::UnableToGetModels {+ message: error.to_string(),+ })?;+ let listing = self.resolve(models, cached, &on_update).await;+ if let Err(error) = self.save_registry(&listing.0) {+ tracing::warn!(?error, "failed to save Mirai registry");+ }+ self.publish(listing.clone(), &on_update);+ Ok(listing)})}}@@ -125,13 +132,39 @@ impl Registry {&self,models: Vec<Model>,cached: Vec<Model>,- ) -> Vec<Model> {- stream::iter(models)- .map(|model| self.resolve_model(model, &cached))- .buffered(CONCURRENT_RESOLUTIONS)- .filter_map(ready)- .collect()- .await+ on_update: &Arc<dyn Fn() + Send + Sync>,+ ) -> (Vec<Model>, bool) {+ // Keep the last usable descriptor while a new revision's files resolve.+ // A failed lookup must not remove another already downloaded model.+ let mut resolved: Vec<Option<Model>> = models+ .iter()+ .map(|model| cached.iter().find(|previous| previous.identifier == model.identifier).cloned())+ .collect();+ let snapshot = |resolved: &[Option<Model>]| resolved.iter().flatten().cloned().collect::<Vec<_>>();+ self.publish((snapshot(&resolved), false), on_update);+ let cached = &cached;+ let mut pending = stream::iter(models.into_iter().enumerate())+ .map(|(index, model)| async move { (index, self.resolve_model(model, cached).await) })+ .buffer_unordered(CONCURRENT_RESOLUTIONS);+ let mut complete = true;+ while let Some((index, model)) = pending.next().await {+ if let Some(model) = model {+ resolved[index] = Some(model);+ self.publish((snapshot(&resolved), false), on_update);+ } else {+ complete = false;+ }+ }+ (snapshot(&resolved), complete)+ }++ fn publish(+ &self,+ listing: (Vec<Model>, bool),+ on_update: &Arc<dyn Fn() + Send + Sync>,+ ) {+ *self.snapshot.lock().expect("Mirai registry snapshot mutex poisoned") = Some(listing);+ on_update();}async fn resolve_model(@@ -178,14 +211,27 @@ impl Registry {Some(model)}- fn registry_path(&self) -> PathBuf {- self.cache_path.join("registry.json")+ fn save_registry(+ &self,+ models: &[Model],+ ) -> io::Result<()> {+ let temporary = self.cache_path.join(format!(".registry-{}.json", uuid::Uuid::new_v4()));+ let saved = (|| {+ let contents = serde_json::to_vec_pretty(models).map_err(io::Error::other)?;+ write(&temporary, contents)?;+ rename(&temporary, self.cache_path.join("registry.json"))+ })();+ if saved.is_err() {+ let _ = remove_file(temporary);+ }+ saved}- fn load_registry(&self) -> Result<Vec<Model>, RegistryError> {- let contents = read_to_string(self.registry_path()).map_err(|error| RegistryError::UnableToGetModels {- message: format!("Unable to read registry: {}", error),- })?;+ fn load_registry(cache_path: &Path) -> Result<Vec<Model>, RegistryError> {+ let contents =+ read_to_string(cache_path.join("registry.json")).map_err(|error| RegistryError::UnableToGetModels {+ message: format!("Unable to read registry: {}", error),+ })?;serde_json::from_str(&contents).map_err(|error| RegistryError::UnableToGetModels {message: format!("Unable to parse registry: {}", error),})diff --git a/crates/legacy/uzu/src/registry/openai/mod.rs b/crates/legacy/uzu/src/registry/openai/mod.rsindex e0bb1353b..885c5bd56 100644--- a/crates/legacy/uzu/src/registry/openai/mod.rs+++ b/crates/legacy/uzu/src/registry/openai/mod.rs@@ -1,6 +1,6 @@mod config;-use std::{future::Future, pin::Pin};+use std::{future::Future, pin::Pin, time::Duration};use async_openai::{Client, config::OpenAIConfig};pub use config::Config;@@ -75,9 +75,14 @@ impl RegistryTrait for Registry {fn models(&self) -> Pin<Box<dyn Future<Output = Result<Vec<ShojiModel>, RegistryError>> + Send + '_>> {Box::pin(async {let response: ListModelsResponse =- self.client.models().list_byot().await.map_err(|error| RegistryError::UnableToGetModels {- message: error.to_string(),- })?;+ tokio::time::timeout(Duration::from_secs(30), self.client.models().list_byot())+ .await+ .map_err(|_| RegistryError::UnableToGetModels {+ message: "Model catalog request timed out after 30 seconds".to_string(),+ })?+ .map_err(|error| RegistryError::UnableToGetModels {+ message: error.to_string(),+ })?;let mut identifiers = response.data.into_iter().map(|model| model.id).collect::<Vec<_>>();identifiers.sort();diff --git a/crates/legacy/uzu/src/storage/error.rs b/crates/legacy/uzu/src/storage/error.rsindex 97aabfd4c..8efcc0c2f 100644--- a/crates/legacy/uzu/src/storage/error.rs+++ b/crates/legacy/uzu/src/storage/error.rs@@ -13,11 +13,6 @@ pub enum StorageError {DownloadManager {message: String,},- #[error("Hash not found for file: {identifier}/{name}")]- HashNotFound {- identifier: ModelIdentifier,- name: String,- },#[error("Model not found: {identifier}")]ModelNotFound {identifier: ModelIdentifier,diff --git a/crates/legacy/uzu/src/storage/model_tasks.rs b/crates/legacy/uzu/src/storage/model_tasks.rsindex bcf555d04..e022a4685 100644--- a/crates/legacy/uzu/src/storage/model_tasks.rs+++ b/crates/legacy/uzu/src/storage/model_tasks.rs@@ -1,7 +1,140 @@-use std::{collections::HashMap, sync::Arc};+use std::{+ collections::HashMap,+ sync::{Arc, Mutex},+ time::Duration,+};-use download_manager::DownloadTask;+use download_manager::{DownloadManager, DownloadPhase, DownloadState, DownloadTask, DownloadTaskRequest};use kiban::rt::TaskJoinHandle;use shoji::types::model::ModelIdentifier;+use tokio::sync::{broadcast, watch};+use tokio_stream::StreamExt;-pub type ModelTasks = HashMap<ModelIdentifier, (Arc<DownloadTask>, Box<dyn TaskJoinHandle<()>>)>;+use super::StorageError;++pub type ModelTasks = HashMap<ModelIdentifier, Arc<ModelTask>>;+type TaskResult = Result<Arc<DownloadTask>, StorageError>;++pub struct ModelTask {+ pub request: Result<DownloadTaskRequest, StorageError>,+ total_bytes: i64,+ result: watch::Sender<Option<TaskResult>>,+ worker: Mutex<Option<Box<dyn TaskJoinHandle<()>>>>,+}++impl ModelTask {+ pub fn new(+ identifier: ModelIdentifier,+ request: Result<DownloadTaskRequest, StorageError>,+ total_bytes: i64,+ manager: Arc<DownloadManager>,+ events: broadcast::Sender<(ModelIdentifier, DownloadState)>,+ previous: Option<Arc<ModelTask>>,+ ) -> Self {+ let (result, _) = watch::channel(None);+ let sender = result.clone();+ let worker = kiban::rt::spawn({+ let request = request.clone();+ async move {+ if let Some(previous) = previous {+ previous.stop().await;+ }+ let _ = events.send((+ identifier.clone(),+ DownloadState {+ total_bytes,+ downloaded_bytes: 0,+ phase: DownloadPhase::Initializing {},+ },+ ));+ let initialized = match request {+ Ok(request) => tokio::select! {+ result = manager.download_task(request) => result.map_err(StorageError::from),+ _ = kiban::time::sleep(Duration::from_secs(10)) => Err(StorageError::DownloadManager {+ message: "Timed out loading this model's download state. Retry the download to try again.".to_string(),+ }),+ },+ Err(error) => Err(error),+ };+ sender.send_replace(Some(initialized.clone()));+ match initialized {+ Ok(task) => {+ let mut progress = task.progress();+ while let Some(state) = progress.next().await {+ let _ = events.send((identifier.clone(), state));+ }+ },+ Err(error) => {+ tracing::warn!(%identifier, %error, "unable to initialize model download");+ let _ = events.send((identifier, Self::error_state(total_bytes, &error)));+ },+ }+ }+ });+ Self {+ request,+ total_bytes,+ result,+ worker: Mutex::new(Some(worker)),+ }+ }++ pub fn state(&self) -> DownloadState {+ match self.result.borrow().as_ref() {+ Some(Ok(task)) => task.state(),+ Some(Err(error)) => Self::error_state(self.total_bytes, error),+ None => DownloadState {+ total_bytes: self.total_bytes,+ downloaded_bytes: 0,+ phase: DownloadPhase::Initializing {},+ },+ }+ }++ pub fn failed(&self) -> bool {+ matches!(self.result.borrow().as_ref(), Some(Err(_)))+ }++ pub async fn ready(&self) -> TaskResult {+ let mut result = self.result.subscribe();+ loop {+ if let Some(result) = result.borrow().clone() {+ return result;+ }+ result.changed().await.map_err(|_| StorageError::DownloadManager {+ message: "Model download initialization stopped".to_string(),+ })?;+ }+ }++ async fn stop(&self) {+ let worker = self.worker.lock().expect("model worker mutex poisoned").take();+ if let Some(worker) = worker {+ worker.abort_and_join().await;+ }+ self.result.send_replace(Some(Err(StorageError::DownloadManager {+ message: "Model download state was replaced by a newer catalog entry".to_string(),+ })));+ }++ fn error_state(+ total_bytes: i64,+ error: &StorageError,+ ) -> DownloadState {+ DownloadState {+ total_bytes,+ downloaded_bytes: 0,+ phase: DownloadPhase::Error {+ message: error.to_string(),+ },+ }+ }+}++impl Drop for ModelTask {+ fn drop(&mut self) {+ if let Some(worker) = self.worker.get_mut().expect("model worker mutex poisoned").take() {+ worker.abort();+ }+ }+}diff --git a/crates/legacy/uzu/src/storage/storage.rs b/crates/legacy/uzu/src/storage/storage.rsindex 298873467..a7c442157 100644--- a/crates/legacy/uzu/src/storage/storage.rs+++ b/crates/legacy/uzu/src/storage/storage.rs@@ -1,31 +1,30 @@-use std::{- collections::HashMap,- path::{Path, PathBuf},- sync::Arc,-};+use std::{collections::HashMap, path::PathBuf, sync::Arc};-use download_manager::{Checksum, DestinationLock, DownloadManager, DownloadState, DownloadTask, DownloadTaskRequest};-use futures_util::future::join_all;+use download_manager::{DestinationLock, DownloadManager, DownloadState, DownloadTask, DownloadTaskRequest};use kiban::{fs, rt::RuntimeHandle};use shoji::types::{- basic::{File, HashMethod},+ basic::File,model::{Model, ModelAccessibility, ModelIdentifier, ModelSource},};use tokio::sync::{Mutex as TokioMutex,broadcast::{Sender as TokioBroadcastSender, channel as tokio_broadcast_channel},};-use tokio_stream::{StreamExt, wrappers::BroadcastStream};+use tokio_stream::wrappers::BroadcastStream;use crate::{helpers::same_origin,- storage::{Config, StorageError, model_tasks::ModelTasks},+ storage::{+ Config, StorageError,+ model_tasks::{ModelTask, ModelTasks},+ },};pub struct Storage {config: Config,- download_manager: DownloadManager,+ download_manager: Arc<DownloadManager>,tasks: TokioMutex<ModelTasks>,+ refresh_lock: TokioMutex<()>,events: TokioBroadcastSender<(ModelIdentifier, DownloadState)>,}@@ -40,9 +39,10 @@ impl Storage {})?;let (events, _) = tokio_broadcast_channel(256);Ok(Self {- download_manager: DownloadManager::new(config.download_manager_type, runtime_handle),+ download_manager: Arc::new(DownloadManager::new(config.download_manager_type, runtime_handle)),config,tasks: TokioMutex::new(ModelTasks::new()),+ refresh_lock: TokioMutex::new(()),events,})}@@ -65,6 +65,7 @@ impl Storage {models: &[Model],complete: bool,) -> Result<(), StorageError> {+ let _refresh = self.refresh_lock.lock().await;let mut requests = HashMap::new();for model in models {let ModelAccessibility::OnDevice {@@ -76,64 +77,73 @@ impl Storage {else {continue;};- requests.entry(model.identifier.clone()).or_insert(self.request(model, files)?);+ let total_bytes = files.iter().map(|file| file.size.max(0)).fold(0_i64, i64::saturating_add);+ requests.entry(model.identifier.clone()).or_insert_with(|| (self.request(model, files), total_bytes));+ }+ let mut tasks = self.tasks.lock().await;+ let mut keep_paths: Vec<PathBuf> = requests+ .values()+ .filter_map(|(request, _)| request.as_ref().ok().map(|request| request.destination.clone()))+ .collect();+ let can_clean = complete && !keep_paths.is_empty();+ keep_paths.extend(+ tasks+ .values()+ .filter(|task| task.state().phase.is_in_progress())+ .filter_map(|task| task.request.as_ref().ok().map(|request| request.destination.clone())),+ );+ if complete {+ tasks.retain(|identifier, _| requests.contains_key(identifier));}- let destinations: Vec<&Path> = requests.values().map(|request| request.destination.as_path()).collect();- let is_listed = |path: &Path| destinations.iter().any(|destination| destination.starts_with(path));+ for (identifier, (request, total_bytes)) in requests {+ if tasks.get(&identifier).is_some_and(|task| task.request == request) {+ continue;+ }+ let previous = tasks.remove(&identifier);+ tasks.insert(+ identifier.clone(),+ Arc::new(ModelTask::new(+ identifier,+ request,+ total_bytes,+ Arc::clone(&self.download_manager),+ self.events.clone(),+ previous,+ )),+ );+ }+ drop(tasks);+ if can_clean {+ self.remove_obsolete_checkpoints(&keep_paths).await;+ }+ Ok(())+ }++ async fn remove_obsolete_checkpoints(+ &self,+ keep_paths: &[PathBuf],+ ) {for model_path in fs::asyn::read_dir(self.models_path()).await.unwrap_or_default() {- let old_paths = if is_listed(&model_path) {+ if !tokio::fs::symlink_metadata(&model_path).await.is_ok_and(|metadata| metadata.is_dir()) {+ continue;+ }+ let candidates = if keep_paths.iter().any(|path| path.starts_with(&model_path)) {fs::asyn::read_dir(&model_path).await.unwrap_or_default()- } else if complete && !destinations.is_empty() {- vec![model_path]} else {- continue;+ vec![model_path]};- for old_path in old_paths {- if is_listed(&old_path)- || fs::asyn::is_file(&old_path).await- || DestinationLock::held_within(&old_path).await+ for path in candidates {+ if keep_paths.iter().any(|keep| keep.starts_with(&path))+ || !tokio::fs::symlink_metadata(&path).await.is_ok_and(|metadata| metadata.is_dir())+ || DestinationLock::held_within(&path).await{continue;}- match fs::asyn::remove_dir_all(&old_path).await {- Ok(()) => tracing::info!(path = %old_path.display(), "removed old model"),- Err(error) => tracing::warn!(?error, path = %old_path.display(), "failed to remove old model"),- }- }- }- let missing: Vec<(ModelIdentifier, DownloadTaskRequest)> = {- let mut tasks = self.tasks.lock().await;- tasks.retain(|identifier, (task, forwarder)| {- let keep = requests.get(identifier).is_some_and(|request| task.request() == request);- if !keep {- forwarder.abort();- }- keep- });- requests.into_iter().filter(|(identifier, _)| !tasks.contains_key(identifier)).collect()- };- let created = join_all(missing.into_iter().map(|(identifier, request)| async move {- (identifier, self.download_manager.download_task(request).await)- }))- .await;- let mut tasks = self.tasks.lock().await;- for (identifier, task) in created {- let task = task?;- let events = self.events.clone();- let mut progress = task.progress();- let forwarder = kiban::rt::spawn({- let identifier = identifier.clone();- async move {- while let Some(state) = progress.next().await {- let _ = events.send((identifier.clone(), state));- }+ if let Err(error) = fs::asyn::remove_dir_all(&path).await {+ tracing::warn!(?error, path = %path.display(), "failed to remove obsolete checkpoint");}- });- if let Some((_, replaced)) = tasks.insert(identifier, (task, forwarder)) {- replaced.abort();}}- Ok(())}pub fn subscribe(&self) -> BroadcastStream<(ModelIdentifier, DownloadState)> {@@ -143,18 +153,41 @@ impl Storage {pub async fn state(&self,identifier: &ModelIdentifier,+ ) -> Result<DownloadState, StorageError> {+ Ok(self.entry(identifier).await?.state())+ }++ pub async fn ready_state(+ &self,+ identifier: &ModelIdentifier,) -> Result<DownloadState, StorageError> {Ok(self.model(identifier).await?.state())}pub async fn states(&self) -> HashMap<ModelIdentifier, DownloadState> {- self.tasks.lock().await.iter().map(|(identifier, (task, _))| (identifier.clone(), task.state())).collect()+ self.tasks.lock().await.iter().map(|(identifier, task)| (identifier.clone(), task.state())).collect()}pub async fn download(&self,identifier: &ModelIdentifier,) -> Result<(), StorageError> {+ {+ let mut tasks = self.tasks.lock().await;+ if let Some(task) = tasks.get(identifier)+ && task.failed()+ {+ let replacement = Arc::new(ModelTask::new(+ identifier.clone(),+ task.request.clone(),+ task.state().total_bytes,+ Arc::clone(&self.download_manager),+ self.events.clone(),+ Some(Arc::clone(task)),+ ));+ tasks.insert(identifier.clone(), replacement);+ }+ }Ok(self.model(identifier).await?.download().await?)}@@ -176,10 +209,15 @@ impl Storage {&self,identifier: &ModelIdentifier,) -> Result<Arc<DownloadTask>, StorageError> {- self.tasks.lock().await.get(identifier).map(|(task, _)| Arc::clone(task)).ok_or_else(|| {- StorageError::ModelNotFound {- identifier: identifier.clone(),- }+ self.entry(identifier).await?.ready().await+ }++ async fn entry(+ &self,+ identifier: &ModelIdentifier,+ ) -> Result<Arc<ModelTask>, StorageError> {+ self.tasks.lock().await.get(identifier).cloned().ok_or_else(|| StorageError::ModelNotFound {+ identifier: identifier.clone(),})}@@ -204,27 +242,14 @@ impl Storage {.huggingface_api_key.clone().filter(|_| same_origin(&file.url, &self.config.huggingface_url));- let checksum = file- .hashes- .first()- .map(|hash| match hash.method {- HashMethod::CRC32C => Checksum::Crc32c(hash.value.clone()),- HashMethod::Sha256 => Checksum::Sha256(hash.value.clone()),- HashMethod::GitBlobSha1 => Checksum::GitBlobSha1(hash.value.clone()),- })- .ok_or_else(|| StorageError::HashNotFound {- identifier: model.identifier.clone(),- name: file.name.clone(),- })?;- Ok(DownloadTaskRequest::file()+ DownloadTaskRequest::file().destination(&file.name).source_url(&file.url).maybe_bearer_token(bearer_token)- .expected_checksum(checksum).maybe_expected_bytes(u64::try_from(file.size).ok())- .build())+ .build()})- .collect::<Result<Vec<_>, StorageError>>()?;+ .collect();Ok(DownloadTaskRequest::group().destination(cache_path).subrequests(subrequests).build())}}diff --git a/crates/legacy/uzu/tests/registry.rs b/crates/legacy/uzu/tests/registry.rsindex 2341d94f4..56fe70e16 100644--- a/crates/legacy/uzu/tests/registry.rs+++ b/crates/legacy/uzu/tests/registry.rs@@ -1,9 +1,14 @@#![cfg(not(target_family = "wasm"))]+use std::{future::Future, pin::Pin, sync::Arc, time::Duration};+use serde_json::{Value, json};use uzu::{device::Device,- registry::mirai::{Backend, HuggingFace, Registry},+ registry::{+ CachedRegistry, MergedRegistry, RegistryError,+ mirai::{Backend, HuggingFace, Registry},+ },traits::Registry as RegistryTrait,types::{basic::{File, Hash, HashMethod, Repository},@@ -78,6 +83,210 @@ fn listed_model(})}+fn registry_response(models: Vec<Value>) -> Value {+ json!({+ "metadatas": [+ { "id": "registry-meta", "name": "Mirai", "description": null, "icons": [] },+ { "id": "backend-meta", "name": "Uzu", "description": null, "icons": [] }+ ],+ "models": models,+ })+}++fn cached_model(identifier: &str) -> Model {+ Model::external(+ identifier.to_string(),+ "mirai".to_string(),+ "Mirai".to_string(),+ "uzu".to_string(),+ "Uzu".to_string(),+ "1".to_string(),+ vec![],+ ModelAccessibility::Remote {+ repository: None,+ },+ None,+ )+}++struct BlockingRegistry {+ started: Arc<tokio::sync::Notify>,+ finish: Arc<tokio::sync::Notify>,+ snapshot: Option<Vec<Model>>,+ result: Result<Vec<Model>, RegistryError>,+}++impl RegistryTrait for BlockingRegistry {+ type Error = RegistryError;++ fn identifier(&self) -> String {+ "blocking".to_string()+ }++ fn cached_listing(&self) -> Option<(Vec<Model>, bool)> {+ self.snapshot.clone().map(|models| (models, true))+ }++ fn models(&self) -> Pin<Box<dyn Future<Output = Result<Vec<Model>, RegistryError>> + Send + '_>> {+ Box::pin(async {+ self.started.notify_one();+ self.finish.notified().await;+ self.result.clone()+ })+ }+}++#[tokio::test]+async fn snapshots_stay_readable_during_refresh_and_survive_failure() {+ let started = Arc::new(tokio::sync::Notify::new());+ let finish = Arc::new(tokio::sync::Notify::new());+ let registry = Arc::new(CachedRegistry::new(Box::new(BlockingRegistry {+ started: started.clone(),+ finish: finish.clone(),+ snapshot: Some(vec![cached_model("cached")]),+ result: Err(RegistryError::UnableToGetModels {+ message: "offline".to_string(),+ }),+ })));+ let mut merged = MergedRegistry::new(vec![]);+ merged.add(registry.clone()).unwrap();+ let refresh = tokio::spawn({+ let registry = registry.clone();+ async move { registry.refresh_listing(Arc::new(|| {})).await }+ });+ started.notified().await;++ let (models, complete) = tokio::time::timeout(Duration::from_secs(1), merged.listing()).await.unwrap().unwrap();+ assert_eq!(models[0].identifier, "cached");+ assert!(!complete);+ finish.notify_one();+ assert!(refresh.await.unwrap().is_err());+ assert_eq!(registry.cached_listing().unwrap().0[0].identifier, "cached");+}++#[tokio::test]+async fn an_empty_completed_snapshot_is_distinct_from_an_unloaded_registry() {+ let finish = Arc::new(tokio::sync::Notify::new());+ let registry = CachedRegistry::new(Box::new(BlockingRegistry {+ started: Arc::new(tokio::sync::Notify::new()),+ finish: finish.clone(),+ snapshot: None,+ result: Ok(vec![]),+ }));+ assert!(registry.cached_listing().is_none());++ finish.notify_one();+ registry.refresh_listing(Arc::new(|| {})).await.unwrap();++ assert_eq!(registry.cached_listing(), Some((vec![], true)));+}++#[tokio::test]+async fn mirai_publishes_healthy_models_before_a_stalled_resolution() -> Result<(), Box<dyn std::error::Error>> {+ let hugging_face = MockServer::start().await;+ for (name, delay) in [("slow", Duration::from_secs(1)), ("fast", Duration::ZERO)] {+ Mock::given(method("GET"))+ .and(path(format!("/api/models/trymirai/{name}/revision/{REVISION}")))+ .respond_with(ResponseTemplate::new(200).set_delay(delay).set_body_json(json!({+ "sha": REVISION, "siblings": siblings(),+ })))+ .mount(&hugging_face)+ .await;+ }+ let mirai = MockServer::start().await;+ Mock::given(method("POST"))+ .and(path("/fetch/models"))+ .respond_with(ResponseTemplate::new(200).set_body_json(registry_response(vec![+ listed_model("slow", pinned("trymirai/slow", REVISION)),+ listed_model("fast", pinned("trymirai/fast", REVISION)),+ ])))+ .mount(&mirai)+ .await;+ let directory = tempfile::tempdir()?;+ let registry = Arc::new(CachedRegistry::new(Box::new(+ Registry::builder()+ .device(Device::new()?)+ .backends(vec![])+ .cache_path(directory.path().to_path_buf())+ .registry_url(mirai.uri())+ .hugging_face_url(hugging_face.uri())+ .build()?,+ )));+ assert!(registry.cached_listing().is_none());+ let (updates, mut received) = tokio::sync::mpsc::unbounded_channel();+ let refresh = tokio::spawn({+ let registry = registry.clone();+ let snapshot = registry.clone();+ async move {+ registry+ .refresh_listing(Arc::new(move || {+ let _ = updates.send(snapshot.cached_listing().unwrap());+ }))+ .await+ }+ });+ let first_nonempty = tokio::time::timeout(Duration::from_secs(5), async {+ loop {+ let listing = received.recv().await.expect("refresh ended without a snapshot");+ if !listing.0.is_empty() {+ break listing;+ }+ }+ })+ .await?;+ assert_eq!(first_nonempty.0.iter().map(|model| model.identifier.as_str()).collect::<Vec<_>>(), ["fast"]);+ assert!(!first_nonempty.1);+ let (models, complete) = refresh.await??;+ assert!(complete);+ assert_eq!(models.iter().map(|model| model.identifier.as_str()).collect::<Vec<_>>(), ["slow", "fast"]);+ Ok(())+}++#[tokio::test]+async fn mirai_serves_disk_cache_and_keeps_it_when_new_metadata_fails() -> Result<(), Box<dyn std::error::Error>> {+ let mirai = MockServer::start().await;+ let hugging_face = MockServer::start().await;+ Mock::given(method("POST"))+ .and(path("/fetch/models"))+ .respond_with(+ ResponseTemplate::new(200)+ .set_body_json(registry_response(vec![listed_model("cached", pinned("trymirai/missing", REVISION))])),+ )+ .expect(1)+ .mount(&mirai)+ .await;+ let directory = tempfile::tempdir()?;+ let mut previous = cached_model("cached");+ previous.accessibility = ModelAccessibility::OnDevice {+ source: ModelSource::Registry {+ toolchain_version: "1".to_string(),+ repository: Some(repository(&"e".repeat(40), None)),+ source_repository: None,+ files: vec![File {+ url: format!("{}/trymirai/model/resolve/{}/config.json", hugging_face.uri(), "e".repeat(40)),+ name: "config.json".to_string(),+ size: 6,+ hashes: vec![],+ }],+ },+ };+ std::fs::write(directory.path().join("registry.json"), serde_json::to_vec(&vec![previous.clone()])?)?;+ let registry = Registry::builder()+ .device(Device::new()?)+ .backends(vec![])+ .cache_path(directory.path().to_path_buf())+ .registry_url(mirai.uri())+ .hugging_face_url(hugging_face.uri())+ .build()?;++ assert_eq!(registry.models().await?, vec![previous.clone()]);+ assert!(mirai.received_requests().await.unwrap().is_empty());+ let (models, complete) = registry.refresh_listing(Arc::new(|| {})).await?;+ assert_eq!(models, vec![previous]);+ assert!(!complete);+ Ok(())+}+#[tokio::test]async fn hugging_face_files() -> Result<(), Box<dyn std::error::Error>> {let server = MockServer::start().await;@@ -115,7 +324,7 @@ async fn hugging_face_rejects_bad_metadata() -> Result<(), Box<dyn std::error::Efor body in [json!({ "sha": "main", "siblings": siblings() }),json!({ "sha": REVISION, "siblings": [{ "rfilename": "../config.json", "size": 6, "blobId": HELLO_GIT_BLOB_SHA1 }] }),- json!({ "sha": REVISION, "siblings": [{ "rfilename": "config.json", "size": 6, "blobId": null }] }),+ json!({ "sha": REVISION, "siblings": [{ "rfilename": "config.json", "blobId": HELLO_GIT_BLOB_SHA1 }] }),json!({ "sha": REVISION, "siblings": [] }),] {let server = MockServer::start().await;@@ -126,6 +335,29 @@ async fn hugging_face_rejects_bad_metadata() -> Result<(), Box<dyn std::error::EOk(())}+#[tokio::test]+async fn hugging_face_accepts_files_without_digests() -> Result<(), Box<dyn std::error::Error>> {+ let server = MockServer::start().await;+ Mock::given(method("GET"))+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({+ "sha": REVISION,+ "siblings": [+ { "rfilename": "config.json", "size": 6, "blobId": null },+ { "rfilename": "model.safetensors", "lfs": { "size": 20 } }+ ],+ })))+ .mount(&server)+ .await;+ let hugging_face = HuggingFace::builder().endpoint(server.uri()).build()?;++ let files = hugging_face.files(&repository(REVISION, None)).await?;++ assert_eq!(files.len(), 2);+ assert_eq!(files.iter().map(|file| file.size).collect::<Vec<_>>(), [6, 20]);+ assert!(files.iter().all(|file| file.hashes.is_empty()));+ Ok(())+}+fn pinned(identifier: &str,revision: &str,@@ -213,7 +445,8 @@ async fn mirai_registry_resolves_pinned_models_once() -> Result<(), Box<dyn std:.registry_url(mirai.uri()).hugging_face_url(hugging_face.uri()).build()?;- let models = registry.models().await?;+ let (models, complete) = registry.refresh_listing(Arc::new(|| {})).await?;+ assert!(!complete);assert_eq!(models.iter().map(|model| model.identifier.as_str()).collect::<Vec<_>>(),["pinned", "tagged", "unpinned"]diff --git a/crates/legacy/uzu/tests/storage.rs b/crates/legacy/uzu/tests/storage.rsindex ca2e422fb..b67cccf57 100644--- a/crates/legacy/uzu/tests/storage.rs+++ b/crates/legacy/uzu/tests/storage.rs@@ -16,7 +16,7 @@ use uzu::{registry::mirai::HUGGING_FACE_URL,storage::{DownloadManagerType, DownloadPhase, DownloadState, Storage},types::{- basic::{File, Hash, HashMethod, Repository},+ basic::{File, Repository},model::{Model, ModelAccessibility, ModelIdentifier, ModelSource},},};@@ -28,8 +28,6 @@ use wiremock::{use crate::common::TestStorage;const HELLO: &[u8] = b"hello\n";-const HELLO_SHA256: &str = "5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03";-const HELLO_GIT_BLOB_SHA1: &str = "ce013625030ba8dba906f756967f9e9ca394464a";const REVISION: &str = "f5dec40ceb1d8c4b15f049bf5e185dd7be2cc150";#[rstest]@@ -66,7 +64,7 @@ async fn model_lifecycle(#[case] kind: DownloadManagerType) -> Result<(), Box<dyfor served in registry.files.iter() {let destination = cache_path.join(&served.file.name);assert_eq!(tokio::fs::read(&destination).await?, served.bytes.to_vec());- assert!(artifact_path(&destination, "checksum").is_file());+ assert!(!artifact_path(&destination, "checksum").exists());}storage.delete(&identifier).await?;@@ -106,11 +104,11 @@ async fn model_lifecycle(#[case] kind: DownloadManagerType) -> Result<(), Box<dyfiles.truncate(1);}storage.refresh(&[changed.clone()], false).await?;- assert_eq!(storage.state(&identifier).await?.total_bytes, served.file.size);+ assert_eq!(storage.ready_state(&identifier).await?.total_bytes, served.file.size);storage.refresh(&[model.clone(), changed.clone()], false).await?;- assert_eq!(storage.state(&identifier).await?.total_bytes, total_bytes);+ assert_eq!(storage.ready_state(&identifier).await?.total_bytes, total_bytes);storage.refresh(&[changed, model.clone()], false).await?;- assert_eq!(storage.state(&identifier).await?.total_bytes, served.file.size);+ assert_eq!(storage.ready_state(&identifier).await?.total_bytes, served.file.size);Ok(())}@@ -185,19 +183,13 @@ async fn hugging_face_model(#[case] kind: DownloadManagerType) -> Result<(), Boxurl: format!("{}/config.json", cdn.uri()),name: "config.json".to_string(),size: HELLO.len() as i64,- hashes: vec![Hash {- method: HashMethod::GitBlobSha1,- value: HELLO_GIT_BLOB_SHA1.to_string(),- }],+ hashes: vec![],},File {url: format!("{}{route}", hugging_face.uri()),name: "model.safetensors".to_string(),size: HELLO.len() as i64,- hashes: vec![Hash {- method: HashMethod::Sha256,- value: HELLO_SHA256.to_string(),- }],+ hashes: vec![],},];let model = Model::external(@@ -239,7 +231,7 @@ async fn hugging_face_model(#[case] kind: DownloadManagerType) -> Result<(), Boxfor name in ["config.json", "model.safetensors"] {let destination = cache_path.join(name);assert_eq!(tokio::fs::read(&destination).await?, HELLO);- assert!(artifact_path(&destination, "checksum").is_file());+ assert!(!artifact_path(&destination, "checksum").exists());}Ok(())}@@ -266,3 +258,187 @@ async fn wait_for(.await.expect("timed out waiting for storage state")}++#[tokio::test(flavor = "multi_thread")]+async fn malformed_model_does_not_hide_a_cached_model() -> Result<(), Box<dyn std::error::Error>> {+ let test_storage =+ TestStorage::new(RuntimeHandle::current(), vec![], DownloadManagerType::Universal, HUGGING_FACE_URL, None)+ .await?;+ let storage = &test_storage.storage;+ let healthy = catalog_model("healthy", &[("model.bin", HELLO.len() as i64)]);+ let cache_path = storage.cache_model_path(&healthy).unwrap();+ tokio::fs::create_dir_all(&cache_path).await?;+ tokio::fs::write(cache_path.join("model.bin"), HELLO).await?;++ // Two incompatible requests for the same file make only this model uninitializable.+ let malformed = catalog_model("broken", &[("model.bin", 1), ("model.bin", 2)]);+ timeout(Duration::from_secs(2), storage.refresh(&[malformed.clone(), healthy.clone()], true)).await??;+ let healthy_state = timeout(Duration::from_secs(2), storage.ready_state(&healthy.identifier)).await??;+ assert_eq!(healthy_state.phase, DownloadPhase::Downloaded {});+ assert_eq!(healthy_state.downloaded_bytes, HELLO.len() as i64);+ let error = timeout(Duration::from_secs(2), storage.ready_state(&malformed.identifier))+ .await?+ .expect_err("conflicting files must fail initialization");+ assert!(error.to_string().contains("conflicting download config"), "unexpected error: {error}");+ assert!(matches!(storage.state(&malformed.identifier).await?.phase, DownloadPhase::Error { .. }));+ assert_eq!(storage.states().await.len(), 2);++ let repaired = catalog_model("broken", &[("model.bin", 1)]);+ storage.refresh(std::slice::from_ref(&repaired), false).await?;+ let repaired_state = timeout(Duration::from_secs(2), storage.ready_state(&repaired.identifier)).await??;+ assert_eq!(repaired_state.phase, DownloadPhase::NotDownloaded {});+ assert_eq!(storage.state(&healthy.identifier).await?, healthy_state);+ Ok(())+}++#[tokio::test(flavor = "multi_thread")]+async fn partial_catalog_keeps_existing_models_and_complete_catalog_removes_obsolete_files()+-> Result<(), Box<dyn std::error::Error>> {+ let test_storage =+ TestStorage::new(RuntimeHandle::current(), vec![], DownloadManagerType::Universal, HUGGING_FACE_URL, None)+ .await?;+ let storage = &test_storage.storage;+ let cached = catalog_model("cached", &[("model.bin", HELLO.len() as i64)]);+ let cache_path = storage.cache_model_path(&cached).unwrap();+ let destination = cache_path.join("model.bin");+ tokio::fs::create_dir_all(&cache_path).await?;+ tokio::fs::write(&destination, HELLO).await?;+ storage.refresh(std::slice::from_ref(&cached), true).await?;+ let cached_state = timeout(Duration::from_secs(2), storage.ready_state(&cached.identifier)).await??;+ assert_eq!(cached_state.phase, DownloadPhase::Downloaded {});++ let added = catalog_model("added", &[("model.bin", 3)]);+ storage.refresh(std::slice::from_ref(&added), false).await?;+ assert_eq!(storage.state(&cached.identifier).await?, cached_state);+ assert_eq!(storage.states().await.len(), 2);+ let added_state = timeout(Duration::from_secs(2), storage.ready_state(&added.identifier)).await??;+ assert_eq!(added_state.phase, DownloadPhase::NotDownloaded {});+ storage.refresh(&[], false).await?;+ assert_eq!(storage.state(&cached.identifier).await?, cached_state);+ assert_eq!(storage.state(&added.identifier).await?, added_state);++ storage.refresh(&[added], true).await?;+ assert!(storage.state(&cached.identifier).await.is_err());+ assert_eq!(storage.states().await.len(), 1);+ assert!(!destination.exists(), "a complete catalog must clean obsolete model files");+ Ok(())+}++#[tokio::test(flavor = "multi_thread")]+async fn checkpoint_cleanup_waits_for_complete_catalog_and_skips_locked_downloads()+-> Result<(), Box<dyn std::error::Error>> {+ let test_storage =+ TestStorage::new(RuntimeHandle::current(), vec![], DownloadManagerType::Universal, HUGGING_FACE_URL, None)+ .await?;+ let storage = &test_storage.storage;+ let old = catalog_model("revised", &[("model.bin", HELLO.len() as i64)]);+ let old_path = storage.cache_model_path(&old).unwrap();+ tokio::fs::create_dir_all(&old_path).await?;+ tokio::fs::write(old_path.join("model.bin"), HELLO).await?;+ storage.refresh(std::slice::from_ref(&old), true).await?;+ assert_eq!(storage.ready_state(&old.identifier).await?.phase, DownloadPhase::Downloaded {});++ let mut current = old.clone();+ if let ModelAccessibility::OnDevice {+ source: ModelSource::Registry {+ toolchain_version,+ ..+ },+ } = &mut current.accessibility+ {+ *toolchain_version = "2".into();+ }+ let current_path = storage.cache_model_path(¤t).unwrap();+ tokio::fs::create_dir_all(¤t_path).await?;+ tokio::fs::write(current_path.join("model.bin"), HELLO).await?;+ let lock = DestinationLock::acquire(+ &old_path.join("model.bin"),+ &LockOwner {+ manager_id: "active-downloader".into(),+ instance_id: Uuid::new_v4(),+ },+ )+ .await?;++ storage.refresh(std::slice::from_ref(¤t), true).await?;+ assert_eq!(tokio::fs::read(old_path.join("model.bin")).await?, HELLO);+ assert_eq!(storage.ready_state(¤t.identifier).await?.phase, DownloadPhase::Downloaded {});+ drop(lock);+ storage.refresh(std::slice::from_ref(¤t), false).await?;+ assert!(old_path.exists(), "partial discovery must not remove checkpoints");+ storage.refresh(&[], true).await?;+ assert!(old_path.exists(), "an empty catalog must not erase the cache");+ #[cfg(unix)]+ {+ let link = old_path.join("linked-checkpoint");+ std::os::unix::fs::symlink(&old_path, &link)?;+ timeout(Duration::from_secs(2), storage.refresh(std::slice::from_ref(¤t), true)).await??;+ assert!(old_path.exists(), "cleanup must not traverse linked checkpoint contents");+ tokio::fs::remove_file(link).await?;+ }+ storage.refresh(std::slice::from_ref(¤t), true).await?;+ assert!(!old_path.exists());+ assert_eq!(tokio::fs::read(current_path.join("model.bin")).await?, HELLO);+ Ok(())+}++#[tokio::test(flavor = "multi_thread")]+async fn changed_file_list_replaces_live_task_without_conflicting_with_it() -> Result<(), Box<dyn std::error::Error>> {+ let test_storage =+ TestStorage::new(RuntimeHandle::current(), vec![], DownloadManagerType::Universal, HUGGING_FACE_URL, None)+ .await?;+ let storage = &test_storage.storage;+ let full = catalog_model("changing", &[("model.bin", HELLO.len() as i64), ("config.json", 2)]);+ let reduced = catalog_model("changing", &[("model.bin", HELLO.len() as i64)]);+ let cache_path = storage.cache_model_path(&full).unwrap();+ tokio::fs::create_dir_all(&cache_path).await?;+ tokio::fs::write(cache_path.join("model.bin"), HELLO).await?;+ tokio::fs::write(cache_path.join("config.json"), b"{}").await?;++ for index in 0..20 {+ let (model, expected_bytes) = if index % 2 == 0 {+ (&full, HELLO.len() + 2)+ } else {+ (&reduced, HELLO.len())+ };+ storage.refresh(std::slice::from_ref(model), false).await?;+ let state = timeout(Duration::from_secs(2), storage.ready_state(&model.identifier)).await??;+ assert_eq!(state.phase, DownloadPhase::Downloaded {}, "replacement {index}");+ assert_eq!(state.total_bytes, expected_bytes as i64);+ assert_eq!(state.downloaded_bytes, expected_bytes as i64);+ }+ assert_eq!(tokio::fs::read(cache_path.join("config.json")).await?, b"{}");+ Ok(())+}++fn catalog_model(+ identifier: &str,+ files: &[(&str, i64)],+) -> Model {+ Model::external(+ identifier.to_string(),+ "test".to_string(),+ "Test".to_string(),+ "uzu".to_string(),+ "Uzu".to_string(),+ "1".to_string(),+ vec![],+ ModelAccessibility::OnDevice {+ source: ModelSource::Registry {+ toolchain_version: "1".to_string(),+ repository: None,+ source_repository: None,+ files: files+ .iter()+ .map(|(name, size)| File {+ url: format!("https://example.invalid/{identifier}/{name}"),+ name: (*name).to_string(),+ size: *size,+ hashes: vec![],+ })+ .collect(),+ },+ },+ None,+ )+}diff --git a/crates/legacy/uzu/tests/test_engine.rs b/crates/legacy/uzu/tests/test_engine.rsindex 1b1ab7030..9bef887bf 100644--- a/crates/legacy/uzu/tests/test_engine.rs+++ b/crates/legacy/uzu/tests/test_engine.rs@@ -62,6 +62,9 @@ async fn test_engine_chat() {let stream = session.reply_with_stream(messages, ChatReplyConfig::default()).await;while let Some(progress) = stream.next().await {match progress {+ ChatSessionStreamChunk::ToolResults {+ ..+ } => {},ChatSessionStreamChunk::Replies {replies,} => {diff --git a/crates/uzu-engine/src/config/model/generation.rs b/crates/uzu-engine/src/config/model/generation.rsindex 753be16c6..47ead823b 100644--- a/crates/uzu-engine/src/config/model/generation.rs+++ b/crates/uzu-engine/src/config/model/generation.rs@@ -1,6 +1,6 @@use uzu_engine_macros::uzu_config;-use crate::utils::strict_serde::Unsupported;+use crate::{encodable_block::sampling::SamplingMethod, utils::strict_serde::Unsupported};#[uzu_config]pub struct GenerationConfig {@@ -15,3 +15,16 @@ pub struct GenerationConfig {pub frequency_penalty: Option<Unsupported>,pub suffix_repetition_length: Option<u32>,}++impl GenerationConfig {+ pub fn default_sampling_method(&self) -> SamplingMethod {+ SamplingMethod::Stochastic {+ temperature: self.temperature,+ top_k: self.top_k,+ top_p: self.top_p,+ min_p: self.min_p,+ repetition_penalty: self.repetition_penalty,+ suffix_repetition_length: self.suffix_repetition_length,+ }+ }+}diff --git a/crates/uzu-engine/src/engine/language_model/mod.rs b/crates/uzu-engine/src/engine/language_model/mod.rsindex 1255404b9..168ba1aa6 100644--- a/crates/uzu-engine/src/engine/language_model/mod.rs+++ b/crates/uzu-engine/src/engine/language_model/mod.rs@@ -162,14 +162,7 @@ impl<B: Backend> LanguageModel<B> {}pub fn default_sampling_method(&self) -> SamplingMethod {- SamplingMethod::Stochastic {- temperature: self.generation_config.temperature,- top_k: self.generation_config.top_k,- top_p: self.generation_config.top_p,- min_p: self.generation_config.min_p,- repetition_penalty: self.generation_config.repetition_penalty,- suffix_repetition_length: self.generation_config.suffix_repetition_length,- }+ self.generation_config.default_sampling_method()}pub fn generation_config(&self) -> &GenerationConfig {diff --git a/crates/uzu-engine/src/engine/mod.rs b/crates/uzu-engine/src/engine/mod.rsindex 109adcfe3..8f1f941b6 100644--- a/crates/uzu-engine/src/engine/mod.rs+++ b/crates/uzu-engine/src/engine/mod.rs@@ -6,6 +6,7 @@ use thiserror::Error;use crate::{backends::common::{Backend, Context},config::model::AnyModelConfig,+ encodable_block::sampling::SamplingMethod,engine::capture::CaptureManager,};@@ -59,11 +60,20 @@ pub enum ResolveModelTypeError {}pub fn resolve_model_type(model_path: &Path) -> Result<ModelType, ResolveModelTypeError> {- let config_path = model_path.join("config.json");- let file = File::open(&config_path)?;- let config: AnyModelConfig = serde_json::from_reader(BufReader::new(file))?;- Ok(match config {+ Ok(match read_model_config(model_path)? {AnyModelConfig::LanguageModelConfig(_) => ModelType::LanguageModel,AnyModelConfig::ClassifierModelConfig(_) => ModelType::Classifier,})}++pub fn resolve_model_sampling_method(model_path: &Path) -> Result<Option<SamplingMethod>, ResolveModelTypeError> {+ Ok(match read_model_config(model_path)? {+ AnyModelConfig::LanguageModelConfig(config) => Some(config.generation_config.default_sampling_method()),+ AnyModelConfig::ClassifierModelConfig(_) => None,+ })+}++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))?)+}diff --git a/docs/snippets/generated/examples/rust/chat.mdx b/docs/snippets/generated/examples/rust/chat.mdxindex c4d3cf338..e6dfc14a9 100644--- a/docs/snippets/generated/examples/rust/chat.mdx+++ b/docs/snippets/generated/examples/rust/chat.mdx@@ -29,6 +29,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {let mut last_message: Option<ChatMessage> = None;while let Some(chunk) = stream.next().await {match chunk {+ ChatSessionStreamChunk::ToolResults {+ ..+ } => {},ChatSessionStreamChunk::Replies {replies,} => {diff --git a/docs/snippets/generated/examples/swift/chat.mdx b/docs/snippets/generated/examples/swift/chat.mdxindex 9eb8f172e..3cadc7114 100644--- a/docs/snippets/generated/examples/swift/chat.mdx+++ b/docs/snippets/generated/examples/swift/chat.mdx@@ -30,6 +30,8 @@ public func runChat() async throws {print("Generated tokens: \(reply?.stats.tokensCountOutput ?? 0)")case .error(let error):print("Error: \(error)")+ case .toolResults:+ break}}print("Reasoning: \(message?.reasoning() ?? "empty")")diff --git a/flake.nix b/flake.nixindex 07ebf6d68..699c3d4f9 100644--- a/flake.nix+++ b/flake.nix@@ -110,6 +110,8 @@[niluv+ nodejs+ pnpm_10wasmtimeevcxrcargo-deny