diff --git a/.github/workflows/notify-discord.yml b/.github/workflows/notify-discord.yml new file mode 100644 index 00000000..c9032c30 --- /dev/null +++ b/.github/workflows/notify-discord.yml @@ -0,0 +1,14 @@ +name: discord + +on: + release: + types: [published] # fires only when a release is published + +jobs: + notify: + runs-on: ubuntu-latest + steps: + - name: Send nicely-formatted embed to Discord + uses: SethCohen/github-releases-to-discord@v1 + with: + webhook_url: ${{ secrets.DISCORD_WEBHOOK }} diff --git a/.github/workflows/opencode.yml b/.github/workflows/opencode.yml new file mode 100644 index 00000000..b2d5dacc --- /dev/null +++ b/.github/workflows/opencode.yml @@ -0,0 +1,24 @@ +name: opencode + +on: + issue_comment: + types: [created] + +jobs: + opencode: + if: startsWith(github.event.comment.body, 'hey opencode') + runs-on: ubuntu-latest + permissions: + id-token: write + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 1 + + - name: Run opencode + uses: sst/opencode/sdks/github@github-v1 + env: + ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} + with: + model: anthropic/claude-sonnet-4-20250514 diff --git a/.github/workflows/publish-github-action.yml b/.github/workflows/publish-github-action.yml new file mode 100644 index 00000000..e4b4ae75 --- /dev/null +++ b/.github/workflows/publish-github-action.yml @@ -0,0 +1,29 @@ +name: publish-github-action + +on: + workflow_dispatch: + +concurrency: ${{ github.workflow }}-${{ github.ref }} + +permissions: + contents: write + +jobs: + publish: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - run: git fetch --force --tags + + - uses: oven-sh/setup-bun@v2 + with: + bun-version: 1.2.17 + + - name: Publish + run: | + git config --global user.email "opencode@sst.dev" + git config --global user.name "opencode" + ./scripts/publish-github-action.ts diff --git a/.github/workflows/stats.yml b/.github/workflows/stats.yml index fed03b68..188996aa 100644 --- a/.github/workflows/stats.yml +++ b/.github/workflows/stats.yml @@ -28,5 +28,5 @@ jobs: git config --local user.email "action@github.com" git config --local user.name "GitHub Action" git add STATS.md - git diff --staged --quiet || git commit -m "Update download stats $(date -I)" + git diff --staged --quiet || git commit -m "ignore: update download stats $(date -I)" git push diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 00000000..d6aaf1bd --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,15 @@ +# TUI Agent Guidelines + +## Style + +- prefer single word variable/function names +- avoid try catch where possible - prefer to let exceptions bubble up +- avoid else statements where possible +- do not make useless helper functions - inline functionality unless the + function is reusable or composable +- prefer Bun apis + +## Workflow + +- you can regenerate the golang sdk by calling ./scripts/stainless.ts +- we use bun for everything diff --git a/README.md b/README.md index 24841799..3a40bc4c 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@

AI coding agent, built for the terminal.

- View docs + Discord npm Build status

@@ -30,7 +30,8 @@ brew install sst/tap/opencode # macOS paru -S opencode-bin # Arch Linux ``` -> **Note:** Remove versions older than 0.1.x before installing +> [!TIP] +> Remove versions older than 0.1.x before installing. ### Documentation @@ -38,10 +39,25 @@ For more info on how to configure opencode [**head over to our docs**](https://o ### Contributing -For any new features we'd appreciate it if you could open an issue first to discuss what you'd like to implement. We're pretty responsive there and it'll save you from working on something that we don't end up using. No need to do this for simpler fixes. +opencode is an opinionated tool so any fundamental feature needs to go through a +design process with the core team. -> **Note**: Please talk to us via github issues before spending time working on -> a new feature +> [!IMPORTANT] +> We do not accept PRs for core features. + +However we still merge a ton of PRs - you can contribute: + +- Bug fixes +- Improvements to LLM performance +- Support for new providers +- Fixes for env specific quirks +- Missing standard behavior +- Documentation + +Take a look at the git history to see what kind of PRs we end up merging. + +> [!NOTE] +> If you do not follow the above guidelines we might close your PR. To run opencode locally you need. @@ -76,4 +92,4 @@ The other confusingly named repo has no relation to this one. You can [read the --- -**Join our community** [YouTube](https://www.youtube.com/c/sst-dev) | [X.com](https://x.com/SST_dev) +**Join our community** [Discord](https://discord.gg/opencode) | [YouTube](https://www.youtube.com/c/sst-dev) | [X.com](https://x.com/SST_dev) diff --git a/STATS.md b/STATS.md index 256ffb5d..8934a878 100644 --- a/STATS.md +++ b/STATS.md @@ -1,10 +1,23 @@ # Download Stats -| Date | GitHub Downloads | npm Downloads | Total | -| ---------- | ---------------- | --------------- | --------------- | -| 2025-06-29 | 18,789 (+0) | 39,420 (+0) | 58,209 (+0) | -| 2025-06-30 | 20,127 (+1,338) | 41,059 (+1,639) | 61,186 (+2,977) | -| 2025-07-01 | 22,108 (+1,981) | 43,745 (+2,686) | 65,853 (+4,667) | -| 2025-07-02 | 24,814 (+2,706) | 46,168 (+2,423) | 70,982 (+5,129) | -| 2025-07-03 | 27,834 (+3,020) | 49,955 (+3,787) | 77,789 (+6,807) | -| 2025-07-04 | 30,608 (+2,774) | 54,758 (+4,803) | 85,366 (+7,577) | +| Date | GitHub Downloads | npm Downloads | Total | +| ---------- | ---------------- | ---------------- | ----------------- | +| 2025-06-29 | 18,789 (+0) | 39,420 (+0) | 58,209 (+0) | +| 2025-06-30 | 20,127 (+1,338) | 41,059 (+1,639) | 61,186 (+2,977) | +| 2025-07-01 | 22,108 (+1,981) | 43,745 (+2,686) | 65,853 (+4,667) | +| 2025-07-02 | 24,814 (+2,706) | 46,168 (+2,423) | 70,982 (+5,129) | +| 2025-07-03 | 27,834 (+3,020) | 49,955 (+3,787) | 77,789 (+6,807) | +| 2025-07-04 | 30,608 (+2,774) | 54,758 (+4,803) | 85,366 (+7,577) | +| 2025-07-05 | 32,524 (+1,916) | 58,371 (+3,613) | 90,895 (+5,529) | +| 2025-07-06 | 33,766 (+1,242) | 59,694 (+1,323) | 93,460 (+2,565) | +| 2025-07-08 | 38,052 (+4,286) | 64,468 (+4,774) | 102,520 (+9,060) | +| 2025-07-10 | 43,796 (+5,744) | 71,402 (+6,934) | 115,198 (+12,678) | +| 2025-07-11 | 46,982 (+3,186) | 77,462 (+6,060) | 124,444 (+9,246) | +| 2025-07-12 | 49,302 (+2,320) | 82,177 (+4,715) | 131,479 (+7,035) | +| 2025-07-13 | 50,803 (+1,501) | 86,394 (+4,217) | 137,197 (+5,718) | +| 2025-07-14 | 53,283 (+2,480) | 87,860 (+1,466) | 141,143 (+3,946) | +| 2025-07-15 | 57,590 (+4,307) | 91,036 (+3,176) | 148,626 (+7,483) | +| 2025-07-16 | 62,313 (+4,723) | 95,258 (+4,222) | 157,571 (+8,945) | +| 2025-07-17 | 66,684 (+4,371) | 100,048 (+4,790) | 166,732 (+9,161) | +| 2025-07-18 | 70,379 (+3,695) | 102,587 (+2,539) | 172,966 (+6,234) | +| 2025-07-18 | 70,380 (+1) | 102,587 (+0) | 172,967 (+1) | diff --git a/bun.lock b/bun.lock index 0fec03e8..f789eb74 100644 --- a/bun.lock +++ b/bun.lock @@ -11,6 +11,11 @@ "packages/function": { "name": "@opencode/function", "version": "0.0.1", + "dependencies": { + "@octokit/auth-app": "8.0.1", + "@octokit/rest": "22.0.0", + "jose": "6.0.11", + }, "devDependencies": { "@cloudflare/workers-types": "4.20250522.0", "@types/node": "catalog:", @@ -25,38 +30,34 @@ }, "dependencies": { "@clack/prompts": "0.11.0", - "@flystorage/file-storage": "1.1.0", - "@flystorage/local-fs": "1.1.0", - "@hono/zod-validator": "0.5.0", + "@hono/zod-validator": "0.4.2", + "@modelcontextprotocol/sdk": "1.15.1", "@openauthjs/openauth": "0.4.3", - "@standard-schema/spec": "1.0.0", "ai": "catalog:", "decimal.js": "10.5.0", "diff": "8.0.2", - "env-paths": "3.0.0", "hono": "4.7.10", "hono-openapi": "0.4.8", "isomorphic-git": "1.32.1", "open": "10.1.2", "remeda": "2.22.3", - "ts-lsp-client": "1.0.3", "turndown": "7.2.0", "vscode-jsonrpc": "8.2.1", - "vscode-languageclient": "8", "xdg-basedir": "5.1.0", "yargs": "18.0.0", "zod": "catalog:", - "zod-openapi": "4.2.4", - "zod-validation-error": "3.5.2", + "zod-openapi": "4.1.0", }, "devDependencies": { "@ai-sdk/amazon-bedrock": "2.2.10", "@ai-sdk/anthropic": "1.2.12", + "@standard-schema/spec": "1.0.0", "@tsconfig/bun": "1.0.7", "@types/bun": "latest", "@types/turndown": "5.0.5", "@types/yargs": "17.0.33", "typescript": "catalog:", + "vscode-languageserver-types": "3.17.5", "zod-to-json-schema": "3.24.5", }, }, @@ -80,10 +81,11 @@ "marked": "15.0.12", "marked-shiki": "1.2.0", "rehype-autolink-headings": "7.1.0", + "remeda": "2.26.0", "sharp": "0.32.5", "shiki": "3.4.2", "solid-js": "1.9.7", - "toolbeam-docs-theme": "0.4.1", + "toolbeam-docs-theme": "0.4.3", }, "devDependencies": { "@types/node": "catalog:", @@ -96,38 +98,30 @@ "sharp", "esbuild", ], - "patchedDependencies": { - "ai@4.3.16": "patches/ai@4.3.16.patch", - }, - "overrides": { - "zod": "3.24.2", - }, "catalog": { "@types/node": "22.13.9", - "ai": "4.3.16", + "ai": "5.0.0-beta.21", "typescript": "5.8.2", - "zod": "3.24.2", + "zod": "3.25.49", }, "packages": { "@ai-sdk/amazon-bedrock": ["@ai-sdk/amazon-bedrock@2.2.10", "", { "dependencies": { "@ai-sdk/provider": "1.1.3", "@ai-sdk/provider-utils": "2.2.8", "@smithy/eventstream-codec": "^4.0.1", "@smithy/util-utf8": "^4.0.0", "aws4fetch": "^1.0.20" }, "peerDependencies": { "zod": "^3.0.0" } }, "sha512-icLGO7Q0NinnHIPgT+y1QjHVwH4HwV+brWbvM+FfCG2Afpa89PyKa3Ret91kGjZpBgM/xnj1B7K5eM+rRlsXQA=="], "@ai-sdk/anthropic": ["@ai-sdk/anthropic@1.2.12", "", { "dependencies": { "@ai-sdk/provider": "1.1.3", "@ai-sdk/provider-utils": "2.2.8" }, "peerDependencies": { "zod": "^3.0.0" } }, "sha512-YSzjlko7JvuiyQFmI9RN1tNZdEiZxc+6xld/0tq/VkJaHpEzGAb1yiNxxvmYVcjvfu/PcvCxAAYXmTYQQ63IHQ=="], - "@ai-sdk/provider": ["@ai-sdk/provider@1.1.3", "", { "dependencies": { "json-schema": "^0.4.0" } }, "sha512-qZMxYJ0qqX/RfnuIaab+zp8UAeJn/ygXXAffR5I4N0n1IrvA6qBsjc8hXLmBiMV2zoXlifkacF7sEFnYnjBcqg=="], + "@ai-sdk/gateway": ["@ai-sdk/gateway@1.0.0-beta.8", "", { "dependencies": { "@ai-sdk/provider": "2.0.0-beta.1", "@ai-sdk/provider-utils": "3.0.0-beta.3" }, "peerDependencies": { "zod": "^3.25.49 || ^4" } }, "sha512-D2SqYRT/42JTiRxUuiWtn5cYQFscpb9Z14UNvJx7lnurBUXx57zy7TbLH0h7O+WbCluTQN5G6146JpUZ/SRyzw=="], - "@ai-sdk/provider-utils": ["@ai-sdk/provider-utils@2.2.8", "", { "dependencies": { "@ai-sdk/provider": "1.1.3", "nanoid": "^3.3.8", "secure-json-parse": "^2.7.0" }, "peerDependencies": { "zod": "^3.23.8" } }, "sha512-fqhG+4sCVv8x7nFzYnFo19ryhAa3w096Kmc3hWxMQfW/TubPOmt3A6tYZhl4mUfQWWQMsuSkLrtjlWuXBVSGQA=="], + "@ai-sdk/provider": ["@ai-sdk/provider@2.0.0-beta.1", "", { "dependencies": { "json-schema": "^0.4.0" } }, "sha512-Z8SPncMtS3RsoXITmT7NVwrAq6M44dmw0DoUOYJqNNtCu8iMWuxB8Nxsoqpa0uEEy9R1V1ZThJAXTYgjTUxl3w=="], - "@ai-sdk/react": ["@ai-sdk/react@1.2.12", "", { "dependencies": { "@ai-sdk/provider-utils": "2.2.8", "@ai-sdk/ui-utils": "1.2.11", "swr": "^2.2.5", "throttleit": "2.1.0" }, "peerDependencies": { "react": "^18 || ^19 || ^19.0.0-rc", "zod": "^3.23.8" }, "optionalPeers": ["zod"] }, "sha512-jK1IZZ22evPZoQW3vlkZ7wvjYGYF+tRBKXtrcolduIkQ/m/sOAVcVeVDUDvh1T91xCnWCdUGCPZg2avZ90mv3g=="], - - "@ai-sdk/ui-utils": ["@ai-sdk/ui-utils@1.2.11", "", { "dependencies": { "@ai-sdk/provider": "1.1.3", "@ai-sdk/provider-utils": "2.2.8", "zod-to-json-schema": "^3.24.1" }, "peerDependencies": { "zod": "^3.23.8" } }, "sha512-3zcwCc8ezzFlwp3ZD15wAPjf2Au4s3vAbKsXQVyhxODHcmu0iyPO2Eua6D/vicq/AUm/BAo60r97O6HU+EI0+w=="], + "@ai-sdk/provider-utils": ["@ai-sdk/provider-utils@3.0.0-beta.3", "", { "dependencies": { "@ai-sdk/provider": "2.0.0-beta.1", "@standard-schema/spec": "^1.0.0", "eventsource-parser": "^3.0.3", "zod-to-json-schema": "^3.24.1" }, "peerDependencies": { "zod": "^3.25.49 || ^4" } }, "sha512-4gZ392GxjzMF7TnReF2eTKhOSyiSS3ydRVq4I7jxkeV5sdEuMoH3gzfItmlctsqGxlMU1/+zKPwl5yYz9O2dzg=="], "@ampproject/remapping": ["@ampproject/remapping@2.3.0", "", { "dependencies": { "@jridgewell/gen-mapping": "^0.3.5", "@jridgewell/trace-mapping": "^0.3.24" } }, "sha512-30iZtAPgz+LTIYoeivqYo853f02jBYSd5uGnGpkFV0M3xOt9aN73erkgYAmZU43x4VfqcnLxW9Kpg3R5LC4YYw=="], "@apidevtools/json-schema-ref-parser": ["@apidevtools/json-schema-ref-parser@11.9.3", "", { "dependencies": { "@jsdevtools/ono": "^7.1.3", "@types/json-schema": "^7.0.15", "js-yaml": "^4.1.0" } }, "sha512-60vepv88RwcJtSHrD6MjIL6Ta3SOYbgfnkHb+ppAVK+o9mXprRtulx7VlRl3lN3bbvysAfCS7WMVfhUYemB0IQ=="], - "@astrojs/cloudflare": ["@astrojs/cloudflare@12.5.4", "", { "dependencies": { "@astrojs/internal-helpers": "0.6.1", "@astrojs/underscore-redirects": "0.6.1", "@cloudflare/workers-types": "^4.20250507.0", "tinyglobby": "^0.2.13", "vite": "^6.3.5", "wrangler": "^4.14.1" }, "peerDependencies": { "astro": "^5.0.0" } }, "sha512-WKUeMP2tIbddEu0tlVEPj8o9m/8CJB6who3a3jupuIyR56ltmW924ZOMYtp/C9uxH7KeDJXrMszRj3LHs9U97w=="], + "@astrojs/cloudflare": ["@astrojs/cloudflare@12.6.0", "", { "dependencies": { "@astrojs/internal-helpers": "0.6.1", "@astrojs/underscore-redirects": "1.0.0", "@cloudflare/workers-types": "^4.20250507.0", "tinyglobby": "^0.2.13", "vite": "^6.3.5", "wrangler": "^4.14.1" }, "peerDependencies": { "astro": "^5.0.0" } }, "sha512-pQ8bokC59GEiXvyXpC4swBNoL7C/EknP+82KFzQwgR/Aeo5N1oPiAoPHgJbpPya/YF4E26WODdCQfBQDvLRfuw=="], - "@astrojs/compiler": ["@astrojs/compiler@2.12.0", "", {}, "sha512-7bCjW6tVDpUurQLeKBUN9tZ5kSv5qYrGmcn0sG0IwacL7isR2ZbyyA3AdZ4uxsuUFOS2SlgReTH7wkxO6zpqWA=="], + "@astrojs/compiler": ["@astrojs/compiler@2.12.2", "", {}, "sha512-w2zfvhjNCkNMmMMOn5b0J8+OmUaBL1o40ipMvqcG6NRpdC+lKxmTi48DT8Xw0SzJ3AfmeFLB45zXZXtmbsjcgw=="], "@astrojs/internal-helpers": ["@astrojs/internal-helpers@0.6.1", "", {}, "sha512-l5Pqf6uZu31aG+3Lv8nl/3s4DbUzdlxTWDof4pEpto6GUJNhhCbelVi9dEyurOVyqaelwmS9oSyOWOENSfgo9A=="], @@ -137,7 +131,7 @@ "@astrojs/prism": ["@astrojs/prism@3.2.0", "", { "dependencies": { "prismjs": "^1.29.0" } }, "sha512-GilTHKGCW6HMq7y3BUv9Ac7GMe/MO9gi9GW62GzKtth0SwukCu/qp2wLiGpEujhY+VVhaG9v7kv/5vFzvf4NYw=="], - "@astrojs/sitemap": ["@astrojs/sitemap@3.4.0", "", { "dependencies": { "sitemap": "^8.0.0", "stream-replace-string": "^2.0.0", "zod": "^3.24.2" } }, "sha512-C5m/xsKvRSILKM3hy47n5wKtTQtJXn8epoYuUmCCstaE9XBt20yInym3Bz2uNbEiNfv11bokoW0MqeXPIvjFIQ=="], + "@astrojs/sitemap": ["@astrojs/sitemap@3.4.1", "", { "dependencies": { "sitemap": "^8.0.0", "stream-replace-string": "^2.0.0", "zod": "^3.24.2" } }, "sha512-VjZvr1e4FH6NHyyHXOiQgLiw94LnCVY4v06wN/D0gZKchTMkg71GrAHJz81/huafcmavtLkIv26HnpfDq6/h/Q=="], "@astrojs/solid-js": ["@astrojs/solid-js@5.1.0", "", { "dependencies": { "vite": "^6.3.5", "vite-plugin-solid": "^2.11.6" }, "peerDependencies": { "solid-devtools": "^0.30.1", "solid-js": "^1.8.5" }, "optionalPeers": ["solid-devtools"] }, "sha512-VmPHOU9k7m6HHCT2Y1mNzifilUnttlowBM36frGcfj5wERJE9Ci0QtWJbzdf6AlcoIirb7xVw+ByupU011Di9w=="], @@ -145,24 +139,26 @@ "@astrojs/telemetry": ["@astrojs/telemetry@3.2.1", "", { "dependencies": { "ci-info": "^4.2.0", "debug": "^4.4.0", "dlv": "^1.1.3", "dset": "^3.1.4", "is-docker": "^3.0.0", "is-wsl": "^3.1.0", "which-pm-runs": "^1.1.0" } }, "sha512-SSVM820Jqc6wjsn7qYfV9qfeQvePtVc1nSofhyap7l0/iakUKywj3hfy3UJAOV4sGV4Q/u450RD4AaCaFvNPlg=="], - "@astrojs/underscore-redirects": ["@astrojs/underscore-redirects@0.6.1", "", {}, "sha512-4bMLrs2KW+8/vHEE5Ffv2HbxCbbgXO+2N6MpoCsMXUlUoi7pgEEx8kbkzMXJ2dZtWF3gvwm9lvgjnFeanC2LGg=="], + "@astrojs/underscore-redirects": ["@astrojs/underscore-redirects@1.0.0", "", {}, "sha512-qZxHwVnmb5FXuvRsaIGaqWgnftjCuMY+GSbaVZdBmE4j8AfgPqKPxYp8SUERyJcjpKCEmO4wD6ybuGH8A2kVRQ=="], "@aws-crypto/crc32": ["@aws-crypto/crc32@5.2.0", "", { "dependencies": { "@aws-crypto/util": "^5.2.0", "@aws-sdk/types": "^3.222.0", "tslib": "^2.6.2" } }, "sha512-nLbCWqQNgUiwwtFsen1AdzAtvuLRsQS8rYgMuxCrdKf9kOssamGLuPwyTY9wyYblNr9+1XM8v6zoDTPPSIeANg=="], "@aws-crypto/util": ["@aws-crypto/util@5.2.0", "", { "dependencies": { "@aws-sdk/types": "^3.222.0", "@smithy/util-utf8": "^2.0.0", "tslib": "^2.6.2" } }, "sha512-4RkU9EsI6ZpBve5fseQlGNUWKMa1RLPQ1dnjnQoe07ldfIzcsGb5hC5W0Dm7u423KWzawlrpbjXBrXCEv9zazQ=="], - "@aws-sdk/types": ["@aws-sdk/types@3.821.0", "", { "dependencies": { "@smithy/types": "^4.3.1", "tslib": "^2.6.2" } }, "sha512-Znroqdai1a90TlxGaJ+FK1lwC0fHpo97Xjsp5UKGR5JODYm7f9+/fF17ebO1KdoBr/Rm0UIFiF5VmI8ts9F1eA=="], + "@aws-sdk/types": ["@aws-sdk/types@3.840.0", "", { "dependencies": { "@smithy/types": "^4.3.1", "tslib": "^2.6.2" } }, "sha512-xliuHaUFZxEx1NSXeLLZ9Dyu6+EJVQKEoD+yM+zqUo3YDZ7medKJWY6fIOKiPX/N7XbLdBYwajb15Q7IL8KkeA=="], "@babel/code-frame": ["@babel/code-frame@7.27.1", "", { "dependencies": { "@babel/helper-validator-identifier": "^7.27.1", "js-tokens": "^4.0.0", "picocolors": "^1.1.1" } }, "sha512-cjQ7ZlQ0Mv3b47hABuTevyTuYN4i+loJKGeV9flcCgIK37cCXRh+L1bd3iBHlynerhQ7BhCkn2BPbQUL+rGqFg=="], - "@babel/compat-data": ["@babel/compat-data@7.27.3", "", {}, "sha512-V42wFfx1ymFte+ecf6iXghnnP8kWTO+ZLXIyZq+1LAXHHvTZdVxicn4yiVYdYMGaCO3tmqub11AorKkv+iodqw=="], + "@babel/compat-data": ["@babel/compat-data@7.28.0", "", {}, "sha512-60X7qkglvrap8mn1lh2ebxXdZYtUcpd7gsmy9kLaBJ4i/WdY8PqTSdxyA8qraikqKQK5C1KRBKXqznrVapyNaw=="], - "@babel/core": ["@babel/core@7.27.4", "", { "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.27.1", "@babel/generator": "^7.27.3", "@babel/helper-compilation-targets": "^7.27.2", "@babel/helper-module-transforms": "^7.27.3", "@babel/helpers": "^7.27.4", "@babel/parser": "^7.27.4", "@babel/template": "^7.27.2", "@babel/traverse": "^7.27.4", "@babel/types": "^7.27.3", "convert-source-map": "^2.0.0", "debug": "^4.1.0", "gensync": "^1.0.0-beta.2", "json5": "^2.2.3", "semver": "^6.3.1" } }, "sha512-bXYxrXFubeYdvB0NhD/NBB3Qi6aZeV20GOWVI47t2dkecCEoneR4NPVcb7abpXDEvejgrUfFtG6vG/zxAKmg+g=="], + "@babel/core": ["@babel/core@7.28.0", "", { "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.27.1", "@babel/generator": "^7.28.0", "@babel/helper-compilation-targets": "^7.27.2", "@babel/helper-module-transforms": "^7.27.3", "@babel/helpers": "^7.27.6", "@babel/parser": "^7.28.0", "@babel/template": "^7.27.2", "@babel/traverse": "^7.28.0", "@babel/types": "^7.28.0", "convert-source-map": "^2.0.0", "debug": "^4.1.0", "gensync": "^1.0.0-beta.2", "json5": "^2.2.3", "semver": "^6.3.1" } }, "sha512-UlLAnTPrFdNGoFtbSXwcGFQBtQZJCNjaN6hQNP3UPvuNXT1i82N26KL3dZeIpNalWywr9IuQuncaAfUaS1g6sQ=="], - "@babel/generator": ["@babel/generator@7.27.3", "", { "dependencies": { "@babel/parser": "^7.27.3", "@babel/types": "^7.27.3", "@jridgewell/gen-mapping": "^0.3.5", "@jridgewell/trace-mapping": "^0.3.25", "jsesc": "^3.0.2" } }, "sha512-xnlJYj5zepml8NXtjkG0WquFUv8RskFqyFcVgTBp5k+NaA/8uw/K+OSVf8AMGw5e9HKP2ETd5xpK5MLZQD6b4Q=="], + "@babel/generator": ["@babel/generator@7.28.0", "", { "dependencies": { "@babel/parser": "^7.28.0", "@babel/types": "^7.28.0", "@jridgewell/gen-mapping": "^0.3.12", "@jridgewell/trace-mapping": "^0.3.28", "jsesc": "^3.0.2" } }, "sha512-lJjzvrbEeWrhB4P3QBsH7tey117PjLZnDbLiQEKjQ/fNJTjuq4HSqgFA+UNSwZT8D7dxxbnuSBMsa1lrWzKlQg=="], "@babel/helper-compilation-targets": ["@babel/helper-compilation-targets@7.27.2", "", { "dependencies": { "@babel/compat-data": "^7.27.2", "@babel/helper-validator-option": "^7.27.1", "browserslist": "^4.24.0", "lru-cache": "^5.1.1", "semver": "^6.3.1" } }, "sha512-2+1thGUUWWjLTYTHZWK1n8Yga0ijBz1XAhUXcKy81rd5g6yh7hGqMp45v7cadSbEHc9G3OTv45SyneRN3ps4DQ=="], + "@babel/helper-globals": ["@babel/helper-globals@7.28.0", "", {}, "sha512-+W6cISkXFa1jXsDEdYA8HeevQT/FULhxzR99pxphltZcVaugps53THCeiWA8SguxxpSp3gKPiuYfSWopkLQ4hw=="], + "@babel/helper-module-imports": ["@babel/helper-module-imports@7.27.1", "", { "dependencies": { "@babel/traverse": "^7.27.1", "@babel/types": "^7.27.1" } }, "sha512-0gSFWUPNXNopqtIPQvlD5WgXYI5GY2kP2cCvoT8kczjbfcfuIljTbcWrulD1CIPIX2gt1wghbDy08yE1p+/r3w=="], "@babel/helper-module-transforms": ["@babel/helper-module-transforms@7.27.3", "", { "dependencies": { "@babel/helper-module-imports": "^7.27.1", "@babel/helper-validator-identifier": "^7.27.1", "@babel/traverse": "^7.27.3" }, "peerDependencies": { "@babel/core": "^7.0.0" } }, "sha512-dSOvYwvyLsWBeIRyOeHXp5vPj5l1I011r52FM1+r1jCERv+aFXYk4whgQccYEGYxK2H3ZAIA8nuPkQ0HaUo3qg=="], @@ -175,19 +171,19 @@ "@babel/helper-validator-option": ["@babel/helper-validator-option@7.27.1", "", {}, "sha512-YvjJow9FxbhFFKDSuFnVCe2WxXk1zWc22fFePVNEaWJEu8IrZVlda6N0uHwzZrUM1il7NC9Mlp4MaJYbYd9JSg=="], - "@babel/helpers": ["@babel/helpers@7.27.4", "", { "dependencies": { "@babel/template": "^7.27.2", "@babel/types": "^7.27.3" } }, "sha512-Y+bO6U+I7ZKaM5G5rDUZiYfUvQPUibYmAFe7EnKdnKBbVXDZxvp+MWOH5gYciY0EPk4EScsuFMQBbEfpdRKSCQ=="], + "@babel/helpers": ["@babel/helpers@7.27.6", "", { "dependencies": { "@babel/template": "^7.27.2", "@babel/types": "^7.27.6" } }, "sha512-muE8Tt8M22638HU31A3CgfSUciwz1fhATfoVai05aPXGor//CdWDCbnlY1yvBPo07njuVOCNGCSp/GTt12lIug=="], - "@babel/parser": ["@babel/parser@7.27.4", "", { "dependencies": { "@babel/types": "^7.27.3" }, "bin": "./bin/babel-parser.js" }, "sha512-BRmLHGwpUqLFR2jzx9orBuX/ABDkj2jLKOXrHDTN2aOKL+jFDDKaRNo9nyYsIl9h/UE/7lMKdDjKQQyxKKDZ7g=="], + "@babel/parser": ["@babel/parser@7.28.0", "", { "dependencies": { "@babel/types": "^7.28.0" }, "bin": "./bin/babel-parser.js" }, "sha512-jVZGvOxOuNSsuQuLRTh13nU0AogFlw32w/MT+LV6D3sP5WdbW61E77RnkbaO2dUvmPAYrBDJXGn5gGS6tH4j8g=="], "@babel/plugin-syntax-jsx": ["@babel/plugin-syntax-jsx@7.27.1", "", { "dependencies": { "@babel/helper-plugin-utils": "^7.27.1" }, "peerDependencies": { "@babel/core": "^7.0.0-0" } }, "sha512-y8YTNIeKoyhGd9O0Jiyzyyqk8gdjnumGTQPsz0xOZOQ2RmkVJeZ1vmmfIvFEKqucBG6axJGBZDE/7iI5suUI/w=="], - "@babel/runtime": ["@babel/runtime@7.27.4", "", {}, "sha512-t3yaEOuGu9NlIZ+hIeGbBjFtZT7j2cb2tg0fuaJKeGotchRjjLfrBA9Kwf8quhpP1EUuxModQg04q/mBwyg8uA=="], + "@babel/runtime": ["@babel/runtime@7.27.6", "", {}, "sha512-vbavdySgbTTrmFE+EsiqUTzlOr5bzlnJtUv9PynGCAKvfQqjIXbvFdumPM/GxMDfyuGMJaJAU6TO4zc1Jf1i8Q=="], "@babel/template": ["@babel/template@7.27.2", "", { "dependencies": { "@babel/code-frame": "^7.27.1", "@babel/parser": "^7.27.2", "@babel/types": "^7.27.1" } }, "sha512-LPDZ85aEJyYSd18/DkjNh4/y1ntkE5KwUHWTiqgRxruuZL2F1yuHligVHLvcHY2vMHXttKFpJn6LwfI7cw7ODw=="], - "@babel/traverse": ["@babel/traverse@7.27.4", "", { "dependencies": { "@babel/code-frame": "^7.27.1", "@babel/generator": "^7.27.3", "@babel/parser": "^7.27.4", "@babel/template": "^7.27.2", "@babel/types": "^7.27.3", "debug": "^4.3.1", "globals": "^11.1.0" } }, "sha512-oNcu2QbHqts9BtOWJosOVJapWjBDSxGCpFvikNR5TGDYDQf3JwpIoMzIKrvfoti93cLfPJEG4tH9SPVeyCGgdA=="], + "@babel/traverse": ["@babel/traverse@7.28.0", "", { "dependencies": { "@babel/code-frame": "^7.27.1", "@babel/generator": "^7.28.0", "@babel/helper-globals": "^7.28.0", "@babel/parser": "^7.28.0", "@babel/template": "^7.27.2", "@babel/types": "^7.28.0", "debug": "^4.3.1" } }, "sha512-mGe7UK5wWyh0bKRfupsUchrQGqvDbZDbKJw+kcRGSmdHVYrv+ltd0pnpDTVpiTqnaBru9iEvA8pz8W46v0Amwg=="], - "@babel/types": ["@babel/types@7.27.3", "", { "dependencies": { "@babel/helper-string-parser": "^7.27.1", "@babel/helper-validator-identifier": "^7.27.1" } }, "sha512-Y1GkI4ktrtvmawoSq+4FCVHNryea6uR+qUQy0AGxLSsjCX0nVmkYQMBLHDkXZuo5hGx7eYdnIaslsdBFm7zbUw=="], + "@babel/types": ["@babel/types@7.28.1", "", { "dependencies": { "@babel/helper-string-parser": "^7.27.1", "@babel/helper-validator-identifier": "^7.27.1" } }, "sha512-x0LvFTekgSX+83TI28Y9wYPUfzrnl2aT5+5QLnO6v7mSJYtEEevuDRN0F0uSHRk1G1IWZC43o00Y0xDDrpBGPQ=="], "@capsizecss/unpack": ["@capsizecss/unpack@2.4.0", "", { "dependencies": { "blob-to-buffer": "^1.2.8", "cross-fetch": "^3.0.4", "fontkit": "^2.0.2" } }, "sha512-GrSU71meACqcmIUxPYOJvGKF0yryjN/L1aCuE9DViCTJI7bfkjgYDPD1zbNDcINJwSSP6UaBZY9GAbYDO7re0Q=="], @@ -197,17 +193,17 @@ "@cloudflare/kv-asset-handler": ["@cloudflare/kv-asset-handler@0.4.0", "", { "dependencies": { "mime": "^3.0.0" } }, "sha512-+tv3z+SPp+gqTIcImN9o0hqE9xyfQjI1XD9pL6NuKjua9B1y7mNYv0S9cP+QEbA4ppVgGZEmKOvHX5G5Ei1CVA=="], - "@cloudflare/unenv-preset": ["@cloudflare/unenv-preset@2.3.2", "", { "peerDependencies": { "unenv": "2.0.0-rc.17", "workerd": "^1.20250508.0" }, "optionalPeers": ["workerd"] }, "sha512-MtUgNl+QkQyhQvv5bbWP+BpBC1N0me4CHHuP2H4ktmOMKdB/6kkz/lo+zqiA4mEazb4y+1cwyNjVrQ2DWeE4mg=="], + "@cloudflare/unenv-preset": ["@cloudflare/unenv-preset@2.3.3", "", { "peerDependencies": { "unenv": "2.0.0-rc.17", "workerd": "^1.20250508.0" }, "optionalPeers": ["workerd"] }, "sha512-/M3MEcj3V2WHIRSW1eAQBPRJ6JnGQHc6JKMAPLkDb7pLs3m6X9ES/+K3ceGqxI6TKeF32AWAi7ls0AYzVxCP0A=="], - "@cloudflare/workerd-darwin-64": ["@cloudflare/workerd-darwin-64@1.20250525.0", "", { "os": "darwin", "cpu": "x64" }, "sha512-L5l+7sSJJT2+riR5rS3Q3PKNNySPjWfRIeaNGMVRi1dPO6QPi4lwuxfRUFNoeUdilZJUVPfSZvTtj9RedsKznQ=="], + "@cloudflare/workerd-darwin-64": ["@cloudflare/workerd-darwin-64@1.20250709.0", "", { "os": "darwin", "cpu": "x64" }, "sha512-VqwcvnbI8FNCP87ZWNHA3/sAC5U9wMbNnjBG0sHEYzM7B9RPHKYHdVKdBEWhzZXnkQYMK81IHm4CZsK16XxAuQ=="], - "@cloudflare/workerd-darwin-arm64": ["@cloudflare/workerd-darwin-arm64@1.20250525.0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-Y3IbIdrF/vJWh/WBvshwcSyUh175VAiLRW7963S1dXChrZ1N5wuKGQm9xY69cIGVtitpMJWWW3jLq7J/Xxwm0Q=="], + "@cloudflare/workerd-darwin-arm64": ["@cloudflare/workerd-darwin-arm64@1.20250709.0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-A54ttSgXMM4huChPTThhkieOjpDxR+srVOO9zjTHVIyoQxA8zVsku4CcY/GQ95RczMV+yCKVVu/tAME7vwBFuA=="], - "@cloudflare/workerd-linux-64": ["@cloudflare/workerd-linux-64@1.20250525.0", "", { "os": "linux", "cpu": "x64" }, "sha512-KSyQPAby+c6cpENoO0ayCQlY6QIh28l/+QID7VC1SLXfiNHy+hPNsH1vVBTST6CilHVAQSsy9tCZ9O9XECB8yg=="], + "@cloudflare/workerd-linux-64": ["@cloudflare/workerd-linux-64@1.20250709.0", "", { "os": "linux", "cpu": "x64" }, "sha512-no4O3OK+VXINIxv99OHJDpIgML2ZssrSvImwLtULzqm+cl4t1PIfXNRUqj89ujTkmad+L9y4G6dBQMPCLnmlGg=="], - "@cloudflare/workerd-linux-arm64": ["@cloudflare/workerd-linux-arm64@1.20250525.0", "", { "os": "linux", "cpu": "arm64" }, "sha512-Nt0FUxS2kQhJUea4hMCNPaetkrAFDhPnNX/ntwcqVlGgnGt75iaAhupWJbU0GB+gIWlKeuClUUnDZqKbicoKyg=="], + "@cloudflare/workerd-linux-arm64": ["@cloudflare/workerd-linux-arm64@1.20250709.0", "", { "os": "linux", "cpu": "arm64" }, "sha512-7cNICk2Qd+m4QGrcmWyAuZJXTHt1ud6isA+dic7Yk42WZmwXhlcUATyvFD9FSQNFcldjuRB4n8JlWEFqZBn+lw=="], - "@cloudflare/workerd-windows-64": ["@cloudflare/workerd-windows-64@1.20250525.0", "", { "os": "win32", "cpu": "x64" }, "sha512-mwTj+9f3uIa4NEXR1cOa82PjLa6dbrb3J+KCVJFYIaq7e63VxEzOchCXS4tublT2pmOhmFqkgBMXrxozxNkR2Q=="], + "@cloudflare/workerd-windows-64": ["@cloudflare/workerd-windows-64@1.20250709.0", "", { "os": "win32", "cpu": "x64" }, "sha512-j1AyO8V/62Q23EJplWgzBlRCqo/diXgox58AbDqSqgyzCBAlvUzXQRDBab/FPNG/erRqt7I1zQhahrBhrM0uLA=="], "@cloudflare/workers-types": ["@cloudflare/workers-types@4.20250522.0", "", {}, "sha512-9RIffHobc35JWeddzBguGgPa4wLDr5x5F94+0/qy7LiV6pTBQ/M5qGEN9VA16IDT3EUpYI0WKh6VpcmeVEtVtw=="], @@ -215,79 +211,73 @@ "@ctrl/tinycolor": ["@ctrl/tinycolor@4.1.0", "", {}, "sha512-WyOx8cJQ+FQus4Mm4uPIZA64gbk3Wxh0so5Lcii0aJifqwoVOlfFtorjLE0Hen4OYyHZMXDWqMmaQemBhgxFRQ=="], - "@emnapi/runtime": ["@emnapi/runtime@1.4.3", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-pBPWdu6MLKROBX05wSNKcNb++m5Er+KQ9QkB+WVM+pW2Kx9hoSrVTnu3BdkI5eBLZoKu/J6mW/B6i6bJB2ytXQ=="], + "@emnapi/runtime": ["@emnapi/runtime@1.4.4", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-hHyapA4A3gPaDCNfiqyZUStTMqIkKRshqPIuDOXv1hcBnD4U3l8cP0T1HMCfGRxQ6V64TGCcoswChANyOAwbQg=="], - "@esbuild/aix-ppc64": ["@esbuild/aix-ppc64@0.25.5", "", { "os": "aix", "cpu": "ppc64" }, "sha512-9o3TMmpmftaCMepOdA5k/yDw8SfInyzWWTjYTFCX3kPSDJMROQTb8jg+h9Cnwnmm1vOzvxN7gIfB5V2ewpjtGA=="], + "@esbuild/aix-ppc64": ["@esbuild/aix-ppc64@0.25.6", "", { "os": "aix", "cpu": "ppc64" }, "sha512-ShbM/3XxwuxjFiuVBHA+d3j5dyac0aEVVq1oluIDf71hUw0aRF59dV/efUsIwFnR6m8JNM2FjZOzmaZ8yG61kw=="], - "@esbuild/android-arm": ["@esbuild/android-arm@0.25.5", "", { "os": "android", "cpu": "arm" }, "sha512-AdJKSPeEHgi7/ZhuIPtcQKr5RQdo6OO2IL87JkianiMYMPbCtot9fxPbrMiBADOWWm3T2si9stAiVsGbTQFkbA=="], + "@esbuild/android-arm": ["@esbuild/android-arm@0.25.6", "", { "os": "android", "cpu": "arm" }, "sha512-S8ToEOVfg++AU/bHwdksHNnyLyVM+eMVAOf6yRKFitnwnbwwPNqKr3srzFRe7nzV69RQKb5DgchIX5pt3L53xg=="], - "@esbuild/android-arm64": ["@esbuild/android-arm64@0.25.5", "", { "os": "android", "cpu": "arm64" }, "sha512-VGzGhj4lJO+TVGV1v8ntCZWJktV7SGCs3Pn1GRWI1SBFtRALoomm8k5E9Pmwg3HOAal2VDc2F9+PM/rEY6oIDg=="], + "@esbuild/android-arm64": ["@esbuild/android-arm64@0.25.6", "", { "os": "android", "cpu": "arm64" }, "sha512-hd5zdUarsK6strW+3Wxi5qWws+rJhCCbMiC9QZyzoxfk5uHRIE8T287giQxzVpEvCwuJ9Qjg6bEjcRJcgfLqoA=="], - "@esbuild/android-x64": ["@esbuild/android-x64@0.25.5", "", { "os": "android", "cpu": "x64" }, "sha512-D2GyJT1kjvO//drbRT3Hib9XPwQeWd9vZoBJn+bu/lVsOZ13cqNdDeqIF/xQ5/VmWvMduP6AmXvylO/PIc2isw=="], + "@esbuild/android-x64": ["@esbuild/android-x64@0.25.6", "", { "os": "android", "cpu": "x64" }, "sha512-0Z7KpHSr3VBIO9A/1wcT3NTy7EB4oNC4upJ5ye3R7taCc2GUdeynSLArnon5G8scPwaU866d3H4BCrE5xLW25A=="], - "@esbuild/darwin-arm64": ["@esbuild/darwin-arm64@0.25.5", "", { "os": "darwin", "cpu": "arm64" }, "sha512-GtaBgammVvdF7aPIgH2jxMDdivezgFu6iKpmT+48+F8Hhg5J/sfnDieg0aeG/jfSvkYQU2/pceFPDKlqZzwnfQ=="], + "@esbuild/darwin-arm64": ["@esbuild/darwin-arm64@0.25.6", "", { "os": "darwin", "cpu": "arm64" }, "sha512-FFCssz3XBavjxcFxKsGy2DYK5VSvJqa6y5HXljKzhRZ87LvEi13brPrf/wdyl/BbpbMKJNOr1Sd0jtW4Ge1pAA=="], - "@esbuild/darwin-x64": ["@esbuild/darwin-x64@0.25.5", "", { "os": "darwin", "cpu": "x64" }, "sha512-1iT4FVL0dJ76/q1wd7XDsXrSW+oLoquptvh4CLR4kITDtqi2e/xwXwdCVH8hVHU43wgJdsq7Gxuzcs6Iq/7bxQ=="], + "@esbuild/darwin-x64": ["@esbuild/darwin-x64@0.25.6", "", { "os": "darwin", "cpu": "x64" }, "sha512-GfXs5kry/TkGM2vKqK2oyiLFygJRqKVhawu3+DOCk7OxLy/6jYkWXhlHwOoTb0WqGnWGAS7sooxbZowy+pK9Yg=="], - "@esbuild/freebsd-arm64": ["@esbuild/freebsd-arm64@0.25.5", "", { "os": "freebsd", "cpu": "arm64" }, "sha512-nk4tGP3JThz4La38Uy/gzyXtpkPW8zSAmoUhK9xKKXdBCzKODMc2adkB2+8om9BDYugz+uGV7sLmpTYzvmz6Sw=="], + "@esbuild/freebsd-arm64": ["@esbuild/freebsd-arm64@0.25.6", "", { "os": "freebsd", "cpu": "arm64" }, "sha512-aoLF2c3OvDn2XDTRvn8hN6DRzVVpDlj2B/F66clWd/FHLiHaG3aVZjxQX2DYphA5y/evbdGvC6Us13tvyt4pWg=="], - "@esbuild/freebsd-x64": ["@esbuild/freebsd-x64@0.25.5", "", { "os": "freebsd", "cpu": "x64" }, "sha512-PrikaNjiXdR2laW6OIjlbeuCPrPaAl0IwPIaRv+SMV8CiM8i2LqVUHFC1+8eORgWyY7yhQY+2U2fA55mBzReaw=="], + "@esbuild/freebsd-x64": ["@esbuild/freebsd-x64@0.25.6", "", { "os": "freebsd", "cpu": "x64" }, "sha512-2SkqTjTSo2dYi/jzFbU9Plt1vk0+nNg8YC8rOXXea+iA3hfNJWebKYPs3xnOUf9+ZWhKAaxnQNUf2X9LOpeiMQ=="], - "@esbuild/linux-arm": ["@esbuild/linux-arm@0.25.5", "", { "os": "linux", "cpu": "arm" }, "sha512-cPzojwW2okgh7ZlRpcBEtsX7WBuqbLrNXqLU89GxWbNt6uIg78ET82qifUy3W6OVww6ZWobWub5oqZOVtwolfw=="], + "@esbuild/linux-arm": ["@esbuild/linux-arm@0.25.6", "", { "os": "linux", "cpu": "arm" }, "sha512-SZHQlzvqv4Du5PrKE2faN0qlbsaW/3QQfUUc6yO2EjFcA83xnwm91UbEEVx4ApZ9Z5oG8Bxz4qPE+HFwtVcfyw=="], - "@esbuild/linux-arm64": ["@esbuild/linux-arm64@0.25.5", "", { "os": "linux", "cpu": "arm64" }, "sha512-Z9kfb1v6ZlGbWj8EJk9T6czVEjjq2ntSYLY2cw6pAZl4oKtfgQuS4HOq41M/BcoLPzrUbNd+R4BXFyH//nHxVg=="], + "@esbuild/linux-arm64": ["@esbuild/linux-arm64@0.25.6", "", { "os": "linux", "cpu": "arm64" }, "sha512-b967hU0gqKd9Drsh/UuAm21Khpoh6mPBSgz8mKRq4P5mVK8bpA+hQzmm/ZwGVULSNBzKdZPQBRT3+WuVavcWsQ=="], - "@esbuild/linux-ia32": ["@esbuild/linux-ia32@0.25.5", "", { "os": "linux", "cpu": "ia32" }, "sha512-sQ7l00M8bSv36GLV95BVAdhJ2QsIbCuCjh/uYrWiMQSUuV+LpXwIqhgJDcvMTj+VsQmqAHL2yYaasENvJ7CDKA=="], + "@esbuild/linux-ia32": ["@esbuild/linux-ia32@0.25.6", "", { "os": "linux", "cpu": "ia32" }, "sha512-aHWdQ2AAltRkLPOsKdi3xv0mZ8fUGPdlKEjIEhxCPm5yKEThcUjHpWB1idN74lfXGnZ5SULQSgtr5Qos5B0bPw=="], - "@esbuild/linux-loong64": ["@esbuild/linux-loong64@0.25.5", "", { "os": "linux", "cpu": "none" }, "sha512-0ur7ae16hDUC4OL5iEnDb0tZHDxYmuQyhKhsPBV8f99f6Z9KQM02g33f93rNH5A30agMS46u2HP6qTdEt6Q1kg=="], + "@esbuild/linux-loong64": ["@esbuild/linux-loong64@0.25.6", "", { "os": "linux", "cpu": "none" }, "sha512-VgKCsHdXRSQ7E1+QXGdRPlQ/e08bN6WMQb27/TMfV+vPjjTImuT9PmLXupRlC90S1JeNNW5lzkAEO/McKeJ2yg=="], - "@esbuild/linux-mips64el": ["@esbuild/linux-mips64el@0.25.5", "", { "os": "linux", "cpu": "none" }, "sha512-kB/66P1OsHO5zLz0i6X0RxlQ+3cu0mkxS3TKFvkb5lin6uwZ/ttOkP3Z8lfR9mJOBk14ZwZ9182SIIWFGNmqmg=="], + "@esbuild/linux-mips64el": ["@esbuild/linux-mips64el@0.25.6", "", { "os": "linux", "cpu": "none" }, "sha512-WViNlpivRKT9/py3kCmkHnn44GkGXVdXfdc4drNmRl15zVQ2+D2uFwdlGh6IuK5AAnGTo2qPB1Djppj+t78rzw=="], - "@esbuild/linux-ppc64": ["@esbuild/linux-ppc64@0.25.5", "", { "os": "linux", "cpu": "ppc64" }, "sha512-UZCmJ7r9X2fe2D6jBmkLBMQetXPXIsZjQJCjgwpVDz+YMcS6oFR27alkgGv3Oqkv07bxdvw7fyB71/olceJhkQ=="], + "@esbuild/linux-ppc64": ["@esbuild/linux-ppc64@0.25.6", "", { "os": "linux", "cpu": "ppc64" }, "sha512-wyYKZ9NTdmAMb5730I38lBqVu6cKl4ZfYXIs31Baf8aoOtB4xSGi3THmDYt4BTFHk7/EcVixkOV2uZfwU3Q2Jw=="], - "@esbuild/linux-riscv64": ["@esbuild/linux-riscv64@0.25.5", "", { "os": "linux", "cpu": "none" }, "sha512-kTxwu4mLyeOlsVIFPfQo+fQJAV9mh24xL+y+Bm6ej067sYANjyEw1dNHmvoqxJUCMnkBdKpvOn0Ahql6+4VyeA=="], + "@esbuild/linux-riscv64": ["@esbuild/linux-riscv64@0.25.6", "", { "os": "linux", "cpu": "none" }, "sha512-KZh7bAGGcrinEj4qzilJ4hqTY3Dg2U82c8bv+e1xqNqZCrCyc+TL9AUEn5WGKDzm3CfC5RODE/qc96OcbIe33w=="], - "@esbuild/linux-s390x": ["@esbuild/linux-s390x@0.25.5", "", { "os": "linux", "cpu": "s390x" }, "sha512-K2dSKTKfmdh78uJ3NcWFiqyRrimfdinS5ErLSn3vluHNeHVnBAFWC8a4X5N+7FgVE1EjXS1QDZbpqZBjfrqMTQ=="], + "@esbuild/linux-s390x": ["@esbuild/linux-s390x@0.25.6", "", { "os": "linux", "cpu": "s390x" }, "sha512-9N1LsTwAuE9oj6lHMyyAM+ucxGiVnEqUdp4v7IaMmrwb06ZTEVCIs3oPPplVsnjPfyjmxwHxHMF8b6vzUVAUGw=="], - "@esbuild/linux-x64": ["@esbuild/linux-x64@0.25.5", "", { "os": "linux", "cpu": "x64" }, "sha512-uhj8N2obKTE6pSZ+aMUbqq+1nXxNjZIIjCjGLfsWvVpy7gKCOL6rsY1MhRh9zLtUtAI7vpgLMK6DxjO8Qm9lJw=="], + "@esbuild/linux-x64": ["@esbuild/linux-x64@0.25.6", "", { "os": "linux", "cpu": "x64" }, "sha512-A6bJB41b4lKFWRKNrWoP2LHsjVzNiaurf7wyj/XtFNTsnPuxwEBWHLty+ZE0dWBKuSK1fvKgrKaNjBS7qbFKig=="], - "@esbuild/netbsd-arm64": ["@esbuild/netbsd-arm64@0.25.5", "", { "os": "none", "cpu": "arm64" }, "sha512-pwHtMP9viAy1oHPvgxtOv+OkduK5ugofNTVDilIzBLpoWAM16r7b/mxBvfpuQDpRQFMfuVr5aLcn4yveGvBZvw=="], + "@esbuild/netbsd-arm64": ["@esbuild/netbsd-arm64@0.25.6", "", { "os": "none", "cpu": "arm64" }, "sha512-IjA+DcwoVpjEvyxZddDqBY+uJ2Snc6duLpjmkXm/v4xuS3H+3FkLZlDm9ZsAbF9rsfP3zeA0/ArNDORZgrxR/Q=="], - "@esbuild/netbsd-x64": ["@esbuild/netbsd-x64@0.25.5", "", { "os": "none", "cpu": "x64" }, "sha512-WOb5fKrvVTRMfWFNCroYWWklbnXH0Q5rZppjq0vQIdlsQKuw6mdSihwSo4RV/YdQ5UCKKvBy7/0ZZYLBZKIbwQ=="], + "@esbuild/netbsd-x64": ["@esbuild/netbsd-x64@0.25.6", "", { "os": "none", "cpu": "x64" }, "sha512-dUXuZr5WenIDlMHdMkvDc1FAu4xdWixTCRgP7RQLBOkkGgwuuzaGSYcOpW4jFxzpzL1ejb8yF620UxAqnBrR9g=="], - "@esbuild/openbsd-arm64": ["@esbuild/openbsd-arm64@0.25.5", "", { "os": "openbsd", "cpu": "arm64" }, "sha512-7A208+uQKgTxHd0G0uqZO8UjK2R0DDb4fDmERtARjSHWxqMTye4Erz4zZafx7Di9Cv+lNHYuncAkiGFySoD+Mw=="], + "@esbuild/openbsd-arm64": ["@esbuild/openbsd-arm64@0.25.6", "", { "os": "openbsd", "cpu": "arm64" }, "sha512-l8ZCvXP0tbTJ3iaqdNf3pjaOSd5ex/e6/omLIQCVBLmHTlfXW3zAxQ4fnDmPLOB1x9xrcSi/xtCWFwCZRIaEwg=="], - "@esbuild/openbsd-x64": ["@esbuild/openbsd-x64@0.25.5", "", { "os": "openbsd", "cpu": "x64" }, "sha512-G4hE405ErTWraiZ8UiSoesH8DaCsMm0Cay4fsFWOOUcz8b8rC6uCvnagr+gnioEjWn0wC+o1/TAHt+It+MpIMg=="], + "@esbuild/openbsd-x64": ["@esbuild/openbsd-x64@0.25.6", "", { "os": "openbsd", "cpu": "x64" }, "sha512-hKrmDa0aOFOr71KQ/19JC7az1P0GWtCN1t2ahYAf4O007DHZt/dW8ym5+CUdJhQ/qkZmI1HAF8KkJbEFtCL7gw=="], - "@esbuild/sunos-x64": ["@esbuild/sunos-x64@0.25.5", "", { "os": "sunos", "cpu": "x64" }, "sha512-l+azKShMy7FxzY0Rj4RCt5VD/q8mG/e+mDivgspo+yL8zW7qEwctQ6YqKX34DTEleFAvCIUviCFX1SDZRSyMQA=="], + "@esbuild/openharmony-arm64": ["@esbuild/openharmony-arm64@0.25.6", "", { "os": "none", "cpu": "arm64" }, "sha512-+SqBcAWoB1fYKmpWoQP4pGtx+pUUC//RNYhFdbcSA16617cchuryuhOCRpPsjCblKukAckWsV+aQ3UKT/RMPcA=="], - "@esbuild/win32-arm64": ["@esbuild/win32-arm64@0.25.5", "", { "os": "win32", "cpu": "arm64" }, "sha512-O2S7SNZzdcFG7eFKgvwUEZ2VG9D/sn/eIiz8XRZ1Q/DO5a3s76Xv0mdBzVM5j5R639lXQmPmSo0iRpHqUUrsxw=="], + "@esbuild/sunos-x64": ["@esbuild/sunos-x64@0.25.6", "", { "os": "sunos", "cpu": "x64" }, "sha512-dyCGxv1/Br7MiSC42qinGL8KkG4kX0pEsdb0+TKhmJZgCUDBGmyo1/ArCjNGiOLiIAgdbWgmWgib4HoCi5t7kA=="], - "@esbuild/win32-ia32": ["@esbuild/win32-ia32@0.25.5", "", { "os": "win32", "cpu": "ia32" }, "sha512-onOJ02pqs9h1iMJ1PQphR+VZv8qBMQ77Klcsqv9CNW2w6yLqoURLcgERAIurY6QE63bbLuqgP9ATqajFLK5AMQ=="], + "@esbuild/win32-arm64": ["@esbuild/win32-arm64@0.25.6", "", { "os": "win32", "cpu": "arm64" }, "sha512-42QOgcZeZOvXfsCBJF5Afw73t4veOId//XD3i+/9gSkhSV6Gk3VPlWncctI+JcOyERv85FUo7RxuxGy+z8A43Q=="], - "@esbuild/win32-x64": ["@esbuild/win32-x64@0.25.5", "", { "os": "win32", "cpu": "x64" }, "sha512-TXv6YnJ8ZMVdX+SXWVBo/0p8LTcrUYngpWjvm91TMjjBQii7Oz11Lw5lbDV5Y0TzuhSJHwiH4hEtC1I42mMS0g=="], + "@esbuild/win32-ia32": ["@esbuild/win32-ia32@0.25.6", "", { "os": "win32", "cpu": "ia32" }, "sha512-4AWhgXmDuYN7rJI6ORB+uU9DHLq/erBbuMoAuB4VWJTu5KtCgcKYPynF0YI1VkBNuEfjNlLrFr9KZPJzrtLkrQ=="], - "@expressive-code/core": ["@expressive-code/core@0.41.2", "", { "dependencies": { "@ctrl/tinycolor": "^4.0.4", "hast-util-select": "^6.0.2", "hast-util-to-html": "^9.0.1", "hast-util-to-text": "^4.0.1", "hastscript": "^9.0.0", "postcss": "^8.4.38", "postcss-nested": "^6.0.1", "unist-util-visit": "^5.0.0", "unist-util-visit-parents": "^6.0.1" } }, "sha512-AJW5Tp9czbLqKMzwudL9Rv4js9afXBxkSGLmCNPq1iRgAYcx9NkTPJiSNCesjKRWoVC328AdSu6fqrD22zDgDg=="], + "@esbuild/win32-x64": ["@esbuild/win32-x64@0.25.6", "", { "os": "win32", "cpu": "x64" }, "sha512-NgJPHHbEpLQgDH2MjQu90pzW/5vvXIZ7KOnPyNBm92A6WgZ/7b6fJyUBjoumLqeOQQGqY2QjQxRo97ah4Sj0cA=="], - "@expressive-code/plugin-frames": ["@expressive-code/plugin-frames@0.41.2", "", { "dependencies": { "@expressive-code/core": "^0.41.2" } }, "sha512-pfy0hkJI4nbaONjmksFDcuHmIuyPTFmi1JpABe4q2ajskiJtfBf+WDAL2pg595R9JNoPrrH5+aT9lbkx2noicw=="], + "@expressive-code/core": ["@expressive-code/core@0.41.3", "", { "dependencies": { "@ctrl/tinycolor": "^4.0.4", "hast-util-select": "^6.0.2", "hast-util-to-html": "^9.0.1", "hast-util-to-text": "^4.0.1", "hastscript": "^9.0.0", "postcss": "^8.4.38", "postcss-nested": "^6.0.1", "unist-util-visit": "^5.0.0", "unist-util-visit-parents": "^6.0.1" } }, "sha512-9qzohqU7O0+JwMEEgQhnBPOw5DtsQRBXhW++5fvEywsuX44vCGGof1SL5OvPElvNgaWZ4pFZAFSlkNOkGyLwSQ=="], - "@expressive-code/plugin-shiki": ["@expressive-code/plugin-shiki@0.41.2", "", { "dependencies": { "@expressive-code/core": "^0.41.2", "shiki": "^3.2.2" } }, "sha512-xD4zwqAkDccXqye+235BH5bN038jYiSMLfUrCOmMlzxPDGWdxJDk5z4uUB/aLfivEF2tXyO2zyaarL3Oqht0fQ=="], + "@expressive-code/plugin-frames": ["@expressive-code/plugin-frames@0.41.3", "", { "dependencies": { "@expressive-code/core": "^0.41.3" } }, "sha512-rFQtmf/3N2CK3Cq/uERweMTYZnBu+CwxBdHuOftEmfA9iBE7gTVvwpbh82P9ZxkPLvc40UMhYt7uNuAZexycRQ=="], - "@expressive-code/plugin-text-markers": ["@expressive-code/plugin-text-markers@0.41.2", "", { "dependencies": { "@expressive-code/core": "^0.41.2" } }, "sha512-JFWBz2qYxxJOJkkWf96LpeolbnOqJY95TvwYc0hXIHf9oSWV0h0SY268w/5N3EtQaD9KktzDE+VIVwb9jdb3nw=="], + "@expressive-code/plugin-shiki": ["@expressive-code/plugin-shiki@0.41.3", "", { "dependencies": { "@expressive-code/core": "^0.41.3", "shiki": "^3.2.2" } }, "sha512-RlTARoopzhFJIOVHLGvuXJ8DCEme/hjV+ZnRJBIxzxsKVpGPW4Oshqg9xGhWTYdHstTsxO663s0cdBLzZj9TQA=="], + + "@expressive-code/plugin-text-markers": ["@expressive-code/plugin-text-markers@0.41.3", "", { "dependencies": { "@expressive-code/core": "^0.41.3" } }, "sha512-SN8tkIzDpA0HLAscEYD2IVrfLiid6qEdE9QLlGVSxO1KEw7qYvjpbNBQjUjMr5/jvTJ7ys6zysU2vLPHE0sb2g=="], "@fastify/busboy": ["@fastify/busboy@2.1.1", "", {}, "sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA=="], - "@flystorage/dynamic-import": ["@flystorage/dynamic-import@1.0.0", "", {}, "sha512-CIbIUrBdaPFyKnkVBaqzksvzNtsMSXITR/G/6zlil3MBnPFq2LX+X4Mv5p2XOmv/3OulFs/ff2SNb+5dc2Twtg=="], - - "@flystorage/file-storage": ["@flystorage/file-storage@1.1.0", "", {}, "sha512-25Gd5EsXDmhHrK5orpRuVqebQms1Cm9m5ACMZ0sVDX+Sbl1V0G88CbcWt7mEoWRYLvQ1U072htqg6Sav76ZlVA=="], - - "@flystorage/local-fs": ["@flystorage/local-fs@1.1.0", "", { "dependencies": { "@flystorage/dynamic-import": "^1.0.0", "@flystorage/file-storage": "^1.1.0", "file-type": "^20.5.0", "mime-types": "^3.0.1" } }, "sha512-dbErRhqmCv2UF0zPdeH7iVWuVeTWAJHuJD/mXDe2V370/SL7XIvdE3ditBHWC+1SzBKXJ0lkykOenwlum+oqIA=="], - "@fontsource/ibm-plex-mono": ["@fontsource/ibm-plex-mono@5.2.5", "", {}, "sha512-G09N3GfuT9qj3Ax2FDZvKqZttzM3v+cco2l8uXamhKyXLdmlaUDH5o88/C3vtTHj2oT7yRKsvxz9F+BXbWKMYA=="], - "@hapi/bourne": ["@hapi/bourne@2.1.0", "", {}, "sha512-i1BpaNDVLJdRBEKeJWkVO6tYX6DMFBuwMhSuWqLsY4ufeTKGVuV5rBsUhxPayXqnnWHgXUAmWK16H/ykO5Wj4Q=="], - - "@hono/zod-validator": ["@hono/zod-validator@0.5.0", "", { "peerDependencies": { "hono": ">=3.9.0", "zod": "^3.19.1" } }, "sha512-ds5bW6DCgAnNHP33E3ieSbaZFd5dkV52ZjyaXtGoR06APFrCtzAsKZxTHwOrJNBdXsi0e5wNwo5L4nVEVnJUdg=="], + "@hono/zod-validator": ["@hono/zod-validator@0.4.2", "", { "peerDependencies": { "hono": ">=3.9.0", "zod": "^3.19.1" } }, "sha512-1rrlBg+EpDPhzOV4hT9pxr5+xDVmKuz6YJl+la7VCwK6ass5ldyKm5fD+umJdV2zhHD6jROoCCv8NbTwyfhT0g=="], "@img/sharp-darwin-arm64": ["@img/sharp-darwin-arm64@0.33.5", "", { "optionalDependencies": { "@img/sharp-libvips-darwin-arm64": "1.0.4" }, "os": "darwin", "cpu": "arm64" }, "sha512-UT4p+iz/2H4twwAoLCqfA9UH5pI6DggwKEGuaPy7nCVQ8ZsiY5PIcrRvD1DzuY3qYL07NtIQcWnBSY/heikIFQ=="], @@ -327,13 +317,11 @@ "@img/sharp-win32-x64": ["@img/sharp-win32-x64@0.33.5", "", { "os": "win32", "cpu": "x64" }, "sha512-MpY/o8/8kj+EcnxwvrP4aTJSWw/aZ7JIGR4aBeZkZw5B7/Jn+tY9/VNwtcoGmdT7GfggGIU4kygOMSbYnOrAbg=="], - "@jridgewell/gen-mapping": ["@jridgewell/gen-mapping@0.3.8", "", { "dependencies": { "@jridgewell/set-array": "^1.2.1", "@jridgewell/sourcemap-codec": "^1.4.10", "@jridgewell/trace-mapping": "^0.3.24" } }, "sha512-imAbBGkb+ebQyxKgzv5Hu2nmROxoDOXHh80evxdoXNOrvAnVx7zimzc1Oo5h9RlfV4vPXaE2iM5pOFbvOCClWA=="], + "@jridgewell/gen-mapping": ["@jridgewell/gen-mapping@0.3.12", "", { "dependencies": { "@jridgewell/sourcemap-codec": "^1.5.0", "@jridgewell/trace-mapping": "^0.3.24" } }, "sha512-OuLGC46TjB5BbN1dH8JULVVZY4WTdkF7tV9Ys6wLL1rubZnCMstOhNHueU5bLCrnRuDhKPDM4g6sw4Bel5Gzqg=="], "@jridgewell/resolve-uri": ["@jridgewell/resolve-uri@3.1.2", "", {}, "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw=="], - "@jridgewell/set-array": ["@jridgewell/set-array@1.2.1", "", {}, "sha512-R8gLRTZeyp03ymzP/6Lil/28tGeGEzhx1q2k703KGWRAI1VdvPIXdG70VJc2pAMw3NA6JKL5hhFu1sJX0Mnn/A=="], - - "@jridgewell/sourcemap-codec": ["@jridgewell/sourcemap-codec@1.5.0", "", {}, "sha512-gv3ZRaISU3fjPAgNsriBRqGWQL6quFx04YMPW/zD8XMLsU32mhCCbfbO6KZFLjvYpCZ8zyDEgqsgf+PwPaM7GQ=="], + "@jridgewell/sourcemap-codec": ["@jridgewell/sourcemap-codec@1.5.4", "", {}, "sha512-VT2+G1VQs/9oz078bLrYbecdZKs912zQlkelYpuf+SXF+QvZDYJlbx/LSx+meSAwdDFnF8FVXW92AVjjkVmgFw=="], "@jridgewell/trace-mapping": ["@jridgewell/trace-mapping@0.3.9", "", { "dependencies": { "@jridgewell/resolve-uri": "^3.0.3", "@jridgewell/sourcemap-codec": "^1.4.10" } }, "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ=="], @@ -343,7 +331,43 @@ "@mixmark-io/domino": ["@mixmark-io/domino@2.2.0", "", {}, "sha512-Y28PR25bHXUg88kCV7nivXrP2Nj2RueZ3/l/jdx6J9f8J4nsEGcgX0Qe6lt7Pa+J79+kPiJU3LguR6O/6zrLOw=="], - "@modelcontextprotocol/sdk": ["@modelcontextprotocol/sdk@1.6.1", "", { "dependencies": { "content-type": "^1.0.5", "cors": "^2.8.5", "eventsource": "^3.0.2", "express": "^5.0.1", "express-rate-limit": "^7.5.0", "pkce-challenge": "^4.1.0", "raw-body": "^3.0.0", "zod": "^3.23.8", "zod-to-json-schema": "^3.24.1" } }, "sha512-oxzMzYCkZHMntzuyerehK3fV6A2Kwh5BD6CGEJSVDU2QNEhfLOptf2X7esQgaHZXHZY0oHmMsOtIDLP71UJXgA=="], + "@modelcontextprotocol/sdk": ["@modelcontextprotocol/sdk@1.15.1", "", { "dependencies": { "ajv": "^6.12.6", "content-type": "^1.0.5", "cors": "^2.8.5", "cross-spawn": "^7.0.5", "eventsource": "^3.0.2", "eventsource-parser": "^3.0.0", "express": "^5.0.1", "express-rate-limit": "^7.5.0", "pkce-challenge": "^5.0.0", "raw-body": "^3.0.0", "zod": "^3.23.8", "zod-to-json-schema": "^3.24.1" } }, "sha512-W/XlN9c528yYn+9MQkVjxiTPgPxoxt+oczfjHBDsJx0+59+O7B75Zhsp0B16Xbwbz8ANISDajh6+V7nIcPMc5w=="], + + "@octokit/auth-app": ["@octokit/auth-app@8.0.1", "", { "dependencies": { "@octokit/auth-oauth-app": "^9.0.1", "@octokit/auth-oauth-user": "^6.0.0", "@octokit/request": "^10.0.2", "@octokit/request-error": "^7.0.0", "@octokit/types": "^14.0.0", "toad-cache": "^3.7.0", "universal-github-app-jwt": "^2.2.0", "universal-user-agent": "^7.0.0" } }, "sha512-P2J5pB3pjiGwtJX4WqJVYCtNkcZ+j5T2Wm14aJAEIC3WJOrv12jvBley3G1U/XI8q9o1A7QMG54LiFED2BiFlg=="], + + "@octokit/auth-oauth-app": ["@octokit/auth-oauth-app@9.0.1", "", { "dependencies": { "@octokit/auth-oauth-device": "^8.0.1", "@octokit/auth-oauth-user": "^6.0.0", "@octokit/request": "^10.0.2", "@octokit/types": "^14.0.0", "universal-user-agent": "^7.0.0" } }, "sha512-TthWzYxuHKLAbmxdFZwFlmwVyvynpyPmjwc+2/cI3cvbT7mHtsAW9b1LvQaNnAuWL+pFnqtxdmrU8QpF633i1g=="], + + "@octokit/auth-oauth-device": ["@octokit/auth-oauth-device@8.0.1", "", { "dependencies": { "@octokit/oauth-methods": "^6.0.0", "@octokit/request": "^10.0.2", "@octokit/types": "^14.0.0", "universal-user-agent": "^7.0.0" } }, "sha512-TOqId/+am5yk9zor0RGibmlqn4V0h8vzjxlw/wYr3qzkQxl8aBPur384D1EyHtqvfz0syeXji4OUvKkHvxk/Gw=="], + + "@octokit/auth-oauth-user": ["@octokit/auth-oauth-user@6.0.0", "", { "dependencies": { "@octokit/auth-oauth-device": "^8.0.1", "@octokit/oauth-methods": "^6.0.0", "@octokit/request": "^10.0.2", "@octokit/types": "^14.0.0", "universal-user-agent": "^7.0.0" } }, "sha512-GV9IW134PHsLhtUad21WIeP9mlJ+QNpFd6V9vuPWmaiN25HEJeEQUcS4y5oRuqCm9iWDLtfIs+9K8uczBXKr6A=="], + + "@octokit/auth-token": ["@octokit/auth-token@6.0.0", "", {}, "sha512-P4YJBPdPSpWTQ1NU4XYdvHvXJJDxM6YwpS0FZHRgP7YFkdVxsWcpWGy/NVqlAA7PcPCnMacXlRm1y2PFZRWL/w=="], + + "@octokit/core": ["@octokit/core@7.0.3", "", { "dependencies": { "@octokit/auth-token": "^6.0.0", "@octokit/graphql": "^9.0.1", "@octokit/request": "^10.0.2", "@octokit/request-error": "^7.0.0", "@octokit/types": "^14.0.0", "before-after-hook": "^4.0.0", "universal-user-agent": "^7.0.0" } }, "sha512-oNXsh2ywth5aowwIa7RKtawnkdH6LgU1ztfP9AIUCQCvzysB+WeU8o2kyyosDPwBZutPpjZDKPQGIzzrfTWweQ=="], + + "@octokit/endpoint": ["@octokit/endpoint@11.0.0", "", { "dependencies": { "@octokit/types": "^14.0.0", "universal-user-agent": "^7.0.2" } }, "sha512-hoYicJZaqISMAI3JfaDr1qMNi48OctWuOih1m80bkYow/ayPw6Jj52tqWJ6GEoFTk1gBqfanSoI1iY99Z5+ekQ=="], + + "@octokit/graphql": ["@octokit/graphql@9.0.1", "", { "dependencies": { "@octokit/request": "^10.0.2", "@octokit/types": "^14.0.0", "universal-user-agent": "^7.0.0" } }, "sha512-j1nQNU1ZxNFx2ZtKmL4sMrs4egy5h65OMDmSbVyuCzjOcwsHq6EaYjOTGXPQxgfiN8dJ4CriYHk6zF050WEULg=="], + + "@octokit/oauth-authorization-url": ["@octokit/oauth-authorization-url@8.0.0", "", {}, "sha512-7QoLPRh/ssEA/HuHBHdVdSgF8xNLz/Bc5m9fZkArJE5bb6NmVkDm3anKxXPmN1zh6b5WKZPRr3697xKT/yM3qQ=="], + + "@octokit/oauth-methods": ["@octokit/oauth-methods@6.0.0", "", { "dependencies": { "@octokit/oauth-authorization-url": "^8.0.0", "@octokit/request": "^10.0.2", "@octokit/request-error": "^7.0.0", "@octokit/types": "^14.0.0" } }, "sha512-Q8nFIagNLIZgM2odAraelMcDssapc+lF+y3OlcIPxyAU+knefO8KmozGqfnma1xegRDP4z5M73ABsamn72bOcA=="], + + "@octokit/openapi-types": ["@octokit/openapi-types@25.1.0", "", {}, "sha512-idsIggNXUKkk0+BExUn1dQ92sfysJrje03Q0bv0e+KPLrvyqZF8MnBpFz8UNfYDwB3Ie7Z0TByjWfzxt7vseaA=="], + + "@octokit/plugin-paginate-rest": ["@octokit/plugin-paginate-rest@13.1.1", "", { "dependencies": { "@octokit/types": "^14.1.0" }, "peerDependencies": { "@octokit/core": ">=6" } }, "sha512-q9iQGlZlxAVNRN2jDNskJW/Cafy7/XE52wjZ5TTvyhyOD904Cvx//DNyoO3J/MXJ0ve3rPoNWKEg5iZrisQSuw=="], + + "@octokit/plugin-request-log": ["@octokit/plugin-request-log@6.0.0", "", { "peerDependencies": { "@octokit/core": ">=6" } }, "sha512-UkOzeEN3W91/eBq9sPZNQ7sUBvYCqYbrrD8gTbBuGtHEuycE4/awMXcYvx6sVYo7LypPhmQwwpUe4Yyu4QZN5Q=="], + + "@octokit/plugin-rest-endpoint-methods": ["@octokit/plugin-rest-endpoint-methods@16.0.0", "", { "dependencies": { "@octokit/types": "^14.1.0" }, "peerDependencies": { "@octokit/core": ">=6" } }, "sha512-kJVUQk6/dx/gRNLWUnAWKFs1kVPn5O5CYZyssyEoNYaFedqZxsfYs7DwI3d67hGz4qOwaJ1dpm07hOAD1BXx6g=="], + + "@octokit/request": ["@octokit/request@10.0.3", "", { "dependencies": { "@octokit/endpoint": "^11.0.0", "@octokit/request-error": "^7.0.0", "@octokit/types": "^14.0.0", "fast-content-type-parse": "^3.0.0", "universal-user-agent": "^7.0.2" } }, "sha512-V6jhKokg35vk098iBqp2FBKunk3kMTXlmq+PtbV9Gl3TfskWlebSofU9uunVKhUN7xl+0+i5vt0TGTG8/p/7HA=="], + + "@octokit/request-error": ["@octokit/request-error@7.0.0", "", { "dependencies": { "@octokit/types": "^14.0.0" } }, "sha512-KRA7VTGdVyJlh0cP5Tf94hTiYVVqmt2f3I6mnimmaVz4UG3gQV/k4mDJlJv3X67iX6rmN7gSHCF8ssqeMnmhZg=="], + + "@octokit/rest": ["@octokit/rest@22.0.0", "", { "dependencies": { "@octokit/core": "^7.0.2", "@octokit/plugin-paginate-rest": "^13.0.1", "@octokit/plugin-request-log": "^6.0.0", "@octokit/plugin-rest-endpoint-methods": "^16.0.0" } }, "sha512-z6tmTu9BTnw51jYGulxrlernpsQYXpui1RK21vmXn8yF5bp6iX16yfTtJYGK5Mh1qDkvDOmp2n8sRMcQmR8jiA=="], + + "@octokit/types": ["@octokit/types@14.1.0", "", { "dependencies": { "@octokit/openapi-types": "^25.1.0" } }, "sha512-1y6DgTy8Jomcpu33N+p5w58l6xyt55Ar2I91RPiIA0xCJBXyUAhXCcmZaDWSANiha7R9a6qJJ2CRomGPZ6f46g=="], "@openauthjs/openauth": ["@openauthjs/openauth@0.4.3", "", { "dependencies": { "@standard-schema/spec": "1.0.0-beta.3", "aws4fetch": "1.0.20", "jose": "5.9.6" }, "peerDependencies": { "arctic": "^2.2.2", "hono": "^4.0.0" } }, "sha512-RlnjqvHzqcbFVymEwhlUEuac4utA5h4nhSK/i2szZuQmxTIqbGUxZ+nM+avM+VV4Ing+/ZaNLKILoXS3yrkOOw=="], @@ -375,47 +399,53 @@ "@pagefind/windows-x64": ["@pagefind/windows-x64@1.3.0", "", { "os": "win32", "cpu": "x64" }, "sha512-BR1bIRWOMqkf8IoU576YDhij1Wd/Zf2kX/kCI0b2qzCKC8wcc2GQJaaRMCpzvCCrmliO4vtJ6RITp/AnoYUUmQ=="], - "@rollup/pluginutils": ["@rollup/pluginutils@5.1.4", "", { "dependencies": { "@types/estree": "^1.0.0", "estree-walker": "^2.0.2", "picomatch": "^4.0.2" }, "peerDependencies": { "rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0" }, "optionalPeers": ["rollup"] }, "sha512-USm05zrsFxYLPdWWq+K3STlWiT/3ELn3RcV5hJMghpeAIhxfsUIg6mt12CBJBInWMV4VneoV7SfGv8xIwo2qNQ=="], + "@poppinss/colors": ["@poppinss/colors@4.1.5", "", { "dependencies": { "kleur": "^4.1.5" } }, "sha512-FvdDqtcRCtz6hThExcFOgW0cWX+xwSMWcRuQe5ZEb2m7cVQOAVZOIMt+/v9RxGiD9/OY16qJBXK4CVKWAPalBw=="], - "@rollup/rollup-android-arm-eabi": ["@rollup/rollup-android-arm-eabi@4.41.1", "", { "os": "android", "cpu": "arm" }, "sha512-NELNvyEWZ6R9QMkiytB4/L4zSEaBC03KIXEghptLGLZWJ6VPrL63ooZQCOnlx36aQPGhzuOMwDerC1Eb2VmrLw=="], + "@poppinss/dumper": ["@poppinss/dumper@0.6.4", "", { "dependencies": { "@poppinss/colors": "^4.1.5", "@sindresorhus/is": "^7.0.2", "supports-color": "^10.0.0" } }, "sha512-iG0TIdqv8xJ3Lt9O8DrPRxw1MRLjNpoqiSGU03P/wNLP/s0ra0udPJ1J2Tx5M0J3H/cVyEgpbn8xUKRY9j59kQ=="], - "@rollup/rollup-android-arm64": ["@rollup/rollup-android-arm64@4.41.1", "", { "os": "android", "cpu": "arm64" }, "sha512-DXdQe1BJ6TK47ukAoZLehRHhfKnKg9BjnQYUu9gzhI8Mwa1d2fzxA1aw2JixHVl403bwp1+/o/NhhHtxWJBgEA=="], + "@poppinss/exception": ["@poppinss/exception@1.2.2", "", {}, "sha512-m7bpKCD4QMlFCjA/nKTs23fuvoVFoA83brRKmObCUNmi/9tVu8Ve3w4YQAnJu4q3Tjf5fr685HYIC/IA2zHRSg=="], - "@rollup/rollup-darwin-arm64": ["@rollup/rollup-darwin-arm64@4.41.1", "", { "os": "darwin", "cpu": "arm64" }, "sha512-5afxvwszzdulsU2w8JKWwY8/sJOLPzf0e1bFuvcW5h9zsEg+RQAojdW0ux2zyYAz7R8HvvzKCjLNJhVq965U7w=="], + "@rollup/pluginutils": ["@rollup/pluginutils@5.2.0", "", { "dependencies": { "@types/estree": "^1.0.0", "estree-walker": "^2.0.2", "picomatch": "^4.0.2" }, "peerDependencies": { "rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0" }, "optionalPeers": ["rollup"] }, "sha512-qWJ2ZTbmumwiLFomfzTyt5Kng4hwPi9rwCYN4SHb6eaRU1KNO4ccxINHr/VhH4GgPlt1XfSTLX2LBTme8ne4Zw=="], - "@rollup/rollup-darwin-x64": ["@rollup/rollup-darwin-x64@4.41.1", "", { "os": "darwin", "cpu": "x64" }, "sha512-egpJACny8QOdHNNMZKf8xY0Is6gIMz+tuqXlusxquWu3F833DcMwmGM7WlvCO9sB3OsPjdC4U0wHw5FabzCGZg=="], + "@rollup/rollup-android-arm-eabi": ["@rollup/rollup-android-arm-eabi@4.45.0", "", { "os": "android", "cpu": "arm" }, "sha512-2o/FgACbji4tW1dzXOqAV15Eu7DdgbKsF2QKcxfG4xbh5iwU7yr5RRP5/U+0asQliSYv5M4o7BevlGIoSL0LXg=="], - "@rollup/rollup-freebsd-arm64": ["@rollup/rollup-freebsd-arm64@4.41.1", "", { "os": "freebsd", "cpu": "arm64" }, "sha512-DBVMZH5vbjgRk3r0OzgjS38z+atlupJ7xfKIDJdZZL6sM6wjfDNo64aowcLPKIx7LMQi8vybB56uh1Ftck/Atg=="], + "@rollup/rollup-android-arm64": ["@rollup/rollup-android-arm64@4.45.0", "", { "os": "android", "cpu": "arm64" }, "sha512-PSZ0SvMOjEAxwZeTx32eI/j5xSYtDCRxGu5k9zvzoY77xUNssZM+WV6HYBLROpY5CkXsbQjvz40fBb7WPwDqtQ=="], - "@rollup/rollup-freebsd-x64": ["@rollup/rollup-freebsd-x64@4.41.1", "", { "os": "freebsd", "cpu": "x64" }, "sha512-3FkydeohozEskBxNWEIbPfOE0aqQgB6ttTkJ159uWOFn42VLyfAiyD9UK5mhu+ItWzft60DycIN1Xdgiy8o/SA=="], + "@rollup/rollup-darwin-arm64": ["@rollup/rollup-darwin-arm64@4.45.0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-BA4yPIPssPB2aRAWzmqzQ3y2/KotkLyZukVB7j3psK/U3nVJdceo6qr9pLM2xN6iRP/wKfxEbOb1yrlZH6sYZg=="], - "@rollup/rollup-linux-arm-gnueabihf": ["@rollup/rollup-linux-arm-gnueabihf@4.41.1", "", { "os": "linux", "cpu": "arm" }, "sha512-wC53ZNDgt0pqx5xCAgNunkTzFE8GTgdZ9EwYGVcg+jEjJdZGtq9xPjDnFgfFozQI/Xm1mh+D9YlYtl+ueswNEg=="], + "@rollup/rollup-darwin-x64": ["@rollup/rollup-darwin-x64@4.45.0", "", { "os": "darwin", "cpu": "x64" }, "sha512-Pr2o0lvTwsiG4HCr43Zy9xXrHspyMvsvEw4FwKYqhli4FuLE5FjcZzuQ4cfPe0iUFCvSQG6lACI0xj74FDZKRA=="], - "@rollup/rollup-linux-arm-musleabihf": ["@rollup/rollup-linux-arm-musleabihf@4.41.1", "", { "os": "linux", "cpu": "arm" }, "sha512-jwKCca1gbZkZLhLRtsrka5N8sFAaxrGz/7wRJ8Wwvq3jug7toO21vWlViihG85ei7uJTpzbXZRcORotE+xyrLA=="], + "@rollup/rollup-freebsd-arm64": ["@rollup/rollup-freebsd-arm64@4.45.0", "", { "os": "freebsd", "cpu": "arm64" }, "sha512-lYE8LkE5h4a/+6VnnLiL14zWMPnx6wNbDG23GcYFpRW1V9hYWHAw9lBZ6ZUIrOaoK7NliF1sdwYGiVmziUF4vA=="], - "@rollup/rollup-linux-arm64-gnu": ["@rollup/rollup-linux-arm64-gnu@4.41.1", "", { "os": "linux", "cpu": "arm64" }, "sha512-g0UBcNknsmmNQ8V2d/zD2P7WWfJKU0F1nu0k5pW4rvdb+BIqMm8ToluW/eeRmxCared5dD76lS04uL4UaNgpNA=="], + "@rollup/rollup-freebsd-x64": ["@rollup/rollup-freebsd-x64@4.45.0", "", { "os": "freebsd", "cpu": "x64" }, "sha512-PVQWZK9sbzpvqC9Q0GlehNNSVHR+4m7+wET+7FgSnKG3ci5nAMgGmr9mGBXzAuE5SvguCKJ6mHL6vq1JaJ/gvw=="], - "@rollup/rollup-linux-arm64-musl": ["@rollup/rollup-linux-arm64-musl@4.41.1", "", { "os": "linux", "cpu": "arm64" }, "sha512-XZpeGB5TKEZWzIrj7sXr+BEaSgo/ma/kCgrZgL0oo5qdB1JlTzIYQKel/RmhT6vMAvOdM2teYlAaOGJpJ9lahg=="], + "@rollup/rollup-linux-arm-gnueabihf": ["@rollup/rollup-linux-arm-gnueabihf@4.45.0", "", { "os": "linux", "cpu": "arm" }, "sha512-hLrmRl53prCcD+YXTfNvXd776HTxNh8wPAMllusQ+amcQmtgo3V5i/nkhPN6FakW+QVLoUUr2AsbtIRPFU3xIA=="], - "@rollup/rollup-linux-loongarch64-gnu": ["@rollup/rollup-linux-loongarch64-gnu@4.41.1", "", { "os": "linux", "cpu": "none" }, "sha512-bkCfDJ4qzWfFRCNt5RVV4DOw6KEgFTUZi2r2RuYhGWC8WhCA8lCAJhDeAmrM/fdiAH54m0mA0Vk2FGRPyzI+tw=="], + "@rollup/rollup-linux-arm-musleabihf": ["@rollup/rollup-linux-arm-musleabihf@4.45.0", "", { "os": "linux", "cpu": "arm" }, "sha512-XBKGSYcrkdiRRjl+8XvrUR3AosXU0NvF7VuqMsm7s5nRy+nt58ZMB19Jdp1RdqewLcaYnpk8zeVs/4MlLZEJxw=="], - "@rollup/rollup-linux-powerpc64le-gnu": ["@rollup/rollup-linux-powerpc64le-gnu@4.41.1", "", { "os": "linux", "cpu": "ppc64" }, "sha512-3mr3Xm+gvMX+/8EKogIZSIEF0WUu0HL9di+YWlJpO8CQBnoLAEL/roTCxuLncEdgcfJcvA4UMOf+2dnjl4Ut1A=="], + "@rollup/rollup-linux-arm64-gnu": ["@rollup/rollup-linux-arm64-gnu@4.45.0", "", { "os": "linux", "cpu": "arm64" }, "sha512-fRvZZPUiBz7NztBE/2QnCS5AtqLVhXmUOPj9IHlfGEXkapgImf4W9+FSkL8cWqoAjozyUzqFmSc4zh2ooaeF6g=="], - "@rollup/rollup-linux-riscv64-gnu": ["@rollup/rollup-linux-riscv64-gnu@4.41.1", "", { "os": "linux", "cpu": "none" }, "sha512-3rwCIh6MQ1LGrvKJitQjZFuQnT2wxfU+ivhNBzmxXTXPllewOF7JR1s2vMX/tWtUYFgphygxjqMl76q4aMotGw=="], + "@rollup/rollup-linux-arm64-musl": ["@rollup/rollup-linux-arm64-musl@4.45.0", "", { "os": "linux", "cpu": "arm64" }, "sha512-Btv2WRZOcUGi8XU80XwIvzTg4U6+l6D0V6sZTrZx214nrwxw5nAi8hysaXj/mctyClWgesyuxbeLylCBNauimg=="], - "@rollup/rollup-linux-riscv64-musl": ["@rollup/rollup-linux-riscv64-musl@4.41.1", "", { "os": "linux", "cpu": "none" }, "sha512-LdIUOb3gvfmpkgFZuccNa2uYiqtgZAz3PTzjuM5bH3nvuy9ty6RGc/Q0+HDFrHrizJGVpjnTZ1yS5TNNjFlklw=="], + "@rollup/rollup-linux-loongarch64-gnu": ["@rollup/rollup-linux-loongarch64-gnu@4.45.0", "", { "os": "linux", "cpu": "none" }, "sha512-Li0emNnwtUZdLwHjQPBxn4VWztcrw/h7mgLyHiEI5Z0MhpeFGlzaiBHpSNVOMB/xucjXTTcO+dhv469Djr16KA=="], - "@rollup/rollup-linux-s390x-gnu": ["@rollup/rollup-linux-s390x-gnu@4.41.1", "", { "os": "linux", "cpu": "s390x" }, "sha512-oIE6M8WC9ma6xYqjvPhzZYk6NbobIURvP/lEbh7FWplcMO6gn7MM2yHKA1eC/GvYwzNKK/1LYgqzdkZ8YFxR8g=="], + "@rollup/rollup-linux-powerpc64le-gnu": ["@rollup/rollup-linux-powerpc64le-gnu@4.45.0", "", { "os": "linux", "cpu": "ppc64" }, "sha512-sB8+pfkYx2kvpDCfd63d5ScYT0Fz1LO6jIb2zLZvmK9ob2D8DeVqrmBDE0iDK8KlBVmsTNzrjr3G1xV4eUZhSw=="], - "@rollup/rollup-linux-x64-gnu": ["@rollup/rollup-linux-x64-gnu@4.41.1", "", { "os": "linux", "cpu": "x64" }, "sha512-cWBOvayNvA+SyeQMp79BHPK8ws6sHSsYnK5zDcsC3Hsxr1dgTABKjMnMslPq1DvZIp6uO7kIWhiGwaTdR4Og9A=="], + "@rollup/rollup-linux-riscv64-gnu": ["@rollup/rollup-linux-riscv64-gnu@4.45.0", "", { "os": "linux", "cpu": "none" }, "sha512-5GQ6PFhh7E6jQm70p1aW05G2cap5zMOvO0se5JMecHeAdj5ZhWEHbJ4hiKpfi1nnnEdTauDXxPgXae/mqjow9w=="], - "@rollup/rollup-linux-x64-musl": ["@rollup/rollup-linux-x64-musl@4.41.1", "", { "os": "linux", "cpu": "x64" }, "sha512-y5CbN44M+pUCdGDlZFzGGBSKCA4A/J2ZH4edTYSSxFg7ce1Xt3GtydbVKWLlzL+INfFIZAEg1ZV6hh9+QQf9YQ=="], + "@rollup/rollup-linux-riscv64-musl": ["@rollup/rollup-linux-riscv64-musl@4.45.0", "", { "os": "linux", "cpu": "none" }, "sha512-N/euLsBd1rekWcuduakTo/dJw6U6sBP3eUq+RXM9RNfPuWTvG2w/WObDkIvJ2KChy6oxZmOSC08Ak2OJA0UiAA=="], - "@rollup/rollup-win32-arm64-msvc": ["@rollup/rollup-win32-arm64-msvc@4.41.1", "", { "os": "win32", "cpu": "arm64" }, "sha512-lZkCxIrjlJlMt1dLO/FbpZbzt6J/A8p4DnqzSa4PWqPEUUUnzXLeki/iyPLfV0BmHItlYgHUqJe+3KiyydmiNQ=="], + "@rollup/rollup-linux-s390x-gnu": ["@rollup/rollup-linux-s390x-gnu@4.45.0", "", { "os": "linux", "cpu": "s390x" }, "sha512-2l9sA7d7QdikL0xQwNMO3xURBUNEWyHVHfAsHsUdq+E/pgLTUcCE+gih5PCdmyHmfTDeXUWVhqL0WZzg0nua3g=="], - "@rollup/rollup-win32-ia32-msvc": ["@rollup/rollup-win32-ia32-msvc@4.41.1", "", { "os": "win32", "cpu": "ia32" }, "sha512-+psFT9+pIh2iuGsxFYYa/LhS5MFKmuivRsx9iPJWNSGbh2XVEjk90fmpUEjCnILPEPJnikAU6SFDiEUyOv90Pg=="], + "@rollup/rollup-linux-x64-gnu": ["@rollup/rollup-linux-x64-gnu@4.45.0", "", { "os": "linux", "cpu": "x64" }, "sha512-XZdD3fEEQcwG2KrJDdEQu7NrHonPxxaV0/w2HpvINBdcqebz1aL+0vM2WFJq4DeiAVT6F5SUQas65HY5JDqoPw=="], - "@rollup/rollup-win32-x64-msvc": ["@rollup/rollup-win32-x64-msvc@4.41.1", "", { "os": "win32", "cpu": "x64" }, "sha512-Wq2zpapRYLfi4aKxf2Xff0tN+7slj2d4R87WEzqw7ZLsVvO5zwYCIuEGSZYiK41+GlwUo1HiR+GdkLEJnCKTCw=="], + "@rollup/rollup-linux-x64-musl": ["@rollup/rollup-linux-x64-musl@4.45.0", "", { "os": "linux", "cpu": "x64" }, "sha512-7ayfgvtmmWgKWBkCGg5+xTQ0r5V1owVm67zTrsEY1008L5ro7mCyGYORomARt/OquB9KY7LpxVBZes+oSniAAQ=="], + + "@rollup/rollup-win32-arm64-msvc": ["@rollup/rollup-win32-arm64-msvc@4.45.0", "", { "os": "win32", "cpu": "arm64" }, "sha512-B+IJgcBnE2bm93jEW5kHisqvPITs4ddLOROAcOc/diBgrEiQJJ6Qcjby75rFSmH5eMGrqJryUgJDhrfj942apQ=="], + + "@rollup/rollup-win32-ia32-msvc": ["@rollup/rollup-win32-ia32-msvc@4.45.0", "", { "os": "win32", "cpu": "ia32" }, "sha512-+CXwwG66g0/FpWOnP/v1HnrGVSOygK/osUbu3wPRy8ECXjoYKjRAyfxYpDQOfghC5qPJYLPH0oN4MCOjwgdMug=="], + + "@rollup/rollup-win32-x64-msvc": ["@rollup/rollup-win32-x64-msvc@4.45.0", "", { "os": "win32", "cpu": "x64" }, "sha512-SRf1cytG7wqcHVLrBc9VtPK4pU5wxiB/lNIkNmW2ApKXIg+RpqwHfsaEK+e7eH4A1BpI6BX/aBWXxZCIrJg3uA=="], "@shikijs/core": ["@shikijs/core@3.4.2", "", { "dependencies": { "@shikijs/types": "3.4.2", "@shikijs/vscode-textmate": "^10.0.2", "@types/hast": "^3.0.4", "hast-util-to-html": "^9.0.5" } }, "sha512-AG8vnSi1W2pbgR2B911EfGqtLE9c4hQBYkv/x7Z+Kt0VxhgQKcW7UNDVYsu9YxwV6u+OJrvdJrMq6DNWoBjihQ=="], @@ -433,6 +463,8 @@ "@shikijs/vscode-textmate": ["@shikijs/vscode-textmate@10.0.2", "", {}, "sha512-83yeghZ2xxin3Nj8z1NMd/NCuca+gsYXswywDy5bHvwlWL8tpTQmzGeUuHd9FC3E/SBEMvzJRwWEOz5gGes9Qg=="], + "@sindresorhus/is": ["@sindresorhus/is@7.0.2", "", {}, "sha512-d9xRovfKNz1SKieM0qJdO+PQonjnnIfSNWfHYnBSJ9hkjm0ZPw6HlxscDXYstp3z+7V2GOFHc+J0CYrYTjqCJw=="], + "@smithy/eventstream-codec": ["@smithy/eventstream-codec@4.0.4", "", { "dependencies": { "@aws-crypto/crc32": "5.2.0", "@smithy/types": "^4.3.1", "@smithy/util-hex-encoding": "^4.0.0", "tslib": "^2.6.2" } }, "sha512-7XoWfZqWb/QoR/rAU4VSi0mWnO2vu9/ltS6JZ5ZSZv0eovLVfDfu0/AX4ub33RsJTOth3TiFWSHS5YdztvFnig=="], "@smithy/is-array-buffer": ["@smithy/is-array-buffer@4.0.0", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-saYhF8ZZNoJDTvJBEWgeBccCg+yvp1CX+ed12yORU3NilJScfc6gfch2oVb4QgxZrGUx3/ZJlb+c/dJbyupxlw=="], @@ -445,14 +477,12 @@ "@smithy/util-utf8": ["@smithy/util-utf8@4.0.0", "", { "dependencies": { "@smithy/util-buffer-from": "^4.0.0", "tslib": "^2.6.2" } }, "sha512-b+zebfKCfRdgNJDknHCob3O7FpeYQN6ZG6YLExMcasDHsCXlsXCEuiPZeLnJLpwa5dvPetGlnGCiMHuLwGvFow=="], + "@speed-highlight/core": ["@speed-highlight/core@1.2.7", "", {}, "sha512-0dxmVj4gxg3Jg879kvFS/msl4s9F3T9UXC1InxgOf7t5NvcPD97u/WTA5vL/IxWHMn7qSxBozqrnnE2wvl1m8g=="], + "@standard-schema/spec": ["@standard-schema/spec@1.0.0", "", {}, "sha512-m2bOd0f2RT9k8QJx1JN85cZYyH1RqFBdlwtkSlf4tBDYLCiiZnv1fIIwacK6cqwXavOydf0NPToMQgpKq+dVlA=="], "@swc/helpers": ["@swc/helpers@0.5.17", "", { "dependencies": { "tslib": "^2.8.0" } }, "sha512-5IKx/Y13RsYd+sauPb2x+U/xZikHjolzfuDgTAl/Tdf3Q8rslRvC19NKDLgAJQ6wsqADk10ntlv08nPFw/gO/A=="], - "@tokenizer/inflate": ["@tokenizer/inflate@0.2.7", "", { "dependencies": { "debug": "^4.4.0", "fflate": "^0.8.2", "token-types": "^6.0.0" } }, "sha512-MADQgmZT1eKjp06jpI2yozxaU9uVs4GzzgSL+uEq7bVcJ9V1ZXQkeGNql1fsSI0gMy1vhvNTNbUqrx+pZfJVmg=="], - - "@tokenizer/token": ["@tokenizer/token@0.3.0", "", {}, "sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A=="], - "@tsconfig/bun": ["@tsconfig/bun@1.0.7", "", {}, "sha512-udGrGJBNQdXGVulehc1aWT73wkR9wdaGBtB6yL70RJsqwW/yJhIg6ZbRlPOfIUiFNrnBuYLBi9CSmMKfDC7dvA=="], "@types/babel__core": ["@types/babel__core@7.20.5", "", { "dependencies": { "@babel/parser": "^7.20.7", "@babel/types": "^7.20.7", "@types/babel__generator": "*", "@types/babel__template": "*", "@types/babel__traverse": "*" } }, "sha512-qoQprZvz5wQFJwMDqeseRXWv3rqMvhgpbXFfVyWhbx9X47POIA6i/+dXefEmZKoAgOaTdaIgNSMqMIU61yRyzA=="], @@ -467,9 +497,7 @@ "@types/debug": ["@types/debug@4.1.12", "", { "dependencies": { "@types/ms": "*" } }, "sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ=="], - "@types/diff-match-patch": ["@types/diff-match-patch@1.0.36", "", {}, "sha512-xFdR6tkm0MWvBfO8xXCSsinYxHcqkQUlcHeSpMC2ukzOb6lwQAfDmW+Qt0AvlGd8HpsS28qKsB+oPeJn9I39jg=="], - - "@types/estree": ["@types/estree@1.0.7", "", {}, "sha512-w28IoSUCJpidD/TGviZwwMJckNESJZXFu7NBZ5YJ4mEUnNraUn9Pm8HSZm/jDF1pDWYKspWE7oVphigUPRakIQ=="], + "@types/estree": ["@types/estree@1.0.8", "", {}, "sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w=="], "@types/estree-jsx": ["@types/estree-jsx@1.0.5", "", { "dependencies": { "@types/estree": "*" } }, "sha512-52CcUVNFyfb1A2ALocQw/Dd1BQFNmSdkuC3BkZ6iqhdMfQz7JWOFRuJFloOzjk+6WijU56m9oKXFAXc7o3Towg=="], @@ -509,13 +537,15 @@ "accepts": ["accepts@2.0.0", "", { "dependencies": { "mime-types": "^3.0.0", "negotiator": "^1.0.0" } }, "sha512-5cvg6CtKwfgdmVqY1WIiXKc3Q1bkRqGLi+2W/6ao+6Y7gu/RCwRuAhGEzh5B4KlszSuTLgZYuqFqo5bImjNKng=="], - "acorn": ["acorn@8.14.1", "", { "bin": { "acorn": "bin/acorn" } }, "sha512-OvQ/2pUDKmgfCg++xsTX1wGxfTaszcHVcTctW4UJB4hibJx2HXxxO5UmVgyjMa+ZDsiaf5wWLXYpRWMmBI0QHg=="], + "acorn": ["acorn@8.15.0", "", { "bin": { "acorn": "bin/acorn" } }, "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg=="], "acorn-jsx": ["acorn-jsx@5.3.2", "", { "peerDependencies": { "acorn": "^6.0.0 || ^7.0.0 || ^8.0.0" } }, "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ=="], "acorn-walk": ["acorn-walk@8.3.2", "", {}, "sha512-cjkyv4OtNCIeqhHrfS81QWXoCBPExR/J62oyEqepVw8WaQeSqpW2uhuLPh1m9eWhDuOo/jUXVTlifvesOWp/4A=="], - "ai": ["ai@4.3.16", "", { "dependencies": { "@ai-sdk/provider": "1.1.3", "@ai-sdk/provider-utils": "2.2.8", "@ai-sdk/react": "1.2.12", "@ai-sdk/ui-utils": "1.2.11", "@opentelemetry/api": "1.9.0", "jsondiffpatch": "0.6.0" }, "peerDependencies": { "react": "^18 || ^19 || ^19.0.0-rc", "zod": "^3.23.8" }, "optionalPeers": ["react"] }, "sha512-KUDwlThJ5tr2Vw0A1ZkbDKNME3wzWhuVfAOwIvFUzl1TPVDFAXDFTXio3p+jaKneB+dKNCvFFlolYmmgHttG1g=="], + "ai": ["ai@5.0.0-beta.21", "", { "dependencies": { "@ai-sdk/gateway": "1.0.0-beta.8", "@ai-sdk/provider": "2.0.0-beta.1", "@ai-sdk/provider-utils": "3.0.0-beta.3", "@opentelemetry/api": "1.9.0" }, "peerDependencies": { "zod": "^3.25.49 || ^4" }, "bin": { "ai": "dist/bin/ai.min.js" } }, "sha512-ZmgUoEIXb2G2HLtK1U3UB+hSDa3qrVIeAfgXf3SIE9r5Vqj6xHG1pN/7fHIZDSgb1TCaypG0ANVB0O9WmnMfiw=="], + + "ajv": ["ajv@6.12.6", "", { "dependencies": { "fast-deep-equal": "^3.1.1", "fast-json-stable-stringify": "^2.0.0", "json-schema-traverse": "^0.4.1", "uri-js": "^4.2.2" } }, "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g=="], "ansi-align": ["ansi-align@3.0.1", "", { "dependencies": { "string-width": "^4.1.0" } }, "sha512-IOfwwBF5iczOjp/WeY4YxyjqAFMQoZufdQWDd19SEExbVLNXqvpzSJ/M7Za4/sCPmQ0+GRquoA7bGcINcxew6w=="], @@ -531,24 +561,18 @@ "argparse": ["argparse@2.0.1", "", {}, "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="], - "args": ["args@5.0.3", "", { "dependencies": { "camelcase": "5.0.0", "chalk": "2.4.2", "leven": "2.1.0", "mri": "1.1.4" } }, "sha512-h6k/zfFgusnv3i5TU08KQkVKuCPBtL/PWQbWkHUxvJrZ2nAyeaUupneemcrgn1xmqxPQsPIzwkUhOpoqPDRZuA=="], - "aria-query": ["aria-query@5.3.2", "", {}, "sha512-COROpnaoap1E2F000S62r6A60uHZnmlvomhfyT2DlTcrY1OrBKn2UhH7qn5wTC9zMvD0AY7csdPSNwKP+7WiQw=="], "array-iterate": ["array-iterate@2.0.1", "", {}, "sha512-I1jXZMjAgCMmxT4qxXfPXa6SthSoE8h6gkSI9BGGNv8mP8G/v0blc+qFnZu6K42vTOiuME596QaLO0TP3Lk0xg=="], - "as-table": ["as-table@1.0.55", "", { "dependencies": { "printable-characters": "^1.0.42" } }, "sha512-xvsWESUJn0JN421Xb9MQw6AsMHRCUknCe0Wjlxvjud80mU4E6hQf1A6NzQKcYNmYw62MfzEtXc+badstZP3JpQ=="], - "astring": ["astring@1.9.0", "", { "bin": { "astring": "bin/astring" } }, "sha512-LElXdjswlqjWrPpJFg1Fx4wpkOCxj1TDHlSV4PlaRxHGWko024xICaa97ZkMfs6DRKlCguiAI+rbXv5GWwXIkg=="], "astro": ["astro@5.7.13", "", { "dependencies": { "@astrojs/compiler": "^2.11.0", "@astrojs/internal-helpers": "0.6.1", "@astrojs/markdown-remark": "6.3.1", "@astrojs/telemetry": "3.2.1", "@capsizecss/unpack": "^2.4.0", "@oslojs/encoding": "^1.1.0", "@rollup/pluginutils": "^5.1.4", "acorn": "^8.14.1", "aria-query": "^5.3.2", "axobject-query": "^4.1.0", "boxen": "8.0.1", "ci-info": "^4.2.0", "clsx": "^2.1.1", "common-ancestor-path": "^1.0.1", "cookie": "^1.0.2", "cssesc": "^3.0.0", "debug": "^4.4.0", "deterministic-object-hash": "^2.0.2", "devalue": "^5.1.1", "diff": "^5.2.0", "dlv": "^1.1.3", "dset": "^3.1.4", "es-module-lexer": "^1.6.0", "esbuild": "^0.25.0", "estree-walker": "^3.0.3", "flattie": "^1.1.1", "fontace": "~0.3.0", "github-slugger": "^2.0.0", "html-escaper": "3.0.3", "http-cache-semantics": "^4.1.1", "js-yaml": "^4.1.0", "kleur": "^4.1.5", "magic-string": "^0.30.17", "magicast": "^0.3.5", "mrmime": "^2.0.1", "neotraverse": "^0.6.18", "p-limit": "^6.2.0", "p-queue": "^8.1.0", "package-manager-detector": "^1.1.0", "picomatch": "^4.0.2", "prompts": "^2.4.2", "rehype": "^13.0.2", "semver": "^7.7.1", "shiki": "^3.2.1", "tinyexec": "^0.3.2", "tinyglobby": "^0.2.12", "tsconfck": "^3.1.5", "ultrahtml": "^1.6.0", "unifont": "~0.5.0", "unist-util-visit": "^5.0.0", "unstorage": "^1.15.0", "vfile": "^6.0.3", "vite": "^6.3.4", "vitefu": "^1.0.6", "xxhash-wasm": "^1.1.0", "yargs-parser": "^21.1.1", "yocto-spinner": "^0.2.1", "zod": "^3.24.2", "zod-to-json-schema": "^3.24.5", "zod-to-ts": "^1.2.0" }, "optionalDependencies": { "sharp": "^0.33.3" }, "bin": { "astro": "astro.js" } }, "sha512-cRGq2llKOhV3XMcYwQpfBIUcssN6HEK5CRbcMxAfd9OcFhvWE7KUy50zLioAZVVl3AqgUTJoNTlmZfD2eG0G1w=="], - "astro-expressive-code": ["astro-expressive-code@0.41.2", "", { "dependencies": { "rehype-expressive-code": "^0.41.2" }, "peerDependencies": { "astro": "^4.0.0-beta || ^5.0.0-beta || ^3.3.0" } }, "sha512-HN0jWTnhr7mIV/2e6uu4PPRNNo/k4UEgTLZqbp3MrHU+caCARveG2yZxaZVBmxyiVdYqW5Pd3u3n2zjnshixbw=="], + "astro-expressive-code": ["astro-expressive-code@0.41.3", "", { "dependencies": { "rehype-expressive-code": "^0.41.3" }, "peerDependencies": { "astro": "^4.0.0-beta || ^5.0.0-beta || ^3.3.0" } }, "sha512-u+zHMqo/QNLE2eqYRCrK3+XMlKakv33Bzuz+56V1gs8H0y6TZ0hIi3VNbIxeTn51NLn+mJfUV/A0kMNfE4rANw=="], "async-lock": ["async-lock@1.4.1", "", {}, "sha512-Az2ZTpuytrtqENulXwO3GGv1Bztugx6TT37NIo7imr/Qo0gsYiGtSdBa2B6fsXhTpVZDNfu1Qn3pk531e3q+nQ=="], - "atomic-sleep": ["atomic-sleep@1.0.0", "", {}, "sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ=="], - "available-typed-arrays": ["available-typed-arrays@1.0.7", "", { "dependencies": { "possible-typed-array-names": "^1.0.0" } }, "sha512-wvUjBtSGN7+7SjNpq/9M2Tg350UZD3q62IFZLbRAR1bSMlCo1ZaeW+BJ+D090e4hIIZLBcTDWe4Mh4jvUDajzQ=="], "aws-sdk": ["aws-sdk@2.1692.0", "", { "dependencies": { "buffer": "4.9.2", "events": "1.1.1", "ieee754": "1.1.13", "jmespath": "0.16.0", "querystring": "0.2.0", "sax": "1.2.1", "url": "0.10.3", "util": "^0.12.4", "uuid": "8.0.0", "xml2js": "0.6.2" } }, "sha512-x511uiJ/57FIsbgUe5csJ13k3uzu25uWQE+XqfBis/sB0SFoiElJWXRkgEAUh0U6n40eT3ay5Ue4oPkRMu1LYw=="], @@ -565,11 +589,9 @@ "bail": ["bail@2.0.2", "", {}, "sha512-0xO6mYd7JB2YesxDKplafRpsiOzPt9V02ddPCLbY1xYGPOX24NTyN50qnUxgCPcSoYMhKpAuBTjQoRZCAkUDRw=="], - "balanced-match": ["balanced-match@1.0.2", "", {}, "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="], + "bare-events": ["bare-events@2.6.0", "", {}, "sha512-EKZ5BTXYExaNqi3I3f9RtEsaI/xBSGjE0XZCZilPzFAV/goswFHuPd9jEZlPIZ/iNZJwDSao9qRiScySz7MbQg=="], - "bare-events": ["bare-events@2.5.4", "", {}, "sha512-+gFfDkR8pj4/TrWCGUGWmJIkBwuxPS5F+a5yWjOHQt2hHvNZd5YLzadjmDUtFmMM4y429bnKLa8bYBMHcYdnQA=="], - - "bare-fs": ["bare-fs@4.1.5", "", { "dependencies": { "bare-events": "^2.5.4", "bare-path": "^3.0.0", "bare-stream": "^2.6.4" }, "peerDependencies": { "bare-buffer": "*" }, "optionalPeers": ["bare-buffer"] }, "sha512-1zccWBMypln0jEE05LzZt+V/8y8AQsQQqxtklqaIyg5nu6OAYFhZxPXinJTSG+kU5qyNmeLgcn9AW7eHiCHVLA=="], + "bare-fs": ["bare-fs@4.1.6", "", { "dependencies": { "bare-events": "^2.5.4", "bare-path": "^3.0.0", "bare-stream": "^2.6.4" }, "peerDependencies": { "bare-buffer": "*" }, "optionalPeers": ["bare-buffer"] }, "sha512-25RsLF33BqooOEFNdMcEhMpJy8EoR88zSMrnOQOaM3USnOK2VmaJ1uaQEwPA6AQjrv1lXChScosN6CzbwbO9OQ=="], "bare-os": ["bare-os@3.6.1", "", {}, "sha512-uaIjxokhFidJP+bmmvKSgiMzj2sV5GPHaZVAIktcxcpCyBFFWO+YlikVAdhmUo2vYFvFhOXIAlldqV29L8126g=="], @@ -585,6 +607,8 @@ "bcp-47-match": ["bcp-47-match@2.0.3", "", {}, "sha512-JtTezzbAibu8G0R9op9zb3vcWZd9JF6M0xOYGPn0fNCd7wOpRB1mU2mH9T8gaBGbAAyIIVgB2G7xG0GP98zMAQ=="], + "before-after-hook": ["before-after-hook@4.0.0", "", {}, "sha512-q6tR3RPqIB1pMiTRMFcZwuG5T8vwp+vUvEG0vuI6B+Rikh5BfPp2fQ82c925FOs+b0lcFQ8CFrL+KbilfZFhOQ=="], + "bl": ["bl@4.1.0", "", { "dependencies": { "buffer": "^5.5.0", "inherits": "^2.0.4", "readable-stream": "^3.4.0" } }, "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w=="], "blake3-wasm": ["blake3-wasm@2.1.5", "", {}, "sha512-F1+K8EbfOZE49dtoPtmxUQrpXaBIl3ICvasLh+nJta0xkz+9kF/7uet9fLnwKqhDrmj6g+6K3Tw9yQPUg2ka5g=="], @@ -597,11 +621,9 @@ "boxen": ["boxen@8.0.1", "", { "dependencies": { "ansi-align": "^3.0.1", "camelcase": "^8.0.0", "chalk": "^5.3.0", "cli-boxes": "^3.0.0", "string-width": "^7.2.0", "type-fest": "^4.21.0", "widest-line": "^5.0.0", "wrap-ansi": "^9.0.0" } }, "sha512-F3PH5k5juxom4xktynS7MoFY+NUWH5LC4CnH11YB8NPew+HLpmBLCybSAEyb2F+4pRXhuhWqFesoQd6DAyc2hw=="], - "brace-expansion": ["brace-expansion@2.0.1", "", { "dependencies": { "balanced-match": "^1.0.0" } }, "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA=="], - "brotli": ["brotli@1.3.3", "", { "dependencies": { "base64-js": "^1.1.2" } }, "sha512-oTKjJdShmDuGW94SyyaoQvAjf30dZaHnjJ8uAF+u2/vGJkJbJPJAT1gDiOJP5v1Zb6f9KEyW/1HpuaWIXtGHPg=="], - "browserslist": ["browserslist@4.25.0", "", { "dependencies": { "caniuse-lite": "^1.0.30001718", "electron-to-chromium": "^1.5.160", "node-releases": "^2.0.19", "update-browserslist-db": "^1.1.3" }, "bin": { "browserslist": "cli.js" } }, "sha512-PJ8gYKeS5e/whHBh8xrwYK+dAvEj7JXtz6uTucnMRB8OiGTsKccFekoRrjajPBHV8oOY+2tI4uxeceSimKwMFA=="], + "browserslist": ["browserslist@4.25.1", "", { "dependencies": { "caniuse-lite": "^1.0.30001726", "electron-to-chromium": "^1.5.173", "node-releases": "^2.0.19", "update-browserslist-db": "^1.1.3" }, "bin": { "browserslist": "cli.js" } }, "sha512-KGj0KoOMXLpSNkkEI6Z6mShmQy0bc1I+T7K9N81k4WWMrfz+6fQ6es80B/YLAeRoKvjYE1YSHHOW1qe9xIVzHw=="], "buffer": ["buffer@4.9.2", "", { "dependencies": { "base64-js": "^1.0.2", "ieee754": "^1.1.4", "isarray": "^1.0.0" } }, "sha512-xq+q3SRMOxGivLhBNaUdC64hDTQwejJ+H0T/NB1XMtTVEwNTrfFF3gAxiyW0Bu/xWEGhjVKgUcMhCrUy2+uCWg=="], @@ -619,7 +641,7 @@ "camelcase": ["camelcase@8.0.0", "", {}, "sha512-8WB3Jcas3swSvjIeA2yvCJ+Miyz5l1ZmB6HFb9R1317dt9LCQoswg/BGrmAmkWVEszSrrg4RwmO46qIm2OEnSA=="], - "caniuse-lite": ["caniuse-lite@1.0.30001720", "", {}, "sha512-Ec/2yV2nNPwb4DnTANEV99ZWwm3ZWfdlfkQbWSDDt+PsXEVYwlhPH8tdMaPunYTKKmz7AnHi2oNEi1GcmKCD8g=="], + "caniuse-lite": ["caniuse-lite@1.0.30001727", "", {}, "sha512-pB68nIHmbN6L/4C6MH1DokyR3bYqFwjaSs/sWDHGj4CTcFtQUQMuJftVwWkXq7mNWOybD3KhUv3oWHoGxgP14Q=="], "ccount": ["ccount@2.0.1", "", {}, "sha512-eyrF0jiFpY+3drT6383f1qhkbGsLSifNAjA61IUjZjmLCWjItY6LB9ft9YhoDgwfmclB2zhu51Lc7+95b8NRAg=="], @@ -637,7 +659,7 @@ "chownr": ["chownr@1.1.4", "", {}, "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg=="], - "ci-info": ["ci-info@4.2.0", "", {}, "sha512-cYY9mypksY8NRqgDB1XD1RiJL338v/551niynFTGkZOO2LHuB2OmOYxDIe/ttN9AHwrqdum1360G3ald0W9kCg=="], + "ci-info": ["ci-info@4.3.0", "", {}, "sha512-l+2bNRMiQgcfILUi33labAZYIWlH1kWDp+ecNo5iisRKrbm0xcRyCww71/YU0Fkw0mAFpz9bJayXPjey6vkmaQ=="], "clean-git-ref": ["clean-git-ref@2.0.1", "", {}, "sha512-bLSptAy2P0s6hU4PzuIMKmMJJSE6gLXGH1cntDu7bWJUksvuM+7ReOK61mozULErYvP6a15rnYl0zFDef+pyPw=="], @@ -681,9 +703,11 @@ "cross-fetch": ["cross-fetch@3.2.0", "", { "dependencies": { "node-fetch": "^2.7.0" } }, "sha512-Q+xVJLoGOeIMXZmbUK4HYk+69cQH6LudR0Vu/pRm2YlU/hDV9CiS0gKUMaWY5f2NeUH9C1nV3bsTlCo0FsTV1Q=="], + "cross-spawn": ["cross-spawn@7.0.6", "", { "dependencies": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", "which": "^2.0.1" } }, "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA=="], + "crossws": ["crossws@0.3.5", "", { "dependencies": { "uncrypto": "^0.1.3" } }, "sha512-ojKiDvcmByhwa8YYqbQI/hg7MEU0NC03+pSdEq4ZUnZR9xXpwk7E43SMNGkn+JxJGPFtNvQ48+vV2p+P1ml5PA=="], - "css-selector-parser": ["css-selector-parser@3.1.2", "", {}, "sha512-WfUcL99xWDs7b3eZPoRszWVfbNo8ErCF15PTvVROjkShGlAfjIkG6hlfj/sl6/rfo5Q9x9ryJ3VqVnAZDA+gcw=="], + "css-selector-parser": ["css-selector-parser@3.1.3", "", {}, "sha512-gJMigczVZqYAk0hPVzx/M4Hm1D9QOtqkdQk9005TNzDIUGzo5cnHEDiKUT7jGPximL/oYb+LIitcHFQ4aKupxg=="], "css-tree": ["css-tree@3.1.0", "", { "dependencies": { "mdn-data": "2.12.2", "source-map-js": "^1.0.1" } }, "sha512-0eW44TGN5SQXU1mWSkKwFstI/22X2bG1nYzZTYMAWjylYURhse752YgbE4Cx46AC+bAvI+/dYTPRk1LqSUnu6w=="], @@ -691,15 +715,11 @@ "csstype": ["csstype@3.1.3", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="], - "data-uri-to-buffer": ["data-uri-to-buffer@2.0.2", "", {}, "sha512-ND9qDTLc6diwj+Xe5cdAgVTbLVdXbtxTJRXRhli8Mowuaan+0EJOtdqJ0QCHNSSPyoXGx9HX2/VMnKeC34AChA=="], - - "dateformat": ["dateformat@4.6.3", "", {}, "sha512-2P0p0pFGzHS5EMnhdxQi7aJN+iMheud0UhG4dlE1DLAlvL8JHjJJTX/CSm4JXwV0Ka5nGk3zC5mcb5bUQUxxMA=="], - "debug": ["debug@4.4.1", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ=="], "decimal.js": ["decimal.js@10.5.0", "", {}, "sha512-8vDa8Qxvr/+d94hSh5P3IJwI5t8/c0KsMp+g8bNw9cY2icONa5aPfvKeieW1WlG0WQYwwhJ7mjui2xtiePQSXw=="], - "decode-named-character-reference": ["decode-named-character-reference@1.1.0", "", { "dependencies": { "character-entities": "^2.0.0" } }, "sha512-Wy+JTSbFThEOXQIR2L6mxJvEs+veIzpmqD7ynWxMXGpnk3smkHQOp6forLdHsKpAMW9iJpaBBIxz285t1n1C3w=="], + "decode-named-character-reference": ["decode-named-character-reference@1.2.0", "", { "dependencies": { "character-entities": "^2.0.0" } }, "sha512-c6fcElNV6ShtZXmsgNgFFV5tVX2PaV4g+MOAkb8eXHvn6sryJBrZa9r0zV6+dtTyoCKxtDy5tyQ5ZwQuidtd+Q=="], "decompress-response": ["decompress-response@6.0.0", "", { "dependencies": { "mimic-response": "^3.1.0" } }, "sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ=="], @@ -733,8 +753,6 @@ "diff": ["diff@8.0.2", "", {}, "sha512-sSuxWU5j5SR9QQji/o2qMvqRNYRDOcBTgsJ/DeCf4iSN4gW+gNMXM7wFIP+fdXZxoNiAnHUTGjCr+TSWXdRDKg=="], - "diff-match-patch": ["diff-match-patch@1.0.5", "", {}, "sha512-IayShXAgj/QMXgB0IWmKx+rOPuGMhqm5w6jvFxmVenXKIzRqTAAsbBPT3kWQeGANj3jGgvcvv4yK6SxqYmikgw=="], - "diff3": ["diff3@0.0.3", "", {}, "sha512-iSq8ngPOt0K53A6eVr4d5Kn6GNrM2nQZtC740pzIriHtn4pOQ2lyzEXQMBeVcWERN0ye7fhBsk9PbLLQOnUx/g=="], "direction": ["direction@2.0.1", "", { "bin": { "direction": "cli.js" } }, "sha512-9S6m9Sukh1cZNknO1CWAr2QAWsbKLafQiyM5gZ7VgXHeuaoUwffKN4q6NC4A/Mf9iiPlOXQEKW/Mv/mh9/3YFA=="], @@ -745,21 +763,19 @@ "dunder-proto": ["dunder-proto@1.0.1", "", { "dependencies": { "call-bind-apply-helpers": "^1.0.1", "es-errors": "^1.3.0", "gopd": "^1.2.0" } }, "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A=="], - "duplexify": ["duplexify@4.1.3", "", { "dependencies": { "end-of-stream": "^1.4.1", "inherits": "^2.0.3", "readable-stream": "^3.1.1", "stream-shift": "^1.0.2" } }, "sha512-M3BmBhwJRZsSx38lZyhE53Csddgzl5R7xGJNk7CVddZD6CcmwMCH8J+7AprIrQKH7TonKxaCjcv27Qmf+sQ+oA=="], - "ee-first": ["ee-first@1.1.1", "", {}, "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow=="], - "electron-to-chromium": ["electron-to-chromium@1.5.161", "", {}, "sha512-hwtetwfKNZo/UlwHIVBlKZVdy7o8bIZxxKs0Mv/ROPiQQQmDgdm5a+KvKtBsxM8ZjFzTaCeLoodZ8jiBE3o9rA=="], + "electron-to-chromium": ["electron-to-chromium@1.5.183", "", {}, "sha512-vCrDBYjQCAEefWGjlK3EpoSKfKbT10pR4XXPdn65q7snuNOZnthoVpBfZPykmDapOKfoD+MMIPG8ZjKyyc9oHA=="], "emoji-regex": ["emoji-regex@10.4.0", "", {}, "sha512-EC+0oUMY1Rqm4O6LLrgjtYDvcVYTy7chDnM4Q7030tP4Kwj3u/pR6gP9ygnp2CJMK5Gq+9Q2oqmrFJAz01DXjw=="], "encodeurl": ["encodeurl@2.0.0", "", {}, "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg=="], - "end-of-stream": ["end-of-stream@1.4.4", "", { "dependencies": { "once": "^1.4.0" } }, "sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q=="], + "end-of-stream": ["end-of-stream@1.4.5", "", { "dependencies": { "once": "^1.4.0" } }, "sha512-ooEGc6HP26xXq/N+GCGOT0JKCLDGrq2bQUZrQ7gyrJiZANJ/8YDTxTpQBXGMn+WbIQXNVpyWymm7KYVICQnyOg=="], - "entities": ["entities@6.0.0", "", {}, "sha512-aKstq2TDOndCn4diEyp9Uq/Flu2i1GlLkc6XIDQSDMuaFE3OPW5OphLCyQ5SpSJZTb4reN+kTcYru5yIfXoRPw=="], + "entities": ["entities@6.0.1", "", {}, "sha512-aN97NXWF6AWBTahfVOIrB/NShkzi5H7F9r1s9mD3cDj4Ko5f2qhhVoYMibXF7GlLveb/D2ioWay8lxI97Ven3g=="], - "env-paths": ["env-paths@3.0.0", "", {}, "sha512-dtJUTepzMW3Lm/NPxRf3wP4642UWhjL2sQxc+ym2YMj1m/H2zDNQOlezafzkHwn6sMstjHTwG6iQQsctDW/b1A=="], + "error-stack-parser-es": ["error-stack-parser-es@1.0.5", "", {}, "sha512-5qucVt2XcuGMcEGgWI7i+yZpmpByQ8J1lHhcL7PwqCwu9FPP3VUXzT4ltHe5i2z9dePwEHcDVOAfSnHsOlCXRA=="], "es-define-property": ["es-define-property@1.0.1", "", {}, "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g=="], @@ -773,13 +789,13 @@ "esast-util-from-js": ["esast-util-from-js@2.0.1", "", { "dependencies": { "@types/estree-jsx": "^1.0.0", "acorn": "^8.0.0", "esast-util-from-estree": "^2.0.0", "vfile-message": "^4.0.0" } }, "sha512-8Ja+rNJ0Lt56Pcf3TAmpBZjmx8ZcK5Ts4cAzIOjsjevg9oSXJnl6SUQ2EevU8tv3h6ZLWmoKL5H4fgWvdvfETw=="], - "esbuild": ["esbuild@0.25.5", "", { "optionalDependencies": { "@esbuild/aix-ppc64": "0.25.5", "@esbuild/android-arm": "0.25.5", "@esbuild/android-arm64": "0.25.5", "@esbuild/android-x64": "0.25.5", "@esbuild/darwin-arm64": "0.25.5", "@esbuild/darwin-x64": "0.25.5", "@esbuild/freebsd-arm64": "0.25.5", "@esbuild/freebsd-x64": "0.25.5", "@esbuild/linux-arm": "0.25.5", "@esbuild/linux-arm64": "0.25.5", "@esbuild/linux-ia32": "0.25.5", "@esbuild/linux-loong64": "0.25.5", "@esbuild/linux-mips64el": "0.25.5", "@esbuild/linux-ppc64": "0.25.5", "@esbuild/linux-riscv64": "0.25.5", "@esbuild/linux-s390x": "0.25.5", "@esbuild/linux-x64": "0.25.5", "@esbuild/netbsd-arm64": "0.25.5", "@esbuild/netbsd-x64": "0.25.5", "@esbuild/openbsd-arm64": "0.25.5", "@esbuild/openbsd-x64": "0.25.5", "@esbuild/sunos-x64": "0.25.5", "@esbuild/win32-arm64": "0.25.5", "@esbuild/win32-ia32": "0.25.5", "@esbuild/win32-x64": "0.25.5" }, "bin": { "esbuild": "bin/esbuild" } }, "sha512-P8OtKZRv/5J5hhz0cUAdu/cLuPIKXpQl1R9pZtvmHWQvrAUVd0UNIPT4IB4W3rNOqVO0rlqHmCIbSwxh/c9yUQ=="], + "esbuild": ["esbuild@0.25.6", "", { "optionalDependencies": { "@esbuild/aix-ppc64": "0.25.6", "@esbuild/android-arm": "0.25.6", "@esbuild/android-arm64": "0.25.6", "@esbuild/android-x64": "0.25.6", "@esbuild/darwin-arm64": "0.25.6", "@esbuild/darwin-x64": "0.25.6", "@esbuild/freebsd-arm64": "0.25.6", "@esbuild/freebsd-x64": "0.25.6", "@esbuild/linux-arm": "0.25.6", "@esbuild/linux-arm64": "0.25.6", "@esbuild/linux-ia32": "0.25.6", "@esbuild/linux-loong64": "0.25.6", "@esbuild/linux-mips64el": "0.25.6", "@esbuild/linux-ppc64": "0.25.6", "@esbuild/linux-riscv64": "0.25.6", "@esbuild/linux-s390x": "0.25.6", "@esbuild/linux-x64": "0.25.6", "@esbuild/netbsd-arm64": "0.25.6", "@esbuild/netbsd-x64": "0.25.6", "@esbuild/openbsd-arm64": "0.25.6", "@esbuild/openbsd-x64": "0.25.6", "@esbuild/openharmony-arm64": "0.25.6", "@esbuild/sunos-x64": "0.25.6", "@esbuild/win32-arm64": "0.25.6", "@esbuild/win32-ia32": "0.25.6", "@esbuild/win32-x64": "0.25.6" }, "bin": { "esbuild": "bin/esbuild" } }, "sha512-GVuzuUwtdsghE3ocJ9Bs8PNoF13HNQ5TXbEi2AhvVb8xU1Iwt9Fos9FEamfoee+u/TOsn7GUWc04lz46n2bbTg=="], "escalade": ["escalade@3.2.0", "", {}, "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA=="], "escape-html": ["escape-html@1.0.3", "", {}, "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow=="], - "escape-string-regexp": ["escape-string-regexp@1.0.5", "", {}, "sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg=="], + "escape-string-regexp": ["escape-string-regexp@5.0.0", "", {}, "sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw=="], "estree-util-attach-comments": ["estree-util-attach-comments@3.0.0", "", { "dependencies": { "@types/estree": "^1.0.0" } }, "sha512-cKUwm/HUcTDsYh/9FgnuFqpfquUbwIqwKM26BVCGDPVgvaCl/nDCCjUfiLlx6lsEZ3Z4RFxNbOQ60pkaEwFxGw=="], @@ -803,7 +819,7 @@ "eventsource": ["eventsource@3.0.7", "", { "dependencies": { "eventsource-parser": "^3.0.1" } }, "sha512-CRT1WTyuQoD771GW56XEZFQ/ZoSfWid1alKGDYMmkt2yl8UXrVR4pspqWNEcqKvVIzg6PAltWjxcSSPrboA4iA=="], - "eventsource-parser": ["eventsource-parser@3.0.2", "", {}, "sha512-6RxOBZ/cYgd8usLwsEl+EC09Au/9BcmCKYF2/xbml6DNczf7nv0MQb+7BA2F+li6//I+28VNlQR37XfQtcAJuA=="], + "eventsource-parser": ["eventsource-parser@3.0.3", "", {}, "sha512-nVpZkTMM9rF6AQ9gPJpFsNAMt48wIzB5TQgiTLdHiuO8XEDhUgZEhqKlZWXbIzo9VmJ/HvysHqEaVeD5v9TPvA=="], "exit-hook": ["exit-hook@2.2.1", "", {}, "sha512-eNTPlAD67BmP31LDINZ3U7HSF8l57TxOY2PmBJ1shpCvpnxBF93mWCE8YHBnXs8qiUZJc9WDcWIeC3a2HIAMfw=="], @@ -811,27 +827,23 @@ "express": ["express@5.1.0", "", { "dependencies": { "accepts": "^2.0.0", "body-parser": "^2.2.0", "content-disposition": "^1.0.0", "content-type": "^1.0.5", "cookie": "^0.7.1", "cookie-signature": "^1.2.1", "debug": "^4.4.0", "encodeurl": "^2.0.0", "escape-html": "^1.0.3", "etag": "^1.8.1", "finalhandler": "^2.1.0", "fresh": "^2.0.0", "http-errors": "^2.0.0", "merge-descriptors": "^2.0.0", "mime-types": "^3.0.0", "on-finished": "^2.4.1", "once": "^1.4.0", "parseurl": "^1.3.3", "proxy-addr": "^2.0.7", "qs": "^6.14.0", "range-parser": "^1.2.1", "router": "^2.2.0", "send": "^1.1.0", "serve-static": "^2.2.0", "statuses": "^2.0.1", "type-is": "^2.0.1", "vary": "^1.1.2" } }, "sha512-DT9ck5YIRU+8GYzzU5kT3eHGA5iL+1Zd0EutOmTE9Dtk+Tvuzd23VBU+ec7HPNSTxXYO55gPV/hq4pSBJDjFpA=="], - "express-rate-limit": ["express-rate-limit@7.5.0", "", { "peerDependencies": { "express": "^4.11 || 5 || ^5.0.0-beta.1" } }, "sha512-eB5zbQh5h+VenMPM3fh+nw1YExi5nMr6HUCR62ELSP11huvxm/Uir1H1QEyTkk5QX6A58pX6NmaTMceKZ0Eodg=="], + "express-rate-limit": ["express-rate-limit@7.5.1", "", { "peerDependencies": { "express": ">= 4.11" } }, "sha512-7iN8iPMDzOMHPUYllBEsQdWVB6fPDMPqwjBaFrgr4Jgr/+okjvzAy+UHlYYL/Vs0OsOrMkwS6PJDkFlJwoxUnw=="], - "expressive-code": ["expressive-code@0.41.2", "", { "dependencies": { "@expressive-code/core": "^0.41.2", "@expressive-code/plugin-frames": "^0.41.2", "@expressive-code/plugin-shiki": "^0.41.2", "@expressive-code/plugin-text-markers": "^0.41.2" } }, "sha512-aLZiZaqorRtNExtGpUjK9zFH9aTpWeoTXMyLo4b4IcuXfPqtLPPxhRm/QlPb8QqIcMMXnSiGRHSFpQfX0m7HJw=="], + "expressive-code": ["expressive-code@0.41.3", "", { "dependencies": { "@expressive-code/core": "^0.41.3", "@expressive-code/plugin-frames": "^0.41.3", "@expressive-code/plugin-shiki": "^0.41.3", "@expressive-code/plugin-text-markers": "^0.41.3" } }, "sha512-YLnD62jfgBZYrXIPQcJ0a51Afv9h8VlWqEGK9uU2T5nL/5rb8SnA86+7+mgCZe5D34Tff5RNEA5hjNVJYHzrFg=="], - "exsolve": ["exsolve@1.0.5", "", {}, "sha512-pz5dvkYYKQ1AHVrgOzBKWeP4u4FRb3a6DNK2ucr0OoNwYIU4QWsJ+NM36LLzORT+z845MzKHHhpXiUF5nvQoJg=="], + "exsolve": ["exsolve@1.0.7", "", {}, "sha512-VO5fQUzZtI6C+vx4w/4BWJpg3s/5l+6pRQEHzFRM8WFi4XffSP1Z+4qi7GbjWbvRQEbdIco5mIMq+zX4rPuLrw=="], "extend": ["extend@3.0.2", "", {}, "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g=="], + "fast-content-type-parse": ["fast-content-type-parse@3.0.0", "", {}, "sha512-ZvLdcY8P+N8mGQJahJV5G4U88CSvT1rP8ApL6uETe88MBXrBHAkZlSEySdUlyztF7ccb+Znos3TFqaepHxdhBg=="], + "fast-deep-equal": ["fast-deep-equal@3.1.3", "", {}, "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="], "fast-fifo": ["fast-fifo@1.3.2", "", {}, "sha512-/d9sfos4yxzpwkDkuN7k2SqFKtYNmCTzgfEpz82x34IM9/zc8KGxQoXg1liNC/izpRM/MBdt44Nmx41ZWqk+FQ=="], - "fast-redact": ["fast-redact@3.5.0", "", {}, "sha512-dwsoQlS7h9hMeYUq1W++23NDcBLV4KqONnITDV9DjfS3q1SgDGVrBdvvTLUotWtPSD7asWDV9/CmsZPy8Hf70A=="], + "fast-json-stable-stringify": ["fast-json-stable-stringify@2.1.0", "", {}, "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw=="], - "fast-safe-stringify": ["fast-safe-stringify@2.1.1", "", {}, "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA=="], - - "fdir": ["fdir@6.4.5", "", { "peerDependencies": { "picomatch": "^3 || ^4" }, "optionalPeers": ["picomatch"] }, "sha512-4BG7puHpVsIYxZUbiUE3RqGloLaSSwzYie5jvasC4LWuBWzZawynvYouhjbQKw2JuIGYdm0DzIxl8iVidKlUEw=="], - - "fflate": ["fflate@0.8.2", "", {}, "sha512-cPJU47OaAoCbg0pBvzsgpTPhmhqI5eJjh/JIu8tPj5q+T7iLvW/JAYUqmE7KOB4R1ZyEhzBaIQpQpardBF5z8A=="], - - "file-type": ["file-type@20.5.0", "", { "dependencies": { "@tokenizer/inflate": "^0.2.6", "strtok3": "^10.2.0", "token-types": "^6.0.0", "uint8array-extras": "^1.4.0" } }, "sha512-BfHZtG/l9iMm4Ecianu7P8HRD2tBHLtjXinm4X62XBOYzi7CYA7jyqfJzOvXHqzVrVPYqBo2/GvbARMaaJkKVg=="], + "fdir": ["fdir@6.4.6", "", { "peerDependencies": { "picomatch": "^3 || ^4" }, "optionalPeers": ["picomatch"] }, "sha512-hiFoqpyZcfNm1yc4u8oWCf9A2c4D3QjCrks3zmoVKVxpQRzmPNar1hUJcBG2RQHvEVGDN+Jm81ZheVLAQMK6+w=="], "finalhandler": ["finalhandler@2.1.0", "", { "dependencies": { "debug": "^4.4.0", "encodeurl": "^2.0.0", "escape-html": "^1.0.3", "on-finished": "^2.4.1", "parseurl": "^1.3.3", "statuses": "^2.0.1" } }, "sha512-/t88Ty3d5JWQbWYgaOGCCYfXRwV1+be02WqYYlL6h0lEiUAMPM8o8qKGO01YIkOHzka2up08wvgYD0mDiI+q3Q=="], @@ -863,22 +875,16 @@ "get-proto": ["get-proto@1.0.1", "", { "dependencies": { "dunder-proto": "^1.0.1", "es-object-atoms": "^1.0.0" } }, "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g=="], - "get-source": ["get-source@2.0.12", "", { "dependencies": { "data-uri-to-buffer": "^2.0.0", "source-map": "^0.6.1" } }, "sha512-X5+4+iD+HoSeEED+uwrQ07BOQr0kEDFMVqqpBuI+RaZBpBpHCuXxo70bjar6f0b0u/DQJsJ7ssurpP0V60Az+w=="], - "github-from-package": ["github-from-package@0.0.0", "", {}, "sha512-SyHy3T1v2NUXn29OsWdxmK6RwHD+vkj3v8en8AOBZ1wBQ/hCAQ5bAQTD02kW4W9tUp/3Qh6J8r9EvntiyCmOOw=="], "github-slugger": ["github-slugger@2.0.0", "", {}, "sha512-IaOQ9puYtjrkq7Y0Ygl9KDZnrf/aiUJYUpVf89y8kyaxbRG7Y1SrX/jaumrv81vc61+kiMempujsM3Yw7w5qcw=="], "glob-to-regexp": ["glob-to-regexp@0.4.1", "", {}, "sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw=="], - "globals": ["globals@11.12.0", "", {}, "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA=="], - "gopd": ["gopd@1.2.0", "", {}, "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg=="], "h3": ["h3@1.15.3", "", { "dependencies": { "cookie-es": "^1.2.2", "crossws": "^0.3.4", "defu": "^6.1.4", "destr": "^2.0.5", "iron-webcrypto": "^1.2.1", "node-mock-http": "^1.0.0", "radix3": "^1.1.2", "ufo": "^1.6.1", "uncrypto": "^0.1.3" } }, "sha512-z6GknHqyX0h9aQaTx22VZDf6QyZn+0Nh+Ym8O/u0SGSkyF5cuTJYKlc8MkzW3Nzf9LE1ivcpmYC3FUGpywhuUQ=="], - "has-flag": ["has-flag@4.0.0", "", {}, "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ=="], - "has-property-descriptors": ["has-property-descriptors@1.0.2", "", { "dependencies": { "es-define-property": "^1.0.0" } }, "sha512-55JNKuIW+vq4Ke1BjOTjM2YctQIvCT7GFzHwmfZPGo5wnrgkid0YQtnAleFSqumZm4az3n2BS+erby5ipJdgrg=="], "has-symbols": ["has-symbols@1.1.0", "", {}, "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ=="], @@ -1001,13 +1007,13 @@ "isarray": ["isarray@1.0.0", "", {}, "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ=="], + "isexe": ["isexe@2.0.0", "", {}, "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw=="], + "isomorphic-git": ["isomorphic-git@1.32.1", "", { "dependencies": { "async-lock": "^1.4.1", "clean-git-ref": "^2.0.1", "crc-32": "^1.2.0", "diff3": "0.0.3", "ignore": "^5.1.4", "minimisted": "^2.0.0", "pako": "^1.0.10", "path-browserify": "^1.0.1", "pify": "^4.0.1", "readable-stream": "^3.4.0", "sha.js": "^2.4.9", "simple-get": "^4.0.1" }, "bin": { "isogit": "cli.cjs" } }, "sha512-NZCS7qpLkCZ1M/IrujYBD31sM6pd/fMVArK4fz4I7h6m0rUW2AsYU7S7zXeABuHL6HIfW6l53b4UQ/K441CQjg=="], "jmespath": ["jmespath@0.16.0", "", {}, "sha512-9FzQjJ7MATs1tSpnco1K6ayiYE3figslrXA72G2HQ/n76RzvYlofyi5QM+iX4YRs/pu3yzxlVQSST23+dMDknw=="], - "jose": ["jose@5.2.3", "", {}, "sha512-KUXdbctm1uHVL8BYhnyHkgp3zDX5KW8ZhAKVFEfUbU2P8Alpzjb+48hHvjOdQIyPshoblhzsuqOwEEAbtHVirA=="], - - "joycon": ["joycon@3.1.1", "", {}, "sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw=="], + "jose": ["jose@6.0.11", "", {}, "sha512-QxG7EaliDARm1O1S8BGakqncGT9s25bKL1WSf6/oa17Tkqwi8D2ZNglqCF+DsYF88/rV66Q/Q2mFAy697E1DUg=="], "js-base64": ["js-base64@3.7.7", "", {}, "sha512-7rCnleh0z2CkXhH67J8K1Ytz0b2Y+yxTPL+/KOJoa20hfnVQ/3/T6W/KflYI4bRHRagNeXeU2bkNGI3v1oS/lw=="], @@ -1017,16 +1023,14 @@ "jsesc": ["jsesc@3.1.0", "", { "bin": { "jsesc": "bin/jsesc" } }, "sha512-/sM3dO2FOzXjKQhJuo0Q173wf2KOo8t4I8vHy6lF9poUp7bKT0/NHE8fPX23PwfhnykfqnC2xRxOnVw5XuGIaA=="], - "json-rpc-2.0": ["json-rpc-2.0@1.7.0", "", {}, "sha512-asnLgC1qD5ytP+fvBP8uL0rvj+l8P6iYICbzZ8dVxCpESffVjzA7KkYkbKCIbavs7cllwH1ZUaNtJwphdeRqpg=="], - "json-schema": ["json-schema@0.4.0", "", {}, "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA=="], + "json-schema-traverse": ["json-schema-traverse@0.4.1", "", {}, "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg=="], + "json-schema-walker": ["json-schema-walker@2.0.0", "", { "dependencies": { "@apidevtools/json-schema-ref-parser": "^11.1.0", "clone": "^2.1.2" } }, "sha512-nXN2cMky0Iw7Af28w061hmxaPDaML5/bQD9nwm1lOoIKEGjHcRGxqWe4MfrkYThYAPjSUhmsp4bJNoLAyVn9Xw=="], "json5": ["json5@2.2.3", "", { "bin": { "json5": "lib/cli.js" } }, "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg=="], - "jsondiffpatch": ["jsondiffpatch@0.6.0", "", { "dependencies": { "@types/diff-match-patch": "^1.0.36", "chalk": "^5.3.0", "diff-match-patch": "^1.0.5" }, "bin": { "jsondiffpatch": "bin/jsondiffpatch.js" } }, "sha512-3QItJOXp2AP1uv7waBkao5nCvhEv+QmJAd38Ybq7wNI74Q+BBmnLn4EDKz6yI9xGAIQoUF87qHt+kc1IVxB4zQ=="], - "kleur": ["kleur@4.1.5", "", {}, "sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ=="], "klona": ["klona@2.0.6", "", {}, "sha512-dhG34DXATL5hSxJbIexCft8FChFXtmskoZYnoPWjXQuebWYCNkVeV3KkGegCK9CP1oswI/vQibS2GY7Em/sJJA=="], @@ -1035,8 +1039,6 @@ "language-map": ["language-map@1.5.0", "", {}, "sha512-n7gFZpe+DwEAX9cXVTw43i3wiudWDDtSn28RmdnS/HCPr284dQI/SztsamWanRr75oSlKSaGbV2nmWCTzGCoVg=="], - "leven": ["leven@2.1.0", "", {}, "sha512-nvVPLpIHUxCUoRLrFqTgSxXJ614d8AgQoWl7zPe/2VadE8+1dpU3LBhowRuBAcuwruWtOdD8oYC9jDNJjXDPyA=="], - "longest-streak": ["longest-streak@3.1.0", "", {}, "sha512-9Ri+o0JYgehTaVBBDoMqIl8GXtbWg711O3srftcHhZ0dqnETqLaoIK0x17fUw9rFSlK/0NlsKe0Ahhyl5pXE2g=="], "lru-cache": ["lru-cache@6.0.0", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA=="], @@ -1181,9 +1183,7 @@ "mimic-response": ["mimic-response@3.1.0", "", {}, "sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ=="], - "miniflare": ["miniflare@4.20250525.1", "", { "dependencies": { "@cspotcode/source-map-support": "0.8.1", "acorn": "8.14.0", "acorn-walk": "8.3.2", "exit-hook": "2.2.1", "glob-to-regexp": "0.4.1", "sharp": "^0.33.5", "stoppable": "1.1.0", "undici": "^5.28.5", "workerd": "1.20250525.0", "ws": "8.18.0", "youch": "3.3.4", "zod": "3.22.3" }, "bin": { "miniflare": "bootstrap.js" } }, "sha512-4PJlT5WA+hfclFU5Q7xnpG1G1VGYTXaf/3iu6iKQ8IsbSi9QvPTA2bSZ5goCFxmJXDjV4cxttVxB0Wl1CLuQ0w=="], - - "minimatch": ["minimatch@5.1.6", "", { "dependencies": { "brace-expansion": "^2.0.1" } }, "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g=="], + "miniflare": ["miniflare@4.20250709.0", "", { "dependencies": { "@cspotcode/source-map-support": "0.8.1", "acorn": "8.14.0", "acorn-walk": "8.3.2", "exit-hook": "2.2.1", "glob-to-regexp": "0.4.1", "sharp": "^0.33.5", "stoppable": "1.1.0", "undici": "^5.28.5", "workerd": "1.20250709.0", "ws": "8.18.0", "youch": "4.1.0-beta.10", "zod": "3.22.3" }, "bin": { "miniflare": "bootstrap.js" } }, "sha512-dRGXi6Do9ArQZt7205QGWZ1tD6k6xQNY/mAZBAtiaQYvKxFuNyiHYlFnSN8Co4AFCVOozo/U52sVAaHvlcmnew=="], "minimist": ["minimist@1.2.8", "", {}, "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA=="], @@ -1191,14 +1191,10 @@ "mkdirp-classic": ["mkdirp-classic@0.5.3", "", {}, "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A=="], - "mri": ["mri@1.1.4", "", {}, "sha512-6y7IjGPm8AzlvoUrwAaw1tLnUBudaS3752vcd8JtrpGGQn+rXIe63LFVHm/YMwtqAuh+LJPCFdlLYPWM1nYn6w=="], - "mrmime": ["mrmime@2.0.1", "", {}, "sha512-Y3wQdFg2Va6etvQ5I82yUhGdsKrcYox6p7FfL1LbK2J4V01F9TGlepTIhnK24t7koZibmg82KGglhA1XK5IsLQ=="], "ms": ["ms@2.1.3", "", {}, "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="], - "mustache": ["mustache@4.2.0", "", { "bin": { "mustache": "bin/mustache" } }, "sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ=="], - "nanoid": ["nanoid@3.3.11", "", { "bin": { "nanoid": "bin/nanoid.cjs" } }, "sha512-N8SpfPUnUp1bK+PMYW8qSWdl9U+wwNWI4QKxOYDy9JAro3WMX7p2OeVRF9v+347pnakNevPmiHhNmZ2HbFA76w=="], "napi-build-utils": ["napi-build-utils@2.0.0", "", {}, "sha512-GEbrYkbfF7MoNaoh2iGG84Mnf/WZfB0GdGEsM8wz7Expx/LlWf5U8t9nvJKXSp3qr5IsEbK04cBGhol/KwOsWA=="], @@ -1217,7 +1213,7 @@ "node-fetch-native": ["node-fetch-native@1.6.6", "", {}, "sha512-8Mc2HhqPdlIfedsuZoc3yioPuzp6b+L5jRCRY1QzuWZh2EGJVQrGppC6V6cF0bLdbW0+O2YpqCA25aF/1lvipQ=="], - "node-mock-http": ["node-mock-http@1.0.0", "", {}, "sha512-0uGYQ1WQL1M5kKvGRXWQ3uZCHtLTO8hln3oBjIusM75WoesZ909uQJs/Hb946i2SS+Gsrhkaa6iAO17jRIv6DQ=="], + "node-mock-http": ["node-mock-http@1.0.1", "", {}, "sha512-0gJJgENizp4ghds/Ywu2FCmcRsgBTmRQzYPZm61wy+Em2sBarSka0OhQS5huLBg6od1zkNpnWMCZloQDFVvOMQ=="], "node-releases": ["node-releases@2.0.19", "", {}, "sha512-xxOWJsBKtzAq7DY0J+DTzuz58K8e7sJbdgwkbMWQe8UYB6ekmsQ45q0M/tJDsGaZmbC+l7n57UV8Hl5tHxO9uw=="], @@ -1237,8 +1233,6 @@ "oidc-token-hash": ["oidc-token-hash@5.1.0", "", {}, "sha512-y0W+X7Ppo7oZX6eovsRkuzcSM40Bicg2JEJkDJ4irIt1wsYAP5MLSNv+QAogO8xivMffw/9OvV3um1pxXgt1uA=="], - "on-exit-leak-free": ["on-exit-leak-free@0.2.0", "", {}, "sha512-dqaz3u44QbRXQooZLTUKU41ZrzYrcvLISVgbrzbyCMxpmSLJvZ3ZamIJIZ29P6OhZIkNIQKosdeM6t1LYbA9hg=="], - "on-finished": ["on-finished@2.4.1", "", { "dependencies": { "ee-first": "1.1.1" } }, "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg=="], "once": ["once@1.4.0", "", { "dependencies": { "wrappy": "1" } }, "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w=="], @@ -1279,31 +1273,23 @@ "path-browserify": ["path-browserify@1.0.1", "", {}, "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g=="], + "path-key": ["path-key@3.1.1", "", {}, "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q=="], + "path-to-regexp": ["path-to-regexp@6.3.0", "", {}, "sha512-Yhpw4T9C6hPpgPeA28us07OJeqZ5EzQTkbfwuhsUg0c237RomFoETJgmp2sa3F/41gfLE6G5cqcYwznmeEeOlQ=="], "pathe": ["pathe@2.0.3", "", {}, "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w=="], - "peek-readable": ["peek-readable@7.0.0", "", {}, "sha512-nri2TO5JE3/mRryik9LlHFT53cgHfRK0Lt0BAZQXku/AW3E6XLt2GaY8siWi7dvW/m1z0ecn+J+bpDa9ZN3IsQ=="], - "picocolors": ["picocolors@1.1.1", "", {}, "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA=="], "picomatch": ["picomatch@4.0.2", "", {}, "sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg=="], "pify": ["pify@4.0.1", "", {}, "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g=="], - "pino": ["pino@7.11.0", "", { "dependencies": { "atomic-sleep": "^1.0.0", "fast-redact": "^3.0.0", "on-exit-leak-free": "^0.2.0", "pino-abstract-transport": "v0.5.0", "pino-std-serializers": "^4.0.0", "process-warning": "^1.0.0", "quick-format-unescaped": "^4.0.3", "real-require": "^0.1.0", "safe-stable-stringify": "^2.1.0", "sonic-boom": "^2.2.1", "thread-stream": "^0.15.1" }, "bin": { "pino": "bin.js" } }, "sha512-dMACeu63HtRLmCG8VKdy4cShCPKaYDR4youZqoSWLxl5Gu99HUw8bw75thbPv9Nip+H+QYX8o3ZJbTdVZZ2TVg=="], - - "pino-abstract-transport": ["pino-abstract-transport@0.5.0", "", { "dependencies": { "duplexify": "^4.1.2", "split2": "^4.0.0" } }, "sha512-+KAgmVeqXYbTtU2FScx1XS3kNyfZ5TrXY07V96QnUSFqo2gAqlvmaxH67Lj7SWazqsMabf+58ctdTcBgnOLUOQ=="], - - "pino-pretty": ["pino-pretty@5.1.3", "", { "dependencies": { "@hapi/bourne": "^2.0.0", "args": "^5.0.1", "chalk": "^4.0.0", "dateformat": "^4.5.1", "fast-safe-stringify": "^2.0.7", "jmespath": "^0.15.0", "joycon": "^3.0.0", "pump": "^3.0.0", "readable-stream": "^3.6.0", "rfdc": "^1.3.0", "split2": "^3.1.1", "strip-json-comments": "^3.1.1" }, "bin": { "pino-pretty": "bin.js" } }, "sha512-Zj+0TVdYKkAAIx9EUCL5e4TttwgsaFvJh2ceIMQeFCY8ak9tseEZQGSgpvyjEj1/iIVGIh5tdhkGEQWSMILKHA=="], - - "pino-std-serializers": ["pino-std-serializers@4.0.0", "", {}, "sha512-cK0pekc1Kjy5w9V2/n+8MkZwusa6EyyxfeQCB799CQRhRt/CqYKiWs5adeu8Shve2ZNffvfC/7J64A2PJo1W/Q=="], - - "pkce-challenge": ["pkce-challenge@4.1.0", "", {}, "sha512-ZBmhE1C9LcPoH9XZSdwiPtbPHZROwAnMy+kIFQVrnMCxY4Cudlz3gBOpzilgc0jOgRaiT3sIWfpMomW2ar2orQ=="], + "pkce-challenge": ["pkce-challenge@5.0.0", "", {}, "sha512-ueGLflrrnvwB3xuo/uGob5pd5FN7l0MsLf0Z87o/UQmRtwjvfylfc9MurIxRAWywCYTgrvpXBcqjV4OfCYGCIQ=="], "possible-typed-array-names": ["possible-typed-array-names@1.1.0", "", {}, "sha512-/+5VFTchJDoVj3bhoqi6UeymcD00DAwb1nJwamzPvHEszJ4FpF6SNNbUbOS8yI56qHzdV8eK0qEfOSiodkTdxg=="], - "postcss": ["postcss@8.5.4", "", { "dependencies": { "nanoid": "^3.3.11", "picocolors": "^1.1.1", "source-map-js": "^1.2.1" } }, "sha512-QSa9EBe+uwlGTFmHsPKokv3B/oEMQZxfqW0QqNCyhpa6mB1afzulwn8hihglqAb2pOw+BJgNlmXQ8la2VeHB7w=="], + "postcss": ["postcss@8.5.6", "", { "dependencies": { "nanoid": "^3.3.11", "picocolors": "^1.1.1", "source-map-js": "^1.2.1" } }, "sha512-3Ybi1tAuwAP9s0r1UQ2J4n5Y0G05bJkpUIO0/bI9MhwmD70S5aTWbXGBwxHrelT+XM1k6dM0pk+SwNkpTRN7Pg=="], "postcss-nested": ["postcss-nested@6.2.0", "", { "dependencies": { "postcss-selector-parser": "^6.1.1" }, "peerDependencies": { "postcss": "^8.2.14" } }, "sha512-HQbt28KulC5AJzG+cZtj9kvKB93CFCdLvog1WFLf1D+xmMvPGlBstkpTEZfK5+AN9hfJocyBFCNiqyS48bpgzQ=="], @@ -1313,19 +1299,15 @@ "prettier": ["prettier@3.5.3", "", { "bin": { "prettier": "bin/prettier.cjs" } }, "sha512-QQtaxnoDJeAkDvDKWCLiwIXkTgRhwYDEQCghU9Z6q03iyek/rxRh/2lC3HB7P8sWT2xC/y5JDctPLBIGzHKbhw=="], - "printable-characters": ["printable-characters@1.0.42", "", {}, "sha512-dKp+C4iXWK4vVYZmYSd0KBH5F/h1HoZRsbJ82AVKRO3PEo8L4lBS/vLwhVtpwwuYcoIsVY+1JYKR268yn480uQ=="], - "prismjs": ["prismjs@1.30.0", "", {}, "sha512-DEvV2ZF2r2/63V+tK8hQvrR2ZGn10srHbXviTlcv7Kpzw8jWiNTqbVgjO3IY8RxrrOUF8VPMQQFysYYYv0YZxw=="], - "process-warning": ["process-warning@1.0.0", "", {}, "sha512-du4wfLyj4yCZq1VupnVSZmRsPJsNuxoDQFdCFHLaYiEbFBD7QE0a+I4D7hOxrVnh78QE/YipFAj9lXHiXocV+Q=="], - "prompts": ["prompts@2.4.2", "", { "dependencies": { "kleur": "^3.0.3", "sisteransi": "^1.0.5" } }, "sha512-NxNv/kLguCA7p3jE8oL2aEBsrJWgAakBpgmgK6lpPWV+WuOmY6r2/zbAVnP+T8bQlA0nzHXSJSJW0Hq7ylaD2Q=="], "property-information": ["property-information@7.1.0", "", {}, "sha512-TwEZ+X+yCJmYfL7TPUOcvBZ4QfoT5YenQiJuX//0th53DE6w0xxLEtfK3iyryQFddXuvkIk51EEgrJQ0WJkOmQ=="], "proxy-addr": ["proxy-addr@2.0.7", "", { "dependencies": { "forwarded": "0.2.0", "ipaddr.js": "1.9.1" } }, "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg=="], - "pump": ["pump@3.0.2", "", { "dependencies": { "end-of-stream": "^1.1.0", "once": "^1.3.1" } }, "sha512-tUPXtzlGM8FE3P0ZL6DVs/3P58k9nk8/jZeQCurTJylQA8qFYzHFfhBJkuqyE0FifOsQ0uKWekiZ5g8wtr28cw=="], + "pump": ["pump@3.0.3", "", { "dependencies": { "end-of-stream": "^1.1.0", "once": "^1.3.1" } }, "sha512-todwxLMY7/heScKmntwQG8CXVkWUOdYxIvY2s0VWAAMh/nd8SoYiRaKjlr7+iCs984f2P8zvrfWcDDYVb73NfA=="], "punycode": ["punycode@1.3.2", "", {}, "sha512-RofWgt/7fL5wP1Y7fxE7/EmTLzQVnB0ycyibJ0OOHIlJqTNzglYFxVwETOcIoJqJmpDXJ9xImDv+Fq34F/d4Dw=="], @@ -1333,8 +1315,6 @@ "querystring": ["querystring@0.2.0", "", {}, "sha512-X/xY82scca2tau62i9mDyU9K+I+djTMUsvwf7xnUX5GLvVzgJybOJf4Y6o9Zx3oJK/LSXg5tTZBjwzqVPaPO2g=="], - "quick-format-unescaped": ["quick-format-unescaped@4.0.4", "", {}, "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg=="], - "radix3": ["radix3@1.1.2", "", {}, "sha512-b484I/7b8rDEdSDKckSSBA8knMpcdsXudlE/LNL639wFoHKwLbEkQFZHWEYwDC0wa0FKUcCY+GAF73Z7wxNVFA=="], "range-parser": ["range-parser@1.2.1", "", {}, "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg=="], @@ -1343,14 +1323,10 @@ "rc": ["rc@1.2.8", "", { "dependencies": { "deep-extend": "^0.6.0", "ini": "~1.3.0", "minimist": "^1.2.0", "strip-json-comments": "~2.0.1" }, "bin": { "rc": "./cli.js" } }, "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw=="], - "react": ["react@19.1.0", "", {}, "sha512-FS+XFBNvn3GTAWq26joslQgWNoFu08F4kl0J4CgdNKADkdSGXQyTCnKteIAJy96Br6YbpEU1LSzV5dYtjMkMDg=="], - "readable-stream": ["readable-stream@3.6.2", "", { "dependencies": { "inherits": "^2.0.3", "string_decoder": "^1.1.1", "util-deprecate": "^1.0.1" } }, "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA=="], "readdirp": ["readdirp@4.1.2", "", {}, "sha512-GDhwkLfywWL2s6vEjyhri+eXmfH6j1L7JE27WhqLeYzoh/A3DBaYGEj2H/HFZCn/kMfim73FXxEJTw06WtxQwg=="], - "real-require": ["real-require@0.1.0", "", {}, "sha512-r/H9MzAWtrv8aSVjPCMFpDMl5q66GqtmmRkRjpHTsp4zBAa+snZyiQNlMONiUmEJcsnaw0wCauJ2GWODr/aFkg=="], - "recma-build-jsx": ["recma-build-jsx@1.0.0", "", { "dependencies": { "@types/estree": "^1.0.0", "estree-util-build-jsx": "^3.0.0", "vfile": "^6.0.0" } }, "sha512-8GtdyqaBcDfva+GUKDr3nev3VpKAhup1+RvkMvUxURHpW7QyIvk9F5wz7Vzo06CEMSilw6uArgRqhpiUcWp8ew=="], "recma-jsx": ["recma-jsx@1.0.0", "", { "dependencies": { "acorn-jsx": "^5.0.0", "estree-util-to-js": "^2.0.0", "recma-parse": "^1.0.0", "recma-stringify": "^1.0.0", "unified": "^11.0.0" } }, "sha512-5vwkv65qWwYxg+Atz95acp8DMu1JDSqdGkA2Of1j6rCreyFUE/gp15fC8MnGEuG1W68UKjM6x6+YTWIh7hZM/Q=="], @@ -1369,7 +1345,7 @@ "rehype-autolink-headings": ["rehype-autolink-headings@7.1.0", "", { "dependencies": { "@types/hast": "^3.0.0", "@ungap/structured-clone": "^1.0.0", "hast-util-heading-rank": "^3.0.0", "hast-util-is-element": "^3.0.0", "unified": "^11.0.0", "unist-util-visit": "^5.0.0" } }, "sha512-rItO/pSdvnvsP4QRB1pmPiNHUskikqtPojZKJPPPAVx9Hj8i8TwMBhofrrAYRhYOOBZH9tgmG5lPqDLuIWPWmw=="], - "rehype-expressive-code": ["rehype-expressive-code@0.41.2", "", { "dependencies": { "expressive-code": "^0.41.2" } }, "sha512-vHYfWO9WxAw6kHHctddOt+P4266BtyT1mrOIuxJD+1ELuvuJAa5uBIhYt0OVMyOhlvf57hzWOXJkHnMhpaHyxw=="], + "rehype-expressive-code": ["rehype-expressive-code@0.41.3", "", { "dependencies": { "expressive-code": "^0.41.3" } }, "sha512-8d9Py4c/V6I/Od2VIXFAdpiO2kc0SV2qTJsRAaqSIcM9aruW4ASLNe2kOEo1inXAAkIhpFzAHTc358HKbvpNUg=="], "rehype-format": ["rehype-format@5.0.1", "", { "dependencies": { "@types/hast": "^3.0.0", "hast-util-format": "^1.0.0" } }, "sha512-zvmVru9uB0josBVpr946OR8ui7nJEdzZobwLOOqHb/OOD88W0Vk2SqLwoVOj0fM6IPCCO6TaV9CvQvJMWwukFQ=="], @@ -1395,7 +1371,7 @@ "remark-stringify": ["remark-stringify@11.0.0", "", { "dependencies": { "@types/mdast": "^4.0.0", "mdast-util-to-markdown": "^2.0.0", "unified": "^11.0.0" } }, "sha512-1OSmLd3awB/t8qdoEOMazZkNsfVTeY4fTsgzcQFdXNq8ToTN4ZGwrMnlda4K6smTFKD+GRV6O48i6Z4iKgPPpw=="], - "remeda": ["remeda@2.22.3", "", { "dependencies": { "type-fest": "^4.40.1" } }, "sha512-Ka6965m9Zu9OLsysWxVf3jdJKmp6+PKzDv7HWHinEevf0JOJ9y02YpjiC/sKxRpCqGhVyvm1U+0YIj+E6DMgKw=="], + "remeda": ["remeda@2.26.0", "", { "dependencies": { "type-fest": "^4.41.0" } }, "sha512-lmNNwtaC6Co4m0WTTNoZ/JlpjEqAjPZO0+czC9YVRQUpkbS4x8Hmh+Mn9HPfJfiXqUQ5IXXgSXSOB2pBKAytdA=="], "restructure": ["restructure@3.0.2", "", {}, "sha512-gSfoiOEA0VPE6Tukkrr7I0RBdE0s7H1eFCDBk05l1KIQT1UIKNc5JZy6jdyW6eYH3aR3g5b3PuL77rq0hvwtAw=="], @@ -1407,9 +1383,7 @@ "retext-stringify": ["retext-stringify@4.0.0", "", { "dependencies": { "@types/nlcst": "^2.0.0", "nlcst-to-string": "^4.0.0", "unified": "^11.0.0" } }, "sha512-rtfN/0o8kL1e+78+uxPTqu1Klt0yPzKuQ2BfWwwfgIUSayyzxpM1PJzkKt4V8803uB9qSy32MvI7Xep9khTpiA=="], - "rfdc": ["rfdc@1.4.1", "", {}, "sha512-q1b3N5QkRUWUl7iyylaaj3kOpIT0N2i9MqIEQXP73GVsN9cw3fdx8X63cEmWhJGi2PPCF23Ijp7ktmd39rawIA=="], - - "rollup": ["rollup@4.41.1", "", { "dependencies": { "@types/estree": "1.0.7" }, "optionalDependencies": { "@rollup/rollup-android-arm-eabi": "4.41.1", "@rollup/rollup-android-arm64": "4.41.1", "@rollup/rollup-darwin-arm64": "4.41.1", "@rollup/rollup-darwin-x64": "4.41.1", "@rollup/rollup-freebsd-arm64": "4.41.1", "@rollup/rollup-freebsd-x64": "4.41.1", "@rollup/rollup-linux-arm-gnueabihf": "4.41.1", "@rollup/rollup-linux-arm-musleabihf": "4.41.1", "@rollup/rollup-linux-arm64-gnu": "4.41.1", "@rollup/rollup-linux-arm64-musl": "4.41.1", "@rollup/rollup-linux-loongarch64-gnu": "4.41.1", "@rollup/rollup-linux-powerpc64le-gnu": "4.41.1", "@rollup/rollup-linux-riscv64-gnu": "4.41.1", "@rollup/rollup-linux-riscv64-musl": "4.41.1", "@rollup/rollup-linux-s390x-gnu": "4.41.1", "@rollup/rollup-linux-x64-gnu": "4.41.1", "@rollup/rollup-linux-x64-musl": "4.41.1", "@rollup/rollup-win32-arm64-msvc": "4.41.1", "@rollup/rollup-win32-ia32-msvc": "4.41.1", "@rollup/rollup-win32-x64-msvc": "4.41.1", "fsevents": "~2.3.2" }, "bin": { "rollup": "dist/bin/rollup" } }, "sha512-cPmwD3FnFv8rKMBc1MxWCwVQFxwf1JEmSX3iQXrRVVG15zerAIXRjMFVWnd5Q5QvgKF7Aj+5ykXFhUl+QGnyOw=="], + "rollup": ["rollup@4.45.0", "", { "dependencies": { "@types/estree": "1.0.8" }, "optionalDependencies": { "@rollup/rollup-android-arm-eabi": "4.45.0", "@rollup/rollup-android-arm64": "4.45.0", "@rollup/rollup-darwin-arm64": "4.45.0", "@rollup/rollup-darwin-x64": "4.45.0", "@rollup/rollup-freebsd-arm64": "4.45.0", "@rollup/rollup-freebsd-x64": "4.45.0", "@rollup/rollup-linux-arm-gnueabihf": "4.45.0", "@rollup/rollup-linux-arm-musleabihf": "4.45.0", "@rollup/rollup-linux-arm64-gnu": "4.45.0", "@rollup/rollup-linux-arm64-musl": "4.45.0", "@rollup/rollup-linux-loongarch64-gnu": "4.45.0", "@rollup/rollup-linux-powerpc64le-gnu": "4.45.0", "@rollup/rollup-linux-riscv64-gnu": "4.45.0", "@rollup/rollup-linux-riscv64-musl": "4.45.0", "@rollup/rollup-linux-s390x-gnu": "4.45.0", "@rollup/rollup-linux-x64-gnu": "4.45.0", "@rollup/rollup-linux-x64-musl": "4.45.0", "@rollup/rollup-win32-arm64-msvc": "4.45.0", "@rollup/rollup-win32-ia32-msvc": "4.45.0", "@rollup/rollup-win32-x64-msvc": "4.45.0", "fsevents": "~2.3.2" }, "bin": { "rollup": "dist/bin/rollup" } }, "sha512-WLjEcJRIo7i3WDDgOIJqVI2d+lAC3EwvOGy+Xfq6hs+GQuAA4Di/H72xmXkOhrIWFg2PFYSKZYfH0f4vfKXN4A=="], "router": ["router@2.2.0", "", { "dependencies": { "debug": "^4.4.0", "depd": "^2.0.0", "is-promise": "^4.0.0", "parseurl": "^1.3.3", "path-to-regexp": "^8.0.0" } }, "sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ=="], @@ -1419,8 +1393,6 @@ "safe-regex-test": ["safe-regex-test@1.1.0", "", { "dependencies": { "call-bound": "^1.0.2", "es-errors": "^1.3.0", "is-regex": "^1.2.1" } }, "sha512-x/+Cz4YrimQxQccJf5mKEbIa1NzeCRNI5Ecl/ekmlYaampdNLPalVyIcCZNNH3MvmqBugV5TMYZXv0ljslUlaw=="], - "safe-stable-stringify": ["safe-stable-stringify@2.5.0", "", {}, "sha512-b3rppTKm9T+PsVCBEOUR46GWI7fdOs00VKZ1+9c1EWDaDMvjQc6tUwuFyIprgGgTcWoVHSKrU8H31ZHA2e0RHA=="], - "safer-buffer": ["safer-buffer@2.1.2", "", {}, "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="], "sax": ["sax@1.2.1", "", {}, "sha512-8I2a3LovHTOpm7NV5yOyO8IHqgVsfK4+UuySrXU8YXkSRX7k6hCV9b3HrkKCr3nMpgj+0bmocaJJWpvp1oc7ZA=="], @@ -1441,10 +1413,14 @@ "setprototypeof": ["setprototypeof@1.2.0", "", {}, "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="], - "sha.js": ["sha.js@2.4.11", "", { "dependencies": { "inherits": "^2.0.1", "safe-buffer": "^5.0.1" }, "bin": { "sha.js": "./bin.js" } }, "sha512-QMEp5B7cftE7APOjk5Y6xgrbWu+WkLVQwk8JNjZ8nKRciZaByEW6MubieAiToS7+dwvrjGhH8jRXz3MVd0AYqQ=="], + "sha.js": ["sha.js@2.4.12", "", { "dependencies": { "inherits": "^2.0.4", "safe-buffer": "^5.2.1", "to-buffer": "^1.2.0" }, "bin": { "sha.js": "bin.js" } }, "sha512-8LzC5+bvI45BjpfXU8V5fdU2mfeKiQe1D1gIMn7XUlF3OTUrpdJpPPH4EMAnF0DsHHdSZqCdSss5qCmJKuiO3w=="], "sharp": ["sharp@0.32.5", "", { "dependencies": { "color": "^4.2.3", "detect-libc": "^2.0.2", "node-addon-api": "^6.1.0", "prebuild-install": "^7.1.1", "semver": "^7.5.4", "simple-get": "^4.0.1", "tar-fs": "^3.0.4", "tunnel-agent": "^0.6.0" } }, "sha512-0dap3iysgDkNaPOaOL4X/0akdu0ma62GcdC2NBQ+93eqpePdDdr2/LM0sFdDSMmN7yS+odyZtPsb7tx/cYBKnQ=="], + "shebang-command": ["shebang-command@2.0.0", "", { "dependencies": { "shebang-regex": "^3.0.0" } }, "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA=="], + + "shebang-regex": ["shebang-regex@3.0.0", "", {}, "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A=="], + "shiki": ["shiki@3.4.2", "", { "dependencies": { "@shikijs/core": "3.4.2", "@shikijs/engine-javascript": "3.4.2", "@shikijs/engine-oniguruma": "3.4.2", "@shikijs/langs": "3.4.2", "@shikijs/themes": "3.4.2", "@shikijs/types": "3.4.2", "@shikijs/vscode-textmate": "^10.0.2", "@types/hast": "^3.0.4" } }, "sha512-wuxzZzQG8kvZndD7nustrNFIKYJ1jJoWIPaBpVe2+KHSvtzMi4SBjOxrigs8qeqce/l3U0cwiC+VAkLKSunHQQ=="], "side-channel": ["side-channel@1.1.0", "", { "dependencies": { "es-errors": "^1.3.0", "object-inspect": "^1.13.3", "side-channel-list": "^1.0.0", "side-channel-map": "^1.0.1", "side-channel-weakmap": "^1.0.2" } }, "sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw=="], @@ -1465,22 +1441,18 @@ "sitemap": ["sitemap@8.0.0", "", { "dependencies": { "@types/node": "^17.0.5", "@types/sax": "^1.2.1", "arg": "^5.0.0", "sax": "^1.2.4" }, "bin": { "sitemap": "dist/cli.js" } }, "sha512-+AbdxhM9kJsHtruUF39bwS/B0Fytw6Fr1o4ZAIAEqA6cke2xcoO2GleBw9Zw7nRzILVEgz7zBM5GiTJjie1G9A=="], - "smol-toml": ["smol-toml@1.3.4", "", {}, "sha512-UOPtVuYkzYGee0Bd2Szz8d2G3RfMfJ2t3qVdZUAozZyAk+a0Sxa+QKix0YCwjL/A1RR0ar44nCxaoN9FxdJGwA=="], + "smol-toml": ["smol-toml@1.4.1", "", {}, "sha512-CxdwHXyYTONGHThDbq5XdwbFsuY4wlClRGejfE2NtwUtiHYsP1QtNsHb/hnj31jKYSchztJsaA8pSQoVzkfCFg=="], "solid-js": ["solid-js@1.9.7", "", { "dependencies": { "csstype": "^3.1.0", "seroval": "~1.3.0", "seroval-plugins": "~1.3.0" } }, "sha512-/saTKi8iWEM233n5OSi1YHCCuh66ZIQ7aK2hsToPe4tqGm7qAejU1SwNuTPivbWAYq7SjuHVVYxxuZQNRbICiw=="], "solid-refresh": ["solid-refresh@0.6.3", "", { "dependencies": { "@babel/generator": "^7.23.6", "@babel/helper-module-imports": "^7.22.15", "@babel/types": "^7.23.6" }, "peerDependencies": { "solid-js": "^1.3" } }, "sha512-F3aPsX6hVw9ttm5LYlth8Q15x6MlI/J3Dn+o3EQyRTtTxidepSTwAYdozt01/YA+7ObcciagGEyXIopGZzQtbA=="], - "sonic-boom": ["sonic-boom@2.8.0", "", { "dependencies": { "atomic-sleep": "^1.0.0" } }, "sha512-kuonw1YOYYNOve5iHdSahXPOK49GqwA+LZhI6Wz/l0rP57iKyXXIHaRagOBHAPmGwJC6od2Z9zgvZ5loSgMlVg=="], - "source-map": ["source-map@0.7.4", "", {}, "sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA=="], "source-map-js": ["source-map-js@1.2.1", "", {}, "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA=="], "space-separated-tokens": ["space-separated-tokens@2.0.2", "", {}, "sha512-PEGlAwrG8yXGXRjW32fGbg66JAlOAwbObuqVoJpv/mRgoWDQfgH1wDPvtzWyUSNAXBGSk8h755YDbbcEy3SH2Q=="], - "split2": ["split2@3.2.2", "", { "dependencies": { "readable-stream": "^3.0.0" } }, "sha512-9NThjpgZnifTkJpzTZ7Eue85S49QwpNhZTq6GRJwObb6jnLFNGB7Qm73V5HewTROPyxD0C29xqmaI68bQtV+hg=="], - "sst": ["sst@3.17.8", "", { "dependencies": { "aws-sdk": "2.1692.0", "aws4fetch": "1.0.18", "jose": "5.2.3", "opencontrol": "0.0.6", "openid-client": "5.6.4" }, "optionalDependencies": { "sst-darwin-arm64": "3.17.8", "sst-darwin-x64": "3.17.8", "sst-linux-arm64": "3.17.8", "sst-linux-x64": "3.17.8", "sst-linux-x86": "3.17.8", "sst-win32-arm64": "3.17.8", "sst-win32-x64": "3.17.8", "sst-win32-x86": "3.17.8" }, "bin": { "sst": "bin/sst.mjs" } }, "sha512-P/a9/ZsjtQRrTBerBMO1ODaVa5HVTmNLrQNJiYvu2Bgd0ov+vefQeHv6oima8HLlPwpDIPS2gxJk8BZrTZMfCA=="], "sst-darwin-arm64": ["sst-darwin-arm64@3.17.8", "", { "os": "darwin", "cpu": "arm64" }, "sha512-50P6YRMnZVItZUfB0+NzqMww2mmm4vB3zhTVtWUtGoXeiw78g1AEnVlmS28gYXPHM1P987jTvR7EON9u9ig/Dg=="], @@ -1499,17 +1471,13 @@ "sst-win32-x86": ["sst-win32-x86@3.17.8", "", { "os": "win32", "cpu": "none" }, "sha512-oVmFa/PoElQmfnGJlB0w6rPXiYuldiagO6AbrLMT/6oAnWerLQ8Uhv9tJWfMh3xtPLImQLTjxDo1v0AIzEv9QA=="], - "stacktracey": ["stacktracey@2.1.8", "", { "dependencies": { "as-table": "^1.0.36", "get-source": "^2.0.12" } }, "sha512-Kpij9riA+UNg7TnphqjH7/CzctQ/owJGNbFkfEeve4Z4uxT5+JapVLFXcsurIfN34gnTWZNJ/f7NMG0E8JDzTw=="], - - "statuses": ["statuses@2.0.1", "", {}, "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ=="], + "statuses": ["statuses@2.0.2", "", {}, "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw=="], "stoppable": ["stoppable@1.1.0", "", {}, "sha512-KXDYZ9dszj6bzvnEMRYvxgeTHU74QBFL54XKtP3nyMuJ81CFYtABZ3bAzL2EdFUaEwJOBOgENyFj3R7oTzDyyw=="], "stream-replace-string": ["stream-replace-string@2.0.0", "", {}, "sha512-TlnjJ1C0QrmxRNrON00JvaFFlNh5TTG00APw23j74ET7gkQpTASi6/L2fuiav8pzK715HXtUeClpBTw2NPSn6w=="], - "stream-shift": ["stream-shift@1.0.3", "", {}, "sha512-76ORR0DO1o1hlKwTbi/DM3EXWGf3ZJYO8cXX5RJwnul2DEg2oyoZyjLNoQM8WsvZiFKCRfC1O0J7iCvie3RZmQ=="], - - "streamx": ["streamx@2.22.0", "", { "dependencies": { "fast-fifo": "^1.3.2", "text-decoder": "^1.1.0" }, "optionalDependencies": { "bare-events": "^2.2.0" } }, "sha512-sLh1evHOzBy/iWRiR6d1zRcLao4gGZr3C1kzNz4fopCOKJb6xD9ub8Mpi9Mr1R6id5o43S+d93fI48UC5uM9aw=="], + "streamx": ["streamx@2.22.1", "", { "dependencies": { "fast-fifo": "^1.3.2", "text-decoder": "^1.1.0" }, "optionalDependencies": { "bare-events": "^2.2.0" } }, "sha512-znKXEBxfatz2GBNK02kRnCXjV+AA4kjZIUxeWSr3UGirZMJfTE9uiwKHobnbgxWyL/JWro8tTq+vOqAK1/qbSA=="], "string-width": ["string-width@7.2.0", "", { "dependencies": { "emoji-regex": "^10.3.0", "get-east-asian-width": "^1.0.0", "strip-ansi": "^7.1.0" } }, "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ=="], @@ -1519,39 +1487,33 @@ "strip-ansi": ["strip-ansi@7.1.0", "", { "dependencies": { "ansi-regex": "^6.0.1" } }, "sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ=="], - "strip-json-comments": ["strip-json-comments@3.1.1", "", {}, "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig=="], + "strip-json-comments": ["strip-json-comments@2.0.1", "", {}, "sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ=="], - "strtok3": ["strtok3@10.2.2", "", { "dependencies": { "@tokenizer/token": "^0.3.0", "peek-readable": "^7.0.0" } }, "sha512-Xt18+h4s7Z8xyZ0tmBoRmzxcop97R4BAh+dXouUDCYn+Em+1P3qpkUfI5ueWLT8ynC5hZ+q4iPEmGG1urvQGBg=="], + "style-to-js": ["style-to-js@1.1.17", "", { "dependencies": { "style-to-object": "1.0.9" } }, "sha512-xQcBGDxJb6jjFCTzvQtfiPn6YvvP2O8U1MDIPNfJQlWMYfktPy+iGsHE7cssjs7y84d9fQaK4UF3RIJaAHSoYA=="], - "style-to-js": ["style-to-js@1.1.16", "", { "dependencies": { "style-to-object": "1.0.8" } }, "sha512-/Q6ld50hKYPH3d/r6nr117TZkHR0w0kGGIVfpG9N6D8NymRPM9RqCUv4pRpJ62E5DqOYx2AFpbZMyCPnjQCnOw=="], + "style-to-object": ["style-to-object@1.0.9", "", { "dependencies": { "inline-style-parser": "0.2.4" } }, "sha512-G4qppLgKu/k6FwRpHiGiKPaPTFcG3g4wNVX/Qsfu+RqQM30E7Tyu/TEgxcL9PNLF5pdRLwQdE3YKKf+KF2Dzlw=="], - "style-to-object": ["style-to-object@1.0.8", "", { "dependencies": { "inline-style-parser": "0.2.4" } }, "sha512-xT47I/Eo0rwJmaXC4oilDGDWLohVhR6o/xAQcPQN8q6QBuZVL8qMYL85kLmST5cPjAorwvqIA4qXTRQoYHaL6g=="], + "supports-color": ["supports-color@10.0.0", "", {}, "sha512-HRVVSbCCMbj7/kdWF9Q+bbckjBHLtHMEoJWlkmYzzdwhYMkjkOwubLM6t7NbWKjgKamGDrWL1++KrjUO1t9oAQ=="], - "supports-color": ["supports-color@7.2.0", "", { "dependencies": { "has-flag": "^4.0.0" } }, "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw=="], - - "swr": ["swr@2.3.3", "", { "dependencies": { "dequal": "^2.0.3", "use-sync-external-store": "^1.4.0" }, "peerDependencies": { "react": "^16.11.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" } }, "sha512-dshNvs3ExOqtZ6kJBaAsabhPdHyeY4P2cKwRCniDVifBMoG/SVI7tfLWqPXriVspf2Rg4tPzXJTnwaihIeFw2A=="], - - "tar-fs": ["tar-fs@3.0.9", "", { "dependencies": { "pump": "^3.0.0", "tar-stream": "^3.1.5" }, "optionalDependencies": { "bare-fs": "^4.0.1", "bare-path": "^3.0.0" } }, "sha512-XF4w9Xp+ZQgifKakjZYmFdkLoSWd34VGKcsTCwlNWM7QG3ZbaxnTsaBwnjFZqHRf/rROxaR8rXnbtwdvaDI+lA=="], + "tar-fs": ["tar-fs@3.1.0", "", { "dependencies": { "pump": "^3.0.0", "tar-stream": "^3.1.5" }, "optionalDependencies": { "bare-fs": "^4.0.1", "bare-path": "^3.0.0" } }, "sha512-5Mty5y/sOF1YWj1J6GiBodjlDc05CUR8PKXrsnFAiSG0xA+GHeWLovaZPYUDXkH/1iKRf2+M5+OrRgzC7O9b7w=="], "tar-stream": ["tar-stream@3.1.7", "", { "dependencies": { "b4a": "^1.6.4", "fast-fifo": "^1.2.0", "streamx": "^2.15.0" } }, "sha512-qJj60CXt7IU1Ffyc3NJMjh6EkuCFej46zUqJ4J7pqYlThyd9bO0XBTmcOIhSzZJVWfsLks0+nle/j538YAW9RQ=="], "text-decoder": ["text-decoder@1.2.3", "", { "dependencies": { "b4a": "^1.6.4" } }, "sha512-3/o9z3X0X0fTupwsYvR03pJ/DjWuqqrfwBgTQzdWDiQSm9KitAyz/9WqsT2JQW7KV2m+bC2ol/zqpW37NHxLaA=="], - "thread-stream": ["thread-stream@0.15.2", "", { "dependencies": { "real-require": "^0.1.0" } }, "sha512-UkEhKIg2pD+fjkHQKyJO3yoIvAP3N6RlNFt2dUhcS1FGvCD1cQa1M/PGknCLFIyZdtJOWQjejp7bdNqmN7zwdA=="], - - "throttleit": ["throttleit@2.1.0", "", {}, "sha512-nt6AMGKW1p/70DF/hGBdJB57B8Tspmbp5gfJ8ilhLnt7kkr2ye7hzD6NVG8GGErk2HWF34igrL2CXmNIkzKqKw=="], - "tiny-inflate": ["tiny-inflate@1.0.3", "", {}, "sha512-pkY1fj1cKHb2seWDy0B16HeWyczlJA9/WW3u3c4z/NiWDsO3DOU5D7nhTLE9CF0yXv/QZFY7sEJmj24dK+Rrqw=="], "tinyexec": ["tinyexec@0.3.2", "", {}, "sha512-KQQR9yN7R5+OSwaK0XQoj22pwHoTlgYqmUscPYoknOoWCWfj/5/ABTMRi69FrKU5ffPVh5QcFikpWJI/P1ocHA=="], "tinyglobby": ["tinyglobby@0.2.14", "", { "dependencies": { "fdir": "^6.4.4", "picomatch": "^4.0.2" } }, "sha512-tX5e7OM1HnYr2+a2C/4V0htOcSQcoSTH9KgJnVvNm5zm/cyEWKJ7j7YutsH9CxMdtOkkLFy2AHrMci9IM8IPZQ=="], + "to-buffer": ["to-buffer@1.2.1", "", { "dependencies": { "isarray": "^2.0.5", "safe-buffer": "^5.2.1", "typed-array-buffer": "^1.0.3" } }, "sha512-tB82LpAIWjhLYbqjx3X4zEeHN6M8CiuOEy2JY8SEQVdYRe3CCHOFaqrBW1doLDrfpWhplcW7BL+bO3/6S3pcDQ=="], + + "toad-cache": ["toad-cache@3.7.0", "", {}, "sha512-/m8M+2BJUpoJdgAHoG+baCwBT+tf2VraSfkBgl0Y00qIWt41DJ8R5B8nsEw0I58YwF5IZH6z24/2TobDKnqSWw=="], + "toidentifier": ["toidentifier@1.0.1", "", {}, "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA=="], - "token-types": ["token-types@6.0.0", "", { "dependencies": { "@tokenizer/token": "^0.3.0", "ieee754": "^1.2.1" } }, "sha512-lbDrTLVsHhOMljPscd0yitpozq7Ga2M5Cvez5AjGg8GASBjtt6iERCAJ93yommPmz62fb45oFIXHEZ3u9bfJEA=="], - - "toolbeam-docs-theme": ["toolbeam-docs-theme@0.4.1", "", { "peerDependencies": { "@astrojs/starlight": "^0.34.3", "astro": "^5.7.13" } }, "sha512-lTI4dHZaVNQky29m7sb36Oy4tWPwxsCuFxFjF8hgGW0vpV+S6qPvI9SwsJFvdE/OHO5DoI7VMbryV1pxZHkkHQ=="], + "toolbeam-docs-theme": ["toolbeam-docs-theme@0.4.3", "", { "peerDependencies": { "@astrojs/starlight": "^0.34.3", "astro": "^5.7.13" } }, "sha512-3um/NsSq4xFeKbKrNGPHIzfTixwnEVvroqA8Q+lecnYHHJ5TtiYTggHDqewOW+I67t0J1IVBwVKUPjxiQfIcog=="], "tr46": ["tr46@0.0.3", "", {}, "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw=="], @@ -1559,11 +1521,9 @@ "trough": ["trough@2.2.0", "", {}, "sha512-tmMpK00BjZiUyVyvrBK7knerNgmgvcV/KLVyuma/SC+TQN167GrMRciANTz09+k3zW8L8t60jWO1GpfkZdjTaw=="], - "ts-lsp-client": ["ts-lsp-client@1.0.3", "", { "dependencies": { "json-rpc-2.0": "^1.7.0", "pino": "^7.0.5", "pino-pretty": "^5.1.3", "tslib": "~2.6.2" } }, "sha512-0ItrsqvNUM9KNFGbeT1N8jSi9gvasGOvxJUXjGf4P2TX0w250AUWLeRStaSrQbYcFDshDtE5d4BshUmYwodDgw=="], - "tsconfck": ["tsconfck@3.1.6", "", { "peerDependencies": { "typescript": "^5.0.0" }, "optionalPeers": ["typescript"], "bin": { "tsconfck": "bin/tsconfck.js" } }, "sha512-ks6Vjr/jEw0P1gmOVwutM3B7fWxoWBL2KRDb1JfqGVawBmO5UsvmWOQFGHBPl5yxYz4eERr19E6L7NMv+Fej4w=="], - "tslib": ["tslib@2.6.3", "", {}, "sha512-xNvxJEOUiWPGhUuUdQgAJPKOOJfGnIyKySOc09XkKsgdUV/3E2zvwZYdejjmRgPCgcym1juLH3226yA7sEFJKQ=="], + "tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], "tunnel-agent": ["tunnel-agent@0.6.0", "", { "dependencies": { "safe-buffer": "^5.0.1" } }, "sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w=="], @@ -1573,12 +1533,12 @@ "type-is": ["type-is@2.0.1", "", { "dependencies": { "content-type": "^1.0.5", "media-typer": "^1.1.0", "mime-types": "^3.0.0" } }, "sha512-OZs6gsjF4vMp32qrCbiVSkrFmXtG/AZhY3t0iAMrMBiAZyV9oALtXO8hsrHbMXF9x6L3grlFuwW2oAz7cav+Gw=="], + "typed-array-buffer": ["typed-array-buffer@1.0.3", "", { "dependencies": { "call-bound": "^1.0.3", "es-errors": "^1.3.0", "is-typed-array": "^1.1.14" } }, "sha512-nAYYwfY3qnzX30IkA6AQZjVbtK6duGontcQm1WSG1MD94YLqK0515GNApXkoxKOWMusVssAHWLh9SeaoefYFGw=="], + "typescript": ["typescript@5.8.2", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-aJn6wq13/afZp/jT9QZmwEjDqqvSGp1VT5GVg+f/t6/oVyrgXM6BY1h9BRh/O5p3PlUPAe+WuiEZOmb/49RqoQ=="], "ufo": ["ufo@1.6.1", "", {}, "sha512-9a4/uxlTWJ4+a5i0ooc1rU7C7YOw3wT+UGqdeNNHWnOF9qcMBgLRS+4IYUqbczewFx4mLEig6gawh7X6mFlEkA=="], - "uint8array-extras": ["uint8array-extras@1.4.0", "", {}, "sha512-ZPtzy0hu4cZjv3z5NW9gfKnNLjoz4y6uv4HlelAjDK7sY/xOkKZv9xK/WQpcsBB3jEybChz9DPC2U/+cusjJVQ=="], - "ultrahtml": ["ultrahtml@1.6.0", "", {}, "sha512-R9fBn90VTJrqqLDwyMph+HGne8eqY1iPfYhPzZrvKpIfwkWZbcYlfpsb8B9dTvBfpy1/hqAD7Wi8EKfP9e8zdw=="], "uncrypto": ["uncrypto@0.1.3", "", {}, "sha512-Ql87qFHB3s/De2ClA9e0gsnS6zXG27SkTiSJwjCc9MebbfapQfuPzumMIUMi38ezPZVNFcHI9sUIepeQfw8J8Q=="], @@ -1595,7 +1555,7 @@ "unified": ["unified@11.0.5", "", { "dependencies": { "@types/unist": "^3.0.0", "bail": "^2.0.0", "devlop": "^1.0.0", "extend": "^3.0.0", "is-plain-obj": "^4.0.0", "trough": "^2.0.0", "vfile": "^6.0.0" } }, "sha512-xKvGhPWw3k84Qjh8bI3ZeJjqnyadK+GEFtazSfZv/rKeTkTjOJho6mFqh2SM96iIcZokxiOpg78GazTSg8+KHA=="], - "unifont": ["unifont@0.5.0", "", { "dependencies": { "css-tree": "^3.0.0", "ohash": "^2.0.0" } }, "sha512-4DueXMP5Hy4n607sh+vJ+rajoLu778aU3GzqeTCqsD/EaUcvqZT9wPC8kgK6Vjh22ZskrxyRCR71FwNOaYn6jA=="], + "unifont": ["unifont@0.5.2", "", { "dependencies": { "css-tree": "^3.0.0", "ofetch": "^1.4.1", "ohash": "^2.0.0" } }, "sha512-LzR4WUqzH9ILFvjLAUU7dK3Lnou/qd5kD+IakBtBK4S15/+x2y9VX+DcWQv6s551R6W+vzwgVS6tFg3XggGBgg=="], "unist-util-find-after": ["unist-util-find-after@5.0.0", "", { "dependencies": { "@types/unist": "^3.0.0", "unist-util-is": "^6.0.0" } }, "sha512-amQa0Ep2m6hE2g72AugUItjbuM8X8cGQnFoHk0pGfrFeT9GZhzN5SW8nRsiGKK7Aif4CrACPENkA6P/Lw6fHGQ=="], @@ -1617,15 +1577,19 @@ "unist-util-visit-parents": ["unist-util-visit-parents@6.0.1", "", { "dependencies": { "@types/unist": "^3.0.0", "unist-util-is": "^6.0.0" } }, "sha512-L/PqWzfTP9lzzEa6CKs0k2nARxTdZduw3zyh8d2NVBnsyvHjSX4TWse388YrrQKbvI8w20fGjGlhgT96WwKykw=="], + "universal-github-app-jwt": ["universal-github-app-jwt@2.2.2", "", {}, "sha512-dcmbeSrOdTnsjGjUfAlqNDJrhxXizjAz94ija9Qw8YkZ1uu0d+GoZzyH+Jb9tIIqvGsadUfwg+22k5aDqqwzbw=="], + + "universal-user-agent": ["universal-user-agent@7.0.3", "", {}, "sha512-TmnEAEAsBJVZM/AADELsK76llnwcf9vMKuPz8JflO1frO8Lchitr0fNaN9d+Ap0BjKtqWqd/J17qeDnXh8CL2A=="], + "unpipe": ["unpipe@1.0.0", "", {}, "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ=="], - "unstorage": ["unstorage@1.16.0", "", { "dependencies": { "anymatch": "^3.1.3", "chokidar": "^4.0.3", "destr": "^2.0.5", "h3": "^1.15.2", "lru-cache": "^10.4.3", "node-fetch-native": "^1.6.6", "ofetch": "^1.4.1", "ufo": "^1.6.1" }, "peerDependencies": { "@azure/app-configuration": "^1.8.0", "@azure/cosmos": "^4.2.0", "@azure/data-tables": "^13.3.0", "@azure/identity": "^4.6.0", "@azure/keyvault-secrets": "^4.9.0", "@azure/storage-blob": "^12.26.0", "@capacitor/preferences": "^6.0.3 || ^7.0.0", "@deno/kv": ">=0.9.0", "@netlify/blobs": "^6.5.0 || ^7.0.0 || ^8.1.0", "@planetscale/database": "^1.19.0", "@upstash/redis": "^1.34.3", "@vercel/blob": ">=0.27.1", "@vercel/kv": "^1.0.1", "aws4fetch": "^1.0.20", "db0": ">=0.2.1", "idb-keyval": "^6.2.1", "ioredis": "^5.4.2", "uploadthing": "^7.4.4" }, "optionalPeers": ["@azure/app-configuration", "@azure/cosmos", "@azure/data-tables", "@azure/identity", "@azure/keyvault-secrets", "@azure/storage-blob", "@capacitor/preferences", "@deno/kv", "@netlify/blobs", "@planetscale/database", "@upstash/redis", "@vercel/blob", "@vercel/kv", "aws4fetch", "db0", "idb-keyval", "ioredis", "uploadthing"] }, "sha512-WQ37/H5A7LcRPWfYOrDa1Ys02xAbpPJq6q5GkO88FBXVSQzHd7+BjEwfRqyaSWCv9MbsJy058GWjjPjcJ16GGA=="], + "unstorage": ["unstorage@1.16.1", "", { "dependencies": { "anymatch": "^3.1.3", "chokidar": "^4.0.3", "destr": "^2.0.5", "h3": "^1.15.3", "lru-cache": "^10.4.3", "node-fetch-native": "^1.6.6", "ofetch": "^1.4.1", "ufo": "^1.6.1" }, "peerDependencies": { "@azure/app-configuration": "^1.8.0", "@azure/cosmos": "^4.2.0", "@azure/data-tables": "^13.3.0", "@azure/identity": "^4.6.0", "@azure/keyvault-secrets": "^4.9.0", "@azure/storage-blob": "^12.26.0", "@capacitor/preferences": "^6.0.3 || ^7.0.0", "@deno/kv": ">=0.9.0", "@netlify/blobs": "^6.5.0 || ^7.0.0 || ^8.1.0 || ^9.0.0 || ^10.0.0", "@planetscale/database": "^1.19.0", "@upstash/redis": "^1.34.3", "@vercel/blob": ">=0.27.1", "@vercel/kv": "^1.0.1", "aws4fetch": "^1.0.20", "db0": ">=0.2.1", "idb-keyval": "^6.2.1", "ioredis": "^5.4.2", "uploadthing": "^7.4.4" }, "optionalPeers": ["@azure/app-configuration", "@azure/cosmos", "@azure/data-tables", "@azure/identity", "@azure/keyvault-secrets", "@azure/storage-blob", "@capacitor/preferences", "@deno/kv", "@netlify/blobs", "@planetscale/database", "@upstash/redis", "@vercel/blob", "@vercel/kv", "aws4fetch", "db0", "idb-keyval", "ioredis", "uploadthing"] }, "sha512-gdpZ3guLDhz+zWIlYP1UwQ259tG5T5vYRzDaHMkQ1bBY1SQPutvZnrRjTFaWUUpseErJIgAZS51h6NOcZVZiqQ=="], "update-browserslist-db": ["update-browserslist-db@1.1.3", "", { "dependencies": { "escalade": "^3.2.0", "picocolors": "^1.1.1" }, "peerDependencies": { "browserslist": ">= 4.21.0" }, "bin": { "update-browserslist-db": "cli.js" } }, "sha512-UxhIZQ+QInVdunkDAaiazvvT/+fXL5Osr0JZlJulepYu6Jd7qJtDZjlur0emRlT71EN3ScPoE7gvsuIKKNavKw=="], - "url": ["url@0.10.3", "", { "dependencies": { "punycode": "1.3.2", "querystring": "0.2.0" } }, "sha512-hzSUW2q06EqL1gKM/a+obYHLIO6ct2hwPuviqTTOcfFVc61UbfJ2Q32+uGL/HCPxKqrdGB5QUwIe7UqlDgwsOQ=="], + "uri-js": ["uri-js@4.4.1", "", { "dependencies": { "punycode": "^2.1.0" } }, "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg=="], - "use-sync-external-store": ["use-sync-external-store@1.5.0", "", { "peerDependencies": { "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" } }, "sha512-Rb46I4cGGVBmjamjphe8L/UnvJD+uPPtTkNvX5mZgqdbavhI4EbgIWJiIHXJ8bc/i9EQGPRh4DwEURJ552Do0A=="], + "url": ["url@0.10.3", "", { "dependencies": { "punycode": "1.3.2", "querystring": "0.2.0" } }, "sha512-hzSUW2q06EqL1gKM/a+obYHLIO6ct2hwPuviqTTOcfFVc61UbfJ2Q32+uGL/HCPxKqrdGB5QUwIe7UqlDgwsOQ=="], "util": ["util@0.12.5", "", { "dependencies": { "inherits": "^2.0.3", "is-arguments": "^1.0.4", "is-generator-function": "^1.0.7", "is-typed-array": "^1.1.3", "which-typed-array": "^1.1.2" } }, "sha512-kZf/K6hEIrWHI6XqOFUiiMa+79wE/D8Q+NCNAWclkyg3b4d2k7s0QGepNjiABc+aR3N1PAyHL7p6UcLY6LmrnA=="], @@ -1633,7 +1597,7 @@ "uuid": ["uuid@8.0.0", "", { "bin": { "uuid": "dist/bin/uuid" } }, "sha512-jOXGuXZAWdsTH7eZLtyXMqUb9EcWMGZNbL9YcGBJl4MH4nrxHmZJhEHvyLFrkxo+28uLb/NYRcStH48fnD0Vzw=="], - "validate-html-nesting": ["validate-html-nesting@1.2.2", "", {}, "sha512-hGdgQozCsQJMyfK5urgFcWEqsSSrK63Awe0t/IMR0bZ0QMtnuaiHzThW81guu3qx9abLi99NEuiaN6P9gVYsNg=="], + "validate-html-nesting": ["validate-html-nesting@1.2.3", "", {}, "sha512-kdkWdCl6eCeLlRShJKbjVOU2kFKxMF8Ghu50n+crEoyx+VKm3FxAxF9z4DCy6+bbTOqNW0+jcIYRnjoIRzigRw=="], "vary": ["vary@1.1.2", "", {}, "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg=="], @@ -1645,17 +1609,13 @@ "vite": ["vite@6.3.5", "", { "dependencies": { "esbuild": "^0.25.0", "fdir": "^6.4.4", "picomatch": "^4.0.2", "postcss": "^8.5.3", "rollup": "^4.34.9", "tinyglobby": "^0.2.13" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "peerDependencies": { "@types/node": "^18.0.0 || ^20.0.0 || >=22.0.0", "jiti": ">=1.21.0", "less": "*", "lightningcss": "^1.21.0", "sass": "*", "sass-embedded": "*", "stylus": "*", "sugarss": "*", "terser": "^5.16.0", "tsx": "^4.8.1", "yaml": "^2.4.2" }, "optionalPeers": ["@types/node", "jiti", "less", "lightningcss", "sass", "sass-embedded", "stylus", "sugarss", "terser", "tsx", "yaml"], "bin": { "vite": "bin/vite.js" } }, "sha512-cZn6NDFE7wdTpINgs++ZJ4N49W2vRp8LCKrn3Ob1kYNtOo21vfDoaV5GzBfLU4MovSAB8uNRm4jgzVQZ+mBzPQ=="], - "vite-plugin-solid": ["vite-plugin-solid@2.11.6", "", { "dependencies": { "@babel/core": "^7.23.3", "@types/babel__core": "^7.20.4", "babel-preset-solid": "^1.8.4", "merge-anything": "^5.1.7", "solid-refresh": "^0.6.3", "vitefu": "^1.0.4" }, "peerDependencies": { "@testing-library/jest-dom": "^5.16.6 || ^5.17.0 || ^6.*", "solid-js": "^1.7.2", "vite": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0" }, "optionalPeers": ["@testing-library/jest-dom"] }, "sha512-Sl5CTqJTGyEeOsmdH6BOgalIZlwH3t4/y0RQuFLMGnvWMBvxb4+lq7x3BSiAw6etf0QexfNJW7HSOO/Qf7pigg=="], + "vite-plugin-solid": ["vite-plugin-solid@2.11.7", "", { "dependencies": { "@babel/core": "^7.23.3", "@types/babel__core": "^7.20.4", "babel-preset-solid": "^1.8.4", "merge-anything": "^5.1.7", "solid-refresh": "^0.6.3", "vitefu": "^1.0.4" }, "peerDependencies": { "@testing-library/jest-dom": "^5.16.6 || ^5.17.0 || ^6.*", "solid-js": "^1.7.2", "vite": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0" }, "optionalPeers": ["@testing-library/jest-dom"] }, "sha512-5TgK1RnE449g0Ryxb9BXqem89RSy7fE8XGVCo+Gw84IHgPuPVP7nYNP6WBVAaY/0xw+OqfdQee+kusL0y3XYNg=="], - "vitefu": ["vitefu@1.0.6", "", { "peerDependencies": { "vite": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0" }, "optionalPeers": ["vite"] }, "sha512-+Rex1GlappUyNN6UfwbVZne/9cYC4+R2XDk9xkNXBKMw6HQagdX9PgZ8V2v1WUSK1wfBLp7qbI1+XSNIlB1xmA=="], + "vitefu": ["vitefu@1.1.1", "", { "peerDependencies": { "vite": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0-beta.0" }, "optionalPeers": ["vite"] }, "sha512-B/Fegf3i8zh0yFbpzZ21amWzHmuNlLlmJT6n7bu5e+pCHUKQIfXSYokrqOBGEMMe9UG2sostKQF9mml/vYaWJQ=="], "vscode-jsonrpc": ["vscode-jsonrpc@8.2.1", "", {}, "sha512-kdjOSJ2lLIn7r1rtrMbbNCHjyMPfRnowdKjBQ+mGq6NAW5QY2bEZC/khaC5OR8svbbjvLEaIXkOq45e2X9BIbQ=="], - "vscode-languageclient": ["vscode-languageclient@8.1.0", "", { "dependencies": { "minimatch": "^5.1.0", "semver": "^7.3.7", "vscode-languageserver-protocol": "3.17.3" } }, "sha512-GL4QdbYUF/XxQlAsvYWZRV3V34kOkpRlvV60/72ghHfsYFnS/v2MANZ9P6sHmxFcZKOse8O+L9G7Czg0NUWing=="], - - "vscode-languageserver-protocol": ["vscode-languageserver-protocol@3.17.3", "", { "dependencies": { "vscode-jsonrpc": "8.1.0", "vscode-languageserver-types": "3.17.3" } }, "sha512-924/h0AqsMtA5yK22GgMtCYiMdCOtWTSGgUOkgEDX+wk2b0x4sAfLiO4NxBxqbiVtz7K7/1/RgVrVI0NClZwqA=="], - - "vscode-languageserver-types": ["vscode-languageserver-types@3.17.3", "", {}, "sha512-SYU4z1dL0PyIMd4Vj8YOqFvHu7Hz/enbWtpfnVbJHU4Nd1YNYx8u0ennumc6h48GQNeOLxmwySmnADouT/AuZA=="], + "vscode-languageserver-types": ["vscode-languageserver-types@3.17.5", "", {}, "sha512-Ld1VelNuX9pdF39h2Hgaeb5hEZM2Z3jUrrMgWQAu82jMtZp7p3vJT3BzToKtZI7NgQssZje5o0zryOrhQvzQAg=="], "web-namespaces": ["web-namespaces@2.0.1", "", {}, "sha512-bKr1DkiNa2krS7qxNtdrtHAmzuYGFQLiQ13TsorsdT6ULTkPLKuu5+GsFpDlg6JFjUTwX2DyhMPG2be8uPrqsQ=="], @@ -1663,15 +1623,17 @@ "whatwg-url": ["whatwg-url@5.0.0", "", { "dependencies": { "tr46": "~0.0.3", "webidl-conversions": "^3.0.0" } }, "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw=="], + "which": ["which@2.0.2", "", { "dependencies": { "isexe": "^2.0.0" }, "bin": { "node-which": "./bin/node-which" } }, "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA=="], + "which-pm-runs": ["which-pm-runs@1.1.0", "", {}, "sha512-n1brCuqClxfFfq/Rb0ICg9giSZqCS+pLtccdag6C2HyufBrh3fBOiy9nb6ggRMvWOVH5GrdJskj5iGTZNxd7SA=="], "which-typed-array": ["which-typed-array@1.1.19", "", { "dependencies": { "available-typed-arrays": "^1.0.7", "call-bind": "^1.0.8", "call-bound": "^1.0.4", "for-each": "^0.3.5", "get-proto": "^1.0.1", "gopd": "^1.2.0", "has-tostringtag": "^1.0.2" } }, "sha512-rEvr90Bck4WZt9HHFC4DJMsjvu7x+r6bImz0/BrbWb7A2djJ8hnZMrWnHo9F8ssv0OMErasDhftrfROTyqSDrw=="], "widest-line": ["widest-line@5.0.0", "", { "dependencies": { "string-width": "^7.0.0" } }, "sha512-c9bZp7b5YtRj2wOe6dlj32MK+Bx/M/d+9VB2SHM1OtsUHR0aV0tdP6DWh/iMt0kWi1t5g1Iudu6hQRNd1A4PVA=="], - "workerd": ["workerd@1.20250525.0", "", { "optionalDependencies": { "@cloudflare/workerd-darwin-64": "1.20250525.0", "@cloudflare/workerd-darwin-arm64": "1.20250525.0", "@cloudflare/workerd-linux-64": "1.20250525.0", "@cloudflare/workerd-linux-arm64": "1.20250525.0", "@cloudflare/workerd-windows-64": "1.20250525.0" }, "bin": { "workerd": "bin/workerd" } }, "sha512-SXJgLREy/Aqw2J71Oah0Pbu+SShbqbTExjVQyRBTM1r7MG7fS5NUlknhnt6sikjA/t4cO09Bi8OJqHdTkrcnYQ=="], + "workerd": ["workerd@1.20250709.0", "", { "optionalDependencies": { "@cloudflare/workerd-darwin-64": "1.20250709.0", "@cloudflare/workerd-darwin-arm64": "1.20250709.0", "@cloudflare/workerd-linux-64": "1.20250709.0", "@cloudflare/workerd-linux-arm64": "1.20250709.0", "@cloudflare/workerd-windows-64": "1.20250709.0" }, "bin": { "workerd": "bin/workerd" } }, "sha512-BqLPpmvRN+TYUSG61OkWamsGdEuMwgvabP8m0QOHIfofnrD2YVyWqE1kXJ0GH5EsVEuWamE5sR8XpTfsGBmIpg=="], - "wrangler": ["wrangler@4.19.1", "", { "dependencies": { "@cloudflare/kv-asset-handler": "0.4.0", "@cloudflare/unenv-preset": "2.3.2", "blake3-wasm": "2.1.5", "esbuild": "0.25.4", "miniflare": "4.20250525.1", "path-to-regexp": "6.3.0", "unenv": "2.0.0-rc.17", "workerd": "1.20250525.0" }, "optionalDependencies": { "fsevents": "~2.3.2" }, "peerDependencies": { "@cloudflare/workers-types": "^4.20250525.0" }, "optionalPeers": ["@cloudflare/workers-types"], "bin": { "wrangler": "bin/wrangler.js", "wrangler2": "bin/wrangler.js" } }, "sha512-b+ed2SJKauHgndl4Im1wHE+FeSSlrdlEZNuvpc8q/94k4EmRxRkXnwBAsVWuicBxG3HStFLQPGGlvL8wGKTtHw=="], + "wrangler": ["wrangler@4.24.3", "", { "dependencies": { "@cloudflare/kv-asset-handler": "0.4.0", "@cloudflare/unenv-preset": "2.3.3", "blake3-wasm": "2.1.5", "esbuild": "0.25.4", "miniflare": "4.20250709.0", "path-to-regexp": "6.3.0", "unenv": "2.0.0-rc.17", "workerd": "1.20250709.0" }, "optionalDependencies": { "fsevents": "~2.3.2" }, "peerDependencies": { "@cloudflare/workers-types": "^4.20250709.0" }, "optionalPeers": ["@cloudflare/workers-types"], "bin": { "wrangler": "bin/wrangler.js", "wrangler2": "bin/wrangler.js" } }, "sha512-stB1Wfs5NKlspsAzz8SBujBKsDqT5lpCyrL+vSUMy3uueEtI1A5qyORbKoJhIguEbwHfWS39mBsxzm6Vm1J2cg=="], "wrap-ansi": ["wrap-ansi@9.0.0", "", { "dependencies": { "ansi-styles": "^6.2.1", "string-width": "^7.0.0", "strip-ansi": "^7.1.0" } }, "sha512-G8ura3S+3Z2G+mkgNRq8dqaFZAuxfsxpBB8OCTGRTCtp+l/v9nbFNmCUP1BZMts3G1142MsZfn6eeUKrr4PD1Q=="], @@ -1701,43 +1663,45 @@ "yoctocolors": ["yoctocolors@2.1.1", "", {}, "sha512-GQHQqAopRhwU8Kt1DDM8NjibDXHC8eoh1erhGAJPEyveY9qqVeXvVikNKrDz69sHowPMorbPUrH/mx8c50eiBQ=="], - "youch": ["youch@3.3.4", "", { "dependencies": { "cookie": "^0.7.1", "mustache": "^4.2.0", "stacktracey": "^2.1.8" } }, "sha512-UeVBXie8cA35DS6+nBkls68xaBBXCye0CNznrhszZjTbRVnJKQuNsyLKBTTL4ln1o1rh2PKtv35twV7irj5SEg=="], + "youch": ["youch@4.1.0-beta.10", "", { "dependencies": { "@poppinss/colors": "^4.1.5", "@poppinss/dumper": "^0.6.4", "@speed-highlight/core": "^1.2.7", "cookie": "^1.0.2", "youch-core": "^0.3.3" } }, "sha512-rLfVLB4FgQneDr0dv1oddCVZmKjcJ6yX6mS4pU82Mq/Dt9a3cLZQ62pDBL4AUO+uVrCvtWz3ZFUL2HFAFJ/BXQ=="], - "zod": ["zod@3.24.2", "", {}, "sha512-lY7CDW43ECgW9u1TcT3IoXHflywfVqDYze4waEz812jR/bZ8FHDsl7pFQoSZTz5N+2NqRXs8GBwnAwo3ZNxqhQ=="], + "youch-core": ["youch-core@0.3.3", "", { "dependencies": { "@poppinss/exception": "^1.2.2", "error-stack-parser-es": "^1.0.5" } }, "sha512-ho7XuGjLaJ2hWHoK8yFnsUGy2Y5uDpqSTq1FkHLK4/oqKtyUU1AFbOOxY4IpC9f0fTLjwYbslUz0Po5BpD1wrA=="], - "zod-openapi": ["zod-openapi@4.2.4", "", { "peerDependencies": { "zod": "^3.21.4" } }, "sha512-tsrQpbpqFCXqVXUzi3TPwFhuMtLN3oNZobOtYnK6/5VkXsNdnIgyNr4r8no4wmYluaxzN3F7iS+8xCW8BmMQ8g=="], + "zod": ["zod@3.25.49", "", {}, "sha512-JMMPMy9ZBk3XFEdbM3iL1brx4NUSejd6xr3ELrrGEfGb355gjhiAWtG3K5o+AViV/3ZfkIrCzXsZn6SbLwTR8Q=="], + + "zod-openapi": ["zod-openapi@4.1.0", "", { "peerDependencies": { "zod": "^3.21.4" } }, "sha512-bRCwRYhEO9CmFLyKgJX8h6j1dRtRiwOe+TLzMVPyV0pRW5vRIgb1rLgIGcuRZ5z3MmSVrZqbv3yva4IJrtZK4g=="], "zod-to-json-schema": ["zod-to-json-schema@3.24.5", "", { "peerDependencies": { "zod": "^3.24.1" } }, "sha512-/AuWwMP+YqiPbsJx5D6TfgRTc4kTLjsh5SOcd4bLsfUg2RcEXrFMJl1DGgdHy2aCfsIA/cr/1JM0xcB2GZji8g=="], "zod-to-ts": ["zod-to-ts@1.2.0", "", { "peerDependencies": { "typescript": "^4.9.4 || ^5.0.2", "zod": "^3" } }, "sha512-x30XE43V+InwGpvTySRNz9kB7qFU8DlyEy7BsSTCHPH1R0QasMmHWZDCzYm6bVXtj/9NNJAZF3jW8rzFvH5OFA=="], - "zod-validation-error": ["zod-validation-error@3.5.2", "", { "peerDependencies": { "zod": "^3.25.0" } }, "sha512-mdi7YOLtram5dzJ5aDtm1AG9+mxRma1iaMrZdYIpFO7epdKBUwLHIxTF8CPDeCQ828zAXYtizrKlEJAtzgfgrw=="], - "zwitch": ["zwitch@2.0.4", "", {}, "sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A=="], + "@ai-sdk/amazon-bedrock/@ai-sdk/provider": ["@ai-sdk/provider@1.1.3", "", { "dependencies": { "json-schema": "^0.4.0" } }, "sha512-qZMxYJ0qqX/RfnuIaab+zp8UAeJn/ygXXAffR5I4N0n1IrvA6qBsjc8hXLmBiMV2zoXlifkacF7sEFnYnjBcqg=="], + + "@ai-sdk/amazon-bedrock/@ai-sdk/provider-utils": ["@ai-sdk/provider-utils@2.2.8", "", { "dependencies": { "@ai-sdk/provider": "1.1.3", "nanoid": "^3.3.8", "secure-json-parse": "^2.7.0" }, "peerDependencies": { "zod": "^3.23.8" } }, "sha512-fqhG+4sCVv8x7nFzYnFo19ryhAa3w096Kmc3hWxMQfW/TubPOmt3A6tYZhl4mUfQWWQMsuSkLrtjlWuXBVSGQA=="], + "@ai-sdk/amazon-bedrock/aws4fetch": ["aws4fetch@1.0.20", "", {}, "sha512-/djoAN709iY65ETD6LKCtyyEI04XIBP5xVvfmNxsEP0uJB5tyaGBztSryRr4HqMStr9R06PisQE7m9zDTXKu6g=="], - "@ampproject/remapping/@jridgewell/trace-mapping": ["@jridgewell/trace-mapping@0.3.25", "", { "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", "@jridgewell/sourcemap-codec": "^1.4.14" } }, "sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ=="], + "@ai-sdk/anthropic/@ai-sdk/provider": ["@ai-sdk/provider@1.1.3", "", { "dependencies": { "json-schema": "^0.4.0" } }, "sha512-qZMxYJ0qqX/RfnuIaab+zp8UAeJn/ygXXAffR5I4N0n1IrvA6qBsjc8hXLmBiMV2zoXlifkacF7sEFnYnjBcqg=="], + + "@ai-sdk/anthropic/@ai-sdk/provider-utils": ["@ai-sdk/provider-utils@2.2.8", "", { "dependencies": { "@ai-sdk/provider": "1.1.3", "nanoid": "^3.3.8", "secure-json-parse": "^2.7.0" }, "peerDependencies": { "zod": "^3.23.8" } }, "sha512-fqhG+4sCVv8x7nFzYnFo19ryhAa3w096Kmc3hWxMQfW/TubPOmt3A6tYZhl4mUfQWWQMsuSkLrtjlWuXBVSGQA=="], + + "@ampproject/remapping/@jridgewell/trace-mapping": ["@jridgewell/trace-mapping@0.3.29", "", { "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", "@jridgewell/sourcemap-codec": "^1.4.14" } }, "sha512-uw6guiW/gcAGPDhLmd77/6lW8QLeiV5RUTsAX46Db6oLhGaVj4lhnPwb184s1bkc8kdVg/+h988dro8GRDpmYQ=="], "@astrojs/mdx/@astrojs/markdown-remark": ["@astrojs/markdown-remark@6.3.2", "", { "dependencies": { "@astrojs/internal-helpers": "0.6.1", "@astrojs/prism": "3.3.0", "github-slugger": "^2.0.0", "hast-util-from-html": "^2.0.3", "hast-util-to-text": "^4.0.2", "import-meta-resolve": "^4.1.0", "js-yaml": "^4.1.0", "mdast-util-definitions": "^6.0.0", "rehype-raw": "^7.0.0", "rehype-stringify": "^10.0.1", "remark-gfm": "^4.0.1", "remark-parse": "^11.0.0", "remark-rehype": "^11.1.2", "remark-smartypants": "^3.0.2", "shiki": "^3.2.1", "smol-toml": "^1.3.1", "unified": "^11.0.5", "unist-util-remove-position": "^5.0.0", "unist-util-visit": "^5.0.0", "unist-util-visit-parents": "^6.0.1", "vfile": "^6.0.3" } }, "sha512-bO35JbWpVvyKRl7cmSJD822e8YA8ThR/YbUsciWNA7yTcqpIAL2hJDToWP5KcZBWxGT6IOdOkHSXARSNZc4l/Q=="], - "@aws-crypto/crc32/tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], - "@aws-crypto/util/@smithy/util-utf8": ["@smithy/util-utf8@2.3.0", "", { "dependencies": { "@smithy/util-buffer-from": "^2.2.0", "tslib": "^2.6.2" } }, "sha512-R8Rdn8Hy72KKcebgLiv8jQcQkXoLMOGGv5uI1/k0l+snqkOzQ1R0ChUBCxWMlBsFMekWjq0wRudIweFs7sKT5A=="], - "@aws-crypto/util/tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], - - "@aws-sdk/types/tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], - "@babel/core/semver": ["semver@6.3.1", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA=="], - "@babel/generator/@jridgewell/trace-mapping": ["@jridgewell/trace-mapping@0.3.25", "", { "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", "@jridgewell/sourcemap-codec": "^1.4.14" } }, "sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ=="], + "@babel/generator/@jridgewell/trace-mapping": ["@jridgewell/trace-mapping@0.3.29", "", { "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", "@jridgewell/sourcemap-codec": "^1.4.14" } }, "sha512-uw6guiW/gcAGPDhLmd77/6lW8QLeiV5RUTsAX46Db6oLhGaVj4lhnPwb184s1bkc8kdVg/+h988dro8GRDpmYQ=="], "@babel/helper-compilation-targets/lru-cache": ["lru-cache@5.1.1", "", { "dependencies": { "yallist": "^3.0.2" } }, "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w=="], "@babel/helper-compilation-targets/semver": ["semver@6.3.1", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA=="], - "@jridgewell/gen-mapping/@jridgewell/trace-mapping": ["@jridgewell/trace-mapping@0.3.25", "", { "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", "@jridgewell/sourcemap-codec": "^1.4.14" } }, "sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ=="], + "@jridgewell/gen-mapping/@jridgewell/trace-mapping": ["@jridgewell/trace-mapping@0.3.29", "", { "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", "@jridgewell/sourcemap-codec": "^1.4.14" } }, "sha512-uw6guiW/gcAGPDhLmd77/6lW8QLeiV5RUTsAX46Db6oLhGaVj4lhnPwb184s1bkc8kdVg/+h988dro8GRDpmYQ=="], "@openauthjs/openauth/@standard-schema/spec": ["@standard-schema/spec@1.0.0-beta.3", "", {}, "sha512-0ifF3BjA1E8SY9C+nUew8RefNOIq0cDlYALPty4rhUm8Rrl6tCM8hBT4bhGhx7I7iXD0uAgt50lgo8dD73ACMw=="], @@ -1749,28 +1713,10 @@ "@rollup/pluginutils/estree-walker": ["estree-walker@2.0.2", "", {}, "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w=="], - "@smithy/eventstream-codec/tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], - - "@smithy/is-array-buffer/tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], - - "@smithy/types/tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], - - "@smithy/util-buffer-from/tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], - - "@smithy/util-hex-encoding/tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], - - "@smithy/util-utf8/tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], - - "@swc/helpers/tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], - "ansi-align/string-width": ["string-width@4.2.3", "", { "dependencies": { "emoji-regex": "^8.0.0", "is-fullwidth-code-point": "^3.0.0", "strip-ansi": "^6.0.1" } }, "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g=="], "anymatch/picomatch": ["picomatch@2.3.1", "", {}, "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA=="], - "args/camelcase": ["camelcase@5.0.0", "", {}, "sha512-faqwZqnWxbxn+F1d399ygeamQNy3lPp/H9H6rNrqYh4FSVCtcY+3cub1MxA8o9mDd55mM8Aghuu/kuyYA6VTsA=="], - - "args/chalk": ["chalk@2.4.2", "", { "dependencies": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", "supports-color": "^5.3.0" } }, "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ=="], - "astro/diff": ["diff@5.2.0", "", {}, "sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A=="], "astro/sharp": ["sharp@0.33.5", "", { "dependencies": { "color": "^4.2.3", "detect-libc": "^2.0.3", "semver": "^7.6.3" }, "optionalDependencies": { "@img/sharp-darwin-arm64": "0.33.5", "@img/sharp-darwin-x64": "0.33.5", "@img/sharp-libvips-darwin-arm64": "1.0.4", "@img/sharp-libvips-darwin-x64": "1.0.4", "@img/sharp-libvips-linux-arm": "1.0.5", "@img/sharp-libvips-linux-arm64": "1.0.4", "@img/sharp-libvips-linux-s390x": "1.0.4", "@img/sharp-libvips-linux-x64": "1.0.4", "@img/sharp-libvips-linuxmusl-arm64": "1.0.4", "@img/sharp-libvips-linuxmusl-x64": "1.0.4", "@img/sharp-linux-arm": "0.33.5", "@img/sharp-linux-arm64": "0.33.5", "@img/sharp-linux-s390x": "0.33.5", "@img/sharp-linux-x64": "0.33.5", "@img/sharp-linuxmusl-arm64": "0.33.5", "@img/sharp-linuxmusl-x64": "0.33.5", "@img/sharp-wasm32": "0.33.5", "@img/sharp-win32-ia32": "0.33.5", "@img/sharp-win32-x64": "0.33.5" } }, "sha512-haPVm1EkS9pgvHrQ/F3Xy+hgcuMV0Wm9vfIBSiwZ05k+xgb0PkBQpGsAA/oWdDobNaZTH5ppvHtzCFbnSEwHVw=="], @@ -1781,55 +1727,55 @@ "express/cookie": ["cookie@0.7.2", "", {}, "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w=="], - "get-source/source-map": ["source-map@0.6.1", "", {}, "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g=="], - "hast-util-to-parse5/property-information": ["property-information@6.5.0", "", {}, "sha512-PgTgs/BlvHxOu8QuEN7wi5A0OmXaBcHpmCSTehcs6Uuu9IkDIEo13Hy7n898RHfrQ49vKCoGeWZSaAK01nwVig=="], - "mdast-util-find-and-replace/escape-string-regexp": ["escape-string-regexp@5.0.0", "", {}, "sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw=="], + "http-errors/statuses": ["statuses@2.0.1", "", {}, "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ=="], "miniflare/acorn": ["acorn@8.14.0", "", { "bin": { "acorn": "bin/acorn" } }, "sha512-cl669nCJTZBsL97OF4kUQm5g5hC2uihk0NxY3WENAC0TYdILVkAyHymAntgxGkl7K+t0cXIrH5siy5S4XkFycA=="], "miniflare/sharp": ["sharp@0.33.5", "", { "dependencies": { "color": "^4.2.3", "detect-libc": "^2.0.3", "semver": "^7.6.3" }, "optionalDependencies": { "@img/sharp-darwin-arm64": "0.33.5", "@img/sharp-darwin-x64": "0.33.5", "@img/sharp-libvips-darwin-arm64": "1.0.4", "@img/sharp-libvips-darwin-x64": "1.0.4", "@img/sharp-libvips-linux-arm": "1.0.5", "@img/sharp-libvips-linux-arm64": "1.0.4", "@img/sharp-libvips-linux-s390x": "1.0.4", "@img/sharp-libvips-linux-x64": "1.0.4", "@img/sharp-libvips-linuxmusl-arm64": "1.0.4", "@img/sharp-libvips-linuxmusl-x64": "1.0.4", "@img/sharp-linux-arm": "0.33.5", "@img/sharp-linux-arm64": "0.33.5", "@img/sharp-linux-s390x": "0.33.5", "@img/sharp-linux-x64": "0.33.5", "@img/sharp-linuxmusl-arm64": "0.33.5", "@img/sharp-linuxmusl-x64": "0.33.5", "@img/sharp-wasm32": "0.33.5", "@img/sharp-win32-ia32": "0.33.5", "@img/sharp-win32-x64": "0.33.5" } }, "sha512-haPVm1EkS9pgvHrQ/F3Xy+hgcuMV0Wm9vfIBSiwZ05k+xgb0PkBQpGsAA/oWdDobNaZTH5ppvHtzCFbnSEwHVw=="], + "miniflare/zod": ["zod@3.22.3", "", {}, "sha512-EjIevzuJRiRPbVH4mGc8nApb/lVLKVpmUhAaR5R5doKGfAnGJ6Gr3CViAVjP+4FWSxCsybeWQdcgCtbX+7oZug=="], + + "opencode/remeda": ["remeda@2.22.3", "", { "dependencies": { "type-fest": "^4.40.1" } }, "sha512-Ka6965m9Zu9OLsysWxVf3jdJKmp6+PKzDv7HWHinEevf0JOJ9y02YpjiC/sKxRpCqGhVyvm1U+0YIj+E6DMgKw=="], + + "opencontrol/@modelcontextprotocol/sdk": ["@modelcontextprotocol/sdk@1.6.1", "", { "dependencies": { "content-type": "^1.0.5", "cors": "^2.8.5", "eventsource": "^3.0.2", "express": "^5.0.1", "express-rate-limit": "^7.5.0", "pkce-challenge": "^4.1.0", "raw-body": "^3.0.0", "zod": "^3.23.8", "zod-to-json-schema": "^3.24.1" } }, "sha512-oxzMzYCkZHMntzuyerehK3fV6A2Kwh5BD6CGEJSVDU2QNEhfLOptf2X7esQgaHZXHZY0oHmMsOtIDLP71UJXgA=="], + "opencontrol/hono": ["hono@4.7.4", "", {}, "sha512-Pst8FuGqz3L7tFF+u9Pu70eI0xa5S3LPUmrNd5Jm8nTHze9FxLTK9Kaj5g/k4UcwuJSXTP65SyHOPLrffpcAJg=="], + "opencontrol/zod": ["zod@3.24.2", "", {}, "sha512-lY7CDW43ECgW9u1TcT3IoXHflywfVqDYze4waEz812jR/bZ8FHDsl7pFQoSZTz5N+2NqRXs8GBwnAwo3ZNxqhQ=="], + "opencontrol/zod-to-json-schema": ["zod-to-json-schema@3.24.3", "", { "peerDependencies": { "zod": "^3.24.1" } }, "sha512-HIAfWdYIt1sssHfYZFCXp4rU1w2r8hVVXYIlmoa0r0gABLs5di3RCqPU5DDROogVz1pAdYBaz7HK5n9pSUNs3A=="], "openid-client/jose": ["jose@4.15.9", "", {}, "sha512-1vUQX+IdDMVPj4k8kOxgUqlcK518yluMuGZwqlr44FS1ppZB/5GWh4rZG89erpOBOJjU/OBsnCVFfapsRz6nEA=="], "parse-entities/@types/unist": ["@types/unist@2.0.11", "", {}, "sha512-CmBKiL6NNo/OqgmMn95Fk9Whlp2mtvIv+KNpQKN2F4SjvrEesubTRWGYSg+BnWZOnlCaSTU1sMpsBOzgbYhnsA=="], - "pino-abstract-transport/split2": ["split2@4.2.0", "", {}, "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg=="], - - "pino-pretty/chalk": ["chalk@4.1.2", "", { "dependencies": { "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" } }, "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA=="], - - "pino-pretty/jmespath": ["jmespath@0.15.0", "", {}, "sha512-+kHj8HXArPfpPEKGLZ+kB5ONRTCiGQXo8RQYL0hH8t6pWXUBBK5KkkQmTNOwKK4LEsd0yTsgtjJVm4UBSZea4w=="], - "prebuild-install/tar-fs": ["tar-fs@2.1.3", "", { "dependencies": { "chownr": "^1.1.1", "mkdirp-classic": "^0.5.2", "pump": "^3.0.0", "tar-stream": "^2.1.4" } }, "sha512-090nwYJDmlhwFwEW3QQl+vaNnxsO2yVsd45eTKRBzSzu+hlb1w2K9inVq5b0ngXuLVqQ4ApvsUHHnu/zQNkWAg=="], "prompts/kleur": ["kleur@3.0.3", "", {}, "sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w=="], - "rc/strip-json-comments": ["strip-json-comments@2.0.1", "", {}, "sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ=="], - "router/path-to-regexp": ["path-to-regexp@8.2.0", "", {}, "sha512-TdrF7fW9Rphjq4RjrW0Kp2AW0Ahwu9sRGTkS6bvDi0SCwZlEZYmcfDbEsTz8RVk0EHIS/Vd1bv3JhG+1xZuAyQ=="], "sitemap/@types/node": ["@types/node@17.0.45", "", {}, "sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw=="], "sitemap/sax": ["sax@1.4.1", "", {}, "sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg=="], - "token-types/ieee754": ["ieee754@1.2.1", "", {}, "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA=="], + "sst/jose": ["jose@5.2.3", "", {}, "sha512-KUXdbctm1uHVL8BYhnyHkgp3zDX5KW8ZhAKVFEfUbU2P8Alpzjb+48hHvjOdQIyPshoblhzsuqOwEEAbtHVirA=="], + + "to-buffer/isarray": ["isarray@2.0.5", "", {}, "sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw=="], "unicode-trie/pako": ["pako@0.2.9", "", {}, "sha512-NUcwaKxUxWrZLpDG+z/xZaCgQITkA/Dv4V/T6bw7VON6l1Xz/VnrBqrYjZQ12TamKHzITTfOEIYUj48y2KXImA=="], "unstorage/lru-cache": ["lru-cache@10.4.3", "", {}, "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ=="], - "vscode-languageserver-protocol/vscode-jsonrpc": ["vscode-jsonrpc@8.1.0", "", {}, "sha512-6TDy/abTQk+zDGYazgbIPc+4JoXdwC8NHU9Pbn4UJP1fehUyZmM4RHp5IthX7A6L5KS30PRui+j+tbbMMMafdw=="], + "uri-js/punycode": ["punycode@2.3.1", "", {}, "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg=="], "wrangler/esbuild": ["esbuild@0.25.4", "", { "optionalDependencies": { "@esbuild/aix-ppc64": "0.25.4", "@esbuild/android-arm": "0.25.4", "@esbuild/android-arm64": "0.25.4", "@esbuild/android-x64": "0.25.4", "@esbuild/darwin-arm64": "0.25.4", "@esbuild/darwin-x64": "0.25.4", "@esbuild/freebsd-arm64": "0.25.4", "@esbuild/freebsd-x64": "0.25.4", "@esbuild/linux-arm": "0.25.4", "@esbuild/linux-arm64": "0.25.4", "@esbuild/linux-ia32": "0.25.4", "@esbuild/linux-loong64": "0.25.4", "@esbuild/linux-mips64el": "0.25.4", "@esbuild/linux-ppc64": "0.25.4", "@esbuild/linux-riscv64": "0.25.4", "@esbuild/linux-s390x": "0.25.4", "@esbuild/linux-x64": "0.25.4", "@esbuild/netbsd-arm64": "0.25.4", "@esbuild/netbsd-x64": "0.25.4", "@esbuild/openbsd-arm64": "0.25.4", "@esbuild/openbsd-x64": "0.25.4", "@esbuild/sunos-x64": "0.25.4", "@esbuild/win32-arm64": "0.25.4", "@esbuild/win32-ia32": "0.25.4", "@esbuild/win32-x64": "0.25.4" }, "bin": { "esbuild": "bin/esbuild" } }, "sha512-8pgjLUcUjcgDg+2Q4NYXnPbo/vncAY4UmyaCm0jZevERqCHZIaWwdJHkf8XQtu4AxSKCdvrUbT0XUr1IdZzI8Q=="], - "yargs/yargs-parser": ["yargs-parser@22.0.0", "", {}, "sha512-rwu/ClNdSMpkSrUb+d6BRsSkLUq1fmfsY6TOpYzTwvwkg1/NRG85KBy3kq++A8LKQwX6lsu+aWad+2khvuXrqw=="], + "xml2js/sax": ["sax@1.4.1", "", {}, "sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg=="], - "youch/cookie": ["cookie@0.7.2", "", {}, "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w=="], + "yargs/yargs-parser": ["yargs-parser@22.0.0", "", {}, "sha512-rwu/ClNdSMpkSrUb+d6BRsSkLUq1fmfsY6TOpYzTwvwkg1/NRG85KBy3kq++A8LKQwX6lsu+aWad+2khvuXrqw=="], "@astrojs/mdx/@astrojs/markdown-remark/@astrojs/prism": ["@astrojs/prism@3.3.0", "", { "dependencies": { "prismjs": "^1.30.0" } }, "sha512-q8VwfU/fDZNoDOf+r7jUnMC2//H2l0TuQ6FkGJL8vD8nw/q5KiL3DS1KKBI3QhI9UQhpJ5dc7AtqfbXWuOgLCQ=="], @@ -1841,13 +1787,11 @@ "ansi-align/string-width/strip-ansi": ["strip-ansi@6.0.1", "", { "dependencies": { "ansi-regex": "^5.0.1" } }, "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A=="], - "args/chalk/ansi-styles": ["ansi-styles@3.2.1", "", { "dependencies": { "color-convert": "^1.9.0" } }, "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA=="], + "opencontrol/@modelcontextprotocol/sdk/pkce-challenge": ["pkce-challenge@4.1.0", "", {}, "sha512-ZBmhE1C9LcPoH9XZSdwiPtbPHZROwAnMy+kIFQVrnMCxY4Cudlz3gBOpzilgc0jOgRaiT3sIWfpMomW2ar2orQ=="], - "args/chalk/supports-color": ["supports-color@5.5.0", "", { "dependencies": { "has-flag": "^3.0.0" } }, "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow=="], + "opencontrol/@modelcontextprotocol/sdk/zod": ["zod@3.25.49", "", {}, "sha512-JMMPMy9ZBk3XFEdbM3iL1brx4NUSejd6xr3ELrrGEfGb355gjhiAWtG3K5o+AViV/3ZfkIrCzXsZn6SbLwTR8Q=="], - "bl/buffer/ieee754": ["ieee754@1.2.1", "", {}, "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA=="], - - "pino-pretty/chalk/ansi-styles": ["ansi-styles@4.3.0", "", { "dependencies": { "color-convert": "^2.0.1" } }, "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg=="], + "opencontrol/@modelcontextprotocol/sdk/zod-to-json-schema": ["zod-to-json-schema@3.24.5", "", { "peerDependencies": { "zod": "^3.24.1" } }, "sha512-/AuWwMP+YqiPbsJx5D6TfgRTc4kTLjsh5SOcd4bLsfUg2RcEXrFMJl1DGgdHy2aCfsIA/cr/1JM0xcB2GZji8g=="], "prebuild-install/tar-fs/tar-stream": ["tar-stream@2.2.0", "", { "dependencies": { "bl": "^4.0.3", "end-of-stream": "^1.4.1", "fs-constants": "^1.0.0", "inherits": "^2.0.3", "readable-stream": "^3.1.1" } }, "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ=="], @@ -1904,11 +1848,5 @@ "@aws-crypto/util/@smithy/util-utf8/@smithy/util-buffer-from/@smithy/is-array-buffer": ["@smithy/is-array-buffer@2.2.0", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-GGP3O9QFD24uGeAXYUjwSTXARoqpZykHadOmA8G5vfJPK0/DC67qa//0qvqrJzL1xc8WQWX7/yc7fwudjPHPhA=="], "ansi-align/string-width/strip-ansi/ansi-regex": ["ansi-regex@5.0.1", "", {}, "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ=="], - - "args/chalk/ansi-styles/color-convert": ["color-convert@1.9.3", "", { "dependencies": { "color-name": "1.1.3" } }, "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg=="], - - "args/chalk/supports-color/has-flag": ["has-flag@3.0.0", "", {}, "sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw=="], - - "args/chalk/ansi-styles/color-convert/color-name": ["color-name@1.1.3", "", {}, "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw=="], } } diff --git a/infra/app.ts b/infra/app.ts index 834936b7..5c646d97 100644 --- a/infra/app.ts +++ b/infra/app.ts @@ -4,6 +4,8 @@ export const domain = (() => { return `${$app.stage}.dev.opencode.ai` })() +const GITHUB_APP_ID = new sst.Secret("GITHUB_APP_ID") +const GITHUB_APP_PRIVATE_KEY = new sst.Secret("GITHUB_APP_PRIVATE_KEY") const bucket = new sst.cloudflare.Bucket("Bucket") export const api = new sst.cloudflare.Worker("Api", { @@ -13,7 +15,7 @@ export const api = new sst.cloudflare.Worker("Api", { WEB_DOMAIN: domain, }, url: true, - link: [bucket], + link: [bucket, GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY], transform: { worker: (args) => { args.logpush = true @@ -39,6 +41,8 @@ new sst.cloudflare.x.Astro("Web", { domain, path: "packages/web", environment: { + // For astro config + SST_STAGE: $app.stage, VITE_API_URL: api.url, }, }) diff --git a/install b/install index e18bd7bb..2a48cc1f 100755 --- a/install +++ b/install @@ -48,7 +48,7 @@ if [ -z "$requested_version" ]; then url="https://github.com/sst/opencode/releases/latest/download/$filename" specific_version=$(curl -s https://api.github.com/repos/sst/opencode/releases/latest | awk -F'"' '/"tag_name": "/ {gsub(/^v/, "", $4); print $4}') - if [[ $? -ne 0 ]]; then + if [[ $? -ne 0 || -z "$specific_version" ]]; then echo "${RED}Failed to fetch version information${NC}" exit 1 fi diff --git a/opencode.json b/opencode.json index 57b94008..2fa64407 100644 --- a/opencode.json +++ b/opencode.json @@ -1,19 +1,9 @@ { "$schema": "https://opencode.ai/config.json", - "experimental": { - "hook": { - "file_edited": { - ".json": [ - { - "command": ["bun", "run", "prettier", "$FILE"] - } - ] - }, - "session_completed": [ - { - "command": ["touch", "./node_modules/foo"] - } - ] + "mcp": { + "weather": { + "type": "local", + "command": ["opencode", "x", "@h1deya/mcp-server-weather"] } } } diff --git a/package.json b/package.json index 09248dcf..9c98d4dc 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "scripts": { "dev": "bun run packages/opencode/src/index.ts", "typecheck": "bun run --filter='*' typecheck", - "stainless": "bun run ./packages/opencode/src/index.ts serve ", + "stainless": "./scripts/stainless", "postinstall": "./scripts/hooks" }, "workspaces": { @@ -17,8 +17,8 @@ "catalog": { "typescript": "5.8.2", "@types/node": "22.13.9", - "zod": "3.24.2", - "ai": "4.3.16" + "zod": "3.25.49", + "ai": "5.0.0-beta.21" } }, "devDependencies": { @@ -31,17 +31,13 @@ }, "license": "MIT", "prettier": { - "semi": false - }, - "overrides": { - "zod": "3.24.2" + "semi": false, + "printWidth": 120 }, "trustedDependencies": [ "esbuild", "protobufjs", "sharp" ], - "patchedDependencies": { - "ai@4.3.16": "patches/ai@4.3.16.patch" - } + "patchedDependencies": {} } diff --git a/packages/function/package.json b/packages/function/package.json index 81a1edc9..633aeff8 100644 --- a/packages/function/package.json +++ b/packages/function/package.json @@ -8,5 +8,10 @@ "@cloudflare/workers-types": "4.20250522.0", "typescript": "catalog:", "@types/node": "catalog:" + }, + "dependencies": { + "@octokit/auth-app": "8.0.1", + "@octokit/rest": "22.0.0", + "jose": "6.0.11" } } diff --git a/packages/function/src/api.ts b/packages/function/src/api.ts index 701b9854..4ba7cbf6 100644 --- a/packages/function/src/api.ts +++ b/packages/function/src/api.ts @@ -1,5 +1,9 @@ import { DurableObject } from "cloudflare:workers" import { randomUUID } from "node:crypto" +import { jwtVerify, createRemoteJWKSet } from "jose" +import { createAppAuth } from "@octokit/auth-app" +import { Octokit } from "@octokit/rest" +import { Resource } from "sst" type Env = { SYNC_SERVER: DurableObjectNamespace @@ -40,7 +44,8 @@ export class SyncServer extends DurableObject { const sessionID = await this.getSessionID() if ( !key.startsWith(`session/info/${sessionID}`) && - !key.startsWith(`session/message/${sessionID}/`) + !key.startsWith(`session/message/${sessionID}/`) && + !key.startsWith(`session/part/${sessionID}/`) ) return new Response("Error: Invalid key", { status: 400 }) @@ -70,7 +75,7 @@ export class SyncServer extends DurableObject { } public async getData() { - const data = await this.ctx.storage.list() + const data = (await this.ctx.storage.list()) as Map return Array.from(data.entries()) .filter(([key, _]) => key.startsWith("session/")) .map(([key, content]) => ({ key, content })) @@ -107,7 +112,7 @@ export class SyncServer extends DurableObject { } export default { - async fetch(request: Request, env: Env, ctx: ExecutionContext) { + async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { const url = new URL(request.url) const splits = url.pathname.split("/") const method = splits[1] @@ -184,8 +189,7 @@ export default { } const id = url.searchParams.get("id") console.log("share_poll", id) - if (!id) - return new Response("Error: Share ID is required", { status: 400 }) + if (!id) return new Response("Error: Share ID is required", { status: 400 }) const stub = env.SYNC_SERVER.get(env.SYNC_SERVER.idFromName(id)) return stub.fetch(request) } @@ -193,8 +197,7 @@ export default { if (request.method === "GET" && method === "share_data") { const id = url.searchParams.get("id") console.log("share_data", id) - if (!id) - return new Response("Error: Share ID is required", { status: 400 }) + if (!id) return new Response("Error: Share ID is required", { status: 400 }) const stub = env.SYNC_SERVER.get(env.SYNC_SERVER.idFromName(id)) const data = await stub.getData() @@ -208,8 +211,13 @@ export default { return } if (type === "message") { - const [, messageID] = splits - messages[messageID] = d.content + messages[d.content.id] = { + parts: [], + ...d.content, + } + } + if (type === "part") { + messages[d.content.messageID].parts.push(d.content) } }) @@ -223,5 +231,95 @@ export default { }, ) } + + /** + * Used by the GitHub action to get GitHub installation access token given the OIDC token + */ + if (request.method === "POST" && method === "exchange_github_app_token") { + const EXPECTED_AUDIENCE = "opencode-github-action" + const GITHUB_ISSUER = "https://token.actions.githubusercontent.com" + const JWKS_URL = `${GITHUB_ISSUER}/.well-known/jwks` + + // get Authorization header + const authHeader = request.headers.get("Authorization") + const token = authHeader?.replace(/^Bearer /, "") + if (!token) + return new Response(JSON.stringify({ error: "Authorization header is required" }), { + status: 401, + headers: { "Content-Type": "application/json" }, + }) + + // verify token + const JWKS = createRemoteJWKSet(new URL(JWKS_URL)) + let owner, repo + try { + const { payload } = await jwtVerify(token, JWKS, { + issuer: GITHUB_ISSUER, + audience: EXPECTED_AUDIENCE, + }) + const sub = payload.sub // e.g. 'repo:my-org/my-repo:ref:refs/heads/main' + const parts = sub.split(":")[1].split("/") + owner = parts[0] + repo = parts[1] + } catch (err) { + console.error("Token verification failed:", err) + return new Response(JSON.stringify({ error: "Invalid or expired token" }), { + status: 403, + headers: { "Content-Type": "application/json" }, + }) + } + + // Create app JWT token + const auth = createAppAuth({ + appId: Resource.GITHUB_APP_ID.value, + privateKey: Resource.GITHUB_APP_PRIVATE_KEY.value, + }) + const appAuth = await auth({ type: "app" }) + + // Lookup installation + const octokit = new Octokit({ auth: appAuth.token }) + const { data: installation } = await octokit.apps.getRepoInstallation({ owner, repo }) + + // Get installation token + const installationAuth = await auth({ type: "installation", installationId: installation.id }) + + return new Response(JSON.stringify({ token: installationAuth.token }), { + headers: { "Content-Type": "application/json" }, + }) + } + + /** + * Used by the opencode CLI to check if the GitHub app is installed + */ + if (request.method === "GET" && method === "get_github_app_installation") { + const owner = url.searchParams.get("owner") + const repo = url.searchParams.get("repo") + + const auth = createAppAuth({ + appId: Resource.GITHUB_APP_ID.value, + privateKey: Resource.GITHUB_APP_PRIVATE_KEY.value, + }) + const appAuth = await auth({ type: "app" }) + + // Lookup installation + const octokit = new Octokit({ auth: appAuth.token }) + let installation + try { + const ret = await octokit.apps.getRepoInstallation({ owner, repo }) + installation = ret.data + } catch (err) { + if (err instanceof Error && err.message.includes("Not Found")) { + // not installed + } else { + throw err + } + } + + return new Response(JSON.stringify({ installation }), { + headers: { "Content-Type": "application/json" }, + }) + } + + return new Response("Not Found", { status: 404 }) }, } diff --git a/packages/function/sst-env.d.ts b/packages/function/sst-env.d.ts index fd95edbb..dab7de3f 100644 --- a/packages/function/sst-env.d.ts +++ b/packages/function/sst-env.d.ts @@ -6,20 +6,28 @@ import "sst" declare module "sst" { export interface Resource { - Web: { - type: "sst.cloudflare.Astro" - url: string + "GITHUB_APP_ID": { + "type": "sst.sst.Secret" + "value": string + } + "GITHUB_APP_PRIVATE_KEY": { + "type": "sst.sst.Secret" + "value": string + } + "Web": { + "type": "sst.cloudflare.Astro" + "url": string } } } -// cloudflare -import * as cloudflare from "@cloudflare/workers-types" +// cloudflare +import * as cloudflare from "@cloudflare/workers-types"; declare module "sst" { export interface Resource { - Api: cloudflare.Service - Bucket: cloudflare.R2Bucket + "Api": cloudflare.Service + "Bucket": cloudflare.R2Bucket } } import "sst" -export {} +export {} \ No newline at end of file diff --git a/packages/opencode/config.schema.json b/packages/opencode/config.schema.json deleted file mode 100644 index 35dfd6f1..00000000 --- a/packages/opencode/config.schema.json +++ /dev/null @@ -1,369 +0,0 @@ -{ - "type": "object", - "properties": { - "$schema": { - "type": "string", - "description": "JSON schema reference for configuration validation" - }, - "theme": { - "type": "string", - "description": "Theme name to use for the interface" - }, - "keybinds": { - "type": "object", - "properties": { - "leader": { - "type": "string", - "description": "Leader key for keybind combinations" - }, - "help": { - "type": "string", - "description": "Show help dialog" - }, - "editor_open": { - "type": "string", - "description": "Open external editor" - }, - "session_new": { - "type": "string", - "description": "Create a new session" - }, - "session_list": { - "type": "string", - "description": "List all sessions" - }, - "session_share": { - "type": "string", - "description": "Share current session" - }, - "session_interrupt": { - "type": "string", - "description": "Interrupt current session" - }, - "session_compact": { - "type": "string", - "description": "Toggle compact mode for session" - }, - "tool_details": { - "type": "string", - "description": "Show tool details" - }, - "model_list": { - "type": "string", - "description": "List available models" - }, - "theme_list": { - "type": "string", - "description": "List available themes" - }, - "project_init": { - "type": "string", - "description": "Initialize project configuration" - }, - "input_clear": { - "type": "string", - "description": "Clear input field" - }, - "input_paste": { - "type": "string", - "description": "Paste from clipboard" - }, - "input_submit": { - "type": "string", - "description": "Submit input" - }, - "input_newline": { - "type": "string", - "description": "Insert newline in input" - }, - "history_previous": { - "type": "string", - "description": "Navigate to previous history item" - }, - "history_next": { - "type": "string", - "description": "Navigate to next history item" - }, - "messages_page_up": { - "type": "string", - "description": "Scroll messages up by one page" - }, - "messages_page_down": { - "type": "string", - "description": "Scroll messages down by one page" - }, - "messages_half_page_up": { - "type": "string", - "description": "Scroll messages up by half page" - }, - "messages_half_page_down": { - "type": "string", - "description": "Scroll messages down by half page" - }, - "messages_previous": { - "type": "string", - "description": "Navigate to previous message" - }, - "messages_next": { - "type": "string", - "description": "Navigate to next message" - }, - "messages_first": { - "type": "string", - "description": "Navigate to first message" - }, - "messages_last": { - "type": "string", - "description": "Navigate to last message" - }, - "app_exit": { - "type": "string", - "description": "Exit the application" - } - }, - "additionalProperties": false, - "description": "Custom keybind configurations" - }, - "autoshare": { - "type": "boolean", - "description": "Share newly created sessions automatically" - }, - "autoupdate": { - "type": "boolean", - "description": "Automatically update to the latest version" - }, - "disabled_providers": { - "type": "array", - "items": { - "type": "string" - }, - "description": "Disable providers that are loaded automatically" - }, - "model": { - "type": "string", - "description": "Model to use in the format of provider/model, eg anthropic/claude-2" - }, - "provider": { - "type": "object", - "additionalProperties": { - "type": "object", - "properties": { - "api": { - "type": "string" - }, - "name": { - "type": "string" - }, - "env": { - "type": "array", - "items": { - "type": "string" - } - }, - "id": { - "type": "string" - }, - "npm": { - "type": "string" - }, - "models": { - "type": "object", - "additionalProperties": { - "type": "object", - "properties": { - "name": { - "type": "string" - }, - "attachment": { - "type": "boolean" - }, - "reasoning": { - "type": "boolean" - }, - "temperature": { - "type": "boolean" - }, - "tool_call": { - "type": "boolean" - }, - "cost": { - "type": "object", - "properties": { - "input": { - "type": "number" - }, - "output": { - "type": "number" - }, - "cache_read": { - "type": "number" - }, - "cache_write": { - "type": "number" - } - }, - "required": ["input", "output"], - "additionalProperties": false - }, - "limit": { - "type": "object", - "properties": { - "context": { - "type": "number" - }, - "output": { - "type": "number" - } - }, - "required": ["context", "output"], - "additionalProperties": false - }, - "id": { - "type": "string" - }, - "options": { - "type": "object", - "additionalProperties": {} - } - }, - "additionalProperties": false - } - }, - "options": { - "type": "object", - "additionalProperties": {} - } - }, - "required": ["models"], - "additionalProperties": false - }, - "description": "Custom provider configurations and model overrides" - }, - "mcp": { - "type": "object", - "additionalProperties": { - "anyOf": [ - { - "type": "object", - "properties": { - "type": { - "type": "string", - "const": "local", - "description": "Type of MCP server connection" - }, - "command": { - "type": "array", - "items": { - "type": "string" - }, - "description": "Command and arguments to run the MCP server" - }, - "environment": { - "type": "object", - "additionalProperties": { - "type": "string" - }, - "description": "Environment variables to set when running the MCP server" - }, - "enabled": { - "type": "boolean", - "description": "Enable or disable the MCP server on startup" - } - }, - "required": ["type", "command"], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "type": { - "type": "string", - "const": "remote", - "description": "Type of MCP server connection" - }, - "url": { - "type": "string", - "description": "URL of the remote MCP server" - }, - "enabled": { - "type": "boolean", - "description": "Enable or disable the MCP server on startup" - } - }, - "required": ["type", "url"], - "additionalProperties": false - } - ] - }, - "description": "MCP (Model Context Protocol) server configurations" - }, - "instructions": { - "type": "array", - "items": { - "type": "string" - }, - "description": "Additional instruction files or patterns to include" - }, - "experimental": { - "type": "object", - "properties": { - "hook": { - "type": "object", - "properties": { - "file_edited": { - "type": "object", - "additionalProperties": { - "type": "array", - "items": { - "type": "object", - "properties": { - "command": { - "type": "array", - "items": { - "type": "string" - } - }, - "environment": { - "type": "object", - "additionalProperties": { - "type": "string" - } - } - }, - "required": ["command"], - "additionalProperties": false - } - } - }, - "session_completed": { - "type": "array", - "items": { - "type": "object", - "properties": { - "command": { - "type": "array", - "items": { - "type": "string" - } - }, - "environment": { - "type": "object", - "additionalProperties": { - "type": "string" - } - } - }, - "required": ["command"], - "additionalProperties": false - } - } - }, - "additionalProperties": false - } - }, - "additionalProperties": false - } - }, - "additionalProperties": false, - "$schema": "http://json-schema.org/draft-07/schema#" -} diff --git a/packages/opencode/package.json b/packages/opencode/package.json index fae2e342..8541e018 100644 --- a/packages/opencode/package.json +++ b/packages/opencode/package.json @@ -17,37 +17,33 @@ "devDependencies": { "@ai-sdk/amazon-bedrock": "2.2.10", "@ai-sdk/anthropic": "1.2.12", + "@standard-schema/spec": "1.0.0", "@tsconfig/bun": "1.0.7", "@types/bun": "latest", "@types/turndown": "5.0.5", "@types/yargs": "17.0.33", "typescript": "catalog:", + "vscode-languageserver-types": "3.17.5", "zod-to-json-schema": "3.24.5" }, "dependencies": { "@clack/prompts": "0.11.0", - "@flystorage/file-storage": "1.1.0", - "@flystorage/local-fs": "1.1.0", - "@hono/zod-validator": "0.5.0", + "@hono/zod-validator": "0.4.2", + "@modelcontextprotocol/sdk": "1.15.1", "@openauthjs/openauth": "0.4.3", - "@standard-schema/spec": "1.0.0", "ai": "catalog:", "decimal.js": "10.5.0", "diff": "8.0.2", - "env-paths": "3.0.0", "hono": "4.7.10", "hono-openapi": "0.4.8", "isomorphic-git": "1.32.1", "open": "10.1.2", "remeda": "2.22.3", - "ts-lsp-client": "1.0.3", "turndown": "7.2.0", "vscode-jsonrpc": "8.2.1", - "vscode-languageclient": "8", "xdg-basedir": "5.1.0", "yargs": "18.0.0", "zod": "catalog:", - "zod-openapi": "4.2.4", - "zod-validation-error": "3.5.2" + "zod-openapi": "4.1.0" } } diff --git a/packages/opencode/script/publish.ts b/packages/opencode/script/publish.ts index 3f4c2005..08e042d7 100755 --- a/packages/opencode/script/publish.ts +++ b/packages/opencode/script/publish.ts @@ -9,7 +9,7 @@ const snapshot = process.argv.includes("--snapshot") const version = snapshot ? `0.0.0-${new Date().toISOString().slice(0, 16).replace(/[-:T]/g, "")}` - : await $`git describe --tags --exact-match HEAD` + : await $`git describe --tags --abbrev=0` .text() .then((x) => x.substring(1).trim()) .catch(() => { @@ -57,8 +57,7 @@ for (const [os, arch] of targets) { 2, ), ) - if (!dry) - await $`cd dist/${name} && bun publish --access public --tag ${npmTag}` + if (!dry) await $`cd dist/${name} && bun publish --access public --tag ${npmTag}` optionalDependencies[name] = version } @@ -82,8 +81,7 @@ await Bun.file(`./dist/${pkg.name}/package.json`).write( 2, ), ) -if (!dry) - await $`cd ./dist/${pkg.name} && bun publish --access public --tag ${npmTag}` +if (!dry) await $`cd ./dist/${pkg.name} && bun publish --access public --tag ${npmTag}` if (!snapshot) { // Github Release @@ -91,15 +89,11 @@ if (!snapshot) { await $`cd dist/${key}/bin && zip -r ../../${key}.zip *` } - const previous = await fetch( - "https://api.github.com/repos/sst/opencode/releases/latest", - ) + const previous = await fetch("https://api.github.com/repos/sst/opencode/releases/latest") .then((res) => res.json()) .then((data) => data.tag_name) - const commits = await fetch( - `https://api.github.com/repos/sst/opencode/compare/${previous}...HEAD`, - ) + const commits = await fetch(`https://api.github.com/repos/sst/opencode/compare/${previous}...HEAD`) .then((res) => res.json()) .then((data) => data.commits || []) @@ -109,6 +103,7 @@ if (!snapshot) { const lower = x.toLowerCase() return ( !lower.includes("ignore:") && + !lower.includes("chore:") && !lower.includes("ci:") && !lower.includes("wip:") && !lower.includes("docs:") && @@ -117,26 +112,13 @@ if (!snapshot) { }) .join("\n") - if (!dry) - await $`gh release create v${version} --title "v${version}" --notes ${notes} ./dist/*.zip` + if (!dry) await $`gh release create v${version} --title "v${version}" --notes ${notes} ./dist/*.zip` // Calculate SHA values - const arm64Sha = - await $`sha256sum ./dist/opencode-linux-arm64.zip | cut -d' ' -f1` - .text() - .then((x) => x.trim()) - const x64Sha = - await $`sha256sum ./dist/opencode-linux-x64.zip | cut -d' ' -f1` - .text() - .then((x) => x.trim()) - const macX64Sha = - await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1` - .text() - .then((x) => x.trim()) - const macArm64Sha = - await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1` - .text() - .then((x) => x.trim()) + const arm64Sha = await $`sha256sum ./dist/opencode-linux-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim()) + const x64Sha = await $`sha256sum ./dist/opencode-linux-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim()) + const macX64Sha = await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim()) + const macArm64Sha = await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim()) // AUR package const pkgbuild = [ @@ -170,9 +152,7 @@ if (!snapshot) { for (const pkg of ["opencode", "opencode-bin"]) { await $`rm -rf ./dist/aur-${pkg}` await $`git clone ssh://aur@aur.archlinux.org/${pkg}.git ./dist/aur-${pkg}` - await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write( - pkgbuild.replace("${pkg}", pkg), - ) + await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild.replace("${pkg}", pkg)) await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO` await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO` await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${version}"` diff --git a/packages/opencode/script/schema.ts b/packages/opencode/script/schema.ts index 1c0067c7..008c168c 100755 --- a/packages/opencode/script/schema.ts +++ b/packages/opencode/script/schema.ts @@ -4,5 +4,32 @@ import "zod-openapi/extend" import { Config } from "../src/config/config" import { zodToJsonSchema } from "zod-to-json-schema" -const result = zodToJsonSchema(Config.Info) -await Bun.write("config.schema.json", JSON.stringify(result, null, 2)) +const file = process.argv[2] + +const result = zodToJsonSchema(Config.Info, { + /** + * We'll use the `default` values of the field as the only value in `examples`. + * This will ensure no docs are needed to be read, as the configuration is + * self-documenting. + * + * See https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.9.5 + */ + postProcess(jsonSchema) { + const schema = jsonSchema as typeof jsonSchema & { + examples?: unknown[] + } + if (schema && typeof schema === "object" && "type" in schema && schema.type === "string" && schema?.default) { + if (!schema.examples) { + schema.examples = [schema.default] + } + + schema.description = [schema.description || "", `default: \`${schema.default}\``] + .filter(Boolean) + .join("\n\n") + .trim() + } + + return jsonSchema + }, +}) +await Bun.write(file, JSON.stringify(result, null, 2)) diff --git a/packages/opencode/src/app/app.ts b/packages/opencode/src/app/app.ts index 6467d252..fc7f49cb 100644 --- a/packages/opencode/src/app/app.ts +++ b/packages/opencode/src/app/app.ts @@ -12,7 +12,6 @@ export namespace App { export const Info = z .object({ - user: z.string(), hostname: z.string(), git: z.boolean(), path: z.object({ @@ -45,23 +44,14 @@ export namespace App { } export const provideExisting = ctx.provide - export async function provide( - input: Input, - cb: (app: App.Info) => Promise, - ) { + export async function provide(input: Input, cb: (app: App.Info) => Promise) { log.info("creating", { cwd: input.cwd, }) - const git = await Filesystem.findUp(".git", input.cwd).then(([x]) => - x ? path.dirname(x) : undefined, - ) + const git = await Filesystem.findUp(".git", input.cwd).then(([x]) => (x ? path.dirname(x) : undefined)) log.info("git", { git }) - const data = path.join( - Global.Path.data, - "project", - git ? directory(git) : "global", - ) + const data = path.join(Global.Path.data, "project", git ? directory(git) : "global") const stateFile = Bun.file(path.join(data, APP_JSON)) const state = (await stateFile.json().catch(() => ({}))) as { initialized: number @@ -79,7 +69,6 @@ export namespace App { const root = git ?? input.cwd const info: Info = { - user: os.userInfo().username, hostname: os.hostname(), time: { initialized: state.initialized, diff --git a/packages/opencode/src/auth/anthropic.ts b/packages/opencode/src/auth/anthropic.ts index df4af692..d3228cb8 100644 --- a/packages/opencode/src/auth/anthropic.ts +++ b/packages/opencode/src/auth/anthropic.ts @@ -4,20 +4,18 @@ import { Auth } from "./index" export namespace AuthAnthropic { const CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e" - export async function authorize() { + export async function authorize(mode: "max" | "console") { const pkce = await generatePKCE() - const url = new URL("https://claude.ai/oauth/authorize", import.meta.url) + + const url = new URL( + `https://${mode === "console" ? "console.anthropic.com" : "claude.ai"}/oauth/authorize`, + import.meta.url, + ) url.searchParams.set("code", "true") url.searchParams.set("client_id", CLIENT_ID) url.searchParams.set("response_type", "code") - url.searchParams.set( - "redirect_uri", - "https://console.anthropic.com/oauth/code/callback", - ) - url.searchParams.set( - "scope", - "org:create_api_key user:profile user:inference", - ) + url.searchParams.set("redirect_uri", "https://console.anthropic.com/oauth/code/callback") + url.searchParams.set("scope", "org:create_api_key user:profile user:inference") url.searchParams.set("code_challenge", pkce.challenge) url.searchParams.set("code_challenge_method", "S256") url.searchParams.set("state", pkce.verifier) @@ -45,32 +43,28 @@ export namespace AuthAnthropic { }) if (!result.ok) throw new ExchangeFailed() const json = await result.json() - await Auth.set("anthropic", { - type: "oauth", + return { refresh: json.refresh_token as string, access: json.access_token as string, expires: Date.now() + json.expires_in * 1000, - }) + } } export async function access() { const info = await Auth.get("anthropic") if (!info || info.type !== "oauth") return if (info.access && info.expires > Date.now()) return info.access - const response = await fetch( - "https://console.anthropic.com/v1/oauth/token", - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - grant_type: "refresh_token", - refresh_token: info.refresh, - client_id: CLIENT_ID, - }), + const response = await fetch("https://console.anthropic.com/v1/oauth/token", { + method: "POST", + headers: { + "Content-Type": "application/json", }, - ) + body: JSON.stringify({ + grant_type: "refresh_token", + refresh_token: info.refresh, + client_id: CLIENT_ID, + }), + }) if (!response.ok) return const json = await response.json() await Auth.set("anthropic", { diff --git a/packages/opencode/src/auth/copilot.ts b/packages/opencode/src/auth/copilot.ts index 4bbbaf2c..042f7c35 100644 --- a/packages/opencode/src/auth/copilot.ts +++ b/packages/opencode/src/auth/copilot.ts @@ -4,9 +4,7 @@ import path from "path" export const AuthCopilot = lazy(async () => { const file = Bun.file(path.join(Global.Path.state, "plugin", "copilot.ts")) - const response = fetch( - "https://raw.githubusercontent.com/sst/opencode-github-copilot/refs/heads/main/auth.ts", - ) + const response = fetch("https://raw.githubusercontent.com/sst/opencode-github-copilot/refs/heads/main/auth.ts") .then((x) => Bun.write(file, x)) .catch(() => {}) diff --git a/packages/opencode/src/auth/github-copilot.ts b/packages/opencode/src/auth/github-copilot.ts index c03caa26..ba5274e5 100644 --- a/packages/opencode/src/auth/github-copilot.ts +++ b/packages/opencode/src/auth/github-copilot.ts @@ -122,10 +122,7 @@ export namespace AuthGithubCopilot { return tokenData.token } - export const DeviceCodeError = NamedError.create( - "DeviceCodeError", - z.object({}), - ) + export const DeviceCodeError = NamedError.create("DeviceCodeError", z.object({})) export const TokenExchangeError = NamedError.create( "TokenExchangeError", diff --git a/packages/opencode/src/bun/index.ts b/packages/opencode/src/bun/index.ts index 7a7d89cf..eea46737 100644 --- a/packages/opencode/src/bun/index.ts +++ b/packages/opencode/src/bun/index.ts @@ -8,10 +8,7 @@ import { readableStreamToText } from "bun" export namespace BunProc { const log = Log.create({ service: "bun" }) - export async function run( - cmd: string[], - options?: Bun.SpawnOptions.OptionsObject, - ) { + export async function run(cmd: string[], options?: Bun.SpawnOptions.OptionsObject) { log.info("running", { cmd: [which(), ...cmd], ...options, @@ -26,9 +23,17 @@ export namespace BunProc { BUN_BE_BUN: "1", }, }) - const code = await result.exited; - const stdout = result.stdout ? typeof result.stdout === "number" ? result.stdout : await readableStreamToText(result.stdout) : undefined - const stderr = result.stderr ? typeof result.stderr === "number" ? result.stderr : await readableStreamToText(result.stderr) : undefined + const code = await result.exited + const stdout = result.stdout + ? typeof result.stdout === "number" + ? result.stdout + : await readableStreamToText(result.stdout) + : undefined + const stderr = result.stderr + ? typeof result.stderr === "number" + ? result.stderr + : await readableStreamToText(result.stderr) + : undefined log.info("done", { code, stdout, @@ -55,13 +60,22 @@ export namespace BunProc { export async function install(pkg: string, version = "latest") { const mod = path.join(Global.Path.cache, "node_modules", pkg) const pkgjson = Bun.file(path.join(Global.Path.cache, "package.json")) - const parsed = await pkgjson.json().catch(() => ({ - dependencies: {}, - })) + const parsed = await pkgjson.json().catch(async () => { + const result = { dependencies: {} } + await Bun.write(pkgjson.name!, JSON.stringify(result, null, 2)) + return result + }) if (parsed.dependencies[pkg] === version) return mod - parsed.dependencies[pkg] = version - await Bun.write(pkgjson, JSON.stringify(parsed, null, 2)) - await BunProc.run(["install", "--registry=https://registry.npmjs.org"], { + + // Build command arguments + const args = ["add", "--force", "--exact", "--cwd", Global.Path.cache, pkg + "@" + version] + + // Let Bun handle registry resolution: + // - If .npmrc files exist, Bun will use them automatically + // - If no .npmrc files exist, Bun will default to https://registry.npmjs.org + log.info("installing package using Bun's default registry resolution", { pkg, version }) + + await BunProc.run(args, { cwd: Global.Path.cache, }).catch((e) => { throw new InstallFailedError( @@ -71,6 +85,8 @@ export namespace BunProc { }, ) }) + parsed.dependencies[pkg] = version + await Bun.write(pkgjson.name!, JSON.stringify(parsed, null, 2)) return mod } } diff --git a/packages/opencode/src/bus/index.ts b/packages/opencode/src/bus/index.ts index 8461269a..0353da90 100644 --- a/packages/opencode/src/bus/index.ts +++ b/packages/opencode/src/bus/index.ts @@ -18,10 +18,7 @@ export namespace Bus { const registry = new Map() - export function event( - type: Type, - properties: Properties, - ) { + export function event(type: Type, properties: Properties) { const result = { type, properties, @@ -72,10 +69,7 @@ export namespace Bus { export function subscribe( def: Definition, - callback: (event: { - type: Definition["type"] - properties: z.infer - }) => void, + callback: (event: { type: Definition["type"]; properties: z.infer }) => void, ) { return raw(def.type, callback) } diff --git a/packages/opencode/src/cli/bootstrap.ts b/packages/opencode/src/cli/bootstrap.ts index 9ae274ed..4419773b 100644 --- a/packages/opencode/src/cli/bootstrap.ts +++ b/packages/opencode/src/cli/bootstrap.ts @@ -1,20 +1,15 @@ import { App } from "../app/app" import { ConfigHooks } from "../config/hooks" -import { FileWatcher } from "../file/watch" import { Format } from "../format" import { LSP } from "../lsp" import { Share } from "../share/share" -export async function bootstrap( - input: App.Input, - cb: (app: App.Info) => Promise, -) { +export async function bootstrap(input: App.Input, cb: (app: App.Info) => Promise) { return App.provide(input, async (app) => { Share.init() Format.init() ConfigHooks.init() LSP.init() - FileWatcher.init() return cb(app) }) diff --git a/packages/opencode/src/cli/cmd/auth.ts b/packages/opencode/src/cli/cmd/auth.ts index 9e8da95b..fd7c0e64 100644 --- a/packages/opencode/src/cli/cmd/auth.ts +++ b/packages/opencode/src/cli/cmd/auth.ts @@ -15,11 +15,7 @@ export const AuthCommand = cmd({ command: "auth", describe: "manage credentials", builder: (yargs) => - yargs - .command(AuthLoginCommand) - .command(AuthLogoutCommand) - .command(AuthListCommand) - .demandCommand(), + yargs.command(AuthLoginCommand).command(AuthLogoutCommand).command(AuthListCommand).demandCommand(), async handler() {}, }) @@ -31,9 +27,7 @@ export const AuthListCommand = cmd({ UI.empty() const authPath = path.join(Global.Path.data, "auth.json") const homedir = os.homedir() - const displayPath = authPath.startsWith(homedir) - ? authPath.replace(homedir, "~") - : authPath + const displayPath = authPath.startsWith(homedir) ? authPath.replace(homedir, "~") : authPath prompts.intro(`Credentials ${UI.Style.TEXT_DIM}${displayPath}`) const results = await Auth.all().then((x) => Object.entries(x)) const database = await ModelsDev.get() @@ -114,8 +108,7 @@ export const AuthLoginCommand = cmd({ if (provider === "other") { provider = await prompts.text({ message: "Enter provider id", - validate: (x) => - x.match(/^[a-z-]+$/) ? undefined : "a-z and hyphens only", + validate: (x) => (x.match(/^[a-z-]+$/) ? undefined : "a-z and hyphens only"), }) if (prompts.isCancel(provider)) throw new UI.CancelledError() provider = provider.replace(/^@ai-sdk\//, "") @@ -127,7 +120,7 @@ export const AuthLoginCommand = cmd({ if (provider === "amazon-bedrock") { prompts.log.info( - "Amazon bedrock can be configured with standard AWS environment variables like AWS_PROFILE or AWS_ACCESS_KEY_ID", + "Amazon bedrock can be configured with standard AWS environment variables like AWS_BEARER_TOKEN_BEDROCK, AWS_PROFILE or AWS_ACCESS_KEY_ID", ) prompts.outro("Done") return @@ -139,20 +132,24 @@ export const AuthLoginCommand = cmd({ options: [ { label: "Claude Pro/Max", - value: "oauth", + value: "max", }, { - label: "API Key", + label: "Create API Key", + value: "console", + }, + { + label: "Manually enter API Key", value: "api", }, ], }) if (prompts.isCancel(method)) throw new UI.CancelledError() - if (method === "oauth") { + if (method === "max") { // some weird bug where program exits without this await new Promise((resolve) => setTimeout(resolve, 10)) - const { url, verifier } = await AuthAnthropic.authorize() + const { url, verifier } = await AuthAnthropic.authorize("max") prompts.note("Trying to open browser...") try { await open(url) @@ -169,13 +166,66 @@ export const AuthLoginCommand = cmd({ }) if (prompts.isCancel(code)) throw new UI.CancelledError() - await AuthAnthropic.exchange(code, verifier) - .then(() => { - prompts.log.success("Login successful") + try { + const credentials = await AuthAnthropic.exchange(code, verifier) + await Auth.set("anthropic", { + type: "oauth", + refresh: credentials.refresh, + access: credentials.access, + expires: credentials.expires, }) - .catch(() => { - prompts.log.error("Invalid code") + prompts.log.success("Login successful") + } catch { + prompts.log.error("Invalid code") + } + prompts.outro("Done") + return + } + + if (method === "console") { + // some weird bug where program exits without this + await new Promise((resolve) => setTimeout(resolve, 10)) + const { url, verifier } = await AuthAnthropic.authorize("console") + prompts.note("Trying to open browser...") + try { + await open(url) + } catch (e) { + prompts.log.error( + "Failed to open browser perhaps you are running without a display or X server, please open the following URL in your browser:", + ) + } + prompts.log.info(url) + + const code = await prompts.text({ + message: "Paste the authorization code here: ", + validate: (x) => (x.length > 0 ? undefined : "Required"), + }) + if (prompts.isCancel(code)) throw new UI.CancelledError() + + try { + const credentials = await AuthAnthropic.exchange(code, verifier) + const accessToken = credentials.access + const response = await fetch("https://api.anthropic.com/api/oauth/claude_cli/create_api_key", { + method: "POST", + headers: { + Authorization: `Bearer ${accessToken}`, + "Content-Type": "application/x-www-form-urlencoded", + Accept: "application/json, text/plain, */*", + }, }) + if (!response.ok) { + throw new Error("Failed to create API key") + } + const json = await response.json() + await Auth.set("anthropic", { + type: "api", + key: json.raw_key, + }) + + prompts.log.success("Login successful - API key created and saved") + } catch (error) { + prompts.log.error("Invalid code or failed to create API key") + } prompts.outro("Done") return } @@ -186,17 +236,13 @@ export const AuthLoginCommand = cmd({ await new Promise((resolve) => setTimeout(resolve, 10)) const deviceInfo = await copilot.authorize() - prompts.note( - `Please visit: ${deviceInfo.verification}\nEnter code: ${deviceInfo.user}`, - ) + prompts.note(`Please visit: ${deviceInfo.verification}\nEnter code: ${deviceInfo.user}`) const spinner = prompts.spinner() spinner.start("Waiting for authorization...") while (true) { - await new Promise((resolve) => - setTimeout(resolve, deviceInfo.interval * 1000), - ) + await new Promise((resolve) => setTimeout(resolve, deviceInfo.interval * 1000)) const response = await copilot.poll(deviceInfo.device) if (response.status === "pending") continue if (response.status === "success") { @@ -248,12 +294,7 @@ export const AuthLogoutCommand = cmd({ const providerID = await prompts.select({ message: "Select provider", options: credentials.map(([key, value]) => ({ - label: - (database[key]?.name || key) + - UI.Style.TEXT_DIM + - " (" + - value.type + - ")", + label: (database[key]?.name || key) + UI.Style.TEXT_DIM + " (" + value.type + ")", value: key, })), }) diff --git a/packages/opencode/src/cli/cmd/debug/file.ts b/packages/opencode/src/cli/cmd/debug/file.ts index 021c49db..f773dbd9 100644 --- a/packages/opencode/src/cli/cmd/debug/file.ts +++ b/packages/opencode/src/cli/cmd/debug/file.ts @@ -31,7 +31,6 @@ const FileStatusCommand = cmd({ export const FileCommand = cmd({ command: "file", - builder: (yargs) => - yargs.command(FileReadCommand).command(FileStatusCommand).demandCommand(), + builder: (yargs) => yargs.command(FileReadCommand).command(FileStatusCommand).demandCommand(), async handler() {}, }) diff --git a/packages/opencode/src/cli/cmd/debug/index.ts b/packages/opencode/src/cli/cmd/debug/index.ts index e748183e..77f4129a 100644 --- a/packages/opencode/src/cli/cmd/debug/index.ts +++ b/packages/opencode/src/cli/cmd/debug/index.ts @@ -3,6 +3,7 @@ import { cmd } from "../cmd" import { FileCommand } from "./file" import { LSPCommand } from "./lsp" import { RipgrepCommand } from "./ripgrep" +import { ScrapCommand } from "./scrap" import { SnapshotCommand } from "./snapshot" export const DebugCommand = cmd({ @@ -12,14 +13,13 @@ export const DebugCommand = cmd({ .command(LSPCommand) .command(RipgrepCommand) .command(FileCommand) + .command(ScrapCommand) .command(SnapshotCommand) .command({ command: "wait", async handler() { await bootstrap({ cwd: process.cwd() }, async () => { - await new Promise((resolve) => - setTimeout(resolve, 1_000 * 60 * 60 * 24), - ) + await new Promise((resolve) => setTimeout(resolve, 1_000 * 60 * 60 * 24)) }) }, }) diff --git a/packages/opencode/src/cli/cmd/debug/lsp.ts b/packages/opencode/src/cli/cmd/debug/lsp.ts index d596bf6c..ac1bac7c 100644 --- a/packages/opencode/src/cli/cmd/debug/lsp.ts +++ b/packages/opencode/src/cli/cmd/debug/lsp.ts @@ -6,14 +6,13 @@ import { Log } from "../../../util/log" export const LSPCommand = cmd({ command: "lsp", builder: (yargs) => - yargs.command(DiagnosticsCommand).command(SymbolsCommand).demandCommand(), + yargs.command(DiagnosticsCommand).command(SymbolsCommand).command(DocumentSymbolsCommand).demandCommand(), async handler() {}, }) const DiagnosticsCommand = cmd({ command: "diagnostics ", - builder: (yargs) => - yargs.positional("file", { type: "string", demandOption: true }), + builder: (yargs) => yargs.positional("file", { type: "string", demandOption: true }), async handler(args) { await bootstrap({ cwd: process.cwd() }, async () => { await LSP.touchFile(args.file, true) @@ -24,14 +23,24 @@ const DiagnosticsCommand = cmd({ export const SymbolsCommand = cmd({ command: "symbols ", - builder: (yargs) => - yargs.positional("query", { type: "string", demandOption: true }), + builder: (yargs) => yargs.positional("query", { type: "string", demandOption: true }), async handler(args) { await bootstrap({ cwd: process.cwd() }, async () => { - await LSP.touchFile("./src/index.ts", true) using _ = Log.Default.time("symbols") const results = await LSP.workspaceSymbol(args.query) console.log(JSON.stringify(results, null, 2)) }) }, }) + +export const DocumentSymbolsCommand = cmd({ + command: "document-symbols ", + builder: (yargs) => yargs.positional("uri", { type: "string", demandOption: true }), + async handler(args) { + await bootstrap({ cwd: process.cwd() }, async () => { + using _ = Log.Default.time("document-symbols") + const results = await LSP.documentSymbol(args.uri) + console.log(JSON.stringify(results, null, 2)) + }) + }, +}) diff --git a/packages/opencode/src/cli/cmd/debug/ripgrep.ts b/packages/opencode/src/cli/cmd/debug/ripgrep.ts index c7136867..b8005c90 100644 --- a/packages/opencode/src/cli/cmd/debug/ripgrep.ts +++ b/packages/opencode/src/cli/cmd/debug/ripgrep.ts @@ -5,12 +5,7 @@ import { cmd } from "../cmd" export const RipgrepCommand = cmd({ command: "rg", - builder: (yargs) => - yargs - .command(TreeCommand) - .command(FilesCommand) - .command(SearchCommand) - .demandCommand(), + builder: (yargs) => yargs.command(TreeCommand).command(FilesCommand).command(SearchCommand).demandCommand(), async handler() {}, }) @@ -50,7 +45,7 @@ const FilesCommand = cmd({ const files = await Ripgrep.files({ cwd: app.path.cwd, query: args.query, - glob: args.glob, + glob: args.glob ? [args.glob] : undefined, limit: args.limit, }) console.log(files.join("\n")) diff --git a/packages/opencode/src/cli/cmd/debug/scrap.ts b/packages/opencode/src/cli/cmd/debug/scrap.ts new file mode 100644 index 00000000..3ba53e3f --- /dev/null +++ b/packages/opencode/src/cli/cmd/debug/scrap.ts @@ -0,0 +1,7 @@ +import { cmd } from "../cmd" + +export const ScrapCommand = cmd({ + command: "scrap", + builder: (yargs) => yargs, + async handler() {}, +}) diff --git a/packages/opencode/src/cli/cmd/debug/snapshot.ts b/packages/opencode/src/cli/cmd/debug/snapshot.ts index a6d129d5..48d7f91e 100644 --- a/packages/opencode/src/cli/cmd/debug/snapshot.ts +++ b/packages/opencode/src/cli/cmd/debug/snapshot.ts @@ -4,15 +4,11 @@ import { cmd } from "../cmd" export const SnapshotCommand = cmd({ command: "snapshot", - builder: (yargs) => - yargs - .command(SnapshotCreateCommand) - .command(SnapshotRestoreCommand) - .demandCommand(), + builder: (yargs) => yargs.command(CreateCommand).command(RestoreCommand).command(DiffCommand).demandCommand(), async handler() {}, }) -export const SnapshotCreateCommand = cmd({ +const CreateCommand = cmd({ command: "create", async handler() { await bootstrap({ cwd: process.cwd() }, async () => { @@ -22,7 +18,7 @@ export const SnapshotCreateCommand = cmd({ }, }) -export const SnapshotRestoreCommand = cmd({ +const RestoreCommand = cmd({ command: "restore ", builder: (yargs) => yargs.positional("commit", { @@ -37,3 +33,20 @@ export const SnapshotRestoreCommand = cmd({ }) }, }) + +export const DiffCommand = cmd({ + command: "diff ", + describe: "diff", + builder: (yargs) => + yargs.positional("commit", { + type: "string", + description: "commit", + demandOption: true, + }), + async handler(args) { + await bootstrap({ cwd: process.cwd() }, async () => { + const diff = await Snapshot.diff("test", args.commit) + console.log(diff) + }) + }, +}) diff --git a/packages/opencode/src/cli/cmd/generate.ts b/packages/opencode/src/cli/cmd/generate.ts index 0cef1077..d6ed0eb1 100644 --- a/packages/opencode/src/cli/cmd/generate.ts +++ b/packages/opencode/src/cli/cmd/generate.ts @@ -10,9 +10,6 @@ export const GenerateCommand = { const dir = "gen" await fs.rmdir(dir, { recursive: true }).catch(() => {}) await fs.mkdir(dir, { recursive: true }) - await Bun.write( - path.join(dir, "openapi.json"), - JSON.stringify(specs, null, 2), - ) + await Bun.write(path.join(dir, "openapi.json"), JSON.stringify(specs, null, 2)) }, } satisfies CommandModule diff --git a/packages/opencode/src/cli/cmd/install-github.ts b/packages/opencode/src/cli/cmd/install-github.ts new file mode 100644 index 00000000..604d76a4 --- /dev/null +++ b/packages/opencode/src/cli/cmd/install-github.ts @@ -0,0 +1,235 @@ +import { $ } from "bun" +import path from "path" +import { exec } from "child_process" +import * as prompts from "@clack/prompts" +import { map, pipe, sortBy, values } from "remeda" +import { UI } from "../ui" +import { cmd } from "./cmd" +import { ModelsDev } from "../../provider/models" +import { App } from "../../app/app" + +const WORKFLOW_FILE = ".github/workflows/opencode.yml" + +export const InstallGithubCommand = cmd({ + command: "install-github", + describe: "install the GitHub agent", + async handler() { + await App.provide({ cwd: process.cwd() }, async () => { + UI.empty() + prompts.intro("Install GitHub agent") + const app = await getAppInfo() + await installGitHubApp() + + const providers = await ModelsDev.get() + const provider = await promptProvider() + const model = await promptModel() + //const key = await promptKey() + + await addWorkflowFiles() + printNextSteps() + + function printNextSteps() { + let step2 + if (provider === "amazon-bedrock") { + step2 = + "Configure OIDC in AWS - https://docs.github.com/en/actions/how-tos/security-for-github-actions/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services" + } else { + const url = `https://github.com/organizations/${app.owner}/settings/secrets/actions` + const env = providers[provider].env + const envStr = + env.length === 1 + ? `\`${env[0]}\` secret` + : `\`${[env.slice(0, -1).join("\`, \`"), ...env.slice(-1)].join("\` and \`")}\` secrets` + step2 = `Add ${envStr} for ${providers[provider].name} - ${url}` + } + + prompts.outro( + [ + "Next steps:", + ` 1. Commit "${WORKFLOW_FILE}" file and push`, + ` 2. ${step2}`, + " 3. Learn how to use the GitHub agent - https://docs.opencode.ai/docs/github/getting-started", + ].join("\n"), + ) + } + + async function getAppInfo() { + const app = App.info() + if (!app.git) { + prompts.log.error(`Could not find git repository. Please run this command from a git repository.`) + throw new UI.CancelledError() + } + + // Get repo info + const info = await $`git remote get-url origin`.quiet().nothrow().text() + // match https or git pattern + // ie. https://github.com/sst/opencode.git + // ie. git@github.com:sst/opencode.git + const parsed = info.match(/git@github\.com:(.*)\.git/) ?? info.match(/github\.com\/(.*)\.git/) + if (!parsed) { + prompts.log.error(`Could not find git repository. Please run this command from a git repository.`) + throw new UI.CancelledError() + } + const [owner, repo] = parsed[1].split("/") + return { owner, repo, root: app.path.root } + } + + async function promptProvider() { + const priority: Record = { + anthropic: 0, + "github-copilot": 1, + openai: 2, + google: 3, + } + let provider = await prompts.select({ + message: "Select provider", + maxItems: 8, + options: [ + ...pipe( + providers, + values(), + sortBy( + (x) => priority[x.id] ?? 99, + (x) => x.name ?? x.id, + ), + map((x) => ({ + label: x.name, + value: x.id, + hint: priority[x.id] === 0 ? "recommended" : undefined, + })), + ), + { + value: "other", + label: "Other", + }, + ], + }) + + if (prompts.isCancel(provider)) throw new UI.CancelledError() + if (provider === "other") { + provider = await prompts.text({ + message: "Enter provider id", + validate: (x) => (x.match(/^[a-z-]+$/) ? undefined : "a-z and hyphens only"), + }) + if (prompts.isCancel(provider)) throw new UI.CancelledError() + provider = provider.replace(/^@ai-sdk\//, "") + if (prompts.isCancel(provider)) throw new UI.CancelledError() + prompts.log.warn( + `This only stores a credential for ${provider} - you will need configure it in opencode.json, check the docs for examples.`, + ) + } + + return provider + } + + async function promptModel() { + const providerData = providers[provider]! + + const model = await prompts.select({ + message: "Select model", + maxItems: 8, + options: pipe( + providerData.models, + values(), + sortBy((x) => x.name ?? x.id), + map((x) => ({ + label: x.name ?? x.id, + value: x.id, + })), + ), + }) + + if (prompts.isCancel(model)) throw new UI.CancelledError() + return model + } + + async function installGitHubApp() { + const s = prompts.spinner() + s.start("Installing GitHub app") + + // Get installation + const installation = await getInstallation() + if (installation) return s.stop("GitHub app already installed") + + // Open browser + const url = "https://github.com/apps/opencode-agent" + const command = + process.platform === "darwin" + ? `open "${url}"` + : process.platform === "win32" + ? `start "${url}"` + : `xdg-open "${url}"` + + exec(command, (error) => { + if (error) { + prompts.log.warn(`Could not open browser. Please visit: ${url}`) + } + }) + + // Wait for installation + s.message("Waiting for GitHub app to be installed") + const MAX_RETRIES = 60 + let retries = 0 + do { + const installation = await getInstallation() + if (installation) break + + if (retries > MAX_RETRIES) { + s.stop( + `Failed to detect GitHub app installation. Make sure to install the app for the \`${app.owner}/${app.repo}\` repository.`, + ) + throw new UI.CancelledError() + } + + retries++ + await new Promise((resolve) => setTimeout(resolve, 1000)) + } while (true) + + s.stop("Installed GitHub app") + + async function getInstallation() { + return await fetch(`https://api.opencode.ai/get_github_app_installation?owner=${app.owner}&repo=${app.repo}`) + .then((res) => res.json()) + .then((data) => data.installation) + } + } + + async function addWorkflowFiles() { + const envStr = + provider === "amazon-bedrock" + ? "" + : `\n env:${providers[provider].env.map((e) => `\n ${e}: \${{ secrets.${e} }}`).join("")}` + + await Bun.write( + path.join(app.root, WORKFLOW_FILE), + ` +name: opencode + +on: + issue_comment: + types: [created] + +jobs: + opencode: + if: startsWith(github.event.comment.body, 'hey opencode') + runs-on: ubuntu-latest + permissions: + id-token: write + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 1 + + - name: Run opencode + uses: sst/opencode/sdks/github@github-v1${envStr} + with: + model: ${provider}/${model} +`.trim(), + ) + + prompts.log.success(`Added workflow file: "${WORKFLOW_FILE}"`) + } + }) + }, +}) diff --git a/packages/opencode/src/cli/cmd/mcp.ts b/packages/opencode/src/cli/cmd/mcp.ts new file mode 100644 index 00000000..5f8b6e5d --- /dev/null +++ b/packages/opencode/src/cli/cmd/mcp.ts @@ -0,0 +1,79 @@ +import { cmd } from "./cmd" +import { Client } from "@modelcontextprotocol/sdk/client/index.js" +import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js" +import * as prompts from "@clack/prompts" +import { UI } from "../ui" + +export const McpCommand = cmd({ + command: "mcp", + builder: (yargs) => yargs.command(McpAddCommand).demandCommand(), + async handler() {}, +}) + +export const McpAddCommand = cmd({ + command: "add", + describe: "add an MCP server", + async handler() { + UI.empty() + prompts.intro("Add MCP server") + + const name = await prompts.text({ + message: "Enter MCP server name", + validate: (x) => (x.length > 0 ? undefined : "Required"), + }) + if (prompts.isCancel(name)) throw new UI.CancelledError() + + const type = await prompts.select({ + message: "Select MCP server type", + options: [ + { + label: "Local", + value: "local", + hint: "Run a local command", + }, + { + label: "Remote", + value: "remote", + hint: "Connect to a remote URL", + }, + ], + }) + if (prompts.isCancel(type)) throw new UI.CancelledError() + + if (type === "local") { + const command = await prompts.text({ + message: "Enter command to run", + placeholder: "e.g., opencode x @modelcontextprotocol/server-filesystem", + validate: (x) => (x.length > 0 ? undefined : "Required"), + }) + if (prompts.isCancel(command)) throw new UI.CancelledError() + + prompts.log.info(`Local MCP server "${name}" configured with command: ${command}`) + prompts.outro("MCP server added successfully") + return + } + + if (type === "remote") { + const url = await prompts.text({ + message: "Enter MCP server URL", + placeholder: "e.g., https://example.com/mcp", + validate: (x) => { + if (x.length === 0) return "Required" + const isValid = URL.canParse(x) + return isValid ? undefined : "Invalid URL" + }, + }) + if (prompts.isCancel(url)) throw new UI.CancelledError() + + const client = new Client({ + name: "opencode", + version: "1.0.0", + }) + const transport = new StreamableHTTPClientTransport(new URL(url)) + await client.connect(transport) + prompts.log.info(`Remote MCP server "${name}" configured with URL: ${url}`) + } + + prompts.outro("MCP server added successfully") + }, +}) diff --git a/packages/opencode/src/cli/cmd/run.ts b/packages/opencode/src/cli/cmd/run.ts index 1905aa17..63e57b21 100644 --- a/packages/opencode/src/cli/cmd/run.ts +++ b/packages/opencode/src/cli/cmd/run.ts @@ -2,12 +2,14 @@ import type { Argv } from "yargs" import { Bus } from "../../bus" import { Provider } from "../../provider/provider" import { Session } from "../../session" -import { Message } from "../../session/message" import { UI } from "../ui" import { cmd } from "./cmd" import { Flag } from "../../flag/flag" import { Config } from "../../config/config" import { bootstrap } from "../bootstrap" +import { MessageV2 } from "../../session/message-v2" +import { Mode } from "../../session/mode" +import { Identifier } from "../../id/id" const TOOL: Record = { todowrite: ["Todo", UI.Style.TEXT_WARNING_BOLD], @@ -52,13 +54,22 @@ export const RunCommand = cmd({ alias: ["m"], describe: "model to use in the format of provider/model", }) + .option("mode", { + type: "string", + describe: "mode to use", + }) }, handler: async (args) => { - const message = args.message.join(" ") + let message = args.message.join(" ") + + if (!process.stdin.isTTY) message += "\n" + (await Bun.stdin.text()) + await bootstrap({ cwd: process.cwd() }, async () => { const session = await (async () => { if (args.continue) { - const first = await Session.list().next() + const list = Session.list() + const first = await list.next() + await list.return() if (first.done) return return first.value } @@ -73,32 +84,27 @@ export const RunCommand = cmd({ return } - const isPiped = !process.stdout.isTTY - UI.empty() UI.println(UI.logo()) UI.empty() - UI.println(UI.Style.TEXT_NORMAL_BOLD + "> ", message) - UI.empty() const cfg = await Config.get() - if (cfg.autoshare || Flag.OPENCODE_AUTO_SHARE || args.share) { - await Session.share(session.id) - UI.println( - UI.Style.TEXT_INFO_BOLD + - "~ https://opencode.ai/s/" + - session.id.slice(-8), - ) + if (cfg.share === "auto" || Flag.OPENCODE_AUTO_SHARE || args.share) { + try { + await Session.share(session.id) + UI.println(UI.Style.TEXT_INFO_BOLD + "~ https://opencode.ai/s/" + session.id.slice(-8)) + } catch (error) { + if (error instanceof Error && error.message.includes("disabled")) { + UI.println(UI.Style.TEXT_DANGER_BOLD + "! " + error.message) + } else { + throw error + } + } } UI.empty() - const { providerID, modelID } = args.model - ? Provider.parseModel(args.model) - : await Provider.defaultModel() - UI.println( - UI.Style.TEXT_NORMAL_BOLD + "@ ", - UI.Style.TEXT_NORMAL + `${providerID}/${modelID}`, - ) + const { providerID, modelID } = args.model ? Provider.parseModel(args.model) : await Provider.defaultModel() + UI.println(UI.Style.TEXT_NORMAL_BOLD + "@ ", UI.Style.TEXT_NORMAL + `${providerID}/${modelID}`) UI.empty() function printEvent(color: string, type: string, title: string) { @@ -110,52 +116,75 @@ export const RunCommand = cmd({ ) } - Bus.subscribe(Message.Event.PartUpdated, async (evt) => { - if (evt.properties.sessionID !== session.id) return + let text = "" + Bus.subscribe(MessageV2.Event.PartUpdated, async (evt) => { + if (evt.properties.part.sessionID !== session.id) return + if (evt.properties.part.messageID === messageID) return const part = evt.properties.part - const message = await Session.getMessage( - evt.properties.sessionID, - evt.properties.messageID, - ) - if ( - part.type === "tool-invocation" && - part.toolInvocation.state === "result" - ) { - const metadata = message.metadata.tool[part.toolInvocation.toolCallId] - const [tool, color] = TOOL[part.toolInvocation.toolName] ?? [ - part.toolInvocation.toolName, - UI.Style.TEXT_INFO_BOLD, - ] - printEvent(color, tool, metadata?.title || "Unknown") + if (part.type === "tool" && part.state.status === "completed") { + const [tool, color] = TOOL[part.tool] ?? [part.tool, UI.Style.TEXT_INFO_BOLD] + const title = + part.state.title || Object.keys(part.state.input).length > 0 ? JSON.stringify(part.state.input) : "Unknown" + printEvent(color, tool, title) } if (part.type === "text") { - if (part.text.includes("\n")) { + text = part.text + + if (part.time?.end) { UI.empty() - UI.println(part.text) + UI.println(UI.markdown(text)) UI.empty() + text = "" return } - printEvent(UI.Style.TEXT_NORMAL_BOLD, "Text", part.text) } }) + let errorMsg: string | undefined + Bus.subscribe(Session.Event.Error, async (evt) => { + const { sessionID, error } = evt.properties + if (sessionID !== session.id || !error) return + let err = String(error.name) + + if ("data" in error && error.data && "message" in error.data) { + err = error.data.message + } + errorMsg = errorMsg ? errorMsg + "\n" + err : err + + UI.error(err) + }) + + const mode = args.mode ? await Mode.get(args.mode) : await Mode.list().then((x) => x[0]) + + const messageID = Identifier.ascending("message") const result = await Session.chat({ sessionID: session.id, - providerID, - modelID, + messageID, + ...(mode.model + ? mode.model + : { + providerID, + modelID, + }), + mode: mode.name, parts: [ { + id: Identifier.ascending("part"), + sessionID: session.id, + messageID: messageID, type: "text", text: message, }, ], }) + const isPiped = !process.stdout.isTTY if (isPiped) { const match = result.parts.findLast((x) => x.type === "text") - if (match) process.stdout.write(match.text) + if (match) process.stdout.write(UI.markdown(match.text)) + if (errorMsg) process.stdout.write(errorMsg) } UI.empty() }) diff --git a/packages/opencode/src/cli/cmd/serve.ts b/packages/opencode/src/cli/cmd/serve.ts index f3686f30..6dab38f1 100644 --- a/packages/opencode/src/cli/cmd/serve.ts +++ b/packages/opencode/src/cli/cmd/serve.ts @@ -38,9 +38,7 @@ export const ServeCommand = cmd({ hostname, }) - console.log( - `opencode server listening on http://${server.hostname}:${server.port}`, - ) + console.log(`opencode server listening on http://${server.hostname}:${server.port}`) await new Promise(() => {}) diff --git a/packages/opencode/src/cli/cmd/stats.ts b/packages/opencode/src/cli/cmd/stats.ts new file mode 100644 index 00000000..39ae86ba --- /dev/null +++ b/packages/opencode/src/cli/cmd/stats.ts @@ -0,0 +1,98 @@ +import { cmd } from "./cmd" + +interface SessionStats { + totalSessions: number + totalMessages: number + totalCost: number + totalTokens: { + input: number + output: number + reasoning: number + cache: { + read: number + write: number + } + } + toolUsage: Record + dateRange: { + earliest: number + latest: number + } + days: number + costPerDay: number +} + +export const StatsCommand = cmd({ + command: "stats", + handler: async () => {}, +}) + +export function displayStats(stats: SessionStats) { + const width = 56 + + function renderRow(label: string, value: string): string { + const availableWidth = width - 1 + const paddingNeeded = availableWidth - label.length - value.length + const padding = Math.max(0, paddingNeeded) + return `│${label}${" ".repeat(padding)}${value} │` + } + + // Overview section + console.log("┌────────────────────────────────────────────────────────┐") + console.log("│ OVERVIEW │") + console.log("├────────────────────────────────────────────────────────┤") + console.log(renderRow("Sessions", stats.totalSessions.toLocaleString())) + console.log(renderRow("Messages", stats.totalMessages.toLocaleString())) + console.log(renderRow("Days", stats.days.toString())) + console.log("└────────────────────────────────────────────────────────┘") + console.log() + + // Cost & Tokens section + console.log("┌────────────────────────────────────────────────────────┐") + console.log("│ COST & TOKENS │") + console.log("├────────────────────────────────────────────────────────┤") + const cost = isNaN(stats.totalCost) ? 0 : stats.totalCost + const costPerDay = isNaN(stats.costPerDay) ? 0 : stats.costPerDay + console.log(renderRow("Total Cost", `$${cost.toFixed(2)}`)) + console.log(renderRow("Cost/Day", `$${costPerDay.toFixed(2)}`)) + console.log(renderRow("Input", formatNumber(stats.totalTokens.input))) + console.log(renderRow("Output", formatNumber(stats.totalTokens.output))) + console.log(renderRow("Cache Read", formatNumber(stats.totalTokens.cache.read))) + console.log(renderRow("Cache Write", formatNumber(stats.totalTokens.cache.write))) + console.log("└────────────────────────────────────────────────────────┘") + console.log() + + // Tool Usage section + if (Object.keys(stats.toolUsage).length > 0) { + const sortedTools = Object.entries(stats.toolUsage) + .sort(([, a], [, b]) => b - a) + .slice(0, 10) + + console.log("┌────────────────────────────────────────────────────────┐") + console.log("│ TOOL USAGE │") + console.log("├────────────────────────────────────────────────────────┤") + + const maxCount = Math.max(...sortedTools.map(([, count]) => count)) + const totalToolUsage = Object.values(stats.toolUsage).reduce((a, b) => a + b, 0) + + for (const [tool, count] of sortedTools) { + const barLength = Math.max(1, Math.floor((count / maxCount) * 20)) + const bar = "█".repeat(barLength) + const percentage = ((count / totalToolUsage) * 100).toFixed(1) + + const content = ` ${tool.padEnd(10)} ${bar.padEnd(20)} ${count.toString().padStart(3)} (${percentage.padStart(4)}%)` + const padding = Math.max(0, width - content.length) + console.log(`│${content}${" ".repeat(padding)} │`) + } + console.log("└────────────────────────────────────────────────────────┘") + } + console.log() +} +function formatNumber(num: number): string { + if (num >= 1000000) { + return (num / 1000000).toFixed(1) + "M" + } else if (num >= 1000) { + return (num / 1000).toFixed(1) + "K" + } + return num.toString() +} diff --git a/packages/opencode/src/cli/cmd/tui.ts b/packages/opencode/src/cli/cmd/tui.ts index 5cdda83c..aa49a856 100644 --- a/packages/opencode/src/cli/cmd/tui.ts +++ b/packages/opencode/src/cli/cmd/tui.ts @@ -10,15 +10,32 @@ import { Installation } from "../../installation" import { Config } from "../../config/config" import { Bus } from "../../bus" import { Log } from "../../util/log" +import { FileWatcher } from "../../file/watch" +import { Mode } from "../../session/mode" export const TuiCommand = cmd({ command: "$0 [project]", describe: "start opencode tui", builder: (yargs) => - yargs.positional("project", { - type: "string", - describe: "path to start opencode in", - }), + yargs + .positional("project", { + type: "string", + describe: "path to start opencode in", + }) + .option("model", { + type: "string", + alias: ["m"], + describe: "model to use in the format of provider/model", + }) + .option("prompt", { + alias: ["p"], + type: "string", + describe: "prompt to use", + }) + .option("mode", { + type: "string", + describe: "mode to use", + }), handler: async (args) => { while (true) { const cwd = args.project ? path.resolve(args.project) : process.cwd() @@ -29,6 +46,7 @@ export const TuiCommand = cmd({ return } const result = await bootstrap({ cwd }, async (app) => { + FileWatcher.init() const providers = await Provider.list() if (Object.keys(providers).length === 0) { return "needs_provider" @@ -40,9 +58,7 @@ export const TuiCommand = cmd({ }) let cmd = ["go", "run", "./main.go"] - let cwd = Bun.fileURLToPath( - new URL("../../../../tui/cmd/opencode", import.meta.url), - ) + let cwd = Bun.fileURLToPath(new URL("../../../../tui/cmd/opencode", import.meta.url)) if (Bun.embeddedFiles.length > 0) { const blob = Bun.embeddedFiles[0] as File let binaryName = blob.name @@ -62,15 +78,22 @@ export const TuiCommand = cmd({ cmd, }) const proc = Bun.spawn({ - cmd: [...cmd, ...process.argv.slice(2)], + cmd: [ + ...cmd, + ...(args.model ? ["--model", args.model] : []), + ...(args.prompt ? ["--prompt", args.prompt] : []), + ...(args.mode ? ["--mode", args.mode] : []), + ], cwd, stdout: "inherit", stderr: "inherit", stdin: "inherit", env: { ...process.env, + CGO_ENABLED: "0", OPENCODE_SERVER: server.url.toString(), OPENCODE_APP_INFO: JSON.stringify(app), + OPENCODE_MODES: JSON.stringify(await Mode.list()), }, onExit: () => { server.stop() diff --git a/packages/opencode/src/cli/cmd/upgrade.ts b/packages/opencode/src/cli/cmd/upgrade.ts index 759ab5ae..17d18168 100644 --- a/packages/opencode/src/cli/cmd/upgrade.ts +++ b/packages/opencode/src/cli/cmd/upgrade.ts @@ -27,22 +27,26 @@ export const UpgradeCommand = { const detectedMethod = await Installation.method() const method = (args.method as Installation.Method) ?? detectedMethod if (method === "unknown") { - prompts.log.error( - `opencode is installed to ${process.execPath} and seems to be managed by a package manager`, - ) + prompts.log.error(`opencode is installed to ${process.execPath} and seems to be managed by a package manager`) prompts.outro("Done") return } prompts.log.info("Using method: " + method) const target = args.target ?? (await Installation.latest()) + + if (Installation.VERSION === target) { + prompts.log.warn(`opencode upgrade skipped: ${target} is already installed`) + prompts.outro("Done") + return + } + prompts.log.info(`From ${Installation.VERSION} → ${target}`) const spinner = prompts.spinner() spinner.start("Upgrading...") const err = await Installation.upgrade(method, target).catch((err) => err) if (err) { spinner.stop("Upgrade failed") - if (err instanceof Installation.UpgradeFailedError) - prompts.log.error(err.data.stderr) + if (err instanceof Installation.UpgradeFailedError) prompts.log.error(err.data.stderr) else if (err instanceof Error) prompts.log.error(err.message) prompts.outro("Done") return diff --git a/packages/opencode/src/cli/error.ts b/packages/opencode/src/cli/error.ts index 752ad696..261206a1 100644 --- a/packages/opencode/src/cli/error.ts +++ b/packages/opencode/src/cli/error.ts @@ -5,14 +5,11 @@ import { UI } from "./ui" export function FormatError(input: unknown) { if (MCP.Failed.isInstance(input)) return `MCP server "${input.data.name}" failed. Note, opencode does not support MCP authentication yet.` - if (Config.JsonError.isInstance(input)) - return `Config file at ${input.data.path} is not valid JSON` + if (Config.JsonError.isInstance(input)) return `Config file at ${input.data.path} is not valid JSON` if (Config.InvalidError.isInstance(input)) return [ `Config file at ${input.data.path} is invalid`, - ...(input.data.issues?.map( - (issue) => "↳ " + issue.message + " " + issue.path.join("."), - ) ?? []), + ...(input.data.issues?.map((issue) => "↳ " + issue.message + " " + issue.path.join(".")) ?? []), ].join("\n") if (UI.CancelledError.isInstance(input)) return "" diff --git a/packages/opencode/src/cli/ui.ts b/packages/opencode/src/cli/ui.ts index 9801b459..0fa4d1ce 100644 --- a/packages/opencode/src/cli/ui.ts +++ b/packages/opencode/src/cli/ui.ts @@ -76,4 +76,8 @@ export namespace UI { export function error(message: string) { println(Style.TEXT_DANGER_BOLD + "Error: " + Style.TEXT_NORMAL + message) } + + export function markdown(text: string): string { + return text + } } diff --git a/packages/opencode/src/config/config.ts b/packages/opencode/src/config/config.ts index eb67778e..5086e5bb 100644 --- a/packages/opencode/src/config/config.ts +++ b/packages/opencode/src/config/config.ts @@ -4,7 +4,7 @@ import { z } from "zod" import { App } from "../app/app" import { Filesystem } from "../util/filesystem" import { ModelsDev } from "../provider/models" -import { mergeDeep } from "remeda" +import { mergeDeep, pipe } from "remeda" import { Global } from "../global" import fs from "fs/promises" import { lazy } from "../util/lazy" @@ -21,6 +21,20 @@ export namespace Config { result = mergeDeep(result, await load(resolved)) } } + + // Handle migration from autoshare to share field + if (result.autoshare === true && !result.share) { + result.share = "auto" + } + + if (!result.username) { + const os = await import("os") + result.username = os.userInfo().username + } + if (!result.layout) { + result.layout = "auto" + } + log.info("loaded", result) return result @@ -29,18 +43,12 @@ export namespace Config { export const McpLocal = z .object({ type: z.literal("local").describe("Type of MCP server connection"), - command: z - .string() - .array() - .describe("Command and arguments to run the MCP server"), + command: z.string().array().describe("Command and arguments to run the MCP server"), environment: z .record(z.string(), z.string()) .optional() .describe("Environment variables to set when running the MCP server"), - enabled: z - .boolean() - .optional() - .describe("Enable or disable the MCP server on startup"), + enabled: z.boolean().optional().describe("Enable or disable the MCP server on startup"), }) .strict() .openapi({ @@ -51,10 +59,8 @@ export namespace Config { .object({ type: z.literal("remote").describe("Type of MCP server connection"), url: z.string().describe("URL of the remote MCP server"), - enabled: z - .boolean() - .optional() - .describe("Enable or disable the MCP server on startup"), + enabled: z.boolean().optional().describe("Enable or disable the MCP server on startup"), + headers: z.record(z.string(), z.string()).optional().describe("Headers to send with the request"), }) .strict() .openapi({ @@ -64,105 +70,101 @@ export namespace Config { export const Mcp = z.discriminatedUnion("type", [McpLocal, McpRemote]) export type Mcp = z.infer + export const Mode = z + .object({ + model: z.string().optional(), + prompt: z.string().optional(), + tools: z.record(z.string(), z.boolean()).optional(), + }) + .openapi({ + ref: "ModeConfig", + }) + export type Mode = z.infer + export const Keybinds = z .object({ - leader: z - .string() - .optional() - .describe("Leader key for keybind combinations"), - help: z.string().optional().describe("Show help dialog"), - editor_open: z.string().optional().describe("Open external editor"), - session_new: z.string().optional().describe("Create a new session"), - session_list: z.string().optional().describe("List all sessions"), - session_share: z.string().optional().describe("Share current session"), - session_interrupt: z - .string() - .optional() - .describe("Interrupt current session"), - session_compact: z - .string() - .optional() - .describe("Toggle compact mode for session"), - tool_details: z.string().optional().describe("Show tool details"), - model_list: z.string().optional().describe("List available models"), - theme_list: z.string().optional().describe("List available themes"), - project_init: z - .string() - .optional() - .describe("Initialize project configuration"), - input_clear: z.string().optional().describe("Clear input field"), - input_paste: z.string().optional().describe("Paste from clipboard"), - input_submit: z.string().optional().describe("Submit input"), - input_newline: z.string().optional().describe("Insert newline in input"), - history_previous: z - .string() - .optional() - .describe("Navigate to previous history item"), - history_next: z - .string() - .optional() - .describe("Navigate to next history item"), - messages_page_up: z - .string() - .optional() - .describe("Scroll messages up by one page"), - messages_page_down: z - .string() - .optional() - .describe("Scroll messages down by one page"), - messages_half_page_up: z - .string() - .optional() - .describe("Scroll messages up by half page"), + leader: z.string().optional().default("ctrl+x").describe("Leader key for keybind combinations"), + app_help: z.string().optional().default("h").describe("Show help dialog"), + switch_mode: z.string().optional().default("tab").describe("Next mode"), + switch_mode_reverse: z.string().optional().default("shift+tab").describe("Previous Mode"), + editor_open: z.string().optional().default("e").describe("Open external editor"), + session_export: z.string().optional().default("x").describe("Export session to editor"), + session_new: z.string().optional().default("n").describe("Create a new session"), + session_list: z.string().optional().default("l").describe("List all sessions"), + session_share: z.string().optional().default("s").describe("Share current session"), + session_unshare: z.string().optional().default("u").describe("Unshare current session"), + session_interrupt: z.string().optional().default("esc").describe("Interrupt current session"), + session_compact: z.string().optional().default("c").describe("Compact the session"), + tool_details: z.string().optional().default("d").describe("Toggle tool details"), + model_list: z.string().optional().default("m").describe("List available models"), + theme_list: z.string().optional().default("t").describe("List available themes"), + file_list: z.string().optional().default("f").describe("List files"), + file_close: z.string().optional().default("esc").describe("Close file"), + file_search: z.string().optional().default("/").describe("Search file"), + file_diff_toggle: z.string().optional().default("v").describe("Split/unified diff"), + project_init: z.string().optional().default("i").describe("Create/update AGENTS.md"), + input_clear: z.string().optional().default("ctrl+c").describe("Clear input field"), + input_paste: z.string().optional().default("ctrl+v").describe("Paste from clipboard"), + input_submit: z.string().optional().default("enter").describe("Submit input"), + input_newline: z.string().optional().default("shift+enter,ctrl+j").describe("Insert newline in input"), + messages_page_up: z.string().optional().default("pgup").describe("Scroll messages up by one page"), + messages_page_down: z.string().optional().default("pgdown").describe("Scroll messages down by one page"), + messages_half_page_up: z.string().optional().default("ctrl+alt+u").describe("Scroll messages up by half page"), messages_half_page_down: z .string() .optional() + .default("ctrl+alt+d") .describe("Scroll messages down by half page"), - messages_previous: z - .string() - .optional() - .describe("Navigate to previous message"), - messages_next: z.string().optional().describe("Navigate to next message"), - messages_first: z - .string() - .optional() - .describe("Navigate to first message"), - messages_last: z.string().optional().describe("Navigate to last message"), - app_exit: z.string().optional().describe("Exit the application"), + messages_previous: z.string().optional().default("ctrl+up").describe("Navigate to previous message"), + messages_next: z.string().optional().default("ctrl+down").describe("Navigate to next message"), + messages_first: z.string().optional().default("ctrl+g").describe("Navigate to first message"), + messages_last: z.string().optional().default("ctrl+alt+g").describe("Navigate to last message"), + messages_layout_toggle: z.string().optional().default("p").describe("Toggle layout"), + messages_copy: z.string().optional().default("y").describe("Copy message"), + messages_revert: z.string().optional().default("r").describe("Revert message"), + app_exit: z.string().optional().default("ctrl+c,q").describe("Exit the application"), }) .strict() .openapi({ ref: "KeybindsConfig", }) + + export const Layout = z.enum(["auto", "stretch"]).openapi({ + ref: "LayoutConfig", + }) + export type Layout = z.infer + export const Info = z .object({ - $schema: z - .string() - .optional() - .describe("JSON schema reference for configuration validation"), - theme: z - .string() - .optional() - .describe("Theme name to use for the interface"), + $schema: z.string().optional().describe("JSON schema reference for configuration validation"), + theme: z.string().optional().describe("Theme name to use for the interface"), keybinds: Keybinds.optional().describe("Custom keybind configurations"), + share: z + .enum(["manual", "auto", "disabled"]) + .optional() + .describe( + "Control sharing behavior:'manual' allows manual sharing via commands, 'auto' enables automatic sharing, 'disabled' disables all sharing", + ), autoshare: z .boolean() .optional() - .describe("Share newly created sessions automatically"), - autoupdate: z - .boolean() - .optional() - .describe("Automatically update to the latest version"), - disabled_providers: z - .array(z.string()) - .optional() - .describe("Disable providers that are loaded automatically"), - model: z + .describe("@deprecated Use 'share' field instead. Share newly created sessions automatically"), + autoupdate: z.boolean().optional().describe("Automatically update to the latest version"), + disabled_providers: z.array(z.string()).optional().describe("Disable providers that are loaded automatically"), + model: z.string().describe("Model to use in the format of provider/model, eg anthropic/claude-2").optional(), + username: z .string() - .describe( - "Model to use in the format of provider/model, eg anthropic/claude-2", - ) - .optional(), + .optional() + .describe("Custom username to display in conversations instead of system username"), + mode: z + .object({ + build: Mode.optional(), + plan: Mode.optional(), + }) + .catchall(Mode) + .optional() + .describe("Modes configuration, see https://opencode.ai/docs/modes"), + log_level: Log.Level.optional().describe("Minimum log level to write to log files"), provider: z .record( ModelsDev.Provider.partial().extend({ @@ -172,14 +174,9 @@ export namespace Config { ) .optional() .describe("Custom provider configurations and model overrides"), - mcp: z - .record(z.string(), Mcp) - .optional() - .describe("MCP (Model Context Protocol) server configurations"), - instructions: z - .array(z.string()) - .optional() - .describe("Additional instruction files or patterns to include"), + mcp: z.record(z.string(), Mcp).optional().describe("MCP (Model Context Protocol) server configurations"), + instructions: z.array(z.string()).optional().describe("Additional instruction files or patterns to include"), + layout: Layout.optional().describe("Layout to use for the TUI"), experimental: z .object({ hook: z @@ -215,7 +212,11 @@ export namespace Config { export type Info = z.output export const global = lazy(async () => { - let result = await load(path.join(Global.Path.config, "config.json")) + let result = pipe( + {}, + mergeDeep(await load(path.join(Global.Path.config, "config.json"))), + mergeDeep(await load(path.join(Global.Path.config, "opencode.json"))), + ) await import(path.join(Global.Path.config, "config"), { with: { @@ -227,10 +228,7 @@ export namespace Config { if (provider && model) result.model = `${provider}/${model}` result["$schema"] = "https://opencode.ai/config.json" result = mergeDeep(result, rest) - await Bun.write( - path.join(Global.Path.config, "config.json"), - JSON.stringify(result, null, 2), - ) + await Bun.write(path.join(Global.Path.config, "config.json"), JSON.stringify(result, null, 2)) await fs.unlink(path.join(Global.Path.config, "config")) }) .catch(() => {}) @@ -238,19 +236,47 @@ export namespace Config { return result }) - async function load(path: string) { - const data = await Bun.file(path) - .json() + async function load(configPath: string) { + let text = await Bun.file(configPath) + .text() .catch((err) => { - if (err.code === "ENOENT") return {} - throw new JsonError({ path }, { cause: err }) + if (err.code === "ENOENT") return + throw new JsonError({ path: configPath }, { cause: err }) }) + if (!text) return {} + + text = text.replace(/\{env:([^}]+)\}/g, (_, varName) => { + return process.env[varName] || "" + }) + + const fileMatches = text.match(/"?\{file:([^}]+)\}"?/g) + if (fileMatches) { + const configDir = path.dirname(configPath) + for (const match of fileMatches) { + const filePath = match.replace(/^"?\{file:/, "").replace(/\}"?$/, "") + const resolvedPath = path.isAbsolute(filePath) ? filePath : path.resolve(configDir, filePath) + const fileContent = await Bun.file(resolvedPath).text() + text = text.replace(match, JSON.stringify(fileContent)) + } + } + + let data: any + try { + data = JSON.parse(text) + } catch (err) { + throw new JsonError({ path: configPath }, { cause: err as Error }) + } const parsed = Info.safeParse(data) - if (parsed.success) return parsed.data - throw new InvalidError({ path, issues: parsed.error.issues }) + if (parsed.success) { + if (!parsed.data.$schema) { + parsed.data.$schema = "https://opencode.ai/config.json" + await Bun.write(configPath, JSON.stringify(parsed.data, null, 2)) + } + return parsed.data + } + throw new InvalidError({ path: configPath, issues: parsed.error.issues }) } - export const JsonError = NamedError.create( "ConfigJsonError", z.object({ diff --git a/packages/opencode/src/config/hooks.ts b/packages/opencode/src/config/hooks.ts index ffa2475f..973575b7 100644 --- a/packages/opencode/src/config/hooks.ts +++ b/packages/opencode/src/config/hooks.ts @@ -22,9 +22,7 @@ export namespace ConfigHooks { command: item.command, }) Bun.spawn({ - cmd: item.command.map((x) => - x.replace("$FILE", payload.properties.file), - ), + cmd: item.command.map((x) => x.replace("$FILE", payload.properties.file)), env: item.environment, cwd: app.path.cwd, stdout: "ignore", diff --git a/packages/opencode/src/file/fzf.ts b/packages/opencode/src/file/fzf.ts index 6d52702f..1376af8c 100644 --- a/packages/opencode/src/file/fzf.ts +++ b/packages/opencode/src/file/fzf.ts @@ -45,10 +45,7 @@ export namespace Fzf { log.info("found", { filepath }) return { filepath } } - filepath = path.join( - Global.Path.bin, - "fzf" + (process.platform === "win32" ? ".exe" : ""), - ) + filepath = path.join(Global.Path.bin, "fzf" + (process.platform === "win32" ? ".exe" : "")) const file = Bun.file(filepath) if (!(await file.exists())) { @@ -56,18 +53,15 @@ export namespace Fzf { const arch = archMap[process.arch as keyof typeof archMap] ?? "amd64" const config = PLATFORM[process.platform as keyof typeof PLATFORM] - if (!config) - throw new UnsupportedPlatformError({ platform: process.platform }) + if (!config) throw new UnsupportedPlatformError({ platform: process.platform }) const version = VERSION - const platformName = - process.platform === "win32" ? "windows" : process.platform + const platformName = process.platform === "win32" ? "windows" : process.platform const filename = `fzf-${version}-${platformName}_${arch}.${config.extension}` const url = `https://github.com/junegunn/fzf/releases/download/v${version}/${filename}` const response = await fetch(url) - if (!response.ok) - throw new DownloadFailedError({ url, status: response.status }) + if (!response.ok) throw new DownloadFailedError({ url, status: response.status }) const buffer = await response.arrayBuffer() const archivePath = path.join(Global.Path.bin, filename) @@ -86,14 +80,11 @@ export namespace Fzf { }) } if (config.extension === "zip") { - const proc = Bun.spawn( - ["unzip", "-j", archivePath, "fzf.exe", "-d", Global.Path.bin], - { - cwd: Global.Path.bin, - stderr: "pipe", - stdout: "ignore", - }, - ) + const proc = Bun.spawn(["unzip", "-j", archivePath, "fzf.exe", "-d", Global.Path.bin], { + cwd: Global.Path.bin, + stderr: "pipe", + stdout: "ignore", + }) await proc.exited if (proc.exitCode !== 0) throw new ExtractionFailedError({ diff --git a/packages/opencode/src/file/index.ts b/packages/opencode/src/file/index.ts index d9e61fa5..b99f35e1 100644 --- a/packages/opencode/src/file/index.ts +++ b/packages/opencode/src/file/index.ts @@ -11,6 +11,19 @@ import { Log } from "../util/log" export namespace File { const log = Log.create({ service: "file" }) + export const Info = z + .object({ + path: z.string(), + added: z.number().int(), + removed: z.number().int(), + status: z.enum(["added", "deleted", "modified"]), + }) + .openapi({ + ref: "File", + }) + + export type Info = z.infer + export const Event = { Edited: Bus.event( "file.edited", @@ -24,20 +37,16 @@ export namespace File { const app = App.info() if (!app.git) return [] - const diffOutput = await $`git diff --numstat HEAD` - .cwd(app.path.cwd) - .quiet() - .nothrow() - .text() + const diffOutput = await $`git diff --numstat HEAD`.cwd(app.path.cwd).quiet().nothrow().text() - const changedFiles = [] + const changedFiles: Info[] = [] if (diffOutput.trim()) { const lines = diffOutput.trim().split("\n") for (const line of lines) { const [added, removed, filepath] = line.split("\t") changedFiles.push({ - file: filepath, + path: filepath, added: added === "-" ? 0 : parseInt(added, 10), removed: removed === "-" ? 0 : parseInt(removed, 10), status: "modified", @@ -45,22 +54,16 @@ export namespace File { } } - const untrackedOutput = await $`git ls-files --others --exclude-standard` - .cwd(app.path.cwd) - .quiet() - .nothrow() - .text() + const untrackedOutput = await $`git ls-files --others --exclude-standard`.cwd(app.path.cwd).quiet().nothrow().text() if (untrackedOutput.trim()) { const untrackedFiles = untrackedOutput.trim().split("\n") for (const filepath of untrackedFiles) { try { - const content = await Bun.file( - path.join(app.path.root, filepath), - ).text() + const content = await Bun.file(path.join(app.path.root, filepath)).text() const lines = content.split("\n").length changedFiles.push({ - file: filepath, + path: filepath, added: lines, removed: 0, status: "added", @@ -72,17 +75,13 @@ export namespace File { } // Get deleted files - const deletedOutput = await $`git diff --name-only --diff-filter=D HEAD` - .cwd(app.path.cwd) - .quiet() - .nothrow() - .text() + const deletedOutput = await $`git diff --name-only --diff-filter=D HEAD`.cwd(app.path.cwd).quiet().nothrow().text() if (deletedOutput.trim()) { const deletedFiles = deletedOutput.trim().split("\n") for (const filepath of deletedFiles) { changedFiles.push({ - file: filepath, + path: filepath, added: 0, removed: 0, // Could get original line count but would require another git command status: "deleted", @@ -92,7 +91,7 @@ export namespace File { return changedFiles.map((x) => ({ ...x, - file: path.relative(app.path.cwd, path.join(app.path.root, x.file)), + path: path.relative(app.path.cwd, path.join(app.path.root, x.path)), })) } @@ -112,11 +111,7 @@ export namespace File { filepath: rel, }) if (diff !== "unmodified") { - const original = await $`git show HEAD:${rel}` - .cwd(app.path.root) - .quiet() - .nothrow() - .text() + const original = await $`git show HEAD:${rel}`.cwd(app.path.root).quiet().nothrow().text() const patch = createPatch(file, original, content, "old", "new", { context: Infinity, }) diff --git a/packages/opencode/src/file/ripgrep.ts b/packages/opencode/src/file/ripgrep.ts index a975d34b..05ebbe7d 100644 --- a/packages/opencode/src/file/ripgrep.ts +++ b/packages/opencode/src/file/ripgrep.ts @@ -34,25 +34,27 @@ export namespace Ripgrep { export const Match = z.object({ type: z.literal("match"), - data: z.object({ - path: z.object({ - text: z.string(), - }), - lines: z.object({ - text: z.string(), - }), - line_number: z.number(), - absolute_offset: z.number(), - submatches: z.array( - z.object({ - match: z.object({ - text: z.string(), - }), - start: z.number(), - end: z.number(), + data: z + .object({ + path: z.object({ + text: z.string(), }), - ), - }), + lines: z.object({ + text: z.string(), + }), + line_number: z.number(), + absolute_offset: z.number(), + submatches: z.array( + z.object({ + match: z.object({ + text: z.string(), + }), + start: z.number(), + end: z.number(), + }), + ), + }) + .openapi({ ref: "Match" }), }) const End = z.object({ @@ -122,15 +124,11 @@ export namespace Ripgrep { const state = lazy(async () => { let filepath = Bun.which("rg") if (filepath) return { filepath } - filepath = path.join( - Global.Path.bin, - "rg" + (process.platform === "win32" ? ".exe" : ""), - ) + filepath = path.join(Global.Path.bin, "rg" + (process.platform === "win32" ? ".exe" : "")) const file = Bun.file(filepath) if (!(await file.exists())) { - const platformKey = - `${process.arch}-${process.platform}` as keyof typeof PLATFORM + const platformKey = `${process.arch}-${process.platform}` as keyof typeof PLATFORM const config = PLATFORM[platformKey] if (!config) throw new UnsupportedPlatformError({ platform: platformKey }) @@ -139,8 +137,7 @@ export namespace Ripgrep { const url = `https://github.com/BurntSushi/ripgrep/releases/download/${version}/${filename}` const response = await fetch(url) - if (!response.ok) - throw new DownloadFailedError({ url, status: response.status }) + if (!response.ok) throw new DownloadFailedError({ url, status: response.status }) const buffer = await response.arrayBuffer() const archivePath = path.join(Global.Path.bin, filename) @@ -164,14 +161,11 @@ export namespace Ripgrep { }) } if (config.extension === "zip") { - const proc = Bun.spawn( - ["unzip", "-j", archivePath, "*/rg.exe", "-d", Global.Path.bin], - { - cwd: Global.Path.bin, - stderr: "pipe", - stdout: "ignore", - }, - ) + const proc = Bun.spawn(["unzip", "-j", archivePath, "*/rg.exe", "-d", Global.Path.bin], { + cwd: Global.Path.bin, + stderr: "pipe", + stdout: "ignore", + }) await proc.exited if (proc.exitCode !== 0) throw new ExtractionFailedError({ @@ -193,17 +187,16 @@ export namespace Ripgrep { return filepath } - export async function files(input: { - cwd: string - query?: string - glob?: string - limit?: number - }) { - const commands = [ - `${await filepath()} --files --hidden --glob='!.git/*' ${input.glob ? `--glob='${input.glob}'` : ``}`, - ] - if (input.query) - commands.push(`${await Fzf.filepath()} --filter=${input.query}`) + export async function files(input: { cwd: string; query?: string; glob?: string[]; limit?: number }) { + const commands = [`${$.escape(await filepath())} --files --follow --hidden --glob='!.git/*'`] + + if (input.glob) { + for (const g of input.glob) { + commands[0] += ` --glob='${g}'` + } + } + + if (input.query) commands.push(`${await Fzf.filepath()} --filter=${input.query}`) if (input.limit) commands.push(`head -n ${input.limit}`) const joined = commands.join(" | ") const result = await $`${{ raw: joined }}`.cwd(input.cwd).nothrow().text() @@ -310,18 +303,8 @@ export namespace Ripgrep { return lines.join("\n") } - export async function search(input: { - cwd: string - pattern: string - glob?: string[] - limit?: number - }) { - const args = [ - `${await filepath()}`, - "--json", - "--hidden", - "--glob='!.git/*'", - ] + export async function search(input: { cwd: string; pattern: string; glob?: string[]; limit?: number }) { + const args = [`${await filepath()}`, "--json", "--hidden", "--glob='!.git/*'"] if (input.glob) { for (const g of input.glob) { diff --git a/packages/opencode/src/file/time.ts b/packages/opencode/src/file/time.ts index 908efdcc..453259e8 100644 --- a/packages/opencode/src/file/time.ts +++ b/packages/opencode/src/file/time.ts @@ -27,10 +27,7 @@ export namespace FileTime { export async function assert(sessionID: string, filepath: string) { const time = get(sessionID, filepath) - if (!time) - throw new Error( - `You must read the file ${filepath} before overwriting it. Use the Read tool first`, - ) + if (!time) throw new Error(`You must read the file ${filepath} before overwriting it. Use the Read tool first`) const stats = await Bun.file(filepath).stat() if (stats.mtime.getTime() > time.getTime()) { throw new Error( diff --git a/packages/opencode/src/file/watch.ts b/packages/opencode/src/file/watch.ts index 1d12168f..383ad6f3 100644 --- a/packages/opencode/src/file/watch.ts +++ b/packages/opencode/src/file/watch.ts @@ -21,23 +21,20 @@ export namespace FileWatcher { "file.watcher", () => { const app = App.use() + if (!app.info.git) return {} try { - const watcher = fs.watch( - app.info.path.cwd, - { recursive: true }, - (event, file) => { - log.info("change", { file, event }) - if (!file) return - // for some reason async local storage is lost here - // https://github.com/oven-sh/bun/issues/20754 - App.provideExisting(app, async () => { - Bus.publish(Event.Updated, { - file, - event, - }) + const watcher = fs.watch(app.info.path.cwd, { recursive: true }, (event, file) => { + log.info("change", { file, event }) + if (!file) return + // for some reason async local storage is lost here + // https://github.com/oven-sh/bun/issues/20754 + App.provideExisting(app, async () => { + Bus.publish(Event.Updated, { + file, + event, }) - }, - ) + }) + }) return { watcher } } catch { return {} @@ -49,7 +46,7 @@ export namespace FileWatcher { ) export function init() { - if (Flag.OPENCODE_DISABLE_WATCHER) return + if (Flag.OPENCODE_DISABLE_WATCHER || true) return state() } } diff --git a/packages/opencode/src/format/formatter.ts b/packages/opencode/src/format/formatter.ts index 60ca6a32..83e359f6 100644 --- a/packages/opencode/src/format/formatter.ts +++ b/packages/opencode/src/format/formatter.ts @@ -1,5 +1,7 @@ import { App } from "../app/app" import { BunProc } from "../bun" +import { Filesystem } from "../util/filesystem" +import path from "path" export interface Info { name: string @@ -29,7 +31,7 @@ export const mix: Info = { export const prettier: Info = { name: "prettier", - command: [BunProc.which(), "run", "prettier", "--write", "$FILE"], + command: [BunProc.which(), "x", "prettier", "--write", "$FILE"], environment: { BUN_BE_BUN: "1", }, @@ -62,23 +64,12 @@ export const prettier: Info = { ".gql", ], async enabled() { - // this is more complicated because we only want to use prettier if it's - // being used with the current project - try { - const proc = Bun.spawn({ - cmd: [BunProc.which(), "run", "prettier", "--version"], - cwd: App.info().path.cwd, - env: { - BUN_BE_BUN: "1", - }, - stdout: "ignore", - stderr: "ignore", - }) - const exit = await proc.exited - return exit === 0 - } catch { - return false + const app = App.info() + const nms = await Filesystem.findUp("node_modules", app.path.cwd, app.path.root) + for (const item of nms) { + if (await Bun.file(path.join(item, ".bin", "prettier")).exists()) return true } + return false }, } @@ -94,21 +85,7 @@ export const zig: Info = { export const clang: Info = { name: "clang-format", command: ["clang-format", "-i", "$FILE"], - extensions: [ - ".c", - ".cc", - ".cpp", - ".cxx", - ".c++", - ".h", - ".hh", - ".hpp", - ".hxx", - ".h++", - ".ino", - ".C", - ".H", - ], + extensions: [".c", ".cc", ".cpp", ".cxx", ".c++", ".h", ".hh", ".hpp", ".hxx", ".h++", ".ino", ".C", ".H"], async enabled() { return Bun.which("clang-format") !== null }, @@ -128,7 +105,29 @@ export const ruff: Info = { command: ["ruff", "format", "$FILE"], extensions: [".py", ".pyi"], async enabled() { - return Bun.which("ruff") !== null + if (!Bun.which("ruff")) return false + const app = App.info() + const configs = ["pyproject.toml", "ruff.toml", ".ruff.toml"] + for (const config of configs) { + const found = await Filesystem.findUp(config, app.path.cwd, app.path.root) + if (found.length > 0) { + if (config === "pyproject.toml") { + const content = await Bun.file(found[0]).text() + if (content.includes("[tool.ruff]")) return true + } else { + return true + } + } + } + const deps = ["requirements.txt", "pyproject.toml", "Pipfile"] + for (const dep of deps) { + const found = await Filesystem.findUp(dep, app.path.cwd, app.path.root) + if (found.length > 0) { + const content = await Bun.file(found[0]).text() + if (content.includes("ruff")) return true + } + } + return false }, } diff --git a/packages/opencode/src/global/index.ts b/packages/opencode/src/global/index.ts index 24e9b6dd..b083e94d 100644 --- a/packages/opencode/src/global/index.ts +++ b/packages/opencode/src/global/index.ts @@ -23,7 +23,17 @@ export namespace Global { await Promise.all([ fs.mkdir(Global.Path.data, { recursive: true }), fs.mkdir(Global.Path.config, { recursive: true }), - fs.mkdir(Global.Path.cache, { recursive: true }), fs.mkdir(Global.Path.providers, { recursive: true }), fs.mkdir(Global.Path.state, { recursive: true }), ]) + +const CACHE_VERSION = "2" + +const version = await Bun.file(path.join(Global.Path.cache, "version")) + .text() + .catch(() => "0") + +if (version !== CACHE_VERSION) { + await fs.rm(Global.Path.cache, { recursive: true, force: true }) + await Bun.file(path.join(Global.Path.cache, "version")).write(CACHE_VERSION) +} diff --git a/packages/opencode/src/id/id.ts b/packages/opencode/src/id/id.ts index cf9a3042..6c1edd50 100644 --- a/packages/opencode/src/id/id.ts +++ b/packages/opencode/src/id/id.ts @@ -6,6 +6,7 @@ export namespace Identifier { session: "ses", message: "msg", user: "usr", + part: "prt", } as const export function schema(prefix: keyof typeof prefixes) { @@ -26,11 +27,7 @@ export namespace Identifier { return generateID(prefix, true, given) } - function generateID( - prefix: keyof typeof prefixes, - descending: boolean, - given?: string, - ): string { + function generateID(prefix: keyof typeof prefixes, descending: boolean, given?: string): string { if (!given) { return generateNewID(prefix, descending) } @@ -42,8 +39,7 @@ export namespace Identifier { } function randomBase62(length: number): string { - const chars = - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" let result = "" const bytes = randomBytes(length) for (let i = 0; i < length; i++) { @@ -52,10 +48,7 @@ export namespace Identifier { return result } - function generateNewID( - prefix: keyof typeof prefixes, - descending: boolean, - ): string { + function generateNewID(prefix: keyof typeof prefixes, descending: boolean): string { const currentTimestamp = Date.now() if (currentTimestamp !== lastTimestamp) { @@ -73,11 +66,6 @@ export namespace Identifier { timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff)) } - return ( - prefixes[prefix] + - "_" + - timeBytes.toString("hex") + - randomBase62(LENGTH - 12) - ) + return prefixes[prefix] + "_" + timeBytes.toString("hex") + randomBase62(LENGTH - 12) } } diff --git a/packages/opencode/src/index.ts b/packages/opencode/src/index.ts index 3ca40ce7..df7f1aa8 100644 --- a/packages/opencode/src/index.ts +++ b/packages/opencode/src/index.ts @@ -14,6 +14,9 @@ import { FormatError } from "./cli/error" import { ServeCommand } from "./cli/cmd/serve" import { TuiCommand } from "./cli/cmd/tui" import { DebugCommand } from "./cli/cmd/debug" +import { StatsCommand } from "./cli/cmd/stats" +import { McpCommand } from "./cli/cmd/mcp" +import { InstallGithubCommand } from "./cli/cmd/install-github" const cancel = new AbortController() @@ -40,12 +43,31 @@ const cli = yargs(hideBin(process.argv)) }) .middleware(async () => { await Log.init({ print: process.argv.includes("--print-logs") }) + + try { + const { Config } = await import("./config/config") + const { App } = await import("./app/app") + + App.provide({ cwd: process.cwd() }, async () => { + const cfg = await Config.get() + if (cfg.log_level) { + Log.setLevel(cfg.log_level as Log.Level) + } else { + const defaultLevel = Installation.isDev() ? "DEBUG" : "INFO" + Log.setLevel(defaultLevel) + } + }) + } catch (e) { + Log.Default.error("failed to load config", { error: e }) + } + Log.Default.info("opencode", { version: Installation.VERSION, args: process.argv.slice(2), }) }) .usage("\n" + UI.logo()) + .command(McpCommand) .command(TuiCommand) .command(RunCommand) .command(GenerateCommand) @@ -54,11 +76,10 @@ const cli = yargs(hideBin(process.argv)) .command(UpgradeCommand) .command(ServeCommand) .command(ModelsCommand) + .command(StatsCommand) + .command(InstallGithubCommand) .fail((msg) => { - if ( - msg.startsWith("Unknown argument") || - msg.startsWith("Not enough non-option arguments") - ) { + if (msg.startsWith("Unknown argument") || msg.startsWith("Not enough non-option arguments")) { cli.showHelp("log") } }) @@ -97,10 +118,7 @@ try { Log.Default.error("fatal", data) const formatted = FormatError(e) if (formatted) UI.error(formatted) - if (formatted === undefined) - UI.error( - "Unexpected error, check log file at " + Log.file() + " for more details", - ) + if (formatted === undefined) UI.error("Unexpected error, check log file at " + Log.file() + " for more details") process.exitCode = 1 } diff --git a/packages/opencode/src/installation/index.ts b/packages/opencode/src/installation/index.ts index 4af5c807..ab631a8d 100644 --- a/packages/opencode/src/installation/index.ts +++ b/packages/opencode/src/installation/index.ts @@ -135,12 +135,17 @@ export namespace Installation { }) } - export const VERSION = - typeof OPENCODE_VERSION === "string" ? OPENCODE_VERSION : "dev" + export const VERSION = typeof OPENCODE_VERSION === "string" ? OPENCODE_VERSION : "dev" export async function latest() { return fetch("https://api.github.com/repos/sst/opencode/releases/latest") .then((res) => res.json()) - .then((data) => data.tag_name.slice(1) as string) + .then((data) => { + if (typeof data.tag_name !== "string") { + log.error("GitHub API error", data) + throw new Error("failed to fetch latest version") + } + return data.tag_name.slice(1) as string + }) } } diff --git a/packages/opencode/src/lsp/client.ts b/packages/opencode/src/lsp/client.ts index 5aff437d..c63e0259 100644 --- a/packages/opencode/src/lsp/client.ts +++ b/packages/opencode/src/lsp/client.ts @@ -1,9 +1,5 @@ import path from "path" -import { - createMessageConnection, - StreamMessageReader, - StreamMessageWriter, -} from "vscode-jsonrpc/node" +import { createMessageConnection, StreamMessageReader, StreamMessageWriter } from "vscode-jsonrpc/node" import type { Diagnostic as VSCodeDiagnostic } from "vscode-languageserver-types" import { App } from "../app/app" import { Log } from "../util/log" @@ -38,45 +34,54 @@ export namespace LSPClient { ), } - export async function create(serverID: string, server: LSPServer.Handle) { + export async function create(input: { serverID: string; server: LSPServer.Handle; root: string }) { const app = App.info() - log.info("starting client", { id: serverID }) + const l = log.clone().tag("serverID", input.serverID) + l.info("starting client") const connection = createMessageConnection( - new StreamMessageReader(server.process.stdout), - new StreamMessageWriter(server.process.stdin), + new StreamMessageReader(input.server.process.stdout), + new StreamMessageWriter(input.server.process.stdin), ) const diagnostics = new Map() connection.onNotification("textDocument/publishDiagnostics", (params) => { const path = new URL(params.uri).pathname - log.info("textDocument/publishDiagnostics", { + l.info("textDocument/publishDiagnostics", { path, }) const exists = diagnostics.has(path) diagnostics.set(path, params.diagnostics) - if (!exists && serverID === "typescript") return - Bus.publish(Event.Diagnostics, { path, serverID }) + if (!exists && input.serverID === "typescript") return + Bus.publish(Event.Diagnostics, { path, serverID: input.serverID }) + }) + connection.onRequest("window/workDoneProgress/create", (params) => { + l.info("window/workDoneProgress/create", params) + return null }) connection.onRequest("workspace/configuration", async () => { return [{}] }) connection.listen() - log.info("sending initialize", { id: serverID }) + l.info("sending initialize") await withTimeout( connection.sendRequest("initialize", { - processId: server.process.pid, + rootUri: "file://" + input.root, + processId: input.server.process.pid, workspaceFolders: [ { name: "workspace", - uri: "file://" + app.path.cwd, + uri: "file://" + input.root, }, ], initializationOptions: { - ...server.initialization, + ...input.server.initialization, }, capabilities: { + window: { + workDoneProgress: true, + }, workspace: { configuration: true, }, @@ -93,9 +98,9 @@ export namespace LSPClient { }), 5_000, ).catch((err) => { - log.error("initialize error", { error: err }) + l.error("initialize error", { error: err }) throw new InitializeError( - { serverID }, + { serverID: input.serverID }, { cause: err, }, @@ -103,26 +108,22 @@ export namespace LSPClient { }) await connection.sendNotification("initialized", {}) - log.info("initialized", { - serverID, - }) const files: { [path: string]: number } = {} const result = { + root: input.root, get serverID() { - return serverID + return input.serverID }, get connection() { return connection }, notify: { async open(input: { path: string }) { - input.path = path.isAbsolute(input.path) - ? input.path - : path.resolve(app.path.cwd, input.path) + input.path = path.isAbsolute(input.path) ? input.path : path.resolve(app.path.cwd, input.path) const file = Bun.file(input.path) const text = await file.text() const version = files[input.path] @@ -154,18 +155,13 @@ export namespace LSPClient { return diagnostics }, async waitForDiagnostics(input: { path: string }) { - input.path = path.isAbsolute(input.path) - ? input.path - : path.resolve(app.path.cwd, input.path) + input.path = path.isAbsolute(input.path) ? input.path : path.resolve(app.path.cwd, input.path) log.info("waiting for diagnostics", input) let unsub: () => void return await withTimeout( new Promise((resolve) => { unsub = Bus.subscribe(Event.Diagnostics, (event) => { - if ( - event.properties.path === input.path && - event.properties.serverID === result.serverID - ) { + if (event.properties.path === input.path && event.properties.serverID === result.serverID) { log.info("got diagnostics", input) unsub?.() resolve() @@ -180,13 +176,16 @@ export namespace LSPClient { }) }, async shutdown() { - log.info("shutting down", { serverID }) + l.info("shutting down") connection.end() connection.dispose() - log.info("shutdown", { serverID }) + input.server.process.kill() + l.info("shutdown") }, } + l.info("initialized") + return result } } diff --git a/packages/opencode/src/lsp/index.ts b/packages/opencode/src/lsp/index.ts index 88e549bb..8f6c1c9c 100644 --- a/packages/opencode/src/lsp/index.ts +++ b/packages/opencode/src/lsp/index.ts @@ -3,64 +3,65 @@ import { Log } from "../util/log" import { LSPClient } from "./client" import path from "path" import { LSPServer } from "./server" -import { Ripgrep } from "../file/ripgrep" import { z } from "zod" export namespace LSP { const log = Log.create({ service: "lsp" }) + export const Range = z + .object({ + start: z.object({ + line: z.number(), + character: z.number(), + }), + end: z.object({ + line: z.number(), + character: z.number(), + }), + }) + .openapi({ + ref: "Range", + }) + export type Range = z.infer + export const Symbol = z .object({ name: z.string(), kind: z.number(), location: z.object({ uri: z.string(), - range: z.object({ - start: z.object({ - line: z.number(), - character: z.number(), - }), - end: z.object({ - line: z.number(), - character: z.number(), - }), - }), + range: Range, }), }) .openapi({ - ref: "LSP.Symbol", + ref: "Symbol", }) export type Symbol = z.infer + export const DocumentSymbol = z + .object({ + name: z.string(), + detail: z.string().optional(), + kind: z.number(), + range: Range, + selectionRange: Range, + }) + .openapi({ + ref: "DocumentSymbol", + }) + export type DocumentSymbol = z.infer + const state = App.state( "lsp", - async (app) => { - log.info("initializing") - const clients = new Map() - for (const server of Object.values(LSPServer)) { - for (const extension of server.extensions) { - const [file] = await Ripgrep.files({ - cwd: app.path.cwd, - glob: "*" + extension, - }) - if (!file) continue - const handle = await server.spawn(App.info()) - if (!handle) break - const client = await LSPClient.create(server.id, handle).catch( - (err) => log.error("", { error: err }), - ) - if (!client) break - clients.set(server.id, client) - break - } - } - log.info("initialized") + async () => { + const clients: LSPClient.Info[] = [] return { + broken: new Set(), clients, } }, async (state) => { - for (const client of state.clients.values()) { + for (const client of state.clients) { await client.shutdown() } }, @@ -70,16 +71,44 @@ export namespace LSP { return state() } + async function getClients(file: string) { + const s = await state() + const extension = path.parse(file).ext + const result: LSPClient.Info[] = [] + for (const server of Object.values(LSPServer)) { + if (!server.extensions.includes(extension)) continue + const root = await server.root(file, App.info()) + if (!root) continue + if (s.broken.has(root + server.id)) continue + + const match = s.clients.find((x) => x.root === root && x.serverID === server.id) + if (match) { + result.push(match) + continue + } + const handle = await server.spawn(App.info(), root) + if (!handle) continue + const client = await LSPClient.create({ + serverID: server.id, + server: handle, + root, + }).catch((err) => { + s.broken.add(root + server.id) + handle.process.kill() + log.error("", { error: err }) + }) + if (!client) continue + s.clients.push(client) + result.push(client) + } + return result + } + export async function touchFile(input: string, waitForDiagnostics?: boolean) { - const extension = path.parse(input).ext - const matches = Object.values(LSPServer) - .filter((x) => x.extensions.includes(extension)) - .map((x) => x.id) + const clients = await getClients(input) await run(async (client) => { - if (!matches.includes(client.serverID)) return - const wait = waitForDiagnostics - ? client.waitForDiagnostics({ path: input }) - : Promise.resolve() + if (!clients.includes(client)) return + const wait = waitForDiagnostics ? client.waitForDiagnostics({ path: input }) : Promise.resolve() await client.notify.open({ path: input }) return wait }) @@ -97,11 +126,7 @@ export namespace LSP { return results } - export async function hover(input: { - file: string - line: number - character: number - }) { + export async function hover(input: { file: string; line: number; character: number }) { return run((client) => { return client.connection.sendRequest("textDocument/hover", { textDocument: { @@ -115,18 +140,74 @@ export namespace LSP { }) } + enum SymbolKind { + File = 1, + Module = 2, + Namespace = 3, + Package = 4, + Class = 5, + Method = 6, + Property = 7, + Field = 8, + Constructor = 9, + Enum = 10, + Interface = 11, + Function = 12, + Variable = 13, + Constant = 14, + String = 15, + Number = 16, + Boolean = 17, + Array = 18, + Object = 19, + Key = 20, + Null = 21, + EnumMember = 22, + Struct = 23, + Event = 24, + Operator = 25, + TypeParameter = 26, + } + + const kinds = [ + SymbolKind.Class, + SymbolKind.Function, + SymbolKind.Method, + SymbolKind.Interface, + SymbolKind.Variable, + SymbolKind.Constant, + SymbolKind.Struct, + SymbolKind.Enum, + ] + export async function workspaceSymbol(query: string) { return run((client) => - client.connection.sendRequest("workspace/symbol", { - query, - }), + client.connection + .sendRequest("workspace/symbol", { + query, + }) + .then((result: any) => result.filter((x: LSP.Symbol) => kinds.includes(x.kind))) + .then((result: any) => result.slice(0, 10)) + .catch(() => []), ).then((result) => result.flat() as LSP.Symbol[]) } - async function run( - input: (client: LSPClient.Info) => Promise, - ): Promise { - const clients = await state().then((x) => [...x.clients.values()]) + export async function documentSymbol(uri: string) { + return run((client) => + client.connection + .sendRequest("textDocument/documentSymbol", { + textDocument: { + uri, + }, + }) + .catch(() => []), + ) + .then((result) => result.flat() as (LSP.DocumentSymbol | LSP.Symbol)[]) + .then((result) => result.filter(Boolean)) + } + + async function run(input: (client: LSPClient.Info) => Promise): Promise { + const clients = await state().then((x) => x.clients) const tasks = clients.map((x) => input(x)) return Promise.all(tasks) } diff --git a/packages/opencode/src/lsp/language.ts b/packages/opencode/src/lsp/language.ts index 6c0da51b..61686bd9 100644 --- a/packages/opencode/src/lsp/language.ts +++ b/packages/opencode/src/lsp/language.ts @@ -94,4 +94,6 @@ export const LANGUAGE_EXTENSIONS: Record = { ".yml": "yaml", ".mjs": "javascript", ".cjs": "javascript", + ".zig": "zig", + ".zon": "zig", } as const diff --git a/packages/opencode/src/lsp/server.ts b/packages/opencode/src/lsp/server.ts index 39a23f0f..8c843fea 100644 --- a/packages/opencode/src/lsp/server.ts +++ b/packages/opencode/src/lsp/server.ts @@ -6,6 +6,7 @@ import { Log } from "../util/log" import { BunProc } from "../bun" import { $ } from "bun" import fs from "fs/promises" +import { Filesystem } from "../util/filesystem" export namespace LSPServer { const log = Log.create({ service: "lsp.server" }) @@ -15,31 +16,44 @@ export namespace LSPServer { initialization?: Record } + type RootFunction = (file: string, app: App.Info) => Promise + + const NearestRoot = (patterns: string[]): RootFunction => { + return async (file, app) => { + const files = Filesystem.up({ + targets: patterns, + start: path.dirname(file), + stop: app.path.root, + }) + const first = await files.next() + await files.return() + if (!first.value) return app.path.root + return path.dirname(first.value) + } + } + export interface Info { id: string extensions: string[] - spawn(app: App.Info): Promise + global?: boolean + root: RootFunction + spawn(app: App.Info, root: string): Promise } export const Typescript: Info = { id: "typescript", + root: NearestRoot(["tsconfig.json", "package.json", "jsconfig.json"]), extensions: [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".mts", ".cts"], - async spawn(app) { - const tsserver = await Bun.resolve( - "typescript/lib/tsserver.js", - app.path.cwd, - ).catch(() => {}) + async spawn(app, root) { + const tsserver = await Bun.resolve("typescript/lib/tsserver.js", app.path.cwd).catch(() => {}) if (!tsserver) return - const proc = spawn( - BunProc.which(), - ["x", "typescript-language-server", "--stdio"], - { - env: { - ...process.env, - BUN_BE_BUN: "1", - }, + const proc = spawn(BunProc.which(), ["x", "typescript-language-server", "--stdio"], { + cwd: root, + env: { + ...process.env, + BUN_BE_BUN: "1", }, - ) + }) return { process: proc, initialization: { @@ -53,8 +67,13 @@ export namespace LSPServer { export const Gopls: Info = { id: "golang", + root: async (file, app) => { + const work = await NearestRoot(["go.work"])(file, app) + if (work) return work + return NearestRoot(["go.mod", "go.sum"])(file, app) + }, extensions: [".go"], - async spawn() { + async spawn(_, root) { let bin = Bun.which("gopls", { PATH: process.env["PATH"] + ":" + Global.Path.bin, }) @@ -73,24 +92,24 @@ export namespace LSPServer { log.error("Failed to install gopls") return } - bin = path.join( - Global.Path.bin, - "gopls" + (process.platform === "win32" ? ".exe" : ""), - ) + bin = path.join(Global.Path.bin, "gopls" + (process.platform === "win32" ? ".exe" : "")) log.info(`installed gopls`, { bin, }) } return { - process: spawn(bin!), + process: spawn(bin!, { + cwd: root, + }), } }, } export const RubyLsp: Info = { id: "ruby-lsp", + root: NearestRoot(["Gemfile"]), extensions: [".rb", ".rake", ".gemspec", ".ru"], - async spawn() { + async spawn(_, root) { let bin = Bun.which("ruby-lsp", { PATH: process.env["PATH"] + ":" + Global.Path.bin, }) @@ -113,16 +132,15 @@ export namespace LSPServer { log.error("Failed to install ruby-lsp") return } - bin = path.join( - Global.Path.bin, - "ruby-lsp" + (process.platform === "win32" ? ".exe" : ""), - ) + bin = path.join(Global.Path.bin, "ruby-lsp" + (process.platform === "win32" ? ".exe" : "")) log.info(`installed ruby-lsp`, { bin, }) } return { - process: spawn(bin!, ["--stdio"]), + process: spawn(bin!, ["--stdio"], { + cwd: root, + }), } }, } @@ -130,17 +148,15 @@ export namespace LSPServer { export const Pyright: Info = { id: "pyright", extensions: [".py", ".pyi"], - async spawn() { - const proc = spawn( - BunProc.which(), - ["x", "pyright-langserver", "--stdio"], - { - env: { - ...process.env, - BUN_BE_BUN: "1", - }, + root: NearestRoot(["pyproject.toml", "setup.py", "setup.cfg", "requirements.txt", "Pipfile", "pyrightconfig.json"]), + async spawn(_, root) { + const proc = spawn(BunProc.which(), ["x", "pyright-langserver", "--stdio"], { + cwd: root, + env: { + ...process.env, + BUN_BE_BUN: "1", }, - ) + }) return { process: proc, } @@ -150,7 +166,8 @@ export namespace LSPServer { export const ElixirLS: Info = { id: "elixir-ls", extensions: [".ex", ".exs"], - async spawn() { + root: NearestRoot(["mix.exs", "mix.lock"]), + async spawn(_, root) { let binary = Bun.which("elixir-ls") if (!binary) { const elixirLsPath = path.join(Global.Path.bin, "elixir-ls") @@ -158,9 +175,7 @@ export namespace LSPServer { Global.Path.bin, "elixir-ls-master", "release", - process.platform === "win32" - ? "language_server.bar" - : "language_server.sh", + process.platform === "win32" ? "language_server.bar" : "language_server.sh", ) if (!(await Bun.file(binary).exists())) { @@ -172,9 +187,7 @@ export namespace LSPServer { log.info("downloading elixir-ls from GitHub releases") - const response = await fetch( - "https://github.com/elixir-lsp/elixir-ls/archive/refs/heads/master.zip", - ) + const response = await fetch("https://github.com/elixir-lsp/elixir-ls/archive/refs/heads/master.zip") if (!response.ok) return const zipPath = path.join(Global.Path.bin, "elixir-ls.zip") await Bun.file(zipPath).write(response) @@ -198,7 +211,114 @@ export namespace LSPServer { } return { - process: spawn(binary), + process: spawn(binary, { + cwd: root, + }), + } + }, + } + + export const Zls: Info = { + id: "zls", + extensions: [".zig", ".zon"], + root: NearestRoot(["build.zig"]), + async spawn(_, root) { + let bin = Bun.which("zls", { + PATH: process.env["PATH"] + ":" + Global.Path.bin, + }) + + if (!bin) { + const zig = Bun.which("zig") + if (!zig) { + log.error("Zig is required to use zls. Please install Zig first.") + return + } + + log.info("downloading zls from GitHub releases") + + const releaseResponse = await fetch("https://api.github.com/repos/zigtools/zls/releases/latest") + if (!releaseResponse.ok) { + log.error("Failed to fetch zls release info") + return + } + + const release = await releaseResponse.json() + + const platform = process.platform + const arch = process.arch + let assetName = "" + + let zlsArch: string = arch + if (arch === "arm64") zlsArch = "aarch64" + else if (arch === "x64") zlsArch = "x86_64" + else if (arch === "ia32") zlsArch = "x86" + + let zlsPlatform: string = platform + if (platform === "darwin") zlsPlatform = "macos" + else if (platform === "win32") zlsPlatform = "windows" + + const ext = platform === "win32" ? "zip" : "tar.xz" + + assetName = `zls-${zlsArch}-${zlsPlatform}.${ext}` + + const supportedCombos = [ + "zls-x86_64-linux.tar.xz", + "zls-x86_64-macos.tar.xz", + "zls-x86_64-windows.zip", + "zls-aarch64-linux.tar.xz", + "zls-aarch64-macos.tar.xz", + "zls-aarch64-windows.zip", + "zls-x86-linux.tar.xz", + "zls-x86-windows.zip", + ] + + if (!supportedCombos.includes(assetName)) { + log.error(`Platform ${platform} and architecture ${arch} is not supported by zls`) + return + } + + const asset = release.assets.find((a: any) => a.name === assetName) + if (!asset) { + log.error(`Could not find asset ${assetName} in latest zls release`) + return + } + + const downloadUrl = asset.browser_download_url + const downloadResponse = await fetch(downloadUrl) + if (!downloadResponse.ok) { + log.error("Failed to download zls") + return + } + + const tempPath = path.join(Global.Path.bin, assetName) + await Bun.file(tempPath).write(downloadResponse) + + if (ext === "zip") { + await $`unzip -o -q ${tempPath}`.cwd(Global.Path.bin).nothrow() + } else { + await $`tar -xf ${tempPath}`.cwd(Global.Path.bin).nothrow() + } + + await fs.rm(tempPath, { force: true }) + + bin = path.join(Global.Path.bin, "zls" + (platform === "win32" ? ".exe" : "")) + + if (!(await Bun.file(bin).exists())) { + log.error("Failed to extract zls binary") + return + } + + if (platform !== "win32") { + await $`chmod +x ${bin}`.nothrow() + } + + log.info(`installed zls`, { bin }) + } + + return { + process: spawn(bin, { + cwd: root, + }), } }, } diff --git a/packages/opencode/src/mcp/index.ts b/packages/opencode/src/mcp/index.ts index ded7d0d2..34aec640 100644 --- a/packages/opencode/src/mcp/index.ts +++ b/packages/opencode/src/mcp/index.ts @@ -37,6 +37,7 @@ export namespace MCP { transport: { type: "sse", url: mcp.url, + headers: mcp.headers, }, }).catch(() => {}) if (!client) { diff --git a/packages/opencode/src/provider/provider.ts b/packages/opencode/src/provider/provider.ts index f05d15ce..7f10b7db 100644 --- a/packages/opencode/src/provider/provider.ts +++ b/packages/opencode/src/provider/provider.ts @@ -21,7 +21,7 @@ import { AuthCopilot } from "../auth/copilot" import { ModelsDev } from "./models" import { NamedError } from "../util/error" import { Auth } from "../auth" -// import { TaskTool } from "../tool/task" +import { TaskTool } from "../tool/task" export namespace Provider { const log = Log.create({ service: "provider" }) @@ -91,8 +91,7 @@ export namespace Provider { if (!info || info.type !== "oauth") return if (!info.access || info.expires < Date.now()) { const tokens = await copilot.access(info.refresh) - if (!tokens) - throw new Error("GitHub Copilot authentication expired") + if (!tokens) throw new Error("GitHub Copilot authentication expired") await Auth.set("github-copilot", { type: "oauth", ...tokens, @@ -100,25 +99,27 @@ export namespace Provider { info.access = tokens.access } let isAgentCall = false + let isVisionRequest = false try { - const body = - typeof init.body === "string" - ? JSON.parse(init.body) - : init.body + const body = typeof init.body === "string" ? JSON.parse(init.body) : init.body if (body?.messages) { - isAgentCall = body.messages.some( + isAgentCall = body.messages.some((msg: any) => msg.role && ["tool", "assistant"].includes(msg.role)) + isVisionRequest = body.messages.some( (msg: any) => - msg.role && ["tool", "assistant"].includes(msg.role), + Array.isArray(msg.content) && msg.content.some((part: any) => part.type === "image_url"), ) } } catch {} - const headers = { + const headers: Record = { ...init.headers, ...copilot.HEADERS, Authorization: `Bearer ${info.access}`, "Openai-Intent": "conversation-edits", "X-Initiator": isAgentCall ? "agent" : "user", } + if (isVisionRequest) { + headers["Copilot-Vision-Request"] = "true" + } delete headers["x-api-key"] return fetch(input, { ...init, @@ -138,14 +139,12 @@ export namespace Provider { } }, "amazon-bedrock": async () => { - if (!process.env["AWS_PROFILE"] && !process.env["AWS_ACCESS_KEY_ID"]) + if (!process.env["AWS_PROFILE"] && !process.env["AWS_ACCESS_KEY_ID"] && !process.env["AWS_BEARER_TOKEN_BEDROCK"]) return { autoload: false } const region = process.env["AWS_REGION"] ?? "us-east-1" - const { fromNodeProviderChain } = await import( - await BunProc.install("@aws-sdk/credential-providers") - ) + const { fromNodeProviderChain } = await import(await BunProc.install("@aws-sdk/credential-providers")) return { autoload: true, options: { @@ -157,9 +156,7 @@ export namespace Provider { switch (regionPrefix) { case "us": { - const modelRequiresPrefix = ["claude", "deepseek"].some((m) => - modelID.includes(m), - ) + const modelRequiresPrefix = ["claude", "deepseek"].some((m) => modelID.includes(m)) if (modelRequiresPrefix) { modelID = `${regionPrefix}.${modelID}` } @@ -174,25 +171,18 @@ export namespace Provider { "eu-south-1", "eu-south-2", ].some((r) => region.includes(r)) - const modelRequiresPrefix = [ - "claude", - "nova-lite", - "nova-micro", - "llama3", - "pixtral", - ].some((m) => modelID.includes(m)) + const modelRequiresPrefix = ["claude", "nova-lite", "nova-micro", "llama3", "pixtral"].some((m) => + modelID.includes(m), + ) if (regionRequiresPrefix && modelRequiresPrefix) { modelID = `${regionPrefix}.${modelID}` } break } case "ap": { - const modelRequiresPrefix = [ - "claude", - "nova-lite", - "nova-micro", - "nova-pro", - ].some((m) => modelID.includes(m)) + const modelRequiresPrefix = ["claude", "nova-lite", "nova-micro", "nova-pro"].some((m) => + modelID.includes(m), + ) if (modelRequiresPrefix) { regionPrefix = "apac" modelID = `${regionPrefix}.${modelID}` @@ -230,10 +220,7 @@ export namespace Provider { options: Record } } = {} - const models = new Map< - string, - { info: ModelsDev.Model; language: LanguageModel } - >() + const models = new Map() const sdk = new Map() log.info("init") @@ -248,7 +235,7 @@ export namespace Provider { if (!provider) { const info = database[id] if (!info) return - if (info.api) options["baseURL"] = info.api + if (info.api && !options["baseURL"]) options["baseURL"] = info.api providers[id] = { source, info, @@ -285,14 +272,20 @@ export namespace Provider { reasoning: model.reasoning ?? existing?.reasoning ?? false, temperature: model.temperature ?? existing?.temperature ?? false, tool_call: model.tool_call ?? existing?.tool_call ?? true, - cost: { - ...existing?.cost, - ...model.cost, - input: 0, - output: 0, - cache_read: 0, - cache_write: 0, - }, + cost: + !model.cost && !existing?.cost + ? { + input: 0, + output: 0, + cache_read: 0, + cache_write: 0, + } + : { + cache_read: 0, + cache_write: 0, + ...existing?.cost, + ...model.cost, + }, options: { ...existing?.options, ...model.options, @@ -308,9 +301,7 @@ export namespace Provider { database[providerID] = parsed } - const disabled = await Config.get().then( - (cfg) => new Set(cfg.disabled_providers ?? []), - ) + const disabled = await Config.get().then((cfg) => new Set(cfg.disabled_providers ?? [])) // load env for (const [providerID, provider] of Object.entries(database)) { if (disabled.has(providerID)) continue @@ -337,12 +328,7 @@ export namespace Provider { if (disabled.has(providerID)) continue const result = await fn(database[providerID]) if (result && (result.autoload || providers[providerID])) { - mergeProvider( - providerID, - result.options ?? {}, - "custom", - result.getModel, - ) + mergeProvider(providerID, result.options ?? {}, "custom", result.getModel) } } @@ -379,7 +365,7 @@ export namespace Provider { const existing = s.sdk.get(provider.id) if (existing) return existing const pkg = provider.npm ?? provider.id - const mod = await import(await BunProc.install(pkg, "latest")) + const mod = await import(await BunProc.install(pkg, "beta")) const fn = mod[Object.keys(mod).find((key) => key.startsWith("create"))!] const loaded = fn(s.providers[provider.id]?.options) s.sdk.set(provider.id, loaded) @@ -406,9 +392,7 @@ export namespace Provider { const sdk = await getSDK(provider.info) try { - const language = provider.getModel - ? await provider.getModel(sdk, modelID) - : sdk.languageModel(modelID) + const language = provider.getModel ? await provider.getModel(sdk, modelID) : sdk.languageModel(modelID) log.info("found", { providerID, modelID }) s.models.set(key, { info, @@ -431,14 +415,22 @@ export namespace Provider { } } + export async function getSmallModel(providerID: string) { + const provider = await state().then((state) => state.providers[providerID]) + if (!provider) return + const priority = ["3-5-haiku", "3.5-haiku", "gemini-2.5-flash"] + for (const item of priority) { + for (const model of Object.keys(provider.info.models)) { + if (model.includes(item)) return getModel(providerID, model) + } + } + } + const priority = ["gemini-2.5-pro-preview", "codex-mini", "claude-sonnet-4"] export function sort(models: ModelsDev.Model[]) { return sortBy( models, - [ - (model) => priority.findIndex((filter) => model.id.includes(filter)), - "desc", - ], + [(model) => priority.findIndex((filter) => model.id.includes(filter)), "desc"], [(model) => (model.id.includes("latest") ? 0 : 1), "asc"], [(model) => model.id, "desc"], ) @@ -449,11 +441,7 @@ export namespace Provider { if (cfg.model) return parseModel(cfg.model) const provider = await list() .then((val) => Object.values(val)) - .then((x) => - x.find( - (p) => !cfg.provider || Object.keys(cfg.provider).includes(p.info.id), - ), - ) + .then((x) => x.find((p) => !cfg.provider || Object.keys(cfg.provider).includes(p.info.id))) if (!provider) throw new Error("no providers found") const [model] = sort(Object.values(provider.info.models)) if (!model) throw new Error("no models found") @@ -486,7 +474,7 @@ export namespace Provider { WriteTool, TodoWriteTool, TodoReadTool, - // TaskTool, + TaskTool, ] const TOOL_MAPPING: Record = { @@ -536,9 +524,11 @@ export namespace Provider { if (schema instanceof z.ZodUnion) { return z.union( - schema.options.map((option: z.ZodTypeAny) => - optionalToNullable(option), - ) as [z.ZodTypeAny, z.ZodTypeAny, ...z.ZodTypeAny[]], + schema.options.map((option: z.ZodTypeAny) => optionalToNullable(option)) as [ + z.ZodTypeAny, + z.ZodTypeAny, + ...z.ZodTypeAny[], + ], ) } @@ -559,12 +549,4 @@ export namespace Provider { providerID: z.string(), }), ) - - export const AuthError = NamedError.create( - "ProviderAuthError", - z.object({ - providerID: z.string(), - message: z.string(), - }), - ) } diff --git a/packages/opencode/src/provider/transform.ts b/packages/opencode/src/provider/transform.ts index d5e140b3..433e9f41 100644 --- a/packages/opencode/src/provider/transform.ts +++ b/packages/opencode/src/provider/transform.ts @@ -1,22 +1,21 @@ -import type { LanguageModelV1Prompt } from "ai" +import type { ModelMessage } from "ai" import { unique } from "remeda" export namespace ProviderTransform { - export function message( - msgs: LanguageModelV1Prompt, - providerID: string, - modelID: string, - ) { + export function message(msgs: ModelMessage[], providerID: string, modelID: string) { if (providerID === "anthropic" || modelID.includes("anthropic")) { const system = msgs.filter((msg) => msg.role === "system").slice(0, 2) const final = msgs.filter((msg) => msg.role !== "system").slice(-2) for (const msg of unique([...system, ...final])) { - msg.providerMetadata = { - ...msg.providerMetadata, + msg.providerOptions = { + ...msg.providerOptions, anthropic: { cacheControl: { type: "ephemeral" }, }, + openaiCompatible: { + cache_control: { type: "ephemeral" }, + }, } } } @@ -25,8 +24,8 @@ export namespace ProviderTransform { const final = msgs.filter((msg) => msg.role !== "system").slice(-2) for (const msg of unique([...system, ...final])) { - msg.providerMetadata = { - ...msg.providerMetadata, + msg.providerOptions = { + ...msg.providerOptions, bedrock: { cachePoint: { type: "ephemeral" }, }, diff --git a/packages/opencode/src/server/server.ts b/packages/opencode/src/server/server.ts index df645cd8..6469b9bb 100644 --- a/packages/opencode/src/server/server.ts +++ b/packages/opencode/src/server/server.ts @@ -6,7 +6,6 @@ import { streamSSE } from "hono/streaming" import { Session } from "../session" import { resolver, validator as zValidator } from "hono-openapi/zod" import { z } from "zod" -import { Message } from "../session/message" import { Provider } from "../provider/provider" import { App } from "../app/app" import { mapValues } from "remeda" @@ -16,6 +15,8 @@ import { Ripgrep } from "../file/ripgrep" import { Config } from "../config/config" import { File } from "../file" import { LSP } from "../lsp" +import { MessageV2 } from "../session/message-v2" +import { Mode } from "../session/mode" const ERRORS = { 400: { @@ -51,12 +52,9 @@ export namespace Server { status: 400, }) } - return c.json( - new NamedError.Unknown({ message: err.toString() }).toObject(), - { - status: 400, - }, - ) + return c.json(new NamedError.Unknown({ message: err.toString() }).toObject(), { + status: 400, + }) }) .use(async (c, next) => { log.info("request", { @@ -271,6 +269,7 @@ export namespace Server { zValidator( "json", z.object({ + messageID: z.string(), providerID: z.string(), modelID: z.string(), }), @@ -407,7 +406,14 @@ export namespace Server { description: "List of messages", content: { "application/json": { - schema: resolver(Message.Info.array()), + schema: resolver( + z + .object({ + info: MessageV2.Info, + parts: MessageV2.Part.array(), + }) + .array(), + ), }, }, }, @@ -433,7 +439,7 @@ export namespace Server { description: "Created message", content: { "application/json": { - schema: resolver(Message.Info), + schema: resolver(MessageV2.Assistant), }, }, }, @@ -448,9 +454,11 @@ export namespace Server { zValidator( "json", z.object({ + messageID: z.string(), providerID: z.string(), modelID: z.string(), - parts: Message.MessagePart.array(), + mode: z.string(), + parts: z.union([MessageV2.FilePart, MessageV2.TextPart]).array(), }), ), async (c) => { @@ -481,15 +489,10 @@ export namespace Server { }, }), async (c) => { - const providers = await Provider.list().then((x) => - mapValues(x, (item) => item.info), - ) + const providers = await Provider.list().then((x) => mapValues(x, (item) => item.info)) return c.json({ providers: Object.values(providers), - default: mapValues( - providers, - (item) => Provider.sort(Object.values(item.models))[0].id, - ), + default: mapValues(providers, (item) => Provider.sort(Object.values(item.models))[0].id), }) }, ) @@ -566,7 +569,7 @@ export namespace Server { description: "Symbols", content: { "application/json": { - schema: resolver(z.unknown().array()), + schema: resolver(LSP.Symbol.array()), }, }, }, @@ -629,16 +632,7 @@ export namespace Server { description: "File status", content: { "application/json": { - schema: resolver( - z - .object({ - file: z.string(), - added: z.number().int(), - removed: z.number().int(), - status: z.enum(["added", "deleted", "modified"]), - }) - .array(), - ), + schema: resolver(File.Info.array()), }, }, }, @@ -649,6 +643,75 @@ export namespace Server { return c.json(content) }, ) + .post( + "/log", + describeRoute({ + description: "Write a log entry to the server logs", + responses: { + 200: { + description: "Log entry written successfully", + content: { + "application/json": { + schema: resolver(z.boolean()), + }, + }, + }, + }, + }), + zValidator( + "json", + z.object({ + service: z.string().openapi({ description: "Service name for the log entry" }), + level: z.enum(["debug", "info", "error", "warn"]).openapi({ description: "Log level" }), + message: z.string().openapi({ description: "Log message" }), + extra: z + .record(z.string(), z.any()) + .optional() + .openapi({ description: "Additional metadata for the log entry" }), + }), + ), + async (c) => { + const { service, level, message, extra } = c.req.valid("json") + const logger = Log.create({ service }) + + switch (level) { + case "debug": + logger.debug(message, extra) + break + case "info": + logger.info(message, extra) + break + case "error": + logger.error(message, extra) + break + case "warn": + logger.warn(message, extra) + break + } + + return c.json(true) + }, + ) + .get( + "/mode", + describeRoute({ + description: "List all modes", + responses: { + 200: { + description: "List of modes", + content: { + "application/json": { + schema: resolver(Mode.Info.array()), + }, + }, + }, + }, + }), + async (c) => { + const modes = await Mode.list() + return c.json(modes) + }, + ) return result } diff --git a/packages/opencode/src/session/index.ts b/packages/opencode/src/session/index.ts index f4e80ce9..db82e291 100644 --- a/packages/opencode/src/session/index.ts +++ b/packages/opencode/src/session/index.ts @@ -1,22 +1,23 @@ -import path from "node:path" +import path from "path" import { Decimal } from "decimal.js" import { z, ZodSchema } from "zod" import { generateText, LoadAPIKeyError, - convertToCoreMessages, streamText, tool, wrapLanguageModel, type Tool as AITool, type LanguageModelUsage, - type CoreMessage, - type UIMessage, type ProviderMetadata, - type Attachment, + type ModelMessage, + stepCountIs, + type StreamTextResult, } from "ai" import PROMPT_INITIALIZE from "../session/prompt/initialize.txt" +import PROMPT_PLAN from "../session/prompt/plan.txt" +import PROMPT_ANTHROPIC_SPOOF from "../session/prompt/anthropic_spoof.txt" import { App } from "../app/app" import { Bus } from "../bus" @@ -31,16 +32,20 @@ import type { ModelsDev } from "../provider/models" import { Share } from "../share/share" import { Snapshot } from "../snapshot" import { Storage } from "../storage/storage" -import type { Tool } from "../tool/tool" import { Log } from "../util/log" import { NamedError } from "../util/error" -import { Message } from "./message" import { SystemPrompt } from "./system" import { FileTime } from "../file/time" +import { MessageV2 } from "./message-v2" +import { Mode } from "./mode" +import { LSP } from "../lsp" +import { ReadTool } from "../tool/read" export namespace Session { const log = Log.create({ service: "session" }) + const OUTPUT_TOKEN_MAX = 32_000 + export const Info = z .object({ id: Identifier.schema("session"), @@ -101,7 +106,8 @@ export namespace Session { Error: Bus.event( "session.error", z.object({ - error: Message.Info.shape.metadata.shape.error, + sessionID: z.string().optional(), + error: MessageV2.Assistant.shape.error, }), ), } @@ -110,7 +116,7 @@ export namespace Session { "session", () => { const sessions = new Map() - const messages = new Map() + const messages = new Map() const pending = new Map() return { @@ -131,9 +137,7 @@ export namespace Session { id: Identifier.descending("session"), version: Installation.VERSION, parentID, - title: - (parentID ? "Child session - " : "New Session - ") + - new Date().toISOString(), + title: (parentID ? "Child session - " : "New Session - ") + new Date().toISOString(), time: { created: Date.now(), updated: Date.now(), @@ -143,12 +147,16 @@ export namespace Session { state().sessions.set(result.id, result) await Storage.writeJSON("session/info/" + result.id, result) const cfg = await Config.get() - if (!result.parentID && (Flag.OPENCODE_AUTO_SHARE || cfg.autoshare)) - share(result.id).then((share) => { - update(result.id, (draft) => { - draft.share = share + if (!result.parentID && (Flag.OPENCODE_AUTO_SHARE || cfg.share === "auto")) + share(result.id) + .then((share) => { + update(result.id, (draft) => { + draft.share = share + }) + }) + .catch(() => { + // Silently ignore sharing errors during session creation }) - }) Bus.publish(Event.Updated, { info: result, }) @@ -170,6 +178,11 @@ export namespace Session { } export async function share(id: string) { + const cfg = await Config.get() + if (cfg.share === "disabled") { + throw new Error("Sharing is disabled in configuration") + } + const session = await get(id) if (session.share) return session.share const share = await Share.create(id) @@ -181,7 +194,10 @@ export namespace Session { await Storage.writeJSON("session/share/" + id, share) await Share.sync("session/info/" + id, session) for (const msg of await messages(id)) { - await Share.sync("session/message/" + id + "/" + msg.id, msg) + await Share.sync("session/message/" + id + "/" + msg.info.id, msg.info) + for (const part of msg.parts) { + await Share.sync("session/part/" + id + "/" + msg.info.id + "/" + part.id, part) + } } return share } @@ -211,24 +227,37 @@ export namespace Session { } export async function messages(sessionID: string) { - const result = [] as Message.Info[] - const list = Storage.list("session/message/" + sessionID) - for await (const p of list) { - const read = await Storage.readJSON(p) + const result = [] as { + info: MessageV2.Info + parts: MessageV2.Part[] + }[] + for (const p of await Storage.list("session/message/" + sessionID)) { + const read = await Storage.readJSON(p) + result.push({ + info: read, + parts: await parts(sessionID, read.id), + }) + } + result.sort((a, b) => (a.info.id > b.info.id ? 1 : -1)) + return result + } + + export async function getMessage(sessionID: string, messageID: string) { + return Storage.readJSON("session/message/" + sessionID + "/" + messageID) + } + + export async function parts(sessionID: string, messageID: string) { + const result = [] as MessageV2.Part[] + for (const item of await Storage.list("session/part/" + sessionID + "/" + messageID)) { + const read = await Storage.readJSON(item) result.push(read) } result.sort((a, b) => (a.id > b.id ? 1 : -1)) return result } - export async function getMessage(sessionID: string, messageID: string) { - return Storage.readJSON( - "session/message/" + sessionID + "/" + messageID, - ) - } - export async function* list() { - for await (const item of Storage.list("session/info")) { + for (const item of await Storage.list("session/info")) { const sessionID = path.basename(item, ".json") yield get(sessionID) } @@ -236,7 +265,7 @@ export namespace Session { export async function children(parentID: string) { const result = [] as Session.Info[] - for await (const item of Storage.list("session/info")) { + for (const item of await Storage.list("session/info")) { const sessionID = path.basename(item, ".json") const session = await get(sessionID) if (session.parentID !== parentID) continue @@ -275,26 +304,32 @@ export namespace Session { } } - async function updateMessage(msg: Message.Info) { - await Storage.writeJSON( - "session/message/" + msg.metadata.sessionID + "/" + msg.id, - msg, - ) - Bus.publish(Message.Event.Updated, { + async function updateMessage(msg: MessageV2.Info) { + await Storage.writeJSON("session/message/" + msg.sessionID + "/" + msg.id, msg) + Bus.publish(MessageV2.Event.Updated, { info: msg, }) } + async function updatePart(part: MessageV2.Part) { + await Storage.writeJSON(["session", "part", part.sessionID, part.messageID, part.id].join("/"), part) + Bus.publish(MessageV2.Event.PartUpdated, { + part, + }) + return part + } + export async function chat(input: { sessionID: string + messageID: string providerID: string modelID: string - parts: Message.MessagePart[] - system?: string[] - tools?: Tool.Info[] + mode?: string + parts: (MessageV2.TextPart | MessageV2.FilePart)[] }) { const l = log.clone().tag("session", input.sessionID) l.info("chatting") + const model = await Provider.getModel(input.providerID, input.modelID) let msgs = await messages(input.sessionID) const session = await get(input.sessionID) @@ -303,20 +338,18 @@ export namespace Session { const trimmed = [] for (const msg of msgs) { if ( - msg.id > session.revert.messageID || - (msg.id === session.revert.messageID && session.revert.part === 0) + msg.info.id > session.revert.messageID || + (msg.info.id === session.revert.messageID && session.revert.part === 0) ) { - await Storage.remove( - "session/message/" + input.sessionID + "/" + msg.id, - ) - await Bus.publish(Message.Event.Removed, { + await Storage.remove("session/message/" + input.sessionID + "/" + msg.info.id) + await Bus.publish(MessageV2.Event.Removed, { sessionID: input.sessionID, - messageID: msg.id, + messageID: msg.info.id, }) continue } - if (msg.id === session.revert.messageID) { + if (msg.info.id === session.revert.messageID) { if (session.revert.part === 0) break msg.parts = msg.parts.slice(0, session.revert.part) } @@ -328,23 +361,14 @@ export namespace Session { }) } - const previous = msgs.at(-1) + const previous = msgs.filter((x) => x.info.role === "assistant").at(-1)?.info as MessageV2.Assistant + const outputLimit = Math.min(model.info.limit.output, OUTPUT_TOKEN_MAX) || OUTPUT_TOKEN_MAX // auto summarize if too long - if (previous?.metadata.assistant) { + if (previous && previous.tokens) { const tokens = - previous.metadata.assistant.tokens.input + - previous.metadata.assistant.tokens.cache.read + - previous.metadata.assistant.tokens.cache.write + - previous.metadata.assistant.tokens.output - if ( - model.info.limit.context && - tokens > - Math.max( - (model.info.limit.context - (model.info.limit.output ?? 0)) * 0.9, - 0, - ) - ) { + previous.tokens.input + previous.tokens.cache.read + previous.tokens.cache.write + previous.tokens.output + if (model.info.limit.context && tokens > Math.max((model.info.limit.context - outputLimit) * 0.9, 0)) { await summarize({ sessionID: input.sessionID, providerID: input.providerID, @@ -356,58 +380,111 @@ export namespace Session { using abort = lock(input.sessionID) - const lastSummary = msgs.findLast( - (msg) => msg.metadata.assistant?.summary === true, - ) - if (lastSummary) msgs = msgs.filter((msg) => msg.id >= lastSummary.id) + const lastSummary = msgs.findLast((msg) => msg.info.role === "assistant" && msg.info.summary === true) + if (lastSummary) msgs = msgs.filter((msg) => msg.info.id >= lastSummary.info.id) + + const userMsg: MessageV2.Info = { + id: input.messageID, + role: "user", + sessionID: input.sessionID, + time: { + created: Date.now(), + }, + } const app = App.info() - input.parts = await Promise.all( - input.parts.map(async (part): Promise => { + const userParts = await Promise.all( + input.parts.map(async (part): Promise => { if (part.type === "file") { const url = new URL(part.url) switch (url.protocol) { case "file:": - const filepath = path.join(app.path.cwd, url.pathname) - let file = Bun.file(filepath) + // have to normalize, symbol search returns absolute paths + // Decode the pathname since URL constructor doesn't automatically decode it + const pathname = decodeURIComponent(url.pathname) + const relativePath = pathname.replace(app.path.cwd, ".") + const filePath = path.join(app.path.cwd, relativePath) - if (part.mediaType === "text/plain") { - let text = await file.text() + if (part.mime === "text/plain") { + let offset: number | undefined = undefined + let limit: number | undefined = undefined const range = { start: url.searchParams.get("start"), end: url.searchParams.get("end"), } - if (range.start != null && part.mediaType === "text/plain") { - const lines = text.split("\n") - const start = parseInt(range.start) - const end = range.end ? parseInt(range.end) : lines.length - text = lines.slice(start, end).join("\n") + if (range.start != null) { + const filePath = part.url.split("?")[0] + let start = parseInt(range.start) + let end = range.end ? parseInt(range.end) : undefined + // some LSP servers (eg, gopls) don't give full range in + // workspace/symbol searches, so we'll try to find the + // symbol in the document to get the full range + if (start === end) { + const symbols = await LSP.documentSymbol(filePath) + for (const symbol of symbols) { + let range: LSP.Range | undefined + if ("range" in symbol) { + range = symbol.range + } else if ("location" in symbol) { + range = symbol.location.range + } + if (range?.start?.line && range?.start?.line === start) { + start = range.start.line + end = range?.end?.line ?? start + break + } + } + offset = Math.max(start - 2, 0) + if (end) { + limit = end - offset + 2 + } + } } - FileTime.read(input.sessionID, filepath) + const args = { filePath, offset, limit } + const result = await ReadTool.execute(args, { + sessionID: input.sessionID, + abort: abort.signal, + messageID: userMsg.id, + metadata: async () => {}, + }) return [ { + id: Identifier.ascending("part"), + messageID: userMsg.id, + sessionID: input.sessionID, type: "text", - text: [ - "Called the Read tool on " + url.pathname, - "", - text, - "", - ].join("\n"), + synthetic: true, + text: `Called the Read tool with the following input: ${JSON.stringify(args)}`, + }, + { + id: Identifier.ascending("part"), + messageID: userMsg.id, + sessionID: input.sessionID, + type: "text", + synthetic: true, + text: result.output, }, ] } + let file = Bun.file(filePath) + FileTime.read(input.sessionID, filePath) return [ { + id: Identifier.ascending("part"), + messageID: userMsg.id, + sessionID: input.sessionID, type: "text", - text: ["Called the Read tool on " + url.pathname].join("\n"), + text: `Called the Read tool with the following input: {\"filePath\":\"${pathname}\"}`, + synthetic: true, }, { + id: Identifier.ascending("part"), + messageID: userMsg.id, + sessionID: input.sessionID, type: "file", - url: - `data:${part.mediaType};base64,` + - Buffer.from(await file.bytes()).toString("base64url"), - mediaType: part.mediaType, + url: `data:${part.mime};base64,` + Buffer.from(await file.bytes()).toString("base64"), + mime: part.mime, filename: part.filename!, }, ] @@ -416,26 +493,44 @@ export namespace Session { return [part] }), ).then((x) => x.flat()) + + if (input.mode === "plan") + userParts.push({ + id: Identifier.ascending("part"), + messageID: userMsg.id, + sessionID: input.sessionID, + type: "text", + text: PROMPT_PLAN, + synthetic: true, + }) + if (msgs.length === 0 && !session.parentID) { + const small = (await Provider.getSmallModel(input.providerID)) ?? model generateText({ - maxTokens: input.providerID === "google" ? 1024 : 20, - providerOptions: model.info.options, + maxOutputTokens: input.providerID === "google" ? 1024 : 20, + providerOptions: small.info.options, messages: [ ...SystemPrompt.title(input.providerID).map( - (x): CoreMessage => ({ + (x): ModelMessage => ({ role: "system", content: x, }), ), - ...convertToCoreMessages([ + ...MessageV2.toModelMessage([ { - role: "user", - content: "", - parts: toParts(input.parts).parts, + info: { + id: Identifier.ascending("message"), + role: "user", + sessionID: input.sessionID, + time: { + created: Date.now(), + }, + }, + parts: userParts, }, ]), ], - model: model.language, + model: small.language, }) .then((result) => { if (result.text) @@ -445,217 +540,128 @@ export namespace Session { }) .catch(() => {}) } - const snapshot = await Snapshot.create(input.sessionID) - const msg: Message.Info = { - role: "user", - id: Identifier.ascending("message"), - parts: input.parts, - metadata: { - time: { - created: Date.now(), - }, - sessionID: input.sessionID, - tool: {}, - snapshot, - }, + await updateMessage(userMsg) + for (const part of userParts) { + await updatePart(part) } - await updateMessage(msg) - msgs.push(msg) + msgs.push({ info: userMsg, parts: userParts }) - const system = input.system ?? SystemPrompt.provider(input.providerID) + const mode = await Mode.get(input.mode ?? "build") + let system = input.providerID === "anthropic" ? [PROMPT_ANTHROPIC_SPOOF.trim()] : [] + system.push(...(mode.prompt ? [mode.prompt] : SystemPrompt.provider(input.modelID))) system.push(...(await SystemPrompt.environment())) system.push(...(await SystemPrompt.custom())) + // max 2 system prompt messages for caching purposes + const [first, ...rest] = system + system = [first, rest.join("\n")] - const next: Message.Info = { + const assistantMsg: MessageV2.Info = { id: Identifier.ascending("message"), role: "assistant", - parts: [], - metadata: { - snapshot, - assistant: { - system, - path: { - cwd: app.path.cwd, - root: app.path.root, - }, - cost: 0, - tokens: { - input: 0, - output: 0, - reasoning: 0, - cache: { read: 0, write: 0 }, - }, - modelID: input.modelID, - providerID: input.providerID, - }, - time: { - created: Date.now(), - }, - sessionID: input.sessionID, - tool: {}, + system, + path: { + cwd: app.path.cwd, + root: app.path.root, }, + cost: 0, + tokens: { + input: 0, + output: 0, + reasoning: 0, + cache: { read: 0, write: 0 }, + }, + modelID: input.modelID, + providerID: input.providerID, + time: { + created: Date.now(), + }, + sessionID: input.sessionID, } - await updateMessage(next) + await updateMessage(assistantMsg) const tools: Record = {} + const processor = createProcessor(assistantMsg, model.info) + for (const item of await Provider.tools(input.providerID)) { - tools[item.id.replaceAll(".", "_")] = tool({ + if (mode.tools[item.id] === false) continue + if (session.parentID && item.id === "task") continue + tools[item.id] = tool({ id: item.id as any, description: item.description, - parameters: item.parameters as ZodSchema, - async execute(args, opts) { - const start = Date.now() - try { - const result = await item.execute(args, { - sessionID: input.sessionID, - abort: abort.signal, - messageID: next.id, - metadata: async (val) => { - next.metadata.tool[opts.toolCallId] = { - ...val, - time: { - start: 0, - end: 0, + inputSchema: item.parameters as ZodSchema, + async execute(args, options) { + const result = await item.execute(args, { + sessionID: input.sessionID, + abort: abort.signal, + messageID: assistantMsg.id, + metadata: async (val) => { + const match = processor.partFromToolCall(options.toolCallId) + if (match && match.state.status === "running") { + await updatePart({ + ...match, + state: { + title: val.title, + metadata: val.metadata, + status: "running", + input: args, + time: { + start: Date.now(), + }, }, - } - await updateMessage(next) - }, - }) - next.metadata!.tool![opts.toolCallId] = { - ...result.metadata, - snapshot: await Snapshot.create(input.sessionID), - time: { - start, - end: Date.now(), - }, - } - await updateMessage(next) - return result.output - } catch (e: any) { - next.metadata!.tool![opts.toolCallId] = { - error: true, - message: e.toString(), - title: e.toString(), - snapshot: await Snapshot.create(input.sessionID), - time: { - start, - end: Date.now(), - }, - } - await updateMessage(next) - return e.toString() + }) + } + }, + }) + return result + }, + toModelOutput(result) { + return { + type: "text", + value: result.output, } }, }) } for (const [key, item] of Object.entries(await MCP.tools())) { + if (mode.tools[key] === false) continue const execute = item.execute if (!execute) continue item.execute = async (args, opts) => { - const start = Date.now() - try { - const result = await execute(args, opts) - next.metadata!.tool![opts.toolCallId] = { - ...result.metadata, - snapshot: await Snapshot.create(input.sessionID), - time: { - start, - end: Date.now(), - }, - } - await updateMessage(next) - return result.content - .filter((x: any) => x.type === "text") - .map((x: any) => x.text) - .join("\n\n") - } catch (e: any) { - next.metadata!.tool![opts.toolCallId] = { - error: true, - message: e.toString(), - snapshot: await Snapshot.create(input.sessionID), - title: "mcp", - time: { - start, - end: Date.now(), - }, - } - await updateMessage(next) - return e.toString() + const result = await execute(args, opts) + const output = result.content + .filter((x: any) => x.type === "text") + .map((x: any) => x.text) + .join("\n\n") + + return { + output, + } + } + item.toModelOutput = (result) => { + return { + type: "text", + value: result.output, } } tools[key] = item } - let text: Message.TextPart | undefined - const result = streamText({ - onStepFinish: async (step) => { - log.info("step finish", { finishReason: step.finishReason }) - const assistant = next.metadata!.assistant! - const usage = getUsage(model.info, step.usage, step.providerMetadata) - assistant.cost += usage.cost - assistant.tokens = usage.tokens - await updateMessage(next) - if (text) { - Bus.publish(Message.Event.PartUpdated, { - part: text, - messageID: next.id, - sessionID: next.metadata.sessionID, - }) - } - text = undefined - }, - onError(err) { - log.error("callback error", err) - switch (true) { - case LoadAPIKeyError.isInstance(err.error): - next.metadata.error = new Provider.AuthError( - { - providerID: input.providerID, - message: err.error.message, - }, - { cause: err.error }, - ).toObject() - break - case err.error instanceof Error: - next.metadata.error = new NamedError.Unknown( - { message: err.error.toString() }, - { cause: err.error }, - ).toObject() - break - default: - next.metadata.error = new NamedError.Unknown( - { message: JSON.stringify(err.error) }, - { cause: err.error }, - ) - } - Bus.publish(Event.Error, { - error: next.metadata.error, - }) - }, - // async prepareStep(step) { - // next.parts.push({ - // type: "step-start", - // }) - // await updateMessage(next) - // return step - // }, - toolCallStreaming: true, + const stream = streamText({ + onError() {}, maxRetries: 10, - maxTokens: Math.max(0, model.info.limit.output) || undefined, + maxOutputTokens: outputLimit, abortSignal: abort.signal, - maxSteps: 1000, + stopWhen: stepCountIs(1000), providerOptions: model.info.options, messages: [ ...system.map( - (x): CoreMessage => ({ + (x): ModelMessage => ({ role: "system", content: x, }), ), - ...convertToCoreMessages( - msgs.map(toUIMessage).filter((x) => x.parts.length > 0), - ), + ...MessageV2.toModelMessage(msgs), ], temperature: model.info.temperature ? 0 : undefined, tools: model.info.tool_call === false ? undefined : tools, @@ -665,11 +671,8 @@ export namespace Session { { async transformParams(args) { if (args.type === "stream") { - args.params.prompt = ProviderTransform.message( - args.params.prompt, - input.providerID, - input.modelID, - ) + // @ts-expect-error + args.params.prompt = ProviderTransform.message(args.params.prompt, input.providerID, input.modelID) } return args.params }, @@ -677,173 +680,265 @@ export namespace Session { ], }), }) - try { - for await (const value of result.fullStream) { - l.info("part", { - type: value.type, - }) - switch (value.type) { - case "step-start": - next.parts.push({ - type: "step-start", - }) - break - case "text-delta": - if (!text) { - text = { - type: "text", - text: value.textDelta, - } - next.parts.push(text) - break - } else text.text += value.textDelta - break - - case "tool-call": { - const [match] = next.parts.flatMap((p) => - p.type === "tool-invocation" && - p.toolInvocation.toolCallId === value.toolCallId - ? [p] - : [], - ) - if (!match) break - match.toolInvocation.args = value.args - match.toolInvocation.state = "call" - Bus.publish(Message.Event.PartUpdated, { - part: match, - messageID: next.id, - sessionID: next.metadata.sessionID, - }) - break - } - - case "tool-call-streaming-start": - next.parts.push({ - type: "tool-invocation", - toolInvocation: { - state: "partial-call", - toolName: value.toolName, - toolCallId: value.toolCallId, - args: {}, - }, - }) - Bus.publish(Message.Event.PartUpdated, { - part: next.parts[next.parts.length - 1], - messageID: next.id, - sessionID: next.metadata.sessionID, - }) - break - - case "tool-call-delta": - continue - - // for some reason ai sdk claims to not send this part but it does - // @ts-expect-error - case "tool-result": - const match = next.parts.find( - (p) => - p.type === "tool-invocation" && - // @ts-expect-error - p.toolInvocation.toolCallId === value.toolCallId, - ) - if (match && match.type === "tool-invocation") { - match.toolInvocation = { - // @ts-expect-error - args: value.args, - // @ts-expect-error - toolCallId: value.toolCallId, - // @ts-expect-error - toolName: value.toolName, - state: "result", - // @ts-expect-error - result: value.result as string, - } - Bus.publish(Message.Event.PartUpdated, { - part: match, - messageID: next.id, - sessionID: next.metadata.sessionID, - }) - } - break - - case "finish": - log.info("message finish", { - reason: value.finishReason, - }) - const assistant = next.metadata!.assistant! - const usage = getUsage( - model.info, - value.usage, - value.providerMetadata, - ) - assistant.cost += usage.cost - await updateMessage(next) - if (value.finishReason === "length") - throw new Message.OutputLengthError({}) - break - default: - l.info("unhandled", { - type: value.type, - }) - continue - } - await updateMessage(next) - } - } catch (e: any) { - log.error("stream error", { - error: e, - }) - switch (true) { - case Message.OutputLengthError.isInstance(e): - next.metadata.error = e - break - case LoadAPIKeyError.isInstance(e): - next.metadata.error = new Provider.AuthError( - { - providerID: input.providerID, - message: e.message, - }, - { cause: e }, - ).toObject() - break - case e instanceof Error: - next.metadata.error = new NamedError.Unknown( - { message: e.toString() }, - { cause: e }, - ).toObject() - break - default: - next.metadata.error = new NamedError.Unknown( - { message: JSON.stringify(e) }, - { cause: e }, - ) - } - Bus.publish(Event.Error, { - error: next.metadata.error, - }) - } - next.metadata!.time.completed = Date.now() - for (const part of next.parts) { - if ( - part.type === "tool-invocation" && - part.toolInvocation.state !== "result" - ) { - part.toolInvocation = { - ...part.toolInvocation, - state: "result", - result: "request was aborted", - } - } - } - await updateMessage(next) - return next + const result = await processor.process(stream) + return result } - export async function revert(input: { - sessionID: string - messageID: string - part: number - }) { + function createProcessor(assistantMsg: MessageV2.Assistant, model: ModelsDev.Model) { + const toolCalls: Record = {} + return { + partFromToolCall(toolCallID: string) { + return toolCalls[toolCallID] + }, + async process(stream: StreamTextResult, never>) { + try { + let currentText: MessageV2.TextPart | undefined + + for await (const value of stream.fullStream) { + log.info("part", { + type: value.type, + }) + switch (value.type) { + case "start": + const snapshot = await Snapshot.create(assistantMsg.sessionID) + if (snapshot) + await updatePart({ + id: Identifier.ascending("part"), + messageID: assistantMsg.id, + sessionID: assistantMsg.sessionID, + type: "snapshot", + snapshot, + }) + break + + case "tool-input-start": + const part = await updatePart({ + id: Identifier.ascending("part"), + messageID: assistantMsg.id, + sessionID: assistantMsg.sessionID, + type: "tool", + tool: value.toolName, + callID: value.id, + state: { + status: "pending", + }, + }) + toolCalls[value.id] = part as MessageV2.ToolPart + break + + case "tool-input-delta": + break + + case "tool-call": { + const match = toolCalls[value.toolCallId] + if (match) { + const part = await updatePart({ + ...match, + state: { + status: "running", + input: value.input, + time: { + start: Date.now(), + }, + }, + }) + toolCalls[value.toolCallId] = part as MessageV2.ToolPart + } + break + } + case "tool-result": { + const match = toolCalls[value.toolCallId] + if (match && match.state.status === "running") { + await updatePart({ + ...match, + state: { + status: "completed", + input: value.input, + output: value.output.output, + metadata: value.output.metadata, + title: value.output.title, + time: { + start: match.state.time.start, + end: Date.now(), + }, + }, + }) + delete toolCalls[value.toolCallId] + const snapshot = await Snapshot.create(assistantMsg.sessionID) + if (snapshot) + await updatePart({ + id: Identifier.ascending("part"), + messageID: assistantMsg.id, + sessionID: assistantMsg.sessionID, + type: "snapshot", + snapshot, + }) + } + break + } + + case "tool-error": { + const match = toolCalls[value.toolCallId] + if (match && match.state.status === "running") { + await updatePart({ + ...match, + state: { + status: "error", + input: value.input, + error: (value.error as any).toString(), + time: { + start: match.state.time.start, + end: Date.now(), + }, + }, + }) + delete toolCalls[value.toolCallId] + const snapshot = await Snapshot.create(assistantMsg.sessionID) + if (snapshot) + await updatePart({ + id: Identifier.ascending("part"), + messageID: assistantMsg.id, + sessionID: assistantMsg.sessionID, + type: "snapshot", + snapshot, + }) + } + break + } + + case "error": + throw value.error + + case "start-step": + await updatePart({ + id: Identifier.ascending("part"), + messageID: assistantMsg.id, + sessionID: assistantMsg.sessionID, + type: "step-start", + }) + break + + case "finish-step": + const usage = getUsage(model, value.usage, value.providerMetadata) + assistantMsg.cost += usage.cost + assistantMsg.tokens = usage.tokens + await updatePart({ + id: Identifier.ascending("part"), + messageID: assistantMsg.id, + sessionID: assistantMsg.sessionID, + type: "step-finish", + tokens: usage.tokens, + cost: usage.cost, + }) + await updateMessage(assistantMsg) + break + + case "text-start": + currentText = { + id: Identifier.ascending("part"), + messageID: assistantMsg.id, + sessionID: assistantMsg.sessionID, + type: "text", + text: "", + time: { + start: Date.now(), + }, + } + break + + case "text": + if (currentText) { + currentText.text += value.text + await updatePart(currentText) + } + break + + case "text-end": + if (currentText && currentText.text) { + currentText.time = { + start: Date.now(), + end: Date.now(), + } + await updatePart(currentText) + } + currentText = undefined + break + + case "finish": + assistantMsg.time.completed = Date.now() + await updateMessage(assistantMsg) + break + + default: + log.info("unhandled", { + ...value, + }) + continue + } + } + } catch (e) { + log.error("", { + error: e, + }) + switch (true) { + case e instanceof DOMException && e.name === "AbortError": + assistantMsg.error = new MessageV2.AbortedError( + { message: e.message }, + { + cause: e, + }, + ).toObject() + break + case MessageV2.OutputLengthError.isInstance(e): + assistantMsg.error = e + break + case LoadAPIKeyError.isInstance(e): + assistantMsg.error = new MessageV2.AuthError( + { + providerID: model.id, + message: e.message, + }, + { cause: e }, + ).toObject() + break + case e instanceof Error: + assistantMsg.error = new NamedError.Unknown({ message: e.toString() }, { cause: e }).toObject() + break + default: + assistantMsg.error = new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e }) + } + Bus.publish(Event.Error, { + sessionID: assistantMsg.sessionID, + error: assistantMsg.error, + }) + } + const p = await parts(assistantMsg.sessionID, assistantMsg.id) + for (const part of p) { + if (part.type === "tool" && part.state.status !== "completed") { + updatePart({ + ...part, + state: { + status: "error", + error: "Tool execution aborted", + time: { + start: Date.now(), + end: Date.now(), + }, + input: {}, + }, + }) + } + } + assistantMsg.time.completed = Date.now() + await updateMessage(assistantMsg) + return { info: assistantMsg, parts: p } + }, + } + } + + export async function revert(_input: { sessionID: string; messageID: string; part: number }) { + // TODO + /* const message = await getMessage(input.sessionID, input.messageID) if (!message) return const part = message.parts[input.part] @@ -871,77 +966,66 @@ export namespace Session { snapshot, } }) + */ } export async function unrevert(sessionID: string) { const session = await get(sessionID) if (!session) return if (!session.revert) return - if (session.revert.snapshot) - await Snapshot.restore(sessionID, session.revert.snapshot) + if (session.revert.snapshot) await Snapshot.restore(sessionID, session.revert.snapshot) update(sessionID, (draft) => { draft.revert = undefined }) } - export async function summarize(input: { - sessionID: string - providerID: string - modelID: string - }) { + export async function summarize(input: { sessionID: string; providerID: string; modelID: string }) { using abort = lock(input.sessionID) const msgs = await messages(input.sessionID) - const lastSummary = msgs.findLast( - (msg) => msg.metadata.assistant?.summary === true, - )?.id - const filtered = msgs.filter((msg) => !lastSummary || msg.id >= lastSummary) + const lastSummary = msgs.findLast((msg) => msg.info.role === "assistant" && msg.info.summary === true) + const filtered = msgs.filter((msg) => !lastSummary || msg.info.id >= lastSummary.info.id) const model = await Provider.getModel(input.providerID, input.modelID) const app = App.info() const system = SystemPrompt.summarize(input.providerID) - const next: Message.Info = { + const next: MessageV2.Info = { id: Identifier.ascending("message"), role: "assistant", - parts: [], - metadata: { - tool: {}, - sessionID: input.sessionID, - assistant: { - system, - path: { - cwd: app.path.cwd, - root: app.path.root, - }, - summary: true, - cost: 0, - modelID: input.modelID, - providerID: input.providerID, - tokens: { - input: 0, - output: 0, - reasoning: 0, - cache: { read: 0, write: 0 }, - }, - }, - time: { - created: Date.now(), - }, + sessionID: input.sessionID, + system, + path: { + cwd: app.path.cwd, + root: app.path.root, + }, + summary: true, + cost: 0, + modelID: input.modelID, + providerID: input.providerID, + tokens: { + input: 0, + output: 0, + reasoning: 0, + cache: { read: 0, write: 0 }, + }, + time: { + created: Date.now(), }, } await updateMessage(next) - let text: Message.TextPart | undefined - const result = streamText({ + const processor = createProcessor(next, model.info) + const stream = streamText({ + maxRetries: 10, abortSignal: abort.signal, model: model.language, messages: [ ...system.map( - (x): CoreMessage => ({ + (x): ModelMessage => ({ role: "system", content: x, }), ), - ...convertToCoreMessages(filtered.map(toUIMessage)), + ...MessageV2.toModelMessage(filtered), { role: "user", content: [ @@ -952,46 +1036,10 @@ export namespace Session { ], }, ], - onStepFinish: async (step) => { - const assistant = next.metadata!.assistant! - const usage = getUsage(model.info, step.usage, step.providerMetadata) - assistant.cost += usage.cost - assistant.tokens = usage.tokens - await updateMessage(next) - if (text) { - Bus.publish(Message.Event.PartUpdated, { - part: text, - messageID: next.id, - sessionID: next.metadata.sessionID, - }) - } - text = undefined - }, - async onFinish(input) { - const assistant = next.metadata!.assistant! - const usage = getUsage(model.info, input.usage, input.providerMetadata) - assistant.cost += usage.cost - assistant.tokens = usage.tokens - next.metadata!.time.completed = Date.now() - await updateMessage(next) - }, }) - for await (const value of result.fullStream) { - switch (value.type) { - case "text-delta": - if (!text) { - text = { - type: "text", - text: value.textDelta, - } - next.parts.push(text) - } else text.text += value.textDelta - - await updateMessage(next) - break - } - } + const result = await processor.process(stream) + return result } function lock(sessionID: string) { @@ -1011,40 +1059,25 @@ export namespace Session { } } - function getUsage( - model: ModelsDev.Model, - usage: LanguageModelUsage, - metadata?: ProviderMetadata, - ) { + function getUsage(model: ModelsDev.Model, usage: LanguageModelUsage, metadata?: ProviderMetadata) { const tokens = { - input: usage.promptTokens ?? 0, - output: usage.completionTokens ?? 0, + input: usage.inputTokens ?? 0, + output: usage.outputTokens ?? 0, reasoning: 0, cache: { write: (metadata?.["anthropic"]?.["cacheCreationInputTokens"] ?? // @ts-expect-error metadata?.["bedrock"]?.["usage"]?.["cacheWriteInputTokens"] ?? 0) as number, - read: (metadata?.["anthropic"]?.["cacheReadInputTokens"] ?? - // @ts-expect-error - metadata?.["bedrock"]?.["usage"]?.["cacheReadInputTokens"] ?? - 0) as number, + read: usage.cachedInputTokens ?? 0, }, } return { cost: new Decimal(0) .add(new Decimal(tokens.input).mul(model.cost.input).div(1_000_000)) .add(new Decimal(tokens.output).mul(model.cost.output).div(1_000_000)) - .add( - new Decimal(tokens.cache.read) - .mul(model.cost.cache_read ?? 0) - .div(1_000_000), - ) - .add( - new Decimal(tokens.cache.write) - .mul(model.cost.cache_write ?? 0) - .div(1_000_000), - ) + .add(new Decimal(tokens.cache.read).mul(model.cost.cache_read ?? 0).div(1_000_000)) + .add(new Decimal(tokens.cache.write).mul(model.cost.cache_write ?? 0).div(1_000_000)) .toNumber(), tokens, } @@ -1060,14 +1093,19 @@ export namespace Session { sessionID: string modelID: string providerID: string + messageID: string }) { const app = App.info() await Session.chat({ sessionID: input.sessionID, + messageID: input.messageID, providerID: input.providerID, modelID: input.modelID, parts: [ { + id: Identifier.ascending("part"), + sessionID: input.sessionID, + messageID: input.messageID, type: "text", text: PROMPT_INITIALIZE.replace("${path}", app.path.root), }, @@ -1076,63 +1114,3 @@ export namespace Session { await App.initialize() } } - -function toUIMessage(msg: Message.Info): UIMessage { - if (msg.role === "assistant") { - return { - id: msg.id, - role: "assistant", - content: "", - ...toParts(msg.parts), - } - } - - if (msg.role === "user") { - return { - id: msg.id, - role: "user", - content: "", - ...toParts(msg.parts), - } - } - - throw new Error("not implemented") -} - -function toParts(parts: Message.MessagePart[]) { - const result: { - parts: UIMessage["parts"] - experimental_attachments: Attachment[] - } = { - parts: [], - experimental_attachments: [], - } - for (const part of parts) { - switch (part.type) { - case "text": - result.parts.push({ type: "text", text: part.text }) - break - case "file": - result.experimental_attachments.push({ - url: part.url, - contentType: part.mediaType, - name: part.filename, - }) - break - case "tool-invocation": - result.parts.push({ - type: "tool-invocation", - toolInvocation: part.toolInvocation, - }) - break - case "step-start": - result.parts.push({ - type: "step-start", - }) - break - default: - break - } - } - return result -} diff --git a/packages/opencode/src/session/message-v2.ts b/packages/opencode/src/session/message-v2.ts new file mode 100644 index 00000000..c74a39a7 --- /dev/null +++ b/packages/opencode/src/session/message-v2.ts @@ -0,0 +1,460 @@ +import z from "zod" +import { Bus } from "../bus" +import { NamedError } from "../util/error" +import { Message } from "./message" +import { convertToModelMessages, type ModelMessage, type UIMessage } from "ai" +import { Identifier } from "../id/id" + +export namespace MessageV2 { + export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({})) + export const AbortedError = NamedError.create("MessageAbortedError", z.object({})) + export const AuthError = NamedError.create( + "ProviderAuthError", + z.object({ + providerID: z.string(), + message: z.string(), + }), + ) + + export const ToolStatePending = z + .object({ + status: z.literal("pending"), + }) + .openapi({ + ref: "ToolStatePending", + }) + + export type ToolStatePending = z.infer + + export const ToolStateRunning = z + .object({ + status: z.literal("running"), + input: z.any(), + title: z.string().optional(), + metadata: z.record(z.any()).optional(), + time: z.object({ + start: z.number(), + }), + }) + .openapi({ + ref: "ToolStateRunning", + }) + export type ToolStateRunning = z.infer + + export const ToolStateCompleted = z + .object({ + status: z.literal("completed"), + input: z.record(z.any()), + output: z.string(), + title: z.string(), + metadata: z.record(z.any()), + time: z.object({ + start: z.number(), + end: z.number(), + }), + }) + .openapi({ + ref: "ToolStateCompleted", + }) + export type ToolStateCompleted = z.infer + + export const ToolStateError = z + .object({ + status: z.literal("error"), + input: z.record(z.any()), + error: z.string(), + time: z.object({ + start: z.number(), + end: z.number(), + }), + }) + .openapi({ + ref: "ToolStateError", + }) + export type ToolStateError = z.infer + + export const ToolState = z + .discriminatedUnion("status", [ToolStatePending, ToolStateRunning, ToolStateCompleted, ToolStateError]) + .openapi({ + ref: "ToolState", + }) + + const PartBase = z.object({ + id: z.string(), + sessionID: z.string(), + messageID: z.string(), + }) + + export const SnapshotPart = PartBase.extend({ + type: z.literal("snapshot"), + snapshot: z.string(), + }).openapi({ + ref: "SnapshotPart", + }) + export type SnapshotPart = z.infer + + export const TextPart = PartBase.extend({ + type: z.literal("text"), + text: z.string(), + synthetic: z.boolean().optional(), + time: z + .object({ + start: z.number(), + end: z.number().optional(), + }) + .optional(), + }).openapi({ + ref: "TextPart", + }) + export type TextPart = z.infer + + export const ToolPart = PartBase.extend({ + type: z.literal("tool"), + callID: z.string(), + tool: z.string(), + state: ToolState, + }).openapi({ + ref: "ToolPart", + }) + export type ToolPart = z.infer + + export const FilePart = PartBase.extend({ + type: z.literal("file"), + mime: z.string(), + filename: z.string().optional(), + url: z.string(), + }).openapi({ + ref: "FilePart", + }) + export type FilePart = z.infer + + export const StepStartPart = PartBase.extend({ + type: z.literal("step-start"), + }).openapi({ + ref: "StepStartPart", + }) + export type StepStartPart = z.infer + + export const StepFinishPart = PartBase.extend({ + type: z.literal("step-finish"), + cost: z.number(), + tokens: z.object({ + input: z.number(), + output: z.number(), + reasoning: z.number(), + cache: z.object({ + read: z.number(), + write: z.number(), + }), + }), + }).openapi({ + ref: "StepFinishPart", + }) + export type StepFinishPart = z.infer + + const Base = z.object({ + id: z.string(), + sessionID: z.string(), + }) + + export const User = Base.extend({ + role: z.literal("user"), + time: z.object({ + created: z.number(), + }), + }).openapi({ + ref: "UserMessage", + }) + export type User = z.infer + + export const Part = z + .discriminatedUnion("type", [TextPart, FilePart, ToolPart, StepStartPart, StepFinishPart, SnapshotPart]) + .openapi({ + ref: "Part", + }) + export type Part = z.infer + + export const Assistant = Base.extend({ + role: z.literal("assistant"), + time: z.object({ + created: z.number(), + completed: z.number().optional(), + }), + error: z + .discriminatedUnion("name", [ + AuthError.Schema, + NamedError.Unknown.Schema, + OutputLengthError.Schema, + AbortedError.Schema, + ]) + .optional(), + system: z.string().array(), + modelID: z.string(), + providerID: z.string(), + path: z.object({ + cwd: z.string(), + root: z.string(), + }), + summary: z.boolean().optional(), + cost: z.number(), + tokens: z.object({ + input: z.number(), + output: z.number(), + reasoning: z.number(), + cache: z.object({ + read: z.number(), + write: z.number(), + }), + }), + }).openapi({ + ref: "AssistantMessage", + }) + export type Assistant = z.infer + + export const Info = z.discriminatedUnion("role", [User, Assistant]).openapi({ + ref: "Message", + }) + export type Info = z.infer + + export const Event = { + Updated: Bus.event( + "message.updated", + z.object({ + info: Info, + }), + ), + Removed: Bus.event( + "message.removed", + z.object({ + sessionID: z.string(), + messageID: z.string(), + }), + ), + PartUpdated: Bus.event( + "message.part.updated", + z.object({ + part: Part, + }), + ), + } + + export function fromV1(v1: Message.Info) { + if (v1.role === "assistant") { + const info: Assistant = { + id: v1.id, + sessionID: v1.metadata.sessionID, + role: "assistant", + time: { + created: v1.metadata.time.created, + completed: v1.metadata.time.completed, + }, + cost: v1.metadata.assistant!.cost, + path: v1.metadata.assistant!.path, + summary: v1.metadata.assistant!.summary, + tokens: v1.metadata.assistant!.tokens, + modelID: v1.metadata.assistant!.modelID, + providerID: v1.metadata.assistant!.providerID, + system: v1.metadata.assistant!.system, + error: v1.metadata.error, + } + const parts = v1.parts.flatMap((part): Part[] => { + const base = { + id: Identifier.ascending("part"), + messageID: v1.id, + sessionID: v1.metadata.sessionID, + } + if (part.type === "text") { + return [ + { + ...base, + type: "text", + text: part.text, + }, + ] + } + if (part.type === "step-start") { + return [ + { + ...base, + type: "step-start", + }, + ] + } + if (part.type === "tool-invocation") { + return [ + { + ...base, + type: "tool", + callID: part.toolInvocation.toolCallId, + tool: part.toolInvocation.toolName, + state: (() => { + if (part.toolInvocation.state === "partial-call") { + return { + status: "pending", + } + } + + const { title, time, ...metadata } = v1.metadata.tool[part.toolInvocation.toolCallId] ?? {} + if (part.toolInvocation.state === "call") { + return { + status: "running", + input: part.toolInvocation.args, + time: { + start: time?.start, + }, + } + } + + if (part.toolInvocation.state === "result") { + return { + status: "completed", + input: part.toolInvocation.args, + output: part.toolInvocation.result, + title, + time, + metadata, + } + } + throw new Error("unknown tool invocation state") + })(), + }, + ] + } + return [] + }) + return { + info, + parts, + } + } + + if (v1.role === "user") { + const info: User = { + id: v1.id, + sessionID: v1.metadata.sessionID, + role: "user", + time: { + created: v1.metadata.time.created, + }, + } + const parts = v1.parts.flatMap((part): Part[] => { + const base = { + id: Identifier.ascending("part"), + messageID: v1.id, + sessionID: v1.metadata.sessionID, + } + if (part.type === "text") { + return [ + { + ...base, + type: "text", + text: part.text, + }, + ] + } + if (part.type === "file") { + return [ + { + ...base, + type: "file", + mime: part.mediaType, + filename: part.filename, + url: part.url, + }, + ] + } + return [] + }) + return { info, parts } + } + + throw new Error("unknown message type") + } + + export function toModelMessage( + input: { + info: Info + parts: Part[] + }[], + ): ModelMessage[] { + const result: UIMessage[] = [] + + for (const msg of input) { + if (msg.parts.length === 0) continue + + if (msg.info.role === "user") { + result.push({ + id: msg.info.id, + role: "user", + parts: msg.parts.flatMap((part): UIMessage["parts"] => { + if (part.type === "text") + return [ + { + type: "text", + text: part.text, + }, + ] + if (part.type === "file") + return [ + { + type: "file", + url: part.url, + mediaType: part.mime, + filename: part.filename, + }, + ] + return [] + }), + }) + } + + if (msg.info.role === "assistant") { + result.push({ + id: msg.info.id, + role: "assistant", + parts: msg.parts.flatMap((part): UIMessage["parts"] => { + if (part.type === "text") + return [ + { + type: "text", + text: part.text, + }, + ] + if (part.type === "step-start") + return [ + { + type: "step-start", + }, + ] + if (part.type === "tool") { + if (part.state.status === "completed") + return [ + { + type: ("tool-" + part.tool) as `tool-${string}`, + state: "output-available", + toolCallId: part.callID, + input: part.state.input, + output: part.state.output, + }, + ] + if (part.state.status === "error") + return [ + { + type: ("tool-" + part.tool) as `tool-${string}`, + state: "output-error", + toolCallId: part.callID, + input: part.state.input, + errorText: part.state.error, + }, + ] + } + + return [] + }), + }) + } + } + + return convertToModelMessages(result) + } +} diff --git a/packages/opencode/src/session/message.ts b/packages/opencode/src/session/message.ts index 2d319e87..e71c35c5 100644 --- a/packages/opencode/src/session/message.ts +++ b/packages/opencode/src/session/message.ts @@ -1,12 +1,14 @@ import z from "zod" -import { Bus } from "../bus" -import { Provider } from "../provider/provider" import { NamedError } from "../util/error" export namespace Message { - export const OutputLengthError = NamedError.create( - "MessageOutputLengthError", - z.object({}), + export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({})) + export const AuthError = NamedError.create( + "ProviderAuthError", + z.object({ + providerID: z.string(), + message: z.string(), + }), ) export const ToolCall = z @@ -49,11 +51,9 @@ export namespace Message { }) export type ToolResult = z.infer - export const ToolInvocation = z - .discriminatedUnion("state", [ToolCall, ToolPartialCall, ToolResult]) - .openapi({ - ref: "ToolInvocation", - }) + export const ToolInvocation = z.discriminatedUnion("state", [ToolCall, ToolPartialCall, ToolResult]).openapi({ + ref: "ToolInvocation", + }) export type ToolInvocation = z.infer export const TextPart = z @@ -122,14 +122,7 @@ export namespace Message { export type StepStartPart = z.infer export const MessagePart = z - .discriminatedUnion("type", [ - TextPart, - ReasoningPart, - ToolInvocationPart, - SourceUrlPart, - FilePart, - StepStartPart, - ]) + .discriminatedUnion("type", [TextPart, ReasoningPart, ToolInvocationPart, SourceUrlPart, FilePart, StepStartPart]) .openapi({ ref: "MessagePart", }) @@ -147,11 +140,7 @@ export namespace Message { completed: z.number().optional(), }), error: z - .discriminatedUnion("name", [ - Provider.AuthError.Schema, - NamedError.Unknown.Schema, - OutputLengthError.Schema, - ]) + .discriminatedUnion("name", [AuthError.Schema, NamedError.Unknown.Schema, OutputLengthError.Schema]) .optional(), sessionID: z.string(), tool: z.record( @@ -197,28 +186,4 @@ export namespace Message { ref: "Message", }) export type Info = z.infer - - export const Event = { - Updated: Bus.event( - "message.updated", - z.object({ - info: Info, - }), - ), - Removed: Bus.event( - "message.removed", - z.object({ - sessionID: z.string(), - messageID: z.string(), - }), - ), - PartUpdated: Bus.event( - "message.part.updated", - z.object({ - part: MessagePart, - sessionID: z.string(), - messageID: z.string(), - }), - ), - } } diff --git a/packages/opencode/src/session/mode.ts b/packages/opencode/src/session/mode.ts new file mode 100644 index 00000000..eb9e6927 --- /dev/null +++ b/packages/opencode/src/session/mode.ts @@ -0,0 +1,69 @@ +import { mergeDeep } from "remeda" +import { App } from "../app/app" +import { Config } from "../config/config" +import z from "zod" + +export namespace Mode { + export const Info = z + .object({ + name: z.string(), + model: z + .object({ + modelID: z.string(), + providerID: z.string(), + }) + .optional(), + prompt: z.string().optional(), + tools: z.record(z.boolean()), + }) + .openapi({ + ref: "Mode", + }) + export type Info = z.infer + const state = App.state("mode", async () => { + const cfg = await Config.get() + const mode = mergeDeep( + { + build: {}, + plan: { + tools: { + write: false, + edit: false, + patch: false, + }, + }, + }, + cfg.mode ?? {}, + ) + const result: Record = {} + for (const [key, value] of Object.entries(mode)) { + let item = result[key] + if (!item) + item = result[key] = { + name: key, + tools: {}, + } + const model = value.model ?? cfg.model + if (model) { + const [providerID, ...rest] = model.split("/") + const modelID = rest.join("/") + item.model = { + modelID, + providerID, + } + } + if (value.prompt) item.prompt = value.prompt + if (value.tools) item.tools = value.tools + } + + return result + }) + + export async function get(mode: string) { + return state().then((x) => x[mode]) + } + + export async function list() { + return state().then((x) => Object.values(x)) + } +} diff --git a/packages/opencode/src/session/prompt/beast.txt b/packages/opencode/src/session/prompt/beast.txt new file mode 100644 index 00000000..473f0286 --- /dev/null +++ b/packages/opencode/src/session/prompt/beast.txt @@ -0,0 +1,117 @@ +You are opencode, an autonomous agent - please keep going until the user's query is completely resolved, before ending your turn and yielding back to the user. + +Your thinking should be thorough and so it's fine if it's very long. However, avoid unnecessary repetition and verbosity. You should be concise, but thorough. + +You MUST iterate and keep going until the problem is solved. + +You have everything you need to resolve this problem. I want you to fully solve this autonomously before coming back to me. + +Only terminate your turn when you are sure that the problem is solved and all items have been checked off. Use the TodoWrite and TodoRead tools to track and manage steps. Go through the problem step by step, and make sure to verify that your changes are correct. Once each step is finished mark it as completed with the TodoWrite tool. NEVER end your turn without having truly and completely solved the problem, use the TodoRead tool to make sure all steps are complete, and when you say you are going to make a tool call, make sure you ACTUALLY make the tool call, instead of ending your turn. If a step is impossible to complete, mark it as cancelled using the TodoWrite tool. + +THE PROBLEM CAN NOT BE SOLVED WITHOUT EXTENSIVE INTERNET RESEARCH. + +You must use the webfetch tool to recursively gather all information from URLs provided to you by the user, as well as any links you find in the content of those pages. + +Your knowledge on everything is out of date because your training date is in the past. + +You CANNOT successfully complete this task without using Bing to verify your understanding of third party packages and dependencies is up to date. You must use the webfetch tool to search bing for how to properly use libraries, packages, frameworks, dependencies, etc. every single time you install or implement one. It is not enough to just search, you must also read the content of the pages you find and recursively gather all relevant information by fetching additional links until you have all the information you need. + +If the user request is "resume" or "continue" or "try again",use the TodoRead tool to find the next pending step. Continue from that step, and do not hand back control to the user until the entire todo list is complete and all steps are marked as complete or cancelled. Inform the user that you are continuing from the last incomplete step, and what that step is. + +Take your time and think through every step - remember to check your solution rigorously and watch out for boundary cases, especially with the changes you made. Use the sequential thinking tool if available. Your solution must be perfect. If not, continue working on it. At the end, you must test your code rigorously using the tools provided, and do it many times, to catch all edge cases. If it is not robust, update the plan and iterate more and make it perfect. Failing to test your code sufficiently rigorously is the NUMBER ONE failure mode on these types of tasks; run the build, and verify that the changes you made actually build; make sure you handle all edge cases, and run existing tests if they are provided. + +You MUST plan extensively before each tool call, and reflect extensively on the outcomes of the previous tool calls. DO NOT do this entire process by making tool calls only, as this can impair your ability to solve the problem and think insightfully. + +You MUST keep working until the problem is completely solved, and all steps in the todo list are complete. Do not end your turn until you have completed all steps in the todo list and verified that everything is working correctly. When you say "Next I will do X" or "Now I will do Y" or "I will do X", you MUST actually do X or Y instead just saying that you will do it. + +You MUST use the ToolRead tool to verify that all steps are complete or cancelled before ending your turn. If any steps are incomplete, you MUST continue working on them until they are all complete. + +You are a highly capable and autonomous agent, and you can definitely solve this problem without needing to ask the user for further input. + +# Workflow +1. Fetch any URL's provided by the user using the `webfetch` tool. +2. Understand the problem deeply. Carefully read the issue and think critically about what is required. Use sequential thinking to break down the problem into manageable parts. Consider the following: + - What is the expected behavior? + - What are the edge cases? + - What are the potential pitfalls? + - How does this fit into the larger context of the codebase? + - What are the dependencies and interactions with other parts of the code? +3. Investigate the codebase. Explore relevant files, search for key functions, and gather context. +4. Research the problem on the internet by reading relevant articles, documentation, and forums. +5. Develop a clear, step-by-step plan. Break down the fix into manageable, incremental steps. Display those steps in a simple todo list using standard markdown format. Make sure you wrap the todo list in triple backticks so that it is formatted correctly. +6. Implement the fix incrementally. Make small, testable code changes. +7. Debug as needed. Use debugging techniques to isolate and resolve issues. +8. Test frequently. Run tests after each change to verify correctness. +9. Iterate until the root cause is fixed and all tests pass. +10. Reflect and validate comprehensively. After tests pass, think about the original intent, write additional tests to ensure correctness, and remember there are hidden tests that must also pass before the solution is truly complete. + +Refer to the detailed sections below for more information on each step. + +## 1. Fetch Provided URLs +- If the user provides a URL, use the `webfetch` tool to retrieve the content of the provided URL. +- After fetching, review the content returned by the fetch tool. +- If you find any additional URLs or links that are relevant, use the `webfetch` tool again to retrieve those links. +- Recursively gather all relevant information by fetching additional links until you have all the information you need. + +## 2. Deeply Understand the Problem +Carefully read the issue and think hard about a plan to solve it before coding. Use the sequential thinking tool if available. + +## 3. Codebase Investigation +- Explore relevant files and directories. +- Search for key functions, classes, or variables related to the issue. +- Read and understand relevant code snippets. +- Identify the root cause of the problem. +- Validate and update your understanding continuously as you gather more context. + +## 4. Internet Research +- Use the `webfetch` tool to search bing by fetching the URL `https://www.bing.com/search?q=your+search+query`. +- After fetching, review the content returned by the fetch tool. +- If you find any additional URLs or links that are relevant, use the `webfetch` tool again to retrieve those links. +- Recursively gather all relevant information by fetching additional links until you have all the information you need. + +## 5. Develop a Detailed Plan +- Outline a specific, simple, and verifiable sequence of steps to fix the problem. +- Add steps using the TodoWrite tool. +- Each time you complete a step, mark it as complete using the TodoWrite tool. +- Each time you check off a step, use the TodoRead tool and display the updated todo list to the user in markdown format. +- You MUST continue on to the next step after checking off a step instead of ending your turn and asking the user what they want to do next. +- You may only end your turn when all steps in the todo list are marked as complete or cancelled. + +## 6. Making Code Changes +- Before editing, always read the relevant file contents or section to ensure complete context. +- Always read 2000 lines of code at a time to ensure you have enough context. +- Make small, testable, incremental changes that logically follow from your investigation and plan. +- When using the edit tool, include 3-5 lines of unchanged code before and after the string you want to replace, to make it unambiguous which part of the file should be edited. +- If a patch or edit is not applied correctly, attempt to reapply it. +- Always validate that your changes build and pass tests after each change. +- If the build fails or test fail, debug why before proceeding, update the plan as needed. + +## 7. Debugging +- Use the `lsp_diagnostics` tool to check for any problems in the code. +- Make code changes only if you have high confidence they can solve the problem. +- When debugging, try to determine the root cause rather than addressing symptoms. +- Debug for as long as needed to identify the root cause and identify a fix. +- Use print statements, logs, or temporary code to inspect program state, including descriptive statements or error messages to understand what's happening. +- To test hypotheses, you can also add test statements or functions. +- Revisit your assumptions if unexpected behavior occurs. + +# How to create a Todo List + Use the following format to show the todo list: + ```markdown + - [ ] Step 1: Description of the first step + - [ ] Step 2: Description of the second step + - [ ] Step 3: Description of the third step + ``` +Do not ever use HTML tags or any other formatting for the todo list, as it will not be rendered correctly. Always use the markdown format shown above. + +# Communication Guidelines +Always communicate clearly and concisely in a casual, friendly yet professional tone. + + +"Let me fetch the URL you provided to gather more information." +"Ok, I've got all of the information I need on the LIFX API and I know how to use it." +"Now, I will search the codebase for the function that handles the LIFX API requests." +"I need to update several files here - stand by" +"OK! Now let's run the tests to make sure everything is working correctly." +"Whelp - I see we have some problems. Let's fix those up." + diff --git a/packages/opencode/src/session/prompt/gemini.txt b/packages/opencode/src/session/prompt/gemini.txt new file mode 100644 index 00000000..87fe422b --- /dev/null +++ b/packages/opencode/src/session/prompt/gemini.txt @@ -0,0 +1,155 @@ +You are opencode, an interactive CLI agent specializing in software engineering tasks. Your primary goal is to help users safely and efficiently, adhering strictly to the following instructions and utilizing your available tools. + +# Core Mandates + +- **Conventions:** Rigorously adhere to existing project conventions when reading or modifying code. Analyze surrounding code, tests, and configuration first. +- **Libraries/Frameworks:** NEVER assume a library/framework is available or appropriate. Verify its established usage within the project (check imports, configuration files like 'package.json', 'Cargo.toml', 'requirements.txt', 'build.gradle', etc., or observe neighboring files) before employing it. +- **Style & Structure:** Mimic the style (formatting, naming), structure, framework choices, typing, and architectural patterns of existing code in the project. +- **Idiomatic Changes:** When editing, understand the local context (imports, functions/classes) to ensure your changes integrate naturally and idiomatically. +- **Comments:** Add code comments sparingly. Focus on *why* something is done, especially for complex logic, rather than *what* is done. Only add high-value comments if necessary for clarity or if requested by the user. Do not edit comments that are separate from the code you are changing. *NEVER* talk to the user or describe your changes through comments. +- **Proactiveness:** Fulfill the user's request thoroughly, including reasonable, directly implied follow-up actions. +- **Confirm Ambiguity/Expansion:** Do not take significant actions beyond the clear scope of the request without confirming with the user. If asked *how* to do something, explain first, don't just do it. +- **Explaining Changes:** After completing a code modification or file operation *do not* provide summaries unless asked. +- **Path Construction:** Before using any file system tool (e.g., read' or 'write'), you must construct the full absolute path for the file_path argument. Always combine the absolute path of the project's root directory with the file's path relative to the root. For example, if the project root is /path/to/project/ and the file is foo/bar/baz.txt, the final path you must use is /path/to/project/foo/bar/baz.txt. If the user provides a relative path, you must resolve it against the root directory to create an absolute path. +- **Do Not revert changes:** Do not revert changes to the codebase unless asked to do so by the user. Only revert changes made by you if they have resulted in an error or if the user has explicitly asked you to revert the changes. + +# Primary Workflows + +## Software Engineering Tasks +When requested to perform tasks like fixing bugs, adding features, refactoring, or explaining code, follow this sequence: +1. **Understand:** Think about the user's request and the relevant codebase context. Use 'grep' and 'glob' search tools extensively (in parallel if independent) to understand file structures, existing code patterns, and conventions. Use 'read' to understand context and validate any assumptions you may have. +2. **Plan:** Build a coherent and grounded (based on the understanding in step 1) plan for how you intend to resolve the user's task. Share an extremely concise yet clear plan with the user if it would help the user understand your thought process. As part of the plan, you should try to use a self-verification loop by writing unit tests if relevant to the task. Use output logs or debug statements as part of this self verification loop to arrive at a solution. +3. **Implement:** Use the available tools (e.g., 'edit', 'write' 'bash' ...) to act on the plan, strictly adhering to the project's established conventions (detailed under 'Core Mandates'). +4. **Verify (Tests):** If applicable and feasible, verify the changes using the project's testing procedures. Identify the correct test commands and frameworks by examining 'README' files, build/package configuration (e.g., 'package.json'), or existing test execution patterns. NEVER assume standard test commands. +5. **Verify (Standards):** VERY IMPORTANT: After making code changes, execute the project-specific build, linting and type-checking commands (e.g., 'tsc', 'npm run lint', 'ruff check .') that you have identified for this project (or obtained from the user). This ensures code quality and adherence to standards. If unsure about these commands, you can ask the user if they'd like you to run them and if so how to. + +## New Applications + +**Goal:** Autonomously implement and deliver a visually appealing, substantially complete, and functional prototype. Utilize all tools at your disposal to implement the application. Some tools you may especially find useful are 'write', 'edit' and 'bash'. + +1. **Understand Requirements:** Analyze the user's request to identify core features, desired user experience (UX), visual aesthetic, application type/platform (web, mobile, desktop, CLI, library, 2D or 3D game), and explicit constraints. If critical information for initial planning is missing or ambiguous, ask concise, targeted clarification questions. +2. **Propose Plan:** Formulate an internal development plan. Present a clear, concise, high-level summary to the user. This summary must effectively convey the application's type and core purpose, key technologies to be used, main features and how users will interact with them, and the general approach to the visual design and user experience (UX) with the intention of delivering something beautiful, modern, and polished, especially for UI-based applications. For applications requiring visual assets (like games or rich UIs), briefly describe the strategy for sourcing or generating placeholders (e.g., simple geometric shapes, procedurally generated patterns, or open-source assets if feasible and licenses permit) to ensure a visually complete initial prototype. Ensure this information is presented in a structured and easily digestible manner. +3. **User Approval:** Obtain user approval for the proposed plan. +4. **Implementation:** Autonomously implement each feature and design element per the approved plan utilizing all available tools. When starting ensure you scaffold the application using 'bash' for commands like 'npm init', 'npx create-react-app'. Aim for full scope completion. Proactively create or source necessary placeholder assets (e.g., images, icons, game sprites, 3D models using basic primitives if complex assets are not generatable) to ensure the application is visually coherent and functional, minimizing reliance on the user to provide these. If the model can generate simple assets (e.g., a uniformly colored square sprite, a simple 3D cube), it should do so. Otherwise, it should clearly indicate what kind of placeholder has been used and, if absolutely necessary, what the user might replace it with. Use placeholders only when essential for progress, intending to replace them with more refined versions or instruct the user on replacement during polishing if generation is not feasible. +5. **Verify:** Review work against the original request, the approved plan. Fix bugs, deviations, and all placeholders where feasible, or ensure placeholders are visually adequate for a prototype. Ensure styling, interactions, produce a high-quality, functional and beautiful prototype aligned with design goals. Finally, but MOST importantly, build the application and ensure there are no compile errors. +6. **Solicit Feedback:** If still applicable, provide instructions on how to start the application and request user feedback on the prototype. + +# Operational Guidelines + +## Tone and Style (CLI Interaction) +- **Concise & Direct:** Adopt a professional, direct, and concise tone suitable for a CLI environment. +- **Minimal Output:** Aim for fewer than 3 lines of text output (excluding tool use/code generation) per response whenever practical. Focus strictly on the user's query. +- **Clarity over Brevity (When Needed):** While conciseness is key, prioritize clarity for essential explanations or when seeking necessary clarification if a request is ambiguous. +- **No Chitchat:** Avoid conversational filler, preambles ("Okay, I will now..."), or postambles ("I have finished the changes..."). Get straight to the action or answer. +- **Formatting:** Use GitHub-flavored Markdown. Responses will be rendered in monospace. +- **Tools vs. Text:** Use tools for actions, text output *only* for communication. Do not add explanatory comments within tool calls or code blocks unless specifically part of the required code/command itself. +- **Handling Inability:** If unable/unwilling to fulfill a request, state so briefly (1-2 sentences) without excessive justification. Offer alternatives if appropriate. + +## Security and Safety Rules +- **Explain Critical Commands:** Before executing commands with 'bash' that modify the file system, codebase, or system state, you *must* provide a brief explanation of the command's purpose and potential impact. Prioritize user understanding and safety. You should not ask permission to use the tool; the user will be presented with a confirmation dialogue upon use (you do not need to tell them this). +- **Security First:** Always apply security best practices. Never introduce code that exposes, logs, or commits secrets, API keys, or other sensitive information. + +## Tool Usage +- **File Paths:** Always use absolute paths when referring to files with tools like 'read' or 'write'. Relative paths are not supported. You must provide an absolute path. +- **Parallelism:** Execute multiple independent tool calls in parallel when feasible (i.e. searching the codebase). +- **Command Execution:** Use the 'bash' tool for running shell commands, remembering the safety rule to explain modifying commands first. +- **Background Processes:** Use background processes (via \`&\`) for commands that are unlikely to stop on their own, e.g. \`node server.js &\`. If unsure, ask the user. +- **Interactive Commands:** Try to avoid shell commands that are likely to require user interaction (e.g. \`git rebase -i\`). Use non-interactive versions of commands (e.g. \`npm init -y\` instead of \`npm init\`) when available, and otherwise remind the user that interactive shell commands are not supported and may cause hangs until canceled by the user. +- **Respect User Confirmations:** Most tool calls (also denoted as 'function calls') will first require confirmation from the user, where they will either approve or cancel the function call. If a user cancels a function call, respect their choice and do _not_ try to make the function call again. It is okay to request the tool call again _only_ if the user requests that same tool call on a subsequent prompt. When a user cancels a function call, assume best intentions from the user and consider inquiring if they prefer any alternative paths forward. + +## Interaction Details +- **Help Command:** The user can use '/help' to display help information. +- **Feedback:** To report a bug or provide feedback, please use the /bug command. + +# Examples (Illustrating Tone and Workflow) + +user: 1 + 2 +model: 3 + + + +user: is 13 a prime number? +model: true + + + +user: list files here. +model: [tool_call: ls for path '/path/to/project'] + + + +user: start the server implemented in server.js +model: [tool_call: bash for 'node server.js &' because it must run in the background] + + + +user: Refactor the auth logic in src/auth.py to use the requests library instead of urllib. +model: Okay, I can refactor 'src/auth.py'. +First, I'll analyze the code and check for a test safety net before planning any changes. +[tool_call: glob for path 'tests/test_auth.py'] +[tool_call: read for absolute_path '/path/to/tests/test_auth.py'] +(After analysis) +Great, 'tests/test_auth.py' exists and covers the core authentication logic. With this safety net in place, I can safely plan the refactoring. +I'll also confirm 'requests' is a dependency. +[tool_call: read for absolute_path '/path/to/requirements.txt'] +(After analysis) +Looks good, 'requests' is available. + +Here's the plan: +1. Replace the 'urllib' calls with 'requests'. +2. Add proper 'try...except' error handling for the new network calls. +3. Remove the old 'urllib' import. +4. Run the project's linter and tests to verify the changes. + +Should I proceed? +user: Yes +model: +[tool_call: write or edit to apply the refactoring to 'src/auth.py'] +Refactoring complete. Running verification... +[tool_call: bash for 'ruff check src/auth.py && pytest'] +(After verification passes) +All checks passed. This is a stable checkpoint. + + +user: Delete the temp directory. +model: I can run \`rm -rf /path/to/project/temp\`. This will permanently delete the directory and all its contents. + + + +user: Write tests for someFile.ts +model: +Okay, I can write those tests. First, I'll read \`someFile.ts\` to understand its functionality. +[tool_call: read for absolute_path '/path/to/someFile.ts' or use glob to find \`someFile.ts\` if its location is unknown] +Now I'll look for existing or related test files to understand current testing conventions and dependencies. +[tool_call: glob for paths ['**/*.test.ts', 'src/**/*.spec.ts'] assuming someFile.ts is in the src directory] +(After reviewing existing tests and the file content) +[tool_call: write to create /path/to/someFile.test.ts with the test code] +I've written the tests. Now I'll run the project's test command to verify them. +[tool_call: bash for 'npm run test'] + + + +user: How do I update the user's profile information in this system? +model: +I'm not immediately sure how user profile information is updated. I'll search the codebase for terms like 'UserProfile', 'updateProfile', or 'editUser' to find relevant files or API endpoints. +[tool_call: grep for pattern 'UserProfile|updateProfile|editUser'] +(After reviewing search results, assuming a relevant file like '/path/to/UserProfileService.java' was found) +Okay, \`/path/to/UserProfileService.java\` seems like the most relevant file. I'll read its content to understand how updates are handled. +[tool_call: read for absolute_path '/path/to/UserProfileService.java'] +(After reading the file) +It appears the \`updateUserProfile\` method in \`UserProfileService.java\` is responsible for this. It expects a user ID and a \`UserProfileDTO\` object... + + + +user: Where are all the 'app.config' files in this project? I need to check their settings. +model: +[tool_call: glob for pattern '**/app.config'] +(Assuming GlobTool returns a list of paths like ['/path/to/moduleA/app.config', '/path/to/moduleB/app.config']) +I found the following 'app.config' files: +- /path/to/moduleA/app.config +- /path/to/moduleB/app.config +To help you check their settings, I can read their contents. Which one would you like to start with, or should I read all of them? + + +# Final Reminder +Your core function is efficient and safe assistance. Balance extreme conciseness with the crucial need for clarity, especially regarding safety and potential system modifications. Always prioritize user control and project conventions. Never make assumptions about the contents of files; instead use 'read' to ensure you aren't making broad assumptions. Finally, you are an agent - please keep going until the user's query is completely resolved. diff --git a/packages/opencode/src/session/prompt/plan.txt b/packages/opencode/src/session/prompt/plan.txt new file mode 100644 index 00000000..fffbfffc --- /dev/null +++ b/packages/opencode/src/session/prompt/plan.txt @@ -0,0 +1,3 @@ + +Plan mode is active. The user indicated that they do not want you to execute yet -- you MUST NOT make any edits, run any non-readonly tools (including changing configs or making commits), or otherwise make any changes to the system. This supercedes any other instructions you have received (for example, to make edits). + diff --git a/packages/opencode/src/session/prompt/title.txt b/packages/opencode/src/session/prompt/title.txt index 373456a7..ac82d60a 100644 --- a/packages/opencode/src/session/prompt/title.txt +++ b/packages/opencode/src/session/prompt/title.txt @@ -1,11 +1,31 @@ -Generate a short title based on the first message a user begins a conversation with. CRITICAL: Your response must be EXACTLY one line with NO line breaks, newlines, or multiple sentences. + +Generate a conversation thread title from the user message. + -Requirements: -- Maximum 50 characters -- Single line only - NO newlines or line breaks -- Summary of the user's message -- No quotes, colons, or special formatting -- Do not include explanatory text like "summary:" or similar -- Your entire response becomes the title + +You are generating titles for a coding assistant conversation. + -IMPORTANT: Return only the title text on a single line. Do not add any explanations, formatting, or additional text. + +- Max 50 chars, single line +- Focus on the specific action or question +- Keep technical terms, numbers, and filenames exactly as written +- Preserve HTTP status codes (401, 404, 500, etc) as numbers +- For file references, include the filename +- Avoid filler words: the, this, my, a, an, properly +- NEVER assume their tech stack or domain +- Use -ing verbs consistently for actions +- Write like a chat thread title, not a blog post + + + +"debug 500 errors in production" → "Debugging production 500 errors" +"refactor user service" → "Refactoring user service" +"why is app.js failing" → "Analyzing app.js failure" +"implement rate limiting" → "Implementing rate limiting" + + + +Return only the thread title text on a single line with no newlines, explanations, or additional formatting. +You should NEVER reply to the user's message. You can only generate titles. + diff --git a/packages/opencode/src/session/system.ts b/packages/opencode/src/session/system.ts index 722964ea..375b627b 100644 --- a/packages/opencode/src/session/system.ts +++ b/packages/opencode/src/session/system.ts @@ -7,23 +7,17 @@ import path from "path" import os from "os" import PROMPT_ANTHROPIC from "./prompt/anthropic.txt" +import PROMPT_BEAST from "./prompt/beast.txt" +import PROMPT_GEMINI from "./prompt/gemini.txt" import PROMPT_ANTHROPIC_SPOOF from "./prompt/anthropic_spoof.txt" import PROMPT_SUMMARIZE from "./prompt/summarize.txt" import PROMPT_TITLE from "./prompt/title.txt" export namespace SystemPrompt { - export function provider(providerID: string) { - const result = [] - switch (providerID) { - case "anthropic": - result.push(PROMPT_ANTHROPIC_SPOOF.trim()) - result.push(PROMPT_ANTHROPIC) - break - default: - result.push(PROMPT_ANTHROPIC) - break - } - return result + export function provider(modelID: string) { + if (modelID.includes("gpt-") || modelID.includes("o1") || modelID.includes("o3")) return [PROMPT_BEAST] + if (modelID.includes("gemini-")) return [PROMPT_GEMINI] + return [PROMPT_ANTHROPIC] } export async function environment() { diff --git a/packages/opencode/src/share/share.ts b/packages/opencode/src/share/share.ts index a5080969..2996e4d9 100644 --- a/packages/opencode/src/share/share.ts +++ b/packages/opencode/src/share/share.ts @@ -53,9 +53,7 @@ export namespace Share { export const URL = process.env["OPENCODE_API"] ?? - (Installation.isSnapshot() || Installation.isDev() - ? "https://api.dev.opencode.ai" - : "https://api.opencode.ai") + (Installation.isSnapshot() || Installation.isDev() ? "https://api.dev.opencode.ai" : "https://api.opencode.ai") export async function create(sessionID: string) { return fetch(`${URL}/share_create`, { diff --git a/packages/opencode/src/snapshot/index.ts b/packages/opencode/src/snapshot/index.ts index 1bbb870f..b23de6e0 100644 --- a/packages/opencode/src/snapshot/index.ts +++ b/packages/opencode/src/snapshot/index.ts @@ -9,10 +9,8 @@ export namespace Snapshot { const log = Log.create({ service: "snapshot" }) export async function create(sessionID: string) { - return log.info("creating snapshot") const app = App.info() - const git = gitdir(sessionID) // not a git repo, check if too big to snapshot if (!app.git) { @@ -21,9 +19,10 @@ export namespace Snapshot { limit: 1000, }) log.info("found files", { count: files.length }) - if (files.length > 1000) return + if (files.length >= 1000) return } + const git = gitdir(sessionID) if (await fs.mkdir(git, { recursive: true })) { await $`git init` .env({ @@ -40,24 +39,27 @@ export namespace Snapshot { log.info("added files") const result = - await $`git --git-dir ${git} commit --allow-empty -m "snapshot" --author="opencode "` + await $`git --git-dir ${git} commit -m "snapshot" --no-gpg-sign --author="opencode "` .quiet() .cwd(app.path.cwd) .nothrow() - log.info("commit") const match = result.stdout.toString().match(/\[.+ ([a-f0-9]+)\]/) if (!match) return return match![1] } - export async function restore(sessionID: string, commit: string) { - log.info("restore", { commit }) + export async function restore(sessionID: string, snapshot: string) { + log.info("restore", { commit: snapshot }) const app = App.info() const git = gitdir(sessionID) - await $`git --git-dir=${git} checkout ${commit} --force` - .quiet() - .cwd(app.path.root) + await $`git --git-dir=${git} checkout ${snapshot} --force`.quiet().cwd(app.path.root) + } + + export async function diff(sessionID: string, commit: string) { + const git = gitdir(sessionID) + const result = await $`git --git-dir=${git} diff -R ${commit}`.quiet().cwd(App.info().path.root) + return result.stdout.toString("utf8") } function gitdir(sessionID: string) { diff --git a/packages/opencode/src/storage/storage.ts b/packages/opencode/src/storage/storage.ts index eee33a09..97efcef7 100644 --- a/packages/opencode/src/storage/storage.ts +++ b/packages/opencode/src/storage/storage.ts @@ -4,61 +4,136 @@ import { Bus } from "../bus" import path from "path" import z from "zod" import fs from "fs/promises" +import { MessageV2 } from "../session/message-v2" +import { Identifier } from "../id/id" export namespace Storage { const log = Log.create({ service: "storage" }) export const Event = { - Write: Bus.event( - "storage.write", - z.object({ key: z.string(), content: z.any() }), - ), + Write: Bus.event("storage.write", z.object({ key: z.string(), content: z.any() })), } - const state = App.state("storage", () => { + type Migration = (dir: string) => Promise + + const MIGRATIONS: Migration[] = [ + async (dir: string) => { + try { + const files = new Bun.Glob("session/message/*/*.json").scanSync({ + cwd: dir, + absolute: true, + }) + for (const file of files) { + const content = await Bun.file(file).json() + if (!content.metadata) continue + log.info("migrating to v2 message", { file }) + try { + const result = MessageV2.fromV1(content) + await Bun.write( + file, + JSON.stringify( + { + ...result.info, + parts: result.parts, + }, + null, + 2, + ), + ) + } catch (e) { + await fs.rename(file, file.replace("storage", "broken")) + } + } + } catch {} + }, + async (dir: string) => { + const files = new Bun.Glob("session/message/*/*.json").scanSync({ + cwd: dir, + absolute: true, + }) + for (const file of files) { + try { + const { parts, ...info } = await Bun.file(file).json() + if (!parts) continue + for (const part of parts) { + const id = Identifier.ascending("part") + await Bun.write( + [dir, "session", "part", info.sessionID, info.id, id + ".json"].join("/"), + JSON.stringify({ + ...part, + id, + sessionID: info.sessionID, + messageID: info.id, + ...(part.type === "tool" ? { callID: part.id } : {}), + }), + ) + } + await Bun.write(file, JSON.stringify(info, null, 2)) + } catch (e) {} + } + }, + ] + + const state = App.state("storage", async () => { const app = App.info() - const dir = path.join(app.path.data, "storage") - log.info("init", { path: dir }) + const dir = path.normalize(path.join(app.path.data, "storage")) + await fs.mkdir(dir, { recursive: true }) + const migration = await Bun.file(path.join(dir, "migration")) + .json() + .then((x) => parseInt(x)) + .catch(() => 0) + for (let index = migration; index < MIGRATIONS.length; index++) { + log.info("running migration", { index }) + const migration = MIGRATIONS[index] + await migration(dir) + await Bun.write(path.join(dir, "migration"), (index + 1).toString()) + } return { dir, } }) export async function remove(key: string) { - const target = path.join(state().dir, key + ".json") + const dir = await state().then((x) => x.dir) + const target = path.join(dir, key + ".json") await fs.unlink(target).catch(() => {}) } export async function removeDir(key: string) { - const target = path.join(state().dir, key) + const dir = await state().then((x) => x.dir) + const target = path.join(dir, key) await fs.rm(target, { recursive: true, force: true }).catch(() => {}) } export async function readJSON(key: string) { - return Bun.file(path.join(state().dir, key + ".json")).json() as Promise + const dir = await state().then((x) => x.dir) + return Bun.file(path.join(dir, key + ".json")).json() as Promise } export async function writeJSON(key: string, content: T) { - const target = path.join(state().dir, key + ".json") + const dir = await state().then((x) => x.dir) + const target = path.join(dir, key + ".json") const tmp = target + Date.now() + ".tmp" - await Bun.write(tmp, JSON.stringify(content)) + await Bun.write(tmp, JSON.stringify(content, null, 2)) await fs.rename(tmp, target).catch(() => {}) await fs.unlink(tmp).catch(() => {}) Bus.publish(Event.Write, { key, content }) } const glob = new Bun.Glob("**/*") - export async function* list(prefix: string) { + export async function list(prefix: string) { + const dir = await state().then((x) => x.dir) try { - for await (const item of glob.scan({ - cwd: path.join(state().dir, prefix), - onlyFiles: true, - })) { - const result = path.join(prefix, item.slice(0, -5)) - yield result - } + const result = await Array.fromAsync( + glob.scan({ + cwd: path.join(dir, prefix), + onlyFiles: true, + }), + ).then((items) => items.map((item) => path.join(prefix, item.slice(0, -5)))) + result.sort() + return result } catch { - return + return [] } } } diff --git a/packages/opencode/src/tool/bash.ts b/packages/opencode/src/tool/bash.ts index 620a8c8d..050a5a97 100644 --- a/packages/opencode/src/tool/bash.ts +++ b/packages/opencode/src/tool/bash.ts @@ -12,12 +12,7 @@ export const BashTool = Tool.define({ description: DESCRIPTION, parameters: z.object({ command: z.string().describe("The command to execute"), - timeout: z - .number() - .min(0) - .max(MAX_TIMEOUT) - .describe("Optional timeout in milliseconds") - .optional(), + timeout: z.number().min(0).max(MAX_TIMEOUT).describe("Optional timeout in milliseconds").optional(), description: z .string() .describe( @@ -41,21 +36,14 @@ export const BashTool = Tool.define({ const stderr = await new Response(process.stderr).text() return { + title: params.command, metadata: { stderr, stdout, exit: process.exitCode, description: params.description, - title: params.command, }, - output: [ - ``, - stdout ?? "", - ``, - ``, - stderr ?? "", - ``, - ].join("\n"), + output: [``, stdout ?? "", ``, ``, stderr ?? "", ``].join("\n"), } }, }) diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.ts index 8c9043e6..4b9f355e 100644 --- a/packages/opencode/src/tool/edit.ts +++ b/packages/opencode/src/tool/edit.ts @@ -20,15 +20,8 @@ export const EditTool = Tool.define({ parameters: z.object({ filePath: z.string().describe("The absolute path to the file to modify"), oldString: z.string().describe("The text to replace"), - newString: z - .string() - .describe( - "The text to replace it with (must be different from old_string)", - ), - replaceAll: z - .boolean() - .optional() - .describe("Replace all occurrences of old_string (default false)"), + newString: z.string().describe("The text to replace it with (must be different from oldString)"), + replaceAll: z.boolean().optional().describe("Replace all occurrences of oldString (default false)"), }), async execute(params, ctx) { if (!params.filePath) { @@ -40,9 +33,7 @@ export const EditTool = Tool.define({ } const app = App.info() - const filepath = path.isAbsolute(params.filePath) - ? params.filePath - : path.join(app.path.cwd, params.filePath) + const filepath = path.isAbsolute(params.filePath) ? params.filePath : path.join(app.path.cwd, params.filePath) await Permission.ask({ id: "edit", @@ -70,17 +61,11 @@ export const EditTool = Tool.define({ const file = Bun.file(filepath) const stats = await file.stat().catch(() => {}) if (!stats) throw new Error(`File ${filepath} not found`) - if (stats.isDirectory()) - throw new Error(`Path is a directory, not a file: ${filepath}`) + if (stats.isDirectory()) throw new Error(`Path is a directory, not a file: ${filepath}`) await FileTime.assert(ctx.sessionID, filepath) contentOld = await file.text() - contentNew = replace( - contentOld, - params.oldString, - params.newString, - params.replaceAll, - ) + contentNew = replace(contentOld, params.oldString, params.newString, params.replaceAll) await file.write(contentNew) await Bus.publish(File.Event.Edited, { file: filepath, @@ -88,9 +73,7 @@ export const EditTool = Tool.define({ contentNew = await file.text() })() - const diff = trimDiff( - createTwoFilesPatch(filepath, filepath, contentOld, contentNew), - ) + const diff = trimDiff(createTwoFilesPatch(filepath, filepath, contentOld, contentNew)) FileTime.read(ctx.sessionID, filepath) @@ -103,24 +86,24 @@ export const EditTool = Tool.define({ output += `\nThis file has errors, please fix\n\n${issues.map(LSP.Diagnostic.pretty).join("\n")}\n\n` continue } - output += `\n\n${file}\n${issues.map(LSP.Diagnostic.pretty).join("\n")}\n\n` + output += `\n\n${file}\n${issues + .filter((item) => item.severity === 1) + .map(LSP.Diagnostic.pretty) + .join("\n")}\n\n` } return { metadata: { diagnostics, diff, - title: `${path.relative(app.path.root, filepath)}`, }, + title: `${path.relative(app.path.root, filepath)}`, output, } }, }) -export type Replacer = ( - content: string, - find: string, -) => Generator +export type Replacer = (content: string, find: string) => Generator export const SimpleReplacer: Replacer = function* (_content, find) { yield find @@ -208,10 +191,7 @@ export const BlockAnchorReplacer: Replacer = function* (content, find) { } } -export const WhitespaceNormalizedReplacer: Replacer = function* ( - content, - find, -) { +export const WhitespaceNormalizedReplacer: Replacer = function* (content, find) { const normalizeWhitespace = (text: string) => text.replace(/\s+/g, " ").trim() const normalizedFind = normalizeWhitespace(find) @@ -229,9 +209,7 @@ export const WhitespaceNormalizedReplacer: Replacer = function* ( // Find the actual substring in the original line that matches const words = find.trim().split(/\s+/) if (words.length > 0) { - const pattern = words - .map((word) => word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")) - .join("\\s+") + const pattern = words.map((word) => word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("\\s+") try { const regex = new RegExp(pattern) const match = line.match(regex) @@ -270,9 +248,7 @@ export const IndentationFlexibleReplacer: Replacer = function* (content, find) { }), ) - return lines - .map((line) => (line.trim().length === 0 ? line : line.slice(minIndent))) - .join("\n") + return lines.map((line) => (line.trim().length === 0 ? line : line.slice(minIndent))).join("\n") } const normalizedFind = removeIndentation(find) @@ -423,10 +399,7 @@ export const ContextAwareReplacer: Replacer = function* (content, find) { } } - if ( - totalNonEmptyLines === 0 || - matchingLines / totalNonEmptyLines >= 0.5 - ) { + if (totalNonEmptyLines === 0 || matchingLines / totalNonEmptyLines >= 0.5) { yield block break // Only match the first occurrence } @@ -473,12 +446,7 @@ function trimDiff(diff: string): string { return trimmedLines.join("\n") } -export function replace( - content: string, - oldString: string, - newString: string, - replaceAll = false, -): string { +export function replace(content: string, oldString: string, newString: string, replaceAll = false): string { if (oldString === newString) { throw new Error("oldString and newString must be different") } @@ -502,11 +470,7 @@ export function replace( } const lastIndex = content.lastIndexOf(search) if (index !== lastIndex) continue - return ( - content.substring(0, index) + - newString + - content.substring(index + search.length) - ) + return content.substring(0, index) + newString + content.substring(index + search.length) } } throw new Error("oldString not found in content or was found multiple times") diff --git a/packages/opencode/src/tool/edit.txt b/packages/opencode/src/tool/edit.txt index fff2eca6..8bf8844d 100644 --- a/packages/opencode/src/tool/edit.txt +++ b/packages/opencode/src/tool/edit.txt @@ -2,8 +2,8 @@ Performs exact string replacements in files. Usage: - You must use your `Read` tool at least once in the conversation before editing. This tool will error if you attempt an edit without reading the file. -- When editing text from Read tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: spaces + line number + tab. Everything after that tab is the actual file content to match. Never include any part of the line number prefix in the old_string or new_string. +- When editing text from Read tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: spaces + line number + tab. Everything after that tab is the actual file content to match. Never include any part of the line number prefix in the oldString or newString. - ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required. - Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked. -- The edit will FAIL if `old_string` is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use `replace_all` to change every instance of `old_string`. -- Use `replace_all` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance. +- The edit will FAIL if `oldString` is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use `replaceAll` to change every instance of `oldString`. +- Use `replaceAll` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance. diff --git a/packages/opencode/src/tool/glob.ts b/packages/opencode/src/tool/glob.ts index 70d5df47..6496099e 100644 --- a/packages/opencode/src/tool/glob.ts +++ b/packages/opencode/src/tool/glob.ts @@ -20,16 +20,14 @@ export const GlobTool = Tool.define({ async execute(params) { const app = App.info() let search = params.path ?? app.path.cwd - search = path.isAbsolute(search) - ? search - : path.resolve(app.path.cwd, search) + search = path.isAbsolute(search) ? search : path.resolve(app.path.cwd, search) const limit = 100 const files = [] let truncated = false for (const file of await Ripgrep.files({ cwd: search, - glob: params.pattern, + glob: [params.pattern], })) { if (files.length >= limit) { truncated = true @@ -53,17 +51,15 @@ export const GlobTool = Tool.define({ output.push(...files.map((f) => f.path)) if (truncated) { output.push("") - output.push( - "(Results are truncated. Consider using a more specific path or pattern.)", - ) + output.push("(Results are truncated. Consider using a more specific path or pattern.)") } } return { + title: path.relative(app.path.root, search), metadata: { count: files.length, truncated, - title: path.relative(app.path.root, search), }, output: output.join("\n"), } diff --git a/packages/opencode/src/tool/grep.ts b/packages/opencode/src/tool/grep.ts index e4ceec5e..898e3e73 100644 --- a/packages/opencode/src/tool/grep.ts +++ b/packages/opencode/src/tool/grep.ts @@ -9,21 +9,9 @@ export const GrepTool = Tool.define({ id: "grep", description: DESCRIPTION, parameters: z.object({ - pattern: z - .string() - .describe("The regex pattern to search for in file contents"), - path: z - .string() - .optional() - .describe( - "The directory to search in. Defaults to the current working directory.", - ), - include: z - .string() - .optional() - .describe( - 'File pattern to include in the search (e.g. "*.js", "*.{ts,tsx}")', - ), + pattern: z.string().describe("The regex pattern to search for in file contents"), + path: z.string().optional().describe("The directory to search in. Defaults to the current working directory."), + include: z.string().optional().describe('File pattern to include in the search (e.g. "*.js", "*.{ts,tsx}")'), }), async execute(params) { if (!params.pattern) { @@ -51,7 +39,8 @@ export const GrepTool = Tool.define({ if (exitCode === 1) { return { - metadata: { matches: 0, truncated: false, title: params.pattern }, + title: params.pattern, + metadata: { matches: 0, truncated: false }, output: "No files found", } } @@ -66,12 +55,11 @@ export const GrepTool = Tool.define({ for (const line of lines) { if (!line) continue - const parts = line.split(":", 3) - if (parts.length < 3) continue + const [filePath, lineNumStr, ...lineTextParts] = line.split(":") + if (!filePath || !lineNumStr || lineTextParts.length === 0) continue - const filePath = parts[0] - const lineNum = parseInt(parts[1], 10) - const lineText = parts[2] + const lineNum = parseInt(lineNumStr, 10) + const lineText = lineTextParts.join(":") const file = Bun.file(filePath) const stats = await file.stat().catch(() => null) @@ -93,7 +81,8 @@ export const GrepTool = Tool.define({ if (finalMatches.length === 0) { return { - metadata: { matches: 0, truncated: false, title: params.pattern }, + title: params.pattern, + metadata: { matches: 0, truncated: false }, output: "No files found", } } @@ -114,16 +103,14 @@ export const GrepTool = Tool.define({ if (truncated) { outputLines.push("") - outputLines.push( - "(Results are truncated. Consider using a more specific path or pattern.)", - ) + outputLines.push("(Results are truncated. Consider using a more specific path or pattern.)") } return { + title: params.pattern, metadata: { matches: finalMatches.length, truncated, - title: params.pattern, }, output: outputLines.join("\n"), } diff --git a/packages/opencode/src/tool/ls.ts b/packages/opencode/src/tool/ls.ts index bfceba85..d96e27e9 100644 --- a/packages/opencode/src/tool/ls.ts +++ b/packages/opencode/src/tool/ls.ts @@ -16,6 +16,19 @@ export const IGNORE_PATTERNS = [ "obj/", ".idea/", ".vscode/", + ".zig-cache/", + "zig-out", + ".coverage", + "coverage/", + "vendor/", + "tmp/", + "temp/", + ".cache/", + "cache/", + "logs/", + ".venv/", + "venv/", + "env/", ] const LIMIT = 100 @@ -24,16 +37,8 @@ export const ListTool = Tool.define({ id: "list", description: DESCRIPTION, parameters: z.object({ - path: z - .string() - .describe( - "The absolute path to the directory to list (must be absolute, not relative)", - ) - .optional(), - ignore: z - .array(z.string()) - .describe("List of glob patterns to ignore") - .optional(), + path: z.string().describe("The absolute path to the directory to list (must be absolute, not relative)").optional(), + ignore: z.array(z.string()).describe("List of glob patterns to ignore").optional(), }), async execute(params) { const app = App.info() @@ -44,8 +49,7 @@ export const ListTool = Tool.define({ for await (const file of glob.scan({ cwd: searchPath, dot: true })) { if (IGNORE_PATTERNS.some((p) => file.includes(p))) continue - if (params.ignore?.some((pattern) => new Bun.Glob(pattern).match(file))) - continue + if (params.ignore?.some((pattern) => new Bun.Glob(pattern).match(file))) continue files.push(file) if (files.length >= LIMIT) break } @@ -99,10 +103,10 @@ export const ListTool = Tool.define({ const output = `${searchPath}/\n` + renderDir(".", 0) return { + title: path.relative(app.path.root, searchPath), metadata: { count: files.length, truncated: files.length >= LIMIT, - title: path.relative(app.path.root, searchPath), }, output, } diff --git a/packages/opencode/src/tool/lsp-diagnostics.ts b/packages/opencode/src/tool/lsp-diagnostics.ts index 97ae7a26..fc9699bf 100644 --- a/packages/opencode/src/tool/lsp-diagnostics.ts +++ b/packages/opencode/src/tool/lsp-diagnostics.ts @@ -13,20 +13,16 @@ export const LspDiagnosticTool = Tool.define({ }), execute: async (args) => { const app = App.info() - const normalized = path.isAbsolute(args.path) - ? args.path - : path.join(app.path.cwd, args.path) + const normalized = path.isAbsolute(args.path) ? args.path : path.join(app.path.cwd, args.path) await LSP.touchFile(normalized, true) const diagnostics = await LSP.diagnostics() const file = diagnostics[normalized] return { + title: path.relative(app.path.root, normalized), metadata: { diagnostics, - title: path.relative(app.path.root, normalized), }, - output: file?.length - ? file.map(LSP.Diagnostic.pretty).join("\n") - : "No errors found", + output: file?.length ? file.map(LSP.Diagnostic.pretty).join("\n") : "No errors found", } }, }) diff --git a/packages/opencode/src/tool/lsp-hover.ts b/packages/opencode/src/tool/lsp-hover.ts index f5031f85..bb94ddb3 100644 --- a/packages/opencode/src/tool/lsp-hover.ts +++ b/packages/opencode/src/tool/lsp-hover.ts @@ -15,9 +15,7 @@ export const LspHoverTool = Tool.define({ }), execute: async (args) => { const app = App.info() - const file = path.isAbsolute(args.file) - ? args.file - : path.join(app.path.cwd, args.file) + const file = path.isAbsolute(args.file) ? args.file : path.join(app.path.cwd, args.file) await LSP.touchFile(file, true) const result = await LSP.hover({ ...args, @@ -25,14 +23,9 @@ export const LspHoverTool = Tool.define({ }) return { + title: path.relative(app.path.root, file) + ":" + args.line + ":" + args.character, metadata: { result, - title: - path.relative(app.path.root, file) + - ":" + - args.line + - ":" + - args.character, }, output: JSON.stringify(result, null, 2), } diff --git a/packages/opencode/src/tool/multiedit.ts b/packages/opencode/src/tool/multiedit.ts index f8d0f16c..041893b9 100644 --- a/packages/opencode/src/tool/multiedit.ts +++ b/packages/opencode/src/tool/multiedit.ts @@ -10,9 +10,7 @@ export const MultiEditTool = Tool.define({ description: DESCRIPTION, parameters: z.object({ filePath: z.string().describe("The absolute path to the file to modify"), - edits: z - .array(EditTool.parameters) - .describe("Array of edit operations to perform sequentially on the file"), + edits: z.array(EditTool.parameters).describe("Array of edit operations to perform sequentially on the file"), }), async execute(params, ctx) { const results = [] @@ -30,9 +28,9 @@ export const MultiEditTool = Tool.define({ } const app = App.info() return { + title: path.relative(app.path.root, params.filePath), metadata: { results: results.map((r) => r.metadata), - title: path.relative(app.path.root, params.filePath), }, output: results.at(-1)!.output, } diff --git a/packages/opencode/src/tool/multiedit.txt b/packages/opencode/src/tool/multiedit.txt index 1b32bbd9..bb481512 100644 --- a/packages/opencode/src/tool/multiedit.txt +++ b/packages/opencode/src/tool/multiedit.txt @@ -8,9 +8,9 @@ Before using this tool: To make multiple file edits, provide the following: 1. file_path: The absolute path to the file to modify (must be absolute, not relative) 2. edits: An array of edit operations to perform, where each edit contains: - - old_string: The text to replace (must match the file contents exactly, including all whitespace and indentation) - - new_string: The edited text to replace the old_string - - replace_all: Replace all occurrences of old_string. This parameter is optional and defaults to false. + - oldString: The text to replace (must match the file contents exactly, including all whitespace and indentation) + - newString: The edited text to replace the oldString + - replaceAll: Replace all occurrences of oldString. This parameter is optional and defaults to false. IMPORTANT: - All edits are applied in sequence, in the order they are provided @@ -24,8 +24,8 @@ CRITICAL REQUIREMENTS: 3. Plan your edits carefully to avoid conflicts between sequential operations WARNING: -- The tool will fail if edits.old_string doesn't match the file contents exactly (including whitespace) -- The tool will fail if edits.old_string and edits.new_string are the same +- The tool will fail if edits.oldString doesn't match the file contents exactly (including whitespace) +- The tool will fail if edits.oldString and edits.newString are the same - Since edits are applied in sequence, ensure that earlier edits don't affect the text that later edits are trying to find When making edits: @@ -33,9 +33,9 @@ When making edits: - Do not leave the code in a broken state - Always use absolute file paths (starting with /) - Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked. -- Use replace_all for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance. +- Use replaceAll for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance. If you want to create a new file, use: - A new file path, including dir name if needed -- First edit: empty old_string and the new file's contents as new_string +- First edit: empty oldString and the new file's contents as newString - Subsequent edits: normal edit operations on the created content diff --git a/packages/opencode/src/tool/patch.ts b/packages/opencode/src/tool/patch.ts index 6266d163..11cc56c9 100644 --- a/packages/opencode/src/tool/patch.ts +++ b/packages/opencode/src/tool/patch.ts @@ -6,9 +6,7 @@ import { FileTime } from "../file/time" import DESCRIPTION from "./patch.txt" const PatchParams = z.object({ - patchText: z - .string() - .describe("The full patch text that describes all changes to be made"), + patchText: z.string().describe("The full patch text that describes all changes to be made"), }) interface Change { @@ -42,10 +40,7 @@ function identifyFilesNeeded(patchText: string): string[] { const files: string[] = [] const lines = patchText.split("\n") for (const line of lines) { - if ( - line.startsWith("*** Update File:") || - line.startsWith("*** Delete File:") - ) { + if (line.startsWith("*** Update File:") || line.startsWith("*** Delete File:")) { const filePath = line.split(":", 2)[1]?.trim() if (filePath) files.push(filePath) } @@ -65,10 +60,7 @@ function identifyFilesAdded(patchText: string): string[] { return files } -function textToPatch( - patchText: string, - _currentFiles: Record, -): [PatchOperation[], number] { +function textToPatch(patchText: string, _currentFiles: Record): [PatchOperation[], number] { const operations: PatchOperation[] = [] const lines = patchText.split("\n") let i = 0 @@ -93,11 +85,7 @@ function textToPatch( const changes: PatchChange[] = [] i++ - while ( - i < lines.length && - !lines[i].startsWith("@@") && - !lines[i].startsWith("***") - ) { + while (i < lines.length && !lines[i].startsWith("@@") && !lines[i].startsWith("***")) { const changeLine = lines[i] if (changeLine.startsWith(" ")) { changes.push({ type: "keep", content: changeLine.substring(1) }) @@ -151,10 +139,7 @@ function textToPatch( return [operations, fuzz] } -function patchToCommit( - operations: PatchOperation[], - currentFiles: Record, -): Commit { +function patchToCommit(operations: PatchOperation[], currentFiles: Record): Commit { const changes: Record = {} for (const op of operations) { @@ -173,9 +158,7 @@ function patchToCommit( const lines = originalContent.split("\n") for (const hunk of op.hunks) { - const contextIndex = lines.findIndex((line) => - line.includes(hunk.contextLine), - ) + const contextIndex = lines.findIndex((line) => line.includes(hunk.contextLine)) if (contextIndex === -1) { throw new Error(`Context line not found: ${hunk.contextLine}`) } @@ -204,11 +187,7 @@ function patchToCommit( return { changes } } -function generateDiff( - oldContent: string, - newContent: string, - filePath: string, -): [string, number, number] { +function generateDiff(oldContent: string, newContent: string, filePath: string): [string, number, number] { // Mock implementation - would need actual diff generation const lines1 = oldContent.split("\n") const lines2 = newContent.split("\n") @@ -296,9 +275,7 @@ export const PatchTool = Tool.define({ // Process the patch const [patch, fuzz] = textToPatch(params.patchText, currentFiles) if (fuzz > 3) { - throw new Error( - `patch contains fuzzy matches (fuzz level: ${fuzz}). Please make your context lines more precise`, - ) + throw new Error(`patch contains fuzzy matches (fuzz level: ${fuzz}). Please make your context lines more precise`) } // Convert patch to commit @@ -343,11 +320,7 @@ export const PatchTool = Tool.define({ const newContent = change.new_content || "" // Calculate diff statistics - const [, additions, removals] = generateDiff( - oldContent, - newContent, - filePath, - ) + const [, additions, removals] = generateDiff(oldContent, newContent, filePath) totalAdditions += additions totalRemovals += removals @@ -358,11 +331,11 @@ export const PatchTool = Tool.define({ const output = result return { + title: `${filesToRead.length} files`, metadata: { changed: changedFiles, additions: totalAdditions, removals: totalRemovals, - title: `${filesToRead.length} files`, }, output, } diff --git a/packages/opencode/src/tool/read.ts b/packages/opencode/src/tool/read.ts index f524389e..81414186 100644 --- a/packages/opencode/src/tool/read.ts +++ b/packages/opencode/src/tool/read.ts @@ -7,7 +7,6 @@ import { FileTime } from "../file/time" import DESCRIPTION from "./read.txt" import { App } from "../app/app" -const MAX_READ_SIZE = 250 * 1024 const DEFAULT_READ_LIMIT = 2000 const MAX_LINE_LENGTH = 2000 @@ -16,14 +15,8 @@ export const ReadTool = Tool.define({ description: DESCRIPTION, parameters: z.object({ filePath: z.string().describe("The path to the file to read"), - offset: z - .number() - .describe("The line number to start reading from (0-based)") - .optional(), - limit: z - .number() - .describe("The number of lines to read (defaults to 2000)") - .optional(), + offset: z.number().describe("The line number to start reading from (0-based)").optional(), + limit: z.number().describe("The number of lines to read (defaults to 2000)").optional(), }), async execute(params, ctx) { let filePath = params.filePath @@ -40,38 +33,25 @@ export const ReadTool = Tool.define({ const suggestions = dirEntries .filter( (entry) => - entry.toLowerCase().includes(base.toLowerCase()) || - base.toLowerCase().includes(entry.toLowerCase()), + entry.toLowerCase().includes(base.toLowerCase()) || base.toLowerCase().includes(entry.toLowerCase()), ) .map((entry) => path.join(dir, entry)) .slice(0, 3) if (suggestions.length > 0) { - throw new Error( - `File not found: ${filePath}\n\nDid you mean one of these?\n${suggestions.join("\n")}`, - ) + throw new Error(`File not found: ${filePath}\n\nDid you mean one of these?\n${suggestions.join("\n")}`) } throw new Error(`File not found: ${filePath}`) } - const stats = await file.stat() - if (stats.size > MAX_READ_SIZE) - throw new Error( - `File is too large (${stats.size} bytes). Maximum size is ${MAX_READ_SIZE} bytes`, - ) const limit = params.limit ?? DEFAULT_READ_LIMIT const offset = params.offset || 0 const isImage = isImageFile(filePath) - if (isImage) - throw new Error( - `This is an image file of type: ${isImage}\nUse a different tool to process images`, - ) + if (isImage) throw new Error(`This is an image file of type: ${isImage}\nUse a different tool to process images`) const lines = await file.text().then((text) => text.split("\n")) const raw = lines.slice(offset, offset + limit).map((line) => { - return line.length > MAX_LINE_LENGTH - ? line.substring(0, MAX_LINE_LENGTH) + "..." - : line + return line.length > MAX_LINE_LENGTH ? line.substring(0, MAX_LINE_LENGTH) + "..." : line }) const content = raw.map((line, index) => { return `${(index + offset + 1).toString().padStart(5, "0")}| ${line}` @@ -82,21 +62,19 @@ export const ReadTool = Tool.define({ output += content.join("\n") if (lines.length > offset + content.length) { - output += `\n\n(File has more lines. Use 'offset' parameter to read beyond line ${ - offset + content.length - })` + output += `\n\n(File has more lines. Use 'offset' parameter to read beyond line ${offset + content.length})` } output += "\n" // just warms the lsp client - await LSP.touchFile(filePath, false) + LSP.touchFile(filePath, false) FileTime.read(ctx.sessionID, filePath) return { + title: path.relative(App.info().path.root, filePath), output, metadata: { preview, - title: path.relative(App.info().path.root, filePath), }, } }, diff --git a/packages/opencode/src/tool/read.txt b/packages/opencode/src/tool/read.txt index d1bf8c5d..be9e9e0c 100644 --- a/packages/opencode/src/tool/read.txt +++ b/packages/opencode/src/tool/read.txt @@ -2,7 +2,7 @@ Reads a file from the local filesystem. You can access any file directly by usin Assume this tool is able to read all files on the machine. If the User provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned. Usage: -- The file_path parameter must be an absolute path, not a relative path +- The filePath parameter must be an absolute path, not a relative path - By default, it reads up to 2000 lines starting from the beginning of the file - You can optionally specify a line offset and limit (especially handy for long files), but it's recommended to read the whole file by not providing these parameters - Any lines longer than 2000 characters will be truncated diff --git a/packages/opencode/src/tool/task.ts b/packages/opencode/src/tool/task.ts index 2796d0fe..49b89495 100644 --- a/packages/opencode/src/tool/task.ts +++ b/packages/opencode/src/tool/task.ts @@ -3,41 +3,33 @@ import DESCRIPTION from "./task.txt" import { z } from "zod" import { Session } from "../session" import { Bus } from "../bus" -import { Message } from "../session/message" +import { MessageV2 } from "../session/message-v2" +import { Identifier } from "../id/id" export const TaskTool = Tool.define({ id: "task", description: DESCRIPTION, parameters: z.object({ - description: z - .string() - .describe("A short (3-5 words) description of the task"), + description: z.string().describe("A short (3-5 words) description of the task"), prompt: z.string().describe("The task for the agent to perform"), }), async execute(params, ctx) { const session = await Session.create(ctx.sessionID) const msg = await Session.getMessage(ctx.sessionID, ctx.messageID) - const metadata = msg.metadata.assistant! + if (msg.role !== "assistant") throw new Error("Not an assistant message") - function summary(input: Message.Info) { - const result = [] - - for (const part of input.parts) { - if (part.type === "tool-invocation") { - result.push({ - toolInvocation: part.toolInvocation, - metadata: input.metadata.tool[part.toolInvocation.toolCallId], - }) - } - } - return result - } - - const unsub = Bus.subscribe(Message.Event.Updated, async (evt) => { - if (evt.properties.info.metadata.sessionID !== session.id) return + const messageID = Identifier.ascending("message") + const parts: Record = {} + const unsub = Bus.subscribe(MessageV2.Event.PartUpdated, async (evt) => { + if (evt.properties.part.sessionID !== session.id) return + if (evt.properties.part.messageID === messageID) return + if (evt.properties.part.type !== "tool") return + parts[evt.properties.part.id] = evt.properties.part ctx.metadata({ title: params.description, - summary: summary(evt.properties.info), + metadata: { + summary: Object.values(parts).sort((a, b) => a.id?.localeCompare(b.id)), + }, }) }) @@ -45,11 +37,15 @@ export const TaskTool = Tool.define({ Session.abort(session.id) }) const result = await Session.chat({ + messageID, sessionID: session.id, - modelID: metadata.modelID, - providerID: metadata.providerID, + modelID: msg.modelID, + providerID: msg.providerID, parts: [ { + id: Identifier.ascending("part"), + messageID, + sessionID: session.id, type: "text", text: params.prompt, }, @@ -57,9 +53,9 @@ export const TaskTool = Tool.define({ }) unsub() return { + title: params.description, metadata: { - title: params.description, - summary: summary(result), + summary: result.parts.filter((x) => x.type === "tool"), }, output: result.parts.findLast((x) => x.type === "text")!.text, } diff --git a/packages/opencode/src/tool/todo.ts b/packages/opencode/src/tool/todo.ts index 33ac3d12..8a330c2d 100644 --- a/packages/opencode/src/tool/todo.ts +++ b/packages/opencode/src/tool/todo.ts @@ -5,12 +5,8 @@ import { App } from "../app/app" const TodoInfo = z.object({ content: z.string().min(1).describe("Brief description of the task"), - status: z - .enum(["pending", "in_progress", "completed"]) - .describe("Current status of the task"), - priority: z - .enum(["high", "medium", "low"]) - .describe("Priority level of the task"), + status: z.enum(["pending", "in_progress", "completed", "cancelled"]).describe("Current status of the task"), + priority: z.enum(["high", "medium", "low"]).describe("Priority level of the task"), id: z.string().describe("Unique identifier for the todo item"), }) type TodoInfo = z.infer @@ -32,9 +28,9 @@ export const TodoWriteTool = Tool.define({ const todos = state() todos[opts.sessionID] = params.todos return { + title: `${params.todos.filter((x) => x.status !== "completed").length} todos`, output: JSON.stringify(params.todos, null, 2), metadata: { - title: `${params.todos.filter((x) => x.status !== "completed").length} todos`, todos: params.todos, }, } @@ -48,9 +44,9 @@ export const TodoReadTool = Tool.define({ async execute(_params, opts) { const todos = state()[opts.sessionID] ?? [] return { + title: `${todos.filter((x) => x.status !== "completed").length} todos`, metadata: { todos, - title: `${todos.filter((x) => x.status !== "completed").length} todos`, }, output: JSON.stringify(todos, null, 2), } diff --git a/packages/opencode/src/tool/tool.ts b/packages/opencode/src/tool/tool.ts index 8c1cbf48..f44322ed 100644 --- a/packages/opencode/src/tool/tool.ts +++ b/packages/opencode/src/tool/tool.ts @@ -2,19 +2,15 @@ import type { StandardSchemaV1 } from "@standard-schema/spec" export namespace Tool { interface Metadata { - title: string [key: string]: any } export type Context = { sessionID: string messageID: string abort: AbortSignal - metadata(meta: M): void + metadata(input: { title?: string; metadata?: M }): void } - export interface Info< - Parameters extends StandardSchemaV1 = StandardSchemaV1, - M extends Metadata = Metadata, - > { + export interface Info { id: string description: string parameters: Parameters @@ -22,15 +18,15 @@ export namespace Tool { args: StandardSchemaV1.InferOutput, ctx: Context, ): Promise<{ + title: string metadata: M output: string }> } - export function define< - Parameters extends StandardSchemaV1, - Result extends Metadata, - >(input: Info): Info { + export function define( + input: Info, + ): Info { return input } } diff --git a/packages/opencode/src/tool/webfetch.ts b/packages/opencode/src/tool/webfetch.ts index 5b7b9f9d..235d2113 100644 --- a/packages/opencode/src/tool/webfetch.ts +++ b/packages/opencode/src/tool/webfetch.ts @@ -14,9 +14,7 @@ export const WebFetchTool = Tool.define({ url: z.string().describe("The URL to fetch content from"), format: z .enum(["text", "markdown", "html"]) - .describe( - "The format to return the content in (text, markdown, or html)", - ), + .describe("The format to return the content in (text, markdown, or html)"), timeout: z .number() .min(0) @@ -26,17 +24,11 @@ export const WebFetchTool = Tool.define({ }), async execute(params, ctx) { // Validate URL - if ( - !params.url.startsWith("http://") && - !params.url.startsWith("https://") - ) { + if (!params.url.startsWith("http://") && !params.url.startsWith("https://")) { throw new Error("URL must start with http:// or https://") } - const timeout = Math.min( - (params.timeout ?? DEFAULT_TIMEOUT / 1000) * 1000, - MAX_TIMEOUT, - ) + const timeout = Math.min((params.timeout ?? DEFAULT_TIMEOUT / 1000) * 1000, MAX_TIMEOUT) const controller = new AbortController() const timeoutId = setTimeout(() => controller.abort(), timeout) @@ -46,8 +38,7 @@ export const WebFetchTool = Tool.define({ headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", - Accept: - "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8", + Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.9", }, }) @@ -79,16 +70,14 @@ export const WebFetchTool = Tool.define({ const text = await extractTextFromHTML(content) return { output: text, - metadata: { - title, - }, + title, + metadata: {}, } } return { output: content, - metadata: { - title, - }, + title, + metadata: {}, } case "markdown": @@ -96,32 +85,28 @@ export const WebFetchTool = Tool.define({ const markdown = convertHTMLToMarkdown(content) return { output: markdown, - metadata: { - title, - }, + title, + metadata: {}, } } return { output: "```\n" + content + "\n```", - metadata: { - title, - }, + title, + metadata: {}, } case "html": return { output: content, - metadata: { - title, - }, + title, + metadata: {}, } default: return { output: content, - metadata: { - title, - }, + title, + metadata: {}, } } }, @@ -143,16 +128,7 @@ async function extractTextFromHTML(html: string) { .on("*", { element(element) { // Reset skip flag when entering other elements - if ( - ![ - "script", - "style", - "noscript", - "iframe", - "object", - "embed", - ].includes(element.tagName) - ) { + if (!["script", "style", "noscript", "iframe", "object", "embed"].includes(element.tagName)) { skipContent = false } }, diff --git a/packages/opencode/src/tool/write.ts b/packages/opencode/src/tool/write.ts index b0515805..be92d626 100644 --- a/packages/opencode/src/tool/write.ts +++ b/packages/opencode/src/tool/write.ts @@ -13,18 +13,12 @@ export const WriteTool = Tool.define({ id: "write", description: DESCRIPTION, parameters: z.object({ - filePath: z - .string() - .describe( - "The absolute path to the file to write (must be absolute, not relative)", - ), + filePath: z.string().describe("The absolute path to the file to write (must be absolute, not relative)"), content: z.string().describe("The content to write to the file"), }), async execute(params, ctx) { const app = App.info() - const filepath = path.isAbsolute(params.filePath) - ? params.filePath - : path.join(app.path.cwd, params.filePath) + const filepath = path.isAbsolute(params.filePath) ? params.filePath : path.join(app.path.cwd, params.filePath) const file = Bun.file(filepath) const exists = await file.exists() @@ -33,9 +27,7 @@ export const WriteTool = Tool.define({ await Permission.ask({ id: "write", sessionID: ctx.sessionID, - title: exists - ? "Overwrite this file: " + filepath - : "Create new file: " + filepath, + title: exists ? "Overwrite this file: " + filepath : "Create new file: " + filepath, metadata: { filePath: filepath, content: params.content, @@ -62,11 +54,11 @@ export const WriteTool = Tool.define({ } return { + title: path.relative(app.path.root, filepath), metadata: { diagnostics, filepath, exists: exists, - title: path.relative(app.path.root, filepath), }, output, } diff --git a/packages/opencode/src/util/error.ts b/packages/opencode/src/util/error.ts index be8764ee..53b434c6 100644 --- a/packages/opencode/src/util/error.ts +++ b/packages/opencode/src/util/error.ts @@ -7,10 +7,7 @@ export abstract class NamedError extends Error { abstract schema(): ZodSchema abstract toObject(): { name: string; data: any } - static create( - name: Name, - data: Data, - ) { + static create(name: Name, data: Data) { const schema = z .object({ name: z.literal(name), diff --git a/packages/opencode/src/util/filesystem.ts b/packages/opencode/src/util/filesystem.ts index c4fd163c..d5149cf3 100644 --- a/packages/opencode/src/util/filesystem.ts +++ b/packages/opencode/src/util/filesystem.ts @@ -1,7 +1,17 @@ import { exists } from "fs/promises" -import { dirname, join } from "path" +import { dirname, join, relative } from "path" export namespace Filesystem { + export function overlaps(a: string, b: string) { + const relA = relative(a, b) + const relB = relative(b, a) + return !relA || !relA.startsWith("..") || !relB || !relB.startsWith("..") + } + + export function contains(parent: string, child: string) { + return relative(parent, child).startsWith("..") + } + export async function findUp(target: string, start: string, stop?: string) { let current = start const result = [] @@ -16,6 +26,21 @@ export namespace Filesystem { return result } + export async function* up(options: { targets: string[]; start: string; stop?: string }) { + const { targets, start, stop } = options + let current = start + while (true) { + for (const target of targets) { + const search = join(current, target) + if (await exists(search)) yield search + } + if (stop === current) break + const parent = dirname(current) + if (parent === current) break + current = parent + } + } + export async function globUp(pattern: string, start: string, stop?: string) { let current = start const result = [] diff --git a/packages/opencode/src/util/log.ts b/packages/opencode/src/util/log.ts index b73e2dee..2c9c9193 100644 --- a/packages/opencode/src/util/log.ts +++ b/packages/opencode/src/util/log.ts @@ -1,15 +1,59 @@ import path from "path" import fs from "fs/promises" import { Global } from "../global" +import z from "zod" + export namespace Log { + export const Level = z.enum(["DEBUG", "INFO", "WARN", "ERROR"]).openapi({ ref: "LogLevel", description: "Log level" }) + export type Level = z.infer + + const levelPriority: Record = { + DEBUG: 0, + INFO: 1, + WARN: 2, + ERROR: 3, + } + + let currentLevel: Level = "INFO" + + export function setLevel(level: Level) { + currentLevel = level + } + + export function getLevel(): Level { + return currentLevel + } + + function shouldLog(level: Level): boolean { + return levelPriority[level] >= levelPriority[currentLevel] + } + + export type Logger = { + debug(message?: any, extra?: Record): void + info(message?: any, extra?: Record): void + error(message?: any, extra?: Record): void + warn(message?: any, extra?: Record): void + tag(key: string, value: string): Logger + clone(): Logger + time( + message: string, + extra?: Record, + ): { + stop(): void + [Symbol.dispose](): void + } + } + + const loggers = new Map() + export const Default = create({ service: "default" }) export interface Options { print: boolean + level?: Level } let logpath = "" - export function file() { return logpath } @@ -19,10 +63,7 @@ export namespace Log { await fs.mkdir(dir, { recursive: true }) cleanup(dir) if (options.print) return - logpath = path.join( - dir, - new Date().toISOString().split(".")[0].replace(/:/g, "") + ".log", - ) + logpath = path.join(dir, new Date().toISOString().split(".")[0].replace(/:/g, "") + ".log") const logfile = Bun.file(logpath) await fs.truncate(logpath).catch(() => {}) const writer = logfile.writer() @@ -43,41 +84,54 @@ export namespace Log { const filesToDelete = files.slice(0, -10) - await Promise.all( - filesToDelete.map((file) => fs.unlink(file).catch(() => {})), - ) + await Promise.all(filesToDelete.map((file) => fs.unlink(file).catch(() => {}))) } let last = Date.now() export function create(tags?: Record) { tags = tags || {} + const service = tags["service"] + if (service && typeof service === "string") { + const cached = loggers.get(service) + if (cached) { + return cached + } + } + function build(message: any, extra?: Record) { const prefix = Object.entries({ ...tags, ...extra, }) .filter(([_, value]) => value !== undefined && value !== null) - .map(([key, value]) => `${key}=${value}`) + .map(([key, value]) => `${key}=${typeof value === "object" ? JSON.stringify(value) : value}`) .join(" ") const next = new Date() const diff = next.getTime() - last last = next.getTime() - return ( - [next.toISOString().split(".")[0], "+" + diff + "ms", prefix, message] - .filter(Boolean) - .join(" ") + "\n" - ) + return [next.toISOString().split(".")[0], "+" + diff + "ms", prefix, message].filter(Boolean).join(" ") + "\n" } - const result = { + const result: Logger = { + debug(message?: any, extra?: Record) { + if (shouldLog("DEBUG")) { + process.stderr.write("DEBUG " + build(message, extra)) + } + }, info(message?: any, extra?: Record) { - process.stderr.write("INFO " + build(message, extra)) + if (shouldLog("INFO")) { + process.stderr.write("INFO " + build(message, extra)) + } }, error(message?: any, extra?: Record) { - process.stderr.write("ERROR " + build(message, extra)) + if (shouldLog("ERROR")) { + process.stderr.write("ERROR " + build(message, extra)) + } }, warn(message?: any, extra?: Record) { - process.stderr.write("WARN " + build(message, extra)) + if (shouldLog("WARN")) { + process.stderr.write("WARN " + build(message, extra)) + } }, tag(key: string, value: string) { if (tags) tags[key] = value @@ -105,6 +159,10 @@ export namespace Log { }, } + if (service && typeof service === "string") { + loggers.set(service, result) + } + return result } } diff --git a/packages/opencode/sst-env.d.ts b/packages/opencode/sst-env.d.ts index 0397645b..b6a7e906 100644 --- a/packages/opencode/sst-env.d.ts +++ b/packages/opencode/sst-env.d.ts @@ -6,4 +6,4 @@ /// import "sst" -export {} +export {} \ No newline at end of file diff --git a/packages/opencode/test/bun.test.ts b/packages/opencode/test/bun.test.ts new file mode 100644 index 00000000..18f0db6b --- /dev/null +++ b/packages/opencode/test/bun.test.ts @@ -0,0 +1,53 @@ +import { describe, expect, test } from "bun:test" +import fs from "fs/promises" +import path from "path" + +describe("BunProc registry configuration", () => { + test("should not contain hardcoded registry parameters", async () => { + // Read the bun/index.ts file + const bunIndexPath = path.join(__dirname, "../src/bun/index.ts") + const content = await fs.readFile(bunIndexPath, "utf-8") + + // Verify that no hardcoded registry is present + expect(content).not.toContain("--registry=") + expect(content).not.toContain("hasNpmRcConfig") + expect(content).not.toContain("NpmRc") + }) + + test("should use Bun's default registry resolution", async () => { + // Read the bun/index.ts file + const bunIndexPath = path.join(__dirname, "../src/bun/index.ts") + const content = await fs.readFile(bunIndexPath, "utf-8") + + // Verify that it uses Bun's default resolution + expect(content).toContain("Bun's default registry resolution") + expect(content).toContain("Bun will use them automatically") + expect(content).toContain("No need to pass --registry flag") + }) + + test("should have correct command structure without registry", async () => { + // Read the bun/index.ts file + const bunIndexPath = path.join(__dirname, "../src/bun/index.ts") + const content = await fs.readFile(bunIndexPath, "utf-8") + + // Extract the install function + const installFunctionMatch = content.match(/export async function install[\s\S]*?^ }/m) + expect(installFunctionMatch).toBeTruthy() + + if (installFunctionMatch) { + const installFunction = installFunctionMatch[0] + + // Verify expected arguments are present + expect(installFunction).toContain('"add"') + expect(installFunction).toContain('"--force"') + expect(installFunction).toContain('"--exact"') + expect(installFunction).toContain('"--cwd"') + expect(installFunction).toContain('Global.Path.cache') + expect(installFunction).toContain('pkg + "@" + version') + + // Verify no registry argument is added + expect(installFunction).not.toContain('"--registry"') + expect(installFunction).not.toContain('args.push("--registry') + } + }) +}) diff --git a/packages/opencode/test/tool/edit.test.ts b/packages/opencode/test/tool/edit.test.ts index 6de4f2a7..6906062d 100644 --- a/packages/opencode/test/tool/edit.test.ts +++ b/packages/opencode/test/tool/edit.test.ts @@ -17,12 +17,7 @@ const testCases: TestCase[] = [ replace: 'console.log("universe");', }, { - content: [ - "if (condition) {", - " doSomething();", - " doSomethingElse();", - "}", - ].join("\n"), + content: ["if (condition) {", " doSomething();", " doSomethingElse();", "}"].join("\n"), find: [" doSomething();", " doSomethingElse();"].join("\n"), replace: [" doNewThing();", " doAnotherThing();"].join("\n"), }, @@ -53,15 +48,8 @@ const testCases: TestCase[] = [ " return result;", "}", ].join("\n"), - find: [ - "function calculate(a, b) {", - " // different middle content", - " return result;", - "}", - ].join("\n"), - replace: ["function calculate(a, b) {", " return a * b * 2;", "}"].join( - "\n", - ), + find: ["function calculate(a, b) {", " // different middle content", " return result;", "}"].join("\n"), + replace: ["function calculate(a, b) {", " return a * b * 2;", "}"].join("\n"), }, { content: [ @@ -76,13 +64,7 @@ const testCases: TestCase[] = [ "}", ].join("\n"), find: ["class MyClass {", " // different implementation", "}"].join("\n"), - replace: [ - "class MyClass {", - " constructor() {", - " this.value = 42;", - " }", - "}", - ].join("\n"), + replace: ["class MyClass {", " constructor() {", " this.value = 42;", " }", "}"].join("\n"), }, // WhitespaceNormalizedReplacer cases @@ -104,48 +86,21 @@ const testCases: TestCase[] = [ // IndentationFlexibleReplacer cases { - content: [ - " function nested() {", - ' console.log("deeply nested");', - " return true;", - " }", - ].join("\n"), - find: [ - "function nested() {", - ' console.log("deeply nested");', - " return true;", - "}", - ].join("\n"), - replace: [ - "function nested() {", - ' console.log("updated");', - " return false;", - "}", - ].join("\n"), + content: [" function nested() {", ' console.log("deeply nested");', " return true;", " }"].join( + "\n", + ), + find: ["function nested() {", ' console.log("deeply nested");', " return true;", "}"].join("\n"), + replace: ["function nested() {", ' console.log("updated");', " return false;", "}"].join("\n"), }, { - content: [ - " if (true) {", - ' console.log("level 1");', - ' console.log("level 2");', - " }", - ].join("\n"), - find: [ - "if (true) {", - 'console.log("level 1");', - ' console.log("level 2");', - "}", - ].join("\n"), + content: [" if (true) {", ' console.log("level 1");', ' console.log("level 2");', " }"].join("\n"), + find: ["if (true) {", 'console.log("level 1");', ' console.log("level 2");', "}"].join("\n"), replace: ["if (true) {", 'console.log("updated");', "}"].join("\n"), }, // replaceAll option cases { - content: [ - 'console.log("test");', - 'console.log("test");', - 'console.log("test");', - ].join("\n"), + content: ['console.log("test");', 'console.log("test");', 'console.log("test");'].join("\n"), find: 'console.log("test");', replace: 'console.log("updated");', all: true, @@ -213,9 +168,7 @@ const testCases: TestCase[] = [ // MultiOccurrenceReplacer cases (with replaceAll) { - content: ["debug('start');", "debug('middle');", "debug('end');"].join( - "\n", - ), + content: ["debug('start');", "debug('middle');", "debug('end');"].join("\n"), find: "debug", replace: "log", all: true, @@ -239,9 +192,7 @@ const testCases: TestCase[] = [ replace: "const value = 24;", }, { - content: ["", " if (condition) {", " doSomething();", " }", ""].join( - "\n", - ), + content: ["", " if (condition) {", " doSomething();", " }", ""].join("\n"), find: ["if (condition) {", " doSomething();", "}"].join("\n"), replace: ["if (condition) {", " doNothing();", "}"].join("\n"), }, @@ -262,9 +213,7 @@ const testCases: TestCase[] = [ " return result;", "}", ].join("\n"), - replace: ["function calculate(a, b) {", " return (a + b) * 2;", "}"].join( - "\n", - ), + replace: ["function calculate(a, b) {", " return (a + b) * 2;", "}"].join("\n"), }, { content: [ @@ -278,15 +227,8 @@ const testCases: TestCase[] = [ " }", "}", ].join("\n"), - find: [ - "class TestClass {", - " // different implementation", - " // with multiple lines", - "}", - ].join("\n"), - replace: ["class TestClass {", " getValue() { return 42; }", "}"].join( - "\n", - ), + find: ["class TestClass {", " // different implementation", " // with multiple lines", "}"].join("\n"), + replace: ["class TestClass {", " getValue() { return 42; }", "}"].join("\n"), }, // Combined edge cases for new replacers @@ -296,9 +238,7 @@ const testCases: TestCase[] = [ replace: 'console.log("updated");', }, { - content: [" ", "function test() {", " return 'value';", "}", " "].join( - "\n", - ), + content: [" ", "function test() {", " return 'value';", "}", " "].join("\n"), find: ["function test() {", "return 'value';", "}"].join("\n"), replace: ["function test() {", "return 'new value';", "}"].join("\n"), }, @@ -346,13 +286,7 @@ const testCases: TestCase[] = [ // ContextAwareReplacer - test with trailing newline in find string { - content: [ - "class Test {", - " method1() {", - " return 1;", - " }", - "}", - ].join("\n"), + content: ["class Test {", " method1() {", " return 1;", " }", "}"].join("\n"), find: [ "class Test {", " // different content", @@ -401,12 +335,7 @@ describe("EditTool Replacers", () => { replace(testCase.content, testCase.find, testCase.replace, testCase.all) }).toThrow() } else { - const result = replace( - testCase.content, - testCase.find, - testCase.replace, - testCase.all, - ) + const result = replace(testCase.content, testCase.find, testCase.replace, testCase.all) expect(result).toContain(testCase.replace) } }) diff --git a/packages/opencode/test/tool/tool.test.ts b/packages/opencode/test/tool/tool.test.ts index 4723a61d..88325029 100644 --- a/packages/opencode/test/tool/tool.test.ts +++ b/packages/opencode/test/tool/tool.test.ts @@ -42,10 +42,7 @@ describe("tool.glob", () => { describe("tool.ls", () => { test("basic", async () => { const result = await App.provide({ cwd: process.cwd() }, async () => { - return await ListTool.execute( - { path: "./example", ignore: [".git"] }, - ctx, - ) + return await ListTool.execute({ path: "./example", ignore: [".git"] }, ctx) }) expect(result.output).toMatchSnapshot() }) diff --git a/packages/tui/.gitignore b/packages/tui/.gitignore index aac2e0bd..4685365f 100644 --- a/packages/tui/.gitignore +++ b/packages/tui/.gitignore @@ -1 +1,2 @@ opencode-test +cmd/opencode/opencode diff --git a/packages/tui/AGENTS.md b/packages/tui/AGENTS.md deleted file mode 100644 index 753374f9..00000000 --- a/packages/tui/AGENTS.md +++ /dev/null @@ -1,25 +0,0 @@ -# TUI Agent Guidelines - -## Build/Test Commands - -- **Build**: `go build ./cmd/opencode` (builds main binary) -- **Test**: `go test ./...` (runs all tests) -- **Single test**: `go test ./internal/theme -run TestLoadThemesFromJSON` (specific test) -- **Release build**: Uses `.goreleaser.yml` configuration - -## Code Style - -- **Language**: Go 1.24+ with standard formatting (`gofmt`) -- **Imports**: Group standard, third-party, local packages with blank lines -- **Naming**: Go conventions - PascalCase exports, camelCase private, ALL_CAPS constants -- **Error handling**: Return errors explicitly, use `fmt.Errorf` for wrapping -- **Structs**: Define clear interfaces, embed when appropriate -- **Testing**: Use table-driven tests, `t.TempDir()` for file operations - -## Architecture - -- **TUI Framework**: Bubble Tea v2 with Lipgloss v2 for styling -- **Client**: Generated OpenAPI client communicates with TypeScript server -- **Components**: Reusable UI components in `internal/components/` -- **Themes**: JSON-based theming system with override hierarchy -- **State**: Centralized app state with message passing diff --git a/packages/tui/cmd/opencode/main.go b/packages/tui/cmd/opencode/main.go index a9283525..a0b2a376 100644 --- a/packages/tui/cmd/opencode/main.go +++ b/packages/tui/cmd/opencode/main.go @@ -5,14 +5,18 @@ import ( "encoding/json" "log/slog" "os" - "path/filepath" + "os/signal" "strings" + "syscall" tea "github.com/charmbracelet/bubbletea/v2" + flag "github.com/spf13/pflag" "github.com/sst/opencode-sdk-go" "github.com/sst/opencode-sdk-go/option" "github.com/sst/opencode/internal/app" + "github.com/sst/opencode/internal/clipboard" "github.com/sst/opencode/internal/tui" + "github.com/sst/opencode/internal/util" ) var Version = "dev" @@ -23,6 +27,11 @@ func main() { version = "v" + Version } + var model *string = flag.String("model", "", "model to begin with") + var prompt *string = flag.String("prompt", "", "prompt to begin with") + var mode *string = flag.String("mode", "", "mode to begin with") + flag.Parse() + url := os.Getenv("OPENCODE_SERVER") appInfoStr := os.Getenv("OPENCODE_APP_INFO") @@ -33,39 +42,36 @@ func main() { os.Exit(1) } - logfile := filepath.Join(appInfo.Path.Data, "log", "tui.log") - if _, err := os.Stat(filepath.Dir(logfile)); os.IsNotExist(err) { - err := os.MkdirAll(filepath.Dir(logfile), 0755) - if err != nil { - slog.Error("Failed to create log directory", "error", err) - os.Exit(1) - } - } - file, err := os.Create(logfile) + modesStr := os.Getenv("OPENCODE_MODES") + var modes []opencode.Mode + err = json.Unmarshal([]byte(modesStr), &modes) if err != nil { - slog.Error("Failed to create log file", "error", err) + slog.Error("Failed to unmarshal modes", "error", err) os.Exit(1) } - defer file.Close() - logger := slog.New(slog.NewTextHandler(file, &slog.HandlerOptions{Level: slog.LevelDebug})) - slog.SetDefault(logger) - - slog.Debug("TUI launched", "app", appInfo) httpClient := opencode.NewClient( option.WithBaseURL(url), ) - if err != nil { - slog.Error("Failed to create client", "error", err) - os.Exit(1) - } + apiHandler := util.NewAPILogHandler(httpClient, "tui", slog.LevelDebug) + logger := slog.New(apiHandler) + slog.SetDefault(logger) + + slog.Debug("TUI launched", "app", appInfoStr, "modes", modesStr) + + go func() { + err = clipboard.Init() + if err != nil { + slog.Error("Failed to initialize clipboard", "error", err) + } + }() // Create main context for the application ctx, cancel := context.WithCancel(context.Background()) defer cancel() - app_, err := app.New(ctx, version, appInfo, httpClient) + app_, err := app.New(ctx, version, appInfo, modes, httpClient, model, prompt, mode) if err != nil { panic(err) } @@ -73,10 +79,14 @@ func main() { program := tea.NewProgram( tui.NewModel(app_), tea.WithAltScreen(), - tea.WithKeyboardEnhancements(), + // tea.WithKeyboardEnhancements(), tea.WithMouseCellMotion(), ) + // Set up signal handling for graceful shutdown + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT) + go func() { stream := httpClient.Event.ListStreaming(ctx) for stream.Next() { @@ -89,6 +99,13 @@ func main() { } }() + // Handle signals in a separate goroutine + go func() { + sig := <-sigChan + slog.Info("Received signal, shutting down gracefully", "signal", sig) + program.Quit() + }() + // Run the TUI result, err := program.Run() if err != nil { diff --git a/packages/tui/go.mod b/packages/tui/go.mod index 74047af1..f2ad0c56 100644 --- a/packages/tui/go.mod +++ b/packages/tui/go.mod @@ -6,21 +6,26 @@ require ( github.com/BurntSushi/toml v1.5.0 github.com/alecthomas/chroma/v2 v2.18.0 github.com/charmbracelet/bubbles/v2 v2.0.0-beta.1 - github.com/charmbracelet/bubbletea/v2 v2.0.0-beta.3 + github.com/charmbracelet/bubbletea/v2 v2.0.0-beta.4 github.com/charmbracelet/glamour v0.10.0 - github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.1 - github.com/charmbracelet/x/ansi v0.8.0 + github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.3 + github.com/charmbracelet/x/ansi v0.9.3 + github.com/charmbracelet/x/input v0.3.7 + github.com/google/uuid v1.6.0 github.com/lithammer/fuzzysearch v1.1.8 github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 github.com/muesli/reflow v0.3.0 github.com/muesli/termenv v0.16.0 github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 github.com/sst/opencode-sdk-go v0.1.0-alpha.8 - github.com/tidwall/gjson v1.14.4 + golang.org/x/image v0.28.0 rsc.io/qr v0.2.0 ) -replace github.com/sst/opencode-sdk-go => ./sdk +replace ( + github.com/charmbracelet/x/input => ./input + github.com/sst/opencode-sdk-go => ./sdk +) require golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect @@ -29,7 +34,6 @@ require ( github.com/atombender/go-jsonschema v0.20.0 // indirect github.com/charmbracelet/lipgloss v1.1.1-0.20250404203927-76690c660834 // indirect github.com/charmbracelet/x/exp/slice v0.0.0-20250327172914-2fdc97757edf // indirect - github.com/charmbracelet/x/input v0.3.5-0.20250424101541-abb4d9a9b197 // indirect github.com/charmbracelet/x/windows v0.2.1 // indirect github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect github.com/fsnotify/fsnotify v1.8.0 // indirect @@ -37,7 +41,6 @@ require ( github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/swag v0.23.0 // indirect github.com/goccy/go-yaml v1.17.1 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/invopop/yaml v0.3.1 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect @@ -50,23 +53,23 @@ require ( github.com/sosodev/duration v1.3.1 // indirect github.com/speakeasy-api/openapi-overlay v0.9.0 // indirect github.com/spf13/cobra v1.9.1 // indirect + github.com/tidwall/gjson v1.14.4 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/tidwall/sjson v1.2.5 // indirect github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect - golang.org/x/mod v0.24.0 // indirect - golang.org/x/tools v0.31.0 // indirect + golang.org/x/mod v0.25.0 // indirect + golang.org/x/tools v0.34.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) require ( - github.com/atotto/clipboard v0.1.4 + github.com/atotto/clipboard v0.1.4 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/aymerick/douceur v0.2.0 // indirect github.com/charmbracelet/colorprofile v0.3.1 // indirect - github.com/charmbracelet/x/cellbuf v0.0.14-0.20250501183327-ad3bc78c6a81 // indirect + github.com/charmbracelet/x/cellbuf v0.0.14-0.20250505150409-97991a1f17d1 // indirect github.com/charmbracelet/x/term v0.2.1 // indirect - github.com/disintegration/imaging v1.6.2 github.com/dlclark/regexp2 v1.11.5 // indirect github.com/google/go-cmp v0.7.0 // indirect github.com/gorilla/css v1.0.1 // indirect @@ -78,16 +81,15 @@ require ( github.com/muesli/cancelreader v0.2.2 // indirect github.com/rivo/uniseg v0.4.7 github.com/rogpeppe/go-internal v1.14.1 // indirect - github.com/spf13/pflag v1.0.6 // indirect + github.com/spf13/pflag v1.0.6 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect github.com/yuin/goldmark v1.7.8 // indirect github.com/yuin/goldmark-emoji v1.0.5 // indirect - golang.org/x/image v0.26.0 - golang.org/x/net v0.39.0 // indirect - golang.org/x/sync v0.13.0 // indirect - golang.org/x/sys v0.32.0 // indirect - golang.org/x/term v0.31.0 // indirect - golang.org/x/text v0.24.0 + golang.org/x/net v0.41.0 // indirect + golang.org/x/sync v0.15.0 // indirect + golang.org/x/sys v0.33.0 // indirect + golang.org/x/term v0.32.0 // indirect + golang.org/x/text v0.26.0 gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/packages/tui/go.sum b/packages/tui/go.sum index fdc5bbb0..370ea712 100644 --- a/packages/tui/go.sum +++ b/packages/tui/go.sum @@ -22,26 +22,24 @@ github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuP github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= github.com/charmbracelet/bubbles/v2 v2.0.0-beta.1 h1:swACzss0FjnyPz1enfX56GKkLiuKg5FlyVmOLIlU2kE= github.com/charmbracelet/bubbles/v2 v2.0.0-beta.1/go.mod h1:6HamsBKWqEC/FVHuQMHgQL+knPyvHH55HwJDHl/adMw= -github.com/charmbracelet/bubbletea/v2 v2.0.0-beta.3 h1:5A2e3myxXMpCES+kjEWgGsaf9VgZXjZbLi5iMTH7j40= -github.com/charmbracelet/bubbletea/v2 v2.0.0-beta.3/go.mod h1:ZFDg5oPjyRYrPAa3iFrtP1DO8xy+LUQxd9JFHEcuwJY= +github.com/charmbracelet/bubbletea/v2 v2.0.0-beta.4 h1:UgUuKKvBwgqm2ZEL+sKv/OLeavrUb4gfHgdxe6oIOno= +github.com/charmbracelet/bubbletea/v2 v2.0.0-beta.4/go.mod h1:0wWFRpsgF7vHsCukVZ5LAhZkiR4j875H6KEM2/tFQmA= github.com/charmbracelet/colorprofile v0.3.1 h1:k8dTHMd7fgw4bnFd7jXTLZrSU/CQrKnL3m+AxCzDz40= github.com/charmbracelet/colorprofile v0.3.1/go.mod h1:/GkGusxNs8VB/RSOh3fu0TJmQ4ICMMPApIIVn0KszZ0= github.com/charmbracelet/glamour v0.10.0 h1:MtZvfwsYCx8jEPFJm3rIBFIMZUfUJ765oX8V6kXldcY= github.com/charmbracelet/glamour v0.10.0/go.mod h1:f+uf+I/ChNmqo087elLnVdCiVgjSKWuXa/l6NU2ndYk= github.com/charmbracelet/lipgloss v1.1.1-0.20250404203927-76690c660834 h1:ZR7e0ro+SZZiIZD7msJyA+NjkCNNavuiPBLgerbOziE= github.com/charmbracelet/lipgloss v1.1.1-0.20250404203927-76690c660834/go.mod h1:aKC/t2arECF6rNOnaKaVU6y4t4ZeHQzqfxedE/VkVhA= -github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.1 h1:D9AJJuYTN5pvz6mpIGO1ijLKpfTYSHOtKGgwoTQ4Gog= -github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.1/go.mod h1:tRlx/Hu0lo/j9viunCN2H+Ze6JrmdjQlXUQvvArgaOc= -github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2llXn7xE= -github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q= -github.com/charmbracelet/x/cellbuf v0.0.14-0.20250501183327-ad3bc78c6a81 h1:iGrflaL5jQW6crML+pZx/ulWAVZQR3CQoRGvFsr2Tyg= -github.com/charmbracelet/x/cellbuf v0.0.14-0.20250501183327-ad3bc78c6a81/go.mod h1:poPFOXFTsJsnLbkV3H2KxAAXT7pdjxxLujLocWjkyzM= +github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.3 h1:W6DpZX6zSkZr0iFq6JVh1vItLoxfYtNlaxOJtWp8Kis= +github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.3/go.mod h1:65HTtKURcv/ict9ZQhr6zT84JqIjMcJbyrZYHHKNfKA= +github.com/charmbracelet/x/ansi v0.9.3 h1:BXt5DHS/MKF+LjuK4huWrC6NCvHtexww7dMayh6GXd0= +github.com/charmbracelet/x/ansi v0.9.3/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE= +github.com/charmbracelet/x/cellbuf v0.0.14-0.20250505150409-97991a1f17d1 h1:MTSs/nsZNfZPbYk/r9hluK2BtwoqvEYruAujNVwgDv0= +github.com/charmbracelet/x/cellbuf v0.0.14-0.20250505150409-97991a1f17d1/go.mod h1:xBlh2Yi3DL3zy/2n15kITpg0YZardf/aa/hgUaIM6Rk= github.com/charmbracelet/x/exp/golden v0.0.0-20250207160936-21c02780d27a h1:FsHEJ52OC4VuTzU8t+n5frMjLvpYWEznSr/u8tnkCYw= github.com/charmbracelet/x/exp/golden v0.0.0-20250207160936-21c02780d27a/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U= github.com/charmbracelet/x/exp/slice v0.0.0-20250327172914-2fdc97757edf h1:rLG0Yb6MQSDKdB52aGX55JT1oi0P0Kuaj7wi1bLUpnI= github.com/charmbracelet/x/exp/slice v0.0.0-20250327172914-2fdc97757edf/go.mod h1:B3UgsnsBZS/eX42BlaNiJkD1pPOUa+oF1IYC6Yd2CEU= -github.com/charmbracelet/x/input v0.3.5-0.20250424101541-abb4d9a9b197 h1:fsWj8NF5njyMVzELc7++HsvRDvgz3VcgGAUgWBDWWWM= -github.com/charmbracelet/x/input v0.3.5-0.20250424101541-abb4d9a9b197/go.mod h1:xseGeVftoP9rVI+/8WKYrJFH6ior6iERGvklwwHz5+s= github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ= github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg= github.com/charmbracelet/x/windows v0.2.1 h1:3x7vnbpQrjpuq/4L+I4gNsG5htYoCiA5oe9hLjAij5I= @@ -54,8 +52,6 @@ github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= -github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ= github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/dprotaso/go-yit v0.0.0-20191028211022-135eb7262960/go.mod h1:9HQzr9D/0PGwMEbC3d5AB7oi67+h4TsQqItC1GVYG58= @@ -218,14 +214,13 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 h1:nDVHiLt8aIbd/VzvPWN6kSOPE7+F/fNFDSXLVYkE/Iw= golang.org/x/exp v0.0.0-20250305212735-054e65f0b394/go.mod h1:sIifuuw/Yco/y6yb6+bDNfyeQ/MdPUy/hKEMYQV17cM= -golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/image v0.26.0 h1:4XjIFEZWQmCZi6Wv8BoxsDhRU3RVnLX04dToTDAEPlY= -golang.org/x/image v0.26.0/go.mod h1:lcxbMFAovzpnJxzXS3nyL83K27tmqtKzIJpctK8YO5c= +golang.org/x/image v0.28.0 h1:gdem5JW1OLS4FbkWgLO+7ZeFzYtL3xClb97GaUzYMFE= +golang.org/x/image v0.28.0/go.mod h1:GUJYXtnGKEUgggyzh+Vxt+AviiCcyiwpsl8iQ8MvwGY= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= -golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= +golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w= +golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -236,15 +231,15 @@ golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY= -golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E= +golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw= +golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610= -golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8= +golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -263,28 +258,28 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20= -golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= +golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o= -golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw= +golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg= +golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0= -golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU= +golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M= +golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU= -golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ= +golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo= +golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/packages/tui/input/cancelreader_other.go b/packages/tui/input/cancelreader_other.go new file mode 100644 index 00000000..dbd22a2e --- /dev/null +++ b/packages/tui/input/cancelreader_other.go @@ -0,0 +1,14 @@ +//go:build !windows +// +build !windows + +package input + +import ( + "io" + + "github.com/muesli/cancelreader" +) + +func newCancelreader(r io.Reader, _ int) (cancelreader.CancelReader, error) { + return cancelreader.NewReader(r) //nolint:wrapcheck +} diff --git a/packages/tui/input/cancelreader_windows.go b/packages/tui/input/cancelreader_windows.go new file mode 100644 index 00000000..19abfce4 --- /dev/null +++ b/packages/tui/input/cancelreader_windows.go @@ -0,0 +1,143 @@ +//go:build windows +// +build windows + +package input + +import ( + "fmt" + "io" + "os" + "sync" + + xwindows "github.com/charmbracelet/x/windows" + "github.com/muesli/cancelreader" + "golang.org/x/sys/windows" +) + +type conInputReader struct { + cancelMixin + conin windows.Handle + originalMode uint32 +} + +var _ cancelreader.CancelReader = &conInputReader{} + +func newCancelreader(r io.Reader, flags int) (cancelreader.CancelReader, error) { + fallback := func(io.Reader) (cancelreader.CancelReader, error) { + return cancelreader.NewReader(r) + } + + var dummy uint32 + if f, ok := r.(cancelreader.File); !ok || f.Fd() != os.Stdin.Fd() || + // If data was piped to the standard input, it does not emit events + // anymore. We can detect this if the console mode cannot be set anymore, + // in this case, we fallback to the default cancelreader implementation. + windows.GetConsoleMode(windows.Handle(f.Fd()), &dummy) != nil { + return fallback(r) + } + + conin, err := windows.GetStdHandle(windows.STD_INPUT_HANDLE) + if err != nil { + return fallback(r) + } + + // Discard any pending input events. + if err := xwindows.FlushConsoleInputBuffer(conin); err != nil { + return fallback(r) + } + + modes := []uint32{ + windows.ENABLE_WINDOW_INPUT, + windows.ENABLE_EXTENDED_FLAGS, + } + + // Enabling mouse mode implicitly blocks console text selection. Thus, we + // need to enable it only if the mouse mode is requested. + // In order to toggle mouse mode, the caller must recreate the reader with + // the appropriate flag toggled. + if flags&FlagMouseMode != 0 { + modes = append(modes, windows.ENABLE_MOUSE_INPUT) + } + + originalMode, err := prepareConsole(conin, modes...) + if err != nil { + return nil, fmt.Errorf("failed to prepare console input: %w", err) + } + + return &conInputReader{ + conin: conin, + originalMode: originalMode, + }, nil +} + +// Cancel implements cancelreader.CancelReader. +func (r *conInputReader) Cancel() bool { + r.setCanceled() + + return windows.CancelIoEx(r.conin, nil) == nil || windows.CancelIo(r.conin) == nil +} + +// Close implements cancelreader.CancelReader. +func (r *conInputReader) Close() error { + if r.originalMode != 0 { + err := windows.SetConsoleMode(r.conin, r.originalMode) + if err != nil { + return fmt.Errorf("reset console mode: %w", err) + } + } + + return nil +} + +// Read implements cancelreader.CancelReader. +func (r *conInputReader) Read(data []byte) (int, error) { + if r.isCanceled() { + return 0, cancelreader.ErrCanceled + } + + var n uint32 + if err := windows.ReadFile(r.conin, data, &n, nil); err != nil { + return int(n), fmt.Errorf("read console input: %w", err) + } + + return int(n), nil +} + +func prepareConsole(input windows.Handle, modes ...uint32) (originalMode uint32, err error) { + err = windows.GetConsoleMode(input, &originalMode) + if err != nil { + return 0, fmt.Errorf("get console mode: %w", err) + } + + var newMode uint32 + for _, mode := range modes { + newMode |= mode + } + + err = windows.SetConsoleMode(input, newMode) + if err != nil { + return 0, fmt.Errorf("set console mode: %w", err) + } + + return originalMode, nil +} + +// cancelMixin represents a goroutine-safe cancelation status. +type cancelMixin struct { + unsafeCanceled bool + lock sync.Mutex +} + +func (c *cancelMixin) setCanceled() { + c.lock.Lock() + defer c.lock.Unlock() + + c.unsafeCanceled = true +} + +func (c *cancelMixin) isCanceled() bool { + c.lock.Lock() + defer c.lock.Unlock() + + return c.unsafeCanceled +} diff --git a/packages/tui/input/clipboard.go b/packages/tui/input/clipboard.go new file mode 100644 index 00000000..725a2d95 --- /dev/null +++ b/packages/tui/input/clipboard.go @@ -0,0 +1,25 @@ +package input + +import "github.com/charmbracelet/x/ansi" + +// ClipboardSelection represents a clipboard selection. The most common +// clipboard selections are "system" and "primary" and selections. +type ClipboardSelection = byte + +// Clipboard selections. +const ( + SystemClipboard ClipboardSelection = ansi.SystemClipboard + PrimaryClipboard ClipboardSelection = ansi.PrimaryClipboard +) + +// ClipboardEvent is a clipboard read message event. This message is emitted when +// a terminal receives an OSC52 clipboard read message event. +type ClipboardEvent struct { + Content string + Selection ClipboardSelection +} + +// String returns the string representation of the clipboard message. +func (e ClipboardEvent) String() string { + return e.Content +} diff --git a/packages/tui/input/color.go b/packages/tui/input/color.go new file mode 100644 index 00000000..9bcf7499 --- /dev/null +++ b/packages/tui/input/color.go @@ -0,0 +1,136 @@ +package input + +import ( + "fmt" + "image/color" + "math" +) + +// ForegroundColorEvent represents a foreground color event. This event is +// emitted when the terminal requests the terminal foreground color using +// [ansi.RequestForegroundColor]. +type ForegroundColorEvent struct{ color.Color } + +// String returns the hex representation of the color. +func (e ForegroundColorEvent) String() string { + return colorToHex(e.Color) +} + +// IsDark returns whether the color is dark. +func (e ForegroundColorEvent) IsDark() bool { + return isDarkColor(e.Color) +} + +// BackgroundColorEvent represents a background color event. This event is +// emitted when the terminal requests the terminal background color using +// [ansi.RequestBackgroundColor]. +type BackgroundColorEvent struct{ color.Color } + +// String returns the hex representation of the color. +func (e BackgroundColorEvent) String() string { + return colorToHex(e) +} + +// IsDark returns whether the color is dark. +func (e BackgroundColorEvent) IsDark() bool { + return isDarkColor(e.Color) +} + +// CursorColorEvent represents a cursor color change event. This event is +// emitted when the program requests the terminal cursor color using +// [ansi.RequestCursorColor]. +type CursorColorEvent struct{ color.Color } + +// String returns the hex representation of the color. +func (e CursorColorEvent) String() string { + return colorToHex(e) +} + +// IsDark returns whether the color is dark. +func (e CursorColorEvent) IsDark() bool { + return isDarkColor(e) +} + +type shiftable interface { + ~uint | ~uint16 | ~uint32 | ~uint64 +} + +func shift[T shiftable](x T) T { + if x > 0xff { + x >>= 8 + } + return x +} + +func colorToHex(c color.Color) string { + if c == nil { + return "" + } + r, g, b, _ := c.RGBA() + return fmt.Sprintf("#%02x%02x%02x", shift(r), shift(g), shift(b)) +} + +func getMaxMin(a, b, c float64) (ma, mi float64) { + if a > b { + ma = a + mi = b + } else { + ma = b + mi = a + } + if c > ma { + ma = c + } else if c < mi { + mi = c + } + return ma, mi +} + +func round(x float64) float64 { + return math.Round(x*1000) / 1000 +} + +// rgbToHSL converts an RGB triple to an HSL triple. +func rgbToHSL(r, g, b uint8) (h, s, l float64) { + // convert uint32 pre-multiplied value to uint8 + // The r,g,b values are divided by 255 to change the range from 0..255 to 0..1: + Rnot := float64(r) / 255 + Gnot := float64(g) / 255 + Bnot := float64(b) / 255 + Cmax, Cmin := getMaxMin(Rnot, Gnot, Bnot) + Δ := Cmax - Cmin + // Lightness calculation: + l = (Cmax + Cmin) / 2 + // Hue and Saturation Calculation: + if Δ == 0 { + h = 0 + s = 0 + } else { + switch Cmax { + case Rnot: + h = 60 * (math.Mod((Gnot-Bnot)/Δ, 6)) + case Gnot: + h = 60 * (((Bnot - Rnot) / Δ) + 2) + case Bnot: + h = 60 * (((Rnot - Gnot) / Δ) + 4) + } + if h < 0 { + h += 360 + } + + s = Δ / (1 - math.Abs((2*l)-1)) + } + + return h, round(s), round(l) +} + +// isDarkColor returns whether the given color is dark. +func isDarkColor(c color.Color) bool { + if c == nil { + return true + } + + r, g, b, _ := c.RGBA() + _, _, l := rgbToHSL(uint8(r>>8), uint8(g>>8), uint8(b>>8)) //nolint:gosec + return l < 0.5 +} diff --git a/packages/tui/input/cursor.go b/packages/tui/input/cursor.go new file mode 100644 index 00000000..cf4e973d --- /dev/null +++ b/packages/tui/input/cursor.go @@ -0,0 +1,7 @@ +package input + +import "image" + +// CursorPositionEvent represents a cursor position event. Where X is the +// zero-based column and Y is the zero-based row. +type CursorPositionEvent image.Point diff --git a/packages/tui/input/da1.go b/packages/tui/input/da1.go new file mode 100644 index 00000000..c2cd94cf --- /dev/null +++ b/packages/tui/input/da1.go @@ -0,0 +1,18 @@ +package input + +import "github.com/charmbracelet/x/ansi" + +// PrimaryDeviceAttributesEvent is an event that represents the terminal +// primary device attributes. +type PrimaryDeviceAttributesEvent []int + +func parsePrimaryDevAttrs(params ansi.Params) Event { + // Primary Device Attributes + da1 := make(PrimaryDeviceAttributesEvent, len(params)) + for i, p := range params { + if !p.HasMore() { + da1[i] = p.Param(0) + } + } + return da1 +} diff --git a/packages/tui/input/doc.go b/packages/tui/input/doc.go new file mode 100644 index 00000000..2877d496 --- /dev/null +++ b/packages/tui/input/doc.go @@ -0,0 +1,6 @@ +// Package input provides a set of utilities for handling input events in a +// terminal environment. It includes support for reading input events, parsing +// escape sequences, and handling clipboard events. +// The package is designed to work with various terminal types and supports +// customization through flags and options. +package input diff --git a/packages/tui/input/driver.go b/packages/tui/input/driver.go new file mode 100644 index 00000000..1e34677a --- /dev/null +++ b/packages/tui/input/driver.go @@ -0,0 +1,192 @@ +//nolint:unused,revive,nolintlint +package input + +import ( + "bytes" + "io" + "unicode/utf8" + + "github.com/muesli/cancelreader" +) + +// Logger is a simple logger interface. +type Logger interface { + Printf(format string, v ...any) +} + +// win32InputState is a state machine for parsing key events from the Windows +// Console API into escape sequences and utf8 runes, and keeps track of the last +// control key state to determine modifier key changes. It also keeps track of +// the last mouse button state and window size changes to determine which mouse +// buttons were released and to prevent multiple size events from firing. +type win32InputState struct { + ansiBuf [256]byte + ansiIdx int + utf16Buf [2]rune + utf16Half bool + lastCks uint32 // the last control key state for the previous event + lastMouseBtns uint32 // the last mouse button state for the previous event + lastWinsizeX, lastWinsizeY int16 // the last window size for the previous event to prevent multiple size events from firing +} + +// Reader represents an input event reader. It reads input events and parses +// escape sequences from the terminal input buffer and translates them into +// human‑readable events. +type Reader struct { + rd cancelreader.CancelReader + table map[string]Key // table is a lookup table for key sequences. + term string // $TERM + paste []byte // bracketed paste buffer; nil when disabled + buf [256]byte // read buffer + partialSeq []byte // holds incomplete escape sequences + keyState win32InputState + parser Parser + logger Logger +} + +// NewReader returns a new input event reader. +func NewReader(r io.Reader, termType string, flags int) (*Reader, error) { + d := new(Reader) + cr, err := newCancelreader(r, flags) + if err != nil { + return nil, err + } + + d.rd = cr + d.table = buildKeysTable(flags, termType) + d.term = termType + d.parser.flags = flags + return d, nil +} + +// SetLogger sets a logger for the reader. +func (d *Reader) SetLogger(l Logger) { d.logger = l } + +// Read implements io.Reader. +func (d *Reader) Read(p []byte) (int, error) { return d.rd.Read(p) } + +// Cancel cancels the underlying reader. +func (d *Reader) Cancel() bool { return d.rd.Cancel() } + +// Close closes the underlying reader. +func (d *Reader) Close() error { return d.rd.Close() } + +func (d *Reader) readEvents() ([]Event, error) { + nb, err := d.rd.Read(d.buf[:]) + if err != nil { + return nil, err + } + + var events []Event + + // Combine any partial sequence from previous read with new data. + var buf []byte + if len(d.partialSeq) > 0 { + buf = make([]byte, len(d.partialSeq)+nb) + copy(buf, d.partialSeq) + copy(buf[len(d.partialSeq):], d.buf[:nb]) + d.partialSeq = nil + } else { + buf = d.buf[:nb] + } + + // Fast path: direct lookup for simple escape sequences. + if bytes.HasPrefix(buf, []byte{0x1b}) { + if k, ok := d.table[string(buf)]; ok { + if d.logger != nil { + d.logger.Printf("input: %q", buf) + } + events = append(events, KeyPressEvent(k)) + return events, nil + } + } + + var i int + for i < len(buf) { + consumed, ev := d.parser.parseSequence(buf[i:]) + if d.logger != nil && consumed > 0 { + d.logger.Printf("input: %q", buf[i:i+consumed]) + } + + // Incomplete sequence – store remainder and exit. + if consumed == 0 && ev == nil { + rem := len(buf) - i + if rem > 0 { + d.partialSeq = make([]byte, rem) + copy(d.partialSeq, buf[i:]) + } + break + } + + // Handle bracketed paste specially so we don’t emit a paste event for + // every byte. + if d.paste != nil { + if _, ok := ev.(PasteEndEvent); !ok { + d.paste = append(d.paste, buf[i]) + i++ + continue + } + } + + switch ev.(type) { + case PasteStartEvent: + d.paste = []byte{} + case PasteEndEvent: + var paste []rune + for len(d.paste) > 0 { + r, w := utf8.DecodeRune(d.paste) + if r != utf8.RuneError { + paste = append(paste, r) + } + d.paste = d.paste[w:] + } + d.paste = nil + events = append(events, PasteEvent(paste)) + case nil: + i++ + continue + } + + if mevs, ok := ev.(MultiEvent); ok { + events = append(events, []Event(mevs)...) + } else { + events = append(events, ev) + } + i += consumed + } + + // Collapse bursts of wheel/motion events into a single event each. + events = coalesceMouseEvents(events) + return events, nil +} + +// coalesceMouseEvents reduces the volume of MouseWheelEvent and MouseMotionEvent +// objects that arrive in rapid succession by keeping only the most recent +// event in each contiguous run. +func coalesceMouseEvents(in []Event) []Event { + if len(in) < 2 { + return in + } + + out := make([]Event, 0, len(in)) + for _, ev := range in { + switch ev.(type) { + case MouseWheelEvent: + if len(out) > 0 { + if _, ok := out[len(out)-1].(MouseWheelEvent); ok { + out[len(out)-1] = ev // replace previous wheel event + continue + } + } + case MouseMotionEvent: + if len(out) > 0 { + if _, ok := out[len(out)-1].(MouseMotionEvent); ok { + out[len(out)-1] = ev // replace previous motion event + continue + } + } + } + out = append(out, ev) + } + return out +} diff --git a/packages/tui/input/driver_other.go b/packages/tui/input/driver_other.go new file mode 100644 index 00000000..fd3df06c --- /dev/null +++ b/packages/tui/input/driver_other.go @@ -0,0 +1,17 @@ +//go:build !windows +// +build !windows + +package input + +// ReadEvents reads input events from the terminal. +// +// It reads the events available in the input buffer and returns them. +func (d *Reader) ReadEvents() ([]Event, error) { + return d.readEvents() +} + +// parseWin32InputKeyEvent parses a Win32 input key events. This function is +// only available on Windows. +func (p *Parser) parseWin32InputKeyEvent(*win32InputState, uint16, uint16, rune, bool, uint32, uint16) Event { + return nil +} diff --git a/packages/tui/input/driver_test.go b/packages/tui/input/driver_test.go new file mode 100644 index 00000000..affdf5b8 --- /dev/null +++ b/packages/tui/input/driver_test.go @@ -0,0 +1,25 @@ +package input + +import ( + "io" + "strings" + "testing" +) + +func BenchmarkDriver(b *testing.B) { + input := "\x1b\x1b[Ztest\x00\x1b]10;1234/1234/1234\x07\x1b[27;2;27~" + rdr := strings.NewReader(input) + drv, err := NewReader(rdr, "dumb", 0) + if err != nil { + b.Fatalf("could not create driver: %v", err) + } + + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + rdr.Reset(input) + if _, err := drv.ReadEvents(); err != nil && err != io.EOF { + b.Errorf("error reading input: %v", err) + } + } +} diff --git a/packages/tui/input/driver_windows.go b/packages/tui/input/driver_windows.go new file mode 100644 index 00000000..acdd6984 --- /dev/null +++ b/packages/tui/input/driver_windows.go @@ -0,0 +1,620 @@ +//go:build windows +// +build windows + +package input + +import ( + "errors" + "fmt" + "strings" + "time" + "unicode" + "unicode/utf16" + "unicode/utf8" + + "github.com/charmbracelet/x/ansi" + xwindows "github.com/charmbracelet/x/windows" + "github.com/muesli/cancelreader" + "golang.org/x/sys/windows" +) + +// ReadEvents reads input events from the terminal. +// +// It reads the events available in the input buffer and returns them. +func (d *Reader) ReadEvents() ([]Event, error) { + events, err := d.handleConInput() + if errors.Is(err, errNotConInputReader) { + return d.readEvents() + } + return events, err +} + +var errNotConInputReader = fmt.Errorf("handleConInput: not a conInputReader") + +func (d *Reader) handleConInput() ([]Event, error) { + cc, ok := d.rd.(*conInputReader) + if !ok { + return nil, errNotConInputReader + } + + var ( + events []xwindows.InputRecord + err error + ) + for { + // Peek up to 256 events, this is to allow for sequences events reported as + // key events. + events, err = peekNConsoleInputs(cc.conin, 256) + if cc.isCanceled() { + return nil, cancelreader.ErrCanceled + } + if err != nil { + return nil, fmt.Errorf("peek coninput events: %w", err) + } + if len(events) > 0 { + break + } + + // Sleep for a bit to avoid busy waiting. + time.Sleep(10 * time.Millisecond) + } + + events, err = readNConsoleInputs(cc.conin, uint32(len(events))) + if cc.isCanceled() { + return nil, cancelreader.ErrCanceled + } + if err != nil { + return nil, fmt.Errorf("read coninput events: %w", err) + } + + var evs []Event + for _, event := range events { + if e := d.parser.parseConInputEvent(event, &d.keyState); e != nil { + if multi, ok := e.(MultiEvent); ok { + evs = append(evs, multi...) + } else { + evs = append(evs, e) + } + } + } + + return evs, nil +} + +func (p *Parser) parseConInputEvent(event xwindows.InputRecord, keyState *win32InputState) Event { + switch event.EventType { + case xwindows.KEY_EVENT: + kevent := event.KeyEvent() + return p.parseWin32InputKeyEvent(keyState, kevent.VirtualKeyCode, kevent.VirtualScanCode, + kevent.Char, kevent.KeyDown, kevent.ControlKeyState, kevent.RepeatCount) + + case xwindows.WINDOW_BUFFER_SIZE_EVENT: + wevent := event.WindowBufferSizeEvent() + if wevent.Size.X != keyState.lastWinsizeX || wevent.Size.Y != keyState.lastWinsizeY { + keyState.lastWinsizeX, keyState.lastWinsizeY = wevent.Size.X, wevent.Size.Y + return WindowSizeEvent{ + Width: int(wevent.Size.X), + Height: int(wevent.Size.Y), + } + } + case xwindows.MOUSE_EVENT: + mevent := event.MouseEvent() + Event := mouseEvent(keyState.lastMouseBtns, mevent) + keyState.lastMouseBtns = mevent.ButtonState + return Event + case xwindows.FOCUS_EVENT: + fevent := event.FocusEvent() + if fevent.SetFocus { + return FocusEvent{} + } + return BlurEvent{} + case xwindows.MENU_EVENT: + // ignore + } + return nil +} + +func mouseEventButton(p, s uint32) (MouseButton, bool) { + var isRelease bool + button := MouseNone + btn := p ^ s + if btn&s == 0 { + isRelease = true + } + + if btn == 0 { + switch { + case s&xwindows.FROM_LEFT_1ST_BUTTON_PRESSED > 0: + button = MouseLeft + case s&xwindows.FROM_LEFT_2ND_BUTTON_PRESSED > 0: + button = MouseMiddle + case s&xwindows.RIGHTMOST_BUTTON_PRESSED > 0: + button = MouseRight + case s&xwindows.FROM_LEFT_3RD_BUTTON_PRESSED > 0: + button = MouseBackward + case s&xwindows.FROM_LEFT_4TH_BUTTON_PRESSED > 0: + button = MouseForward + } + return button, isRelease + } + + switch btn { + case xwindows.FROM_LEFT_1ST_BUTTON_PRESSED: // left button + button = MouseLeft + case xwindows.RIGHTMOST_BUTTON_PRESSED: // right button + button = MouseRight + case xwindows.FROM_LEFT_2ND_BUTTON_PRESSED: // middle button + button = MouseMiddle + case xwindows.FROM_LEFT_3RD_BUTTON_PRESSED: // unknown (possibly mouse backward) + button = MouseBackward + case xwindows.FROM_LEFT_4TH_BUTTON_PRESSED: // unknown (possibly mouse forward) + button = MouseForward + } + + return button, isRelease +} + +func mouseEvent(p uint32, e xwindows.MouseEventRecord) (ev Event) { + var mod KeyMod + var isRelease bool + if e.ControlKeyState&(xwindows.LEFT_ALT_PRESSED|xwindows.RIGHT_ALT_PRESSED) != 0 { + mod |= ModAlt + } + if e.ControlKeyState&(xwindows.LEFT_CTRL_PRESSED|xwindows.RIGHT_CTRL_PRESSED) != 0 { + mod |= ModCtrl + } + if e.ControlKeyState&(xwindows.SHIFT_PRESSED) != 0 { + mod |= ModShift + } + + m := Mouse{ + X: int(e.MousePositon.X), + Y: int(e.MousePositon.Y), + Mod: mod, + } + + wheelDirection := int16(highWord(e.ButtonState)) //nolint:gosec + switch e.EventFlags { + case 0, xwindows.DOUBLE_CLICK: + m.Button, isRelease = mouseEventButton(p, e.ButtonState) + case xwindows.MOUSE_WHEELED: + if wheelDirection > 0 { + m.Button = MouseWheelUp + } else { + m.Button = MouseWheelDown + } + case xwindows.MOUSE_HWHEELED: + if wheelDirection > 0 { + m.Button = MouseWheelRight + } else { + m.Button = MouseWheelLeft + } + case xwindows.MOUSE_MOVED: + m.Button, _ = mouseEventButton(p, e.ButtonState) + return MouseMotionEvent(m) + } + + if isWheel(m.Button) { + return MouseWheelEvent(m) + } else if isRelease { + return MouseReleaseEvent(m) + } + + return MouseClickEvent(m) +} + +func highWord(data uint32) uint16 { + return uint16((data & 0xFFFF0000) >> 16) //nolint:gosec +} + +func readNConsoleInputs(console windows.Handle, maxEvents uint32) ([]xwindows.InputRecord, error) { + if maxEvents == 0 { + return nil, fmt.Errorf("maxEvents cannot be zero") + } + + records := make([]xwindows.InputRecord, maxEvents) + n, err := readConsoleInput(console, records) + return records[:n], err +} + +func readConsoleInput(console windows.Handle, inputRecords []xwindows.InputRecord) (uint32, error) { + if len(inputRecords) == 0 { + return 0, fmt.Errorf("size of input record buffer cannot be zero") + } + + var read uint32 + + err := xwindows.ReadConsoleInput(console, &inputRecords[0], uint32(len(inputRecords)), &read) //nolint:gosec + + return read, err //nolint:wrapcheck +} + +func peekConsoleInput(console windows.Handle, inputRecords []xwindows.InputRecord) (uint32, error) { + if len(inputRecords) == 0 { + return 0, fmt.Errorf("size of input record buffer cannot be zero") + } + + var read uint32 + + err := xwindows.PeekConsoleInput(console, &inputRecords[0], uint32(len(inputRecords)), &read) //nolint:gosec + + return read, err //nolint:wrapcheck +} + +func peekNConsoleInputs(console windows.Handle, maxEvents uint32) ([]xwindows.InputRecord, error) { + if maxEvents == 0 { + return nil, fmt.Errorf("maxEvents cannot be zero") + } + + records := make([]xwindows.InputRecord, maxEvents) + n, err := peekConsoleInput(console, records) + return records[:n], err +} + +// parseWin32InputKeyEvent parses a single key event from either the Windows +// Console API or win32-input-mode events. When state is nil, it means this is +// an event from win32-input-mode. Otherwise, it's a key event from the Windows +// Console API and needs a state to decode ANSI escape sequences and utf16 +// runes. +func (p *Parser) parseWin32InputKeyEvent(state *win32InputState, vkc uint16, _ uint16, r rune, keyDown bool, cks uint32, repeatCount uint16) (event Event) { + defer func() { + // Respect the repeat count. + if repeatCount > 1 { + var multi MultiEvent + for i := 0; i < int(repeatCount); i++ { + multi = append(multi, event) + } + event = multi + } + }() + if state != nil { + defer func() { + state.lastCks = cks + }() + } + + var utf8Buf [utf8.UTFMax]byte + var key Key + if state != nil && state.utf16Half { + state.utf16Half = false + state.utf16Buf[1] = r + codepoint := utf16.DecodeRune(state.utf16Buf[0], state.utf16Buf[1]) + rw := utf8.EncodeRune(utf8Buf[:], codepoint) + r, _ = utf8.DecodeRune(utf8Buf[:rw]) + key.Code = r + key.Text = string(r) + key.Mod = translateControlKeyState(cks) + key = ensureKeyCase(key, cks) + if keyDown { + return KeyPressEvent(key) + } + return KeyReleaseEvent(key) + } + + var baseCode rune + switch { + case vkc == 0: + // Zero means this event is either an escape code or a unicode + // codepoint. + if state != nil && state.ansiIdx == 0 && r != ansi.ESC { + // This is a unicode codepoint. + baseCode = r + break + } + + if state != nil { + // Collect ANSI escape code. + state.ansiBuf[state.ansiIdx] = byte(r) + state.ansiIdx++ + if state.ansiIdx <= 2 { + // We haven't received enough bytes to determine if this is an + // ANSI escape code. + return nil + } + if r == ansi.ESC { + // We're expecting a closing String Terminator [ansi.ST]. + return nil + } + + n, event := p.parseSequence(state.ansiBuf[:state.ansiIdx]) + if n == 0 { + return nil + } + if _, ok := event.(UnknownEvent); ok { + return nil + } + + state.ansiIdx = 0 + return event + } + case vkc == xwindows.VK_BACK: + baseCode = KeyBackspace + case vkc == xwindows.VK_TAB: + baseCode = KeyTab + case vkc == xwindows.VK_RETURN: + baseCode = KeyEnter + case vkc == xwindows.VK_SHIFT: + //nolint:nestif + if cks&xwindows.SHIFT_PRESSED != 0 { + if cks&xwindows.ENHANCED_KEY != 0 { + baseCode = KeyRightShift + } else { + baseCode = KeyLeftShift + } + } else if state != nil { + if state.lastCks&xwindows.SHIFT_PRESSED != 0 { + if state.lastCks&xwindows.ENHANCED_KEY != 0 { + baseCode = KeyRightShift + } else { + baseCode = KeyLeftShift + } + } + } + case vkc == xwindows.VK_CONTROL: + if cks&xwindows.LEFT_CTRL_PRESSED != 0 { + baseCode = KeyLeftCtrl + } else if cks&xwindows.RIGHT_CTRL_PRESSED != 0 { + baseCode = KeyRightCtrl + } else if state != nil { + if state.lastCks&xwindows.LEFT_CTRL_PRESSED != 0 { + baseCode = KeyLeftCtrl + } else if state.lastCks&xwindows.RIGHT_CTRL_PRESSED != 0 { + baseCode = KeyRightCtrl + } + } + case vkc == xwindows.VK_MENU: + if cks&xwindows.LEFT_ALT_PRESSED != 0 { + baseCode = KeyLeftAlt + } else if cks&xwindows.RIGHT_ALT_PRESSED != 0 { + baseCode = KeyRightAlt + } else if state != nil { + if state.lastCks&xwindows.LEFT_ALT_PRESSED != 0 { + baseCode = KeyLeftAlt + } else if state.lastCks&xwindows.RIGHT_ALT_PRESSED != 0 { + baseCode = KeyRightAlt + } + } + case vkc == xwindows.VK_PAUSE: + baseCode = KeyPause + case vkc == xwindows.VK_CAPITAL: + baseCode = KeyCapsLock + case vkc == xwindows.VK_ESCAPE: + baseCode = KeyEscape + case vkc == xwindows.VK_SPACE: + baseCode = KeySpace + case vkc == xwindows.VK_PRIOR: + baseCode = KeyPgUp + case vkc == xwindows.VK_NEXT: + baseCode = KeyPgDown + case vkc == xwindows.VK_END: + baseCode = KeyEnd + case vkc == xwindows.VK_HOME: + baseCode = KeyHome + case vkc == xwindows.VK_LEFT: + baseCode = KeyLeft + case vkc == xwindows.VK_UP: + baseCode = KeyUp + case vkc == xwindows.VK_RIGHT: + baseCode = KeyRight + case vkc == xwindows.VK_DOWN: + baseCode = KeyDown + case vkc == xwindows.VK_SELECT: + baseCode = KeySelect + case vkc == xwindows.VK_SNAPSHOT: + baseCode = KeyPrintScreen + case vkc == xwindows.VK_INSERT: + baseCode = KeyInsert + case vkc == xwindows.VK_DELETE: + baseCode = KeyDelete + case vkc >= '0' && vkc <= '9': + baseCode = rune(vkc) + case vkc >= 'A' && vkc <= 'Z': + // Convert to lowercase. + baseCode = rune(vkc) + 32 + case vkc == xwindows.VK_LWIN: + baseCode = KeyLeftSuper + case vkc == xwindows.VK_RWIN: + baseCode = KeyRightSuper + case vkc == xwindows.VK_APPS: + baseCode = KeyMenu + case vkc >= xwindows.VK_NUMPAD0 && vkc <= xwindows.VK_NUMPAD9: + baseCode = rune(vkc-xwindows.VK_NUMPAD0) + KeyKp0 + case vkc == xwindows.VK_MULTIPLY: + baseCode = KeyKpMultiply + case vkc == xwindows.VK_ADD: + baseCode = KeyKpPlus + case vkc == xwindows.VK_SEPARATOR: + baseCode = KeyKpComma + case vkc == xwindows.VK_SUBTRACT: + baseCode = KeyKpMinus + case vkc == xwindows.VK_DECIMAL: + baseCode = KeyKpDecimal + case vkc == xwindows.VK_DIVIDE: + baseCode = KeyKpDivide + case vkc >= xwindows.VK_F1 && vkc <= xwindows.VK_F24: + baseCode = rune(vkc-xwindows.VK_F1) + KeyF1 + case vkc == xwindows.VK_NUMLOCK: + baseCode = KeyNumLock + case vkc == xwindows.VK_SCROLL: + baseCode = KeyScrollLock + case vkc == xwindows.VK_LSHIFT: + baseCode = KeyLeftShift + case vkc == xwindows.VK_RSHIFT: + baseCode = KeyRightShift + case vkc == xwindows.VK_LCONTROL: + baseCode = KeyLeftCtrl + case vkc == xwindows.VK_RCONTROL: + baseCode = KeyRightCtrl + case vkc == xwindows.VK_LMENU: + baseCode = KeyLeftAlt + case vkc == xwindows.VK_RMENU: + baseCode = KeyRightAlt + case vkc == xwindows.VK_VOLUME_MUTE: + baseCode = KeyMute + case vkc == xwindows.VK_VOLUME_DOWN: + baseCode = KeyLowerVol + case vkc == xwindows.VK_VOLUME_UP: + baseCode = KeyRaiseVol + case vkc == xwindows.VK_MEDIA_NEXT_TRACK: + baseCode = KeyMediaNext + case vkc == xwindows.VK_MEDIA_PREV_TRACK: + baseCode = KeyMediaPrev + case vkc == xwindows.VK_MEDIA_STOP: + baseCode = KeyMediaStop + case vkc == xwindows.VK_MEDIA_PLAY_PAUSE: + baseCode = KeyMediaPlayPause + case vkc == xwindows.VK_OEM_1: + baseCode = ';' + case vkc == xwindows.VK_OEM_PLUS: + baseCode = '+' + case vkc == xwindows.VK_OEM_COMMA: + baseCode = ',' + case vkc == xwindows.VK_OEM_MINUS: + baseCode = '-' + case vkc == xwindows.VK_OEM_PERIOD: + baseCode = '.' + case vkc == xwindows.VK_OEM_2: + baseCode = '/' + case vkc == xwindows.VK_OEM_3: + baseCode = '`' + case vkc == xwindows.VK_OEM_4: + baseCode = '[' + case vkc == xwindows.VK_OEM_5: + baseCode = '\\' + case vkc == xwindows.VK_OEM_6: + baseCode = ']' + case vkc == xwindows.VK_OEM_7: + baseCode = '\'' + } + + if utf16.IsSurrogate(r) { + if state != nil { + state.utf16Buf[0] = r + state.utf16Half = true + } + return nil + } + + // AltGr is left ctrl + right alt. On non-US keyboards, this is used to type + // special characters and produce printable events. + // XXX: Should this be a KeyMod? + altGr := cks&(xwindows.LEFT_CTRL_PRESSED|xwindows.RIGHT_ALT_PRESSED) == xwindows.LEFT_CTRL_PRESSED|xwindows.RIGHT_ALT_PRESSED + + var text string + keyCode := baseCode + if !unicode.IsControl(r) { + rw := utf8.EncodeRune(utf8Buf[:], r) + keyCode, _ = utf8.DecodeRune(utf8Buf[:rw]) + if unicode.IsPrint(keyCode) && (cks == 0 || + cks == xwindows.SHIFT_PRESSED || + cks == xwindows.CAPSLOCK_ON || + altGr) { + // If the control key state is 0, shift is pressed, or caps lock + // then the key event is a printable event i.e. [text] is not empty. + text = string(keyCode) + } + } + + key.Code = keyCode + key.Text = text + key.Mod = translateControlKeyState(cks) + key.BaseCode = baseCode + key = ensureKeyCase(key, cks) + if keyDown { + return KeyPressEvent(key) + } + + return KeyReleaseEvent(key) +} + +// ensureKeyCase ensures that the key's text is in the correct case based on the +// control key state. +func ensureKeyCase(key Key, cks uint32) Key { + if len(key.Text) == 0 { + return key + } + + hasShift := cks&xwindows.SHIFT_PRESSED != 0 + hasCaps := cks&xwindows.CAPSLOCK_ON != 0 + if hasShift || hasCaps { + if unicode.IsLower(key.Code) { + key.ShiftedCode = unicode.ToUpper(key.Code) + key.Text = string(key.ShiftedCode) + } + } else { + if unicode.IsUpper(key.Code) { + key.ShiftedCode = unicode.ToLower(key.Code) + key.Text = string(key.ShiftedCode) + } + } + + return key +} + +// translateControlKeyState translates the control key state from the Windows +// Console API into a Mod bitmask. +func translateControlKeyState(cks uint32) (m KeyMod) { + if cks&xwindows.LEFT_CTRL_PRESSED != 0 || cks&xwindows.RIGHT_CTRL_PRESSED != 0 { + m |= ModCtrl + } + if cks&xwindows.LEFT_ALT_PRESSED != 0 || cks&xwindows.RIGHT_ALT_PRESSED != 0 { + m |= ModAlt + } + if cks&xwindows.SHIFT_PRESSED != 0 { + m |= ModShift + } + if cks&xwindows.CAPSLOCK_ON != 0 { + m |= ModCapsLock + } + if cks&xwindows.NUMLOCK_ON != 0 { + m |= ModNumLock + } + if cks&xwindows.SCROLLLOCK_ON != 0 { + m |= ModScrollLock + } + return +} + +//nolint:unused +func keyEventString(vkc, sc uint16, r rune, keyDown bool, cks uint32, repeatCount uint16) string { + var s strings.Builder + s.WriteString("vkc: ") + s.WriteString(fmt.Sprintf("%d, 0x%02x", vkc, vkc)) + s.WriteString(", sc: ") + s.WriteString(fmt.Sprintf("%d, 0x%02x", sc, sc)) + s.WriteString(", r: ") + s.WriteString(fmt.Sprintf("%q", r)) + s.WriteString(", down: ") + s.WriteString(fmt.Sprintf("%v", keyDown)) + s.WriteString(", cks: [") + if cks&xwindows.LEFT_ALT_PRESSED != 0 { + s.WriteString("left alt, ") + } + if cks&xwindows.RIGHT_ALT_PRESSED != 0 { + s.WriteString("right alt, ") + } + if cks&xwindows.LEFT_CTRL_PRESSED != 0 { + s.WriteString("left ctrl, ") + } + if cks&xwindows.RIGHT_CTRL_PRESSED != 0 { + s.WriteString("right ctrl, ") + } + if cks&xwindows.SHIFT_PRESSED != 0 { + s.WriteString("shift, ") + } + if cks&xwindows.CAPSLOCK_ON != 0 { + s.WriteString("caps lock, ") + } + if cks&xwindows.NUMLOCK_ON != 0 { + s.WriteString("num lock, ") + } + if cks&xwindows.SCROLLLOCK_ON != 0 { + s.WriteString("scroll lock, ") + } + if cks&xwindows.ENHANCED_KEY != 0 { + s.WriteString("enhanced key, ") + } + s.WriteString("], repeat count: ") + s.WriteString(fmt.Sprintf("%d", repeatCount)) + return s.String() +} diff --git a/packages/tui/input/driver_windows_test.go b/packages/tui/input/driver_windows_test.go new file mode 100644 index 00000000..45371fd1 --- /dev/null +++ b/packages/tui/input/driver_windows_test.go @@ -0,0 +1,271 @@ +package input + +import ( + "encoding/binary" + "image/color" + "reflect" + "testing" + "unicode/utf16" + + "github.com/charmbracelet/x/ansi" + xwindows "github.com/charmbracelet/x/windows" + "golang.org/x/sys/windows" +) + +func TestWindowsInputEvents(t *testing.T) { + cases := []struct { + name string + events []xwindows.InputRecord + expected []Event + sequence bool // indicates that the input events are ANSI sequence or utf16 + }{ + { + name: "single key event", + events: []xwindows.InputRecord{ + encodeKeyEvent(xwindows.KeyEventRecord{ + KeyDown: true, + Char: 'a', + VirtualKeyCode: 'A', + }), + }, + expected: []Event{KeyPressEvent{Code: 'a', BaseCode: 'a', Text: "a"}}, + }, + { + name: "single key event with control key", + events: []xwindows.InputRecord{ + encodeKeyEvent(xwindows.KeyEventRecord{ + KeyDown: true, + Char: 'a', + VirtualKeyCode: 'A', + ControlKeyState: xwindows.LEFT_CTRL_PRESSED, + }), + }, + expected: []Event{KeyPressEvent{Code: 'a', BaseCode: 'a', Mod: ModCtrl}}, + }, + { + name: "escape alt key event", + events: []xwindows.InputRecord{ + encodeKeyEvent(xwindows.KeyEventRecord{ + KeyDown: true, + Char: ansi.ESC, + VirtualKeyCode: ansi.ESC, + ControlKeyState: xwindows.LEFT_ALT_PRESSED, + }), + }, + expected: []Event{KeyPressEvent{Code: ansi.ESC, BaseCode: ansi.ESC, Mod: ModAlt}}, + }, + { + name: "single shifted key event", + events: []xwindows.InputRecord{ + encodeKeyEvent(xwindows.KeyEventRecord{ + KeyDown: true, + Char: 'A', + VirtualKeyCode: 'A', + ControlKeyState: xwindows.SHIFT_PRESSED, + }), + }, + expected: []Event{KeyPressEvent{Code: 'A', BaseCode: 'a', Text: "A", Mod: ModShift}}, + }, + { + name: "utf16 rune", + events: encodeUtf16Rune('😊'), // smiley emoji '😊' + expected: []Event{ + KeyPressEvent{Code: '😊', Text: "😊"}, + }, + sequence: true, + }, + { + name: "background color response", + events: encodeSequence("\x1b]11;rgb:ff/ff/ff\x07"), + expected: []Event{BackgroundColorEvent{Color: color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}}}, + sequence: true, + }, + { + name: "st terminated background color response", + events: encodeSequence("\x1b]11;rgb:ffff/ffff/ffff\x1b\\"), + expected: []Event{BackgroundColorEvent{Color: color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}}}, + sequence: true, + }, + { + name: "simple mouse event", + events: []xwindows.InputRecord{ + encodeMouseEvent(xwindows.MouseEventRecord{ + MousePositon: windows.Coord{X: 10, Y: 20}, + ButtonState: xwindows.FROM_LEFT_1ST_BUTTON_PRESSED, + EventFlags: 0, + }), + encodeMouseEvent(xwindows.MouseEventRecord{ + MousePositon: windows.Coord{X: 10, Y: 20}, + EventFlags: 0, + }), + }, + expected: []Event{ + MouseClickEvent{Button: MouseLeft, X: 10, Y: 20}, + MouseReleaseEvent{Button: MouseLeft, X: 10, Y: 20}, + }, + }, + { + name: "focus event", + events: []xwindows.InputRecord{ + encodeFocusEvent(xwindows.FocusEventRecord{ + SetFocus: true, + }), + encodeFocusEvent(xwindows.FocusEventRecord{ + SetFocus: false, + }), + }, + expected: []Event{ + FocusEvent{}, + BlurEvent{}, + }, + }, + { + name: "window size event", + events: []xwindows.InputRecord{ + encodeWindowBufferSizeEvent(xwindows.WindowBufferSizeRecord{ + Size: windows.Coord{X: 10, Y: 20}, + }), + }, + expected: []Event{ + WindowSizeEvent{Width: 10, Height: 20}, + }, + }, + } + + // p is the parser to parse the input events + var p Parser + + // keep track of the state of the driver to handle ANSI sequences and utf16 + var state win32InputState + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if tc.sequence { + var Event Event + for _, ev := range tc.events { + if ev.EventType != xwindows.KEY_EVENT { + t.Fatalf("expected key event, got %v", ev.EventType) + } + + key := ev.KeyEvent() + Event = p.parseWin32InputKeyEvent(&state, key.VirtualKeyCode, key.VirtualScanCode, key.Char, key.KeyDown, key.ControlKeyState, key.RepeatCount) + } + if len(tc.expected) != 1 { + t.Fatalf("expected 1 event, got %d", len(tc.expected)) + } + if !reflect.DeepEqual(Event, tc.expected[0]) { + t.Errorf("expected %v, got %v", tc.expected[0], Event) + } + } else { + if len(tc.events) != len(tc.expected) { + t.Fatalf("expected %d events, got %d", len(tc.expected), len(tc.events)) + } + for j, ev := range tc.events { + Event := p.parseConInputEvent(ev, &state) + if !reflect.DeepEqual(Event, tc.expected[j]) { + t.Errorf("expected %#v, got %#v", tc.expected[j], Event) + } + } + } + }) + } +} + +func boolToUint32(b bool) uint32 { + if b { + return 1 + } + return 0 +} + +func encodeMenuEvent(menu xwindows.MenuEventRecord) xwindows.InputRecord { + var bts [16]byte + binary.LittleEndian.PutUint32(bts[0:4], menu.CommandID) + return xwindows.InputRecord{ + EventType: xwindows.MENU_EVENT, + Event: bts, + } +} + +func encodeWindowBufferSizeEvent(size xwindows.WindowBufferSizeRecord) xwindows.InputRecord { + var bts [16]byte + binary.LittleEndian.PutUint16(bts[0:2], uint16(size.Size.X)) + binary.LittleEndian.PutUint16(bts[2:4], uint16(size.Size.Y)) + return xwindows.InputRecord{ + EventType: xwindows.WINDOW_BUFFER_SIZE_EVENT, + Event: bts, + } +} + +func encodeFocusEvent(focus xwindows.FocusEventRecord) xwindows.InputRecord { + var bts [16]byte + if focus.SetFocus { + bts[0] = 1 + } + return xwindows.InputRecord{ + EventType: xwindows.FOCUS_EVENT, + Event: bts, + } +} + +func encodeMouseEvent(mouse xwindows.MouseEventRecord) xwindows.InputRecord { + var bts [16]byte + binary.LittleEndian.PutUint16(bts[0:2], uint16(mouse.MousePositon.X)) + binary.LittleEndian.PutUint16(bts[2:4], uint16(mouse.MousePositon.Y)) + binary.LittleEndian.PutUint32(bts[4:8], mouse.ButtonState) + binary.LittleEndian.PutUint32(bts[8:12], mouse.ControlKeyState) + binary.LittleEndian.PutUint32(bts[12:16], mouse.EventFlags) + return xwindows.InputRecord{ + EventType: xwindows.MOUSE_EVENT, + Event: bts, + } +} + +func encodeKeyEvent(key xwindows.KeyEventRecord) xwindows.InputRecord { + var bts [16]byte + binary.LittleEndian.PutUint32(bts[0:4], boolToUint32(key.KeyDown)) + binary.LittleEndian.PutUint16(bts[4:6], key.RepeatCount) + binary.LittleEndian.PutUint16(bts[6:8], key.VirtualKeyCode) + binary.LittleEndian.PutUint16(bts[8:10], key.VirtualScanCode) + binary.LittleEndian.PutUint16(bts[10:12], uint16(key.Char)) + binary.LittleEndian.PutUint32(bts[12:16], key.ControlKeyState) + return xwindows.InputRecord{ + EventType: xwindows.KEY_EVENT, + Event: bts, + } +} + +// encodeSequence encodes a string of ANSI escape sequences into a slice of +// Windows input key records. +func encodeSequence(s string) (evs []xwindows.InputRecord) { + var state byte + for len(s) > 0 { + seq, _, n, newState := ansi.DecodeSequence(s, state, nil) + for i := 0; i < n; i++ { + evs = append(evs, encodeKeyEvent(xwindows.KeyEventRecord{ + KeyDown: true, + Char: rune(seq[i]), + })) + } + state = newState + s = s[n:] + } + return +} + +func encodeUtf16Rune(r rune) []xwindows.InputRecord { + r1, r2 := utf16.EncodeRune(r) + return encodeUtf16Pair(r1, r2) +} + +func encodeUtf16Pair(r1, r2 rune) []xwindows.InputRecord { + return []xwindows.InputRecord{ + encodeKeyEvent(xwindows.KeyEventRecord{ + KeyDown: true, + Char: r1, + }), + encodeKeyEvent(xwindows.KeyEventRecord{ + KeyDown: true, + Char: r2, + }), + } +} diff --git a/packages/tui/input/focus.go b/packages/tui/input/focus.go new file mode 100644 index 00000000..796d95f6 --- /dev/null +++ b/packages/tui/input/focus.go @@ -0,0 +1,9 @@ +package input + +// FocusEvent represents a terminal focus event. +// This occurs when the terminal gains focus. +type FocusEvent struct{} + +// BlurEvent represents a terminal blur event. +// This occurs when the terminal loses focus. +type BlurEvent struct{} diff --git a/packages/tui/input/focus_test.go b/packages/tui/input/focus_test.go new file mode 100644 index 00000000..2d35e476 --- /dev/null +++ b/packages/tui/input/focus_test.go @@ -0,0 +1,27 @@ +package input + +import ( + "testing" +) + +func TestFocus(t *testing.T) { + var p Parser + _, e := p.parseSequence([]byte("\x1b[I")) + switch e.(type) { + case FocusEvent: + // ok + default: + t.Error("invalid sequence") + } +} + +func TestBlur(t *testing.T) { + var p Parser + _, e := p.parseSequence([]byte("\x1b[O")) + switch e.(type) { + case BlurEvent: + // ok + default: + t.Error("invalid sequence") + } +} diff --git a/packages/tui/input/go.mod b/packages/tui/input/go.mod new file mode 100644 index 00000000..36a9a92a --- /dev/null +++ b/packages/tui/input/go.mod @@ -0,0 +1,18 @@ +module github.com/charmbracelet/x/input + +go 1.23.0 + +require ( + github.com/charmbracelet/x/ansi v0.9.3 + github.com/charmbracelet/x/windows v0.2.1 + github.com/muesli/cancelreader v0.2.2 + github.com/rivo/uniseg v0.4.7 + github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e + golang.org/x/sys v0.33.0 +) + +require ( + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect + golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect +) diff --git a/packages/tui/input/go.sum b/packages/tui/input/go.sum new file mode 100644 index 00000000..7bc7a2eb --- /dev/null +++ b/packages/tui/input/go.sum @@ -0,0 +1,19 @@ +github.com/charmbracelet/x/ansi v0.9.3 h1:BXt5DHS/MKF+LjuK4huWrC6NCvHtexww7dMayh6GXd0= +github.com/charmbracelet/x/ansi v0.9.3/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE= +github.com/charmbracelet/x/windows v0.2.1 h1:3x7vnbpQrjpuq/4L+I4gNsG5htYoCiA5oe9hLjAij5I= +github.com/charmbracelet/x/windows v0.2.1/go.mod h1:ptZp16h40gDYqs5TSawSVW+yiLB13j4kSMA0lSCHL0M= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= +golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= diff --git a/packages/tui/input/input.go b/packages/tui/input/input.go new file mode 100644 index 00000000..da5e4f0b --- /dev/null +++ b/packages/tui/input/input.go @@ -0,0 +1,45 @@ +package input + +import ( + "fmt" + "strings" +) + +// Event represents a terminal event. +type Event any + +// UnknownEvent represents an unknown event. +type UnknownEvent string + +// String returns a string representation of the unknown event. +func (e UnknownEvent) String() string { + return fmt.Sprintf("%q", string(e)) +} + +// MultiEvent represents multiple messages event. +type MultiEvent []Event + +// String returns a string representation of the multiple messages event. +func (e MultiEvent) String() string { + var sb strings.Builder + for _, ev := range e { + sb.WriteString(fmt.Sprintf("%v\n", ev)) + } + return sb.String() +} + +// WindowSizeEvent is used to report the terminal size. Note that Windows does +// not have support for reporting resizes via SIGWINCH signals and relies on +// the Windows Console API to report window size changes. +type WindowSizeEvent struct { + Width int + Height int +} + +// WindowOpEvent is a window operation (XTWINOPS) report event. This is used to +// report various window operations such as reporting the window size or cell +// size. +type WindowOpEvent struct { + Op int + Args []int +} diff --git a/packages/tui/input/key.go b/packages/tui/input/key.go new file mode 100644 index 00000000..8d3e3ebe --- /dev/null +++ b/packages/tui/input/key.go @@ -0,0 +1,574 @@ +package input + +import ( + "fmt" + "strings" + "unicode" + + "github.com/charmbracelet/x/ansi" +) + +const ( + // KeyExtended is a special key code used to signify that a key event + // contains multiple runes. + KeyExtended = unicode.MaxRune + 1 +) + +// Special key symbols. +const ( + + // Special keys. + + KeyUp rune = KeyExtended + iota + 1 + KeyDown + KeyRight + KeyLeft + KeyBegin + KeyFind + KeyInsert + KeyDelete + KeySelect + KeyPgUp + KeyPgDown + KeyHome + KeyEnd + + // Keypad keys. + + KeyKpEnter + KeyKpEqual + KeyKpMultiply + KeyKpPlus + KeyKpComma + KeyKpMinus + KeyKpDecimal + KeyKpDivide + KeyKp0 + KeyKp1 + KeyKp2 + KeyKp3 + KeyKp4 + KeyKp5 + KeyKp6 + KeyKp7 + KeyKp8 + KeyKp9 + + //nolint:godox + // The following are keys defined in the Kitty keyboard protocol. + // TODO: Investigate the names of these keys. + + KeyKpSep + KeyKpUp + KeyKpDown + KeyKpLeft + KeyKpRight + KeyKpPgUp + KeyKpPgDown + KeyKpHome + KeyKpEnd + KeyKpInsert + KeyKpDelete + KeyKpBegin + + // Function keys. + + KeyF1 + KeyF2 + KeyF3 + KeyF4 + KeyF5 + KeyF6 + KeyF7 + KeyF8 + KeyF9 + KeyF10 + KeyF11 + KeyF12 + KeyF13 + KeyF14 + KeyF15 + KeyF16 + KeyF17 + KeyF18 + KeyF19 + KeyF20 + KeyF21 + KeyF22 + KeyF23 + KeyF24 + KeyF25 + KeyF26 + KeyF27 + KeyF28 + KeyF29 + KeyF30 + KeyF31 + KeyF32 + KeyF33 + KeyF34 + KeyF35 + KeyF36 + KeyF37 + KeyF38 + KeyF39 + KeyF40 + KeyF41 + KeyF42 + KeyF43 + KeyF44 + KeyF45 + KeyF46 + KeyF47 + KeyF48 + KeyF49 + KeyF50 + KeyF51 + KeyF52 + KeyF53 + KeyF54 + KeyF55 + KeyF56 + KeyF57 + KeyF58 + KeyF59 + KeyF60 + KeyF61 + KeyF62 + KeyF63 + + //nolint:godox + // The following are keys defined in the Kitty keyboard protocol. + // TODO: Investigate the names of these keys. + + KeyCapsLock + KeyScrollLock + KeyNumLock + KeyPrintScreen + KeyPause + KeyMenu + + KeyMediaPlay + KeyMediaPause + KeyMediaPlayPause + KeyMediaReverse + KeyMediaStop + KeyMediaFastForward + KeyMediaRewind + KeyMediaNext + KeyMediaPrev + KeyMediaRecord + + KeyLowerVol + KeyRaiseVol + KeyMute + + KeyLeftShift + KeyLeftAlt + KeyLeftCtrl + KeyLeftSuper + KeyLeftHyper + KeyLeftMeta + KeyRightShift + KeyRightAlt + KeyRightCtrl + KeyRightSuper + KeyRightHyper + KeyRightMeta + KeyIsoLevel3Shift + KeyIsoLevel5Shift + + // Special names in C0. + + KeyBackspace = rune(ansi.DEL) + KeyTab = rune(ansi.HT) + KeyEnter = rune(ansi.CR) + KeyReturn = KeyEnter + KeyEscape = rune(ansi.ESC) + KeyEsc = KeyEscape + + // Special names in G0. + + KeySpace = rune(ansi.SP) +) + +// KeyPressEvent represents a key press event. +type KeyPressEvent Key + +// String implements [fmt.Stringer] and is quite useful for matching key +// events. For details, on what this returns see [Key.String]. +func (k KeyPressEvent) String() string { + return Key(k).String() +} + +// Keystroke returns the keystroke representation of the [Key]. While less type +// safe than looking at the individual fields, it will usually be more +// convenient and readable to use this method when matching against keys. +// +// Note that modifier keys are always printed in the following order: +// - ctrl +// - alt +// - shift +// - meta +// - hyper +// - super +// +// For example, you'll always see "ctrl+shift+alt+a" and never +// "shift+ctrl+alt+a". +func (k KeyPressEvent) Keystroke() string { + return Key(k).Keystroke() +} + +// Key returns the underlying key event. This is a syntactic sugar for casting +// the key event to a [Key]. +func (k KeyPressEvent) Key() Key { + return Key(k) +} + +// KeyReleaseEvent represents a key release event. +type KeyReleaseEvent Key + +// String implements [fmt.Stringer] and is quite useful for matching key +// events. For details, on what this returns see [Key.String]. +func (k KeyReleaseEvent) String() string { + return Key(k).String() +} + +// Keystroke returns the keystroke representation of the [Key]. While less type +// safe than looking at the individual fields, it will usually be more +// convenient and readable to use this method when matching against keys. +// +// Note that modifier keys are always printed in the following order: +// - ctrl +// - alt +// - shift +// - meta +// - hyper +// - super +// +// For example, you'll always see "ctrl+shift+alt+a" and never +// "shift+ctrl+alt+a". +func (k KeyReleaseEvent) Keystroke() string { + return Key(k).Keystroke() +} + +// Key returns the underlying key event. This is a convenience method and +// syntactic sugar to satisfy the [KeyEvent] interface, and cast the key event to +// [Key]. +func (k KeyReleaseEvent) Key() Key { + return Key(k) +} + +// KeyEvent represents a key event. This can be either a key press or a key +// release event. +type KeyEvent interface { + fmt.Stringer + + // Key returns the underlying key event. + Key() Key +} + +// Key represents a Key press or release event. It contains information about +// the Key pressed, like the runes, the type of Key, and the modifiers pressed. +// There are a couple general patterns you could use to check for key presses +// or releases: +// +// // Switch on the string representation of the key (shorter) +// switch ev := ev.(type) { +// case KeyPressEvent: +// switch ev.String() { +// case "enter": +// fmt.Println("you pressed enter!") +// case "a": +// fmt.Println("you pressed a!") +// } +// } +// +// // Switch on the key type (more foolproof) +// switch ev := ev.(type) { +// case KeyEvent: +// // catch both KeyPressEvent and KeyReleaseEvent +// switch key := ev.Key(); key.Code { +// case KeyEnter: +// fmt.Println("you pressed enter!") +// default: +// switch key.Text { +// case "a": +// fmt.Println("you pressed a!") +// } +// } +// } +// +// Note that [Key.Text] will be empty for special keys like [KeyEnter], +// [KeyTab], and for keys that don't represent printable characters like key +// combos with modifier keys. In other words, [Key.Text] is populated only for +// keys that represent printable characters shifted or unshifted (like 'a', +// 'A', '1', '!', etc.). +type Key struct { + // Text contains the actual characters received. This usually the same as + // [Key.Code]. When [Key.Text] is non-empty, it indicates that the key + // pressed represents printable character(s). + Text string + + // Mod represents modifier keys, like [ModCtrl], [ModAlt], and so on. + Mod KeyMod + + // Code represents the key pressed. This is usually a special key like + // [KeyTab], [KeyEnter], [KeyF1], or a printable character like 'a'. + Code rune + + // ShiftedCode is the actual, shifted key pressed by the user. For example, + // if the user presses shift+a, or caps lock is on, [Key.ShiftedCode] will + // be 'A' and [Key.Code] will be 'a'. + // + // In the case of non-latin keyboards, like Arabic, [Key.ShiftedCode] is the + // unshifted key on the keyboard. + // + // This is only available with the Kitty Keyboard Protocol or the Windows + // Console API. + ShiftedCode rune + + // BaseCode is the key pressed according to the standard PC-101 key layout. + // On international keyboards, this is the key that would be pressed if the + // keyboard was set to US PC-101 layout. + // + // For example, if the user presses 'q' on a French AZERTY keyboard, + // [Key.BaseCode] will be 'q'. + // + // This is only available with the Kitty Keyboard Protocol or the Windows + // Console API. + BaseCode rune + + // IsRepeat indicates whether the key is being held down and sending events + // repeatedly. + // + // This is only available with the Kitty Keyboard Protocol or the Windows + // Console API. + IsRepeat bool +} + +// String implements [fmt.Stringer] and is quite useful for matching key +// events. It will return the textual representation of the [Key] if there is +// one, otherwise, it will fallback to [Key.Keystroke]. +// +// For example, you'll always get "?" and instead of "shift+/" on a US ANSI +// keyboard. +func (k Key) String() string { + if len(k.Text) > 0 && k.Text != " " { + return k.Text + } + return k.Keystroke() +} + +// Keystroke returns the keystroke representation of the [Key]. While less type +// safe than looking at the individual fields, it will usually be more +// convenient and readable to use this method when matching against keys. +// +// Note that modifier keys are always printed in the following order: +// - ctrl +// - alt +// - shift +// - meta +// - hyper +// - super +// +// For example, you'll always see "ctrl+shift+alt+a" and never +// "shift+ctrl+alt+a". +func (k Key) Keystroke() string { + var sb strings.Builder + if k.Mod.Contains(ModCtrl) && k.Code != KeyLeftCtrl && k.Code != KeyRightCtrl { + sb.WriteString("ctrl+") + } + if k.Mod.Contains(ModAlt) && k.Code != KeyLeftAlt && k.Code != KeyRightAlt { + sb.WriteString("alt+") + } + if k.Mod.Contains(ModShift) && k.Code != KeyLeftShift && k.Code != KeyRightShift { + sb.WriteString("shift+") + } + if k.Mod.Contains(ModMeta) && k.Code != KeyLeftMeta && k.Code != KeyRightMeta { + sb.WriteString("meta+") + } + if k.Mod.Contains(ModHyper) && k.Code != KeyLeftHyper && k.Code != KeyRightHyper { + sb.WriteString("hyper+") + } + if k.Mod.Contains(ModSuper) && k.Code != KeyLeftSuper && k.Code != KeyRightSuper { + sb.WriteString("super+") + } + + if kt, ok := keyTypeString[k.Code]; ok { + sb.WriteString(kt) + } else { + code := k.Code + if k.BaseCode != 0 { + // If a [Key.BaseCode] is present, use it to represent a key using the standard + // PC-101 key layout. + code = k.BaseCode + } + + switch code { + case KeySpace: + // Space is the only invisible printable character. + sb.WriteString("space") + case KeyExtended: + // Write the actual text of the key when the key contains multiple + // runes. + sb.WriteString(k.Text) + default: + sb.WriteRune(code) + } + } + + return sb.String() +} + +var keyTypeString = map[rune]string{ + KeyEnter: "enter", + KeyTab: "tab", + KeyBackspace: "backspace", + KeyEscape: "esc", + KeySpace: "space", + KeyUp: "up", + KeyDown: "down", + KeyLeft: "left", + KeyRight: "right", + KeyBegin: "begin", + KeyFind: "find", + KeyInsert: "insert", + KeyDelete: "delete", + KeySelect: "select", + KeyPgUp: "pgup", + KeyPgDown: "pgdown", + KeyHome: "home", + KeyEnd: "end", + KeyKpEnter: "kpenter", + KeyKpEqual: "kpequal", + KeyKpMultiply: "kpmul", + KeyKpPlus: "kpplus", + KeyKpComma: "kpcomma", + KeyKpMinus: "kpminus", + KeyKpDecimal: "kpperiod", + KeyKpDivide: "kpdiv", + KeyKp0: "kp0", + KeyKp1: "kp1", + KeyKp2: "kp2", + KeyKp3: "kp3", + KeyKp4: "kp4", + KeyKp5: "kp5", + KeyKp6: "kp6", + KeyKp7: "kp7", + KeyKp8: "kp8", + KeyKp9: "kp9", + + // Kitty keyboard extension + KeyKpSep: "kpsep", + KeyKpUp: "kpup", + KeyKpDown: "kpdown", + KeyKpLeft: "kpleft", + KeyKpRight: "kpright", + KeyKpPgUp: "kppgup", + KeyKpPgDown: "kppgdown", + KeyKpHome: "kphome", + KeyKpEnd: "kpend", + KeyKpInsert: "kpinsert", + KeyKpDelete: "kpdelete", + KeyKpBegin: "kpbegin", + + KeyF1: "f1", + KeyF2: "f2", + KeyF3: "f3", + KeyF4: "f4", + KeyF5: "f5", + KeyF6: "f6", + KeyF7: "f7", + KeyF8: "f8", + KeyF9: "f9", + KeyF10: "f10", + KeyF11: "f11", + KeyF12: "f12", + KeyF13: "f13", + KeyF14: "f14", + KeyF15: "f15", + KeyF16: "f16", + KeyF17: "f17", + KeyF18: "f18", + KeyF19: "f19", + KeyF20: "f20", + KeyF21: "f21", + KeyF22: "f22", + KeyF23: "f23", + KeyF24: "f24", + KeyF25: "f25", + KeyF26: "f26", + KeyF27: "f27", + KeyF28: "f28", + KeyF29: "f29", + KeyF30: "f30", + KeyF31: "f31", + KeyF32: "f32", + KeyF33: "f33", + KeyF34: "f34", + KeyF35: "f35", + KeyF36: "f36", + KeyF37: "f37", + KeyF38: "f38", + KeyF39: "f39", + KeyF40: "f40", + KeyF41: "f41", + KeyF42: "f42", + KeyF43: "f43", + KeyF44: "f44", + KeyF45: "f45", + KeyF46: "f46", + KeyF47: "f47", + KeyF48: "f48", + KeyF49: "f49", + KeyF50: "f50", + KeyF51: "f51", + KeyF52: "f52", + KeyF53: "f53", + KeyF54: "f54", + KeyF55: "f55", + KeyF56: "f56", + KeyF57: "f57", + KeyF58: "f58", + KeyF59: "f59", + KeyF60: "f60", + KeyF61: "f61", + KeyF62: "f62", + KeyF63: "f63", + + // Kitty keyboard extension + KeyCapsLock: "capslock", + KeyScrollLock: "scrolllock", + KeyNumLock: "numlock", + KeyPrintScreen: "printscreen", + KeyPause: "pause", + KeyMenu: "menu", + KeyMediaPlay: "mediaplay", + KeyMediaPause: "mediapause", + KeyMediaPlayPause: "mediaplaypause", + KeyMediaReverse: "mediareverse", + KeyMediaStop: "mediastop", + KeyMediaFastForward: "mediafastforward", + KeyMediaRewind: "mediarewind", + KeyMediaNext: "medianext", + KeyMediaPrev: "mediaprev", + KeyMediaRecord: "mediarecord", + KeyLowerVol: "lowervol", + KeyRaiseVol: "raisevol", + KeyMute: "mute", + KeyLeftShift: "leftshift", + KeyLeftAlt: "leftalt", + KeyLeftCtrl: "leftctrl", + KeyLeftSuper: "leftsuper", + KeyLeftHyper: "lefthyper", + KeyLeftMeta: "leftmeta", + KeyRightShift: "rightshift", + KeyRightAlt: "rightalt", + KeyRightCtrl: "rightctrl", + KeyRightSuper: "rightsuper", + KeyRightHyper: "righthyper", + KeyRightMeta: "rightmeta", + KeyIsoLevel3Shift: "isolevel3shift", + KeyIsoLevel5Shift: "isolevel5shift", +} diff --git a/packages/tui/input/key_test.go b/packages/tui/input/key_test.go new file mode 100644 index 00000000..9bf4d9a5 --- /dev/null +++ b/packages/tui/input/key_test.go @@ -0,0 +1,880 @@ +package input + +import ( + "bytes" + "context" + "errors" + "flag" + "fmt" + "image/color" + "io" + "math/rand" + "reflect" + "regexp" + "runtime" + "sort" + "strings" + "sync" + "testing" + "time" + + "github.com/charmbracelet/x/ansi" + "github.com/charmbracelet/x/ansi/kitty" +) + +var sequences = buildKeysTable(FlagTerminfo, "dumb") + +func TestKeyString(t *testing.T) { + t.Run("alt+space", func(t *testing.T) { + k := KeyPressEvent{Code: KeySpace, Mod: ModAlt} + if got := k.String(); got != "alt+space" { + t.Fatalf(`expected a "alt+space", got %q`, got) + } + }) + + t.Run("runes", func(t *testing.T) { + k := KeyPressEvent{Code: 'a', Text: "a"} + if got := k.String(); got != "a" { + t.Fatalf(`expected an "a", got %q`, got) + } + }) + + t.Run("invalid", func(t *testing.T) { + k := KeyPressEvent{Code: 99999} + if got := k.String(); got != "𘚟" { + t.Fatalf(`expected a "unknown", got %q`, got) + } + }) + + t.Run("space", func(t *testing.T) { + k := KeyPressEvent{Code: KeySpace, Text: " "} + if got := k.String(); got != "space" { + t.Fatalf(`expected a "space", got %q`, got) + } + }) + + t.Run("shift+space", func(t *testing.T) { + k := KeyPressEvent{Code: KeySpace, Mod: ModShift} + if got := k.String(); got != "shift+space" { + t.Fatalf(`expected a "shift+space", got %q`, got) + } + }) + + t.Run("?", func(t *testing.T) { + k := KeyPressEvent{Code: '/', Mod: ModShift, Text: "?"} + if got := k.String(); got != "?" { + t.Fatalf(`expected a "?", got %q`, got) + } + }) +} + +type seqTest struct { + seq []byte + Events []Event +} + +var f3CurPosRegexp = regexp.MustCompile(`\x1b\[1;(\d+)R`) + +// buildBaseSeqTests returns sequence tests that are valid for the +// detectSequence() function. +func buildBaseSeqTests() []seqTest { + td := []seqTest{} + for seq, key := range sequences { + k := KeyPressEvent(key) + st := seqTest{seq: []byte(seq), Events: []Event{k}} + + // XXX: This is a special case to handle F3 key sequence and cursor + // position report having the same sequence. See [parseCsi] for more + // information. + if f3CurPosRegexp.MatchString(seq) { + st.Events = []Event{k, CursorPositionEvent{Y: 0, X: int(key.Mod)}} + } + td = append(td, st) + } + + // Additional special cases. + td = append(td, + // Unrecognized CSI sequence. + seqTest{ + []byte{'\x1b', '[', '-', '-', '-', '-', 'X'}, + []Event{ + UnknownEvent([]byte{'\x1b', '[', '-', '-', '-', '-', 'X'}), + }, + }, + // A lone space character. + seqTest{ + []byte{' '}, + []Event{ + KeyPressEvent{Code: KeySpace, Text: " "}, + }, + }, + // An escape character with the alt modifier. + seqTest{ + []byte{'\x1b', ' '}, + []Event{ + KeyPressEvent{Code: KeySpace, Mod: ModAlt}, + }, + }, + ) + return td +} + +func TestParseSequence(t *testing.T) { + td := buildBaseSeqTests() + td = append(td, + // Background color. + seqTest{ + []byte("\x1b]11;rgb:1234/1234/1234\x07"), + []Event{BackgroundColorEvent{ + Color: color.RGBA{R: 0x12, G: 0x12, B: 0x12, A: 0xff}, + }}, + }, + seqTest{ + []byte("\x1b]11;rgb:1234/1234/1234\x1b\\"), + []Event{BackgroundColorEvent{ + Color: color.RGBA{R: 0x12, G: 0x12, B: 0x12, A: 0xff}, + }}, + }, + seqTest{ + []byte("\x1b]11;rgb:1234/1234/1234\x1b"), // Incomplete sequences are ignored. + []Event{ + UnknownEvent("\x1b]11;rgb:1234/1234/1234\x1b"), + }, + }, + + // Kitty Graphics response. + seqTest{ + []byte("\x1b_Ga=t;OK\x1b\\"), + []Event{KittyGraphicsEvent{ + Options: kitty.Options{Action: kitty.Transmit}, + Payload: []byte("OK"), + }}, + }, + seqTest{ + []byte("\x1b_Gi=99,I=13;OK\x1b\\"), + []Event{KittyGraphicsEvent{ + Options: kitty.Options{ID: 99, Number: 13}, + Payload: []byte("OK"), + }}, + }, + seqTest{ + []byte("\x1b_Gi=1337,q=1;EINVAL:your face\x1b\\"), + []Event{KittyGraphicsEvent{ + Options: kitty.Options{ID: 1337, Quite: 1}, + Payload: []byte("EINVAL:your face"), + }}, + }, + + // Xterm modifyOtherKeys CSI 27 ; ; ~ + seqTest{ + []byte("\x1b[27;3;20320~"), + []Event{KeyPressEvent{Code: '你', Mod: ModAlt}}, + }, + seqTest{ + []byte("\x1b[27;3;65~"), + []Event{KeyPressEvent{Code: 'A', Mod: ModAlt}}, + }, + seqTest{ + []byte("\x1b[27;3;8~"), + []Event{KeyPressEvent{Code: KeyBackspace, Mod: ModAlt}}, + }, + seqTest{ + []byte("\x1b[27;3;27~"), + []Event{KeyPressEvent{Code: KeyEscape, Mod: ModAlt}}, + }, + seqTest{ + []byte("\x1b[27;3;127~"), + []Event{KeyPressEvent{Code: KeyBackspace, Mod: ModAlt}}, + }, + + // Xterm report window text area size. + seqTest{ + []byte("\x1b[4;24;80t"), + []Event{ + WindowOpEvent{Op: 4, Args: []int{24, 80}}, + }, + }, + + // Kitty keyboard / CSI u (fixterms) + seqTest{ + []byte("\x1b[1B"), + []Event{KeyPressEvent{Code: KeyDown}}, + }, + seqTest{ + []byte("\x1b[1;B"), + []Event{KeyPressEvent{Code: KeyDown}}, + }, + seqTest{ + []byte("\x1b[1;4B"), + []Event{KeyPressEvent{Mod: ModShift | ModAlt, Code: KeyDown}}, + }, + seqTest{ + []byte("\x1b[1;4:1B"), + []Event{KeyPressEvent{Mod: ModShift | ModAlt, Code: KeyDown}}, + }, + seqTest{ + []byte("\x1b[1;4:2B"), + []Event{KeyPressEvent{Mod: ModShift | ModAlt, Code: KeyDown, IsRepeat: true}}, + }, + seqTest{ + []byte("\x1b[1;4:3B"), + []Event{KeyReleaseEvent{Mod: ModShift | ModAlt, Code: KeyDown}}, + }, + seqTest{ + []byte("\x1b[8~"), + []Event{KeyPressEvent{Code: KeyEnd}}, + }, + seqTest{ + []byte("\x1b[8;~"), + []Event{KeyPressEvent{Code: KeyEnd}}, + }, + seqTest{ + []byte("\x1b[8;10~"), + []Event{KeyPressEvent{Mod: ModShift | ModMeta, Code: KeyEnd}}, + }, + seqTest{ + []byte("\x1b[27;4u"), + []Event{KeyPressEvent{Mod: ModShift | ModAlt, Code: KeyEscape}}, + }, + seqTest{ + []byte("\x1b[127;4u"), + []Event{KeyPressEvent{Mod: ModShift | ModAlt, Code: KeyBackspace}}, + }, + seqTest{ + []byte("\x1b[57358;4u"), + []Event{KeyPressEvent{Mod: ModShift | ModAlt, Code: KeyCapsLock}}, + }, + seqTest{ + []byte("\x1b[9;2u"), + []Event{KeyPressEvent{Mod: ModShift, Code: KeyTab}}, + }, + seqTest{ + []byte("\x1b[195;u"), + []Event{KeyPressEvent{Text: "Ã", Code: 'Ã'}}, + }, + seqTest{ + []byte("\x1b[20320;2u"), + []Event{KeyPressEvent{Text: "你", Mod: ModShift, Code: '你'}}, + }, + seqTest{ + []byte("\x1b[195;:1u"), + []Event{KeyPressEvent{Text: "Ã", Code: 'Ã'}}, + }, + seqTest{ + []byte("\x1b[195;2:3u"), + []Event{KeyReleaseEvent{Code: 'Ã', Text: "Ã", Mod: ModShift}}, + }, + seqTest{ + []byte("\x1b[195;2:2u"), + []Event{KeyPressEvent{Code: 'Ã', Text: "Ã", IsRepeat: true, Mod: ModShift}}, + }, + seqTest{ + []byte("\x1b[195;2:1u"), + []Event{KeyPressEvent{Code: 'Ã', Text: "Ã", Mod: ModShift}}, + }, + seqTest{ + []byte("\x1b[195;2:3u"), + []Event{KeyReleaseEvent{Code: 'Ã', Text: "Ã", Mod: ModShift}}, + }, + seqTest{ + []byte("\x1b[97;2;65u"), + []Event{KeyPressEvent{Code: 'a', Text: "A", Mod: ModShift}}, + }, + seqTest{ + []byte("\x1b[97;;229u"), + []Event{KeyPressEvent{Code: 'a', Text: "å"}}, + }, + + // focus/blur + seqTest{ + []byte{'\x1b', '[', 'I'}, + []Event{ + FocusEvent{}, + }, + }, + seqTest{ + []byte{'\x1b', '[', 'O'}, + []Event{ + BlurEvent{}, + }, + }, + // Mouse event. + seqTest{ + []byte{'\x1b', '[', 'M', byte(32) + 0b0100_0000, byte(65), byte(49)}, + []Event{ + MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelUp}, + }, + }, + // SGR Mouse event. + seqTest{ + []byte("\x1b[<0;33;17M"), + []Event{ + MouseClickEvent{X: 32, Y: 16, Button: MouseLeft}, + }, + }, + // Runes. + seqTest{ + []byte{'a'}, + []Event{ + KeyPressEvent{Code: 'a', Text: "a"}, + }, + }, + seqTest{ + []byte{'\x1b', 'a'}, + []Event{ + KeyPressEvent{Code: 'a', Mod: ModAlt}, + }, + }, + seqTest{ + []byte{'a', 'a', 'a'}, + []Event{ + KeyPressEvent{Code: 'a', Text: "a"}, + KeyPressEvent{Code: 'a', Text: "a"}, + KeyPressEvent{Code: 'a', Text: "a"}, + }, + }, + // Multi-byte rune. + seqTest{ + []byte("☃"), + []Event{ + KeyPressEvent{Code: '☃', Text: "☃"}, + }, + }, + seqTest{ + []byte("\x1b☃"), + []Event{ + KeyPressEvent{Code: '☃', Mod: ModAlt}, + }, + }, + // Standalone control characters. + seqTest{ + []byte{'\x1b'}, + []Event{ + KeyPressEvent{Code: KeyEscape}, + }, + }, + seqTest{ + []byte{ansi.SOH}, + []Event{ + KeyPressEvent{Code: 'a', Mod: ModCtrl}, + }, + }, + seqTest{ + []byte{'\x1b', ansi.SOH}, + []Event{ + KeyPressEvent{Code: 'a', Mod: ModCtrl | ModAlt}, + }, + }, + seqTest{ + []byte{ansi.NUL}, + []Event{ + KeyPressEvent{Code: KeySpace, Mod: ModCtrl}, + }, + }, + seqTest{ + []byte{'\x1b', ansi.NUL}, + []Event{ + KeyPressEvent{Code: KeySpace, Mod: ModCtrl | ModAlt}, + }, + }, + // C1 control characters. + seqTest{ + []byte{'\x80'}, + []Event{ + KeyPressEvent{Code: rune(0x80 - '@'), Mod: ModCtrl | ModAlt}, + }, + }, + ) + + if runtime.GOOS != "windows" { + // Sadly, utf8.DecodeRune([]byte(0xfe)) returns a valid rune on windows. + // This is incorrect, but it makes our test fail if we try it out. + td = append(td, seqTest{ + []byte{'\xfe'}, + []Event{ + UnknownEvent(rune(0xfe)), + }, + }) + } + + var p Parser + for _, tc := range td { + t.Run(fmt.Sprintf("%q", string(tc.seq)), func(t *testing.T) { + var events []Event + buf := tc.seq + for len(buf) > 0 { + width, Event := p.parseSequence(buf) + switch Event := Event.(type) { + case MultiEvent: + events = append(events, Event...) + default: + events = append(events, Event) + } + buf = buf[width:] + } + if !reflect.DeepEqual(tc.Events, events) { + t.Errorf("\nexpected event for %q:\n %#v\ngot:\n %#v", tc.seq, tc.Events, events) + } + }) + } +} + +func TestReadLongInput(t *testing.T) { + expect := make([]Event, 1000) + for i := range 1000 { + expect[i] = KeyPressEvent{Code: 'a', Text: "a"} + } + input := strings.Repeat("a", 1000) + drv, err := NewReader(strings.NewReader(input), "dumb", 0) + if err != nil { + t.Fatalf("unexpected input driver error: %v", err) + } + + var Events []Event + for { + events, err := drv.ReadEvents() + if err == io.EOF { + break + } + if err != nil { + t.Fatalf("unexpected input error: %v", err) + } + Events = append(Events, events...) + } + + if !reflect.DeepEqual(expect, Events) { + t.Errorf("unexpected messages, expected:\n %+v\ngot:\n %+v", expect, Events) + } +} + +func TestReadInput(t *testing.T) { + type test struct { + keyname string + in []byte + out []Event + } + testData := []test{ + { + "a", + []byte{'a'}, + []Event{ + KeyPressEvent{Code: 'a', Text: "a"}, + }, + }, + { + "space", + []byte{' '}, + []Event{ + KeyPressEvent{Code: KeySpace, Text: " "}, + }, + }, + { + "a alt+a", + []byte{'a', '\x1b', 'a'}, + []Event{ + KeyPressEvent{Code: 'a', Text: "a"}, + KeyPressEvent{Code: 'a', Mod: ModAlt}, + }, + }, + { + "a alt+a a", + []byte{'a', '\x1b', 'a', 'a'}, + []Event{ + KeyPressEvent{Code: 'a', Text: "a"}, + KeyPressEvent{Code: 'a', Mod: ModAlt}, + KeyPressEvent{Code: 'a', Text: "a"}, + }, + }, + { + "ctrl+a", + []byte{byte(ansi.SOH)}, + []Event{ + KeyPressEvent{Code: 'a', Mod: ModCtrl}, + }, + }, + { + "ctrl+a ctrl+b", + []byte{byte(ansi.SOH), byte(ansi.STX)}, + []Event{ + KeyPressEvent{Code: 'a', Mod: ModCtrl}, + KeyPressEvent{Code: 'b', Mod: ModCtrl}, + }, + }, + { + "alt+a", + []byte{byte(0x1b), 'a'}, + []Event{ + KeyPressEvent{Code: 'a', Mod: ModAlt}, + }, + }, + { + "a b c d", + []byte{'a', 'b', 'c', 'd'}, + []Event{ + KeyPressEvent{Code: 'a', Text: "a"}, + KeyPressEvent{Code: 'b', Text: "b"}, + KeyPressEvent{Code: 'c', Text: "c"}, + KeyPressEvent{Code: 'd', Text: "d"}, + }, + }, + { + "up", + []byte("\x1b[A"), + []Event{ + KeyPressEvent{Code: KeyUp}, + }, + }, + { + "wheel up", + []byte{'\x1b', '[', 'M', byte(32) + 0b0100_0000, byte(65), byte(49)}, + []Event{ + MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelUp}, + }, + }, + { + "left motion release", + []byte{ + '\x1b', '[', 'M', byte(32) + 0b0010_0000, byte(32 + 33), byte(16 + 33), + '\x1b', '[', 'M', byte(32) + 0b0000_0011, byte(64 + 33), byte(32 + 33), + }, + []Event{ + MouseMotionEvent{X: 32, Y: 16, Button: MouseLeft}, + MouseReleaseEvent{X: 64, Y: 32, Button: MouseNone}, + }, + }, + { + "shift+tab", + []byte{'\x1b', '[', 'Z'}, + []Event{ + KeyPressEvent{Code: KeyTab, Mod: ModShift}, + }, + }, + { + "enter", + []byte{'\r'}, + []Event{KeyPressEvent{Code: KeyEnter}}, + }, + { + "alt+enter", + []byte{'\x1b', '\r'}, + []Event{ + KeyPressEvent{Code: KeyEnter, Mod: ModAlt}, + }, + }, + { + "insert", + []byte{'\x1b', '[', '2', '~'}, + []Event{ + KeyPressEvent{Code: KeyInsert}, + }, + }, + { + "ctrl+alt+a", + []byte{'\x1b', byte(ansi.SOH)}, + []Event{ + KeyPressEvent{Code: 'a', Mod: ModCtrl | ModAlt}, + }, + }, + { + "CSI?----X?", + []byte{'\x1b', '[', '-', '-', '-', '-', 'X'}, + []Event{UnknownEvent([]byte{'\x1b', '[', '-', '-', '-', '-', 'X'})}, + }, + // Powershell sequences. + { + "up", + []byte{'\x1b', 'O', 'A'}, + []Event{KeyPressEvent{Code: KeyUp}}, + }, + { + "down", + []byte{'\x1b', 'O', 'B'}, + []Event{KeyPressEvent{Code: KeyDown}}, + }, + { + "right", + []byte{'\x1b', 'O', 'C'}, + []Event{KeyPressEvent{Code: KeyRight}}, + }, + { + "left", + []byte{'\x1b', 'O', 'D'}, + []Event{KeyPressEvent{Code: KeyLeft}}, + }, + { + "alt+enter", + []byte{'\x1b', '\x0d'}, + []Event{KeyPressEvent{Code: KeyEnter, Mod: ModAlt}}, + }, + { + "alt+backspace", + []byte{'\x1b', '\x7f'}, + []Event{KeyPressEvent{Code: KeyBackspace, Mod: ModAlt}}, + }, + { + "ctrl+space", + []byte{'\x00'}, + []Event{KeyPressEvent{Code: KeySpace, Mod: ModCtrl}}, + }, + { + "ctrl+alt+space", + []byte{'\x1b', '\x00'}, + []Event{KeyPressEvent{Code: KeySpace, Mod: ModCtrl | ModAlt}}, + }, + { + "esc", + []byte{'\x1b'}, + []Event{KeyPressEvent{Code: KeyEscape}}, + }, + { + "alt+esc", + []byte{'\x1b', '\x1b'}, + []Event{KeyPressEvent{Code: KeyEscape, Mod: ModAlt}}, + }, + { + "a b o", + []byte{ + '\x1b', '[', '2', '0', '0', '~', + 'a', ' ', 'b', + '\x1b', '[', '2', '0', '1', '~', + 'o', + }, + []Event{ + PasteStartEvent{}, + PasteEvent("a b"), + PasteEndEvent{}, + KeyPressEvent{Code: 'o', Text: "o"}, + }, + }, + { + "a\x03\nb", + []byte{ + '\x1b', '[', '2', '0', '0', '~', + 'a', '\x03', '\n', 'b', + '\x1b', '[', '2', '0', '1', '~', + }, + []Event{ + PasteStartEvent{}, + PasteEvent("a\x03\nb"), + PasteEndEvent{}, + }, + }, + { + "?0xfe?", + []byte{'\xfe'}, + []Event{ + UnknownEvent(rune(0xfe)), + }, + }, + { + "a ?0xfe? b", + []byte{'a', '\xfe', ' ', 'b'}, + []Event{ + KeyPressEvent{Code: 'a', Text: "a"}, + UnknownEvent(rune(0xfe)), + KeyPressEvent{Code: KeySpace, Text: " "}, + KeyPressEvent{Code: 'b', Text: "b"}, + }, + }, + } + + for i, td := range testData { + t.Run(fmt.Sprintf("%d: %s", i, td.keyname), func(t *testing.T) { + Events := testReadInputs(t, bytes.NewReader(td.in)) + var buf strings.Builder + for i, Event := range Events { + if i > 0 { + buf.WriteByte(' ') + } + if s, ok := Event.(fmt.Stringer); ok { + buf.WriteString(s.String()) + } else { + fmt.Fprintf(&buf, "%#v:%T", Event, Event) + } + } + + if len(Events) != len(td.out) { + t.Fatalf("unexpected message list length: got %d, expected %d\n got: %#v\n expected: %#v\n", len(Events), len(td.out), Events, td.out) + } + + if !reflect.DeepEqual(td.out, Events) { + t.Fatalf("expected:\n%#v\ngot:\n%#v", td.out, Events) + } + }) + } +} + +func testReadInputs(t *testing.T, input io.Reader) []Event { + // We'll check that the input reader finishes at the end + // without error. + var wg sync.WaitGroup + var inputErr error + ctx, cancel := context.WithCancel(context.Background()) + defer func() { + cancel() + wg.Wait() + if inputErr != nil && !errors.Is(inputErr, io.EOF) { + t.Fatalf("unexpected input error: %v", inputErr) + } + }() + + dr, err := NewReader(input, "dumb", 0) + if err != nil { + t.Fatalf("unexpected input driver error: %v", err) + } + + // The messages we're consuming. + EventsC := make(chan Event) + + // Start the reader in the background. + wg.Add(1) + go func() { + defer wg.Done() + var events []Event + events, inputErr = dr.ReadEvents() + out: + for _, ev := range events { + select { + case EventsC <- ev: + case <-ctx.Done(): + break out + } + } + EventsC <- nil + }() + + var Events []Event +loop: + for { + select { + case Event := <-EventsC: + if Event == nil { + // end of input marker for the test. + break loop + } + Events = append(Events, Event) + case <-time.After(2 * time.Second): + t.Errorf("timeout waiting for input event") + break loop + } + } + return Events +} + +// randTest defines the test input and expected output for a sequence +// of interleaved control sequences and control characters. +type randTest struct { + data []byte + lengths []int + names []string +} + +// seed is the random seed to randomize the input. This helps check +// that all the sequences get ultimately exercised. +var seed = flag.Int64("seed", 0, "random seed (0 to autoselect)") + +// genRandomData generates a randomized test, with a random seed unless +// the seed flag was set. +func genRandomData(logfn func(int64), length int) randTest { + // We'll use a random source. However, we give the user the option + // to override it to a specific value for reproduceability. + s := *seed + if s == 0 { + s = time.Now().UnixNano() + } + // Inform the user so they know what to reuse to get the same data. + logfn(s) + return genRandomDataWithSeed(s, length) +} + +// genRandomDataWithSeed generates a randomized test with a fixed seed. +func genRandomDataWithSeed(s int64, length int) randTest { + src := rand.NewSource(s) + r := rand.New(src) + + // allseqs contains all the sequences, in sorted order. We sort + // to make the test deterministic (when the seed is also fixed). + type seqpair struct { + seq string + name string + } + var allseqs []seqpair + for seq, key := range sequences { + allseqs = append(allseqs, seqpair{seq, key.String()}) + } + sort.Slice(allseqs, func(i, j int) bool { return allseqs[i].seq < allseqs[j].seq }) + + // res contains the computed test. + var res randTest + + for len(res.data) < length { + alt := r.Intn(2) + prefix := "" + esclen := 0 + if alt == 1 { + prefix = "alt+" + esclen = 1 + } + kind := r.Intn(3) + switch kind { + case 0: + // A control character. + if alt == 1 { + res.data = append(res.data, '\x1b') + } + res.data = append(res.data, 1) + res.names = append(res.names, "ctrl+"+prefix+"a") + res.lengths = append(res.lengths, 1+esclen) + + case 1, 2: + // A sequence. + seqi := r.Intn(len(allseqs)) + s := allseqs[seqi] + if strings.Contains(s.name, "alt+") || strings.Contains(s.name, "meta+") { + esclen = 0 + prefix = "" + alt = 0 + } + if alt == 1 { + res.data = append(res.data, '\x1b') + } + res.data = append(res.data, s.seq...) + if strings.HasPrefix(s.name, "ctrl+") { + prefix = "ctrl+" + prefix + } + name := prefix + strings.TrimPrefix(s.name, "ctrl+") + res.names = append(res.names, name) + res.lengths = append(res.lengths, len(s.seq)+esclen) + } + } + return res +} + +func FuzzParseSequence(f *testing.F) { + var p Parser + for seq := range sequences { + f.Add(seq) + } + f.Add("\x1b]52;?\x07") // OSC 52 + f.Add("\x1b]11;rgb:0000/0000/0000\x1b\\") // OSC 11 + f.Add("\x1bP>|charm terminal(0.1.2)\x1b\\") // DCS (XTVERSION) + f.Add("\x1b_Gi=123\x1b\\") // APC + f.Fuzz(func(t *testing.T, seq string) { + n, _ := p.parseSequence([]byte(seq)) + if n == 0 && seq != "" { + t.Errorf("expected a non-zero width for %q", seq) + } + }) +} + +// BenchmarkDetectSequenceMap benchmarks the map-based sequence +// detector. +func BenchmarkDetectSequenceMap(b *testing.B) { + var p Parser + td := genRandomDataWithSeed(123, 10000) + for i := 0; i < b.N; i++ { + for j, w := 0, 0; j < len(td.data); j += w { + w, _ = p.parseSequence(td.data[j:]) + } + } +} diff --git a/packages/tui/input/kitty.go b/packages/tui/input/kitty.go new file mode 100644 index 00000000..4da00b50 --- /dev/null +++ b/packages/tui/input/kitty.go @@ -0,0 +1,353 @@ +package input + +import ( + "unicode" + "unicode/utf8" + + "github.com/charmbracelet/x/ansi" + "github.com/charmbracelet/x/ansi/kitty" +) + +// KittyGraphicsEvent represents a Kitty Graphics response event. +// +// See https://sw.kovidgoyal.net/kitty/graphics-protocol/ +type KittyGraphicsEvent struct { + Options kitty.Options + Payload []byte +} + +// KittyEnhancementsEvent represents a Kitty enhancements event. +type KittyEnhancementsEvent int + +// Kitty keyboard enhancement constants. +// See https://sw.kovidgoyal.net/kitty/keyboard-protocol/#progressive-enhancement +const ( + KittyDisambiguateEscapeCodes KittyEnhancementsEvent = 1 << iota + KittyReportEventTypes + KittyReportAlternateKeys + KittyReportAllKeysAsEscapeCodes + KittyReportAssociatedText +) + +// Contains reports whether m contains the given enhancements. +func (e KittyEnhancementsEvent) Contains(enhancements KittyEnhancementsEvent) bool { + return e&enhancements == enhancements +} + +// Kitty Clipboard Control Sequences. +var kittyKeyMap = map[int]Key{ + ansi.BS: {Code: KeyBackspace}, + ansi.HT: {Code: KeyTab}, + ansi.CR: {Code: KeyEnter}, + ansi.ESC: {Code: KeyEscape}, + ansi.DEL: {Code: KeyBackspace}, + + 57344: {Code: KeyEscape}, + 57345: {Code: KeyEnter}, + 57346: {Code: KeyTab}, + 57347: {Code: KeyBackspace}, + 57348: {Code: KeyInsert}, + 57349: {Code: KeyDelete}, + 57350: {Code: KeyLeft}, + 57351: {Code: KeyRight}, + 57352: {Code: KeyUp}, + 57353: {Code: KeyDown}, + 57354: {Code: KeyPgUp}, + 57355: {Code: KeyPgDown}, + 57356: {Code: KeyHome}, + 57357: {Code: KeyEnd}, + 57358: {Code: KeyCapsLock}, + 57359: {Code: KeyScrollLock}, + 57360: {Code: KeyNumLock}, + 57361: {Code: KeyPrintScreen}, + 57362: {Code: KeyPause}, + 57363: {Code: KeyMenu}, + 57364: {Code: KeyF1}, + 57365: {Code: KeyF2}, + 57366: {Code: KeyF3}, + 57367: {Code: KeyF4}, + 57368: {Code: KeyF5}, + 57369: {Code: KeyF6}, + 57370: {Code: KeyF7}, + 57371: {Code: KeyF8}, + 57372: {Code: KeyF9}, + 57373: {Code: KeyF10}, + 57374: {Code: KeyF11}, + 57375: {Code: KeyF12}, + 57376: {Code: KeyF13}, + 57377: {Code: KeyF14}, + 57378: {Code: KeyF15}, + 57379: {Code: KeyF16}, + 57380: {Code: KeyF17}, + 57381: {Code: KeyF18}, + 57382: {Code: KeyF19}, + 57383: {Code: KeyF20}, + 57384: {Code: KeyF21}, + 57385: {Code: KeyF22}, + 57386: {Code: KeyF23}, + 57387: {Code: KeyF24}, + 57388: {Code: KeyF25}, + 57389: {Code: KeyF26}, + 57390: {Code: KeyF27}, + 57391: {Code: KeyF28}, + 57392: {Code: KeyF29}, + 57393: {Code: KeyF30}, + 57394: {Code: KeyF31}, + 57395: {Code: KeyF32}, + 57396: {Code: KeyF33}, + 57397: {Code: KeyF34}, + 57398: {Code: KeyF35}, + 57399: {Code: KeyKp0}, + 57400: {Code: KeyKp1}, + 57401: {Code: KeyKp2}, + 57402: {Code: KeyKp3}, + 57403: {Code: KeyKp4}, + 57404: {Code: KeyKp5}, + 57405: {Code: KeyKp6}, + 57406: {Code: KeyKp7}, + 57407: {Code: KeyKp8}, + 57408: {Code: KeyKp9}, + 57409: {Code: KeyKpDecimal}, + 57410: {Code: KeyKpDivide}, + 57411: {Code: KeyKpMultiply}, + 57412: {Code: KeyKpMinus}, + 57413: {Code: KeyKpPlus}, + 57414: {Code: KeyKpEnter}, + 57415: {Code: KeyKpEqual}, + 57416: {Code: KeyKpSep}, + 57417: {Code: KeyKpLeft}, + 57418: {Code: KeyKpRight}, + 57419: {Code: KeyKpUp}, + 57420: {Code: KeyKpDown}, + 57421: {Code: KeyKpPgUp}, + 57422: {Code: KeyKpPgDown}, + 57423: {Code: KeyKpHome}, + 57424: {Code: KeyKpEnd}, + 57425: {Code: KeyKpInsert}, + 57426: {Code: KeyKpDelete}, + 57427: {Code: KeyKpBegin}, + 57428: {Code: KeyMediaPlay}, + 57429: {Code: KeyMediaPause}, + 57430: {Code: KeyMediaPlayPause}, + 57431: {Code: KeyMediaReverse}, + 57432: {Code: KeyMediaStop}, + 57433: {Code: KeyMediaFastForward}, + 57434: {Code: KeyMediaRewind}, + 57435: {Code: KeyMediaNext}, + 57436: {Code: KeyMediaPrev}, + 57437: {Code: KeyMediaRecord}, + 57438: {Code: KeyLowerVol}, + 57439: {Code: KeyRaiseVol}, + 57440: {Code: KeyMute}, + 57441: {Code: KeyLeftShift}, + 57442: {Code: KeyLeftCtrl}, + 57443: {Code: KeyLeftAlt}, + 57444: {Code: KeyLeftSuper}, + 57445: {Code: KeyLeftHyper}, + 57446: {Code: KeyLeftMeta}, + 57447: {Code: KeyRightShift}, + 57448: {Code: KeyRightCtrl}, + 57449: {Code: KeyRightAlt}, + 57450: {Code: KeyRightSuper}, + 57451: {Code: KeyRightHyper}, + 57452: {Code: KeyRightMeta}, + 57453: {Code: KeyIsoLevel3Shift}, + 57454: {Code: KeyIsoLevel5Shift}, +} + +func init() { + // These are some faulty C0 mappings some terminals such as WezTerm have + // and doesn't follow the specs. + kittyKeyMap[ansi.NUL] = Key{Code: KeySpace, Mod: ModCtrl} + for i := ansi.SOH; i <= ansi.SUB; i++ { + if _, ok := kittyKeyMap[i]; !ok { + kittyKeyMap[i] = Key{Code: rune(i + 0x60), Mod: ModCtrl} + } + } + for i := ansi.FS; i <= ansi.US; i++ { + if _, ok := kittyKeyMap[i]; !ok { + kittyKeyMap[i] = Key{Code: rune(i + 0x40), Mod: ModCtrl} + } + } +} + +const ( + kittyShift = 1 << iota + kittyAlt + kittyCtrl + kittySuper + kittyHyper + kittyMeta + kittyCapsLock + kittyNumLock +) + +func fromKittyMod(mod int) KeyMod { + var m KeyMod + if mod&kittyShift != 0 { + m |= ModShift + } + if mod&kittyAlt != 0 { + m |= ModAlt + } + if mod&kittyCtrl != 0 { + m |= ModCtrl + } + if mod&kittySuper != 0 { + m |= ModSuper + } + if mod&kittyHyper != 0 { + m |= ModHyper + } + if mod&kittyMeta != 0 { + m |= ModMeta + } + if mod&kittyCapsLock != 0 { + m |= ModCapsLock + } + if mod&kittyNumLock != 0 { + m |= ModNumLock + } + return m +} + +// parseKittyKeyboard parses a Kitty Keyboard Protocol sequence. +// +// In `CSI u`, this is parsed as: +// +// CSI codepoint ; modifiers u +// codepoint: ASCII Dec value +// +// The Kitty Keyboard Protocol extends this with optional components that can be +// enabled progressively. The full sequence is parsed as: +// +// CSI unicode-key-code:alternate-key-codes ; modifiers:event-type ; text-as-codepoints u +// +// See https://sw.kovidgoyal.net/kitty/keyboard-protocol/ +func parseKittyKeyboard(params ansi.Params) (Event Event) { + var isRelease bool + var key Key + + // The index of parameters separated by semicolons ';'. Sub parameters are + // separated by colons ':'. + var paramIdx int + var sudIdx int // The sub parameter index + for _, p := range params { + // Kitty Keyboard Protocol has 3 optional components. + switch paramIdx { + case 0: + switch sudIdx { + case 0: + var foundKey bool + code := p.Param(1) // CSI u has a default value of 1 + key, foundKey = kittyKeyMap[code] + if !foundKey { + r := rune(code) + if !utf8.ValidRune(r) { + r = utf8.RuneError + } + + key.Code = r + } + + case 2: + // shifted key + base key + if b := rune(p.Param(1)); unicode.IsPrint(b) { + // XXX: When alternate key reporting is enabled, the protocol + // can return 3 things, the unicode codepoint of the key, + // the shifted codepoint of the key, and the standard + // PC-101 key layout codepoint. + // This is useful to create an unambiguous mapping of keys + // when using a different language layout. + key.BaseCode = b + } + fallthrough + + case 1: + // shifted key + if s := rune(p.Param(1)); unicode.IsPrint(s) { + // XXX: We swap keys here because we want the shifted key + // to be the Rune that is returned by the event. + // For example, shift+a should produce "A" not "a". + // In such a case, we set AltRune to the original key "a" + // and Rune to "A". + key.ShiftedCode = s + } + } + case 1: + switch sudIdx { + case 0: + mod := p.Param(1) + if mod > 1 { + key.Mod = fromKittyMod(mod - 1) + if key.Mod > ModShift { + // XXX: We need to clear the text if we have a modifier key + // other than a [ModShift] key. + key.Text = "" + } + } + + case 1: + switch p.Param(1) { + case 2: + key.IsRepeat = true + case 3: + isRelease = true + } + case 2: + } + case 2: + if code := p.Param(0); code != 0 { + key.Text += string(rune(code)) + } + } + + sudIdx++ + if !p.HasMore() { + paramIdx++ + sudIdx = 0 + } + } + + //nolint:nestif + if len(key.Text) == 0 && unicode.IsPrint(key.Code) && + (key.Mod <= ModShift || key.Mod == ModCapsLock || key.Mod == ModShift|ModCapsLock) { + if key.Mod == 0 { + key.Text = string(key.Code) + } else { + desiredCase := unicode.ToLower + if key.Mod.Contains(ModShift) || key.Mod.Contains(ModCapsLock) { + desiredCase = unicode.ToUpper + } + if key.ShiftedCode != 0 { + key.Text = string(key.ShiftedCode) + } else { + key.Text = string(desiredCase(key.Code)) + } + } + } + + if isRelease { + return KeyReleaseEvent(key) + } + + return KeyPressEvent(key) +} + +// parseKittyKeyboardExt parses a Kitty Keyboard Protocol sequence extensions +// for non CSI u sequences. This includes things like CSI A, SS3 A and others, +// and CSI ~. +func parseKittyKeyboardExt(params ansi.Params, k KeyPressEvent) Event { + // Handle Kitty keyboard protocol + if len(params) > 2 && // We have at least 3 parameters + params[0].Param(1) == 1 && // The first parameter is 1 (defaults to 1) + params[1].HasMore() { // The second parameter is a subparameter (separated by a ":") + switch params[2].Param(1) { // The third parameter is the event type (defaults to 1) + case 2: + k.IsRepeat = true + case 3: + return KeyReleaseEvent(k) + } + } + return k +} diff --git a/packages/tui/input/mod.go b/packages/tui/input/mod.go new file mode 100644 index 00000000..c0076276 --- /dev/null +++ b/packages/tui/input/mod.go @@ -0,0 +1,37 @@ +package input + +// KeyMod represents modifier keys. +type KeyMod int + +// Modifier keys. +const ( + ModShift KeyMod = 1 << iota + ModAlt + ModCtrl + ModMeta + + // These modifiers are used with the Kitty protocol. + // XXX: Meta and Super are swapped in the Kitty protocol, + // this is to preserve compatibility with XTerm modifiers. + + ModHyper + ModSuper // Windows/Command keys + + // These are key lock states. + + ModCapsLock + ModNumLock + ModScrollLock // Defined in Windows API only +) + +// Contains reports whether m contains the given modifiers. +// +// Example: +// +// m := ModAlt | ModCtrl +// m.Contains(ModCtrl) // true +// m.Contains(ModAlt | ModCtrl) // true +// m.Contains(ModAlt | ModCtrl | ModShift) // false +func (m KeyMod) Contains(mods KeyMod) bool { + return m&mods == mods +} diff --git a/packages/tui/input/mode.go b/packages/tui/input/mode.go new file mode 100644 index 00000000..ea1ba571 --- /dev/null +++ b/packages/tui/input/mode.go @@ -0,0 +1,14 @@ +package input + +import "github.com/charmbracelet/x/ansi" + +// ModeReportEvent is a message that represents a mode report event (DECRPM). +// +// See: https://vt100.net/docs/vt510-rm/DECRPM.html +type ModeReportEvent struct { + // Mode is the mode number. + Mode ansi.Mode + + // Value is the mode value. + Value ansi.ModeSetting +} diff --git a/packages/tui/input/mouse.go b/packages/tui/input/mouse.go new file mode 100644 index 00000000..d97eb72e --- /dev/null +++ b/packages/tui/input/mouse.go @@ -0,0 +1,292 @@ +package input + +import ( + "fmt" + + "github.com/charmbracelet/x/ansi" +) + +// MouseButton represents the button that was pressed during a mouse message. +type MouseButton = ansi.MouseButton + +// Mouse event buttons +// +// This is based on X11 mouse button codes. +// +// 1 = left button +// 2 = middle button (pressing the scroll wheel) +// 3 = right button +// 4 = turn scroll wheel up +// 5 = turn scroll wheel down +// 6 = push scroll wheel left +// 7 = push scroll wheel right +// 8 = 4th button (aka browser backward button) +// 9 = 5th button (aka browser forward button) +// 10 +// 11 +// +// Other buttons are not supported. +const ( + MouseNone = ansi.MouseNone + MouseLeft = ansi.MouseLeft + MouseMiddle = ansi.MouseMiddle + MouseRight = ansi.MouseRight + MouseWheelUp = ansi.MouseWheelUp + MouseWheelDown = ansi.MouseWheelDown + MouseWheelLeft = ansi.MouseWheelLeft + MouseWheelRight = ansi.MouseWheelRight + MouseBackward = ansi.MouseBackward + MouseForward = ansi.MouseForward + MouseButton10 = ansi.MouseButton10 + MouseButton11 = ansi.MouseButton11 +) + +// MouseEvent represents a mouse message. This is a generic mouse message that +// can represent any kind of mouse event. +type MouseEvent interface { + fmt.Stringer + + // Mouse returns the underlying mouse event. + Mouse() Mouse +} + +// Mouse represents a Mouse message. Use [MouseEvent] to represent all mouse +// messages. +// +// The X and Y coordinates are zero-based, with (0,0) being the upper left +// corner of the terminal. +// +// // Catch all mouse events +// switch Event := Event.(type) { +// case MouseEvent: +// m := Event.Mouse() +// fmt.Println("Mouse event:", m.X, m.Y, m) +// } +// +// // Only catch mouse click events +// switch Event := Event.(type) { +// case MouseClickEvent: +// fmt.Println("Mouse click event:", Event.X, Event.Y, Event) +// } +type Mouse struct { + X, Y int + Button MouseButton + Mod KeyMod +} + +// String returns a string representation of the mouse message. +func (m Mouse) String() (s string) { + if m.Mod.Contains(ModCtrl) { + s += "ctrl+" + } + if m.Mod.Contains(ModAlt) { + s += "alt+" + } + if m.Mod.Contains(ModShift) { + s += "shift+" + } + + str := m.Button.String() + if str == "" { + s += "unknown" + } else if str != "none" { // motion events don't have a button + s += str + } + + return s +} + +// MouseClickEvent represents a mouse button click event. +type MouseClickEvent Mouse + +// String returns a string representation of the mouse click event. +func (e MouseClickEvent) String() string { + return Mouse(e).String() +} + +// Mouse returns the underlying mouse event. This is a convenience method and +// syntactic sugar to satisfy the [MouseEvent] interface, and cast the mouse +// event to [Mouse]. +func (e MouseClickEvent) Mouse() Mouse { + return Mouse(e) +} + +// MouseReleaseEvent represents a mouse button release event. +type MouseReleaseEvent Mouse + +// String returns a string representation of the mouse release event. +func (e MouseReleaseEvent) String() string { + return Mouse(e).String() +} + +// Mouse returns the underlying mouse event. This is a convenience method and +// syntactic sugar to satisfy the [MouseEvent] interface, and cast the mouse +// event to [Mouse]. +func (e MouseReleaseEvent) Mouse() Mouse { + return Mouse(e) +} + +// MouseWheelEvent represents a mouse wheel message event. +type MouseWheelEvent Mouse + +// String returns a string representation of the mouse wheel event. +func (e MouseWheelEvent) String() string { + return Mouse(e).String() +} + +// Mouse returns the underlying mouse event. This is a convenience method and +// syntactic sugar to satisfy the [MouseEvent] interface, and cast the mouse +// event to [Mouse]. +func (e MouseWheelEvent) Mouse() Mouse { + return Mouse(e) +} + +// MouseMotionEvent represents a mouse motion event. +type MouseMotionEvent Mouse + +// String returns a string representation of the mouse motion event. +func (e MouseMotionEvent) String() string { + m := Mouse(e) + if m.Button != 0 { + return m.String() + "+motion" + } + return m.String() + "motion" +} + +// Mouse returns the underlying mouse event. This is a convenience method and +// syntactic sugar to satisfy the [MouseEvent] interface, and cast the mouse +// event to [Mouse]. +func (e MouseMotionEvent) Mouse() Mouse { + return Mouse(e) +} + +// Parse SGR-encoded mouse events; SGR extended mouse events. SGR mouse events +// look like: +// +// ESC [ < Cb ; Cx ; Cy (M or m) +// +// where: +// +// Cb is the encoded button code +// Cx is the x-coordinate of the mouse +// Cy is the y-coordinate of the mouse +// M is for button press, m is for button release +// +// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Extended-coordinates +func parseSGRMouseEvent(cmd ansi.Cmd, params ansi.Params) Event { + x, _, ok := params.Param(1, 1) + if !ok { + x = 1 + } + y, _, ok := params.Param(2, 1) + if !ok { + y = 1 + } + release := cmd.Final() == 'm' + b, _, _ := params.Param(0, 0) + mod, btn, _, isMotion := parseMouseButton(b) + + // (1,1) is the upper left. We subtract 1 to normalize it to (0,0). + x-- + y-- + + m := Mouse{X: x, Y: y, Button: btn, Mod: mod} + + // Wheel buttons don't have release events + // Motion can be reported as a release event in some terminals (Windows Terminal) + if isWheel(m.Button) { + return MouseWheelEvent(m) + } else if !isMotion && release { + return MouseReleaseEvent(m) + } else if isMotion { + return MouseMotionEvent(m) + } + return MouseClickEvent(m) +} + +const x10MouseByteOffset = 32 + +// Parse X10-encoded mouse events; the simplest kind. The last release of X10 +// was December 1986, by the way. The original X10 mouse protocol limits the Cx +// and Cy coordinates to 223 (=255-032). +// +// X10 mouse events look like: +// +// ESC [M Cb Cx Cy +// +// See: http://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking +func parseX10MouseEvent(buf []byte) Event { + v := buf[3:6] + b := int(v[0]) + if b >= x10MouseByteOffset { + // XXX: b < 32 should be impossible, but we're being defensive. + b -= x10MouseByteOffset + } + + mod, btn, isRelease, isMotion := parseMouseButton(b) + + // (1,1) is the upper left. We subtract 1 to normalize it to (0,0). + x := int(v[1]) - x10MouseByteOffset - 1 + y := int(v[2]) - x10MouseByteOffset - 1 + + m := Mouse{X: x, Y: y, Button: btn, Mod: mod} + if isWheel(m.Button) { + return MouseWheelEvent(m) + } else if isMotion { + return MouseMotionEvent(m) + } else if isRelease { + return MouseReleaseEvent(m) + } + return MouseClickEvent(m) +} + +// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Extended-coordinates +func parseMouseButton(b int) (mod KeyMod, btn MouseButton, isRelease bool, isMotion bool) { + // mouse bit shifts + const ( + bitShift = 0b0000_0100 + bitAlt = 0b0000_1000 + bitCtrl = 0b0001_0000 + bitMotion = 0b0010_0000 + bitWheel = 0b0100_0000 + bitAdd = 0b1000_0000 // additional buttons 8-11 + + bitsMask = 0b0000_0011 + ) + + // Modifiers + if b&bitAlt != 0 { + mod |= ModAlt + } + if b&bitCtrl != 0 { + mod |= ModCtrl + } + if b&bitShift != 0 { + mod |= ModShift + } + + if b&bitAdd != 0 { + btn = MouseBackward + MouseButton(b&bitsMask) + } else if b&bitWheel != 0 { + btn = MouseWheelUp + MouseButton(b&bitsMask) + } else { + btn = MouseLeft + MouseButton(b&bitsMask) + // X10 reports a button release as 0b0000_0011 (3) + if b&bitsMask == bitsMask { + btn = MouseNone + isRelease = true + } + } + + // Motion bit doesn't get reported for wheel events. + if b&bitMotion != 0 && !isWheel(btn) { + isMotion = true + } + + return //nolint:nakedret +} + +// isWheel returns true if the mouse event is a wheel event. +func isWheel(btn MouseButton) bool { + return btn >= MouseWheelUp && btn <= MouseWheelRight +} diff --git a/packages/tui/input/mouse_test.go b/packages/tui/input/mouse_test.go new file mode 100644 index 00000000..d55e4148 --- /dev/null +++ b/packages/tui/input/mouse_test.go @@ -0,0 +1,481 @@ +package input + +import ( + "fmt" + "testing" + + "github.com/charmbracelet/x/ansi" + "github.com/charmbracelet/x/ansi/parser" +) + +func TestMouseEvent_String(t *testing.T) { + tt := []struct { + name string + event Event + expected string + }{ + { + name: "unknown", + event: MouseClickEvent{Button: MouseButton(0xff)}, + expected: "unknown", + }, + { + name: "left", + event: MouseClickEvent{Button: MouseLeft}, + expected: "left", + }, + { + name: "right", + event: MouseClickEvent{Button: MouseRight}, + expected: "right", + }, + { + name: "middle", + event: MouseClickEvent{Button: MouseMiddle}, + expected: "middle", + }, + { + name: "release", + event: MouseReleaseEvent{Button: MouseNone}, + expected: "", + }, + { + name: "wheelup", + event: MouseWheelEvent{Button: MouseWheelUp}, + expected: "wheelup", + }, + { + name: "wheeldown", + event: MouseWheelEvent{Button: MouseWheelDown}, + expected: "wheeldown", + }, + { + name: "wheelleft", + event: MouseWheelEvent{Button: MouseWheelLeft}, + expected: "wheelleft", + }, + { + name: "wheelright", + event: MouseWheelEvent{Button: MouseWheelRight}, + expected: "wheelright", + }, + { + name: "motion", + event: MouseMotionEvent{Button: MouseNone}, + expected: "motion", + }, + { + name: "shift+left", + event: MouseReleaseEvent{Button: MouseLeft, Mod: ModShift}, + expected: "shift+left", + }, + { + name: "shift+left", event: MouseClickEvent{Button: MouseLeft, Mod: ModShift}, + expected: "shift+left", + }, + { + name: "ctrl+shift+left", + event: MouseClickEvent{Button: MouseLeft, Mod: ModCtrl | ModShift}, + expected: "ctrl+shift+left", + }, + { + name: "alt+left", + event: MouseClickEvent{Button: MouseLeft, Mod: ModAlt}, + expected: "alt+left", + }, + { + name: "ctrl+left", + event: MouseClickEvent{Button: MouseLeft, Mod: ModCtrl}, + expected: "ctrl+left", + }, + { + name: "ctrl+alt+left", + event: MouseClickEvent{Button: MouseLeft, Mod: ModAlt | ModCtrl}, + expected: "ctrl+alt+left", + }, + { + name: "ctrl+alt+shift+left", + event: MouseClickEvent{Button: MouseLeft, Mod: ModAlt | ModCtrl | ModShift}, + expected: "ctrl+alt+shift+left", + }, + { + name: "ignore coordinates", + event: MouseClickEvent{X: 100, Y: 200, Button: MouseLeft}, + expected: "left", + }, + { + name: "broken type", + event: MouseClickEvent{Button: MouseButton(120)}, + expected: "unknown", + }, + } + + for i := range tt { + tc := tt[i] + + t.Run(tc.name, func(t *testing.T) { + actual := fmt.Sprint(tc.event) + + if tc.expected != actual { + t.Fatalf("expected %q but got %q", + tc.expected, + actual, + ) + } + }) + } +} + +func TestParseX10MouseDownEvent(t *testing.T) { + encode := func(b byte, x, y int) []byte { + return []byte{ + '\x1b', + '[', + 'M', + byte(32) + b, + byte(x + 32 + 1), + byte(y + 32 + 1), + } + } + + tt := []struct { + name string + buf []byte + expected Event + }{ + // Position. + { + name: "zero position", + buf: encode(0b0000_0000, 0, 0), + expected: MouseClickEvent{X: 0, Y: 0, Button: MouseLeft}, + }, + { + name: "max position", + buf: encode(0b0000_0000, 222, 222), // Because 255 (max int8) - 32 - 1. + expected: MouseClickEvent{X: 222, Y: 222, Button: MouseLeft}, + }, + // Simple. + { + name: "left", + buf: encode(0b0000_0000, 32, 16), + expected: MouseClickEvent{X: 32, Y: 16, Button: MouseLeft}, + }, + { + name: "left in motion", + buf: encode(0b0010_0000, 32, 16), + expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseLeft}, + }, + { + name: "middle", + buf: encode(0b0000_0001, 32, 16), + expected: MouseClickEvent{X: 32, Y: 16, Button: MouseMiddle}, + }, + { + name: "middle in motion", + buf: encode(0b0010_0001, 32, 16), + expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseMiddle}, + }, + { + name: "right", + buf: encode(0b0000_0010, 32, 16), + expected: MouseClickEvent{X: 32, Y: 16, Button: MouseRight}, + }, + { + name: "right in motion", + buf: encode(0b0010_0010, 32, 16), + expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseRight}, + }, + { + name: "motion", + buf: encode(0b0010_0011, 32, 16), + expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseNone}, + }, + { + name: "wheel up", + buf: encode(0b0100_0000, 32, 16), + expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelUp}, + }, + { + name: "wheel down", + buf: encode(0b0100_0001, 32, 16), + expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelDown}, + }, + { + name: "wheel left", + buf: encode(0b0100_0010, 32, 16), + expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelLeft}, + }, + { + name: "wheel right", + buf: encode(0b0100_0011, 32, 16), + expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelRight}, + }, + { + name: "release", + buf: encode(0b0000_0011, 32, 16), + expected: MouseReleaseEvent{X: 32, Y: 16, Button: MouseNone}, + }, + { + name: "backward", + buf: encode(0b1000_0000, 32, 16), + expected: MouseClickEvent{X: 32, Y: 16, Button: MouseBackward}, + }, + { + name: "forward", + buf: encode(0b1000_0001, 32, 16), + expected: MouseClickEvent{X: 32, Y: 16, Button: MouseForward}, + }, + { + name: "button 10", + buf: encode(0b1000_0010, 32, 16), + expected: MouseClickEvent{X: 32, Y: 16, Button: MouseButton10}, + }, + { + name: "button 11", + buf: encode(0b1000_0011, 32, 16), + expected: MouseClickEvent{X: 32, Y: 16, Button: MouseButton11}, + }, + // Combinations. + { + name: "alt+right", + buf: encode(0b0000_1010, 32, 16), + expected: MouseClickEvent{X: 32, Y: 16, Mod: ModAlt, Button: MouseRight}, + }, + { + name: "ctrl+right", + buf: encode(0b0001_0010, 32, 16), + expected: MouseClickEvent{X: 32, Y: 16, Mod: ModCtrl, Button: MouseRight}, + }, + { + name: "left in motion", + buf: encode(0b0010_0000, 32, 16), + expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseLeft}, + }, + { + name: "alt+right in motion", + buf: encode(0b0010_1010, 32, 16), + expected: MouseMotionEvent{X: 32, Y: 16, Mod: ModAlt, Button: MouseRight}, + }, + { + name: "ctrl+right in motion", + buf: encode(0b0011_0010, 32, 16), + expected: MouseMotionEvent{X: 32, Y: 16, Mod: ModCtrl, Button: MouseRight}, + }, + { + name: "ctrl+alt+right", + buf: encode(0b0001_1010, 32, 16), + expected: MouseClickEvent{X: 32, Y: 16, Mod: ModAlt | ModCtrl, Button: MouseRight}, + }, + { + name: "ctrl+wheel up", + buf: encode(0b0101_0000, 32, 16), + expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModCtrl, Button: MouseWheelUp}, + }, + { + name: "alt+wheel down", + buf: encode(0b0100_1001, 32, 16), + expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModAlt, Button: MouseWheelDown}, + }, + { + name: "ctrl+alt+wheel down", + buf: encode(0b0101_1001, 32, 16), + expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModAlt | ModCtrl, Button: MouseWheelDown}, + }, + // Overflow position. + { + name: "overflow position", + buf: encode(0b0010_0000, 250, 223), // Because 255 (max int8) - 32 - 1. + expected: MouseMotionEvent{X: -6, Y: -33, Button: MouseLeft}, + }, + } + + for i := range tt { + tc := tt[i] + + t.Run(tc.name, func(t *testing.T) { + actual := parseX10MouseEvent(tc.buf) + + if tc.expected != actual { + t.Fatalf("expected %#v but got %#v", + tc.expected, + actual, + ) + } + }) + } +} + +func TestParseSGRMouseEvent(t *testing.T) { + type csiSequence struct { + params []ansi.Param + cmd ansi.Cmd + } + encode := func(b, x, y int, r bool) *csiSequence { + re := 'M' + if r { + re = 'm' + } + return &csiSequence{ + params: []ansi.Param{ + ansi.Param(b), + ansi.Param(x + 1), + ansi.Param(y + 1), + }, + cmd: ansi.Cmd(re) | ('<' << parser.PrefixShift), + } + } + + tt := []struct { + name string + buf *csiSequence + expected Event + }{ + // Position. + { + name: "zero position", + buf: encode(0, 0, 0, false), + expected: MouseClickEvent{X: 0, Y: 0, Button: MouseLeft}, + }, + { + name: "225 position", + buf: encode(0, 225, 225, false), + expected: MouseClickEvent{X: 225, Y: 225, Button: MouseLeft}, + }, + // Simple. + { + name: "left", + buf: encode(0, 32, 16, false), + expected: MouseClickEvent{X: 32, Y: 16, Button: MouseLeft}, + }, + { + name: "left in motion", + buf: encode(32, 32, 16, false), + expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseLeft}, + }, + { + name: "left", + buf: encode(0, 32, 16, true), + expected: MouseReleaseEvent{X: 32, Y: 16, Button: MouseLeft}, + }, + { + name: "middle", + buf: encode(1, 32, 16, false), + expected: MouseClickEvent{X: 32, Y: 16, Button: MouseMiddle}, + }, + { + name: "middle in motion", + buf: encode(33, 32, 16, false), + expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseMiddle}, + }, + { + name: "middle", + buf: encode(1, 32, 16, true), + expected: MouseReleaseEvent{X: 32, Y: 16, Button: MouseMiddle}, + }, + { + name: "right", + buf: encode(2, 32, 16, false), + expected: MouseClickEvent{X: 32, Y: 16, Button: MouseRight}, + }, + { + name: "right", + buf: encode(2, 32, 16, true), + expected: MouseReleaseEvent{X: 32, Y: 16, Button: MouseRight}, + }, + { + name: "motion", + buf: encode(35, 32, 16, false), + expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseNone}, + }, + { + name: "wheel up", + buf: encode(64, 32, 16, false), + expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelUp}, + }, + { + name: "wheel down", + buf: encode(65, 32, 16, false), + expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelDown}, + }, + { + name: "wheel left", + buf: encode(66, 32, 16, false), + expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelLeft}, + }, + { + name: "wheel right", + buf: encode(67, 32, 16, false), + expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelRight}, + }, + { + name: "backward", + buf: encode(128, 32, 16, false), + expected: MouseClickEvent{X: 32, Y: 16, Button: MouseBackward}, + }, + { + name: "backward in motion", + buf: encode(160, 32, 16, false), + expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseBackward}, + }, + { + name: "forward", + buf: encode(129, 32, 16, false), + expected: MouseClickEvent{X: 32, Y: 16, Button: MouseForward}, + }, + { + name: "forward in motion", + buf: encode(161, 32, 16, false), + expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseForward}, + }, + // Combinations. + { + name: "alt+right", + buf: encode(10, 32, 16, false), + expected: MouseClickEvent{X: 32, Y: 16, Mod: ModAlt, Button: MouseRight}, + }, + { + name: "ctrl+right", + buf: encode(18, 32, 16, false), + expected: MouseClickEvent{X: 32, Y: 16, Mod: ModCtrl, Button: MouseRight}, + }, + { + name: "ctrl+alt+right", + buf: encode(26, 32, 16, false), + expected: MouseClickEvent{X: 32, Y: 16, Mod: ModAlt | ModCtrl, Button: MouseRight}, + }, + { + name: "alt+wheel", + buf: encode(73, 32, 16, false), + expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModAlt, Button: MouseWheelDown}, + }, + { + name: "ctrl+wheel", + buf: encode(81, 32, 16, false), + expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModCtrl, Button: MouseWheelDown}, + }, + { + name: "ctrl+alt+wheel", + buf: encode(89, 32, 16, false), + expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModAlt | ModCtrl, Button: MouseWheelDown}, + }, + { + name: "ctrl+alt+shift+wheel", + buf: encode(93, 32, 16, false), + expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModAlt | ModShift | ModCtrl, Button: MouseWheelDown}, + }, + } + + for i := range tt { + tc := tt[i] + + t.Run(tc.name, func(t *testing.T) { + actual := parseSGRMouseEvent(tc.buf.cmd, tc.buf.params) + if tc.expected != actual { + t.Fatalf("expected %#v but got %#v", + tc.expected, + actual, + ) + } + }) + } +} diff --git a/packages/tui/input/parse.go b/packages/tui/input/parse.go new file mode 100644 index 00000000..ad8e2184 --- /dev/null +++ b/packages/tui/input/parse.go @@ -0,0 +1,1030 @@ +package input + +import ( + "bytes" + "encoding/base64" + "slices" + "strings" + "unicode" + "unicode/utf8" + + "github.com/charmbracelet/x/ansi" + "github.com/charmbracelet/x/ansi/parser" + "github.com/rivo/uniseg" +) + +// Flags to control the behavior of the parser. +const ( + // When this flag is set, the driver will treat both Ctrl+Space and Ctrl+@ + // as the same key sequence. + // + // Historically, the ANSI specs generate NUL (0x00) on both the Ctrl+Space + // and Ctrl+@ key sequences. This flag allows the driver to treat both as + // the same key sequence. + FlagCtrlAt = 1 << iota + + // When this flag is set, the driver will treat the Tab key and Ctrl+I as + // the same key sequence. + // + // Historically, the ANSI specs generate HT (0x09) on both the Tab key and + // Ctrl+I. This flag allows the driver to treat both as the same key + // sequence. + FlagCtrlI + + // When this flag is set, the driver will treat the Enter key and Ctrl+M as + // the same key sequence. + // + // Historically, the ANSI specs generate CR (0x0D) on both the Enter key + // and Ctrl+M. This flag allows the driver to treat both as the same key. + FlagCtrlM + + // When this flag is set, the driver will treat Escape and Ctrl+[ as + // the same key sequence. + // + // Historically, the ANSI specs generate ESC (0x1B) on both the Escape key + // and Ctrl+[. This flag allows the driver to treat both as the same key + // sequence. + FlagCtrlOpenBracket + + // When this flag is set, the driver will send a BS (0x08 byte) character + // instead of a DEL (0x7F byte) character when the Backspace key is + // pressed. + // + // The VT100 terminal has both a Backspace and a Delete key. The VT220 + // terminal dropped the Backspace key and replaced it with the Delete key. + // Both terminals send a DEL character when the Delete key is pressed. + // Modern terminals and PCs later readded the Delete key but used a + // different key sequence, and the Backspace key was standardized to send a + // DEL character. + FlagBackspace + + // When this flag is set, the driver will recognize the Find key instead of + // treating it as a Home key. + // + // The Find key was part of the VT220 keyboard, and is no longer used in + // modern day PCs. + FlagFind + + // When this flag is set, the driver will recognize the Select key instead + // of treating it as a End key. + // + // The Symbol key was part of the VT220 keyboard, and is no longer used in + // modern day PCs. + FlagSelect + + // When this flag is set, the driver will use Terminfo databases to + // overwrite the default key sequences. + FlagTerminfo + + // When this flag is set, the driver will preserve function keys (F13-F63) + // as symbols. + // + // Since these keys are not part of today's standard 20th century keyboard, + // we treat them as F1-F12 modifier keys i.e. ctrl/shift/alt + Fn combos. + // Key definitions come from Terminfo, this flag is only useful when + // FlagTerminfo is not set. + FlagFKeys + + // When this flag is set, the driver will enable mouse mode on Windows. + // This is only useful on Windows and has no effect on other platforms. + FlagMouseMode +) + +// Parser is a parser for input escape sequences. +type Parser struct { + flags int +} + +// NewParser returns a new input parser. This is a low-level parser that parses +// escape sequences into human-readable events. +// This differs from [ansi.Parser] and [ansi.DecodeSequence] in which it +// recognizes incorrect sequences that some terminals may send. +// +// For instance, the X10 mouse protocol sends a `CSI M` sequence followed by 3 +// bytes. If the parser doesn't recognize the 3 bytes, they might be echoed to +// the terminal output causing a mess. +// +// Another example is how URxvt sends invalid sequences for modified keys using +// invalid CSI final characters like '$'. +// +// Use flags to control the behavior of ambiguous key sequences. +func NewParser(flags int) *Parser { + return &Parser{flags: flags} +} + +// parseSequence finds the first recognized event sequence and returns it along +// with its length. +// +// It will return zero and nil no sequence is recognized or when the buffer is +// empty. If a sequence is not supported, an UnknownEvent is returned. +func (p *Parser) parseSequence(buf []byte) (n int, Event Event) { + if len(buf) == 0 { + return 0, nil + } + + switch b := buf[0]; b { + case ansi.ESC: + if len(buf) == 1 { + // Escape key + return 1, KeyPressEvent{Code: KeyEscape} + } + + switch bPrime := buf[1]; bPrime { + case 'O': // Esc-prefixed SS3 + return p.parseSs3(buf) + case 'P': // Esc-prefixed DCS + return p.parseDcs(buf) + case '[': // Esc-prefixed CSI + return p.parseCsi(buf) + case ']': // Esc-prefixed OSC + return p.parseOsc(buf) + case '_': // Esc-prefixed APC + return p.parseApc(buf) + case '^': // Esc-prefixed PM + return p.parseStTerminated(ansi.PM, '^', nil)(buf) + case 'X': // Esc-prefixed SOS + return p.parseStTerminated(ansi.SOS, 'X', nil)(buf) + default: + n, e := p.parseSequence(buf[1:]) + if k, ok := e.(KeyPressEvent); ok { + k.Text = "" + k.Mod |= ModAlt + return n + 1, k + } + + // Not a key sequence, nor an alt modified key sequence. In that + // case, just report a single escape key. + return 1, KeyPressEvent{Code: KeyEscape} + } + case ansi.SS3: + return p.parseSs3(buf) + case ansi.DCS: + return p.parseDcs(buf) + case ansi.CSI: + return p.parseCsi(buf) + case ansi.OSC: + return p.parseOsc(buf) + case ansi.APC: + return p.parseApc(buf) + case ansi.PM: + return p.parseStTerminated(ansi.PM, '^', nil)(buf) + case ansi.SOS: + return p.parseStTerminated(ansi.SOS, 'X', nil)(buf) + default: + if b <= ansi.US || b == ansi.DEL || b == ansi.SP { + return 1, p.parseControl(b) + } else if b >= ansi.PAD && b <= ansi.APC { + // C1 control code + // UTF-8 never starts with a C1 control code + // Encode these as Ctrl+Alt+ + code := rune(b) - 0x40 + return 1, KeyPressEvent{Code: code, Mod: ModCtrl | ModAlt} + } + return p.parseUtf8(buf) + } +} + +func (p *Parser) parseCsi(b []byte) (int, Event) { + if len(b) == 2 && b[0] == ansi.ESC { + // short cut if this is an alt+[ key + return 2, KeyPressEvent{Text: string(rune(b[1])), Mod: ModAlt} + } + + var cmd ansi.Cmd + var params [parser.MaxParamsSize]ansi.Param + var paramsLen int + + var i int + if b[i] == ansi.CSI || b[i] == ansi.ESC { + i++ + } + if i < len(b) && b[i-1] == ansi.ESC && b[i] == '[' { + i++ + } + + // Initial CSI byte + if i < len(b) && b[i] >= '<' && b[i] <= '?' { + cmd |= ansi.Cmd(b[i]) << parser.PrefixShift + } + + // Scan parameter bytes in the range 0x30-0x3F + var j int + for j = 0; i < len(b) && paramsLen < len(params) && b[i] >= 0x30 && b[i] <= 0x3F; i, j = i+1, j+1 { + if b[i] >= '0' && b[i] <= '9' { + if params[paramsLen] == parser.MissingParam { + params[paramsLen] = 0 + } + params[paramsLen] *= 10 + params[paramsLen] += ansi.Param(b[i]) - '0' + } + if b[i] == ':' { + params[paramsLen] |= parser.HasMoreFlag + } + if b[i] == ';' || b[i] == ':' { + paramsLen++ + if paramsLen < len(params) { + // Don't overflow the params slice + params[paramsLen] = parser.MissingParam + } + } + } + + if j > 0 && paramsLen < len(params) { + // has parameters + paramsLen++ + } + + // Scan intermediate bytes in the range 0x20-0x2F + var intermed byte + for ; i < len(b) && b[i] >= 0x20 && b[i] <= 0x2F; i++ { + intermed = b[i] + } + + // Set the intermediate byte + cmd |= ansi.Cmd(intermed) << parser.IntermedShift + + // Scan final byte in the range 0x40-0x7E + if i >= len(b) { + // Incomplete sequence + return 0, nil + } + if b[i] < 0x40 || b[i] > 0x7E { + // Special case for URxvt keys + // CSI $ is an invalid sequence, but URxvt uses it for + // shift modified keys. + if b[i-1] == '$' { + n, ev := p.parseCsi(append(b[:i-1], '~')) + if k, ok := ev.(KeyPressEvent); ok { + k.Mod |= ModShift + return n, k + } + } + return i, UnknownEvent(b[:i-1]) + } + + // Add the final byte + cmd |= ansi.Cmd(b[i]) + i++ + + pa := ansi.Params(params[:paramsLen]) + switch cmd { + case 'y' | '?'<= 3 { + pa = pa[:3] + return i, parseSGRMouseEvent(cmd, pa) + } + case 'm' | '>'< R (which is modified F3) when the cursor is at the + // row 1. In this case, we report both messages. + // + // For a non ambiguous cursor position report, use + // [ansi.RequestExtendedCursorPosition] (DECXCPR) instead. + return i, MultiEvent{KeyPressEvent{Code: KeyF3, Mod: KeyMod(col - 1)}, m} + } + + return i, m + } + + if paramsLen != 0 { + break + } + + // Unmodified key F3 (CSI R) + fallthrough + case 'a', 'b', 'c', 'd', 'A', 'B', 'C', 'D', 'E', 'F', 'H', 'P', 'Q', 'S', 'Z': + var k KeyPressEvent + switch cmd { + case 'a', 'b', 'c', 'd': + k = KeyPressEvent{Code: KeyUp + rune(cmd-'a'), Mod: ModShift} + case 'A', 'B', 'C', 'D': + k = KeyPressEvent{Code: KeyUp + rune(cmd-'A')} + case 'E': + k = KeyPressEvent{Code: KeyBegin} + case 'F': + k = KeyPressEvent{Code: KeyEnd} + case 'H': + k = KeyPressEvent{Code: KeyHome} + case 'P', 'Q', 'R', 'S': + k = KeyPressEvent{Code: KeyF1 + rune(cmd-'P')} + case 'Z': + k = KeyPressEvent{Code: KeyTab, Mod: ModShift} + } + id, _, _ := pa.Param(0, 1) + if id == 0 { + id = 1 + } + mod, _, _ := pa.Param(1, 1) + if mod == 0 { + mod = 1 + } + if paramsLen > 1 && id == 1 && mod != -1 { + // CSI 1 ; A + k.Mod |= KeyMod(mod - 1) + } + // Don't forget to handle Kitty keyboard protocol + return i, parseKittyKeyboardExt(pa, k) + case 'M': + // Handle X10 mouse + if i+2 >= len(b) { + // Incomplete sequence + return 0, nil + } + // PERFORMANCE: Do not use append here, as it will allocate a new slice + // for every mouse event. Instead, pass a sub-slice of the original + // buffer. + return i + 3, parseX10MouseEvent(b[i-1 : i+3]) + case 'y' | '$'< 1 && mod != -1 { + k.Mod |= KeyMod(mod - 1) + } + + // Handle URxvt weird keys + switch cmd { + case '~': + // Don't forget to handle Kitty keyboard protocol + return i, parseKittyKeyboardExt(pa, k) + case '^': + k.Mod |= ModCtrl + case '@': + k.Mod |= ModCtrl | ModShift + } + + return i, k + } + + case 't': + param, _, ok := pa.Param(0, 0) + if !ok { + break + } + + var winop WindowOpEvent + winop.Op = param + for j := 1; j < paramsLen; j++ { + val, _, ok := pa.Param(j, 0) + if ok { + winop.Args = append(winop.Args, val) + } + } + + return i, winop + } + return i, UnknownEvent(b[:i]) +} + +// parseSs3 parses a SS3 sequence. +// See https://vt100.net/docs/vt220-rm/chapter4.html#S4.4.4.2 +func (p *Parser) parseSs3(b []byte) (int, Event) { + if len(b) == 2 && b[0] == ansi.ESC { + // short cut if this is an alt+O key + return 2, KeyPressEvent{Code: rune(b[1]), Mod: ModAlt} + } + + var i int + if b[i] == ansi.SS3 || b[i] == ansi.ESC { + i++ + } + if i < len(b) && b[i-1] == ansi.ESC && b[i] == 'O' { + i++ + } + + // Scan numbers from 0-9 + var mod int + for ; i < len(b) && b[i] >= '0' && b[i] <= '9'; i++ { + mod *= 10 + mod += int(b[i]) - '0' + } + + // Scan a GL character + // A GL character is a single byte in the range 0x21-0x7E + // See https://vt100.net/docs/vt220-rm/chapter2.html#S2.3.2 + if i >= len(b) { + // Incomplete sequence + return 0, nil + } + if b[i] < 0x21 || b[i] > 0x7E { + return i, UnknownEvent(b[:i]) + } + + // GL character(s) + gl := b[i] + i++ + + var k KeyPressEvent + switch gl { + case 'a', 'b', 'c', 'd': + k = KeyPressEvent{Code: KeyUp + rune(gl-'a'), Mod: ModCtrl} + case 'A', 'B', 'C', 'D': + k = KeyPressEvent{Code: KeyUp + rune(gl-'A')} + case 'E': + k = KeyPressEvent{Code: KeyBegin} + case 'F': + k = KeyPressEvent{Code: KeyEnd} + case 'H': + k = KeyPressEvent{Code: KeyHome} + case 'P', 'Q', 'R', 'S': + k = KeyPressEvent{Code: KeyF1 + rune(gl-'P')} + case 'M': + k = KeyPressEvent{Code: KeyKpEnter} + case 'X': + k = KeyPressEvent{Code: KeyKpEqual} + case 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y': + k = KeyPressEvent{Code: KeyKpMultiply + rune(gl-'j')} + default: + return i, UnknownEvent(b[:i]) + } + + // Handle weird SS3 Func + if mod > 0 { + k.Mod |= KeyMod(mod - 1) + } + + return i, k +} + +func (p *Parser) parseOsc(b []byte) (int, Event) { + defaultKey := func() KeyPressEvent { + return KeyPressEvent{Code: rune(b[1]), Mod: ModAlt} + } + if len(b) == 2 && b[0] == ansi.ESC { + // short cut if this is an alt+] key + return 2, defaultKey() + } + + var i int + if b[i] == ansi.OSC || b[i] == ansi.ESC { + i++ + } + if i < len(b) && b[i-1] == ansi.ESC && b[i] == ']' { + i++ + } + + // Parse OSC command + // An OSC sequence is terminated by a BEL, ESC, or ST character + var start, end int + cmd := -1 + for ; i < len(b) && b[i] >= '0' && b[i] <= '9'; i++ { + if cmd == -1 { + cmd = 0 + } else { + cmd *= 10 + } + cmd += int(b[i]) - '0' + } + + if i < len(b) && b[i] == ';' { + // mark the start of the sequence data + i++ + start = i + } + + for ; i < len(b); i++ { + // advance to the end of the sequence + if slices.Contains([]byte{ansi.BEL, ansi.ESC, ansi.ST, ansi.CAN, ansi.SUB}, b[i]) { + break + } + } + + if i >= len(b) { + // Incomplete sequence + return 0, nil + } + + end = i // end of the sequence data + i++ + + // Check 7-bit ST (string terminator) character + switch b[i-1] { + case ansi.CAN, ansi.SUB: + return i, UnknownEvent(b[:i]) + case ansi.ESC: + if i >= len(b) || b[i] != '\\' { + if cmd == -1 || (start == 0 && end == 2) { + return 2, defaultKey() + } + + // If we don't have a valid ST terminator, then this is a + // cancelled sequence and should be ignored. + return i, UnknownEvent(b[:i]) + } + + i++ + } + + if end <= start { + return i, UnknownEvent(b[:i]) + } + + // PERFORMANCE: Only allocate the data string if we know we have a handler + // for the command. This avoids allocations for unknown OSC sequences that + // can be sent in high frequency by trackpads. + switch cmd { + case 10, 11, 12: + data := string(b[start:end]) + color := ansi.XParseColor(data) + switch cmd { + case 10: + return i, ForegroundColorEvent{color} + case 11: + return i, BackgroundColorEvent{color} + case 12: + return i, CursorColorEvent{color} + } + case 52: + data := string(b[start:end]) + parts := strings.Split(data, ";") + if len(parts) == 0 { + return i, ClipboardEvent{} + } + if len(parts) != 2 || len(parts[0]) < 1 { + break + } + + b64 := parts[1] + bts, err := base64.StdEncoding.DecodeString(b64) + if err != nil { + break + } + + sel := ClipboardSelection(parts[0][0]) //nolint:unconvert + return i, ClipboardEvent{Selection: sel, Content: string(bts)} + } + + return i, UnknownEvent(b[:i]) +} + +// parseStTerminated parses a control sequence that gets terminated by a ST character. +func (p *Parser) parseStTerminated( + intro8, intro7 byte, + fn func([]byte) Event, +) func([]byte) (int, Event) { + defaultKey := func(b []byte) (int, Event) { + switch intro8 { + case ansi.SOS: + return 2, KeyPressEvent{Code: 'x', Mod: ModShift | ModAlt} + case ansi.PM, ansi.APC: + return 2, KeyPressEvent{Code: rune(b[1]), Mod: ModAlt} + } + return 0, nil + } + return func(b []byte) (int, Event) { + if len(b) == 2 && b[0] == ansi.ESC { + return defaultKey(b) + } + + var i int + if b[i] == intro8 || b[i] == ansi.ESC { + i++ + } + if i < len(b) && b[i-1] == ansi.ESC && b[i] == intro7 { + i++ + } + + // Scan control sequence + // Most common control sequence is terminated by a ST character + // ST is a 7-bit string terminator character is (ESC \) + start := i + for ; i < len(b); i++ { + if slices.Contains([]byte{ansi.ESC, ansi.ST, ansi.CAN, ansi.SUB}, b[i]) { + break + } + } + + if i >= len(b) { + // Incomplete sequence + return 0, nil + } + + end := i // end of the sequence data + i++ + + // Check 7-bit ST (string terminator) character + switch b[i-1] { + case ansi.CAN, ansi.SUB: + return i, UnknownEvent(b[:i]) + case ansi.ESC: + if i >= len(b) || b[i] != '\\' { + if start == end { + return defaultKey(b) + } + + // If we don't have a valid ST terminator, then this is a + // cancelled sequence and should be ignored. + return i, UnknownEvent(b[:i]) + } + + i++ + } + + // Call the function to parse the sequence and return the result + if fn != nil { + if e := fn(b[start:end]); e != nil { + return i, e + } + } + + return i, UnknownEvent(b[:i]) + } +} + +func (p *Parser) parseDcs(b []byte) (int, Event) { + if len(b) == 2 && b[0] == ansi.ESC { + // short cut if this is an alt+P key + return 2, KeyPressEvent{Code: 'p', Mod: ModShift | ModAlt} + } + + var params [16]ansi.Param + var paramsLen int + var cmd ansi.Cmd + + // DCS sequences are introduced by DCS (0x90) or ESC P (0x1b 0x50) + var i int + if b[i] == ansi.DCS || b[i] == ansi.ESC { + i++ + } + if i < len(b) && b[i-1] == ansi.ESC && b[i] == 'P' { + i++ + } + + // initial DCS byte + if i < len(b) && b[i] >= '<' && b[i] <= '?' { + cmd |= ansi.Cmd(b[i]) << parser.PrefixShift + } + + // Scan parameter bytes in the range 0x30-0x3F + var j int + for j = 0; i < len(b) && paramsLen < len(params) && b[i] >= 0x30 && b[i] <= 0x3F; i, j = i+1, j+1 { + if b[i] >= '0' && b[i] <= '9' { + if params[paramsLen] == parser.MissingParam { + params[paramsLen] = 0 + } + params[paramsLen] *= 10 + params[paramsLen] += ansi.Param(b[i]) - '0' + } + if b[i] == ':' { + params[paramsLen] |= parser.HasMoreFlag + } + if b[i] == ';' || b[i] == ':' { + paramsLen++ + if paramsLen < len(params) { + // Don't overflow the params slice + params[paramsLen] = parser.MissingParam + } + } + } + + if j > 0 && paramsLen < len(params) { + // has parameters + paramsLen++ + } + + // Scan intermediate bytes in the range 0x20-0x2F + var intermed byte + for j := 0; i < len(b) && b[i] >= 0x20 && b[i] <= 0x2F; i, j = i+1, j+1 { + intermed = b[i] + } + + // set intermediate byte + cmd |= ansi.Cmd(intermed) << parser.IntermedShift + + // Scan final byte in the range 0x40-0x7E + if i >= len(b) { + // Incomplete sequence + return 0, nil + } + if b[i] < 0x40 || b[i] > 0x7E { + return i, UnknownEvent(b[:i]) + } + + // Add the final byte + cmd |= ansi.Cmd(b[i]) + i++ + + start := i // start of the sequence data + for ; i < len(b); i++ { + if b[i] == ansi.ST || b[i] == ansi.ESC { + break + } + } + + if i >= len(b) { + // Incomplete sequence + return 0, nil + } + + end := i // end of the sequence data + i++ + + // Check 7-bit ST (string terminator) character + if i < len(b) && b[i-1] == ansi.ESC && b[i] == '\\' { + i++ + } + + pa := ansi.Params(params[:paramsLen]) + switch cmd { + case 'r' | '+'<'< 1 { + g.Payload = parts[1] + } + return g + } + + return nil + })(b) +} + +func (p *Parser) parseUtf8(b []byte) (int, Event) { + if len(b) == 0 { + return 0, nil + } + + c := b[0] + if c <= ansi.US || c == ansi.DEL || c == ansi.SP { + // Control codes get handled by parseControl + return 1, p.parseControl(c) + } else if c > ansi.US && c < ansi.DEL { + // ASCII printable characters + code := rune(c) + k := KeyPressEvent{Code: code, Text: string(code)} + if unicode.IsUpper(code) { + // Convert upper case letters to lower case + shift modifier + k.Code = unicode.ToLower(code) + k.ShiftedCode = code + k.Mod |= ModShift + } + + return 1, k + } + + code, _ := utf8.DecodeRune(b) + if code == utf8.RuneError { + return 1, UnknownEvent(b[0]) + } + + cluster, _, _, _ := uniseg.FirstGraphemeCluster(b, -1) + // PERFORMANCE: Use RuneCount to check for multi-rune graphemes instead of + // looping over the string representation. + if utf8.RuneCount(cluster) > 1 { + code = KeyExtended + } + + return len(cluster), KeyPressEvent{Code: code, Text: string(cluster)} +} + +func (p *Parser) parseControl(b byte) Event { + switch b { + case ansi.NUL: + if p.flags&FlagCtrlAt != 0 { + return KeyPressEvent{Code: '@', Mod: ModCtrl} + } + return KeyPressEvent{Code: KeySpace, Mod: ModCtrl} + case ansi.BS: + return KeyPressEvent{Code: 'h', Mod: ModCtrl} + case ansi.HT: + if p.flags&FlagCtrlI != 0 { + return KeyPressEvent{Code: 'i', Mod: ModCtrl} + } + return KeyPressEvent{Code: KeyTab} + case ansi.CR: + if p.flags&FlagCtrlM != 0 { + return KeyPressEvent{Code: 'm', Mod: ModCtrl} + } + return KeyPressEvent{Code: KeyEnter} + case ansi.ESC: + if p.flags&FlagCtrlOpenBracket != 0 { + return KeyPressEvent{Code: '[', Mod: ModCtrl} + } + return KeyPressEvent{Code: KeyEscape} + case ansi.DEL: + if p.flags&FlagBackspace != 0 { + return KeyPressEvent{Code: KeyDelete} + } + return KeyPressEvent{Code: KeyBackspace} + case ansi.SP: + return KeyPressEvent{Code: KeySpace, Text: " "} + default: + if b >= ansi.SOH && b <= ansi.SUB { + // Use lower case letters for control codes + code := rune(b + 0x60) + return KeyPressEvent{Code: code, Mod: ModCtrl} + } else if b >= ansi.FS && b <= ansi.US { + code := rune(b + 0x40) + return KeyPressEvent{Code: code, Mod: ModCtrl} + } + return UnknownEvent(b) + } +} diff --git a/packages/tui/input/parse_test.go b/packages/tui/input/parse_test.go new file mode 100644 index 00000000..dc892e0c --- /dev/null +++ b/packages/tui/input/parse_test.go @@ -0,0 +1,47 @@ +package input + +import ( + "image/color" + "reflect" + "testing" + + "github.com/charmbracelet/x/ansi" +) + +func TestParseSequence_Events(t *testing.T) { + input := []byte("\x1b\x1b[Ztest\x00\x1b]10;rgb:1234/1234/1234\x07\x1b[27;2;27~\x1b[?1049;2$y\x1b[4;1$y") + want := []Event{ + KeyPressEvent{Code: KeyTab, Mod: ModShift | ModAlt}, + KeyPressEvent{Code: 't', Text: "t"}, + KeyPressEvent{Code: 'e', Text: "e"}, + KeyPressEvent{Code: 's', Text: "s"}, + KeyPressEvent{Code: 't', Text: "t"}, + KeyPressEvent{Code: KeySpace, Mod: ModCtrl}, + ForegroundColorEvent{color.RGBA{R: 0x12, G: 0x12, B: 0x12, A: 0xff}}, + KeyPressEvent{Code: KeyEscape, Mod: ModShift}, + ModeReportEvent{Mode: ansi.AltScreenSaveCursorMode, Value: ansi.ModeReset}, + ModeReportEvent{Mode: ansi.InsertReplaceMode, Value: ansi.ModeSet}, + } + + var p Parser + for i := 0; len(input) != 0; i++ { + if i >= len(want) { + t.Fatalf("reached end of want events") + } + n, got := p.parseSequence(input) + if !reflect.DeepEqual(got, want[i]) { + t.Errorf("got %#v (%T), want %#v (%T)", got, got, want[i], want[i]) + } + input = input[n:] + } +} + +func BenchmarkParseSequence(b *testing.B) { + var p Parser + input := []byte("\x1b\x1b[Ztest\x00\x1b]10;1234/1234/1234\x07\x1b[27;2;27~") + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + p.parseSequence(input) + } +} diff --git a/packages/tui/input/paste.go b/packages/tui/input/paste.go new file mode 100644 index 00000000..4e8fe68c --- /dev/null +++ b/packages/tui/input/paste.go @@ -0,0 +1,13 @@ +package input + +// PasteEvent is an message that is emitted when a terminal receives pasted text +// using bracketed-paste. +type PasteEvent string + +// PasteStartEvent is an message that is emitted when the terminal starts the +// bracketed-paste text. +type PasteStartEvent struct{} + +// PasteEndEvent is an message that is emitted when the terminal ends the +// bracketed-paste text. +type PasteEndEvent struct{} diff --git a/packages/tui/input/table.go b/packages/tui/input/table.go new file mode 100644 index 00000000..d2373236 --- /dev/null +++ b/packages/tui/input/table.go @@ -0,0 +1,389 @@ +package input + +import ( + "maps" + "strconv" + + "github.com/charmbracelet/x/ansi" +) + +// buildKeysTable builds a table of key sequences and their corresponding key +// events based on the VT100/VT200, XTerm, and Urxvt terminal specs. +func buildKeysTable(flags int, term string) map[string]Key { + nul := Key{Code: KeySpace, Mod: ModCtrl} // ctrl+@ or ctrl+space + if flags&FlagCtrlAt != 0 { + nul = Key{Code: '@', Mod: ModCtrl} + } + + tab := Key{Code: KeyTab} // ctrl+i or tab + if flags&FlagCtrlI != 0 { + tab = Key{Code: 'i', Mod: ModCtrl} + } + + enter := Key{Code: KeyEnter} // ctrl+m or enter + if flags&FlagCtrlM != 0 { + enter = Key{Code: 'm', Mod: ModCtrl} + } + + esc := Key{Code: KeyEscape} // ctrl+[ or escape + if flags&FlagCtrlOpenBracket != 0 { + esc = Key{Code: '[', Mod: ModCtrl} // ctrl+[ or escape + } + + del := Key{Code: KeyBackspace} + if flags&FlagBackspace != 0 { + del.Code = KeyDelete + } + + find := Key{Code: KeyHome} + if flags&FlagFind != 0 { + find.Code = KeyFind + } + + sel := Key{Code: KeyEnd} + if flags&FlagSelect != 0 { + sel.Code = KeySelect + } + + // The following is a table of key sequences and their corresponding key + // events based on the VT100/VT200 terminal specs. + // + // See: https://vt100.net/docs/vt100-ug/chapter3.html#S3.2 + // See: https://vt100.net/docs/vt220-rm/chapter3.html + // + // XXX: These keys may be overwritten by other options like XTerm or + // Terminfo. + table := map[string]Key{ + // C0 control characters + string(byte(ansi.NUL)): nul, + string(byte(ansi.SOH)): {Code: 'a', Mod: ModCtrl}, + string(byte(ansi.STX)): {Code: 'b', Mod: ModCtrl}, + string(byte(ansi.ETX)): {Code: 'c', Mod: ModCtrl}, + string(byte(ansi.EOT)): {Code: 'd', Mod: ModCtrl}, + string(byte(ansi.ENQ)): {Code: 'e', Mod: ModCtrl}, + string(byte(ansi.ACK)): {Code: 'f', Mod: ModCtrl}, + string(byte(ansi.BEL)): {Code: 'g', Mod: ModCtrl}, + string(byte(ansi.BS)): {Code: 'h', Mod: ModCtrl}, + string(byte(ansi.HT)): tab, + string(byte(ansi.LF)): {Code: 'j', Mod: ModCtrl}, + string(byte(ansi.VT)): {Code: 'k', Mod: ModCtrl}, + string(byte(ansi.FF)): {Code: 'l', Mod: ModCtrl}, + string(byte(ansi.CR)): enter, + string(byte(ansi.SO)): {Code: 'n', Mod: ModCtrl}, + string(byte(ansi.SI)): {Code: 'o', Mod: ModCtrl}, + string(byte(ansi.DLE)): {Code: 'p', Mod: ModCtrl}, + string(byte(ansi.DC1)): {Code: 'q', Mod: ModCtrl}, + string(byte(ansi.DC2)): {Code: 'r', Mod: ModCtrl}, + string(byte(ansi.DC3)): {Code: 's', Mod: ModCtrl}, + string(byte(ansi.DC4)): {Code: 't', Mod: ModCtrl}, + string(byte(ansi.NAK)): {Code: 'u', Mod: ModCtrl}, + string(byte(ansi.SYN)): {Code: 'v', Mod: ModCtrl}, + string(byte(ansi.ETB)): {Code: 'w', Mod: ModCtrl}, + string(byte(ansi.CAN)): {Code: 'x', Mod: ModCtrl}, + string(byte(ansi.EM)): {Code: 'y', Mod: ModCtrl}, + string(byte(ansi.SUB)): {Code: 'z', Mod: ModCtrl}, + string(byte(ansi.ESC)): esc, + string(byte(ansi.FS)): {Code: '\\', Mod: ModCtrl}, + string(byte(ansi.GS)): {Code: ']', Mod: ModCtrl}, + string(byte(ansi.RS)): {Code: '^', Mod: ModCtrl}, + string(byte(ansi.US)): {Code: '_', Mod: ModCtrl}, + + // Special keys in G0 + string(byte(ansi.SP)): {Code: KeySpace, Text: " "}, + string(byte(ansi.DEL)): del, + + // Special keys + + "\x1b[Z": {Code: KeyTab, Mod: ModShift}, + + "\x1b[1~": find, + "\x1b[2~": {Code: KeyInsert}, + "\x1b[3~": {Code: KeyDelete}, + "\x1b[4~": sel, + "\x1b[5~": {Code: KeyPgUp}, + "\x1b[6~": {Code: KeyPgDown}, + "\x1b[7~": {Code: KeyHome}, + "\x1b[8~": {Code: KeyEnd}, + + // Normal mode + "\x1b[A": {Code: KeyUp}, + "\x1b[B": {Code: KeyDown}, + "\x1b[C": {Code: KeyRight}, + "\x1b[D": {Code: KeyLeft}, + "\x1b[E": {Code: KeyBegin}, + "\x1b[F": {Code: KeyEnd}, + "\x1b[H": {Code: KeyHome}, + "\x1b[P": {Code: KeyF1}, + "\x1b[Q": {Code: KeyF2}, + "\x1b[R": {Code: KeyF3}, + "\x1b[S": {Code: KeyF4}, + + // Application Cursor Key Mode (DECCKM) + "\x1bOA": {Code: KeyUp}, + "\x1bOB": {Code: KeyDown}, + "\x1bOC": {Code: KeyRight}, + "\x1bOD": {Code: KeyLeft}, + "\x1bOE": {Code: KeyBegin}, + "\x1bOF": {Code: KeyEnd}, + "\x1bOH": {Code: KeyHome}, + "\x1bOP": {Code: KeyF1}, + "\x1bOQ": {Code: KeyF2}, + "\x1bOR": {Code: KeyF3}, + "\x1bOS": {Code: KeyF4}, + + // Keypad Application Mode (DECKPAM) + + "\x1bOM": {Code: KeyKpEnter}, + "\x1bOX": {Code: KeyKpEqual}, + "\x1bOj": {Code: KeyKpMultiply}, + "\x1bOk": {Code: KeyKpPlus}, + "\x1bOl": {Code: KeyKpComma}, + "\x1bOm": {Code: KeyKpMinus}, + "\x1bOn": {Code: KeyKpDecimal}, + "\x1bOo": {Code: KeyKpDivide}, + "\x1bOp": {Code: KeyKp0}, + "\x1bOq": {Code: KeyKp1}, + "\x1bOr": {Code: KeyKp2}, + "\x1bOs": {Code: KeyKp3}, + "\x1bOt": {Code: KeyKp4}, + "\x1bOu": {Code: KeyKp5}, + "\x1bOv": {Code: KeyKp6}, + "\x1bOw": {Code: KeyKp7}, + "\x1bOx": {Code: KeyKp8}, + "\x1bOy": {Code: KeyKp9}, + + // Function keys + + "\x1b[11~": {Code: KeyF1}, + "\x1b[12~": {Code: KeyF2}, + "\x1b[13~": {Code: KeyF3}, + "\x1b[14~": {Code: KeyF4}, + "\x1b[15~": {Code: KeyF5}, + "\x1b[17~": {Code: KeyF6}, + "\x1b[18~": {Code: KeyF7}, + "\x1b[19~": {Code: KeyF8}, + "\x1b[20~": {Code: KeyF9}, + "\x1b[21~": {Code: KeyF10}, + "\x1b[23~": {Code: KeyF11}, + "\x1b[24~": {Code: KeyF12}, + "\x1b[25~": {Code: KeyF13}, + "\x1b[26~": {Code: KeyF14}, + "\x1b[28~": {Code: KeyF15}, + "\x1b[29~": {Code: KeyF16}, + "\x1b[31~": {Code: KeyF17}, + "\x1b[32~": {Code: KeyF18}, + "\x1b[33~": {Code: KeyF19}, + "\x1b[34~": {Code: KeyF20}, + } + + // CSI ~ sequence keys + csiTildeKeys := map[string]Key{ + "1": find, "2": {Code: KeyInsert}, + "3": {Code: KeyDelete}, "4": sel, + "5": {Code: KeyPgUp}, "6": {Code: KeyPgDown}, + "7": {Code: KeyHome}, "8": {Code: KeyEnd}, + // There are no 9 and 10 keys + "11": {Code: KeyF1}, "12": {Code: KeyF2}, + "13": {Code: KeyF3}, "14": {Code: KeyF4}, + "15": {Code: KeyF5}, "17": {Code: KeyF6}, + "18": {Code: KeyF7}, "19": {Code: KeyF8}, + "20": {Code: KeyF9}, "21": {Code: KeyF10}, + "23": {Code: KeyF11}, "24": {Code: KeyF12}, + "25": {Code: KeyF13}, "26": {Code: KeyF14}, + "28": {Code: KeyF15}, "29": {Code: KeyF16}, + "31": {Code: KeyF17}, "32": {Code: KeyF18}, + "33": {Code: KeyF19}, "34": {Code: KeyF20}, + } + + // URxvt keys + // See https://manpages.ubuntu.com/manpages/trusty/man7/urxvt.7.html#key%20codes + table["\x1b[a"] = Key{Code: KeyUp, Mod: ModShift} + table["\x1b[b"] = Key{Code: KeyDown, Mod: ModShift} + table["\x1b[c"] = Key{Code: KeyRight, Mod: ModShift} + table["\x1b[d"] = Key{Code: KeyLeft, Mod: ModShift} + table["\x1bOa"] = Key{Code: KeyUp, Mod: ModCtrl} + table["\x1bOb"] = Key{Code: KeyDown, Mod: ModCtrl} + table["\x1bOc"] = Key{Code: KeyRight, Mod: ModCtrl} + table["\x1bOd"] = Key{Code: KeyLeft, Mod: ModCtrl} + //nolint:godox + // TODO: invistigate if shift-ctrl arrow keys collide with DECCKM keys i.e. + // "\x1bOA", "\x1bOB", "\x1bOC", "\x1bOD" + + // URxvt modifier CSI ~ keys + for k, v := range csiTildeKeys { + key := v + // Normal (no modifier) already defined part of VT100/VT200 + // Shift modifier + key.Mod = ModShift + table["\x1b["+k+"$"] = key + // Ctrl modifier + key.Mod = ModCtrl + table["\x1b["+k+"^"] = key + // Shift-Ctrl modifier + key.Mod = ModShift | ModCtrl + table["\x1b["+k+"@"] = key + } + + // URxvt F keys + // Note: Shift + F1-F10 generates F11-F20. + // This means Shift + F1 and Shift + F2 will generate F11 and F12, the same + // applies to Ctrl + Shift F1 & F2. + // + // P.S. Don't like this? Blame URxvt, configure your terminal to use + // different escapes like XTerm, or switch to a better terminal ¯\_(ツ)_/¯ + // + // See https://manpages.ubuntu.com/manpages/trusty/man7/urxvt.7.html#key%20codes + table["\x1b[23$"] = Key{Code: KeyF11, Mod: ModShift} + table["\x1b[24$"] = Key{Code: KeyF12, Mod: ModShift} + table["\x1b[25$"] = Key{Code: KeyF13, Mod: ModShift} + table["\x1b[26$"] = Key{Code: KeyF14, Mod: ModShift} + table["\x1b[28$"] = Key{Code: KeyF15, Mod: ModShift} + table["\x1b[29$"] = Key{Code: KeyF16, Mod: ModShift} + table["\x1b[31$"] = Key{Code: KeyF17, Mod: ModShift} + table["\x1b[32$"] = Key{Code: KeyF18, Mod: ModShift} + table["\x1b[33$"] = Key{Code: KeyF19, Mod: ModShift} + table["\x1b[34$"] = Key{Code: KeyF20, Mod: ModShift} + table["\x1b[11^"] = Key{Code: KeyF1, Mod: ModCtrl} + table["\x1b[12^"] = Key{Code: KeyF2, Mod: ModCtrl} + table["\x1b[13^"] = Key{Code: KeyF3, Mod: ModCtrl} + table["\x1b[14^"] = Key{Code: KeyF4, Mod: ModCtrl} + table["\x1b[15^"] = Key{Code: KeyF5, Mod: ModCtrl} + table["\x1b[17^"] = Key{Code: KeyF6, Mod: ModCtrl} + table["\x1b[18^"] = Key{Code: KeyF7, Mod: ModCtrl} + table["\x1b[19^"] = Key{Code: KeyF8, Mod: ModCtrl} + table["\x1b[20^"] = Key{Code: KeyF9, Mod: ModCtrl} + table["\x1b[21^"] = Key{Code: KeyF10, Mod: ModCtrl} + table["\x1b[23^"] = Key{Code: KeyF11, Mod: ModCtrl} + table["\x1b[24^"] = Key{Code: KeyF12, Mod: ModCtrl} + table["\x1b[25^"] = Key{Code: KeyF13, Mod: ModCtrl} + table["\x1b[26^"] = Key{Code: KeyF14, Mod: ModCtrl} + table["\x1b[28^"] = Key{Code: KeyF15, Mod: ModCtrl} + table["\x1b[29^"] = Key{Code: KeyF16, Mod: ModCtrl} + table["\x1b[31^"] = Key{Code: KeyF17, Mod: ModCtrl} + table["\x1b[32^"] = Key{Code: KeyF18, Mod: ModCtrl} + table["\x1b[33^"] = Key{Code: KeyF19, Mod: ModCtrl} + table["\x1b[34^"] = Key{Code: KeyF20, Mod: ModCtrl} + table["\x1b[23@"] = Key{Code: KeyF11, Mod: ModShift | ModCtrl} + table["\x1b[24@"] = Key{Code: KeyF12, Mod: ModShift | ModCtrl} + table["\x1b[25@"] = Key{Code: KeyF13, Mod: ModShift | ModCtrl} + table["\x1b[26@"] = Key{Code: KeyF14, Mod: ModShift | ModCtrl} + table["\x1b[28@"] = Key{Code: KeyF15, Mod: ModShift | ModCtrl} + table["\x1b[29@"] = Key{Code: KeyF16, Mod: ModShift | ModCtrl} + table["\x1b[31@"] = Key{Code: KeyF17, Mod: ModShift | ModCtrl} + table["\x1b[32@"] = Key{Code: KeyF18, Mod: ModShift | ModCtrl} + table["\x1b[33@"] = Key{Code: KeyF19, Mod: ModShift | ModCtrl} + table["\x1b[34@"] = Key{Code: KeyF20, Mod: ModShift | ModCtrl} + + // Register Alt + combinations + // XXX: this must come after URxvt but before XTerm keys to register URxvt + // keys with alt modifier + tmap := map[string]Key{} + for seq, key := range table { + key := key + key.Mod |= ModAlt + key.Text = "" // Clear runes + tmap["\x1b"+seq] = key + } + maps.Copy(table, tmap) + + // XTerm modifiers + // These are offset by 1 to be compatible with our Mod type. + // See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-PC-Style-Function-Keys + modifiers := []KeyMod{ + ModShift, // 1 + ModAlt, // 2 + ModShift | ModAlt, // 3 + ModCtrl, // 4 + ModShift | ModCtrl, // 5 + ModAlt | ModCtrl, // 6 + ModShift | ModAlt | ModCtrl, // 7 + ModMeta, // 8 + ModMeta | ModShift, // 9 + ModMeta | ModAlt, // 10 + ModMeta | ModShift | ModAlt, // 11 + ModMeta | ModCtrl, // 12 + ModMeta | ModShift | ModCtrl, // 13 + ModMeta | ModAlt | ModCtrl, // 14 + ModMeta | ModShift | ModAlt | ModCtrl, // 15 + } + + // SS3 keypad function keys + ss3FuncKeys := map[string]Key{ + // These are defined in XTerm + // Taken from Foot keymap.h and XTerm modifyOtherKeys + // https://codeberg.org/dnkl/foot/src/branch/master/keymap.h + "M": {Code: KeyKpEnter}, "X": {Code: KeyKpEqual}, + "j": {Code: KeyKpMultiply}, "k": {Code: KeyKpPlus}, + "l": {Code: KeyKpComma}, "m": {Code: KeyKpMinus}, + "n": {Code: KeyKpDecimal}, "o": {Code: KeyKpDivide}, + "p": {Code: KeyKp0}, "q": {Code: KeyKp1}, + "r": {Code: KeyKp2}, "s": {Code: KeyKp3}, + "t": {Code: KeyKp4}, "u": {Code: KeyKp5}, + "v": {Code: KeyKp6}, "w": {Code: KeyKp7}, + "x": {Code: KeyKp8}, "y": {Code: KeyKp9}, + } + + // XTerm keys + csiFuncKeys := map[string]Key{ + "A": {Code: KeyUp}, "B": {Code: KeyDown}, + "C": {Code: KeyRight}, "D": {Code: KeyLeft}, + "E": {Code: KeyBegin}, "F": {Code: KeyEnd}, + "H": {Code: KeyHome}, "P": {Code: KeyF1}, + "Q": {Code: KeyF2}, "R": {Code: KeyF3}, + "S": {Code: KeyF4}, + } + + // CSI 27 ; ; ~ keys defined in XTerm modifyOtherKeys + modifyOtherKeys := map[int]Key{ + ansi.BS: {Code: KeyBackspace}, + ansi.HT: {Code: KeyTab}, + ansi.CR: {Code: KeyEnter}, + ansi.ESC: {Code: KeyEscape}, + ansi.DEL: {Code: KeyBackspace}, + } + + for _, m := range modifiers { + // XTerm modifier offset +1 + xtermMod := strconv.Itoa(int(m) + 1) + + // CSI 1 ; + for k, v := range csiFuncKeys { + // Functions always have a leading 1 param + seq := "\x1b[1;" + xtermMod + k + key := v + key.Mod = m + table[seq] = key + } + // SS3 + for k, v := range ss3FuncKeys { + seq := "\x1bO" + xtermMod + k + key := v + key.Mod = m + table[seq] = key + } + // CSI ; ~ + for k, v := range csiTildeKeys { + seq := "\x1b[" + k + ";" + xtermMod + "~" + key := v + key.Mod = m + table[seq] = key + } + // CSI 27 ; ; ~ + for k, v := range modifyOtherKeys { + code := strconv.Itoa(k) + seq := "\x1b[27;" + xtermMod + ";" + code + "~" + key := v + key.Mod = m + table[seq] = key + } + } + + // Register terminfo keys + // XXX: this might override keys already registered in table + if flags&FlagTerminfo != 0 { + titable := buildTerminfoKeys(flags, term) + maps.Copy(table, titable) + } + + return table +} diff --git a/packages/tui/input/termcap.go b/packages/tui/input/termcap.go new file mode 100644 index 00000000..3502189f --- /dev/null +++ b/packages/tui/input/termcap.go @@ -0,0 +1,54 @@ +package input + +import ( + "bytes" + "encoding/hex" + "strings" +) + +// CapabilityEvent represents a Termcap/Terminfo response event. Termcap +// responses are generated by the terminal in response to RequestTermcap +// (XTGETTCAP) requests. +// +// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands +type CapabilityEvent string + +func parseTermcap(data []byte) CapabilityEvent { + // XTGETTCAP + if len(data) == 0 { + return CapabilityEvent("") + } + + var tc strings.Builder + split := bytes.Split(data, []byte{';'}) + for _, s := range split { + parts := bytes.SplitN(s, []byte{'='}, 2) + if len(parts) == 0 { + return CapabilityEvent("") + } + + name, err := hex.DecodeString(string(parts[0])) + if err != nil || len(name) == 0 { + continue + } + + var value []byte + if len(parts) > 1 { + value, err = hex.DecodeString(string(parts[1])) + if err != nil { + continue + } + } + + if tc.Len() > 0 { + tc.WriteByte(';') + } + tc.WriteString(string(name)) + if len(value) > 0 { + tc.WriteByte('=') + tc.WriteString(string(value)) + } + } + + return CapabilityEvent(tc.String()) +} diff --git a/packages/tui/input/terminfo.go b/packages/tui/input/terminfo.go new file mode 100644 index 00000000..a54da2c3 --- /dev/null +++ b/packages/tui/input/terminfo.go @@ -0,0 +1,277 @@ +package input + +import ( + "strings" + + "github.com/xo/terminfo" +) + +func buildTerminfoKeys(flags int, term string) map[string]Key { + table := make(map[string]Key) + ti, _ := terminfo.Load(term) + if ti == nil { + return table + } + + tiTable := defaultTerminfoKeys(flags) + + // Default keys + for name, seq := range ti.StringCapsShort() { + if !strings.HasPrefix(name, "k") || len(seq) == 0 { + continue + } + + if k, ok := tiTable[name]; ok { + table[string(seq)] = k + } + } + + // Extended keys + for name, seq := range ti.ExtStringCapsShort() { + if !strings.HasPrefix(name, "k") || len(seq) == 0 { + continue + } + + if k, ok := tiTable[name]; ok { + table[string(seq)] = k + } + } + + return table +} + +// This returns a map of terminfo keys to key events. It's a mix of ncurses +// terminfo default and user-defined key capabilities. +// Upper-case caps that are defined in the default terminfo database are +// - kNXT +// - kPRV +// - kHOM +// - kEND +// - kDC +// - kIC +// - kLFT +// - kRIT +// +// See https://man7.org/linux/man-pages/man5/terminfo.5.html +// See https://github.com/mirror/ncurses/blob/master/include/Caps-ncurses +func defaultTerminfoKeys(flags int) map[string]Key { + keys := map[string]Key{ + "kcuu1": {Code: KeyUp}, + "kUP": {Code: KeyUp, Mod: ModShift}, + "kUP3": {Code: KeyUp, Mod: ModAlt}, + "kUP4": {Code: KeyUp, Mod: ModShift | ModAlt}, + "kUP5": {Code: KeyUp, Mod: ModCtrl}, + "kUP6": {Code: KeyUp, Mod: ModShift | ModCtrl}, + "kUP7": {Code: KeyUp, Mod: ModAlt | ModCtrl}, + "kUP8": {Code: KeyUp, Mod: ModShift | ModAlt | ModCtrl}, + "kcud1": {Code: KeyDown}, + "kDN": {Code: KeyDown, Mod: ModShift}, + "kDN3": {Code: KeyDown, Mod: ModAlt}, + "kDN4": {Code: KeyDown, Mod: ModShift | ModAlt}, + "kDN5": {Code: KeyDown, Mod: ModCtrl}, + "kDN7": {Code: KeyDown, Mod: ModAlt | ModCtrl}, + "kDN6": {Code: KeyDown, Mod: ModShift | ModCtrl}, + "kDN8": {Code: KeyDown, Mod: ModShift | ModAlt | ModCtrl}, + "kcub1": {Code: KeyLeft}, + "kLFT": {Code: KeyLeft, Mod: ModShift}, + "kLFT3": {Code: KeyLeft, Mod: ModAlt}, + "kLFT4": {Code: KeyLeft, Mod: ModShift | ModAlt}, + "kLFT5": {Code: KeyLeft, Mod: ModCtrl}, + "kLFT6": {Code: KeyLeft, Mod: ModShift | ModCtrl}, + "kLFT7": {Code: KeyLeft, Mod: ModAlt | ModCtrl}, + "kLFT8": {Code: KeyLeft, Mod: ModShift | ModAlt | ModCtrl}, + "kcuf1": {Code: KeyRight}, + "kRIT": {Code: KeyRight, Mod: ModShift}, + "kRIT3": {Code: KeyRight, Mod: ModAlt}, + "kRIT4": {Code: KeyRight, Mod: ModShift | ModAlt}, + "kRIT5": {Code: KeyRight, Mod: ModCtrl}, + "kRIT6": {Code: KeyRight, Mod: ModShift | ModCtrl}, + "kRIT7": {Code: KeyRight, Mod: ModAlt | ModCtrl}, + "kRIT8": {Code: KeyRight, Mod: ModShift | ModAlt | ModCtrl}, + "kich1": {Code: KeyInsert}, + "kIC": {Code: KeyInsert, Mod: ModShift}, + "kIC3": {Code: KeyInsert, Mod: ModAlt}, + "kIC4": {Code: KeyInsert, Mod: ModShift | ModAlt}, + "kIC5": {Code: KeyInsert, Mod: ModCtrl}, + "kIC6": {Code: KeyInsert, Mod: ModShift | ModCtrl}, + "kIC7": {Code: KeyInsert, Mod: ModAlt | ModCtrl}, + "kIC8": {Code: KeyInsert, Mod: ModShift | ModAlt | ModCtrl}, + "kdch1": {Code: KeyDelete}, + "kDC": {Code: KeyDelete, Mod: ModShift}, + "kDC3": {Code: KeyDelete, Mod: ModAlt}, + "kDC4": {Code: KeyDelete, Mod: ModShift | ModAlt}, + "kDC5": {Code: KeyDelete, Mod: ModCtrl}, + "kDC6": {Code: KeyDelete, Mod: ModShift | ModCtrl}, + "kDC7": {Code: KeyDelete, Mod: ModAlt | ModCtrl}, + "kDC8": {Code: KeyDelete, Mod: ModShift | ModAlt | ModCtrl}, + "khome": {Code: KeyHome}, + "kHOM": {Code: KeyHome, Mod: ModShift}, + "kHOM3": {Code: KeyHome, Mod: ModAlt}, + "kHOM4": {Code: KeyHome, Mod: ModShift | ModAlt}, + "kHOM5": {Code: KeyHome, Mod: ModCtrl}, + "kHOM6": {Code: KeyHome, Mod: ModShift | ModCtrl}, + "kHOM7": {Code: KeyHome, Mod: ModAlt | ModCtrl}, + "kHOM8": {Code: KeyHome, Mod: ModShift | ModAlt | ModCtrl}, + "kend": {Code: KeyEnd}, + "kEND": {Code: KeyEnd, Mod: ModShift}, + "kEND3": {Code: KeyEnd, Mod: ModAlt}, + "kEND4": {Code: KeyEnd, Mod: ModShift | ModAlt}, + "kEND5": {Code: KeyEnd, Mod: ModCtrl}, + "kEND6": {Code: KeyEnd, Mod: ModShift | ModCtrl}, + "kEND7": {Code: KeyEnd, Mod: ModAlt | ModCtrl}, + "kEND8": {Code: KeyEnd, Mod: ModShift | ModAlt | ModCtrl}, + "kpp": {Code: KeyPgUp}, + "kprv": {Code: KeyPgUp}, + "kPRV": {Code: KeyPgUp, Mod: ModShift}, + "kPRV3": {Code: KeyPgUp, Mod: ModAlt}, + "kPRV4": {Code: KeyPgUp, Mod: ModShift | ModAlt}, + "kPRV5": {Code: KeyPgUp, Mod: ModCtrl}, + "kPRV6": {Code: KeyPgUp, Mod: ModShift | ModCtrl}, + "kPRV7": {Code: KeyPgUp, Mod: ModAlt | ModCtrl}, + "kPRV8": {Code: KeyPgUp, Mod: ModShift | ModAlt | ModCtrl}, + "knp": {Code: KeyPgDown}, + "knxt": {Code: KeyPgDown}, + "kNXT": {Code: KeyPgDown, Mod: ModShift}, + "kNXT3": {Code: KeyPgDown, Mod: ModAlt}, + "kNXT4": {Code: KeyPgDown, Mod: ModShift | ModAlt}, + "kNXT5": {Code: KeyPgDown, Mod: ModCtrl}, + "kNXT6": {Code: KeyPgDown, Mod: ModShift | ModCtrl}, + "kNXT7": {Code: KeyPgDown, Mod: ModAlt | ModCtrl}, + "kNXT8": {Code: KeyPgDown, Mod: ModShift | ModAlt | ModCtrl}, + + "kbs": {Code: KeyBackspace}, + "kcbt": {Code: KeyTab, Mod: ModShift}, + + // Function keys + // This only includes the first 12 function keys. The rest are treated + // as modifiers of the first 12. + // Take a look at XTerm modifyFunctionKeys + // + // XXX: To use unambiguous function keys, use fixterms or kitty clipboard. + // + // See https://invisible-island.net/xterm/manpage/xterm.html#VT100-Widget-Resources:modifyFunctionKeys + // See https://invisible-island.net/xterm/terminfo.html + + "kf1": {Code: KeyF1}, + "kf2": {Code: KeyF2}, + "kf3": {Code: KeyF3}, + "kf4": {Code: KeyF4}, + "kf5": {Code: KeyF5}, + "kf6": {Code: KeyF6}, + "kf7": {Code: KeyF7}, + "kf8": {Code: KeyF8}, + "kf9": {Code: KeyF9}, + "kf10": {Code: KeyF10}, + "kf11": {Code: KeyF11}, + "kf12": {Code: KeyF12}, + "kf13": {Code: KeyF1, Mod: ModShift}, + "kf14": {Code: KeyF2, Mod: ModShift}, + "kf15": {Code: KeyF3, Mod: ModShift}, + "kf16": {Code: KeyF4, Mod: ModShift}, + "kf17": {Code: KeyF5, Mod: ModShift}, + "kf18": {Code: KeyF6, Mod: ModShift}, + "kf19": {Code: KeyF7, Mod: ModShift}, + "kf20": {Code: KeyF8, Mod: ModShift}, + "kf21": {Code: KeyF9, Mod: ModShift}, + "kf22": {Code: KeyF10, Mod: ModShift}, + "kf23": {Code: KeyF11, Mod: ModShift}, + "kf24": {Code: KeyF12, Mod: ModShift}, + "kf25": {Code: KeyF1, Mod: ModCtrl}, + "kf26": {Code: KeyF2, Mod: ModCtrl}, + "kf27": {Code: KeyF3, Mod: ModCtrl}, + "kf28": {Code: KeyF4, Mod: ModCtrl}, + "kf29": {Code: KeyF5, Mod: ModCtrl}, + "kf30": {Code: KeyF6, Mod: ModCtrl}, + "kf31": {Code: KeyF7, Mod: ModCtrl}, + "kf32": {Code: KeyF8, Mod: ModCtrl}, + "kf33": {Code: KeyF9, Mod: ModCtrl}, + "kf34": {Code: KeyF10, Mod: ModCtrl}, + "kf35": {Code: KeyF11, Mod: ModCtrl}, + "kf36": {Code: KeyF12, Mod: ModCtrl}, + "kf37": {Code: KeyF1, Mod: ModShift | ModCtrl}, + "kf38": {Code: KeyF2, Mod: ModShift | ModCtrl}, + "kf39": {Code: KeyF3, Mod: ModShift | ModCtrl}, + "kf40": {Code: KeyF4, Mod: ModShift | ModCtrl}, + "kf41": {Code: KeyF5, Mod: ModShift | ModCtrl}, + "kf42": {Code: KeyF6, Mod: ModShift | ModCtrl}, + "kf43": {Code: KeyF7, Mod: ModShift | ModCtrl}, + "kf44": {Code: KeyF8, Mod: ModShift | ModCtrl}, + "kf45": {Code: KeyF9, Mod: ModShift | ModCtrl}, + "kf46": {Code: KeyF10, Mod: ModShift | ModCtrl}, + "kf47": {Code: KeyF11, Mod: ModShift | ModCtrl}, + "kf48": {Code: KeyF12, Mod: ModShift | ModCtrl}, + "kf49": {Code: KeyF1, Mod: ModAlt}, + "kf50": {Code: KeyF2, Mod: ModAlt}, + "kf51": {Code: KeyF3, Mod: ModAlt}, + "kf52": {Code: KeyF4, Mod: ModAlt}, + "kf53": {Code: KeyF5, Mod: ModAlt}, + "kf54": {Code: KeyF6, Mod: ModAlt}, + "kf55": {Code: KeyF7, Mod: ModAlt}, + "kf56": {Code: KeyF8, Mod: ModAlt}, + "kf57": {Code: KeyF9, Mod: ModAlt}, + "kf58": {Code: KeyF10, Mod: ModAlt}, + "kf59": {Code: KeyF11, Mod: ModAlt}, + "kf60": {Code: KeyF12, Mod: ModAlt}, + "kf61": {Code: KeyF1, Mod: ModShift | ModAlt}, + "kf62": {Code: KeyF2, Mod: ModShift | ModAlt}, + "kf63": {Code: KeyF3, Mod: ModShift | ModAlt}, + } + + // Preserve F keys from F13 to F63 instead of using them for F-keys + // modifiers. + if flags&FlagFKeys != 0 { + keys["kf13"] = Key{Code: KeyF13} + keys["kf14"] = Key{Code: KeyF14} + keys["kf15"] = Key{Code: KeyF15} + keys["kf16"] = Key{Code: KeyF16} + keys["kf17"] = Key{Code: KeyF17} + keys["kf18"] = Key{Code: KeyF18} + keys["kf19"] = Key{Code: KeyF19} + keys["kf20"] = Key{Code: KeyF20} + keys["kf21"] = Key{Code: KeyF21} + keys["kf22"] = Key{Code: KeyF22} + keys["kf23"] = Key{Code: KeyF23} + keys["kf24"] = Key{Code: KeyF24} + keys["kf25"] = Key{Code: KeyF25} + keys["kf26"] = Key{Code: KeyF26} + keys["kf27"] = Key{Code: KeyF27} + keys["kf28"] = Key{Code: KeyF28} + keys["kf29"] = Key{Code: KeyF29} + keys["kf30"] = Key{Code: KeyF30} + keys["kf31"] = Key{Code: KeyF31} + keys["kf32"] = Key{Code: KeyF32} + keys["kf33"] = Key{Code: KeyF33} + keys["kf34"] = Key{Code: KeyF34} + keys["kf35"] = Key{Code: KeyF35} + keys["kf36"] = Key{Code: KeyF36} + keys["kf37"] = Key{Code: KeyF37} + keys["kf38"] = Key{Code: KeyF38} + keys["kf39"] = Key{Code: KeyF39} + keys["kf40"] = Key{Code: KeyF40} + keys["kf41"] = Key{Code: KeyF41} + keys["kf42"] = Key{Code: KeyF42} + keys["kf43"] = Key{Code: KeyF43} + keys["kf44"] = Key{Code: KeyF44} + keys["kf45"] = Key{Code: KeyF45} + keys["kf46"] = Key{Code: KeyF46} + keys["kf47"] = Key{Code: KeyF47} + keys["kf48"] = Key{Code: KeyF48} + keys["kf49"] = Key{Code: KeyF49} + keys["kf50"] = Key{Code: KeyF50} + keys["kf51"] = Key{Code: KeyF51} + keys["kf52"] = Key{Code: KeyF52} + keys["kf53"] = Key{Code: KeyF53} + keys["kf54"] = Key{Code: KeyF54} + keys["kf55"] = Key{Code: KeyF55} + keys["kf56"] = Key{Code: KeyF56} + keys["kf57"] = Key{Code: KeyF57} + keys["kf58"] = Key{Code: KeyF58} + keys["kf59"] = Key{Code: KeyF59} + keys["kf60"] = Key{Code: KeyF60} + keys["kf61"] = Key{Code: KeyF61} + keys["kf62"] = Key{Code: KeyF62} + keys["kf63"] = Key{Code: KeyF63} + } + + return keys +} diff --git a/packages/tui/input/xterm.go b/packages/tui/input/xterm.go new file mode 100644 index 00000000..b3bbc308 --- /dev/null +++ b/packages/tui/input/xterm.go @@ -0,0 +1,47 @@ +package input + +import ( + "github.com/charmbracelet/x/ansi" +) + +func parseXTermModifyOtherKeys(params ansi.Params) Event { + // XTerm modify other keys starts with ESC [ 27 ; ; ~ + xmod, _, _ := params.Param(1, 1) + xrune, _, _ := params.Param(2, 1) + mod := KeyMod(xmod - 1) + r := rune(xrune) + + switch r { + case ansi.BS: + return KeyPressEvent{Mod: mod, Code: KeyBackspace} + case ansi.HT: + return KeyPressEvent{Mod: mod, Code: KeyTab} + case ansi.CR: + return KeyPressEvent{Mod: mod, Code: KeyEnter} + case ansi.ESC: + return KeyPressEvent{Mod: mod, Code: KeyEscape} + case ansi.DEL: + return KeyPressEvent{Mod: mod, Code: KeyBackspace} + } + + // CSI 27 ; ; ~ keys defined in XTerm modifyOtherKeys + k := KeyPressEvent{Code: r, Mod: mod} + if k.Mod <= ModShift { + k.Text = string(r) + } + + return k +} + +// TerminalVersionEvent is a message that represents the terminal version. +type TerminalVersionEvent string + +// ModifyOtherKeysEvent represents a modifyOtherKeys event. +// +// 0: disable +// 1: enable mode 1 +// 2: enable mode 2 +// +// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_ +// See: https://invisible-island.net/xterm/manpage/xterm.html#VT100-Widget-Resources:modifyOtherKeys +type ModifyOtherKeysEvent uint8 diff --git a/packages/tui/internal/app/app.go b/packages/tui/internal/app/app.go index 469857ab..011d5a89 100644 --- a/packages/tui/internal/app/app.go +++ b/packages/tui/internal/app/app.go @@ -12,28 +12,47 @@ import ( tea "github.com/charmbracelet/bubbletea/v2" "github.com/sst/opencode-sdk-go" + "github.com/sst/opencode/internal/clipboard" "github.com/sst/opencode/internal/commands" "github.com/sst/opencode/internal/components/toast" "github.com/sst/opencode/internal/config" + "github.com/sst/opencode/internal/id" "github.com/sst/opencode/internal/styles" "github.com/sst/opencode/internal/theme" "github.com/sst/opencode/internal/util" ) -type App struct { - Info opencode.App - Version string - StatePath string - Config *opencode.Config - Client *opencode.Client - State *config.State - Provider *opencode.Provider - Model *opencode.Model - Session *opencode.Session - Messages []opencode.Message - Commands commands.CommandRegistry +type Message struct { + Info opencode.MessageUnion + Parts []opencode.PartUnion } +type App struct { + Info opencode.App + Modes []opencode.Mode + Providers []opencode.Provider + Version string + StatePath string + Config *opencode.Config + Client *opencode.Client + State *config.State + ModeIndex int + Mode *opencode.Mode + Provider *opencode.Provider + Model *opencode.Model + Session *opencode.Session + Messages []Message + Commands commands.CommandRegistry + InitialModel *string + InitialPrompt *string + IntitialMode *string + compactCancel context.CancelFunc + IsLeaderSequence bool +} + +type SessionCreatedMsg = struct { + Session *opencode.Session +} type SessionSelectedMsg = *opencode.Session type SessionLoadedMsg struct{} type ModelSelectedMsg struct { @@ -46,8 +65,11 @@ type SendMsg struct { Text string Attachments []opencode.FilePartParam } +type SetEditorContentMsg struct { + Text string +} type OptimisticMessageAddedMsg struct { - Message opencode.Message + Message opencode.MessageUnion } type FileRenderedMsg struct { FilePath string @@ -57,7 +79,11 @@ func New( ctx context.Context, version string, appInfo opencode.App, + modes []opencode.Mode, httpClient *opencode.Client, + initialModel *string, + initialPrompt *string, + initialMode *string, ) (*App, error) { util.RootPath = appInfo.Path.Root util.CwdPath = appInfo.Path.Cwd @@ -78,14 +104,36 @@ func New( config.SaveState(appStatePath, appState) } + if appState.ModeModel == nil { + appState.ModeModel = make(map[string]config.ModeModel) + } + if configInfo.Theme != "" { appState.Theme = configInfo.Theme } - if configInfo.Model != "" { - splits := strings.Split(configInfo.Model, "/") - appState.Provider = splits[0] - appState.Model = strings.Join(splits[1:], "/") + var modeIndex int + var mode *opencode.Mode + modeName := "build" + if appState.Mode != "" { + modeName = appState.Mode + } + if initialMode != nil && *initialMode != "" { + modeName = *initialMode + } + for i, m := range modes { + if m.Name == modeName { + modeIndex = i + break + } + } + mode = &modes[modeIndex] + + if mode.Model.ModelID != "" { + appState.ModeModel[mode.Name] = config.ModeModel{ + ProviderID: mode.Model.ProviderID, + ModelID: mode.Model.ModelID, + } } if err := theme.LoadThemesFromDirectories( @@ -109,15 +157,21 @@ func New( slog.Debug("Loaded config", "config", configInfo) app := &App{ - Info: appInfo, - Version: version, - StatePath: appStatePath, - Config: configInfo, - State: appState, - Client: httpClient, - Session: &opencode.Session{}, - Messages: []opencode.Message{}, - Commands: commands.LoadFromConfig(configInfo), + Info: appInfo, + Modes: modes, + Version: version, + StatePath: appStatePath, + Config: configInfo, + State: appState, + Client: httpClient, + ModeIndex: modeIndex, + Mode: mode, + Session: &opencode.Session{}, + Messages: []Message{}, + Commands: commands.LoadFromConfig(configInfo), + InitialModel: initialModel, + InitialPrompt: initialPrompt, + IntitialMode: initialMode, } return app, nil @@ -140,70 +194,168 @@ func (a *App) Key(commandName commands.CommandName) string { return base(key) + muted(" "+command.Description) } -func (a *App) InitializeProvider() tea.Cmd { - return func() tea.Msg { - providersResponse, err := a.Client.Config.Providers(context.Background()) - if err != nil { - slog.Error("Failed to list providers", "error", err) - // TODO: notify user - return nil - } - providers := providersResponse.Providers - var defaultProvider *opencode.Provider - var defaultModel *opencode.Model +func (a *App) SetClipboard(text string) tea.Cmd { + var cmds []tea.Cmd + cmds = append(cmds, func() tea.Msg { + clipboard.Write(clipboard.FmtText, []byte(text)) + return nil + }) + // try to set the clipboard using OSC52 for terminals that support it + cmds = append(cmds, tea.SetClipboard(text)) + return tea.Sequence(cmds...) +} - var anthropic *opencode.Provider - for _, provider := range providers { - if provider.ID == "anthropic" { - anthropic = &provider - } +func (a *App) cycleMode(forward bool) (*App, tea.Cmd) { + if forward { + a.ModeIndex++ + if a.ModeIndex >= len(a.Modes) { + a.ModeIndex = 0 } - - // default to anthropic if available - if anthropic != nil { - defaultProvider = anthropic - defaultModel = getDefaultModel(providersResponse, *anthropic) + } else { + a.ModeIndex-- + if a.ModeIndex < 0 { + a.ModeIndex = len(a.Modes) - 1 } + } + a.Mode = &a.Modes[a.ModeIndex] - for _, provider := range providers { - if defaultProvider == nil || defaultModel == nil { - defaultProvider = &provider - defaultModel = getDefaultModel(providersResponse, provider) - } - providers = append(providers, provider) - } - if len(providers) == 0 { - slog.Error("No providers configured") - return nil + modelID := a.Mode.Model.ModelID + providerID := a.Mode.Model.ProviderID + if modelID == "" { + if model, ok := a.State.ModeModel[a.Mode.Name]; ok { + modelID = model.ModelID + providerID = model.ProviderID } + } - var currentProvider *opencode.Provider - var currentModel *opencode.Model - for _, provider := range providers { - if provider.ID == a.State.Provider { - currentProvider = &provider - + if modelID != "" { + for _, provider := range a.Providers { + if provider.ID == providerID { + a.Provider = &provider for _, model := range provider.Models { - if model.ID == a.State.Model { - currentModel = &model + if model.ID == modelID { + a.Model = &model + break + } + } + break + } + } + } + + a.State.Mode = a.Mode.Name + + return a, func() tea.Msg { + a.SaveState() + return nil + } +} + +func (a *App) SwitchMode() (*App, tea.Cmd) { + return a.cycleMode(true) +} + +func (a *App) SwitchModeReverse() (*App, tea.Cmd) { + return a.cycleMode(false) +} + +func (a *App) InitializeProvider() tea.Cmd { + providersResponse, err := a.Client.App.Providers(context.Background()) + if err != nil { + slog.Error("Failed to list providers", "error", err) + // TODO: notify user + return nil + } + providers := providersResponse.Providers + var defaultProvider *opencode.Provider + var defaultModel *opencode.Model + + var anthropic *opencode.Provider + for _, provider := range providers { + if provider.ID == "anthropic" { + anthropic = &provider + } + } + + // default to anthropic if available + if anthropic != nil { + defaultProvider = anthropic + defaultModel = getDefaultModel(providersResponse, *anthropic) + } + + for _, provider := range providers { + if defaultProvider == nil || defaultModel == nil { + defaultProvider = &provider + defaultModel = getDefaultModel(providersResponse, provider) + } + providers = append(providers, provider) + } + if len(providers) == 0 { + slog.Error("No providers configured") + return nil + } + + a.Providers = providers + + // retains backwards compatibility with old state format + if model, ok := a.State.ModeModel[a.State.Mode]; ok { + a.State.Provider = model.ProviderID + a.State.Model = model.ModelID + } + + var currentProvider *opencode.Provider + var currentModel *opencode.Model + for _, provider := range providers { + if provider.ID == a.State.Provider { + currentProvider = &provider + + for _, model := range provider.Models { + if model.ID == a.State.Model { + currentModel = &model + } + } + } + } + if currentProvider == nil || currentModel == nil { + currentProvider = defaultProvider + currentModel = defaultModel + } + + var initialProvider *opencode.Provider + var initialModel *opencode.Model + if a.InitialModel != nil && *a.InitialModel != "" { + splits := strings.Split(*a.InitialModel, "/") + for _, provider := range providers { + if provider.ID == splits[0] { + initialProvider = &provider + for _, model := range provider.Models { + modelID := strings.Join(splits[1:], "/") + if model.ID == modelID { + initialModel = &model } } } } - if currentProvider == nil || currentModel == nil { - currentProvider = defaultProvider - currentModel = defaultModel - } - - return ModelSelectedMsg{ - Provider: *currentProvider, - Model: *currentModel, - } } + + if initialProvider != nil && initialModel != nil { + currentProvider = initialProvider + currentModel = initialModel + } + + var cmds []tea.Cmd + cmds = append(cmds, util.CmdHandler(ModelSelectedMsg{ + Provider: *currentProvider, + Model: *currentModel, + })) + if a.InitialPrompt != nil && *a.InitialPrompt != "" { + cmds = append(cmds, util.CmdHandler(SendMsg{Text: *a.InitialPrompt})) + } + return tea.Sequence(cmds...) } func getDefaultModel( - response *opencode.ConfigProvidersResponse, + response *opencode.AppProvidersResponse, provider opencode.Provider, ) *opencode.Model { if match, ok := response.Default[provider.ID]; ok { @@ -223,7 +375,10 @@ func (a *App) IsBusy() bool { } lastMessage := a.Messages[len(a.Messages)-1] - return lastMessage.Metadata.Time.Completed == 0 + if casted, ok := lastMessage.Info.(opencode.AssistantMessage); ok { + return casted.Time.Completed == 0 + } + return false } func (a *App) SaveState() { @@ -243,10 +398,11 @@ func (a *App) InitializeProject(ctx context.Context) tea.Cmd { } a.Session = session - cmds = append(cmds, util.CmdHandler(SessionSelectedMsg(session))) + cmds = append(cmds, util.CmdHandler(SessionCreatedMsg{Session: session})) go func() { _, err := a.Client.Session.Init(ctx, a.Session.ID, opencode.SessionInitParams{ + MessageID: opencode.F(id.Ascending(id.Message)), ProviderID: opencode.F(a.Provider.ID), ModelID: opencode.F(a.Model.ID), }) @@ -260,13 +416,30 @@ func (a *App) InitializeProject(ctx context.Context) tea.Cmd { } func (a *App) CompactSession(ctx context.Context) tea.Cmd { + if a.compactCancel != nil { + a.compactCancel() + } + + compactCtx, cancel := context.WithCancel(ctx) + a.compactCancel = cancel + go func() { - _, err := a.Client.Session.Summarize(ctx, a.Session.ID, opencode.SessionSummarizeParams{ - ProviderID: opencode.F(a.Provider.ID), - ModelID: opencode.F(a.Model.ID), - }) + defer func() { + a.compactCancel = nil + }() + + _, err := a.Client.Session.Summarize( + compactCtx, + a.Session.ID, + opencode.SessionSummarizeParams{ + ProviderID: opencode.F(a.Provider.ID), + ModelID: opencode.F(a.Model.ID), + }, + ) if err != nil { - slog.Error("Failed to compact session", "error", err) + if compactCtx.Err() != context.Canceled { + slog.Error("Failed to compact session", "error", err) + } } }() return nil @@ -301,61 +474,73 @@ func (a *App) SendChatMessage( return a, toast.NewErrorToast(err.Error()) } a.Session = session - cmds = append(cmds, util.CmdHandler(SessionSelectedMsg(session))) + cmds = append(cmds, util.CmdHandler(SessionCreatedMsg{Session: session})) } - optimisticParts := []opencode.MessagePart{{ - Type: opencode.MessagePartTypeText, - Text: text, + message := opencode.UserMessage{ + ID: id.Ascending(id.Message), + SessionID: a.Session.ID, + Role: opencode.UserMessageRoleUser, + Time: opencode.UserMessageTime{ + Created: float64(time.Now().UnixMilli()), + }, + } + + parts := []opencode.PartUnion{opencode.TextPart{ + ID: id.Ascending(id.Part), + MessageID: message.ID, + SessionID: a.Session.ID, + Type: opencode.TextPartTypeText, + Text: text, }} if len(attachments) > 0 { for _, attachment := range attachments { - optimisticParts = append(optimisticParts, opencode.MessagePart{ - Type: opencode.MessagePartTypeFile, + parts = append(parts, opencode.FilePart{ + ID: id.Ascending(id.Part), + MessageID: message.ID, + SessionID: a.Session.ID, + Type: opencode.FilePartTypeFile, Filename: attachment.Filename.Value, - MediaType: attachment.MediaType.Value, + Mime: attachment.Mime.Value, URL: attachment.URL.Value, }) } } - optimisticMessage := opencode.Message{ - ID: fmt.Sprintf("optimistic-%d", time.Now().UnixNano()), - Role: opencode.MessageRoleUser, - Parts: optimisticParts, - Metadata: opencode.MessageMetadata{ - SessionID: a.Session.ID, - Time: opencode.MessageMetadataTime{ - Created: float64(time.Now().Unix()), - }, - }, - } - - a.Messages = append(a.Messages, optimisticMessage) - cmds = append(cmds, util.CmdHandler(OptimisticMessageAddedMsg{Message: optimisticMessage})) + a.Messages = append(a.Messages, Message{Info: message, Parts: parts}) + cmds = append(cmds, util.CmdHandler(OptimisticMessageAddedMsg{Message: message})) cmds = append(cmds, func() tea.Msg { - parts := []opencode.MessagePartUnionParam{ - opencode.TextPartParam{ - Type: opencode.F(opencode.TextPartTypeText), - Text: opencode.F(text), - }, - } - if len(attachments) > 0 { - for _, attachment := range attachments { - parts = append(parts, opencode.FilePartParam{ - MediaType: attachment.MediaType, - Type: attachment.Type, - URL: attachment.URL, - Filename: attachment.Filename, + partsParam := []opencode.SessionChatParamsPartUnion{} + for _, part := range parts { + switch casted := part.(type) { + case opencode.TextPart: + partsParam = append(partsParam, opencode.TextPartParam{ + ID: opencode.F(casted.ID), + MessageID: opencode.F(casted.MessageID), + SessionID: opencode.F(casted.SessionID), + Type: opencode.F(casted.Type), + Text: opencode.F(casted.Text), + }) + case opencode.FilePart: + partsParam = append(partsParam, opencode.FilePartParam{ + ID: opencode.F(casted.ID), + Mime: opencode.F(casted.Mime), + MessageID: opencode.F(casted.MessageID), + SessionID: opencode.F(casted.SessionID), + Type: opencode.F(casted.Type), + URL: opencode.F(casted.URL), + Filename: opencode.F(casted.Filename), }) } } _, err := a.Client.Session.Chat(ctx, a.Session.ID, opencode.SessionChatParams{ - Parts: opencode.F(parts), + Parts: opencode.F(partsParam), + MessageID: opencode.F(message.ID), ProviderID: opencode.F(a.Provider.ID), ModelID: opencode.F(a.Model.ID), + Mode: opencode.F(a.Mode.Name), }) if err != nil { errormsg := fmt.Sprintf("failed to send message: %v", err) @@ -371,6 +556,12 @@ func (a *App) SendChatMessage( } func (a *App) Cancel(ctx context.Context, sessionID string) error { + // Cancel any running compact operation + if a.compactCancel != nil { + a.compactCancel() + a.compactCancel = nil + } + _, err := a.Client.Session.Abort(ctx, sessionID) if err != nil { slog.Error("Failed to cancel session", "error", err) @@ -404,20 +595,30 @@ func (a *App) DeleteSession(ctx context.Context, sessionID string) error { return nil } -func (a *App) ListMessages(ctx context.Context, sessionId string) ([]opencode.Message, error) { +func (a *App) ListMessages(ctx context.Context, sessionId string) ([]Message, error) { response, err := a.Client.Session.Messages(ctx, sessionId) if err != nil { return nil, err } if response == nil { - return []opencode.Message{}, nil + return []Message{}, nil + } + messages := []Message{} + for _, message := range *response { + msg := Message{ + Info: message.Info.AsUnion(), + Parts: []opencode.PartUnion{}, + } + for _, part := range message.Parts { + msg.Parts = append(msg.Parts, part.AsUnion()) + } + messages = append(messages, msg) } - messages := *response return messages, nil } func (a *App) ListProviders(ctx context.Context) ([]opencode.Provider, error) { - response, err := a.Client.Config.Providers(ctx) + response, err := a.Client.App.Providers(ctx) if err != nil { return nil, err } diff --git a/packages/tui/internal/app/constants.go b/packages/tui/internal/app/constants.go new file mode 100644 index 00000000..0d37833f --- /dev/null +++ b/packages/tui/internal/app/constants.go @@ -0,0 +1,4 @@ +package app + +const MAX_CONTAINER_WIDTH = 86 +const EDIT_DIFF_MAX_WIDTH = 180 diff --git a/packages/tui/internal/clipboard/clipboard.go b/packages/tui/internal/clipboard/clipboard.go new file mode 100644 index 00000000..70e05bd2 --- /dev/null +++ b/packages/tui/internal/clipboard/clipboard.go @@ -0,0 +1,155 @@ +// Copyright 2021 The golang.design Initiative Authors. +// All rights reserved. Use of this source code is governed +// by a MIT license that can be found in the LICENSE file. +// +// Written by Changkun Ou + +/* +Package clipboard provides cross platform clipboard access and supports +macOS/Linux/Windows/Android/iOS platform. Before interacting with the +clipboard, one must call Init to assert if it is possible to use this +package: + + err := clipboard.Init() + if err != nil { + panic(err) + } + +The most common operations are `Read` and `Write`. To use them: + + // write/read text format data of the clipboard, and + // the byte buffer regarding the text are UTF8 encoded. + clipboard.Write(clipboard.FmtText, []byte("text data")) + clipboard.Read(clipboard.FmtText) + + // write/read image format data of the clipboard, and + // the byte buffer regarding the image are PNG encoded. + clipboard.Write(clipboard.FmtImage, []byte("image data")) + clipboard.Read(clipboard.FmtImage) + +Note that read/write regarding image format assumes that the bytes are +PNG encoded since it serves the alpha blending purpose that might be +used in other graphical software. + +In addition, `clipboard.Write` returns a channel that can receive an +empty struct as a signal, which indicates the corresponding write call +to the clipboard is outdated, meaning the clipboard has been overwritten +by others and the previously written data is lost. For instance: + + changed := clipboard.Write(clipboard.FmtText, []byte("text data")) + + select { + case <-changed: + println(`"text data" is no longer available from clipboard.`) + } + +You can ignore the returning channel if you don't need this type of +notification. Furthermore, when you need more than just knowing whether +clipboard data is changed, use the watcher API: + + ch := clipboard.Watch(context.TODO(), clipboard.FmtText) + for data := range ch { + // print out clipboard data whenever it is changed + println(string(data)) + } +*/ +package clipboard + +import ( + "context" + "errors" + "fmt" + "os" + "sync" +) + +var ( + // activate only for running tests. + debug = false + errUnavailable = errors.New("clipboard unavailable") + errUnsupported = errors.New("unsupported format") + errNoCgo = errors.New("clipboard: cannot use when CGO_ENABLED=0") +) + +// Format represents the format of clipboard data. +type Format int + +// All sorts of supported clipboard data +const ( + // FmtText indicates plain text clipboard format + FmtText Format = iota + // FmtImage indicates image/png clipboard format + FmtImage +) + +var ( + // Due to the limitation on operating systems (such as darwin), + // concurrent read can even cause panic, use a global lock to + // guarantee one read at a time. + lock = sync.Mutex{} + initOnce sync.Once + initError error +) + +// Init initializes the clipboard package. It returns an error +// if the clipboard is not available to use. This may happen if the +// target system lacks required dependency, such as libx11-dev in X11 +// environment. For example, +// +// err := clipboard.Init() +// if err != nil { +// panic(err) +// } +// +// If Init returns an error, any subsequent Read/Write/Watch call +// may result in an unrecoverable panic. +func Init() error { + initOnce.Do(func() { + initError = initialize() + }) + return initError +} + +// Read returns a chunk of bytes of the clipboard data if it presents +// in the desired format t presents. Otherwise, it returns nil. +func Read(t Format) []byte { + lock.Lock() + defer lock.Unlock() + + buf, err := read(t) + if err != nil { + if debug { + fmt.Fprintf(os.Stderr, "read clipboard err: %v\n", err) + } + return nil + } + return buf +} + +// Write writes a given buffer to the clipboard in a specified format. +// Write returned a receive-only channel can receive an empty struct +// as a signal, which indicates the clipboard has been overwritten from +// this write. +// If format t indicates an image, then the given buf assumes +// the image data is PNG encoded. +func Write(t Format, buf []byte) <-chan struct{} { + lock.Lock() + defer lock.Unlock() + + changed, err := write(t, buf) + if err != nil { + if debug { + fmt.Fprintf(os.Stderr, "write to clipboard err: %v\n", err) + } + return nil + } + return changed +} + +// Watch returns a receive-only channel that received the clipboard data +// whenever any change of clipboard data in the desired format happens. +// +// The returned channel will be closed if the given context is canceled. +func Watch(ctx context.Context, t Format) <-chan []byte { + return watch(ctx, t) +} diff --git a/packages/tui/internal/clipboard/clipboard_darwin.go b/packages/tui/internal/clipboard/clipboard_darwin.go new file mode 100644 index 00000000..ead6811f --- /dev/null +++ b/packages/tui/internal/clipboard/clipboard_darwin.go @@ -0,0 +1,266 @@ +// Copyright 2021 The golang.design Initiative Authors. +// All rights reserved. Use of this source code is governed +// by a MIT license that can be found in the LICENSE file. +// +// Written by Changkun Ou + +//go:build darwin + +package clipboard + +import ( + "bytes" + "context" + "fmt" + "os" + "os/exec" + "strconv" + "strings" + "sync" + "time" +) + +var ( + lastChangeCount int64 + changeCountMu sync.Mutex +) + +func initialize() error { return nil } + +func read(t Format) (buf []byte, err error) { + switch t { + case FmtText: + return readText() + case FmtImage: + return readImage() + default: + return nil, errUnsupported + } +} + +func readText() ([]byte, error) { + // Check if clipboard contains string data + checkScript := ` + try + set clipboardTypes to (clipboard info) + repeat with aType in clipboardTypes + if (first item of aType) is string then + return "hastext" + end if + end repeat + return "notext" + on error + return "error" + end try + ` + + cmd := exec.Command("osascript", "-e", checkScript) + checkOut, err := cmd.Output() + if err != nil { + return nil, errUnavailable + } + + checkOut = bytes.TrimSpace(checkOut) + if !bytes.Equal(checkOut, []byte("hastext")) { + return nil, errUnavailable + } + + // Now get the actual text + cmd = exec.Command("osascript", "-e", "get the clipboard") + out, err := cmd.Output() + if err != nil { + return nil, errUnavailable + } + // Remove trailing newline that osascript adds + out = bytes.TrimSuffix(out, []byte("\n")) + + // If clipboard was set to empty string, return nil + if len(out) == 0 { + return nil, nil + } + return out, nil +} +func readImage() ([]byte, error) { + // AppleScript to read image data from clipboard as base64 + script := ` + try + set theData to the clipboard as «class PNGf» + return theData + on error + return "" + end try + ` + + cmd := exec.Command("osascript", "-e", script) + out, err := cmd.Output() + if err != nil { + return nil, errUnavailable + } + + // Check if we got any data + out = bytes.TrimSpace(out) + if len(out) == 0 { + return nil, errUnavailable + } + + // The output is in hex format (e.g., «data PNGf89504E...») + // We need to extract and convert it + outStr := string(out) + if !strings.HasPrefix(outStr, "«data PNGf") || !strings.HasSuffix(outStr, "»") { + return nil, errUnavailable + } + + // Extract hex data + hexData := strings.TrimPrefix(outStr, "«data PNGf") + hexData = strings.TrimSuffix(hexData, "»") + + // Convert hex to bytes + buf := make([]byte, len(hexData)/2) + for i := 0; i < len(hexData); i += 2 { + b, err := strconv.ParseUint(hexData[i:i+2], 16, 8) + if err != nil { + return nil, errUnavailable + } + buf[i/2] = byte(b) + } + + return buf, nil +} + +// write writes the given data to clipboard and +// returns true if success or false if failed. +func write(t Format, buf []byte) (<-chan struct{}, error) { + var err error + switch t { + case FmtText: + err = writeText(buf) + case FmtImage: + err = writeImage(buf) + default: + return nil, errUnsupported + } + + if err != nil { + return nil, err + } + + // Update change count + changeCountMu.Lock() + lastChangeCount++ + currentCount := lastChangeCount + changeCountMu.Unlock() + + // use unbuffered channel to prevent goroutine leak + changed := make(chan struct{}, 1) + go func() { + for { + time.Sleep(time.Second) + changeCountMu.Lock() + if lastChangeCount != currentCount { + changeCountMu.Unlock() + changed <- struct{}{} + close(changed) + return + } + changeCountMu.Unlock() + } + }() + return changed, nil +} + +func writeText(buf []byte) error { + if len(buf) == 0 { + // Clear clipboard + script := `set the clipboard to ""` + cmd := exec.Command("osascript", "-e", script) + if err := cmd.Run(); err != nil { + return errUnavailable + } + return nil + } + + // Escape the text for AppleScript + text := string(buf) + text = strings.ReplaceAll(text, "\\", "\\\\") + text = strings.ReplaceAll(text, "\"", "\\\"") + + script := fmt.Sprintf(`set the clipboard to "%s"`, text) + cmd := exec.Command("osascript", "-e", script) + if err := cmd.Run(); err != nil { + return errUnavailable + } + return nil +} +func writeImage(buf []byte) error { + if len(buf) == 0 { + // Clear clipboard + script := `set the clipboard to ""` + cmd := exec.Command("osascript", "-e", script) + if err := cmd.Run(); err != nil { + return errUnavailable + } + return nil + } + + // Create a temporary file to store the PNG data + tmpFile, err := os.CreateTemp("", "clipboard*.png") + if err != nil { + return errUnavailable + } + defer os.Remove(tmpFile.Name()) + + if _, err := tmpFile.Write(buf); err != nil { + tmpFile.Close() + return errUnavailable + } + tmpFile.Close() + + // Use osascript to set clipboard to the image file + script := fmt.Sprintf(` + set theFile to POSIX file "%s" + set theImage to read theFile as «class PNGf» + set the clipboard to theImage + `, tmpFile.Name()) + + cmd := exec.Command("osascript", "-e", script) + if err := cmd.Run(); err != nil { + return errUnavailable + } + return nil +} +func watch(ctx context.Context, t Format) <-chan []byte { + recv := make(chan []byte, 1) + ti := time.NewTicker(time.Second) + + // Get initial clipboard content + var lastContent []byte + if b := Read(t); b != nil { + lastContent = make([]byte, len(b)) + copy(lastContent, b) + } + + go func() { + defer close(recv) + defer ti.Stop() + + for { + select { + case <-ctx.Done(): + return + case <-ti.C: + b := Read(t) + if b == nil { + continue + } + + // Check if content changed + if !bytes.Equal(lastContent, b) { + recv <- b + lastContent = make([]byte, len(b)) + copy(lastContent, b) + } + } + } + }() + return recv +} diff --git a/packages/tui/internal/clipboard/clipboard_linux.go b/packages/tui/internal/clipboard/clipboard_linux.go new file mode 100644 index 00000000..10190639 --- /dev/null +++ b/packages/tui/internal/clipboard/clipboard_linux.go @@ -0,0 +1,311 @@ +// Copyright 2021 The golang.design Initiative Authors. +// All rights reserved. Use of this source code is governed +// by a MIT license that can be found in the LICENSE file. +// +// Written by Changkun Ou + +//go:build linux + +package clipboard + +import ( + "bytes" + "context" + "fmt" + "log/slog" + "os" + "os/exec" + "strings" + "sync" + "time" +) + +var ( + // Clipboard tools in order of preference + clipboardTools = []struct { + name string + readCmd []string + writeCmd []string + readImg []string + writeImg []string + available bool + }{ + { + name: "xclip", + readCmd: []string{"xclip", "-selection", "clipboard", "-o"}, + writeCmd: []string{"xclip", "-selection", "clipboard"}, + readImg: []string{"xclip", "-selection", "clipboard", "-t", "image/png", "-o"}, + writeImg: []string{"xclip", "-selection", "clipboard", "-t", "image/png"}, + }, + { + name: "xsel", + readCmd: []string{"xsel", "--clipboard", "--output"}, + writeCmd: []string{"xsel", "--clipboard", "--input"}, + readImg: []string{"xsel", "--clipboard", "--output"}, + writeImg: []string{"xsel", "--clipboard", "--input"}, + }, + { + name: "wl-copy", + readCmd: []string{"wl-paste", "-n"}, + writeCmd: []string{"wl-copy"}, + readImg: []string{"wl-paste", "-t", "image/png", "-n"}, + writeImg: []string{"wl-copy", "-t", "image/png"}, + }, + } + + selectedTool int = -1 + toolMutex sync.Mutex + lastChangeTime time.Time + changeTimeMu sync.Mutex +) + +func initialize() error { + toolMutex.Lock() + defer toolMutex.Unlock() + + if selectedTool >= 0 { + return nil // Already initialized + } + + order := []string{"xclip", "xsel", "wl-copy"} + if os.Getenv("WAYLAND_DISPLAY") != "" { + order = []string{"wl-copy", "xclip", "xsel"} + } + + for _, name := range order { + for i, tool := range clipboardTools { + if tool.name == name { + cmd := exec.Command("which", tool.name) + if err := cmd.Run(); err == nil { + clipboardTools[i].available = true + if selectedTool < 0 { + selectedTool = i + slog.Debug("Clipboard tool found", "tool", tool.name) + } + } + break + } + } + } + + if selectedTool < 0 { + slog.Warn( + "No clipboard utility found on system. Copy/paste functionality will be disabled. See https://opencode.ai/docs/troubleshooting/ for more information.", + ) + return fmt.Errorf(`%w: No clipboard utility found. Install one of the following: + +For X11 systems: + apt install -y xclip + # or + apt install -y xsel + +For Wayland systems: + apt install -y wl-clipboard + +If running in a headless environment, you may also need: + apt install -y xvfb + # and run: + Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 & + export DISPLAY=:99.0`, errUnavailable) + } + + return nil +} + +func read(t Format) (buf []byte, err error) { + // Ensure clipboard is initialized before attempting to read + if err := initialize(); err != nil { + slog.Debug("Clipboard read failed: not initialized", "error", err) + return nil, err + } + + toolMutex.Lock() + tool := clipboardTools[selectedTool] + toolMutex.Unlock() + + switch t { + case FmtText: + return readText(tool) + case FmtImage: + return readImage(tool) + default: + return nil, errUnsupported + } +} + +func readText(tool struct { + name string + readCmd []string + writeCmd []string + readImg []string + writeImg []string + available bool +}) ([]byte, error) { + // First check if clipboard contains text + cmd := exec.Command(tool.readCmd[0], tool.readCmd[1:]...) + out, err := cmd.Output() + if err != nil { + // Check if it's because clipboard contains non-text data + if tool.name == "xclip" { + // xclip returns error when clipboard doesn't contain requested type + checkCmd := exec.Command("xclip", "-selection", "clipboard", "-t", "TARGETS", "-o") + targets, _ := checkCmd.Output() + if bytes.Contains(targets, []byte("image/png")) && + !bytes.Contains(targets, []byte("UTF8_STRING")) { + return nil, errUnavailable + } + } + return nil, errUnavailable + } + + return out, nil +} + +func readImage(tool struct { + name string + readCmd []string + writeCmd []string + readImg []string + writeImg []string + available bool +}) ([]byte, error) { + if tool.name == "xsel" { + // xsel doesn't support image types well, return error + return nil, errUnavailable + } + + cmd := exec.Command(tool.readImg[0], tool.readImg[1:]...) + out, err := cmd.Output() + if err != nil { + return nil, errUnavailable + } + + // Verify it's PNG data + if len(out) < 8 || + !bytes.Equal(out[:8], []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}) { + return nil, errUnavailable + } + + return out, nil +} + +func write(t Format, buf []byte) (<-chan struct{}, error) { + // Ensure clipboard is initialized before attempting to write + if err := initialize(); err != nil { + return nil, err + } + + toolMutex.Lock() + tool := clipboardTools[selectedTool] + toolMutex.Unlock() + + var cmd *exec.Cmd + switch t { + case FmtText: + if len(buf) == 0 { + // Write empty string + cmd = exec.Command(tool.writeCmd[0], tool.writeCmd[1:]...) + cmd.Stdin = bytes.NewReader([]byte{}) + } else { + cmd = exec.Command(tool.writeCmd[0], tool.writeCmd[1:]...) + cmd.Stdin = bytes.NewReader(buf) + } + case FmtImage: + if tool.name == "xsel" { + // xsel doesn't support image types well + return nil, errUnavailable + } + if len(buf) == 0 { + // Clear clipboard + cmd = exec.Command(tool.writeCmd[0], tool.writeCmd[1:]...) + cmd.Stdin = bytes.NewReader([]byte{}) + } else { + cmd = exec.Command(tool.writeImg[0], tool.writeImg[1:]...) + cmd.Stdin = bytes.NewReader(buf) + } + default: + return nil, errUnsupported + } + + if err := cmd.Run(); err != nil { + return nil, errUnavailable + } + + // Update change time + changeTimeMu.Lock() + lastChangeTime = time.Now() + currentTime := lastChangeTime + changeTimeMu.Unlock() + + // Create change notification channel + changed := make(chan struct{}, 1) + go func() { + for { + time.Sleep(time.Second) + changeTimeMu.Lock() + if !lastChangeTime.Equal(currentTime) { + changeTimeMu.Unlock() + changed <- struct{}{} + close(changed) + return + } + changeTimeMu.Unlock() + } + }() + + return changed, nil +} + +func watch(ctx context.Context, t Format) <-chan []byte { + recv := make(chan []byte, 1) + + // Ensure clipboard is initialized before starting watch + if err := initialize(); err != nil { + close(recv) + return recv + } + + ti := time.NewTicker(time.Second) + + // Get initial clipboard content + var lastContent []byte + if b := Read(t); b != nil { + lastContent = make([]byte, len(b)) + copy(lastContent, b) + } + + go func() { + defer close(recv) + defer ti.Stop() + + for { + select { + case <-ctx.Done(): + return + case <-ti.C: + b := Read(t) + if b == nil { + continue + } + + // Check if content changed + if !bytes.Equal(lastContent, b) { + recv <- b + lastContent = make([]byte, len(b)) + copy(lastContent, b) + } + } + } + }() + return recv +} + +// Helper function to check clipboard content type for xclip +func getClipboardTargets() []string { + cmd := exec.Command("xclip", "-selection", "clipboard", "-t", "TARGETS", "-o") + out, err := cmd.Output() + if err != nil { + return nil + } + return strings.Split(string(out), "\n") +} diff --git a/packages/tui/internal/clipboard/clipboard_nocgo.go b/packages/tui/internal/clipboard/clipboard_nocgo.go new file mode 100644 index 00000000..7b3e05f6 --- /dev/null +++ b/packages/tui/internal/clipboard/clipboard_nocgo.go @@ -0,0 +1,25 @@ +//go:build !windows && !darwin && !linux && !cgo + +package clipboard + +import "context" + +func initialize() error { + return errNoCgo +} + +func read(t Format) (buf []byte, err error) { + panic("clipboard: cannot use when CGO_ENABLED=0") +} + +func readc(t string) ([]byte, error) { + panic("clipboard: cannot use when CGO_ENABLED=0") +} + +func write(t Format, buf []byte) (<-chan struct{}, error) { + panic("clipboard: cannot use when CGO_ENABLED=0") +} + +func watch(ctx context.Context, t Format) <-chan []byte { + panic("clipboard: cannot use when CGO_ENABLED=0") +} diff --git a/packages/tui/internal/clipboard/clipboard_windows.go b/packages/tui/internal/clipboard/clipboard_windows.go new file mode 100644 index 00000000..bd042cda --- /dev/null +++ b/packages/tui/internal/clipboard/clipboard_windows.go @@ -0,0 +1,551 @@ +// Copyright 2021 The golang.design Initiative Authors. +// All rights reserved. Use of this source code is governed +// by a MIT license that can be found in the LICENSE file. +// +// Written by Changkun Ou + +//go:build windows + +package clipboard + +// Interacting with Clipboard on Windows: +// https://docs.microsoft.com/zh-cn/windows/win32/dataxchg/using-the-clipboard + +import ( + "bytes" + "context" + "encoding/binary" + "errors" + "fmt" + "image" + "image/color" + "image/png" + "reflect" + "runtime" + "syscall" + "time" + "unicode/utf16" + "unsafe" + + "golang.org/x/image/bmp" +) + +func initialize() error { return nil } + +// readText reads the clipboard and returns the text data if presents. +// The caller is responsible for opening/closing the clipboard before +// calling this function. +func readText() (buf []byte, err error) { + hMem, _, err := getClipboardData.Call(cFmtUnicodeText) + if hMem == 0 { + return nil, err + } + p, _, err := gLock.Call(hMem) + if p == 0 { + return nil, err + } + defer gUnlock.Call(hMem) + + // Find NUL terminator + n := 0 + for ptr := unsafe.Pointer(p); *(*uint16)(ptr) != 0; n++ { + ptr = unsafe.Pointer(uintptr(ptr) + + unsafe.Sizeof(*((*uint16)(unsafe.Pointer(p))))) + } + + var s []uint16 + h := (*reflect.SliceHeader)(unsafe.Pointer(&s)) + h.Data = p + h.Len = n + h.Cap = n + return []byte(string(utf16.Decode(s))), nil +} + +// writeText writes given data to the clipboard. It is the caller's +// responsibility for opening/closing the clipboard before calling +// this function. +func writeText(buf []byte) error { + r, _, err := emptyClipboard.Call() + if r == 0 { + return fmt.Errorf("failed to clear clipboard: %w", err) + } + + // empty text, we are done here. + if len(buf) == 0 { + return nil + } + + s, err := syscall.UTF16FromString(string(buf)) + if err != nil { + return fmt.Errorf("failed to convert given string: %w", err) + } + + hMem, _, err := gAlloc.Call(gmemMoveable, uintptr(len(s)*int(unsafe.Sizeof(s[0])))) + if hMem == 0 { + return fmt.Errorf("failed to alloc global memory: %w", err) + } + + p, _, err := gLock.Call(hMem) + if p == 0 { + return fmt.Errorf("failed to lock global memory: %w", err) + } + defer gUnlock.Call(hMem) + + // no return value + memMove.Call(p, uintptr(unsafe.Pointer(&s[0])), + uintptr(len(s)*int(unsafe.Sizeof(s[0])))) + + v, _, err := setClipboardData.Call(cFmtUnicodeText, hMem) + if v == 0 { + gFree.Call(hMem) + return fmt.Errorf("failed to set text to clipboard: %w", err) + } + + return nil +} + +// readImage reads the clipboard and returns PNG encoded image data +// if presents. The caller is responsible for opening/closing the +// clipboard before calling this function. +func readImage() ([]byte, error) { + hMem, _, err := getClipboardData.Call(cFmtDIBV5) + if hMem == 0 { + // second chance to try FmtDIB + return readImageDib() + } + p, _, err := gLock.Call(hMem) + if p == 0 { + return nil, err + } + defer gUnlock.Call(hMem) + + // inspect header information + info := (*bitmapV5Header)(unsafe.Pointer(p)) + + // maybe deal with other formats? + if info.BitCount != 32 { + return nil, errUnsupported + } + + var data []byte + sh := (*reflect.SliceHeader)(unsafe.Pointer(&data)) + sh.Data = uintptr(p) + sh.Cap = int(info.Size + 4*uint32(info.Width)*uint32(info.Height)) + sh.Len = int(info.Size + 4*uint32(info.Width)*uint32(info.Height)) + img := image.NewRGBA(image.Rect(0, 0, int(info.Width), int(info.Height))) + offset := int(info.Size) + stride := int(info.Width) + for y := 0; y < int(info.Height); y++ { + for x := 0; x < int(info.Width); x++ { + idx := offset + 4*(y*stride+x) + xhat := (x + int(info.Width)) % int(info.Width) + yhat := int(info.Height) - 1 - y + r := data[idx+2] + g := data[idx+1] + b := data[idx+0] + a := data[idx+3] + img.SetRGBA(xhat, yhat, color.RGBA{r, g, b, a}) + } + } + // always use PNG encoding. + var buf bytes.Buffer + png.Encode(&buf, img) + return buf.Bytes(), nil +} + +func readImageDib() ([]byte, error) { + const ( + fileHeaderLen = 14 + infoHeaderLen = 40 + cFmtDIB = 8 + ) + + hClipDat, _, err := getClipboardData.Call(cFmtDIB) + if err != nil { + return nil, errors.New("not dib format data: " + err.Error()) + } + pMemBlk, _, err := gLock.Call(hClipDat) + if pMemBlk == 0 { + return nil, errors.New("failed to call global lock: " + err.Error()) + } + defer gUnlock.Call(hClipDat) + + bmpHeader := (*bitmapHeader)(unsafe.Pointer(pMemBlk)) + dataSize := bmpHeader.SizeImage + fileHeaderLen + infoHeaderLen + + if bmpHeader.SizeImage == 0 && bmpHeader.Compression == 0 { + iSizeImage := bmpHeader.Height * ((bmpHeader.Width*uint32(bmpHeader.BitCount)/8 + 3) &^ 3) + dataSize += iSizeImage + } + buf := new(bytes.Buffer) + binary.Write(buf, binary.LittleEndian, uint16('B')|(uint16('M')<<8)) + binary.Write(buf, binary.LittleEndian, uint32(dataSize)) + binary.Write(buf, binary.LittleEndian, uint32(0)) + const sizeof_colorbar = 0 + binary.Write(buf, binary.LittleEndian, uint32(fileHeaderLen+infoHeaderLen+sizeof_colorbar)) + j := 0 + for i := fileHeaderLen; i < int(dataSize); i++ { + binary.Write(buf, binary.BigEndian, *(*byte)(unsafe.Pointer(pMemBlk + uintptr(j)))) + j++ + } + return bmpToPng(buf) +} + +func bmpToPng(bmpBuf *bytes.Buffer) (buf []byte, err error) { + var f bytes.Buffer + original_image, err := bmp.Decode(bmpBuf) + if err != nil { + return nil, err + } + err = png.Encode(&f, original_image) + if err != nil { + return nil, err + } + return f.Bytes(), nil +} + +func writeImage(buf []byte) error { + r, _, err := emptyClipboard.Call() + if r == 0 { + return fmt.Errorf("failed to clear clipboard: %w", err) + } + + // empty text, we are done here. + if len(buf) == 0 { + return nil + } + + img, err := png.Decode(bytes.NewReader(buf)) + if err != nil { + return fmt.Errorf("input bytes is not PNG encoded: %w", err) + } + + offset := unsafe.Sizeof(bitmapV5Header{}) + width := img.Bounds().Dx() + height := img.Bounds().Dy() + imageSize := 4 * width * height + + data := make([]byte, int(offset)+imageSize) + for y := 0; y < height; y++ { + for x := 0; x < width; x++ { + idx := int(offset) + 4*(y*width+x) + r, g, b, a := img.At(x, height-1-y).RGBA() + data[idx+2] = uint8(r) + data[idx+1] = uint8(g) + data[idx+0] = uint8(b) + data[idx+3] = uint8(a) + } + } + + info := bitmapV5Header{} + info.Size = uint32(offset) + info.Width = int32(width) + info.Height = int32(height) + info.Planes = 1 + info.Compression = 0 // BI_RGB + info.SizeImage = uint32(4 * info.Width * info.Height) + info.RedMask = 0xff0000 // default mask + info.GreenMask = 0xff00 + info.BlueMask = 0xff + info.AlphaMask = 0xff000000 + info.BitCount = 32 // we only deal with 32 bpp at the moment. + // Use calibrated RGB values as Go's image/png assumes linear color space. + // Other options: + // - LCS_CALIBRATED_RGB = 0x00000000 + // - LCS_sRGB = 0x73524742 + // - LCS_WINDOWS_COLOR_SPACE = 0x57696E20 + // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-wmf/eb4bbd50-b3ce-4917-895c-be31f214797f + info.CSType = 0x73524742 + // Use GL_IMAGES for GamutMappingIntent + // Other options: + // - LCS_GM_ABS_COLORIMETRIC = 0x00000008 + // - LCS_GM_BUSINESS = 0x00000001 + // - LCS_GM_GRAPHICS = 0x00000002 + // - LCS_GM_IMAGES = 0x00000004 + // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-wmf/9fec0834-607d-427d-abd5-ab240fb0db38 + info.Intent = 4 // LCS_GM_IMAGES + + infob := make([]byte, int(unsafe.Sizeof(info))) + for i, v := range *(*[unsafe.Sizeof(info)]byte)(unsafe.Pointer(&info)) { + infob[i] = v + } + copy(data[:], infob[:]) + + hMem, _, err := gAlloc.Call(gmemMoveable, + uintptr(len(data)*int(unsafe.Sizeof(data[0])))) + if hMem == 0 { + return fmt.Errorf("failed to alloc global memory: %w", err) + } + + p, _, err := gLock.Call(hMem) + if p == 0 { + return fmt.Errorf("failed to lock global memory: %w", err) + } + defer gUnlock.Call(hMem) + + memMove.Call(p, uintptr(unsafe.Pointer(&data[0])), + uintptr(len(data)*int(unsafe.Sizeof(data[0])))) + + v, _, err := setClipboardData.Call(cFmtDIBV5, hMem) + if v == 0 { + gFree.Call(hMem) + return fmt.Errorf("failed to set text to clipboard: %w", err) + } + + return nil +} + +func read(t Format) (buf []byte, err error) { + // On Windows, OpenClipboard and CloseClipboard must be executed on + // the same thread. Thus, lock the OS thread for further execution. + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var format uintptr + switch t { + case FmtImage: + format = cFmtDIBV5 + case FmtText: + fallthrough + default: + format = cFmtUnicodeText + } + + // check if clipboard is avaliable for the requested format + r, _, err := isClipboardFormatAvailable.Call(format) + if r == 0 { + return nil, errUnavailable + } + + // try again until open clipboard successed + for { + r, _, _ = openClipboard.Call() + if r == 0 { + continue + } + break + } + defer closeClipboard.Call() + + switch format { + case cFmtDIBV5: + return readImage() + case cFmtUnicodeText: + fallthrough + default: + return readText() + } +} + +// write writes the given data to clipboard and +// returns true if success or false if failed. +func write(t Format, buf []byte) (<-chan struct{}, error) { + errch := make(chan error) + changed := make(chan struct{}, 1) + go func() { + // make sure GetClipboardSequenceNumber happens with + // OpenClipboard on the same thread. + runtime.LockOSThread() + defer runtime.UnlockOSThread() + for { + r, _, _ := openClipboard.Call(0) + if r == 0 { + continue + } + break + } + + // var param uintptr + switch t { + case FmtImage: + err := writeImage(buf) + if err != nil { + errch <- err + closeClipboard.Call() + return + } + case FmtText: + fallthrough + default: + // param = cFmtUnicodeText + err := writeText(buf) + if err != nil { + errch <- err + closeClipboard.Call() + return + } + } + // Close the clipboard otherwise other applications cannot + // paste the data. + closeClipboard.Call() + + cnt, _, _ := getClipboardSequenceNumber.Call() + errch <- nil + for { + time.Sleep(time.Second) + cur, _, _ := getClipboardSequenceNumber.Call() + if cur != cnt { + changed <- struct{}{} + close(changed) + return + } + } + }() + err := <-errch + if err != nil { + return nil, err + } + return changed, nil +} + +func watch(ctx context.Context, t Format) <-chan []byte { + recv := make(chan []byte, 1) + ready := make(chan struct{}) + go func() { + // not sure if we are too slow or the user too fast :) + ti := time.NewTicker(time.Second) + cnt, _, _ := getClipboardSequenceNumber.Call() + ready <- struct{}{} + for { + select { + case <-ctx.Done(): + close(recv) + return + case <-ti.C: + cur, _, _ := getClipboardSequenceNumber.Call() + if cnt != cur { + b := Read(t) + if b == nil { + continue + } + recv <- b + cnt = cur + } + } + } + }() + <-ready + return recv +} + +const ( + cFmtBitmap = 2 // Win+PrintScreen + cFmtUnicodeText = 13 + cFmtDIBV5 = 17 + // Screenshot taken from special shortcut is in different format (why??), see: + // https://jpsoft.com/forums/threads/detecting-clipboard-format.5225/ + cFmtDataObject = 49161 // Shift+Win+s, returned from enumClipboardFormats + gmemMoveable = 0x0002 +) + +// BITMAPV5Header structure, see: +// https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapv5header +type bitmapV5Header struct { + Size uint32 + Width int32 + Height int32 + Planes uint16 + BitCount uint16 + Compression uint32 + SizeImage uint32 + XPelsPerMeter int32 + YPelsPerMeter int32 + ClrUsed uint32 + ClrImportant uint32 + RedMask uint32 + GreenMask uint32 + BlueMask uint32 + AlphaMask uint32 + CSType uint32 + Endpoints struct { + CiexyzRed, CiexyzGreen, CiexyzBlue struct { + CiexyzX, CiexyzY, CiexyzZ int32 // FXPT2DOT30 + } + } + GammaRed uint32 + GammaGreen uint32 + GammaBlue uint32 + Intent uint32 + ProfileData uint32 + ProfileSize uint32 + Reserved uint32 +} + +type bitmapHeader struct { + Size uint32 + Width uint32 + Height uint32 + PLanes uint16 + BitCount uint16 + Compression uint32 + SizeImage uint32 + XPelsPerMeter uint32 + YPelsPerMeter uint32 + ClrUsed uint32 + ClrImportant uint32 +} + +// Calling a Windows DLL, see: +// https://github.com/golang/go/wiki/WindowsDLLs +var ( + user32 = syscall.MustLoadDLL("user32") + // Opens the clipboard for examination and prevents other + // applications from modifying the clipboard content. + // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-openclipboard + openClipboard = user32.MustFindProc("OpenClipboard") + // Closes the clipboard. + // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-closeclipboard + closeClipboard = user32.MustFindProc("CloseClipboard") + // Empties the clipboard and frees handles to data in the clipboard. + // The function then assigns ownership of the clipboard to the + // window that currently has the clipboard open. + // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-emptyclipboard + emptyClipboard = user32.MustFindProc("EmptyClipboard") + // Retrieves data from the clipboard in a specified format. + // The clipboard must have been opened previously. + // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getclipboarddata + getClipboardData = user32.MustFindProc("GetClipboardData") + // Places data on the clipboard in a specified clipboard format. + // The window must be the current clipboard owner, and the + // application must have called the OpenClipboard function. (When + // responding to the WM_RENDERFORMAT message, the clipboard owner + // must not call OpenClipboard before calling SetClipboardData.) + // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setclipboarddata + setClipboardData = user32.MustFindProc("SetClipboardData") + // Determines whether the clipboard contains data in the specified format. + // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-isclipboardformatavailable + isClipboardFormatAvailable = user32.MustFindProc("IsClipboardFormatAvailable") + // Clipboard data formats are stored in an ordered list. To perform + // an enumeration of clipboard data formats, you make a series of + // calls to the EnumClipboardFormats function. For each call, the + // format parameter specifies an available clipboard format, and the + // function returns the next available clipboard format. + // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-isclipboardformatavailable + enumClipboardFormats = user32.MustFindProc("EnumClipboardFormats") + // Retrieves the clipboard sequence number for the current window station. + // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getclipboardsequencenumber + getClipboardSequenceNumber = user32.MustFindProc("GetClipboardSequenceNumber") + // Registers a new clipboard format. This format can then be used as + // a valid clipboard format. + // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerclipboardformata + registerClipboardFormatA = user32.MustFindProc("RegisterClipboardFormatA") + + kernel32 = syscall.NewLazyDLL("kernel32") + + // Locks a global memory object and returns a pointer to the first + // byte of the object's memory block. + // https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globallock + gLock = kernel32.NewProc("GlobalLock") + // Decrements the lock count associated with a memory object that was + // allocated with GMEM_MOVEABLE. This function has no effect on memory + // objects allocated with GMEM_FIXED. + // https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalunlock + gUnlock = kernel32.NewProc("GlobalUnlock") + // Allocates the specified number of bytes from the heap. + // https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalalloc + gAlloc = kernel32.NewProc("GlobalAlloc") + // Frees the specified global memory object and invalidates its handle. + // https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalfree + gFree = kernel32.NewProc("GlobalFree") + memMove = kernel32.NewProc("RtlMoveMemory") +) diff --git a/packages/tui/internal/commands/command.go b/packages/tui/internal/commands/command.go index 9c4da12e..5a981549 100644 --- a/packages/tui/internal/commands/command.go +++ b/packages/tui/internal/commands/command.go @@ -29,7 +29,7 @@ type Command struct { Name CommandName Description string Keybindings []Keybinding - Trigger string + Trigger []string } func (c Command) Keys() []string { @@ -40,6 +40,21 @@ func (c Command) Keys() []string { return keys } +func (c Command) HasTrigger() bool { + return len(c.Trigger) > 0 +} + +func (c Command) PrimaryTrigger() string { + if len(c.Trigger) > 0 { + return c.Trigger[0] + } + return "" +} + +func (c Command) MatchesTrigger(trigger string) bool { + return slices.Contains(c.Trigger, trigger) +} + type CommandRegistry map[CommandName]Command func (r CommandRegistry) Sorted() []Command { @@ -71,6 +86,8 @@ func (r CommandRegistry) Matches(msg tea.KeyPressMsg, leader bool) []Command { const ( AppHelpCommand CommandName = "app_help" + SwitchModeCommand CommandName = "switch_mode" + SwitchModeReverseCommand CommandName = "switch_mode_reverse" EditorOpenCommand CommandName = "editor_open" SessionNewCommand CommandName = "session_new" SessionListCommand CommandName = "session_list" @@ -78,6 +95,7 @@ const ( SessionUnshareCommand CommandName = "session_unshare" SessionInterruptCommand CommandName = "session_interrupt" SessionCompactCommand CommandName = "session_compact" + SessionExportCommand CommandName = "session_export" ToolDetailsCommand CommandName = "tool_details" ModelListCommand CommandName = "model_list" ThemeListCommand CommandName = "theme_list" @@ -135,37 +153,53 @@ func LoadFromConfig(config *opencode.Config) CommandRegistry { Name: AppHelpCommand, Description: "show help", Keybindings: parseBindings("h"), - Trigger: "help", + Trigger: []string{"help"}, + }, + { + Name: SwitchModeCommand, + Description: "next mode", + Keybindings: parseBindings("tab"), + }, + { + Name: SwitchModeReverseCommand, + Description: "previous mode", + Keybindings: parseBindings("shift+tab"), }, { Name: EditorOpenCommand, Description: "open editor", Keybindings: parseBindings("e"), - Trigger: "editor", + Trigger: []string{"editor"}, + }, + { + Name: SessionExportCommand, + Description: "export conversation", + Keybindings: parseBindings("x"), + Trigger: []string{"export"}, }, { Name: SessionNewCommand, Description: "new session", Keybindings: parseBindings("n"), - Trigger: "new", + Trigger: []string{"new", "clear"}, }, { Name: SessionListCommand, Description: "list sessions", Keybindings: parseBindings("l"), - Trigger: "sessions", + Trigger: []string{"sessions", "resume", "continue"}, }, { Name: SessionShareCommand, Description: "share session", Keybindings: parseBindings("s"), - Trigger: "share", + Trigger: []string{"share"}, }, { Name: SessionUnshareCommand, Description: "unshare session", Keybindings: parseBindings("u"), - Trigger: "unshare", + Trigger: []string{"unshare"}, }, { Name: SessionInterruptCommand, @@ -176,32 +210,32 @@ func LoadFromConfig(config *opencode.Config) CommandRegistry { Name: SessionCompactCommand, Description: "compact the session", Keybindings: parseBindings("c"), - Trigger: "compact", + Trigger: []string{"compact", "summarize"}, }, { Name: ToolDetailsCommand, Description: "toggle tool details", Keybindings: parseBindings("d"), - Trigger: "details", + Trigger: []string{"details"}, }, { Name: ModelListCommand, Description: "list models", Keybindings: parseBindings("m"), - Trigger: "models", + Trigger: []string{"models"}, }, { Name: ThemeListCommand, Description: "list themes", Keybindings: parseBindings("t"), - Trigger: "themes", - }, - { - Name: FileListCommand, - Description: "list files", - Keybindings: parseBindings("f"), - Trigger: "files", + Trigger: []string{"themes"}, }, + // { + // Name: FileListCommand, + // Description: "list files", + // Keybindings: parseBindings("f"), + // Trigger: []string{"files"}, + // }, { Name: FileCloseCommand, Description: "close file", @@ -221,7 +255,7 @@ func LoadFromConfig(config *opencode.Config) CommandRegistry { Name: ProjectInitCommand, Description: "create/update AGENTS.md", Keybindings: parseBindings("i"), - Trigger: "init", + Trigger: []string{"init"}, }, { Name: InputClearCommand, @@ -231,7 +265,7 @@ func LoadFromConfig(config *opencode.Config) CommandRegistry { { Name: InputPasteCommand, Description: "paste content", - Keybindings: parseBindings("ctrl+v"), + Keybindings: parseBindings("ctrl+v", "super+v"), }, { Name: InputSubmitCommand, @@ -302,7 +336,7 @@ func LoadFromConfig(config *opencode.Config) CommandRegistry { Name: AppExitCommand, Description: "exit the app", Keybindings: parseBindings("ctrl+c", "q"), - Trigger: "exit", + Trigger: []string{"exit", "quit", "q"}, }, } registry := make(CommandRegistry) @@ -310,6 +344,10 @@ func LoadFromConfig(config *opencode.Config) CommandRegistry { marshalled, _ := json.Marshal(config.Keybinds) json.Unmarshal(marshalled, &keybinds) for _, command := range defaults { + // Remove share/unshare commands if sharing is disabled + if config.Share == opencode.ConfigShareDisabled && (command.Name == SessionShareCommand || command.Name == SessionUnshareCommand) { + continue + } if keybind, ok := keybinds[string(command.Name)]; ok && keybind != "" { command.Keybindings = parseBindings(keybind) } diff --git a/packages/tui/internal/completions/commands.go b/packages/tui/internal/completions/commands.go index c73923e8..2ffe3ea9 100644 --- a/packages/tui/internal/completions/commands.go +++ b/packages/tui/internal/completions/commands.go @@ -8,7 +8,6 @@ import ( "github.com/lithammer/fuzzysearch/fuzzy" "github.com/sst/opencode/internal/app" "github.com/sst/opencode/internal/commands" - "github.com/sst/opencode/internal/components/dialog" "github.com/sst/opencode/internal/styles" "github.com/sst/opencode/internal/theme" ) @@ -17,7 +16,7 @@ type CommandCompletionProvider struct { app *app.App } -func NewCommandCompletionProvider(app *app.App) dialog.CompletionProvider { +func NewCommandCompletionProvider(app *app.App) CompletionProvider { return &CommandCompletionProvider{app: app} } @@ -29,24 +28,37 @@ func (c *CommandCompletionProvider) GetEmptyMessage() string { return "no matching commands" } -func getCommandCompletionItem(cmd commands.Command, space int, t theme.Theme) dialog.CompletionItemI { - spacer := strings.Repeat(" ", space) - title := " /" + cmd.Trigger + styles.NewStyle().Foreground(t.TextMuted()).Render(spacer+cmd.Description) +func (c *CommandCompletionProvider) getCommandCompletionItem( + cmd commands.Command, + space int, +) CompletionSuggestion { + displayFunc := func(s styles.Style) string { + t := theme.CurrentTheme() + spacer := strings.Repeat(" ", space) + display := " /" + cmd.PrimaryTrigger() + s. + Foreground(t.TextMuted()). + Render(spacer+cmd.Description) + return display + } + value := string(cmd.Name) - return dialog.NewCompletionItem(dialog.CompletionItem{ - Title: title, - Value: value, - }) + return CompletionSuggestion{ + Display: displayFunc, + Value: value, + ProviderID: c.GetId(), + RawData: cmd, + } } -func (c *CommandCompletionProvider) GetChildEntries(query string) ([]dialog.CompletionItemI, error) { - t := theme.CurrentTheme() +func (c *CommandCompletionProvider) GetChildEntries( + query string, +) ([]CompletionSuggestion, error) { commands := c.app.Commands space := 1 for _, cmd := range c.app.Commands { - if lipgloss.Width(cmd.Trigger) > space { - space = lipgloss.Width(cmd.Trigger) + if cmd.HasTrigger() && lipgloss.Width(cmd.PrimaryTrigger()) > space { + space = lipgloss.Width(cmd.PrimaryTrigger()) } } space += 2 @@ -54,41 +66,44 @@ func (c *CommandCompletionProvider) GetChildEntries(query string) ([]dialog.Comp sorted := commands.Sorted() if query == "" { // If no query, return all commands - items := []dialog.CompletionItemI{} + items := []CompletionSuggestion{} for _, cmd := range sorted { - if cmd.Trigger == "" { + if !cmd.HasTrigger() { continue } - space := space - lipgloss.Width(cmd.Trigger) - items = append(items, getCommandCompletionItem(cmd, space, t)) + space := space - lipgloss.Width(cmd.PrimaryTrigger()) + items = append(items, c.getCommandCompletionItem(cmd, space)) } return items, nil } - // Use fuzzy matching for commands var commandNames []string - commandMap := make(map[string]dialog.CompletionItemI) + commandMap := make(map[string]CompletionSuggestion) for _, cmd := range sorted { - if cmd.Trigger == "" { + if !cmd.HasTrigger() { continue } - space := space - lipgloss.Width(cmd.Trigger) - commandNames = append(commandNames, cmd.Trigger) - commandMap[cmd.Trigger] = getCommandCompletionItem(cmd, space, t) + space := space - lipgloss.Width(cmd.PrimaryTrigger()) + for _, trigger := range cmd.Trigger { + commandNames = append(commandNames, trigger) + commandMap[trigger] = c.getCommandCompletionItem(cmd, space) + } } - // Find fuzzy matches - matches := fuzzy.RankFind(query, commandNames) - - // Sort by score (best matches first) + matches := fuzzy.RankFindFold(query, commandNames) sort.Sort(matches) - // Convert matches to completion items - items := []dialog.CompletionItemI{} + // Convert matches to completion items, deduplicating by command name + items := []CompletionSuggestion{} + seen := make(map[string]bool) for _, match := range matches { if item, ok := commandMap[match.Target]; ok { - items = append(items, item) + // Use the command's value (name) as the deduplication key + if !seen[item.Value] { + seen[item.Value] = true + items = append(items, item) + } } } return items, nil diff --git a/packages/tui/internal/completions/files-folders.go b/packages/tui/internal/completions/files.go similarity index 50% rename from packages/tui/internal/completions/files-folders.go rename to packages/tui/internal/completions/files.go index ec298af9..bece89a8 100644 --- a/packages/tui/internal/completions/files-folders.go +++ b/packages/tui/internal/completions/files.go @@ -9,30 +9,25 @@ import ( "github.com/sst/opencode-sdk-go" "github.com/sst/opencode/internal/app" - "github.com/sst/opencode/internal/components/dialog" "github.com/sst/opencode/internal/styles" "github.com/sst/opencode/internal/theme" ) -type filesAndFoldersContextGroup struct { +type filesContextGroup struct { app *app.App - gitFiles []dialog.CompletionItemI + gitFiles []CompletionSuggestion } -func (cg *filesAndFoldersContextGroup) GetId() string { +func (cg *filesContextGroup) GetId() string { return "files" } -func (cg *filesAndFoldersContextGroup) GetEmptyMessage() string { +func (cg *filesContextGroup) GetEmptyMessage() string { return "no matching files" } -func (cg *filesAndFoldersContextGroup) getGitFiles() []dialog.CompletionItemI { - t := theme.CurrentTheme() - items := make([]dialog.CompletionItemI, 0) - base := styles.NewStyle().Background(t.BackgroundElement()) - green := base.Foreground(t.Success()).Render - red := base.Foreground(t.Error()).Render +func (cg *filesContextGroup) getGitFiles() []CompletionSuggestion { + items := make([]CompletionSuggestion, 0) status, _ := cg.app.Client.File.Status(context.Background()) if status != nil { @@ -42,17 +37,25 @@ func (cg *filesAndFoldersContextGroup) getGitFiles() []dialog.CompletionItemI { }) for _, file := range files { - title := file.File - if file.Added > 0 { - title += green(" +" + strconv.Itoa(int(file.Added))) + displayFunc := func(s styles.Style) string { + t := theme.CurrentTheme() + green := s.Foreground(t.Success()).Render + red := s.Foreground(t.Error()).Render + display := file.Path + if file.Added > 0 { + display += green(" +" + strconv.Itoa(int(file.Added))) + } + if file.Removed > 0 { + display += red(" -" + strconv.Itoa(int(file.Removed))) + } + return display } - if file.Removed > 0 { - title += red(" -" + strconv.Itoa(int(file.Removed))) + item := CompletionSuggestion{ + Display: displayFunc, + Value: file.Path, + ProviderID: cg.GetId(), + RawData: file, } - item := dialog.NewCompletionItem(dialog.CompletionItem{ - Title: title, - Value: file.File, - }) items = append(items, item) } } @@ -60,10 +63,10 @@ func (cg *filesAndFoldersContextGroup) getGitFiles() []dialog.CompletionItemI { return items } -func (cg *filesAndFoldersContextGroup) GetChildEntries( +func (cg *filesContextGroup) GetChildEntries( query string, -) ([]dialog.CompletionItemI, error) { - items := make([]dialog.CompletionItemI, 0) +) ([]CompletionSuggestion, error) { + items := make([]CompletionSuggestion, 0) query = strings.TrimSpace(query) if query == "" { @@ -85,7 +88,7 @@ func (cg *filesAndFoldersContextGroup) GetChildEntries( for _, file := range *files { exists := false for _, existing := range cg.gitFiles { - if existing.GetValue() == file { + if existing.Value == file { if query != "" { items = append(items, existing) } @@ -93,10 +96,18 @@ func (cg *filesAndFoldersContextGroup) GetChildEntries( } } if !exists { - item := dialog.NewCompletionItem(dialog.CompletionItem{ - Title: file, - Value: file, - }) + displayFunc := func(s styles.Style) string { + // t := theme.CurrentTheme() + // return s.Foreground(t.Text()).Render(file) + return s.Render(file) + } + + item := CompletionSuggestion{ + Display: displayFunc, + Value: file, + ProviderID: cg.GetId(), + RawData: file, + } items = append(items, item) } } @@ -104,8 +115,8 @@ func (cg *filesAndFoldersContextGroup) GetChildEntries( return items, nil } -func NewFileAndFolderContextGroup(app *app.App) dialog.CompletionProvider { - cg := &filesAndFoldersContextGroup{ +func NewFileContextGroup(app *app.App) CompletionProvider { + cg := &filesContextGroup{ app: app, } go func() { diff --git a/packages/tui/internal/completions/provider.go b/packages/tui/internal/completions/provider.go new file mode 100644 index 00000000..dc11522c --- /dev/null +++ b/packages/tui/internal/completions/provider.go @@ -0,0 +1,8 @@ +package completions + +// CompletionProvider defines the interface for completion data providers +type CompletionProvider interface { + GetId() string + GetChildEntries(query string) ([]CompletionSuggestion, error) + GetEmptyMessage() string +} diff --git a/packages/tui/internal/completions/suggestion.go b/packages/tui/internal/completions/suggestion.go new file mode 100644 index 00000000..fac6b681 --- /dev/null +++ b/packages/tui/internal/completions/suggestion.go @@ -0,0 +1,24 @@ +package completions + +import "github.com/sst/opencode/internal/styles" + +// CompletionSuggestion represents a data-only completion suggestion +// with no styling or rendering logic +type CompletionSuggestion struct { + // The text to be displayed in the list. May contain minimal inline + // ANSI styling if intrinsic to the data (e.g., git diff colors). + Display func(styles.Style) string + + // The value to be used when the item is selected (e.g., inserted into the editor). + Value string + + // An optional, longer description to be displayed. + Description string + + // The ID of the provider that generated this suggestion. + ProviderID string + + // The raw, underlying data object (e.g., opencode.Symbol, commands.Command). + // This allows the selection handler to perform rich actions. + RawData any +} diff --git a/packages/tui/internal/completions/symbols.go b/packages/tui/internal/completions/symbols.go new file mode 100644 index 00000000..725e2e69 --- /dev/null +++ b/packages/tui/internal/completions/symbols.go @@ -0,0 +1,119 @@ +package completions + +import ( + "context" + "fmt" + "log/slog" + "strings" + + "github.com/sst/opencode-sdk-go" + "github.com/sst/opencode/internal/app" + "github.com/sst/opencode/internal/styles" + "github.com/sst/opencode/internal/theme" +) + +type symbolsContextGroup struct { + app *app.App +} + +func (cg *symbolsContextGroup) GetId() string { + return "symbols" +} + +func (cg *symbolsContextGroup) GetEmptyMessage() string { + return "no matching symbols" +} + +type SymbolKind int + +const ( + SymbolKindFile SymbolKind = 1 + SymbolKindModule SymbolKind = 2 + SymbolKindNamespace SymbolKind = 3 + SymbolKindPackage SymbolKind = 4 + SymbolKindClass SymbolKind = 5 + SymbolKindMethod SymbolKind = 6 + SymbolKindProperty SymbolKind = 7 + SymbolKindField SymbolKind = 8 + SymbolKindConstructor SymbolKind = 9 + SymbolKindEnum SymbolKind = 10 + SymbolKindInterface SymbolKind = 11 + SymbolKindFunction SymbolKind = 12 + SymbolKindVariable SymbolKind = 13 + SymbolKindConstant SymbolKind = 14 + SymbolKindString SymbolKind = 15 + SymbolKindNumber SymbolKind = 16 + SymbolKindBoolean SymbolKind = 17 + SymbolKindArray SymbolKind = 18 + SymbolKindObject SymbolKind = 19 + SymbolKindKey SymbolKind = 20 + SymbolKindNull SymbolKind = 21 + SymbolKindEnumMember SymbolKind = 22 + SymbolKindStruct SymbolKind = 23 + SymbolKindEvent SymbolKind = 24 + SymbolKindOperator SymbolKind = 25 + SymbolKindTypeParameter SymbolKind = 26 +) + +func (cg *symbolsContextGroup) GetChildEntries( + query string, +) ([]CompletionSuggestion, error) { + items := make([]CompletionSuggestion, 0) + + query = strings.TrimSpace(query) + if query == "" { + return items, nil + } + + symbols, err := cg.app.Client.Find.Symbols( + context.Background(), + opencode.FindSymbolsParams{Query: opencode.F(query)}, + ) + if err != nil { + slog.Error("Failed to get symbol completion items", "error", err) + return items, err + } + if symbols == nil { + return items, nil + } + + for _, sym := range *symbols { + parts := strings.Split(sym.Name, ".") + lastPart := parts[len(parts)-1] + start := int(sym.Location.Range.Start.Line) + end := int(sym.Location.Range.End.Line) + + displayFunc := func(s styles.Style) string { + t := theme.CurrentTheme() + base := s.Foreground(t.Text()).Render + muted := s.Foreground(t.TextMuted()).Render + display := base(lastPart) + + uriParts := strings.Split(sym.Location.Uri, "/") + lastTwoParts := uriParts[len(uriParts)-2:] + joined := strings.Join(lastTwoParts, "/") + display += muted(fmt.Sprintf(" %s", joined)) + + display += muted(fmt.Sprintf(":L%d-%d", start, end)) + return display + } + + value := fmt.Sprintf("%s?start=%d&end=%d", sym.Location.Uri, start, end) + + item := CompletionSuggestion{ + Display: displayFunc, + Value: value, + ProviderID: cg.GetId(), + RawData: sym, + } + items = append(items, item) + } + + return items, nil +} + +func NewSymbolsContextGroup(app *app.App) CompletionProvider { + return &symbolsContextGroup{ + app: app, + } +} diff --git a/packages/tui/internal/components/chat/cache.go b/packages/tui/internal/components/chat/cache.go index 1586c2cc..454f1a5a 100644 --- a/packages/tui/internal/components/chat/cache.go +++ b/packages/tui/internal/components/chat/cache.go @@ -1,28 +1,28 @@ package chat import ( - "crypto/sha256" "encoding/hex" "fmt" + "hash/fnv" "sync" ) -// MessageCache caches rendered messages to avoid re-rendering -type MessageCache struct { +// PartCache caches rendered messages to avoid re-rendering +type PartCache struct { mu sync.RWMutex cache map[string]string } -// NewMessageCache creates a new message cache -func NewMessageCache() *MessageCache { - return &MessageCache{ +// NewPartCache creates a new message cache +func NewPartCache() *PartCache { + return &PartCache{ cache: make(map[string]string), } } // generateKey creates a unique key for a message based on its content and rendering parameters -func (c *MessageCache) GenerateKey(params ...any) string { - h := sha256.New() +func (c *PartCache) GenerateKey(params ...any) string { + h := fnv.New64a() for _, param := range params { h.Write(fmt.Appendf(nil, ":%v", param)) } @@ -30,7 +30,7 @@ func (c *MessageCache) GenerateKey(params ...any) string { } // Get retrieves a cached rendered message -func (c *MessageCache) Get(key string) (string, bool) { +func (c *PartCache) Get(key string) (string, bool) { c.mu.RLock() defer c.mu.RUnlock() @@ -39,14 +39,14 @@ func (c *MessageCache) Get(key string) (string, bool) { } // Set stores a rendered message in the cache -func (c *MessageCache) Set(key string, content string) { +func (c *PartCache) Set(key string, content string) { c.mu.Lock() defer c.mu.Unlock() c.cache[key] = content } // Clear removes all entries from the cache -func (c *MessageCache) Clear() { +func (c *PartCache) Clear() { c.mu.Lock() defer c.mu.Unlock() @@ -54,7 +54,7 @@ func (c *MessageCache) Clear() { } // Size returns the number of cached entries -func (c *MessageCache) Size() int { +func (c *PartCache) Size() int { c.mu.RLock() defer c.mu.RUnlock() diff --git a/packages/tui/internal/components/chat/editor.go b/packages/tui/internal/components/chat/editor.go index 9466d541..3db6fa80 100644 --- a/packages/tui/internal/components/chat/editor.go +++ b/packages/tui/internal/components/chat/editor.go @@ -1,9 +1,13 @@ package chat import ( + "encoding/base64" "fmt" "log/slog" + "net/url" + "os" "path/filepath" + "strconv" "strings" "github.com/charmbracelet/bubbles/v2/spinner" @@ -12,10 +16,10 @@ import ( "github.com/google/uuid" "github.com/sst/opencode-sdk-go" "github.com/sst/opencode/internal/app" + "github.com/sst/opencode/internal/clipboard" "github.com/sst/opencode/internal/commands" "github.com/sst/opencode/internal/components/dialog" "github.com/sst/opencode/internal/components/textarea" - "github.com/sst/opencode/internal/image" "github.com/sst/opencode/internal/styles" "github.com/sst/opencode/internal/theme" "github.com/sst/opencode/internal/util" @@ -23,10 +27,11 @@ import ( type EditorComponent interface { tea.Model - View(width int) string - Content(width int) string + tea.ViewModel + Content() string Lines() int Value() string + Length() int Focused() bool Focus() (tea.Model, tea.Cmd) Blur() @@ -34,14 +39,19 @@ type EditorComponent interface { Clear() (tea.Model, tea.Cmd) Paste() (tea.Model, tea.Cmd) Newline() (tea.Model, tea.Cmd) + SetValue(value string) + SetValueWithAttachments(value string) SetInterruptKeyInDebounce(inDebounce bool) + SetExitKeyInDebounce(inDebounce bool) } type editorComponent struct { app *app.App + width int textarea textarea.Model spinner spinner.Model interruptKeyInDebounce bool + exitKeyInDebounce bool } func (m *editorComponent) Init() tea.Cmd { @@ -53,6 +63,12 @@ func (m *editorComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd switch msg := msg.(type) { + case tea.WindowSizeMsg: + m.width = min(msg.Width-4, app.MAX_CONTAINER_WIDTH) + if m.app.Config.Layout == opencode.LayoutConfigStretch { + m.width = msg.Width - 4 + } + return m, nil case spinner.TickMsg: m.spinner, cmd = m.spinner.Update(msg) return m, cmd @@ -63,14 +79,42 @@ func (m *editorComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, cmd) return m, tea.Batch(cmds...) } + case tea.PasteMsg: + text := string(msg) + text = strings.ReplaceAll(text, "\\", "") + text, err := strconv.Unquote(`"` + text + `"`) + if err != nil { + slog.Error("Failed to unquote text", "error", err) + m.textarea.InsertRunesFromUserInput([]rune(msg)) + return m, nil + } + if _, err := os.Stat(text); err != nil { + slog.Error("Failed to paste file", "error", err) + m.textarea.InsertRunesFromUserInput([]rune(msg)) + return m, nil + } + + filePath := text + + attachment := m.createAttachmentFromFile(filePath) + if attachment == nil { + m.textarea.InsertRunesFromUserInput([]rune(msg)) + return m, nil + } + + m.textarea.InsertAttachment(attachment) + m.textarea.InsertString(" ") + case tea.ClipboardMsg: + text := string(msg) + m.textarea.InsertRunesFromUserInput([]rune(text)) case dialog.ThemeSelectedMsg: - m.textarea = createTextArea(&m.textarea) + m.textarea = updateTextareaStyles(m.textarea) m.spinner = createSpinner() return m, tea.Batch(m.spinner.Tick, m.textarea.Focus()) case dialog.CompletionSelectedMsg: - switch msg.ProviderID { + switch msg.Item.ProviderID { case "commands": - commandName := strings.TrimPrefix(msg.CompletionValue, "/") + commandName := strings.TrimPrefix(msg.Item.Value, "/") updated, cmd := m.Clear() m = updated.(*editorComponent) cmds = append(cmds, cmd) @@ -80,7 +124,7 @@ func (m *editorComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { atIndex := m.textarea.LastRuneIndex('@') if atIndex == -1 { // Should not happen, but as a fallback, just insert. - m.textarea.InsertString(msg.CompletionValue + " ") + m.textarea.InsertString(msg.Item.Value + " ") return m, nil } @@ -91,38 +135,37 @@ func (m *editorComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // Now, insert the attachment at the position where the '@' was. // The cursor is now at `atIndex` after the replacement. - filePath := msg.CompletionValue - extension := filepath.Ext(filePath) - mediaType := "" - switch extension { - case ".jpg": - mediaType = "image/jpeg" - case ".png", ".jpeg", ".gif", ".webp": - mediaType = "image/" + extension[1:] - case ".pdf": - mediaType = "application/pdf" - default: - mediaType = "text/plain" + filePath := msg.Item.Value + attachment := m.createAttachmentFromPath(filePath) + m.textarea.InsertAttachment(attachment) + m.textarea.InsertString(" ") + return m, nil + case "symbols": + atIndex := m.textarea.LastRuneIndex('@') + if atIndex == -1 { + // Should not happen, but as a fallback, just insert. + m.textarea.InsertString(msg.Item.Value + " ") + return m, nil } + + cursorCol := m.textarea.CursorColumn() + m.textarea.ReplaceRange(atIndex, cursorCol, "") + + symbol := msg.Item.RawData.(opencode.Symbol) + parts := strings.Split(symbol.Name, ".") + lastPart := parts[len(parts)-1] attachment := &textarea.Attachment{ ID: uuid.NewString(), - Display: "@" + filePath, - URL: fmt.Sprintf("file://./%s", filePath), - Filename: filePath, - MediaType: mediaType, + Display: "@" + lastPart, + URL: msg.Item.Value, + Filename: lastPart, + MediaType: "text/plain", } m.textarea.InsertAttachment(attachment) m.textarea.InsertString(" ") return m, nil default: - existingValue := m.textarea.Value() - lastSpaceIndex := strings.LastIndex(existingValue, " ") - if lastSpaceIndex == -1 { - m.textarea.SetValue(msg.CompletionValue + " ") - } else { - modifiedValue := existingValue[:lastSpaceIndex+1] + msg.CompletionValue - m.textarea.SetValue(modifiedValue + " ") - } + slog.Debug("Unknown provider", "provider", msg.Item.ProviderID) return m, nil } } @@ -136,7 +179,7 @@ func (m *editorComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Batch(cmds...) } -func (m *editorComponent) Content(width int) string { +func (m *editorComponent) Content() string { t := theme.CurrentTheme() base := styles.NewStyle().Foreground(t.Text()).Background(t.Background()).Render muted := styles.NewStyle().Foreground(t.TextMuted()).Background(t.Background()).Render @@ -145,26 +188,33 @@ func (m *editorComponent) Content(width int) string { Bold(true) prompt := promptStyle.Render(">") - m.textarea.SetWidth(width - 6) + m.textarea.SetWidth(m.width - 6) textarea := lipgloss.JoinHorizontal( lipgloss.Top, prompt, m.textarea.View(), ) + borderForeground := t.Border() + if m.app.IsLeaderSequence { + borderForeground = t.Accent() + } textarea = styles.NewStyle(). Background(t.BackgroundElement()). - Width(width). + Width(m.width). PaddingTop(1). PaddingBottom(1). BorderStyle(lipgloss.ThickBorder()). - BorderForeground(t.Border()). + BorderForeground(borderForeground). BorderBackground(t.Background()). BorderLeft(true). BorderRight(true). Render(textarea) hint := base(m.getSubmitKeyText()) + muted(" send ") - if m.app.IsBusy() { + if m.exitKeyInDebounce { + keyText := m.getExitKeyText() + hint = base(keyText+" again") + muted(" to exit") + } else if m.app.IsBusy() { keyText := m.getInterruptKeyText() if m.interruptKeyInDebounce { hint = muted( @@ -186,7 +236,7 @@ func (m *editorComponent) Content(width int) string { model = muted(m.app.Provider.Name) + base(" "+m.app.Model.Name) } - space := width - 2 - lipgloss.Width(model) - lipgloss.Width(hint) + space := m.width - 2 - lipgloss.Width(model) - lipgloss.Width(hint) spacer := styles.NewStyle().Background(t.Background()).Width(space).Render("") info := hint + spacer + model @@ -196,10 +246,10 @@ func (m *editorComponent) Content(width int) string { return content } -func (m *editorComponent) View(width int) string { +func (m *editorComponent) View() string { if m.Lines() > 1 { return lipgloss.Place( - width, + m.width, 5, lipgloss.Center, lipgloss.Center, @@ -207,7 +257,7 @@ func (m *editorComponent) View(width int) string { styles.WhitespaceStyle(theme.CurrentTheme().Background()), ) } - return m.Content(width) + return m.Content() } func (m *editorComponent) Focused() bool { @@ -230,14 +280,25 @@ func (m *editorComponent) Value() string { return m.textarea.Value() } +func (m *editorComponent) Length() int { + return m.textarea.Length() +} + func (m *editorComponent) Submit() (tea.Model, tea.Cmd) { value := strings.TrimSpace(m.Value()) if value == "" { return m, nil } + + switch value { + case "exit", "quit", "q", ":q": + return m, tea.Quit + } + if len(value) > 0 && value[len(value)-1] == '\\' { // If the last character is a backslash, remove it and add a newline - m.textarea.ReplaceRange(len(value)-1, len(value), "") + backslashCol := m.textarea.CurrentRowLength() - 1 + m.textarea.ReplaceRange(backslashCol, backslashCol+1, "") m.textarea.InsertString("\n") return m, nil } @@ -248,10 +309,10 @@ func (m *editorComponent) Submit() (tea.Model, tea.Cmd) { fileParts := make([]opencode.FilePartParam, 0) for _, attachment := range attachments { fileParts = append(fileParts, opencode.FilePartParam{ - Type: opencode.F(opencode.FilePartTypeFile), - MediaType: opencode.F(attachment.MediaType), - URL: opencode.F(attachment.URL), - Filename: opencode.F(attachment.Filename), + Type: opencode.F(opencode.FilePartTypeFile), + Mime: opencode.F(attachment.MediaType), + URL: opencode.F(attachment.URL), + Filename: opencode.F(attachment.Filename), }) } @@ -269,24 +330,31 @@ func (m *editorComponent) Clear() (tea.Model, tea.Cmd) { } func (m *editorComponent) Paste() (tea.Model, tea.Cmd) { - _, text, err := image.GetImageFromClipboard() - if err != nil { - slog.Error(err.Error()) + imageBytes := clipboard.Read(clipboard.FmtImage) + if imageBytes != nil { + attachmentCount := len(m.textarea.GetAttachments()) + attachmentIndex := attachmentCount + 1 + base64EncodedFile := base64.StdEncoding.EncodeToString(imageBytes) + attachment := &textarea.Attachment{ + ID: uuid.NewString(), + MediaType: "image/png", + Display: fmt.Sprintf("[Image #%d]", attachmentIndex), + Filename: fmt.Sprintf("image-%d.png", attachmentIndex), + URL: fmt.Sprintf("data:image/png;base64,%s", base64EncodedFile), + } + m.textarea.InsertAttachment(attachment) + m.textarea.InsertString(" ") return m, nil } - // if len(imageBytes) != 0 { - // attachmentName := fmt.Sprintf("clipboard-image-%d", len(m.attachments)) - // attachment := app.Attachment{ - // FilePath: attachmentName, - // FileName: attachmentName, - // Content: imageBytes, - // MimeType: "image/png", - // } - // m.attachments = append(m.attachments, attachment) - // } else { - m.textarea.InsertString(text) - // } - return m, nil + + textBytes := clipboard.Read(clipboard.FmtText) + if textBytes != nil { + m.textarea.InsertRunesFromUserInput([]rune(string(textBytes))) + return m, nil + } + + // fallback to reading the clipboard using OSC52 + return m, tea.ReadClipboard } func (m *editorComponent) Newline() (tea.Model, tea.Cmd) { @@ -298,6 +366,46 @@ func (m *editorComponent) SetInterruptKeyInDebounce(inDebounce bool) { m.interruptKeyInDebounce = inDebounce } +func (m *editorComponent) SetValue(value string) { + m.textarea.SetValue(value) +} + +func (m *editorComponent) SetValueWithAttachments(value string) { + m.textarea.Reset() + + i := 0 + for i < len(value) { + // Check if filepath and add attachment + if value[i] == '@' { + start := i + 1 + end := start + for end < len(value) && value[end] != ' ' && value[end] != '\t' && value[end] != '\n' && value[end] != '\r' { + end++ + } + + if end > start { + filePath := value[start:end] + if _, err := os.Stat(filePath); err == nil { + attachment := m.createAttachmentFromFile(filePath) + if attachment != nil { + m.textarea.InsertAttachment(attachment) + i = end + continue + } + } + } + } + + // Not a valid file path, insert the character normally + m.textarea.InsertRune(rune(value[i])) + i++ + } +} + +func (m *editorComponent) SetExitKeyInDebounce(inDebounce bool) { + m.exitKeyInDebounce = inDebounce +} + func (m *editorComponent) getInterruptKeyText() string { return m.app.Commands[commands.SessionInterruptCommand].Keys()[0] } @@ -306,14 +414,16 @@ func (m *editorComponent) getSubmitKeyText() string { return m.app.Commands[commands.InputSubmitCommand].Keys()[0] } -func createTextArea(existing *textarea.Model) textarea.Model { +func (m *editorComponent) getExitKeyText() string { + return m.app.Commands[commands.AppExitCommand].Keys()[0] +} + +func updateTextareaStyles(ta textarea.Model) textarea.Model { t := theme.CurrentTheme() bgColor := t.BackgroundElement() textColor := t.Text() textMutedColor := t.TextMuted() - ta := textarea.New() - ta.Styles.Blurred.Base = styles.NewStyle().Foreground(textColor).Background(bgColor).Lipgloss() ta.Styles.Blurred.CursorLine = styles.NewStyle().Background(bgColor).Lipgloss() ta.Styles.Blurred.Placeholder = styles.NewStyle(). @@ -337,17 +447,6 @@ func createTextArea(existing *textarea.Model) textarea.Model { Background(t.Secondary()). Lipgloss() ta.Styles.Cursor.Color = t.Primary() - - ta.Prompt = " " - ta.ShowLineNumbers = false - ta.CharLimit = -1 - - if existing != nil { - ta.SetValue(existing.Value()) - // ta.SetWidth(existing.Width()) - ta.SetHeight(existing.Height()) - } - return ta } @@ -367,12 +466,85 @@ func createSpinner() spinner.Model { func NewEditorComponent(app *app.App) EditorComponent { s := createSpinner() - ta := createTextArea(nil) - return &editorComponent{ + ta := textarea.New() + ta.Prompt = " " + ta.ShowLineNumbers = false + ta.CharLimit = -1 + ta = updateTextareaStyles(ta) + + m := &editorComponent{ app: app, textarea: ta, spinner: s, interruptKeyInDebounce: false, } + + return m +} + +func getMediaTypeFromExtension(ext string) string { + switch strings.ToLower(ext) { + case ".jpg": + return "image/jpeg" + case ".png", ".jpeg", ".gif", ".webp": + return "image/" + ext[1:] + case ".pdf": + return "application/pdf" + default: + return "text/plain" + } +} + +func (m *editorComponent) createAttachmentFromFile(filePath string) *textarea.Attachment { + ext := strings.ToLower(filepath.Ext(filePath)) + mediaType := getMediaTypeFromExtension(ext) + + // For text files, create a simple file reference + if mediaType == "text/plain" { + return &textarea.Attachment{ + ID: uuid.NewString(), + Display: "@" + filePath, + URL: fmt.Sprintf("file://./%s", filePath), + Filename: filePath, + MediaType: mediaType, + } + } + + // For binary files (images, PDFs), read and encode + fileBytes, err := os.ReadFile(filePath) + if err != nil { + slog.Error("Failed to read file", "error", err) + return nil + } + + base64EncodedFile := base64.StdEncoding.EncodeToString(fileBytes) + url := fmt.Sprintf("data:%s;base64,%s", mediaType, base64EncodedFile) + attachmentCount := len(m.textarea.GetAttachments()) + attachmentIndex := attachmentCount + 1 + label := "File" + if strings.HasPrefix(mediaType, "image/") { + label = "Image" + } + + return &textarea.Attachment{ + ID: uuid.NewString(), + MediaType: mediaType, + Display: fmt.Sprintf("[%s #%d]", label, attachmentIndex), + URL: url, + Filename: filePath, + } +} + +func (m *editorComponent) createAttachmentFromPath(filePath string) *textarea.Attachment { + extension := filepath.Ext(filePath) + mediaType := getMediaTypeFromExtension(extension) + + return &textarea.Attachment{ + ID: uuid.NewString(), + Display: "@" + filePath, + URL: fmt.Sprintf("file://./%s", url.PathEscape(filePath)), + Filename: filePath, + MediaType: mediaType, + } } diff --git a/packages/tui/internal/components/chat/message.go b/packages/tui/internal/components/chat/message.go index 9e245c8b..d3263b05 100644 --- a/packages/tui/internal/components/chat/message.go +++ b/packages/tui/internal/components/chat/message.go @@ -9,15 +9,14 @@ import ( "github.com/charmbracelet/lipgloss/v2" "github.com/charmbracelet/lipgloss/v2/compat" + "github.com/charmbracelet/x/ansi" + "github.com/muesli/reflow/truncate" "github.com/sst/opencode-sdk-go" "github.com/sst/opencode/internal/app" - "github.com/sst/opencode/internal/commands" "github.com/sst/opencode/internal/components/diff" - "github.com/sst/opencode/internal/layout" "github.com/sst/opencode/internal/styles" "github.com/sst/opencode/internal/theme" "github.com/sst/opencode/internal/util" - "github.com/tidwall/gjson" "golang.org/x/text/cases" "golang.org/x/text/language" ) @@ -110,7 +109,6 @@ func WithPaddingBottom(padding int) renderingOption { func renderContentBlock( app *app.App, content string, - highlight bool, width int, options ...renderingOption, ) string { @@ -135,7 +133,6 @@ func renderContentBlock( style := styles.NewStyle(). Foreground(renderer.textColor). Background(t.BackgroundPanel()). - Width(width). PaddingTop(renderer.paddingTop). PaddingBottom(renderer.paddingBottom). PaddingLeft(renderer.paddingLeft). @@ -160,18 +157,6 @@ func renderContentBlock( BorderRightBackground(t.Background()) } - if highlight { - style = style. - BorderLeftForeground(borderColor). - BorderRightForeground(borderColor) - } - } - - if highlight { - style = style. - Foreground(t.Text()). - Background(t.BackgroundElement()). - Bold(true) } content = style.Render(content) @@ -186,49 +171,45 @@ func renderContentBlock( } } - if highlight { - copy := app.Key(commands.MessagesCopyCommand) - // revert := app.Key(commands.MessagesRevertCommand) - - background := t.Background() - header := layout.Render( - layout.FlexOptions{ - Background: &background, - Direction: layout.Row, - Justify: layout.JustifyCenter, - Align: layout.AlignStretch, - Width: width - 2, - Gap: 5, - }, - layout.FlexItem{ - View: copy, - }, - // layout.FlexItem{ - // View: revert, - // }, - ) - header = styles.NewStyle().Background(t.Background()).Padding(0, 1).Render(header) - - content = "\n\n\n" + header + "\n\n" + content + "\n\n\n" - } - return content } func renderText( app *app.App, - message opencode.Message, + message opencode.MessageUnion, text string, author string, showToolDetails bool, - highlight bool, width int, extra string, - toolCalls ...opencode.ToolInvocationPart, + toolCalls ...opencode.ToolPart, ) string { t := theme.CurrentTheme() - timestamp := time.UnixMilli(int64(message.Metadata.Time.Created)). + var ts time.Time + backgroundColor := t.BackgroundPanel() + var content string + switch casted := message.(type) { + case opencode.AssistantMessage: + ts = time.UnixMilli(int64(casted.Time.Created)) + content = util.ToMarkdown(text, width, backgroundColor) + case opencode.UserMessage: + ts = time.UnixMilli(int64(casted.Time.Created)) + base := styles.NewStyle().Foreground(t.Text()).Background(backgroundColor) + words := strings.Fields(text) + for i, word := range words { + if strings.HasPrefix(word, "@") { + words[i] = base.Foreground(t.Secondary()).Render(word + " ") + } else { + words[i] = base.Render(word + " ") + } + } + text = strings.Join(words, "") + text = ansi.WordwrapWc(text, width-6, " -") + content = base.Width(width - 6).Render(text) + } + + timestamp := ts. Local(). Format("02 Jan 2006 03:04 PM") if time.Now().Format("02 Jan 2006") == timestamp[:11] { @@ -238,30 +219,12 @@ func renderText( info := fmt.Sprintf("%s (%s)", author, timestamp) info = styles.NewStyle().Foreground(t.TextMuted()).Render(info) - backgroundColor := t.BackgroundPanel() - if highlight { - backgroundColor = t.BackgroundElement() - } - messageStyle := styles.NewStyle().Background(backgroundColor) - if message.Role == opencode.MessageRoleUser { - messageStyle = messageStyle.Width(width - 6) - } - - content := messageStyle.Render(text) - if message.Role == opencode.MessageRoleAssistant { - content = util.ToMarkdown(text, width, backgroundColor) - } - if !showToolDetails && toolCalls != nil && len(toolCalls) > 0 { content = content + "\n\n" for _, toolCall := range toolCalls { - title := renderToolTitle(toolCall, message.Metadata, width) - metadata := opencode.MessageMetadataTool{} - if _, ok := message.Metadata.Tool[toolCall.ToolInvocation.ToolCallID]; ok { - metadata = message.Metadata.Tool[toolCall.ToolInvocation.ToolCallID] - } + title := renderToolTitle(toolCall, width) style := styles.NewStyle() - if _, ok := metadata.ExtraFields["error"]; ok { + if toolCall.State.Status == opencode.ToolPartStateStatusError { style = style.Foreground(t.Error()) } title = style.Render(title) @@ -276,21 +239,19 @@ func renderText( } content = strings.Join(sections, "\n") - switch message.Role { - case opencode.MessageRoleUser: + switch message.(type) { + case opencode.UserMessage: return renderContentBlock( app, content, - highlight, width, WithTextColor(t.Text()), WithBorderColorRight(t.Secondary()), ) - case opencode.MessageRoleAssistant: + case opencode.AssistantMessage: return renderContentBlock( app, content, - highlight, width, WithBorderColor(t.Accent()), ) @@ -300,39 +261,31 @@ func renderText( func renderToolDetails( app *app.App, - toolCall opencode.ToolInvocationPart, - messageMetadata opencode.MessageMetadata, - highlight bool, + toolCall opencode.ToolPart, width int, ) string { ignoredTools := []string{"todoread"} - if slices.Contains(ignoredTools, toolCall.ToolInvocation.ToolName) { + if slices.Contains(ignoredTools, toolCall.Tool) { return "" } - toolCallID := toolCall.ToolInvocation.ToolCallID - metadata := opencode.MessageMetadataTool{} - if _, ok := messageMetadata.Tool[toolCallID]; ok { - metadata = messageMetadata.Tool[toolCallID] + if toolCall.State.Status == opencode.ToolPartStateStatusPending { + title := renderToolTitle(toolCall, width) + return renderContentBlock(app, title, width) } var result *string - if toolCall.ToolInvocation.Result != "" { - result = &toolCall.ToolInvocation.Result + if toolCall.State.Output != "" { + result = &toolCall.State.Output } - if toolCall.ToolInvocation.State == "partial-call" { - title := renderToolTitle(toolCall, messageMetadata, width) - return renderContentBlock(app, title, highlight, width) - } - - toolArgsMap := make(map[string]any) - if toolCall.ToolInvocation.Args != nil { - value := toolCall.ToolInvocation.Args + toolInputMap := make(map[string]any) + if toolCall.State.Input != nil { + value := toolCall.State.Input if m, ok := value.(map[string]any); ok { - toolArgsMap = m - keys := make([]string, 0, len(toolArgsMap)) - for key := range toolArgsMap { + toolInputMap = m + keys := make([]string, 0, len(toolInputMap)) + for key := range toolInputMap { keys = append(keys, key) } slices.Sort(keys) @@ -340,150 +293,151 @@ func renderToolDetails( } body := "" - finished := result != nil && *result != "" t := theme.CurrentTheme() backgroundColor := t.BackgroundPanel() borderColor := t.BackgroundPanel() - if highlight { - backgroundColor = t.BackgroundElement() - borderColor = t.BorderActive() - } + defaultStyle := styles.NewStyle().Background(backgroundColor).Width(width - 6).Render - switch toolCall.ToolInvocation.ToolName { - case "read": - preview := metadata.ExtraFields["preview"] - if preview != nil && toolArgsMap["filePath"] != nil { - filename := toolArgsMap["filePath"].(string) - body = preview.(string) - body = util.RenderFile(filename, body, width, util.WithTruncate(6)) - } - case "edit": - if filename, ok := toolArgsMap["filePath"].(string); ok { - diffField := metadata.ExtraFields["diff"] - if diffField != nil { - patch := diffField.(string) - var formattedDiff string - formattedDiff, _ = diff.FormatUnifiedDiff( - filename, - patch, - diff.WithWidth(width-2), - ) - body = strings.TrimSpace(formattedDiff) - style := styles.NewStyle(). - Background(backgroundColor). - Foreground(t.TextMuted()). - Padding(1, 2). - Width(width - 4) - if highlight { - style = style.Foreground(t.Text()).Bold(true) - } - - if diagnostics := renderDiagnostics(metadata, filename); diagnostics != "" { - diagnostics = style.Render(diagnostics) - body += "\n" + diagnostics - } - - title := renderToolTitle(toolCall, messageMetadata, width) - title = style.Render(title) - content := title + "\n" + body - content = renderContentBlock( - app, - content, - highlight, - width, - WithPadding(0), - WithBorderColor(borderColor), - ) - return content + if toolCall.State.Metadata != nil { + metadata := toolCall.State.Metadata.(map[string]any) + switch toolCall.Tool { + case "read": + var preview any + if metadata != nil { + preview = metadata["preview"] } - } - case "write": - if filename, ok := toolArgsMap["filePath"].(string); ok { - if content, ok := toolArgsMap["content"].(string); ok { - body = util.RenderFile(filename, content, width) - if diagnostics := renderDiagnostics(metadata, filename); diagnostics != "" { - body += "\n\n" + diagnostics + if preview != nil && toolInputMap["filePath"] != nil { + filename := toolInputMap["filePath"].(string) + body = preview.(string) + body = util.RenderFile(filename, body, width, util.WithTruncate(6)) + } + case "edit": + if filename, ok := toolInputMap["filePath"].(string); ok { + var diffField any + if metadata != nil { + diffField = metadata["diff"] + } + if diffField != nil { + patch := diffField.(string) + var formattedDiff string + if width < 120 { + formattedDiff, _ = diff.FormatUnifiedDiff( + filename, + patch, + diff.WithWidth(width-2), + ) + } else { + formattedDiff, _ = diff.FormatDiff( + filename, + patch, + diff.WithWidth(width-2), + ) + } + body = strings.TrimSpace(formattedDiff) + style := styles.NewStyle(). + Background(backgroundColor). + Foreground(t.TextMuted()). + Padding(1, 2). + Width(width - 4) + + if diagnostics := renderDiagnostics(metadata, filename, backgroundColor, width-6); diagnostics != "" { + diagnostics = style.Render(diagnostics) + body += "\n" + diagnostics + } + + title := renderToolTitle(toolCall, width) + title = style.Render(title) + content := title + "\n" + body + content = renderContentBlock( + app, + content, + width, + WithPadding(0), + WithBorderColor(borderColor), + ) + return content } } - } - case "bash": - stdout := metadata.ExtraFields["stdout"] - if stdout != nil { - command := toolArgsMap["command"].(string) - body = fmt.Sprintf("```console\n> %s\n%s```", command, stdout) - body = util.ToMarkdown(body, width, backgroundColor) - } - case "webfetch": - if format, ok := toolArgsMap["format"].(string); ok && result != nil { - body = *result - body = util.TruncateHeight(body, 10) - if format == "html" || format == "markdown" { - body = util.ToMarkdown(body, width, backgroundColor) - } - } - case "todowrite": - todos := metadata.JSON.ExtraFields["todos"] - if !todos.IsNull() && finished { - strTodos := todos.Raw() - todos := gjson.Parse(strTodos) - for _, todo := range todos.Array() { - content := todo.Get("content").String() - switch todo.Get("status").String() { - case "completed": - body += fmt.Sprintf("- [x] %s\n", content) - // case "in-progress": - // body += fmt.Sprintf("- [ ] %s\n", content) - default: - body += fmt.Sprintf("- [ ] %s\n", content) - } - } - body = util.ToMarkdown(body, width, backgroundColor) - } - case "task": - summary := metadata.JSON.ExtraFields["summary"] - if !summary.IsNull() { - strValue := summary.Raw() - toolcalls := gjson.Parse(strValue).Array() - - steps := []string{} - for _, toolcall := range toolcalls { - call := toolcall.Value().(map[string]any) - if toolInvocation, ok := call["toolInvocation"].(map[string]any); ok { - data, _ := json.Marshal(toolInvocation) - var toolCall opencode.ToolInvocationPart - _ = json.Unmarshal(data, &toolCall) - - if metadata, ok := call["metadata"].(map[string]any); ok { - data, _ = json.Marshal(metadata) - var toolMetadata opencode.MessageMetadataTool - _ = json.Unmarshal(data, &toolMetadata) - - step := renderToolTitle(toolCall, messageMetadata, width) - step = "∟ " + step - steps = append(steps, step) + case "write": + if filename, ok := toolInputMap["filePath"].(string); ok { + if content, ok := toolInputMap["content"].(string); ok { + body = util.RenderFile(filename, content, width) + if diagnostics := renderDiagnostics(metadata, filename, backgroundColor, width-4); diagnostics != "" { + body += "\n\n" + diagnostics } } } - body = strings.Join(steps, "\n") + case "bash": + stdout := metadata["stdout"] + if stdout != nil { + command := toolInputMap["command"].(string) + body = fmt.Sprintf("```console\n> %s\n%s```", command, stdout) + body = util.ToMarkdown(body, width, backgroundColor) + } + case "webfetch": + if format, ok := toolInputMap["format"].(string); ok && result != nil { + body = *result + body = util.TruncateHeight(body, 10) + if format == "html" || format == "markdown" { + body = util.ToMarkdown(body, width, backgroundColor) + } + } + case "todowrite": + todos := metadata["todos"] + if todos != nil { + for _, item := range todos.([]any) { + todo := item.(map[string]any) + content := todo["content"].(string) + switch todo["status"] { + case "completed": + body += fmt.Sprintf("- [x] %s\n", content) + case "cancelled": + // strike through cancelled todo + body += fmt.Sprintf("- [~] ~~%s~~\n", content) + case "in_progress": + // highlight in progress todo + body += fmt.Sprintf("- [ ] `%s`\n", content) + default: + body += fmt.Sprintf("- [ ] %s\n", content) + } + } + body = util.ToMarkdown(body, width, backgroundColor) + } + case "task": + summary := metadata["summary"] + if summary != nil { + toolcalls := summary.([]any) + steps := []string{} + for _, item := range toolcalls { + data, _ := json.Marshal(item) + var toolCall opencode.ToolPart + _ = json.Unmarshal(data, &toolCall) + step := renderToolTitle(toolCall, width) + step = "∟ " + step + steps = append(steps, step) + } + body = strings.Join(steps, "\n") + } + body = defaultStyle(body) + default: + if result == nil { + empty := "" + result = &empty + } + body = *result + body = util.TruncateHeight(body, 10) + body = defaultStyle(body) } - default: - if result == nil { - empty := "" - result = &empty - } - body = *result - body = util.TruncateHeight(body, 10) } error := "" - if err, ok := metadata.ExtraFields["error"].(bool); ok && err { - if message, ok := metadata.ExtraFields["message"].(string); ok { - error = message - } + if toolCall.State.Status == opencode.ToolPartStateStatusError { + error = toolCall.State.Error } if error != "" { body = styles.NewStyle(). + Width(width - 6). Foreground(t.Error()). Background(backgroundColor). Render(error) @@ -492,19 +446,22 @@ func renderToolDetails( if body == "" && error == "" && result != nil { body = *result body = util.TruncateHeight(body, 10) + body = defaultStyle(body) } - title := renderToolTitle(toolCall, messageMetadata, width) + if body == "" { + body = defaultStyle("") + } + + title := renderToolTitle(toolCall, width) content := title + "\n\n" + body - return renderContentBlock(app, content, highlight, width, WithBorderColor(borderColor)) + return renderContentBlock(app, content, width, WithBorderColor(borderColor)) } func renderToolName(name string) string { switch name { case "webfetch": return "Fetch" - case "todowrite", "todoread": - return "Plan" default: normalizedName := name if after, ok := strings.CutPrefix(name, "opencode_"); ok { @@ -514,21 +471,54 @@ func renderToolName(name string) string { } } +func getTodoPhase(metadata map[string]any) string { + todos, ok := metadata["todos"].([]any) + if !ok || len(todos) == 0 { + return "Plan" + } + + counts := map[string]int{"pending": 0, "completed": 0} + for _, item := range todos { + if todo, ok := item.(map[string]any); ok { + if status, ok := todo["status"].(string); ok { + counts[status]++ + } + } + } + + total := len(todos) + switch { + case counts["pending"] == total: + return "Creating plan" + case counts["completed"] == total: + return "Completing plan" + default: + return "Updating plan" + } +} + +func getTodoTitle(toolCall opencode.ToolPart) string { + if toolCall.State.Status == opencode.ToolPartStateStatusCompleted { + if metadata, ok := toolCall.State.Metadata.(map[string]any); ok { + return getTodoPhase(metadata) + } + } + return "Plan" +} + func renderToolTitle( - toolCall opencode.ToolInvocationPart, - messageMetadata opencode.MessageMetadata, + toolCall opencode.ToolPart, width int, ) string { - // TODO: handle truncate to width - - if toolCall.ToolInvocation.State == "partial-call" { - return renderToolAction(toolCall.ToolInvocation.ToolName) + if toolCall.State.Status == opencode.ToolPartStateStatusPending { + title := renderToolAction(toolCall.Tool) + return styles.NewStyle().Width(width - 6).Render(title) } toolArgs := "" toolArgsMap := make(map[string]any) - if toolCall.ToolInvocation.Args != nil { - value := toolCall.ToolInvocation.Args + if toolCall.State.Input != nil { + value := toolCall.State.Input if m, ok := value.(map[string]any); ok { toolArgsMap = m @@ -546,8 +536,8 @@ func renderToolTitle( } } - title := renderToolName(toolCall.ToolInvocation.ToolName) - switch toolCall.ToolInvocation.ToolName { + title := renderToolName(toolCall.Tool) + switch toolCall.Tool { case "read": toolArgs = renderArgs(&toolArgsMap, "filePath") title = fmt.Sprintf("%s %s", title, toolArgs) @@ -562,19 +552,23 @@ func renderToolTitle( case "webfetch": toolArgs = renderArgs(&toolArgsMap, "url") title = fmt.Sprintf("%s %s", title, toolArgs) - case "todowrite", "todoread": - // title is just the tool name + case "todowrite": + title = getTodoTitle(toolCall) + case "todoread": + return "Plan" default: - toolName := renderToolName(toolCall.ToolInvocation.ToolName) + toolName := renderToolName(toolCall.Tool) title = fmt.Sprintf("%s %s", toolName, toolArgs) } + + title = truncate.StringWithTail(title, uint(width-6), "...") return title } func renderToolAction(name string) string { switch name { case "task": - return "Searching..." + return "Planning..." case "bash": return "Writing command..." case "edit": @@ -645,8 +639,13 @@ type Diagnostic struct { } // renderDiagnostics formats LSP diagnostics for display in the TUI -func renderDiagnostics(metadata opencode.MessageMetadataTool, filePath string) string { - if diagnosticsData, ok := metadata.ExtraFields["diagnostics"].(map[string]any); ok { +func renderDiagnostics( + metadata map[string]any, + filePath string, + backgroundColor compat.AdaptiveColor, + width int, +) string { + if diagnosticsData, ok := metadata["diagnostics"].(map[string]any); ok { if fileDiagnostics, ok := diagnosticsData[filePath].([]any); ok { var errorDiagnostics []string for _, diagInterface := range fileDiagnostics { @@ -681,9 +680,15 @@ func renderDiagnostics(metadata opencode.MessageMetadataTool, filePath string) s var result strings.Builder for _, diagnostic := range errorDiagnostics { if result.Len() > 0 { - result.WriteString("\n") + result.WriteString("\n\n") } - result.WriteString(styles.NewStyle().Foreground(t.Error()).Render(diagnostic)) + diagnostic = ansi.WordwrapWc(diagnostic, width, " -") + result.WriteString( + styles.NewStyle(). + Background(backgroundColor). + Foreground(t.Error()). + Render(diagnostic), + ) } return result.String() } diff --git a/packages/tui/internal/components/chat/messages.go b/packages/tui/internal/components/chat/messages.go index a59b5d79..9c2dd7e8 100644 --- a/packages/tui/internal/components/chat/messages.go +++ b/packages/tui/internal/components/chat/messages.go @@ -1,6 +1,7 @@ package chat import ( + "fmt" "strings" "github.com/charmbracelet/bubbles/v2/viewport" @@ -9,6 +10,7 @@ import ( "github.com/sst/opencode-sdk-go" "github.com/sst/opencode/internal/app" "github.com/sst/opencode/internal/components/dialog" + "github.com/sst/opencode/internal/components/toast" "github.com/sst/opencode/internal/layout" "github.com/sst/opencode/internal/styles" "github.com/sst/opencode/internal/theme" @@ -17,37 +19,30 @@ import ( type MessagesComponent interface { tea.Model - View(width, height int) string - SetWidth(width int) tea.Cmd + tea.ViewModel PageUp() (tea.Model, tea.Cmd) PageDown() (tea.Model, tea.Cmd) HalfPageUp() (tea.Model, tea.Cmd) HalfPageDown() (tea.Model, tea.Cmd) - First() (tea.Model, tea.Cmd) - Last() (tea.Model, tea.Cmd) - Previous() (tea.Model, tea.Cmd) - Next() (tea.Model, tea.Cmd) ToolDetailsVisible() bool - Selected() string + GotoTop() (tea.Model, tea.Cmd) + GotoBottom() (tea.Model, tea.Cmd) + CopyLastMessage() (tea.Model, tea.Cmd) } type messagesComponent struct { - width int + width, height int app *app.App + header string viewport viewport.Model - cache *MessageCache + cache *PartCache rendering bool showToolDetails bool tail bool partCount int lineCount int - selectedPart int - selectedText string } type renderFinishedMsg struct{} -type selectedMessagePartChangedMsg struct { - part int -} type ToggleToolDetailsMsg struct{} @@ -55,17 +50,23 @@ func (m *messagesComponent) Init() tea.Cmd { return tea.Batch(m.viewport.Init()) } -func (m *messagesComponent) Selected() string { - return m.selectedText -} - func (m *messagesComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmds []tea.Cmd switch msg := msg.(type) { + case tea.WindowSizeMsg: + effectiveWidth := msg.Width - 4 + // Clear cache on resize since width affects rendering + if m.width != effectiveWidth { + m.cache.Clear() + } + m.width = effectiveWidth + m.height = msg.Height - 7 + m.viewport.SetWidth(m.width) + m.header = m.renderHeader() + return m, m.Reload() case app.SendMsg: m.viewport.GotoBottom() m.tail = true - m.selectedPart = -1 return m, nil case app.OptimisticMessageAddedMsg: m.tail = true @@ -89,18 +90,21 @@ func (m *messagesComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if m.tail { m.viewport.GotoBottom() } - case selectedMessagePartChangedMsg: - return m, m.Reload() + case opencode.EventListResponseEventSessionUpdated: if msg.Properties.Info.ID == m.app.Session.ID { - m.renderView(m.width) + m.header = m.renderHeader() + } + case opencode.EventListResponseEventMessageUpdated: + if msg.Properties.Info.SessionID == m.app.Session.ID { + m.renderView() if m.tail { m.viewport.GotoBottom() } } - case opencode.EventListResponseEventMessageUpdated: - if msg.Properties.Info.Metadata.SessionID == m.app.Session.ID { - m.renderView(m.width) + case opencode.EventListResponseEventMessagePartUpdated: + if msg.Properties.Part.SessionID == m.app.Session.ID { + m.renderView() if m.tail { m.viewport.GotoBottom() } @@ -115,31 +119,40 @@ func (m *messagesComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Batch(cmds...) } -func (m *messagesComponent) renderView(width int) { +func (m *messagesComponent) renderView() { measure := util.Measure("messages.renderView") defer measure("messageCount", len(m.app.Messages)) + m.header = m.renderHeader() + t := theme.CurrentTheme() blocks := make([]string, 0) m.partCount = 0 m.lineCount = 0 - orphanedToolCalls := make([]opencode.ToolInvocationPart, 0) + orphanedToolCalls := make([]opencode.ToolPart, 0) + + width := min(m.width, app.MAX_CONTAINER_WIDTH) + if m.app.Config.Layout == opencode.LayoutConfigStretch { + width = m.width + } for _, message := range m.app.Messages { var content string var cached bool - switch message.Role { - case opencode.MessageRoleUser: - userLoop: + switch casted := message.Info.(type) { + case opencode.UserMessage: for partIndex, part := range message.Parts { - switch part := part.AsUnion().(type) { + switch part := part.(type) { case opencode.TextPart: + if part.Synthetic { + continue + } remainingParts := message.Parts[partIndex+1:] fileParts := make([]opencode.FilePart, 0) for _, part := range remainingParts { - switch part := part.AsUnion().(type) { + switch part := part.(type) { case opencode.FilePart: fileParts = append(fileParts, part) } @@ -150,7 +163,7 @@ func (m *messagesComponent) renderView(width int) { mediaTypeStyle := styles.NewStyle().Background(t.Secondary()).Foreground(t.BackgroundPanel()).Padding(0, 1) for _, filePart := range fileParts { mediaType := "" - switch filePart.MediaType { + switch filePart.Mime { case "text/plain": mediaType = "txt" case "image/png", "image/jpeg", "image/gif", "image/webp": @@ -175,45 +188,49 @@ func (m *messagesComponent) renderView(width int) { flexItems..., ) - key := m.cache.GenerateKey(message.ID, part.Text, width, m.selectedPart == m.partCount, files) + key := m.cache.GenerateKey(casted.ID, part.Text, width, files) content, cached = m.cache.Get(key) if !cached { content = renderText( m.app, - message, + message.Info, part.Text, - m.app.Info.User, + m.app.Config.Username, m.showToolDetails, - m.partCount == m.selectedPart, width, files, ) + content = lipgloss.PlaceHorizontal( + m.width, + lipgloss.Center, + content, + styles.WhitespaceStyle(t.Background()), + ) m.cache.Set(key, content) } if content != "" { - m = m.updateSelected(content, part.Text) + m.partCount++ + m.lineCount += lipgloss.Height(content) + 1 blocks = append(blocks, content) } - // Only render the first text part - break userLoop } } - case opencode.MessageRoleAssistant: + case opencode.AssistantMessage: hasTextPart := false for partIndex, p := range message.Parts { - switch part := p.AsUnion().(type) { + switch part := p.(type) { case opencode.TextPart: hasTextPart = true - finished := message.Metadata.Time.Completed > 0 + finished := casted.Time.Completed > 0 remainingParts := message.Parts[partIndex+1:] - toolCallParts := make([]opencode.ToolInvocationPart, 0) + toolCallParts := make([]opencode.ToolPart, 0) // sometimes tool calls happen without an assistant message // these should be included in this assistant message as well if len(orphanedToolCalls) > 0 { toolCallParts = append(toolCallParts, orphanedToolCalls...) - orphanedToolCalls = make([]opencode.ToolInvocationPart, 0) + orphanedToolCalls = make([]opencode.ToolPart, 0) } remaining := true @@ -221,14 +238,14 @@ func (m *messagesComponent) renderView(width int) { if !remaining { break } - switch part := part.AsUnion().(type) { + switch part := part.(type) { case opencode.TextPart: // we only want tool calls associated with the current text part. // if we hit another text part, we're done. remaining = false - case opencode.ToolInvocationPart: + case opencode.ToolPart: toolCallParts = append(toolCallParts, part) - if part.ToolInvocation.State != "result" { + if part.State.Status != opencode.ToolPartStateStatusCompleted && part.State.Status != opencode.ToolPartStateStatusError { // i don't think there's a case where a tool call isn't in result state // and the message time is 0, but just in case finished = false @@ -237,40 +254,51 @@ func (m *messagesComponent) renderView(width int) { } if finished { - key := m.cache.GenerateKey(message.ID, p.Text, width, m.showToolDetails, m.selectedPart == m.partCount) + key := m.cache.GenerateKey(casted.ID, part.Text, width, m.showToolDetails) content, cached = m.cache.Get(key) if !cached { content = renderText( m.app, - message, - p.Text, - message.Metadata.Assistant.ModelID, + message.Info, + part.Text, + casted.ModelID, m.showToolDetails, - m.partCount == m.selectedPart, width, "", toolCallParts..., ) + content = lipgloss.PlaceHorizontal( + m.width, + lipgloss.Center, + content, + styles.WhitespaceStyle(t.Background()), + ) m.cache.Set(key, content) } } else { content = renderText( m.app, - message, - p.Text, - message.Metadata.Assistant.ModelID, + message.Info, + part.Text, + casted.ModelID, m.showToolDetails, - m.partCount == m.selectedPart, width, "", toolCallParts..., ) + content = lipgloss.PlaceHorizontal( + m.width, + lipgloss.Center, + content, + styles.WhitespaceStyle(t.Background()), + ) } if content != "" { - m = m.updateSelected(content, p.Text) + m.partCount++ + m.lineCount += lipgloss.Height(content) + 1 blocks = append(blocks, content) } - case opencode.ToolInvocationPart: + case opencode.ToolPart: if !m.showToolDetails { if !hasTextPart { orphanedToolCalls = append(orphanedToolCalls, part) @@ -278,22 +306,32 @@ func (m *messagesComponent) renderView(width int) { continue } - if part.ToolInvocation.State == "result" { - key := m.cache.GenerateKey(message.ID, - part.ToolInvocation.ToolCallID, + width := width + if m.app.Config.Layout == opencode.LayoutConfigAuto && + part.Tool == "edit" && + part.State.Error == "" { + width = min(m.width, app.EDIT_DIFF_MAX_WIDTH) + } + + if part.State.Status == opencode.ToolPartStateStatusCompleted || part.State.Status == opencode.ToolPartStateStatusError { + key := m.cache.GenerateKey(casted.ID, + part.ID, m.showToolDetails, width, - m.partCount == m.selectedPart, ) content, cached = m.cache.Get(key) if !cached { content = renderToolDetails( m.app, part, - message.Metadata, - m.partCount == m.selectedPart, width, ) + content = lipgloss.PlaceHorizontal( + m.width, + lipgloss.Center, + content, + styles.WhitespaceStyle(t.Background()), + ) m.cache.Set(key, content) } } else { @@ -301,13 +339,18 @@ func (m *messagesComponent) renderView(width int) { content = renderToolDetails( m.app, part, - message.Metadata, - m.partCount == m.selectedPart, width, ) + content = lipgloss.PlaceHorizontal( + m.width, + lipgloss.Center, + content, + styles.WhitespaceStyle(t.Background()), + ) } if content != "" { - m = m.updateSelected(content, "") + m.partCount++ + m.lineCount += lipgloss.Height(content) + 1 blocks = append(blocks, content) } } @@ -315,69 +358,133 @@ func (m *messagesComponent) renderView(width int) { } error := "" - switch err := message.Metadata.Error.AsUnion().(type) { - case nil: - case opencode.MessageMetadataErrorMessageOutputLengthError: - error = "Message output length exceeded" - case opencode.ProviderAuthError: - error = err.Data.Message - case opencode.UnknownError: - error = err.Data.Message + if assistant, ok := message.Info.(opencode.AssistantMessage); ok { + switch err := assistant.Error.AsUnion().(type) { + case nil: + case opencode.AssistantMessageErrorMessageOutputLengthError: + error = "Message output length exceeded" + case opencode.ProviderAuthError: + error = err.Data.Message + case opencode.MessageAbortedError: + error = "Request was aborted" + case opencode.UnknownError: + error = err.Data.Message + } } if error != "" { + error = styles.NewStyle().Width(width - 6).Render(error) error = renderContentBlock( m.app, error, - false, width, WithBorderColor(t.Error()), ) + error = lipgloss.PlaceHorizontal( + m.width, + lipgloss.Center, + error, + styles.WhitespaceStyle(t.Background()), + ) blocks = append(blocks, error) m.lineCount += lipgloss.Height(error) + 1 } } + m.viewport.SetHeight(m.height - lipgloss.Height(m.header)) m.viewport.SetContent("\n" + strings.Join(blocks, "\n\n")) - if m.selectedPart == m.partCount { + if m.tail { m.viewport.GotoBottom() } - } -func (m *messagesComponent) updateSelected(content string, selectedText string) *messagesComponent { - if m.selectedPart == m.partCount { - m.viewport.SetYOffset(m.lineCount - (m.viewport.Height() / 2) + 4) - m.selectedText = selectedText - } - m.partCount++ - m.lineCount += lipgloss.Height(content) + 1 - return m -} - -func (m *messagesComponent) header(width int) string { +func (m *messagesComponent) renderHeader() string { if m.app.Session.ID == "" { return "" } + headerWidth := min(m.width, app.MAX_CONTAINER_WIDTH) + if m.app.Config.Layout == opencode.LayoutConfigStretch { + headerWidth = m.width + } + t := theme.CurrentTheme() base := styles.NewStyle().Foreground(t.Text()).Background(t.Background()).Render muted := styles.NewStyle().Foreground(t.TextMuted()).Background(t.Background()).Render headerLines := []string{} headerLines = append( headerLines, - util.ToMarkdown("# "+m.app.Session.Title, width-6, t.Background()), + util.ToMarkdown("# "+m.app.Session.Title, headerWidth-6, t.Background()), ) - if m.app.Session.Share.URL != "" { - headerLines = append(headerLines, muted(m.app.Session.Share.URL+" /unshare")) - } else { - headerLines = append(headerLines, base("/share")+muted(" to create a shareable link")) - } - header := strings.Join(headerLines, "\n") + share := "" + if m.app.Session.Share.URL != "" { + share = muted(m.app.Session.Share.URL + " /unshare") + } else { + share = base("/share") + muted(" to create a shareable link") + } + + sessionInfo := "" + tokens := float64(0) + cost := float64(0) + contextWindow := m.app.Model.Limit.Context + + for _, message := range m.app.Messages { + if assistant, ok := message.Info.(opencode.AssistantMessage); ok { + cost += assistant.Cost + usage := assistant.Tokens + if usage.Output > 0 { + if assistant.Summary { + tokens = usage.Output + continue + } + tokens = (usage.Input + + usage.Cache.Write + + usage.Cache.Read + + usage.Output + + usage.Reasoning) + } + } + } + + // Check if current model is a subscription model (cost is 0 for both input and output) + isSubscriptionModel := m.app.Model != nil && + m.app.Model.Cost.Input == 0 && m.app.Model.Cost.Output == 0 + + sessionInfo = styles.NewStyle(). + Foreground(t.TextMuted()). + Background(t.Background()). + Render(formatTokensAndCost(tokens, contextWindow, cost, isSubscriptionModel)) + + background := t.Background() + + var items []layout.FlexItem + justify := layout.JustifyEnd + + if m.app.Config.Share != opencode.ConfigShareDisabled { + items = append(items, layout.FlexItem{View: share}) + justify = layout.JustifySpaceBetween + } + + items = append(items, layout.FlexItem{View: sessionInfo}) + + headerRow := layout.Render( + layout.FlexOptions{ + Background: &background, + Direction: layout.Row, + Justify: justify, + Align: layout.AlignStretch, + Width: headerWidth - 6, + }, + items..., + ) + + headerLines = append(headerLines, headerRow) + + header := strings.Join(headerLines, "\n") header = styles.NewStyle(). Background(t.Background()). - Width(width). + Width(headerWidth). PaddingLeft(2). PaddingRight(2). BorderLeft(true). @@ -386,48 +493,84 @@ func (m *messagesComponent) header(width int) string { BorderForeground(t.BackgroundElement()). BorderStyle(lipgloss.ThickBorder()). Render(header) + header = lipgloss.PlaceHorizontal( + m.width, + lipgloss.Center, + header, + styles.WhitespaceStyle(t.Background()), + ) return "\n" + header + "\n" } -func (m *messagesComponent) View(width, height int) string { +func formatTokensAndCost( + tokens float64, + contextWindow float64, + cost float64, + isSubscriptionModel bool, +) string { + // Format tokens in human-readable format (e.g., 110K, 1.2M) + var formattedTokens string + switch { + case tokens >= 1_000_000: + formattedTokens = fmt.Sprintf("%.1fM", float64(tokens)/1_000_000) + case tokens >= 1_000: + formattedTokens = fmt.Sprintf("%.1fK", float64(tokens)/1_000) + default: + formattedTokens = fmt.Sprintf("%d", int(tokens)) + } + + // Remove .0 suffix if present + if strings.HasSuffix(formattedTokens, ".0K") { + formattedTokens = strings.Replace(formattedTokens, ".0K", "K", 1) + } + if strings.HasSuffix(formattedTokens, ".0M") { + formattedTokens = strings.Replace(formattedTokens, ".0M", "M", 1) + } + + percentage := 0.0 + if contextWindow > 0 { + percentage = (float64(tokens) / float64(contextWindow)) * 100 + } + + if isSubscriptionModel { + return fmt.Sprintf( + "%s/%d%%", + formattedTokens, + int(percentage), + ) + } + + formattedCost := fmt.Sprintf("$%.2f", cost) + return fmt.Sprintf( + "%s/%d%% (%s)", + formattedTokens, + int(percentage), + formattedCost, + ) +} + +func (m *messagesComponent) View() string { t := theme.CurrentTheme() if m.rendering { return lipgloss.Place( - width, - height, + m.width, + m.height, lipgloss.Center, lipgloss.Center, styles.NewStyle().Background(t.Background()).Render(""), styles.WhitespaceStyle(t.Background()), ) } - header := m.header(width) - m.viewport.SetWidth(width) - m.viewport.SetHeight(height - lipgloss.Height(header)) return styles.NewStyle(). Background(t.Background()). - Render(header + "\n" + m.viewport.View()) -} - -func (m *messagesComponent) SetWidth(width int) tea.Cmd { - if m.width == width { - return nil - } - // Clear cache on resize since width affects rendering - if m.width != width { - m.cache.Clear() - } - m.width = width - m.viewport.SetWidth(width) - m.renderView(width) - return nil + Render(m.header + "\n" + m.viewport.View()) } func (m *messagesComponent) Reload() tea.Cmd { return func() tea.Msg { - m.renderView(m.width) + m.renderView() return renderFinishedMsg{} } } @@ -452,61 +595,50 @@ func (m *messagesComponent) HalfPageDown() (tea.Model, tea.Cmd) { return m, nil } -func (m *messagesComponent) Previous() (tea.Model, tea.Cmd) { - m.tail = false - if m.selectedPart < 0 { - m.selectedPart = m.partCount - } - m.selectedPart-- - if m.selectedPart < 0 { - m.selectedPart = 0 - } - return m, util.CmdHandler(selectedMessagePartChangedMsg{ - part: m.selectedPart, - }) -} - -func (m *messagesComponent) Next() (tea.Model, tea.Cmd) { - m.tail = false - m.selectedPart++ - if m.selectedPart >= m.partCount { - m.selectedPart = m.partCount - } - return m, util.CmdHandler(selectedMessagePartChangedMsg{ - part: m.selectedPart, - }) -} - -func (m *messagesComponent) First() (tea.Model, tea.Cmd) { - m.selectedPart = 0 - m.tail = false - return m, util.CmdHandler(selectedMessagePartChangedMsg{ - part: m.selectedPart, - }) -} - -func (m *messagesComponent) Last() (tea.Model, tea.Cmd) { - m.selectedPart = m.partCount - 1 - m.tail = true - return m, util.CmdHandler(selectedMessagePartChangedMsg{ - part: m.selectedPart, - }) -} - func (m *messagesComponent) ToolDetailsVisible() bool { return m.showToolDetails } +func (m *messagesComponent) GotoTop() (tea.Model, tea.Cmd) { + m.viewport.GotoTop() + return m, nil +} + +func (m *messagesComponent) GotoBottom() (tea.Model, tea.Cmd) { + m.viewport.GotoBottom() + return m, nil +} + +func (m *messagesComponent) CopyLastMessage() (tea.Model, tea.Cmd) { + if len(m.app.Messages) == 0 { + return m, nil + } + lastMessage := m.app.Messages[len(m.app.Messages)-1] + var lastTextPart *opencode.TextPart + for _, part := range lastMessage.Parts { + if p, ok := part.(opencode.TextPart); ok { + lastTextPart = &p + } + } + if lastTextPart == nil { + return m, nil + } + var cmds []tea.Cmd + cmds = append(cmds, m.app.SetClipboard(lastTextPart.Text)) + cmds = append(cmds, toast.NewSuccessToast("Message copied to clipboard")) + return m, tea.Batch(cmds...) +} + func NewMessagesComponent(app *app.App) MessagesComponent { vp := viewport.New() vp.KeyMap = viewport.KeyMap{} + vp.MouseWheelDelta = 4 return &messagesComponent{ app: app, viewport: vp, showToolDetails: true, - cache: NewMessageCache(), + cache: NewPartCache(), tail: true, - selectedPart: -1, } } diff --git a/packages/tui/internal/components/commands/commands.go b/packages/tui/internal/components/commands/commands.go index f3080b38..7f293230 100644 --- a/packages/tui/internal/components/commands/commands.go +++ b/packages/tui/internal/components/commands/commands.go @@ -56,8 +56,8 @@ func (c *commandsComponent) View() string { var untriggeredCommands []commands.Command for _, cmd := range c.app.Commands.Sorted() { - if c.showAll || cmd.Trigger != "" { - if cmd.Trigger != "" { + if c.showAll || cmd.HasTrigger() { + if cmd.HasTrigger() { triggeredCommands = append(triggeredCommands, cmd) } else if c.showAll { untriggeredCommands = append(untriggeredCommands, cmd) @@ -97,8 +97,8 @@ func (c *commandsComponent) View() string { for _, cmd := range commandsToShow { trigger := "" - if cmd.Trigger != "" { - trigger = "/" + cmd.Trigger + if cmd.HasTrigger() { + trigger = "/" + cmd.PrimaryTrigger() } else { trigger = string(cmd.Name) } diff --git a/packages/tui/internal/components/dialog/complete.go b/packages/tui/internal/components/dialog/complete.go index 7ba91dc5..f18d9751 100644 --- a/packages/tui/internal/components/dialog/complete.go +++ b/packages/tui/internal/components/dialog/complete.go @@ -2,69 +2,25 @@ package dialog import ( "log/slog" + "sort" + "strings" "github.com/charmbracelet/bubbles/v2/key" "github.com/charmbracelet/bubbles/v2/textarea" tea "github.com/charmbracelet/bubbletea/v2" "github.com/charmbracelet/lipgloss/v2" + "github.com/lithammer/fuzzysearch/fuzzy" + "github.com/muesli/reflow/truncate" + "github.com/sst/opencode/internal/completions" "github.com/sst/opencode/internal/components/list" "github.com/sst/opencode/internal/styles" "github.com/sst/opencode/internal/theme" "github.com/sst/opencode/internal/util" ) -type CompletionItem struct { - Title string - Value string -} - -type CompletionItemI interface { - list.ListItem - GetValue() string - DisplayValue() string -} - -func (ci *CompletionItem) Render(selected bool, width int) string { - t := theme.CurrentTheme() - baseStyle := styles.NewStyle().Foreground(t.Text()) - - itemStyle := baseStyle. - Background(t.BackgroundElement()). - Width(width). - Padding(0, 1) - - if selected { - itemStyle = itemStyle.Foreground(t.Primary()) - } - - title := itemStyle.Render( - ci.DisplayValue(), - ) - return title -} - -func (ci *CompletionItem) DisplayValue() string { - return ci.Title -} - -func (ci *CompletionItem) GetValue() string { - return ci.Value -} - -func NewCompletionItem(completionItem CompletionItem) CompletionItemI { - return &completionItem -} - -type CompletionProvider interface { - GetId() string - GetChildEntries(query string) ([]CompletionItemI, error) - GetEmptyMessage() string -} - type CompletionSelectedMsg struct { - SearchString string - CompletionValue string - ProviderID string + Item completions.CompletionSuggestion + SearchString string } type CompletionDialogCompleteItemMsg struct { @@ -82,11 +38,12 @@ type CompletionDialog interface { type completionDialogComponent struct { query string - completionProvider CompletionProvider + providers []completions.CompletionProvider width int height int pseudoSearchTextArea textarea.Model - list list.List[CompletionItemI] + list list.List[completions.CompletionSuggestion] + trigger string } type completionDialogKeyMap struct { @@ -99,7 +56,7 @@ var completionDialogKeys = completionDialogKeyMap{ key.WithKeys("tab", "enter", "right"), ), Cancel: key.NewBinding( - key.WithKeys(" ", "esc", "backspace", "ctrl+c"), + key.WithKeys("space", " ", "esc", "backspace", "ctrl+h", "ctrl+c"), ), } @@ -107,10 +64,59 @@ func (c *completionDialogComponent) Init() tea.Cmd { return nil } +func (c *completionDialogComponent) getAllCompletions(query string) tea.Cmd { + return func() tea.Msg { + allItems := make([]completions.CompletionSuggestion, 0) + providersWithResults := 0 + + // Collect results from all providers + for _, provider := range c.providers { + items, err := provider.GetChildEntries(query) + if err != nil { + slog.Error( + "Failed to get completion items", + "provider", + provider.GetId(), + "error", + err, + ) + continue + } + if len(items) > 0 { + providersWithResults++ + allItems = append(allItems, items...) + } + } + + // If there's a query, use fuzzy ranking to sort results + if query != "" && providersWithResults > 1 { + t := theme.CurrentTheme() + baseStyle := styles.NewStyle().Background(t.BackgroundElement()) + // Create a slice of display values for fuzzy matching + displayValues := make([]string, len(allItems)) + for i, item := range allItems { + displayValues[i] = item.Display(baseStyle) + } + + matches := fuzzy.RankFindFold(query, displayValues) + sort.Sort(matches) + + // Reorder items based on fuzzy ranking + rankedItems := make([]completions.CompletionSuggestion, 0, len(matches)) + for _, match := range matches { + rankedItems = append(rankedItems, allItems[match.OriginalIndex]) + } + + return rankedItems + } + + return allItems + } +} func (c *completionDialogComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmds []tea.Cmd switch msg := msg.(type) { - case []CompletionItemI: + case []completions.CompletionSuggestion: c.list.SetItems(msg) case tea.KeyMsg: if c.pseudoSearchTextArea.Focused() { @@ -119,23 +125,16 @@ func (c *completionDialogComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { c.pseudoSearchTextArea, cmd = c.pseudoSearchTextArea.Update(msg) cmds = append(cmds, cmd) - var query string - query = c.pseudoSearchTextArea.Value() + fullValue := c.pseudoSearchTextArea.Value() + query := strings.TrimPrefix(fullValue, c.trigger) if query != c.query { c.query = query - cmd = func() tea.Msg { - items, err := c.completionProvider.GetChildEntries(query) - if err != nil { - slog.Error("Failed to get completion items", "error", err) - } - return items - } - cmds = append(cmds, cmd) + cmds = append(cmds, c.getAllCompletions(query)) } u, cmd := c.list.Update(msg) - c.list = u.(list.List[CompletionItemI]) + c.list = u.(list.List[completions.CompletionSuggestion]) cmds = append(cmds, cmd) } @@ -147,22 +146,18 @@ func (c *completionDialogComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } return c, c.complete(item) case key.Matches(msg, completionDialogKeys.Cancel): - // Only close on backspace when there are no characters left - if msg.String() != "backspace" || len(c.pseudoSearchTextArea.Value()) <= 0 { + value := c.pseudoSearchTextArea.Value() + width := lipgloss.Width(value) + triggerWidth := lipgloss.Width(c.trigger) + // Only close on backspace when there are no characters left, unless we're back to just the trigger + if (msg.String() != "backspace" && msg.String() != "ctrl+h") || (width <= triggerWidth && value != c.trigger) { return c, c.close() } } return c, tea.Batch(cmds...) } else { - cmd := func() tea.Msg { - items, err := c.completionProvider.GetChildEntries("") - if err != nil { - slog.Error("Failed to get completion items", "error", err) - } - return items - } - cmds = append(cmds, cmd) + cmds = append(cmds, c.getAllCompletions("")) cmds = append(cmds, c.pseudoSearchTextArea.Focus()) return c, tea.Batch(cmds...) } @@ -173,23 +168,11 @@ func (c *completionDialogComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (c *completionDialogComponent) View() string { t := theme.CurrentTheme() - baseStyle := styles.NewStyle().Foreground(t.Text()) + c.list.SetMaxWidth(c.width) - maxWidth := 40 - completions := c.list.GetItems() - - for _, cmd := range completions { - title := cmd.DisplayValue() - width := lipgloss.Width(title) - if width > maxWidth-4 { - maxWidth = width + 4 - } - } - - c.list.SetMaxWidth(maxWidth) - - return baseStyle. - Padding(0, 0). + return styles.NewStyle(). + Padding(0, 1). + Foreground(t.Text()). Background(t.BackgroundElement()). BorderStyle(lipgloss.ThickBorder()). BorderLeft(true). @@ -208,14 +191,12 @@ func (c *completionDialogComponent) IsEmpty() bool { return c.list.IsEmpty() } -func (c *completionDialogComponent) complete(item CompletionItemI) tea.Cmd { +func (c *completionDialogComponent) complete(item completions.CompletionSuggestion) tea.Cmd { value := c.pseudoSearchTextArea.Value() - return tea.Batch( util.CmdHandler(CompletionSelectedMsg{ - SearchString: value, - CompletionValue: item.GetValue(), - ProviderID: c.completionProvider.GetId(), + SearchString: value, + Item: item, }), c.close(), ) @@ -227,28 +208,76 @@ func (c *completionDialogComponent) close() tea.Cmd { return util.CmdHandler(CompletionDialogCloseMsg{}) } -func NewCompletionDialogComponent(completionProvider CompletionProvider) CompletionDialog { +func NewCompletionDialogComponent( + trigger string, + providers ...completions.CompletionProvider, +) CompletionDialog { ti := textarea.New() + ti.SetValue(trigger) + + // Use a generic empty message if we have multiple providers + emptyMessage := "no matching items" + if len(providers) == 1 { + emptyMessage = providers[0].GetEmptyMessage() + } + + // Define render function for completion suggestions + renderFunc := func(item completions.CompletionSuggestion, selected bool, width int, baseStyle styles.Style) string { + t := theme.CurrentTheme() + style := baseStyle + + if selected { + style = style.Background(t.BackgroundElement()).Foreground(t.Primary()) + } else { + style = style.Background(t.BackgroundElement()).Foreground(t.Text()) + } + + // The item.Display string already has any inline colors from the provider + truncatedStr := truncate.String(item.Display(style), uint(width-4)) + return style.Width(width - 4).Render(truncatedStr) + } + + // Define selectable function - all completion suggestions are selectable + selectableFunc := func(item completions.CompletionSuggestion) bool { + return true + } li := list.NewListComponent( - []CompletionItemI{}, - 7, - completionProvider.GetEmptyMessage(), - false, + list.WithItems([]completions.CompletionSuggestion{}), + list.WithMaxVisibleHeight[completions.CompletionSuggestion](7), + list.WithFallbackMessage[completions.CompletionSuggestion](emptyMessage), + list.WithAlphaNumericKeys[completions.CompletionSuggestion](false), + list.WithRenderFunc(renderFunc), + list.WithSelectableFunc(selectableFunc), ) - go func() { - items, err := completionProvider.GetChildEntries("") - if err != nil { - slog.Error("Failed to get completion items", "error", err) - } - li.SetItems(items) - }() - - return &completionDialogComponent{ + c := &completionDialogComponent{ query: "", - completionProvider: completionProvider, + providers: providers, pseudoSearchTextArea: ti, list: li, + trigger: trigger, } + + // Load initial items from all providers + go func() { + allItems := make([]completions.CompletionSuggestion, 0) + for _, provider := range providers { + items, err := provider.GetChildEntries("") + if err != nil { + slog.Error( + "Failed to get completion items", + "provider", + provider.GetId(), + "error", + err, + ) + continue + } + allItems = append(allItems, items...) + } + li.SetItems(allItems) + }() + + return c } diff --git a/packages/tui/internal/components/dialog/find.go b/packages/tui/internal/components/dialog/find.go index 3fc6e599..40be600c 100644 --- a/packages/tui/internal/components/dialog/find.go +++ b/packages/tui/internal/components/dialog/find.go @@ -3,9 +3,8 @@ package dialog import ( "log/slog" - "github.com/charmbracelet/bubbles/v2/key" - "github.com/charmbracelet/bubbles/v2/textinput" tea "github.com/charmbracelet/bubbletea/v2" + "github.com/sst/opencode/internal/completions" "github.com/sst/opencode/internal/components/list" "github.com/sst/opencode/internal/components/modal" "github.com/sst/opencode/internal/layout" @@ -14,12 +13,20 @@ import ( "github.com/sst/opencode/internal/util" ) +const ( + findDialogWidth = 76 +) + type FindSelectedMsg struct { FilePath string } type FindDialogCloseMsg struct{} +type findInitialSuggestionsMsg struct { + suggestions []completions.CompletionSuggestion +} + type FindDialog interface { layout.Modal tea.Model @@ -29,117 +36,157 @@ type FindDialog interface { IsEmpty() bool } +// findItem is a custom list item for file suggestions +type findItem struct { + suggestion completions.CompletionSuggestion +} + +func (f findItem) Render( + selected bool, + width int, + baseStyle styles.Style, +) string { + t := theme.CurrentTheme() + + itemStyle := baseStyle. + Background(t.BackgroundPanel()). + Foreground(t.TextMuted()) + + if selected { + itemStyle = itemStyle.Foreground(t.Primary()) + } + + return itemStyle.PaddingLeft(1).Render(f.suggestion.Display(itemStyle)) +} + +func (f findItem) Selectable() bool { + return true +} + type findDialogComponent struct { - query string - completionProvider CompletionProvider + completionProvider completions.CompletionProvider + allSuggestions []completions.CompletionSuggestion width, height int modal *modal.Modal - textInput textinput.Model - list list.List[CompletionItemI] -} - -type findDialogKeyMap struct { - Select key.Binding - Cancel key.Binding -} - -var findDialogKeys = findDialogKeyMap{ - Select: key.NewBinding( - key.WithKeys("enter"), - ), - Cancel: key.NewBinding( - key.WithKeys("esc"), - ), + searchDialog *SearchDialog + dialogWidth int } func (f *findDialogComponent) Init() tea.Cmd { - return textinput.Blink + return tea.Batch( + f.loadInitialSuggestions(), + f.searchDialog.Init(), + ) +} + +func (f *findDialogComponent) loadInitialSuggestions() tea.Cmd { + return func() tea.Msg { + items, err := f.completionProvider.GetChildEntries("") + if err != nil { + slog.Error("Failed to get initial completion items", "error", err) + return findInitialSuggestionsMsg{suggestions: []completions.CompletionSuggestion{}} + } + return findInitialSuggestionsMsg{suggestions: items} + } } func (f *findDialogComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - var cmd tea.Cmd - var cmds []tea.Cmd - switch msg := msg.(type) { - case []CompletionItemI: - f.list.SetItems(msg) - case tea.KeyMsg: - switch msg.String() { - case "ctrl+c": - if f.textInput.Value() == "" { - return f, nil - } - f.textInput.SetValue("") - return f.update(msg) + case findInitialSuggestionsMsg: + // Handle initial suggestions setup + f.allSuggestions = msg.suggestions + + // Calculate dialog width + f.dialogWidth = f.calculateDialogWidth() + + // Initialize search dialog with calculated width + f.searchDialog = NewSearchDialog("Search files...", 10) + f.searchDialog.SetWidth(f.dialogWidth) + + // Convert to list items + items := make([]list.Item, len(f.allSuggestions)) + for i, suggestion := range f.allSuggestions { + items[i] = findItem{suggestion: suggestion} } + f.searchDialog.SetItems(items) - switch { - case key.Matches(msg, findDialogKeys.Select): - item, i := f.list.GetSelectedItem() - if i == -1 { - return f, nil - } - return f, f.selectFile(item) - case key.Matches(msg, findDialogKeys.Cancel): - return f, f.Close() - default: - f.textInput, cmd = f.textInput.Update(msg) - cmds = append(cmds, cmd) + // Update modal with calculated width + f.modal = modal.New( + modal.WithTitle("Find Files"), + modal.WithMaxWidth(f.dialogWidth+4), + ) - f, cmd = f.update(msg) - cmds = append(cmds, cmd) + return f, f.searchDialog.Init() + + case []completions.CompletionSuggestion: + // Store suggestions and convert to findItem for the search dialog + f.allSuggestions = msg + items := make([]list.Item, len(msg)) + for i, suggestion := range msg { + items[i] = findItem{suggestion: suggestion} } - } + f.searchDialog.SetItems(items) + return f, nil - return f, tea.Batch(cmds...) -} + case SearchSelectionMsg: + // Handle selection from search dialog - now we can directly access the suggestion + if item, ok := msg.Item.(findItem); ok { + return f, f.selectFile(item.suggestion) + } + return f, nil -func (f *findDialogComponent) update(msg tea.Msg) (*findDialogComponent, tea.Cmd) { - var cmd tea.Cmd - var cmds []tea.Cmd + case SearchCancelledMsg: + return f, f.Close() - query := f.textInput.Value() - if query != f.query { - f.query = query - cmd = func() tea.Msg { - items, err := f.completionProvider.GetChildEntries(query) + case SearchQueryChangedMsg: + // Update completion items based on search query + return f, func() tea.Msg { + items, err := f.completionProvider.GetChildEntries(msg.Query) if err != nil { slog.Error("Failed to get completion items", "error", err) + return []completions.CompletionSuggestion{} } return items } - cmds = append(cmds, cmd) + + case tea.WindowSizeMsg: + f.width = msg.Width + f.height = msg.Height + // Recalculate width based on new viewport size + oldWidth := f.dialogWidth + f.dialogWidth = f.calculateDialogWidth() + if oldWidth != f.dialogWidth { + f.searchDialog.SetWidth(f.dialogWidth) + // Update modal max width too + f.modal = modal.New( + modal.WithTitle("Find Files"), + modal.WithMaxWidth(f.dialogWidth+4), + ) + } + f.searchDialog.SetHeight(msg.Height) } - u, cmd := f.list.Update(msg) - f.list = u.(list.List[CompletionItemI]) - cmds = append(cmds, cmd) - - return f, tea.Batch(cmds...) + // Forward all other messages to the search dialog + updatedDialog, cmd := f.searchDialog.Update(msg) + f.searchDialog = updatedDialog.(*SearchDialog) + return f, cmd } func (f *findDialogComponent) View() string { - t := theme.CurrentTheme() - f.textInput.SetWidth(f.width - 8) - f.list.SetMaxWidth(f.width - 4) - inputView := f.textInput.View() - inputView = styles.NewStyle(). - Background(t.BackgroundElement()). - Height(1). - Width(f.width-4). - Padding(0, 0). - Render(inputView) + return f.searchDialog.View() +} - listView := f.list.View() - return styles.NewStyle().Height(12).Render(inputView + "\n" + listView) +func (f *findDialogComponent) calculateDialogWidth() int { + // Use fixed width unless viewport is smaller + if f.width > 0 && f.width < findDialogWidth+10 { + return f.width - 10 + } + return findDialogWidth } func (f *findDialogComponent) SetWidth(width int) { f.width = width - if width > 4 { - f.textInput.SetWidth(width - 4) - f.list.SetMaxWidth(width - 4) - } + f.searchDialog.SetWidth(f.dialogWidth) } func (f *findDialogComponent) SetHeight(height int) { @@ -147,14 +194,14 @@ func (f *findDialogComponent) SetHeight(height int) { } func (f *findDialogComponent) IsEmpty() bool { - return f.list.IsEmpty() + return f.searchDialog.GetQuery() == "" } -func (f *findDialogComponent) selectFile(item CompletionItemI) tea.Cmd { +func (f *findDialogComponent) selectFile(item completions.CompletionSuggestion) tea.Cmd { return tea.Sequence( f.Close(), util.CmdHandler(FindSelectedMsg{ - FilePath: item.GetValue(), + FilePath: item.Value, }), ) } @@ -164,70 +211,26 @@ func (f *findDialogComponent) Render(background string) string { } func (f *findDialogComponent) Close() tea.Cmd { - f.textInput.Reset() - f.textInput.Blur() + f.searchDialog.SetQuery("") + f.searchDialog.Blur() return util.CmdHandler(modal.CloseModalMsg{}) } -func createTextInput(existing *textinput.Model) textinput.Model { - t := theme.CurrentTheme() - bgColor := t.BackgroundElement() - textColor := t.Text() - textMutedColor := t.TextMuted() - - ti := textinput.New() - - ti.Styles.Blurred.Placeholder = styles.NewStyle(). - Foreground(textMutedColor). - Background(bgColor). - Lipgloss() - ti.Styles.Blurred.Text = styles.NewStyle().Foreground(textColor).Background(bgColor).Lipgloss() - ti.Styles.Focused.Placeholder = styles.NewStyle(). - Foreground(textMutedColor). - Background(bgColor). - Lipgloss() - ti.Styles.Focused.Text = styles.NewStyle().Foreground(textColor).Background(bgColor).Lipgloss() - ti.Styles.Cursor.Color = t.Primary() - ti.VirtualCursor = true - - ti.Prompt = " " - ti.CharLimit = -1 - ti.Focus() - - if existing != nil { - ti.SetValue(existing.Value()) - ti.SetWidth(existing.Width()) +func NewFindDialog(completionProvider completions.CompletionProvider) FindDialog { + component := &findDialogComponent{ + completionProvider: completionProvider, + dialogWidth: findDialogWidth, + allSuggestions: []completions.CompletionSuggestion{}, } - return ti -} + // Create search dialog and modal with fixed width + component.searchDialog = NewSearchDialog("Search files...", 10) + component.searchDialog.SetWidth(findDialogWidth) -func NewFindDialog(completionProvider CompletionProvider) FindDialog { - ti := createTextInput(nil) - - li := list.NewListComponent( - []CompletionItemI{}, - 10, // max visible items - completionProvider.GetEmptyMessage(), - false, + component.modal = modal.New( + modal.WithTitle("Find Files"), + modal.WithMaxWidth(findDialogWidth+4), ) - go func() { - items, err := completionProvider.GetChildEntries("") - if err != nil { - slog.Error("Failed to get completion items", "error", err) - } - li.SetItems(items) - }() - - return &findDialogComponent{ - query: "", - completionProvider: completionProvider, - textInput: ti, - list: li, - modal: modal.New( - modal.WithTitle("Find Files"), - modal.WithMaxWidth(80), - ), - } + return component } diff --git a/packages/tui/internal/components/dialog/help.go b/packages/tui/internal/components/dialog/help.go index 4107384f..80123165 100644 --- a/packages/tui/internal/components/dialog/help.go +++ b/packages/tui/internal/components/dialog/help.go @@ -30,9 +30,10 @@ func (h *helpDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.WindowSizeMsg: h.width = msg.Width h.height = msg.Height - // Set viewport size with some padding for the modal - h.viewport = viewport.New(viewport.WithWidth(msg.Width-4), viewport.WithHeight(msg.Height-6)) - h.commandsComponent.SetSize(msg.Width-4, msg.Height-6) + // Set viewport size with some padding for the modal, but cap at reasonable width + maxWidth := min(80, msg.Width-8) + h.viewport = viewport.New(viewport.WithWidth(maxWidth-4), viewport.WithHeight(msg.Height-6)) + h.commandsComponent.SetSize(maxWidth-4, msg.Height-6) } // Update viewport content @@ -48,7 +49,7 @@ func (h *helpDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (h *helpDialog) View() string { t := theme.CurrentTheme() - h.commandsComponent.SetBackgroundColor(t.BackgroundElement()) + h.commandsComponent.SetBackgroundColor(t.BackgroundPanel()) return h.viewport.View() } @@ -69,11 +70,11 @@ func NewHelpDialog(app *app.App) HelpDialog { return &helpDialog{ app: app, commandsComponent: commandsComponent.New(app, - commandsComponent.WithBackground(theme.CurrentTheme().BackgroundElement()), + commandsComponent.WithBackground(theme.CurrentTheme().BackgroundPanel()), commandsComponent.WithShowAll(true), commandsComponent.WithKeybinds(true), ), - modal: modal.New(modal.WithTitle("Help")), + modal: modal.New(modal.WithTitle("Help"), modal.WithMaxWidth(80)), viewport: vp, } } diff --git a/packages/tui/internal/components/dialog/models.go b/packages/tui/internal/components/dialog/models.go index f8cda82a..8f1069fc 100644 --- a/packages/tui/internal/components/dialog/models.go +++ b/packages/tui/internal/components/dialog/models.go @@ -8,6 +8,7 @@ import ( "github.com/charmbracelet/bubbles/v2/key" tea "github.com/charmbracelet/bubbletea/v2" + "github.com/lithammer/fuzzysearch/fuzzy" "github.com/sst/opencode-sdk-go" "github.com/sst/opencode/internal/app" "github.com/sst/opencode/internal/components/list" @@ -22,6 +23,7 @@ const ( numVisibleModels = 10 minDialogWidth = 40 maxDialogWidth = 80 + maxRecentModels = 5 ) // ModelDialog interface for the model selection dialog @@ -30,13 +32,13 @@ type ModelDialog interface { } type modelDialog struct { - app *app.App - allModels []ModelWithProvider - width int - height int - modal *modal.Modal - modelList list.List[ModelItem] - dialogWidth int + app *app.App + allModels []ModelWithProvider + width int + height int + modal *modal.Modal + searchDialog *SearchDialog + dialogWidth int } type ModelWithProvider struct { @@ -44,39 +46,42 @@ type ModelWithProvider struct { Provider opencode.Provider } -type ModelItem struct { - ModelName string - ProviderName string +// modelItem is a custom list item for model selections +type modelItem struct { + model ModelWithProvider } -func (m ModelItem) Render(selected bool, width int) string { +func (m modelItem) Render( + selected bool, + width int, + baseStyle styles.Style, +) string { t := theme.CurrentTheme() + itemStyle := baseStyle. + Background(t.BackgroundPanel()). + Foreground(t.Text()) + if selected { - displayText := fmt.Sprintf("%s (%s)", m.ModelName, m.ProviderName) - return styles.NewStyle(). - Background(t.Primary()). - Foreground(t.BackgroundPanel()). - Width(width). - PaddingLeft(1). - Render(displayText) - } else { - modelStyle := styles.NewStyle(). - Foreground(t.Text()). - Background(t.BackgroundPanel()) - providerStyle := styles.NewStyle(). - Foreground(t.TextMuted()). - Background(t.BackgroundPanel()) - - modelPart := modelStyle.Render(m.ModelName) - providerPart := providerStyle.Render(fmt.Sprintf(" (%s)", m.ProviderName)) - - combinedText := modelPart + providerPart - return styles.NewStyle(). - Background(t.BackgroundPanel()). - PaddingLeft(1). - Render(combinedText) + itemStyle = itemStyle.Foreground(t.Primary()) } + + providerStyle := baseStyle. + Foreground(t.TextMuted()). + Background(t.BackgroundPanel()) + + modelPart := itemStyle.Render(m.model.Model.Name) + providerPart := providerStyle.Render(fmt.Sprintf(" %s", m.model.Provider.Name)) + + combinedText := modelPart + providerPart + return baseStyle. + Background(t.BackgroundPanel()). + PaddingLeft(1). + Render(combinedText) +} + +func (m modelItem) Selectable() bool { + return true } type modelKeyMap struct { @@ -97,52 +102,67 @@ var modelKeys = modelKeyMap{ func (m *modelDialog) Init() tea.Cmd { m.setupAllModels() - return nil + return m.searchDialog.Init() } func (m *modelDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { - case tea.KeyMsg: - switch { - case key.Matches(msg, modelKeys.Enter): - _, selectedIndex := m.modelList.GetSelectedItem() - if selectedIndex >= 0 && selectedIndex < len(m.allModels) { - selectedModel := m.allModels[selectedIndex] - return m, tea.Sequence( - util.CmdHandler(modal.CloseModalMsg{}), - util.CmdHandler( - app.ModelSelectedMsg{ - Provider: selectedModel.Provider, - Model: selectedModel.Model, - }), - ) - } - return m, util.CmdHandler(modal.CloseModalMsg{}) - case key.Matches(msg, modelKeys.Escape): - return m, util.CmdHandler(modal.CloseModalMsg{}) + case SearchSelectionMsg: + // Handle selection from search dialog + if item, ok := msg.Item.(modelItem); ok { + return m, tea.Sequence( + util.CmdHandler(modal.CloseModalMsg{}), + util.CmdHandler( + app.ModelSelectedMsg{ + Provider: item.model.Provider, + Model: item.model.Model, + }), + ) } + return m, util.CmdHandler(modal.CloseModalMsg{}) + case SearchCancelledMsg: + return m, util.CmdHandler(modal.CloseModalMsg{}) + + case SearchRemoveItemMsg: + if item, ok := msg.Item.(modelItem); ok { + if m.isModelInRecentSection(item.model, msg.Index) { + m.app.State.RemoveModelFromRecentlyUsed(item.model.Provider.ID, item.model.Model.ID) + m.app.SaveState() + items := m.buildDisplayList(m.searchDialog.GetQuery()) + m.searchDialog.SetItems(items) + } + } + return m, nil + + case SearchQueryChangedMsg: + // Update the list based on search query + items := m.buildDisplayList(msg.Query) + m.searchDialog.SetItems(items) + return m, nil + case tea.WindowSizeMsg: m.width = msg.Width m.height = msg.Height + m.searchDialog.SetWidth(m.dialogWidth) + m.searchDialog.SetHeight(msg.Height) } - // Update the list component - updatedList, cmd := m.modelList.Update(msg) - m.modelList = updatedList.(list.List[ModelItem]) + updatedDialog, cmd := m.searchDialog.Update(msg) + m.searchDialog = updatedDialog.(*SearchDialog) return m, cmd } func (m *modelDialog) View() string { - return m.modelList.View() + return m.searchDialog.View() } -func (m *modelDialog) calculateOptimalWidth(modelItems []ModelItem) int { +func (m *modelDialog) calculateOptimalWidth(models []ModelWithProvider) int { maxWidth := minDialogWidth - for _, item := range modelItems { + for _, model := range models { // Calculate the width needed for this item: "ModelName (ProviderName)" // Add 4 for the parentheses, space, and some padding - itemWidth := len(item.ModelName) + len(item.ProviderName) + 4 + itemWidth := len(model.Model.Name) + len(model.Provider.Name) + 4 if itemWidth > maxWidth { maxWidth = itemWidth } @@ -170,22 +190,16 @@ func (m *modelDialog) setupAllModels() { m.sortModels() - modelItems := make([]ModelItem, len(m.allModels)) - for i, modelWithProvider := range m.allModels { - modelItems[i] = ModelItem{ - ModelName: modelWithProvider.Model.Name, - ProviderName: modelWithProvider.Provider.Name, - } - } + // Calculate optimal width based on all models + m.dialogWidth = m.calculateOptimalWidth(m.allModels) - m.dialogWidth = m.calculateOptimalWidth(modelItems) + // Initialize search dialog + m.searchDialog = NewSearchDialog("Search models...", numVisibleModels) + m.searchDialog.SetWidth(m.dialogWidth) - m.modelList = list.NewListComponent(modelItems, numVisibleModels, "No models available", true) - m.modelList.SetMaxWidth(m.dialogWidth) - - if len(m.allModels) > 0 { - m.modelList.SetSelectedIndex(0) - } + // Build initial display list (empty query shows grouped view) + items := m.buildDisplayList("") + m.searchDialog.SetItems(items) } func (m *modelDialog) sortModels() { @@ -248,6 +262,176 @@ func (m *modelDialog) getModelUsageTime(providerID, modelID string) time.Time { return time.Time{} } +// buildDisplayList creates the list items based on search query +func (m *modelDialog) buildDisplayList(query string) []list.Item { + if query != "" { + // Search mode: use fuzzy matching + return m.buildSearchResults(query) + } else { + // Grouped mode: show Recent section and provider groups + return m.buildGroupedResults() + } +} + +// buildSearchResults creates a flat list of search results using fuzzy matching +func (m *modelDialog) buildSearchResults(query string) []list.Item { + type modelMatch struct { + model ModelWithProvider + score int + } + + modelNames := []string{} + modelMap := make(map[string]ModelWithProvider) + + // Create search strings and perform fuzzy matching + for _, model := range m.allModels { + searchStr := fmt.Sprintf("%s %s", model.Model.Name, model.Provider.Name) + modelNames = append(modelNames, searchStr) + modelMap[searchStr] = model + + searchStr = fmt.Sprintf("%s %s", model.Provider.Name, model.Model.Name) + modelNames = append(modelNames, searchStr) + modelMap[searchStr] = model + } + + matches := fuzzy.RankFindFold(query, modelNames) + sort.Sort(matches) + + items := []list.Item{} + seenModels := make(map[string]bool) + + for _, match := range matches { + model := modelMap[match.Target] + // Create a unique key to avoid duplicates + key := fmt.Sprintf("%s:%s", model.Provider.ID, model.Model.ID) + if seenModels[key] { + continue + } + seenModels[key] = true + items = append(items, modelItem{model: model}) + } + + return items +} + +// buildGroupedResults creates a grouped list with Recent section and provider groups +func (m *modelDialog) buildGroupedResults() []list.Item { + var items []list.Item + + // Add Recent section + recentModels := m.getRecentModels(maxRecentModels) + if len(recentModels) > 0 { + items = append(items, list.HeaderItem("Recent")) + for _, model := range recentModels { + items = append(items, modelItem{model: model}) + } + } + + // Group models by provider + providerGroups := make(map[string][]ModelWithProvider) + for _, model := range m.allModels { + providerName := model.Provider.Name + providerGroups[providerName] = append(providerGroups[providerName], model) + } + + // Get sorted provider names for consistent order + var providerNames []string + for name := range providerGroups { + providerNames = append(providerNames, name) + } + sort.Strings(providerNames) + + // Add provider groups + for _, providerName := range providerNames { + models := providerGroups[providerName] + + // Sort models within provider group + sort.Slice(models, func(i, j int) bool { + modelA := models[i] + modelB := models[j] + + usageA := m.getModelUsageTime(modelA.Provider.ID, modelA.Model.ID) + usageB := m.getModelUsageTime(modelB.Provider.ID, modelB.Model.ID) + + // Sort by usage time first, then by release date, then alphabetically + if !usageA.IsZero() && !usageB.IsZero() { + return usageA.After(usageB) + } + if !usageA.IsZero() && usageB.IsZero() { + return true + } + if usageA.IsZero() && !usageB.IsZero() { + return false + } + + // Sort by release date if available + if modelA.Model.ReleaseDate != "" && modelB.Model.ReleaseDate != "" { + dateA := m.parseReleaseDate(modelA.Model.ReleaseDate) + dateB := m.parseReleaseDate(modelB.Model.ReleaseDate) + if !dateA.IsZero() && !dateB.IsZero() { + return dateA.After(dateB) + } + } + + return modelA.Model.Name < modelB.Model.Name + }) + + // Add provider header + items = append(items, list.HeaderItem(providerName)) + + // Add models in this provider group + for _, model := range models { + items = append(items, modelItem{model: model}) + } + } + + return items +} + +// getRecentModels returns the most recently used models +func (m *modelDialog) getRecentModels(limit int) []ModelWithProvider { + var recentModels []ModelWithProvider + + // Get recent models from app state + for _, usage := range m.app.State.RecentlyUsedModels { + if len(recentModels) >= limit { + break + } + + // Find the corresponding model + for _, model := range m.allModels { + if model.Provider.ID == usage.ProviderID && model.Model.ID == usage.ModelID { + recentModels = append(recentModels, model) + break + } + } + } + + return recentModels +} + +func (m *modelDialog) isModelInRecentSection(model ModelWithProvider, index int) bool { + // Only check if we're in grouped mode (no search query) + if m.searchDialog.GetQuery() != "" { + return false + } + + recentModels := m.getRecentModels(maxRecentModels) + if len(recentModels) == 0 { + return false + } + + // Index 0 is the "Recent" header, so recent models are at indices 1 to len(recentModels) + if index >= 1 && index <= len(recentModels) { + if index-1 < len(recentModels) { + recentModel := recentModels[index-1] + return recentModel.Provider.ID == model.Provider.ID && recentModel.Model.ID == model.Model.ID + } + } + + return false +} + func (m *modelDialog) Render(background string) string { return m.modal.Render(m.View(), background) } diff --git a/packages/tui/internal/components/dialog/permission.go b/packages/tui/internal/components/dialog/permission.go deleted file mode 100644 index 5bc40624..00000000 --- a/packages/tui/internal/components/dialog/permission.go +++ /dev/null @@ -1,496 +0,0 @@ -package dialog - -import ( - "fmt" - "github.com/charmbracelet/bubbles/v2/key" - "github.com/charmbracelet/bubbles/v2/viewport" - tea "github.com/charmbracelet/bubbletea/v2" - "github.com/charmbracelet/lipgloss/v2" - "github.com/sst/opencode/internal/styles" - "github.com/sst/opencode/internal/theme" - "github.com/sst/opencode/internal/util" - "strings" -) - -type PermissionAction string - -// Permission responses -const ( - PermissionAllow PermissionAction = "allow" - PermissionAllowForSession PermissionAction = "allow_session" - PermissionDeny PermissionAction = "deny" -) - -// PermissionResponseMsg represents the user's response to a permission request -type PermissionResponseMsg struct { - // Permission permission.PermissionRequest - Action PermissionAction -} - -// PermissionDialogComponent interface for permission dialog component -type PermissionDialogComponent interface { - tea.Model - tea.ViewModel - // SetPermissions(permission permission.PermissionRequest) tea.Cmd -} - -type permissionsMapping struct { - Left key.Binding - Right key.Binding - EnterSpace key.Binding - Allow key.Binding - AllowSession key.Binding - Deny key.Binding - Tab key.Binding -} - -var permissionsKeys = permissionsMapping{ - Left: key.NewBinding( - key.WithKeys("left"), - key.WithHelp("←", "switch options"), - ), - Right: key.NewBinding( - key.WithKeys("right"), - key.WithHelp("→", "switch options"), - ), - EnterSpace: key.NewBinding( - key.WithKeys("enter", " "), - key.WithHelp("enter/space", "confirm"), - ), - Allow: key.NewBinding( - key.WithKeys("a"), - key.WithHelp("a", "allow"), - ), - AllowSession: key.NewBinding( - key.WithKeys("s"), - key.WithHelp("s", "allow for session"), - ), - Deny: key.NewBinding( - key.WithKeys("d"), - key.WithHelp("d", "deny"), - ), - Tab: key.NewBinding( - key.WithKeys("tab"), - key.WithHelp("tab", "switch options"), - ), -} - -// permissionDialogComponent is the implementation of PermissionDialog -type permissionDialogComponent struct { - width int - height int - // permission permission.PermissionRequest - windowSize tea.WindowSizeMsg - contentViewPort viewport.Model - selectedOption int // 0: Allow, 1: Allow for session, 2: Deny - - diffCache map[string]string - markdownCache map[string]string -} - -func (p *permissionDialogComponent) Init() tea.Cmd { - return p.contentViewPort.Init() -} - -func (p *permissionDialogComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - var cmds []tea.Cmd - - switch msg := msg.(type) { - case tea.WindowSizeMsg: - p.windowSize = msg - cmd := p.SetSize() - cmds = append(cmds, cmd) - p.markdownCache = make(map[string]string) - p.diffCache = make(map[string]string) - // case tea.KeyMsg: - // switch { - // case key.Matches(msg, permissionsKeys.Right) || key.Matches(msg, permissionsKeys.Tab): - // p.selectedOption = (p.selectedOption + 1) % 3 - // return p, nil - // case key.Matches(msg, permissionsKeys.Left): - // p.selectedOption = (p.selectedOption + 2) % 3 - // case key.Matches(msg, permissionsKeys.EnterSpace): - // return p, p.selectCurrentOption() - // case key.Matches(msg, permissionsKeys.Allow): - // return p, util.CmdHandler(PermissionResponseMsg{Action: PermissionAllow, Permission: p.permission}) - // case key.Matches(msg, permissionsKeys.AllowSession): - // return p, util.CmdHandler(PermissionResponseMsg{Action: PermissionAllowForSession, Permission: p.permission}) - // case key.Matches(msg, permissionsKeys.Deny): - // return p, util.CmdHandler(PermissionResponseMsg{Action: PermissionDeny, Permission: p.permission}) - // default: - // // Pass other keys to viewport - // viewPort, cmd := p.contentViewPort.Update(msg) - // p.contentViewPort = viewPort - // cmds = append(cmds, cmd) - // } - } - - return p, tea.Batch(cmds...) -} - -func (p *permissionDialogComponent) selectCurrentOption() tea.Cmd { - var action PermissionAction - - switch p.selectedOption { - case 0: - action = PermissionAllow - case 1: - action = PermissionAllowForSession - case 2: - action = PermissionDeny - } - - return util.CmdHandler(PermissionResponseMsg{Action: action}) // , Permission: p.permission}) -} - -func (p *permissionDialogComponent) renderButtons() string { - t := theme.CurrentTheme() - baseStyle := styles.NewStyle().Foreground(t.Text()) - - allowStyle := baseStyle - allowSessionStyle := baseStyle - denyStyle := baseStyle - spacerStyle := baseStyle.Background(t.Background()) - - // Style the selected button - switch p.selectedOption { - case 0: - allowStyle = allowStyle.Background(t.Primary()).Foreground(t.Background()) - allowSessionStyle = allowSessionStyle.Background(t.Background()).Foreground(t.Primary()) - denyStyle = denyStyle.Background(t.Background()).Foreground(t.Primary()) - case 1: - allowStyle = allowStyle.Background(t.Background()).Foreground(t.Primary()) - allowSessionStyle = allowSessionStyle.Background(t.Primary()).Foreground(t.Background()) - denyStyle = denyStyle.Background(t.Background()).Foreground(t.Primary()) - case 2: - allowStyle = allowStyle.Background(t.Background()).Foreground(t.Primary()) - allowSessionStyle = allowSessionStyle.Background(t.Background()).Foreground(t.Primary()) - denyStyle = denyStyle.Background(t.Primary()).Foreground(t.Background()) - } - - allowButton := allowStyle.Padding(0, 1).Render("Allow (a)") - allowSessionButton := allowSessionStyle.Padding(0, 1).Render("Allow for session (s)") - denyButton := denyStyle.Padding(0, 1).Render("Deny (d)") - - content := lipgloss.JoinHorizontal( - lipgloss.Left, - allowButton, - spacerStyle.Render(" "), - allowSessionButton, - spacerStyle.Render(" "), - denyButton, - spacerStyle.Render(" "), - ) - - remainingWidth := p.width - lipgloss.Width(content) - if remainingWidth > 0 { - content = spacerStyle.Render(strings.Repeat(" ", remainingWidth)) + content - } - return content -} - -func (p *permissionDialogComponent) renderHeader() string { - return "NOT IMPLEMENTED" - // t := theme.CurrentTheme() - // baseStyle := styles.BaseStyle() - // - // toolKey := baseStyle.Foreground(t.TextMuted()).Bold(true).Render("Tool") - // toolValue := baseStyle. - // Foreground(t.Text()). - // Width(p.width - lipgloss.Width(toolKey)). - // Render(fmt.Sprintf(": %s", p.permission.ToolName)) - // - // pathKey := baseStyle.Foreground(t.TextMuted()).Bold(true).Render("Path") - // - // // Get the current working directory to display relative path - // relativePath := p.permission.Path - // if filepath.IsAbs(relativePath) { - // if cwd, err := filepath.Rel(config.WorkingDirectory(), relativePath); err == nil { - // relativePath = cwd - // } - // } - // - // pathValue := baseStyle. - // Foreground(t.Text()). - // Width(p.width - lipgloss.Width(pathKey)). - // Render(fmt.Sprintf(": %s", relativePath)) - // - // headerParts := []string{ - // lipgloss.JoinHorizontal( - // lipgloss.Left, - // toolKey, - // toolValue, - // ), - // baseStyle.Render(strings.Repeat(" ", p.width)), - // lipgloss.JoinHorizontal( - // lipgloss.Left, - // pathKey, - // pathValue, - // ), - // baseStyle.Render(strings.Repeat(" ", p.width)), - // } - // - // // Add tool-specific header information - // switch p.permission.ToolName { - // case "bash": - // headerParts = append(headerParts, baseStyle.Foreground(t.TextMuted()).Width(p.width).Bold(true).Render("Command")) - // case "edit": - // headerParts = append(headerParts, baseStyle.Foreground(t.TextMuted()).Width(p.width).Bold(true).Render("Diff")) - // case "write": - // headerParts = append(headerParts, baseStyle.Foreground(t.TextMuted()).Width(p.width).Bold(true).Render("Diff")) - // case "fetch": - // headerParts = append(headerParts, baseStyle.Foreground(t.TextMuted()).Width(p.width).Bold(true).Render("URL")) - // } - // - // return lipgloss.NewStyle().Background(t.Background()).Render(lipgloss.JoinVertical(lipgloss.Left, headerParts...)) -} - -func (p *permissionDialogComponent) renderBashContent() string { - // t := theme.CurrentTheme() - // baseStyle := styles.BaseStyle() - // - // if pr, ok := p.permission.Params.(tools.BashPermissionsParams); ok { - // content := fmt.Sprintf("```bash\n%s\n```", pr.Command) - // - // // Use the cache for markdown rendering - // renderedContent := p.GetOrSetMarkdown(p.permission.ID, func() (string, error) { - // r := styles.GetMarkdownRenderer(p.width - 10) - // s, err := r.Render(content) - // return s - // }) - // - // finalContent := baseStyle. - // Width(p.contentViewPort.Width). - // Render(renderedContent) - // p.contentViewPort.SetContent(finalContent) - // return p.styleViewport() - // } - return "" -} - -func (p *permissionDialogComponent) renderEditContent() string { - // if pr, ok := p.permission.Params.(tools.EditPermissionsParams); ok { - // diff := p.GetOrSetDiff(p.permission.ID, func() (string, error) { - // return diff.FormatDiff(pr.Diff, diff.WithTotalWidth(p.contentViewPort.Width)) - // }) - // - // p.contentViewPort.SetContent(diff) - // return p.styleViewport() - // } - return "" -} - -func (p *permissionDialogComponent) renderPatchContent() string { - // if pr, ok := p.permission.Params.(tools.EditPermissionsParams); ok { - // diff := p.GetOrSetDiff(p.permission.ID, func() (string, error) { - // return diff.FormatDiff(pr.Diff, diff.WithTotalWidth(p.contentViewPort.Width)) - // }) - // - // p.contentViewPort.SetContent(diff) - // return p.styleViewport() - // } - return "" -} - -func (p *permissionDialogComponent) renderWriteContent() string { - // if pr, ok := p.permission.Params.(tools.WritePermissionsParams); ok { - // // Use the cache for diff rendering - // diff := p.GetOrSetDiff(p.permission.ID, func() (string, error) { - // return diff.FormatDiff(pr.Diff, diff.WithTotalWidth(p.contentViewPort.Width)) - // }) - // - // p.contentViewPort.SetContent(diff) - // return p.styleViewport() - // } - return "" -} - -func (p *permissionDialogComponent) renderFetchContent() string { - // t := theme.CurrentTheme() - // baseStyle := styles.BaseStyle() - // - // if pr, ok := p.permission.Params.(tools.FetchPermissionsParams); ok { - // content := fmt.Sprintf("```bash\n%s\n```", pr.URL) - // - // // Use the cache for markdown rendering - // renderedContent := p.GetOrSetMarkdown(p.permission.ID, func() (string, error) { - // r := styles.GetMarkdownRenderer(p.width - 10) - // s, err := r.Render(content) - // return s - // }) - // - // finalContent := baseStyle. - // Width(p.contentViewPort.Width). - // Render(renderedContent) - // p.contentViewPort.SetContent(finalContent) - // return p.styleViewport() - // } - return "" -} - -func (p *permissionDialogComponent) renderDefaultContent() string { - // t := theme.CurrentTheme() - // baseStyle := styles.BaseStyle() - // - // content := p.permission.Description - // - // // Use the cache for markdown rendering - // renderedContent := p.GetOrSetMarkdown(p.permission.ID, func() (string, error) { - // r := styles.GetMarkdownRenderer(p.width - 10) - // s, err := r.Render(content) - // return s - // }) - // - // finalContent := baseStyle. - // Width(p.contentViewPort.Width). - // Render(renderedContent) - // p.contentViewPort.SetContent(finalContent) - // - // if renderedContent == "" { - // return "" - // } - // - return p.styleViewport() -} - -func (p *permissionDialogComponent) styleViewport() string { - t := theme.CurrentTheme() - contentStyle := styles.NewStyle().Background(t.Background()) - - return contentStyle.Render(p.contentViewPort.View()) -} - -func (p *permissionDialogComponent) render() string { - return "NOT IMPLEMENTED" - // t := theme.CurrentTheme() - // baseStyle := styles.BaseStyle() - // - // title := baseStyle. - // Bold(true). - // Width(p.width - 4). - // Foreground(t.Primary()). - // Render("Permission Required") - // // Render header - // headerContent := p.renderHeader() - // // Render buttons - // buttons := p.renderButtons() - // - // // Calculate content height dynamically based on window size - // p.contentViewPort.Height = p.height - lipgloss.Height(headerContent) - lipgloss.Height(buttons) - 2 - lipgloss.Height(title) - // p.contentViewPort.Width = p.width - 4 - // - // // Render content based on tool type - // var contentFinal string - // switch p.permission.ToolName { - // case "bash": - // contentFinal = p.renderBashContent() - // case "edit": - // contentFinal = p.renderEditContent() - // case "patch": - // contentFinal = p.renderPatchContent() - // case "write": - // contentFinal = p.renderWriteContent() - // case "fetch": - // contentFinal = p.renderFetchContent() - // default: - // contentFinal = p.renderDefaultContent() - // } - // - // content := lipgloss.JoinVertical( - // lipgloss.Top, - // title, - // baseStyle.Render(strings.Repeat(" ", lipgloss.Width(title))), - // headerContent, - // contentFinal, - // buttons, - // baseStyle.Render(strings.Repeat(" ", p.width-4)), - // ) - // - // return baseStyle. - // Padding(1, 0, 0, 1). - // Border(lipgloss.RoundedBorder()). - // BorderBackground(t.Background()). - // BorderForeground(t.TextMuted()). - // Width(p.width). - // Height(p.height). - // Render( - // content, - // ) -} - -func (p *permissionDialogComponent) View() string { - return p.render() -} - -func (p *permissionDialogComponent) SetSize() tea.Cmd { - // if p.permission.ID == "" { - // return nil - // } - // switch p.permission.ToolName { - // case "bash": - // p.width = int(float64(p.windowSize.Width) * 0.4) - // p.height = int(float64(p.windowSize.Height) * 0.3) - // case "edit": - // p.width = int(float64(p.windowSize.Width) * 0.8) - // p.height = int(float64(p.windowSize.Height) * 0.8) - // case "write": - // p.width = int(float64(p.windowSize.Width) * 0.8) - // p.height = int(float64(p.windowSize.Height) * 0.8) - // case "fetch": - // p.width = int(float64(p.windowSize.Width) * 0.4) - // p.height = int(float64(p.windowSize.Height) * 0.3) - // default: - // p.width = int(float64(p.windowSize.Width) * 0.7) - // p.height = int(float64(p.windowSize.Height) * 0.5) - // } - return nil -} - -// func (p *permissionDialogCmp) SetPermissions(permission permission.PermissionRequest) tea.Cmd { -// p.permission = permission -// return p.SetSize() -// } - -// Helper to get or set cached diff content -func (c *permissionDialogComponent) GetOrSetDiff(key string, generator func() (string, error)) string { - if cached, ok := c.diffCache[key]; ok { - return cached - } - - content, err := generator() - if err != nil { - return fmt.Sprintf("Error formatting diff: %v", err) - } - - c.diffCache[key] = content - - return content -} - -// Helper to get or set cached markdown content -func (c *permissionDialogComponent) GetOrSetMarkdown(key string, generator func() (string, error)) string { - if cached, ok := c.markdownCache[key]; ok { - return cached - } - - content, err := generator() - if err != nil { - return fmt.Sprintf("Error rendering markdown: %v", err) - } - - c.markdownCache[key] = content - - return content -} - -func NewPermissionDialogCmp() PermissionDialogComponent { - // Create viewport for content - contentViewport := viewport.New() // (0, 0) - - return &permissionDialogComponent{ - contentViewPort: contentViewport, - selectedOption: 0, // Default to "Allow" - diffCache: make(map[string]string), - markdownCache: make(map[string]string), - } -} diff --git a/packages/tui/internal/components/dialog/search.go b/packages/tui/internal/components/dialog/search.go new file mode 100644 index 00000000..cdb2b824 --- /dev/null +++ b/packages/tui/internal/components/dialog/search.go @@ -0,0 +1,247 @@ +package dialog + +import ( + "github.com/charmbracelet/bubbles/v2/key" + "github.com/charmbracelet/bubbles/v2/textinput" + tea "github.com/charmbracelet/bubbletea/v2" + "github.com/charmbracelet/lipgloss/v2" + "github.com/sst/opencode/internal/components/list" + "github.com/sst/opencode/internal/styles" + "github.com/sst/opencode/internal/theme" +) + +// SearchQueryChangedMsg is emitted when the search query changes +type SearchQueryChangedMsg struct { + Query string +} + +// SearchSelectionMsg is emitted when an item is selected +type SearchSelectionMsg struct { + Item any + Index int +} + +// SearchCancelledMsg is emitted when the search is cancelled +type SearchCancelledMsg struct{} + +// SearchRemoveItemMsg is emitted when Ctrl+X is pressed to remove an item +type SearchRemoveItemMsg struct { + Item any + Index int +} + +// SearchDialog is a reusable component that combines a text input with a list +type SearchDialog struct { + textInput textinput.Model + list list.List[list.Item] + width int + height int + focused bool +} + +type searchKeyMap struct { + Up key.Binding + Down key.Binding + Enter key.Binding + Escape key.Binding + Remove key.Binding +} + +var searchKeys = searchKeyMap{ + Up: key.NewBinding( + key.WithKeys("up", "ctrl+p"), + key.WithHelp("↑", "previous item"), + ), + Down: key.NewBinding( + key.WithKeys("down", "ctrl+n"), + key.WithHelp("↓", "next item"), + ), + Enter: key.NewBinding( + key.WithKeys("enter"), + key.WithHelp("enter", "select"), + ), + Escape: key.NewBinding( + key.WithKeys("esc"), + key.WithHelp("esc", "cancel"), + ), + Remove: key.NewBinding( + key.WithKeys("ctrl+x"), + key.WithHelp("ctrl+x", "remove from recent"), + ), +} + +// NewSearchDialog creates a new SearchDialog +func NewSearchDialog(placeholder string, maxVisibleHeight int) *SearchDialog { + t := theme.CurrentTheme() + bgColor := t.BackgroundElement() + textColor := t.Text() + textMutedColor := t.TextMuted() + + ti := textinput.New() + ti.Placeholder = placeholder + ti.Styles.Blurred.Placeholder = styles.NewStyle(). + Foreground(textMutedColor). + Background(bgColor). + Lipgloss() + ti.Styles.Blurred.Text = styles.NewStyle(). + Foreground(textColor). + Background(bgColor). + Lipgloss() + ti.Styles.Focused.Placeholder = styles.NewStyle(). + Foreground(textMutedColor). + Background(bgColor). + Lipgloss() + ti.Styles.Focused.Text = styles.NewStyle(). + Foreground(textColor). + Background(bgColor). + Lipgloss() + ti.Styles.Focused.Prompt = styles.NewStyle(). + Background(bgColor). + Lipgloss() + ti.Styles.Cursor.Color = t.Primary() + ti.VirtualCursor = true + + ti.Prompt = " " + ti.CharLimit = -1 + ti.Focus() + + emptyList := list.NewListComponent( + list.WithItems([]list.Item{}), + list.WithMaxVisibleHeight[list.Item](maxVisibleHeight), + list.WithFallbackMessage[list.Item](" No items"), + list.WithAlphaNumericKeys[list.Item](false), + list.WithRenderFunc( + func(item list.Item, selected bool, width int, baseStyle styles.Style) string { + return item.Render(selected, width, baseStyle) + }, + ), + list.WithSelectableFunc(func(item list.Item) bool { + return item.Selectable() + }), + ) + + return &SearchDialog{ + textInput: ti, + list: emptyList, + focused: true, + } +} + +func (s *SearchDialog) Init() tea.Cmd { + return textinput.Blink +} + +func (s *SearchDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + var cmds []tea.Cmd + + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + case "ctrl+c": + value := s.textInput.Value() + if value == "" { + return s, nil + } + s.textInput.Reset() + cmds = append(cmds, func() tea.Msg { + return SearchQueryChangedMsg{Query: ""} + }) + } + + switch { + case key.Matches(msg, searchKeys.Escape): + return s, func() tea.Msg { return SearchCancelledMsg{} } + + case key.Matches(msg, searchKeys.Enter): + if selectedItem, idx := s.list.GetSelectedItem(); idx != -1 { + return s, func() tea.Msg { + return SearchSelectionMsg{Item: selectedItem, Index: idx} + } + } + + case key.Matches(msg, searchKeys.Remove): + if selectedItem, idx := s.list.GetSelectedItem(); idx != -1 { + return s, func() tea.Msg { + return SearchRemoveItemMsg{Item: selectedItem, Index: idx} + } + } + + case key.Matches(msg, searchKeys.Up): + var cmd tea.Cmd + listModel, cmd := s.list.Update(msg) + s.list = listModel.(list.List[list.Item]) + if cmd != nil { + cmds = append(cmds, cmd) + } + + case key.Matches(msg, searchKeys.Down): + var cmd tea.Cmd + listModel, cmd := s.list.Update(msg) + s.list = listModel.(list.List[list.Item]) + if cmd != nil { + cmds = append(cmds, cmd) + } + + default: + oldValue := s.textInput.Value() + var cmd tea.Cmd + s.textInput, cmd = s.textInput.Update(msg) + if cmd != nil { + cmds = append(cmds, cmd) + } + if newValue := s.textInput.Value(); newValue != oldValue { + cmds = append(cmds, func() tea.Msg { + return SearchQueryChangedMsg{Query: newValue} + }) + } + } + } + + return s, tea.Batch(cmds...) +} + +func (s *SearchDialog) View() string { + s.list.SetMaxWidth(s.width) + listView := s.list.View() + listView = lipgloss.PlaceVertical(s.list.GetMaxVisibleHeight(), lipgloss.Top, listView) + textinput := s.textInput.View() + return textinput + "\n\n" + listView +} + +// SetWidth sets the width of the search dialog +func (s *SearchDialog) SetWidth(width int) { + s.width = width + s.textInput.SetWidth(width - 2) // Account for padding and borders +} + +// SetHeight sets the height of the search dialog +func (s *SearchDialog) SetHeight(height int) { + s.height = height +} + +// SetItems updates the list items +func (s *SearchDialog) SetItems(items []list.Item) { + s.list.SetItems(items) +} + +// GetQuery returns the current search query +func (s *SearchDialog) GetQuery() string { + return s.textInput.Value() +} + +// SetQuery sets the search query +func (s *SearchDialog) SetQuery(query string) { + s.textInput.SetValue(query) +} + +// Focus focuses the search dialog +func (s *SearchDialog) Focus() { + s.focused = true + s.textInput.Focus() +} + +// Blur removes focus from the search dialog +func (s *SearchDialog) Blur() { + s.focused = false + s.textInput.Blur() +} diff --git a/packages/tui/internal/components/dialog/session.go b/packages/tui/internal/components/dialog/session.go index 6f18dba0..307897bc 100644 --- a/packages/tui/internal/components/dialog/session.go +++ b/packages/tui/internal/components/dialog/session.go @@ -28,17 +28,26 @@ type SessionDialog interface { type sessionItem struct { title string isDeleteConfirming bool + isCurrentSession bool } -func (s sessionItem) Render(selected bool, width int) string { +func (s sessionItem) Render( + selected bool, + width int, + isFirstInViewport bool, + baseStyle styles.Style, +) string { t := theme.CurrentTheme() - baseStyle := styles.NewStyle() var text string if s.isDeleteConfirming { text = "Press again to confirm delete" } else { - text = s.title + if s.isCurrentSession { + text = "● " + s.title + } else { + text = s.title + } } truncatedStr := truncate.StringWithTail(text, uint(width-1), "...") @@ -52,6 +61,14 @@ func (s sessionItem) Render(selected bool, width int) string { Foreground(t.BackgroundElement()). Width(width). PaddingLeft(1) + } else if s.isCurrentSession { + // Different style for current session when selected + itemStyle = baseStyle. + Background(t.Primary()). + Foreground(t.BackgroundElement()). + Width(width). + PaddingLeft(1). + Bold(true) } else { // Normal selection itemStyle = baseStyle. @@ -66,6 +83,12 @@ func (s sessionItem) Render(selected bool, width int) string { itemStyle = baseStyle. Foreground(t.Error()). PaddingLeft(1) + } else if s.isCurrentSession { + // Highlight current session when not selected + itemStyle = baseStyle. + Foreground(t.Primary()). + PaddingLeft(1). + Bold(true) } else { itemStyle = baseStyle. PaddingLeft(1) @@ -75,6 +98,10 @@ func (s sessionItem) Render(selected bool, width int) string { return itemStyle.Render(truncatedStr) } +func (s sessionItem) Selectable() bool { + return true +} + type sessionDialog struct { width int height int @@ -110,6 +137,13 @@ func (s *sessionDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) { util.CmdHandler(app.SessionSelectedMsg(&selectedSession)), ) } + case "n": + s.app.Session = &opencode.Session{} + s.app.Messages = []app.Message{} + return s, tea.Sequence( + util.CmdHandler(modal.CloseModalMsg{}), + util.CmdHandler(app.SessionClearedMsg{}), + ) case "x", "delete", "backspace": if _, idx := s.list.GetSelectedItem(); idx >= 0 && idx < len(s.sessions) { if s.deleteConfirmation == idx { @@ -150,10 +184,21 @@ func (s *sessionDialog) Render(background string) string { listView := s.list.View() t := theme.CurrentTheme() - helpStyle := styles.NewStyle().PaddingLeft(1).PaddingTop(1) - helpText := styles.NewStyle().Foreground(t.Text()).Render("x/del") - helpText = helpText + styles.NewStyle().Background(t.BackgroundElement()).Foreground(t.TextMuted()).Render(" delete session") - helpText = helpStyle.Render(helpText) + keyStyle := styles.NewStyle().Foreground(t.Text()).Background(t.BackgroundPanel()).Render + mutedStyle := styles.NewStyle().Foreground(t.TextMuted()).Background(t.BackgroundPanel()).Render + + leftHelp := keyStyle("n") + mutedStyle(" new session") + rightHelp := keyStyle("x/del") + mutedStyle(" delete session") + + bgColor := t.BackgroundPanel() + helpText := layout.Render(layout.FlexOptions{ + Direction: layout.Row, + Justify: layout.JustifySpaceBetween, + Width: layout.Current.Container.Width - 14, + Background: &bgColor, + }, layout.FlexItem{View: leftHelp}, layout.FlexItem{View: rightHelp}) + + helpText = styles.NewStyle().PaddingLeft(1).PaddingTop(1).Render(helpText) content := strings.Join([]string{listView, helpText}, "\n") @@ -168,6 +213,7 @@ func (s *sessionDialog) updateListItems() { item := sessionItem{ title: sess.Title, isDeleteConfirming: s.deleteConfirmation == i, + isCurrentSession: s.app.Session != nil && s.app.Session.ID == sess.ID, } items = append(items, item) } @@ -203,15 +249,23 @@ func NewSessionDialog(app *app.App) SessionDialog { items = append(items, sessionItem{ title: sess.Title, isDeleteConfirming: false, + isCurrentSession: app.Session != nil && app.Session.ID == sess.ID, }) } - // Create a generic list component listComponent := list.NewListComponent( - items, - 10, // maxVisibleSessions - "No sessions available", - true, // useAlphaNumericKeys + list.WithItems(items), + list.WithMaxVisibleHeight[sessionItem](10), + list.WithFallbackMessage[sessionItem]("No sessions available"), + list.WithAlphaNumericKeys[sessionItem](true), + list.WithRenderFunc( + func(item sessionItem, selected bool, width int, baseStyle styles.Style) string { + return item.Render(selected, width, false, baseStyle) + }, + ), + list.WithSelectableFunc(func(item sessionItem) bool { + return true + }), ) listComponent.SetMaxWidth(layout.Current.Container.Width - 12) diff --git a/packages/tui/internal/components/dialog/theme.go b/packages/tui/internal/components/dialog/theme.go index b6e97061..c71cddc8 100644 --- a/packages/tui/internal/components/dialog/theme.go +++ b/packages/tui/internal/components/dialog/theme.go @@ -5,6 +5,7 @@ import ( list "github.com/sst/opencode/internal/components/list" "github.com/sst/opencode/internal/components/modal" "github.com/sst/opencode/internal/layout" + "github.com/sst/opencode/internal/styles" "github.com/sst/opencode/internal/theme" "github.com/sst/opencode/internal/util" ) @@ -24,7 +25,7 @@ type themeDialog struct { height int modal *modal.Modal - list list.List[list.StringItem] + list list.List[list.Item] originalTheme string themeApplied bool } @@ -42,16 +43,18 @@ func (t *themeDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg.String() { case "enter": if item, idx := t.list.GetSelectedItem(); idx >= 0 { - selectedTheme := string(item) - if err := theme.SetTheme(selectedTheme); err != nil { - // status.Error(err.Error()) - return t, nil + if stringItem, ok := item.(list.StringItem); ok { + selectedTheme := string(stringItem) + if err := theme.SetTheme(selectedTheme); err != nil { + // status.Error(err.Error()) + return t, nil + } + t.themeApplied = true + return t, tea.Sequence( + util.CmdHandler(modal.CloseModalMsg{}), + util.CmdHandler(ThemeSelectedMsg{ThemeName: selectedTheme}), + ) } - t.themeApplied = true - return t, tea.Sequence( - util.CmdHandler(modal.CloseModalMsg{}), - util.CmdHandler(ThemeSelectedMsg{ThemeName: selectedTheme}), - ) } } @@ -61,11 +64,13 @@ func (t *themeDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd listModel, cmd := t.list.Update(msg) - t.list = listModel.(list.List[list.StringItem]) + t.list = listModel.(list.List[list.Item]) if item, newIdx := t.list.GetSelectedItem(); newIdx >= 0 && newIdx != prevIdx { - theme.SetTheme(string(item)) - return t, util.CmdHandler(ThemeSelectedMsg{ThemeName: string(item)}) + if stringItem, ok := item.(list.StringItem); ok { + theme.SetTheme(string(stringItem)) + return t, util.CmdHandler(ThemeSelectedMsg{ThemeName: string(stringItem)}) + } } return t, cmd } @@ -94,21 +99,32 @@ func NewThemeDialog() ThemeDialog { } } - list := list.NewStringList( - themes, - 10, // maxVisibleThemes - "No themes available", - true, + // Convert themes to list items + items := make([]list.Item, len(themes)) + for i, theme := range themes { + items[i] = list.StringItem(theme) + } + + listComponent := list.NewListComponent( + list.WithItems(items), + list.WithMaxVisibleHeight[list.Item](10), + list.WithFallbackMessage[list.Item]("No themes available"), + list.WithAlphaNumericKeys[list.Item](true), + list.WithRenderFunc(func(item list.Item, selected bool, width int, baseStyle styles.Style) string { + return item.Render(selected, width, baseStyle) + }), + list.WithSelectableFunc(func(item list.Item) bool { + return item.Selectable() + }), ) // Set the initial selection to the current theme - list.SetSelectedIndex(selectedIdx) + listComponent.SetSelectedIndex(selectedIdx) // Set the max width for the list to match the modal width - list.SetMaxWidth(36) // 40 (modal max width) - 4 (modal padding) - + listComponent.SetMaxWidth(36) // 40 (modal max width) - 4 (modal padding) return &themeDialog{ - list: list, + list: listComponent, modal: modal.New(modal.WithTitle("Select Theme"), modal.WithMaxWidth(40)), originalTheme: currentTheme, themeApplied: false, diff --git a/packages/tui/internal/components/diff/diff.go b/packages/tui/internal/components/diff/diff.go index 03f58cc2..da2e007c 100644 --- a/packages/tui/internal/components/diff/diff.go +++ b/packages/tui/internal/components/diff/diff.go @@ -38,6 +38,10 @@ const ( LineRemoved // Line removed from the old file ) +var ( + ansiRegex = regexp.MustCompile(`\x1b(?:[@-Z\\-_]|\[[0-9?]*(?:;[0-9?]*)*[@-~])`) +) + // Segment represents a portion of a line for intra-line highlighting type Segment struct { Start int @@ -548,7 +552,6 @@ func createStyles(t theme.Theme) (removedLineStyle, addedLineStyle, contextLineS // applyHighlighting applies intra-line highlighting to a piece of text func applyHighlighting(content string, segments []Segment, segmentType LineType, highlightBg compat.AdaptiveColor) string { // Find all ANSI sequences in the content - ansiRegex := regexp.MustCompile(`\x1b(?:[@-Z\\-_]|\[[0-9?]*(?:;[0-9?]*)*[@-~])`) ansiMatches := ansiRegex.FindAllStringIndex(content, -1) // Build a mapping of visible character positions to their actual indices diff --git a/packages/tui/internal/components/list/list.go b/packages/tui/internal/components/list/list.go index 16bc73ca..fd2d7d93 100644 --- a/packages/tui/internal/components/list/list.go +++ b/packages/tui/internal/components/list/list.go @@ -5,16 +5,88 @@ import ( "github.com/charmbracelet/bubbles/v2/key" tea "github.com/charmbracelet/bubbletea/v2" + "github.com/charmbracelet/lipgloss/v2" "github.com/muesli/reflow/truncate" "github.com/sst/opencode/internal/styles" "github.com/sst/opencode/internal/theme" ) -type ListItem interface { - Render(selected bool, width int) string +// Item interface that all list items must implement +type Item interface { + Render(selected bool, width int, baseStyle styles.Style) string + Selectable() bool } -type List[T ListItem] interface { +// RenderFunc defines how to render an item in the list +type RenderFunc[T any] func(item T, selected bool, width int, baseStyle styles.Style) string + +// SelectableFunc defines whether an item is selectable +type SelectableFunc[T any] func(item T) bool + +// Options holds configuration for the list component +type Options[T any] struct { + items []T + maxVisibleHeight int + fallbackMsg string + useAlphaNumericKeys bool + renderItem RenderFunc[T] + isSelectable SelectableFunc[T] + baseStyle styles.Style +} + +// Option is a function that configures the list component +type Option[T any] func(*Options[T]) + +// WithItems sets the initial items for the list +func WithItems[T any](items []T) Option[T] { + return func(o *Options[T]) { + o.items = items + } +} + +// WithMaxVisibleHeight sets the maximum visible height in lines +func WithMaxVisibleHeight[T any](height int) Option[T] { + return func(o *Options[T]) { + o.maxVisibleHeight = height + } +} + +// WithFallbackMessage sets the message to show when the list is empty +func WithFallbackMessage[T any](msg string) Option[T] { + return func(o *Options[T]) { + o.fallbackMsg = msg + } +} + +// WithAlphaNumericKeys enables j/k navigation keys +func WithAlphaNumericKeys[T any](enabled bool) Option[T] { + return func(o *Options[T]) { + o.useAlphaNumericKeys = enabled + } +} + +// WithRenderFunc sets the function to render items +func WithRenderFunc[T any](fn RenderFunc[T]) Option[T] { + return func(o *Options[T]) { + o.renderItem = fn + } +} + +// WithSelectableFunc sets the function to determine if items are selectable +func WithSelectableFunc[T any](fn SelectableFunc[T]) Option[T] { + return func(o *Options[T]) { + o.isSelectable = fn + } +} + +// WithStyle sets the base style that gets passed to render functions +func WithStyle[T any](style styles.Style) Option[T] { + return func(o *Options[T]) { + o.baseStyle = style + } +} + +type List[T any] interface { tea.Model tea.ViewModel SetMaxWidth(maxWidth int) @@ -24,17 +96,21 @@ type List[T ListItem] interface { SetSelectedIndex(idx int) SetEmptyMessage(msg string) IsEmpty() bool + GetMaxVisibleHeight() int } -type listComponent[T ListItem] struct { +type listComponent[T any] struct { fallbackMsg string items []T selectedIdx int maxWidth int - maxVisibleItems int + maxVisibleHeight int useAlphaNumericKeys bool width int height int + renderItem RenderFunc[T] + isSelectable SelectableFunc[T] + baseStyle styles.Style } type listKeyMap struct { @@ -46,11 +122,11 @@ type listKeyMap struct { var simpleListKeys = listKeyMap{ Up: key.NewBinding( - key.WithKeys("up"), + key.WithKeys("up", "ctrl+p"), key.WithHelp("↑", "previous list item"), ), Down: key.NewBinding( - key.WithKeys("down"), + key.WithKeys("down", "ctrl+n"), key.WithHelp("↓", "next list item"), ), UpAlpha: key.NewBinding( @@ -72,14 +148,10 @@ func (c *listComponent[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch { case key.Matches(msg, simpleListKeys.Up) || (c.useAlphaNumericKeys && key.Matches(msg, simpleListKeys.UpAlpha)): - if c.selectedIdx > 0 { - c.selectedIdx-- - } + c.moveUp() return c, nil case key.Matches(msg, simpleListKeys.Down) || (c.useAlphaNumericKeys && key.Matches(msg, simpleListKeys.DownAlpha)): - if c.selectedIdx < len(c.items)-1 { - c.selectedIdx++ - } + c.moveDown() return c, nil } } @@ -87,8 +159,50 @@ func (c *listComponent[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return c, nil } +// moveUp moves the selection up, skipping non-selectable items +func (c *listComponent[T]) moveUp() { + if len(c.items) == 0 { + return + } + + // Find the previous selectable item + for i := c.selectedIdx - 1; i >= 0; i-- { + if c.isSelectable(c.items[i]) { + c.selectedIdx = i + return + } + } + + // If no selectable item found above, stay at current position +} + +// moveDown moves the selection down, skipping non-selectable items +func (c *listComponent[T]) moveDown() { + if len(c.items) == 0 { + return + } + + originalIdx := c.selectedIdx + for { + if c.selectedIdx < len(c.items)-1 { + c.selectedIdx++ + } else { + break + } + + if c.isSelectable(c.items[c.selectedIdx]) { + return + } + + // Prevent infinite loop + if c.selectedIdx == originalIdx { + break + } + } +} + func (c *listComponent[T]) GetSelectedItem() (T, int) { - if len(c.items) > 0 { + if len(c.items) > 0 && c.isSelectable(c.items[c.selectedIdx]) { return c.items[c.selectedIdx], c.selectedIdx } @@ -97,8 +211,13 @@ func (c *listComponent[T]) GetSelectedItem() (T, int) { } func (c *listComponent[T]) SetItems(items []T) { - c.selectedIdx = 0 c.items = items + c.selectedIdx = 0 + + // Ensure initial selection is on a selectable item + if len(items) > 0 && !c.isSelectable(items[0]) { + c.moveDown() + } } func (c *listComponent[T]) GetItems() []T { @@ -123,62 +242,145 @@ func (c *listComponent[T]) SetSelectedIndex(idx int) { } } +func (c *listComponent[T]) GetMaxVisibleHeight() int { + return c.maxVisibleHeight +} + func (c *listComponent[T]) View() string { items := c.items maxWidth := c.maxWidth if maxWidth == 0 { maxWidth = 80 // Default width if not set } - maxVisibleItems := min(c.maxVisibleItems, len(items)) - startIdx := 0 if len(items) <= 0 { return c.fallbackMsg } - if len(items) > maxVisibleItems { - halfVisible := maxVisibleItems / 2 - if c.selectedIdx >= halfVisible && c.selectedIdx < len(items)-halfVisible { - startIdx = c.selectedIdx - halfVisible - } else if c.selectedIdx >= len(items)-halfVisible { - startIdx = len(items) - maxVisibleItems - } - } + // Calculate viewport based on actual heights + startIdx, endIdx := c.calculateViewport() - endIdx := min(startIdx+maxVisibleItems, len(items)) - - listItems := make([]string, 0, maxVisibleItems) + listItems := make([]string, 0, endIdx-startIdx) for i := startIdx; i < endIdx; i++ { item := items[i] - title := item.Render(i == c.selectedIdx, maxWidth) + + // Special handling for HeaderItem to remove top margin on first item + if i == startIdx { + // Check if this is a HeaderItem + if _, ok := any(item).(Item); ok { + if headerItem, isHeader := any(item).(HeaderItem); isHeader { + // Render header without top margin when it's first + t := theme.CurrentTheme() + truncatedStr := truncate.StringWithTail(string(headerItem), uint(maxWidth-1), "...") + headerStyle := c.baseStyle. + Foreground(t.Accent()). + Bold(true). + MarginBottom(0). + PaddingLeft(1) + listItems = append(listItems, headerStyle.Render(truncatedStr)) + continue + } + } + } + + title := c.renderItem(item, i == c.selectedIdx, maxWidth, c.baseStyle) listItems = append(listItems, title) } return strings.Join(listItems, "\n") } -func NewListComponent[T ListItem]( - items []T, - maxVisibleItems int, - fallbackMsg string, - useAlphaNumericKeys bool, -) List[T] { +// calculateViewport determines which items to show based on available space +func (c *listComponent[T]) calculateViewport() (startIdx, endIdx int) { + items := c.items + if len(items) == 0 { + return 0, 0 + } + + // Calculate heights of all items + itemHeights := make([]int, len(items)) + for i, item := range items { + rendered := c.renderItem(item, false, c.maxWidth, c.baseStyle) + itemHeights[i] = lipgloss.Height(rendered) + } + + // Find the range of items that fit within maxVisibleHeight + // Start by trying to center the selected item + start := 0 + end := len(items) + + // Calculate height from start to selected + heightToSelected := 0 + for i := 0; i <= c.selectedIdx && i < len(items); i++ { + heightToSelected += itemHeights[i] + } + + // If selected item is beyond visible height, scroll to show it + if heightToSelected > c.maxVisibleHeight { + // Start from selected and work backwards to find start + currentHeight := itemHeights[c.selectedIdx] + start = c.selectedIdx + + for i := c.selectedIdx - 1; i >= 0 && currentHeight+itemHeights[i] <= c.maxVisibleHeight; i-- { + currentHeight += itemHeights[i] + start = i + } + } + + // Calculate end based on start + currentHeight := 0 + for i := start; i < len(items); i++ { + if currentHeight+itemHeights[i] > c.maxVisibleHeight { + end = i + break + } + currentHeight += itemHeights[i] + } + + return start, end +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func NewListComponent[T any](opts ...Option[T]) List[T] { + options := &Options[T]{ + baseStyle: styles.NewStyle(), // Default empty style + } + + for _, opt := range opts { + opt(options) + } + return &listComponent[T]{ - fallbackMsg: fallbackMsg, - items: items, - maxVisibleItems: maxVisibleItems, - useAlphaNumericKeys: useAlphaNumericKeys, + fallbackMsg: options.fallbackMsg, + items: options.items, + maxVisibleHeight: options.maxVisibleHeight, + useAlphaNumericKeys: options.useAlphaNumericKeys, selectedIdx: 0, + renderItem: options.renderItem, + isSelectable: options.isSelectable, + baseStyle: options.baseStyle, } } -// StringItem is a simple implementation of ListItem for string values +// StringItem is a simple implementation of Item for string values type StringItem string -func (s StringItem) Render(selected bool, width int) string { +func (s StringItem) Render(selected bool, width int, baseStyle styles.Style) string { t := theme.CurrentTheme() - baseStyle := styles.NewStyle() truncatedStr := truncate.StringWithTail(string(s), uint(width-1), "...") @@ -198,16 +400,32 @@ func (s StringItem) Render(selected bool, width int) string { return itemStyle.Render(truncatedStr) } -// NewStringList creates a new list component with string items -func NewStringList( - items []string, - maxVisibleItems int, - fallbackMsg string, - useAlphaNumericKeys bool, -) List[StringItem] { - stringItems := make([]StringItem, len(items)) - for i, item := range items { - stringItems[i] = StringItem(item) - } - return NewListComponent(stringItems, maxVisibleItems, fallbackMsg, useAlphaNumericKeys) +func (s StringItem) Selectable() bool { + return true } + +// HeaderItem is a non-selectable header item for grouping +type HeaderItem string + +func (h HeaderItem) Render(selected bool, width int, baseStyle styles.Style) string { + t := theme.CurrentTheme() + + truncatedStr := truncate.StringWithTail(string(h), uint(width-1), "...") + + headerStyle := baseStyle. + Foreground(t.Accent()). + Bold(true). + MarginTop(1). + MarginBottom(0). + PaddingLeft(1) + + return headerStyle.Render(truncatedStr) +} + +func (h HeaderItem) Selectable() bool { + return false +} + +// Ensure StringItem and HeaderItem implement Item +var _ Item = StringItem("") +var _ Item = HeaderItem("") diff --git a/packages/tui/internal/components/list/list_test.go b/packages/tui/internal/components/list/list_test.go new file mode 100644 index 00000000..4d954409 --- /dev/null +++ b/packages/tui/internal/components/list/list_test.go @@ -0,0 +1,219 @@ +package list + +import ( + "testing" + + tea "github.com/charmbracelet/bubbletea/v2" + "github.com/sst/opencode/internal/styles" +) + +// testItem is a simple test implementation of ListItem +type testItem struct { + value string +} + +func (t testItem) Render( + selected bool, + width int, + isFirstInViewport bool, + baseStyle styles.Style, +) string { + return t.value +} + +func (t testItem) Selectable() bool { + return true +} + +// createTestList creates a list with test items for testing +func createTestList() *listComponent[testItem] { + items := []testItem{ + {value: "item1"}, + {value: "item2"}, + {value: "item3"}, + } + list := NewListComponent( + WithItems(items), + WithMaxVisibleItems[testItem](5), + WithFallbackMessage[testItem]("empty"), + WithAlphaNumericKeys[testItem](false), + WithRenderFunc( + func(item testItem, selected bool, width int, baseStyle styles.Style) string { + return item.Render(selected, width, false, baseStyle) + }, + ), + WithSelectableFunc(func(item testItem) bool { + return item.Selectable() + }), + WithHeightFunc(func(item testItem, isFirstInViewport bool) int { + return 1 + }), + ) + + return list.(*listComponent[testItem]) +} + +func TestArrowKeyNavigation(t *testing.T) { + list := createTestList() + + // Test down arrow navigation + downKey := tea.KeyPressMsg{Code: tea.KeyDown} + updatedModel, _ := list.Update(downKey) + list = updatedModel.(*listComponent[testItem]) + _, idx := list.GetSelectedItem() + if idx != 1 { + t.Errorf("Expected selected index 1 after down arrow, got %d", idx) + } + + // Test up arrow navigation + upKey := tea.KeyPressMsg{Code: tea.KeyUp} + updatedModel, _ = list.Update(upKey) + list = updatedModel.(*listComponent[testItem]) + _, idx = list.GetSelectedItem() + if idx != 0 { + t.Errorf("Expected selected index 0 after up arrow, got %d", idx) + } +} + +func TestJKKeyNavigation(t *testing.T) { + items := []testItem{ + {value: "item1"}, + {value: "item2"}, + {value: "item3"}, + } + // Create list with alpha keys enabled + list := NewListComponent( + WithItems(items), + WithMaxVisibleItems[testItem](5), + WithFallbackMessage[testItem]("empty"), + WithAlphaNumericKeys[testItem](true), + WithRenderFunc( + func(item testItem, selected bool, width int, baseStyle styles.Style) string { + return item.Render(selected, width, false, baseStyle) + }, + ), + WithSelectableFunc(func(item testItem) bool { + return item.Selectable() + }), + WithHeightFunc(func(item testItem, isFirstInViewport bool) int { + return 1 + }), + ) + + // Test j key (down) + jKey := tea.KeyPressMsg{Code: 'j', Text: "j"} + updatedModel, _ := list.Update(jKey) + list = updatedModel.(*listComponent[testItem]) + _, idx := list.GetSelectedItem() + if idx != 1 { + t.Errorf("Expected selected index 1 after 'j' key, got %d", idx) + } + + // Test k key (up) + kKey := tea.KeyPressMsg{Code: 'k', Text: "k"} + updatedModel, _ = list.Update(kKey) + list = updatedModel.(*listComponent[testItem]) + _, idx = list.GetSelectedItem() + if idx != 0 { + t.Errorf("Expected selected index 0 after 'k' key, got %d", idx) + } +} + +func TestCtrlNavigation(t *testing.T) { + list := createTestList() + + // Test Ctrl-N (down) + ctrlN := tea.KeyPressMsg{Code: 'n', Mod: tea.ModCtrl} + updatedModel, _ := list.Update(ctrlN) + list = updatedModel.(*listComponent[testItem]) + _, idx := list.GetSelectedItem() + if idx != 1 { + t.Errorf("Expected selected index 1 after Ctrl-N, got %d", idx) + } + + // Test Ctrl-P (up) + ctrlP := tea.KeyPressMsg{Code: 'p', Mod: tea.ModCtrl} + updatedModel, _ = list.Update(ctrlP) + list = updatedModel.(*listComponent[testItem]) + _, idx = list.GetSelectedItem() + if idx != 0 { + t.Errorf("Expected selected index 0 after Ctrl-P, got %d", idx) + } +} + +func TestNavigationBoundaries(t *testing.T) { + list := createTestList() + + // Test up arrow at first item (should stay at 0) + upKey := tea.KeyPressMsg{Code: tea.KeyUp} + updatedModel, _ := list.Update(upKey) + list = updatedModel.(*listComponent[testItem]) + _, idx := list.GetSelectedItem() + if idx != 0 { + t.Errorf("Expected to stay at index 0 when pressing up at first item, got %d", idx) + } + + // Move to last item + downKey := tea.KeyPressMsg{Code: tea.KeyDown} + updatedModel, _ = list.Update(downKey) + list = updatedModel.(*listComponent[testItem]) + updatedModel, _ = list.Update(downKey) + list = updatedModel.(*listComponent[testItem]) + _, idx = list.GetSelectedItem() + if idx != 2 { + t.Errorf("Expected to be at index 2, got %d", idx) + } + + // Test down arrow at last item (should stay at 2) + updatedModel, _ = list.Update(downKey) + list = updatedModel.(*listComponent[testItem]) + _, idx = list.GetSelectedItem() + if idx != 2 { + t.Errorf("Expected to stay at index 2 when pressing down at last item, got %d", idx) + } +} + +func TestEmptyList(t *testing.T) { + emptyList := NewListComponent( + WithItems([]testItem{}), + WithMaxVisibleItems[testItem](5), + WithFallbackMessage[testItem]("empty"), + WithAlphaNumericKeys[testItem](false), + WithRenderFunc( + func(item testItem, selected bool, width int, baseStyle styles.Style) string { + return item.Render(selected, width, false, baseStyle) + }, + ), + WithSelectableFunc(func(item testItem) bool { + return item.Selectable() + }), + WithHeightFunc(func(item testItem, isFirstInViewport bool) int { + return 1 + }), + ) + + // Test navigation on empty list (should not crash) + downKey := tea.KeyPressMsg{Code: tea.KeyDown} + upKey := tea.KeyPressMsg{Code: tea.KeyUp} + ctrlN := tea.KeyPressMsg{Code: 'n', Mod: tea.ModCtrl} + ctrlP := tea.KeyPressMsg{Code: 'p', Mod: tea.ModCtrl} + + updatedModel, _ := emptyList.Update(downKey) + emptyList = updatedModel.(*listComponent[testItem]) + updatedModel, _ = emptyList.Update(upKey) + emptyList = updatedModel.(*listComponent[testItem]) + updatedModel, _ = emptyList.Update(ctrlN) + emptyList = updatedModel.(*listComponent[testItem]) + updatedModel, _ = emptyList.Update(ctrlP) + emptyList = updatedModel.(*listComponent[testItem]) + + // Verify empty list behavior + _, idx := emptyList.GetSelectedItem() + if idx != -1 { + t.Errorf("Expected index -1 for empty list, got %d", idx) + } + + if !emptyList.IsEmpty() { + t.Error("Expected IsEmpty() to return true for empty list") + } +} diff --git a/packages/tui/internal/components/modal/modal.go b/packages/tui/internal/components/modal/modal.go index 5c2fbf8b..09989d8e 100644 --- a/packages/tui/internal/components/modal/modal.go +++ b/packages/tui/internal/components/modal/modal.go @@ -95,7 +95,7 @@ func (m *Modal) Render(contentView string, background string) string { var finalContent string if m.title != "" { titleStyle := baseStyle. - Foreground(t.Primary()). + Foreground(t.Text()). Bold(true). Padding(0, 1) @@ -140,6 +140,6 @@ func (m *Modal) Render(contentView string, background string) string { modalView, background, layout.WithOverlayBorder(), - layout.WithOverlayBorderColor(t.Primary()), + layout.WithOverlayBorderColor(t.BorderActive()), ) } diff --git a/packages/tui/internal/components/status/status.go b/packages/tui/internal/components/status/status.go index 791267e0..8ab54277 100644 --- a/packages/tui/internal/components/status/status.go +++ b/packages/tui/internal/components/status/status.go @@ -1,12 +1,14 @@ package status import ( - "fmt" + "os" "strings" tea "github.com/charmbracelet/bubbletea/v2" "github.com/charmbracelet/lipgloss/v2" + "github.com/charmbracelet/lipgloss/v2/compat" "github.com/sst/opencode/internal/app" + "github.com/sst/opencode/internal/commands" "github.com/sst/opencode/internal/styles" "github.com/sst/opencode/internal/theme" ) @@ -19,6 +21,7 @@ type StatusComponent interface { type statusComponent struct { app *app.App width int + cwd string } func (m statusComponent) Init() tea.Cmd { @@ -52,38 +55,6 @@ func (m statusComponent) logo() string { Render(open + code + version) } -func formatTokensAndCost(tokens float64, contextWindow float64, cost float64) string { - // Format tokens in human-readable format (e.g., 110K, 1.2M) - var formattedTokens string - switch { - case tokens >= 1_000_000: - formattedTokens = fmt.Sprintf("%.1fM", float64(tokens)/1_000_000) - case tokens >= 1_000: - formattedTokens = fmt.Sprintf("%.1fK", float64(tokens)/1_000) - default: - formattedTokens = fmt.Sprintf("%d", int(tokens)) - } - - // Remove .0 suffix if present - if strings.HasSuffix(formattedTokens, ".0K") { - formattedTokens = strings.Replace(formattedTokens, ".0K", "K", 1) - } - if strings.HasSuffix(formattedTokens, ".0M") { - formattedTokens = strings.Replace(formattedTokens, ".0M", "M", 1) - } - - // Format cost with $ symbol and 2 decimal places - formattedCost := fmt.Sprintf("$%.2f", cost) - percentage := (float64(tokens) / float64(contextWindow)) * 100 - - return fmt.Sprintf( - "Context: %s (%d%%), Cost: %s", - formattedTokens, - int(percentage), - formattedCost, - ) -} - func (m statusComponent) View() string { t := theme.CurrentTheme() logo := m.logo() @@ -92,46 +63,70 @@ func (m statusComponent) View() string { Foreground(t.TextMuted()). Background(t.BackgroundPanel()). Padding(0, 1). - Render(m.app.Info.Path.Cwd) + Render(m.cwd) - sessionInfo := "" - if m.app.Session.ID != "" { - tokens := float64(0) - cost := float64(0) - contextWindow := m.app.Model.Limit.Context - - for _, message := range m.app.Messages { - cost += message.Metadata.Assistant.Cost - usage := message.Metadata.Assistant.Tokens - if usage.Output > 0 { - if message.Metadata.Assistant.Summary { - tokens = usage.Output - continue - } - tokens = (usage.Input + - usage.Cache.Write + - usage.Cache.Read + - usage.Output + - usage.Reasoning) - } - } - - sessionInfo = styles.NewStyle(). - Foreground(t.TextMuted()). - Background(t.BackgroundElement()). - Padding(0, 1). - Render(formatTokensAndCost(tokens, contextWindow, cost)) + var modeBackground compat.AdaptiveColor + var modeForeground compat.AdaptiveColor + switch m.app.ModeIndex { + case 0: + modeBackground = t.BackgroundElement() + modeForeground = t.TextMuted() + case 1: + modeBackground = t.Secondary() + modeForeground = t.BackgroundPanel() + case 2: + modeBackground = t.Accent() + modeForeground = t.BackgroundPanel() + case 3: + modeBackground = t.Success() + modeForeground = t.BackgroundPanel() + case 4: + modeBackground = t.Warning() + modeForeground = t.BackgroundPanel() + case 5: + modeBackground = t.Primary() + modeForeground = t.BackgroundPanel() + case 6: + modeBackground = t.Error() + modeForeground = t.BackgroundPanel() + default: + modeBackground = t.Secondary() + modeForeground = t.BackgroundPanel() } - // diagnostics := styles.Padded().Background(t.BackgroundElement()).Render(m.projectDiagnostics()) + command := m.app.Commands[commands.SwitchModeCommand] + kb := command.Keybindings[0] + key := kb.Key + if kb.RequiresLeader { + key = m.app.Config.Keybinds.Leader + " " + kb.Key + } + + modeStyle := styles.NewStyle().Background(modeBackground).Foreground(modeForeground) + modeNameStyle := modeStyle.Bold(true).Render + modeDescStyle := modeStyle.Render + mode := modeNameStyle(strings.ToUpper(m.app.Mode.Name)) + modeDescStyle(" MODE") + mode = modeStyle. + Padding(0, 1). + BorderLeft(true). + BorderStyle(lipgloss.ThickBorder()). + BorderForeground(modeBackground). + BorderBackground(t.BackgroundPanel()). + Render(mode) + + mode = styles.NewStyle(). + Faint(true). + Background(t.BackgroundPanel()). + Foreground(t.TextMuted()). + Render(key+" ") + + mode space := max( 0, - m.width-lipgloss.Width(logo)-lipgloss.Width(cwd)-lipgloss.Width(sessionInfo), + m.width-lipgloss.Width(logo)-lipgloss.Width(cwd)-lipgloss.Width(mode), ) spacer := styles.NewStyle().Background(t.BackgroundPanel()).Width(space).Render("") - status := logo + cwd + spacer + sessionInfo + status := logo + cwd + spacer + mode blank := styles.NewStyle().Background(t.Background()).Width(m.width).Render("") return blank + "\n" + status @@ -142,5 +137,12 @@ func NewStatusCmp(app *app.App) StatusComponent { app: app, } + homePath, err := os.UserHomeDir() + cwdPath := app.Info.Path.Cwd + if err == nil && homePath != "" && strings.HasPrefix(cwdPath, homePath) { + cwdPath = "~" + cwdPath[len(homePath):] + } + statusComponent.cwd = cwdPath + return statusComponent } diff --git a/packages/tui/internal/components/textarea/textarea.go b/packages/tui/internal/components/textarea/textarea.go index 5ff936f1..cc073e27 100644 --- a/packages/tui/internal/components/textarea/textarea.go +++ b/packages/tui/internal/components/textarea/textarea.go @@ -11,7 +11,6 @@ import ( "slices" - "github.com/atotto/clipboard" "github.com/charmbracelet/bubbles/v2/cursor" "github.com/charmbracelet/bubbles/v2/key" tea "github.com/charmbracelet/bubbletea/v2" @@ -219,11 +218,11 @@ func DefaultKeyMap() KeyMap { key.WithHelp("left", "character backward"), ), WordForward: key.NewBinding( - key.WithKeys("alt+right", "alt+f"), + key.WithKeys("alt+right", "ctrl+right", "alt+f"), key.WithHelp("alt+right", "word forward"), ), WordBackward: key.NewBinding( - key.WithKeys("alt+left", "alt+b"), + key.WithKeys("alt+left", "ctrl+left", "alt+b"), key.WithHelp("alt+left", "word backward"), ), LineNext: key.NewBinding( @@ -653,12 +652,12 @@ func (m *Model) SetValue(s string) { // InsertString inserts a string at the cursor position. func (m *Model) InsertString(s string) { - m.insertRunesFromUserInput([]rune(s)) + m.InsertRunesFromUserInput([]rune(s)) } // InsertRune inserts a rune at the cursor position. func (m *Model) InsertRune(r rune) { - m.insertRunesFromUserInput([]rune{r}) + m.InsertRunesFromUserInput([]rune{r}) } // InsertAttachment inserts an attachment at the cursor position. @@ -730,8 +729,8 @@ func (m Model) GetAttachments() []*Attachment { return attachments } -// insertRunesFromUserInput inserts runes at the current cursor position. -func (m *Model) insertRunesFromUserInput(runes []rune) { +// InsertRunesFromUserInput inserts runes at the current cursor position. +func (m *Model) InsertRunesFromUserInput(runes []rune) { // Clean up any special characters in the input provided by the // clipboard. This avoids bugs due to e.g. tab characters and // whatnot. @@ -932,7 +931,6 @@ func (m *Model) mapVisualOffsetToSliceIndex(row int, charOffset int) int { } // CursorDown moves the cursor down by one line. -// Returns whether or not the cursor blink should be reset. func (m *Model) CursorDown() { li := m.LineInfo() charOffset := max(m.lastCharOffset, li.CharOffset) @@ -941,11 +939,73 @@ func (m *Model) CursorDown() { if li.RowOffset+1 >= li.Height && m.row < len(m.value)-1 { // Move to the next model line m.row++ - m.col = m.mapVisualOffsetToSliceIndex(m.row, charOffset) + + // We want to land on the first wrapped line of the new model line. + grid := m.memoizedWrap(m.value[m.row], m.width) + targetLineContent := grid[0] + + // Find position within the first wrapped line. + offset := 0 + colInLine := 0 + for i, item := range targetLineContent { + var itemWidth int + switch v := item.(type) { + case rune: + itemWidth = rw.RuneWidth(v) + case *Attachment: + itemWidth = uniseg.StringWidth(v.Display) + } + if offset+itemWidth > charOffset { + // Decide whether to stick with the previous index or move to the current + // one based on which is closer to the target offset. + if (charOffset - offset) > ((offset + itemWidth) - charOffset) { + colInLine = i + 1 + } else { + colInLine = i + } + goto foundNextLine + } + offset += itemWidth + } + colInLine = len(targetLineContent) + foundNextLine: + m.col = colInLine // startCol is 0 for the first wrapped line } else if li.RowOffset+1 < li.Height { // Move to the next wrapped line within the same model line - startOfNextWrappedLine := li.StartColumn + li.Width - m.col = startOfNextWrappedLine + m.mapVisualOffsetToSliceIndex(m.row, charOffset) + grid := m.memoizedWrap(m.value[m.row], m.width) + targetLineContent := grid[li.RowOffset+1] + + startCol := 0 + for i := 0; i < li.RowOffset+1; i++ { + startCol += len(grid[i]) + } + + // Find position within the target wrapped line. + offset := 0 + colInLine := 0 + for i, item := range targetLineContent { + var itemWidth int + switch v := item.(type) { + case rune: + itemWidth = rw.RuneWidth(v) + case *Attachment: + itemWidth = uniseg.StringWidth(v.Display) + } + if offset+itemWidth > charOffset { + // Decide whether to stick with the previous index or move to the current + // one based on which is closer to the target offset. + if (charOffset - offset) > ((offset + itemWidth) - charOffset) { + colInLine = i + 1 + } else { + colInLine = i + } + goto foundSameLine + } + offset += itemWidth + } + colInLine = len(targetLineContent) + foundSameLine: + m.col = startCol + colInLine } m.SetCursorColumn(m.col) } @@ -957,22 +1017,77 @@ func (m *Model) CursorUp() { m.lastCharOffset = charOffset if li.RowOffset <= 0 && m.row > 0 { - // Move to the previous model line + // Move to the previous model line. We want to land on the last wrapped + // line of the previous model line. m.row-- - m.col = m.mapVisualOffsetToSliceIndex(m.row, charOffset) - } else if li.RowOffset > 0 { - // Move to the previous wrapped line within the same model line - // To do this, we need to find the start of the previous wrapped line. - prevLineInfo := m.LineInfo() - // prevLineStart := 0 - if prevLineInfo.RowOffset > 0 { - // This is complex, so we'll approximate by moving to the start of the current wrapped line - // and then letting characterLeft handle it. A more precise calculation would - // require re-wrapping to find the previous line's start. - // For now, a simpler approach: - m.col = li.StartColumn - 1 + grid := m.memoizedWrap(m.value[m.row], m.width) + targetLineContent := grid[len(grid)-1] + + // Find start of last wrapped line. + startCol := len(m.value[m.row]) - len(targetLineContent) + + // Find position within the last wrapped line. + offset := 0 + colInLine := 0 + for i, item := range targetLineContent { + var itemWidth int + switch v := item.(type) { + case rune: + itemWidth = rw.RuneWidth(v) + case *Attachment: + itemWidth = uniseg.StringWidth(v.Display) + } + if offset+itemWidth > charOffset { + // Decide whether to stick with the previous index or move to the current + // one based on which is closer to the target offset. + if (charOffset - offset) > ((offset + itemWidth) - charOffset) { + colInLine = i + 1 + } else { + colInLine = i + } + goto foundPrevLine + } + offset += itemWidth } - m.col = m.mapVisualOffsetToSliceIndex(m.row, charOffset) + colInLine = len(targetLineContent) + foundPrevLine: + m.col = startCol + colInLine + } else if li.RowOffset > 0 { + // Move to the previous wrapped line within the same model line. + grid := m.memoizedWrap(m.value[m.row], m.width) + targetLineContent := grid[li.RowOffset-1] + + startCol := 0 + for i := 0; i < li.RowOffset-1; i++ { + startCol += len(grid[i]) + } + + // Find position within the target wrapped line. + offset := 0 + colInLine := 0 + for i, item := range targetLineContent { + var itemWidth int + switch v := item.(type) { + case rune: + itemWidth = rw.RuneWidth(v) + case *Attachment: + itemWidth = uniseg.StringWidth(v.Display) + } + if offset+itemWidth > charOffset { + // Decide whether to stick with the previous index or move to the current + // one based on which is closer to the target offset. + if (charOffset - offset) > ((offset + itemWidth) - charOffset) { + colInLine = i + 1 + } else { + colInLine = i + } + goto foundSameLine + } + offset += itemWidth + } + colInLine = len(targetLineContent) + foundSameLine: + m.col = startCol + colInLine } m.SetCursorColumn(m.col) } @@ -1429,8 +1544,6 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { } switch msg := msg.(type) { - case tea.PasteMsg: - m.insertRunesFromUserInput([]rune(msg)) case tea.KeyPressMsg: switch { case key.Matches(msg, m.KeyMap.DeleteAfterCursor): @@ -1490,8 +1603,6 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { m.CursorDown() case key.Matches(msg, m.KeyMap.WordForward): m.wordRight() - case key.Matches(msg, m.KeyMap.Paste): - return m, Paste case key.Matches(msg, m.KeyMap.CharacterBackward): m.characterLeft(false /* insideLine */) case key.Matches(msg, m.KeyMap.LinePrevious): @@ -1512,11 +1623,11 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { m.transposeLeft() default: - m.insertRunesFromUserInput([]rune(msg.Text)) + m.InsertRunesFromUserInput([]rune(msg.Text)) } case pasteMsg: - m.insertRunesFromUserInput([]rune(msg)) + m.InsertRunesFromUserInput([]rune(msg)) case pasteErrMsg: m.Err = msg @@ -1596,7 +1707,7 @@ func (m Model) View() string { // guaranteed to be a space since any other character would // have been wrapped. wrappedLineStr = strings.TrimSuffix(wrappedLineStr, " ") - padding -= m.width - strwidth + padding = m.width - uniseg.StringWidth(wrappedLineStr) } if m.row == l && lineInfo.RowOffset == wl { @@ -1908,13 +2019,14 @@ func (m *Model) splitLine(row, col int) { m.row++ } -// Paste is a command for pasting from the clipboard into the text input. -func Paste() tea.Msg { - str, err := clipboard.ReadAll() - if err != nil { - return pasteErrMsg{err} +func itemWidth(item any) int { + switch v := item.(type) { + case rune: + return rw.RuneWidth(v) + case *Attachment: + return uniseg.StringWidth(v.Display) } - return pasteMsg(str) + return 0 } func wrapInterfaces(content []any, width int) [][]any { @@ -1959,19 +2071,18 @@ func wrapInterfaces(content []any, width int) [][]any { } inSpaces = true spaceW += itemW - } else { + } else { // It's not a space, it's a character for a word. if inSpaces { - // End of spaces - if lineW > 0 && lineW+spaceW > width { - lines = append(lines, []any{}) - lineW = 0 - } else { - lineW += spaceW - } - // Add spaces to current line + // We just finished a block of spaces. Handle them now. + lineW += spaceW for i := 0; i < spaceW; i++ { lines[len(lines)-1] = append(lines[len(lines)-1], rune(' ')) } + if lineW > width { + // The spaces made the line overflow. Start a new line for the upcoming word. + lines = append(lines, []any{}) + lineW = 0 + } spaceW = 0 } inSpaces = false @@ -1980,20 +2091,24 @@ func wrapInterfaces(content []any, width int) [][]any { } } - // Handle any remaining word/spaces + // Handle any remaining word/spaces at the end of the content. if wordW > 0 { if lineW > 0 && lineW+wordW > width { lines = append(lines, word) + lineW = wordW } else { lines[len(lines)-1] = append(lines[len(lines)-1], word...) + lineW += wordW } } if spaceW > 0 { - if lineW > 0 && lineW+spaceW > width { - lines = append(lines, []any{}) - } + // There are trailing spaces. Add them. for i := 0; i < spaceW; i++ { lines[len(lines)-1] = append(lines[len(lines)-1], rune(' ')) + lineW += 1 + } + if lineW > width { + lines = append(lines, []any{}) } } diff --git a/packages/tui/internal/config/config.go b/packages/tui/internal/config/config.go index 3dd6fcf5..d20376dd 100644 --- a/packages/tui/internal/config/config.go +++ b/packages/tui/internal/config/config.go @@ -16,18 +16,27 @@ type ModelUsage struct { LastUsed time.Time `toml:"last_used"` } +type ModeModel struct { + ProviderID string `toml:"provider_id"` + ModelID string `toml:"model_id"` +} + type State struct { - Theme string `toml:"theme"` - Provider string `toml:"provider"` - Model string `toml:"model"` - RecentlyUsedModels []ModelUsage `toml:"recently_used_models"` - MessagesRight bool `toml:"messages_right"` - SplitDiff bool `toml:"split_diff"` + Theme string `toml:"theme"` + ModeModel map[string]ModeModel `toml:"mode_model"` + Provider string `toml:"provider"` + Model string `toml:"model"` + Mode string `toml:"mode"` + RecentlyUsedModels []ModelUsage `toml:"recently_used_models"` + MessagesRight bool `toml:"messages_right"` + SplitDiff bool `toml:"split_diff"` } func NewState() *State { return &State{ Theme: "opencode", + Mode: "build", + ModeModel: make(map[string]ModeModel), RecentlyUsedModels: make([]ModelUsage, 0), } } @@ -60,6 +69,15 @@ func (s *State) UpdateModelUsage(providerID, modelID string) { } } +func (s *State) RemoveModelFromRecentlyUsed(providerID, modelID string) { + for i, usage := range s.RecentlyUsedModels { + if usage.ProviderID == providerID && usage.ModelID == modelID { + s.RecentlyUsedModels = append(s.RecentlyUsedModels[:i], s.RecentlyUsedModels[i+1:]...) + return + } + } +} + // SaveState writes the provided Config struct to the specified TOML file. // It will create the file if it doesn't exist, or overwrite it if it does. func SaveState(filePath string, state *State) error { diff --git a/packages/tui/internal/id/id.go b/packages/tui/internal/id/id.go new file mode 100644 index 00000000..0490b8f2 --- /dev/null +++ b/packages/tui/internal/id/id.go @@ -0,0 +1,96 @@ +package id + +import ( + "crypto/rand" + "encoding/hex" + "fmt" + "strings" + "sync" + "time" +) + +const ( + PrefixSession = "ses" + PrefixMessage = "msg" + PrefixUser = "usr" + PrefixPart = "prt" +) + +const length = 26 + +var ( + lastTimestamp int64 + counter int64 + mu sync.Mutex +) + +type Prefix string + +const ( + Session Prefix = PrefixSession + Message Prefix = PrefixMessage + User Prefix = PrefixUser + Part Prefix = PrefixPart +) + +func ValidatePrefix(id string, prefix Prefix) bool { + return strings.HasPrefix(id, string(prefix)) +} + +func Ascending(prefix Prefix, given ...string) string { + return generateID(prefix, false, given...) +} + +func Descending(prefix Prefix, given ...string) string { + return generateID(prefix, true, given...) +} + +func generateID(prefix Prefix, descending bool, given ...string) string { + if len(given) > 0 && given[0] != "" { + if !strings.HasPrefix(given[0], string(prefix)) { + panic(fmt.Sprintf("ID %s does not start with %s", given[0], string(prefix))) + } + return given[0] + } + + return generateNewID(prefix, descending) +} + +func randomBase62(length int) string { + const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + result := make([]byte, length) + bytes := make([]byte, length) + rand.Read(bytes) + + for i := 0; i < length; i++ { + result[i] = chars[bytes[i]%62] + } + + return string(result) +} + +func generateNewID(prefix Prefix, descending bool) string { + mu.Lock() + defer mu.Unlock() + + currentTimestamp := time.Now().UnixMilli() + + if currentTimestamp != lastTimestamp { + lastTimestamp = currentTimestamp + counter = 0 + } + counter++ + + now := uint64(currentTimestamp)*0x1000 + uint64(counter) + + if descending { + now = ^now + } + + timeBytes := make([]byte, 6) + for i := 0; i < 6; i++ { + timeBytes[i] = byte((now >> (40 - 8*i)) & 0xff) + } + + return string(prefix) + "_" + hex.EncodeToString(timeBytes) + randomBase62(length-12) +} \ No newline at end of file diff --git a/packages/tui/internal/image/clipboard_unix.go b/packages/tui/internal/image/clipboard_unix.go deleted file mode 100644 index 2653d8ca..00000000 --- a/packages/tui/internal/image/clipboard_unix.go +++ /dev/null @@ -1,46 +0,0 @@ -//go:build !windows - -package image - -import ( - "bytes" - "fmt" - "github.com/atotto/clipboard" - "image" -) - -func GetImageFromClipboard() ([]byte, string, error) { - text, err := clipboard.ReadAll() - if err != nil { - return nil, "", fmt.Errorf("Error reading clipboard") - } - - if text == "" { - return nil, "", nil - } - - binaryData := []byte(text) - imageBytes, err := binaryToImage(binaryData) - if err != nil { - return nil, text, nil - } - return imageBytes, "", nil - -} - -func binaryToImage(data []byte) ([]byte, error) { - reader := bytes.NewReader(data) - img, _, err := image.Decode(reader) - if err != nil { - return nil, fmt.Errorf("Unable to covert bytes to image") - } - - return ImageToBytes(img) -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} diff --git a/packages/tui/internal/image/clipboard_windows.go b/packages/tui/internal/image/clipboard_windows.go deleted file mode 100644 index 6431ce3d..00000000 --- a/packages/tui/internal/image/clipboard_windows.go +++ /dev/null @@ -1,192 +0,0 @@ -//go:build windows - -package image - -import ( - "bytes" - "fmt" - "image" - "image/color" - "log/slog" - "syscall" - "unsafe" -) - -var ( - user32 = syscall.NewLazyDLL("user32.dll") - kernel32 = syscall.NewLazyDLL("kernel32.dll") - openClipboard = user32.NewProc("OpenClipboard") - closeClipboard = user32.NewProc("CloseClipboard") - getClipboardData = user32.NewProc("GetClipboardData") - isClipboardFormatAvailable = user32.NewProc("IsClipboardFormatAvailable") - globalLock = kernel32.NewProc("GlobalLock") - globalUnlock = kernel32.NewProc("GlobalUnlock") - globalSize = kernel32.NewProc("GlobalSize") -) - -const ( - CF_TEXT = 1 - CF_UNICODETEXT = 13 - CF_DIB = 8 -) - -type BITMAPINFOHEADER struct { - BiSize uint32 - BiWidth int32 - BiHeight int32 - BiPlanes uint16 - BiBitCount uint16 - BiCompression uint32 - BiSizeImage uint32 - BiXPelsPerMeter int32 - BiYPelsPerMeter int32 - BiClrUsed uint32 - BiClrImportant uint32 -} - -func GetImageFromClipboard() ([]byte, string, error) { - ret, _, _ := openClipboard.Call(0) - if ret == 0 { - return nil, "", fmt.Errorf("failed to open clipboard") - } - defer func(closeClipboard *syscall.LazyProc, a ...uintptr) { - _, _, err := closeClipboard.Call(a...) - if err != nil { - slog.Error("close clipboard failed") - return - } - }(closeClipboard) - isTextAvailable, _, _ := isClipboardFormatAvailable.Call(uintptr(CF_TEXT)) - isUnicodeTextAvailable, _, _ := isClipboardFormatAvailable.Call(uintptr(CF_UNICODETEXT)) - - if isTextAvailable != 0 || isUnicodeTextAvailable != 0 { - // Get text from clipboard - var formatToUse uintptr = CF_TEXT - if isUnicodeTextAvailable != 0 { - formatToUse = CF_UNICODETEXT - } - - hClipboardText, _, _ := getClipboardData.Call(formatToUse) - if hClipboardText != 0 { - textPtr, _, _ := globalLock.Call(hClipboardText) - if textPtr != 0 { - defer func(globalUnlock *syscall.LazyProc, a ...uintptr) { - _, _, err := globalUnlock.Call(a...) - if err != nil { - slog.Error("Global unlock failed") - return - } - }(globalUnlock, hClipboardText) - - // Get clipboard text - var clipboardText string - if formatToUse == CF_UNICODETEXT { - // Convert wide string to Go string - clipboardText = syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(textPtr))[:]) - } else { - // Get size of ANSI text - size, _, _ := globalSize.Call(hClipboardText) - if size > 0 { - // Convert ANSI string to Go string - textBytes := make([]byte, size) - copy(textBytes, (*[1 << 20]byte)(unsafe.Pointer(textPtr))[:size:size]) - clipboardText = bytesToString(textBytes) - } - } - - // Check if the text is not empty - if clipboardText != "" { - return nil, clipboardText, nil - } - } - } - } - hClipboardData, _, _ := getClipboardData.Call(uintptr(CF_DIB)) - if hClipboardData == 0 { - return nil, "", fmt.Errorf("failed to get clipboard data") - } - - dataPtr, _, _ := globalLock.Call(hClipboardData) - if dataPtr == 0 { - return nil, "", fmt.Errorf("failed to lock clipboard data") - } - defer func(globalUnlock *syscall.LazyProc, a ...uintptr) { - _, _, err := globalUnlock.Call(a...) - if err != nil { - slog.Error("Global unlock failed") - return - } - }(globalUnlock, hClipboardData) - - bmiHeader := (*BITMAPINFOHEADER)(unsafe.Pointer(dataPtr)) - - width := int(bmiHeader.BiWidth) - height := int(bmiHeader.BiHeight) - if height < 0 { - height = -height - } - bitsPerPixel := int(bmiHeader.BiBitCount) - - img := image.NewRGBA(image.Rect(0, 0, width, height)) - - var bitsOffset uintptr - if bitsPerPixel <= 8 { - numColors := uint32(1) << bitsPerPixel - if bmiHeader.BiClrUsed > 0 { - numColors = bmiHeader.BiClrUsed - } - bitsOffset = unsafe.Sizeof(*bmiHeader) + uintptr(numColors*4) - } else { - bitsOffset = unsafe.Sizeof(*bmiHeader) - } - - for y := range height { - for x := range width { - - srcY := height - y - 1 - if bmiHeader.BiHeight < 0 { - srcY = y - } - - var pixelPointer unsafe.Pointer - var r, g, b, a uint8 - - switch bitsPerPixel { - case 24: - stride := (width*3 + 3) &^ 3 - pixelPointer = unsafe.Pointer(dataPtr + bitsOffset + uintptr(srcY*stride+x*3)) - b = *(*byte)(pixelPointer) - g = *(*byte)(unsafe.Add(pixelPointer, 1)) - r = *(*byte)(unsafe.Add(pixelPointer, 2)) - a = 255 - case 32: - pixelPointer = unsafe.Pointer(dataPtr + bitsOffset + uintptr(srcY*width*4+x*4)) - b = *(*byte)(pixelPointer) - g = *(*byte)(unsafe.Add(pixelPointer, 1)) - r = *(*byte)(unsafe.Add(pixelPointer, 2)) - a = *(*byte)(unsafe.Add(pixelPointer, 3)) - if a == 0 { - a = 255 - } - default: - return nil, "", fmt.Errorf("unsupported bit count: %d", bitsPerPixel) - } - - img.Set(x, y, color.RGBA{R: r, G: g, B: b, A: a}) - } - } - - imageBytes, err := ImageToBytes(img) - if err != nil { - return nil, "", err - } - return imageBytes, "", nil -} - -func bytesToString(b []byte) string { - i := bytes.IndexByte(b, 0) - if i == -1 { - return string(b) - } - return string(b[:i]) -} diff --git a/packages/tui/internal/image/images.go b/packages/tui/internal/image/images.go deleted file mode 100644 index 742eb30a..00000000 --- a/packages/tui/internal/image/images.go +++ /dev/null @@ -1,86 +0,0 @@ -package image - -import ( - "bytes" - "fmt" - "image" - "image/color" - "image/png" - "os" - "strings" - - "github.com/charmbracelet/lipgloss/v2" - "github.com/disintegration/imaging" - "github.com/lucasb-eyer/go-colorful" - _ "golang.org/x/image/webp" -) - -func ValidateFileSize(filePath string, sizeLimit int64) (bool, error) { - fileInfo, err := os.Stat(filePath) - if err != nil { - return false, fmt.Errorf("error getting file info: %w", err) - } - - if fileInfo.Size() > sizeLimit { - return true, nil - } - - return false, nil -} - -func ToString(width int, img image.Image) string { - img = imaging.Resize(img, width, 0, imaging.Lanczos) - b := img.Bounds() - imageWidth := b.Max.X - h := b.Max.Y - str := strings.Builder{} - - for heightCounter := 0; heightCounter < h; heightCounter += 2 { - for x := range imageWidth { - c1, _ := colorful.MakeColor(img.At(x, heightCounter)) - color1 := lipgloss.Color(c1.Hex()) - - var color2 color.Color - if heightCounter+1 < h { - c2, _ := colorful.MakeColor(img.At(x, heightCounter+1)) - color2 = lipgloss.Color(c2.Hex()) - } else { - color2 = color1 - } - - str.WriteString(lipgloss.NewStyle().Foreground(color1). - Background(color2).Render("▀")) - } - - str.WriteString("\n") - } - - return str.String() -} - -func ImagePreview(width int, filename string) (string, error) { - imageContent, err := os.Open(filename) - if err != nil { - return "", err - } - defer imageContent.Close() - - img, _, err := image.Decode(imageContent) - if err != nil { - return "", err - } - - imageString := ToString(width, img) - - return imageString, nil -} - -func ImageToBytes(image image.Image) ([]byte, error) { - buf := new(bytes.Buffer) - err := png.Encode(buf, image) - if err != nil { - return nil, err - } - - return buf.Bytes(), nil -} diff --git a/packages/tui/internal/layout/overlay.go b/packages/tui/internal/layout/overlay.go index 48064c91..08016e31 100644 --- a/packages/tui/internal/layout/overlay.go +++ b/packages/tui/internal/layout/overlay.go @@ -15,6 +15,11 @@ import ( "github.com/sst/opencode/internal/util" ) +var ( + // ANSI escape sequence regex + ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`) +) + // Split a string into lines, additionally returning the size of the widest line. func getLines(s string) (lines []string, widest int) { lines = strings.Split(s, "\n") @@ -272,9 +277,6 @@ func combineStyles(bgStyle ansiStyle, fgColor *compat.AdaptiveColor) string { // getStyleAtPosition extracts the active ANSI style at a given visual position func getStyleAtPosition(s string, targetPos int) ansiStyle { - // ANSI escape sequence regex - ansiRegex := regexp.MustCompile(`\x1b\[[0-9;]*m`) - visualPos := 0 currentStyle := ansiStyle{} diff --git a/packages/tui/internal/tui/tui.go b/packages/tui/internal/tui/tui.go index 150a1b26..b201a908 100644 --- a/packages/tui/internal/tui/tui.go +++ b/packages/tui/internal/tui/tui.go @@ -2,9 +2,11 @@ package tui import ( "context" + "fmt" "log/slog" "os" "os/exec" + "slices" "strings" "time" @@ -23,6 +25,7 @@ import ( "github.com/sst/opencode/internal/components/modal" "github.com/sst/opencode/internal/components/status" "github.com/sst/opencode/internal/components/toast" + "github.com/sst/opencode/internal/config" "github.com/sst/opencode/internal/layout" "github.com/sst/opencode/internal/styles" "github.com/sst/opencode/internal/theme" @@ -32,16 +35,27 @@ import ( // InterruptDebounceTimeoutMsg is sent when the interrupt key debounce timeout expires type InterruptDebounceTimeoutMsg struct{} +// ExitDebounceTimeoutMsg is sent when the exit key debounce timeout expires +type ExitDebounceTimeoutMsg struct{} + // InterruptKeyState tracks the state of interrupt key presses for debouncing type InterruptKeyState int +// ExitKeyState tracks the state of exit key presses for debouncing +type ExitKeyState int + const ( InterruptKeyIdle InterruptKeyState = iota InterruptKeyFirstPress ) +const ( + ExitKeyIdle ExitKeyState = iota + ExitKeyFirstPress +) + const interruptDebounceTimeout = 1 * time.Second -const fileViewerFullWidthCutoff = 160 +const exitDebounceTimeout = 1 * time.Second type appModel struct { width, height int @@ -51,21 +65,17 @@ type appModel struct { editor chat.EditorComponent messages chat.MessagesComponent completions dialog.CompletionDialog - commandProvider dialog.CompletionProvider - fileProvider dialog.CompletionProvider + commandProvider completions.CompletionProvider + fileProvider completions.CompletionProvider + symbolsProvider completions.CompletionProvider showCompletionDialog bool - fileCompletionActive bool leaderBinding *key.Binding - isLeaderSequence bool - toastManager *toast.ToastManager - interruptKeyState InterruptKeyState - lastScroll time.Time - messagesRight bool - fileViewer fileviewer.Model - lastMouse tea.Mouse - fileViewerStart int - fileViewerEnd int - fileViewerHit bool + // isLeaderSequence bool + toastManager *toast.ToastManager + interruptKeyState InterruptKeyState + exitKeyState ExitKeyState + messagesRight bool + fileViewer fileviewer.Model } func (a appModel) Init() tea.Cmd { @@ -92,44 +102,6 @@ func (a appModel) Init() tea.Cmd { return tea.Batch(cmds...) } -var BUGGED_SCROLL_KEYS = map[string]bool{ - "0": true, - "1": true, - "2": true, - "3": true, - "4": true, - "5": true, - "6": true, - "7": true, - "8": true, - "9": true, - "M": true, - "m": true, - "[": true, - ";": true, - "<": true, -} - -func isScrollRelatedInput(keyString string) bool { - if len(keyString) == 0 { - return false - } - - for _, char := range keyString { - charStr := string(char) - if !BUGGED_SCROLL_KEYS[charStr] { - return false - } - } - - if len(keyString) > 3 && - (keyString[len(keyString)-1] == 'M' || keyString[len(keyString)-1] == 'm') { - return true - } - - return len(keyString) > 1 -} - func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd var cmds []tea.Cmd @@ -138,10 +110,6 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyPressMsg: keyString := msg.String() - if time.Since(a.lastScroll) < time.Millisecond*100 && (BUGGED_SCROLL_KEYS[keyString] || isScrollRelatedInput(keyString)) { - return a, nil - } - // 1. Handle active modal if a.modal != nil { switch keyString { @@ -169,9 +137,9 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } // 2. Check for commands that require leader - if a.isLeaderSequence { - matches := a.app.Commands.Matches(msg, a.isLeaderSequence) - a.isLeaderSequence = false + if a.app.IsLeaderSequence { + matches := a.app.Commands.Matches(msg, a.app.IsLeaderSequence) + a.app.IsLeaderSequence = false if len(matches) > 0 { return a, util.CmdHandler(commands.ExecuteCommandsMsg(matches)) } @@ -182,14 +150,13 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { !a.showCompletionDialog && a.editor.Value() == "" { a.showCompletionDialog = true - a.fileCompletionActive = false updated, cmd := a.editor.Update(msg) a.editor = updated.(chat.EditorComponent) cmds = append(cmds, cmd) // Set command provider for command completion - a.completions = dialog.NewCompletionDialogComponent(a.commandProvider) + a.completions = dialog.NewCompletionDialogComponent("/", a.commandProvider) updated, cmd = a.completions.Update(msg) a.completions = updated.(dialog.CompletionDialog) cmds = append(cmds, cmd) @@ -201,14 +168,13 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if keyString == "@" && !a.showCompletionDialog { a.showCompletionDialog = true - a.fileCompletionActive = true updated, cmd := a.editor.Update(msg) a.editor = updated.(chat.EditorComponent) cmds = append(cmds, cmd) - // Set file provider for file completion - a.completions = dialog.NewCompletionDialogComponent(a.fileProvider) + // Set both file and symbols providers for @ completion + a.completions = dialog.NewCompletionDialogComponent("@", a.fileProvider, a.symbolsProvider) updated, cmd = a.completions.Update(msg) a.completions = updated.(dialog.CompletionDialog) cmds = append(cmds, cmd) @@ -218,7 +184,7 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if a.showCompletionDialog { switch keyString { - case "tab", "enter", "esc", "ctrl+c", "up", "down": + case "tab", "enter", "esc", "ctrl+c", "up", "down", "ctrl+p", "ctrl+n": updated, cmd := a.completions.Update(msg) a.completions = updated.(dialog.CompletionDialog) cmds = append(cmds, cmd) @@ -246,15 +212,21 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // 5. Check for leader key activation if a.leaderBinding != nil && - !a.isLeaderSequence && + !a.app.IsLeaderSequence && key.Matches(msg, *a.leaderBinding) { - a.isLeaderSequence = true + a.app.IsLeaderSequence = true return a, nil } - // 6. Handle interrupt key debounce for session interrupt + // 6 Handle input clear command + inputClearCommand := a.app.Commands[commands.InputClearCommand] + if inputClearCommand.Matches(msg, a.app.IsLeaderSequence) && a.editor.Length() > 0 { + return a, util.CmdHandler(commands.ExecuteCommandMsg(inputClearCommand)) + } + + // 7. Handle interrupt key debounce for session interrupt interruptCommand := a.app.Commands[commands.SessionInterruptCommand] - if interruptCommand.Matches(msg, a.isLeaderSequence) && a.app.IsBusy() { + if interruptCommand.Matches(msg, a.app.IsLeaderSequence) && a.app.IsBusy() { switch a.interruptKeyState { case InterruptKeyIdle: // First interrupt key press - start debounce timer @@ -271,48 +243,56 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } - // 7. Check again for commands that don't require leader (excluding interrupt when busy) - matches := a.app.Commands.Matches(msg, a.isLeaderSequence) + // 8. Handle exit key debounce for app exit when using non-leader command + exitCommand := a.app.Commands[commands.AppExitCommand] + if exitCommand.Matches(msg, a.app.IsLeaderSequence) { + switch a.exitKeyState { + case ExitKeyIdle: + // First exit key press - start debounce timer + a.exitKeyState = ExitKeyFirstPress + a.editor.SetExitKeyInDebounce(true) + return a, tea.Tick(exitDebounceTimeout, func(t time.Time) tea.Msg { + return ExitDebounceTimeoutMsg{} + }) + case ExitKeyFirstPress: + // Second exit key press within timeout - actually exit + a.exitKeyState = ExitKeyIdle + a.editor.SetExitKeyInDebounce(false) + return a, util.CmdHandler(commands.ExecuteCommandMsg(exitCommand)) + } + } + + // 9. Check again for commands that don't require leader (excluding interrupt when busy and exit when in debounce) + matches := a.app.Commands.Matches(msg, a.app.IsLeaderSequence) if len(matches) > 0 { // Skip interrupt key if we're in debounce mode and app is busy - if interruptCommand.Matches(msg, a.isLeaderSequence) && a.app.IsBusy() && a.interruptKeyState != InterruptKeyIdle { + if interruptCommand.Matches(msg, a.app.IsLeaderSequence) && a.app.IsBusy() && a.interruptKeyState != InterruptKeyIdle { return a, nil } return a, util.CmdHandler(commands.ExecuteCommandsMsg(matches)) } - // 7. Fallback to editor. This is for other characters - // like backspace, tab, etc. + // Fallback: suspend if ctrl+z is pressed and no user keybind matched + if keyString == "ctrl+z" { + return a, tea.Suspend + } + + // 10. Fallback to editor. This is for other characters like backspace, tab, etc. updatedEditor, cmd := a.editor.Update(msg) a.editor = updatedEditor.(chat.EditorComponent) return a, cmd case tea.MouseWheelMsg: - a.lastScroll = time.Now() if a.modal != nil { - return a, nil - } - - var cmd tea.Cmd - if a.fileViewerHit { - a.fileViewer, cmd = a.fileViewer.Update(msg) - cmds = append(cmds, cmd) - } else { - updated, cmd := a.messages.Update(msg) - a.messages = updated.(chat.MessagesComponent) - cmds = append(cmds, cmd) + u, cmd := a.modal.Update(msg) + a.modal = u.(layout.Modal) + cmds = append(cmds, cmd) + return a, tea.Batch(cmds...) } + updated, cmd := a.messages.Update(msg) + a.messages = updated.(chat.MessagesComponent) + cmds = append(cmds, cmd) return a, tea.Batch(cmds...) - case tea.MouseMotionMsg: - a.lastMouse = msg.Mouse() - a.fileViewerHit = a.fileViewer.HasFile() && - a.lastMouse.X > a.fileViewerStart && - a.lastMouse.X < a.fileViewerEnd - case tea.MouseClickMsg: - a.lastMouse = msg.Mouse() - a.fileViewerHit = a.fileViewer.HasFile() && - a.lastMouse.X > a.fileViewerStart && - a.lastMouse.X < a.fileViewerEnd case tea.BackgroundColorMsg: styles.Terminal = &styles.TerminalInfo{ Background: msg.Color, @@ -352,9 +332,14 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { a.showCompletionDialog = false a.app, cmd = a.app.SendChatMessage(context.Background(), msg.Text, msg.Attachments) cmds = append(cmds, cmd) + case app.SetEditorContentMsg: + // Set the editor content without sending + a.editor.SetValueWithAttachments(msg.Text) + updated, cmd := a.editor.Focus() + a.editor = updated.(chat.EditorComponent) + cmds = append(cmds, cmd) case dialog.CompletionDialogCloseMsg: a.showCompletionDialog = false - a.fileCompletionActive = false case opencode.EventListResponseEventInstallationUpdated: return a, toast.NewSuccessToast( "opencode updated to "+msg.Properties.Version+", restart to apply.", @@ -363,45 +348,76 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case opencode.EventListResponseEventSessionDeleted: if a.app.Session != nil && msg.Properties.Info.ID == a.app.Session.ID { a.app.Session = &opencode.Session{} - a.app.Messages = []opencode.Message{} + a.app.Messages = []app.Message{} } return a, toast.NewSuccessToast("Session deleted successfully") case opencode.EventListResponseEventSessionUpdated: if msg.Properties.Info.ID == a.app.Session.ID { a.app.Session = &msg.Properties.Info } + case opencode.EventListResponseEventMessagePartUpdated: + slog.Info("message part updated", "message", msg.Properties.Part.MessageID, "part", msg.Properties.Part.ID) + if msg.Properties.Part.SessionID == a.app.Session.ID { + messageIndex := slices.IndexFunc(a.app.Messages, func(m app.Message) bool { + switch casted := m.Info.(type) { + case opencode.UserMessage: + return casted.ID == msg.Properties.Part.MessageID + case opencode.AssistantMessage: + return casted.ID == msg.Properties.Part.MessageID + } + return false + }) + if messageIndex > -1 { + message := a.app.Messages[messageIndex] + partIndex := slices.IndexFunc(message.Parts, func(p opencode.PartUnion) bool { + switch casted := p.(type) { + case opencode.TextPart: + return casted.ID == msg.Properties.Part.ID + case opencode.FilePart: + return casted.ID == msg.Properties.Part.ID + case opencode.ToolPart: + return casted.ID == msg.Properties.Part.ID + case opencode.StepStartPart: + return casted.ID == msg.Properties.Part.ID + case opencode.StepFinishPart: + return casted.ID == msg.Properties.Part.ID + } + return false + }) + if partIndex > -1 { + message.Parts[partIndex] = msg.Properties.Part.AsUnion() + } + if partIndex == -1 { + message.Parts = append(message.Parts, msg.Properties.Part.AsUnion()) + } + a.app.Messages[messageIndex] = message + } + } case opencode.EventListResponseEventMessageUpdated: - if msg.Properties.Info.Metadata.SessionID == a.app.Session.ID { - exists := false - optimisticReplaced := false + if msg.Properties.Info.SessionID == a.app.Session.ID { + matchIndex := slices.IndexFunc(a.app.Messages, func(m app.Message) bool { + switch casted := m.Info.(type) { + case opencode.UserMessage: + return casted.ID == msg.Properties.Info.ID + case opencode.AssistantMessage: + return casted.ID == msg.Properties.Info.ID + } + return false + }) - // First check if this is replacing an optimistic message - if msg.Properties.Info.Role == opencode.MessageRoleUser { - // Look for optimistic messages to replace - for i, m := range a.app.Messages { - if strings.HasPrefix(m.ID, "optimistic-") && m.Role == opencode.MessageRoleUser { - // Replace the optimistic message with the real one - a.app.Messages[i] = msg.Properties.Info - exists = true - optimisticReplaced = true - break - } + if matchIndex > -1 { + match := a.app.Messages[matchIndex] + a.app.Messages[matchIndex] = app.Message{ + Info: msg.Properties.Info.AsUnion(), + Parts: match.Parts, } } - // If not replacing optimistic, check for existing message with same ID - if !optimisticReplaced { - for i, m := range a.app.Messages { - if m.ID == msg.Properties.Info.ID { - a.app.Messages[i] = msg.Properties.Info - exists = true - break - } - } - } - - if !exists { - a.app.Messages = append(a.app.Messages, msg.Properties.Info) + if matchIndex == -1 { + a.app.Messages = append(a.app.Messages, app.Message{ + Info: msg.Properties.Info.AsUnion(), + Parts: []opencode.PartUnion{}, + }) } } case opencode.EventListResponseEventSessionError: @@ -423,14 +439,7 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.WindowSizeMsg: msg.Height -= 2 // Make space for the status bar a.width, a.height = msg.Width, msg.Height - container := min(a.width, 84) - if a.fileViewer.HasFile() { - if a.width < fileViewerFullWidthCutoff { - container = a.width - } else { - container = min(min(a.width, max(a.width/2, 50)), 84) - } - } + container := min(a.width, app.MAX_CONTAINER_WIDTH) layout.Current = &layout.LayoutInfo{ Viewport: layout.Dimensions{ Width: a.width, @@ -440,35 +449,25 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { Width: container, }, } - mainWidth := layout.Current.Container.Width - a.messages.SetWidth(mainWidth - 4) - - sideWidth := a.width - mainWidth - if a.width < fileViewerFullWidthCutoff { - sideWidth = a.width - } - a.fileViewerStart = mainWidth - a.fileViewerEnd = a.fileViewerStart + sideWidth - if a.messagesRight { - a.fileViewerStart = 0 - a.fileViewerEnd = sideWidth - } - a.fileViewer, cmd = a.fileViewer.SetSize(sideWidth, layout.Current.Viewport.Height) - cmds = append(cmds, cmd) case app.SessionSelectedMsg: messages, err := a.app.ListMessages(context.Background(), msg.ID) if err != nil { - slog.Error("Failed to list messages", "error", err) + slog.Error("Failed to list messages", "error", err.Error()) return a, toast.NewErrorToast("Failed to open session") } a.app.Session = msg a.app.Messages = messages return a, util.CmdHandler(app.SessionLoadedMsg{}) + case app.SessionCreatedMsg: + a.app.Session = msg.Session + return a, util.CmdHandler(app.SessionLoadedMsg{}) case app.ModelSelectedMsg: a.app.Provider = &msg.Provider a.app.Model = &msg.Model - a.app.State.Provider = msg.Provider.ID - a.app.State.Model = msg.Model.ID + a.app.State.ModeModel[a.app.Mode.Name] = config.ModeModel{ + ProviderID: msg.Provider.ID, + ModelID: msg.Model.ID, + } a.app.State.UpdateModelUsage(msg.Provider.ID, msg.Model.ID) a.app.SaveState() case dialog.ThemeSelectedMsg: @@ -486,6 +485,10 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // Reset interrupt key state after timeout a.interruptKeyState = InterruptKeyIdle a.editor.SetInterruptKeyInDebounce(false) + case ExitDebounceTimeoutMsg: + // Reset exit key state after timeout + a.exitKeyState = ExitKeyIdle + a.editor.SetExitKeyInDebounce(false) case dialog.FindSelectedMsg: return a.openFile(msg.FilePath) } @@ -525,48 +528,22 @@ func (a appModel) View() string { t := theme.CurrentTheme() var mainLayout string - mainWidth := layout.Current.Container.Width - 4 + if a.app.Session.ID == "" { - mainLayout = a.home(mainWidth) + mainLayout = a.home() } else { - mainLayout = a.chat(mainWidth) + mainLayout = a.chat() } mainLayout = styles.NewStyle(). Background(t.Background()). Padding(0, 2). Render(mainLayout) - - mainHeight := lipgloss.Height(mainLayout) - - if a.fileViewer.HasFile() { - file := a.fileViewer.View() - baseStyle := styles.NewStyle().Background(t.BackgroundPanel()) - sidePanel := baseStyle.Height(mainHeight).Render(file) - if a.width >= fileViewerFullWidthCutoff { - if a.messagesRight { - mainLayout = lipgloss.JoinHorizontal( - lipgloss.Top, - sidePanel, - mainLayout, - ) - } else { - mainLayout = lipgloss.JoinHorizontal( - lipgloss.Top, - mainLayout, - sidePanel, - ) - } - } else { - mainLayout = sidePanel - } - } else { - mainLayout = lipgloss.PlaceHorizontal( - a.width, - lipgloss.Center, - mainLayout, - styles.WhitespaceStyle(t.Background()), - ) - } + mainLayout = lipgloss.PlaceHorizontal( + a.width, + lipgloss.Center, + mainLayout, + styles.WhitespaceStyle(t.Background()), + ) mainStyle := styles.NewStyle().Background(t.Background()) mainLayout = mainStyle.Render(mainLayout) @@ -602,8 +579,9 @@ func (a appModel) openFile(filepath string) (tea.Model, tea.Cmd) { return a, cmd } -func (a appModel) home(width int) string { +func (a appModel) home() string { t := theme.CurrentTheme() + effectiveWidth := a.width - 4 baseStyle := styles.NewStyle().Background(t.Background()) base := baseStyle.Render muted := styles.NewStyle().Foreground(t.TextMuted()).Background(t.Background()).Render @@ -634,7 +612,7 @@ func (a appModel) home(width int) string { logoAndVersion := strings.Join([]string{logo, version}, "\n") logoAndVersion = lipgloss.PlaceHorizontal( - width, + effectiveWidth, lipgloss.Center, logoAndVersion, styles.WhitespaceStyle(t.Background()), @@ -645,7 +623,7 @@ func (a appModel) home(width int) string { cmdcomp.WithLimit(6), ) cmds := lipgloss.PlaceHorizontal( - width, + effectiveWidth, lipgloss.Center, commandsView.View(), styles.WhitespaceStyle(t.Background()), @@ -657,19 +635,16 @@ func (a appModel) home(width int) string { lines = append(lines, logoAndVersion) lines = append(lines, "") lines = append(lines, "") - // lines = append(lines, base("cwd ")+muted(cwd)) - // lines = append(lines, base("config ")+muted(config)) - // lines = append(lines, "") lines = append(lines, cmds) lines = append(lines, "") lines = append(lines, "") mainHeight := lipgloss.Height(strings.Join(lines, "\n")) - editorWidth := min(width, 80) - editorView := a.editor.View(editorWidth) + editorView := a.editor.View() + editorWidth := lipgloss.Width(editorView) editorView = lipgloss.PlaceHorizontal( - width, + effectiveWidth, lipgloss.Center, editorView, styles.WhitespaceStyle(t.Background()), @@ -679,7 +654,7 @@ func (a appModel) home(width int) string { editorLines := a.editor.Lines() mainLayout := lipgloss.Place( - width, + effectiveWidth, a.height, lipgloss.Center, lipgloss.Center, @@ -687,14 +662,14 @@ func (a appModel) home(width int) string { styles.WhitespaceStyle(t.Background()), ) - editorX := (width - editorWidth) / 2 + editorX := (effectiveWidth - editorWidth) / 2 editorY := (a.height / 2) + (mainHeight / 2) - 2 if editorLines > 1 { mainLayout = layout.PlaceOverlay( editorX, editorY, - a.editor.Content(editorWidth), + a.editor.Content(), mainLayout, ) } @@ -715,23 +690,31 @@ func (a appModel) home(width int) string { return mainLayout } -func (a appModel) chat(width int) string { - editorView := a.editor.View(width) +func (a appModel) chat() string { + effectiveWidth := a.width - 4 + t := theme.CurrentTheme() + editorView := a.editor.View() lines := a.editor.Lines() - messagesView := a.messages.View(width, a.height-5) + messagesView := a.messages.View() editorWidth := lipgloss.Width(editorView) editorHeight := max(lines, 5) + editorView = lipgloss.PlaceHorizontal( + effectiveWidth, + lipgloss.Center, + editorView, + styles.WhitespaceStyle(t.Background()), + ) mainLayout := messagesView + "\n" + editorView - editorX := (a.width - editorWidth) / 2 + editorX := (effectiveWidth - editorWidth) / 2 if lines > 1 { editorY := a.height - editorHeight mainLayout = layout.PlaceOverlay( editorX, editorY, - a.editor.Content(width), + a.editor.Content(), mainLayout, ) } @@ -762,6 +745,14 @@ func (a appModel) executeCommand(command commands.Command) (tea.Model, tea.Cmd) case commands.AppHelpCommand: helpDialog := dialog.NewHelpDialog(a.app) a.modal = helpDialog + case commands.SwitchModeCommand: + updated, cmd := a.app.SwitchMode() + a.app = updated + cmds = append(cmds, cmd) + case commands.SwitchModeReverseCommand: + updated, cmd := a.app.SwitchModeReverse() + a.app = updated + cmds = append(cmds, cmd) case commands.EditorOpenCommand: if a.app.IsBusy() { // status.Warn("Agent is working, please wait...") @@ -803,7 +794,7 @@ func (a appModel) executeCommand(command commands.Command) (tea.Model, tea.Cmd) return nil } os.Remove(tmpfile.Name()) - return app.SendMsg{ + return app.SetEditorContentMsg{ Text: string(content), } }) @@ -813,7 +804,7 @@ func (a appModel) executeCommand(command commands.Command) (tea.Model, tea.Cmd) return a, nil } a.app.Session = &opencode.Session{} - a.app.Messages = []opencode.Message{} + a.app.Messages = []app.Message{} cmds = append(cmds, util.CmdHandler(app.SessionClearedMsg{})) case commands.SessionListCommand: sessionDialog := dialog.NewSessionDialog(a.app) @@ -828,7 +819,7 @@ func (a appModel) executeCommand(command commands.Command) (tea.Model, tea.Cmd) return a, toast.NewErrorToast("Failed to share session") } shareUrl := response.Share.URL - cmds = append(cmds, tea.SetClipboard(shareUrl)) + cmds = append(cmds, a.app.SetClipboard(shareUrl)) cmds = append(cmds, toast.NewSuccessToast("Share URL copied to clipboard!")) case commands.SessionUnshareCommand: if a.app.Session.ID == "" { @@ -853,6 +844,56 @@ func (a appModel) executeCommand(command commands.Command) (tea.Model, tea.Cmd) } // TODO: block until compaction is complete a.app.CompactSession(context.Background()) + case commands.SessionExportCommand: + if a.app.Session.ID == "" { + return a, toast.NewErrorToast("No active session to export.") + } + + // Use current conversation history + messages := a.app.Messages + if len(messages) == 0 { + return a, toast.NewInfoToast("No messages to export.") + } + + // Format to Markdown + markdownContent := formatConversationToMarkdown(messages) + + // Check if EDITOR is set + editor := os.Getenv("EDITOR") + if editor == "" { + return a, toast.NewErrorToast("No EDITOR set, can't open editor") + } + + // Create and write to temp file + tmpfile, err := os.CreateTemp("", "conversation-*.md") + if err != nil { + slog.Error("Failed to create temp file", "error", err) + return a, toast.NewErrorToast("Failed to create temporary file.") + } + + _, err = tmpfile.WriteString(markdownContent) + if err != nil { + slog.Error("Failed to write to temp file", "error", err) + tmpfile.Close() + os.Remove(tmpfile.Name()) + return a, toast.NewErrorToast("Failed to write conversation to file.") + } + tmpfile.Close() + + // Open in editor + c := exec.Command(editor, tmpfile.Name()) + c.Stdin = os.Stdin + c.Stdout = os.Stdout + c.Stderr = os.Stderr + cmd = tea.ExecProcess(c, func(err error) tea.Msg { + if err != nil { + slog.Error("Failed to open editor for conversation", "error", err) + } + // Clean up the file after editor closes + os.Remove(tmpfile.Name()) + return nil + }) + cmds = append(cmds, cmd) case commands.ToolDetailsCommand: message := "Tool details are now visible" if a.messages.ToolDetailsVisible() { @@ -866,12 +907,11 @@ func (a appModel) executeCommand(command commands.Command) (tea.Model, tea.Cmd) case commands.ThemeListCommand: themeDialog := dialog.NewThemeDialog() a.modal = themeDialog - case commands.FileListCommand: - a.editor.Blur() - provider := completions.NewFileAndFolderContextGroup(a.app) - findDialog := dialog.NewFindDialog(provider) - findDialog.SetWidth(layout.Current.Container.Width - 8) - a.modal = findDialog + // case commands.FileListCommand: + // a.editor.Blur() + // findDialog := dialog.NewFindDialog(a.fileProvider) + // cmds = append(cmds, findDialog.Init()) + // a.modal = findDialog case commands.FileCloseCommand: a.fileViewer, cmd = a.fileViewer.Clear() cmds = append(cmds, cmd) @@ -904,11 +944,11 @@ func (a appModel) executeCommand(command commands.Command) (tea.Model, tea.Cmd) a.editor = updated.(chat.EditorComponent) cmds = append(cmds, cmd) case commands.MessagesFirstCommand: - updated, cmd := a.messages.First() + updated, cmd := a.messages.GotoTop() a.messages = updated.(chat.MessagesComponent) cmds = append(cmds, cmd) case commands.MessagesLastCommand: - updated, cmd := a.messages.Last() + updated, cmd := a.messages.GotoBottom() a.messages = updated.(chat.MessagesComponent) cmds = append(cmds, cmd) case commands.MessagesPageUpCommand: @@ -947,26 +987,14 @@ func (a appModel) executeCommand(command commands.Command) (tea.Model, tea.Cmd) a.messages = updated.(chat.MessagesComponent) cmds = append(cmds, cmd) } - case commands.MessagesPreviousCommand: - updated, cmd := a.messages.Previous() - a.messages = updated.(chat.MessagesComponent) - cmds = append(cmds, cmd) - case commands.MessagesNextCommand: - updated, cmd := a.messages.Next() - a.messages = updated.(chat.MessagesComponent) - cmds = append(cmds, cmd) case commands.MessagesLayoutToggleCommand: a.messagesRight = !a.messagesRight a.app.State.MessagesRight = a.messagesRight a.app.SaveState() case commands.MessagesCopyCommand: - selected := a.messages.Selected() - if selected != "" { - cmd = tea.SetClipboard(selected) - cmds = append(cmds, cmd) - cmd = toast.NewSuccessToast("Message copied to clipboard") - cmds = append(cmds, cmd) - } + updated, cmd := a.messages.CopyLastMessage() + a.messages = updated.(chat.MessagesComponent) + cmds = append(cmds, cmd) case commands.MessagesRevertCommand: case commands.AppExitCommand: return a, tea.Quit @@ -976,11 +1004,12 @@ func (a appModel) executeCommand(command commands.Command) (tea.Model, tea.Cmd) func NewModel(app *app.App) tea.Model { commandProvider := completions.NewCommandCompletionProvider(app) - fileProvider := completions.NewFileAndFolderContextGroup(app) + fileProvider := completions.NewFileContextGroup(app) + symbolsProvider := completions.NewSymbolsContextGroup(app) messages := chat.NewMessagesComponent(app) editor := chat.NewEditorComponent(app) - completions := dialog.NewCompletionDialogComponent(commandProvider) + completions := dialog.NewCompletionDialogComponent("/", commandProvider) var leaderBinding *key.Binding if app.Config.Keybinds.Leader != "" { @@ -996,15 +1025,56 @@ func NewModel(app *app.App) tea.Model { completions: completions, commandProvider: commandProvider, fileProvider: fileProvider, + symbolsProvider: symbolsProvider, leaderBinding: leaderBinding, - isLeaderSequence: false, showCompletionDialog: false, - fileCompletionActive: false, toastManager: toast.NewToastManager(), interruptKeyState: InterruptKeyIdle, + exitKeyState: ExitKeyIdle, fileViewer: fileviewer.New(app), messagesRight: app.State.MessagesRight, } return model } + +func formatConversationToMarkdown(messages []app.Message) string { + var builder strings.Builder + + builder.WriteString("# Conversation History\n\n") + + for _, msg := range messages { + builder.WriteString("---\n\n") + + var role string + var timestamp time.Time + + switch info := msg.Info.(type) { + case opencode.UserMessage: + role = "User" + timestamp = time.UnixMilli(int64(info.Time.Created)) + case opencode.AssistantMessage: + role = "Assistant" + timestamp = time.UnixMilli(int64(info.Time.Created)) + default: + continue + } + + builder.WriteString( + fmt.Sprintf("**%s** (*%s*)\n\n", role, timestamp.Format("2006-01-02 15:04:05")), + ) + + for _, part := range msg.Parts { + switch p := part.(type) { + case opencode.TextPart: + builder.WriteString(p.Text + "\n\n") + case opencode.FilePart: + builder.WriteString(fmt.Sprintf("[File: %s]\n\n", p.Filename)) + case opencode.ToolPart: + builder.WriteString(fmt.Sprintf("[Tool: %s]\n\n", p.Tool)) + } + } + } + + return builder.String() +} diff --git a/packages/tui/internal/util/apilogger.go b/packages/tui/internal/util/apilogger.go new file mode 100644 index 00000000..b439bbec --- /dev/null +++ b/packages/tui/internal/util/apilogger.go @@ -0,0 +1,123 @@ +package util + +import ( + "context" + "log/slog" + "sync" + + opencode "github.com/sst/opencode-sdk-go" +) + +type APILogHandler struct { + client *opencode.Client + service string + level slog.Level + attrs []slog.Attr + groups []string + mu sync.Mutex +} + +func NewAPILogHandler(client *opencode.Client, service string, level slog.Level) *APILogHandler { + return &APILogHandler{ + client: client, + service: service, + level: level, + attrs: make([]slog.Attr, 0), + groups: make([]string, 0), + } +} + +func (h *APILogHandler) Enabled(_ context.Context, level slog.Level) bool { + return level >= h.level +} + +func (h *APILogHandler) Handle(ctx context.Context, r slog.Record) error { + var apiLevel opencode.AppLogParamsLevel + switch r.Level { + case slog.LevelDebug: + apiLevel = opencode.AppLogParamsLevelDebug + case slog.LevelInfo: + apiLevel = opencode.AppLogParamsLevelInfo + case slog.LevelWarn: + apiLevel = opencode.AppLogParamsLevelWarn + case slog.LevelError: + apiLevel = opencode.AppLogParamsLevelError + default: + apiLevel = opencode.AppLogParamsLevelInfo + } + + extra := make(map[string]any) + + h.mu.Lock() + for _, attr := range h.attrs { + extra[attr.Key] = attr.Value.Any() + } + h.mu.Unlock() + + r.Attrs(func(attr slog.Attr) bool { + extra[attr.Key] = attr.Value.Any() + return true + }) + + params := opencode.AppLogParams{ + Service: opencode.F(h.service), + Level: opencode.F(apiLevel), + Message: opencode.F(r.Message), + } + + if len(extra) > 0 { + params.Extra = opencode.F(extra) + } + + go func() { + _, err := h.client.App.Log(context.Background(), params) + if err != nil { + // Fallback: we can't log the error using slog as it would create a loop + // TODO: fallback file? + } + }() + + return nil +} + +// WithAttrs returns a new Handler whose attributes consist of +// both the receiver's attributes and the arguments. +func (h *APILogHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + h.mu.Lock() + defer h.mu.Unlock() + + newHandler := &APILogHandler{ + client: h.client, + service: h.service, + level: h.level, + attrs: make([]slog.Attr, len(h.attrs)+len(attrs)), + groups: make([]string, len(h.groups)), + } + + copy(newHandler.attrs, h.attrs) + copy(newHandler.attrs[len(h.attrs):], attrs) + copy(newHandler.groups, h.groups) + + return newHandler +} + +// WithGroup returns a new Handler with the given group appended to +// the receiver's existing groups. +func (h *APILogHandler) WithGroup(name string) slog.Handler { + h.mu.Lock() + defer h.mu.Unlock() + + newHandler := &APILogHandler{ + client: h.client, + service: h.service, + level: h.level, + attrs: make([]slog.Attr, len(h.attrs)), + groups: make([]string, len(h.groups)+1), + } + + copy(newHandler.attrs, h.attrs) + copy(newHandler.groups, h.groups) + newHandler.groups[len(h.groups)] = name + + return newHandler +} diff --git a/packages/tui/sdk/.stats.yml b/packages/tui/sdk/.stats.yml index ab6c4a20..22b27ac6 100644 --- a/packages/tui/sdk/.stats.yml +++ b/packages/tui/sdk/.stats.yml @@ -1,4 +1,4 @@ -configured_endpoints: 20 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/opencode%2Fopencode-945f9da9e9a4c4008834deef63e4346c0076e020eed3d3c98c249095033c1ac5.yml -openapi_spec_hash: 522a44f6cb0677435fe2ac7693848ad7 -config_hash: 6c8822d278ba83456e5eed6d774ca230 +configured_endpoints: 22 +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/opencode%2Fopencode-8792f91dd070f7b4ee671fc86e8a03976dc7fb6ee49f8c99ad989e1597003774.yml +openapi_spec_hash: fe9dc3a074be560de0b97df9b5af2c1b +config_hash: b7f3d9742335715c458494988498b183 diff --git a/packages/tui/sdk/README.md b/packages/tui/sdk/README.md index 2b578234..2588b614 100644 --- a/packages/tui/sdk/README.md +++ b/packages/tui/sdk/README.md @@ -49,11 +49,11 @@ import ( func main() { client := opencode.NewClient() - events, err := client.Event.List(context.TODO()) + sessions, err := client.Session.List(context.TODO()) if err != nil { panic(err.Error()) } - fmt.Printf("%+v\n", events) + fmt.Printf("%+v\n", sessions) } ``` @@ -142,7 +142,7 @@ client := opencode.NewClient( option.WithHeader("X-Some-Header", "custom_header_info"), ) -client.Event.List(context.TODO(), ..., +client.Session.List(context.TODO(), ..., // Override the header option.WithHeader("X-Some-Header", "some_other_custom_header_info"), // Add an undocumented field to the request body, using sjson syntax @@ -171,14 +171,14 @@ When the API returns a non-success status code, we return an error with type To handle errors, we recommend that you use the `errors.As` pattern: ```go -_, err := client.Event.List(context.TODO()) +_, err := client.Session.List(context.TODO()) if err != nil { var apierr *opencode.Error if errors.As(err, &apierr) { println(string(apierr.DumpRequest(true))) // Prints the serialized HTTP request println(string(apierr.DumpResponse(true))) // Prints the serialized HTTP response } - panic(err.Error()) // GET "/event": 400 Bad Request { ... } + panic(err.Error()) // GET "/session": 400 Bad Request { ... } } ``` @@ -196,7 +196,7 @@ To set a per-retry timeout, use `option.WithRequestTimeout()`. // This sets the timeout for the request, including all the retries. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) defer cancel() -client.Event.List( +client.Session.List( ctx, // This sets the per-retry timeout option.WithRequestTimeout(20*time.Second), @@ -231,7 +231,7 @@ client := opencode.NewClient( ) // Override per-request: -client.Event.List(context.TODO(), option.WithMaxRetries(5)) +client.Session.List(context.TODO(), option.WithMaxRetries(5)) ``` ### Accessing raw response data (e.g. response headers) @@ -242,11 +242,11 @@ you need to examine response headers, status codes, or other details. ```go // Create a variable to store the HTTP response var response *http.Response -events, err := client.Event.List(context.TODO(), option.WithResponseInto(&response)) +sessions, err := client.Session.List(context.TODO(), option.WithResponseInto(&response)) if err != nil { // handle error } -fmt.Printf("%+v\n", events) +fmt.Printf("%+v\n", sessions) fmt.Printf("Status Code: %d\n", response.StatusCode) fmt.Printf("Headers: %+#v\n", response.Header) diff --git a/packages/tui/sdk/aliases.go b/packages/tui/sdk/aliases.go index 84dd614a..6ab36d04 100644 --- a/packages/tui/sdk/aliases.go +++ b/packages/tui/sdk/aliases.go @@ -9,6 +9,15 @@ import ( type Error = apierror.Error +// This is an alias to an internal type. +type MessageAbortedError = shared.MessageAbortedError + +// This is an alias to an internal type. +type MessageAbortedErrorName = shared.MessageAbortedErrorName + +// This is an alias to an internal value. +const MessageAbortedErrorNameMessageAbortedError = shared.MessageAbortedErrorNameMessageAbortedError + // This is an alias to an internal type. type ProviderAuthError = shared.ProviderAuthError diff --git a/packages/tui/sdk/api.md b/packages/tui/sdk/api.md index 4ac9edcf..1c3f5a5e 100644 --- a/packages/tui/sdk/api.md +++ b/packages/tui/sdk/api.md @@ -1,5 +1,6 @@ # Shared Response Types +- shared.MessageAbortedError - shared.ProviderAuthError - shared.UnknownError @@ -18,83 +19,85 @@ Methods: Response Types: - opencode.App +- opencode.LogLevel +- opencode.Mode +- opencode.Model +- opencode.Provider +- opencode.AppProvidersResponse Methods: - client.App.Get(ctx context.Context) (opencode.App, error) - client.App.Init(ctx context.Context) (bool, error) +- client.App.Log(ctx context.Context, body opencode.AppLogParams) (bool, error) +- client.App.Modes(ctx context.Context) ([]opencode.Mode, error) +- client.App.Providers(ctx context.Context) (opencode.AppProvidersResponse, error) # Find Response Types: -- opencode.FindSymbolsResponse -- opencode.FindTextResponse +- opencode.Match +- opencode.Symbol Methods: - client.Find.Files(ctx context.Context, query opencode.FindFilesParams) ([]string, error) -- client.Find.Symbols(ctx context.Context, query opencode.FindSymbolsParams) ([]opencode.FindSymbolsResponse, error) -- client.Find.Text(ctx context.Context, query opencode.FindTextParams) ([]opencode.FindTextResponse, error) +- client.Find.Symbols(ctx context.Context, query opencode.FindSymbolsParams) ([]opencode.Symbol, error) +- client.Find.Text(ctx context.Context, query opencode.FindTextParams) ([]opencode.Match, error) # File Response Types: +- opencode.File - opencode.FileReadResponse -- opencode.FileStatusResponse Methods: - client.File.Read(ctx context.Context, query opencode.FileReadParams) (opencode.FileReadResponse, error) -- client.File.Status(ctx context.Context) ([]opencode.FileStatusResponse, error) +- client.File.Status(ctx context.Context) ([]opencode.File, error) # Config Response Types: - opencode.Config -- opencode.Keybinds -- opencode.McpLocal -- opencode.McpRemote -- opencode.Model -- opencode.Provider -- opencode.ConfigProvidersResponse +- opencode.KeybindsConfig +- opencode.LayoutConfig +- opencode.McpLocalConfig +- opencode.McpRemoteConfig +- opencode.ModeConfig Methods: - client.Config.Get(ctx context.Context) (opencode.Config, error) -- client.Config.Providers(ctx context.Context) (opencode.ConfigProvidersResponse, error) # Session Params Types: - opencode.FilePartParam -- opencode.MessagePartUnionParam -- opencode.ReasoningPartParam -- opencode.SourceURLPartParam -- opencode.StepStartPartParam - opencode.TextPartParam -- opencode.ToolCallParam -- opencode.ToolInvocationPartParam -- opencode.ToolPartialCallParam -- opencode.ToolResultParam Response Types: +- opencode.AssistantMessage - opencode.FilePart - opencode.Message -- opencode.MessagePart -- opencode.ReasoningPart +- opencode.Part - opencode.Session -- opencode.SourceURLPart +- opencode.SnapshotPart +- opencode.StepFinishPart - opencode.StepStartPart - opencode.TextPart -- opencode.ToolCall -- opencode.ToolInvocationPart -- opencode.ToolPartialCall -- opencode.ToolResult +- opencode.ToolPart +- opencode.ToolStateCompleted +- opencode.ToolStateError +- opencode.ToolStatePending +- opencode.ToolStateRunning +- opencode.UserMessage +- opencode.SessionMessagesResponse Methods: @@ -102,9 +105,9 @@ Methods: - client.Session.List(ctx context.Context) ([]opencode.Session, error) - client.Session.Delete(ctx context.Context, id string) (bool, error) - client.Session.Abort(ctx context.Context, id string) (bool, error) -- client.Session.Chat(ctx context.Context, id string, body opencode.SessionChatParams) (opencode.Message, error) +- client.Session.Chat(ctx context.Context, id string, body opencode.SessionChatParams) (opencode.AssistantMessage, error) - client.Session.Init(ctx context.Context, id string, body opencode.SessionInitParams) (bool, error) -- client.Session.Messages(ctx context.Context, id string) ([]opencode.Message, error) +- client.Session.Messages(ctx context.Context, id string) ([]opencode.SessionMessagesResponse, error) - client.Session.Share(ctx context.Context, id string) (opencode.Session, error) - client.Session.Summarize(ctx context.Context, id string, body opencode.SessionSummarizeParams) (bool, error) - client.Session.Unshare(ctx context.Context, id string) (opencode.Session, error) diff --git a/packages/tui/sdk/app.go b/packages/tui/sdk/app.go index dc44a74b..aa47e83b 100644 --- a/packages/tui/sdk/app.go +++ b/packages/tui/sdk/app.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/sst/opencode-sdk-go/internal/apijson" + "github.com/sst/opencode-sdk-go/internal/param" "github.com/sst/opencode-sdk-go/internal/requestconfig" "github.com/sst/opencode-sdk-go/option" ) @@ -46,12 +47,35 @@ func (r *AppService) Init(ctx context.Context, opts ...option.RequestOption) (re return } +// Write a log entry to the server logs +func (r *AppService) Log(ctx context.Context, body AppLogParams, opts ...option.RequestOption) (res *bool, err error) { + opts = append(r.Options[:], opts...) + path := "log" + err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...) + return +} + +// List all modes +func (r *AppService) Modes(ctx context.Context, opts ...option.RequestOption) (res *[]Mode, err error) { + opts = append(r.Options[:], opts...) + path := "mode" + err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...) + return +} + +// List all providers +func (r *AppService) Providers(ctx context.Context, opts ...option.RequestOption) (res *AppProvidersResponse, err error) { + opts = append(r.Options[:], opts...) + path := "config/providers" + err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...) + return +} + type App struct { Git bool `json:"git,required"` Hostname string `json:"hostname,required"` Path AppPath `json:"path,required"` Time AppTime `json:"time,required"` - User string `json:"user,required"` JSON appJSON `json:"-"` } @@ -61,7 +85,6 @@ type appJSON struct { Hostname apijson.Field Path apijson.Field Time apijson.Field - User apijson.Field raw string ExtraFields map[string]apijson.Field } @@ -121,3 +144,241 @@ func (r *AppTime) UnmarshalJSON(data []byte) (err error) { func (r appTimeJSON) RawJSON() string { return r.raw } + +// Log level +type LogLevel string + +const ( + LogLevelDebug LogLevel = "DEBUG" + LogLevelInfo LogLevel = "INFO" + LogLevelWarn LogLevel = "WARN" + LogLevelError LogLevel = "ERROR" +) + +func (r LogLevel) IsKnown() bool { + switch r { + case LogLevelDebug, LogLevelInfo, LogLevelWarn, LogLevelError: + return true + } + return false +} + +type Mode struct { + Name string `json:"name,required"` + Tools map[string]bool `json:"tools,required"` + Model ModeModel `json:"model"` + Prompt string `json:"prompt"` + JSON modeJSON `json:"-"` +} + +// modeJSON contains the JSON metadata for the struct [Mode] +type modeJSON struct { + Name apijson.Field + Tools apijson.Field + Model apijson.Field + Prompt apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *Mode) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r modeJSON) RawJSON() string { + return r.raw +} + +type ModeModel struct { + ModelID string `json:"modelID,required"` + ProviderID string `json:"providerID,required"` + JSON modeModelJSON `json:"-"` +} + +// modeModelJSON contains the JSON metadata for the struct [ModeModel] +type modeModelJSON struct { + ModelID apijson.Field + ProviderID apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *ModeModel) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r modeModelJSON) RawJSON() string { + return r.raw +} + +type Model struct { + ID string `json:"id,required"` + Attachment bool `json:"attachment,required"` + Cost ModelCost `json:"cost,required"` + Limit ModelLimit `json:"limit,required"` + Name string `json:"name,required"` + Options map[string]interface{} `json:"options,required"` + Reasoning bool `json:"reasoning,required"` + ReleaseDate string `json:"release_date,required"` + Temperature bool `json:"temperature,required"` + ToolCall bool `json:"tool_call,required"` + JSON modelJSON `json:"-"` +} + +// modelJSON contains the JSON metadata for the struct [Model] +type modelJSON struct { + ID apijson.Field + Attachment apijson.Field + Cost apijson.Field + Limit apijson.Field + Name apijson.Field + Options apijson.Field + Reasoning apijson.Field + ReleaseDate apijson.Field + Temperature apijson.Field + ToolCall apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *Model) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r modelJSON) RawJSON() string { + return r.raw +} + +type ModelCost struct { + Input float64 `json:"input,required"` + Output float64 `json:"output,required"` + CacheRead float64 `json:"cache_read"` + CacheWrite float64 `json:"cache_write"` + JSON modelCostJSON `json:"-"` +} + +// modelCostJSON contains the JSON metadata for the struct [ModelCost] +type modelCostJSON struct { + Input apijson.Field + Output apijson.Field + CacheRead apijson.Field + CacheWrite apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *ModelCost) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r modelCostJSON) RawJSON() string { + return r.raw +} + +type ModelLimit struct { + Context float64 `json:"context,required"` + Output float64 `json:"output,required"` + JSON modelLimitJSON `json:"-"` +} + +// modelLimitJSON contains the JSON metadata for the struct [ModelLimit] +type modelLimitJSON struct { + Context apijson.Field + Output apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *ModelLimit) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r modelLimitJSON) RawJSON() string { + return r.raw +} + +type Provider struct { + ID string `json:"id,required"` + Env []string `json:"env,required"` + Models map[string]Model `json:"models,required"` + Name string `json:"name,required"` + API string `json:"api"` + Npm string `json:"npm"` + JSON providerJSON `json:"-"` +} + +// providerJSON contains the JSON metadata for the struct [Provider] +type providerJSON struct { + ID apijson.Field + Env apijson.Field + Models apijson.Field + Name apijson.Field + API apijson.Field + Npm apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *Provider) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r providerJSON) RawJSON() string { + return r.raw +} + +type AppProvidersResponse struct { + Default map[string]string `json:"default,required"` + Providers []Provider `json:"providers,required"` + JSON appProvidersResponseJSON `json:"-"` +} + +// appProvidersResponseJSON contains the JSON metadata for the struct +// [AppProvidersResponse] +type appProvidersResponseJSON struct { + Default apijson.Field + Providers apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *AppProvidersResponse) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r appProvidersResponseJSON) RawJSON() string { + return r.raw +} + +type AppLogParams struct { + // Log level + Level param.Field[AppLogParamsLevel] `json:"level,required"` + // Log message + Message param.Field[string] `json:"message,required"` + // Service name for the log entry + Service param.Field[string] `json:"service,required"` + // Additional metadata for the log entry + Extra param.Field[map[string]interface{}] `json:"extra"` +} + +func (r AppLogParams) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +// Log level +type AppLogParamsLevel string + +const ( + AppLogParamsLevelDebug AppLogParamsLevel = "debug" + AppLogParamsLevelInfo AppLogParamsLevel = "info" + AppLogParamsLevelError AppLogParamsLevel = "error" + AppLogParamsLevelWarn AppLogParamsLevel = "warn" +) + +func (r AppLogParamsLevel) IsKnown() bool { + switch r { + case AppLogParamsLevelDebug, AppLogParamsLevelInfo, AppLogParamsLevelError, AppLogParamsLevelWarn: + return true + } + return false +} diff --git a/packages/tui/sdk/app_test.go b/packages/tui/sdk/app_test.go index f96495f1..16bb8ff8 100644 --- a/packages/tui/sdk/app_test.go +++ b/packages/tui/sdk/app_test.go @@ -56,3 +56,76 @@ func TestAppInit(t *testing.T) { t.Fatalf("err should be nil: %s", err.Error()) } } + +func TestAppLogWithOptionalParams(t *testing.T) { + t.Skip("skipped: tests are disabled for the time being") + baseURL := "http://localhost:4010" + if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok { + baseURL = envURL + } + if !testutil.CheckTestServer(t, baseURL) { + return + } + client := opencode.NewClient( + option.WithBaseURL(baseURL), + ) + _, err := client.App.Log(context.TODO(), opencode.AppLogParams{ + Level: opencode.F(opencode.AppLogParamsLevelDebug), + Message: opencode.F("message"), + Service: opencode.F("service"), + Extra: opencode.F(map[string]interface{}{ + "foo": "bar", + }), + }) + if err != nil { + var apierr *opencode.Error + if errors.As(err, &apierr) { + t.Log(string(apierr.DumpRequest(true))) + } + t.Fatalf("err should be nil: %s", err.Error()) + } +} + +func TestAppModes(t *testing.T) { + t.Skip("skipped: tests are disabled for the time being") + baseURL := "http://localhost:4010" + if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok { + baseURL = envURL + } + if !testutil.CheckTestServer(t, baseURL) { + return + } + client := opencode.NewClient( + option.WithBaseURL(baseURL), + ) + _, err := client.App.Modes(context.TODO()) + if err != nil { + var apierr *opencode.Error + if errors.As(err, &apierr) { + t.Log(string(apierr.DumpRequest(true))) + } + t.Fatalf("err should be nil: %s", err.Error()) + } +} + +func TestAppProviders(t *testing.T) { + t.Skip("skipped: tests are disabled for the time being") + baseURL := "http://localhost:4010" + if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok { + baseURL = envURL + } + if !testutil.CheckTestServer(t, baseURL) { + return + } + client := opencode.NewClient( + option.WithBaseURL(baseURL), + ) + _, err := client.App.Providers(context.TODO()) + if err != nil { + var apierr *opencode.Error + if errors.As(err, &apierr) { + t.Log(string(apierr.DumpRequest(true))) + } + t.Fatalf("err should be nil: %s", err.Error()) + } +} diff --git a/packages/tui/sdk/client_test.go b/packages/tui/sdk/client_test.go index e75d6492..0f5b8205 100644 --- a/packages/tui/sdk/client_test.go +++ b/packages/tui/sdk/client_test.go @@ -38,7 +38,7 @@ func TestUserAgentHeader(t *testing.T) { }, }), ) - client.Event.List(context.Background()) + client.Session.List(context.Background()) if userAgent != fmt.Sprintf("Opencode/Go %s", internal.PackageVersion) { t.Errorf("Expected User-Agent to be correct, but got: %#v", userAgent) } @@ -61,7 +61,7 @@ func TestRetryAfter(t *testing.T) { }, }), ) - _, err := client.Event.List(context.Background()) + _, err := client.Session.List(context.Background()) if err == nil { t.Error("Expected there to be a cancel error") } @@ -95,7 +95,7 @@ func TestDeleteRetryCountHeader(t *testing.T) { }), option.WithHeaderDel("X-Stainless-Retry-Count"), ) - _, err := client.Event.List(context.Background()) + _, err := client.Session.List(context.Background()) if err == nil { t.Error("Expected there to be a cancel error") } @@ -124,7 +124,7 @@ func TestOverwriteRetryCountHeader(t *testing.T) { }), option.WithHeader("X-Stainless-Retry-Count", "42"), ) - _, err := client.Event.List(context.Background()) + _, err := client.Session.List(context.Background()) if err == nil { t.Error("Expected there to be a cancel error") } @@ -152,7 +152,7 @@ func TestRetryAfterMs(t *testing.T) { }, }), ) - _, err := client.Event.List(context.Background()) + _, err := client.Session.List(context.Background()) if err == nil { t.Error("Expected there to be a cancel error") } @@ -174,7 +174,7 @@ func TestContextCancel(t *testing.T) { ) cancelCtx, cancel := context.WithCancel(context.Background()) cancel() - _, err := client.Event.List(cancelCtx) + _, err := client.Session.List(cancelCtx) if err == nil { t.Error("Expected there to be a cancel error") } @@ -193,7 +193,7 @@ func TestContextCancelDelay(t *testing.T) { ) cancelCtx, cancel := context.WithTimeout(context.Background(), 2*time.Millisecond) defer cancel() - _, err := client.Event.List(cancelCtx) + _, err := client.Session.List(cancelCtx) if err == nil { t.Error("expected there to be a cancel error") } @@ -218,7 +218,7 @@ func TestContextDeadline(t *testing.T) { }, }), ) - _, err := client.Event.List(deadlineCtx) + _, err := client.Session.List(deadlineCtx) if err == nil { t.Error("expected there to be a deadline error") } diff --git a/packages/tui/sdk/config.go b/packages/tui/sdk/config.go index 39da2f94..cf89e2d9 100644 --- a/packages/tui/sdk/config.go +++ b/packages/tui/sdk/config.go @@ -40,18 +40,11 @@ func (r *ConfigService) Get(ctx context.Context, opts ...option.RequestOption) ( return } -// List all providers -func (r *ConfigService) Providers(ctx context.Context, opts ...option.RequestOption) (res *ConfigProvidersResponse, err error) { - opts = append(r.Options[:], opts...) - path := "config/providers" - err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...) - return -} - type Config struct { // JSON schema reference for configuration validation Schema string `json:"$schema"` - // Share newly created sessions automatically + // @deprecated Use 'share' field instead. Share newly created sessions + // automatically Autoshare bool `json:"autoshare"` // Automatically update to the latest version Autoupdate bool `json:"autoupdate"` @@ -61,16 +54,27 @@ type Config struct { // Additional instruction files or patterns to include Instructions []string `json:"instructions"` // Custom keybind configurations - Keybinds Keybinds `json:"keybinds"` + Keybinds KeybindsConfig `json:"keybinds"` + // Layout to use for the TUI + Layout LayoutConfig `json:"layout"` + // Minimum log level to write to log files + LogLevel LogLevel `json:"log_level"` // MCP (Model Context Protocol) server configurations Mcp map[string]ConfigMcp `json:"mcp"` + // Modes configuration, see https://opencode.ai/docs/modes + Mode ConfigMode `json:"mode"` // Model to use in the format of provider/model, eg anthropic/claude-2 Model string `json:"model"` // Custom provider configurations and model overrides Provider map[string]ConfigProvider `json:"provider"` + // Control sharing behavior: 'auto' enables automatic sharing, 'disabled' disables + // all sharing + Share ConfigShare `json:"share"` // Theme name to use for the interface - Theme string `json:"theme"` - JSON configJSON `json:"-"` + Theme string `json:"theme"` + // Custom username to display in conversations instead of system username + Username string `json:"username"` + JSON configJSON `json:"-"` } // configJSON contains the JSON metadata for the struct [Config] @@ -82,10 +86,15 @@ type configJSON struct { Experimental apijson.Field Instructions apijson.Field Keybinds apijson.Field + Layout apijson.Field + LogLevel apijson.Field Mcp apijson.Field + Mode apijson.Field Model apijson.Field Provider apijson.Field + Share apijson.Field Theme apijson.Field + Username apijson.Field raw string ExtraFields map[string]apijson.Field } @@ -230,12 +239,12 @@ func (r *ConfigMcp) UnmarshalJSON(data []byte) (err error) { // AsUnion returns a [ConfigMcpUnion] interface which you can cast to the specific // types for more type safety. // -// Possible runtime types of the union are [McpLocal], [McpRemote]. +// Possible runtime types of the union are [McpLocalConfig], [McpRemoteConfig]. func (r ConfigMcp) AsUnion() ConfigMcpUnion { return r.union } -// Union satisfied by [McpLocal] or [McpRemote]. +// Union satisfied by [McpLocalConfig] or [McpRemoteConfig]. type ConfigMcpUnion interface { implementsConfigMcp() } @@ -246,12 +255,12 @@ func init() { "type", apijson.UnionVariant{ TypeFilter: gjson.JSON, - Type: reflect.TypeOf(McpLocal{}), + Type: reflect.TypeOf(McpLocalConfig{}), DiscriminatorValue: "local", }, apijson.UnionVariant{ TypeFilter: gjson.JSON, - Type: reflect.TypeOf(McpRemote{}), + Type: reflect.TypeOf(McpRemoteConfig{}), DiscriminatorValue: "remote", }, ) @@ -273,6 +282,30 @@ func (r ConfigMcpType) IsKnown() bool { return false } +// Modes configuration, see https://opencode.ai/docs/modes +type ConfigMode struct { + Build ModeConfig `json:"build"` + Plan ModeConfig `json:"plan"` + ExtraFields map[string]ModeConfig `json:"-,extras"` + JSON configModeJSON `json:"-"` +} + +// configModeJSON contains the JSON metadata for the struct [ConfigMode] +type configModeJSON struct { + Build apijson.Field + Plan apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *ConfigMode) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r configModeJSON) RawJSON() string { + return r.raw +} + type ConfigProvider struct { Models map[string]ConfigProviderModel `json:"models,required"` ID string `json:"id"` @@ -394,119 +427,178 @@ func (r configProviderModelsLimitJSON) RawJSON() string { return r.raw } -type Keybinds struct { - // Exit the application - AppExit string `json:"app_exit"` - // Open external editor - EditorOpen string `json:"editor_open"` - // Show help dialog - Help string `json:"help"` - // Navigate to next history item - HistoryNext string `json:"history_next"` - // Navigate to previous history item - HistoryPrevious string `json:"history_previous"` - // Clear input field - InputClear string `json:"input_clear"` - // Insert newline in input - InputNewline string `json:"input_newline"` - // Paste from clipboard - InputPaste string `json:"input_paste"` - // Submit input - InputSubmit string `json:"input_submit"` - // Leader key for keybind combinations - Leader string `json:"leader"` - // Navigate to first message - MessagesFirst string `json:"messages_first"` - // Scroll messages down by half page - MessagesHalfPageDown string `json:"messages_half_page_down"` - // Scroll messages up by half page - MessagesHalfPageUp string `json:"messages_half_page_up"` - // Navigate to last message - MessagesLast string `json:"messages_last"` - // Navigate to next message - MessagesNext string `json:"messages_next"` - // Scroll messages down by one page - MessagesPageDown string `json:"messages_page_down"` - // Scroll messages up by one page - MessagesPageUp string `json:"messages_page_up"` - // Navigate to previous message - MessagesPrevious string `json:"messages_previous"` - // List available models - ModelList string `json:"model_list"` - // Initialize project configuration - ProjectInit string `json:"project_init"` - // Toggle compact mode for session - SessionCompact string `json:"session_compact"` - // Interrupt current session - SessionInterrupt string `json:"session_interrupt"` - // List all sessions - SessionList string `json:"session_list"` - // Create a new session - SessionNew string `json:"session_new"` - // Share current session - SessionShare string `json:"session_share"` - // List available themes - ThemeList string `json:"theme_list"` - // Show tool details - ToolDetails string `json:"tool_details"` - JSON keybindsJSON `json:"-"` +// Control sharing behavior: 'auto' enables automatic sharing, 'disabled' disables +// all sharing +type ConfigShare string + +const ( + ConfigShareAuto ConfigShare = "auto" + ConfigShareDisabled ConfigShare = "disabled" +) + +func (r ConfigShare) IsKnown() bool { + switch r { + case ConfigShareAuto, ConfigShareDisabled: + return true + } + return false } -// keybindsJSON contains the JSON metadata for the struct [Keybinds] -type keybindsJSON struct { +type KeybindsConfig struct { + // Exit the application + AppExit string `json:"app_exit,required"` + // Show help dialog + AppHelp string `json:"app_help,required"` + // Open external editor + EditorOpen string `json:"editor_open,required"` + // Close file + FileClose string `json:"file_close,required"` + // Split/unified diff + FileDiffToggle string `json:"file_diff_toggle,required"` + // List files + FileList string `json:"file_list,required"` + // Search file + FileSearch string `json:"file_search,required"` + // Clear input field + InputClear string `json:"input_clear,required"` + // Insert newline in input + InputNewline string `json:"input_newline,required"` + // Paste from clipboard + InputPaste string `json:"input_paste,required"` + // Submit input + InputSubmit string `json:"input_submit,required"` + // Leader key for keybind combinations + Leader string `json:"leader,required"` + // Copy message + MessagesCopy string `json:"messages_copy,required"` + // Navigate to first message + MessagesFirst string `json:"messages_first,required"` + // Scroll messages down by half page + MessagesHalfPageDown string `json:"messages_half_page_down,required"` + // Scroll messages up by half page + MessagesHalfPageUp string `json:"messages_half_page_up,required"` + // Navigate to last message + MessagesLast string `json:"messages_last,required"` + // Toggle layout + MessagesLayoutToggle string `json:"messages_layout_toggle,required"` + // Navigate to next message + MessagesNext string `json:"messages_next,required"` + // Scroll messages down by one page + MessagesPageDown string `json:"messages_page_down,required"` + // Scroll messages up by one page + MessagesPageUp string `json:"messages_page_up,required"` + // Navigate to previous message + MessagesPrevious string `json:"messages_previous,required"` + // Revert message + MessagesRevert string `json:"messages_revert,required"` + // List available models + ModelList string `json:"model_list,required"` + // Create/update AGENTS.md + ProjectInit string `json:"project_init,required"` + // Compact the session + SessionCompact string `json:"session_compact,required"` + // Export session to editor + SessionExport string `json:"session_export,required"` + // Interrupt current session + SessionInterrupt string `json:"session_interrupt,required"` + // List all sessions + SessionList string `json:"session_list,required"` + // Create a new session + SessionNew string `json:"session_new,required"` + // Share current session + SessionShare string `json:"session_share,required"` + // Unshare current session + SessionUnshare string `json:"session_unshare,required"` + // Switch mode + SwitchMode string `json:"switch_mode,required"` + // Switch mode reverse + SwitchModeReverse string `json:"switch_mode_reverse,required"` + // List available themes + ThemeList string `json:"theme_list,required"` + // Toggle tool details + ToolDetails string `json:"tool_details,required"` + JSON keybindsConfigJSON `json:"-"` +} + +// keybindsConfigJSON contains the JSON metadata for the struct [KeybindsConfig] +type keybindsConfigJSON struct { AppExit apijson.Field + AppHelp apijson.Field EditorOpen apijson.Field - Help apijson.Field - HistoryNext apijson.Field - HistoryPrevious apijson.Field + FileClose apijson.Field + FileDiffToggle apijson.Field + FileList apijson.Field + FileSearch apijson.Field InputClear apijson.Field InputNewline apijson.Field InputPaste apijson.Field InputSubmit apijson.Field Leader apijson.Field + MessagesCopy apijson.Field MessagesFirst apijson.Field MessagesHalfPageDown apijson.Field MessagesHalfPageUp apijson.Field MessagesLast apijson.Field + MessagesLayoutToggle apijson.Field MessagesNext apijson.Field MessagesPageDown apijson.Field MessagesPageUp apijson.Field MessagesPrevious apijson.Field + MessagesRevert apijson.Field ModelList apijson.Field ProjectInit apijson.Field SessionCompact apijson.Field + SessionExport apijson.Field SessionInterrupt apijson.Field SessionList apijson.Field SessionNew apijson.Field SessionShare apijson.Field + SessionUnshare apijson.Field + SwitchMode apijson.Field + SwitchModeReverse apijson.Field ThemeList apijson.Field ToolDetails apijson.Field raw string ExtraFields map[string]apijson.Field } -func (r *Keybinds) UnmarshalJSON(data []byte) (err error) { +func (r *KeybindsConfig) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } -func (r keybindsJSON) RawJSON() string { +func (r keybindsConfigJSON) RawJSON() string { return r.raw } -type McpLocal struct { +type LayoutConfig string + +const ( + LayoutConfigAuto LayoutConfig = "auto" + LayoutConfigStretch LayoutConfig = "stretch" +) + +func (r LayoutConfig) IsKnown() bool { + switch r { + case LayoutConfigAuto, LayoutConfigStretch: + return true + } + return false +} + +type McpLocalConfig struct { // Command and arguments to run the MCP server Command []string `json:"command,required"` // Type of MCP server connection - Type McpLocalType `json:"type,required"` + Type McpLocalConfigType `json:"type,required"` // Enable or disable the MCP server on startup Enabled bool `json:"enabled"` // Environment variables to set when running the MCP server - Environment map[string]string `json:"environment"` - JSON mcpLocalJSON `json:"-"` + Environment map[string]string `json:"environment"` + JSON mcpLocalConfigJSON `json:"-"` } -// mcpLocalJSON contains the JSON metadata for the struct [McpLocal] -type mcpLocalJSON struct { +// mcpLocalConfigJSON contains the JSON metadata for the struct [McpLocalConfig] +type mcpLocalConfigJSON struct { Command apijson.Field Type apijson.Field Enabled apijson.Field @@ -515,43 +607,43 @@ type mcpLocalJSON struct { ExtraFields map[string]apijson.Field } -func (r *McpLocal) UnmarshalJSON(data []byte) (err error) { +func (r *McpLocalConfig) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } -func (r mcpLocalJSON) RawJSON() string { +func (r mcpLocalConfigJSON) RawJSON() string { return r.raw } -func (r McpLocal) implementsConfigMcp() {} +func (r McpLocalConfig) implementsConfigMcp() {} // Type of MCP server connection -type McpLocalType string +type McpLocalConfigType string const ( - McpLocalTypeLocal McpLocalType = "local" + McpLocalConfigTypeLocal McpLocalConfigType = "local" ) -func (r McpLocalType) IsKnown() bool { +func (r McpLocalConfigType) IsKnown() bool { switch r { - case McpLocalTypeLocal: + case McpLocalConfigTypeLocal: return true } return false } -type McpRemote struct { +type McpRemoteConfig struct { // Type of MCP server connection - Type McpRemoteType `json:"type,required"` + Type McpRemoteConfigType `json:"type,required"` // URL of the remote MCP server URL string `json:"url,required"` // Enable or disable the MCP server on startup - Enabled bool `json:"enabled"` - JSON mcpRemoteJSON `json:"-"` + Enabled bool `json:"enabled"` + JSON mcpRemoteConfigJSON `json:"-"` } -// mcpRemoteJSON contains the JSON metadata for the struct [McpRemote] -type mcpRemoteJSON struct { +// mcpRemoteConfigJSON contains the JSON metadata for the struct [McpRemoteConfig] +type mcpRemoteConfigJSON struct { Type apijson.Field URL apijson.Field Enabled apijson.Field @@ -559,166 +651,51 @@ type mcpRemoteJSON struct { ExtraFields map[string]apijson.Field } -func (r *McpRemote) UnmarshalJSON(data []byte) (err error) { +func (r *McpRemoteConfig) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } -func (r mcpRemoteJSON) RawJSON() string { +func (r mcpRemoteConfigJSON) RawJSON() string { return r.raw } -func (r McpRemote) implementsConfigMcp() {} +func (r McpRemoteConfig) implementsConfigMcp() {} // Type of MCP server connection -type McpRemoteType string +type McpRemoteConfigType string const ( - McpRemoteTypeRemote McpRemoteType = "remote" + McpRemoteConfigTypeRemote McpRemoteConfigType = "remote" ) -func (r McpRemoteType) IsKnown() bool { +func (r McpRemoteConfigType) IsKnown() bool { switch r { - case McpRemoteTypeRemote: + case McpRemoteConfigTypeRemote: return true } return false } -type Model struct { - ID string `json:"id,required"` - Attachment bool `json:"attachment,required"` - Cost ModelCost `json:"cost,required"` - Limit ModelLimit `json:"limit,required"` - Name string `json:"name,required"` - Options map[string]interface{} `json:"options,required"` - Reasoning bool `json:"reasoning,required"` - ReleaseDate string `json:"release_date,required"` - Temperature bool `json:"temperature,required"` - ToolCall bool `json:"tool_call,required"` - JSON modelJSON `json:"-"` +type ModeConfig struct { + Model string `json:"model"` + Prompt string `json:"prompt"` + Tools map[string]bool `json:"tools"` + JSON modeConfigJSON `json:"-"` } -// modelJSON contains the JSON metadata for the struct [Model] -type modelJSON struct { - ID apijson.Field - Attachment apijson.Field - Cost apijson.Field - Limit apijson.Field - Name apijson.Field - Options apijson.Field - Reasoning apijson.Field - ReleaseDate apijson.Field - Temperature apijson.Field - ToolCall apijson.Field +// modeConfigJSON contains the JSON metadata for the struct [ModeConfig] +type modeConfigJSON struct { + Model apijson.Field + Prompt apijson.Field + Tools apijson.Field raw string ExtraFields map[string]apijson.Field } -func (r *Model) UnmarshalJSON(data []byte) (err error) { +func (r *ModeConfig) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } -func (r modelJSON) RawJSON() string { - return r.raw -} - -type ModelCost struct { - Input float64 `json:"input,required"` - Output float64 `json:"output,required"` - CacheRead float64 `json:"cache_read"` - CacheWrite float64 `json:"cache_write"` - JSON modelCostJSON `json:"-"` -} - -// modelCostJSON contains the JSON metadata for the struct [ModelCost] -type modelCostJSON struct { - Input apijson.Field - Output apijson.Field - CacheRead apijson.Field - CacheWrite apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *ModelCost) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r modelCostJSON) RawJSON() string { - return r.raw -} - -type ModelLimit struct { - Context float64 `json:"context,required"` - Output float64 `json:"output,required"` - JSON modelLimitJSON `json:"-"` -} - -// modelLimitJSON contains the JSON metadata for the struct [ModelLimit] -type modelLimitJSON struct { - Context apijson.Field - Output apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *ModelLimit) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r modelLimitJSON) RawJSON() string { - return r.raw -} - -type Provider struct { - ID string `json:"id,required"` - Env []string `json:"env,required"` - Models map[string]Model `json:"models,required"` - Name string `json:"name,required"` - API string `json:"api"` - Npm string `json:"npm"` - JSON providerJSON `json:"-"` -} - -// providerJSON contains the JSON metadata for the struct [Provider] -type providerJSON struct { - ID apijson.Field - Env apijson.Field - Models apijson.Field - Name apijson.Field - API apijson.Field - Npm apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *Provider) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r providerJSON) RawJSON() string { - return r.raw -} - -type ConfigProvidersResponse struct { - Default map[string]string `json:"default,required"` - Providers []Provider `json:"providers,required"` - JSON configProvidersResponseJSON `json:"-"` -} - -// configProvidersResponseJSON contains the JSON metadata for the struct -// [ConfigProvidersResponse] -type configProvidersResponseJSON struct { - Default apijson.Field - Providers apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *ConfigProvidersResponse) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r configProvidersResponseJSON) RawJSON() string { +func (r modeConfigJSON) RawJSON() string { return r.raw } diff --git a/packages/tui/sdk/config_test.go b/packages/tui/sdk/config_test.go index 57a1d158..86e058a9 100644 --- a/packages/tui/sdk/config_test.go +++ b/packages/tui/sdk/config_test.go @@ -34,25 +34,3 @@ func TestConfigGet(t *testing.T) { t.Fatalf("err should be nil: %s", err.Error()) } } - -func TestConfigProviders(t *testing.T) { - t.Skip("skipped: tests are disabled for the time being") - baseURL := "http://localhost:4010" - if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok { - baseURL = envURL - } - if !testutil.CheckTestServer(t, baseURL) { - return - } - client := opencode.NewClient( - option.WithBaseURL(baseURL), - ) - _, err := client.Config.Providers(context.TODO()) - if err != nil { - var apierr *opencode.Error - if errors.As(err, &apierr) { - t.Log(string(apierr.DumpRequest(true))) - } - t.Fatalf("err should be nil: %s", err.Error()) - } -} diff --git a/packages/tui/sdk/event.go b/packages/tui/sdk/event.go index ed92b2ae..9002d2aa 100644 --- a/packages/tui/sdk/event.go +++ b/packages/tui/sdk/event.go @@ -51,11 +51,11 @@ type EventListResponse struct { // [EventListResponseEventLspClientDiagnosticsProperties], // [EventListResponseEventPermissionUpdatedProperties], // [EventListResponseEventFileEditedProperties], - // [EventListResponseEventStorageWriteProperties], // [EventListResponseEventInstallationUpdatedProperties], // [EventListResponseEventMessageUpdatedProperties], // [EventListResponseEventMessageRemovedProperties], // [EventListResponseEventMessagePartUpdatedProperties], + // [EventListResponseEventStorageWriteProperties], // [EventListResponseEventSessionUpdatedProperties], // [EventListResponseEventSessionDeletedProperties], // [EventListResponseEventSessionIdleProperties], @@ -95,12 +95,12 @@ func (r *EventListResponse) UnmarshalJSON(data []byte) (err error) { // Possible runtime types of the union are // [EventListResponseEventLspClientDiagnostics], // [EventListResponseEventPermissionUpdated], [EventListResponseEventFileEdited], -// [EventListResponseEventStorageWrite], // [EventListResponseEventInstallationUpdated], // [EventListResponseEventMessageUpdated], [EventListResponseEventMessageRemoved], // [EventListResponseEventMessagePartUpdated], -// [EventListResponseEventSessionUpdated], [EventListResponseEventSessionDeleted], -// [EventListResponseEventSessionIdle], [EventListResponseEventSessionError], +// [EventListResponseEventStorageWrite], [EventListResponseEventSessionUpdated], +// [EventListResponseEventSessionDeleted], [EventListResponseEventSessionIdle], +// [EventListResponseEventSessionError], // [EventListResponseEventFileWatcherUpdated]. func (r EventListResponse) AsUnion() EventListResponseUnion { return r.union @@ -108,12 +108,12 @@ func (r EventListResponse) AsUnion() EventListResponseUnion { // Union satisfied by [EventListResponseEventLspClientDiagnostics], // [EventListResponseEventPermissionUpdated], [EventListResponseEventFileEdited], -// [EventListResponseEventStorageWrite], // [EventListResponseEventInstallationUpdated], // [EventListResponseEventMessageUpdated], [EventListResponseEventMessageRemoved], // [EventListResponseEventMessagePartUpdated], -// [EventListResponseEventSessionUpdated], [EventListResponseEventSessionDeleted], -// [EventListResponseEventSessionIdle], [EventListResponseEventSessionError] or +// [EventListResponseEventStorageWrite], [EventListResponseEventSessionUpdated], +// [EventListResponseEventSessionDeleted], [EventListResponseEventSessionIdle], +// [EventListResponseEventSessionError] or // [EventListResponseEventFileWatcherUpdated]. type EventListResponseUnion interface { implementsEventListResponse() @@ -138,11 +138,6 @@ func init() { Type: reflect.TypeOf(EventListResponseEventFileEdited{}), DiscriminatorValue: "file.edited", }, - apijson.UnionVariant{ - TypeFilter: gjson.JSON, - Type: reflect.TypeOf(EventListResponseEventStorageWrite{}), - DiscriminatorValue: "storage.write", - }, apijson.UnionVariant{ TypeFilter: gjson.JSON, Type: reflect.TypeOf(EventListResponseEventInstallationUpdated{}), @@ -163,6 +158,11 @@ func init() { Type: reflect.TypeOf(EventListResponseEventMessagePartUpdated{}), DiscriminatorValue: "message.part.updated", }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(EventListResponseEventStorageWrite{}), + DiscriminatorValue: "storage.write", + }, apijson.UnionVariant{ TypeFilter: gjson.JSON, Type: reflect.TypeOf(EventListResponseEventSessionUpdated{}), @@ -402,68 +402,6 @@ func (r EventListResponseEventFileEditedType) IsKnown() bool { return false } -type EventListResponseEventStorageWrite struct { - Properties EventListResponseEventStorageWriteProperties `json:"properties,required"` - Type EventListResponseEventStorageWriteType `json:"type,required"` - JSON eventListResponseEventStorageWriteJSON `json:"-"` -} - -// eventListResponseEventStorageWriteJSON contains the JSON metadata for the struct -// [EventListResponseEventStorageWrite] -type eventListResponseEventStorageWriteJSON struct { - Properties apijson.Field - Type apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *EventListResponseEventStorageWrite) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r eventListResponseEventStorageWriteJSON) RawJSON() string { - return r.raw -} - -func (r EventListResponseEventStorageWrite) implementsEventListResponse() {} - -type EventListResponseEventStorageWriteProperties struct { - Key string `json:"key,required"` - Content interface{} `json:"content"` - JSON eventListResponseEventStorageWritePropertiesJSON `json:"-"` -} - -// eventListResponseEventStorageWritePropertiesJSON contains the JSON metadata for -// the struct [EventListResponseEventStorageWriteProperties] -type eventListResponseEventStorageWritePropertiesJSON struct { - Key apijson.Field - Content apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *EventListResponseEventStorageWriteProperties) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r eventListResponseEventStorageWritePropertiesJSON) RawJSON() string { - return r.raw -} - -type EventListResponseEventStorageWriteType string - -const ( - EventListResponseEventStorageWriteTypeStorageWrite EventListResponseEventStorageWriteType = "storage.write" -) - -func (r EventListResponseEventStorageWriteType) IsKnown() bool { - switch r { - case EventListResponseEventStorageWriteTypeStorageWrite: - return true - } - return false -} - type EventListResponseEventInstallationUpdated struct { Properties EventListResponseEventInstallationUpdatedProperties `json:"properties,required"` Type EventListResponseEventInstallationUpdatedType `json:"type,required"` @@ -672,18 +610,14 @@ func (r eventListResponseEventMessagePartUpdatedJSON) RawJSON() string { func (r EventListResponseEventMessagePartUpdated) implementsEventListResponse() {} type EventListResponseEventMessagePartUpdatedProperties struct { - MessageID string `json:"messageID,required"` - Part MessagePart `json:"part,required"` - SessionID string `json:"sessionID,required"` - JSON eventListResponseEventMessagePartUpdatedPropertiesJSON `json:"-"` + Part Part `json:"part,required"` + JSON eventListResponseEventMessagePartUpdatedPropertiesJSON `json:"-"` } // eventListResponseEventMessagePartUpdatedPropertiesJSON contains the JSON // metadata for the struct [EventListResponseEventMessagePartUpdatedProperties] type eventListResponseEventMessagePartUpdatedPropertiesJSON struct { - MessageID apijson.Field Part apijson.Field - SessionID apijson.Field raw string ExtraFields map[string]apijson.Field } @@ -710,6 +644,68 @@ func (r EventListResponseEventMessagePartUpdatedType) IsKnown() bool { return false } +type EventListResponseEventStorageWrite struct { + Properties EventListResponseEventStorageWriteProperties `json:"properties,required"` + Type EventListResponseEventStorageWriteType `json:"type,required"` + JSON eventListResponseEventStorageWriteJSON `json:"-"` +} + +// eventListResponseEventStorageWriteJSON contains the JSON metadata for the struct +// [EventListResponseEventStorageWrite] +type eventListResponseEventStorageWriteJSON struct { + Properties apijson.Field + Type apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *EventListResponseEventStorageWrite) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r eventListResponseEventStorageWriteJSON) RawJSON() string { + return r.raw +} + +func (r EventListResponseEventStorageWrite) implementsEventListResponse() {} + +type EventListResponseEventStorageWriteProperties struct { + Key string `json:"key,required"` + Content interface{} `json:"content"` + JSON eventListResponseEventStorageWritePropertiesJSON `json:"-"` +} + +// eventListResponseEventStorageWritePropertiesJSON contains the JSON metadata for +// the struct [EventListResponseEventStorageWriteProperties] +type eventListResponseEventStorageWritePropertiesJSON struct { + Key apijson.Field + Content apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *EventListResponseEventStorageWriteProperties) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r eventListResponseEventStorageWritePropertiesJSON) RawJSON() string { + return r.raw +} + +type EventListResponseEventStorageWriteType string + +const ( + EventListResponseEventStorageWriteTypeStorageWrite EventListResponseEventStorageWriteType = "storage.write" +) + +func (r EventListResponseEventStorageWriteType) IsKnown() bool { + switch r { + case EventListResponseEventStorageWriteTypeStorageWrite: + return true + } + return false +} + type EventListResponseEventSessionUpdated struct { Properties EventListResponseEventSessionUpdatedProperties `json:"properties,required"` Type EventListResponseEventSessionUpdatedType `json:"type,required"` @@ -916,14 +912,16 @@ func (r eventListResponseEventSessionErrorJSON) RawJSON() string { func (r EventListResponseEventSessionError) implementsEventListResponse() {} type EventListResponseEventSessionErrorProperties struct { - Error EventListResponseEventSessionErrorPropertiesError `json:"error"` - JSON eventListResponseEventSessionErrorPropertiesJSON `json:"-"` + Error EventListResponseEventSessionErrorPropertiesError `json:"error"` + SessionID string `json:"sessionID"` + JSON eventListResponseEventSessionErrorPropertiesJSON `json:"-"` } // eventListResponseEventSessionErrorPropertiesJSON contains the JSON metadata for // the struct [EventListResponseEventSessionErrorProperties] type eventListResponseEventSessionErrorPropertiesJSON struct { Error apijson.Field + SessionID apijson.Field raw string ExtraFields map[string]apijson.Field } @@ -972,13 +970,15 @@ func (r *EventListResponseEventSessionErrorPropertiesError) UnmarshalJSON(data [ // // Possible runtime types of the union are [shared.ProviderAuthError], // [shared.UnknownError], -// [EventListResponseEventSessionErrorPropertiesErrorMessageOutputLengthError]. +// [EventListResponseEventSessionErrorPropertiesErrorMessageOutputLengthError], +// [shared.MessageAbortedError]. func (r EventListResponseEventSessionErrorPropertiesError) AsUnion() EventListResponseEventSessionErrorPropertiesErrorUnion { return r.union } -// Union satisfied by [shared.ProviderAuthError], [shared.UnknownError] or -// [EventListResponseEventSessionErrorPropertiesErrorMessageOutputLengthError]. +// Union satisfied by [shared.ProviderAuthError], [shared.UnknownError], +// [EventListResponseEventSessionErrorPropertiesErrorMessageOutputLengthError] or +// [shared.MessageAbortedError]. type EventListResponseEventSessionErrorPropertiesErrorUnion interface { ImplementsEventListResponseEventSessionErrorPropertiesError() } @@ -1002,6 +1002,11 @@ func init() { Type: reflect.TypeOf(EventListResponseEventSessionErrorPropertiesErrorMessageOutputLengthError{}), DiscriminatorValue: "MessageOutputLengthError", }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(shared.MessageAbortedError{}), + DiscriminatorValue: "MessageAbortedError", + }, ) } @@ -1052,11 +1057,12 @@ const ( EventListResponseEventSessionErrorPropertiesErrorNameProviderAuthError EventListResponseEventSessionErrorPropertiesErrorName = "ProviderAuthError" EventListResponseEventSessionErrorPropertiesErrorNameUnknownError EventListResponseEventSessionErrorPropertiesErrorName = "UnknownError" EventListResponseEventSessionErrorPropertiesErrorNameMessageOutputLengthError EventListResponseEventSessionErrorPropertiesErrorName = "MessageOutputLengthError" + EventListResponseEventSessionErrorPropertiesErrorNameMessageAbortedError EventListResponseEventSessionErrorPropertiesErrorName = "MessageAbortedError" ) func (r EventListResponseEventSessionErrorPropertiesErrorName) IsKnown() bool { switch r { - case EventListResponseEventSessionErrorPropertiesErrorNameProviderAuthError, EventListResponseEventSessionErrorPropertiesErrorNameUnknownError, EventListResponseEventSessionErrorPropertiesErrorNameMessageOutputLengthError: + case EventListResponseEventSessionErrorPropertiesErrorNameProviderAuthError, EventListResponseEventSessionErrorPropertiesErrorNameUnknownError, EventListResponseEventSessionErrorPropertiesErrorNameMessageOutputLengthError, EventListResponseEventSessionErrorPropertiesErrorNameMessageAbortedError: return true } return false @@ -1159,11 +1165,11 @@ const ( EventListResponseTypeLspClientDiagnostics EventListResponseType = "lsp.client.diagnostics" EventListResponseTypePermissionUpdated EventListResponseType = "permission.updated" EventListResponseTypeFileEdited EventListResponseType = "file.edited" - EventListResponseTypeStorageWrite EventListResponseType = "storage.write" EventListResponseTypeInstallationUpdated EventListResponseType = "installation.updated" EventListResponseTypeMessageUpdated EventListResponseType = "message.updated" EventListResponseTypeMessageRemoved EventListResponseType = "message.removed" EventListResponseTypeMessagePartUpdated EventListResponseType = "message.part.updated" + EventListResponseTypeStorageWrite EventListResponseType = "storage.write" EventListResponseTypeSessionUpdated EventListResponseType = "session.updated" EventListResponseTypeSessionDeleted EventListResponseType = "session.deleted" EventListResponseTypeSessionIdle EventListResponseType = "session.idle" @@ -1173,7 +1179,7 @@ const ( func (r EventListResponseType) IsKnown() bool { switch r { - case EventListResponseTypeLspClientDiagnostics, EventListResponseTypePermissionUpdated, EventListResponseTypeFileEdited, EventListResponseTypeStorageWrite, EventListResponseTypeInstallationUpdated, EventListResponseTypeMessageUpdated, EventListResponseTypeMessageRemoved, EventListResponseTypeMessagePartUpdated, EventListResponseTypeSessionUpdated, EventListResponseTypeSessionDeleted, EventListResponseTypeSessionIdle, EventListResponseTypeSessionError, EventListResponseTypeFileWatcherUpdated: + case EventListResponseTypeLspClientDiagnostics, EventListResponseTypePermissionUpdated, EventListResponseTypeFileEdited, EventListResponseTypeInstallationUpdated, EventListResponseTypeMessageUpdated, EventListResponseTypeMessageRemoved, EventListResponseTypeMessagePartUpdated, EventListResponseTypeStorageWrite, EventListResponseTypeSessionUpdated, EventListResponseTypeSessionDeleted, EventListResponseTypeSessionIdle, EventListResponseTypeSessionError, EventListResponseTypeFileWatcherUpdated: return true } return false diff --git a/packages/tui/sdk/file.go b/packages/tui/sdk/file.go index a9d6f018..0a8a4b2b 100644 --- a/packages/tui/sdk/file.go +++ b/packages/tui/sdk/file.go @@ -42,13 +42,55 @@ func (r *FileService) Read(ctx context.Context, query FileReadParams, opts ...op } // Get file status -func (r *FileService) Status(ctx context.Context, opts ...option.RequestOption) (res *[]FileStatusResponse, err error) { +func (r *FileService) Status(ctx context.Context, opts ...option.RequestOption) (res *[]File, err error) { opts = append(r.Options[:], opts...) path := "file/status" err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...) return } +type File struct { + Added int64 `json:"added,required"` + Path string `json:"path,required"` + Removed int64 `json:"removed,required"` + Status FileStatus `json:"status,required"` + JSON fileJSON `json:"-"` +} + +// fileJSON contains the JSON metadata for the struct [File] +type fileJSON struct { + Added apijson.Field + Path apijson.Field + Removed apijson.Field + Status apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *File) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r fileJSON) RawJSON() string { + return r.raw +} + +type FileStatus string + +const ( + FileStatusAdded FileStatus = "added" + FileStatusDeleted FileStatus = "deleted" + FileStatusModified FileStatus = "modified" +) + +func (r FileStatus) IsKnown() bool { + switch r { + case FileStatusAdded, FileStatusDeleted, FileStatusModified: + return true + } + return false +} + type FileReadResponse struct { Content string `json:"content,required"` Type FileReadResponseType `json:"type,required"` @@ -87,49 +129,6 @@ func (r FileReadResponseType) IsKnown() bool { return false } -type FileStatusResponse struct { - Added int64 `json:"added,required"` - File string `json:"file,required"` - Removed int64 `json:"removed,required"` - Status FileStatusResponseStatus `json:"status,required"` - JSON fileStatusResponseJSON `json:"-"` -} - -// fileStatusResponseJSON contains the JSON metadata for the struct -// [FileStatusResponse] -type fileStatusResponseJSON struct { - Added apijson.Field - File apijson.Field - Removed apijson.Field - Status apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *FileStatusResponse) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r fileStatusResponseJSON) RawJSON() string { - return r.raw -} - -type FileStatusResponseStatus string - -const ( - FileStatusResponseStatusAdded FileStatusResponseStatus = "added" - FileStatusResponseStatusDeleted FileStatusResponseStatus = "deleted" - FileStatusResponseStatusModified FileStatusResponseStatus = "modified" -) - -func (r FileStatusResponseStatus) IsKnown() bool { - switch r { - case FileStatusResponseStatusAdded, FileStatusResponseStatusDeleted, FileStatusResponseStatusModified: - return true - } - return false -} - type FileReadParams struct { Path param.Field[string] `query:"path,required"` } diff --git a/packages/tui/sdk/find.go b/packages/tui/sdk/find.go index bbd6b680..10f92fc0 100644 --- a/packages/tui/sdk/find.go +++ b/packages/tui/sdk/find.go @@ -42,7 +42,7 @@ func (r *FindService) Files(ctx context.Context, query FindFilesParams, opts ... } // Find workspace symbols -func (r *FindService) Symbols(ctx context.Context, query FindSymbolsParams, opts ...option.RequestOption) (res *[]FindSymbolsResponse, err error) { +func (r *FindService) Symbols(ctx context.Context, query FindSymbolsParams, opts ...option.RequestOption) (res *[]Symbol, err error) { opts = append(r.Options[:], opts...) path := "find/symbol" err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...) @@ -50,27 +50,24 @@ func (r *FindService) Symbols(ctx context.Context, query FindSymbolsParams, opts } // Find text in files -func (r *FindService) Text(ctx context.Context, query FindTextParams, opts ...option.RequestOption) (res *[]FindTextResponse, err error) { +func (r *FindService) Text(ctx context.Context, query FindTextParams, opts ...option.RequestOption) (res *[]Match, err error) { opts = append(r.Options[:], opts...) path := "find" err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...) return } -type FindSymbolsResponse = interface{} - -type FindTextResponse struct { - AbsoluteOffset float64 `json:"absolute_offset,required"` - LineNumber float64 `json:"line_number,required"` - Lines FindTextResponseLines `json:"lines,required"` - Path FindTextResponsePath `json:"path,required"` - Submatches []FindTextResponseSubmatch `json:"submatches,required"` - JSON findTextResponseJSON `json:"-"` +type Match struct { + AbsoluteOffset float64 `json:"absolute_offset,required"` + LineNumber float64 `json:"line_number,required"` + Lines MatchLines `json:"lines,required"` + Path MatchPath `json:"path,required"` + Submatches []MatchSubmatch `json:"submatches,required"` + JSON matchJSON `json:"-"` } -// findTextResponseJSON contains the JSON metadata for the struct -// [FindTextResponse] -type findTextResponseJSON struct { +// matchJSON contains the JSON metadata for the struct [Match] +type matchJSON struct { AbsoluteOffset apijson.Field LineNumber apijson.Field Lines apijson.Field @@ -80,66 +77,63 @@ type findTextResponseJSON struct { ExtraFields map[string]apijson.Field } -func (r *FindTextResponse) UnmarshalJSON(data []byte) (err error) { +func (r *Match) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } -func (r findTextResponseJSON) RawJSON() string { +func (r matchJSON) RawJSON() string { return r.raw } -type FindTextResponseLines struct { - Text string `json:"text,required"` - JSON findTextResponseLinesJSON `json:"-"` +type MatchLines struct { + Text string `json:"text,required"` + JSON matchLinesJSON `json:"-"` } -// findTextResponseLinesJSON contains the JSON metadata for the struct -// [FindTextResponseLines] -type findTextResponseLinesJSON struct { +// matchLinesJSON contains the JSON metadata for the struct [MatchLines] +type matchLinesJSON struct { Text apijson.Field raw string ExtraFields map[string]apijson.Field } -func (r *FindTextResponseLines) UnmarshalJSON(data []byte) (err error) { +func (r *MatchLines) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } -func (r findTextResponseLinesJSON) RawJSON() string { +func (r matchLinesJSON) RawJSON() string { return r.raw } -type FindTextResponsePath struct { - Text string `json:"text,required"` - JSON findTextResponsePathJSON `json:"-"` +type MatchPath struct { + Text string `json:"text,required"` + JSON matchPathJSON `json:"-"` } -// findTextResponsePathJSON contains the JSON metadata for the struct -// [FindTextResponsePath] -type findTextResponsePathJSON struct { +// matchPathJSON contains the JSON metadata for the struct [MatchPath] +type matchPathJSON struct { Text apijson.Field raw string ExtraFields map[string]apijson.Field } -func (r *FindTextResponsePath) UnmarshalJSON(data []byte) (err error) { +func (r *MatchPath) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } -func (r findTextResponsePathJSON) RawJSON() string { +func (r matchPathJSON) RawJSON() string { return r.raw } -type FindTextResponseSubmatch struct { - End float64 `json:"end,required"` - Match FindTextResponseSubmatchesMatch `json:"match,required"` - Start float64 `json:"start,required"` - JSON findTextResponseSubmatchJSON `json:"-"` +type MatchSubmatch struct { + End float64 `json:"end,required"` + Match MatchSubmatchesMatch `json:"match,required"` + Start float64 `json:"start,required"` + JSON matchSubmatchJSON `json:"-"` } -// findTextResponseSubmatchJSON contains the JSON metadata for the struct -// [FindTextResponseSubmatch] -type findTextResponseSubmatchJSON struct { +// matchSubmatchJSON contains the JSON metadata for the struct [MatchSubmatch] +type matchSubmatchJSON struct { End apijson.Field Match apijson.Field Start apijson.Field @@ -147,32 +141,147 @@ type findTextResponseSubmatchJSON struct { ExtraFields map[string]apijson.Field } -func (r *FindTextResponseSubmatch) UnmarshalJSON(data []byte) (err error) { +func (r *MatchSubmatch) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } -func (r findTextResponseSubmatchJSON) RawJSON() string { +func (r matchSubmatchJSON) RawJSON() string { return r.raw } -type FindTextResponseSubmatchesMatch struct { - Text string `json:"text,required"` - JSON findTextResponseSubmatchesMatchJSON `json:"-"` +type MatchSubmatchesMatch struct { + Text string `json:"text,required"` + JSON matchSubmatchesMatchJSON `json:"-"` } -// findTextResponseSubmatchesMatchJSON contains the JSON metadata for the struct -// [FindTextResponseSubmatchesMatch] -type findTextResponseSubmatchesMatchJSON struct { +// matchSubmatchesMatchJSON contains the JSON metadata for the struct +// [MatchSubmatchesMatch] +type matchSubmatchesMatchJSON struct { Text apijson.Field raw string ExtraFields map[string]apijson.Field } -func (r *FindTextResponseSubmatchesMatch) UnmarshalJSON(data []byte) (err error) { +func (r *MatchSubmatchesMatch) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } -func (r findTextResponseSubmatchesMatchJSON) RawJSON() string { +func (r matchSubmatchesMatchJSON) RawJSON() string { + return r.raw +} + +type Symbol struct { + Kind float64 `json:"kind,required"` + Location SymbolLocation `json:"location,required"` + Name string `json:"name,required"` + JSON symbolJSON `json:"-"` +} + +// symbolJSON contains the JSON metadata for the struct [Symbol] +type symbolJSON struct { + Kind apijson.Field + Location apijson.Field + Name apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *Symbol) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r symbolJSON) RawJSON() string { + return r.raw +} + +type SymbolLocation struct { + Range SymbolLocationRange `json:"range,required"` + Uri string `json:"uri,required"` + JSON symbolLocationJSON `json:"-"` +} + +// symbolLocationJSON contains the JSON metadata for the struct [SymbolLocation] +type symbolLocationJSON struct { + Range apijson.Field + Uri apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *SymbolLocation) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r symbolLocationJSON) RawJSON() string { + return r.raw +} + +type SymbolLocationRange struct { + End SymbolLocationRangeEnd `json:"end,required"` + Start SymbolLocationRangeStart `json:"start,required"` + JSON symbolLocationRangeJSON `json:"-"` +} + +// symbolLocationRangeJSON contains the JSON metadata for the struct +// [SymbolLocationRange] +type symbolLocationRangeJSON struct { + End apijson.Field + Start apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *SymbolLocationRange) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r symbolLocationRangeJSON) RawJSON() string { + return r.raw +} + +type SymbolLocationRangeEnd struct { + Character float64 `json:"character,required"` + Line float64 `json:"line,required"` + JSON symbolLocationRangeEndJSON `json:"-"` +} + +// symbolLocationRangeEndJSON contains the JSON metadata for the struct +// [SymbolLocationRangeEnd] +type symbolLocationRangeEndJSON struct { + Character apijson.Field + Line apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *SymbolLocationRangeEnd) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r symbolLocationRangeEndJSON) RawJSON() string { + return r.raw +} + +type SymbolLocationRangeStart struct { + Character float64 `json:"character,required"` + Line float64 `json:"line,required"` + JSON symbolLocationRangeStartJSON `json:"-"` +} + +// symbolLocationRangeStartJSON contains the JSON metadata for the struct +// [SymbolLocationRangeStart] +type symbolLocationRangeStartJSON struct { + Character apijson.Field + Line apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *SymbolLocationRangeStart) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r symbolLocationRangeStartJSON) RawJSON() string { return r.raw } diff --git a/packages/tui/sdk/packages/ssestream/ssestream.go b/packages/tui/sdk/packages/ssestream/ssestream.go index 81adbd69..cc0afb7b 100644 --- a/packages/tui/sdk/packages/ssestream/ssestream.go +++ b/packages/tui/sdk/packages/ssestream/ssestream.go @@ -29,7 +29,7 @@ func NewDecoder(res *http.Response) Decoder { decoder = t(res.Body) } else { scn := bufio.NewScanner(res.Body) - scn.Buffer(nil, bufio.MaxScanTokenSize<<4) + scn.Buffer(nil, bufio.MaxScanTokenSize<<9) decoder = &eventStreamDecoder{rc: res.Body, scn: scn} } return decoder diff --git a/packages/tui/sdk/scripts/lint b/packages/tui/sdk/scripts/lint index fa7ba1f6..7e03a7be 100755 --- a/packages/tui/sdk/scripts/lint +++ b/packages/tui/sdk/scripts/lint @@ -6,3 +6,6 @@ cd "$(dirname "$0")/.." echo "==> Running Go build" go build ./... + +echo "==> Checking tests compile" +go test -run=^$ ./... diff --git a/packages/tui/sdk/session.go b/packages/tui/sdk/session.go index e8216a1f..b62ade1f 100644 --- a/packages/tui/sdk/session.go +++ b/packages/tui/sdk/session.go @@ -77,7 +77,7 @@ func (r *SessionService) Abort(ctx context.Context, id string, opts ...option.Re } // Create and send a new message to a session -func (r *SessionService) Chat(ctx context.Context, id string, body SessionChatParams, opts ...option.RequestOption) (res *Message, err error) { +func (r *SessionService) Chat(ctx context.Context, id string, body SessionChatParams, opts ...option.RequestOption) (res *AssistantMessage, err error) { opts = append(r.Options[:], opts...) if id == "" { err = errors.New("missing required id parameter") @@ -101,7 +101,7 @@ func (r *SessionService) Init(ctx context.Context, id string, body SessionInitPa } // List messages for a session -func (r *SessionService) Messages(ctx context.Context, id string, opts ...option.RequestOption) (res *[]Message, err error) { +func (r *SessionService) Messages(ctx context.Context, id string, opts ...option.RequestOption) (res *[]SessionMessagesResponse, err error) { opts = append(r.Options[:], opts...) if id == "" { err = errors.New("missing required id parameter") @@ -148,8 +148,296 @@ func (r *SessionService) Unshare(ctx context.Context, id string, opts ...option. return } +type AssistantMessage struct { + ID string `json:"id,required"` + Cost float64 `json:"cost,required"` + ModelID string `json:"modelID,required"` + Path AssistantMessagePath `json:"path,required"` + ProviderID string `json:"providerID,required"` + Role AssistantMessageRole `json:"role,required"` + SessionID string `json:"sessionID,required"` + System []string `json:"system,required"` + Time AssistantMessageTime `json:"time,required"` + Tokens AssistantMessageTokens `json:"tokens,required"` + Error AssistantMessageError `json:"error"` + Summary bool `json:"summary"` + JSON assistantMessageJSON `json:"-"` +} + +// assistantMessageJSON contains the JSON metadata for the struct +// [AssistantMessage] +type assistantMessageJSON struct { + ID apijson.Field + Cost apijson.Field + ModelID apijson.Field + Path apijson.Field + ProviderID apijson.Field + Role apijson.Field + SessionID apijson.Field + System apijson.Field + Time apijson.Field + Tokens apijson.Field + Error apijson.Field + Summary apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *AssistantMessage) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r assistantMessageJSON) RawJSON() string { + return r.raw +} + +func (r AssistantMessage) implementsMessage() {} + +type AssistantMessagePath struct { + Cwd string `json:"cwd,required"` + Root string `json:"root,required"` + JSON assistantMessagePathJSON `json:"-"` +} + +// assistantMessagePathJSON contains the JSON metadata for the struct +// [AssistantMessagePath] +type assistantMessagePathJSON struct { + Cwd apijson.Field + Root apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *AssistantMessagePath) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r assistantMessagePathJSON) RawJSON() string { + return r.raw +} + +type AssistantMessageRole string + +const ( + AssistantMessageRoleAssistant AssistantMessageRole = "assistant" +) + +func (r AssistantMessageRole) IsKnown() bool { + switch r { + case AssistantMessageRoleAssistant: + return true + } + return false +} + +type AssistantMessageTime struct { + Created float64 `json:"created,required"` + Completed float64 `json:"completed"` + JSON assistantMessageTimeJSON `json:"-"` +} + +// assistantMessageTimeJSON contains the JSON metadata for the struct +// [AssistantMessageTime] +type assistantMessageTimeJSON struct { + Created apijson.Field + Completed apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *AssistantMessageTime) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r assistantMessageTimeJSON) RawJSON() string { + return r.raw +} + +type AssistantMessageTokens struct { + Cache AssistantMessageTokensCache `json:"cache,required"` + Input float64 `json:"input,required"` + Output float64 `json:"output,required"` + Reasoning float64 `json:"reasoning,required"` + JSON assistantMessageTokensJSON `json:"-"` +} + +// assistantMessageTokensJSON contains the JSON metadata for the struct +// [AssistantMessageTokens] +type assistantMessageTokensJSON struct { + Cache apijson.Field + Input apijson.Field + Output apijson.Field + Reasoning apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *AssistantMessageTokens) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r assistantMessageTokensJSON) RawJSON() string { + return r.raw +} + +type AssistantMessageTokensCache struct { + Read float64 `json:"read,required"` + Write float64 `json:"write,required"` + JSON assistantMessageTokensCacheJSON `json:"-"` +} + +// assistantMessageTokensCacheJSON contains the JSON metadata for the struct +// [AssistantMessageTokensCache] +type assistantMessageTokensCacheJSON struct { + Read apijson.Field + Write apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *AssistantMessageTokensCache) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r assistantMessageTokensCacheJSON) RawJSON() string { + return r.raw +} + +type AssistantMessageError struct { + // This field can have the runtime type of [shared.ProviderAuthErrorData], + // [shared.UnknownErrorData], [interface{}]. + Data interface{} `json:"data,required"` + Name AssistantMessageErrorName `json:"name,required"` + JSON assistantMessageErrorJSON `json:"-"` + union AssistantMessageErrorUnion +} + +// assistantMessageErrorJSON contains the JSON metadata for the struct +// [AssistantMessageError] +type assistantMessageErrorJSON struct { + Data apijson.Field + Name apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r assistantMessageErrorJSON) RawJSON() string { + return r.raw +} + +func (r *AssistantMessageError) UnmarshalJSON(data []byte) (err error) { + *r = AssistantMessageError{} + err = apijson.UnmarshalRoot(data, &r.union) + if err != nil { + return err + } + return apijson.Port(r.union, &r) +} + +// AsUnion returns a [AssistantMessageErrorUnion] interface which you can cast to +// the specific types for more type safety. +// +// Possible runtime types of the union are [shared.ProviderAuthError], +// [shared.UnknownError], [AssistantMessageErrorMessageOutputLengthError], +// [shared.MessageAbortedError]. +func (r AssistantMessageError) AsUnion() AssistantMessageErrorUnion { + return r.union +} + +// Union satisfied by [shared.ProviderAuthError], [shared.UnknownError], +// [AssistantMessageErrorMessageOutputLengthError] or [shared.MessageAbortedError]. +type AssistantMessageErrorUnion interface { + ImplementsAssistantMessageError() +} + +func init() { + apijson.RegisterUnion( + reflect.TypeOf((*AssistantMessageErrorUnion)(nil)).Elem(), + "name", + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(shared.ProviderAuthError{}), + DiscriminatorValue: "ProviderAuthError", + }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(shared.UnknownError{}), + DiscriminatorValue: "UnknownError", + }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(AssistantMessageErrorMessageOutputLengthError{}), + DiscriminatorValue: "MessageOutputLengthError", + }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(shared.MessageAbortedError{}), + DiscriminatorValue: "MessageAbortedError", + }, + ) +} + +type AssistantMessageErrorMessageOutputLengthError struct { + Data interface{} `json:"data,required"` + Name AssistantMessageErrorMessageOutputLengthErrorName `json:"name,required"` + JSON assistantMessageErrorMessageOutputLengthErrorJSON `json:"-"` +} + +// assistantMessageErrorMessageOutputLengthErrorJSON contains the JSON metadata for +// the struct [AssistantMessageErrorMessageOutputLengthError] +type assistantMessageErrorMessageOutputLengthErrorJSON struct { + Data apijson.Field + Name apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *AssistantMessageErrorMessageOutputLengthError) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r assistantMessageErrorMessageOutputLengthErrorJSON) RawJSON() string { + return r.raw +} + +func (r AssistantMessageErrorMessageOutputLengthError) ImplementsAssistantMessageError() {} + +type AssistantMessageErrorMessageOutputLengthErrorName string + +const ( + AssistantMessageErrorMessageOutputLengthErrorNameMessageOutputLengthError AssistantMessageErrorMessageOutputLengthErrorName = "MessageOutputLengthError" +) + +func (r AssistantMessageErrorMessageOutputLengthErrorName) IsKnown() bool { + switch r { + case AssistantMessageErrorMessageOutputLengthErrorNameMessageOutputLengthError: + return true + } + return false +} + +type AssistantMessageErrorName string + +const ( + AssistantMessageErrorNameProviderAuthError AssistantMessageErrorName = "ProviderAuthError" + AssistantMessageErrorNameUnknownError AssistantMessageErrorName = "UnknownError" + AssistantMessageErrorNameMessageOutputLengthError AssistantMessageErrorName = "MessageOutputLengthError" + AssistantMessageErrorNameMessageAbortedError AssistantMessageErrorName = "MessageAbortedError" +) + +func (r AssistantMessageErrorName) IsKnown() bool { + switch r { + case AssistantMessageErrorNameProviderAuthError, AssistantMessageErrorNameUnknownError, AssistantMessageErrorNameMessageOutputLengthError, AssistantMessageErrorNameMessageAbortedError: + return true + } + return false +} + type FilePart struct { - MediaType string `json:"mediaType,required"` + ID string `json:"id,required"` + MessageID string `json:"messageID,required"` + Mime string `json:"mime,required"` + SessionID string `json:"sessionID,required"` Type FilePartType `json:"type,required"` URL string `json:"url,required"` Filename string `json:"filename"` @@ -158,7 +446,10 @@ type FilePart struct { // filePartJSON contains the JSON metadata for the struct [FilePart] type filePartJSON struct { - MediaType apijson.Field + ID apijson.Field + MessageID apijson.Field + Mime apijson.Field + SessionID apijson.Field Type apijson.Field URL apijson.Field Filename apijson.Field @@ -174,7 +465,7 @@ func (r filePartJSON) RawJSON() string { return r.raw } -func (r FilePart) implementsMessagePart() {} +func (r FilePart) implementsPart() {} type FilePartType string @@ -191,7 +482,10 @@ func (r FilePartType) IsKnown() bool { } type FilePartParam struct { - MediaType param.Field[string] `json:"mediaType,required"` + ID param.Field[string] `json:"id,required"` + MessageID param.Field[string] `json:"messageID,required"` + Mime param.Field[string] `json:"mime,required"` + SessionID param.Field[string] `json:"sessionID,required"` Type param.Field[FilePartType] `json:"type,required"` URL param.Field[string] `json:"url,required"` Filename param.Field[string] `json:"filename"` @@ -201,266 +495,55 @@ func (r FilePartParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } -func (r FilePartParam) implementsMessagePartUnionParam() {} +func (r FilePartParam) implementsSessionChatParamsPartUnion() {} type Message struct { - ID string `json:"id,required"` - Metadata MessageMetadata `json:"metadata,required"` - Parts []MessagePart `json:"parts,required"` - Role MessageRole `json:"role,required"` - JSON messageJSON `json:"-"` + ID string `json:"id,required"` + Role MessageRole `json:"role,required"` + SessionID string `json:"sessionID,required"` + // This field can have the runtime type of [UserMessageTime], + // [AssistantMessageTime]. + Time interface{} `json:"time,required"` + Cost float64 `json:"cost"` + // This field can have the runtime type of [AssistantMessageError]. + Error interface{} `json:"error"` + ModelID string `json:"modelID"` + // This field can have the runtime type of [AssistantMessagePath]. + Path interface{} `json:"path"` + ProviderID string `json:"providerID"` + Summary bool `json:"summary"` + // This field can have the runtime type of [[]string]. + System interface{} `json:"system"` + // This field can have the runtime type of [AssistantMessageTokens]. + Tokens interface{} `json:"tokens"` + JSON messageJSON `json:"-"` + union MessageUnion } // messageJSON contains the JSON metadata for the struct [Message] type messageJSON struct { ID apijson.Field - Metadata apijson.Field - Parts apijson.Field Role apijson.Field + SessionID apijson.Field + Time apijson.Field + Cost apijson.Field + Error apijson.Field + ModelID apijson.Field + Path apijson.Field + ProviderID apijson.Field + Summary apijson.Field + System apijson.Field + Tokens apijson.Field raw string ExtraFields map[string]apijson.Field } -func (r *Message) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - func (r messageJSON) RawJSON() string { return r.raw } -type MessageMetadata struct { - SessionID string `json:"sessionID,required"` - Time MessageMetadataTime `json:"time,required"` - Tool map[string]MessageMetadataTool `json:"tool,required"` - Assistant MessageMetadataAssistant `json:"assistant"` - Error MessageMetadataError `json:"error"` - Snapshot string `json:"snapshot"` - JSON messageMetadataJSON `json:"-"` -} - -// messageMetadataJSON contains the JSON metadata for the struct [MessageMetadata] -type messageMetadataJSON struct { - SessionID apijson.Field - Time apijson.Field - Tool apijson.Field - Assistant apijson.Field - Error apijson.Field - Snapshot apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *MessageMetadata) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r messageMetadataJSON) RawJSON() string { - return r.raw -} - -type MessageMetadataTime struct { - Created float64 `json:"created,required"` - Completed float64 `json:"completed"` - JSON messageMetadataTimeJSON `json:"-"` -} - -// messageMetadataTimeJSON contains the JSON metadata for the struct -// [MessageMetadataTime] -type messageMetadataTimeJSON struct { - Created apijson.Field - Completed apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *MessageMetadataTime) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r messageMetadataTimeJSON) RawJSON() string { - return r.raw -} - -type MessageMetadataTool struct { - Time MessageMetadataToolTime `json:"time,required"` - Title string `json:"title,required"` - Snapshot string `json:"snapshot"` - ExtraFields map[string]interface{} `json:"-,extras"` - JSON messageMetadataToolJSON `json:"-"` -} - -// messageMetadataToolJSON contains the JSON metadata for the struct -// [MessageMetadataTool] -type messageMetadataToolJSON struct { - Time apijson.Field - Title apijson.Field - Snapshot apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *MessageMetadataTool) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r messageMetadataToolJSON) RawJSON() string { - return r.raw -} - -type MessageMetadataToolTime struct { - End float64 `json:"end,required"` - Start float64 `json:"start,required"` - JSON messageMetadataToolTimeJSON `json:"-"` -} - -// messageMetadataToolTimeJSON contains the JSON metadata for the struct -// [MessageMetadataToolTime] -type messageMetadataToolTimeJSON struct { - End apijson.Field - Start apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *MessageMetadataToolTime) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r messageMetadataToolTimeJSON) RawJSON() string { - return r.raw -} - -type MessageMetadataAssistant struct { - Cost float64 `json:"cost,required"` - ModelID string `json:"modelID,required"` - Path MessageMetadataAssistantPath `json:"path,required"` - ProviderID string `json:"providerID,required"` - System []string `json:"system,required"` - Tokens MessageMetadataAssistantTokens `json:"tokens,required"` - Summary bool `json:"summary"` - JSON messageMetadataAssistantJSON `json:"-"` -} - -// messageMetadataAssistantJSON contains the JSON metadata for the struct -// [MessageMetadataAssistant] -type messageMetadataAssistantJSON struct { - Cost apijson.Field - ModelID apijson.Field - Path apijson.Field - ProviderID apijson.Field - System apijson.Field - Tokens apijson.Field - Summary apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *MessageMetadataAssistant) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r messageMetadataAssistantJSON) RawJSON() string { - return r.raw -} - -type MessageMetadataAssistantPath struct { - Cwd string `json:"cwd,required"` - Root string `json:"root,required"` - JSON messageMetadataAssistantPathJSON `json:"-"` -} - -// messageMetadataAssistantPathJSON contains the JSON metadata for the struct -// [MessageMetadataAssistantPath] -type messageMetadataAssistantPathJSON struct { - Cwd apijson.Field - Root apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *MessageMetadataAssistantPath) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r messageMetadataAssistantPathJSON) RawJSON() string { - return r.raw -} - -type MessageMetadataAssistantTokens struct { - Cache MessageMetadataAssistantTokensCache `json:"cache,required"` - Input float64 `json:"input,required"` - Output float64 `json:"output,required"` - Reasoning float64 `json:"reasoning,required"` - JSON messageMetadataAssistantTokensJSON `json:"-"` -} - -// messageMetadataAssistantTokensJSON contains the JSON metadata for the struct -// [MessageMetadataAssistantTokens] -type messageMetadataAssistantTokensJSON struct { - Cache apijson.Field - Input apijson.Field - Output apijson.Field - Reasoning apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *MessageMetadataAssistantTokens) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r messageMetadataAssistantTokensJSON) RawJSON() string { - return r.raw -} - -type MessageMetadataAssistantTokensCache struct { - Read float64 `json:"read,required"` - Write float64 `json:"write,required"` - JSON messageMetadataAssistantTokensCacheJSON `json:"-"` -} - -// messageMetadataAssistantTokensCacheJSON contains the JSON metadata for the -// struct [MessageMetadataAssistantTokensCache] -type messageMetadataAssistantTokensCacheJSON struct { - Read apijson.Field - Write apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *MessageMetadataAssistantTokensCache) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r messageMetadataAssistantTokensCacheJSON) RawJSON() string { - return r.raw -} - -type MessageMetadataError struct { - // This field can have the runtime type of [shared.ProviderAuthErrorData], - // [shared.UnknownErrorData], [interface{}]. - Data interface{} `json:"data,required"` - Name MessageMetadataErrorName `json:"name,required"` - JSON messageMetadataErrorJSON `json:"-"` - union MessageMetadataErrorUnion -} - -// messageMetadataErrorJSON contains the JSON metadata for the struct -// [MessageMetadataError] -type messageMetadataErrorJSON struct { - Data apijson.Field - Name apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r messageMetadataErrorJSON) RawJSON() string { - return r.raw -} - -func (r *MessageMetadataError) UnmarshalJSON(data []byte) (err error) { - *r = MessageMetadataError{} +func (r *Message) UnmarshalJSON(data []byte) (err error) { + *r = Message{} err = apijson.UnmarshalRoot(data, &r.union) if err != nil { return err @@ -468,98 +551,36 @@ func (r *MessageMetadataError) UnmarshalJSON(data []byte) (err error) { return apijson.Port(r.union, &r) } -// AsUnion returns a [MessageMetadataErrorUnion] interface which you can cast to -// the specific types for more type safety. +// AsUnion returns a [MessageUnion] interface which you can cast to the specific +// types for more type safety. // -// Possible runtime types of the union are [shared.ProviderAuthError], -// [shared.UnknownError], [MessageMetadataErrorMessageOutputLengthError]. -func (r MessageMetadataError) AsUnion() MessageMetadataErrorUnion { +// Possible runtime types of the union are [UserMessage], [AssistantMessage]. +func (r Message) AsUnion() MessageUnion { return r.union } -// Union satisfied by [shared.ProviderAuthError], [shared.UnknownError] or -// [MessageMetadataErrorMessageOutputLengthError]. -type MessageMetadataErrorUnion interface { - ImplementsMessageMetadataError() +// Union satisfied by [UserMessage] or [AssistantMessage]. +type MessageUnion interface { + implementsMessage() } func init() { apijson.RegisterUnion( - reflect.TypeOf((*MessageMetadataErrorUnion)(nil)).Elem(), - "name", + reflect.TypeOf((*MessageUnion)(nil)).Elem(), + "role", apijson.UnionVariant{ TypeFilter: gjson.JSON, - Type: reflect.TypeOf(shared.ProviderAuthError{}), - DiscriminatorValue: "ProviderAuthError", + Type: reflect.TypeOf(UserMessage{}), + DiscriminatorValue: "user", }, apijson.UnionVariant{ TypeFilter: gjson.JSON, - Type: reflect.TypeOf(shared.UnknownError{}), - DiscriminatorValue: "UnknownError", - }, - apijson.UnionVariant{ - TypeFilter: gjson.JSON, - Type: reflect.TypeOf(MessageMetadataErrorMessageOutputLengthError{}), - DiscriminatorValue: "MessageOutputLengthError", + Type: reflect.TypeOf(AssistantMessage{}), + DiscriminatorValue: "assistant", }, ) } -type MessageMetadataErrorMessageOutputLengthError struct { - Data interface{} `json:"data,required"` - Name MessageMetadataErrorMessageOutputLengthErrorName `json:"name,required"` - JSON messageMetadataErrorMessageOutputLengthErrorJSON `json:"-"` -} - -// messageMetadataErrorMessageOutputLengthErrorJSON contains the JSON metadata for -// the struct [MessageMetadataErrorMessageOutputLengthError] -type messageMetadataErrorMessageOutputLengthErrorJSON struct { - Data apijson.Field - Name apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *MessageMetadataErrorMessageOutputLengthError) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r messageMetadataErrorMessageOutputLengthErrorJSON) RawJSON() string { - return r.raw -} - -func (r MessageMetadataErrorMessageOutputLengthError) ImplementsMessageMetadataError() {} - -type MessageMetadataErrorMessageOutputLengthErrorName string - -const ( - MessageMetadataErrorMessageOutputLengthErrorNameMessageOutputLengthError MessageMetadataErrorMessageOutputLengthErrorName = "MessageOutputLengthError" -) - -func (r MessageMetadataErrorMessageOutputLengthErrorName) IsKnown() bool { - switch r { - case MessageMetadataErrorMessageOutputLengthErrorNameMessageOutputLengthError: - return true - } - return false -} - -type MessageMetadataErrorName string - -const ( - MessageMetadataErrorNameProviderAuthError MessageMetadataErrorName = "ProviderAuthError" - MessageMetadataErrorNameUnknownError MessageMetadataErrorName = "UnknownError" - MessageMetadataErrorNameMessageOutputLengthError MessageMetadataErrorName = "MessageOutputLengthError" -) - -func (r MessageMetadataErrorName) IsKnown() bool { - switch r { - case MessageMetadataErrorNameProviderAuthError, MessageMetadataErrorNameUnknownError, MessageMetadataErrorNameMessageOutputLengthError: - return true - } - return false -} - type MessageRole string const ( @@ -575,43 +596,58 @@ func (r MessageRole) IsKnown() bool { return false } -type MessagePart struct { - Type MessagePartType `json:"type,required"` - Filename string `json:"filename"` - MediaType string `json:"mediaType"` - // This field can have the runtime type of [map[string]interface{}]. - ProviderMetadata interface{} `json:"providerMetadata"` - SourceID string `json:"sourceId"` - Text string `json:"text"` - Title string `json:"title"` - // This field can have the runtime type of [ToolInvocationPartToolInvocation]. - ToolInvocation interface{} `json:"toolInvocation"` - URL string `json:"url"` - JSON messagePartJSON `json:"-"` - union MessagePartUnion +type Part struct { + ID string `json:"id,required"` + MessageID string `json:"messageID,required"` + SessionID string `json:"sessionID,required"` + Type PartType `json:"type,required"` + CallID string `json:"callID"` + Cost float64 `json:"cost"` + Filename string `json:"filename"` + Mime string `json:"mime"` + Snapshot string `json:"snapshot"` + // This field can have the runtime type of [ToolPartState]. + State interface{} `json:"state"` + Synthetic bool `json:"synthetic"` + Text string `json:"text"` + // This field can have the runtime type of [TextPartTime]. + Time interface{} `json:"time"` + // This field can have the runtime type of [StepFinishPartTokens]. + Tokens interface{} `json:"tokens"` + Tool string `json:"tool"` + URL string `json:"url"` + JSON partJSON `json:"-"` + union PartUnion } -// messagePartJSON contains the JSON metadata for the struct [MessagePart] -type messagePartJSON struct { - Type apijson.Field - Filename apijson.Field - MediaType apijson.Field - ProviderMetadata apijson.Field - SourceID apijson.Field - Text apijson.Field - Title apijson.Field - ToolInvocation apijson.Field - URL apijson.Field - raw string - ExtraFields map[string]apijson.Field +// partJSON contains the JSON metadata for the struct [Part] +type partJSON struct { + ID apijson.Field + MessageID apijson.Field + SessionID apijson.Field + Type apijson.Field + CallID apijson.Field + Cost apijson.Field + Filename apijson.Field + Mime apijson.Field + Snapshot apijson.Field + State apijson.Field + Synthetic apijson.Field + Text apijson.Field + Time apijson.Field + Tokens apijson.Field + Tool apijson.Field + URL apijson.Field + raw string + ExtraFields map[string]apijson.Field } -func (r messagePartJSON) RawJSON() string { +func (r partJSON) RawJSON() string { return r.raw } -func (r *MessagePart) UnmarshalJSON(data []byte) (err error) { - *r = MessagePart{} +func (r *Part) UnmarshalJSON(data []byte) (err error) { + *r = Part{} err = apijson.UnmarshalRoot(data, &r.union) if err != nil { return err @@ -619,153 +655,77 @@ func (r *MessagePart) UnmarshalJSON(data []byte) (err error) { return apijson.Port(r.union, &r) } -// AsUnion returns a [MessagePartUnion] interface which you can cast to the -// specific types for more type safety. +// AsUnion returns a [PartUnion] interface which you can cast to the specific types +// for more type safety. // -// Possible runtime types of the union are [TextPart], [ReasoningPart], -// [ToolInvocationPart], [SourceURLPart], [FilePart], [StepStartPart]. -func (r MessagePart) AsUnion() MessagePartUnion { +// Possible runtime types of the union are [TextPart], [FilePart], [ToolPart], +// [StepStartPart], [StepFinishPart], [SnapshotPart]. +func (r Part) AsUnion() PartUnion { return r.union } -// Union satisfied by [TextPart], [ReasoningPart], [ToolInvocationPart], -// [SourceURLPart], [FilePart] or [StepStartPart]. -type MessagePartUnion interface { - implementsMessagePart() +// Union satisfied by [TextPart], [FilePart], [ToolPart], [StepStartPart], +// [StepFinishPart] or [SnapshotPart]. +type PartUnion interface { + implementsPart() } func init() { apijson.RegisterUnion( - reflect.TypeOf((*MessagePartUnion)(nil)).Elem(), + reflect.TypeOf((*PartUnion)(nil)).Elem(), "type", apijson.UnionVariant{ TypeFilter: gjson.JSON, Type: reflect.TypeOf(TextPart{}), DiscriminatorValue: "text", }, - apijson.UnionVariant{ - TypeFilter: gjson.JSON, - Type: reflect.TypeOf(ReasoningPart{}), - DiscriminatorValue: "reasoning", - }, - apijson.UnionVariant{ - TypeFilter: gjson.JSON, - Type: reflect.TypeOf(ToolInvocationPart{}), - DiscriminatorValue: "tool-invocation", - }, - apijson.UnionVariant{ - TypeFilter: gjson.JSON, - Type: reflect.TypeOf(SourceURLPart{}), - DiscriminatorValue: "source-url", - }, apijson.UnionVariant{ TypeFilter: gjson.JSON, Type: reflect.TypeOf(FilePart{}), DiscriminatorValue: "file", }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(ToolPart{}), + DiscriminatorValue: "tool", + }, apijson.UnionVariant{ TypeFilter: gjson.JSON, Type: reflect.TypeOf(StepStartPart{}), DiscriminatorValue: "step-start", }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(StepFinishPart{}), + DiscriminatorValue: "step-finish", + }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(SnapshotPart{}), + DiscriminatorValue: "snapshot", + }, ) } -type MessagePartType string +type PartType string const ( - MessagePartTypeText MessagePartType = "text" - MessagePartTypeReasoning MessagePartType = "reasoning" - MessagePartTypeToolInvocation MessagePartType = "tool-invocation" - MessagePartTypeSourceURL MessagePartType = "source-url" - MessagePartTypeFile MessagePartType = "file" - MessagePartTypeStepStart MessagePartType = "step-start" + PartTypeText PartType = "text" + PartTypeFile PartType = "file" + PartTypeTool PartType = "tool" + PartTypeStepStart PartType = "step-start" + PartTypeStepFinish PartType = "step-finish" + PartTypeSnapshot PartType = "snapshot" ) -func (r MessagePartType) IsKnown() bool { +func (r PartType) IsKnown() bool { switch r { - case MessagePartTypeText, MessagePartTypeReasoning, MessagePartTypeToolInvocation, MessagePartTypeSourceURL, MessagePartTypeFile, MessagePartTypeStepStart: + case PartTypeText, PartTypeFile, PartTypeTool, PartTypeStepStart, PartTypeStepFinish, PartTypeSnapshot: return true } return false } -type MessagePartParam struct { - Type param.Field[MessagePartType] `json:"type,required"` - Filename param.Field[string] `json:"filename"` - MediaType param.Field[string] `json:"mediaType"` - ProviderMetadata param.Field[interface{}] `json:"providerMetadata"` - SourceID param.Field[string] `json:"sourceId"` - Text param.Field[string] `json:"text"` - Title param.Field[string] `json:"title"` - ToolInvocation param.Field[interface{}] `json:"toolInvocation"` - URL param.Field[string] `json:"url"` -} - -func (r MessagePartParam) MarshalJSON() (data []byte, err error) { - return apijson.MarshalRoot(r) -} - -func (r MessagePartParam) implementsMessagePartUnionParam() {} - -// Satisfied by [TextPartParam], [ReasoningPartParam], [ToolInvocationPartParam], -// [SourceURLPartParam], [FilePartParam], [StepStartPartParam], [MessagePartParam]. -type MessagePartUnionParam interface { - implementsMessagePartUnionParam() -} - -type ReasoningPart struct { - Text string `json:"text,required"` - Type ReasoningPartType `json:"type,required"` - ProviderMetadata map[string]interface{} `json:"providerMetadata"` - JSON reasoningPartJSON `json:"-"` -} - -// reasoningPartJSON contains the JSON metadata for the struct [ReasoningPart] -type reasoningPartJSON struct { - Text apijson.Field - Type apijson.Field - ProviderMetadata apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *ReasoningPart) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r reasoningPartJSON) RawJSON() string { - return r.raw -} - -func (r ReasoningPart) implementsMessagePart() {} - -type ReasoningPartType string - -const ( - ReasoningPartTypeReasoning ReasoningPartType = "reasoning" -) - -func (r ReasoningPartType) IsKnown() bool { - switch r { - case ReasoningPartTypeReasoning: - return true - } - return false -} - -type ReasoningPartParam struct { - Text param.Field[string] `json:"text,required"` - Type param.Field[ReasoningPartType] `json:"type,required"` - ProviderMetadata param.Field[map[string]interface{}] `json:"providerMetadata"` -} - -func (r ReasoningPartParam) MarshalJSON() (data []byte, err error) { - return apijson.MarshalRoot(r) -} - -func (r ReasoningPartParam) implementsMessagePartUnionParam() {} - type Session struct { ID string `json:"id,required"` Time SessionTime `json:"time,required"` @@ -864,71 +824,159 @@ func (r sessionShareJSON) RawJSON() string { return r.raw } -type SourceURLPart struct { - SourceID string `json:"sourceId,required"` - Type SourceURLPartType `json:"type,required"` - URL string `json:"url,required"` - ProviderMetadata map[string]interface{} `json:"providerMetadata"` - Title string `json:"title"` - JSON sourceURLPartJSON `json:"-"` +type SnapshotPart struct { + ID string `json:"id,required"` + MessageID string `json:"messageID,required"` + SessionID string `json:"sessionID,required"` + Snapshot string `json:"snapshot,required"` + Type SnapshotPartType `json:"type,required"` + JSON snapshotPartJSON `json:"-"` } -// sourceURLPartJSON contains the JSON metadata for the struct [SourceURLPart] -type sourceURLPartJSON struct { - SourceID apijson.Field - Type apijson.Field - URL apijson.Field - ProviderMetadata apijson.Field - Title apijson.Field - raw string - ExtraFields map[string]apijson.Field +// snapshotPartJSON contains the JSON metadata for the struct [SnapshotPart] +type snapshotPartJSON struct { + ID apijson.Field + MessageID apijson.Field + SessionID apijson.Field + Snapshot apijson.Field + Type apijson.Field + raw string + ExtraFields map[string]apijson.Field } -func (r *SourceURLPart) UnmarshalJSON(data []byte) (err error) { +func (r *SnapshotPart) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } -func (r sourceURLPartJSON) RawJSON() string { +func (r snapshotPartJSON) RawJSON() string { return r.raw } -func (r SourceURLPart) implementsMessagePart() {} +func (r SnapshotPart) implementsPart() {} -type SourceURLPartType string +type SnapshotPartType string const ( - SourceURLPartTypeSourceURL SourceURLPartType = "source-url" + SnapshotPartTypeSnapshot SnapshotPartType = "snapshot" ) -func (r SourceURLPartType) IsKnown() bool { +func (r SnapshotPartType) IsKnown() bool { switch r { - case SourceURLPartTypeSourceURL: + case SnapshotPartTypeSnapshot: return true } return false } -type SourceURLPartParam struct { - SourceID param.Field[string] `json:"sourceId,required"` - Type param.Field[SourceURLPartType] `json:"type,required"` - URL param.Field[string] `json:"url,required"` - ProviderMetadata param.Field[map[string]interface{}] `json:"providerMetadata"` - Title param.Field[string] `json:"title"` +type StepFinishPart struct { + ID string `json:"id,required"` + Cost float64 `json:"cost,required"` + MessageID string `json:"messageID,required"` + SessionID string `json:"sessionID,required"` + Tokens StepFinishPartTokens `json:"tokens,required"` + Type StepFinishPartType `json:"type,required"` + JSON stepFinishPartJSON `json:"-"` } -func (r SourceURLPartParam) MarshalJSON() (data []byte, err error) { - return apijson.MarshalRoot(r) +// stepFinishPartJSON contains the JSON metadata for the struct [StepFinishPart] +type stepFinishPartJSON struct { + ID apijson.Field + Cost apijson.Field + MessageID apijson.Field + SessionID apijson.Field + Tokens apijson.Field + Type apijson.Field + raw string + ExtraFields map[string]apijson.Field } -func (r SourceURLPartParam) implementsMessagePartUnionParam() {} +func (r *StepFinishPart) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r stepFinishPartJSON) RawJSON() string { + return r.raw +} + +func (r StepFinishPart) implementsPart() {} + +type StepFinishPartTokens struct { + Cache StepFinishPartTokensCache `json:"cache,required"` + Input float64 `json:"input,required"` + Output float64 `json:"output,required"` + Reasoning float64 `json:"reasoning,required"` + JSON stepFinishPartTokensJSON `json:"-"` +} + +// stepFinishPartTokensJSON contains the JSON metadata for the struct +// [StepFinishPartTokens] +type stepFinishPartTokensJSON struct { + Cache apijson.Field + Input apijson.Field + Output apijson.Field + Reasoning apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *StepFinishPartTokens) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r stepFinishPartTokensJSON) RawJSON() string { + return r.raw +} + +type StepFinishPartTokensCache struct { + Read float64 `json:"read,required"` + Write float64 `json:"write,required"` + JSON stepFinishPartTokensCacheJSON `json:"-"` +} + +// stepFinishPartTokensCacheJSON contains the JSON metadata for the struct +// [StepFinishPartTokensCache] +type stepFinishPartTokensCacheJSON struct { + Read apijson.Field + Write apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *StepFinishPartTokensCache) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r stepFinishPartTokensCacheJSON) RawJSON() string { + return r.raw +} + +type StepFinishPartType string + +const ( + StepFinishPartTypeStepFinish StepFinishPartType = "step-finish" +) + +func (r StepFinishPartType) IsKnown() bool { + switch r { + case StepFinishPartTypeStepFinish: + return true + } + return false +} type StepStartPart struct { - Type StepStartPartType `json:"type,required"` - JSON stepStartPartJSON `json:"-"` + ID string `json:"id,required"` + MessageID string `json:"messageID,required"` + SessionID string `json:"sessionID,required"` + Type StepStartPartType `json:"type,required"` + JSON stepStartPartJSON `json:"-"` } // stepStartPartJSON contains the JSON metadata for the struct [StepStartPart] type stepStartPartJSON struct { + ID apijson.Field + MessageID apijson.Field + SessionID apijson.Field Type apijson.Field raw string ExtraFields map[string]apijson.Field @@ -942,7 +990,7 @@ func (r stepStartPartJSON) RawJSON() string { return r.raw } -func (r StepStartPart) implementsMessagePart() {} +func (r StepStartPart) implementsPart() {} type StepStartPartType string @@ -958,26 +1006,26 @@ func (r StepStartPartType) IsKnown() bool { return false } -type StepStartPartParam struct { - Type param.Field[StepStartPartType] `json:"type,required"` -} - -func (r StepStartPartParam) MarshalJSON() (data []byte, err error) { - return apijson.MarshalRoot(r) -} - -func (r StepStartPartParam) implementsMessagePartUnionParam() {} - type TextPart struct { - Text string `json:"text,required"` - Type TextPartType `json:"type,required"` - JSON textPartJSON `json:"-"` + ID string `json:"id,required"` + MessageID string `json:"messageID,required"` + SessionID string `json:"sessionID,required"` + Text string `json:"text,required"` + Type TextPartType `json:"type,required"` + Synthetic bool `json:"synthetic"` + Time TextPartTime `json:"time"` + JSON textPartJSON `json:"-"` } // textPartJSON contains the JSON metadata for the struct [TextPart] type textPartJSON struct { + ID apijson.Field + MessageID apijson.Field + SessionID apijson.Field Text apijson.Field Type apijson.Field + Synthetic apijson.Field + Time apijson.Field raw string ExtraFields map[string]apijson.Field } @@ -990,7 +1038,7 @@ func (r textPartJSON) RawJSON() string { return r.raw } -func (r TextPart) implementsMessagePart() {} +func (r TextPart) implementsPart() {} type TextPartType string @@ -1006,131 +1054,122 @@ func (r TextPartType) IsKnown() bool { return false } +type TextPartTime struct { + Start float64 `json:"start,required"` + End float64 `json:"end"` + JSON textPartTimeJSON `json:"-"` +} + +// textPartTimeJSON contains the JSON metadata for the struct [TextPartTime] +type textPartTimeJSON struct { + Start apijson.Field + End apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *TextPartTime) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r textPartTimeJSON) RawJSON() string { + return r.raw +} + type TextPartParam struct { - Text param.Field[string] `json:"text,required"` - Type param.Field[TextPartType] `json:"type,required"` + ID param.Field[string] `json:"id,required"` + MessageID param.Field[string] `json:"messageID,required"` + SessionID param.Field[string] `json:"sessionID,required"` + Text param.Field[string] `json:"text,required"` + Type param.Field[TextPartType] `json:"type,required"` + Synthetic param.Field[bool] `json:"synthetic"` + Time param.Field[TextPartTimeParam] `json:"time"` } func (r TextPartParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } -func (r TextPartParam) implementsMessagePartUnionParam() {} +func (r TextPartParam) implementsSessionChatParamsPartUnion() {} -type ToolCall struct { - State ToolCallState `json:"state,required"` - ToolCallID string `json:"toolCallId,required"` - ToolName string `json:"toolName,required"` - Args interface{} `json:"args"` - Step float64 `json:"step"` - JSON toolCallJSON `json:"-"` +type TextPartTimeParam struct { + Start param.Field[float64] `json:"start,required"` + End param.Field[float64] `json:"end"` } -// toolCallJSON contains the JSON metadata for the struct [ToolCall] -type toolCallJSON struct { - State apijson.Field - ToolCallID apijson.Field - ToolName apijson.Field - Args apijson.Field - Step apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *ToolCall) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r toolCallJSON) RawJSON() string { - return r.raw -} - -func (r ToolCall) implementsToolInvocationPartToolInvocation() {} - -type ToolCallState string - -const ( - ToolCallStateCall ToolCallState = "call" -) - -func (r ToolCallState) IsKnown() bool { - switch r { - case ToolCallStateCall: - return true - } - return false -} - -type ToolCallParam struct { - State param.Field[ToolCallState] `json:"state,required"` - ToolCallID param.Field[string] `json:"toolCallId,required"` - ToolName param.Field[string] `json:"toolName,required"` - Args param.Field[interface{}] `json:"args"` - Step param.Field[float64] `json:"step"` -} - -func (r ToolCallParam) MarshalJSON() (data []byte, err error) { +func (r TextPartTimeParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } -func (r ToolCallParam) implementsToolInvocationPartToolInvocationUnionParam() {} - -type ToolInvocationPart struct { - ToolInvocation ToolInvocationPartToolInvocation `json:"toolInvocation,required"` - Type ToolInvocationPartType `json:"type,required"` - JSON toolInvocationPartJSON `json:"-"` +type ToolPart struct { + ID string `json:"id,required"` + CallID string `json:"callID,required"` + MessageID string `json:"messageID,required"` + SessionID string `json:"sessionID,required"` + State ToolPartState `json:"state,required"` + Tool string `json:"tool,required"` + Type ToolPartType `json:"type,required"` + JSON toolPartJSON `json:"-"` } -// toolInvocationPartJSON contains the JSON metadata for the struct -// [ToolInvocationPart] -type toolInvocationPartJSON struct { - ToolInvocation apijson.Field - Type apijson.Field - raw string - ExtraFields map[string]apijson.Field -} - -func (r *ToolInvocationPart) UnmarshalJSON(data []byte) (err error) { - return apijson.UnmarshalRoot(data, r) -} - -func (r toolInvocationPartJSON) RawJSON() string { - return r.raw -} - -func (r ToolInvocationPart) implementsMessagePart() {} - -type ToolInvocationPartToolInvocation struct { - State ToolInvocationPartToolInvocationState `json:"state,required"` - ToolCallID string `json:"toolCallId,required"` - ToolName string `json:"toolName,required"` - // This field can have the runtime type of [interface{}]. - Args interface{} `json:"args"` - Result string `json:"result"` - Step float64 `json:"step"` - JSON toolInvocationPartToolInvocationJSON `json:"-"` - union ToolInvocationPartToolInvocationUnion -} - -// toolInvocationPartToolInvocationJSON contains the JSON metadata for the struct -// [ToolInvocationPartToolInvocation] -type toolInvocationPartToolInvocationJSON struct { +// toolPartJSON contains the JSON metadata for the struct [ToolPart] +type toolPartJSON struct { + ID apijson.Field + CallID apijson.Field + MessageID apijson.Field + SessionID apijson.Field State apijson.Field - ToolCallID apijson.Field - ToolName apijson.Field - Args apijson.Field - Result apijson.Field - Step apijson.Field + Tool apijson.Field + Type apijson.Field raw string ExtraFields map[string]apijson.Field } -func (r toolInvocationPartToolInvocationJSON) RawJSON() string { +func (r *ToolPart) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r toolPartJSON) RawJSON() string { return r.raw } -func (r *ToolInvocationPartToolInvocation) UnmarshalJSON(data []byte) (err error) { - *r = ToolInvocationPartToolInvocation{} +func (r ToolPart) implementsPart() {} + +type ToolPartState struct { + Status ToolPartStateStatus `json:"status,required"` + Error string `json:"error"` + // This field can have the runtime type of [interface{}], [map[string]interface{}]. + Input interface{} `json:"input"` + // This field can have the runtime type of [map[string]interface{}]. + Metadata interface{} `json:"metadata"` + Output string `json:"output"` + // This field can have the runtime type of [ToolStateRunningTime], + // [ToolStateCompletedTime], [ToolStateErrorTime]. + Time interface{} `json:"time"` + Title string `json:"title"` + JSON toolPartStateJSON `json:"-"` + union ToolPartStateUnion +} + +// toolPartStateJSON contains the JSON metadata for the struct [ToolPartState] +type toolPartStateJSON struct { + Status apijson.Field + Error apijson.Field + Input apijson.Field + Metadata apijson.Field + Output apijson.Field + Time apijson.Field + Title apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r toolPartStateJSON) RawJSON() string { + return r.raw +} + +func (r *ToolPartState) UnmarshalJSON(data []byte) (err error) { + *r = ToolPartState{} err = apijson.UnmarshalRoot(data, &r.union) if err != nil { return err @@ -1138,235 +1177,455 @@ func (r *ToolInvocationPartToolInvocation) UnmarshalJSON(data []byte) (err error return apijson.Port(r.union, &r) } -// AsUnion returns a [ToolInvocationPartToolInvocationUnion] interface which you -// can cast to the specific types for more type safety. +// AsUnion returns a [ToolPartStateUnion] interface which you can cast to the +// specific types for more type safety. // -// Possible runtime types of the union are [ToolCall], [ToolPartialCall], -// [ToolResult]. -func (r ToolInvocationPartToolInvocation) AsUnion() ToolInvocationPartToolInvocationUnion { +// Possible runtime types of the union are [ToolStatePending], [ToolStateRunning], +// [ToolStateCompleted], [ToolStateError]. +func (r ToolPartState) AsUnion() ToolPartStateUnion { return r.union } -// Union satisfied by [ToolCall], [ToolPartialCall] or [ToolResult]. -type ToolInvocationPartToolInvocationUnion interface { - implementsToolInvocationPartToolInvocation() +// Union satisfied by [ToolStatePending], [ToolStateRunning], [ToolStateCompleted] +// or [ToolStateError]. +type ToolPartStateUnion interface { + implementsToolPartState() } func init() { apijson.RegisterUnion( - reflect.TypeOf((*ToolInvocationPartToolInvocationUnion)(nil)).Elem(), - "state", + reflect.TypeOf((*ToolPartStateUnion)(nil)).Elem(), + "status", apijson.UnionVariant{ TypeFilter: gjson.JSON, - Type: reflect.TypeOf(ToolCall{}), - DiscriminatorValue: "call", + Type: reflect.TypeOf(ToolStatePending{}), + DiscriminatorValue: "pending", }, apijson.UnionVariant{ TypeFilter: gjson.JSON, - Type: reflect.TypeOf(ToolPartialCall{}), - DiscriminatorValue: "partial-call", + Type: reflect.TypeOf(ToolStateRunning{}), + DiscriminatorValue: "running", }, apijson.UnionVariant{ TypeFilter: gjson.JSON, - Type: reflect.TypeOf(ToolResult{}), - DiscriminatorValue: "result", + Type: reflect.TypeOf(ToolStateCompleted{}), + DiscriminatorValue: "completed", + }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(ToolStateError{}), + DiscriminatorValue: "error", }, ) } -type ToolInvocationPartToolInvocationState string +type ToolPartStateStatus string const ( - ToolInvocationPartToolInvocationStateCall ToolInvocationPartToolInvocationState = "call" - ToolInvocationPartToolInvocationStatePartialCall ToolInvocationPartToolInvocationState = "partial-call" - ToolInvocationPartToolInvocationStateResult ToolInvocationPartToolInvocationState = "result" + ToolPartStateStatusPending ToolPartStateStatus = "pending" + ToolPartStateStatusRunning ToolPartStateStatus = "running" + ToolPartStateStatusCompleted ToolPartStateStatus = "completed" + ToolPartStateStatusError ToolPartStateStatus = "error" ) -func (r ToolInvocationPartToolInvocationState) IsKnown() bool { +func (r ToolPartStateStatus) IsKnown() bool { switch r { - case ToolInvocationPartToolInvocationStateCall, ToolInvocationPartToolInvocationStatePartialCall, ToolInvocationPartToolInvocationStateResult: + case ToolPartStateStatusPending, ToolPartStateStatusRunning, ToolPartStateStatusCompleted, ToolPartStateStatusError: return true } return false } -type ToolInvocationPartType string +type ToolPartType string const ( - ToolInvocationPartTypeToolInvocation ToolInvocationPartType = "tool-invocation" + ToolPartTypeTool ToolPartType = "tool" ) -func (r ToolInvocationPartType) IsKnown() bool { +func (r ToolPartType) IsKnown() bool { switch r { - case ToolInvocationPartTypeToolInvocation: + case ToolPartTypeTool: return true } return false } -type ToolInvocationPartParam struct { - ToolInvocation param.Field[ToolInvocationPartToolInvocationUnionParam] `json:"toolInvocation,required"` - Type param.Field[ToolInvocationPartType] `json:"type,required"` +type ToolStateCompleted struct { + Input map[string]interface{} `json:"input,required"` + Metadata map[string]interface{} `json:"metadata,required"` + Output string `json:"output,required"` + Status ToolStateCompletedStatus `json:"status,required"` + Time ToolStateCompletedTime `json:"time,required"` + Title string `json:"title,required"` + JSON toolStateCompletedJSON `json:"-"` } -func (r ToolInvocationPartParam) MarshalJSON() (data []byte, err error) { - return apijson.MarshalRoot(r) -} - -func (r ToolInvocationPartParam) implementsMessagePartUnionParam() {} - -type ToolInvocationPartToolInvocationParam struct { - State param.Field[ToolInvocationPartToolInvocationState] `json:"state,required"` - ToolCallID param.Field[string] `json:"toolCallId,required"` - ToolName param.Field[string] `json:"toolName,required"` - Args param.Field[interface{}] `json:"args"` - Result param.Field[string] `json:"result"` - Step param.Field[float64] `json:"step"` -} - -func (r ToolInvocationPartToolInvocationParam) MarshalJSON() (data []byte, err error) { - return apijson.MarshalRoot(r) -} - -func (r ToolInvocationPartToolInvocationParam) implementsToolInvocationPartToolInvocationUnionParam() { -} - -// Satisfied by [ToolCallParam], [ToolPartialCallParam], [ToolResultParam], -// [ToolInvocationPartToolInvocationParam]. -type ToolInvocationPartToolInvocationUnionParam interface { - implementsToolInvocationPartToolInvocationUnionParam() -} - -type ToolPartialCall struct { - State ToolPartialCallState `json:"state,required"` - ToolCallID string `json:"toolCallId,required"` - ToolName string `json:"toolName,required"` - Args interface{} `json:"args"` - Step float64 `json:"step"` - JSON toolPartialCallJSON `json:"-"` -} - -// toolPartialCallJSON contains the JSON metadata for the struct [ToolPartialCall] -type toolPartialCallJSON struct { - State apijson.Field - ToolCallID apijson.Field - ToolName apijson.Field - Args apijson.Field - Step apijson.Field +// toolStateCompletedJSON contains the JSON metadata for the struct +// [ToolStateCompleted] +type toolStateCompletedJSON struct { + Input apijson.Field + Metadata apijson.Field + Output apijson.Field + Status apijson.Field + Time apijson.Field + Title apijson.Field raw string ExtraFields map[string]apijson.Field } -func (r *ToolPartialCall) UnmarshalJSON(data []byte) (err error) { +func (r *ToolStateCompleted) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } -func (r toolPartialCallJSON) RawJSON() string { +func (r toolStateCompletedJSON) RawJSON() string { return r.raw } -func (r ToolPartialCall) implementsToolInvocationPartToolInvocation() {} +func (r ToolStateCompleted) implementsToolPartState() {} -type ToolPartialCallState string +type ToolStateCompletedStatus string const ( - ToolPartialCallStatePartialCall ToolPartialCallState = "partial-call" + ToolStateCompletedStatusCompleted ToolStateCompletedStatus = "completed" ) -func (r ToolPartialCallState) IsKnown() bool { +func (r ToolStateCompletedStatus) IsKnown() bool { switch r { - case ToolPartialCallStatePartialCall: + case ToolStateCompletedStatusCompleted: return true } return false } -type ToolPartialCallParam struct { - State param.Field[ToolPartialCallState] `json:"state,required"` - ToolCallID param.Field[string] `json:"toolCallId,required"` - ToolName param.Field[string] `json:"toolName,required"` - Args param.Field[interface{}] `json:"args"` - Step param.Field[float64] `json:"step"` +type ToolStateCompletedTime struct { + End float64 `json:"end,required"` + Start float64 `json:"start,required"` + JSON toolStateCompletedTimeJSON `json:"-"` } -func (r ToolPartialCallParam) MarshalJSON() (data []byte, err error) { - return apijson.MarshalRoot(r) -} - -func (r ToolPartialCallParam) implementsToolInvocationPartToolInvocationUnionParam() {} - -type ToolResult struct { - Result string `json:"result,required"` - State ToolResultState `json:"state,required"` - ToolCallID string `json:"toolCallId,required"` - ToolName string `json:"toolName,required"` - Args interface{} `json:"args"` - Step float64 `json:"step"` - JSON toolResultJSON `json:"-"` -} - -// toolResultJSON contains the JSON metadata for the struct [ToolResult] -type toolResultJSON struct { - Result apijson.Field - State apijson.Field - ToolCallID apijson.Field - ToolName apijson.Field - Args apijson.Field - Step apijson.Field +// toolStateCompletedTimeJSON contains the JSON metadata for the struct +// [ToolStateCompletedTime] +type toolStateCompletedTimeJSON struct { + End apijson.Field + Start apijson.Field raw string ExtraFields map[string]apijson.Field } -func (r *ToolResult) UnmarshalJSON(data []byte) (err error) { +func (r *ToolStateCompletedTime) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } -func (r toolResultJSON) RawJSON() string { +func (r toolStateCompletedTimeJSON) RawJSON() string { return r.raw } -func (r ToolResult) implementsToolInvocationPartToolInvocation() {} +type ToolStateError struct { + Error string `json:"error,required"` + Input map[string]interface{} `json:"input,required"` + Status ToolStateErrorStatus `json:"status,required"` + Time ToolStateErrorTime `json:"time,required"` + JSON toolStateErrorJSON `json:"-"` +} -type ToolResultState string +// toolStateErrorJSON contains the JSON metadata for the struct [ToolStateError] +type toolStateErrorJSON struct { + Error apijson.Field + Input apijson.Field + Status apijson.Field + Time apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *ToolStateError) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r toolStateErrorJSON) RawJSON() string { + return r.raw +} + +func (r ToolStateError) implementsToolPartState() {} + +type ToolStateErrorStatus string const ( - ToolResultStateResult ToolResultState = "result" + ToolStateErrorStatusError ToolStateErrorStatus = "error" ) -func (r ToolResultState) IsKnown() bool { +func (r ToolStateErrorStatus) IsKnown() bool { switch r { - case ToolResultStateResult: + case ToolStateErrorStatusError: return true } return false } -type ToolResultParam struct { - Result param.Field[string] `json:"result,required"` - State param.Field[ToolResultState] `json:"state,required"` - ToolCallID param.Field[string] `json:"toolCallId,required"` - ToolName param.Field[string] `json:"toolName,required"` - Args param.Field[interface{}] `json:"args"` - Step param.Field[float64] `json:"step"` +type ToolStateErrorTime struct { + End float64 `json:"end,required"` + Start float64 `json:"start,required"` + JSON toolStateErrorTimeJSON `json:"-"` } -func (r ToolResultParam) MarshalJSON() (data []byte, err error) { - return apijson.MarshalRoot(r) +// toolStateErrorTimeJSON contains the JSON metadata for the struct +// [ToolStateErrorTime] +type toolStateErrorTimeJSON struct { + End apijson.Field + Start apijson.Field + raw string + ExtraFields map[string]apijson.Field } -func (r ToolResultParam) implementsToolInvocationPartToolInvocationUnionParam() {} +func (r *ToolStateErrorTime) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r toolStateErrorTimeJSON) RawJSON() string { + return r.raw +} + +type ToolStatePending struct { + Status ToolStatePendingStatus `json:"status,required"` + JSON toolStatePendingJSON `json:"-"` +} + +// toolStatePendingJSON contains the JSON metadata for the struct +// [ToolStatePending] +type toolStatePendingJSON struct { + Status apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *ToolStatePending) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r toolStatePendingJSON) RawJSON() string { + return r.raw +} + +func (r ToolStatePending) implementsToolPartState() {} + +type ToolStatePendingStatus string + +const ( + ToolStatePendingStatusPending ToolStatePendingStatus = "pending" +) + +func (r ToolStatePendingStatus) IsKnown() bool { + switch r { + case ToolStatePendingStatusPending: + return true + } + return false +} + +type ToolStateRunning struct { + Status ToolStateRunningStatus `json:"status,required"` + Time ToolStateRunningTime `json:"time,required"` + Input interface{} `json:"input"` + Metadata map[string]interface{} `json:"metadata"` + Title string `json:"title"` + JSON toolStateRunningJSON `json:"-"` +} + +// toolStateRunningJSON contains the JSON metadata for the struct +// [ToolStateRunning] +type toolStateRunningJSON struct { + Status apijson.Field + Time apijson.Field + Input apijson.Field + Metadata apijson.Field + Title apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *ToolStateRunning) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r toolStateRunningJSON) RawJSON() string { + return r.raw +} + +func (r ToolStateRunning) implementsToolPartState() {} + +type ToolStateRunningStatus string + +const ( + ToolStateRunningStatusRunning ToolStateRunningStatus = "running" +) + +func (r ToolStateRunningStatus) IsKnown() bool { + switch r { + case ToolStateRunningStatusRunning: + return true + } + return false +} + +type ToolStateRunningTime struct { + Start float64 `json:"start,required"` + JSON toolStateRunningTimeJSON `json:"-"` +} + +// toolStateRunningTimeJSON contains the JSON metadata for the struct +// [ToolStateRunningTime] +type toolStateRunningTimeJSON struct { + Start apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *ToolStateRunningTime) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r toolStateRunningTimeJSON) RawJSON() string { + return r.raw +} + +type UserMessage struct { + ID string `json:"id,required"` + Role UserMessageRole `json:"role,required"` + SessionID string `json:"sessionID,required"` + Time UserMessageTime `json:"time,required"` + JSON userMessageJSON `json:"-"` +} + +// userMessageJSON contains the JSON metadata for the struct [UserMessage] +type userMessageJSON struct { + ID apijson.Field + Role apijson.Field + SessionID apijson.Field + Time apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *UserMessage) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r userMessageJSON) RawJSON() string { + return r.raw +} + +func (r UserMessage) implementsMessage() {} + +type UserMessageRole string + +const ( + UserMessageRoleUser UserMessageRole = "user" +) + +func (r UserMessageRole) IsKnown() bool { + switch r { + case UserMessageRoleUser: + return true + } + return false +} + +type UserMessageTime struct { + Created float64 `json:"created,required"` + JSON userMessageTimeJSON `json:"-"` +} + +// userMessageTimeJSON contains the JSON metadata for the struct [UserMessageTime] +type userMessageTimeJSON struct { + Created apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *UserMessageTime) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r userMessageTimeJSON) RawJSON() string { + return r.raw +} + +type SessionMessagesResponse struct { + Info Message `json:"info,required"` + Parts []Part `json:"parts,required"` + JSON sessionMessagesResponseJSON `json:"-"` +} + +// sessionMessagesResponseJSON contains the JSON metadata for the struct +// [SessionMessagesResponse] +type sessionMessagesResponseJSON struct { + Info apijson.Field + Parts apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *SessionMessagesResponse) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r sessionMessagesResponseJSON) RawJSON() string { + return r.raw +} type SessionChatParams struct { - ModelID param.Field[string] `json:"modelID,required"` - Parts param.Field[[]MessagePartUnionParam] `json:"parts,required"` - ProviderID param.Field[string] `json:"providerID,required"` + MessageID param.Field[string] `json:"messageID,required"` + Mode param.Field[string] `json:"mode,required"` + ModelID param.Field[string] `json:"modelID,required"` + Parts param.Field[[]SessionChatParamsPartUnion] `json:"parts,required"` + ProviderID param.Field[string] `json:"providerID,required"` } func (r SessionChatParams) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } +type SessionChatParamsPart struct { + ID param.Field[string] `json:"id,required"` + MessageID param.Field[string] `json:"messageID,required"` + SessionID param.Field[string] `json:"sessionID,required"` + Type param.Field[SessionChatParamsPartsType] `json:"type,required"` + Filename param.Field[string] `json:"filename"` + Mime param.Field[string] `json:"mime"` + Synthetic param.Field[bool] `json:"synthetic"` + Text param.Field[string] `json:"text"` + Time param.Field[interface{}] `json:"time"` + URL param.Field[string] `json:"url"` +} + +func (r SessionChatParamsPart) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +func (r SessionChatParamsPart) implementsSessionChatParamsPartUnion() {} + +// Satisfied by [FilePartParam], [TextPartParam], [SessionChatParamsPart]. +type SessionChatParamsPartUnion interface { + implementsSessionChatParamsPartUnion() +} + +type SessionChatParamsPartsType string + +const ( + SessionChatParamsPartsTypeFile SessionChatParamsPartsType = "file" + SessionChatParamsPartsTypeText SessionChatParamsPartsType = "text" +) + +func (r SessionChatParamsPartsType) IsKnown() bool { + switch r { + case SessionChatParamsPartsTypeFile, SessionChatParamsPartsTypeText: + return true + } + return false +} + type SessionInitParams struct { + MessageID param.Field[string] `json:"messageID,required"` ModelID param.Field[string] `json:"modelID,required"` ProviderID param.Field[string] `json:"providerID,required"` } diff --git a/packages/tui/sdk/session_test.go b/packages/tui/sdk/session_test.go index da9fb825..c74a4a38 100644 --- a/packages/tui/sdk/session_test.go +++ b/packages/tui/sdk/session_test.go @@ -117,10 +117,17 @@ func TestSessionChat(t *testing.T) { context.TODO(), "id", opencode.SessionChatParams{ - ModelID: opencode.F("modelID"), - Parts: opencode.F([]opencode.MessagePartUnionParam{opencode.TextPartParam{ - Text: opencode.F("text"), - Type: opencode.F(opencode.TextPartTypeText), + MessageID: opencode.F("messageID"), + Mode: opencode.F("mode"), + ModelID: opencode.F("modelID"), + Parts: opencode.F([]opencode.SessionChatParamsPartUnion{opencode.FilePartParam{ + ID: opencode.F("id"), + MessageID: opencode.F("messageID"), + Mime: opencode.F("mime"), + SessionID: opencode.F("sessionID"), + Type: opencode.F(opencode.FilePartTypeFile), + URL: opencode.F("url"), + Filename: opencode.F("filename"), }}), ProviderID: opencode.F("providerID"), }, @@ -150,6 +157,7 @@ func TestSessionInit(t *testing.T) { context.TODO(), "id", opencode.SessionInitParams{ + MessageID: opencode.F("messageID"), ModelID: opencode.F("modelID"), ProviderID: opencode.F("providerID"), }, diff --git a/packages/tui/sdk/shared/shared.go b/packages/tui/sdk/shared/shared.go index 121f64a5..58baf3d9 100644 --- a/packages/tui/sdk/shared/shared.go +++ b/packages/tui/sdk/shared/shared.go @@ -6,6 +6,47 @@ import ( "github.com/sst/opencode-sdk-go/internal/apijson" ) +type MessageAbortedError struct { + Data interface{} `json:"data,required"` + Name MessageAbortedErrorName `json:"name,required"` + JSON messageAbortedErrorJSON `json:"-"` +} + +// messageAbortedErrorJSON contains the JSON metadata for the struct +// [MessageAbortedError] +type messageAbortedErrorJSON struct { + Data apijson.Field + Name apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *MessageAbortedError) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r messageAbortedErrorJSON) RawJSON() string { + return r.raw +} + +func (r MessageAbortedError) ImplementsEventListResponseEventSessionErrorPropertiesError() {} + +func (r MessageAbortedError) ImplementsAssistantMessageError() {} + +type MessageAbortedErrorName string + +const ( + MessageAbortedErrorNameMessageAbortedError MessageAbortedErrorName = "MessageAbortedError" +) + +func (r MessageAbortedErrorName) IsKnown() bool { + switch r { + case MessageAbortedErrorNameMessageAbortedError: + return true + } + return false +} + type ProviderAuthError struct { Data ProviderAuthErrorData `json:"data,required"` Name ProviderAuthErrorName `json:"name,required"` @@ -31,7 +72,7 @@ func (r providerAuthErrorJSON) RawJSON() string { func (r ProviderAuthError) ImplementsEventListResponseEventSessionErrorPropertiesError() {} -func (r ProviderAuthError) ImplementsMessageMetadataError() {} +func (r ProviderAuthError) ImplementsAssistantMessageError() {} type ProviderAuthErrorData struct { Message string `json:"message,required"` @@ -94,7 +135,7 @@ func (r unknownErrorJSON) RawJSON() string { func (r UnknownError) ImplementsEventListResponseEventSessionErrorPropertiesError() {} -func (r UnknownError) ImplementsMessageMetadataError() {} +func (r UnknownError) ImplementsAssistantMessageError() {} type UnknownErrorData struct { Message string `json:"message,required"` diff --git a/packages/tui/sdk/usage_test.go b/packages/tui/sdk/usage_test.go index 0e261a7a..ef7ce8bd 100644 --- a/packages/tui/sdk/usage_test.go +++ b/packages/tui/sdk/usage_test.go @@ -23,10 +23,10 @@ func TestUsage(t *testing.T) { client := opencode.NewClient( option.WithBaseURL(baseURL), ) - events, err := client.Event.List(context.TODO()) + sessions, err := client.Session.List(context.TODO()) if err != nil { t.Error(err) return } - t.Logf("%+v\n", events) + t.Logf("%+v\n", sessions) } diff --git a/packages/web/astro.config.mjs b/packages/web/astro.config.mjs index 0c3a5ecc..a250ce60 100644 --- a/packages/web/astro.config.mjs +++ b/packages/web/astro.config.mjs @@ -7,6 +7,7 @@ import theme from "toolbeam-docs-theme" import config from "./config.mjs" import { rehypeHeadingIds } from "@astrojs/markdown-remark" import rehypeAutolinkHeadings from "rehype-autolink-headings" +import { spawnSync } from "child_process" const github = "https://github.com/sst/opencode" @@ -20,18 +21,24 @@ export default defineConfig({ devToolbar: { enabled: false, }, - markdown: { - rehypePlugins: [ - rehypeHeadingIds, - [rehypeAutolinkHeadings, { behavior: "wrap" }], - ], + server: { + host: "0.0.0.0", }, + markdown: { + rehypePlugins: [rehypeHeadingIds, [rehypeAutolinkHeadings, { behavior: "wrap" }]], + }, + build: {}, integrations: [ + configSchema(), solidJs(), starlight({ title: "opencode", + lastUpdated: true, expressiveCode: { themes: ["github-light", "github-dark"] }, - social: [{ icon: "github", label: "GitHub", href: config.github }], + social: [ + { icon: "github", label: "GitHub", href: config.github }, + { icon: "discord", label: "Dscord", href: config.discord }, + ], head: [ { tag: "link", @@ -56,12 +63,16 @@ export default defineConfig({ sidebar: [ "docs", "docs/cli", + "docs/share", + "docs/modes", "docs/rules", "docs/config", "docs/models", "docs/themes", "docs/keybinds", + "docs/enterprise", "docs/mcp-servers", + "docs/troubleshooting", ], components: { Hero: "./src/components/Hero.astro", @@ -75,4 +86,19 @@ export default defineConfig({ ], }), ], + redirects: { + "/discord": "https://discord.gg/opencode", + }, }) + +function configSchema() { + return { + name: "configSchema", + hooks: { + "astro:build:done": async () => { + console.log("generating config schema") + spawnSync("../opencode/script/schema.ts", ["./dist/config.json"]) + }, + }, + } +} diff --git a/packages/web/config.mjs b/packages/web/config.mjs index b6ec26e3..bb1ec003 100644 --- a/packages/web/config.mjs +++ b/packages/web/config.mjs @@ -1,7 +1,12 @@ +const stage = process.env.SST_STAGE || "dev" + export default { - url: "https://opencode.ai", + url: stage === "production" + ? "https://opencode.ai" + : `https://${stage}.opencode.ai`, socialCard: "https://social-cards.sst.dev", github: "https://github.com/sst/opencode", + discord: "https://opencode.ai/discord", headerLinks: [ { name: "Home", url: "/" }, { name: "Docs", url: "/docs/" }, diff --git a/packages/web/package.json b/packages/web/package.json index c1722b2b..548c84c3 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -4,6 +4,7 @@ "version": "0.0.1", "scripts": { "dev": "astro dev", + "dev:remote": "sst shell --stage=dev --target=Web astro dev", "start": "astro dev", "build": "astro build", "preview": "astro preview", @@ -26,10 +27,11 @@ "marked": "15.0.12", "marked-shiki": "1.2.0", "rehype-autolink-headings": "7.1.0", + "remeda": "2.26.0", "sharp": "0.32.5", "shiki": "3.4.2", "solid-js": "1.9.7", - "toolbeam-docs-theme": "0.4.1" + "toolbeam-docs-theme": "0.4.3" }, "devDependencies": { "opencode": "workspace:*", diff --git a/packages/web/public/theme.json b/packages/web/public/theme.json index 0b1b95f0..b3e97f7c 100644 --- a/packages/web/public/theme.json +++ b/packages/web/public/theme.json @@ -88,14 +88,7 @@ "syntaxOperator": { "$ref": "#/definitions/colorValue" }, "syntaxPunctuation": { "$ref": "#/definitions/colorValue" } }, - "required": [ - "primary", - "secondary", - "accent", - "text", - "textMuted", - "background" - ], + "required": ["primary", "secondary", "accent", "text", "textMuted", "background"], "additionalProperties": false } }, diff --git a/packages/web/src/assets/lander/screenshot.png b/packages/web/src/assets/lander/screenshot.png index d49a62b4..feb61758 100644 Binary files a/packages/web/src/assets/lander/screenshot.png and b/packages/web/src/assets/lander/screenshot.png differ diff --git a/packages/web/src/components/CodeBlock.tsx b/packages/web/src/components/CodeBlock.tsx deleted file mode 100644 index 6a684689..00000000 --- a/packages/web/src/components/CodeBlock.tsx +++ /dev/null @@ -1,36 +0,0 @@ -import { - type JSX, - splitProps, - createResource, -} from "solid-js" -import { codeToHtml } from "shiki" -import styles from "./codeblock.module.css" -import { transformerNotationDiff } from "@shikijs/transformers" - -interface CodeBlockProps extends JSX.HTMLAttributes { - code: string - lang?: string -} -function CodeBlock(props: CodeBlockProps) { - const [local, rest] = splitProps(props, ["code", "lang"]) - - const [html] = createResource( - () => [local.code, local.lang], - async ([code, lang]) => { - // TODO: For testing delays - // await new Promise((resolve) => setTimeout(resolve, 3000)) - return (await codeToHtml(code || "", { - lang: lang || "text", - themes: { - light: "github-light", - dark: "github-dark", - }, - transformers: [transformerNotationDiff()], - })) as string - }, - ) - - return
-} - -export default CodeBlock diff --git a/packages/web/src/components/MarkdownView.tsx b/packages/web/src/components/MarkdownView.tsx deleted file mode 100644 index 7a63bc0c..00000000 --- a/packages/web/src/components/MarkdownView.tsx +++ /dev/null @@ -1,39 +0,0 @@ -import { type JSX, splitProps, createResource } from "solid-js" -import { marked } from "marked" -import markedShiki from "marked-shiki" -import { codeToHtml } from "shiki" -import { transformerNotationDiff } from "@shikijs/transformers" -import styles from "./markdownview.module.css" - -interface MarkdownViewProps extends JSX.HTMLAttributes { - markdown: string -} - -const markedWithShiki = marked.use( - markedShiki({ - highlight(code, lang) { - return codeToHtml(code, { - lang: lang || "text", - themes: { - light: "github-light", - dark: "github-dark", - }, - transformers: [transformerNotationDiff()], - }) - }, - }), -) - -function MarkdownView(props: MarkdownViewProps) { - const [local, rest] = splitProps(props, ["markdown"]) - const [html] = createResource( - () => local.markdown, - async (markdown) => { - return markedWithShiki.parse(markdown) - }, - ) - - return
-} - -export default MarkdownView diff --git a/packages/web/src/components/Share.tsx b/packages/web/src/components/Share.tsx index ed889790..7d9265bb 100644 --- a/packages/web/src/components/Share.tsx +++ b/packages/web/src/components/Share.tsx @@ -1,83 +1,18 @@ -import { type JSX } from "solid-js" -import { - For, - Show, - Match, - Switch, - onMount, - Suspense, - onCleanup, - splitProps, - createMemo, - createEffect, - createSignal, - SuspenseList, -} from "solid-js" -import map from "lang-map" +import { For, Show, onMount, Suspense, onCleanup, createMemo, createSignal, SuspenseList, createEffect } from "solid-js" import { DateTime } from "luxon" -import { createStore, reconcile } from "solid-js/store" -import type { Diagnostic } from "vscode-languageserver-types" -import { - IconOpenAI, - IconGemini, - IconOpencode, - IconAnthropic, -} from "./icons/custom" -import { - IconHashtag, - IconSparkles, - IconGlobeAlt, - IconDocument, - IconQueueList, - IconUserCircle, - IconCheckCircle, - IconChevronDown, - IconCommandLine, - IconChevronRight, - IconDocumentPlus, - IconPencilSquare, - IconRectangleStack, - IconMagnifyingGlass, - IconWrenchScrewdriver, - IconDocumentMagnifyingGlass, - IconArrowDown, -} from "./icons" -import DiffView from "./DiffView" -import CodeBlock from "./CodeBlock" -import MarkdownView from "./MarkdownView" +import { createStore, reconcile, unwrap } from "solid-js/store" +import { mapValues } from "remeda" +import { IconArrowDown } from "./icons" +import { IconOpencode } from "./icons/custom" import styles from "./share.module.css" +import type { MessageV2 } from "opencode/session/message-v2" import type { Message } from "opencode/session/message" import type { Session } from "opencode/session/index" +import { Part, ProviderIcon } from "./share/part" -const MIN_DURATION = 2 +type MessageWithParts = MessageV2.Info & { parts: MessageV2.Part[] } -type Status = - | "disconnected" - | "connecting" - | "connected" - | "error" - | "reconnecting" - -type TodoStatus = "pending" | "in_progress" | "completed" - -interface Todo { - id: string - content: string - status: TodoStatus - priority: "low" | "medium" | "high" -} - -function sortTodosByStatus(todos: Todo[]) { - const statusPriority: Record = { - in_progress: 0, - pending: 1, - completed: 2, - } - - return todos - .slice() - .sort((a, b) => statusPriority[a.status] - statusPriority[b.status]) -} +type Status = "disconnected" | "connecting" | "connected" | "error" | "reconnecting" function scrollToAnchor(id: string) { const el = document.getElementById(id) @@ -86,146 +21,6 @@ function scrollToAnchor(id: string) { el.scrollIntoView({ behavior: "smooth" }) } -function stripWorkingDirectory(filePath?: string, workingDir?: string) { - if (filePath === undefined || workingDir === undefined) return filePath - - const prefix = workingDir.endsWith("/") ? workingDir : workingDir + "/" - - if (filePath === workingDir) { - return "" - } - - if (filePath.startsWith(prefix)) { - return filePath.slice(prefix.length) - } - - return filePath -} - -function getShikiLang(filename: string) { - const ext = filename.split(".").pop()?.toLowerCase() ?? "" - - // map.languages(ext) returns an array of matching Linguist language names (e.g. ['TypeScript']) - const langs = map.languages(ext) - const type = langs?.[0]?.toLowerCase() - - // Overrride any specific language mappings - const overrides: Record = { - conf: "shellscript", - } - - return type ? (overrides[type] ?? type) : "plaintext" -} - -function formatDuration(ms: number): string { - const ONE_SECOND = 1000 - const ONE_MINUTE = 60 * ONE_SECOND - - if (ms >= ONE_MINUTE) { - const minutes = Math.floor(ms / ONE_MINUTE) - return minutes === 1 ? `1min` : `${minutes}mins` - } - - if (ms >= ONE_SECOND) { - const seconds = Math.floor(ms / ONE_SECOND) - return `${seconds}s` - } - - return `${ms}ms` -} - -// Converts nested objects/arrays into [path, value] pairs. -// E.g. {a:{b:{c:1}}, d:[{e:2}, 3]} => [["a.b.c",1], ["d[0].e",2], ["d[1]",3]] -function flattenToolArgs(obj: any, prefix: string = ""): Array<[string, any]> { - const entries: Array<[string, any]> = [] - - for (const [key, value] of Object.entries(obj)) { - const path = prefix ? `${prefix}.${key}` : key - - if (value !== null && typeof value === "object") { - if (Array.isArray(value)) { - value.forEach((item, index) => { - const arrayPath = `${path}[${index}]` - if (item !== null && typeof item === "object") { - entries.push(...flattenToolArgs(item, arrayPath)) - } else { - entries.push([arrayPath, item]) - } - }) - } else { - entries.push(...flattenToolArgs(value, path)) - } - } else { - entries.push([path, value]) - } - } - - return entries -} - -function formatErrorString(error: string): JSX.Element { - const errorMarker = "Error: " - const startsWithError = error.startsWith(errorMarker) - - return startsWithError ? ( -
-      
-        Error
-      
-      {error.slice(errorMarker.length)}
-    
- ) : ( -
-      {error}
-    
- ) -} - -function getDiagnostics( - diagnosticsByFile: Record, - currentFile: string, -): JSX.Element[] { - // Return a flat array of error diagnostics, in the format: - // "Error [65:20] Property 'x' does not exist on type 'Y'" - const result: JSX.Element[] = [] - - if ( - diagnosticsByFile === undefined || - diagnosticsByFile[currentFile] === undefined - ) - return result - - for (const diags of Object.values(diagnosticsByFile)) { - for (const d of diags) { - // Only keep diagnostics explicitly marked as Error (severity === 1) - if (d.severity !== 1) continue - - const line = d.range.start.line + 1 // 1-based - const column = d.range.start.character + 1 // 1-based - - result.push( -
-          
-            Error
-          
-          
-            [{line}:{column}]
-          
-          {d.message}
-        
, - ) - } - } - - return result -} - -function stripEnclosingTag(text: string): string { - const wrappedRe = /^\s*<([A-Za-z]\w*)>\s*([\s\S]*?)\s*<\/\1>\s*$/ - const match = text.match(wrappedRe) - return match ? match[2] : text -} - function getStatusText(status: [Status, string?]): string { switch (status[0]) { case "connected": @@ -243,317 +38,11 @@ function getStatusText(status: [Status, string?]): string { } } -function checkOverflow(getEl: () => HTMLElement | undefined, watch?: () => any) { - const [needsToggle, setNeedsToggle] = createSignal(false) - - function measure() { - const el = getEl() - if (!el) return - setNeedsToggle(el.scrollHeight > el.clientHeight + 1) - } - - onMount(() => { - let raf = 0 - - function probe() { - const el = getEl() - if (el && el.offsetParent !== null && el.getBoundingClientRect().height) { - measure() - } - else { - raf = requestAnimationFrame(probe) - } - } - raf = requestAnimationFrame(probe) - - const ro = new ResizeObserver(measure) - const el = getEl() - if (el) ro.observe(el) - - onCleanup(() => { - cancelAnimationFrame(raf) - ro.disconnect() - }) - }) - - if (watch) createEffect(measure) - - return needsToggle -} - -function ProviderIcon(props: { provider: string; size?: number }) { - const size = props.size || 16 - return ( - }> - - - - - - - - - - - ) -} - -interface ResultsButtonProps extends JSX.HTMLAttributes { - showCopy?: string - hideCopy?: string - results: boolean -} -function ResultsButton(props: ResultsButtonProps) { - const [local, rest] = splitProps(props, ["results", "showCopy", "hideCopy"]) - return ( - - ) -} - -interface TextPartProps extends JSX.HTMLAttributes { - text: string - expand?: boolean -} -function TextPart(props: TextPartProps) { - let preEl: HTMLPreElement | undefined - - const [local, rest] = splitProps(props, ["text", "expand"]) - const [expanded, setExpanded] = createSignal(false) - const overflowed = checkOverflow(() => preEl, () => local.expand) - - return ( -
-
{local.text}
- {((!local.expand && overflowed()) || expanded()) && ( - - )} -
- ) -} - -interface ErrorPartProps extends JSX.HTMLAttributes { - expand?: boolean -} -function ErrorPart(props: ErrorPartProps) { - let preEl: HTMLDivElement | undefined - - const [local, rest] = splitProps(props, ["expand", "children"]) - const [expanded, setExpanded] = createSignal(false) - const overflowed = checkOverflow(() => preEl, () => local.expand) - - return ( -
-
- {local.children} -
- {((!local.expand && overflowed()) || expanded()) && ( - - )} -
- ) -} - -interface MarkdownPartProps extends JSX.HTMLAttributes { - text: string - expand?: boolean - highlight?: boolean -} -function MarkdownPart(props: MarkdownPartProps) { - let divEl: HTMLDivElement | undefined - - const [local, rest] = splitProps(props, ["text", "expand", "highlight"]) - const [expanded, setExpanded] = createSignal(false) - const overflowed = checkOverflow(() => divEl, () => local.expand) - - return ( -
- (divEl = el)} - /> - {((!local.expand && overflowed()) || expanded()) && ( - - )} -
- ) -} - -interface TerminalPartProps extends JSX.HTMLAttributes { - command: string - error?: string - result?: string - desc?: string - expand?: boolean -} -function TerminalPart(props: TerminalPartProps) { - const [local, rest] = splitProps(props, [ - "command", - "error", - "result", - "desc", - "expand", - ]) - let preEl: HTMLDivElement | undefined - - const [expanded, setExpanded] = createSignal(false) - const overflowed = checkOverflow( - () => { - if (!preEl) return - return preEl.getElementsByTagName("pre")[0] - }, - () => local.expand - ) - - return ( -
-
-
- {local.desc} -
-
- - - - - - - - - -
-
- {((!local.expand && overflowed()) || expanded()) && ( - - )} -
- ) -} - -function ToolFooter(props: { time: number }) { - return props.time > MIN_DURATION ? ( - - {formatDuration(props.time)} - - ) : ( -
- ) -} - -interface AnchorProps extends JSX.HTMLAttributes { - id: string -} -function AnchorIcon(props: AnchorProps) { - const [local, rest] = splitProps(props, ["id", "children"]) - const [copied, setCopied] = createSignal(false) - - return ( - - ) -} - export default function Share(props: { id: string api: string info: Session.Info - messages: Record + messages: Record }) { let lastScrollY = 0 let hasScrolledToAnchor = false @@ -571,14 +60,13 @@ export default function Share(props: { const [store, setStore] = createStore<{ info?: Session.Info - messages: Record - }>({ info: props.info, messages: props.messages }) - const messages = createMemo(() => - Object.values(store.messages).toSorted((a, b) => a.id?.localeCompare(b.id)), - ) - const [connectionStatus, setConnectionStatus] = createSignal< - [Status, string?] - >(["disconnected", "Disconnected"]) + messages: Record + }>({ info: props.info, messages: mapValues(props.messages, (x: any) => "metadata" in x ? fromV1(x) : x) }) + const messages = createMemo(() => Object.values(store.messages).toSorted((a, b) => a.id?.localeCompare(b.id))) + const [connectionStatus, setConnectionStatus] = createSignal<[Status, string?]>(["disconnected", "Disconnected"]) + createEffect(() => { + console.log(unwrap(store)) + }) onMount(() => { const apiUrl = props.api @@ -633,8 +121,22 @@ export default function Share(props: { } if (type === "message") { const [, messageID] = splits + if ("metadata" in d.content) { + d.content = fromV1(d.content) + } + d.content.parts = d.content.parts ?? store.messages[messageID]?.parts ?? [] setStore("messages", messageID, reconcile(d.content)) } + if (type === "part") { + setStore("messages", d.content.messageID, "parts", arr => { + const index = arr.findIndex((x) => x.id === d.content.id) + if (index === -1) + arr.push(d.content) + if (index > -1) + arr[index] = d.content + return [...arr] + }) + } } catch (error) { console.error("Error parsing WebSocket message:", error) } @@ -653,10 +155,7 @@ export default function Share(props: { // Try to reconnect after 2 seconds clearTimeout(reconnectTimer) - reconnectTimer = window.setTimeout( - setupWebSocket, - 2000, - ) as unknown as number + reconnectTimer = window.setTimeout(setupWebSocket, 2000) as unknown as number } } @@ -754,7 +253,7 @@ export default function Share(props: { rootDir: undefined as string | undefined, created: undefined as number | undefined, completed: undefined as number | undefined, - messages: [] as Message.Info[], + messages: [] as MessageWithParts[], models: {} as Record, cost: 0, tokens: { @@ -766,30 +265,26 @@ export default function Share(props: { result.created = props.info.time.created - for (let i = 0; i < messages().length; i++) { - const msg = messages()[i] - - const assistant = msg.metadata?.assistant + const msgs = messages() + for (let i = 0; i < msgs.length; i++) { + const msg = msgs[i] result.messages.push(msg) - if (assistant) { - result.cost += assistant.cost - result.tokens.input += assistant.tokens.input - result.tokens.output += assistant.tokens.output - result.tokens.reasoning += assistant.tokens.reasoning + if (msg.role === "assistant") { + result.cost += msg.cost + result.tokens.input += msg.tokens.input + result.tokens.output += msg.tokens.output + result.tokens.reasoning += msg.tokens.reasoning - result.models[`${assistant.providerID} ${assistant.modelID}`] = [ - assistant.providerID, - assistant.modelID, - ] + result.models[`${msg.providerID} ${msg.modelID}`] = [msg.providerID, msg.modelID] - if (assistant.path?.root) { - result.rootDir = assistant.path.root + if (msg.path.root) { + result.rootDir = msg.path.root } - if (msg.metadata?.time.completed) { - result.completed = msg.metadata?.time.completed + if (msg.time.completed) { + result.completed = msg.time.completed } } } @@ -797,15 +292,13 @@ export default function Share(props: { }) return ( -
-
-
-

{store.info?.title}

-
-
-
    -
  • -
    +
    +
    +

    {store.info?.title}

    +
    +
      +
    • +
      @@ -815,11 +308,11 @@ export default function Share(props: { {Object.values(data().models).length > 0 ? ( {([provider, model]) => ( -
    • -
      - +
    • +
      +
      - {model} + {model}
    • )} @@ -830,1096 +323,69 @@ export default function Share(props: { )}
    -
    - {data().created ? ( - - {DateTime.fromMillis(data().created || 0).toLocaleString( - DateTime.DATETIME_MED, - )} - - ) : ( - - Started at — - - )} +
    + {DateTime.fromMillis(data().created || 0).toLocaleString(DateTime.DATETIME_MED)}
    - 0} - fallback={

    Waiting for messages...

    } - > + 0} fallback={

    Waiting for messages...

    }>
    - {(msg, msgIndex) => ( - - - {(part, partIndex) => { - if ( - (part.type === "step-start" && - (partIndex() > 0 || !msg.metadata?.assistant)) || - (msg.role === "assistant" && - part.type === "tool-invocation" && - part.toolInvocation.toolName === "todoread") - ) - return null + {(msg, msgIndex) => { + const filteredParts = createMemo(() => + msg.parts.filter((x, index) => { + if (x.type === "step-start" && index > 0) return false + if (x.type === "snapshot") return false + if (x.type === "step-finish") return false + if (x.type === "text" && x.synthetic === true) return false + if (x.type === "tool" && x.tool === "todoread") return false + if (x.type === "text" && !x.text) return false + if (x.type === "tool" && (x.state.status === "pending" || x.state.status === "running")) + return false + return true + }) + ) - const anchor = createMemo( - () => `${msg.id}-${partIndex()}`, - ) - const [showResults, setShowResults] = - createSignal(false) - const isLastPart = createMemo( - () => - data().messages.length === msgIndex() + 1 && - msg.parts.length === partIndex() + 1, - ) - const toolData = createMemo(() => { - if ( - msg.role !== "assistant" || - part.type !== "tool-invocation" + return ( + + + {(part, partIndex) => { + const last = createMemo( + () => + data().messages.length === msgIndex() + 1 && filteredParts().length === partIndex() + 1, ) - return {} - const metadata = - msg.metadata?.tool[part.toolInvocation.toolCallId] - const args = part.toolInvocation.args - const result = - part.toolInvocation.state === "result" && - part.toolInvocation.result - const duration = DateTime.fromMillis( - metadata?.time.end || 0, - ) - .diff( - DateTime.fromMillis(metadata?.time.start || 0), - ) - .toMillis() + onMount(() => { + const hash = window.location.hash.slice(1) + // Wait till all parts are loaded + if ( + hash !== "" && + !hasScrolledToAnchor && + filteredParts().length === partIndex() + 1 && + data().messages.length === msgIndex() + 1 + ) { + hasScrolledToAnchor = true + scrollToAnchor(hash) + } + }) - return { metadata, args, result, duration } - }) - - onMount(() => { - const hash = window.location.hash.slice(1) - // Wait till all parts are loaded - if ( - hash !== "" - && !hasScrolledToAnchor - && msg.parts.length === partIndex() + 1 - && data().messages.length === msgIndex() + 1 - ) { - hasScrolledToAnchor = true - scrollToAnchor(hash) - } - }) - - return ( - - {/* User text */} - - {(part) => ( -
    -
    - - - -
    -
    -
    - -
    -
    - )} -
    - {/* AI text */} - - {(part) => ( -
    -
    - - - -
    -
    -
    - - - - {DateTime.fromMillis( - data().completed || 0, - ).toLocaleString(DateTime.DATETIME_MED)} - - -
    -
    - )} -
    - {/* AI model */} - - {(assistant) => { - return ( -
    -
    - - - -
    -
    -
    -
    -
    - - {assistant().providerID} - -
    - - {assistant().modelID} - -
    -
    -
    - ) - }} -
    - - {/* Grep tool */} - - {(_part) => { - const matches = () => - toolData()?.metadata?.matches - const splitArgs = () => { - const { pattern, ...rest } = toolData()?.args - return { pattern, rest } - } - - return ( -
    -
    - - - -
    -
    -
    -
    -
    - Grep - - “{splitArgs().pattern}” - -
    - 0 - } - > -
    - - {([name, value]) => ( - <> -
    -
    {name}
    -
    {value}
    - - )} -
    -
    -
    - - 0}> -
    - - setShowResults((e) => !e) - } - /> - - - -
    -
    - -
    - -
    -
    -
    -
    - -
    -
    - ) - }} -
    - {/* Glob tool */} - - {(_part) => { - const count = () => toolData()?.metadata?.count - const pattern = () => toolData()?.args.pattern - - return ( -
    -
    - - - -
    -
    -
    -
    -
    - Glob - “{pattern()}” -
    - - 0}> -
    - - setShowResults((e) => !e) - } - /> - - - -
    -
    - -
    - -
    -
    -
    -
    - -
    -
    - ) - }} -
    - {/* LS tool */} - - {(_part) => { - const path = createMemo(() => - toolData()?.args?.path !== data().rootDir - ? stripWorkingDirectory( - toolData()?.args?.path, - data().rootDir, - ) - : toolData()?.args?.path, - ) - - return ( -
    -
    - - - -
    -
    -
    -
    -
    - LS - - {path()} - -
    - - -
    - - setShowResults((e) => !e) - } - /> - - - -
    -
    -
    -
    - -
    -
    - ) - }} -
    - {/* Read tool */} - - {(_part) => { - const filePath = createMemo(() => - stripWorkingDirectory( - toolData()?.args?.filePath, - data().rootDir, - ), - ) - const hasError = () => - toolData()?.metadata?.error - const preview = () => - toolData()?.metadata?.preview - - return ( -
    -
    - - - -
    -
    -
    -
    -
    - Read - - {filePath()} - -
    - - -
    - - {formatErrorString( - toolData()?.result, - )} - -
    -
    - {/* Always try to show CodeBlock if preview is available (even if empty string) */} - -
    - - setShowResults((e) => !e) - } - /> - -
    - -
    -
    -
    -
    - {/* Fallback to TextPart if preview is not a string (e.g. undefined) AND result exists */} - -
    - - setShowResults((e) => !e) - } - /> - - - -
    -
    -
    -
    - -
    -
    - ) - }} -
    - {/* Write tool */} - - {(_part) => { - const filePath = createMemo(() => - stripWorkingDirectory( - toolData()?.args?.filePath, - data().rootDir, - ), - ) - const hasError = () => - toolData()?.metadata?.error - const content = () => toolData()?.args?.content - const diagnostics = createMemo(() => - getDiagnostics( - toolData()?.metadata?.diagnostics, - toolData()?.args.filePath, - ), - ) - - return ( -
    -
    - - - -
    -
    -
    -
    -
    - Write - - {filePath()} - -
    - 0}> - {diagnostics()} - - - -
    - - {formatErrorString( - toolData()?.result, - )} - -
    -
    - -
    - - setShowResults((e) => !e) - } - /> - -
    - -
    -
    -
    -
    -
    -
    - -
    -
    - ) - }} -
    - {/* Edit tool */} - - {(_part) => { - const diff = () => toolData()?.metadata?.diff - const message = () => - toolData()?.metadata?.message - const hasError = () => - toolData()?.metadata?.error - const filePath = createMemo(() => - stripWorkingDirectory( - toolData()?.args.filePath, - data().rootDir, - ), - ) - const diagnostics = createMemo(() => - getDiagnostics( - toolData()?.metadata?.diagnostics, - toolData()?.args.filePath, - ), - ) - - return ( -
    -
    - - - -
    -
    -
    -
    -
    - Edit - - {filePath()} - -
    - - -
    - - {formatErrorString(message())} - -
    -
    - -
    - -
    -
    -
    - 0}> - {diagnostics()} - -
    - -
    -
    - ) - }} -
    - {/* Bash tool */} - - {(_part) => { - const command = () => - toolData()?.metadata?.title - const desc = () => - toolData()?.metadata?.description - const result = () => - toolData()?.metadata?.stdout - const error = () => toolData()?.metadata?.stderr - - return ( -
    -
    - - - -
    -
    -
    - {command() && ( -
    - -
    - )} - -
    -
    - ) - }} -
    - {/* Todo write */} - - {(_part) => { - const todos = createMemo(() => - sortTodosByStatus( - toolData()?.args?.todos ?? [], - ), - ) - const starting = () => - todos().every((t) => t.status === "pending") - const finished = () => - todos().every((t) => t.status === "completed") - - return ( -
    -
    - - - -
    -
    -
    -
    -
    - - - - Creating plan - - - Completing plan - - - -
    - 0}> -
      - - {(todo) => ( -
    • - - {todo.content} -
    • - )} -
      -
    -
    -
    - -
    -
    - ) - }} -
    - {/* Fetch tool */} - - {(_part) => { - const url = () => toolData()?.args.url - const format = () => toolData()?.args.format - const hasError = () => - toolData()?.metadata?.error - - return ( -
    -
    - - - -
    -
    -
    -
    -
    - Fetch - {url()} -
    - - -
    - - {formatErrorString( - toolData()?.result, - )} - -
    -
    - -
    - - setShowResults((e) => !e) - } - /> - -
    - -
    -
    -
    -
    -
    -
    - -
    -
    - ) - }} -
    - {/* Tool call */} - - {(part) => { - return ( -
    -
    - - - -
    -
    -
    -
    -
    - {part().toolInvocation.toolName} -
    -
    - - {(arg) => ( - <> -
    -
    {arg[0]}
    -
    {arg[1]}
    - - )} -
    -
    - - -
    - - setShowResults((e) => !e) - } - /> - - - -
    -
    - - - -
    -
    - -
    -
    - ) - }} -
    - {/* Fallback */} - -
    -
    - - - } - > - - - - - - - - - -
    -
    -
    -
    -
    - - {part.type} - -
    - -
    -
    -
    -
    -
    - ) - }} -
    -
    - )} + return + }} +
    +
    + ) + }}
    -

    {getStatusText(connectionStatus())}

    @@ -1934,19 +400,11 @@ export default function Share(props: {
  • Input Tokens - {data().tokens.input ? ( - {data().tokens.input} - ) : ( - - )} + {data().tokens.input ? {data().tokens.input} : }
  • Output Tokens - {data().tokens.output ? ( - {data().tokens.output} - ) : ( - - )} + {data().tokens.output ? {data().tokens.output} : }
  • Reasoning Tokens @@ -1972,10 +430,7 @@ export default function Share(props: { "overflow-y": "auto", }} > - 0} - fallback={

    Waiting for messages...

    } - > + 0} fallback={

    Waiting for messages...

    }>
      {(msg) => ( @@ -2003,9 +458,7 @@ export default function Share(props: {
) } + +export function fromV1(v1: Message.Info): MessageWithParts { + if (v1.role === "assistant") { + return { + id: v1.id, + sessionID: v1.metadata.sessionID, + role: "assistant", + time: { + created: v1.metadata.time.created, + completed: v1.metadata.time.completed, + }, + cost: v1.metadata.assistant!.cost, + path: v1.metadata.assistant!.path, + summary: v1.metadata.assistant!.summary, + tokens: v1.metadata.assistant!.tokens ?? { + input: 0, + output: 0, + cache: { + read: 0, + write: 0, + }, + reasoning: 0, + }, + modelID: v1.metadata.assistant!.modelID, + providerID: v1.metadata.assistant!.providerID, + system: v1.metadata.assistant!.system, + error: v1.metadata.error, + parts: v1.parts.flatMap((part, index): MessageV2.Part[] => { + const base = { + id: index.toString(), + messageID: v1.id, + sessionID: v1.metadata.sessionID, + } + if (part.type === "text") { + return [ + { + ...base, + type: "text", + text: part.text, + }, + ] + } + if (part.type === "step-start") { + return [ + { + ...base, + type: "step-start", + }, + ] + } + if (part.type === "tool-invocation") { + return [ + { + ...base, + type: "tool", + callID: part.toolInvocation.toolCallId, + tool: part.toolInvocation.toolName, + state: (() => { + if (part.toolInvocation.state === "partial-call") { + return { + status: "pending", + } + } + + const { title, time, ...metadata } = v1.metadata.tool[part.toolInvocation.toolCallId] + if (part.toolInvocation.state === "call") { + return { + status: "running", + input: part.toolInvocation.args, + time: { + start: time.start, + }, + } + } + + if (part.toolInvocation.state === "result") { + return { + status: "completed", + input: part.toolInvocation.args, + output: part.toolInvocation.result, + title, + time, + metadata, + } + } + throw new Error("unknown tool invocation state") + })(), + }, + ] + } + return [] + }), + } + } + + if (v1.role === "user") { + return { + id: v1.id, + sessionID: v1.metadata.sessionID, + role: "user", + time: { + created: v1.metadata.time.created, + }, + parts: v1.parts.flatMap((part, index): MessageV2.Part[] => { + const base = { + id: index.toString(), + messageID: v1.id, + sessionID: v1.metadata.sessionID, + } + if (part.type === "text") { + return [ + { + ...base, + type: "text", + text: part.text, + }, + ] + } + if (part.type === "file") { + return [ + { + ...base, + type: "file", + mime: part.mediaType, + filename: part.filename, + url: part.url, + }, + ] + } + return [] + }), + } + } + + throw new Error("unknown message type") +} diff --git a/packages/web/src/components/codeblock.module.css b/packages/web/src/components/codeblock.module.css deleted file mode 100644 index ddd88ef1..00000000 --- a/packages/web/src/components/codeblock.module.css +++ /dev/null @@ -1,11 +0,0 @@ -.codeblock { - pre { - --shiki-dark-bg: var(--sl-color-bg-surface) !important; - background-color: var(--sl-color-bg-surface) !important; - - span { - white-space: break-spaces; - } - } -} - diff --git a/packages/web/src/components/diffview.module.css b/packages/web/src/components/diffview.module.css deleted file mode 100644 index a748c5d0..00000000 --- a/packages/web/src/components/diffview.module.css +++ /dev/null @@ -1,121 +0,0 @@ -.diff { - display: flex; - flex-direction: column; - border: 1px solid var(--sl-color-divider); - background-color: var(--sl-color-bg-surface); - border-radius: 0.25rem; -} - -.desktopView { - display: block; -} - -.mobileView { - display: none; -} - -.mobileBlock { - display: flex; - flex-direction: column; -} - -.row { - display: grid; - grid-template-columns: 1fr 1fr; - align-items: stretch; -} - -.beforeColumn, -.afterColumn { - display: flex; - flex-direction: column; - overflow-x: visible; - min-width: 0; - align-items: stretch; -} - -.beforeColumn { - border-right: 1px solid var(--sl-color-divider); -} - -.diff > .row:first-child [data-section="cell"]:first-child { - padding-top: 0.5rem; -} - -.diff > .row:last-child [data-section="cell"]:last-child { - padding-bottom: 0.5rem; -} - -[data-section="cell"] { - position: relative; - flex: 1; - display: flex; - flex-direction: column; - - width: 100%; - padding: 0.1875rem 0.5rem 0.1875rem 2.2ch; - margin: 0; - - &[data-display-mobile="true"] { - display: none; - } - - pre { - --shiki-dark-bg: var(--sl-color-bg-surface) !important; - background-color: var(--sl-color-bg-surface) !important; - - white-space: pre-wrap; - word-break: break-word; - - code > span:empty::before { - content: "\00a0"; - white-space: pre; - display: inline-block; - width: 0; - } - } -} - -[data-diff-type="removed"] { - background-color: var(--sl-color-red-low); - - pre { - --shiki-dark-bg: var(--sl-color-red-low) !important; - background-color: var(--sl-color-red-low) !important; - } - - &::before { - content: "-"; - position: absolute; - left: 0.5ch; - user-select: none; - color: var(--sl-color-red-high); - } -} - -[data-diff-type="added"] { - background-color: var(--sl-color-green-low); - - pre { - --shiki-dark-bg: var(--sl-color-green-low) !important; - background-color: var(--sl-color-green-low) !important; - } - - &::before { - content: "+"; - position: absolute; - left: 0.6ch; - user-select: none; - color: var(--sl-color-green-high); - } -} - -@media (max-width: 40rem) { - .desktopView { - display: none; - } - - .mobileView { - display: block; - } -} diff --git a/packages/web/src/components/icons/custom.tsx b/packages/web/src/components/icons/custom.tsx index b4e32d0c..ba06ddfb 100644 --- a/packages/web/src/components/icons/custom.tsx +++ b/packages/web/src/components/icons/custom.tsx @@ -39,8 +39,30 @@ export function IconGemini(props: JSX.SvgSVGAttributes) { export function IconOpencode(props: JSX.SvgSVGAttributes) { return ( - + ) } + +// https://icones.js.org/collection/ri?s=meta&icon=ri:meta-fill +export function IconMeta(props: JSX.SvgSVGAttributes) { + return ( + + + + ) +} + +// https://icones.js.org/collection/ri?s=robot&icon=ri:robot-2-line +export function IconRobot(props: JSX.SvgSVGAttributes) { + return ( + + + ) +} diff --git a/packages/web/src/components/icons/index.tsx b/packages/web/src/components/icons/index.tsx index a788d8f4..62445611 100644 --- a/packages/web/src/components/icons/index.tsx +++ b/packages/web/src/components/icons/index.tsx @@ -3,12 +3,7 @@ import { type JSX } from "solid-js" export function IconAcademicCap(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconAdjustmentsHorizontal( - props: JSX.SvgSVGAttributes, -) { +export function IconAdjustmentsHorizontal(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconAdjustmentsVertical( - props: JSX.SvgSVGAttributes, -) { +export function IconAdjustmentsVertical(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconArchiveBoxArrowDown( - props: JSX.SvgSVGAttributes, -) { +export function IconArchiveBoxArrowDown(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconArchiveBoxXMark( - props: JSX.SvgSVGAttributes, -) { +export function IconArchiveBoxXMark(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { ) } -export function IconArrowDownCircle( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowDownCircle(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { ) } -export function IconArrowDownOnSquareStack( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowDownOnSquareStack(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconArrowDownOnSquare( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowDownOnSquare(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconArrowDownTray(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconArrowDown(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconArrowLeftCircle( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowLeftCircle(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconArrowLeftOnRectangle( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowLeftOnRectangle(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconArrowLongDown(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconArrowLongLeft(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconArrowLongRight(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconArrowLongUp(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconArrowPathRoundedSquare( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowPathRoundedSquare(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { ) } -export function IconArrowRightCircle( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowRightCircle(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconArrowRightOnRectangle( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowRightOnRectangle(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconArrowSmallDown(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconArrowSmallLeft(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconArrowSmallRight( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowSmallRight(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { ) } -export function IconArrowTopRightOnSquare( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowTopRightOnSquare(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconArrowTrendingDown( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowTrendingDown(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconArrowTrendingUp( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowTrendingUp(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconArrowUpLeft(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconArrowUpOnSquareStack( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowUpOnSquareStack(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconArrowUpOnSquare( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowUpOnSquare(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconArrowUpTray(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconArrowUp(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconArrowUturnDown(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconArrowUturnLeft(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconArrowUturnRight( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowUturnRight(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { ) } -export function IconArrowsPointingIn( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowsPointingIn(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconArrowsPointingOut( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowsPointingOut(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconArrowsRightLeft( - props: JSX.SvgSVGAttributes, -) { +export function IconArrowsRightLeft(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconAtSymbol(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBackspace(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBackward(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBanknotes(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBars2(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconBars3BottomLeft( - props: JSX.SvgSVGAttributes, -) { +export function IconBars3BottomLeft(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconBars3BottomRight( - props: JSX.SvgSVGAttributes, -) { +export function IconBars3BottomRight(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconBars3CenterLeft( - props: JSX.SvgSVGAttributes, -) { +export function IconBars3CenterLeft(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconBars4(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBarsArrowDown(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBarsArrowUp(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBattery0(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBattery100(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBattery50(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBeaker(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBellAlert(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBellSlash(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBellSnooze(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBell(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBoltSlash(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBolt(props: JSX.SvgSVGAttributes) { return ( - + ) { export function IconBoltSolid(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBookOpen(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBookmarkSlash(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBookmarkSquare(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBookmark(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBriefcase(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconBugAnt(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconBuildingLibrary( - props: JSX.SvgSVGAttributes, -) { +export function IconBuildingLibrary(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconBuildingOffice2( - props: JSX.SvgSVGAttributes, -) { +export function IconBuildingOffice2(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { ) } -export function IconBuildingStorefront( - props: JSX.SvgSVGAttributes, -) { +export function IconBuildingStorefront(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconCalculator(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCalendarDays(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCalendar(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCamera(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconChartBarSquare(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconChartBar(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconChartPie(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconChatBubbleBottomCenterText( - props: JSX.SvgSVGAttributes, -) { +export function IconChatBubbleBottomCenterText(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconChatBubbleBottomCenter( - props: JSX.SvgSVGAttributes, -) { +export function IconChatBubbleBottomCenter(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconChatBubbleLeftEllipsis( - props: JSX.SvgSVGAttributes, -) { +export function IconChatBubbleLeftEllipsis(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconChatBubbleLeftRight( - props: JSX.SvgSVGAttributes, -) { +export function IconChatBubbleLeftRight(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { ) } -export function IconChatBubbleOvalLeftEllipsis( - props: JSX.SvgSVGAttributes, -) { +export function IconChatBubbleOvalLeftEllipsis(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconChatBubbleOvalLeft( - props: JSX.SvgSVGAttributes, -) { +export function IconChatBubbleOvalLeft(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconCheckCircle(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCheck(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconChevronDoubleDown( - props: JSX.SvgSVGAttributes, -) { +export function IconChevronDoubleDown(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconChevronDoubleLeft( - props: JSX.SvgSVGAttributes, -) { +export function IconChevronDoubleLeft(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconChevronDoubleRight( - props: JSX.SvgSVGAttributes, -) { +export function IconChevronDoubleRight(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconChevronDoubleUp( - props: JSX.SvgSVGAttributes, -) { +export function IconChevronDoubleUp(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconChevronLeft(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconChevronRight(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconChevronUpDown(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconChevronUp(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCircleStack(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconClipboardDocumentCheck( - props: JSX.SvgSVGAttributes, -) { +export function IconClipboardDocumentCheck(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconClipboardDocumentList( - props: JSX.SvgSVGAttributes, -) { +export function IconClipboardDocumentList(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconClipboardDocument( - props: JSX.SvgSVGAttributes, -) { +export function IconClipboardDocument(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconClock(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCloudArrowDown(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCloudArrowUp(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCloud(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconCodeBracketSquare( - props: JSX.SvgSVGAttributes, -) { +export function IconCodeBracketSquare(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconCog6Tooth(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCog8Tooth(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCog(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCommandLine(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconComputerDesktop( - props: JSX.SvgSVGAttributes, -) { +export function IconComputerDesktop(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconCreditCard(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconCubeTransparent( - props: JSX.SvgSVGAttributes, -) { +export function IconCubeTransparent(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { ) } -export function IconCurrencyBangladeshi( - props: JSX.SvgSVGAttributes, -) { +export function IconCurrencyBangladeshi(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconCurrencyEuro(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCurrencyPound(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCurrencyRupee(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconCurrencyYen(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconCursorArrowRays( - props: JSX.SvgSVGAttributes, -) { +export function IconCursorArrowRays(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconCursorArrowRipple( - props: JSX.SvgSVGAttributes, -) { +export function IconCursorArrowRipple(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconDevicePhoneMobile( - props: JSX.SvgSVGAttributes, -) { +export function IconDevicePhoneMobile(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { ) } -export function IconDocumentArrowDown( - props: JSX.SvgSVGAttributes, -) { +export function IconDocumentArrowDown(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconDocumentArrowUp( - props: JSX.SvgSVGAttributes, -) { +export function IconDocumentArrowUp(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconDocumentChartBar( - props: JSX.SvgSVGAttributes, -) { +export function IconDocumentChartBar(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { ) } -export function IconDocumentDuplicate( - props: JSX.SvgSVGAttributes, -) { +export function IconDocumentDuplicate(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconDocumentMagnifyingGlass( - props: JSX.SvgSVGAttributes, -) { +export function IconDocumentMagnifyingGlass(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconDocumentPlus(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconDocumentText(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconDocument(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconEllipsisHorizontalCircle( - props: JSX.SvgSVGAttributes, -) { +export function IconEllipsisHorizontalCircle(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconEllipsisHorizontal( - props: JSX.SvgSVGAttributes, -) { +export function IconEllipsisHorizontal(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconEllipsisVertical( - props: JSX.SvgSVGAttributes, -) { +export function IconEllipsisVertical(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconEnvelope(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconEnvelopeSolid(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconExclamationCircle( - props: JSX.SvgSVGAttributes, -) { +export function IconExclamationCircle(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconExclamationTriangle( - props: JSX.SvgSVGAttributes, -) { +export function IconExclamationTriangle(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconEyeSlash(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconEye(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconFaceFrown(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconFaceSmile(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconFilm(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconFingerPrint(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconFire(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconFlag(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconFolderArrowDown( - props: JSX.SvgSVGAttributes, -) { +export function IconFolderArrowDown(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconFolderOpen(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconFolderPlus(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconFolder(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconForward(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconFunnel(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconGif(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconGiftTop(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconGift(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconGlobeAlt(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconGlobeAmericas(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconGlobeAsiaAustralia( - props: JSX.SvgSVGAttributes, -) { +export function IconGlobeAsiaAustralia(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconGlobeEuropeAfrica( - props: JSX.SvgSVGAttributes, -) { +export function IconGlobeEuropeAfrica(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconHandThumbDown(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconHandThumbUp(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconHashtag(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconHeart(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconHomeModern(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconHome(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconIdentification(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconInboxArrowDown(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconInboxStack(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconInbox(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconInformationCircle( - props: JSX.SvgSVGAttributes, -) { +export function IconInformationCircle(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconLanguage(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconLifebuoy(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconLightBulb(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconLink(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconListBullet(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconLockClosed(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconLockOpen(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconMagnifyingGlassCircle( - props: JSX.SvgSVGAttributes, -) { +export function IconMagnifyingGlassCircle(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconMagnifyingGlassMinus( - props: JSX.SvgSVGAttributes, -) { +export function IconMagnifyingGlassMinus(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconMagnifyingGlassPlus( - props: JSX.SvgSVGAttributes, -) { +export function IconMagnifyingGlassPlus(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconMagnifyingGlass( - props: JSX.SvgSVGAttributes, -) { +export function IconMagnifyingGlass(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconMap(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconMegaphone(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconMicrophone(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconMinusCircle(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconMinusSmall(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconMinus(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconMoon(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconMusicalNote(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconNewspaper(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconNoSymbol(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPaintBrush(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPaperAirplane(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPaperClip(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPauseCircle(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPause(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPencilSquare(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPencil(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconPhoneArrowDownLeft( - props: JSX.SvgSVGAttributes, -) { +export function IconPhoneArrowDownLeft(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconPhoneArrowUpRight( - props: JSX.SvgSVGAttributes, -) { +export function IconPhoneArrowUpRight(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconPhone(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPhoto(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPlayCircle(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPlayPause(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPlay(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPlusCircle(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPlusSmall(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPlus(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconPower(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconPresentationChartBar( - props: JSX.SvgSVGAttributes, -) { +export function IconPresentationChartBar(props: JSX.SvgSVGAttributes) { return ( - + ) } -export function IconPresentationChartLine( - props: JSX.SvgSVGAttributes, -) { +export function IconPresentationChartLine(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconPuzzlePiece(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconQrCode(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconQuestionMarkCircle( - props: JSX.SvgSVGAttributes, -) { +export function IconQuestionMarkCircle(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconRadio(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconReceiptPercent(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconReceiptRefund(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconRectangleGroup(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconRectangleStack(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconRocketLaunch(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconRss(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconScale(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconScissors(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconServerStack(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconServer(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconShare(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconShieldCheck(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconShieldExclamation( - props: JSX.SvgSVGAttributes, -) { +export function IconShieldExclamation(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconShoppingCart(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSignalSlash(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSignal(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSparkles(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSpeakerWave(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSpeakerXMark(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSquare2Stack(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSquare3Stack3d(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSquares2x2(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSquaresPlus(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconStar(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconStopCircle(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconStop(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSun(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSwatch(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconTableCells(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconTag(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconTicket(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconTrash(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconTrophy(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconTruck(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconTv(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconUserCircle(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconUserGroup(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconUserMinus(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconUserPlus(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconUser(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconUsers(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconVariable(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconVideoCameraSlash( - props: JSX.SvgSVGAttributes, -) { +export function IconVideoCameraSlash(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconViewColumns(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconViewfinderCircle( - props: JSX.SvgSVGAttributes, -) { +export function IconViewfinderCircle(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconWifi(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconWindow(props: JSX.SvgSVGAttributes) { return ( - + ) { ) } -export function IconWrenchScrewdriver( - props: JSX.SvgSVGAttributes, -) { +export function IconWrenchScrewdriver(props: JSX.SvgSVGAttributes) { return ( - + ) { return ( - + ) { } export function IconXCircle(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconXMark(props: JSX.SvgSVGAttributes) { return ( - + ) { // index export function IconCommand(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconLetter(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconMultiSelect(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSettings(props: JSX.SvgSVGAttributes) { return ( - + ) { } export function IconSingleSelect(props: JSX.SvgSVGAttributes) { return ( - + *:last-child { - margin-bottom: 0; - } - - pre { - --shiki-dark-bg: var(--sl-color-bg-surface) !important; - background-color: var(--sl-color-bg-surface) !important; - padding: 0.5rem 0.75rem; - line-height: 1.6; - font-size: 0.75rem; - white-space: pre-wrap; - word-break: break-word; - - span { - white-space: break-spaces; - } - } - - code { - font-weight: 500; - - &:not(pre code) { - &::before { - content: "`"; - font-weight: 700; - } - &::after { - content: "`"; - font-weight: 700; - } - } - } - - table { - border-collapse: collapse; - width: 100%; - } - - th, - td { - border: 1px solid var(--sl-color-border); - padding: 0.5rem 0.75rem; - text-align: left; - } - - th { - border-bottom: 1px solid var(--sl-color-border); - } - - /* Remove outer borders */ - table tr:first-child th, - table tr:first-child td { - border-top: none; - } - - table tr:last-child td { - border-bottom: none; - } - - table th:first-child, - table td:first-child { - border-left: none; - } - - table th:last-child, - table td:last-child { - border-right: none; - } -} diff --git a/packages/web/src/components/share.module.css b/packages/web/src/components/share.module.css index c339d2b8..9930e6b5 100644 --- a/packages/web/src/components/share.module.css +++ b/packages/web/src/components/share.module.css @@ -15,118 +15,106 @@ --lg-tool-width: 56rem; --term-icon: url("data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%2060%2016'%20preserveAspectRatio%3D'xMidYMid%20meet'%3E%3Ccircle%20cx%3D'8'%20cy%3D'8'%20r%3D'8'%2F%3E%3Ccircle%20cx%3D'30'%20cy%3D'8'%20r%3D'8'%2F%3E%3Ccircle%20cx%3D'52'%20cy%3D'8'%20r%3D'8'%2F%3E%3C%2Fsvg%3E"); -} -[data-element-button-text] { - cursor: pointer; - appearance: none; - background-color: transparent; - border: none; - padding: 0; - color: var(--sl-color-text-secondary); - - &:hover { - color: var(--sl-color-text); - } - - &[data-element-button-more] { + [data-component="header"] { display: flex; - align-items: center; - gap: 0.125rem; - - span[data-button-icon] { - line-height: 1; - opacity: 0.85; - svg { - display: block; - } - } - } -} - -[data-element-label] { - text-transform: uppercase; - letter-spacing: -0.5px; - color: var(--sl-color-text-dimmed); -} - -.header { - display: flex; - flex-direction: column; - gap: 1rem; - - @media (max-width: 30rem) { + flex-direction: column; gap: 1rem; - } - [data-section="title"] { - h1 { - font-size: 2.75rem; - font-weight: 500; - line-height: 1.2; - letter-spacing: -0.05em; - display: -webkit-box; - -webkit-box-orient: vertical; - -webkit-line-clamp: 3; - overflow: hidden; - - @media (max-width: 30rem) { - font-size: 1.75rem; - line-height: 1.25; - -webkit-line-clamp: 3; - } + @media (max-width: 30rem) { + gap: 1rem; } } - [data-section="row"] { + [data-component="header-title"] { + font-size: 2.75rem; + font-weight: 500; + line-height: 1.2; + letter-spacing: -0.05em; + display: -webkit-box; + -webkit-box-orient: vertical; + -webkit-line-clamp: 3; + line-clamp: 3; + overflow: hidden; + + @media (max-width: 30rem) { + font-size: 1.75rem; + line-height: 1.25; + -webkit-line-clamp: 3; + } + } + + [data-component="header-details"] { display: flex; flex-direction: column; gap: 0.5rem; } - [data-section="stats"] { + [data-component="header-stats"] { list-style-type: none; padding: 0; margin: 0; display: flex; gap: 0.5rem 0.875rem; flex-wrap: wrap; + max-width: var(--lg-tool-width); - li { + [data-slot="item"] { display: flex; align-items: center; - gap: 0.5rem; + gap: 0.3125rem; font-size: 0.875rem; span[data-placeholder] { color: var(--sl-color-text-dimmed); } } + + [data-slot="icon"] { + flex: 0 0 auto; + color: var(--sl-color-text-dimmed); + opacity: 0.85; + + svg { + display: block; + } + } + + [data-slot="model"] { + color: var(--sl-color-text); + } } - [data-section="stats"] { - li { - gap: 0.3125rem; + [data-component="header-time"] { + color: var(--sl-color-text-dimmed); + font-size: 0.875rem; + } - [data-stat-icon] { - flex: 0 0 auto; - color: var(--sl-color-text-dimmed); + [data-component="text-button"] { + cursor: pointer; + appearance: none; + background-color: transparent; + border: none; + padding: 0; + color: var(--sl-color-text-secondary); + + &:hover { + color: var(--sl-color-text); + } + + &[data-element-button-more] { + display: flex; + align-items: center; + gap: 0.125rem; + + span[data-button-icon] { + line-height: 1; opacity: 0.85; + svg { display: block; } } - - span[data-stat-model] { - color: var(--sl-color-text); - } - } - } - - [data-section="time"] { - span { - color: var(--sl-color-text-dimmed); - font-size: 0.875rem; } } } @@ -170,10 +158,12 @@ svg:nth-child(3) { display: none; } + &:hover { svg:nth-child(1) { display: none; } + svg:nth-child(2) { display: block; } @@ -213,12 +203,14 @@ opacity: 1; visibility: visible; } + a, a:hover { svg:nth-child(1), svg:nth-child(2) { display: none; } + svg:nth-child(3) { display: block; } @@ -264,7 +256,7 @@ } b { - color: var(--sl-color-text); + color: var(--sl-color-text); word-break: break-all; font-weight: 500; } @@ -348,8 +340,7 @@ } [data-part-type="tool-grep"] { - &:not(:has([data-part-tool-args])) - > [data-section="content"] > [data-part-tool-body] { + &:not(:has([data-part-tool-args])) > [data-section="content"] > [data-part-tool-body] { gap: 0.5rem; } } @@ -374,6 +365,7 @@ } } } + [data-part-type="summary"] { & > [data-section="decoration"] { span:first-child { @@ -388,15 +380,19 @@ &[data-status="connected"] { background-color: var(--sl-color-green); } + &[data-status="connecting"] { background-color: var(--sl-color-orange); } + &[data-status="disconnected"] { background-color: var(--sl-color-divider); } + &[data-status="reconnecting"] { background-color: var(--sl-color-orange); } + &[data-status="error"] { background-color: var(--sl-color-red); } @@ -493,14 +489,20 @@ } } - &[data-background="none"] { background-color: transparent; } - &[data-background="blue"] { background-color: var(--sl-color-blue-low); } + &[data-background="none"] { + background-color: transparent; + } + + &[data-background="blue"] { + background-color: var(--sl-color-blue-low); + } &[data-expanded="true"] { pre { display: block; } } + &[data-expanded="false"] { pre { display: -webkit-box; @@ -536,20 +538,25 @@ span { margin-right: 0.25rem; + &:last-child { margin-right: 0; } } + span[data-color="red"] { color: var(--sl-color-red); } + span[data-color="dimmed"] { color: var(--sl-color-text-dimmed); } + span[data-marker="label"] { text-transform: uppercase; letter-spacing: -0.5px; } + span[data-separator] { margin-right: 0.375rem; } @@ -561,6 +568,7 @@ display: block; } } + &[data-expanded="false"] { [data-section="content"] { display: -webkit-box; @@ -575,7 +583,6 @@ padding: 2px 0; font-size: 0.75rem; } - } .message-terminal { @@ -611,7 +618,7 @@ } &::before { - content: ''; + content: ""; position: absolute; pointer-events: none; top: 8px; @@ -651,6 +658,7 @@ display: block; } } + &[data-expanded="false"] { pre { display: -webkit-box; @@ -693,6 +701,7 @@ display: block; } } + &[data-expanded="false"] { [data-element-markdown] { display: -webkit-box; @@ -750,10 +759,14 @@ &[data-status="pending"] { color: var(--sl-color-text); } + &[data-status="in_progress"] { color: var(--sl-color-text); - & > span { border-color: var(--sl-color-orange); } + & > span { + border-color: var(--sl-color-orange); + } + & > span::before { content: ""; position: absolute; @@ -764,10 +777,14 @@ box-shadow: inset 1rem 1rem var(--sl-color-orange-low); } } + &[data-status="completed"] { color: var(--sl-color-text-secondary); - & > span { border-color: var(--sl-color-green-low); } + & > span { + border-color: var(--sl-color-green-low); + } + & > span::before { content: ""; position: absolute; @@ -798,7 +815,9 @@ display: flex; align-items: center; justify-content: center; - transition: all 0.15s ease, opacity 0.5s ease; + transition: + all 0.15s ease, + opacity 0.5s ease; z-index: 100; appearance: none; opacity: 1; diff --git a/packages/web/src/components/share/common.tsx b/packages/web/src/components/share/common.tsx new file mode 100644 index 00000000..cab2dbdb --- /dev/null +++ b/packages/web/src/components/share/common.tsx @@ -0,0 +1,77 @@ +import { createSignal, onCleanup, splitProps } from "solid-js" +import type { JSX } from "solid-js/jsx-runtime" +import { IconCheckCircle, IconHashtag } from "../icons" + +interface AnchorProps extends JSX.HTMLAttributes { + id: string +} +export function AnchorIcon(props: AnchorProps) { + const [local, rest] = splitProps(props, ["id", "children"]) + const [copied, setCopied] = createSignal(false) + + return ( + + ) +} + +export function createOverflow() { + const [overflow, setOverflow] = createSignal(false) + return { + get status() { + return overflow() + }, + ref(el: HTMLElement) { + const ro = new ResizeObserver(() => { + if (el.scrollHeight > el.clientHeight + 1) { + setOverflow(true) + } + return + }) + ro.observe(el) + + onCleanup(() => { + ro.disconnect() + }) + }, + } +} + +export function formatDuration(ms: number): string { + const ONE_SECOND = 1000 + const ONE_MINUTE = 60 * ONE_SECOND + + if (ms >= ONE_MINUTE) { + const minutes = Math.floor(ms / ONE_MINUTE) + return minutes === 1 ? `1min` : `${minutes}mins` + } + + if (ms >= ONE_SECOND) { + const seconds = Math.floor(ms / ONE_SECOND) + return `${seconds}s` + } + + return `${ms}ms` +} diff --git a/packages/web/src/components/share/content-bash.module.css b/packages/web/src/components/share/content-bash.module.css new file mode 100644 index 00000000..0915282d --- /dev/null +++ b/packages/web/src/components/share/content-bash.module.css @@ -0,0 +1,85 @@ +.root { + display: contents; + + [data-slot="expand-button"] { + flex: 0 0 auto; + padding: 2px 0; + font-size: 0.75rem; + } + + [data-slot="body"] { + border: 1px solid var(--sl-color-divider); + border-radius: 0.25rem; + overflow: hidden; + width: 100%; + } + + [data-slot="header"] { + position: relative; + border-bottom: 1px solid var(--sl-color-divider); + width: 100%; + height: 1.625rem; + text-align: center; + padding: 0 3.25rem; + + > span { + max-width: min(100%, 140ch); + display: inline-block; + white-space: nowrap; + overflow: hidden; + line-height: 1.625rem; + font-size: 0.75rem; + text-overflow: ellipsis; + color: var(--sl-color-text-dimmed); + } + + &::before { + content: ""; + position: absolute; + pointer-events: none; + top: 8px; + left: 10px; + width: 2rem; + height: 0.5rem; + line-height: 0; + background-color: var(--sl-color-hairline); + mask-image: var(--term-icon); + mask-repeat: no-repeat; + } + } + + [data-slot="content"] { + display: flex; + flex-direction: column; + padding: 0.5rem calc(0.5rem + 3px); + + pre { + --shiki-dark-bg: var(--sl-color-bg) !important; + background-color: var(--sl-color-bg) !important; + line-height: 1.6; + font-size: 0.75rem; + white-space: pre-wrap; + word-break: break-word; + margin: 0; + + span { + white-space: break-spaces; + } + } + } + + [data-slot="output"] { + display: -webkit-box; + -webkit-box-orient: vertical; + -webkit-line-clamp: 10; + line-clamp: 10; + overflow: hidden; + } + + &[data-expanded] [data-slot="output"] { + display: block; + -webkit-line-clamp: none; + line-clamp: none; + overflow: visible; + } +} diff --git a/packages/web/src/components/share/content-bash.tsx b/packages/web/src/components/share/content-bash.tsx new file mode 100644 index 00000000..5ccd95c0 --- /dev/null +++ b/packages/web/src/components/share/content-bash.tsx @@ -0,0 +1,67 @@ +import style from "./content-bash.module.css" +import { createResource, createSignal } from "solid-js" +import { createOverflow } from "./common" +import { codeToHtml } from "shiki" + +interface Props { + command: string + output: string + description?: string + expand?: boolean +} + +export function ContentBash(props: Props) { + const [commandHtml] = createResource( + () => props.command, + async (command) => { + return codeToHtml(command || "", { + lang: "bash", + themes: { + light: "github-light", + dark: "github-dark", + }, + }) + }, + ) + + const [outputHtml] = createResource( + () => props.output, + async (output) => { + return codeToHtml(output || "", { + lang: "console", + themes: { + light: "github-light", + dark: "github-dark", + }, + }) + }, + ) + + const [expanded, setExpanded] = createSignal(false) + const overflow = createOverflow() + + return ( +
+
+
+ {props.description} +
+
+
+
+
+
+ + {!props.expand && overflow.status && ( + + )} +
+ ) +} diff --git a/packages/web/src/components/share/content-code.module.css b/packages/web/src/components/share/content-code.module.css new file mode 100644 index 00000000..ec159d64 --- /dev/null +++ b/packages/web/src/components/share/content-code.module.css @@ -0,0 +1,26 @@ +.root { + border: 1px solid var(--sl-color-divider); + background-color: var(--sl-color-bg-surface); + border-radius: 0.25rem; + padding: 0.5rem calc(0.5rem + 3px); + + &[data-flush="true"] { + border: none; + background-color: transparent; + padding: 0; + border-radius: 0; + } + + pre { + --shiki-dark-bg: var(--sl-color-bg-surface) !important; + background-color: var(--sl-color-bg-surface) !important; + line-height: 1.6; + font-size: 0.75rem; + white-space: pre-wrap; + word-break: break-word; + + span { + white-space: break-spaces; + } + } +} diff --git a/packages/web/src/components/share/content-code.tsx b/packages/web/src/components/share/content-code.tsx new file mode 100644 index 00000000..2f383b8b --- /dev/null +++ b/packages/web/src/components/share/content-code.tsx @@ -0,0 +1,32 @@ +import { codeToHtml, bundledLanguages } from "shiki" +import { createResource, Suspense } from "solid-js" +import { transformerNotationDiff } from "@shikijs/transformers" +import style from "./content-code.module.css" + +interface Props { + code: string + lang?: string + flush?: boolean +} +export function ContentCode(props: Props) { + const [html] = createResource( + () => [props.code, props.lang], + async ([code, lang]) => { + // TODO: For testing delays + // await new Promise((resolve) => setTimeout(resolve, 3000)) + return (await codeToHtml(code || "", { + lang: lang && lang in bundledLanguages ? lang : "text", + themes: { + light: "github-light", + dark: "github-dark", + }, + transformers: [transformerNotationDiff()], + })) as string + }, + ) + return ( + +
+ + ) +} diff --git a/packages/web/src/components/share/content-diff.module.css b/packages/web/src/components/share/content-diff.module.css new file mode 100644 index 00000000..5bf6e224 --- /dev/null +++ b/packages/web/src/components/share/content-diff.module.css @@ -0,0 +1,154 @@ +.root { + display: flex; + flex-direction: column; + border: 1px solid var(--sl-color-divider); + background-color: var(--sl-color-bg-surface); + border-radius: 0.25rem; + + [data-component="desktop"] { + display: block; + } + + [data-component="mobile"] { + display: none; + } + + [data-component="diff-block"] { + display: flex; + flex-direction: column; + } + + [data-component="diff-row"] { + display: grid; + grid-template-columns: 1fr 1fr; + align-items: stretch; + + &:first-child { + [data-slot="before"], + [data-slot="after"] { + padding-top: 0.25rem; + } + } + + &:last-child { + [data-slot="before"], + [data-slot="after"] { + padding-bottom: 0.25rem; + } + } + + [data-slot="before"], + [data-slot="after"] { + position: relative; + display: flex; + flex-direction: column; + overflow-x: visible; + min-width: 0; + align-items: stretch; + padding: 0 1rem 0 2.2ch; + + &[data-diff-type="removed"] { + background-color: var(--sl-color-red-low); + + pre { + --shiki-dark-bg: var(--sl-color-red-low) !important; + background-color: var(--sl-color-red-low) !important; + } + + &::before { + content: "-"; + position: absolute; + left: 0.6ch; + top: 1px; + user-select: none; + color: var(--sl-color-red-high); + } + } + + &[data-diff-type="added"] { + background-color: var(--sl-color-green-low); + + pre { + --shiki-dark-bg: var(--sl-color-green-low) !important; + background-color: var(--sl-color-green-low) !important; + } + + &::before { + content: "+"; + position: absolute; + user-select: none; + color: var(--sl-color-green-high); + left: 0.6ch; + top: 1px; + } + } + } + + [data-slot="before"] { + border-right: 1px solid var(--sl-color-divider); + } + } + + [data-component="mobile"] { + + & > [data-component="diff-block"]:first-child > div { + padding-top: 0.25rem; + } + + & > [data-component="diff-block"]:last-child > div { + padding-bottom: 0.25rem; + } + + & > [data-component="diff-block"] > div { + padding: 0 1rem 0 2.2ch; + + &[data-diff-type="removed"] { + position: relative; + background-color: var(--sl-color-red-low); + + pre { + --shiki-dark-bg: var(--sl-color-red-low) !important; + background-color: var(--sl-color-red-low) !important; + } + + &::before { + content: "-"; + position: absolute; + left: 0.6ch; + top: 1px; + user-select: none; + color: var(--sl-color-red-high); + } + } + + &[data-diff-type="added"] { + position: relative; + background-color: var(--sl-color-green-low); + + pre { + --shiki-dark-bg: var(--sl-color-green-low) !important; + background-color: var(--sl-color-green-low) !important; + } + + &::before { + content: "+"; + position: absolute; + left: 0.6ch; + top: 1px; + user-select: none; + color: var(--sl-color-green-high); + } + } + } + } + + @media (max-width: 40rem) { + [data-component="desktop"] { + display: none; + } + + [data-component="mobile"] { + display: block; + } + } +} diff --git a/packages/web/src/components/DiffView.tsx b/packages/web/src/components/share/content-diff.tsx similarity index 64% rename from packages/web/src/components/DiffView.tsx rename to packages/web/src/components/share/content-diff.tsx index 66dd7f0f..45249e0c 100644 --- a/packages/web/src/components/DiffView.tsx +++ b/packages/web/src/components/share/content-diff.tsx @@ -1,7 +1,7 @@ -import { type Component, createMemo } from "solid-js" import { parsePatch } from "diff" -import CodeBlock from "./CodeBlock" -import styles from "./diffview.module.css" +import { createMemo } from "solid-js" +import { ContentCode } from "./content-code" +import styles from "./content-diff.module.css" type DiffRow = { left: string @@ -9,14 +9,12 @@ type DiffRow = { type: "added" | "removed" | "unchanged" | "modified" } -interface DiffViewProps { +interface Props { diff: string lang?: string - class?: string } -const DiffView: Component = (props) => { - +export function ContentDiff(props: Props) { const rows = createMemo(() => { const diffRows: DiffRow[] = [] @@ -33,20 +31,20 @@ const DiffView: Component = (props) => { const content = line.slice(1) const prefix = line[0] - if (prefix === '-') { + if (prefix === "-") { // Look ahead for consecutive additions to pair with removals const removals: string[] = [content] let j = i + 1 // Collect all consecutive removals - while (j < lines.length && lines[j][0] === '-') { + while (j < lines.length && lines[j][0] === "-") { removals.push(lines[j].slice(1)) j++ } // Collect all consecutive additions that follow const additions: string[] = [] - while (j < lines.length && lines[j][0] === '+') { + while (j < lines.length && lines[j][0] === "+") { additions.push(lines[j].slice(1)) j++ } @@ -62,39 +60,39 @@ const DiffView: Component = (props) => { diffRows.push({ left: removals[k], right: additions[k], - type: "modified" + type: "modified", }) } else if (hasLeft) { // Pure removal diffRows.push({ left: removals[k], right: "", - type: "removed" + type: "removed", }) } else if (hasRight) { // Pure addition - only create if we actually have content diffRows.push({ left: "", right: additions[k], - type: "added" + type: "added", }) } } i = j - } else if (prefix === '+') { + } else if (prefix === "+") { // Standalone addition (not paired with removal) diffRows.push({ left: "", right: content, - type: "added" + type: "added", }) i++ - } else if (prefix === ' ') { + } else if (prefix === " ") { diffRows.push({ - left: content, - right: content, - type: "unchanged" + left: content === "" ? " " : content, + right: content === "" ? " " : content, + type: "unchanged", }) i++ } else { @@ -112,7 +110,7 @@ const DiffView: Component = (props) => { }) const mobileRows = createMemo(() => { - const mobileBlocks: { type: 'removed' | 'added' | 'unchanged', lines: string[] }[] = [] + const mobileBlocks: { type: "removed" | "added" | "unchanged"; lines: string[] }[] = [] const currentRows = rows() let i = 0 @@ -121,15 +119,15 @@ const DiffView: Component = (props) => { const addedLines: string[] = [] // Collect consecutive modified/removed/added rows - while (i < currentRows.length && - (currentRows[i].type === 'modified' || - currentRows[i].type === 'removed' || - currentRows[i].type === 'added')) { + while ( + i < currentRows.length && + (currentRows[i].type === "modified" || currentRows[i].type === "removed" || currentRows[i].type === "added") + ) { const row = currentRows[i] - if (row.left && (row.type === 'removed' || row.type === 'modified')) { + if (row.left && (row.type === "removed" || row.type === "modified")) { removedLines.push(row.left) } - if (row.right && (row.type === 'added' || row.type === 'modified')) { + if (row.right && (row.type === "added" || row.type === "modified")) { addedLines.push(row.right) } i++ @@ -137,17 +135,17 @@ const DiffView: Component = (props) => { // Add grouped blocks if (removedLines.length > 0) { - mobileBlocks.push({ type: 'removed', lines: removedLines }) + mobileBlocks.push({ type: "removed", lines: removedLines }) } if (addedLines.length > 0) { - mobileBlocks.push({ type: 'added', lines: addedLines }) + mobileBlocks.push({ type: "added", lines: addedLines }) } // Add unchanged rows as-is - if (i < currentRows.length && currentRows[i].type === 'unchanged') { + if (i < currentRows.length && currentRows[i].type === "unchanged") { mobileBlocks.push({ - type: 'unchanged', - lines: [currentRows[i].left] + type: "unchanged", + lines: [currentRows[i].left], }) i++ } @@ -157,41 +155,27 @@ const DiffView: Component = (props) => { }) return ( -
-
+
+
{rows().map((r) => ( -
-
- +
+
+
-
- +
+
))}
-
+
{mobileRows().map((block) => ( -
+
{block.lines.map((line) => ( - +
+ +
))}
))} @@ -200,8 +184,6 @@ const DiffView: Component = (props) => { ) } -export default DiffView - // const testDiff = `--- combined_before.txt 2025-06-24 16:38:08 // +++ combined_after.txt 2025-06-24 16:38:12 // @@ -1,21 +1,25 @@ @@ -210,12 +192,12 @@ export default DiffView // -old content // +added line // +new content -// +// // -removed empty line below // +added empty line above -// +// // - tab indented -// -trailing spaces +// -trailing spaces // -very long line that will definitely wrap in most editors and cause potential alignment issues when displayed in a two column diff view // -unicode content: 🚀 ✨ 中文 // -mixed content with tabs and spaces @@ -226,14 +208,14 @@ export default DiffView // +different unicode: 🎉 💻 日本語 // +normalized content with consistent spacing // +newline to content -// +// // -content to remove -// -whitespace only: +// -whitespace only: // -multiple // -consecutive // -deletions // -single deletion -// + +// + // +single addition // +first addition // +second addition diff --git a/packages/web/src/components/share/content-error.module.css b/packages/web/src/components/share/content-error.module.css new file mode 100644 index 00000000..6303be63 --- /dev/null +++ b/packages/web/src/components/share/content-error.module.css @@ -0,0 +1,65 @@ +.root { + background-color: var(--sl-color-bg-surface); + padding: 0.5rem calc(0.5rem + 3px); + border-radius: 0.25rem; + display: flex; + flex-direction: column; + align-items: flex-start; + gap: 1rem; + align-self: flex-start; + + [data-section="content"] { + pre { + margin-bottom: 0.5rem; + line-height: 1.5; + font-size: 0.75rem; + white-space: pre-wrap; + word-break: break-word; + + &:last-child { + margin-bottom: 0; + } + + span { + margin-right: 0.25rem; + &:last-child { + margin-right: 0; + } + } + span[data-color="red"] { + color: var(--sl-color-red); + } + span[data-color="dimmed"] { + color: var(--sl-color-text-dimmed); + } + span[data-marker="label"] { + text-transform: uppercase; + letter-spacing: -0.5px; + } + span[data-separator] { + margin-right: 0.375rem; + } + } + } + + &[data-expanded="true"] { + [data-section="content"] { + display: block; + } + } + &[data-expanded="false"] { + [data-section="content"] { + display: -webkit-box; + -webkit-box-orient: vertical; + -webkit-line-clamp: 7; + overflow: hidden; + } + } + + button { + flex: 0 0 auto; + padding: 2px 0; + font-size: 0.75rem; + } + +} diff --git a/packages/web/src/components/share/content-error.tsx b/packages/web/src/components/share/content-error.tsx new file mode 100644 index 00000000..b6d7023b --- /dev/null +++ b/packages/web/src/components/share/content-error.tsx @@ -0,0 +1,31 @@ +import style from "./content-error.module.css" +import { type JSX, createSignal } from "solid-js" +import { createOverflow } from "./common" + +interface Props extends JSX.HTMLAttributes { + expand?: boolean +} +export function ContentError(props: Props) { + const [expanded, setExpanded] = createSignal(false) + const overflow = createOverflow() + + return ( +
+
+ {props.children} +
+ {((!props.expand && overflow.status) || expanded()) && ( + + )} +
+ ) +} diff --git a/packages/web/src/components/share/content-markdown.module.css b/packages/web/src/components/share/content-markdown.module.css new file mode 100644 index 00000000..3e38ddf0 --- /dev/null +++ b/packages/web/src/components/share/content-markdown.module.css @@ -0,0 +1,131 @@ +.root { + display: flex; + flex-direction: column; + align-items: flex-start; + gap: 1rem; + + [data-slot="expand-button"] { + flex: 0 0 auto; + padding: 2px 0; + font-size: 0.857em; + } + + [data-slot="markdown"] { + display: -webkit-box; + -webkit-box-orient: vertical; + -webkit-line-clamp: 3; + line-clamp: 3; + overflow: hidden; + + [data-expanded] & { + display: block; + } + + font-size: 1em; + line-height: 1.5; + + p, + blockquote, + ul, + ol, + dl, + table, + pre { + margin-bottom: 1rem; + } + + strong { + font-weight: 600; + } + + ol { + list-style-position: inside; + padding-left: 0.75rem; + } + + ul { + padding-left: 1.5rem; + } + + h1, + h2, + h3, + h4, + h5, + h6 { + font-size: 1em; + font-weight: 600; + margin-bottom: 0.5rem; + } + + & > *:last-child { + margin-bottom: 0; + } + + pre { + --shiki-dark-bg: var(--sl-color-bg-surface) !important; + background-color: var(--sl-color-bg-surface) !important; + padding: 0.5rem 0.75rem; + line-height: 1.6; + font-size: 0.857em; + white-space: pre-wrap; + word-break: break-word; + + span { + white-space: break-spaces; + } + } + + code { + font-weight: 500; + + &:not(pre code) { + &::before { + content: "`"; + font-weight: 700; + } + + &::after { + content: "`"; + font-weight: 700; + } + } + } + + table { + border-collapse: collapse; + width: 100%; + } + + th, + td { + border: 1px solid var(--sl-color-border); + padding: 0.5rem 0.75rem; + text-align: left; + } + + th { + border-bottom: 1px solid var(--sl-color-border); + } + + /* Remove outer borders */ + table tr:first-child th, + table tr:first-child td { + border-top: none; + } + + table tr:last-child td { + border-bottom: none; + } + + table th:first-child, + table td:first-child { + border-left: none; + } + + table th:last-child, + table td:last-child { + border-right: none; + } + } +} diff --git a/packages/web/src/components/share/content-markdown.tsx b/packages/web/src/components/share/content-markdown.tsx new file mode 100644 index 00000000..69cde82b --- /dev/null +++ b/packages/web/src/components/share/content-markdown.tsx @@ -0,0 +1,67 @@ +import { marked } from "marked" +import { codeToHtml } from "shiki" +import markedShiki from "marked-shiki" +import { createOverflow } from "./common" +import { CopyButton } from "./copy-button" +import { createResource, createSignal } from "solid-js" +import { transformerNotationDiff } from "@shikijs/transformers" +import style from "./content-markdown.module.css" + +const markedWithShiki = marked.use( + markedShiki({ + highlight(code, lang) { + return codeToHtml(code, { + lang: lang || "text", + themes: { + light: "github-light", + dark: "github-dark", + }, + transformers: [transformerNotationDiff()], + }) + }, + }), +) + +interface Props { + text: string + expand?: boolean + highlight?: boolean +} +export function ContentMarkdown(props: Props) { + const [html] = createResource( + () => strip(props.text), + async (markdown) => { + return markedWithShiki.parse(markdown) + }, + ) + const [expanded, setExpanded] = createSignal(false) + const overflow = createOverflow() + + return ( +
+
+ + {!props.expand && overflow.status && ( + + )} + +
+ ) +} + +function strip(text: string): string { + const wrappedRe = /^\s*<([A-Za-z]\w*)>\s*([\s\S]*?)\s*<\/\1>\s*$/ + const match = text.match(wrappedRe) + return match ? match[2] : text +} diff --git a/packages/web/src/components/share/content-text.module.css b/packages/web/src/components/share/content-text.module.css new file mode 100644 index 00000000..a3842275 --- /dev/null +++ b/packages/web/src/components/share/content-text.module.css @@ -0,0 +1,57 @@ +.root { + color: var(--sl-color-text); + background-color: var(--sl-color-bg-surface); + padding: 0.5rem calc(0.5rem + 3px); + padding-right: calc(1rem + 18px); + border-radius: 0.25rem; + display: flex; + flex-direction: column; + align-items: flex-start; + gap: 1rem; + align-self: flex-start; + font-size: 0.875rem; + + &[data-compact] { + font-size: 0.75rem; + color: var(--sl-color-text-dimmed); + } + + [data-slot="text"] { + line-height: 1.5; + white-space: pre-wrap; + overflow-wrap: anywhere; + display: -webkit-box; + -webkit-box-orient: vertical; + -webkit-line-clamp: 3; + line-clamp: 3; + overflow: hidden; + + [data-expanded] & { + display: block; + } + } + + [data-slot="expand-button"] { + flex: 0 0 auto; + padding: 2px 0; + font-size: 0.75rem; + } + + &[data-theme="invert"] { + background-color: var(--sl-color-blue-high); + color: var(--sl-color-text-invert); + + [data-slot="expand-button"] { + opacity: 0.85; + color: var(--sl-color-text-invert); + + &:hover { + opacity: 1; + } + } + } + + &[data-theme="blue"] { + background-color: var(--sl-color-blue-low); + } +} diff --git a/packages/web/src/components/share/content-text.tsx b/packages/web/src/components/share/content-text.tsx new file mode 100644 index 00000000..c52e0dfc --- /dev/null +++ b/packages/web/src/components/share/content-text.tsx @@ -0,0 +1,35 @@ +import style from "./content-text.module.css" +import { createSignal } from "solid-js" +import { createOverflow } from "./common" + +interface Props { + text: string + expand?: boolean + compact?: boolean +} +export function ContentText(props: Props) { + const [expanded, setExpanded] = createSignal(false) + const overflow = createOverflow() + + return ( +
+
+        {props.text}
+      
+ {((!props.expand && overflow.status) || expanded()) && ( + + )} +
+ ) +} diff --git a/packages/web/src/components/share/copy-button.module.css b/packages/web/src/components/share/copy-button.module.css new file mode 100644 index 00000000..9da67a1b --- /dev/null +++ b/packages/web/src/components/share/copy-button.module.css @@ -0,0 +1,31 @@ +.root { + position: absolute; + opacity: 0; + visibility: hidden; + transition: opacity 0.15s ease; + + button { + cursor: pointer; + background: none; + border: none; + padding: 0.125rem; + background-color: var(--sl-color-bg); + color: var(--sl-color-text-secondary); + + svg { + display: block; + width: 1rem; + height: 1rem; + } + + &[data-copied="true"] { + color: var(--sl-color-green-high); + } + } +} + +/* Show copy button when parent is hovered */ +*:hover > .root { + opacity: 1; + visibility: visible; +} diff --git a/packages/web/src/components/share/copy-button.tsx b/packages/web/src/components/share/copy-button.tsx new file mode 100644 index 00000000..ad2e83b2 --- /dev/null +++ b/packages/web/src/components/share/copy-button.tsx @@ -0,0 +1,36 @@ +import { createSignal } from "solid-js" +import { IconClipboard, IconCheckCircle } from "../icons" +import styles from "./copy-button.module.css" + +interface CopyButtonProps { + text: string +} + +export function CopyButton(props: CopyButtonProps) { + const [copied, setCopied] = createSignal(false) + + function handleCopyClick() { + if (props.text) { + navigator.clipboard.writeText(props.text) + .catch((err) => console.error("Copy failed", err)) + + setCopied(true) + setTimeout(() => setCopied(false), 2000) + } + } + + return ( +
+ +
+ ) +} diff --git a/packages/web/src/components/share/part.module.css b/packages/web/src/components/share/part.module.css new file mode 100644 index 00000000..ffae0c3b --- /dev/null +++ b/packages/web/src/components/share/part.module.css @@ -0,0 +1,419 @@ +.root { + display: flex; + gap: 0.625rem; + + [data-component="decoration"] { + flex: 0 0 auto; + display: flex; + flex-direction: column; + gap: 0.625rem; + align-items: center; + justify-content: flex-start; + + [data-slot="anchor"] { + position: relative; + + a:first-child { + display: block; + flex: 0 0 auto; + width: 18px; + opacity: 0.65; + + svg { + color: var(--sl-color-text-secondary); + display: block; + + &:nth-child(3) { + color: var(--sl-color-green-high); + } + } + + svg:nth-child(2), + svg:nth-child(3) { + display: none; + } + + &:hover { + svg:nth-child(1) { + display: none; + } + + svg:nth-child(2) { + display: block; + } + } + } + + [data-copied] & { + a, + a:hover { + svg:nth-child(1), + svg:nth-child(2) { + display: none; + } + + svg:nth-child(3) { + display: block; + } + } + } + } + + [data-slot="bar"] { + width: 3px; + height: 100%; + border-radius: 1px; + background-color: var(--sl-color-hairline); + } + + [data-slot="tooltip"] { + position: absolute; + top: 50%; + left: calc(100% + 12px); + transform: translate(0, -50%); + line-height: 1.1; + padding: 0.375em 0.5em calc(0.375em + 2px); + background: var(--sl-color-white); + color: var(--sl-color-text-invert); + font-size: 0.6875rem; + border-radius: 7px; + white-space: nowrap; + + z-index: 1; + opacity: 0; + visibility: hidden; + + &::after { + content: ""; + position: absolute; + top: 50%; + left: -15px; + transform: translateY(-50%); + border: 8px solid transparent; + border-right-color: var(--sl-color-white); + } + + [data-copied] & { + opacity: 1; + visibility: visible; + } + } + } + + [data-component="content"] { + flex: 1 1 auto; + min-width: 0; + padding: 0 0 1rem; + display: flex; + flex-direction: column; + gap: 1rem; + } + + [data-component="spacer"] { + height: 0rem; + } + + [data-component="content-footer"] { + align-self: flex-start; + font-size: 0.75rem; + color: var(--sl-color-text-dimmed); + } + + [data-component="user-text"] { + min-width: 0; + display: flex; + flex-direction: column; + gap: 1rem; + flex-grow: 1; + max-width: var(--md-tool-width); + } + + [data-component="assistant-text"] { + min-width: 0; + display: flex; + flex-direction: column; + gap: 1rem; + flex-grow: 1; + max-width: var(--md-tool-width); + + & > [data-component="assistant-text-markdown"] { + align-self: flex-start; + font-size: 0.875rem; + border: 1px solid var(--sl-color-blue-high); + padding: 0.5rem calc(0.5rem + 3px); + border-radius: 0.25rem; + position: relative; + + [data-component="copy-button"] { + top: 0.5rem; + right: calc(0.5rem - 1px); + } + } + } + + [data-component="step-start"] { + display: flex; + flex-direction: column; + align-items: flex-start; + gap: 0.375rem; + + [data-slot="provider"] { + line-height: 18px; + font-size: 0.875rem; + text-transform: uppercase; + letter-spacing: -0.5px; + color: var(--sl-color-text-secondary); + } + + [data-slot="model"] { + line-height: 1.5; + } + } + + [data-component="attachment"] { + display: flex; + flex-direction: column; + align-items: flex-start; + gap: 0.375rem; + padding-bottom: 1rem; + + [data-slot="copy"] { + line-height: 18px; + font-size: 0.875rem; + text-transform: uppercase; + letter-spacing: -0.5px; + color: var(--sl-color-text-secondary); + } + + [data-slot="filename"] { + line-height: 1.5; + font-size: 0.875rem; + font-weight: 500; + max-width: var(--md-tool-width); + } + } + + [data-component="button-text"] { + cursor: pointer; + appearance: none; + background-color: transparent; + border: none; + padding: 0; + color: var(--sl-color-text-secondary); + font-size: 0.75rem; + + &:hover { + color: var(--sl-color-text); + } + + &[data-more] { + display: flex; + align-items: center; + gap: 0.125rem; + + span[data-slot="icon"] { + line-height: 1; + opacity: 0.85; + + svg { + display: block; + } + } + } + } + + [data-component="tool"] { + display: flex; + flex-direction: column; + align-items: flex-start; + gap: 0.375rem; + + &[data-tool="bash"] { + max-width: var(--sm-tool-width); + } + + &[data-tool="error"] { + max-width: var(--md-tool-width); + } + + &[data-tool="read"], + &[data-tool="edit"], + &[data-tool="list"], + &[data-tool="glob"], + &[data-tool="grep"], + &[data-tool="write"], + &[data-tool="webfetch"] { + [data-component="tool-result"] { + max-width: var(--sm-tool-width); + } + } + &[data-tool="edit"] { + [data-component="tool-result"] { + max-width: var(--lg-tool-width); + align-items: stretch; + width: 100%; + } + } + &[data-tool="task"] { + [data-component="tool-input"] { + font-size: 0.75rem; + line-height: 1.5; + max-width: var(--md-tool-width); + display: -webkit-box; + -webkit-line-clamp: 3; + -webkit-box-orient: vertical; + overflow: hidden; + } + [data-component="tool-output"] { + max-width: var(--sm-tool-width); + font-size: 0.75rem; + border: 1px solid var(--sl-color-divider); + padding: 0.5rem calc(0.5rem + 3px); + border-radius: 0.25rem; + position: relative; + + [data-component="copy-button"] { + top: 0.5rem; + right: calc(0.5rem - 1px); + } + } + } + } + + [data-component="tool-title"] { + line-height: 18px; + font-size: 0.875rem; + color: var(--sl-color-text-secondary); + max-width: var(--md-tool-width); + display: flex; + align-items: flex-start; + gap: 0.375rem; + + [data-slot="name"] { + text-transform: uppercase; + letter-spacing: -0.5px; + } + + [data-slot="target"] { + color: var(--sl-color-text); + word-break: break-all; + font-weight: 500; + } + } + + [data-component="tool-result"] { + display: flex; + flex-direction: column; + align-items: flex-start; + gap: 0.5rem; + } + + [data-component="todos"] { + list-style-type: none; + padding: 0; + margin: 0; + width: 100%; + max-width: var(--sm-tool-width); + border: 1px solid var(--sl-color-divider); + border-radius: 0.25rem; + + [data-slot="item"] { + margin: 0; + position: relative; + padding-left: 1.5rem; + font-size: 0.75rem; + padding: 0.375rem 0.625rem 0.375rem 1.75rem; + border-bottom: 1px solid var(--sl-color-divider); + line-height: 1.5; + word-break: break-word; + + &:last-child { + border-bottom: none; + } + + & > span { + position: absolute; + display: inline-block; + left: 0.5rem; + top: calc(0.5rem + 1px); + width: 0.75rem; + height: 0.75rem; + border: 1px solid var(--sl-color-divider); + border-radius: 0.15rem; + + &::before { + } + } + + &[data-status="pending"] { + color: var(--sl-color-text); + } + + &[data-status="in_progress"] { + color: var(--sl-color-text); + + & > span { + border-color: var(--sl-color-orange); + } + + & > span::before { + content: ""; + position: absolute; + top: 2px; + left: 2px; + width: calc(0.75rem - 2px - 4px); + height: calc(0.75rem - 2px - 4px); + box-shadow: inset 1rem 1rem var(--sl-color-orange-low); + } + } + + &[data-status="completed"] { + color: var(--sl-color-text-secondary); + + & > span { + border-color: var(--sl-color-green-low); + } + + & > span::before { + content: ""; + position: absolute; + top: 2px; + left: 2px; + width: calc(0.75rem - 2px - 4px); + height: calc(0.75rem - 2px - 4px); + box-shadow: inset 1rem 1rem var(--sl-color-green); + + transform-origin: bottom left; + clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%); + } + } + } + } + + [data-component="tool-args"] { + display: inline-grid; + align-items: center; + grid-template-columns: max-content max-content minmax(0, 1fr); + max-width: var(--md-tool-width); + gap: 0.25rem 0.375rem; + + & > div:nth-child(3n + 1) { + width: 8px; + height: 2px; + border-radius: 1px; + background: var(--sl-color-divider); + } + + & > div:nth-child(3n + 2), + & > div:nth-child(3n + 3) { + font-size: 0.75rem; + line-height: 1.5; + } + + & > div:nth-child(3n + 3) { + padding-left: 0.125rem; + word-break: break-word; + color: var(--sl-color-text-secondary); + } + } +} diff --git a/packages/web/src/components/share/part.tsx b/packages/web/src/components/share/part.tsx new file mode 100644 index 00000000..4a9320e6 --- /dev/null +++ b/packages/web/src/components/share/part.tsx @@ -0,0 +1,757 @@ +import map from "lang-map" +import { DateTime } from "luxon" +import { For, Show, Match, Switch, type JSX, createMemo, createSignal, type ParentProps } from "solid-js" +import { + IconHashtag, + IconSparkles, + IconGlobeAlt, + IconDocument, + IconPaperClip, + IconQueueList, + IconUserCircle, + IconCommandLine, + IconCheckCircle, + IconChevronDown, + IconChevronRight, + IconDocumentPlus, + IconPencilSquare, + IconRectangleStack, + IconMagnifyingGlass, + IconDocumentMagnifyingGlass, +} from "../icons" +import { IconMeta, IconRobot, IconOpenAI, IconGemini, IconAnthropic } from "../icons/custom" +import { ContentCode } from "./content-code" +import { ContentDiff } from "./content-diff" +import { ContentText } from "./content-text" +import { ContentBash } from "./content-bash" +import { ContentError } from "./content-error" +import { formatDuration } from "../share/common" +import { ContentMarkdown } from "./content-markdown" +import type { MessageV2 } from "opencode/session/message-v2" +import type { Diagnostic } from "vscode-languageserver-types" + +import styles from "./part.module.css" + +const MIN_DURATION = 2000 + +export interface PartProps { + index: number + message: MessageV2.Info + part: MessageV2.Part + last: boolean +} + +export function Part(props: PartProps) { + const [copied, setCopied] = createSignal(false) + const id = createMemo(() => props.message.id + "-" + props.index) + + return ( +
+ +
+ {props.message.role === "user" && props.part.type === "text" && ( +
+ +
+ )} + {props.message.role === "assistant" && props.part.type === "text" && ( +
+
+ +
+ {props.last && props.message.role === "assistant" && props.message.time.completed && ( +
+ {DateTime.fromMillis(props.message.time.completed).toLocaleString(DateTime.DATETIME_MED)} +
+ )} +
+ )} + {props.message.role === "user" && props.part.type === "file" && ( +
+
Attachment
+
{props.part.filename}
+
+ )} + {props.part.type === "step-start" && props.message.role === "assistant" && ( +
+
{props.message.providerID}
+
{props.message.modelID}
+
+ )} + {props.part.type === "tool" && props.part.state.status === "error" && ( +
+ {formatErrorString(props.part.state.error)} + +
+ )} + {props.part.type === "tool" && + props.part.state.status === "completed" && + props.message.role === "assistant" && ( + <> +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + )} +
+
+ ) +} + +type ToolProps = { + id: MessageV2.ToolPart["id"] + tool: MessageV2.ToolPart["tool"] + state: MessageV2.ToolStateCompleted + message: MessageV2.Assistant + isLastPart?: boolean +} + +interface Todo { + id: string + content: string + status: "pending" | "in_progress" | "completed" + priority: "low" | "medium" | "high" +} + +function stripWorkingDirectory(filePath?: string, workingDir?: string) { + if (filePath === undefined || workingDir === undefined) return filePath + + const prefix = workingDir.endsWith("/") ? workingDir : workingDir + "/" + + if (filePath === workingDir) { + return "" + } + + if (filePath.startsWith(prefix)) { + return filePath.slice(prefix.length) + } + + return filePath +} + +function getShikiLang(filename: string) { + const ext = filename.split(".").pop()?.toLowerCase() ?? "" + const langs = map.languages(ext) + const type = langs?.[0]?.toLowerCase() + + const overrides: Record = { + conf: "shellscript", + } + + return type ? (overrides[type] ?? type) : "plaintext" +} + +function getDiagnostics(diagnosticsByFile: Record, currentFile: string): JSX.Element[] { + const result: JSX.Element[] = [] + + if (diagnosticsByFile === undefined || diagnosticsByFile[currentFile] === undefined) return result + + for (const diags of Object.values(diagnosticsByFile)) { + for (const d of diags) { + if (d.severity !== 1) continue + + const line = d.range.start.line + 1 + const column = d.range.start.character + 1 + + result.push( +
+          
+            Error
+          
+          
+            [{line}:{column}]
+          
+          {d.message}
+        
, + ) + } + } + + return result +} + +function formatErrorString(error: string): JSX.Element { + const errorMarker = "Error: " + const startsWithError = error.startsWith(errorMarker) + + return startsWithError ? ( +
+      
+        Error
+      
+      {error.slice(errorMarker.length)}
+    
+ ) : ( +
+      {error}
+    
+ ) +} + +export function TodoWriteTool(props: ToolProps) { + const priority: Record = { + in_progress: 0, + pending: 1, + completed: 2, + } + const todos = createMemo(() => + ((props.state.input?.todos ?? []) as Todo[]).slice().sort((a, b) => priority[a.status] - priority[b.status]), + ) + const starting = () => todos().every((t: Todo) => t.status === "pending") + const finished = () => todos().every((t: Todo) => t.status === "completed") + + return ( + <> +
+ + + Creating plan + Completing plan + + +
+ 0}> +
    + + {(todo) => ( +
  • + + {todo.content} +
  • + )} +
    +
+
+ + ) +} + +export function GrepTool(props: ToolProps) { + return ( + <> +
+ Grep + “{props.state.input.pattern}” +
+
+ + 0}> + + + + + + + + +
+ + ) +} + +export function ListTool(props: ToolProps) { + const path = createMemo(() => + props.state.input?.path !== props.message.path.cwd + ? stripWorkingDirectory(props.state.input?.path, props.message.path.cwd) + : props.state.input?.path, + ) + + return ( + <> +
+ LS + + {path()} + +
+
+ + + + + + + +
+ + ) +} + +export function WebFetchTool(props: ToolProps) { + return ( + <> +
+ Fetch + {props.state.input.url} +
+
+ + + {formatErrorString(props.state.output)} + + + + + + + +
+ + ) +} + +export function ReadTool(props: ToolProps) { + const filePath = createMemo(() => stripWorkingDirectory(props.state.input?.filePath, props.message.path.cwd)) + + return ( + <> +
+ Read + + {filePath()} + +
+
+ + + {formatErrorString(props.state.output)} + + + + + + + + + + + + +
+ + ) +} + +export function WriteTool(props: ToolProps) { + const filePath = createMemo(() => stripWorkingDirectory(props.state.input?.filePath, props.message.path.cwd)) + const diagnostics = createMemo(() => getDiagnostics(props.state.metadata?.diagnostics, props.state.input.filePath)) + + return ( + <> +
+ Write + + {filePath()} + +
+ 0}> + {diagnostics()} + +
+ + + {formatErrorString(props.state.output)} + + + + + + + +
+ + ) +} + +export function EditTool(props: ToolProps) { + const filePath = createMemo(() => stripWorkingDirectory(props.state.input.filePath, props.message.path.cwd)) + const diagnostics = createMemo(() => getDiagnostics(props.state.metadata?.diagnostics, props.state.input.filePath)) + + return ( + <> +
+ Edit + + {filePath()} + +
+
+ + + {formatErrorString(props.state.metadata?.message || "")} + + +
+ +
+
+
+
+ 0}> + {diagnostics()} + + + ) +} + +export function BashTool(props: ToolProps) { + return ( + + ) +} + +export function GlobTool(props: ToolProps) { + return ( + <> +
+ Glob + “{props.state.input.pattern}” +
+ + 0}> +
+ + + +
+
+ + + +
+ + ) +} + +interface ResultsButtonProps extends ParentProps { + showCopy?: string + hideCopy?: string +} +function ResultsButton(props: ResultsButtonProps) { + const [show, setShow] = createSignal(false) + + return ( + <> + + {props.children} + + ) +} + +export function Spacer() { + return
+} + +function Footer(props: ParentProps<{ title: string }>) { + return ( +
+ {props.children} +
+ ) +} + +function ToolFooter(props: { time: number }) { + return props.time > MIN_DURATION &&
{formatDuration(props.time)}
+} + +function TaskTool(props: ToolProps) { + return ( + <> +
+ Task + {props.state.input.description} +
+
+ “{props.state.input.prompt}” +
+ +
+ +
+
+ + ) +} + +export function FallbackTool(props: ToolProps) { + return ( + <> +
+ {props.tool} +
+
+ + {(arg) => ( + <> +
+
{arg[0]}
+
{arg[1]}
+ + )} +
+
+ + +
+ + + +
+
+
+ + ) +} + +// Converts nested objects/arrays into [path, value] pairs. +// E.g. {a:{b:{c:1}}, d:[{e:2}, 3]} => [["a.b.c",1], ["d[0].e",2], ["d[1]",3]] +function flattenToolArgs(obj: any, prefix: string = ""): Array<[string, any]> { + const entries: Array<[string, any]> = [] + + for (const [key, value] of Object.entries(obj)) { + const path = prefix ? `${prefix}.${key}` : key + + if (value !== null && typeof value === "object") { + if (Array.isArray(value)) { + value.forEach((item, index) => { + const arrayPath = `${path}[${index}]` + if (item !== null && typeof item === "object") { + entries.push(...flattenToolArgs(item, arrayPath)) + } else { + entries.push([arrayPath, item]) + } + }) + } else { + entries.push(...flattenToolArgs(value, path)) + } + } else { + entries.push([path, value]) + } + } + + return entries +} + +function getProvider(model: string) { + const lowerModel = model.toLowerCase() + + if (/claude|anthropic/.test(lowerModel)) return "anthropic" + if (/gpt|o[1-4]|codex|openai/.test(lowerModel)) return "openai" + if (/gemini|palm|bard|google/.test(lowerModel)) return "gemini" + if (/llama|meta/.test(lowerModel)) return "meta" + + return "any" +} + +export function ProviderIcon(props: { model: string; size?: number }) { + const provider = getProvider(props.model) + const size = props.size || 16 + return ( + }> + + + + + + + + + + + + + + ) +} diff --git a/packages/web/src/content/docs/docs/cli.mdx b/packages/web/src/content/docs/docs/cli.mdx index 49d343be..61592461 100644 --- a/packages/web/src/content/docs/docs/cli.mdx +++ b/packages/web/src/content/docs/docs/cli.mdx @@ -39,12 +39,12 @@ opencode run Explain the use of context in Go #### Flags -| Flag | Short | Description | -| ----------------- | ----- | --------------------- | -| `--continue` | `-c` | Continue the last session | -| `--session` | `-s` | Session ID to continue | -| `--share` | | Share the session | -| `--model` | `-m` | Model to use in the form of provider/model | +| Flag | Short | Description | +| ------------ | ----- | ------------------------------------------ | +| `--continue` | `-c` | Continue the last session | +| `--session` | `-s` | Session ID to continue | +| `--share` | | Share the session | +| `--model` | `-m` | Model to use in the form of provider/model | --- @@ -66,7 +66,7 @@ Logs you into a provider and saves them in the credentials file in `~/.local/sha opencode auth login ``` -When opencode starts up it will loads the providers from the credentials file. And if there are any keys defined in your environments or a `.env` file in your project. +When opencode starts up it loads the providers from the credentials file. And if there are any keys defined in your environments or a `.env` file in your project. --- @@ -122,8 +122,11 @@ opencode upgrade v0.1.48 The opencode CLI takes the following flags. -| Flag | Short | Description | -| ----------------- | ----- | --------------------- | -| `--help` | `-h` | Display help | -| `--version` | | Print version number | -| `--print-logs` | | Print logs to stderr | +| Flag | Short | Description | +| -------------- | ----- | -------------------- | +| `--help` | `-h` | Display help | +| `--version` | | Print version number | +| `--print-logs` | | Print logs to stderr | +| `--prompt` | `-p` | Prompt to use | +| `--model` | `-m` | Model to use in the form of provider/model | +| `--mode` | | Mode to use | diff --git a/packages/web/src/content/docs/docs/config.mdx b/packages/web/src/content/docs/docs/config.mdx index d88749c6..8cb91169 100644 --- a/packages/web/src/content/docs/docs/config.mdx +++ b/packages/web/src/content/docs/docs/config.mdx @@ -3,25 +3,35 @@ title: Config description: Using the opencode JSON config. --- -You can configure opencode using a JSON config file that can be placed in: +You can configure opencode using a JSON config file. -- Globally under `~/.config/opencode/config.json`. -- Your project root under `opencode.json`. This is safe to be checked into Git and uses the same schema as the global one. - -```json +```json title="opencode.json" { "$schema": "https://opencode.ai/config.json", "theme": "opencode", "model": "anthropic/claude-sonnet-4-20250514", - "autoshare": false, "autoupdate": true } ``` -In most cases, you'll want to use the global config for things like themes, providers, or keybinds. Having a config per project is useful if you are using different providers for your company. +This can be used to configure opencode globally or for a specific project. + +--- + +### Global + +Place your global opencode config in `~/.config/opencode/opencode.json`. You'll want to use the global config for things like themes, providers, or keybinds. + +--- + +### Per project + +You can also add a `opencode.json` in your project. This is useful for configuring providers or modes specific to your project. When opencode starts up, it looks for a config file in the current directory or traverse up to the nearest Git directory. +This is also safe to be checked into Git and uses the same schema as the global one. + --- ## Schema @@ -32,6 +42,25 @@ Your editor should be able to validate and autocomplete based on the schema. --- +### Modes + +opencode comes with two built-in modes: _build_, the default with all tools enabled. And _plan_, restricted mode with file modification tools disabled. You can override these built-in modes or define your own custom modes with the `mode` option. + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "mode": { + "build": { }, + "plan": { }, + "my-custom-mode": { } + } +} +``` + +[Learn more here](/docs/modes). + +--- + ### Models You can configure the providers and models you want to use in your opencode config through the `provider` and `model` options. @@ -39,12 +68,12 @@ You can configure the providers and models you want to use in your opencode conf ```json title="opencode.json" { "$schema": "https://opencode.ai/config.json", - "provider": { }, + "provider": {}, "model": "" } ``` -[Learn more here](/docs/models). +You can also configure [local models](/docs/models#local). [Learn more](/docs/models). --- @@ -63,6 +92,74 @@ You can configure the theme you want to use in your opencode config through the --- +### Layout + +You can configure the layout of the TUI with the `layout` option. + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "layout": "stretch" +} +``` + +This takes: + +- `"auto"`: Centers content with padding. This is the default. +- `"stretch"`: Uses full terminal width. + +--- + +### Logging + +Logs are written to: + +- **macOS/Linux**: `~/.local/share/opencode/log/` +- **Windows**: `%APPDATA%\opencode\log\` + +You can configure the minimum log level through the `log_level` option. + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "log_level": "INFO" +} +``` + +With the following options: + +| Level | Description | +| ------- | ---------------------------------------- | +| `DEBUG` | All messages including debug information | +| `INFO` | Informational messages and above | +| `WARN` | Warnings and errors only | +| `ERROR` | Errors only | + +The **default** log level is `INFO`. If you are running opencode locally in development mode it's set to `DEBUG`. + +--- + +### Sharing + +You can configure the [share](/docs/share) feature through the `share` option. + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "share": "manual" +} +``` + +This takes: + +- `"manual"` - Allow manual sharing via commands (default) +- `"auto"` - Automatically share new conversations +- `"disabled"` - Disable sharing entirely + +By default, sharing is set to manual mode where you need to explicitly share conversations using the `/share` command. + +--- + ### Keybinds You can customize your keybinds through the `keybinds` option. @@ -70,7 +167,7 @@ You can customize your keybinds through the `keybinds` option. ```json title="opencode.json" { "$schema": "https://opencode.ai/config.json", - "keybinds": { } + "keybinds": {} } ``` @@ -78,6 +175,19 @@ You can customize your keybinds through the `keybinds` option. --- +### Autoupdate + +opencode will automatically download any new updates when it starts up. You can disable this with the `autoupdate` option. + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "autoupdate": false +} +``` + +--- + ### MCP servers You can configure MCP servers you want to use through the `mcp` option. @@ -85,7 +195,7 @@ You can configure MCP servers you want to use through the `mcp` option. ```json title="opencode.json" { "$schema": "https://opencode.ai/config.json", - "mcp": { } + "mcp": {} } ``` @@ -93,6 +203,22 @@ You can configure MCP servers you want to use through the `mcp` option. --- +### Instructions + +You can configure the instructions for the model you're using through the `instructions` option. + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "instructions": ["CONTRIBUTING.md", "docs/guidelines.md", ".cursor/rules/*.md"] +} +``` + +This takes an array of paths and glob patterns to instruction files. [Learn more +about rules here](/docs/rules). + +--- + ### Disabled providers You can disable providers that are loaded automatically through the `disabled_providers` option. This is useful when you want to prevent certain providers from being loaded even if their credentials are available. @@ -105,6 +231,66 @@ You can disable providers that are loaded automatically through the `disabled_pr ``` The `disabled_providers` option accepts an array of provider IDs. When a provider is disabled: + - It won't be loaded even if environment variables are set - It won't be loaded even if API keys are configured through `opencode auth login` - The provider's models won't appear in the model selection list + +--- + +## Variables + +You can use variable substitution in your config files to reference environment variables and file contents. + +--- + +### Env vars + +Use `{env:VARIABLE_NAME}` to substitute environment variables: + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "model": "{env:OPENCODE_MODEL}", + "provider": { + "anthropic": { + "options": { + "apiKey": "{env:ANTHROPIC_API_KEY}" + } + } + } +} +``` + +If the environment variable is not set, it will be replaced with an empty string. + +--- + +### Files + +Use `{file:path/to/file}` to substitute the contents of a file: + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "instructions": ["{file:./custom-instructions.md}"], + "provider": { + "openai": { + "options": { + "apiKey": "{file:~/.secrets/openai-key}" + } + } + } +} +``` + +File paths can be: + +- Relative to the config file directory +- Or absolute paths starting with `/` or `~` + +These are useful for: + +- Keeping sensitive data like API keys in separate files. +- Including large instruction files without cluttering your config. +- Sharing common configuration snippets across multiple config files. diff --git a/packages/web/src/content/docs/docs/enterprise.mdx b/packages/web/src/content/docs/docs/enterprise.mdx new file mode 100644 index 00000000..d73d1d3a --- /dev/null +++ b/packages/web/src/content/docs/docs/enterprise.mdx @@ -0,0 +1,102 @@ +--- +title: Enterprise +description: Using opencode in your organization. +--- + +opencode does not store any of your code or context data. This makes it easy for +you to use opencode at your organization. + +To get started, we recommend: + +1. Do a trial internally with your team. +2. [**Contact us**](mailto:hello@sst.dev) to discuss pricing and implementation options. + +--- + +## Trial + +Since opencode is open source and does not store any of your code or context data, your developers can simply [get started](/docs/) and carry out a trial. + +--- + +### Data handling + +**opencode does not store your code or context data.** All processing happens locally or through direct API calls to your AI provider. + +The only caveat here is the optional `/share` feature. + +--- + +#### Sharing conversations + +If a user enables the `/share` feature, the conversation and the data associated with it are sent to the service we use to host these shares pages at opencode.ai. + +The data is currently served through our CDN's edge network, and is cached on the edge near your users. + +We recommend you disable this for your trial. + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "share": "disabled" +} +``` + +[Learn more about sharing](/docs/share). + +--- + +### Code ownership + +**You own all code produced by opencode.** There are no licensing restrictions or ownership claims. + +--- + +## Deployment + +Once you have completed your trial and you are ready to self-host opencode at +your organization, you can [**contact us**](mailto:hello@sst.dev) to discuss +pricing and implementation options. + +--- + +### SSO + +SSO integration can be implemented for enterprise deployments after your trial. +This will allow your team's session data and shared conversations to be protected +by your enterprise's authentication system. + +--- + +### Private NPM + +opencode supports private npm registries through Bun's native `.npmrc` file support. If your organization uses a private registry, such as JFrog Artifactory, Nexus, or similar, ensure developers are authenticated before running opencode. + +To set up authentication with your private registry: + +```bash +npm login --registry=https://your-company.jfrog.io/api/npm/npm-virtual/ +``` + +This creates `~/.npmrc` with authentication details. opencode will automatically +pick this up. + +:::caution +You must be logged into the private registry before running opencode. +::: + +Alternatively, you can manually configure a `.npmrc` file: + +```bash title="~/.npmrc" +registry=https://your-company.jfrog.io/api/npm/npm-virtual/ +//your-company.jfrog.io/api/npm/npm-virtual/:_authToken=${NPM_AUTH_TOKEN} +``` + +Developers must be logged into the private registry before running opencode to ensure packages can be installed from your enterprise registry. + +--- + +### Self-hosting + +The share feature can be self-hosted and the share pages can be made accessible +only after the user has been authenticated. diff --git a/packages/web/src/content/docs/docs/index.mdx b/packages/web/src/content/docs/docs/index.mdx index b39ce452..0484e9b3 100644 --- a/packages/web/src/content/docs/docs/index.mdx +++ b/packages/web/src/content/docs/docs/index.mdx @@ -3,7 +3,7 @@ title: Intro description: Get started with opencode. --- -import { Tabs, TabItem } from '@astrojs/starlight/components'; +import { Tabs, TabItem } from "@astrojs/starlight/components" [**opencode**](/) is an AI coding agent built for the terminal. It features: @@ -22,24 +22,24 @@ import { Tabs, TabItem } from '@astrojs/starlight/components'; - ```bash - npm install -g opencode-ai - ``` + ```bash + npm install -g opencode-ai + ``` - ```bash - bun install -g opencode-ai - ``` + ```bash + bun install -g opencode-ai + ``` - ```bash - pnpm install -g opencode-ai - ``` + ```bash + pnpm install -g opencode-ai + ``` - ```bash - yarn global add opencode-ai - ``` + ```bash + yarn global add opencode-ai + ``` diff --git a/packages/web/src/content/docs/docs/keybinds.mdx b/packages/web/src/content/docs/docs/keybinds.mdx index 8c5aa2c4..1d0c53b3 100644 --- a/packages/web/src/content/docs/docs/keybinds.mdx +++ b/packages/web/src/content/docs/docs/keybinds.mdx @@ -9,32 +9,45 @@ opencode has a list of keybinds that you can customize through the opencode conf { "$schema": "https://opencode.ai/config.json", "keybinds": { + "leader": "ctrl+x", - "help": "h", + "app_help": "h", + "switch_mode": "tab", + "editor_open": "e", + "session_new": "n", "session_list": "l", "session_share": "s", + "session_unshare": "u", "session_interrupt": "esc", "session_compact": "c", + "tool_details": "d", "model_list": "m", "theme_list": "t", "project_init": "i", + + "file_list": "f", + "file_close": "esc", + "file_diff_toggle": "v", + "input_clear": "ctrl+c", "input_paste": "ctrl+v", "input_submit": "enter", "input_newline": "shift+enter,ctrl+j", - "history_previous": "up", - "history_next": "down", + "messages_page_up": "pgup", "messages_page_down": "pgdown", "messages_half_page_up": "ctrl+alt+u", "messages_half_page_down": "ctrl+alt+d", - "messages_previous": "ctrl+alt+k", - "messages_next": "ctrl+alt+j", + "messages_previous": "ctrl+up", + "messages_next": "ctrl+down", "messages_first": "ctrl+g", "messages_last": "ctrl+alt+g", + "messages_layout_toggle": "p", + "messages_copy": "y", + "messages_revert": "r", "app_exit": "ctrl+c,q" } } diff --git a/packages/web/src/content/docs/docs/mcp-servers.mdx b/packages/web/src/content/docs/docs/mcp-servers.mdx index 0496e31c..1f473d83 100644 --- a/packages/web/src/content/docs/docs/mcp-servers.mdx +++ b/packages/web/src/content/docs/docs/mcp-servers.mdx @@ -49,7 +49,10 @@ Add a remote MCP servers under `mcp.remotemcp`. "remotemcp": { "type": "remote", "url": "https://my-mcp-server.com", - "enabled": true + "enabled": true, + "headers": { + "Authorization": "Bearer MY_API_KEY" + } } } } diff --git a/packages/web/src/content/docs/docs/models.mdx b/packages/web/src/content/docs/docs/models.mdx index f60a2544..cde1d2a4 100644 --- a/packages/web/src/content/docs/docs/models.mdx +++ b/packages/web/src/content/docs/docs/models.mdx @@ -11,10 +11,14 @@ opencode uses the [AI SDK](https://ai-sdk.dev/) and [Models.dev](https://models. You can configure providers in your opencode config under the `provider` section. +--- + ### Defaults Most popular providers are preloaded by default. If you've added the credentials for a provider through `opencode auth login`, they'll be available when you start opencode. +--- + ### Custom You can add custom providers by specifying the npm package for the provider and the models you want to use. @@ -23,13 +27,60 @@ You can add custom providers by specifying the npm package for the provider and { "$schema": "https://opencode.ai/config.json", "provider": { - "openrouter": { - "npm": "@openrouter/ai-sdk-provider", - "name": "OpenRouter", - "options": {}, + "moonshot": { + "npm": "@ai-sdk/openai-compatible", + "options": { + "baseURL": "https://api.moonshot.ai/v1" + }, "models": { - "anthropic/claude-3.5-sonnet": { - "name": "Claude 3.5 Sonnet" + "kimi-k2-0711-preview": {} + } + } + } +} +``` + +--- + +### Base URL + +You can customize the base URL for any provider by setting the `baseURL` option. This is useful when using proxy services or custom endpoints. + +```json title="opencode.json" {6} +{ + "$schema": "https://opencode.ai/config.json", + "provider": { + "anthropic": { + "options": { + "baseURL": "https://api.anthropic.com/v1" + } + } + } +} +``` + +--- + +### Local + +You can configure local model like ones served through LM Studio or Ollama. To +do so, you'll need to specify a couple of things. + +Here's an example of configuring a local model from LM Studio: + +```json title="opencode.json" {4-15} +{ + "$schema": "https://opencode.ai/config.json", + "provider": { + "lmstudio": { + "npm": "@ai-sdk/openai-compatible", + "name": "LM Studio (local)", + "options": { + "baseURL": "http://127.0.0.1:1234/v1" + }, + "models": { + "google/gemma-3n-e4b": { + "name": "Gemma 3n-e4b (local)" } } } @@ -37,9 +88,15 @@ You can add custom providers by specifying the npm package for the provider and } ``` -### Local +In this example: -To configure a local model, specify the npm package to use and the `baseURL`. +- `lmstudio` is the custom provider ID. We'll use this later. +- `npm` specifies the package to use for this provider. Here, `@ai-sdk/openai-compatible` is used for any OpenAI-compatible API. +- `name` is the display name for the provider in the UI. +- `options.baseURL` is the endpoint for the local server. +- `models` is a map of model IDs to their configurations. The model name will be displayed in the model selection list. + +Similarly, to configure a local model from Ollama: ```json title="opencode.json" {5,7} { @@ -58,6 +115,18 @@ To configure a local model, specify the npm package to use and the `baseURL`. } ``` +To set one of these as the default model, you can set the `model` key at the +root. + +```json title="opencode.json" {3} +{ + "$schema": "https://opencode.ai/config.json", + "model": "lmstudio/google/gemma-3n-e4b" +} +``` + +Here the full ID is `provider_id/model_id`, where `provider_id` is the key in the `provider` list we set above and `model_id` is the key from the `provider.models` list. + --- ## Select a model diff --git a/packages/web/src/content/docs/docs/modes.mdx b/packages/web/src/content/docs/docs/modes.mdx new file mode 100644 index 00000000..97e9248e --- /dev/null +++ b/packages/web/src/content/docs/docs/modes.mdx @@ -0,0 +1,199 @@ +--- +title: Modes +description: Different modes for different use cases. +--- + +Modes in opencode allow you to customize the behavior, tools, and prompts for different use cases. + +It comes with two built-in modes: **build** and **plan**. You can customize +these or configure your own through the opencode config. + +:::tip +Use the plan mode to analyze code and review suggestions without making any code +changes. +::: + +You can switch between modes during a session or configure them in your config file. + +--- + +## Built-in + +opencode comes with two built-in modes. + +--- + +### Build + +Build is the **default** mode with all tools enabled. This is the standard mode for development work where you need full access to file operations and system commands. + +--- + +### Plan + +A restricted mode designed for planning and analysis. In plan mode, the following tools are disabled by default: + +- `write` - Cannot create new files +- `edit` - Cannot modify existing files +- `patch` - Cannot apply patches +- `bash` - Cannot execute shell commands + +This mode is useful when you want the AI to analyze code, suggest changes, or create plans without making any actual modifications to your codebase. + +--- + +## Switching + +You can switch between modes during a session using the _Tab_ key. Or your configured `switch_mode` keybind. + +--- + +## Configure + +You can customize the built-in modes or create your own in the opencode [config](/docs/config). + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "mode": { + "build": { + "model": "anthropic/claude-sonnet-4-20250514", + "prompt": "{file:./prompts/build.txt}", + "tools": { + "write": true, + "edit": true, + "bash": true + } + }, + "plan": { + "model": "anthropic/claude-haiku-4-20250514", + "tools": { + "write": false, + "edit": false, + "bash": false + } + } + } +} +``` + +Let's look at these options in detail. + +--- + +### Model + +Use the `model` config to override the default model for this mode. Useful for using different models optimized for different tasks. For example, a faster model for planning, a more capable model for implementation. + +```json title="opencode.json" +{ + "mode": { + "plan": { + "model": "anthropic/claude-haiku-4-20250514" + } + } +} +``` + +--- + +### Prompt + +Specify a custom system prompt file for this mode with the `prompt` config. The prompt file should contain instructions specific to the mode's purpose. + +```json title="opencode.json" +{ + "mode": { + "review": { + "prompt": "{file:./prompts/code-review.txt}" + } + } +} +``` + +This path is relative to where the config file is located. So this works for +both the global opencode config and the project specific config. + +--- + +### Tools + +Control which tools are available in this mode with the `tools` config. You can enable or disable specific tools by setting them to `true` or `false`. + +```json +{ + "mode": { + "readonly": { + "tools": { + "write": false, + "edit": false, + "bash": false, + "read": true, + "grep": true, + "glob": true + } + } + } +} +``` + +If no tools are specified, all tools are enabled by default. + +--- + +#### Available tools + +Here are all the tools can be controlled through the mode config. + +| Tool | Description | +| ----------- | ----------------------- | +| `bash` | Execute shell commands | +| `edit` | Modify existing files | +| `write` | Create new files | +| `read` | Read file contents | +| `grep` | Search file contents | +| `glob` | Find files by pattern | +| `list` | List directory contents | +| `patch` | Apply patches to files | +| `todowrite` | Manage todo lists | +| `todoread` | Read todo lists | +| `webfetch` | Fetch web content | + +--- + +## Custom modes + +You can create your own custom modes by adding them to the `mode` configuration. For example, a documentation mode that focuses on reading and analysis. + +```json title="opencode.json" {4-14} +{ + "$schema": "https://opencode.ai/config.json", + "mode": { + "docs": { + "prompt": "{file:./prompts/documentation.txt}", + "tools": { + "write": true, + "edit": true, + "bash": false, + "read": true, + "grep": true, + "glob": true + } + } + } +} +``` + +--- + +### Use cases + +Here are some common use cases for different modes. + +- **Build mode**: Full development work with all tools enabled +- **Plan mode**: Analysis and planning without making changes +- **Review mode**: Code review with read-only access plus documentation tools +- **Debug mode**: Focused on investigation with bash and read tools enabled +- **Docs mode**: Documentation writing with file operations but no system commands + +You might also find different models are good for different use cases. diff --git a/packages/web/src/content/docs/docs/rules.mdx b/packages/web/src/content/docs/docs/rules.mdx index b7818d71..aa5590bb 100644 --- a/packages/web/src/content/docs/docs/rules.mdx +++ b/packages/web/src/content/docs/docs/rules.mdx @@ -31,17 +31,20 @@ You can also just create this file manually. Here's an example of some things yo This is an SST v3 monorepo with TypeScript. The project uses bun workspaces for package management. ## Project Structure + - `packages/` - Contains all workspace packages (functions, core, web, etc.) - `infra/` - Infrastructure definitions split by service (storage.ts, api.ts, web.ts) - `sst.config.ts` - Main SST configuration with dynamic imports ## Code Standards + - Use TypeScript with strict mode enabled - Shared code goes in `packages/core/` with proper exports configuration - Functions go in `packages/functions/` - Infrastructure should be split into logical files in `infra/` ## Monorepo Conventions + - Import shared modules using workspace names: `@my-app/core/example` ``` @@ -78,24 +81,72 @@ If you have both global and project-specific rules, opencode will combine them t ## Custom Instructions -You can also specify custom instruction files using the `instructions` configuration in your `opencode.json` or global `~/.config/opencode/config.json`: +You can specify custom instruction files in your `opencode.json` or the global `~/.config/opencode/opencode.json`. This allows you and your team to reuse existing rules rather than having to duplicate them to AGENTS.md. + +Example: ```json title="opencode.json" { "$schema": "https://opencode.ai/config.json", - "instructions": [".cursor/rules/*.md"] -} -``` - -You can specify multiple files like `CONTRIBUTING.md` and `docs/guidelines.md`, and use glob patterns to match multiple files. - -For example, to reuse your existing Cursor rules: - -```json title="opencode.json" -{ - "$schema": "https://opencode.ai/config.json", - "instructions": [".cursor/rules/*.md"] + "instructions": ["CONTRIBUTING.md", "docs/guidelines.md", ".cursor/rules/*.md"] } ``` All instruction files are combined with your `AGENTS.md` files. + +--- + +## Referencing External Files + +While opencode doesn't automatically parse file references in `AGENTS.md`, you can achieve similar functionality in two ways: + +### Using opencode.json + +The recommended approach is to use the `instructions` field in `opencode.json`: + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "instructions": ["docs/development-standards.md", "test/testing-guidelines.md", "packages/*/AGENTS.md"] +} +``` + +### Manual Instructions in AGENTS.md + +You can teach opencode to read external files by providing explicit instructions in your `AGENTS.md`. Here's a practical example: + +```markdown title="AGENTS.md" +# TypeScript Project Rules + +## External File Loading + +CRITICAL: When you encounter a file reference (e.g., @rules/general.md), use your Read tool to load it on a need-to-know basis. They're relevant to the SPECIFIC task at hand. + +Instructions: + +- Do NOT preemptively load all references - use lazy loading based on actual need +- When loaded, treat content as mandatory instructions that override defaults +- Follow references recursively when needed + +## Development Guidelines + +For TypeScript code style and best practices: @docs/typescript-guidelines.md +For React component architecture and hooks patterns: @docs/react-patterns.md +For REST API design and error handling: @docs/api-standards.md +For testing strategies and coverage requirements: @test/testing-guidelines.md + +## General Guidelines + +Read the following file immediately as it's relevant to all workflows: @rules/general-guidelines.md. +``` + +This approach allows you to: + +- Create modular, reusable rule files +- Share rules across projects via symlinks or git submodules +- Keep AGENTS.md concise while referencing detailed guidelines +- Ensure opencode loads files only when needed for the specific task + +:::tip +For monorepos or projects with shared standards, using `opencode.json` with glob patterns (like `packages/*/AGENTS.md`) is more maintainable than manual instructions. +::: diff --git a/packages/web/src/content/docs/docs/share.mdx b/packages/web/src/content/docs/docs/share.mdx new file mode 100644 index 00000000..efb54c2d --- /dev/null +++ b/packages/web/src/content/docs/docs/share.mdx @@ -0,0 +1,128 @@ +--- +title: Share +description: Share your opencode conversations. +--- + +opencode's share feature allows you to create public links to your opencode conversations, so you can collaborate with teammates or get help from others. + +:::note +Shared conversations are publicly accessible to anyone with the link. +::: + +--- + +## How it works + +When you share a conversation, opencode: + +1. Creates a unique public URL for your session +2. Syncs your conversation history to our servers +3. Makes the conversation accessible via the shareable link — `opencode.ai/s/` + +--- + +## Sharing + +opencode supports three sharing modes that control how conversations are shared: + +--- + +### Manual (default) + +By default, opencode uses manual sharing mode. Sessions are not shared automatically, but you can manually share them using the `/share` command: + +``` +/share +``` + +This will generate a unique URL that'll be copied to your clipboard. + +To explicitly set manual mode in your [config file](/docs/config): + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "share": "manual" +} +``` + +--- + +### Auto-share + +You can enable automatic sharing for all new conversations by setting the `share` option to `"auto"` in your [config file](/docs/config): + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "share": "auto" +} +``` + +With auto-share enabled, every new conversation will automatically be shared and a link will be generated. + +--- + +### Disabled + +You can disable sharing entirely by setting the `share` option to `"disabled"` in your [config file](/docs/config): + +```json title="opencode.json" +{ + "$schema": "https://opencode.ai/config.json", + "share": "disabled" +} +``` + +To enforce this across your team for a given project, add it to the `opencode.json` in your project and check into Git. + +--- + +## Un-sharing + +To stop sharing a conversation and remove it from public access: + +``` +/unshare +``` + +This will remove the share link and delete the data related to the conversation. + +--- + +## Privacy + +There are a few things to keep in mind when sharing a conversation. + +--- + +### Data retention + +Shared conversations remain accessible until you explicitly unshare them. This +includes: + +- Full conversation history +- All messages and responses +- Session metadata + +--- + +### Recommendations + +- Only share conversations that don't contain sensitive information. +- Review conversation content before sharing. +- Unshare conversations when collaboration is complete. +- Avoid sharing conversations with proprietary code or confidential data. +- For sensitive projects, disable sharing entirely. + +--- + +## For enterprises + +For enterprise deployments, the share feature can be: + +- **Disabled** entirely for security compliance +- **Restricted** to users authenticated through SSO only +- **Self-hosted** on your own infrastructure + +[Learn more](/docs/enterprise) about using opencode in your organization. diff --git a/packages/web/src/content/docs/docs/themes.mdx b/packages/web/src/content/docs/docs/themes.mdx index da612284..3defceae 100644 --- a/packages/web/src/content/docs/docs/themes.mdx +++ b/packages/web/src/content/docs/docs/themes.mdx @@ -9,22 +9,34 @@ By default, opencode uses our own `opencode` theme. --- +## Terminal requirements + +For themes to display correctly with their full color palette, your terminal must support **truecolor** (24-bit color). Most modern terminals support this by default, but you may need to enable it: + +- **Check support**: Run `echo $COLORTERM` - it should output `truecolor` or `24bit` +- **Enable truecolor**: Set the environment variable `COLORTERM=truecolor` in your shell profile +- **Terminal compatibility**: Ensure your terminal emulator supports 24-bit color (most modern terminals like iTerm2, Alacritty, Kitty, Windows Terminal, and recent versions of GNOME Terminal do) + +Without truecolor support, themes may appear with reduced color accuracy or fall back to the nearest 256-color approximation. + +--- + ## Built-in themes opencode comes with several built-in themes. -| Name | Description | -| --- | --- | -| `system` | Adapts to your terminal's background color | -| `tokyonight` | Based on the Tokyonight theme | -| `everforest` | Based on the Everforest theme | -| `ayu` | Based on the Ayu dark theme | -| `catppuccin` | Based on the Catppuccin theme | -| `gruvbox` | Based on the Gruvbox theme | -| `kanagawa` | Based on the Kanagawa theme | -| `nord` | Based on the Nord theme | -| `matrix` | Hacker-style green on black theme | -| `one-dark` | Based on the Atom One Dark theme | +| Name | Description | +| ------------ | ------------------------------------------ | +| `system` | Adapts to your terminal's background color | +| `tokyonight` | Based on the Tokyonight theme | +| `everforest` | Based on the Everforest theme | +| `ayu` | Based on the Ayu dark theme | +| `catppuccin` | Based on the Catppuccin theme | +| `gruvbox` | Based on the Gruvbox theme | +| `kanagawa` | Based on the Kanagawa theme | +| `nord` | Based on the Nord theme | +| `matrix` | Hacker-style green on black theme | +| `one-dark` | Based on the Atom One Dark theme | And more, we are constantly adding new themes. @@ -61,7 +73,7 @@ You can select a theme by bringing up the theme select with the `/theme` command ## Custom themes -opencode supports a flexible JSON-based theme system that allows users to create and customize themes easily. +opencode supports a flexible JSON-based theme system that allows users to create and customize themes easily. --- diff --git a/packages/web/src/content/docs/docs/troubleshooting.mdx b/packages/web/src/content/docs/docs/troubleshooting.mdx new file mode 100644 index 00000000..9d7dd33a --- /dev/null +++ b/packages/web/src/content/docs/docs/troubleshooting.mdx @@ -0,0 +1,143 @@ +--- +title: Troubleshooting +description: Common issues and how to resolve them. +--- + +To debug any issues with opencode, you can check the logs or the session data +that it stores locally. + +--- + +### Logs + +Log files are written to: + +- **macOS/Linux**: `~/.local/share/opencode/log/` +- **Windows**: `%APPDATA%\opencode\log\` + +Log files are named with timestamps (e.g., `2025-01-09T123456.log`) and the most recent 10 log files are kept. + +You can configure the log level in your [config file](/docs/config#logging) to get more detailed debug information. + +--- + +### Storage + +opencode stores session data and other application data on disk at: + +- **macOS/Linux**: `~/.local/share/opencode/` +- **Windows**: `%USERPROFILE%\.local\share\opencode` + +This directory contains: + +- `auth.json` - Authentication data like API keys, OAuth tokens +- `log/` - Application logs +- `project/` - Project-specific data like session and message data + - If the project is within a Git repo, it is stored in `.//storage/` + - If it is not a Git repo, it is stored in `./global/storage/` + +--- + +## Getting help + +If you're experiencing issues with opencode: + +1. **Report issues on GitHub** + + The best way to report bugs or request features is through our GitHub repository: + + [**github.com/sst/opencode/issues**](https://github.com/sst/opencode/issues) + + Before creating a new issue, search existing issues to see if your problem has already been reported. + +2. **Join our Discord** + + For real-time help and community discussion, join our Discord server: + + [**opencode.ai/discord**](https://opencode.ai/discord) + +--- + +## Common issues + +Here are some common issues and how to resolve them. + +--- + +### opencode won't start + +1. Check the logs for error messages +2. Try running with `--print-logs` to see output in the terminal +3. Ensure you have the latest version with `opencode upgrade` + +--- + +### Authentication issues + +1. Try re-authenticating with `opencode auth login ` +2. Check that your API keys are valid +3. Ensure your network allows connections to the provider's API + +--- + +### Model not available + +1. Check that you've authenticated with the provider +2. Verify the model name in your config is correct +3. Some models may require specific access or subscriptions + +--- + +### Copy/paste not working on Linux + +Linux users need to have one of the following clipboard utilities installed for copy/paste functionality to work: + +**For X11 systems:** + +```bash +apt install -y xclip +# or +apt install -y xsel +``` + +**For Wayland systems:** + +```bash +apt install -y wl-clipboard +``` + +**For headless environments:** + +```bash +apt install -y xvfb +# and run: +Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 & +export DISPLAY=:99.0 +``` + +opencode will detect if you're using Wayland and prefer `wl-clipboard`, otherwise it will try to find clipboard tools in order of: `xclip` and `xsel`. + +--- + +### How to select and copy text in the TUI + +There are several ways to copy text from opencode's TUI: + +- **Copy latest message**: Use `y` to copy the most recent message in your current session to the clipboard +- **Export session**: Use `/export` (or `x`) to open the current session as plain text in your `$EDITOR` (requires the `EDITOR` environment variable to be set) + +We're working on adding click & drag text selection in a future update. + +--- + +### TUI not rendering full width + +By default, opencode's TUI uses an "auto" layout that centers content with padding. If you want the TUI to use the full width of your terminal, you can configure the layout setting: + +```json title="opencode.json" +{ + "layout": "stretch" +} +``` + +Read more about this in the [config docs](/docs/config#layout). diff --git a/packages/web/src/pages/s/[id].astro b/packages/web/src/pages/s/[id].astro index a81d2e78..fadf0eb0 100644 --- a/packages/web/src/pages/s/[id].astro +++ b/packages/web/src/pages/s/[id].astro @@ -23,9 +23,8 @@ const models: Set = new Set(); const version = data.info.version ? `v${data.info.version}` : "v0.0.1"; Object.values(data.messages).forEach((d) => { - const assistant = d.metadata?.assistant; - if (assistant) { - models.add(assistant.modelID); + if (d.role === "assistant" && d.modelID) { + models.add(d.modelID); } }); @@ -39,8 +38,19 @@ const encodedTitle = encodeURIComponent( ) ); -const ogImage = `${config.socialCard}/opencode-share/${encodedTitle}.png?model=${Array.from(models).join(",")}&version=${version}&id=${id}`; +const modelsArray = Array.from(models); +let modelParam; +if (modelsArray.length === 1) { + modelParam = modelsArray[0]; +} +else if (modelsArray.length === 2) { + modelParam = encodeURIComponent(`${modelsArray[0]} & ${modelsArray[1]}`); +} +else { + modelParam = encodeURIComponent(`${modelsArray[0]} & ${modelsArray.length - 1} others`); +} +const ogImage = `${config.socialCard}/opencode-share/${encodedTitle}.png?model=${modelParam}&version=${version}&id=${id}`; --- ; + extensions: Record /** All languages keyed by file-extension */ - languages: Record; + languages: Record } /** @@ -14,14 +14,14 @@ declare module "lang-map" { * const { extensions, languages } = map(); * ``` */ - function map(): MapReturn; + function map(): MapReturn /** Static method: get extensions for a given language */ namespace map { - function extensions(language: string): string[]; + function extensions(language: string): string[] /** Static method: get languages for a given extension */ - function languages(extension: string): string[]; + function languages(extension: string): string[] } - export = map; + export = map } diff --git a/patches/ai@4.3.16.patch b/patches/ai@4.3.16.patch deleted file mode 100644 index 7d6df589..00000000 --- a/patches/ai@4.3.16.patch +++ /dev/null @@ -1,13 +0,0 @@ -diff --git a/dist/index.mjs b/dist/index.mjs -index 92a80377692488c4ba8801ce33e7736ad7055e43..add6281bbecaa1c03d3b48eb99aead4a7a7336b2 100644 ---- a/dist/index.mjs -+++ b/dist/index.mjs -@@ -1593,7 +1593,7 @@ function prepareCallSettings({ - return { - maxTokens, - // TODO v5 remove default 0 for temperature -- temperature: temperature != null ? temperature : 0, -+ temperature: temperature, - topP, - topK, - presencePenalty, diff --git a/scripts/publish-github-action.ts b/scripts/publish-github-action.ts new file mode 100755 index 00000000..9d0cbb7e --- /dev/null +++ b/scripts/publish-github-action.ts @@ -0,0 +1,16 @@ +#!/usr/bin/env bun + +import { $ } from "bun" + +try { + await $`git tag -d github-v1` + await $`git push origin :refs/tags/github-v1` +} catch (e: any) { + if (e instanceof $.ShellError && e.stderr.toString().match(/tag \S+ not found/)) { + console.log("tag not found, continuing...") + } else { + throw e + } +} +await $`git tag -a github-v1 -m "Update github-v1 to latest"` +await $`git push origin github-v1` diff --git a/scripts/stainless b/scripts/stainless index dae9016f..4b417a00 100755 --- a/scripts/stainless +++ b/scripts/stainless @@ -9,8 +9,8 @@ SERVER_PID=$! echo "Waiting for server to start..." sleep 3 -echo "Fetching OpenAPI spec from http://localhost:4096/doc..." -curl -s http://localhost:4096/doc > openapi.json +echo "Fetching OpenAPI spec from http://127.0.0.1:4096/doc..." +curl -s http://127.0.0.1:4096/doc > openapi.json echo "Stopping server..." kill $SERVER_PID @@ -23,4 +23,7 @@ rm -rf packages/tui/sdk mv opencode-go/ packages/tui/sdk/ rm -rf packages/tui/sdk/.git +echo "Kicking off production build..." +stl builds create --branch main --wait false + echo "Done!" diff --git a/scripts/stats.ts b/scripts/stats.ts index b30e57d9..bce21185 100755 --- a/scripts/stats.ts +++ b/scripts/stats.ts @@ -26,13 +26,9 @@ async function fetchNpmDownloads(packageName: string): Promise { // Use a range from 2020 to current year + 5 years to ensure it works forever const currentYear = new Date().getFullYear() const endYear = currentYear + 5 - const response = await fetch( - `https://api.npmjs.org/downloads/range/2020-01-01:${endYear}-12-31/${packageName}`, - ) + const response = await fetch(`https://api.npmjs.org/downloads/range/2020-01-01:${endYear}-12-31/${packageName}`) if (!response.ok) { - console.warn( - `Failed to fetch npm downloads for ${packageName}: ${response.status}`, - ) + console.warn(`Failed to fetch npm downloads for ${packageName}: ${response.status}`) return 0 } const data: NpmDownloadsRange = await response.json() @@ -53,9 +49,7 @@ async function fetchReleases(): Promise { const response = await fetch(url) if (!response.ok) { - throw new Error( - `GitHub API error: ${response.status} ${response.statusText}`, - ) + throw new Error(`GitHub API error: ${response.status} ${response.statusText}`) } const batch: Release[] = await response.json() @@ -66,6 +60,7 @@ async function fetchReleases(): Promise { if (batch.length < per) break page++ + await new Promise((resolve) => setTimeout(resolve, 1000)) } return releases @@ -115,11 +110,7 @@ async function save(githubTotal: number, npmDownloads: number) { for (let i = lines.length - 1; i >= 0; i--) { const line = lines[i].trim() - if ( - line.startsWith("|") && - !line.includes("Date") && - !line.includes("---") - ) { + if (line.startsWith("|") && !line.includes("Date") && !line.includes("---")) { const match = line.match( /\|\s*[\d-]+\s*\|\s*([\d,]+)\s*(?:\([^)]*\))?\s*\|\s*([\d,]+)\s*(?:\([^)]*\))?\s*\|\s*([\d,]+)\s*(?:\([^)]*\))?\s*\|/, ) @@ -147,11 +138,7 @@ async function save(githubTotal: number, npmDownloads: number) { ? ` (${githubChange.toLocaleString()})` : " (+0)" const npmChangeStr = - npmChange > 0 - ? ` (+${npmChange.toLocaleString()})` - : npmChange < 0 - ? ` (${npmChange.toLocaleString()})` - : " (+0)" + npmChange > 0 ? ` (+${npmChange.toLocaleString()})` : npmChange < 0 ? ` (${npmChange.toLocaleString()})` : " (+0)" const totalChangeStr = totalChange > 0 ? ` (+${totalChange.toLocaleString()})` @@ -182,9 +169,7 @@ const { total: githubTotal, stats } = calculate(releases) console.log("Fetching npm all-time downloads for opencode-ai...\n") const npmDownloads = await fetchNpmDownloads("opencode-ai") -console.log( - `Fetched npm all-time downloads: ${npmDownloads.toLocaleString()}\n`, -) +console.log(`Fetched npm all-time downloads: ${npmDownloads.toLocaleString()}\n`) await save(githubTotal, npmDownloads) @@ -202,24 +187,18 @@ console.log("-".repeat(60)) stats .sort((a, b) => b.downloads - a.downloads) .forEach((release) => { - console.log( - `${release.tag.padEnd(15)} ${release.downloads.toLocaleString().padStart(10)} downloads`, - ) + console.log(`${release.tag.padEnd(15)} ${release.downloads.toLocaleString().padStart(10)} downloads`) if (release.assets.length > 1) { release.assets .sort((a, b) => b.downloads - a.downloads) .forEach((asset) => { - console.log( - ` └─ ${asset.name.padEnd(25)} ${asset.downloads.toLocaleString().padStart(8)}`, - ) + console.log(` └─ ${asset.name.padEnd(25)} ${asset.downloads.toLocaleString().padStart(8)}`) }) } }) console.log("-".repeat(60)) -console.log( - `GitHub Total: ${githubTotal.toLocaleString()} downloads across ${releases.length} releases`, -) +console.log(`GitHub Total: ${githubTotal.toLocaleString()} downloads across ${releases.length} releases`) console.log(`npm Total: ${npmDownloads.toLocaleString()} downloads`) console.log(`Combined Total: ${totalDownloads.toLocaleString()} downloads`) diff --git a/sdks/github/action.yml b/sdks/github/action.yml new file mode 100644 index 00000000..8501ce09 --- /dev/null +++ b/sdks/github/action.yml @@ -0,0 +1,58 @@ +name: "opencode GitHub Action" +description: "Run opencode in GitHub Actions workflows" +branding: + icon: "code" + color: "orange" + +inputs: + model: + description: "Model to use" + required: false + + share: + description: "Share the opencode session (defaults to true for public repos)" + required: false + +outputs: + share_url: + description: "URL to share the opencode execution" + value: ${{ steps.run_opencode.outputs.share_url }} + +runs: + using: "composite" + steps: + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: 22 + + - name: Install Bun + uses: oven-sh/setup-bun@v2 + with: + bun-version: 1.2.16 + + - name: Install Dependencies + shell: bash + run: | + cd ${GITHUB_ACTION_PATH} + bun install + + - name: Install opencode + shell: bash + run: curl -fsSL https://opencode.ai/install | bash + + - name: Run opencode + shell: bash + id: run_opencode + run: | + bun run ${GITHUB_ACTION_PATH}/src/index.ts + env: + INPUT_MODEL: ${{ inputs.model }} + INPUT_SHARE: ${{ inputs.share }} + + #- name: Testing + # shell: bash + # run: | + # gh pr comment ${{ github.event.number }} --body "This is an automated comment" + # env: + # GH_TOKEN: ${{ github.token }} diff --git a/sdks/github/bun.lock b/sdks/github/bun.lock new file mode 100644 index 00000000..25587751 --- /dev/null +++ b/sdks/github/bun.lock @@ -0,0 +1,157 @@ +{ + "lockfileVersion": 1, + "workspaces": { + "": { + "name": "github", + "dependencies": { + "@actions/core": "^1.11.1", + "@actions/github": "^6.0.1", + "@octokit/graphql": "^9.0.1", + "@octokit/rest": "^22.0.0", + }, + "devDependencies": { + "@octokit/webhooks-types": "^7.6.1", + "@types/bun": "latest", + "@types/node": "^24.0.10", + }, + "peerDependencies": { + "typescript": "^5", + }, + }, + }, + "packages": { + "@actions/core": ["@actions/core@1.11.1", "", { "dependencies": { "@actions/exec": "^1.1.1", "@actions/http-client": "^2.0.1" } }, "sha512-hXJCSrkwfA46Vd9Z3q4cpEpHB1rL5NG04+/rbqW9d3+CSvtB1tYe8UTpAlixa1vj0m/ULglfEK2UKxMGxCxv5A=="], + + "@actions/exec": ["@actions/exec@1.1.1", "", { "dependencies": { "@actions/io": "^1.0.1" } }, "sha512-+sCcHHbVdk93a0XT19ECtO/gIXoxvdsgQLzb2fE2/5sIZmWQuluYyjPQtrtTHdU1YzTZ7bAPN4sITq2xi1679w=="], + + "@actions/github": ["@actions/github@6.0.1", "", { "dependencies": { "@actions/http-client": "^2.2.0", "@octokit/core": "^5.0.1", "@octokit/plugin-paginate-rest": "^9.2.2", "@octokit/plugin-rest-endpoint-methods": "^10.4.0", "@octokit/request": "^8.4.1", "@octokit/request-error": "^5.1.1", "undici": "^5.28.5" } }, "sha512-xbZVcaqD4XnQAe35qSQqskb3SqIAfRyLBrHMd/8TuL7hJSz2QtbDwnNM8zWx4zO5l2fnGtseNE3MbEvD7BxVMw=="], + + "@actions/http-client": ["@actions/http-client@2.2.3", "", { "dependencies": { "tunnel": "^0.0.6", "undici": "^5.25.4" } }, "sha512-mx8hyJi/hjFvbPokCg4uRd4ZX78t+YyRPtnKWwIl+RzNaVuFpQHfmlGVfsKEJN8LwTCvL+DfVgAM04XaHkm6bA=="], + + "@actions/io": ["@actions/io@1.1.3", "", {}, "sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q=="], + + "@fastify/busboy": ["@fastify/busboy@2.1.1", "", {}, "sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA=="], + + "@octokit/auth-token": ["@octokit/auth-token@4.0.0", "", {}, "sha512-tY/msAuJo6ARbK6SPIxZrPBms3xPbfwBrulZe0Wtr/DIY9lje2HeV1uoebShn6mx7SjCHif6EjMvoREj+gZ+SA=="], + + "@octokit/core": ["@octokit/core@5.2.2", "", { "dependencies": { "@octokit/auth-token": "^4.0.0", "@octokit/graphql": "^7.1.0", "@octokit/request": "^8.4.1", "@octokit/request-error": "^5.1.1", "@octokit/types": "^13.0.0", "before-after-hook": "^2.2.0", "universal-user-agent": "^6.0.0" } }, "sha512-/g2d4sW9nUDJOMz3mabVQvOGhVa4e/BN/Um7yca9Bb2XTzPPnfTWHWQg+IsEYO7M3Vx+EXvaM/I2pJWIMun1bg=="], + + "@octokit/endpoint": ["@octokit/endpoint@9.0.6", "", { "dependencies": { "@octokit/types": "^13.1.0", "universal-user-agent": "^6.0.0" } }, "sha512-H1fNTMA57HbkFESSt3Y9+FBICv+0jFceJFPWDePYlR/iMGrwM5ph+Dd4XRQs+8X+PUFURLQgX9ChPfhJ/1uNQw=="], + + "@octokit/graphql": ["@octokit/graphql@9.0.1", "", { "dependencies": { "@octokit/request": "^10.0.2", "@octokit/types": "^14.0.0", "universal-user-agent": "^7.0.0" } }, "sha512-j1nQNU1ZxNFx2ZtKmL4sMrs4egy5h65OMDmSbVyuCzjOcwsHq6EaYjOTGXPQxgfiN8dJ4CriYHk6zF050WEULg=="], + + "@octokit/openapi-types": ["@octokit/openapi-types@25.1.0", "", {}, "sha512-idsIggNXUKkk0+BExUn1dQ92sfysJrje03Q0bv0e+KPLrvyqZF8MnBpFz8UNfYDwB3Ie7Z0TByjWfzxt7vseaA=="], + + "@octokit/plugin-paginate-rest": ["@octokit/plugin-paginate-rest@9.2.2", "", { "dependencies": { "@octokit/types": "^12.6.0" }, "peerDependencies": { "@octokit/core": "5" } }, "sha512-u3KYkGF7GcZnSD/3UP0S7K5XUFT2FkOQdcfXZGZQPGv3lm4F2Xbf71lvjldr8c1H3nNbF+33cLEkWYbokGWqiQ=="], + + "@octokit/plugin-request-log": ["@octokit/plugin-request-log@6.0.0", "", { "peerDependencies": { "@octokit/core": ">=6" } }, "sha512-UkOzeEN3W91/eBq9sPZNQ7sUBvYCqYbrrD8gTbBuGtHEuycE4/awMXcYvx6sVYo7LypPhmQwwpUe4Yyu4QZN5Q=="], + + "@octokit/plugin-rest-endpoint-methods": ["@octokit/plugin-rest-endpoint-methods@10.4.1", "", { "dependencies": { "@octokit/types": "^12.6.0" }, "peerDependencies": { "@octokit/core": "5" } }, "sha512-xV1b+ceKV9KytQe3zCVqjg+8GTGfDYwaT1ATU5isiUyVtlVAO3HNdzpS4sr4GBx4hxQ46s7ITtZrAsxG22+rVg=="], + + "@octokit/request": ["@octokit/request@8.4.1", "", { "dependencies": { "@octokit/endpoint": "^9.0.6", "@octokit/request-error": "^5.1.1", "@octokit/types": "^13.1.0", "universal-user-agent": "^6.0.0" } }, "sha512-qnB2+SY3hkCmBxZsR/MPCybNmbJe4KAlfWErXq+rBKkQJlbjdJeS85VI9r8UqeLYLvnAenU8Q1okM/0MBsAGXw=="], + + "@octokit/request-error": ["@octokit/request-error@5.1.1", "", { "dependencies": { "@octokit/types": "^13.1.0", "deprecation": "^2.0.0", "once": "^1.4.0" } }, "sha512-v9iyEQJH6ZntoENr9/yXxjuezh4My67CBSu9r6Ve/05Iu5gNgnisNWOsoJHTP6k0Rr0+HQIpnH+kyammu90q/g=="], + + "@octokit/rest": ["@octokit/rest@22.0.0", "", { "dependencies": { "@octokit/core": "^7.0.2", "@octokit/plugin-paginate-rest": "^13.0.1", "@octokit/plugin-request-log": "^6.0.0", "@octokit/plugin-rest-endpoint-methods": "^16.0.0" } }, "sha512-z6tmTu9BTnw51jYGulxrlernpsQYXpui1RK21vmXn8yF5bp6iX16yfTtJYGK5Mh1qDkvDOmp2n8sRMcQmR8jiA=="], + + "@octokit/types": ["@octokit/types@14.1.0", "", { "dependencies": { "@octokit/openapi-types": "^25.1.0" } }, "sha512-1y6DgTy8Jomcpu33N+p5w58l6xyt55Ar2I91RPiIA0xCJBXyUAhXCcmZaDWSANiha7R9a6qJJ2CRomGPZ6f46g=="], + + "@octokit/webhooks-types": ["@octokit/webhooks-types@7.6.1", "", {}, "sha512-S8u2cJzklBC0FgTwWVLaM8tMrDuDMVE4xiTK4EYXM9GntyvrdbSoxqDQa+Fh57CCNApyIpyeqPhhFEmHPfrXgw=="], + + "@types/bun": ["@types/bun@1.2.18", "", { "dependencies": { "bun-types": "1.2.18" } }, "sha512-Xf6RaWVheyemaThV0kUfaAUvCNokFr+bH8Jxp+tTZfx7dAPA8z9ePnP9S9+Vspzuxxx9JRAXhnyccRj3GyCMdQ=="], + + "@types/node": ["@types/node@24.0.13", "", { "dependencies": { "undici-types": "~7.8.0" } }, "sha512-Qm9OYVOFHFYg3wJoTSrz80hoec5Lia/dPp84do3X7dZvLikQvM1YpmvTBEdIr/e+U8HTkFjLHLnl78K/qjf+jQ=="], + + "@types/react": ["@types/react@19.1.8", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-AwAfQ2Wa5bCx9WP8nZL2uMZWod7J7/JSplxbTmBQ5ms6QpqNYm672H0Vu9ZVKVngQ+ii4R/byguVEUZQyeg44g=="], + + "before-after-hook": ["before-after-hook@2.2.3", "", {}, "sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ=="], + + "bun-types": ["bun-types@1.2.18", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-04+Eha5NP7Z0A9YgDAzMk5PHR16ZuLVa83b26kH5+cp1qZW4F6FmAURngE7INf4tKOvCE69vYvDEwoNl1tGiWw=="], + + "csstype": ["csstype@3.1.3", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="], + + "deprecation": ["deprecation@2.3.1", "", {}, "sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ=="], + + "fast-content-type-parse": ["fast-content-type-parse@3.0.0", "", {}, "sha512-ZvLdcY8P+N8mGQJahJV5G4U88CSvT1rP8ApL6uETe88MBXrBHAkZlSEySdUlyztF7ccb+Znos3TFqaepHxdhBg=="], + + "once": ["once@1.4.0", "", { "dependencies": { "wrappy": "1" } }, "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w=="], + + "tunnel": ["tunnel@0.0.6", "", {}, "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg=="], + + "typescript": ["typescript@5.8.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ=="], + + "undici": ["undici@5.29.0", "", { "dependencies": { "@fastify/busboy": "^2.0.0" } }, "sha512-raqeBD6NQK4SkWhQzeYKd1KmIG6dllBOTt55Rmkt4HtI9mwdWtJljnrXjAFUBLTSN67HWrOIZ3EPF4kjUw80Bg=="], + + "undici-types": ["undici-types@7.8.0", "", {}, "sha512-9UJ2xGDvQ43tYyVMpuHlsgApydB8ZKfVYTsLDhXkFL/6gfkp+U8xTGdh8pMJv1SpZna0zxG1DwsKZsreLbXBxw=="], + + "universal-user-agent": ["universal-user-agent@7.0.3", "", {}, "sha512-TmnEAEAsBJVZM/AADELsK76llnwcf9vMKuPz8JflO1frO8Lchitr0fNaN9d+Ap0BjKtqWqd/J17qeDnXh8CL2A=="], + + "wrappy": ["wrappy@1.0.2", "", {}, "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="], + + "@octokit/core/@octokit/graphql": ["@octokit/graphql@7.1.1", "", { "dependencies": { "@octokit/request": "^8.4.1", "@octokit/types": "^13.0.0", "universal-user-agent": "^6.0.0" } }, "sha512-3mkDltSfcDUoa176nlGoA32RGjeWjl3K7F/BwHwRMJUW/IteSa4bnSV8p2ThNkcIcZU2umkZWxwETSSCJf2Q7g=="], + + "@octokit/core/@octokit/types": ["@octokit/types@13.10.0", "", { "dependencies": { "@octokit/openapi-types": "^24.2.0" } }, "sha512-ifLaO34EbbPj0Xgro4G5lP5asESjwHracYJvVaPIyXMuiuXLlhic3S47cBdTb+jfODkTE5YtGCLt3Ay3+J97sA=="], + + "@octokit/core/universal-user-agent": ["universal-user-agent@6.0.1", "", {}, "sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ=="], + + "@octokit/endpoint/@octokit/types": ["@octokit/types@13.10.0", "", { "dependencies": { "@octokit/openapi-types": "^24.2.0" } }, "sha512-ifLaO34EbbPj0Xgro4G5lP5asESjwHracYJvVaPIyXMuiuXLlhic3S47cBdTb+jfODkTE5YtGCLt3Ay3+J97sA=="], + + "@octokit/endpoint/universal-user-agent": ["universal-user-agent@6.0.1", "", {}, "sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ=="], + + "@octokit/graphql/@octokit/request": ["@octokit/request@10.0.3", "", { "dependencies": { "@octokit/endpoint": "^11.0.0", "@octokit/request-error": "^7.0.0", "@octokit/types": "^14.0.0", "fast-content-type-parse": "^3.0.0", "universal-user-agent": "^7.0.2" } }, "sha512-V6jhKokg35vk098iBqp2FBKunk3kMTXlmq+PtbV9Gl3TfskWlebSofU9uunVKhUN7xl+0+i5vt0TGTG8/p/7HA=="], + + "@octokit/plugin-paginate-rest/@octokit/types": ["@octokit/types@12.6.0", "", { "dependencies": { "@octokit/openapi-types": "^20.0.0" } }, "sha512-1rhSOfRa6H9w4YwK0yrf5faDaDTb+yLyBUKOCV4xtCDB5VmIPqd/v9yr9o6SAzOAlRxMiRiCic6JVM1/kunVkw=="], + + "@octokit/plugin-request-log/@octokit/core": ["@octokit/core@7.0.3", "", { "dependencies": { "@octokit/auth-token": "^6.0.0", "@octokit/graphql": "^9.0.1", "@octokit/request": "^10.0.2", "@octokit/request-error": "^7.0.0", "@octokit/types": "^14.0.0", "before-after-hook": "^4.0.0", "universal-user-agent": "^7.0.0" } }, "sha512-oNXsh2ywth5aowwIa7RKtawnkdH6LgU1ztfP9AIUCQCvzysB+WeU8o2kyyosDPwBZutPpjZDKPQGIzzrfTWweQ=="], + + "@octokit/plugin-rest-endpoint-methods/@octokit/types": ["@octokit/types@12.6.0", "", { "dependencies": { "@octokit/openapi-types": "^20.0.0" } }, "sha512-1rhSOfRa6H9w4YwK0yrf5faDaDTb+yLyBUKOCV4xtCDB5VmIPqd/v9yr9o6SAzOAlRxMiRiCic6JVM1/kunVkw=="], + + "@octokit/request/@octokit/types": ["@octokit/types@13.10.0", "", { "dependencies": { "@octokit/openapi-types": "^24.2.0" } }, "sha512-ifLaO34EbbPj0Xgro4G5lP5asESjwHracYJvVaPIyXMuiuXLlhic3S47cBdTb+jfODkTE5YtGCLt3Ay3+J97sA=="], + + "@octokit/request/universal-user-agent": ["universal-user-agent@6.0.1", "", {}, "sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ=="], + + "@octokit/request-error/@octokit/types": ["@octokit/types@13.10.0", "", { "dependencies": { "@octokit/openapi-types": "^24.2.0" } }, "sha512-ifLaO34EbbPj0Xgro4G5lP5asESjwHracYJvVaPIyXMuiuXLlhic3S47cBdTb+jfODkTE5YtGCLt3Ay3+J97sA=="], + + "@octokit/rest/@octokit/core": ["@octokit/core@7.0.3", "", { "dependencies": { "@octokit/auth-token": "^6.0.0", "@octokit/graphql": "^9.0.1", "@octokit/request": "^10.0.2", "@octokit/request-error": "^7.0.0", "@octokit/types": "^14.0.0", "before-after-hook": "^4.0.0", "universal-user-agent": "^7.0.0" } }, "sha512-oNXsh2ywth5aowwIa7RKtawnkdH6LgU1ztfP9AIUCQCvzysB+WeU8o2kyyosDPwBZutPpjZDKPQGIzzrfTWweQ=="], + + "@octokit/rest/@octokit/plugin-paginate-rest": ["@octokit/plugin-paginate-rest@13.1.1", "", { "dependencies": { "@octokit/types": "^14.1.0" }, "peerDependencies": { "@octokit/core": ">=6" } }, "sha512-q9iQGlZlxAVNRN2jDNskJW/Cafy7/XE52wjZ5TTvyhyOD904Cvx//DNyoO3J/MXJ0ve3rPoNWKEg5iZrisQSuw=="], + + "@octokit/rest/@octokit/plugin-rest-endpoint-methods": ["@octokit/plugin-rest-endpoint-methods@16.0.0", "", { "dependencies": { "@octokit/types": "^14.1.0" }, "peerDependencies": { "@octokit/core": ">=6" } }, "sha512-kJVUQk6/dx/gRNLWUnAWKFs1kVPn5O5CYZyssyEoNYaFedqZxsfYs7DwI3d67hGz4qOwaJ1dpm07hOAD1BXx6g=="], + + "@octokit/core/@octokit/types/@octokit/openapi-types": ["@octokit/openapi-types@24.2.0", "", {}, "sha512-9sIH3nSUttelJSXUrmGzl7QUBFul0/mB8HRYl3fOlgHbIWG+WnYDXU3v/2zMtAvuzZ/ed00Ei6on975FhBfzrg=="], + + "@octokit/endpoint/@octokit/types/@octokit/openapi-types": ["@octokit/openapi-types@24.2.0", "", {}, "sha512-9sIH3nSUttelJSXUrmGzl7QUBFul0/mB8HRYl3fOlgHbIWG+WnYDXU3v/2zMtAvuzZ/ed00Ei6on975FhBfzrg=="], + + "@octokit/graphql/@octokit/request/@octokit/endpoint": ["@octokit/endpoint@11.0.0", "", { "dependencies": { "@octokit/types": "^14.0.0", "universal-user-agent": "^7.0.2" } }, "sha512-hoYicJZaqISMAI3JfaDr1qMNi48OctWuOih1m80bkYow/ayPw6Jj52tqWJ6GEoFTk1gBqfanSoI1iY99Z5+ekQ=="], + + "@octokit/graphql/@octokit/request/@octokit/request-error": ["@octokit/request-error@7.0.0", "", { "dependencies": { "@octokit/types": "^14.0.0" } }, "sha512-KRA7VTGdVyJlh0cP5Tf94hTiYVVqmt2f3I6mnimmaVz4UG3gQV/k4mDJlJv3X67iX6rmN7gSHCF8ssqeMnmhZg=="], + + "@octokit/plugin-paginate-rest/@octokit/types/@octokit/openapi-types": ["@octokit/openapi-types@20.0.0", "", {}, "sha512-EtqRBEjp1dL/15V7WiX5LJMIxxkdiGJnabzYx5Apx4FkQIFgAfKumXeYAqqJCj1s+BMX4cPFIFC4OLCR6stlnA=="], + + "@octokit/plugin-request-log/@octokit/core/@octokit/auth-token": ["@octokit/auth-token@6.0.0", "", {}, "sha512-P4YJBPdPSpWTQ1NU4XYdvHvXJJDxM6YwpS0FZHRgP7YFkdVxsWcpWGy/NVqlAA7PcPCnMacXlRm1y2PFZRWL/w=="], + + "@octokit/plugin-request-log/@octokit/core/@octokit/request": ["@octokit/request@10.0.3", "", { "dependencies": { "@octokit/endpoint": "^11.0.0", "@octokit/request-error": "^7.0.0", "@octokit/types": "^14.0.0", "fast-content-type-parse": "^3.0.0", "universal-user-agent": "^7.0.2" } }, "sha512-V6jhKokg35vk098iBqp2FBKunk3kMTXlmq+PtbV9Gl3TfskWlebSofU9uunVKhUN7xl+0+i5vt0TGTG8/p/7HA=="], + + "@octokit/plugin-request-log/@octokit/core/@octokit/request-error": ["@octokit/request-error@7.0.0", "", { "dependencies": { "@octokit/types": "^14.0.0" } }, "sha512-KRA7VTGdVyJlh0cP5Tf94hTiYVVqmt2f3I6mnimmaVz4UG3gQV/k4mDJlJv3X67iX6rmN7gSHCF8ssqeMnmhZg=="], + + "@octokit/plugin-request-log/@octokit/core/before-after-hook": ["before-after-hook@4.0.0", "", {}, "sha512-q6tR3RPqIB1pMiTRMFcZwuG5T8vwp+vUvEG0vuI6B+Rikh5BfPp2fQ82c925FOs+b0lcFQ8CFrL+KbilfZFhOQ=="], + + "@octokit/plugin-rest-endpoint-methods/@octokit/types/@octokit/openapi-types": ["@octokit/openapi-types@20.0.0", "", {}, "sha512-EtqRBEjp1dL/15V7WiX5LJMIxxkdiGJnabzYx5Apx4FkQIFgAfKumXeYAqqJCj1s+BMX4cPFIFC4OLCR6stlnA=="], + + "@octokit/request-error/@octokit/types/@octokit/openapi-types": ["@octokit/openapi-types@24.2.0", "", {}, "sha512-9sIH3nSUttelJSXUrmGzl7QUBFul0/mB8HRYl3fOlgHbIWG+WnYDXU3v/2zMtAvuzZ/ed00Ei6on975FhBfzrg=="], + + "@octokit/request/@octokit/types/@octokit/openapi-types": ["@octokit/openapi-types@24.2.0", "", {}, "sha512-9sIH3nSUttelJSXUrmGzl7QUBFul0/mB8HRYl3fOlgHbIWG+WnYDXU3v/2zMtAvuzZ/ed00Ei6on975FhBfzrg=="], + + "@octokit/rest/@octokit/core/@octokit/auth-token": ["@octokit/auth-token@6.0.0", "", {}, "sha512-P4YJBPdPSpWTQ1NU4XYdvHvXJJDxM6YwpS0FZHRgP7YFkdVxsWcpWGy/NVqlAA7PcPCnMacXlRm1y2PFZRWL/w=="], + + "@octokit/rest/@octokit/core/@octokit/request": ["@octokit/request@10.0.3", "", { "dependencies": { "@octokit/endpoint": "^11.0.0", "@octokit/request-error": "^7.0.0", "@octokit/types": "^14.0.0", "fast-content-type-parse": "^3.0.0", "universal-user-agent": "^7.0.2" } }, "sha512-V6jhKokg35vk098iBqp2FBKunk3kMTXlmq+PtbV9Gl3TfskWlebSofU9uunVKhUN7xl+0+i5vt0TGTG8/p/7HA=="], + + "@octokit/rest/@octokit/core/@octokit/request-error": ["@octokit/request-error@7.0.0", "", { "dependencies": { "@octokit/types": "^14.0.0" } }, "sha512-KRA7VTGdVyJlh0cP5Tf94hTiYVVqmt2f3I6mnimmaVz4UG3gQV/k4mDJlJv3X67iX6rmN7gSHCF8ssqeMnmhZg=="], + + "@octokit/rest/@octokit/core/before-after-hook": ["before-after-hook@4.0.0", "", {}, "sha512-q6tR3RPqIB1pMiTRMFcZwuG5T8vwp+vUvEG0vuI6B+Rikh5BfPp2fQ82c925FOs+b0lcFQ8CFrL+KbilfZFhOQ=="], + + "@octokit/plugin-request-log/@octokit/core/@octokit/request/@octokit/endpoint": ["@octokit/endpoint@11.0.0", "", { "dependencies": { "@octokit/types": "^14.0.0", "universal-user-agent": "^7.0.2" } }, "sha512-hoYicJZaqISMAI3JfaDr1qMNi48OctWuOih1m80bkYow/ayPw6Jj52tqWJ6GEoFTk1gBqfanSoI1iY99Z5+ekQ=="], + + "@octokit/rest/@octokit/core/@octokit/request/@octokit/endpoint": ["@octokit/endpoint@11.0.0", "", { "dependencies": { "@octokit/types": "^14.0.0", "universal-user-agent": "^7.0.2" } }, "sha512-hoYicJZaqISMAI3JfaDr1qMNi48OctWuOih1m80bkYow/ayPw6Jj52tqWJ6GEoFTk1gBqfanSoI1iY99Z5+ekQ=="], + } +} diff --git a/sdks/github/package.json b/sdks/github/package.json new file mode 100644 index 00000000..e1b9222e --- /dev/null +++ b/sdks/github/package.json @@ -0,0 +1,19 @@ +{ + "name": "github", + "type": "module", + "private": true, + "devDependencies": { + "@octokit/webhooks-types": "^7.6.1", + "@types/bun": "latest", + "@types/node": "^24.0.10" + }, + "peerDependencies": { + "typescript": "^5" + }, + "dependencies": { + "@actions/core": "^1.11.1", + "@actions/github": "^6.0.1", + "@octokit/graphql": "^9.0.1", + "@octokit/rest": "^22.0.0" + } +} diff --git a/sdks/github/src/index.ts b/sdks/github/src/index.ts new file mode 100644 index 00000000..fd6e08aa --- /dev/null +++ b/sdks/github/src/index.ts @@ -0,0 +1,541 @@ +#!/usr/bin/env bun + +import os from "os" +import path from "path" +import { $ } from "bun" +import { Octokit } from "@octokit/rest" +import { graphql } from "@octokit/graphql" +import * as core from "@actions/core" +import * as github from "@actions/github" +import type { IssueCommentEvent } from "@octokit/webhooks-types" +import type { GitHubIssue, GitHubPullRequest, IssueQueryResponse, PullRequestQueryResponse } from "./types" + +if (github.context.eventName !== "issue_comment") { + core.setFailed(`Unsupported event type: ${github.context.eventName}`) + process.exit(1) +} + +const { owner, repo } = github.context.repo +const payload = github.context.payload as IssueCommentEvent +const actor = github.context.actor +const issueId = payload.issue.number +const body = payload.comment.body + +let appToken: string +let octoRest: Octokit +let octoGraph: typeof graphql +let commentId: number +let gitCredentials: string +let shareUrl: string | undefined +let state: + | { + type: "issue" + issue: GitHubIssue + } + | { + type: "local-pr" + pr: GitHubPullRequest + } + | { + type: "fork-pr" + pr: GitHubPullRequest + } + +async function run() { + try { + const match = body.match(/^hey\s*opencode,?\s*(.*)$/) + if (!match?.[1]) throw new Error("Command must start with `hey opencode`") + const userPrompt = match[1] + + const oidcToken = await generateGitHubToken() + appToken = await exchangeForAppToken(oidcToken) + octoRest = new Octokit({ auth: appToken }) + octoGraph = graphql.defaults({ + headers: { authorization: `token ${appToken}` }, + }) + + await configureGit(appToken) + await assertPermissions() + + const comment = await createComment("opencode started...") + commentId = comment.data.id + + // Set state + const repoData = await fetchRepo() + if (payload.issue.pull_request) { + const prData = await fetchPR() + state = { + type: prData.headRepository.nameWithOwner === prData.baseRepository.nameWithOwner ? "local-pr" : "fork-pr", + pr: prData, + } + } else { + state = { + type: "issue", + issue: await fetchIssue(), + } + } + + // Setup git branch + if (state.type === "local-pr") await checkoutLocalBranch(state.pr) + else if (state.type === "fork-pr") await checkoutForkBranch(state.pr) + + // Prompt + const share = process.env.INPUT_SHARE === "true" || !repoData.data.private + const promptData = state.type === "issue" ? buildPromptDataForIssue(state.issue) : buildPromptDataForPR(state.pr) + const responseRet = await runOpencode(`${userPrompt}\n\n${promptData}`, { + share, + }) + + const response = responseRet.stdout + shareUrl = responseRet.stderr.match(/https:\/\/opencode\.ai\/s\/\w+/)?.[0] + + // Comment and push changes + if (await branchIsDirty()) { + const summary = + (await runOpencode(`Summarize the following in less than 40 characters:\n\n${response}`, { share: false })) + ?.stdout || `Fix issue: ${payload.issue.title}` + + if (state.type === "issue") { + const branch = await pushToNewBranch(summary) + const pr = await createPR(repoData.data.default_branch, branch, summary, `${response}\n\nCloses #${issueId}`) + await updateComment(`opencode created pull request #${pr}`) + } else if (state.type === "local-pr") { + await pushToCurrentBranch(summary) + await updateComment(response) + } else if (state.type === "fork-pr") { + await pushToForkBranch(summary, state.pr) + await updateComment(response) + } + } else { + await updateComment(response) + } + await restoreGitConfig() + await revokeAppToken() + } catch (e: any) { + await restoreGitConfig() + await revokeAppToken() + console.error(e) + let msg = e + if (e instanceof $.ShellError) { + msg = e.stderr.toString() + } else if (e instanceof Error) { + msg = e.message + } + if (commentId) await updateComment(msg) + core.setFailed(`opencode failed with error: ${msg}`) + // Also output the clean error message for the action to capture + //core.setOutput("prepare_error", e.message); + process.exit(1) + } +} + +if (import.meta.main) { + run() +} + +async function generateGitHubToken() { + try { + return await core.getIDToken("opencode-github-action") + } catch (error) { + console.error("Failed to get OIDC token:", error) + throw new Error("Could not fetch an OIDC token. Make sure to add `id-token: write` to your workflow permissions.") + } +} + +async function exchangeForAppToken(oidcToken: string) { + const response = await fetch("https://api.opencode.ai/exchange_github_app_token", { + method: "POST", + headers: { + Authorization: `Bearer ${oidcToken}`, + }, + }) + + if (!response.ok) { + const responseJson = (await response.json()) as { error?: string } + throw new Error(`App token exchange failed: ${response.status} ${response.statusText} - ${responseJson.error}`) + } + + const responseJson = (await response.json()) as { token: string } + return responseJson.token +} + +async function configureGit(appToken: string) { + console.log("Configuring git...") + const config = "http.https://github.com/.extraheader" + const ret = await $`git config --local --get ${config}` + gitCredentials = ret.stdout.toString().trim() + + const newCredentials = Buffer.from(`x-access-token:${appToken}`, "utf8").toString("base64") + + await $`git config --local --unset-all ${config}` + await $`git config --local ${config} "AUTHORIZATION: basic ${newCredentials}"` + await $`git config --global user.name "opencode-agent[bot]"` + await $`git config --global user.email "opencode-agent[bot]@users.noreply.github.com"` +} + +async function checkoutLocalBranch(pr: GitHubPullRequest) { + console.log("Checking out local branch...") + + const branch = pr.headRefName + const depth = Math.max(pr.commits.totalCount, 20) + + await $`git fetch origin --depth=${depth} ${branch}` + await $`git checkout ${branch}` +} + +async function checkoutForkBranch(pr: GitHubPullRequest) { + console.log("Checking out fork branch...") + + const remoteBranch = pr.headRefName + const localBranch = generateBranchName() + const depth = Math.max(pr.commits.totalCount, 20) + + await $`git remote add fork https://github.com/${pr.headRepository.nameWithOwner}.git` + await $`git fetch fork --depth=${depth} ${remoteBranch}` + await $`git checkout -b ${localBranch} fork/${remoteBranch}` +} + +async function restoreGitConfig() { + if (!gitCredentials) return + const config = "http.https://github.com/.extraheader" + await $`git config --local ${config} "${gitCredentials}"` +} + +async function assertPermissions() { + console.log(`Asserting permissions for user ${actor}...`) + + let permission + try { + const response = await octoRest.repos.getCollaboratorPermissionLevel({ + owner, + repo, + username: actor, + }) + + permission = response.data.permission + console.log(` permission: ${permission}`) + } catch (error) { + console.error(`Failed to check permissions: ${error}`) + throw new Error(`Failed to check permissions for user ${actor}: ${error}`) + } + + if (!["admin", "write"].includes(permission)) throw new Error(`User ${actor} does not have write permissions`) +} + +function buildComment(content: string) { + const runId = process.env.GITHUB_RUN_ID! + const runUrl = `/${owner}/${repo}/actions/runs/${runId}` + return [content, "\n\n", shareUrl ? `[view session](${shareUrl}) | ` : "", `[view log](${runUrl})`].join("") +} + +async function createComment(body: string) { + console.log("Creating comment...") + return await octoRest.rest.issues.createComment({ + owner, + repo, + issue_number: issueId, + body: buildComment(body), + }) +} + +async function updateComment(body: string) { + console.log("Updating comment...") + return await octoRest.rest.issues.updateComment({ + owner, + repo, + comment_id: commentId, + body: buildComment(body), + }) +} + +function generateBranchName() { + const type = state.type === "issue" ? "issue" : "pr" + const timestamp = new Date() + .toISOString() + .replace(/[:-]/g, "") + .replace(/\.\d{3}Z/, "") + .split("T") + .join("_") + return `opencode/${type}${issueId}-${timestamp}` +} + +async function pushToCurrentBranch(summary: string) { + console.log("Pushing to current branch...") + await $`git add .` + await $`git commit -m "${summary} + +Co-authored-by: ${actor} <${actor}@users.noreply.github.com>"` + await $`git push` +} + +async function pushToForkBranch(summary: string, pr: GitHubPullRequest) { + console.log("Pushing to fork branch...") + + const remoteBranch = pr.headRefName + + await $`git add .` + await $`git commit -m "${summary} + +Co-authored-by: ${actor} <${actor}@users.noreply.github.com>"` + await $`git push fork HEAD:${remoteBranch}` +} + +async function pushToNewBranch(summary: string) { + console.log("Pushing to new branch...") + const branch = generateBranchName() + await $`git checkout -b ${branch}` + await $`git add .` + await $`git commit -m "${summary} + +Co-authored-by: ${actor} <${actor}@users.noreply.github.com>"` + await $`git push -u origin ${branch}` + return branch +} + +async function createPR(base: string, branch: string, title: string, body: string) { + console.log("Creating pull request...") + const pr = await octoRest.rest.pulls.create({ + owner, + repo, + head: branch, + base, + title, + body: buildComment(body), + }) + return pr.data.number +} + +async function runOpencode( + prompt: string, + opts?: { + share?: boolean + }, +) { + console.log("Running opencode...") + + const promptPath = path.join(os.tmpdir(), "PROMPT") + await Bun.write(promptPath, prompt) + const ret = await $`cat ${promptPath} | opencode run -m ${process.env.INPUT_MODEL} ${opts?.share ? "--share" : ""}` + return { + stdout: ret.stdout.toString().trim(), + stderr: ret.stderr.toString().trim(), + } +} + +async function branchIsDirty() { + console.log("Checking if branch is dirty...") + const ret = await $`git status --porcelain` + return ret.stdout.toString().trim().length > 0 +} + +async function fetchRepo() { + return await octoRest.rest.repos.get({ owner, repo }) +} + +async function fetchIssue() { + console.log("Fetching prompt data for issue...") + const issueResult = await octoGraph( + ` +query($owner: String!, $repo: String!, $number: Int!) { + repository(owner: $owner, name: $repo) { + issue(number: $number) { + title + body + author { + login + } + createdAt + state + comments(first: 100) { + nodes { + id + databaseId + body + author { + login + } + createdAt + } + } + } + } +}`, + { + owner, + repo, + number: issueId, + }, + ) + + const issue = issueResult.repository.issue + if (!issue) throw new Error(`Issue #${issueId} not found`) + + return issue +} + +function buildPromptDataForIssue(issue: GitHubIssue) { + const comments = (issue.comments?.nodes || []) + .filter((c) => { + const id = parseInt(c.databaseId) + return id !== commentId && id !== payload.comment.id + }) + .map((c) => ` - ${c.author.login} at ${c.createdAt}: ${c.body}`) + + return [ + "Here is the context for the issue:", + `- Title: ${issue.title}`, + `- Body: ${issue.body}`, + `- Author: ${issue.author.login}`, + `- Created At: ${issue.createdAt}`, + `- State: ${issue.state}`, + ...(comments.length > 0 ? ["- Comments:", ...comments] : []), + ].join("\n") +} + +async function fetchPR() { + console.log("Fetching prompt data for PR...") + const prResult = await octoGraph( + ` +query($owner: String!, $repo: String!, $number: Int!) { + repository(owner: $owner, name: $repo) { + pullRequest(number: $number) { + title + body + author { + login + } + baseRefName + headRefName + headRefOid + createdAt + additions + deletions + state + baseRepository { + nameWithOwner + } + headRepository { + nameWithOwner + } + commits(first: 100) { + totalCount + nodes { + commit { + oid + message + author { + name + email + } + } + } + } + files(first: 100) { + nodes { + path + additions + deletions + changeType + } + } + comments(first: 100) { + nodes { + id + databaseId + body + author { + login + } + createdAt + } + } + reviews(first: 100) { + nodes { + id + databaseId + author { + login + } + body + state + submittedAt + comments(first: 100) { + nodes { + id + databaseId + body + path + line + author { + login + } + createdAt + } + } + } + } + } + } +}`, + { + owner, + repo, + number: issueId, + }, + ) + + const pr = prResult.repository.pullRequest + if (!pr) throw new Error(`PR #${issueId} not found`) + + return pr +} + +function buildPromptDataForPR(pr: GitHubPullRequest) { + const comments = (pr.comments?.nodes || []) + .filter((c) => { + const id = parseInt(c.databaseId) + return id !== commentId && id !== payload.comment.id + }) + .map((c) => ` - ${c.author.login} at ${c.createdAt}: ${c.body}`) + + const files = (pr.files.nodes || []).map((f) => ` - ${f.path} (${f.changeType}) +${f.additions}/-${f.deletions}`) + const reviewData = (pr.reviews.nodes || []).map((r) => { + const comments = (r.comments.nodes || []).map((c) => ` - ${c.path}:${c.line ?? "?"}: ${c.body}`) + return [ + ` - ${r.author.login} at ${r.submittedAt}:`, + ` - Review body: ${r.body}`, + ...(comments.length > 0 ? [" - Comments:", ...comments] : []), + ] + }) + + return [ + "Here is the context for the pull request:", + `- Title: ${pr.title}`, + `- Body: ${pr.body}`, + `- Author: ${pr.author.login}`, + `- Created At: ${pr.createdAt}`, + `- Base Branch: ${pr.baseRefName}`, + `- Head Branch: ${pr.headRefName}`, + `- State: ${pr.state}`, + `- Additions: ${pr.additions}`, + `- Deletions: ${pr.deletions}`, + `- Total Commits: ${pr.commits.totalCount}`, + `- Changed Files: ${pr.files.nodes.length} files`, + ...(comments.length > 0 ? ["- Comments:", ...comments] : []), + ...(files.length > 0 ? ["- Changed files:", ...files] : []), + ...(reviewData.length > 0 ? ["- Reviews:", ...reviewData] : []), + ].join("\n") +} + +async function revokeAppToken() { + if (!appToken) return + + await fetch("https://api.github.com/installation/token", { + method: "DELETE", + headers: { + Authorization: `Bearer ${appToken}`, + Accept: "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + }, + }) +} diff --git a/sdks/github/src/types.ts b/sdks/github/src/types.ts new file mode 100644 index 00000000..fe0058fb --- /dev/null +++ b/sdks/github/src/types.ts @@ -0,0 +1,103 @@ +// Types for GitHub GraphQL query responses +export type GitHubAuthor = { + login: string; + name?: string; +}; + +export type GitHubComment = { + id: string; + databaseId: string; + body: string; + author: GitHubAuthor; + createdAt: string; +}; + +export type GitHubReviewComment = GitHubComment & { + path: string; + line: number | null; +}; + +export type GitHubCommit = { + oid: string; + message: string; + author: { + name: string; + email: string; + }; +}; + +export type GitHubFile = { + path: string; + additions: number; + deletions: number; + changeType: string; +}; + +export type GitHubReview = { + id: string; + databaseId: string; + author: GitHubAuthor; + body: string; + state: string; + submittedAt: string; + comments: { + nodes: GitHubReviewComment[]; + }; +}; + +export type GitHubPullRequest = { + title: string; + body: string; + author: GitHubAuthor; + baseRefName: string; + headRefName: string; + headRefOid: string; + createdAt: string; + additions: number; + deletions: number; + state: string; + baseRepository: { + nameWithOwner: string; + }; + headRepository: { + nameWithOwner: string; + }; + commits: { + totalCount: number; + nodes: Array<{ + commit: GitHubCommit; + }>; + }; + files: { + nodes: GitHubFile[]; + }; + comments: { + nodes: GitHubComment[]; + }; + reviews: { + nodes: GitHubReview[]; + }; +}; + +export type GitHubIssue = { + title: string; + body: string; + author: GitHubAuthor; + createdAt: string; + state: string; + comments: { + nodes: GitHubComment[]; + }; +}; + +export type PullRequestQueryResponse = { + repository: { + pullRequest: GitHubPullRequest; + }; +}; + +export type IssueQueryResponse = { + repository: { + issue: GitHubIssue; + }; +}; diff --git a/sdks/github/sst-env.d.ts b/sdks/github/sst-env.d.ts new file mode 100644 index 00000000..b6a7e906 --- /dev/null +++ b/sdks/github/sst-env.d.ts @@ -0,0 +1,9 @@ +/* This file is auto-generated by SST. Do not edit. */ +/* tslint:disable */ +/* eslint-disable */ +/* deno-fmt-ignore-file */ + +/// + +import "sst" +export {} \ No newline at end of file diff --git a/sdks/github/tsconfig.json b/sdks/github/tsconfig.json new file mode 100644 index 00000000..59435b49 --- /dev/null +++ b/sdks/github/tsconfig.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + // Environment setup & latest features + "lib": ["ESNext"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedIndexedAccess": true, + "noImplicitOverride": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + } +} diff --git a/sst-env.d.ts b/sst-env.d.ts index 627d74a5..2c3e3d5a 100644 --- a/sst-env.d.ts +++ b/sst-env.d.ts @@ -12,6 +12,14 @@ declare module "sst" { "Bucket": { "type": "sst.cloudflare.Bucket" } + "GITHUB_APP_ID": { + "type": "sst.sst.Secret" + "value": string + } + "GITHUB_APP_PRIVATE_KEY": { + "type": "sst.sst.Secret" + "value": string + } "Web": { "type": "sst.cloudflare.Astro" "url": string diff --git a/stainless.yml b/stainless.yml index f8d654fb..ba36156b 100644 --- a/stainless.yml +++ b/stainless.yml @@ -34,6 +34,7 @@ resources: models: unknownError: UnknownError providerAuthError: ProviderAuthError + messageAbortedError: MessageAbortedError event: methods: @@ -47,17 +48,29 @@ resources: app: models: app: App + logLevel: LogLevel + provider: Provider + model: Model + mode: Mode methods: get: get /app init: post /app/init + log: post /log + modes: get /mode + providers: get /config/providers find: + models: + match: Match + symbol: Symbol methods: text: get /find files: get /find/file symbols: get /find/symbol file: + models: + file: File methods: read: get /file status: get /file/status @@ -65,29 +78,32 @@ resources: config: models: config: Config - keybinds: KeybindsConfig - mcpLocal: McpLocalConfig - mcpRemote: McpRemoteConfig - provider: Provider - model: Model + keybindsConfig: KeybindsConfig + mcpLocalConfig: McpLocalConfig + mcpRemoteConfig: McpRemoteConfig + modeConfig: ModeConfig + layoutConfig: LayoutConfig methods: get: get /config - providers: get /config/providers session: models: session: Session message: Message - toolCall: ToolCall - toolPartialCall: ToolPartialCall - toolResult: ToolResult + part: Part textPart: TextPart - reasoningPart: ReasoningPart - toolInvocationPart: ToolInvocationPart - sourceUrlPart: SourceUrlPart filePart: FilePart + toolPart: ToolPart stepStartPart: StepStartPart - messagePart: MessagePart + stepFinishPart: StepFinishPart + snapshotPart: SnapshotPart + assistantMessage: AssistantMessage + userMessage: UserMessage + toolStatePending: ToolStatePending + toolStateRunning: ToolStateRunning + toolStateCompleted: ToolStateCompleted + toolStateError: ToolStateError + methods: list: get /session create: post /session @@ -111,9 +127,13 @@ readme: example_requests: default: type: request - endpoint: get /event + endpoint: get /session params: {} headline: + type: request + endpoint: get /session + params: {} + streaming: type: request endpoint: get /event params: {}