mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-19 22:56:54 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8c20d6a11 | ||
|
|
8697481d43 | ||
|
|
4ac274cdd4 | ||
|
|
eea844320d | ||
|
|
d255e76806 | ||
|
|
2be1093b91 | ||
|
|
83e902862c | ||
|
|
0a66776067 | ||
|
|
c55804a548 | ||
|
|
97c2939b5e | ||
|
|
0325435125 | ||
|
|
00fa5fed75 | ||
|
|
35a0e82284 | ||
|
|
843a56304c |
@@ -53,9 +53,6 @@ jobs:
|
||||
echo "Smoke testing shumai..."
|
||||
bun run /tmp/shumai-local-release/node/node_modules/@shumai-one/shumai/bin/shumai-app.js --check
|
||||
|
||||
echo "Smoke testing shumai-agent..."
|
||||
bun run /tmp/shumai-local-release/node/node_modules/@shumai-one/shumai-agent/bin/shumai-agent-app.js --check
|
||||
|
||||
echo "Smoke testing shumai-transcode..."
|
||||
bun run /tmp/shumai-local-release/node/node_modules/@shumai-one/shumai-transcode/bin/shumai-transcode-app.js --check
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
name: Native CLI
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [main]
|
||||
push:
|
||||
branches: [main]
|
||||
workflow_call:
|
||||
inputs:
|
||||
source_ref:
|
||||
required: true
|
||||
type: string
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
native:
|
||||
name: CLI (${{ matrix.target }})
|
||||
runs-on: ${{ matrix.runner }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- { target: linux-x64, runner: ubuntu-22.04 }
|
||||
- { target: linux-arm64, runner: ubuntu-22.04-arm }
|
||||
- { target: darwin-x64, runner: macos-15-intel }
|
||||
- { target: darwin-arm64, runner: macos-15 }
|
||||
- { target: windows-x64, runner: windows-2022 }
|
||||
- { target: windows-arm64, runner: windows-11-arm }
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ inputs.source_ref || github.ref }}
|
||||
- uses: oven-sh/setup-bun@v2
|
||||
with:
|
||||
bun-version: 1.4.4
|
||||
- name: Install dependencies
|
||||
run: bun install --frozen-lockfile --ignore-scripts
|
||||
- name: Build CLI archive and installer
|
||||
run: bun run build:cli --target ${{ matrix.target }}
|
||||
- name: Test installed binary and file transfers
|
||||
run: bun run test:cli-release
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: cli-${{ matrix.target }}
|
||||
path: out/cli-release/shumai-cli-v*
|
||||
if-no-files-found: error
|
||||
retention-days: 7
|
||||
@@ -14,13 +14,30 @@ on:
|
||||
description: 'Source ref to build/publish (defaults to tag)'
|
||||
required: false
|
||||
type: string
|
||||
validate_only:
|
||||
description: 'Build and test without publishing npm packages or a GitHub Release'
|
||||
required: false
|
||||
default: false
|
||||
type: boolean
|
||||
|
||||
permissions: {}
|
||||
|
||||
concurrency:
|
||||
group: npm-release-${{ github.event.inputs.tag || github.ref_name }}
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
cli:
|
||||
uses: ./.github/workflows/cli-native.yaml
|
||||
permissions:
|
||||
contents: read
|
||||
with:
|
||||
source_ref: ${{ inputs.source_ref || inputs.tag || github.ref_name }}
|
||||
|
||||
release:
|
||||
name: Build, Release and Publish
|
||||
runs-on: ubuntu-latest
|
||||
needs: cli
|
||||
permissions:
|
||||
contents: write
|
||||
id-token: write
|
||||
@@ -32,6 +49,7 @@ jobs:
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ env.SOURCE_REF }}
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Setup Bun
|
||||
uses: oven-sh/setup-bun@v2
|
||||
@@ -53,12 +71,18 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: bun install
|
||||
|
||||
- name: Validate release source and version
|
||||
run: bun scripts/publish-cli-release.ts ${{ inputs.validate_only && 'validate-only' || 'validate' }}
|
||||
|
||||
- name: Compile i18n
|
||||
run: bun run i18n:compile
|
||||
|
||||
- name: Lint
|
||||
run: bun run lint
|
||||
|
||||
- name: Format
|
||||
run: bun run format && git diff --exit-code
|
||||
|
||||
- name: Typecheck
|
||||
run: bun run typecheck
|
||||
|
||||
@@ -68,7 +92,18 @@ jobs:
|
||||
- name: Build NPM packages
|
||||
run: bun run build-npm
|
||||
|
||||
- name: Download tested CLI archives
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
pattern: cli-*
|
||||
path: out/cli-release
|
||||
merge-multiple: true
|
||||
|
||||
- name: Assemble CLI release assets
|
||||
run: bun run build:cli --assemble
|
||||
|
||||
- name: Generate release notes
|
||||
if: ${{ !inputs.validate_only }}
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
@@ -87,29 +122,24 @@ jobs:
|
||||
fi
|
||||
} > /tmp/release-notes.md
|
||||
|
||||
- name: Create GitHub Release
|
||||
- name: Stage draft GitHub Release and CLI assets
|
||||
if: ${{ !inputs.validate_only }}
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
if [ ! -s /tmp/release-notes.md ]; then
|
||||
NOTES_FLAG="--generate-notes"
|
||||
else
|
||||
NOTES_FLAG="--notes-file /tmp/release-notes.md"
|
||||
fi
|
||||
if gh release view "${{ env.RELEASE_TAG }}" >/dev/null 2>&1; then
|
||||
gh release edit "${{ env.RELEASE_TAG }}" \
|
||||
--title "${{ env.RELEASE_TAG }}" \
|
||||
$NOTES_FLAG
|
||||
else
|
||||
gh release create "${{ env.RELEASE_TAG }}" \
|
||||
--title "${{ env.RELEASE_TAG }}" \
|
||||
$NOTES_FLAG
|
||||
fi
|
||||
run: bun scripts/publish-cli-release.ts stage
|
||||
|
||||
- name: Upgrade npm for trusted publishing
|
||||
if: ${{ !inputs.validate_only }}
|
||||
run: |
|
||||
npm install -g npm@11.16.0 --ignore-scripts
|
||||
npm --version
|
||||
|
||||
- name: Publish to NPM
|
||||
if: ${{ !inputs.validate_only }}
|
||||
run: bun run scripts/publish.ts
|
||||
|
||||
- name: Publish completed GitHub Release
|
||||
if: ${{ !inputs.validate_only }}
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: bun scripts/publish-cli-release.ts publish
|
||||
|
||||
@@ -77,6 +77,8 @@ Add any additional context, caveats, or follow-up work.
|
||||
|
||||
## Workspace Architecture
|
||||
|
||||
**Single-team design**: Each instance has one team, automatically created when the first user signs up.
|
||||
|
||||
The project is a Bun Workspaces monorepo (Bun runtime, Hono API, Prisma + Pgvector18 DB, React + Tailwind CSS + Shadcn UI frontend):
|
||||
|
||||
- **`packages/webui`**: React frontend (`bun run dev` runs on port 3000).
|
||||
|
||||
@@ -12,6 +12,36 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
||||
|
||||
### Changed
|
||||
|
||||
## [0.7.0] - 2026-11-18
|
||||
|
||||
### Added
|
||||
|
||||
- **i18n**: Add Korean as a display language, selectable on the sign-up page and in team settings.
|
||||
|
||||
### Fixed
|
||||
|
||||
- **cli**: Fix release publication failing to find staged standalone downloads.
|
||||
|
||||
### Changed
|
||||
|
||||
- **agent**: ⚠️ **Breaking Change**: Replaced the built-in server-side Bash sandbox with external Runners and removed the team-level sandbox domain allowlist settings. The Bash tool is now disabled by default. If your Agent or Skills require Bash execution, you'll need to set up and connect an external Runner using shumai-cli. The Shumai CLI includes built-in sandbox support, allowing you to configure filesystem access and network domain allowlists for Bash execution directly on the Runner. See [Agent Runners](https://docs.shumai.one/using-shumai/agent-sandbox) for setup instructions and configuration details. The Docker Compose configuration has also been updated to improve security following the removal of the server-side sandbox. We strongly recommend upgrading to the latest Shumai image and updating your Docker Compose file to apply these security improvements.
|
||||
- **cli**: Show copyable commands and shell configuration instructions when the standalone CLI installation directory is missing from PATH.
|
||||
|
||||
## [0.6.6] - 2026-11-13
|
||||
|
||||
### Added
|
||||
|
||||
- **project**: Support creating new projects via the built-in agent, MCP server, and CLI (`shumai-cli project create`).
|
||||
- **cli**: Add standalone downloads and one-line installers for macOS, Linux, and Windows without requiring Node.js or Bun, with verified downloads, version selection, and upgrade support.
|
||||
|
||||
### Fixed
|
||||
|
||||
- **storage**: Remove empty local folders left behind after generating video search embeddings.
|
||||
|
||||
### Changed
|
||||
|
||||
- **agent**: Upgrade the agent runtime and automatically migrate existing conversations, keep thread histories independent, prevent duplicate history during simultaneous mentions, improve session log scrolling and readability, and fix authentication for OpenAI and custom providers. Agents now run with the main server; standalone agent workers are no longer supported.
|
||||
|
||||
## [0.6.5] - 2026-11-07
|
||||
|
||||
### Added
|
||||
|
||||
+2
-6
@@ -8,7 +8,6 @@ WORKDIR /app
|
||||
COPY package.json bun.lock ./
|
||||
COPY --parents packages/*/package.json ./
|
||||
COPY --parents apps/*/package.json ./
|
||||
COPY patches/ ./patches/
|
||||
|
||||
# Install dependencies (including devDependencies for build tools)
|
||||
RUN bun install --frozen-lockfile
|
||||
@@ -47,9 +46,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
ripgrep \
|
||||
procps \
|
||||
lsof \
|
||||
socat \
|
||||
ca-certificates \
|
||||
bubblewrap \
|
||||
gosu \
|
||||
poppler-utils \
|
||||
imagemagick \
|
||||
@@ -96,11 +93,10 @@ USER root
|
||||
COPY scripts/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
|
||||
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# Expose "bun index.js" as "shumai" CLI command (and worker commands "shumai-transcode", "shumai-agent")
|
||||
# Expose "bun index.js" as "shumai" CLI command (and the transcode worker command)
|
||||
RUN printf '#!/bin/sh\nexec bun /app/index.js "$@"\n' > /usr/local/bin/shumai \
|
||||
&& printf '#!/bin/sh\nexec bun /app/transcode-worker.js "$@"\n' > /usr/local/bin/shumai-transcode \
|
||||
&& printf '#!/bin/sh\nexec bun /app/agent-worker.js "$@"\n' > /usr/local/bin/shumai-agent \
|
||||
&& chmod +x /usr/local/bin/shumai /usr/local/bin/shumai-transcode /usr/local/bin/shumai-agent \
|
||||
&& chmod +x /usr/local/bin/shumai /usr/local/bin/shumai-transcode \
|
||||
&& ln -s /usr/local/bin/shumai /usr/local/bin/shuami
|
||||
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
@@ -8,7 +8,6 @@ WORKDIR /app
|
||||
COPY package.json bun.lock ./
|
||||
COPY --parents packages/*/package.json ./
|
||||
COPY --parents apps/*/package.json ./
|
||||
COPY patches/ ./patches/
|
||||
|
||||
# Install dependencies (including devDependencies for build tools)
|
||||
RUN bun install --frozen-lockfile
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a target="_blank" href="https://shumai.one/">Website</a> | <a target="_blank" href="https://docs.shumai.one/getting-started/overview">Docs</a> | <a target="_blank" href="https://staging.shumai.one">Demo</a> | <a target="_blank" href="https://discord.gg/sUzhujK6DD">Discord</a> | <a target="_blank" href="README_CN.md">中文</a>
|
||||
<a target="_blank" href="https://shumai.one/">Website</a> | <a target="_blank" href="https://docs.shumai.one/getting-started/overview">Docs</a> | <a target="_blank" href="https://staging.shumai.one">Demo</a> | <a target="_blank" href="https://shumai.discourse.group/">Discourse Forum</a> | <a target="_blank" href="README_CN.md">中文</a>
|
||||
</p>
|
||||
|
||||

|
||||
@@ -239,6 +239,20 @@ To set up Shumai locally for development:
|
||||
|
||||
Shumai provides a Command Line Interface (CLI) tool to manage projects, folders, and assets, upload files/folders, and create new versions directly from your terminal.
|
||||
|
||||
Install the standalone CLI on macOS/Linux without Node.js or Bun:
|
||||
|
||||
```sh
|
||||
curl -fsSL https://github.com/shumaiOne/shumai/releases/latest/download/install.sh | sh
|
||||
```
|
||||
|
||||
On Windows:
|
||||
|
||||
```powershell
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -Command "irm https://github.com/shumaiOne/shumai/releases/latest/download/install.ps1 | iex"
|
||||
```
|
||||
|
||||
The npm installation remains available: `npm install -g @shumai-one/shumai-cli` (Node.js 22.12+).
|
||||
|
||||
For more details on installation and usage, see the [CLI Readme](apps/cli/README.md).
|
||||
|
||||
## MCP Server
|
||||
|
||||
+15
-1
@@ -16,7 +16,7 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a target="_blank" href="https://shumai.one/">官方网站</a> | <a target="_blank" href="https://docs.shumai.one/getting-started/overview">文档(英文)</a> | <a target="_blank" href="https://staging.shumai.one">在线演示</a> | <a target="_blank" href="README.md">English</a>
|
||||
<a target="_blank" href="https://shumai.one/">官方网站</a> | <a target="_blank" href="https://docs.shumai.one/getting-started/overview">文档(英文)</a> | <a target="_blank" href="https://staging.shumai.one">在线演示</a> | <a target="_blank" href="https://shumai.discourse.group/">Discourse 论坛</a> | <a target="_blank" href="README.md">English</a>
|
||||
</p>
|
||||
|
||||

|
||||
@@ -269,6 +269,20 @@ Shumai 提供了独立的转码 Worker 镜像(支持 Docker Hub 与阿里云
|
||||
|
||||
Shumai 自带一个功能完备的命令行工具(CLI),支持你直接在终端里高效管理项目、文件夹、素材,批量上传文件/目录,以及快速创建新版本。
|
||||
|
||||
macOS/Linux 可直接安装独立 CLI,无需安装 Node.js 或 Bun:
|
||||
|
||||
```sh
|
||||
curl -fsSL https://github.com/shumaiOne/shumai/releases/latest/download/install.sh | sh
|
||||
```
|
||||
|
||||
Windows 用户可使用 PowerShell:
|
||||
|
||||
```powershell
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -Command "irm https://github.com/shumaiOne/shumai/releases/latest/download/install.ps1 | iex"
|
||||
```
|
||||
|
||||
仍可通过 npm 安装:`npm install -g @shumai-one/shumai-cli`(需要 Node.js 22.12 或更高版本)。
|
||||
|
||||
关于 CLI 的安装与详细使用指南,请参阅 [CLI Readme](apps/cli/README.md)。
|
||||
|
||||
## MCP 服务器
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
## Reporting a Vulnerability
|
||||
|
||||
Please report security vulnerabilities **privately**. Do not open a public issue,
|
||||
pull request or Discord message for a suspected vulnerability, as that discloses it
|
||||
pull request, or public forum post for a suspected vulnerability, as that discloses it
|
||||
to everyone before a fix exists.
|
||||
|
||||
Use **[GitHub private vulnerability reporting](https://github.com/shumaiOne/shumai/security/advisories/new)**
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
{
|
||||
"name": "@shumai/agent-app",
|
||||
"version": "0.6.5",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"bin": {
|
||||
"shumai-agent": "./src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@shumai/agent": "workspace:*",
|
||||
"@shumai/core": "workspace:*",
|
||||
"@shumai/workflow-core": "workspace:*"
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import { loadEnvConfig } from '@shumai/core/src/env-loader'
|
||||
loadEnvConfig(process.cwd(), process.env.NODE_ENV !== 'production')
|
||||
|
||||
import { workflowService } from '@shumai/workflow-core'
|
||||
import { TaskQueueAgent } from '@shumai/workflow-core'
|
||||
import { initAgentWorkflows } from '@shumai/agent'
|
||||
import { sandboxService } from '@shumai/core'
|
||||
import { handleDaemonCommands } from '@shumai/core/src/utils/daemon'
|
||||
|
||||
if (process.argv.includes('--check')) {
|
||||
console.log('✅ Agent worker evaluated successfully!')
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
async function run() {
|
||||
// Initialize workflows and activities
|
||||
initAgentWorkflows()
|
||||
await sandboxService.ensureInitialized().catch((err) => {
|
||||
console.error('Failed to eagerly initialize sandbox in agent worker:', err)
|
||||
})
|
||||
|
||||
console.log('🚀 Starting Agent Worker...')
|
||||
|
||||
await workflowService.startWorkers(TaskQueueAgent)
|
||||
}
|
||||
|
||||
handleDaemonCommands('shumai-agent', run).catch((err) => {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
+118
-3
@@ -8,7 +8,65 @@ The CLI allows you to view projects, list folder contents, create folders, uploa
|
||||
|
||||
## Installation
|
||||
|
||||
The CLI package is bundled inside the Shumai workspace. The published npm package (`@shumai-one/shumai-cli`) requires Node.js 22.12 or later.
|
||||
### Standalone executable
|
||||
|
||||
Install the CLI without Node.js or Bun. The installers download a platform-specific executable from GitHub Releases and verify its SHA-256 checksum.
|
||||
|
||||
**macOS / Linux:**
|
||||
|
||||
```sh
|
||||
curl -fsSL https://github.com/shumaiOne/shumai/releases/latest/download/install.sh | sh
|
||||
```
|
||||
|
||||
**Windows (PowerShell):**
|
||||
|
||||
```powershell
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -Command "irm https://github.com/shumaiOne/shumai/releases/latest/download/install.ps1 | iex"
|
||||
```
|
||||
|
||||
| Platform | Architectures | Requirements |
|
||||
| --- | --- | --- |
|
||||
| macOS | Apple Silicon, Intel | macOS 13 or later |
|
||||
| Linux | ARM64, x64 | glibc 2.17 or later; kernel 5.6+ recommended |
|
||||
| Windows | ARM64, x64 | Windows 10 version 1809 or later |
|
||||
|
||||
Native builds use Bun 1.4.4's runtime; x64 CPUs need SSE4.2. See [Bun's system requirements](https://bun.com/docs/installation). Alpine/musl is not included in these native downloads. Native downloads are available for releases containing the installer assets; use npm for earlier releases.
|
||||
|
||||
On macOS/Linux the default directory is `~/.local/bin`. If it is missing from your current PATH, the installer prints a command to run in your current Bash/Zsh terminal:
|
||||
|
||||
```sh
|
||||
export PATH="$HOME/.local/bin:$PATH"
|
||||
```
|
||||
|
||||
To keep the CLI available in future terminals, add the same export line to `~/.bashrc` (Bash) or `~/.zshrc` (Zsh). For Bash login shells, use your existing login profile, such as `~/.bash_profile` or `~/.profile`. Open a new terminal after saving. If you already added it, you do not need to add it again. For a custom installation directory, use the exact command printed by the installer.
|
||||
|
||||
On Windows the installer uses `%LOCALAPPDATA%\Shumai\bin` and updates the user PATH. Open a new terminal afterward. Administrator access is not required.
|
||||
|
||||
Re-run the installer to upgrade. To pin a version, roll back, or choose an installation directory, download the installer and pass its options (replace `X.Y.Z` with a release containing native binaries):
|
||||
|
||||
```sh
|
||||
curl -fsSL https://github.com/shumaiOne/shumai/releases/latest/download/install.sh -o install.sh
|
||||
sh install.sh --version X.Y.Z --install-dir "$HOME/.local/bin"
|
||||
```
|
||||
|
||||
```powershell
|
||||
Invoke-WebRequest https://github.com/shumaiOne/shumai/releases/latest/download/install.ps1 -OutFile install.ps1
|
||||
& .\install.ps1 -Version X.Y.Z -InstallDir "$env:LOCALAPPDATA\Shumai\bin"
|
||||
```
|
||||
|
||||
The installers preserve the existing executable if download or verification fails and refuse to replace unmanaged installations. If an npm installation takes precedence on PATH, uninstall it with `npm uninstall -g @shumai-one/shumai-cli` or adjust PATH.
|
||||
|
||||
To uninstall a native installation, remove `shumai-cli` (`shumai-cli.exe` on Windows) and `.shumai-cli-install` from its installation directory. Remove the PATH entry if it is no longer needed.
|
||||
|
||||
### npm
|
||||
|
||||
The existing npm package continues to run on Node.js 22.12 or later:
|
||||
|
||||
```sh
|
||||
npm install -g @shumai-one/shumai-cli
|
||||
```
|
||||
|
||||
`shumai-cli --version` reports the installed version for either installation method.
|
||||
|
||||
### Environment Setup
|
||||
|
||||
@@ -28,10 +86,48 @@ export SHUMAI_ALLOW_DELETE="true"
|
||||
|
||||
---
|
||||
|
||||
## Agent runner
|
||||
|
||||
Create a runner in **Settings → Agent Runners** and copy its one-time key. On macOS, Linux, or Windows, start it in an existing dedicated workspace:
|
||||
|
||||
```sh
|
||||
export SHUMAI_API_SERVER="https://shumai.example.com"
|
||||
export SHUMAI_RUNNER_KEY="your-runner-key"
|
||||
shumai-cli runner start --cwd /path/to/workspace
|
||||
```
|
||||
|
||||
For Windows PowerShell:
|
||||
|
||||
```powershell
|
||||
$env:SHUMAI_API_SERVER = "https://shumai.example.com"
|
||||
$env:SHUMAI_RUNNER_KEY = "your-runner-key"
|
||||
shumai-cli runner start --cwd C:\workspace
|
||||
```
|
||||
|
||||
Use `--url` or `--key` to override those variables. Pass `-v` or `--verbose` to log executed commands, output sizes, and file transfer paths (by default, only lifecycle events, execution IDs, exit status, and durations are printed to stderr). A runner key is separate from `SHUMAI_API_KEY`. The runner stays in the foreground; Ctrl+C stops it and its active commands. Remote servers require HTTPS; localhost and `host.docker.internal` support HTTP for development. No inbound port is opened.
|
||||
|
||||
Commands use `/bin/sh` on macOS and Linux. On Windows, the runner checks for Git Bash in the standard installation directories, then for `bash.exe` on PATH, then falls back to `cmd.exe`. It prints the selected shell at startup and reports it to the agent. Commands run with the local user's permissions unless SRT is requested.
|
||||
|
||||
[Anthropic Sandbox Runtime](https://github.com/anthropics/sandbox-runtime) is bundled with the CLI. Install its platform prerequisites (Linux: bubblewrap, socat, and ripgrep; macOS: ripgrep), then pass its native JSON settings:
|
||||
|
||||
```sh
|
||||
shumai-cli runner start --srt sandbox.json
|
||||
```
|
||||
|
||||
Windows sandboxing uses SRT's alpha Windows support. Use Git Bash if your selected shell is the legacy WSL Bash launcher. Run the one-time SRT setup before starting a sandboxed runner (it requests administrator permission):
|
||||
|
||||
```powershell
|
||||
npx @anthropic-ai/sandbox-runtime windows-install
|
||||
```
|
||||
|
||||
SRT settings are read and validated at startup. Invalid settings or unavailable sandbox prerequisites stop startup; commands never fall back to unrestricted execution. Only shell commands and their children are sandboxed. Runner connections and server-directed file transfers stay outside SRT, and no server domain is automatically allowed. The shared SRT runtime is cleaned up when the runner stops.
|
||||
|
||||
See [Agent Runners](../../docs/using-shumai/agent-sandbox.mdx) for configuration, Docker usage, limits, and security boundaries.
|
||||
|
||||
## Usage
|
||||
|
||||
> [!IMPORTANT]
|
||||
> **For Bun Users:** Since the package binary contains a `#!/usr/bin/env node` shebang, running `shumai-cli` directly will run under Node.js. If you wish to run it using Bun, prefix the command with `bun run --bun` (e.g., `bun run --bun shumai-cli project ls`).
|
||||
> **For npm installations using Bun:** The npm entry point uses `#!/usr/bin/env node`. Use `bun run --bun shumai-cli project ls` to run that package under Bun. Standalone installations already include their runtime.
|
||||
|
||||
Run `shumai-cli` or query specific commands:
|
||||
|
||||
@@ -49,7 +145,7 @@ When the output is piped instead of shown in a terminal, tables are printed as t
|
||||
|
||||
### Commands
|
||||
|
||||
#### 1. List Projects
|
||||
#### 1. List and Create Projects
|
||||
|
||||
List all projects you have access to. Returns the project ID, name, and its root folder ID.
|
||||
|
||||
@@ -57,6 +153,12 @@ List all projects you have access to. Returns the project ID, name, and its root
|
||||
shumai-cli project ls
|
||||
```
|
||||
|
||||
Create a new project (returns the project ID and its root folder ID):
|
||||
|
||||
```bash
|
||||
shumai-cli project create "Project Name"
|
||||
```
|
||||
|
||||
#### 2. List Directory Contents
|
||||
|
||||
List direct children (folders and files) of a parent folder. Folders are listed first.
|
||||
@@ -258,3 +360,16 @@ bun run apps/cli/src/index.ts project ls
|
||||
### Adding Commands
|
||||
|
||||
Most commands are generated from the shared Operation definitions in `packages/operations` (see "Operations" in `AGENTS.md`). An Operation with the `cli` surface and a `cli` spec becomes a command automatically, with arguments, options, and help text derived from its input schema (key/value inputs such as `meta set --values` take a JSON object). It is also available to Shumai's built-in agent and MCP server. Behavior that needs the local machine, such as downloading files, goes in a CLI extension in `src/operations/extensions/` (extra options, input mapping, and output). Only commands that stream local files (`upload`, `create-version`) are written by hand in `src/program.ts`; the `create_file`, `create_version`, and `complete_upload` Operations are for the agent and MCP only.
|
||||
|
||||
### Building and verifying native releases
|
||||
|
||||
```sh
|
||||
bun run build:cli --target darwin-arm64 # or darwin-x64, linux-arm64, linux-x64, windows-arm64, windows-x64
|
||||
bun run test:cli-release
|
||||
```
|
||||
|
||||
Without `--target`, the build produces all six targets. Output defaults to `out/cli-release`. Set `--out /absolute/path` on either command to change it. Unix archives require `tar`. Creating Windows archives on Unix also requires `zip`. The test exercises installation, runner shell quoting, authenticated API calls, file transfers, and failure recovery. Install Git Bash to run the quoting check on Windows. With SRT configured, pass `--srt /absolute/path/to/settings.json` to repeat the quoting check in the sandbox.
|
||||
|
||||
The Native CLI workflow builds and tests each target on a matching runner. The npm release workflow assembles those tested archives, stages a draft GitHub Release, publishes npm packages, then publishes the completed release. Its `validate_only` dispatch input builds and tests without publishing. Published assets are verified and preserved on retries; draft assets can be replaced while resuming a failed run. Prerelease npm packages use the `next` tag and prerelease GitHub Releases do not become latest.
|
||||
|
||||
Installers use `SHUMAI_CLI_RELEASE_BASE_URL` only when explicitly set, for private mirrors or local testing. It should point to a release download base whose children are `vX.Y.Z/<asset>`. Native executables do not automatically load `.env` or `bunfig.toml`; export the API configuration explicitly as shown above.
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
The Windows Bash discovery and related tests include material adapted from Pi.
|
||||
https://github.com/earendil-works/pi
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Mario Zechner
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+11
-1
@@ -1,19 +1,29 @@
|
||||
{
|
||||
"name": "@shumai/cli",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"bin": {
|
||||
"shumai-cli": "./src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sandbox-runtime": "0.0.83",
|
||||
"@shumai/api": "workspace:*",
|
||||
"@shumai/dtos": "workspace:*",
|
||||
"@shumai/operations": "workspace:*",
|
||||
"@shumai/tableprinter": "workspace:*",
|
||||
"commander": "15.0.0",
|
||||
"execa": "10.2.1",
|
||||
"hono": "4.13.5",
|
||||
"ulid": "3.0.2",
|
||||
"ws": "8.22.2",
|
||||
"yauzl": "3.4.0",
|
||||
"zod": "4.5.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/adm-zip": "0.5.8",
|
||||
"@types/ws": "8.18.2",
|
||||
"@types/yauzl": "3.4.0",
|
||||
"adm-zip": "0.6.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
copyAssets,
|
||||
createComment,
|
||||
createFolder,
|
||||
createProject,
|
||||
createVersionStack,
|
||||
deleteAsset,
|
||||
downloadAsset,
|
||||
@@ -25,14 +26,17 @@ import {
|
||||
import type { CallOperation } from './operations/call-operation'
|
||||
import { CliError, formatCliError } from './operations/errors'
|
||||
import { createProgram } from './program'
|
||||
import { version } from '../package.json'
|
||||
|
||||
/** Runs the CLI with mocked Operation calls and captures stdout/stderr. */
|
||||
function setup() {
|
||||
let stdout = ''
|
||||
let stderr = ''
|
||||
const callOperation = vi.fn<CallOperation>()
|
||||
const startRunner = vi.fn(async () => {})
|
||||
const program = createProgram({
|
||||
callOperation,
|
||||
startRunner,
|
||||
output: { out: { write: (s) => void (stdout += s) }, isTty: true, columns: 120 },
|
||||
})
|
||||
const configure = (cmd: Command) => {
|
||||
@@ -45,6 +49,7 @@ function setup() {
|
||||
configure(program)
|
||||
return {
|
||||
callOperation,
|
||||
startRunner,
|
||||
run: (...argv: string[]) => program.parseAsync(['node', 'shumai-cli', ...argv]),
|
||||
stdout: () => stdout,
|
||||
stderr: () => stderr,
|
||||
@@ -75,6 +80,13 @@ const pendingRead = {
|
||||
}
|
||||
|
||||
describe('shumai-cli', () => {
|
||||
it('reports its package version without calling the server', async () => {
|
||||
const cli = setup()
|
||||
await expect(cli.run('--version')).rejects.toMatchObject({ code: 'commander.version' })
|
||||
expect(cli.stdout()).toBe(`${version}\n`)
|
||||
expect(cli.callOperation).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
const originalAllowDelete = process.env.SHUMAI_ALLOW_DELETE
|
||||
|
||||
afterEach(() => {
|
||||
@@ -110,6 +122,31 @@ describe('shumai-cli', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('project create', () => {
|
||||
it('creates a project with positional name and optional --team', async () => {
|
||||
const cli = setup()
|
||||
cli.callOperation.mockResolvedValue({
|
||||
id: 'p-new',
|
||||
name: 'Summer Shoot',
|
||||
teamId: 't1',
|
||||
rootFolderId: 'root-new',
|
||||
})
|
||||
|
||||
await cli.run('project', 'create', 'Summer Shoot')
|
||||
|
||||
expect(cli.callOperation).toHaveBeenCalledWith(createProject, { name: 'Summer Shoot' })
|
||||
expect(cli.stdout()).toBe(
|
||||
'Created project "Summer Shoot" (ID: p-new, rootFolderId: root-new)\n',
|
||||
)
|
||||
|
||||
await cli.run('project', 'create', 'Team Shoot', '-t', 't2', '--json')
|
||||
expect(cli.callOperation).toHaveBeenLastCalledWith(createProject, {
|
||||
name: 'Team Shoot',
|
||||
teamId: 't2',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('ls', () => {
|
||||
it('lists a folder with the maximum page size, folders first', async () => {
|
||||
const cli = setup()
|
||||
@@ -823,6 +860,23 @@ describe('shumai-cli', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('runner start', () => {
|
||||
it('documents and forwards the -v, --verbose flag', async () => {
|
||||
const cli = setup()
|
||||
await expect(cli.run('runner', 'start', '--help')).rejects.toThrow()
|
||||
expect(cli.stdout()).toContain('-v, --verbose')
|
||||
|
||||
await cli.run('runner', 'start')
|
||||
expect(cli.startRunner).toHaveBeenLastCalledWith({})
|
||||
|
||||
await cli.run('runner', 'start', '-v')
|
||||
expect(cli.startRunner).toHaveBeenLastCalledWith({ verbose: true })
|
||||
|
||||
await cli.run('runner', 'start', '--verbose')
|
||||
expect(cli.startRunner).toHaveBeenLastCalledWith({ verbose: true })
|
||||
})
|
||||
})
|
||||
|
||||
it('has no upload Operation commands: upload and create-version stream local files', () => {
|
||||
const program = createProgram({ callOperation: vi.fn<CallOperation>() })
|
||||
const names = program.commands.map((c) => c.name())
|
||||
|
||||
@@ -6,10 +6,14 @@ import { callOperation, type CallOperation } from './operations/call-operation'
|
||||
import { cliExtensions } from './operations/extensions'
|
||||
import { registerOperationCommands } from './operations/generate'
|
||||
import type { Output } from './operations/render'
|
||||
import { version } from '../package.json'
|
||||
import { startRunner } from './runner/client'
|
||||
import type { RunnerOptions } from './runner/config'
|
||||
|
||||
export interface ProgramOptions {
|
||||
callOperation?: CallOperation
|
||||
output?: Output
|
||||
startRunner?: (options: RunnerOptions) => Promise<void>
|
||||
}
|
||||
|
||||
function isDeleteAllowed(): boolean {
|
||||
@@ -26,6 +30,7 @@ export function createProgram(options: ProgramOptions = {}): Command {
|
||||
const program = new Command()
|
||||
.name('shumai-cli')
|
||||
.description('Command line interface for the Shumai media workspace.')
|
||||
.version(version)
|
||||
.option('--json', 'Print raw JSON output instead of human-friendly text')
|
||||
.showHelpAfterError()
|
||||
.showSuggestionAfterError()
|
||||
@@ -41,6 +46,18 @@ export function createProgram(options: ProgramOptions = {}): Command {
|
||||
extensions: cliExtensions,
|
||||
})
|
||||
|
||||
program
|
||||
.command('runner')
|
||||
.description('Connect a local command runner to Shumai.')
|
||||
.command('start')
|
||||
.description('Run in the foreground using a key created in Settings → Agent Runners.')
|
||||
.option('--url <url>', 'Shumai server URL (or SHUMAI_API_SERVER).')
|
||||
.option('--key <key>', 'Runner key (prefer SHUMAI_RUNNER_KEY to avoid shell history).')
|
||||
.option('--cwd <directory>', 'Runner workspace directory (defaults to the current directory).')
|
||||
.option('--srt <file>', 'Sandbox commands with bundled SRT using this JSON settings file.')
|
||||
.option('-v, --verbose', 'Show command details, output sizes, and file transfer paths.')
|
||||
.action((opts: RunnerOptions) => (options.startRunner ?? startRunner)(opts))
|
||||
|
||||
program
|
||||
.command('upload')
|
||||
.description('Upload a local file or folder to a parent folder.')
|
||||
|
||||
@@ -0,0 +1,446 @@
|
||||
import { createServer } from 'node:http'
|
||||
import { mkdtemp, rm } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import path from 'node:path'
|
||||
import { WebSocketServer, type WebSocket } from 'ws'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { runnerToServerMessageSchema, type RunnerToServerMessage } from '@shumai/dtos'
|
||||
import { RunnerClient } from './client'
|
||||
import { RunnerFiles } from './files'
|
||||
import { RunnerShell } from './shell'
|
||||
|
||||
describe('runner WebSocket client', () => {
|
||||
let cwd: string
|
||||
let server: WebSocketServer
|
||||
let runner: RunnerClient
|
||||
let running: Promise<void>
|
||||
let socket: WebSocket
|
||||
let messages: RunnerToServerMessage[]
|
||||
let welcomeInFlight: string[]
|
||||
let connections: number
|
||||
let replacePrevious: boolean
|
||||
let connectRunner: (options?: { verbose?: boolean; log?: (line: string) => void }) => RunnerClient
|
||||
beforeEach(async () => {
|
||||
cwd = await mkdtemp(path.join(tmpdir(), 'shumai-client-test-'))
|
||||
messages = []
|
||||
welcomeInFlight = []
|
||||
connections = 0
|
||||
replacePrevious = false
|
||||
server = new WebSocketServer({ host: '127.0.0.1', port: 0 })
|
||||
await new Promise<void>((resolve) => server.once('listening', resolve))
|
||||
const address = server.address()
|
||||
if (!address || typeof address === 'string') throw new Error('No WebSocket address')
|
||||
server.on('connection', (connection, request) => {
|
||||
expect(request.headers.authorization).toBe('Bearer runner-key')
|
||||
if (replacePrevious) socket.close(4000, 'Replaced by a newer connection')
|
||||
socket = connection
|
||||
connections++
|
||||
connection.on('message', (raw) =>
|
||||
messages.push(runnerToServerMessageSchema.parse(JSON.parse(raw.toString()))),
|
||||
)
|
||||
connection.send(
|
||||
JSON.stringify({
|
||||
type: 'welcome',
|
||||
runnerId: 'runner',
|
||||
runnerName: 'Test',
|
||||
serverTime: Date.now(),
|
||||
inFlightExecutionIds: welcomeInFlight,
|
||||
}),
|
||||
)
|
||||
})
|
||||
connectRunner = (options) => {
|
||||
const files = new RunnerFiles(cwd)
|
||||
return new RunnerClient(
|
||||
{
|
||||
url: new URL(`ws://127.0.0.1:${address.port}/api/runners/ws`),
|
||||
key: 'runner-key',
|
||||
cwd,
|
||||
srt: undefined,
|
||||
verbose: options?.verbose ?? false,
|
||||
},
|
||||
files,
|
||||
new RunnerShell(files),
|
||||
options?.log ?? (() => {}),
|
||||
)
|
||||
}
|
||||
runner = connectRunner()
|
||||
running = runner.run()
|
||||
// Attach an error handler immediately, including for tests exercising terminal errors.
|
||||
void running.catch(() => {})
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'reconcile')).toBe(true),
|
||||
)
|
||||
})
|
||||
afterEach(async () => {
|
||||
runner.stop()
|
||||
await running.catch(() => {})
|
||||
for (const connection of server.clients) connection.terminate()
|
||||
await new Promise<void>((resolve) => server.close(() => resolve()))
|
||||
await rm(cwd, { recursive: true, force: true })
|
||||
})
|
||||
const send = (message: unknown) => socket.send(JSON.stringify(message))
|
||||
|
||||
it('responds to heartbeat, executes a command once and streams byte offsets', async () => {
|
||||
expect(messages).toContainEqual(
|
||||
expect.objectContaining({
|
||||
type: 'reconcile',
|
||||
environment: { platform: process.platform, shell: '/bin/sh' },
|
||||
}),
|
||||
)
|
||||
send({ type: 'ping', ts: 123 })
|
||||
const command = {
|
||||
type: 'exec.start',
|
||||
executionId: 'one',
|
||||
command: 'printf "世界"; printf "error" >&2',
|
||||
}
|
||||
send(command)
|
||||
send(command)
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'exec.result')).toBe(true),
|
||||
)
|
||||
expect(messages).toContainEqual({ type: 'pong', ts: 123 })
|
||||
expect(messages.filter((message) => message.type === 'exec.result')).toHaveLength(1)
|
||||
const chunks = messages.filter((message) => message.type === 'exec.output')
|
||||
expect(chunks.map((chunk) => chunk.data).join('')).toContain('世界')
|
||||
let offset = 0
|
||||
for (const chunk of chunks) {
|
||||
expect(chunk.offset).toBe(offset)
|
||||
offset += Buffer.byteLength(chunk.data)
|
||||
}
|
||||
})
|
||||
|
||||
it('cancels an active command through the protocol', async () => {
|
||||
send({ type: 'exec.start', executionId: 'cancel', command: 'echo ready; sleep 30' })
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'exec.output')).toBe(true),
|
||||
)
|
||||
send({ type: 'exec.cancel', executionId: 'cancel' })
|
||||
await vi.waitFor(() =>
|
||||
expect(messages).toContainEqual(
|
||||
expect.objectContaining({ type: 'exec.result', executionId: 'cancel', aborted: true }),
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
it('reconnects and replays output before completing an in-flight command', async () => {
|
||||
send({
|
||||
type: 'exec.start',
|
||||
executionId: 'reconnect',
|
||||
command: 'echo before; sleep 0.2; echo after',
|
||||
})
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'exec.output')).toBe(true),
|
||||
)
|
||||
welcomeInFlight = ['reconnect']
|
||||
socket.terminate()
|
||||
messages = []
|
||||
await vi.waitFor(() => expect(connections).toBe(2), { timeout: 5000 })
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'reconcile')).toBe(true),
|
||||
)
|
||||
const reconciliation = messages.findIndex((message) => message.type === 'reconcile')
|
||||
expect(
|
||||
messages
|
||||
.slice(0, reconciliation)
|
||||
.filter((message) => message.type === 'exec.output')
|
||||
.map((message) => message.data)
|
||||
.join(''),
|
||||
).toContain('after')
|
||||
expect(messages[reconciliation]).toMatchObject({
|
||||
completedExecutions: [expect.objectContaining({ executionId: 'reconnect', exitCode: 0 })],
|
||||
})
|
||||
})
|
||||
|
||||
it('handles file requests and returns a cached result for a duplicate request', async () => {
|
||||
const request = { type: 'file.stat', executionId: 'stat', srcPath: '.pi/missing' }
|
||||
send(request)
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'file.stat.result')).toBe(true),
|
||||
)
|
||||
send(request)
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.filter((message) => message.type === 'file.stat.result')).toHaveLength(2),
|
||||
)
|
||||
expect(messages.at(-1)).toMatchObject({ ok: true, exists: false })
|
||||
})
|
||||
|
||||
it('stops instead of reconnecting when the runner key is revoked', async () => {
|
||||
socket.close(1008, 'revoked')
|
||||
await expect(running).rejects.toThrow('revoked')
|
||||
expect(connections).toBe(1)
|
||||
})
|
||||
|
||||
it('stops the superseded client when a second runner connects with the same key', async () => {
|
||||
replacePrevious = true
|
||||
let replaced: unknown
|
||||
void running.catch((error: unknown) => {
|
||||
replaced = error
|
||||
})
|
||||
const replacement = connectRunner()
|
||||
const replacementRun = replacement.run()
|
||||
void replacementRun.catch(() => {})
|
||||
try {
|
||||
await vi.waitFor(() => expect(replaced).toBeInstanceOf(Error))
|
||||
await expect(running).rejects.toThrow('replaced')
|
||||
expect(connections).toBe(2)
|
||||
} finally {
|
||||
replacement.stop()
|
||||
await replacementRun.catch(() => {})
|
||||
}
|
||||
})
|
||||
|
||||
it('rejects invalid messages and path-like execution IDs', async () => {
|
||||
send({ type: 'exec.start', executionId: '../escape', command: 'echo unsafe' })
|
||||
await expect(running).rejects.toThrow('invalid runner protocol')
|
||||
expect(messages.some((message) => message.type === 'exec.output')).toBe(false)
|
||||
})
|
||||
|
||||
it('logs compact lifecycle, bash, and transfer summaries in default mode', async () => {
|
||||
runner.stop()
|
||||
await running.catch(() => {})
|
||||
const logs: string[] = []
|
||||
messages = []
|
||||
runner = connectRunner({ verbose: false, log: (line) => logs.push(line) })
|
||||
running = runner.run()
|
||||
void running.catch(() => {})
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'reconcile')).toBe(true),
|
||||
)
|
||||
|
||||
send({ type: 'exec.start', executionId: '01JTEST0000A8F2', command: 'printf "hello"' })
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'exec.result')).toBe(true),
|
||||
)
|
||||
|
||||
send({
|
||||
type: 'exec.start',
|
||||
executionId: '01JTEST0000TOUT',
|
||||
command: 'sleep 5',
|
||||
timeoutSec: 0.1,
|
||||
})
|
||||
await vi.waitFor(() =>
|
||||
expect(
|
||||
messages.some(
|
||||
(message) => message.type === 'exec.result' && message.executionId === '01JTEST0000TOUT',
|
||||
),
|
||||
).toBe(true),
|
||||
)
|
||||
|
||||
send({ type: 'file.stat', executionId: '01JTEST0000STAT', srcPath: '.pi/missing' })
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'file.stat.result')).toBe(true),
|
||||
)
|
||||
|
||||
const httpServer = createServer((_request, response) => {
|
||||
response.end('payload')
|
||||
})
|
||||
await new Promise<void>((resolve) => httpServer.listen(0, '127.0.0.1', resolve))
|
||||
try {
|
||||
const httpAddress = httpServer.address()
|
||||
if (!httpAddress || typeof httpAddress === 'string') throw new Error('No HTTP address')
|
||||
send({
|
||||
type: 'file.download',
|
||||
executionId: '01JTEST0000C3D9',
|
||||
url: `http://127.0.0.1:${httpAddress.port}/file`,
|
||||
destPath: '.pi/assets/file.txt',
|
||||
})
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'file.download.result')).toBe(true),
|
||||
)
|
||||
} finally {
|
||||
httpServer.closeAllConnections()
|
||||
await new Promise<void>((resolve) => httpServer.close(() => resolve()))
|
||||
}
|
||||
|
||||
expect(
|
||||
logs.some((line) =>
|
||||
/^\d{2}:\d{2}:\d{2} INFO {2}Runner connected: Test \(runner\)$/.test(line),
|
||||
),
|
||||
).toBe(true)
|
||||
expect(
|
||||
logs.some((line) => /^\d{2}:\d{2}:\d{2} INFO {2}Bash started \[a8f2\]$/.test(line)),
|
||||
).toBe(true)
|
||||
expect(
|
||||
logs.some((line) =>
|
||||
/^\d{2}:\d{2}:\d{2} INFO {2}Bash completed \[a8f2\] exit=0 duration=\d+\.\ds$/.test(line),
|
||||
),
|
||||
).toBe(true)
|
||||
expect(
|
||||
logs.some((line) =>
|
||||
/^\d{2}:\d{2}:\d{2} WARN {2}Bash timed out \[tout\] duration=\d+\.\ds$/.test(line),
|
||||
),
|
||||
).toBe(true)
|
||||
expect(
|
||||
logs.some((line) =>
|
||||
/^\d{2}:\d{2}:\d{2} INFO {2}File download completed \[c3d9\] size=7B duration=\d+\.\ds$/.test(
|
||||
line,
|
||||
),
|
||||
),
|
||||
).toBe(true)
|
||||
expect(logs.join('\n')).not.toContain('command=')
|
||||
expect(logs.join('\n')).not.toContain('File stat')
|
||||
expect(logs.join('\n')).not.toContain('.pi/assets/file.txt')
|
||||
})
|
||||
|
||||
it('logs commands, stream sizes, and file paths in verbose mode', async () => {
|
||||
runner.stop()
|
||||
await running.catch(() => {})
|
||||
const logs: string[] = []
|
||||
messages = []
|
||||
runner = connectRunner({ verbose: true, log: (line) => logs.push(line) })
|
||||
running = runner.run()
|
||||
void running.catch(() => {})
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'reconcile')).toBe(true),
|
||||
)
|
||||
|
||||
send({
|
||||
type: 'exec.start',
|
||||
executionId: '01JTEST0000B9E1',
|
||||
command: 'printf "hi"; printf "err" >&2',
|
||||
})
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'exec.result')).toBe(true),
|
||||
)
|
||||
|
||||
send({ type: 'file.stat', executionId: '01JTEST0000B1A4', srcPath: '.pi/missing' })
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'file.stat.result')).toBe(true),
|
||||
)
|
||||
|
||||
const httpServer = createServer((_request, response) => {
|
||||
response.writeHead(500)
|
||||
response.end()
|
||||
})
|
||||
await new Promise<void>((resolve) => httpServer.listen(0, '127.0.0.1', resolve))
|
||||
try {
|
||||
const httpAddress = httpServer.address()
|
||||
if (!httpAddress || typeof httpAddress === 'string') throw new Error('No HTTP address')
|
||||
send({
|
||||
type: 'file.download',
|
||||
executionId: '01JTEST0000E5F1',
|
||||
url: `http://127.0.0.1:${httpAddress.port}/fail`,
|
||||
destPath: '.pi/assets/fail.txt',
|
||||
})
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'file.download.result')).toBe(true),
|
||||
)
|
||||
} finally {
|
||||
httpServer.closeAllConnections()
|
||||
await new Promise<void>((resolve) => httpServer.close(() => resolve()))
|
||||
}
|
||||
|
||||
expect(
|
||||
logs.some((line) =>
|
||||
/^\d{2}:\d{2}:\d{2} INFO {2}Bash started \[b9e1\] command="printf \\"hi\\"; printf \\"err\\" >&2"$/.test(
|
||||
line,
|
||||
),
|
||||
),
|
||||
).toBe(true)
|
||||
expect(
|
||||
logs.some((line) =>
|
||||
/^\d{2}:\d{2}:\d{2} INFO {2}Bash completed \[b9e1\] exit=0 duration=\d+\.\ds stdout=2B stderr=3B$/.test(
|
||||
line,
|
||||
),
|
||||
),
|
||||
).toBe(true)
|
||||
expect(
|
||||
logs.some((line) =>
|
||||
/^\d{2}:\d{2}:\d{2} INFO {2}File stat \[b1a4\] path="\.pi\/missing" exists=false$/.test(
|
||||
line,
|
||||
),
|
||||
),
|
||||
).toBe(true)
|
||||
expect(
|
||||
logs.some((line) =>
|
||||
/^\d{2}:\d{2}:\d{2} INFO {2}File download started \[e5f1\] path="\.pi\/assets\/fail\.txt"$/.test(
|
||||
line,
|
||||
),
|
||||
),
|
||||
).toBe(true)
|
||||
expect(
|
||||
logs.some((line) =>
|
||||
/^\d{2}:\d{2}:\d{2} WARN {2}File download failed \[e5f1\] path="\.pi\/assets\/fail\.txt" duration=\d+\.\ds error="Download failed \(HTTP 500\)\."$/.test(
|
||||
line,
|
||||
),
|
||||
),
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
it.each([false, true])(
|
||||
'logs a single busy rejection without false starts at capacity (verbose=%s)',
|
||||
async (verbose) => {
|
||||
runner.stop()
|
||||
await running.catch(() => {})
|
||||
const logs: string[] = []
|
||||
messages = []
|
||||
runner = connectRunner({ verbose, log: (line) => logs.push(line) })
|
||||
running = runner.run()
|
||||
void running.catch(() => {})
|
||||
await vi.waitFor(() =>
|
||||
expect(messages.some((message) => message.type === 'reconcile')).toBe(true),
|
||||
)
|
||||
|
||||
for (let index = 0; index < 8; index++) {
|
||||
send({
|
||||
type: 'exec.start',
|
||||
executionId: `01JACTIVE00000${index}`,
|
||||
command: 'sleep 30',
|
||||
})
|
||||
}
|
||||
await vi.waitFor(() =>
|
||||
expect(logs.filter((line) => line.includes('Bash started'))).toHaveLength(8),
|
||||
)
|
||||
|
||||
send({
|
||||
type: 'exec.start',
|
||||
executionId: '01JBUSYCMD00B123',
|
||||
command: 'echo rejected',
|
||||
})
|
||||
send({
|
||||
type: 'file.upload',
|
||||
executionId: '01JBUSYFILE0U456',
|
||||
srcPath: '.pi/out.bin',
|
||||
url: 'http://127.0.0.1:9/upload',
|
||||
})
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(messages).toContainEqual(
|
||||
expect.objectContaining({
|
||||
type: 'exec.result',
|
||||
executionId: '01JBUSYCMD00B123',
|
||||
error: 'Runner is busy. Try again later.',
|
||||
}),
|
||||
)
|
||||
expect(messages).toContainEqual(
|
||||
expect.objectContaining({
|
||||
type: 'file.upload.result',
|
||||
executionId: '01JBUSYFILE0U456',
|
||||
ok: false,
|
||||
error: 'Runner is busy. Try again later.',
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
const bashLogs = logs.filter((line) => line.includes('[b123]'))
|
||||
const uploadLogs = logs.filter((line) => line.includes('[u456]'))
|
||||
expect(bashLogs).toHaveLength(1)
|
||||
expect(uploadLogs).toHaveLength(1)
|
||||
if (verbose) {
|
||||
expect(bashLogs[0]).toMatch(
|
||||
/^\d{2}:\d{2}:\d{2} WARN {2}Bash rejected \[b123\] command="echo rejected" error="Runner is busy\. Try again later\."$/,
|
||||
)
|
||||
expect(uploadLogs[0]).toMatch(
|
||||
/^\d{2}:\d{2}:\d{2} WARN {2}File upload rejected \[u456\] path="\.pi\/out\.bin" error="Runner is busy\. Try again later\."$/,
|
||||
)
|
||||
} else {
|
||||
expect(bashLogs[0]).toMatch(
|
||||
/^\d{2}:\d{2}:\d{2} WARN {2}Bash rejected \[b123\] \(runner is busy\)$/,
|
||||
)
|
||||
expect(uploadLogs[0]).toMatch(
|
||||
/^\d{2}:\d{2}:\d{2} WARN {2}File upload rejected \[u456\] \(runner is busy\)$/,
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,461 @@
|
||||
import WebSocket from 'ws'
|
||||
import { setTimeout as delay } from 'node:timers/promises'
|
||||
import {
|
||||
RUNNER_REPLACED_CLOSE_CODE,
|
||||
serverToRunnerMessageSchema,
|
||||
type RunnerExecOutputMessage,
|
||||
type RunnerExecResultMessage,
|
||||
type RunnerToServerMessage,
|
||||
type ServerToRunnerMessage,
|
||||
} from '@shumai/dtos'
|
||||
import { CliError } from '../operations/errors'
|
||||
import { resolveRunnerConfig, type RunnerConfig, type RunnerOptions } from './config'
|
||||
import { RunnerFiles, type FileMessage } from './files'
|
||||
import {
|
||||
createRunnerLogger,
|
||||
formatBytes,
|
||||
formatDuration,
|
||||
formatLogString,
|
||||
shortId,
|
||||
type RunnerLogger,
|
||||
} from './log'
|
||||
import { RunnerShell } from './shell'
|
||||
|
||||
const MAX_REPLAY_BYTES = 1024 * 1024
|
||||
const MAX_COMPLETED = 16
|
||||
const MAX_ACTIVE = 8
|
||||
const RUNNER_BUSY_ERROR = 'Runner is busy. Try again later.'
|
||||
const FILE_OPERATION_KINDS: Record<FileMessage['type'], 'download' | 'upload' | 'stat'> = {
|
||||
'file.download': 'download',
|
||||
'file.upload': 'upload',
|
||||
'file.stat': 'stat',
|
||||
}
|
||||
|
||||
interface Execution {
|
||||
controller: AbortController
|
||||
chunks: RunnerExecOutputMessage[]
|
||||
bytes: number
|
||||
offset: number
|
||||
startedAt: number
|
||||
stdoutBytes: number
|
||||
stderrBytes: number
|
||||
result?: RunnerExecResultMessage
|
||||
}
|
||||
|
||||
export class RunnerClient {
|
||||
private socket?: WebSocket
|
||||
private ready = false
|
||||
private stopped = new AbortController()
|
||||
private executions = new Map<string, Execution>()
|
||||
private transfers = new Map<string, AbortController>()
|
||||
private completedFiles = new Map<string, RunnerToServerMessage>()
|
||||
private pending = new Set<Promise<void>>()
|
||||
private readonly logger: RunnerLogger
|
||||
|
||||
constructor(
|
||||
private readonly config: RunnerConfig,
|
||||
private readonly files: RunnerFiles,
|
||||
private readonly shell: RunnerShell,
|
||||
log: (message: string) => void = console.error,
|
||||
) {
|
||||
this.logger = createRunnerLogger(log, Boolean(config.verbose))
|
||||
}
|
||||
|
||||
async run(signal?: AbortSignal): Promise<void> {
|
||||
const stop = () => this.stop()
|
||||
signal?.addEventListener('abort', stop, { once: true })
|
||||
if (signal?.aborted) this.stop()
|
||||
let attempt = 0
|
||||
try {
|
||||
while (!this.stopped.signal.aborted) {
|
||||
const wasReady = await this.connect()
|
||||
if (this.stopped.signal.aborted) break
|
||||
attempt = wasReady ? 0 : Math.min(attempt + 1, 5)
|
||||
this.logger.warn('Runner disconnected; reconnecting…')
|
||||
await delay(Math.min(1000 * 2 ** attempt, 30_000), undefined, {
|
||||
signal: this.stopped.signal,
|
||||
}).catch(() => {})
|
||||
}
|
||||
} finally {
|
||||
signal?.removeEventListener('abort', stop)
|
||||
this.stop()
|
||||
await Promise.allSettled(this.pending)
|
||||
}
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.stopped.abort()
|
||||
this.ready = false
|
||||
this.socket?.terminate()
|
||||
for (const execution of this.executions.values()) execution.controller.abort()
|
||||
for (const transfer of this.transfers.values()) transfer.abort()
|
||||
}
|
||||
|
||||
private connect(): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const socket = new WebSocket(this.config.url, {
|
||||
headers: { authorization: `Bearer ${this.config.key}` },
|
||||
handshakeTimeout: 10_000,
|
||||
maxPayload: 1024 * 1024,
|
||||
perMessageDeflate: false,
|
||||
followRedirects: false,
|
||||
})
|
||||
this.socket = socket
|
||||
let welcomed = false
|
||||
let lastMessage = Date.now()
|
||||
const heartbeat = setInterval(() => {
|
||||
if (Date.now() - lastMessage > (welcomed ? 75_000 : 10_000)) socket.terminate()
|
||||
}, 5_000)
|
||||
socket.on('error', () => {
|
||||
/* Close drives reconnect; never print credential-bearing request errors. */
|
||||
})
|
||||
socket.on('unexpected-response', (_request, response) => {
|
||||
response.destroy()
|
||||
socket.terminate()
|
||||
if ([401, 403, 404, 426].includes(response.statusCode ?? 0))
|
||||
reject(
|
||||
new CliError(
|
||||
`Runner connection rejected (HTTP ${response.statusCode}). Check the server URL and runner key.`,
|
||||
),
|
||||
)
|
||||
})
|
||||
socket.on('close', (code) => {
|
||||
clearInterval(heartbeat)
|
||||
this.ready = false
|
||||
if ((code === 1008 || code === RUNNER_REPLACED_CLOSE_CODE) && !this.stopped.signal.aborted)
|
||||
reject(
|
||||
new CliError('Runner access was revoked or another connection replaced this runner.'),
|
||||
)
|
||||
else resolve(welcomed)
|
||||
})
|
||||
socket.on('message', (raw) => {
|
||||
try {
|
||||
const parsed = serverToRunnerMessageSchema.parse(JSON.parse(raw.toString()))
|
||||
if ('executionId' in parsed && !/^[A-Za-z0-9_-]{1,128}$/.test(parsed.executionId))
|
||||
throw new Error('Invalid execution ID.')
|
||||
lastMessage = Date.now()
|
||||
if (parsed.type === 'welcome') {
|
||||
if (welcomed) throw new Error('Duplicate welcome.')
|
||||
welcomed = true
|
||||
this.ready = true
|
||||
this.reconcile(parsed.inFlightExecutionIds)
|
||||
this.logger.info(`Runner connected: ${parsed.runnerName} (${parsed.runnerId})`)
|
||||
} else {
|
||||
if (!welcomed) throw new Error('Expected welcome before runner requests.')
|
||||
this.handle(parsed)
|
||||
}
|
||||
} catch {
|
||||
reject(new CliError('Server sent an invalid runner protocol message.'))
|
||||
socket.terminate()
|
||||
}
|
||||
})
|
||||
if (this.stopped.signal.aborted) socket.terminate()
|
||||
})
|
||||
}
|
||||
|
||||
private send(message: RunnerToServerMessage) {
|
||||
if (this.ready && this.socket?.readyState === WebSocket.OPEN)
|
||||
this.socket.send(JSON.stringify(message))
|
||||
}
|
||||
|
||||
private reconcile(inFlight: string[]) {
|
||||
const expected = new Set(inFlight)
|
||||
for (const [id, execution] of this.executions) {
|
||||
if (!expected.has(id)) {
|
||||
execution.controller.abort()
|
||||
if (execution.result) this.executions.delete(id)
|
||||
continue
|
||||
}
|
||||
// Replay before results: the server deduplicates chunks by UTF-8 byte offset.
|
||||
for (const chunk of execution.chunks) this.send(chunk)
|
||||
}
|
||||
this.send({
|
||||
type: 'reconcile',
|
||||
environment: this.shell.environment,
|
||||
activeExecutions: [...this.executions]
|
||||
.filter(([id, execution]) => expected.has(id) && !execution.result)
|
||||
.map(([executionId, execution]) => ({ executionId, nextOffset: execution.offset })),
|
||||
completedExecutions: [...this.executions]
|
||||
.filter(([id, execution]) => expected.has(id) && execution.result)
|
||||
.map(([, execution]) => execution.result!),
|
||||
})
|
||||
}
|
||||
|
||||
private handle(message: Exclude<ServerToRunnerMessage, { type: 'welcome' }>) {
|
||||
if (message.type === 'ping') {
|
||||
this.send({ type: 'pong', ts: message.ts })
|
||||
return
|
||||
}
|
||||
if (message.type === 'exec.cancel') {
|
||||
this.executions.get(message.executionId)?.controller.abort()
|
||||
return
|
||||
}
|
||||
if (message.type === 'exec.start') {
|
||||
const previous = this.executions.get(message.executionId)
|
||||
if (previous) {
|
||||
if (previous.result) this.send(previous.result)
|
||||
return
|
||||
}
|
||||
const id = shortId(message.executionId)
|
||||
if (this.activeCount() >= MAX_ACTIVE) {
|
||||
const result: RunnerExecResultMessage = {
|
||||
type: 'exec.result',
|
||||
executionId: message.executionId,
|
||||
exitCode: null,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
error: RUNNER_BUSY_ERROR,
|
||||
}
|
||||
this.recordCompletedExecution(message.executionId, result)
|
||||
this.logger.warn(
|
||||
this.logger.verbose
|
||||
? `Bash rejected [${id}] command=${formatLogString(message.command)} error=${formatLogString(RUNNER_BUSY_ERROR)}`
|
||||
: `Bash rejected [${id}] (runner is busy)`,
|
||||
)
|
||||
this.send(result)
|
||||
return
|
||||
}
|
||||
const execution: Execution = {
|
||||
controller: new AbortController(),
|
||||
chunks: [],
|
||||
bytes: 0,
|
||||
offset: 0,
|
||||
startedAt: performance.now(),
|
||||
stdoutBytes: 0,
|
||||
stderrBytes: 0,
|
||||
}
|
||||
this.executions.set(message.executionId, execution)
|
||||
this.logger.info(
|
||||
this.logger.verbose
|
||||
? `Bash started [${id}] command=${formatLogString(message.command)}`
|
||||
: `Bash started [${id}]`,
|
||||
)
|
||||
this.track(
|
||||
(async () => {
|
||||
const result = await this.shell.run(
|
||||
message,
|
||||
execution.controller.signal,
|
||||
(stream, data) => {
|
||||
// Small frames keep reconnect replay and WebSocket buffering bounded.
|
||||
for (const part of data.match(/[\s\S]{1,8192}/gu) ?? []) {
|
||||
const chunk: RunnerExecOutputMessage = {
|
||||
type: 'exec.output',
|
||||
executionId: message.executionId,
|
||||
stream,
|
||||
data: part,
|
||||
offset: execution.offset,
|
||||
}
|
||||
const bytes = Buffer.byteLength(part)
|
||||
execution.offset += bytes
|
||||
execution.bytes += bytes
|
||||
if (stream === 'stdout') execution.stdoutBytes += bytes
|
||||
else execution.stderrBytes += bytes
|
||||
execution.chunks.push(chunk)
|
||||
while (execution.bytes > MAX_REPLAY_BYTES) {
|
||||
const removed = execution.chunks.shift()!
|
||||
execution.bytes -= Buffer.byteLength(removed.data)
|
||||
if (!this.ready) execution.controller.abort()
|
||||
}
|
||||
if ((this.socket?.bufferedAmount ?? 0) > MAX_REPLAY_BYTES) {
|
||||
this.ready = false
|
||||
this.socket?.terminate()
|
||||
}
|
||||
this.send(chunk)
|
||||
}
|
||||
},
|
||||
)
|
||||
execution.result = result
|
||||
this.logExecResult(id, execution, result)
|
||||
this.send(result)
|
||||
this.pruneCompletedExecutions()
|
||||
})(),
|
||||
)
|
||||
return
|
||||
}
|
||||
this.handleFile(message)
|
||||
}
|
||||
|
||||
private recordCompletedExecution(executionId: string, result: RunnerExecResultMessage) {
|
||||
this.executions.set(executionId, {
|
||||
controller: new AbortController(),
|
||||
chunks: [],
|
||||
bytes: 0,
|
||||
offset: 0,
|
||||
startedAt: performance.now(),
|
||||
stdoutBytes: 0,
|
||||
stderrBytes: 0,
|
||||
result,
|
||||
})
|
||||
this.pruneCompletedExecutions()
|
||||
}
|
||||
|
||||
private pruneCompletedExecutions() {
|
||||
const completed = [...this.executions].filter(([, value]) => value.result)
|
||||
for (const [completedId] of completed.slice(0, -MAX_COMPLETED))
|
||||
this.executions.delete(completedId)
|
||||
}
|
||||
|
||||
private logExecResult(id: string, execution: Execution, result: RunnerExecResultMessage) {
|
||||
const duration = formatDuration(performance.now() - execution.startedAt)
|
||||
const sizes = this.logger.verbose
|
||||
? ` stdout=${formatBytes(execution.stdoutBytes)} stderr=${formatBytes(execution.stderrBytes)}`
|
||||
: ''
|
||||
if (result.timedOut) {
|
||||
const error =
|
||||
this.logger.verbose && result.error ? ` error=${formatLogString(result.error)}` : ''
|
||||
this.logger.warn(`Bash timed out [${id}] duration=${duration}${sizes}${error}`)
|
||||
return
|
||||
}
|
||||
if (result.aborted) {
|
||||
this.logger.info(`Bash cancelled [${id}] duration=${duration}${sizes}`)
|
||||
return
|
||||
}
|
||||
if (result.error) {
|
||||
const error = this.logger.verbose ? ` error=${formatLogString(result.error)}` : ''
|
||||
this.logger.warn(`Bash failed [${id}] duration=${duration}${sizes}${error}`)
|
||||
return
|
||||
}
|
||||
if (result.exitCode === null && result.signal) {
|
||||
this.logger.warn(`Bash killed [${id}] signal=${result.signal} duration=${duration}${sizes}`)
|
||||
return
|
||||
}
|
||||
this.logger.info(`Bash completed [${id}] exit=${result.exitCode} duration=${duration}${sizes}`)
|
||||
}
|
||||
|
||||
private activeCount() {
|
||||
return (
|
||||
[...this.executions.values()].filter((execution) => !execution.result).length +
|
||||
this.transfers.size
|
||||
)
|
||||
}
|
||||
|
||||
private handleFile(message: FileMessage) {
|
||||
const previous = this.completedFiles.get(message.executionId)
|
||||
if (previous) {
|
||||
this.send(previous)
|
||||
return
|
||||
}
|
||||
if (this.transfers.has(message.executionId)) return
|
||||
const id = shortId(message.executionId)
|
||||
const transferPath = message.type === 'file.download' ? message.destPath : message.srcPath
|
||||
if (this.activeCount() >= MAX_ACTIVE) {
|
||||
const result = this.fileBusy(message)
|
||||
this.recordCompletedFile(message.executionId, result)
|
||||
const kind = FILE_OPERATION_KINDS[message.type]
|
||||
this.logger.warn(
|
||||
this.logger.verbose
|
||||
? `File ${kind} rejected [${id}] path=${formatLogString(transferPath)} error=${formatLogString(RUNNER_BUSY_ERROR)}`
|
||||
: `File ${kind} rejected [${id}] (runner is busy)`,
|
||||
)
|
||||
this.send(result)
|
||||
return
|
||||
}
|
||||
const controller = new AbortController()
|
||||
this.transfers.set(message.executionId, controller)
|
||||
const timer = setTimeout(() => controller.abort(), 120_000)
|
||||
const startedAt = performance.now()
|
||||
if (this.logger.verbose && message.type !== 'file.stat') {
|
||||
const kind = message.type === 'file.download' ? 'download' : 'upload'
|
||||
this.logger.info(`File ${kind} started [${id}] path=${formatLogString(transferPath)}`)
|
||||
}
|
||||
this.track(
|
||||
(async () => {
|
||||
try {
|
||||
const result = await this.files.handle(message, controller.signal)
|
||||
this.recordCompletedFile(message.executionId, result)
|
||||
this.logFileResult(id, transferPath, startedAt, result)
|
||||
this.send(result)
|
||||
} finally {
|
||||
clearTimeout(timer)
|
||||
this.transfers.delete(message.executionId)
|
||||
}
|
||||
})(),
|
||||
)
|
||||
}
|
||||
|
||||
private recordCompletedFile(executionId: string, result: RunnerToServerMessage) {
|
||||
this.completedFiles.set(executionId, result)
|
||||
if (this.completedFiles.size > MAX_COMPLETED)
|
||||
this.completedFiles.delete(this.completedFiles.keys().next().value!)
|
||||
}
|
||||
|
||||
private logFileResult(
|
||||
id: string,
|
||||
transferPath: string,
|
||||
startedAt: number,
|
||||
result: RunnerToServerMessage,
|
||||
) {
|
||||
if (result.type === 'file.stat.result') {
|
||||
if (!this.logger.verbose) return
|
||||
const pathPart = `path=${formatLogString(transferPath)}`
|
||||
if (!result.ok) {
|
||||
this.logger.warn(
|
||||
`File stat failed [${id}] ${pathPart} error=${formatLogString(result.error ?? 'Unknown error')}`,
|
||||
)
|
||||
return
|
||||
}
|
||||
const sizePart = result.exists ? ` size=${formatBytes(result.size ?? 0)}` : ''
|
||||
this.logger.info(`File stat [${id}] ${pathPart} exists=${result.exists}${sizePart}`)
|
||||
return
|
||||
}
|
||||
if (result.type !== 'file.download.result' && result.type !== 'file.upload.result') return
|
||||
const kind = result.type === 'file.download.result' ? 'download' : 'upload'
|
||||
const duration = formatDuration(performance.now() - startedAt)
|
||||
const pathPart = this.logger.verbose ? ` path=${formatLogString(transferPath)}` : ''
|
||||
if (result.ok) {
|
||||
this.logger.info(
|
||||
`File ${kind} completed [${id}]${pathPart} size=${formatBytes(result.size ?? 0)} duration=${duration}`,
|
||||
)
|
||||
return
|
||||
}
|
||||
const errorPart = this.logger.verbose
|
||||
? ` error=${formatLogString(result.error ?? 'Unknown error')}`
|
||||
: ''
|
||||
this.logger.warn(`File ${kind} failed [${id}]${pathPart} duration=${duration}${errorPart}`)
|
||||
}
|
||||
|
||||
private fileBusy(message: FileMessage): RunnerToServerMessage {
|
||||
const result = {
|
||||
executionId: message.executionId,
|
||||
ok: false,
|
||||
error: RUNNER_BUSY_ERROR,
|
||||
}
|
||||
switch (message.type) {
|
||||
case 'file.download':
|
||||
return { ...result, type: 'file.download.result' }
|
||||
case 'file.upload':
|
||||
return { ...result, type: 'file.upload.result' }
|
||||
case 'file.stat':
|
||||
return { ...result, type: 'file.stat.result', exists: false }
|
||||
}
|
||||
}
|
||||
|
||||
private track(task: Promise<void>) {
|
||||
this.pending.add(task)
|
||||
void task.finally(() => this.pending.delete(task)).catch(() => this.stop())
|
||||
}
|
||||
}
|
||||
|
||||
export async function startRunner(options: RunnerOptions) {
|
||||
const config = await resolveRunnerConfig(options)
|
||||
const files = new RunnerFiles(config.cwd)
|
||||
const shell = new RunnerShell(files, config.srt)
|
||||
// SRT resolves relative policy paths against the runner workspace.
|
||||
process.chdir(config.cwd)
|
||||
await shell.initialize()
|
||||
createRunnerLogger(console.error, Boolean(config.verbose)).info(
|
||||
`Runner shell: ${shell.environment.shell}`,
|
||||
)
|
||||
const runner = new RunnerClient(config, files, shell)
|
||||
const controller = new AbortController()
|
||||
const shutdown = () => controller.abort()
|
||||
process.once('SIGINT', shutdown)
|
||||
process.once('SIGTERM', shutdown)
|
||||
try {
|
||||
await runner.run(controller.signal)
|
||||
} finally {
|
||||
process.removeListener('SIGINT', shutdown)
|
||||
process.removeListener('SIGTERM', shutdown)
|
||||
await shell.close()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { commandEnv, resolveRunnerConfig } from './config'
|
||||
|
||||
afterEach(() => vi.unstubAllEnvs())
|
||||
|
||||
describe('runner configuration', () => {
|
||||
it('preserves Windows OS settings, normalizes explicit overrides, and excludes credentials', () => {
|
||||
vi.stubEnv('Path', 'C:\\Windows\\System32')
|
||||
vi.stubEnv('SystemRoot', 'C:\\Windows')
|
||||
vi.stubEnv('USERPROFILE', 'C:\\Users\\runner')
|
||||
const env = commandEnv({ PATH: 'C:\\tools', ['shumai_runner_key']: 'secret' })
|
||||
expect(env).toMatchObject({
|
||||
PATH: 'C:\\tools',
|
||||
SystemRoot: 'C:\\Windows',
|
||||
USERPROFILE: 'C:\\Users\\runner',
|
||||
})
|
||||
expect(env).not.toHaveProperty('Path')
|
||||
expect(env).not.toHaveProperty('shumai_runner_key')
|
||||
})
|
||||
it('uses a dedicated runner key and preserves a server path prefix', async () => {
|
||||
vi.stubEnv('SHUMAI_API_SERVER', 'https://example.com/shumai/')
|
||||
vi.stubEnv('SHUMAI_RUNNER_KEY', 'runner-secret')
|
||||
const config = await resolveRunnerConfig({})
|
||||
expect(config.url.href).toBe('wss://example.com/shumai/api/runners/ws')
|
||||
expect(config.key).toBe('runner-secret')
|
||||
expect(config.cwd).toBe(process.cwd())
|
||||
expect(config.verbose).toBe(false)
|
||||
expect((await resolveRunnerConfig({ verbose: true })).verbose).toBe(true)
|
||||
})
|
||||
|
||||
it.each([
|
||||
'http://example.com',
|
||||
'ftp://localhost',
|
||||
'https://user:secret@example.com',
|
||||
'https://example.com/?key=secret',
|
||||
'not a URL',
|
||||
])('rejects unsafe server URL %s', async (url) => {
|
||||
await expect(resolveRunnerConfig({ url, key: 'key' })).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('allows the Docker host development address', async () => {
|
||||
expect(
|
||||
(await resolveRunnerConfig({ url: 'http://host.docker.internal:3000', key: 'key' })).url.href,
|
||||
).toBe('ws://host.docker.internal:3000/api/runners/ws')
|
||||
})
|
||||
|
||||
it('does not accept a personal API token in place of a missing runner key', async () => {
|
||||
vi.stubEnv('SHUMAI_RUNNER_KEY', '')
|
||||
vi.stubEnv('SHUMAI_API_KEY', 'personal-secret')
|
||||
await expect(resolveRunnerConfig({ url: 'https://example.com' })).rejects.toThrow(
|
||||
'SHUMAI_RUNNER_KEY',
|
||||
)
|
||||
})
|
||||
|
||||
it('inherits only baseline environment and explicit skill variables', () => {
|
||||
vi.stubEnv('SHUMAI_RUNNER_KEY', 'runner-secret')
|
||||
vi.stubEnv('SHUMAI_API_KEY', 'personal-secret')
|
||||
vi.stubEnv('AWS_SECRET_ACCESS_KEY', 'aws-secret')
|
||||
vi.stubEnv('BASH_ENV', '/malicious-startup')
|
||||
expect(
|
||||
commandEnv({ SKILL_TOKEN: 'skill-secret', SHUMAI_RUNNER_KEY: 'injected' }),
|
||||
).toMatchObject({ SKILL_TOKEN: 'skill-secret' })
|
||||
for (const name of ['SHUMAI_RUNNER_KEY', 'SHUMAI_API_KEY', 'AWS_SECRET_ACCESS_KEY', 'BASH_ENV'])
|
||||
expect(commandEnv()).not.toHaveProperty(name)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,84 @@
|
||||
import { realpath, stat } from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import { CliError } from '../operations/errors'
|
||||
|
||||
export interface RunnerOptions {
|
||||
url?: string
|
||||
key?: string
|
||||
cwd?: string
|
||||
srt?: string
|
||||
verbose?: boolean
|
||||
}
|
||||
|
||||
export interface RunnerConfig {
|
||||
url: URL
|
||||
key: string
|
||||
cwd: string
|
||||
srt?: string
|
||||
verbose?: boolean
|
||||
}
|
||||
|
||||
export async function resolveRunnerConfig(options: RunnerOptions): Promise<RunnerConfig> {
|
||||
const address = options.url ?? process.env.SHUMAI_API_SERVER
|
||||
const key = options.key ?? process.env.SHUMAI_RUNNER_KEY
|
||||
if (!address) throw new CliError('Set SHUMAI_API_SERVER or pass --url.')
|
||||
if (!key?.trim()) throw new CliError('Set SHUMAI_RUNNER_KEY or pass --key with a runner key.')
|
||||
let url: URL
|
||||
try {
|
||||
url = new URL(address)
|
||||
} catch {
|
||||
throw new CliError('Invalid Shumai server URL.')
|
||||
}
|
||||
if (
|
||||
url.username ||
|
||||
url.password ||
|
||||
url.search ||
|
||||
url.hash ||
|
||||
!['http:', 'https:'].includes(url.protocol)
|
||||
) {
|
||||
throw new CliError(
|
||||
'Use an http:// or https:// server URL without credentials, query, or fragment.',
|
||||
)
|
||||
}
|
||||
const localHosts = ['localhost', '127.0.0.1', '[::1]', 'host.docker.internal']
|
||||
if (url.protocol === 'http:' && !localHosts.includes(url.hostname)) {
|
||||
throw new CliError(
|
||||
'Remote runners require HTTPS. HTTP is only supported for local development.',
|
||||
)
|
||||
}
|
||||
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||
url.pathname = `${url.pathname.replace(/\/+$/, '')}/api/runners/ws`
|
||||
const cwd = await realpath(path.resolve(options.cwd ?? process.cwd()))
|
||||
if (!(await stat(cwd)).isDirectory()) throw new CliError('Runner workspace must be a directory.')
|
||||
return {
|
||||
url,
|
||||
key: key.trim(),
|
||||
cwd,
|
||||
srt: options.srt ? path.resolve(options.srt) : undefined,
|
||||
verbose: Boolean(options.verbose),
|
||||
}
|
||||
}
|
||||
|
||||
/** Commands inherit baseline OS settings and explicitly supplied skill variables, never CLI credentials. */
|
||||
export function commandEnv(extra: Record<string, string> = {}, inherit = true): NodeJS.ProcessEnv {
|
||||
const env: NodeJS.ProcessEnv = {}
|
||||
for (const [key, value] of Object.entries(inherit ? process.env : {})) {
|
||||
if (
|
||||
/^(PATH|PATHEXT|HOME|USER|LOGNAME|LANG|TERM|TMPDIR|TEMP|TMP|SYSTEMROOT|SYSTEMDRIVE|COMSPEC|USERPROFILE|USERNAME|APPDATA|LOCALAPPDATA|PROGRAMDATA|PROGRAMFILES|PROGRAMFILES\(X86\)|LC_.*)$/i.test(
|
||||
key,
|
||||
)
|
||||
)
|
||||
env[key] = value
|
||||
}
|
||||
for (const [key, value] of Object.entries(extra)) {
|
||||
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key))
|
||||
throw new Error('Invalid skill environment variable name.')
|
||||
if (['SHUMAI_RUNNER_KEY', 'SHUMAI_API_KEY'].includes(key.toUpperCase())) continue
|
||||
// Windows treats environment names as case-insensitive.
|
||||
for (const existing of Object.keys(env)) {
|
||||
if (existing.toUpperCase() === key.toUpperCase()) delete env[existing]
|
||||
}
|
||||
env[key] = value
|
||||
}
|
||||
return env
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { createServer, type Server } from 'node:http'
|
||||
import { mkdtemp, mkdir, readFile, readdir, rm, stat, symlink, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import path from 'node:path'
|
||||
import AdmZip from 'adm-zip'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import yauzl from 'yauzl'
|
||||
import { RunnerFiles } from './files'
|
||||
|
||||
describe('runner workspace file transfers', () => {
|
||||
let cwd: string
|
||||
let files: RunnerFiles
|
||||
let server: Server
|
||||
let url: string
|
||||
let body: Buffer
|
||||
let uploaded: Buffer
|
||||
beforeEach(async () => {
|
||||
cwd = await mkdtemp(path.join(tmpdir(), 'shumai-files-test-'))
|
||||
files = new RunnerFiles(cwd)
|
||||
body = Buffer.from('download contents')
|
||||
uploaded = Buffer.alloc(0)
|
||||
server = createServer(async (request, response) => {
|
||||
if (request.method === 'PUT') {
|
||||
const chunks: Buffer[] = []
|
||||
for await (const chunk of request) chunks.push(Buffer.from(chunk))
|
||||
uploaded = Buffer.concat(chunks)
|
||||
response.end()
|
||||
} else if (request.url === '/slow') {
|
||||
const timer = setInterval(() => response.write(Buffer.alloc(65536)), 20)
|
||||
response.once('close', () => clearInterval(timer))
|
||||
} else if (request.url === '/redirect') {
|
||||
response.writeHead(302, { location: '/file' })
|
||||
response.end()
|
||||
} else response.end(body)
|
||||
})
|
||||
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
|
||||
const address = server.address()
|
||||
if (!address || typeof address === 'string') throw new Error('Missing test HTTP address')
|
||||
url = `http://127.0.0.1:${address.port}`
|
||||
})
|
||||
afterEach(async () => {
|
||||
server.closeAllConnections()
|
||||
await new Promise<void>((resolve) => server.close(() => resolve()))
|
||||
await rm(cwd, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
const signal = () => new AbortController().signal
|
||||
|
||||
it('downloads, checks SHA-256, stats and uploads actual bytes', async () => {
|
||||
expect(
|
||||
await files.handle(
|
||||
{
|
||||
type: 'file.download',
|
||||
executionId: 'download',
|
||||
url,
|
||||
destPath: '.pi/input/data.bin',
|
||||
expectedHash: createHash('sha256').update(body).digest('hex'),
|
||||
},
|
||||
signal(),
|
||||
),
|
||||
).toMatchObject({ ok: true, size: body.length })
|
||||
expect(await readFile(path.join(cwd, '.pi/input/data.bin'))).toEqual(body)
|
||||
expect(
|
||||
await files.handle(
|
||||
{ type: 'file.stat', executionId: 'stat', srcPath: '.pi/input/data.bin' },
|
||||
signal(),
|
||||
),
|
||||
).toMatchObject({ ok: true, exists: true, isFile: true, size: body.length })
|
||||
expect(
|
||||
await files.handle(
|
||||
{ type: 'file.upload', executionId: 'upload', srcPath: '.pi/input/data.bin', url },
|
||||
signal(),
|
||||
),
|
||||
).toMatchObject({ ok: true, size: body.length, name: 'data.bin' })
|
||||
expect(uploaded).toEqual(body)
|
||||
expect(
|
||||
await files.handle(
|
||||
{ type: 'file.stat', executionId: 'absent', srcPath: '.pi/absent' },
|
||||
signal(),
|
||||
),
|
||||
).toMatchObject({ ok: true, exists: false })
|
||||
})
|
||||
|
||||
it('does not publish mismatched downloads or follow redirects', async () => {
|
||||
expect(
|
||||
await files.handle(
|
||||
{
|
||||
type: 'file.download',
|
||||
executionId: 'bad-hash',
|
||||
url,
|
||||
destPath: '.pi/data',
|
||||
expectedHash: 'bad',
|
||||
},
|
||||
signal(),
|
||||
),
|
||||
).toMatchObject({ ok: false, error: 'Downloaded file hash does not match.' })
|
||||
await expect(readFile(path.join(cwd, '.pi/data'))).rejects.toThrow()
|
||||
expect(
|
||||
await files.handle(
|
||||
{
|
||||
type: 'file.download',
|
||||
executionId: 'redirect',
|
||||
url: `${url}/redirect`,
|
||||
destPath: '.pi/data',
|
||||
},
|
||||
signal(),
|
||||
),
|
||||
).toHaveProperty('ok', false)
|
||||
})
|
||||
|
||||
it.each([
|
||||
'../secret',
|
||||
'/etc/passwd',
|
||||
'.pi/../secret',
|
||||
'.pi/dir/../../secret',
|
||||
'.pi\\secret',
|
||||
'.pi//file',
|
||||
'.pi/a:b',
|
||||
'.pi/a\0b',
|
||||
'.pi/.. /outside',
|
||||
'.pi/folder./file',
|
||||
'.pi/CON.txt',
|
||||
'.pi/nul .txt',
|
||||
'.pi/COM1',
|
||||
'.pi/lpt¹.log',
|
||||
'.pi/CONOUT$',
|
||||
])('rejects unsafe protocol path %s', async (srcPath) => {
|
||||
expect(
|
||||
await files.handle({ type: 'file.stat', executionId: 'unsafe', srcPath }, signal()),
|
||||
).toHaveProperty('ok', false)
|
||||
})
|
||||
|
||||
it('rejects parent and final symlinks, including a replaced .pi root', async () => {
|
||||
await mkdir(path.join(cwd, '.pi'))
|
||||
await writeFile(path.join(cwd, 'secret'), 'secret')
|
||||
await symlink(cwd, path.join(cwd, '.pi/escape'))
|
||||
await symlink(path.join(cwd, 'secret'), path.join(cwd, '.pi/link'))
|
||||
for (const srcPath of ['.pi/escape/secret', '.pi/link']) {
|
||||
expect(
|
||||
await files.handle({ type: 'file.upload', executionId: 'symlink', srcPath, url }, signal()),
|
||||
).toHaveProperty('ok', false)
|
||||
}
|
||||
await rm(path.join(cwd, '.pi'), { recursive: true })
|
||||
await symlink(cwd, path.join(cwd, '.pi'))
|
||||
expect(
|
||||
await files.handle(
|
||||
{ type: 'file.download', executionId: 'root', destPath: '.pi/file', url },
|
||||
signal(),
|
||||
),
|
||||
).toHaveProperty('ok', false)
|
||||
})
|
||||
|
||||
it('extracts skills and safely replaces old contents', async () => {
|
||||
const zip = new AdmZip()
|
||||
zip.addFile('SKILL.md', Buffer.from('# Test skill'))
|
||||
zip.addFile('scripts/run.sh', Buffer.from('echo skill'))
|
||||
body = zip.toBuffer()
|
||||
for (let attempt = 0; attempt < 2; attempt++) {
|
||||
expect(
|
||||
await files.handle(
|
||||
{
|
||||
type: 'file.download',
|
||||
executionId: `zip-${attempt}`,
|
||||
url,
|
||||
destPath: '.pi/skills/test',
|
||||
extractZip: true,
|
||||
},
|
||||
signal(),
|
||||
),
|
||||
).toHaveProperty('ok', true)
|
||||
}
|
||||
expect(await readFile(path.join(cwd, '.pi/skills/test/SKILL.md'), 'utf8')).toBe('# Test skill')
|
||||
})
|
||||
|
||||
it('rejects symlink ZIP entries and zip-slip paths without publishing them', async () => {
|
||||
const zip = new AdmZip()
|
||||
zip.addFile('link', Buffer.from('/etc/passwd'))
|
||||
const entry = zip.getEntry('link')!
|
||||
entry.header.attr = (0o120777 << 16) >>> 0
|
||||
body = zip.toBuffer()
|
||||
expect(
|
||||
await files.handle(
|
||||
{
|
||||
type: 'file.download',
|
||||
executionId: 'zip-link',
|
||||
url,
|
||||
destPath: '.pi/skills/bad',
|
||||
extractZip: true,
|
||||
},
|
||||
signal(),
|
||||
),
|
||||
).toHaveProperty('ok', false)
|
||||
const normal = new AdmZip()
|
||||
normal.addFile('aa/secret', Buffer.from('escape'))
|
||||
body = Buffer.from(
|
||||
normal.toBuffer().toString('latin1').replaceAll('aa/secret', '../secret'),
|
||||
'latin1',
|
||||
)
|
||||
expect(
|
||||
await files.handle(
|
||||
{
|
||||
type: 'file.download',
|
||||
executionId: 'zip-slip',
|
||||
url,
|
||||
destPath: '.pi/skills/bad',
|
||||
extractZip: true,
|
||||
},
|
||||
signal(),
|
||||
),
|
||||
).toHaveProperty('ok', false)
|
||||
await expect(readFile(path.join(cwd, '.pi/skills/bad/link'))).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('preserves executable script permissions and rejects oversized ZIP entries', async () => {
|
||||
const zip = new AdmZip()
|
||||
zip.addFile('run.sh', Buffer.from('#!/bin/sh\necho executable\n'))
|
||||
zip.getEntry('run.sh')!.header.attr = (0o100755 << 16) >>> 0
|
||||
body = zip.toBuffer()
|
||||
expect(
|
||||
await files.handle(
|
||||
{
|
||||
type: 'file.download',
|
||||
executionId: 'exec-zip',
|
||||
url,
|
||||
destPath: '.pi/skills/executable',
|
||||
extractZip: true,
|
||||
},
|
||||
signal(),
|
||||
),
|
||||
).toHaveProperty('ok', true)
|
||||
expect((await stat(path.join(cwd, '.pi/skills/executable/run.sh'))).mode & 0o7777).toBe(0o755)
|
||||
const central = body.indexOf(Buffer.from([0x50, 0x4b, 0x01, 0x02]))
|
||||
body.writeUInt32LE(256 * 1024 * 1024 + 1, central + 24)
|
||||
expect(
|
||||
await files.handle(
|
||||
{
|
||||
type: 'file.download',
|
||||
executionId: 'large-zip',
|
||||
url,
|
||||
destPath: '.pi/skills/large',
|
||||
extractZip: true,
|
||||
},
|
||||
signal(),
|
||||
),
|
||||
).toMatchObject({ ok: false, error: 'Extracted ZIP exceeds 256 MiB.' })
|
||||
})
|
||||
|
||||
it('cancels a streaming download without publishing partial bytes or leaving staging files', async () => {
|
||||
await mkdir(path.join(cwd, '.pi/input'), { recursive: true })
|
||||
await writeFile(path.join(cwd, '.pi/input/data'), 'original')
|
||||
const controller = new AbortController()
|
||||
const transfer = files.handle(
|
||||
{
|
||||
type: 'file.download',
|
||||
executionId: 'stream-cancel',
|
||||
url: `${url}/slow`,
|
||||
destPath: '.pi/input/data',
|
||||
},
|
||||
controller.signal,
|
||||
)
|
||||
await vi.waitFor(async () =>
|
||||
expect(
|
||||
(await readdir(path.join(cwd, '.pi/input'))).some((name) => name.startsWith('.shumai-')),
|
||||
).toBe(true),
|
||||
)
|
||||
controller.abort()
|
||||
expect(await transfer).toHaveProperty('ok', false)
|
||||
expect(await readFile(path.join(cwd, '.pi/input/data'), 'utf8')).toBe('original')
|
||||
expect(await readdir(path.join(cwd, '.pi/input'))).toEqual(['data'])
|
||||
})
|
||||
|
||||
it('cancels extraction without deleting the installed skill or leaving staging trees', async () => {
|
||||
await mkdir(path.join(cwd, '.pi/skills/installed'), { recursive: true })
|
||||
await writeFile(path.join(cwd, '.pi/skills/installed/SKILL.md'), 'original')
|
||||
const zip = new AdmZip()
|
||||
for (let index = 0; index < 2000; index++)
|
||||
zip.addFile(`file-${index}.txt`, Buffer.alloc(32768, 65))
|
||||
body = zip.toBuffer()
|
||||
const controller = new AbortController()
|
||||
const transfer = files.handle(
|
||||
{
|
||||
type: 'file.download',
|
||||
executionId: 'extract-cancel',
|
||||
url,
|
||||
destPath: '.pi/skills/installed',
|
||||
extractZip: true,
|
||||
},
|
||||
controller.signal,
|
||||
)
|
||||
await vi.waitFor(
|
||||
async () => {
|
||||
const temporary = (await readdir(path.join(cwd, '.pi/skills'))).find((name) =>
|
||||
name.startsWith('.shumai-'),
|
||||
)
|
||||
expect(temporary).toBeDefined()
|
||||
expect(
|
||||
(await readdir(path.join(cwd, '.pi/skills', temporary!, 'extracted'))).length,
|
||||
).toBeGreaterThan(0)
|
||||
},
|
||||
{ interval: 5, timeout: 5000 },
|
||||
)
|
||||
controller.abort()
|
||||
expect(await transfer).toHaveProperty('ok', false)
|
||||
expect(await readFile(path.join(cwd, '.pi/skills/installed/SKILL.md'), 'utf8')).toBe('original')
|
||||
expect(await readdir(path.join(cwd, '.pi/skills'))).toEqual(['installed'])
|
||||
}, 10000)
|
||||
|
||||
it('waits for an in-flight ZIP entry read before closing on cancellation', async () => {
|
||||
const zip = new AdmZip()
|
||||
zip.addFile('SKILL.md', Buffer.from('skill'))
|
||||
body = zip.toBuffer()
|
||||
const controller = new AbortController()
|
||||
const pendingRead = Promise.withResolvers<() => void>()
|
||||
const readEntry = yauzl.ZipFile.prototype.readEntry
|
||||
const read = vi.spyOn(yauzl.ZipFile.prototype, 'readEntry').mockImplementationOnce(function (
|
||||
this: yauzl.ZipFile,
|
||||
) {
|
||||
pendingRead.resolve(() => readEntry.call(this))
|
||||
})
|
||||
const close = vi.spyOn(yauzl.ZipFile.prototype, 'close')
|
||||
try {
|
||||
const transfer = files.handle(
|
||||
{
|
||||
type: 'file.download',
|
||||
executionId: 'zip-entry-cancel',
|
||||
url,
|
||||
destPath: '.pi/skills/new',
|
||||
extractZip: true,
|
||||
},
|
||||
controller.signal,
|
||||
)
|
||||
const finishRead = await pendingRead.promise
|
||||
controller.abort()
|
||||
await new Promise<void>((resolve) => setImmediate(resolve))
|
||||
const closedDuringRead = close.mock.calls.length > 0
|
||||
if (!closedDuringRead) finishRead()
|
||||
expect(await transfer).toHaveProperty('ok', false)
|
||||
expect(closedDuringRead).toBe(false)
|
||||
expect(await readdir(path.join(cwd, '.pi/skills'))).toEqual([])
|
||||
} finally {
|
||||
read.mockRestore()
|
||||
close.mockRestore()
|
||||
}
|
||||
})
|
||||
|
||||
it('handles cancelled transfers and refuses uploads of directories', async () => {
|
||||
const controller = new AbortController()
|
||||
controller.abort()
|
||||
expect(
|
||||
await files.handle(
|
||||
{ type: 'file.download', executionId: 'cancelled', url, destPath: '.pi/data' },
|
||||
controller.signal,
|
||||
),
|
||||
).toHaveProperty('ok', false)
|
||||
expect(
|
||||
await files.handle(
|
||||
{ type: 'file.upload', executionId: 'directory', url, srcPath: '.pi/transfers' },
|
||||
signal(),
|
||||
),
|
||||
).toHaveProperty('ok', false)
|
||||
expect(uploaded).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,282 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { constants, createWriteStream } from 'node:fs'
|
||||
import { lstat, mkdir, mkdtemp, open, rename, rm } from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import { Readable, Transform } from 'node:stream'
|
||||
import { pipeline } from 'node:stream/promises'
|
||||
import yauzl from 'yauzl'
|
||||
import type {
|
||||
RunnerFileDownloadMessage,
|
||||
RunnerFileUploadMessage,
|
||||
RunnerFileStatMessage,
|
||||
RunnerToServerMessage,
|
||||
} from '@shumai/dtos'
|
||||
|
||||
const MAX_FILE_BYTES = 256 * 1024 * 1024
|
||||
const MAX_ZIP_ENTRIES = 10_000
|
||||
export type FileMessage =
|
||||
RunnerFileDownloadMessage | RunnerFileUploadMessage | RunnerFileStatMessage
|
||||
|
||||
function pathParts(value: string): string[] {
|
||||
const parts = value.split('/')
|
||||
if (
|
||||
value.includes('\\') ||
|
||||
value.includes('\0') ||
|
||||
parts.some(
|
||||
(part) =>
|
||||
!part ||
|
||||
part === '.' ||
|
||||
part === '..' ||
|
||||
part.includes(':') ||
|
||||
/[. ]$/.test(part) ||
|
||||
/^(?:con|prn|aux|nul|conin\$|conout\$|com[1-9¹²³]|lpt[1-9¹²³])(?: *\.|$)/i.test(part),
|
||||
)
|
||||
)
|
||||
throw new Error('Invalid workspace path.')
|
||||
return parts
|
||||
}
|
||||
|
||||
function isMissing(error: unknown): boolean {
|
||||
return error instanceof Error && 'code' in error && error.code === 'ENOENT'
|
||||
}
|
||||
|
||||
function transferUrl(address: string): URL {
|
||||
const url = new URL(address)
|
||||
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password)
|
||||
throw new Error('Invalid transfer URL.')
|
||||
return url
|
||||
}
|
||||
|
||||
export class RunnerFiles {
|
||||
constructor(readonly cwd: string) {}
|
||||
|
||||
/** Basic workspace validation. Concurrent hostile filesystem mutation is outside v1's scope. */
|
||||
private async resolve(relative: string, createParents = false): Promise<string> {
|
||||
const parts = pathParts(relative)
|
||||
if (parts[0] !== '.pi' || parts.length < 2) throw new Error('File paths must stay inside .pi/.')
|
||||
let current = this.cwd
|
||||
for (let index = 0; index < parts.length; index++) {
|
||||
current = path.join(current, parts[index])
|
||||
const parent = index < parts.length - 1
|
||||
if (parent && createParents) await mkdir(current, { recursive: true, mode: 0o700 })
|
||||
try {
|
||||
const info = await lstat(current)
|
||||
if (info.isSymbolicLink() || (parent && !info.isDirectory()))
|
||||
throw new Error('Workspace paths cannot contain symlinks or non-directory parents.')
|
||||
} catch (error) {
|
||||
if (!isMissing(error)) throw error
|
||||
}
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
async createWriter(relative: string) {
|
||||
const file = await open(
|
||||
await this.resolve(relative, true),
|
||||
constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY | constants.O_NOFOLLOW,
|
||||
0o600,
|
||||
)
|
||||
return {
|
||||
write: async (data: string) => {
|
||||
await file.writeFile(data)
|
||||
},
|
||||
close: () => file.close(),
|
||||
}
|
||||
}
|
||||
|
||||
async handle(message: FileMessage, signal: AbortSignal): Promise<RunnerToServerMessage> {
|
||||
try {
|
||||
signal.throwIfAborted()
|
||||
if (message.type === 'file.download')
|
||||
return {
|
||||
type: 'file.download.result',
|
||||
executionId: message.executionId,
|
||||
ok: true,
|
||||
...(await this.download(message, signal)),
|
||||
}
|
||||
const filename = await this.resolve(message.srcPath)
|
||||
if (message.type === 'file.upload') return await this.upload(message, filename, signal)
|
||||
try {
|
||||
const info = await lstat(filename)
|
||||
return {
|
||||
type: 'file.stat.result',
|
||||
executionId: message.executionId,
|
||||
ok: true,
|
||||
exists: true,
|
||||
isFile: info.isFile(),
|
||||
size: info.size,
|
||||
name: path.basename(filename),
|
||||
}
|
||||
} catch (error) {
|
||||
if (!isMissing(error)) throw error
|
||||
return {
|
||||
type: 'file.stat.result',
|
||||
executionId: message.executionId,
|
||||
ok: true,
|
||||
exists: false,
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
const result = {
|
||||
executionId: message.executionId,
|
||||
ok: false,
|
||||
error: error instanceof Error ? error.message : 'File operation failed.',
|
||||
}
|
||||
switch (message.type) {
|
||||
case 'file.download':
|
||||
return { ...result, type: 'file.download.result' }
|
||||
case 'file.upload':
|
||||
return { ...result, type: 'file.upload.result' }
|
||||
case 'file.stat':
|
||||
return { ...result, type: 'file.stat.result', exists: false }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async upload(
|
||||
message: RunnerFileUploadMessage,
|
||||
filename: string,
|
||||
signal: AbortSignal,
|
||||
): Promise<RunnerToServerMessage> {
|
||||
const file = await open(
|
||||
filename,
|
||||
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
||||
)
|
||||
try {
|
||||
const info = await file.stat()
|
||||
if (!info.isFile() || info.size > MAX_FILE_BYTES)
|
||||
throw new Error('Upload must be a regular file no larger than 256 MiB.')
|
||||
// Node and Bun declare different DOM stream types for the same fetch-compatible stream.
|
||||
const body = info.size
|
||||
? (Readable.toWeb(
|
||||
file.createReadStream({ autoClose: false, end: info.size - 1 }),
|
||||
) as unknown as ReadableStream<Uint8Array>)
|
||||
: new Uint8Array()
|
||||
const response = await fetch(transferUrl(message.url), {
|
||||
method: 'PUT',
|
||||
body,
|
||||
...{ duplex: 'half' },
|
||||
headers: {
|
||||
'content-type': message.contentType ?? 'application/octet-stream',
|
||||
'content-length': String(info.size),
|
||||
},
|
||||
signal,
|
||||
redirect: 'error',
|
||||
})
|
||||
await response.body?.cancel()
|
||||
if (!response.ok) throw new Error(`Upload failed (HTTP ${response.status}).`)
|
||||
return {
|
||||
type: 'file.upload.result',
|
||||
executionId: message.executionId,
|
||||
ok: true,
|
||||
size: info.size,
|
||||
name: path.basename(filename),
|
||||
}
|
||||
} finally {
|
||||
await file.close()
|
||||
}
|
||||
}
|
||||
|
||||
private async download(message: RunnerFileDownloadMessage, signal: AbortSignal) {
|
||||
const destination = await this.resolve(message.destPath, true)
|
||||
const temporary = await mkdtemp(path.join(path.dirname(destination), '.shumai-'))
|
||||
let preserveBackup = false
|
||||
try {
|
||||
const response = await fetch(transferUrl(message.url), { signal, redirect: 'error' })
|
||||
if (!response.ok || !response.body) {
|
||||
await response.body?.cancel()
|
||||
throw new Error(`Download failed (HTTP ${response.status}).`)
|
||||
}
|
||||
const hash = createHash('sha256')
|
||||
let size = 0
|
||||
const archive = path.join(temporary, 'download')
|
||||
await pipeline(
|
||||
response.body,
|
||||
new Transform({
|
||||
transform(chunk: Buffer, _encoding, callback) {
|
||||
size += chunk.length
|
||||
if (size > MAX_FILE_BYTES) {
|
||||
callback(new Error('Download exceeds 256 MiB.'))
|
||||
return
|
||||
}
|
||||
hash.update(chunk)
|
||||
callback(null, chunk)
|
||||
},
|
||||
}),
|
||||
createWriteStream(archive, { flags: 'wx', mode: 0o600 }),
|
||||
{ signal },
|
||||
)
|
||||
if (message.expectedHash && hash.digest('hex') !== message.expectedHash)
|
||||
throw new Error('Downloaded file hash does not match.')
|
||||
let source = archive
|
||||
if (message.extractZip) {
|
||||
source = path.join(temporary, 'extracted')
|
||||
await mkdir(source, { mode: 0o700 })
|
||||
await this.extract(archive, source, signal)
|
||||
}
|
||||
signal.throwIfAborted()
|
||||
await this.resolve(message.destPath)
|
||||
if (message.extractZip) {
|
||||
const backup = path.join(temporary, 'previous')
|
||||
let backedUp = false
|
||||
try {
|
||||
await rename(destination, backup)
|
||||
backedUp = true
|
||||
} catch (error) {
|
||||
if (!isMissing(error)) throw error
|
||||
}
|
||||
try {
|
||||
await rename(source, destination)
|
||||
} catch (error) {
|
||||
if (backedUp) {
|
||||
preserveBackup = true
|
||||
await rename(backup, destination)
|
||||
preserveBackup = false
|
||||
}
|
||||
throw error
|
||||
}
|
||||
} else await rename(source, destination)
|
||||
return { size, cached: false }
|
||||
} finally {
|
||||
if (!preserveBackup) await rm(temporary, { recursive: true, force: true })
|
||||
}
|
||||
}
|
||||
|
||||
private async extract(archive: string, directory: string, signal: AbortSignal) {
|
||||
const zip = await yauzl.openPromise(archive, {
|
||||
lazyEntries: true,
|
||||
strictFileNames: true,
|
||||
autoClose: false,
|
||||
})
|
||||
let size = 0
|
||||
let count = 0
|
||||
try {
|
||||
signal.throwIfAborted()
|
||||
// Let each metadata read finish before cancellation can close the ZIP descriptor.
|
||||
for await (const entry of zip.eachEntry()) {
|
||||
signal.throwIfAborted()
|
||||
if (++count > MAX_ZIP_ENTRIES) throw new Error('ZIP contains too many entries.')
|
||||
const parts = pathParts(entry.fileName.replace(/\/$/, ''))
|
||||
const mode = entry.externalFileAttributes >>> 16
|
||||
const kind = mode & 0o170000
|
||||
if (kind && kind !== 0o100000 && kind !== 0o040000)
|
||||
throw new Error('ZIP links and special files are forbidden.')
|
||||
size += entry.uncompressedSize
|
||||
if (size > MAX_FILE_BYTES) throw new Error('Extracted ZIP exceeds 256 MiB.')
|
||||
const destination = path.join(directory, ...parts)
|
||||
if (entry.fileName.endsWith('/')) await mkdir(destination, { recursive: true, mode: 0o700 })
|
||||
else {
|
||||
await mkdir(path.dirname(destination), { recursive: true, mode: 0o700 })
|
||||
const input = await zip.openReadStreamPromise(entry)
|
||||
await pipeline(
|
||||
input,
|
||||
createWriteStream(destination, { flags: 'wx', mode: 0o600 | (mode & 0o155) }),
|
||||
{ signal },
|
||||
)
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
zip.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
createRunnerLogger,
|
||||
formatBytes,
|
||||
formatDuration,
|
||||
formatLogString,
|
||||
formatTimestamp,
|
||||
shortId,
|
||||
} from './log'
|
||||
|
||||
describe('runner log helpers', () => {
|
||||
it('formats timestamps and aligned log levels', () => {
|
||||
const fixed = new Date(2026, 0, 1, 9, 4, 5)
|
||||
expect(formatTimestamp(fixed)).toBe('09:04:05')
|
||||
const lines: string[] = []
|
||||
const logger = createRunnerLogger(
|
||||
(line) => lines.push(line),
|
||||
false,
|
||||
() => fixed,
|
||||
)
|
||||
logger.info('Bash started [a8f2]')
|
||||
logger.warn('Runner disconnected; reconnecting…')
|
||||
logger.error('Fatal error')
|
||||
expect(lines).toEqual([
|
||||
'09:04:05 INFO Bash started [a8f2]',
|
||||
'09:04:05 WARN Runner disconnected; reconnecting…',
|
||||
'09:04:05 ERROR Fatal error',
|
||||
])
|
||||
})
|
||||
|
||||
it('formats short IDs, durations, byte counts, and single-line quoted strings', () => {
|
||||
expect(shortId('01JABCDEF1234567890A8F2')).toBe('a8f2')
|
||||
expect(shortId('one')).toBe('one')
|
||||
expect(formatDuration(1840)).toBe('1.8s')
|
||||
expect(formatDuration(-10)).toBe('0.0s')
|
||||
expect(formatBytes(0)).toBe('0')
|
||||
expect(formatBytes(512)).toBe('512B')
|
||||
expect(formatBytes(1536)).toBe('1.5KB')
|
||||
expect(formatBytes(12 * 1024)).toBe('12KB')
|
||||
expect(formatBytes(Math.round(2.4 * 1024 * 1024))).toBe('2.4MB')
|
||||
expect(formatLogString('echo "hi"\nls')).toBe('"echo \\"hi\\"\\nls"')
|
||||
expect(formatLogString('x'.repeat(210))).toBe(`"${'x'.repeat(200)}…"`)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,59 @@
|
||||
export type RunnerLogLevel = 'INFO' | 'WARN' | 'ERROR'
|
||||
|
||||
export interface RunnerLogger {
|
||||
readonly verbose: boolean
|
||||
info(message: string): void
|
||||
warn(message: string): void
|
||||
error(message: string): void
|
||||
}
|
||||
|
||||
/** Formats a local wall-clock timestamp as HH:MM:SS for runner stderr logs. */
|
||||
export function formatTimestamp(date = new Date()): string {
|
||||
const pad = (value: number) => String(value).padStart(2, '0')
|
||||
return `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
|
||||
}
|
||||
|
||||
/** Builds a prefixed runner logger writing uniform timestamped lines to stderr. */
|
||||
export function createRunnerLogger(
|
||||
write: (message: string) => void = console.error,
|
||||
verbose = false,
|
||||
now: () => Date = () => new Date(),
|
||||
): RunnerLogger {
|
||||
const emit = (level: RunnerLogLevel, message: string) => {
|
||||
write(`${formatTimestamp(now())} ${level.padEnd(5)} ${message}`)
|
||||
}
|
||||
return {
|
||||
verbose,
|
||||
info: (message) => emit('INFO', message),
|
||||
warn: (message) => emit('WARN', message),
|
||||
error: (message) => emit('ERROR', message),
|
||||
}
|
||||
}
|
||||
|
||||
/** Extracts the trailing 4-character tail of an execution ID for compact log labels. */
|
||||
export function shortId(executionId: string): string {
|
||||
return executionId.slice(-4).toLowerCase()
|
||||
}
|
||||
|
||||
/** Formats a millisecond duration as seconds with one decimal place (e.g. `1.8s`). */
|
||||
export function formatDuration(ms: number): string {
|
||||
return `${(Math.max(0, ms) / 1000).toFixed(1)}s`
|
||||
}
|
||||
|
||||
/** Formats a byte count concisely for runner logs (`0`, `128B`, `12KB`, `2.4MB`). */
|
||||
export function formatBytes(bytes: number): string {
|
||||
if (bytes <= 0) return '0'
|
||||
if (bytes < 1024) return `${bytes}B`
|
||||
if (bytes < 1024 * 1024) {
|
||||
const kb = bytes / 1024
|
||||
return kb < 10 ? `${kb.toFixed(1).replace(/\.0$/, '')}KB` : `${Math.round(kb)}KB`
|
||||
}
|
||||
const mb = bytes / (1024 * 1024)
|
||||
return `${mb.toFixed(1).replace(/\.0$/, '')}MB`
|
||||
}
|
||||
|
||||
/** Formats an arbitrary string (command, path, error) as a bounded single-line JSON string. */
|
||||
export function formatLogString(value: string, maxLength = 200): string {
|
||||
const text = value.length > maxLength ? `${value.slice(0, maxLength)}…` : value
|
||||
return JSON.stringify(text)
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import { spawnSync, type SpawnSyncReturns } from 'node:child_process'
|
||||
import { accessSync, constants } from 'node:fs'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { resolveRunnerShell } from './shell-config'
|
||||
|
||||
vi.mock('node:child_process', () => ({ spawnSync: vi.fn() }))
|
||||
vi.mock('node:fs', { spy: true })
|
||||
|
||||
const access = vi.mocked(accessSync)
|
||||
const where = vi.mocked(spawnSync)
|
||||
const gitBash = 'D:\\Program Files\\Git\\bin\\bash.exe'
|
||||
const gitBashX86 = 'D:\\Program Files (x86)\\Git\\bin\\bash.exe'
|
||||
|
||||
function whereResult(stdout = '', status = 0): SpawnSyncReturns<string> {
|
||||
return { stdout, stderr: '', status, output: [null, stdout, ''], pid: 1, signal: null }
|
||||
}
|
||||
|
||||
function available(...paths: string[]) {
|
||||
access.mockImplementation((executable) => {
|
||||
if (!paths.some((path) => executable === path)) throw new Error('File not found')
|
||||
})
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubEnv('ProgramFiles', 'D:\\Program Files')
|
||||
vi.stubEnv('ProgramFiles(x86)', 'D:\\Program Files (x86)')
|
||||
vi.stubEnv('SystemRoot', 'E:\\WinNT')
|
||||
available()
|
||||
where.mockReturnValue(whereResult('', 1))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.resetAllMocks()
|
||||
vi.unstubAllEnvs()
|
||||
})
|
||||
|
||||
describe('runner shell selection', () => {
|
||||
it.each(['linux', 'darwin'] as const)('uses /bin/sh on %s', (platform) => {
|
||||
expect(resolveRunnerShell(platform)).toEqual({
|
||||
executable: '/bin/sh',
|
||||
commandTransport: 'shell',
|
||||
})
|
||||
expect(where).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it.each([
|
||||
[gitBash, [gitBash, gitBashX86]],
|
||||
[gitBashX86, [gitBashX86]],
|
||||
] as const)('prefers installed Git Bash at %s before PATH', (executable, paths) => {
|
||||
available(...paths)
|
||||
expect(resolveRunnerShell('win32')).toEqual({ executable, commandTransport: 'argv' })
|
||||
expect(where).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
// Adapted from Pi's shell-path-exists.test.ts. See apps/cli/THIRD-PARTY-NOTICES.txt.
|
||||
it('accepts a Windows Store Bash alias found on PATH via access(F_OK)', () => {
|
||||
const alias = 'D:\\Users\\runner\\AppData\\Local\\Microsoft\\WindowsApps\\bash.exe'
|
||||
available(alias)
|
||||
where.mockReturnValue(whereResult(alias + '\r\n'))
|
||||
expect(resolveRunnerShell('win32')).toEqual({ executable: alias, commandTransport: 'argv' })
|
||||
expect(access).toHaveBeenCalledWith(alias, constants.F_OK)
|
||||
expect(where).toHaveBeenCalledWith('E:\\WinNT\\System32\\where.exe', ['$PATH:bash.exe'], {
|
||||
encoding: 'utf8',
|
||||
timeout: 5000,
|
||||
windowsHide: true,
|
||||
})
|
||||
})
|
||||
|
||||
it('skips relative and inaccessible PATH results', () => {
|
||||
const executable = 'D:\\MSYS2\\usr\\bin\\bash.exe'
|
||||
available(executable)
|
||||
where.mockReturnValue(
|
||||
whereResult(['bash.exe', 'D:\\missing\\bash.exe', executable].join('\r\n')),
|
||||
)
|
||||
expect(resolveRunnerShell('win32')).toEqual({ executable, commandTransport: 'argv' })
|
||||
})
|
||||
|
||||
it.each([
|
||||
['E:\\WinNT', 'E:\\WinNT\\System32\\cmd.exe'],
|
||||
[undefined, 'C:\\Windows\\System32\\cmd.exe'],
|
||||
] as const)(
|
||||
'falls back to cmd.exe when no Bash is available (SystemRoot %s)',
|
||||
(root, executable) => {
|
||||
vi.stubEnv('SystemRoot', root)
|
||||
expect(resolveRunnerShell('win32')).toEqual({ executable, commandTransport: 'shell' })
|
||||
},
|
||||
)
|
||||
|
||||
// Adapted from Pi's windows-shell-config.test.ts.
|
||||
it.each([
|
||||
['D:\\WinNT\\', 'd:/WINNT/System32/BASH.EXE', 'stdin'],
|
||||
['D:\\WinNT', 'D:\\WinNT\\Sysnative\\bash.exe', 'stdin'],
|
||||
[undefined, 'E:\\Windows\\System32\\bash.exe', 'stdin'],
|
||||
['D:\\WinNT', 'D:\\Git\\bin\\bash.exe', 'argv'],
|
||||
['D:\\WinNT', 'E:\\Windows\\System32\\bash.exe', 'argv'],
|
||||
['WinNT', 'C:\\Windows\\System32\\bash.exe', 'argv'],
|
||||
] as const)(
|
||||
'uses the correct command transport for SystemRoot %s and %s',
|
||||
(root, executable, commandTransport) => {
|
||||
vi.stubEnv('SystemRoot', root)
|
||||
available(executable)
|
||||
where.mockReturnValue(whereResult(executable + '\r\n'))
|
||||
expect(resolveRunnerShell('win32')).toEqual({ executable, commandTransport })
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,64 @@
|
||||
import { spawnSync } from 'node:child_process'
|
||||
import { accessSync, constants } from 'node:fs'
|
||||
import { win32 } from 'node:path'
|
||||
|
||||
export interface RunnerShellConfig {
|
||||
executable: string
|
||||
commandTransport: 'shell' | 'argv' | 'stdin'
|
||||
}
|
||||
|
||||
// Windows Bash discovery and legacy WSL detection adapted from pi-mono.
|
||||
// See apps/cli/THIRD-PARTY-NOTICES.txt.
|
||||
function isLegacyWslBashPath(executable: string, systemRoot: string | undefined): boolean {
|
||||
const normalized = win32.normalize(executable).toLowerCase()
|
||||
if (systemRoot) {
|
||||
if (!/^[a-z]:[\\/]/i.test(systemRoot)) return false
|
||||
return ['System32', 'Sysnative'].some(
|
||||
(directory) => normalized === win32.join(systemRoot, directory, 'bash.exe').toLowerCase(),
|
||||
)
|
||||
}
|
||||
return /^[a-z]:\\windows\\(?:system32|sysnative)\\bash\.exe$/.test(normalized)
|
||||
}
|
||||
|
||||
function shellPathExists(executable: string): boolean {
|
||||
// access(F_OK) accepts runnable Windows Store aliases that fail an extra stat check.
|
||||
try {
|
||||
accessSync(executable, constants.F_OK)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function findWindowsBash(system32: string): string | undefined {
|
||||
for (const directory of [process.env.ProgramFiles, process.env['ProgramFiles(x86)']]) {
|
||||
if (!directory || !win32.isAbsolute(directory)) continue
|
||||
const executable = win32.join(directory, 'Git', 'bin', 'bash.exe')
|
||||
if (shellPathExists(executable)) return executable
|
||||
}
|
||||
|
||||
// Use the system tool and limit its search to PATH. The workspace may contain a bash.exe.
|
||||
const found = spawnSync(win32.join(system32, 'where.exe'), ['$PATH:bash.exe'], {
|
||||
encoding: 'utf8',
|
||||
timeout: 5000,
|
||||
windowsHide: true,
|
||||
})
|
||||
if (found.status !== 0 || !found.stdout) return
|
||||
return found.stdout
|
||||
.trim()
|
||||
.split(/\r?\n/)
|
||||
.find((executable) => win32.isAbsolute(executable) && shellPathExists(executable))
|
||||
}
|
||||
|
||||
export function resolveRunnerShell(platform = process.platform): RunnerShellConfig {
|
||||
if (platform !== 'win32') return { executable: '/bin/sh', commandTransport: 'shell' }
|
||||
|
||||
const systemRoot = process.env.SystemRoot
|
||||
const system32 = win32.join(systemRoot ?? 'C:\\Windows', 'System32')
|
||||
const executable = findWindowsBash(system32)
|
||||
if (!executable) return { executable: win32.join(system32, 'cmd.exe'), commandTransport: 'shell' }
|
||||
return {
|
||||
executable,
|
||||
commandTransport: isLegacyWslBashPath(executable, systemRoot) ? 'stdin' : 'argv',
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import path from 'node:path'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
|
||||
import { RunnerFiles } from './files'
|
||||
import { RunnerShell } from './shell'
|
||||
|
||||
describe('runner bash execution', () => {
|
||||
let cwd: string
|
||||
let shell: RunnerShell
|
||||
beforeEach(async () => {
|
||||
cwd = await mkdtemp(path.join(tmpdir(), 'shumai-shell-test-'))
|
||||
shell = new RunnerShell(new RunnerFiles(cwd))
|
||||
vi.spyOn(SandboxManager, 'initialize').mockResolvedValue()
|
||||
vi.spyOn(SandboxManager, 'reset').mockResolvedValue()
|
||||
vi.spyOn(SandboxManager, 'wrapWithSandboxArgv').mockImplementation(async (command) => ({
|
||||
argv: ['/bin/sh', '-c', command],
|
||||
env: process.env,
|
||||
}))
|
||||
})
|
||||
afterEach(async () => {
|
||||
await shell.close()
|
||||
vi.restoreAllMocks()
|
||||
vi.unstubAllEnvs()
|
||||
await rm(cwd, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
const request = (command: string, executionId = 'test') => ({
|
||||
type: 'exec.start' as const,
|
||||
executionId,
|
||||
command,
|
||||
})
|
||||
|
||||
it('runs with no executables on PATH when SRT is not requested', async () => {
|
||||
vi.stubEnv('PATH', '')
|
||||
await shell.initialize()
|
||||
let output = ''
|
||||
const result = await shell.run(
|
||||
request('printf no-extra-runtime'),
|
||||
new AbortController().signal,
|
||||
(_stream, data) => {
|
||||
output += data
|
||||
},
|
||||
)
|
||||
expect(result).toMatchObject({ exitCode: 0 })
|
||||
expect(output).toBe('no-extra-runtime')
|
||||
})
|
||||
|
||||
// Adapted from Pi's tools.test.ts. See apps/cli/THIRD-PARTY-NOTICES.txt.
|
||||
it('sends the complete command over stdin for the legacy Bash launcher', async () => {
|
||||
shell = new RunnerShell(new RunnerFiles(cwd), undefined, {
|
||||
executable: '/bin/sh',
|
||||
commandTransport: 'stdin',
|
||||
})
|
||||
const command = [
|
||||
'name="Hello, 世界"',
|
||||
'for choice in "one two" \'three "four"\'; do',
|
||||
' printf \'%s / %s\\n\' "$name" "$choice"',
|
||||
'done',
|
||||
].join('\n')
|
||||
let output = ''
|
||||
const result = await shell.run(
|
||||
request(command),
|
||||
new AbortController().signal,
|
||||
(_stream, data) => {
|
||||
output += data
|
||||
},
|
||||
)
|
||||
expect(result).toMatchObject({ exitCode: 0 })
|
||||
expect(output).toBe('Hello, 世界 / one two\nHello, 世界 / three "four"\n')
|
||||
})
|
||||
|
||||
it('passes a command with shell quoting directly to the selected executable', async () => {
|
||||
shell = new RunnerShell(new RunnerFiles(cwd), undefined, {
|
||||
executable: '/bin/sh',
|
||||
commandTransport: 'argv',
|
||||
})
|
||||
const command = "printf '%s' '$literal \"quotes\"'"
|
||||
let output = ''
|
||||
const result = await shell.run(
|
||||
request(command),
|
||||
new AbortController().signal,
|
||||
(_stream, data) => {
|
||||
output += data
|
||||
},
|
||||
)
|
||||
expect(result).toMatchObject({ exitCode: 0 })
|
||||
expect(output).toBe('$literal "quotes"')
|
||||
})
|
||||
|
||||
it('rejects the legacy WSL launcher before starting SRT', async () => {
|
||||
shell = new RunnerShell(new RunnerFiles(cwd), path.join(cwd, 'sandbox.json'), {
|
||||
executable: 'C:\\Windows\\System32\\bash.exe',
|
||||
commandTransport: 'stdin',
|
||||
})
|
||||
await expect(shell.initialize()).rejects.toThrow('SRT cannot run the legacy WSL Bash launcher')
|
||||
expect(SandboxManager.initialize).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('streams both outputs, returns the exit code, and saves a log in the runner workspace', async () => {
|
||||
const output: string[] = []
|
||||
const result = await shell.run(
|
||||
request('printf "hello 世界"; printf "failure" >&2; exit 7'),
|
||||
new AbortController().signal,
|
||||
(stream, data) => output.push(`${stream}:${data}`),
|
||||
)
|
||||
expect(result).toMatchObject({
|
||||
exitCode: 7,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
fullOutputPath: '.pi/logs/bash-test.log',
|
||||
})
|
||||
expect(output.join('|')).toContain('stdout:hello 世界')
|
||||
expect(output.join('|')).toContain('stderr:failure')
|
||||
expect(await readFile(path.join(cwd, result.fullOutputPath!), 'utf8')).toContain('hello 世界')
|
||||
})
|
||||
|
||||
it('passes skill env without inheriting runner or unrelated credentials', async () => {
|
||||
vi.stubEnv('SHUMAI_RUNNER_KEY', 'runner-secret')
|
||||
vi.stubEnv('AWS_SECRET_ACCESS_KEY', 'aws-secret')
|
||||
let output = ''
|
||||
await shell.run(
|
||||
{
|
||||
...request(
|
||||
'printf "%s|%s|%s" "$SKILL_TOKEN" "$SHUMAI_RUNNER_KEY" "$AWS_SECRET_ACCESS_KEY"',
|
||||
),
|
||||
env: { SKILL_TOKEN: 'skill-secret' },
|
||||
},
|
||||
new AbortController().signal,
|
||||
(_stream, data) => {
|
||||
output += data
|
||||
},
|
||||
)
|
||||
expect(output).toBe('skill-secret||')
|
||||
})
|
||||
|
||||
it('times out and terminates background children', async () => {
|
||||
const result = await shell.run(
|
||||
{ ...request('sleep 30 & echo $!; wait'), timeoutSec: 0.1 },
|
||||
new AbortController().signal,
|
||||
() => {},
|
||||
)
|
||||
expect(result).toMatchObject({ timedOut: true, signal: 'SIGKILL' })
|
||||
const pid = Number((await readFile(path.join(cwd, '.pi/logs/bash-test.log'), 'utf8')).trim())
|
||||
expect(() => process.kill(pid, 0)).toThrow()
|
||||
})
|
||||
|
||||
it('cancels a running command and never starts an already cancelled one', async () => {
|
||||
const controller = new AbortController()
|
||||
const running = shell.run(request('echo ready; sleep 30'), controller.signal, () =>
|
||||
controller.abort(),
|
||||
)
|
||||
expect(await running).toMatchObject({ aborted: true, signal: 'SIGKILL' })
|
||||
expect(
|
||||
await shell.run(request('touch unwanted', 'cancelled'), controller.signal, () => {}),
|
||||
).toMatchObject({ aborted: true })
|
||||
await expect(readFile(path.join(cwd, 'unwanted'))).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('finishes draining after a timely root exit even if a background process holds the pipes', async () => {
|
||||
const source = `const {spawn} = require('node:child_process'); const child = spawn(process.execPath, ['-e', 'setTimeout(() => {}, 30000)'], {detached: true, stdio: ['ignore', 1, 2]}); console.log(child.pid); child.unref()`
|
||||
const quote = (value: string) => `'${value.replaceAll("'", "'\\''")}'`
|
||||
const result = await shell.run(
|
||||
{ ...request(`${quote(process.execPath)} -e ${quote(source)}`), timeoutSec: 0.5 },
|
||||
new AbortController().signal,
|
||||
() => {},
|
||||
)
|
||||
const pid = Number((await readFile(path.join(cwd, '.pi/logs/bash-test.log'), 'utf8')).trim())
|
||||
try {
|
||||
expect(result).toMatchObject({ exitCode: 0, timedOut: false, aborted: false })
|
||||
expect(result.error).toBeUndefined()
|
||||
} finally {
|
||||
if (pid > 0) process.kill(pid, 'SIGKILL')
|
||||
}
|
||||
})
|
||||
|
||||
it('reports launch failure without including command text or skill secrets', async () => {
|
||||
shell = new RunnerShell(new RunnerFiles(cwd), undefined, {
|
||||
executable: path.join(cwd, 'missing-shell'),
|
||||
commandTransport: 'shell',
|
||||
})
|
||||
const result = await shell.run(
|
||||
request('echo private-command'),
|
||||
new AbortController().signal,
|
||||
() => {},
|
||||
)
|
||||
expect(result).toMatchObject({
|
||||
exitCode: null,
|
||||
signal: null,
|
||||
error: 'Command execution failed (ENOENT).',
|
||||
})
|
||||
expect(result.error).not.toContain('private-command')
|
||||
})
|
||||
|
||||
it('terminates a running process if the output consumer fails', async () => {
|
||||
let pid = 0
|
||||
const result = await shell.run(
|
||||
request('printf "%s\\n" "$$"; sleep 30'),
|
||||
new AbortController().signal,
|
||||
(_stream, data) => {
|
||||
pid = Number(data.trim())
|
||||
throw new Error('Output consumer failed')
|
||||
},
|
||||
)
|
||||
expect(result.error).toBe('Output consumer failed')
|
||||
expect(pid).toBeGreaterThan(0)
|
||||
expect(() => process.kill(pid, 0)).toThrow()
|
||||
})
|
||||
|
||||
it('rejects invalid timeouts and caps runaway output', async () => {
|
||||
expect(
|
||||
await shell.run(
|
||||
{ ...request('sleep 30'), timeoutSec: -1 },
|
||||
new AbortController().signal,
|
||||
() => {},
|
||||
),
|
||||
).toHaveProperty('error')
|
||||
expect(
|
||||
await shell.run(
|
||||
request('head -c 70000000 /dev/zero', 'large'),
|
||||
new AbortController().signal,
|
||||
() => {},
|
||||
),
|
||||
).toMatchObject({ error: 'Command exceeded the 64 MiB output limit.' })
|
||||
})
|
||||
|
||||
it('uses the exact SRT JSON snapshot and validates SRT before connecting', async () => {
|
||||
const settings = path.join(cwd, 'sandbox.json')
|
||||
const config = {
|
||||
network: { allowedDomains: [], deniedDomains: [] },
|
||||
filesystem: { denyRead: [], allowWrite: ['.pi'], denyWrite: [] },
|
||||
}
|
||||
const json = JSON.stringify(config)
|
||||
await writeFile(settings, json)
|
||||
vi.stubEnv('PATH', '')
|
||||
shell = new RunnerShell(new RunnerFiles(cwd), settings)
|
||||
await shell.initialize()
|
||||
await writeFile(settings, '{}')
|
||||
expect(
|
||||
await shell.run(request('echo sandboxed'), new AbortController().signal, () => {}),
|
||||
).toMatchObject({ exitCode: 0 })
|
||||
expect(SandboxManager.initialize).toHaveBeenCalledExactlyOnceWith(
|
||||
expect.objectContaining(config),
|
||||
)
|
||||
expect(SandboxManager.wrapWithSandboxArgv).toHaveBeenCalledWith(
|
||||
expect.stringContaining('echo sandboxed'),
|
||||
'/bin/sh',
|
||||
undefined,
|
||||
expect.any(AbortSignal),
|
||||
cwd,
|
||||
{ commandId: 'test', commandText: 'echo sandboxed' },
|
||||
)
|
||||
expect(SandboxManager.reset).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('keeps skill PATH and Node startup settings out of the SRT launcher', async () => {
|
||||
const settings = path.join(cwd, 'sandbox.json')
|
||||
const marker = path.join(cwd, 'injected')
|
||||
const evil = path.join(cwd, 'evil.cjs')
|
||||
await writeFile(
|
||||
settings,
|
||||
JSON.stringify({
|
||||
network: { allowedDomains: [], deniedDomains: [] },
|
||||
filesystem: { denyRead: [], allowWrite: ['.pi'], denyWrite: [] },
|
||||
}),
|
||||
)
|
||||
await writeFile(evil, `require('node:fs').writeFileSync('${marker}', 'escaped')`)
|
||||
shell = new RunnerShell(new RunnerFiles(cwd), settings)
|
||||
await shell.initialize()
|
||||
const result = await shell.run(
|
||||
{
|
||||
...request('printf secure'),
|
||||
env: { PATH: '/evil-srt', NODE_OPTIONS: `--require=${evil}` },
|
||||
},
|
||||
new AbortController().signal,
|
||||
() => {},
|
||||
)
|
||||
expect(result).toMatchObject({ exitCode: 0 })
|
||||
await expect(readFile(marker)).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('initializes one sandbox for parallel commands and resets only at shutdown', async () => {
|
||||
const settings = path.join(cwd, 'sandbox.json')
|
||||
await writeFile(
|
||||
settings,
|
||||
JSON.stringify({
|
||||
network: { allowedDomains: [], deniedDomains: [] },
|
||||
filesystem: { denyRead: [], allowWrite: ['.pi'], denyWrite: [] },
|
||||
}),
|
||||
)
|
||||
shell = new RunnerShell(new RunnerFiles(cwd), settings)
|
||||
await shell.initialize()
|
||||
const value = "quoted ' value\n$(echo should-not-expand)"
|
||||
let output = ''
|
||||
const results = await Promise.all(
|
||||
['one', 'two'].map((id) =>
|
||||
shell.run(
|
||||
{ ...request('printf "%s" "$SKILL_TOKEN"', id), env: { SKILL_TOKEN: value } },
|
||||
new AbortController().signal,
|
||||
(_stream, data) => {
|
||||
output += data
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
expect(results.every((result) => result.exitCode === 0)).toBe(true)
|
||||
expect(output).toBe(value.repeat(2))
|
||||
expect(SandboxManager.initialize).toHaveBeenCalledTimes(1)
|
||||
expect(SandboxManager.reset).not.toHaveBeenCalled()
|
||||
await shell.close()
|
||||
expect(SandboxManager.reset).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('fails startup and cleans up if sandbox initialization or execution fails', async () => {
|
||||
const settings = path.join(cwd, 'sandbox.json')
|
||||
await writeFile(
|
||||
settings,
|
||||
JSON.stringify({
|
||||
network: { allowedDomains: [], deniedDomains: [] },
|
||||
filesystem: { denyRead: [], allowWrite: ['.pi'], denyWrite: [] },
|
||||
}),
|
||||
)
|
||||
shell = new RunnerShell(new RunnerFiles(cwd), settings)
|
||||
vi.mocked(SandboxManager.initialize).mockRejectedValueOnce(
|
||||
new Error('Run windows-install first'),
|
||||
)
|
||||
await expect(shell.initialize()).rejects.toThrow('windows-install')
|
||||
expect(SandboxManager.reset).toHaveBeenCalledTimes(1)
|
||||
expect(
|
||||
await shell.run(request('echo unsafe'), new AbortController().signal, () => {}),
|
||||
).toHaveProperty('error', 'SRT has not been initialized.')
|
||||
vi.mocked(SandboxManager.wrapWithSandboxArgv).mockRejectedValueOnce(
|
||||
new Error('sandbox unavailable'),
|
||||
)
|
||||
await expect(shell.initialize()).rejects.toThrow('SRT preflight failed: sandbox unavailable')
|
||||
})
|
||||
|
||||
it('fails closed for missing or malformed SRT settings', async () => {
|
||||
const settings = path.join(cwd, 'sandbox.json')
|
||||
await expect(new RunnerShell(new RunnerFiles(cwd), settings).initialize()).rejects.toThrow()
|
||||
await writeFile(settings, 'invalid json')
|
||||
await expect(new RunnerShell(new RunnerFiles(cwd), settings).initialize()).rejects.toThrow()
|
||||
await writeFile(settings, '[]')
|
||||
await expect(new RunnerShell(new RunnerFiles(cwd), settings).initialize()).rejects.toThrow(
|
||||
/object/,
|
||||
)
|
||||
await writeFile(settings, JSON.stringify({ network: { allowedDomains: 42 } }))
|
||||
await expect(new RunnerShell(new RunnerFiles(cwd), settings).initialize()).rejects.toThrow()
|
||||
expect(SandboxManager.initialize).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,232 @@
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { execa } from 'execa'
|
||||
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
|
||||
import {
|
||||
runnerEnvironmentSchema,
|
||||
type RunnerEnvironment,
|
||||
type RunnerExecResultMessage,
|
||||
type RunnerExecStartMessage,
|
||||
} from '@shumai/dtos'
|
||||
import { commandEnv } from './config'
|
||||
import { RunnerFiles } from './files'
|
||||
import { resolveRunnerShell } from './shell-config'
|
||||
import { loadSandbox } from './srt'
|
||||
|
||||
const MAX_OUTPUT_BYTES = 64 * 1024 * 1024
|
||||
const MAX_TIMEOUT_SEC = 3600
|
||||
|
||||
export class RunnerShell {
|
||||
readonly environment: RunnerEnvironment
|
||||
private sandbox?: Awaited<ReturnType<typeof loadSandbox>>
|
||||
private initialized = false
|
||||
private active = 0
|
||||
|
||||
constructor(
|
||||
private readonly files: RunnerFiles,
|
||||
private readonly srtPath?: string,
|
||||
private readonly config = resolveRunnerShell(),
|
||||
) {
|
||||
this.environment = runnerEnvironmentSchema.parse({
|
||||
platform: process.platform,
|
||||
shell: config.executable,
|
||||
})
|
||||
}
|
||||
|
||||
async initialize() {
|
||||
if (!this.srtPath) return
|
||||
if (this.config.commandTransport === 'stdin')
|
||||
throw new Error(
|
||||
'SRT cannot run the legacy WSL Bash launcher. Install Git for Windows to use Bash with SRT.',
|
||||
)
|
||||
// Read and validate once; commands cannot weaken the policy by editing the file.
|
||||
this.sandbox = await loadSandbox(this.srtPath)
|
||||
try {
|
||||
await SandboxManager.initialize(this.sandbox.config)
|
||||
this.initialized = true
|
||||
let diagnostic = ''
|
||||
const probe = await this.run(
|
||||
{ type: 'exec.start', executionId: randomUUID(), command: 'exit 0', timeoutSec: 15 },
|
||||
new AbortController().signal,
|
||||
(_stream, data) => {
|
||||
diagnostic = (diagnostic + data).slice(-4096)
|
||||
},
|
||||
)
|
||||
if (probe.exitCode !== 0 || probe.error || probe.timedOut)
|
||||
throw new Error(
|
||||
`SRT preflight failed: ${probe.error || diagnostic.trim() || 'Check settings and local SRT prerequisites.'}`,
|
||||
)
|
||||
} catch (error) {
|
||||
await this.close()
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async close() {
|
||||
if (!this.sandbox) return
|
||||
try {
|
||||
await SandboxManager.reset()
|
||||
} finally {
|
||||
await this.sandbox.cleanup()
|
||||
this.sandbox = undefined
|
||||
this.initialized = false
|
||||
}
|
||||
}
|
||||
|
||||
async run(
|
||||
message: RunnerExecStartMessage,
|
||||
signal: AbortSignal,
|
||||
output: (stream: 'stdout' | 'stderr', data: string) => void,
|
||||
): Promise<RunnerExecResultMessage> {
|
||||
const result: RunnerExecResultMessage = {
|
||||
type: 'exec.result',
|
||||
executionId: message.executionId,
|
||||
exitCode: null,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
aborted: false,
|
||||
}
|
||||
if (this.srtPath) this.active++
|
||||
try {
|
||||
if (signal.aborted) return { ...result, aborted: true }
|
||||
const timeout = message.timeoutSec ?? 120
|
||||
if (!Number.isFinite(timeout) || timeout <= 0 || timeout > MAX_TIMEOUT_SEC)
|
||||
throw new Error(`Command timeout must be between 0 and ${MAX_TIMEOUT_SEC} seconds.`)
|
||||
const logPath = `.pi/logs/bash-${message.executionId}.log`
|
||||
const log = await this.files.createWriter(logPath)
|
||||
try {
|
||||
const windows = process.platform === 'win32'
|
||||
const { commandTransport } = this.config
|
||||
const useShell = commandTransport === 'shell'
|
||||
const sendCommandOnStdin = commandTransport === 'stdin'
|
||||
let executable = useShell ? message.command : this.config.executable
|
||||
let args = useShell ? [] : sendCommandOnStdin ? ['-s'] : ['-c', message.command]
|
||||
let shell: string | false = useShell && this.config.executable
|
||||
let env = commandEnv(message.env)
|
||||
if (this.srtPath) {
|
||||
if (!this.initialized) throw new Error('SRT has not been initialized.')
|
||||
const skillEnv = commandEnv(message.env, false)
|
||||
const command = windows
|
||||
? message.command
|
||||
: [
|
||||
...Object.entries(skillEnv).map(
|
||||
([key, value]) => `export ${key}='${value!.replaceAll("'", "'\\''")}'`,
|
||||
),
|
||||
message.command,
|
||||
].join('\n')
|
||||
const wrapped = await SandboxManager.wrapWithSandboxArgv(
|
||||
command,
|
||||
this.config.executable,
|
||||
undefined,
|
||||
signal,
|
||||
this.files.cwd,
|
||||
{ commandId: message.executionId, commandText: message.command },
|
||||
)
|
||||
;[executable, ...args] = wrapped.argv
|
||||
shell = false
|
||||
if (windows) {
|
||||
// SRT's broker starts a fresh sandbox-user environment. Explicit skill
|
||||
// values belong in its child overlay, never in the broker's startup env.
|
||||
const separator = args.indexOf('--')
|
||||
if (separator < 0) throw new Error('SRT returned an invalid Windows launch command.')
|
||||
args.splice(
|
||||
separator,
|
||||
0,
|
||||
...Object.entries(skillEnv).flatMap(([key, value]) => ['--env', `${key}=${value}`]),
|
||||
)
|
||||
env = wrapped.env
|
||||
} else env = commandEnv()
|
||||
}
|
||||
if (signal.aborted) return { ...result, aborted: true }
|
||||
const deadline = Date.now() + Math.ceil(timeout * 1000)
|
||||
const child = execa(executable, args, {
|
||||
cwd: this.files.cwd,
|
||||
env,
|
||||
extendEnv: false,
|
||||
shell,
|
||||
stdin: sendCommandOnStdin ? [Buffer.from(message.command)] : 'ignore',
|
||||
buffer: false,
|
||||
reject: false,
|
||||
stripFinalNewline: false,
|
||||
verbose: 'none',
|
||||
windowsHide: true,
|
||||
killDescendants: true,
|
||||
killSignal: 'SIGKILL',
|
||||
cancelSignal: signal,
|
||||
timeout: Math.ceil(timeout * 1000),
|
||||
})
|
||||
let bytes = 0
|
||||
let writes = Promise.resolve()
|
||||
let drainTimer: ReturnType<typeof setTimeout> | undefined
|
||||
let drainExpired = false
|
||||
let exitedWithinDeadline = false
|
||||
child.nodeChildProcess.once('exit', (exitCode) => {
|
||||
exitedWithinDeadline = exitCode !== null && Date.now() < deadline
|
||||
// Execa cleans up descendants where the OS still permits it. Bound
|
||||
// draining if a background child keeps inherited output pipes open.
|
||||
child.kill('SIGKILL')
|
||||
drainTimer = setTimeout(() => {
|
||||
drainExpired = true
|
||||
child.stdout.destroy()
|
||||
child.stderr.destroy()
|
||||
}, 1000)
|
||||
})
|
||||
try {
|
||||
const readers = (['stdout', 'stderr'] as const).map(async (stream) => {
|
||||
child[stream].setEncoding('utf8')
|
||||
try {
|
||||
for await (const data of child[stream]) {
|
||||
bytes += Buffer.byteLength(data)
|
||||
if (bytes > MAX_OUTPUT_BYTES)
|
||||
throw new Error('Command exceeded the 64 MiB output limit.')
|
||||
await (writes = writes.then(async () => {
|
||||
await log.write(data)
|
||||
output(stream, data)
|
||||
}))
|
||||
}
|
||||
} catch (error) {
|
||||
if (
|
||||
drainExpired &&
|
||||
error instanceof Error &&
|
||||
'code' in error &&
|
||||
error.code === 'ERR_STREAM_PREMATURE_CLOSE'
|
||||
)
|
||||
return
|
||||
result.error ??= error instanceof Error ? error.message : 'Output streaming failed.'
|
||||
child.kill('SIGKILL')
|
||||
}
|
||||
})
|
||||
const [completed] = await Promise.all([child, Promise.all(readers)])
|
||||
await writes
|
||||
result.exitCode = completed.exitCode ?? null
|
||||
result.signal = completed.signal ?? null
|
||||
// Execa's timeout includes stream draining. Report the command's
|
||||
// runtime, so a timely exit does not become a timeout while draining.
|
||||
result.timedOut = completed.timedOut && !exitedWithinDeadline
|
||||
result.aborted = completed.isCanceled
|
||||
if (
|
||||
completed.failed &&
|
||||
completed.exitCode === undefined &&
|
||||
!result.timedOut &&
|
||||
!result.aborted &&
|
||||
!completed.signal
|
||||
)
|
||||
result.error = `Command execution failed (${completed.code ?? 'unknown error'}).`
|
||||
result.fullOutputPath = logPath
|
||||
} finally {
|
||||
clearTimeout(drainTimer)
|
||||
}
|
||||
} finally {
|
||||
await log.close()
|
||||
}
|
||||
} catch (error) {
|
||||
result.aborted = signal.aborted
|
||||
result.error = error instanceof Error ? error.message : 'Command execution failed.'
|
||||
} finally {
|
||||
// SRT's temporary mount resources are shared: clean up only after every
|
||||
// concurrent command has finished, and keep the proxies until shutdown.
|
||||
if (this.srtPath && --this.active === 0 && this.initialized)
|
||||
SandboxManager.cleanupAfterCommand()
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// Release builds embed SRT's native helpers through scripts/srt-bundle.ts.
|
||||
export const bundledSrtVendor: Record<string, string> | undefined = undefined
|
||||
@@ -0,0 +1,59 @@
|
||||
import { mkdtemp, readFile, rm, stat, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import path from 'node:path'
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
import { loadSandbox } from './srt'
|
||||
|
||||
describe('runner SRT configuration', () => {
|
||||
let directory: string
|
||||
let settings: string
|
||||
const config = {
|
||||
network: { allowedDomains: [], deniedDomains: [] },
|
||||
filesystem: { denyRead: [], allowWrite: ['.pi'], denyWrite: [] },
|
||||
}
|
||||
beforeEach(async () => {
|
||||
directory = await mkdtemp(path.join(tmpdir(), 'shumai-srt-settings-'))
|
||||
settings = path.join(directory, 'sandbox.json')
|
||||
})
|
||||
afterEach(() => rm(directory, { recursive: true, force: true }))
|
||||
|
||||
it('validates upstream settings, supplies packaged helpers, and removes them on cleanup', async () => {
|
||||
await writeFile(settings, JSON.stringify(config))
|
||||
const sandbox = await loadSandbox(settings)
|
||||
const helper = sandbox.config.javaAgentJarPath!
|
||||
expect(sandbox.config).toMatchObject(config)
|
||||
expect((await readFile(helper)).subarray(0, 2).toString()).toBe('PK')
|
||||
if (process.platform === 'linux')
|
||||
expect(await stat(sandbox.config.seccomp!.applyPath!)).toHaveProperty(
|
||||
'size',
|
||||
expect.any(Number),
|
||||
)
|
||||
if (process.platform === 'win32')
|
||||
expect(await stat(sandbox.config.windows!.srtWin!.path!)).toHaveProperty(
|
||||
'size',
|
||||
expect.any(Number),
|
||||
)
|
||||
await sandbox.cleanup()
|
||||
await expect(stat(helper)).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('preserves explicit upstream helper paths', async () => {
|
||||
await writeFile(
|
||||
settings,
|
||||
JSON.stringify({
|
||||
...config,
|
||||
javaAgentJarPath: '/custom/proxy.jar',
|
||||
seccomp: { applyPath: '/custom/seccomp' },
|
||||
windows: { srtWin: { path: 'C:\\custom\\srt-win.exe' } },
|
||||
}),
|
||||
)
|
||||
const sandbox = await loadSandbox(settings)
|
||||
try {
|
||||
expect(sandbox.config.javaAgentJarPath).toBe('/custom/proxy.jar')
|
||||
expect(sandbox.config.seccomp?.applyPath).toBe('/custom/seccomp')
|
||||
expect(sandbox.config.windows?.srtWin?.path).toBe('C:\\custom\\srt-win.exe')
|
||||
} finally {
|
||||
await sandbox.cleanup()
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,59 @@
|
||||
import { createRequire } from 'node:module'
|
||||
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import path from 'node:path'
|
||||
import {
|
||||
SandboxRuntimeConfigSchema,
|
||||
type SandboxRuntimeConfig,
|
||||
} from '@anthropic-ai/sandbox-runtime'
|
||||
import { bundledSrtVendor } from './srt-vendor'
|
||||
|
||||
/** Load the upstream JSON format and supply the helpers bundled with this CLI. */
|
||||
export async function loadSandbox(configPath: string) {
|
||||
const config = SandboxRuntimeConfigSchema.parse(JSON.parse(await readFile(configPath, 'utf8')))
|
||||
const directory = await mkdtemp(path.join(tmpdir(), 'shumai-srt-'))
|
||||
const cleanup = () => rm(directory, { recursive: true, force: true })
|
||||
const asset = async (name: string) => {
|
||||
const encoded = bundledSrtVendor?.[name]
|
||||
const bytes =
|
||||
encoded !== undefined
|
||||
? Buffer.from(encoded, 'base64')
|
||||
: await readFile(
|
||||
path.join(
|
||||
path.dirname(
|
||||
createRequire(import.meta.url).resolve(
|
||||
'@anthropic-ai/sandbox-runtime/package.json',
|
||||
),
|
||||
),
|
||||
'vendor',
|
||||
name,
|
||||
),
|
||||
)
|
||||
const destination = path.join(directory, path.basename(name))
|
||||
await writeFile(destination, bytes, { mode: 0o500 })
|
||||
return destination
|
||||
}
|
||||
try {
|
||||
if (process.platform === 'linux' && !config.seccomp?.applyPath) {
|
||||
config.seccomp = {
|
||||
...config.seccomp,
|
||||
applyPath: await asset(`seccomp/${process.arch}/apply-seccomp`),
|
||||
}
|
||||
}
|
||||
if (process.platform === 'win32' && !config.windows?.srtWin?.path) {
|
||||
config.windows = {
|
||||
...config.windows,
|
||||
srtWin: {
|
||||
...config.windows?.srtWin,
|
||||
path: await asset(`srt-win/${process.arch}/srt-win.exe`),
|
||||
},
|
||||
}
|
||||
}
|
||||
if (!config.javaAgentJarPath)
|
||||
config.javaAgentJarPath = await asset('java-proxy-agent/srt-proxy-agent.jar')
|
||||
return { config: config satisfies SandboxRuntimeConfig, cleanup }
|
||||
} catch (error) {
|
||||
await cleanup()
|
||||
throw error
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/premiere-uxp",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"private": true,
|
||||
"type": "commonjs",
|
||||
"scripts": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/transcode-app",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"bin": {
|
||||
|
||||
@@ -67,6 +67,7 @@ apps/web/e2e/
|
||||
| Owner changes a team member role in the members dialog and permissions update | `tests/team/change-member-role.spec.ts` |
|
||||
| Owner manages resource quotas in settings and resets usage from the dashboard | `tests/team/quotas-settings.spec.ts` |
|
||||
| Owner creates an API token that is shown once, listed by hint, and authenticates API calls | `tests/team/api-tokens.spec.ts` |
|
||||
| Session logs stay within the dialog and scroll through long messages and expanded records on desktop and narrow screens | `tests/team/agent-session-logs.spec.ts` |
|
||||
|
||||
### project
|
||||
| Test | File |
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
import { expect, test } from '../../fixtures'
|
||||
|
||||
test('session logs stay within the dialog and scroll through long messages and expanded records', async ({
|
||||
owner,
|
||||
prisma,
|
||||
}) => {
|
||||
const { page, teamId, email } = owner
|
||||
const user = await prisma.user.findUniqueOrThrow({ where: { email } })
|
||||
const agentUser = await prisma.user.create({
|
||||
data: { name: 'Session Logs Agent', email: 'session-logs@shumai.test', type: 'agent' },
|
||||
})
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: agentUser.id,
|
||||
teamId,
|
||||
type: 'chat',
|
||||
enabled: false,
|
||||
config: { provider: 'test', model: 'test' },
|
||||
},
|
||||
})
|
||||
const session = await prisma.agentSession.create({
|
||||
data: {
|
||||
agentId: agent.id,
|
||||
userId: user.id,
|
||||
type: 'chat',
|
||||
name: 'Scrollable Session Logs',
|
||||
cwd: '/test',
|
||||
},
|
||||
})
|
||||
await prisma.agentSessionCommit.create({
|
||||
data: {
|
||||
sessionId: session.id,
|
||||
seq: 1n,
|
||||
data: {
|
||||
version: 1,
|
||||
writes: [
|
||||
{ type: 'conversation', value: { id: 1 } },
|
||||
...Array.from({ length: 30 }, (_, index) => ({
|
||||
type: 'entry',
|
||||
value: {
|
||||
id: index + 2,
|
||||
conversationId: 1,
|
||||
kind: 'pi.user',
|
||||
model: [
|
||||
{
|
||||
role: 'user',
|
||||
content: `Log message ${index + 1}\n\n\`\`\`text\n${'unbroken_output_'.repeat(100)}\n\`\`\``,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
],
|
||||
data: { output: 'raw_output_'.repeat(100) },
|
||||
},
|
||||
})),
|
||||
],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await page.goto(`/teams/${teamId}/dashboard`)
|
||||
await page.getByRole('button', { name: /^(Sessions|会话)$/ }).click()
|
||||
await page.getByRole('row').filter({ hasText: session.name! }).click()
|
||||
const dialog = page.getByRole('dialog')
|
||||
await expect(dialog.getByText('Log message 1', { exact: true })).toBeVisible()
|
||||
await expect(dialog.getByText(/^(Entry ID|条目 ID):/)).toHaveCount(0)
|
||||
const viewport = dialog.locator('[data-slot="scroll-area-viewport"]')
|
||||
const heading = dialog.getByRole('heading')
|
||||
|
||||
for (const size of [
|
||||
{ width: 1280, height: 800 },
|
||||
{ width: 390, height: 640 },
|
||||
]) {
|
||||
await page.setViewportSize(size)
|
||||
await dialog.evaluate(async (element) => {
|
||||
await Promise.all(element.getAnimations().map((animation) => animation.finished))
|
||||
})
|
||||
await expect
|
||||
.poll(async () => {
|
||||
return dialog.evaluate((element) => {
|
||||
const bounds = element.getBoundingClientRect()
|
||||
return (
|
||||
bounds.top >= 0 &&
|
||||
bounds.bottom <= window.innerHeight &&
|
||||
bounds.left >= 0 &&
|
||||
bounds.right <= window.innerWidth
|
||||
)
|
||||
})
|
||||
})
|
||||
.toBe(true)
|
||||
await expect
|
||||
.poll(() =>
|
||||
viewport.evaluate(
|
||||
(element) =>
|
||||
element.clientHeight > 0 &&
|
||||
element.scrollHeight > element.clientHeight &&
|
||||
element.scrollWidth <= element.clientWidth,
|
||||
),
|
||||
)
|
||||
.toBe(true)
|
||||
const headerTop = (await heading.boundingBox())!.y
|
||||
await viewport.evaluate((element) => {
|
||||
element.scrollTop = 0
|
||||
})
|
||||
await viewport.hover()
|
||||
// Wheel events may be capped; verify movement before jumping to the final record.
|
||||
await page.mouse.wheel(0, 400)
|
||||
await expect.poll(() => viewport.evaluate((element) => element.scrollTop)).toBeGreaterThan(0)
|
||||
const record = dialog.locator('details').last()
|
||||
await viewport.evaluate((element) => {
|
||||
element.scrollTop = element.scrollHeight
|
||||
})
|
||||
await expect(record.locator('summary')).toBeInViewport()
|
||||
expect((await heading.boundingBox())!.y).toBeCloseTo(headerTop, 0)
|
||||
|
||||
await record.locator('summary').click()
|
||||
await viewport.evaluate((element) => {
|
||||
element.scrollTop = element.scrollHeight
|
||||
})
|
||||
await expect(record.locator('pre')).toBeInViewport()
|
||||
await expect(record.locator('pre')).toContainText('"id": 31')
|
||||
expect(await viewport.evaluate((element) => element.scrollWidth <= element.clientWidth)).toBe(
|
||||
true,
|
||||
)
|
||||
await record.locator('summary').click()
|
||||
}
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/web-app",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
|
||||
+27
-25
@@ -5,23 +5,24 @@ loadEnvConfig(process.cwd(), process.env.NODE_ENV !== 'production')
|
||||
|
||||
import index from '@shumai/webui/index.html'
|
||||
|
||||
import { initAgentWorkflows } from '@shumai/agent'
|
||||
import { app } from '@shumai/api'
|
||||
import { closeAgentSessions, initAgentWorkflows } from '@shumai/agent'
|
||||
import { app, runnerWebsocket } from '@shumai/api'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import { migrateLegacyAgentAvatars } from '@shumai/core/src/agent/migration'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { validateArchiveStorageConfig } from '@shumai/core/src/s3/archive-storage'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
import { validateArchiveStorageConfig } from '@shumai/core/src/s3/archive-storage'
|
||||
import { initTranscodeWorkflows } from '@shumai/transcode'
|
||||
import { workflowService } from '@shumai/workflow-core'
|
||||
import { migrateLegacyAgentAvatars } from '@shumai/core/src/agent/migration'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
|
||||
import { handleDaemonCommands } from '@shumai/core/src/utils/daemon'
|
||||
import { authService } from '@shumai/core/src/auth/auth'
|
||||
import { embeddingService } from '@shumai/core/src/search/embedding-service'
|
||||
import { sandboxService } from '@shumai/core'
|
||||
import { notificationJobService } from '@shumai/core/src/notification/notification-job'
|
||||
import { runnerService } from '@shumai/core'
|
||||
import { autoArchiveJobService } from '@shumai/core/src/asset/auto-archive-job'
|
||||
import { authService } from '@shumai/core/src/auth/auth'
|
||||
import { notificationJobService } from '@shumai/core/src/notification/notification-job'
|
||||
import { embeddingService } from '@shumai/core/src/search/embedding-service'
|
||||
import { handleDaemonCommands } from '@shumai/core/src/utils/daemon'
|
||||
import { createHtmlHandler, createStaticFileHandler } from './static'
|
||||
import { getServerQueues } from './workers'
|
||||
|
||||
if (process.argv.includes('--check')) {
|
||||
console.log('✅ Web app evaluated successfully!')
|
||||
@@ -80,12 +81,20 @@ if (regenCmdIndex !== -1) {
|
||||
|
||||
async function run() {
|
||||
validateArchiveStorageConfig()
|
||||
const runnerLease = await runnerService.startServerTopology()
|
||||
runnerLease.addEventListener(
|
||||
'abort',
|
||||
() => {
|
||||
console.error('Lost the runner server lease:', runnerLease.reason)
|
||||
process.exit(1)
|
||||
},
|
||||
{ once: true },
|
||||
)
|
||||
runnerLease.throwIfAborted()
|
||||
// Initialize workflows and activities for local executor mode
|
||||
initAgentWorkflows()
|
||||
initTranscodeWorkflows()
|
||||
await sandboxService.ensureInitialized().catch((err) => {
|
||||
console.error('Failed to eagerly initialize sandbox in web app:', err)
|
||||
})
|
||||
runnerService.startHeartbeatMonitor()
|
||||
|
||||
// Start services
|
||||
await metadataService.syncSystemFields().catch(console.error)
|
||||
@@ -104,18 +113,7 @@ async function run() {
|
||||
}
|
||||
}
|
||||
|
||||
let listenQueues: string[] = ['agent_queue', 'transcode_queue', 'archive_queue']
|
||||
if (workersOption === 'none') {
|
||||
listenQueues = []
|
||||
} else if (workersOption === 'agent' || workersOption === 'ai') {
|
||||
listenQueues = ['agent_queue', 'archive_queue']
|
||||
} else if (workersOption === 'transcode') {
|
||||
listenQueues = ['transcode_queue']
|
||||
} else if (workersOption === 'archive') {
|
||||
listenQueues = ['archive_queue']
|
||||
}
|
||||
|
||||
workflowService.start(listenQueues)
|
||||
workflowService.start(getServerQueues(workersOption))
|
||||
workflowService.startStaleTaskRecovery()
|
||||
|
||||
const isProd = process.env.NODE_ENV === 'production'
|
||||
@@ -220,6 +218,7 @@ async function run() {
|
||||
: 1024 * 1024 * 1024 * 20, // Default 20GB
|
||||
development: !isProd,
|
||||
fetch: app.fetch,
|
||||
websocket: runnerWebsocket,
|
||||
routes,
|
||||
})
|
||||
|
||||
@@ -228,9 +227,12 @@ async function run() {
|
||||
const shutdown = async () => {
|
||||
console.log('\nShutting down gracefully...')
|
||||
workflowService.stopStaleTaskRecovery()
|
||||
runnerService.stopHeartbeatMonitor()
|
||||
await closeAgentSessions()
|
||||
await workflowService.shutdownWorkers().catch(() => {})
|
||||
assetService.stopCleanupJob()
|
||||
server.stop(true)
|
||||
await runnerService.stopServerTopology().catch(console.error)
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { getServerQueues } from './workers'
|
||||
|
||||
describe('server queues', () => {
|
||||
it.each(['none', 'agent', 'ai', 'archive'])(
|
||||
'keeps agent and archive embedded for %s',
|
||||
(option) => {
|
||||
expect(getServerQueues(option)).toEqual(['agent_queue', 'archive_queue'])
|
||||
},
|
||||
)
|
||||
|
||||
it.each(['all', 'transcode'])('also processes local transcoding for %s', (option) => {
|
||||
expect(getServerQueues(option)).toEqual(['agent_queue', 'archive_queue', 'transcode_queue'])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,8 @@
|
||||
import { TaskQueueAgent, TaskQueueArchive, TaskQueueTranscode } from '@shumai/workflow-core'
|
||||
|
||||
/** Agent and archive work always belongs to the server; only transcoding is optional. */
|
||||
export function getServerQueues(option: string): string[] {
|
||||
const queues = [TaskQueueAgent, TaskQueueArchive]
|
||||
if (option === 'all' || option === 'transcode') queues.push(TaskQueueTranscode)
|
||||
return queues
|
||||
}
|
||||
@@ -37,7 +37,6 @@ const allWorkers = [
|
||||
entrypoint: './apps/transcode/src/index.ts',
|
||||
naming: 'transcode-worker.[ext]',
|
||||
},
|
||||
{ name: 'agent', entrypoint: './apps/agent/src/index.ts', naming: 'agent-worker.[ext]' },
|
||||
]
|
||||
|
||||
const workersToBuild = target === 'all' ? allWorkers : allWorkers.filter((w) => w.name === target)
|
||||
|
||||
@@ -30,38 +30,36 @@
|
||||
"vitest": "4.1.11",
|
||||
},
|
||||
},
|
||||
"apps/agent": {
|
||||
"name": "@shumai/agent-app",
|
||||
"version": "0.6.5",
|
||||
"bin": {
|
||||
"shumai-agent": "./src/index.ts",
|
||||
},
|
||||
"dependencies": {
|
||||
"@shumai/agent": "workspace:*",
|
||||
"@shumai/core": "workspace:*",
|
||||
"@shumai/workflow-core": "workspace:*",
|
||||
},
|
||||
},
|
||||
"apps/cli": {
|
||||
"name": "@shumai/cli",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"bin": {
|
||||
"shumai-cli": "./src/index.ts",
|
||||
},
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sandbox-runtime": "0.0.83",
|
||||
"@shumai/api": "workspace:*",
|
||||
"@shumai/dtos": "workspace:*",
|
||||
"@shumai/operations": "workspace:*",
|
||||
"@shumai/tableprinter": "workspace:*",
|
||||
"commander": "15.0.0",
|
||||
"execa": "10.2.1",
|
||||
"hono": "4.13.5",
|
||||
"ulid": "3.0.2",
|
||||
"ws": "8.22.2",
|
||||
"yauzl": "3.4.0",
|
||||
"zod": "4.5.4",
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/adm-zip": "0.5.8",
|
||||
"@types/ws": "8.18.2",
|
||||
"@types/yauzl": "3.4.0",
|
||||
"adm-zip": "0.6.1",
|
||||
},
|
||||
},
|
||||
"apps/premiere-uxp": {
|
||||
"name": "@shumai/premiere-uxp",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"dependencies": {
|
||||
"@shumai/api": "workspace:*",
|
||||
"@shumai/dtos": "workspace:*",
|
||||
@@ -115,7 +113,7 @@
|
||||
},
|
||||
"apps/transcode": {
|
||||
"name": "@shumai/transcode-app",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"bin": {
|
||||
"shumai-transcode": "./src/index.ts",
|
||||
},
|
||||
@@ -127,7 +125,7 @@
|
||||
},
|
||||
"apps/web": {
|
||||
"name": "@shumai/web-app",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"dependencies": {
|
||||
"@shumai/agent": "workspace:*",
|
||||
"@shumai/api": "workspace:*",
|
||||
@@ -147,11 +145,11 @@
|
||||
},
|
||||
"packages/agent": {
|
||||
"name": "@shumai/agent",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sandbox-runtime": "0.0.74",
|
||||
"@earendil-works/pi-agent-core": "0.82.1",
|
||||
"@earendil-works/pi-ai": "0.82.1",
|
||||
"@earendil-works/chord": "1.5.0",
|
||||
"@earendil-works/pi-ai": "1.5.0",
|
||||
"@earendil-works/pi-durable": "1.5.0",
|
||||
"@google/genai": "2.28.0",
|
||||
"@shumai/core": "workspace:*",
|
||||
"@shumai/db": "workspace:*",
|
||||
@@ -171,9 +169,10 @@
|
||||
},
|
||||
"packages/api": {
|
||||
"name": "@shumai/api",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"dependencies": {
|
||||
"@better-auth/mcp": "1.7.12",
|
||||
"@earendil-works/pi-durable": "1.5.0",
|
||||
"@hono/zod-validator": "0.7.6",
|
||||
"@modelcontextprotocol/server": "2.0.0",
|
||||
"@shumai/core": "workspace:*",
|
||||
@@ -191,7 +190,7 @@
|
||||
},
|
||||
"packages/core": {
|
||||
"name": "@shumai/core",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"dependencies": {
|
||||
"@ai-sdk/alibaba": "2.0.38",
|
||||
"@ai-sdk/amazon-bedrock": "5.0.67",
|
||||
@@ -211,13 +210,13 @@
|
||||
"@ai-sdk/replicate": "3.0.34",
|
||||
"@ai-sdk/togetherai": "3.0.41",
|
||||
"@ai-sdk/xai": "4.0.49",
|
||||
"@anthropic-ai/sandbox-runtime": "0.0.74",
|
||||
"@aws-sdk/client-s3": "3.1121.0",
|
||||
"@aws-sdk/s3-request-presigner": "3.1121.0",
|
||||
"@better-auth/mcp": "1.7.12",
|
||||
"@better-auth/oauth-provider": "1.7.12",
|
||||
"@earendil-works/pi-agent-core": "0.82.1",
|
||||
"@earendil-works/pi-ai": "0.82.1",
|
||||
"@earendil-works/chord": "1.5.0",
|
||||
"@earendil-works/pi-ai": "1.5.0",
|
||||
"@earendil-works/pi-durable": "1.5.0",
|
||||
"@google/genai": "2.28.0",
|
||||
"@modelcontextprotocol/client": "2.0.0",
|
||||
"@shumai/db": "workspace:*",
|
||||
@@ -255,9 +254,8 @@
|
||||
},
|
||||
"packages/db": {
|
||||
"name": "@shumai/db",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "0.82.1",
|
||||
"@prisma/adapter-pg": "7.10.0",
|
||||
"@prisma/client": "7.10.0",
|
||||
"pg": "8.23.0",
|
||||
@@ -271,17 +269,19 @@
|
||||
},
|
||||
"packages/dtos": {
|
||||
"name": "@shumai/dtos",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "0.82.1",
|
||||
"@earendil-works/pi-ai": "1.5.0",
|
||||
"@earendil-works/pi-durable": "1.5.0",
|
||||
"zod": "4.5.4",
|
||||
},
|
||||
},
|
||||
"packages/e2e": {
|
||||
"name": "@shumai/e2e",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "0.82.1",
|
||||
"@earendil-works/pi-ai": "1.5.0",
|
||||
"@earendil-works/pi-durable": "1.5.0",
|
||||
"@google/genai": "2.28.0",
|
||||
"@shumai/agent": "workspace:*",
|
||||
"@shumai/core": "workspace:*",
|
||||
@@ -294,18 +294,18 @@
|
||||
},
|
||||
"packages/operations": {
|
||||
"name": "@shumai/operations",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"dependencies": {
|
||||
"zod": "4.5.4",
|
||||
},
|
||||
},
|
||||
"packages/tableprinter": {
|
||||
"name": "@shumai/tableprinter",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
},
|
||||
"packages/transcode": {
|
||||
"name": "@shumai/transcode",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"dependencies": {
|
||||
"@shumai/core": "workspace:*",
|
||||
"@shumai/db": "workspace:*",
|
||||
@@ -316,13 +316,15 @@
|
||||
},
|
||||
"packages/webui": {
|
||||
"name": "@shumai/webui",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"dependencies": {
|
||||
"@base-ui/react": "1.7.0",
|
||||
"@better-auth/oauth-provider": "1.7.12",
|
||||
"@dnd-kit/abstract": "0.5.0",
|
||||
"@dnd-kit/dom": "0.5.0",
|
||||
"@dnd-kit/react": "0.5.0",
|
||||
"@earendil-works/pi-ai": "1.5.0",
|
||||
"@earendil-works/pi-durable": "1.5.0",
|
||||
"@ferrucc-io/emoji-picker": "0.1.2",
|
||||
"@hookform/resolvers": "5.9.1",
|
||||
"@lexical/code": "0.49.0",
|
||||
@@ -419,7 +421,7 @@
|
||||
},
|
||||
"packages/workflow-core": {
|
||||
"name": "@shumai/workflow-core",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"dependencies": {
|
||||
"@dbos-inc/dbos-sdk": "5.2.11",
|
||||
"@shumai/core": "workspace:*",
|
||||
@@ -429,9 +431,6 @@
|
||||
},
|
||||
},
|
||||
},
|
||||
"patchedDependencies": {
|
||||
"@earendil-works/pi-ai@0.82.1": "patches/@earendil-works%2Fpi-ai@0.82.1.patch",
|
||||
},
|
||||
"packages": {
|
||||
"@adobe/premierepro": ["@adobe/premierepro@26.3.0", "", {}, "sha512-J84zEX8R4L5EU5EVVs3AWkd4LRoXPKueo28jPNfwwDAo69TOSNsAblImtsAmJR4HGDDazsvHkMQE3JBJqIcB9Q=="],
|
||||
|
||||
@@ -485,21 +484,13 @@
|
||||
|
||||
"@alloc/quick-lru": ["@alloc/quick-lru@5.2.0", "", {}, "sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw=="],
|
||||
|
||||
"@anthropic-ai/sandbox-runtime": ["@anthropic-ai/sandbox-runtime@0.0.74", "", { "dependencies": { "@pondwader/socks5-server": "^1.0.10", "commander": "^12.1.0", "node-forge": "^1.4.0", "zod": "^3.24.1" }, "bin": { "srt": "dist/cli.js" } }, "sha512-k6xD9HPp4gCwIAQtAloTBSaKGGNQhH7o1ck0057iBxu3iHclM2vx+JzIXLDB9j4oaP9hwGcNlWSP5K7bgc1CIQ=="],
|
||||
"@anthropic-ai/sandbox-runtime": ["@anthropic-ai/sandbox-runtime@0.0.83", "", { "dependencies": { "@pondwader/socks5-server": "^1.0.10", "commander": "^12.1.0", "node-forge": "^1.4.0", "zod": "^3.25.0" }, "peerDependencies": { "@microsoft/mxc-sdk": "^1.0.1" }, "optionalPeers": ["@microsoft/mxc-sdk"], "bin": { "srt": "dist/cli.js" } }, "sha512-yEusORnVJrNseF/9fvjVYyk6tr79/aFbIB3T/X8p2puLZdaaiGNeYN50v0CE+1DT70v7XuzKOAnTivh1f5cksA=="],
|
||||
|
||||
"@anthropic-ai/sdk": ["@anthropic-ai/sdk@0.91.1", "", { "dependencies": { "json-schema-to-ts": "^3.1.1" }, "peerDependencies": { "zod": "^3.25.0 || ^4.0.0" }, "optionalPeers": ["zod"], "bin": { "anthropic-ai-sdk": "bin/cli" } }, "sha512-LAmu761tSN9r66ixvmciswUj/ZC+1Q4iAfpedTfSVLeswRwnY3n2Nb6Tsk+cLPP28aLOPWeMgIuTuCcMC6W/iw=="],
|
||||
|
||||
"@aws-crypto/sha256-browser": ["@aws-crypto/sha256-browser@5.2.0", "", { "dependencies": { "@aws-crypto/sha256-js": "^5.2.0", "@aws-crypto/supports-web-crypto": "^5.2.0", "@aws-crypto/util": "^5.2.0", "@aws-sdk/types": "^3.222.0", "@aws-sdk/util-locate-window": "^3.0.0", "@smithy/util-utf8": "^2.0.0", "tslib": "^2.6.2" } }, "sha512-AXfN/lGotSQwu6HNcEsIASo7kWXZ5HYWvfOmSNKDsEqC4OashTp8alTmaz+F7TC2L083SFv5RdB+qU3Vs1kZqw=="],
|
||||
|
||||
"@aws-crypto/sha256-js": ["@aws-crypto/sha256-js@5.2.0", "", { "dependencies": { "@aws-crypto/util": "^5.2.0", "@aws-sdk/types": "^3.222.0", "tslib": "^2.6.2" } }, "sha512-FFQQyu7edu4ufvIZ+OadFpHHOt+eSTBaYaki44c+akjg7qZg9oOQeLlk77F6tSYqjDAFClrHJk9tMf0HdVyOvA=="],
|
||||
|
||||
"@aws-crypto/supports-web-crypto": ["@aws-crypto/supports-web-crypto@5.2.0", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-iAvUotm021kM33eCdNfwIN//F77/IADDSs58i+MDaOqFrVjZo9bAal0NK7HurRuWLLpF1iLX7gbWrjHjeo+YFg=="],
|
||||
|
||||
"@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=="],
|
||||
"@anthropic-ai/sdk": ["@anthropic-ai/sdk@0.129.0", "", { "dependencies": { "json-schema-to-ts": "^3.1.1", "standardwebhooks": "^1.0.0" }, "peerDependencies": { "zod": "^3.25.0 || ^4.0.0" }, "optionalPeers": ["zod"], "bin": { "anthropic-ai-sdk": "bin/cli" } }, "sha512-MH7LB20kNpGLUpPTe2OG5XvL/KhX8HUO9XyBaFjgFk6TLS4Xjt0VPIuMKywe7POKKPslX+QxSlVok9e+8kG5Nw=="],
|
||||
|
||||
"@aws-sdk/checksums": ["@aws-sdk/checksums@3.1000.29", "", { "dependencies": { "@aws-sdk/core": "^3.977.9", "@aws-sdk/types": "^3.974.5", "@smithy/core": "^3.33.3", "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-Dtu0gr4dnATZAPwEYbpCsG+MpLM7OAliy2gTepEFQwl1vZ6DL3QMH2FveMa3HLvPsOdhJsPRB3KtxVhph9T75A=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime": ["@aws-sdk/client-bedrock-runtime@3.1048.0", "", { "dependencies": { "@aws-crypto/sha256-browser": "5.2.0", "@aws-crypto/sha256-js": "5.2.0", "@aws-sdk/core": "^3.974.11", "@aws-sdk/credential-provider-node": "^3.972.42", "@aws-sdk/eventstream-handler-node": "^3.972.16", "@aws-sdk/middleware-eventstream": "^3.972.12", "@aws-sdk/middleware-websocket": "^3.972.19", "@aws-sdk/token-providers": "3.1048.0", "@aws-sdk/types": "^3.973.8", "@smithy/core": "^3.24.2", "@smithy/fetch-http-handler": "^5.4.2", "@smithy/node-http-handler": "^4.7.2", "@smithy/types": "^4.14.1", "tslib": "^2.6.2" } }, "sha512-u+NT61JZEkRFtpL0CAw1N1dwxnaLgwVXQl/zjJxTGgLyS/jTIdg2SdoEoCTHxgDyCnqa1HEi9QOoE9/pYRNpOQ=="],
|
||||
"@aws-sdk/client-bedrock-runtime": ["@aws-sdk/client-bedrock-runtime@3.1127.0", "", { "dependencies": { "@aws-sdk/core": "^3.977.9", "@aws-sdk/credential-provider-node": "^3.972.82", "@aws-sdk/eventstream-handler-node": "^3.972.34", "@aws-sdk/middleware-eventstream": "^3.972.29", "@aws-sdk/middleware-websocket": "^3.972.52", "@aws-sdk/token-providers": "3.1127.0", "@aws-sdk/types": "^3.974.5", "@smithy/core": "^3.33.3", "@smithy/fetch-http-handler": "^5.7.2", "@smithy/node-http-handler": "^4.11.3", "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-IDl/lrPb90aH+pZFHGNDmgH9nAUQj5PlZH1sJ3w7RikctyjHSnY3oNjZhrLoaBoQn/rNK0zsP6OHEqEhj2tdLA=="],
|
||||
|
||||
"@aws-sdk/client-s3": ["@aws-sdk/client-s3@3.1121.0", "", { "dependencies": { "@aws-sdk/checksums": "^3.1000.29", "@aws-sdk/core": "^3.977.9", "@aws-sdk/credential-provider-node": "^3.972.81", "@aws-sdk/middleware-sdk-s3": "^3.972.75", "@aws-sdk/signature-v4-multi-region": "^3.996.46", "@aws-sdk/types": "^3.974.5", "@smithy/core": "^3.33.3", "@smithy/fetch-http-handler": "^5.7.2", "@smithy/node-http-handler": "^4.11.3", "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-hBnoqaVBeWdkgXcJElMXA2yUZWkBCBntu2qmN+tfqmzC+j4LzJC3ox8qIgS2WdMS1cb8UwyBogUVrkRXybNm0A=="],
|
||||
|
||||
@@ -535,12 +526,10 @@
|
||||
|
||||
"@aws-sdk/signature-v4-multi-region": ["@aws-sdk/signature-v4-multi-region@3.996.46", "", { "dependencies": { "@aws-sdk/types": "^3.974.5", "@smithy/signature-v4": "^5.6.12", "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-L+2xZTye/2T96f3lwCws0Zw6GG2JHZW9e8FpVgGBeeExSKyeoZ6CWRpBml/7DNiK/O26jrgPM9F+Ay8VkgzUWQ=="],
|
||||
|
||||
"@aws-sdk/token-providers": ["@aws-sdk/token-providers@3.1048.0", "", { "dependencies": { "@aws-sdk/core": "^3.974.11", "@aws-sdk/nested-clients": "^3.997.9", "@aws-sdk/types": "^3.973.8", "@smithy/core": "^3.24.2", "@smithy/types": "^4.14.1", "tslib": "^2.6.2" } }, "sha512-k0y/GcuesuSfWyUM0WamrGyeZmltRYaPbHO82UDA6mZ/doB+FOHKutikPAtSXMn/hDz970cF+iRuuiYO9VEbAA=="],
|
||||
"@aws-sdk/token-providers": ["@aws-sdk/token-providers@3.1127.0", "", { "dependencies": { "@aws-sdk/core": "^3.977.9", "@aws-sdk/nested-clients": "^3.997.44", "@aws-sdk/types": "^3.974.5", "@smithy/core": "^3.33.3", "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-Dv2TMWBshJ+tF6ahs2Sy5bh4Iabsd4GAQqVvE9XZmYmnoaVbpS2QKIKE/HRacc7bTtbjEEvP+laGzHvHlf1CiQ=="],
|
||||
|
||||
"@aws-sdk/types": ["@aws-sdk/types@3.974.5", "", { "dependencies": { "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-LkwLL2BLbC6wNNm4JaH9mbEqBMdOZCct6VAYqhdN4U1xrWM+fUJQEfbHwQgDypapOWTRtlk25akb5afM0P8CIQ=="],
|
||||
|
||||
"@aws-sdk/util-locate-window": ["@aws-sdk/util-locate-window@3.965.10", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-ycwH6Zd2GhuSqdXX9ihbCjeGTB6xOJs+O3+Jb8/zDG9978XU80qs75dfkPJRMNKe5MvBZPuNeFpd4JZKPoUF4g=="],
|
||||
|
||||
"@aws-sdk/xml-builder": ["@aws-sdk/xml-builder@3.972.40", "", { "dependencies": { "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-wlFmCIGUlwF4zx/kncw+bmxTQh1HeSJq4mYV/V5cZUSJadDP3kXvGW8Rn21cimj/7y9ju+47oYWXi97vF7czaA=="],
|
||||
|
||||
"@aws/lambda-invoke-store": ["@aws/lambda-invoke-store@0.3.0", "", {}, "sha512-sl4Bm6yiMNYrZKkqqDFWN0UfnWhlS8ivKxrYl+6t0gCLrqr8y3B2IqZZbFRkfaVVp7C/baApyh71P+LeE1A2sQ=="],
|
||||
@@ -629,9 +618,17 @@
|
||||
|
||||
"@dnd-kit/state": ["@dnd-kit/state@0.5.0", "", { "dependencies": { "@preact/signals-core": "^1.10.0", "tslib": "^2.6.2" } }, "sha512-y7XbabQqjF58Lk8YmDQuR8l6QjN+Kh4qlGEjUvHuIeasLk1QP+9L5diXS98VMxQIivyMmUtX2//f+3N7qPJX4w=="],
|
||||
|
||||
"@earendil-works/pi-agent-core": ["@earendil-works/pi-agent-core@0.82.1", "", { "dependencies": { "@earendil-works/pi-ai": "^0.82.1", "diff": "8.0.4", "ignore": "7.0.5", "typebox": "1.1.38", "yaml": "2.9.0" } }, "sha512-Z3kloziJIE2dmrisRckZX8zDca/gIv9/YdFAzeoqpHiLV2wsni6bL4hInNSjVKLbqT+4kqLIkph2JQLKvSepjg=="],
|
||||
"@earendil-works/chord": ["@earendil-works/chord@1.5.0", "", { "dependencies": { "@earendil-works/pi-schema": "1.5.0", "@earendil-works/pi-telemetry": "1.5.0" }, "peerDependencies": { "esbuild": "0.28.2", "ws": "8.21.0" }, "optionalPeers": ["esbuild", "ws"] }, "sha512-jf555zbCM21E03DVBDPD/Tuae0lz64YjaHx/athUxNZ/H7phVVb0dWfZsaYnxaXPQ1odW8iHEoVLOlE/prvUPg=="],
|
||||
|
||||
"@earendil-works/pi-ai": ["@earendil-works/pi-ai@0.82.1", "", { "dependencies": { "@anthropic-ai/sdk": "0.91.1", "@aws-sdk/client-bedrock-runtime": "3.1048.0", "@google/genai": "1.52.0", "@mistralai/mistralai": "2.2.6", "@opentelemetry/api": "1.9.0", "@smithy/node-http-handler": "4.7.3", "http-proxy-agent": "7.0.2", "https-proxy-agent": "7.0.6", "openai": "6.26.0", "partial-json": "0.1.7", "typebox": "1.1.38" }, "bin": { "pi-ai": "dist/cli.js" } }, "sha512-3WFYRhEp3lQB3444EhPMBcM7zSaEUE3eJgHOR7s4081NLqbw/FsWilIKWXSua0Gv3sRr7m9xMidR3pPDE7jI/A=="],
|
||||
"@earendil-works/pi-ai": ["@earendil-works/pi-ai@1.5.0", "", { "dependencies": { "@anthropic-ai/sdk": "0.129.0", "@aws-sdk/client-bedrock-runtime": "3.1127.0", "@earendil-works/pi-schema": "1.5.0", "@earendil-works/pi-telemetry": "1.5.0", "@google/genai": "2.21.0", "@smithy/node-http-handler": "4.12.1", "http-proxy-agent": "9.1.0", "https-proxy-agent": "9.1.0", "openai": "7.19.0", "partial-json": "0.1.7" }, "bin": { "pi-ai": "dist/cli.js" } }, "sha512-VeWxNqTHSqRCf8AcY/6kvNwh+neRL6MPxZPePVztIQy6qqK9MR/YoCSN/ZgDnIyCD3pqEXgRyQbzk4KV4uvjbg=="],
|
||||
|
||||
"@earendil-works/pi-codemode": ["@earendil-works/pi-codemode@1.5.0", "", { "dependencies": { "quickjs-wasi": "3.6.2" } }, "sha512-PLBJZ17AEWGzOlAAcFuOkw3PxkOti0JlFuZQyeF2GRTqbpuYo4k9t8YqUV2uOQVzrUnKdxahwaFUqc1eNS1WAg=="],
|
||||
|
||||
"@earendil-works/pi-durable": ["@earendil-works/pi-durable@1.5.0", "", { "dependencies": { "@earendil-works/chord": "1.5.0", "@earendil-works/pi-ai": "1.5.0", "@earendil-works/pi-codemode": "1.5.0", "@earendil-works/pi-schema": "1.5.0", "@earendil-works/pi-telemetry": "1.5.0", "@silvia-odwyer/photon": "0.3.3", "diff": "8.0.4" } }, "sha512-xeBP2vIo5NvsCMQ4Y/V+Z164xQEi9JWiBWHdjn4Z15EnFqV3c0FxJAX26FqyyyintrYyk+Enre94GNcvRlBA5g=="],
|
||||
|
||||
"@earendil-works/pi-schema": ["@earendil-works/pi-schema@1.5.0", "", {}, "sha512-RW0MbT2tR65OY6xMsgB/yVBb7m/RTNwA+9hurSN1vVXfHZRVmVnXbruBPrx/PzbrQnW5yLVlMRWxErGDQtF1Hg=="],
|
||||
|
||||
"@earendil-works/pi-telemetry": ["@earendil-works/pi-telemetry@1.5.0", "", {}, "sha512-VW3tcVdnNfl/Vk8wHPTzZdKkxcsCOM12ppiO4DXnd9wMxUCmOfLt/wvUnWeKWbTVlVSRg0uqKtnnhE2UCsG2Bw=="],
|
||||
|
||||
"@electric-sql/pglite": ["@electric-sql/pglite@0.4.3", "", {}, "sha512-ichuWTgtd4mOM1G4SpyGJa5trT03lWbMypDV0fUXUCXg5hiHqVAz/bZyV68NqmkLB7WcYmj1RMJVSp8HV/v/ZQ=="],
|
||||
|
||||
@@ -833,8 +830,6 @@
|
||||
|
||||
"@lix-js/sdk-win32-x64": ["@lix-js/sdk-win32-x64@0.12.3", "", { "os": "win32", "cpu": "x64" }, "sha512-hnxMI7kPS4ngXs0ZVnOve5CmtC4+vaNIyNDi/yWU0rSvxj7mqw/1RCegTrCB7LjxlX4DG+eg9HruXurk9NDjGg=="],
|
||||
|
||||
"@mistralai/mistralai": ["@mistralai/mistralai@2.2.6", "", { "dependencies": { "@opentelemetry/semantic-conventions": "^1.40.0", "ws": "^8.18.0", "zod": "^3.25.0 || ^4.0.0", "zod-to-json-schema": "^3.25.0" }, "peerDependencies": { "@opentelemetry/api": "^1.9.0" }, "optionalPeers": ["@opentelemetry/api"] }, "sha512-W8pX7zHxjJvMIpw8JMxeJEleapXX0Q9NPszdNzqkM3MIEoIGPObdodujj+WHteXEvGfaP/AMwlNyRfEzSY6dQQ=="],
|
||||
|
||||
"@modelcontextprotocol/client": ["@modelcontextprotocol/client@2.0.0", "", { "dependencies": { "@modelcontextprotocol/core": "2.0.0", "cross-spawn": "^7.0.5", "eventsource": "^3.0.2", "eventsource-parser": "^3.0.0", "jose": "^6.1.3", "pkce-challenge": "^5.0.0", "zod": "^4.2.0" } }, "sha512-8f1OghQ2rjzIOfqgUCP+8GiUWqRs89njoWLNqAe8kWmDePv3s1fZXseej+QXemssEuuOvLLmLO/kqM3IQHtISw=="],
|
||||
|
||||
"@modelcontextprotocol/core": ["@modelcontextprotocol/core@2.0.0", "", { "dependencies": { "zod": "^4.2.0" } }, "sha512-pJCEwGG7Lfr/+PQp9ZTwKXNeO5wzbfKL7H3MYpCorM4oFBoQrdjnBgEoqG+RjhsvS1FKrDbKux+M1HhlnGWqcA=="],
|
||||
@@ -869,10 +864,6 @@
|
||||
|
||||
"@noble/hashes": ["@noble/hashes@2.4.0", "", {}, "sha512-X5XaVWZIBCT7HHZGm5I7ZQXDwLG+bGXuSrMQAW+7Zvl87h1kmc1ZB1VSRJcpUfoUrGQp4Fkoxm5kZ+Ms+aW+eA=="],
|
||||
|
||||
"@opentelemetry/api": ["@opentelemetry/api@1.9.0", "", {}, "sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg=="],
|
||||
|
||||
"@opentelemetry/semantic-conventions": ["@opentelemetry/semantic-conventions@1.43.0", "", {}, "sha512-eSYWTm620tTk45EKSedaUL8MFYI8hW164hIXsgIHyxu3VobUB3fFCu5t0hQby6OoWRPsG1KkKUG2M5UadiLiVg=="],
|
||||
|
||||
"@oven/bun-darwin-aarch64": ["@oven/bun-darwin-aarch64@1.4.0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-GCpf8QuFLsyioVawP5HrMxA1ZRBlu6Hq9RNnSc3UTUWAzIxBso9trjoZczw1HdgpqSssFkszfIV2zmOzFTjhkw=="],
|
||||
|
||||
"@oven/bun-darwin-x64": ["@oven/bun-darwin-x64@1.4.0", "", { "os": "darwin", "cpu": "x64" }, "sha512-cIrhwOr0SPEraewznhC+c/k6TG8bwFn5uZ4EJuXwjiKJLcAF36q7/bGjWkeXSe48JwMcPRUR054JXF7+cRwSSA=="],
|
||||
@@ -1117,9 +1108,9 @@
|
||||
|
||||
"@rolldown/pluginutils": ["@rolldown/pluginutils@1.0.1", "", {}, "sha512-2j9bGt5Jh8hj+vPtgzPtl72j0yRxHAyumoo6TNfAjsLB04UtpSvPbPcDcBMxz7n+9CYB0c1GxQFxYRg2jimqGw=="],
|
||||
|
||||
"@shumai/agent": ["@shumai/agent@workspace:packages/agent"],
|
||||
"@sec-ant/readable-stream": ["@sec-ant/readable-stream@0.4.1", "", {}, "sha512-831qok9r2t8AlxLko40y2ebgSDhenenCatLVeW/uBtnHPyhHOvG0C7TvfgecV+wHzIm5KUICgzmVpWS+IMEAeg=="],
|
||||
|
||||
"@shumai/agent-app": ["@shumai/agent-app@workspace:apps/agent"],
|
||||
"@shumai/agent": ["@shumai/agent@workspace:packages/agent"],
|
||||
|
||||
"@shumai/api": ["@shumai/api@workspace:packages/api"],
|
||||
|
||||
@@ -1149,10 +1140,14 @@
|
||||
|
||||
"@shumai/workflow-core": ["@shumai/workflow-core@workspace:packages/workflow-core"],
|
||||
|
||||
"@silvia-odwyer/photon": ["@silvia-odwyer/photon@0.3.3", "", {}, "sha512-8BhUjEch4slwRe8uXnaA4vcA5uiiOTT90UMsxulOj2gN98X1p0q9Z4Ysk4DkD05uNgbR9XoSqtZ37w+33w4QKQ=="],
|
||||
|
||||
"@sinclair/typebox": ["@sinclair/typebox@0.34.52", "", {}, "sha512-XiMQh7qqVlxZzcVD+kkGMNGMzcTrDMLWI7S4x7z1MkCkbDPrekpZXEUK0eZqZFMuHQg2a2DZOcDIh9o5v3Gonw=="],
|
||||
|
||||
"@sindresorhus/is": ["@sindresorhus/is@4.6.0", "", {}, "sha512-t09vSN3MdfsyCHoFcTRCH/iUtG7OJ0CsjzB8cjAmKc/va/kIgeDI/TxsigdncE/4be734m0cvIYwNaV4i2XqAw=="],
|
||||
|
||||
"@sindresorhus/merge-streams": ["@sindresorhus/merge-streams@4.0.0", "", {}, "sha512-tlqY9xq5ukxTUZBmoOp+m61cqwQD5pHJtFY3Mn8CA8ps6yghLH/Hw8UPdqg4OLmFW3IFlcXnQNmo/dh8HzXYIQ=="],
|
||||
|
||||
"@smithy/core": ["@smithy/core@3.33.3", "", { "dependencies": { "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-CsOeKq/9kA3y6VJHt+/+VTCtBaxJ4OTFpgrjIUhPpDIKxBci1k2bJaQASF2h/ELWrulGp+t97DZ0mevfAD8idg=="],
|
||||
|
||||
"@smithy/credential-provider-imds": ["@smithy/credential-provider-imds@4.5.2", "", { "dependencies": { "@smithy/core": "^3.33.2", "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-A9uSdn72ozbRUSit0eib0TW7nXuNPlaeM0zcGkJ+nE6tFcSDbnmtwoxbTCFBukVQcszDAyvsd7+rTduPTXpygg=="],
|
||||
@@ -1161,16 +1156,12 @@
|
||||
|
||||
"@smithy/fetch-http-handler": ["@smithy/fetch-http-handler@5.7.2", "", { "dependencies": { "@smithy/core": "^3.33.2", "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-nZyWTmSpJEXl6VtWVMBJve/7x12DZu6sIX1z1a+ZMaHlQQRs9Zpu6NbTe/gmxYXVRpkjxyDYpZ5gx2IM6f/Wkw=="],
|
||||
|
||||
"@smithy/is-array-buffer": ["@smithy/is-array-buffer@2.2.0", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-GGP3O9QFD24uGeAXYUjwSTXARoqpZykHadOmA8G5vfJPK0/DC67qa//0qvqrJzL1xc8WQWX7/yc7fwudjPHPhA=="],
|
||||
|
||||
"@smithy/node-http-handler": ["@smithy/node-http-handler@4.7.3", "", { "dependencies": { "@smithy/core": "^3.24.3", "@smithy/types": "^4.14.2", "tslib": "^2.6.2" } }, "sha512-/jPhevcTFPMVl6KNjbaI47iOg1zxC7IsnX4PQDGVZKMFceOXtB8IEYaB7a9VvkP/3oC60WzTeKocvSI7vLT0vA=="],
|
||||
"@smithy/node-http-handler": ["@smithy/node-http-handler@4.12.1", "", { "dependencies": { "@smithy/core": "^3.33.3", "@smithy/types": "^4.18.0", "tslib": "^2.6.2" } }, "sha512-ThMkboGeONWXAelq9FvGsuJC4rOi+qyC4/zhUF58xYpxUg5sQKx2VXZYJmtNjr4dSuBJ1HeJXETQILCz3wOHvw=="],
|
||||
|
||||
"@smithy/signature-v4": ["@smithy/signature-v4@5.7.3", "", { "dependencies": { "@smithy/core": "^3.33.3", "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-7ImGm+FkHRLcBaRttIAMZ6bzJZWb2cJGoYjq46F2UjycujWzrL9GEN9h4w7eQyXJYnltrUhxbbieBAIRrdqpow=="],
|
||||
|
||||
"@smithy/types": ["@smithy/types@4.17.2", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-FOKpVZob9MPTn2znRzGrnsMHv7BOsKVw3XiP/cOyYLDVZ9qKp4nifIiSCuUU/fIj5Vu0UOAxCFr+qRAtG0NUkA=="],
|
||||
|
||||
"@smithy/util-buffer-from": ["@smithy/util-buffer-from@2.2.0", "", { "dependencies": { "@smithy/is-array-buffer": "^2.2.0", "tslib": "^2.6.2" } }, "sha512-IJdWBbTcMQ6DA0gdNhh/BwrLkDR+ADW5Kr1aZmd4k3DIF6ezMV4R2NIAmT08wQJ3yUK82thHWmC/TnK/wpMMIA=="],
|
||||
|
||||
"@smithy/util-utf8": ["@smithy/util-utf8@4.5.2", "", { "dependencies": { "@smithy/core": "^3.33.2", "tslib": "^2.6.2" } }, "sha512-7wNWV7SugHpcMA7uzEawJNpE0GrasXM7a9E+1+Wm6NxVuDClESac/AKt+G7jMZNUz5vBLWqKlqVV7Sv7AtUr6Q=="],
|
||||
|
||||
"@spectrum-web-components/action-button": ["@spectrum-web-components/action-button@0.37.0", "", { "dependencies": { "@spectrum-web-components/base": "^0.37.0", "@spectrum-web-components/button": "^0.37.0", "@spectrum-web-components/icon": "^0.37.0", "@spectrum-web-components/icons-ui": "^0.37.0", "@spectrum-web-components/shared": "^0.37.0" } }, "sha512-cU/5sHpRSA823sHUH6jZccmizkTxWr/LJHTDJ4jj0LxCLh15OHfxKM+hu/vcc5fXYrAPma5Hp54xTBc/l/frDA=="],
|
||||
@@ -1245,6 +1236,8 @@
|
||||
|
||||
"@sqlite.org/sqlite-wasm": ["@sqlite.org/sqlite-wasm@3.48.0-build4", "", { "bin": { "sqlite-wasm": "bin/index.js" } }, "sha512-hI6twvUkzOmyGZhQMza1gpfqErZxXRw6JEsiVjUbo7tFanVD+8Oil0Ih3l2nGzHdxPI41zFmfUQG7GHqhciKZQ=="],
|
||||
|
||||
"@stablelib/base64": ["@stablelib/base64@1.0.1", "", {}, "sha512-1bnPQqSxSuc3Ii6MhBysoWCg58j97aUjuCSZrGSmDxNqtytIi0k8utUenAwTZN4V5mXXYGsVUI9zeBqy+jBOSQ=="],
|
||||
|
||||
"@standard-schema/spec": ["@standard-schema/spec@1.1.0", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="],
|
||||
|
||||
"@standard-schema/utils": ["@standard-schema/utils@0.3.0", "", {}, "sha512-e7Mew686owMaPJVNNLs55PUvgz371nKgwsc4vxE49zsODpJEnxgxRo2y/OKrqueavXgZNMDVj3DdHFlaSAeU8g=="],
|
||||
@@ -1539,9 +1532,9 @@
|
||||
|
||||
"@types/whatwg-mimetype": ["@types/whatwg-mimetype@3.0.2", "", {}, "sha512-c2AKvDT8ToxLIOUlN51gTiHXflsfIFisS4pO7pDPoKouJCESkhZnEy623gwP9laCy5lnLDAw1vAzu2vM2YLOrA=="],
|
||||
|
||||
"@types/ws": ["@types/ws@8.18.1", "", { "dependencies": { "@types/node": "*" } }, "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg=="],
|
||||
"@types/ws": ["@types/ws@8.18.2", "", { "dependencies": { "@types/node": "*" } }, "sha512-67MQl+fpWKVTT1NYdnmo3U4sc/xPo/zQBncVnI74qmQa0z/b+1g6iYqNmGCPbxO+zz2aklb08a0oHfegiVd0/w=="],
|
||||
|
||||
"@types/yauzl": ["@types/yauzl@2.10.3", "", { "dependencies": { "@types/node": "*" } }, "sha512-oJoftv0LSuaDZE3Le4DbKX+KS9G36NzOeSap90UIK0yMA/NhKJhqlSGtNDORNRaIbQfzjXDrQa0ytJ6mNRGz/Q=="],
|
||||
"@types/yauzl": ["@types/yauzl@3.4.0", "", { "dependencies": { "@types/node": "*" } }, "sha512-NRPn5w6h8dhcnmx3YIRQcqMywY/+nND/uOkJessedcrowO3C0AssHp3tMJpxKAwOhFOo0OV1y9VtsC5hbKKBAw=="],
|
||||
|
||||
"@typescript-eslint/eslint-plugin": ["@typescript-eslint/eslint-plugin@8.68.0", "", { "dependencies": { "@eslint-community/regexpp": "^4.12.2", "@typescript-eslint/scope-manager": "8.68.0", "@typescript-eslint/type-utils": "8.68.0", "@typescript-eslint/utils": "8.68.0", "@typescript-eslint/visitor-keys": "8.68.0", "ignore": "^7.0.5", "natural-compare": "^1.4.0", "ts-api-utils": "^2.5.0" }, "peerDependencies": { "@typescript-eslint/parser": "^8.68.0", "eslint": "^8.57.0 || ^9.0.0 || ^10.0.0", "typescript": ">=4.8.4 <6.1.0" } }, "sha512-WASHDpCm6qO5jj9g1a+8NiW5+GCkAyLReR56/4VruYmNgfUmqpxOfZ2Yfb8xGfJPWv5Qi6LSD8sXdces3vbp/Q=="],
|
||||
|
||||
@@ -1615,7 +1608,7 @@
|
||||
|
||||
"adm-zip": ["adm-zip@0.6.0", "", {}, "sha512-XleryMhbuksdKtofnWZ9Sk+4CUTbms4Mb/EU32SZwToAyZ5RgVos/ki8n+yr0LWHOGKuakbXTuuYNHLQjhddgg=="],
|
||||
|
||||
"agent-base": ["agent-base@7.1.4", "", {}, "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ=="],
|
||||
"agent-base": ["agent-base@9.0.0", "", {}, "sha512-TQf59BsZnytt8GdJKLPfUZ54g/iaUL2OWDSFCCvMOhsHduDQxO8xC4PNeyIkVcA5KwL2phPSv0douC0fgWzmnA=="],
|
||||
|
||||
"ai": ["ai@7.0.84", "", { "dependencies": { "@ai-sdk/gateway": "4.0.68", "@ai-sdk/provider": "4.0.8", "@ai-sdk/provider-utils": "5.0.33" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-znHNBWADOy79k8EK7636kDm5LmnjF4tMzegC2TfmlLDobDZbs0z0OXD2sxtH3eDTp5Re+pJQESvjinWJ6+kadQ=="],
|
||||
|
||||
@@ -1999,6 +1992,8 @@
|
||||
|
||||
"eventsource-parser": ["eventsource-parser@3.1.1", "", {}, "sha512-EKN1vKAMcZ8MlYMpaNuxN6R9yakzH6uajHcHVTqWJzvu5pWw9DyhbP35HH8MVBQ+dZjAfDxk+A8NiR9KWaXiyQ=="],
|
||||
|
||||
"execa": ["execa@10.2.1", "", { "dependencies": { "@sindresorhus/merge-streams": "^4.0.0", "figures": "^6.1.0", "get-stream": "^9.0.1", "human-signals": "^8.0.1", "is-plain-obj": "^4.1.0", "is-stream": "^4.0.1", "npm-run-path": "^6.0.0", "pretty-ms": "^9.3.1", "signal-exit": "^4.1.0", "strip-final-newline": "^4.0.0", "which-command": "^0.1.0", "yoctocolors": "^2.2.0" } }, "sha512-/Bofl3ZBh2Q4/PG0W63qFzP5vDAypi8Zc90UWTBzaDNIYiX7apPOd0N6L8hG8CojUdq4V51adu8BObP+iVsSTw=="],
|
||||
|
||||
"exiftool-vendored": ["exiftool-vendored@38.1.0", "", { "dependencies": { "@photostructure/tz-lookup": "^11.7.0", "@types/luxon": "^3.7.5", "batch-cluster": "^19.2.0", "he": "^1.2.0", "luxon": "^3.7.2" }, "optionalDependencies": { "exiftool-vendored.exe": "13.59.2", "exiftool-vendored.pl": "13.59.2" } }, "sha512-LIERqM/Qb0oqJlSswLFKWsxGVH81skp7omsX2BPKA/dEzfLP5syNO5KL7k5UlMCf+m0jqEXrKTQr/xV5vScPow=="],
|
||||
|
||||
"exiftool-vendored.exe": ["exiftool-vendored.exe@13.59.2", "", { "os": "win32" }, "sha512-U0l0CApIGO+AjpFYCiNCApQsMdlf6sqCyEl4wlKKqvwVAEVcvMRw91k7zj32OPFZE+/+XlLFsc6876MpOqBLiw=="],
|
||||
@@ -2031,6 +2026,8 @@
|
||||
|
||||
"fast-safe-stringify": ["fast-safe-stringify@2.1.1", "", {}, "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA=="],
|
||||
|
||||
"fast-sha256": ["fast-sha256@1.3.0", "", {}, "sha512-n11RGP/lrWEFI/bWdygLxhI+pVeo1ZYIVwvvPkW7azl/rOy+F3HYRZ2K5zeE9mmkhQppyv9sQFx0JM9UabnpPQ=="],
|
||||
|
||||
"fast-uri": ["fast-uri@3.1.6", "", {}, "sha512-7Ical1vFEMr0onbVzEDIreM22I4khW+fzyQPwvAFWBp1iwdshSZRsL4jjRvPG9JP1uiqMHRto+YU6R2/CzDz5Q=="],
|
||||
|
||||
"fd-slicer": ["fd-slicer@1.1.0", "", { "dependencies": { "pend": "~1.2.0" } }, "sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g=="],
|
||||
@@ -2041,6 +2038,8 @@
|
||||
|
||||
"fflate": ["fflate@0.8.3", "", {}, "sha512-tbZNuJrLwGUp3zshBtdy4W+ORxZuIh8a5ilyIEQDC5rY1f3U20JMry0Ll3WBzU58EZKsEuJFXhb5gwv8CsPvgA=="],
|
||||
|
||||
"figures": ["figures@6.1.0", "", { "dependencies": { "is-unicode-supported": "^2.0.0" } }, "sha512-d+l3qxjSesT4V7v2fh+QnmFnUWv9lSpjarhShNTgBOfA0ttejbQUAlHLitbjkoRiDulW0OPoQPYIGhIC8ohejg=="],
|
||||
|
||||
"file-entry-cache": ["file-entry-cache@8.0.0", "", { "dependencies": { "flat-cache": "^4.0.0" } }, "sha512-XXTUwCvisa5oacNGRP9SfNtYBNAMi+RPwBFmblZEF7N7swHYQS6/Zfk7SRwx4D5j3CH211YNRco1DEMNVfZCnQ=="],
|
||||
|
||||
"find-my-way": ["find-my-way@9.7.0", "", { "dependencies": { "fast-deep-equal": "^3.1.3", "fast-querystring": "^1.0.0", "safe-regex2": "^5.0.0" } }, "sha512-f2JHn75x2JlwUwLenZypgczR7YWMb/uO9BvUXtus+JMgkbIkLADd38cI4EiV+OQqrGo1Zlq6V8wnqMJ8e62wUQ=="],
|
||||
@@ -2097,7 +2096,7 @@
|
||||
|
||||
"get-proto": ["get-proto@1.0.1", "", { "dependencies": { "dunder-proto": "^1.0.1", "es-object-atoms": "^1.0.0" } }, "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g=="],
|
||||
|
||||
"get-stream": ["get-stream@5.2.0", "", { "dependencies": { "pump": "^3.0.0" } }, "sha512-nBF+F1rAZVCu/p7rjzgA+Yb4lfYXrpl7a6VmJrU8wF9I1CKvP/QwPNZHnOlwbTkY6dvtFIzFMSyQXbLoTQPRpA=="],
|
||||
"get-stream": ["get-stream@9.0.1", "", { "dependencies": { "@sec-ant/readable-stream": "^0.4.1", "is-stream": "^4.0.1" } }, "sha512-kVCxPF3vQM/N0B1PmoqVUqgHP+EeVjmZSQn+1oCRPxd2P21P2F19lIgbR3HBosbB1PUhOAoctJnfEn2GbN2eZA=="],
|
||||
|
||||
"get-symbol-description": ["get-symbol-description@1.1.0", "", { "dependencies": { "call-bound": "^1.0.3", "es-errors": "^1.3.0", "get-intrinsic": "^1.2.6" } }, "sha512-w9UMqWwJxHNOvoNzSJ2oPF5wvYcvP7jUvYzhp67yEhTi17ZDBBC1z9pTdGuzjD+EFIqLSYRweZjqfiPzQ06Ebg=="],
|
||||
|
||||
@@ -2153,9 +2152,11 @@
|
||||
|
||||
"html-url-attributes": ["html-url-attributes@3.0.1", "", {}, "sha512-ol6UPyBWqsrO6EJySPz2O7ZSr856WDrEzM5zMqp+FJJLGMW35cLYmmZnl0vztAZxRUoNZJFTCohfjuIJ8I4QBQ=="],
|
||||
|
||||
"http-proxy-agent": ["http-proxy-agent@7.0.2", "", { "dependencies": { "agent-base": "^7.1.0", "debug": "^4.3.4" } }, "sha512-T1gkAiYYDWYx3V5Bmyu7HcfcvL7mUrTWiM6yOfa3PIphViJ/gFPbvidQ+veqSOHci/PxBcDabeUNCzpOODJZig=="],
|
||||
"http-proxy-agent": ["http-proxy-agent@9.1.0", "", { "dependencies": { "agent-base": "9.0.0", "debug": "^4.3.4", "proxy-agent-negotiate": "1.1.0" } }, "sha512-2NxoveTT58mjYT4n3RPTEfCZGLMbidoO8XEieXfpSYxu+PQJ1qpx4ypwH6N+uF9twBPIvRRgvkvW5HUTYWENig=="],
|
||||
|
||||
"https-proxy-agent": ["https-proxy-agent@7.0.6", "", { "dependencies": { "agent-base": "^7.1.2", "debug": "4" } }, "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw=="],
|
||||
"https-proxy-agent": ["https-proxy-agent@9.1.0", "", { "dependencies": { "agent-base": "9.0.0", "debug": "^4.3.4", "proxy-agent-negotiate": "1.1.0" } }, "sha512-ag87y7cJJ9/3+GxFr8Oy4O5faDsGRGnBGsJj/YjOSsSx/5eadKLYTMPlzuR6obgoCDDm0abAAZitXXQkMOPSpA=="],
|
||||
|
||||
"human-signals": ["human-signals@8.0.1", "", {}, "sha512-eKCa6bwnJhvxj14kZk5NCPc6Hb6BdsU9DZcOnmQKSnO1VKrfV0zCvtttPZUsBvjmNDn8rpcJfpwSYnHBjc95MQ=="],
|
||||
|
||||
"iconv-lite": ["iconv-lite@0.7.3", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "sha512-IKXpvIzjnC9XTAUbVBcMfGS0EPaIXtW6v+zr+RRp+hqULEpo0owZax6wyRwPOJbWbzjYspQwusTsfVr0ifh4uQ=="],
|
||||
|
||||
@@ -2231,7 +2232,7 @@
|
||||
|
||||
"is-shared-array-buffer": ["is-shared-array-buffer@1.0.4", "", { "dependencies": { "call-bound": "^1.0.3" } }, "sha512-ISWac8drv4ZGfwKl5slpHG9OwPNty4jOWPRIhBpxOoD+hqITiwuipOQ2bNthAzwA3B4fIjO4Nln74N0S9byq8A=="],
|
||||
|
||||
"is-stream": ["is-stream@2.0.1", "", {}, "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg=="],
|
||||
"is-stream": ["is-stream@4.0.1", "", {}, "sha512-Dnz92NInDqYckGEUJv689RbRiTSEHCQ7wOVeALbkOz999YpqT46yMRIGtSNl2iCL1waAZSx40+h59NV/EwzV/A=="],
|
||||
|
||||
"is-string": ["is-string@1.1.1", "", { "dependencies": { "call-bound": "^1.0.3", "has-tostringtag": "^1.0.2" } }, "sha512-BtEeSsoaQjlSPBemMQIrY1MY0uM6vnS1g5fmufYOtnxLGUZM2178PKbhsk7Ffv58IX+ZtcvoGwccYsh0PglkAA=="],
|
||||
|
||||
@@ -2239,6 +2240,8 @@
|
||||
|
||||
"is-typed-array": ["is-typed-array@1.1.15", "", { "dependencies": { "which-typed-array": "^1.1.16" } }, "sha512-p3EcsicXjit7SaskXHs1hA91QxgTw46Fv6EFKKGS5DRFLD8yKnohjF3hxoju94b/OcMZoQukzpPpBE9uLVKzgQ=="],
|
||||
|
||||
"is-unicode-supported": ["is-unicode-supported@2.1.0", "", {}, "sha512-mE00Gnza5EEB3Ds0HfMyllZzbBrmLOX3vfWoj9A9PEnTfratQ/BcaJOuMhnkhjXvb2+FkY3VuHqtAGpTPmglFQ=="],
|
||||
|
||||
"is-weakmap": ["is-weakmap@2.0.2", "", {}, "sha512-K5pXYOm9wqY1RgjpL3YTkF39tni1XajUIkawTLUo9EZEVUFga5gSQJF8nNS7ZwJQ02y+1YCNYcMh+HIf1ZqE+w=="],
|
||||
|
||||
"is-weakref": ["is-weakref@1.1.1", "", { "dependencies": { "call-bound": "^1.0.3" } }, "sha512-6i9mGWSlqzNMEqpCp93KwRS1uUOodk2OJ6b+sq7ZPDSy2WuI5NFIxp/254TytR8ftefexkWn5xNiHUNpPOfSew=="],
|
||||
@@ -2463,7 +2466,7 @@
|
||||
|
||||
"node-fetch": ["node-fetch@3.3.2", "", { "dependencies": { "data-uri-to-buffer": "^4.0.0", "fetch-blob": "^3.1.4", "formdata-polyfill": "^4.0.10" } }, "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA=="],
|
||||
|
||||
"node-forge": ["node-forge@1.4.0", "", {}, "sha512-LarFH0+6VfriEhqMMcLX2F7SwSXeWwnEAJEsYm5QKWchiVYVvJyV9v7UDvUv+w5HO23ZpQTXDv/GxdDdMyOuoQ=="],
|
||||
"node-forge": ["node-forge@1.4.3", "", {}, "sha512-P6wzfh6t70avmV8FrVEAd/36sOPJQ+hkaJVX1Laap9J/77m7M0eyozDvc8SwnDf4P7MP94gwbAAo0rg3YVXT+g=="],
|
||||
|
||||
"node-releases": ["node-releases@2.0.54", "", {}, "sha512-YHs7BmmcsdAI5Ozuf8JZo6PT0mv2GIWC9vMfvUC3dp65M8hn7Ux8CPL+2oBI7juNuj9d0ndhTcznq2ODBps9cQ=="],
|
||||
|
||||
@@ -2471,6 +2474,8 @@
|
||||
|
||||
"normalize-path": ["normalize-path@3.0.0", "", {}, "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA=="],
|
||||
|
||||
"npm-run-path": ["npm-run-path@6.0.0", "", { "dependencies": { "path-key": "^4.0.0", "unicorn-magic": "^0.3.0" } }, "sha512-9qny7Z9DsQU8Ou39ERsPU4OZQlSTP47ShQzuKZ6PRXpYLtIFgl/DEBYEXKlvcEa+9tHVcK8CF81Y2V72qaZhWA=="],
|
||||
|
||||
"object-assign": ["object-assign@4.1.1", "", {}, "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg=="],
|
||||
|
||||
"object-inspect": ["object-inspect@1.13.4", "", {}, "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew=="],
|
||||
@@ -2493,7 +2498,7 @@
|
||||
|
||||
"once": ["once@1.4.0", "", { "dependencies": { "wrappy": "1" } }, "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w=="],
|
||||
|
||||
"openai": ["openai@6.26.0", "", { "peerDependencies": { "ws": "^8.18.0", "zod": "^3.25 || ^4.0" }, "optionalPeers": ["ws", "zod"], "bin": { "openai": "bin/cli" } }, "sha512-zd23dbWTjiJ6sSAX6s0HrCZi41JwTA1bQVs0wLQPZ2/5o2gxOJA5wh7yOAUgwYybfhDXyhwlpeQf7Mlgx8EOCA=="],
|
||||
"openai": ["openai@7.19.0", "", { "peerDependencies": { "@aws-sdk/credential-provider-node": ">=3.972.0 <4", "@smithy/hash-node": ">=4.3.0 <5", "@smithy/signature-v4": ">=5.4.0 <6", "undici": ">=5 <9", "ws": "^8.21.0", "zod": "^3.25 || ^4.0" }, "optionalPeers": ["@aws-sdk/credential-provider-node", "@smithy/hash-node", "@smithy/signature-v4", "undici", "ws", "zod"] }, "sha512-MX2s3u2L5racTO0CC/SWpCOasJQBCJrqLKXK+l82cAhdeF8mPMBEe/gxMm0ZFa2xpKpOFLRjxv5afYEZbBXmbQ=="],
|
||||
|
||||
"optionator": ["optionator@0.9.4", "", { "dependencies": { "deep-is": "^0.1.3", "fast-levenshtein": "^2.0.6", "levn": "^0.4.1", "prelude-ls": "^1.2.1", "type-check": "^0.4.0", "word-wrap": "^1.2.5" } }, "sha512-6IpQ7mKUxRcZNLIObR0hz7lxsapSSIYNZJwXPGeF0mTVqGKFIXj1DQcMoT22S3ROcLyY/rz0PWaWZ9ayWmad9g=="],
|
||||
|
||||
@@ -2515,6 +2520,8 @@
|
||||
|
||||
"parse-entities": ["parse-entities@4.0.2", "", { "dependencies": { "@types/unist": "^2.0.0", "character-entities-legacy": "^3.0.0", "character-reference-invalid": "^2.0.0", "decode-named-character-reference": "^1.0.0", "is-alphanumerical": "^2.0.0", "is-decimal": "^2.0.0", "is-hexadecimal": "^2.0.0" } }, "sha512-GG2AQYWoLgL877gQIKeRPGO1xF9+eG1ujIb5soS5gPvLQ1y2o8FL90w2QWNdf9I361Mpp7726c+lj3U0qK1uGw=="],
|
||||
|
||||
"parse-ms": ["parse-ms@4.0.0", "", {}, "sha512-TXfryirbmq34y8QBwgqCVLi+8oA3oWx2eAnSn62ITyEhEYaWRlVZ2DvMM9eZbMs/RfxPu/PK/aBLyGj4IrqMHw=="],
|
||||
|
||||
"partial-json": ["partial-json@0.1.7", "", {}, "sha512-Njv/59hHaokb/hRUjce3Hdv12wd60MtM9Z5Olmn+nehe0QDAsRtRbJPvJ0Z91TusF0SuZRIvnM+S4l6EIP8leA=="],
|
||||
|
||||
"path-exists": ["path-exists@4.0.0", "", {}, "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w=="],
|
||||
@@ -2597,6 +2604,8 @@
|
||||
|
||||
"pretty-format": ["pretty-format@27.5.1", "", { "dependencies": { "ansi-regex": "^5.0.1", "ansi-styles": "^5.0.0", "react-is": "^17.0.1" } }, "sha512-Qb1gy5OrP5+zDf2Bvnzdl3jsTf1qXVMazbvCoKhtKqVs4/YK4ozX4gKQJJVyNe+cajNPn0KoC0MC3FUmaHWEmQ=="],
|
||||
|
||||
"pretty-ms": ["pretty-ms@9.3.1", "", { "dependencies": { "parse-ms": "^4.0.0" } }, "sha512-HzMy3Geq23nVALD/M2LliU+F+M+gVNsvkQWWqeBZ8HDiCgzo6YPJ/Omrmtq24EFrIsk0a3EkQGEd7bDOo+IhGA=="],
|
||||
|
||||
"prisma": ["prisma@7.10.0", "", { "dependencies": { "@prisma/config": "7.10.0", "@prisma/dev": "0.24.17", "@prisma/engines": "7.10.0", "@prisma/studio-core": "0.33.0", "mysql2": "3.15.3", "postgres": "3.4.7" }, "peerDependencies": { "better-sqlite3": ">=9.0.0", "typescript": ">=5.4.0" }, "optionalPeers": ["better-sqlite3", "typescript"], "bin": { "prisma": "build/index.js" } }, "sha512-o0ornyJOWgygVAzGCpr8PdXV8EJLHyVGDDUr/voBQt8Azzw8cYTByzzPGcA/m4tCkPcnJA8raEOv2CslsKhPEw=="],
|
||||
|
||||
"prisma-json-types-generator": ["prisma-json-types-generator@5.1.1", "", { "dependencies": { "@prisma/generator-helper": "^7.8.0", "semver": "^7.8.5", "try": "^1.0.3", "tslib": "^2.8.1", "typescript": "npm:typescript@^6.0.3" }, "peerDependencies": { "@prisma/client": "^7.8.0", "prisma": "^7.8.0" }, "bin": { "prisma-json-types-generator": "index.js" } }, "sha512-vtj68YkCla7bEnblC54xzKU3w+H/1uoKxtLU3F2eC9LL8j1Dz8GadVagKhQqQd03KGfHmO7o83Tf3yXxCKQwzQ=="],
|
||||
@@ -2619,6 +2628,8 @@
|
||||
|
||||
"protobufjs": ["protobufjs@7.6.6", "", { "dependencies": { "@protobufjs/aspromise": "^1.1.2", "@protobufjs/base64": "^1.1.2", "@protobufjs/codegen": "^2.0.5", "@protobufjs/eventemitter": "^1.1.1", "@protobufjs/fetch": "^1.1.1", "@protobufjs/float": "^1.0.2", "@protobufjs/path": "^1.1.2", "@protobufjs/pool": "^1.1.0", "@protobufjs/utf8": "^1.1.1", "@types/node": ">=13.7.0", "long": "^5.3.2" } }, "sha512-dYDWdjSl5RNb7SgPxGQcRU+GtvP7s2fpkrY0r432PcOIaZ0/rBcxEZnQN67iJhFuQiVw754JDoPruPCNdGsbjg=="],
|
||||
|
||||
"proxy-agent-negotiate": ["proxy-agent-negotiate@1.1.0", "", { "peerDependencies": { "kerberos": "^2.0.0" }, "optionalPeers": ["kerberos"] }, "sha512-N8IBcM3UgCVzz2L2Lqv8DVntDnnC8/hiV4nEDUPkqq72TPUgYWjQc+bdZlBPZK9LzPAvOY//gAt0S0DApoOXWQ=="],
|
||||
|
||||
"pump": ["pump@3.0.4", "", { "dependencies": { "end-of-stream": "^1.1.0", "once": "^1.3.1" } }, "sha512-VS7sjc6KR7e1ukRFhQSY5LM2uBWAUPiOPa/A3mkKmiMwSmRFUITt0xuj+/lesgnCv+dPIEYlkzrcyXgquIHMcA=="],
|
||||
|
||||
"punycode": ["punycode@2.3.1", "", {}, "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg=="],
|
||||
@@ -2627,6 +2638,8 @@
|
||||
|
||||
"quick-format-unescaped": ["quick-format-unescaped@4.0.4", "", {}, "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg=="],
|
||||
|
||||
"quickjs-wasi": ["quickjs-wasi@3.6.2", "", {}, "sha512-FCqGtGOrMgzUiIrMNMA2YnsOxCNwo31dzqXvclXUC6xeT35NJLKXQJsvbeCTjvoFAwZgEAPg8U6+KAPDGXn8Mg=="],
|
||||
|
||||
"radix-ui": ["radix-ui@1.6.7", "", { "dependencies": { "@radix-ui/primitive": "1.1.7", "@radix-ui/react-accessible-icon": "1.1.15", "@radix-ui/react-accordion": "1.2.20", "@radix-ui/react-alert-dialog": "1.1.23", "@radix-ui/react-arrow": "1.1.15", "@radix-ui/react-aspect-ratio": "1.1.15", "@radix-ui/react-avatar": "1.2.6", "@radix-ui/react-checkbox": "1.3.11", "@radix-ui/react-collapsible": "1.1.20", "@radix-ui/react-collection": "1.1.15", "@radix-ui/react-compose-refs": "1.1.5", "@radix-ui/react-context": "1.2.2", "@radix-ui/react-context-menu": "2.3.7", "@radix-ui/react-dialog": "1.1.23", "@radix-ui/react-direction": "1.1.4", "@radix-ui/react-dismissable-layer": "1.1.19", "@radix-ui/react-dropdown-menu": "2.1.24", "@radix-ui/react-focus-guards": "1.1.6", "@radix-ui/react-focus-scope": "1.1.16", "@radix-ui/react-form": "0.1.16", "@radix-ui/react-hover-card": "1.1.23", "@radix-ui/react-label": "2.1.15", "@radix-ui/react-menu": "2.1.24", "@radix-ui/react-menubar": "1.1.24", "@radix-ui/react-navigation-menu": "1.2.22", "@radix-ui/react-one-time-password-field": "0.1.16", "@radix-ui/react-password-toggle-field": "0.1.11", "@radix-ui/react-popover": "1.1.23", "@radix-ui/react-popper": "1.3.7", "@radix-ui/react-portal": "1.1.17", "@radix-ui/react-presence": "1.1.10", "@radix-ui/react-primitive": "2.1.10", "@radix-ui/react-progress": "1.1.16", "@radix-ui/react-radio-group": "1.4.7", "@radix-ui/react-roving-focus": "1.1.19", "@radix-ui/react-scroll-area": "1.2.18", "@radix-ui/react-select": "2.3.7", "@radix-ui/react-separator": "1.1.15", "@radix-ui/react-slider": "1.4.7", "@radix-ui/react-slot": "1.3.3", "@radix-ui/react-switch": "1.3.7", "@radix-ui/react-tabs": "1.1.21", "@radix-ui/react-toast": "1.2.23", "@radix-ui/react-toggle": "1.1.18", "@radix-ui/react-toggle-group": "1.1.19", "@radix-ui/react-toolbar": "1.1.19", "@radix-ui/react-tooltip": "1.2.16", "@radix-ui/react-use-callback-ref": "1.1.4", "@radix-ui/react-use-controllable-state": "1.2.6", "@radix-ui/react-use-effect-event": "0.0.5", "@radix-ui/react-use-escape-keydown": "1.1.5", "@radix-ui/react-use-is-hydrated": "0.1.3", "@radix-ui/react-use-layout-effect": "1.1.4", "@radix-ui/react-use-size": "1.1.4", "@radix-ui/react-visually-hidden": "1.2.11" }, "peerDependencies": { "@types/react": "*", "@types/react-dom": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc", "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react", "@types/react-dom"] }, "sha512-QBdhh1arIEUvPC0dQ5+nwWAxt7+N+oP/9jPwjJkGFoSk/sqxg32gJtSXGtFh8frAIcS6oC9cx2Q+7KYCQLOAeA=="],
|
||||
|
||||
"rc9": ["rc9@3.0.1", "", { "dependencies": { "defu": "^6.1.6", "destr": "^2.0.5" } }, "sha512-gMDyleLWVE+i6Sgtc0QbbY6pEKqYs97NGi6isHQPqYlLemPoO8dxQ3uGi0f4NiP98c+jMW6cG1Kx9dDwfvqARQ=="],
|
||||
@@ -2755,7 +2768,7 @@
|
||||
|
||||
"siginfo": ["siginfo@2.0.0", "", {}, "sha512-ybx0WO1/8bSBLEWXZvEd7gMW3Sn3JFlW3TvX1nREbDLRNQNaeNN8WK0meBwPdAaOI7TtRRRJn/Es1zhrrCHu7g=="],
|
||||
|
||||
"signal-exit": ["signal-exit@3.0.7", "", {}, "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ=="],
|
||||
"signal-exit": ["signal-exit@4.1.0", "", {}, "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw=="],
|
||||
|
||||
"skin-tone": ["skin-tone@2.0.0", "", { "dependencies": { "unicode-emoji-modifier-base": "^1.0.0" } }, "sha512-kUMbT1oBJCpgrnKoSr0o6wPtvRWT9W9UKvGLwfJYO2WuahZRHOpEyL1ckyMGgMWh0UdpmaoFqKKD29WTomNEGA=="],
|
||||
|
||||
@@ -2781,6 +2794,8 @@
|
||||
|
||||
"stackback": ["stackback@0.0.2", "", {}, "sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw=="],
|
||||
|
||||
"standardwebhooks": ["standardwebhooks@1.1.1", "", { "dependencies": { "@stablelib/base64": "^1.0.0", "fast-sha256": "^1.3.0" } }, "sha512-bCbX9ZEyFkWPsRz7Bl3NuQUJohmwGSev/yhr7vhaGPlc4AfIrspIRa6cPTBuI1ItmrTDJ4d/S2hCsfe4+vQGnQ=="],
|
||||
|
||||
"std-env": ["std-env@4.2.0", "", {}, "sha512-oCUKSupKTHX53EyjDtuZQ64pjLJ6yYCtpmEw0goYxtjG9KpbRe8KAsl2tBUGU9DyMcJ0RwJ8GqJAFzMXcXW1Rw=="],
|
||||
|
||||
"stop-iteration-iterator": ["stop-iteration-iterator@1.1.0", "", { "dependencies": { "es-errors": "^1.3.0", "internal-slot": "^1.1.0" } }, "sha512-eLoXW/DHyl62zxY4SCaIgnRhuMr6ri4juEYARS8E6sCEqzKpOiE521Ucofdx+KnDZl5xmvGYaaKCk5FEOxJCoQ=="],
|
||||
@@ -2809,6 +2824,8 @@
|
||||
|
||||
"strip-ansi-cjs": ["strip-ansi@6.0.1", "", { "dependencies": { "ansi-regex": "^5.0.1" } }, "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A=="],
|
||||
|
||||
"strip-final-newline": ["strip-final-newline@4.0.0", "", {}, "sha512-aulFJcD6YK8V1G7iRB5tigAP4TsHBZZrOV8pjV++zdUwmeV8uzbY7yn6h9MswN62adStNZFuCIx4haBnRuMDaw=="],
|
||||
|
||||
"strip-json-comments": ["strip-json-comments@5.0.3", "", {}, "sha512-1tB5mhVo7U+ETBKNf92xT4hrQa3pm0MZ0PQvuDnWgAAGHDsfp4lPSpiS6psrSiet87wyGPh9ft6wmhOMQ0hDiw=="],
|
||||
|
||||
"style-to-js": ["style-to-js@1.1.21", "", { "dependencies": { "style-to-object": "1.0.14" } }, "sha512-RjQetxJrrUJLQPHbLku6U/ocGtzyjbJMP9lCNK7Ag0CNh690nSH8woqWH9u16nMjYBAok+i7JO1NP2pOy8IsPQ=="],
|
||||
@@ -2907,6 +2924,8 @@
|
||||
|
||||
"unicode-trie": ["unicode-trie@2.0.0", "", { "dependencies": { "pako": "^0.2.5", "tiny-inflate": "^1.0.0" } }, "sha512-x7bc76x0bm4prf1VLg79uhAzKw8DVboClSN5VxJuQ+LKDOVEW9CdH+VY7SP+vX7xCYQqzzgQpFqz15zeLvAtZQ=="],
|
||||
|
||||
"unicorn-magic": ["unicorn-magic@0.3.0", "", {}, "sha512-+QBBXBCvifc56fsbuxZQ6Sic3wqqc3WWaqxs58gvJrcOuN83HGTCwz3oS5phzU9LthRNE9VrJCFCLUgHeeFnfA=="],
|
||||
|
||||
"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=="],
|
||||
|
||||
"unist-util-is": ["unist-util-is@6.0.1", "", { "dependencies": { "@types/unist": "^3.0.0" } }, "sha512-LsiILbtBETkDz8I9p1dQ0uyRUWuaQzd/cuEeS1hoRSyW5E5XGmTzlwY1OrNzzakGowI9Dr/I8HVaw4hTtnxy8g=="],
|
||||
@@ -2969,6 +2988,8 @@
|
||||
|
||||
"which-collection": ["which-collection@1.0.2", "", { "dependencies": { "is-map": "^2.0.3", "is-set": "^2.0.3", "is-weakmap": "^2.0.2", "is-weakset": "^2.0.3" } }, "sha512-K4jVyjnBdgvc86Y6BkaLZEN933SwYOuBFkdmBu9ZfkcAbdVbpITnDmjvZ/aQjRXQrv5EPkTnD1s39GiiqbngCw=="],
|
||||
|
||||
"which-command": ["which-command@0.1.0", "", { "bin": { "which-command": "cli.js" } }, "sha512-XZyoF5/5hZtXitIwzrU4NKK+Wtbb9aB9CezUEw2Q0wlYK8NUYQxC1rRXgNueYLtBAJwXIb+/tFVk4dozciNJMA=="],
|
||||
|
||||
"which-typed-array": ["which-typed-array@1.1.22", "", { "dependencies": { "available-typed-arrays": "^1.0.7", "call-bind": "^1.0.9", "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-fvO4ExWMFsqyhG3AiPAObMuY1lxaqgYcxbc49CNdWDDECOJNgQyvsOWVwbZc+qf3rzRtxojBK+CMEv0Ld5CYpw=="],
|
||||
|
||||
"why-is-node-running": ["why-is-node-running@2.3.0", "", { "dependencies": { "siginfo": "^2.0.0", "stackback": "0.0.2" }, "bin": { "why-is-node-running": "cli.js" } }, "sha512-hUrmaWBdVDcxvYqnyh09zunKzROWjbZTiNy8dBEjkS7ehEDQibXJ7XvlmtbwuTclUiIyN+CyXQD4Vmko8fNm8w=="],
|
||||
@@ -2983,7 +3004,7 @@
|
||||
|
||||
"wrappy": ["wrappy@1.0.2", "", {}, "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="],
|
||||
|
||||
"ws": ["ws@8.21.3", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-201TZ/kPWxoPr/OKWjquZR1SWKXcvxdH+e1xrx89b3YbmzLMFCLfnaG1HFIgWzJOEWZ7MvpK++odZufgYR50Rw=="],
|
||||
"ws": ["ws@8.22.2", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-/rEZ2ejown2N5gxVTHUu9bVVC7ttu0/Tgzf5hbTOixR3Fs7mbbXgn2DbdbcKgJOmpQMtrIUWivtYuVMnp/W/yA=="],
|
||||
|
||||
"xtend": ["xtend@4.0.2", "", {}, "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ=="],
|
||||
|
||||
@@ -2997,20 +3018,20 @@
|
||||
|
||||
"yargs-parser": ["yargs-parser@21.1.1", "", {}, "sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw=="],
|
||||
|
||||
"yauzl": ["yauzl@2.10.0", "", { "dependencies": { "buffer-crc32": "~0.2.3", "fd-slicer": "~1.1.0" } }, "sha512-p4a9I6X6nu6IhoGmBqAcbJy1mlC4j27vEPZX9F4L4/vZT3Lyq1VkFHw/V/PUcB9Buo+DG3iHkT0x3Qya58zc3g=="],
|
||||
"yauzl": ["yauzl@3.4.0", "", { "dependencies": { "pend": "~1.2.0" } }, "sha512-jIH9yLR9wqr0wOS0TpBvo/g/2UgZH5qePVbjgRliiF0BYvOZyaBknKsF+x9Iht0O6sqgnB93rCICdOZFecJuDw=="],
|
||||
|
||||
"yjs": ["yjs@13.6.32", "", { "dependencies": { "lib0": "^0.2.99" } }, "sha512-lfiJIIC4Xayt5ItynE407ehlE03pCjeOc4hkR4yxxvvNJ4kuiN25B0g+Qp8XagYz361LLL7DCzR5bvFJ81QKtQ=="],
|
||||
|
||||
"yocto-queue": ["yocto-queue@0.1.0", "", {}, "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q=="],
|
||||
|
||||
"yoctocolors": ["yoctocolors@2.2.0", "", {}, "sha512-xYqdZFUK/VYazNl/oCDYN+3WloWQwMfZxBoiNt6qNyk+xfOdi598muWE42rNZFp1kNOiqW936q5RhUdnpqElSg=="],
|
||||
|
||||
"zeptomatch": ["zeptomatch@2.1.0", "", { "dependencies": { "grammex": "^3.1.11", "graphmatch": "^1.1.0" } }, "sha512-KiGErG2J0G82LSpniV0CtIzjlJ10E04j02VOudJsPyPwNZgGnRKQy7I1R7GMyg/QswnE4l7ohSGrQbQbjXPPDA=="],
|
||||
|
||||
"zip-stream": ["zip-stream@6.0.1", "", { "dependencies": { "archiver-utils": "^5.0.0", "compress-commons": "^6.0.2", "readable-stream": "^4.0.0" } }, "sha512-zK7YHHz4ZXpW89AHXUPbQVGKI7uvkd3hzusTdotCg1UxyaVtg0zFJSTfW/Dq5f7OBBVnq6cZIaC8Ti4hb6dtCA=="],
|
||||
|
||||
"zod": ["zod@4.5.4", "", {}, "sha512-sC95tT5iHHH9gtpj6A81kh+NEaRAUFN+qlUPDUbRfOMvNf5QCBqsb3WgvnpVtK5Y+4UfA6KqufotuTvMGiTlsA=="],
|
||||
|
||||
"zod-to-json-schema": ["zod-to-json-schema@3.25.2", "", { "peerDependencies": { "zod": "^3.25.28 || ^4" } }, "sha512-O/PgfnpT1xKSDeQYSCfRI5Gy3hPf91mKVDuYLUHZJMiDFptvP41MSnWofm8dnCm0256ZNfZIM7DSzuSMAFnjHA=="],
|
||||
|
||||
"zustand": ["zustand@5.0.15", "", { "peerDependencies": { "@types/react": ">=18.0.0", "immer": ">=9.0.6", "react": ">=18.0.0", "use-sync-external-store": ">=1.2.0" }, "optionalPeers": ["@types/react", "immer", "react", "use-sync-external-store"] }, "sha512-MpSEjRiBkA9crSYeOUH32rJC7SVqAbm0Fqcqge/bUi2PPoLcBWKOsG+C8mevmpr8TwXHBVkChbbJiyvkE+i/3A=="],
|
||||
|
||||
"zwitch": ["zwitch@2.0.4", "", {}, "sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A=="],
|
||||
@@ -3021,9 +3042,7 @@
|
||||
|
||||
"@anthropic-ai/sandbox-runtime/zod": ["zod@3.25.76", "", {}, "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ=="],
|
||||
|
||||
"@aws-crypto/sha256-browser/@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/@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-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node": ["@aws-sdk/credential-provider-node@3.972.92", "", { "dependencies": { "@aws-sdk/credential-provider-env": "^3.972.80", "@aws-sdk/credential-provider-http": "^3.972.82", "@aws-sdk/credential-provider-ini": "^3.973.25", "@aws-sdk/credential-provider-process": "^3.972.80", "@aws-sdk/credential-provider-sso": "^3.973.24", "@aws-sdk/credential-provider-web-identity": "^3.972.86", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.38.0", "@smithy/credential-provider-imds": "^4.5.2", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-jC3zmOo7Qxlk+oBivvSsS1Ph7sFGm0QfuhqDGbqgRXi4cxUYMG+dWtBwS2XYYXgc8TIVXJiwgQ65sbNpbYupTA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@smithy/node-http-handler": ["@smithy/node-http-handler@4.11.3", "", { "dependencies": { "@smithy/core": "^3.33.3", "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-2jY1tSpERfPfWqyBV2pH+iGFaghVsIJszJNsT7hxtQYhVJpWDyc0LqOWI+nXOxOAHaEfZ4PXXtp1wW1TGpHhkA=="],
|
||||
|
||||
@@ -3039,16 +3058,16 @@
|
||||
|
||||
"@dbos-inc/dbos-sdk/commander": ["commander@12.1.0", "", {}, "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA=="],
|
||||
|
||||
"@earendil-works/pi-agent-core/ignore": ["ignore@7.0.5", "", {}, "sha512-Hs59xBNfUIunMFgWAbGX5cq6893IbWg4KnrjbYwX3tx0ztorVgTDA6B2sxf8ejHJ4wz8BqGUMYlnzNBer5NvGg=="],
|
||||
"@dbos-inc/dbos-sdk/ws": ["ws@8.21.3", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-201TZ/kPWxoPr/OKWjquZR1SWKXcvxdH+e1xrx89b3YbmzLMFCLfnaG1HFIgWzJOEWZ7MvpK++odZufgYR50Rw=="],
|
||||
|
||||
"@earendil-works/pi-agent-core/typebox": ["typebox@1.1.38", "", {}, "sha512-pZ0aQPmMmXoUvSbeuWf/Hzsc+avNw/Zd6VeE8CFgkVGWyuHPJvqeJJDeJqLve+K70LvjYIoleGcoJHPT17cWoA=="],
|
||||
"@earendil-works/chord/ws": ["ws@8.21.3", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-201TZ/kPWxoPr/OKWjquZR1SWKXcvxdH+e1xrx89b3YbmzLMFCLfnaG1HFIgWzJOEWZ7MvpK++odZufgYR50Rw=="],
|
||||
|
||||
"@earendil-works/pi-ai/@google/genai": ["@google/genai@1.52.0", "", { "dependencies": { "google-auth-library": "^10.3.0", "p-retry": "^4.6.2", "protobufjs": "^7.5.4", "ws": "^8.18.0" }, "peerDependencies": { "@modelcontextprotocol/sdk": "^1.25.2" }, "optionalPeers": ["@modelcontextprotocol/sdk"] }, "sha512-gwSvbpiN/17O9TbsqSsE/OzZcpv5Fo4RQjdngGgogtuB9RsyJ8ZHhX5KjHj1bp5N9snN2eK8LDGXSaWW2hof8Q=="],
|
||||
|
||||
"@earendil-works/pi-ai/typebox": ["typebox@1.1.38", "", {}, "sha512-pZ0aQPmMmXoUvSbeuWf/Hzsc+avNw/Zd6VeE8CFgkVGWyuHPJvqeJJDeJqLve+K70LvjYIoleGcoJHPT17cWoA=="],
|
||||
"@earendil-works/pi-ai/@google/genai": ["@google/genai@2.21.0", "", { "dependencies": { "google-auth-library": "^10.3.0", "p-retry": "^4.6.2", "protobufjs": "^7.5.4", "ws": "^8.18.0" }, "peerDependencies": { "@modelcontextprotocol/sdk": "^1.25.2" }, "optionalPeers": ["@modelcontextprotocol/sdk"], "hasInstallScript": true }, "sha512-+PDtco2/Z0ONdzCGekCoCT+O1VJS9xJQNN4XzQpXG/t3El/SWWMkCWlFRO1KmivOHPa4Q0VjUYu1HBKCZ/v33Q=="],
|
||||
|
||||
"@eslint-community/eslint-utils/eslint-visitor-keys": ["eslint-visitor-keys@3.4.3", "", {}, "sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag=="],
|
||||
|
||||
"@google/genai/ws": ["ws@8.21.3", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-201TZ/kPWxoPr/OKWjquZR1SWKXcvxdH+e1xrx89b3YbmzLMFCLfnaG1HFIgWzJOEWZ7MvpK++odZufgYR50Rw=="],
|
||||
|
||||
"@grpc/grpc-js/@grpc/proto-loader": ["@grpc/proto-loader@0.8.1", "", { "dependencies": { "lodash.camelcase": "^4.3.0", "long": "^5.0.0", "protobufjs": "^7.5.5", "yargs": "^17.7.2" }, "bin": { "proto-loader-gen-types": "build/bin/proto-loader-gen-types.js" } }, "sha512-wtF6h+DY6M3YaDBPAmvuuA6jV8Sif9MjtOI5euKFWRgCDl5PeDpPsHR9u2l6St5ceY8AZgoNDww5+HvEsXFsGg=="],
|
||||
|
||||
"@inlang/paraglide-js/commander": ["commander@11.1.0", "", {}, "sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ=="],
|
||||
@@ -3075,8 +3094,12 @@
|
||||
|
||||
"@reduxjs/toolkit/reselect": ["reselect@5.3.0", "", {}, "sha512-XGoLeRAVzUTcJ1qkxPQhDJyIZ5d6zzZD9nT7AEZOaaU9UbWclhycElmhO+VD5bFeLuzhPBaOV2oXC8uG35ZSpg=="],
|
||||
|
||||
"@shumai/cli/adm-zip": ["adm-zip@0.6.1", "", {}, "sha512-Xwrja8nx9e5o2N1my4DsKCeKpdrnACyr1wtbPxBDgGzKzKyE9kRtBFA8mWldI+RVlD7CBZNWY/wQ2+ydwOR6kQ=="],
|
||||
|
||||
"@shumai/webui/@types/react-dom": ["@types/react-dom@19.2.5", "", { "peerDependencies": { "@types/react": "^19.2.0" } }, "sha512-fMPwH9v7r/pp43yUd2/Mbiex5KouJwwR3dzHkhLREUC6764VyDsqxhAxv6OFEYR1RhjOyD1naqba8ECDBe7ZQg=="],
|
||||
|
||||
"@smithy/node-http-handler/@smithy/types": ["@smithy/types@4.19.0", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-r7jh49VJxGerfAcTQA6gXcKc+98zOp/tqRwzYjgOE+iSQsP6cEU1hq2QzbuipmP68QtYdY9wKEhiCQZIzHgZ4Q=="],
|
||||
|
||||
"@spectrum-web-components/action-button/@spectrum-web-components/icon": ["@spectrum-web-components/icon@0.37.0", "", { "dependencies": { "@spectrum-web-components/base": "^0.37.0", "@spectrum-web-components/iconset": "^0.37.0" } }, "sha512-DZ1yOKHxNQygzIJ7u9akYComDnMmxuTzfmYxxt9sHdUXmc1NbMkCJqEaAG/LYsdb+hc4xWaDTT9SzArLKZ/btA=="],
|
||||
|
||||
"@spectrum-web-components/action-group/@spectrum-web-components/icons-workflow": ["@spectrum-web-components/icons-workflow@0.37.0", "", { "dependencies": { "@spectrum-web-components/base": "^0.37.0", "@spectrum-web-components/icon": "^0.37.0" } }, "sha512-D1Dg+ZjlqbaU/2uKYhmMtZXQMVUw9J9LevHSKSzMFL+Y++JMqRROQuzwAOFW7tttCw1w0+3kIuv2ohkeE+8fNQ=="],
|
||||
@@ -3219,6 +3242,8 @@
|
||||
|
||||
"@visx/vendor/d3-format": ["d3-format@3.1.0", "", {}, "sha512-YyUI6AEuY/Wpt8KWLgZHsIU86atmikuoOmCfommt0LYHiQSPjvX2AcFc38PX0CBpr2RCyZhjex+NS/LPOv6YqA=="],
|
||||
|
||||
"archiver-utils/is-stream": ["is-stream@2.0.1", "", {}, "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg=="],
|
||||
|
||||
"bcrypt-pbkdf/tweetnacl": ["tweetnacl@0.14.5", "", {}, "sha512-KXXFFdAbFXY4geFIwoyNK+f5Z1b7swfXABfL7HXCmoIWMKU3dmS26672A4EeQtDzLKy7SXmfBu51JolvEKwtGA=="],
|
||||
|
||||
"better-call/@better-fetch/fetch": ["@better-fetch/fetch@1.3.1", "", {}, "sha512-ABkD1WhyfPZprKRQI3bhATjeiFuNWC9PXhfGWqL+sg/gKrM977oFrYkdb4msM3hgUGonr7KlOsOFT5TU2rht9g=="],
|
||||
@@ -3227,18 +3252,30 @@
|
||||
|
||||
"bl/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=="],
|
||||
|
||||
"compress-commons/is-stream": ["is-stream@2.0.1", "", {}, "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg=="],
|
||||
|
||||
"docker-modem/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=="],
|
||||
|
||||
"dockerode/tar-fs": ["tar-fs@2.1.5", "", { "dependencies": { "chownr": "^1.1.1", "mkdirp-classic": "^0.5.2", "pump": "^3.0.0", "tar-stream": "^2.1.4" } }, "sha512-OboTd8mmMhZDNPV+UjQcK9yKAatXu2aJ+r1w4im1Otd4M4fl2hwvdoXUxIYHFTHWK/3y3FarBP70v3vwmGlOxw=="],
|
||||
|
||||
"eslint-plugin-react/minimatch": ["minimatch@3.1.5", "", { "dependencies": { "brace-expansion": "^1.1.7" } }, "sha512-VgjWUsnnT6n+NUk6eZq77zeFdpW2LWDzP6zFGrCbHXiYNul5Dzqk2HHQ5uFH2DNW5Xbp8+jVzaeNt94ssEEl4w=="],
|
||||
|
||||
"foreground-child/signal-exit": ["signal-exit@4.1.0", "", {}, "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw=="],
|
||||
"extract-zip/@types/yauzl": ["@types/yauzl@2.10.3", "", { "dependencies": { "@types/node": "*" } }, "sha512-oJoftv0LSuaDZE3Le4DbKX+KS9G36NzOeSap90UIK0yMA/NhKJhqlSGtNDORNRaIbQfzjXDrQa0ytJ6mNRGz/Q=="],
|
||||
|
||||
"extract-zip/get-stream": ["get-stream@5.2.0", "", { "dependencies": { "pump": "^3.0.0" } }, "sha512-nBF+F1rAZVCu/p7rjzgA+Yb4lfYXrpl7a6VmJrU8wF9I1CKvP/QwPNZHnOlwbTkY6dvtFIzFMSyQXbLoTQPRpA=="],
|
||||
|
||||
"extract-zip/yauzl": ["yauzl@2.10.0", "", { "dependencies": { "buffer-crc32": "~0.2.3", "fd-slicer": "~1.1.0" } }, "sha512-p4a9I6X6nu6IhoGmBqAcbJy1mlC4j27vEPZX9F4L4/vZT3Lyq1VkFHw/V/PUcB9Buo+DG3iHkT0x3Qya58zc3g=="],
|
||||
|
||||
"gaxios/https-proxy-agent": ["https-proxy-agent@7.0.6", "", { "dependencies": { "agent-base": "^7.1.2", "debug": "4" } }, "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw=="],
|
||||
|
||||
"gcp-metadata/google-logging-utils": ["google-logging-utils@1.2.0", "", {}, "sha512-WE9av4wKDZgRjBwgVUabocx8T6/7o3Ca1Fat46FXDhXVAFibzNadedcOXrdgd1Kzmk8tsk/9ZH89Wyf/SqeZ3A=="],
|
||||
|
||||
"glob/minimatch": ["minimatch@9.0.9", "", { "dependencies": { "brace-expansion": "^2.0.2" } }, "sha512-OBwBN9AL4dqmETlpS2zasx+vTeWclWzkblfZk7KTA5j3jeOONz/tRCnZomUyvNg83wL5Zv9Ss6HMJXAgL8R2Yg=="],
|
||||
|
||||
"happy-dom/@types/ws": ["@types/ws@8.18.1", "", { "dependencies": { "@types/node": "*" } }, "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg=="],
|
||||
|
||||
"happy-dom/ws": ["ws@8.21.3", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-201TZ/kPWxoPr/OKWjquZR1SWKXcvxdH+e1xrx89b3YbmzLMFCLfnaG1HFIgWzJOEWZ7MvpK++odZufgYR50Rw=="],
|
||||
|
||||
"its-fine/@types/react-reconciler": ["@types/react-reconciler@0.28.9", "", { "peerDependencies": { "@types/react": "*" } }, "sha512-HHM3nxyUZ3zAylX8ZEyrDNd2XZOnQ0D5XfunJF5FLQnZbHHYq4UWvW1QfelQNXv1ICNkwYhfxjwfnqivYB6bFg=="],
|
||||
|
||||
"lazystream/readable-stream": ["readable-stream@2.3.8", "", { "dependencies": { "core-util-is": "~1.0.0", "inherits": "~2.0.3", "isarray": "~1.0.0", "process-nextick-args": "~2.0.0", "safe-buffer": "~5.1.1", "string_decoder": "~1.1.1", "util-deprecate": "~1.0.1" } }, "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA=="],
|
||||
@@ -3249,6 +3286,8 @@
|
||||
|
||||
"lit-element/@lit/reactive-element": ["@lit/reactive-element@1.6.3", "", { "dependencies": { "@lit-labs/ssr-dom-shim": "^1.0.0" } }, "sha512-QuTgnG52Poic7uM1AN5yJ09QMe0O28e10XzSvWDz02TJiiKee4stsiownEIadWm8nYzyDAyT+gKzUoZmiWQtsQ=="],
|
||||
|
||||
"npm-run-path/path-key": ["path-key@4.0.0", "", {}, "sha512-haREypq7xkM7ErfgIyA0z+Bj4AGKlMSdlQE2jvJo6huWD1EdkKYV+G/T4nq0YEF2vgTT8kqMFKo1uHn950r4SQ=="],
|
||||
|
||||
"p-retry/retry": ["retry@0.13.1", "", {}, "sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg=="],
|
||||
|
||||
"parse-entities/@types/unist": ["@types/unist@2.0.11", "", {}, "sha512-CmBKiL6NNo/OqgmMn95Fk9Whlp2mtvIv+KNpQKN2F4SjvrEesubTRWGYSg+BnWZOnlCaSTU1sMpsBOzgbYhnsA=="],
|
||||
@@ -3267,6 +3306,8 @@
|
||||
|
||||
"prop-types/react-is": ["react-is@16.13.1", "", {}, "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ=="],
|
||||
|
||||
"proper-lockfile/signal-exit": ["signal-exit@3.0.7", "", {}, "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ=="],
|
||||
|
||||
"readdir-glob/minimatch": ["minimatch@5.1.9", "", { "dependencies": { "brace-expansion": "^2.0.1" } }, "sha512-7o1wEA2RyMP7Iu7GNba9vc0RWWGACJOCZBJX2GJWip0ikV+wcOsgVuY9uE8CPiyQhkGFSlhuSkZPavN7u1c2Fw=="],
|
||||
|
||||
"sharp/semver": ["semver@7.8.5", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA=="],
|
||||
@@ -3279,7 +3320,25 @@
|
||||
|
||||
"wrap-ansi-cjs/ansi-styles": ["ansi-styles@4.3.0", "", { "dependencies": { "color-convert": "^2.0.1" } }, "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg=="],
|
||||
|
||||
"yauzl/buffer-crc32": ["buffer-crc32@0.2.13", "", {}, "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ=="],
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-env": ["@aws-sdk/credential-provider-env@3.972.80", "", { "dependencies": { "@aws-sdk/core": "^3.979.1", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-uO5TiSvb6j1w9RTe5z28OaOmsrOxVQUPaC5+wZl50RfOXcrYD/KgnS8I9lfyGXc7sGbkCopK6VnhNIAoLqfewQ=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-http": ["@aws-sdk/credential-provider-http@3.972.82", "", { "dependencies": { "@aws-sdk/core": "^3.979.1", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.38.0", "@smithy/fetch-http-handler": "^5.8.0", "@smithy/node-http-handler": "^4.14.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-vQzlDHlC24Yz42p+rT4NBoP/5VdetkBoujbOLq9h6QYWC8THuZSPYnXIR9OP3XcOrdMgPBBipOrdssfxnIiWUg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-ini": ["@aws-sdk/credential-provider-ini@3.973.25", "", { "dependencies": { "@aws-sdk/core": "^3.979.1", "@aws-sdk/credential-provider-env": "^3.972.80", "@aws-sdk/credential-provider-http": "^3.972.82", "@aws-sdk/credential-provider-login": "^3.972.87", "@aws-sdk/credential-provider-process": "^3.972.80", "@aws-sdk/credential-provider-sso": "^3.973.24", "@aws-sdk/credential-provider-web-identity": "^3.972.86", "@aws-sdk/nested-clients": "^3.997.54", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.38.0", "@smithy/credential-provider-imds": "^4.5.2", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-b5xl71uCRdWsu7P1VeGpiGuCf6zfYETsSNOv8AmYVTrsgbvTJRXdmmOPVfxkvGGVNsiKkGctClQW3jAn0SZLug=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-process": ["@aws-sdk/credential-provider-process@3.972.80", "", { "dependencies": { "@aws-sdk/core": "^3.979.1", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-xqfixO5aclje7tbQJDias+iNND5wp/3QZixCdZcVQpgWPTZ0V5DfTW9A804A5Z7ezu7sH3WRO+vzUjtiJBA8yg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-sso": ["@aws-sdk/credential-provider-sso@3.973.24", "", { "dependencies": { "@aws-sdk/core": "^3.979.1", "@aws-sdk/nested-clients": "^3.997.54", "@aws-sdk/token-providers": "3.1161.0", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-nlg3BKG03Cgzvv/dMnhxvDJPhAhQSpaN0RUdwgU7ziwkasoev7iI8el8Xy4Rd5sqzgaDUbV269fOXck5SargqA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-web-identity": ["@aws-sdk/credential-provider-web-identity@3.972.86", "", { "dependencies": { "@aws-sdk/core": "^3.979.1", "@aws-sdk/nested-clients": "^3.997.54", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-zKTyLJBEyfQBVlYqT6Jt+mYV4r8kGbWM7dn6v4Y4MRARizhYafjDXP/iMu76UMRhToSPNmIKX1XAb24gtpBFIA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/types": ["@aws-sdk/types@3.975.0", "", { "dependencies": { "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-WQlhs6juByzWOzj5fwxOx0mPJaEqMcG/sDpl3Id61B3XhKE63xY+0QWcnG+sWR/sCfprmreyUo1lM7IKZmHMzw=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@smithy/core": ["@smithy/core@3.38.1", "", { "dependencies": { "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-U/mB2XiwTLaf+NSgLSWwJfptsMT/H8aBi0aTAA/E5e2/TagFeGCqAyhKGG0w0A9IWTkm4qYEmLoD/HB6aYfnRA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@smithy/types": ["@smithy/types@4.19.0", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-r7jh49VJxGerfAcTQA6gXcKc+98zOp/tqRwzYjgOE+iSQsP6cEU1hq2QzbuipmP68QtYdY9wKEhiCQZIzHgZ4Q=="],
|
||||
|
||||
"@earendil-works/pi-ai/@google/genai/ws": ["ws@8.21.3", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-201TZ/kPWxoPr/OKWjquZR1SWKXcvxdH+e1xrx89b3YbmzLMFCLfnaG1HFIgWzJOEWZ7MvpK++odZufgYR50Rw=="],
|
||||
|
||||
"@isaacs/cliui/string-width/emoji-regex": ["emoji-regex@9.2.2", "", {}, "sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg=="],
|
||||
|
||||
@@ -3383,6 +3442,10 @@
|
||||
|
||||
"eslint-plugin-react/minimatch/brace-expansion": ["brace-expansion@1.1.18", "", { "dependencies": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" } }, "sha512-Edep/X9fGqVNmzKBVsDYIOtD+z1tuezV70LBjdCst9Tqu76lsnvRiZ6oTic1n+/BIwX6QDGAO94PN4N2SADvtw=="],
|
||||
|
||||
"extract-zip/yauzl/buffer-crc32": ["buffer-crc32@0.2.13", "", {}, "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ=="],
|
||||
|
||||
"gaxios/https-proxy-agent/agent-base": ["agent-base@7.1.4", "", {}, "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ=="],
|
||||
|
||||
"glob/minimatch/brace-expansion": ["brace-expansion@2.1.4", "", { "dependencies": { "balanced-match": "^1.0.0" } }, "sha512-hGfVzPxthbf3+2yjg/RBs60cB0FhqBS/zvdV/4wn4/BmN0bNMMHPc4V/BbFieqf1TKAGGAHnY4eSjajCl0f2Xg=="],
|
||||
|
||||
"lazystream/readable-stream/isarray": ["isarray@1.0.0", "", {}, "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ=="],
|
||||
@@ -3393,6 +3456,32 @@
|
||||
|
||||
"readdir-glob/minimatch/brace-expansion": ["brace-expansion@2.1.4", "", { "dependencies": { "balanced-match": "^1.0.0" } }, "sha512-hGfVzPxthbf3+2yjg/RBs60cB0FhqBS/zvdV/4wn4/BmN0bNMMHPc4V/BbFieqf1TKAGGAHnY4eSjajCl0f2Xg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-env/@aws-sdk/core": ["@aws-sdk/core@3.979.1", "", { "dependencies": { "@aws-sdk/types": "^3.975.0", "@aws-sdk/xml-builder": "^3.972.42", "@aws/lambda-invoke-store": "^0.3.0", "@smithy/core": "^3.38.0", "@smithy/signature-v4": "^5.7.5", "@smithy/types": "^4.19.0", "bowser": "^2.11.0", "tslib": "^2.6.2" } }, "sha512-ayOlV1VhLNMiq8PW9ZkVobG4gkldnUoHjG69yy2b8j+VYFlhz/uAL7ZdYQAoQ+eShtGUdNDnnG5rxDg6yUJrOg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-http/@aws-sdk/core": ["@aws-sdk/core@3.979.1", "", { "dependencies": { "@aws-sdk/types": "^3.975.0", "@aws-sdk/xml-builder": "^3.972.42", "@aws/lambda-invoke-store": "^0.3.0", "@smithy/core": "^3.38.0", "@smithy/signature-v4": "^5.7.5", "@smithy/types": "^4.19.0", "bowser": "^2.11.0", "tslib": "^2.6.2" } }, "sha512-ayOlV1VhLNMiq8PW9ZkVobG4gkldnUoHjG69yy2b8j+VYFlhz/uAL7ZdYQAoQ+eShtGUdNDnnG5rxDg6yUJrOg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-http/@smithy/fetch-http-handler": ["@smithy/fetch-http-handler@5.8.0", "", { "dependencies": { "@smithy/core": "^3.33.3", "@smithy/types": "^4.18.0", "tslib": "^2.6.2" } }, "sha512-ycSJu3tFAQ4v04CBB0agqFMVsSQ1iG3yw+SpgxRqKfaURpQD4CZ8Wn0zPMmSnOuTpTh65Vz+EA0rMrw089wvkA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-http/@smithy/node-http-handler": ["@smithy/node-http-handler@4.14.1", "", { "dependencies": { "@smithy/core": "^3.38.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-/T6E1jzCL0fErS9uq8JqES/5yYQQ4ppVmmYHwXw2nlov8KFzLb+3QqHR5iAun1BsuAB50IPxZliV8YMgghLwhA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-ini/@aws-sdk/core": ["@aws-sdk/core@3.979.1", "", { "dependencies": { "@aws-sdk/types": "^3.975.0", "@aws-sdk/xml-builder": "^3.972.42", "@aws/lambda-invoke-store": "^0.3.0", "@smithy/core": "^3.38.0", "@smithy/signature-v4": "^5.7.5", "@smithy/types": "^4.19.0", "bowser": "^2.11.0", "tslib": "^2.6.2" } }, "sha512-ayOlV1VhLNMiq8PW9ZkVobG4gkldnUoHjG69yy2b8j+VYFlhz/uAL7ZdYQAoQ+eShtGUdNDnnG5rxDg6yUJrOg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-ini/@aws-sdk/credential-provider-login": ["@aws-sdk/credential-provider-login@3.972.87", "", { "dependencies": { "@aws-sdk/core": "^3.979.1", "@aws-sdk/nested-clients": "^3.997.54", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-C4ZFmcwM+Zj14Z8huVUk54Gv4uzT1ZdzG9cY3Gdh8FePakq7Bss1OFsgZjAhWFOkFOGpBvFBeu0OWjWf8HNKRQ=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-ini/@aws-sdk/nested-clients": ["@aws-sdk/nested-clients@3.997.54", "", { "dependencies": { "@aws-sdk/core": "^3.979.1", "@aws-sdk/signature-v4-multi-region": "^3.996.51", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.38.0", "@smithy/fetch-http-handler": "^5.8.0", "@smithy/node-http-handler": "^4.14.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-/OczlavsS7U5sOodRO10Ug4R/AKFVZiXTstKipWe68EfjPkmdDFNXNl9FCJyXWkg1DG6RBm3qAuVB8FNW9O8KQ=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-process/@aws-sdk/core": ["@aws-sdk/core@3.979.1", "", { "dependencies": { "@aws-sdk/types": "^3.975.0", "@aws-sdk/xml-builder": "^3.972.42", "@aws/lambda-invoke-store": "^0.3.0", "@smithy/core": "^3.38.0", "@smithy/signature-v4": "^5.7.5", "@smithy/types": "^4.19.0", "bowser": "^2.11.0", "tslib": "^2.6.2" } }, "sha512-ayOlV1VhLNMiq8PW9ZkVobG4gkldnUoHjG69yy2b8j+VYFlhz/uAL7ZdYQAoQ+eShtGUdNDnnG5rxDg6yUJrOg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-sso/@aws-sdk/core": ["@aws-sdk/core@3.979.1", "", { "dependencies": { "@aws-sdk/types": "^3.975.0", "@aws-sdk/xml-builder": "^3.972.42", "@aws/lambda-invoke-store": "^0.3.0", "@smithy/core": "^3.38.0", "@smithy/signature-v4": "^5.7.5", "@smithy/types": "^4.19.0", "bowser": "^2.11.0", "tslib": "^2.6.2" } }, "sha512-ayOlV1VhLNMiq8PW9ZkVobG4gkldnUoHjG69yy2b8j+VYFlhz/uAL7ZdYQAoQ+eShtGUdNDnnG5rxDg6yUJrOg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-sso/@aws-sdk/nested-clients": ["@aws-sdk/nested-clients@3.997.54", "", { "dependencies": { "@aws-sdk/core": "^3.979.1", "@aws-sdk/signature-v4-multi-region": "^3.996.51", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.38.0", "@smithy/fetch-http-handler": "^5.8.0", "@smithy/node-http-handler": "^4.14.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-/OczlavsS7U5sOodRO10Ug4R/AKFVZiXTstKipWe68EfjPkmdDFNXNl9FCJyXWkg1DG6RBm3qAuVB8FNW9O8KQ=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-sso/@aws-sdk/token-providers": ["@aws-sdk/token-providers@3.1161.0", "", { "dependencies": { "@aws-sdk/core": "^3.979.1", "@aws-sdk/nested-clients": "^3.997.54", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-ypbXSofNcSHwf/KA40bFCXJb0MLaAKrfT7sot4dFaA+2lsuXNah4zLRJIAU7Qmvs9PFJ6rnY0bZFEC+mE7attA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-web-identity/@aws-sdk/core": ["@aws-sdk/core@3.979.1", "", { "dependencies": { "@aws-sdk/types": "^3.975.0", "@aws-sdk/xml-builder": "^3.972.42", "@aws/lambda-invoke-store": "^0.3.0", "@smithy/core": "^3.38.0", "@smithy/signature-v4": "^5.7.5", "@smithy/types": "^4.19.0", "bowser": "^2.11.0", "tslib": "^2.6.2" } }, "sha512-ayOlV1VhLNMiq8PW9ZkVobG4gkldnUoHjG69yy2b8j+VYFlhz/uAL7ZdYQAoQ+eShtGUdNDnnG5rxDg6yUJrOg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-web-identity/@aws-sdk/nested-clients": ["@aws-sdk/nested-clients@3.997.54", "", { "dependencies": { "@aws-sdk/core": "^3.979.1", "@aws-sdk/signature-v4-multi-region": "^3.996.51", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.38.0", "@smithy/fetch-http-handler": "^5.8.0", "@smithy/node-http-handler": "^4.14.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-/OczlavsS7U5sOodRO10Ug4R/AKFVZiXTstKipWe68EfjPkmdDFNXNl9FCJyXWkg1DG6RBm3qAuVB8FNW9O8KQ=="],
|
||||
|
||||
"@prisma/studio-core/@radix-ui/react-toggle/@radix-ui/react-primitive/@radix-ui/react-slot": ["@radix-ui/react-slot@1.2.3", "", { "dependencies": { "@radix-ui/react-compose-refs": "1.1.2" }, "peerDependencies": { "@types/react": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react"] }, "sha512-aeNmHnBxbi2St0au6VBVC7JXFlhLlOnvIIlePNniyUNAClzmtAUEY8/pBiK3iHjufOlwA+c20/8jngo7xcrg8A=="],
|
||||
|
||||
"@prisma/studio-core/@radix-ui/react-toggle/@radix-ui/react-use-controllable-state/@radix-ui/react-use-effect-event": ["@radix-ui/react-use-effect-event@0.0.2", "", { "dependencies": { "@radix-ui/react-use-layout-effect": "1.1.1" }, "peerDependencies": { "@types/react": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react"] }, "sha512-Qp8WbZOBe+blgpuUT+lw2xheLP8q0oatc9UpmiemEICxGvFLYmHm9QowVZGHtJlGbS6A6yJ3iViad/2cVjnOiA=="],
|
||||
@@ -3419,6 +3508,70 @@
|
||||
|
||||
"readdir-glob/minimatch/brace-expansion/balanced-match": ["balanced-match@1.0.2", "", {}, "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-env/@aws-sdk/core/@aws-sdk/xml-builder": ["@aws-sdk/xml-builder@3.972.42", "", { "dependencies": { "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-smX8/gNP8Kjq+CWTBMZQLQ912vOQjiWExzwzvMO+eujPB5RY8IbtF0AGdUUXHKN7KoDWK9dIcuW0WG269kiP6w=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-env/@aws-sdk/core/@smithy/signature-v4": ["@smithy/signature-v4@5.7.5", "", { "dependencies": { "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-WPjdr5oWOk1LArpI4pavu+fSo+/evi0Re1olxGvqwuZopd4gYR1UGCi+Soyn/IjmafqbWs+ZC+3AKxJS3ZpwUg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-http/@aws-sdk/core/@aws-sdk/xml-builder": ["@aws-sdk/xml-builder@3.972.42", "", { "dependencies": { "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-smX8/gNP8Kjq+CWTBMZQLQ912vOQjiWExzwzvMO+eujPB5RY8IbtF0AGdUUXHKN7KoDWK9dIcuW0WG269kiP6w=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-http/@aws-sdk/core/@smithy/signature-v4": ["@smithy/signature-v4@5.7.5", "", { "dependencies": { "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-WPjdr5oWOk1LArpI4pavu+fSo+/evi0Re1olxGvqwuZopd4gYR1UGCi+Soyn/IjmafqbWs+ZC+3AKxJS3ZpwUg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-http/@smithy/fetch-http-handler/@smithy/core": ["@smithy/core@3.33.3", "", { "dependencies": { "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-CsOeKq/9kA3y6VJHt+/+VTCtBaxJ4OTFpgrjIUhPpDIKxBci1k2bJaQASF2h/ELWrulGp+t97DZ0mevfAD8idg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-ini/@aws-sdk/core/@aws-sdk/xml-builder": ["@aws-sdk/xml-builder@3.972.42", "", { "dependencies": { "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-smX8/gNP8Kjq+CWTBMZQLQ912vOQjiWExzwzvMO+eujPB5RY8IbtF0AGdUUXHKN7KoDWK9dIcuW0WG269kiP6w=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-ini/@aws-sdk/core/@smithy/signature-v4": ["@smithy/signature-v4@5.7.5", "", { "dependencies": { "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-WPjdr5oWOk1LArpI4pavu+fSo+/evi0Re1olxGvqwuZopd4gYR1UGCi+Soyn/IjmafqbWs+ZC+3AKxJS3ZpwUg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-ini/@aws-sdk/nested-clients/@aws-sdk/signature-v4-multi-region": ["@aws-sdk/signature-v4-multi-region@3.996.51", "", { "dependencies": { "@aws-sdk/types": "^3.975.0", "@smithy/signature-v4": "^5.7.5", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-+aHvUugVBuMPXxgkPfWnTMyGdE/yWS8GHi5+qQ37HowS0YqzrbuaDLznj+Dtmtu5jgCdqz36gXmujOToUhNNWw=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-ini/@aws-sdk/nested-clients/@smithy/fetch-http-handler": ["@smithy/fetch-http-handler@5.8.0", "", { "dependencies": { "@smithy/core": "^3.33.3", "@smithy/types": "^4.18.0", "tslib": "^2.6.2" } }, "sha512-ycSJu3tFAQ4v04CBB0agqFMVsSQ1iG3yw+SpgxRqKfaURpQD4CZ8Wn0zPMmSnOuTpTh65Vz+EA0rMrw089wvkA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-ini/@aws-sdk/nested-clients/@smithy/node-http-handler": ["@smithy/node-http-handler@4.14.1", "", { "dependencies": { "@smithy/core": "^3.38.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-/T6E1jzCL0fErS9uq8JqES/5yYQQ4ppVmmYHwXw2nlov8KFzLb+3QqHR5iAun1BsuAB50IPxZliV8YMgghLwhA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-process/@aws-sdk/core/@aws-sdk/xml-builder": ["@aws-sdk/xml-builder@3.972.42", "", { "dependencies": { "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-smX8/gNP8Kjq+CWTBMZQLQ912vOQjiWExzwzvMO+eujPB5RY8IbtF0AGdUUXHKN7KoDWK9dIcuW0WG269kiP6w=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-process/@aws-sdk/core/@smithy/signature-v4": ["@smithy/signature-v4@5.7.5", "", { "dependencies": { "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-WPjdr5oWOk1LArpI4pavu+fSo+/evi0Re1olxGvqwuZopd4gYR1UGCi+Soyn/IjmafqbWs+ZC+3AKxJS3ZpwUg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-sso/@aws-sdk/core/@aws-sdk/xml-builder": ["@aws-sdk/xml-builder@3.972.42", "", { "dependencies": { "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-smX8/gNP8Kjq+CWTBMZQLQ912vOQjiWExzwzvMO+eujPB5RY8IbtF0AGdUUXHKN7KoDWK9dIcuW0WG269kiP6w=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-sso/@aws-sdk/core/@smithy/signature-v4": ["@smithy/signature-v4@5.7.5", "", { "dependencies": { "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-WPjdr5oWOk1LArpI4pavu+fSo+/evi0Re1olxGvqwuZopd4gYR1UGCi+Soyn/IjmafqbWs+ZC+3AKxJS3ZpwUg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-sso/@aws-sdk/nested-clients/@aws-sdk/signature-v4-multi-region": ["@aws-sdk/signature-v4-multi-region@3.996.51", "", { "dependencies": { "@aws-sdk/types": "^3.975.0", "@smithy/signature-v4": "^5.7.5", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-+aHvUugVBuMPXxgkPfWnTMyGdE/yWS8GHi5+qQ37HowS0YqzrbuaDLznj+Dtmtu5jgCdqz36gXmujOToUhNNWw=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-sso/@aws-sdk/nested-clients/@smithy/fetch-http-handler": ["@smithy/fetch-http-handler@5.8.0", "", { "dependencies": { "@smithy/core": "^3.33.3", "@smithy/types": "^4.18.0", "tslib": "^2.6.2" } }, "sha512-ycSJu3tFAQ4v04CBB0agqFMVsSQ1iG3yw+SpgxRqKfaURpQD4CZ8Wn0zPMmSnOuTpTh65Vz+EA0rMrw089wvkA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-sso/@aws-sdk/nested-clients/@smithy/node-http-handler": ["@smithy/node-http-handler@4.14.1", "", { "dependencies": { "@smithy/core": "^3.38.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-/T6E1jzCL0fErS9uq8JqES/5yYQQ4ppVmmYHwXw2nlov8KFzLb+3QqHR5iAun1BsuAB50IPxZliV8YMgghLwhA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-web-identity/@aws-sdk/core/@aws-sdk/xml-builder": ["@aws-sdk/xml-builder@3.972.42", "", { "dependencies": { "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-smX8/gNP8Kjq+CWTBMZQLQ912vOQjiWExzwzvMO+eujPB5RY8IbtF0AGdUUXHKN7KoDWK9dIcuW0WG269kiP6w=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-web-identity/@aws-sdk/core/@smithy/signature-v4": ["@smithy/signature-v4@5.7.5", "", { "dependencies": { "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-WPjdr5oWOk1LArpI4pavu+fSo+/evi0Re1olxGvqwuZopd4gYR1UGCi+Soyn/IjmafqbWs+ZC+3AKxJS3ZpwUg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-web-identity/@aws-sdk/nested-clients/@aws-sdk/signature-v4-multi-region": ["@aws-sdk/signature-v4-multi-region@3.996.51", "", { "dependencies": { "@aws-sdk/types": "^3.975.0", "@smithy/signature-v4": "^5.7.5", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-+aHvUugVBuMPXxgkPfWnTMyGdE/yWS8GHi5+qQ37HowS0YqzrbuaDLznj+Dtmtu5jgCdqz36gXmujOToUhNNWw=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-web-identity/@aws-sdk/nested-clients/@smithy/fetch-http-handler": ["@smithy/fetch-http-handler@5.8.0", "", { "dependencies": { "@smithy/core": "^3.33.3", "@smithy/types": "^4.18.0", "tslib": "^2.6.2" } }, "sha512-ycSJu3tFAQ4v04CBB0agqFMVsSQ1iG3yw+SpgxRqKfaURpQD4CZ8Wn0zPMmSnOuTpTh65Vz+EA0rMrw089wvkA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-web-identity/@aws-sdk/nested-clients/@smithy/node-http-handler": ["@smithy/node-http-handler@4.14.1", "", { "dependencies": { "@smithy/core": "^3.38.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-/T6E1jzCL0fErS9uq8JqES/5yYQQ4ppVmmYHwXw2nlov8KFzLb+3QqHR5iAun1BsuAB50IPxZliV8YMgghLwhA=="],
|
||||
|
||||
"@prisma/studio-core/@radix-ui/react-toggle/@radix-ui/react-primitive/@radix-ui/react-slot/@radix-ui/react-compose-refs": ["@radix-ui/react-compose-refs@1.1.2", "", { "peerDependencies": { "@types/react": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react"] }, "sha512-z4eqJvfiNnFMHIIvXP3CY57y2WJs5g2v3X0zm9mEJkrkNv4rDxu+sg9Jh8EkXyeqBkB7SOcboo9dMVqhyrACIg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-http/@smithy/fetch-http-handler/@smithy/core/@smithy/types": ["@smithy/types@4.17.2", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-FOKpVZob9MPTn2znRzGrnsMHv7BOsKVw3XiP/cOyYLDVZ9qKp4nifIiSCuUU/fIj5Vu0UOAxCFr+qRAtG0NUkA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-ini/@aws-sdk/nested-clients/@aws-sdk/signature-v4-multi-region/@smithy/signature-v4": ["@smithy/signature-v4@5.7.5", "", { "dependencies": { "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-WPjdr5oWOk1LArpI4pavu+fSo+/evi0Re1olxGvqwuZopd4gYR1UGCi+Soyn/IjmafqbWs+ZC+3AKxJS3ZpwUg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-ini/@aws-sdk/nested-clients/@smithy/fetch-http-handler/@smithy/core": ["@smithy/core@3.33.3", "", { "dependencies": { "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-CsOeKq/9kA3y6VJHt+/+VTCtBaxJ4OTFpgrjIUhPpDIKxBci1k2bJaQASF2h/ELWrulGp+t97DZ0mevfAD8idg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-sso/@aws-sdk/nested-clients/@aws-sdk/signature-v4-multi-region/@smithy/signature-v4": ["@smithy/signature-v4@5.7.5", "", { "dependencies": { "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-WPjdr5oWOk1LArpI4pavu+fSo+/evi0Re1olxGvqwuZopd4gYR1UGCi+Soyn/IjmafqbWs+ZC+3AKxJS3ZpwUg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-sso/@aws-sdk/nested-clients/@smithy/fetch-http-handler/@smithy/core": ["@smithy/core@3.33.3", "", { "dependencies": { "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-CsOeKq/9kA3y6VJHt+/+VTCtBaxJ4OTFpgrjIUhPpDIKxBci1k2bJaQASF2h/ELWrulGp+t97DZ0mevfAD8idg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-web-identity/@aws-sdk/nested-clients/@aws-sdk/signature-v4-multi-region/@smithy/signature-v4": ["@smithy/signature-v4@5.7.5", "", { "dependencies": { "@smithy/core": "^3.38.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-WPjdr5oWOk1LArpI4pavu+fSo+/evi0Re1olxGvqwuZopd4gYR1UGCi+Soyn/IjmafqbWs+ZC+3AKxJS3ZpwUg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-web-identity/@aws-sdk/nested-clients/@smithy/fetch-http-handler/@smithy/core": ["@smithy/core@3.33.3", "", { "dependencies": { "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-CsOeKq/9kA3y6VJHt+/+VTCtBaxJ4OTFpgrjIUhPpDIKxBci1k2bJaQASF2h/ELWrulGp+t97DZ0mevfAD8idg=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-ini/@aws-sdk/nested-clients/@smithy/fetch-http-handler/@smithy/core/@smithy/types": ["@smithy/types@4.17.2", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-FOKpVZob9MPTn2znRzGrnsMHv7BOsKVw3XiP/cOyYLDVZ9qKp4nifIiSCuUU/fIj5Vu0UOAxCFr+qRAtG0NUkA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-sso/@aws-sdk/nested-clients/@smithy/fetch-http-handler/@smithy/core/@smithy/types": ["@smithy/types@4.17.2", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-FOKpVZob9MPTn2znRzGrnsMHv7BOsKVw3XiP/cOyYLDVZ9qKp4nifIiSCuUU/fIj5Vu0UOAxCFr+qRAtG0NUkA=="],
|
||||
|
||||
"@aws-sdk/client-bedrock-runtime/@aws-sdk/credential-provider-node/@aws-sdk/credential-provider-web-identity/@aws-sdk/nested-clients/@smithy/fetch-http-handler/@smithy/core/@smithy/types": ["@smithy/types@4.17.2", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-FOKpVZob9MPTn2znRzGrnsMHv7BOsKVw3XiP/cOyYLDVZ9qKp4nifIiSCuUU/fIj5Vu0UOAxCFr+qRAtG0NUkA=="],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,11 +21,6 @@ services:
|
||||
|
||||
shumai:
|
||||
image: crpi-000nkafzu6xvpwsc.cn-hongkong.personal.cr.aliyuncs.com/shumai-one/shumai:latest
|
||||
cap_add:
|
||||
- SYS_ADMIN
|
||||
security_opt:
|
||||
- apparmor=unconfined
|
||||
- seccomp=unconfined
|
||||
container_name: shumai_app
|
||||
ports:
|
||||
- '3000:3000'
|
||||
@@ -51,9 +46,6 @@ services:
|
||||
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
|
||||
BETTER_AUTH_URL: http://localhost:3000
|
||||
|
||||
# Sandbox Configuration
|
||||
ENABLE_WEAKER_NESTED_SANDBOX: 'true'
|
||||
|
||||
# Embedding Configuration
|
||||
EMBEDDING_PROVIDER: gemma-embedding-2-openai
|
||||
GEMMA_EMBEDDING_OPENAI_BASE_URL: http://embedding-server:9379/v1
|
||||
|
||||
@@ -31,11 +31,6 @@ services:
|
||||
|
||||
shumai:
|
||||
image: crpi-000nkafzu6xvpwsc.cn-hongkong.personal.cr.aliyuncs.com/shumai-one/shumai:latest
|
||||
cap_add:
|
||||
- SYS_ADMIN
|
||||
security_opt:
|
||||
- apparmor=unconfined
|
||||
- seccomp=unconfined
|
||||
container_name: shumai_app
|
||||
ports:
|
||||
- '3000:3000'
|
||||
@@ -61,9 +56,6 @@ services:
|
||||
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
|
||||
BETTER_AUTH_URL: http://localhost:3000
|
||||
|
||||
# Sandbox Configuration
|
||||
ENABLE_WEAKER_NESTED_SANDBOX: 'true'
|
||||
|
||||
# Embedding Configuration
|
||||
EMBEDDING_PROVIDER: gemma-embedding-2-openai
|
||||
GEMMA_EMBEDDING_OPENAI_BASE_URL: http://embedding-server:9379/v1
|
||||
|
||||
@@ -16,11 +16,6 @@ services:
|
||||
|
||||
shumai:
|
||||
image: crpi-000nkafzu6xvpwsc.cn-hongkong.personal.cr.aliyuncs.com/shumai-one/shumai:latest
|
||||
cap_add:
|
||||
- SYS_ADMIN
|
||||
security_opt:
|
||||
- apparmor=unconfined
|
||||
- seccomp=unconfined
|
||||
container_name: shumai_app
|
||||
ports:
|
||||
- '3000:3000'
|
||||
@@ -46,9 +41,6 @@ services:
|
||||
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
|
||||
BETTER_AUTH_URL: http://localhost:3000
|
||||
|
||||
# Sandbox Configuration
|
||||
ENABLE_WEAKER_NESTED_SANDBOX: 'true'
|
||||
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
|
||||
@@ -21,11 +21,6 @@ services:
|
||||
|
||||
shumai:
|
||||
image: shumaione/shumai:latest
|
||||
cap_add:
|
||||
- SYS_ADMIN
|
||||
security_opt:
|
||||
- apparmor=unconfined
|
||||
- seccomp=unconfined
|
||||
container_name: shumai_app
|
||||
ports:
|
||||
- '3000:3000'
|
||||
@@ -51,9 +46,6 @@ services:
|
||||
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
|
||||
BETTER_AUTH_URL: http://localhost:3000
|
||||
|
||||
# Sandbox Configuration
|
||||
ENABLE_WEAKER_NESTED_SANDBOX: 'true'
|
||||
|
||||
# Embedding Configuration
|
||||
EMBEDDING_PROVIDER: gemma-embedding-2-openai
|
||||
GEMMA_EMBEDDING_OPENAI_BASE_URL: http://embedding-server:9379/v1
|
||||
|
||||
@@ -31,11 +31,6 @@ services:
|
||||
|
||||
shumai:
|
||||
image: shumaione/shumai:latest
|
||||
cap_add:
|
||||
- SYS_ADMIN
|
||||
security_opt:
|
||||
- apparmor=unconfined
|
||||
- seccomp=unconfined
|
||||
container_name: shumai_app
|
||||
ports:
|
||||
- '3000:3000'
|
||||
@@ -61,9 +56,6 @@ services:
|
||||
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
|
||||
BETTER_AUTH_URL: http://localhost:3000
|
||||
|
||||
# Sandbox Configuration
|
||||
ENABLE_WEAKER_NESTED_SANDBOX: 'true'
|
||||
|
||||
# Embedding Configuration
|
||||
EMBEDDING_PROVIDER: gemma-embedding-2-openai
|
||||
GEMMA_EMBEDDING_OPENAI_BASE_URL: http://embedding-server:9379/v1
|
||||
|
||||
@@ -16,11 +16,6 @@ services:
|
||||
|
||||
shumai:
|
||||
image: shumaione/shumai:latest
|
||||
cap_add:
|
||||
- SYS_ADMIN
|
||||
security_opt:
|
||||
- apparmor=unconfined
|
||||
- seccomp=unconfined
|
||||
container_name: shumai_app
|
||||
ports:
|
||||
- '3000:3000'
|
||||
@@ -46,9 +41,6 @@ services:
|
||||
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
|
||||
BETTER_AUTH_URL: http://localhost:3000
|
||||
|
||||
# Sandbox Configuration
|
||||
ENABLE_WEAKER_NESTED_SANDBOX: 'true'
|
||||
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
|
||||
@@ -3,6 +3,40 @@ title: "Changelog"
|
||||
description: "Recent updates, new features, and improvements in Shumai"
|
||||
---
|
||||
|
||||
<Update label="v0.7.0" description="November 18, 2026">
|
||||
|
||||
### Added
|
||||
|
||||
- **i18n**: Add Korean as a display language, selectable on the sign-up page and in team settings.
|
||||
|
||||
### Fixed
|
||||
|
||||
- **cli**: Fix release publication failing to find staged standalone downloads.
|
||||
|
||||
### Changed
|
||||
|
||||
- **agent**: ⚠️ **Breaking Change**: Replaced the built-in server-side Bash sandbox with external Runners and removed the team-level sandbox domain allowlist settings. The Bash tool is now disabled by default. If your Agent or Skills require Bash execution, you'll need to set up and connect an external Runner using shumai-cli. The Shumai CLI includes built-in sandbox support, allowing you to configure filesystem access and network domain allowlists for Bash execution directly on the Runner. See [Agent Runners](https://docs.shumai.one/using-shumai/agent-sandbox) for setup instructions and configuration details. The Docker Compose configuration has also been updated to improve security following the removal of the server-side sandbox. We strongly recommend upgrading to the latest Shumai image and updating your Docker Compose file to apply these security improvements.
|
||||
- **cli**: Show copyable commands and shell configuration instructions when the standalone CLI installation directory is missing from PATH.
|
||||
|
||||
</Update>
|
||||
|
||||
<Update label="v0.6.6" description="November 13, 2026">
|
||||
|
||||
### Added
|
||||
|
||||
- **project**: Support creating new projects via the built-in agent, MCP server, and CLI (`shumai-cli project create`).
|
||||
- **cli**: Add standalone downloads and one-line installers for macOS, Linux, and Windows without requiring Node.js or Bun, with verified downloads, version selection, and upgrade support.
|
||||
|
||||
### Fixed
|
||||
|
||||
- **storage**: Remove empty local folders left behind after generating video search embeddings.
|
||||
|
||||
### Changed
|
||||
|
||||
- **agent**: Upgrade the agent runtime and automatically migrate existing conversations, keep thread histories independent, prevent duplicate history during simultaneous mentions, improve session log scrolling and readability, and fix authentication for OpenAI and custom providers. Agents now run with the main server; standalone agent workers are no longer supported.
|
||||
|
||||
</Update>
|
||||
|
||||
<Update label="v0.6.5" description="November 7, 2026">
|
||||
|
||||
### Added
|
||||
|
||||
@@ -1,39 +1,20 @@
|
||||
---
|
||||
title: Deploy with Distributed Workers
|
||||
description: Configure a high-scale production deployment using dedicated background workers, DBOS, and S3-compatible storage.
|
||||
description: Run media transcoding on dedicated workers while agents run with the main server.
|
||||
---
|
||||
|
||||
For production environments or workloads with high-volume video processing and heavy AI agent usage, running background tasks on a single server can cause bottlenecks. Shumai resolves this by decoupling the web dashboard from background processing through dedicated worker nodes orchestrated by **DBOS Transact**.
|
||||
|
||||
All background tasks (media transcoding, AI embeddings, agent tasks) are dispatched to durable task queues (`transcode_queue` and `agent_queue`) hosted directly in PostgreSQL. No separate external cluster (like Temporal) is required.
|
||||
|
||||
---
|
||||
|
||||
## How it Works
|
||||
|
||||
In a distributed deployment, the core Shumai web application handles user requests, database updates, and file uploads, while delegating durable background workflows to specialized worker instances via PostgreSQL task queues.
|
||||
Shumai runs agent, embedding, and archive queues with the main web server. Media transcoding can run on dedicated workers through PostgreSQL queues managed by DBOS Transact.
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
UserUpload[User Uploads Video / Triggers Agent] --> WebApp[Shumai Web UI / API]
|
||||
WebApp -->|Enqueue Task| DB[(PostgreSQL / DBOS Queues)]
|
||||
DB -->|Queue: transcode_queue| TranscodeWorker[Transcode Worker Machine]
|
||||
DB -->|Queue: agent_queue| AgentWorker[Agent Worker Machine]
|
||||
User[User] --> Server[Shumai Web UI / API + Agents + Archives]
|
||||
Server --> DB[(PostgreSQL / DBOS Queues)]
|
||||
DB --> Transcode[Dedicated Transcode Workers]
|
||||
```
|
||||
|
||||
- **Transcoding Workers**: Can be run on GPU-accelerated or high-CPU compute instances.
|
||||
- **AI Agent Workers**: Can be run on isolated, sandboxed hosts to safely execute automation scripts.
|
||||
## Main Server
|
||||
|
||||
> [!IMPORTANT]
|
||||
> **Storage Requirement**: Because the core application and the background workers run on separate machines in a distributed setup, **you must configure an S3-compatible storage backend** (such as AWS S3, Cloudflare R2, or MinIO). Local storage (`STORAGE_BACKEND=local`) is not supported in this mode because the workers and the web application do not share a common filesystem.
|
||||
|
||||
---
|
||||
|
||||
## Step 1: Deploy the Core Web Application
|
||||
|
||||
On your primary host machine, run PostgreSQL and the main Shumai web application.
|
||||
|
||||
To instruct the main web application to handle only API and UI traffic without running background worker queues locally, set `SHUMAI_WORKERS=none`:
|
||||
Set `SHUMAI_WORKERS=none` (or `--workers=none`) to run agents and archives with the main server and send transcoding jobs to dedicated workers. `all` and `transcode` also run the transcode queue in the server. The older `agent`, `ai`, and `archive` values remain accepted and run agents and archives.
|
||||
|
||||
```env
|
||||
DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public
|
||||
@@ -47,67 +28,30 @@ SHUMAI_WORKERS=none
|
||||
BETTER_AUTH_SECRET=your-random-32-character-secret-key
|
||||
```
|
||||
|
||||
When started with `SHUMAI_WORKERS=none` (or `--workers=none`), the web server starts immediately without polling or consuming tasks from the transcode or agent queues.
|
||||
Agent tools run on the server, so install its sandbox dependencies (`bubblewrap` on Linux or `sandbox-exec` on macOS). Standalone agent workers are no longer supported.
|
||||
|
||||
---
|
||||
## Dedicated Transcode Workers
|
||||
|
||||
## Step 2: Configure & Run Dedicated Workers
|
||||
|
||||
On your specialized worker machines, configure each worker with a `.env` file referencing the shared PostgreSQL database and S3 storage:
|
||||
|
||||
```env
|
||||
DATABASE_URL=postgresql://shumai:shumai_password@<primary-host-ip>:5432/shumai_db?schema=public
|
||||
STORAGE_BACKEND=s3
|
||||
AWS_ENDPOINT_URL_S3=https://your-s3-endpoint.com
|
||||
S3_BUCKET=shumai
|
||||
S3_REGION=us-east-1
|
||||
S3_ACCESS_KEY_ID=your-access-key-id
|
||||
S3_SECRET_ACCESS_KEY=your-secret-access-key
|
||||
CONCURRENCY_TRANSCODE=1
|
||||
CONCURRENCY_AGENT=5
|
||||
```
|
||||
|
||||
### Media Transcoding Worker
|
||||
|
||||
Run this on your media processing nodes (requires system installations of `ffmpeg` and `ffprobe`):
|
||||
Configure workers with the same PostgreSQL database and S3 storage as the server. A shared S3-compatible backend is required when machines do not share a filesystem.
|
||||
|
||||
```bash
|
||||
npm install -g @shumai-one/shumai-transcode
|
||||
shumai-transcode
|
||||
```
|
||||
|
||||
Alternatively, you can run the transcode worker using the dedicated lightweight Docker image:
|
||||
Or use the dedicated image:
|
||||
|
||||
```bash
|
||||
docker run -d \
|
||||
--name shumai-transcode-worker \
|
||||
--env-file .env \
|
||||
shumaione/shumai-transcode:latest
|
||||
docker run -d --name shumai-transcode-worker --env-file .env shumaione/shumai-transcode:latest
|
||||
```
|
||||
|
||||
(You can also use the all-in-one image: `docker run -d --name shumai-transcode-worker --env-file .env shumaione/shumai:latest shumai-transcode`.)
|
||||
Bun users can run `bun run --bun shumai-transcode`.
|
||||
|
||||
### AI Agent Worker
|
||||
## Upgrading Existing Agent Sessions
|
||||
|
||||
Run this on your sandboxed agent execution nodes (requires sandbox dependencies like `bubblewrap` or `sandbox-exec`):
|
||||
Stop and drain old agent workers before upgrading the server. The normal database migration copies existing session context into independent histories automatically. No separate migration command is required. Legacy records remain in the database during this transition.
|
||||
|
||||
```bash
|
||||
npm install -g @shumai-one/shumai-agent
|
||||
shumai-agent
|
||||
```
|
||||
|
||||
Alternatively, you can run the agent worker using the official Docker image:
|
||||
|
||||
```bash
|
||||
docker run -d \
|
||||
--name shumai-agent-worker \
|
||||
--env-file .env \
|
||||
--cap-add=SYS_ADMIN \
|
||||
shumaione/shumai:latest shumai-agent
|
||||
```
|
||||
|
||||
> [!IMPORTANT]
|
||||
> **For Bun Users:** Since the worker package binaries contain a `#!/usr/bin/env node` shebang, running `shumai-transcode` or `shumai-agent` directly will run under Node.js. If you wish to run them using Bun, prefix the commands with `bun run --bun` (e.g., `bun run --bun shumai-transcode` or `bun run --bun shumai-agent`).
|
||||
Each session uses a local SQLite working copy, restored from PostgreSQL when missing. `AGENT_SESSION_CACHE_DIR` optionally selects the cache directory. Within each server process, history reads and the active agent share the same SQLite database. History can show locally committed entries before PostgreSQL persistence completes; these entries may disappear after recovery if persistence fails. The server mirrors compact session commits to PostgreSQL before acknowledging them. Keep the database in your backup policy; local SQLite copies are disposable. Image and skill placeholders remain compact in both copies and are resolved when the agent needs them.
|
||||
|
||||
---
|
||||
|
||||
@@ -118,16 +62,18 @@ docker run -d \
|
||||
Workers pull tasks from PostgreSQL queues based on configurable concurrency limits:
|
||||
|
||||
- `CONCURRENCY_TRANSCODE`: Maximum concurrent video transcoding jobs per worker (defaults to `1`).
|
||||
- `CONCURRENCY_AGENT`: Maximum concurrent agent and embedding workflows per worker (defaults to `5`, recommend setting to `1` when paired with a local `embedding-server`).
|
||||
- `CONCURRENCY_AGENT`: Maximum concurrent agent and embedding workflows per main server (defaults to `5`, recommend setting to `1` when paired with a local `embedding-server`).
|
||||
|
||||
### Replica Identity
|
||||
|
||||
When running multiple replicas of the transcode or agent worker across multiple machines or pods:
|
||||
When running multiple replicas of the transcode workers or main servers across multiple machines or pods:
|
||||
|
||||
- `DbosExecutor` automatically derives a unique per-instance ID from the machine hostname and process ID to ensure startup recovery only re-enqueues tasks belonging to that specific instance.
|
||||
- **Custom Worker ID**: Set `SHUMAI_WORKERS_ID` (or `SHUMAI_WORKER_ID`) if you wish to assign an explicit identifier.
|
||||
- **Kubernetes**: In Kubernetes Pods, set `POD_NAME` (via Downward API `fieldRef: { fieldPath: metadata.name }`) to provide stable, unique per-pod identity across pod restarts.
|
||||
|
||||
Agent runners require a single main server per database. With runners configured, stop the old main server before starting its replacement. For Kubernetes, use a single main server replica with the Recreate deployment strategy. Transcode workers can still scale independently.
|
||||
|
||||
---
|
||||
|
||||
## Scalable Deployment on Kubernetes & AWS EKS
|
||||
|
||||
@@ -14,6 +14,10 @@
|
||||
},
|
||||
"navbar": {
|
||||
"links": [
|
||||
{
|
||||
"label": "Forum",
|
||||
"href": "https://shumai.discourse.group/"
|
||||
},
|
||||
{
|
||||
"label": "Changelog",
|
||||
"href": "/changelog"
|
||||
@@ -27,6 +31,10 @@
|
||||
"default": true,
|
||||
"navbar": {
|
||||
"links": [
|
||||
{
|
||||
"label": "Forum",
|
||||
"href": "https://shumai.discourse.group/"
|
||||
},
|
||||
{
|
||||
"label": "Changelog",
|
||||
"href": "/changelog"
|
||||
@@ -109,6 +117,10 @@
|
||||
"language": "zh",
|
||||
"navbar": {
|
||||
"links": [
|
||||
{
|
||||
"label": "论坛",
|
||||
"href": "https://shumai.discourse.group/"
|
||||
},
|
||||
{
|
||||
"label": "Changelog",
|
||||
"href": "/changelog"
|
||||
|
||||
@@ -99,7 +99,7 @@ Docker Compose is the fastest way to get Shumai running. You do not need to clon
|
||||
|
||||
### Production Deployments (Distributed Workers)
|
||||
|
||||
If you are deploying Shumai for a production team with high-volume video uploads, we recommend setting up a distributed deployment using dedicated workers and an S3-compatible storage backend. This offloads resource-heavy video transcoding and AI agents to separate, dedicated worker nodes to keep your main dashboard fast and responsive. For setup instructions, see the [Deploy with Distributed Workers](/configuration/distributed) guide.
|
||||
If you are deploying Shumai for a production team with high-volume video uploads, we recommend setting up a distributed deployment using dedicated workers and an S3-compatible storage backend. This offloads resource-heavy video transcoding to dedicated worker nodes to keep your main dashboard fast and responsive. For setup instructions, see the [Deploy with Distributed Workers](/configuration/distributed) guide.
|
||||
|
||||
---
|
||||
|
||||
@@ -216,3 +216,23 @@ To set up Shumai locally for development:
|
||||
bun run dev
|
||||
```
|
||||
This spins up the web server on port `3000` with hot-reloading active.
|
||||
|
||||
---
|
||||
|
||||
## Install the command-line client
|
||||
|
||||
To connect to a running Shumai server, install the standalone CLI without Node.js or Bun:
|
||||
|
||||
```sh
|
||||
curl -fsSL https://github.com/shumaiOne/shumai/releases/latest/download/install.sh | sh
|
||||
```
|
||||
|
||||
On Windows:
|
||||
|
||||
```powershell
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -Command "irm https://github.com/shumaiOne/shumai/releases/latest/download/install.ps1 | iex"
|
||||
```
|
||||
|
||||
Native releases support macOS, Linux with glibc, and Windows on ARM64 and x64. The scripts verify SHA-256 checksums and install into your user directory. Re-run the installer to upgrade, and check the installed version with `shumai-cli --version`. The npm alternative, `npm install -g @shumai-one/shumai-cli`, requires Node.js 22.12+.
|
||||
|
||||
Set `SHUMAI_API_SERVER` to your server URL and `SHUMAI_API_KEY` to a personal API token from Settings → API Tokens, then run `shumai-cli project ls`. See the [CLI guide](https://github.com/shumaiOne/shumai/blob/main/apps/cli/README.md) for supported OS versions, PATH setup, pinned versions, rollback, and uninstall instructions.
|
||||
|
||||
@@ -1,45 +1,101 @@
|
||||
---
|
||||
title: Agent Sandbox
|
||||
description: Secure execution environment and network domain restrictions for AI agent scripts.
|
||||
title: Agent Runners
|
||||
description: Connect an optional command runner and configure sandboxing on its machine.
|
||||
---
|
||||
|
||||
Running automated scripts poses security risks. Shumai resolves this by executing all agent skills inside a secure, isolated **Sandbox** environment.
|
||||
Agents use Shumai's built-in tools without a runner. To execute shell commands or download assets into a shell workspace, connect a **runner** on a machine you control. The runner opens an authenticated outbound WebSocket connection; no inbound port is needed.
|
||||
|
||||
---
|
||||
Agent runners require one main Shumai server per database. Multiple main servers can share a database while it has no configured runners. Stop the other main servers before creating a runner. Transcode workers can run separately.
|
||||
|
||||
## Sandbox Security
|
||||
## Connect a runner
|
||||
|
||||
The sandbox restricts the execution process from accessing the host filesystem, system processes, and internal networks:
|
||||
1. In **Settings → Agent Runners**, create a runner and copy its one-time key. Only team owners can manage runners and rotate their keys.
|
||||
2. Install [Shumai CLI](https://github.com/shumaiOne/shumai/tree/main/apps/cli#installation) on macOS, Linux, or Windows.
|
||||
3. Start it in a dedicated, existing workspace:
|
||||
|
||||
- **Isolation**: Scripts run in a virtualized workspace container that prevents them from modifying critical system files or reading other project data.
|
||||
- **Network Bridging**: External network calls from inside the sandbox are strictly monitored.
|
||||
```bash
|
||||
export SHUMAI_API_SERVER="https://shumai.example.com"
|
||||
export SHUMAI_RUNNER_KEY="your-runner-key"
|
||||
shumai-cli runner start --cwd /path/to/workspace
|
||||
```
|
||||
|
||||
### File System Restrictions
|
||||
On Windows, set the variables in PowerShell:
|
||||
|
||||
To protect your workspace environments and sensitive data, the sandbox enforces strict file-level access controls:
|
||||
```powershell
|
||||
$env:SHUMAI_API_SERVER = "https://shumai.example.com"
|
||||
$env:SHUMAI_RUNNER_KEY = "your-runner-key"
|
||||
shumai-cli runner start --cwd C:\workspace
|
||||
```
|
||||
|
||||
- **Read Access**: The sandbox is allowed to read files within the workspace, with a strict exception for sensitive environment and certificate files. It cannot read: `['.env', '.env.*', '*.pem', '*.key']`.
|
||||
- **Write Access**: The sandbox is heavily restricted and may only write to the `.pi` folder located in the current working directory (cwd). Even within this allowed directory, the sandbox is explicitly blocked from writing or creating sensitive files matching: `['.env', '.env.*', '*.pem', '*.key']`.
|
||||
Use a runner key, not a personal API token. Prefer the environment variable over `--key` to keep the key out of shell history. The runner stays in the foreground; Ctrl+C stops it and its active commands. Temporary connection losses trigger reconnection. Revoked or replaced connections stop instead of reconnecting.
|
||||
|
||||
---
|
||||
The first runner becomes the team default. Each agent can use that default or a dedicated runner selected in its settings. Without a configured runner, `bash` and `download_asset` are hidden. With one configured, authorized editors and reviewers can run commands immediately without reading a skill first. Disabled-tool policies still apply. An offline runner produces an execution error.
|
||||
|
||||
## Domain Whitelisting
|
||||
The `bash` tool uses `/bin/sh` on macOS and Linux. On Windows, the runner looks for Git Bash in the standard installation directories, then for `bash.exe` on PATH, and falls back to `cmd.exe`. The runner prints its selected shell at startup and reports it to the agent so it can use compatible syntax. For Bash with SRT on Windows, use Git Bash in place of the legacy WSL Bash launcher.
|
||||
|
||||
By default, Network Sandbox is **disabled** (allowing all outbound network requests). Team Owners can toggle Network Sandbox **ON** in Sandbox Settings to enforce strict domain whitelisting:
|
||||
Commands start in the workspace directory. Skills, downloaded assets, generated files, and output logs live under `.pi`. Commands inherit baseline OS settings and explicit skill variables, rather than the runner's full environment, including its runner key and personal API token.
|
||||
|
||||
### Allowed Domains
|
||||
Everyone with shell access to the same runner can read files left in its workspace, including assets and logs from other projects. Shumai project permissions apply when assets are downloaded. Share a runner only among users who may read all files in that workspace. For separate trust groups, use separate runner accounts or virtual machines and restrict access to the agents bound to each runner.
|
||||
|
||||
A list of domains that sandboxed scripts are permitted to access.
|
||||
## Optional SRT sandbox
|
||||
|
||||
- Default whitelisted domains include standard package registries (like npm or python package hosts) and repository hosts (like `github.com`).
|
||||
- If a script needs to call an external API (e.g. `api.slack.com`), the administrator must add it to the allowed list.
|
||||
Without `--srt`, commands have the runner operating-system user's permissions. Connect only to a trusted Shumai server; use a dedicated container, VM, or account when appropriate.
|
||||
|
||||
### Pending Domains
|
||||
[Anthropic Sandbox Runtime](https://github.com/anthropics/sandbox-runtime) is bundled as a library in Shumai CLI. Install its platform prerequisites on the runner machine (Linux: bubblewrap, socat, and ripgrep; macOS: ripgrep), then start with its JSON settings:
|
||||
|
||||
When an agent script attempts to reach a domain that is not currently authorized:
|
||||
```bash
|
||||
shumai-cli runner start --cwd /path/to/workspace --srt sandbox.json
|
||||
```
|
||||
|
||||
1. The network connection is blocked immediately.
|
||||
2. The target domain is logged and added to the **Pending Domains** list.
|
||||
3. Admins can review this list and click **Approve** to authorize the domain, or **Reject** to keep it blocked.
|
||||
Windows uses SRT's alpha Windows support. Run the one-time setup before starting a sandboxed runner; it requests administrator permission:
|
||||
|
||||

|
||||
```powershell
|
||||
npx @anthropic-ai/sandbox-runtime windows-install
|
||||
```
|
||||
|
||||
SRT verifies the setup at startup and reports an error if it is missing. A runner without `--srt` does not need this setup.
|
||||
|
||||
The JSON uses upstream SRT settings directly. For example:
|
||||
|
||||
```json
|
||||
{
|
||||
"filesystem": {
|
||||
"denyRead": ["~/.ssh", "~/.aws", ".env", ".env.*"],
|
||||
"allowWrite": [".pi"],
|
||||
"denyWrite": []
|
||||
},
|
||||
"network": {
|
||||
"allowedDomains": [],
|
||||
"deniedDomains": []
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This example allows writes only in `.pi`, denies reads of the listed credential locations, and blocks command network access. Review read permissions and network rules for your environment using SRT's documentation. Relative filesystem paths use the runner workspace. Settings are snapshotted at startup; restart after changing them. Missing or invalid settings, missing SRT prerequisites, and sandbox initialization failures stop startup. Commands never fall back to unrestricted execution.
|
||||
|
||||
SRT wraps shell commands and their child processes. One shared SRT runtime handles concurrent commands and is reset when the runner stops. The runner's WebSocket and server-directed file transfers operate outside SRT. **The Shumai server address is not automatically added to allowed domains.** If a script itself needs network access, explicitly allow its destination in the JSON.
|
||||
|
||||
Shumai has no server-side sandbox or domain settings. SRT is initialized only on runners that opt into it. SRT in Docker may require namespace permissions on that dedicated runner container; these permissions are not needed by the Shumai server.
|
||||
|
||||
## Runner in Docker
|
||||
|
||||
An ordinary runner can run in its own container without extra capabilities or an exposed port:
|
||||
|
||||
```bash
|
||||
docker run --rm --init --name shumai-runner \
|
||||
--cap-drop ALL --security-opt no-new-privileges \
|
||||
-e SHUMAI_API_SERVER -e SHUMAI_RUNNER_KEY \
|
||||
-v shumai-runner-workspace:/workspace -w /workspace \
|
||||
node:22-bookworm sh -c \
|
||||
'npm install -g @shumai-one/shumai-cli && exec shumai-cli runner start'
|
||||
```
|
||||
|
||||
For development, HTTP is accepted for `localhost`, `127.0.0.1`, `[::1]`, and Docker's `host.docker.internal`. Remote servers require HTTPS. Avoid mounting sensitive host directories or the Docker socket into the runner workspace.
|
||||
|
||||
## Limits and lifecycle
|
||||
|
||||
Commands default to 120 seconds and accept a maximum timeout of one hour. Timeout and cancellation attempt to terminate the command and its process tree. Each command saves up to 64 MiB of output in `.pi/logs`; manage retained logs as part of workspace maintenance. Transfers and extracted archives are limited to 256 MiB. Protocol file operations reject paths outside `.pi`, existing symlinks, and unsafe archive entries. V1 assumes commands do not deliberately change workspace paths while transfers are running.
|
||||
|
||||
Temporary reconnects reconcile active and recently completed commands without deliberately executing them again. Restarting the runner process does not resume commands from the previous process.
|
||||
|
||||
Up to eight commands and file transfers can run concurrently in the shared workspace. Additional requests return a busy error. On Windows without SRT, background processes may survive after the command's shell exits; remaining output pipes are closed after one second. Use foreground commands for predictable timeout and cancellation. SRT manages the sandboxed Windows process tree.
|
||||
|
||||
@@ -35,7 +35,7 @@ The official Shumai Premiere Pro UXP plugin provides a native panel inside Adobe
|
||||
|
||||
> [!NOTE]
|
||||
> **Experimental Feature:**
|
||||
> The Premiere Pro UXP plugin is currently experimental. While functional, it has not yet been tested across all Premiere Pro minor versions and operating system configurations. If you encounter any bugs, unexpected behavior, or have workflow suggestions, please [open an issue on GitHub](https://github.com/shumaiOne/shumai/issues) or join our [Discord community](https://discord.gg/sUzhujK6DD).
|
||||
> The Premiere Pro UXP plugin is currently experimental. While functional, it has not yet been tested across all Premiere Pro minor versions and operating system configurations. If you encounter any bugs, unexpected behavior, or have workflow suggestions, please [open an issue on GitHub](https://github.com/shumaiOne/shumai/issues) or join our [Discourse forum](https://shumai.discourse.group/).
|
||||
|
||||
### Minimum Version Requirements
|
||||
|
||||
|
||||
@@ -1,113 +1,38 @@
|
||||
---
|
||||
title: 分布式 Worker 部署
|
||||
description: 通过独立的后台 Worker、DBOS 与兼容 S3 的对象存储配置大规模生产级部署。
|
||||
description: 将媒体转码部署到独立 Worker,智能体随主服务器运行。
|
||||
---
|
||||
|
||||
在面对高并发视频处理或重度 AI 智能体调用的生产环境中,若将所有后台任务集中在单台服务器上运行,极易引发性能瓶颈。Shumai 通过由 **DBOS Transact** 统一编排的独立 Worker 节点,将 Web 控制台与后台处理彻底解耦,轻松化解瓶颈。
|
||||
智能体、嵌入和归档队列随 Shumai 主服务器一起启动。媒体转码仍可通过 PostgreSQL 和 DBOS 队列交给独立 Worker。
|
||||
|
||||
所有后台任务(媒体转码、AI 向量嵌入、智能体任务)均分发至直接托管在 PostgreSQL 中的持久化任务队列(`transcode_queue` 与 `agent_queue`)。无需部署额外的外部集群(如 Temporal)。
|
||||
## 主服务器
|
||||
|
||||
---
|
||||
设置 `SHUMAI_WORKERS=none`(或 `--workers=none`)时,主服务器处理智能体和归档任务,转码交给独立 Worker。`all` 和 `transcode` 会同时在主服务器运行转码队列。旧的 `agent`、`ai`、`archive` 参数仍可使用,均启动智能体和归档队列。
|
||||
|
||||
## 核心工作原理
|
||||
主服务器需安装智能体沙箱依赖(Linux 使用 `bubblewrap`,macOS 使用 `sandbox-exec`)。不再支持独立启动智能体 Worker。
|
||||
|
||||
在分布式部署中,Shumai 核心 Web 应用负责处理用户请求、数据库更新和文件上传,并通过 PostgreSQL 任务队列将持久化后台工作流委派给专用的 Worker 实例。
|
||||
## 独立转码 Worker
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
UserUpload[用户上传视频 / 触发智能体] --> WebApp[Shumai Web UI / API]
|
||||
WebApp -->|入队任务| DB[(PostgreSQL / DBOS 队列)]
|
||||
DB -->|队列: transcode_queue| TranscodeWorker[转码 Worker 机器]
|
||||
DB -->|队列: agent_queue| AgentWorker[智能体 Worker 机器]
|
||||
```
|
||||
|
||||
- **转码 Worker**:可在配备 GPU 加速或高配 CPU 的计算实例上运行。
|
||||
- **AI 智能体 Worker**:可在隔离沙箱环境中运行,以安全执行自动化脚本。
|
||||
|
||||
> [!IMPORTANT]
|
||||
> **存储配置硬性要求**:由于在分布式部署下核心应用与后台 Worker 分别运行在不同机器上,**必须配置兼容 S3 的对象存储后端**(例如 AWS S3、Cloudflare R2 或 MinIO)。此模式下不支持本地存储(`STORAGE_BACKEND=local`),因为 Worker 节点与 Web 应用无法共享本地文件系统。
|
||||
|
||||
---
|
||||
|
||||
## 第 1 步:部署核心 Web 应用
|
||||
|
||||
在主服务器上运行 PostgreSQL 和 Shumai 核心 Web 应用。
|
||||
|
||||
若需指示核心 Web 应用仅处理 API 和前端 UI 流量,而不本地消费后台 Worker 队列任务,请设置 `SHUMAI_WORKERS=none`:
|
||||
|
||||
```env
|
||||
DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public
|
||||
STORAGE_BACKEND=s3
|
||||
AWS_ENDPOINT_URL_S3=https://your-s3-endpoint.com
|
||||
S3_BUCKET=shumai
|
||||
S3_REGION=us-east-1
|
||||
S3_ACCESS_KEY_ID=your-access-key-id
|
||||
S3_SECRET_ACCESS_KEY=your-secret-access-key
|
||||
SHUMAI_WORKERS=none
|
||||
BETTER_AUTH_SECRET=your-random-32-character-secret-key
|
||||
```
|
||||
|
||||
当使用 `SHUMAI_WORKERS=none`(或启动参数 `--workers=none`)启动时,Web 服务器将即刻启动,不会轮询或消费来自转码或智能体队列的任务。
|
||||
|
||||
---
|
||||
|
||||
## 第 2 步:配置并运行专属 Worker
|
||||
|
||||
在专用 Worker 机器上,配置各 Worker 的 `.env` 文件以指向共享的 PostgreSQL 数据库与 S3 存储:
|
||||
|
||||
```env
|
||||
DATABASE_URL=postgresql://shumai:shumai_password@<primary-host-ip>:5432/shumai_db?schema=public
|
||||
STORAGE_BACKEND=s3
|
||||
AWS_ENDPOINT_URL_S3=https://your-s3-endpoint.com
|
||||
S3_BUCKET=shumai
|
||||
S3_REGION=us-east-1
|
||||
S3_ACCESS_KEY_ID=your-access-key-id
|
||||
S3_SECRET_ACCESS_KEY=your-secret-access-key
|
||||
CONCURRENCY_TRANSCODE=1
|
||||
CONCURRENCY_AGENT=5
|
||||
```
|
||||
|
||||
### 媒体转码 Worker
|
||||
|
||||
在媒体处理计算节点上运行(需要预先在系统上安装 `ffmpeg` 与 `ffprobe`):
|
||||
在 Worker 上配置与主服务器相同的 PostgreSQL 数据库和 S3 存储。多台机器不共享文件系统时,必须使用 S3 兼容存储。
|
||||
|
||||
```bash
|
||||
npm install -g @shumai-one/shumai-transcode
|
||||
shumai-transcode
|
||||
```
|
||||
|
||||
或者,可以使用轻量级专属 Docker 镜像运行转码 Worker:
|
||||
也可以使用 Docker:
|
||||
|
||||
```bash
|
||||
docker run -d \
|
||||
--name shumai-transcode-worker \
|
||||
--env-file .env \
|
||||
shumaione/shumai-transcode:latest
|
||||
docker run -d --name shumai-transcode-worker --env-file .env shumaione/shumai-transcode:latest
|
||||
```
|
||||
|
||||
(亦可使用一体化全功能镜像:`docker run -d --name shumai-transcode-worker --env-file .env shumaione/shumai:latest shumai-transcode`。)
|
||||
Bun 用户可运行 `bun run --bun shumai-transcode`。
|
||||
|
||||
### AI 智能体 Worker
|
||||
## 升级现有智能体会话
|
||||
|
||||
在沙箱化智能体执行节点上运行(需要安装 `bubblewrap` 或 `sandbox-exec` 等沙箱依赖项):
|
||||
升级主服务器前,请等待旧智能体 Worker 完成任务并将其停止。正常数据库迁移会自动将已有会话上下文复制为独立记录,无需额外运行迁移脚本。过渡期间保留旧记录表。
|
||||
|
||||
```bash
|
||||
npm install -g @shumai-one/shumai-agent
|
||||
shumai-agent
|
||||
```
|
||||
|
||||
或者,可以使用官方 Docker 镜像运行智能体 Worker:
|
||||
|
||||
```bash
|
||||
docker run -d \
|
||||
--name shumai-agent-worker \
|
||||
--env-file .env \
|
||||
--cap-add=SYS_ADMIN \
|
||||
shumaione/shumai:latest shumai-agent
|
||||
```
|
||||
|
||||
> [!IMPORTANT]
|
||||
> **Bun 用户须知**:由于 Worker 依赖包的二进制可执行文件包含 `#!/usr/bin/env node` Shebang,直接运行 `shumai-transcode` 或 `shumai-agent` 将在 Node.js 下执行。若希望使用 Bun 运行,请在命令前加上 `bun run --bun`(例如 `bun run --bun shumai-transcode` 或 `bun run --bun shumai-agent`)。
|
||||
每个会话使用独立的本地 SQLite 工作副本,缺失时自动从 PostgreSQL 恢复。可用 `AGENT_SESSION_CACHE_DIR` 指定缓存目录。主服务器在确认提交前将精简记录同步至 PostgreSQL。本地副本可重建,备份应包含 PostgreSQL。图片和技能内容在两种存储中继续使用动态占位符,在智能体需要时解析。
|
||||
|
||||
---
|
||||
|
||||
@@ -118,16 +43,18 @@ docker run -d \
|
||||
Worker 根据可配置的并发上限从 PostgreSQL 队列中拉取任务:
|
||||
|
||||
- `CONCURRENCY_TRANSCODE`:单个 Worker 最大并发视频转码任务数(默认 `1`)。
|
||||
- `CONCURRENCY_AGENT`:单个 Worker 最大并发智能体及向量嵌入工作流数(默认 `5`,若搭配本地 `embedding-server` 建议设为 `1`)。
|
||||
- `CONCURRENCY_AGENT`:单个主服务器最大并发智能体及向量嵌入工作流数(默认 `5`,若搭配本地 `embedding-server` 建议设为 `1`)。
|
||||
|
||||
### 副本实例标识
|
||||
|
||||
当在多台机器或 Pod 上运行转码或智能体 Worker 的多个副本时:
|
||||
当在多台机器或 Pod 上运行转码 Worker 或主服务器 的多个副本时:
|
||||
|
||||
- `DbosExecutor` 会自动基于机器主机名与进程 ID 派生唯一的单实例标识,确保启动恢复时仅重新入队属于该实例的任务。
|
||||
- **自定义 Worker ID**:若需显式指定标识符,可设置 `SHUMAI_WORKERS_ID`(或 `SHUMAI_WORKER_ID`)。
|
||||
- **Kubernetes 环境**:在 Kubernetes Pod 中,可通过 Downward API(`fieldRef: { fieldPath: metadata.name }`)设置 `POD_NAME`,以在 Pod 重启期间保持稳定且唯一的单 Pod 身份标识。
|
||||
|
||||
使用智能体运行器时,每个数据库只允许运行一个主服务器。已配置运行器时,请先停止旧主服务器,再启动替换实例。在 Kubernetes 中,主服务器应使用单副本和 Recreate 部署策略。转码 Worker 仍可独立扩展。
|
||||
|
||||
---
|
||||
|
||||
## 基于 Kubernetes 与 AWS EKS 的弹性扩展部署
|
||||
|
||||
@@ -216,3 +216,23 @@ Shumai 提供三种安装方式。对于大多数管理员,推荐使用 **Dock
|
||||
bun run dev
|
||||
```
|
||||
命令执行后将在 `3000` 端口启动 Web 服务,并已开启热重载(Hot-Reloading)。
|
||||
|
||||
---
|
||||
|
||||
## 安装命令行客户端
|
||||
|
||||
若要连接到已运行的 Shumai 服务器,可直接安装独立 CLI,无需安装 Node.js 或 Bun:
|
||||
|
||||
```sh
|
||||
curl -fsSL https://github.com/shumaiOne/shumai/releases/latest/download/install.sh | sh
|
||||
```
|
||||
|
||||
Windows 用户可使用 PowerShell:
|
||||
|
||||
```powershell
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -Command "irm https://github.com/shumaiOne/shumai/releases/latest/download/install.ps1 | iex"
|
||||
```
|
||||
|
||||
独立版本支持 ARM64 和 x64 架构的 macOS、使用 glibc 的 Linux,以及 Windows。安装脚本会校验 SHA-256 校验和,并将 CLI 安装到当前用户的目录中。重新运行安装脚本即可升级,使用 `shumai-cli --version` 可查看已安装的版本。也可通过 `npm install -g @shumai-one/shumai-cli` 安装,此方式需要 Node.js 22.12 或更高版本。
|
||||
|
||||
将 `SHUMAI_API_SERVER` 设置为服务器地址,将 `SHUMAI_API_KEY` 设置为在“设置 → API 令牌”中创建的个人 API 令牌,然后运行 `shumai-cli project ls`。有关支持的系统版本、PATH 配置、指定版本安装、回滚及卸载的详细说明,请参阅 [CLI 指南](https://github.com/shumaiOne/shumai/blob/main/apps/cli/README.md)。
|
||||
|
||||
@@ -1,45 +1,99 @@
|
||||
---
|
||||
title: 智能体沙箱
|
||||
description: AI 智能体脚本的安全执行环境与网络域名限制规范。
|
||||
title: 智能体运行器
|
||||
description: 连接可选的命令运行器,并在运行器机器上配置沙箱。
|
||||
---
|
||||
|
||||
运行自动化脚本存在潜在的安全风险。Shumai 通过在安全且隔离的**沙箱(Sandbox)**环境中执行所有智能体技能来化解这一隐患。
|
||||
智能体无需运行器即可使用内置工具。如需执行命令或将素材下载到命令工作区,请在自己管理的机器上连接**运行器**。运行器主动通过经过身份验证的 WebSocket 连接 Shumai,无需开放入站端口。
|
||||
|
||||
---
|
||||
每个数据库只支持一台主 Shumai 服务器使用智能体运行器。未配置运行器时,多台主服务器可以共享数据库。创建运行器前,请停止其他主服务器副本。转码 Worker 可以独立运行。
|
||||
|
||||
## 沙箱安全机制
|
||||
## 连接运行器
|
||||
|
||||
沙箱严格限制执行进程访问宿主机文件系统、系统进程及内部网络:
|
||||
1. 在 **设置 → 智能体运行器** 中创建运行器,复制仅显示一次的密钥。只有团队所有者可以管理运行器和轮换密钥。
|
||||
2. 在 macOS、Linux 或 Windows 机器上安装 [Shumai CLI](https://github.com/shumaiOne/shumai/tree/main/apps/cli#installation)。
|
||||
3. 在已有的专用工作目录中启动:
|
||||
|
||||
- **环境隔离**:脚本在虚拟化的工作区容器中运行,无法篡改系统核心文件或读取其他项目数据。
|
||||
- **网络桥接管控**:来自沙箱内部的外部网络请求会受到严格监控。
|
||||
```bash
|
||||
export SHUMAI_API_SERVER="https://shumai.example.com"
|
||||
export SHUMAI_RUNNER_KEY="your-runner-key"
|
||||
shumai-cli runner start --cwd /path/to/workspace
|
||||
```
|
||||
|
||||
### 文件系统访问限制
|
||||
Windows 用户可在 PowerShell 中设置变量:
|
||||
|
||||
为保障工作区环境与敏感数据安全,沙箱实施了严密的文件级访问控制策略:
|
||||
```powershell
|
||||
$env:SHUMAI_API_SERVER = "https://shumai.example.com"
|
||||
$env:SHUMAI_RUNNER_KEY = "your-runner-key"
|
||||
shumai-cli runner start --cwd C:\workspace
|
||||
```
|
||||
|
||||
- **读取权限**:沙箱允许读取工作区内的普通文件,但严格禁止访问敏感环境配置与证书密钥文件。禁止读取的文件模式包括:`['.env', '.env.*', '*.pem', '*.key']`。
|
||||
- **写入权限**:沙箱的写入权限受到极为严苛的限制,仅允许写入当前工作目录(cwd)下的 `.pi` 文件夹。即便在该受信任的目录内部,沙箱也严禁写入或创建匹配以下模式的敏感文件:`['.env', '.env.*', '*.pem', '*.key']`。
|
||||
使用运行器密钥,而非个人 API 令牌。建议通过环境变量传入,避免 `--key` 将密钥留在命令历史中。运行器在前台执行,Ctrl+C 会停止运行器及其活动命令。临时断线后自动重连;密钥撤销或连接被替换时退出。
|
||||
|
||||
---
|
||||
第一个运行器是团队默认运行器,也可在智能体设置中指定专用运行器。未配置运行器时隐藏 `bash` 和 `download_asset`。配置后,具有智能体使用权限的编辑者和审阅者可立即使用 Bash,无需先读取技能;禁用工具设置仍然生效。运行器离线时执行命令会返回错误。
|
||||
|
||||
## 域名白名单
|
||||
命令从工作目录启动,技能、下载素材、生成文件和日志位于 `.pi` 中。子进程只继承基础系统环境及明确配置的技能变量,不自动继承运行器密钥、个人 API 令牌或其他任意环境变量。
|
||||
|
||||
默认情况下,网络沙箱处于**关闭**状态(允许所有出站网络请求)。团队所有者可在沙箱设置中开启网络沙箱,以强制推行严格的域名白名单管控:
|
||||
所有有权在同一运行器执行命令的用户都可以读取工作区中保留的文件,包括其他项目的素材和日志。Shumai 下载素材时会检查项目权限。请仅在有权读取工作区内所有文件的用户之间共享运行器。不同信任范围应使用独立的运行器账号或虚拟机,并限制对应智能体的访问权限。
|
||||
|
||||
### 允许的域名 (Allowed Domains)
|
||||
## 可选 SRT 沙箱
|
||||
|
||||
允许沙箱内脚本正常访问的域名清单。
|
||||
未使用 `--srt` 时,命令具有运行器系统用户的权限。只连接可信 Shumai 服务器,可按需使用独立容器、虚拟机或账号。
|
||||
|
||||
- 默认白名单包含标准软件包注册表(如 npm、Python 官方源等)以及代码托管平台(如 `github.com`)。
|
||||
- 若脚本需要调用外部 API(如 `api.slack.com`),管理员须预先将其添加至白名单中。
|
||||
[Anthropic Sandbox Runtime](https://github.com/anthropics/sandbox-runtime) 已作为库内置于 Shumai CLI。安装平台依赖(Linux:bubblewrap、socat 和 ripgrep;macOS:ripgrep)后传入 JSON 配置:
|
||||
|
||||
### 待审批域名 (Pending Domains)
|
||||
```bash
|
||||
shumai-cli runner start --cwd /path/to/workspace --srt sandbox.json
|
||||
```
|
||||
|
||||
当智能体脚本尝试访问未经授权的域名时:
|
||||
Windows 使用 SRT 的实验性 Windows 支持。启用沙箱前执行一次初始化;此命令会请求管理员权限:
|
||||
|
||||
1. 网络连接将被即刻拦截。
|
||||
2. 系统会记录目标域名并将其加入**待审批域名**列表。
|
||||
3. 管理员可审阅该列表,点击**批准**将其加白,或点击**拒绝**保持拦截。
|
||||
```powershell
|
||||
npx @anthropic-ai/sandbox-runtime windows-install
|
||||
```
|
||||
|
||||

|
||||
SRT 会在启动时检查安装状态,缺少初始化时返回错误。未使用 `--srt` 的运行器不需要此步骤。`bash` 工具在 macOS 和 Linux 上使用 `/bin/sh`。在 Windows 上,运行器先在标准安装目录查找 Git Bash,再在 PATH 中查找 `bash.exe`,找不到时使用 `cmd.exe`。运行器在启动时显示所选 Shell,并将其报告给智能体,让智能体使用匹配的命令语法。在 Windows 上搭配 SRT 使用 Bash 时,请用 Git Bash 代替旧版 WSL Bash 启动器。
|
||||
|
||||
直接使用上游 SRT JSON 格式,例如:
|
||||
|
||||
```json
|
||||
{
|
||||
"filesystem": {
|
||||
"denyRead": ["~/.ssh", "~/.aws", ".env", ".env.*"],
|
||||
"allowWrite": [".pi"],
|
||||
"denyWrite": []
|
||||
},
|
||||
"network": {
|
||||
"allowedDomains": [],
|
||||
"deniedDomains": []
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
此例只允许写入 `.pi`,禁止读取所列凭证位置,并禁止命令访问网络。请参考 SRT 文档审查适合自己环境的读取权限和域名规则。相对路径以运行器工作目录为基准。配置在启动时读取并保存快照,修改后需要重启。配置缺失或无效、SRT 依赖缺失、沙箱初始化失败都会阻止启动,不会回退到无沙箱执行。
|
||||
|
||||
SRT 只包裹 Shell 命令及其子进程。并发命令共享同一个 SRT 运行时,运行器停止时统一清理。运行器 WebSocket 和服务器发起的文件传输在 SRT 外运行。**不会自动将 Shumai 服务器地址添加到允许域名中。** 如果脚本自身需要网络,请在 JSON 中明确允许目标域名。
|
||||
|
||||
Shumai 服务器不管理沙箱和域名规则。只有启用沙箱的运行器才初始化 SRT。在 Docker 中使用 SRT 可能需要为专用运行器容器增加命名空间权限,Shumai 服务器不需要这些权限。
|
||||
|
||||
## Docker 运行器
|
||||
|
||||
普通运行器可在独立容器中执行,无需额外权限或暴露端口:
|
||||
|
||||
```bash
|
||||
docker run --rm --init --name shumai-runner \
|
||||
--cap-drop ALL --security-opt no-new-privileges \
|
||||
-e SHUMAI_API_SERVER -e SHUMAI_RUNNER_KEY \
|
||||
-v shumai-runner-workspace:/workspace -w /workspace \
|
||||
node:22-bookworm sh -c \
|
||||
'npm install -g @shumai-one/shumai-cli && exec shumai-cli runner start'
|
||||
```
|
||||
|
||||
本地开发允许 `localhost`、`127.0.0.1`、`[::1]`、Docker 的 `host.docker.internal` HTTP 地址;远程连接必须使用 HTTPS。不要将敏感宿主机目录或 Docker socket 挂载到运行器工作区。
|
||||
|
||||
## 限制与生命周期
|
||||
|
||||
默认超时 120 秒,最长一小时。超时或取消会尝试终止命令及其子进程。单条命令最多在 `.pi/logs` 保存 64 MiB 输出,请按工作区维护需求清理日志。文件传输和技能解压限制为 256 MiB。协议文件操作拒绝 `.pi` 外路径、已有符号链接及不安全的压缩包条目。V1 假定命令不会在文件传输期间故意修改工作区路径。
|
||||
|
||||
临时重连会同步执行中及近期完成的命令,避免主动重复执行。重启运行器进程不会恢复旧进程中的命令。
|
||||
|
||||
共享工作区最多同时执行八个命令或文件传输,超出时返回繁忙错误。Windows 未启用 SRT 时,后台进程可能在命令 Shell 退出后继续运行,剩余输出管道会在一秒后关闭。请使用前台命令,以确保超时和取消可预测。启用 SRT 时由 SRT 管理 Windows 沙箱进程树。
|
||||
|
||||
@@ -35,7 +35,7 @@ Shumai 官方 Premiere Pro UXP 插件在 Adobe Premiere Pro 内提供了原生
|
||||
|
||||
> [!NOTE]
|
||||
> **实验性功能:**
|
||||
> Premiere Pro UXP 插件当前处于实验阶段。功能虽然完整,但尚未覆盖所有 Premiere Pro 小版本与操作系统环境。若遇到 Bug、异常表现或有工作流建议,欢迎在 [GitHub 提交 Issue](https://github.com/shumaiOne/shumai/issues) 或加入我们的 [Discord 社区](https://discord.gg/sUzhujK6DD) 反馈交流。
|
||||
> Premiere Pro UXP 插件当前处于实验阶段。功能虽然完整,但尚未覆盖所有 Premiere Pro 小版本与操作系统环境。若遇到 Bug、异常表现或有工作流建议,欢迎在 [GitHub 提交 Issue](https://github.com/shumaiOne/shumai/issues) 或前往我们的 [Discourse 论坛](https://shumai.discourse.group/) 反馈交流。
|
||||
|
||||
### 最低版本要求
|
||||
|
||||
|
||||
+3
-5
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "shumai",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"repository": {
|
||||
@@ -28,10 +28,11 @@
|
||||
"db:migrate": "bun run prisma migrate dev",
|
||||
"db:deploy": "bun run prisma migrate deploy",
|
||||
"db:generate": "cd packages/db && bunx prisma generate",
|
||||
"worker:agent": "bun apps/agent/src/index.ts",
|
||||
"worker:transcode": "bun apps/transcode/src/index.ts",
|
||||
"build": "bun run i18n:compile && bun run build.ts",
|
||||
"build-npm": "bun run i18n:compile && bun scripts/build-npm.ts",
|
||||
"build:cli": "bun scripts/build-cli.ts",
|
||||
"test:cli-release": "bun scripts/test-cli-release.ts",
|
||||
"prepublishOnly": "bun run build-npm && bun run lint && bun run typecheck",
|
||||
"publish": "bun run prepublishOnly && bun run scripts/publish.ts",
|
||||
"publish:dry": "bun run prepublishOnly && bun run scripts/publish.ts --dry-run",
|
||||
@@ -67,8 +68,5 @@
|
||||
"typescript-eslint": "8.68.0",
|
||||
"vite-tsconfig-paths": "6.1.1",
|
||||
"vitest": "4.1.11"
|
||||
},
|
||||
"patchedDependencies": {
|
||||
"@earendil-works/pi-ai@0.82.1": "patches/@earendil-works%2Fpi-ai@0.82.1.patch"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
{
|
||||
"name": "@shumai/agent",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
"types": "./src/index.ts",
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sandbox-runtime": "0.0.74",
|
||||
"@earendil-works/pi-agent-core": "0.82.1",
|
||||
"@earendil-works/pi-ai": "0.82.1",
|
||||
"@earendil-works/chord": "1.5.0",
|
||||
"@earendil-works/pi-ai": "1.5.0",
|
||||
"@earendil-works/pi-durable": "1.5.0",
|
||||
"@google/genai": "2.28.0",
|
||||
"@shumai/core": "workspace:*",
|
||||
"@shumai/db": "workspace:*",
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,18 @@
|
||||
import { type AgentMessage, type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { BACKGROUND_CONTEXT as piContext } from '@earendil-works/chord/context'
|
||||
import type { AssistantMessage } from '@earendil-works/pi-ai'
|
||||
import { type ImageContent } from '@earendil-works/pi-ai'
|
||||
import type { ToolRegistration as AgentTool, JsonObject } from '@earendil-works/pi-durable'
|
||||
import { getAgentRequiredLevel, getRoleLevel } from '@shumai/core/src/agent/permissions'
|
||||
import { S3_PLACEHOLDER } from '@shumai/core/src/agent/session-content'
|
||||
import { agentSessionService } from '@shumai/core/src/agent/session-service'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { mcpService } from '@shumai/core/src/mcp/mcp-service'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { getAgentRequiredLevel, getRoleLevel } from '@shumai/core/src/agent/permissions'
|
||||
import { AssetType, prisma, Prisma, type Skill } from '@shumai/db'
|
||||
import { serializeContextToXml, ShumaiMessageEntry } from '@shumai/dtos'
|
||||
import {
|
||||
ApplicationFailure,
|
||||
defineStep,
|
||||
@@ -15,23 +20,18 @@ import {
|
||||
registerLocalCancelHandler,
|
||||
unregisterLocalCancelHandler,
|
||||
} from '@shumai/workflow-core'
|
||||
import { DatabaseSessionStorage } from '../database-session-storage'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { initializeCommentSession, synchronizeCommentContext } from '../comment-context'
|
||||
import {
|
||||
createAgentSession,
|
||||
fieldsToTypeBoxSchema,
|
||||
type AutofillField,
|
||||
type DbProviderInfo,
|
||||
} from '../index'
|
||||
} from '../runtime'
|
||||
|
||||
import { aiUsageService } from '@shumai/core/src/ai-usage/ai-usage'
|
||||
import { quotaService, QuotaExceededError } from '@shumai/core/src/quota/quota-service'
|
||||
import { QuotaExceededError, quotaService } from '@shumai/core/src/quota/quota-service'
|
||||
import { getProxyType } from '@shumai/core/src/utils/mime'
|
||||
import { secondToDegree } from '@shumai/core/src/utils/model3d'
|
||||
import {
|
||||
UpdateAssetMetadataRequest,
|
||||
type ShumaiMessageContext,
|
||||
type ShumaiMediaPosition,
|
||||
} from '@shumai/dtos'
|
||||
import { UpdateAssetMetadataRequest, type ShumaiMessageContext } from '@shumai/dtos'
|
||||
|
||||
export interface Usage {
|
||||
inputTokens: number
|
||||
@@ -66,7 +66,7 @@ export interface AgentExecutionContext {
|
||||
teamSkills: Skill[]
|
||||
mcpServers: Prisma.McpServerGetPayload<Record<string, never>>[]
|
||||
enabledSkillIds: string[]
|
||||
allowedDomains: string[]
|
||||
allowedDomains?: string[]
|
||||
}
|
||||
|
||||
export function formatProjectContextPrompt(
|
||||
@@ -97,7 +97,7 @@ async function executeAgentPrompt(params: {
|
||||
context: AgentExecutionContext
|
||||
attachedAssets?: Array<{ id: string; name: string; type: string }>
|
||||
}): Promise<{ text: string; usage: Usage; sessionId: string }> {
|
||||
const { agent, dbProviders, teamSkills, enabledSkillIds, allowedDomains } = params.context
|
||||
const { agent, dbProviders, teamSkills, enabledSkillIds } = params.context
|
||||
|
||||
if (!agent.provider) {
|
||||
throw ApplicationFailure.create({
|
||||
@@ -206,7 +206,30 @@ User messages may contain a <context> block detailing the user, active asset loc
|
||||
)
|
||||
: []
|
||||
|
||||
const { session, harness } = await createAgentSession({
|
||||
// A workflow retry must reopen the same autofill session before submitting its stable request ID.
|
||||
let persistedSessionId = params.sessionId
|
||||
if (!persistedSessionId && params.taskId) {
|
||||
persistedSessionId = await prisma.$transaction(async (tx) => {
|
||||
await tx.$queryRaw`SELECT id FROM workflow_tasks WHERE id = ${params.taskId} FOR UPDATE`
|
||||
const task = await tx.workflowTask.findUniqueOrThrow({ where: { id: params.taskId } })
|
||||
if (task.sessionId) return task.sessionId
|
||||
const record = await tx.agentSession.create({
|
||||
data: {
|
||||
agentId: params.agentId,
|
||||
userId: params.userId,
|
||||
cwd: process.cwd(),
|
||||
assetId: params.assetId,
|
||||
},
|
||||
})
|
||||
await tx.workflowTask.update({ where: { id: task.id }, data: { sessionId: record.id } })
|
||||
return record.id
|
||||
})
|
||||
}
|
||||
const {
|
||||
session,
|
||||
harness,
|
||||
sessionId: activeSessionId,
|
||||
} = await createAgentSession({
|
||||
teamId: params.teamId,
|
||||
agentId: params.agentId,
|
||||
providerName,
|
||||
@@ -219,8 +242,7 @@ User messages may contain a <context> block detailing the user, active asset loc
|
||||
description: s.description,
|
||||
})),
|
||||
enabledSkillIds,
|
||||
allowedDomains,
|
||||
sessionId: params.sessionId,
|
||||
sessionId: persistedSessionId,
|
||||
userId: params.userId,
|
||||
projectId: params.projectId,
|
||||
assetId: params.assetId,
|
||||
@@ -237,225 +259,155 @@ User messages may contain a <context> block detailing the user, active asset loc
|
||||
})),
|
||||
})
|
||||
|
||||
const imagesToPass: ImageContent[] = []
|
||||
|
||||
if (params.images && params.images.length > 0) {
|
||||
for (const key of params.images) {
|
||||
const { buffer, contentType } = await s3Service.getObject(
|
||||
process.env.S3_BUCKET || 'shumai',
|
||||
key,
|
||||
)
|
||||
imagesToPass.push({
|
||||
type: 'image',
|
||||
data: buffer.toString('base64'),
|
||||
mimeType: contentType,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const onAbort = () => {
|
||||
harness.abort()
|
||||
void session.abort(piContext).catch((err) => logger.error({ err }, 'Failed to stop agent'))
|
||||
}
|
||||
|
||||
const abortSignal = getLocalTaskAbortSignal(params.taskId)
|
||||
if (abortSignal) {
|
||||
if (abortSignal.aborted) {
|
||||
harness.abort()
|
||||
} else {
|
||||
abortSignal.addEventListener('abort', onAbort, { once: true })
|
||||
}
|
||||
} else if (params.taskId) {
|
||||
registerLocalCancelHandler(params.taskId, () => {
|
||||
harness.abort()
|
||||
})
|
||||
}
|
||||
|
||||
if (abortSignal) abortSignal.addEventListener('abort', onAbort, { once: true })
|
||||
else if (params.taskId) registerLocalCancelHandler(params.taskId, onAbort)
|
||||
try {
|
||||
const storage = session.getStorage()
|
||||
if (storage instanceof DatabaseSessionStorage && params.messageContext) {
|
||||
storage.currentMessageContext = params.messageContext
|
||||
}
|
||||
|
||||
let totalInputTokens = 0
|
||||
let totalOutputTokens = 0
|
||||
let totalCacheReadTokens = 0
|
||||
let grandTotalTokens = 0
|
||||
let totalCost = 0
|
||||
|
||||
if (abortSignal?.aborted)
|
||||
throw ApplicationFailure.create({ message: 'Agent execution stopped.', nonRetryable: true })
|
||||
const effectiveRole = params.userId
|
||||
? await resolveEffectiveRole(params.teamId, params.projectId, params.userId)
|
||||
: undefined
|
||||
|
||||
try {
|
||||
await quotaService.checkQuota(
|
||||
{
|
||||
teamId: params.teamId,
|
||||
userId: params.userId,
|
||||
role: effectiveRole,
|
||||
resource: 'agent_total_tokens',
|
||||
},
|
||||
0,
|
||||
)
|
||||
await quotaService.checkQuota(
|
||||
{
|
||||
teamId: params.teamId,
|
||||
userId: params.userId,
|
||||
role: effectiveRole,
|
||||
resource: 'agent_cost',
|
||||
},
|
||||
0,
|
||||
)
|
||||
} catch (err) {
|
||||
if (err instanceof QuotaExceededError) {
|
||||
throw ApplicationFailure.create({
|
||||
message: err.message,
|
||||
nonRetryable: true,
|
||||
})
|
||||
for (const resource of ['agent_total_tokens', 'agent_cost'] as const) {
|
||||
await quotaService.checkQuota(
|
||||
{ teamId: params.teamId, userId: params.userId, role: effectiveRole, resource },
|
||||
0,
|
||||
)
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof QuotaExceededError)
|
||||
throw ApplicationFailure.create({ message: err.message, nonRetryable: true })
|
||||
throw err
|
||||
}
|
||||
|
||||
harness.subscribe(async (event) => {
|
||||
if (event.type === 'message_end' && event.message.role === 'assistant') {
|
||||
const assistantMsg = event.message
|
||||
const u = assistantMsg.usage
|
||||
if (u) {
|
||||
const inputTokens = u.input || 0
|
||||
const outputTokens = u.output || 0
|
||||
const cacheReadTokens = u.cacheRead || 0
|
||||
const totalTokens = u.totalTokens || inputTokens + outputTokens
|
||||
const cost = u.cost?.total || 0
|
||||
|
||||
totalInputTokens += inputTokens
|
||||
totalOutputTokens += outputTokens
|
||||
totalCacheReadTokens += cacheReadTokens
|
||||
grandTotalTokens += totalTokens
|
||||
totalCost += cost
|
||||
|
||||
try {
|
||||
await aiUsageService.recordUsage({
|
||||
teamId: params.teamId,
|
||||
userId: params.userId,
|
||||
inputTokens,
|
||||
outputTokens,
|
||||
cacheReadTokens,
|
||||
totalTokens,
|
||||
cost,
|
||||
})
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'Failed to record AI usage')
|
||||
}
|
||||
|
||||
try {
|
||||
await quotaService.consumeQuota(
|
||||
{
|
||||
teamId: params.teamId,
|
||||
userId: params.userId,
|
||||
role: effectiveRole,
|
||||
resource: 'agent_total_tokens',
|
||||
},
|
||||
totalTokens,
|
||||
)
|
||||
await quotaService.consumeQuota(
|
||||
{
|
||||
teamId: params.teamId,
|
||||
userId: params.userId,
|
||||
role: effectiveRole,
|
||||
resource: 'agent_cost',
|
||||
},
|
||||
cost,
|
||||
)
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'Failed to record quota usage for AI call')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const assistantMessage = await harness.prompt(params.prompt, { images: imagesToPass })
|
||||
|
||||
if (totalInputTokens === 0 && totalOutputTokens === 0 && assistantMessage.usage) {
|
||||
totalInputTokens = assistantMessage.usage.input || 0
|
||||
totalOutputTokens = assistantMessage.usage.output || 0
|
||||
totalCacheReadTokens = assistantMessage.usage.cacheRead || 0
|
||||
grandTotalTokens = assistantMessage.usage.totalTokens || totalInputTokens + totalOutputTokens
|
||||
totalCost = assistantMessage.usage.cost?.total || 0
|
||||
|
||||
try {
|
||||
await quotaService.consumeQuota(
|
||||
{
|
||||
teamId: params.teamId,
|
||||
userId: params.userId,
|
||||
role: effectiveRole,
|
||||
resource: 'agent_total_tokens',
|
||||
},
|
||||
grandTotalTokens,
|
||||
)
|
||||
await quotaService.consumeQuota(
|
||||
{
|
||||
teamId: params.teamId,
|
||||
userId: params.userId,
|
||||
role: effectiveRole,
|
||||
resource: 'agent_cost',
|
||||
},
|
||||
totalCost,
|
||||
)
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'Failed to record fallback quota usage for AI call')
|
||||
}
|
||||
}
|
||||
|
||||
const sessionEntries = await session.getEntries()
|
||||
sessionEntries.forEach((entry) => {
|
||||
if (entry.type === 'message' && entry.message) {
|
||||
const msg = entry.message
|
||||
if (msg.role === 'toolResult') {
|
||||
const logMsg = { ...msg, content: undefined }
|
||||
logger.debug(logMsg, 'agent message')
|
||||
} else {
|
||||
logger.debug(msg, 'agent message')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
let text = assistantMessage.content
|
||||
.filter((c) => c.type === 'text')
|
||||
.map((c) => {
|
||||
if ('text' in c && typeof c.text === 'string') {
|
||||
return c.text
|
||||
}
|
||||
return ''
|
||||
if (params.userCommentId) {
|
||||
await synchronizeCommentContext({
|
||||
conversation: session,
|
||||
sessionId: activeSessionId,
|
||||
teamId: params.teamId,
|
||||
agentId: params.agentId,
|
||||
userCommentId: params.userCommentId,
|
||||
})
|
||||
.join('\n')
|
||||
|
||||
if (assistantMessage.stopReason === 'error' && assistantMessage.errorMessage) {
|
||||
if (text) {
|
||||
text += '\n\n'
|
||||
}
|
||||
text += `Error: ${assistantMessage.errorMessage}`
|
||||
}
|
||||
|
||||
const requestId = params.taskId || params.userCommentId || randomUUID()
|
||||
const context = {
|
||||
...params.messageContext,
|
||||
id: params.userCommentId || `chat:${activeSessionId}:${requestId}`,
|
||||
}
|
||||
const xml = serializeContextToXml(context)
|
||||
const images: ImageContent[] = []
|
||||
for (const key of params.images) {
|
||||
const { contentType } = await s3Service.getObject(process.env.S3_BUCKET || 'shumai', key)
|
||||
images.push({ type: 'image', data: S3_PLACEHOLDER, mimeType: contentType })
|
||||
}
|
||||
const submission = await session.submit(
|
||||
{
|
||||
type: 'input',
|
||||
requestId,
|
||||
content: [
|
||||
{ type: 'text', text: xml ? `${params.prompt}\n\n${xml}` : params.prompt },
|
||||
...images,
|
||||
],
|
||||
entry: {
|
||||
kind: ShumaiMessageEntry.kind,
|
||||
data: JSON.parse(
|
||||
JSON.stringify({
|
||||
displayText: params.prompt,
|
||||
timestamp: Date.now(),
|
||||
context,
|
||||
sourceCommentId: params.userCommentId,
|
||||
sourceKeys: params.images,
|
||||
}),
|
||||
) as JsonObject,
|
||||
},
|
||||
},
|
||||
piContext,
|
||||
)
|
||||
const settled = await submission.wait(piContext)
|
||||
if (settled.status !== 'done' || settled.type !== 'input') {
|
||||
let detail = ''
|
||||
const recent = await session.entries?.({ order: 'descending' }, 20, undefined, piContext)
|
||||
for (const entry of recent?.items ?? []) {
|
||||
const failure = entry.model?.find(
|
||||
(message) => message.role === 'assistant' && message.errorMessage,
|
||||
)
|
||||
if (failure?.role === 'assistant') {
|
||||
detail = `: ${failure.errorMessage}`
|
||||
break
|
||||
}
|
||||
}
|
||||
throw ApplicationFailure.create({
|
||||
message: `Agent run ended without an answer: ${settled.status === 'unanswered' ? settled.reason : settled.status}${detail}`,
|
||||
nonRetryable: true,
|
||||
})
|
||||
}
|
||||
const answer = await session.commit((tx) => tx.entry(settled.answer), piContext)
|
||||
const assistantMessage = answer?.model?.find(
|
||||
(message): message is AssistantMessage => message.role === 'assistant',
|
||||
)
|
||||
if (!assistantMessage) throw new Error('Agent answer is missing')
|
||||
if (assistantMessage.stopReason === 'error')
|
||||
throw ApplicationFailure.create({
|
||||
message: assistantMessage.errorMessage || 'Agent generation failed',
|
||||
nonRetryable: true,
|
||||
})
|
||||
const totals = await harness.usage(piContext)
|
||||
const usage: Usage = {
|
||||
model: modelId,
|
||||
inputTokens: totalInputTokens,
|
||||
outputTokens: totalOutputTokens,
|
||||
cacheReadTokens: totalCacheReadTokens,
|
||||
totalTokens: grandTotalTokens,
|
||||
cost: totalCost,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
totalTokens: 0,
|
||||
cost: 0,
|
||||
}
|
||||
|
||||
let sessionId = ''
|
||||
if (storage instanceof DatabaseSessionStorage) {
|
||||
sessionId = storage.sessionId
|
||||
for (const value of Object.values(totals.models)) {
|
||||
usage.inputTokens += value.input
|
||||
usage.outputTokens += value.output
|
||||
usage.cacheReadTokens += value.cacheRead
|
||||
usage.totalTokens += value.totalTokens
|
||||
usage.cost += value.cost.total
|
||||
}
|
||||
return { text, usage, sessionId }
|
||||
// Accounting is committed with the native usage ledger; workflow reporting uses this request's entries.
|
||||
if (session.entries) {
|
||||
Object.assign(usage, {
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
totalTokens: 0,
|
||||
cost: 0,
|
||||
})
|
||||
let cursor: import('@earendil-works/pi-durable').Cursor | undefined
|
||||
do {
|
||||
const page = await session.entries(
|
||||
{ order: 'ascending', minEntryId: settled.entry, maxEntryId: settled.answer },
|
||||
100,
|
||||
cursor,
|
||||
piContext,
|
||||
)
|
||||
for (const entry of page.items)
|
||||
for (const message of entry.model ?? [])
|
||||
if (message.role === 'assistant') {
|
||||
usage.inputTokens += message.usage.input
|
||||
usage.outputTokens += message.usage.output
|
||||
usage.cacheReadTokens += message.usage.cacheRead
|
||||
usage.totalTokens += message.usage.totalTokens
|
||||
usage.cost += message.usage.cost.total
|
||||
}
|
||||
cursor = page.next
|
||||
} while (cursor)
|
||||
}
|
||||
const text = assistantMessage.content
|
||||
.filter((block) => block.type === 'text')
|
||||
.map((block) => block.text)
|
||||
.join('\n')
|
||||
return { text, usage, sessionId: activeSessionId }
|
||||
} finally {
|
||||
if (abortSignal) {
|
||||
abortSignal.removeEventListener('abort', onAbort)
|
||||
}
|
||||
if (params.taskId) {
|
||||
unregisterLocalCancelHandler(params.taskId)
|
||||
}
|
||||
abortSignal?.removeEventListener('abort', onAbort)
|
||||
if (params.taskId) unregisterLocalCancelHandler(params.taskId)
|
||||
await harness.close(piContext)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -522,6 +474,7 @@ export const agentChatActivity = defineStep(async function agentChatActivity(
|
||||
})
|
||||
|
||||
export interface AutofillAiParams {
|
||||
taskId?: string
|
||||
teamId: string
|
||||
images?: string[]
|
||||
fields: AutofillField[]
|
||||
@@ -542,14 +495,13 @@ export const autofillAiActivity = defineStep(async function autofillAiActivity(
|
||||
|
||||
const autofillTool: AgentTool = {
|
||||
name: 'autofill_metadata',
|
||||
label: 'Autofill Metadata',
|
||||
description: 'Provide the extracted metadata once you have analyzed the asset content.',
|
||||
parameters: toolSchema,
|
||||
execute: async (_toolCallId, toolParams) => {
|
||||
execute: async (toolParams) => {
|
||||
capturedData = toolParams as Record<string, unknown>
|
||||
return {
|
||||
content: [{ type: 'text', text: 'Metadata captured successfully.' }],
|
||||
details: {},
|
||||
output: [{ type: 'text', text: 'Metadata captured successfully.' }],
|
||||
details: { metadata: toolParams as JsonObject },
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -592,6 +544,7 @@ export const autofillAiActivity = defineStep(async function autofillAiActivity(
|
||||
const { agent } = params.context
|
||||
|
||||
const { usage, sessionId } = await executeAgentPrompt({
|
||||
taskId: params.taskId,
|
||||
teamId: params.teamId,
|
||||
agentId: agent.id,
|
||||
prompt: fullPrompt,
|
||||
@@ -604,6 +557,24 @@ export const autofillAiActivity = defineStep(async function autofillAiActivity(
|
||||
context: params.context,
|
||||
})
|
||||
|
||||
if (!capturedData) {
|
||||
for (const entry of await agentSessionService.getEntries(sessionId))
|
||||
for (const message of entry.model ?? []) {
|
||||
if (
|
||||
message.role === 'toolResult' &&
|
||||
message.toolName === 'autofill_metadata' &&
|
||||
!message.isError &&
|
||||
message.details &&
|
||||
typeof message.details === 'object' &&
|
||||
!Array.isArray(message.details) &&
|
||||
'metadata' in message.details &&
|
||||
message.details.metadata &&
|
||||
typeof message.details.metadata === 'object' &&
|
||||
!Array.isArray(message.details.metadata)
|
||||
)
|
||||
capturedData = Object.fromEntries(Object.entries(message.details.metadata))
|
||||
}
|
||||
}
|
||||
return {
|
||||
text: capturedData ? JSON.stringify(capturedData) : '{}',
|
||||
usage,
|
||||
@@ -611,355 +582,9 @@ export const autofillAiActivity = defineStep(async function autofillAiActivity(
|
||||
}
|
||||
})
|
||||
|
||||
export const initializeAgentSessionActivity = defineStep(
|
||||
async function initializeAgentSessionActivity(params: {
|
||||
teamId: string
|
||||
agentId: string
|
||||
userCommentId: string
|
||||
userId?: string
|
||||
}): Promise<string> {
|
||||
const agentId = params.agentId === 'default' ? 'default' : params.agentId
|
||||
|
||||
// Ensure user exists for agent
|
||||
const userExists = await prisma.user.findUnique({ where: { id: agentId } })
|
||||
if (!userExists) {
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: agentId,
|
||||
name: 'Ai Agent',
|
||||
email: `${agentId}@shumai.ai`,
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Ensure agent exists
|
||||
const agentExists = await prisma.agent.findUnique({ where: { id: agentId } })
|
||||
if (!agentExists) {
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: agentId,
|
||||
teamId: params.teamId,
|
||||
type: 'chat',
|
||||
config: {
|
||||
provider: 'openai',
|
||||
model: 'gpt-4',
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const userComment = await prisma.assetComment.findUnique({
|
||||
where: { id: params.userCommentId },
|
||||
include: { creator: true },
|
||||
})
|
||||
|
||||
if (!userComment) {
|
||||
throw new Error(`User comment ${params.userCommentId} not found`)
|
||||
}
|
||||
|
||||
const rootCommentId = userComment.replyToId || userComment.id
|
||||
const targetUserCommentId = rootCommentId
|
||||
|
||||
let session = await prisma.agentSession.findFirst({
|
||||
where: {
|
||||
assetId: userComment.assetId,
|
||||
type: 'comment',
|
||||
userCommentId: targetUserCommentId,
|
||||
},
|
||||
})
|
||||
|
||||
if (!session) {
|
||||
session = await prisma.agentSession.create({
|
||||
data: {
|
||||
agentId,
|
||||
userId: params.userId || null,
|
||||
cwd: process.cwd(),
|
||||
assetId: userComment.assetId,
|
||||
userCommentId: targetUserCommentId,
|
||||
type: 'comment',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const rootComment = await prisma.assetComment.findUnique({
|
||||
where: { id: rootCommentId },
|
||||
include: { creator: true },
|
||||
})
|
||||
if (!rootComment) {
|
||||
throw new Error(`Root comment ${rootCommentId} not found`)
|
||||
}
|
||||
|
||||
// Ensure preceding top-level comments up to rootCommentId are synced in Main Session first
|
||||
let mainSession = await prisma.agentSession.findFirst({
|
||||
where: {
|
||||
assetId: userComment.assetId,
|
||||
type: 'comment',
|
||||
userCommentId: null,
|
||||
},
|
||||
})
|
||||
if (!mainSession) {
|
||||
mainSession = await prisma.agentSession.create({
|
||||
data: {
|
||||
agentId,
|
||||
userId: params.userId || null,
|
||||
cwd: process.cwd(),
|
||||
assetId: userComment.assetId,
|
||||
userCommentId: null,
|
||||
type: 'comment',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// 1. Fetch top-level comments up to rootComment.createdAt sorted by id
|
||||
const topLevelComments = await prisma.assetComment.findMany({
|
||||
where: {
|
||||
assetId: userComment.assetId,
|
||||
replyToId: null,
|
||||
createdAt: { lte: rootComment.createdAt },
|
||||
},
|
||||
orderBy: { id: 'asc' },
|
||||
include: {
|
||||
creator: true,
|
||||
attachments: { include: { asset: true } },
|
||||
},
|
||||
})
|
||||
|
||||
// 2. Fetch thread replies up to userComment.createdAt excluding userComment itself
|
||||
const threadReplies = userComment.replyToId
|
||||
? await prisma.assetComment.findMany({
|
||||
where: {
|
||||
replyToId: rootCommentId,
|
||||
createdAt: { lte: userComment.createdAt },
|
||||
id: { not: userComment.id },
|
||||
},
|
||||
orderBy: { id: 'asc' },
|
||||
include: {
|
||||
creator: true,
|
||||
attachments: { include: { asset: true } },
|
||||
},
|
||||
})
|
||||
: []
|
||||
|
||||
const allCommentsToSync = [...topLevelComments, ...threadReplies]
|
||||
|
||||
// Resolve user mentions from IDs to names and roles
|
||||
const mentionRegex = /<@([^>]+)>/g
|
||||
const mentionedUserIds = new Set<string>()
|
||||
for (const c of allCommentsToSync) {
|
||||
if (c.message) {
|
||||
const matches = [...c.message.matchAll(mentionRegex)]
|
||||
for (const match of matches) {
|
||||
mentionedUserIds.add(match[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const userIdToNameMap = new Map<string, string>()
|
||||
if (mentionedUserIds.size > 0) {
|
||||
const resolvedUsers = await prisma.user.findMany({
|
||||
where: { id: { in: Array.from(mentionedUserIds) } },
|
||||
select: { id: true, name: true },
|
||||
})
|
||||
for (const u of resolvedUsers) {
|
||||
if (u.name) userIdToNameMap.set(u.id, u.name)
|
||||
}
|
||||
}
|
||||
|
||||
const userRoleMap = new Map<string, string>()
|
||||
const humanCreatorIds = Array.from(
|
||||
new Set(
|
||||
allCommentsToSync
|
||||
.filter((c) => c.creator?.type !== 'agent' && c.creatorId)
|
||||
.map((c) => c.creatorId!),
|
||||
),
|
||||
)
|
||||
if (humanCreatorIds.length > 0) {
|
||||
const members = await prisma.teamMember.findMany({
|
||||
where: {
|
||||
teamId: params.teamId,
|
||||
userId: { in: humanCreatorIds },
|
||||
},
|
||||
select: { userId: true, role: true },
|
||||
})
|
||||
for (const m of members) {
|
||||
userRoleMap.set(m.userId, m.role)
|
||||
}
|
||||
}
|
||||
|
||||
const assetRecord = await prisma.asset.findUnique({
|
||||
where: { id: userComment.assetId },
|
||||
select: { media: true },
|
||||
})
|
||||
const proxyType = (assetRecord?.media as PrismaJson.MediaInfo | null)?.proxyType
|
||||
|
||||
function buildCommentEntry(c: (typeof allCommentsToSync)[0]) {
|
||||
const isAgent = c.creator?.type === 'agent' || c.sessionId !== null
|
||||
let messageContent = c.message || ''
|
||||
|
||||
messageContent = messageContent.replace(mentionRegex, (match, userId) => {
|
||||
const resolvedName = userIdToNameMap.get(userId)
|
||||
return resolvedName ? `<@${resolvedName}>` : match
|
||||
})
|
||||
|
||||
if (isAgent) {
|
||||
const message: AgentMessage = {
|
||||
role: 'assistant',
|
||||
content: [{ type: 'text', text: messageContent }],
|
||||
api: 'custom',
|
||||
provider: 'shumai',
|
||||
model: 'agent',
|
||||
usage: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
totalTokens: 0,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
||||
},
|
||||
stopReason: 'stop',
|
||||
timestamp: c.createdAt.getTime(),
|
||||
}
|
||||
return {
|
||||
type: 'message' as const,
|
||||
data: { message },
|
||||
}
|
||||
}
|
||||
|
||||
let userObj: ShumaiMessageContext['user'] = undefined
|
||||
if (c.creator) {
|
||||
const role = (c.creatorId ? userRoleMap.get(c.creatorId) : undefined) || 'user'
|
||||
userObj = {
|
||||
id: c.creator.id,
|
||||
name: c.creator.name || 'Unknown',
|
||||
role,
|
||||
}
|
||||
}
|
||||
|
||||
let position: ShumaiMediaPosition | undefined = undefined
|
||||
if (c.second !== null && c.second !== undefined) {
|
||||
if (proxyType === '3d') {
|
||||
position = { type: 'degree', degree: secondToDegree(c.second) }
|
||||
} else if (proxyType === 'pdf') {
|
||||
position = { type: 'page', page: Math.round(c.second) }
|
||||
} else {
|
||||
position = { type: 'time', seconds: c.second }
|
||||
}
|
||||
}
|
||||
|
||||
const annotation = !!(c.annotation && Array.isArray(c.annotation) && c.annotation.length > 0)
|
||||
|
||||
const attachedFiles: ShumaiMessageContext['attachedFiles'] = []
|
||||
if (c.attachments && c.attachments.length > 0) {
|
||||
for (const att of c.attachments) {
|
||||
if (att.asset) {
|
||||
const attProxyType = (att.asset.media as PrismaJson.MediaInfo | null)?.proxyType
|
||||
attachedFiles.push({
|
||||
id: att.asset.id,
|
||||
name: att.asset.name,
|
||||
type: att.asset.type,
|
||||
mediaType: attProxyType as
|
||||
'image' | 'video' | 'pdf' | 'audio' | '3d' | 'other' | undefined,
|
||||
mimeType: att.asset.mediaType || undefined,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const commentDetails: ShumaiMessageContext = {
|
||||
id: c.id,
|
||||
...(userObj ? { user: userObj } : {}),
|
||||
...(position ? { position } : {}),
|
||||
...(annotation
|
||||
? {
|
||||
annotation: true,
|
||||
annotations: c.annotation as unknown as ShumaiMessageContext['annotations'],
|
||||
}
|
||||
: {}),
|
||||
...(attachedFiles.length > 0 ? { attachedFiles } : {}),
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'custom_message' as const,
|
||||
data: {
|
||||
customType: 'shumai_message',
|
||||
content: messageContent,
|
||||
display: true,
|
||||
details: commentDetails,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Sync top-level comments into agent_session_entries (DAG spine)
|
||||
let mainPrevId: string | null = null
|
||||
for (const c of topLevelComments) {
|
||||
const existingEntry = await prisma.agentSessionEntry.findUnique({
|
||||
where: { id: c.id },
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
if (!existingEntry) {
|
||||
const entryData = buildCommentEntry(c)
|
||||
await prisma.agentSessionEntry.create({
|
||||
data: {
|
||||
id: c.id,
|
||||
sessionId: mainSession.id,
|
||||
assetId: userComment.assetId,
|
||||
type: entryData.type,
|
||||
parentId: mainPrevId,
|
||||
createdAt: c.createdAt,
|
||||
data: entryData.data as PrismaJson.PiSessionEntryData,
|
||||
},
|
||||
})
|
||||
}
|
||||
mainPrevId = c.id
|
||||
}
|
||||
|
||||
if (mainPrevId) {
|
||||
await prisma.agentSession.update({
|
||||
where: { id: mainSession.id },
|
||||
data: { leafId: mainPrevId },
|
||||
})
|
||||
}
|
||||
|
||||
// 4. Sync thread replies and set Thread Session leafId
|
||||
let threadLeafId: string = rootCommentId
|
||||
|
||||
if (threadReplies.length > 0) {
|
||||
let lastReplyParentId: string = rootCommentId
|
||||
for (const reply of threadReplies) {
|
||||
const existingReplyEntry = await prisma.agentSessionEntry.findUnique({
|
||||
where: { id: reply.id },
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
if (!existingReplyEntry) {
|
||||
const entryData = buildCommentEntry(reply)
|
||||
await prisma.agentSessionEntry.create({
|
||||
data: {
|
||||
id: reply.id,
|
||||
sessionId: session.id,
|
||||
assetId: userComment.assetId,
|
||||
type: entryData.type,
|
||||
parentId: lastReplyParentId,
|
||||
createdAt: reply.createdAt,
|
||||
data: entryData.data as PrismaJson.PiSessionEntryData,
|
||||
},
|
||||
})
|
||||
}
|
||||
lastReplyParentId = reply.id
|
||||
}
|
||||
threadLeafId = lastReplyParentId
|
||||
}
|
||||
|
||||
await prisma.agentSession.update({
|
||||
where: { id: session.id },
|
||||
data: { leafId: threadLeafId },
|
||||
})
|
||||
|
||||
return session.id
|
||||
},
|
||||
)
|
||||
export const initializeAgentSessionActivity = defineStep(initializeCommentSession, {
|
||||
name: 'initializeAgentSessionActivity',
|
||||
})
|
||||
|
||||
export const deleteCommentActivity = defineStep(async function deleteCommentActivity(
|
||||
commentId: string,
|
||||
@@ -1167,18 +792,12 @@ export const getAgentChatContextActivity = defineStep(
|
||||
})
|
||||
const mcpServers = assignments.map((a) => a.mcpServer)
|
||||
|
||||
const sandbox = await prisma.sandbox.findUnique({
|
||||
where: { teamId: params.teamId },
|
||||
})
|
||||
const allowedDomains = sandbox?.networkSandboxEnabled ? sandbox.allowedDomains : ['*']
|
||||
|
||||
return {
|
||||
agent,
|
||||
dbProviders,
|
||||
teamSkills: permittedSkills,
|
||||
mcpServers,
|
||||
enabledSkillIds,
|
||||
allowedDomains,
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -1256,18 +875,12 @@ export const getAgentAutofillContextActivity = defineStep(
|
||||
// Autofill agents currently do not use MCP tools.
|
||||
const mcpServers: Prisma.McpServerGetPayload<Record<string, never>>[] = []
|
||||
|
||||
const sandbox = await prisma.sandbox.findUnique({
|
||||
where: { teamId: params.teamId },
|
||||
})
|
||||
const allowedDomains = sandbox?.networkSandboxEnabled ? sandbox.allowedDomains : ['*']
|
||||
|
||||
return {
|
||||
agent: agentWithDetails,
|
||||
dbProviders,
|
||||
teamSkills: enabledSkills,
|
||||
mcpServers,
|
||||
enabledSkillIds,
|
||||
allowedDomains,
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -1499,7 +1112,23 @@ export const generateSessionNameActivity = defineStep(async function generateSes
|
||||
})
|
||||
|
||||
const promptToTitle = `Generate a short 2 to 4 word title for a session starting with this message:\n\n<user_message>\n${prompt}\n</user_message>`
|
||||
const result = await harness.prompt(promptToTitle)
|
||||
let result: AssistantMessage
|
||||
try {
|
||||
const conversation = await harness.root(piContext)
|
||||
const settled = await (
|
||||
await conversation.submit(
|
||||
{ type: 'input', content: promptToTitle, requestId: 'title' },
|
||||
piContext,
|
||||
)
|
||||
).wait(piContext)
|
||||
if (settled.status !== 'done' || settled.type !== 'input') return
|
||||
const entry = await conversation.commit((tx) => tx.entry(settled.answer), piContext)
|
||||
const message = entry?.model?.find((m): m is AssistantMessage => m.role === 'assistant')
|
||||
if (!message) return
|
||||
result = message
|
||||
} finally {
|
||||
await harness.close(piContext)
|
||||
}
|
||||
|
||||
if (result.stopReason === 'error' || result.stopReason === 'aborted' || result.errorMessage) {
|
||||
logger.error(
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import { BACKGROUND_CONTEXT } from '@earendil-works/chord/context'
|
||||
import { createModels } from '@earendil-works/pi-ai'
|
||||
import { Harness, createRegistry } from '@earendil-works/pi-durable'
|
||||
import { agentSessionService } from '@shumai/core/src/agent/session-service'
|
||||
import { resolveAnnotationsById } from '@shumai/core/src/asset-read/annotations'
|
||||
import { degreeToSecond } from '@shumai/core/src/utils/model3d'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { resolveAnnotationsById } from '@shumai/core/src/asset-read/annotations'
|
||||
import { DatabaseSessionStorage } from './database-session-storage'
|
||||
import type { SessionTreeEntry } from '@earendil-works/pi-agent-core'
|
||||
import { degreeToSecond } from '@shumai/core/src/utils/model3d'
|
||||
import type { ShumaiMessageContext } from '@shumai/dtos'
|
||||
import { beforeEach, describe, expect, it } from 'vitest'
|
||||
|
||||
describe('resolveAnnotationsById', () => {
|
||||
setupTestDbHooks()
|
||||
@@ -84,10 +87,10 @@ describe('resolveAnnotationsById', () => {
|
||||
data: { name: 'Project', teamId: team.id },
|
||||
})
|
||||
const assetA = await prisma.asset.create({
|
||||
data: { name: 'videoA.mp4', type: 'file', projectId: project.id, status: 'uploaded' },
|
||||
data: { name: 'videoA.mp4', type: 'file', status: 'uploaded', projectId: project.id },
|
||||
})
|
||||
const assetB = await prisma.asset.create({
|
||||
data: { name: 'videoB.mp4', type: 'file', projectId: project.id, status: 'uploaded' },
|
||||
data: { name: 'videoB.mp4', type: 'file', status: 'uploaded', projectId: project.id },
|
||||
})
|
||||
|
||||
const commentB = await prisma.assetComment.create({
|
||||
@@ -113,236 +116,94 @@ describe('resolveAnnotationsById', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('resolves annotations and timestamp from AgentSessionEntry (1-on-1 Chat Mode)', async () => {
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Chat User', email: 'chatuser@test.com', password: 'pw' },
|
||||
})
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Chat Team' },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Chat Project', teamId: team.id },
|
||||
})
|
||||
async function fixture() {
|
||||
const team = await prisma.team.create({ data: { name: 'Native annotation team' } })
|
||||
const project = await prisma.project.create({ data: { name: 'Project', teamId: team.id } })
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'chat-video.mp4',
|
||||
type: 'file',
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
data: { name: 'video.mp4', type: 'file', status: 'uploaded', projectId: project.id },
|
||||
})
|
||||
const agentUser = await prisma.user.create({
|
||||
data: { name: 'Chat Bot User', email: 'bot@example.com', type: 'agent' },
|
||||
const other = await prisma.asset.create({
|
||||
data: { name: 'other.mp4', type: 'file', status: 'uploaded', projectId: project.id },
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Agent', email: 'native@test.com', type: 'agent' },
|
||||
})
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: agentUser.id,
|
||||
id: user.id,
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'test-provider', model: 'test-model' },
|
||||
config: { provider: 'faux', model: 'faux-1' },
|
||||
},
|
||||
})
|
||||
const session = await prisma.agentSession.create({ data: { agentId: agent.id, cwd: '/test' } })
|
||||
return { session, asset, other }
|
||||
}
|
||||
|
||||
// Create session storage
|
||||
const storage = await DatabaseSessionStorage.create({
|
||||
agentId: agent.id,
|
||||
userId: user.id,
|
||||
assetId: asset.id,
|
||||
async function annotate(sessionId: string, requestId: string, context: ShumaiMessageContext) {
|
||||
await agentSessionService.withWriter(sessionId, async (storage) => {
|
||||
const harness = await Harness.open(
|
||||
storage,
|
||||
{ models: createModels(), registry: createRegistry() },
|
||||
BACKGROUND_CONTEXT,
|
||||
)
|
||||
try {
|
||||
const root = await harness.root(BACKGROUND_CONTEXT)
|
||||
await root.submit(
|
||||
{
|
||||
type: 'write',
|
||||
requestId,
|
||||
entry: {
|
||||
kind: 'shumai.message',
|
||||
data: JSON.parse(
|
||||
JSON.stringify({ displayText: 'Check markup', timestamp: 1, context }),
|
||||
),
|
||||
},
|
||||
},
|
||||
BACKGROUND_CONTEXT,
|
||||
)
|
||||
} finally {
|
||||
await harness.close(BACKGROUND_CONTEXT)
|
||||
}
|
||||
})
|
||||
return `chat:${sessionId}:${requestId}`
|
||||
}
|
||||
|
||||
// Set active message context with markup annotations and position
|
||||
storage.currentMessageContext = {
|
||||
currentAsset: {
|
||||
id: asset.id,
|
||||
name: asset.name,
|
||||
type: 'file',
|
||||
path: asset.name,
|
||||
},
|
||||
it('resolves annotations after a SQLite cache rebuild using the durable submission reference', async () => {
|
||||
const { session, asset } = await fixture()
|
||||
const id = await annotate(session.id, 'request', {
|
||||
currentAsset: { id: asset.id, name: asset.name, type: 'file' },
|
||||
position: { type: 'time', seconds: 12.34 },
|
||||
annotation: true,
|
||||
annotations: [
|
||||
{
|
||||
type: 'rectangle',
|
||||
x: 100,
|
||||
y: 200,
|
||||
width: 50,
|
||||
height: 50,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
// User sends a message -> DatabaseSessionStorage.appendEntry is called
|
||||
const userEntry: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: '01JTESTENTRY1234567890',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'user',
|
||||
content: 'Check this rectangle markup on video',
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
}
|
||||
|
||||
await storage.appendEntry(userEntry)
|
||||
|
||||
// Verify DatabaseSessionStorage persisted entry with details.id = entry.id
|
||||
const dbEntry = await prisma.agentSessionEntry.findUnique({
|
||||
where: { id: '01JTESTENTRY1234567890' },
|
||||
annotations: [{ type: 'box' }],
|
||||
})
|
||||
await agentSessionService.closeAll()
|
||||
expect(await resolveAnnotationsById(asset.id, id)).toEqual({
|
||||
timestamp: 12.34,
|
||||
annotations: [{ type: 'box' }],
|
||||
})
|
||||
expect(dbEntry).toBeDefined()
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const details = (dbEntry?.data as any)?.details
|
||||
expect(details.id).toBe('01JTESTENTRY1234567890')
|
||||
expect(details.position).toEqual({ type: 'time', seconds: 12.34 })
|
||||
expect(details.annotations).toHaveLength(1)
|
||||
|
||||
// Now resolveAnnotationsById directly with the entry id and matching asset.id
|
||||
const result = await resolveAnnotationsById(asset.id, '01JTESTENTRY1234567890')
|
||||
expect(result.timestamp).toBe(12.34)
|
||||
expect(result.annotations).toEqual([
|
||||
{
|
||||
type: 'rectangle',
|
||||
x: 100,
|
||||
y: 200,
|
||||
width: 50,
|
||||
height: 50,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it('resolves annotations and degree position timestamp from 1-on-1 chat AgentSessionEntry for 3D assets', async () => {
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'User 3D', email: 'user3d@test.com', password: 'pw' },
|
||||
})
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Team 3D' },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Project 3D', teamId: team.id },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'robot.glb',
|
||||
type: 'file',
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
const agentUser = await prisma.user.create({
|
||||
data: { name: 'Agent 3D', email: 'agent3d@example.com', type: 'agent' },
|
||||
})
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: agentUser.id,
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'test-provider', model: 'test-model' },
|
||||
},
|
||||
})
|
||||
|
||||
const storage = await DatabaseSessionStorage.create({
|
||||
agentId: agent.id,
|
||||
userId: user.id,
|
||||
assetId: asset.id,
|
||||
})
|
||||
|
||||
storage.currentMessageContext = {
|
||||
currentAsset: {
|
||||
id: asset.id,
|
||||
name: asset.name,
|
||||
type: 'file',
|
||||
path: asset.name,
|
||||
},
|
||||
it('resolves degree positions as timestamps for 3D assets', async () => {
|
||||
const { session, asset } = await fixture()
|
||||
const id = await annotate(session.id, 'degree', {
|
||||
currentAsset: { id: asset.id, name: asset.name, type: 'file' },
|
||||
position: { type: 'degree', degree: 45 },
|
||||
annotation: true,
|
||||
annotations: [
|
||||
{
|
||||
type: 'rectangle',
|
||||
x: 10,
|
||||
y: 20,
|
||||
width: 30,
|
||||
height: 40,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const userEntry: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: '01JTEST3DENTRY123456789',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'user',
|
||||
content: 'Check markup at 45 degrees',
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
}
|
||||
|
||||
await storage.appendEntry(userEntry)
|
||||
|
||||
const result = await resolveAnnotationsById(asset.id, '01JTEST3DENTRY123456789')
|
||||
expect(result.timestamp).toBeCloseTo(degreeToSecond(45))
|
||||
expect(result.annotations).toEqual([
|
||||
{
|
||||
type: 'rectangle',
|
||||
x: 10,
|
||||
y: 20,
|
||||
width: 30,
|
||||
height: 40,
|
||||
},
|
||||
])
|
||||
annotations: [{ type: 'box' }],
|
||||
})
|
||||
expect((await resolveAnnotationsById(asset.id, id)).timestamp).toBeCloseTo(degreeToSecond(45))
|
||||
})
|
||||
|
||||
it('throws an error when AgentSessionEntry belongs to a different asset', async () => {
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'User', email: 'user3@test.com', password: 'pw' },
|
||||
})
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Team' },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Project', teamId: team.id },
|
||||
})
|
||||
const assetA = await prisma.asset.create({
|
||||
data: { name: 'chatA.mp4', type: 'file', projectId: project.id, status: 'uploaded' },
|
||||
})
|
||||
const assetB = await prisma.asset.create({
|
||||
data: { name: 'chatB.mp4', type: 'file', projectId: project.id, status: 'uploaded' },
|
||||
})
|
||||
const agentUser = await prisma.user.create({
|
||||
data: { name: 'Bot User', email: 'bot2@example.com', type: 'agent' },
|
||||
})
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: agentUser.id,
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'test-provider', model: 'test-model' },
|
||||
},
|
||||
})
|
||||
|
||||
const storage = await DatabaseSessionStorage.create({
|
||||
agentId: agent.id,
|
||||
userId: user.id,
|
||||
assetId: assetB.id,
|
||||
})
|
||||
storage.currentMessageContext = {
|
||||
currentAsset: { id: assetB.id, name: assetB.name, type: 'file', path: assetB.name },
|
||||
it('rejects references to another asset', async () => {
|
||||
const { session, asset, other } = await fixture()
|
||||
const id = await annotate(session.id, 'request', {
|
||||
currentAsset: { id: asset.id, name: asset.name, type: 'file' },
|
||||
annotation: true,
|
||||
annotations: [{ type: 'box' }],
|
||||
}
|
||||
const userEntry: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: '01JENTRYONASSETB123456',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: { role: 'user', content: 'Markup on B', timestamp: Date.now() },
|
||||
}
|
||||
await storage.appendEntry(userEntry)
|
||||
|
||||
await expect(resolveAnnotationsById(assetA.id, '01JENTRYONASSETB123456')).rejects.toThrow(
|
||||
`Annotation "01JENTRYONASSETB123456" belongs to asset "${assetB.id}", not target asset "${assetA.id}".`,
|
||||
})
|
||||
await expect(resolveAnnotationsById(other.id, id)).rejects.toThrow(
|
||||
`belongs to asset "${asset.id}"`,
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,396 @@
|
||||
import { createModels, fauxAssistantMessage, type Models } from '@earendil-works/pi-ai'
|
||||
import { AssistantMessageEventStream } from '@earendil-works/pi-ai/utils/event-stream'
|
||||
import { BACKGROUND_CONTEXT } from '@earendil-works/chord/context'
|
||||
import { Harness, createRegistry } from '@earendil-works/pi-durable'
|
||||
import { agentSessionService } from '@shumai/core/src/agent/session-service'
|
||||
import { prisma, type AssetComment } from '@shumai/db'
|
||||
import { acquireAdvisoryLock } from '@shumai/db/src/advisory-lock'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { getShumaiMessage } from '@shumai/dtos'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi, type MockInstance } from 'vitest'
|
||||
import { agentChatActivity, getAgentChatContextActivity } from './activities/agent'
|
||||
import { initializeCommentSession, synchronizeCommentContext } from './comment-context'
|
||||
import { closeAgentSessions } from './runtime'
|
||||
|
||||
async function createThread() {
|
||||
const team = await prisma.team.create({ data: { name: 'Concurrent comment team' } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Concurrent comment project', teamId: team.id },
|
||||
})
|
||||
const users = await Promise.all(
|
||||
Array.from({ length: 5 }, (_, index) =>
|
||||
prisma.user.create({
|
||||
data: { name: `User ${index}`, email: `comment-user-${index}@example.test` },
|
||||
}),
|
||||
),
|
||||
)
|
||||
await prisma.teamMember.createMany({
|
||||
data: users.map((user) => ({ teamId: team.id, userId: user.id, role: 'owner' })),
|
||||
})
|
||||
const provider = await prisma.provider.create({
|
||||
data: {
|
||||
teamId: team.id,
|
||||
name: 'google',
|
||||
config: { api: 'google-generative-ai', apiKey: 'unused-test-key' },
|
||||
},
|
||||
})
|
||||
const model = await prisma.model.create({
|
||||
data: {
|
||||
providerId: provider.id,
|
||||
modelId: 'gemini-test',
|
||||
config: {
|
||||
input: ['text'],
|
||||
reasoning: false,
|
||||
contextWindow: 8192,
|
||||
maxTokens: 1024,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
},
|
||||
},
|
||||
})
|
||||
const agentUser = await prisma.user.create({
|
||||
data: { name: 'Comment agent', email: 'comment-agent@example.test', type: 'agent' },
|
||||
})
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: agentUser.id,
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
providerId: provider.id,
|
||||
modelId: model.id,
|
||||
config: { provider: 'google', model: 'gemini-test' },
|
||||
},
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: { name: 'comments.txt', type: 'file', projectId: project.id, status: 'processed' },
|
||||
})
|
||||
const root = await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: asset.id,
|
||||
creatorId: users[0].id,
|
||||
message: 'Discuss this asset here.',
|
||||
createdAt: new Date('2026-01-01T00:00:00.000Z'),
|
||||
},
|
||||
})
|
||||
const mentions: AssetComment[] = []
|
||||
for (const [index, user] of users.entries()) {
|
||||
mentions.push(
|
||||
await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: asset.id,
|
||||
creatorId: user.id,
|
||||
replyToId: root.id,
|
||||
message: `<@${agent.id}> Request ${index}`,
|
||||
// Explicit ordering makes the imported history independent of clock resolution.
|
||||
createdAt: new Date(root.createdAt.getTime() + index + 1),
|
||||
},
|
||||
}),
|
||||
)
|
||||
}
|
||||
const initialize = (comment: AssetComment) =>
|
||||
initializeCommentSession({
|
||||
teamId: team.id,
|
||||
agentId: agent.id,
|
||||
userId: comment.creatorId!,
|
||||
userCommentId: comment.id,
|
||||
})
|
||||
const tasks = await prisma.workflowTask.createManyAndReturn({
|
||||
data: mentions.map((comment) => ({
|
||||
teamId: team.id,
|
||||
projectId: project.id,
|
||||
assetId: asset.id,
|
||||
type: 'chat',
|
||||
status: 'pending',
|
||||
payload: {
|
||||
projectId: project.id,
|
||||
agent: { agentId: agent.id, userId: comment.creatorId!, userCommentId: comment.id },
|
||||
},
|
||||
})),
|
||||
})
|
||||
const reply = async (comment: AssetComment, sessionId: string) => {
|
||||
const context = await getAgentChatContextActivity({
|
||||
teamId: team.id,
|
||||
agentId: agent.id,
|
||||
userId: comment.creatorId!,
|
||||
projectId: project.id,
|
||||
})
|
||||
return agentChatActivity({
|
||||
taskId: tasks.find((task) => task.payload?.agent?.userCommentId === comment.id)!.id,
|
||||
teamId: team.id,
|
||||
agentId: agent.id,
|
||||
sessionId,
|
||||
userId: comment.creatorId!,
|
||||
userCommentId: comment.id,
|
||||
message: comment.message!,
|
||||
imageUrls: [],
|
||||
projectId: project.id,
|
||||
folderId: asset.id,
|
||||
assetId: asset.id,
|
||||
context,
|
||||
})
|
||||
}
|
||||
return { team, project, users, agent, asset, root, mentions, tasks, initialize, reply }
|
||||
}
|
||||
|
||||
describe('Comment session concurrency regressions', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
afterEach(async () => {
|
||||
await closeAgentSessions()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('simultaneous first mentions in a human-created thread', () => {
|
||||
let sessionIds: string[]
|
||||
let persistedIds: string[]
|
||||
|
||||
beforeEach(async () => {
|
||||
const thread = await createThread()
|
||||
sessionIds = await Promise.all(thread.mentions.map(thread.initialize))
|
||||
const sessions = await prisma.agentSession.findMany({
|
||||
where: { assetId: thread.asset.id, type: 'comment', userCommentId: thread.root.id },
|
||||
orderBy: { id: 'asc' },
|
||||
})
|
||||
persistedIds = sessions.map((session) => session.id)
|
||||
// Keep setup/storage failures outside the expected-failure assertion.
|
||||
expect(sessionIds).toHaveLength(5)
|
||||
expect(new Set(persistedIds)).toEqual(new Set(sessionIds))
|
||||
for (const sessionId of new Set(sessionIds)) {
|
||||
expect(await prisma.agentSession.findUnique({ where: { id: sessionId } })).toMatchObject({
|
||||
agentId: thread.agent.id,
|
||||
assetId: thread.asset.id,
|
||||
userCommentId: thread.root.id,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
it('uses one durable session for all five callers', () => {
|
||||
expect({ returned: [...new Set(sessionIds)], persisted: persistedIds }).toEqual({
|
||||
returned: [sessionIds[0]],
|
||||
persisted: [sessionIds[0]],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('concurrent history synchronization before live submissions', () => {
|
||||
let sourceCounts: number[]
|
||||
let thread: Awaited<ReturnType<typeof createThread>>
|
||||
let sessionId: string
|
||||
let model: MockInstance<Models['streamSimple']>
|
||||
|
||||
beforeEach(async () => {
|
||||
// Mock only the provider boundary; admission, history, locks, SQLite, and DB remain real.
|
||||
model = vi
|
||||
.spyOn(Object.getPrototypeOf(createModels()) as Models, 'streamSimple')
|
||||
.mockImplementation(() => {
|
||||
const stream = new AssistantMessageEventStream()
|
||||
const answer = fauxAssistantMessage('Test reply')
|
||||
answer.usage = { ...answer.usage, input: 3, output: 2, totalTokens: 5 }
|
||||
stream.push({ type: 'done', reason: 'stop', message: answer })
|
||||
stream.end()
|
||||
return stream
|
||||
})
|
||||
thread = await createThread()
|
||||
sessionId = await thread.initialize(thread.root)
|
||||
const initialized = await Promise.all(thread.mentions.map(thread.initialize))
|
||||
expect(new Set(initialized)).toEqual(new Set([sessionId]))
|
||||
// All initializers can finish before the workflows acquire their generation writer.
|
||||
const replies = await Promise.all(
|
||||
thread.mentions.map((comment) => thread.reply(comment, sessionId)),
|
||||
)
|
||||
expect(replies.map((reply) => reply.text)).toEqual(Array(5).fill('Test reply'))
|
||||
expect(model).toHaveBeenCalledTimes(5)
|
||||
// Reopen from PostgreSQL commits rather than trusting the in-process SQLite cache.
|
||||
await closeAgentSessions()
|
||||
const entries = await agentSessionService.getEntries(sessionId)
|
||||
sourceCounts = thread.mentions.map(
|
||||
(comment) =>
|
||||
entries.filter((entry) => getShumaiMessage(entry)?.sourceCommentId === comment.id).length,
|
||||
)
|
||||
expect(sourceCounts).toHaveLength(5)
|
||||
expect(sourceCounts.every((count) => count >= 1)).toBe(true)
|
||||
})
|
||||
|
||||
it('includes each source comment only once in persisted model history', () => {
|
||||
expect(sourceCounts).toEqual([1, 1, 1, 1, 1])
|
||||
})
|
||||
|
||||
it('recovers a completed request without another model call, input, or usage charge', async () => {
|
||||
const usage = await prisma.aiUsage.findMany({
|
||||
where: { teamId: thread.team.id },
|
||||
orderBy: { id: 'desc' },
|
||||
})
|
||||
const entries = await agentSessionService.getEntries(sessionId)
|
||||
await closeAgentSessions()
|
||||
expect((await thread.reply(thread.mentions[0], sessionId)).text).toBe('Test reply')
|
||||
expect(model).toHaveBeenCalledTimes(5)
|
||||
expect(await agentSessionService.getEntries(sessionId)).toEqual(entries)
|
||||
expect(
|
||||
await prisma.aiUsage.findMany({
|
||||
where: { teamId: thread.team.id },
|
||||
orderBy: { id: 'desc' },
|
||||
}),
|
||||
).toEqual(usage)
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps different agents and different thread roots in separate sessions', async () => {
|
||||
const thread = await createThread()
|
||||
const first = await thread.initialize(thread.root)
|
||||
const secondRoot = await prisma.assetComment.create({
|
||||
data: { assetId: thread.asset.id, creatorId: thread.users[0].id, message: 'Another thread' },
|
||||
})
|
||||
const agentUser = await prisma.user.create({
|
||||
data: { name: 'Second agent', email: 'second-agent@example.test', type: 'agent' },
|
||||
})
|
||||
await prisma.agent.create({
|
||||
data: { id: agentUser.id, teamId: thread.team.id, type: 'chat', config: thread.agent.config },
|
||||
})
|
||||
const [secondThread, secondAgent] = await Promise.all([
|
||||
thread.initialize(secondRoot),
|
||||
initializeCommentSession({
|
||||
teamId: thread.team.id,
|
||||
agentId: agentUser.id,
|
||||
userCommentId: thread.root.id,
|
||||
}),
|
||||
])
|
||||
expect(new Set([first, secondThread, secondAgent]).size).toBe(3)
|
||||
expect(await thread.initialize(thread.root)).toBe(first)
|
||||
})
|
||||
|
||||
it('imports context-only comments once while reserving workflow-owned comments in every status', async () => {
|
||||
const thread = await createThread()
|
||||
const statuses = ['pending', 'processing', 'failed', 'completed', 'pending'] as const
|
||||
for (const [index, task] of thread.tasks.entries()) {
|
||||
await prisma.workflowTask.update({
|
||||
where: { id: task.id },
|
||||
data: { status: statuses[index] },
|
||||
})
|
||||
}
|
||||
const note = await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: thread.asset.id,
|
||||
creatorId: thread.users[0].id,
|
||||
replyToId: thread.root.id,
|
||||
message: 'Keep this review note.',
|
||||
createdAt: new Date(thread.root.createdAt.getTime() + 1),
|
||||
},
|
||||
})
|
||||
const otherAgentComment = await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: thread.asset.id,
|
||||
creatorId: thread.users[1].id,
|
||||
replyToId: thread.root.id,
|
||||
message: 'A request for another agent.',
|
||||
createdAt: new Date(thread.root.createdAt.getTime() + 2),
|
||||
},
|
||||
})
|
||||
await prisma.workflowTask.create({
|
||||
data: {
|
||||
assetId: thread.asset.id,
|
||||
type: 'chat',
|
||||
status: 'pending',
|
||||
payload: {
|
||||
projectId: thread.project.id,
|
||||
agent: { agentId: 'another-agent', userCommentId: otherAgentComment.id },
|
||||
},
|
||||
},
|
||||
})
|
||||
await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: thread.asset.id,
|
||||
replyToId: thread.root.id,
|
||||
message: '__CHAT__',
|
||||
createdAt: new Date(thread.root.createdAt.getTime() + 3),
|
||||
},
|
||||
})
|
||||
const earlierThread = await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: thread.asset.id,
|
||||
creatorId: thread.users[0].id,
|
||||
message: 'An earlier top-level agent request.',
|
||||
createdAt: new Date(thread.root.createdAt.getTime() - 1),
|
||||
},
|
||||
})
|
||||
await prisma.workflowTask.create({
|
||||
data: {
|
||||
assetId: thread.asset.id,
|
||||
type: 'chat',
|
||||
status: 'pending',
|
||||
payload: {
|
||||
projectId: thread.project.id,
|
||||
agent: { agentId: thread.agent.id, userCommentId: earlierThread.id },
|
||||
},
|
||||
},
|
||||
})
|
||||
const sessionId = await thread.initialize(thread.root)
|
||||
await agentSessionService.withWriter(sessionId, async (storage) => {
|
||||
const harness = await Harness.open(
|
||||
storage,
|
||||
{ models: createModels(), registry: createRegistry() },
|
||||
BACKGROUND_CONTEXT,
|
||||
)
|
||||
try {
|
||||
const params = {
|
||||
conversation: await harness.root(BACKGROUND_CONTEXT),
|
||||
sessionId,
|
||||
teamId: thread.team.id,
|
||||
agentId: thread.agent.id,
|
||||
userCommentId: thread.mentions[4].id,
|
||||
}
|
||||
await synchronizeCommentContext(params)
|
||||
await synchronizeCommentContext(params)
|
||||
} finally {
|
||||
await harness.close(BACKGROUND_CONTEXT)
|
||||
}
|
||||
})
|
||||
await closeAgentSessions()
|
||||
const sources = (await agentSessionService.getEntries(sessionId)).map(
|
||||
(entry) => getShumaiMessage(entry)?.sourceCommentId,
|
||||
)
|
||||
expect(new Set(sources)).toEqual(
|
||||
new Set([earlierThread.id, thread.root.id, note.id, otherAgentComment.id]),
|
||||
)
|
||||
expect(sources).toHaveLength(4)
|
||||
})
|
||||
|
||||
it('reuses the oldest duplicate session without rewriting existing records', async () => {
|
||||
const thread = await createThread()
|
||||
const first = await thread.initialize(thread.root)
|
||||
const duplicate = await prisma.agentSession.create({
|
||||
data: {
|
||||
agentId: thread.agent.id,
|
||||
assetId: thread.asset.id,
|
||||
userCommentId: thread.root.id,
|
||||
type: 'comment',
|
||||
cwd: process.cwd(),
|
||||
},
|
||||
})
|
||||
const original = await prisma.agentSession.findMany({
|
||||
where: { id: { in: [first, duplicate.id] } },
|
||||
orderBy: { id: 'desc' },
|
||||
})
|
||||
expect(await thread.initialize(thread.root)).toBe([first, duplicate.id].sort()[0])
|
||||
expect(
|
||||
await prisma.agentSession.findMany({
|
||||
where: { id: { in: [first, duplicate.id] } },
|
||||
orderBy: { id: 'desc' },
|
||||
}),
|
||||
).toEqual(original)
|
||||
})
|
||||
|
||||
it('releases the thread creation lock when initialization rolls back', async () => {
|
||||
const thread = await createThread()
|
||||
const agentUser = await prisma.user.create({
|
||||
data: { name: 'New agent', email: 'new-agent@example.test', type: 'agent' },
|
||||
})
|
||||
const params = { teamId: thread.team.id, agentId: agentUser.id, userCommentId: thread.root.id }
|
||||
await expect(initializeCommentSession({ ...params, teamId: 'missing-team' })).rejects.toThrow()
|
||||
const lock = await acquireAdvisoryLock(
|
||||
`agent-thread:${agentUser.id}:${thread.asset.id}:${thread.root.id}`,
|
||||
AbortSignal.timeout(2000),
|
||||
)
|
||||
await lock.release()
|
||||
expect(await prisma.agent.findUnique({ where: { id: agentUser.id } })).toBeNull()
|
||||
expect(await initializeCommentSession(params)).toBeTruthy()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,373 @@
|
||||
import { BACKGROUND_CONTEXT as piContext } from '@earendil-works/chord/context'
|
||||
import { type Message as AgentMessage } from '@earendil-works/pi-ai'
|
||||
import {
|
||||
type Conversation,
|
||||
defineDoc,
|
||||
type Cursor,
|
||||
type EntryDraft,
|
||||
type JsonObject,
|
||||
} from '@earendil-works/pi-durable'
|
||||
import { secondToDegree } from '@shumai/core/src/utils/model3d'
|
||||
import { prisma } from '@shumai/db'
|
||||
import {
|
||||
ShumaiMessageEntry,
|
||||
type ShumaiMediaPosition,
|
||||
type ShumaiMessageContext,
|
||||
} from '@shumai/dtos'
|
||||
import { ApplicationFailure } from '@shumai/workflow-core'
|
||||
|
||||
const ThreadContextDoc = defineDoc<{ commentIds: string[] }>({
|
||||
kind: 'shumai.thread',
|
||||
version: 1,
|
||||
scope: 'conversation',
|
||||
history: 'latest',
|
||||
fork: 'initial',
|
||||
initial: () => ({ commentIds: [] }),
|
||||
checkpointWhen: () => true,
|
||||
})
|
||||
|
||||
export async function initializeCommentSession(params: {
|
||||
teamId: string
|
||||
agentId: string
|
||||
userCommentId: string
|
||||
userId?: string
|
||||
}): Promise<string> {
|
||||
const userComment = await prisma.assetComment.findUnique({
|
||||
where: { id: params.userCommentId },
|
||||
select: { assetId: true, replyToId: true },
|
||||
})
|
||||
|
||||
if (!userComment) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `User comment ${params.userCommentId} not found`,
|
||||
nonRetryable: true,
|
||||
})
|
||||
}
|
||||
|
||||
const rootCommentId = userComment.replyToId || params.userCommentId
|
||||
const threadKey = `agent-thread:${params.agentId}:${userComment.assetId}:${rootCommentId}`
|
||||
return prisma.$transaction(async (tx) => {
|
||||
// The lock and insert share a transaction, so rollback also releases ownership.
|
||||
// Unlike the writer lock, this key exists before a session ID has been chosen.
|
||||
await tx.$executeRaw`SELECT pg_advisory_xact_lock(hashtextextended(${threadKey}, 0))`
|
||||
await tx.user.upsert({
|
||||
where: { id: params.agentId },
|
||||
update: {},
|
||||
create: {
|
||||
id: params.agentId,
|
||||
name: 'Ai Agent',
|
||||
email: `${params.agentId}@shumai.ai`,
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
await tx.agent.upsert({
|
||||
where: { id: params.agentId },
|
||||
update: {},
|
||||
create: {
|
||||
id: params.agentId,
|
||||
teamId: params.teamId,
|
||||
type: 'chat',
|
||||
config: { provider: 'openai', model: 'gpt-4' },
|
||||
},
|
||||
})
|
||||
const existing = await tx.agentSession.findFirst({
|
||||
where: {
|
||||
agentId: params.agentId,
|
||||
assetId: userComment.assetId,
|
||||
type: 'comment',
|
||||
userCommentId: rootCommentId,
|
||||
},
|
||||
// Reuse the original session deterministically if older data contains duplicates.
|
||||
orderBy: { id: 'asc' },
|
||||
})
|
||||
if (existing) return existing.id
|
||||
const session = await tx.agentSession.create({
|
||||
data: {
|
||||
agentId: params.agentId,
|
||||
userId: params.userId || null,
|
||||
cwd: process.cwd(),
|
||||
assetId: userComment.assetId,
|
||||
userCommentId: rootCommentId,
|
||||
type: 'comment',
|
||||
},
|
||||
})
|
||||
return session.id
|
||||
})
|
||||
}
|
||||
|
||||
/** Import passive history while the caller owns the generation's session writer. */
|
||||
export async function synchronizeCommentContext(params: {
|
||||
conversation: Conversation
|
||||
sessionId: string
|
||||
teamId: string
|
||||
agentId: string
|
||||
userCommentId: string
|
||||
}): Promise<void> {
|
||||
const userComment = await prisma.assetComment.findUnique({
|
||||
where: { id: params.userCommentId },
|
||||
include: { creator: true },
|
||||
})
|
||||
if (!userComment) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `User comment ${params.userCommentId} not found`,
|
||||
nonRetryable: true,
|
||||
})
|
||||
}
|
||||
const rootCommentId = userComment.replyToId || userComment.id
|
||||
const rootComment = await prisma.assetComment.findUnique({
|
||||
where: { id: rootCommentId },
|
||||
include: { creator: true },
|
||||
})
|
||||
if (!rootComment) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `Root comment ${rootCommentId} not found`,
|
||||
nonRetryable: true,
|
||||
})
|
||||
}
|
||||
|
||||
// 1. Fetch top-level comments up to rootComment.createdAt sorted by id
|
||||
const topLevelComments = await prisma.assetComment.findMany({
|
||||
where: {
|
||||
assetId: userComment.assetId,
|
||||
replyToId: null,
|
||||
createdAt: { lte: rootComment.createdAt },
|
||||
},
|
||||
orderBy: { id: 'asc' },
|
||||
include: {
|
||||
creator: true,
|
||||
attachments: { include: { asset: true } },
|
||||
},
|
||||
})
|
||||
|
||||
// 2. Fetch thread replies up to userComment.createdAt excluding userComment itself
|
||||
const threadReplies = userComment.replyToId
|
||||
? await prisma.assetComment.findMany({
|
||||
where: {
|
||||
replyToId: rootCommentId,
|
||||
createdAt: { lte: userComment.createdAt },
|
||||
id: { not: userComment.id },
|
||||
},
|
||||
orderBy: { id: 'asc' },
|
||||
include: {
|
||||
creator: true,
|
||||
attachments: { include: { asset: true } },
|
||||
},
|
||||
})
|
||||
: []
|
||||
|
||||
const replyCounts = await prisma.assetComment.groupBy({
|
||||
by: ['replyToId'],
|
||||
where: { assetId: userComment.assetId, replyToId: { not: null }, message: { not: '__CHAT__' } },
|
||||
_count: true,
|
||||
})
|
||||
const counts = new Map(replyCounts.map((row) => [row.replyToId, row._count]))
|
||||
const allCommentsToSync = [...topLevelComments, ...threadReplies]
|
||||
|
||||
// Resolve user mentions from IDs to names and roles
|
||||
const mentionRegex = /<@([^>]+)>/g
|
||||
const mentionedUserIds = new Set<string>()
|
||||
for (const c of allCommentsToSync) {
|
||||
if (c.message) {
|
||||
const matches = [...c.message.matchAll(mentionRegex)]
|
||||
for (const match of matches) {
|
||||
mentionedUserIds.add(match[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const userIdToNameMap = new Map<string, string>()
|
||||
if (mentionedUserIds.size > 0) {
|
||||
const resolvedUsers = await prisma.user.findMany({
|
||||
where: { id: { in: Array.from(mentionedUserIds) } },
|
||||
select: { id: true, name: true },
|
||||
})
|
||||
for (const u of resolvedUsers) {
|
||||
if (u.name) userIdToNameMap.set(u.id, u.name)
|
||||
}
|
||||
}
|
||||
|
||||
const userRoleMap = new Map<string, string>()
|
||||
const humanCreatorIds = Array.from(
|
||||
new Set(
|
||||
allCommentsToSync
|
||||
.filter((c) => c.creator?.type !== 'agent' && c.creatorId)
|
||||
.map((c) => c.creatorId!),
|
||||
),
|
||||
)
|
||||
if (humanCreatorIds.length > 0) {
|
||||
const members = await prisma.teamMember.findMany({
|
||||
where: {
|
||||
teamId: params.teamId,
|
||||
userId: { in: humanCreatorIds },
|
||||
},
|
||||
select: { userId: true, role: true },
|
||||
})
|
||||
for (const m of members) {
|
||||
userRoleMap.set(m.userId, m.role)
|
||||
}
|
||||
}
|
||||
|
||||
const assetRecord = await prisma.asset.findUnique({
|
||||
where: { id: userComment.assetId },
|
||||
select: { media: true },
|
||||
})
|
||||
const proxyType = (assetRecord?.media as PrismaJson.MediaInfo | null)?.proxyType
|
||||
|
||||
function buildCommentEntry(c: (typeof allCommentsToSync)[0]): EntryDraft {
|
||||
const isAgent = c.creator?.type === 'agent' || c.sessionId !== null
|
||||
let messageContent = c.message || ''
|
||||
|
||||
messageContent = messageContent.replace(mentionRegex, (match, userId) => {
|
||||
const resolvedName = userIdToNameMap.get(userId)
|
||||
return resolvedName ? `<@${resolvedName}>` : match
|
||||
})
|
||||
|
||||
if (isAgent) {
|
||||
const message: AgentMessage = {
|
||||
role: 'assistant',
|
||||
content: [{ type: 'text', text: messageContent }],
|
||||
api: 'custom',
|
||||
provider: 'shumai',
|
||||
model: 'agent',
|
||||
usage: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
totalTokens: 0,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
||||
},
|
||||
stopReason: 'stop',
|
||||
timestamp: c.createdAt.getTime(),
|
||||
}
|
||||
return {
|
||||
kind: 'pi.assistant',
|
||||
model: [message],
|
||||
data: { sourceCommentId: c.id },
|
||||
}
|
||||
}
|
||||
|
||||
let userObj: ShumaiMessageContext['user'] = undefined
|
||||
if (c.creator) {
|
||||
const role = (c.creatorId ? userRoleMap.get(c.creatorId) : undefined) || 'user'
|
||||
userObj = {
|
||||
id: c.creator.id,
|
||||
name: c.creator.name || 'Unknown',
|
||||
role,
|
||||
}
|
||||
}
|
||||
|
||||
let position: ShumaiMediaPosition | undefined = undefined
|
||||
if (c.second !== null && c.second !== undefined) {
|
||||
if (proxyType === '3d') {
|
||||
position = { type: 'degree', degree: secondToDegree(c.second) }
|
||||
} else if (proxyType === 'pdf') {
|
||||
position = { type: 'page', page: Math.round(c.second) }
|
||||
} else {
|
||||
position = { type: 'time', seconds: c.second }
|
||||
}
|
||||
}
|
||||
|
||||
const annotation = !!(c.annotation && Array.isArray(c.annotation) && c.annotation.length > 0)
|
||||
|
||||
const attachedFiles: ShumaiMessageContext['attachedFiles'] = []
|
||||
if (c.attachments && c.attachments.length > 0) {
|
||||
for (const att of c.attachments) {
|
||||
if (att.asset) {
|
||||
const attProxyType = (att.asset.media as PrismaJson.MediaInfo | null)?.proxyType
|
||||
attachedFiles.push({
|
||||
id: att.asset.id,
|
||||
name: att.asset.name,
|
||||
type: att.asset.type,
|
||||
mediaType: attProxyType as
|
||||
'image' | 'video' | 'pdf' | 'audio' | '3d' | 'other' | undefined,
|
||||
mimeType: att.asset.mediaType || undefined,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const commentDetails: ShumaiMessageContext = {
|
||||
id: c.id,
|
||||
...(!c.replyToId && counts.get(c.id)
|
||||
? { thread: { id: c.id, replyCount: counts.get(c.id)! } }
|
||||
: {}),
|
||||
...(userObj ? { user: userObj } : {}),
|
||||
...(position ? { position } : {}),
|
||||
...(annotation
|
||||
? {
|
||||
annotation: true,
|
||||
annotations: c.annotation as unknown as ShumaiMessageContext['annotations'],
|
||||
}
|
||||
: {}),
|
||||
...(attachedFiles.length > 0 ? { attachedFiles } : {}),
|
||||
}
|
||||
|
||||
return {
|
||||
kind: ShumaiMessageEntry.kind,
|
||||
model: [{ role: 'user' as const, content: messageContent, timestamp: c.createdAt.getTime() }],
|
||||
data: JSON.parse(
|
||||
JSON.stringify({
|
||||
displayText: messageContent,
|
||||
timestamp: c.createdAt.getTime(),
|
||||
context: commentDetails,
|
||||
sourceCommentId: c.id,
|
||||
}),
|
||||
) as JsonObject,
|
||||
}
|
||||
}
|
||||
|
||||
const root = params.conversation
|
||||
const present = new Set<string>()
|
||||
let cursor: Cursor | undefined
|
||||
do {
|
||||
const page = await root.entries({ order: 'ascending' }, 100, cursor, piContext)
|
||||
for (const entry of page.items) {
|
||||
if (
|
||||
entry.data &&
|
||||
typeof entry.data === 'object' &&
|
||||
!Array.isArray(entry.data) &&
|
||||
'sourceCommentId' in entry.data &&
|
||||
typeof entry.data.sourceCommentId === 'string'
|
||||
)
|
||||
present.add(entry.data.sourceCommentId)
|
||||
}
|
||||
cursor = page.next
|
||||
} while (cursor)
|
||||
const candidates = allCommentsToSync.filter(
|
||||
(comment) =>
|
||||
comment.id !== params.userCommentId &&
|
||||
comment.sessionId !== params.sessionId &&
|
||||
comment.message !== '__CHAT__' &&
|
||||
!present.has(comment.id),
|
||||
)
|
||||
if (!candidates.length) return
|
||||
// A comment and its agent workflows are created in the same DB transaction.
|
||||
// Reserve these comments for their live input, including across workflow retries.
|
||||
const threadCommentIds = candidates
|
||||
.filter((comment) => (comment.replyToId || comment.id) === rootCommentId)
|
||||
.map((comment) => comment.id)
|
||||
const tasks = threadCommentIds.length
|
||||
? await prisma.workflowTask.findMany({
|
||||
where: {
|
||||
assetId: userComment.assetId,
|
||||
type: 'chat',
|
||||
payload: { path: ['agent', 'agentId'], equals: params.agentId },
|
||||
OR: threadCommentIds.map((id) => ({
|
||||
payload: { path: ['agent', 'userCommentId'], equals: id },
|
||||
})),
|
||||
},
|
||||
select: { payload: true },
|
||||
orderBy: { id: 'desc' },
|
||||
})
|
||||
: []
|
||||
const liveComments = new Set(tasks.map((task) => task.payload?.agent?.userCommentId))
|
||||
await root.commit(async (tx) => {
|
||||
const state = await tx.doc(ThreadContextDoc, root.id)
|
||||
for (const comment of candidates) {
|
||||
if (liveComments.has(comment.id) || state.commentIds.includes(comment.id)) continue
|
||||
await tx.appendEntry(root.id, buildCommentEntry(comment))
|
||||
state.commentIds.push(comment.id)
|
||||
}
|
||||
}, piContext)
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { hashRunnerKey, runnerService, type RunnerSocket } from '@shumai/core/src/runner'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createAgentSession, type DbProviderInfo } from './index'
|
||||
import { closeAgentSessions } from './runtime'
|
||||
import { createRunnerBashTool } from './tools/runner-bash'
|
||||
|
||||
const mockProviders: DbProviderInfo[] = [
|
||||
{
|
||||
name: 'google',
|
||||
config: { api: 'google-generative-ai', apiKey: 'GOOGLE_API_KEY' },
|
||||
models: [
|
||||
{
|
||||
modelId: 'gemini',
|
||||
name: 'Gemini',
|
||||
config: {
|
||||
api: 'google-generative-ai',
|
||||
reasoning: false,
|
||||
input: ['text'],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 8192,
|
||||
maxTokens: 4096,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
function createMockWs() {
|
||||
const sent: string[] = []
|
||||
const ws: RunnerSocket = {
|
||||
send: vi.fn((data: string) => {
|
||||
sent.push(data)
|
||||
}),
|
||||
close: vi.fn(),
|
||||
}
|
||||
return {
|
||||
ws,
|
||||
sent,
|
||||
lastSentJson: () => (sent.length > 0 ? JSON.parse(sent[sent.length - 1]) : null),
|
||||
}
|
||||
}
|
||||
|
||||
function reconcile(runnerId: string, ws: RunnerSocket) {
|
||||
runnerService.handleMessage(
|
||||
runnerId,
|
||||
ws,
|
||||
JSON.stringify({
|
||||
type: 'reconcile',
|
||||
environment: { platform: 'linux', shell: '/bin/sh' },
|
||||
activeExecutions: [],
|
||||
completedExecutions: [],
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
describe('Concurrent Runner Sessions', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await closeAgentSessions()
|
||||
})
|
||||
|
||||
it('executes concurrent bash commands on the same runner with isolated executionIds', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Concurrent Runner Team' },
|
||||
})
|
||||
const { runner, key } = await runnerService.createRunner(team.id, { name: 'Shared Runner' })
|
||||
const conn = createMockWs()
|
||||
await runnerService.registerConnection(
|
||||
{ id: runner.id, teamId: team.id, name: runner.name, keyHash: hashRunnerKey(key) },
|
||||
conn.ws,
|
||||
)
|
||||
reconcile(runner.id, conn.ws)
|
||||
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: 'agent-1',
|
||||
name: 'Agent User',
|
||||
email: 'agent-user@shumai.ai',
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: 'agent-1',
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'google', model: 'gemini' },
|
||||
},
|
||||
})
|
||||
|
||||
await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: 'agent-1',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'Session A prompt',
|
||||
teamSkills: [],
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
const tool = createRunnerBashTool({}, { teamId: team.id })
|
||||
|
||||
// Note: registerConnection sends a 'welcome' message first (conn.sent[0])
|
||||
const execA = invokeTool(tool, 'call-a', {
|
||||
command: 'echo session-a',
|
||||
})
|
||||
await vi.waitFor(() => expect(conn.sent.length).toBe(2))
|
||||
const msgA = JSON.parse(conn.sent[1])
|
||||
|
||||
const execB = invokeTool(tool, 'call-b', {
|
||||
command: 'echo session-b',
|
||||
})
|
||||
await vi.waitFor(() => expect(conn.sent.length).toBe(3))
|
||||
const msgB = JSON.parse(conn.sent[2])
|
||||
|
||||
expect(msgA.executionId).not.toBe(msgB.executionId)
|
||||
|
||||
// Interleave output and completion for A and B
|
||||
runnerService.handleMessage(
|
||||
runner.id,
|
||||
conn.ws,
|
||||
JSON.stringify({
|
||||
type: 'exec.output',
|
||||
executionId: msgB.executionId,
|
||||
stream: 'stdout',
|
||||
offset: 0,
|
||||
data: 'out-b',
|
||||
}),
|
||||
)
|
||||
runnerService.handleMessage(
|
||||
runner.id,
|
||||
conn.ws,
|
||||
JSON.stringify({
|
||||
type: 'exec.output',
|
||||
executionId: msgA.executionId,
|
||||
stream: 'stdout',
|
||||
offset: 0,
|
||||
data: 'out-a',
|
||||
}),
|
||||
)
|
||||
runnerService.handleMessage(
|
||||
runner.id,
|
||||
conn.ws,
|
||||
JSON.stringify({
|
||||
type: 'exec.result',
|
||||
executionId: msgA.executionId,
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
aborted: false,
|
||||
}),
|
||||
)
|
||||
runnerService.handleMessage(
|
||||
runner.id,
|
||||
conn.ws,
|
||||
JSON.stringify({
|
||||
type: 'exec.result',
|
||||
executionId: msgB.executionId,
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
aborted: false,
|
||||
}),
|
||||
)
|
||||
|
||||
const [resA, resB] = await Promise.all([execA, execB])
|
||||
expect(resA.content).toContainEqual(
|
||||
expect.objectContaining({ text: expect.stringContaining('out-a') }),
|
||||
)
|
||||
expect(resB.content).toContainEqual(
|
||||
expect.objectContaining({ text: expect.stringContaining('out-b') }),
|
||||
)
|
||||
|
||||
runnerService.handleDisconnect(runner.id, conn.ws)
|
||||
})
|
||||
|
||||
it('routes agent commands to their bound runner or team default runner independently', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Multi Runner Team' },
|
||||
})
|
||||
const { runner: defaultRunner, key: defaultKey } = await runnerService.createRunner(team.id, {
|
||||
name: 'Default Runner',
|
||||
isDefault: true,
|
||||
})
|
||||
const { runner: gpuRunner, key: gpuKey } = await runnerService.createRunner(team.id, {
|
||||
name: 'GPU Runner',
|
||||
isDefault: false,
|
||||
})
|
||||
|
||||
const defaultConn = createMockWs()
|
||||
const gpuConn = createMockWs()
|
||||
await runnerService.registerConnection(
|
||||
{
|
||||
id: defaultRunner.id,
|
||||
teamId: team.id,
|
||||
name: defaultRunner.name,
|
||||
keyHash: hashRunnerKey(defaultKey),
|
||||
},
|
||||
defaultConn.ws,
|
||||
)
|
||||
await runnerService.registerConnection(
|
||||
{
|
||||
id: gpuRunner.id,
|
||||
teamId: team.id,
|
||||
name: gpuRunner.name,
|
||||
keyHash: hashRunnerKey(gpuKey),
|
||||
},
|
||||
gpuConn.ws,
|
||||
)
|
||||
reconcile(defaultRunner.id, defaultConn.ws)
|
||||
reconcile(gpuRunner.id, gpuConn.ws)
|
||||
|
||||
const defaultTool = createRunnerBashTool(
|
||||
{},
|
||||
{
|
||||
teamId: team.id,
|
||||
agentRunnerId: null,
|
||||
},
|
||||
)
|
||||
const gpuTool = createRunnerBashTool(
|
||||
{},
|
||||
{
|
||||
teamId: team.id,
|
||||
agentRunnerId: gpuRunner.id,
|
||||
},
|
||||
)
|
||||
|
||||
const pDefault = invokeTool(defaultTool, '1', { command: 'uname -a' })
|
||||
const pGpu = invokeTool(gpuTool, '2', { command: 'nvidia-smi' })
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(defaultConn.lastSentJson()?.type).toBe('exec.start')
|
||||
expect(gpuConn.lastSentJson()?.type).toBe('exec.start')
|
||||
})
|
||||
|
||||
const defaultMsg = defaultConn.lastSentJson()
|
||||
const gpuMsg = gpuConn.lastSentJson()
|
||||
expect(defaultMsg.command).toBe('uname -a')
|
||||
expect(gpuMsg.command).toBe('nvidia-smi')
|
||||
|
||||
runnerService.handleMessage(
|
||||
defaultRunner.id,
|
||||
defaultConn.ws,
|
||||
JSON.stringify({
|
||||
type: 'exec.result',
|
||||
executionId: defaultMsg.executionId,
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
aborted: false,
|
||||
}),
|
||||
)
|
||||
runnerService.handleMessage(
|
||||
gpuRunner.id,
|
||||
gpuConn.ws,
|
||||
JSON.stringify({
|
||||
type: 'exec.result',
|
||||
executionId: gpuMsg.executionId,
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
aborted: false,
|
||||
}),
|
||||
)
|
||||
|
||||
await Promise.all([pDefault, pGpu])
|
||||
runnerService.handleDisconnect(defaultRunner.id, defaultConn.ws)
|
||||
runnerService.handleDisconnect(gpuRunner.id, gpuConn.ws)
|
||||
})
|
||||
})
|
||||
@@ -1,324 +0,0 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { sandboxService } from '@shumai/core'
|
||||
import type { SandboxProvider, SandboxConfig, WrapCommandOptions } from '@shumai/core'
|
||||
import { createAgentSession, type DbProviderInfo } from './index'
|
||||
import { createSandboxedBashTool } from './tools/sandboxed-bash'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { spawn } from 'node:child_process'
|
||||
import { EventEmitter } from 'node:events'
|
||||
|
||||
vi.mock('node:child_process')
|
||||
|
||||
class TestSandboxProvider implements SandboxProvider {
|
||||
initialized = false
|
||||
config: SandboxConfig | null = null
|
||||
initializeCalls = 0
|
||||
updateConfigCalls = 0
|
||||
resetCalls = 0
|
||||
wrapCommandCalls: Array<{ command: string; options?: WrapCommandOptions }> = []
|
||||
violationsByCommandId = new Map<string, string>()
|
||||
|
||||
async initialize(config: SandboxConfig): Promise<void> {
|
||||
this.initialized = true
|
||||
this.config = config
|
||||
this.initializeCalls++
|
||||
}
|
||||
|
||||
async wrapCommand(command: string, options?: WrapCommandOptions): Promise<string> {
|
||||
this.wrapCommandCalls.push({ command, options })
|
||||
return `sandboxed-${command}`
|
||||
}
|
||||
|
||||
async updateConfig(config: Partial<SandboxConfig>): Promise<void> {
|
||||
this.updateConfigCalls++
|
||||
if (this.config && config.network) {
|
||||
this.config.network = { ...this.config.network, ...config.network }
|
||||
}
|
||||
}
|
||||
|
||||
async reset(): Promise<void> {
|
||||
this.resetCalls++
|
||||
this.initialized = false
|
||||
this.config = null
|
||||
}
|
||||
|
||||
isInitialized(): boolean {
|
||||
return this.initialized
|
||||
}
|
||||
|
||||
getBlockedHostForCommand(commandId: string): string | null {
|
||||
return this.violationsByCommandId.get(commandId) ?? null
|
||||
}
|
||||
}
|
||||
|
||||
const mockProviders: DbProviderInfo[] = [
|
||||
{
|
||||
name: 'google',
|
||||
config: { api: 'google-generative-ai', apiKey: 'GOOGLE_API_KEY' },
|
||||
models: [
|
||||
{
|
||||
modelId: 'gemini',
|
||||
name: 'Gemini',
|
||||
config: {
|
||||
api: 'google-generative-ai',
|
||||
reasoning: false,
|
||||
input: ['text'],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 8192,
|
||||
maxTokens: 4096,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
describe('Concurrent Sandbox Sessions', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
let testProvider: TestSandboxProvider
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks()
|
||||
testProvider = new TestSandboxProvider()
|
||||
sandboxService.setProvider(testProvider)
|
||||
})
|
||||
|
||||
it('does not reset sandbox when new sessions start while a command is running', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Concurrent Test Team' },
|
||||
})
|
||||
await prisma.sandbox.create({
|
||||
data: {
|
||||
teamId: team.id,
|
||||
networkSandboxEnabled: true,
|
||||
allowedDomains: ['api.github.com'],
|
||||
},
|
||||
})
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: 'agent-1',
|
||||
name: 'Agent User',
|
||||
email: 'agent-user@shumai.ai',
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: 'agent-1',
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'google', model: 'gemini' },
|
||||
},
|
||||
})
|
||||
|
||||
// 1. Session A starts and syncs domains
|
||||
await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: 'agent-1',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'Session A prompt',
|
||||
teamSkills: [],
|
||||
allowedDomains: ['api.github.com'],
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
expect(testProvider.initializeCalls).toBe(1)
|
||||
expect(testProvider.resetCalls).toBe(0)
|
||||
|
||||
// 2. Session A runs a bash command that takes time
|
||||
const tool = createSandboxedBashTool('/mock/cwd')
|
||||
|
||||
const mockStdout = new EventEmitter()
|
||||
const mockStderr = new EventEmitter()
|
||||
const mockChild = Object.assign(new EventEmitter(), {
|
||||
stdout: mockStdout,
|
||||
stderr: mockStderr,
|
||||
pid: 999,
|
||||
kill: vi.fn(),
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mock spawn return value
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
|
||||
const sessionExecutionA = tool.execute('call-session-a-1', {
|
||||
command: 'curl https://api.github.com/repos',
|
||||
source: 'skill',
|
||||
})
|
||||
|
||||
// 3. Concurrently, Session B starts with additional allowed domains
|
||||
await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: 'agent-1',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'Session B prompt',
|
||||
teamSkills: [],
|
||||
allowedDomains: ['api.github.com', 'pypi.org'],
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
// Assert sandbox was hot-reloaded without any reset!
|
||||
expect(testProvider.resetCalls).toBe(0)
|
||||
expect(testProvider.updateConfigCalls).toBe(1)
|
||||
expect(sandboxService.getCurrentAllowedDomains()).toEqual(['api.github.com', 'pypi.org'])
|
||||
|
||||
// 4. Concurrently, Session C starts with the same domains (should be no-op, no reset, no updateConfig)
|
||||
await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: 'agent-1',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'Session C prompt',
|
||||
teamSkills: [],
|
||||
allowedDomains: ['pypi.org', 'api.github.com'],
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
expect(testProvider.resetCalls).toBe(0)
|
||||
expect(testProvider.updateConfigCalls).toBe(1) // Still 1, no duplicate update
|
||||
|
||||
// 5. Session A's command completes successfully
|
||||
mockStdout.emit('data', Buffer.from('{"status":"ok"}'))
|
||||
mockChild.emit('close', 0)
|
||||
|
||||
const result = await sessionExecutionA
|
||||
expect(result.details).toEqual(
|
||||
expect.objectContaining({
|
||||
exitCode: 0,
|
||||
}),
|
||||
)
|
||||
|
||||
// Verify command was wrapped with options containing commandId: 'call-session-a-1'
|
||||
expect(testProvider.wrapCommandCalls).toContainEqual({
|
||||
command: 'curl https://api.github.com/repos',
|
||||
options: { commandId: 'call-session-a-1' },
|
||||
})
|
||||
})
|
||||
|
||||
it('isolates blocked host violations per commandId during concurrent execution', async () => {
|
||||
await sandboxService.ensureInitialized({ allowedDomains: ['api.github.com'] })
|
||||
|
||||
// Command 1 and Command 2 run concurrently
|
||||
await sandboxService.wrapCommand('curl http://blocked1.com', { commandId: 'cmd-1' })
|
||||
await sandboxService.wrapCommand('curl http://blocked2.com', { commandId: 'cmd-2' })
|
||||
|
||||
testProvider.violationsByCommandId.set('cmd-1', 'blocked1.com')
|
||||
testProvider.violationsByCommandId.set('cmd-2', 'blocked2.com')
|
||||
|
||||
// Blocked hosts should be accurately isolated
|
||||
expect(sandboxService.getBlockedHost('cmd-1')).toBe('blocked1.com')
|
||||
expect(sandboxService.getBlockedHost('cmd-2')).toBe('blocked2.com')
|
||||
expect(sandboxService.getBlockedHost('cmd-unknown')).toBe('')
|
||||
})
|
||||
|
||||
it('does not reject concurrent allowed command when another command is blocked', async () => {
|
||||
await sandboxService.ensureInitialized({ allowedDomains: ['api.github.com'] })
|
||||
|
||||
const tool = createSandboxedBashTool('/mock/cwd', undefined, {
|
||||
getBlockedHost: (commandId?: string) => sandboxService.getBlockedHost(commandId),
|
||||
clearBlockedHost: () => sandboxService.clearBlockedHost(),
|
||||
})
|
||||
|
||||
const childBlocked = Object.assign(new EventEmitter(), {
|
||||
stdout: new EventEmitter(),
|
||||
stderr: new EventEmitter(),
|
||||
pid: 101,
|
||||
kill: vi.fn(),
|
||||
})
|
||||
const childAllowed = Object.assign(new EventEmitter(), {
|
||||
stdout: new EventEmitter(),
|
||||
stderr: new EventEmitter(),
|
||||
pid: 102,
|
||||
kill: vi.fn(),
|
||||
})
|
||||
|
||||
let spawnIndex = 0
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mock spawn implementation
|
||||
;(spawn as any).mockImplementation(() => {
|
||||
spawnIndex++
|
||||
return spawnIndex === 1 ? childBlocked : childAllowed
|
||||
})
|
||||
|
||||
// Record violation for the blocked command only
|
||||
testProvider.violationsByCommandId.set('call-blocked', 'blocked-target.com')
|
||||
|
||||
const blockedExecution = tool.execute('call-blocked', {
|
||||
command: 'curl http://blocked-target.com',
|
||||
source: 'skill',
|
||||
})
|
||||
const allowedExecution = tool.execute('call-allowed', {
|
||||
command: 'curl https://api.github.com/data',
|
||||
source: 'skill',
|
||||
})
|
||||
|
||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalledTimes(2))
|
||||
|
||||
childBlocked.stdout.emit('data', Buffer.from('blocked output'))
|
||||
childBlocked.emit('close', 0)
|
||||
|
||||
childAllowed.stdout.emit('data', Buffer.from('allowed output'))
|
||||
childAllowed.emit('close', 0)
|
||||
|
||||
await expect(blockedExecution).rejects.toThrow(
|
||||
'Network request to blocked-target.com is blocked',
|
||||
)
|
||||
const allowedResult = await allowedExecution
|
||||
expect(allowedResult.details).toEqual(expect.objectContaining({ exitCode: 0 }))
|
||||
})
|
||||
|
||||
it('does not synchronize or overwrite sandbox allowlist when allowedDomains is omitted (e.g. naming sessions)', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Omitted Domains Team' },
|
||||
})
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: 'agent-omitted',
|
||||
name: 'Agent User',
|
||||
email: 'agent-omitted@shumai.ai',
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: 'agent-omitted',
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'google', model: 'gemini' },
|
||||
},
|
||||
})
|
||||
|
||||
// 1. First session sets allowedDomains to ['*.github.com']
|
||||
await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: 'agent-omitted',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'Main prompt',
|
||||
teamSkills: [],
|
||||
allowedDomains: ['*.github.com'],
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
expect(sandboxService.getCurrentAllowedDomains()).toEqual(['*.github.com'])
|
||||
const updateCallsBefore = testProvider.updateConfigCalls
|
||||
|
||||
// 2. Utility / naming session runs WITHOUT allowedDomains
|
||||
await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: 'agent-omitted',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'Title generator',
|
||||
teamSkills: [],
|
||||
// allowedDomains omitted
|
||||
disableTools: true,
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
// Assert sandbox was NOT touched or overwritten
|
||||
expect(testProvider.updateConfigCalls).toBe(updateCallsBefore)
|
||||
expect(sandboxService.getCurrentAllowedDomains()).toEqual(['*.github.com'])
|
||||
})
|
||||
})
|
||||
@@ -1,820 +0,0 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { type SessionTreeEntry } from '@earendil-works/pi-agent-core'
|
||||
import { describe, expect, it, vi, afterEach } from 'vitest'
|
||||
import { DatabaseSessionStorage } from './database-session-storage'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import { createAgentSession, type DbProviderInfo } from './index'
|
||||
import * as sandboxedBashModule from './tools/sandboxed-bash'
|
||||
import * as operationToolsModule from './operation-tools'
|
||||
|
||||
describe('DatabaseSessionStorage', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
const mockModelConfig: PrismaJson.ModelConfig = {
|
||||
reasoning: false,
|
||||
input: ['text'],
|
||||
contextWindow: 128000,
|
||||
maxTokens: 4096,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
}
|
||||
|
||||
const mockProviders: DbProviderInfo[] = [
|
||||
{
|
||||
name: 'test-provider',
|
||||
config: { api: 'openai-responses', apiKey: 'TEST_KEY' },
|
||||
models: [
|
||||
{
|
||||
modelId: 'test-model',
|
||||
name: 'Test Model',
|
||||
config: mockModelConfig,
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
const setupTestData = async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const provider = await prisma.provider.create({
|
||||
data: {
|
||||
name: 'test-provider',
|
||||
teamId: team.id,
|
||||
config: { api: 'openai-responses' },
|
||||
},
|
||||
})
|
||||
const model = await prisma.model.create({
|
||||
data: {
|
||||
modelId: 'test-model',
|
||||
name: 'Test Model',
|
||||
providerId: provider.id,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
config: mockModelConfig as any,
|
||||
},
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Test User', email: 'test@example.com', type: 'human' },
|
||||
})
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: user.id,
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
providerId: provider.id,
|
||||
modelId: model.id,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
config: { provider: 'test-provider', model: 'test-model' } as any,
|
||||
},
|
||||
})
|
||||
return { team, user, agent }
|
||||
}
|
||||
|
||||
it('should create and retrieve metadata', async () => {
|
||||
const { agent, user } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({
|
||||
agentId: agent.id,
|
||||
userId: user.id,
|
||||
cwd: '/test/cwd',
|
||||
})
|
||||
|
||||
const metadata = await storage.getMetadata()
|
||||
expect(metadata.agentId).toBe(agent.id)
|
||||
expect(metadata.userId).toBe(user.id)
|
||||
expect(metadata.cwd).toBe('/test/cwd')
|
||||
expect(metadata.id).toBeDefined()
|
||||
expect(metadata.createdAt).toBeDefined()
|
||||
})
|
||||
|
||||
it('should manage leafId', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
expect(await storage.getLeafId()).toBeNull()
|
||||
|
||||
const entry: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: 'entry-1',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: 'Test' }],
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
}
|
||||
await storage.appendEntry(entry)
|
||||
expect(await storage.getLeafId()).toBe('entry-1')
|
||||
|
||||
await storage.setLeafId(null)
|
||||
expect(await storage.getLeafId()).toBeNull()
|
||||
})
|
||||
|
||||
it('should append and retrieve entries', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
const entry: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: 'msg-1',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: 'hello' }],
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
}
|
||||
|
||||
await storage.appendEntry(entry)
|
||||
|
||||
// Verify leafId was auto-updated
|
||||
expect(await storage.getLeafId()).toBe('msg-1')
|
||||
|
||||
const retrieved = await storage.getEntry('msg-1')
|
||||
expect(retrieved).toEqual(entry)
|
||||
|
||||
const allEntries = await storage.getEntries()
|
||||
expect(allEntries).toHaveLength(1)
|
||||
expect(allEntries[0]).toEqual(entry)
|
||||
})
|
||||
|
||||
it('should find entries by type', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
await storage.appendEntry({
|
||||
type: 'message',
|
||||
id: 'msg-1',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: { role: 'user', content: [], timestamp: Date.now() },
|
||||
})
|
||||
|
||||
await storage.appendEntry({
|
||||
type: 'thinking_level_change',
|
||||
id: 'think-1',
|
||||
parentId: 'msg-1',
|
||||
timestamp: new Date().toISOString(),
|
||||
thinkingLevel: 'high',
|
||||
})
|
||||
|
||||
const messages = await storage.findEntries('message')
|
||||
expect(messages).toHaveLength(1)
|
||||
expect(messages[0].id).toBe('msg-1')
|
||||
|
||||
const thinkChanges = await storage.findEntries('thinking_level_change')
|
||||
expect(thinkChanges).toHaveLength(1)
|
||||
expect(thinkChanges[0].id).toBe('think-1')
|
||||
})
|
||||
|
||||
it('should retrieve path to root', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
await storage.appendEntry({
|
||||
type: 'message',
|
||||
id: 'msg-1',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: { role: 'user', content: [], timestamp: Date.now() },
|
||||
})
|
||||
|
||||
await storage.appendEntry({
|
||||
type: 'message',
|
||||
id: 'msg-2',
|
||||
parentId: 'msg-1',
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: [],
|
||||
timestamp: Date.now(),
|
||||
provider: 'p',
|
||||
model: 'm',
|
||||
api: 'api',
|
||||
usage: { input: 0, output: 0 },
|
||||
// AssistantMessage has many internal fields from pi-ai
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any,
|
||||
})
|
||||
|
||||
await storage.appendEntry({
|
||||
type: 'message',
|
||||
id: 'msg-3',
|
||||
parentId: 'msg-2',
|
||||
timestamp: new Date().toISOString(),
|
||||
message: { role: 'user', content: [], timestamp: Date.now() },
|
||||
})
|
||||
|
||||
const path = await storage.getPathToRoot('msg-3')
|
||||
expect(path).toHaveLength(3)
|
||||
expect(path[0].id).toBe('msg-1')
|
||||
expect(path[1].id).toBe('msg-2')
|
||||
expect(path[2].id).toBe('msg-3')
|
||||
})
|
||||
|
||||
it('should strip and reinject image data', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
const getObjectSpy = vi.spyOn(s3Service, 'getObject').mockResolvedValue({
|
||||
buffer: Buffer.from('reinjected-data'),
|
||||
contentType: 'image/png',
|
||||
// S3Object has complex metadata requirements not needed for this test
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any)
|
||||
|
||||
const entry: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: 'msg-1',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'toolResult',
|
||||
toolCallId: 'call-1',
|
||||
toolName: 'tool',
|
||||
isError: false,
|
||||
timestamp: Date.now(),
|
||||
content: [{ type: 'image', data: 'original-base64-data', mimeType: 'image/png' }],
|
||||
details: {
|
||||
sourceKeys: ['path/to/image.png'],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
await storage.appendEntry(entry)
|
||||
|
||||
// Record data is generic Json
|
||||
const record = await prisma.agentSessionEntry.findUnique({ where: { id: 'msg-1' } })
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const savedEntry = record?.data as any
|
||||
expect(savedEntry.message.content[0].data).toBe('__S3_DATA__')
|
||||
|
||||
// Verify retrieval reinjects data
|
||||
const retrieved = await storage.getEntry('msg-1')
|
||||
expect(getObjectSpy).toHaveBeenCalledWith('shumai', 'path/to/image.png')
|
||||
// retrieved is a SessionTreeEntry, but we need to access internal message properties for verification
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((retrieved as any).message.content[0].data).toBe(
|
||||
Buffer.from('reinjected-data').toString('base64'),
|
||||
)
|
||||
})
|
||||
|
||||
it('should strip and reinject skill content', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
const getSkillContentSpy = vi
|
||||
.spyOn(agentService, 'getSkillContent')
|
||||
.mockResolvedValue('# Mocked Skill Content')
|
||||
|
||||
const entry: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: 'msg-skill',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'toolResult',
|
||||
toolCallId: 'call-2',
|
||||
toolName: 'read_skill',
|
||||
isError: false,
|
||||
timestamp: Date.now(),
|
||||
content: [{ type: 'text', text: 'original-skill-content' }],
|
||||
details: {
|
||||
skillId: 'some-skill-id',
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
await storage.appendEntry(entry)
|
||||
|
||||
// Verify saved entry is stripped
|
||||
const record = await prisma.agentSessionEntry.findUnique({ where: { id: 'msg-skill' } })
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const savedEntry = record?.data as any
|
||||
expect(savedEntry.message.content[0].text).toBe('__SKILL_CONTENT__')
|
||||
|
||||
// Verify retrieval reinjects data
|
||||
const retrieved = await storage.getEntry('msg-skill')
|
||||
expect(getSkillContentSpy).toHaveBeenCalledWith('some-skill-id')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((retrieved as any).message.content[0].text).toBe('# Mocked Skill Content')
|
||||
})
|
||||
|
||||
it('should restore environment variables when resuming session', async () => {
|
||||
const { agent, user } = await setupTestData()
|
||||
|
||||
// 1. Create a session first to get a sessionId
|
||||
const initialStorage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
const sessionId = initialStorage.sessionId
|
||||
|
||||
// 2. Append a successful read_skill entry
|
||||
const entry: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: 'msg-skill-env',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'toolResult',
|
||||
toolCallId: 'call-env-1',
|
||||
toolName: 'read_skill',
|
||||
isError: false,
|
||||
timestamp: Date.now(),
|
||||
content: [{ type: 'text', text: 'skill content' }],
|
||||
details: {
|
||||
skillId: 'env-skill-id',
|
||||
},
|
||||
},
|
||||
}
|
||||
await initialStorage.appendEntry(entry)
|
||||
|
||||
// 3. Mock getSkillEnvs to return environment variables
|
||||
const getSkillEnvsSpy = vi.spyOn(agentService, 'getSkillEnvs').mockResolvedValue({
|
||||
MOCK_ENV_VAR: 'mocked-value',
|
||||
})
|
||||
|
||||
// 4. Mock agentService.getSkillContent to avoid actual download throws
|
||||
vi.spyOn(agentService, 'getSkillContent').mockResolvedValue('# Skill')
|
||||
|
||||
// 5. Spy on createSandboxedBashTool
|
||||
const createSandboxedBashToolSpy = vi.spyOn(sandboxedBashModule, 'createSandboxedBashTool')
|
||||
|
||||
// 6. Resume the session via createAgentSession
|
||||
await createAgentSession({
|
||||
teamId: 't1',
|
||||
agentId: agent.id,
|
||||
providerName: 'test-provider',
|
||||
modelId: 'test-model',
|
||||
systemPrompt: 'Test prompt',
|
||||
teamSkills: [],
|
||||
allowedDomains: [],
|
||||
sessionId: sessionId,
|
||||
userId: user.id,
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
// 7. Verify skill environment variables are restored.
|
||||
expect(getSkillEnvsSpy).toHaveBeenCalledWith('env-skill-id')
|
||||
expect(createSandboxedBashToolSpy).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
expect.objectContaining({
|
||||
MOCK_ENV_VAR: 'mocked-value',
|
||||
}),
|
||||
expect.any(Object),
|
||||
)
|
||||
})
|
||||
|
||||
it('should retrieve label', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
await storage.appendEntry({
|
||||
type: 'label',
|
||||
id: 'label-1',
|
||||
targetId: 'target-1',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
label: 'Test Label',
|
||||
})
|
||||
|
||||
const label = await storage.getLabel('label-1')
|
||||
expect(label).toBe('Test Label')
|
||||
})
|
||||
|
||||
it('should retrieve entries in order', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
// Insert entries out of order (by insertion time) but with ordered IDs
|
||||
await storage.appendEntry({
|
||||
type: 'message',
|
||||
id: '01B',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: { role: 'user', content: [], timestamp: Date.now() },
|
||||
})
|
||||
|
||||
await storage.appendEntry({
|
||||
type: 'message',
|
||||
id: '01A',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: { role: 'user', content: [], timestamp: Date.now() },
|
||||
})
|
||||
|
||||
await storage.appendEntry({
|
||||
type: 'message',
|
||||
id: '01C',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: { role: 'user', content: [], timestamp: Date.now() },
|
||||
})
|
||||
|
||||
const entries = await storage.getEntries()
|
||||
expect(entries).toHaveLength(3)
|
||||
expect(entries[0].id).toBe('01A')
|
||||
expect(entries[1].id).toBe('01B')
|
||||
expect(entries[2].id).toBe('01C')
|
||||
})
|
||||
|
||||
it('should use the current comment ID for media tools in subsequent turns (image)', async () => {
|
||||
const { agent, user, team } = await setupTestData()
|
||||
|
||||
// Create a project and asset to satisfy FK constraints
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
id: 'asset-123',
|
||||
name: 'test.png',
|
||||
type: 'file',
|
||||
mediaType: 'image/png',
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- partial MediaInfo mock for testing
|
||||
media: { proxyType: 'image' } as any,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// 1. Create a session with an original comment ID and an asset ID using the storage class
|
||||
const initialStorage = await DatabaseSessionStorage.create({
|
||||
agentId: agent.id,
|
||||
userId: user.id,
|
||||
cwd: '/test/cwd',
|
||||
assetId: asset.id,
|
||||
userCommentId: 'original-comment-123',
|
||||
})
|
||||
const sessionId = initialStorage.sessionId
|
||||
|
||||
// 2. Spy on the Operation tools builder (read_asset comes from the Operation registry)
|
||||
const buildOperationToolsSpy = vi.spyOn(operationToolsModule, 'buildOperationTools')
|
||||
|
||||
// 3. Resume the session via createAgentSession with a NEW comment ID
|
||||
await createAgentSession({
|
||||
teamId: 't1',
|
||||
agentId: agent.id,
|
||||
providerName: 'test-provider',
|
||||
modelId: 'test-model',
|
||||
systemPrompt: 'Test prompt',
|
||||
teamSkills: [],
|
||||
allowedDomains: [],
|
||||
sessionId: sessionId,
|
||||
userId: user.id,
|
||||
userCommentId: 'new-comment-456',
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
// 4. Assert that the tools were built for the user
|
||||
expect(buildOperationToolsSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ userId: user.id }),
|
||||
expect.objectContaining({}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should instantiate media tools with user ID in subsequent turns (video)', async () => {
|
||||
const { agent, user, team } = await setupTestData()
|
||||
|
||||
// Create a project and asset to satisfy FK constraints
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
id: 'asset-456',
|
||||
name: 'test.mp4',
|
||||
type: 'file',
|
||||
mediaType: 'video/mp4',
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- partial MediaInfo mock for testing
|
||||
media: { proxyType: 'video' } as any,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// 1. Create a session with an original comment ID and an asset ID using the storage class
|
||||
const initialStorage = await DatabaseSessionStorage.create({
|
||||
agentId: agent.id,
|
||||
userId: user.id,
|
||||
cwd: '/test/cwd',
|
||||
assetId: asset.id,
|
||||
userCommentId: 'original-comment-123',
|
||||
})
|
||||
const sessionId = initialStorage.sessionId
|
||||
|
||||
// 2. Spy on the Operation tools builder (read_asset comes from the Operation registry)
|
||||
const buildOperationToolsSpy = vi.spyOn(operationToolsModule, 'buildOperationTools')
|
||||
|
||||
// 3. Resume the session via createAgentSession with a NEW comment ID
|
||||
await createAgentSession({
|
||||
teamId: 't1',
|
||||
agentId: agent.id,
|
||||
providerName: 'test-provider',
|
||||
modelId: 'test-model',
|
||||
systemPrompt: 'Test prompt',
|
||||
teamSkills: [],
|
||||
allowedDomains: [],
|
||||
sessionId: sessionId,
|
||||
userId: user.id,
|
||||
userCommentId: 'new-comment-456',
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
// 4. Assert that the tools were built for the user
|
||||
expect(buildOperationToolsSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ userId: user.id }),
|
||||
expect.objectContaining({}),
|
||||
)
|
||||
})
|
||||
|
||||
describe('P1 Bug Reproductions', () => {
|
||||
it('should persist a leaf entry and validate targetId when setLeafId is called', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
const msgEntry: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: 'msg-100',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: 'Hello' }],
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
}
|
||||
await storage.appendEntry(msgEntry)
|
||||
expect(await storage.getLeafId()).toBe('msg-100')
|
||||
|
||||
// setLeafId to an existing entry
|
||||
await storage.setLeafId('msg-100')
|
||||
|
||||
// Verify that a 'leaf' entry was appended to database entries
|
||||
const entries = await storage.getEntries()
|
||||
const leafEntries = entries.filter((e) => e.type === 'leaf')
|
||||
expect(leafEntries.length).toBe(1)
|
||||
expect(leafEntries[0]).toMatchObject({
|
||||
type: 'leaf',
|
||||
targetId: 'msg-100',
|
||||
})
|
||||
|
||||
// Calling setLeafId with non-existent targetId should throw not_found error
|
||||
await expect(storage.setLeafId('non-existent-id')).rejects.toThrow(
|
||||
'Entry non-existent-id not found',
|
||||
)
|
||||
})
|
||||
|
||||
it('should execute appendEntry in a single atomic transaction', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
// Mock prisma.agentSession.update to throw an error during setLeafId inside appendEntry
|
||||
const updateSpy = vi
|
||||
.spyOn(prisma.agentSession, 'update')
|
||||
.mockRejectedValueOnce(new Error('DB Update Failed'))
|
||||
|
||||
const entry: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: 'atomic-msg-1',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: 'Atomic test' }],
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
}
|
||||
|
||||
await expect(storage.appendEntry(entry)).rejects.toThrow('DB Update Failed')
|
||||
|
||||
// Because appendEntry should be atomic, atomic-msg-1 must NOT exist in the database
|
||||
const dbEntry = await prisma.agentSessionEntry.findUnique({
|
||||
where: { id: 'atomic-msg-1' },
|
||||
})
|
||||
expect(dbEntry).toBeNull()
|
||||
|
||||
updateSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('should return session name from agentSession record', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
expect(await storage.getSessionName()).toBeUndefined()
|
||||
|
||||
await prisma.agentSession.update({
|
||||
where: { id: storage.sessionId },
|
||||
data: { name: 'My Chat Session' },
|
||||
})
|
||||
|
||||
expect(await storage.getSessionName()).toBe('My Chat Session')
|
||||
})
|
||||
|
||||
it('should aggregate session stats correctly', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
const msg1: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: 'stats-msg-1',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: 'Hello' }],
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
}
|
||||
|
||||
const msg2: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: 'stats-msg-2',
|
||||
parentId: 'stats-msg-1',
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: [{ type: 'text', text: 'Hi there!' }],
|
||||
api: 'openai-responses',
|
||||
provider: 'test-provider',
|
||||
model: 'test-model',
|
||||
stopReason: 'stop',
|
||||
timestamp: Date.now(),
|
||||
usage: {
|
||||
input: 100,
|
||||
output: 50,
|
||||
cacheRead: 20,
|
||||
cacheWrite: 0,
|
||||
totalTokens: 170,
|
||||
cost: { input: 0.01, output: 0.02, cacheRead: 0.002, cacheWrite: 0, total: 0.032 },
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
await storage.appendEntry(msg1)
|
||||
await storage.appendEntry(msg2)
|
||||
|
||||
const stats = await storage.getSessionStats()
|
||||
expect(stats.messageCount).toBe(2)
|
||||
expect(stats.cachedTokens).toBe(20)
|
||||
expect(stats.uncachedTokens).toBe(150)
|
||||
expect(stats.totalTokens).toBe(170)
|
||||
expect(stats.costTotal).toBeCloseTo(0.032)
|
||||
})
|
||||
|
||||
it('should save user message turn as custom_message (shumai_message) in DB when currentMessageContext is set', async () => {
|
||||
const { agent, user } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id, userId: user.id })
|
||||
|
||||
storage.currentMessageContext = {
|
||||
user: { id: user.id, name: user.name, role: 'owner' },
|
||||
currentAsset: { id: 'asset-123', name: 'video.mp4', type: 'file' },
|
||||
}
|
||||
|
||||
const userEntry: SessionTreeEntry = {
|
||||
type: 'message',
|
||||
id: 'msg-user-1',
|
||||
parentId: null,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: {
|
||||
role: 'user',
|
||||
content: 'Hello, help me analyze this video',
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
}
|
||||
|
||||
await storage.appendEntry(userEntry)
|
||||
|
||||
// 1. Verify DB record type is 'custom_message', not 'message'
|
||||
const record = await prisma.agentSessionEntry.findUnique({
|
||||
where: { id: 'msg-user-1' },
|
||||
})
|
||||
expect(record).toBeDefined()
|
||||
expect(record?.type).toBe('custom_message')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const data = record?.data as any
|
||||
expect(data.customType).toBe('shumai_message')
|
||||
expect(data.content).toBe('Hello, help me analyze this video')
|
||||
expect(data.details?.user?.id).toBe(user.id)
|
||||
expect(data.details?.currentAsset?.id).toBe('asset-123')
|
||||
|
||||
// 2. Verify getEntries() and getPathToRoot() do not throw and return proper custom_message
|
||||
const entries = await storage.getEntries()
|
||||
expect(entries).toHaveLength(1)
|
||||
expect(entries[0].type).toBe('custom_message')
|
||||
|
||||
const pathEntries = await storage.getPathToRoot('msg-user-1')
|
||||
expect(pathEntries).toHaveLength(1)
|
||||
expect(pathEntries[0].type).toBe('custom_message')
|
||||
|
||||
const stats = await storage.getSessionStats()
|
||||
expect(stats.messageCount).toBe(1)
|
||||
})
|
||||
|
||||
it('should safely handle legacy records where type was message but data was shumai_message', async () => {
|
||||
const { agent } = await setupTestData()
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
|
||||
|
||||
await prisma.agentSessionEntry.create({
|
||||
data: {
|
||||
id: 'legacy-corrupted-entry',
|
||||
sessionId: storage.sessionId,
|
||||
type: 'message',
|
||||
data: {
|
||||
customType: 'shumai_message',
|
||||
content: 'Legacy prompt',
|
||||
display: true,
|
||||
details: { user: { id: 'u1', name: 'User 1' } },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// Must not throw in getEntries(), getPathToRoot(), or getSessionStats()
|
||||
const entries = await storage.getEntries()
|
||||
expect(entries).toHaveLength(1)
|
||||
expect(entries[0].type).toBe('custom_message')
|
||||
|
||||
const path = await storage.getPathToRoot('legacy-corrupted-entry')
|
||||
expect(path).toHaveLength(1)
|
||||
expect(path[0].type).toBe('custom_message')
|
||||
|
||||
const stats = await storage.getSessionStats()
|
||||
expect(stats.messageCount).toBe(1)
|
||||
})
|
||||
|
||||
it('should dynamically inject thread metadata into details.thread in getPathToRoot when replies exist', async () => {
|
||||
const { agent, user } = await setupTestData()
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: agent.teamId },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Test Asset',
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
projectId: project.id,
|
||||
},
|
||||
})
|
||||
const storage = await DatabaseSessionStorage.create({ agentId: agent.id, assetId: asset.id })
|
||||
|
||||
const rootComment = await prisma.assetComment.create({
|
||||
data: {
|
||||
id: 'comment-root-1',
|
||||
assetId: asset.id,
|
||||
message: 'Top-level question',
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
|
||||
// Add a reply
|
||||
await prisma.assetComment.create({
|
||||
data: {
|
||||
id: 'comment-reply-1',
|
||||
assetId: asset.id,
|
||||
message: 'Reply 1',
|
||||
creatorId: user.id,
|
||||
replyToId: rootComment.id,
|
||||
},
|
||||
})
|
||||
|
||||
// Create session entry for root comment
|
||||
await prisma.agentSessionEntry.create({
|
||||
data: {
|
||||
id: rootComment.id,
|
||||
sessionId: storage.sessionId,
|
||||
assetId: asset.id,
|
||||
type: 'custom_message',
|
||||
data: {
|
||||
customType: 'shumai_message',
|
||||
content: 'Top-level question',
|
||||
display: true,
|
||||
details: { user: { id: user.id, name: user.name } },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const path = await storage.getPathToRoot(rootComment.id)
|
||||
expect(path).toHaveLength(1)
|
||||
expect(path[0].type).toBe('custom_message')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const details = (path[0] as any).details
|
||||
expect(details.thread).toEqual({
|
||||
id: rootComment.id,
|
||||
replyCount: 1,
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((path[0] as any).content).toBe('Top-level question')
|
||||
|
||||
const entries = await storage.getEntries()
|
||||
expect(entries).toHaveLength(1)
|
||||
expect(entries[0].type).toBe('custom_message')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((entries[0] as any).details?.thread).toEqual({
|
||||
id: rootComment.id,
|
||||
replyCount: 1,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,511 +0,0 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { ulid } from 'ulid'
|
||||
import {
|
||||
type AgentMessage,
|
||||
type SessionMetadata,
|
||||
type SessionStorage,
|
||||
type SessionTreeEntry,
|
||||
} from '@earendil-works/pi-agent-core'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import type { ShumaiMessageContext } from '@shumai/dtos'
|
||||
|
||||
export interface DatabaseSessionMetadata extends SessionMetadata {
|
||||
agentId: string
|
||||
userId?: string
|
||||
cwd: string
|
||||
assetId?: string
|
||||
userCommentId?: string
|
||||
}
|
||||
|
||||
export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMetadata> {
|
||||
constructor(public readonly sessionId: string) {}
|
||||
|
||||
async getMetadata(): Promise<DatabaseSessionMetadata> {
|
||||
const session = await prisma.agentSession.findUnique({
|
||||
where: { id: this.sessionId },
|
||||
})
|
||||
if (!session) throw new Error(`Session ${this.sessionId} not found`)
|
||||
return {
|
||||
id: session.id,
|
||||
createdAt: session.createdAt.toISOString(),
|
||||
agentId: session.agentId,
|
||||
userId: session.userId || undefined,
|
||||
cwd: session.cwd,
|
||||
assetId: session.assetId || undefined,
|
||||
userCommentId: session.userCommentId || undefined,
|
||||
}
|
||||
}
|
||||
|
||||
async getLeafId(): Promise<string | null> {
|
||||
const session = await prisma.agentSession.findUnique({
|
||||
where: { id: this.sessionId },
|
||||
select: { leafId: true },
|
||||
})
|
||||
return session?.leafId || null
|
||||
}
|
||||
|
||||
async setLeafId(leafId: string | null): Promise<void> {
|
||||
if (leafId === null) {
|
||||
await prisma.agentSession.update({
|
||||
where: { id: this.sessionId },
|
||||
data: { leafId: null },
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const existing = await prisma.agentSessionEntry.findFirst({
|
||||
where: { id: leafId },
|
||||
})
|
||||
if (!existing) {
|
||||
throw new Error(`Entry ${leafId} not found`)
|
||||
}
|
||||
|
||||
const currentLeafId = await this.getLeafId()
|
||||
const leafEntry: SessionTreeEntry = {
|
||||
type: 'leaf',
|
||||
id: await this.createEntryId(),
|
||||
parentId: currentLeafId,
|
||||
timestamp: new Date().toISOString(),
|
||||
targetId: leafId,
|
||||
}
|
||||
await this.appendEntry(leafEntry)
|
||||
}
|
||||
|
||||
nextEntryId?: string | null
|
||||
currentMessageContext?: PrismaJson.ShumaiMessageContext
|
||||
|
||||
async createEntryId(): Promise<string> {
|
||||
if (this.nextEntryId) {
|
||||
const id = this.nextEntryId
|
||||
this.nextEntryId = null
|
||||
return id
|
||||
}
|
||||
return ulid()
|
||||
}
|
||||
|
||||
private recordToEntry(record: {
|
||||
id: string
|
||||
type: string | null
|
||||
parentId: string | null
|
||||
createdAt: Date
|
||||
data: unknown
|
||||
}): SessionTreeEntry {
|
||||
const payload = (record.data as Record<string, unknown>) || {}
|
||||
let type = (record.type || 'message') as SessionTreeEntry['type']
|
||||
if (type === 'message' && payload.customType === 'shumai_message') {
|
||||
type = 'custom_message'
|
||||
}
|
||||
return {
|
||||
id: record.id,
|
||||
type,
|
||||
parentId: record.parentId,
|
||||
timestamp: record.createdAt.toISOString(),
|
||||
...payload,
|
||||
} as unknown as SessionTreeEntry
|
||||
}
|
||||
|
||||
async appendEntry(entry: SessionTreeEntry): Promise<void> {
|
||||
let entryToProcess = entry
|
||||
if (
|
||||
entryToProcess.type === 'message' &&
|
||||
entryToProcess.message &&
|
||||
entryToProcess.message.role === 'user' &&
|
||||
this.currentMessageContext
|
||||
) {
|
||||
let contentText = ''
|
||||
if (typeof entryToProcess.message.content === 'string') {
|
||||
contentText = entryToProcess.message.content
|
||||
} else if (Array.isArray(entryToProcess.message.content)) {
|
||||
contentText = entryToProcess.message.content
|
||||
.map((c) =>
|
||||
typeof c === 'object' && c && 'text' in c && typeof c.text === 'string' ? c.text : '',
|
||||
)
|
||||
.join('')
|
||||
}
|
||||
|
||||
entryToProcess = {
|
||||
type: 'custom_message',
|
||||
id: entryToProcess.id,
|
||||
parentId: entryToProcess.parentId,
|
||||
timestamp: entryToProcess.timestamp,
|
||||
customType: 'shumai_message',
|
||||
content: contentText,
|
||||
display: true,
|
||||
details: {
|
||||
...this.currentMessageContext,
|
||||
id: entryToProcess.id,
|
||||
},
|
||||
} as unknown as SessionTreeEntry
|
||||
this.currentMessageContext = undefined
|
||||
}
|
||||
|
||||
const strippedEntry = structuredClone(entryToProcess)
|
||||
|
||||
// Strip image data and skill content before saving to DB
|
||||
if (strippedEntry.type === 'message' && strippedEntry.message) {
|
||||
const msg = strippedEntry.message
|
||||
if (msg.role === 'toolResult') {
|
||||
const details = msg.details as { sourceKeys?: string[]; skillId?: string } | undefined
|
||||
|
||||
// Strip S3 image data
|
||||
if (Array.isArray(msg.content) && details?.sourceKeys) {
|
||||
const sourceKeys = details.sourceKeys
|
||||
let keyIdx = 0
|
||||
msg.content.forEach((item: { type: string; data?: string }) => {
|
||||
if (item.type === 'image' && sourceKeys[keyIdx]) {
|
||||
item.data = '__S3_DATA__'
|
||||
keyIdx++
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Strip skill content
|
||||
if (msg.toolName === 'read_skill' && !msg.isError) {
|
||||
if (details?.skillId && Array.isArray(msg.content)) {
|
||||
msg.content.forEach((item: { type: string; text?: string }) => {
|
||||
if (
|
||||
item.type === 'text' &&
|
||||
item.text &&
|
||||
!item.text.startsWith('Skill with ID') &&
|
||||
!item.text.startsWith('Skill downloaded but') &&
|
||||
!item.text.startsWith('Error reading skill')
|
||||
) {
|
||||
item.text = '__SKILL_CONTENT__'
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const targetLeafId =
|
||||
strippedEntry.type === 'leaf'
|
||||
? (strippedEntry as unknown as { targetId?: string }).targetId || strippedEntry.id
|
||||
: strippedEntry.id
|
||||
|
||||
// Extract base fields and store type-specific properties in data payload
|
||||
const payload = { ...strippedEntry } as Record<string, unknown>
|
||||
delete payload.id
|
||||
delete payload.type
|
||||
delete payload.parentId
|
||||
delete payload.timestamp
|
||||
|
||||
const session = await prisma.agentSession.findUnique({
|
||||
where: { id: this.sessionId },
|
||||
select: { assetId: true },
|
||||
})
|
||||
|
||||
await prisma.$transaction([
|
||||
prisma.agentSessionEntry.upsert({
|
||||
where: { id: strippedEntry.id },
|
||||
create: {
|
||||
id: strippedEntry.id,
|
||||
sessionId: this.sessionId,
|
||||
assetId: session?.assetId || null,
|
||||
type: strippedEntry.type,
|
||||
parentId: strippedEntry.parentId || null,
|
||||
createdAt: strippedEntry.timestamp ? new Date(strippedEntry.timestamp) : new Date(),
|
||||
data: payload as PrismaJson.PiSessionEntryData,
|
||||
},
|
||||
update: {
|
||||
type: strippedEntry.type,
|
||||
data: payload as PrismaJson.PiSessionEntryData,
|
||||
},
|
||||
}),
|
||||
prisma.agentSession.update({
|
||||
where: { id: this.sessionId },
|
||||
data: { leafId: targetLeafId },
|
||||
}),
|
||||
])
|
||||
}
|
||||
|
||||
async getEntry(id: string): Promise<SessionTreeEntry | undefined> {
|
||||
const record = await prisma.agentSessionEntry.findUnique({
|
||||
where: { id },
|
||||
})
|
||||
if (!record) return undefined
|
||||
const entry = this.recordToEntry(record)
|
||||
await this.reinjectImageDataAsync(entry)
|
||||
await this.reinjectSkillContentAsync(entry)
|
||||
return entry
|
||||
}
|
||||
|
||||
async findEntries<TType extends SessionTreeEntry['type']>(
|
||||
type: TType,
|
||||
): Promise<Array<Extract<SessionTreeEntry, { type: TType }>>> {
|
||||
const records = await prisma.agentSessionEntry.findMany({
|
||||
where: {
|
||||
sessionId: this.sessionId,
|
||||
type,
|
||||
},
|
||||
orderBy: { id: 'asc' },
|
||||
})
|
||||
const entries = records.map((r) => this.recordToEntry(r)) as Array<
|
||||
Extract<SessionTreeEntry, { type: TType }>
|
||||
>
|
||||
for (const entry of entries) {
|
||||
await this.reinjectImageDataAsync(entry)
|
||||
await this.reinjectSkillContentAsync(entry)
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
async getLabel(id: string): Promise<string | undefined> {
|
||||
const entry = await this.getEntry(id)
|
||||
return (entry as { label?: string } | undefined)?.label
|
||||
}
|
||||
|
||||
async getSessionName(): Promise<string | undefined> {
|
||||
const session = await prisma.agentSession.findUnique({
|
||||
where: { id: this.sessionId },
|
||||
select: { name: true },
|
||||
})
|
||||
return session?.name || undefined
|
||||
}
|
||||
|
||||
async getSessionStats(): Promise<{
|
||||
messageCount: number
|
||||
cachedTokens: number
|
||||
uncachedTokens: number
|
||||
totalTokens: number
|
||||
costTotal: number
|
||||
}> {
|
||||
const entries = await this.getEntries()
|
||||
|
||||
let messageCount = 0
|
||||
let cachedTokens = 0
|
||||
let uncachedTokens = 0
|
||||
let totalTokens = 0
|
||||
let costTotal = 0
|
||||
|
||||
for (const entry of entries) {
|
||||
if (entry.type === 'custom_message' && entry.customType === 'shumai_message') {
|
||||
messageCount++
|
||||
} else if (entry.type === 'message' && entry.message) {
|
||||
const msg = entry.message
|
||||
if (msg.role === 'user' || msg.role === 'assistant') {
|
||||
messageCount++
|
||||
}
|
||||
if (msg.role === 'assistant' && msg.usage) {
|
||||
cachedTokens += msg.usage.cacheRead || 0
|
||||
uncachedTokens += (msg.usage.input || 0) + (msg.usage.output || 0)
|
||||
totalTokens +=
|
||||
msg.usage.totalTokens ||
|
||||
(msg.usage.input || 0) + (msg.usage.output || 0) + (msg.usage.cacheRead || 0)
|
||||
if (msg.usage.cost) {
|
||||
costTotal += msg.usage.cost.total || 0
|
||||
}
|
||||
}
|
||||
} else if (entry.type === 'compaction' || entry.type === 'branch_summary') {
|
||||
const usage = entry.usage
|
||||
if (usage) {
|
||||
cachedTokens += usage.cacheRead || 0
|
||||
uncachedTokens += (usage.input || 0) + (usage.output || 0)
|
||||
totalTokens +=
|
||||
usage.totalTokens || (usage.input || 0) + (usage.output || 0) + (usage.cacheRead || 0)
|
||||
if (usage.cost) {
|
||||
costTotal += usage.cost.total || 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
messageCount,
|
||||
cachedTokens,
|
||||
uncachedTokens,
|
||||
totalTokens,
|
||||
costTotal,
|
||||
}
|
||||
}
|
||||
|
||||
async getPathToRootOrCompaction(leafId: string | null): Promise<SessionTreeEntry[]> {
|
||||
return this.getPathToRoot(leafId)
|
||||
}
|
||||
|
||||
async getPathToRoot(leafId: string | null): Promise<SessionTreeEntry[]> {
|
||||
if (!leafId) return []
|
||||
|
||||
interface EntryRow {
|
||||
id: string
|
||||
type: string | null
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
parent_id: string | null
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
created_at: Date
|
||||
data: unknown
|
||||
}
|
||||
|
||||
const rows = await prisma.$queryRaw<EntryRow[]>`
|
||||
WITH RECURSIVE entry_path AS (
|
||||
SELECT id, type, parent_id, created_at, data, 1 AS depth
|
||||
FROM agent_session_entries
|
||||
WHERE id = ${leafId}
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT e.id, e.type, e.parent_id, e.created_at, e.data, ep.depth + 1
|
||||
FROM agent_session_entries e
|
||||
INNER JOIN entry_path ep ON e.id = ep.parent_id
|
||||
)
|
||||
SELECT id, type, parent_id, created_at, data
|
||||
FROM entry_path
|
||||
ORDER BY depth DESC;
|
||||
`
|
||||
|
||||
const pathEntries: SessionTreeEntry[] = rows.map((r) =>
|
||||
this.recordToEntry({
|
||||
id: r.id,
|
||||
type: r.type,
|
||||
parentId: r.parent_id,
|
||||
createdAt: r.created_at,
|
||||
data: r.data,
|
||||
}),
|
||||
)
|
||||
|
||||
// Only reinject image data (which may involve S3 calls) for the entries in the path
|
||||
for (const entry of pathEntries) {
|
||||
await this.reinjectImageDataAsync(entry)
|
||||
await this.reinjectSkillContentAsync(entry)
|
||||
}
|
||||
|
||||
return this.injectThreadMetadataAsync(pathEntries)
|
||||
}
|
||||
|
||||
async getEntries(): Promise<SessionTreeEntry[]> {
|
||||
const records = await prisma.agentSessionEntry.findMany({
|
||||
where: { sessionId: this.sessionId },
|
||||
orderBy: { id: 'asc' },
|
||||
})
|
||||
const entries = records.map((r) => this.recordToEntry(r))
|
||||
for (const entry of entries) {
|
||||
await this.reinjectImageDataAsync(entry)
|
||||
await this.reinjectSkillContentAsync(entry)
|
||||
}
|
||||
return this.injectThreadMetadataAsync(entries)
|
||||
}
|
||||
|
||||
private async injectThreadMetadataAsync(
|
||||
entries: SessionTreeEntry[],
|
||||
): Promise<SessionTreeEntry[]> {
|
||||
const entryIds = entries.map((e) => e.id)
|
||||
if (entryIds.length === 0) {
|
||||
return entries
|
||||
}
|
||||
|
||||
const threadCounts = await prisma.assetComment.groupBy({
|
||||
by: ['replyToId'],
|
||||
where: {
|
||||
replyToId: { in: entryIds },
|
||||
message: { not: '__CHAT__' },
|
||||
},
|
||||
_count: {
|
||||
id: true,
|
||||
},
|
||||
})
|
||||
const threadReplyCountMap = new Map<string, number>()
|
||||
for (const tc of threadCounts) {
|
||||
if (tc.replyToId) {
|
||||
threadReplyCountMap.set(tc.replyToId, tc._count.id)
|
||||
}
|
||||
}
|
||||
|
||||
if (threadReplyCountMap.size > 0) {
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
const entry = entries[i]
|
||||
const count = threadReplyCountMap.get(entry.id)
|
||||
if (count !== undefined && count > 0) {
|
||||
if (entry.type === 'custom_message') {
|
||||
const cloned = structuredClone(entry)
|
||||
const existingDetails = ((cloned as unknown as { details?: ShumaiMessageContext })
|
||||
.details || {}) as ShumaiMessageContext
|
||||
;(cloned as unknown as { details: ShumaiMessageContext }).details = {
|
||||
...existingDetails,
|
||||
thread: {
|
||||
id: entry.id,
|
||||
replyCount: count,
|
||||
},
|
||||
}
|
||||
entries[i] = cloned
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entries
|
||||
}
|
||||
|
||||
private async reinjectImageDataAsync(entry: SessionTreeEntry) {
|
||||
if (entry.type === 'message' && entry.message) {
|
||||
const msg = entry.message as AgentMessage
|
||||
if (msg.role === 'toolResult') {
|
||||
const details = msg.details as { sourceKeys?: string[] } | undefined
|
||||
if (Array.isArray(msg.content) && details?.sourceKeys) {
|
||||
const sourceKeys = details.sourceKeys
|
||||
let keyIdx = 0
|
||||
for (const item of msg.content) {
|
||||
if (item.type === 'image' && item.data === '__S3_DATA__' && sourceKeys[keyIdx]) {
|
||||
const key = sourceKeys[keyIdx]
|
||||
try {
|
||||
const { buffer } = await s3Service.getObject(process.env.S3_BUCKET || 'shumai', key)
|
||||
item.data = buffer.toString('base64')
|
||||
} catch (err) {
|
||||
console.error(`Failed to re-inject image data for key ${key}:`, err)
|
||||
}
|
||||
keyIdx++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async reinjectSkillContentAsync(entry: SessionTreeEntry) {
|
||||
if (entry.type === 'message' && entry.message) {
|
||||
const msg = entry.message as AgentMessage
|
||||
if (msg.role === 'toolResult' && msg.toolName === 'read_skill') {
|
||||
const details = msg.details as { skillId?: string } | undefined
|
||||
if (details?.skillId && Array.isArray(msg.content)) {
|
||||
for (const item of msg.content) {
|
||||
if (item.type === 'text' && item.text === '__SKILL_CONTENT__') {
|
||||
try {
|
||||
const content = await agentService.getSkillContent(details.skillId)
|
||||
item.text = content
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`Failed to re-inject skill content for skill ${details.skillId}:`,
|
||||
err,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static async create(params: {
|
||||
agentId: string
|
||||
userId?: string
|
||||
sessionId?: string
|
||||
cwd?: string
|
||||
assetId?: string
|
||||
userCommentId?: string
|
||||
}): Promise<DatabaseSessionStorage> {
|
||||
if (params.sessionId) {
|
||||
return new DatabaseSessionStorage(params.sessionId)
|
||||
} else {
|
||||
const session = await prisma.agentSession.create({
|
||||
data: {
|
||||
agentId: params.agentId,
|
||||
userId: params.userId,
|
||||
cwd: params.cwd || process.cwd(),
|
||||
assetId: params.assetId || null,
|
||||
userCommentId: params.userCommentId || null,
|
||||
},
|
||||
})
|
||||
return new DatabaseSessionStorage(session.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
+282
-846
File diff suppressed because it is too large
Load Diff
+6
-597
@@ -1,594 +1,3 @@
|
||||
import { sandboxService } from '@shumai/core'
|
||||
import {
|
||||
AgentHarness,
|
||||
Session,
|
||||
type AgentTool,
|
||||
type ThinkingLevel,
|
||||
} from '@earendil-works/pi-agent-core'
|
||||
import type { Api, Model } from '@earendil-works/pi-ai'
|
||||
import { createModels, InMemoryCredentialStore } from '@earendil-works/pi-ai'
|
||||
import { builtinProviders } from '@earendil-works/pi-ai/providers/all'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
|
||||
import { quotaService, QuotaExceededError } from '@shumai/core/src/quota/quota-service'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { Type, type TSchema } from 'typebox'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { DatabaseSessionStorage } from './database-session-storage'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
import { createReadSkillTool } from './tools/read-skill'
|
||||
import { createReadThreadTool } from './tools/read-thread'
|
||||
import { createSandboxedBashTool } from './tools/sandboxed-bash'
|
||||
import { createGenerateImageTool } from './tools/generate-image'
|
||||
import { createGenerateVideoTool } from './tools/generate-video'
|
||||
import { buildOperationTools } from './operation-tools'
|
||||
import { createAutofillReadAssetTool } from './operation-extensions'
|
||||
import { mediaGenerationService } from '@shumai/core/src/media-generation/media-generation'
|
||||
import { serializeContextToXml } from './context/serialize-context'
|
||||
import { DEFAULT_DENIED_TOOLS, type ShumaiMessageContext } from '@shumai/dtos'
|
||||
|
||||
export interface DbModelInfo {
|
||||
modelId: string
|
||||
name: string | null
|
||||
config: PrismaJson.ModelConfig
|
||||
}
|
||||
|
||||
export interface DbProviderInfo {
|
||||
name: string
|
||||
config: PrismaJson.ProviderConfig
|
||||
models: DbModelInfo[]
|
||||
}
|
||||
|
||||
export function getModelFromDb(providers: DbProviderInfo[], providerName: string, modelId: string) {
|
||||
const dbProvider = providers.find((p) => p.name === providerName)
|
||||
const dbModel = dbProvider?.models.find((m) => m.modelId === modelId)
|
||||
|
||||
if (!dbProvider || !dbModel) {
|
||||
throw new Error(
|
||||
`Provider "${providerName}" or model "${modelId}" not found in database configuration`,
|
||||
)
|
||||
}
|
||||
|
||||
const m = {
|
||||
id: modelId,
|
||||
name: dbModel.name || modelId,
|
||||
provider: providerName,
|
||||
api: 'openai-responses',
|
||||
baseUrl: '',
|
||||
reasoning: false,
|
||||
input: ['text'],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 4096,
|
||||
maxTokens: 4096,
|
||||
}
|
||||
|
||||
// Override with database config
|
||||
if (dbProvider.config.baseUrl) {
|
||||
m.baseUrl = dbProvider.config.baseUrl
|
||||
}
|
||||
Object.assign(m, dbModel.config)
|
||||
if (dbProvider.config.api) {
|
||||
m.api = dbProvider.config.api
|
||||
}
|
||||
if (dbModel.name) {
|
||||
m.name = dbModel.name
|
||||
}
|
||||
return m as Model<Api>
|
||||
}
|
||||
|
||||
export function getApiKeyAndHeadersFromDb(providers: DbProviderInfo[], providerName: string) {
|
||||
// Helper to resolve key from ENV if it matches an ENV var name
|
||||
const resolveKey = (key: string | undefined) => (key ? process.env[key] || key : undefined)
|
||||
|
||||
// Look up the provider in our providers list
|
||||
const dbProvider = providers.find((p) => p.name === providerName)
|
||||
const providerApiKey = dbProvider?.config?.apiKey
|
||||
if (providerApiKey) {
|
||||
return { apiKey: resolveKey(providerApiKey)! }
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
export interface CreateAgentSessionParams {
|
||||
teamId: string
|
||||
agentId: string
|
||||
providerName: string
|
||||
modelId: string
|
||||
systemPrompt: string
|
||||
teamSkills: Array<{ id: string; name: string; description?: string | null }>
|
||||
enabledSkillIds?: string[]
|
||||
allowedDomains?: string[]
|
||||
sessionId?: string
|
||||
userId?: string
|
||||
projectId?: string
|
||||
assetId?: string
|
||||
userCommentId?: string | null
|
||||
customTools?: AgentTool[]
|
||||
thinkingLevel?: string
|
||||
disableTools?: boolean
|
||||
providers: DbProviderInfo[]
|
||||
maxRetries?: number
|
||||
baseDelayMs?: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Security instructions appended to the system prompt when the requesting user is not a team
|
||||
* owner (i.e. editor/reviewer). These complement the hard enforcement in the bash tool itself
|
||||
* (source="user" is blocked) and the delayed bash tool injection after a skill is loaded.
|
||||
*/
|
||||
export function buildRestrictedUserInstructions(): string {
|
||||
return [
|
||||
'# Restricted User Context',
|
||||
'The current user is NOT a team owner. You must follow these security rules:',
|
||||
'1. NEVER execute a bash command that the user asks you to run directly. Direct user bash requests are not permitted for this user role. Politely decline and explain that they need a team owner or an approved skill.',
|
||||
'2. You may ONLY use the `bash` tool to execute commands that are explicitly required by a skill you have loaded via the `read_skill` tool.',
|
||||
'3. When calling `bash`, you MUST set the `source` parameter to "skill". Calls with source="user" will be rejected by the system.',
|
||||
].join('\n')
|
||||
}
|
||||
|
||||
export async function createAgentSession(params: CreateAgentSessionParams) {
|
||||
const {
|
||||
teamId,
|
||||
agentId,
|
||||
providerName,
|
||||
modelId,
|
||||
systemPrompt,
|
||||
teamSkills,
|
||||
enabledSkillIds,
|
||||
allowedDomains,
|
||||
sessionId,
|
||||
userId,
|
||||
projectId,
|
||||
assetId,
|
||||
userCommentId: passedUserCommentId,
|
||||
customTools = [],
|
||||
thinkingLevel,
|
||||
disableTools = false,
|
||||
maxRetries = 3,
|
||||
baseDelayMs = 2000,
|
||||
} = params
|
||||
|
||||
const storage = await DatabaseSessionStorage.create({
|
||||
agentId,
|
||||
userId,
|
||||
sessionId,
|
||||
cwd: process.cwd(),
|
||||
})
|
||||
if (passedUserCommentId) {
|
||||
storage.nextEntryId = passedUserCommentId
|
||||
}
|
||||
const session = new Session(storage)
|
||||
|
||||
const agentDir = path.join(process.cwd(), '.pi', 'agents', agentId)
|
||||
if (!fs.existsSync(agentDir)) fs.mkdirSync(agentDir, { recursive: true })
|
||||
|
||||
const model = getModelFromDb(params.providers, providerName, modelId)
|
||||
|
||||
const piDir = path.join(process.cwd(), '.pi')
|
||||
if (!fs.existsSync(piDir)) fs.mkdirSync(piDir, { recursive: true })
|
||||
|
||||
const role = userId ? await resolveEffectiveRole(teamId, projectId, userId) : undefined
|
||||
const isOwner = !userId || role === 'owner'
|
||||
const restricted = !isOwner
|
||||
|
||||
const wrapToolWithQuota = (tool: AgentTool): AgentTool => {
|
||||
const originalExecute = tool.execute
|
||||
return {
|
||||
...tool,
|
||||
execute: async (toolCallId, args, signal, onUpdate) => {
|
||||
try {
|
||||
await quotaService.checkQuota(
|
||||
{
|
||||
teamId,
|
||||
userId,
|
||||
role,
|
||||
resource: 'agent_tool_call_count',
|
||||
resourceData: { name: tool.name, toolName: tool.name },
|
||||
},
|
||||
1,
|
||||
)
|
||||
} catch (err) {
|
||||
if (err instanceof QuotaExceededError) {
|
||||
return {
|
||||
content: [{ type: 'text', text: `Quota exceeded: ${err.message}` }],
|
||||
details: { error: err.message },
|
||||
}
|
||||
}
|
||||
throw err
|
||||
}
|
||||
|
||||
const result = await originalExecute(toolCallId, args, signal, onUpdate)
|
||||
|
||||
try {
|
||||
await quotaService.consumeQuota(
|
||||
{
|
||||
teamId,
|
||||
userId,
|
||||
role,
|
||||
resource: 'agent_tool_call_count',
|
||||
resourceData: { name: tool.name, toolName: tool.name },
|
||||
},
|
||||
1,
|
||||
)
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'Failed to record quota usage for agent tool call')
|
||||
}
|
||||
|
||||
return result
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if (allowedDomains !== undefined) {
|
||||
await sandboxService.syncAllowedDomains(allowedDomains, teamId)
|
||||
}
|
||||
|
||||
const skillEnvs: Record<string, string> = {}
|
||||
const onEnvsAdded = (envs: Record<string, string>) => {
|
||||
Object.assign(skillEnvs, envs)
|
||||
}
|
||||
|
||||
// Restore env variables from previously loaded skills in this session
|
||||
let skillLoadedInSession = false
|
||||
if (sessionId) {
|
||||
try {
|
||||
const entries = await storage.getEntries()
|
||||
for (const entry of entries) {
|
||||
if (
|
||||
entry.type === 'message' &&
|
||||
entry.message &&
|
||||
entry.message.role === 'toolResult' &&
|
||||
entry.message.toolName === 'read_skill' &&
|
||||
!entry.message.isError
|
||||
) {
|
||||
const details = entry.message.details as { skillId?: string } | undefined
|
||||
if (details?.skillId) {
|
||||
skillLoadedInSession = true
|
||||
const envs = await agentService.getSkillEnvs(details.skillId)
|
||||
onEnvsAdded(envs)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to restore skill environment variables:', err)
|
||||
}
|
||||
}
|
||||
|
||||
const agent = await prisma.agent.findUnique({
|
||||
where: { id: agentId },
|
||||
})
|
||||
const agentConfig = agent?.config as PrismaJson.AgentConfig | null | undefined
|
||||
const deniedTools = agentConfig?.deniedTools ?? [...DEFAULT_DENIED_TOOLS]
|
||||
|
||||
const mediaTools: AgentTool[] = []
|
||||
// Agents acting for a user get read_asset / get_asset_render from the Operation registry
|
||||
// (see systemTools below); the autofill system agent has its own team-scoped read_asset.
|
||||
if (!userId && agent?.type === 'autofill' && assetId) {
|
||||
mediaTools.push(
|
||||
createAutofillReadAssetTool({
|
||||
teamId,
|
||||
targetAssetId: assetId,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
if (!deniedTools.includes('generate_image') || !deniedTools.includes('generate_video')) {
|
||||
try {
|
||||
const { imageModels, videoModels, providerKeys } =
|
||||
await mediaGenerationService.getValidModels(teamId)
|
||||
if (imageModels.length > 0 && !deniedTools.includes('generate_image')) {
|
||||
mediaTools.push(createGenerateImageTool(imageModels, providerKeys, userId))
|
||||
}
|
||||
if (videoModels.length > 0 && !deniedTools.includes('generate_video')) {
|
||||
mediaTools.push(createGenerateVideoTool(videoModels, providerKeys, userId))
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error({ err, teamId }, 'Failed to initialize media generation tools')
|
||||
}
|
||||
}
|
||||
|
||||
const sandboxedBash = createSandboxedBashTool(process.cwd(), skillEnvs, {
|
||||
teamId,
|
||||
userId,
|
||||
role,
|
||||
getBlockedHost: (commandId?: string) => sandboxService.getBlockedHost(commandId),
|
||||
clearBlockedHost: () => {
|
||||
sandboxService.clearBlockedHost()
|
||||
},
|
||||
// Non-owner users can only run bash commands required by a loaded skill
|
||||
restrictedUser: restricted,
|
||||
})
|
||||
|
||||
// For non-owner users, the bash tool is not injected initially. It becomes available
|
||||
// only after a skill is successfully loaded via the read_skill tool (or immediately when
|
||||
// a skill was already loaded earlier in this session).
|
||||
let harness: AgentHarness | undefined = undefined
|
||||
const bashIncludedFromStart = isOwner || skillLoadedInSession
|
||||
let bashInjected = bashIncludedFromStart
|
||||
const onSkillLoaded = async () => {
|
||||
if (!harness || bashInjected || deniedTools.includes('bash')) return
|
||||
if (harness.getTools().some((t) => t.name === 'bash')) return
|
||||
bashInjected = true
|
||||
// Pass the full post-update tool list as activeToolNames: setTools only updates the
|
||||
// registry otherwise, and the model would never see the bash tool on the next turn.
|
||||
const next = [...harness.getTools(), wrapToolWithQuota(sandboxedBash)]
|
||||
await harness.setTools(
|
||||
next,
|
||||
next.map((t) => t.name),
|
||||
)
|
||||
}
|
||||
const readSkill = createReadSkillTool(
|
||||
userId,
|
||||
onEnvsAdded,
|
||||
restricted ? onSkillLoaded : undefined,
|
||||
enabledSkillIds,
|
||||
projectId,
|
||||
)
|
||||
|
||||
const systemTools: AgentTool[] = []
|
||||
if (userId) {
|
||||
const creationMetadataSchema = await buildProjectMetadataSchema(projectId)
|
||||
systemTools.push(
|
||||
// Shared Operations (also exposed via MCP and the CLI): browsing, searching, organizing,
|
||||
// reading and downloading assets, creating files and versions, comments and metadata
|
||||
...buildOperationTools({ userId, agentId, teamId, projectId }, { creationMetadataSchema }),
|
||||
)
|
||||
}
|
||||
|
||||
const readThread = createReadThreadTool({ userId, teamId })
|
||||
const rawTools = [
|
||||
...mediaTools,
|
||||
readSkill,
|
||||
readThread,
|
||||
...(bashIncludedFromStart ? [sandboxedBash] : []),
|
||||
...systemTools,
|
||||
...customTools,
|
||||
]
|
||||
const allTools = rawTools.map((tool) => wrapToolWithQuota(tool))
|
||||
const enabledTools = disableTools
|
||||
? []
|
||||
: allTools.filter((tool) => !deniedTools.includes(tool.name))
|
||||
|
||||
const credentials = new InMemoryCredentialStore()
|
||||
const resolveKey = (key: string | undefined) => (key ? process.env[key] || key : undefined)
|
||||
for (const p of params.providers) {
|
||||
const apiKey = resolveKey(p.config?.apiKey)
|
||||
if (apiKey) {
|
||||
await credentials.modify(p.name, async () => ({ type: 'api_key' as const, key: apiKey }))
|
||||
}
|
||||
}
|
||||
const modelsStore = createModels({ credentials })
|
||||
for (const provider of builtinProviders()) {
|
||||
modelsStore.setProvider(provider)
|
||||
}
|
||||
|
||||
harness = new AgentHarness({
|
||||
session,
|
||||
models: modelsStore,
|
||||
model,
|
||||
thinkingLevel: (thinkingLevel || 'off') as ThinkingLevel,
|
||||
streamOptions: {
|
||||
maxRetries,
|
||||
maxRetryDelayMs: baseDelayMs,
|
||||
},
|
||||
systemPrompt: async () => {
|
||||
let prompt = systemPrompt
|
||||
|
||||
if (disableTools) {
|
||||
return prompt
|
||||
}
|
||||
|
||||
if (restricted) {
|
||||
prompt += `\n\n${buildRestrictedUserInstructions()}`
|
||||
}
|
||||
|
||||
// Sandbox environment restrictions
|
||||
prompt +=
|
||||
'\n\n' +
|
||||
[
|
||||
'# Sandbox Environment Restrictions',
|
||||
'Your shell environment (the `bash` tool) is highly sandboxed to protect the host system:',
|
||||
'1. **Filesystem Isolation**: You only have read and write permissions to the `.pi` folder in the project root directory.',
|
||||
'2. **Read/Write Restrictions**: All reading and writing to directories outside `.pi` (e.g. your home directory `~/`, `/tmp`, `/etc`, or the rest of the workspace) are strictly denied by the sandbox security policy.',
|
||||
"3. **Avoid System Temp Directory Writes**: You must strictly avoid any commands or shell constructs that attempt to write to the system temporary directory `/tmp` or `/var/tmp`. For example, do not use Bash here documents (`<<EOF` or `<<'EOF'`) in your commands, as the bash shell internally implements here documents by writing temporary files to `/tmp`. If you need to create a file or write content, write it directly using file creation tools or write to files located inside the `.pi` directory without utilizing here documents.",
|
||||
'4. **Temporary File Cleanup**: If you download workspace assets to `.pi` using the `download_asset` tool for local processing or script execution, you MUST delete the temporary downloaded files from `.pi` after finishing your task.',
|
||||
].join('\n')
|
||||
|
||||
if (teamSkills.length > 0) {
|
||||
prompt += formatSkillsForPrompt(teamSkills)
|
||||
}
|
||||
return prompt
|
||||
},
|
||||
tools: enabledTools,
|
||||
})
|
||||
|
||||
harness.on('context', async ({ messages }) => {
|
||||
return {
|
||||
messages: messages.map((msg) => {
|
||||
if (
|
||||
msg.role === 'custom' &&
|
||||
msg.customType === 'shumai_message' &&
|
||||
msg.details &&
|
||||
typeof msg.details === 'object'
|
||||
) {
|
||||
const context = msg.details as ShumaiMessageContext
|
||||
const xml = serializeContextToXml(context)
|
||||
const text = typeof msg.content === 'string' ? msg.content : ''
|
||||
const fullContent = xml ? `${text}\n\n${xml}`.trim() : text
|
||||
return {
|
||||
...msg,
|
||||
content: fullContent,
|
||||
}
|
||||
}
|
||||
return msg
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
return { session, harness }
|
||||
}
|
||||
|
||||
export interface AutofillField {
|
||||
id: string
|
||||
config: PrismaJson.FieldConfig
|
||||
description?: string | null
|
||||
}
|
||||
|
||||
export function fieldsToTypeBoxSchema(fields: AutofillField[]) {
|
||||
const newOptionSchema = Type.Object(
|
||||
{
|
||||
newOption: Type.Object(
|
||||
{
|
||||
value: Type.String({
|
||||
minLength: 1,
|
||||
description:
|
||||
'The new option display name/value to create and select if none of the existing options match.',
|
||||
}),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
)
|
||||
|
||||
const properties: Record<string, TSchema> = {}
|
||||
for (const f of fields) {
|
||||
let schema: TSchema
|
||||
const fieldName = f.config.name
|
||||
const fieldDesc = f.description || fieldName
|
||||
const baseDescription = `The field '${fieldName}' represents ${fieldDesc}.`
|
||||
switch (f.config.type) {
|
||||
case 'text':
|
||||
case 'longText':
|
||||
schema = Type.String({ description: baseDescription })
|
||||
break
|
||||
case 'number':
|
||||
case 'rating':
|
||||
schema = Type.Number({ description: baseDescription })
|
||||
break
|
||||
case 'toggle':
|
||||
schema = Type.Boolean({ description: baseDescription })
|
||||
break
|
||||
case 'select': {
|
||||
const options = f.config.select?.options || []
|
||||
const description =
|
||||
options.length > 0
|
||||
? `${baseDescription}\nSelect one existing option ID or provide {"newOption": {"value": "..."}} to create a new option.\n\nAvailable options:\n${options.map((o) => `- ${o.displayName} => ${o.id}`).join('\n')}`
|
||||
: `${baseDescription}\nProvide {"newOption": {"value": "..."}} with the desired option name.`
|
||||
if (options.length > 0) {
|
||||
schema = Type.Union(
|
||||
[
|
||||
Type.String({
|
||||
enum: options.map((o) => o.id),
|
||||
}),
|
||||
newOptionSchema,
|
||||
],
|
||||
{ description },
|
||||
)
|
||||
} else {
|
||||
schema = Type.Object(newOptionSchema.properties, {
|
||||
additionalProperties: false,
|
||||
description,
|
||||
})
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'selectMulti': {
|
||||
const options = f.config.selectMulti?.options || []
|
||||
const description =
|
||||
options.length > 0
|
||||
? `${baseDescription}\nSelect applicable option IDs or provide {"newOption": {"value": "..."}} objects for new options (e.g. ["opt1", {"newOption": {"value": "new_name"}}]).\n\nAvailable options:\n${options.map((o) => `- ${o.displayName} => ${o.id}`).join('\n')}`
|
||||
: `${baseDescription}\nProvide an array of {"newOption": {"value": "..."}} objects for new options.`
|
||||
const itemSchema =
|
||||
options.length > 0
|
||||
? Type.Union([
|
||||
Type.String({
|
||||
enum: options.map((o) => o.id),
|
||||
}),
|
||||
newOptionSchema,
|
||||
])
|
||||
: newOptionSchema
|
||||
schema = Type.Array(itemSchema, { description })
|
||||
break
|
||||
}
|
||||
default:
|
||||
schema = Type.String({ description: baseDescription })
|
||||
}
|
||||
// Every field is required but nullable: the agent must return each field key,
|
||||
// using `null` for values it does not know. Nulls are skipped when persisting.
|
||||
// This matches the OpenAI structured-outputs strict-mode convention (all
|
||||
// properties required, optionality expressed via `null`).
|
||||
properties[f.id] = Type.Union([schema, Type.Null()])
|
||||
}
|
||||
|
||||
return Type.Object(properties, { additionalProperties: false })
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the strict, all-optional metadata schema for the project's CREATION_CONTEXT
|
||||
* fields (the same field set the agent may attach when creating files/versions).
|
||||
* Returns undefined when no project is known or the project defines no fields, in
|
||||
* which case the `metadata` parameter is omitted from the tools entirely.
|
||||
*/
|
||||
async function buildProjectMetadataSchema(projectId?: string): Promise<TSchema | undefined> {
|
||||
if (!projectId) return undefined
|
||||
const fields = await metadataService.listProjectFields('', projectId)
|
||||
const creationFields: AutofillField[] = []
|
||||
for (const f of fields) {
|
||||
const config = f.field.config
|
||||
if (!config || config.autofillSource !== 'CREATION_CONTEXT') continue
|
||||
creationFields.push({
|
||||
id: f.field.key,
|
||||
config,
|
||||
description: f.field.description,
|
||||
})
|
||||
}
|
||||
if (creationFields.length === 0) return undefined
|
||||
return fieldsToTypeBoxSchema(creationFields)
|
||||
}
|
||||
|
||||
function escapeXml(str: string): string {
|
||||
return str
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''')
|
||||
}
|
||||
|
||||
export function formatSkillsForPrompt(
|
||||
skills: Array<{ id: string; name: string; description?: string | null }>,
|
||||
): string {
|
||||
if (skills.length === 0) {
|
||||
return ''
|
||||
}
|
||||
|
||||
const lines = [
|
||||
'\n\nThe following skills provide specialized instructions for specific tasks.',
|
||||
"Use the read_skill tool to load a skill's file when the task matches its description.",
|
||||
'When a skill file references a relative path, resolve it against the skill directory (parent of SKILL.md / dirname of the path) and use that absolute path in tool commands.',
|
||||
'',
|
||||
'<available_skills>',
|
||||
]
|
||||
|
||||
for (const skill of skills) {
|
||||
const filePath = path.join(process.cwd(), '.pi', 'skills', skill.id, 'SKILL.md')
|
||||
lines.push(' <skill>')
|
||||
lines.push(` <id>${escapeXml(skill.id)}</id>`)
|
||||
lines.push(` <name>${escapeXml(skill.name)}</name>`)
|
||||
lines.push(` <description>${escapeXml(skill.description || 'No description')}</description>`)
|
||||
lines.push(` <location>${escapeXml(filePath)}</location>`)
|
||||
lines.push(' </skill>')
|
||||
}
|
||||
|
||||
lines.push('</available_skills>')
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
import { WorkflowTaskType } from '@shumai/db'
|
||||
import { registerWorkflow } from '@shumai/workflow-core'
|
||||
import { agentAutofillMedia } from './workflows/agent-autofill'
|
||||
@@ -603,22 +12,22 @@ export function initAgentWorkflows() {
|
||||
|
||||
export * from './activities/agent'
|
||||
export {
|
||||
deleteAssetEmbeddingsActivity,
|
||||
extractAiMetadataActivity,
|
||||
generateImageEmbeddingActivity,
|
||||
generateTextEmbeddingActivity,
|
||||
generateVideoChunkEmbeddingActivity,
|
||||
saveAssetEmbeddingsActivity,
|
||||
deleteAssetEmbeddingsActivity,
|
||||
type ExistingAssetEmbeddingInfo,
|
||||
type ExtractAiMetadataParams,
|
||||
type GeneratedEmbedding,
|
||||
type GenerateTextEmbeddingParams,
|
||||
type ExistingAssetEmbeddingInfo,
|
||||
} from './activities/ai'
|
||||
export * from './database-session-storage'
|
||||
export * from './context/serialize-context'
|
||||
export * from './operation-tools'
|
||||
export * from './runtime'
|
||||
export * from './tools/generate-image'
|
||||
export * from './tools/generate-video'
|
||||
export * from './workflows/agent-autofill'
|
||||
export * from './workflows/agent-chat'
|
||||
export * from './workflows/agent-embedding'
|
||||
export * from './tools/generate-image'
|
||||
export * from './tools/generate-video'
|
||||
export * from './operation-tools'
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { prisma, type Asset, type StorageKey } from '@shumai/db'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { authzService } from '@shumai/core/src/authz/authz'
|
||||
import { getOperation } from '@shumai/core/src/operations'
|
||||
import { runnerService } from '@shumai/core/src/runner'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { prisma, type Asset, type StorageKey } from '@shumai/db'
|
||||
import * as path from 'path'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { toAgentTool } from '../operation-tools'
|
||||
import './download-asset'
|
||||
|
||||
@@ -31,18 +32,28 @@ vi.mock('@shumai/core/src/s3/s3', async (importOriginal) => {
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('@shumai/core/src/runner', () => ({
|
||||
runnerService: {
|
||||
requireOnlineRunner: vi.fn(),
|
||||
isRunnerConnected: vi.fn(),
|
||||
downloadToRunner: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@shumai/core/src/authz/authz', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@shumai/core/src/authz/authz')>()
|
||||
return { ...actual, authzService: { hasPermission: vi.fn() } }
|
||||
})
|
||||
|
||||
type Head = Awaited<ReturnType<typeof s3Service.headObject>>
|
||||
type Saved = { filePath: string; absolutePath: string; contentType: string; size: number }
|
||||
|
||||
const piDir = path.join(process.cwd(), '.pi')
|
||||
type Saved = { filePath: string; contentType: string; size: number }
|
||||
|
||||
function downloadTool(userId = 'user-1') {
|
||||
return toAgentTool(getOperation('download_asset')!, { userId })
|
||||
return toAgentTool(getOperation('download_asset')!, {
|
||||
userId,
|
||||
teamId: 'team-1',
|
||||
runnerId: null,
|
||||
})
|
||||
}
|
||||
|
||||
function textOf(result: { content: unknown[] }): string {
|
||||
@@ -56,10 +67,14 @@ function mockAssets(...assets: Array<Record<string, unknown>>) {
|
||||
)
|
||||
}
|
||||
|
||||
function mockStorage(contentType: string, write = 'bytes') {
|
||||
vi.mocked(s3Service.headObject).mockResolvedValue({ size: 1234, contentType } as Head)
|
||||
vi.mocked(s3Service.downloadToFile).mockImplementation(async (_bucket, _key, dest) => {
|
||||
await fs.promises.writeFile(dest, write)
|
||||
function mockStorage(contentType: string, size = 11) {
|
||||
vi.mocked(s3Service.headObject).mockResolvedValue({ size, contentType } as Head)
|
||||
vi.mocked(runnerService.downloadToRunner).mockResolvedValue({
|
||||
type: 'file.download.result',
|
||||
executionId: 'exec-dl-1',
|
||||
ok: true,
|
||||
size,
|
||||
mimeType: contentType,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -73,15 +88,19 @@ const photo = {
|
||||
}
|
||||
|
||||
describe('download_asset agent tool', () => {
|
||||
const created: string[] = []
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
vi.mocked(authzService.hasPermission).mockResolvedValue()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
for (const file of created.splice(0)) fs.rmSync(file, { force: true })
|
||||
vi.mocked(runnerService.requireOnlineRunner).mockResolvedValue({
|
||||
id: 'runner-1',
|
||||
teamId: 'team-1',
|
||||
name: 'Local Mac Mini',
|
||||
keyPrefix: 'shm_rn_12345678',
|
||||
isDefault: true,
|
||||
status: 'connected',
|
||||
createdAt: '2026-11-14T00:00:00.000Z',
|
||||
updatedAt: '2026-11-14T00:00:00.000Z',
|
||||
})
|
||||
})
|
||||
|
||||
it('offers assetIds, proxy, and the agent-only key parameter', () => {
|
||||
@@ -92,35 +111,32 @@ describe('download_asset agent tool', () => {
|
||||
expect(tool.description).toContain('.pi directory')
|
||||
})
|
||||
|
||||
it('saves the web preview into .pi by default, named after the asset', async () => {
|
||||
it('saves the web preview into .pi on the runner by default, named after the asset', async () => {
|
||||
mockAssets(photo)
|
||||
mockStorage('image/webp', 'proxy-bytes')
|
||||
mockStorage('image/webp', 11)
|
||||
|
||||
const result = await downloadTool().execute('call-1', { assetIds: ['img-1'] })
|
||||
const result = await invokeTool(downloadTool(), 'call-1', { assetIds: ['img-1'] })
|
||||
|
||||
const expectedPath = path.join(piDir, 'img-1_sample_photo.webp')
|
||||
created.push(expectedPath)
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: { id: 'user-1' },
|
||||
permission: 'Read',
|
||||
type: 'asset',
|
||||
id: 'img-1',
|
||||
})
|
||||
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
|
||||
'shumai',
|
||||
'files/img-1/proxy.webp',
|
||||
expectedPath,
|
||||
)
|
||||
expect(fs.readFileSync(expectedPath, 'utf-8')).toBe('proxy-bytes')
|
||||
expect(runnerService.requireOnlineRunner).toHaveBeenCalledWith('team-1', null)
|
||||
expect(runnerService.downloadToRunner).toHaveBeenCalledWith('runner-1', {
|
||||
url: 'https://signed.example/file',
|
||||
destPath: path.posix.join('.pi', 'img-1_sample_photo.webp'),
|
||||
})
|
||||
expect(textOf(result)).toBe(
|
||||
[
|
||||
`Downloaded "sample_photo.webp" (ID: img-1, proxy, image/webp, 11 B) to "${path.join('.pi', 'img-1_sample_photo.webp')}".`,
|
||||
`Downloaded "sample_photo.webp" (ID: img-1, proxy, image/webp, 11 B) to "${path.posix.join('.pi', 'img-1_sample_photo.webp')}".`,
|
||||
'Remember to delete these temporary files when finished.',
|
||||
].join('\n'),
|
||||
)
|
||||
const details = result.details as { files: Saved[] }
|
||||
expect(details.files[0]).toMatchObject({
|
||||
filePath: path.join('.pi', 'img-1_sample_photo.webp'),
|
||||
filePath: path.posix.join('.pi', 'img-1_sample_photo.webp'),
|
||||
contentType: 'image/webp',
|
||||
size: 11,
|
||||
})
|
||||
@@ -128,17 +144,23 @@ describe('download_asset agent tool', () => {
|
||||
|
||||
it('saves the original with proxy false', async () => {
|
||||
mockAssets(photo)
|
||||
mockStorage('image/jpeg')
|
||||
mockStorage('image/jpeg', 1234)
|
||||
|
||||
await downloadTool().execute('call-1', { assetIds: ['img-1'], proxy: false })
|
||||
await invokeTool(downloadTool(), 'call-1', { assetIds: ['img-1'], proxy: false })
|
||||
|
||||
const expectedPath = path.join(piDir, 'img-1_sample_photo.jpg')
|
||||
created.push(expectedPath)
|
||||
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
|
||||
expect(s3Service.presign).toHaveBeenCalledWith(
|
||||
'shumai',
|
||||
'files/up-1/sample_photo.jpg',
|
||||
expectedPath,
|
||||
'GET',
|
||||
false,
|
||||
undefined,
|
||||
false,
|
||||
600,
|
||||
)
|
||||
expect(runnerService.downloadToRunner).toHaveBeenCalledWith('runner-1', {
|
||||
url: 'https://signed.example/file',
|
||||
destPath: path.posix.join('.pi', 'img-1_sample_photo.jpg'),
|
||||
})
|
||||
})
|
||||
|
||||
it('downloads several files and the latest version of a version stack', async () => {
|
||||
@@ -153,30 +175,26 @@ describe('download_asset agent tool', () => {
|
||||
} as unknown as Asset)
|
||||
mockStorage('image/webp')
|
||||
|
||||
const result = await downloadTool().execute('call-1', {
|
||||
const result = await invokeTool(downloadTool(), 'call-1', {
|
||||
assetIds: ['img-1', 'stack-1'],
|
||||
key: null,
|
||||
})
|
||||
|
||||
created.push(
|
||||
path.join(piDir, 'img-1_sample_photo.webp'),
|
||||
path.join(piDir, 'file-v2_banana_pig.webp'),
|
||||
)
|
||||
expect(prisma.asset.findFirst).toHaveBeenCalledWith({
|
||||
where: { parentId: 'stack-1', isDeleted: false },
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
include: { storageKey: true },
|
||||
})
|
||||
expect(s3Service.downloadToFile).toHaveBeenCalledTimes(2)
|
||||
expect(runnerService.downloadToRunner).toHaveBeenCalledTimes(2)
|
||||
expect(textOf(result)).toContain('Downloaded "banana_pig.webp" (ID: file-v2, proxy')
|
||||
})
|
||||
|
||||
it('rejects folders', async () => {
|
||||
mockAssets({ id: 'folder-1', name: 'Renders', type: 'folder', isDeleted: false })
|
||||
await expect(downloadTool().execute('call-1', { assetIds: ['folder-1'] })).rejects.toThrow(
|
||||
await expect(invokeTool(downloadTool(), 'call-1', { assetIds: ['folder-1'] })).rejects.toThrow(
|
||||
'Asset folder-1 ("Renders") is a folder. Download the files inside it instead.',
|
||||
)
|
||||
expect(s3Service.downloadToFile).not.toHaveBeenCalled()
|
||||
expect(runnerService.downloadToRunner).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('checks Read on every asset before downloading anything', async () => {
|
||||
@@ -185,36 +203,36 @@ describe('download_asset agent tool', () => {
|
||||
if (id === 'secret') throw new Error('Forbidden')
|
||||
})
|
||||
await expect(
|
||||
downloadTool().execute('call-1', { assetIds: ['img-1', 'secret'] }),
|
||||
invokeTool(downloadTool(), 'call-1', { assetIds: ['img-1', 'secret'] }),
|
||||
).rejects.toThrow('Forbidden')
|
||||
expect(s3Service.downloadToFile).not.toHaveBeenCalled()
|
||||
expect(runnerService.downloadToRunner).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('requires assetIds or key', async () => {
|
||||
await expect(downloadTool().execute('call-1', { assetIds: null, key: null })).rejects.toThrow(
|
||||
'Invalid input for download_asset',
|
||||
)
|
||||
await expect(
|
||||
invokeTool(downloadTool(), 'call-1', { assetIds: null, key: null }),
|
||||
).rejects.toThrow('Invalid input for download_asset')
|
||||
})
|
||||
|
||||
it('rejects assetIds and key together', async () => {
|
||||
await expect(
|
||||
downloadTool().execute('call-1', { assetIds: ['img-1'], key: 'files/img-1/a.webp' }),
|
||||
invokeTool(downloadTool(), 'call-1', { assetIds: ['img-1'], key: 'files/img-1/a.webp' }),
|
||||
).rejects.toThrow('Provide either "assetIds" or "key", not both.')
|
||||
})
|
||||
|
||||
describe('by storage key', () => {
|
||||
it('requires a user', async () => {
|
||||
await expect(
|
||||
downloadTool('').execute('call-1', { key: 'files/ast-1/shot.webp' }),
|
||||
invokeTool(downloadTool(''), 'call-1', { key: 'files/ast-1/shot.webp' }),
|
||||
).rejects.toThrow('User ID is required for authorization.')
|
||||
})
|
||||
|
||||
it('rejects keys outside files/ and path traversal', async () => {
|
||||
await expect(downloadTool().execute('call-1', { key: 'avatars/user-1.png' })).rejects.toThrow(
|
||||
'Invalid storage key format: key must start with "files/"',
|
||||
)
|
||||
await expect(
|
||||
downloadTool().execute('call-1', { key: 'files/../../etc/passwd' }),
|
||||
invokeTool(downloadTool(), 'call-1', { key: 'avatars/user-1.png' }),
|
||||
).rejects.toThrow('Invalid storage key format: key must start with "files/"')
|
||||
await expect(
|
||||
invokeTool(downloadTool(), 'call-1', { key: 'files/../../etc/passwd' }),
|
||||
).rejects.toThrow('cannot contain ".."')
|
||||
})
|
||||
|
||||
@@ -223,7 +241,7 @@ describe('download_asset agent tool', () => {
|
||||
vi.mocked(prisma.storageKey.findFirst).mockResolvedValue(null)
|
||||
vi.mocked(prisma.storageKey.findUnique).mockResolvedValue(null)
|
||||
await expect(
|
||||
downloadTool().execute('call-1', { key: 'files/nope/shot.webp' }),
|
||||
invokeTool(downloadTool(), 'call-1', { key: 'files/nope/shot.webp' }),
|
||||
).rejects.toThrow('does not belong to any valid asset or the asset has been deleted')
|
||||
})
|
||||
|
||||
@@ -231,7 +249,7 @@ describe('download_asset agent tool', () => {
|
||||
mockAssets({ id: 'other-project-asset' })
|
||||
vi.mocked(authzService.hasPermission).mockRejectedValue(new Error('Forbidden'))
|
||||
await expect(
|
||||
downloadTool().execute('call-1', { key: 'files/other-project-asset/shots/a.webp' }),
|
||||
invokeTool(downloadTool(), 'call-1', { key: 'files/other-project-asset/shots/a.webp' }),
|
||||
).rejects.toThrow('Forbidden')
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: { id: 'user-1' },
|
||||
@@ -239,27 +257,28 @@ describe('download_asset agent tool', () => {
|
||||
type: 'asset',
|
||||
id: 'other-project-asset',
|
||||
})
|
||||
expect(s3Service.downloadToFile).not.toHaveBeenCalled()
|
||||
expect(runnerService.downloadToRunner).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('downloads a read_asset artifact into .pi', async () => {
|
||||
it('downloads a read_asset artifact into .pi on the runner', async () => {
|
||||
mockAssets({ id: 'ast-123' })
|
||||
mockStorage('image/webp', 'frame-bytes')
|
||||
mockStorage('image/webp', 11)
|
||||
|
||||
const result = await downloadTool().execute('call-1', {
|
||||
const result = await invokeTool(downloadTool(), 'call-1', {
|
||||
key: 'files/ast-123/screenshots/shot_5.0s.webp',
|
||||
})
|
||||
|
||||
const expectedPath = path.join(piDir, 'shot_5.0s.webp')
|
||||
created.push(expectedPath)
|
||||
expect(fs.readFileSync(expectedPath, 'utf-8')).toBe('frame-bytes')
|
||||
expect(runnerService.downloadToRunner).toHaveBeenCalledWith('runner-1', {
|
||||
url: 'https://signed.example/file',
|
||||
destPath: path.posix.join('.pi', 'shot_5.0s.webp'),
|
||||
})
|
||||
expect(textOf(result)).toBe(
|
||||
`Downloaded storage key "files/ast-123/screenshots/shot_5.0s.webp" to "${path.join('.pi', 'shot_5.0s.webp')}". Remember to delete this temporary file when finished.`,
|
||||
`Downloaded storage key "files/ast-123/screenshots/shot_5.0s.webp" to "${path.posix.join('.pi', 'shot_5.0s.webp')}". Remember to delete this temporary file when finished.`,
|
||||
)
|
||||
expect(result.details).toMatchObject({
|
||||
key: 'files/ast-123/screenshots/shot_5.0s.webp',
|
||||
assetId: 'ast-123',
|
||||
size: Buffer.from('frame-bytes').length,
|
||||
size: 11,
|
||||
})
|
||||
})
|
||||
|
||||
@@ -270,24 +289,25 @@ describe('download_asset agent tool', () => {
|
||||
key: 'files/upload-ulid-456/photo.png',
|
||||
assets: [{ id: 'asset-photo-1' }],
|
||||
} as unknown as StorageKey & { assets: Asset[] })
|
||||
const pngHeader = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
|
||||
vi.mocked(s3Service.headObject).mockResolvedValue({
|
||||
size: 8,
|
||||
contentType: 'application/octet-stream',
|
||||
} as Head)
|
||||
vi.mocked(s3Service.downloadToFile).mockImplementation(async (_b, _k, dest) => {
|
||||
await fs.promises.writeFile(dest, pngHeader)
|
||||
vi.mocked(runnerService.downloadToRunner).mockResolvedValue({
|
||||
type: 'file.download.result',
|
||||
executionId: 'exec-dl-2',
|
||||
ok: true,
|
||||
size: 8,
|
||||
mimeType: 'image/png',
|
||||
})
|
||||
|
||||
const result = await downloadTool().execute('call-1', {
|
||||
const result = await invokeTool(downloadTool(), 'call-1', {
|
||||
key: 'files/upload-ulid-456/photo.png',
|
||||
})
|
||||
|
||||
created.push(path.join(piDir, 'photo.png'))
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ id: 'asset-photo-1' }),
|
||||
)
|
||||
// Falls back to detecting the type from the bytes
|
||||
expect(result.details).toMatchObject({ assetId: 'asset-photo-1', contentType: 'image/png' })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,53 +1,67 @@
|
||||
import * as fs from 'fs'
|
||||
import type { ToolExecutionResult as AgentToolResult } from '@earendil-works/pi-durable'
|
||||
import { storageBucket } from '@shumai/core/src/asset-read'
|
||||
import { assertStorageKeyNotArchived } from '@shumai/core/src/asset/asset-archive'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { runnerService } from '@shumai/core/src/runner'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { getFileMimeType } from '@shumai/core/src/utils/file-mime'
|
||||
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
||||
import type { User } from '@shumai/db'
|
||||
import { downloadAsset, MAX_DOWNLOAD_ASSETS, type DownloadAssetOutput } from '@shumai/operations'
|
||||
import * as path from 'path'
|
||||
import { z } from 'zod'
|
||||
import type { AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import { downloadAsset, MAX_DOWNLOAD_ASSETS, type DownloadAssetOutput } from '@shumai/operations'
|
||||
import type { User } from '@shumai/db'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { assertStorageKeyNotArchived } from '@shumai/core/src/asset/asset-archive'
|
||||
import { storageBucket } from '@shumai/core/src/asset-read'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
||||
import { readFileMimeType } from '@shumai/core/src/utils/file-mime'
|
||||
import {
|
||||
registerAgentToolExtension,
|
||||
type AgentOperationContext,
|
||||
type AgentToolExtension,
|
||||
} from '../operation-tools'
|
||||
import { resolveAssetIdFromKey } from '../tools/download-asset'
|
||||
import { registerAgentToolExtension, type AgentToolExtension } from '../operation-tools'
|
||||
|
||||
const CLEANUP_REMINDER = 'Remember to delete these temporary files when finished.'
|
||||
|
||||
/** The agent's sandbox, where downloaded files are saved for local scripts and bash. */
|
||||
function sandboxDir(): string {
|
||||
const dir = path.join(process.cwd(), '.pi')
|
||||
fs.mkdirSync(dir, { recursive: true })
|
||||
return dir
|
||||
}
|
||||
|
||||
function formatSize(bytes: number): string {
|
||||
if (bytes < 1024) return `${bytes} B`
|
||||
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
|
||||
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
|
||||
}
|
||||
|
||||
/** Saves a storage object into the sandbox; returns the paths and detected content type. */
|
||||
async function saveToSandbox(key: string, filename: string) {
|
||||
const dir = sandboxDir()
|
||||
const absolutePath = path.join(dir, filename)
|
||||
async function resolveTargetRunnerId(ctx: AgentOperationContext): Promise<string> {
|
||||
if (ctx.runnerId && !ctx.teamId) {
|
||||
if (!runnerService.isRunnerConnected(ctx.runnerId)) {
|
||||
throw new Error(
|
||||
'Runner is currently offline. Please start your runner with "shumai-cli runner start" and try again.',
|
||||
)
|
||||
}
|
||||
return ctx.runnerId
|
||||
}
|
||||
const runner = await runnerService.requireOnlineRunner(ctx.teamId || '', ctx.runnerId)
|
||||
return runner.id
|
||||
}
|
||||
|
||||
/** Saves a storage object into the runner's .pi directory; returns the path, size, and content type. */
|
||||
async function saveToRunner(key: string, filename: string, ctx: AgentOperationContext) {
|
||||
const runnerId = await resolveTargetRunnerId(ctx)
|
||||
const bucket = storageBucket()
|
||||
const info = await s3Service.headObject(bucket, key).catch(() => null)
|
||||
await s3Service.downloadToFile(bucket, key, absolutePath)
|
||||
const size = await fs.promises
|
||||
.stat(absolutePath)
|
||||
.then((s) => s.size)
|
||||
.catch(() => info?.size ?? 0)
|
||||
const url = await s3Service.presign(bucket, key, 'GET', false, undefined, false, 600)
|
||||
const filePath = path.posix.join('.pi', filename)
|
||||
const res = await runnerService.downloadToRunner(runnerId, { url, destPath: filePath })
|
||||
if (!res.ok) {
|
||||
throw new Error(res.error || `Failed to download "${filename}" to runner`)
|
||||
}
|
||||
const size = res.size ?? info?.size ?? 0
|
||||
const contentType =
|
||||
info?.contentType && info.contentType !== 'application/octet-stream'
|
||||
? info.contentType
|
||||
: readFileMimeType(absolutePath)
|
||||
return { absolutePath, filePath: path.join('.pi', filename), size, contentType }
|
||||
res.mimeType && res.mimeType !== 'application/octet-stream'
|
||||
? res.mimeType
|
||||
: info?.contentType && info.contentType !== 'application/octet-stream'
|
||||
? info.contentType
|
||||
: getFileMimeType(null, filename)
|
||||
return { filePath, size, contentType }
|
||||
}
|
||||
|
||||
/** Downloads one storage key (e.g. a frame from read_asset) after checking Read on its asset. */
|
||||
async function downloadKey(key: string, userId: string): Promise<AgentToolResult<unknown>> {
|
||||
async function downloadKey(key: string, ctx: AgentOperationContext): Promise<AgentToolResult> {
|
||||
const userId = ctx.userId || ''
|
||||
const owningAssetId = await resolveAssetIdFromKey(key)
|
||||
await authzService.hasPermission({
|
||||
user: { id: userId } as User,
|
||||
@@ -57,9 +71,9 @@ async function downloadKey(key: string, userId: string): Promise<AgentToolResult
|
||||
})
|
||||
await assertStorageKeyNotArchived(key)
|
||||
const filename = sanitizeFilename(path.basename(key)) || 'downloaded_file'
|
||||
const saved = await saveToSandbox(key, filename)
|
||||
const saved = await saveToRunner(key, filename, ctx)
|
||||
return {
|
||||
content: [
|
||||
output: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Downloaded storage key "${key}" to "${saved.filePath}". Remember to delete this temporary file when finished.`,
|
||||
@@ -69,17 +83,19 @@ async function downloadKey(key: string, userId: string): Promise<AgentToolResult
|
||||
}
|
||||
}
|
||||
|
||||
/** Saves each file of a download_asset result into the sandbox. */
|
||||
/** Saves each file of a download_asset result into the runner's .pi directory. */
|
||||
export async function presentDownloadOutput(
|
||||
output: DownloadAssetOutput,
|
||||
): Promise<AgentToolResult<unknown>> {
|
||||
ctx: AgentOperationContext,
|
||||
): Promise<AgentToolResult> {
|
||||
const lines: string[] = []
|
||||
const files = []
|
||||
for (const file of output.files) {
|
||||
if (!file.key) throw new Error(`download_asset returned no storage key for ${file.assetId}`)
|
||||
const saved = await saveToSandbox(
|
||||
const saved = await saveToRunner(
|
||||
file.key,
|
||||
`${file.assetId}_${sanitizeFilename(file.name) || 'file'}`,
|
||||
ctx,
|
||||
)
|
||||
lines.push(
|
||||
`Downloaded "${file.name}" (ID: ${file.assetId}, ${file.kind}, ${saved.contentType}, ${formatSize(saved.size)}) to "${saved.filePath}".`,
|
||||
@@ -87,12 +103,12 @@ export async function presentDownloadOutput(
|
||||
files.push({ assetId: file.assetId, name: file.name, kind: file.kind, ...saved })
|
||||
}
|
||||
lines.push(CLEANUP_REMINDER)
|
||||
return { content: [{ type: 'text', text: lines.join('\n') }], details: { files } }
|
||||
return { output: [{ type: 'text', text: lines.join('\n') }], details: { files } }
|
||||
}
|
||||
|
||||
const downloadAssetExtension: AgentToolExtension = {
|
||||
description:
|
||||
`Downloads up to ${MAX_DOWNLOAD_ASSETS} workspace files into the local .pi directory, for processing with local scripts or bash commands. ` +
|
||||
`Downloads up to ${MAX_DOWNLOAD_ASSETS} workspace files into the runner's .pi directory, for processing with local scripts or bash commands. ` +
|
||||
'By default it saves the web preview version (proxy) when there is one, e.g. the transcoded video or the web image; set proxy to false for the original uploaded file. ' +
|
||||
'A version stack downloads its latest version; folders are not supported. ' +
|
||||
'To download a specific artifact from read_asset (e.g. a frame or page), pass its storage key as "key" instead of assetIds. ' +
|
||||
@@ -118,9 +134,9 @@ const downloadAssetExtension: AgentToolExtension = {
|
||||
throw new Error('Provide either "assetIds" or "key", not both.')
|
||||
}
|
||||
if (!ctx.userId) throw new Error('User ID is required for authorization.')
|
||||
return downloadKey(extra.key, ctx.userId)
|
||||
return downloadKey(extra.key, ctx)
|
||||
},
|
||||
present: (output) => presentDownloadOutput(output as DownloadAssetOutput),
|
||||
present: (output, _extra, ctx) => presentDownloadOutput(output as DownloadAssetOutput, ctx),
|
||||
}
|
||||
|
||||
registerAgentToolExtension('download_asset', downloadAssetExtension)
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { resolveAnnotationsById } from '@shumai/core/src/asset-read/annotations'
|
||||
import { authzService } from '@shumai/core/src/authz/authz'
|
||||
import { getOperation } from '@shumai/core/src/operations'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { degreeToSecond } from '@shumai/core/src/utils/model3d'
|
||||
import {
|
||||
prisma,
|
||||
WorkflowTaskType,
|
||||
WorkflowTaskStatus,
|
||||
WorkflowTaskType,
|
||||
type Asset,
|
||||
type WorkflowTask,
|
||||
} from '@shumai/db'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { workflowService } from '@shumai/workflow-core'
|
||||
import { authzService } from '@shumai/core/src/authz/authz'
|
||||
import { resolveAnnotationsById } from '@shumai/core/src/asset-read/annotations'
|
||||
import { getOperation } from '@shumai/core/src/operations'
|
||||
import { degreeToSecond } from '@shumai/core/src/utils/model3d'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { toAgentTool } from '../operation-tools'
|
||||
import { createAutofillReadAssetTool } from './read-asset'
|
||||
|
||||
@@ -90,7 +91,7 @@ type Details = {
|
||||
taskId?: string
|
||||
isTruncated?: boolean
|
||||
}
|
||||
function details(result: { details: unknown }): Details {
|
||||
function details(result: { details?: unknown }): Details {
|
||||
return result.details as Details
|
||||
}
|
||||
|
||||
@@ -212,7 +213,7 @@ describe('read_asset agent tool', () => {
|
||||
})
|
||||
|
||||
it('throws an authorization error if the user ID is empty', async () => {
|
||||
await expect(readAssetTool('').execute('call-1', { assetId: 'asset-1' })).rejects.toThrow(
|
||||
await expect(invokeTool(readAssetTool(''), 'call-1', { assetId: 'asset-1' })).rejects.toThrow(
|
||||
'User ID is required for authorization.',
|
||||
)
|
||||
})
|
||||
@@ -220,7 +221,7 @@ describe('read_asset agent tool', () => {
|
||||
it('throws if the asset is not found', async () => {
|
||||
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
|
||||
await expect(
|
||||
readAssetTool().execute('call-1', {
|
||||
invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'asset-1',
|
||||
annotationId: null,
|
||||
imageConfig: null,
|
||||
@@ -233,7 +234,7 @@ describe('read_asset agent tool', () => {
|
||||
it('does not plan or render anything when Read is denied', async () => {
|
||||
vi.mocked(authzService.hasPermission).mockRejectedValue(new Error('Forbidden'))
|
||||
await expect(
|
||||
readAssetTool().execute('call-1', {
|
||||
invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'vid-1',
|
||||
videoConfig: { start: 0, end: 1, count: 1 },
|
||||
}),
|
||||
@@ -244,7 +245,7 @@ describe('read_asset agent tool', () => {
|
||||
|
||||
it('rejects unsupported asset types', async () => {
|
||||
mockAsset({ id: 'zip-1', name: 'bundle.zip', mediaType: 'application/zip', media: null })
|
||||
await expect(readAssetTool().execute('call-1', { assetId: 'zip-1' })).rejects.toThrow(
|
||||
await expect(invokeTool(readAssetTool(), 'call-1', { assetId: 'zip-1' })).rejects.toThrow(
|
||||
'Unsupported asset type for read_asset. Asset zip-1 has media type "application/zip".',
|
||||
)
|
||||
})
|
||||
@@ -254,7 +255,7 @@ describe('read_asset agent tool', () => {
|
||||
mockAsset(imageAsset)
|
||||
mockObjects()
|
||||
|
||||
const result = await readAssetTool().execute('call-1', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'img-1',
|
||||
annotationId: null,
|
||||
imageConfig: null,
|
||||
@@ -289,14 +290,17 @@ describe('read_asset agent tool', () => {
|
||||
it('ignores an empty imageConfig', async () => {
|
||||
mockAsset(imageAsset)
|
||||
mockObjects()
|
||||
const result = await readAssetTool().execute('call-1', { assetId: 'img-1', imageConfig: {} })
|
||||
const result = await invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'img-1',
|
||||
imageConfig: {},
|
||||
})
|
||||
expect(details(result).key).toBe('proxy/photo.webp')
|
||||
})
|
||||
|
||||
it('rejects a config meant for another asset type', async () => {
|
||||
mockAsset(imageAsset)
|
||||
await expect(
|
||||
readAssetTool().execute('call-1', {
|
||||
invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'img-1',
|
||||
docConfig: { mode: 'pages', startPage: 1, endPage: 1 },
|
||||
}),
|
||||
@@ -312,7 +316,7 @@ describe('read_asset agent tool', () => {
|
||||
mockTaskFinished('task-anno', { key: 'annotated/photo.webp' })
|
||||
mockObjects()
|
||||
|
||||
const result = await readAssetTool().execute('call-1', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'img-1',
|
||||
annotationId: 'comment-1',
|
||||
})
|
||||
@@ -348,7 +352,7 @@ describe('read_asset agent tool', () => {
|
||||
mockAsset(modelAsset)
|
||||
mockObjects()
|
||||
|
||||
const result = await readAssetTool().execute('call-3d', { assetId: 'model-1' })
|
||||
const result = await invokeTool(readAssetTool(), 'call-3d', { assetId: 'model-1' })
|
||||
|
||||
expect(s3Service.getObject).toHaveBeenCalledWith('shumai', 'proxy/views.webp')
|
||||
const text = textOf(result)
|
||||
@@ -367,7 +371,7 @@ describe('read_asset agent tool', () => {
|
||||
|
||||
it('supports s3KeyOnly for 3D assets', async () => {
|
||||
mockAsset(modelAsset)
|
||||
const result = await readAssetTool().execute('call-3d-s3', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-3d-s3', {
|
||||
assetId: 'model-1',
|
||||
s3KeyOnly: true,
|
||||
})
|
||||
@@ -381,7 +385,7 @@ describe('read_asset agent tool', () => {
|
||||
it('rejects videoConfig for 3D assets', async () => {
|
||||
mockAsset(modelAsset)
|
||||
await expect(
|
||||
readAssetTool().execute('call-3d-err', {
|
||||
invokeTool(readAssetTool(), 'call-3d-err', {
|
||||
assetId: 'model-1',
|
||||
videoConfig: { start: 0, end: 4, count: 4 },
|
||||
}),
|
||||
@@ -392,9 +396,9 @@ describe('read_asset agent tool', () => {
|
||||
|
||||
it('throws if the 4-views image is missing when the overview is requested', async () => {
|
||||
mockAsset({ ...modelAsset, media: { proxyType: '3d', imageTranscodes: [] } })
|
||||
await expect(readAssetTool().execute('call-3d-err2', { assetId: 'model-1' })).rejects.toThrow(
|
||||
'4-views image (views.webp) is missing',
|
||||
)
|
||||
await expect(
|
||||
invokeTool(readAssetTool(), 'call-3d-err2', { assetId: 'model-1' }),
|
||||
).rejects.toThrow('4-views image (views.webp) is missing')
|
||||
})
|
||||
|
||||
it('renders a turntable frame when config3d.degree is provided', async () => {
|
||||
@@ -405,7 +409,7 @@ describe('read_asset agent tool', () => {
|
||||
})
|
||||
mockObjects()
|
||||
|
||||
const result = await readAssetTool().execute('call-3d-degree', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-3d-degree', {
|
||||
assetId: 'model-1',
|
||||
config3d: { degree: 45 },
|
||||
})
|
||||
@@ -446,10 +450,10 @@ describe('read_asset agent tool', () => {
|
||||
mockAsset(modelAsset)
|
||||
const tool = readAssetTool()
|
||||
await expect(
|
||||
tool.execute('call-1', { assetId: 'model-1', config3d: { degree: 40 } }),
|
||||
invokeTool(tool, 'call-1', { assetId: 'model-1', config3d: { degree: 40 } }),
|
||||
).rejects.toThrow('Invalid turntable degree: 40')
|
||||
await expect(
|
||||
tool.execute('call-2', { assetId: 'model-1', config3d: { degree: 360 } }),
|
||||
invokeTool(tool, 'call-2', { assetId: 'model-1', config3d: { degree: 360 } }),
|
||||
).rejects.toThrow('Invalid turntable degree: 360')
|
||||
expect(resolveAnnotationsById).not.toHaveBeenCalled()
|
||||
})
|
||||
@@ -463,7 +467,7 @@ describe('read_asset agent tool', () => {
|
||||
})
|
||||
mockObjects()
|
||||
|
||||
const result = await readAssetTool().execute('call-3d-ann', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-3d-ann', {
|
||||
assetId: 'model-1',
|
||||
annotationId: 'comm-1',
|
||||
config3d: null,
|
||||
@@ -500,7 +504,7 @@ describe('read_asset agent tool', () => {
|
||||
})
|
||||
mockObjects()
|
||||
|
||||
const result = await readAssetTool().execute('call-3d-session-ann', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-3d-session-ann', {
|
||||
assetId: 'model-1',
|
||||
annotationId: 'session-entry-1',
|
||||
})
|
||||
@@ -524,7 +528,7 @@ describe('read_asset agent tool', () => {
|
||||
mockAsset(modelAsset)
|
||||
vi.mocked(resolveAnnotationsById).mockResolvedValue({ annotations: arrow, timestamp: 0.5 })
|
||||
await expect(
|
||||
readAssetTool().execute('call-3d-conflict', {
|
||||
invokeTool(readAssetTool(), 'call-3d-conflict', {
|
||||
assetId: 'model-1',
|
||||
annotationId: 'comm-1',
|
||||
config3d: { degree: 90 },
|
||||
@@ -538,7 +542,7 @@ describe('read_asset agent tool', () => {
|
||||
it('throws if the turntable video is missing when an angle is requested', async () => {
|
||||
mockAsset({ ...modelAsset, media: { proxyType: '3d', videoTranscodes: [] } })
|
||||
await expect(
|
||||
readAssetTool().execute('call-1', { assetId: 'model-1', config3d: { degree: 45 } }),
|
||||
invokeTool(readAssetTool(), 'call-1', { assetId: 'model-1', config3d: { degree: 45 } }),
|
||||
).rejects.toThrow('turntable video (turntable.mp4) is missing')
|
||||
})
|
||||
})
|
||||
@@ -546,7 +550,7 @@ describe('read_asset agent tool', () => {
|
||||
describe('Video Assets', () => {
|
||||
it('requires videoConfig', async () => {
|
||||
mockAsset(videoAsset)
|
||||
await expect(readAssetTool().execute('call-1', { assetId: 'vid-1' })).rejects.toThrow(
|
||||
await expect(invokeTool(readAssetTool(), 'call-1', { assetId: 'vid-1' })).rejects.toThrow(
|
||||
'videoConfig with start, end, and count is required',
|
||||
)
|
||||
})
|
||||
@@ -554,7 +558,7 @@ describe('read_asset agent tool', () => {
|
||||
it('rejects an invalid time range', async () => {
|
||||
mockAsset(videoAsset)
|
||||
await expect(
|
||||
readAssetTool().execute('call-1', {
|
||||
invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'vid-1',
|
||||
videoConfig: { start: 10, end: 5, count: 1 },
|
||||
}),
|
||||
@@ -566,7 +570,7 @@ describe('read_asset agent tool', () => {
|
||||
it('rejects more than 20 frames', async () => {
|
||||
mockAsset(videoAsset)
|
||||
await expect(
|
||||
readAssetTool().execute('call-1', {
|
||||
invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'vid-1',
|
||||
videoConfig: { start: 0, end: 60, count: 21 },
|
||||
}),
|
||||
@@ -584,7 +588,7 @@ describe('read_asset agent tool', () => {
|
||||
})
|
||||
mockObjects()
|
||||
|
||||
const result = await readAssetTool().execute('call-1', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'vid-1',
|
||||
annotationId: null,
|
||||
imageConfig: null,
|
||||
@@ -638,7 +642,7 @@ describe('read_asset agent tool', () => {
|
||||
done: false,
|
||||
}))
|
||||
|
||||
const result = await readAssetTool().execute('call-1', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'vid-1',
|
||||
videoConfig: { start: 0, end: 10, count: 5 },
|
||||
maxWaitSeconds: 0,
|
||||
@@ -661,7 +665,7 @@ describe('read_asset agent tool', () => {
|
||||
mockTaskFinished('task-shot', { screenshots: [{ key: 's/1.webp', timestamp: 0 }] })
|
||||
mockObjects()
|
||||
|
||||
await readAssetTool().execute('call-1', {
|
||||
await invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'vid-1',
|
||||
videoConfig: { start: 0, end: 0, count: 1 },
|
||||
maxWaitSeconds: 600,
|
||||
@@ -673,7 +677,7 @@ describe('read_asset agent tool', () => {
|
||||
describe('Document Assets', () => {
|
||||
it('requires docConfig', async () => {
|
||||
mockAsset(pdfAsset)
|
||||
await expect(readAssetTool().execute('call-1', { assetId: 'doc-1' })).rejects.toThrow(
|
||||
await expect(invokeTool(readAssetTool(), 'call-1', { assetId: 'doc-1' })).rejects.toThrow(
|
||||
'docConfig with mode ("pages" or "text") is required',
|
||||
)
|
||||
})
|
||||
@@ -689,7 +693,7 @@ describe('read_asset agent tool', () => {
|
||||
})
|
||||
mockObjects()
|
||||
|
||||
const result = await readAssetTool().execute('call-1', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'doc-1',
|
||||
docConfig: { mode: 'pages', startPage: 1, endPage: 2 },
|
||||
})
|
||||
@@ -716,7 +720,7 @@ describe('read_asset agent tool', () => {
|
||||
it('rejects page ranges over 20 pages', async () => {
|
||||
mockAsset(pdfAsset)
|
||||
await expect(
|
||||
readAssetTool().execute('call-1', {
|
||||
invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'doc-1',
|
||||
docConfig: { mode: 'pages', startPage: 1, endPage: 25 },
|
||||
}),
|
||||
@@ -726,7 +730,7 @@ describe('read_asset agent tool', () => {
|
||||
it('rejects mode "text" on a binary PDF', async () => {
|
||||
mockAsset({ ...pdfAsset, name: 'manual.pdf' })
|
||||
await expect(
|
||||
readAssetTool().execute('call-1', {
|
||||
invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'doc-1',
|
||||
docConfig: { mode: 'text', startPage: null, endPage: null },
|
||||
}),
|
||||
@@ -736,7 +740,7 @@ describe('read_asset agent tool', () => {
|
||||
it('rejects mode "text" with annotationId', async () => {
|
||||
mockAsset(markdownAsset)
|
||||
await expect(
|
||||
readAssetTool().execute('call-1', {
|
||||
invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'doc-md',
|
||||
annotationId: 'comment-123',
|
||||
docConfig: { mode: 'text', startPage: null, endPage: null },
|
||||
@@ -748,7 +752,7 @@ describe('read_asset agent tool', () => {
|
||||
it('rejects page numbers in mode "text"', async () => {
|
||||
mockAsset(markdownAsset)
|
||||
await expect(
|
||||
readAssetTool().execute('call-1', {
|
||||
invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'doc-md',
|
||||
docConfig: { mode: 'text', startPage: 1, endPage: 2 },
|
||||
}),
|
||||
@@ -762,7 +766,7 @@ describe('read_asset agent tool', () => {
|
||||
contentType: 'text/markdown',
|
||||
})
|
||||
|
||||
const result = await readAssetTool().execute('call-1', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'doc-md',
|
||||
docConfig: { mode: 'text', startPage: null, endPage: null },
|
||||
})
|
||||
@@ -793,7 +797,7 @@ describe('read_asset agent tool', () => {
|
||||
contentType: 'text/markdown',
|
||||
})
|
||||
|
||||
const result = await readAssetTool().execute('call-1', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'doc-md',
|
||||
docConfig: { mode: 'text' },
|
||||
})
|
||||
@@ -824,7 +828,7 @@ describe('read_asset agent tool', () => {
|
||||
} as unknown as Asset)
|
||||
mockObjects()
|
||||
|
||||
const result = await readAssetTool().execute('call-1', { assetId: 'stack-1' })
|
||||
const result = await invokeTool(readAssetTool(), 'call-1', { assetId: 'stack-1' })
|
||||
|
||||
expect(prisma.asset.findFirst).toHaveBeenCalledWith({
|
||||
where: { parentId: 'stack-1', isDeleted: false },
|
||||
@@ -839,7 +843,7 @@ describe('read_asset agent tool', () => {
|
||||
describe('s3KeyOnly mode', () => {
|
||||
it('returns only the S3 key and MIME for image assets', async () => {
|
||||
mockAsset(imageAsset)
|
||||
const result = await readAssetTool().execute('call-1', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'img-1',
|
||||
s3KeyOnly: true,
|
||||
})
|
||||
@@ -859,7 +863,7 @@ describe('read_asset agent tool', () => {
|
||||
screenshots: [{ key: 'screenshots/shot1.webp', timestamp: 1.5 }],
|
||||
})
|
||||
|
||||
const result = await readAssetTool().execute('call-1', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'vid-1',
|
||||
s3KeyOnly: true,
|
||||
videoConfig: { start: 1.5, end: 1.5, count: 1 },
|
||||
@@ -875,7 +879,7 @@ describe('read_asset agent tool', () => {
|
||||
|
||||
it('returns only the document S3 key and MIME without reading the text', async () => {
|
||||
mockAsset({ ...markdownAsset, name: 'notes.md', storageKey: { key: 'raw/notes.md' } })
|
||||
const result = await readAssetTool().execute('call-1', {
|
||||
const result = await invokeTool(readAssetTool(), 'call-1', {
|
||||
assetId: 'doc-md',
|
||||
s3KeyOnly: true,
|
||||
docConfig: { mode: 'text', startPage: null, endPage: null },
|
||||
@@ -919,7 +923,7 @@ describe('get_asset_render agent tool', () => {
|
||||
})
|
||||
mockObjects()
|
||||
|
||||
const result = await getAssetRenderTool().execute('call-1', { taskId: 'task-1' })
|
||||
const result = await invokeTool(getAssetRenderTool(), 'call-1', { taskId: 'task-1' })
|
||||
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: { id: 'user-1' },
|
||||
@@ -936,14 +940,14 @@ describe('get_asset_render agent tool', () => {
|
||||
mockPendingTask('user-1')
|
||||
vi.mocked(workflowService.waitFor).mockImplementation(async (task) => ({ task, done: false }))
|
||||
|
||||
const result = await getAssetRenderTool().execute('call-1', { taskId: 'task-1' })
|
||||
const result = await invokeTool(getAssetRenderTool(), 'call-1', { taskId: 'task-1' })
|
||||
expect(textOf(result)).toContain('Call get_asset_render with taskId "task-1"')
|
||||
expect(details(result)).toEqual({ taskId: 'task-1', status: 'pending' })
|
||||
})
|
||||
|
||||
it('hides renders requested by another user', async () => {
|
||||
mockPendingTask('someone-else')
|
||||
await expect(getAssetRenderTool().execute('call-1', { taskId: 'task-1' })).rejects.toThrow(
|
||||
await expect(invokeTool(getAssetRenderTool(), 'call-1', { taskId: 'task-1' })).rejects.toThrow(
|
||||
'Render not found',
|
||||
)
|
||||
expect(workflowService.waitFor).not.toHaveBeenCalled()
|
||||
@@ -952,7 +956,7 @@ describe('get_asset_render agent tool', () => {
|
||||
it('hides renders once the user loses access to the asset', async () => {
|
||||
mockPendingTask('user-1')
|
||||
vi.mocked(authzService.hasPermission).mockRejectedValue(new Error('Forbidden'))
|
||||
await expect(getAssetRenderTool().execute('call-1', { taskId: 'task-1' })).rejects.toThrow(
|
||||
await expect(invokeTool(getAssetRenderTool(), 'call-1', { taskId: 'task-1' })).rejects.toThrow(
|
||||
'Render not found',
|
||||
)
|
||||
})
|
||||
@@ -963,7 +967,7 @@ describe('get_asset_render agent tool', () => {
|
||||
assetId: 'vid-1',
|
||||
payload: { projectId: 'project-1' },
|
||||
} as unknown as WorkflowTask)
|
||||
await expect(getAssetRenderTool().execute('call-1', { taskId: 'task-2' })).rejects.toThrow(
|
||||
await expect(invokeTool(getAssetRenderTool(), 'call-1', { taskId: 'task-2' })).rejects.toThrow(
|
||||
'Render not found',
|
||||
)
|
||||
})
|
||||
@@ -976,14 +980,14 @@ describe('autofill read_asset tool', () => {
|
||||
|
||||
it('requires targetAssetId', async () => {
|
||||
const tool = createAutofillReadAssetTool({ teamId: 'team-1' })
|
||||
await expect(tool.execute('call-1', { assetId: 'asset-1' })).rejects.toThrow(
|
||||
await expect(invokeTool(tool, 'call-1', { assetId: 'asset-1' })).rejects.toThrow(
|
||||
'Target asset ID is required for autofill agent authorization.',
|
||||
)
|
||||
})
|
||||
|
||||
it('requires teamId', async () => {
|
||||
const tool = createAutofillReadAssetTool({ targetAssetId: 'asset-1' })
|
||||
await expect(tool.execute('call-1', { assetId: 'asset-1' })).rejects.toThrow(
|
||||
await expect(invokeTool(tool, 'call-1', { assetId: 'asset-1' })).rejects.toThrow(
|
||||
'Team ID is required for autofill agent authorization.',
|
||||
)
|
||||
})
|
||||
@@ -991,7 +995,7 @@ describe('autofill read_asset tool', () => {
|
||||
it('denies assets from another team', async () => {
|
||||
mockAsset({ id: 'asset-1', project: { teamId: 'other-team' } })
|
||||
const tool = createAutofillReadAssetTool({ teamId: 'team-1', targetAssetId: 'asset-1' })
|
||||
await expect(tool.execute('call-1', { assetId: 'asset-1' })).rejects.toThrow(
|
||||
await expect(invokeTool(tool, 'call-1', { assetId: 'asset-1' })).rejects.toThrow(
|
||||
'Access denied: asset asset-1 does not belong to team team-1.',
|
||||
)
|
||||
})
|
||||
@@ -1002,7 +1006,7 @@ describe('autofill read_asset tool', () => {
|
||||
teamId: 'team-1',
|
||||
targetAssetId: 'target-asset-id',
|
||||
})
|
||||
await expect(tool.execute('call-1', { assetId: 'other-asset' })).rejects.toThrow(
|
||||
await expect(invokeTool(tool, 'call-1', { assetId: 'other-asset' })).rejects.toThrow(
|
||||
'Access denied: autofill agent can only read target asset target-asset-id.',
|
||||
)
|
||||
})
|
||||
@@ -1021,7 +1025,7 @@ describe('autofill read_asset tool', () => {
|
||||
targetAssetId: 'target-asset-id',
|
||||
})
|
||||
|
||||
const result = await tool.execute('call-1', {
|
||||
const result = await invokeTool(tool, 'call-1', {
|
||||
assetId: 'target-asset-id',
|
||||
annotationId: null,
|
||||
s3KeyOnly: true,
|
||||
@@ -1041,7 +1045,7 @@ describe('autofill read_asset tool', () => {
|
||||
mockTaskFinished('task-shot', { screenshots: [{ key: 's/1.webp', timestamp: 0 }] })
|
||||
|
||||
const tool = createAutofillReadAssetTool({ teamId: 'team-1', targetAssetId: 'vid-1' })
|
||||
await tool.execute('call-1', {
|
||||
await invokeTool(tool, 'call-1', {
|
||||
assetId: 'vid-1',
|
||||
s3KeyOnly: true,
|
||||
videoConfig: { start: 0, end: 0, count: 1 },
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
import { z } from 'zod'
|
||||
import { type AgentTool, type AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import { type ImageContent, type TextContent } from '@earendil-works/pi-ai'
|
||||
import { readAsset, type AssetReadOutput } from '@shumai/operations'
|
||||
import { assetReadService, toAssetReadOutput } from '@shumai/core/src/asset-read'
|
||||
import {
|
||||
type ToolRegistration as AgentTool,
|
||||
type ToolExecutionResult as AgentToolResult,
|
||||
} from '@earendil-works/pi-durable'
|
||||
import {
|
||||
assetReadService,
|
||||
MAX_TEXT_BYTES,
|
||||
MAX_TEXT_LINES,
|
||||
toAssetReadOutput,
|
||||
} from '@shumai/core/src/asset-read'
|
||||
import { getOperation, READ_WAIT_BUDGET_MS } from '@shumai/core/src/operations'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { getFileMimeType } from '@shumai/core/src/utils/file-mime'
|
||||
import { MAX_TEXT_BYTES, MAX_TEXT_LINES } from '@shumai/core/src/asset-read'
|
||||
import { readAsset, type AssetReadOutput } from '@shumai/operations'
|
||||
import { z } from 'zod'
|
||||
import {
|
||||
registerAgentToolExtension,
|
||||
splitAgentParams,
|
||||
@@ -24,7 +31,7 @@ const extraParameters = z.object({
|
||||
})
|
||||
|
||||
/** Details kept compatible with the previous tool; `sourceKeys` drives session image storage. */
|
||||
interface ReadAssetDetails {
|
||||
type ReadAssetDetails = {
|
||||
assetId: string
|
||||
name: string
|
||||
mediaType: string | null
|
||||
@@ -45,7 +52,7 @@ export async function presentReadOutput(
|
||||
if (output.status === 'pending') {
|
||||
const retry = output.retryAfterSeconds ?? 5
|
||||
return {
|
||||
content: [
|
||||
output: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `${output.summary} Call get_asset_render with taskId "${output.taskId}" in about ${retry} seconds to get the result.`,
|
||||
@@ -87,7 +94,7 @@ export async function presentReadOutput(
|
||||
const imageKeys = output.images.map((i) => i.key ?? '')
|
||||
const content: Array<TextContent | ImageContent> = [{ type: 'text', text }, ...imageBlocks]
|
||||
return {
|
||||
content,
|
||||
output: content,
|
||||
details: {
|
||||
assetId: output.asset.id,
|
||||
name: output.asset.name,
|
||||
@@ -131,11 +138,11 @@ export function createAutofillReadAssetTool(access: {
|
||||
const op = getOperation(readAsset.name)!
|
||||
return {
|
||||
name: readAsset.name,
|
||||
label: readAsset.title,
|
||||
|
||||
description: readAsset.description,
|
||||
parameters: toToolParameters(op, readAssetExtension),
|
||||
execute: async (_toolCallId, params) => {
|
||||
const { input, extra } = splitAgentParams(op, readAssetExtension, params)
|
||||
execute: async (params) => {
|
||||
const { input, extra } = await splitAgentParams(op, readAssetExtension, params)
|
||||
const parsed = readAsset.input.safeParse(input)
|
||||
if (!parsed.success) {
|
||||
throw new Error(`Invalid input for read_asset: ${z.prettifyError(parsed.error)}`)
|
||||
|
||||
@@ -1,21 +1,26 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { Value } from 'typebox/value'
|
||||
import { validateToolArguments } from '@earendil-works/pi-ai'
|
||||
import * as fs from 'fs'
|
||||
import * as os from 'os'
|
||||
import * as path from 'path'
|
||||
import { AuditAction, type AssetInfo } from '@shumai/dtos'
|
||||
import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
import { getOperation } from '@shumai/core/src/operations'
|
||||
import { runnerService } from '@shumai/core/src/runner'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { AuditAction, type AssetInfo } from '@shumai/dtos'
|
||||
import { Value } from 'typebox/value'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { fieldsToTypeBoxSchema } from '../index'
|
||||
import { toAgentTool, type AgentToolOptions } from '../operation-tools'
|
||||
import './upload'
|
||||
|
||||
const ctx = { userId: 'user-1', agentId: 'agent-1', teamId: 'team-1', projectId: 'proj-1' }
|
||||
const ctx = {
|
||||
userId: 'user-1',
|
||||
agentId: 'agent-1',
|
||||
teamId: 'team-1',
|
||||
projectId: 'proj-1',
|
||||
runnerId: null,
|
||||
}
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
|
||||
function tool(name: 'create_file' | 'create_version', options: AgentToolOptions = {}) {
|
||||
@@ -54,14 +59,6 @@ const promptField = {
|
||||
}
|
||||
|
||||
describe('create_file and create_version agent tools', () => {
|
||||
const tempFiles: string[] = []
|
||||
const tempFile = (content: string, filename: string): string => {
|
||||
const filePath = path.join(os.tmpdir(), filename)
|
||||
fs.writeFileSync(filePath, content)
|
||||
tempFiles.push(filePath)
|
||||
return filePath
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.spyOn(authzService, 'hasPermission').mockResolvedValue(undefined)
|
||||
vi.spyOn(auditLogService, 'logAction').mockResolvedValue(null)
|
||||
@@ -82,7 +79,6 @@ describe('create_file and create_version agent tools', () => {
|
||||
vi.spyOn(assetService, 'createVersion').mockResolvedValue(assetInfo({ name: 'v2' }))
|
||||
vi.spyOn(metadataService, 'listProjectFields').mockResolvedValue([promptField] as never)
|
||||
vi.spyOn(s3Service, 'presign').mockResolvedValue('https://storage.example/upload')
|
||||
// Read file streams to the end, as real storage would.
|
||||
vi.spyOn(s3Service, 'putObject').mockImplementation(async (_bucket, _key, body) => {
|
||||
if (body && typeof body === 'object' && Symbol.asyncIterator in body) {
|
||||
for await (const chunk of body as AsyncIterable<unknown>) void chunk
|
||||
@@ -95,20 +91,46 @@ describe('create_file and create_version agent tools', () => {
|
||||
contentType: 'text/markdown',
|
||||
eTag: '',
|
||||
}))
|
||||
vi.spyOn(s3Service, 'getObject').mockResolvedValue({
|
||||
buffer: Buffer.from('# Generated content'),
|
||||
contentType: 'text/markdown',
|
||||
})
|
||||
vi.spyOn(runnerService, 'requireOnlineRunner').mockResolvedValue({
|
||||
id: 'runner-1',
|
||||
teamId: 'team-1',
|
||||
name: 'Local Mac Mini',
|
||||
keyPrefix: 'shm_rn_12345678',
|
||||
isDefault: true,
|
||||
status: 'connected',
|
||||
createdAt: '2026-11-14T00:00:00.000Z',
|
||||
updatedAt: '2026-11-14T00:00:00.000Z',
|
||||
})
|
||||
vi.spyOn(runnerService, 'statOnRunner').mockResolvedValue({
|
||||
type: 'file.stat.result',
|
||||
executionId: 'exec-stat-1',
|
||||
ok: true,
|
||||
exists: true,
|
||||
isFile: true,
|
||||
size: 17,
|
||||
mimeType: 'text/markdown',
|
||||
})
|
||||
vi.spyOn(runnerService, 'uploadFromRunner').mockResolvedValue({
|
||||
type: 'file.upload.result',
|
||||
executionId: 'exec-ul-1',
|
||||
ok: true,
|
||||
size: 17,
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
for (const f of tempFiles.splice(0)) fs.rmSync(f, { force: true })
|
||||
})
|
||||
|
||||
describe('create_file', () => {
|
||||
it('uploads a local file to storage and creates the file as the agent', async () => {
|
||||
const filePath = tempFile('# Hello from disk', 'agent-create-file.md')
|
||||
|
||||
const result = await tool('create_file').execute('call-1', {
|
||||
it('uploads a file from the runner to storage and creates the file as the agent', async () => {
|
||||
const result = await invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: filePath,
|
||||
path: '.pi/agent-create-file.md',
|
||||
} as never)
|
||||
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
@@ -117,16 +139,21 @@ describe('create_file and create_version agent tools', () => {
|
||||
type: ResourceType.Asset,
|
||||
id: 'folder-1',
|
||||
})
|
||||
const [putBucket, key, , size, contentType] = vi.mocked(s3Service.putObject).mock.calls[0]
|
||||
expect(putBucket).toBe(bucket)
|
||||
expect(key).toMatch(/^files\/.+\/agent-create-file\.md$/)
|
||||
expect(size).toBe(Buffer.byteLength('# Hello from disk'))
|
||||
expect(contentType).toBe('text/markdown')
|
||||
expect(runnerService.requireOnlineRunner).toHaveBeenCalledWith('team-1', null)
|
||||
expect(runnerService.statOnRunner).toHaveBeenCalledWith(
|
||||
'runner-1',
|
||||
'.pi/agent-create-file.md',
|
||||
)
|
||||
expect(runnerService.uploadFromRunner).toHaveBeenCalledWith('runner-1', {
|
||||
srcPath: '.pi/agent-create-file.md',
|
||||
url: 'https://storage.example/upload',
|
||||
contentType: 'text/markdown',
|
||||
})
|
||||
|
||||
expect(assetService.createFile).toHaveBeenCalledWith({
|
||||
parentId: 'folder-1',
|
||||
name: 'agent-create-file.md',
|
||||
key,
|
||||
key: expect.stringMatching(/^files\/.+\/agent-create-file\.md$/),
|
||||
sizeByte: 17,
|
||||
contentType: 'text/markdown',
|
||||
creatorId: 'user-1',
|
||||
@@ -154,8 +181,92 @@ describe('create_file and create_version agent tools', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('falls back to temporary S3 storage when no runner is configured for .pi/generated/', async () => {
|
||||
vi.mocked(runnerService.requireOnlineRunner).mockRejectedValueOnce(
|
||||
new Error('No Runner is configured for this team.'),
|
||||
)
|
||||
|
||||
await invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: '.pi/generated/gen123/poster.png',
|
||||
} as never)
|
||||
|
||||
expect(runnerService.statOnRunner).not.toHaveBeenCalled()
|
||||
expect(s3Service.getObject).toHaveBeenCalledWith(bucket, 'temp/generated/gen123/poster.png')
|
||||
expect(s3Service.putObject).toHaveBeenCalled()
|
||||
expect(assetService.createFile).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
parentId: 'folder-1',
|
||||
name: 'poster.png',
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('falls back to temporary S3 storage when .pi/generated/ file is not staged on the online runner', async () => {
|
||||
vi.mocked(runnerService.statOnRunner).mockResolvedValueOnce({
|
||||
type: 'file.stat.result',
|
||||
executionId: 'exec-stat-missing',
|
||||
ok: true,
|
||||
exists: false,
|
||||
})
|
||||
|
||||
await invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: '.pi/generated/gen123/unstaged.png',
|
||||
} as never)
|
||||
|
||||
expect(runnerService.statOnRunner).toHaveBeenCalledWith(
|
||||
'runner-1',
|
||||
'.pi/generated/gen123/unstaged.png',
|
||||
)
|
||||
expect(s3Service.getObject).toHaveBeenCalledWith(bucket, 'temp/generated/gen123/unstaged.png')
|
||||
expect(runnerService.uploadFromRunner).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('prefers uploading edited or newly created .pi/generated/ files from the runner over S3', async () => {
|
||||
vi.mocked(s3Service.headObject).mockImplementation(async (_bucket, key) => {
|
||||
if (key === 'temp/generated/gen123/edited.png') {
|
||||
throw new Error('NotFound')
|
||||
}
|
||||
return {
|
||||
key,
|
||||
size: 17,
|
||||
lastModified: new Date(),
|
||||
contentType: 'text/markdown',
|
||||
eTag: '',
|
||||
}
|
||||
})
|
||||
|
||||
await invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: '.pi/generated/gen123/edited.png',
|
||||
} as never)
|
||||
|
||||
expect(runnerService.statOnRunner).toHaveBeenCalledWith(
|
||||
'runner-1',
|
||||
'.pi/generated/gen123/edited.png',
|
||||
)
|
||||
expect(runnerService.uploadFromRunner).toHaveBeenCalledWith('runner-1', {
|
||||
srcPath: '.pi/generated/gen123/edited.png',
|
||||
url: 'https://storage.example/upload',
|
||||
contentType: 'text/markdown',
|
||||
})
|
||||
expect(s3Service.getObject).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reads explicit temp/generated/ paths directly from S3 without touching the runner', async () => {
|
||||
await invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: 'temp/generated/gen123/direct.png',
|
||||
} as never)
|
||||
|
||||
expect(runnerService.requireOnlineRunner).not.toHaveBeenCalled()
|
||||
expect(runnerService.statOnRunner).not.toHaveBeenCalled()
|
||||
expect(s3Service.getObject).toHaveBeenCalledWith(bucket, 'temp/generated/gen123/direct.png')
|
||||
})
|
||||
|
||||
it('creates a text file from name and content', async () => {
|
||||
await tool('create_file').execute('call-1', {
|
||||
await invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
name: 'notes.md',
|
||||
content: '# Notes\n\nHello',
|
||||
@@ -172,7 +283,7 @@ describe('create_file and create_version agent tools', () => {
|
||||
})
|
||||
|
||||
it('defaults unknown extensions to text/plain when creating from content', async () => {
|
||||
await tool('create_file').execute('call-1', {
|
||||
await invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
name: 'script.ts',
|
||||
content: 'const x = 1',
|
||||
@@ -185,7 +296,7 @@ describe('create_file and create_version agent tools', () => {
|
||||
})
|
||||
|
||||
it('sanitizes the name', async () => {
|
||||
await tool('create_file').execute('call-1', {
|
||||
await invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
name: 'my/notes.md',
|
||||
content: 'hello',
|
||||
@@ -198,11 +309,9 @@ describe('create_file and create_version agent tools', () => {
|
||||
})
|
||||
|
||||
it('forwards Creation Context metadata, including new select options', async () => {
|
||||
const filePath = tempFile('# Hello', 'agent-create-file-meta.md')
|
||||
|
||||
await tool('create_file').execute('call-1', {
|
||||
await invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: filePath,
|
||||
path: '.pi/agent-create-file-meta.md',
|
||||
metadata: { prompt: { newOption: { value: 'Kling' } } },
|
||||
} as never)
|
||||
|
||||
@@ -212,11 +321,9 @@ describe('create_file and create_version agent tools', () => {
|
||||
})
|
||||
|
||||
it('treats null metadata as omitted', async () => {
|
||||
const filePath = tempFile('# Hello', 'agent-create-file-null.md')
|
||||
|
||||
await tool('create_file').execute('call-1', {
|
||||
await invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: filePath,
|
||||
path: '.pi/agent-create-file-null.md',
|
||||
metadata: null,
|
||||
} as never)
|
||||
|
||||
@@ -224,26 +331,24 @@ describe('create_file and create_version agent tools', () => {
|
||||
})
|
||||
|
||||
it('rejects metadata keys that are not Creation Context fields before uploading', async () => {
|
||||
const filePath = tempFile('# Hello', 'agent-create-file-bad-meta.md')
|
||||
|
||||
await expect(
|
||||
tool('create_file').execute('call-1', {
|
||||
invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: filePath,
|
||||
path: '.pi/agent-create-file-bad-meta.md',
|
||||
metadata: { status: 'approved' },
|
||||
} as never),
|
||||
).rejects.toThrow(/not allowed: status/)
|
||||
|
||||
expect(s3Service.putObject).not.toHaveBeenCalled()
|
||||
expect(runnerService.uploadFromRunner).not.toHaveBeenCalled()
|
||||
expect(assetService.createFile).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects a call with neither or both of "path" and "content"', async () => {
|
||||
await expect(
|
||||
tool('create_file').execute('call-1', { parent: 'folder-1' } as never),
|
||||
invokeTool(tool('create_file'), 'call-1', { parent: 'folder-1' } as never),
|
||||
).rejects.toThrow('Provide exactly one of "path" (a local file) or "content" (with "name").')
|
||||
await expect(
|
||||
tool('create_file').execute('call-1', {
|
||||
invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: '/some/file.md',
|
||||
name: 'notes.md',
|
||||
@@ -255,18 +360,24 @@ describe('create_file and create_version agent tools', () => {
|
||||
|
||||
it('requires a name with "content"', async () => {
|
||||
await expect(
|
||||
tool('create_file').execute('call-1', { parent: 'folder-1', content: 'x' } as never),
|
||||
invokeTool(tool('create_file'), 'call-1', { parent: 'folder-1', content: 'x' } as never),
|
||||
).rejects.toThrow('"name" is required')
|
||||
})
|
||||
|
||||
it('fails when the local file does not exist, without touching storage', async () => {
|
||||
it('fails when the file does not exist on the runner, without touching storage', async () => {
|
||||
vi.mocked(runnerService.statOnRunner).mockResolvedValue({
|
||||
type: 'file.stat.result',
|
||||
executionId: 'exec-stat-2',
|
||||
ok: true,
|
||||
exists: false,
|
||||
})
|
||||
await expect(
|
||||
tool('create_file').execute('call-1', {
|
||||
invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: '/nonexistent/file.md',
|
||||
path: '.pi/nonexistent.md',
|
||||
} as never),
|
||||
).rejects.toThrow('Local file not found at path: /nonexistent/file.md')
|
||||
expect(s3Service.putObject).not.toHaveBeenCalled()
|
||||
).rejects.toThrow('Local file not found at path: .pi/nonexistent.md')
|
||||
expect(runnerService.uploadFromRunner).not.toHaveBeenCalled()
|
||||
expect(assetService.createFile).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
@@ -274,13 +385,15 @@ describe('create_file and create_version agent tools', () => {
|
||||
vi.mocked(authzService.hasPermission).mockRejectedValue(
|
||||
new Error('User is not a member of the team'),
|
||||
)
|
||||
const filePath = tempFile('# Not allowed', 'agent-create-file-denied.md')
|
||||
|
||||
await expect(
|
||||
tool('create_file').execute('call-1', { parent: 'folder-1', path: filePath } as never),
|
||||
invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: '.pi/agent-create-file-denied.md',
|
||||
} as never),
|
||||
).rejects.toThrow('User is not a member of the team')
|
||||
await expect(
|
||||
tool('create_file').execute('call-1', {
|
||||
invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
name: 'notes.md',
|
||||
content: 'x',
|
||||
@@ -289,6 +402,7 @@ describe('create_file and create_version agent tools', () => {
|
||||
|
||||
expect(s3Service.putObject).not.toHaveBeenCalled()
|
||||
expect(s3Service.presign).not.toHaveBeenCalled()
|
||||
expect(runnerService.uploadFromRunner).not.toHaveBeenCalled()
|
||||
expect(assetService.createFile).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
@@ -306,7 +420,7 @@ describe('create_file and create_version agent tools', () => {
|
||||
} as Parameters<typeof validateToolArguments>[1],
|
||||
)
|
||||
|
||||
await t.execute('call-1', validated as never)
|
||||
await invokeTool(t, 'call-1', validated as never)
|
||||
expect(assetService.createFile).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ name: 'random.txt' }),
|
||||
)
|
||||
@@ -321,19 +435,21 @@ describe('create_file and create_version agent tools', () => {
|
||||
})
|
||||
|
||||
describe('create_version', () => {
|
||||
it('uploads a local file and creates a version as the agent', async () => {
|
||||
const filePath = tempFile('# Hello from disk', 'agent-create-version.md')
|
||||
|
||||
const result = await tool('create_version').execute('call-1', {
|
||||
it('uploads a file from the runner and creates a version as the agent', async () => {
|
||||
const result = await invokeTool(tool('create_version'), 'call-1', {
|
||||
parent: 'file-1',
|
||||
path: filePath,
|
||||
path: '.pi/agent-create-version.md',
|
||||
} as never)
|
||||
|
||||
const key = vi.mocked(s3Service.putObject).mock.calls[0][1]
|
||||
expect(runnerService.uploadFromRunner).toHaveBeenCalledWith('runner-1', {
|
||||
srcPath: '.pi/agent-create-version.md',
|
||||
url: 'https://storage.example/upload',
|
||||
contentType: 'text/markdown',
|
||||
})
|
||||
expect(assetService.createVersion).toHaveBeenCalledWith({
|
||||
parentId: 'file-1',
|
||||
name: 'agent-create-version.md',
|
||||
key,
|
||||
key: expect.stringMatching(/^files\/.+\/agent-create-version\.md$/),
|
||||
sizeByte: 17,
|
||||
contentType: 'text/markdown',
|
||||
creatorId: 'user-1',
|
||||
@@ -347,11 +463,9 @@ describe('create_file and create_version agent tools', () => {
|
||||
})
|
||||
|
||||
it('forwards Creation Context metadata', async () => {
|
||||
const filePath = tempFile('# Hello', 'agent-create-version-meta.md')
|
||||
|
||||
await tool('create_version').execute('call-1', {
|
||||
await invokeTool(tool('create_version'), 'call-1', {
|
||||
parent: 'file-1',
|
||||
path: filePath,
|
||||
path: '.pi/agent-create-version-meta.md',
|
||||
metadata: { prompt: 'Generated using gemini' },
|
||||
} as never)
|
||||
|
||||
@@ -364,23 +478,31 @@ describe('create_file and create_version agent tools', () => {
|
||||
vi.mocked(authzService.hasPermission).mockRejectedValueOnce(
|
||||
new Error('User is not a member of the team'),
|
||||
)
|
||||
const filePath = tempFile('# Not allowed', 'agent-create-version-denied.md')
|
||||
|
||||
await expect(
|
||||
tool('create_version').execute('call-1', { parent: 'file-1', path: filePath } as never),
|
||||
invokeTool(tool('create_version'), 'call-1', {
|
||||
parent: 'file-1',
|
||||
path: '.pi/agent-create-version-denied.md',
|
||||
} as never),
|
||||
).rejects.toThrow('User is not a member of the team')
|
||||
expect(s3Service.putObject).not.toHaveBeenCalled()
|
||||
expect(runnerService.uploadFromRunner).not.toHaveBeenCalled()
|
||||
expect(assetService.createVersion).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('fails when the local file does not exist', async () => {
|
||||
vi.mocked(runnerService.statOnRunner).mockResolvedValue({
|
||||
type: 'file.stat.result',
|
||||
executionId: 'exec-stat-3',
|
||||
ok: true,
|
||||
exists: false,
|
||||
})
|
||||
await expect(
|
||||
tool('create_version').execute('call-1', {
|
||||
invokeTool(tool('create_version'), 'call-1', {
|
||||
parent: 'file-1',
|
||||
path: '/nonexistent/file.md',
|
||||
path: '.pi/nonexistent.md',
|
||||
} as never),
|
||||
).rejects.toThrow('Local file not found at path: /nonexistent/file.md')
|
||||
expect(s3Service.putObject).not.toHaveBeenCalled()
|
||||
).rejects.toThrow('Local file not found at path: .pi/nonexistent.md')
|
||||
expect(runnerService.uploadFromRunner).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('shows the agent its own parameters', () => {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { z } from 'zod'
|
||||
import { Type } from 'typebox'
|
||||
import type { AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import { createFile, type StartedUploadOutput } from '@shumai/operations'
|
||||
import type { ToolExecutionResult as AgentToolResult } from '@earendil-works/pi-durable'
|
||||
import { storageBucket } from '@shumai/core/src/asset-read'
|
||||
import { completeUploadOp, runOperation } from '@shumai/core/src/operations'
|
||||
import { runnerService } from '@shumai/core/src/runner'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { getFileMimeType, readFileMimeType } from '@shumai/core/src/utils/file-mime'
|
||||
import { getFileMimeType } from '@shumai/core/src/utils/file-mime'
|
||||
import { createFile, type StartedUploadOutput } from '@shumai/operations'
|
||||
import * as path from 'path'
|
||||
import { Type } from 'typebox'
|
||||
import { z } from 'zod'
|
||||
import {
|
||||
registerAgentToolExtension,
|
||||
type AgentOperationContext,
|
||||
@@ -32,35 +32,159 @@ function setCreationMetadata(schema: ToolParametersSchema, options: AgentToolOpt
|
||||
}
|
||||
}
|
||||
|
||||
function resolveLocalFile(localPath: string): string {
|
||||
const absolutePath = path.resolve(process.cwd(), localPath)
|
||||
if (!fs.existsSync(absolutePath) || !fs.statSync(absolutePath).isFile()) {
|
||||
function resolveGeneratedStorageKey(rawPath: string): {
|
||||
storageKey: string
|
||||
explicitStorageRef: boolean
|
||||
} | null {
|
||||
const cleaned = rawPath.replace(/\\/g, '/').replace(/^\.\//, '')
|
||||
if (cleaned.includes('..')) return null
|
||||
if (cleaned.startsWith('.pi/generated/')) {
|
||||
const rest = cleaned.slice('.pi/generated/'.length)
|
||||
return rest ? { storageKey: `temp/generated/${rest}`, explicitStorageRef: false } : null
|
||||
}
|
||||
if (cleaned.startsWith('temp/generated/')) {
|
||||
const rest = cleaned.slice('temp/generated/'.length)
|
||||
return rest ? { storageKey: cleaned, explicitStorageRef: true } : null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function normalizeRunnerPath(rawPath: string): string | null {
|
||||
const cleaned = rawPath.replace(/\\/g, '/').replace(/^\.\//, '')
|
||||
if (!cleaned || cleaned.startsWith('/') || cleaned.includes('..')) {
|
||||
return null
|
||||
}
|
||||
const normalized = path.posix.normalize(cleaned)
|
||||
if (normalized.startsWith('..') || normalized.startsWith('/')) {
|
||||
return null
|
||||
}
|
||||
if (normalized === '.pi' || normalized.startsWith('.pi/')) {
|
||||
return normalized
|
||||
}
|
||||
return `.pi/${normalized}`
|
||||
}
|
||||
|
||||
async function resolveTargetRunnerId(ctx?: AgentOperationContext): Promise<string> {
|
||||
if (ctx?.runnerId && !ctx.teamId) {
|
||||
if (!runnerService.isRunnerConnected(ctx.runnerId)) {
|
||||
throw new Error(
|
||||
'Runner is currently offline. Please start your runner with "shumai-cli runner start" and try again.',
|
||||
)
|
||||
}
|
||||
return ctx.runnerId
|
||||
}
|
||||
const runner = await runnerService.requireOnlineRunner(ctx?.teamId || '', ctx?.runnerId)
|
||||
return runner.id
|
||||
}
|
||||
|
||||
async function resolveStorageGeneratedSource(storageKey: string): Promise<{
|
||||
kind: 'generated'
|
||||
storageKey: string
|
||||
name: string
|
||||
contentType: string
|
||||
} | null> {
|
||||
const head = await s3Service.headObject(storageBucket(), storageKey).catch(() => null)
|
||||
if (!head) {
|
||||
return null
|
||||
}
|
||||
const name = path.posix.basename(storageKey)
|
||||
const contentType =
|
||||
head.contentType && head.contentType !== 'application/octet-stream'
|
||||
? head.contentType
|
||||
: getFileMimeType(null, name)
|
||||
return { kind: 'generated', storageKey, name, contentType }
|
||||
}
|
||||
|
||||
async function resolvePathSource(
|
||||
localPath: string,
|
||||
ctx?: AgentOperationContext,
|
||||
): Promise<
|
||||
| { kind: 'generated'; storageKey: string; name: string; contentType: string }
|
||||
| { kind: 'runner'; runnerId: string; runnerPath: string; name: string; contentType: string }
|
||||
> {
|
||||
const generatedRef = resolveGeneratedStorageKey(localPath)
|
||||
if (generatedRef?.explicitStorageRef) {
|
||||
const source = await resolveStorageGeneratedSource(generatedRef.storageKey)
|
||||
if (!source) {
|
||||
throw new Error(`Local file not found at path: ${localPath}`)
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
const runnerPath = normalizeRunnerPath(localPath)
|
||||
if (!runnerPath) {
|
||||
throw new Error(`Local file not found at path: ${localPath}`)
|
||||
}
|
||||
return absolutePath
|
||||
|
||||
let runnerError: unknown
|
||||
try {
|
||||
const runnerId = await resolveTargetRunnerId(ctx)
|
||||
const stat = await runnerService.statOnRunner(runnerId, runnerPath)
|
||||
if (stat.ok && stat.exists && stat.isFile !== false) {
|
||||
const name = path.posix.basename(runnerPath)
|
||||
const contentType =
|
||||
stat.mimeType && stat.mimeType !== 'application/octet-stream'
|
||||
? stat.mimeType
|
||||
: getFileMimeType(null, name)
|
||||
return { kind: 'runner', runnerId, runnerPath, name, contentType }
|
||||
}
|
||||
} catch (err) {
|
||||
if (!generatedRef) {
|
||||
throw err
|
||||
}
|
||||
runnerError = err
|
||||
}
|
||||
|
||||
// Fallback for unstaged generated files stored in temporary S3
|
||||
if (generatedRef) {
|
||||
const source = await resolveStorageGeneratedSource(generatedRef.storageKey)
|
||||
if (source) {
|
||||
return source
|
||||
}
|
||||
if (runnerError) {
|
||||
throw runnerError
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`Local file not found at path: ${localPath}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the file straight to the upload's storage key (the agent runs next to storage, so
|
||||
* it doesn't need the upload URL), then completes the upload as the agent.
|
||||
* Transfers the file from either temporary generated storage or the remote runner workspace
|
||||
* into the upload's storage key, then completes the upload as the agent.
|
||||
*/
|
||||
async function uploadAndComplete(
|
||||
raw: unknown,
|
||||
extra: Record<string, unknown>,
|
||||
ctx: AgentOperationContext,
|
||||
): Promise<AgentToolResult<unknown>> {
|
||||
): Promise<AgentToolResult> {
|
||||
const started = raw as StartedUploadOutput
|
||||
if (!started.key) throw new Error('The upload did not return a storage key')
|
||||
|
||||
if (typeof extra.path === 'string') {
|
||||
const absolutePath = resolveLocalFile(extra.path)
|
||||
await s3Service.putObject(
|
||||
storageBucket(),
|
||||
started.key,
|
||||
fs.createReadStream(absolutePath),
|
||||
fs.statSync(absolutePath).size,
|
||||
readFileMimeType(absolutePath),
|
||||
)
|
||||
const source =
|
||||
(extra.__resolvedSource as Awaited<ReturnType<typeof resolvePathSource>> | undefined) ??
|
||||
(await resolvePathSource(extra.path, ctx))
|
||||
|
||||
if (source.kind === 'generated') {
|
||||
const obj = await s3Service.getObject(storageBucket(), source.storageKey)
|
||||
await s3Service.putObject(
|
||||
storageBucket(),
|
||||
started.key,
|
||||
obj.buffer,
|
||||
obj.buffer.length,
|
||||
source.contentType,
|
||||
)
|
||||
} else {
|
||||
const uploadRes = await runnerService.uploadFromRunner(source.runnerId, {
|
||||
srcPath: source.runnerPath,
|
||||
url: started.uploadUrl,
|
||||
contentType: source.contentType,
|
||||
})
|
||||
if (!uploadRes.ok) {
|
||||
throw new Error(uploadRes.error || `Failed to upload file "${extra.path}" from runner`)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const content = Buffer.from(typeof extra.content === 'string' ? extra.content : '', 'utf-8')
|
||||
// The key ends with the file name, which gives the same type as the upload's.
|
||||
@@ -79,13 +203,13 @@ async function uploadAndComplete(
|
||||
{ ...ctx, via: 'agent' },
|
||||
{ uploadId: started.uploadId },
|
||||
)
|
||||
return { content: [{ type: 'text', text: JSON.stringify(asset, null, 2) }], details: {} }
|
||||
return { output: [{ type: 'text', text: JSON.stringify(asset, null, 2) }], details: {} }
|
||||
}
|
||||
|
||||
const createFileExtension: AgentToolExtension = {
|
||||
description:
|
||||
'Create a new file in a folder. Choose exactly ONE source: ' +
|
||||
'(1) "path" to upload a local file (e.g. one you made in .pi), OR ' +
|
||||
'(1) "path" to upload a file from .pi (e.g. a generated file in .pi/generated/... or a file created in .pi on the runner), OR ' +
|
||||
'(2) "name" and "content" to create a text file with that content. ' +
|
||||
METADATA_HINT,
|
||||
parameterOverrides: z.object({
|
||||
@@ -100,7 +224,9 @@ const createFileExtension: AgentToolExtension = {
|
||||
.string()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe('Absolute or relative path of a local file to upload. Omit when using "content".'),
|
||||
.describe(
|
||||
'Path of a file in .pi (e.g. ".pi/generated/..." or ".pi/output.mp4") to upload. Omit when using "content".',
|
||||
),
|
||||
content: z
|
||||
.string()
|
||||
.optional()
|
||||
@@ -108,18 +234,19 @@ const createFileExtension: AgentToolExtension = {
|
||||
}),
|
||||
hiddenParameters: ['contentType'],
|
||||
adjustParameters: setCreationMetadata,
|
||||
toInput(input, extra) {
|
||||
async toInput(input, extra, ctx) {
|
||||
const hasPath = typeof extra.path === 'string'
|
||||
const hasContent = typeof extra.content === 'string'
|
||||
if (hasPath === hasContent) {
|
||||
throw new Error('Provide exactly one of "path" (a local file) or "content" (with "name").')
|
||||
}
|
||||
if (hasPath) {
|
||||
const absolutePath = resolveLocalFile(extra.path as string)
|
||||
const source = await resolvePathSource(extra.path as string, ctx)
|
||||
extra.__resolvedSource = source
|
||||
return {
|
||||
...input,
|
||||
name: input.name ?? path.basename(absolutePath),
|
||||
contentType: readFileMimeType(absolutePath),
|
||||
name: input.name ?? source.name,
|
||||
contentType: source.contentType,
|
||||
}
|
||||
}
|
||||
if (typeof input.name !== 'string' || !input.name.trim()) {
|
||||
@@ -132,23 +259,26 @@ const createFileExtension: AgentToolExtension = {
|
||||
|
||||
const createVersionExtension: AgentToolExtension = {
|
||||
description:
|
||||
'Create a new version of an existing file (or version stack) from a local file path. ' +
|
||||
'Create a new version of an existing file (or version stack) from a file path in .pi. ' +
|
||||
'The new version becomes the current one. ' +
|
||||
METADATA_HINT,
|
||||
extraParameters: z.object({
|
||||
path: z
|
||||
.string()
|
||||
.min(1)
|
||||
.describe('Absolute or relative path of the local file to upload as the new version.'),
|
||||
.describe(
|
||||
'Path of the file in .pi (e.g. ".pi/generated/..." or ".pi/output.mp4") to upload as the new version.',
|
||||
),
|
||||
}),
|
||||
hiddenParameters: ['name', 'contentType'],
|
||||
adjustParameters: setCreationMetadata,
|
||||
toInput(input, extra) {
|
||||
const absolutePath = resolveLocalFile(extra.path as string)
|
||||
async toInput(input, extra, ctx) {
|
||||
const source = await resolvePathSource(extra.path as string, ctx)
|
||||
extra.__resolvedSource = source
|
||||
return {
|
||||
...input,
|
||||
name: path.basename(absolutePath),
|
||||
contentType: readFileMimeType(absolutePath),
|
||||
name: source.name,
|
||||
contentType: source.contentType,
|
||||
}
|
||||
},
|
||||
present: uploadAndComplete,
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { Value } from 'typebox/value'
|
||||
import { validateToolArguments } from '@earendil-works/pi-ai'
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { AuditAction, type AssetInfo, type ProjectInfo } from '@shumai/dtos'
|
||||
import { type ToolRegistration as AgentTool } from '@earendil-works/pi-durable'
|
||||
import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { projectService } from '@shumai/core/src/project/project'
|
||||
import { AuditAction, type AssetInfo, type ProjectInfo } from '@shumai/dtos'
|
||||
import { Value } from 'typebox/value'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { buildOperationTools } from './operation-tools'
|
||||
|
||||
const ctx = { userId: 'user-1', agentId: 'agent-1', teamId: 'team-1', projectId: 'proj-1' }
|
||||
@@ -33,7 +34,7 @@ function getTool(name: string): AgentTool {
|
||||
}
|
||||
|
||||
async function run(name: string, params: Record<string, unknown>) {
|
||||
const result = await getTool(name).execute('call-1', params as never)
|
||||
const result = await invokeTool(getTool(name), 'call-1', params as never)
|
||||
return { result, output: JSON.parse((result.content[0] as { text: string }).text) as unknown }
|
||||
}
|
||||
|
||||
@@ -72,6 +73,7 @@ describe('operation-backed agent tools', () => {
|
||||
'create_comment',
|
||||
'create_file',
|
||||
'create_folder',
|
||||
'create_project',
|
||||
'create_version',
|
||||
'create_version_stack',
|
||||
'delete_asset',
|
||||
@@ -102,6 +104,12 @@ describe('operation-backed agent tools', () => {
|
||||
const params = (name: string) =>
|
||||
Object.keys((getTool(name).parameters as unknown as { properties: object }).properties)
|
||||
expect(params('create_folder')).toEqual(['parent', 'name'])
|
||||
expect(params('create_project')).toEqual([
|
||||
'name',
|
||||
'teamId',
|
||||
'coverImageKey',
|
||||
'enableNotification',
|
||||
])
|
||||
expect(params('rename_asset')).toEqual(['assetId', 'name'])
|
||||
expect(params('move_assets')).toEqual(['assetIds', 'newParentId'])
|
||||
expect(params('delete_asset')).toEqual(['assetId'])
|
||||
@@ -136,7 +144,50 @@ describe('operation-backed agent tools', () => {
|
||||
arguments: { parent: 'folder-1' },
|
||||
} as Parameters<typeof validateToolArguments>[1],
|
||||
)
|
||||
await expect(tool.execute('call-1', validated as never)).resolves.toBeDefined()
|
||||
await expect(invokeTool(tool, 'call-1', validated as never)).resolves.toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('create_project', () => {
|
||||
it('uses ctx.teamId when teamId is omitted and logs project_create audit', async () => {
|
||||
vi.spyOn(projectService, 'createProject').mockResolvedValue({
|
||||
id: 'proj-new',
|
||||
name: 'Campaign',
|
||||
teamId: 'team-1',
|
||||
rootFolder: 'root-new',
|
||||
enableNotification: true,
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
} satisfies ProjectInfo)
|
||||
|
||||
const { output } = await run('create_project', { name: 'Campaign' })
|
||||
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: { id: 'user-1' },
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Team,
|
||||
id: 'team-1',
|
||||
})
|
||||
expect(projectService.createProject).toHaveBeenCalledWith(
|
||||
{ id: 'user-1' },
|
||||
{ teamId: 'team-1', name: 'Campaign', coverImageKey: undefined, enableNotification: true },
|
||||
)
|
||||
expect(auditLogService.logAction).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
action: AuditAction.project_create,
|
||||
teamId: 'team-1',
|
||||
projectId: 'proj-new',
|
||||
itemId: 'proj-new',
|
||||
userId: 'user-1',
|
||||
agentId: 'agent-1',
|
||||
via: 'agent',
|
||||
}),
|
||||
)
|
||||
expect(output).toEqual({
|
||||
id: 'proj-new',
|
||||
name: 'Campaign',
|
||||
teamId: 'team-1',
|
||||
rootFolderId: 'root-new',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
import { z } from 'zod'
|
||||
import type { TSchema } from 'typebox'
|
||||
import { Compile } from 'typebox/compile'
|
||||
import { type AgentTool, type AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import { dropNullOptionals } from '@shumai/operations'
|
||||
import {
|
||||
type ToolRegistration as AgentTool,
|
||||
type ToolExecutionResult as AgentToolResult,
|
||||
} from '@earendil-works/pi-durable'
|
||||
import {
|
||||
getOperationsForSurface,
|
||||
runOperation,
|
||||
type Operation,
|
||||
type OperationContext,
|
||||
} from '@shumai/core/src/operations'
|
||||
import { dropNullOptionals } from '@shumai/operations'
|
||||
import type { TSchema } from 'typebox'
|
||||
import { Compile } from 'typebox/compile'
|
||||
import { z } from 'zod'
|
||||
|
||||
/** Context for agent-invoked Operations; `via` is always `agent`. */
|
||||
export type AgentOperationContext = Omit<OperationContext, 'via'>
|
||||
export type AgentOperationContext = Omit<OperationContext, 'via'> & {
|
||||
runnerId?: string | null
|
||||
}
|
||||
|
||||
/** Per-session settings for building tools, e.g. schemas that depend on the project. */
|
||||
export interface AgentToolOptions {
|
||||
@@ -20,6 +25,10 @@ export interface AgentToolOptions {
|
||||
* parameter of tools that create files. Undefined when the project has none.
|
||||
*/
|
||||
creationMetadataSchema?: TSchema
|
||||
/**
|
||||
* Operation names to exclude from the built tool list (e.g. `download_asset` when no runner is configured).
|
||||
*/
|
||||
excludeOperations?: string[]
|
||||
}
|
||||
|
||||
/** The JSON Schema of a tool's parameters (an object schema). */
|
||||
@@ -51,7 +60,11 @@ export interface AgentToolExtension {
|
||||
/** Adjust the parameters' JSON Schema for this session (e.g. a project-specific field). */
|
||||
adjustParameters?(schema: ToolParametersSchema, options: AgentToolOptions): void
|
||||
/** Adjust the Operation input using the extra parameters. */
|
||||
toInput?(input: Record<string, unknown>, extra: Record<string, unknown>): Record<string, unknown>
|
||||
toInput?(
|
||||
input: Record<string, unknown>,
|
||||
extra: Record<string, unknown>,
|
||||
ctx?: AgentOperationContext,
|
||||
): Promise<Record<string, unknown>> | Record<string, unknown>
|
||||
/**
|
||||
* Handle an agent-only mode without running the Operation (e.g. a storage key instead of
|
||||
* an asset). Return undefined to run the Operation.
|
||||
@@ -60,12 +73,12 @@ export interface AgentToolExtension {
|
||||
input: Record<string, unknown>,
|
||||
extra: Record<string, unknown>,
|
||||
ctx: AgentOperationContext,
|
||||
): Promise<AgentToolResult<unknown> | undefined>
|
||||
): Promise<AgentToolResult | undefined>
|
||||
present(
|
||||
output: unknown,
|
||||
extra: Record<string, unknown>,
|
||||
ctx: AgentOperationContext,
|
||||
): Promise<AgentToolResult<unknown>>
|
||||
): Promise<AgentToolResult>
|
||||
}
|
||||
|
||||
const extensions = new Map<string, AgentToolExtension>()
|
||||
@@ -107,11 +120,12 @@ export function toToolParameters(
|
||||
}
|
||||
|
||||
/** Split agent-only parameters from the Operation input. */
|
||||
export function splitAgentParams(
|
||||
export async function splitAgentParams(
|
||||
op: Operation,
|
||||
extension: AgentToolExtension | undefined,
|
||||
params: unknown,
|
||||
): { input: unknown; extra: Record<string, unknown> } {
|
||||
ctx?: AgentOperationContext,
|
||||
): Promise<{ input: unknown; extra: Record<string, unknown> }> {
|
||||
const cleaned = dropNullOptionals(toolInputSchema(op, extension), params)
|
||||
if (!extension?.extraParameters || !cleaned || typeof cleaned !== 'object') {
|
||||
return { input: cleaned, extra: {} }
|
||||
@@ -122,7 +136,10 @@ export function splitAgentParams(
|
||||
for (const [key, value] of Object.entries(cleaned)) {
|
||||
;(extraKeys.has(key) ? extra : input)[key] = value
|
||||
}
|
||||
return { input: extension.toInput ? extension.toInput(input, extra) : input, extra }
|
||||
return {
|
||||
input: extension.toInput ? await extension.toInput(input, extra, ctx) : input,
|
||||
extra,
|
||||
}
|
||||
}
|
||||
|
||||
/** Expose an Operation as a native agent tool that runs through `runOperation`. */
|
||||
@@ -135,11 +152,10 @@ export function toAgentTool(
|
||||
const extension = extensions.get(definition.name)
|
||||
return {
|
||||
name: definition.name,
|
||||
label: definition.title,
|
||||
description: extension?.description ?? definition.description,
|
||||
parameters: toToolParameters(op, extension, options),
|
||||
execute: async (_toolCallId, params) => {
|
||||
const { input, extra } = splitAgentParams(op, extension, params)
|
||||
execute: async (params) => {
|
||||
const { input, extra } = await splitAgentParams(op, extension, params, ctx)
|
||||
if (extension?.intercept) {
|
||||
const handled = await extension.intercept(input as Record<string, unknown>, extra, ctx)
|
||||
if (handled) return handled
|
||||
@@ -149,7 +165,7 @@ export function toAgentTool(
|
||||
return extension.present(output, extra, ctx)
|
||||
}
|
||||
return {
|
||||
content: [{ type: 'text', text: JSON.stringify(output, null, 2) }],
|
||||
output: [{ type: 'text', text: JSON.stringify(output, null, 2) }],
|
||||
details: {},
|
||||
}
|
||||
},
|
||||
@@ -161,5 +177,8 @@ export function buildOperationTools(
|
||||
ctx: AgentOperationContext,
|
||||
options: AgentToolOptions = {},
|
||||
): AgentTool[] {
|
||||
return getOperationsForSurface('agent').map((op) => toAgentTool(op, ctx, options))
|
||||
const excluded = new Set(options.excludeOperations ?? [])
|
||||
return getOperationsForSurface('agent')
|
||||
.filter((op) => !excluded.has(op.definition.name))
|
||||
.map((op) => toAgentTool(op, ctx, options))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,638 @@
|
||||
import { BACKGROUND_CONTEXT as piContext } from '@earendil-works/chord/context'
|
||||
import type { Model, ModelThinkingLevel } from '@earendil-works/pi-ai'
|
||||
import {
|
||||
createModels,
|
||||
createProvider,
|
||||
envApiKeyAuth,
|
||||
InMemoryCredentialStore,
|
||||
type ProviderStreams,
|
||||
} from '@earendil-works/pi-ai'
|
||||
import { anthropicMessagesApi } from '@earendil-works/pi-ai/api/anthropic-messages.lazy'
|
||||
import { azureOpenAIResponsesApi } from '@earendil-works/pi-ai/api/azure-openai-responses.lazy'
|
||||
import { bedrockConverseStreamApi } from '@earendil-works/pi-ai/api/bedrock-converse-stream.lazy'
|
||||
import { googleGenerativeAIApi } from '@earendil-works/pi-ai/api/google-generative-ai.lazy'
|
||||
import { googleVertexApi } from '@earendil-works/pi-ai/api/google-vertex.lazy'
|
||||
import { mistralConversationsApi } from '@earendil-works/pi-ai/api/mistral-conversations.lazy'
|
||||
import { openAICodexResponsesApi } from '@earendil-works/pi-ai/api/openai-codex-responses.lazy'
|
||||
import { openAICompletionsApi } from '@earendil-works/pi-ai/api/openai-completions.lazy'
|
||||
import { openAIResponsesApi } from '@earendil-works/pi-ai/api/openai-responses.lazy'
|
||||
import { builtinProviders } from '@earendil-works/pi-ai/providers/all'
|
||||
import {
|
||||
createRegistry,
|
||||
defineExtension,
|
||||
GenerationTask,
|
||||
Harness,
|
||||
hook,
|
||||
type ToolRegistration as AgentTool,
|
||||
} from '@earendil-works/pi-durable'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import { hydrateRequest } from '@shumai/core/src/agent/session-content'
|
||||
import { agentSessionService } from '@shumai/core/src/agent/session-service'
|
||||
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { mediaGenerationService } from '@shumai/core/src/media-generation/media-generation'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
import { QuotaExceededError, quotaService } from '@shumai/core/src/quota/quota-service'
|
||||
import { runnerService } from '@shumai/core/src/runner'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { DEFAULT_DENIED_TOOLS } from '@shumai/dtos'
|
||||
import * as path from 'path'
|
||||
import { Type, type TSchema } from 'typebox'
|
||||
import { createAutofillReadAssetTool } from './operation-extensions'
|
||||
import { buildOperationTools } from './operation-tools'
|
||||
import { createGenerateImageTool } from './tools/generate-image'
|
||||
import { createGenerateVideoTool } from './tools/generate-video'
|
||||
import { createReadSkillTool } from './tools/read-skill'
|
||||
import { createReadThreadTool } from './tools/read-thread'
|
||||
import { createRunnerBashTool, type LoadedSkillRef } from './tools/runner-bash'
|
||||
|
||||
const activeHarnesses = new Set<Harness>()
|
||||
|
||||
export async function closeAgentSessions() {
|
||||
await Promise.allSettled([...activeHarnesses].map((harness) => harness.close(piContext)))
|
||||
await agentSessionService.closeAll()
|
||||
}
|
||||
|
||||
export interface DbModelInfo {
|
||||
modelId: string
|
||||
name: string | null
|
||||
config: PrismaJson.ModelConfig
|
||||
}
|
||||
|
||||
export interface DbProviderInfo {
|
||||
name: string
|
||||
config: PrismaJson.ProviderConfig
|
||||
models: DbModelInfo[]
|
||||
}
|
||||
|
||||
type ConfiguredApi = PrismaJson.ProviderConfig['api']
|
||||
|
||||
const apiAdapters = {
|
||||
'anthropic-messages': anthropicMessagesApi,
|
||||
'azure-openai-responses': azureOpenAIResponsesApi,
|
||||
'bedrock-converse-stream': bedrockConverseStreamApi,
|
||||
'google-generative-ai': googleGenerativeAIApi,
|
||||
'google-vertex': googleVertexApi,
|
||||
'mistral-conversations': mistralConversationsApi,
|
||||
'openai-codex-responses': openAICodexResponsesApi,
|
||||
'openai-completions': openAICompletionsApi,
|
||||
'openai-responses': openAIResponsesApi,
|
||||
} satisfies Record<ConfiguredApi, () => ProviderStreams>
|
||||
|
||||
export function getModelFromDb(providers: DbProviderInfo[], providerName: string, modelId: string) {
|
||||
const dbProvider = providers.find((p) => p.name === providerName)
|
||||
const dbModel = dbProvider?.models.find((m) => m.modelId === modelId)
|
||||
|
||||
if (!dbProvider || !dbModel) {
|
||||
throw new Error(
|
||||
`Provider "${providerName}" or model "${modelId}" not found in database configuration`,
|
||||
)
|
||||
}
|
||||
|
||||
const m = {
|
||||
id: modelId,
|
||||
name: dbModel.name || modelId,
|
||||
provider: providerName,
|
||||
api: 'openai-responses',
|
||||
baseUrl: '',
|
||||
reasoning: false,
|
||||
input: ['text'],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 4096,
|
||||
maxTokens: 4096,
|
||||
}
|
||||
|
||||
// Override with database config
|
||||
if (dbProvider.config.baseUrl) {
|
||||
m.baseUrl = dbProvider.config.baseUrl
|
||||
}
|
||||
Object.assign(m, dbModel.config)
|
||||
if (dbProvider.config.api) {
|
||||
m.api = dbProvider.config.api
|
||||
}
|
||||
if (dbModel.name) {
|
||||
m.name = dbModel.name
|
||||
}
|
||||
return m as Model<ConfiguredApi>
|
||||
}
|
||||
|
||||
export function getApiKeyAndHeadersFromDb(providers: DbProviderInfo[], providerName: string) {
|
||||
// Helper to resolve key from ENV if it matches an ENV var name
|
||||
const resolveKey = (key: string | undefined) => (key ? process.env[key] || key : undefined)
|
||||
|
||||
// Look up the provider in our providers list
|
||||
const dbProvider = providers.find((p) => p.name === providerName)
|
||||
const providerApiKey = dbProvider?.config?.apiKey
|
||||
if (providerApiKey) {
|
||||
return { apiKey: resolveKey(providerApiKey)! }
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
export interface CreateAgentSessionParams {
|
||||
teamId: string
|
||||
agentId: string
|
||||
providerName: string
|
||||
modelId: string
|
||||
systemPrompt: string
|
||||
teamSkills: Array<{ id: string; name: string; description?: string | null }>
|
||||
enabledSkillIds?: string[]
|
||||
allowedDomains?: string[]
|
||||
sessionId?: string
|
||||
userId?: string
|
||||
projectId?: string
|
||||
assetId?: string
|
||||
userCommentId?: string | null
|
||||
customTools?: AgentTool[]
|
||||
thinkingLevel?: string
|
||||
disableTools?: boolean
|
||||
providers: DbProviderInfo[]
|
||||
maxRetries?: number
|
||||
baseDelayMs?: number
|
||||
}
|
||||
|
||||
export async function createAgentSession(params: CreateAgentSessionParams) {
|
||||
const {
|
||||
teamId,
|
||||
agentId,
|
||||
providerName,
|
||||
modelId,
|
||||
systemPrompt,
|
||||
teamSkills,
|
||||
enabledSkillIds,
|
||||
sessionId,
|
||||
userId,
|
||||
projectId,
|
||||
assetId,
|
||||
userCommentId: passedUserCommentId,
|
||||
customTools = [],
|
||||
thinkingLevel,
|
||||
disableTools = false,
|
||||
maxRetries = 3,
|
||||
baseDelayMs = 2000,
|
||||
} = params
|
||||
|
||||
const record = sessionId
|
||||
? await prisma.agentSession.findUniqueOrThrow({ where: { id: sessionId } })
|
||||
: await prisma.agentSession.create({
|
||||
data: { agentId, userId, cwd: process.cwd(), assetId, userCommentId: passedUserCommentId },
|
||||
})
|
||||
const opened = await agentSessionService.openWriter(record.id, piContext, {
|
||||
teamId,
|
||||
userId,
|
||||
role: userId ? await resolveEffectiveRole(teamId, projectId, userId) : undefined,
|
||||
})
|
||||
const storage = opened.storage
|
||||
try {
|
||||
getModelFromDb(params.providers, providerName, modelId)
|
||||
|
||||
const role = userId ? await resolveEffectiveRole(teamId, projectId, userId) : undefined
|
||||
|
||||
const wrapToolWithQuota = (tool: AgentTool): AgentTool => {
|
||||
const originalExecute = tool.execute
|
||||
return {
|
||||
...tool,
|
||||
execute: async (args, api, context) => {
|
||||
try {
|
||||
await quotaService.checkQuota(
|
||||
{
|
||||
teamId,
|
||||
userId,
|
||||
role,
|
||||
resource: 'agent_tool_call_count',
|
||||
resourceData: { name: tool.name, toolName: tool.name },
|
||||
},
|
||||
1,
|
||||
)
|
||||
} catch (err) {
|
||||
if (err instanceof QuotaExceededError) {
|
||||
return {
|
||||
output: [{ type: 'text', text: `Quota exceeded: ${err.message}` }],
|
||||
details: { error: err.message },
|
||||
}
|
||||
}
|
||||
throw err
|
||||
}
|
||||
|
||||
const result = await originalExecute(args, api, context)
|
||||
|
||||
try {
|
||||
await quotaService.consumeQuota(
|
||||
{
|
||||
teamId,
|
||||
userId,
|
||||
role,
|
||||
resource: 'agent_tool_call_count',
|
||||
resourceData: { name: tool.name, toolName: tool.name },
|
||||
},
|
||||
1,
|
||||
)
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'Failed to record quota usage for agent tool call')
|
||||
}
|
||||
|
||||
return result
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const skillEnvs: Record<string, string> = {}
|
||||
const onEnvsAdded = (envs: Record<string, string>) => {
|
||||
Object.assign(skillEnvs, envs)
|
||||
}
|
||||
|
||||
const loadedSkills = new Map<string, LoadedSkillRef>()
|
||||
|
||||
// Restore env variables and loaded skill refs from previously loaded skills in this session
|
||||
if (sessionId) {
|
||||
try {
|
||||
const entries = await agentSessionService.getEntries(record.id)
|
||||
for (const entry of entries) {
|
||||
for (const message of entry.model ?? []) {
|
||||
if (
|
||||
message.role === 'toolResult' &&
|
||||
message.toolName === 'read_skill' &&
|
||||
!message.isError
|
||||
) {
|
||||
const details = message.details
|
||||
if (
|
||||
details &&
|
||||
typeof details === 'object' &&
|
||||
!Array.isArray(details) &&
|
||||
'skillId' in details &&
|
||||
typeof details.skillId === 'string'
|
||||
) {
|
||||
onEnvsAdded(await agentService.getSkillEnvs(details.skillId))
|
||||
const dbSkill = await prisma.skill.findUnique({
|
||||
where: { id: details.skillId },
|
||||
select: { id: true, assetId: true, hash: true },
|
||||
})
|
||||
if (dbSkill) {
|
||||
loadedSkills.set(dbSkill.id, dbSkill)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error({ err, sessionId }, 'Failed to restore skill environment variables')
|
||||
}
|
||||
}
|
||||
|
||||
const agent = await prisma.agent.findUnique({
|
||||
where: { id: agentId },
|
||||
})
|
||||
const agentConfig = agent?.config as PrismaJson.AgentConfig | null | undefined
|
||||
const deniedTools = agentConfig?.deniedTools ?? [...DEFAULT_DENIED_TOOLS]
|
||||
|
||||
const resolvedRunner = await runnerService.resolveRunnerForAgent(teamId, agent?.runnerId)
|
||||
const hasRunner = resolvedRunner.hasConfiguredRunner
|
||||
|
||||
const mediaTools: AgentTool[] = []
|
||||
// Agents acting for a user get read_asset / get_asset_render from the Operation registry
|
||||
// (see systemTools below); the autofill system agent has its own team-scoped read_asset.
|
||||
if (!userId && agent?.type === 'autofill' && assetId) {
|
||||
mediaTools.push(
|
||||
createAutofillReadAssetTool({
|
||||
teamId,
|
||||
targetAssetId: assetId,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
if (!deniedTools.includes('generate_image') || !deniedTools.includes('generate_video')) {
|
||||
try {
|
||||
const { imageModels, videoModels, providerKeys } =
|
||||
await mediaGenerationService.getValidModels(teamId)
|
||||
if (imageModels.length > 0 && !deniedTools.includes('generate_image')) {
|
||||
mediaTools.push(createGenerateImageTool(imageModels, providerKeys, userId))
|
||||
}
|
||||
if (videoModels.length > 0 && !deniedTools.includes('generate_video')) {
|
||||
mediaTools.push(createGenerateVideoTool(videoModels, providerKeys, userId))
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error({ err, teamId }, 'Failed to initialize media generation tools')
|
||||
}
|
||||
}
|
||||
|
||||
const runnerBash = createRunnerBashTool(skillEnvs, {
|
||||
teamId,
|
||||
agentRunnerId: agent?.runnerId,
|
||||
environment: resolvedRunner.runner
|
||||
? runnerService.getRunnerEnvironment(resolvedRunner.runner.id)
|
||||
: undefined,
|
||||
userId,
|
||||
role,
|
||||
getLoadedSkills: () => loadedSkills.values(),
|
||||
})
|
||||
|
||||
const registry = createRegistry()
|
||||
const onSkillLoaded = async (skill: LoadedSkillRef) => {
|
||||
loadedSkills.set(skill.id, skill)
|
||||
if (resolvedRunner.runner && runnerService.isRunnerConnected(resolvedRunner.runner.id)) {
|
||||
await runnerService.ensureSkillOnRunner(resolvedRunner.runner.id, skill).catch((err) => {
|
||||
logger.warn(
|
||||
{ err, skillId: skill.id, runnerId: resolvedRunner.runner?.id },
|
||||
'Failed to eagerly stage skill on runner',
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
const readSkill = createReadSkillTool(
|
||||
userId,
|
||||
onEnvsAdded,
|
||||
onSkillLoaded,
|
||||
enabledSkillIds,
|
||||
projectId,
|
||||
)
|
||||
|
||||
const systemTools: AgentTool[] = []
|
||||
if (userId) {
|
||||
const creationMetadataSchema = await buildProjectMetadataSchema(projectId)
|
||||
systemTools.push(
|
||||
// Shared Operations (also exposed via MCP and the CLI): browsing, searching, organizing,
|
||||
// reading and downloading assets, creating files and versions, comments and metadata
|
||||
...buildOperationTools(
|
||||
{ userId, agentId, teamId, projectId, runnerId: agent?.runnerId },
|
||||
{
|
||||
creationMetadataSchema,
|
||||
excludeOperations: hasRunner ? [] : ['download_asset'],
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
const readThread = createReadThreadTool({ userId, teamId })
|
||||
const rawTools = [
|
||||
...mediaTools,
|
||||
readSkill,
|
||||
readThread,
|
||||
...(hasRunner ? [runnerBash] : []),
|
||||
...systemTools,
|
||||
...customTools,
|
||||
]
|
||||
const allTools = rawTools.map((tool) => wrapToolWithQuota(tool))
|
||||
const enabledTools = disableTools
|
||||
? []
|
||||
: allTools.filter((tool) => !deniedTools.includes(tool.name))
|
||||
|
||||
const credentials = new InMemoryCredentialStore()
|
||||
const resolveKey = (key: string | undefined) => (key ? process.env[key] || key : undefined)
|
||||
for (const p of params.providers) {
|
||||
const apiKey = resolveKey(p.config?.apiKey)
|
||||
if (apiKey) {
|
||||
await credentials.modify(p.name, async () => ({ type: 'api_key' as const, key: apiKey }))
|
||||
}
|
||||
}
|
||||
const modelsStore = createModels({ credentials })
|
||||
for (const provider of builtinProviders()) {
|
||||
modelsStore.setProvider(provider)
|
||||
}
|
||||
|
||||
const configuredModel = getModelFromDb(params.providers, providerName, modelId)
|
||||
const builtin = modelsStore.getProvider(providerName)
|
||||
if (builtin?.getModels().some((candidate) => candidate.api === configuredModel.api)) {
|
||||
modelsStore.setProvider({ ...builtin, getModels: () => [configuredModel] })
|
||||
} else {
|
||||
// Providers that share an API can still require different authentication and headers.
|
||||
modelsStore.setProvider(
|
||||
createProvider({
|
||||
id: providerName,
|
||||
models: [configuredModel],
|
||||
api: apiAdapters[configuredModel.api](),
|
||||
auth: { apiKey: envApiKeyAuth('API key', []) },
|
||||
}),
|
||||
)
|
||||
}
|
||||
registry.install(
|
||||
defineExtension({
|
||||
name: 'shumai',
|
||||
tools: enabledTools,
|
||||
hooks: [
|
||||
hook(GenerationTask, {
|
||||
beforeRequest: async ({ messages }, api, context) => ({
|
||||
messages: await hydrateRequest(messages, api, context),
|
||||
}),
|
||||
}),
|
||||
],
|
||||
}),
|
||||
)
|
||||
const harness = await Harness.open(
|
||||
storage,
|
||||
{
|
||||
models: modelsStore,
|
||||
registry,
|
||||
settings: { retry: { enabled: true, maxRetries, baseDelayMs } },
|
||||
onReport: (report) =>
|
||||
logger.error({ kind: report.kind, err: report.error }, 'Agent runtime report'),
|
||||
},
|
||||
piContext,
|
||||
)
|
||||
const buildPrompt = async () => {
|
||||
let prompt = systemPrompt
|
||||
|
||||
if (disableTools) {
|
||||
return prompt
|
||||
}
|
||||
|
||||
if (hasRunner && !deniedTools.includes('bash')) {
|
||||
prompt +=
|
||||
'\n\n' +
|
||||
[
|
||||
'# Runner Environment & Workspace Rules',
|
||||
'Your shell environment (`bash` tool) runs on a dedicated Runner workspace:',
|
||||
'1. **Workspace Directory**: Read and write files inside the `.pi` folder in the current working directory.',
|
||||
'2. **Generated Media Staging**: Files generated by `generate_image` or `generate_video` are returned at virtual paths `.pi/generated/<id>/<filename>`. You can pass those paths directly to `create_file` or `create_version` without using `bash`. If you need to process a generated file inside `bash`, pass its path in the `inputFiles` array parameter of the `bash` tool so it is staged into the runner workspace before the command runs.',
|
||||
'3. **Temporary File Cleanup**: If you download workspace assets to `.pi` using the `download_asset` tool for local processing or script execution, you MUST delete the temporary downloaded files from `.pi` after finishing your task.',
|
||||
].join('\n')
|
||||
}
|
||||
|
||||
if (teamSkills.length > 0) {
|
||||
prompt += formatSkillsForPrompt(teamSkills)
|
||||
}
|
||||
return prompt
|
||||
}
|
||||
const session = await harness.root(piContext)
|
||||
await session.configure(
|
||||
{
|
||||
model: { provider: providerName, modelId },
|
||||
thinkingLevel: (thinkingLevel || 'off') as ModelThinkingLevel,
|
||||
instructions: await buildPrompt(),
|
||||
tools: enabledTools,
|
||||
},
|
||||
piContext,
|
||||
)
|
||||
const onLost = () => {
|
||||
void harness.close(piContext)
|
||||
}
|
||||
opened.signal.addEventListener('abort', onLost, { once: true })
|
||||
activeHarnesses.add(harness)
|
||||
harness.subscribeClose(() => {
|
||||
activeHarnesses.delete(harness)
|
||||
opened.signal.removeEventListener('abort', onLost)
|
||||
})
|
||||
return { session, harness, storage, sessionId: record.id }
|
||||
} catch (error) {
|
||||
await storage.close(piContext)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export interface AutofillField {
|
||||
id: string
|
||||
config: PrismaJson.FieldConfig
|
||||
description?: string | null
|
||||
}
|
||||
|
||||
export function fieldsToTypeBoxSchema(fields: AutofillField[]) {
|
||||
const newOptionSchema = Type.Object(
|
||||
{
|
||||
newOption: Type.Object(
|
||||
{
|
||||
value: Type.String({
|
||||
minLength: 1,
|
||||
description:
|
||||
'The new option display name/value to create and select if none of the existing options match.',
|
||||
}),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
)
|
||||
|
||||
const properties: Record<string, TSchema> = {}
|
||||
for (const f of fields) {
|
||||
let schema: TSchema
|
||||
const fieldName = f.config.name
|
||||
const fieldDesc = f.description || fieldName
|
||||
const baseDescription = `The field '${fieldName}' represents ${fieldDesc}.`
|
||||
switch (f.config.type) {
|
||||
case 'text':
|
||||
case 'longText':
|
||||
schema = Type.String({ description: baseDescription })
|
||||
break
|
||||
case 'number':
|
||||
case 'rating':
|
||||
schema = Type.Number({ description: baseDescription })
|
||||
break
|
||||
case 'toggle':
|
||||
schema = Type.Boolean({ description: baseDescription })
|
||||
break
|
||||
case 'select': {
|
||||
const options = f.config.select?.options || []
|
||||
const description =
|
||||
options.length > 0
|
||||
? `${baseDescription}\nSelect one existing option ID or provide {"newOption": {"value": "..."}} to create a new option.\n\nAvailable options:\n${options.map((o) => `- ${o.displayName} => ${o.id}`).join('\n')}`
|
||||
: `${baseDescription}\nProvide {"newOption": {"value": "..."}} with the desired option name.`
|
||||
if (options.length > 0) {
|
||||
schema = Type.Union(
|
||||
[
|
||||
Type.String({
|
||||
enum: options.map((o) => o.id),
|
||||
}),
|
||||
newOptionSchema,
|
||||
],
|
||||
{ description },
|
||||
)
|
||||
} else {
|
||||
schema = Type.Object(newOptionSchema.properties, {
|
||||
additionalProperties: false,
|
||||
description,
|
||||
})
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'selectMulti': {
|
||||
const options = f.config.selectMulti?.options || []
|
||||
const description =
|
||||
options.length > 0
|
||||
? `${baseDescription}\nSelect applicable option IDs or provide {"newOption": {"value": "..."}} objects for new options (e.g. ["opt1", {"newOption": {"value": "new_name"}}]).\n\nAvailable options:\n${options.map((o) => `- ${o.displayName} => ${o.id}`).join('\n')}`
|
||||
: `${baseDescription}\nProvide an array of {"newOption": {"value": "..."}} objects for new options.`
|
||||
const itemSchema =
|
||||
options.length > 0
|
||||
? Type.Union([
|
||||
Type.String({
|
||||
enum: options.map((o) => o.id),
|
||||
}),
|
||||
newOptionSchema,
|
||||
])
|
||||
: newOptionSchema
|
||||
schema = Type.Array(itemSchema, { description })
|
||||
break
|
||||
}
|
||||
default:
|
||||
schema = Type.String({ description: baseDescription })
|
||||
}
|
||||
// Every field is required but nullable: the agent must return each field key,
|
||||
// using `null` for values it does not know. Nulls are skipped when persisting.
|
||||
// This matches the OpenAI structured-outputs strict-mode convention (all
|
||||
// properties required, optionality expressed via `null`).
|
||||
properties[f.id] = Type.Union([schema, Type.Null()])
|
||||
}
|
||||
|
||||
return Type.Object(properties, { additionalProperties: false })
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the strict, all-optional metadata schema for the project's CREATION_CONTEXT
|
||||
* fields (the same field set the agent may attach when creating files/versions).
|
||||
* Returns undefined when no project is known or the project defines no fields, in
|
||||
* which case the `metadata` parameter is omitted from the tools entirely.
|
||||
*/
|
||||
async function buildProjectMetadataSchema(projectId?: string): Promise<TSchema | undefined> {
|
||||
if (!projectId) return undefined
|
||||
const fields = await metadataService.listProjectFields('', projectId)
|
||||
const creationFields: AutofillField[] = []
|
||||
for (const f of fields) {
|
||||
const config = f.field.config
|
||||
if (!config || config.autofillSource !== 'CREATION_CONTEXT') continue
|
||||
creationFields.push({
|
||||
id: f.field.key,
|
||||
config,
|
||||
description: f.field.description,
|
||||
})
|
||||
}
|
||||
if (creationFields.length === 0) return undefined
|
||||
return fieldsToTypeBoxSchema(creationFields)
|
||||
}
|
||||
|
||||
function escapeXml(str: string): string {
|
||||
return str
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''')
|
||||
}
|
||||
|
||||
export function formatSkillsForPrompt(
|
||||
skills: Array<{ id: string; name: string; description?: string | null }>,
|
||||
): string {
|
||||
if (skills.length === 0) {
|
||||
return ''
|
||||
}
|
||||
|
||||
const lines = [
|
||||
'\n\nThe following skills provide specialized instructions for specific tasks.',
|
||||
"Use the read_skill tool to load a skill's file when the task matches its description.",
|
||||
'When a skill file references a relative path, resolve it against the skill directory (parent of SKILL.md / dirname of the path) and use that relative path in tool commands.',
|
||||
'',
|
||||
'<available_skills>',
|
||||
]
|
||||
|
||||
for (const skill of skills) {
|
||||
const filePath = path.posix.join('.pi', 'skills', skill.id, 'SKILL.md')
|
||||
lines.push(' <skill>')
|
||||
lines.push(` <id>${escapeXml(skill.id)}</id>`)
|
||||
lines.push(` <name>${escapeXml(skill.name)}</name>`)
|
||||
lines.push(` <description>${escapeXml(skill.description || 'No description')}</description>`)
|
||||
lines.push(` <location>${escapeXml(filePath)}</location>`)
|
||||
lines.push(' </skill>')
|
||||
}
|
||||
|
||||
lines.push('</available_skills>')
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { fauxAssistantMessage, type AssistantMessage } from '@earendil-works/pi-ai'
|
||||
import {
|
||||
MemoryStorage,
|
||||
ROOT_CONVERSATION_ID,
|
||||
type Conversation,
|
||||
type Harness,
|
||||
type Tx,
|
||||
} from '@earendil-works/pi-durable'
|
||||
import { vi } from 'vitest'
|
||||
import type { createAgentSession } from '../runtime'
|
||||
|
||||
/** Native conversation fixture for activity orchestration tests; model behavior remains independently scripted. */
|
||||
export function mockAgentSession(
|
||||
model: { prompt: (...args: unknown[]) => Promise<unknown>; abort?: () => void },
|
||||
metadata: { getStorage?: () => unknown } = {},
|
||||
): Awaited<ReturnType<typeof createAgentSession>> {
|
||||
let answer = fauxAssistantMessage('')
|
||||
const information = metadata.getStorage?.() as { sessionId?: string } | undefined
|
||||
const entry = () => ({
|
||||
id: 9,
|
||||
conversationId: ROOT_CONVERSATION_ID,
|
||||
kind: 'pi.assistant',
|
||||
model: [answer],
|
||||
})
|
||||
const session = {
|
||||
id: ROOT_CONVERSATION_ID,
|
||||
submit: vi.fn(async (draft: { content: string | Array<{ type: string; text?: string }> }) => {
|
||||
const text =
|
||||
typeof draft.content === 'string'
|
||||
? draft.content
|
||||
: draft.content
|
||||
.filter((block) => block.type === 'text')
|
||||
.map((block) => block.text)
|
||||
.join('\n')
|
||||
const response = (await model.prompt(text, {
|
||||
images:
|
||||
typeof draft.content === 'string'
|
||||
? []
|
||||
: draft.content.filter((block) => block.type === 'image'),
|
||||
})) as Partial<AssistantMessage>
|
||||
answer = {
|
||||
...fauxAssistantMessage(''),
|
||||
...response,
|
||||
usage: { ...fauxAssistantMessage('').usage, ...response.usage },
|
||||
}
|
||||
return { wait: async () => ({ type: 'input', status: 'done', entry: 7, answer: 9 }) }
|
||||
}),
|
||||
abort: vi.fn(async () => {
|
||||
model.abort?.()
|
||||
}),
|
||||
commit: vi.fn(async (change: (tx: Tx) => unknown) =>
|
||||
change({ entry: async () => entry() } as unknown as Tx),
|
||||
),
|
||||
} as unknown as Conversation
|
||||
const harness = {
|
||||
root: vi.fn(async () => session),
|
||||
close: vi.fn(async () => {}),
|
||||
usage: vi.fn(async () => ({ models: { faux: answer.usage }, tools: {} })),
|
||||
} as unknown as Harness
|
||||
return {
|
||||
session,
|
||||
harness,
|
||||
storage: new MemoryStorage(),
|
||||
sessionId: information?.sessionId ?? 'mock-session-id',
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,12 @@
|
||||
import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { vi } from 'vitest'
|
||||
|
||||
vi.mock('fs')
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import { createGenerateImageTool } from './generate-image'
|
||||
import * as downloadAsset from './download-asset'
|
||||
import { mediaGenerationService } from '@shumai/core/src/media-generation/media-generation'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { EnabledMediaModel } from '@shumai/dtos'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
import * as downloadAsset from './download-asset'
|
||||
import { createGenerateImageTool } from './generate-image'
|
||||
|
||||
describe('generate_image tool', () => {
|
||||
const enabledModels: EnabledMediaModel[] = [
|
||||
@@ -44,13 +42,9 @@ describe('generate_image tool', () => {
|
||||
google: { apiKey: 'mock-google-key' },
|
||||
}
|
||||
|
||||
const piDir = path.join(process.cwd(), '.pi')
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
vi.spyOn(fs, 'mkdirSync').mockImplementation(() => undefined)
|
||||
vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
|
||||
vi.spyOn(s3Service, 'putObject').mockResolvedValue(undefined)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
@@ -60,7 +54,6 @@ describe('generate_image tool', () => {
|
||||
it('creates tool with correct metadata and provider-prefixed model schema', () => {
|
||||
const tool = createGenerateImageTool(enabledModels, providerKeys, 'user-1')
|
||||
expect(tool.name).toBe('generate_image')
|
||||
expect(tool.label).toBe('Generate Image')
|
||||
expect(tool.description).toContain('Generate an image')
|
||||
const modelDesc = (tool.parameters as { properties: { model: { description: string } } })
|
||||
.properties.model.description
|
||||
@@ -68,7 +61,7 @@ describe('generate_image tool', () => {
|
||||
expect(modelDesc).toContain('azure:dall-e-3')
|
||||
})
|
||||
|
||||
it('generates an image successfully and writes to .pi directory', async () => {
|
||||
it('generates an image successfully and saves to temporary storage with .pi/generated path', async () => {
|
||||
const tool = createGenerateImageTool(enabledModels, providerKeys, 'user-1')
|
||||
|
||||
const mockBuffer = Buffer.from('fake-image-png-content')
|
||||
@@ -76,9 +69,10 @@ describe('generate_image tool', () => {
|
||||
buffer: mockBuffer,
|
||||
mimeType: 'image/png',
|
||||
})
|
||||
const writeSpy = vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
|
||||
const putSpy = vi.spyOn(s3Service, 'putObject').mockResolvedValue(undefined)
|
||||
|
||||
const result = await tool.execute(
|
||||
const result = await invokeTool(
|
||||
tool,
|
||||
'call-1',
|
||||
{
|
||||
prompt: 'A futuristic city skyline at dusk',
|
||||
@@ -104,14 +98,19 @@ describe('generate_image tool', () => {
|
||||
}),
|
||||
)
|
||||
|
||||
const expectedPath = path.join(piDir, 'test_city.png')
|
||||
expect(writeSpy).toHaveBeenCalledWith(expectedPath, mockBuffer)
|
||||
expect(putSpy).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
expect.stringMatching(/^temp\/generated\/.+\/test_city\.png$/),
|
||||
mockBuffer,
|
||||
mockBuffer.length,
|
||||
'image/png',
|
||||
)
|
||||
|
||||
const textContent = result.content[0]
|
||||
expect(textContent.type).toBe('text')
|
||||
if (textContent.type === 'text') {
|
||||
expect(textContent.text).toContain('Successfully generated image')
|
||||
expect(textContent.text).toContain('.pi/test_city.png')
|
||||
expect(textContent.text).toMatch(/\.pi\/generated\/.+\/test_city\.png/)
|
||||
}
|
||||
expect(result.details).toMatchObject({
|
||||
fileName: 'test_city.png',
|
||||
@@ -129,7 +128,8 @@ describe('generate_image tool', () => {
|
||||
})
|
||||
|
||||
// Invoke azure:dall-e-3
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-azure',
|
||||
{
|
||||
prompt: 'Azure generation',
|
||||
@@ -153,7 +153,8 @@ describe('generate_image tool', () => {
|
||||
)
|
||||
|
||||
// Invoke openai:dall-e-3
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-openai',
|
||||
{
|
||||
prompt: 'OpenAI generation',
|
||||
@@ -186,7 +187,8 @@ describe('generate_image tool', () => {
|
||||
mimeType: 'image/png',
|
||||
})
|
||||
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-2',
|
||||
{
|
||||
prompt: 'A cute cat',
|
||||
@@ -224,7 +226,8 @@ describe('generate_image tool', () => {
|
||||
mimeType: 'image/png',
|
||||
}))
|
||||
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-3',
|
||||
{
|
||||
prompt: 'Transform into watercolor',
|
||||
@@ -257,7 +260,8 @@ describe('generate_image tool', () => {
|
||||
const tool = createGenerateImageTool(enabledModels, {}, 'user-1') // No keys configured
|
||||
|
||||
await expect(
|
||||
tool.execute(
|
||||
invokeTool(
|
||||
tool,
|
||||
'call-4',
|
||||
{
|
||||
prompt: 'A sunset',
|
||||
@@ -278,7 +282,8 @@ describe('generate_image tool', () => {
|
||||
const tool = createGenerateImageTool(enabledModels, providerKeys, 'user-1')
|
||||
|
||||
await expect(
|
||||
tool.execute(
|
||||
invokeTool(
|
||||
tool,
|
||||
'call-5',
|
||||
{
|
||||
prompt: 'A sunset',
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { EnabledMediaModel } from '@shumai/dtos'
|
||||
import { type ToolRegistration as AgentTool } from '@earendil-works/pi-durable'
|
||||
import { storageBucket } from '@shumai/core/src/asset-read'
|
||||
import { mediaGenerationService } from '@shumai/core/src/media-generation/media-generation'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
||||
import { EnabledMediaModel } from '@shumai/dtos'
|
||||
import { Type, type Static, type TSchema } from 'typebox'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { ulid } from 'ulid'
|
||||
import { resolveS3MediaBuffer } from './download-asset'
|
||||
|
||||
@@ -83,11 +83,11 @@ export function createGenerateImageTool(
|
||||
|
||||
return {
|
||||
name: 'generate_image',
|
||||
label: 'Generate Image',
|
||||
|
||||
description:
|
||||
'Generate an image using a configured image model. Supports text-to-image, reference images (image-to-image using S3 keys from read_asset), and inpainting (using a mask image S3 key from read_asset). The generated file is saved in the local .pi/ directory.',
|
||||
parameters: generateImageSchema,
|
||||
execute: async (_toolCallId, params) => {
|
||||
execute: async (params) => {
|
||||
const p = params as GenerateImageParams
|
||||
|
||||
const defaultModel = `${enabledModels[0].provider}:${enabledModels[0].modelId}`
|
||||
@@ -138,30 +138,26 @@ export function createGenerateImageTool(
|
||||
seed: p.seed !== null && p.seed !== undefined ? p.seed : undefined,
|
||||
})
|
||||
|
||||
const piDir = path.join(process.cwd(), '.pi')
|
||||
if (!fs.existsSync(piDir)) {
|
||||
fs.mkdirSync(piDir, { recursive: true })
|
||||
}
|
||||
|
||||
const safeMime = mimeType || 'image/png'
|
||||
let ext = '.png'
|
||||
if (safeMime.includes('jpeg') || safeMime.includes('jpg')) ext = '.jpg'
|
||||
else if (safeMime.includes('webp')) ext = '.webp'
|
||||
|
||||
const genId = ulid()
|
||||
let filename: string
|
||||
if (p.outputFileName) {
|
||||
const safe = sanitizeFilename(p.outputFileName)
|
||||
filename = safe.includes('.') ? safe : `${safe}${ext}`
|
||||
} else {
|
||||
filename = `generated_image_${ulid()}${ext}`
|
||||
filename = `generated_image_${genId}${ext}`
|
||||
}
|
||||
|
||||
const targetPath = path.join(piDir, filename)
|
||||
const relativePath = path.join('.pi', filename)
|
||||
fs.writeFileSync(targetPath, buffer)
|
||||
const storageKey = `temp/generated/${genId}/${filename}`
|
||||
const relativePath = `.pi/generated/${genId}/${filename}`
|
||||
await s3Service.putObject(storageBucket(), storageKey, buffer, buffer.length, safeMime)
|
||||
|
||||
return {
|
||||
content: [
|
||||
output: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Successfully generated image using model "${modelConfig.modelId}" (${modelConfig.provider}). Saved to "${relativePath}". Size: ${(buffer.length / 1024).toFixed(1)} KB.`,
|
||||
@@ -170,7 +166,7 @@ export function createGenerateImageTool(
|
||||
details: {
|
||||
fileName: filename,
|
||||
filePath: relativePath,
|
||||
absolutePath: targetPath,
|
||||
storageKey,
|
||||
size: buffer.length,
|
||||
mimeType,
|
||||
provider: modelConfig.provider,
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { vi } from 'vitest'
|
||||
|
||||
vi.mock('fs')
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import { createGenerateVideoTool } from './generate-video'
|
||||
import * as downloadAsset from './download-asset'
|
||||
import { mediaGenerationService } from '@shumai/core/src/media-generation/media-generation'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { EnabledMediaModel } from '@shumai/dtos'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
import * as downloadAsset from './download-asset'
|
||||
import { createGenerateVideoTool } from './generate-video'
|
||||
|
||||
describe('generate_video tool', () => {
|
||||
const enabledModels: EnabledMediaModel[] = [
|
||||
@@ -44,13 +42,9 @@ describe('generate_video tool', () => {
|
||||
google: { apiKey: 'mock-google-key' },
|
||||
}
|
||||
|
||||
const piDir = path.join(process.cwd(), '.pi')
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
vi.spyOn(fs, 'mkdirSync').mockImplementation(() => undefined)
|
||||
vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
|
||||
vi.spyOn(s3Service, 'putObject').mockResolvedValue(undefined)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
@@ -60,7 +54,6 @@ describe('generate_video tool', () => {
|
||||
it('creates tool with correct metadata and provider-prefixed model schema', () => {
|
||||
const tool = createGenerateVideoTool(enabledModels, providerKeys, 'user-1')
|
||||
expect(tool.name).toBe('generate_video')
|
||||
expect(tool.label).toBe('Generate Video')
|
||||
expect(tool.description).toContain('Generate a video')
|
||||
const modelDesc = (tool.parameters as { properties: { model: { description: string } } })
|
||||
.properties.model.description
|
||||
@@ -76,9 +69,10 @@ describe('generate_video tool', () => {
|
||||
buffer: mockBuffer,
|
||||
mimeType: 'video/mp4',
|
||||
})
|
||||
const writeSpy = vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
|
||||
const putSpy = vi.spyOn(s3Service, 'putObject').mockResolvedValue(undefined)
|
||||
|
||||
const result = await tool.execute(
|
||||
const result = await invokeTool(
|
||||
tool,
|
||||
'call-vid-1',
|
||||
{
|
||||
mode: 'text_to_video',
|
||||
@@ -114,14 +108,19 @@ describe('generate_video tool', () => {
|
||||
}),
|
||||
)
|
||||
|
||||
const expectedPath = path.join(piDir, 'test_nature.mp4')
|
||||
expect(writeSpy).toHaveBeenCalledWith(expectedPath, mockBuffer)
|
||||
expect(putSpy).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
expect.stringMatching(/^temp\/generated\/.+\/test_nature\.mp4$/),
|
||||
mockBuffer,
|
||||
mockBuffer.length,
|
||||
'video/mp4',
|
||||
)
|
||||
|
||||
const textContent = result.content[0]
|
||||
expect(textContent.type).toBe('text')
|
||||
if (textContent.type === 'text') {
|
||||
expect(textContent.text).toContain('Successfully generated video')
|
||||
expect(textContent.text).toContain('.pi/test_nature.mp4')
|
||||
expect(textContent.text).toMatch(/\.pi\/generated\/.+\/test_nature\.mp4/)
|
||||
}
|
||||
expect(result.details).toMatchObject({
|
||||
fileName: 'test_nature.mp4',
|
||||
@@ -140,7 +139,8 @@ describe('generate_video tool', () => {
|
||||
})
|
||||
|
||||
// Select fal:kling-v1-standard
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-fal',
|
||||
{
|
||||
mode: 'text_to_video',
|
||||
@@ -169,7 +169,8 @@ describe('generate_video tool', () => {
|
||||
)
|
||||
|
||||
// Select klingai:kling-v1-standard
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-klingai',
|
||||
{
|
||||
mode: 'text_to_video',
|
||||
@@ -211,7 +212,8 @@ describe('generate_video tool', () => {
|
||||
mimeType: 'image/png',
|
||||
})
|
||||
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-vid-2',
|
||||
{
|
||||
mode: 'image_to_video',
|
||||
@@ -257,7 +259,8 @@ describe('generate_video tool', () => {
|
||||
mimeType: 'image/png',
|
||||
}))
|
||||
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-vid-3',
|
||||
{
|
||||
mode: 'first_last_frame',
|
||||
@@ -304,7 +307,8 @@ describe('generate_video tool', () => {
|
||||
mimeType: 'image/png',
|
||||
}))
|
||||
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-vid-4',
|
||||
{
|
||||
mode: 'reference_to_video',
|
||||
@@ -343,7 +347,8 @@ describe('generate_video tool', () => {
|
||||
const tool = createGenerateVideoTool(enabledModels, {}, 'user-1')
|
||||
|
||||
await expect(
|
||||
tool.execute(
|
||||
invokeTool(
|
||||
tool,
|
||||
'call-vid-5',
|
||||
{
|
||||
mode: 'text_to_video',
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { EnabledMediaModel } from '@shumai/dtos'
|
||||
import { type ToolRegistration as AgentTool } from '@earendil-works/pi-durable'
|
||||
import { storageBucket } from '@shumai/core/src/asset-read'
|
||||
import { mediaGenerationService } from '@shumai/core/src/media-generation/media-generation'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
||||
import { EnabledMediaModel } from '@shumai/dtos'
|
||||
import { Type, type Static, type TSchema } from 'typebox'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { ulid } from 'ulid'
|
||||
import { resolveS3MediaBuffer } from './download-asset'
|
||||
|
||||
@@ -162,11 +162,11 @@ export function createGenerateVideoTool(
|
||||
|
||||
return {
|
||||
name: 'generate_video',
|
||||
label: 'Generate Video',
|
||||
|
||||
description:
|
||||
'Generate a video using a configured video model. Supports text-to-video, image-to-video (using an S3 image key from read_asset), first/last frame (using S3 image keys from read_asset), and reference-to-video (using S3 image keys from read_asset). The generated file is saved in the local .pi/ directory.',
|
||||
parameters: generateVideoSchema,
|
||||
execute: async (_toolCallId, params) => {
|
||||
execute: async (params) => {
|
||||
const p = params as GenerateVideoParams
|
||||
|
||||
const defaultModel = `${enabledModels[0].provider}:${enabledModels[0].modelId}`
|
||||
@@ -262,25 +262,22 @@ export function createGenerateVideoTool(
|
||||
seed: p.seed !== null && p.seed !== undefined ? p.seed : undefined,
|
||||
})
|
||||
|
||||
const piDir = path.join(process.cwd(), '.pi')
|
||||
if (!fs.existsSync(piDir)) {
|
||||
fs.mkdirSync(piDir, { recursive: true })
|
||||
}
|
||||
|
||||
const genId = ulid()
|
||||
let filename: string
|
||||
if (p.outputFileName) {
|
||||
const safe = sanitizeFilename(p.outputFileName)
|
||||
filename = safe.endsWith('.mp4') ? safe : `${safe}.mp4`
|
||||
} else {
|
||||
filename = `generated_video_${ulid()}.mp4`
|
||||
filename = `generated_video_${genId}.mp4`
|
||||
}
|
||||
|
||||
const targetPath = path.join(piDir, filename)
|
||||
const relativePath = path.join('.pi', filename)
|
||||
fs.writeFileSync(targetPath, buffer)
|
||||
const safeMime = mimeType || 'video/mp4'
|
||||
const storageKey = `temp/generated/${genId}/${filename}`
|
||||
const relativePath = `.pi/generated/${genId}/${filename}`
|
||||
await s3Service.putObject(storageBucket(), storageKey, buffer, buffer.length, safeMime)
|
||||
|
||||
return {
|
||||
content: [
|
||||
output: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Successfully generated video using model "${modelConfig.modelId}" (${modelConfig.provider}) in mode "${p.mode}". Saved to "${relativePath}". Size: ${(buffer.length / (1024 * 1024)).toFixed(2)} MB.`,
|
||||
@@ -289,7 +286,7 @@ export function createGenerateVideoTool(
|
||||
details: {
|
||||
fileName: filename,
|
||||
filePath: relativePath,
|
||||
absolutePath: targetPath,
|
||||
storageKey,
|
||||
size: buffer.length,
|
||||
mimeType,
|
||||
provider: modelConfig.provider,
|
||||
|
||||
@@ -1,31 +1,50 @@
|
||||
import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { vi } from 'vitest'
|
||||
|
||||
vi.mock('@shumai/core/src/s3/s3')
|
||||
vi.mock('fs')
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { createReadSkillTool } from './read-skill'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import AdmZip from 'adm-zip'
|
||||
import * as fs from 'fs'
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
import { createReadSkillTool } from './read-skill'
|
||||
|
||||
function makeSkillZipBuffer(skillMdContent: string): Buffer {
|
||||
const zip = new AdmZip()
|
||||
zip.addFile('SKILL.md', Buffer.from(skillMdContent, 'utf-8'))
|
||||
return zip.toBuffer()
|
||||
}
|
||||
|
||||
describe('readSkillTool', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks()
|
||||
await prisma.asset.create({
|
||||
data: {
|
||||
id: 'asset1',
|
||||
name: 'skill.zip',
|
||||
storageKey: { create: { key: 'skills/skill.zip' } },
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking s3Service.getObject return value
|
||||
;(s3Service.getObject as any).mockResolvedValue({
|
||||
buffer: makeSkillZipBuffer('# Content'),
|
||||
contentType: 'application/zip',
|
||||
})
|
||||
})
|
||||
|
||||
it('should throw error if skill not found', async () => {
|
||||
const readSkillTool = createReadSkillTool(undefined, () => {})
|
||||
await expect(
|
||||
readSkillTool.execute('1', { skillId: 'non-existent' }, undefined, undefined),
|
||||
invokeTool(readSkillTool, '1', { skillId: 'non-existent' }, undefined, undefined),
|
||||
).rejects.toThrow('not found')
|
||||
})
|
||||
|
||||
it('should read skill from cache if hash matches', async () => {
|
||||
it('should read SKILL.md directly from the skill zip in storage', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const skill = await prisma.skill.create({
|
||||
data: {
|
||||
@@ -36,23 +55,21 @@ describe('readSkillTool', () => {
|
||||
},
|
||||
})
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs calls
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'match-hash'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Test Skill Content'
|
||||
return ''
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking s3Service return
|
||||
;(s3Service.getObject as any).mockResolvedValue({
|
||||
buffer: makeSkillZipBuffer('# Test Skill Content'),
|
||||
contentType: 'application/zip',
|
||||
})
|
||||
|
||||
const readSkillTool = createReadSkillTool(undefined, () => {})
|
||||
const result = await readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined)
|
||||
const result = await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((result.content[0] as any).text).toBe('# Test Skill Content')
|
||||
expect(s3Service.getObject).not.toHaveBeenCalled()
|
||||
expect(s3Service.getObject).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should download and extract skill if cache missing or hash mismatch', async () => {
|
||||
it('should read nested SKILL.md inside zip archive', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const skill = await prisma.skill.create({
|
||||
data: {
|
||||
@@ -63,31 +80,8 @@ describe('readSkillTool', () => {
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.asset.create({
|
||||
data: {
|
||||
id: 'asset1',
|
||||
name: 'skill.zip',
|
||||
storageKey: { create: { key: 'skills/skill.zip' } },
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs calls
|
||||
vi.spyOn(fs, 'existsSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return false
|
||||
if (path.toString().endsWith('SKILL.md')) return true
|
||||
return true
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs calls
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Extracted Content'
|
||||
return ''
|
||||
})
|
||||
|
||||
const zip = new AdmZip()
|
||||
zip.addFile('SKILL.md', Buffer.from('# Extracted Content'))
|
||||
zip.addFile('nested/SKILL.md', Buffer.from('# Extracted Content'))
|
||||
const zipBuffer = zip.toBuffer()
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking s3Service return
|
||||
@@ -97,12 +91,11 @@ describe('readSkillTool', () => {
|
||||
})
|
||||
|
||||
const readSkillTool = createReadSkillTool(undefined, () => {})
|
||||
const result = await readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined)
|
||||
const result = await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((result.content[0] as any).text).toBe('# Extracted Content')
|
||||
expect(s3Service.getObject).toHaveBeenCalled()
|
||||
expect(fs.writeFileSync).toHaveBeenCalledWith(expect.stringContaining('.hash'), 'new-hash')
|
||||
})
|
||||
|
||||
describe('environment variables', () => {
|
||||
@@ -132,19 +125,11 @@ describe('readSkillTool', () => {
|
||||
|
||||
delete process.env.CONFIGURED_VAR
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'hash1'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Content'
|
||||
return ''
|
||||
})
|
||||
|
||||
let capturedEnvs: Record<string, string> = {}
|
||||
const readSkillTool = createReadSkillTool(undefined, (envs) => {
|
||||
capturedEnvs = { ...capturedEnvs, ...envs }
|
||||
})
|
||||
await readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined)
|
||||
await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
|
||||
|
||||
expect(capturedEnvs).toEqual({
|
||||
CONFIGURED_VAR: 'user-val',
|
||||
@@ -167,19 +152,11 @@ describe('readSkillTool', () => {
|
||||
|
||||
process.env.OVERRIDDEN_VAR = 'host-val'
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'hash2'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Content'
|
||||
return ''
|
||||
})
|
||||
|
||||
let capturedEnvs: Record<string, string> = {}
|
||||
const readSkillTool = createReadSkillTool(undefined, (envs) => {
|
||||
capturedEnvs = { ...capturedEnvs, ...envs }
|
||||
})
|
||||
await readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined)
|
||||
await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
|
||||
|
||||
expect(capturedEnvs).toEqual({
|
||||
OVERRIDDEN_VAR: 'user-val',
|
||||
@@ -202,19 +179,11 @@ describe('readSkillTool', () => {
|
||||
|
||||
process.env.FALLBACK_VAR = 'host-val'
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'hash3'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Content'
|
||||
return ''
|
||||
})
|
||||
|
||||
let capturedEnvs: Record<string, string> = {}
|
||||
const readSkillTool = createReadSkillTool(undefined, (envs) => {
|
||||
capturedEnvs = { ...capturedEnvs, ...envs }
|
||||
})
|
||||
await readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined)
|
||||
await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
|
||||
|
||||
expect(capturedEnvs).toEqual({
|
||||
FALLBACK_VAR: 'host-val',
|
||||
@@ -237,19 +206,11 @@ describe('readSkillTool', () => {
|
||||
|
||||
process.env.FALLBACK_VAR_UNDEF = 'host-val2'
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'hash4'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Content'
|
||||
return ''
|
||||
})
|
||||
|
||||
let capturedEnvs: Record<string, string> = {}
|
||||
const readSkillTool = createReadSkillTool(undefined, (envs) => {
|
||||
capturedEnvs = { ...capturedEnvs, ...envs }
|
||||
})
|
||||
await readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined)
|
||||
await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
|
||||
|
||||
expect(capturedEnvs).toEqual({
|
||||
FALLBACK_VAR_UNDEF: 'host-val2',
|
||||
@@ -284,16 +245,15 @@ describe('readSkillTool', () => {
|
||||
},
|
||||
})
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'hash-owner'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Owner Skill'
|
||||
return ''
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking s3Service return
|
||||
;(s3Service.getObject as any).mockResolvedValue({
|
||||
buffer: makeSkillZipBuffer('# Owner Skill'),
|
||||
contentType: 'application/zip',
|
||||
})
|
||||
|
||||
const ownerTool = createReadSkillTool(ownerUser.id, () => {})
|
||||
const resultOwner = await ownerTool.execute(
|
||||
const resultOwner = await invokeTool(
|
||||
ownerTool,
|
||||
'1',
|
||||
{ skillId: ownerSkill.id },
|
||||
undefined,
|
||||
@@ -304,7 +264,7 @@ describe('readSkillTool', () => {
|
||||
|
||||
const reviewerTool = createReadSkillTool(reviewerUser.id, () => {})
|
||||
await expect(
|
||||
reviewerTool.execute('1', { skillId: ownerSkill.id }, undefined, undefined),
|
||||
invokeTool(reviewerTool, '1', { skillId: ownerSkill.id }, undefined, undefined),
|
||||
).rejects.toThrow('Permission denied')
|
||||
})
|
||||
|
||||
@@ -340,7 +300,7 @@ describe('readSkillTool', () => {
|
||||
project.id,
|
||||
)
|
||||
await expect(
|
||||
demotedTool.execute('1', { skillId: editorSkill.id }, undefined, undefined),
|
||||
invokeTool(demotedTool, '1', { skillId: editorSkill.id }, undefined, undefined),
|
||||
).rejects.toThrow('Permission denied')
|
||||
|
||||
// Project-scoped member invited as editor: allowed
|
||||
@@ -354,12 +314,10 @@ describe('readSkillTool', () => {
|
||||
data: { projectId: project.id, teamMemberId: projEditorTm.id, role: 'editor' },
|
||||
})
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs calls
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'proj-editor-hash'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Editor Project Skill Content'
|
||||
return ''
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking s3Service return
|
||||
;(s3Service.getObject as any).mockResolvedValue({
|
||||
buffer: makeSkillZipBuffer('# Editor Project Skill Content'),
|
||||
contentType: 'application/zip',
|
||||
})
|
||||
|
||||
const projEditorTool = createReadSkillTool(
|
||||
@@ -369,7 +327,8 @@ describe('readSkillTool', () => {
|
||||
undefined,
|
||||
project.id,
|
||||
)
|
||||
const result = await projEditorTool.execute(
|
||||
const result = await invokeTool(
|
||||
projEditorTool,
|
||||
'1',
|
||||
{ skillId: editorSkill.id },
|
||||
undefined,
|
||||
@@ -392,19 +351,16 @@ describe('readSkillTool', () => {
|
||||
},
|
||||
})
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'cb-hash'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Callback Skill'
|
||||
return ''
|
||||
})
|
||||
|
||||
const onSkillLoaded = vi.fn()
|
||||
const readSkillTool = createReadSkillTool(undefined, () => {}, onSkillLoaded)
|
||||
await readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined)
|
||||
await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
|
||||
|
||||
expect(onSkillLoaded).toHaveBeenCalledTimes(1)
|
||||
expect(onSkillLoaded).toHaveBeenCalledWith({
|
||||
id: skill.id,
|
||||
assetId: 'asset1',
|
||||
hash: 'cb-hash',
|
||||
})
|
||||
})
|
||||
|
||||
it('should not invoke onSkillLoaded when loading is denied', async () => {
|
||||
@@ -428,7 +384,7 @@ describe('readSkillTool', () => {
|
||||
const onSkillLoaded = vi.fn()
|
||||
const reviewerTool = createReadSkillTool(reviewerUser.id, () => {}, onSkillLoaded)
|
||||
await expect(
|
||||
reviewerTool.execute('1', { skillId: ownerSkill.id }, undefined, undefined),
|
||||
invokeTool(reviewerTool, '1', { skillId: ownerSkill.id }, undefined, undefined),
|
||||
).rejects.toThrow('Permission denied')
|
||||
|
||||
expect(onSkillLoaded).not.toHaveBeenCalled()
|
||||
@@ -451,7 +407,7 @@ describe('readSkillTool', () => {
|
||||
'some-other-enabled-skill',
|
||||
])
|
||||
await expect(
|
||||
readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined),
|
||||
invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined),
|
||||
).rejects.toThrow('not enabled for this agent')
|
||||
})
|
||||
|
||||
@@ -466,16 +422,20 @@ describe('readSkillTool', () => {
|
||||
},
|
||||
})
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'enabled-agent-hash'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Enabled For Agent Skill'
|
||||
return ''
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking s3Service return
|
||||
;(s3Service.getObject as any).mockResolvedValue({
|
||||
buffer: makeSkillZipBuffer('# Enabled For Agent Skill'),
|
||||
contentType: 'application/zip',
|
||||
})
|
||||
|
||||
const readSkillTool = createReadSkillTool(undefined, () => {}, undefined, [skill.id])
|
||||
const result = await readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined)
|
||||
const result = await invokeTool(
|
||||
readSkillTool,
|
||||
'1',
|
||||
{ skillId: skill.id },
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((result.content[0] as any).text).toBe('# Enabled For Agent Skill')
|
||||
})
|
||||
@@ -491,16 +451,20 @@ describe('readSkillTool', () => {
|
||||
},
|
||||
})
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'no-filter-hash'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# No Filter Skill'
|
||||
return ''
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking s3Service return
|
||||
;(s3Service.getObject as any).mockResolvedValue({
|
||||
buffer: makeSkillZipBuffer('# No Filter Skill'),
|
||||
contentType: 'application/zip',
|
||||
})
|
||||
|
||||
const readSkillTool = createReadSkillTool(undefined, () => {})
|
||||
const result = await readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined)
|
||||
const result = await invokeTool(
|
||||
readSkillTool,
|
||||
'1',
|
||||
{ skillId: skill.id },
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((result.content[0] as any).text).toBe('# No Filter Skill')
|
||||
})
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import { Type } from 'typebox'
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { type ToolRegistration as AgentTool } from '@earendil-works/pi-durable'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { Type } from 'typebox'
|
||||
|
||||
const readSkillSchema = Type.Object({
|
||||
skillId: Type.String({ description: 'The ID of the skill to read' }),
|
||||
@@ -17,15 +17,15 @@ const ROLE_HIERARCHY: Record<string, number> = {
|
||||
export const createReadSkillTool = (
|
||||
userId: string | undefined,
|
||||
onEnvsAdded: (envs: Record<string, string>) => void,
|
||||
onSkillLoaded?: () => Promise<void> | void,
|
||||
onSkillLoaded?: (skill: { id: string; assetId: string; hash: string }) => Promise<void> | void,
|
||||
enabledSkillIds?: string[],
|
||||
projectId?: string,
|
||||
): AgentTool<typeof readSkillSchema, { skillId: string }> => ({
|
||||
name: 'read_skill',
|
||||
label: 'Read Agent Skill',
|
||||
|
||||
description: 'Downloads and reads an agent skill to understand how to use it.',
|
||||
parameters: readSkillSchema,
|
||||
execute: async (_toolCallId, params) => {
|
||||
execute: async (params) => {
|
||||
const skill = await prisma.skill.findUnique({
|
||||
where: { id: params.skillId },
|
||||
})
|
||||
@@ -67,11 +67,11 @@ export const createReadSkillTool = (
|
||||
// Retrieve skill content using agentService
|
||||
const skillMdContent = await agentService.getSkillContent(params.skillId)
|
||||
|
||||
// Notify that a skill was successfully loaded (e.g. to enable restricted tools)
|
||||
await onSkillLoaded?.()
|
||||
// Notify that a skill was successfully loaded (e.g. to stage the skill on the runner)
|
||||
await onSkillLoaded?.({ id: skill.id, assetId: skill.assetId, hash: skill.hash })
|
||||
|
||||
return {
|
||||
content: [{ type: 'text', text: skillMdContent }],
|
||||
output: [{ type: 'text', text: skillMdContent }],
|
||||
details: { skillId: params.skillId },
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { createReadThreadTool } from './read-thread'
|
||||
|
||||
/**
|
||||
@@ -45,7 +46,7 @@ describe('read_thread authorization', () => {
|
||||
const a = await seedTeamWithThread('a')
|
||||
|
||||
const tool = createReadThreadTool({ userId: a.author.id, teamId: a.team.id })
|
||||
const result = await tool.execute('call-1', { threadId: a.thread.id })
|
||||
const result = await invokeTool(tool, 'call-1', { threadId: a.thread.id })
|
||||
const text = (result.content[0] as { text: string }).text
|
||||
|
||||
expect(text).toContain('secret message a')
|
||||
@@ -58,7 +59,7 @@ describe('read_thread authorization', () => {
|
||||
|
||||
const tool = createReadThreadTool({ userId: a.author.id, teamId: a.team.id })
|
||||
|
||||
await expect(tool.execute('call-1', { threadId: b.thread.id })).rejects.toThrow(
|
||||
await expect(invokeTool(tool, 'call-1', { threadId: b.thread.id })).rejects.toThrow(
|
||||
'User is not a member of the team',
|
||||
)
|
||||
})
|
||||
@@ -74,7 +75,7 @@ describe('read_thread authorization', () => {
|
||||
|
||||
const tool = createReadThreadTool({ userId: outsider.id, teamId: b.team.id })
|
||||
|
||||
await expect(tool.execute('call-1', { threadId: b.thread.id })).rejects.toThrow()
|
||||
await expect(invokeTool(tool, 'call-1', { threadId: b.thread.id })).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('confines a system agent (no user) to threads in its own team', async () => {
|
||||
@@ -83,9 +84,11 @@ describe('read_thread authorization', () => {
|
||||
|
||||
const tool = createReadThreadTool({ teamId: a.team.id })
|
||||
|
||||
const own = await tool.execute('call-1', { threadId: a.thread.id })
|
||||
const own = await invokeTool(tool, 'call-1', { threadId: a.thread.id })
|
||||
expect((own.content[0] as { text: string }).text).toContain('secret message a')
|
||||
|
||||
await expect(tool.execute('call-2', { threadId: b.thread.id })).rejects.toThrow('Access denied')
|
||||
await expect(invokeTool(tool, 'call-2', { threadId: b.thread.id })).rejects.toThrow(
|
||||
'Access denied',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { createReadThreadTool } from './read-thread'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { authzService } from '@shumai/core/src/authz/authz'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createReadThreadTool } from './read-thread'
|
||||
|
||||
vi.mock('@shumai/core/src/authz/authz', () => ({
|
||||
authzService: {
|
||||
@@ -37,7 +38,7 @@ describe('createReadThreadTool', () => {
|
||||
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
|
||||
|
||||
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
|
||||
const result = await tool.execute('call-1', { threadId: 'non-existent' })
|
||||
const result = await invokeTool(tool, 'call-1', { threadId: 'non-existent' })
|
||||
const textContent = (result.content[0] as { text: string }).text
|
||||
|
||||
expect(textContent).toContain('Comment thread with ID "non-existent" not found.')
|
||||
@@ -49,7 +50,7 @@ describe('createReadThreadTool', () => {
|
||||
vi.mocked(prisma.asset.findUnique).mockResolvedValue({ id: 'asset-1' } as any)
|
||||
|
||||
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
|
||||
const result = await tool.execute('call-asset', { threadId: 'asset-1' })
|
||||
const result = await invokeTool(tool, 'call-asset', { threadId: 'asset-1' })
|
||||
const textContent = (result.content[0] as { text: string }).text
|
||||
|
||||
expect(textContent).toContain('ID "asset-1" is an Asset ID, not a Comment Thread ID')
|
||||
@@ -68,7 +69,7 @@ describe('createReadThreadTool', () => {
|
||||
vi.mocked(prisma.assetComment.findMany).mockResolvedValue([])
|
||||
|
||||
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
|
||||
const result = await tool.execute('call-2', { threadId: 'root-1' })
|
||||
const result = await invokeTool(tool, 'call-2', { threadId: 'root-1' })
|
||||
const textContent = (result.content[0] as { text: string }).text
|
||||
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
@@ -107,7 +108,7 @@ describe('createReadThreadTool', () => {
|
||||
] as any)
|
||||
|
||||
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
|
||||
const result = await tool.execute('call-3', { threadId: 'root-1' })
|
||||
const result = await invokeTool(tool, 'call-3', { threadId: 'root-1' })
|
||||
const textContent = (result.content[0] as { text: string }).text
|
||||
|
||||
expect(textContent).toContain(
|
||||
@@ -139,7 +140,7 @@ describe('createReadThreadTool', () => {
|
||||
] as any)
|
||||
|
||||
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
|
||||
const result = await tool.execute('call-agent', { threadId: 'root-1' })
|
||||
const result = await invokeTool(tool, 'call-agent', { threadId: 'root-1' })
|
||||
const textContent = (result.content[0] as { text: string }).text
|
||||
|
||||
expect(textContent).toContain('- [Ai Agent] (id: reply-1, has_markup: false): Agent answer')
|
||||
@@ -170,7 +171,7 @@ describe('createReadThreadTool', () => {
|
||||
] as any)
|
||||
|
||||
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
|
||||
const result = await tool.execute('call-3d', { threadId: 'root-3d' })
|
||||
const result = await invokeTool(tool, 'call-3d', { threadId: 'root-3d' })
|
||||
const textContent = (result.content[0] as { text: string }).text
|
||||
|
||||
expect(textContent).toContain(
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { prisma, type User } from '@shumai/db'
|
||||
import { Type } from 'typebox'
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { type ToolRegistration as AgentTool } from '@earendil-works/pi-durable'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { secondToDegree } from '@shumai/core/src/utils/model3d'
|
||||
import { getProxyType } from '@shumai/core/src/utils/mime'
|
||||
import { secondToDegree } from '@shumai/core/src/utils/model3d'
|
||||
import { prisma, type User } from '@shumai/db'
|
||||
import { Type } from 'typebox'
|
||||
|
||||
const readThreadSchema = Type.Object({
|
||||
threadId: Type.String({
|
||||
@@ -67,10 +67,10 @@ export const createReadThreadTool = (
|
||||
ctx: ReadThreadToolContext,
|
||||
): AgentTool<typeof readThreadSchema, { threadId: string }> => ({
|
||||
name: 'read_thread',
|
||||
label: 'Read Comment Thread',
|
||||
|
||||
description: 'Reads all comments and replies inside a specific comment thread.',
|
||||
parameters: readThreadSchema,
|
||||
execute: async (_toolCallId, params) => {
|
||||
execute: async (params) => {
|
||||
const rootComment = await prisma.assetComment.findUnique({
|
||||
where: { id: params.threadId },
|
||||
include: {
|
||||
@@ -89,7 +89,7 @@ export const createReadThreadTool = (
|
||||
: `Comment thread with ID "${params.threadId}" not found.`
|
||||
|
||||
return {
|
||||
content: [{ type: 'text', text: errorText }],
|
||||
output: [{ type: 'text', text: errorText }],
|
||||
details: { threadId: params.threadId },
|
||||
}
|
||||
}
|
||||
@@ -127,7 +127,7 @@ export const createReadThreadTool = (
|
||||
}
|
||||
|
||||
return {
|
||||
content: [{ type: 'text', text: output }],
|
||||
output: [{ type: 'text', text: output }],
|
||||
details: { threadId: params.threadId },
|
||||
}
|
||||
},
|
||||
|
||||
@@ -0,0 +1,311 @@
|
||||
import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { runnerService } from '@shumai/core/src/runner'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createRunnerBashTool } from './runner-bash'
|
||||
|
||||
vi.mock('@shumai/core/src/runner', () => ({
|
||||
runnerService: {
|
||||
requireOnlineRunner: vi.fn(),
|
||||
isRunnerConnected: vi.fn(),
|
||||
ensureSkillOnRunner: vi.fn(),
|
||||
ensureGeneratedFileOnRunner: vi.fn(),
|
||||
exec: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
describe('createRunnerBashTool', () => {
|
||||
const mockTeamId = 'team-1'
|
||||
const mockRunner = {
|
||||
id: 'runner-1',
|
||||
teamId: mockTeamId,
|
||||
name: 'Local Mac Mini',
|
||||
keyPrefix: 'shm_rn_12345678',
|
||||
isDefault: true,
|
||||
status: 'connected' as const,
|
||||
createdAt: '2026-11-14T00:00:00.000Z',
|
||||
updatedAt: '2026-11-14T00:00:00.000Z',
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
vi.mocked(runnerService.requireOnlineRunner).mockResolvedValue(mockRunner)
|
||||
vi.mocked(runnerService.isRunnerConnected).mockReturnValue(true)
|
||||
vi.mocked(runnerService.ensureSkillOnRunner).mockResolvedValue('.pi/skills/skill-1')
|
||||
vi.mocked(runnerService.ensureGeneratedFileOnRunner).mockResolvedValue(
|
||||
'.pi/generated/gen1/input.png',
|
||||
)
|
||||
})
|
||||
|
||||
const createMockSignal = () => new AbortController().signal
|
||||
|
||||
const createMockOnUpdate = () => vi.fn()
|
||||
|
||||
it.each(['C:\\Windows\\System32\\cmd.exe', 'C:\\Program Files\\Git\\bin\\bash.exe'])(
|
||||
'tells the agent to use the runner OS and shell syntax for %s',
|
||||
(shell) => {
|
||||
const tool = createRunnerBashTool({}, { environment: { platform: 'win32', shell } })
|
||||
expect(tool.description).toContain('Runner OS: win32')
|
||||
expect(tool.description).toContain(shell)
|
||||
expect(tool.description).toContain('Use syntax and programs compatible with this environment')
|
||||
},
|
||||
)
|
||||
|
||||
it('should execute a command on the runner with injected env vars', async () => {
|
||||
const tool = createRunnerBashTool(
|
||||
{ TEST_ENV: 'test-value' },
|
||||
{
|
||||
teamId: mockTeamId,
|
||||
agentRunnerId: null,
|
||||
},
|
||||
)
|
||||
|
||||
vi.mocked(runnerService.exec).mockImplementation(async (_runnerId, opts) => {
|
||||
opts.onOutput?.('hello output', 'stdout')
|
||||
return {
|
||||
executionId: 'exec-1',
|
||||
output: 'hello output',
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
aborted: false,
|
||||
}
|
||||
})
|
||||
|
||||
const onUpdate = createMockOnUpdate()
|
||||
const signal = createMockSignal()
|
||||
|
||||
const result = await invokeTool(tool, '1', { command: 'echo hello' }, signal, onUpdate)
|
||||
|
||||
expect(runnerService.requireOnlineRunner).toHaveBeenCalledWith(mockTeamId, null)
|
||||
expect(runnerService.exec).toHaveBeenCalledWith(
|
||||
'runner-1',
|
||||
expect.objectContaining({
|
||||
command: 'echo hello',
|
||||
env: { TEST_ENV: 'test-value' },
|
||||
collectOutput: false,
|
||||
}),
|
||||
)
|
||||
expect(result.content).toContainEqual(
|
||||
expect.objectContaining({ text: expect.stringContaining('hello output') }),
|
||||
)
|
||||
expect(onUpdate).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should stage loaded skills and inputFiles before executing', async () => {
|
||||
const loadedSkills = [{ id: 'skill-1', assetId: 'asset-1', hash: 'hash-1' }]
|
||||
const tool = createRunnerBashTool(
|
||||
{},
|
||||
{
|
||||
teamId: mockTeamId,
|
||||
agentRunnerId: 'runner-pinned',
|
||||
getLoadedSkills: () => loadedSkills,
|
||||
},
|
||||
)
|
||||
|
||||
vi.mocked(runnerService.exec).mockResolvedValue({
|
||||
executionId: 'exec-1',
|
||||
output: '',
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
aborted: false,
|
||||
})
|
||||
|
||||
await invokeTool(
|
||||
tool,
|
||||
'1',
|
||||
{
|
||||
command: 'ffmpeg -i .pi/generated/gen1/input.png out.mp4',
|
||||
inputFiles: ['.pi/generated/gen1/input.png'],
|
||||
},
|
||||
createMockSignal(),
|
||||
)
|
||||
|
||||
expect(runnerService.requireOnlineRunner).toHaveBeenCalledWith(mockTeamId, 'runner-pinned')
|
||||
expect(runnerService.ensureSkillOnRunner).toHaveBeenCalledWith('runner-1', loadedSkills[0])
|
||||
expect(runnerService.ensureGeneratedFileOnRunner).toHaveBeenCalledWith(
|
||||
'runner-1',
|
||||
'.pi/generated/gen1/input.png',
|
||||
)
|
||||
})
|
||||
|
||||
it('allows editor commands without a source classification', async () => {
|
||||
const tool = createRunnerBashTool(
|
||||
{},
|
||||
{
|
||||
teamId: mockTeamId,
|
||||
role: 'editor',
|
||||
},
|
||||
)
|
||||
|
||||
vi.mocked(runnerService.exec).mockImplementation(async (_runnerId, opts) => {
|
||||
opts.onOutput?.('skill output', 'stdout')
|
||||
return {
|
||||
executionId: 'exec-1',
|
||||
output: 'skill output',
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
aborted: false,
|
||||
}
|
||||
})
|
||||
|
||||
const signal = createMockSignal()
|
||||
const result = await invokeTool(tool, '1', { command: 'echo skill' }, signal)
|
||||
expect(result.content).toContainEqual(
|
||||
expect.objectContaining({ text: expect.stringContaining('skill output') }),
|
||||
)
|
||||
})
|
||||
|
||||
it('should handle runner execution errors', async () => {
|
||||
const tool = createRunnerBashTool({}, { teamId: mockTeamId })
|
||||
|
||||
vi.mocked(runnerService.exec).mockRejectedValue(new Error('Runner disconnected'))
|
||||
|
||||
const signal = createMockSignal()
|
||||
await expect(
|
||||
invokeTool(tool, '1', { command: 'fail' }, signal, createMockOnUpdate()),
|
||||
).rejects.toThrow('Runner disconnected')
|
||||
})
|
||||
|
||||
it('should handle non-zero exit codes', async () => {
|
||||
const tool = createRunnerBashTool({}, { teamId: mockTeamId })
|
||||
|
||||
vi.mocked(runnerService.exec).mockResolvedValue({
|
||||
executionId: 'exec-1',
|
||||
output: '',
|
||||
exitCode: 1,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
aborted: false,
|
||||
})
|
||||
|
||||
const signal = createMockSignal()
|
||||
await expect(
|
||||
invokeTool(tool, '1', { command: 'exit 1' }, signal, createMockOnUpdate()),
|
||||
).rejects.toThrow(/exited with code 1/)
|
||||
})
|
||||
|
||||
it('reports signal termination as a command failure', async () => {
|
||||
const tool = createRunnerBashTool({}, { teamId: mockTeamId })
|
||||
vi.mocked(runnerService.exec).mockResolvedValue({
|
||||
executionId: 'signaled',
|
||||
output: '',
|
||||
exitCode: null,
|
||||
signal: 'SIGTERM',
|
||||
timedOut: false,
|
||||
aborted: false,
|
||||
})
|
||||
await expect(invokeTool(tool, '1', { command: 'command' }, createMockSignal())).rejects.toThrow(
|
||||
'terminated by SIGTERM',
|
||||
)
|
||||
})
|
||||
|
||||
it('should handle timeouts', async () => {
|
||||
const tool = createRunnerBashTool({}, { teamId: mockTeamId })
|
||||
|
||||
vi.mocked(runnerService.exec).mockImplementation(async (_runnerId, opts) => {
|
||||
opts.onOutput?.('output before timeout\n', 'stdout')
|
||||
return {
|
||||
executionId: 'exec-1',
|
||||
output: 'output before timeout\n',
|
||||
exitCode: null,
|
||||
signal: 'SIGKILL',
|
||||
timedOut: true,
|
||||
aborted: false,
|
||||
}
|
||||
})
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = invokeTool(
|
||||
tool,
|
||||
'1',
|
||||
{ command: 'sleep 10', timeout: 2 },
|
||||
signal,
|
||||
createMockOnUpdate(),
|
||||
)
|
||||
|
||||
await expect(executePromise).rejects.toThrow(/output before timeout/)
|
||||
await expect(executePromise).rejects.toThrow(/timed out after 2 seconds/)
|
||||
})
|
||||
|
||||
it('should handle abort signals', async () => {
|
||||
const tool = createRunnerBashTool({}, { teamId: mockTeamId })
|
||||
|
||||
vi.mocked(runnerService.exec).mockResolvedValue({
|
||||
executionId: 'exec-1',
|
||||
output: '',
|
||||
exitCode: null,
|
||||
signal: 'SIGKILL',
|
||||
timedOut: false,
|
||||
aborted: true,
|
||||
})
|
||||
|
||||
const signal = createMockSignal()
|
||||
await expect(
|
||||
invokeTool(tool, '1', { command: 'long-run' }, signal, createMockOnUpdate()),
|
||||
).rejects.toThrow(/aborted/)
|
||||
})
|
||||
|
||||
it('should truncate output exceeding max lines (2000 lines) and report runner log file path', async () => {
|
||||
const tool = createRunnerBashTool({}, { teamId: mockTeamId })
|
||||
|
||||
vi.mocked(runnerService.exec).mockImplementation(async (_runnerId, opts) => {
|
||||
const lines: string[] = []
|
||||
for (let i = 1; i <= 3000; i++) {
|
||||
lines.push(`line-${i}`)
|
||||
}
|
||||
const chunk = lines.join('\n') + '\n'
|
||||
opts.onOutput?.(chunk, 'stdout')
|
||||
return {
|
||||
executionId: 'exec1',
|
||||
output: chunk,
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
aborted: false,
|
||||
fullOutputPath: '.pi/logs/bash-exec1.log',
|
||||
}
|
||||
})
|
||||
|
||||
const signal = createMockSignal()
|
||||
const result = await invokeTool(tool, '1', { command: 'generate lines' }, signal)
|
||||
const text = result.content.map((block) => (block.type === 'text' ? block.text : '')).join('')
|
||||
expect(text).toContain('line-3000')
|
||||
expect(text).toContain(
|
||||
'[Showing lines 1001-3000 of 3000. Full output: .pi/logs/bash-exec1.log]',
|
||||
)
|
||||
expect(result.details?.truncation).toMatchObject({
|
||||
truncated: true,
|
||||
truncatedBy: 'lines',
|
||||
totalLines: 3000,
|
||||
outputLines: 2000,
|
||||
})
|
||||
expect(result.details?.fullOutputPath).toBe('.pi/logs/bash-exec1.log')
|
||||
})
|
||||
|
||||
it('should truncate output exceeding max bytes (50KB) and report partial line size', async () => {
|
||||
const tool = createRunnerBashTool({}, { teamId: mockTeamId })
|
||||
|
||||
vi.mocked(runnerService.exec).mockImplementation(async (_runnerId, opts) => {
|
||||
const longLine = 'a'.repeat(60000)
|
||||
opts.onOutput?.(longLine, 'stdout')
|
||||
return {
|
||||
executionId: 'exec2',
|
||||
output: longLine,
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
aborted: false,
|
||||
fullOutputPath: '.pi/logs/bash-exec2.log',
|
||||
}
|
||||
})
|
||||
|
||||
const signal = createMockSignal()
|
||||
const result = await invokeTool(tool, '1', { command: 'long single line' }, signal)
|
||||
const text = result.content.map((block) => (block.type === 'text' ? block.text : '')).join('')
|
||||
expect(text).toMatch(
|
||||
/Showing last 50\.0KB of line 1 \(line is 58\.6KB\)\. Full output: \.pi\/logs\/bash-exec2\.log/,
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,243 @@
|
||||
import {
|
||||
type ToolRegistration as AgentTool,
|
||||
type ToolExecutionResult as AgentToolResult,
|
||||
} from '@earendil-works/pi-durable'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { quotaService } from '@shumai/core/src/quota/quota-service'
|
||||
import { runnerService } from '@shumai/core/src/runner'
|
||||
import type { TeamMemberRole } from '@shumai/db'
|
||||
import type { RunnerEnvironment } from '@shumai/dtos'
|
||||
import { Type } from 'typebox'
|
||||
import { OutputAccumulator } from '../utils/output-accumulator'
|
||||
import {
|
||||
DEFAULT_MAX_BYTES,
|
||||
DEFAULT_MAX_LINES,
|
||||
formatSize,
|
||||
type TruncationResult,
|
||||
} from '../utils/truncate'
|
||||
|
||||
const BashParameters = Type.Object({
|
||||
command: Type.String({ description: "The command to execute using the runner's shell." }),
|
||||
timeout: Type.Optional(Type.Number({ description: 'Maximum execution time in seconds.' })),
|
||||
inputFiles: Type.Optional(
|
||||
Type.Union([
|
||||
Type.Array(Type.String(), {
|
||||
description:
|
||||
'Optional list of generated file paths (e.g. ".pi/generated/<id>/<filename>") to stage into the runner workspace before executing the command.',
|
||||
}),
|
||||
Type.Null(),
|
||||
]),
|
||||
),
|
||||
})
|
||||
|
||||
export type BashDetails = {
|
||||
exitCode: number | null
|
||||
truncation?: TruncationResult
|
||||
fullOutputPath?: string
|
||||
}
|
||||
|
||||
export interface LoadedSkillRef {
|
||||
id: string
|
||||
assetId: string
|
||||
hash: string
|
||||
}
|
||||
|
||||
export interface RunnerBashOptions {
|
||||
teamId?: string
|
||||
agentRunnerId?: string | null
|
||||
runnerId?: string
|
||||
userId?: string
|
||||
role?: TeamMemberRole | null
|
||||
environment?: RunnerEnvironment
|
||||
getLoadedSkills?: () => Iterable<LoadedSkillRef>
|
||||
}
|
||||
|
||||
export const createRunnerBashTool = (
|
||||
skillEnvs: Record<string, string> = {},
|
||||
options?: RunnerBashOptions,
|
||||
): AgentTool<typeof BashParameters, BashDetails> => {
|
||||
return {
|
||||
name: 'bash',
|
||||
description: `Execute a shell command on the configured runner. ${options?.environment ? `Runner OS: ${options.environment.platform}. Shell: ${options.environment.shell}. Use syntax and programs compatible with this environment.` : 'The runner reports its OS and shell when connected.'} Returns stdout and stderr. Output is truncated to last ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). If truncated, full output is saved to a log file in .pi/logs/ on the runner. Optionally provide a timeout in seconds, or inputFiles to stage generated files (.pi/generated/...) into the runner workspace before running.`,
|
||||
parameters: BashParameters,
|
||||
outputLimits: { maxBytes: DEFAULT_MAX_BYTES, maxLines: DEFAULT_MAX_LINES, retain: 'tail' },
|
||||
execute: async (
|
||||
{ command, timeout, inputFiles },
|
||||
api,
|
||||
context,
|
||||
): Promise<AgentToolResult<BashDetails>> => {
|
||||
const signal = context.abortSignal
|
||||
if (options?.teamId) {
|
||||
await quotaService.checkQuota(
|
||||
{
|
||||
teamId: options.teamId,
|
||||
userId: options.userId,
|
||||
role: options.role,
|
||||
resource: 'agent_bash_call_count',
|
||||
resourceData: { command },
|
||||
},
|
||||
1,
|
||||
)
|
||||
}
|
||||
|
||||
let targetRunnerId: string
|
||||
if (options?.runnerId) {
|
||||
if (!runnerService.isRunnerConnected(options.runnerId)) {
|
||||
throw new Error(
|
||||
'Runner is currently offline. Please start your runner with "shumai-cli runner start" and try again.',
|
||||
)
|
||||
}
|
||||
targetRunnerId = options.runnerId
|
||||
} else if (options?.teamId) {
|
||||
const runner = await runnerService.requireOnlineRunner(
|
||||
options.teamId,
|
||||
options.agentRunnerId,
|
||||
)
|
||||
targetRunnerId = runner.id
|
||||
} else {
|
||||
throw new Error('No runner configured for this session.')
|
||||
}
|
||||
|
||||
// Ensure any loaded skills in this session are extracted on the runner before executing bash
|
||||
if (options?.getLoadedSkills) {
|
||||
for (const skill of options.getLoadedSkills()) {
|
||||
await runnerService.ensureSkillOnRunner(targetRunnerId, skill)
|
||||
}
|
||||
}
|
||||
|
||||
// Stage any explicitly requested generated files (.pi/generated/<id>/<file>) onto the runner
|
||||
if (inputFiles && Array.isArray(inputFiles)) {
|
||||
for (const filePath of inputFiles) {
|
||||
await runnerService.ensureGeneratedFileOnRunner(targetRunnerId, filePath)
|
||||
}
|
||||
}
|
||||
|
||||
const output = new OutputAccumulator({
|
||||
tempFilePrefix: 'shumai-bash',
|
||||
disableTempFile: true,
|
||||
})
|
||||
let acceptingOutput = true
|
||||
await api.details({ exitCode: null }, context)
|
||||
|
||||
const handleData = (data: Buffer) => {
|
||||
if (!acceptingOutput) return
|
||||
output.append(data)
|
||||
api.output(data)
|
||||
}
|
||||
|
||||
const finishOutput = async () => {
|
||||
acceptingOutput = false
|
||||
output.finish()
|
||||
const snapshot = output.snapshot({ persistIfTruncated: false })
|
||||
await output.closeTempFile()
|
||||
return snapshot
|
||||
}
|
||||
|
||||
const formatOutput = (
|
||||
snapshot: Awaited<ReturnType<typeof finishOutput>>,
|
||||
runnerLogPath: string,
|
||||
emptyText = '(no output)',
|
||||
) => {
|
||||
const truncation = snapshot.truncation
|
||||
let text = snapshot.content || emptyText
|
||||
let details: BashDetails | undefined
|
||||
if (truncation.truncated) {
|
||||
details = { exitCode: null, truncation, fullOutputPath: runnerLogPath }
|
||||
const startLine = truncation.totalLines - truncation.outputLines + 1
|
||||
const endLine = truncation.totalLines
|
||||
if (truncation.lastLinePartial) {
|
||||
const lastLineSize = formatSize(output.getLastLineBytes())
|
||||
text += `\n\n[Showing last ${formatSize(truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${runnerLogPath}]`
|
||||
} else if (truncation.truncatedBy === 'lines') {
|
||||
text += `\n\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines}. Full output: ${runnerLogPath}]`
|
||||
} else {
|
||||
text += `\n\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${runnerLogPath}]`
|
||||
}
|
||||
}
|
||||
return { text, details }
|
||||
}
|
||||
|
||||
const appendStatus = (text: string, status: string) => `${text ? `${text}\n\n` : ''}${status}`
|
||||
|
||||
const execResult = await runnerService.exec(targetRunnerId, {
|
||||
command,
|
||||
timeoutSec: timeout,
|
||||
env: skillEnvs,
|
||||
signal,
|
||||
collectOutput: false,
|
||||
onOutput: (chunk) => handleData(Buffer.from(chunk, 'utf-8')),
|
||||
})
|
||||
|
||||
const snapshot = await finishOutput()
|
||||
const runnerLogPath =
|
||||
execResult.fullOutputPath || `.pi/logs/bash-${execResult.executionId}.log`
|
||||
const { text: outputText, details: truncationDetails } = formatOutput(snapshot, runnerLogPath)
|
||||
|
||||
if (options?.teamId) {
|
||||
try {
|
||||
await quotaService.consumeQuota(
|
||||
{
|
||||
teamId: options.teamId,
|
||||
userId: options.userId,
|
||||
role: options.role,
|
||||
resource: 'agent_bash_call_count',
|
||||
resourceData: { command },
|
||||
},
|
||||
1,
|
||||
)
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'Failed to record quota usage for bash command')
|
||||
}
|
||||
}
|
||||
|
||||
if (signal?.aborted || execResult.aborted) {
|
||||
logger.error({ command, outputText }, 'Bash command aborted')
|
||||
throw new Error(appendStatus(outputText, 'Command aborted'))
|
||||
}
|
||||
|
||||
if (execResult.timedOut) {
|
||||
logger.error({ timeout, command, outputText }, 'Bash command timed out')
|
||||
throw new Error(appendStatus(outputText, `Bash command timed out after ${timeout} seconds`))
|
||||
}
|
||||
|
||||
if (execResult.error) {
|
||||
logger.error(
|
||||
{ error: execResult.error, command, outputText },
|
||||
'Bash command encountered runner error',
|
||||
)
|
||||
throw new Error(appendStatus(outputText, execResult.error))
|
||||
}
|
||||
|
||||
if (execResult.signal) {
|
||||
logger.error(
|
||||
{ signal: execResult.signal, command, outputText },
|
||||
'Bash command was terminated',
|
||||
)
|
||||
throw new Error(appendStatus(outputText, 'Bash command terminated by ' + execResult.signal))
|
||||
}
|
||||
|
||||
if (execResult.exitCode !== 0 && execResult.exitCode !== null) {
|
||||
logger.error(
|
||||
{ exitCode: execResult.exitCode, command, outputText },
|
||||
'Bash command exited with failure code',
|
||||
)
|
||||
throw new Error(
|
||||
appendStatus(outputText, `Bash command exited with code ${execResult.exitCode}`),
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
output: [
|
||||
{
|
||||
type: 'text',
|
||||
text: outputText,
|
||||
},
|
||||
],
|
||||
details: {
|
||||
exitCode: execResult.exitCode,
|
||||
...(truncationDetails ?? {}),
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,363 +0,0 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { createSandboxedBashTool } from './sandboxed-bash'
|
||||
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
|
||||
import { spawn } from 'node:child_process'
|
||||
import { EventEmitter } from 'node:events'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
|
||||
vi.mock('@anthropic-ai/sandbox-runtime')
|
||||
vi.mock('node:child_process')
|
||||
|
||||
describe('createSandboxedBashTool', () => {
|
||||
const mockCwd = '/mock/cwd'
|
||||
const mockSessionManager = {
|
||||
getSkillEnvs: vi.fn(),
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockSessionManager.getSkillEnvs.mockReturnValue({ TEST_ENV: 'test-value' })
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- SandboxManager.wrapWithSandbox is a mocked async function from a 3rd party library
|
||||
;(SandboxManager.wrapWithSandbox as any).mockImplementation(
|
||||
async (cmd: string) => `sandboxed-${cmd}`,
|
||||
)
|
||||
})
|
||||
|
||||
const createMockSignal = () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- signal needs to be partially compatible with AbortSignal but we need to emit events for testing
|
||||
const signal = new EventEmitter() as any
|
||||
signal.addEventListener = vi.fn((event: string, cb: (ev: Event) => unknown) => {
|
||||
signal.on(event, cb)
|
||||
})
|
||||
signal.removeEventListener = vi.fn((event: string, cb: (ev: Event) => unknown) => {
|
||||
signal.off(event, cb)
|
||||
})
|
||||
signal.aborted = false
|
||||
return signal
|
||||
}
|
||||
|
||||
const createMockOnUpdate = () => vi.fn()
|
||||
|
||||
it('should execute a command in the sandbox with injected env vars', async () => {
|
||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager.getSkillEnvs())
|
||||
|
||||
const mockStdout = new EventEmitter()
|
||||
const mockStderr = new EventEmitter()
|
||||
const mockChild = Object.assign(new EventEmitter(), {
|
||||
stdout: mockStdout,
|
||||
stderr: mockStderr,
|
||||
pid: 123,
|
||||
kill: vi.fn(),
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn is mocked to return a partial ChildProcess
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
|
||||
const onUpdate = createMockOnUpdate()
|
||||
const signal = createMockSignal()
|
||||
|
||||
// pi-coding-agent AgentTool.execute signature: (toolCallId, params, signal?, onUpdate?)
|
||||
const executePromise = tool.execute(
|
||||
'1',
|
||||
{ command: 'echo hello', source: 'user' },
|
||||
signal,
|
||||
onUpdate,
|
||||
)
|
||||
|
||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||
|
||||
expect(SandboxManager.wrapWithSandbox).toHaveBeenCalledWith(
|
||||
'echo hello',
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
{ commandId: '1' },
|
||||
)
|
||||
expect(spawn).toHaveBeenCalledWith(
|
||||
'bash',
|
||||
['-c', 'sandboxed-echo hello'],
|
||||
expect.objectContaining({
|
||||
env: expect.objectContaining({
|
||||
TEST_ENV: 'test-value',
|
||||
}),
|
||||
}),
|
||||
)
|
||||
|
||||
// Simulate stdout data
|
||||
mockStdout.emit('data', Buffer.from('hello output'))
|
||||
mockChild.emit('close', 0)
|
||||
|
||||
const result = await executePromise
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ToolResponse content is an array of content blocks
|
||||
expect((result as any).content[0].text).toContain('hello output')
|
||||
})
|
||||
|
||||
it('should block user-requested commands when restrictedUser is enabled', async () => {
|
||||
const tool = createSandboxedBashTool(mockCwd, {}, { restrictedUser: true })
|
||||
|
||||
const signal = createMockSignal()
|
||||
await expect(
|
||||
tool.execute('1', { command: 'echo hack', source: 'user' }, signal, createMockOnUpdate()),
|
||||
).rejects.toThrow(/not permitted for your role/)
|
||||
|
||||
expect(spawn).not.toHaveBeenCalled()
|
||||
expect(SandboxManager.wrapWithSandbox).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should allow skill-requested commands when restrictedUser is enabled', async () => {
|
||||
const tool = createSandboxedBashTool(mockCwd, {}, { restrictedUser: true })
|
||||
|
||||
const mockStdout = new EventEmitter()
|
||||
const mockStderr = new EventEmitter()
|
||||
const mockChild = Object.assign(new EventEmitter(), {
|
||||
stdout: mockStdout,
|
||||
stderr: mockStderr,
|
||||
pid: 124,
|
||||
kill: vi.fn(),
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn is mocked to return a partial ChildProcess
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute('1', { command: 'echo skill', source: 'skill' }, signal)
|
||||
|
||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||
expect(SandboxManager.wrapWithSandbox).toHaveBeenCalledWith(
|
||||
'echo skill',
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
{ commandId: '1' },
|
||||
)
|
||||
|
||||
mockStdout.emit('data', Buffer.from('skill output'))
|
||||
mockChild.emit('close', 0)
|
||||
|
||||
const result = await executePromise
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ToolResponse content is an array of content blocks
|
||||
expect((result as any).content[0].text).toContain('skill output')
|
||||
})
|
||||
|
||||
it('should handle process errors', async () => {
|
||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager.getSkillEnvs())
|
||||
const mockChild = new EventEmitter()
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute(
|
||||
'1',
|
||||
{ command: 'fail', source: 'user' },
|
||||
signal,
|
||||
createMockOnUpdate(),
|
||||
)
|
||||
|
||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||
mockChild.emit('error', new Error('spawn failed'))
|
||||
|
||||
await expect(executePromise).rejects.toThrow('spawn failed')
|
||||
})
|
||||
|
||||
it('should handle non-zero exit codes', async () => {
|
||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager.getSkillEnvs())
|
||||
const mockChild = Object.assign(new EventEmitter(), {
|
||||
stdout: new EventEmitter(),
|
||||
stderr: new EventEmitter(),
|
||||
pid: 1,
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute(
|
||||
'1',
|
||||
{ command: 'exit 1', source: 'user' },
|
||||
signal,
|
||||
createMockOnUpdate(),
|
||||
)
|
||||
|
||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||
mockChild.emit('close', 1)
|
||||
|
||||
await expect(executePromise).rejects.toThrow(/exited with code 1/)
|
||||
})
|
||||
|
||||
it('should handle timeouts', async () => {
|
||||
vi.useFakeTimers()
|
||||
|
||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager.getSkillEnvs())
|
||||
const mockChild = Object.assign(new EventEmitter(), {
|
||||
stdout: new EventEmitter(),
|
||||
stderr: new EventEmitter(),
|
||||
pid: 456,
|
||||
kill: vi.fn(),
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- process.kill return type is boolean
|
||||
const killSpy = vi.spyOn(process, 'kill').mockImplementation(() => true as any)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute(
|
||||
'1',
|
||||
{ command: 'sleep 10', timeout: 2, source: 'user' },
|
||||
signal,
|
||||
createMockOnUpdate(),
|
||||
)
|
||||
|
||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||
|
||||
await vi.advanceTimersByTimeAsync(2100)
|
||||
|
||||
expect(killSpy).toHaveBeenCalledWith(-456, 'SIGKILL')
|
||||
|
||||
mockChild.emit('close', null)
|
||||
|
||||
await expect(executePromise).rejects.toThrow(/timed out/)
|
||||
vi.useRealTimers()
|
||||
killSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('should handle abort signals', async () => {
|
||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager.getSkillEnvs())
|
||||
const mockChild = Object.assign(new EventEmitter(), {
|
||||
stdout: new EventEmitter(),
|
||||
stderr: new EventEmitter(),
|
||||
pid: 789,
|
||||
kill: vi.fn(),
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- process.kill return type is boolean
|
||||
const killSpy = vi.spyOn(process, 'kill').mockImplementation(() => true as any)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute(
|
||||
'1',
|
||||
{ command: 'long-run', source: 'user' },
|
||||
signal,
|
||||
createMockOnUpdate(),
|
||||
)
|
||||
|
||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||
|
||||
signal.aborted = true
|
||||
signal.emit('abort')
|
||||
|
||||
expect(killSpy).toHaveBeenCalledWith(-789, 'SIGKILL')
|
||||
|
||||
mockChild.emit('close', null)
|
||||
|
||||
await expect(executePromise).rejects.toThrow(/aborted/)
|
||||
killSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('should truncate output exceeding max lines (2000 lines) and persist full output to temp file', async () => {
|
||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager.getSkillEnvs())
|
||||
const mockStdout = new EventEmitter()
|
||||
const mockStderr = new EventEmitter()
|
||||
const mockChild = Object.assign(new EventEmitter(), {
|
||||
stdout: mockStdout,
|
||||
stderr: mockStderr,
|
||||
pid: 101,
|
||||
kill: vi.fn(),
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute('1', { command: 'generate lines', source: 'user' }, signal)
|
||||
|
||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||
|
||||
const lines: string[] = []
|
||||
for (let i = 1; i <= 3000; i++) {
|
||||
lines.push(`line-${i}`)
|
||||
}
|
||||
mockStdout.emit('data', Buffer.from(lines.join('\n') + '\n'))
|
||||
mockChild.emit('close', 0)
|
||||
|
||||
const result = await executePromise
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- content block array
|
||||
const text = (result as any).content[0].text
|
||||
expect(text).toContain('line-3000')
|
||||
expect(text).toContain('[Showing lines 1001-3000 of 3000. Full output:')
|
||||
expect(result.details?.truncation).toMatchObject({
|
||||
truncated: true,
|
||||
truncatedBy: 'lines',
|
||||
totalLines: 3000,
|
||||
outputLines: 2000,
|
||||
})
|
||||
|
||||
expect(result.details?.fullOutputPath).toBeDefined()
|
||||
const fileContent = await readFile(result.details!.fullOutputPath!, 'utf-8')
|
||||
expect(fileContent).toContain('line-1\nline-2')
|
||||
expect(fileContent).toContain('line-2999\nline-3000')
|
||||
})
|
||||
|
||||
it('should truncate output exceeding max bytes (50KB) and report partial line size', async () => {
|
||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager.getSkillEnvs())
|
||||
const mockStdout = new EventEmitter()
|
||||
const mockChild = Object.assign(new EventEmitter(), {
|
||||
stdout: mockStdout,
|
||||
stderr: new EventEmitter(),
|
||||
pid: 102,
|
||||
kill: vi.fn(),
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute(
|
||||
'1',
|
||||
{ command: 'long single line', source: 'user' },
|
||||
signal,
|
||||
)
|
||||
|
||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||
|
||||
const longLine = 'a'.repeat(60000)
|
||||
mockStdout.emit('data', Buffer.from(longLine))
|
||||
mockChild.emit('close', 0)
|
||||
|
||||
const result = await executePromise
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- content block array
|
||||
const text = (result as any).content[0].text
|
||||
expect(text).toMatch(/Showing last 50\.0KB of line 1 \(line is 58\.6KB\)\. Full output:/)
|
||||
})
|
||||
|
||||
it('should preserve truncated output when a command times out', async () => {
|
||||
vi.useFakeTimers()
|
||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager.getSkillEnvs())
|
||||
const mockStdout = new EventEmitter()
|
||||
const mockChild = Object.assign(new EventEmitter(), {
|
||||
stdout: mockStdout,
|
||||
stderr: new EventEmitter(),
|
||||
pid: 103,
|
||||
kill: vi.fn(),
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- process.kill return type boolean
|
||||
const killSpy = vi.spyOn(process, 'kill').mockImplementation(() => true as any)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute(
|
||||
'1',
|
||||
{ command: 'slow', timeout: 1, source: 'user' },
|
||||
signal,
|
||||
)
|
||||
|
||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||
|
||||
mockStdout.emit('data', Buffer.from('output before timeout\n'))
|
||||
await vi.advanceTimersByTimeAsync(1100)
|
||||
mockChild.emit('close', null)
|
||||
|
||||
await expect(executePromise).rejects.toThrow(/output before timeout/)
|
||||
await expect(executePromise).rejects.toThrow(/timed out after 1 seconds/)
|
||||
|
||||
vi.useRealTimers()
|
||||
killSpy.mockRestore()
|
||||
})
|
||||
})
|
||||
@@ -1,303 +0,0 @@
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { sandboxService } from '@shumai/core/src/sandbox/sandbox-service'
|
||||
import { type AgentTool, type AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import { Type } from 'typebox'
|
||||
import { spawn } from 'node:child_process'
|
||||
import { OutputAccumulator } from '../utils/output-accumulator'
|
||||
import {
|
||||
DEFAULT_MAX_BYTES,
|
||||
DEFAULT_MAX_LINES,
|
||||
formatSize,
|
||||
type TruncationResult,
|
||||
} from '../utils/truncate'
|
||||
import { quotaService } from '@shumai/core/src/quota/quota-service'
|
||||
import type { TeamMemberRole } from '@shumai/db'
|
||||
|
||||
const BashSource = Type.String({
|
||||
enum: ['user', 'skill'],
|
||||
description:
|
||||
'Indicates who requested this bash command. Set to "user" when the end user directly ' +
|
||||
'requested the command. Set to "skill" when the command is required by a skill you loaded ' +
|
||||
'via the read_skill tool. Users without the owner role can only execute commands with ' +
|
||||
'source="skill"; source="user" will be blocked.',
|
||||
})
|
||||
|
||||
const BashParameters = Type.Object({
|
||||
command: Type.String({ description: 'The bash command to execute.' }),
|
||||
timeout: Type.Optional(Type.Number({ description: 'Maximum execution time in seconds.' })),
|
||||
source: BashSource,
|
||||
})
|
||||
|
||||
export interface BashDetails {
|
||||
exitCode: number | null
|
||||
truncation?: TruncationResult
|
||||
fullOutputPath?: string
|
||||
}
|
||||
|
||||
export interface SandboxedBashOptions {
|
||||
teamId?: string
|
||||
userId?: string
|
||||
role?: TeamMemberRole | null
|
||||
getBlockedHost?: (commandId?: string) => string
|
||||
clearBlockedHost?: () => void
|
||||
/**
|
||||
* When true, the tool only allows commands requested by a loaded skill (source="skill").
|
||||
* Commands requested directly by the end user (source="user") are blocked.
|
||||
*/
|
||||
restrictedUser?: boolean
|
||||
}
|
||||
|
||||
const BASH_UPDATE_THROTTLE_MS = 100
|
||||
|
||||
export const createSandboxedBashTool = (
|
||||
cwd: string,
|
||||
skillEnvs: Record<string, string> = {},
|
||||
options?: SandboxedBashOptions,
|
||||
): AgentTool<typeof BashParameters, BashDetails> => {
|
||||
return {
|
||||
name: 'bash',
|
||||
description: `Execute a bash command in a sandboxed environment. Returns stdout and stderr. Output is truncated to last ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds. When in a restricted (non-owner) user context, only commands required by a skill loaded via the read_skill tool are allowed, and you must set source="skill".`,
|
||||
label: 'Executing bash command',
|
||||
parameters: BashParameters,
|
||||
execute: async (
|
||||
toolCallId,
|
||||
{ command, timeout, source },
|
||||
signal,
|
||||
onUpdate,
|
||||
): Promise<AgentToolResult<BashDetails>> => {
|
||||
if (options?.restrictedUser && source !== 'skill') {
|
||||
throw new Error(
|
||||
'Bash commands requested directly by the user (source="user") are not permitted for your role. ' +
|
||||
'Only commands required by a loaded skill (source="skill") can be executed.',
|
||||
)
|
||||
}
|
||||
if (options?.teamId) {
|
||||
await quotaService.checkQuota(
|
||||
{
|
||||
teamId: options.teamId,
|
||||
userId: options.userId,
|
||||
role: options.role,
|
||||
resource: 'agent_bash_call_count',
|
||||
resourceData: { command },
|
||||
},
|
||||
1,
|
||||
)
|
||||
}
|
||||
options?.clearBlockedHost?.()
|
||||
const wrappedCommand = await sandboxService.wrapCommand(command, { commandId: toolCallId })
|
||||
|
||||
const output = new OutputAccumulator({ tempFilePrefix: 'shumai-bash' })
|
||||
let acceptingOutput = true
|
||||
let updateTimer: NodeJS.Timeout | undefined
|
||||
let updateDirty = false
|
||||
let lastUpdateAt = 0
|
||||
|
||||
const emitOutputUpdate = () => {
|
||||
if (!onUpdate || !updateDirty) return
|
||||
updateDirty = false
|
||||
lastUpdateAt = Date.now()
|
||||
const snapshot = output.snapshot({ persistIfTruncated: true })
|
||||
onUpdate({
|
||||
content: [{ type: 'text', text: snapshot.content || '' }],
|
||||
details: {
|
||||
exitCode: null,
|
||||
truncation: snapshot.truncation.truncated ? snapshot.truncation : undefined,
|
||||
fullOutputPath: snapshot.fullOutputPath,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const clearUpdateTimer = () => {
|
||||
if (updateTimer) {
|
||||
clearTimeout(updateTimer)
|
||||
updateTimer = undefined
|
||||
}
|
||||
}
|
||||
|
||||
const scheduleOutputUpdate = () => {
|
||||
if (!onUpdate) return
|
||||
updateDirty = true
|
||||
const delay = BASH_UPDATE_THROTTLE_MS - (Date.now() - lastUpdateAt)
|
||||
if (delay <= 0) {
|
||||
clearUpdateTimer()
|
||||
emitOutputUpdate()
|
||||
return
|
||||
}
|
||||
updateTimer ??= setTimeout(() => {
|
||||
updateTimer = undefined
|
||||
emitOutputUpdate()
|
||||
}, delay)
|
||||
}
|
||||
|
||||
if (onUpdate) {
|
||||
onUpdate({ content: [], details: { exitCode: null } })
|
||||
}
|
||||
|
||||
const handleData = (data: Buffer) => {
|
||||
if (!acceptingOutput) return
|
||||
output.append(data)
|
||||
scheduleOutputUpdate()
|
||||
}
|
||||
|
||||
const finishOutput = async () => {
|
||||
acceptingOutput = false
|
||||
output.finish()
|
||||
clearUpdateTimer()
|
||||
emitOutputUpdate()
|
||||
const snapshot = output.snapshot({ persistIfTruncated: true })
|
||||
await output.closeTempFile()
|
||||
return snapshot
|
||||
}
|
||||
|
||||
const formatOutput = (
|
||||
snapshot: Awaited<ReturnType<typeof finishOutput>>,
|
||||
emptyText = '(no output)',
|
||||
) => {
|
||||
const truncation = snapshot.truncation
|
||||
let text = snapshot.content || emptyText
|
||||
let details: BashDetails | undefined
|
||||
if (truncation.truncated) {
|
||||
details = { exitCode: null, truncation, fullOutputPath: snapshot.fullOutputPath }
|
||||
const startLine = truncation.totalLines - truncation.outputLines + 1
|
||||
const endLine = truncation.totalLines
|
||||
if (truncation.lastLinePartial) {
|
||||
const lastLineSize = formatSize(output.getLastLineBytes())
|
||||
text += `\n\n[Showing last ${formatSize(truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${snapshot.fullOutputPath}]`
|
||||
} else if (truncation.truncatedBy === 'lines') {
|
||||
text += `\n\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines}. Full output: ${snapshot.fullOutputPath}]`
|
||||
} else {
|
||||
text += `\n\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${snapshot.fullOutputPath}]`
|
||||
}
|
||||
}
|
||||
return { text, details }
|
||||
}
|
||||
|
||||
const appendStatus = (text: string, status: string) => `${text ? `${text}\n\n` : ''}${status}`
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn('bash', ['-c', wrappedCommand], {
|
||||
cwd,
|
||||
detached: true,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env: {
|
||||
PATH: process.env.PATH || '',
|
||||
HOME: process.env.HOME || '',
|
||||
...skillEnvs,
|
||||
},
|
||||
})
|
||||
|
||||
let timedOut = false
|
||||
let timeoutHandle: NodeJS.Timeout | undefined
|
||||
|
||||
if (timeout !== undefined && timeout > 0) {
|
||||
timeoutHandle = setTimeout(() => {
|
||||
timedOut = true
|
||||
if (child.pid) {
|
||||
try {
|
||||
process.kill(-child.pid, 'SIGKILL')
|
||||
} catch {
|
||||
child.kill('SIGKILL')
|
||||
}
|
||||
}
|
||||
}, timeout * 1000)
|
||||
}
|
||||
|
||||
child.stdout?.on('data', handleData)
|
||||
child.stderr?.on('data', handleData)
|
||||
|
||||
const onAbort = () => {
|
||||
if (child.pid) {
|
||||
try {
|
||||
process.kill(-child.pid, 'SIGKILL')
|
||||
} catch {
|
||||
child.kill('SIGKILL')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
signal?.addEventListener('abort', onAbort, { once: true })
|
||||
|
||||
child.on('error', async (err) => {
|
||||
if (timeoutHandle) clearTimeout(timeoutHandle)
|
||||
signal?.removeEventListener('abort', onAbort)
|
||||
const snapshot = await finishOutput()
|
||||
logger.error(
|
||||
{ err, command, content: snapshot.content },
|
||||
'Sandboxed bash command encountered process error',
|
||||
)
|
||||
reject(err)
|
||||
})
|
||||
|
||||
child.on('close', async (code) => {
|
||||
if (timeoutHandle) clearTimeout(timeoutHandle)
|
||||
signal?.removeEventListener('abort', onAbort)
|
||||
|
||||
const blocked = options?.getBlockedHost?.(toolCallId)
|
||||
const snapshot = await finishOutput()
|
||||
const { text: outputText, details: truncationDetails } = formatOutput(snapshot)
|
||||
|
||||
if (options?.teamId) {
|
||||
try {
|
||||
await quotaService.consumeQuota(
|
||||
{
|
||||
teamId: options.teamId,
|
||||
userId: options.userId,
|
||||
role: options.role,
|
||||
resource: 'agent_bash_call_count',
|
||||
resourceData: { command },
|
||||
},
|
||||
1,
|
||||
)
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'Failed to record quota usage for bash command')
|
||||
}
|
||||
}
|
||||
|
||||
if (signal?.aborted) {
|
||||
logger.error({ command, outputText }, 'Sandboxed bash command aborted')
|
||||
reject(new Error(appendStatus(outputText, 'Command aborted')))
|
||||
} else if (timedOut) {
|
||||
logger.error({ timeout, command, outputText }, 'Sandboxed bash command timed out')
|
||||
reject(
|
||||
new Error(
|
||||
appendStatus(
|
||||
outputText,
|
||||
`Sandboxed bash command timed out after ${timeout} seconds`,
|
||||
),
|
||||
),
|
||||
)
|
||||
} else if (blocked) {
|
||||
reject(
|
||||
new Error(
|
||||
`Network request to ${blocked} is blocked, please ask admin to allow it in sandbox settings.`,
|
||||
),
|
||||
)
|
||||
} else if (code !== 0 && code !== null) {
|
||||
logger.error(
|
||||
{ exitCode: code, command, outputText },
|
||||
'Sandboxed bash command exited with failure code',
|
||||
)
|
||||
reject(
|
||||
new Error(
|
||||
appendStatus(outputText, `Sandboxed bash command exited with code ${code}`),
|
||||
),
|
||||
)
|
||||
} else {
|
||||
resolve({
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: outputText,
|
||||
},
|
||||
],
|
||||
details: {
|
||||
exitCode: code,
|
||||
...(truncationDetails ?? {}),
|
||||
},
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { OutputAccumulator } from './output-accumulator'
|
||||
|
||||
describe('OutputAccumulator', () => {
|
||||
it('does not accumulate rawChunks in memory when disableTempFile is true', () => {
|
||||
const acc = new OutputAccumulator({
|
||||
maxBytes: 1024,
|
||||
maxLines: 50,
|
||||
disableTempFile: true,
|
||||
})
|
||||
|
||||
const chunk = Buffer.from(`${'x'.repeat(2047)}\n`, 'utf-8')
|
||||
for (let i = 0; i < 256; i++) {
|
||||
acc.append(chunk)
|
||||
}
|
||||
acc.finish()
|
||||
|
||||
const internal = acc as unknown as { rawChunks: Buffer[]; tailBytes: number }
|
||||
expect(internal.rawChunks).toEqual([])
|
||||
expect(internal.tailBytes).toBeLessThanOrEqual(1024 * 4)
|
||||
|
||||
const snapshot = acc.snapshot({ persistIfTruncated: false })
|
||||
expect(snapshot.truncation.truncated).toBe(true)
|
||||
expect(snapshot.fullOutputPath).toBeUndefined()
|
||||
expect(Buffer.byteLength(snapshot.content, 'utf-8')).toBeLessThanOrEqual(1024)
|
||||
})
|
||||
})
|
||||
@@ -13,6 +13,7 @@ export interface OutputAccumulatorOptions {
|
||||
maxLines?: number
|
||||
maxBytes?: number
|
||||
tempFilePrefix?: string
|
||||
disableTempFile?: boolean
|
||||
}
|
||||
|
||||
export interface OutputSnapshot {
|
||||
@@ -35,6 +36,7 @@ export class OutputAccumulator {
|
||||
private readonly maxBytes: number
|
||||
private readonly maxRollingBytes: number
|
||||
private readonly tempFilePrefix: string
|
||||
private readonly disableTempFile: boolean
|
||||
private readonly decoder = new TextDecoder()
|
||||
|
||||
private rawChunks: Buffer[] = []
|
||||
@@ -57,6 +59,7 @@ export class OutputAccumulator {
|
||||
this.maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES
|
||||
this.maxRollingBytes = Math.max(this.maxBytes * 2, 1)
|
||||
this.tempFilePrefix = options.tempFilePrefix ?? 'shumai-bash'
|
||||
this.disableTempFile = options.disableTempFile ?? false
|
||||
}
|
||||
|
||||
append(data: Buffer): void {
|
||||
@@ -70,7 +73,7 @@ export class OutputAccumulator {
|
||||
if (this.tempFileStream || this.shouldUseTempFile()) {
|
||||
this.ensureTempFile()
|
||||
this.tempFileStream?.write(data)
|
||||
} else if (data.length > 0) {
|
||||
} else if (!this.disableTempFile && data.length > 0) {
|
||||
this.rawChunks.push(data)
|
||||
}
|
||||
}
|
||||
@@ -202,6 +205,9 @@ export class OutputAccumulator {
|
||||
}
|
||||
|
||||
private shouldUseTempFile(): boolean {
|
||||
if (this.disableTempFile) {
|
||||
return false
|
||||
}
|
||||
return (
|
||||
this.totalRawBytes > this.maxBytes ||
|
||||
this.totalDecodedBytes > this.maxBytes ||
|
||||
@@ -210,7 +216,7 @@ export class OutputAccumulator {
|
||||
}
|
||||
|
||||
private ensureTempFile(): void {
|
||||
if (this.tempFilePath) {
|
||||
if (this.disableTempFile || this.tempFilePath) {
|
||||
return
|
||||
}
|
||||
this.tempFilePath = defaultTempFilePath(this.tempFilePrefix)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
export const DEFAULT_MAX_LINES = 2000
|
||||
export const DEFAULT_MAX_BYTES = 50 * 1024 // 50KB
|
||||
|
||||
export interface TruncationResult {
|
||||
export type TruncationResult = {
|
||||
content: string
|
||||
truncated: boolean
|
||||
truncatedBy: 'lines' | 'bytes' | null
|
||||
|
||||
@@ -108,6 +108,7 @@ describe('Agent Autofill Workflow', () => {
|
||||
|
||||
// Verify AI autofill called with asset details and mapped fields
|
||||
expect(mockActivities.autofillAiActivity).toHaveBeenCalledWith({
|
||||
taskId: task.id,
|
||||
teamId: 't1',
|
||||
assetId: 'a1',
|
||||
assetName: 'test.png',
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { getProxyType } from '@shumai/core/src/utils/mime'
|
||||
import type { WorkflowTask } from '@shumai/db'
|
||||
import {
|
||||
ApplicationFailure,
|
||||
updateTaskStatusActivity,
|
||||
updateTaskUsageActivity,
|
||||
} from '@shumai/workflow-core'
|
||||
import { getProxyType } from '@shumai/core/src/utils/mime'
|
||||
import {
|
||||
autofillAiActivity,
|
||||
createCommentActivity,
|
||||
@@ -64,6 +64,7 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
||||
: undefined
|
||||
|
||||
const aiResult = await autofillAiActivity({
|
||||
taskId: task.id,
|
||||
teamId,
|
||||
assetId: asset.id,
|
||||
assetName: asset.name,
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
{
|
||||
"name": "@shumai/api",
|
||||
"version": "0.6.5",
|
||||
"version": "0.7.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
"types": "./src/index.ts",
|
||||
"dependencies": {
|
||||
"@better-auth/mcp": "1.7.12",
|
||||
"@earendil-works/pi-durable": "1.5.0",
|
||||
"@hono/zod-validator": "0.7.6",
|
||||
"@modelcontextprotocol/server": "2.0.0",
|
||||
"@shumai/core": "workspace:*",
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user