Compare commits

..
92 Commits
Author SHA1 Message Date
yiling 3383079787 Release v0.1.10 2026-07-16 16:48:25 +08:00
Yiling-J f888a575b2 Merge pull request #192 from shumaiOne/feat/disable-agent-tools
feat(agent): support disabling specific tools for agents
2026-07-16 16:38:32 +08:00
yiling 0a7d5d1f46 feat(agent): support disabling specific tools for agents 2026-07-16 15:56:56 +08:00
Yiling-J 879922a9fe Merge pull request #191 from shumaiOne/feat/comment-delete-video-shortcuts
feat(comments): support comment deletion and video player shortcuts
2026-07-16 11:16:18 +08:00
yiling 58967d1a63 minor ui improvement 2026-07-16 11:06:26 +08:00
yiling e978878a90 refactor(comments): unconditionally delete S3 attachment objects directly 2026-07-16 10:59:14 +08:00
yiling ecfbfc2bac fix(shortcuts): resolve keyboard accessibility and contenteditable issues 2026-07-16 10:23:47 +08:00
yiling 490c6a3da6 refactor(comments): simplify teamId lookup and clean up comment attachments in S3 and database 2026-07-16 10:17:32 +08:00
yiling 62b1fc1d37 feat(comments): support comment deletion and video player shortcuts 2026-07-16 09:55:05 +08:00
Yiling-J fa6bf73452 Merge pull request #189 from shumaiOne/feat/workflow-e2e-tests
test(e2e): add E2E workflow tests for remaining agent and transcode services
2026-07-15 15:36:41 +08:00
yiling 25c747aa87 test(e2e): add E2E workflow tests for remaining agent and transcode services 2026-07-15 15:19:25 +08:00
Yiling-J ff7712744d Merge pull request #188 from shumaiOne/feat/workflow-e2e-tests
feat(workflow-e2e): add E2E tests for agentAutofillMedia workflow
2026-07-15 14:45:04 +08:00
yiling 6f369625d8 docs(agents): update AGENTS.md with E2E workflow testing requirements 2026-07-15 12:04:45 +08:00
yiling c6acd075e7 refactor(workflow-core): statically import Worker type from @temporalio/worker to ensure activeWorkers type-safety 2026-07-15 11:58:17 +08:00
yiling 7d9f3276f1 test(e2e): verify multiple types in autofill and check that non-autofill field is ignored 2026-07-15 11:54:29 +08:00
yiling d524a81ecd chore(vitest): exclude packages/e2e/** from root unit test suite 2026-07-15 11:40:57 +08:00
yiling 83c427e646 refactor(e2e): mock LLM prompt via AgentHarness prototype spy instead of global activity override 2026-07-15 10:52:01 +08:00
yiling b018fd0fec feat(workflow-e2e): add E2E tests for agentAutofillMedia workflow
- Setup E2E package under packages/e2e
- Use testcontainers to spin up postgres and temporal dev server
- Overwrite activities in global registry to mock AI model output
- Track temporal workers and expose shutdownWorkers() method for clean shutdown
- Integrate test:e2e:workflow into root package.json and GitHub actions CI
2026-07-15 10:14:59 +08:00
yiling 8efc9c1865 Release v0.1.9 2026-07-14 20:21:21 +08:00
Yiling-J 0eeb2f18f3 Merge pull request #187 from shumaiOne/fix/chat-api-db-queries
refactor(api/chat): remove direct Prisma query from API layer
2026-07-14 16:47:44 +08:00
yiling 6643d63e81 refactor(api/chat): remove direct Prisma query from API layer
API layer routes in packages/api/src/chat.ts accessed the database directly via prisma, violating communication layer boundaries.

This refactoring migrates these queries to packages/core/src/chat/chat.ts:
- Added getNewSessionMessages to fetch mapped SSE entries.
- Added getChatWorkflowStatus to check task completions.
- Added abortSession to handle workflow cancellation and task updates in core.
- Updated both core and API tests to match refactored signatures and mock boundaries.
2026-07-14 16:40:55 +08:00
Yiling-J b53b76bc6e Merge pull request #186 from shumaiOne/feat/agent-abort
feat(agent): support aborting agent execution
2026-07-14 16:35:19 +08:00
yiling 636e92af4c fix(agent): unregister temporal cancellation listener on completion 2026-07-14 16:08:43 +08:00
yiling a7dc7bbff2 perf(agent): optimize active task lookup using indexed sessionId column 2026-07-14 15:44:10 +08:00
yiling cd48d2d389 fix(agent): resolve lint, typecheck, and stop button UI issues 2026-07-14 10:35:25 +08:00
yiling 8a942d4f4e feat(agent): support aborting agent execution in local and temporal modes 2026-07-14 10:25:06 +08:00
Yiling-J e772581542 Merge pull request #185 from shumaiOne/feat/chatbot-ui-layout
feat(chat): chatbot UI
2026-07-13 16:55:25 +08:00
yiling 7baf95e48c fix(webui): preserve chatbot sidebar scroll position across navigations 2026-07-13 16:49:04 +08:00
yiling 15cbf12162 fix(webui): render theme-aware markdown code blocks in dark/light mode 2026-07-13 14:52:38 +08:00
yiling d560032013 feat(chat): send SSE keep-alive ping heartbeats during polling 2026-07-13 12:17:00 +08:00
yiling 02f2fde8ac fix(chat): remove explicit any in test mock to fix CI lint 2026-07-13 11:52:44 +08:00
yiling e080167ac5 feat(chat): enforce team-scoped authorization and session creator check
- Update chat API routes and service methods to include and verify teamId context
- Validate that the requesting user is the creator of the chat session during continuation
- Update frontend chatbot store and components to pass teamId parameter
- Update backend API/core and frontend store tests to verify team scoping and creator checks
2026-07-13 11:35:55 +08:00
yiling 61c49b4d7f feat(chat): allow user to choose chatbot in sidebar
- Remove chatbotAgentId from team settings and schema definition
- Make agentId a required parameter in ChatRequest DTO and chatService
- Fetch chatbot agents on chatbot sidebar mount and default to first enabled chat agent
- Render an agent selector dropdown inside the message input area
- Update existing chat sessions to follow the selected agent when continuing a conversation
2026-07-13 10:10:56 +08:00
yiling bad93466ae feat(webui): use filled bot icon when chatbot sidebar is open 2026-07-13 09:37:37 +08:00
yiling 1f8b0bac21 update ui 2026-07-13 09:34:51 +08:00
yiling c55380c0fd feat(webui): make toolCall block collapsible and render footer conditionally 2026-07-12 15:40:21 +08:00
yiling 3de57fd176 feat(webui): refactor chatbot message rendering to type-safe nested switch 2026-07-12 15:18:31 +08:00
yiling 0a4622ed8a syle minor fix 2026-07-12 10:29:29 +08:00
yiling bcb17acaf7 chore(serve): set idleTimeout to 120 seconds in Bun.serve 2026-07-11 21:11:18 +08:00
yiling b57a3e0239 chore(chat): support isNewChat payload flag and fix any casts 2026-07-11 21:05:47 +08:00
yiling c5ce4d07e9 feat(chat): add chatbot session name generation 2026-07-11 20:54:09 +08:00
yiling b5d52a4e56 feat(chat): add AgentSessionType enum and filter chatbot history sessions 2026-07-11 20:14:40 +08:00
yiling 94a3ec0b9b feat(chat): support optimistic context display entry locally during message sending 2026-07-11 17:02:58 +08:00
yiling bd897b951a chore: format codebase 2026-07-11 16:58:37 +08:00
yiling 482df3315c feat(chat): append and display context_display_info custom entry for attached assets 2026-07-11 16:58:09 +08:00
yiling cc9b32716d feat(chat): filter out context messages from API list and SSE stream 2026-07-11 15:47:48 +08:00
yiling 35db3fc49e fix(chatbot): preserve historical message order during SSE playback 2026-07-11 15:12:59 +08:00
yiling 6a5f8db0d6 fix(chatbot): preserve optimistic message during history playback of active sessions 2026-07-11 15:08:07 +08:00
yiling b13c47f207 feat(chatbot): highlight entire sidebar area with semi-transparent cover on dragover 2026-07-11 14:56:21 +08:00
yiling 2f68043e17 fix(chatbot): clear attached assets on message send and remove drag-drop toast 2026-07-11 14:41:23 +08:00
yiling 3d266a5606 feat(chatbot): remove agentId from request and resolve it via team settings, toggle chatbot icon with tooltip guard 2026-07-11 11:49:38 +08:00
yiling 87d811dad6 style(chatbot): move send button inside input border area to match comment input design 2026-07-11 10:59:56 +08:00
yiling 4a7da10ca8 fix(chatbot): prevent Enter from sending message during IME composition 2026-07-11 10:55:01 +08:00
yiling d19200e9ef feat(chatbot): implement chatbot sidebar, message view, history mode and sse stream 2026-07-11 10:49:40 +08:00
yiling 77718a3e1a refactor(webui): render chatbot button as standalone Bot icon outside the toggle group 2026-07-11 10:19:41 +08:00
yiling 8d2d915f36 feat(webui): add chatbot layout and drag-drop context store 2026-07-11 10:16:46 +08:00
Yiling-J 6ede64495f Merge pull request #184 from shumaiOne/feat/app-e2e-tests
feat(e2e): set up fullstack app-level e2e test framework and signup test case
2026-07-10 23:06:16 +08:00
yiling 13f220ef0f refactor(e2e): replace S3 bucket name check with explicit SHUMAI_E2E env flag 2026-07-10 20:11:54 +08:00
yiling 4e9fae96b1 fix(ci): split E2E jobs and support skipping app server via E2E_SKIP_APP_SERVER 2026-07-10 19:41:31 +08:00
yiling 8d23d8b1c3 fix(ci): split E2E tests into harness and app jobs 2026-07-10 19:28:14 +08:00
yiling 1e1ddd1a3d fix(e2e): declare types dependencies and adjust CI projects execution using --no-deps 2026-07-10 19:10:30 +08:00
yiling 55457fcd36 fix(webui): snap playhead to last frame upon ended event in useFramePlayer 2026-07-10 16:35:41 +08:00
yiling 9414ebaefd feat(e2e): run harness tests in parallel and app E2E tests sequentially using project dependencies 2026-07-10 16:27:51 +08:00
yiling 515c491d5d fix(e2e): restore Playwright empty object destructuring for fixture parameters 2026-07-10 16:21:09 +08:00
yiling 0aba2ee557 refactor(e2e): migrate database reset logic to standard Playwright custom fixture 2026-07-10 16:18:45 +08:00
yiling cd96015b9d feat(e2e): add browser selection support for app-level E2E tests 2026-07-10 15:35:59 +08:00
yiling 26982ee494 feat(e2e): set up fullstack app-level e2e test framework and signup test case 2026-07-10 15:21:01 +08:00
Yiling-J af5c85a87b Add installation command for ffmpeg using Chocolatey 2026-07-10 09:57:21 +08:00
yiling 5636a1b581 Release v0.1.8 2026-07-10 09:39:40 +08:00
Yiling-J 27af3a315e Merge pull request #183 from shumaiOne/fix/splitting-build-error
fix(webui): enable build splitting and code-split all page routes
2026-07-09 20:14:23 +08:00
yiling d3bbd42208 fix(web): optimize serveMainHtml performance by processing HTML at startup 2026-07-09 15:50:09 +08:00
yiling c3d88bb972 docs: add concise route code-splitting guideline to AGENTS.md 2026-07-09 15:32:15 +08:00
yiling a80e9de6cc feat(webui): code-split remaining team and share sub-routes to optimize vendor chunk size 2026-07-09 15:16:33 +08:00
yiling 6b5c2d8b23 feat(webui): enable route-level manual code-splitting to optimize bundle size 2026-07-09 15:09:39 +08:00
yiling 8d19b578e7 fix(build): resolve code-splitting chunk import issue on backend and frontend 2026-07-09 15:00:23 +08:00
Yiling-J e20e147445 Merge pull request #182 from shumaiOne/update-readme-notes
docs(readme): clarify displayed file size represents original file size
2026-07-09 10:40:58 +08:00
yiling 2116f2b1ab docs(readme): clarify displayed file size represents original file size 2026-07-09 10:40:24 +08:00
Yiling-J 7889b08008 Merge pull request #180 from shumaiOne/feat/chatbot-backend-apis
feat(chatbot): implement type-safe Chat APIs with compaction support
2026-07-08 10:07:00 +08:00
yiling 10279d74bc feat(chatbot): append turn-specific agentsInstruction as a custom message session entry 2026-07-07 21:51:10 +08:00
yiling 985bf0170c fix(api): prevent SSE connection resource leak by checking stream.aborted 2026-07-07 20:59:25 +08:00
yiling f04729b4bf refactor(chatbot): use SessionTreeEntry type directly in ChatService, removing SimpleMessageEntry 2026-07-07 14:16:31 +08:00
yiling 55026af22e refactor(chatbot): import AgentMessage type statically in DTO layer 2026-07-07 12:30:41 +08:00
yiling b8eddcd854 feat(chatbot): return rich AgentMessage structure directly from listMessages API 2026-07-07 12:25:32 +08:00
yiling d326a4cab8 refactor(chatbot): make mapEntryToMessage fully type-safe without any casts 2026-07-07 12:04:08 +08:00
yiling 243a4ab2c5 feat(chatbot): remove manual AgentSessionEntry creation from ChatService 2026-07-07 11:50:31 +08:00
yiling e4c1830e02 feat(chatbot): delegate context prompt building to AgentChatPromptBuilder in workflow 2026-07-07 11:42:40 +08:00
yiling 7b6db5b56c feat(chatbot): require agentId in ChatRequest and validate agent presence 2026-07-07 11:27:29 +08:00
yiling 63590d5329 feat(chatbot): decouple chatbot sessions from AssetComment table 2026-07-07 11:12:15 +08:00
yiling ccf742e158 refactor(chatbot): make user parameter in startOrContinueChat type-safe 2026-07-07 11:04:51 +08:00
yiling e55ee3a3f0 feat(chatbot): add chatbot backend api support 2026-07-07 10:55:22 +08:00
yiling 5e73fcf4aa update readme 2026-07-07 09:32:23 +08:00
yiling ca5f54b385 update readme 2026-07-07 09:27:50 +08:00
225 changed files with 12107 additions and 2670 deletions
+69 -4
View File
@@ -57,7 +57,7 @@ jobs:
bun run /tmp/shumai-local-release/node/node_modules/@shumai-one/shumai-transcode/bin/shumai-transcode-app.js --check
e2e_video_player:
name: E2E (${{ matrix.project }})
name: Harness E2E (${{ matrix.project }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
@@ -86,13 +86,78 @@ jobs:
- name: Install Playwright browser (${{ matrix.project }})
run: bunx playwright install --with-deps ${{ matrix.project }}
- name: Run video player e2e tests
run: bun run test:e2e --project=${{ matrix.project }}
- name: Run video player harness tests
env:
E2E_SKIP_APP_SERVER: true
run: bun run test:e2e --project=harness-${{ matrix.project }}
- name: Upload Playwright report
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: playwright-report-${{ matrix.project }}
name: playwright-report-harness-${{ matrix.project }}
path: playwright-report/
retention-days: 7
e2e_app:
name: App E2E (${{ matrix.project }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
project: chromium
- os: ubuntu-latest
project: firefox
# App E2E needs Docker for testcontainers, run webkit on Ubuntu
- os: ubuntu-latest
project: webkit
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: Install dependencies
run: bun install
- name: Install Playwright browser (${{ matrix.project }})
run: bunx playwright install --with-deps ${{ matrix.project }}
- name: Run app E2E tests
run: bun run test:e2e --project=app-${{ matrix.project }} --no-deps
- name: Upload Playwright report
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: playwright-report-app-${{ matrix.project }}
path: playwright-report/
retention-days: 7
workflow_e2e:
name: Workflow E2E
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: Install dependencies
run: bun install
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y ripgrep bubblewrap socat ffmpeg
- name: Run Workflow E2E Tests
run: bun run test:e2e:workflow
+15
View File
@@ -91,6 +91,7 @@ Add any additional context, caveats, or follow-up work.
- `bun run typecheck`
- `bun run test`
- `bun run test:e2e`
- `bun run test:e2e:workflow`
- **Backend Testing Mandate**: Every backend feature, service method, workflow, and activity MUST be accompanied by comprehensive tests. Logic-heavy code without corresponding test coverage is considered incomplete.
@@ -197,10 +198,15 @@ The frontend is built with:
- **Prefer System Colors**: Always prefer using semantic system color design tokens (e.g., `bg-background`, `text-foreground`, `text-sidebar-primary`, `text-muted-foreground`, etc.) over hardcoded color utilities (e.g., `bg-blue-50`, `text-emerald-600`, `bg-emerald-500`). This ensures seamless compatibility with light and dark themes.
### Route Code-Splitting
To optimize initial bundle size, all routes must be code-split into `[route].tsx` (lightweight route definition and loaders) and `[route].lazy.tsx` (UI component and heavy imports).
### Development
- **Run Dev**: `bun run dev` (Runs on port 3000)
## Testing
We use **Vitest** for testing (via `bun run test`). **Do not use `bun test`** as it uses Bun's native test runner which is not compatible with this project's test suite.
@@ -268,6 +274,14 @@ describe('Team API', () => {
})
```
### Workflow E2E Tests
- Located in `packages/e2e/workflow/**/*.test.ts`.
- Run via `bun run test:e2e:workflow` (which executes tests against both `local` and `temporal` executors dynamically).
- **Mandatory test coverage**: When creating or updating workflows or activities, you MUST create or update the corresponding E2E tests.
- **Mocking**: Mock ONLY AI API calls. Do not mock S3, databases, or media/transcode extraction services.
- Mock the AI response by spying on `AgentHarness.prototype.prompt`:
## Prisma Configuration & Migrations
- Schema: `packages/db/prisma/schema.prisma`
@@ -300,6 +314,7 @@ We use `prisma-json-types-generator` to enforce strict type-safety for Prisma `J
- Development: Use `bun --bun run prisma migrate dev` to create and apply migrations during development.
- Production: Use `bun --bun run prisma migrate deploy` to apply pending migrations in production environments.
- **No Automatic Dev DB Reset**: Do not run `prisma migrate reset --force` or commands that force-reset the database automatically. If migrations become out of sync or a reset is required, stop executing, report the situation to the user, and present suggested manual cleanup/reset steps for the user to execute.
### Commands
+73 -10
View File
@@ -32,7 +32,7 @@
- **Semantic Search**: Find assets based on visual or conceptual search queries using vector embeddings(Gemini Embedding 2).
> [!NOTE]
> **Media Processing & Storage:** When uploading media, Shumai always preserves and stores your original raw file. For all media types (both images and videos), Shumai creates a 480p WebP poster image to serve as the preview in the file list view. Additionally:
> **Media Processing & Storage:** When uploading media, Shumai always preserves and stores your original raw file. Note that the file size shown in the system represents the original file size and does not include the size of any transcoded files. For all media types (both images and videos), Shumai creates a 480p WebP poster image to serve as the preview in the file list view. Additionally:
> * **For Images:** Shumai generates a high-quality WebP proxy at the exact same resolution as your original file for optimized viewing.
> * **For Videos:** It generates at least one proxy video (depending on your transcoding settings), alongside a sprite image and a small preview video (capped at 1600 frames) to enable fast timeline scrubbing and lightning-quick previews.
@@ -120,23 +120,86 @@ mkdir shumai && cd shumai
#### Step 3: Install Platform-Specific Dependencies
Make sure the following system dependencies are installed on your host machine before installing Shumai:
#### Linux
| Package | Description | Ubuntu/Debian | Fedora | Arch | macOS |
| :------------- | :------------------------------------------------------ | :------------------------------- | :------------------------------- | :-------------------------------------- | :--------------------- |
| **ffmpeg** | Media transcoding and metadata extraction | `sudo apt install -y ffmpeg` | `sudo dnf install -y ffmpeg` | `sudo pacman -S --noconfirm ffmpeg` | `brew install ffmpeg` |
| **bubblewrap** | Sandboxing environment for secure AI agent execution | `sudo apt install -y bubblewrap` | `sudo dnf install -y bubblewrap` | `sudo pacman -S --noconfirm bubblewrap` | _NOT REQUIRED_ |
| **socat** | Bidirectional socket relay for sandbox network bridging | `sudo apt install -y socat` | `sudo dnf install -y socat` | `sudo pacman -S --noconfirm socat` | `brew install socat` |
| **ripgrep** | Fast search tool for workspace security policies | `sudo apt install -y ripgrep` | `sudo dnf install -y ripgrep` | `sudo pacman -S --noconfirm ripgrep` | `brew install ripgrep` |
Shumai requires the following system packages on Linux:
- **`ffmpeg`** – Used for media transcoding and metadata extraction.
- **`bubblewrap`**, **`socat`**, and **`ripgrep`** – Required by the AI agent sandbox (`anthropic-experimental/sandbox-runtime`) for process isolation, networking, and workspace search.
Install all required packages with one command:
> Ubuntu/Debian
```bash
sudo apt install -y ffmpeg bubblewrap socat ripgrep
```
> Fedora
```bash
sudo dnf install -y ffmpeg bubblewrap socat ripgrep
```
> Arch Linux
```bash
sudo pacman -S --noconfirm ffmpeg bubblewrap socat ripgrep
```
> [!NOTE]
> **Ubuntu 24.04+ Note:** These releases restrict unprivileged user namespaces by default. To allow `bubblewrap` and the sandbox isolation layer to function, disable this restriction:
> **Ubuntu 24.04+** restricts unprivileged user namespaces by default. This prevents `anthropic-experimental/sandbox-runtime` from using `bubblewrap` for sandbox isolation.
>
> To temporarily allow user namespaces:
>
> ```bash
> sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
> ```
>
> Alternatively, configure an AppArmor profile that grants user namespace creation (`userns`) privileges to the relevant binaries.
> Alternatively, configure an AppArmor profile that grants the required `userns` permission to the relevant binaries.
---
#### macOS
Shumai requires:
- **`ffmpeg`** – Used for media transcoding and metadata extraction.
- **`ripgrep`** – Required by the AI agent sandbox (`anthropic-experimental/sandbox-runtime`).
Install the required packages with Homebrew:
```bash
brew install ffmpeg ripgrep
```
---
#### Windows (alpha)
> [!WARNING]
> Windows support in anthropic-experimental/sandbox-runtime is **alpha and the design is in flux**.
>
> The current implementation provides:
> - Network egress filtering via the **Windows Filtering Platform (WFP)**.
> - File read/write denial via **ACL stamping**.
>
> However, it is **not a security boundary against a deliberately adversarial sandboxed process**.
>
> The sandbox implementation will be substantially revised in a future release, where sandboxed processes will run under a dedicated sandbox user account.
Shumai requires:
- **`ffmpeg`** – Used for media transcoding and metadata extraction.
Install `ffmpeg` using your preferred package manager:
**winget**
```powershell
winget install Gyan.FFmpeg
```
**Chocolatey**
```powershell
choco install ffmpeg
```
#### Step 4: Install Shumai globally
+76 -14
View File
@@ -33,7 +33,7 @@
- **语义搜索**:基于向量进行搜索(Gemini Embedding 2),支持按视觉内容或语义概念搜索资产。
> [!NOTE]
> **媒体处理与存储机制:** 上传媒体文件后,Shumai 始终会保留并存储原始文件。无论图片还是视频,都会额外生成一张 480p 的 WebP 封面图,作为文件列表中的预览图。此外:
> **媒体处理与存储机制:** 上传媒体文件后,Shumai 始终会保留并存储原始文件。请注意,系统中显示的文件大小为原始文件的大小,不包含转码后生成的代理文件大小。无论图片还是视频,都会额外生成一张 480p 的 WebP 封面图,作为文件列表中的预览图。此外:
>
> * **图片:** 会生成一份与原图分辨率完全一致的高质量 WebP 转码版本,在保证画质的同时提升浏览性能。
> * **视频:** 会根据转码配置生成至少一个代理视频,同时生成雪碧图(Sprite)和一段最长 1600 帧的低分辨率转码视频,用于实现快速拖动时间轴和即时视频预览。
@@ -132,26 +132,88 @@ mkdir shumai && cd shumai
```
#### 第三步:安装平台特定系统依赖
#### 步骤 3:安装平台依赖
在安装 Shumai 之前,请确保您的宿主机上已经安装了以下系统依赖项:
#### Linux
| 依赖包 | 功能描述 | Ubuntu/Debian | Fedora | Arch | macOS |
| --- | --- | --- | --- | --- | --- |
| **ffmpeg** | 媒体转码及元数据提取 | `sudo apt install -y ffmpeg` | `sudo dnf install -y ffmpeg` | `sudo pacman -S --noconfirm ffmpeg` | `brew install ffmpeg` |
| **bubblewrap** | 用于安全执行 AI 助手脚本的隔离沙箱环境 | `sudo apt install -y bubblewrap` | `sudo dnf install -y bubblewrap` | `sudo pacman -S --noconfirm bubblewrap` | *无需安装* |
| **socat** | 用于沙箱网络桥接的双向套接字中继 | `sudo apt install -y socat` | `sudo dnf install -y socat` | `sudo pacman -S --noconfirm socat` | `brew install socat` |
| **ripgrep** | 用于工作区安全策略的高效文本搜索工具 | `sudo apt install -y ripgrep` | `sudo dnf install -y ripgrep` | `sudo pacman -S --noconfirm ripgrep` | `brew install ripgrep` |
在 Linux 上运行 Shumai 需要安装以下系统依赖:
- **`ffmpeg`** —— 用于媒体转码和元数据提取。
- **`bubblewrap`**、**`socat`** 和 **`ripgrep`** —— AI Agent 沙箱(`anthropic-experimental/sandbox-runtime`)所需,用于进程隔离、网络通信和工作区搜索。
可以使用以下命令一次性安装所有依赖:
> Ubuntu/Debian
```bash
sudo apt install -y ffmpeg bubblewrap socat ripgrep
```
> Fedora
```bash
sudo dnf install -y ffmpeg bubblewrap socat ripgrep
```
> Arch Linux
```bash
sudo pacman -S --noconfirm ffmpeg bubblewrap socat ripgrep
```
> [!NOTE]
> **Ubuntu 24.04+ 用户请注意:** 这些系统版本默认限制了非特权用户命名空间(unprivileged user namespaces)。为了让 `bubblewrap` 和沙箱隔离层正常工作,需要关闭此限制:
> **Ubuntu 24.04+** 默认限制非特权用户创建 User Namespace,这会导致 `anthropic-experimental/sandbox-runtime` 无法使用 `bubblewrap` 提供沙箱隔离。
>
> 如需临时启用 User Namespace,请执行:
>
> ```bash
> sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
>
> ```
>
>
> 或者,您也可以专门配置一个 AppArmor 配置文件,为相关二进制文件授予用户命名空间创建(`userns`)权限。
>
> 或者,配置 AppArmor Profile,为相关可执行文件授予所需的 `userns` 权限。
---
#### macOS
在 macOS 上运行 Shumai 需要:
- **`ffmpeg`** —— 用于媒体转码和元数据提取。
- **`ripgrep`** —— AI Agent 沙箱(`anthropic-experimental/sandbox-runtime`)所需。
使用 Homebrew 安装所需依赖:
```bash
brew install ffmpeg ripgrep
```
---
#### Windows(Alpha)
> [!WARNING]
> anthropic-experimental/sandbox-runtime 的 Windows 支持目前仍处于 **Alpha** 阶段,整体设计仍在持续调整中。
>
> 当前实现提供了以下能力:
> - 通过 **Windows Filtering Platform (WFP)** 实现网络出口(egress)过滤。
> - 通过 **ACL Stamping** 实现文件读写权限限制。
>
> **但这并不能作为针对恶意沙箱进程的安全边界(security boundary)**。
>
> 未来版本将对沙箱机制进行重大调整,届时沙箱进程将运行在独立的沙箱用户账户下。
在 Windows 上运行 Shumai 需要:
- **`ffmpeg`** —— 用于媒体转码和元数据提取。
使用你偏好的包管理器安装 `ffmpeg`:
**winget**
```powershell
winget install Gyan.FFmpeg
```
**Chocolatey**
```powershell
choco install ffmpeg
```
#### 第四步:全局安装 Shumai
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/agent-app",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"bin": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/cli",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"bin": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/transcode-app",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"bin": {
+56
View File
@@ -0,0 +1,56 @@
import { test as base } from '@playwright/test'
import { PrismaClient } from '../../../packages/db/src/generated/prisma/client'
import { PrismaPg } from '@prisma/adapter-pg'
import { Pool } from 'pg'
import * as fs from 'fs'
import * as path from 'path'
import { fileURLToPath } from 'url'
const currentDir = path.dirname(fileURLToPath(import.meta.url))
const envPath = path.resolve(currentDir, '.env.e2e')
// Load E2E environment variables if present
if (fs.existsSync(envPath)) {
const content = fs.readFileSync(envPath, 'utf8')
content.split('\n').forEach((line) => {
const parts = line.split('=')
if (parts.length >= 2) {
const key = parts[0].trim()
const value = parts.slice(1).join('=').trim()
process.env[key] = value
}
})
}
export const test = base.extend<{ prisma: PrismaClient }>({
prisma: [
// eslint-disable-next-line no-empty-pattern
async ({}, use) => {
const connectionString = process.env.DATABASE_URL
const pool = new Pool({ connectionString })
const adapter = new PrismaPg(pool)
const prisma = new PrismaClient({ adapter })
// Reset database state before the test runs
await prisma.$executeRawUnsafe(`
DO $$ DECLARE
r RECORD;
BEGIN
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public' AND tablename <> '_prisma_migrations') LOOP
EXECUTE 'TRUNCATE TABLE ' || quote_ident(r.tablename) || ' CASCADE;';
END LOOP;
END $$;
`)
// Provide the clean prisma instance to the test
await use(prisma)
// Disconnect and clean up resources after the test finishes
await prisma.$disconnect()
await pool.end()
},
{ auto: true },
],
})
export { expect } from '@playwright/test'
+36
View File
@@ -0,0 +1,36 @@
import { teardown as dbTeardown } from '../../../packages/db/src/test-global-setup'
import * as fs from 'fs'
import * as path from 'path'
import { fileURLToPath } from 'url'
const currentDir = path.dirname(fileURLToPath(import.meta.url))
export default async function globalTeardown() {
const envPath = path.resolve(currentDir, '.env.e2e')
if (fs.existsSync(envPath)) {
const content = fs.readFileSync(envPath, 'utf8')
let bucketName: string | undefined
content.split('\n').forEach((line) => {
const parts = line.split('=')
if (parts.length >= 2 && parts[0].trim() === 'S3_BUCKET') {
bucketName = parts.slice(1).join('=').trim()
}
})
if (bucketName) {
const bucketDir = path.resolve('data', bucketName)
if (fs.existsSync(bucketDir)) {
console.log(`Cleaning up E2E local storage bucket: ${bucketDir}`)
fs.rmSync(bucketDir, { recursive: true, force: true })
}
}
// Delete the temp .env.e2e file
fs.unlinkSync(envPath)
}
// Stop pgvector container
await dbTeardown()
console.log('Global teardown complete.')
}
+35
View File
@@ -0,0 +1,35 @@
import { globalTestSetup } from '../../../packages/db/src/test-global-setup'
import { randomBytes } from 'crypto'
import * as fs from 'fs'
import * as path from 'path'
import { fileURLToPath } from 'url'
const currentDir = path.dirname(fileURLToPath(import.meta.url))
async function start() {
console.log('Starting E2E test database container...')
await globalTestSetup()
// Generate a random S3 bucket name
const randomSuffix = randomBytes(4).toString('hex')
const bucketName = `shumai-e2e-${randomSuffix}`
process.env.S3_BUCKET = bucketName
process.env.SHUMAI_SERVER_PORT = '5200'
process.env.BETTER_AUTH_URL = 'http://localhost:5200'
process.env.NODE_ENV = 'development'
process.env.SHUMAI_E2E = 'true'
// Write variables to .env.e2e file so the teardown can load them
const envContent = [`DATABASE_URL=${process.env.DATABASE_URL}`, `S3_BUCKET=${bucketName}`].join(
'\n',
)
fs.writeFileSync(path.resolve(currentDir, '.env.e2e'), envContent)
console.log(`E2E env file written. Starting Web App...`)
// Now run the web app
await import('../src/index.ts')
}
start().catch(console.error)
+75
View File
@@ -0,0 +1,75 @@
import { expect, test } from './fixtures'
import * as fs from 'fs'
import * as path from 'path'
import { fileURLToPath } from 'url'
const currentDir = path.dirname(fileURLToPath(import.meta.url))
const tempImgPath = path.resolve(currentDir, 'temp-cover.png')
test.beforeAll(async () => {
// Create a 1x1 transparent PNG file
const pngBase64 =
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='
fs.writeFileSync(tempImgPath, Buffer.from(pngBase64, 'base64'))
})
test.afterAll(async () => {
if (fs.existsSync(tempImgPath)) {
fs.unlinkSync(tempImgPath)
}
})
test('should sign up and create a project with a cover image', async ({ page }) => {
const email = `test-${Date.now()}@shumai.local`
const password = 'Password123!'
// 1. Sign Up Flow
await page.goto('/signup')
await expect(page).toHaveURL(/\/signup/)
// Fill credentials
await page.fill('#email', email)
await page.fill('#password', password)
// Submit sign up
await page.click('button[type="submit"]')
// Wait for the automatic redirect to the team page (since only one team exists)
await expect(page).toHaveURL(/\/teams\/[^/]+/)
// 2. Create Project Flow
// Click the "Create Project" button on the dashboard
await page.click('button:has-text("Create Project")')
const dialog = page.locator('[role="dialog"]')
await expect(dialog).toBeVisible()
// Fill project name
const projectName = `My Project ${Date.now()}`
await dialog.locator('#name').fill(projectName)
// Set cover image
await dialog.locator('input[type="file"]').setInputFiles(tempImgPath)
// Wait for image upload completion and preview
await expect(dialog.locator('img[alt="Cover Preview"]')).toBeVisible({ timeout: 15_000 })
// Click submit in dialog
await dialog.locator('button[type="submit"]').click()
// Wait for navigation to the project page
await expect(page).toHaveURL(/\/projects\/[^/]+/)
// Verify project name appears in the project view
await expect(page.locator(`text=${projectName}`).first()).toBeVisible()
// 3. Verification on Dashboard
// Navigate back to the dashboard via sidebar
await page.click('button[aria-label="Dashboard"]')
// Wait for dashboard URL
await expect(page).toHaveURL(/\/teams\/[^/]+/)
// Verify the project card is visible and displays the cover image with project name alt text
await expect(page.locator(`img[alt="${projectName}"]`)).toBeVisible()
})
+6 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/web-app",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"dependencies": {
@@ -13,5 +13,10 @@
"@shumai/transcode": "workspace:*",
"@shumai/webui": "workspace:*",
"hono": "4.12.23"
},
"devDependencies": {
"@prisma/adapter-pg": "7.8.0",
"pg": "8.21.0",
"@types/pg": "8.20.0"
}
}
+18 -5
View File
@@ -89,7 +89,12 @@ async function run() {
const mainHtmlHeaders = bundle.files.find((f) => f.path === bundle.index)?.headers || {
'content-type': 'text/html;charset=utf-8',
}
const serveMainHtml = () => new Response(mainHtmlFile, { headers: mainHtmlHeaders })
const raw = await mainHtmlFile.text()
const mainHtmlText = raw
.replaceAll('href="./', 'href="/')
.replaceAll('src="./', 'src="/')
.replaceAll('content="./', 'content="/')
const serveMainHtml = () => new Response(mainHtmlText, { headers: mainHtmlHeaders })
routes['/'] = serveMainHtml
routes['/*'] = serveMainHtml
@@ -109,6 +114,7 @@ async function run() {
const server = Bun.serve({
port,
idleTimeout: 120,
maxRequestBodySize: process.env.MAX_REQUEST_BODY_SIZE
? parseInt(process.env.MAX_REQUEST_BODY_SIZE)
: 1024 * 1024 * 1024 * 10, // Default 10GB
@@ -130,7 +136,14 @@ async function run() {
process.on('SIGTERM', shutdown)
}
handleDaemonCommands('shumai', run).catch((err) => {
console.error(err)
process.exit(1)
})
if (process.env.SHUMAI_E2E === 'true') {
run().catch((err) => {
console.error(err)
process.exit(1)
})
} else {
handleDaemonCommands('shumai', run).catch((err) => {
console.error(err)
process.exit(1)
})
}
+2 -2
View File
@@ -19,7 +19,8 @@ await Bun.build({
root: '.',
target: 'bun',
outdir: outdir,
minify: false,
minify: true,
splitting: true,
naming: '[name].[ext]',
plugins: [
temporalWorkflow({
@@ -44,5 +45,4 @@ await Bun.build({
define: {
'process.env.NODE_ENV': JSON.stringify('production'),
},
publicPath: '/',
})
+36 -13
View File
@@ -37,7 +37,7 @@
},
"apps/agent": {
"name": "@shumai/agent-app",
"version": "0.1.5",
"version": "0.1.9",
"bin": {
"shumai-agent": "./src/index.ts",
},
@@ -49,7 +49,7 @@
},
"apps/cli": {
"name": "@shumai/cli",
"version": "0.1.5",
"version": "0.1.9",
"bin": {
"shumai-cli": "./src/index.ts",
},
@@ -64,7 +64,7 @@
},
"apps/transcode": {
"name": "@shumai/transcode-app",
"version": "0.1.5",
"version": "0.1.9",
"bin": {
"shumai-transcode": "./src/index.ts",
},
@@ -76,7 +76,7 @@
},
"apps/web": {
"name": "@shumai/web-app",
"version": "0.1.5",
"version": "0.1.9",
"dependencies": {
"@shumai/agent": "workspace:*",
"@shumai/api": "workspace:*",
@@ -88,10 +88,15 @@
"@shumai/workflow-core": "workspace:*",
"hono": "4.12.23",
},
"devDependencies": {
"@prisma/adapter-pg": "7.8.0",
"@types/pg": "8.20.0",
"pg": "8.21.0",
},
},
"packages/agent": {
"name": "@shumai/agent",
"version": "0.1.5",
"version": "0.1.9",
"dependencies": {
"@anthropic-ai/sandbox-runtime": "0.0.52",
"@earendil-works/pi-agent-core": "0.78.0",
@@ -115,7 +120,7 @@
},
"packages/api": {
"name": "@shumai/api",
"version": "0.1.5",
"version": "0.1.9",
"dependencies": {
"@hono/zod-validator": "0.7.6",
"@shumai/core": "workspace:*",
@@ -129,8 +134,9 @@
},
"packages/core": {
"name": "@shumai/core",
"version": "0.1.5",
"version": "0.1.9",
"dependencies": {
"@earendil-works/pi-agent-core": "0.78.0",
"@shumai/db": "workspace:*",
"@shumai/dtos": "workspace:*",
"@sinclair/typebox": "0.34.49",
@@ -156,7 +162,7 @@
},
"packages/db": {
"name": "@shumai/db",
"version": "0.1.5",
"version": "0.1.9",
"dependencies": {
"@earendil-works/pi-agent-core": "0.78.0",
"@prisma/adapter-pg": "7.8.0",
@@ -172,18 +178,33 @@
},
"packages/dtos": {
"name": "@shumai/dtos",
"version": "0.1.5",
"version": "0.1.9",
"dependencies": {
"@earendil-works/pi-agent-core": "0.78.0",
"zod": "4.4.3",
},
},
"packages/e2e": {
"name": "@shumai/e2e",
"version": "0.1.9",
"dependencies": {
"@earendil-works/pi-agent-core": "0.78.0",
"@google/genai": "1.52.0",
"@shumai/agent": "workspace:*",
"@shumai/core": "workspace:*",
"@shumai/db": "workspace:*",
"@shumai/dtos": "workspace:*",
"@shumai/transcode": "workspace:*",
"@shumai/workflow-core": "workspace:*",
},
},
"packages/tableprinter": {
"name": "@shumai/tableprinter",
"version": "0.1.5",
"version": "0.1.9",
},
"packages/transcode": {
"name": "@shumai/transcode",
"version": "0.1.5",
"version": "0.1.9",
"dependencies": {
"@shumai/core": "workspace:*",
"@shumai/db": "workspace:*",
@@ -197,7 +218,7 @@
},
"packages/webui": {
"name": "@shumai/webui",
"version": "0.1.5",
"version": "0.1.9",
"dependencies": {
"@base-ui/react": "1.5.0",
"@dnd-kit/abstract": "0.4.0",
@@ -294,7 +315,7 @@
},
"packages/workflow-core": {
"name": "@shumai/workflow-core",
"version": "0.1.5",
"version": "0.1.9",
"dependencies": {
"@shumai/core": "workspace:*",
"@shumai/db": "workspace:*",
@@ -1003,6 +1024,8 @@
"@shumai/dtos": ["@shumai/dtos@workspace:packages/dtos"],
"@shumai/e2e": ["@shumai/e2e@workspace:packages/e2e"],
"@shumai/tableprinter": ["@shumai/tableprinter@workspace:packages/tableprinter"],
"@shumai/transcode": ["@shumai/transcode@workspace:packages/transcode"],
+1
View File
@@ -43,6 +43,7 @@
"using-shumai/agent-providers",
"using-shumai/agents",
"using-shumai/agent-skills",
"using-shumai/agent-tools",
"using-shumai/agent-sandbox"
]
},
+61
View File
@@ -0,0 +1,61 @@
---
title: Agent Tools
description: Configure the tools and capabilities permitted for each agent.
---
Shumai agents are equipped with built-in tools that allow them to interact with the file system, view files, extract video frames, and execute commands.
By default, **all tools are enabled** for new agents. Team Owners can deny specific tools to restrict agent capabilities.
<Warning>
**Caution:** Disabling tools can cause agents or skills to fail. For example, disabling the **Sandboxed Terminal** tool will break almost all custom skills that rely on executing commands or script environments. Disable tools only when you have a specific security policy or isolation requirement.
</Warning>
---
## Available Tools
The following tools can be individually toggled in the agent configuration dialog:
### 1. Sandboxed Terminal (`bash`)
- **Description:** Allows the agent to run terminal/shell commands in a highly isolated environment.
- **Why it matters:** Essential for running custom scripts, processing files on disk, or using standard Unix utilities. If disabled, the agent won't be able to run command-line code.
### 2. Read Skills (`read_skill`)
- **Description:** Allows the agent to load and read skill definitions to perform specialized actions.
- **Why it matters:** Enables the agent to inspect the instructions/configurations of custom skills registered in your workspace before executing them.
### 3. Analyze Image (`analyze_image`)
- **Description:** Allows the agent to view and analyze the visual contents of an image file.
- **Why it matters:** Enables multimodal agents to "see" image assets uploaded to Shumai, allowing them to answer comments or write metadata about images.
### 4. Video Frame Extraction (`screenshot`)
- **Description:** Allows the agent to extract still image frames from video files for analysis.
- **Why it matters:** Enables the agent to perform frame-by-frame analysis on videos (e.g. for comments, annotation, or search).
### 5. Browse Shumai (`list_assets`)
- **Description:** Allows the agent to list folders and browse files in Shumai.
- **Why it matters:** Gives the agent visual mapping of the project structure so it can search for other files or locate related assets.
### 6. Create Folder (`create_folder`)
- **Description:** Allows the agent to create new folders in Shumai.
- **Why it matters:** Needed if the agent organizes outputs or places files in custom subdirectory folders.
### 7. Create File (`create_file`)
- **Description:** Allows the agent to create and upload new files to Shumai.
- **Why it matters:** Allows the agent to generate new assets (e.g. text logs, transcoded clips, reports) and save them directly back into your project.
### 8. Create Version (`create_version`)
- **Description:** Allows the agent to upload and stack a new version of an existing file in Shumai.
- **Why it matters:** Allows the agent to update files in a clean, non-destructive way by stacking revisions rather than creating duplicate files.
---
## Restricting Tools
To change which tools an agent can use:
1. Navigate to **Team Settings** > **Agents** (Note: Only Team Owners can view and modify these settings).
2. Click on the agent you want to edit.
3. Scroll down below the **Skills** section to find the **Tools** configuration panel.
4. Toggle the switches next to the tools to enable or disable them.
5. Click **Save Changes**.
+2 -1
View File
@@ -1,6 +1,6 @@
{
"name": "shumai",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"repository": {
@@ -19,6 +19,7 @@
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"test": "bun --bun vitest run",
"test:e2e": "playwright test",
"test:e2e:workflow": "bun --bun vitest run -c packages/e2e/vitest.config.ts",
"test:e2e:ui": "playwright test --ui",
"test:e2e:report": "playwright show-report",
"test:e2e:install": "playwright install --with-deps",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/agent",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"main": "./src/index.ts",
+317
View File
@@ -15,10 +15,15 @@ import {
updateAssetMetadataActivity,
getAssetPathContextActivity,
executeAgentToolActivity,
generateSessionNameActivity,
type GenerateSessionNameParams,
} from './agent'
import * as piAgent from '../index'
import { type AgentHarness, type Session } from '@earendil-works/pi-agent-core'
import { type DatabaseSessionMetadata } from '../database-session-storage'
import { triggerLocalCancel } from '@shumai/workflow-core'
import { Context } from '@temporalio/activity'
import {
prisma,
AssetType,
@@ -44,6 +49,9 @@ vi.mock('../index', async () => {
describe('Agent Activities', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.spyOn(Context, 'current').mockImplementation(() => {
throw new Error('Not in Temporal')
})
})
it('should call agentChatActivity and prompt the harness', async () => {
@@ -87,6 +95,143 @@ describe('Agent Activities', () => {
expect(piAgent.createAgentSession).toHaveBeenCalled()
})
it('should register local cancellation handler and call harness.abort when cancelled in local mode', async () => {
let resolvePrompt!: (value: {
content: Array<{ type: string; text: string } | { type: string }>
usage: { input: number; output: number }
stopReason?: string
errorMessage?: string
}) => void
const promptPromise = new Promise<Parameters<typeof resolvePrompt>[0]>((resolve) => {
resolvePrompt = resolve
})
const mockHarness = {
prompt: vi.fn().mockReturnValue(promptPromise),
abort: vi.fn(),
}
const mockSession = {
getEntries: vi.fn().mockResolvedValue([]),
getStorage: vi.fn().mockReturnValue({ sessionId: 'mock-session-id' }),
}
vi.mocked(piAgent.createAgentSession).mockResolvedValue({
session: mockSession as unknown as Session<DatabaseSessionMetadata>,
harness: mockHarness as unknown as AgentHarness,
})
const context = {
agent: { id: 'b1', provider: { name: 'google' }, modelRef: { modelId: 'gemini' } },
dbProviders: [],
teamSkills: [],
allowedDomains: [],
} as unknown as AgentExecutionContext
const executionPromise = agentChatActivity({
taskId: 'task-123',
teamId: 't1',
agentId: 'b1',
message: 'Hi',
imageUrls: [],
projectId: 'p1',
folderId: 'f1',
sessionId: 'mock-session-id',
context,
})
// Yield to let the asynchronous setup register the cancel handler
await new Promise((resolve) => setTimeout(resolve, 50))
// Simulate user cancellation
const cancelled = triggerLocalCancel('task-123')
expect(cancelled).toBe(true)
expect(mockHarness.abort).toHaveBeenCalled()
// Resolve the prompt promise to allow the activity to finish clean
resolvePrompt({
content: [{ type: 'text', text: 'AI response' }],
usage: { input: 10, output: 20 },
})
await executionPromise
})
it('should listen to Temporal cancellation signal and call harness.abort when cancelled in Temporal mode', async () => {
const mockTemporalContext = {
cancellationSignal: new EventTarget(),
heartbeat: vi.fn(),
} as unknown as Context
vi.spyOn(Context, 'current').mockReturnValue(mockTemporalContext)
let resolvePrompt!: (value: {
content: Array<{ type: string; text: string } | { type: string }>
usage: { input: number; output: number }
stopReason?: string
errorMessage?: string
}) => void
const promptPromise = new Promise<Parameters<typeof resolvePrompt>[0]>((resolve) => {
resolvePrompt = resolve
})
const mockHarness = {
prompt: vi.fn().mockReturnValue(promptPromise),
abort: vi.fn(),
}
const mockSession = {
getEntries: vi.fn().mockResolvedValue([]),
getStorage: vi.fn().mockReturnValue({ sessionId: 'mock-session-id' }),
}
vi.mocked(piAgent.createAgentSession).mockResolvedValue({
session: mockSession as unknown as Session<DatabaseSessionMetadata>,
harness: mockHarness as unknown as AgentHarness,
})
const context = {
agent: { id: 'b1', provider: { name: 'google' }, modelRef: { modelId: 'gemini' } },
dbProviders: [],
teamSkills: [],
allowedDomains: [],
} as unknown as AgentExecutionContext
const addEventListenerSpy = vi.spyOn(mockTemporalContext.cancellationSignal, 'addEventListener')
const removeEventListenerSpy = vi.spyOn(
mockTemporalContext.cancellationSignal,
'removeEventListener',
)
const executionPromise = agentChatActivity({
teamId: 't1',
agentId: 'b1',
message: 'Hi',
imageUrls: [],
projectId: 'p1',
folderId: 'f1',
sessionId: 'mock-session-id',
context,
})
// Yield to let the asynchronous setup add the abort event listener
await new Promise((resolve) => setTimeout(resolve, 50))
expect(addEventListenerSpy).toHaveBeenCalledWith('abort', expect.any(Function), { once: true })
// Simulate Temporal cancellation event
mockTemporalContext.cancellationSignal.dispatchEvent(new Event('abort'))
expect(mockHarness.abort).toHaveBeenCalled()
// Resolve the prompt promise to allow the activity to finish clean
resolvePrompt({
content: [{ type: 'text', text: 'AI response' }],
usage: { input: 10, output: 20 },
})
await executionPromise
expect(removeEventListenerSpy).toHaveBeenCalledWith('abort', expect.any(Function))
addEventListenerSpy.mockRestore()
removeEventListenerSpy.mockRestore()
})
it('should include error message in text when stopReason is error', async () => {
const mockHarness = {
prompt: vi.fn().mockResolvedValue({
@@ -741,4 +886,176 @@ describe('Agent Database Activities Integration', () => {
})
})
})
describe('generateSessionNameActivity', () => {
it('should generate and save session name for chat sessions', async () => {
await prisma.team.create({
data: { id: 't1', name: 'Test Team' },
})
const user = await prisma.user.create({
data: { id: 'u1', name: 'Test User', email: 'u1@test.com' },
})
const agentUser = await prisma.user.create({
data: {
id: 'agent-123',
name: 'Chat Agent User',
email: 'agent@shumai.ai',
type: 'agent',
},
})
const agent = await prisma.agent.create({
data: {
id: agentUser.id,
teamId: 't1',
type: 'chat',
config: { provider: 'openai', model: 'gpt-4' },
},
})
await prisma.agentSession.create({
data: {
id: 'session-123',
agentId: 'agent-123',
userId: user.id,
cwd: process.cwd(),
type: 'chat',
name: null,
},
})
const mockHarness = {
prompt: vi.fn().mockResolvedValue({
content: [{ type: 'text', text: ' "Summarized Chat Title" ' }],
}),
}
vi.mocked(piAgent.createAgentSession).mockResolvedValue({
session: {} as unknown as Session<DatabaseSessionMetadata>,
harness: mockHarness as unknown as AgentHarness,
})
await generateSessionNameActivity({
teamId: 't1',
agentId: 'agent-123',
prompt: 'hello world prompt',
sessionId: 'session-123',
context: {
agent: {
...agent,
provider: { name: 'openai' },
modelRef: {
id: 'm1',
providerName: 'openai',
modelId: 'gpt-4',
name: 'GPT-4',
config: {},
createdAt: new Date(),
updatedAt: new Date(),
},
} as unknown as GenerateSessionNameParams['context']['agent'],
dbProviders: [
{
name: 'openai',
config: { api: 'openai-responses', apiKey: 'fake-key' },
models: [
{
modelId: 'gpt-4',
name: 'GPT-4',
config: {
reasoning: false,
input: ['text'],
contextWindow: 4096,
maxTokens: 4096,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
},
],
},
],
},
})
const updatedSession = await prisma.agentSession.findUnique({
where: { id: 'session-123' },
})
expect(updatedSession?.name).toBe('Summarized Chat Title')
expect(mockHarness.prompt).toHaveBeenCalledWith('hello world prompt')
// Verify that transient naming session was cleaned up (deleted)
const namingSessions = await prisma.agentSession.findMany({
where: { type: 'naming' },
})
expect(namingSessions).toHaveLength(0)
})
it('should skip name generation for comment sessions or if name is already set', async () => {
await prisma.team.create({
data: { id: 't1', name: 'Test Team' },
})
const user = await prisma.user.create({
data: { id: 'u1', name: 'Test User', email: 'u1@test.com' },
})
const agentUser = await prisma.user.create({
data: {
id: 'agent-123',
name: 'Chat Agent User',
email: 'agent2@shumai.ai',
type: 'agent',
},
})
await prisma.agent.create({
data: {
id: agentUser.id,
teamId: 't1',
type: 'chat',
config: { provider: 'openai', model: 'gpt-4' },
},
})
await prisma.agentSession.create({
data: {
id: 'session-comment',
agentId: 'agent-123',
userId: user.id,
cwd: process.cwd(),
type: 'comment',
name: null,
},
})
await prisma.agentSession.create({
data: {
id: 'session-named',
agentId: 'agent-123',
userId: user.id,
cwd: process.cwd(),
type: 'chat',
name: 'Existing Name',
},
})
vi.mocked(piAgent.createAgentSession).mockClear()
await generateSessionNameActivity({
teamId: 't1',
agentId: 'agent-123',
prompt: 'hello comment',
sessionId: 'session-comment',
context: {
agent: {} as unknown as GenerateSessionNameParams['context']['agent'],
dbProviders: [],
},
})
await generateSessionNameActivity({
teamId: 't1',
agentId: 'agent-123',
prompt: 'hello named',
sessionId: 'session-named',
context: {
agent: {} as unknown as GenerateSessionNameParams['context']['agent'],
dbProviders: [],
},
})
expect(piAgent.createAgentSession).not.toHaveBeenCalled()
})
})
})
+206 -16
View File
@@ -1,23 +1,29 @@
import { createAgentSession, fieldsToTypeBoxSchema, type AutofillField } from '../index'
import { DatabaseSessionStorage } from '../database-session-storage'
import { prisma, AssetType, Prisma, type Skill } from '@shumai/db'
import { logger } from '@shumai/core/src/logger'
import { s3Service } from '@shumai/core/src/s3/s3'
import {
type AgentTool,
type AgentMessage,
type AgentTool,
type SessionTreeEntry,
} from '@earendil-works/pi-agent-core'
import { type ImageContent } from '@earendil-works/pi-ai'
import { ApplicationFailure } from '@temporalio/activity'
import { assetService } from '@shumai/core/src/asset/asset'
import { VersionStackService } from '@shumai/core/src/versionStack/versionStack'
import { uploadService } from '@shumai/core/src/upload/upload'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { generateKeyBetween } from 'jittered-fractional-indexing'
import { paginateQuery, encodeCursor } from '@shumai/core/src/pagination'
import { ulid } from 'ulid'
import { logger } from '@shumai/core/src/logger'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { encodeCursor, paginateQuery } from '@shumai/core/src/pagination'
import { s3Service } from '@shumai/core/src/s3/s3'
import { uploadService } from '@shumai/core/src/upload/upload'
import { VersionStackService } from '@shumai/core/src/versionStack/versionStack'
import { AssetType, prisma, Prisma, type Skill } from '@shumai/db'
import { ApplicationFailure, Context } from '@temporalio/activity'
import { registerLocalCancelHandler, unregisterLocalCancelHandler } from '@shumai/workflow-core'
import { generateKeyBetween } from 'jittered-fractional-indexing'
import { ulid } from 'ulid'
import { DatabaseSessionStorage } from '../database-session-storage'
import {
createAgentSession,
fieldsToTypeBoxSchema,
type AutofillField,
type DbProviderInfo,
} from '../index'
import { UpdateAssetMetadataRequest } from '@shumai/dtos'
@@ -48,6 +54,7 @@ export interface AgentExecutionContext {
}
async function executeAgentPrompt(params: {
taskId?: string
teamId: string
agentId: string
prompt: string
@@ -58,6 +65,7 @@ async function executeAgentPrompt(params: {
userCommentId?: string | null
tools?: AgentTool[]
context: AgentExecutionContext
attachedAssets?: Array<{ id: string; name: string; type: string }>
}): Promise<{ text: string; usage: Usage; sessionId: string }> {
const { agent, dbProviders, teamSkills, allowedDomains } = params.context
@@ -88,9 +96,8 @@ If you need to create files in the local filesystem (for example, a temporary fi
systemPrompt = `${systemPrompt}\n\nAgent Personality and Core Instructions:\n${agent.soul}`
}
if (params.agentsInstruction) {
systemPrompt = `${systemPrompt}\n\nContext and Instructions:\n${params.agentsInstruction}`
}
// Note: params.agentsInstruction is no longer appended to systemPrompt here.
// Instead, it is persisted as a custom message session entry in the conversation history below.
const modelConfig = agent.modelRef?.config
if (modelConfig?.input) {
@@ -144,7 +151,64 @@ If you need to create files in the local filesystem (for example, a temporary fi
}
}
let heartbeatInterval: ReturnType<typeof setInterval> | null = null
const onAbort = () => {
harness.abort()
}
let temporalSignal: EventTarget | undefined
try {
temporalSignal = Context.current().cancellationSignal
} catch {
// Ignore
}
if (temporalSignal) {
temporalSignal.addEventListener('abort', onAbort, { once: true })
heartbeatInterval = setInterval(() => {
try {
Context.current().heartbeat()
} catch (err) {
logger.debug({ err }, 'Failed to send activity heartbeat')
}
}, 2000)
} else if (params.taskId) {
registerLocalCancelHandler(params.taskId, () => {
harness.abort()
})
}
try {
if (params.agentsInstruction && params.agentsInstruction.trim()) {
const storage = session.getStorage()
const parentId = await storage.getLeafId()
await storage.appendEntry({
id: ulid(),
type: 'custom_message',
parentId,
timestamp: new Date().toISOString(),
customType: 'context',
content: params.agentsInstruction.trim(),
} as unknown as SessionTreeEntry)
}
if (params.attachedAssets && params.attachedAssets.length > 0) {
const storage = session.getStorage()
const parentId = await storage.getLeafId()
await storage.appendEntry({
id: ulid(),
type: 'custom',
parentId,
timestamp: new Date().toISOString(),
customType: 'context_display_info',
data: {
assets: params.attachedAssets,
},
} as unknown as SessionTreeEntry)
}
const assistantMessage = await harness.prompt(params.prompt, { images: imagesToPass })
const sessionEntries = await session.getEntries()
@@ -190,11 +254,20 @@ If you need to create files in the local filesystem (for example, a temporary fi
}
return { text, usage, sessionId }
} finally {
// Cleanup handled by agent session if needed
if (heartbeatInterval) {
clearInterval(heartbeatInterval)
}
if (temporalSignal) {
temporalSignal.removeEventListener('abort', onAbort)
}
if (params.taskId) {
unregisterLocalCancelHandler(params.taskId)
}
}
}
export interface AgentChatParams {
taskId?: string
teamId: string
agentId: string
message: string
@@ -207,6 +280,7 @@ export interface AgentChatParams {
userCommentId?: string | null
explicitMention?: boolean
context: AgentExecutionContext
attachedAssets?: Array<{ id: string; name: string; type: string }>
}
export async function agentChatActivity(params: AgentChatParams) {
@@ -214,6 +288,7 @@ export async function agentChatActivity(params: AgentChatParams) {
const sessionId = params.sessionId
return executeAgentPrompt({
taskId: params.taskId,
teamId: params.teamId,
agentId: params.agentId,
prompt: cleanMessage,
@@ -223,6 +298,7 @@ export async function agentChatActivity(params: AgentChatParams) {
userId: params.userId,
userCommentId: params.userCommentId,
context: params.context,
attachedAssets: params.attachedAssets,
})
}
@@ -1108,3 +1184,117 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
})
}
}
export interface GenerateSessionNameParams {
teamId: string
agentId: string
prompt: string
sessionId: string
context: {
agent: Prisma.AgentGetPayload<{ include: { modelRef: true; provider: true } }>
dbProviders: DbProviderInfo[]
}
}
export async function generateSessionNameActivity(
params: GenerateSessionNameParams,
): Promise<void> {
const { sessionId, prompt, teamId, agentId } = params
const { agent, dbProviders } = params.context
// 1. Fetch current session and verify it is a chatbot session and unnamed
const sessionRecord = await prisma.agentSession.findUnique({
where: { id: sessionId },
})
if (!sessionRecord || sessionRecord.type !== 'chat' || sessionRecord.name !== null) {
logger.info({ sessionId }, 'Skipping session name generation: not an unnamed chatbot session')
return
}
if (!agent || !agent.modelRef || !agent.provider) {
logger.warn(
{ sessionId },
'Skipping session name generation: Agent, model, or provider configuration not found',
)
return
}
const providerName = agent.provider.name
const modelId = agent.modelRef.modelId
let namingSessionId: string | undefined
try {
// 2. Create transient naming session in database
const namingSession = await prisma.agentSession.create({
data: {
agentId,
userId: sessionRecord.userId,
cwd: process.cwd(),
assetId: sessionRecord.assetId,
type: 'naming',
},
})
namingSessionId = namingSession.id
const systemInstruction =
"You are a helpful assistant. Generate a short 2 to 4 words title/name for a chat session based on the user's first message. " +
'Do NOT include quotation marks, markdown formatting, or any extra conversational text. Return only the title.'
// 3. Create agent session and harness using the naming session
const { harness } = await createAgentSession({
teamId,
agentId,
providerName,
modelId,
systemPrompt: systemInstruction,
teamSkills: [],
allowedDomains: [],
sessionId: namingSessionId,
userId: sessionRecord.userId || undefined,
providers: dbProviders,
})
const result = await harness.prompt(prompt)
const resultText = result.content
.filter((c) => c.type === 'text')
.map((c) => {
if ('text' in c && typeof c.text === 'string') {
return c.text
}
return ''
})
.join('\n')
let chatName = resultText.trim()
// Clean up quotes if returned
if (
(chatName.startsWith('"') && chatName.endsWith('"')) ||
(chatName.startsWith("'") && chatName.endsWith("'"))
) {
chatName = chatName.slice(1, -1).trim()
}
if (chatName) {
await prisma.agentSession.update({
where: { id: sessionId },
data: { name: chatName },
})
logger.info({ sessionId, chatName }, 'Session name generated successfully')
}
} catch (err) {
logger.error({ sessionId, err }, 'Failed to generate session name')
} finally {
if (namingSessionId) {
await prisma.agentSession
.delete({
where: { id: namingSessionId },
})
.catch((deleteErr) => {
logger.error({ namingSessionId, deleteErr }, 'Failed to delete naming session')
})
}
}
}
+43
View File
@@ -329,4 +329,47 @@ describe('Sandbox Network isolation integration', () => {
initializeSpy.mockRestore()
}
})
it('should filter out denied tools from AgentHarness', async () => {
const team = await prisma.team.create({
data: { name: 'Test Team 3' },
})
await prisma.user.create({
data: {
id: 'test-agent-3',
name: 'Agent 3',
email: 'agent3@shumai.ai',
type: 'agent',
},
})
await prisma.agent.create({
data: {
id: 'test-agent-3',
teamId: team.id,
type: 'chat',
config: { provider: 'google', model: 'gemini', deniedTools: ['bash'] },
},
})
const initializeSpy = vi.spyOn(SandboxManager, 'initialize').mockResolvedValue()
try {
const { harness } = await createAgentSession({
teamId: team.id,
agentId: 'test-agent-3',
providerName: 'google',
modelId: 'gemini',
systemPrompt: 'prompt',
teamSkills: [],
allowedDomains: [],
providers: [],
})
const tools = harness.getTools()
const bashTool = tools.find((t) => t.name === 'bash')
expect(bashTool).toBeUndefined()
const readSkillTool = tools.find((t) => t.name === 'read_skill')
expect(readSkillTool).toBeDefined()
} finally {
initializeSpy.mockRestore()
}
})
})
+84 -67
View File
@@ -23,6 +23,75 @@ import { createListAssetsTool } from './tools/list-assets'
import { createReadSkillTool } from './tools/read-skill'
import { createSandboxedBashTool } from './tools/sandboxed-bash'
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) return undefined
// Model contains complex internal types from pi-ai
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let m: any
try {
// Try to get built-in model as template
// eslint-disable-next-line @typescript-eslint/no-explicit-any
m = { ...(getModel as any)(providerName, modelId) }
} catch {
// Not a built-in model
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
}
if (dbProvider.config.api) {
m.api = dbProvider.config.api
}
Object.assign(m, dbModel.config)
if (dbModel.name) {
m.name = dbModel.name
}
return m
}
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
@@ -36,15 +105,7 @@ export interface CreateAgentSessionParams {
userCommentId?: string | null
customTools?: AgentTool[]
thinkingLevel?: string
providers: Array<{
name: string
config: PrismaJson.ProviderConfig
models: Array<{
modelId: string
name: string | null
config: PrismaJson.ModelConfig
}>
}>
providers: DbProviderInfo[]
}
export async function createAgentSession(params: CreateAgentSessionParams) {
@@ -74,52 +135,9 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
const agentDir = path.join(process.cwd(), '.pi', 'agents', agentId)
if (!fs.existsSync(agentDir)) fs.mkdirSync(agentDir, { recursive: true })
// Helper to construct a Model object from database providers list
const getModelFromDb = (pName: string, mId: string) => {
const dbProvider = params.providers.find((p) => p.name === pName)
const dbModel = dbProvider?.models.find((m) => m.modelId === mId)
if (!dbProvider || !dbModel) return undefined
// Model contains complex internal types from pi-ai
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let m: any
try {
// Try to get built-in model as template
// eslint-disable-next-line @typescript-eslint/no-explicit-any
m = { ...(getModel as any)(pName, mId) }
} catch {
// Not a built-in model
m = {
id: mId,
name: dbModel.name || mId,
provider: pName,
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
}
if (dbProvider.config.api) {
m.api = dbProvider.config.api
}
Object.assign(m, dbModel.config)
if (dbModel.name) {
m.name = dbModel.name
}
return m
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const model = getModelFromDb(providerName, modelId) || (getModel as any)(providerName, modelId)
const model =
getModelFromDb(params.providers, providerName, modelId) ||
(getModel as unknown as (p: string, m: string) => unknown)(providerName, modelId)
const piDir = path.join(process.cwd(), '.pi')
if (!fs.existsSync(piDir)) fs.mkdirSync(piDir, { recursive: true })
@@ -244,6 +262,15 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
)
}
const agent = await prisma.agent.findUnique({
where: { id: agentId },
})
const agentConfig = agent?.config as PrismaJson.AgentConfig | null | undefined
const deniedTools = agentConfig?.deniedTools || []
const allTools = [...mediaTools, readSkill, sandboxedBash, ...systemTools, ...customTools]
const enabledTools = allTools.filter((tool) => !deniedTools.includes(tool.name))
const harness = new AgentHarness({
env: new NodeExecutionEnv({ cwd: process.cwd() }),
session,
@@ -269,19 +296,9 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
return prompt
},
getApiKeyAndHeaders: async (targetModel) => {
// 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 = params.providers.find((p) => p.name === targetModel.provider)
const providerApiKey = dbProvider?.config?.apiKey
if (providerApiKey) {
return { apiKey: resolveKey(providerApiKey)! }
}
return undefined
return getApiKeyAndHeadersFromDb(params.providers, targetModel.provider)
},
tools: [...mediaTools, readSkill, sandboxedBash, ...systemTools, ...customTools],
tools: enabledTools,
})
return { session, harness }
@@ -73,4 +73,34 @@ describe('AgentChatPromptBuilder', () => {
expect(result).toContain("call the 'screenshot' tool")
expect(result).toContain('The user explicitly mentioned you')
})
it('should build prompt with attached files and referenced assets', () => {
const builder = new AgentChatPromptBuilder('a1')
.withAttachedFiles(['- file1.txt', '- file2.png'])
.withReferencedAssets(['- folder1', '- file3.mp4'])
const result = builder.build()
expect(result).toContain('[Context: Attached Files & Referenced Assets]')
expect(result).toContain('Attached Files:\n- file1.txt\n- file2.png')
expect(result).toContain('Referenced Workspace Assets:\n- folder1\n- file3.mp4')
})
it('should build prompt for continuation mode returning only turn-specific modifications', () => {
const builder = new AgentChatPromptBuilder('a1')
.withContinuation(true)
.withAssetDetails('movie.mp4', 'video/mp4', 12.34)
.withPathContext('src/main.ts')
.withCommentTimestamp(8.2)
.withAttachedFiles(['- file1.txt'])
.withReferencedAssets(['- folder1'])
const result = builder.build()
expect(result).not.toContain('The user is discussing an asset with ID:')
expect(result).not.toContain('File Name: movie.mp4')
expect(result).not.toContain('Asset Path Context:')
expect(result).toContain('Comment Timestamp: 8.20 seconds')
expect(result).toContain('[Context: New Attached Files & Referenced Assets]')
expect(result).toContain('Attached Files:\n- file1.txt')
expect(result).toContain('Referenced Workspace Assets:\n- folder1')
})
})
@@ -22,11 +22,34 @@ export class AgentChatPromptBuilder {
private commentTimestamp?: number
private pathContext?: string
private explicitMention = false
private attachedFiles: string[] = []
private referencedAssets: string[] = []
private isContinuation = false
constructor(assetId: string) {
this.assetId = assetId
}
withContinuation(isContinuation: boolean): this {
this.isContinuation = isContinuation
return this
}
withAttachedFiles(files?: string[]): this {
if (files) {
this.attachedFiles = files
}
return this
}
withReferencedAssets(assets?: string[]): this {
if (assets) {
this.referencedAssets = assets
}
return this
}
withPathContext(pathContext?: string): this {
this.pathContext = pathContext
return this
@@ -52,6 +75,23 @@ export class AgentChatPromptBuilder {
}
build(): string {
if (this.isContinuation) {
let instruction = ''
if (this.commentTimestamp !== undefined) {
instruction += `Comment Timestamp: ${this.commentTimestamp.toFixed(2)} seconds`
}
if (this.attachedFiles.length > 0 || this.referencedAssets.length > 0) {
instruction += `\n\n[Context: New Attached Files & Referenced Assets]`
if (this.attachedFiles.length > 0) {
instruction += `\nAttached Files:\n${this.attachedFiles.join('\n')}`
}
if (this.referencedAssets.length > 0) {
instruction += `\nReferenced Workspace Assets:\n${this.referencedAssets.join('\n')}`
}
}
return instruction.trim()
}
let instruction = `The user is discussing an asset with ID: ${this.assetId}.`
if (this.assetName) {
instruction += `\nFile Name: ${this.assetName}`
@@ -70,6 +110,16 @@ export class AgentChatPromptBuilder {
instruction += `\n\nAsset Path Context:\n${this.pathContext}`
}
if (this.attachedFiles.length > 0 || this.referencedAssets.length > 0) {
instruction += `\n\n[Context: Attached Files & Referenced Assets]`
if (this.attachedFiles.length > 0) {
instruction += `\nAttached Files:\n${this.attachedFiles.join('\n')}`
}
if (this.referencedAssets.length > 0) {
instruction += `\nReferenced Workspace Assets:\n${this.referencedAssets.join('\n')}`
}
}
if (this.mediaType) {
const type = getSimpleMediaType(this.mediaType)
if (type === 'image') {
@@ -46,11 +46,15 @@ describe('Agent Chat Workflow', () => {
getAssetPathContextActivity: Object.assign(vi.fn(), {
_activityName: 'getAssetPathContextActivity',
}),
generateSessionNameActivity: Object.assign(vi.fn(), {
_activityName: 'generateSessionNameActivity',
}),
}
mockActivities.getAgentWorkerQueueActivity.mockResolvedValue('agent_queue')
mockActivities.updateTaskStatusActivity.mockResolvedValue({})
mockActivities.createCommentActivity.mockResolvedValue({ id: 'comment-placeholder-id' })
mockActivities.generateSessionNameActivity.mockResolvedValue(undefined)
mockActivities.getAssetActivity.mockResolvedValue({
id: 'a1',
project: { teamId: 't1' },
@@ -166,6 +170,15 @@ describe('Agent Chat Workflow', () => {
status: 'completed',
output: { sessionId: 'session-123' },
})
// Verify session name generation triggered
expect(mockActivities.generateSessionNameActivity).toHaveBeenCalledWith({
teamId: 't1',
agentId: 'b1',
prompt: 'hello agent',
sessionId: 'session-123',
context: expect.any(Object),
})
})
it('should skip session initialization if sessionId is already provided', async () => {
@@ -185,6 +198,7 @@ describe('Agent Chat Workflow', () => {
expect(mockActivities.initializeAgentSessionActivity).not.toHaveBeenCalled()
const expectedInstruction2 = new AgentChatPromptBuilder('a1')
.withContinuation(true)
.withPathContext('Path: folder/subfolder/file.png')
.withAssetDetails('test-file.png', 'image/png', undefined)
.withCommentTimestamp(null)
@@ -197,6 +211,9 @@ describe('Agent Chat Workflow', () => {
agentsInstruction: expectedInstruction2,
}),
)
// Verify session name generation is skipped
expect(mockActivities.generateSessionNameActivity).not.toHaveBeenCalled()
})
it('should delete placeholder comment and finish when AI responds with __NO_REPLY__', async () => {
@@ -397,4 +414,102 @@ describe('Agent Chat Workflow', () => {
message: 'Sorry, I encountered an error while processing your request.',
})
})
it('should execute direct-context chatbot flow resolving file metadata and skipping comment placeholder', async () => {
mockActivities.getAssetActivity.mockImplementation(async (id: string) => {
if (id === 'file-attachment-1') {
return {
id: 'file-attachment-1',
name: 'attachment.png',
type: 'file',
mediaType: 'image/png',
projectId: 'p1',
}
}
if (id === 'referenced-asset-1') {
return {
id: 'referenced-asset-1',
name: 'ref-folder',
type: 'folder',
mediaType: null,
projectId: 'p1',
}
}
return {
id,
name: 'test-file.png',
type: 'file',
mediaType: 'image/png',
projectId: 'p1',
project: { teamId: 't1' },
}
})
mockActivities.getAssetPathContextActivity.mockImplementation(async (id: string) => {
if (id === 'file-attachment-1') return 'attachment.png'
if (id === 'referenced-asset-1') return 'ref-folder'
return 'folder/subfolder/file.png'
})
const task = await prisma.workflowTask.create({
data: {
type: 'chat',
status: 'pending',
assetId: 'a1',
payload: {
projectId: 'p1',
agent: {
agentId: 'b1',
sessionId: 'session-direct-123',
prompt: 'chatbot prompt',
attachedFiles: ['file-attachment-1'],
assetIds: ['referenced-asset-1'],
explicitMention: true,
},
},
},
})
await agentChat(task)
// Verify placeholder comment is NOT created
expect(mockActivities.createCommentActivity).not.toHaveBeenCalled()
expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled()
// Verify prompt builder is populated and workflow runs agentChatActivity with context instruction
const expectedInstruction = new AgentChatPromptBuilder('a1')
.withContinuation(true)
.withPathContext('folder/subfolder/file.png')
.withAssetDetails('test-file.png', 'image/png', undefined)
.withCommentTimestamp(undefined)
.withExplicitMention(true)
.withAttachedFiles([
'- Name: attachment.png (ID: file-attachment-1, Type: file, Media Type: image/png, Project ID: p1, Path: attachment.png)',
])
.withReferencedAssets([
'- Name: ref-folder (ID: referenced-asset-1, Type: folder, Media Type: unknown, Project ID: p1, Path: ref-folder)',
])
.build()
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
teamId: 't1',
agentId: 'b1',
message: 'chatbot prompt',
imageUrls: [],
projectId: 'p1',
folderId: '',
agentsInstruction: expectedInstruction,
sessionId: 'session-direct-123',
userId: undefined,
userCommentId: undefined,
explicitMention: true,
context: { agent: { id: 'b1' } },
attachedAssets: [
{ id: 'file-attachment-1', name: 'attachment.png', type: 'file' },
{ id: 'referenced-asset-1', name: 'ref-folder', type: 'folder' },
],
}),
)
})
})
+118 -36
View File
@@ -17,6 +17,7 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
deleteCommentActivity,
initializeAgentSessionActivity,
getAssetPathContextActivity,
generateSessionNameActivity,
} = getActivities()
let placeholderCommentId: string | undefined
@@ -46,26 +47,42 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
// 1. Get User Comment
const userCommentId = payload.agent?.userCommentId
if (!userCommentId) {
throw ApplicationFailure.create({
message: 'userCommentId missing in payload',
nonRetryable: true,
})
}
const userComment = await executeActivity(agentWorkerQueue, getCommentActivity, userCommentId)
if (!userComment) {
throw ApplicationFailure.create({ message: 'User comment not found', nonRetryable: true })
}
let prompt = ''
let attachmentImageUrls: string[] = []
let commentTimestamp: number | undefined
// 2. Create Placeholder Comment
const placeholder = await executeActivity(agentWorkerQueue, createCommentActivity, {
assetId: task.assetId,
message: '__CHAT__',
sessionId: sessionId || 'pending',
agentId: agentId,
replyToId: userComment.replyToId ?? userComment.id,
})
placeholderCommentId = placeholder.id
if (userCommentId) {
const userComment = await executeActivity(agentWorkerQueue, getCommentActivity, userCommentId)
if (!userComment) {
throw ApplicationFailure.create({ message: 'User comment not found', nonRetryable: true })
}
prompt = userComment.message || ''
if (userComment.second !== null && userComment.second !== undefined) {
commentTimestamp = userComment.second
}
// 2. Create Placeholder Comment
const placeholder = await executeActivity(agentWorkerQueue, createCommentActivity, {
assetId: task.assetId,
message: '__CHAT__',
sessionId: sessionId || 'pending',
agentId: agentId,
replyToId: userComment.replyToId ?? userComment.id,
})
placeholderCommentId = placeholder.id
// 5. Prepare Images (Attachments only)
if (userComment.attachments) {
for (const att of userComment.attachments) {
if (att.asset?.mediaType?.startsWith('image/') && att.asset?.storageKey?.key) {
attachmentImageUrls.push(att.asset.storageKey.key)
}
}
}
} else {
prompt = payload.agent?.prompt || ''
attachmentImageUrls = payload.agent?.imageUrls || []
}
// 3. Get Asset
const asset = await executeActivity(agentWorkerQueue, getAssetActivity, task.assetId)
@@ -75,13 +92,21 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
const teamId = asset.project.teamId
// 4. Initialize Session if missing
let isNewChat = !payload.agent?.sessionId || payload.agent?.isNewChat === true
if (!sessionId) {
if (!userCommentId) {
throw ApplicationFailure.create({
message: 'sessionId is required when userCommentId is missing',
nonRetryable: true,
})
}
sessionId = await executeActivity(agentWorkerQueue, initializeAgentSessionActivity, {
teamId,
agentId: agentId,
userCommentId,
userId: payload.agent?.userId,
})
isNewChat = true
}
// 4b. Fetch Agent Context
@@ -90,16 +115,6 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
agentId: agentId,
})
// 5. Prepare Images (Attachments only)
const attachmentImageUrls: string[] = []
if (userComment?.attachments) {
for (const att of userComment.attachments) {
if (att.asset?.mediaType?.startsWith('image/') && att.asset?.storageKey?.key) {
attachmentImageUrls.push(att.asset.storageKey.key)
}
}
}
const pathContext = await executeActivity(
agentWorkerQueue,
getAssetPathContextActivity,
@@ -110,11 +125,49 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
const mediaInfo = asset.media as any
const duration = mediaInfo?.duration
// Resolve Attached Files Details
const attachedFileDetailsList: string[] = []
const attachedAssets: Array<{ id: string; name: string; type: string }> = []
if (payload.agent?.attachedFiles) {
for (const fileId of payload.agent.attachedFiles) {
const file = await executeActivity(agentWorkerQueue, getAssetActivity, fileId)
if (file) {
const path = await executeActivity(agentWorkerQueue, getAssetPathContextActivity, fileId)
attachedFileDetailsList.push(
`- Name: ${file.name} (ID: ${file.id}, Type: ${file.type}, Media Type: ${file.mediaType || 'unknown'}, Project ID: ${file.projectId || 'unknown'}, Path: ${path})`,
)
attachedAssets.push({ id: file.id, name: file.name, type: file.type })
}
}
}
// Resolve Referenced Workspace Assets Details
const referencedAssetDetailsList: string[] = []
if (payload.agent?.assetIds) {
for (const assetId of payload.agent.assetIds) {
const referencedAsset = await executeActivity(agentWorkerQueue, getAssetActivity, assetId)
if (referencedAsset) {
const path = await executeActivity(agentWorkerQueue, getAssetPathContextActivity, assetId)
referencedAssetDetailsList.push(
`- Name: ${referencedAsset.name} (ID: ${referencedAsset.id}, Type: ${referencedAsset.type}, Media Type: ${referencedAsset.mediaType || 'unknown'}, Project ID: ${referencedAsset.projectId || 'unknown'}, Path: ${path})`,
)
attachedAssets.push({
id: referencedAsset.id,
name: referencedAsset.name,
type: referencedAsset.type,
})
}
}
}
const instruction = new AgentChatPromptBuilder(asset.id)
.withContinuation(!isNewChat)
.withPathContext(pathContext)
.withAssetDetails(asset.name, asset.mediaType, duration)
.withCommentTimestamp(userComment.second)
.withCommentTimestamp(commentTimestamp)
.withExplicitMention(payload.agent?.explicitMention)
.withAttachedFiles(attachedFileDetailsList)
.withReferencedAssets(referencedAssetDetailsList)
.build()
// 6. Call AI Chat
@@ -126,20 +179,34 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
}
const aiResult = await executeActivity(agentWorkerQueue, agentChatActivity, {
taskId: task.id,
teamId,
agentId: agentId,
message: userComment.message || '',
message: prompt,
imageUrls: attachmentImageUrls,
projectId: payload.projectId,
folderId,
agentsInstruction: instruction,
sessionId,
userId: payload.agent?.userId,
userCommentId,
userCommentId: userCommentId || undefined,
explicitMention: payload.agent?.explicitMention,
context,
attachedAssets,
})
if (isNewChat) {
await executeActivity(agentWorkerQueue, generateSessionNameActivity, {
teamId,
agentId,
prompt,
sessionId,
context,
}).catch((err) => {
console.error('Failed to run generateSessionNameActivity:', err)
})
}
// 7. Update Placeholder Comment
if (placeholderCommentId) {
if (aiResult.text.trim() === '__NO_REPLY__') {
@@ -175,10 +242,25 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
// Update placeholder comment with error message
if (placeholderCommentId && agentWorkerQueue) {
try {
const errorMessage =
err instanceof Error && err.message.startsWith('AI error:')
? `AI error: ${err.message.substring(9)}`
: 'Sorry, I encountered an error while processing your request.'
let errorMessage = 'Sorry, I encountered an error while processing your request.'
if (err instanceof Error) {
if (err.message.startsWith('AI error:')) {
errorMessage = `AI error: ${err.message.substring(9)}`
} else if (
err.message.includes('aborted') ||
err.message.includes('abort') ||
err.name === 'AbortError' ||
err.message.includes('cancel')
) {
errorMessage = 'Agent execution stopped.'
}
} else if (
String(err).includes('aborted') ||
String(err).includes('abort') ||
String(err).includes('cancel')
) {
errorMessage = 'Agent execution stopped.'
}
await executeActivity(agentWorkerQueue, updateCommentActivity, {
commentId: placeholderCommentId,
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/api",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"main": "./src/index.ts",
+14 -2
View File
@@ -27,6 +27,7 @@ describe('Agent API', () => {
model: 'model1',
thinkingLevel: 'high',
systemPrompt: 'prompt',
deniedTools: ['bash'],
},
skills: [],
},
@@ -43,6 +44,7 @@ describe('Agent API', () => {
expect(data).toHaveLength(1)
expect(data[0].name).toBe('Bot 1')
expect(data[0].providerId).toBe('prov1')
expect(data[0].deniedTools).toEqual(['bash'])
})
})
@@ -55,7 +57,13 @@ describe('Agent API', () => {
providerId: 'prov1',
modelId: 'model1',
soul: 'New Soul',
config: { provider: 'prov1', model: 'model1', thinkingLevel: 'low', systemPrompt: 'p' },
config: {
provider: 'prov1',
model: 'model1',
thinkingLevel: 'low',
systemPrompt: 'p',
deniedTools: ['bash'],
},
skills: [],
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -73,6 +81,7 @@ describe('Agent API', () => {
systemPrompt: 'p',
soul: 'New Soul',
skills: ['skill1'],
deniedTools: ['bash'],
}),
})
@@ -80,6 +89,7 @@ describe('Agent API', () => {
const data = await res.json()
expect(data.name).toBe('New Agent')
expect(data.soul).toBe('New Soul')
expect(data.deniedTools).toEqual(['bash'])
})
it('validates request', async () => {
@@ -105,7 +115,7 @@ describe('Agent API', () => {
providerId: 'prov1',
modelId: 'model1',
soul: 'Updated Soul',
config: { provider: 'prov1', model: 'model1' },
config: { provider: 'prov1', model: 'model1', deniedTools: ['bash'] },
skills: [],
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -121,6 +131,7 @@ describe('Agent API', () => {
modelId: 'model1',
soul: 'Updated Soul',
skills: [],
deniedTools: ['bash'],
}),
})
@@ -128,6 +139,7 @@ describe('Agent API', () => {
const data = await res.json()
expect(data.name).toBe('Updated Agent')
expect(data.soul).toBe('Updated Soul')
expect(data.deniedTools).toEqual(['bash'])
})
})
+3
View File
@@ -47,6 +47,7 @@ const route = new Hono<{ Variables: { user: User } }>()
skillId: s.skillId,
skill: s.skill,
})),
deniedTools: config.deniedTools || [],
}
}),
)
@@ -89,6 +90,7 @@ const route = new Hono<{ Variables: { user: User } }>()
skillId: s.skillId,
skill: s.skill,
})),
deniedTools: config.deniedTools || [],
}
return c.json(info, 200)
@@ -127,6 +129,7 @@ const route = new Hono<{ Variables: { user: User } }>()
skillId: s.skillId,
skill: s.skill,
})),
deniedTools: config.deniedTools || [],
}
return c.json(info, 200)
+214
View File
@@ -0,0 +1,214 @@
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'
vi.mock('@shumai/core/src/chat/chat', async (importOriginal) => {
const original = await importOriginal<typeof import('@shumai/core/src/chat/chat')>()
return {
...original,
chatService: {
listSessions: vi.fn(),
listMessages: vi.fn(),
deleteSession: vi.fn(),
startOrContinueChat: vi.fn(),
getNewSessionMessages: vi.fn(),
getChatWorkflowStatus: vi.fn(),
abortSession: vi.fn(),
},
}
})
vi.mock('@shumai/core/src/authz/authz')
describe('Chat API', () => {
const app = new Hono<{ Variables: { user: { id: string; name: string } } }>()
.use('*', async (c, next) => {
c.set('user', { id: 'user1', name: 'Test User' })
await next()
})
.route('/', chatRoute)
beforeEach(() => {
vi.restoreAllMocks()
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
})
describe('GET /teams/:teamId/chat/sessions', () => {
it('returns list of sessions', async () => {
const mockSessions = {
data: [
{
id: 'session1',
agentId: 'default',
userId: 'user1',
assetId: 'asset1',
userCommentId: 'comment1',
createdAt: '2026-07-07T10:00:00Z',
updatedAt: '2026-07-07T10:00:00Z',
},
],
pageInfo: { total: 1 },
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(chatService.listSessions).mockResolvedValue(mockSessions as any)
const res = await app.request('/teams/team1/chat/sessions?first=10')
expect(res.status).toBe(200)
const data = await res.json()
expect(data.data).toHaveLength(1)
expect(data.data[0].id).toBe('session1')
expect(chatService.listSessions).toHaveBeenCalledWith('user1', 'team1', expect.any(Object))
})
})
describe('GET /teams/:teamId/chat/sessions/:sessionId/messages', () => {
it('returns list of messages', async () => {
const mockMessages = [
{
id: 'msg1',
role: 'user',
content: 'hello',
timestamp: '2026-07-07T10:00:00Z',
entry: {},
},
]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(chatService.listMessages).mockResolvedValue(mockMessages as any)
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(chatService.listMessages).toHaveBeenCalledWith('user1', 'team1', 'session1')
})
})
describe('DELETE /teams/:teamId/chat/sessions/:sessionId', () => {
it('deletes a session', async () => {
vi.mocked(chatService.deleteSession).mockResolvedValue(undefined)
const res = await app.request('/teams/team1/chat/sessions/session1', {
method: 'DELETE',
})
expect(res.status).toBe(204)
expect(chatService.deleteSession).toHaveBeenCalledWith('user1', 'team1', 'session1')
})
})
describe('POST /teams/:teamId/chat', () => {
it('starts or continues chat and returns stream', async () => {
vi.mocked(chatService.startOrContinueChat).mockResolvedValue({
sessionId: 'session1',
taskId: 'task1',
})
vi.mocked(chatService.getNewSessionMessages)
.mockResolvedValueOnce({
messages: [
{
id: 'entry1',
role: 'user',
content: 'hi',
} as unknown as ChatMessage,
],
lastEntryId: 'entry1',
})
.mockResolvedValue({
messages: [],
lastEntryId: 'entry1',
})
vi.mocked(chatService.getChatWorkflowStatus).mockResolvedValue({
status: 'completed',
output: {},
})
const res = await app.request('/teams/team1/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
agentId: 'agent1',
textPrompt: 'hi',
}),
})
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('text/event-stream')
const text = await res.text()
expect(text).toContain('data:')
expect(text).toContain('entry1')
expect(chatService.startOrContinueChat).toHaveBeenCalledWith(
expect.objectContaining({ id: 'user1' }),
'team1',
expect.any(Object),
)
})
it('sends ping keep-alive SSE events when there are no new messages', async () => {
vi.mocked(chatService.startOrContinueChat).mockResolvedValue({
sessionId: 'session1',
taskId: 'task1',
})
vi.mocked(chatService.getNewSessionMessages).mockResolvedValue({
messages: [],
lastEntryId: null,
})
vi.mocked(chatService.getChatWorkflowStatus).mockResolvedValue({
status: 'completed',
output: {},
})
const res = await app.request('/teams/team1/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
agentId: 'agent1',
textPrompt: 'hi',
}),
})
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('text/event-stream')
const text = await res.text()
expect(text).toContain('data:')
expect(text).toContain('{"type":"ping"}')
})
})
describe('POST /teams/:teamId/chat/sessions/:sessionId/abort', () => {
it('aborts an active chat session and updates task status', async () => {
vi.mocked(chatService.abortSession).mockResolvedValue(undefined)
const res = await app.request('/teams/team1/chat/sessions/session1/abort', {
method: 'POST',
})
expect(res.status).toBe(200)
const data = await res.json()
expect(data).toEqual({ success: true })
expect(chatService.abortSession).toHaveBeenCalledWith('user1', 'session1')
})
it('returns 404 if no active task is found for the session', async () => {
vi.mocked(chatService.abortSession).mockRejectedValue(
new Error('No active execution found for this session'),
)
const res = await app.request('/teams/team1/chat/sessions/session1/abort', {
method: 'POST',
})
expect(res.status).toBe(404)
const data = await res.json()
expect(data.error).toBe('No active execution found for this session')
})
})
})
+193
View File
@@ -0,0 +1,193 @@
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 type { Prisma } from '@shumai/db'
type User = Prisma.UserGetPayload<Record<string, never>>
const route = new Hono<{ Variables: { user: User } }>()
.get('/teams/:teamId/chat/sessions', zValidator('query', paginationParamsSchema), async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
const query = c.req.valid('query')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Team,
id: teamId,
})
const res = await chatService.listSessions(user.id, teamId, query)
return c.json(res, 200)
})
.get('/teams/:teamId/chat/sessions/:sessionId/messages', async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
const sessionId = c.req.param('sessionId')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Team,
id: teamId,
})
const res = await chatService.listMessages(user.id, teamId, sessionId)
return c.json(res, 200)
})
.delete('/teams/:teamId/chat/sessions/:sessionId', async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
const sessionId = c.req.param('sessionId')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Team,
id: teamId,
})
await chatService.deleteSession(user.id, teamId, sessionId)
return new Response(null, { status: 204 })
})
.post('/teams/:teamId/chat', zValidator('json', chatRequestSchema), async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
const req = c.req.valid('json')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Team,
id: teamId,
})
const { sessionId, taskId } = await chatService.startOrContinueChat(user, teamId, req)
c.header('x-session-id', sessionId)
return streamSSE(c, async (stream) => {
// Send session ID as the first event
await stream.writeSSE({
data: JSON.stringify({
type: 'session',
sessionId,
}),
})
let lastEntryId = ''
const timeoutMs = 5 * 60 * 1000 // 5 minutes timeout
const startTime = Date.now()
while (Date.now() - startTime < timeoutMs) {
if (stream.aborted) {
break
}
// Query new entries from the DB via chatService
const { messages, lastEntryId: newLastId } = await chatService.getNewSessionMessages(
sessionId,
lastEntryId || undefined,
)
if (messages.length > 0) {
for (const message of messages) {
await stream.writeSSE({
data: JSON.stringify({
type: 'entry',
entry: message,
}),
})
}
} else {
await stream.writeSSE({
data: JSON.stringify({
type: 'ping',
}),
})
}
if (newLastId) {
lastEntryId = newLastId
}
// Check if workflow has finished via chatService
const task = await chatService.getChatWorkflowStatus(taskId)
if (task && (task.status === 'completed' || task.status === 'failed')) {
// Fetch any remaining entries one last time
const { messages: finalMessages } = await chatService.getNewSessionMessages(
sessionId,
lastEntryId || undefined,
)
for (const message of finalMessages) {
await stream.writeSSE({
data: JSON.stringify({
type: 'entry',
entry: message,
}),
})
}
// Send done status
const output = task.output
await stream.writeSSE({
data: JSON.stringify({
type: 'done',
status: task.status,
error:
task.status === 'failed'
? (output?.error as string) || 'Workflow failed'
: undefined,
}),
})
break
}
// Wait before polling again
await stream.sleep(1000)
}
if (Date.now() - startTime >= timeoutMs) {
await stream.writeSSE({
data: JSON.stringify({
type: 'done',
status: 'failed',
error: 'Request timed out',
}),
})
}
})
})
.post('/teams/:teamId/chat/sessions/:sessionId/abort', async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
const sessionId = c.req.param('sessionId')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Team,
id: teamId,
})
try {
await chatService.abortSession(user.id, sessionId)
return c.json({ success: true }, 200)
} catch (error: unknown) {
const err = error as Error
if (
err.message === 'Session not found' ||
err.message === 'Unauthorized session access' ||
err.message === 'No active execution found for this session'
) {
return c.json({ error: err.message }, 404)
}
throw error
}
})
export default route
+25
View File
@@ -57,6 +57,7 @@ describe('file api', () => {
vi.spyOn(assetService, 'deleteAssets').mockImplementation(vi.fn())
vi.spyOn(assetService, 'createComment').mockImplementation(vi.fn())
vi.spyOn(assetService, 'completeComment').mockImplementation(vi.fn())
vi.spyOn(assetService, 'deleteComment').mockImplementation(vi.fn())
vi.spyOn(assetService, 'listComments').mockImplementation(vi.fn())
vi.spyOn(assetService, 'restoreAssets').mockImplementation(vi.fn())
vi.spyOn(metadataService, 'updateAssetMetadata').mockImplementation(vi.fn())
@@ -358,6 +359,30 @@ describe('file api', () => {
})
})
it('DELETE /comments/:commentId', async () => {
vi.mocked(assetService.deleteComment).mockResolvedValue(undefined)
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/comments/comment-id', {
method: 'DELETE',
})
expect(res.status).toBe(200)
const json = await res.json()
expect(json.success).toBe(true)
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user1', name: 'Test User' },
permission: Permission.Read,
type: ResourceType.Comment,
id: 'comment-id',
})
expect(assetService.deleteComment).toHaveBeenCalledWith({
commentId: 'comment-id',
userId: 'user1',
})
})
it('POST /teams/:teamId/files - image with compression', async () => {
vi.mocked(s3Service.putObject).mockResolvedValue(undefined)
+14
View File
@@ -220,6 +220,20 @@ const route = new Hono<{ Variables: { user: User } }>()
return c.json(updated)
},
)
.delete('/comments/:commentId', async (c) => {
const commentId = c.req.param('commentId')
const user = c.get('user')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Comment,
id: commentId,
})
await assetService.deleteComment({ commentId, userId: user.id })
return c.json({ success: true })
})
.post('/files/restore', zValidator('json', restoreFilesRequestSchema), async (c) => {
const user = c.get('user')
const req = c.req.valid('json')
+2
View File
@@ -1,6 +1,7 @@
import type { Prisma } from '@shumai/db'
import { Hono } from 'hono'
import agentRoute from './agent'
import chatRoute from './chat'
import attachmentRoute from './attachment'
import authnRoute from './authn'
import collectionRoute from './collection'
@@ -54,6 +55,7 @@ const apiRoute = new Hono<{ Variables: { user: User } }>()
.route('/', fileRoute)
.route('/', folderRoute)
.route('/', agentRoute)
.route('/', chatRoute)
.route('/', attachmentRoute)
.route('/', uploadRoute)
.route('/', shareRoute)
+2 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/core",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"main": "./src/index.ts",
@@ -8,6 +8,7 @@
"dependencies": {
"@shumai/db": "workspace:*",
"@shumai/dtos": "workspace:*",
"@earendil-works/pi-agent-core": "0.78.0",
"@sinclair/typebox": "0.34.49",
"adm-zip": "0.5.17",
"better-auth": "1.6.11",
+24
View File
@@ -126,6 +126,27 @@ describe('AgentService', () => {
expect(agent?.skills?.[0].skillId).toBe(skill.id)
})
test('Create Agent with Denied Tools', async () => {
const db = prisma
const svc = new AgentService()
const { team, provider, model } = await setupTestData(db)
const agent = await svc.createAgent({
teamId: team.id,
name: 'Denied Tools Agent',
type: 'chat',
enabled: true,
providerId: provider.id,
modelId: model.id,
thinkingLevel: 'medium',
deniedTools: ['bash', 'screenshot'],
})
expect(agent?.id).toBeDefined()
const config = agent?.config as unknown as PrismaJson.AgentConfig
expect(config.deniedTools).toEqual(['bash', 'screenshot'])
})
test('Create Agent validation fails if model mismatch', async () => {
const db = prisma
const svc = new AgentService()
@@ -186,8 +207,11 @@ describe('AgentService', () => {
thinkingLevel: 'high',
soul: 'New soul',
skills: [skill.id], // Re-enable one
deniedTools: ['list_assets'],
})
expect(reUpdated.skills?.length).toBe(1)
const config = reUpdated.config as unknown as PrismaJson.AgentConfig
expect(config.deniedTools).toEqual(['list_assets'])
})
test('Create Agent with presigned URL extracts S3 key', async () => {
+4
View File
@@ -114,6 +114,7 @@ export class AgentService {
systemPrompt,
soul,
skills,
deniedTools,
} = params
if (type === 'embedding' || type === 'autofill') {
@@ -152,6 +153,7 @@ export class AgentService {
model: modelId || '',
thinkingLevel,
systemPrompt,
deniedTools: deniedTools || [],
}
const user = await tx.user.create({
@@ -211,6 +213,7 @@ export class AgentService {
systemPrompt,
soul,
skills,
deniedTools,
} = params
const agent = await this.prismaClient.agent.findUnique({
@@ -251,6 +254,7 @@ export class AgentService {
model: modelId || '',
thinkingLevel,
systemPrompt,
deniedTools: deniedTools || [],
}
return this.prismaClient.$transaction(async (tx) => {
+156
View File
@@ -1,6 +1,7 @@
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { HTTPException } from 'hono/http-exception'
import { AssetType, AssetStatus, Prisma } from '@shumai/db'
import { AssetService } from './asset'
@@ -721,6 +722,161 @@ describe('AssetService', () => {
expect(list.data[0].replies.length).toBe(1)
})
it('deleteComment validation and execution', async () => {
const { user, project, assets } = await setupBasicAssets()
// 1. Create a comment
const comment = await assetService.createComment({
assetId: assets.fileA1.id,
userId: user.id,
message: 'Self Comment',
attachmentIds: [],
})
// 2. Add user as a team member with owner role (team owner)
await prisma.teamMember.create({
data: {
teamId: project.teamId,
userId: user.id,
role: 'owner',
},
})
// Create another user
const otherUser = await prisma.user.create({
data: {
name: 'OtherUser',
type: 'human',
email: `other-${Date.now()}@example.com`,
},
})
// Add other user to the team as editor
await prisma.teamMember.create({
data: {
teamId: project.teamId,
userId: otherUser.id,
role: 'editor',
},
})
// Create comment by other user
const otherComment = await assetService.createComment({
assetId: assets.fileA1.id,
userId: otherUser.id,
message: 'Other Comment',
attachmentIds: [],
})
// 3. User (owner) should be able to delete their own comment
await expect(
assetService.deleteComment({ commentId: comment.id, userId: user.id }),
).resolves.not.toThrow()
// Verify it is deleted from DB
await expect(prisma.assetComment.findUnique({ where: { id: comment.id } })).resolves.toBeNull()
// 4. User (owner) should be able to delete other user's comment
await expect(
assetService.deleteComment({ commentId: otherComment.id, userId: user.id }),
).resolves.not.toThrow()
// Verify it is deleted from DB
await expect(
prisma.assetComment.findUnique({ where: { id: otherComment.id } }),
).resolves.toBeNull()
// Create a new comment by user
const comment2 = await assetService.createComment({
assetId: assets.fileA1.id,
userId: user.id,
message: 'Self Comment 2',
attachmentIds: [],
})
// 5. Other user (editor, non-creator) should NOT be able to delete user's comment
await expect(
assetService.deleteComment({ commentId: comment2.id, userId: otherUser.id }),
).rejects.toThrow(HTTPException)
// 6. Other user (editor, creator) should be able to delete their own comment
const otherComment2 = await assetService.createComment({
assetId: assets.fileA1.id,
userId: otherUser.id,
message: 'Other Comment 2',
attachmentIds: [],
})
await expect(
assetService.deleteComment({ commentId: otherComment2.id, userId: otherUser.id }),
).resolves.not.toThrow()
})
it('deleteComment cleans up S3 attachment files and database records', async () => {
const { user, project, assets } = await setupBasicAssets()
// 1. Add user as team owner
await prisma.teamMember.create({
data: {
teamId: project.teamId,
userId: user.id,
role: 'owner',
},
})
// 2. Create StorageKey and Asset for attachment
const storageKey = await prisma.storageKey.create({
data: {
key: 'attachments/test-attachment-file.png',
status: 'active',
},
})
const attachmentAsset = await prisma.asset.create({
data: {
name: 'test-attachment-file.png',
type: AssetType.file,
projectId: project.id,
parentId: assets.root.id,
creatorId: user.id,
storageKeyId: storageKey.id,
sizeByte: 100,
status: 'uploaded',
},
})
// 3. Create a comment with this attachment
const comment = await assetService.createComment({
assetId: assets.fileA1.id,
userId: user.id,
message: 'Comment with attachment',
attachmentIds: [attachmentAsset.id],
})
// Verify it is created in DB
const dbComment = await prisma.assetComment.findUnique({
where: { id: comment.id },
include: { attachments: true },
})
expect(dbComment?.attachments.length).toBe(1)
// 4. Delete the comment
await expect(
assetService.deleteComment({ commentId: comment.id, userId: user.id }),
).resolves.not.toThrow()
// 5. Verify the comment is deleted from DB
await expect(prisma.assetComment.findUnique({ where: { id: comment.id } })).resolves.toBeNull()
// 6. Verify that the attachment Asset and StorageKey are deleted from DB
await expect(prisma.asset.findUnique({ where: { id: attachmentAsset.id } })).resolves.toBeNull()
await expect(prisma.storageKey.findUnique({ where: { id: storageKey.id } })).resolves.toBeNull()
// 7. Verify S3 deletion was called
const { s3Service } = await import('@shumai/core/src/s3/s3')
expect(s3Service.deleteObject).toHaveBeenCalledWith(
'shumai',
'attachments/test-attachment-file.png',
)
})
it('can create and retrieve comments with video timestamp', async () => {
const { user, assets } = await setupBasicAssets()
+86
View File
@@ -18,6 +18,7 @@ import {
AssetUserInfo,
} from '@shumai/dtos'
import { type Asset, AssetStatus, AssetType, Prisma, type StorageKey } from '@shumai/db'
import { HTTPException } from 'hono/http-exception'
import { logger } from '@shumai/core/src/logger'
import { PaginatedData, paginateQuery, PaginationParams } from '@shumai/core/src/pagination'
import { s3Service } from '@shumai/core/src/s3/s3'
@@ -1413,6 +1414,91 @@ export class AssetService {
return this.getComment(commentId)
}
async deleteComment({ commentId, userId }: { commentId: string; userId: string }): Promise<void> {
const comment = await this.prismaClient.assetComment.findUnique({
where: { id: commentId },
include: {
attachments: {
include: {
asset: {
include: {
storageKey: true,
},
},
},
},
asset: {
select: {
project: { select: { id: true, teamId: true } },
},
},
},
})
if (!comment) {
throw new HTTPException(404, { message: 'Comment not found' })
}
const teamId = comment.asset.project?.teamId
if (!teamId) {
throw new HTTPException(400, {
message: 'Data corruption: Comment asset is not associated with a project',
})
}
const member = await this.prismaClient.teamMember.findUnique({
where: {
teamIdUserId: {
teamId,
userId,
},
},
})
if (!member) {
throw new HTTPException(403, { message: 'User is not a member of the team' })
}
const isOwner = member.role === 'owner'
const isCreator = comment.creatorId === userId
if (!isOwner && !isCreator) {
throw new HTTPException(403, { message: 'You do not have permission to delete this comment' })
}
// Clean up attachment files from S3/storage and delete their DB records
const bucket = process.env.S3_BUCKET || 'shumai'
for (const att of comment.attachments) {
const asset = att.asset
if (asset.storageKey) {
try {
await s3Service.deleteObject(bucket, asset.storageKey.key)
} catch (err) {
logger.error({ err, assetId: asset.id }, 'Failed to delete attachment file from S3')
}
}
await this.prismaClient.asset.delete({
where: { id: asset.id },
})
if (asset.storageKey) {
await this.prismaClient.storageKey
.delete({
where: { id: asset.storageKey.id },
})
.catch(() => {
// Silently ignore if already deleted
})
}
}
await this.prismaClient.assetComment.delete({
where: { id: commentId },
})
}
async listComments(
assetId: string,
params: PaginationParams,
+645
View File
@@ -0,0 +1,645 @@
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'
describe('ChatService', () => {
setupTestDbHooks()
async function setupBasicData() {
const user = await prisma.user.create({
data: { name: 'Test User', email: `test-${Date.now()}@example.com`, password: 'pw' },
})
const team = await prisma.team.create({
data: {
name: 'Test Team',
settings: { transcode: { videoStrategy: 'best_match' }, chatbotAgentId: 'test-agent-id' },
sandbox: { create: {} },
},
})
const teamMember = await prisma.teamMember.create({
data: {
teamId: team.id,
userId: user.id,
role: 'owner',
},
})
const project = await prisma.project.create({
data: {
name: 'Test Project',
teamId: team.id,
},
})
const rootFolder = await prisma.asset.create({
data: {
name: 'root',
type: 'root',
status: 'processed',
projectId: project.id,
},
})
await prisma.project.update({
where: { id: project.id },
data: { rootFolderId: rootFolder.id },
})
const agentUser = await prisma.user.create({
data: {
id: 'test-agent-id',
name: 'Test Agent',
email: 'test-agent@shumai.ai',
type: 'agent',
},
})
const agent = await prisma.agent.create({
data: {
id: 'test-agent-id',
teamId: team.id,
type: 'chat',
config: {
provider: 'openai',
model: 'gpt-4',
},
},
})
return { user, team, teamMember, project, rootFolder, agent, agentUser }
}
it('should start a new chat session and trigger workflow', async () => {
const { user, team, project, rootFolder } = await setupBasicData()
const { sessionId, taskId } = await chatService.startOrContinueChat(user, team.id, {
agentId: 'test-agent-id',
textPrompt: 'hello world',
projectId: project.id,
})
expect(sessionId).toBeDefined()
expect(taskId).toBeDefined()
// Verify AgentSession is created
const session = await prisma.agentSession.findUnique({
where: { id: sessionId },
})
expect(session).toBeDefined()
expect(session?.userId).toBe(user.id)
expect(session?.assetId).toBe(rootFolder.id)
// Verify no comment is created
const comment = await prisma.assetComment.findFirst({
where: { sessionId },
})
expect(comment).toBeNull()
// Verify no session entry is created yet (it is created automatically when the workflow runs)
const entry = await prisma.agentSessionEntry.findFirst({
where: { sessionId },
})
expect(entry).toBeNull()
// Verify WorkflowTask is created with correct payload
const task = await prisma.workflowTask.findUnique({
where: { id: taskId },
})
expect(task).toBeDefined()
expect(task?.type).toBe('chat')
expect(['pending', 'processing']).toContain(task?.status)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const taskPayload = task?.payload as any
expect(taskPayload?.agent?.prompt).toBe('hello world')
})
it('should continue an existing chat session', async () => {
const { user, team, project } = await setupBasicData()
// Start
const { sessionId } = await chatService.startOrContinueChat(user, team.id, {
agentId: 'test-agent-id',
textPrompt: 'first message',
projectId: project.id,
})
// Continue
const { sessionId: nextSessionId, taskId: nextTaskId } = await chatService.startOrContinueChat(
user,
team.id,
{
agentId: 'test-agent-id',
textPrompt: 'second message',
sessionId,
},
)
expect(nextSessionId).toBe(sessionId)
expect(nextTaskId).toBeDefined()
// Verify no entries are created yet (they are created automatically when the workflow runs)
const entries = await prisma.agentSessionEntry.findMany({
where: { sessionId },
})
expect(entries).toHaveLength(0)
// Verify WorkflowTask is created with correct payload
const task = await prisma.workflowTask.findUnique({
where: { id: nextTaskId },
})
expect(task).toBeDefined()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const taskPayload = task?.payload as any
expect(taskPayload?.agent?.prompt).toBe('second message')
})
it('should inject context of referenced assets and attached files', async () => {
const { user, team, project, rootFolder } = await setupBasicData()
const childFolder = await prisma.asset.create({
data: {
name: 'child_folder',
type: 'folder',
status: 'processed',
projectId: project.id,
parentId: rootFolder.id,
},
})
const storageKey = await prisma.storageKey.create({
data: {
key: 'doc_s3_key',
},
})
const textFile = await prisma.asset.create({
data: {
name: 'doc.jpg',
type: 'file',
mediaType: 'image/jpeg',
status: 'processed',
projectId: project.id,
parentId: childFolder.id,
storageKeyId: storageKey.id,
},
})
const { taskId } = await chatService.startOrContinueChat(user, team.id, {
agentId: 'test-agent-id',
textPrompt: 'process file',
projectId: project.id,
assetIds: [childFolder.id],
attachedFiles: [textFile.id],
})
const task = await prisma.workflowTask.findUnique({
where: { id: taskId },
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const taskPayload = task?.payload as any
expect(taskPayload?.agent?.prompt).toBe('process file')
expect(taskPayload?.agent?.attachedFiles).toContain(textFile.id)
expect(taskPayload?.agent?.assetIds).toContain(childFolder.id)
expect(taskPayload?.agent?.imageUrls).toContain('doc_s3_key')
})
it('should list sessions of a user and only return chat type sessions', async () => {
const { user, team, project } = await setupBasicData()
await chatService.startOrContinueChat(user, team.id, {
agentId: 'test-agent-id',
textPrompt: 'chat 1',
projectId: project.id,
})
await chatService.startOrContinueChat(user, team.id, {
agentId: 'test-agent-id',
textPrompt: 'chat 2',
projectId: project.id,
})
// Create a comment type session (which should be ignored by listSessions)
const defaultAgent = await prisma.agent.findFirst()
await prisma.agentSession.create({
data: {
agentId: defaultAgent ? defaultAgent.id : 'default',
userId: user.id,
cwd: process.cwd(),
type: 'comment',
},
})
const list = await chatService.listSessions(user.id, team.id, { first: 10 })
expect(list.data).toHaveLength(2)
})
it('should list messages mapped correctly', 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,
entry: {
type: 'message',
id: 'test-entry-id',
parentId: null,
timestamp: new Date().toISOString(),
message: {
role: 'user',
content: [{ type: 'text', text: 'hello' }],
timestamp: Date.now(),
},
},
},
})
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,
})
// 1. Manually insert a test entry representing user message
await prisma.agentSessionEntry.create({
data: {
id: 'test-entry-1-user',
sessionId,
entry: {
type: 'message',
id: 'test-entry-1-user',
parentId: null,
timestamp: new Date().toISOString(),
message: {
role: 'user',
content: [{ type: 'text', text: 'hello' }],
timestamp: Date.now(),
},
} as unknown as PrismaJson.PiSessionEntry,
},
})
// 2. Manually insert a test entry representing a custom context message
await prisma.agentSessionEntry.create({
data: {
id: 'test-entry-2-context',
sessionId,
entry: {
type: 'custom_message',
id: 'test-entry-2-context',
parentId: 'test-entry-1-user',
timestamp: new Date().toISOString(),
customType: 'context',
content: 'some context',
display: 'chat',
} as unknown as PrismaJson.PiSessionEntry,
},
})
// 3. Manually insert a test entry representing a custom thinking level change
await prisma.agentSessionEntry.create({
data: {
id: 'test-entry-3-thinking',
sessionId,
entry: {
type: 'thinking_level_change',
id: 'test-entry-3-thinking',
parentId: 'test-entry-2-context',
timestamp: new Date().toISOString(),
thinkingLevel: 'deep',
} as unknown as PrismaJson.PiSessionEntry,
},
})
// 4. Manually insert a test entry representing custom context_display_info entry
await prisma.agentSessionEntry.create({
data: {
id: 'test-entry-4-display',
sessionId,
entry: {
type: 'custom',
id: 'test-entry-4-display',
parentId: 'test-entry-3-thinking',
timestamp: new Date().toISOString(),
customType: 'context_display_info',
data: {
assets: [{ id: 'a1', name: 'File A', type: 'file' }],
},
} as unknown as PrismaJson.PiSessionEntry,
},
})
// 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()
})
it('should delete session and cascade', async () => {
const { user, team, project } = await setupBasicData()
const { sessionId } = await chatService.startOrContinueChat(user, team.id, {
agentId: 'test-agent-id',
textPrompt: 'to delete',
projectId: project.id,
})
await chatService.deleteSession(user.id, team.id, sessionId)
const session = await prisma.agentSession.findUnique({
where: { id: sessionId },
})
expect(session).toBeNull()
const entries = await prisma.agentSessionEntry.findMany({
where: { sessionId },
})
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')
})
describe('getNewSessionMessages', () => {
it('returns new messages and lastEntryId', async () => {
const { user, team, project } = await setupBasicData()
const { sessionId } = await chatService.startOrContinueChat(user, team.id, {
agentId: 'test-agent-id',
textPrompt: 'hello',
projectId: project.id,
})
// Insert two entries
const entry1 = await prisma.agentSessionEntry.create({
data: {
id: 'entry-id-1',
sessionId,
entry: {
type: 'message',
id: 'entry-id-1',
parentId: null,
timestamp: new Date().toISOString(),
message: {
role: 'user',
content: [{ type: 'text', text: 'msg 1' }],
},
} as unknown as PrismaJson.PiSessionEntry,
},
})
const entry2 = await prisma.agentSessionEntry.create({
data: {
id: 'entry-id-2',
sessionId,
entry: {
type: 'message',
id: 'entry-id-2',
parentId: 'entry-id-1',
timestamp: new Date().toISOString(),
message: {
role: 'assistant',
content: [{ type: 'text', text: 'msg 2' }],
},
} as unknown as PrismaJson.PiSessionEntry,
},
})
// 1. Get all messages
const res1 = await chatService.getNewSessionMessages(sessionId)
expect(res1.messages).toHaveLength(2)
expect(res1.lastEntryId).toBe(entry2.id)
expect(res1.messages[0].id).toBe(entry1.id)
// 2. Get with lastEntryId
const res2 = await chatService.getNewSessionMessages(sessionId, entry1.id)
expect(res2.messages).toHaveLength(1)
expect(res2.lastEntryId).toBe(entry2.id)
expect(res2.messages[0].id).toBe(entry2.id)
// 3. Get with latest lastEntryId
const res3 = await chatService.getNewSessionMessages(sessionId, entry2.id)
expect(res3.messages).toHaveLength(0)
expect(res3.lastEntryId).toBe(entry2.id)
})
})
describe('getChatWorkflowStatus', () => {
it('returns task status and output', async () => {
const { team, project } = await setupBasicData()
const rootFolder = await prisma.asset.findFirst({
where: { projectId: project.id, type: 'root' },
})
const task = await prisma.workflowTask.create({
data: {
assetId: rootFolder!.id,
type: 'chat',
status: 'completed',
teamId: team.id,
projectId: project.id,
payload: { projectId: project.id },
output: { success: true },
},
})
const statusRes = await chatService.getChatWorkflowStatus(task.id)
expect(statusRes).not.toBeNull()
expect(statusRes?.status).toBe('completed')
expect(statusRes?.output).toEqual({ success: true })
})
it('returns null if task not found', async () => {
const statusRes = await chatService.getChatWorkflowStatus('non-existent-task')
expect(statusRes).toBeNull()
})
})
describe('abortSession', () => {
it('cancels active task and marks failed/aborted', async () => {
const { user, team, project } = await setupBasicData()
const { sessionId, taskId } = await chatService.startOrContinueChat(user, team.id, {
agentId: 'test-agent-id',
textPrompt: 'long execution',
projectId: project.id,
})
// Mark the task as processing
await prisma.workflowTask.update({
where: { id: taskId },
data: { status: 'processing' },
})
const cancelSpy = vi.spyOn(workflowService, 'cancel').mockResolvedValue(undefined)
await chatService.abortSession(user.id, sessionId)
expect(cancelSpy).toHaveBeenCalledWith(taskId)
const updatedTask = await prisma.workflowTask.findUnique({
where: { id: taskId },
})
expect(updatedTask?.status).toBe('failed')
expect(updatedTask?.output).toEqual({ error: 'aborted' })
cancelSpy.mockRestore()
})
it('throws error if session not found', async () => {
await expect(chatService.abortSession('user-1', 'non-existent-session')).rejects.toThrow(
'Session not found',
)
})
it('throws error if unauthorized', async () => {
const { team, project } = await setupBasicData()
const otherUser = await prisma.user.create({
data: { name: 'Other User', email: `other-${Date.now()}@example.com`, password: 'pw' },
})
const { sessionId } = await chatService.startOrContinueChat(otherUser, team.id, {
agentId: 'test-agent-id',
textPrompt: 'hello',
projectId: project.id,
})
await expect(chatService.abortSession('different-user-id', sessionId)).rejects.toThrow(
'Unauthorized session access',
)
})
it('throws error if no active task', async () => {
const { user, team, project } = await setupBasicData()
const { sessionId, taskId } = await chatService.startOrContinueChat(user, team.id, {
agentId: 'test-agent-id',
textPrompt: 'hello',
projectId: project.id,
})
// Complete the task
await prisma.workflowTask.update({
where: { id: taskId },
data: { status: 'completed' },
})
await expect(chatService.abortSession(user.id, sessionId)).rejects.toThrow(
'No active execution found for this session',
)
})
})
})
+479
View File
@@ -0,0 +1,479 @@
import { prisma } from '@shumai/db'
import type { Prisma } from '@shumai/db'
import { authzService, Permission, 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 { workflowService } from '@shumai/workflow-core'
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') {
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)
}
}
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 entryObj = entryRecord.entry as unknown as SessionTreeEntry
if (
entryObj.type === 'custom_message' &&
(entryObj as { customType?: string }).customType === 'context'
) {
return null
}
const pathEntries = [{ ...entryObj, id: entryRecord.id }]
const messages = buildSessionMessages(pathEntries)
if (messages.length > 0) {
return messages[0]
}
return {
id: entryRecord.id,
role: 'user',
content: '',
} as unknown as ChatMessage
}
export class ChatService {
constructor(private readonly prismaClient: typeof prisma = prisma) {}
private async getAssetPath(assetId: string): Promise<string> {
const parts: string[] = []
let currentId: string | null = assetId
while (currentId) {
const dbAssetNode = (await prisma.asset.findUnique({
where: { id: currentId },
select: { id: true, name: true, parentId: true, type: true },
})) as { id: string; name: string; parentId: string | null; type: unknown } | null
if (!dbAssetNode) break
if (dbAssetNode.type === 'root' || dbAssetNode.type === 'share_root') {
break
}
parts.unshift(dbAssetNode.name)
currentId = dbAssetNode.parentId
}
return parts.join('/')
}
async startOrContinueChat(
user: User,
teamId: string,
req: ChatRequest,
): Promise<{ sessionId: string; taskId: string }> {
const {
agentId,
textPrompt,
attachedFiles = [],
assetIds = [],
sessionId: passedSessionId,
contextAssetId,
projectId: passedProjectId,
} = req
// 1. Permissions verification
if (contextAssetId) {
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Asset,
id: contextAssetId,
})
}
for (const fileId of attachedFiles) {
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Asset,
id: fileId,
})
}
for (const assetId of assetIds) {
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Asset,
id: assetId,
})
}
// 2. Project / Asset scoping resolution
let targetAssetId = ''
if (passedSessionId) {
const session = await this.prismaClient.agentSession.findUnique({
where: { id: passedSessionId },
include: { asset: { include: { project: true } } },
})
if (!session) throw new Error('Session not found')
if (session.userId !== user.id) throw new Error('Unauthorized session access')
if (!session.assetId) throw new Error('Session has no associated asset')
targetAssetId = session.assetId
} else {
// Start a new session
if (contextAssetId) {
const asset = await this.prismaClient.asset.findUnique({
where: { id: contextAssetId },
include: { project: true },
})
if (!asset) throw new Error('Context asset not found')
targetAssetId = contextAssetId
} else if (passedProjectId) {
const project = await this.prismaClient.project.findUnique({
where: { id: passedProjectId },
})
if (!project || !project.rootFolderId) throw new Error('Project or root folder not found')
targetAssetId = project.rootFolderId
} else {
// Find default project from user's first team
const teamMember = await this.prismaClient.teamMember.findFirst({
where: { userId: user.id },
include: { team: { include: { projects: { include: { rootFolder: true } } } } },
})
const project = teamMember?.team?.projects?.[0]
if (!project || !project.rootFolderId) {
throw new Error('No active project found for the user. Please specify a projectId.')
}
targetAssetId = project.rootFolderId
}
}
const finalPrompt = textPrompt || ''
const resolvedImageUrls: string[] = []
for (const fileId of attachedFiles) {
const file = await this.prismaClient.asset.findUnique({
where: { id: fileId },
include: { storageKey: true },
})
if (file && file.mediaType?.startsWith('image/') && file.storageKey?.key) {
resolvedImageUrls.push(file.storageKey.key)
}
}
// 4. Create database records in a transaction
return await this.prismaClient.$transaction(async (tx) => {
const asset = await tx.asset.findUnique({
where: { id: targetAssetId },
include: { project: { include: { team: true } } },
})
if (!asset || !asset.project) {
throw new Error('Resolved asset or project not found')
}
const teamId = asset.project.teamId
const projectId = asset.project.id
let activeSessionId = passedSessionId
// Ensure AI agent configuration exists
const agent = await tx.agent.findUnique({ where: { id: agentId } })
if (!agent) {
throw new Error(`Agent with ID "${agentId}" not found`)
}
if (agent.teamId !== teamId) {
throw new Error(`Agent does not belong to the specified team`)
}
if (activeSessionId) {
const sessionExists = await tx.agentSession.findUnique({
where: { id: activeSessionId },
select: { id: true, userId: true },
})
if (!sessionExists) throw new Error('Session not found')
if (sessionExists.userId !== user.id) throw new Error('Unauthorized session access')
// Update the session's agentId to match the selected agent
await tx.agentSession.update({
where: { id: activeSessionId },
data: { agentId },
})
} else {
const newSession = await tx.agentSession.create({
data: {
agentId,
userId: user.id,
cwd: process.cwd(),
assetId: asset.id,
type: 'chat',
},
})
activeSessionId = newSession.id
}
// Trigger chat workflow
const task = await tx.workflowTask.create({
data: {
assetId: asset.id,
type: 'chat',
status: 'pending',
teamId,
projectId,
sessionId: activeSessionId,
payload: {
projectId,
agent: {
prompt: finalPrompt,
imageUrls: resolvedImageUrls,
attachedFiles,
assetIds,
explicitMention: true,
agentId,
sessionId: activeSessionId,
userId: user.id,
isNewChat: !passedSessionId,
},
},
},
})
return {
sessionId: activeSessionId!,
taskId: task.id,
}
})
}
async listSessions(
userId: string,
teamId: string,
params: { first?: number; after?: string },
): Promise<PaginatedData<ChatSessionInfo[]>> {
const where: Prisma.AgentSessionWhereInput = {
userId,
type: 'chat',
agent: {
teamId,
},
}
return await paginateQuery(
async (skip, take) => {
const sessions = await this.prismaClient.agentSession.findMany({
where,
skip,
take,
orderBy: { id: 'desc' },
})
return sessions.map((s) => ({
id: s.id,
agentId: s.agentId,
userId: s.userId,
assetId: s.assetId,
userCommentId: s.userCommentId,
name: s.name,
createdAt: s.createdAt.toISOString(),
updatedAt: s.updatedAt.toISOString(),
}))
},
async () => this.prismaClient.agentSession.count({ where }),
params,
)
}
async listMessages(userId: string, teamId: string, sessionId: string): Promise<ChatMessage[]> {
const session = await this.prismaClient.agentSession.findUnique({
where: { id: sessionId },
})
if (!session) {
throw new Error('Session not found')
}
if (session.userId !== userId) {
throw new Error('Unauthorized session access')
}
const entries = await this.prismaClient.agentSessionEntry.findMany({
where: { sessionId },
orderBy: { id: 'asc' },
})
const pathEntries = entries.map((e) => {
const entryObj = e.entry as unknown as SessionTreeEntry
return {
...entryObj,
id: e.id,
}
})
return buildSessionMessages(pathEntries)
}
async deleteSession(userId: string, teamId: string, sessionId: string): Promise<void> {
const session = await this.prismaClient.agentSession.findUnique({
where: { id: sessionId },
})
if (!session) {
throw new Error('Session not found')
}
if (session.userId !== userId) {
throw new Error('Unauthorized session access')
}
await this.prismaClient.agentSession.delete({
where: { id: sessionId },
})
}
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)
}
}
return {
messages,
lastEntryId: entries[entries.length - 1].id,
}
}
async getChatWorkflowStatus(
taskId: string,
): Promise<{ status: string | null; output: Record<string, unknown> | null } | null> {
const task = await this.prismaClient.workflowTask.findUnique({
where: { id: taskId },
select: { status: true, output: true },
})
if (!task) return null
return {
status: task.status,
output: task.output as Record<string, unknown> | null,
}
}
async abortSession(userId: string, sessionId: string): Promise<void> {
const session = await this.prismaClient.agentSession.findUnique({
where: { id: sessionId },
})
if (!session) {
throw new Error('Session not found')
}
if (session.userId !== userId) {
throw new Error('Unauthorized session access')
}
const taskToAbort = await this.prismaClient.workflowTask.findFirst({
where: {
status: { in: ['pending', 'processing'] },
type: 'chat',
sessionId,
},
})
if (!taskToAbort) {
throw new Error('No active execution found for this session')
}
await workflowService.cancel(taskToAbort.id)
await this.prismaClient.workflowTask.update({
where: { id: taskToAbort.id },
data: {
status: 'failed',
output: { error: 'aborted' },
},
})
}
}
export const chatService = new ChatService()
+1
View File
@@ -20,3 +20,4 @@ export * from './user/avatar'
export * from './versionStack/versionStack'
export * from './hyrumtoken'
export * from './pagination'
export * from './chat/chat'
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/db",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"main": "./src/index.ts",
@@ -0,0 +1,8 @@
-- CreateEnum
CREATE TYPE "AgentSessionType" AS ENUM ('comment', 'chat');
-- AlterTable
ALTER TABLE "agent_sessions" ADD COLUMN "type" "AgentSessionType" NOT NULL DEFAULT 'comment';
-- CreateIndex
CREATE INDEX "agent_sessions_type_idx" ON "agent_sessions"("type");
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "agent_sessions" ADD COLUMN "name" TEXT;
@@ -0,0 +1,2 @@
-- AlterEnum
ALTER TYPE "AgentSessionType" ADD VALUE 'naming';
@@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "workflow_tasks" ADD COLUMN "session_id" TEXT;
-- CreateIndex
CREATE INDEX "workflow_tasks_session_id_idx" ON "workflow_tasks"("session_id");
@@ -0,0 +1,4 @@
-- Backfill session_id from existing task payloads
UPDATE "workflow_tasks"
SET "session_id" = (payload->'agent'->>'sessionId')
WHERE "session_id" IS NULL AND (payload->'agent'->>'sessionId') IS NOT NULL;
+11
View File
@@ -629,6 +629,7 @@ model WorkflowTask {
inputTokens Int @default(0) @map("input_tokens")
outputTokens Int @default(0) @map("output_tokens")
model String?
sessionId String? @map("session_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@ -638,6 +639,7 @@ model WorkflowTask {
@@index([teamId])
@@index([projectId])
@@index([type])
@@index([sessionId])
}
enum AgentType {
chat
@@ -645,6 +647,12 @@ enum AgentType {
embedding
}
enum AgentSessionType {
comment
chat
naming
}
model Agent {
id String @id
user User @relation(fields: [id], references: [id], onDelete: Cascade)
@@ -693,11 +701,14 @@ model AgentSession {
assetId String? @map("asset_id")
asset Asset? @relation("AssetToAgentSession", fields: [assetId], references: [id], onDelete: Cascade)
userCommentId String? @map("user_comment_id")
type AgentSessionType @default(comment)
name String?
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
entries AgentSessionEntry[]
comments AssetComment[]
@@index([type])
@@map("agent_sessions")
}
+34
View File
@@ -626,6 +626,23 @@ export type JsonWithAggregatesFilterBase<$PrismaModel = never> = {
_max?: Prisma.NestedJsonFilter<$PrismaModel>
}
export type EnumAgentSessionTypeFilter<$PrismaModel = never> = {
equals?: $Enums.AgentSessionType | Prisma.EnumAgentSessionTypeFieldRefInput<$PrismaModel>
in?: $Enums.AgentSessionType[] | Prisma.ListEnumAgentSessionTypeFieldRefInput<$PrismaModel>
notIn?: $Enums.AgentSessionType[] | Prisma.ListEnumAgentSessionTypeFieldRefInput<$PrismaModel>
not?: Prisma.NestedEnumAgentSessionTypeFilter<$PrismaModel> | $Enums.AgentSessionType
}
export type EnumAgentSessionTypeWithAggregatesFilter<$PrismaModel = never> = {
equals?: $Enums.AgentSessionType | Prisma.EnumAgentSessionTypeFieldRefInput<$PrismaModel>
in?: $Enums.AgentSessionType[] | Prisma.ListEnumAgentSessionTypeFieldRefInput<$PrismaModel>
notIn?: $Enums.AgentSessionType[] | Prisma.ListEnumAgentSessionTypeFieldRefInput<$PrismaModel>
not?: Prisma.NestedEnumAgentSessionTypeWithAggregatesFilter<$PrismaModel> | $Enums.AgentSessionType
_count?: Prisma.NestedIntFilter<$PrismaModel>
_min?: Prisma.NestedEnumAgentSessionTypeFilter<$PrismaModel>
_max?: Prisma.NestedEnumAgentSessionTypeFilter<$PrismaModel>
}
export type NestedStringFilter<$PrismaModel = never> = {
equals?: string | Prisma.StringFieldRefInput<$PrismaModel>
in?: string[] | Prisma.ListStringFieldRefInput<$PrismaModel>
@@ -1184,4 +1201,21 @@ export type NestedJsonFilterBase<$PrismaModel = never> = {
not?: runtime.InputJsonValue | Prisma.JsonFieldRefInput<$PrismaModel> | Prisma.JsonNullValueFilter
}
export type NestedEnumAgentSessionTypeFilter<$PrismaModel = never> = {
equals?: $Enums.AgentSessionType | Prisma.EnumAgentSessionTypeFieldRefInput<$PrismaModel>
in?: $Enums.AgentSessionType[] | Prisma.ListEnumAgentSessionTypeFieldRefInput<$PrismaModel>
notIn?: $Enums.AgentSessionType[] | Prisma.ListEnumAgentSessionTypeFieldRefInput<$PrismaModel>
not?: Prisma.NestedEnumAgentSessionTypeFilter<$PrismaModel> | $Enums.AgentSessionType
}
export type NestedEnumAgentSessionTypeWithAggregatesFilter<$PrismaModel = never> = {
equals?: $Enums.AgentSessionType | Prisma.EnumAgentSessionTypeFieldRefInput<$PrismaModel>
in?: $Enums.AgentSessionType[] | Prisma.ListEnumAgentSessionTypeFieldRefInput<$PrismaModel>
notIn?: $Enums.AgentSessionType[] | Prisma.ListEnumAgentSessionTypeFieldRefInput<$PrismaModel>
not?: Prisma.NestedEnumAgentSessionTypeWithAggregatesFilter<$PrismaModel> | $Enums.AgentSessionType
_count?: Prisma.NestedIntFilter<$PrismaModel>
_min?: Prisma.NestedEnumAgentSessionTypeFilter<$PrismaModel>
_max?: Prisma.NestedEnumAgentSessionTypeFilter<$PrismaModel>
}
+9
View File
@@ -153,3 +153,12 @@ export const AgentType = {
} as const
export type AgentType = (typeof AgentType)[keyof typeof AgentType]
export const AgentSessionType = {
comment: 'comment',
chat: 'chat',
naming: 'naming'
} as const
export type AgentSessionType = (typeof AgentSessionType)[keyof typeof AgentSessionType]
File diff suppressed because one or more lines are too long
@@ -3266,6 +3266,7 @@ export const WorkflowTaskScalarFieldEnum = {
inputTokens: 'inputTokens',
outputTokens: 'outputTokens',
model: 'model',
sessionId: 'sessionId',
createdAt: 'createdAt',
updatedAt: 'updatedAt'
} as const
@@ -3308,6 +3309,8 @@ export const AgentSessionScalarFieldEnum = {
leafId: 'leafId',
assetId: 'assetId',
userCommentId: 'userCommentId',
type: 'type',
name: 'name',
createdAt: 'createdAt',
updatedAt: 'updatedAt'
} as const
@@ -3728,6 +3731,20 @@ export type EnumAgentTypeFieldRefInput<$PrismaModel> = FieldRefInputType<$Prisma
export type ListEnumAgentTypeFieldRefInput<$PrismaModel> = FieldRefInputType<$PrismaModel, 'AgentType[]'>
/**
* Reference to a field of type 'AgentSessionType'
*/
export type EnumAgentSessionTypeFieldRefInput<$PrismaModel> = FieldRefInputType<$PrismaModel, 'AgentSessionType'>
/**
* Reference to a field of type 'AgentSessionType[]'
*/
export type ListEnumAgentSessionTypeFieldRefInput<$PrismaModel> = FieldRefInputType<$PrismaModel, 'AgentSessionType[]'>
/**
* Batch Payload for updateMany & deleteMany & createMany
*/
@@ -451,6 +451,7 @@ export const WorkflowTaskScalarFieldEnum = {
inputTokens: 'inputTokens',
outputTokens: 'outputTokens',
model: 'model',
sessionId: 'sessionId',
createdAt: 'createdAt',
updatedAt: 'updatedAt'
} as const
@@ -493,6 +494,8 @@ export const AgentSessionScalarFieldEnum = {
leafId: 'leafId',
assetId: 'assetId',
userCommentId: 'userCommentId',
type: 'type',
name: 'name',
createdAt: 'createdAt',
updatedAt: 'updatedAt'
} as const
+115 -1
View File
@@ -34,6 +34,8 @@ export type AgentSessionMinAggregateOutputType = {
leafId: string | null
assetId: string | null
userCommentId: string | null
type: $Enums.AgentSessionType | null
name: string | null
createdAt: Date | null
updatedAt: Date | null
}
@@ -46,6 +48,8 @@ export type AgentSessionMaxAggregateOutputType = {
leafId: string | null
assetId: string | null
userCommentId: string | null
type: $Enums.AgentSessionType | null
name: string | null
createdAt: Date | null
updatedAt: Date | null
}
@@ -58,6 +62,8 @@ export type AgentSessionCountAggregateOutputType = {
leafId: number
assetId: number
userCommentId: number
type: number
name: number
createdAt: number
updatedAt: number
_all: number
@@ -72,6 +78,8 @@ export type AgentSessionMinAggregateInputType = {
leafId?: true
assetId?: true
userCommentId?: true
type?: true
name?: true
createdAt?: true
updatedAt?: true
}
@@ -84,6 +92,8 @@ export type AgentSessionMaxAggregateInputType = {
leafId?: true
assetId?: true
userCommentId?: true
type?: true
name?: true
createdAt?: true
updatedAt?: true
}
@@ -96,6 +106,8 @@ export type AgentSessionCountAggregateInputType = {
leafId?: true
assetId?: true
userCommentId?: true
type?: true
name?: true
createdAt?: true
updatedAt?: true
_all?: true
@@ -181,6 +193,8 @@ export type AgentSessionGroupByOutputType = {
leafId: string | null
assetId: string | null
userCommentId: string | null
type: $Enums.AgentSessionType
name: string | null
createdAt: Date
updatedAt: Date
_count: AgentSessionCountAggregateOutputType | null
@@ -214,6 +228,8 @@ export type AgentSessionWhereInput = {
leafId?: Prisma.StringNullableFilter<"AgentSession"> | string | null
assetId?: Prisma.StringNullableFilter<"AgentSession"> | string | null
userCommentId?: Prisma.StringNullableFilter<"AgentSession"> | string | null
type?: Prisma.EnumAgentSessionTypeFilter<"AgentSession"> | $Enums.AgentSessionType
name?: Prisma.StringNullableFilter<"AgentSession"> | string | null
createdAt?: Prisma.DateTimeFilter<"AgentSession"> | Date | string
updatedAt?: Prisma.DateTimeFilter<"AgentSession"> | Date | string
agent?: Prisma.XOR<Prisma.AgentScalarRelationFilter, Prisma.AgentWhereInput>
@@ -231,6 +247,8 @@ export type AgentSessionOrderByWithRelationInput = {
leafId?: Prisma.SortOrderInput | Prisma.SortOrder
assetId?: Prisma.SortOrderInput | Prisma.SortOrder
userCommentId?: Prisma.SortOrderInput | Prisma.SortOrder
type?: Prisma.SortOrder
name?: Prisma.SortOrderInput | Prisma.SortOrder
createdAt?: Prisma.SortOrder
updatedAt?: Prisma.SortOrder
agent?: Prisma.AgentOrderByWithRelationInput
@@ -251,6 +269,8 @@ export type AgentSessionWhereUniqueInput = Prisma.AtLeast<{
leafId?: Prisma.StringNullableFilter<"AgentSession"> | string | null
assetId?: Prisma.StringNullableFilter<"AgentSession"> | string | null
userCommentId?: Prisma.StringNullableFilter<"AgentSession"> | string | null
type?: Prisma.EnumAgentSessionTypeFilter<"AgentSession"> | $Enums.AgentSessionType
name?: Prisma.StringNullableFilter<"AgentSession"> | string | null
createdAt?: Prisma.DateTimeFilter<"AgentSession"> | Date | string
updatedAt?: Prisma.DateTimeFilter<"AgentSession"> | Date | string
agent?: Prisma.XOR<Prisma.AgentScalarRelationFilter, Prisma.AgentWhereInput>
@@ -268,6 +288,8 @@ export type AgentSessionOrderByWithAggregationInput = {
leafId?: Prisma.SortOrderInput | Prisma.SortOrder
assetId?: Prisma.SortOrderInput | Prisma.SortOrder
userCommentId?: Prisma.SortOrderInput | Prisma.SortOrder
type?: Prisma.SortOrder
name?: Prisma.SortOrderInput | Prisma.SortOrder
createdAt?: Prisma.SortOrder
updatedAt?: Prisma.SortOrder
_count?: Prisma.AgentSessionCountOrderByAggregateInput
@@ -286,6 +308,8 @@ export type AgentSessionScalarWhereWithAggregatesInput = {
leafId?: Prisma.StringNullableWithAggregatesFilter<"AgentSession"> | string | null
assetId?: Prisma.StringNullableWithAggregatesFilter<"AgentSession"> | string | null
userCommentId?: Prisma.StringNullableWithAggregatesFilter<"AgentSession"> | string | null
type?: Prisma.EnumAgentSessionTypeWithAggregatesFilter<"AgentSession"> | $Enums.AgentSessionType
name?: Prisma.StringNullableWithAggregatesFilter<"AgentSession"> | string | null
createdAt?: Prisma.DateTimeWithAggregatesFilter<"AgentSession"> | Date | string
updatedAt?: Prisma.DateTimeWithAggregatesFilter<"AgentSession"> | Date | string
}
@@ -295,6 +319,8 @@ export type AgentSessionCreateInput = {
cwd: string
leafId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
agent: Prisma.AgentCreateNestedOneWithoutAgentSessionsInput
@@ -312,6 +338,8 @@ export type AgentSessionUncheckedCreateInput = {
leafId?: string | null
assetId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
entries?: Prisma.AgentSessionEntryUncheckedCreateNestedManyWithoutSessionInput
@@ -323,6 +351,8 @@ export type AgentSessionUpdateInput = {
cwd?: Prisma.StringFieldUpdateOperationsInput | string
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
agent?: Prisma.AgentUpdateOneRequiredWithoutAgentSessionsNestedInput
@@ -340,6 +370,8 @@ export type AgentSessionUncheckedUpdateInput = {
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
assetId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
entries?: Prisma.AgentSessionEntryUncheckedUpdateManyWithoutSessionNestedInput
@@ -354,6 +386,8 @@ export type AgentSessionCreateManyInput = {
leafId?: string | null
assetId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
}
@@ -363,6 +397,8 @@ export type AgentSessionUpdateManyMutationInput = {
cwd?: Prisma.StringFieldUpdateOperationsInput | string
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
}
@@ -375,6 +411,8 @@ export type AgentSessionUncheckedUpdateManyInput = {
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
assetId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
}
@@ -402,6 +440,8 @@ export type AgentSessionCountOrderByAggregateInput = {
leafId?: Prisma.SortOrder
assetId?: Prisma.SortOrder
userCommentId?: Prisma.SortOrder
type?: Prisma.SortOrder
name?: Prisma.SortOrder
createdAt?: Prisma.SortOrder
updatedAt?: Prisma.SortOrder
}
@@ -414,6 +454,8 @@ export type AgentSessionMaxOrderByAggregateInput = {
leafId?: Prisma.SortOrder
assetId?: Prisma.SortOrder
userCommentId?: Prisma.SortOrder
type?: Prisma.SortOrder
name?: Prisma.SortOrder
createdAt?: Prisma.SortOrder
updatedAt?: Prisma.SortOrder
}
@@ -426,6 +468,8 @@ export type AgentSessionMinOrderByAggregateInput = {
leafId?: Prisma.SortOrder
assetId?: Prisma.SortOrder
userCommentId?: Prisma.SortOrder
type?: Prisma.SortOrder
name?: Prisma.SortOrder
createdAt?: Prisma.SortOrder
updatedAt?: Prisma.SortOrder
}
@@ -577,6 +621,10 @@ export type AgentSessionUncheckedUpdateManyWithoutAgentNestedInput = {
deleteMany?: Prisma.AgentSessionScalarWhereInput | Prisma.AgentSessionScalarWhereInput[]
}
export type EnumAgentSessionTypeFieldUpdateOperationsInput = {
set?: $Enums.AgentSessionType
}
export type AgentSessionCreateNestedOneWithoutEntriesInput = {
create?: Prisma.XOR<Prisma.AgentSessionCreateWithoutEntriesInput, Prisma.AgentSessionUncheckedCreateWithoutEntriesInput>
connectOrCreate?: Prisma.AgentSessionCreateOrConnectWithoutEntriesInput
@@ -596,6 +644,8 @@ export type AgentSessionCreateWithoutUserInput = {
cwd: string
leafId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
agent: Prisma.AgentCreateNestedOneWithoutAgentSessionsInput
@@ -611,6 +661,8 @@ export type AgentSessionUncheckedCreateWithoutUserInput = {
leafId?: string | null
assetId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
entries?: Prisma.AgentSessionEntryUncheckedCreateNestedManyWithoutSessionInput
@@ -654,6 +706,8 @@ export type AgentSessionScalarWhereInput = {
leafId?: Prisma.StringNullableFilter<"AgentSession"> | string | null
assetId?: Prisma.StringNullableFilter<"AgentSession"> | string | null
userCommentId?: Prisma.StringNullableFilter<"AgentSession"> | string | null
type?: Prisma.EnumAgentSessionTypeFilter<"AgentSession"> | $Enums.AgentSessionType
name?: Prisma.StringNullableFilter<"AgentSession"> | string | null
createdAt?: Prisma.DateTimeFilter<"AgentSession"> | Date | string
updatedAt?: Prisma.DateTimeFilter<"AgentSession"> | Date | string
}
@@ -663,6 +717,8 @@ export type AgentSessionCreateWithoutAssetInput = {
cwd: string
leafId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
agent: Prisma.AgentCreateNestedOneWithoutAgentSessionsInput
@@ -678,6 +734,8 @@ export type AgentSessionUncheckedCreateWithoutAssetInput = {
cwd: string
leafId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
entries?: Prisma.AgentSessionEntryUncheckedCreateNestedManyWithoutSessionInput
@@ -715,6 +773,8 @@ export type AgentSessionCreateWithoutCommentsInput = {
cwd: string
leafId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
agent: Prisma.AgentCreateNestedOneWithoutAgentSessionsInput
@@ -731,6 +791,8 @@ export type AgentSessionUncheckedCreateWithoutCommentsInput = {
leafId?: string | null
assetId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
entries?: Prisma.AgentSessionEntryUncheckedCreateNestedManyWithoutSessionInput
@@ -757,6 +819,8 @@ export type AgentSessionUpdateWithoutCommentsInput = {
cwd?: Prisma.StringFieldUpdateOperationsInput | string
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
agent?: Prisma.AgentUpdateOneRequiredWithoutAgentSessionsNestedInput
@@ -773,6 +837,8 @@ export type AgentSessionUncheckedUpdateWithoutCommentsInput = {
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
assetId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
entries?: Prisma.AgentSessionEntryUncheckedUpdateManyWithoutSessionNestedInput
@@ -783,6 +849,8 @@ export type AgentSessionCreateWithoutAgentInput = {
cwd: string
leafId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
user?: Prisma.UserCreateNestedOneWithoutAgentSessionsInput
@@ -798,6 +866,8 @@ export type AgentSessionUncheckedCreateWithoutAgentInput = {
leafId?: string | null
assetId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
entries?: Prisma.AgentSessionEntryUncheckedCreateNestedManyWithoutSessionInput
@@ -835,6 +905,8 @@ export type AgentSessionCreateWithoutEntriesInput = {
cwd: string
leafId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
agent: Prisma.AgentCreateNestedOneWithoutAgentSessionsInput
@@ -851,6 +923,8 @@ export type AgentSessionUncheckedCreateWithoutEntriesInput = {
leafId?: string | null
assetId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
comments?: Prisma.AssetCommentUncheckedCreateNestedManyWithoutSessionInput
@@ -877,6 +951,8 @@ export type AgentSessionUpdateWithoutEntriesInput = {
cwd?: Prisma.StringFieldUpdateOperationsInput | string
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
agent?: Prisma.AgentUpdateOneRequiredWithoutAgentSessionsNestedInput
@@ -893,6 +969,8 @@ export type AgentSessionUncheckedUpdateWithoutEntriesInput = {
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
assetId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
comments?: Prisma.AssetCommentUncheckedUpdateManyWithoutSessionNestedInput
@@ -905,6 +983,8 @@ export type AgentSessionCreateManyUserInput = {
leafId?: string | null
assetId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
}
@@ -914,6 +994,8 @@ export type AgentSessionUpdateWithoutUserInput = {
cwd?: Prisma.StringFieldUpdateOperationsInput | string
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
agent?: Prisma.AgentUpdateOneRequiredWithoutAgentSessionsNestedInput
@@ -929,6 +1011,8 @@ export type AgentSessionUncheckedUpdateWithoutUserInput = {
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
assetId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
entries?: Prisma.AgentSessionEntryUncheckedUpdateManyWithoutSessionNestedInput
@@ -942,6 +1026,8 @@ export type AgentSessionUncheckedUpdateManyWithoutUserInput = {
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
assetId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
}
@@ -953,6 +1039,8 @@ export type AgentSessionCreateManyAssetInput = {
cwd: string
leafId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
}
@@ -962,6 +1050,8 @@ export type AgentSessionUpdateWithoutAssetInput = {
cwd?: Prisma.StringFieldUpdateOperationsInput | string
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
agent?: Prisma.AgentUpdateOneRequiredWithoutAgentSessionsNestedInput
@@ -977,6 +1067,8 @@ export type AgentSessionUncheckedUpdateWithoutAssetInput = {
cwd?: Prisma.StringFieldUpdateOperationsInput | string
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
entries?: Prisma.AgentSessionEntryUncheckedUpdateManyWithoutSessionNestedInput
@@ -990,6 +1082,8 @@ export type AgentSessionUncheckedUpdateManyWithoutAssetInput = {
cwd?: Prisma.StringFieldUpdateOperationsInput | string
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
}
@@ -1001,6 +1095,8 @@ export type AgentSessionCreateManyAgentInput = {
leafId?: string | null
assetId?: string | null
userCommentId?: string | null
type?: $Enums.AgentSessionType
name?: string | null
createdAt?: Date | string
updatedAt?: Date | string
}
@@ -1010,6 +1106,8 @@ export type AgentSessionUpdateWithoutAgentInput = {
cwd?: Prisma.StringFieldUpdateOperationsInput | string
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
user?: Prisma.UserUpdateOneWithoutAgentSessionsNestedInput
@@ -1025,6 +1123,8 @@ export type AgentSessionUncheckedUpdateWithoutAgentInput = {
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
assetId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
entries?: Prisma.AgentSessionEntryUncheckedUpdateManyWithoutSessionNestedInput
@@ -1038,6 +1138,8 @@ export type AgentSessionUncheckedUpdateManyWithoutAgentInput = {
leafId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
assetId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
userCommentId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
type?: Prisma.EnumAgentSessionTypeFieldUpdateOperationsInput | $Enums.AgentSessionType
name?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
}
@@ -1090,6 +1192,8 @@ export type AgentSessionSelect<ExtArgs extends runtime.Types.Extensions.Internal
leafId?: boolean
assetId?: boolean
userCommentId?: boolean
type?: boolean
name?: boolean
createdAt?: boolean
updatedAt?: boolean
agent?: boolean | Prisma.AgentDefaultArgs<ExtArgs>
@@ -1108,6 +1212,8 @@ export type AgentSessionSelectCreateManyAndReturn<ExtArgs extends runtime.Types.
leafId?: boolean
assetId?: boolean
userCommentId?: boolean
type?: boolean
name?: boolean
createdAt?: boolean
updatedAt?: boolean
agent?: boolean | Prisma.AgentDefaultArgs<ExtArgs>
@@ -1123,6 +1229,8 @@ export type AgentSessionSelectUpdateManyAndReturn<ExtArgs extends runtime.Types.
leafId?: boolean
assetId?: boolean
userCommentId?: boolean
type?: boolean
name?: boolean
createdAt?: boolean
updatedAt?: boolean
agent?: boolean | Prisma.AgentDefaultArgs<ExtArgs>
@@ -1138,11 +1246,13 @@ export type AgentSessionSelectScalar = {
leafId?: boolean
assetId?: boolean
userCommentId?: boolean
type?: boolean
name?: boolean
createdAt?: boolean
updatedAt?: boolean
}
export type AgentSessionOmit<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = runtime.Types.Extensions.GetOmit<"id" | "agentId" | "userId" | "cwd" | "leafId" | "assetId" | "userCommentId" | "createdAt" | "updatedAt", ExtArgs["result"]["agentSession"]>
export type AgentSessionOmit<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = runtime.Types.Extensions.GetOmit<"id" | "agentId" | "userId" | "cwd" | "leafId" | "assetId" | "userCommentId" | "type" | "name" | "createdAt" | "updatedAt", ExtArgs["result"]["agentSession"]>
export type AgentSessionInclude<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = {
agent?: boolean | Prisma.AgentDefaultArgs<ExtArgs>
user?: boolean | Prisma.AgentSession$userArgs<ExtArgs>
@@ -1179,6 +1289,8 @@ export type $AgentSessionPayload<ExtArgs extends runtime.Types.Extensions.Intern
leafId: string | null
assetId: string | null
userCommentId: string | null
type: $Enums.AgentSessionType
name: string | null
createdAt: Date
updatedAt: Date
}, ExtArgs["result"]["agentSession"]>
@@ -1616,6 +1728,8 @@ export interface AgentSessionFieldRefs {
readonly leafId: Prisma.FieldRef<"AgentSession", 'String'>
readonly assetId: Prisma.FieldRef<"AgentSession", 'String'>
readonly userCommentId: Prisma.FieldRef<"AgentSession", 'String'>
readonly type: Prisma.FieldRef<"AgentSession", 'AgentSessionType'>
readonly name: Prisma.FieldRef<"AgentSession", 'String'>
readonly createdAt: Prisma.FieldRef<"AgentSession", 'DateTime'>
readonly updatedAt: Prisma.FieldRef<"AgentSession", 'DateTime'>
}
+29 -1
View File
@@ -50,6 +50,7 @@ export type WorkflowTaskMinAggregateOutputType = {
inputTokens: number | null
outputTokens: number | null
model: string | null
sessionId: string | null
createdAt: Date | null
updatedAt: Date | null
}
@@ -66,6 +67,7 @@ export type WorkflowTaskMaxAggregateOutputType = {
inputTokens: number | null
outputTokens: number | null
model: string | null
sessionId: string | null
createdAt: Date | null
updatedAt: Date | null
}
@@ -84,6 +86,7 @@ export type WorkflowTaskCountAggregateOutputType = {
inputTokens: number
outputTokens: number
model: number
sessionId: number
createdAt: number
updatedAt: number
_all: number
@@ -112,6 +115,7 @@ export type WorkflowTaskMinAggregateInputType = {
inputTokens?: true
outputTokens?: true
model?: true
sessionId?: true
createdAt?: true
updatedAt?: true
}
@@ -128,6 +132,7 @@ export type WorkflowTaskMaxAggregateInputType = {
inputTokens?: true
outputTokens?: true
model?: true
sessionId?: true
createdAt?: true
updatedAt?: true
}
@@ -146,6 +151,7 @@ export type WorkflowTaskCountAggregateInputType = {
inputTokens?: true
outputTokens?: true
model?: true
sessionId?: true
createdAt?: true
updatedAt?: true
_all?: true
@@ -251,6 +257,7 @@ export type WorkflowTaskGroupByOutputType = {
inputTokens: number
outputTokens: number
model: string | null
sessionId: string | null
createdAt: Date
updatedAt: Date
_count: WorkflowTaskCountAggregateOutputType | null
@@ -292,6 +299,7 @@ export type WorkflowTaskWhereInput = {
inputTokens?: Prisma.IntFilter<"WorkflowTask"> | number
outputTokens?: Prisma.IntFilter<"WorkflowTask"> | number
model?: Prisma.StringNullableFilter<"WorkflowTask"> | string | null
sessionId?: Prisma.StringNullableFilter<"WorkflowTask"> | string | null
createdAt?: Prisma.DateTimeFilter<"WorkflowTask"> | Date | string
updatedAt?: Prisma.DateTimeFilter<"WorkflowTask"> | Date | string
}
@@ -310,6 +318,7 @@ export type WorkflowTaskOrderByWithRelationInput = {
inputTokens?: Prisma.SortOrder
outputTokens?: Prisma.SortOrder
model?: Prisma.SortOrderInput | Prisma.SortOrder
sessionId?: Prisma.SortOrderInput | Prisma.SortOrder
createdAt?: Prisma.SortOrder
updatedAt?: Prisma.SortOrder
}
@@ -331,6 +340,7 @@ export type WorkflowTaskWhereUniqueInput = Prisma.AtLeast<{
inputTokens?: Prisma.IntFilter<"WorkflowTask"> | number
outputTokens?: Prisma.IntFilter<"WorkflowTask"> | number
model?: Prisma.StringNullableFilter<"WorkflowTask"> | string | null
sessionId?: Prisma.StringNullableFilter<"WorkflowTask"> | string | null
createdAt?: Prisma.DateTimeFilter<"WorkflowTask"> | Date | string
updatedAt?: Prisma.DateTimeFilter<"WorkflowTask"> | Date | string
}, "id">
@@ -349,6 +359,7 @@ export type WorkflowTaskOrderByWithAggregationInput = {
inputTokens?: Prisma.SortOrder
outputTokens?: Prisma.SortOrder
model?: Prisma.SortOrderInput | Prisma.SortOrder
sessionId?: Prisma.SortOrderInput | Prisma.SortOrder
createdAt?: Prisma.SortOrder
updatedAt?: Prisma.SortOrder
_count?: Prisma.WorkflowTaskCountOrderByAggregateInput
@@ -375,6 +386,7 @@ export type WorkflowTaskScalarWhereWithAggregatesInput = {
inputTokens?: Prisma.IntWithAggregatesFilter<"WorkflowTask"> | number
outputTokens?: Prisma.IntWithAggregatesFilter<"WorkflowTask"> | number
model?: Prisma.StringNullableWithAggregatesFilter<"WorkflowTask"> | string | null
sessionId?: Prisma.StringNullableWithAggregatesFilter<"WorkflowTask"> | string | null
createdAt?: Prisma.DateTimeWithAggregatesFilter<"WorkflowTask"> | Date | string
updatedAt?: Prisma.DateTimeWithAggregatesFilter<"WorkflowTask"> | Date | string
}
@@ -393,6 +405,7 @@ export type WorkflowTaskCreateInput = {
inputTokens?: number
outputTokens?: number
model?: string | null
sessionId?: string | null
createdAt?: Date | string
updatedAt?: Date | string
}
@@ -411,6 +424,7 @@ export type WorkflowTaskUncheckedCreateInput = {
inputTokens?: number
outputTokens?: number
model?: string | null
sessionId?: string | null
createdAt?: Date | string
updatedAt?: Date | string
}
@@ -429,6 +443,7 @@ export type WorkflowTaskUpdateInput = {
inputTokens?: Prisma.IntFieldUpdateOperationsInput | number
outputTokens?: Prisma.IntFieldUpdateOperationsInput | number
model?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
sessionId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
}
@@ -447,6 +462,7 @@ export type WorkflowTaskUncheckedUpdateInput = {
inputTokens?: Prisma.IntFieldUpdateOperationsInput | number
outputTokens?: Prisma.IntFieldUpdateOperationsInput | number
model?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
sessionId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
}
@@ -465,6 +481,7 @@ export type WorkflowTaskCreateManyInput = {
inputTokens?: number
outputTokens?: number
model?: string | null
sessionId?: string | null
createdAt?: Date | string
updatedAt?: Date | string
}
@@ -483,6 +500,7 @@ export type WorkflowTaskUpdateManyMutationInput = {
inputTokens?: Prisma.IntFieldUpdateOperationsInput | number
outputTokens?: Prisma.IntFieldUpdateOperationsInput | number
model?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
sessionId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
}
@@ -501,6 +519,7 @@ export type WorkflowTaskUncheckedUpdateManyInput = {
inputTokens?: Prisma.IntFieldUpdateOperationsInput | number
outputTokens?: Prisma.IntFieldUpdateOperationsInput | number
model?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
sessionId?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
}
@@ -519,6 +538,7 @@ export type WorkflowTaskCountOrderByAggregateInput = {
inputTokens?: Prisma.SortOrder
outputTokens?: Prisma.SortOrder
model?: Prisma.SortOrder
sessionId?: Prisma.SortOrder
createdAt?: Prisma.SortOrder
updatedAt?: Prisma.SortOrder
}
@@ -540,6 +560,7 @@ export type WorkflowTaskMaxOrderByAggregateInput = {
inputTokens?: Prisma.SortOrder
outputTokens?: Prisma.SortOrder
model?: Prisma.SortOrder
sessionId?: Prisma.SortOrder
createdAt?: Prisma.SortOrder
updatedAt?: Prisma.SortOrder
}
@@ -556,6 +577,7 @@ export type WorkflowTaskMinOrderByAggregateInput = {
inputTokens?: Prisma.SortOrder
outputTokens?: Prisma.SortOrder
model?: Prisma.SortOrder
sessionId?: Prisma.SortOrder
createdAt?: Prisma.SortOrder
updatedAt?: Prisma.SortOrder
}
@@ -589,6 +611,7 @@ export type WorkflowTaskSelect<ExtArgs extends runtime.Types.Extensions.Internal
inputTokens?: boolean
outputTokens?: boolean
model?: boolean
sessionId?: boolean
createdAt?: boolean
updatedAt?: boolean
}, ExtArgs["result"]["workflowTask"]>
@@ -607,6 +630,7 @@ export type WorkflowTaskSelectCreateManyAndReturn<ExtArgs extends runtime.Types.
inputTokens?: boolean
outputTokens?: boolean
model?: boolean
sessionId?: boolean
createdAt?: boolean
updatedAt?: boolean
}, ExtArgs["result"]["workflowTask"]>
@@ -625,6 +649,7 @@ export type WorkflowTaskSelectUpdateManyAndReturn<ExtArgs extends runtime.Types.
inputTokens?: boolean
outputTokens?: boolean
model?: boolean
sessionId?: boolean
createdAt?: boolean
updatedAt?: boolean
}, ExtArgs["result"]["workflowTask"]>
@@ -643,11 +668,12 @@ export type WorkflowTaskSelectScalar = {
inputTokens?: boolean
outputTokens?: boolean
model?: boolean
sessionId?: boolean
createdAt?: boolean
updatedAt?: boolean
}
export type WorkflowTaskOmit<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = runtime.Types.Extensions.GetOmit<"id" | "uid" | "teamId" | "projectId" | "assetId" | "type" | "status" | "payload" | "output" | "heartbeat" | "inputTokens" | "outputTokens" | "model" | "createdAt" | "updatedAt", ExtArgs["result"]["workflowTask"]>
export type WorkflowTaskOmit<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = runtime.Types.Extensions.GetOmit<"id" | "uid" | "teamId" | "projectId" | "assetId" | "type" | "status" | "payload" | "output" | "heartbeat" | "inputTokens" | "outputTokens" | "model" | "sessionId" | "createdAt" | "updatedAt", ExtArgs["result"]["workflowTask"]>
export type $WorkflowTaskPayload<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = {
name: "WorkflowTask"
@@ -672,6 +698,7 @@ export type $WorkflowTaskPayload<ExtArgs extends runtime.Types.Extensions.Intern
inputTokens: number
outputTokens: number
model: string | null
sessionId: string | null
createdAt: Date
updatedAt: Date
}, ExtArgs["result"]["workflowTask"]>
@@ -1110,6 +1137,7 @@ export interface WorkflowTaskFieldRefs {
readonly inputTokens: Prisma.FieldRef<"WorkflowTask", 'Int'>
readonly outputTokens: Prisma.FieldRef<"WorkflowTask", 'Int'>
readonly model: Prisma.FieldRef<"WorkflowTask", 'String'>
readonly sessionId: Prisma.FieldRef<"WorkflowTask", 'String'>
readonly createdAt: Prisma.FieldRef<"WorkflowTask", 'DateTime'>
readonly updatedAt: Prisma.FieldRef<"WorkflowTask", 'DateTime'>
}
+6
View File
@@ -30,6 +30,7 @@ declare global {
thinkingLevel?: 'off' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh'
systemPrompt?: string
appendSystemPrompt?: string[]
deniedTools?: string[]
}
// ----------------------------------------------------------------------
@@ -288,6 +289,11 @@ declare global {
sessionId?: string
userCommentId?: string
explicitMention?: boolean
prompt?: string
imageUrls?: string[]
attachedFiles?: string[]
assetIds?: string[]
isNewChat?: boolean
}
// ----------------------------------------------------------------------
+1 -1
View File
@@ -235,7 +235,7 @@ export class PrismaTestingHelper<
})
throw internalRollbackErrorSymbol
},
{ ...opts, timeout: 30000 },
{ timeout: 60000, ...opts },
)
.catch((error) => {
if (error !== internalRollbackErrorSymbol) {
+2 -1
View File
@@ -1,11 +1,12 @@
{
"name": "@shumai/dtos",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"main": "./src/index.ts",
"types": "./src/index.ts",
"dependencies": {
"@earendil-works/pi-agent-core": "0.78.0",
"zod": "4.4.3"
}
}
+2
View File
@@ -30,6 +30,7 @@ export const agentInfoSchema = z.object({
systemPrompt: z.string().optional(),
soul: z.string().optional(),
skills: z.array(agentSkillSchema).optional(),
deniedTools: z.array(z.string()).optional(),
})
export type AgentInfo = z.infer<typeof agentInfoSchema>
@@ -44,6 +45,7 @@ const baseAgentRequest = z.object({
systemPrompt: z.string().optional(),
soul: z.string().optional(),
skills: z.array(z.string()).optional(),
deniedTools: z.array(z.string()).optional(),
})
export const createAgentRequestSchema = baseAgentRequest.superRefine((data, ctx) => {
+49
View File
@@ -0,0 +1,49 @@
import { z } from 'zod'
import type { AgentMessage } from '@earendil-works/pi-agent-core'
export const chatRequestSchema = z
.object({
agentId: z.string(),
textPrompt: z.string().optional(),
attachedFiles: z.array(z.string()).optional(), // Uploaded file asset IDs
assetIds: z.array(z.string()).optional(), // Workspace file/folder IDs referenced
sessionId: z.string().optional(), // ID of existing session (to continue)
contextAssetId: z.string().optional(), // Active page context asset ID
projectId: z.string().optional(), // Target project ID
})
.superRefine((data, ctx) => {
const hasPrompt = data.textPrompt && data.textPrompt.trim().length > 0
const hasFiles = data.attachedFiles && data.attachedFiles.length > 0
const hasAssets = data.assetIds && data.assetIds.length > 0
if (!hasPrompt && !hasFiles && !hasAssets) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'At least one of textPrompt, attachedFiles, or assetIds must be provided.',
path: ['textPrompt'],
})
}
})
export type ChatRequest = z.infer<typeof chatRequestSchema>
export const chatSessionInfoSchema = z.object({
id: z.string(),
agentId: z.string(),
userId: z.string().nullable(),
assetId: z.string().nullable(),
userCommentId: z.string().nullable(),
name: z.string().nullable().optional(),
createdAt: z.string(),
updatedAt: z.string(),
})
export type ChatSessionInfo = z.infer<typeof chatSessionInfoSchema>
export const chatMessageSchema = z.intersection(
z.object({
id: z.string(),
}),
z.unknown(),
) as unknown as z.ZodType<AgentMessage & { id: string }>
export type ChatMessage = AgentMessage & { id: string }
+1
View File
@@ -18,3 +18,4 @@ export * from './team'
export * from './upload'
export * from './user-metadata'
export * from './versionStack'
export * from './chat'
+1 -1
View File
@@ -71,7 +71,7 @@ export const VideoTranscodeStrategy = {
export type VideoTranscodeStrategy =
(typeof VideoTranscodeStrategy)[keyof typeof VideoTranscodeStrategy]
export const updateTeamSettingsRequestSchema = z.discriminatedUnion('key', [
export const updateTeamSettingsRequestSchema = z.union([
z.object({
key: z.literal('transcode.videoStrategy'),
value: z.nativeEnum(VideoTranscodeStrategy),
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 68 B

Binary file not shown.
+16
View File
@@ -0,0 +1,16 @@
{
"name": "@shumai/e2e",
"version": "0.1.10",
"private": true,
"type": "module",
"dependencies": {
"@shumai/db": "workspace:*",
"@shumai/core": "workspace:*",
"@shumai/workflow-core": "workspace:*",
"@shumai/agent": "workspace:*",
"@shumai/transcode": "workspace:*",
"@shumai/dtos": "workspace:*",
"@earendil-works/pi-agent-core": "0.78.0",
"@google/genai": "1.52.0"
}
}
+7
View File
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"ignoreDeprecations": "6.0"
},
"include": ["workflow/**/*"]
}
+21
View File
@@ -0,0 +1,21 @@
import tsconfigPaths from 'vite-tsconfig-paths'
import { defineConfig } from 'vitest/config'
export default defineConfig({
plugins: [tsconfigPaths()],
test: {
globalSetup: './packages/e2e/workflow/test-global-setup.ts',
globals: false,
pool: 'forks',
fileParallelism: false,
sequence: {
concurrent: false,
},
include: ['packages/e2e/workflow/**/*.test.ts'],
server: {
deps: {
inline: ['zod', '@shumai/workflow-core', '@temporalio/activity'],
},
},
},
})
@@ -0,0 +1,334 @@
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { workflowService, TaskQueueAgent, TaskQueueTranscode } from '@shumai/workflow-core'
import { initAgentWorkflows } from '@shumai/agent'
import { initTranscodeWorkflows } from '@shumai/transcode'
import { s3Service } from '@shumai/core/src/s3/s3'
import { AgentHarness, type AgentTool } from '@earendil-works/pi-agent-core'
import { fileURLToPath } from 'url'
import * as path from 'path'
const currentDir = path.dirname(fileURLToPath(import.meta.url))
const agentWorkflowsPath = path.resolve(currentDir, '../../../apps/agent/src/workflows.ts')
const transcodeWorkflowsPath = path.resolve(currentDir, '../../../apps/transcode/src/workflows.ts')
describe.each(['local', 'temporal'] as const)(
'Workflow E2E - agentAutofillMedia (executor: %s)',
(mode) => {
setupTestDbHooks()
let agentWorkerPromise: Promise<void> | null = null
let transcodeWorkerPromise: Promise<void> | null = null
beforeAll(async () => {
// Set bucket environment
process.env.S3_BUCKET = 'shumai-e2e-test-bucket'
// Configure the workflow service executor dynamically
workflowService.setExecutorType(mode)
// Initialize registries
initAgentWorkflows()
initTranscodeWorkflows()
// Spy on the harness prompt method to intercept the LLM call while running real activities & tools
vi.spyOn(AgentHarness.prototype, 'prompt').mockImplementation(async function (
this: AgentHarness,
) {
// Find the real autofill_metadata tool configured on the harness
const tools = this.getTools()
const autofillTool = tools.find((t: AgentTool) => t.name === 'autofill_metadata')
if (autofillTool) {
// Execute the real tool callback to perform the actual DB updates
await autofillTool.execute('call-123', {
title: 'E2E Title Extracted',
confidence: 0.95,
completed: true,
})
}
// Return a successful assistant response mock
/* eslint-disable @typescript-eslint/no-explicit-any -- Mock assistant response structure */
return {
content: [{ type: 'text', text: 'Success' }],
usage: { input: 10, output: 20 },
stopReason: 'stop',
} as any
/* eslint-enable @typescript-eslint/no-explicit-any */
})
if (mode === 'temporal') {
console.log('Starting background workers for Temporal E2E tests...')
agentWorkerPromise = workflowService.startWorkers(TaskQueueAgent, {
workflowsPath: agentWorkflowsPath,
})
transcodeWorkerPromise = workflowService.startWorkers(TaskQueueTranscode, {
workflowsPath: transcodeWorkflowsPath,
})
// Wait a moment for workers to establish connections
await new Promise((resolve) => setTimeout(resolve, 2000))
} else {
console.log('Starting local workflow service polling...')
workflowService.start()
}
})
afterAll(async () => {
if (mode === 'temporal') {
console.log('Shutting down Temporal workers...')
await workflowService.shutdownWorkers()
await Promise.all([agentWorkerPromise, transcodeWorkerPromise].filter(Boolean))
}
workflowService.close()
// Restore spied methods
vi.restoreAllMocks()
// Clean up E2E files in local filesystem storage
try {
console.log('Cleaning up local E2E storage files...')
await s3Service.deletePrefix('shumai-e2e-test-bucket', '')
} catch (err) {
console.error('Failed to clean up E2E storage folder:', err)
}
})
it('should run agentAutofillMedia workflow and update asset metadata successfully', async () => {
// 1. Seed Database Models
const team = await prisma.team.create({
data: { name: 'E2E Test Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Test Project', teamId: team.id },
})
// Create Metadata Field (aiAutofill = true, text)
await prisma.metadataField.create({
data: {
key: 'title',
scope: 'PROJECT',
projectId: project.id,
teamId: team.id,
config: { name: 'Title', type: 'text' },
aiAutofill: true,
description: 'Auto-extracted title',
},
})
// Create Metadata Field (aiAutofill = true, number)
await prisma.metadataField.create({
data: {
key: 'confidence',
scope: 'PROJECT',
projectId: project.id,
teamId: team.id,
config: { name: 'Confidence', type: 'number' },
aiAutofill: true,
description: 'AI extraction confidence',
},
})
// Create Metadata Field (aiAutofill = true, boolean)
await prisma.metadataField.create({
data: {
key: 'completed',
scope: 'PROJECT',
projectId: project.id,
teamId: team.id,
config: { name: 'Completed', type: 'toggle' },
aiAutofill: true,
description: 'Completed flag',
},
})
// Create Metadata Field (aiAutofill = false, text)
await prisma.metadataField.create({
data: {
key: 'manual_notes',
scope: 'PROJECT',
projectId: project.id,
teamId: team.id,
config: { name: 'Manual Notes', type: 'text' },
aiAutofill: false,
description: 'Manual notes (should not be auto-filled)',
},
})
// Create Agent User
const agentUser = await prisma.user.create({
data: {
name: 'E2E Autofill Agent User',
email: 'e2e-autofill-agent@shumai.ai',
type: 'agent',
},
})
// Link Agent User to Team
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: agentUser.id,
role: 'editor',
},
})
// Setup Agent Provider & Model configuration
const provider = await prisma.provider.create({
data: {
name: 'google',
teamId: team.id,
config: { api: 'google-generative-ai', apiKey: 'dummy-google-api-key' },
},
})
const model = await prisma.model.create({
data: {
modelId: 'gemini',
name: 'Gemini',
providerId: provider.id,
config: {
input: ['text', 'image'],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 8192,
maxTokens: 2048,
reasoning: false,
},
},
})
const agent = await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'autofill',
enabled: true,
providerId: provider.id,
modelId: model.id,
config: {
provider: 'google',
model: 'gemini',
},
},
})
// Create StorageKey first
const storageKey = await prisma.storageKey.create({
data: {
key: 'projects/e2e/test-image.png',
},
})
// Create Asset
const asset = await prisma.asset.create({
data: {
name: 'test-image.png',
type: 'file',
status: 'uploaded',
mediaType: 'image/png',
projectId: project.id,
storageKeyId: storageKey.id,
},
})
// 2. Seed S3 Storage (LocalStorageService) with a valid 1x1 base64-encoded PNG image
const base64Png =
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='
const pngBuffer = Buffer.from(base64Png, 'base64')
await s3Service.putObject(
'shumai-e2e-test-bucket',
'projects/e2e/test-image.png',
pngBuffer,
pngBuffer.length,
'image/png',
)
// 3. Create Workflow Task (ai_metadata_autofill, pending)
const task = await prisma.workflowTask.create({
data: {
type: 'ai_metadata_autofill',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
agent: { sessionId: 'mock-session-123', agentId: agent.id },
},
},
})
// 4. Wait for workflow to complete
console.log(`Submitted E2E Workflow Task. ID: ${task.id}. Awaiting completion...`)
const completedTask = await workflowService.executeWait(task, 45000)
// 5. Verification
expect(completedTask.status).toBe('completed')
// Verify placeholder comment is updated correctly
const comments = await prisma.assetComment.findMany({
where: { assetId: asset.id },
orderBy: { createdAt: 'asc' },
})
expect(comments.length).toBeGreaterThan(0)
// The last updated message of placeholder comment should indicate success
const finalComment = comments[comments.length - 1]
expect(finalComment.message).toBe('Autofill completed successfully.')
// Verify AssetMetadataValue contains the AI-extracted title
const titleVal = await prisma.assetMetadataValue.findUnique({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
assetId_fieldKey: {
assetId: asset.id,
fieldKey: 'title',
},
},
})
expect(titleVal).toBeDefined()
expect(titleVal?.stringValue).toBe('E2E Title Extracted')
// Verify AssetMetadataValue contains the AI-extracted confidence
const confidenceVal = await prisma.assetMetadataValue.findUnique({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
assetId_fieldKey: {
assetId: asset.id,
fieldKey: 'confidence',
},
},
})
expect(confidenceVal).toBeDefined()
expect(confidenceVal?.numberValue).toBe(0.95)
// Verify AssetMetadataValue contains the AI-extracted completed flag
const completedVal = await prisma.assetMetadataValue.findUnique({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
assetId_fieldKey: {
assetId: asset.id,
fieldKey: 'completed',
},
},
})
expect(completedVal).toBeDefined()
expect(completedVal?.booleanValue).toBe(true)
// Verify manual_notes is NOT auto-filled (remains null/undefined in DB)
const manualNotesVal = await prisma.assetMetadataValue.findUnique({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
assetId_fieldKey: {
assetId: asset.id,
fieldKey: 'manual_notes',
},
},
})
expect(manualNotesVal).toBeNull()
}, 50000)
},
)
+328
View File
@@ -0,0 +1,328 @@
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { workflowService, TaskQueueAgent } from '@shumai/workflow-core'
import { initAgentWorkflows } from '@shumai/agent'
import { s3Service } from '@shumai/core/src/s3/s3'
import { AgentHarness } from '@earendil-works/pi-agent-core'
import { fileURLToPath } from 'url'
import * as path from 'path'
const currentDir = path.dirname(fileURLToPath(import.meta.url))
const agentWorkflowsPath = path.resolve(currentDir, '../../../apps/agent/src/workflows.ts')
describe.each(['local', 'temporal'] as const)('Workflow E2E - agentChat (executor: %s)', (mode) => {
setupTestDbHooks()
let agentWorkerPromise: Promise<void> | null = null
beforeAll(async () => {
process.env.S3_BUCKET = 'shumai-e2e-test-bucket-chat'
process.env.GEMINI_API_KEY = 'dummy-key'
workflowService.setExecutorType(mode)
initAgentWorkflows()
vi.spyOn(AgentHarness.prototype, 'prompt').mockResolvedValue({
content: [{ type: 'text', text: 'E2E Agent Chat Response Success' }],
usage: { input: 10, output: 20 },
stopReason: 'stop',
} as unknown as Awaited<ReturnType<typeof AgentHarness.prototype.prompt>>)
if (mode === 'temporal') {
console.log('Starting background worker for chat Temporal E2E tests...')
agentWorkerPromise = workflowService.startWorkers(TaskQueueAgent, {
workflowsPath: agentWorkflowsPath,
})
await new Promise((resolve) => setTimeout(resolve, 2000))
} else {
console.log('Starting local workflow service polling...')
workflowService.start()
}
})
afterAll(async () => {
if (mode === 'temporal') {
console.log('Shutting down Temporal workers...')
await workflowService.shutdownWorkers()
await Promise.all([agentWorkerPromise].filter(Boolean))
}
workflowService.close()
vi.restoreAllMocks()
try {
console.log('Cleaning up local E2E storage files...')
await s3Service.deletePrefix('shumai-e2e-test-bucket-chat', '')
} catch (err) {
console.error('Failed to clean up E2E storage folder:', err)
}
})
it('should run agentChat workflow with userCommentId and reply comment successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Chat Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Chat Project', teamId: team.id },
})
const agentUser = await prisma.user.create({
data: {
name: 'E2E Chat Agent User',
email: 'e2e-chat-agent@shumai.ai',
type: 'agent',
},
})
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: agentUser.id,
role: 'editor',
},
})
const provider = await prisma.provider.create({
data: {
name: 'google',
teamId: team.id,
config: { api: 'google-generative-ai', apiKey: 'dummy-google-api-key' },
},
})
const model = await prisma.model.create({
data: {
modelId: 'gemini',
name: 'Gemini',
providerId: provider.id,
config: {
input: ['text', 'image'],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 8192,
maxTokens: 2048,
reasoning: false,
},
},
})
await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'chat',
enabled: true,
providerId: provider.id,
modelId: model.id,
config: {
provider: 'google',
model: 'gemini',
},
},
})
const asset = await prisma.asset.create({
data: {
name: 'chat-asset.png',
type: 'file',
status: 'uploaded',
mediaType: 'image/png',
projectId: project.id,
},
})
// Create a regular user who authors the comment
const regularUser = await prisma.user.create({
data: {
name: 'Regular User',
email: 'user@example.com',
type: 'human',
},
})
// Create AgentSession first
await prisma.agentSession.create({
data: {
id: 'chat-session-123',
agentId: agentUser.id,
userId: regularUser.id,
cwd: process.cwd(),
assetId: asset.id,
},
})
// Create the User Comment
const userComment = await prisma.assetComment.create({
data: {
assetId: asset.id,
message: 'Hello E2E Agent!',
creatorId: regularUser.id,
sessionId: 'chat-session-123',
},
})
// 2. Create Workflow Task
const task = await prisma.workflowTask.create({
data: {
type: 'chat',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
agent: {
agentId: agentUser.id,
sessionId: 'chat-session-123',
userCommentId: userComment.id,
userId: regularUser.id,
},
},
},
})
// 3. Wait for workflow to complete
console.log(
`Submitted E2E Chat Workflow Task with Comment. ID: ${task.id}. Awaiting completion...`,
)
const completedTask = await workflowService.executeWait(task, 45000)
// 4. Verification
expect(completedTask.status).toBe('completed')
// Verify reply comment in DB
const comments = await prisma.assetComment.findMany({
where: {
assetId: asset.id,
replyToId: userComment.id,
},
})
expect(comments.length).toBeGreaterThan(0)
expect(comments[0].message).toBe('E2E Agent Chat Response Success')
expect(comments[0].creatorId).toBe(agentUser.id)
}, 50000)
it('should run agentChat workflow with direct prompt successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Direct Chat Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Direct Chat Project', teamId: team.id },
})
const agentUser = await prisma.user.create({
data: {
name: 'E2E Direct Chat Agent User',
email: 'e2e-dir-chat-agent@shumai.ai',
type: 'agent',
},
})
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: agentUser.id,
role: 'editor',
},
})
const provider = await prisma.provider.create({
data: {
name: 'google',
teamId: team.id,
config: { api: 'google-generative-ai', apiKey: 'dummy-google-api-key' },
},
})
const model = await prisma.model.create({
data: {
modelId: 'gemini',
name: 'Gemini',
providerId: provider.id,
config: {
input: ['text'],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 8192,
maxTokens: 2048,
reasoning: false,
},
},
})
await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'chat',
enabled: true,
providerId: provider.id,
modelId: model.id,
config: {
provider: 'google',
model: 'gemini',
},
},
})
const asset = await prisma.asset.create({
data: {
name: 'direct-chat-asset.png',
type: 'file',
status: 'uploaded',
mediaType: 'image/png',
projectId: project.id,
},
})
const regularUser = await prisma.user.create({
data: {
name: 'Regular User 2',
email: 'user2@example.com',
type: 'human',
},
})
// Create AgentSession first
await prisma.agentSession.create({
data: {
id: 'direct-session-456',
agentId: agentUser.id,
userId: regularUser.id,
cwd: process.cwd(),
assetId: asset.id,
},
})
// 2. Create Workflow Task (without userCommentId, but with prompt/sessionId)
const task = await prisma.workflowTask.create({
data: {
type: 'chat',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
agent: {
agentId: agentUser.id,
sessionId: 'direct-session-456',
prompt: 'Direct test message',
userId: regularUser.id,
},
},
},
})
// 3. Wait for workflow to complete
console.log(`Submitted E2E Direct Chat Workflow Task. ID: ${task.id}. Awaiting completion...`)
const completedTask = await workflowService.executeWait(task, 45000)
// 4. Verification
expect(completedTask.status).toBe('completed')
expect((completedTask.output as unknown as { sessionId: string })?.sessionId).toBe(
'direct-session-456',
)
}, 50000)
})
@@ -0,0 +1,481 @@
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
import { prisma, WorkflowTaskStatus } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { workflowService, TaskQueueAgent, TaskQueueTranscode } from '@shumai/workflow-core'
import { initAgentWorkflows } from '@shumai/agent'
import { initTranscodeWorkflows } from '@shumai/transcode'
import { s3Service } from '@shumai/core/src/s3/s3'
import { fileURLToPath } from 'url'
import * as path from 'path'
import * as fs from 'fs'
const mockEmbedContent = vi.fn().mockResolvedValue({
embeddings: [
{
values: Array(1536).fill(0.2),
},
],
})
vi.mock('@google/genai', () => {
return {
// eslint-disable-next-line @typescript-eslint/naming-convention
GoogleGenAI: class {
models = {
embedContent: mockEmbedContent,
}
},
}
})
const currentDir = path.dirname(fileURLToPath(import.meta.url))
const agentWorkflowsPath = path.resolve(currentDir, '../../../apps/agent/src/workflows.ts')
const transcodeWorkflowsPath = path.resolve(currentDir, '../../../apps/transcode/src/workflows.ts')
const fixturesDir = path.resolve(currentDir, '../fixtures')
describe.each(['local', 'temporal'] as const)(
'Workflow E2E - agentEmbeddingMedia (executor: %s)',
(mode) => {
setupTestDbHooks()
let agentWorkerPromise: Promise<void> | null = null
let transcodeWorkerPromise: Promise<void> | null = null
beforeAll(async () => {
// Set bucket environment
process.env.S3_BUCKET = 'shumai-e2e-test-bucket-embedding'
process.env.GEMINI_API_KEY = 'dummy-key'
// Configure the workflow service executor dynamically
workflowService.setExecutorType(mode)
// Initialize registries
initAgentWorkflows()
initTranscodeWorkflows()
if (mode === 'temporal') {
console.log('Starting background workers for Temporal E2E tests...')
agentWorkerPromise = workflowService.startWorkers(TaskQueueAgent, {
workflowsPath: agentWorkflowsPath,
})
transcodeWorkerPromise = workflowService.startWorkers(TaskQueueTranscode, {
workflowsPath: transcodeWorkflowsPath,
})
// Wait a moment for workers to establish connections
await new Promise((resolve) => setTimeout(resolve, 2000))
} else {
console.log('Starting local workflow service polling...')
workflowService.start()
}
})
afterAll(async () => {
if (mode === 'temporal') {
console.log('Shutting down Temporal workers...')
await workflowService.shutdownWorkers()
await Promise.all([agentWorkerPromise, transcodeWorkerPromise].filter(Boolean))
}
workflowService.close()
// Restore spied methods
vi.restoreAllMocks()
// Clean up E2E files in local filesystem storage
try {
console.log('Cleaning up local E2E storage files...')
await s3Service.deletePrefix('shumai-e2e-test-bucket-embedding', '')
} catch (err) {
console.error('Failed to clean up E2E storage folder:', err)
}
})
it('should run agentEmbeddingMedia workflow for an image asset successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Image Embedding Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Image Embedding Project', teamId: team.id },
})
const agentUser = await prisma.user.create({
data: {
name: 'E2E Image Embedding Agent User',
email: 'e2e-img-emb-agent@shumai.ai',
type: 'agent',
},
})
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: agentUser.id,
role: 'editor',
},
})
const provider = await prisma.provider.create({
data: {
name: 'google',
teamId: team.id,
config: { api: 'google-generative-ai', apiKey: 'dummy-google-api-key' },
},
})
const model = await prisma.model.create({
data: {
modelId: 'gemini',
name: 'Gemini',
providerId: provider.id,
config: {
input: ['text', 'image'],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 8192,
maxTokens: 2048,
reasoning: false,
},
},
})
await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'embedding',
enabled: true,
providerId: provider.id,
modelId: model.id,
config: {
provider: 'google',
model: 'gemini',
},
},
})
const storageKey = await prisma.storageKey.create({
data: {
key: 'projects/e2e/test-image.png',
},
})
const asset = await prisma.asset.create({
data: {
name: 'test-image.png',
type: 'file',
status: 'uploaded',
mediaType: 'image/png',
projectId: project.id,
storageKeyId: storageKey.id,
},
})
// 2. Seed S3 Storage from Fixture
const pngPath = path.join(fixturesDir, 'small.png')
const pngBuffer = fs.readFileSync(pngPath)
await s3Service.putObject(
'shumai-e2e-test-bucket-embedding',
'projects/e2e/test-image.png',
pngBuffer,
pngBuffer.length,
'image/png',
)
// 3. Create Workflow Task (ai_embedding, pending)
const task = await prisma.workflowTask.create({
data: {
type: 'ai_embedding',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
agent: { agentId: agentUser.id },
},
},
})
// 4. Wait for workflow to complete
console.log(
`Submitted E2E Image Embedding Workflow Task. ID: ${task.id}. Awaiting completion...`,
)
const completedTask = await workflowService.executeWait(task, 45000)
// 5. Verification
expect(completedTask.status).toBe('completed')
// Verify AssetComment indicates success
const comments = await prisma.assetComment.findMany({
where: { assetId: asset.id },
orderBy: { createdAt: 'asc' },
})
expect(comments.length).toBeGreaterThan(0)
const finalComment = comments[comments.length - 1]
expect(finalComment.message).toBe('Embedding completed successfully.')
// Verify raw embedding is saved in DB
const dbEmbeddings = (await prisma.$queryRaw`
SELECT id, asset_id as "assetId", start_time as "startTime", end_time as "endTime"
FROM asset_embeddings
WHERE asset_id = ${asset.id}
`) as { id: string; startTime: number | null; endTime: number | null }[]
expect(dbEmbeddings.length).toBe(1)
expect(dbEmbeddings[0].startTime).toBeNull()
expect(dbEmbeddings[0].endTime).toBeNull()
}, 50000)
it('should run agentEmbeddingMedia workflow for a video asset successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Video Embedding Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Video Embedding Project', teamId: team.id },
})
const agentUser = await prisma.user.create({
data: {
name: 'E2E Video Embedding Agent User',
email: 'e2e-vid-emb-agent@shumai.ai',
type: 'agent',
},
})
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: agentUser.id,
role: 'editor',
},
})
const provider = await prisma.provider.create({
data: {
name: 'google',
teamId: team.id,
config: { api: 'google-generative-ai', apiKey: 'dummy-google-api-key' },
},
})
const model = await prisma.model.create({
data: {
modelId: 'gemini',
name: 'Gemini',
providerId: provider.id,
config: {
input: ['text', 'image'],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 8192,
maxTokens: 2048,
reasoning: false,
},
},
})
await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'embedding',
enabled: true,
providerId: provider.id,
modelId: model.id,
config: {
provider: 'google',
model: 'gemini',
},
},
})
const storageKey = await prisma.storageKey.create({
data: {
key: 'projects/e2e/test-video.mp4',
},
})
const asset = await prisma.asset.create({
data: {
name: 'test-video.mp4',
type: 'file',
status: 'uploaded',
mediaType: 'video/mp4',
media: { duration: 1.0 } as unknown as PrismaJson.MediaInfo, // 1-second video
projectId: project.id,
storageKeyId: storageKey.id,
},
})
// 2. Seed S3 Storage from Fixture
const mp4Path = path.join(fixturesDir, 'small.mp4')
const mp4Buffer = fs.readFileSync(mp4Path)
await s3Service.putObject(
'shumai-e2e-test-bucket-embedding',
'projects/e2e/test-video.mp4',
mp4Buffer,
mp4Buffer.length,
'video/mp4',
)
// 3. Create Workflow Task (ai_embedding, pending)
const task = await prisma.workflowTask.create({
data: {
type: 'ai_embedding',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
agent: { agentId: agentUser.id },
},
},
})
// 4. Wait for workflow to complete
console.log(
`Submitted E2E Video Embedding Workflow Task. ID: ${task.id}. Awaiting completion...`,
)
const completedTask = await workflowService.executeWait(task, 45000)
// 5. Verification
expect(completedTask.status).toBe('completed')
// Verify raw embedding is saved in DB with start and end times
const dbEmbeddings = (await prisma.$queryRaw`
SELECT id, asset_id as "assetId", start_time as "startTime", end_time as "endTime"
FROM asset_embeddings
WHERE asset_id = ${asset.id}
ORDER BY start_time ASC
`) as { id: string; startTime: number | null; endTime: number | null }[]
expect(dbEmbeddings.length).toBeGreaterThan(0)
expect(dbEmbeddings[0].startTime).toBe(0)
expect(dbEmbeddings[0].endTime).toBe(1)
}, 50000)
it('should fail agentEmbeddingMedia workflow for an unsupported audio asset', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Audio Embedding Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Audio Embedding Project', teamId: team.id },
})
const agentUser = await prisma.user.create({
data: {
name: 'E2E Audio Embedding Agent User',
email: 'e2e-aud-emb-agent@shumai.ai',
type: 'agent',
},
})
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: agentUser.id,
role: 'editor',
},
})
const provider = await prisma.provider.create({
data: {
name: 'google',
teamId: team.id,
config: { api: 'google-generative-ai', apiKey: 'dummy-google-api-key' },
},
})
const model = await prisma.model.create({
data: {
modelId: 'gemini',
name: 'Gemini',
providerId: provider.id,
config: {
input: ['text'],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 8192,
maxTokens: 2048,
reasoning: false,
},
},
})
await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'embedding',
enabled: true,
providerId: provider.id,
modelId: model.id,
config: {
provider: 'google',
model: 'gemini',
},
},
})
const storageKey = await prisma.storageKey.create({
data: {
key: 'projects/e2e/test-audio.wav',
},
})
const asset = await prisma.asset.create({
data: {
name: 'test-audio.wav',
type: 'file',
status: 'uploaded',
mediaType: 'audio/wav',
projectId: project.id,
storageKeyId: storageKey.id,
},
})
// 2. Seed S3 Storage from Fixture
const wavPath = path.join(fixturesDir, 'small.wav')
const wavBuffer = fs.readFileSync(wavPath)
await s3Service.putObject(
'shumai-e2e-test-bucket-embedding',
'projects/e2e/test-audio.wav',
wavBuffer,
wavBuffer.length,
'audio/wav',
)
// 3. Create Workflow Task (ai_embedding, pending)
const task = await prisma.workflowTask.create({
data: {
type: 'ai_embedding',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
agent: { agentId: agentUser.id },
},
},
})
// 4. Wait for workflow to fail and verify error message
console.log(
`Submitted E2E Audio Embedding Workflow Task. ID: ${task.id}. Awaiting failure...`,
)
await expect(workflowService.executeWait(task, 45000)).rejects.toThrow(
'unsupported media type for embeddings: audio/wav',
)
// Verify task status in DB is failed
const dbTask = await prisma.workflowTask.findUnique({
where: { id: task.id },
})
expect(dbTask?.status).toBe(WorkflowTaskStatus.failed)
}, 50000)
},
)
@@ -0,0 +1,146 @@
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { workflowService, TaskQueueAgent } from '@shumai/workflow-core'
import { initAgentWorkflows } from '@shumai/agent'
import { s3Service } from '@shumai/core/src/s3/s3'
import { fileURLToPath } from 'url'
import * as path from 'path'
const currentDir = path.dirname(fileURLToPath(import.meta.url))
const agentWorkflowsPath = path.resolve(currentDir, '../../../apps/agent/src/workflows.ts')
describe.each(['local', 'temporal'] as const)(
'Workflow E2E - agentToolCall (executor: %s)',
(mode) => {
setupTestDbHooks()
let agentWorkerPromise: Promise<void> | null = null
beforeAll(async () => {
process.env.S3_BUCKET = 'shumai-e2e-test-bucket-toolcall'
process.env.GEMINI_API_KEY = 'dummy-key'
workflowService.setExecutorType(mode)
initAgentWorkflows()
if (mode === 'temporal') {
console.log('Starting background worker for tool call Temporal E2E tests...')
agentWorkerPromise = workflowService.startWorkers(TaskQueueAgent, {
workflowsPath: agentWorkflowsPath,
})
await new Promise((resolve) => setTimeout(resolve, 2000))
} else {
console.log('Starting local workflow service polling...')
workflowService.start()
}
})
afterAll(async () => {
if (mode === 'temporal') {
console.log('Shutting down Temporal workers...')
await workflowService.shutdownWorkers()
await Promise.all([agentWorkerPromise].filter(Boolean))
}
workflowService.close()
vi.restoreAllMocks()
try {
console.log('Cleaning up local E2E storage files...')
await s3Service.deletePrefix('shumai-e2e-test-bucket-toolcall', '')
} catch (err) {
console.error('Failed to clean up E2E storage folder:', err)
}
})
it('should run agentToolCall workflow and execute list_assets successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Tool Call Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Tool Call Project', teamId: team.id },
})
const regularUser = await prisma.user.create({
data: {
name: 'Regular Editor User',
email: 'editor@example.com',
type: 'human',
},
})
// Setup team member with owner/editor role and team scope
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: regularUser.id,
role: 'editor',
scope: 'team',
},
})
// Create a folder asset (parent)
const parentFolder = await prisma.asset.create({
data: {
name: 'TestParentFolder',
type: 'folder',
status: 'processed',
projectId: project.id,
},
})
// Create a child file asset inside the parent folder
const childFile = await prisma.asset.create({
data: {
name: 'TestChildFile.png',
type: 'file',
status: 'uploaded',
mediaType: 'image/png',
projectId: project.id,
parentId: parentFolder.id,
},
})
// 2. Create Workflow Task
const task = await prisma.workflowTask.create({
data: {
type: 'agent_tool_call',
status: 'pending',
assetId: parentFolder.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
agentToolCall: {
toolName: 'list_assets',
args: {
parent: parentFolder.id,
page: 1,
pageSize: 20,
type: 'all',
},
userId: regularUser.id,
},
},
},
})
// 3. Wait for workflow to complete
console.log(`Submitted E2E Tool Call Workflow Task. ID: ${task.id}. Awaiting completion...`)
const completedTask = await workflowService.executeWait(task, 45000)
// 4. Verification
expect(completedTask.status).toBe('completed')
// Verify task output contains the list of files, and childFile.id is present in assets
const output = completedTask.output as unknown as { assets: { id: string }[] }
expect(output).toBeDefined()
expect(output.assets).toBeDefined()
expect(output.assets.length).toBeGreaterThan(0)
const assetIds = output.assets.map((a) => a.id)
expect(assetIds).toContain(childFile.id)
}, 50000)
},
)
@@ -0,0 +1,127 @@
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { workflowService, TaskQueueAgent } from '@shumai/workflow-core'
import { initAgentWorkflows } from '@shumai/agent'
import { s3Service } from '@shumai/core/src/s3/s3'
const mockEmbedContent = vi.fn().mockResolvedValue({
embeddings: [
{
values: Array(1536).fill(0.35),
},
],
})
vi.mock('@google/genai', () => {
return {
// eslint-disable-next-line @typescript-eslint/naming-convention
GoogleGenAI: class {
models = {
embedContent: mockEmbedContent,
}
},
}
})
import { fileURLToPath } from 'url'
import * as path from 'path'
const currentDir = path.dirname(fileURLToPath(import.meta.url))
const agentWorkflowsPath = path.resolve(currentDir, '../../../apps/agent/src/workflows.ts')
describe.each(['local', 'temporal'] as const)(
'Workflow E2E - queryEmbeddingForSearch (executor: %s)',
(mode) => {
setupTestDbHooks()
let agentWorkerPromise: Promise<void> | null = null
beforeAll(async () => {
process.env.S3_BUCKET = 'shumai-e2e-test-bucket-query'
process.env.GEMINI_API_KEY = 'dummy-key'
workflowService.setExecutorType(mode)
initAgentWorkflows()
if (mode === 'temporal') {
console.log('Starting background worker for query embedding Temporal E2E tests...')
agentWorkerPromise = workflowService.startWorkers(TaskQueueAgent, {
workflowsPath: agentWorkflowsPath,
})
await new Promise((resolve) => setTimeout(resolve, 2000))
} else {
console.log('Starting local workflow service polling...')
workflowService.start()
}
})
afterAll(async () => {
if (mode === 'temporal') {
console.log('Shutting down Temporal workers...')
await workflowService.shutdownWorkers()
await Promise.all([agentWorkerPromise].filter(Boolean))
}
workflowService.close()
vi.restoreAllMocks()
try {
console.log('Cleaning up local E2E storage files...')
await s3Service.deletePrefix('shumai-e2e-test-bucket-query', '')
} catch (err) {
console.error('Failed to clean up E2E storage folder:', err)
}
})
it('should run queryEmbeddingForSearch workflow and return embedding successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Query Embedding Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Query Embedding Project', teamId: team.id },
})
const asset = await prisma.asset.create({
data: {
name: 'dummy-search-asset',
type: 'file',
status: 'uploaded',
mediaType: 'text/plain',
projectId: project.id,
},
})
// 2. Create Workflow Task
const task = await prisma.workflowTask.create({
data: {
type: 'query_embedding_for_search',
status: 'pending',
teamId: team.id,
projectId: project.id,
assetId: asset.id,
payload: {
projectId: project.id,
queryEmbeddingForSearch: {
text: 'test search query',
},
},
},
})
// 3. Wait for workflow to complete
console.log(
`Submitted E2E Query Embedding Workflow Task. ID: ${task.id}. Awaiting completion...`,
)
const completedTask = await workflowService.executeWait(task, 45000)
// 4. Verification
expect(completedTask.status).toBe('completed')
// Verify output payload contains correct embedding values and usage metadata
const output = completedTask.output as unknown as { embedding: number[] }
expect(output).toBeDefined()
expect(output.embedding).toEqual(Array(1536).fill(0.35))
expect(completedTask.model).toBe('gemini-embedding-2')
}, 50000)
},
)
@@ -0,0 +1,47 @@
import { setup as dbSetup, teardown as dbTeardown } from '../../db/src/test-global-setup'
import { GenericContainer, StartedTestContainer } from 'testcontainers'
let temporalContainer: StartedTestContainer | null = null
export async function setup() {
// 1. Start Postgres DB container & run migrations
await dbSetup()
// 2. Start Temporal Dev Server container for the test suite
console.log('Starting Temporal Dev Server container using testcontainers...')
try {
temporalContainer = await new GenericContainer('temporalio/temporal:latest')
.withExposedPorts(7233, 8233)
.withCommand(['server', 'start-dev', '--ip', '0.0.0.0'])
.start()
const host = temporalContainer.getHost()
const mappedPort = temporalContainer.getMappedPort(7233)
const address = `${host}:${mappedPort}`
process.env.TEMPORAL_ADDRESS = address
console.log(`Temporal Dev Server container started. Address: ${address}`)
// Brief sleep to ensure temporal dev server is fully ready to accept gRPC clients
await new Promise((resolve) => setTimeout(resolve, 2000))
} catch (err) {
console.error('Failed to start Temporal Dev Server container:', err)
throw err
}
}
export async function teardown() {
// 1. Stop Temporal container if running
if (temporalContainer) {
console.log('Stopping Temporal Dev Server container...')
try {
await temporalContainer.stop()
} catch (err) {
console.error('Failed to stop Temporal Dev Server container:', err)
}
temporalContainer = null
}
// 2. Stop Postgres DB container
await dbTeardown()
}
+459
View File
@@ -0,0 +1,459 @@
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
import { prisma, AssetStatus } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { workflowService, TaskQueueTranscode } from '@shumai/workflow-core'
import { initTranscodeWorkflows } from '@shumai/transcode'
import { s3Service } from '@shumai/core/src/s3/s3'
import { fileURLToPath } from 'url'
import * as path from 'path'
import * as fs from 'fs'
const currentDir = path.dirname(fileURLToPath(import.meta.url))
const transcodeWorkflowsPath = path.resolve(currentDir, '../../../apps/transcode/src/workflows.ts')
const fixturesDir = path.resolve(currentDir, '../fixtures')
describe.each(['local', 'temporal'] as const)(
'Workflow E2E - transcodeMedia (executor: %s)',
(mode) => {
setupTestDbHooks()
let transcodeWorkerPromise: Promise<void> | null = null
beforeAll(async () => {
process.env.S3_BUCKET = 'shumai-e2e-test-bucket-transcode'
workflowService.setExecutorType(mode)
initTranscodeWorkflows()
if (mode === 'temporal') {
console.log('Starting background worker for transcode Temporal E2E tests...')
transcodeWorkerPromise = workflowService.startWorkers(TaskQueueTranscode, {
workflowsPath: transcodeWorkflowsPath,
})
await new Promise((resolve) => setTimeout(resolve, 2000))
} else {
console.log('Starting local workflow service polling...')
workflowService.start()
}
})
afterAll(async () => {
if (mode === 'temporal') {
console.log('Shutting down Temporal workers...')
await workflowService.shutdownWorkers()
await Promise.all([transcodeWorkerPromise].filter(Boolean))
}
workflowService.close()
vi.restoreAllMocks()
try {
console.log('Cleaning up local E2E storage files...')
await s3Service.deletePrefix('shumai-e2e-test-bucket-transcode', '')
} catch (err) {
console.error('Failed to clean up E2E storage folder:', err)
}
})
it('should run transcodeMedia workflow for a video asset successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Video Transcode Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Video Transcode Project', teamId: team.id },
})
const storageKey = await prisma.storageKey.create({
data: {
key: 'projects/e2e/video-trans.mp4',
},
})
const asset = await prisma.asset.create({
data: {
name: 'video-trans.mp4',
type: 'file',
status: 'uploaded',
mediaType: 'video/mp4',
projectId: project.id,
storageKeyId: storageKey.id,
},
})
// 2. Seed S3 Storage from Fixture
const mp4Path = path.join(fixturesDir, 'small.mp4')
const mp4Buffer = fs.readFileSync(mp4Path)
await s3Service.putObject(
'shumai-e2e-test-bucket-transcode',
'projects/e2e/video-trans.mp4',
mp4Buffer,
mp4Buffer.length,
'video/mp4',
)
// 3. Create Workflow Task
const task = await prisma.workflowTask.create({
data: {
type: 'transcode',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
transcode: {
videoStrategy: 'best_match',
thumbnail: false,
poster: true,
sprite: true,
},
},
},
})
// 4. Wait for workflow to complete
console.log(
`Submitted E2E Video Transcode Workflow Task. ID: ${task.id}. Awaiting completion...`,
)
const completedTask = await workflowService.executeWait(task, 45000)
// 5. Verification
expect(completedTask.status).toBe('completed')
// Verify Asset status is updated to processed
const updatedAsset = await prisma.asset.findUnique({
where: { id: asset.id },
})
expect(updatedAsset?.status).toBe(AssetStatus.processed)
// Verify media info contains videoTranscodes, poster, sprite, and duration details
const mediaInfo = updatedAsset?.media as unknown as {
mimeType: string
duration: number
videoTranscodes: { key: string }[]
poster: unknown
sprite: unknown
}
expect(mediaInfo).toBeDefined()
expect(mediaInfo.mimeType).toBe('video/mp4')
expect(mediaInfo.duration).toBeCloseTo(1.0, 1)
expect(mediaInfo.videoTranscodes).toBeDefined()
expect(mediaInfo.videoTranscodes.length).toBeGreaterThan(0)
expect(mediaInfo.poster).toBeDefined()
expect(mediaInfo.sprite).toBeDefined()
}, 50000)
it('should run transcodeMedia workflow for an image asset successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Image Transcode Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Image Transcode Project', teamId: team.id },
})
const storageKey = await prisma.storageKey.create({
data: {
key: 'projects/e2e/image-trans.png',
},
})
const asset = await prisma.asset.create({
data: {
name: 'image-trans.png',
type: 'file',
status: 'uploaded',
mediaType: 'image/png',
projectId: project.id,
storageKeyId: storageKey.id,
},
})
// 2. Seed S3 Storage from Fixture
const pngPath = path.join(fixturesDir, 'small.png')
const pngBuffer = fs.readFileSync(pngPath)
await s3Service.putObject(
'shumai-e2e-test-bucket-transcode',
'projects/e2e/image-trans.png',
pngBuffer,
pngBuffer.length,
'image/png',
)
// 3. Create Workflow Task
const task = await prisma.workflowTask.create({
data: {
type: 'transcode',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
transcode: {
thumbnail: true,
},
},
},
})
// 4. Wait for workflow to complete
console.log(
`Submitted E2E Image Transcode Workflow Task. ID: ${task.id}. Awaiting completion...`,
)
const completedTask = await workflowService.executeWait(task, 45000)
// 5. Verification
expect(completedTask.status).toBe('completed')
const updatedAsset = await prisma.asset.findUnique({
where: { id: asset.id },
})
expect(updatedAsset?.status).toBe(AssetStatus.processed)
const mediaInfo = updatedAsset?.media as unknown as {
mimeType: string
imageTranscodes: unknown[]
thumbnail: unknown
}
expect(mediaInfo).toBeDefined()
expect(mediaInfo.mimeType).toBe('image/png')
expect(mediaInfo.imageTranscodes).toBeDefined()
expect(mediaInfo.imageTranscodes.length).toBeGreaterThan(0)
expect(mediaInfo.thumbnail).toBeDefined()
}, 50000)
it('should run transcodeMedia workflow for an audio asset successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Audio Transcode Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Audio Transcode Project', teamId: team.id },
})
const storageKey = await prisma.storageKey.create({
data: {
key: 'projects/e2e/audio-trans.wav',
},
})
const asset = await prisma.asset.create({
data: {
name: 'audio-trans.wav',
type: 'file',
status: 'uploaded',
mediaType: 'audio/wav',
projectId: project.id,
storageKeyId: storageKey.id,
},
})
// 2. Seed S3 Storage from Fixture
const wavPath = path.join(fixturesDir, 'small.wav')
const wavBuffer = fs.readFileSync(wavPath)
await s3Service.putObject(
'shumai-e2e-test-bucket-transcode',
'projects/e2e/audio-trans.wav',
wavBuffer,
wavBuffer.length,
'audio/wav',
)
// 3. Create Workflow Task
const task = await prisma.workflowTask.create({
data: {
type: 'transcode',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
transcode: {},
},
},
})
// 4. Wait for workflow to complete
console.log(
`Submitted E2E Audio Transcode Workflow Task. ID: ${task.id}. Awaiting completion...`,
)
const completedTask = await workflowService.executeWait(task, 45000)
// 5. Verification
expect(completedTask.status).toBe('completed')
const updatedAsset = await prisma.asset.findUnique({
where: { id: asset.id },
})
expect(updatedAsset?.status).toBe(AssetStatus.processed)
const mediaInfo = updatedAsset?.media as unknown as {
mimeType: string
videoTranscodes: { key: string }[]
}
expect(mediaInfo).toBeDefined()
expect(mediaInfo.mimeType).toBe('audio/wav')
expect(mediaInfo.videoTranscodes).toBeDefined()
expect(mediaInfo.videoTranscodes.length).toBeGreaterThan(0)
// Audio proxy key should end with -audio-proxy.mp4
expect(mediaInfo.videoTranscodes[0].key).toContain('-audio-proxy.mp4')
}, 50000)
it('should run transcodeMedia screenshot extraction successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Video Screenshot Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Video Screenshot Project', teamId: team.id },
})
const storageKey = await prisma.storageKey.create({
data: {
key: 'projects/e2e/video-shot.mp4',
},
})
const asset = await prisma.asset.create({
data: {
name: 'video-shot.mp4',
type: 'file',
status: 'uploaded',
mediaType: 'video/mp4',
projectId: project.id,
storageKeyId: storageKey.id,
},
})
// 2. Seed S3 Storage from Fixture
const mp4Path = path.join(fixturesDir, 'small.mp4')
const mp4Buffer = fs.readFileSync(mp4Path)
await s3Service.putObject(
'shumai-e2e-test-bucket-transcode',
'projects/e2e/video-shot.mp4',
mp4Buffer,
mp4Buffer.length,
'video/mp4',
)
// 3. Create Workflow Task
const task = await prisma.workflowTask.create({
data: {
type: 'transcode',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
screenshot: {
start: 0,
end: 1,
count: 1,
},
},
},
})
// 4. Wait for workflow to complete
console.log(
`Submitted E2E Video Screenshot Workflow Task. ID: ${task.id}. Awaiting completion...`,
)
const completedTask = await workflowService.executeWait(task, 45000)
// 5. Verification
expect(completedTask.status).toBe('completed')
// Verify task output contains list of screenshots
const output = completedTask.output as unknown as { screenshots: { key: string }[] }
expect(output).toBeDefined()
expect(output.screenshots).toBeDefined()
expect(output.screenshots.length).toBe(1)
expect(output.screenshots[0].key).toContain('screenshots/')
}, 50000)
it('should run transcodeMedia image annotation overlay successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Image Annotation Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Image Annotation Project', teamId: team.id },
})
const storageKey = await prisma.storageKey.create({
data: {
key: 'projects/e2e/image-ann.png',
},
})
const asset = await prisma.asset.create({
data: {
name: 'image-ann.png',
type: 'file',
status: 'uploaded',
mediaType: 'image/png',
projectId: project.id,
storageKeyId: storageKey.id,
},
})
// 2. Seed S3 Storage from Fixture
const pngPath = path.join(fixturesDir, 'small.png')
const pngBuffer = fs.readFileSync(pngPath)
await s3Service.putObject(
'shumai-e2e-test-bucket-transcode',
'projects/e2e/image-ann.png',
pngBuffer,
pngBuffer.length,
'image/png',
)
// 3. Create Workflow Task
const task = await prisma.workflowTask.create({
data: {
type: 'transcode',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
imageAnnotation: {
annotations: [
{
type: 'box',
color: '#ff0000',
points: [
[0.1, 0.1],
[0.9, 0.9],
],
},
],
},
},
},
})
// 4. Wait for workflow to complete
console.log(
`Submitted E2E Image Annotation Workflow Task. ID: ${task.id}. Awaiting completion...`,
)
const completedTask = await workflowService.executeWait(task, 45000)
// 5. Verification
expect(completedTask.status).toBe('completed')
// Verify task output contains overlay image key
const output = completedTask.output as unknown as { key: string }
expect(output).toBeDefined()
expect(output.key).toContain('annotations/')
}, 50000)
},
)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/tableprinter",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"main": "./src/index.ts",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/transcode",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"main": "./src/index.ts",
@@ -75,6 +75,7 @@ describe('Transcode Workflow', () => {
assetId: 'asset-1',
type: WorkflowTaskType.transcode,
status: WorkflowTaskStatus.pending,
sessionId: null,
output: null,
payload: {
projectId: 'proj-1',
@@ -166,6 +167,7 @@ describe('Transcode Workflow', () => {
assetId: 'asset-image',
type: WorkflowTaskType.transcode,
status: WorkflowTaskStatus.pending,
sessionId: null,
output: null,
payload: {
projectId: 'proj-1',
@@ -249,6 +251,7 @@ describe('Transcode Workflow', () => {
assetId: 'asset-1',
type: WorkflowTaskType.transcode,
status: WorkflowTaskStatus.pending,
sessionId: null,
output: null,
payload: {
projectId: 'proj-1',
@@ -281,6 +284,7 @@ describe('Transcode Workflow', () => {
assetId: 'asset-video',
type: WorkflowTaskType.transcode,
status: WorkflowTaskStatus.pending,
sessionId: null,
output: null,
payload: {
projectId: 'proj-1',
@@ -344,6 +348,7 @@ describe('Transcode Workflow', () => {
assetId: 'asset-image',
type: WorkflowTaskType.transcode,
status: WorkflowTaskStatus.pending,
sessionId: null,
output: null,
payload: {
projectId: 'proj-1',
@@ -396,6 +401,7 @@ describe('Transcode Workflow', () => {
assetId: 'asset-audio',
type: WorkflowTaskType.transcode,
status: WorkflowTaskStatus.pending,
sessionId: null,
output: null,
payload: {
projectId: 'proj-1',
+74 -10
View File
@@ -1,5 +1,5 @@
import { Badge } from '@/ui/components/ui/badge'
import { client } from '@/ui/api/client'
import { Badge } from '@/ui/components/ui/badge'
import {
DropdownMenu,
DropdownMenuContent,
@@ -13,17 +13,33 @@ import {
DropdownMenuTrigger,
} from '@/ui/components/ui/dropdown-menu'
import {
BotFilled,
DockToLeft,
DockToLeftFilled,
DockToRight,
DockToRightFilled,
} from '@/ui/components/ui/icons'
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from '@/ui/components/ui/tooltip'
import { usePermissions } from '@/ui/hooks/use-permissions'
import { cn } from '@/ui/lib/utils'
import { m } from '@/ui/paraglide/messages.js'
import type { AncestorFolder } from '@shumai/dtos'
import { Link, useNavigate } from '@tanstack/react-router'
import { Check, ChevronDown, Columns2, FileIcon, History, LayoutGrid, List } from 'lucide-react'
import {
Bot,
Check,
ChevronDown,
Columns2,
FileIcon,
History,
LayoutGrid,
List,
} from 'lucide-react'
interface BreadcrumbNavProps {
teamId: string
@@ -43,6 +59,8 @@ interface BreadcrumbNavProps {
onLeftSidebarToggle?: () => void
isRightSidebarCollapsed: boolean
onRightSidebarToggle: () => void
isChatbotOpen?: boolean
onChatbotToggle?: () => void
isPublic?: boolean
shareId?: string
onFolderClick?: (folderId: string) => void
@@ -81,6 +99,8 @@ export function BreadcrumbNav({
onLeftSidebarToggle,
isRightSidebarCollapsed,
onRightSidebarToggle,
isChatbotOpen = false,
onChatbotToggle,
isPublic = false,
onFolderClick,
fileId,
@@ -94,6 +114,8 @@ export function BreadcrumbNav({
const navigate = useNavigate()
const { canEdit } = usePermissions(projectId)
const isChatbotDisabled = false
const handleVersionClick = (versionId: string) => {
if (!fileId) return
if (isPublic && shareId) {
@@ -364,10 +386,10 @@ export function BreadcrumbNav({
)}
</div>
{!fileId && (
<div className="flex items-center gap-2">
<div className="flex flex-shrink-0 items-center gap-1 rounded-md border border-border bg-background p-1">
{/* Left Sidebar Toggle */}
{!isPublic && onLeftSidebarToggle && (
{!fileId && !isPublic && onLeftSidebarToggle && (
<button
onClick={onLeftSidebarToggle}
className="rounded p-1.5 text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground"
@@ -382,7 +404,7 @@ export function BreadcrumbNav({
)}
{/* Card View */}
{!isPublic && displayStyle && onDisplayStyleChange && (
{!fileId && !isPublic && displayStyle && onDisplayStyleChange && (
<>
<button
onClick={() => onDisplayStyleChange('card')}
@@ -414,17 +436,59 @@ export function BreadcrumbNav({
{/* Right Sidebar Toggle */}
<button
onClick={onRightSidebarToggle}
className="rounded p-1.5 text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground"
className={`rounded p-1.5 transition-colors ${
!isRightSidebarCollapsed && !isChatbotOpen
? 'bg-accent text-accent-foreground'
: 'text-muted-foreground hover:bg-accent/50 hover:text-foreground'
}`}
title={isRightSidebarCollapsed ? 'Show Right Sidebar' : 'Hide Right Sidebar'}
>
{isRightSidebarCollapsed ? (
<DockToRight className="h-4 w-4" />
) : (
{!isRightSidebarCollapsed && !isChatbotOpen ? (
<DockToRightFilled className="h-4 w-4 fill-primary stroke-0" />
) : (
<DockToRight className="h-4 w-4" />
)}
</button>
</div>
)}
{/* Chatbot Toggle Button */}
{!isPublic && onChatbotToggle && (
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<span>
<button
disabled={isChatbotDisabled}
onClick={onChatbotToggle}
className={cn(
'h-9 w-9 flex items-center justify-center rounded-full border border-border bg-background transition-colors',
isChatbotOpen
? 'bg-accent text-accent-foreground'
: 'text-muted-foreground hover:bg-accent hover:text-foreground',
isChatbotDisabled && 'opacity-50 cursor-not-allowed',
)}
>
{isChatbotOpen ? (
<BotFilled className="h-6 w-6 text-primary transition-colors" />
) : (
<Bot className="h-6 w-6 text-muted-foreground transition-colors" />
)}
</button>
</span>
</TooltipTrigger>
<TooltipContent>
<p>
{isChatbotDisabled
? m.configure_chatbot_agent_first()
: isChatbotOpen
? m.hide_chatbot()
: m.show_chatbot()}
</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
)}
</div>
</div>
)
}
+110 -9
View File
@@ -7,11 +7,27 @@ import {
DialogHeader,
DialogTitle,
} from '@/ui/components/ui/dialog'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/ui/components/ui/dropdown-menu'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@/ui/components/ui/alert-dialog'
import { Separator } from '@/ui/components/ui/separator'
import { formatTimeAgo } from '@/ui/lib/time'
import type { AttachmentInfo, CommentInfo, UserInfo } from '@shumai/dtos'
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { Download, File, Terminal } from 'lucide-react'
import { Download, File, Terminal, MoreHorizontal, Trash2 } from 'lucide-react'
import React from 'react'
import Markdown from 'react-markdown'
import { Avatar, AvatarFallback, AvatarImage } from '../ui/avatar'
@@ -145,6 +161,31 @@ export const MessageCard: React.FC<MessageCardProps> = ({
},
})
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = React.useState(false)
const { mutate: deleteComment, isPending: isDeleting } = useMutation({
mutationFn: async () => {
const res = await client.api.comments[':commentId'].$delete({
param: { commentId: message.id },
})
if (!res.ok) throw new Error('Failed to delete comment')
return await res.json()
},
onSuccess: () => {
queryClient.invalidateQueries({
predicate: (query) => query.queryKey.includes('comments'),
})
setIsDeleteDialogOpen(false)
},
})
const canDelete = React.useMemo(() => {
if (!me) return false
if (me.role === 'owner') return true
if (message.creator?.id === me.id) return true
return false
}, [me, message.creator?.id])
const { data: logs, isLoading: isLogsLoading } = useQuery({
queryKey: ['agent-sessions', message.sessionId, 'entries'],
queryFn: async () => {
@@ -222,14 +263,44 @@ export const MessageCard: React.FC<MessageCardProps> = ({
{/* Content */}
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1">
<span className="font-semibold text-sm">{creator?.name || 'Unknown'}</span>
<span className="text-xs text-gray-400">
{new Date(message.createdAt!).toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
})}
</span>
<div className="flex items-center justify-between w-full mb-1">
<div className="flex items-center gap-2">
<span className="font-semibold text-sm">{creator?.name || 'Unknown'}</span>
<span className="text-xs text-gray-400">
{new Date(message.createdAt!).toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
})}
</span>
</div>
{canDelete && (
<div className="opacity-0 group-hover:opacity-100 transition-opacity">
<DropdownMenu modal={false}>
<DropdownMenuTrigger asChild>
<Button
size="icon-xs"
variant="ghost"
className="h-6 w-6 cursor-pointer"
onClick={(e) => e.stopPropagation()}
>
<MoreHorizontal className="h-4 w-4 text-foreground/70" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" onClick={(e) => e.stopPropagation()}>
<DropdownMenuItem
className="text-destructive focus:text-destructive cursor-pointer"
onClick={(e) => {
e.stopPropagation()
setIsDeleteDialogOpen(true)
}}
>
<Trash2 className="mr-2 h-4 w-4" />
<span>{m.delete()}</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
)}
</div>
<div className="flow-root mt-1">
@@ -664,6 +735,36 @@ export const MessageCard: React.FC<MessageCardProps> = ({
</div>
</DialogContent>
</Dialog>
<AlertDialog open={isDeleteDialogOpen} onOpenChange={setIsDeleteDialogOpen}>
<AlertDialogContent onClick={(e) => e.stopPropagation()}>
<AlertDialogHeader>
<AlertDialogTitle>{m.delete_comment_confirm()}</AlertDialogTitle>
<AlertDialogDescription>{m.delete_comment_description()}</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel
disabled={isDeleting}
onClick={(e) => {
e.stopPropagation()
setIsDeleteDialogOpen(false)
}}
>
{m.cancel()}
</AlertDialogCancel>
<AlertDialogAction
className="bg-destructive hover:bg-destructive/90 text-white"
disabled={isDeleting}
onClick={(e) => {
e.stopPropagation()
deleteComment()
}}
>
{m.delete()}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
)
}
@@ -0,0 +1,673 @@
import { client } from '@/ui/api/client'
import { ScrollArea } from '@/ui/components/ui/scroll-area'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/ui/components/ui/select'
import { cn } from '@/ui/lib/utils'
import { m } from '@/ui/paraglide/messages.js'
import { useChatbotStore } from '@/ui/stores/chatbot'
import { useTeamContextStore } from '@/ui/stores/team-context'
import { useDroppable } from '@dnd-kit/react'
import type { ChatMessage } from '@shumai/dtos'
import { useQuery } from '@tanstack/react-query'
import {
ArrowLeft,
ArrowUp,
Bot,
Brain,
ChevronDown,
History,
Loader2,
Plus,
Trash2,
Wrench,
} from 'lucide-react'
import { useEffect, useRef, useState } from 'react'
import Markdown from 'react-markdown'
import { formatTimeAgo } from '../lib/time'
interface ChatbotSidebarProps {
projectId: string
contextAssetId?: string
}
export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProps) {
const {
isHistoryMode,
setIsHistoryMode,
currentSessionId,
chatAssets,
removeAsset,
messages,
historySessions,
isStreaming,
fetchHistorySessions,
loadSession,
deleteSession,
startNewSession,
sendMessage,
abortActiveSession,
selectedAgentId,
setSelectedAgentId,
} = useChatbotStore()
const { teamId, ensureTeamIdForProject } = useTeamContextStore()
useEffect(() => {
if (projectId) {
ensureTeamIdForProject(projectId)
}
}, [projectId, ensureTeamIdForProject])
const { data: agents = [] } = useQuery({
queryKey: ['agents', teamId],
queryFn: async () => {
if (!teamId) return []
const res = await client.api.teams[':teamId'].agents.$get({
param: { teamId },
})
if (!res.ok) throw new Error('failed to fetch agents')
return res.json()
},
enabled: !!teamId,
})
const chatAgents = agents.filter((a) => a.type === 'chat' && a.enabled)
useEffect(() => {
if (chatAgents.length > 0) {
const exists = chatAgents.some((a) => a.id === selectedAgentId)
if (!exists) {
setSelectedAgentId(chatAgents[0].id)
}
}
}, [chatAgents, selectedAgentId, setSelectedAgentId])
const [inputText, setInputText] = useState('')
const messagesEndRef = useRef<HTMLDivElement>(null)
const scrollAreaRef = useRef<HTMLDivElement>(null)
const isFirstScrollRef = useRef(true)
const isRestoringRef = useRef(true)
useEffect(() => {
isRestoringRef.current = true
isFirstScrollRef.current = true
}, [projectId, contextAssetId, isHistoryMode])
// Restore scroll position on mount or when navigation changes the project/context
useEffect(() => {
const viewport = scrollAreaRef.current?.querySelector('[data-slot="scroll-area-viewport"]')
if (!viewport) return
const { scrollTop, isAtBottom } = useChatbotStore.getState()
// Use a timeout to ensure the DOM has settled and scrollHeight is fully calculated
const timer = setTimeout(() => {
if (isAtBottom) {
viewport.scrollTop = viewport.scrollHeight
} else {
viewport.scrollTop = scrollTop
}
// Allow scroll events triggered by this restoration to fire and be ignored before tracking
const enableTimer = setTimeout(() => {
isRestoringRef.current = false
}, 50)
return () => clearTimeout(enableTimer)
}, 0)
return () => clearTimeout(timer)
}, [projectId, contextAssetId, isHistoryMode])
// Listen to scroll events on the viewport to save scroll state
useEffect(() => {
const viewport = scrollAreaRef.current?.querySelector('[data-slot="scroll-area-viewport"]')
if (!viewport) return
const handleScroll = () => {
if (isRestoringRef.current) return
const scrollTop = viewport.scrollTop
// We consider the user at the bottom if they are within 10px of the bottom boundary
const isAtBottom = viewport.scrollHeight - scrollTop - viewport.clientHeight < 10
useChatbotStore.getState().setScrollState(scrollTop, isAtBottom)
}
viewport.addEventListener('scroll', handleScroll)
return () => {
viewport.removeEventListener('scroll', handleScroll)
}
}, [projectId, contextAssetId, isHistoryMode])
const { ref: setDroppableRef, isDropTarget: isOver } = useDroppable({
id: 'chatbot-sidebar',
data: {
type: 'chatbot-sidebar',
},
})
// Auto-scroll messages list to bottom
useEffect(() => {
if (!isHistoryMode) {
const { isAtBottom } = useChatbotStore.getState()
if (isAtBottom || isStreaming) {
const behavior = isFirstScrollRef.current ? 'auto' : 'smooth'
messagesEndRef.current?.scrollIntoView({ behavior })
}
isFirstScrollRef.current = false
}
}, [messages, isStreaming, isHistoryMode])
// Fetch history when entering history mode
useEffect(() => {
if (isHistoryMode && teamId) {
fetchHistorySessions(teamId)
}
}, [isHistoryMode, fetchHistorySessions, teamId])
const handleSend = () => {
if (!inputText.trim() || isStreaming || !teamId) return
sendMessage(teamId, inputText, projectId, contextAssetId)
setInputText('')
}
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.nativeEvent.isComposing) return
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault()
handleSend()
}
}
const getMessageText = (content: unknown): string => {
if (typeof content === 'string') return content
if (Array.isArray(content)) {
return content
.map((c) => {
if (typeof c === 'string') return c
if (c && typeof c === 'object' && 'type' in c && c.type === 'text' && 'text' in c) {
return String((c as { text: unknown }).text)
}
return ''
})
.join('')
}
return ''
}
const preprocessMarkdown = (text: string): string => {
return text.replace(/<@([a-zA-Z0-9_-]+)>/g, '@$1').replace(/^\[([^\]]+)\]:/gm, '\\[$1\\]:')
}
const renderMessage = (msg: ChatMessage) => {
switch (msg.role as string) {
case 'user': {
const msgObj = msg as unknown as Record<string, unknown>
return (
<div key={msg.id} className="flex justify-end w-full">
<div className="bg-primary text-primary-foreground rounded-lg px-3 py-2 max-w-[85%] text-sm whitespace-pre-wrap shadow-xs break-words">
{getMessageText(msgObj.content)}
</div>
</div>
)
}
case 'assistant': {
const msgObj = msg as unknown as Record<string, unknown>
const content = msgObj.content as unknown[]
if (!content) return null
const renderedBlocks: React.ReactNode[] = []
content.forEach((block: unknown, idx: number) => {
if (!block || typeof block !== 'object') return
const b = block as unknown as Record<string, unknown>
switch (b.type) {
case 'text': {
const text = b.text
if (typeof text !== 'string' || !text.trim()) return
renderedBlocks.push(
<div
key={`txt-${idx}`}
className="text-sm leading-[1.8] prose prose-sm dark:prose-invert max-w-none break-words"
>
<Markdown
components={{
// eslint-disable-next-line @typescript-eslint/no-unused-vars
pre: ({ node, ...props }) => (
<pre
className="whitespace-pre-wrap break-all break-words bg-muted text-foreground p-4 rounded-md border border-border/50 font-mono text-sm"
{...props}
/>
),
}}
>
{preprocessMarkdown(text)}
</Markdown>
</div>,
)
break
}
case 'thinking': {
const thinking = b.thinking
if (typeof thinking !== 'string' || !thinking.trim()) return
renderedBlocks.push(
<details
key={`think-${idx}`}
className="group border border-border/50 rounded-lg bg-muted/20 my-2 overflow-hidden"
>
<summary className="flex items-center gap-2 p-2.5 text-xs font-medium text-muted-foreground select-none cursor-pointer hover:bg-muted/40 transition-colors list-none [&::-webkit-details-marker]:hidden">
<Brain className="h-3.5 w-3.5 text-primary/70 animate-pulse" />
<span>{m.role_thinking() || 'Thinking'}</span>
<ChevronDown className="h-3 w-3 ml-auto transition-transform group-open:rotate-180" />
</summary>
<div className="p-3 pt-0 text-xs text-muted-foreground/90 border-t border-border/40 bg-muted/10 leading-relaxed prose prose-sm dark:prose-invert max-w-none whitespace-pre-wrap font-sans">
<Markdown>{preprocessMarkdown(thinking)}</Markdown>
</div>
</details>,
)
break
}
case 'toolCall': {
const toolName = String(b.name || b.toolName || m.unknown())
const toolArgs = b.arguments || b.args
renderedBlocks.push(
<details
key={`tool-${idx}`}
className="group border border-border/50 rounded-lg bg-violet-500/5 my-2 overflow-hidden"
>
<summary className="flex items-center gap-2 p-2.5 text-xs font-semibold text-violet-600 dark:text-violet-400 select-none cursor-pointer hover:bg-violet-500/10 transition-colors list-none [&::-webkit-details-marker]:hidden">
<Wrench className="h-3.5 w-3.5" />
<span>{m.calling_tool()}</span>
<span className="font-mono bg-violet-500/10 px-1.5 py-0.5 rounded text-[11px] text-violet-700 dark:text-violet-300">
{toolName}
</span>
<ChevronDown className="h-3 w-3 ml-auto transition-transform group-open:rotate-180" />
</summary>
{!!toolArgs && (
<div className="p-3 pt-0 text-[10px] text-muted-foreground/90 border-t border-border/40 bg-violet-500/5 font-sans">
<div className="text-[9px] text-muted-foreground/60 uppercase font-bold tracking-wider mb-1.5">
{m.tool_arguments() || 'Arguments'}
</div>
<pre className="font-mono bg-muted/30 p-2 rounded border border-border/30 overflow-x-auto max-h-32">
{JSON.stringify(toolArgs, null, 2)}
</pre>
</div>
)}
</details>,
)
break
}
case 'image': {
const mimeType = String(b.mimeType || 'image/png')
const data = String(b.data || '')
renderedBlocks.push(
<div key={`img-${idx}`} className="my-2 max-w-full">
{data ? (
<img
src={data.startsWith('data:') ? data : `data:${mimeType};base64,${data}`}
alt="Attached asset"
className="max-h-60 rounded border border-border/50 object-contain shadow-xs"
/>
) : (
<div className="text-xs text-muted-foreground italic flex items-center gap-1.5 py-1">
{m.image_object()}
</div>
)}
</div>,
)
break
}
default: {
const stringified = JSON.stringify(block, null, 2)
if (stringified === '{}') return
renderedBlocks.push(
<pre
key={`other-${idx}`}
className="text-xs text-foreground/80 font-mono whitespace-pre-wrap break-words bg-muted/20 p-2 rounded"
>
{stringified}
</pre>,
)
}
}
})
if (renderedBlocks.length === 0) return null
let shouldShowFooter = false
if (content.length > 0) {
const lastBlock = content[content.length - 1]
if (typeof lastBlock === 'string' && lastBlock.trim().length > 0) {
shouldShowFooter = true
} else if (lastBlock && typeof lastBlock === 'object') {
const lb = lastBlock as Record<string, unknown>
if (lb.type === 'text' && typeof lb.text === 'string' && lb.text.trim().length > 0) {
shouldShowFooter = true
}
}
}
return (
<div key={msg.id} className="flex flex-col w-full space-y-1">
<div className="space-y-2 w-full">{renderedBlocks}</div>
{shouldShowFooter && (
<div className="text-[10px] text-muted-foreground self-start italic pt-1">
{m.created_by_agent()}
</div>
)}
</div>
)
}
case 'toolResult': {
return (
<div
key={msg.id}
className="flex items-center gap-2 text-xs text-muted-foreground bg-muted/40 p-2 rounded-md border border-border/50"
>
<span className="font-semibold">
{m.tool_result_with_name({ name: (msg as { toolName?: string }).toolName || 'tool' })}
</span>
</div>
)
}
case 'thinking_level_change':
case 'custom': {
const msgObj = msg as unknown as Record<string, unknown>
if (msgObj.customType === 'context_display_info') {
const details = msgObj.details as
| { assets?: Array<{ id: string; name: string; type: string }> }
| undefined
const assets = details?.assets || []
if (assets.length === 0) return null
return (
<div
key={msg.id}
className="text-xs text-muted-foreground bg-muted/40 p-2.5 rounded-lg border border-border/50 space-y-1 my-1"
>
<div className="font-semibold">{m.assets_added_to_context()}</div>
<ul className="list-disc list-inside space-y-0.5">
{assets.map((asset) => (
<li key={asset.id} className="truncate">
{asset.name} <span className="text-muted-foreground/70">({asset.type})</span>
</li>
))}
</ul>
</div>
)
}
const systemContent = msgObj.content || ''
return (
<div
key={msg.id}
className="text-center text-xs text-muted-foreground italic bg-muted/20 py-1.5 rounded"
>
{getMessageText(systemContent)}
</div>
)
}
default:
return null
}
}
return (
<div
ref={setDroppableRef}
className={cn(
'relative flex flex-col h-full bg-background transition-colors duration-200 min-h-0',
)}
>
{isOver && (
<div className="absolute inset-0 bg-accent/30 border-2 border-primary z-50 pointer-events-none flex items-center justify-center p-6 text-center animate-in fade-in duration-150">
<div className="bg-background/95 text-foreground px-4 py-3 rounded-xl border border-border/80 flex flex-col items-center gap-2 shadow-lg max-w-[80%] animate-in zoom-in-95 duration-150">
<Bot className="h-6 w-6 text-primary animate-bounce" />
<span className="text-sm font-semibold text-foreground">
{m.chatbot_drag_drop_hint() || 'Drop assets here'}
</span>
</div>
</div>
)}
{/* Header section */}
<div className="flex items-center justify-between p-4 pt-[0.7rem] flex-shrink-0">
{isHistoryMode ? (
<>
<div className="flex items-center gap-2 font-semibold">
<span>{m.history()}</span>
</div>
<button
onClick={() => setIsHistoryMode(false)}
className="p-1.5 hover:bg-accent rounded-md text-muted-foreground hover:text-foreground transition-colors"
title="Close History"
>
<ArrowLeft className="h-4 w-4" />
</button>
</>
) : (
<>
<div className="flex items-center gap-2 font-semibold">
<span>{m.shumai_agent()}</span>
</div>
<div className="flex items-center gap-1">
<button
onClick={startNewSession}
className="p-1.5 hover:bg-accent rounded-md text-muted-foreground hover:text-foreground transition-colors"
title={m.new_chat()}
>
<Plus className="h-4 w-4" />
</button>
<button
onClick={() => setIsHistoryMode(true)}
className="p-1.5 hover:bg-accent rounded-md text-muted-foreground hover:text-foreground transition-colors"
title={m.history()}
>
<History className="h-4 w-4" />
</button>
</div>
</>
)}
</div>
{/* Main body view */}
{isHistoryMode ? (
<ScrollArea className="flex-1 p-4 min-h-0">
{historySessions.length === 0 ? (
<div className="flex flex-col items-center justify-center h-48 text-muted-foreground p-4 text-center">
<History className="h-8 w-8 mb-2 opacity-50 text-muted-foreground" />
<p className="text-sm">{m.no_history_sessions()}</p>
</div>
) : (
<div className="space-y-2 pb-4">
{historySessions.map((sess) => {
const isActive = sess.id === currentSessionId
return (
<div
key={sess.id}
className={cn(
'group relative flex items-center justify-between p-3 border rounded-lg transition-all cursor-pointer bg-card',
isActive ? 'border-primary' : 'border-border hover:bg-accent/30',
)}
>
<div
onClick={() => teamId && loadSession(teamId, sess.id)}
className="flex-1 min-w-0 pr-6"
>
<div className="text-sm font-semibold text-foreground truncate">
{sess.name || m.new_chat()}
</div>
<div className="text-xs text-muted-foreground mt-0.5">
{formatTimeAgo(sess.createdAt)}
</div>
</div>
<button
onClick={(e) => {
e.stopPropagation()
if (teamId && confirm(m.delete_session_confirm())) {
deleteSession(teamId, sess.id)
}
}}
className="absolute right-3 opacity-0 group-hover:opacity-100 focus:opacity-100 transition-opacity text-muted-foreground hover:text-destructive p-1 rounded hover:bg-muted"
title="Delete Session"
>
<Trash2 className="h-4 w-4" />
</button>
</div>
)
})}
</div>
)}
</ScrollArea>
) : (
<>
{/* Active Chat view */}
<ScrollArea ref={scrollAreaRef} className="flex-1 p-4 min-h-0 [&>div>div]:!block">
{messages.length === 0 ? (
<div className="flex flex-col items-center justify-center h-48 border-2 border-dashed border-border rounded-lg text-muted-foreground p-4 text-center">
<Bot className="h-8 w-8 mb-2 opacity-50 text-muted-foreground" />
<p className="text-sm">{m.chatbot_drag_drop_hint()}</p>
</div>
) : (
<div className="space-y-4 pb-4">
{messages.map(renderMessage)}
{isStreaming &&
messages.length > 0 &&
messages[messages.length - 1].role === 'user' && (
<div className="flex items-center gap-1.5 text-xs text-muted-foreground italic">
<Loader2 className="h-3 w-3 animate-spin text-primary" />
<span>Thinking...</span>
</div>
)}
<div ref={messagesEndRef} />
</div>
)}
</ScrollArea>
{/* Input Area (nested inside the border exactly like comment input) */}
<div className="p-2 flex-shrink-0 bg-background">
<div className="relative flex flex-col w-full border border-foreground/20 rounded-2xl shadow-xs transition-all duration-200 focus-within:ring-2 focus-within:ring-primary focus-within:border-transparent bg-background overflow-hidden">
{/* Context assets list at the top inside the border */}
{chatAssets.length > 0 && (
<div className="flex flex-col gap-1.5 min-h-0 max-h-[160px] overflow-y-auto border-b border-border bg-muted/5 p-3 divide-y divide-border/50">
{chatAssets.map((asset) => (
<div
key={asset.id}
className="flex items-center justify-between py-1.5 text-xs group"
>
<div className="flex items-baseline gap-1.5 min-w-0 pr-2">
<span className="truncate font-medium text-foreground">{asset.name}</span>
<span className="text-[9px] text-muted-foreground uppercase flex-shrink-0">
{asset.type}
</span>
</div>
<button
onClick={() => removeAsset(asset.id)}
className="text-muted-foreground hover:text-destructive transition-colors p-0.5"
title={m.remove_from_context()}
>
<Trash2 className="h-3.5 w-3.5" />
</button>
</div>
))}
</div>
)}
{/* Textarea inside the border */}
<textarea
value={inputText}
onChange={(e) => setInputText(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Type a message..."
rows={2}
className="w-full bg-transparent border-none focus:ring-0 focus:outline-hidden resize-none min-h-[44px] max-h-[120px] px-3 py-2 text-sm focus-visible:outline-hidden focus:ring-transparent focus:border-transparent focus-visible:ring-0"
disabled={isStreaming}
/>
{/* Bottom row inside the border containing the agent selector and send button */}
<div className="flex justify-between items-center px-3 pb-2 pt-1">
{/* Agent selector */}
<div className="flex items-center gap-1.5 min-w-0 max-w-[70%]">
{chatAgents.length > 0 ? (
<Select
value={selectedAgentId || ''}
onValueChange={(val) => setSelectedAgentId(val)}
disabled={isStreaming}
>
<SelectTrigger className="h-8 border-none bg-transparent hover:bg-muted text-muted-foreground hover:text-foreground focus:ring-0 focus:ring-offset-0 px-2 py-0 gap-1.5 text-xs shrink-0 select-none shadow-none max-w-full">
<SelectValue placeholder={m.select_agent()} />
</SelectTrigger>
<SelectContent>
{chatAgents.map((agent) => (
<SelectItem key={agent.id} value={agent.id}>
<div className="flex items-center gap-2">
{agent.avatar ? (
<img
src={agent.avatar}
alt={agent.name}
className="w-4 h-4 rounded-full object-cover"
/>
) : (
<Bot className="w-4 h-4 text-muted-foreground" />
)}
<span className="text-xs">{agent.name}</span>
</div>
</SelectItem>
))}
</SelectContent>
</Select>
) : (
<div className="flex items-center gap-1.5 px-2 py-1 text-[11px] text-muted-foreground/60 italic select-none">
<span>{m.no_chat_agents_warning()}</span>
</div>
)}
</div>
{isStreaming ? (
<button
onClick={() => {
if (teamId) {
abortActiveSession(teamId)
}
}}
className="p-2 rounded-full transition-all duration-200 flex items-center justify-center shrink-0 shadow-sm bg-destructive text-destructive-foreground hover:bg-destructive/90 transform hover:-translate-y-0.5"
title="Stop generation"
>
<div className="h-4 w-4 flex items-center justify-center">
<div className="h-3 w-3 bg-current rounded-[2px] bg-white" />
</div>
</button>
) : (
<button
onClick={handleSend}
disabled={!inputText.trim() || !selectedAgentId}
className={cn(
'p-2 rounded-full transition-all duration-200 flex items-center justify-center shrink-0 shadow-sm',
inputText.trim() && selectedAgentId
? 'bg-primary text-primary-foreground hover:bg-primary/95 transform hover:-translate-y-0.5'
: 'bg-muted text-muted-foreground cursor-not-allowed opacity-50',
)}
>
<ArrowUp className="h-4 w-4" />
</button>
)}
</div>
</div>
</div>
</>
)}
</div>
)
}
@@ -149,6 +149,47 @@ export function CompareViewer({
applyBoth((h) => (shouldPlay ? h.play() : h.pause()))
}, [activeState?.video?.isPlaying, applyBoth])
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
const activeEl = document.activeElement
if (
activeEl &&
(activeEl.tagName === 'INPUT' ||
activeEl.tagName === 'TEXTAREA' ||
(activeEl instanceof HTMLElement && activeEl.isContentEditable))
) {
return
}
const isSpace = e.key === ' '
const isInteractive =
activeEl &&
(activeEl.tagName === 'BUTTON' ||
activeEl.tagName === 'A' ||
activeEl.tagName === 'SELECT' ||
activeEl.tagName === 'OPTION' ||
activeEl.getAttribute('role') === 'button' ||
activeEl.getAttribute('role') === 'link' ||
activeEl.getAttribute('role') === 'checkbox' ||
activeEl.getAttribute('role') === 'radio' ||
activeEl.getAttribute('role') === 'menuitem')
if (isSpace && isInteractive) {
return
}
if (e.key === ' ' || e.key.toLowerCase() === 'k') {
e.preventDefault()
handleTogglePlay()
}
}
window.addEventListener('keydown', handleKeyDown)
return () => {
window.removeEventListener('keydown', handleKeyDown)
}
}, [handleTogglePlay])
const handleSeek = useCallback(
(frame: number) => applyBoth((h) => h.seekToFrame(frame)),
[applyBoth],
@@ -1,6 +1,6 @@
import { client } from '@/ui/api/client'
import type { AssetInfo, FieldValueInfo } from '@shumai/dtos'
import { m } from '@/ui/paraglide/messages.js'
import type { AssetInfo, FieldValueInfo } from '@shumai/dtos'
import { useQuery } from '@tanstack/react-query'
import { Badge } from '@/ui/components/ui/badge'
@@ -14,17 +14,17 @@ import {
DropdownMenuTrigger,
} from '@/ui/components/ui/dropdown-menu'
import { EditableText } from '@/ui/components/ui/editable-text'
import { ProgressCircle } from '@/ui/components/ui/progress-circle'
import { Skeleton } from '@/ui/components/ui/skeleton'
import { formatTimeAgo } from '@/ui/lib/time'
import { cn } from '@/ui/lib/utils'
import { useUploadStore } from '@/ui/stores/upload'
import { useDraggable, useDroppable } from '@dnd-kit/react'
import { Download, Edit, History, MoreHorizontal, Trash2 } from 'lucide-react'
import React, { useEffect, useMemo, useRef, useState } from 'react'
import type { DragState } from '../dnd-types'
import FieldRenderer from '../field-renderer'
import { FilePreview } from './file-preview'
import { useUploadStore } from '@/ui/stores/upload'
import { ProgressCircle } from '@/ui/components/ui/progress-circle'
import { FieldType, type FieldInfo as MetadataFieldInfo } from '@shumai/dtos'
import { FIELD_TYPE_ICONS } from '../fields-manager'
@@ -282,7 +282,7 @@ export function FileCard({
</div>
<div className="flex items-center justify-between gap-2 p-3">
<div className="min-w-0 flex-1">
<div className="min-w-0 flex-1 line-clamp-3 h-[3lh]">
<EditableText
ref={inputRef}
value={name}
@@ -1,13 +1,18 @@
import type { AssetInfo, AssetInfoPaginatedList } from '@shumai/dtos'
import type { CollectionInfo } from '@shumai/dtos'
import { type FieldInfo as MetadataFieldInfo } from '@shumai/dtos'
import type { SearchCondition, SearchSort } from '@shumai/dtos'
import { client } from '@/ui/api/client'
import { usePermissions } from '@/ui/hooks/use-permissions'
import { m } from '@/ui/paraglide/messages.js'
import type {
AssetInfo,
AssetInfoPaginatedList,
CollectionInfo,
SearchCondition,
SearchSort,
} from '@shumai/dtos'
import { type FieldInfo as MetadataFieldInfo } from '@shumai/dtos'
import { useMutation } from '@tanstack/react-query'
import { InferRequestType, InferResponseType } from 'hono/client'
import { m } from '@/ui/paraglide/messages.js'
import { useChatbotStore } from '@/ui/stores/chatbot'
import { useFieldStore } from '@/ui/stores/fields'
import { useMemberStore } from '@/ui/stores/members'
import { useTopNavStore } from '@/ui/stores/top-nav'
@@ -19,6 +24,7 @@ import { useInfiniteQuery, useQuery, useQueryClient } from '@tanstack/react-quer
import { useNavigate } from '@tanstack/react-router'
import { useEffect, useMemo, useRef, useState } from 'react'
import { cn } from '../lib/utils'
import { ChatbotSidebar } from './chatbot-sidebar'
import { SnapToPointer } from './dnd-modifiers'
import { FileBrowser } from './file-browser/file-browser'
import { FileViewerRightSidebar } from './file-viewer-right-sidebar'
@@ -87,6 +93,7 @@ export default function FileSystemManager({
const [lastSelectedId, setLastSelectedId] = useState<string | null>(null)
const [leftSidebarWidth, setLeftSidebarWidth] = useState(240)
const [rightSidebarWidth, setRightSidebarWidth] = useState(360)
const { isChatbotOpen } = useChatbotStore()
const { members, fetchMembers } = useMemberStore()
const { metadata, fetchMetadata, setMetadata: setUserMetadata } = useUserMetadataStore()
@@ -433,10 +440,13 @@ export default function FileSystemManager({
onClick={() => setIsLeftSidebarCollapsed(true)}
/>
)}
{!isRightSidebarCollapsed && (
{(!isRightSidebarCollapsed || isChatbotOpen) && (
<div
className="md:hidden fixed inset-0 bg-black/50 z-40"
onClick={() => setIsRightSidebarCollapsed(true)}
onClick={() => {
setIsRightSidebarCollapsed(true)
useChatbotStore.getState().setIsChatbotOpen(false)
}}
/>
)}
@@ -495,7 +505,7 @@ export default function FileSystemManager({
rootFolderId={rootFolderId}
/>
{!isRightSidebarCollapsed && (
{(!isRightSidebarCollapsed || isChatbotOpen) && (
<>
<ResizeHandle
onResize={(delta) => {
@@ -505,9 +515,11 @@ export default function FileSystemManager({
/>
<div
style={{ width: rightSidebarWidth }}
className="bg-background border-l border-border flex flex-col flex-shrink-0"
className="bg-background flex flex-col flex-shrink-0"
>
{singleSelectedFile ? (
{isChatbotOpen ? (
<ChatbotSidebar projectId={projectId} contextAssetId={assetId} />
) : singleSelectedFile ? (
<FileViewerRightSidebar
teamId={teamId}
projectId={projectId}
@@ -21,11 +21,12 @@ import {
import { Switch } from '@/ui/components/ui/switch'
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { useForm, useStore } from '@tanstack/react-form'
import { Loader2, Puzzle } from 'lucide-react'
import { Loader2, Puzzle, Terminal } from 'lucide-react'
import { useEffect, useMemo } from 'react'
import { m } from '@/ui/paraglide/messages.js'
import { toast } from 'sonner'
import { AgentInfo, AgentType, ThinkingLevel, thinkingLevelSchema } from '@shumai/dtos'
import { usePermissions } from '@/ui/hooks/use-permissions'
import { Textarea } from '@/ui/components/ui/textarea'
import { ScrollArea } from '@/ui/components/ui/scroll-area'
import { z } from 'zod'
@@ -51,8 +52,52 @@ const agentFormSchema = z.object({
thinkingLevel: thinkingLevelSchema.optional(),
systemPrompt: z.string().optional(),
skills: z.array(z.string()).optional(),
deniedTools: z.array(z.string()).optional(),
})
const BUILTIN_TOOLS = [
{
id: 'bash',
name: () => m.agent_tool_bash_name(),
description: () => m.agent_tool_bash_desc(),
},
{
id: 'read_skill',
name: () => m.agent_tool_read_skill_name(),
description: () => m.agent_tool_read_skill_desc(),
},
{
id: 'analyze_image',
name: () => m.agent_tool_analyze_image_name(),
description: () => m.agent_tool_analyze_image_desc(),
},
{
id: 'screenshot',
name: () => m.agent_tool_screenshot_name(),
description: () => m.agent_tool_screenshot_desc(),
},
{
id: 'list_assets',
name: () => m.agent_tool_list_assets_name(),
description: () => m.agent_tool_list_assets_desc(),
},
{
id: 'create_folder',
name: () => m.agent_tool_create_folder_name(),
description: () => m.agent_tool_create_folder_desc(),
},
{
id: 'create_file',
name: () => m.agent_tool_create_file_name(),
description: () => m.agent_tool_create_file_desc(),
},
{
id: 'create_version',
name: () => m.agent_tool_create_version_name(),
description: () => m.agent_tool_create_version_desc(),
},
]
type AgentFormValues = z.infer<typeof agentFormSchema>
export function AgentFormDialog({
@@ -64,6 +109,7 @@ export function AgentFormDialog({
title,
}: AgentFormDialogProps) {
const queryClient = useQueryClient()
const { canAdmin } = usePermissions()
// Fetch Providers
const { data: providers } = useQuery({
@@ -125,6 +171,7 @@ export function AgentFormDialog({
thinkingLevel: initialValues?.thinkingLevel || 'off',
systemPrompt: initialValues?.systemPrompt || '',
skills: initialValues?.skills?.map((s) => s.skillId) || ([] as string[]),
deniedTools: initialValues?.deniedTools || ([] as string[]),
} as AgentFormValues,
validators: {
onChange: schema,
@@ -510,6 +557,67 @@ export function AgentFormDialog({
/>
</div>
</div>
<div className="space-y-4 border-t border-border pt-6">
<Label className="text-base font-bold flex items-center gap-2">
<Terminal className="w-5 h-5 text-primary" />
{m.agent_tools()}
</Label>
<p className="text-xs text-muted-foreground">{m.agent_tools_description()}</p>
<div className="space-y-2">
<form.Field
name="deniedTools"
children={(deniedToolsField) => {
const currentDenied = deniedToolsField.state.value || []
return (
<>
{BUILTIN_TOOLS.map((tool) => {
const isEnabled = !currentDenied.includes(tool.id)
return (
<div
key={tool.id}
className="flex items-center justify-between p-3 bg-muted/30 border border-border rounded-lg"
>
<div className="flex items-center gap-3">
<Terminal className="w-4 h-4 text-muted-foreground" />
<div className="flex flex-col">
<span className="text-sm font-medium">{tool.name()}</span>
<span className="text-[10px] text-muted-foreground line-clamp-1">
{tool.description()}
</span>
</div>
</div>
<div className="flex items-center gap-2">
<span className="text-xs text-muted-foreground">
{isEnabled ? m.enabled() : m.disabled()}
</span>
<Switch
checked={isEnabled}
disabled={!canAdmin}
onCheckedChange={(checked) => {
if (checked) {
deniedToolsField.handleChange(
currentDenied.filter((id) => id !== tool.id),
)
} else {
deniedToolsField.handleChange([
...currentDenied,
tool.id,
])
}
}}
/>
</div>
</div>
)
})}
</>
)
}}
/>
</div>
</div>
</>
)}
</div>
@@ -115,6 +115,7 @@ export function AgentsSettings({ teamId }: AgentsSettingsProps) {
systemPrompt?: string
soul?: string
skills?: string[]
deniedTools?: string[]
}) => {
const res = await client.api.agents[':agentId'].$put({
param: { agentId: params.agentId },
@@ -129,6 +130,7 @@ export function AgentsSettings({ teamId }: AgentsSettingsProps) {
systemPrompt: params.systemPrompt,
soul: params.soul,
skills: params.skills,
deniedTools: params.deniedTools,
},
})
if (!res.ok) throw new Error('failed to update agent')
@@ -213,6 +215,7 @@ export function AgentsSettings({ teamId }: AgentsSettingsProps) {
<Loader2 className="w-4 h-4 animate-spin text-muted-foreground" />
</div>
)}
{AGENT_TYPES.map((section) => {
const typeAgents = agents.filter((a) => a.type === section.type)
const Icon = section.icon
@@ -322,6 +325,7 @@ export function AgentsSettings({ teamId }: AgentsSettingsProps) {
systemPrompt: agent.systemPrompt,
soul: agent.soul,
skills: agent.skills?.map((s) => s.skillId),
deniedTools: agent.deniedTools,
})
}}
/>
+30 -8
View File
@@ -2,10 +2,12 @@ import { useTopNavStore } from '@/ui/stores/top-nav'
import { BreadcrumbNav } from './breadcrumb-nav'
import { ShumaiLogo } from '@/ui/components/ui/icons'
import { useUiStore } from '@/ui/stores/ui'
import { useChatbotStore } from '@/ui/stores/chatbot'
export function TopNav() {
const { projectState } = useTopNavStore()
const uiStore = useUiStore()
const { isChatbotOpen, setIsChatbotOpen } = useChatbotStore()
if (!projectState) {
return <TeamHeader />
@@ -58,14 +60,32 @@ export function TopNav() {
const onRightSidebarToggle =
isPublic && projectState.onRightSidebarToggle
? projectState.onRightSidebarToggle
: () =>
isFileView
? uiStore.setFileViewRightSidebarCollapsed(!uiStore.fileViewRightSidebarCollapsed)
: isShareConfig
? uiStore.setShareConfigRightSidebarCollapsed(
!uiStore.shareConfigRightSidebarCollapsed,
)
: uiStore.setFileListRightSidebarCollapsed(!uiStore.fileListRightSidebarCollapsed)
: () => {
if (!isPublic && isChatbotOpen) {
setIsChatbotOpen(false)
if (isFileView) {
uiStore.setFileViewRightSidebarCollapsed(false)
} else if (isShareConfig) {
uiStore.setShareConfigRightSidebarCollapsed(false)
} else {
uiStore.setFileListRightSidebarCollapsed(false)
}
} else {
if (isFileView) {
uiStore.setFileViewRightSidebarCollapsed(!uiStore.fileViewRightSidebarCollapsed)
} else if (isShareConfig) {
uiStore.setShareConfigRightSidebarCollapsed(!uiStore.shareConfigRightSidebarCollapsed)
} else {
uiStore.setFileListRightSidebarCollapsed(!uiStore.fileListRightSidebarCollapsed)
}
}
}
const onChatbotToggle = isPublic
? undefined
: () => {
setIsChatbotOpen(!isChatbotOpen)
}
const displayStyle = projectId ? (uiStore.viewModes[projectId] ?? 'card') : 'card'
const onDisplayStyleChange = isPublic
@@ -88,6 +108,8 @@ export function TopNav() {
onLeftSidebarToggle={onLeftSidebarToggle}
isRightSidebarCollapsed={isRightSidebarCollapsed}
onRightSidebarToggle={onRightSidebarToggle}
isChatbotOpen={!isPublic && isChatbotOpen}
onChatbotToggle={onChatbotToggle}
isPublic={isPublic}
shareId={shareId}
fileId={fileId}
@@ -14,8 +14,8 @@ const EditableText = React.forwardRef<HTMLInputElement, EditableTextProps>(
className={cn(
'h-auto min-h-0 w-full rounded-sm border px-1 py-0.5 text-base shadow-none transition-all outline-none focus-visible:ring-0',
!disabled
? 'border-blue-500 bg-muted/10 ring-1 ring-blue-500'
: 'border-transparent bg-transparent cursor-default opacity-100 focus-visible:border-transparent focus-visible:ring-0 caret-transparent select-none',
? 'border-blue-500 !bg-muted/10 ring-1 ring-blue-500'
: 'border-transparent !bg-transparent cursor-default opacity-100 focus-visible:border-transparent focus-visible:ring-0 caret-transparent select-none',
className,
)}
onClick={(e) => {
+28
View File
@@ -55,6 +55,34 @@ export const DockToRightFilled: React.FC<IconProps> = (props) => (
</svg>
)
/**
* Icon: Bot (Filled)
* A filled version of the Bot icon, useful when the chatbot sidebar is active.
*/
export const BotFilled: React.FC<IconProps> = (props) => (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
{...props}
>
<path d="M12 8V4H8" />
<path
d="M6 7H18A3 3 0 0 1 21 10V18A3 3 0 0 1 18 21H6A3 3 0 0 1 3 18V10A3 3 0 0 1 6 7ZM8 13V15A1 1 0 0 0 10 15V13A1 1 0 0 0 8 13ZM14 13V15A1 1 0 0 0 16 15V13A1 1 0 0 0 14 13Z"
fill="currentColor"
stroke="none"
/>
<path d="M2 14h2" />
<path d="M20 14h2" />
</svg>
)
export const NotificationFillIcon: React.FC<React.SVGProps<SVGSVGElement>> = (props) => (
<svg
xmlns="http://www.w3.org/2000/svg"
@@ -9,6 +9,7 @@ import { useState } from 'react'
import { toast } from 'sonner'
import type { DragEndEvent, DragStartEvent } from '@dnd-kit/react'
import type { DragState } from './dnd-types'
import { useChatbotStore } from '@/ui/stores/chatbot'
interface UseFileSystemDndProps {
teamId: string
@@ -188,6 +189,23 @@ export function useFileSystemDnd({
const targetId = (target.id as string).split(':').pop()!
const targetData = target.data as { type: string; item: AssetInfo } | undefined
if (targetData?.type === 'chatbot-sidebar') {
const draggedItems = [...folders, ...files].filter((item) =>
dragState.draggedIds.has(item.id!),
)
const chatAssets = draggedItems.map((item) => ({
id: item.id!,
name: item.name || 'Unnamed',
type: item.type as 'file' | 'folder',
}))
useChatbotStore.getState().addAssets(chatAssets)
onClearSelection()
setDragState(undefined)
return
}
if (targetData?.type === 'share-link') {
const shareId = targetId
const assetIds = Array.from(dragState.draggedIds)
@@ -194,7 +194,7 @@ export const CompareImagePane = forwardRef<ComparePaneHandle, CompareImagePanePr
return (
<div
ref={containerRef}
className="relative flex-1 overflow-hidden bg-gray-100 dark:bg-gray-950"
className="relative flex-1 overflow-hidden bg-black"
onMouseDown={onActivate}
>
<DrawingCanvas
@@ -186,8 +186,13 @@ export function useFramePlayer(
}
}
const handleEnded = () => {
setCurrentFrame(totalFrames - 1)
}
video.addEventListener('play', handlePlay)
video.addEventListener('pause', handlePause)
video.addEventListener('ended', handleEnded)
// If it's already playing when this effect runs
if (!video.paused) {
@@ -199,6 +204,7 @@ export function useFramePlayer(
active = false
video.removeEventListener('play', handlePlay)
video.removeEventListener('pause', handlePause)
video.removeEventListener('ended', handleEnded)
if (rVfcId !== null) {
const videoWithCallback = video as unknown as HtmlVideoElementWithCallback
if (videoWithCallback.cancelVideoFrameCallback) {
@@ -413,6 +413,47 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
}
}, [])
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
const activeEl = document.activeElement
if (
activeEl &&
(activeEl.tagName === 'INPUT' ||
activeEl.tagName === 'TEXTAREA' ||
(activeEl instanceof HTMLElement && activeEl.isContentEditable))
) {
return
}
const isSpace = e.key === ' '
const isInteractive =
activeEl &&
(activeEl.tagName === 'BUTTON' ||
activeEl.tagName === 'A' ||
activeEl.tagName === 'SELECT' ||
activeEl.tagName === 'OPTION' ||
activeEl.getAttribute('role') === 'button' ||
activeEl.getAttribute('role') === 'link' ||
activeEl.getAttribute('role') === 'checkbox' ||
activeEl.getAttribute('role') === 'radio' ||
activeEl.getAttribute('role') === 'menuitem')
if (isSpace && isInteractive) {
return
}
if (e.key === ' ' || e.key.toLowerCase() === 'k') {
e.preventDefault()
togglePlay()
}
}
window.addEventListener('keydown', handleKeyDown)
return () => {
window.removeEventListener('keydown', handleKeyDown)
}
}, [togglePlay])
const toggleLoop = () => {
const player = playerRef.current
if (!player) return
+25
View File
@@ -167,3 +167,28 @@
.no-scrollbar::-webkit-scrollbar {
display: none; /* Chrome, Safari and Opera */
}
/* Style code blocks and inline code inside .prose (rendered markdown) */
.prose pre {
background-color: var(--muted);
color: var(--foreground);
padding: 1rem;
border-radius: var(--radius-md);
border: 1px solid var(--border);
font-family: var(--font-mono);
font-size: 0.875em;
overflow-x: auto;
}
.prose code {
font-family: var(--font-mono);
}
/* Style inline code (code not inside pre) */
.prose :not(pre) > code {
background-color: var(--muted);
color: var(--foreground);
padding: 0.15rem 0.3rem;
border-radius: var(--radius-sm);
font-size: 0.875em;
}
+42 -1
View File
@@ -6,6 +6,7 @@
"language": "Language",
"personal_info": "Personal Info",
"language_settings": "Language Settings",
"assets_added_to_context": "Assets added to context:",
"select_preferred_language": "Select your preferred display language.",
"settings": "Settings",
"welcome_back": "Welcome Back",
@@ -378,6 +379,24 @@
"thinking_level": "Thinking Level",
"agent_soul": "Agent Soul (Personality)",
"agent_soul_placeholder": "Describe the agent's personality, tone, and behavior...",
"agent_tools": "Tools",
"agent_tools_description": "Configure which tools the agent is permitted to use.",
"agent_tool_bash_name": "Sandboxed Terminal",
"agent_tool_bash_desc": "Allows the agent to run terminal/shell commands in a highly isolated environment.",
"agent_tool_read_skill_name": "Read Skills",
"agent_tool_read_skill_desc": "Allows the agent to load and read skill definitions to perform specialized actions.",
"agent_tool_analyze_image_name": "Analyze Image",
"agent_tool_analyze_image_desc": "Allows the agent to view and analyze the visual contents of an image file.",
"agent_tool_screenshot_name": "Video Frame Extraction",
"agent_tool_screenshot_desc": "Allows the agent to extract still image frames from video files for analysis.",
"agent_tool_list_assets_name": "Browse Shumai",
"agent_tool_list_assets_desc": "Allows the agent to list folders and browse files in Shumai.",
"agent_tool_create_folder_name": "Create Folder",
"agent_tool_create_folder_desc": "Allows the agent to create new folders in Shumai.",
"agent_tool_create_file_name": "Create File",
"agent_tool_create_file_desc": "Allows the agent to create and upload new files to Shumai.",
"agent_tool_create_version_name": "Create Version",
"agent_tool_create_version_desc": "Allows the agent to upload and stack a new version of an existing file in Shumai.",
"enabled": "Enabled",
"disabled": "Disabled",
"no_skills_available": "No skills available.",
@@ -819,5 +838,27 @@
"created_by_at": "{time} by {author}",
"date_format_long": "MMMM d, yyyy",
"tool_result_with_name": "Tool Result: {name}",
"reply_to_message": "reply to: {message}"
"reply_to_message": "reply to: {message}",
"chatbot_context": "Chatbot Context",
"clear": "Clear",
"chatbot_drag_drop_hint": "Drag and drop files or folders here to add them to chatbot context",
"context_assets_count": "Context Assets ({count})",
"remove_from_context": "Remove from context",
"hide_chatbot": "Hide Chatbot",
"show_chatbot": "Show Chatbot",
"created_by_agent": "Created by Agent",
"new_chat": "New Chat",
"shumai_agent": "Shumai Agent",
"history": "History",
"send": "Send",
"no_history_sessions": "No chat history found.",
"delete_session_confirm": "Are you sure you want to delete this chat session?",
"configure_chatbot_agent_first": "Please configure a chatbot agent in team settings first.",
"global_chatbot": "Global Chatbot",
"global_chatbot_description": "Select the agent that handles conversation inside the global chatbot sidebar.",
"select_chatbot_agent": "Select chatbot agent",
"no_chat_agents_warning": "No chat agents found. Please create a Chat Agent below first.",
"select_agent": "Select Agent",
"delete_comment_confirm": "Delete Comment?",
"delete_comment_description": "Are you sure you want to delete this comment? This action cannot be undone."
}
+42 -1
View File
@@ -6,6 +6,7 @@
"language": "语言",
"personal_info": "个人信息",
"language_settings": "语言设置",
"assets_added_to_context": "已添加至上下文的资源:",
"select_preferred_language": "选择您首选的显示语言。",
"settings": "设置",
"welcome_back": "欢迎回来",
@@ -378,6 +379,24 @@
"thinking_level": "思考级别",
"agent_soul": "智能体灵魂(个性)",
"agent_soul_placeholder": "描述智能体的个性、语调和行为...",
"agent_tools": "工具",
"agent_tools_description": "配置允许智能体使用的工具。",
"agent_tool_bash_name": "沙箱终端",
"agent_tool_bash_desc": "允许智能体在高度隔离的环境中运行终端/命令行命令。",
"agent_tool_read_skill_name": "读取技能",
"agent_tool_read_skill_desc": "允许智能体加载和读取技能定义以执行特定操作。",
"agent_tool_analyze_image_name": "分析图片",
"agent_tool_analyze_image_desc": "允许智能体查看和分析图片文件的视觉内容。",
"agent_tool_screenshot_name": "提取视频帧",
"agent_tool_screenshot_desc": "允许智能体从视频文件中提取静态画面进行分析。",
"agent_tool_list_assets_name": "浏览 Shumai",
"agent_tool_list_assets_desc": "允许智能体列出文件夹并浏览 Shumai 中的文件。",
"agent_tool_create_folder_name": "创建文件夹",
"agent_tool_create_folder_desc": "允许智能体在 Shumai 中创建新文件夹。",
"agent_tool_create_file_name": "创建文件",
"agent_tool_create_file_desc": "允许智能体创建新文件并上传到 Shumai。",
"agent_tool_create_version_name": "创建版本",
"agent_tool_create_version_desc": "允许智能体在 Shumai 中为已有文件上传并叠加新版本。",
"enabled": "已启用",
"disabled": "已禁用",
"no_skills_available": "暂无可用技能。",
@@ -819,5 +838,27 @@
"created_by_at": "{time} 由 {author} 创建",
"date_format_long": "yyyy年M月d日",
"tool_result_with_name": "工具结果:{name}",
"reply_to_message": "回复:{message}"
"reply_to_message": "回复:{message}",
"chatbot_context": "聊天机器人上下文",
"clear": "清空",
"chatbot_drag_drop_hint": "拖拽文件或文件夹到此处以添加到聊天机器人上下文",
"context_assets_count": "上下文资源 ({count})",
"remove_from_context": "从上下文中移除",
"hide_chatbot": "隐藏聊天机器人",
"show_chatbot": "显示聊天机器人",
"created_by_agent": "由智能体创建",
"new_chat": "开启新会话",
"shumai_agent": "数脉智能体",
"history": "历史记录",
"send": "发送",
"no_history_sessions": "暂无历史会话记录",
"delete_session_confirm": "您确定要删除此会话记录吗?",
"configure_chatbot_agent_first": "请先在团队设置中配置聊天机器人智能体。",
"global_chatbot": "全局聊天机器人",
"global_chatbot_description": "选择在全局聊天机器人边栏中进行对话的智能体。",
"select_chatbot_agent": "选择聊天机器人智能体",
"no_chat_agents_warning": "未找到聊天智能体。请先在下方创建聊天智能体。",
"select_agent": "选择智能体",
"delete_comment_confirm": "删除评论?",
"delete_comment_description": "您确定要删除这条评论吗?此操作将无法撤回。"
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/webui",
"version": "0.1.7",
"version": "0.1.10",
"private": true,
"type": "module",
"main": "./index.html",
+40
View File
@@ -4,6 +4,7 @@ export * from "./uploads.js";
export * from "./language.js";
export * from "./personal_info.js";
export * from "./language_settings.js";
export * from "./assets_added_to_context.js";
export * from "./select_preferred_language.js";
export * from "./settings.js";
export * from "./welcome_back.js";
@@ -360,6 +361,24 @@ export * from "./current_custom_avatar.js";
export * from "./thinking_level.js";
export * from "./agent_soul.js";
export * from "./agent_soul_placeholder.js";
export * from "./agent_tools.js";
export * from "./agent_tools_description.js";
export * from "./agent_tool_bash_name.js";
export * from "./agent_tool_bash_desc.js";
export * from "./agent_tool_read_skill_name.js";
export * from "./agent_tool_read_skill_desc.js";
export * from "./agent_tool_analyze_image_name.js";
export * from "./agent_tool_analyze_image_desc.js";
export * from "./agent_tool_screenshot_name.js";
export * from "./agent_tool_screenshot_desc.js";
export * from "./agent_tool_list_assets_name.js";
export * from "./agent_tool_list_assets_desc.js";
export * from "./agent_tool_create_folder_name.js";
export * from "./agent_tool_create_folder_desc.js";
export * from "./agent_tool_create_file_name.js";
export * from "./agent_tool_create_file_desc.js";
export * from "./agent_tool_create_version_name.js";
export * from "./agent_tool_create_version_desc.js";
export * from "./enabled.js";
export * from "./disabled.js";
export * from "./no_skills_available.js";
@@ -767,4 +786,25 @@ export * from "./created_by_at.js";
export * from "./date_format_long.js";
export * from "./tool_result_with_name.js";
export * from "./reply_to_message.js";
export * from "./chatbot_context.js";
export * from "./clear.js";
export * from "./chatbot_drag_drop_hint.js";
export * from "./context_assets_count.js";
export * from "./remove_from_context.js";
export * from "./hide_chatbot.js";
export * from "./show_chatbot.js";
export * from "./new_chat.js";
export * from "./shumai_agent.js";
export * from "./history.js";
export * from "./send.js";
export * from "./no_history_sessions.js";
export * from "./delete_session_confirm.js";
export * from "./configure_chatbot_agent_first.js";
export * from "./global_chatbot.js";
export * from "./global_chatbot_description.js";
export * from "./select_chatbot_agent.js";
export * from "./no_chat_agents_warning.js";
export * from "./select_agent.js";
export * from "./delete_comment_confirm.js";
export * from "./delete_comment_description.js";
export type LocalizedString = import("../runtime.js").LocalizedString;

Some files were not shown because too many files have changed in this diff Show More