mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 14:08:24 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
||||
|
||||
### Changed
|
||||
|
||||
## [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
-4
@@ -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
|
||||
@@ -96,11 +95,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)
|
||||
})
|
||||
+78
-3
@@ -8,7 +8,63 @@ 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`. Add it to your shell's PATH if needed:
|
||||
|
||||
```sh
|
||||
export PATH="$HOME/.local/bin:$PATH"
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
@@ -31,7 +87,7 @@ export SHUMAI_ALLOW_DELETE="true"
|
||||
## 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 +105,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 +113,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 +320,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`; both commands accept `--out /absolute/path`. Unix archives require `tar`; creating Windows archives on Unix also requires `zip`. The test runs the generated installer against local HTTP fixtures, then authenticates, creates a folder, uploads and downloads random bytes, and checks failure recovery with no runtime on the executable's PATH.
|
||||
|
||||
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.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/cli",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"bin": {
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
copyAssets,
|
||||
createComment,
|
||||
createFolder,
|
||||
createProject,
|
||||
createVersionStack,
|
||||
deleteAsset,
|
||||
downloadAsset,
|
||||
@@ -25,6 +26,7 @@ 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() {
|
||||
@@ -75,6 +77,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 +119,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()
|
||||
|
||||
@@ -6,6 +6,7 @@ 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'
|
||||
|
||||
export interface ProgramOptions {
|
||||
callOperation?: CallOperation
|
||||
@@ -26,6 +27,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()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/premiere-uxp",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"private": true,
|
||||
"type": "commonjs",
|
||||
"scripts": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/transcode-app",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"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.6.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
|
||||
+11
-20
@@ -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 { closeAgentSessions, initAgentWorkflows } from '@shumai/agent'
|
||||
import { app } 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 { 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!')
|
||||
@@ -104,18 +105,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'
|
||||
@@ -228,6 +218,7 @@ async function run() {
|
||||
const shutdown = async () => {
|
||||
console.log('\nShutting down gracefully...')
|
||||
workflowService.stopStaleTaskRecovery()
|
||||
await closeAgentSessions()
|
||||
await workflowService.shutdownWorkers().catch(() => {})
|
||||
assetService.stopCleanupJob()
|
||||
server.stop(true)
|
||||
|
||||
@@ -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,21 +30,9 @@
|
||||
"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.6.6",
|
||||
"bin": {
|
||||
"shumai-cli": "./src/index.ts",
|
||||
},
|
||||
@@ -61,7 +49,7 @@
|
||||
},
|
||||
"apps/premiere-uxp": {
|
||||
"name": "@shumai/premiere-uxp",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"dependencies": {
|
||||
"@shumai/api": "workspace:*",
|
||||
"@shumai/dtos": "workspace:*",
|
||||
@@ -115,7 +103,7 @@
|
||||
},
|
||||
"apps/transcode": {
|
||||
"name": "@shumai/transcode-app",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"bin": {
|
||||
"shumai-transcode": "./src/index.ts",
|
||||
},
|
||||
@@ -127,7 +115,7 @@
|
||||
},
|
||||
"apps/web": {
|
||||
"name": "@shumai/web-app",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"dependencies": {
|
||||
"@shumai/agent": "workspace:*",
|
||||
"@shumai/api": "workspace:*",
|
||||
@@ -147,11 +135,12 @@
|
||||
},
|
||||
"packages/agent": {
|
||||
"name": "@shumai/agent",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"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 +160,10 @@
|
||||
},
|
||||
"packages/api": {
|
||||
"name": "@shumai/api",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"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 +181,7 @@
|
||||
},
|
||||
"packages/core": {
|
||||
"name": "@shumai/core",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"dependencies": {
|
||||
"@ai-sdk/alibaba": "2.0.38",
|
||||
"@ai-sdk/amazon-bedrock": "5.0.67",
|
||||
@@ -216,8 +206,9 @@
|
||||
"@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 +246,8 @@
|
||||
},
|
||||
"packages/db": {
|
||||
"name": "@shumai/db",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"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 +261,19 @@
|
||||
},
|
||||
"packages/dtos": {
|
||||
"name": "@shumai/dtos",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"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.6.6",
|
||||
"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 +286,18 @@
|
||||
},
|
||||
"packages/operations": {
|
||||
"name": "@shumai/operations",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"dependencies": {
|
||||
"zod": "4.5.4",
|
||||
},
|
||||
},
|
||||
"packages/tableprinter": {
|
||||
"name": "@shumai/tableprinter",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
},
|
||||
"packages/transcode": {
|
||||
"name": "@shumai/transcode",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"dependencies": {
|
||||
"@shumai/core": "workspace:*",
|
||||
"@shumai/db": "workspace:*",
|
||||
@@ -316,13 +308,15 @@
|
||||
},
|
||||
"packages/webui": {
|
||||
"name": "@shumai/webui",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"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 +413,7 @@
|
||||
},
|
||||
"packages/workflow-core": {
|
||||
"name": "@shumai/workflow-core",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"dependencies": {
|
||||
"@dbos-inc/dbos-sdk": "5.2.11",
|
||||
"@shumai/core": "workspace:*",
|
||||
@@ -429,9 +423,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=="],
|
||||
|
||||
@@ -487,19 +478,11 @@
|
||||
|
||||
"@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/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 +518,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 +610,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 +822,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 +856,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=="],
|
||||
@@ -1119,8 +1102,6 @@
|
||||
|
||||
"@shumai/agent": ["@shumai/agent@workspace:packages/agent"],
|
||||
|
||||
"@shumai/agent-app": ["@shumai/agent-app@workspace:apps/agent"],
|
||||
|
||||
"@shumai/api": ["@shumai/api@workspace:packages/api"],
|
||||
|
||||
"@shumai/cli": ["@shumai/cli@workspace:apps/cli"],
|
||||
@@ -1149,6 +1130,8 @@
|
||||
|
||||
"@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=="],
|
||||
@@ -1161,16 +1144,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 +1224,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=="],
|
||||
@@ -1615,7 +1596,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=="],
|
||||
|
||||
@@ -2031,6 +2012,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=="],
|
||||
@@ -2153,9 +2136,9 @@
|
||||
|
||||
"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=="],
|
||||
|
||||
"iconv-lite": ["iconv-lite@0.7.3", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "sha512-IKXpvIzjnC9XTAUbVBcMfGS0EPaIXtW6v+zr+RRp+hqULEpo0owZax6wyRwPOJbWbzjYspQwusTsfVr0ifh4uQ=="],
|
||||
|
||||
@@ -2493,7 +2476,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=="],
|
||||
|
||||
@@ -2619,6 +2602,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 +2612,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=="],
|
||||
@@ -2781,6 +2768,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=="],
|
||||
@@ -3009,8 +2998,6 @@
|
||||
|
||||
"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 +3008,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,13 +3024,7 @@
|
||||
|
||||
"@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=="],
|
||||
|
||||
"@earendil-works/pi-agent-core/typebox": ["typebox@1.1.38", "", {}, "sha512-pZ0aQPmMmXoUvSbeuWf/Hzsc+avNw/Zd6VeE8CFgkVGWyuHPJvqeJJDeJqLve+K70LvjYIoleGcoJHPT17cWoA=="],
|
||||
|
||||
"@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=="],
|
||||
|
||||
@@ -3077,6 +3056,8 @@
|
||||
|
||||
"@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=="],
|
||||
@@ -3235,6 +3216,8 @@
|
||||
|
||||
"foreground-child/signal-exit": ["signal-exit@4.1.0", "", {}, "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw=="],
|
||||
|
||||
"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=="],
|
||||
@@ -3281,6 +3264,24 @@
|
||||
|
||||
"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=="],
|
||||
|
||||
"@isaacs/cliui/string-width/emoji-regex": ["emoji-regex@9.2.2", "", {}, "sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg=="],
|
||||
|
||||
"@isaacs/cliui/strip-ansi/ansi-regex": ["ansi-regex@6.3.0", "", {}, "sha512-WpDfL7NO6j7tH88IDBNVdUJxDh9nmCteAVW9dsep846XdwF4naCBK+/tGLX3KJgcpgMRXCFlTM2hKGoK9FsdrQ=="],
|
||||
@@ -3383,6 +3384,8 @@
|
||||
|
||||
"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=="],
|
||||
|
||||
"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 +3396,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 +3448,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=="],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,23 @@ title: "Changelog"
|
||||
description: "Recent updates, new features, and improvements in Shumai"
|
||||
---
|
||||
|
||||
<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,11 +62,11 @@ 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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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,11 +43,11 @@ 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`)。
|
||||
|
||||
@@ -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)。
|
||||
|
||||
@@ -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.6.6",
|
||||
"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,15 @@
|
||||
{
|
||||
"name": "@shumai/agent",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"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
|
||||
@@ -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,
|
||||
@@ -220,7 +243,7 @@ User messages may contain a <context> block detailing the user, active asset loc
|
||||
})),
|
||||
enabledSkillIds,
|
||||
allowedDomains,
|
||||
sessionId: params.sessionId,
|
||||
sessionId: persistedSessionId,
|
||||
userId: params.userId,
|
||||
projectId: params.projectId,
|
||||
assetId: params.assetId,
|
||||
@@ -237,225 +260,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 +475,7 @@ export const agentChatActivity = defineStep(async function agentChatActivity(
|
||||
})
|
||||
|
||||
export interface AutofillAiParams {
|
||||
taskId?: string
|
||||
teamId: string
|
||||
images?: string[]
|
||||
fields: AutofillField[]
|
||||
@@ -542,14 +496,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 +545,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 +558,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 +583,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,
|
||||
@@ -1499,7 +1125,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)
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import type { SandboxConfig, SandboxProvider, WrapCommandOptions } from '@shumai/core'
|
||||
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 { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { spawn } from 'node:child_process'
|
||||
import { EventEmitter } from 'node:events'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createAgentSession, type DbProviderInfo } from './index'
|
||||
import { closeAgentSessions } from './runtime'
|
||||
import { createSandboxedBashTool } from './tools/sandboxed-bash'
|
||||
|
||||
vi.mock('node:child_process')
|
||||
|
||||
@@ -142,7 +144,7 @@ describe('Concurrent Sandbox Sessions', () => {
|
||||
// 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', {
|
||||
const sessionExecutionA = invokeTool(tool, 'call-session-a-1', {
|
||||
command: 'curl https://api.github.com/repos',
|
||||
source: 'skill',
|
||||
})
|
||||
@@ -244,11 +246,11 @@ describe('Concurrent Sandbox Sessions', () => {
|
||||
// Record violation for the blocked command only
|
||||
testProvider.violationsByCommandId.set('call-blocked', 'blocked-target.com')
|
||||
|
||||
const blockedExecution = tool.execute('call-blocked', {
|
||||
const blockedExecution = invokeTool(tool, 'call-blocked', {
|
||||
command: 'curl http://blocked-target.com',
|
||||
source: 'skill',
|
||||
})
|
||||
const allowedExecution = tool.execute('call-allowed', {
|
||||
const allowedExecution = invokeTool(tool, 'call-allowed', {
|
||||
command: 'curl https://api.github.com/data',
|
||||
source: 'skill',
|
||||
})
|
||||
@@ -322,3 +324,5 @@ describe('Concurrent Sandbox Sessions', () => {
|
||||
expect(sandboxService.getCurrentAllowedDomains()).toEqual(['*.github.com'])
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(closeAgentSessions)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,30 @@
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import {
|
||||
formatSkillsForPrompt,
|
||||
createAgentSession,
|
||||
getModelFromDb,
|
||||
buildRestrictedUserInstructions,
|
||||
type DbProviderInfo,
|
||||
} from './index'
|
||||
import { createSandboxedBashTool } from './tools/sandboxed-bash'
|
||||
import { SandboxManager, type SandboxAskCallback } from '@anthropic-ai/sandbox-runtime'
|
||||
import { quotaService } from '@shumai/core/src/quota/quota-service'
|
||||
import { BACKGROUND_CONTEXT as piContext } from '@earendil-works/chord/context'
|
||||
import { Harness } from '@earendil-works/pi-durable'
|
||||
import { appendSessionEntries } from '@shumai/core/src/agent/session-test-utils'
|
||||
import { getAgentPrompt, getAgentTools, invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { mediaGenerationService } from '@shumai/core/src/media-generation/media-generation'
|
||||
import { quotaService } from '@shumai/core/src/quota/quota-service'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import * as fs from 'fs'
|
||||
import * as childProcess from 'node:child_process'
|
||||
import * as path from 'path'
|
||||
import { type TSchema } from 'typebox'
|
||||
import { Value } from 'typebox/value'
|
||||
import * as path from 'path'
|
||||
import * as childProcess from 'node:child_process'
|
||||
import * as fs from 'fs'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
buildRestrictedUserInstructions,
|
||||
createAgentSession,
|
||||
formatSkillsForPrompt,
|
||||
getModelFromDb,
|
||||
type DbProviderInfo,
|
||||
} from './index'
|
||||
import { closeAgentSessions } from './runtime'
|
||||
import { createSandboxedBashTool } from './tools/sandboxed-bash'
|
||||
afterEach(async () => {
|
||||
await closeAgentSessions()
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let mockSpawn: any = null
|
||||
@@ -187,7 +195,10 @@ describe('Sandbox Network isolation integration', () => {
|
||||
return mockChild
|
||||
}
|
||||
|
||||
const bashPromise = bashTool.execute('1', { command: 'curl blocked-api.com', source: 'user' })
|
||||
const bashPromise = invokeTool(bashTool, '1', {
|
||||
command: 'curl blocked-api.com',
|
||||
source: 'user',
|
||||
})
|
||||
|
||||
try {
|
||||
await expect(bashPromise).rejects.toThrow(
|
||||
@@ -417,7 +428,9 @@ describe('Sandbox Network isolation integration', () => {
|
||||
providers: mockProviders,
|
||||
thinkingLevel: 'high',
|
||||
})
|
||||
expect(harnessHigh.getThinkingLevel()).toBe('high')
|
||||
expect((await (await harnessHigh.root(piContext)).agent(piContext)).thinkingLevel).toBe(
|
||||
'high',
|
||||
)
|
||||
|
||||
// Call createAgentSession without thinkingLevel
|
||||
const { harness: harnessDefault } = await createAgentSession({
|
||||
@@ -430,7 +443,9 @@ describe('Sandbox Network isolation integration', () => {
|
||||
allowedDomains: [],
|
||||
providers: mockProviders,
|
||||
})
|
||||
expect(harnessDefault.getThinkingLevel()).toBe('off')
|
||||
expect((await (await harnessDefault.root(piContext)).agent(piContext)).thinkingLevel).toBe(
|
||||
'off',
|
||||
)
|
||||
} finally {
|
||||
initializeSpy.mockRestore()
|
||||
}
|
||||
@@ -457,9 +472,10 @@ describe('Sandbox Network isolation integration', () => {
|
||||
},
|
||||
})
|
||||
|
||||
const openSpy = vi.spyOn(Harness, 'open')
|
||||
const initializeSpy = vi.spyOn(SandboxManager, 'initialize').mockResolvedValue()
|
||||
try {
|
||||
const { harness } = await createAgentSession({
|
||||
await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: 'test-agent-retry',
|
||||
providerName: 'google',
|
||||
@@ -472,13 +488,76 @@ describe('Sandbox Network isolation integration', () => {
|
||||
baseDelayMs: 1500,
|
||||
})
|
||||
|
||||
// @ts-expect-error accessing private property for verification
|
||||
expect(harness.streamOptions?.maxRetries).toBe(5)
|
||||
expect(openSpy.mock.calls.at(-1)?.[1].settings).toMatchObject({
|
||||
retry: { maxRetries: 5, baseDelayMs: 1500 },
|
||||
})
|
||||
} finally {
|
||||
initializeSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
|
||||
it.each(['openai', 'custom-responses'])(
|
||||
'authenticates %s Responses sessions using the configured API key',
|
||||
async (providerName) => {
|
||||
const team = await prisma.team.create({ data: { name: 'Responses team' } })
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Responses agent', email: 'responses@test.com', type: 'agent' },
|
||||
})
|
||||
const modelId = 'gpt-4.1'
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: user.id,
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: providerName, model: modelId },
|
||||
},
|
||||
})
|
||||
const baseUrl =
|
||||
providerName === 'openai' ? 'https://api.openai.com/v1' : 'https://example.test/v1'
|
||||
const openSpy = vi.spyOn(Harness, 'open')
|
||||
vi.stubEnv('CLOUDFLARE_ACCOUNT_ID', '')
|
||||
vi.stubEnv('CLOUDFLARE_GATEWAY_ID', '')
|
||||
vi.stubEnv('TEST_RESPONSES_API_KEY', 'test-responses-key')
|
||||
try {
|
||||
await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: agent.id,
|
||||
providerName,
|
||||
modelId,
|
||||
systemPrompt: 'prompt',
|
||||
teamSkills: [],
|
||||
disableTools: true,
|
||||
providers: [
|
||||
{
|
||||
name: providerName,
|
||||
config: { api: 'openai-responses', baseUrl, apiKey: 'TEST_RESPONSES_API_KEY' },
|
||||
models: [
|
||||
{
|
||||
modelId,
|
||||
name: 'Responses model',
|
||||
config: { ...mockProviders[0].models[0].config, api: 'openai-responses' },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
const configuredModels = openSpy.mock.lastCall?.[1].models
|
||||
const models =
|
||||
typeof configuredModels === 'function' ? configuredModels() : configuredModels
|
||||
expect(models?.getModel(providerName, modelId)).toMatchObject({
|
||||
api: 'openai-responses',
|
||||
baseUrl,
|
||||
})
|
||||
await expect(models?.getAuth(providerName)).resolves.toMatchObject({
|
||||
auth: { apiKey: 'test-responses-key' },
|
||||
})
|
||||
} finally {
|
||||
openSpy.mockRestore()
|
||||
vi.unstubAllEnvs()
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
it('should filter out denied tools from AgentHarness', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Test Team 3' },
|
||||
@@ -512,7 +591,7 @@ describe('Sandbox Network isolation integration', () => {
|
||||
allowedDomains: [],
|
||||
providers: mockProviders,
|
||||
})
|
||||
const tools = harness.getTools()
|
||||
const tools = await getAgentTools(harness)
|
||||
const bashTool = tools.find((t) => t.name === 'bash')
|
||||
expect(bashTool).toBeUndefined()
|
||||
const readSkillTool = tools.find((t) => t.name === 'read_skill')
|
||||
@@ -724,21 +803,20 @@ describe('createAgentSession bash restriction for non-owner users', () => {
|
||||
|
||||
it('should include the bash tool from the start for team owners', async () => {
|
||||
const { harness } = await setupSession({ role: 'owner' })
|
||||
expect(harness.getTools().some((t) => t.name === 'bash')).toBe(true)
|
||||
expect((await getAgentTools(harness)).some((t) => t.name === 'bash')).toBe(true)
|
||||
})
|
||||
|
||||
it('should include the bash tool from the start when there is no user context', async () => {
|
||||
const { harness } = await setupSession({ role: 'none' })
|
||||
expect(harness.getTools().some((t) => t.name === 'bash')).toBe(true)
|
||||
expect((await getAgentTools(harness)).some((t) => t.name === 'bash')).toBe(true)
|
||||
})
|
||||
|
||||
it('should treat a user who is not a team member as restricted (fail-closed)', async () => {
|
||||
const { harness } = await setupSession({ role: 'non-member' })
|
||||
const tools = harness.getTools()
|
||||
const tools = await getAgentTools(harness)
|
||||
expect(tools.some((t) => t.name === 'bash')).toBe(false)
|
||||
expect(tools.some((t) => t.name === 'read_skill')).toBe(true)
|
||||
// @ts-expect-error accessing private property for verification
|
||||
const prompt = await harness.systemPrompt()
|
||||
const prompt = await await getAgentPrompt(harness)
|
||||
expect(prompt).toContain('# Restricted User Context')
|
||||
})
|
||||
|
||||
@@ -746,7 +824,7 @@ describe('createAgentSession bash restriction for non-owner users', () => {
|
||||
'should exclude the bash tool initially for %s users',
|
||||
async (role) => {
|
||||
const { harness } = await setupSession({ role })
|
||||
const tools = harness.getTools()
|
||||
const tools = await getAgentTools(harness)
|
||||
expect(tools.some((t) => t.name === 'bash')).toBe(false)
|
||||
expect(tools.some((t) => t.name === 'read_skill')).toBe(true)
|
||||
},
|
||||
@@ -785,23 +863,22 @@ describe('createAgentSession bash restriction for non-owner users', () => {
|
||||
type: 'chat',
|
||||
},
|
||||
})
|
||||
await prisma.agentSessionEntry.create({
|
||||
data: {
|
||||
id: 'restore-entry-1',
|
||||
sessionId: session.id,
|
||||
type: 'message',
|
||||
data: {
|
||||
message: {
|
||||
await appendSessionEntries(session.id, [
|
||||
{
|
||||
kind: 'pi.tool-result',
|
||||
model: [
|
||||
{
|
||||
role: 'toolResult',
|
||||
toolCallId: 'loaded',
|
||||
content: [{ type: 'text', text: 'skill content restored' }],
|
||||
toolName: 'read_skill',
|
||||
isError: false,
|
||||
details: { skillId: 'previously-loaded-skill' },
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
])
|
||||
|
||||
const initializeSpy = vi.spyOn(SandboxManager, 'initialize').mockResolvedValue()
|
||||
try {
|
||||
@@ -817,7 +894,7 @@ describe('createAgentSession bash restriction for non-owner users', () => {
|
||||
userId: user.id,
|
||||
sessionId: session.id,
|
||||
})
|
||||
expect(harness.getTools().some((t) => t.name === 'bash')).toBe(true)
|
||||
expect((await getAgentTools(harness)).some((t) => t.name === 'bash')).toBe(true)
|
||||
} finally {
|
||||
initializeSpy.mockRestore()
|
||||
}
|
||||
@@ -837,17 +914,17 @@ describe('createAgentSession bash restriction for non-owner users', () => {
|
||||
return ''
|
||||
})
|
||||
|
||||
const readSkillTool = harness.getTools().find((t) => t.name === 'read_skill')
|
||||
const readSkillTool = (await getAgentTools(harness)).find((t) => t.name === 'read_skill')
|
||||
expect(readSkillTool).toBeDefined()
|
||||
expect(harness.getTools().some((t) => t.name === 'bash')).toBe(false)
|
||||
expect(harness.getActiveTools().some((t) => t.name === 'bash')).toBe(false)
|
||||
expect((await getAgentTools(harness)).some((t) => t.name === 'bash')).toBe(false)
|
||||
expect((await getAgentTools(harness)).some((t) => t.name === 'bash')).toBe(false)
|
||||
|
||||
await readSkillTool!.execute('1', { skillId: skill.id }, undefined, undefined, undefined)
|
||||
await invokeTool(readSkillTool!, '1', { skillId: skill.id }, undefined, undefined)
|
||||
|
||||
expect(harness.getTools().some((t) => t.name === 'bash')).toBe(true)
|
||||
expect((await getAgentTools(harness)).some((t) => t.name === 'bash')).toBe(true)
|
||||
// bash must be part of the ACTIVE tool set, not just the registry, or the model
|
||||
// would never receive it on the next turn
|
||||
expect(harness.getActiveTools().some((t) => t.name === 'bash')).toBe(true)
|
||||
expect((await getAgentTools(harness)).some((t) => t.name === 'bash')).toBe(true)
|
||||
})
|
||||
|
||||
it('should not inject the bash tool for restricted users when bash is in deniedTools', async () => {
|
||||
@@ -864,26 +941,24 @@ describe('createAgentSession bash restriction for non-owner users', () => {
|
||||
return ''
|
||||
})
|
||||
|
||||
const readSkillTool = harness.getTools().find((t) => t.name === 'read_skill')
|
||||
const readSkillTool = (await getAgentTools(harness)).find((t) => t.name === 'read_skill')
|
||||
expect(readSkillTool).toBeDefined()
|
||||
|
||||
await readSkillTool!.execute('1', { skillId: skill.id }, undefined, undefined, undefined)
|
||||
await invokeTool(readSkillTool!, '1', { skillId: skill.id }, undefined, undefined)
|
||||
|
||||
expect(harness.getTools().some((t) => t.name === 'bash')).toBe(false)
|
||||
expect((await getAgentTools(harness)).some((t) => t.name === 'bash')).toBe(false)
|
||||
})
|
||||
|
||||
it('should include restricted user instructions in the system prompt for non-owners', async () => {
|
||||
const { harness } = await setupSession({ role: 'editor' })
|
||||
// @ts-expect-error accessing private property for verification
|
||||
const prompt = await harness.systemPrompt()
|
||||
const prompt = await await getAgentPrompt(harness)
|
||||
expect(prompt).toContain('# Restricted User Context')
|
||||
expect(prompt).toContain('source="user" will be rejected')
|
||||
})
|
||||
|
||||
it('should not include restricted user instructions in the system prompt for owners', async () => {
|
||||
const { harness } = await setupSession({ role: 'owner' })
|
||||
// @ts-expect-error accessing private property for verification
|
||||
const prompt = await harness.systemPrompt()
|
||||
const prompt = await await getAgentPrompt(harness)
|
||||
expect(prompt).not.toContain('# Restricted User Context')
|
||||
})
|
||||
})
|
||||
@@ -982,7 +1057,7 @@ describe('createAgentSession typed metadata schema', () => {
|
||||
|
||||
it('should build create_file/create_version with the project CREATION_CONTEXT fields', async () => {
|
||||
const { harness } = await setupSessionWithProject({ withCreationFields: true })
|
||||
const tools = harness.getTools()
|
||||
const tools = await getAgentTools(harness)
|
||||
|
||||
expect(tools.some((t) => t.name === 'list_autofill_fields')).toBe(false)
|
||||
|
||||
@@ -1018,8 +1093,8 @@ describe('createAgentSession typed metadata schema', () => {
|
||||
|
||||
it('should omit the metadata parameter when the project has no CREATION_CONTEXT fields', async () => {
|
||||
const { harness } = await setupSessionWithProject({ withCreationFields: false })
|
||||
const createFile = harness.getTools().find((t) => t.name === 'create_file')
|
||||
const createVersion = harness.getTools().find((t) => t.name === 'create_version')
|
||||
const createFile = (await getAgentTools(harness)).find((t) => t.name === 'create_file')
|
||||
const createVersion = (await getAgentTools(harness)).find((t) => t.name === 'create_version')
|
||||
expect(createFile).toBeDefined()
|
||||
expect(createVersion).toBeDefined()
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- inspecting the runtime schema shape of the dynamically-built tool
|
||||
@@ -1032,7 +1107,7 @@ describe('createAgentSession typed metadata schema', () => {
|
||||
|
||||
it('should include rename_asset and move_assets by default, but exclude delete_asset', async () => {
|
||||
const { harness } = await setupSessionWithProject({ withCreationFields: false })
|
||||
const toolNames = harness.getTools().map((t) => t.name)
|
||||
const toolNames = (await getAgentTools(harness)).map((t) => t.name)
|
||||
|
||||
expect(toolNames).toContain('rename_asset')
|
||||
expect(toolNames).toContain('move_assets')
|
||||
@@ -1081,7 +1156,7 @@ describe('createAgentSession typed metadata schema', () => {
|
||||
providers: mockProviders,
|
||||
userId: user.id,
|
||||
})
|
||||
const toolNames = harness.getTools().map((t) => t.name)
|
||||
const toolNames = (await getAgentTools(harness)).map((t) => t.name)
|
||||
expect(toolNames).toContain('delete_asset')
|
||||
} finally {
|
||||
initializeSpy.mockRestore()
|
||||
@@ -1151,13 +1226,12 @@ describe('createAgentSession enabled skill filtering', () => {
|
||||
})
|
||||
|
||||
// The disabled skill must not be advertised in the available skills list
|
||||
// @ts-expect-error accessing private property for verification
|
||||
const prompt = await harness.systemPrompt()
|
||||
const prompt = await await getAgentPrompt(harness)
|
||||
expect(prompt).toContain(enabledSkill.id)
|
||||
expect(prompt).not.toContain(disabledSkill.id)
|
||||
|
||||
// read_skill must reject the disabled skill id
|
||||
const readSkillTool = harness.getTools().find((t) => t.name === 'read_skill')
|
||||
const readSkillTool = (await getAgentTools(harness)).find((t) => t.name === 'read_skill')
|
||||
expect(readSkillTool).toBeDefined()
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
@@ -1169,15 +1243,15 @@ describe('createAgentSession enabled skill filtering', () => {
|
||||
})
|
||||
|
||||
await expect(
|
||||
readSkillTool!.execute('1', { skillId: disabledSkill.id }, undefined, undefined, undefined),
|
||||
invokeTool(readSkillTool!, '1', { skillId: disabledSkill.id }, undefined, undefined),
|
||||
).rejects.toThrow('not enabled for this agent')
|
||||
|
||||
const result = await readSkillTool!.execute(
|
||||
const result = await invokeTool(
|
||||
readSkillTool!,
|
||||
'1',
|
||||
{ skillId: enabledSkill.id },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((result.content[0] as any).text).toBe('# Enabled Skill')
|
||||
@@ -1223,11 +1297,11 @@ describe('createAgentSession enabled skill filtering', () => {
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
const readSkillTool = harness.getTools().find((t) => t.name === 'read_skill')
|
||||
const readSkillTool = (await getAgentTools(harness)).find((t) => t.name === 'read_skill')
|
||||
expect(readSkillTool).toBeDefined()
|
||||
|
||||
await expect(
|
||||
readSkillTool!.execute('1', { skillId: skill.id }, undefined, undefined, undefined),
|
||||
invokeTool(readSkillTool!, '1', { skillId: skill.id }, undefined, undefined),
|
||||
).rejects.toThrow('not enabled for this agent')
|
||||
} finally {
|
||||
initializeSpy.mockRestore()
|
||||
@@ -1283,26 +1357,26 @@ describe('createAgentSession enabled skill filtering', () => {
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
const readThreadTool = harness.getTools().find((t) => t.name === 'read_thread')
|
||||
const readThreadTool = (await getAgentTools(harness)).find((t) => t.name === 'read_thread')
|
||||
expect(readThreadTool).toBeDefined()
|
||||
|
||||
// First call executes and consumes quota
|
||||
const res1 = await readThreadTool!.execute(
|
||||
const res1 = await invokeTool(
|
||||
readThreadTool!,
|
||||
'call-1',
|
||||
{ threadId: 'non-existent' },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
expect(res1).toBeDefined()
|
||||
|
||||
// Second call exceeds quota and returns error
|
||||
const res2 = await readThreadTool!.execute(
|
||||
const res2 = await invokeTool(
|
||||
readThreadTool!,
|
||||
'call-2',
|
||||
{ threadId: 'non-existent' },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
expect(res2).toBeDefined()
|
||||
const textContent = res2.content[0]
|
||||
@@ -1393,7 +1467,7 @@ describe('createAgentSession media generation tools injection', () => {
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
const tools = harness.getTools()
|
||||
const tools = await getAgentTools(harness)
|
||||
expect(tools.find((t) => t.name === 'generate_image')).toBeUndefined()
|
||||
expect(tools.find((t) => t.name === 'generate_video')).toBeUndefined()
|
||||
} finally {
|
||||
@@ -1467,7 +1541,7 @@ describe('createAgentSession media generation tools injection', () => {
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
const tools = harness.getTools()
|
||||
const tools = await getAgentTools(harness)
|
||||
expect(tools.find((t) => t.name === 'generate_image')).toBeDefined()
|
||||
expect(tools.find((t) => t.name === 'generate_video')).toBeDefined()
|
||||
} finally {
|
||||
@@ -1545,7 +1619,7 @@ describe('createAgentSession media generation tools injection', () => {
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
const tools = harness.getTools()
|
||||
const tools = await getAgentTools(harness)
|
||||
expect(tools.find((t) => t.name === 'generate_image')).toBeUndefined()
|
||||
expect(tools.find((t) => t.name === 'generate_video')).toBeDefined()
|
||||
} finally {
|
||||
|
||||
+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 { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { prisma, type Asset, type StorageKey } from '@shumai/db'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { toAgentTool } from '../operation-tools'
|
||||
import './download-asset'
|
||||
|
||||
@@ -96,7 +97,7 @@ describe('download_asset agent tool', () => {
|
||||
mockAssets(photo)
|
||||
mockStorage('image/webp', 'proxy-bytes')
|
||||
|
||||
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)
|
||||
@@ -130,7 +131,7 @@ describe('download_asset agent tool', () => {
|
||||
mockAssets(photo)
|
||||
mockStorage('image/jpeg')
|
||||
|
||||
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)
|
||||
@@ -153,7 +154,7 @@ 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,
|
||||
})
|
||||
@@ -173,7 +174,7 @@ describe('download_asset agent tool', () => {
|
||||
|
||||
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()
|
||||
@@ -185,36 +186,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()
|
||||
})
|
||||
|
||||
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 +224,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 +232,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' },
|
||||
@@ -246,7 +247,7 @@ describe('download_asset agent tool', () => {
|
||||
mockAssets({ id: 'ast-123' })
|
||||
mockStorage('image/webp', 'frame-bytes')
|
||||
|
||||
const result = await downloadTool().execute('call-1', {
|
||||
const result = await invokeTool(downloadTool(), 'call-1', {
|
||||
key: 'files/ast-123/screenshots/shot_5.0s.webp',
|
||||
})
|
||||
|
||||
@@ -279,7 +280,7 @@ describe('download_asset agent tool', () => {
|
||||
await fs.promises.writeFile(dest, pngHeader)
|
||||
})
|
||||
|
||||
const result = await downloadTool().execute('call-1', {
|
||||
const result = await invokeTool(downloadTool(), 'call-1', {
|
||||
key: 'files/upload-ulid-456/photo.png',
|
||||
})
|
||||
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
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 { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { readFileMimeType } 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 fs from 'fs'
|
||||
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 { resolveAssetIdFromKey } from '../tools/download-asset'
|
||||
import { registerAgentToolExtension, type AgentToolExtension } from '../operation-tools'
|
||||
import { resolveAssetIdFromKey } from '../tools/download-asset'
|
||||
|
||||
const CLEANUP_REMINDER = 'Remember to delete these temporary files when finished.'
|
||||
|
||||
@@ -47,7 +47,7 @@ async function saveToSandbox(key: string, filename: string) {
|
||||
}
|
||||
|
||||
/** 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, userId: string): Promise<AgentToolResult> {
|
||||
const owningAssetId = await resolveAssetIdFromKey(key)
|
||||
await authzService.hasPermission({
|
||||
user: { id: userId } as User,
|
||||
@@ -59,7 +59,7 @@ async function downloadKey(key: string, userId: string): Promise<AgentToolResult
|
||||
const filename = sanitizeFilename(path.basename(key)) || 'downloaded_file'
|
||||
const saved = await saveToSandbox(key, filename)
|
||||
return {
|
||||
content: [
|
||||
output: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Downloaded storage key "${key}" to "${saved.filePath}". Remember to delete this temporary file when finished.`,
|
||||
@@ -70,9 +70,7 @@ async function downloadKey(key: string, userId: string): Promise<AgentToolResult
|
||||
}
|
||||
|
||||
/** Saves each file of a download_asset result into the sandbox. */
|
||||
export async function presentDownloadOutput(
|
||||
output: DownloadAssetOutput,
|
||||
): Promise<AgentToolResult<unknown>> {
|
||||
export async function presentDownloadOutput(output: DownloadAssetOutput): Promise<AgentToolResult> {
|
||||
const lines: string[] = []
|
||||
const files = []
|
||||
for (const file of output.files) {
|
||||
@@ -87,7 +85,7 @@ 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 = {
|
||||
|
||||
@@ -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,10 +138,10 @@ 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) => {
|
||||
execute: async (params) => {
|
||||
const { input, extra } = splitAgentParams(op, readAssetExtension, params)
|
||||
const parsed = readAsset.input.safeParse(input)
|
||||
if (!parsed.success) {
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
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 { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { AuditAction, type AssetInfo } from '@shumai/dtos'
|
||||
import * as fs from 'fs'
|
||||
import * as os from 'os'
|
||||
import * as path from 'path'
|
||||
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'
|
||||
@@ -106,7 +107,7 @@ describe('create_file and create_version agent tools', () => {
|
||||
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', {
|
||||
const result = await invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: filePath,
|
||||
} as never)
|
||||
@@ -155,7 +156,7 @@ describe('create_file and create_version agent tools', () => {
|
||||
})
|
||||
|
||||
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 +173,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 +186,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',
|
||||
@@ -200,7 +201,7 @@ 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,
|
||||
metadata: { prompt: { newOption: { value: 'Kling' } } },
|
||||
@@ -214,7 +215,7 @@ 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,
|
||||
metadata: null,
|
||||
@@ -227,7 +228,7 @@ describe('create_file and create_version agent tools', () => {
|
||||
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,
|
||||
metadata: { status: 'approved' },
|
||||
@@ -240,10 +241,10 @@ describe('create_file and create_version agent tools', () => {
|
||||
|
||||
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,13 +256,13 @@ 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 () => {
|
||||
await expect(
|
||||
tool('create_file').execute('call-1', {
|
||||
invokeTool(tool('create_file'), 'call-1', {
|
||||
parent: 'folder-1',
|
||||
path: '/nonexistent/file.md',
|
||||
} as never),
|
||||
@@ -277,10 +278,10 @@ describe('create_file and create_version agent tools', () => {
|
||||
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: filePath } 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',
|
||||
@@ -306,7 +307,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' }),
|
||||
)
|
||||
@@ -324,7 +325,7 @@ describe('create_file and create_version agent tools', () => {
|
||||
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', {
|
||||
const result = await invokeTool(tool('create_version'), 'call-1', {
|
||||
parent: 'file-1',
|
||||
path: filePath,
|
||||
} as never)
|
||||
@@ -349,7 +350,7 @@ 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,
|
||||
metadata: { prompt: 'Generated using gemini' },
|
||||
@@ -367,7 +368,7 @@ describe('create_file and create_version agent tools', () => {
|
||||
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: filePath } as never),
|
||||
).rejects.toThrow('User is not a member of the team')
|
||||
expect(s3Service.putObject).not.toHaveBeenCalled()
|
||||
expect(assetService.createVersion).not.toHaveBeenCalled()
|
||||
@@ -375,7 +376,7 @@ describe('create_file and create_version agent tools', () => {
|
||||
|
||||
it('fails when the local file does not exist', async () => {
|
||||
await expect(
|
||||
tool('create_version').execute('call-1', {
|
||||
invokeTool(tool('create_version'), 'call-1', {
|
||||
parent: 'file-1',
|
||||
path: '/nonexistent/file.md',
|
||||
} as never),
|
||||
|
||||
@@ -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 { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { getFileMimeType, readFileMimeType } from '@shumai/core/src/utils/file-mime'
|
||||
import { createFile, type StartedUploadOutput } from '@shumai/operations'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { Type } from 'typebox'
|
||||
import { z } from 'zod'
|
||||
import {
|
||||
registerAgentToolExtension,
|
||||
type AgentOperationContext,
|
||||
@@ -48,7 +48,7 @@ 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')
|
||||
|
||||
@@ -79,7 +79,7 @@ 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 = {
|
||||
|
||||
@@ -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,14 +1,17 @@
|
||||
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'>
|
||||
@@ -60,12 +63,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>()
|
||||
@@ -135,10 +138,9 @@ 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) => {
|
||||
execute: async (params) => {
|
||||
const { input, extra } = splitAgentParams(op, extension, params)
|
||||
if (extension?.intercept) {
|
||||
const handled = await extension.intercept(input as Record<string, unknown>, extra, ctx)
|
||||
@@ -149,7 +151,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: {},
|
||||
}
|
||||
},
|
||||
|
||||
@@ -0,0 +1,664 @@
|
||||
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 { sandboxService } from '@shumai/core'
|
||||
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 { prisma } from '@shumai/db'
|
||||
import { DEFAULT_DENIED_TOOLS } from '@shumai/dtos'
|
||||
import * as fs from 'fs'
|
||||
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 { createSandboxedBashTool } from './tools/sandboxed-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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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 {
|
||||
const agentDir = path.join(process.cwd(), '.pi', 'agents', agentId)
|
||||
if (!fs.existsSync(agentDir)) fs.mkdirSync(agentDir, { recursive: true })
|
||||
|
||||
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 (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
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
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 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'
|
||||
) {
|
||||
skillLoadedInSession = true
|
||||
onEnvsAdded(await agentService.getSkillEnvs(details.skillId))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} 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 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).
|
||||
const registry = createRegistry()
|
||||
const bashIncludedFromStart = isOwner || skillLoadedInSession
|
||||
let bashInjected = bashIncludedFromStart
|
||||
const onSkillLoaded = async () => {
|
||||
if (bashInjected || deniedTools.includes('bash')) return
|
||||
bashInjected = true
|
||||
registry.install(
|
||||
defineExtension({ name: 'shumai.bash', tools: [wrapToolWithQuota(sandboxedBash)] }),
|
||||
)
|
||||
await session?.configure(
|
||||
{
|
||||
tools: [...enabledTools, wrapToolWithQuota(sandboxedBash)],
|
||||
modelTools: { add: [sandboxedBash] },
|
||||
},
|
||||
piContext,
|
||||
)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
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 (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
|
||||
}
|
||||
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 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')
|
||||
}
|
||||
@@ -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,15 @@
|
||||
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 { 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[] = [
|
||||
@@ -60,7 +61,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
|
||||
@@ -78,7 +78,8 @@ describe('generate_image tool', () => {
|
||||
})
|
||||
const writeSpy = vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
|
||||
|
||||
const result = await tool.execute(
|
||||
const result = await invokeTool(
|
||||
tool,
|
||||
'call-1',
|
||||
{
|
||||
prompt: 'A futuristic city skyline at dusk',
|
||||
@@ -129,7 +130,8 @@ describe('generate_image tool', () => {
|
||||
})
|
||||
|
||||
// Invoke azure:dall-e-3
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-azure',
|
||||
{
|
||||
prompt: 'Azure generation',
|
||||
@@ -153,7 +155,8 @@ describe('generate_image tool', () => {
|
||||
)
|
||||
|
||||
// Invoke openai:dall-e-3
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-openai',
|
||||
{
|
||||
prompt: 'OpenAI generation',
|
||||
@@ -186,7 +189,8 @@ describe('generate_image tool', () => {
|
||||
mimeType: 'image/png',
|
||||
})
|
||||
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-2',
|
||||
{
|
||||
prompt: 'A cute cat',
|
||||
@@ -224,7 +228,8 @@ describe('generate_image tool', () => {
|
||||
mimeType: 'image/png',
|
||||
}))
|
||||
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-3',
|
||||
{
|
||||
prompt: 'Transform into watercolor',
|
||||
@@ -257,7 +262,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 +284,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 { mediaGenerationService } from '@shumai/core/src/media-generation/media-generation'
|
||||
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
||||
import { Type, type Static, type TSchema } from 'typebox'
|
||||
import { EnabledMediaModel } from '@shumai/dtos'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { Type, type Static, type TSchema } from 'typebox'
|
||||
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}`
|
||||
@@ -161,7 +161,7 @@ export function createGenerateImageTool(
|
||||
fs.writeFileSync(targetPath, buffer)
|
||||
|
||||
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.`,
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
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 { 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[] = [
|
||||
@@ -60,7 +61,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
|
||||
@@ -78,7 +78,8 @@ describe('generate_video tool', () => {
|
||||
})
|
||||
const writeSpy = vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
|
||||
|
||||
const result = await tool.execute(
|
||||
const result = await invokeTool(
|
||||
tool,
|
||||
'call-vid-1',
|
||||
{
|
||||
mode: 'text_to_video',
|
||||
@@ -140,7 +141,8 @@ describe('generate_video tool', () => {
|
||||
})
|
||||
|
||||
// Select fal:kling-v1-standard
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-fal',
|
||||
{
|
||||
mode: 'text_to_video',
|
||||
@@ -169,7 +171,8 @@ describe('generate_video tool', () => {
|
||||
)
|
||||
|
||||
// Select klingai:kling-v1-standard
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-klingai',
|
||||
{
|
||||
mode: 'text_to_video',
|
||||
@@ -211,7 +214,8 @@ describe('generate_video tool', () => {
|
||||
mimeType: 'image/png',
|
||||
})
|
||||
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-vid-2',
|
||||
{
|
||||
mode: 'image_to_video',
|
||||
@@ -257,7 +261,8 @@ describe('generate_video tool', () => {
|
||||
mimeType: 'image/png',
|
||||
}))
|
||||
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-vid-3',
|
||||
{
|
||||
mode: 'first_last_frame',
|
||||
@@ -304,7 +309,8 @@ describe('generate_video tool', () => {
|
||||
mimeType: 'image/png',
|
||||
}))
|
||||
|
||||
await tool.execute(
|
||||
await invokeTool(
|
||||
tool,
|
||||
'call-vid-4',
|
||||
{
|
||||
mode: 'reference_to_video',
|
||||
@@ -343,7 +349,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 { mediaGenerationService } from '@shumai/core/src/media-generation/media-generation'
|
||||
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
||||
import { Type, type Static, type TSchema } from 'typebox'
|
||||
import { EnabledMediaModel } from '@shumai/dtos'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { Type, type Static, type TSchema } from 'typebox'
|
||||
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}`
|
||||
@@ -280,7 +280,7 @@ export function createGenerateVideoTool(
|
||||
fs.writeFileSync(targetPath, buffer)
|
||||
|
||||
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.`,
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
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'
|
||||
|
||||
describe('readSkillTool', () => {
|
||||
setupTestDbHooks()
|
||||
@@ -21,7 +22,7 @@ describe('readSkillTool', () => {
|
||||
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')
|
||||
})
|
||||
|
||||
@@ -45,7 +46,7 @@ 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('# Test Skill Content')
|
||||
@@ -97,7 +98,7 @@ 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')
|
||||
@@ -144,7 +145,7 @@ describe('readSkillTool', () => {
|
||||
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',
|
||||
@@ -179,7 +180,7 @@ describe('readSkillTool', () => {
|
||||
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',
|
||||
@@ -214,7 +215,7 @@ describe('readSkillTool', () => {
|
||||
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',
|
||||
@@ -249,7 +250,7 @@ describe('readSkillTool', () => {
|
||||
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',
|
||||
@@ -293,7 +294,8 @@ describe('readSkillTool', () => {
|
||||
})
|
||||
|
||||
const ownerTool = createReadSkillTool(ownerUser.id, () => {})
|
||||
const resultOwner = await ownerTool.execute(
|
||||
const resultOwner = await invokeTool(
|
||||
ownerTool,
|
||||
'1',
|
||||
{ skillId: ownerSkill.id },
|
||||
undefined,
|
||||
@@ -304,7 +306,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 +342,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
|
||||
@@ -369,7 +371,8 @@ describe('readSkillTool', () => {
|
||||
undefined,
|
||||
project.id,
|
||||
)
|
||||
const result = await projEditorTool.execute(
|
||||
const result = await invokeTool(
|
||||
projEditorTool,
|
||||
'1',
|
||||
{ skillId: editorSkill.id },
|
||||
undefined,
|
||||
@@ -402,7 +405,7 @@ describe('readSkillTool', () => {
|
||||
|
||||
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)
|
||||
})
|
||||
@@ -428,7 +431,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 +454,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')
|
||||
})
|
||||
|
||||
@@ -475,7 +478,13 @@ describe('readSkillTool', () => {
|
||||
})
|
||||
|
||||
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')
|
||||
})
|
||||
@@ -500,7 +509,13 @@ 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('# 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' }),
|
||||
@@ -22,10 +22,10 @@ export const createReadSkillTool = (
|
||||
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 },
|
||||
})
|
||||
@@ -71,7 +71,7 @@ export const createReadSkillTool = (
|
||||
await onSkillLoaded?.()
|
||||
|
||||
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 },
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { createSandboxedBashTool } from './sandboxed-bash'
|
||||
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
|
||||
import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
|
||||
import { spawn } from 'node:child_process'
|
||||
import { EventEmitter } from 'node:events'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createSandboxedBashTool } from './sandboxed-bash'
|
||||
|
||||
vi.mock('@anthropic-ai/sandbox-runtime')
|
||||
vi.mock('node:child_process')
|
||||
@@ -57,7 +58,8 @@ describe('createSandboxedBashTool', () => {
|
||||
const signal = createMockSignal()
|
||||
|
||||
// pi-coding-agent AgentTool.execute signature: (toolCallId, params, signal?, onUpdate?)
|
||||
const executePromise = tool.execute(
|
||||
const executePromise = invokeTool(
|
||||
tool,
|
||||
'1',
|
||||
{ command: 'echo hello', source: 'user' },
|
||||
signal,
|
||||
@@ -97,7 +99,7 @@ describe('createSandboxedBashTool', () => {
|
||||
|
||||
const signal = createMockSignal()
|
||||
await expect(
|
||||
tool.execute('1', { command: 'echo hack', source: 'user' }, signal, createMockOnUpdate()),
|
||||
invokeTool(tool, '1', { command: 'echo hack', source: 'user' }, signal, createMockOnUpdate()),
|
||||
).rejects.toThrow(/not permitted for your role/)
|
||||
|
||||
expect(spawn).not.toHaveBeenCalled()
|
||||
@@ -119,7 +121,7 @@ describe('createSandboxedBashTool', () => {
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute('1', { command: 'echo skill', source: 'skill' }, signal)
|
||||
const executePromise = invokeTool(tool, '1', { command: 'echo skill', source: 'skill' }, signal)
|
||||
|
||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||
expect(SandboxManager.wrapWithSandbox).toHaveBeenCalledWith(
|
||||
@@ -145,7 +147,8 @@ describe('createSandboxedBashTool', () => {
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute(
|
||||
const executePromise = invokeTool(
|
||||
tool,
|
||||
'1',
|
||||
{ command: 'fail', source: 'user' },
|
||||
signal,
|
||||
@@ -169,7 +172,8 @@ describe('createSandboxedBashTool', () => {
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute(
|
||||
const executePromise = invokeTool(
|
||||
tool,
|
||||
'1',
|
||||
{ command: 'exit 1', source: 'user' },
|
||||
signal,
|
||||
@@ -198,7 +202,8 @@ describe('createSandboxedBashTool', () => {
|
||||
const killSpy = vi.spyOn(process, 'kill').mockImplementation(() => true as any)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute(
|
||||
const executePromise = invokeTool(
|
||||
tool,
|
||||
'1',
|
||||
{ command: 'sleep 10', timeout: 2, source: 'user' },
|
||||
signal,
|
||||
@@ -232,7 +237,8 @@ describe('createSandboxedBashTool', () => {
|
||||
const killSpy = vi.spyOn(process, 'kill').mockImplementation(() => true as any)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute(
|
||||
const executePromise = invokeTool(
|
||||
tool,
|
||||
'1',
|
||||
{ command: 'long-run', source: 'user' },
|
||||
signal,
|
||||
@@ -266,7 +272,12 @@ describe('createSandboxedBashTool', () => {
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute('1', { command: 'generate lines', source: 'user' }, signal)
|
||||
const executePromise = invokeTool(
|
||||
tool,
|
||||
'1',
|
||||
{ command: 'generate lines', source: 'user' },
|
||||
signal,
|
||||
)
|
||||
|
||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||
|
||||
@@ -308,7 +319,8 @@ describe('createSandboxedBashTool', () => {
|
||||
;(spawn as any).mockReturnValue(mockChild)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute(
|
||||
const executePromise = invokeTool(
|
||||
tool,
|
||||
'1',
|
||||
{ command: 'long single line', source: 'user' },
|
||||
signal,
|
||||
@@ -342,7 +354,8 @@ describe('createSandboxedBashTool', () => {
|
||||
const killSpy = vi.spyOn(process, 'kill').mockImplementation(() => true as any)
|
||||
|
||||
const signal = createMockSignal()
|
||||
const executePromise = tool.execute(
|
||||
const executePromise = invokeTool(
|
||||
tool,
|
||||
'1',
|
||||
{ command: 'slow', timeout: 1, source: 'user' },
|
||||
signal,
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
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 { sandboxService } from '@shumai/core/src/sandbox/sandbox-service'
|
||||
import { type AgentTool, type AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import { Type } from 'typebox'
|
||||
import type { TeamMemberRole } from '@shumai/db'
|
||||
import { spawn } from 'node:child_process'
|
||||
import { Type } from 'typebox'
|
||||
import { OutputAccumulator } from '../utils/output-accumulator'
|
||||
import {
|
||||
DEFAULT_MAX_BYTES,
|
||||
@@ -10,8 +15,6 @@ import {
|
||||
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'],
|
||||
@@ -28,7 +31,7 @@ const BashParameters = Type.Object({
|
||||
source: BashSource,
|
||||
})
|
||||
|
||||
export interface BashDetails {
|
||||
export type BashDetails = {
|
||||
exitCode: number | null
|
||||
truncation?: TruncationResult
|
||||
fullOutputPath?: string
|
||||
@@ -47,8 +50,6 @@ export interface SandboxedBashOptions {
|
||||
restrictedUser?: boolean
|
||||
}
|
||||
|
||||
const BASH_UPDATE_THROTTLE_MS = 100
|
||||
|
||||
export const createSandboxedBashTool = (
|
||||
cwd: string,
|
||||
skillEnvs: Record<string, string> = {},
|
||||
@@ -57,14 +58,15 @@ export const createSandboxedBashTool = (
|
||||
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,
|
||||
outputLimits: { maxBytes: DEFAULT_MAX_BYTES, maxLines: DEFAULT_MAX_LINES, retain: 'tail' },
|
||||
execute: async (
|
||||
toolCallId,
|
||||
{ command, timeout, source },
|
||||
signal,
|
||||
onUpdate,
|
||||
api,
|
||||
context,
|
||||
): Promise<AgentToolResult<BashDetails>> => {
|
||||
const toolCallId = api.callId
|
||||
const signal = context.abortSignal
|
||||
if (options?.restrictedUser && source !== 'skill') {
|
||||
throw new Error(
|
||||
'Bash commands requested directly by the user (source="user") are not permitted for your role. ' +
|
||||
@@ -88,62 +90,17 @@ export const createSandboxedBashTool = (
|
||||
|
||||
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 } })
|
||||
}
|
||||
await api.details({ exitCode: null }, context)
|
||||
|
||||
const handleData = (data: Buffer) => {
|
||||
if (!acceptingOutput) return
|
||||
output.append(data)
|
||||
scheduleOutputUpdate()
|
||||
api.output(data)
|
||||
}
|
||||
|
||||
const finishOutput = async () => {
|
||||
acceptingOutput = false
|
||||
output.finish()
|
||||
clearUpdateTimer()
|
||||
emitOutputUpdate()
|
||||
const snapshot = output.snapshot({ persistIfTruncated: true })
|
||||
await output.closeTempFile()
|
||||
return snapshot
|
||||
@@ -284,7 +241,7 @@ export const createSandboxedBashTool = (
|
||||
)
|
||||
} else {
|
||||
resolve({
|
||||
content: [
|
||||
output: [
|
||||
{
|
||||
type: 'text',
|
||||
text: outputText,
|
||||
|
||||
@@ -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.6.6",
|
||||
"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:*",
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import type { EntryRecord } from '@earendil-works/pi-durable'
|
||||
import type { TypedResponse } from 'hono'
|
||||
|
||||
/** Pi records are already JSON; retain their public types without recursively remapping SDK schemas. */
|
||||
export function agentEntriesResponse(
|
||||
entries: EntryRecord[],
|
||||
): Response & TypedResponse<EntryRecord[], 200, 'json'> {
|
||||
return new Response(JSON.stringify(entries), {
|
||||
headers: { 'Content-Type': 'application/json; charset=UTF-8' },
|
||||
}) as Response & TypedResponse<EntryRecord[], 200, 'json'>
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { ROOT_CONVERSATION_ID, type EntryId, type EntryRecord } from '@earendil-works/pi-durable'
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
@@ -352,15 +353,15 @@ describe('Agent API', () => {
|
||||
|
||||
describe('GET /agent-sessions/:sessionId/entries', () => {
|
||||
it('returns entries of agent session if user is admin', async () => {
|
||||
const mockEntries = [
|
||||
const mockEntries: EntryRecord[] = [
|
||||
{
|
||||
id: 'entry1',
|
||||
sessionId: 'session1',
|
||||
entry: { step: 1, message: 'Step 1' },
|
||||
id: 7 as EntryId,
|
||||
conversationId: ROOT_CONVERSATION_ID,
|
||||
kind: 'pi.user',
|
||||
model: [{ role: 'user', content: 'Step 1', timestamp: 1 }],
|
||||
},
|
||||
]
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
vi.mocked(agentService.getSessionEntries).mockResolvedValue(mockEntries as any)
|
||||
vi.mocked(agentService.getSessionEntries).mockResolvedValue(mockEntries)
|
||||
|
||||
const res = await app.request('/agent-sessions/session1/entries', {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -369,8 +370,9 @@ describe('Agent API', () => {
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data).toHaveLength(1)
|
||||
expect(data[0].id).toBe('entry1')
|
||||
expect(data[0].entry.step).toBe(1)
|
||||
expect(data[0].id).toBe(7)
|
||||
expect(data[0].kind).toBe('pi.user')
|
||||
expect(data[0].model[0].content).toBe('Step 1')
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: undefined,
|
||||
permission: 'Admin',
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
import { Hono } from 'hono'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import {
|
||||
createAgentRequestSchema,
|
||||
updateAgentRequestSchema,
|
||||
updateAgentPermissionRequestSchema,
|
||||
paginationParamsSchema,
|
||||
AgentInfo,
|
||||
AuditAction,
|
||||
createAgentRequestSchema,
|
||||
paginationParamsSchema,
|
||||
updateAgentPermissionRequestSchema,
|
||||
updateAgentRequestSchema,
|
||||
} from '@shumai/dtos'
|
||||
import { Hono } from 'hono'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { agentEntriesResponse } from './agent-entries'
|
||||
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
|
||||
@@ -219,13 +220,7 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
})
|
||||
|
||||
const entries = await agentService.getSessionEntries({ sessionId })
|
||||
const infos = entries.map((entry) => ({
|
||||
id: entry.id,
|
||||
sessionId: entry.sessionId,
|
||||
entry: entry.entry,
|
||||
}))
|
||||
|
||||
return c.json(infos, 200)
|
||||
return agentEntriesResponse(entries)
|
||||
})
|
||||
.get('/teams/:teamId/agent-sessions', zValidator('query', paginationParamsSchema), async (c) => {
|
||||
const teamId = c.req.param('teamId')
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { describe, expect, it, vi, beforeEach } from 'vitest'
|
||||
import { Hono } from 'hono'
|
||||
import chatRoute from './chat'
|
||||
import { chatService } from '@shumai/core/src/chat/chat'
|
||||
import { authzService } from '@shumai/core/src/authz/authz'
|
||||
import type { ChatMessage } from '@shumai/dtos'
|
||||
import { chatService } from '@shumai/core/src/chat/chat'
|
||||
import { ROOT_CONVERSATION_ID, type EntryId, type EntryRecord } from '@earendil-works/pi-durable'
|
||||
import { Hono } from 'hono'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import chatRoute from './chat'
|
||||
|
||||
vi.mock('@shumai/core/src/chat/chat', async (importOriginal) => {
|
||||
const original = await importOriginal<typeof import('@shumai/core/src/chat/chat')>()
|
||||
@@ -65,23 +65,22 @@ describe('Chat API', () => {
|
||||
|
||||
describe('GET /teams/:teamId/chat/sessions/:sessionId/messages', () => {
|
||||
it('returns list of messages', async () => {
|
||||
const mockMessages = [
|
||||
const mockMessages: EntryRecord[] = [
|
||||
{
|
||||
id: 'msg1',
|
||||
role: 'user',
|
||||
content: 'hello',
|
||||
timestamp: '2026-07-07T10:00:00Z',
|
||||
entry: {},
|
||||
id: 7 as EntryId,
|
||||
conversationId: ROOT_CONVERSATION_ID,
|
||||
kind: 'pi.user',
|
||||
model: [{ role: 'user', content: 'hello', timestamp: 1 }],
|
||||
},
|
||||
]
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
vi.mocked(chatService.listMessages).mockResolvedValue(mockMessages as any)
|
||||
vi.mocked(chatService.listMessages).mockResolvedValue(mockMessages)
|
||||
|
||||
const res = await app.request('/teams/team1/chat/sessions/session1/messages')
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data).toHaveLength(1)
|
||||
expect(data[0].content).toBe('hello')
|
||||
expect(data[0].model[0].content).toBe('hello')
|
||||
expect(data[0].kind).toBe('pi.user')
|
||||
expect(chatService.listMessages).toHaveBeenCalledWith('user1', 'team1', 'session1')
|
||||
})
|
||||
})
|
||||
@@ -109,16 +108,17 @@ describe('Chat API', () => {
|
||||
.mockResolvedValueOnce({
|
||||
messages: [
|
||||
{
|
||||
id: 'entry1',
|
||||
role: 'user',
|
||||
content: 'hi',
|
||||
} as unknown as ChatMessage,
|
||||
id: 7 as EntryId,
|
||||
conversationId: ROOT_CONVERSATION_ID,
|
||||
kind: 'pi.user',
|
||||
model: [{ role: 'user', content: 'hi', timestamp: 1 }],
|
||||
},
|
||||
],
|
||||
lastEntryId: 'entry1',
|
||||
lastEntryId: '7',
|
||||
})
|
||||
.mockResolvedValue({
|
||||
messages: [],
|
||||
lastEntryId: 'entry1',
|
||||
lastEntryId: '7',
|
||||
})
|
||||
|
||||
vi.mocked(chatService.getChatWorkflowStatus).mockResolvedValue({
|
||||
@@ -140,7 +140,8 @@ describe('Chat API', () => {
|
||||
|
||||
const text = await res.text()
|
||||
expect(text).toContain('data:')
|
||||
expect(text).toContain('entry1')
|
||||
expect(text).toContain('"id":7')
|
||||
expect(text).toContain('"kind":"pi.user"')
|
||||
expect(chatService.startOrContinueChat).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ id: 'user1' }),
|
||||
'team1',
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { Hono } from 'hono'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { streamSSE } from 'hono/streaming'
|
||||
import { chatService } from '@shumai/core/src/chat/chat'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { chatRequestSchema, paginationParamsSchema } from '@shumai/dtos'
|
||||
import { chatService } from '@shumai/core/src/chat/chat'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import { chatRequestSchema, paginationParamsSchema } from '@shumai/dtos'
|
||||
import { Hono } from 'hono'
|
||||
import { streamSSE } from 'hono/streaming'
|
||||
import { agentEntriesResponse } from './agent-entries'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
|
||||
@@ -37,7 +38,7 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
})
|
||||
|
||||
const res = await chatService.listMessages(user.id, teamId, sessionId)
|
||||
return c.json(res, 200)
|
||||
return agentEntriesResponse(res)
|
||||
})
|
||||
.delete('/teams/:teamId/chat/sessions/:sessionId', async (c) => {
|
||||
const user = c.get('user')
|
||||
|
||||
@@ -82,6 +82,34 @@ describe('MCP server and /api/ops (integration)', () => {
|
||||
expect(audit).toMatchObject({ userId: owner.id, teamId, projectId, via: 'mcp' })
|
||||
})
|
||||
|
||||
it('lets an MCP client create a project without specifying teamId and create a folder in it', async () => {
|
||||
const client = await connect(ownerToken)
|
||||
|
||||
const createdProject = await client.callTool({
|
||||
name: 'create_project',
|
||||
arguments: { name: 'MCP Created Project' },
|
||||
})
|
||||
expect(createdProject.isError).toBeFalsy()
|
||||
const created = createdProject.structuredContent as {
|
||||
id: string
|
||||
name: string
|
||||
teamId: string
|
||||
rootFolderId: string
|
||||
}
|
||||
expect(created).toMatchObject({
|
||||
id: expect.any(String),
|
||||
name: 'MCP Created Project',
|
||||
teamId,
|
||||
rootFolderId: expect.any(String),
|
||||
})
|
||||
|
||||
const createdFolder = await client.callTool({
|
||||
name: 'create_folder',
|
||||
arguments: { parent: created.rootFolderId, name: 'Inside New Project' },
|
||||
})
|
||||
expect(createdFolder.isError).toBeFalsy()
|
||||
})
|
||||
|
||||
it("enforces the token owner's permissions", async () => {
|
||||
const client = await connect(reviewerToken)
|
||||
|
||||
|
||||
@@ -59,6 +59,7 @@ describe('MCP server', () => {
|
||||
'create_comment',
|
||||
'create_file',
|
||||
'create_folder',
|
||||
'create_project',
|
||||
'create_version',
|
||||
'create_version_stack',
|
||||
'delete_asset',
|
||||
|
||||
@@ -5,7 +5,12 @@ import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/a
|
||||
import { projectService } from '@shumai/core/src/project/project'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import type { ProjectInfo } from '@shumai/dtos'
|
||||
import { copyAssetsOp, moveAssetsOp, runOperation } from '@shumai/core/src/operations'
|
||||
import {
|
||||
copyAssetsOp,
|
||||
createProjectOp,
|
||||
moveAssetsOp,
|
||||
runOperation,
|
||||
} from '@shumai/core/src/operations'
|
||||
|
||||
vi.mock('./middleware/auth', () => ({
|
||||
authMiddleware: async (
|
||||
@@ -98,12 +103,24 @@ describe('project api', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('POST /teams/:teamId/projects', async () => {
|
||||
vi.mocked(projectService.createProject).mockResolvedValue({
|
||||
it('POST /teams/:teamId/projects runs create_project and returns ProjectInfo', async () => {
|
||||
vi.mocked(runOperation).mockResolvedValue({
|
||||
id: 'foo',
|
||||
name: 'foo.png',
|
||||
teamId: 't',
|
||||
rootFolderId: 'root1',
|
||||
})
|
||||
const projectInfo: ProjectInfo = {
|
||||
id: 'foo',
|
||||
name: 'foo.png',
|
||||
teamId: 't',
|
||||
rootFolder: 'root1',
|
||||
coverImage: 'http://s3/bucket/key',
|
||||
} as any) // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
coverImageKey: 'key',
|
||||
enableNotification: true,
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
}
|
||||
vi.mocked(projectService.getProject).mockResolvedValue(projectInfo)
|
||||
|
||||
const res = await app.request('/teams/t/projects', {
|
||||
method: 'POST',
|
||||
@@ -113,15 +130,13 @@ describe('project api', () => {
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json()
|
||||
expect(json.id).toBe('foo')
|
||||
expect(json.name).toBe('foo.png')
|
||||
expect(json.coverImage).toBe('http://s3/bucket/key')
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: expect.anything(),
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Team,
|
||||
id: 't',
|
||||
})
|
||||
expect(json).toEqual(projectInfo)
|
||||
expect(runOperation).toHaveBeenCalledWith(
|
||||
createProjectOp,
|
||||
{ userId: 'user1', via: 'api' },
|
||||
{ teamId: 't', name: 'foo.png', coverImageKey: 'key', enableNotification: true },
|
||||
)
|
||||
expect(projectService.getProject).toHaveBeenCalledWith('foo')
|
||||
})
|
||||
|
||||
it('PUT /projects/:projectId', async () => {
|
||||
|
||||
+12
-21
@@ -18,7 +18,12 @@ import {
|
||||
import { listMembersQuerySchema, AuditAction } from '@shumai/dtos'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
import { copyAssetsOp, moveAssetsOp, runOperation } from '@shumai/core/src/operations'
|
||||
import {
|
||||
copyAssetsOp,
|
||||
createProjectOp,
|
||||
moveAssetsOp,
|
||||
runOperation,
|
||||
} from '@shumai/core/src/operations'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
|
||||
@@ -50,27 +55,13 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Team,
|
||||
id: teamId,
|
||||
})
|
||||
const newProject = await runOperation(
|
||||
createProjectOp,
|
||||
{ userId: user.id, via: 'api' },
|
||||
{ teamId, ...req },
|
||||
)
|
||||
|
||||
const newProject = await projectService.createProject(user, {
|
||||
teamId,
|
||||
...req,
|
||||
})
|
||||
|
||||
await auditLogService.logAction({
|
||||
action: AuditAction.project_create,
|
||||
teamId,
|
||||
userId: user.id,
|
||||
projectId: newProject.id,
|
||||
itemId: newProject.id,
|
||||
})
|
||||
|
||||
return c.json(newProject)
|
||||
return c.json(await projectService.getProject(newProject.id))
|
||||
})
|
||||
.put('/projects/:projectId', zValidator('json', updateProjectRequestSchema), async (c) => {
|
||||
const projectId = c.req.param('projectId')
|
||||
|
||||
@@ -77,7 +77,6 @@ vi.mock('@shumai/core/src/workflowTask/workflowTask', () => ({
|
||||
|
||||
describe('team api', () => {
|
||||
const app = new Hono().use('*', authMiddleware).route('/', teamRoute)
|
||||
let mockCreateTeam: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockGetUserTeams: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockGetMe: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockGetTeamMembers: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
@@ -90,7 +89,6 @@ describe('team api', () => {
|
||||
let mockUpdateMe: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
|
||||
beforeEach(() => {
|
||||
mockCreateTeam = vi.spyOn(teamService, 'createTeam')
|
||||
mockGetUserTeams = vi.spyOn(teamService, 'getUserTeams')
|
||||
mockGetMe = vi.spyOn(teamService, 'getMe')
|
||||
mockGetTeamMembers = vi.spyOn(teamService, 'getTeamMembers')
|
||||
@@ -108,9 +106,13 @@ describe('team api', () => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('POST /teams creates a new team', async () => {
|
||||
const mockTeam = { id: 't1', name: 'New Team', updatedAt: new Date() }
|
||||
mockCreateTeam.mockResolvedValue(mockTeam)
|
||||
it('POST /teams returns 404 without creating a team or writing an audit entry', async () => {
|
||||
const createTeam = vi.spyOn(teamService, 'createTeam').mockResolvedValue({
|
||||
id: 't1',
|
||||
name: 'New Team',
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
const { auditLogService } = await import('@shumai/core/src/auditLog/auditLog')
|
||||
|
||||
const res = await app.request('/teams', {
|
||||
method: 'POST',
|
||||
@@ -120,13 +122,9 @@ describe('team api', () => {
|
||||
}),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data.id).toBe('t1')
|
||||
expect(mockCreateTeam).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ id: 'user1' }),
|
||||
expect.objectContaining({ name: 'New Team' }),
|
||||
)
|
||||
expect(res.status).toBe(404)
|
||||
expect(createTeam).not.toHaveBeenCalled()
|
||||
expect(auditLogService.logAction).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('GET /teams returns user teams', async () => {
|
||||
|
||||
@@ -4,7 +4,6 @@ import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/a
|
||||
import { teamService } from '@shumai/core/src/team/team'
|
||||
import { userMetadataService } from '@shumai/core/src/user-metadata/user-metadata'
|
||||
import {
|
||||
createTeamRequestSchema,
|
||||
getUserTeamsRequestSchema,
|
||||
updateTeamSettingsRequestSchema,
|
||||
listMembersQuerySchema,
|
||||
@@ -35,19 +34,6 @@ import type { Prisma } from '@shumai/db'
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
|
||||
const route = new Hono<{ Variables: { user: User } }>()
|
||||
.post('/teams', zValidator('json', createTeamRequestSchema), async (c) => {
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
const newTeam = await teamService.createTeam(user, req)
|
||||
await auditLogService.logAction({
|
||||
action: AuditAction.team_create,
|
||||
teamId: newTeam.id,
|
||||
userId: user.id,
|
||||
itemId: newTeam.id,
|
||||
})
|
||||
return c.json(newTeam)
|
||||
})
|
||||
.get('/teams', zValidator('query', getUserTeamsRequestSchema), async (c) => {
|
||||
const user = c.get('user')
|
||||
const pg = c.req.valid('query')
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/core",
|
||||
"version": "0.6.5",
|
||||
"version": "0.6.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
@@ -29,8 +29,9 @@
|
||||
"@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:*",
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { describe, expect, test } from 'vitest'
|
||||
import { AgentService } from './agent'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { describe, expect, test } from 'vitest'
|
||||
import { AgentService } from './agent'
|
||||
import { appendSessionEntries } from './session-test-utils'
|
||||
|
||||
describe('AgentService', () => {
|
||||
setupTestDbHooks()
|
||||
@@ -477,56 +478,23 @@ describe('AgentService', () => {
|
||||
expect(agents[0].modelRef).toBeDefined()
|
||||
})
|
||||
|
||||
test('getSessionEntries follows parentId across shared sessions', async () => {
|
||||
const db = prisma
|
||||
const svc = new AgentService()
|
||||
const { agent } = await setupTestData(db)
|
||||
|
||||
const mainSession = await db.agentSession.create({
|
||||
data: {
|
||||
agentId: agent.id,
|
||||
type: 'comment',
|
||||
cwd: '/tmp',
|
||||
},
|
||||
test('getSessionEntries reads independent native session transcripts', async () => {
|
||||
const { agent } = await setupTestData(prisma)
|
||||
const main = await prisma.agentSession.create({
|
||||
data: { agentId: agent.id, type: 'comment', cwd: '/tmp' },
|
||||
})
|
||||
|
||||
const threadSession = await db.agentSession.create({
|
||||
data: {
|
||||
agentId: agent.id,
|
||||
type: 'comment',
|
||||
cwd: '/tmp',
|
||||
},
|
||||
const thread = await prisma.agentSession.create({
|
||||
data: { agentId: agent.id, type: 'comment', cwd: '/tmp' },
|
||||
})
|
||||
|
||||
const entry1 = await db.agentSessionEntry.create({
|
||||
data: {
|
||||
id: '01ENTRY0000000000000000001',
|
||||
sessionId: mainSession.id,
|
||||
type: 'message',
|
||||
parentId: null,
|
||||
data: { text: 'Main Entry 1' },
|
||||
},
|
||||
})
|
||||
|
||||
const entry2 = await db.agentSessionEntry.create({
|
||||
data: {
|
||||
id: '01ENTRY0000000000000000002',
|
||||
sessionId: threadSession.id,
|
||||
type: 'message',
|
||||
parentId: entry1.id,
|
||||
data: { text: 'Thread Entry 2' },
|
||||
},
|
||||
})
|
||||
|
||||
await db.agentSession.update({
|
||||
where: { id: threadSession.id },
|
||||
data: { leafId: entry2.id },
|
||||
})
|
||||
|
||||
const entries = await svc.getSessionEntries({ sessionId: threadSession.id })
|
||||
expect(entries).toHaveLength(2)
|
||||
expect(entries[0].id).toBe(entry1.id)
|
||||
expect(entries[1].id).toBe(entry2.id)
|
||||
await appendSessionEntries(main.id, [
|
||||
{ kind: 'pi.user', model: [{ role: 'user', content: 'main', timestamp: 1 }] },
|
||||
])
|
||||
const records = await appendSessionEntries(thread.id, [
|
||||
{ kind: 'pi.user', model: [{ role: 'user', content: 'copied context', timestamp: 1 }] },
|
||||
{ kind: 'pi.user', model: [{ role: 'user', content: 'thread', timestamp: 2 }] },
|
||||
])
|
||||
await prisma.agentSession.delete({ where: { id: main.id } })
|
||||
expect(await new AgentService().getSessionEntries({ sessionId: thread.id })).toEqual(records)
|
||||
})
|
||||
|
||||
test('getSessionEntries dynamically attaches details.thread on custom_message entries with replies', async () => {
|
||||
@@ -577,34 +545,23 @@ describe('AgentService', () => {
|
||||
},
|
||||
})
|
||||
|
||||
const entry = await db.agentSessionEntry.create({
|
||||
data: {
|
||||
id: rootComment.id,
|
||||
sessionId: session.id,
|
||||
type: 'custom_message',
|
||||
parentId: null,
|
||||
await appendSessionEntries(session.id, [
|
||||
{
|
||||
kind: 'shumai.message',
|
||||
data: {
|
||||
customType: 'shumai_message',
|
||||
content: 'Top-level comment',
|
||||
display: true,
|
||||
details: { user: { id: user.id, name: user.name } },
|
||||
displayText: 'Top-level comment',
|
||||
timestamp: 1,
|
||||
context: { id: rootComment.id, thread: { id: rootComment.id, replyCount: 1 } },
|
||||
sourceCommentId: rootComment.id,
|
||||
},
|
||||
model: [{ role: 'user', content: 'Top-level comment', timestamp: 1 }],
|
||||
},
|
||||
})
|
||||
|
||||
await db.agentSession.update({
|
||||
where: { id: session.id },
|
||||
data: { leafId: entry.id },
|
||||
})
|
||||
|
||||
])
|
||||
const entries = await svc.getSessionEntries({ sessionId: session.id })
|
||||
expect(entries).toHaveLength(1)
|
||||
expect(entries[0].id).toBe(rootComment.id)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const entryDetails = (entries[0].entry as any).details
|
||||
expect(entryDetails?.thread).toEqual({
|
||||
id: rootComment.id,
|
||||
replyCount: 1,
|
||||
expect(entries[0]).toMatchObject({
|
||||
kind: 'shumai.message',
|
||||
data: { context: { thread: { id: rootComment.id, replyCount: 1 } } },
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,28 +1,29 @@
|
||||
import { prisma, type Prisma, type TeamMemberRole } from '@shumai/db'
|
||||
import {
|
||||
CreateAgentParams,
|
||||
DeleteAgentParams,
|
||||
ListAgentsParams,
|
||||
UpdateAgentParams,
|
||||
AgentSessionInfo,
|
||||
AgentInfo,
|
||||
AgentType,
|
||||
DEFAULT_DENIED_TOOLS,
|
||||
} from '@shumai/dtos'
|
||||
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
|
||||
import {
|
||||
paginateQuery,
|
||||
type PaginatedData,
|
||||
type PaginationParams,
|
||||
} from '@shumai/core/src/pagination'
|
||||
import '@shumai/db/src/prisma-json-types'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { getAvatarUrl } from '@shumai/core/src/user/avatar'
|
||||
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
|
||||
import { getAllowedAgentRoles } from './permissions'
|
||||
import { isPresetAvatarId, getPresetAvatarBuffer, getPresetAvatarStorageKey } from './presets'
|
||||
import { prisma, type Prisma, type TeamMemberRole } from '@shumai/db'
|
||||
import '@shumai/db/src/prisma-json-types'
|
||||
import {
|
||||
AgentInfo,
|
||||
AgentSessionInfo,
|
||||
AgentType,
|
||||
CreateAgentParams,
|
||||
DEFAULT_DENIED_TOOLS,
|
||||
DeleteAgentParams,
|
||||
ListAgentsParams,
|
||||
UpdateAgentParams,
|
||||
} from '@shumai/dtos'
|
||||
import AdmZip from 'adm-zip'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { getAllowedAgentRoles } from './permissions'
|
||||
import { getPresetAvatarBuffer, getPresetAvatarStorageKey, isPresetAvatarId } from './presets'
|
||||
import { agentSessionService } from './session-service'
|
||||
|
||||
const agentInclude = {
|
||||
user: true,
|
||||
@@ -572,95 +573,7 @@ export class AgentService {
|
||||
}
|
||||
|
||||
async getSessionEntries(params: { sessionId: string }) {
|
||||
const session = await this.prismaClient.agentSession.findUnique({
|
||||
where: { id: params.sessionId },
|
||||
select: { leafId: true },
|
||||
})
|
||||
|
||||
if (!session?.leafId) {
|
||||
return []
|
||||
}
|
||||
|
||||
interface EntryRow {
|
||||
id: string
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
session_id: string | null
|
||||
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 this.prismaClient.$queryRaw<EntryRow[]>`
|
||||
WITH RECURSIVE entry_path AS (
|
||||
SELECT id, session_id, type, parent_id, created_at, data, 1 AS depth
|
||||
FROM agent_session_entries
|
||||
WHERE id = ${session.leafId}
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT e.id, e.session_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, session_id, type, parent_id, created_at, data
|
||||
FROM entry_path
|
||||
ORDER BY depth DESC;
|
||||
`
|
||||
|
||||
const entryIds = rows.map((r) => r.id)
|
||||
const threadReplyCountMap = new Map<string, number>()
|
||||
if (entryIds.length > 0) {
|
||||
const threadCounts = await this.prismaClient.assetComment.groupBy({
|
||||
by: ['replyToId'],
|
||||
where: {
|
||||
replyToId: { in: entryIds },
|
||||
message: { not: '__CHAT__' },
|
||||
},
|
||||
_count: {
|
||||
id: true,
|
||||
},
|
||||
})
|
||||
for (const tc of threadCounts) {
|
||||
if (tc.replyToId) {
|
||||
threadReplyCountMap.set(tc.replyToId, tc._count.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rows.map((r) => {
|
||||
const payload = (r.data as Record<string, unknown>) || {}
|
||||
const count = threadReplyCountMap.get(r.id)
|
||||
let details = payload.details as Record<string, unknown> | undefined
|
||||
if (count !== undefined && count > 0 && r.type === 'custom_message') {
|
||||
details = {
|
||||
...details,
|
||||
thread: {
|
||||
id: r.id,
|
||||
replyCount: count,
|
||||
},
|
||||
}
|
||||
}
|
||||
const entryObj = {
|
||||
id: r.id,
|
||||
type: r.type || 'message',
|
||||
parentId: r.parent_id,
|
||||
timestamp: r.created_at.toISOString(),
|
||||
...payload,
|
||||
...(details ? { details } : {}),
|
||||
}
|
||||
return {
|
||||
id: r.id,
|
||||
sessionId: r.session_id || params.sessionId,
|
||||
type: r.type,
|
||||
parentId: r.parent_id,
|
||||
data: details !== payload.details ? { ...payload, details } : r.data,
|
||||
createdAt: r.created_at,
|
||||
entry: entryObj,
|
||||
}
|
||||
})
|
||||
return agentSessionService.getEntries(params.sessionId)
|
||||
}
|
||||
|
||||
async listSessions(
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import { BACKGROUND_CONTEXT } from '@earendil-works/chord/context'
|
||||
import type { Seq, StorageWrite } from '@earendil-works/pi-durable'
|
||||
import { openNodeSqliteStorage } from '@earendil-works/pi-durable/storage/sqlite/node'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { mkdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
|
||||
// Process-owned copies prevent an API replica from replacing a live writer's SQLite files.
|
||||
const instance = randomUUID()
|
||||
const root = join(
|
||||
process.env.AGENT_SESSION_CACHE_DIR || join(tmpdir(), 'shumai-sessions'),
|
||||
instance,
|
||||
)
|
||||
|
||||
// Call only while the session has no open connections; live readers borrow the writer's storage.
|
||||
export async function openSessionCache(sessionId: string) {
|
||||
const directory = join(root, encodeURIComponent(sessionId))
|
||||
await mkdir(directory, { recursive: true, mode: 0o700 })
|
||||
const path = join(directory, 'session.sqlite')
|
||||
const marker = join(directory, 'sync.json')
|
||||
const latest = await prisma.agentSessionCommit.findFirst({
|
||||
where: { sessionId },
|
||||
orderBy: { seq: 'desc' },
|
||||
select: { seq: true },
|
||||
})
|
||||
const head = Number(latest?.seq ?? 0)
|
||||
if (!Number.isSafeInteger(head))
|
||||
throw new Error('Session commit sequence exceeds the supported range')
|
||||
let seq = 0
|
||||
let reusable = false
|
||||
try {
|
||||
if (!(await stat(path)).isFile()) throw new Error('SQLite cache missing')
|
||||
const value: unknown = JSON.parse(await readFile(marker, 'utf8'))
|
||||
if (
|
||||
value &&
|
||||
typeof value === 'object' &&
|
||||
'seq' in value &&
|
||||
typeof value.seq === 'number' &&
|
||||
'clean' in value &&
|
||||
value.clean === true &&
|
||||
value.seq <= head
|
||||
) {
|
||||
seq = value.seq
|
||||
reusable = true
|
||||
}
|
||||
} catch {
|
||||
/* A missing/uncertain marker requires rebuilding the disposable copy. */
|
||||
}
|
||||
const save = async (clean: boolean) => {
|
||||
await writeFile(`${marker}.tmp`, JSON.stringify({ clean, seq }), { mode: 0o600 })
|
||||
await rename(`${marker}.tmp`, marker)
|
||||
}
|
||||
const remove = async () => {
|
||||
for (const suffix of ['', '-wal', '-shm']) await rm(`${path}${suffix}`, { force: true })
|
||||
seq = 0
|
||||
}
|
||||
if (!reusable) await remove()
|
||||
let storage
|
||||
try {
|
||||
storage = await openNodeSqliteStorage(path)
|
||||
} catch (error) {
|
||||
if (!reusable) throw error
|
||||
await remove()
|
||||
storage = await openNodeSqliteStorage(path)
|
||||
}
|
||||
try {
|
||||
await save(false)
|
||||
while (seq < head) {
|
||||
const commits = await prisma.agentSessionCommit.findMany({
|
||||
where: { sessionId, seq: { gt: BigInt(seq), lte: BigInt(head) } },
|
||||
orderBy: { seq: 'asc' },
|
||||
take: 100,
|
||||
})
|
||||
if (commits.length === 0) throw new Error(`Session ${sessionId} has a commit gap`)
|
||||
const writes = commits.map(({ data }) => {
|
||||
if (data.version !== 1 || !Array.isArray(data.writes))
|
||||
throw new Error('Unsupported session commit format')
|
||||
return data.writes as unknown as StorageWrite[]
|
||||
})
|
||||
const receipts: Array<{ index: number; seq: Seq }> = []
|
||||
const result = await storage.commit(
|
||||
writes,
|
||||
(index, storedSeq) => {
|
||||
receipts.push({ index, seq: storedSeq })
|
||||
},
|
||||
BACKGROUND_CONTEXT,
|
||||
)
|
||||
for (const receipt of receipts) {
|
||||
if (Number(commits[receipt.index].seq) !== receipt.seq)
|
||||
throw new Error('Session commit sequence mismatch')
|
||||
seq = receipt.seq
|
||||
}
|
||||
if (result.stored !== commits.length)
|
||||
throw result.refused ?? new Error('Session restoration refused')
|
||||
}
|
||||
await save(true)
|
||||
} catch (error) {
|
||||
await storage.close(BACKGROUND_CONTEXT)
|
||||
throw error
|
||||
}
|
||||
return {
|
||||
storage,
|
||||
seq,
|
||||
path,
|
||||
dirty: () => save(false),
|
||||
clean: async (next?: Seq) => {
|
||||
if (next !== undefined) seq = next
|
||||
await save(true)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export async function removeSessionCaches() {
|
||||
await rm(root, { recursive: true, force: true })
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
import type { ImageContent, Message, ToolResultMessage } from '@earendil-works/pi-ai'
|
||||
import type { Cursor, EntryRecord, HookApi, StorageWrite } from '@earendil-works/pi-durable'
|
||||
import { serializeContextToXml, shumaiMessageContextSchema } from '@shumai/dtos'
|
||||
import { s3Service } from '../s3/s3'
|
||||
import { agentService } from './agent'
|
||||
|
||||
export const S3_PLACEHOLDER = '__S3_DATA__'
|
||||
export const SKILL_PLACEHOLDER = '__SKILL_CONTENT__'
|
||||
|
||||
function contentReferences(value: unknown): { sourceKeys: string[]; skillId?: string } {
|
||||
if (!value || typeof value !== 'object') return { sourceKeys: [] }
|
||||
const record = value as Record<string, unknown>
|
||||
return {
|
||||
sourceKeys: Array.isArray(record.sourceKeys)
|
||||
? record.sourceKeys.filter((key): key is string => typeof key === 'string')
|
||||
: [],
|
||||
skillId: typeof record.skillId === 'string' ? record.skillId : undefined,
|
||||
}
|
||||
}
|
||||
|
||||
function compactMessage(message: Message, entryData: unknown): Message {
|
||||
if (message.role !== 'toolResult' && message.role !== 'user') return message
|
||||
const refs = contentReferences(message.role === 'toolResult' ? message.details : entryData)
|
||||
if (typeof message.content === 'string') return message
|
||||
let imageIndex = 0
|
||||
return {
|
||||
...message,
|
||||
content: message.content.map((block) => {
|
||||
if (block.type === 'image') {
|
||||
const key = refs.sourceKeys[imageIndex++]
|
||||
return key ? { ...block, data: S3_PLACEHOLDER } : block
|
||||
}
|
||||
if (
|
||||
message.role === 'toolResult' &&
|
||||
message.toolName === 'read_skill' &&
|
||||
!message.isError &&
|
||||
refs.skillId &&
|
||||
block.type === 'text'
|
||||
)
|
||||
return { ...block, text: SKILL_PLACEHOLDER }
|
||||
return block
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/** Only immutable entries are rewritten. Durable's document deltas keep their original bases. */
|
||||
export function compactSessionWrites(writes: readonly StorageWrite[]): StorageWrite[] {
|
||||
return writes.map((write) =>
|
||||
write.type === 'entry'
|
||||
? {
|
||||
...write,
|
||||
value: {
|
||||
...write.value,
|
||||
model: write.value.model?.map((m) => compactMessage(m, write.value.data)),
|
||||
},
|
||||
}
|
||||
: write,
|
||||
)
|
||||
}
|
||||
|
||||
export async function hydrateMessages(
|
||||
messages: readonly Message[],
|
||||
entryData?: unknown,
|
||||
): Promise<Message[]> {
|
||||
return Promise.all(
|
||||
messages.map(async (message): Promise<Message> => {
|
||||
if (message.role !== 'toolResult' && message.role !== 'user') return message
|
||||
if (typeof message.content === 'string') return message
|
||||
const refs = contentReferences(message.role === 'toolResult' ? message.details : entryData)
|
||||
let imageIndex = 0
|
||||
const skillText =
|
||||
message.role === 'toolResult' &&
|
||||
message.toolName === 'read_skill' &&
|
||||
!message.isError &&
|
||||
refs.skillId
|
||||
? await agentService.getSkillContent(refs.skillId)
|
||||
: undefined
|
||||
const content = await Promise.all(
|
||||
message.content.map(async (block) => {
|
||||
if (block.type === 'image') {
|
||||
const key = refs.sourceKeys[imageIndex++]
|
||||
if (block.data === S3_PLACEHOLDER && key) {
|
||||
const { buffer } = await s3Service.getObject(process.env.S3_BUCKET || 'shumai', key)
|
||||
return { ...block, data: buffer.toString('base64') } satisfies ImageContent
|
||||
}
|
||||
}
|
||||
if (block.type === 'text' && skillText !== undefined) return { ...block, text: skillText }
|
||||
return block
|
||||
}),
|
||||
)
|
||||
return { ...message, content } as ToolResultMessage | Extract<Message, { role: 'user' }>
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
export async function hydrateEntry(entry: EntryRecord): Promise<EntryRecord> {
|
||||
if (!entry.model) return entry
|
||||
let model = await hydrateMessages(entry.model, entry.data)
|
||||
if (
|
||||
entry.kind === 'shumai.message' &&
|
||||
entry.data &&
|
||||
typeof entry.data === 'object' &&
|
||||
!Array.isArray(entry.data)
|
||||
) {
|
||||
const parsed = shumaiMessageContextSchema.safeParse(entry.data.context)
|
||||
if (parsed.success) {
|
||||
const xml = serializeContextToXml(parsed.data)
|
||||
model = model.map((message) => {
|
||||
if (message.role !== 'user') return message
|
||||
const content =
|
||||
typeof message.content === 'string'
|
||||
? message.content
|
||||
: message.content
|
||||
.filter((b) => b.type === 'text')
|
||||
.map((b) => b.text)
|
||||
.join('\n')
|
||||
if (!xml || content.includes('<context>')) return message
|
||||
return {
|
||||
...message,
|
||||
content:
|
||||
typeof message.content === 'string'
|
||||
? `${message.content}\n\n${xml}`
|
||||
: [...message.content, { type: 'text' as const, text: xml }],
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
return { ...entry, model }
|
||||
}
|
||||
|
||||
/** An admitted input may still be in Durable's warm context before its first storage read. */
|
||||
export async function hydrateRequest(
|
||||
messages: readonly Message[],
|
||||
api: HookApi,
|
||||
context: import('@earendil-works/chord/context').Context,
|
||||
) {
|
||||
const waiting = new Set(
|
||||
messages
|
||||
.filter(
|
||||
(m) =>
|
||||
m.role === 'user' &&
|
||||
Array.isArray(m.content) &&
|
||||
m.content.some((b) => b.type === 'image' && b.data === S3_PLACEHOLDER),
|
||||
)
|
||||
.map((m) => m.timestamp),
|
||||
)
|
||||
const references = new Map<number, unknown>()
|
||||
let cursor: Cursor | undefined
|
||||
while (waiting.size) {
|
||||
const page = await api.entries({ order: 'descending' }, 100, cursor, context)
|
||||
for (const entry of page.items) {
|
||||
for (const message of entry.model ?? []) {
|
||||
if (message.role === 'user' && waiting.delete(message.timestamp))
|
||||
references.set(message.timestamp, entry.data)
|
||||
}
|
||||
}
|
||||
if (!page.next) break
|
||||
cursor = page.next
|
||||
}
|
||||
return (
|
||||
await Promise.all(
|
||||
messages.map((message) => hydrateMessages([message], references.get(message.timestamp))),
|
||||
)
|
||||
).flat()
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
import { openSessionCache } from './session-cache'
|
||||
import { BACKGROUND_CONTEXT as context } from '@earendil-works/chord/context'
|
||||
import { createModels, fauxAssistantMessage, fauxProvider } from '@earendil-works/pi-ai'
|
||||
import { Harness, createRegistry, UsageDoc } from '@earendil-works/pi-durable'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { agentSessionService } from './session-service'
|
||||
|
||||
const migration = new URL(
|
||||
'../../../db/prisma/migrations/20261110070817_pi_durable_session_commits/migration.sql',
|
||||
import.meta.url,
|
||||
)
|
||||
async function backfill() {
|
||||
const sql = await readFile(migration, 'utf8')
|
||||
const start = sql.indexOf('DO $migration$')
|
||||
const checkpoint = await readFile(
|
||||
new URL(
|
||||
'../../../db/prisma/migrations/20261110072953_agent_session_usage_checkpoint/migration.sql',
|
||||
import.meta.url,
|
||||
),
|
||||
'utf8',
|
||||
)
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.$executeRawUnsafe(sql.slice(start, sql.lastIndexOf('COMMIT;')))
|
||||
await tx.$executeRawUnsafe(checkpoint.slice(checkpoint.indexOf('-- Historical')))
|
||||
const preserve = await readFile(
|
||||
new URL(
|
||||
'../../../db/prisma/migrations/20261110094140_preserve_session_json_nulls/migration.sql',
|
||||
import.meta.url,
|
||||
),
|
||||
'utf8',
|
||||
)
|
||||
await tx.$executeRawUnsafe(
|
||||
preserve.slice(preserve.indexOf('CREATE FUNCTION'), preserve.lastIndexOf('COMMIT;')),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
describe('SQL migration to Pi Durable', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
async function fixture() {
|
||||
const team = await prisma.team.create({ data: { name: 'Migration team' } })
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Migration agent', email: 'pi-migration@example.test', type: 'agent' },
|
||||
})
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: user.id,
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'faux', model: 'faux-1' },
|
||||
},
|
||||
})
|
||||
const parent = await prisma.agentSession.create({
|
||||
data: { agentId: agent.id, cwd: '/migration', leafId: 'legacy-root' },
|
||||
})
|
||||
const child = await prisma.agentSession.create({
|
||||
data: { agentId: agent.id, cwd: '/migration', leafId: 'legacy-reply' },
|
||||
})
|
||||
await prisma.agentSessionEntry.createMany({
|
||||
data: [
|
||||
{
|
||||
id: 'legacy-root',
|
||||
sessionId: parent.id,
|
||||
type: 'custom_message',
|
||||
data: {
|
||||
customType: 'shumai_message',
|
||||
content: 'Original question',
|
||||
details: {
|
||||
annotation: true,
|
||||
annotations: [{ type: 'line' }],
|
||||
currentAsset: { id: 'asset', name: 'Image', type: 'file' },
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'legacy-reply',
|
||||
sessionId: child.id,
|
||||
parentId: 'legacy-root',
|
||||
type: 'custom_message',
|
||||
data: { customType: 'shumai_message', content: 'Follow-up', details: {} },
|
||||
},
|
||||
],
|
||||
})
|
||||
return { team, parent, child }
|
||||
}
|
||||
|
||||
it('copies inherited records, preserves annotations and opens as a continuable native session', async () => {
|
||||
const { parent, child } = await fixture()
|
||||
await backfill()
|
||||
expect(await agentSessionService.getEntries(parent.id)).toHaveLength(1)
|
||||
const entries = await agentSessionService.getEntries(child.id)
|
||||
expect(entries.map((entry) => entry.id)).toEqual([7, 9])
|
||||
expect(entries[0].data).toMatchObject({
|
||||
displayText: 'Original question',
|
||||
context: { annotation: true, id: `chat:${child.id}:legacy-legacy-root` },
|
||||
})
|
||||
expect(
|
||||
await agentSessionService.getEntryForRequest(child.id, 'legacy-legacy-root'),
|
||||
).toMatchObject({ id: 7 })
|
||||
const faux = fauxProvider()
|
||||
faux.setResponses([fauxAssistantMessage('Continued migrated conversation')])
|
||||
const models = createModels()
|
||||
models.setProvider(faux.provider)
|
||||
await agentSessionService.withWriter(child.id, async (storage) => {
|
||||
const harness = await Harness.open(storage, { models, registry: createRegistry() }, context)
|
||||
try {
|
||||
const root = await harness.root(context)
|
||||
await root.configure(
|
||||
{ model: { provider: faux.provider.id, modelId: faux.getModel().id } },
|
||||
context,
|
||||
)
|
||||
const settled = await (
|
||||
await root.submit({ type: 'input', content: 'Continue', requestId: 'continued' }, context)
|
||||
).wait(context)
|
||||
expect(settled.status).toBe('done')
|
||||
} finally {
|
||||
await harness.close(context)
|
||||
}
|
||||
})
|
||||
const count = await prisma.agentSessionCommit.count({ where: { sessionId: child.id } })
|
||||
await backfill()
|
||||
expect(await prisma.agentSessionCommit.count({ where: { sessionId: child.id } })).toBe(count)
|
||||
})
|
||||
|
||||
it('preserves compact tool results, remaps annotation references, compaction boundaries and historical usage', async () => {
|
||||
const { child } = await fixture()
|
||||
const assistant = fauxAssistantMessage([
|
||||
{
|
||||
type: 'toolCall',
|
||||
id: 'read',
|
||||
name: 'read_asset',
|
||||
arguments: { annotationId: 'legacy-root' },
|
||||
},
|
||||
])
|
||||
assistant.usage = {
|
||||
input: 10,
|
||||
output: 5,
|
||||
cacheRead: 2,
|
||||
cacheWrite: 0,
|
||||
totalTokens: 17,
|
||||
cost: { input: 0.1, output: 0.1, cacheRead: 0, cacheWrite: 0, total: 0.2 },
|
||||
}
|
||||
await prisma.agentSessionEntry.createMany({
|
||||
data: [
|
||||
{
|
||||
id: 'legacy-call',
|
||||
parentId: 'legacy-reply',
|
||||
sessionId: child.id,
|
||||
type: 'message',
|
||||
data: { message: assistant },
|
||||
},
|
||||
{
|
||||
id: 'legacy-tool',
|
||||
parentId: 'legacy-call',
|
||||
sessionId: child.id,
|
||||
type: 'message',
|
||||
data: {
|
||||
message: {
|
||||
role: 'toolResult',
|
||||
toolCallId: 'read',
|
||||
toolName: 'read_asset',
|
||||
content: [{ type: 'image', data: '__S3_DATA__', mimeType: 'image/png' }],
|
||||
details: { sourceKeys: ['historical-image'], exitCode: null },
|
||||
isError: false,
|
||||
timestamp: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'legacy-compact',
|
||||
parentId: 'legacy-tool',
|
||||
sessionId: child.id,
|
||||
type: 'compaction',
|
||||
data: { summary: 'Earlier discussion', firstKeptEntryId: 'legacy-call' },
|
||||
},
|
||||
{
|
||||
id: 'legacy-branch',
|
||||
parentId: 'legacy-compact',
|
||||
sessionId: child.id,
|
||||
type: 'branch_summary',
|
||||
data: { summary: 'Copied branch context' },
|
||||
},
|
||||
],
|
||||
})
|
||||
await prisma.agentSession.update({ where: { id: child.id }, data: { leafId: 'legacy-branch' } })
|
||||
await backfill()
|
||||
const entries = await agentSessionService.getEntries(child.id)
|
||||
expect(entries[2].model?.[0]).toMatchObject({
|
||||
content: [{ arguments: { annotationId: `chat:${child.id}:legacy-legacy-root` } }],
|
||||
})
|
||||
expect(entries[3].model?.[0]).toMatchObject({
|
||||
content: [{ data: '__S3_DATA__' }],
|
||||
details: { sourceKeys: ['historical-image'], exitCode: null },
|
||||
})
|
||||
expect(entries[4]).toMatchObject({ kind: 'pi.compaction', head: entries[2].id })
|
||||
expect(JSON.stringify(entries[5].model)).toContain('Copied branch context')
|
||||
expect(
|
||||
(await prisma.agentSession.findUniqueOrThrow({ where: { id: child.id } })).accountedUsage,
|
||||
).toMatchObject({ totalTokens: 17, cost: 0.2 })
|
||||
// Read the native ledger without hydrating image references.
|
||||
const cache = await openSessionCache(child.id)
|
||||
const harness = await Harness.open(
|
||||
cache.storage,
|
||||
{ models: createModels(), registry: createRegistry() },
|
||||
context,
|
||||
)
|
||||
try {
|
||||
expect(
|
||||
(await harness.snapshot(UsageDoc, (await harness.root(context)).id, context))?.models[
|
||||
'faux/faux-1'
|
||||
].totalTokens,
|
||||
).toBe(17)
|
||||
} finally {
|
||||
await harness.close(context)
|
||||
}
|
||||
})
|
||||
|
||||
it('rejects missing ancestry before writing any converted records', async () => {
|
||||
const { child } = await fixture()
|
||||
await prisma.agentSessionEntry.update({
|
||||
where: { id: 'legacy-reply' },
|
||||
data: { parentId: 'missing' },
|
||||
})
|
||||
await expect(backfill()).rejects.toThrow('ancestor is missing')
|
||||
expect(await prisma.agentSessionCommit.count({ where: { sessionId: child.id } })).toBe(0)
|
||||
})
|
||||
|
||||
it('rejects cyclic ancestry', async () => {
|
||||
await fixture()
|
||||
await prisma.agentSessionEntry.update({
|
||||
where: { id: 'legacy-root' },
|
||||
data: { parentId: 'legacy-reply' },
|
||||
})
|
||||
await expect(backfill()).rejects.toThrow('cyclic history')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,480 @@
|
||||
import { BACKGROUND_CONTEXT as context } from '@earendil-works/chord/context'
|
||||
import { createModels, fauxAssistantMessage, fauxProvider } from '@earendil-works/pi-ai'
|
||||
import { Harness, ROOT_CONVERSATION_ID, UsageDoc, createRegistry } from '@earendil-works/pi-durable'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { acquireAdvisoryLock } from '@shumai/db/src/advisory-lock'
|
||||
import * as advisoryLock from '@shumai/db/src/advisory-lock'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { rm, writeFile } from 'node:fs/promises'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { s3Service } from '../s3/s3'
|
||||
import { openSessionCache } from './session-cache'
|
||||
import * as sessionCache from './session-cache'
|
||||
import * as sessionStorage from './session-storage'
|
||||
import { agentSessionService } from './session-service'
|
||||
import { appendSessionEntries } from './session-test-utils'
|
||||
|
||||
describe('Native session storage and PostgreSQL synchronization', () => {
|
||||
setupTestDbHooks()
|
||||
afterEach(async () => {
|
||||
vi.restoreAllMocks()
|
||||
await agentSessionService.closeAll()
|
||||
})
|
||||
|
||||
async function fixture() {
|
||||
const team = await prisma.team.create({ data: { name: 'Storage team' } })
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Agent', email: 'storage@example.test', type: 'agent' },
|
||||
})
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: user.id,
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'faux', model: 'faux-1' },
|
||||
},
|
||||
})
|
||||
const session = await prisma.agentSession.create({ data: { agentId: agent.id, cwd: '/test' } })
|
||||
return { team, session }
|
||||
}
|
||||
|
||||
it('reconstructs missing SQLite files and preserves native IDs, ordering and pagination', async () => {
|
||||
const { session } = await fixture()
|
||||
const entries = await appendSessionEntries(
|
||||
session.id,
|
||||
Array.from({ length: 25 }, (_, i) => ({
|
||||
kind: 'pi.user',
|
||||
model: [{ role: 'user' as const, content: `message ${i}`, timestamp: i }],
|
||||
})),
|
||||
)
|
||||
await agentSessionService.closeAll()
|
||||
expect(await agentSessionService.getEntries(session.id)).toEqual(entries)
|
||||
const first = await agentSessionService.listEntries(session.id)
|
||||
expect(first.items).toHaveLength(20)
|
||||
const next = await agentSessionService.listEntries(session.id, {
|
||||
after: first.items.at(-1)!.id,
|
||||
})
|
||||
expect(next.items).toEqual(entries.slice(20))
|
||||
const cursor = await agentSessionService.listEntries(session.id, { cursor: first.next })
|
||||
expect(cursor.items).toEqual(next.items)
|
||||
expect(await prisma.agentSessionEntry.count({ where: { sessionId: session.id } })).toBe(0)
|
||||
})
|
||||
|
||||
it.each(['missing', 'corrupt', 'dirty'] as const)(
|
||||
'rebuilds a %s local cache from mirrored commits',
|
||||
async (failure) => {
|
||||
const { session } = await fixture()
|
||||
const entries = await appendSessionEntries(session.id, [
|
||||
{ kind: 'pi.user', model: [{ role: 'user', content: 'safe', timestamp: 1 }] },
|
||||
])
|
||||
const cache = await openSessionCache(session.id)
|
||||
await cache.storage.close(context)
|
||||
if (failure === 'missing') await rm(cache.path)
|
||||
else if (failure === 'corrupt') await writeFile(cache.path, 'invalid sqlite')
|
||||
else await cache.dirty()
|
||||
expect(await agentSessionService.getEntries(session.id)).toEqual(entries)
|
||||
},
|
||||
)
|
||||
|
||||
it('holds the session ownership lock until close and permits cancellation while waiting', async () => {
|
||||
const { session } = await fixture()
|
||||
const writer = await agentSessionService.openWriter(session.id)
|
||||
const abort = new AbortController()
|
||||
const waiting = acquireAdvisoryLock(`agent-session:${session.id}`, abort.signal)
|
||||
abort.abort(new Error('cancelled'))
|
||||
await expect(waiting).rejects.toThrow('cancelled')
|
||||
await writer.storage.close(context)
|
||||
const next = await acquireAdvisoryLock(`agent-session:${session.id}`)
|
||||
await next.release()
|
||||
})
|
||||
|
||||
it('reads locally committed entries while PostgreSQL persistence is pending', async () => {
|
||||
const { session } = await fixture()
|
||||
const started = Promise.withResolvers<void>()
|
||||
const resume = Promise.withResolvers<void>()
|
||||
const synchronize = sessionStorage.synchronizeStorage
|
||||
let paused = false
|
||||
vi.spyOn(sessionStorage, 'synchronizeStorage').mockImplementation((storage, callbacks) =>
|
||||
synchronize(storage, {
|
||||
...callbacks,
|
||||
persist: async (commits) => {
|
||||
if (paused) {
|
||||
started.resolve()
|
||||
await resume.promise
|
||||
}
|
||||
await callbacks.persist(commits)
|
||||
},
|
||||
}),
|
||||
)
|
||||
const writer = await agentSessionService.openWriter(session.id)
|
||||
const harness = await Harness.open(
|
||||
writer.storage,
|
||||
{ models: createModels(), registry: createRegistry() },
|
||||
context,
|
||||
)
|
||||
const root = await harness.root(context)
|
||||
paused = true
|
||||
let acknowledged = false
|
||||
const commit = root.commit(
|
||||
(tx) => tx.appendEntry(root.id, { kind: 'pi.user', data: { text: 'locally committed' } }),
|
||||
context,
|
||||
)
|
||||
void commit.then(() => {
|
||||
acknowledged = true
|
||||
})
|
||||
let closing: Promise<void> | undefined
|
||||
try {
|
||||
await started.promise
|
||||
expect(await prisma.agentSessionCommit.count({ where: { sessionId: session.id } })).toBe(1)
|
||||
expect(await agentSessionService.getEntries(session.id)).toMatchObject([
|
||||
{ kind: 'pi.user', data: { text: 'locally committed' } },
|
||||
])
|
||||
expect(acknowledged).toBe(false)
|
||||
closing = writer.storage.close(context)
|
||||
// Closing drains persistence before entering the lifecycle queue, so reads still work.
|
||||
expect(await agentSessionService.getEntries(session.id)).toHaveLength(1)
|
||||
} finally {
|
||||
resume.resolve()
|
||||
await commit
|
||||
await closing
|
||||
await harness.close(context)
|
||||
}
|
||||
expect(acknowledged).toBe(true)
|
||||
// A history read must not close the writer's connection.
|
||||
await agentSessionService.withWriter(session.id, async (storage) => {
|
||||
expect(await storage.conversation(root.id, context)).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
it('uses one cache directory for concurrent initial reads and subsequent writes', async () => {
|
||||
const { session } = await fixture()
|
||||
const paths: string[] = []
|
||||
const openCache = sessionCache.openSessionCache
|
||||
vi.spyOn(sessionCache, 'openSessionCache').mockImplementation(async (...args) => {
|
||||
const cache = await openCache(...args)
|
||||
paths.push(cache.path)
|
||||
return cache
|
||||
})
|
||||
expect(
|
||||
await Promise.all([
|
||||
agentSessionService.getEntries(session.id),
|
||||
agentSessionService.listEntries(session.id),
|
||||
]),
|
||||
).toEqual([[], { items: [], next: undefined }])
|
||||
const [written] = await Promise.all([
|
||||
appendSessionEntries(session.id, [{ kind: 'pi.user', data: { text: 'shared cache' } }]),
|
||||
agentSessionService.getEntries(session.id),
|
||||
])
|
||||
expect(await agentSessionService.getEntries(session.id)).toEqual(written)
|
||||
expect(new Set(paths).size).toBe(1)
|
||||
})
|
||||
|
||||
it('keeps history compact while the live writer hydrates image references', async () => {
|
||||
const { session } = await fixture()
|
||||
const image = Buffer.from('image bytes')
|
||||
vi.spyOn(s3Service, 'getObject').mockResolvedValue({ buffer: image, contentType: 'image/png' })
|
||||
await agentSessionService.withWriter(session.id, async (storage) => {
|
||||
const harness = await Harness.open(
|
||||
storage,
|
||||
{ models: createModels(), registry: createRegistry() },
|
||||
context,
|
||||
)
|
||||
try {
|
||||
const root = await harness.root(context)
|
||||
const entry = await root.commit(
|
||||
(tx) =>
|
||||
tx.appendEntry(root.id, {
|
||||
kind: 'pi.user',
|
||||
data: { sourceKeys: ['image-key'] },
|
||||
model: [
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'image', data: image.toString('base64'), mimeType: 'image/png' },
|
||||
],
|
||||
timestamp: 1,
|
||||
},
|
||||
],
|
||||
}),
|
||||
context,
|
||||
)
|
||||
expect(JSON.stringify((await storage.entry(entry.id, context))?.entry)).toContain(
|
||||
image.toString('base64'),
|
||||
)
|
||||
const history = await agentSessionService.getEntries(session.id)
|
||||
expect(JSON.stringify(history)).toContain('__S3_DATA__')
|
||||
expect(JSON.stringify(history)).not.toContain(image.toString('base64'))
|
||||
expect((await agentSessionService.listEntries(session.id)).items).toEqual(history)
|
||||
} finally {
|
||||
await harness.close(context)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('waits for an admitted history read before closing the live writer', async () => {
|
||||
const { session } = await fixture()
|
||||
const entered = Promise.withResolvers<void>()
|
||||
const resume = Promise.withResolvers<void>()
|
||||
const openCache = sessionCache.openSessionCache
|
||||
let paused = false
|
||||
vi.spyOn(sessionCache, 'openSessionCache').mockImplementation(async (...args) => {
|
||||
const cache = await openCache(...args)
|
||||
const scan = cache.storage.scanEntries.bind(cache.storage)
|
||||
cache.storage.scanEntries = async (...query) => {
|
||||
if (paused) {
|
||||
entered.resolve()
|
||||
await resume.promise
|
||||
}
|
||||
return scan(...query)
|
||||
}
|
||||
return cache
|
||||
})
|
||||
const writer = await agentSessionService.openWriter(session.id)
|
||||
const harness = await Harness.open(
|
||||
writer.storage,
|
||||
{ models: createModels(), registry: createRegistry() },
|
||||
context,
|
||||
)
|
||||
const root = await harness.root(context)
|
||||
await root.commit((tx) => tx.appendEntry(root.id, { kind: 'pi.user', data: {} }), context)
|
||||
paused = true
|
||||
const read = agentSessionService.getEntries(session.id)
|
||||
await entered.promise
|
||||
let closed = false
|
||||
const closing = writer.storage.close(context).then(() => {
|
||||
closed = true
|
||||
})
|
||||
try {
|
||||
expect(closed).toBe(false)
|
||||
} finally {
|
||||
resume.resolve()
|
||||
expect(await read).toHaveLength(1)
|
||||
await closing
|
||||
await harness.close(context)
|
||||
}
|
||||
expect(closed).toBe(true)
|
||||
})
|
||||
|
||||
it('drains a writer being opened before shutdown removes the shared cache', async () => {
|
||||
const { session } = await fixture()
|
||||
const entered = Promise.withResolvers<void>()
|
||||
const resume = Promise.withResolvers<void>()
|
||||
const openCache = sessionCache.openSessionCache
|
||||
vi.spyOn(sessionCache, 'openSessionCache').mockImplementationOnce(async (...args) => {
|
||||
const cache = await openCache(...args)
|
||||
entered.resolve()
|
||||
await resume.promise
|
||||
return cache
|
||||
})
|
||||
const opening = agentSessionService.openWriter(session.id)
|
||||
await entered.promise
|
||||
const shutdown = agentSessionService.closeAll()
|
||||
try {
|
||||
expect(agentSessionService.closeAll()).toBe(shutdown)
|
||||
await expect(agentSessionService.getEntries(session.id)).rejects.toThrow('service is closing')
|
||||
} finally {
|
||||
resume.resolve()
|
||||
await shutdown
|
||||
}
|
||||
const writer = await opening
|
||||
await expect(writer.storage.conversation(ROOT_CONVERSATION_ID, context)).rejects.toThrow(
|
||||
'closed',
|
||||
)
|
||||
expect(await agentSessionService.getEntries(session.id)).toEqual([])
|
||||
})
|
||||
|
||||
it('cancels a writer waiting for ownership when the service shuts down', async () => {
|
||||
const { session } = await fixture()
|
||||
const held = await acquireAdvisoryLock(`agent-session:${session.id}`)
|
||||
const opening = agentSessionService.openWriter(session.id)
|
||||
const rejected = expect(opening).rejects.toThrow('Session service is closing')
|
||||
try {
|
||||
await agentSessionService.closeAll()
|
||||
await rejected
|
||||
} finally {
|
||||
await held.release()
|
||||
}
|
||||
expect(await agentSessionService.getEntries(session.id)).toEqual([])
|
||||
})
|
||||
|
||||
it('drains a writer that lost ownership before a replacement rebuilds its shared cache', async () => {
|
||||
const { session } = await fixture()
|
||||
const acquire = advisoryLock.acquireAdvisoryLock
|
||||
const lost = new AbortController()
|
||||
let release: (() => Promise<void>) | undefined
|
||||
vi.spyOn(advisoryLock, 'acquireAdvisoryLock').mockImplementationOnce(async (...args) => {
|
||||
const lock = await acquire(...args)
|
||||
release = lock.release
|
||||
return { ...lock, signal: lost.signal }
|
||||
})
|
||||
const started = Promise.withResolvers<void>()
|
||||
const resume = Promise.withResolvers<void>()
|
||||
const synchronize = sessionStorage.synchronizeStorage
|
||||
vi.spyOn(sessionStorage, 'synchronizeStorage').mockImplementationOnce((storage, callbacks) =>
|
||||
synchronize(storage, {
|
||||
...callbacks,
|
||||
persist: async (commits) => {
|
||||
started.resolve()
|
||||
await resume.promise
|
||||
await callbacks.persist(commits)
|
||||
},
|
||||
}),
|
||||
)
|
||||
const opens = vi.spyOn(sessionCache, 'openSessionCache')
|
||||
const first = await agentSessionService.openWriter(session.id)
|
||||
const closing = vi.spyOn(first.storage, 'close')
|
||||
const acknowledge = vi.fn()
|
||||
const commit = first.storage.commit(
|
||||
[
|
||||
[
|
||||
{ type: 'conversation', value: { id: ROOT_CONVERSATION_ID } },
|
||||
{
|
||||
type: 'entry',
|
||||
value: {
|
||||
id: await first.storage.mintId(),
|
||||
conversationId: ROOT_CONVERSATION_ID,
|
||||
kind: 'pi.user',
|
||||
data: { text: 'unmirrored' },
|
||||
},
|
||||
},
|
||||
],
|
||||
],
|
||||
acknowledge,
|
||||
context,
|
||||
)
|
||||
const rejected = expect(commit).rejects.toThrow('lock lost')
|
||||
await started.promise
|
||||
lost.abort(new Error('lock lost'))
|
||||
await release?.()
|
||||
const replacement = agentSessionService.openWriter(session.id)
|
||||
try {
|
||||
await vi.waitFor(() => expect(closing).toHaveBeenCalledOnce())
|
||||
expect(await agentSessionService.getEntries(session.id)).toMatchObject([
|
||||
{ data: { text: 'unmirrored' } },
|
||||
])
|
||||
expect(opens).toHaveBeenCalledTimes(1)
|
||||
} finally {
|
||||
resume.resolve()
|
||||
await rejected
|
||||
await replacement
|
||||
}
|
||||
expect(acknowledge).not.toHaveBeenCalled()
|
||||
await first.storage.close(context)
|
||||
expect(await agentSessionService.getEntries(session.id)).toEqual([])
|
||||
expect(opens).toHaveBeenCalledTimes(2)
|
||||
await (await replacement).storage.close(context)
|
||||
})
|
||||
|
||||
it('refuses a fenced writer and rebuilds its unacknowledged SQLite commit', async () => {
|
||||
const { session } = await fixture()
|
||||
const writer = await agentSessionService.openWriter(session.id)
|
||||
await prisma.agentSession.update({
|
||||
where: { id: session.id },
|
||||
data: { writerVersion: { increment: 1 } },
|
||||
})
|
||||
await expect(
|
||||
writer.storage.commit(
|
||||
[
|
||||
[
|
||||
{
|
||||
type: 'conversation',
|
||||
value: { id: ROOT_CONVERSATION_ID },
|
||||
},
|
||||
],
|
||||
],
|
||||
() => {
|
||||
throw new Error('must not acknowledge')
|
||||
},
|
||||
context,
|
||||
),
|
||||
).rejects.toThrow('ownership was lost')
|
||||
await writer.storage.close(context)
|
||||
expect(await agentSessionService.getEntries(session.id)).toEqual([])
|
||||
})
|
||||
|
||||
it('commits usage and transcript together, and does not bill replay or completed request recovery', async () => {
|
||||
const { team, session } = await fixture()
|
||||
const faux = fauxProvider()
|
||||
const answer = fauxAssistantMessage('paid answer')
|
||||
answer.usage = {
|
||||
input: 12,
|
||||
output: 7,
|
||||
cacheRead: 3,
|
||||
cacheWrite: 0,
|
||||
totalTokens: 22,
|
||||
cost: { input: 0.1, output: 0.2, cacheRead: 0, cacheWrite: 0, total: 0.3 },
|
||||
}
|
||||
faux.setResponses([answer])
|
||||
const models = createModels()
|
||||
models.setProvider(faux.provider)
|
||||
const run = async () => {
|
||||
const writer = await agentSessionService.openWriter(session.id, context, { teamId: team.id })
|
||||
const harness = await Harness.open(
|
||||
writer.storage,
|
||||
{ models, registry: createRegistry() },
|
||||
context,
|
||||
)
|
||||
try {
|
||||
const root = await harness.root(context)
|
||||
await root.configure(
|
||||
{ model: { provider: faux.provider.id, modelId: faux.getModel().id } },
|
||||
context,
|
||||
)
|
||||
const receipt = await (
|
||||
await root.submit({ type: 'input', requestId: 'once', content: 'question' }, context)
|
||||
).wait(context)
|
||||
expect(await agentSessionService.getEntryForRequest(session.id, 'once')).toBeDefined()
|
||||
return receipt
|
||||
} finally {
|
||||
await harness.close(context)
|
||||
}
|
||||
}
|
||||
const receipt = await run()
|
||||
await agentSessionService.closeAll()
|
||||
expect(await run()).toEqual(receipt)
|
||||
const usage = await prisma.aiUsage.findFirstOrThrow({ where: { teamId: team.id } })
|
||||
const checkpoint = (await prisma.agentSession.findUniqueOrThrow({ where: { id: session.id } }))
|
||||
.accountedUsage!
|
||||
expect(Number(usage.totalTokens)).toBeGreaterThan(0)
|
||||
expect(Number(usage.totalTokens)).toBe(checkpoint.totalTokens)
|
||||
expect(Number(usage.cost)).toBe(checkpoint.cost)
|
||||
})
|
||||
|
||||
it('rolls back usage if PostgreSQL cannot persist the native commit', async () => {
|
||||
const { team, session } = await fixture()
|
||||
const writer = await agentSessionService.openWriter(session.id, context, { teamId: team.id })
|
||||
const harness = await Harness.open(
|
||||
writer.storage,
|
||||
{ models: createModels(), registry: createRegistry() },
|
||||
context,
|
||||
)
|
||||
const root = await harness.root(context)
|
||||
const failure = vi
|
||||
.spyOn(prisma.agentSessionCommit, 'createMany')
|
||||
.mockRejectedValueOnce(new Error('mirror unavailable'))
|
||||
await expect(
|
||||
root.commit(async (tx) => {
|
||||
const usage = await tx.doc(UsageDoc, root.id)
|
||||
usage.models['faux/test'] = fauxAssistantMessage('answer').usage
|
||||
usage.models['faux/test'].totalTokens = 25
|
||||
await tx.appendEntry(root.id, {
|
||||
kind: 'pi.assistant',
|
||||
model: [fauxAssistantMessage('answer')],
|
||||
})
|
||||
}, context),
|
||||
).rejects.toThrow('mirror unavailable')
|
||||
expect(failure).toHaveBeenCalledTimes(1)
|
||||
failure.mockRestore()
|
||||
await harness.close(context)
|
||||
expect(await prisma.aiUsage.count({ where: { teamId: team.id } })).toBe(0)
|
||||
expect(await agentSessionService.getEntries(session.id)).toEqual([])
|
||||
})
|
||||
|
||||
it('deletes mirrored records with their session and rejects subsequent history reads', async () => {
|
||||
const { session } = await fixture()
|
||||
await appendSessionEntries(session.id, [{ kind: 'pi.user', data: {} }])
|
||||
await prisma.agentSession.delete({ where: { id: session.id } })
|
||||
expect(await prisma.agentSessionCommit.count({ where: { sessionId: session.id } })).toBe(0)
|
||||
await expect(agentSessionService.getEntries(session.id)).rejects.toThrow('Session not found')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,299 @@
|
||||
import { BACKGROUND_CONTEXT, type Context } from '@earendil-works/chord/context'
|
||||
import {
|
||||
ROOT_CONVERSATION_ID,
|
||||
type Cursor,
|
||||
type EntryId,
|
||||
type EntryRecord,
|
||||
type Storage,
|
||||
type UsageState,
|
||||
} from '@earendil-works/pi-durable'
|
||||
import { prisma, type Prisma, type TeamMemberRole } from '@shumai/db'
|
||||
import { acquireAdvisoryLock } from '@shumai/db/src/advisory-lock'
|
||||
import { AiUsageService } from '../ai-usage/ai-usage'
|
||||
import { quotaService } from '../quota/quota-service'
|
||||
import { openSessionCache, removeSessionCaches } from './session-cache'
|
||||
import { hydrateStorage, synchronizeStorage, type CommitReceipt } from './session-storage'
|
||||
|
||||
type SessionReader = Pick<Storage, 'conversation' | 'scanEntries' | 'submissionByRequest' | 'entry'>
|
||||
|
||||
type UsageOwner = { teamId: string; userId?: string; role?: TeamMemberRole | null }
|
||||
|
||||
export async function readSessionUsage(storage: Storage): Promise<PrismaJson.AgentSessionUsage> {
|
||||
const doc = await storage.findDocument(
|
||||
{ kind: 'pi.usage', scope: { kind: 'conversation', conversationId: ROOT_CONVERSATION_ID } },
|
||||
'current',
|
||||
BACKGROUND_CONTEXT,
|
||||
)
|
||||
const stored = doc ? await storage.document(doc.id, 'current', BACKGROUND_CONTEXT) : undefined
|
||||
const state = stored?.value as unknown as UsageState | undefined
|
||||
const totals = { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, totalTokens: 0, cost: 0 }
|
||||
for (const usage of Object.values(state?.models ?? {})) {
|
||||
totals.inputTokens += usage.input
|
||||
totals.outputTokens += usage.output
|
||||
totals.cacheReadTokens += usage.cacheRead
|
||||
totals.totalTokens += usage.totalTokens
|
||||
totals.cost += usage.cost.total
|
||||
}
|
||||
return totals
|
||||
}
|
||||
|
||||
export class AgentSessionService {
|
||||
private readonly access = new Map<string, Promise<unknown>>()
|
||||
private readonly writers = new Map<string, { storage: Storage; reader: SessionReader }>()
|
||||
private closing: Promise<void> | undefined
|
||||
private shutdown = new AbortController()
|
||||
|
||||
async openWriter(sessionId: string, context: Context = BACKGROUND_CONTEXT, owner?: UsageOwner) {
|
||||
if (this.closing) throw new Error('Session service is closing')
|
||||
const shutdown = this.shutdown.signal
|
||||
const signal = context.abortSignal ? AbortSignal.any([context.abortSignal, shutdown]) : shutdown
|
||||
const lock = await acquireAdvisoryLock(`agent-session:${sessionId}`, signal)
|
||||
let storage: Storage | undefined
|
||||
try {
|
||||
// A previous writer may still be draining after losing its advisory-lock connection.
|
||||
const previous = await this.withCacheAccess(sessionId, async () =>
|
||||
this.writers.get(sessionId),
|
||||
)
|
||||
await previous?.storage.close(BACKGROUND_CONTEXT)
|
||||
return await this.withCacheAccess(sessionId, async () => {
|
||||
shutdown.throwIfAborted()
|
||||
lock.signal.throwIfAborted()
|
||||
const session = await prisma.agentSession.update({
|
||||
where: { id: sessionId },
|
||||
data: { writerVersion: { increment: 1 } },
|
||||
})
|
||||
const cache = await openSessionCache(sessionId)
|
||||
storage = cache.storage
|
||||
// Capture native reads before writer hydration decorates this instance.
|
||||
const reader: SessionReader = {
|
||||
conversation: storage.conversation.bind(storage),
|
||||
scanEntries: storage.scanEntries.bind(storage),
|
||||
submissionByRequest: storage.submissionByRequest.bind(storage),
|
||||
entry: storage.entry.bind(storage),
|
||||
}
|
||||
const close = storage.close.bind(storage)
|
||||
storage.close = (context) =>
|
||||
this.withCacheAccess(sessionId, async () => {
|
||||
try {
|
||||
await close(context)
|
||||
} finally {
|
||||
if (this.writers.get(sessionId)?.storage === cache.storage)
|
||||
this.writers.delete(sessionId)
|
||||
await lock.release()
|
||||
}
|
||||
})
|
||||
let head = cache.seq
|
||||
storage = hydrateStorage(
|
||||
synchronizeStorage(cache.storage, {
|
||||
dirty: cache.dirty,
|
||||
clean: cache.clean,
|
||||
persist: async (commits: readonly CommitReceipt[]) => {
|
||||
lock.signal.throwIfAborted()
|
||||
if (!commits.length) return
|
||||
await prisma.$transaction(async (tx) => {
|
||||
const rows = await tx.$queryRaw<
|
||||
Array<{ writerVersion: number }>
|
||||
>`SELECT writer_version AS "writerVersion" FROM agent_sessions WHERE id = ${sessionId} FOR UPDATE`
|
||||
if (rows[0]?.writerVersion !== session.writerVersion)
|
||||
throw new Error('Session writer ownership was lost')
|
||||
const latest = await tx.agentSessionCommit.findFirst({
|
||||
where: { sessionId },
|
||||
orderBy: { seq: 'desc' },
|
||||
select: { seq: true },
|
||||
})
|
||||
if (Number(latest?.seq ?? 0) !== head)
|
||||
throw new Error('Session changed while writing')
|
||||
if (owner)
|
||||
await this.recordUsage(
|
||||
sessionId,
|
||||
await readSessionUsage(cache.storage),
|
||||
owner,
|
||||
tx,
|
||||
)
|
||||
await tx.agentSessionCommit.createMany({
|
||||
data: commits.map(({ seq, writes }) => ({
|
||||
sessionId,
|
||||
seq: BigInt(seq),
|
||||
data: JSON.parse(
|
||||
JSON.stringify({ version: 1, writes }),
|
||||
) as PrismaJson.AgentSessionCommitData,
|
||||
})),
|
||||
})
|
||||
})
|
||||
head = commits[commits.length - 1].seq
|
||||
},
|
||||
}),
|
||||
)
|
||||
this.writers.set(sessionId, { storage, reader })
|
||||
return { storage, signal: lock.signal }
|
||||
})
|
||||
} catch (error) {
|
||||
try {
|
||||
await storage?.close(BACKGROUND_CONTEXT)
|
||||
} finally {
|
||||
await lock.release()
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async withWriter<T>(
|
||||
sessionId: string,
|
||||
use: (storage: Storage, signal: AbortSignal) => Promise<T>,
|
||||
context: Context = BACKGROUND_CONTEXT,
|
||||
): Promise<T> {
|
||||
const opened = await this.openWriter(sessionId, context)
|
||||
try {
|
||||
return await use(opened.storage, opened.signal)
|
||||
} finally {
|
||||
await opened.storage.close(BACKGROUND_CONTEXT)
|
||||
}
|
||||
}
|
||||
|
||||
closeAll(): Promise<void> {
|
||||
if (this.closing) return this.closing
|
||||
this.shutdown.abort(new Error('Session service is closing'))
|
||||
return (this.closing ??= (async () => {
|
||||
// An admitted open may publish its writer after shutdown starts.
|
||||
await Promise.allSettled(this.access.values())
|
||||
await Promise.allSettled(
|
||||
[...this.writers.values()].map(({ storage }) => storage.close(BACKGROUND_CONTEXT)),
|
||||
)
|
||||
await Promise.allSettled(this.access.values())
|
||||
await removeSessionCaches()
|
||||
})().finally(() => {
|
||||
this.shutdown = new AbortController()
|
||||
this.closing = undefined
|
||||
}))
|
||||
}
|
||||
|
||||
/** Serialize file initialization, history reads and final close, never PostgreSQL mirroring. */
|
||||
private async withCacheAccess<T>(sessionId: string, use: () => Promise<T>): Promise<T> {
|
||||
const previous = this.access.get(sessionId)
|
||||
const current = (async () => {
|
||||
await previous?.catch(() => {})
|
||||
return use()
|
||||
})()
|
||||
this.access.set(sessionId, current)
|
||||
try {
|
||||
return await current
|
||||
} finally {
|
||||
if (this.access.get(sessionId) === current) this.access.delete(sessionId)
|
||||
}
|
||||
}
|
||||
|
||||
private async read<T>(
|
||||
sessionId: string,
|
||||
use: (storage: SessionReader) => Promise<T>,
|
||||
): Promise<T> {
|
||||
if (this.closing) throw new Error('Session service is closing')
|
||||
return this.withCacheAccess(sessionId, async () => {
|
||||
const session = await prisma.agentSession.findUnique({
|
||||
where: { id: sessionId },
|
||||
select: { id: true },
|
||||
})
|
||||
if (!session) throw new Error('Session not found')
|
||||
const writer = this.writers.get(sessionId)
|
||||
if (writer) return use(writer.reader)
|
||||
const cache = await openSessionCache(sessionId)
|
||||
try {
|
||||
return await use(cache.storage)
|
||||
} finally {
|
||||
await cache.storage.close(BACKGROUND_CONTEXT)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async listEntries(
|
||||
sessionId: string,
|
||||
options: { after?: EntryId; limit?: number; cursor?: Cursor } = {},
|
||||
) {
|
||||
return this.read(sessionId, async (storage) => {
|
||||
if (!(await storage.conversation(ROOT_CONVERSATION_ID, BACKGROUND_CONTEXT)))
|
||||
return { items: [], next: undefined }
|
||||
return storage.scanEntries(
|
||||
{
|
||||
conversationId: ROOT_CONVERSATION_ID,
|
||||
order: 'ascending',
|
||||
...(options.after === undefined ? {} : { minEntryId: (options.after + 1) as EntryId }),
|
||||
},
|
||||
options.limit ?? 20,
|
||||
options.cursor,
|
||||
BACKGROUND_CONTEXT,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
async getEntries(sessionId: string): Promise<EntryRecord[]> {
|
||||
return this.read(sessionId, async (storage) => {
|
||||
const entries: EntryRecord[] = []
|
||||
if (!(await storage.conversation(ROOT_CONVERSATION_ID, BACKGROUND_CONTEXT))) return entries
|
||||
let cursor: Cursor | undefined
|
||||
do {
|
||||
const page = await storage.scanEntries(
|
||||
{ conversationId: ROOT_CONVERSATION_ID, order: 'ascending' },
|
||||
100,
|
||||
cursor,
|
||||
BACKGROUND_CONTEXT,
|
||||
)
|
||||
entries.push(...page.items)
|
||||
cursor = page.next
|
||||
} while (cursor)
|
||||
return entries
|
||||
})
|
||||
}
|
||||
|
||||
async getEntryForRequest(sessionId: string, requestId: string) {
|
||||
return this.read(sessionId, async (storage) => {
|
||||
const submission = await storage.submissionByRequest(
|
||||
ROOT_CONVERSATION_ID,
|
||||
requestId,
|
||||
BACKGROUND_CONTEXT,
|
||||
)
|
||||
return submission?.entry === undefined
|
||||
? undefined
|
||||
: (await storage.entry(submission.entry, BACKGROUND_CONTEXT))?.entry
|
||||
})
|
||||
}
|
||||
|
||||
async recordUsage(
|
||||
sessionId: string,
|
||||
totals: PrismaJson.AgentSessionUsage,
|
||||
owner: UsageOwner,
|
||||
transaction?: Prisma.TransactionClient,
|
||||
): Promise<PrismaJson.AgentSessionUsage> {
|
||||
const account = async (tx: Prisma.TransactionClient) => {
|
||||
await tx.$queryRaw`SELECT id FROM agent_sessions WHERE id = ${sessionId} FOR UPDATE`
|
||||
const session = await tx.agentSession.findUniqueOrThrow({
|
||||
where: { id: sessionId },
|
||||
select: { accountedUsage: true },
|
||||
})
|
||||
const previous = session.accountedUsage
|
||||
const delta = {
|
||||
inputTokens: Math.max(0, totals.inputTokens - (previous?.inputTokens ?? 0)),
|
||||
outputTokens: Math.max(0, totals.outputTokens - (previous?.outputTokens ?? 0)),
|
||||
cacheReadTokens: Math.max(0, totals.cacheReadTokens - (previous?.cacheReadTokens ?? 0)),
|
||||
totalTokens: Math.max(0, totals.totalTokens - (previous?.totalTokens ?? 0)),
|
||||
cost: Math.max(0, totals.cost - (previous?.cost ?? 0)),
|
||||
}
|
||||
if (delta.totalTokens || delta.cost) {
|
||||
await new AiUsageService(tx).recordUsage({
|
||||
teamId: owner.teamId,
|
||||
userId: owner.userId,
|
||||
...delta,
|
||||
})
|
||||
await quotaService.consumeQuota(
|
||||
{ ...owner, resource: 'agent_total_tokens' },
|
||||
delta.totalTokens,
|
||||
tx,
|
||||
)
|
||||
await quotaService.consumeQuota({ ...owner, resource: 'agent_cost' }, delta.cost, tx)
|
||||
}
|
||||
await tx.agentSession.update({ where: { id: sessionId }, data: { accountedUsage: totals } })
|
||||
return delta
|
||||
}
|
||||
return transaction ? account(transaction) : prisma.$transaction(account)
|
||||
}
|
||||
}
|
||||
|
||||
export const agentSessionService = new AgentSessionService()
|
||||
@@ -0,0 +1,224 @@
|
||||
import { hydrateRequest } from './session-content'
|
||||
import { BACKGROUND_CONTEXT as context } from '@earendil-works/chord/context'
|
||||
import {
|
||||
createModels,
|
||||
fauxAssistantMessage,
|
||||
fauxProvider,
|
||||
fauxToolCall,
|
||||
} from '@earendil-works/pi-ai'
|
||||
import {
|
||||
createRegistry,
|
||||
defineExtension,
|
||||
Harness,
|
||||
GenerationTask,
|
||||
hook,
|
||||
ROOT_CONVERSATION_ID,
|
||||
type StorageWrite,
|
||||
} from '@earendil-works/pi-durable'
|
||||
import { openNodeSqliteStorage } from '@earendil-works/pi-durable/storage/sqlite/node'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { mkdtemp, rm } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { s3Service } from '../s3/s3'
|
||||
import { agentService } from './agent'
|
||||
import { hydrateStorage, synchronizeStorage } from './session-storage'
|
||||
|
||||
describe('native Durable content storage', () => {
|
||||
setupTestDbHooks()
|
||||
afterEach(() => vi.restoreAllMocks())
|
||||
|
||||
it('keeps an uncertain commit dirty and refuses subsequent writes', async () => {
|
||||
const directory = await mkdtemp(join(tmpdir(), 'shumai-failed-session-'))
|
||||
const dirty = vi.fn(async () => {})
|
||||
const clean = vi.fn(async () => {})
|
||||
const persist = vi.fn(async () => {
|
||||
throw new Error('mirror unavailable')
|
||||
})
|
||||
const storage = synchronizeStorage(
|
||||
await openNodeSqliteStorage(join(directory, 'session.sqlite')),
|
||||
{
|
||||
dirty,
|
||||
clean,
|
||||
persist,
|
||||
},
|
||||
)
|
||||
const acknowledge = vi.fn()
|
||||
try {
|
||||
await expect(
|
||||
storage.commit(
|
||||
[[{ type: 'conversation', value: { id: ROOT_CONVERSATION_ID } }]],
|
||||
acknowledge,
|
||||
context,
|
||||
),
|
||||
).rejects.toThrow('mirror unavailable')
|
||||
expect(await storage.conversation(ROOT_CONVERSATION_ID, context)).toBeDefined()
|
||||
await expect(storage.commit([[]], acknowledge, context)).rejects.toThrow('mirror unavailable')
|
||||
expect(dirty).toHaveBeenCalledTimes(1)
|
||||
expect(persist).toHaveBeenCalledTimes(1)
|
||||
expect(clean).not.toHaveBeenCalled()
|
||||
expect(acknowledge).not.toHaveBeenCalled()
|
||||
} finally {
|
||||
await storage.close(context)
|
||||
await rm(directory, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('hydrates generation and compaction after reopening while both persisted copies stay compact', async () => {
|
||||
const directory = await mkdtemp(join(tmpdir(), 'shumai-content-'))
|
||||
const path = join(directory, 'session.sqlite')
|
||||
const commits: StorageWrite[][] = []
|
||||
const text = 'Resolved skill instructions '.repeat(100)
|
||||
vi.spyOn(agentService, 'getSkillContent').mockResolvedValue(text)
|
||||
vi.spyOn(s3Service, 'getObject').mockResolvedValue({
|
||||
buffer: Buffer.from('image bytes'),
|
||||
contentType: 'image/png',
|
||||
})
|
||||
const faux = fauxProvider({
|
||||
models: [{ id: 'test', input: ['text', 'image'], contextWindow: 100000, maxTokens: 1024 }],
|
||||
})
|
||||
const models = createModels()
|
||||
models.setProvider(faux.provider)
|
||||
const registry = createRegistry()
|
||||
registry.install(
|
||||
defineExtension({
|
||||
name: 'shumai',
|
||||
hooks: [
|
||||
hook(GenerationTask, {
|
||||
beforeRequest: async ({ messages }, api, context) => ({
|
||||
messages: await hydrateRequest(messages, api, context),
|
||||
}),
|
||||
}),
|
||||
],
|
||||
tools: [
|
||||
{
|
||||
name: 'read_skill',
|
||||
description: 'Read a skill',
|
||||
parameters: { type: 'object', properties: {} },
|
||||
execute: async () => ({
|
||||
output: [{ type: 'text', text }],
|
||||
details: { skillId: 'skill' },
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: 'read_asset',
|
||||
description: 'Read an image',
|
||||
parameters: { type: 'object', properties: {} },
|
||||
execute: async () => ({
|
||||
output: [
|
||||
{
|
||||
type: 'image',
|
||||
data: Buffer.from('image bytes').toString('base64'),
|
||||
mimeType: 'image/png',
|
||||
},
|
||||
],
|
||||
details: { sourceKeys: ['image-key'] },
|
||||
}),
|
||||
},
|
||||
],
|
||||
}),
|
||||
)
|
||||
const open = async () =>
|
||||
Harness.open(
|
||||
hydrateStorage(
|
||||
synchronizeStorage(await openNodeSqliteStorage(path), {
|
||||
dirty: async () => {},
|
||||
clean: async () => {},
|
||||
persist: async (accepted) => {
|
||||
commits.push(
|
||||
...accepted.map(
|
||||
({ writes }) => JSON.parse(JSON.stringify(writes)) as StorageWrite[],
|
||||
),
|
||||
)
|
||||
},
|
||||
}),
|
||||
),
|
||||
{ models, registry, settings: () => ({ compaction: { keepRecentTokens: 1 } }) },
|
||||
context,
|
||||
)
|
||||
let harness = await open()
|
||||
try {
|
||||
faux.setResponses([
|
||||
(input) => {
|
||||
expect(JSON.stringify(input.messages)).toContain(
|
||||
Buffer.from('image bytes').toString('base64'),
|
||||
)
|
||||
return fauxAssistantMessage(
|
||||
[fauxToolCall('read_skill', {}), fauxToolCall('read_asset', {})],
|
||||
{ stopReason: 'toolUse' },
|
||||
)
|
||||
},
|
||||
(input) => {
|
||||
expect(JSON.stringify(input.messages)).toContain('Resolved skill instructions')
|
||||
expect(JSON.stringify(input.messages)).toContain(
|
||||
Buffer.from('image bytes').toString('base64'),
|
||||
)
|
||||
return fauxAssistantMessage('First answer')
|
||||
},
|
||||
])
|
||||
let root = await harness.root(context, {
|
||||
agent: { model: { provider: faux.provider.id, modelId: 'test' } },
|
||||
})
|
||||
expect(
|
||||
(
|
||||
await (
|
||||
await root.submit(
|
||||
{
|
||||
type: 'input',
|
||||
content: [
|
||||
{ type: 'text', text: 'Read the skill and image' },
|
||||
{ type: 'image', data: '__S3_DATA__', mimeType: 'image/png' },
|
||||
],
|
||||
entry: {
|
||||
kind: 'shumai.message',
|
||||
data: {
|
||||
displayText: 'Read the skill and image',
|
||||
timestamp: 1,
|
||||
context: {},
|
||||
sourceKeys: ['image-key'],
|
||||
},
|
||||
},
|
||||
},
|
||||
context,
|
||||
)
|
||||
).wait(context)
|
||||
).status,
|
||||
).toBe('done')
|
||||
await harness.close(context)
|
||||
expect(JSON.stringify(commits)).toContain('__S3_DATA__')
|
||||
expect(JSON.stringify(commits)).toContain('__SKILL_CONTENT__')
|
||||
expect(JSON.stringify(commits)).not.toContain(text)
|
||||
expect(JSON.stringify(commits)).not.toContain(Buffer.from('image bytes').toString('base64'))
|
||||
harness = await open()
|
||||
root = await harness.root(context)
|
||||
faux.setResponses([
|
||||
(input) => {
|
||||
expect(JSON.stringify(input.messages)).toContain('Resolved skill instructions')
|
||||
expect(JSON.stringify(input.messages)).not.toContain('__SKILL_CONTENT__')
|
||||
return fauxAssistantMessage('Summary')
|
||||
},
|
||||
])
|
||||
const compact = await root.compact(undefined, context)
|
||||
expect((await harness.waitForTask(compact, context)).state.status).toBe('terminal')
|
||||
await harness.close(context)
|
||||
const native = await openNodeSqliteStorage(path)
|
||||
try {
|
||||
const entries = await native.scanEntries(
|
||||
{ conversationId: root.id },
|
||||
100,
|
||||
undefined,
|
||||
context,
|
||||
)
|
||||
const stored = JSON.stringify(entries.items)
|
||||
expect(stored).toContain('__SKILL_CONTENT__')
|
||||
expect(stored).not.toContain(text)
|
||||
} finally {
|
||||
await native.close(context)
|
||||
}
|
||||
} finally {
|
||||
await harness.close(context)
|
||||
await rm(directory, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,84 @@
|
||||
import type { Context } from '@earendil-works/chord/context'
|
||||
import type {
|
||||
ConversationId,
|
||||
EntryId,
|
||||
Seq,
|
||||
Storage,
|
||||
StorageWrite,
|
||||
} from '@earendil-works/pi-durable'
|
||||
import { compactSessionWrites, hydrateEntry } from './session-content'
|
||||
|
||||
export interface CommitReceipt {
|
||||
seq: Seq
|
||||
writes: readonly StorageWrite[]
|
||||
}
|
||||
|
||||
/** Decorate the native instance; every query and ID rule remains implemented by Pi. */
|
||||
export function synchronizeStorage(
|
||||
storage: Storage,
|
||||
callbacks: {
|
||||
dirty(): Promise<void>
|
||||
persist(commits: readonly CommitReceipt[]): Promise<void>
|
||||
clean(seq?: Seq): Promise<void>
|
||||
},
|
||||
): Storage {
|
||||
const commit = storage.commit.bind(storage)
|
||||
const close = storage.close.bind(storage)
|
||||
let pending = Promise.resolve()
|
||||
let failure: { error: unknown } | undefined
|
||||
let closing: Promise<void> | undefined
|
||||
storage.commit = (batches, receive, context) => {
|
||||
if (closing) return Promise.reject(new Error('Session storage is closing'))
|
||||
const current = pending.then(async () => {
|
||||
if (failure) throw failure.error
|
||||
try {
|
||||
await callbacks.dirty()
|
||||
const compact = batches.map(compactSessionWrites)
|
||||
const receipts: Array<{ index: number; seq: Seq }> = []
|
||||
const result = await commit(compact, (index, seq) => receipts.push({ index, seq }), context)
|
||||
await callbacks.persist(receipts.map(({ index, seq }) => ({ seq, writes: compact[index] })))
|
||||
await callbacks.clean(receipts.at(-1)?.seq)
|
||||
for (const { index, seq } of receipts) receive(index, seq)
|
||||
return result
|
||||
} catch (error) {
|
||||
// An uncertain local commit must never be marked clean by a later write.
|
||||
failure = { error }
|
||||
throw error
|
||||
}
|
||||
})
|
||||
pending = current.then(
|
||||
() => {},
|
||||
() => {},
|
||||
)
|
||||
return current
|
||||
}
|
||||
storage.close = (context) => (closing ??= pending.then(() => close(context)))
|
||||
return storage
|
||||
}
|
||||
|
||||
/** Runtime reads hydrate detached entries; API reads use the compact native instance. */
|
||||
export function hydrateStorage(storage: Storage): Storage {
|
||||
const entry = storage.entry.bind(storage)
|
||||
const scan = storage.scanEntries.bind(storage)
|
||||
const head = storage.findLatestHeadMarker.bind(storage)
|
||||
storage.entry = (async (
|
||||
first: EntryId | ConversationId,
|
||||
second: EntryId | Context,
|
||||
third?: Context,
|
||||
) => {
|
||||
const record =
|
||||
third === undefined
|
||||
? await entry(first as EntryId, second as Context)
|
||||
: await entry(first as ConversationId, second as EntryId, third)
|
||||
return record ? { ...record, entry: await hydrateEntry(record.entry) } : undefined
|
||||
}) as Storage['entry']
|
||||
storage.scanEntries = async (...args) => {
|
||||
const page = await scan(...args)
|
||||
return { ...page, items: await Promise.all(page.items.map(hydrateEntry)) }
|
||||
}
|
||||
storage.findLatestHeadMarker = async (...args) => {
|
||||
const found = await head(...args)
|
||||
return found ? { ...(await hydrateEntry(found)), head: found.head } : undefined
|
||||
}
|
||||
return storage
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { BACKGROUND_CONTEXT } from '@earendil-works/chord/context'
|
||||
import { createModels } from '@earendil-works/pi-ai'
|
||||
import { Harness, createRegistry, type EntryDraft } from '@earendil-works/pi-durable'
|
||||
import { agentSessionService } from './session-service'
|
||||
|
||||
/** Seed real native records through the same writer used by the application. */
|
||||
export async function appendSessionEntries(sessionId: string, drafts: EntryDraft[]) {
|
||||
return 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)
|
||||
return await root.commit(async (tx) => {
|
||||
const entries = []
|
||||
for (const draft of drafts) entries.push(await tx.appendEntry(root.id, draft))
|
||||
return entries
|
||||
}, BACKGROUND_CONTEXT)
|
||||
} finally {
|
||||
await harness.close(BACKGROUND_CONTEXT)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { BACKGROUND_CONTEXT } from '@earendil-works/chord/context'
|
||||
import { OutputBuffer } from '@earendil-works/chord/output'
|
||||
import type {
|
||||
Harness,
|
||||
JsonObject,
|
||||
ToolExecutionApi,
|
||||
ToolExecutionResult,
|
||||
ToolRegistration,
|
||||
} from '@earendil-works/pi-durable'
|
||||
|
||||
/** Exercise a tool with the native execution API while keeping assertions focused on its result. */
|
||||
export async function invokeTool<T extends ToolRegistration>(
|
||||
tool: T,
|
||||
callId: string,
|
||||
args: Parameters<T['execute']>[0],
|
||||
signal?: AbortSignal,
|
||||
onUpdate?: (update: {
|
||||
content: NonNullable<ToolExecutionResult['output']>
|
||||
details?: unknown
|
||||
}) => void,
|
||||
) {
|
||||
const buffer = new OutputBuffer({
|
||||
maxBytes: tool.outputLimits?.maxBytes ?? 50000,
|
||||
maxLines: tool.outputLimits?.maxLines ?? 2000,
|
||||
retain: tool.outputLimits?.retain ?? 'head',
|
||||
})
|
||||
let details: unknown
|
||||
const notify = () =>
|
||||
onUpdate?.({
|
||||
content: buffer.snapshot().text ? [{ type: 'text', text: buffer.snapshot().text }] : [],
|
||||
details,
|
||||
})
|
||||
const api = {
|
||||
callId,
|
||||
output: (chunk: string | Uint8Array) => {
|
||||
buffer.push(chunk)
|
||||
notify()
|
||||
},
|
||||
details: async (value: unknown) => {
|
||||
details = value
|
||||
notify()
|
||||
},
|
||||
retainedOutput: () => ({ text: buffer.snapshot().text, truncated: false }),
|
||||
diagnostic: () => {},
|
||||
} as unknown as ToolExecutionApi
|
||||
const result = await tool.execute(args, api, { ...BACKGROUND_CONTEXT, abortSignal: signal })
|
||||
return {
|
||||
...result,
|
||||
details: result.details as Awaited<ReturnType<T['execute']>>['details'],
|
||||
content: result.output ?? [{ type: 'text' as const, text: buffer.snapshot().text }],
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAgentTools(harness: Harness) {
|
||||
return (await (await harness.root(BACKGROUND_CONTEXT)).agent(BACKGROUND_CONTEXT)).tools
|
||||
}
|
||||
|
||||
export async function getAgentPrompt(harness: Harness) {
|
||||
return (
|
||||
(await (await harness.root(BACKGROUND_CONTEXT)).agent(BACKGROUND_CONTEXT)).instructions ?? ''
|
||||
)
|
||||
}
|
||||
|
||||
export function jsonData(value: unknown): JsonObject {
|
||||
return JSON.parse(JSON.stringify(value)) as JsonObject
|
||||
}
|
||||
@@ -20,6 +20,7 @@ export interface GetTeamUsageStatsParams {
|
||||
}
|
||||
|
||||
export class AiUsageService {
|
||||
constructor(private readonly prismaClient: Prisma.TransactionClient = prisma) {}
|
||||
private async incrementBucket(params: {
|
||||
teamId: string
|
||||
userId: string | null
|
||||
@@ -31,7 +32,7 @@ export class AiUsageService {
|
||||
cost: number
|
||||
}): Promise<void> {
|
||||
if (params.userId !== null) {
|
||||
await prisma.aiUsage.upsert({
|
||||
await this.prismaClient.aiUsage.upsert({
|
||||
where: {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
teamId_userId_periodStart: {
|
||||
@@ -59,7 +60,7 @@ export class AiUsageService {
|
||||
},
|
||||
})
|
||||
} else {
|
||||
const existing = await prisma.aiUsage.findFirst({
|
||||
const existing = await this.prismaClient.aiUsage.findFirst({
|
||||
where: {
|
||||
teamId: params.teamId,
|
||||
userId: null,
|
||||
@@ -68,7 +69,7 @@ export class AiUsageService {
|
||||
})
|
||||
|
||||
if (existing) {
|
||||
await prisma.aiUsage.update({
|
||||
await this.prismaClient.aiUsage.update({
|
||||
where: { id: existing.id },
|
||||
data: {
|
||||
inputTokens: { increment: params.inputTokens },
|
||||
@@ -80,7 +81,7 @@ export class AiUsageService {
|
||||
})
|
||||
} else {
|
||||
try {
|
||||
await prisma.aiUsage.create({
|
||||
await this.prismaClient.aiUsage.create({
|
||||
data: {
|
||||
teamId: params.teamId,
|
||||
userId: null,
|
||||
@@ -94,7 +95,7 @@ export class AiUsageService {
|
||||
})
|
||||
} catch (err) {
|
||||
if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === 'P2002') {
|
||||
await prisma.aiUsage.updateMany({
|
||||
await this.prismaClient.aiUsage.updateMany({
|
||||
where: {
|
||||
teamId: params.teamId,
|
||||
userId: null,
|
||||
@@ -173,7 +174,7 @@ export class AiUsageService {
|
||||
|
||||
if (!params.userId) {
|
||||
// Return Team-Level Usage (userId = null)
|
||||
const teamRecords = await prisma.aiUsage.findMany({
|
||||
const teamRecords = await this.prismaClient.aiUsage.findMany({
|
||||
where: {
|
||||
teamId: params.teamId,
|
||||
userId: null,
|
||||
@@ -219,7 +220,7 @@ export class AiUsageService {
|
||||
throw new HTTPException(404, { message: 'Team member not found' })
|
||||
}
|
||||
|
||||
const memberRecords = await prisma.aiUsage.findMany({
|
||||
const memberRecords = await this.prismaClient.aiUsage.findMany({
|
||||
where: {
|
||||
teamId: params.teamId,
|
||||
userId: params.userId,
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import type { ShumaiMessageContext } from '@shumai/dtos'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { degreeToSecond } from '@shumai/core/src/utils/model3d'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { getShumaiMessage } from '@shumai/dtos'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { agentSessionService } from '../agent/session-service'
|
||||
|
||||
/**
|
||||
* Resolve the visual markup referenced by an annotation ID: a comment (comments mode) or an
|
||||
@@ -32,15 +33,13 @@ export async function resolveAnnotationsById(
|
||||
return { annotations, timestamp: comment.second }
|
||||
}
|
||||
|
||||
// 2. Check AgentSessionEntry table (1-on-1 Chat Mode)
|
||||
const entry = await prisma.agentSessionEntry.findUnique({
|
||||
where: { id: annotationId },
|
||||
select: { assetId: true, data: true },
|
||||
})
|
||||
const reference = /^chat:([^:]+):(.+)$/.exec(annotationId)
|
||||
const entry = reference
|
||||
? await agentSessionService.getEntryForRequest(reference[1], reference[2])
|
||||
: undefined
|
||||
if (entry) {
|
||||
const details = (entry.data as Record<string, unknown> | null)?.details as
|
||||
ShumaiMessageContext | undefined
|
||||
const entryAssetId = entry.assetId || details?.currentAsset?.id
|
||||
const details = getShumaiMessage(entry)?.context
|
||||
const entryAssetId = details?.currentAsset?.id
|
||||
if (entryAssetId && entryAssetId !== assetId) {
|
||||
throw new HTTPException(400, {
|
||||
message: `Annotation "${annotationId}" belongs to asset "${entryAssetId}", not target asset "${assetId}".`,
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { fauxAssistantMessage } from '@earendil-works/pi-ai'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { chatService, buildSessionMessages, mapEntryToMessage, type PathEntry } from './chat'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { workflowService } from '@shumai/workflow-core'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { appendSessionEntries } from '../agent/session-test-utils'
|
||||
import { chatService } from './chat'
|
||||
|
||||
describe('ChatService', () => {
|
||||
setupTestDbHooks()
|
||||
@@ -340,141 +342,33 @@ describe('ChatService', () => {
|
||||
expect(list.data).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('should list messages mapped correctly', async () => {
|
||||
it('returns native entries with structured context and model messages', async () => {
|
||||
const { user, team, project } = await setupBasicData()
|
||||
|
||||
const { sessionId } = await chatService.startOrContinueChat(user, team.id, {
|
||||
agentId: 'test-agent-id',
|
||||
textPrompt: 'hello',
|
||||
projectId: project.id,
|
||||
})
|
||||
|
||||
// Manually insert a test entry representing user message
|
||||
await prisma.agentSessionEntry.create({
|
||||
data: {
|
||||
id: 'test-entry-id',
|
||||
sessionId,
|
||||
type: 'message',
|
||||
parentId: null,
|
||||
data: {
|
||||
message: {
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: 'hello' }],
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
} as PrismaJson.PiSessionEntryData,
|
||||
await appendSessionEntries(sessionId, [
|
||||
{
|
||||
kind: 'shumai.message',
|
||||
data: { displayText: 'hello', timestamp: 1, context: {} },
|
||||
model: [{ role: 'user', content: 'hello', timestamp: 1 }],
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.agentSession.update({
|
||||
where: { id: sessionId },
|
||||
data: { leafId: 'test-entry-id' },
|
||||
})
|
||||
|
||||
{ kind: 'pi.assistant', model: [fauxAssistantMessage('answer')] },
|
||||
{ kind: 'pi.compaction', data: { reason: 'manual' } },
|
||||
])
|
||||
const messages = await chatService.listMessages(user.id, team.id, sessionId)
|
||||
expect(messages).toHaveLength(1)
|
||||
// Casting to any to access properties of union in test assertions
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const msg = messages[0] as any
|
||||
expect(msg.role).toBe('user')
|
||||
expect(msg.content).toEqual([{ type: 'text', text: 'hello' }])
|
||||
})
|
||||
|
||||
it('should hide customType context messages', async () => {
|
||||
const { user, team, project } = await setupBasicData()
|
||||
|
||||
const { sessionId } = await chatService.startOrContinueChat(user, team.id, {
|
||||
agentId: 'test-agent-id',
|
||||
textPrompt: 'hello',
|
||||
projectId: project.id,
|
||||
expect(messages.map((entry) => entry.kind)).toEqual([
|
||||
'shumai.message',
|
||||
'pi.assistant',
|
||||
'pi.compaction',
|
||||
])
|
||||
expect(messages[0]).toMatchObject({
|
||||
data: { displayText: 'hello' },
|
||||
model: [{ role: 'user', content: 'hello' }],
|
||||
})
|
||||
|
||||
// 1. Manually insert a test entry representing user message
|
||||
await prisma.agentSessionEntry.create({
|
||||
data: {
|
||||
id: 'test-entry-1-user',
|
||||
sessionId,
|
||||
type: 'message',
|
||||
parentId: null,
|
||||
data: {
|
||||
message: {
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: 'hello' }],
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
} as PrismaJson.PiSessionEntryData,
|
||||
},
|
||||
})
|
||||
|
||||
// 2. Manually insert a test entry representing a custom context message
|
||||
await prisma.agentSessionEntry.create({
|
||||
data: {
|
||||
id: 'test-entry-2-context',
|
||||
sessionId,
|
||||
type: 'custom_message',
|
||||
parentId: 'test-entry-1-user',
|
||||
data: {
|
||||
customType: 'context',
|
||||
content: 'some context',
|
||||
display: 'chat',
|
||||
} as PrismaJson.PiSessionEntryData,
|
||||
},
|
||||
})
|
||||
|
||||
// 3. Manually insert a test entry representing a custom thinking level change
|
||||
await prisma.agentSessionEntry.create({
|
||||
data: {
|
||||
id: 'test-entry-3-thinking',
|
||||
sessionId,
|
||||
type: 'thinking_level_change',
|
||||
parentId: 'test-entry-2-context',
|
||||
data: {
|
||||
thinkingLevel: 'deep',
|
||||
} as PrismaJson.PiSessionEntryData,
|
||||
},
|
||||
})
|
||||
|
||||
// 4. Manually insert a test entry representing custom context_display_info entry
|
||||
await prisma.agentSessionEntry.create({
|
||||
data: {
|
||||
id: 'test-entry-4-display',
|
||||
sessionId,
|
||||
type: 'custom',
|
||||
parentId: 'test-entry-3-thinking',
|
||||
data: {
|
||||
customType: 'context_display_info',
|
||||
data: {
|
||||
assets: [{ id: 'a1', name: 'File A', type: 'file' }],
|
||||
},
|
||||
} as PrismaJson.PiSessionEntryData,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.agentSession.update({
|
||||
where: { id: sessionId },
|
||||
data: { leafId: 'test-entry-4-display' },
|
||||
})
|
||||
|
||||
// Verify listMessages returns only the user, thinking level change and custom display entry message
|
||||
const messages = await chatService.listMessages(user.id, team.id, sessionId)
|
||||
expect(messages).toHaveLength(3)
|
||||
expect(messages[0].id).toBe('test-entry-1-user')
|
||||
expect(messages[1].id).toBe('test-entry-3-thinking')
|
||||
expect(messages[2].id).toBe('test-entry-4-display')
|
||||
expect(messages[2].role).toBe('custom')
|
||||
expect((messages[2] as { customType?: string }).customType).toBe('context_display_info')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((messages[2] as any).details).toEqual({
|
||||
assets: [{ id: 'a1', name: 'File A', type: 'file' }],
|
||||
})
|
||||
|
||||
// Verify mapEntryToMessage returns null for context message
|
||||
const contextRecord = await prisma.agentSessionEntry.findUnique({
|
||||
where: { id: 'test-entry-2-context' },
|
||||
})
|
||||
expect(contextRecord).not.toBeNull()
|
||||
const mapped = mapEntryToMessage(contextRecord!)
|
||||
expect(mapped).toBeNull()
|
||||
expect(typeof messages[0].id).toBe('number')
|
||||
})
|
||||
|
||||
it('should delete session and cascade', async () => {
|
||||
@@ -499,167 +393,6 @@ describe('ChatService', () => {
|
||||
expect(entries).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('should build session messages correctly handling compaction and custom messages', () => {
|
||||
const pathEntries = [
|
||||
{
|
||||
id: 'entry-1',
|
||||
type: 'message',
|
||||
timestamp: '2026-07-07T00:00:00.000Z',
|
||||
message: {
|
||||
role: 'user',
|
||||
content: 'first message',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'entry-compaction',
|
||||
type: 'compaction',
|
||||
timestamp: '2026-07-07T00:01:00.000Z',
|
||||
summary: 'Compacted conversation history',
|
||||
tokensBefore: 200,
|
||||
firstKeptEntryId: 'entry-2',
|
||||
},
|
||||
{
|
||||
id: 'entry-2',
|
||||
type: 'message',
|
||||
timestamp: '2026-07-07T00:02:00.000Z',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: 'kept message',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'entry-3',
|
||||
type: 'thinking_level_change',
|
||||
timestamp: '2026-07-07T00:03:00.000Z',
|
||||
thinkingLevel: 'high',
|
||||
},
|
||||
{
|
||||
id: 'entry-4',
|
||||
type: 'branch_summary',
|
||||
timestamp: '2026-07-07T00:04:00.000Z',
|
||||
summary: 'Branch summary details',
|
||||
fromId: 'entry-old',
|
||||
},
|
||||
]
|
||||
|
||||
const messages = buildSessionMessages(pathEntries as unknown as PathEntry[])
|
||||
|
||||
expect(messages).toHaveLength(4)
|
||||
|
||||
// 1. Compaction summary message
|
||||
// Casting to any to access properties of union in test assertions
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const msg0 = messages[0] as any
|
||||
expect(msg0.role).toBe('custom')
|
||||
expect(msg0.customType).toBe('compaction-summary')
|
||||
expect(msg0.content).toBe('Compacted conversation history')
|
||||
expect((msg0.details as Record<string, unknown>).tokensBefore).toBe(200)
|
||||
|
||||
// 2. Kept entry (entry-2)
|
||||
// Casting to any to access properties of union in test assertions
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const msg1 = messages[1] as any
|
||||
expect(msg1.id).toBe('entry-2')
|
||||
expect(msg1.role).toBe('assistant')
|
||||
expect(msg1.content).toBe('kept message')
|
||||
|
||||
// 3. Thinking level change (entry-3)
|
||||
// Casting to any to access properties of union in test assertions
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const msg2 = messages[2] as any
|
||||
expect(msg2.role).toBe('thinking_level_change')
|
||||
expect(msg2.content).toContain('Thinking level changed to high')
|
||||
|
||||
// 4. Branch summary (entry-4)
|
||||
// Casting to any to access properties of union in test assertions
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const msg3 = messages[3] as any
|
||||
expect(msg3.role).toBe('custom')
|
||||
expect(msg3.customType).toBe('branch-summary')
|
||||
expect(msg3.content).toBe('Branch summary details')
|
||||
expect((msg3.details as Record<string, unknown>).fromId).toBe('entry-old')
|
||||
})
|
||||
|
||||
it('should render active_tools_change entries as custom messages', () => {
|
||||
const pathEntries = [
|
||||
{
|
||||
id: 'entry-tools',
|
||||
type: 'active_tools_change',
|
||||
timestamp: '2026-07-07T00:05:00.000Z',
|
||||
activeToolNames: ['read_skill', 'bash'],
|
||||
},
|
||||
]
|
||||
|
||||
const messages = buildSessionMessages(pathEntries as unknown as PathEntry[])
|
||||
expect(messages).toHaveLength(1)
|
||||
// Casting to any to access properties of union in test assertions
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const msg = messages[0] as any
|
||||
expect(msg.role).toBe('custom')
|
||||
expect(msg.customType).toBe('active-tools-change')
|
||||
expect(msg.content).toBe('Active tools: read_skill, bash')
|
||||
expect(msg.details).toEqual({ activeToolNames: ['read_skill', 'bash'] })
|
||||
})
|
||||
|
||||
it('should return null for entry types without a chat representation instead of an empty message', () => {
|
||||
const leafRecord = {
|
||||
id: 'entry-leaf',
|
||||
type: 'leaf',
|
||||
parentId: null,
|
||||
createdAt: new Date('2026-07-07T00:05:00.000Z'),
|
||||
data: { targetId: 'some-entry' },
|
||||
} as unknown as Parameters<typeof mapEntryToMessage>[0]
|
||||
|
||||
expect(mapEntryToMessage(leafRecord)).toBeNull()
|
||||
})
|
||||
|
||||
it('should map shumai_message custom_message entries with clean text and structured details', () => {
|
||||
const entryRecord = {
|
||||
id: 'entry-shumai-msg',
|
||||
type: 'custom_message',
|
||||
parentId: null,
|
||||
createdAt: new Date('2026-07-07T00:05:00.000Z'),
|
||||
data: {
|
||||
customType: 'shumai_message',
|
||||
content: 'Please adjust color grading',
|
||||
display: true,
|
||||
details: {
|
||||
user: { id: 'u1', name: 'Alice', role: 'editor' },
|
||||
currentAsset: { id: 'a1', name: 'video.mp4', type: 'file' },
|
||||
position: { type: 'time', seconds: 12.5 },
|
||||
},
|
||||
},
|
||||
} as unknown as Parameters<typeof mapEntryToMessage>[0]
|
||||
|
||||
const msg = mapEntryToMessage(entryRecord)
|
||||
expect(msg).toBeDefined()
|
||||
expect(msg?.id).toBe('entry-shumai-msg')
|
||||
expect(msg?.role).toBe('custom')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((msg as any)?.customType).toBe('shumai_message')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((msg as any)?.content).toBe('Please adjust color grading')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((msg as any)?.details?.user?.name).toBe('Alice')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((msg as any)?.details?.position?.seconds).toBe(12.5)
|
||||
})
|
||||
|
||||
it('should skip legacy context custom_message in mapEntryToMessage', () => {
|
||||
const entryRecord = {
|
||||
id: 'entry-legacy-context',
|
||||
type: 'custom_message',
|
||||
parentId: null,
|
||||
createdAt: new Date('2026-07-07T00:05:00.000Z'),
|
||||
data: {
|
||||
customType: 'context',
|
||||
content: 'System context prompt...',
|
||||
},
|
||||
} as unknown as Parameters<typeof mapEntryToMessage>[0]
|
||||
|
||||
expect(mapEntryToMessage(entryRecord)).toBeNull()
|
||||
})
|
||||
|
||||
describe('getNewSessionMessages', () => {
|
||||
it('returns new messages and lastEntryId', async () => {
|
||||
const { user, team, project } = await setupBasicData()
|
||||
@@ -669,53 +402,27 @@ describe('ChatService', () => {
|
||||
projectId: project.id,
|
||||
})
|
||||
|
||||
// Insert two entries
|
||||
const entry1 = await prisma.agentSessionEntry.create({
|
||||
data: {
|
||||
id: 'entry-id-1',
|
||||
sessionId,
|
||||
type: 'message',
|
||||
parentId: null,
|
||||
data: {
|
||||
message: {
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: 'msg 1' }],
|
||||
},
|
||||
} as PrismaJson.PiSessionEntryData,
|
||||
},
|
||||
})
|
||||
|
||||
const entry2 = await prisma.agentSessionEntry.create({
|
||||
data: {
|
||||
id: 'entry-id-2',
|
||||
sessionId,
|
||||
type: 'message',
|
||||
parentId: 'entry-id-1',
|
||||
data: {
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: [{ type: 'text', text: 'msg 2' }],
|
||||
},
|
||||
} as PrismaJson.PiSessionEntryData,
|
||||
},
|
||||
})
|
||||
const [entry1, entry2] = await appendSessionEntries(sessionId, [
|
||||
{ kind: 'pi.user', model: [{ role: 'user', content: 'msg 1', timestamp: 1 }] },
|
||||
{ kind: 'pi.assistant', model: [fauxAssistantMessage('msg 2')] },
|
||||
])
|
||||
|
||||
// 1. Get all messages
|
||||
const res1 = await chatService.getNewSessionMessages(sessionId)
|
||||
expect(res1.messages).toHaveLength(2)
|
||||
expect(res1.lastEntryId).toBe(entry2.id)
|
||||
expect(res1.lastEntryId).toBe(String(entry2.id))
|
||||
expect(res1.messages[0].id).toBe(entry1.id)
|
||||
|
||||
// 2. Get with lastEntryId
|
||||
const res2 = await chatService.getNewSessionMessages(sessionId, entry1.id)
|
||||
const res2 = await chatService.getNewSessionMessages(sessionId, String(entry1.id))
|
||||
expect(res2.messages).toHaveLength(1)
|
||||
expect(res2.lastEntryId).toBe(entry2.id)
|
||||
expect(res2.lastEntryId).toBe(String(entry2.id))
|
||||
expect(res2.messages[0].id).toBe(entry2.id)
|
||||
|
||||
// 3. Get with latest lastEntryId
|
||||
const res3 = await chatService.getNewSessionMessages(sessionId, entry2.id)
|
||||
const res3 = await chatService.getNewSessionMessages(sessionId, String(entry2.id))
|
||||
expect(res3.messages).toHaveLength(0)
|
||||
expect(res3.lastEntryId).toBe(entry2.id)
|
||||
expect(res3.lastEntryId).toBe(String(entry2.id))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -948,47 +655,6 @@ describe('ChatService', () => {
|
||||
).rejects.toThrow(/Insufficient role/)
|
||||
})
|
||||
|
||||
it('safely maps entries without message object or with customType shumai_message', () => {
|
||||
// 1. Entry with type: 'message' but customType: 'shumai_message'
|
||||
const legacyEntry = {
|
||||
id: 'legacy-1',
|
||||
sessionId: 'sess-1',
|
||||
assetId: null,
|
||||
parentId: null,
|
||||
type: 'message',
|
||||
data: {
|
||||
customType: 'shumai_message',
|
||||
content: 'Hello legacy',
|
||||
display: true,
|
||||
details: { user: { id: 'u1', name: 'User 1' } },
|
||||
},
|
||||
createdAt: new Date(),
|
||||
}
|
||||
|
||||
const msg1 = mapEntryToMessage(legacyEntry)
|
||||
expect(msg1).toBeDefined()
|
||||
expect(msg1?.role).toBe('custom')
|
||||
if (msg1 && msg1.role === 'custom') {
|
||||
expect(msg1.customType).toBe('shumai_message')
|
||||
expect(msg1.content).toBe('Hello legacy')
|
||||
expect(msg1.details).toEqual({ user: { id: 'u1', name: 'User 1' } })
|
||||
}
|
||||
|
||||
// 2. Entry with type: 'message' but empty data / no message
|
||||
const emptyEntry = {
|
||||
id: 'empty-1',
|
||||
sessionId: 'sess-1',
|
||||
assetId: null,
|
||||
parentId: null,
|
||||
type: 'message',
|
||||
data: {},
|
||||
createdAt: new Date(),
|
||||
}
|
||||
|
||||
const msg2 = mapEntryToMessage(emptyEntry)
|
||||
expect(msg2).toBeNull()
|
||||
})
|
||||
|
||||
it('should forward annotations and second to workflow task payload', async () => {
|
||||
const { user, team, project } = await setupBasicData()
|
||||
|
||||
|
||||
+29
-204
@@ -1,170 +1,20 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import type { EntryId, EntryRecord } from '@earendil-works/pi-durable'
|
||||
import { getAgentRequiredLevel, getRoleLevel } from '@shumai/core/src/agent/permissions'
|
||||
import {
|
||||
authzService,
|
||||
Permission,
|
||||
ResourceType,
|
||||
resolveEffectiveRole,
|
||||
ResourceType,
|
||||
} from '@shumai/core/src/authz/authz'
|
||||
import { paginateQuery, type PaginatedData } from '@shumai/core/src/pagination'
|
||||
import type { ChatRequest, ChatSessionInfo, ChatMessage } from '@shumai/dtos'
|
||||
import type { SessionTreeEntry } from '@earendil-works/pi-agent-core'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import { prisma } from '@shumai/db'
|
||||
import type { ChatRequest, ChatSessionInfo } from '@shumai/dtos'
|
||||
import { workflowService } from '@shumai/workflow-core'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import { getAgentRequiredLevel, getRoleLevel } from '@shumai/core/src/agent/permissions'
|
||||
import { agentSessionService } from '../agent/session-service'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
|
||||
export type PathEntry = SessionTreeEntry & { id: string }
|
||||
|
||||
export function buildSessionMessages(pathEntries: PathEntry[]): ChatMessage[] {
|
||||
let compaction: (SessionTreeEntry & { type: 'compaction' } & { id: string }) | null = null
|
||||
|
||||
for (const entry of pathEntries) {
|
||||
if (entry.type === 'compaction') {
|
||||
compaction = entry as SessionTreeEntry & { type: 'compaction' } & { id: string }
|
||||
}
|
||||
}
|
||||
|
||||
const messages: ChatMessage[] = []
|
||||
const appendMessage = (entry: PathEntry) => {
|
||||
const timestampMs = new Date(entry.timestamp).getTime()
|
||||
if (entry.type === 'message') {
|
||||
if ((entry as unknown as { customType?: string }).customType === 'shumai_message') {
|
||||
const customEntry = entry as unknown as {
|
||||
customType: string
|
||||
content: string
|
||||
display?: boolean
|
||||
details?: Record<string, unknown>
|
||||
}
|
||||
messages.push({
|
||||
id: entry.id,
|
||||
role: 'custom',
|
||||
customType: customEntry.customType,
|
||||
content: customEntry.content,
|
||||
display: customEntry.display,
|
||||
details: customEntry.details,
|
||||
timestamp: timestampMs,
|
||||
} as unknown as ChatMessage)
|
||||
return
|
||||
}
|
||||
if (entry.message) {
|
||||
messages.push({
|
||||
...entry.message,
|
||||
id: entry.id,
|
||||
timestamp: entry.message.timestamp || timestampMs,
|
||||
} as unknown as ChatMessage)
|
||||
}
|
||||
} else if (entry.type === 'custom_message') {
|
||||
if (entry.customType === 'context') return
|
||||
messages.push({
|
||||
id: entry.id,
|
||||
role: 'custom',
|
||||
customType: entry.customType,
|
||||
content: entry.content,
|
||||
display: entry.display,
|
||||
details: entry.details as Record<string, unknown> | undefined,
|
||||
timestamp: timestampMs,
|
||||
} as unknown as ChatMessage)
|
||||
} else if (entry.type === 'custom') {
|
||||
messages.push({
|
||||
id: entry.id,
|
||||
role: 'custom',
|
||||
customType: entry.customType,
|
||||
details: (entry as { data?: unknown }).data as Record<string, unknown> | undefined,
|
||||
timestamp: timestampMs,
|
||||
} as unknown as ChatMessage)
|
||||
} else if (entry.type === 'branch_summary') {
|
||||
messages.push({
|
||||
id: entry.id,
|
||||
role: 'custom',
|
||||
customType: 'branch-summary',
|
||||
content: entry.summary,
|
||||
details: { fromId: entry.fromId },
|
||||
timestamp: timestampMs,
|
||||
} as unknown as ChatMessage)
|
||||
} else if (entry.type === 'thinking_level_change') {
|
||||
messages.push({
|
||||
id: entry.id,
|
||||
role: 'thinking_level_change',
|
||||
content: `Thinking level changed to ${entry.thinkingLevel}`,
|
||||
timestamp: timestampMs,
|
||||
} as unknown as ChatMessage)
|
||||
} else if (entry.type === 'active_tools_change') {
|
||||
messages.push({
|
||||
id: entry.id,
|
||||
role: 'custom',
|
||||
customType: 'active-tools-change',
|
||||
content: `Active tools: ${entry.activeToolNames.join(', ')}`,
|
||||
details: { activeToolNames: entry.activeToolNames },
|
||||
timestamp: timestampMs,
|
||||
} as unknown as ChatMessage)
|
||||
}
|
||||
}
|
||||
|
||||
if (compaction) {
|
||||
const compactionTimestamp = new Date(compaction.timestamp).getTime()
|
||||
messages.push({
|
||||
id: compaction.id,
|
||||
role: 'custom',
|
||||
customType: 'compaction-summary',
|
||||
content: compaction.summary,
|
||||
details: { tokensBefore: compaction.tokensBefore },
|
||||
timestamp: compactionTimestamp,
|
||||
} as unknown as ChatMessage)
|
||||
const compactionIdx = pathEntries.findIndex(
|
||||
(e) => e.type === 'compaction' && e.id === compaction?.id,
|
||||
)
|
||||
let foundFirstKept = false
|
||||
for (let i = 0; i < compactionIdx; i++) {
|
||||
const entry = pathEntries[i]
|
||||
if (entry.id === compaction.firstKeptEntryId) foundFirstKept = true
|
||||
if (foundFirstKept) appendMessage(entry)
|
||||
}
|
||||
for (let i = compactionIdx + 1; i < pathEntries.length; i++) {
|
||||
appendMessage(pathEntries[i])
|
||||
}
|
||||
} else {
|
||||
for (const entry of pathEntries) {
|
||||
appendMessage(entry)
|
||||
}
|
||||
}
|
||||
|
||||
return messages
|
||||
}
|
||||
|
||||
export function mapEntryToMessage(
|
||||
entryRecord: Prisma.AgentSessionEntryGetPayload<Record<string, never>>,
|
||||
): ChatMessage | null {
|
||||
const payload = (entryRecord.data as Record<string, unknown>) || {}
|
||||
let type = (entryRecord.type || 'message') as SessionTreeEntry['type']
|
||||
if (type === 'message' && payload.customType === 'shumai_message') {
|
||||
type = 'custom_message'
|
||||
}
|
||||
const entryObj = {
|
||||
id: entryRecord.id,
|
||||
type,
|
||||
parentId: entryRecord.parentId,
|
||||
timestamp: entryRecord.createdAt.toISOString(),
|
||||
...payload,
|
||||
} as unknown as SessionTreeEntry
|
||||
|
||||
if (
|
||||
entryObj.type === 'custom_message' &&
|
||||
(entryObj as { customType?: string }).customType === 'context'
|
||||
) {
|
||||
return null
|
||||
}
|
||||
const pathEntries = [entryObj]
|
||||
const messages = buildSessionMessages(pathEntries)
|
||||
if (messages.length > 0) {
|
||||
return messages[0]
|
||||
}
|
||||
// Entry types that have no chat representation (e.g. leaf, label, model_change,
|
||||
// or any unknown/unsupported type) are skipped rather than rendered as an empty message.
|
||||
return null
|
||||
}
|
||||
|
||||
export class ChatService {
|
||||
constructor(private readonly prismaClient: typeof prisma = prisma) {}
|
||||
|
||||
@@ -454,7 +304,7 @@ export class ChatService {
|
||||
)
|
||||
}
|
||||
|
||||
async listMessages(userId: string, teamId: string, sessionId: string): Promise<ChatMessage[]> {
|
||||
async listMessages(userId: string, teamId: string, sessionId: string): Promise<EntryRecord[]> {
|
||||
const session = await this.prismaClient.agentSession.findUnique({
|
||||
where: { id: sessionId },
|
||||
})
|
||||
@@ -465,28 +315,8 @@ export class ChatService {
|
||||
throw new Error('Unauthorized session access')
|
||||
}
|
||||
|
||||
const entries = await agentService.getSessionEntries({ sessionId })
|
||||
|
||||
const pathEntries = entries.map(
|
||||
(e: {
|
||||
id: string
|
||||
type: string | null
|
||||
parentId: string | null
|
||||
createdAt: Date
|
||||
data: unknown
|
||||
}) => {
|
||||
const payload = (e.data as Record<string, unknown>) || {}
|
||||
return {
|
||||
id: e.id,
|
||||
type: (e.type || 'message') as SessionTreeEntry['type'],
|
||||
parentId: e.parentId,
|
||||
timestamp: e.createdAt.toISOString(),
|
||||
...payload,
|
||||
} as unknown as SessionTreeEntry
|
||||
},
|
||||
)
|
||||
|
||||
return buildSessionMessages(pathEntries)
|
||||
const entries = await agentSessionService.getEntries(sessionId)
|
||||
return entries.filter((entry) => entry.kind !== 'pi.system')
|
||||
}
|
||||
|
||||
async deleteSession(userId: string, teamId: string, sessionId: string): Promise<void> {
|
||||
@@ -507,31 +337,26 @@ export class ChatService {
|
||||
async getNewSessionMessages(
|
||||
sessionId: string,
|
||||
lastEntryId?: string,
|
||||
): Promise<{ messages: ChatMessage[]; lastEntryId: string | null }> {
|
||||
const entries = await this.prismaClient.agentSessionEntry.findMany({
|
||||
where: {
|
||||
sessionId,
|
||||
...(lastEntryId ? { id: { gt: lastEntryId } } : {}),
|
||||
},
|
||||
orderBy: { id: 'asc' },
|
||||
})
|
||||
|
||||
if (entries.length === 0) {
|
||||
return { messages: [], lastEntryId: lastEntryId ?? null }
|
||||
}
|
||||
|
||||
const messages: ChatMessage[] = []
|
||||
for (const entry of entries) {
|
||||
const message = mapEntryToMessage(entry)
|
||||
if (message) {
|
||||
messages.push(message)
|
||||
): Promise<{ messages: EntryRecord[]; lastEntryId: string | null }> {
|
||||
const after = lastEntryId ? Number(lastEntryId) : undefined
|
||||
if (after !== undefined && (!Number.isSafeInteger(after) || after < 0))
|
||||
throw new Error('Invalid entry cursor')
|
||||
const messages: EntryRecord[] = []
|
||||
let cursor: import('@earendil-works/pi-durable').Cursor | undefined
|
||||
let last = lastEntryId ?? null
|
||||
do {
|
||||
const page = await agentSessionService.listEntries(sessionId, {
|
||||
after: after as EntryId | undefined,
|
||||
limit: 100,
|
||||
cursor,
|
||||
})
|
||||
for (const entry of page.items) {
|
||||
last = String(entry.id)
|
||||
if (entry.kind !== 'pi.system') messages.push(entry)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
messages,
|
||||
lastEntryId: entries[entries.length - 1].id,
|
||||
}
|
||||
cursor = page.next
|
||||
} while (cursor)
|
||||
return { messages, lastEntryId: last }
|
||||
}
|
||||
|
||||
async getChatWorkflowStatus(
|
||||
|
||||
@@ -8,9 +8,12 @@
|
||||
* discovery/auth/status and for tools not in direct mode.
|
||||
*/
|
||||
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { AgentTool, AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import type {
|
||||
ToolRegistration as AgentTool,
|
||||
ToolExecutionResult as AgentToolResult,
|
||||
} from '@earendil-works/pi-durable'
|
||||
import '@shumai/db/src/prisma-json-types'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { ToolMetadata } from './mcp-tool-registry'
|
||||
import { isToolAllowed } from './mcp-tool-registry'
|
||||
import { toToolParameters } from './mcp-tool-schema'
|
||||
@@ -24,7 +27,7 @@ export interface DirectToolCallOptions {
|
||||
args: Record<string, unknown>,
|
||||
): Promise<{
|
||||
ok: boolean
|
||||
content: AgentToolResult<Record<string, unknown>>['content']
|
||||
content: NonNullable<AgentToolResult['output']>
|
||||
}>
|
||||
}
|
||||
|
||||
@@ -63,18 +66,17 @@ export function buildDirectTools(
|
||||
const parameters = toToolParameters(tool.inputSchema)
|
||||
tools.push({
|
||||
name: tool.name,
|
||||
label: tool.name,
|
||||
description:
|
||||
tool.description || `Call ${tool.originalName} on MCP server ${opts.serverName}.`,
|
||||
parameters: parameters as unknown as import('typebox').TSchema,
|
||||
execute: async (_toolCallId, args): Promise<AgentToolResult<Record<string, unknown>>> => {
|
||||
execute: async (args): Promise<AgentToolResult> => {
|
||||
const result = await opts.callTool(
|
||||
opts.serverId,
|
||||
tool.originalName,
|
||||
args as Record<string, unknown>,
|
||||
)
|
||||
return {
|
||||
content: result.content,
|
||||
output: result.content,
|
||||
details: { server: opts.serverName, tool: tool.originalName },
|
||||
}
|
||||
},
|
||||
|
||||
@@ -7,12 +7,15 @@
|
||||
* server(list) > status.
|
||||
*/
|
||||
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { AgentTool, AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import type {
|
||||
ToolRegistration as AgentTool,
|
||||
ToolExecutionResult as AgentToolResult,
|
||||
} from '@earendil-works/pi-durable'
|
||||
import { Type } from 'typebox'
|
||||
import type { McpToolRegistry, ToolMetadata } from './mcp-tool-registry'
|
||||
import { formatSchema } from './mcp-tool-schema'
|
||||
|
||||
type ProxyToolResult = AgentToolResult<Record<string, unknown>>
|
||||
type ProxyToolResult = AgentToolResult
|
||||
|
||||
/** Truncated instructions head baked into the proxy tool description (L1). */
|
||||
const INSTRUCTIONS_SNIPPET_LENGTH = 150
|
||||
@@ -45,7 +48,7 @@ export type ConnectionOutcome =
|
||||
|
||||
export interface CallToolOutcome {
|
||||
ok: boolean
|
||||
content: AgentToolResult<Record<string, unknown>>['content']
|
||||
content: NonNullable<AgentToolResult['output']>
|
||||
}
|
||||
|
||||
export interface McpProxyToolContext {
|
||||
@@ -167,7 +170,7 @@ function parseArgs(raw: unknown): Record<string, unknown> {
|
||||
}
|
||||
|
||||
function contentText(text: string): ProxyToolResult {
|
||||
return { content: [{ type: 'text', text }], details: {} }
|
||||
return { output: [{ type: 'text', text }], details: {} }
|
||||
}
|
||||
|
||||
/** Transform MCP content blocks into model content blocks. */
|
||||
@@ -181,7 +184,7 @@ export function transformMcpContent(
|
||||
uri?: string
|
||||
name?: string
|
||||
}>,
|
||||
): AgentToolResult<Record<string, unknown>>['content'] {
|
||||
): NonNullable<AgentToolResult['output']> {
|
||||
return content.map((c) => {
|
||||
if (c.type === 'text') {
|
||||
return { type: 'text', text: c.text ?? '' }
|
||||
@@ -331,10 +334,10 @@ function renderServerListing(
|
||||
export function buildProxyTool(ctx: McpProxyToolContext): AgentTool<typeof proxyToolSchema> {
|
||||
return {
|
||||
name: 'mcp',
|
||||
label: 'MCP Tools',
|
||||
|
||||
description: buildProxyDescription(ctx.servers),
|
||||
parameters: proxyToolSchema,
|
||||
execute: async (_toolCallId, params): Promise<ProxyToolResult> => {
|
||||
execute: async (params): Promise<ProxyToolResult> => {
|
||||
const action = params.action
|
||||
const server = params.server
|
||||
const tool = params.tool
|
||||
@@ -396,10 +399,10 @@ export function buildProxyTool(ctx: McpProxyToolContext): AgentTool<typeof proxy
|
||||
parseArgs(params.args),
|
||||
)
|
||||
if (outcome.ok) {
|
||||
return { content: outcome.content, details: {} }
|
||||
return { output: outcome.content, details: {} }
|
||||
}
|
||||
return {
|
||||
content: outcome.content,
|
||||
output: outcome.content,
|
||||
details: { error: 'tool_call_failed', tool, server: resolved.serverId },
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user