Compare commits

..
23 Commits
Author SHA1 Message Date
yiling 52330869a2 Release v0.4.2 2026-09-03 21:49:37 +08:00
Yiling-J 0c561e9b39 refactor(webui): use semantic system colors in notification settings (#379) 2026-09-03 21:36:28 +08:00
Yiling-J a236cd3a97 feat(provider): add provider and model sync feature with vendored generator (#378)
* feat(provider): vendor and adapt pi model generator

- Vendor generate-models.ts, model-data.ts, and models-dev-reasoning-options.ts from @earendil-works/pi tag v0.82.1
- Adapt imports to use @earendil-works/pi-ai and expose generateModels programmatic function
- Add compare.test.ts to verify vendored generator against original built-in catalog
- Add packages/core/src/provider/generator to eslint ignores

* feat(provider): add provider and model sync api and ui

* feat(webui): add checkbox for existing providers and clean dialog description

* feat(webui): add expand all and collapse all to sync providers dialog

* refactor(provider): remove compare.test.ts and cli overhead from generate-models

* fix(provider): add 30s timeout and strict error handling to sync catalog fetching

* fix(e2e): eliminate drag-and-drop race condition on webkit

Allow requestAnimationFrame collision updates to settle in dragCard helper before releasing pointer, and scope fileCard locator via data-testid to avoid matching sidebar tree items.
2026-09-03 21:01:14 +08:00
Yiling-J c3ef769f51 feat(provider): optimize provider and model configuration management (#377)
* feat(provider): optimize provider and model configuration management

* style(provider): use default tabs variant in provider settings dialog

* style(provider): make tabs list full width

* feat(provider): make model card row clickable and remove pencil edit icon

* feat(settings): persist active settings tab in url hash

* feat(agent): add search box and alphabetical sorting for provider and model selectors

* refactor(providers): remove frontend pagination and display all models in scrollable list

* fix(providers): address code review findings for model editing, i18n, and api protocol propagation
2026-09-03 15:16:16 +08:00
Yiling-J e58dfaa71a feat(agent): consolidate media tools into read_asset and add S3 key support to download_asset (#376)
* feat(agent): consolidate media tools into read_asset and add S3 key support to download_asset

* fix(agent): refine parameter descriptions and remove download URL from read_asset text content

* fix(agent): resolve owning asset and enforce RBAC permissions when downloading by S3 key
2026-09-02 21:51:07 +08:00
Yiling-J 35b57cc0ab feat(agent): support dynamic annotationId resolution across chat and comments (#375)
* feat(agent): improve timestamp precision and proxy selection for video screenshots

* feat(agent): support dynamic annotationId resolution across chat and comments

* fix(agent): scope annotation resolution to asset and format has_markup consistently
2026-09-02 15:17:56 +08:00
Yiling-J 5a754bcb94 feat(agent): improve timestamp precision and proxy selection for video screenshots (#374) 2026-09-01 21:15:28 +08:00
Yiling-J 64b68e7d63 feat(agent): move thread metadata to structured context XML (#373)
* feat(agent): move thread metadata to structured context XML

* feat(core): dynamically inject thread metadata in getSessionEntries
2026-09-01 17:42:56 +08:00
Yiling-J c2f477a391 fix(agent): hide main comment sessions in dashboard sessions list (#372)
* fix(agent): hide main comment sessions in dashboard sessions list

* feat(webui): auto mention comment creator when replying
2026-09-01 16:20:58 +08:00
Yiling-J 9263971653 更新 README_CN.md 以包含 Docker Compose 部署指南
添加了使用 Docker Compose 部署 Shumai 的详细说明和提示词。
2026-09-01 10:25:57 +08:00
Yiling-J 79a479c87c fix(webui): strip styles and preserve multiline text on paste in chat input (#371)
* fix(webui): strip styles and preserve multiline text on paste in chat input

* fix(db): remove custom log wait strategy in test global setup to prevent postgres startup race condition
2026-08-31 21:29:00 +08:00
Yiling-J 134eb8b7fa fix(agent): set empty details in asset tool results to avoid data duplication (#370) 2026-08-31 20:43:05 +08:00
Yiling-J e06478df88 feat(chat): support markups, attachments, and timestamps in agent 1-to-1 chat (#368)
* feat(chat): support markups, attachments, and timestamps in agent 1-to-1 chat

* feat(chat): improve message card layout with inline badges and integrated assets

* fix(chat): enable image attachment thumbnails in 1-to-1 chat cards

* fix(chat): address code review feedback on input state, mentions, i18n, and session deletion

* fix(chat): replace ring with border color on selected message card
2026-08-31 20:11:27 +08:00
yiling 6e9cf8dfb6 Release v0.4.1 2026-08-30 20:26:57 +08:00
Yiling-J d2620f6911 fix(webui): prevent empty folder flash during initial loading (#367) 2026-08-30 18:43:23 +08:00
Yiling-J 4537baa6a2 feat(webui): redesign comment attachment display into compact rows (#366)
* feat(webui): redesign comment attachment display into compact rows

* feat(webui): support pure frontend image extension matching and self-contained preview dialog

* fix(webui): size image preview dialog to min(image size, 80% screen size)

* fix(webui): prevent horizontal overflow of comment attachment rows in scroll areas

* fix(webui): centralize attachment image preview ownership and localize download title
2026-08-30 17:16:20 +08:00
yiling 810e0c4cf5 fix(agent): only include duration_seconds for time-based media (video/audio) 2026-08-30 15:51:23 +08:00
yiling 07cb648183 fix(agent): use total_frames for video and total_pages for pdf in asset context 2026-08-30 15:44:59 +08:00
yiling c6602c9f1c docs(readme): add docker pulls, release, and license badges 2026-08-30 15:32:27 +08:00
Yiling-J 86032539ed feat(agent): refactor message context into unified shumai_message (#365)
* feat(agent): refactor message context into unified shumai_message

* fix(agent): persist custom_message entry type in DatabaseSessionStorage and handle message mapping defensively

* fix(agent): pass clean prompt to harness and replace optimistic messages on shumai_message stream

* fix(agent): align asset context DTO contract, sync comment attachments, and fix session stats
2026-08-30 15:25:13 +08:00
Yiling-J b2ec012e10 feat(webui): add manage versions option to file card context menu (#364) 2026-08-29 19:19:49 +08:00
Yiling-J 7fd8623684 fix(asset,webui): disallow dropping version stacks onto files and version stacks (#363)
* fix(asset,webui): disallow dropping version stacks onto files and version stacks

* fix(webui): derive isSingleFile from total drag selection and remove unused hasVersionStacks
2026-08-29 16:09:36 +08:00
Yiling-J 083753a96d refactor(agent): remove executeAgentToolWorkflow and invoke core services directly (#362)
* refactor(agent): remove executeAgentToolWorkflow and invoke core services directly

* fix(core): ensure creation atomicity, strict assetType filtering, and id-desc listing
2026-08-29 15:19:08 +08:00
260 changed files with 17468 additions and 4956 deletions
+6
View File
@@ -8,6 +8,12 @@
<strong>An open-source platform for all your creative work.</strong>
</p>
<p align="center">
<a href="https://hub.docker.com/r/shumaione/shumai"><img alt="Docker pulls" src="https://img.shields.io/docker/pulls/shumaione/shumai" /></a>
<a href="https://github.com/shumaiOne/shumai/releases"><img alt="GitHub latest release" src="https://img.shields.io/github/v/release/shumaiOne/shumai" /></a>
<a href="LICENSE"><img alt="License" src="https://img.shields.io/badge/license-MIT-blue" /></a>
</p>
<p align="center">
<a target="_blank" href="https://shumai.one/">Website</a> | <a target="_blank" href="https://docs.shumai.one/getting-started/overview">Docs</a> | <a target="_blank" href="https://staging.shumai.one">Demo</a> | <a target="_blank" href="https://discord.gg/sUzhujK6DD">Discord</a> | <a target="_blank" href="README_CN.md">中文</a>
</p>
+24 -1
View File
@@ -9,8 +9,13 @@
</p>
<p align="center">
<a target="_blank" href="https://shumai.one/">官方网站</a> | <a target="_blank" href="https://docs.shumai.one/getting-started/overview">文档</a> | <a target="_blank" href="https://staging.shumai.one">在线演示</a> | <a target="_blank" href="README.md">English</a>
<a href="https://hub.docker.com/r/shumaione/shumai"><img alt="Docker pulls" src="https://img.shields.io/docker/pulls/shumaione/shumai" /></a>
<a href="https://github.com/shumaiOne/shumai/releases"><img alt="GitHub latest release" src="https://img.shields.io/github/v/release/shumaiOne/shumai" /></a>
<a href="LICENSE"><img alt="License" src="https://img.shields.io/badge/license-MIT-blue" /></a>
</p>
<p align="center">
<a target="_blank" href="https://shumai.one/">官方网站</a> | <a target="_blank" href="https://docs.shumai.one/getting-started/overview">文档</a> | <a target="_blank" href="https://staging.shumai.one">在线演示</a> | <a target="_blank" href="README.md">English</a>
</p>
![Shumai 应用截图](docs/screenshot.webp)
@@ -64,6 +69,24 @@ Shumai 也将项目管理融入了创意工作流程。 内置的看板可以帮
以下是使用本地存储运行 Shumai 的快速上手指南。如需了解高级配置(包括 S3 兼容存储配置、Temporal 工作流编排等),请参阅我们的[官方文档](https://docs.shumai.one/getting-started/overview)。
### 方案一:使用 Docker Compose(推荐)
<details>
<summary>复制这个提示词,让agent帮你安装</summary>
```
请帮我在本机使用 Docker Compose 一键部署 Shumai。
请严格参考官方文档:
https://gh-proxy.com/https://raw.githubusercontent.com/shumaiOne/shumai/main/README_CN.md
要求:
1. 只能使用 Docker Compose local方式部署,不要使用其他部署方式。
2. 部署前请先检查本机是否已安装并可正常使用 Docker 和 Docker Compose。
3. 如果本机未安装 Docker,请立即提醒用户先安装 Docker,并停止后续部署操作。不要自行安装 Docker,也不要采用其他方式部署。
4. 获取 `docker-compose-aliyun.yaml` 文件内容时,必须通过 `https://gh-proxy.com` 加速访问,不要直接访问 GitHub Raw 地址。
```
</details>
使用 Docker Compose 是运行 Shumai 最快的方式。您无需克隆仓库,也不需要手动安装依赖包。请确保电脑上已安装 [Docker](https://docs.docker.com/get-docker/) 和 [Docker Compose](https://docs.docker.com/compose/install/),然后按照以下步骤操作:
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/agent-app",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"bin": {
-1
View File
@@ -1,5 +1,4 @@
export * from '@shumai/agent/src/workflows/agent-autofill'
export * from '@shumai/agent/src/workflows/agent-chat'
export * from '@shumai/agent/src/workflows/agent-embedding'
export * from '@shumai/agent/src/workflows/agent-tool-call'
export * from '@shumai/agent/src/workflows/query-embedding-for-search'
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/cli",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"bin": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/transcode-app",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"bin": {
+22 -4
View File
@@ -24,14 +24,32 @@ export async function dragCard(page: Page, source: Locator, target: Locator): Pr
await page.mouse.move(fromX, fromY)
await page.mouse.down()
// Move in several chunks so the dnd-kit sensor activates mid-drag
const dx = toX - fromX
const dy = toY - fromY
const dist = Math.hypot(dx, dy)
// Step past the 10px activation constraint to trigger drag activation
// and give the browser a brief moment for status to become 'dragging'
const activationRatio = dist > 0 ? Math.min(1, 15 / dist) : 1
await page.mouse.move(fromX + dx * activationRatio, fromY + dy * activationRatio)
await page.waitForTimeout(50)
// Move in several chunks so the intermediate dragmove handlers run
const chunks = 8
for (let i = 1; i <= chunks; i++) {
const x = fromX + ((toX - fromX) * i) / chunks
const y = fromY + ((toY - fromY) * i) / chunks
const x = fromX + (dx * i) / chunks
const y = fromY + (dy * i) / chunks
await page.mouse.move(x, y)
await page.waitForTimeout(15)
await page.waitForTimeout(20)
}
// Ensure pointer is directly over target center and allow requestAnimationFrame
// and collision microtasks to settle before triggering drop
await page.mouse.move(toX, toY)
await page.evaluate(
() => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve))),
)
await page.waitForTimeout(100)
await page.mouse.up()
}
+1 -1
View File
@@ -13,7 +13,7 @@ interface CreateShareResponse {
*/
export function fileCard(page: Page, name: string): Locator {
return page
.locator('div.group')
.locator('[data-testid="file-card"]')
.filter({
has: page.locator(`input[value="${name}"]`).or(page.getByText(name, { exact: true })),
})
@@ -12,6 +12,11 @@ test('owner manages resource quotas in settings and resets usage from the dashbo
// 2. Click the Quotas sidebar tab
await page.getByRole('button', { name: /Quotas|配额/i }).click()
// Verify URL contains #quotas and remains after reload
expect(page.url()).toContain('#quotas')
await page.reload()
expect(page.url()).toContain('#quotas')
// 3. Verify Quotas settings card is displayed with empty state
await expect(page.getByRole('heading', { name: /^(Resource Quotas|资源配额)$/i })).toBeVisible()
await expect(page.getByText(/No resource quotas configured|暂未配置资源配额/i)).toBeVisible()
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/web-app",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"dependencies": {
+12 -12
View File
@@ -30,36 +30,36 @@ The following tools can be individually toggled in the agent configuration dialo
- **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`)
### 3. Read Asset (`read_asset`)
- **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.
- **Description:** Allows the agent to inspect images, extract still frames from video files, render PDF document pages, and read text/markdown documents.
- **Why it matters:** Enables multimodal agents to view visual media assets uploaded to Shumai, extract frame-by-frame video screenshots, inspect document layouts, and answer questions about uploaded creative assets.
### 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`)
### 4. 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`)
### 5. 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`)
### 6. 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`)
### 7. 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.
### 8. Download Asset (`download_asset`)
- **Description:** Allows the agent to download a workspace asset or specific storage S3 key to the local `.pi` directory for processing with local scripts or bash commands.
- **Why it matters:** Enables agents executing custom skill scripts to fetch files or intermediate transcode artifacts to local disk.
---
## Restricting Tools
+1 -1
View File
@@ -53,7 +53,7 @@ Shumai supports quota enforcement across multiple layers of the AI and agent arc
| **AI Cost ($)** | Dollar expenditure accrued from AI model providers. | Team, Role, or Members |
| **MCP Server Calls** | Number of tool invocations directed to a connected Model Context Protocol (MCP) server. | Target MCP Server ID |
| **Sandboxed Bash Commands** | Shell commands executed by the agent inside the isolated sandbox. | Wildcard pattern (e.g. `npm *`, `*`) |
| **Agent Tool Calls** | Calls to specific native tools (e.g. `analyze_image`, `screenshot`, `create_file`). | Tool name |
| **Agent Tool Calls** | Calls to specific native tools (e.g. `read_asset`, `download_asset`, `create_file`). | Tool name |
### Bash Command Wildcard Matching
+1
View File
@@ -74,6 +74,7 @@ export default tseslint.config(
'**/packages/db/src/generated/**',
'**/packages/core/src/generated/**',
'**/data/**',
'**/packages/core/src/provider/generator/**',
'**/packages/webui/components/ui/**',
'**/packages/webui/paraglide/**',
'**/packages/webui/public/**',
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "shumai",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"repository": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/agent",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"main": "./src/index.ts",
+123 -567
View File
@@ -13,8 +13,8 @@ import {
getCommentActivity,
getProjectAutofillFieldsActivity,
updateAssetMetadataActivity,
getAssetPathHierarchyActivity,
getAssetPathContextActivity,
executeAgentToolActivity,
generateSessionNameActivity,
type GenerateSessionNameParams,
getUserTeamInfoActivity,
@@ -36,8 +36,6 @@ import {
type Asset,
} from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { authzService } from '@shumai/core/src/authz/authz'
import { uploadService } from '@shumai/core/src/upload/upload'
vi.mock('../index', async () => {
const actual = await vi.importActual('../index')
@@ -567,8 +565,18 @@ describe('Agent Database Activities Integration', () => {
describe('initializeAgentSessionActivity', () => {
it('should initialize a session, create agent/user records if missing, and store existing comments context', async () => {
const attAsset = await prisma.asset.create({
data: {
name: 'attached-doc.pdf',
type: AssetType.file,
mediaType: 'application/pdf',
status: AssetStatus.uploaded,
projectId: project.id,
},
})
// Create first comment so it acts as context
await prisma.assetComment.create({
const firstComment = await prisma.assetComment.create({
data: {
assetId: asset.id,
message: 'Hello <@' + user.id + '> check this',
@@ -576,6 +584,13 @@ describe('Agent Database Activities Integration', () => {
},
})
await prisma.assetCommentAttachment.create({
data: {
commentId: firstComment.id,
assetId: attAsset.id,
},
})
await sleep(2)
// Create second comment that user responds with, triggering session initialization
const userComment = await prisma.assetComment.create({
@@ -614,9 +629,20 @@ describe('Agent Database Activities Integration', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- entry data is stored as Json in DB and needs casting to check properties
const parsedEntry = pathEntries[0] as any
expect(parsedEntry.message.content[0].text).toContain(
`[${user.name} (owner)]: Hello <@${user.name}> check this`,
)
expect(parsedEntry.type).toBe('custom_message')
expect(parsedEntry.customType).toBe('shumai_message')
expect(parsedEntry.content).toContain(`Hello <@${user.name}> check this`)
expect(parsedEntry.details.user.name).toBe(user.name)
expect(parsedEntry.details.user.role).toBe('owner')
expect(parsedEntry.details.attachedFiles).toEqual([
{
id: attAsset.id,
name: 'attached-doc.pdf',
type: 'file',
mediaType: undefined,
mimeType: 'application/pdf',
},
])
})
it('should distinguish Main Session (userCommentId: null) and Thread Session (userCommentId: rootId) during lazy sync', async () => {
@@ -685,7 +711,7 @@ describe('Agent Database Activities Integration', () => {
expect(threadSession1?.userCommentId).toBe(mainComment1.id) // Thread Session has userCommentId = rootComment.id
})
it('should dynamically tag top-level comments with [Thread ID: id] even if thread was created after initial sync', async () => {
it('should dynamically tag top-level comments with thread metadata in context even if thread was created after initial sync', async () => {
const msg3 = await prisma.assetComment.create({
data: { assetId: asset.id, message: 'Question in msg3', creatorId: user.id },
})
@@ -730,8 +756,13 @@ describe('Agent Database Activities Integration', () => {
const msg3Entry = pathEntries.find((e) => e.id.includes(msg3.id))
expect(msg3Entry).toBeDefined()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const text = (msg3Entry as any).message.content[0].text
expect(text).toContain(`[Thread ID: ${msg3.id}] [Replies: 1]`)
expect((msg3Entry as any).details?.thread).toEqual({
id: msg3.id,
replyCount: 1,
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const text = (msg3Entry as any).content ?? (msg3Entry as any).message?.content[0]?.text
expect(text).not.toContain(`[Thread ID: ${msg3.id}]`)
})
it('should create separate thread sessions when agent is mentioned in top-level comments (msg3 & msg5) sharing DAG entries without duplication', async () => {
@@ -953,7 +984,7 @@ describe('Agent Database Activities Integration', () => {
).resolves.not.toThrow()
})
it('should not tag Thread ID when only __CHAT__ placeholder comment exists under root comment', async () => {
it('should not tag thread when only __CHAT__ placeholder comment exists under root comment', async () => {
const rootComment = await prisma.assetComment.create({
data: { assetId: asset.id, message: 'Root comment asking agent', creatorId: user.id },
})
@@ -980,8 +1011,7 @@ describe('Agent Database Activities Integration', () => {
expect(rootEntry).toBeDefined()
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- entry data structure parsing
const content = (rootEntry as any)?.message?.content?.[0]?.text || ''
expect(content).not.toContain(`[Thread ID: ${rootComment.id}]`)
expect((rootEntry as any)?.details?.thread).toBeUndefined()
})
it('should return user name and role for team member', async () => {
@@ -1343,10 +1373,14 @@ describe('Agent Database Activities Integration', () => {
})
const pathCtx = await getAssetPathContextActivity(targetAsset.id)
expect(pathCtx).toContain('Path: RootFolder/SubFolder/TargetImage.webp')
expect(pathCtx).toContain(`name: RootFolder, id: ${rootFolder.id}`)
expect(pathCtx).toContain(`name: SubFolder, id: ${subfolder.id}`)
expect(pathCtx).toContain(`name: TargetImage.webp, id: ${targetAsset.id}`)
expect(pathCtx).toBe('RootFolder/SubFolder/TargetImage.webp')
const hierarchy = await getAssetPathHierarchyActivity(targetAsset.id)
expect(hierarchy.path).toBe('RootFolder/SubFolder/TargetImage.webp')
expect(hierarchy.ancestors).toEqual([
{ id: rootFolder.id, name: 'RootFolder' },
{ id: subfolder.id, name: 'SubFolder' },
])
})
})
@@ -1378,556 +1412,6 @@ describe('Agent Database Activities Integration', () => {
})
})
describe('executeAgentToolActivity', () => {
beforeEach(() => {
vi.spyOn(authzService, 'hasPermission').mockResolvedValue()
vi.spyOn(uploadService, 'triggerPostUploadWorkflows').mockResolvedValue()
})
describe('list_assets', () => {
it('should list assets inside a folder with cursor pagination', async () => {
const folder = await prisma.asset.create({
data: {
name: 'WorkspaceFolder',
type: AssetType.folder,
status: AssetStatus.uploaded,
projectId: project.id,
},
})
await prisma.asset.create({
data: {
name: 'file1.txt',
type: AssetType.file,
status: AssetStatus.uploaded,
projectId: project.id,
parentId: folder.id,
},
})
await prisma.asset.create({
data: {
name: 'subfolder1',
type: AssetType.folder,
status: AssetStatus.uploaded,
projectId: project.id,
parentId: folder.id,
},
})
const res = await executeAgentToolActivity({
taskId: 'task-1',
toolName: 'list_assets',
args: { parent: folder.id, page: 1, pageSize: 1 },
userId: user.id,
})
expect(res.assets.length).toBe(1)
expect(res.pageInfo.cursor).toBeDefined()
})
it('should resolve version_stack name and size from top child version when container name is empty', async () => {
const folder = await prisma.asset.create({
data: {
name: 'WorkspaceFolder',
type: AssetType.folder,
status: AssetStatus.uploaded,
projectId: project.id,
},
})
const versionStack = await prisma.asset.create({
data: {
name: '',
type: AssetType.version_stack,
status: AssetStatus.uploaded,
projectId: project.id,
parentId: folder.id,
},
})
await prisma.asset.create({
data: {
name: 'banana_ghost.txt',
type: AssetType.file,
status: AssetStatus.uploaded,
projectId: project.id,
parentId: versionStack.id,
sizeByte: 622960,
sortIndex: '0|i00000:',
},
})
const res = await executeAgentToolActivity({
taskId: 'task-1',
toolName: 'list_assets',
args: { parent: folder.id, page: 1, pageSize: 10 },
userId: user.id,
})
const stackItem = res.assets.find(
(a: { id: string; name: string; size: number; type: string }) => a.id === versionStack.id,
)
expect(stackItem).toBeDefined()
expect(stackItem?.name).toBe('banana_ghost.txt')
expect(stackItem?.size).toBe(622960)
expect(stackItem?.type).toBe('version_stack')
})
})
describe('create_folder', () => {
it('should create folder and increment parent fileCount', async () => {
const folder = await prisma.asset.create({
data: {
name: 'WorkspaceFolder',
type: AssetType.folder,
status: AssetStatus.uploaded,
projectId: project.id,
fileCount: 0,
},
})
const res = await executeAgentToolActivity({
taskId: 'task-1',
toolName: 'create_folder',
args: { parent: folder.id, name: 'subfolder' },
userId: user.id,
})
expect(res.name).toBe('subfolder')
expect(res.type).toBe('folder')
const parentFolder = await prisma.asset.findUnique({ where: { id: folder.id } })
expect(parentFolder?.fileCount).toBe(1)
})
})
describe('create_file', () => {
it('should create file, increment parent fileCount, and update parent sizes', async () => {
const folder = await prisma.asset.create({
data: {
name: 'WorkspaceFolder',
type: AssetType.folder,
status: AssetStatus.uploaded,
projectId: project.id,
fileCount: 0,
sizeByte: 0,
},
})
const res = await executeAgentToolActivity({
taskId: 'task-1',
toolName: 'create_file',
args: {
parent: folder.id,
s3Key: 'uploads/testfile.txt',
name: 'testfile.txt',
size: 500,
contentType: 'text/plain',
},
userId: user.id,
})
expect(res.name).toBe('testfile.txt')
expect(res.size).toBe(500)
const parentFolder = await prisma.asset.findUnique({ where: { id: folder.id } })
expect(parentFolder?.fileCount).toBe(1)
expect(Number(parentFolder?.sizeByte)).toBe(500)
expect(uploadService.triggerPostUploadWorkflows).toHaveBeenCalled()
})
it('should persist metadata on the created file when metadata map is provided', async () => {
await prisma.metadataField.create({
data: {
key: 'prompt',
scope: 'PROJECT',
projectId: project.id,
teamId: team.id,
config: { name: 'Prompt', type: 'text', autofillSource: 'CREATION_CONTEXT' },
},
})
const folder = await prisma.asset.create({
data: {
name: 'WorkspaceFolder',
type: AssetType.folder,
status: AssetStatus.uploaded,
projectId: project.id,
fileCount: 0,
sizeByte: 0,
},
})
const res = await executeAgentToolActivity({
taskId: 'task-1',
toolName: 'create_file',
args: {
parent: folder.id,
s3Key: 'uploads/ctx.txt',
name: 'ctx.txt',
size: 100,
contentType: 'text/plain',
metadata: { prompt: 'Generated using gemini' },
},
userId: user.id,
})
const val = await prisma.assetMetadataValue.findFirst({
where: { assetId: res.id, fieldKey: 'prompt' },
})
expect(val?.stringValue).toBe('Generated using gemini')
})
it('should persist select and selectMulti metadata with dynamic newOption', async () => {
const selectField = await prisma.metadataField.create({
data: {
key: 'provider',
scope: 'PROJECT',
project: { connect: { id: project.id } },
config: {
name: 'Provider',
type: 'select',
autofillSource: 'CREATION_CONTEXT',
select: { options: [{ id: 'openai', displayName: 'OpenAI', color: '#f43f5e' }] },
},
},
})
const multiField = await prisma.metadataField.create({
data: {
key: 'tags',
scope: 'PROJECT',
project: { connect: { id: project.id } },
config: {
name: 'Tags',
type: 'selectMulti',
autofillSource: 'CREATION_CONTEXT',
selectMulti: { options: [{ id: 'tag1', displayName: 'Tag 1', color: '#3b82f6' }] },
},
},
})
const folder = await prisma.asset.create({
data: {
name: 'WorkspaceFolder',
type: AssetType.folder,
status: AssetStatus.uploaded,
projectId: project.id,
fileCount: 0,
sizeByte: 0,
},
})
const res = await executeAgentToolActivity({
taskId: 'task-1',
toolName: 'create_file',
args: {
parent: folder.id,
s3Key: 'uploads/new_opt.txt',
name: 'new_opt.txt',
size: 100,
contentType: 'text/plain',
metadata: {
provider: { newOption: { value: 'Kling' } },
tags: ['tag1', { newOption: { value: 'Tag 2' } }],
},
},
userId: user.id,
})
// Verify select field
const providerVal = await prisma.assetMetadataValue.findFirst({
where: { assetId: res.id, fieldKey: selectField.key },
})
expect(providerVal?.stringValue).toBe('kling')
// Verify select field config was updated with new option
const updatedSelectField = await prisma.metadataField.findUnique({
where: { key: selectField.key },
})
const selectOpts =
(updatedSelectField?.config as PrismaJson.FieldConfig)?.select?.options || []
expect(selectOpts.some((o) => o.displayName === 'Kling' && o.id === 'kling')).toBe(true)
// Verify selectMulti field
const tagsVal = await prisma.assetMetadataValue.findFirst({
where: { assetId: res.id, fieldKey: multiField.key },
})
expect(tagsVal?.jsonValue).toEqual(['tag1', 'tag-2'])
// Verify selectMulti field config was updated with new option
const updatedMultiField = await prisma.metadataField.findUnique({
where: { key: multiField.key },
})
const multiOpts =
(updatedMultiField?.config as PrismaJson.FieldConfig)?.selectMulti?.options || []
expect(multiOpts.some((o) => o.displayName === 'Tag 2' && o.id === 'tag-2')).toBe(true)
})
it('should reject metadata with keys that are not valid in this project', async () => {
const folder = await prisma.asset.create({
data: {
name: 'WorkspaceFolder',
type: AssetType.folder,
status: AssetStatus.uploaded,
projectId: project.id,
fileCount: 0,
sizeByte: 0,
},
})
await expect(
executeAgentToolActivity({
taskId: 'task-1',
toolName: 'create_file',
args: {
parent: folder.id,
s3Key: 'uploads/ctx.txt',
name: 'ctx.txt',
size: 100,
contentType: 'text/plain',
metadata: { prompt: 'ok', unknownKey: 'not allowed' },
},
userId: user.id,
}),
).rejects.toThrow(/not valid in this project/)
// No metadata value should have been written for the unknown key
const val = await prisma.assetMetadataValue.findFirst({
where: { fieldKey: 'unknownKey' },
})
expect(val).toBeNull()
})
})
describe('create_version', () => {
it('should create a version stack when parent is a regular file', async () => {
const folder = await prisma.asset.create({
data: {
name: 'WorkspaceFolder',
type: AssetType.folder,
status: AssetStatus.uploaded,
projectId: project.id,
fileCount: 1,
sizeByte: 200,
},
})
const fileAsset = await prisma.asset.create({
data: {
name: 'v1.txt',
type: AssetType.file,
status: AssetStatus.uploaded,
projectId: project.id,
parentId: folder.id,
sizeByte: 200,
},
})
const res = await executeAgentToolActivity({
taskId: 'task-1',
toolName: 'create_version',
args: {
parent: fileAsset.id,
s3Key: 'uploads/v2.txt',
name: 'v2.txt',
size: 300,
contentType: 'text/plain',
},
userId: user.id,
})
expect(res.name).toBe('v2.txt')
expect(res.size).toBe(300)
// Check version stack was created
const stack = await prisma.asset.findFirst({
where: { type: AssetType.version_stack, projectId: project.id },
include: { children: true },
})
expect(stack).not.toBeNull()
expect(stack?.children.length).toBe(2) // v1.txt and v2.txt should both be inside the stack
})
it('should add to version stack when parent is already inside a version stack', async () => {
const folder = await prisma.asset.create({
data: {
name: 'WorkspaceFolder',
type: AssetType.folder,
status: AssetStatus.uploaded,
projectId: project.id,
},
})
const stack = await prisma.asset.create({
data: {
name: 'VersionStack',
type: AssetType.version_stack,
status: AssetStatus.uploaded,
projectId: project.id,
parentId: folder.id,
fileCount: 1,
sizeByte: 100,
},
})
const existingFileVersion = await prisma.asset.create({
data: {
name: 'v1.txt',
type: AssetType.file,
status: AssetStatus.uploaded,
projectId: project.id,
parentId: stack.id,
sizeByte: 100,
},
})
const res = await executeAgentToolActivity({
taskId: 'task-1',
toolName: 'create_version',
args: {
parent: existingFileVersion.id,
s3Key: 'uploads/v2.txt',
name: 'v2.txt',
size: 250,
contentType: 'text/plain',
},
userId: user.id,
})
expect(res.name).toBe('v2.txt')
const updatedStack = await prisma.asset.findUnique({
where: { id: stack.id },
include: { children: true },
})
expect(updatedStack?.fileCount).toBe(2)
expect(Number(updatedStack?.sizeByte)).toBe(350)
})
it('should persist metadata on the created version when metadata map is provided', async () => {
await prisma.metadataField.create({
data: {
key: 'prompt',
scope: 'PROJECT',
projectId: project.id,
teamId: team.id,
config: { name: 'Prompt', type: 'text', autofillSource: 'CREATION_CONTEXT' },
},
})
const folder = await prisma.asset.create({
data: {
name: 'WorkspaceFolder',
type: AssetType.folder,
status: AssetStatus.uploaded,
projectId: project.id,
fileCount: 1,
sizeByte: 200,
},
})
const fileAsset = await prisma.asset.create({
data: {
name: 'v1.txt',
type: AssetType.file,
status: AssetStatus.uploaded,
projectId: project.id,
parentId: folder.id,
sizeByte: 200,
},
})
const res = await executeAgentToolActivity({
taskId: 'task-1',
toolName: 'create_version',
args: {
parent: fileAsset.id,
s3Key: 'uploads/v2.txt',
name: 'v2.txt',
size: 300,
contentType: 'text/plain',
metadata: { prompt: 'Generated using seedance' },
},
userId: user.id,
})
const val = await prisma.assetMetadataValue.findFirst({
where: { assetId: res.id, fieldKey: 'prompt' },
})
expect(val?.stringValue).toBe('Generated using seedance')
})
it('should persist select and selectMulti newOption metadata in create_version', async () => {
const selectField = await prisma.metadataField.create({
data: {
key: 'versionProvider',
scope: 'PROJECT',
project: { connect: { id: project.id } },
config: {
name: 'Version Provider',
type: 'select',
autofillSource: 'CREATION_CONTEXT',
select: { options: [{ id: 'openai', displayName: 'OpenAI', color: '#f43f5e' }] },
},
},
})
const folder = await prisma.asset.create({
data: {
name: 'WorkspaceFolder',
type: AssetType.folder,
status: AssetStatus.uploaded,
projectId: project.id,
fileCount: 1,
sizeByte: 200,
},
})
const fileAsset = await prisma.asset.create({
data: {
name: 'v1.txt',
type: AssetType.file,
status: AssetStatus.uploaded,
projectId: project.id,
parentId: folder.id,
sizeByte: 200,
},
})
const res = await executeAgentToolActivity({
taskId: 'task-1',
toolName: 'create_version',
args: {
parent: fileAsset.id,
s3Key: 'uploads/v2.txt',
name: 'v2.txt',
size: 300,
contentType: 'text/plain',
metadata: {
versionProvider: { newOption: { value: 'Midjourney' } },
},
},
userId: user.id,
})
const val = await prisma.assetMetadataValue.findFirst({
where: { assetId: res.id, fieldKey: selectField.key },
})
expect(val?.stringValue).toBe('midjourney')
const updatedField = await prisma.metadataField.findUnique({
where: { key: selectField.key },
})
const opts = (updatedField?.config as PrismaJson.FieldConfig)?.select?.options || []
expect(opts.some((o) => o.displayName === 'Midjourney' && o.id === 'midjourney')).toBe(true)
})
})
})
describe('generateSessionNameActivity', () => {
it('should generate and save session name for chat sessions', async () => {
await prisma.team.create({
@@ -2216,5 +1700,77 @@ describe('Agent Database Activities Integration', () => {
'<project_instructions path="/subfolder/AGENTS.md">\n# Subfolder Policy\n</project_instructions>',
)
})
it('agentChatActivity injects # Comment Threads into systemPrompt when userCommentId is present', async () => {
const user = await prisma.user.create({
data: { name: 'User_Comment_Prompt', email: `comment-prompt-${Date.now()}@example.com` },
})
const team = await prisma.team.create({
data: { name: 'Team_Comment_Prompt_' + Date.now() },
})
const project = await prisma.project.create({
data: { name: 'Project_Comment_Prompt', teamId: team.id },
})
const asset = await prisma.asset.create({
data: {
name: 'Asset_1',
type: AssetType.file,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
const comment = await prisma.assetComment.create({
data: {
assetId: asset.id,
message: 'Question',
creatorId: user.id,
},
})
const mockHarness = {
subscribe: vi.fn(),
prompt: vi.fn().mockResolvedValue({
content: [{ type: 'text', text: 'Chat reply' }],
usage: { input: 10, output: 20 },
}),
}
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>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Mock AgentHarness instance for activity test
harness: mockHarness as unknown as AgentHarness<any, any, any, any>,
})
const context = {
agent: { id: 'agent-1', provider: { name: 'google' }, modelRef: { modelId: 'gemini' } },
dbProviders: [],
teamSkills: [],
allowedDomains: [],
} as unknown as AgentExecutionContext
await agentChatActivity({
teamId: team.id,
agentId: 'agent-1',
message: 'Hello',
imageUrls: [],
projectId: project.id,
folderId: asset.id,
assetId: asset.id,
sessionId: 'mock-session-id',
userCommentId: comment.id,
userId: user.id,
context,
})
const lastCall = vi.mocked(piAgent.createAgentSession).mock.calls.at(-1)
const systemPromptArg = lastCall?.[0].systemPrompt as string
expect(systemPromptArg).toContain('# Comment Threads')
expect(systemPromptArg).toContain('<thread id="..." reply_count="..." />')
expect(systemPromptArg).toContain('read_thread')
})
})
})
+201 -565
View File
@@ -1,29 +1,15 @@
import {
type AgentMessage,
type AgentTool,
type SessionTreeEntry,
} from '@earendil-works/pi-agent-core'
import { type AgentMessage, type AgentTool } from '@earendil-works/pi-agent-core'
import { type ImageContent } from '@earendil-works/pi-ai'
import { assetService } from '@shumai/core/src/asset/asset'
import {
authzService,
Permission,
ResourceType,
resolveEffectiveRole,
} from '@shumai/core/src/authz/authz'
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
import { logger } from '@shumai/core/src/logger'
import { mcpService } from '@shumai/core/src/mcp/mcp-service'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { 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 { getAgentRequiredLevel, getRoleLevel } from '@shumai/core/src/agent/permissions'
import { AssetType, prisma, Prisma, type Skill } from '@shumai/db'
import { registerLocalCancelHandler, unregisterLocalCancelHandler } from '@shumai/workflow-core'
import { ApplicationFailure, Context } from '@temporalio/activity'
import { generateKeyBetween } from 'jittered-fractional-indexing'
import { ulid } from 'ulid'
import { DatabaseSessionStorage } from '../database-session-storage'
import {
createAgentSession,
@@ -34,7 +20,11 @@ import {
import { aiUsageService } from '@shumai/core/src/ai-usage/ai-usage'
import { quotaService, QuotaExceededError } from '@shumai/core/src/quota/quota-service'
import { UpdateAssetMetadataRequest } from '@shumai/dtos'
import {
UpdateAssetMetadataRequest,
type ShumaiMessageContext,
type ShumaiMediaPosition,
} from '@shumai/dtos'
export interface Usage {
inputTokens: number
@@ -90,7 +80,7 @@ async function executeAgentPrompt(params: {
agentId: string
prompt: string
images: string[]
agentsInstruction: string
messageContext?: ShumaiMessageContext
sessionId?: string
userId?: string
projectId?: string
@@ -125,15 +115,15 @@ shumai has its own cloud file system. If a user asks you to perform file system
If you need to create files in the local filesystem (for example, a temporary file for uploading), only the '.pi' folder in the current directory has write permissions. Do NOT attempt to create files in any other directories.
When creating a file or version, you may attach metadata (for example, the AI model or prompt used to generate the asset). The allowed field keys and value types are declared directly in the 'metadata' parameter of the 'create_file' and 'create_version' tools.`
When creating a file or version, you may attach metadata (for example, the AI model or prompt used to generate the asset). The allowed field keys and value types are declared directly in the 'metadata' parameter of the 'create_file' and 'create_version' tools.
# Message Context & Tools
User messages may contain a <context> block detailing the user, active asset location, playback position, and attachments. Use the asset IDs from <context> when invoking tools (e.g. 'read_asset', 'download_asset', 'list_assets'). When an <annotation id="..." /> tag is present, it indicates the user has drawn visual markups on the asset at the specified position. When calling 'read_asset', pass the 'annotationId' parameter with that ID to view the image or rendered pages with the visual markups overlaid.`
if (agent.soul) {
systemPrompt = `${systemPrompt}\n\nAgent Personality and Core Instructions:\n${agent.soul}`
}
// 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.
let targetAssetId = params.assetId
if (!targetAssetId) {
if (params.userCommentId) {
@@ -173,6 +163,21 @@ When creating a file or version, you may attach metadata (for example, the AI mo
systemPrompt += `\n\nYour current model supports the following input types: ${modelConfig.input.join(', ')}.`
}
const isCommentMode =
!!params.userCommentId ||
(params.sessionId
? (
await prisma.agentSession.findUnique({
where: { id: params.sessionId },
select: { type: true },
})
)?.type === 'comment'
: false)
if (isCommentMode) {
systemPrompt += `\n\n# Comment Threads\nWhen operating in comment mode, previous top-level messages in the conversation history may contain a <thread id="..." reply_count="..." /> tag indicating an earlier discussion thread with replies. If a user's question refers to or depends on earlier comments or discussions, you can use the 'read_thread' tool with the thread ID to inspect all replies in that thread.`
}
const agentConfig = agent.config as PrismaJson.AgentConfig | null | undefined
const thinkingLevel = agentConfig?.thinkingLevel || 'off'
@@ -264,32 +269,9 @@ When creating a file or version, you may attach metadata (for example, the AI mo
}
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 storage = session.getStorage()
if (storage instanceof DatabaseSessionStorage && params.messageContext) {
storage.currentMessageContext = params.messageContext
}
let totalInputTokens = 0
@@ -423,7 +405,7 @@ When creating a file or version, you may attach metadata (for example, the AI mo
const sessionEntries = await session.getEntries()
sessionEntries.forEach((entry) => {
if (entry.type === 'message') {
if (entry.type === 'message' && entry.message) {
const msg = entry.message
if (msg.role === 'toolResult') {
const logMsg = { ...msg, content: undefined }
@@ -460,7 +442,6 @@ When creating a file or version, you may attach metadata (for example, the AI mo
cost: totalCost,
}
const storage = session.getStorage()
let sessionId = ''
if (storage instanceof DatabaseSessionStorage) {
sessionId = storage.sessionId
@@ -488,11 +469,11 @@ export interface AgentChatParams {
projectId: string
folderId: string
assetId?: string
agentsInstruction?: string
sessionId: string
userId?: string
userCommentId?: string | null
context: AgentExecutionContext
messageContext?: ShumaiMessageContext
attachedAssets?: Array<{ id: string; name: string; type: string }>
}
@@ -522,40 +503,19 @@ export async function agentChatActivity(params: AgentChatParams) {
const cleanMessage = params.message.replace(/<@[A-Z0-9]+>/g, '').trim()
const sessionId = params.sessionId
let prompt = cleanMessage
if (params.userCommentId) {
let targetUserId = params.userId
if (!targetUserId) {
const comment = await prisma.assetComment.findUnique({
where: { id: params.userCommentId },
select: { creatorId: true },
})
targetUserId = comment?.creatorId || undefined
}
if (targetUserId) {
const userInfo = await getUserTeamInfoActivity({
userId: targetUserId,
teamId: params.teamId,
})
if (userInfo) {
prompt = `[${userInfo.name} (${userInfo.role})]: ${cleanMessage}`
}
}
}
return executeAgentPrompt({
taskId: params.taskId,
teamId: params.teamId,
agentId: params.agentId,
prompt,
prompt: cleanMessage,
images: params.imageUrls,
agentsInstruction: params.agentsInstruction || '',
sessionId,
userId: params.userId,
projectId: params.projectId,
assetId: params.assetId || params.folderId,
userCommentId: params.userCommentId,
context: params.context,
messageContext: params.messageContext,
attachedAssets: params.attachedAssets,
})
}
@@ -597,7 +557,6 @@ export async function autofillAiActivity(params: AutofillAiParams) {
agentId: agent.id,
prompt: fullPrompt,
images: params.images,
agentsInstruction: '',
sessionId: undefined,
userId: undefined,
projectId: params.projectId,
@@ -720,7 +679,10 @@ export async function initializeAgentSessionActivity(params: {
createdAt: { lte: rootComment.createdAt },
},
orderBy: { id: 'asc' },
include: { creator: true },
include: {
creator: true,
attachments: { include: { asset: true } },
},
})
// 2. Fetch thread replies up to userComment.createdAt excluding userComment itself
@@ -732,7 +694,10 @@ export async function initializeAgentSessionActivity(params: {
id: { not: userComment.id },
},
orderBy: { id: 'asc' },
include: { creator: true },
include: {
creator: true,
attachments: { include: { asset: true } },
},
})
: []
@@ -782,7 +747,13 @@ export async function initializeAgentSessionActivity(params: {
}
}
function formatCommentMessage(c: (typeof allCommentsToSync)[0]): string {
const assetRecord = await prisma.asset.findUnique({
where: { id: userComment.assetId },
select: { media: true },
})
const proxyType = (assetRecord?.media as PrismaJson.MediaInfo | null)?.proxyType
function buildCommentEntry(c: (typeof allCommentsToSync)[0]) {
const isAgent = c.creator?.type === 'agent' || c.sessionId !== null
let messageContent = c.message || ''
@@ -792,13 +763,88 @@ export async function initializeAgentSessionActivity(params: {
})
if (isAgent) {
const agentName = c.creator?.name || 'Ai Agent'
return `[Agent Message][${agentName}]: ${messageContent}`
} else if (c.creator?.name) {
const message: AgentMessage = {
role: 'assistant',
content: [{ type: 'text', text: messageContent }],
api: 'custom',
provider: 'shumai',
model: 'agent',
usage: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
},
stopReason: 'stop',
timestamp: c.createdAt.getTime(),
}
return {
type: 'message' as const,
data: { message },
}
}
let userObj: ShumaiMessageContext['user'] = undefined
if (c.creator) {
const role = (c.creatorId ? userRoleMap.get(c.creatorId) : undefined) || 'user'
return `[${c.creator.name} (${role})]: ${messageContent}`
userObj = {
id: c.creator.id,
name: c.creator.name || 'Unknown',
role,
}
}
let position: ShumaiMediaPosition | undefined = undefined
if (c.second !== null && c.second !== undefined) {
if (proxyType === 'pdf') {
position = { type: 'page', page: Math.round(c.second) }
} else {
position = { type: 'time', seconds: c.second }
}
}
const annotation = !!(c.annotation && Array.isArray(c.annotation) && c.annotation.length > 0)
const attachedFiles: ShumaiMessageContext['attachedFiles'] = []
if (c.attachments && c.attachments.length > 0) {
for (const att of c.attachments) {
if (att.asset) {
const attProxyType = (att.asset.media as PrismaJson.MediaInfo | null)?.proxyType
attachedFiles.push({
id: att.asset.id,
name: att.asset.name,
type: att.asset.type,
mediaType: attProxyType as 'image' | 'video' | 'pdf' | 'audio' | 'other' | undefined,
mimeType: att.asset.mediaType || undefined,
})
}
}
}
const commentDetails: ShumaiMessageContext = {
id: c.id,
...(userObj ? { user: userObj } : {}),
...(position ? { position } : {}),
...(annotation
? {
annotation: true,
annotations: c.annotation as unknown as ShumaiMessageContext['annotations'],
}
: {}),
...(attachedFiles.length > 0 ? { attachedFiles } : {}),
}
return {
type: 'custom_message' as const,
data: {
customType: 'shumai_message',
content: messageContent,
display: true,
details: commentDetails,
},
}
return messageContent
}
// 3. Sync top-level comments into agent_session_entries (DAG spine)
@@ -810,21 +856,16 @@ export async function initializeAgentSessionActivity(params: {
})
if (!existingEntry) {
const messageContent = formatCommentMessage(c)
const message: AgentMessage = {
role: 'user',
content: [{ type: 'text', text: messageContent }],
timestamp: c.createdAt.getTime(),
}
const entryData = buildCommentEntry(c)
await prisma.agentSessionEntry.create({
data: {
id: c.id,
sessionId: mainSession.id,
assetId: userComment.assetId,
type: 'message',
type: entryData.type,
parentId: mainPrevId,
createdAt: c.createdAt,
data: { message },
data: entryData.data as PrismaJson.PiSessionEntryData,
},
})
}
@@ -850,21 +891,16 @@ export async function initializeAgentSessionActivity(params: {
})
if (!existingReplyEntry) {
const messageContent = formatCommentMessage(reply)
const message: AgentMessage = {
role: 'user',
content: [{ type: 'text', text: messageContent }],
timestamp: reply.createdAt.getTime(),
}
const entryData = buildCommentEntry(reply)
await prisma.agentSessionEntry.create({
data: {
id: reply.id,
sessionId: session.id,
assetId: userComment.assetId,
type: 'message',
type: entryData.type,
parentId: lastReplyParentId,
createdAt: reply.createdAt,
data: { message },
data: entryData.data as PrismaJson.PiSessionEntryData,
},
})
}
@@ -1175,6 +1211,7 @@ export async function getAssetActivity(assetId: string) {
},
})
let resolvedAsset = asset
if (asset && asset.type === AssetType.version_stack) {
const latestVersion = await prisma.asset.findFirst({
where: { parentId: asset.id, isDeleted: false },
@@ -1190,21 +1227,71 @@ export async function getAssetActivity(assetId: string) {
})
if (latestVersion) {
return {
resolvedAsset = {
...latestVersion,
project: latestVersion.project ?? asset.project,
}
} as typeof asset
}
}
return asset
if (!resolvedAsset) return null
let url: string | undefined
if (resolvedAsset.storageKey?.key) {
try {
url = await s3Service.presign(
process.env.S3_BUCKET || 'shumai',
resolvedAsset.storageKey.key,
'GET',
)
} catch {
// Ignore presign errors
}
}
return {
...resolvedAsset,
url,
}
}
export async function getCommentActivity(commentId: string) {
return prisma.assetComment.findUnique({
const comment = await prisma.assetComment.findUnique({
where: { id: commentId },
include: { attachments: { include: { asset: { include: { storageKey: true } } } } },
include: {
attachments: {
include: { asset: { include: { storageKey: true } } },
},
},
})
if (!comment) return null
const attachmentsWithUrl = await Promise.all(
comment.attachments.map(async (att) => {
let url: string | undefined
if (att.asset?.storageKey?.key) {
try {
url = await s3Service.presign(
process.env.S3_BUCKET || 'shumai',
att.asset.storageKey.key,
'GET',
)
} catch {
// Ignore
}
}
return {
...att,
asset: att.asset ? { ...att.asset, url } : null,
}
}),
)
return {
...comment,
attachments: attachmentsWithUrl,
}
}
export async function getProjectAutofillFieldsActivity(projectId: string) {
@@ -1219,7 +1306,9 @@ export async function updateAssetMetadataActivity(params: {
return metadataService.updateAssetMetadata(params.assetId, params.metadata)
}
export async function getAssetPathContextActivity(assetId: string): Promise<string> {
export async function getAssetPathHierarchyActivity(
assetId: string,
): Promise<{ path: string; ancestors: Array<{ id: string; name: string }> }> {
const parts: { name: string; id: string }[] = []
let currentId: string | null = assetId
@@ -1238,469 +1327,16 @@ export async function getAssetPathContextActivity(assetId: string): Promise<stri
currentId = assetNode.parentId
}
if (parts.length === 0) return ''
if (parts.length === 0) return { path: '', ancestors: [] }
const pathStr = parts.map((p) => p.name).join('/')
let contextStr = `Path: ${pathStr}\n\n`
for (const part of parts) {
contextStr += `name: ${part.name}, id: ${part.id}\n`
}
return contextStr
const path = parts.map((p) => p.name).join('/')
const ancestors = parts.slice(0, -1)
return { path, ancestors }
}
export interface ExecuteAgentToolParams {
taskId: string
toolName: string
// args are dynamic JSON arguments passed from the agent harness at runtime
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args: any
userId?: string
}
/**
* Validates that every metadata key provided by the agent is a defined
* CREATION_CONTEXT field of the project. Unknown keys are rejected with a
* non-retryable failure so Temporal does not retry expected validation errors.
*/
async function validateCreationContextMetadata(
userId: string,
projectId: string,
metadata: Record<string, unknown> | undefined,
): Promise<void> {
if (!metadata || Object.keys(metadata).length === 0) return
const fields = await metadataService.listProjectFields(userId, projectId)
const allowedKeys = new Set(
fields
.filter((f) => f.field.config?.autofillSource === 'CREATION_CONTEXT')
.map((f) => f.field.key),
)
const invalidKeys = Object.keys(metadata).filter((key) => !allowedKeys.has(key))
if (invalidKeys.length > 0) {
throw ApplicationFailure.create({
message:
`metadata contains keys that are not valid in this project: ${invalidKeys.join(', ')}. ` +
"The valid metadata fields are fixed to the current conversation's project. " +
'If the target folder or file belongs to a different project, call the tool WITHOUT the metadata parameter.',
nonRetryable: true,
})
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Activity return type is a union of tool result shapes
export async function executeAgentToolActivity(params: ExecuteAgentToolParams): Promise<any> {
const { toolName, args, userId } = params
if (!userId) {
throw ApplicationFailure.create({
message: 'User ID is required for tool authz',
nonRetryable: true,
})
}
// 1. Fetch User
const user = await prisma.user.findUnique({ where: { id: userId } })
if (!user) {
throw ApplicationFailure.create({
message: `User not found: ${userId}`,
nonRetryable: true,
})
}
// 2. Perform Authz and Action based on toolName
switch (toolName) {
case 'list_assets': {
const parent = args.parent
if (!parent) {
throw ApplicationFailure.create({
message: 'parent parameter is required',
nonRetryable: true,
})
}
// Authz check
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Asset,
id: parent,
})
const page = args.page || 1
const pageSize = args.pageSize || 20
const type = args.type || 'all'
// We need to list files and folders of parent dir
const where: Prisma.AssetWhereInput = {
parentId: parent,
isDeleted: false,
}
if (type === 'file') {
where.type = { in: ['file', 'version_stack'] }
} else if (type === 'folder') {
where.type = 'folder'
} else {
where.type = { in: ['file', 'folder', 'version_stack'] }
}
const limit = pageSize
const offset = (page - 1) * limit
const after = offset > 0 ? encodeCursor(offset) : undefined
const { data: assets, pageInfo } = await paginateQuery<
Prisma.AssetGetPayload<Record<string, never>>
>(
async (skip, take) => {
return prisma.asset.findMany({
where,
orderBy: { id: 'desc' }, // sort desc per ULID default in sorting guidelines!
skip,
take,
})
},
async () => prisma.asset.count({ where }),
{ first: limit, after },
)
const stackIds = assets.filter((a) => a.type === AssetType.version_stack).map((a) => a.id)
const latestVersionsMap = new Map<string, Prisma.AssetGetPayload<Record<string, never>>>()
if (stackIds.length > 0) {
const versions = await prisma.asset.findMany({
where: { parentId: { in: stackIds }, isDeleted: false },
orderBy: { sortIndex: 'asc' },
})
for (const v of versions) {
if (!latestVersionsMap.has(v.parentId!)) {
latestVersionsMap.set(v.parentId!, v)
}
}
}
// return asset id, asset name, asset type, asset size (sizeByte or size)
const results = assets.map((a) => {
const latestVersion = latestVersionsMap.get(a.id)
return {
id: a.id,
name: a.type === AssetType.version_stack ? (latestVersion?.name ?? a.name) : a.name,
type: a.type,
size:
a.type === AssetType.version_stack && latestVersion
? Number(latestVersion.sizeByte)
: Number(a.sizeByte),
}
})
return {
assets: results,
pageInfo,
}
}
case 'create_folder': {
const parent = args.parent
const name = args.name
if (!parent || !name) {
throw ApplicationFailure.create({
message: 'parent and name parameters are required',
nonRetryable: true,
})
}
// Authz check
await authzService.hasPermission({
user,
permission: Permission.Edit,
type: ResourceType.Asset,
id: parent,
})
const newFolder = await assetService.createAsset({
name,
parentId: parent,
type: 'folder',
creatorId: userId,
})
return {
id: newFolder.id,
name: newFolder.name,
type: newFolder.type,
size: Number(newFolder.sizeByte),
}
}
case 'create_file': {
const parent = args.parent
const s3Key = args.s3Key as string
const name = args.name as string
const fileSize = args.size as number
const mimeType = args.contentType as string
const metadata =
typeof args.metadata === 'object' && args.metadata !== null
? (args.metadata as Record<string, unknown>)
: undefined
if (!parent || !s3Key || !name || fileSize === undefined || !mimeType) {
throw ApplicationFailure.create({
message: 'parent, s3Key, name, size, and contentType parameters are required',
nonRetryable: true,
})
}
// Authz check
await authzService.hasPermission({
user,
permission: Permission.Edit,
type: ResourceType.Asset,
id: parent,
})
// Fetch parent folder to resolve projectId and teamId
const parentAsset = await prisma.asset.findUnique({
where: { id: parent },
include: { project: true },
})
if (!parentAsset) {
throw ApplicationFailure.create({
message: `Parent folder not found with ID: ${parent}`,
nonRetryable: true,
})
}
if (!parentAsset.projectId || !parentAsset.project?.teamId) {
throw ApplicationFailure.create({
message: `Parent folder ${parent} has no project or team associated`,
nonRetryable: true,
})
}
// Validate that any provided metadata keys are defined CREATION_CONTEXT fields
await validateCreationContextMetadata(userId, parentAsset.projectId, metadata)
// Create asset via assetService
const newFile = await assetService.createAsset({
name,
type: 'file',
parentId: parent,
key: s3Key,
sizeByte: fileSize,
contentType: mimeType,
creatorId: userId,
})
// Increment update ancestor size
await prisma.$transaction(async (tx) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await assetService.updateAncestorsSize(tx as any, parent, fileSize)
// Persist creation context metadata provided by the agent
if (metadata) {
const metadataUpdates = Object.entries(metadata)
.filter(([, value]) => value !== null)
.map(([key, value]) => ({ key, value }))
if (metadataUpdates.length > 0) {
await metadataService.updateAssetMetadataInTx(tx, newFile.id, metadataUpdates)
}
}
// Trigger post-upload transcode and AI workflows
await uploadService.triggerPostUploadWorkflows(
tx,
newFile.id,
parentAsset.project!.teamId,
parentAsset.projectId!,
)
})
return {
id: newFile.id,
name: newFile.name,
type: newFile.type,
size: Number(newFile.sizeByte),
}
}
case 'create_version': {
const parent = args.parent // parent file id
const s3Key = args.s3Key as string
const name = args.name as string
const fileSize = args.size as number
const mimeType = args.contentType as string
const metadata =
typeof args.metadata === 'object' && args.metadata !== null
? (args.metadata as Record<string, unknown>)
: undefined
if (!parent || !s3Key || !name || fileSize === undefined || !mimeType) {
throw ApplicationFailure.create({
message: 'parent, s3Key, name, size, and contentType parameters are required',
nonRetryable: true,
})
}
// Fetch parent file and cast to any due to Prisma type resolution limits
/* prettier-ignore */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const parentFile = (await prisma.asset.findUnique({ where: { id: parent }, include: { parent: true, project: true } })) as any
if (!parentFile) {
throw ApplicationFailure.create({
message: `Parent file not found with ID: ${parent}`,
nonRetryable: true,
})
}
if (!parentFile.projectId || !parentFile.project?.teamId) {
throw ApplicationFailure.create({
message: `Parent file ${parent} has no project or team associated`,
nonRetryable: true,
})
}
// Validate that any provided metadata keys are defined CREATION_CONTEXT fields
await validateCreationContextMetadata(userId, parentFile.projectId, metadata)
// Authz check: verify Edit permission on the parent file (resource ID is parent file id)
await authzService.hasPermission({
user,
permission: Permission.Edit,
type: ResourceType.Asset,
id: parent,
})
// If already in a version stack
if (parentFile.parentId && parentFile.parent?.type === AssetType.version_stack) {
const stackId = parentFile.parentId
// Create the new file asset
const newFile = await assetService.createAsset({
name,
type: 'file',
parentId: stackId,
key: s3Key,
sizeByte: fileSize,
contentType: mimeType,
creatorId: userId,
})
// Generate sort index for new version inside the stack (newest gets the lowest sortIndex)
const firstChild = await prisma.asset.findFirst({
where: {
parentId: stackId,
NOT: { id: newFile.id },
},
orderBy: { sortIndex: 'asc' },
})
const newSortIndex = generateKeyBetween(null, firstChild?.sortIndex || null)
await prisma.$transaction(async (tx) => {
// Assign sort index
await tx.asset.update({
where: { id: newFile.id },
data: { sortIndex: newSortIndex },
})
// Persist creation context metadata provided by the agent
if (metadata) {
const metadataUpdates = Object.entries(metadata)
.filter(([, value]) => value !== null)
.map(([key, value]) => ({ key, value }))
if (metadataUpdates.length > 0) {
await metadataService.updateAssetMetadataInTx(tx, newFile.id, metadataUpdates)
}
}
// Increment stack's size (fileCount is incremented by createAsset already)
const updatedStack = await tx.asset.update({
where: { id: stackId },
data: {
sizeByte: { increment: fileSize },
},
})
// Update ancestors' size of stack's parent
if (updatedStack.parentId) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await assetService.updateAncestorsSize(tx as any, updatedStack.parentId, fileSize)
}
// Trigger post-upload transcode and AI workflows
await uploadService.triggerPostUploadWorkflows(
tx,
newFile.id,
parentFile.project.teamId,
parentFile.projectId,
)
})
return {
id: newFile.id,
name: newFile.name,
type: newFile.type,
size: Number(newFile.sizeByte),
}
} else {
// Parent is a regular file. We must create a new version stack.
const folderParentId = parentFile.parentId
if (!folderParentId) {
throw ApplicationFailure.create({
message: `Parent file ${parent} has no parent folder`,
nonRetryable: true,
})
}
// Create the new file asset under parent folder first (so that createVersionStack can run on them,
// which enforces both files must have same parent initially)
const newFile = await assetService.createAsset({
name,
type: 'file',
parentId: folderParentId,
key: s3Key,
sizeByte: fileSize,
contentType: mimeType,
creatorId: userId,
})
await prisma.$transaction(async (tx) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const txVersionStackService = new VersionStackService(tx as any)
await txVersionStackService.createVersionStack({
fileIds: [newFile.id, parentFile.id],
projectId: parentFile.projectId!,
creatorId: userId,
})
// Persist creation context metadata provided by the agent
if (metadata) {
const metadataUpdates = Object.entries(metadata)
.filter(([, value]) => value !== null)
.map(([key, value]) => ({ key, value }))
if (metadataUpdates.length > 0) {
await metadataService.updateAssetMetadataInTx(tx, newFile.id, metadataUpdates)
}
}
// Update ancestors' size (add size of the new file version)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await assetService.updateAncestorsSize(tx as any, folderParentId!, fileSize)
// Trigger post-upload transcode and AI workflows
await uploadService.triggerPostUploadWorkflows(
tx,
newFile.id,
parentFile.project.teamId,
parentFile.projectId!,
)
})
return {
id: newFile.id,
name: newFile.name,
type: newFile.type,
size: Number(newFile.sizeByte),
}
}
}
default:
throw ApplicationFailure.create({
message: `Unsupported tool name: ${toolName}`,
nonRetryable: true,
})
}
export async function getAssetPathContextActivity(assetId: string): Promise<string> {
const { path } = await getAssetPathHierarchyActivity(assetId)
return path
}
export interface GenerateSessionNameParams {
@@ -0,0 +1,354 @@
import { describe, it, expect } from 'vitest'
import { escapeXmlAttr, serializeContextToXml } from './serialize-context'
import type { ShumaiMessageContext } from '@shumai/dtos'
describe('serializeContextToXml', () => {
describe('escapeXmlAttr', () => {
it('escapes &, <, >, ", and \' characters correctly', () => {
expect(escapeXmlAttr('Alice & Bob <Admin> "v1" \'test\'')).toBe(
'Alice &amp; Bob &lt;Admin&gt; &quot;v1&quot; &apos;test&apos;',
)
})
it('handles clean strings without modification', () => {
expect(escapeXmlAttr('normal-string_123.mp4')).toBe('normal-string_123.mp4')
})
})
describe('Tag Serialization', () => {
it('returns empty string for null, undefined, or empty context', () => {
expect(serializeContextToXml(null)).toBe('')
expect(serializeContextToXml(undefined)).toBe('')
expect(serializeContextToXml({})).toBe('')
})
it('serializes minimal context with only user', () => {
const context: ShumaiMessageContext = {
user: { id: 'usr_1', name: 'Alice', role: 'editor' },
}
expect(serializeContextToXml(context)).toBe(
'<context>\n <user id="usr_1" name="Alice" role="editor" />\n</context>',
)
})
it('serializes context with user and thread tag with reply_count', () => {
const context: ShumaiMessageContext = {
user: { id: 'usr_1', name: 'Alice', role: 'editor' },
thread: { id: 'th_01JXYZ', replyCount: 3 },
}
expect(serializeContextToXml(context)).toBe(
'<context>\n <user id="usr_1" name="Alice" role="editor" />\n <thread id="th_01JXYZ" reply_count="3" />\n</context>',
)
})
it('serializes context with thread tag without reply_count', () => {
const context: ShumaiMessageContext = {
thread: { id: 'th_01JXYZ' },
}
expect(serializeContextToXml(context)).toBe(
'<context>\n <thread id="th_01JXYZ" />\n</context>',
)
})
it('serializes minimal context with only currentAsset (without ancestors)', () => {
const context: ShumaiMessageContext = {
currentAsset: {
id: 'ast_1',
name: 'clip.mp4',
type: 'file',
mediaType: 'video',
path: 'clip.mp4',
},
}
expect(serializeContextToXml(context)).toBe(
'<context>\n <current_asset id="ast_1" name="clip.mp4" type="file" media_type="video" path="clip.mp4" />\n</context>',
)
})
it('serializes full video context with duration, timestamp, annotation, attached files, and referenced assets', () => {
const context: ShumaiMessageContext = {
user: { id: 'usr_01', name: 'Alice Chen', role: 'editor' },
currentAsset: {
id: 'ast_01JC04_CURRENT',
name: 'product_launch_v2.mp4',
type: 'file',
mediaType: 'video',
parentId: 'ast_01JC03_PARENT',
path: 'Marketing/2026_Launch/Footage/product_launch_v2.mp4',
durationSeconds: 85.4,
ancestors: [
{ id: 'ast_01JC01_ROOT', name: 'Marketing' },
{ id: 'ast_01JC02_SUB', name: '2026_Launch' },
{ id: 'ast_01JC03_PARENT', name: 'Footage' },
],
},
position: { type: 'time', seconds: 14.25 },
annotation: true,
attachedFiles: [
{
id: 'ast_01JC05_ATT1',
name: 'brand_guidelines_2026.pdf',
type: 'file',
mediaType: 'pdf',
path: 'Brand/brand_guidelines_2026.pdf',
},
],
referencedAssets: [
{
id: 'ast_01JC07_REF2',
name: 'Color_LUTs',
type: 'folder',
path: 'Assets/Color_LUTs',
},
{
id: 'ast_01JC06_REF1',
name: 'logo_white.png',
type: 'file',
mediaType: 'image',
path: 'Brand/Logos/logo_white.png',
},
],
}
const expected = `<context>
<user id="usr_01" name="Alice Chen" role="editor" />
<current_asset id="ast_01JC04_CURRENT" name="product_launch_v2.mp4" type="file" media_type="video" parent_id="ast_01JC03_PARENT" path="Marketing/2026_Launch/Footage/product_launch_v2.mp4" duration_seconds="85.40">
<ancestors>
<folder id="ast_01JC01_ROOT" name="Marketing" />
<folder id="ast_01JC02_SUB" name="2026_Launch" />
<folder id="ast_01JC03_PARENT" name="Footage" />
</ancestors>
</current_asset>
<position type="time" seconds="14.2500" />
<annotation />
<attached_files>
<file id="ast_01JC05_ATT1" name="brand_guidelines_2026.pdf" type="file" media_type="pdf" path="Brand/brand_guidelines_2026.pdf" />
</attached_files>
<referenced_assets>
<asset id="ast_01JC06_REF1" name="logo_white.png" type="file" media_type="image" path="Brand/Logos/logo_white.png" />
<folder id="ast_01JC07_REF2" name="Color_LUTs" path="Assets/Color_LUTs" />
</referenced_assets>
</context>`
expect(serializeContextToXml(context)).toBe(expected)
})
it('serializes PDF document context with total_pages and position.type === "page"', () => {
const context: ShumaiMessageContext = {
currentAsset: {
id: 'pdf_1',
name: 'report.pdf',
type: 'file',
mediaType: 'pdf',
totalPages: 12,
},
position: { type: 'page', page: 3 },
}
const expected = `<context>
<current_asset id="pdf_1" name="report.pdf" type="file" media_type="pdf" total_pages="12" />
<position type="page" page="3" />
</context>`
expect(serializeContextToXml(context)).toBe(expected)
})
it('serializes Video context with total_frames and duration_seconds', () => {
const context: ShumaiMessageContext = {
currentAsset: {
id: 'vid_1',
name: 'clip.mp4',
type: 'file',
mediaType: 'video',
mimeType: 'video/mp4',
durationSeconds: 9,
totalFrames: 268,
},
}
const expected = `<context>
<current_asset id="vid_1" name="clip.mp4" type="file" media_type="video" mime_type="video/mp4" duration_seconds="9.00" total_frames="268" />
</context>`
expect(serializeContextToXml(context)).toBe(expected)
})
it('serializes navigation context with navigated="true" and ancestors hierarchy', () => {
const context: ShumaiMessageContext = {
currentAsset: {
id: 'f_sub',
name: 'Subfolder',
type: 'folder',
navigated: true,
ancestors: [{ id: 'f_root', name: 'Root' }],
},
}
const expected = `<context>
<current_asset id="f_sub" name="Subfolder" type="folder" navigated="true">
<ancestors>
<folder id="f_root" name="Root" />
</ancestors>
</current_asset>
</context>`
expect(serializeContextToXml(context)).toBe(expected)
})
it('serializes mime_type alongside media_type for current asset and attached files', () => {
const context: ShumaiMessageContext = {
currentAsset: {
id: 'img_1',
name: 'photo.png',
type: 'file',
mediaType: 'image',
mimeType: 'image/png',
},
attachedFiles: [
{
id: 'doc_1',
name: 'spec.pdf',
type: 'file',
mediaType: 'pdf',
mimeType: 'application/pdf',
},
],
}
const expected = `<context>
<current_asset id="img_1" name="photo.png" type="file" media_type="image" mime_type="image/png" />
<attached_files>
<file id="doc_1" name="spec.pdf" type="file" media_type="pdf" mime_type="application/pdf" />
</attached_files>
</context>`
expect(serializeContextToXml(context)).toBe(expected)
})
})
describe('XML Escaping & Security', () => {
it('escapes user names, asset names, and paths with special characters', () => {
const context: ShumaiMessageContext = {
user: {
id: 'u_1',
name: 'Alice & Bob <Admin>',
role: 'reviewer "lead"',
},
currentAsset: {
id: 'ast_1',
name: 'Final <Cut> "v2" & Test.mp4',
type: 'file',
path: 'Folder A > Folder B & C/clip.mp4',
ancestors: [{ id: 'anc_1', name: 'Folder B & C' }],
},
attachedFiles: [
{
id: 'att_1',
name: "Don't <Touch> & Go.pdf",
type: 'file',
path: "Path 'A' & 'B'/doc.pdf",
},
],
}
const xml = serializeContextToXml(context)
expect(xml).toContain('name="Alice &amp; Bob &lt;Admin&gt;"')
expect(xml).toContain('role="reviewer &quot;lead&quot;"')
expect(xml).toContain('name="Final &lt;Cut&gt; &quot;v2&quot; &amp; Test.mp4"')
expect(xml).toContain('path="Folder A &gt; Folder B &amp; C/clip.mp4"')
expect(xml).toContain('name="Folder B &amp; C"')
expect(xml).toContain('name="Don&apos;t &lt;Touch&gt; &amp; Go.pdf"')
expect(xml).toContain('path="Path &apos;A&apos; &amp; &apos;B&apos;/doc.pdf"')
// Verify no unescaped dangerous characters exist inside attribute quotes
const attrMatch = xml.match(/\w+="([^"]*)"/g)
expect(attrMatch).not.toBeNull()
})
})
describe('Order Stability & Prompt Caching Invariants', () => {
it('produces identical XML regardless of object key order', () => {
const contextA: ShumaiMessageContext = {
user: { role: 'editor', name: 'Alice', id: 'u_1' },
currentAsset: {
name: 'video.mp4',
type: 'file',
id: 'ast_1',
mediaType: 'video',
},
position: { seconds: 12.345, type: 'time' },
}
const contextB: ShumaiMessageContext = {
position: { type: 'time', seconds: 12.345 },
currentAsset: {
id: 'ast_1',
mediaType: 'video',
type: 'file',
name: 'video.mp4',
},
user: { id: 'u_1', name: 'Alice', role: 'editor' },
}
expect(serializeContextToXml(contextA)).toBe(serializeContextToXml(contextB))
})
it('sorts attachedFiles and referencedAssets by id ascending', () => {
const context: ShumaiMessageContext = {
attachedFiles: [
{ id: 'z_file', name: 'Z', type: 'file' },
{ id: 'a_file', name: 'A', type: 'file' },
{ id: 'm_file', name: 'M', type: 'file' },
],
referencedAssets: [
{ id: 'z_asset', name: 'Z', type: 'file' },
{ id: 'a_asset', name: 'A', type: 'folder' },
{ id: 'm_asset', name: 'M', type: 'file' },
],
}
const xml = serializeContextToXml(context)
const fileOrder = [...xml.matchAll(/<file id="([^"]+)"/g)].map((m) => m[1])
const refOrder = [...xml.matchAll(/<(?:asset|folder) id="([^"]+)"/g)].map((m) => m[1])
expect(fileOrder).toEqual(['a_file', 'm_file', 'z_file'])
expect(refOrder).toEqual(['a_asset', 'm_asset', 'z_asset'])
})
it('normalizes float formatting with fixed precision (toFixed(4) for position, toFixed(2) for duration)', () => {
const context1: ShumaiMessageContext = {
currentAsset: { id: '1', name: 'a', type: 'file', durationSeconds: 14 },
position: { type: 'time', seconds: 5.5 },
}
const context2: ShumaiMessageContext = {
currentAsset: { id: '1', name: 'a', type: 'file', durationSeconds: 14.0001 },
position: { type: 'time', seconds: 5.50002 },
}
expect(serializeContextToXml(context1)).toContain('duration_seconds="14.00"')
expect(serializeContextToXml(context1)).toContain('seconds="5.5000"')
expect(serializeContextToXml(context2)).toContain('duration_seconds="14.00"')
expect(serializeContextToXml(context2)).toContain('seconds="5.5000"')
})
it('serializes annotation tag with id when context.id is provided', () => {
const context: ShumaiMessageContext = {
id: '01JABCDEF1234567890',
annotation: true,
}
expect(serializeContextToXml(context)).toBe(
'<context>\n <annotation id="01JABCDEF1234567890" />\n</context>',
)
})
it('escapes special XML characters in annotation id', () => {
const context: ShumaiMessageContext = {
id: 'test<id>&"123"',
annotation: true,
}
expect(serializeContextToXml(context)).toBe(
'<context>\n <annotation id="test&lt;id&gt;&amp;&quot;123&quot;" />\n</context>',
)
})
})
})
@@ -0,0 +1 @@
export { escapeXmlAttr, serializeContextToXml } from '@shumai/dtos'
@@ -7,8 +7,7 @@ import { DatabaseSessionStorage } from './database-session-storage'
import { agentService } from '@shumai/core/src/agent/agent'
import { createAgentSession, type DbProviderInfo } from './index'
import * as sandboxedBashModule from './tools/sandboxed-bash'
import * as analyzeImageModule from './tools/analyze-image'
import * as screenshotModule from './tools/screenshot'
import * as readAssetModule from './tools/read-asset'
describe('DatabaseSessionStorage', () => {
setupTestDbHooks()
@@ -451,9 +450,8 @@ describe('DatabaseSessionStorage', () => {
})
const sessionId = initialStorage.sessionId
// 2. Spy on media tools constructors
const createAnalyzeImageToolSpy = vi.spyOn(analyzeImageModule, 'createAnalyzeImageTool')
const createScreenshotToolSpy = vi.spyOn(screenshotModule, 'createScreenshotTool')
// 2. Spy on media tool constructor
const createReadAssetToolSpy = vi.spyOn(readAssetModule, 'createReadAssetTool')
// 3. Resume the session via createAgentSession with a NEW comment ID
await createAgentSession({
@@ -470,12 +468,11 @@ describe('DatabaseSessionStorage', () => {
providers: mockProviders,
})
// 4. Assert that the tools were instantiated using the new comment ID and user ID
expect(createAnalyzeImageToolSpy).toHaveBeenCalledWith(user.id, 'new-comment-456')
expect(createScreenshotToolSpy).toHaveBeenCalledWith(user.id, 'new-comment-456')
// 4. Assert that the tool was instantiated using user ID
expect(createReadAssetToolSpy).toHaveBeenCalledWith(user.id)
})
it('should use the current comment ID for media tools in subsequent turns (video)', async () => {
it('should instantiate media tools with user ID in subsequent turns (video)', async () => {
const { agent, user, team } = await setupTestData()
// Create a project and asset to satisfy FK constraints
@@ -505,9 +502,8 @@ describe('DatabaseSessionStorage', () => {
})
const sessionId = initialStorage.sessionId
// 2. Spy on media tools constructors
const createAnalyzeImageToolSpy = vi.spyOn(analyzeImageModule, 'createAnalyzeImageTool')
const createScreenshotToolSpy = vi.spyOn(screenshotModule, 'createScreenshotTool')
// 2. Spy on media tool constructor
const createReadAssetToolSpy = vi.spyOn(readAssetModule, 'createReadAssetTool')
// 3. Resume the session via createAgentSession with a NEW comment ID
await createAgentSession({
@@ -524,9 +520,8 @@ describe('DatabaseSessionStorage', () => {
providers: mockProviders,
})
// 4. Assert that the tools were instantiated using the new comment ID and user ID
expect(createScreenshotToolSpy).toHaveBeenCalledWith(user.id, 'new-comment-456')
expect(createAnalyzeImageToolSpy).toHaveBeenCalledWith(user.id, 'new-comment-456')
// 4. Assert that the tool was instantiated using user ID
expect(createReadAssetToolSpy).toHaveBeenCalledWith(user.id)
})
describe('P1 Bug Reproductions', () => {
@@ -662,5 +657,158 @@ describe('DatabaseSessionStorage', () => {
expect(stats.totalTokens).toBe(170)
expect(stats.costTotal).toBeCloseTo(0.032)
})
it('should save user message turn as custom_message (shumai_message) in DB when currentMessageContext is set', async () => {
const { agent, user } = await setupTestData()
const storage = await DatabaseSessionStorage.create({ agentId: agent.id, userId: user.id })
storage.currentMessageContext = {
user: { id: user.id, name: user.name, role: 'owner' },
currentAsset: { id: 'asset-123', name: 'video.mp4', type: 'file' },
}
const userEntry: SessionTreeEntry = {
type: 'message',
id: 'msg-user-1',
parentId: null,
timestamp: new Date().toISOString(),
message: {
role: 'user',
content: 'Hello, help me analyze this video',
timestamp: Date.now(),
},
}
await storage.appendEntry(userEntry)
// 1. Verify DB record type is 'custom_message', not 'message'
const record = await prisma.agentSessionEntry.findUnique({
where: { id: 'msg-user-1' },
})
expect(record).toBeDefined()
expect(record?.type).toBe('custom_message')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const data = record?.data as any
expect(data.customType).toBe('shumai_message')
expect(data.content).toBe('Hello, help me analyze this video')
expect(data.details?.user?.id).toBe(user.id)
expect(data.details?.currentAsset?.id).toBe('asset-123')
// 2. Verify getEntries() and getPathToRoot() do not throw and return proper custom_message
const entries = await storage.getEntries()
expect(entries).toHaveLength(1)
expect(entries[0].type).toBe('custom_message')
const pathEntries = await storage.getPathToRoot('msg-user-1')
expect(pathEntries).toHaveLength(1)
expect(pathEntries[0].type).toBe('custom_message')
const stats = await storage.getSessionStats()
expect(stats.messageCount).toBe(1)
})
it('should safely handle legacy records where type was message but data was shumai_message', async () => {
const { agent } = await setupTestData()
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
await prisma.agentSessionEntry.create({
data: {
id: 'legacy-corrupted-entry',
sessionId: storage.sessionId,
type: 'message',
data: {
customType: 'shumai_message',
content: 'Legacy prompt',
display: true,
details: { user: { id: 'u1', name: 'User 1' } },
},
},
})
// Must not throw in getEntries(), getPathToRoot(), or getSessionStats()
const entries = await storage.getEntries()
expect(entries).toHaveLength(1)
expect(entries[0].type).toBe('custom_message')
const path = await storage.getPathToRoot('legacy-corrupted-entry')
expect(path).toHaveLength(1)
expect(path[0].type).toBe('custom_message')
const stats = await storage.getSessionStats()
expect(stats.messageCount).toBe(1)
})
it('should dynamically inject thread metadata into details.thread in getPathToRoot when replies exist', async () => {
const { agent, user } = await setupTestData()
const project = await prisma.project.create({
data: { name: 'Test Project', teamId: agent.teamId },
})
const asset = await prisma.asset.create({
data: {
name: 'Test Asset',
type: 'file',
status: 'uploaded',
projectId: project.id,
},
})
const storage = await DatabaseSessionStorage.create({ agentId: agent.id, assetId: asset.id })
const rootComment = await prisma.assetComment.create({
data: {
id: 'comment-root-1',
assetId: asset.id,
message: 'Top-level question',
creatorId: user.id,
},
})
// Add a reply
await prisma.assetComment.create({
data: {
id: 'comment-reply-1',
assetId: asset.id,
message: 'Reply 1',
creatorId: user.id,
replyToId: rootComment.id,
},
})
// Create session entry for root comment
await prisma.agentSessionEntry.create({
data: {
id: rootComment.id,
sessionId: storage.sessionId,
assetId: asset.id,
type: 'custom_message',
data: {
customType: 'shumai_message',
content: 'Top-level question',
display: true,
details: { user: { id: user.id, name: user.name } },
},
},
})
const path = await storage.getPathToRoot(rootComment.id)
expect(path).toHaveLength(1)
expect(path[0].type).toBe('custom_message')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const details = (path[0] as any).details
expect(details.thread).toEqual({
id: rootComment.id,
replyCount: 1,
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((path[0] as any).content).toBe('Top-level question')
const entries = await storage.getEntries()
expect(entries).toHaveLength(1)
expect(entries[0].type).toBe('custom_message')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((entries[0] as any).details?.thread).toEqual({
id: rootComment.id,
replyCount: 1,
})
})
})
})
+109 -54
View File
@@ -8,6 +8,7 @@ import {
type SessionTreeEntry,
} from '@earendil-works/pi-agent-core'
import { agentService } from '@shumai/core/src/agent/agent'
import type { ShumaiMessageContext } from '@shumai/dtos'
export interface DatabaseSessionMetadata extends SessionMetadata {
agentId: string
@@ -72,6 +73,7 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
}
nextEntryId?: string | null
currentMessageContext?: PrismaJson.ShumaiMessageContext
async createEntryId(): Promise<string> {
if (this.nextEntryId) {
@@ -90,9 +92,13 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
data: unknown
}): SessionTreeEntry {
const payload = (record.data as Record<string, unknown>) || {}
let type = (record.type || 'message') as SessionTreeEntry['type']
if (type === 'message' && payload.customType === 'shumai_message') {
type = 'custom_message'
}
return {
id: record.id,
type: (record.type || 'message') as SessionTreeEntry['type'],
type,
parentId: record.parentId,
timestamp: record.createdAt.toISOString(),
...payload,
@@ -100,10 +106,44 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
}
async appendEntry(entry: SessionTreeEntry): Promise<void> {
const strippedEntry = structuredClone(entry)
let entryToProcess = entry
if (
entryToProcess.type === 'message' &&
entryToProcess.message &&
entryToProcess.message.role === 'user' &&
this.currentMessageContext
) {
let contentText = ''
if (typeof entryToProcess.message.content === 'string') {
contentText = entryToProcess.message.content
} else if (Array.isArray(entryToProcess.message.content)) {
contentText = entryToProcess.message.content
.map((c) =>
typeof c === 'object' && c && 'text' in c && typeof c.text === 'string' ? c.text : '',
)
.join('')
}
entryToProcess = {
type: 'custom_message',
id: entryToProcess.id,
parentId: entryToProcess.parentId,
timestamp: entryToProcess.timestamp,
customType: 'shumai_message',
content: contentText,
display: true,
details: {
...this.currentMessageContext,
id: entryToProcess.id,
},
} as unknown as SessionTreeEntry
this.currentMessageContext = undefined
}
const strippedEntry = structuredClone(entryToProcess)
// Strip image data and skill content before saving to DB
if (strippedEntry.type === 'message') {
if (strippedEntry.type === 'message' && strippedEntry.message) {
const msg = strippedEntry.message
if (msg.role === 'toolResult') {
const details = msg.details as { sourceKeys?: string[]; skillId?: string } | undefined
@@ -139,7 +179,10 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
}
}
const targetLeafId = entry.type === 'leaf' ? entry.targetId : entry.id
const targetLeafId =
strippedEntry.type === 'leaf'
? (strippedEntry as unknown as { targetId?: string }).targetId || strippedEntry.id
: strippedEntry.id
// Extract base fields and store type-specific properties in data payload
const payload = { ...strippedEntry } as Record<string, unknown>
@@ -155,17 +198,18 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
await prisma.$transaction([
prisma.agentSessionEntry.upsert({
where: { id: entry.id },
where: { id: strippedEntry.id },
create: {
id: entry.id,
id: strippedEntry.id,
sessionId: this.sessionId,
assetId: session?.assetId || null,
type: entry.type,
parentId: entry.parentId || null,
createdAt: entry.timestamp ? new Date(entry.timestamp) : new Date(),
type: strippedEntry.type,
parentId: strippedEntry.parentId || null,
createdAt: strippedEntry.timestamp ? new Date(strippedEntry.timestamp) : new Date(),
data: payload as PrismaJson.PiSessionEntryData,
},
update: {
type: strippedEntry.type,
data: payload as PrismaJson.PiSessionEntryData,
},
}),
@@ -236,7 +280,9 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
let costTotal = 0
for (const entry of entries) {
if (entry.type === 'message') {
if (entry.type === 'custom_message' && entry.customType === 'shumai_message') {
messageCount++
} else if (entry.type === 'message' && entry.message) {
const msg = entry.message
if (msg.role === 'user' || msg.role === 'assistant') {
messageCount++
@@ -324,48 +370,7 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
await this.reinjectSkillContentAsync(entry)
}
// Dynamically tag comments that currently have reply threads
const entryIds = pathEntries.map((e) => e.id)
if (entryIds.length > 0) {
const threadCounts = await prisma.assetComment.groupBy({
by: ['replyToId'],
where: {
replyToId: { in: entryIds },
message: { not: '__CHAT__' },
},
_count: {
id: true,
},
})
const threadReplyCountMap = new Map<string, number>()
for (const tc of threadCounts) {
if (tc.replyToId) {
threadReplyCountMap.set(tc.replyToId, tc._count.id)
}
}
if (threadReplyCountMap.size > 0) {
for (let i = 0; i < pathEntries.length; i++) {
const entry = pathEntries[i]
const count = threadReplyCountMap.get(entry.id)
if (count !== undefined && count > 0 && entry.type === 'message') {
const cloned = structuredClone(entry)
const msg = cloned.message as unknown as {
content?: Array<{ type: string; text: string }>
}
if (Array.isArray(msg.content) && msg.content[0] && msg.content[0].type === 'text') {
const threadTag = `[Thread ID: ${entry.id}] [Replies: ${count}]`
if (!msg.content[0].text.includes(`[Thread ID: ${entry.id}]`)) {
msg.content[0].text = `${threadTag} ${msg.content[0].text}`
}
}
pathEntries[i] = cloned
}
}
}
}
return pathEntries
return this.injectThreadMetadataAsync(pathEntries)
}
async getEntries(): Promise<SessionTreeEntry[]> {
@@ -378,11 +383,61 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
await this.reinjectImageDataAsync(entry)
await this.reinjectSkillContentAsync(entry)
}
return this.injectThreadMetadataAsync(entries)
}
private async injectThreadMetadataAsync(
entries: SessionTreeEntry[],
): Promise<SessionTreeEntry[]> {
const entryIds = entries.map((e) => e.id)
if (entryIds.length === 0) {
return entries
}
const threadCounts = await prisma.assetComment.groupBy({
by: ['replyToId'],
where: {
replyToId: { in: entryIds },
message: { not: '__CHAT__' },
},
_count: {
id: true,
},
})
const threadReplyCountMap = new Map<string, number>()
for (const tc of threadCounts) {
if (tc.replyToId) {
threadReplyCountMap.set(tc.replyToId, tc._count.id)
}
}
if (threadReplyCountMap.size > 0) {
for (let i = 0; i < entries.length; i++) {
const entry = entries[i]
const count = threadReplyCountMap.get(entry.id)
if (count !== undefined && count > 0) {
if (entry.type === 'custom_message') {
const cloned = structuredClone(entry)
const existingDetails = ((cloned as unknown as { details?: ShumaiMessageContext })
.details || {}) as ShumaiMessageContext
;(cloned as unknown as { details: ShumaiMessageContext }).details = {
...existingDetails,
thread: {
id: entry.id,
replyCount: count,
},
}
entries[i] = cloned
}
}
}
}
return entries
}
private async reinjectImageDataAsync(entry: SessionTreeEntry) {
if (entry.type === 'message') {
if (entry.type === 'message' && entry.message) {
const msg = entry.message as AgentMessage
if (msg.role === 'toolResult') {
const details = msg.details as { sourceKeys?: string[] } | undefined
@@ -407,7 +462,7 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
}
private async reinjectSkillContentAsync(entry: SessionTreeEntry) {
if (entry.type === 'message') {
if (entry.type === 'message' && entry.message) {
const msg = entry.message as AgentMessage
if (msg.role === 'toolResult' && msg.toolName === 'read_skill') {
const details = msg.details as { skillId?: string } | undefined
+28
View File
@@ -1257,3 +1257,31 @@ describe('createAgentSession enabled skill filtering', () => {
}
})
})
describe('getModelFromDb', () => {
it('should prioritize provider active api protocol over model config api', () => {
const providers: DbProviderInfo[] = [
{
name: 'custom-provider',
config: { api: 'anthropic-messages', apiKey: 'KEY' },
models: [
{
modelId: 'my-model',
name: 'My Model',
config: {
api: 'openai-responses', // stale model config
reasoning: false,
input: ['text'],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 128000,
maxTokens: 4096,
},
},
],
},
]
const model = getModelFromDb(providers, 'custom-provider', 'my-model')
expect(model.api).toBe('anthropic-messages')
})
})
+30 -16
View File
@@ -19,17 +19,17 @@ import * as os from 'os'
import * as path from 'path'
import { DatabaseSessionStorage } from './database-session-storage'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { createAnalyzeImageTool } from './tools/analyze-image'
import { createCreateFileTool } from './tools/create-file'
import { createCreateFolderTool } from './tools/create-folder'
import { createCreateVersionTool } from './tools/create-version'
import { createDownloadAssetTool } from './tools/download-asset'
import { createListAssetsTool } from './tools/list-assets'
import { createReadPdfPagesTool } from './tools/read-pdf-pages'
import { createReadAssetTool } from './tools/read-asset'
import { createReadSkillTool } from './tools/read-skill'
import { createReadThreadTool } from './tools/read-thread'
import { createSandboxedBashTool } from './tools/sandboxed-bash'
import { createScreenshotTool } from './tools/screenshot'
import { serializeContextToXml } from './context/serialize-context'
import type { ShumaiMessageContext } from '@shumai/dtos'
export interface DbModelInfo {
modelId: string
@@ -70,10 +70,10 @@ export function getModelFromDb(providers: DbProviderInfo[], providerName: string
if (dbProvider.config.baseUrl) {
m.baseUrl = dbProvider.config.baseUrl
}
Object.assign(m, dbModel.config)
if (dbProvider.config.api) {
m.api = dbProvider.config.api
}
Object.assign(m, dbModel.config)
if (dbModel.name) {
m.name = dbModel.name
}
@@ -292,6 +292,7 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
for (const entry of entries) {
if (
entry.type === 'message' &&
entry.message &&
entry.message.role === 'toolResult' &&
entry.message.toolName === 'read_skill' &&
!entry.message.isError
@@ -309,17 +310,9 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
}
}
const metadata = await storage.getMetadata()
const userCommentId =
passedUserCommentId !== undefined ? passedUserCommentId : metadata.userCommentId
const mediaTools: AgentTool[] = []
if (userId) {
mediaTools.push(
createAnalyzeImageTool(userId, userCommentId),
createScreenshotTool(userId, userCommentId),
createReadPdfPagesTool(userId, userCommentId),
)
mediaTools.push(createReadAssetTool(userId))
}
const agent = await prisma.agent.findUnique({
@@ -445,6 +438,29 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
tools: enabledTools,
})
harness.on('context', async ({ messages }) => {
return {
messages: messages.map((msg) => {
if (
msg.role === 'custom' &&
msg.customType === 'shumai_message' &&
msg.details &&
typeof msg.details === 'object'
) {
const context = msg.details as ShumaiMessageContext
const xml = serializeContextToXml(context)
const text = typeof msg.content === 'string' ? msg.content : ''
const fullContent = xml ? `${text}\n\n${xml}`.trim() : text
return {
...msg,
content: fullContent,
}
}
return msg
}),
}
})
return { session, harness }
}
@@ -613,7 +629,6 @@ import * as aiActivities from './activities/ai'
import { agentAutofillMedia } from './workflows/agent-autofill'
import { agentChat } from './workflows/agent-chat'
import { agentEmbeddingMedia } from './workflows/agent-embedding'
import { agentToolCall } from './workflows/agent-tool-call'
import { queryEmbeddingForSearch } from './workflows/query-embedding-for-search'
export function initAgentWorkflows() {
@@ -621,7 +636,6 @@ export function initAgentWorkflows() {
registerWorkflow(WorkflowTaskType.query_embedding_for_search, queryEmbeddingForSearch)
registerWorkflow(WorkflowTaskType.ai_metadata_autofill, agentAutofillMedia)
registerWorkflow(WorkflowTaskType.chat, agentChat)
registerWorkflow(WorkflowTaskType.agent_tool_call, agentToolCall)
registerActivities(agentActivities)
registerActivities(aiActivities)
@@ -640,8 +654,8 @@ export {
type GenerateTextEmbeddingParams,
} from './activities/ai'
export * from './database-session-storage'
export * from './context/serialize-context'
export * from './workflows/agent-autofill'
export * from './workflows/agent-chat'
export * from './workflows/agent-embedding'
export * from './workflows/agent-tool-call'
export * from './workflows/query-embedding-for-search'
@@ -3,7 +3,8 @@ import { validateToolArguments } from '@earendil-works/pi-ai'
import { createCreateFileTool } from './tools/create-file'
import { fieldsToTypeBoxSchema } from './index'
import { s3Service } from '@shumai/core/src/s3/s3'
import { executeAgentToolWorkflow } from './tools/utils'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService } from '@shumai/core/src/authz/authz'
vi.mock('@shumai/core/src/s3/s3', () => ({
s3Service: {
@@ -12,8 +13,20 @@ vi.mock('@shumai/core/src/s3/s3', () => ({
},
}))
vi.mock('./tools/utils', () => ({
executeAgentToolWorkflow: vi.fn().mockResolvedValue({ id: 'file-1', name: 'test', type: 'file' }),
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
createFile: vi
.fn()
.mockResolvedValue({ id: 'file-1', name: 'test', type: 'file', sizeByte: 100 }),
},
}))
/**
@@ -63,9 +76,16 @@ describe('reproduce null coercion in harness tool validation', () => {
// execute must accept the valid call (data-only) without throwing
const result = await tool.execute('call-1', validated as never)
expect(result.details).toEqual({ id: 'file-1', name: 'test', type: 'file' })
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
id: 'file-1',
name: 'test',
type: 'file',
size: 100,
})
expect(s3Service.putObject).toHaveBeenCalledTimes(1)
expect(executeAgentToolWorkflow).toHaveBeenCalledTimes(1)
expect(authzService.hasPermission).toHaveBeenCalledTimes(1)
expect(assetService.createFile).toHaveBeenCalledTimes(1)
})
it('keeps null metadata values (the "unknown" sentinel) through validation', () => {
@@ -1,175 +0,0 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createAnalyzeImageTool } from './analyze-image'
import {
prisma,
WorkflowTaskStatus,
type Asset,
type AssetComment,
type User,
type WorkflowTask,
} from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { workflowService } from '@shumai/workflow-core'
import { authzService } from '@shumai/core/src/authz/authz'
vi.mock('@shumai/db', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shumai/db')>()
return {
...actual,
prisma: {
user: {
findUnique: vi.fn(),
},
asset: {
findUnique: vi.fn(),
},
assetComment: {
findUnique: vi.fn(),
},
workflowTask: {
create: vi.fn(),
},
},
}
})
vi.mock('@shumai/core/src/s3/s3', () => ({
s3Service: {
getObject: vi.fn(),
},
}))
vi.mock('@shumai/workflow-core', () => ({
workflowService: {
executeWait: vi.fn(),
},
}))
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn(),
},
Permission: { Read: 'Read' },
ResourceType: { Asset: 'asset' },
}))
describe('analyzeImageTool', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('should check user permissions and fetch the image from S3 when comment has no annotations', async () => {
vi.mocked(prisma.user.findUnique).mockResolvedValue({ id: 'user-1' } as User)
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-1',
projectId: 'project-1',
storageKey: { key: 'raw-image.png' },
media: null,
} as unknown as Asset)
vi.mocked(prisma.assetComment.findUnique).mockResolvedValue({
id: 'comment-1',
assetId: 'asset-1',
annotation: null,
} as unknown as AssetComment)
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('fake-image-bytes'),
contentType: 'image/png',
} as unknown as { buffer: Buffer; contentType: string })
const tool = createAnalyzeImageTool('user-1', 'comment-1')
const result = await tool.execute('call-1', { assetId: 'asset-1' })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'asset-1',
})
expect(result.content[0].type).toBe('image')
expect((result.content[0] as unknown as { data: string }).data).toBe(
Buffer.from('fake-image-bytes').toString('base64'),
)
expect(result.details.sourceKeys).toEqual(['raw-image.png'])
expect(prisma.workflowTask.create).not.toHaveBeenCalled()
})
it('should trigger overlay transcode workflow when comment has annotations', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-1',
projectId: 'project-1',
storageKey: { key: 'raw-image.png' },
media: {
imageTranscodes: [{ key: 'transcoded-image.webp' }],
},
} as unknown as Asset)
vi.mocked(prisma.assetComment.findUnique).mockResolvedValue({
id: 'comment-1',
assetId: 'asset-1',
annotation: [
{
type: 'box',
color: '#ff0000',
points: [
[0, 0],
[1, 1],
],
},
],
} as unknown as AssetComment)
vi.mocked(prisma.workflowTask.create).mockResolvedValue({
id: 'task-1',
} as unknown as WorkflowTask)
vi.mocked(workflowService.executeWait).mockResolvedValue({
id: 'task-1',
status: WorkflowTaskStatus.completed,
output: { key: 'annotated-image.webp' },
} as unknown as WorkflowTask)
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('fake-annotated-image-bytes'),
contentType: 'image/webp',
} as unknown as { buffer: Buffer; contentType: string })
const tool = createAnalyzeImageTool('user-1', 'comment-1')
const result = await tool.execute('call-1', { assetId: 'asset-1' })
expect(prisma.workflowTask.create).toHaveBeenCalledWith({
data: {
assetId: 'asset-1',
projectId: 'project-1',
type: 'transcode_image_annotation',
status: 'pending',
payload: {
projectId: 'project-1',
imageAnnotation: {
annotations: [
{
type: 'box',
color: '#ff0000',
points: [
[0, 0],
[1, 1],
],
},
],
},
},
},
})
expect(result.content[0].type).toBe('image')
expect((result.content[0] as unknown as { data: string }).data).toBe(
Buffer.from('fake-annotated-image-bytes').toString('base64'),
)
expect(result.details.sourceKeys).toEqual(['annotated-image.webp'])
})
})
-139
View File
@@ -1,139 +0,0 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { type ImageContent } from '@earendil-works/pi-ai'
import { prisma, WorkflowTaskType, WorkflowTaskStatus, type User } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { workflowService } from '@shumai/workflow-core'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
const analyzeImageSchema = Type.Object({
assetId: Type.String({
description: 'The asset ID of the image to analyze. This parameter is required.',
}),
})
export function createAnalyzeImageTool(
userId: string,
userCommentId?: string | null,
): AgentTool<typeof analyzeImageSchema> {
return {
name: 'analyze_image',
label: 'Analyze Image',
description:
'Retrieves and views the image content. Call this tool if you need to analyze the image.',
parameters: analyzeImageSchema,
execute: async (_toolCallId, params) => {
const assetId = params.assetId
if (!userId) {
throw new Error('User ID is required for authorization.')
}
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: assetId,
})
let asset = await prisma.asset.findUnique({
where: { id: assetId },
include: { storageKey: true },
})
if (!asset) {
throw new Error(`Asset with ID ${assetId} not found.`)
}
if (asset.type === 'version_stack') {
const latestVersion = await prisma.asset.findFirst({
where: { parentId: asset.id, isDeleted: false },
orderBy: { sortIndex: 'asc' },
include: { storageKey: true },
})
if (latestVersion) {
asset = latestVersion
}
}
// Get default media key
let mediaKey = asset.storageKey?.key
if (asset.media) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mediaInfo = asset.media as any
if (mediaInfo.imageTranscodes && mediaInfo.imageTranscodes.length > 0) {
mediaKey = mediaInfo.imageTranscodes[0].key || mediaKey
}
}
if (!mediaKey) {
throw new Error('No media content found for this asset.')
}
// Check if trigger comment has draw annotations for this asset
let annotations: unknown = null
if (userCommentId) {
const comment = await prisma.assetComment.findUnique({
where: { id: userCommentId },
})
if (comment && comment.assetId === assetId && comment.annotation) {
const list = comment.annotation
if (Array.isArray(list) && list.length > 0) {
annotations = list
}
}
}
let keyToUse = mediaKey
if (annotations) {
// Trigger transcode workflow to draw annotation
const task = await prisma.workflowTask.create({
data: {
assetId,
projectId: asset.projectId || 'none',
type: WorkflowTaskType.transcode_image_annotation,
status: WorkflowTaskStatus.pending,
payload: {
projectId: asset.projectId || 'none',
imageAnnotation: {
annotations: annotations as PrismaJson.AnnotationList,
},
},
},
})
const completedTask = await workflowService.executeWait(task)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const output = completedTask.output as any
if (output?.key) {
keyToUse = output.key
} else {
throw new Error('Image annotation transcode workflow failed to output S3 key.')
}
}
// Retrieve the image content from S3
const bucket = process.env.S3_BUCKET || 'shumai'
const { buffer, contentType } = await s3Service.getObject(bucket, keyToUse)
const mimeType =
contentType && contentType !== 'application/octet-stream' ? contentType : 'image/webp'
const content: ImageContent[] = [
{
type: 'image',
data: buffer.toString('base64'),
mimeType,
},
]
return {
content,
details: {
sourceKeys: [keyToUse],
},
}
},
}
}
@@ -0,0 +1,270 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { resolveAnnotationsById } from './annotation-resolver'
import { DatabaseSessionStorage } from '../database-session-storage'
import type { SessionTreeEntry } from '@earendil-works/pi-agent-core'
describe('resolveAnnotationsById', () => {
setupTestDbHooks()
beforeEach(() => {
// DB hooks automatically manage transactions
})
it('returns null annotations and null timestamp when annotationId is undefined or empty', async () => {
const result1 = await resolveAnnotationsById('asset-1', undefined)
expect(result1).toEqual({ annotations: null, timestamp: null })
const result2 = await resolveAnnotationsById('asset-1', '')
expect(result2).toEqual({ annotations: null, timestamp: null })
})
it('resolves annotations and timestamp from AssetComment (Comments Mode)', async () => {
const user = await prisma.user.create({
data: { name: 'Comment Author', email: 'author@test.com', password: 'pw' },
})
const team = await prisma.team.create({
data: { name: 'Test Team' },
})
const project = await prisma.project.create({
data: { name: 'Test Project', teamId: team.id },
})
const asset = await prisma.asset.create({
data: {
name: 'video.mp4',
type: 'file',
projectId: project.id,
status: 'uploaded',
},
})
const comment = await prisma.assetComment.create({
data: {
assetId: asset.id,
creatorId: user.id,
message: 'Look at the highlighted area',
second: 18.5,
annotation: [
{
type: 'line',
color: '#ff0000',
points: [
[10, 10],
[50, 50],
],
},
],
},
})
const result = await resolveAnnotationsById(asset.id, comment.id)
expect(result.timestamp).toBe(18.5)
expect(result.annotations).toEqual([
{
type: 'line',
color: '#ff0000',
points: [
[10, 10],
[50, 50],
],
},
])
})
it('throws an error when AssetComment belongs to a different asset', async () => {
const user = await prisma.user.create({
data: { name: 'Author', email: 'author2@test.com', password: 'pw' },
})
const team = await prisma.team.create({
data: { name: 'Team' },
})
const project = await prisma.project.create({
data: { name: 'Project', teamId: team.id },
})
const assetA = await prisma.asset.create({
data: { name: 'videoA.mp4', type: 'file', projectId: project.id, status: 'uploaded' },
})
const assetB = await prisma.asset.create({
data: { name: 'videoB.mp4', type: 'file', projectId: project.id, status: 'uploaded' },
})
const commentB = await prisma.assetComment.create({
data: {
assetId: assetB.id,
creatorId: user.id,
message: 'Comment on B',
annotation: [
{
type: 'box',
color: '#ff0000',
points: [
[0, 0],
[10, 10],
],
},
],
},
})
await expect(resolveAnnotationsById(assetA.id, commentB.id)).rejects.toThrow(
`Annotation "${commentB.id}" belongs to asset "${assetB.id}", not target asset "${assetA.id}".`,
)
})
it('resolves annotations and timestamp from AgentSessionEntry (1-on-1 Chat Mode)', async () => {
const user = await prisma.user.create({
data: { name: 'Chat User', email: 'chatuser@test.com', password: 'pw' },
})
const team = await prisma.team.create({
data: { name: 'Chat Team' },
})
const project = await prisma.project.create({
data: { name: 'Chat Project', teamId: team.id },
})
const asset = await prisma.asset.create({
data: {
name: 'chat-video.mp4',
type: 'file',
projectId: project.id,
status: 'uploaded',
},
})
const agentUser = await prisma.user.create({
data: { name: 'Chat Bot User', email: 'bot@example.com', type: 'agent' },
})
const agent = await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'chat',
config: { provider: 'test-provider', model: 'test-model' },
},
})
// Create session storage
const storage = await DatabaseSessionStorage.create({
agentId: agent.id,
userId: user.id,
assetId: asset.id,
})
// Set active message context with markup annotations and position
storage.currentMessageContext = {
currentAsset: {
id: asset.id,
name: asset.name,
type: 'file',
path: asset.name,
},
position: { type: 'time', seconds: 12.34 },
annotation: true,
annotations: [
{
type: 'rectangle',
x: 100,
y: 200,
width: 50,
height: 50,
},
],
}
// User sends a message -> DatabaseSessionStorage.appendEntry is called
const userEntry: SessionTreeEntry = {
type: 'message',
id: '01JTESTENTRY1234567890',
parentId: null,
timestamp: new Date().toISOString(),
message: {
role: 'user',
content: 'Check this rectangle markup on video',
timestamp: Date.now(),
},
}
await storage.appendEntry(userEntry)
// Verify DatabaseSessionStorage persisted entry with details.id = entry.id
const dbEntry = await prisma.agentSessionEntry.findUnique({
where: { id: '01JTESTENTRY1234567890' },
})
expect(dbEntry).toBeDefined()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const details = (dbEntry?.data as any)?.details
expect(details.id).toBe('01JTESTENTRY1234567890')
expect(details.position).toEqual({ type: 'time', seconds: 12.34 })
expect(details.annotations).toHaveLength(1)
// Now resolveAnnotationsById directly with the entry id and matching asset.id
const result = await resolveAnnotationsById(asset.id, '01JTESTENTRY1234567890')
expect(result.timestamp).toBe(12.34)
expect(result.annotations).toEqual([
{
type: 'rectangle',
x: 100,
y: 200,
width: 50,
height: 50,
},
])
})
it('throws an error when AgentSessionEntry belongs to a different asset', async () => {
const user = await prisma.user.create({
data: { name: 'User', email: 'user3@test.com', password: 'pw' },
})
const team = await prisma.team.create({
data: { name: 'Team' },
})
const project = await prisma.project.create({
data: { name: 'Project', teamId: team.id },
})
const assetA = await prisma.asset.create({
data: { name: 'chatA.mp4', type: 'file', projectId: project.id, status: 'uploaded' },
})
const assetB = await prisma.asset.create({
data: { name: 'chatB.mp4', type: 'file', projectId: project.id, status: 'uploaded' },
})
const agentUser = await prisma.user.create({
data: { name: 'Bot User', email: 'bot2@example.com', type: 'agent' },
})
const agent = await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'chat',
config: { provider: 'test-provider', model: 'test-model' },
},
})
const storage = await DatabaseSessionStorage.create({
agentId: agent.id,
userId: user.id,
assetId: assetB.id,
})
storage.currentMessageContext = {
currentAsset: { id: assetB.id, name: assetB.name, type: 'file', path: assetB.name },
annotation: true,
annotations: [{ type: 'box' }],
}
const userEntry: SessionTreeEntry = {
type: 'message',
id: '01JENTRYONASSETB123456',
parentId: null,
timestamp: new Date().toISOString(),
message: { role: 'user', content: 'Markup on B', timestamp: Date.now() },
}
await storage.appendEntry(userEntry)
await expect(resolveAnnotationsById(assetA.id, '01JENTRYONASSETB123456')).rejects.toThrow(
`Annotation "01JENTRYONASSETB123456" belongs to asset "${assetB.id}", not target asset "${assetA.id}".`,
)
})
it('throws an error if annotationId does not match any record', async () => {
await expect(resolveAnnotationsById('asset-1', 'non_existent_id')).rejects.toThrow(
'Annotation with ID "non_existent_id" not found.',
)
})
})
@@ -0,0 +1,52 @@
import { prisma } from '@shumai/db'
import type { ShumaiMessageContext } from '@shumai/dtos'
export async function resolveAnnotationsById(
assetId: string,
annotationId?: string | null,
): Promise<{ annotations: PrismaJson.AnnotationList | null; timestamp: number | null }> {
if (!annotationId) {
return { annotations: null, timestamp: null }
}
// 1. Check AssetComment table (Comments Mode)
const comment = await prisma.assetComment.findUnique({
where: { id: annotationId },
select: { assetId: true, annotation: true, second: true },
})
if (comment) {
if (comment.assetId !== assetId) {
throw new Error(
`Annotation "${annotationId}" belongs to asset "${comment.assetId}", not target asset "${assetId}".`,
)
}
const list = comment.annotation
const annotations =
Array.isArray(list) && list.length > 0 ? (list as PrismaJson.AnnotationList) : null
return { annotations, timestamp: comment.second }
}
// 2. Check AgentSessionEntry table (1-on-1 Chat Mode)
const entry = await prisma.agentSessionEntry.findUnique({
where: { id: annotationId },
select: { assetId: true, data: true },
})
if (entry) {
const details = (entry.data as Record<string, unknown> | null)?.details as
| ShumaiMessageContext
| undefined
const entryAssetId = entry.assetId || details?.currentAsset?.id
if (entryAssetId && entryAssetId !== assetId) {
throw new Error(
`Annotation "${annotationId}" belongs to asset "${entryAssetId}", not target asset "${assetId}".`,
)
}
const list = details?.annotations
const annotations =
Array.isArray(list) && list.length > 0 ? (list as unknown as PrismaJson.AnnotationList) : null
const timestamp = details?.position?.type === 'time' ? details.position.seconds : null
return { annotations, timestamp }
}
throw new Error(`Annotation with ID "${annotationId}" not found.`)
}
+68 -46
View File
@@ -2,7 +2,8 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { createCreateFileTool } from './create-file'
import { fieldsToTypeBoxSchema } from '../index'
import { s3Service } from '@shumai/core/src/s3/s3'
import { executeAgentToolWorkflow } from './utils'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService } from '@shumai/core/src/authz/authz'
import { Value } from 'typebox/value'
import * as fs from 'fs'
import * as os from 'os'
@@ -15,8 +16,20 @@ vi.mock('@shumai/core/src/s3/s3', () => ({
},
}))
vi.mock('./utils', () => ({
executeAgentToolWorkflow: vi.fn().mockResolvedValue({ id: 'file-1', name: 'test', type: 'file' }),
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
createFile: vi
.fn()
.mockResolvedValue({ id: 'file-1', name: 'test', type: 'file', sizeByte: 100 }),
},
}))
describe('createCreateFileTool', () => {
@@ -56,23 +69,33 @@ describe('createCreateFileTool', () => {
expect(s3Key).toMatch(/^files\/.+\.md$/)
expect(uploadArgs[2]).toBe('text/markdown')
expect(executeAgentToolWorkflow).toHaveBeenCalledWith({
toolName: 'create_file',
args: {
parent: 'folder-1',
s3Key,
name: 'create-file-test.md',
size: Buffer.byteLength('# Hello from disk', 'utf-8'),
contentType: 'text/markdown',
},
userId: 'user-1',
assetId: 'folder-1',
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'edit',
type: 'asset',
id: 'folder-1',
})
expect(result.details).toEqual({ id: 'file-1', name: 'test', type: 'file' })
expect(assetService.createFile).toHaveBeenCalledWith({
parentId: 'folder-1',
name: 'create-file-test.md',
key: s3Key,
sizeByte: Buffer.byteLength('# Hello from disk', 'utf-8'),
contentType: 'text/markdown',
creatorId: 'user-1',
metadata: undefined,
})
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
id: 'file-1',
name: 'test',
type: 'file',
size: 100,
})
})
it('should forward the metadata to the agent tool workflow when provided', async () => {
it('should forward the metadata to assetService.createFile when provided', async () => {
const filePath = createTempFile('# Hello from disk', 'create-file-metadata.md')
const metadataSchema = fieldsToTypeBoxSchema([
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
@@ -86,9 +109,9 @@ describe('createCreateFileTool', () => {
metadata: { prompt: 'Generated using gemini' },
})
expect(executeAgentToolWorkflow).toHaveBeenCalledWith(
expect(assetService.createFile).toHaveBeenCalledWith(
expect.objectContaining({
args: expect.objectContaining({ metadata: { prompt: 'Generated using gemini' } }),
metadata: { prompt: 'Generated using gemini' },
}),
)
})
@@ -125,19 +148,17 @@ describe('createCreateFileTool', () => {
},
})
expect(executeAgentToolWorkflow).toHaveBeenCalledWith(
expect(assetService.createFile).toHaveBeenCalledWith(
expect.objectContaining({
args: expect.objectContaining({
metadata: {
provider: { newOption: { value: 'Kling' } },
tags: ['tag1', { newOption: { value: 'Tag 2' } }],
},
}),
metadata: {
provider: { newOption: { value: 'Kling' } },
tags: ['tag1', { newOption: { value: 'Tag 2' } }],
},
}),
)
})
it('should not include metadata in args when metadata is null', async () => {
it('should not include metadata when metadata is null', async () => {
const filePath = createTempFile('# Hello from disk', 'create-file-no-metadata.md')
const metadataSchema = fieldsToTypeBoxSchema([
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
@@ -151,8 +172,8 @@ describe('createCreateFileTool', () => {
metadata: null,
})
const callArgs = vi.mocked(executeAgentToolWorkflow).mock.calls[0][0].args
expect(callArgs).not.toHaveProperty('metadata')
const callArgs = vi.mocked(assetService.createFile).mock.calls[0][0]
expect(callArgs.metadata).toBeUndefined()
})
it('should throw when the local file does not exist', async () => {
@@ -161,7 +182,7 @@ describe('createCreateFileTool', () => {
tool.execute('call-1', { parent: 'folder-1', path: '/nonexistent/file.md', data: null }),
).rejects.toThrow('Local file not found at path: /nonexistent/file.md')
expect(s3Service.uploadFileToKey).not.toHaveBeenCalled()
expect(executeAgentToolWorkflow).not.toHaveBeenCalled()
expect(assetService.createFile).not.toHaveBeenCalled()
})
it('should create a file directly from name and content', async () => {
@@ -184,20 +205,23 @@ describe('createCreateFileTool', () => {
expect(size).toBe(Buffer.byteLength('# Notes\n\nHello', 'utf-8'))
expect(contentType).toBe('text/markdown')
expect(executeAgentToolWorkflow).toHaveBeenCalledWith({
toolName: 'create_file',
args: {
parent: 'folder-1',
s3Key,
name: 'notes.md',
size: Buffer.byteLength('# Notes\n\nHello', 'utf-8'),
contentType: 'text/markdown',
},
userId: 'user-1',
assetId: 'folder-1',
expect(assetService.createFile).toHaveBeenCalledWith({
parentId: 'folder-1',
name: 'notes.md',
key: s3Key,
sizeByte: Buffer.byteLength('# Notes\n\nHello', 'utf-8'),
contentType: 'text/markdown',
creatorId: 'user-1',
metadata: undefined,
})
expect(result.details).toEqual({ id: 'file-1', name: 'test', type: 'file' })
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
id: 'file-1',
name: 'test',
type: 'file',
size: 100,
})
})
it('should default unknown extensions to text/plain when creating from content', async () => {
@@ -235,10 +259,8 @@ describe('createCreateFileTool', () => {
]
expect(s3Key).toContain('my_notes.md')
expect(executeAgentToolWorkflow).toHaveBeenCalledWith(
expect.objectContaining({
args: expect.objectContaining({ name: 'my_notes.md' }),
}),
expect(assetService.createFile).toHaveBeenCalledWith(
expect.objectContaining({ name: 'my_notes.md' }),
)
})
@@ -260,7 +282,7 @@ describe('createCreateFileTool', () => {
).rejects.toThrow('Provide exactly one of "path" (a local file) or "data" (name and content).')
expect(s3Service.uploadFileToKey).not.toHaveBeenCalled()
expect(s3Service.putObject).not.toHaveBeenCalled()
expect(executeAgentToolWorkflow).not.toHaveBeenCalled()
expect(assetService.createFile).not.toHaveBeenCalled()
})
it('should expose a strict, all-required (nullable) metadata schema when fields are provided', () => {
+27 -14
View File
@@ -1,12 +1,14 @@
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { s3Service } from '@shumai/core/src/s3/s3'
import { getFileMimeType, readFileMimeType } from '@shumai/core/src/utils/file-mime'
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
import { type User } from '@shumai/db'
import { Type, type TSchema } from 'typebox'
import * as fs from 'fs'
import * as path from 'path'
import { ulid } from 'ulid'
import { executeAgentToolWorkflow } from './utils'
interface CreateFileToolParams {
parent: string
@@ -105,22 +107,33 @@ export function createCreateFileTool(userId: string, metadataSchema?: TSchema):
await s3Service.putObject(process.env.S3_BUCKET || 'shumai', s3Key, content, size, mimeType)
}
const result = await executeAgentToolWorkflow({
toolName: 'create_file',
args: {
parent: p.parent,
s3Key,
name,
size,
contentType: mimeType,
...(p.metadata ? { metadata: p.metadata } : {}),
},
userId,
assetId: p.parent,
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Edit,
type: ResourceType.Asset,
id: p.parent,
})
const asset = await assetService.createFile({
parentId: p.parent,
name,
key: s3Key,
sizeByte: size,
contentType: mimeType,
creatorId: userId,
metadata: p.metadata ?? undefined,
})
const result = {
id: asset.id,
name: asset.name,
type: asset.type,
size: Number(asset.sizeByte),
}
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
details: result,
details: {},
}
},
}
@@ -0,0 +1,56 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createCreateFolderTool } from './create-folder'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService } from '@shumai/core/src/authz/authz'
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
createAsset: vi.fn().mockResolvedValue({
id: 'folder-1',
name: 'New Folder',
type: 'folder',
sizeByte: 0,
}),
},
}))
describe('createCreateFolderTool', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('checks edit permission and calls assetService.createAsset with folder type', async () => {
const tool = createCreateFolderTool('user-1')
const result = await tool.execute('call-1', { parent: 'root-folder', name: 'New Folder' })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'edit',
type: 'asset',
id: 'root-folder',
})
expect(assetService.createAsset).toHaveBeenCalledWith({
name: 'New Folder',
parentId: 'root-folder',
type: 'folder',
creatorId: 'user-1',
})
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
id: 'folder-1',
name: 'New Folder',
type: 'folder',
size: 0,
})
})
})
+24 -7
View File
@@ -1,6 +1,8 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { executeAgentToolWorkflow } from './utils'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { type User } from '@shumai/db'
const createFolderSchema = Type.Object({
parent: Type.String({
@@ -19,15 +21,30 @@ export function createCreateFolderTool(userId: string): AgentTool<typeof createF
description: 'Create a new folder in a specified parent folder.',
parameters: createFolderSchema,
execute: async (_toolCallId, params) => {
const result = await executeAgentToolWorkflow({
toolName: 'create_folder',
args: params,
userId,
assetId: params.parent,
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Edit,
type: ResourceType.Asset,
id: params.parent,
})
const newFolder = await assetService.createAsset({
name: params.name,
parentId: params.parent,
type: 'folder',
creatorId: userId,
})
const result = {
id: newFolder.id,
name: newFolder.name,
type: newFolder.type,
size: Number(newFolder.sizeByte),
}
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
details: result,
details: {},
}
},
}
+51 -30
View File
@@ -2,7 +2,8 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { createCreateVersionTool } from './create-version'
import { fieldsToTypeBoxSchema } from '../index'
import { s3Service } from '@shumai/core/src/s3/s3'
import { executeAgentToolWorkflow } from './utils'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService } from '@shumai/core/src/authz/authz'
import { Value } from 'typebox/value'
import * as fs from 'fs'
import * as os from 'os'
@@ -14,8 +15,20 @@ vi.mock('@shumai/core/src/s3/s3', () => ({
},
}))
vi.mock('./utils', () => ({
executeAgentToolWorkflow: vi.fn().mockResolvedValue({ id: 'file-1', name: 'v2', type: 'file' }),
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
createVersion: vi
.fn()
.mockResolvedValue({ id: 'file-1', name: 'v2', type: 'file', sizeByte: 100 }),
},
}))
describe('createCreateVersionTool', () => {
@@ -41,7 +54,7 @@ describe('createCreateVersionTool', () => {
return filePath
}
it('should upload a local file and create a version via the agent tool workflow', async () => {
it('should upload a local file and create a version via assetService.createVersion', async () => {
const filePath = createTempFile('# Hello from disk', 'create-version-test.md')
const tool = createCreateVersionTool('user-1')
@@ -53,23 +66,33 @@ describe('createCreateVersionTool', () => {
expect(s3Key).toMatch(/^files\/.+\.md$/)
expect(uploadArgs[2]).toBe('text/markdown')
expect(executeAgentToolWorkflow).toHaveBeenCalledWith({
toolName: 'create_version',
args: {
parent: 'file-1',
s3Key,
name: 'create-version-test.md',
size: Buffer.byteLength('# Hello from disk', 'utf-8'),
contentType: 'text/markdown',
},
userId: 'user-1',
assetId: 'file-1',
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'edit',
type: 'asset',
id: 'file-1',
})
expect(result.details).toEqual({ id: 'file-1', name: 'v2', type: 'file' })
expect(assetService.createVersion).toHaveBeenCalledWith({
parentId: 'file-1',
name: 'create-version-test.md',
key: s3Key,
sizeByte: Buffer.byteLength('# Hello from disk', 'utf-8'),
contentType: 'text/markdown',
creatorId: 'user-1',
metadata: undefined,
})
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
id: 'file-1',
name: 'v2',
type: 'file',
size: 100,
})
})
it('should forward the metadata to the agent tool workflow when provided', async () => {
it('should forward the metadata to assetService.createVersion when provided', async () => {
const filePath = createTempFile('# Hello from disk', 'create-version-metadata.md')
const metadataSchema = fieldsToTypeBoxSchema([
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
@@ -82,9 +105,9 @@ describe('createCreateVersionTool', () => {
metadata: { prompt: 'Generated using gemini' },
})
expect(executeAgentToolWorkflow).toHaveBeenCalledWith(
expect(assetService.createVersion).toHaveBeenCalledWith(
expect.objectContaining({
args: expect.objectContaining({ metadata: { prompt: 'Generated using gemini' } }),
metadata: { prompt: 'Generated using gemini' },
}),
)
})
@@ -120,19 +143,17 @@ describe('createCreateVersionTool', () => {
},
})
expect(executeAgentToolWorkflow).toHaveBeenCalledWith(
expect(assetService.createVersion).toHaveBeenCalledWith(
expect.objectContaining({
args: expect.objectContaining({
metadata: {
provider: { newOption: { value: 'Kling' } },
tags: ['tag1', { newOption: { value: 'Tag 2' } }],
},
}),
metadata: {
provider: { newOption: { value: 'Kling' } },
tags: ['tag1', { newOption: { value: 'Tag 2' } }],
},
}),
)
})
it('should not include metadata in args when metadata is null', async () => {
it('should not include metadata when metadata is null', async () => {
const filePath = createTempFile('# Hello from disk', 'create-version-no-metadata.md')
const metadataSchema = fieldsToTypeBoxSchema([
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
@@ -141,8 +162,8 @@ describe('createCreateVersionTool', () => {
const tool = createCreateVersionTool('user-1', metadataSchema)
await tool.execute('call-1', { parent: 'file-1', path: filePath, metadata: null })
const callArgs = vi.mocked(executeAgentToolWorkflow).mock.calls[0][0].args
expect(callArgs).not.toHaveProperty('metadata')
const callArgs = vi.mocked(assetService.createVersion).mock.calls[0][0]
expect(callArgs.metadata).toBeUndefined()
})
it('should throw when the local file does not exist', async () => {
@@ -151,7 +172,7 @@ describe('createCreateVersionTool', () => {
tool.execute('call-1', { parent: 'file-1', path: '/nonexistent/file.md' }),
).rejects.toThrow('Local file not found at path: /nonexistent/file.md')
expect(s3Service.uploadFileToKey).not.toHaveBeenCalled()
expect(executeAgentToolWorkflow).not.toHaveBeenCalled()
expect(assetService.createVersion).not.toHaveBeenCalled()
})
it('should expose a strict, all-required (nullable) metadata schema when fields are provided', () => {
+33 -19
View File
@@ -1,12 +1,14 @@
import { Type, type TSchema } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { executeAgentToolWorkflow } from './utils'
import * as fs from 'fs'
import * as path from 'path'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { s3Service } from '@shumai/core/src/s3/s3'
import { readFileMimeType } from '@shumai/core/src/utils/file-mime'
import { ulid } from 'ulid'
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
import { type User } from '@shumai/db'
import { Type, type TSchema } from 'typebox'
import * as fs from 'fs'
import * as path from 'path'
import { ulid } from 'ulid'
interface CreateVersionToolParams {
parent: string
@@ -48,27 +50,39 @@ export function createCreateVersionTool(userId: string, metadataSchema?: TSchema
const fileSize = fs.statSync(absolutePath).size
const mimeType = readFileMimeType(absolutePath)
const name = path.basename(absolutePath)
// Generate compliant S3 key matching normal file upload format
const s3Key = `files/${ulid()}/${sanitizeFilename(path.basename(absolutePath))}`
const s3Key = `files/${ulid()}/${sanitizeFilename(name)}`
await s3Service.uploadFileToKey(absolutePath, s3Key, mimeType)
const result = await executeAgentToolWorkflow({
toolName: 'create_version',
args: {
parent: p.parent,
s3Key,
name: path.basename(absolutePath),
size: fileSize,
contentType: mimeType,
...(p.metadata ? { metadata: p.metadata } : {}),
},
userId,
assetId: p.parent,
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Edit,
type: ResourceType.Asset,
id: p.parent,
})
const asset = await assetService.createVersion({
parentId: p.parent,
name,
key: s3Key,
sizeByte: fileSize,
contentType: mimeType,
creatorId: userId,
metadata: p.metadata ?? undefined,
})
const result = {
id: asset.id,
name: asset.name,
type: asset.type,
size: Number(asset.sizeByte),
}
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
details: result,
details: {},
}
},
}
+272 -143
View File
@@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { createDownloadAssetTool } from './download-asset'
import { prisma, type Asset } from '@shumai/db'
import { createDownloadAssetTool, resolveAssetIdFromKey } from './download-asset'
import { prisma, type Asset, type StorageKey } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { authzService } from '@shumai/core/src/authz/authz'
import * as fs from 'fs'
@@ -15,6 +15,10 @@ vi.mock('@shumai/db', async (importOriginal) => {
findUnique: vi.fn(),
findFirst: vi.fn(),
},
storageKey: {
findFirst: vi.fn(),
findUnique: vi.fn(),
},
},
}
})
@@ -52,163 +56,288 @@ describe('downloadAssetTool', () => {
it('should throw authorization error if user ID is empty', async () => {
const tool = createDownloadAssetTool('')
await expect(tool.execute('call-1', { assetId: 'asset-1' })).rejects.toThrow(
await expect(tool.execute('call-1', { assetId: 'asset-1', key: null })).rejects.toThrow(
'User ID is required for authorization.',
)
})
it('should throw error if asset not found', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
it('should throw error if both assetId and key are null', async () => {
const tool = createDownloadAssetTool('user-1')
await expect(tool.execute('call-1', { assetId: 'asset-1' })).rejects.toThrow(
'Asset with ID asset-1 not found.',
await expect(tool.execute('call-1', { assetId: null, key: null })).rejects.toThrow(
'Provide exactly one of "assetId" (to download a workspace asset) or "key" (to download a specific S3 storage key). Set the unused parameter to null.',
)
})
it('should download proxy image to .pi directory when imageTranscodes is present', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-img-1',
name: 'sample_photo.jpg',
storageKey: { key: 'raw/sample_photo.jpg' },
media: {
proxyType: 'image',
imageTranscodes: [{ key: 'proxy/sample_photo.webp' }],
},
} as unknown as Asset)
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('fake-proxy-image-bytes'),
contentType: 'image/webp',
} as unknown as { buffer: Buffer; contentType: string })
it('should throw error if both assetId and key are provided', async () => {
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', { assetId: 'asset-img-1' })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'asset-img-1',
})
expect(s3Service.getObject).toHaveBeenCalledWith('shumai', 'proxy/sample_photo.webp')
const expectedPath = path.join(piDir, 'asset-img-1_sample_photo.jpg')
createdFiles.push(expectedPath)
expect(fs.existsSync(expectedPath)).toBe(true)
expect(fs.readFileSync(expectedPath, 'utf-8')).toBe('fake-proxy-image-bytes')
expect(result.details.filePath).toBe(path.join('.pi', 'asset-img-1_sample_photo.jpg'))
expect(result.details.contentType).toBe('image/webp')
expect((result.content[0] as { type: 'text'; text: string }).text).toContain(
'Remember to delete this temporary file when finished.',
await expect(
tool.execute('call-1', { assetId: 'asset-1', key: 'files/path/file.png' }),
).rejects.toThrow(
'Provide exactly one of "assetId" (to download a workspace asset) or "key" (to download a specific S3 storage key). Set the unused parameter to null.',
)
})
it('should download proxy video to .pi directory when videoTranscodes is present', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-vid-1',
name: 'video_test.mov',
storageKey: { key: 'raw/video_test.mov' },
media: {
proxyType: 'video',
videoTranscodes: [{ key: 'proxy/video_test.mp4' }],
},
} as unknown as Asset)
describe('Download by assetId', () => {
it('should throw error if asset not found', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('fake-video-bytes'),
contentType: 'video/mp4',
} as unknown as { buffer: Buffer; contentType: string })
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', { assetId: 'asset-vid-1' })
expect(s3Service.getObject).toHaveBeenCalledWith('shumai', 'proxy/video_test.mp4')
const expectedPath = path.join(piDir, 'asset-vid-1_video_test.mov')
createdFiles.push(expectedPath)
expect(fs.existsSync(expectedPath)).toBe(true)
expect(result.details.size).toBe(Buffer.from('fake-video-bytes').length)
})
it('should download original file to .pi directory even when pdfTranscode is present', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-md-1',
name: 'doc.md',
storageKey: { key: 'raw/doc.md' },
media: {
proxyType: 'pdf',
pdfTranscode: { key: 'proxy/doc.pdf' },
original: { key: 'raw/doc.md' },
},
} as unknown as Asset)
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('# fake markdown bytes'),
contentType: 'text/markdown',
} as unknown as { buffer: Buffer; contentType: string })
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', { assetId: 'asset-md-1' })
expect(s3Service.getObject).toHaveBeenCalledWith('shumai', 'raw/doc.md')
const expectedPath = path.join(piDir, 'asset-md-1_doc.md')
createdFiles.push(expectedPath)
expect(fs.existsSync(expectedPath)).toBe(true)
expect(result.details.contentType).toBe('text/markdown')
})
it('should resolve version_stack asset to its latest version file', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'stack-1',
name: '',
type: 'version_stack',
storageKey: null,
media: null,
} as unknown as Asset)
vi.mocked(prisma.asset.findFirst).mockResolvedValue({
id: 'file-ver-2',
name: 'banana_pig.png',
type: 'file',
storageKey: { key: 'raw/banana_pig.png' },
media: {
proxyType: 'image',
imageTranscodes: [{ key: 'proxy/banana_pig.webp' }],
},
} as unknown as Asset)
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('fake-version-bytes'),
contentType: 'image/webp',
} as unknown as { buffer: Buffer; contentType: string })
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', { assetId: 'stack-1' })
expect(prisma.asset.findFirst).toHaveBeenCalledWith({
where: { parentId: 'stack-1', isDeleted: false },
orderBy: { sortIndex: 'asc' },
include: { storageKey: true },
const tool = createDownloadAssetTool('user-1')
await expect(tool.execute('call-1', { assetId: 'asset-1', key: null })).rejects.toThrow(
'Asset with ID asset-1 not found.',
)
})
expect(s3Service.getObject).toHaveBeenCalledWith('shumai', 'proxy/banana_pig.webp')
it('should download proxy image to .pi directory when downloading by assetId', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-img-1',
name: 'sample_photo.jpg',
storageKey: { key: 'raw/sample_photo.jpg' },
media: {
proxyType: 'image',
imageTranscodes: [{ key: 'proxy/sample_photo.webp' }],
},
} as unknown as Asset)
const expectedPath = path.join(piDir, 'file-ver-2_banana_pig.png')
createdFiles.push(expectedPath)
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('fake-proxy-image-bytes'),
contentType: 'image/webp',
} as unknown as { buffer: Buffer; contentType: string })
expect(fs.existsSync(expectedPath)).toBe(true)
expect(result.details.name).toBe('banana_pig.png')
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', { assetId: 'asset-img-1', key: null })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'asset-img-1',
})
expect(s3Service.getObject).toHaveBeenCalledWith('shumai', 'proxy/sample_photo.webp')
const expectedPath = path.join(piDir, 'asset-img-1_sample_photo.jpg')
createdFiles.push(expectedPath)
expect(fs.existsSync(expectedPath)).toBe(true)
expect(fs.readFileSync(expectedPath, 'utf-8')).toBe('fake-proxy-image-bytes')
expect(result.details.filePath).toBe(path.join('.pi', 'asset-img-1_sample_photo.jpg'))
expect(result.details.contentType).toBe('image/webp')
expect((result.content[0] as { type: 'text'; text: string }).text).toContain(
'Remember to delete this temporary file when finished.',
)
})
it('should resolve version_stack asset to its latest version file', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'stack-1',
name: '',
type: 'version_stack',
storageKey: null,
media: null,
} as unknown as Asset)
vi.mocked(prisma.asset.findFirst).mockResolvedValue({
id: 'file-ver-2',
name: 'banana_pig.png',
type: 'file',
storageKey: { key: 'raw/banana_pig.png' },
media: {
proxyType: 'image',
imageTranscodes: [{ key: 'proxy/banana_pig.webp' }],
},
} as unknown as Asset)
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('fake-version-bytes'),
contentType: 'image/webp',
} as unknown as { buffer: Buffer; contentType: string })
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', { assetId: 'stack-1', key: null })
expect(prisma.asset.findFirst).toHaveBeenCalledWith({
where: { parentId: 'stack-1', isDeleted: false },
orderBy: { sortIndex: 'asc' },
include: { storageKey: true },
})
expect(s3Service.getObject).toHaveBeenCalledWith('shumai', 'proxy/banana_pig.webp')
const expectedPath = path.join(piDir, 'file-ver-2_banana_pig.png')
createdFiles.push(expectedPath)
expect(fs.existsSync(expectedPath)).toBe(true)
expect(result.details.name).toBe('banana_pig.png')
})
})
describe('Download by S3 key and Authorization', () => {
it('should reject keys that do not start with files/', async () => {
const tool = createDownloadAssetTool('user-1')
await expect(
tool.execute('call-1', { assetId: null, key: 'avatars/user-1.png' }),
).rejects.toThrow('Invalid storage key format: key must start with "files/"')
})
it('should reject keys with path traversal (..)', async () => {
const tool = createDownloadAssetTool('user-1')
await expect(
tool.execute('call-1', { assetId: null, key: 'files/../../etc/passwd' }),
).rejects.toThrow('cannot contain ".."')
})
it('should reject keys with insufficient path segments', async () => {
const tool = createDownloadAssetTool('user-1')
await expect(tool.execute('call-1', { assetId: null, key: 'files/invalid' })).rejects.toThrow(
'Invalid storage key format',
)
})
it('should reject keys that do not belong to any valid asset in database', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findFirst).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findUnique).mockResolvedValue(null)
const tool = createDownloadAssetTool('user-1')
await expect(
tool.execute('call-1', { assetId: null, key: 'files/nonexistent-id/shot.webp' }),
).rejects.toThrow('does not belong to any valid asset or the asset has been deleted')
})
it('should reject download when user lacks read permission on the owning asset (cross-project protection)', async () => {
// Key belongs to asset-in-other-project
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-in-other-project',
} as unknown as Asset)
// Authz rejects because user is in a different project/team
vi.mocked(authzService.hasPermission).mockRejectedValue(
new Error('User does not have Read permission on asset asset-in-other-project'),
)
const tool = createDownloadAssetTool('user-1')
await expect(
tool.execute('call-1', {
assetId: null,
key: 'files/asset-in-other-project/screenshots/secret_frame.webp',
}),
).rejects.toThrow('User does not have Read permission on asset asset-in-other-project')
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'asset-in-other-project',
})
expect(s3Service.getObject).not.toHaveBeenCalled()
})
it('should authorize and download derived artifact key (files/<assetId>/...)', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'ast-123',
} as unknown as Asset)
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('fake-screenshot-bytes'),
contentType: 'image/webp',
} as unknown as { buffer: Buffer; contentType: string })
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', {
assetId: null,
key: 'files/ast-123/screenshots/shot_5.0s.webp',
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'ast-123',
})
expect(s3Service.getObject).toHaveBeenCalledWith(
'shumai',
'files/ast-123/screenshots/shot_5.0s.webp',
)
const expectedPath = path.join(piDir, 'shot_5.0s.webp')
createdFiles.push(expectedPath)
expect(fs.existsSync(expectedPath)).toBe(true)
expect(fs.readFileSync(expectedPath, 'utf-8')).toBe('fake-screenshot-bytes')
expect(result.details.filePath).toBe(path.join('.pi', 'shot_5.0s.webp'))
expect(result.details.key).toBe('files/ast-123/screenshots/shot_5.0s.webp')
expect(result.details.assetId).toBe('ast-123')
expect(result.details.size).toBe(Buffer.from('fake-screenshot-bytes').length)
})
it('should authorize and download original upload key (files/<uploadUlid>/...) via StorageKey', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findFirst).mockResolvedValue({
id: 'sk-1',
key: 'files/upload-ulid-456/photo.png',
assets: [{ id: 'asset-photo-1' }],
} as unknown as StorageKey & { assets: Asset[] })
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('fake-original-photo-bytes'),
contentType: 'image/png',
} as unknown as { buffer: Buffer; contentType: string })
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', {
assetId: null,
key: 'files/upload-ulid-456/photo.png',
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'asset-photo-1',
})
expect(s3Service.getObject).toHaveBeenCalledWith('shumai', 'files/upload-ulid-456/photo.png')
const expectedPath = path.join(piDir, 'photo.png')
createdFiles.push(expectedPath)
expect(fs.existsSync(expectedPath)).toBe(true)
expect(result.details.assetId).toBe('asset-photo-1')
})
})
describe('resolveAssetIdFromKey helper unit tests', () => {
it('resolves directly by asset ID', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue({ id: 'ast-999' } as unknown as Asset)
const assetId = await resolveAssetIdFromKey('files/ast-999/pdf_pages/page_1.webp')
expect(assetId).toBe('ast-999')
})
it('resolves via StorageKey prefix', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findFirst).mockResolvedValue({
assets: [{ id: 'ast-888' }],
} as unknown as StorageKey & { assets: Asset[] })
const assetId = await resolveAssetIdFromKey('files/ulid-777/document.pdf')
expect(assetId).toBe('ast-888')
})
it('resolves via exact StorageKey match', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findFirst).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findUnique).mockResolvedValue({
assets: [{ id: 'ast-777' }],
} as unknown as StorageKey & { assets: Asset[] })
const assetId = await resolveAssetIdFromKey('files/ulid-special/document.pdf')
expect(assetId).toBe('ast-777')
})
})
})
+208 -83
View File
@@ -7,82 +7,107 @@ import { sanitizeFilename } from '@shumai/core/src/utils/filename'
import * as fs from 'fs'
import * as path from 'path'
const downloadAssetSchema = Type.Object({
assetId: Type.String({
description:
'The asset ID of the workspace asset to download to local disk. This parameter is required.',
}),
})
export const downloadAssetSchema = Type.Object(
{
assetId: Type.Union([
Type.String({
description:
'The asset ID of the workspace asset to download to local disk, or null if providing "key".',
}),
Type.Null(),
]),
key: Type.Union([
Type.String({
description:
'The storage S3 key of a specific media artifact (e.g. a screenshot or transcode key from read_asset) to download to local disk, or null if providing "assetId".',
}),
Type.Null(),
]),
},
{ additionalProperties: false },
)
export async function resolveAssetIdFromKey(key: string): Promise<string> {
if (!key.startsWith('files/') || key.includes('..')) {
throw new Error(
'Invalid storage key format: key must start with "files/" and cannot contain "..".',
)
}
const segments = key.split('/')
if (segments.length < 3) {
throw new Error(
'Invalid storage key format: key must follow "files/<identifier>/<path>" structure.',
)
}
const identifier = segments[1]
// 1. Check if identifier is directly an Asset ID (Derived Artifacts: screenshots, pdf_pages, annotations, proxies)
const assetById = await prisma.asset.findUnique({
where: { id: identifier, isDeleted: false },
select: { id: true },
})
if (assetById) {
return assetById.id
}
// 2. Check if identifier is a StorageKey directory prefix (Original uploads / agent-created files)
const storageKeyByPrefix = await prisma.storageKey.findFirst({
where: { key: { startsWith: `files/${identifier}/` } },
include: {
assets: {
where: { isDeleted: false },
select: { id: true },
},
},
})
if (storageKeyByPrefix?.assets && storageKeyByPrefix.assets.length > 0) {
return storageKeyByPrefix.assets[0].id
}
// 3. Exact StorageKey fallback
const exactStorageKey = await prisma.storageKey.findUnique({
where: { key },
include: {
assets: {
where: { isDeleted: false },
select: { id: true },
},
},
})
if (exactStorageKey?.assets && exactStorageKey.assets.length > 0) {
return exactStorageKey.assets[0].id
}
throw new Error(
`Storage key "${key}" does not belong to any valid asset or the asset has been deleted.`,
)
}
export function createDownloadAssetTool(userId: string): AgentTool<typeof downloadAssetSchema> {
return {
name: 'download_asset',
label: 'Download Asset',
description:
'Downloads a workspace asset (proxy version if available) to the local .pi directory for processing with local scripts or bash commands. Remember to delete the temporary downloaded file when finished.',
'Downloads a workspace asset or specific storage S3 key to the local .pi directory for processing with local scripts or bash commands. ' +
'Provide exactly one of "assetId" (for full assets) or "key" (for intermediate artifacts like screenshots from read_asset). ' +
'Remember to delete the temporary downloaded file when finished.',
parameters: downloadAssetSchema,
execute: async (_toolCallId, params) => {
const assetId = params.assetId
const { assetId, key } = params
if (!userId) {
throw new Error('User ID is required for authorization.')
}
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: assetId,
})
const hasAssetId = assetId !== null
const hasKey = key !== null
let asset = await prisma.asset.findUnique({
where: { id: assetId },
include: { storageKey: true },
})
if (!asset) {
throw new Error(`Asset with ID ${assetId} not found.`)
}
if (asset.type === 'version_stack') {
const latestVersion = await prisma.asset.findFirst({
where: { parentId: asset.id, isDeleted: false },
orderBy: { sortIndex: 'asc' },
include: { storageKey: true },
})
if (latestVersion) {
asset = latestVersion
}
}
let mediaKey = asset.storageKey?.key
if (asset.media) {
const mediaInfo = asset.media as unknown as PrismaJson.MediaInfo
if (
mediaInfo.proxyType === 'image' ||
(mediaInfo.imageTranscodes && mediaInfo.imageTranscodes.length > 0)
) {
if (mediaInfo.imageTranscodes && mediaInfo.imageTranscodes.length > 0) {
mediaKey = mediaInfo.imageTranscodes[0].key || mediaKey
}
} else if (
mediaInfo.proxyType === 'video' ||
(mediaInfo.videoTranscodes && mediaInfo.videoTranscodes.length > 0) ||
mediaInfo.videoPreview?.key
) {
if (mediaInfo.videoTranscodes && mediaInfo.videoTranscodes.length > 0) {
mediaKey = mediaInfo.videoTranscodes[0].key || mediaKey
} else if (mediaInfo.videoPreview?.key) {
mediaKey = mediaInfo.videoPreview.key
}
} else if (mediaInfo.original?.key) {
mediaKey = mediaInfo.original.key || mediaKey
}
}
if (!mediaKey) {
throw new Error(`No media content found for asset ${assetId}.`)
if (hasAssetId === hasKey) {
throw new Error(
'Provide exactly one of "assetId" (to download a workspace asset) or "key" (to download a specific S3 storage key). Set the unused parameter to null.',
)
}
const piDir = path.join(process.cwd(), '.pi')
@@ -90,32 +115,132 @@ export function createDownloadAssetTool(userId: string): AgentTool<typeof downlo
fs.mkdirSync(piDir, { recursive: true })
}
const safeName = sanitizeFilename(asset.name || 'file')
const filename = `${asset.id}_${safeName}`
const targetFilePath = path.join(piDir, filename)
const relativePath = path.join('.pi', filename)
const bucket = process.env.S3_BUCKET || 'shumai'
const { buffer, contentType } = await s3Service.getObject(bucket, mediaKey)
fs.writeFileSync(targetFilePath, buffer)
// Branch A: Download by assetId
if (assetId) {
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: assetId,
})
return {
content: [
{
type: 'text',
text: `Downloaded asset "${asset.name}" (ID: ${assetId}) to "${relativePath}". Remember to delete this temporary file when finished.`,
let asset = await prisma.asset.findUnique({
where: { id: assetId },
include: { storageKey: true },
})
if (!asset) {
throw new Error(`Asset with ID ${assetId} not found.`)
}
if (asset.type === 'version_stack') {
const latestVersion = await prisma.asset.findFirst({
where: { parentId: asset.id, isDeleted: false },
orderBy: { sortIndex: 'asc' },
include: { storageKey: true },
})
if (latestVersion) {
asset = latestVersion
}
}
let mediaKey = asset.storageKey?.key
if (asset.media) {
const mediaInfo = asset.media as unknown as PrismaJson.MediaInfo
if (
mediaInfo.proxyType === 'image' ||
(mediaInfo.imageTranscodes && mediaInfo.imageTranscodes.length > 0)
) {
if (mediaInfo.imageTranscodes && mediaInfo.imageTranscodes.length > 0) {
mediaKey = mediaInfo.imageTranscodes[0].key || mediaKey
}
} else if (
mediaInfo.proxyType === 'video' ||
(mediaInfo.videoTranscodes && mediaInfo.videoTranscodes.length > 0) ||
mediaInfo.videoPreview?.key
) {
if (mediaInfo.videoTranscodes && mediaInfo.videoTranscodes.length > 0) {
mediaKey = mediaInfo.videoTranscodes[0].key || mediaKey
} else if (mediaInfo.videoPreview?.key) {
mediaKey = mediaInfo.videoPreview.key
}
} else if (mediaInfo.original?.key) {
mediaKey = mediaInfo.original.key || mediaKey
}
}
if (!mediaKey) {
throw new Error(`No media content found for asset ${assetId}.`)
}
const safeName = sanitizeFilename(asset.name || 'file')
const filename = `${asset.id}_${safeName}`
const targetFilePath = path.join(piDir, filename)
const relativePath = path.join('.pi', filename)
const { buffer, contentType } = await s3Service.getObject(bucket, mediaKey)
fs.writeFileSync(targetFilePath, buffer)
return {
content: [
{
type: 'text',
text: `Downloaded asset "${asset.name}" (ID: ${assetId}) to "${relativePath}". Remember to delete this temporary file when finished.`,
},
],
details: {
assetId,
name: asset.name,
filePath: relativePath,
absolutePath: targetFilePath,
contentType: contentType || 'application/octet-stream',
size: buffer.length,
},
],
details: {
assetId,
name: asset.name,
filePath: relativePath,
absolutePath: targetFilePath,
contentType: contentType || 'application/octet-stream',
size: buffer.length,
},
}
}
// Branch B: Download by S3 storage key
if (key) {
const owningAssetId = await resolveAssetIdFromKey(key)
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: owningAssetId,
})
const rawBasename = path.basename(key)
const safeBasename = sanitizeFilename(rawBasename) || 'downloaded_file'
const filename = safeBasename
const targetFilePath = path.join(piDir, filename)
const relativePath = path.join('.pi', filename)
const { buffer, contentType } = await s3Service.getObject(bucket, key)
fs.writeFileSync(targetFilePath, buffer)
return {
content: [
{
type: 'text',
text: `Downloaded storage key "${key}" to "${relativePath}". Remember to delete this temporary file when finished.`,
},
],
details: {
key,
assetId: owningAssetId,
filePath: relativePath,
absolutePath: targetFilePath,
contentType: contentType || 'application/octet-stream',
size: buffer.length,
},
}
}
throw new Error('Invalid download_asset parameters.')
},
}
}
@@ -0,0 +1,79 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createListAssetsTool } from './list-assets'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService } from '@shumai/core/src/authz/authz'
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
listChildren: vi.fn().mockResolvedValue({
data: [
{ id: '01M2FILE2', name: 'file2.txt', type: 'file', sizeByte: 200 },
{ id: '01M1FILE1', name: 'file1.txt', type: 'file', sizeByte: 100 },
],
pageInfo: { hasNextPage: false, hasPreviousPage: false, cursor: 'abc' },
}),
},
}))
describe('createListAssetsTool', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('checks read permissions and invokes assetService.listChildren with default all and id desc ordering', async () => {
const tool = createListAssetsTool('user-1')
const result = await tool.execute('call-1', { parent: 'folder-1' })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'read',
type: 'asset',
id: 'folder-1',
})
expect(assetService.listChildren).toHaveBeenCalledWith({
assetId: 'folder-1',
assetType: 'all',
sort: 'createdAt',
order: 'desc',
first: 20,
after: undefined,
})
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
assets: [
{ id: '01M2FILE2', name: 'file2.txt', type: 'file', size: 200 },
{ id: '01M1FILE1', name: 'file1.txt', type: 'file', size: 100 },
],
pageInfo: { hasNextPage: false, hasPreviousPage: false, cursor: 'abc' },
})
})
it('passes explicit pagination and type filter options to assetService.listChildren', async () => {
const tool = createListAssetsTool('user-1')
await tool.execute('call-1', {
parent: 'folder-1',
page: 2,
pageSize: 10,
type: 'folder',
})
expect(assetService.listChildren).toHaveBeenCalledWith({
assetId: 'folder-1',
assetType: 'folder',
sort: 'createdAt',
order: 'desc',
first: 10,
after: expect.any(String),
})
})
})
+38 -10
View File
@@ -1,6 +1,9 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { executeAgentToolWorkflow } from './utils'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { encodeCursor } from '@shumai/core/src/pagination'
import { type User } from '@shumai/db'
const listAssetsSchema = Type.Object({
parent: Type.String({
@@ -19,9 +22,8 @@ const listAssetsSchema = Type.Object({
}),
),
type: Type.Optional(
Type.String({
Type.Union([Type.Literal('file'), Type.Literal('folder'), Type.Literal('all')], {
description: "Filter assets by type: 'file', 'folder', or 'all'. Default is 'all'.",
enum: ['file', 'folder', 'all'],
default: 'all',
}),
),
@@ -34,15 +36,41 @@ export function createListAssetsTool(userId: string): AgentTool<typeof listAsset
description: 'List the assets (files and folders) inside a parent folder with pagination.',
parameters: listAssetsSchema,
execute: async (_toolCallId, params) => {
const result = await executeAgentToolWorkflow({
toolName: 'list_assets',
args: params,
userId,
assetId: params.parent,
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: params.parent,
})
const page = params.page || 1
const pageSize = params.pageSize || 20
const type = params.type || 'all'
const result = await assetService.listChildren({
assetId: params.parent,
assetType: type,
sort: 'createdAt',
order: 'desc',
first: pageSize,
after: page > 1 ? encodeCursor((page - 1) * pageSize) : undefined,
})
const assets = result.data.map((a) => ({
id: a.id,
name: a.name,
type: a.type,
size: Number(a.sizeByte),
}))
const output = {
assets,
pageInfo: result.pageInfo,
}
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
details: result,
content: [{ type: 'text', text: JSON.stringify(output, null, 2) }],
details: {},
}
},
}
+544
View File
@@ -0,0 +1,544 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createReadAssetTool } from './read-asset'
import {
prisma,
WorkflowTaskType,
WorkflowTaskStatus,
type Asset,
type WorkflowTask,
} from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { workflowService } from '@shumai/workflow-core'
import { authzService } from '@shumai/core/src/authz/authz'
import * as annotationResolver from './annotation-resolver'
vi.mock('@shumai/db', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shumai/db')>()
return {
...actual,
prisma: {
asset: {
findUnique: vi.fn(),
findFirst: vi.fn(),
},
workflowTask: {
create: vi.fn(),
},
},
}
})
vi.mock('@shumai/core/src/s3/s3', () => ({
s3Service: {
getObject: vi.fn(),
presign: vi.fn().mockResolvedValue('https://mock-presigned-url.com'),
},
}))
vi.mock('@shumai/workflow-core', () => ({
workflowService: {
executeWait: vi.fn(),
},
}))
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn(),
},
Permission: { Read: 'Read' },
ResourceType: { Asset: 'asset' },
}))
describe('readAssetTool', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.spyOn(annotationResolver, 'resolveAnnotationsById').mockResolvedValue({
annotations: null,
timestamp: null,
})
})
it('should throw authorization error if user ID is empty', async () => {
const tool = createReadAssetTool('')
await expect(
tool.execute('call-1', {
assetId: 'asset-1',
annotationId: null,
imageConfig: null,
videoConfig: null,
docConfig: null,
}),
).rejects.toThrow('User ID is required for authorization.')
})
it('should throw error if asset not found', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
const tool = createReadAssetTool('user-1')
await expect(
tool.execute('call-1', {
assetId: 'asset-1',
annotationId: null,
imageConfig: null,
videoConfig: null,
docConfig: null,
}),
).rejects.toThrow('Asset with ID asset-1 not found.')
})
describe('Image Assets', () => {
it('should read image and return ImageContent and S3 key', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'img-1',
name: 'photo.png',
mediaType: 'image/png',
storageKey: { key: 'raw/photo.png' },
media: {
proxyType: 'image',
imageTranscodes: [{ key: 'proxy/photo.webp' }],
},
} as unknown as Asset)
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('fake-image-bytes'),
contentType: 'image/webp',
} as unknown as { buffer: Buffer; contentType: string })
const tool = createReadAssetTool('user-1')
const result = await tool.execute('call-1', {
assetId: 'img-1',
annotationId: null,
imageConfig: null,
videoConfig: null,
docConfig: null,
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'img-1',
})
expect(s3Service.getObject).toHaveBeenCalledWith('shumai', 'proxy/photo.webp')
const textItem = result.content[0] as { type: 'text'; text: string }
const imageItem = result.content[1] as { type: 'image'; data: string; mimeType: string }
expect(textItem.text).toContain('proxy/photo.webp')
expect(imageItem.type).toBe('image')
expect(imageItem.data).toBe(Buffer.from('fake-image-bytes').toString('base64'))
expect(result.details.key).toBe('proxy/photo.webp')
})
it('should trigger image annotation transcode workflow when annotationId is provided', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'img-1',
name: 'photo.png',
mediaType: 'image/png',
projectId: 'project-1',
storageKey: { key: 'raw/photo.png' },
media: {
proxyType: 'image',
imageTranscodes: [{ key: 'proxy/photo.webp' }],
},
} as unknown as Asset)
vi.spyOn(annotationResolver, 'resolveAnnotationsById').mockResolvedValue({
annotations: [
{
type: 'arrow',
color: '#ff0000',
points: [
[0, 0],
[10, 10],
],
},
],
timestamp: null,
})
vi.mocked(prisma.workflowTask.create).mockResolvedValue({
id: 'task-anno',
} as unknown as WorkflowTask)
vi.mocked(workflowService.executeWait).mockResolvedValue({
id: 'task-anno',
output: { key: 'annotated/photo.webp' },
} as unknown as WorkflowTask)
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('annotated-bytes'),
contentType: 'image/webp',
} as unknown as { buffer: Buffer; contentType: string })
const tool = createReadAssetTool('user-1')
const result = await tool.execute('call-1', {
assetId: 'img-1',
annotationId: 'comment-1',
imageConfig: null,
videoConfig: null,
docConfig: null,
})
expect(prisma.workflowTask.create).toHaveBeenCalledWith({
data: {
assetId: 'img-1',
projectId: 'project-1',
type: WorkflowTaskType.transcode_image_annotation,
status: WorkflowTaskStatus.pending,
payload: {
projectId: 'project-1',
imageAnnotation: {
annotations: [
{
type: 'arrow',
color: '#ff0000',
points: [
[0, 0],
[10, 10],
],
},
],
},
},
},
})
expect(s3Service.getObject).toHaveBeenCalledWith('shumai', 'annotated/photo.webp')
expect(result.details.key).toBe('annotated/photo.webp')
})
})
describe('Video Assets', () => {
it('should throw error if videoConfig is missing for video assets', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'vid-1',
name: 'video.mp4',
mediaType: 'video/mp4',
media: { proxyType: 'video' },
} as unknown as Asset)
const tool = createReadAssetTool('user-1')
await expect(
tool.execute('call-1', {
assetId: 'vid-1',
annotationId: null,
imageConfig: null,
videoConfig: null,
docConfig: null,
}),
).rejects.toThrow('videoConfig with start, end, and count is required')
})
it('should throw error on invalid video time range', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'vid-1',
name: 'video.mp4',
mediaType: 'video/mp4',
media: { proxyType: 'video' },
} as unknown as Asset)
const tool = createReadAssetTool('user-1')
await expect(
tool.execute('call-1', {
assetId: 'vid-1',
annotationId: null,
imageConfig: null,
videoConfig: { start: 10, end: 5, count: 1 },
docConfig: null,
}),
).rejects.toThrow(
'Invalid video time range: start (10) must be less than or equal to end (5).',
)
})
it('should extract frames and return ImageContent array and keys', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'vid-1',
name: 'video.mp4',
mediaType: 'video/mp4',
projectId: 'project-1',
media: { proxyType: 'video' },
} as unknown as Asset)
vi.mocked(prisma.workflowTask.create).mockResolvedValue({
id: 'task-shot',
} as unknown as WorkflowTask)
vi.mocked(workflowService.executeWait).mockResolvedValue({
id: 'task-shot',
output: {
screenshots: [
{ key: 'screenshots/shot1.webp', timestamp: 2.0 },
{ key: 'screenshots/shot2.webp', timestamp: 4.0 },
],
},
} as unknown as WorkflowTask)
vi.mocked(s3Service.getObject).mockImplementation(async (_bucket, key) => ({
buffer: Buffer.from(`bytes-for-${key}`),
contentType: 'image/webp',
}))
const tool = createReadAssetTool('user-1')
const result = await tool.execute('call-1', {
assetId: 'vid-1',
annotationId: null,
imageConfig: null,
videoConfig: { start: 2.0, end: 4.0, count: 2 },
docConfig: null,
})
expect(prisma.workflowTask.create).toHaveBeenCalledWith({
data: {
assetId: 'vid-1',
projectId: 'project-1',
type: WorkflowTaskType.transcode_screenshot,
status: WorkflowTaskStatus.pending,
payload: {
projectId: 'project-1',
screenshot: {
start: 2.0,
end: 4.0,
count: 2,
commentTimestamp: null,
annotations: null,
},
},
},
})
expect(result.content.length).toBe(3) // 1 text + 2 images
expect(result.details.keys).toEqual(['screenshots/shot1.webp', 'screenshots/shot2.webp'])
})
})
describe('Document Assets', () => {
it('should throw error if docConfig is missing for document assets', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'doc-1',
name: 'doc.pdf',
mediaType: 'application/pdf',
media: { proxyType: 'pdf' },
} as unknown as Asset)
const tool = createReadAssetTool('user-1')
await expect(
tool.execute('call-1', {
assetId: 'doc-1',
annotationId: null,
imageConfig: null,
videoConfig: null,
docConfig: null,
}),
).rejects.toThrow('docConfig with mode ("pages" or "text") is required')
})
it('should render PDF pages in mode: "pages"', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'doc-1',
name: 'doc.pdf',
mediaType: 'application/pdf',
projectId: 'project-1',
media: { proxyType: 'pdf' },
} as unknown as Asset)
vi.mocked(prisma.workflowTask.create).mockResolvedValue({
id: 'task-pdf',
} as unknown as WorkflowTask)
vi.mocked(workflowService.executeWait).mockResolvedValue({
id: 'task-pdf',
output: {
pages: [
{ key: 'pdf_pages/page1.webp', page: 1 },
{ key: 'pdf_pages/page2.webp', page: 2 },
],
},
} as unknown as WorkflowTask)
vi.mocked(s3Service.getObject).mockImplementation(async (_bucket, key) => ({
buffer: Buffer.from(`bytes-for-${key}`),
contentType: 'image/webp',
}))
const tool = createReadAssetTool('user-1')
const result = await tool.execute('call-1', {
assetId: 'doc-1',
annotationId: null,
imageConfig: null,
videoConfig: null,
docConfig: { mode: 'pages', startPage: 1, endPage: 2 },
})
expect(result.content.length).toBe(3) // 1 text + 2 images
expect(result.details.keys).toEqual(['pdf_pages/page1.webp', 'pdf_pages/page2.webp'])
})
it('should throw error in mode: "pages" when page range exceeds 20 pages', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'doc-1',
name: 'doc.pdf',
mediaType: 'application/pdf',
media: { proxyType: 'pdf' },
} as unknown as Asset)
const tool = createReadAssetTool('user-1')
await expect(
tool.execute('call-1', {
assetId: 'doc-1',
annotationId: null,
imageConfig: null,
videoConfig: null,
docConfig: { mode: 'pages', startPage: 1, endPage: 25 },
}),
).rejects.toThrow('exceeds the maximum limit of 20 pages')
})
it('should throw error in mode: "text" when called on binary PDF', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'doc-1',
name: 'manual.pdf',
mediaType: 'application/pdf',
media: { proxyType: 'pdf' },
} as unknown as Asset)
const tool = createReadAssetTool('user-1')
await expect(
tool.execute('call-1', {
assetId: 'doc-1',
annotationId: null,
imageConfig: null,
videoConfig: null,
docConfig: { mode: 'text', startPage: null, endPage: null },
}),
).rejects.toThrow('is a binary PDF/document and cannot be read as raw text')
})
it('should throw error in mode: "text" when annotationId is provided', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'doc-1',
name: 'notes.md',
mediaType: 'text/markdown',
media: { proxyType: 'pdf' },
} as unknown as Asset)
const tool = createReadAssetTool('user-1')
await expect(
tool.execute('call-1', {
assetId: 'doc-1',
annotationId: 'comment-123',
imageConfig: null,
videoConfig: null,
docConfig: { mode: 'text', startPage: null, endPage: null },
}),
).rejects.toThrow('annotationId cannot be used with docConfig mode "text"')
})
it('should throw error in mode: "text" when startPage or endPage is non-null', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'doc-1',
name: 'notes.md',
mediaType: 'text/markdown',
media: { proxyType: 'pdf' },
} as unknown as Asset)
const tool = createReadAssetTool('user-1')
await expect(
tool.execute('call-1', {
assetId: 'doc-1',
annotationId: null,
imageConfig: null,
videoConfig: null,
docConfig: { mode: 'text', startPage: 1, endPage: 2 },
}),
).rejects.toThrow('startPage and endPage must be null when docConfig mode is "text"')
})
it('should read raw text from S3 in mode: "text" for markdown files', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'doc-md',
name: 'README.md',
mediaType: 'text/markdown',
storageKey: { key: 'raw/README.md' },
media: { proxyType: 'pdf' },
} as unknown as Asset)
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('# Hello Shumai\nThis is markdown text.'),
contentType: 'text/markdown',
} as unknown as { buffer: Buffer; contentType: string })
const tool = createReadAssetTool('user-1')
const result = await tool.execute('call-1', {
assetId: 'doc-md',
annotationId: null,
imageConfig: null,
videoConfig: null,
docConfig: { mode: 'text', startPage: null, endPage: null },
})
expect(s3Service.getObject).toHaveBeenCalledWith('shumai', 'raw/README.md')
expect(result.content[0].type).toBe('text')
expect((result.content[0] as { type: 'text'; text: string }).text).toContain(
'# Hello Shumai\nThis is markdown text.',
)
expect(result.details.key).toBe('raw/README.md')
})
})
describe('Version Stack Resolution', () => {
it('should resolve version_stack to latest version asset', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'stack-1',
name: '',
type: 'version_stack',
} as unknown as Asset)
vi.mocked(prisma.asset.findFirst).mockResolvedValue({
id: 'file-v2',
name: 'diagram_v2.png',
type: 'file',
mediaType: 'image/png',
storageKey: { key: 'raw/diagram_v2.png' },
media: {
proxyType: 'image',
imageTranscodes: [{ key: 'proxy/diagram_v2.webp' }],
},
} as unknown as Asset)
vi.mocked(s3Service.getObject).mockResolvedValue({
buffer: Buffer.from('v2-image-bytes'),
contentType: 'image/webp',
} as unknown as { buffer: Buffer; contentType: string })
const tool = createReadAssetTool('user-1')
const result = await tool.execute('call-1', {
assetId: 'stack-1',
annotationId: null,
imageConfig: null,
videoConfig: null,
docConfig: null,
})
expect(prisma.asset.findFirst).toHaveBeenCalledWith({
where: { parentId: 'stack-1', isDeleted: false },
orderBy: { sortIndex: 'asc' },
include: { storageKey: true },
})
expect(result.details.name).toBe('diagram_v2.png')
expect(result.details.key).toBe('proxy/diagram_v2.webp')
})
})
})
+576
View File
@@ -0,0 +1,576 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { type ImageContent, type TextContent } from '@earendil-works/pi-ai'
import { prisma, WorkflowTaskType, WorkflowTaskStatus, type User } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { workflowService } from '@shumai/workflow-core'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { resolveAnnotationsById } from './annotation-resolver'
export const readAssetSchema = Type.Object(
{
assetId: Type.String({
description: 'The asset ID of the workspace asset to inspect. Required.',
}),
annotationId: Type.Union([
Type.String({
description:
'The ID of the comment or message entry from <annotation id="..." /> whose visual markup should be overlaid, or null.',
}),
Type.Null(),
]),
imageConfig: Type.Union([
Type.Object(
{},
{
additionalProperties: false,
description: 'Optional configuration for image assets, or null.',
},
),
Type.Null(),
]),
videoConfig: Type.Union([
Type.Object(
{
start: Type.Number({ description: 'Start time in seconds.' }),
end: Type.Number({ description: 'End time in seconds.' }),
count: Type.Number({ description: 'Number of frames to extract (e.g. 1 to 10).' }),
},
{
additionalProperties: false,
description:
'Configuration for video assets. Required when inspecting videos, or null for other asset types.',
},
),
Type.Null(),
]),
docConfig: Type.Union([
Type.Object(
{
mode: Type.Union([
Type.Literal('pages', {
description:
'Render specific page range as images for visual layout, charts, or visual annotations (required for binary PDFs).',
}),
Type.Literal('text', {
description:
'Read raw text content directly (ideal for .md, .txt, .csv, code files).',
}),
]),
startPage: Type.Union([
Type.Number({
description:
'Start page number (1-based index, required when mode="pages"), or null when mode="text".',
}),
Type.Null(),
]),
endPage: Type.Union([
Type.Number({
description:
'End page number (1-based index, max 20 pages per call, required when mode="pages"), or null when mode="text".',
}),
Type.Null(),
]),
},
{
additionalProperties: false,
description:
'Configuration for document/PDF/text assets, or null if inspecting another asset type.',
},
),
Type.Null(),
]),
},
{ additionalProperties: false },
)
export function isPlainTextAsset(mediaType: string, filename: string): boolean {
if (
mediaType.startsWith('text/') ||
mediaType === 'application/json' ||
mediaType === 'application/javascript' ||
mediaType === 'application/typescript' ||
mediaType === 'application/xml' ||
mediaType === 'application/x-yaml'
) {
return true
}
const textExtensions = [
'.txt',
'.md',
'.markdown',
'.json',
'.csv',
'.tsv',
'.js',
'.ts',
'.tsx',
'.jsx',
'.html',
'.htm',
'.css',
'.scss',
'.yaml',
'.yml',
'.xml',
'.py',
'.rb',
'.go',
'.rs',
'.c',
'.cpp',
'.h',
'.hpp',
'.java',
'.sh',
'.bash',
'.zsh',
'.sql',
]
return textExtensions.some((ext) => filename.endsWith(ext))
}
const MAX_TEXT_BYTES = 50 * 1024
const MAX_TEXT_LINES = 1000
export function createReadAssetTool(userId: string): AgentTool<typeof readAssetSchema> {
return {
name: 'read_asset',
label: 'Read Asset',
description:
'Inspects and reads the content of an asset in the workspace. ' +
'Supports visual analysis for images, video frame extraction, PDF page rendering, and direct text reading for text/markdown files. ' +
'Pass assetId, and provide the specific config (imageConfig, videoConfig, or docConfig) while setting unused configs to null. ' +
'Optionally provide annotationId to view visual markups drawn on the asset.',
parameters: readAssetSchema,
execute: async (_toolCallId, params) => {
const { assetId, annotationId, videoConfig, docConfig } = params
if (!userId) {
throw new Error('User ID is required for authorization.')
}
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: assetId,
})
let asset = await prisma.asset.findUnique({
where: { id: assetId },
include: { storageKey: true },
})
if (!asset) {
throw new Error(`Asset with ID ${assetId} not found.`)
}
if (asset.type === 'version_stack') {
const latestVersion = await prisma.asset.findFirst({
where: { parentId: asset.id, isDeleted: false },
orderBy: { sortIndex: 'asc' },
include: { storageKey: true },
})
if (latestVersion) {
asset = latestVersion
}
}
const mediaInfo = asset.media as unknown as PrismaJson.MediaInfo | null
const proxyType = mediaInfo?.proxyType
const mediaType = asset.mediaType?.toLowerCase() || ''
const filename = asset.name?.toLowerCase() || ''
const bucket = process.env.S3_BUCKET || 'shumai'
// ----------------------------------------------------------------------
// Branch 1: Image Assets
// ----------------------------------------------------------------------
if (proxyType === 'image' || mediaType.startsWith('image/') || filename.endsWith('.psd')) {
let mediaKey = asset.storageKey?.key
if (mediaInfo?.imageTranscodes && mediaInfo.imageTranscodes.length > 0) {
mediaKey = mediaInfo.imageTranscodes[0].key || mediaKey
}
if (!mediaKey) {
throw new Error('No media content found for this asset.')
}
const { annotations } = await resolveAnnotationsById(asset.id, annotationId)
let keyToUse = mediaKey
if (annotations) {
const task = await prisma.workflowTask.create({
data: {
assetId: asset.id,
projectId: asset.projectId || 'none',
type: WorkflowTaskType.transcode_image_annotation,
status: WorkflowTaskStatus.pending,
payload: {
projectId: asset.projectId || 'none',
imageAnnotation: {
annotations,
},
},
},
})
const completedTask = await workflowService.executeWait(task)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- transcode image annotation task output
const output = completedTask.output as any
if (output?.key) {
keyToUse = output.key
} else {
throw new Error('Image annotation transcode workflow failed to output S3 key.')
}
}
const { buffer, contentType } = await s3Service.getObject(bucket, keyToUse)
const mimeType =
contentType && contentType !== 'application/octet-stream' ? contentType : 'image/webp'
const downloadUrl = await s3Service.presign(
bucket,
keyToUse,
'GET',
true,
asset.name || undefined,
)
const content: Array<TextContent | ImageContent> = [
{
type: 'text',
text: `Image asset "${asset.name}" (ID: ${asset.id}):\n- S3 Key: "${keyToUse}"`,
},
{
type: 'image',
data: buffer.toString('base64'),
mimeType,
},
]
return {
content,
details: {
assetId: asset.id,
name: asset.name,
mediaType: asset.mediaType,
key: keyToUse,
downloadUrl,
sourceKeys: [keyToUse],
},
}
}
// ----------------------------------------------------------------------
// Branch 2: Video Assets
// ----------------------------------------------------------------------
if (proxyType === 'video' || mediaType.startsWith('video/')) {
if (!videoConfig) {
throw new Error(
'videoConfig with start, end, and count is required when inspecting video assets.',
)
}
const { start, end, count } = videoConfig
if (start < 0 || end < 0) {
throw new Error('Invalid video time range: start and end must be non-negative numbers.')
}
if (start > end) {
throw new Error(
`Invalid video time range: start (${start}) must be less than or equal to end (${end}).`,
)
}
if (count < 1) {
throw new Error(`Invalid frame count: count (${count}) must be at least 1.`)
}
const { annotations, timestamp: commentTimestamp } = await resolveAnnotationsById(
asset.id,
annotationId,
)
const task = await prisma.workflowTask.create({
data: {
assetId: asset.id,
projectId: asset.projectId || 'none',
type: WorkflowTaskType.transcode_screenshot,
status: WorkflowTaskStatus.pending,
payload: {
projectId: asset.projectId || 'none',
screenshot: {
start,
end,
count,
commentTimestamp,
annotations,
},
},
},
})
const completedTask = await workflowService.executeWait(task)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- transcode screenshot output
const output = completedTask.output as any
const screenshots = output?.screenshots as
| Array<{ key: string; timestamp: number }>
| undefined
if (!screenshots || screenshots.length === 0) {
throw new Error('No screenshots were generated.')
}
const content: Array<TextContent | ImageContent> = []
const sourceKeys: string[] = []
const results: Array<{ key: string; timestamp: number; downloadUrl: string }> = []
const textLines: string[] = [
`Extracted ${screenshots.length} frame(s) from "${asset.name}" (${start}s - ${end}s):`,
]
for (const shot of screenshots) {
const { buffer, contentType } = await s3Service.getObject(bucket, shot.key)
const mimeType =
contentType && contentType !== 'application/octet-stream' ? contentType : 'image/webp'
const downloadUrl = await s3Service.presign(
bucket,
shot.key,
'GET',
true,
asset.name || undefined,
)
textLines.push(`- Frame at ${shot.timestamp}s: S3 Key: "${shot.key}"`)
content.push({
type: 'image',
data: buffer.toString('base64'),
mimeType,
})
sourceKeys.push(shot.key)
results.push({
key: shot.key,
timestamp: shot.timestamp,
downloadUrl,
})
}
content.unshift({
type: 'text',
text: textLines.join('\n'),
})
return {
content,
details: {
assetId: asset.id,
name: asset.name,
mediaType: asset.mediaType,
keys: sourceKeys,
results,
sourceKeys,
},
}
}
// ----------------------------------------------------------------------
// Branch 3: Documents & PDF Assets
// ----------------------------------------------------------------------
if (
proxyType === 'pdf' ||
mediaType === 'application/pdf' ||
isPlainTextAsset(mediaType, filename)
) {
if (!docConfig) {
throw new Error(
'docConfig with mode ("pages" or "text") is required when inspecting document/PDF assets.',
)
}
if (docConfig.mode === 'text') {
if (annotationId !== null) {
throw new Error(
'annotationId cannot be used with docConfig mode "text" because visual markups can only be rendered on visual pages. Use docConfig: { mode: "pages", startPage: ..., endPage: ... } instead.',
)
}
if (docConfig.startPage !== null || docConfig.endPage !== null) {
throw new Error(
'startPage and endPage must be null when docConfig mode is "text". To inspect specific page numbers, use mode: "pages".',
)
}
if (!isPlainTextAsset(mediaType, filename)) {
throw new Error(
`Asset "${asset.name}" is a binary PDF/document and cannot be read as raw text. Please call read_asset with docConfig: { mode: "pages", startPage: 1, endPage: ... } to view its visual pages.`,
)
}
const mediaKey = asset.storageKey?.key
if (!mediaKey) {
throw new Error(`No media content found for asset ${asset.id}.`)
}
const { buffer } = await s3Service.getObject(bucket, mediaKey)
const rawText = buffer.toString('utf-8')
let text = rawText
let isTruncated = false
const lines = text.split('\n')
if (lines.length > MAX_TEXT_LINES) {
text = lines.slice(0, MAX_TEXT_LINES).join('\n')
isTruncated = true
}
if (Buffer.byteLength(text, 'utf-8') > MAX_TEXT_BYTES) {
text = Buffer.from(text, 'utf-8').subarray(0, MAX_TEXT_BYTES).toString('utf-8')
isTruncated = true
}
if (isTruncated) {
text += `\n\n[Content truncated to ${MAX_TEXT_LINES} lines / ${MAX_TEXT_BYTES / 1024}KB limit. Use download_asset to process the full file.]`
}
const downloadUrl = await s3Service.presign(
bucket,
mediaKey,
'GET',
true,
asset.name || undefined,
)
return {
content: [
{
type: 'text',
text: `Document "${asset.name}" (ID: ${asset.id}, S3 Key: "${mediaKey}"):\n\n${text}`,
},
],
details: {
assetId: asset.id,
name: asset.name,
mediaType: asset.mediaType,
key: mediaKey,
downloadUrl,
sourceKeys: [mediaKey],
size: buffer.length,
isTruncated,
},
}
}
// docConfig.mode === 'pages'
if (docConfig.startPage === null || docConfig.endPage === null) {
throw new Error('startPage and endPage are required when docConfig mode is "pages".')
}
const start = docConfig.startPage
const end = docConfig.endPage
if (start < 1) {
throw new Error(`Invalid page range: start page (${start}) must be at least 1.`)
}
if (start > end) {
throw new Error(
`Invalid page range: start page (${start}) must be less than or equal to end page (${end}).`,
)
}
const MAX_PAGE_RANGE = 20
const requestedPages = end - start + 1
if (requestedPages > MAX_PAGE_RANGE) {
throw new Error(
`Page range (${requestedPages} pages requested) exceeds the maximum limit of ${MAX_PAGE_RANGE} pages per request.`,
)
}
const { annotations, timestamp: commentTimestamp } = await resolveAnnotationsById(
asset.id,
annotationId,
)
const task = await prisma.workflowTask.create({
data: {
assetId: asset.id,
projectId: asset.projectId || 'none',
type: WorkflowTaskType.transcode_pdf_pages,
status: WorkflowTaskStatus.pending,
payload: {
projectId: asset.projectId || 'none',
pdfPages: {
start,
end,
commentTimestamp,
annotations,
},
},
},
})
const completedTask = await workflowService.executeWait(task)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- transcode pdf pages output
const output = completedTask.output as any
const pages = output?.pages as Array<{ key: string; page: number }> | undefined
if (!pages || pages.length === 0) {
throw new Error('No PDF page images were generated.')
}
const content: Array<TextContent | ImageContent> = []
const sourceKeys: string[] = []
const results: Array<{ key: string; page: number; downloadUrl: string }> = []
const textLines: string[] = [
`Rendered ${pages.length} page(s) of "${asset.name}" (Pages ${start} to ${end}):`,
]
for (const pageItem of pages) {
const { buffer, contentType } = await s3Service.getObject(bucket, pageItem.key)
const mimeType =
contentType && contentType !== 'application/octet-stream' ? contentType : 'image/webp'
const downloadUrl = await s3Service.presign(
bucket,
pageItem.key,
'GET',
true,
asset.name || undefined,
)
textLines.push(`- Page ${pageItem.page}: S3 Key: "${pageItem.key}"`)
content.push({
type: 'image',
data: buffer.toString('base64'),
mimeType,
})
sourceKeys.push(pageItem.key)
results.push({
key: pageItem.key,
page: pageItem.page,
downloadUrl,
})
}
content.unshift({
type: 'text',
text: textLines.join('\n'),
})
return {
content,
details: {
assetId: asset.id,
name: asset.name,
mediaType: asset.mediaType,
keys: sourceKeys,
results,
sourceKeys,
},
}
}
throw new Error(
`Unsupported asset type for read_asset. Asset ${asset.id} has media type "${mediaType || 'unknown'}". Use download_asset to download this file to disk.`,
)
},
}
}
@@ -1,209 +0,0 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createReadPdfPagesTool } from './read-pdf-pages'
import {
prisma,
WorkflowTaskStatus,
type Asset,
type AssetComment,
type User,
type WorkflowTask,
} from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { workflowService } from '@shumai/workflow-core'
import { authzService } from '@shumai/core/src/authz/authz'
vi.mock('@shumai/db', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shumai/db')>()
return {
...actual,
prisma: {
user: {
findUnique: vi.fn(),
},
asset: {
findUnique: vi.fn(),
},
assetComment: {
findUnique: vi.fn(),
},
workflowTask: {
create: vi.fn(),
},
},
}
})
vi.mock('@shumai/core/src/s3/s3', () => ({
s3Service: {
getObject: vi.fn(),
},
}))
vi.mock('@shumai/workflow-core', () => ({
workflowService: {
executeWait: vi.fn(),
},
}))
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn(),
},
Permission: { Read: 'Read' },
ResourceType: { Asset: 'asset' },
}))
describe('readPdfPagesTool', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(authzService.hasPermission).mockResolvedValue()
})
it('should trigger pdfPages transcode workflow and return image outputs', async () => {
vi.mocked(prisma.user.findUnique).mockResolvedValue({ id: 'user-1' } as User)
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-1',
projectId: 'project-1',
media: { proxyType: 'pdf' },
} as unknown as Asset)
vi.mocked(prisma.assetComment.findUnique).mockResolvedValue({
id: 'comment-1',
assetId: 'asset-1',
second: 2,
annotation: [
{
type: 'box',
color: '#ff0000',
points: [
[0, 0],
[1, 1],
],
},
],
} as unknown as AssetComment)
vi.mocked(prisma.workflowTask.create).mockResolvedValue({
id: 'task-1',
} as unknown as WorkflowTask)
vi.mocked(workflowService.executeWait).mockResolvedValue({
id: 'task-1',
status: WorkflowTaskStatus.completed,
output: {
pages: [
{ key: 'pdf_pages/page_1.webp', page: 1 },
{ key: 'pdf_pages/page_2.webp', page: 2 },
],
},
} as unknown as WorkflowTask)
vi.mocked(s3Service.getObject).mockImplementation(async (_bucket: string, key: string) => {
return {
buffer: Buffer.from(`fake-bytes-for-${key}`),
contentType: 'image/webp',
} as unknown as { buffer: Buffer; contentType: string }
})
const tool = createReadPdfPagesTool('user-1', 'comment-1')
const result = await tool.execute('call-1', { assetId: 'asset-1', start: 1, end: 2 })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'asset-1',
})
expect(prisma.workflowTask.create).toHaveBeenCalledWith({
data: {
assetId: 'asset-1',
projectId: 'project-1',
type: 'transcode_pdf_pages',
status: 'pending',
payload: {
projectId: 'project-1',
pdfPages: {
start: 1,
end: 2,
commentTimestamp: 2,
annotations: [
{
type: 'box',
color: '#ff0000',
points: [
[0, 0],
[1, 1],
],
},
],
},
},
},
})
expect(result.content.length).toBe(2)
expect(result.content[0].type).toBe('image')
expect((result.content[0] as unknown as { data: string }).data).toBe(
Buffer.from('fake-bytes-for-pdf_pages/page_1.webp').toString('base64'),
)
expect(result.details.sourceKeys).toEqual(['pdf_pages/page_1.webp', 'pdf_pages/page_2.webp'])
})
it('should throw error if start page is less than 1', async () => {
const tool = createReadPdfPagesTool('user-1')
await expect(tool.execute('call-1', { assetId: 'asset-1', start: 0, end: 5 })).rejects.toThrow(
'Invalid page range: start page (0) must be at least 1.',
)
})
it('should throw error if start page is greater than end page', async () => {
const tool = createReadPdfPagesTool('user-1')
await expect(tool.execute('call-1', { assetId: 'asset-1', start: 5, end: 2 })).rejects.toThrow(
'Invalid page range: start page (5) must be less than or equal to end page (2).',
)
})
it('should throw error if page range exceeds maximum limit of 20', async () => {
const tool = createReadPdfPagesTool('user-1')
await expect(tool.execute('call-1', { assetId: 'asset-1', start: 1, end: 25 })).rejects.toThrow(
'Page range (25 pages requested) exceeds the maximum limit of 20 pages per request.',
)
})
it('should throw error if asset not found', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
const tool = createReadPdfPagesTool('user-1')
await expect(tool.execute('call-1', { assetId: 'asset-1', start: 1, end: 3 })).rejects.toThrow(
'Asset with ID asset-1 not found.',
)
})
it('should throw error if proxyType is not pdf', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-1',
projectId: 'project-1',
media: { proxyType: 'image' },
} as unknown as Asset)
const tool = createReadPdfPagesTool('user-1')
await expect(tool.execute('call-1', { assetId: 'asset-1', start: 1, end: 3 })).rejects.toThrow(
'Asset asset-1 is not a PDF or document.',
)
})
it('should throw error if proxyType is null/undefined (unprocessed)', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-1',
projectId: 'project-1',
media: null,
} as unknown as Asset)
const tool = createReadPdfPagesTool('user-1')
await expect(tool.execute('call-1', { assetId: 'asset-1', start: 1, end: 3 })).rejects.toThrow(
'Asset asset-1 is not a PDF or document.',
)
})
})
-152
View File
@@ -1,152 +0,0 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { type ImageContent } from '@earendil-works/pi-ai'
import { prisma, WorkflowTaskType, WorkflowTaskStatus, type User } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { workflowService } from '@shumai/workflow-core'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
const readPdfPagesSchema = Type.Object({
assetId: Type.String({
description: 'Asset ID of the PDF document. This parameter is required.',
}),
start: Type.Number({ description: 'Start page number (1-based index)' }),
end: Type.Number({ description: 'End page number (1-based index)' }),
})
export function createReadPdfPagesTool(
userId: string,
userCommentId?: string | null,
): AgentTool<typeof readPdfPagesSchema> {
return {
name: 'read_pdf_pages',
label: 'Read PDF Pages',
description:
'Converts specific page range of a PDF document into 1080p WebP images for visual analysis. Maximum 20 pages allowed per call.',
parameters: readPdfPagesSchema,
execute: async (_toolCallId, params) => {
if (params.start < 1) {
throw new Error(`Invalid page range: start page (${params.start}) must be at least 1.`)
}
if (params.start > params.end) {
throw new Error(
`Invalid page range: start page (${params.start}) must be less than or equal to end page (${params.end}).`,
)
}
const MAX_PAGE_RANGE = 20
const requestedPages = params.end - params.start + 1
if (requestedPages > MAX_PAGE_RANGE) {
throw new Error(
`Page range (${requestedPages} pages requested) exceeds the maximum limit of ${MAX_PAGE_RANGE} pages per request.`,
)
}
const targetAssetId = params.assetId
if (!userId) {
throw new Error('User ID is required for authorization.')
}
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: targetAssetId,
})
let asset = await prisma.asset.findUnique({
where: { id: targetAssetId },
})
if (!asset) {
throw new Error(`Asset with ID ${targetAssetId} not found.`)
}
if (asset.type === 'version_stack') {
const latestVersion = await prisma.asset.findFirst({
where: { parentId: asset.id, isDeleted: false },
orderBy: { sortIndex: 'asc' },
})
if (latestVersion) {
asset = latestVersion
}
}
const proxyType = (asset.media as PrismaJson.MediaInfo | null)?.proxyType
if (proxyType !== 'pdf') {
throw new Error(`Asset ${targetAssetId} is not a PDF or document.`)
}
// Fetch trigger comment details for comment page and annotations
let commentTimestamp: number | null = null
let annotations: unknown = null
if (userCommentId) {
const comment = await prisma.assetComment.findUnique({
where: { id: userCommentId },
})
if (comment && comment.assetId === targetAssetId) {
commentTimestamp = comment.second !== null ? comment.second : null
if (comment.annotation) {
const list = comment.annotation
if (Array.isArray(list) && list.length > 0) {
annotations = list
}
}
}
}
// Trigger transcode workflow to render PDF pages
const task = await prisma.workflowTask.create({
data: {
assetId: targetAssetId,
projectId: asset.projectId || 'none',
type: WorkflowTaskType.transcode_pdf_pages,
status: WorkflowTaskStatus.pending,
payload: {
projectId: asset.projectId || 'none',
pdfPages: {
start: params.start,
end: params.end,
commentTimestamp,
annotations: annotations as PrismaJson.AnnotationList,
},
},
},
})
const completedTask = await workflowService.executeWait(task)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const output = completedTask.output as any
const pages = output?.pages as Array<{ key: string; page: number }> | undefined
if (!pages || pages.length === 0) {
throw new Error('No PDF page images were generated.')
}
const content: ImageContent[] = []
const sourceKeys: string[] = []
const bucket = process.env.S3_BUCKET || 'shumai'
for (const pageItem of pages) {
const { buffer, contentType } = await s3Service.getObject(bucket, pageItem.key)
const mimeType =
contentType && contentType !== 'application/octet-stream' ? contentType : 'image/webp'
content.push({
type: 'image',
data: buffer.toString('base64'),
mimeType,
})
sourceKeys.push(pageItem.key)
}
return {
content,
details: {
sourceKeys,
},
}
},
}
}
+47 -4
View File
@@ -51,6 +51,8 @@ describe('createReadThreadTool', () => {
id: 'root-1',
message: 'Initial top-level question',
creator: { name: 'Alice' },
second: 12.5,
annotation: [{ type: 'box' }],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
vi.mocked(prisma.assetComment.findMany).mockResolvedValue([])
@@ -59,7 +61,9 @@ describe('createReadThreadTool', () => {
const result = await tool.execute('call-2', { threadId: 'root-1' })
const textContent = (result.content[0] as { text: string }).text
expect(textContent).toContain('Thread Root [Alice] (root-1): Initial top-level question')
expect(textContent).toContain(
'Thread Root [Alice] (id: root-1, time: 12.5s, has_markup: true): Initial top-level question',
)
expect(textContent).toContain('(No replies in this thread yet)')
})
@@ -75,6 +79,8 @@ describe('createReadThreadTool', () => {
id: 'reply-1',
message: 'First reply to thread',
creator: { name: 'Bob' },
second: 15.0,
annotation: [{ type: 'line' }],
},
{
id: 'reply-2',
@@ -88,8 +94,45 @@ describe('createReadThreadTool', () => {
const result = await tool.execute('call-3', { threadId: 'root-1' })
const textContent = (result.content[0] as { text: string }).text
expect(textContent).toContain('Thread Root [Alice] (root-1): Initial top-level question')
expect(textContent).toContain('- [Bob]: First reply to thread')
expect(textContent).toContain('- [Charlie]: Second reply to thread')
expect(textContent).toContain(
'Thread Root [Alice] (id: root-1, has_markup: false): Initial top-level question',
)
expect(textContent).toContain(
'- [Bob] (id: reply-1, time: 15s, has_markup: true): First reply to thread',
)
expect(textContent).toContain(
'- [Charlie] (id: reply-2, has_markup: false): Second reply to thread',
)
})
it('should label agent replies as [Ai Agent] when creator is null but sessionId exists', async () => {
vi.mocked(prisma.assetComment.findUnique).mockResolvedValue({
id: 'root-1',
message: 'Question to agent',
creator: { name: 'Alice' },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
vi.mocked(prisma.assetComment.findMany).mockResolvedValue([
{
id: 'reply-1',
message: 'Agent answer',
creator: null,
sessionId: 'session-123',
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
] as any)
const tool = createReadThreadTool()
const result = await tool.execute('call-agent', { threadId: 'root-1' })
const textContent = (result.content[0] as { text: string }).text
expect(textContent).toContain('- [Ai Agent] (id: reply-1, has_markup: false): Agent answer')
})
it('should describe threadId parameter as referencing <thread id="..." /> in <context>', () => {
const tool = createReadThreadTool()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const description = (tool.parameters as any).properties.threadId.description
expect(description).toContain('<thread id="..." />')
})
})
+23 -5
View File
@@ -5,10 +5,24 @@ import { type AgentTool } from '@earendil-works/pi-agent-core'
const readThreadSchema = Type.Object({
threadId: Type.String({
description:
'The root comment ID of a thread (e.g. from [Thread ID: ...]). Do NOT pass an asset ID or project ID.',
'The root comment ID of a thread (from <thread id="..." /> in the <context> block). Do NOT pass an asset ID or project ID.',
}),
})
function formatCommentMeta(c: {
id: string
second?: number | null
annotation?: unknown
}): string {
const parts = [`id: ${c.id}`]
if (c.second !== null && c.second !== undefined) {
parts.push(`time: ${c.second}s`)
}
const hasMarkup = Boolean(c.annotation && Array.isArray(c.annotation) && c.annotation.length > 0)
parts.push(`has_markup: ${hasMarkup}`)
return `(${parts.join(', ')})`
}
export const createReadThreadTool = (): AgentTool<
typeof readThreadSchema,
{ threadId: string }
@@ -47,15 +61,19 @@ export const createReadThreadTool = (): AgentTool<
include: { creator: true },
})
const rootAuthor = rootComment.creator?.name || 'User'
let output = `Thread Root [${rootAuthor}] (${rootComment.id}): ${rootComment.message || ''}\n\nReplies (${replies.length}):`
const isRootAgent = rootComment.creator?.type === 'agent' || rootComment.sessionId !== null
const rootAuthor = rootComment.creator?.name || (isRootAgent ? 'Ai Agent' : 'User')
const rootMeta = formatCommentMeta(rootComment)
let output = `Thread Root [${rootAuthor}] ${rootMeta}: ${rootComment.message || ''}\n\nReplies (${replies.length}):`
if (replies.length === 0) {
output += '\n(No replies in this thread yet)'
} else {
for (const reply of replies) {
const author = reply.creator?.name || 'User'
output += `\n- [${author}]: ${reply.message || ''}`
const isAgent = reply.creator?.type === 'agent' || reply.sessionId !== null
const author = reply.creator?.name || (isAgent ? 'Ai Agent' : 'User')
const replyMeta = formatCommentMeta(reply)
output += `\n- [${author}] ${replyMeta}: ${reply.message || ''}`
}
}
-156
View File
@@ -1,156 +0,0 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createScreenshotTool } from './screenshot'
import {
prisma,
WorkflowTaskStatus,
type Asset,
type AssetComment,
type User,
type WorkflowTask,
} from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { workflowService } from '@shumai/workflow-core'
import { authzService } from '@shumai/core/src/authz/authz'
vi.mock('@shumai/db', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shumai/db')>()
return {
...actual,
prisma: {
user: {
findUnique: vi.fn(),
},
asset: {
findUnique: vi.fn(),
},
assetComment: {
findUnique: vi.fn(),
},
workflowTask: {
create: vi.fn(),
},
},
}
})
vi.mock('@shumai/core/src/s3/s3', () => ({
s3Service: {
getObject: vi.fn(),
},
}))
vi.mock('@shumai/workflow-core', () => ({
workflowService: {
executeWait: vi.fn(),
},
}))
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn(),
},
Permission: { Read: 'Read' },
ResourceType: { Asset: 'asset' },
}))
describe('screenshotTool', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('should trigger screenshot transcode workflow and return image outputs', async () => {
vi.mocked(prisma.user.findUnique).mockResolvedValue({ id: 'user-1' } as User)
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-1',
projectId: 'project-1',
} as unknown as Asset)
vi.mocked(prisma.assetComment.findUnique).mockResolvedValue({
id: 'comment-1',
assetId: 'asset-1',
second: 5.0,
annotation: [
{
type: 'line',
color: '#00ff00',
points: [
[0, 0],
[1, 1],
],
},
],
} as unknown as AssetComment)
vi.mocked(prisma.workflowTask.create).mockResolvedValue({
id: 'task-1',
} as unknown as WorkflowTask)
vi.mocked(workflowService.executeWait).mockResolvedValue({
id: 'task-1',
status: WorkflowTaskStatus.completed,
output: {
screenshots: [
{ key: 'screenshots/shot1.webp', timestamp: 0.0 },
{ key: 'screenshots/shot2.webp', timestamp: 5.0 },
],
},
} as unknown as WorkflowTask)
vi.mocked(s3Service.getObject).mockImplementation(async (_bucket: string, key: string) => {
return {
buffer: Buffer.from(`fake-bytes-for-${key}`),
contentType: 'image/webp',
} as unknown as { buffer: Buffer; contentType: string }
})
const tool = createScreenshotTool('user-1', 'comment-1')
const result = await tool.execute('call-1', { assetId: 'asset-1', start: 0, end: 10, count: 2 })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'asset-1',
})
expect(prisma.workflowTask.create).toHaveBeenCalledWith({
data: {
assetId: 'asset-1',
projectId: 'project-1',
type: 'transcode_screenshot',
status: 'pending',
payload: {
projectId: 'project-1',
screenshot: {
start: 0,
end: 10,
count: 2,
commentTimestamp: 5.0,
annotations: [
{
type: 'line',
color: '#00ff00',
points: [
[0, 0],
[1, 1],
],
},
],
},
},
},
})
expect(result.content.length).toBe(2)
expect(result.content[0].type).toBe('image')
expect((result.content[0] as unknown as { data: string }).data).toBe(
Buffer.from('fake-bytes-for-screenshots/shot1.webp').toString('base64'),
)
expect((result.content[1] as unknown as { data: string }).data).toBe(
Buffer.from('fake-bytes-for-screenshots/shot2.webp').toString('base64'),
)
expect(result.details.sourceKeys).toEqual(['screenshots/shot1.webp', 'screenshots/shot2.webp'])
})
})
-123
View File
@@ -1,123 +0,0 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { type ImageContent } from '@earendil-works/pi-ai'
import { prisma, WorkflowTaskType, WorkflowTaskStatus, type User } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { workflowService } from '@shumai/workflow-core'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
const screenshotSchema = Type.Object({
assetId: Type.String({
description: 'The asset ID of the video asset. This parameter is required.',
}),
start: Type.Number({ description: 'Start time in seconds' }),
end: Type.Number({ description: 'End time in seconds' }),
count: Type.Number({ description: 'Number of screenshots to take' }),
})
export function createScreenshotTool(
userId: string,
userCommentId?: string | null,
): AgentTool<typeof screenshotSchema> {
return {
name: 'screenshot',
label: 'Take Video Screenshots',
description:
'Takes screenshots for a video at specified interval. It returns the images to the agent context.',
parameters: screenshotSchema,
execute: async (_toolCallId, params) => {
const assetId = params.assetId
if (!userId) {
throw new Error('User ID is required for authorization.')
}
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: assetId,
})
const asset = await prisma.asset.findUnique({
where: { id: assetId },
})
if (!asset) {
throw new Error(`Asset with ID ${assetId} not found.`)
}
// Fetch trigger comment details for timestamp and annotations
let commentTimestamp: number | null = null
let annotations: unknown = null
if (userCommentId) {
const comment = await prisma.assetComment.findUnique({
where: { id: userCommentId },
})
if (comment && comment.assetId === assetId) {
commentTimestamp = comment.second !== null ? comment.second : null
if (comment.annotation) {
const list = comment.annotation
if (Array.isArray(list) && list.length > 0) {
annotations = list
}
}
}
}
// Trigger transcode workflow to take screenshots
const task = await prisma.workflowTask.create({
data: {
assetId,
projectId: asset.projectId || 'none',
type: WorkflowTaskType.transcode_screenshot,
status: WorkflowTaskStatus.pending,
payload: {
projectId: asset.projectId || 'none',
screenshot: {
start: params.start,
end: params.end,
count: params.count,
commentTimestamp,
annotations: annotations as PrismaJson.AnnotationList,
},
},
},
})
const completedTask = await workflowService.executeWait(task)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const output = completedTask.output as any
const screenshots = output?.screenshots as
| Array<{ key: string; timestamp: number }>
| undefined
if (!screenshots || screenshots.length === 0) {
throw new Error('No screenshots were generated.')
}
const content: ImageContent[] = []
const sourceKeys: string[] = []
const bucket = process.env.S3_BUCKET || 'shumai'
for (const shot of screenshots) {
const { buffer, contentType } = await s3Service.getObject(bucket, shot.key)
const mimeType =
contentType && contentType !== 'application/octet-stream' ? contentType : 'image/webp'
content.push({
type: 'image',
data: buffer.toString('base64'),
mimeType,
})
sourceKeys.push(shot.key)
}
return {
content,
details: {
sourceKeys,
},
}
},
}
}
-35
View File
@@ -1,35 +0,0 @@
import { prisma } from '@shumai/db'
import { workflowService } from '@shumai/workflow-core'
import { WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db'
export async function executeAgentToolWorkflow(params: {
toolName: string
args: Record<string, unknown>
userId: string
assetId: string
}): Promise<unknown> {
const task = await prisma.workflowTask.create({
data: {
type: WorkflowTaskType.agent_tool_call,
status: WorkflowTaskStatus.pending,
assetId: params.assetId,
payload: {
projectId: 'none',
agentToolCall: {
toolName: params.toolName,
args: params.args,
userId: params.userId,
},
},
},
})
const completedTask = await workflowService.executeWait(task)
if (completedTask.status === WorkflowTaskStatus.failed) {
const output = completedTask.output as Record<string, unknown> | null
const errorMsg = (output?.error as string) || 'Workflow task failed'
throw new Error(errorMsg)
}
return completedTask.output
}
@@ -1,171 +0,0 @@
import { describe, it, expect } from 'vitest'
import { AgentChatPromptBuilder } from './agent-chat-prompt-builder'
describe('AgentChatPromptBuilder', () => {
it('should build prompt with baseline configurations', () => {
const builder = new AgentChatPromptBuilder('a1')
const result = builder.build()
expect(result).toContain('The user is discussing an asset with ID: a1.')
})
it('should build prompt with path context', () => {
const builder = new AgentChatPromptBuilder('a1').withPathContext('folder/sub/file.png')
const result = builder.build()
expect(result).toContain('Asset Path Context:\nfolder/sub/file.png')
})
it('should build prompt with image asset details', () => {
const builder = new AgentChatPromptBuilder('a1').withAssetDetails(
'test.png',
'image/png',
undefined,
undefined,
'image',
)
const result = builder.build()
expect(result).toContain('File Name: test.png')
expect(result).toContain('File Type: image')
expect(result).toContain("call the 'analyze_image' tool")
expect(result).not.toContain("call the 'screenshot' tool")
})
it('should build prompt with video asset details and duration', () => {
const builder = new AgentChatPromptBuilder('a1').withAssetDetails(
'movie.mp4',
'video/mp4',
12.34,
undefined,
'video',
)
const result = builder.build()
expect(result).toContain('File Name: movie.mp4')
expect(result).toContain('File Type: video')
expect(result).toContain('Video Length: 12.34 seconds')
expect(result).toContain("call the 'screenshot' tool")
expect(result).not.toContain("call the 'analyze_image' tool")
})
it('should build prompt with PDF asset details and total pages', () => {
const builder = new AgentChatPromptBuilder('a1').withAssetDetails(
'doc.pdf',
'application/pdf',
undefined,
15,
'pdf',
)
const result = builder.build()
expect(result).toContain('File Name: doc.pdf')
expect(result).toContain('File Type: pdf')
expect(result).toContain('Total Pages: 15')
expect(result).toContain("call the 'read_pdf_pages' tool")
expect(result).toContain('Maximum 20 pages allowed per call.')
expect(result).not.toContain("call the 'screenshot' tool")
expect(result).not.toContain("call the 'analyze_image' tool")
})
it('should build prompt with comment page for PDF asset', () => {
const builder = new AgentChatPromptBuilder('a1')
.withAssetDetails('doc.pdf', 'application/pdf', undefined, 10, 'pdf')
.withCommentTimestamp(3)
const result = builder.build()
expect(result).toContain('Comment Page: 3')
})
it('should build prompt for PDF continuation mode with comment page', () => {
const builder = new AgentChatPromptBuilder('a1')
.withContinuation(true)
.withAssetDetails('doc.pdf', 'application/pdf', undefined, 10, 'pdf')
.withCommentTimestamp(4)
const result = builder.build()
expect(result).toContain('Comment Page: 4')
expect(result).not.toContain('Comment Timestamp:')
})
it('should build prompt with comment timestamp', () => {
const builder = new AgentChatPromptBuilder('a1').withCommentTimestamp(5.5)
const result = builder.build()
expect(result).toContain('Comment Timestamp: 5.50 seconds')
})
it('should build prompt with all fields combined', () => {
const builder = new AgentChatPromptBuilder('a1')
.withPathContext('src/main.ts')
.withAssetDetails('movie.mp4', 'video/mp4', 12.34, undefined, 'video')
.withCommentTimestamp(8.2)
const result = builder.build()
expect(result).toContain('The user is discussing an asset with ID: a1.')
expect(result).toContain('File Name: movie.mp4')
expect(result).toContain('File Type: video')
expect(result).toContain('Video Length: 12.34 seconds')
expect(result).toContain('Comment Timestamp: 8.20 seconds')
expect(result).toContain('Asset Path Context:\nsrc/main.ts')
expect(result).toContain("call the 'screenshot' tool")
})
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')
})
it('should include location change context block in continuation mode when asset has changed', () => {
const builder = new AgentChatPromptBuilder('a2')
.withContinuation(true)
.withAssetChanged(true)
.withAssetDetails('new-folder', 'folder', undefined, undefined, 'folder')
.withPathContext('projects/my-proj/new-folder')
const result = builder.build()
expect(result).toContain('[Context: User Navigated to a New Location]')
expect(result).toContain('New Location Asset ID: a2')
expect(result).toContain('File Name: new-folder')
expect(result).toContain('Asset Path Context:\nprojects/my-proj/new-folder')
})
it('should build prompt with user info when provided and not continuation', () => {
const builder = new AgentChatPromptBuilder('a1').withUserInfo('Alice', 'owner')
const result = builder.build()
expect(result).toContain('User Info:\nName: Alice\nRole: owner')
})
it('should build prompt with download_asset tool instruction', () => {
const builder = new AgentChatPromptBuilder('a1')
const result = builder.build()
expect(result).toContain("call the 'download_asset' tool with assetId.")
})
})
@@ -1,185 +0,0 @@
export function getSimpleMediaType(proxyType?: string | null): 'image' | 'video' | 'pdf' | 'other' {
if (proxyType === 'image') return 'image'
if (proxyType === 'video') return 'video'
if (proxyType === 'pdf') return 'pdf'
return 'other'
}
export class AgentChatPromptBuilder {
private assetId: string
private assetName?: string
private mediaType?: string
private proxyType?: string
private videoDuration?: number
private totalPages?: number
private commentTimestamp?: number
private pathContext?: string
private attachedFiles: string[] = []
private referencedAssets: string[] = []
private userInfo?: { name: string; role: string }
private isContinuation = false
private hasAssetChanged = false
constructor(assetId: string) {
this.assetId = assetId
}
withUserInfo(name?: string, role?: string): this {
if (name) {
this.userInfo = { name, role: role || 'user' }
}
return this
}
withContinuation(isContinuation: boolean): this {
this.isContinuation = isContinuation
return this
}
withAssetChanged(hasAssetChanged?: boolean): this {
this.hasAssetChanged = !!hasAssetChanged
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
}
withAssetDetails(
name: string,
mediaType: string | null,
duration?: number,
totalPages?: number,
proxyType?: string | null,
): this {
this.assetName = name
this.mediaType = mediaType || undefined
this.proxyType = proxyType || undefined
this.videoDuration = duration
this.totalPages = totalPages
return this
}
withCommentTimestamp(second?: number | null): this {
if (second !== undefined && second !== null) {
this.commentTimestamp = second
}
return this
}
build(): string {
const effectiveType = this.proxyType
if (this.isContinuation) {
let instruction = ''
if (this.hasAssetChanged) {
instruction += `[Context: User Navigated to a New Location]\nNew Location Asset ID: ${this.assetId}`
if (this.assetName) {
instruction += `\nFile Name: ${this.assetName}`
}
if (effectiveType) {
const type = getSimpleMediaType(effectiveType)
instruction += `\nFile Type: ${type}`
if (type === 'video' && this.videoDuration !== undefined) {
instruction += `\nVideo Length: ${this.videoDuration.toFixed(2)} seconds`
} else if (type === 'pdf' && this.totalPages !== undefined) {
instruction += `\nTotal Pages: ${this.totalPages}`
}
}
if (this.pathContext) {
instruction += `\n\nAsset Path Context:\n${this.pathContext}`
}
}
if (this.commentTimestamp !== undefined) {
const type = getSimpleMediaType(effectiveType)
if (instruction) instruction += '\n\n'
if (type === 'pdf') {
instruction += `Comment Page: ${Math.round(this.commentTimestamp)}`
} else {
instruction += `Comment Timestamp: ${this.commentTimestamp.toFixed(2)} seconds`
}
}
if (this.attachedFiles.length > 0 || this.referencedAssets.length > 0) {
if (instruction) instruction += '\n\n'
instruction += `[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}`
}
if (effectiveType) {
const type = getSimpleMediaType(effectiveType)
instruction += `\nFile Type: ${type}`
if (type === 'video' && this.videoDuration !== undefined) {
instruction += `\nVideo Length: ${this.videoDuration.toFixed(2)} seconds`
} else if (type === 'pdf' && this.totalPages !== undefined) {
instruction += `\nTotal Pages: ${this.totalPages}`
}
}
if (this.commentTimestamp !== undefined) {
const type = getSimpleMediaType(effectiveType)
if (type === 'pdf') {
instruction += `\nComment Page: ${Math.round(this.commentTimestamp)}`
} else {
instruction += `\nComment Timestamp: ${this.commentTimestamp.toFixed(2)} seconds`
}
}
if (this.pathContext) {
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 (effectiveType) {
const type = getSimpleMediaType(effectiveType)
if (type === 'image') {
instruction += `\n\nIf you need to view the image data, call the 'analyze_image' tool with assetId "${this.assetId}".`
} else if (type === 'video') {
instruction += `\n\nIf you need to view visual frames or take screenshots of the video, call the 'screenshot' tool. You must specify the 'start' (seconds), 'end' (seconds), and 'count' (number of screenshots) parameters.`
} else if (type === 'pdf') {
instruction += `\n\nIf you need to view pages of the PDF document, call the 'read_pdf_pages' tool. You must specify the 'start' (page number) and 'end' (page number) parameters. Maximum 20 pages allowed per call.`
}
}
instruction += `\n\nIf you need a local file on disk to run bash commands or scripts (e.g. processing or combining assets), call the 'download_asset' tool with assetId.`
if (this.userInfo) {
instruction += `\n\nUser Info:\nName: ${this.userInfo.name}\nRole: ${this.userInfo.role}`
}
return instruction
}
}
+304 -59
View File
@@ -3,7 +3,6 @@ import { agentChat } from './agent-chat'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import * as workflowUtils from '@shumai/workflow-core'
import { AgentChatPromptBuilder } from './agent-chat-prompt-builder'
vi.mock('@shumai/workflow-core', async () => {
const actual = await vi.importActual('@shumai/workflow-core')
@@ -43,6 +42,9 @@ describe('Agent Chat Workflow', () => {
updateCommentActivity: Object.assign(vi.fn(), { _activityName: 'updateCommentActivity' }),
updateTaskUsageActivity: Object.assign(vi.fn(), { _activityName: 'updateTaskUsageActivity' }),
deleteCommentActivity: Object.assign(vi.fn(), { _activityName: 'deleteCommentActivity' }),
getAssetPathHierarchyActivity: Object.assign(vi.fn(), {
_activityName: 'getAssetPathHierarchyActivity',
}),
getAssetPathContextActivity: Object.assign(vi.fn(), {
_activityName: 'getAssetPathContextActivity',
}),
@@ -70,13 +72,21 @@ describe('Agent Chat Workflow', () => {
})
mockActivities.getCommentActivity.mockResolvedValue({
id: 'c1',
creatorId: 'user-c1',
message: 'hello agent',
replyToId: null,
attachments: [],
})
mockActivities.initializeAgentSessionActivity.mockResolvedValue('session-123')
mockActivities.getAgentChatContextActivity.mockResolvedValue({ agent: { id: 'b1' } })
mockActivities.getAssetPathContextActivity.mockResolvedValue('Path: folder/subfolder/file.png')
mockActivities.getAssetPathHierarchyActivity.mockResolvedValue({
path: 'folder/subfolder/test-file.png',
ancestors: [
{ id: 'f1', name: 'folder' },
{ id: 'f2', name: 'subfolder' },
],
})
mockActivities.getAssetPathContextActivity.mockResolvedValue('folder/subfolder/test-file.png')
mockActivities.agentChatActivity.mockResolvedValue({
text: 'Hello user, how can I help you?',
sessionId: 'session-123',
@@ -119,7 +129,6 @@ describe('Agent Chat Workflow', () => {
expect(mockActivities.getCommentActivity).toHaveBeenCalledWith('c1')
// Verify agent context fetched with the project context forwarded
// (required for project-aware role enforcement in the activity)
expect(mockActivities.getAgentChatContextActivity).toHaveBeenCalledWith({
teamId: 't1',
agentId: 'b1',
@@ -144,22 +153,31 @@ describe('Agent Chat Workflow', () => {
userId: undefined,
})
// Verify agent instruction composition
const expectedInstruction1 = new AgentChatPromptBuilder('a1')
.withPathContext('Path: folder/subfolder/file.png')
.withAssetDetails('test-file.png', 'image/png', undefined, undefined, 'image')
.withCommentTimestamp(null)
.build()
// Verify agent chat called with structured messageContext
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
teamId: 't1',
agentId: 'b1',
message: 'hello agent',
agentsInstruction: expectedInstruction1,
sessionId: 'session-123',
folderId: 'parent-folder-id',
assetId: 'a1',
messageContext: expect.objectContaining({
user: { id: 'user-c1', name: 'Test User', role: 'owner' },
currentAsset: expect.objectContaining({
id: 'a1',
name: 'test-file.png',
type: 'file',
mediaType: 'image',
mimeType: 'image/png',
parentId: 'parent-folder-id',
path: 'folder/subfolder/test-file.png',
ancestors: [
{ id: 'f1', name: 'folder' },
{ id: 'f2', name: 'subfolder' },
],
}),
}),
}),
)
@@ -211,17 +229,13 @@ describe('Agent Chat Workflow', () => {
await agentChat(task)
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)
.build()
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
sessionId: 'existing-session-456',
agentsInstruction: expectedInstruction2,
messageContext: expect.objectContaining({
currentAsset: expect.objectContaining({ id: 'a1', name: 'test-file.png' }),
}),
}),
)
@@ -273,16 +287,9 @@ describe('Agent Chat Workflow', () => {
await agentChat(task)
const expectedInstruction4 = new AgentChatPromptBuilder('a1')
.withPathContext('Path: folder/subfolder/file.png')
.withAssetDetails('test-file.png', 'image/png', undefined, undefined, 'image')
.withCommentTimestamp(null)
.build()
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
imageUrls: ['attachments/image1.png', 'attachments/image2.jpg'],
agentsInstruction: expectedInstruction4,
}),
)
})
@@ -315,15 +322,13 @@ describe('Agent Chat Workflow', () => {
await agentChat(task)
const expectedInstruction5 = new AgentChatPromptBuilder('a1')
.withPathContext('Path: folder/subfolder/file.png')
.withAssetDetails('test-asset', 'video/mp4', 10)
.withCommentTimestamp(null)
.build()
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
agentsInstruction: expectedInstruction5,
messageContext: expect.objectContaining({
currentAsset: expect.objectContaining({
durationSeconds: 10,
}),
}),
}),
)
})
@@ -391,6 +396,7 @@ describe('Agent Chat Workflow', () => {
name: 'attachment.png',
type: 'file',
mediaType: 'image/png',
media: { proxyType: 'image' },
projectId: 'p1',
}
}
@@ -400,6 +406,7 @@ describe('Agent Chat Workflow', () => {
name: 'ref-folder',
type: 'folder',
mediaType: null,
media: null,
projectId: 'p1',
}
}
@@ -408,6 +415,7 @@ describe('Agent Chat Workflow', () => {
name: 'test-file.png',
type: 'file',
mediaType: 'image/png',
media: { proxyType: 'image' },
projectId: 'p1',
project: { teamId: 't1' },
}
@@ -443,20 +451,7 @@ describe('Agent Chat Workflow', () => {
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)
.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()
// Verify messageContext is populated with attachedFiles and referencedAssets
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
teamId: 't1',
@@ -465,15 +460,32 @@ describe('Agent Chat Workflow', () => {
imageUrls: [],
projectId: 'p1',
folderId: '',
agentsInstruction: expectedInstruction,
sessionId: 'session-direct-123',
userId: undefined,
userCommentId: undefined,
context: { agent: { id: 'b1' } },
attachedAssets: [
{ id: 'file-attachment-1', name: 'attachment.png', type: 'file' },
{ id: 'referenced-asset-1', name: 'ref-folder', type: 'folder' },
],
messageContext: expect.objectContaining({
attachedFiles: [
{
id: 'file-attachment-1',
name: 'attachment.png',
type: 'file',
mediaType: 'image',
mimeType: 'image/png',
path: 'attachment.png',
},
],
referencedAssets: [
{
id: 'referenced-asset-1',
name: 'ref-folder',
type: 'folder',
mediaType: undefined,
mimeType: undefined,
path: 'ref-folder',
},
],
}),
}),
)
})
@@ -512,19 +524,18 @@ describe('Agent Chat Workflow', () => {
await agentChat(task)
const expectedInstruction = new AgentChatPromptBuilder('v2-latest')
.withContinuation(true)
.withPathContext('Path: folder/subfolder/file.png')
.withAssetDetails('latest-video.mp4', 'video/mp4', 42, undefined, 'video')
.build()
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
teamId: 't1',
agentId: 'b1',
message: 'Explain this version stack video',
agentsInstruction: expectedInstruction,
sessionId: 'session-vs-123',
messageContext: expect.objectContaining({
currentAsset: expect.objectContaining({
id: 'v2-latest',
durationSeconds: 42,
}),
}),
}),
)
})
@@ -557,7 +568,241 @@ describe('Agent Chat Workflow', () => {
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
agentsInstruction: expect.stringContaining('User Info:\nName: Test User\nRole: owner'),
messageContext: expect.objectContaining({
user: { id: 'user-alice', name: 'Test User', role: 'owner' },
}),
}),
)
})
it('should populate comment attachments into structured messageContext.attachedFiles', async () => {
mockActivities.getCommentActivity.mockResolvedValue({
id: 'c1',
creatorId: 'user-c1',
message: 'check these attachments',
replyToId: null,
attachments: [
{
asset: {
id: 'att-doc-1',
name: 'brief.pdf',
type: 'file',
mediaType: 'application/pdf',
media: { proxyType: 'pdf' },
storageKey: { key: 'brief.pdf' },
},
},
{
asset: {
id: 'att-img-1',
name: 'mockup.png',
type: 'file',
mediaType: 'image/png',
media: { proxyType: 'image' },
storageKey: { key: 'mockup.png' },
},
},
],
})
mockActivities.getAssetPathContextActivity.mockImplementation(async (id: string) => {
if (id === 'att-doc-1') return 'Docs/brief.pdf'
if (id === 'att-img-1') return 'Designs/mockup.png'
return 'folder/subfolder/file.png'
})
const task = await prisma.workflowTask.create({
data: {
type: 'chat',
status: 'pending',
assetId: 'a1',
payload: {
projectId: 'p1',
agent: { userCommentId: 'c1', agentId: 'b1' },
},
},
})
await agentChat(task)
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
imageUrls: ['mockup.png'],
messageContext: expect.objectContaining({
attachedFiles: [
{
id: 'att-doc-1',
name: 'brief.pdf',
type: 'file',
mediaType: 'pdf',
mimeType: 'application/pdf',
path: 'Docs/brief.pdf',
},
{
id: 'att-img-1',
name: 'mockup.png',
type: 'file',
mediaType: 'image',
mimeType: 'image/png',
path: 'Designs/mockup.png',
},
],
}),
}),
)
})
it('should set totalFrames for video assets and totalPages for pdf assets in currentAsset context', async () => {
mockActivities.getAssetActivity.mockImplementation(async (id: string) => {
if (id === 'video-asset-1') {
return {
id: 'video-asset-1',
project: { teamId: 't1' },
type: 'file',
name: 'clip.mp4',
mediaType: 'video/mp4',
media: { proxyType: 'video', duration: 9, frames: 268 },
parentId: 'f1',
}
}
if (id === 'pdf-asset-1') {
return {
id: 'pdf-asset-1',
project: { teamId: 't1' },
type: 'file',
name: 'doc.pdf',
mediaType: 'application/pdf',
media: { proxyType: 'pdf', frames: 15, duration: 0 },
parentId: 'f1',
}
}
return null
})
// Test Video asset
const videoTask = await prisma.workflowTask.create({
data: {
type: 'chat',
status: 'pending',
assetId: 'video-asset-1',
payload: {
projectId: 'p1',
agent: { prompt: 'Analyze video', agentId: 'b1', sessionId: 'session-video-1' },
},
},
})
await agentChat(videoTask)
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
messageContext: expect.objectContaining({
currentAsset: expect.objectContaining({
id: 'video-asset-1',
mediaType: 'video',
durationSeconds: 9,
totalFrames: 268,
totalPages: undefined,
}),
}),
}),
)
// Test PDF asset
const pdfTask = await prisma.workflowTask.create({
data: {
type: 'chat',
status: 'pending',
assetId: 'pdf-asset-1',
payload: {
projectId: 'p1',
agent: { prompt: 'Analyze pdf', agentId: 'b1', sessionId: 'session-pdf-1' },
},
},
})
await agentChat(pdfTask)
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
messageContext: expect.objectContaining({
currentAsset: expect.objectContaining({
id: 'pdf-asset-1',
mediaType: 'pdf',
totalPages: 15,
totalFrames: undefined,
durationSeconds: undefined,
}),
}),
}),
)
})
it('should include annotations and second in direct chat messageContext', async () => {
const task = await prisma.workflowTask.create({
data: {
type: 'chat',
status: 'pending',
assetId: 'a1',
payload: {
projectId: 'p1',
agent: {
prompt: 'Look at the highlighted zone',
agentId: 'b1',
sessionId: 'session-markup-1',
second: 14.2,
annotations: [{ type: 'rectangle', x: 20, y: 30, width: 40, height: 50 }],
},
},
},
})
await agentChat(task)
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
messageContext: expect.objectContaining({
position: { type: 'time', seconds: 14.2 },
annotations: [{ type: 'rectangle', x: 20, y: 30, width: 40, height: 50 }],
}),
}),
)
})
it('should include userCommentId as id in comment messageContext', async () => {
mockActivities.getCommentActivity.mockResolvedValue({
id: 'comment-with-id-123',
creatorId: 'user-c1',
message: 'Check this out',
replyToId: null,
second: 5.0,
annotation: [{ type: 'arrow' }],
attachments: [],
})
const task = await prisma.workflowTask.create({
data: {
type: 'chat',
status: 'pending',
assetId: 'a1',
payload: {
projectId: 'p1',
agent: {
agentId: 'b1',
userCommentId: 'comment-with-id-123',
},
},
},
})
await agentChat(task)
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
messageContext: expect.objectContaining({
id: 'comment-with-id-123',
position: { type: 'time', seconds: 5.0 },
annotation: true,
annotations: [{ type: 'arrow' }],
}),
}),
)
})
+158 -32
View File
@@ -1,7 +1,13 @@
import { ApplicationFailure } from '@temporalio/workflow'
import type { WorkflowTask } from '@shumai/db'
import { executeActivity, getActivities, TaskQueueAgent } from '@shumai/workflow-core'
import { AgentChatPromptBuilder } from './agent-chat-prompt-builder'
import type {
ShumaiMessageContext,
ShumaiAssetContext,
ShumaiAttachedFileContext,
ShumaiMediaPosition,
} from '@shumai/dtos'
import { getProxyType } from '@shumai/core/src/utils/mime'
export async function agentChat(task: WorkflowTask): Promise<void> {
const {
@@ -15,6 +21,7 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
updateTaskUsageActivity,
getAgentWorkerQueueActivity,
initializeAgentSessionActivity,
getAssetPathHierarchyActivity,
getAssetPathContextActivity,
generateSessionNameActivity,
getUserTeamInfoActivity,
@@ -47,19 +54,33 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
// 1. Get User Comment
const userCommentId = payload.agent?.userCommentId
let userComment: Awaited<ReturnType<typeof getCommentActivity>> | null = null
let prompt = ''
let attachmentImageUrls: string[] = []
let commentTimestamp: number | undefined
let commentAnnotation = false
let commentCreatorId: string | undefined
let commentAnnotations: unknown[] | undefined
if (userCommentId) {
const userComment = await executeActivity(agentWorkerQueue, getCommentActivity, userCommentId)
userComment = await executeActivity(agentWorkerQueue, getCommentActivity, userCommentId)
if (!userComment) {
throw ApplicationFailure.create({ message: 'User comment not found', nonRetryable: true })
}
prompt = userComment.message || ''
commentCreatorId = userComment.creatorId || undefined
if (userComment.second !== null && userComment.second !== undefined) {
commentTimestamp = userComment.second
}
if (
userComment.annotation &&
Array.isArray(userComment.annotation) &&
userComment.annotation.length > 0
) {
commentAnnotation = true
commentAnnotations = userComment.annotation
}
// 2. Create Placeholder Comment
const placeholder = await executeActivity(agentWorkerQueue, createCommentActivity, {
@@ -71,7 +92,7 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
})
placeholderCommentId = placeholder.id
// 5. Prepare Images (Attachments only)
// Prepare Images (Attachments only)
if (userComment.attachments) {
for (const att of userComment.attachments) {
const attProxyType = (att.asset?.media as PrismaJson.MediaInfo | null)?.proxyType
@@ -83,6 +104,17 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
} else {
prompt = payload.agent?.prompt || ''
attachmentImageUrls = payload.agent?.imageUrls || []
if (payload.agent?.second !== null && payload.agent?.second !== undefined) {
commentTimestamp = payload.agent.second
}
if (
payload.agent?.annotations &&
Array.isArray(payload.agent.annotations) &&
payload.agent.annotations.length > 0
) {
commentAnnotation = true
commentAnnotations = payload.agent.annotations
}
}
// 3. Get Asset
@@ -118,43 +150,107 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
projectId: payload.projectId,
})
const pathContext = await executeActivity(
const pathHierarchy = await executeActivity(
agentWorkerQueue,
getAssetPathContextActivity,
getAssetPathHierarchyActivity,
task.assetId,
)
const assetPath = pathHierarchy?.path || ''
const ancestors = pathHierarchy?.ancestors || []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mediaInfo = asset.media as any
const duration = mediaInfo?.duration
const totalPages = mediaInfo?.frames || mediaInfo?.metadata?.totalFrames
const proxyType =
(asset.media as PrismaJson.MediaInfo | null)?.proxyType ||
getProxyType(asset.mediaType, asset.name) ||
undefined
const duration =
(proxyType === 'video' || proxyType === 'audio') && typeof mediaInfo?.duration === 'number'
? mediaInfo.duration
: undefined
const totalPages =
proxyType === 'pdf' ? mediaInfo?.frames || mediaInfo?.metadata?.totalFrames : undefined
const totalFrames =
proxyType === 'video' ? mediaInfo?.frames || mediaInfo?.metadata?.totalFrames : undefined
// Resolve Attached Files Details
const attachedFileDetailsList: string[] = []
const attachedFiles: ShumaiAttachedFileContext[] = []
const attachedAssets: Array<{ id: string; name: string; type: string }> = []
if (payload.agent?.attachedFiles) {
if (userCommentId && userComment?.attachments) {
for (const att of userComment.attachments) {
if (att.asset) {
const filePath = await executeActivity(
agentWorkerQueue,
getAssetPathContextActivity,
att.asset.id,
)
const attProxyType =
(att.asset.media as PrismaJson.MediaInfo | null)?.proxyType ||
getProxyType(att.asset.mediaType, att.asset.name) ||
undefined
attachedFiles.push({
id: att.asset.id,
name: att.asset.name,
type: att.asset.type,
mediaType: attProxyType as ShumaiAttachedFileContext['mediaType'],
mimeType: att.asset.mediaType || undefined,
path: filePath || undefined,
url: (att.asset as { url?: string }).url || undefined,
})
attachedAssets.push({ id: att.asset.id, name: att.asset.name, type: att.asset.type })
}
}
} else 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})`,
const filePath = await executeActivity(
agentWorkerQueue,
getAssetPathContextActivity,
fileId,
)
const fileProxyType =
(file.media as PrismaJson.MediaInfo | null)?.proxyType ||
getProxyType(file.mediaType, file.name) ||
undefined
attachedFiles.push({
id: file.id,
name: file.name,
type: file.type,
mediaType: fileProxyType as ShumaiAttachedFileContext['mediaType'],
mimeType: file.mediaType || undefined,
path: filePath || undefined,
url: (file as { url?: string }).url || undefined,
})
attachedAssets.push({ id: file.id, name: file.name, type: file.type })
}
}
}
// Resolve Referenced Workspace Assets Details
const referencedAssetDetailsList: string[] = []
const referencedAssets: ShumaiAttachedFileContext[] = []
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})`,
const filePath = await executeActivity(
agentWorkerQueue,
getAssetPathContextActivity,
assetId,
)
const refProxyType =
(referencedAsset.media as PrismaJson.MediaInfo | null)?.proxyType ||
getProxyType(referencedAsset.mediaType, referencedAsset.name) ||
undefined
referencedAssets.push({
id: referencedAsset.id,
name: referencedAsset.name,
type: referencedAsset.type,
mediaType: refProxyType as ShumaiAttachedFileContext['mediaType'],
mimeType: referencedAsset.mediaType || undefined,
path: filePath || undefined,
url: (referencedAsset as { url?: string }).url || undefined,
})
attachedAssets.push({
id: referencedAsset.id,
name: referencedAsset.name,
@@ -164,29 +260,59 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
}
}
// Resolve User Info
const targetUserId = userCommentId ? commentCreatorId : payload.agent?.userId
let userInfo: { name: string; role: string } | null = null
if (!userCommentId && isNewChat && payload.agent?.userId) {
if (targetUserId) {
userInfo = await executeActivity(agentWorkerQueue, getUserTeamInfoActivity, {
userId: payload.agent.userId,
userId: targetUserId,
teamId,
})
}
const proxyType = (asset.media as PrismaJson.MediaInfo | null)?.proxyType
const promptBuilder = new AgentChatPromptBuilder(asset.id)
.withContinuation(!isNewChat)
.withAssetChanged(payload.agent?.hasAssetChanged)
.withPathContext(pathContext)
.withAssetDetails(asset.name, asset.mediaType, duration, totalPages, proxyType)
.withCommentTimestamp(commentTimestamp)
.withAttachedFiles(attachedFileDetailsList)
.withReferencedAssets(referencedAssetDetailsList)
if (userInfo) {
promptBuilder.withUserInfo(userInfo.name, userInfo.role)
// Build position
let mediaPosition: ShumaiMediaPosition | undefined
if (commentTimestamp !== undefined && commentTimestamp !== null) {
if (proxyType === 'pdf') {
mediaPosition = { type: 'page', page: Math.round(commentTimestamp) }
} else {
mediaPosition = { type: 'time', seconds: commentTimestamp }
}
}
const instruction = promptBuilder.build()
// Build Current Asset Context
const currentAssetContext: ShumaiAssetContext = {
id: asset.id,
name: asset.name,
type: asset.type,
mediaType: proxyType as ShumaiAssetContext['mediaType'],
mimeType: asset.mediaType || undefined,
parentId: asset.parentId || undefined,
path: assetPath || undefined,
durationSeconds: duration !== undefined ? duration : undefined,
totalFrames: totalFrames !== undefined ? totalFrames : undefined,
totalPages: totalPages !== undefined ? totalPages : undefined,
navigated: payload.agent?.hasAssetChanged === true ? true : undefined,
ancestors: ancestors.length > 0 ? ancestors : undefined,
}
// Build structured ShumaiMessageContext
const messageContext: ShumaiMessageContext = {
...(userCommentId ? { id: userCommentId } : {}),
...(userInfo
? { user: { id: targetUserId!, name: userInfo.name, role: userInfo.role } }
: {}),
currentAsset: currentAssetContext,
...(mediaPosition ? { position: mediaPosition } : {}),
...(commentAnnotation
? {
annotation: true,
annotations: commentAnnotations as ShumaiMessageContext['annotations'],
}
: {}),
...(attachedFiles.length > 0 ? { attachedFiles } : {}),
...(referencedAssets.length > 0 ? { referencedAssets } : {}),
}
// 6. Call AI Chat
let folderId = ''
@@ -205,11 +331,11 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
projectId: payload.projectId,
folderId,
assetId: asset.id,
agentsInstruction: instruction,
sessionId,
userId: payload.agent?.userId,
userCommentId: userCommentId || undefined,
context,
messageContext,
attachedAssets,
})
@@ -1,140 +0,0 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { agentToolCall } from './agent-tool-call'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import * as workflowUtils from '@shumai/workflow-core'
vi.mock('@shumai/workflow-core', async () => {
const actual = await vi.importActual('@shumai/workflow-core')
return {
...actual,
getActivities: vi.fn(),
executeActivity: vi.fn(),
}
})
describe('Agent Tool Call Workflow', () => {
setupTestDbHooks()
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mockActivities holds mock functions cast to expected types
let mockActivities: any
beforeEach(() => {
vi.clearAllMocks()
mockActivities = {
updateTaskStatusActivity: Object.assign(vi.fn(), {
_activityName: 'updateTaskStatusActivity',
}),
executeAgentToolActivity: Object.assign(vi.fn(), {
_activityName: 'executeAgentToolActivity',
}),
getAgentWorkerQueueActivity: Object.assign(vi.fn(), {
_activityName: 'getAgentWorkerQueueActivity',
}),
}
mockActivities.getAgentWorkerQueueActivity.mockResolvedValue('agent_queue')
mockActivities.updateTaskStatusActivity.mockResolvedValue({})
mockActivities.executeAgentToolActivity.mockResolvedValue({ success: true, count: 42 })
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mockActivities contains vi.fn mock functions which are cast to expected activity proxy types
vi.mocked(workflowUtils.getActivities).mockReturnValue(mockActivities as any)
vi.mocked(workflowUtils.executeActivity).mockImplementation(async (_queue, act, ...args) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- act is one of the mocked activities
return (act as any)(...args)
})
})
it('should run agent tool call workflow successfully', async () => {
const task = await prisma.workflowTask.create({
data: {
type: 'agent_tool_call',
status: 'pending',
assetId: 'a1',
payload: {
projectId: 'p1',
agentToolCall: {
toolName: 'list_assets',
args: { parent: 'folder-123' },
userId: 'user-456',
},
},
},
})
await agentToolCall(task)
// Verify queue discovery
expect(mockActivities.getAgentWorkerQueueActivity).toHaveBeenCalled()
// Verify task processing status
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: task.id,
status: 'processing',
})
// Verify tool execution
expect(mockActivities.executeAgentToolActivity).toHaveBeenCalledWith({
taskId: task.id,
toolName: 'list_assets',
args: { parent: 'folder-123' },
userId: 'user-456',
})
// Verify task completed status with output
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: task.id,
status: 'completed',
output: { success: true, count: 42 },
})
})
it('should throw if payload or agentToolCall is missing', async () => {
const task = await prisma.workflowTask.create({
data: {
type: 'agent_tool_call',
status: 'pending',
assetId: 'a1',
},
})
await expect(agentToolCall(task)).rejects.toThrow('Task payload or agentToolCall is missing')
// Verify status updated to failed
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: task.id,
status: 'failed',
output: { error: 'Task payload or agentToolCall is missing' },
})
})
it('should handle tool execution failures by updating task to failed and throwing', async () => {
mockActivities.executeAgentToolActivity.mockRejectedValue(new Error('Authz failure'))
const task = await prisma.workflowTask.create({
data: {
type: 'agent_tool_call',
status: 'pending',
assetId: 'a1',
payload: {
projectId: 'p1',
agentToolCall: {
toolName: 'create_folder',
args: { parent: 'folder-123', name: 'new-folder' },
userId: 'user-456',
},
},
},
})
await expect(agentToolCall(task)).rejects.toThrow('Authz failure')
// Verify task status failed
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: task.id,
status: 'failed',
output: { error: 'Authz failure' },
})
})
})
@@ -1,60 +0,0 @@
import { ApplicationFailure } from '@temporalio/workflow'
import type { WorkflowTask } from '@shumai/db'
import { getActivities, executeActivity, TaskQueueAgent } from '@shumai/workflow-core'
export async function agentToolCall(task: WorkflowTask): Promise<void> {
const { updateTaskStatusActivity, executeAgentToolActivity, getAgentWorkerQueueActivity } =
getActivities()
let agentWorkerQueue = ''
try {
// 0. Discover queue
agentWorkerQueue = await executeActivity(TaskQueueAgent, getAgentWorkerQueueActivity)
// Update status to processing
await executeActivity(agentWorkerQueue, updateTaskStatusActivity, {
taskId: task.id,
status: 'processing',
})
const payload = task.payload
if (!payload || !payload.agentToolCall) {
throw ApplicationFailure.create({
message: 'Task payload or agentToolCall is missing',
nonRetryable: true,
})
}
const { toolName, args, userId } = payload.agentToolCall
// Execute the agent activity on specific queue
const result = await executeActivity(agentWorkerQueue, executeAgentToolActivity, {
taskId: task.id,
toolName,
args,
userId,
})
// Update status to completed with output
await executeActivity(agentWorkerQueue, updateTaskStatusActivity, {
taskId: task.id,
status: 'completed',
output: result,
})
} catch (err) {
console.error(`AgentToolCall failed for task ${task.id}:`, err)
// Update status to failed
if (agentWorkerQueue) {
await executeActivity(agentWorkerQueue, updateTaskStatusActivity, {
taskId: task.id,
status: 'failed',
output: { error: err instanceof Error ? err.message : String(err) },
})
}
throw err
}
}
export const agentToolCallWorkflow = agentToolCall
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/api",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"main": "./src/index.ts",
+42
View File
@@ -148,6 +148,48 @@ describe('Chat API', () => {
)
})
it('accepts chat request with annotations, second, and attachmentIds', 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: 'Review annotation',
attachedFiles: ['att1'],
annotations: [{ type: 'rectangle', x: 10, y: 20 }],
second: 5.5,
}),
})
expect(res.status).toBe(200)
expect(chatService.startOrContinueChat).toHaveBeenCalledWith(
expect.objectContaining({ id: 'user1' }),
'team1',
expect.objectContaining({
agentId: 'agent1',
textPrompt: 'Review annotation',
attachedFiles: ['att1'],
annotations: [{ type: 'rectangle', x: 10, y: 20 }],
second: 5.5,
}),
)
})
it('sends ping keep-alive SSE events when there are no new messages', async () => {
vi.mocked(chatService.startOrContinueChat).mockResolvedValue({
sessionId: 'session1',
+192 -2
View File
@@ -163,13 +163,13 @@ describe('provider api', () => {
const res = await app.request('/providers/p1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ config, models }),
body: JSON.stringify({ name: 'new-name', config, models }),
})
expect(res.status).toBe(200)
const data = await res.json()
expect(data.config.baseUrl).toBe('https://new-url.com')
expect(providerService.update).toHaveBeenCalledWith('t1', 'p1', config, models)
expect(providerService.update).toHaveBeenCalledWith('t1', 'p1', config, models, 'new-name')
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: expect.anything(),
permission: Permission.Admin,
@@ -184,6 +184,111 @@ describe('provider api', () => {
})
})
it('POST /providers/:id/models creates a model', async () => {
vi.mocked(providerService.getById).mockResolvedValue({
id: 'p1',
teamId: 't1',
} as unknown as Awaited<ReturnType<typeof providerService.getById>>)
const modelData = {
modelId: 'm1',
name: 'Model 1',
config: {
reasoning: true,
input: ['text' as const],
contextWindow: 128000,
maxTokens: 4096,
cost: { input: 1, output: 2, cacheRead: 0, cacheWrite: 0 },
},
}
vi.mocked(providerService.createModel).mockResolvedValue({
id: 'm-db-1',
providerId: 'p1',
modelId: 'm1',
name: 'Model 1',
config: modelData.config,
createdAt: new Date(),
updatedAt: new Date(),
})
const res = await app.request('/providers/p1/models', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(modelData),
})
expect(res.status).toBe(201)
const data = await res.json()
expect(data.id).toBe('m-db-1')
expect(providerService.createModel).toHaveBeenCalledWith('t1', 'p1', modelData)
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: expect.anything(),
permission: Permission.Admin,
type: ResourceType.Provider,
id: 'p1',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'provider_update',
teamId: 't1',
userId: 'user1',
itemId: 'p1',
})
})
it('PUT /providers/:id/models/:modelDbId updates a model', async () => {
vi.mocked(providerService.getById).mockResolvedValue({
id: 'p1',
teamId: 't1',
} as unknown as Awaited<ReturnType<typeof providerService.getById>>)
const updateData = {
name: 'Updated Model',
}
vi.mocked(providerService.updateModel).mockResolvedValue({
id: 'm-db-1',
providerId: 'p1',
modelId: 'm1',
name: 'Updated Model',
config: {
reasoning: false,
input: ['text'],
contextWindow: 128000,
maxTokens: 4096,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
createdAt: new Date(),
updatedAt: new Date(),
})
const res = await app.request('/providers/p1/models/m-db-1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updateData),
})
expect(res.status).toBe(200)
const data = await res.json()
expect(data.name).toBe('Updated Model')
expect(providerService.updateModel).toHaveBeenCalledWith('t1', 'p1', 'm-db-1', updateData)
})
it('DELETE /providers/:id/models/:modelDbId deletes a model', async () => {
vi.mocked(providerService.getById).mockResolvedValue({
id: 'p1',
teamId: 't1',
} as unknown as Awaited<ReturnType<typeof providerService.getById>>)
vi.mocked(providerService.deleteModel).mockResolvedValue({
id: 'm-db-1',
} as unknown as Awaited<ReturnType<typeof providerService.deleteModel>>)
const res = await app.request('/providers/p1/models/m-db-1', {
method: 'DELETE',
})
expect(res.status).toBe(200)
const data = await res.json()
expect(data.success).toBe(true)
expect(providerService.deleteModel).toHaveBeenCalledWith('t1', 'p1', 'm-db-1')
})
it('DELETE /providers/:id deletes provider', async () => {
vi.mocked(providerService.getById).mockResolvedValue({
id: 'p1',
@@ -214,4 +319,89 @@ describe('provider api', () => {
itemId: 'p1',
})
})
it('POST /teams/:teamId/providers/sync-check checks for updates', async () => {
vi.mocked(providerService.checkUpdates).mockResolvedValue({
providers: [
{
name: 'deepseek',
isNewProvider: true,
config: { api: 'openai-responses', apiKey: 'DEEPSEEK_API_KEY' },
models: [],
},
],
totalNewProviders: 1,
totalNewModels: 0,
})
const res = await app.request('/teams/t1/providers/sync-check', {
method: 'POST',
})
expect(res.status).toBe(200)
const data = await res.json()
expect(data.totalNewProviders).toBe(1)
expect(data.providers).toHaveLength(1)
expect(providerService.checkUpdates).toHaveBeenCalledWith('t1')
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: expect.anything(),
permission: Permission.Admin,
type: ResourceType.Team,
id: 't1',
})
})
it('POST /teams/:teamId/providers/sync-apply applies sync selection', async () => {
vi.mocked(providerService.applySync).mockResolvedValue({
addedProviders: 1,
addedModels: 3,
})
const payload = {
providers: [
{
name: 'deepseek',
isNewProvider: true,
config: { api: 'openai-responses', apiKey: 'KEY' },
models: [
{
modelId: 'deepseek-v4-flash',
name: 'DeepSeek V4 Flash',
config: {
api: 'openai-responses',
reasoning: true,
input: ['text'],
contextWindow: 128000,
maxTokens: 4096,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
},
],
},
],
}
const res = await app.request('/teams/t1/providers/sync-apply', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
})
expect(res.status).toBe(200)
const data = await res.json()
expect(data.addedProviders).toBe(1)
expect(data.addedModels).toBe(3)
expect(providerService.applySync).toHaveBeenCalledWith('t1', payload)
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: expect.anything(),
permission: Permission.Admin,
type: ResourceType.Team,
id: 't1',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'provider_update',
teamId: 't1',
userId: 'user1',
})
})
})
+130 -3
View File
@@ -2,7 +2,14 @@ import { zValidator } from '@hono/zod-validator'
import { Hono } from 'hono'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { providerService } from '@shumai/core/src/provider/provider'
import { createProviderRequestSchema, updateProviderRequestSchema, AuditAction } from '@shumai/dtos'
import {
createModelRequestSchema,
createProviderRequestSchema,
syncApplyRequestSchema,
updateModelRequestSchema,
updateProviderRequestSchema,
AuditAction,
} from '@shumai/dtos'
import type { Prisma } from '@shumai/db'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
@@ -24,6 +31,46 @@ const route = new Hono<{ Variables: { user: User } }>()
return c.json(providers)
})
.post('/teams/:teamId/providers/sync-check', async (c) => {
const teamId = c.req.param('teamId')
const user = c.get('user')
await authzService.hasPermission({
user,
permission: Permission.Admin,
type: ResourceType.Team,
id: teamId,
})
const result = await providerService.checkUpdates(teamId)
return c.json(result)
})
.post(
'/teams/:teamId/providers/sync-apply',
zValidator('json', syncApplyRequestSchema),
async (c) => {
const teamId = c.req.param('teamId')
const user = c.get('user')
const body = c.req.valid('json')
await authzService.hasPermission({
user,
permission: Permission.Admin,
type: ResourceType.Team,
id: teamId,
})
const result = await providerService.applySync(teamId, body)
await auditLogService.logAction({
action: AuditAction.provider_update,
teamId,
userId: user?.id,
})
return c.json(result)
},
)
.get('/providers/:id/models', async (c) => {
const id = c.req.param('id')
const user = c.get('user')
@@ -67,7 +114,7 @@ const route = new Hono<{ Variables: { user: User } }>()
.put('/providers/:id', zValidator('json', updateProviderRequestSchema), async (c) => {
const id = c.req.param('id')
const user = c.get('user')
const { config, models } = c.req.valid('json')
const { name, config, models } = c.req.valid('json')
await authzService.hasPermission({
user,
@@ -79,7 +126,7 @@ const route = new Hono<{ Variables: { user: User } }>()
const providerBefore = await providerService.getById(id)
if (!providerBefore) throw new Error('Provider not found')
const provider = await providerService.update(providerBefore.teamId, id, config, models)
const provider = await providerService.update(providerBefore.teamId, id, config, models, name)
await auditLogService.logAction({
action: AuditAction.provider_update,
teamId: providerBefore.teamId,
@@ -89,6 +136,86 @@ const route = new Hono<{ Variables: { user: User } }>()
return c.json(provider)
})
.post('/providers/:id/models', zValidator('json', createModelRequestSchema), async (c) => {
const id = c.req.param('id')
const user = c.get('user')
const data = c.req.valid('json')
await authzService.hasPermission({
user,
permission: Permission.Admin,
type: ResourceType.Provider,
id,
})
const provider = await providerService.getById(id)
if (!provider) throw new Error('Provider not found')
const model = await providerService.createModel(provider.teamId, id, data)
await auditLogService.logAction({
action: AuditAction.provider_update,
teamId: provider.teamId,
userId: user?.id,
itemId: id,
})
return c.json(model, 201)
})
.put(
'/providers/:id/models/:modelDbId',
zValidator('json', updateModelRequestSchema),
async (c) => {
const id = c.req.param('id')
const modelDbId = c.req.param('modelDbId')
const user = c.get('user')
const data = c.req.valid('json')
await authzService.hasPermission({
user,
permission: Permission.Admin,
type: ResourceType.Provider,
id,
})
const provider = await providerService.getById(id)
if (!provider) throw new Error('Provider not found')
const model = await providerService.updateModel(provider.teamId, id, modelDbId, data)
await auditLogService.logAction({
action: AuditAction.provider_update,
teamId: provider.teamId,
userId: user?.id,
itemId: id,
})
return c.json(model)
},
)
.delete('/providers/:id/models/:modelDbId', async (c) => {
const id = c.req.param('id')
const modelDbId = c.req.param('modelDbId')
const user = c.get('user')
await authzService.hasPermission({
user,
permission: Permission.Admin,
type: ResourceType.Provider,
id,
})
const providerBefore = await providerService.getById(id)
if (!providerBefore) throw new Error('Provider not found')
await providerService.deleteModel(providerBefore.teamId, id, modelDbId)
await auditLogService.logAction({
action: AuditAction.provider_update,
teamId: providerBefore.teamId,
userId: user?.id,
itemId: id,
})
return c.json({ success: true })
})
.delete('/providers/:id', async (c) => {
const id = c.req.param('id')
const user = c.get('user')
+2 -2
View File
@@ -4,7 +4,7 @@ import { zValidator } from '@hono/zod-validator'
import { shareService } from '@shumai/core/src/share/share'
import { assetService } from '@shumai/core/src/asset/asset'
import { watermarkService } from '@shumai/core/src/watermark/watermark'
import { paginationParamsSchema } from '@shumai/dtos'
import { paginationParamsSchema, assetTypeFilterSchema } from '@shumai/dtos'
import { z } from 'zod'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { FieldInfo } from '@shumai/dtos'
@@ -117,7 +117,7 @@ export function handlePublicShareError(c: Context, err: unknown) {
}
const sharedChildrenRequestSchema = paginationParamsSchema.extend({
assetType: z.string().optional(),
assetType: assetTypeFilterSchema.optional().default('file'),
})
const route = app
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/core",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"main": "./src/index.ts",
+114 -13
View File
@@ -338,10 +338,89 @@ describe('AgentService', () => {
expect(entries[0].id).toBe(entry1.id)
expect(entries[1].id).toBe(entry2.id)
})
test('getSessionEntries dynamically attaches details.thread on custom_message entries with replies', async () => {
const db = prisma
const svc = new AgentService()
const { agent, team } = await setupTestData(db)
const project = await db.project.create({
data: { name: 'Test Project', teamId: team.id },
})
const asset = await db.asset.create({
data: {
name: 'video.mp4',
type: 'file',
status: 'uploaded',
projectId: project.id,
},
})
const user = await db.user.create({
data: { name: 'Alice', email: `alice-${Date.now()}@example.com` },
})
const rootComment = await db.assetComment.create({
data: {
id: 'comment-root-entries',
assetId: asset.id,
message: 'Top-level comment',
creatorId: user.id,
},
})
// Add a reply
await db.assetComment.create({
data: {
id: 'comment-reply-entries',
assetId: asset.id,
message: 'Reply to root',
creatorId: user.id,
replyToId: rootComment.id,
},
})
const session = await db.agentSession.create({
data: {
agentId: agent.id,
type: 'comment',
cwd: '/tmp',
},
})
const entry = await db.agentSessionEntry.create({
data: {
id: rootComment.id,
sessionId: session.id,
type: 'custom_message',
parentId: null,
data: {
customType: 'shumai_message',
content: 'Top-level comment',
display: true,
details: { user: { id: user.id, name: user.name } },
},
},
})
await db.agentSession.update({
where: { id: session.id },
data: { leafId: entry.id },
})
const entries = await svc.getSessionEntries({ sessionId: session.id })
expect(entries).toHaveLength(1)
expect(entries[0].id).toBe(rootComment.id)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const entryDetails = (entries[0].entry as any).details
expect(entryDetails?.thread).toEqual({
id: rootComment.id,
replyCount: 1,
})
})
})
describe('listSessions', () => {
test('lists sessions for team and excludes pending id', async () => {
test('lists sessions for team, includes chat sessions and leaf comment sessions, hides main comment sessions and excludes pending id', async () => {
const db = prisma
const svc = new AgentService()
const { team, agent } = await setupTestData(db)
@@ -353,27 +432,43 @@ describe('AgentService', () => {
},
})
const session1 = await db.agentSession.create({
// 1. Chat session (userCommentId is null) - should be included!
const chatSession = await db.agentSession.create({
data: {
name: 'Session One',
name: 'Chat Session',
agentId: agent.id,
userId: user.id,
type: 'chat',
cwd: '/tmp',
userCommentId: null,
},
})
const session2 = await db.agentSession.create({
// 2. Main comment session (userCommentId is null) - should be HIDDEN / EXCLUDED!
const mainCommentSession = await db.agentSession.create({
data: {
name: 'Session Two',
name: 'Main Comment Session',
agentId: agent.id,
userId: user.id,
type: 'comment',
cwd: '/tmp',
userCommentId: null,
},
})
// Pending session - should be excluded!
// 3. Leaf comment session (userCommentId is not null) - should be included!
const leafCommentSession = await db.agentSession.create({
data: {
name: 'Leaf Comment Session',
agentId: agent.id,
userId: user.id,
type: 'comment',
cwd: '/tmp',
userCommentId: 'comment-123',
},
})
// 4. Pending session - should be excluded!
await db.agentSession.create({
data: {
id: 'pending',
@@ -388,15 +483,21 @@ describe('AgentService', () => {
const res = await svc.listSessions(team.id, { first: 10 })
expect(res.data).toHaveLength(2)
expect(res.data.map((s) => s.id)).not.toContain('pending')
const sessionIds = res.data.map((s) => s.id)
expect(sessionIds).toContain(session1.id)
expect(sessionIds).toContain(session2.id)
expect(sessionIds).not.toContain('pending')
expect(sessionIds).not.toContain(mainCommentSession.id)
expect(sessionIds).toContain(chatSession.id)
expect(sessionIds).toContain(leafCommentSession.id)
const found = res.data.find((s) => s.id === session1.id)
expect(found?.name).toBe('Session One')
expect(found?.type).toBe('chat')
expect(found?.creator?.name).toBe('Regular User')
const foundChat = res.data.find((s) => s.id === chatSession.id)
expect(foundChat?.name).toBe('Chat Session')
expect(foundChat?.type).toBe('chat')
expect(foundChat?.creator?.name).toBe('Regular User')
const foundLeafComment = res.data.find((s) => s.id === leafCommentSession.id)
expect(foundLeafComment?.name).toBe('Leaf Comment Session')
expect(foundLeafComment?.type).toBe('comment')
expect(foundLeafComment?.creator?.name).toBe('Regular User')
})
})
+40 -1
View File
@@ -549,21 +549,53 @@ export class AgentService {
ORDER BY depth DESC;
`
const entryIds = rows.map((r) => r.id)
const threadReplyCountMap = new Map<string, number>()
if (entryIds.length > 0) {
const threadCounts = await this.prismaClient.assetComment.groupBy({
by: ['replyToId'],
where: {
replyToId: { in: entryIds },
message: { not: '__CHAT__' },
},
_count: {
id: true,
},
})
for (const tc of threadCounts) {
if (tc.replyToId) {
threadReplyCountMap.set(tc.replyToId, tc._count.id)
}
}
}
return rows.map((r) => {
const payload = (r.data as Record<string, unknown>) || {}
const count = threadReplyCountMap.get(r.id)
let details = payload.details as Record<string, unknown> | undefined
if (count !== undefined && count > 0 && r.type === 'custom_message') {
details = {
...details,
thread: {
id: r.id,
replyCount: count,
},
}
}
const entryObj = {
id: r.id,
type: r.type || 'message',
parentId: r.parent_id,
timestamp: r.created_at.toISOString(),
...payload,
...(details ? { details } : {}),
}
return {
id: r.id,
sessionId: r.session_id || params.sessionId,
type: r.type,
parentId: r.parent_id,
data: r.data,
data: details !== payload.details ? { ...payload, details } : r.data,
createdAt: r.created_at,
entry: entryObj,
}
@@ -579,6 +611,13 @@ export class AgentService {
agent: {
teamId,
},
OR: [
{ type: { not: 'comment' } },
{
type: 'comment',
userCommentId: { not: null },
},
],
}
return await paginateQuery(
+462
View File
@@ -5,6 +5,8 @@ import { HTTPException } from 'hono/http-exception'
import { AssetType, AssetStatus, Prisma } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { uploadService } from '@shumai/core/src/upload/upload'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { AssetService } from './asset'
vi.mock('@shumai/core/src/s3/s3', () => ({
@@ -568,6 +570,62 @@ describe('AssetService', () => {
})
})
it('rejects reparenting a version_stack onto a file', async () => {
const { user, assets } = await setupBasicAssets()
await expect(
assetService.reparentAssets({
assetIds: [assets.stackB.id],
newParentId: assets.fileA1.id,
creatorId: user.id,
}),
).rejects.toThrow('Source asset must be a file to create a version stack')
})
it('rejects reparenting a version_stack onto another version_stack', async () => {
const { user, assets, project } = await setupBasicAssets()
const stack2 = await prisma.asset.create({
data: {
name: 'stack2',
type: AssetType.version_stack,
projectId: project.id,
parentId: assets.folderA.id,
creatorId: user.id,
fileCount: 2,
sizeByte: 100,
status: 'uploaded',
},
})
await expect(
assetService.reparentAssets({
assetIds: [assets.stackB.id],
newParentId: stack2.id,
creatorId: user.id,
}),
).rejects.toThrow('Cannot move version_stack asset into a version stack')
})
it('rejects reparenting a folder onto a version_stack', async () => {
const { user, assets } = await setupBasicAssets()
await expect(
assetService.reparentAssets({
assetIds: [assets.folderA.id],
newParentId: assets.stackB.id,
creatorId: user.id,
}),
).rejects.toThrow('Cannot move folder asset into a version stack')
})
it('rejects reparenting multiple files onto a version_stack at once', async () => {
const { user, assets } = await setupBasicAssets()
await expect(
assetService.reparentAssets({
assetIds: [assets.fileA1.id, assets.fileA2.id],
newParentId: assets.stackB.id,
creatorId: user.id,
}),
).rejects.toThrow('Can only move one file at a time into a version stack')
})
describe('fileCount verification', () => {
it('createAsset should increment parent fileCount', async () => {
const { user, assets, project } = await setupBasicAssets()
@@ -3704,4 +3762,408 @@ describe('AssetService — natural sort by name', () => {
expect(purgedSymlink).toBeNull()
})
})
describe('createFile', () => {
it('creates a file, updates ancestor sizes, persists metadata, and triggers workflows', async () => {
const user = await prisma.user.create({
data: { name: 'FileUser', email: `fileuser-${Date.now()}@example.com` },
})
const team = await prisma.team.create({ data: { name: 'FileTeam' } })
const project = await prisma.project.create({
data: { name: 'FileProject', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'RootFolder',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
const promptField = await metadataService.createProjectField(project.id, {
description: 'Prompt field',
config: {
name: 'Prompt',
type: 'text',
autofillSource: 'CREATION_CONTEXT',
},
})
const triggerSpy = vi
.spyOn(uploadService, 'triggerPostUploadWorkflows')
.mockResolvedValue(undefined)
const fileInfo = await assetService.createFile({
parentId: rootFolder.id,
name: 'document.txt',
key: 'files/test/doc.txt',
sizeByte: 1024,
contentType: 'text/plain',
creatorId: user.id,
metadata: { [promptField.key]: 'Project overview document' },
})
expect(fileInfo.id).toBeDefined()
expect(fileInfo.name).toBe('document.txt')
expect(fileInfo.type).toBe(AssetType.file)
expect(fileInfo.sizeByte).toBe(1024)
const folderAfter = await prisma.asset.findUnique({ where: { id: rootFolder.id } })
expect(folderAfter?.sizeByte).toBe(1024n)
expect(folderAfter?.fileCount).toBe(1)
const metaVal = await prisma.assetMetadataValue.findFirst({
where: { assetId: fileInfo.id, fieldKey: promptField.key },
})
expect(metaVal?.stringValue).toBe('Project overview document')
expect(triggerSpy).toHaveBeenCalledWith(expect.anything(), fileInfo.id, team.id, project.id)
})
it('rejects if metadata contains invalid keys', async () => {
const user = await prisma.user.create({
data: { name: 'FileUser2', email: `fileuser2-${Date.now()}@example.com` },
})
const team = await prisma.team.create({ data: { name: 'FileTeam2' } })
const project = await prisma.project.create({
data: { name: 'FileProject2', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'RootFolder2',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
await expect(
assetService.createFile({
parentId: rootFolder.id,
name: 'invalid.txt',
key: 'files/test/inv.txt',
sizeByte: 100,
contentType: 'text/plain',
creatorId: user.id,
metadata: { unknownField: 'val' },
}),
).rejects.toThrow(/metadata contains keys that are not valid in this project/)
})
it('rolls back completely if post-upload workflow fails in transaction', async () => {
const user = await prisma.user.create({
data: { name: 'FileRollbackUser', email: `filerollback-${Date.now()}@example.com` },
})
const team = await prisma.team.create({ data: { name: 'FileRollbackTeam' } })
const project = await prisma.project.create({
data: { name: 'FileRollbackProject', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'RootFolder',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.uploaded,
sizeByte: 0n,
fileCount: 0,
},
})
vi.spyOn(uploadService, 'triggerPostUploadWorkflows').mockRejectedValueOnce(
new Error('Simulated workflow failure'),
)
await expect(
assetService.createFile({
parentId: rootFolder.id,
name: 'should_rollback.txt',
key: 'files/test/should_rollback.txt',
sizeByte: 1024,
contentType: 'text/plain',
creatorId: user.id,
}),
).rejects.toThrow('Simulated workflow failure')
// Assert no asset was created
const created = await prisma.asset.findFirst({
where: { name: 'should_rollback.txt', projectId: project.id },
})
expect(created).toBeNull()
// Assert parent folder counters remained untouched
const folderAfter = await prisma.asset.findUnique({ where: { id: rootFolder.id } })
expect(folderAfter?.fileCount).toBe(0)
expect(folderAfter?.sizeByte).toBe(0n)
})
})
describe('createVersion', () => {
it('creates a version stack when target is a regular file', async () => {
const user = await prisma.user.create({
data: { name: 'VersionUser', email: `veruser-${Date.now()}@example.com` },
})
const team = await prisma.team.create({ data: { name: 'VersionTeam' } })
const project = await prisma.project.create({
data: { name: 'VersionProject', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'RootFolder',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
const v1 = await prisma.asset.create({
data: {
name: 'image.png',
type: AssetType.file,
projectId: project.id,
parentId: rootFolder.id,
status: AssetStatus.uploaded,
sizeByte: 2000n,
},
})
const triggerSpy = vi
.spyOn(uploadService, 'triggerPostUploadWorkflows')
.mockResolvedValue(undefined)
const v2Info = await assetService.createVersion({
parentId: v1.id,
name: 'image.png',
key: 'files/test/v2.png',
sizeByte: 3000,
contentType: 'image/png',
creatorId: user.id,
})
expect(v2Info.id).toBeDefined()
const v2Record = await prisma.asset.findUnique({
where: { id: v2Info.id },
include: { parent: true },
})
expect(v2Record?.parent?.type).toBe(AssetType.version_stack)
expect(v2Record?.parent?.parentId).toBe(rootFolder.id)
const v1Record = await prisma.asset.findUnique({ where: { id: v1.id } })
expect(v1Record?.parentId).toBe(v2Record?.parentId)
const stackRecord = await prisma.asset.findUnique({ where: { id: v2Record!.parentId! } })
expect(stackRecord?.fileCount).toBe(2)
expect(stackRecord?.sizeByte).toBe(5000n)
expect(triggerSpy).toHaveBeenCalledWith(expect.anything(), v2Info.id, team.id, project.id)
})
it('appends a new version when target is in an existing version stack', async () => {
const user = await prisma.user.create({
data: { name: 'VersionUser2', email: `veruser2-${Date.now()}@example.com` },
})
const team = await prisma.team.create({ data: { name: 'VersionTeam2' } })
const project = await prisma.project.create({
data: { name: 'VersionProject2', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'RootFolder',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
const stack = await prisma.asset.create({
data: {
name: 'image.png',
type: AssetType.version_stack,
projectId: project.id,
parentId: rootFolder.id,
status: AssetStatus.uploaded,
sizeByte: 2000n,
fileCount: 1,
},
})
const v1 = await prisma.asset.create({
data: {
name: 'image.png',
type: AssetType.file,
projectId: project.id,
parentId: stack.id,
status: AssetStatus.uploaded,
sortIndex: 'a0',
sizeByte: 2000n,
},
})
const triggerSpy = vi
.spyOn(uploadService, 'triggerPostUploadWorkflows')
.mockResolvedValue(undefined)
const v2Info = await assetService.createVersion({
parentId: v1.id,
name: 'image_v2.png',
key: 'files/test/v2.png',
sizeByte: 3000,
contentType: 'image/png',
creatorId: user.id,
})
expect(v2Info.id).toBeDefined()
const v2Record = await prisma.asset.findUnique({ where: { id: v2Info.id } })
expect(v2Record?.parentId).toBe(stack.id)
const stackAfter = await prisma.asset.findUnique({ where: { id: stack.id } })
expect(stackAfter?.fileCount).toBe(2)
expect(stackAfter?.sizeByte).toBe(5000n)
expect(triggerSpy).toHaveBeenCalledWith(expect.anything(), v2Info.id, team.id, project.id)
})
it('rolls back completely if workflow triggering fails in createVersion', async () => {
const user = await prisma.user.create({
data: { name: 'VersionRollbackUser', email: `verrollback-${Date.now()}@example.com` },
})
const team = await prisma.team.create({ data: { name: 'VersionRollbackTeam' } })
const project = await prisma.project.create({
data: { name: 'VersionRollbackProject', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'RootFolder',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.uploaded,
sizeByte: 2000n,
fileCount: 1,
},
})
const v1 = await prisma.asset.create({
data: {
name: 'image.png',
type: AssetType.file,
projectId: project.id,
parentId: rootFolder.id,
status: AssetStatus.uploaded,
sizeByte: 2000n,
},
})
vi.spyOn(uploadService, 'triggerPostUploadWorkflows').mockRejectedValueOnce(
new Error('Simulated version workflow failure'),
)
await expect(
assetService.createVersion({
parentId: v1.id,
name: 'image_v2_fail.png',
key: 'files/test/v2_fail.png',
sizeByte: 3000,
contentType: 'image/png',
creatorId: user.id,
}),
).rejects.toThrow('Simulated version workflow failure')
// Check that new version was rolled back
const createdV2 = await prisma.asset.findFirst({
where: { name: 'image_v2_fail.png', projectId: project.id },
})
expect(createdV2).toBeNull()
// Check that v1 remains under rootFolder (no orphaned version stack created)
const v1After = await prisma.asset.findUnique({ where: { id: v1.id } })
expect(v1After?.parentId).toBe(rootFolder.id)
// Check root folder counters remained unchanged
const rootFolderAfter = await prisma.asset.findUnique({ where: { id: rootFolder.id } })
expect(rootFolderAfter?.fileCount).toBe(1)
expect(rootFolderAfter?.sizeByte).toBe(2000n)
})
})
describe('listChildren type filtering', () => {
it('returns files, folders, and version stacks when assetType is all or omitted', async () => {
const team = await prisma.team.create({ data: { name: 'ListTypeTeam' } })
const project = await prisma.project.create({
data: { name: 'ListTypeProject', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'RootFolder',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
const subFolder = await prisma.asset.create({
data: {
name: 'subfolder',
type: AssetType.folder,
projectId: project.id,
parentId: rootFolder.id,
status: AssetStatus.uploaded,
},
})
const file = await prisma.asset.create({
data: {
name: 'file.txt',
type: AssetType.file,
projectId: project.id,
parentId: rootFolder.id,
status: AssetStatus.uploaded,
},
})
const stack = await prisma.asset.create({
data: {
name: 'stack.png',
type: AssetType.version_stack,
projectId: project.id,
parentId: rootFolder.id,
status: AssetStatus.uploaded,
},
})
// Query with assetType: 'all'
const allRes = await assetService.listChildren({
assetId: rootFolder.id,
assetType: 'all',
})
const allIds = allRes.data.map((a) => a.id)
expect(allIds).toContain(subFolder.id)
expect(allIds).toContain(file.id)
expect(allIds).toContain(stack.id)
expect(allRes.data.length).toBe(3)
// Query with assetType: 'file'
const fileRes = await assetService.listChildren({
assetId: rootFolder.id,
assetType: 'file',
})
const fileIds = fileRes.data.map((a) => a.id)
expect(fileIds).toContain(file.id)
expect(fileIds).toContain(stack.id)
expect(fileIds).not.toContain(subFolder.id)
expect(fileRes.data.length).toBe(2)
// Query with assetType: 'folder'
const folderRes = await assetService.listChildren({
assetId: rootFolder.id,
assetType: 'folder',
})
const folderIds = folderRes.data.map((a) => a.id)
expect(folderIds).toContain(subFolder.id)
expect(folderIds).not.toContain(file.id)
expect(folderIds).not.toContain(stack.id)
expect(folderRes.data.length).toBe(1)
})
})
})
+194 -8
View File
@@ -29,6 +29,8 @@ import { generateKeyBetween } from 'jittered-fractional-indexing'
import { getAvatarUrl } from '@shumai/core/src/user/avatar'
import { getAgentRequiredLevel, getRoleLevel } from '@shumai/core/src/agent/permissions'
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { uploadService } from '@shumai/core/src/upload/upload'
export const assetInclude = {
creator: true,
@@ -209,6 +211,12 @@ export class AssetService {
return this.reparentFileToFile(tx, newParent, req.assetIds, req.creatorId)
}
if (newParent.type === AssetType.version_stack) {
if (req.assetIds.length !== 1) {
throw new Error('Can only move one file at a time into a version stack')
}
}
const assetsToMove = await tx.asset.findMany({
where: { id: { in: req.assetIds } },
include: { project: { include: { team: true } } },
@@ -220,6 +228,14 @@ export class AssetService {
if (assetsToMove.length === 0) return
if (newParent.type === AssetType.version_stack) {
for (const a of assetsToMove) {
if (a.type !== AssetType.file) {
throw new Error(`Cannot move ${a.type} asset into a version stack`)
}
}
}
let totalSize = 0
const oldParentId = assetsToMove[0].parentId
if (!oldParentId) {
@@ -522,6 +538,10 @@ export class AssetService {
assetIds: string[],
creatorId?: string,
) {
if (destFile.type !== AssetType.file) {
throw new Error('Destination asset must be a file')
}
if (assetIds.length !== 1) {
throw new Error('Can only reparent one file to another file')
}
@@ -537,6 +557,10 @@ export class AssetService {
if (!sourceAsset) throw new Error('Source asset not found')
if (sourceAsset.type !== AssetType.file) {
throw new Error('Source asset must be a file to create a version stack')
}
if (!sourceAsset.project || !sourceAsset.project.team) {
throw new Error(`Asset ${sourceAsset.id} is not associated with a team`)
}
@@ -614,9 +638,13 @@ export class AssetService {
}
async listChildren(req: ListChildrenRequest): Promise<PaginatedData<AssetInfo[]>> {
const typesToQuery: AssetType[] = [req.assetType as AssetType]
if (req.assetType === AssetType.file) {
typesToQuery.push(AssetType.version_stack)
let typesToQuery: AssetType[]
if (req.assetType === 'file') {
typesToQuery = [AssetType.file, AssetType.version_stack]
} else if (req.assetType === 'folder') {
typesToQuery = [AssetType.folder]
} else {
typesToQuery = [AssetType.file, AssetType.folder, AssetType.version_stack]
}
let where: Prisma.AssetWhereInput = {}
@@ -721,12 +749,17 @@ export class AssetService {
return { data: infos, pageInfo }
}
async createAsset(req: CreateAssetRequest): Promise<AssetInfo> {
async createAsset(
req: CreateAssetRequest,
txClient?: Prisma.TransactionClient,
): Promise<AssetInfo> {
const db = txClient ?? this.prismaClient
let projectId = req.projectId
let parent: Prisma.AssetGetPayload<{ include: { project: true } }> | null = null
if (req.parentId) {
parent = await this.prismaClient.asset.findUnique({
parent = await db.asset.findUnique({
where: { id: req.parentId },
include: { project: true },
})
@@ -738,7 +771,7 @@ export class AssetService {
let sortIndex: string | null = null
if (parent) {
const firstFile = await this.prismaClient.asset.findFirst({
const firstFile = await db.asset.findFirst({
where: { parentId: parent.id },
orderBy: { sortIndex: 'asc' },
})
@@ -766,7 +799,7 @@ export class AssetService {
if (req.contentType) data.mediaType = req.contentType
if (req.creatorId) data.creator = { connect: { id: req.creatorId } }
const asset = await this.prismaClient.$transaction(async (tx) => {
const executeInTx = async (tx: Prisma.TransactionClient) => {
const createdAsset = await tx.asset.create({
data,
include: {
@@ -803,12 +836,165 @@ export class AssetService {
}
return createdAsset
})
}
const asset = txClient
? await executeInTx(txClient)
: await this.prismaClient.$transaction(executeInTx)
const infos = await this.toAssetInfos([asset])
return infos[0]
}
async createFile(params: {
parentId: string
name: string
key: string
sizeByte: number
contentType: string
creatorId: string
metadata?: Record<string, unknown>
}): Promise<AssetInfo> {
const { parentId, name, key, sizeByte, contentType, creatorId, metadata } = params
const parentAsset = await this.prismaClient.asset.findUnique({
where: { id: parentId },
include: { project: true },
})
if (!parentAsset || !parentAsset.projectId || !parentAsset.project?.teamId) {
throw new Error(`Parent folder ${parentId} is invalid or has no project associated`)
}
if (metadata) {
await metadataService.validateCreationContextMetadata(
creatorId,
parentAsset.projectId,
metadata,
)
}
let newFileId = ''
await this.prismaClient.$transaction(async (tx) => {
const newFile = await this.createAsset(
{
name,
type: 'file',
parentId,
key,
sizeByte,
contentType,
creatorId,
},
tx,
)
newFileId = newFile.id
await this.updateAncestorsSize(tx, parentId, sizeByte)
if (metadata) {
const metadataUpdates = Object.entries(metadata)
.filter(([, value]) => value !== null)
.map(([k, v]) => ({ key: k, value: v }))
if (metadataUpdates.length > 0) {
await metadataService.updateAssetMetadataInTx(tx, newFile.id, metadataUpdates)
}
}
await uploadService.triggerPostUploadWorkflows(
tx,
newFile.id,
parentAsset.project!.teamId,
parentAsset.projectId!,
)
})
return this.getAsset({ assetId: newFileId })
}
async createVersion(params: {
parentId: string
name: string
key: string
sizeByte: number
contentType: string
creatorId: string
metadata?: Record<string, unknown>
}): Promise<AssetInfo> {
const { parentId, name, key, sizeByte, contentType, creatorId, metadata } = params
const targetAsset = await this.prismaClient.asset.findUnique({
where: { id: parentId },
include: { project: true, parent: true },
})
if (!targetAsset || !targetAsset.projectId || !targetAsset.project?.teamId) {
throw new Error(`Target file ${parentId} is invalid or has no project associated`)
}
if (metadata) {
await metadataService.validateCreationContextMetadata(
creatorId,
targetAsset.projectId,
metadata,
)
}
let targetFolderId: string
let reparentTargetId: string
if (targetAsset.type === AssetType.version_stack) {
targetFolderId = targetAsset.parentId!
reparentTargetId = targetAsset.id
} else if (targetAsset.parent?.type === AssetType.version_stack) {
targetFolderId = targetAsset.parent.parentId!
reparentTargetId = targetAsset.parentId!
} else {
targetFolderId = targetAsset.parentId!
reparentTargetId = targetAsset.id
}
let newFileId = ''
await this.prismaClient.$transaction(async (tx) => {
const newFile = await this.createAsset(
{
name,
type: 'file',
parentId: targetFolderId,
key,
sizeByte,
contentType,
creatorId,
},
tx,
)
newFileId = newFile.id
await this.reparentAssets(
{
assetIds: [newFile.id],
newParentId: reparentTargetId,
creatorId,
},
tx,
)
if (metadata) {
const metadataUpdates = Object.entries(metadata)
.filter(([, value]) => value !== null)
.map(([k, v]) => ({ key: k, value: v }))
if (metadataUpdates.length > 0) {
await metadataService.updateAssetMetadataInTx(tx, newFile.id, metadataUpdates)
}
}
await uploadService.triggerPostUploadWorkflows(
tx,
newFile.id,
targetAsset.project!.teamId,
targetAsset.projectId!,
)
})
return this.getAsset({ assetId: newFileId })
}
async findAsset(parentId: string, name: string): Promise<AssetInfo | null> {
const asset = await this.prismaClient.asset.findFirst({
where: { parentId, name },
+112
View File
@@ -613,6 +613,53 @@ describe('ChatService', () => {
expect(mapEntryToMessage(leafRecord)).toBeNull()
})
it('should map shumai_message custom_message entries with clean text and structured details', () => {
const entryRecord = {
id: 'entry-shumai-msg',
type: 'custom_message',
parentId: null,
createdAt: new Date('2026-07-07T00:05:00.000Z'),
data: {
customType: 'shumai_message',
content: 'Please adjust color grading',
display: true,
details: {
user: { id: 'u1', name: 'Alice', role: 'editor' },
currentAsset: { id: 'a1', name: 'video.mp4', type: 'file' },
position: { type: 'time', seconds: 12.5 },
},
},
} as unknown as Parameters<typeof mapEntryToMessage>[0]
const msg = mapEntryToMessage(entryRecord)
expect(msg).toBeDefined()
expect(msg?.id).toBe('entry-shumai-msg')
expect(msg?.role).toBe('custom')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((msg as any)?.customType).toBe('shumai_message')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((msg as any)?.content).toBe('Please adjust color grading')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((msg as any)?.details?.user?.name).toBe('Alice')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((msg as any)?.details?.position?.seconds).toBe(12.5)
})
it('should skip legacy context custom_message in mapEntryToMessage', () => {
const entryRecord = {
id: 'entry-legacy-context',
type: 'custom_message',
parentId: null,
createdAt: new Date('2026-07-07T00:05:00.000Z'),
data: {
customType: 'context',
content: 'System context prompt...',
},
} as unknown as Parameters<typeof mapEntryToMessage>[0]
expect(mapEntryToMessage(entryRecord)).toBeNull()
})
describe('getNewSessionMessages', () => {
it('returns new messages and lastEntryId', async () => {
const { user, team, project } = await setupBasicData()
@@ -900,5 +947,70 @@ describe('ChatService', () => {
}),
).rejects.toThrow(/Insufficient role/)
})
it('safely maps entries without message object or with customType shumai_message', () => {
// 1. Entry with type: 'message' but customType: 'shumai_message'
const legacyEntry = {
id: 'legacy-1',
sessionId: 'sess-1',
assetId: null,
parentId: null,
type: 'message',
data: {
customType: 'shumai_message',
content: 'Hello legacy',
display: true,
details: { user: { id: 'u1', name: 'User 1' } },
},
createdAt: new Date(),
}
const msg1 = mapEntryToMessage(legacyEntry)
expect(msg1).toBeDefined()
expect(msg1?.role).toBe('custom')
if (msg1 && msg1.role === 'custom') {
expect(msg1.customType).toBe('shumai_message')
expect(msg1.content).toBe('Hello legacy')
expect(msg1.details).toEqual({ user: { id: 'u1', name: 'User 1' } })
}
// 2. Entry with type: 'message' but empty data / no message
const emptyEntry = {
id: 'empty-1',
sessionId: 'sess-1',
assetId: null,
parentId: null,
type: 'message',
data: {},
createdAt: new Date(),
}
const msg2 = mapEntryToMessage(emptyEntry)
expect(msg2).toBeNull()
})
it('should forward annotations and second to workflow task payload', async () => {
const { user, team, project } = await setupBasicData()
const { sessionId, taskId } = await chatService.startOrContinueChat(user, team.id, {
agentId: 'test-agent-id',
textPrompt: 'Check out this area',
projectId: project.id,
annotations: [{ type: 'rectangle', x: 10, y: 20, width: 30, height: 40 }],
second: 8.5,
})
expect(sessionId).toBeDefined()
expect(taskId).toBeDefined()
const task = await prisma.workflowTask.findUnique({
where: { id: taskId },
})
expect(task).toBeDefined()
expect(task?.payload?.agent?.second).toBe(8.5)
expect(task?.payload?.agent?.annotations).toEqual([
{ type: 'rectangle', x: 10, y: 20, width: 30, height: 40 },
])
})
})
})
+34 -6
View File
@@ -30,11 +30,31 @@ export function buildSessionMessages(pathEntries: PathEntry[]): 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)
if ((entry as unknown as { customType?: string }).customType === 'shumai_message') {
const customEntry = entry as unknown as {
customType: string
content: string
display?: boolean
details?: Record<string, unknown>
}
messages.push({
id: entry.id,
role: 'custom',
customType: customEntry.customType,
content: customEntry.content,
display: customEntry.display,
details: customEntry.details,
timestamp: timestampMs,
} as unknown as ChatMessage)
return
}
if (entry.message) {
messages.push({
...entry.message,
id: entry.id,
timestamp: entry.message.timestamp || timestampMs,
} as unknown as ChatMessage)
}
} else if (entry.type === 'custom_message') {
if (entry.customType === 'context') return
messages.push({
@@ -117,9 +137,13 @@ export function mapEntryToMessage(
entryRecord: Prisma.AgentSessionEntryGetPayload<Record<string, never>>,
): ChatMessage | null {
const payload = (entryRecord.data as Record<string, unknown>) || {}
let type = (entryRecord.type || 'message') as SessionTreeEntry['type']
if (type === 'message' && payload.customType === 'shumai_message') {
type = 'custom_message'
}
const entryObj = {
id: entryRecord.id,
type: (entryRecord.type || 'message') as SessionTreeEntry['type'],
type,
parentId: entryRecord.parentId,
timestamp: entryRecord.createdAt.toISOString(),
...payload,
@@ -175,6 +199,8 @@ export class ChatService {
sessionId: passedSessionId,
contextAssetId,
projectId: passedProjectId,
second,
annotations,
} = req
// 1. Permissions verification
@@ -377,6 +403,8 @@ export class ChatService {
userId: user.id,
isNewChat: !passedSessionId,
hasAssetChanged,
second: second ?? undefined,
annotations: annotations ?? undefined,
},
},
},
@@ -549,4 +549,65 @@ describe('MetadataService', () => {
]),
).rejects.toThrow('expected string or {newOption}')
})
describe('validateCreationContextMetadata', () => {
it('passes for allowed CREATION_CONTEXT fields and throws for invalid fields', async () => {
const team = await prisma.team.create({ data: { name: 'test-team-meta' } })
const project = await prisma.project.create({
data: { name: 'test-project-meta', teamId: team.id },
})
const validField = await prisma.metadataField.create({
data: {
key: 'prompt_field',
scope: 'PROJECT',
project: { connect: { id: project.id } },
config: {
name: 'Prompt',
type: 'text',
autofillSource: 'CREATION_CONTEXT',
},
},
})
await prisma.metadataField.create({
data: {
key: 'other_field',
scope: 'PROJECT',
project: { connect: { id: project.id } },
config: {
name: 'Other',
type: 'text',
autofillSource: 'NONE',
},
},
})
// Valid: empty metadata or null/undefined
await expect(
metadataService.validateCreationContextMetadata(null, project.id, null),
).resolves.toBeUndefined()
await expect(
metadataService.validateCreationContextMetadata(null, project.id, {}),
).resolves.toBeUndefined()
// Valid: allowed key
await expect(
metadataService.validateCreationContextMetadata(null, project.id, {
[validField.key]: 'valid value',
}),
).resolves.toBeUndefined()
// Invalid: non-CREATION_CONTEXT key or non-existent key
await expect(
metadataService.validateCreationContextMetadata(null, project.id, {
otherField: 'value',
}),
).rejects.toThrow(/metadata contains keys that are not valid in this project/)
await expect(
metadataService.validateCreationContextMetadata(null, project.id, {
randomFakeKey: 'value',
}),
).rejects.toThrow(/metadata contains keys that are not valid in this project/)
})
})
})
+23
View File
@@ -215,6 +215,29 @@ export class MetadataService {
return res
}
async validateCreationContextMetadata(
userId: string | null,
projectId: string,
metadata?: Record<string, unknown> | null,
): Promise<void> {
if (!metadata || Object.keys(metadata).length === 0) return
const fields = await this.listProjectFields(userId, projectId)
const allowedKeys = new Set(
fields
.filter((f) => f.field.config?.autofillSource === 'CREATION_CONTEXT')
.map((f) => f.field.key),
)
const invalidKeys = Object.keys(metadata).filter((key) => !allowedKeys.has(key))
if (invalidKeys.length > 0) {
throw new Error(
`metadata contains keys that are not valid in this project: ${invalidKeys.join(', ')}. ` +
"The valid metadata fields are fixed to the current conversation's project. " +
'If the target folder or file belongs to a different project, call the tool WITHOUT the metadata parameter.',
)
}
}
private async getProjectFieldsOrder(
userId: string,
projectId: string,
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,42 @@
import type { ThinkingLevel, ThinkingLevelMap } from '@earendil-works/pi-ai'
export type ModelsDevReasoningOption =
| { type: 'toggle' }
| {
type: 'effort'
values: Array<
'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | 'default' | null
>
}
| { type: 'budget_tokens'; min?: number; max?: number }
const THINKING_LEVELS: readonly ThinkingLevel[] = [
'minimal',
'low',
'medium',
'high',
'xhigh',
'max',
]
/**
* Converts models.dev verified effort values into Pi's selectable thinking levels.
* Values without a Pi equivalent (`default` and JSON `null`) are intentionally
* omitted.
*/
export function getEffortThinkingLevelMap(
options: readonly ModelsDevReasoningOption[],
): ThinkingLevelMap | undefined {
const effortValues = options.flatMap((option) => (option.type === 'effort' ? option.values : []))
if (effortValues.length === 0) return undefined
const supported = new Set(effortValues)
if (!THINKING_LEVELS.some((level) => supported.has(level)) && !supported.has('none'))
return undefined
const map: ThinkingLevelMap = { off: supported.has('none') ? 'none' : null }
for (const level of THINKING_LEVELS) {
map[level] = supported.has(level) ? level : null
}
return map
}
+335 -1
View File
@@ -1,8 +1,10 @@
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { teamService } from '@shumai/core/src/team/team'
import { agentService } from '@shumai/core/src/agent/agent'
import { providerService } from './provider'
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import * as generateModelsModule from './generator/generate-models'
import { ProviderConfigSerializable, providerModelSchema } from '@shumai/dtos'
import { getBuiltinProvidersMap } from '@shumai/core/src/provider/builtin'
import { z } from 'zod'
@@ -191,4 +193,336 @@ describe('ProviderService', () => {
const updatedAgent = updatedAgents.find((a) => a.id === agent!.id)
expect(updatedAgent?.modelId).toBe(initialModelId)
})
it('should support granular model CRUD operations and order by id desc', async () => {
const team = await teamService.ensureDefaultTeam()
const provider = await providerService.create(
team.id,
'test-granular-provider',
{ api: 'openai-completions', apiKey: 'key1' },
[],
)
// 1. createModel
const createdModel1 = await providerService.createModel(team.id, provider.id, {
modelId: 'custom-model-1',
name: 'Custom Model 1',
config: {
reasoning: true,
input: ['text', 'image'],
contextWindow: 200000,
maxTokens: 8192,
cost: { input: 1.5, output: 5, cacheRead: 0.1, cacheWrite: 0.5 },
},
})
expect(createdModel1.modelId).toBe('custom-model-1')
expect(createdModel1.name).toBe('Custom Model 1')
expect((createdModel1.config as { reasoning: boolean }).reasoning).toBe(true)
// Check duplicate modelId error
await expect(
providerService.createModel(team.id, provider.id, {
modelId: 'custom-model-1',
name: 'Duplicate',
config: {
reasoning: false,
input: ['text'],
contextWindow: 100000,
maxTokens: 4000,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
}),
).rejects.toThrow('already exists')
// 2. createModel 2
const createdModel2 = await providerService.createModel(team.id, provider.id, {
modelId: 'custom-model-2',
name: 'Custom Model 2',
config: {
reasoning: false,
input: ['text'],
contextWindow: 100000,
maxTokens: 4000,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
})
// 3. listModelsByProvider should return newest first (id desc)
const models = await providerService.listModelsByProvider(team.id, provider.id)
expect(models).toHaveLength(2)
expect(models[0].id).toBe(createdModel2.id)
expect(models[1].id).toBe(createdModel1.id)
// 4. updateModel
const updatedModel = await providerService.updateModel(team.id, provider.id, createdModel1.id, {
name: 'Updated Model 1',
config: {
reasoning: false,
input: ['text'],
contextWindow: 64000,
maxTokens: 2048,
cost: { input: 2, output: 8, cacheRead: 0.2, cacheWrite: 0.8 },
},
})
expect(updatedModel.name).toBe('Updated Model 1')
expect((updatedModel.config as { reasoning: boolean }).reasoning).toBe(false)
expect((updatedModel.config as { cost: { input: number } }).cost.input).toBe(2)
// 5. update provider config and name without touching models
const updatedProvider = await providerService.update(
team.id,
provider.id,
{ api: 'openai-completions', apiKey: 'new-key' },
undefined,
'renamed-provider',
)
expect(updatedProvider.name).toBe('renamed-provider')
expect(updatedProvider.config.apiKey).toBe('new-key')
const modelsAfterProviderUpdate = await providerService.listModelsByProvider(
team.id,
provider.id,
)
expect(modelsAfterProviderUpdate).toHaveLength(2)
// 6. deleteModel
await providerService.deleteModel(team.id, provider.id, createdModel2.id)
const modelsAfterDelete = await providerService.listModelsByProvider(team.id, provider.id)
expect(modelsAfterDelete).toHaveLength(1)
expect(modelsAfterDelete[0].id).toBe(createdModel1.id)
})
it('should propagate provider API protocol change to existing models', async () => {
const team = await teamService.ensureDefaultTeam()
// 1. Create provider with openai-responses
const provider = await providerService.create(
team.id,
'test-protocol-provider',
{ api: 'openai-responses', apiKey: 'key-1' },
[
{
modelId: 'm1',
name: 'Model 1',
config: {
api: 'openai-responses',
reasoning: false,
input: ['text'],
contextWindow: 128000,
maxTokens: 4096,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
},
{
modelId: 'm2',
name: 'Model 2',
config: {
api: 'openai-responses',
reasoning: true,
input: ['text'],
contextWindow: 64000,
maxTokens: 2048,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
},
],
)
const initialModels = await providerService.listModelsByProvider(team.id, provider.id)
expect(initialModels).toHaveLength(2)
expect((initialModels[0].config as { api: string }).api).toBe('openai-responses')
expect((initialModels[1].config as { api: string }).api).toBe('openai-responses')
// 2. Update provider global API protocol to anthropic-messages without passing models
const updatedProvider = await providerService.update(
team.id,
provider.id,
{ api: 'anthropic-messages', apiKey: 'key-2' },
undefined,
'test-protocol-provider',
)
expect(updatedProvider.config.api).toBe('anthropic-messages')
// 3. Verify that all existing models under this provider were updated
const modelsAfterUpdate = await providerService.listModelsByProvider(team.id, provider.id)
expect(modelsAfterUpdate).toHaveLength(2)
expect((modelsAfterUpdate[0].config as { api: string }).api).toBe('anthropic-messages')
expect((modelsAfterUpdate[1].config as { api: string }).api).toBe('anthropic-messages')
})
it(
'should check for updates and correctly segregate new vs existing providers',
{ timeout: 30000 },
async () => {
// 1. Check updates with empty team: all discovered providers are new
const emptyTeam = await prisma.team.create({
data: { name: 'Empty Provider Team' },
})
const initialCheck = await providerService.checkUpdates(emptyTeam.id)
expect(initialCheck.totalNewProviders).toBeGreaterThan(0)
expect(initialCheck.totalNewModels).toBeGreaterThan(0)
expect(initialCheck.providers.every((p) => p.isNewProvider)).toBe(true)
// 2. With default team (which has builtin providers initialized)
const teamWithBuiltins = await teamService.ensureDefaultTeam()
// 3. Check updates after init: existing providers should have isNewProvider: false
const afterInitCheck = await providerService.checkUpdates(teamWithBuiltins.id)
const existing = afterInitCheck.providers.find((p) => !p.isNewProvider)
if (existing) {
expect(existing.isNewProvider).toBe(false)
const dbModels = await providerService.listModelsByProvider(
teamWithBuiltins.id,
(await providerService.listByTeam(teamWithBuiltins.id)).find(
(p) => p.name === existing.name,
)!.id,
)
const dbModelIds = new Set(dbModels.map((m) => m.modelId))
for (const m of existing.models) {
expect(dbModelIds.has(m.modelId)).toBe(false)
}
}
},
)
it('should throw an error when upstream catalog fetch fails in checkUpdates', async () => {
const team = await teamService.ensureDefaultTeam()
const spy = vi
.spyOn(generateModelsModule, 'generateModels')
.mockRejectedValueOnce(new Error('Failed to load models.dev catalog: 502 Bad Gateway'))
await expect(providerService.checkUpdates(team.id)).rejects.toThrow(
'Failed to load models.dev catalog: 502 Bad Gateway',
)
spy.mockRestore()
})
it('should throw an error when upstream catalog is empty in checkUpdates', async () => {
const team = await teamService.ensureDefaultTeam()
const spy = vi.spyOn(generateModelsModule, 'generateModels').mockResolvedValueOnce({
providers: {},
groupedProviders: {},
sortedProviderIds: [],
})
await expect(providerService.checkUpdates(team.id)).rejects.toThrow(
'Failed to retrieve model catalog from upstream services',
)
spy.mockRestore()
})
it('should apply sync by adding selected providers and models without modifying existing ones', async () => {
const team = await teamService.ensureDefaultTeam()
// Create an existing provider with 1 model and a custom apiKey
const existingProvider = await providerService.create(
team.id,
'test-existing-provider',
{ api: 'openai-responses', apiKey: 'custom-api-key', baseUrl: 'https://example.com' },
[
{
modelId: 'existing-m1',
name: 'Existing Model 1',
config: {
api: 'openai-responses',
reasoning: false,
input: ['text'],
contextWindow: 128000,
maxTokens: 4096,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
},
],
)
// Apply sync with 1 new provider and 1 new model on the existing provider
const syncResult = await providerService.applySync(team.id, {
providers: [
{
name: 'test-new-synced-provider',
isNewProvider: true,
config: {
api: 'anthropic-messages',
apiKey: 'ENV_SYNC_KEY',
baseUrl: 'https://api.anthropic.com',
},
models: [
{
modelId: 'new-p-m1',
name: 'New Provider Model 1',
config: {
api: 'anthropic-messages',
reasoning: false,
input: ['text'],
contextWindow: 200000,
maxTokens: 8192,
cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
},
},
],
},
{
name: 'test-existing-provider',
isNewProvider: false,
config: { api: 'openai-responses', apiKey: 'should-not-override' },
models: [
{
modelId: 'existing-m2',
name: 'Newly Added Model 2',
config: {
api: 'openai-responses',
reasoning: true,
input: ['text'],
contextWindow: 128000,
maxTokens: 4096,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
},
// Duplicate model that already exists: should be ignored
{
modelId: 'existing-m1',
name: 'Existing Model 1 Overwrite Attempt',
config: {
api: 'openai-responses',
reasoning: false,
input: ['text'],
contextWindow: 999999,
maxTokens: 999999,
cost: { input: 9, output: 9, cacheRead: 9, cacheWrite: 9 },
},
},
],
},
],
})
expect(syncResult.addedProviders).toBe(1)
expect(syncResult.addedModels).toBe(2)
// Verify existing provider config was NOT modified
const reloadedExisting = await providerService.getById(existingProvider.id)
expect((reloadedExisting?.config as { apiKey: string }).apiKey).toBe('custom-api-key')
// Verify existing model was NOT modified
const existingModels = await providerService.listModelsByProvider(team.id, existingProvider.id)
expect(existingModels).toHaveLength(2)
const m1 = existingModels.find((m) => m.modelId === 'existing-m1')
expect(m1?.name).toBe('Existing Model 1')
expect((m1?.config as { contextWindow: number }).contextWindow).toBe(128000)
// Verify newly added model on existing provider
const m2 = existingModels.find((m) => m.modelId === 'existing-m2')
expect(m2).toBeDefined()
expect(m2?.name).toBe('Newly Added Model 2')
// Verify newly added provider
const providers = await providerService.listByTeam(team.id)
const newProvider = providers.find((p) => p.name === 'test-new-synced-provider')
expect(newProvider).toBeDefined()
expect(newProvider?.isBuiltin).toBe(true)
expect((newProvider?.config as { apiKey: string }).apiKey).toBe('ENV_SYNC_KEY')
const newProviderModels = await providerService.listModelsByProvider(team.id, newProvider!.id)
expect(newProviderModels).toHaveLength(1)
expect(newProviderModels[0].modelId).toBe('new-p-m1')
})
})
+346 -39
View File
@@ -1,6 +1,21 @@
import { prisma } from '@shumai/db'
import { ProviderConfigSerializable, providerModelSchema } from '@shumai/dtos'
import { getBuiltinProvidersMap } from '@shumai/core/src/provider/builtin'
import {
CreateModelRequest,
ProviderConfigSerializable,
providerModelSchema,
SyncApplyRequest,
SyncApplyResponse,
SyncCheckResponse,
SyncModelItem,
SyncProviderItem,
UpdateModelRequest,
} from '@shumai/dtos'
import {
ENV_MAP,
getBuiltinProvidersMap,
PRIORITY_PROVIDERS,
} from '@shumai/core/src/provider/builtin'
import { generateModels } from '@shumai/core/src/provider/generator/generate-models'
import { z } from 'zod'
type ProviderModel = z.infer<typeof providerModelSchema>
@@ -74,7 +89,7 @@ export class ProviderService {
providerId,
provider: { teamId },
},
orderBy: { id: 'asc' },
orderBy: { id: 'desc' },
})
}
@@ -82,7 +97,7 @@ export class ProviderService {
teamId: string,
name: string,
config: ProviderConfigSerializable,
models: ProviderModel[],
models: ProviderModel[] = [],
) {
return this.prismaClient.provider.create({
data: {
@@ -105,60 +120,177 @@ export class ProviderService {
teamId: string,
id: string,
config: ProviderConfigSerializable,
models: ProviderModel[],
models?: ProviderModel[],
name?: string,
) {
return this.prismaClient.$transaction(async (tx) => {
const existingModels = await tx.model.findMany({
where: { providerId: id },
})
const existingByModelId = new Map(existingModels.map((m) => [m.modelId, m]))
const incomingModelIds = new Set(models.map((m) => m.modelId))
// Delete models that are no longer in the incoming list
const modelsToDelete = existingModels.filter((m) => !incomingModelIds.has(m.modelId))
if (modelsToDelete.length > 0) {
await tx.model.deleteMany({
where: {
id: { in: modelsToDelete.map((m) => m.id) },
},
if (models !== undefined) {
const existingModels = await tx.model.findMany({
where: { providerId: id },
})
}
// Update existing models or create new ones
for (const m of models) {
const existing = existingByModelId.get(m.modelId)
if (existing) {
await tx.model.update({
where: { id: existing.id },
data: {
name: m.name,
config: m.config,
},
})
} else {
await tx.model.create({
data: {
providerId: id,
modelId: m.modelId,
name: m.name,
config: m.config,
const existingByModelId = new Map(existingModels.map((m) => [m.modelId, m]))
const incomingModelIds = new Set(models.map((m) => m.modelId))
// Delete models that are no longer in the incoming list
const modelsToDelete = existingModels.filter((m) => !incomingModelIds.has(m.modelId))
if (modelsToDelete.length > 0) {
await tx.model.deleteMany({
where: {
id: { in: modelsToDelete.map((m) => m.id) },
},
})
}
// Update existing models or create new ones
for (const m of models) {
const existing = existingByModelId.get(m.modelId)
const modelConfig = {
...m.config,
api: m.config?.api || config.api,
}
if (existing) {
await tx.model.update({
where: { id: existing.id },
data: {
name: m.name,
config: modelConfig,
},
})
} else {
await tx.model.create({
data: {
providerId: id,
modelId: m.modelId,
name: m.name,
config: modelConfig,
},
})
}
}
} else if (config.api) {
// Propagate provider API protocol change to all existing models
const existingModels = await tx.model.findMany({
where: { providerId: id },
})
for (const m of existingModels) {
const currentConfig = m.config
if (currentConfig.api !== config.api) {
await tx.model.update({
where: { id: m.id },
data: {
config: {
...currentConfig,
api: config.api,
},
},
})
}
}
}
// Update provider config
// Update provider config and optional name
return tx.provider.update({
where: { id, teamId },
data: {
config,
...(name ? { name } : {}),
},
include: { models: true },
})
})
}
async createModel(teamId: string, providerId: string, data: CreateModelRequest) {
const provider = await this.prismaClient.provider.findUnique({
where: { id: providerId, teamId },
})
if (!provider) {
throw new Error('Provider not found')
}
const existing = await this.prismaClient.model.findFirst({
where: {
providerId,
modelId: data.modelId,
},
})
if (existing) {
throw new Error(`Model with id "${data.modelId}" already exists for this provider`)
}
return this.prismaClient.model.create({
data: {
providerId,
modelId: data.modelId,
name: data.name ?? '',
config: data.config,
},
})
}
async updateModel(
teamId: string,
providerId: string,
modelDbId: string,
data: UpdateModelRequest,
) {
const provider = await this.prismaClient.provider.findUnique({
where: { id: providerId, teamId },
})
if (!provider) {
throw new Error('Provider not found')
}
const model = await this.prismaClient.model.findFirst({
where: { id: modelDbId, providerId },
})
if (!model) {
throw new Error('Model not found')
}
if (data.modelId && data.modelId !== model.modelId) {
const existingWithNewId = await this.prismaClient.model.findFirst({
where: {
providerId,
modelId: data.modelId,
},
})
if (existingWithNewId) {
throw new Error(`Model with id "${data.modelId}" already exists for this provider`)
}
}
return this.prismaClient.model.update({
where: { id: modelDbId },
data: {
...(data.modelId ? { modelId: data.modelId } : {}),
...(data.name !== undefined ? { name: data.name } : {}),
...(data.config ? { config: data.config } : {}),
},
})
}
async deleteModel(teamId: string, providerId: string, modelDbId: string) {
const provider = await this.prismaClient.provider.findUnique({
where: { id: providerId, teamId },
})
if (!provider) {
throw new Error('Provider not found')
}
const model = await this.prismaClient.model.findFirst({
where: { id: modelDbId, providerId },
})
if (!model) {
throw new Error('Model not found')
}
return this.prismaClient.model.delete({
where: { id: modelDbId },
})
}
async delete(teamId: string, id: string) {
const provider = await this.prismaClient.provider.findUnique({
where: { id, teamId },
@@ -172,6 +304,181 @@ export class ProviderService {
where: { id, teamId },
})
}
async checkUpdates(teamId: string): Promise<SyncCheckResponse> {
const catalog = await generateModels({ strict: true, timeoutMs: 30000 })
if (!catalog || catalog.sortedProviderIds.length === 0) {
throw new Error('Failed to retrieve model catalog from upstream services')
}
const existingProviders = await this.prismaClient.provider.findMany({
where: { teamId },
include: { models: true },
})
const existingProviderMap = new Map<string, (typeof existingProviders)[number]>()
for (const p of existingProviders) {
existingProviderMap.set(p.name, p)
}
const items: SyncProviderItem[] = []
let totalNewProviders = 0
let totalNewModels = 0
for (const providerId of catalog.sortedProviderIds) {
const modelsMap = catalog.providers[providerId]
if (!modelsMap) continue
const modelList = Object.values(modelsMap)
if (modelList.length === 0) continue
const existingProvider = existingProviderMap.get(providerId)
if (!existingProvider) {
const firstModel = modelList[0]
const newModels: SyncModelItem[] = modelList.map((m) => ({
modelId: m.id,
name: m.name || m.id,
config: {
api: m.api as ProviderConfigSerializable['api'],
reasoning: m.reasoning ?? false,
input: (m.input ?? ['text']) as ('text' | 'image')[],
contextWindow: m.contextWindow,
maxTokens: m.maxTokens,
cost: {
input: m.cost?.input ?? 0,
output: m.cost?.output ?? 0,
cacheRead: m.cost?.cacheRead ?? 0,
cacheWrite: m.cost?.cacheWrite ?? 0,
},
},
}))
items.push({
name: providerId,
isNewProvider: true,
config: {
api: firstModel.api as ProviderConfigSerializable['api'],
baseUrl: firstModel.baseUrl,
apiKey: ENV_MAP[providerId] || '',
},
models: newModels,
})
totalNewProviders++
totalNewModels += newModels.length
} else {
const existingModelIds = new Set(existingProvider.models.map((m) => m.modelId))
const newModels: SyncModelItem[] = []
for (const m of modelList) {
if (!existingModelIds.has(m.id)) {
newModels.push({
modelId: m.id,
name: m.name || m.id,
config: {
api: m.api as ProviderConfigSerializable['api'],
reasoning: m.reasoning ?? false,
input: (m.input ?? ['text']) as ('text' | 'image')[],
contextWindow: m.contextWindow,
maxTokens: m.maxTokens,
cost: {
input: m.cost?.input ?? 0,
output: m.cost?.output ?? 0,
cacheRead: m.cost?.cacheRead ?? 0,
cacheWrite: m.cost?.cacheWrite ?? 0,
},
},
})
}
}
if (newModels.length > 0) {
items.push({
name: providerId,
isNewProvider: false,
config: existingProvider.config as ProviderConfigSerializable,
models: newModels,
})
totalNewModels += newModels.length
}
}
}
items.sort((a, b) => {
const indexA = PRIORITY_PROVIDERS.indexOf(a.name)
const indexB = PRIORITY_PROVIDERS.indexOf(b.name)
if (indexA !== -1 && indexB !== -1) return indexA - indexB
if (indexA !== -1) return -1
if (indexB !== -1) return 1
return a.name.localeCompare(b.name)
})
return {
providers: items,
totalNewProviders,
totalNewModels,
}
}
async applySync(teamId: string, payload: SyncApplyRequest): Promise<SyncApplyResponse> {
let addedProviders = 0
let addedModels = 0
await this.prismaClient.$transaction(async (tx) => {
for (const providerItem of payload.providers) {
let providerId: string
if (providerItem.isNewProvider) {
const existing = await tx.provider.findFirst({
where: { teamId, name: providerItem.name },
})
if (!existing) {
const created = await tx.provider.create({
data: {
teamId,
name: providerItem.name,
isBuiltin: true,
config: providerItem.config,
},
})
providerId = created.id
addedProviders++
} else {
providerId = existing.id
}
} else {
const existing = await tx.provider.findFirst({
where: { teamId, name: providerItem.name },
})
if (!existing) continue
providerId = existing.id
}
for (const modelItem of providerItem.models) {
const existingModel = await tx.model.findFirst({
where: {
providerId,
modelId: modelItem.modelId,
},
})
if (!existingModel) {
await tx.model.create({
data: {
providerId,
modelId: modelItem.modelId,
name: modelItem.name || modelItem.modelId,
config: modelItem.config,
},
})
addedModels++
}
}
}
})
return { addedProviders, addedModels }
}
}
export const providerService = new ProviderService()
+10 -10
View File
@@ -21,12 +21,12 @@ describe('QuotaRule Wildcard Matchers', () => {
it('correctly matches agent tool name wildcards', () => {
const starTool = wildcardToRegex('*')
expect(starTool.test('analyze_image')).toBe(true)
expect(starTool.test('screenshot')).toBe(true)
expect(starTool.test('read_asset')).toBe(true)
expect(starTool.test('create_file')).toBe(true)
const exactTool = wildcardToRegex('analyze_image')
expect(exactTool.test('analyze_image')).toBe(true)
expect(exactTool.test('screenshot')).toBe(false)
const exactTool = wildcardToRegex('read_asset')
expect(exactTool.test('read_asset')).toBe(true)
expect(exactTool.test('create_file')).toBe(false)
})
})
@@ -405,29 +405,29 @@ describe('QuotaService', () => {
await quotaService.createRule(team.id, {
scopeMode: 'all_members',
resource: 'agent_tool_call_count',
resourceData: { name: 'analyze_image' },
resourceData: { name: 'read_asset' },
limit: 1,
period: '1hour',
enabled: true,
})
// First call to analyze_image
// First call to read_asset
await quotaService.consumeQuota(
{
teamId: team.id,
resource: 'agent_tool_call_count',
resourceData: { name: 'analyze_image' },
resourceData: { name: 'read_asset' },
},
1,
)
// 2nd call to analyze_image is blocked
// 2nd call to read_asset is blocked
await expect(
quotaService.checkQuota(
{
teamId: team.id,
resource: 'agent_tool_call_count',
resourceData: { name: 'analyze_image' },
resourceData: { name: 'read_asset' },
},
1,
),
+3 -3
View File
@@ -1225,12 +1225,12 @@ export class TranscodeService {
// 4. Extract each screenshot
for (const t of timestamps) {
const outName = `shot-${t.toFixed(3)}-${ulid()}.webp`
const outName = `shot-${t.toFixed(4)}-${ulid()}.webp`
const localShotPath = path.join(tmpDir, outName)
const args = [
'-ss',
t.toFixed(3),
t.toFixed(4),
'-i',
videoPath,
'-vframes',
@@ -1248,7 +1248,7 @@ export class TranscodeService {
const isMatch =
commentTimestamp !== undefined &&
commentTimestamp !== null &&
Math.abs(t - commentTimestamp) < 1e-4
Math.abs(t - commentTimestamp) <= 1e-3
// 5. Overlay annotations if timestamp matches commentTimestamp (within float tolerance)
if (isMatch && params.annotations && params.annotations.length > 0) {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/db",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"main": "./src/index.ts",
+1 -1
View File
@@ -117,7 +117,7 @@ enum WorkflowTaskType {
chat
ai_embedding
query_embedding_for_search
agent_tool_call
agent_tool_call // @deprecated: Agent tools now execute directly in-process via Core services
kanban_agent_run
}
+5 -7
View File
@@ -8,6 +8,8 @@ declare global {
'id' | 'type' | 'parentId' | 'timestamp'
>
export type ShumaiMessageContext = import('@shumai/dtos').ShumaiMessageContext
// ----------------------------------------------------------------------
// User Agent Settings
// ----------------------------------------------------------------------
@@ -275,7 +277,6 @@ declare global {
queryEmbeddingForSearch?: {
text: string
}
agentToolCall?: AgentToolCallPayload
screenshot?: ScreenshotSpec
imageAnnotation?: ImageAnnotationSpec
pdfPages?: PdfPagesSpec
@@ -285,12 +286,6 @@ declare global {
// ----------------------------------------------------------------------
// Task Spec & Payload
// ----------------------------------------------------------------------
export interface AgentToolCallPayload {
toolName: string
args: Record<string, unknown>
userId: string
}
export interface TaskSpec {
videoStrategy?: VideoTranscodeStrategy
hardwareAcceleration?: HardwareAcceleration
@@ -310,6 +305,9 @@ declare global {
assetIds?: string[]
isNewChat?: boolean
hasAssetChanged?: boolean
second?: number | null
annotations?: Record<string, unknown>[]
context?: import('@shumai/dtos').ShumaiMessageContext
}
// ----------------------------------------------------------------------
+1 -8
View File
@@ -1,7 +1,6 @@
import { PostgreSqlContainer } from '@testcontainers/postgresql'
import { exec } from 'node:child_process'
import { promisify } from 'node:util'
import { Wait } from 'testcontainers'
const execAsync = promisify(exec)
@@ -9,13 +8,7 @@ let dbContainer: import('@testcontainers/postgresql').StartedPostgreSqlContainer
export async function globalTestSetup() {
console.log('Starting Testcontainers for global setup...')
dbContainer = await new PostgreSqlContainer('pgvector/pgvector:pg18')
.withWaitStrategy(
// casting to any due to testcontainers wait strategy type mismatch
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Wait.forLogMessage('database system is ready to accept connections', 1) as any,
)
.start()
dbContainer = await new PostgreSqlContainer('pgvector/pgvector:pg18').start()
const databaseUrl = dbContainer.getConnectionUri()
console.log('Database started at:', databaseUrl)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/dtos",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"main": "./src/index.ts",
+4 -1
View File
@@ -160,9 +160,12 @@ export const updateAssetNameRequestSchema = z.object({
})
export type UpdateAssetNameRequest = z.infer<typeof updateAssetNameRequestSchema>
export const assetTypeFilterSchema = z.enum(['file', 'folder', 'all'])
export type AssetTypeFilter = z.infer<typeof assetTypeFilterSchema>
export const listChildrenRequestSchema = z.object({
assetId: z.string().optional(),
assetType: z.string(),
assetType: assetTypeFilterSchema.optional().default('all'),
projectId: z.string().optional(),
showDeleted: z.boolean().optional(),
prefix: z.string().optional(),
+86 -4
View File
@@ -10,15 +10,19 @@ export const chatRequestSchema = z
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
second: z.number().nullable().optional(),
annotations: z.array(z.record(z.string(), z.unknown())).optional(),
})
.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) {
const hasAnnotations = data.annotations && data.annotations.length > 0
if (!hasPrompt && !hasFiles && !hasAssets && !hasAnnotations) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'At least one of textPrompt, attachedFiles, or assetIds must be provided.',
message:
'At least one of textPrompt, attachedFiles, assetIds, or annotations must be provided.',
path: ['textPrompt'],
})
}
@@ -39,11 +43,89 @@ export const chatSessionInfoSchema = z.object({
export type ChatSessionInfo = z.infer<typeof chatSessionInfoSchema>
export const shumaiAssetContextSchema = z.object({
id: z.string(),
name: z.string(),
type: z.string(),
mediaType: z.enum(['image', 'video', 'pdf', 'audio', 'other']).optional(),
mimeType: z.string().optional(),
parentId: z.string().optional(),
path: z.string().optional(),
durationSeconds: z.number().optional(),
totalFrames: z.number().optional(),
totalPages: z.number().optional(),
navigated: z.boolean().optional(),
ancestors: z.array(z.object({ id: z.string(), name: z.string() })).optional(),
})
export type ShumaiAssetContext = z.infer<typeof shumaiAssetContextSchema>
export const shumaiAttachedFileContextSchema = z.object({
id: z.string(),
name: z.string(),
type: z.string(),
mediaType: z.enum(['image', 'video', 'pdf', 'audio', 'other']).optional(),
mimeType: z.string().optional(),
path: z.string().optional(),
url: z.string().optional(),
})
export type ShumaiAttachedFileContext = z.infer<typeof shumaiAttachedFileContextSchema>
export const shumaiMediaPositionSchema = z.discriminatedUnion('type', [
z.object({ type: z.literal('time'), seconds: z.number() }),
z.object({ type: z.literal('page'), page: z.number() }),
])
export type ShumaiMediaPosition = z.infer<typeof shumaiMediaPositionSchema>
export const shumaiThreadContextSchema = z.object({
id: z.string(),
replyCount: z.number().optional(),
})
export type ShumaiThreadContext = z.infer<typeof shumaiThreadContextSchema>
export const shumaiMessageContextSchema = z.object({
id: z.string().optional(),
user: z
.object({
id: z.string(),
name: z.string(),
role: z.string(),
})
.optional(),
thread: shumaiThreadContextSchema.optional(),
currentAsset: shumaiAssetContextSchema.optional(),
position: shumaiMediaPositionSchema.optional(),
annotation: z.boolean().optional(),
annotations: z.array(z.record(z.string(), z.unknown())).optional(),
attachedFiles: z.array(shumaiAttachedFileContextSchema).optional(),
referencedAssets: z.array(shumaiAttachedFileContextSchema).optional(),
})
export type ShumaiMessageContext = z.infer<typeof shumaiMessageContextSchema>
export const chatMessageSchema = z.intersection(
z.object({
id: z.string(),
}),
z.unknown(),
) as unknown as z.ZodType<AgentMessage & { id: string }>
) as unknown as z.ZodType<ChatMessage>
export type ChatMessage = AgentMessage & { id: string }
export type ChatMessage = (
| AgentMessage
| {
role: 'custom'
customType: string
content?: unknown
display?: boolean
details?: unknown
timestamp?: number
}
| {
role: 'thinking_level_change'
content: string
timestamp?: number
}
) & { id: string }
+164
View File
@@ -0,0 +1,164 @@
import type { ShumaiMessageContext } from './chat'
/**
* Escapes special XML characters in attribute and text values to prevent
* XML malformation and prompt injection.
*/
export function escapeXmlAttr(str: string): string {
return str
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
}
function formatAttr(name: string, value: string | undefined): string {
if (value === undefined || value === null) return ''
return ` ${name}="${escapeXmlAttr(value)}"`
}
/**
* Pure, deterministic serializer that converts a ShumaiMessageContext
* object into a standardized, order-stable <context> XML block.
*
* Adheres strictly to the following invariants:
* 1. Tag sequence: <user>, <thread>, <current_asset>, <position>, <annotation>, <attached_files>, <referenced_assets>
* 2. Attribute sequence on every tag follows fixed, invariant order.
* 3. Arrays (attachedFiles, referencedAssets) sorted by id ascending.
* 4. Time position floats formatted with .toFixed(4), duration with .toFixed(2), integers rounded.
* 5. String values escaped via escapeXmlAttr.
* 6. Exact 2-space indentation and LF newlines.
*/
export function serializeContextToXml(context?: ShumaiMessageContext | null): string {
if (!context) return ''
const lines: string[] = []
// 1. <user ... />
if (context.user) {
let userAttrs = ''
if (context.user.id !== undefined) userAttrs += formatAttr('id', context.user.id)
if (context.user.name !== undefined) userAttrs += formatAttr('name', context.user.name)
if (context.user.role !== undefined) userAttrs += formatAttr('role', context.user.role)
if (userAttrs) {
lines.push(` <user${userAttrs} />`)
}
}
// 2. <thread ... />
if (context.thread) {
let threadAttrs = ''
if (context.thread.id !== undefined) threadAttrs += formatAttr('id', context.thread.id)
if (context.thread.replyCount !== undefined) {
threadAttrs += ` reply_count="${Math.round(context.thread.replyCount)}"`
}
if (threadAttrs) {
lines.push(` <thread${threadAttrs} />`)
}
}
// 3. <current_asset ...> ... </current_asset>
if (context.currentAsset) {
const ca = context.currentAsset
let assetAttrs = ''
if (ca.id !== undefined) assetAttrs += formatAttr('id', ca.id)
if (ca.name !== undefined) assetAttrs += formatAttr('name', ca.name)
if (ca.type !== undefined) assetAttrs += formatAttr('type', ca.type)
if (ca.mediaType !== undefined) assetAttrs += formatAttr('media_type', ca.mediaType)
if (ca.mimeType !== undefined) assetAttrs += formatAttr('mime_type', ca.mimeType)
if (ca.parentId !== undefined) assetAttrs += formatAttr('parent_id', ca.parentId)
if (ca.path !== undefined) assetAttrs += formatAttr('path', ca.path)
if (ca.durationSeconds !== undefined) {
assetAttrs += ` duration_seconds="${ca.durationSeconds.toFixed(2)}"`
}
if (ca.totalFrames !== undefined) {
assetAttrs += ` total_frames="${Math.round(ca.totalFrames)}"`
}
if (ca.totalPages !== undefined) {
assetAttrs += ` total_pages="${Math.round(ca.totalPages)}"`
}
if (ca.navigated !== undefined) {
assetAttrs += ` navigated="${String(ca.navigated)}"`
}
if (ca.ancestors && ca.ancestors.length > 0) {
lines.push(` <current_asset${assetAttrs}>`)
lines.push(' <ancestors>')
for (const anc of ca.ancestors) {
let ancAttrs = ''
if (anc.id !== undefined) ancAttrs += formatAttr('id', anc.id)
if (anc.name !== undefined) ancAttrs += formatAttr('name', anc.name)
lines.push(` <folder${ancAttrs} />`)
}
lines.push(' </ancestors>')
lines.push(' </current_asset>')
} else {
lines.push(` <current_asset${assetAttrs} />`)
}
}
// 3. <position ... />
if (context.position) {
if (context.position.type === 'time') {
lines.push(` <position type="time" seconds="${context.position.seconds.toFixed(4)}" />`)
} else if (context.position.type === 'page') {
lines.push(` <position type="page" page="${Math.round(context.position.page)}" />`)
}
}
// 4. <annotation ... />
if (context.annotation) {
if (context.id !== undefined) {
lines.push(` <annotation id="${escapeXmlAttr(context.id)}" />`)
} else {
lines.push(' <annotation />')
}
}
// 5. <attached_files> ... </attached_files>
if (context.attachedFiles && context.attachedFiles.length > 0) {
const sortedFiles = [...context.attachedFiles].sort((a, b) => a.id.localeCompare(b.id))
lines.push(' <attached_files>')
for (const file of sortedFiles) {
let fileAttrs = ''
if (file.id !== undefined) fileAttrs += formatAttr('id', file.id)
if (file.name !== undefined) fileAttrs += formatAttr('name', file.name)
if (file.type !== undefined) fileAttrs += formatAttr('type', file.type)
if (file.mediaType !== undefined) fileAttrs += formatAttr('media_type', file.mediaType)
if (file.mimeType !== undefined) fileAttrs += formatAttr('mime_type', file.mimeType)
if (file.path !== undefined) fileAttrs += formatAttr('path', file.path)
lines.push(` <file${fileAttrs} />`)
}
lines.push(' </attached_files>')
}
// 6. <referenced_assets> ... </referenced_assets>
if (context.referencedAssets && context.referencedAssets.length > 0) {
const sortedAssets = [...context.referencedAssets].sort((a, b) => a.id.localeCompare(b.id))
lines.push(' <referenced_assets>')
for (const item of sortedAssets) {
if (item.type === 'folder') {
let folderAttrs = ''
if (item.id !== undefined) folderAttrs += formatAttr('id', item.id)
if (item.name !== undefined) folderAttrs += formatAttr('name', item.name)
if (item.path !== undefined) folderAttrs += formatAttr('path', item.path)
lines.push(` <folder${folderAttrs} />`)
} else {
let assetAttrs = ''
if (item.id !== undefined) assetAttrs += formatAttr('id', item.id)
if (item.name !== undefined) assetAttrs += formatAttr('name', item.name)
if (item.type !== undefined) assetAttrs += formatAttr('type', item.type)
if (item.mediaType !== undefined) assetAttrs += formatAttr('media_type', item.mediaType)
if (item.mimeType !== undefined) assetAttrs += formatAttr('mime_type', item.mimeType)
if (item.path !== undefined) assetAttrs += formatAttr('path', item.path)
lines.push(` <asset${assetAttrs} />`)
}
}
lines.push(' </referenced_assets>')
}
if (lines.length === 0) return ''
return `<context>\n${lines.join('\n')}\n</context>`
}
+1
View File
@@ -25,3 +25,4 @@ export * from './watermark'
export * from './quota'
export * from './kanban'
export * from './recents'
export * from './context'
+2 -1
View File
@@ -1,6 +1,7 @@
import { z } from 'zod'
import { UserType } from '@shumai/db/enums'
import { paginationPageInfoSchema, paginationParamsSchema } from './pagination'
import { assetTypeFilterSchema } from './asset'
export const createProjectRequestSchema = z.object({
name: z.string(),
@@ -58,7 +59,7 @@ export type ProjectUserInfo = z.infer<typeof projectUserInfoSchema>
export const recentlyDeletedRequestSchema = z
.object({
assetType: z.string(),
assetType: assetTypeFilterSchema.optional().default('all'),
sort: z.enum(['createdAt', 'name', 'size']).optional(),
order: z.enum(['asc', 'desc']).optional(),
})
+68 -2
View File
@@ -49,14 +49,35 @@ export type ProviderConfigSerializable = z.infer<typeof providerConfigSchema>
export const createProviderRequestSchema = z.object({
name: z.string().min(1, 'Provider name is required'),
config: providerConfigSchema,
models: z.array(providerModelSchema).min(1, 'At least one model is required'),
models: z.array(providerModelSchema).default([]),
})
export type CreateProviderRequest = z.infer<typeof createProviderRequestSchema>
export const updateProviderRequestSchema = z.object({
name: z.string().min(1, 'Provider name is required').optional(),
config: providerConfigSchema,
models: z.array(providerModelSchema).min(1, 'At least one model is required'),
models: z.array(providerModelSchema).optional(),
})
export type UpdateProviderRequest = z.infer<typeof updateProviderRequestSchema>
export const createModelRequestSchema = z.object({
modelId: z.string().min(1, 'Model ID is required'),
name: z.string().min(0).default(''),
config: providerModelConfigSchema,
})
export type CreateModelRequest = z.infer<typeof createModelRequestSchema>
export const updateModelRequestSchema = z.object({
modelId: z.string().min(1, 'Model ID is required').optional(),
name: z.string().min(0).optional(),
config: providerModelConfigSchema.optional(),
})
export type UpdateModelRequest = z.infer<typeof updateModelRequestSchema>
export const providerResponseSchema = z.object({
id: z.string(),
name: z.string(),
@@ -69,3 +90,48 @@ export const providerResponseSchema = z.object({
})
export type ProviderResponse = z.infer<typeof providerResponseSchema>
export const syncModelItemSchema = z.object({
modelId: z.string().min(1),
name: z.string().default(''),
config: providerModelConfigSchema,
})
export type SyncModelItem = z.infer<typeof syncModelItemSchema>
export const syncProviderItemSchema = z.object({
name: z.string(),
isNewProvider: z.boolean(),
config: providerConfigSchema,
models: z.array(syncModelItemSchema),
})
export type SyncProviderItem = z.infer<typeof syncProviderItemSchema>
export const syncCheckResponseSchema = z.object({
providers: z.array(syncProviderItemSchema),
totalNewProviders: z.number(),
totalNewModels: z.number(),
})
export type SyncCheckResponse = z.infer<typeof syncCheckResponseSchema>
export const syncApplyRequestSchema = z.object({
providers: z.array(
z.object({
name: z.string(),
isNewProvider: z.boolean(),
config: providerConfigSchema,
models: z.array(syncModelItemSchema),
}),
),
})
export type SyncApplyRequest = z.infer<typeof syncApplyRequestSchema>
export const syncApplyResponseSchema = z.object({
addedProviders: z.number(),
addedModels: z.number(),
})
export type SyncApplyResponse = z.infer<typeof syncApplyResponseSchema>
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/e2e",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"dependencies": {
@@ -1,146 +0,0 @@
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)
},
)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/tableprinter",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"main": "./src/index.ts",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/transcode",
"version": "0.4.0",
"version": "0.4.2",
"private": true,
"type": "module",
"main": "./src/index.ts",
@@ -107,4 +107,63 @@ describe('takeVideoScreenshotsWorkflow', () => {
},
})
})
it('should prefer highest-resolution video transcode proxy over master storageKey', async () => {
const task: WorkflowTask = {
id: 'task-screenshot-proxy',
assetId: 'asset-video-proxy',
type: WorkflowTaskType.transcode_screenshot,
status: WorkflowTaskStatus.pending,
sessionId: null,
output: null,
payload: {
projectId: 'proj-1',
screenshot: {
start: 1.2345,
end: 1.2345,
count: 1,
commentTimestamp: 1.2345,
annotations: [],
},
},
createdAt: new Date(),
updatedAt: new Date(),
heartbeat: null,
teamId: 'team-1',
projectId: 'proj-1',
uid: 'task-uid',
model: null,
inputTokens: 0,
outputTokens: 0,
}
mockActivities.getAssetActivity.mockResolvedValue({
id: 'asset-video-proxy',
storageKey: { key: 'raw-master.mp4' },
mediaType: 'video/mp4',
media: {
videoTranscodes: [
{ key: 'proxy-720p.mp4', height: 720, width: 1280 },
{ key: 'proxy-1080p.mp4', height: 1080, width: 1920 },
{ key: 'proxy-360p.mp4', height: 360, width: 640 },
],
},
})
mockActivities.takeScreenshotsActivity.mockResolvedValue([
{ key: 'files/asset-video-proxy/screenshots/shot1.webp', timestamp: 1.2345 },
])
await takeVideoScreenshotsWorkflow(task)
expect(mockActivities.takeScreenshotsActivity).toHaveBeenCalledWith({
assetKey: 'proxy-1080p.mp4',
assetId: 'asset-video-proxy',
start: 1.2345,
end: 1.2345,
count: 1,
commentTimestamp: 1.2345,
annotations: [],
})
})
})
@@ -17,10 +17,23 @@ export async function takeVideoScreenshotsWorkflow(task: WorkflowTask): Promise<
}
const { asset, key } = await fetchAssetWithKey(workerQueue, task.assetId)
let targetKey = key
if (asset?.media) {
const mediaInfo = asset.media as unknown as PrismaJson.MediaInfo
if (mediaInfo.videoTranscodes && mediaInfo.videoTranscodes.length > 0) {
const sorted = [...mediaInfo.videoTranscodes]
.filter((t) => t.key)
.sort((a, b) => (b.height || 0) - (a.height || 0))
if (sorted.length > 0 && sorted[0].key) {
targetKey = sorted[0].key
}
}
}
const { takeScreenshotsActivity } = getActivities()
const screenshots = await executeActivity(workerQueue, takeScreenshotsActivity, {
assetKey: key,
assetKey: targetKey,
assetId: asset.id,
start: payload.screenshot.start,
end: payload.screenshot.end,
@@ -14,6 +14,7 @@ import { formatTimeAgo } from '@/ui/lib/time'
import { m } from '@/ui/paraglide/messages.js'
import { Terminal } from 'lucide-react'
import Markdown from 'react-markdown'
import { serializeContextToXml, type ShumaiMessageContext } from '@shumai/dtos'
interface AgentSessionLogsDialogProps {
sessionId?: string | null
@@ -87,14 +88,59 @@ export function AgentSessionLogsDialog({
}
if (piEntry && typeof piEntry === 'object') {
if (piEntry.type === 'custom_message') {
if (piEntry.customType === 'context') return null
if (piEntry.customType === 'shumai_message') {
const xml = serializeContextToXml(piEntry.details as ShumaiMessageContext)
const text = typeof piEntry.content === 'string' ? piEntry.content : ''
const fullText = xml ? `${text}\n\n${xml}`.trim() : text
const badgeColor = 'bg-primary/10 text-primary'
const roleName = m.role_user()
return (
<div key={entry.id} className="relative pl-6 pb-6 last:pb-0">
{/* Timeline Line */}
<div className="absolute left-[9px] top-2 bottom-0 w-0.5 bg-foreground/10 last:hidden" />
{/* Timeline Node */}
<div className="absolute left-0 top-1.5 h-5 w-5 rounded-full border-4 border-background flex items-center justify-center bg-primary" />
<div className="flex flex-col gap-1.5 bg-muted/30 dark:bg-muted/10 p-3 rounded-lg border border-foreground/5 backdrop-blur-xs w-full">
<div className="flex items-center justify-between gap-2">
<div className="flex items-center gap-2 flex-wrap">
<span
className={`text-[10px] px-2 py-0.5 rounded-full font-semibold uppercase tracking-wider ${badgeColor}`}
>
{roleName}
</span>
<span
className="text-[10px] font-mono text-muted-foreground bg-muted/50 px-1.5 py-0.5 rounded border border-foreground/10 select-all"
title={m.entry_id()}
>
{m.entry_id()}: {entry.id}
</span>
</div>
<span className="text-[10px] text-muted-foreground/60 shrink-0">
{timestamp}
</span>
</div>
<div className="text-xs text-foreground/90 whitespace-pre-wrap leading-relaxed font-sans">
{fullText}
</div>
</div>
</div>
)
}
}
if (piEntry.type === 'message' && piEntry.message) {
const msg = piEntry.message as Record<string, unknown>
let badgeColor = 'bg-gray-100 text-gray-800 dark:bg-gray-800 dark:text-gray-200'
let roleName: string = String(msg.role || '')
if (msg.role === 'user') {
badgeColor =
'bg-slate-100 text-slate-800 dark:bg-slate-900/30 dark:text-slate-300'
badgeColor = 'bg-primary/10 text-primary'
roleName = m.role_user()
} else if (msg.role === 'assistant') {
badgeColor =
@@ -283,7 +329,7 @@ export function AgentSessionLogsDialog({
<div
className={`absolute left-0 top-1.5 h-5 w-5 rounded-full border-4 border-background flex items-center justify-center ${
msg.role === 'user'
? 'bg-slate-500'
? 'bg-primary'
: msg.role === 'assistant'
? 'bg-violet-500'
: msg.role === 'toolResult'
@@ -304,9 +350,9 @@ export function AgentSessionLogsDialog({
</span>
<span
className="text-[10px] font-mono text-muted-foreground bg-muted/50 px-1.5 py-0.5 rounded border border-foreground/10 select-all"
title="Entry ID"
title={m.entry_id()}
>
ID: {entry.id}
{m.entry_id()}: {entry.id}
</span>
</div>
<span className="text-[10px] text-muted-foreground/60 shrink-0">
@@ -0,0 +1,101 @@
// @vitest-environment happy-dom
import React from 'react'
import { cleanup, render, screen, fireEvent } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import type { CommentInfo } from '@shumai/dtos'
import { MessageCard } from './message-card'
function createWrapper() {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
})
return ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)
}
describe('MessageCard component', () => {
afterEach(() => {
cleanup()
vi.clearAllMocks()
})
it('renders message text and attachment rows with 2x height for image and standard height for other files', () => {
const mockMessage: CommentInfo = {
id: 'msg-1',
assetId: 'asset-1',
message: 'Here is the feedback on design',
annotations: null,
second: null,
creator: { id: 'u-1', name: 'Bob Reviewer' },
replies: [],
attachments: [
{
id: 'att-img-1',
assetId: 'asset-1',
url: 'https://example.com/screenshot.png?AWSAccessKeyId=123',
proxyType: null,
},
{
id: 'att-doc-2',
assetId: 'asset-1',
url: 'https://example.com/notes.pdf?AWSAccessKeyId=123',
proxyType: null,
},
{
id: 'att-psd-3',
assetId: 'asset-1',
url: 'https://example.com/mockup.psd',
proxyType: null,
},
],
mentions: [],
createdAt: '2026-08-20T10:30:00.000Z',
updatedAt: '2026-08-20T10:30:00.000Z',
sessionId: null,
isCompleted: false,
completionLastChangedBy: null,
}
const onReply = vi.fn()
const getUser = vi.fn().mockReturnValue({ id: 'u-1', name: 'Bob Reviewer' })
render(<MessageCard message={mockMessage} getUser={getUser} onReply={onReply} />, {
wrapper: createWrapper(),
})
expect(screen.getByText('Here is the feedback on design')).toBeDefined()
expect(screen.getByText('screenshot.png')).toBeDefined()
expect(screen.getByText('notes.pdf')).toBeDefined()
expect(screen.getByText('mockup.psd')).toBeDefined()
// Verify row height classes: image row is h-18 (72px), file row is h-9 (36px)
const imgEl = screen.getByAltText('screenshot.png')
const imgRow = imgEl.closest('.group')
expect(imgRow?.className).toContain('h-18')
const fileEl = screen.getByText('notes.pdf')
const fileRow = fileEl.closest('.group')
expect(fileRow?.className).toContain('h-9')
// PSD should NOT be an image row (web browsers cannot render PSD in <img>)
const psdEl = screen.getByText('mockup.psd')
const psdRow = psdEl.closest('.group')
expect(psdRow?.className).toContain('h-9')
// Clicking image row opens image preview dialog
fireEvent.click(imgRow!)
expect(screen.getAllByAltText('screenshot.png').length).toBeGreaterThanOrEqual(2)
// Clicking file row opens URL in a new tab
const windowOpenSpy = vi.spyOn(window, 'open').mockImplementation(() => null)
fireEvent.click(fileRow!)
expect(windowOpenSpy).toHaveBeenCalledWith(
'https://example.com/notes.pdf?AWSAccessKeyId=123',
'_blank',
'noreferrer',
)
windowOpenSpy.mockRestore()
})
})
+64 -22
View File
@@ -16,10 +16,11 @@ import {
AlertDialogHeader,
AlertDialogTitle,
} from '@/ui/components/ui/alert-dialog'
import type { AttachmentInfo, CommentInfo, UserInfo } from '@shumai/dtos'
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/ui/components/ui/dialog'
import type { CommentInfo, UserInfo } from '@shumai/dtos'
import type { MemberInfo } from '@/ui/stores/members'
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { Download, File, MoreHorizontal, Trash2 } from 'lucide-react'
import { Download, FileText, MoreHorizontal, Trash2 } from 'lucide-react'
import React from 'react'
import Markdown from 'react-markdown'
import { Avatar, AvatarFallback, AvatarImage } from '../ui/avatar'
@@ -30,6 +31,7 @@ import { m } from '@/ui/paraglide/messages.js'
import { useUiStore } from '@/ui/stores/ui'
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '../ui/tooltip'
import { AgentSessionLogsDialog } from '../agent/agent-session-logs-dialog'
import { isImageFileName } from '@/ui/lib/media'
interface MessageCardProps {
teamId?: string
@@ -39,7 +41,6 @@ interface MessageCardProps {
isLastReply?: boolean
getUser: (id: string) => MemberInfo | UserInfo
onReply: (message: CommentInfo) => void
onViewAttachment: (attachment: AttachmentInfo) => void
isSelected?: boolean
onSelect?: () => void
frameRate?: number
@@ -100,7 +101,6 @@ export const MessageCard: React.FC<MessageCardProps> = ({
isLastReply,
getUser,
onReply,
onViewAttachment,
isSelected,
onSelect,
frameRate,
@@ -116,6 +116,7 @@ export const MessageCard: React.FC<MessageCardProps> = ({
const loadingText = (message.message && AI_PLACEHOLDERS[message.message]) || 'Generating...'
const [isLogsOpen, setIsLogsOpen] = React.useState(false)
const [previewImage, setPreviewImage] = React.useState<{ url: string; name: string } | null>(null)
const { videoTimeDisplayMode } = useUiStore()
const displayTime = React.useMemo(() => {
@@ -325,39 +326,56 @@ export const MessageCard: React.FC<MessageCardProps> = ({
{/* Attachments */}
{message.attachments && message.attachments.length > 0 && (
<div className="flex flex-wrap gap-2 mt-3">
<div className="flex flex-col gap-1.5 mt-3 w-full max-w-full overflow-hidden">
{message.attachments.map((att) => {
const isImage = att.proxyType === 'image'
const name = att.url?.split('/').pop()?.split('?')[0] || 'file'
const rawName = att.url?.split('/').pop()?.split('?')[0] || 'file'
const name = decodeURIComponent(rawName)
const isImage = isImageFileName(name)
return (
<div
key={att.id}
className="relative group rounded-lg overflow-hidden border bg-white shadow-sm hover:shadow-md transition-shadow w-48 h-32"
onClick={() => isImage && onViewAttachment(att)}
className={`group relative flex items-center w-full max-w-full rounded-lg border border-border bg-card hover:bg-muted/40 transition-colors overflow-hidden ${
isImage ? 'h-18 p-1.5 gap-2.5 cursor-pointer' : 'h-9 px-2.5 gap-2.5'
}`}
onClick={() => {
if (isImage) {
setPreviewImage({ url: att.url, name })
} else {
window.open(att.url, '_blank', 'noreferrer')
}
}}
>
{isImage ? (
<img
src={att.url}
alt={name}
className="w-full h-full object-cover cursor-pointer"
/>
) : (
<div className="w-full h-full bg-gray-100 flex flex-col items-center justify-center p-2 text-center">
<File className="w-8 h-8 text-gray-500 mb-2" />
<span className="text-xs font-medium text-gray-700 break-all line-clamp-2">
{name}
</span>
<div className="h-full aspect-square rounded-md overflow-hidden bg-muted/40 shrink-0">
<img
src={att.url}
alt={name}
className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-200"
/>
</div>
) : (
<FileText className="w-4 h-4 text-muted-foreground shrink-0" />
)}
<div className="min-w-0 flex-1 overflow-hidden">
<p
className="text-xs font-medium text-foreground truncate block w-full"
title={name}
>
{name}
</p>
</div>
<a
href={att.url}
target="_blank"
rel="noreferrer"
download={name}
className="absolute right-2 bottom-2 bg-black/60 text-white hover:bg-black/80 p-1.5 rounded-full opacity-0 group-hover:opacity-100 transition-opacity"
className="p-1 text-muted-foreground hover:text-foreground shrink-0 rounded-md hover:bg-muted transition-colors opacity-0 group-hover:opacity-100"
onClick={(e) => e.stopPropagation()}
title={m.download()}
>
<Download className="w-4 h-4" />
<Download className="w-3.5 h-3.5" />
</a>
</div>
)
@@ -458,6 +476,30 @@ export const MessageCard: React.FC<MessageCardProps> = ({
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
{/* Image Preview Dialog */}
<Dialog open={!!previewImage} onOpenChange={(open) => !open && setPreviewImage(null)}>
<DialogContent
className="w-auto max-w-[80vw] sm:max-w-[80vw] max-h-[80vh] p-2 sm:p-3 bg-background/95 backdrop-blur-sm border-border flex flex-col items-center justify-center overflow-hidden"
onClick={(e) => e.stopPropagation()}
>
<DialogHeader className="sr-only">
<DialogTitle>{previewImage?.name || 'Image Preview'}</DialogTitle>
</DialogHeader>
{previewImage && (
<div className="flex flex-col items-center justify-center max-w-[80vw] max-h-[80vh] overflow-hidden">
<img
src={previewImage.url}
alt={previewImage.name}
className="max-w-[80vw] max-h-[calc(80vh-2.5rem)] w-auto h-auto object-contain rounded-md"
/>
<p className="text-xs text-muted-foreground truncate max-w-full px-2 pt-1.5 text-center">
{previewImage.name}
</p>
</div>
)}
</DialogContent>
</Dialog>
</div>
)
}
@@ -0,0 +1,155 @@
// @vitest-environment happy-dom
import React from 'react'
import { cleanup, render, fireEvent } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import type { CommentInfo } from '@shumai/dtos'
import { ChatInput } from './message-input'
function createWrapper() {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
})
return ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)
}
describe('ChatInput auto-mention on reply', () => {
afterEach(() => {
cleanup()
vi.clearAllMocks()
})
it('automatically inserts creator mention when replyingTo is set', () => {
const mockComment: CommentInfo = {
id: 'comment-1',
assetId: 'asset-1',
message: 'Initial feedback',
annotations: null,
second: null,
creator: { id: 'user-alice', name: 'Alice Smith' },
replies: [],
attachments: [],
mentions: [],
createdAt: '2026-08-20T10:30:00.000Z',
updatedAt: '2026-08-20T10:30:00.000Z',
sessionId: null,
isCompleted: false,
completionLastChangedBy: null,
}
const onSendMessage = vi.fn()
const { rerender } = render(
<ChatInput projectId="proj-1" onSendMessage={onSendMessage} replyingTo={null} />,
{ wrapper: createWrapper() },
)
// Set replyingTo
rerender(
<ChatInput projectId="proj-1" onSendMessage={onSendMessage} replyingTo={mockComment} />,
)
// Check that mention node is inserted in contentEditable
const mentionSpan = document.querySelector('span[data-type="mention"]')
expect(mentionSpan).not.toBeNull()
expect(mentionSpan?.getAttribute('data-id')).toBe('user-alice')
expect(mentionSpan?.textContent).toBe('@Alice Smith')
// Click send and verify serialized text contains <@user-alice>
const sendButton = document.querySelector('button .lucide-arrow-up')?.closest('button')
expect(sendButton).toBeTruthy()
fireEvent.click(sendButton as HTMLButtonElement)
expect(onSendMessage).toHaveBeenCalledWith('<@user-alice>', [], [], 'comment-1', undefined, [])
})
it('does not insert mention if disableMentions is true', () => {
const mockComment: CommentInfo = {
id: 'comment-1',
assetId: 'asset-1',
message: 'Initial feedback',
annotations: null,
second: null,
creator: { id: 'user-alice', name: 'Alice Smith' },
replies: [],
attachments: [],
mentions: [],
createdAt: '2026-08-20T10:30:00.000Z',
updatedAt: '2026-08-20T10:30:00.000Z',
sessionId: null,
isCompleted: false,
completionLastChangedBy: null,
}
const onSendMessage = vi.fn()
render(
<ChatInput
projectId="proj-1"
onSendMessage={onSendMessage}
replyingTo={mockComment}
disableMentions={true}
/>,
{ wrapper: createWrapper() },
)
const mentionSpan = document.querySelector('span[data-type="mention"]')
expect(mentionSpan).toBeNull()
})
it('updates mention when replyingTo switches to a different comment author', () => {
const commentAlice: CommentInfo = {
id: 'comment-1',
assetId: 'asset-1',
message: 'Alice note',
annotations: null,
second: null,
creator: { id: 'user-alice', name: 'Alice Smith' },
replies: [],
attachments: [],
mentions: [],
createdAt: '2026-08-20T10:30:00.000Z',
updatedAt: '2026-08-20T10:30:00.000Z',
sessionId: null,
isCompleted: false,
completionLastChangedBy: null,
}
const commentBob: CommentInfo = {
id: 'comment-2',
assetId: 'asset-1',
message: 'Bob note',
annotations: null,
second: null,
creator: { id: 'user-bob', name: 'Bob Jones' },
replies: [],
attachments: [],
mentions: [],
createdAt: '2026-08-20T10:31:00.000Z',
updatedAt: '2026-08-20T10:31:00.000Z',
sessionId: null,
isCompleted: false,
completionLastChangedBy: null,
}
const onSendMessage = vi.fn()
const { rerender } = render(
<ChatInput projectId="proj-1" onSendMessage={onSendMessage} replyingTo={commentAlice} />,
{ wrapper: createWrapper() },
)
let mentionSpans = document.querySelectorAll('span[data-type="mention"]')
expect(mentionSpans).toHaveLength(1)
expect(mentionSpans[0].getAttribute('data-id')).toBe('user-alice')
// Switch to Bob's comment
rerender(<ChatInput projectId="proj-1" onSendMessage={onSendMessage} replyingTo={commentBob} />)
mentionSpans = document.querySelectorAll('span[data-type="mention"]')
expect(mentionSpans).toHaveLength(2)
expect(mentionSpans[1].getAttribute('data-id')).toBe('user-bob')
})
})
+410 -92
View File
@@ -19,6 +19,7 @@ import {
Paperclip,
Redo,
Square,
Trash2,
Undo,
XIcon,
} from 'lucide-react'
@@ -27,10 +28,12 @@ import { Button } from '../ui/button'
import { DrawAnnotation } from '../ui/icons'
import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover'
import { ProgressCircle } from '../ui/progress-circle'
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '../ui/tooltip'
import { formatTimecode } from '../viewers/video/utils'
import { useUiStore } from '@/ui/stores/ui'
import { isImageFileName } from '@/ui/lib/media'
type UploadingFile = {
export type UploadingFile = {
id: string // A unique ID for the file, e.g., timestamp + name
file: File
progress: number
@@ -38,7 +41,16 @@ type UploadingFile = {
error?: string
}
interface ChatInputProps {
export interface AttachedFileMeta {
id: string
name: string
type: string
url?: string
mediaType?: string
mimeType?: string
}
export interface ChatInputProps {
projectId: string
onSendMessage: (
text: string,
@@ -46,16 +58,51 @@ interface ChatInputProps {
annotations?: Annotation[],
replyToId?: string | null,
second?: number | null,
attachedFilesMeta?: AttachedFileMeta[],
) => void
replyingTo?: CommentInfo | null
onCancelReply?: () => void
placeholder?: string
initialText?: string
hideAnnotationControl?: boolean
value?: string
onChangeText?: (text: string) => void
disabled?: boolean
// Mentions
allowMentions?: boolean
disableMentions?: boolean
// Local file attachments
allowAttachments?: boolean
// Referenced workspace assets
workspaceAssets?: Array<{ id: string; name: string; type: string }>
onRemoveWorkspaceAsset?: (id: string) => void
// Markup / Annotations
allowMarkup?: boolean
hideAnnotationControl?: boolean
canMarkup?: boolean
markupDisabledTooltip?: string
// Timestamps
allowTimestamp?: boolean
currentTime?: number
frameRate?: number
startTimecode?: string
formatTimestamp?: (second: number) => string
// Comment Thread Reply
replyingTo?: CommentInfo | null
onCancelReply?: () => void
// Bottom action bar extras (e.g. Agent Selector)
bottomLeftExtra?: React.ReactNode
// Streaming & Abort
isStreaming?: boolean
onAbort?: () => void
// Callbacks
onTyping?: () => void
}
@@ -80,13 +127,28 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
onSendMessage,
replyingTo,
onCancelReply,
placeholder,
initialText = '',
hideAnnotationControl = false,
value,
onChangeText,
disabled = false,
allowMentions,
disableMentions = false,
allowAttachments = true,
workspaceAssets,
onRemoveWorkspaceAsset,
allowMarkup,
hideAnnotationControl = false,
canMarkup = true,
markupDisabledTooltip,
allowTimestamp = true,
currentTime,
frameRate,
startTimecode,
formatTimestamp,
bottomLeftExtra,
isStreaming = false,
onAbort,
onTyping,
},
ref,
@@ -95,6 +157,10 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
const [viewingFile, setViewingFile] = useState<File | null>(null)
const [isTimestampEnabled, setIsTimestampEnabled] = useState(true)
const effectiveAllowMentions = allowMentions ?? !disableMentions
const effectiveAllowMarkup = allowMarkup ?? !hideAnnotationControl
const effectiveAllowTimestamp = allowTimestamp
const { videoTimeDisplayMode } = useUiStore()
const displayTime = React.useMemo(() => {
if (currentTime === undefined) return ''
@@ -145,6 +211,7 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
const paddingLeftClass = React.useMemo(() => {
if (
!isTimestampEnabled ||
!effectiveAllowTimestamp ||
currentTime === undefined ||
(!formatTimestamp && !frameRate) ||
hasContent
@@ -155,12 +222,21 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
return 'pl-[120px]'
}
return 'pl-[85px]'
}, [isTimestampEnabled, currentTime, frameRate, hasContent, videoTimeDisplayMode])
}, [
isTimestampEnabled,
effectiveAllowTimestamp,
currentTime,
frameRate,
hasContent,
videoTimeDisplayMode,
formatTimestamp,
])
const editorRef = useRef<HTMLDivElement>(null)
const fileInputRef = useRef<HTMLInputElement>(null)
const mentionRangeRef = useRef<Range | null>(null)
const mentionListRef = useRef<HTMLDivElement>(null)
const isInternalUpdateRef = useRef(false)
const { mutateAsync: uploadAttachment } = useMutation({
mutationFn: async ({
@@ -180,7 +256,16 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
})
useEffect(() => {
if (initialText && editorRef.current) {
if (value !== undefined && editorRef.current) {
if (isInternalUpdateRef.current) {
isInternalUpdateRef.current = false
return
}
if (editorRef.current.innerText !== value) {
editorRef.current.innerText = value
setHasContent(!!value.trim())
}
} else if (initialText && editorRef.current) {
editorRef.current.innerText = initialText
setHasContent(!!initialText.trim())
// Focus and move cursor to end
@@ -192,11 +277,66 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
sel?.removeAllRanges()
sel?.addRange(range)
}
}, [initialText])
}, [value, initialText])
// Focus editor when replyingTo becomes truthy
const prevReplyingToIdRef = useRef<string | null>(null)
const insertMention = useCallback(
(user: { id: string; name: string }) => {
if (!editorRef.current) return
const mentionNode = document.createElement('span')
mentionNode.contentEditable = 'false'
mentionNode.dataset.type = 'mention'
mentionNode.dataset.id = user.id
mentionNode.className =
'inline-block bg-blue-100 text-blue-700 dark:bg-blue-900/40 dark:text-blue-300 px-1.5 py-0.5 rounded-md text-sm font-medium align-middle select-none mx-0.5'
mentionNode.innerText = `@${user.name}`
const spaceNode = document.createTextNode('\u00A0')
editorRef.current.appendChild(mentionNode)
editorRef.current.appendChild(spaceNode)
// Move cursor after the space
const newRange = document.createRange()
const sel = window.getSelection()
newRange.setStartAfter(spaceNode)
newRange.collapse(true)
sel?.removeAllRanges()
sel?.addRange(newRange)
setHasContent(true)
const currentText = editorRef.current.innerText || ''
isInternalUpdateRef.current = true
onChangeText?.(currentText)
onTyping?.()
editorRef.current.focus()
},
[onChangeText, onTyping],
)
// Auto mention creator and focus editor when replyingTo is set or changed
useEffect(() => {
if (replyingTo && editorRef.current) {
if (replyingTo.id !== prevReplyingToIdRef.current) {
prevReplyingToIdRef.current = replyingTo.id
if (effectiveAllowMentions && replyingTo.creator?.id && replyingTo.creator?.name) {
const existingMentions = editorRef.current.querySelectorAll('span[data-type="mention"]')
const alreadyMentioned = Array.from(existingMentions).some(
(node) => (node as HTMLElement).dataset.id === replyingTo.creator.id,
)
if (!alreadyMentioned) {
insertMention({
id: replyingTo.creator.id,
name: replyingTo.creator.name,
})
return
}
}
}
editorRef.current.focus()
// Focus and move cursor to end
const range = document.createRange()
@@ -205,8 +345,10 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
range.collapse(false)
sel?.removeAllRanges()
sel?.addRange(range)
} else if (!replyingTo) {
prevReplyingToIdRef.current = null
}
}, [replyingTo])
}, [replyingTo, effectiveAllowMentions, insertMention])
// Clean up drawing mode on unmount
useEffect(() => {
@@ -254,10 +396,13 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
const handleInput = () => {
if (!editorRef.current) return
setHasContent(!!editorRef.current.innerText.trim())
const text = editorRef.current.innerText || ''
setHasContent(!!text.trim())
isInternalUpdateRef.current = true
onChangeText?.(text)
onTyping?.()
if (disableMentions) {
if (!effectiveAllowMentions) {
setShowMentionList(false)
mentionRangeRef.current = null
return
@@ -337,13 +482,63 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
setShowMentionList(false)
mentionRangeRef.current = null
setHasContent(true)
const currentText = editorRef.current.innerText || ''
isInternalUpdateRef.current = true
onChangeText?.(currentText)
onTyping?.()
editorRef.current.focus()
}
const handlePaste = (e: React.ClipboardEvent<HTMLDivElement>) => {
e.preventDefault()
const text = e.clipboardData.getData('text/plain')
if (!text) return
let selection = window.getSelection()
if (
!selection ||
selection.rangeCount === 0 ||
!editorRef.current?.contains(selection.anchorNode)
) {
if (editorRef.current) {
editorRef.current.focus()
const range = document.createRange()
range.selectNodeContents(editorRef.current)
range.collapse(false)
selection = window.getSelection()
selection?.removeAllRanges()
selection?.addRange(range)
}
}
const success =
typeof document.execCommand === 'function' &&
document.queryCommandSupported?.('insertText') &&
document.execCommand('insertText', false, text)
if (!success) {
const sel = window.getSelection()
if (sel && sel.rangeCount > 0) {
const range = sel.getRangeAt(0)
range.deleteContents()
const textNode = document.createTextNode(text)
range.insertNode(textNode)
range.setStartAfter(textNode)
range.collapse(true)
sel.removeAllRanges()
sel.addRange(range)
} else if (editorRef.current) {
editorRef.current.innerText += text
}
}
handleInput()
}
const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
if (e.nativeEvent.isComposing) return
if (showMentionList && filteredEntities.length > 0) {
if (showMentionList && effectiveAllowMentions && filteredEntities.length > 0) {
if (e.key === 'ArrowUp') {
e.preventDefault()
setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredEntities.length - 1))
@@ -444,6 +639,18 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
.map((f) => f.attachmentId)
.filter((id): id is string => !!id)
const successfulAttachedFilesMeta: AttachedFileMeta[] = uploadingFiles
.filter((f) => !f.error && !!f.attachmentId)
.map((f) => ({
id: f.attachmentId!,
name: f.file.name,
type: f.file.type,
url:
f.file.type.startsWith('image/') || isImageFileName(f.file.name)
? URL.createObjectURL(f.file)
: undefined,
}))
// Serialize content
let processedText = ''
editorRef.current.childNodes.forEach((node) => {
@@ -466,20 +673,31 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
processedText = processedText.trim()
// Allow sending if annotations exist, even if text is empty
if (!processedText && successfulAttachmentIds.length === 0 && annotations.length === 0) return
const hasWorkspaceAssets = (workspaceAssets?.length ?? 0) > 0
// Allow sending if annotations, attachments, or workspace assets exist, even if text is empty
if (
!processedText &&
successfulAttachmentIds.length === 0 &&
annotations.length === 0 &&
!hasWorkspaceAssets
)
return
onSendMessage(
processedText,
successfulAttachmentIds,
annotations,
replyingTo?.id,
isTimestampEnabled ? currentTime : undefined,
effectiveAllowTimestamp && isTimestampEnabled ? currentTime : undefined,
successfulAttachedFilesMeta,
)
// Reset editor
editorRef.current.innerHTML = ''
setHasContent(false)
isInternalUpdateRef.current = true
onChangeText?.('')
setUploadingFiles([])
setViewingFile(null)
setIsDrawing(false)
@@ -490,13 +708,18 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
const isUploading = uploadingFiles.some((f) => !f.attachmentId && !f.error)
const hasSuccessfulUploads = uploadingFiles.some((f) => !!f.attachmentId)
const canSend = hasContent || hasSuccessfulUploads || annotations.length > 0
const showUpperPart = uploadingFiles.length > 0 || !!replyingTo
const hasWorkspaceAssets = (workspaceAssets?.length ?? 0) > 0
const effectiveDisabled = disabled || isStreaming
const canSend =
(hasContent || hasSuccessfulUploads || annotations.length > 0 || hasWorkspaceAssets) &&
!effectiveDisabled
const showUpperPart =
(workspaceAssets && workspaceAssets.length > 0) || uploadingFiles.length > 0 || !!replyingTo
return (
<div
ref={ref}
className="relative flex flex-col w-full border border-foreground/35 rounded-3xl shadow-sm transition-all duration-200"
className="relative flex flex-col w-full border border-foreground/35 rounded-3xl shadow-sm transition-all duration-200 bg-background"
>
<style>{`
[contenteditable]:empty:before {
@@ -506,8 +729,12 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
}
`}</style>
{showMentionList &&
effectiveAllowMentions &&
(membersLoading || filteredAgents.length > 0 || filteredHumans.length > 0) && (
<div className="absolute bottom-full left-4 mb-2 w-64 rounded-xl shadow-lg border border-border overflow-hidden z-20 bg-popover text-popover-foreground animate-in fade-in slide-in-from-bottom-2 duration-200">
<div
onMouseDown={(e) => e.preventDefault()}
className="absolute bottom-full left-4 mb-2 w-64 rounded-xl shadow-lg border border-border overflow-hidden z-20 bg-popover text-popover-foreground animate-in fade-in slide-in-from-bottom-2 duration-200"
>
<div ref={mentionListRef} className="h-64 overflow-y-auto">
{membersLoading ? (
<div className="p-2 space-y-4 animate-pulse">
@@ -545,12 +772,15 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
{filteredAgents.length > 0 && (
<>
<button
type="button"
onMouseDown={(e) => e.preventDefault()}
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
setCollapsedSections((prev) => ({ ...prev, bots: !prev.bots }))
editorRef.current?.focus()
}}
className="w-full text-xs font-semibold text-muted-foreground px-3 py-2 bg-muted/50 border-b border-border flex items-center justify-between hover:bg-muted transition-colors"
className="w-full text-xs font-semibold text-muted-foreground px-3 py-2 bg-muted/50 border-b border-border flex items-center justify-between hover:bg-muted transition-colors cursor-pointer select-none"
>
<span>Agents</span>
<ChevronDown
@@ -562,11 +792,13 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
const actualIndex = index
return (
<button
type="button"
key={item.data.id}
data-index={actualIndex}
onMouseDown={(e) => e.preventDefault()}
onClick={() => handleSelectEntity(item)}
onMouseEnter={() => setHighlightedIndex(actualIndex)}
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors text-left ${
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors text-left cursor-pointer ${
actualIndex === highlightedIndex
? 'bg-accent text-accent-foreground'
: 'hover:bg-accent/50'
@@ -594,12 +826,15 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
{filteredHumans.length > 0 && (
<>
<button
type="button"
onMouseDown={(e) => e.preventDefault()}
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
setCollapsedSections((prev) => ({ ...prev, users: !prev.users }))
editorRef.current?.focus()
}}
className="w-full text-xs font-semibold text-muted-foreground px-3 py-2 bg-muted/50 border-b border-border flex items-center justify-between hover:bg-muted transition-colors"
className="w-full text-xs font-semibold text-muted-foreground px-3 py-2 bg-muted/50 border-b border-border flex items-center justify-between hover:bg-muted transition-colors cursor-pointer select-none"
>
<span>Members</span>
<ChevronDown
@@ -608,15 +843,17 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
</button>
{!collapsedSections.users &&
filteredHumans.map((item, index) => {
const actualIndex =
(collapsedSections.bots ? 0 : filteredAgents.length) + index
const agentOffset = collapsedSections.bots ? 0 : filteredAgents.length
const actualIndex = index + agentOffset
return (
<button
type="button"
key={item.data.id}
data-index={actualIndex}
onMouseDown={(e) => e.preventDefault()}
onClick={() => handleSelectEntity(item)}
onMouseEnter={() => setHighlightedIndex(actualIndex)}
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors text-left ${
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors text-left cursor-pointer ${
actualIndex === highlightedIndex
? 'bg-accent text-accent-foreground'
: 'hover:bg-accent/50'
@@ -630,7 +867,7 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
className="object-cover"
/>
)}
<AvatarFallback className="text-[10px] bg-blue-400 text-black font-semibold">
<AvatarFallback className="text-[10px] bg-purple-400 text-black font-semibold">
{getInitials(item.data.name)}
</AvatarFallback>
</Avatar>
@@ -648,6 +885,35 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
{showUpperPart && (
<div className="flex flex-col gap-2 p-3 pb-0 animate-in fade-in slide-in-from-bottom-2 duration-200">
{/* Referenced Workspace Assets */}
{workspaceAssets && workspaceAssets.length > 0 && (
<div className="flex flex-col gap-1.5 min-h-0 max-h-[160px] overflow-y-auto border border-border/60 rounded-lg bg-muted/20 p-2.5 divide-y divide-border/50">
{workspaceAssets.map((asset) => (
<div
key={asset.id}
className="flex items-center justify-between py-1 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>
{onRemoveWorkspaceAsset && (
<button
onClick={() => onRemoveWorkspaceAsset(asset.id)}
className="text-muted-foreground hover:text-destructive transition-colors p-0.5 cursor-pointer"
title={m.remove_from_context?.() || 'Remove from context'}
>
<Trash2 className="h-3.5 w-3.5" />
</button>
)}
</div>
))}
</div>
)}
{/* Replying banner */}
{replyingTo && (
<div className="flex items-center justify-between bg-muted p-2 px-3 rounded-lg border border-border">
<div className="text-xs text-muted-foreground truncate mr-2">
@@ -662,6 +928,7 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
</div>
)}
{/* Local file uploads thumbnail strip */}
{uploadingFiles.length > 0 && (
<div className="flex gap-3 overflow-x-auto py-2 px-1 scrollbar-hide">
{uploadingFiles.map((upload) => (
@@ -687,7 +954,7 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
</div>
<button
onClick={() => removeFile(upload.id)}
className="absolute -top-1.5 -right-1.5 bg-gray-500 text-white rounded-full p-0.5 shadow-md opacity-0 group-hover:opacity-100 transition-opacity hover:bg-red-500 z-10"
className="absolute -top-1.5 -right-1.5 bg-gray-500 text-white rounded-full p-0.5 shadow-md opacity-0 group-hover:opacity-100 transition-opacity hover:bg-red-500 z-10 cursor-pointer"
>
<XIcon className="w-3 h-3" />
</button>
@@ -705,25 +972,31 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
onClick={() => editorRef.current?.focus()}
className="w-full px-4 pt-1 pb-1 cursor-text flow-root max-h-60 overflow-y-auto"
>
{isTimestampEnabled && currentTime !== undefined && frameRate !== undefined && (
<div
onClick={(e) => {
e.stopPropagation()
editorRef.current?.focus()
}}
className="float-left select-none bg-blue-100 dark:bg-blue-900/40 text-blue-600 dark:text-blue-300 px-2.5 py-0.5 mt-3 mr-2 rounded-sm text-xs font-mono font-bold flex items-center gap-1 cursor-text"
>
{displayTime}
</div>
)}
{effectiveAllowTimestamp &&
isTimestampEnabled &&
currentTime !== undefined &&
(frameRate !== undefined || formatTimestamp !== undefined) && (
<div
onClick={(e) => {
e.stopPropagation()
editorRef.current?.focus()
}}
className="float-left select-none bg-blue-100 dark:bg-blue-900/40 text-blue-600 dark:text-blue-300 px-2.5 py-0.5 mt-3 mr-2 rounded-sm text-xs font-mono font-bold flex items-center gap-1 cursor-text"
>
{displayTime}
</div>
)}
<div
ref={editorRef}
contentEditable
contentEditable={!disabled && !isStreaming}
suppressContentEditableWarning
onInput={handleInput}
onKeyDown={handleKeyDown}
data-placeholder={replyingTo ? m.reply_placeholder() : m.message_placeholder()}
className={`bg-transparent border-none focus:ring-0 resize-none min-h-[40px] leading-relaxed py-2 focus:outline-none block ${paddingLeftClass}`}
onPaste={handlePaste}
data-placeholder={
placeholder || (replyingTo ? m.reply_placeholder() : m.message_placeholder())
}
className={`bg-transparent border-none focus:ring-0 resize-none min-h-[40px] leading-relaxed py-2 focus:outline-none block whitespace-pre-wrap break-words ${paddingLeftClass}`}
/>
</div>
@@ -818,59 +1091,104 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
</button>
</div>
) : (
<div className="flex items-center gap-1">
<input
type="file"
ref={fileInputRef}
className="hidden"
multiple
onChange={handleFileSelect}
/>
{currentTime !== undefined && frameRate !== undefined && (
<Button
onClick={() => setIsTimestampEnabled(!isTimestampEnabled)}
variant={isTimestampEnabled ? 'secondary' : 'ghost'}
size="icon"
className={`p-2 rounded-full cursor-pointer ${isTimestampEnabled ? 'text-blue-600 bg-blue-50 dark:bg-blue-900/20' : ''}`}
title={isTimestampEnabled ? m.disable_timestamp() : m.enable_timestamp()}
>
<Clock className="w-4 h-4" />
</Button>
)}
<Button
onClick={() => fileInputRef.current?.click()}
className="p-2 rounded-full cursor-pointer"
variant="ghost"
size="icon"
title={m.attach_files()}
>
<Paperclip className="w-4 h-4" />
</Button>
{!hideAnnotationControl && (
<Button
onClick={() => setIsDrawing(true)}
variant="ghost"
size="icon"
className="p-2 rounded-full cursor-pointer"
title={m.toggle_annotation()}
>
<DrawAnnotation className="w-4 h-4" />
</Button>
<div className="flex items-center gap-1.5 min-w-0 max-w-[80%]">
{bottomLeftExtra}
{allowAttachments && (
<>
<input
type="file"
ref={fileInputRef}
className="hidden"
multiple
onChange={handleFileSelect}
/>
<Button
onClick={() => fileInputRef.current?.click()}
className="p-2 rounded-full cursor-pointer shrink-0"
variant="ghost"
size="icon"
title={m.attach_files()}
>
<Paperclip className="w-4 h-4" />
</Button>
</>
)}
{effectiveAllowTimestamp &&
currentTime !== undefined &&
(frameRate !== undefined || formatTimestamp !== undefined) && (
<Button
onClick={() => setIsTimestampEnabled(!isTimestampEnabled)}
variant={isTimestampEnabled ? 'secondary' : 'ghost'}
size="icon"
className={`p-2 rounded-full cursor-pointer shrink-0 ${isTimestampEnabled ? 'text-blue-600 bg-blue-50 dark:bg-blue-900/20' : ''}`}
title={isTimestampEnabled ? m.disable_timestamp() : m.enable_timestamp()}
>
<Clock className="w-4 h-4" />
</Button>
)}
{effectiveAllowMarkup &&
(canMarkup ? (
<Button
onClick={() => {
onTyping?.()
setIsDrawing(true)
}}
variant="ghost"
size="icon"
className="p-2 rounded-full cursor-pointer shrink-0"
title={m.toggle_annotation()}
>
<DrawAnnotation className="w-4 h-4" />
</Button>
) : (
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<span className="inline-block cursor-not-allowed shrink-0">
<Button
disabled
variant="ghost"
size="icon"
className="p-2 rounded-full opacity-40 cursor-not-allowed pointer-events-none"
>
<DrawAnnotation className="w-4 h-4" />
</Button>
</span>
</TooltipTrigger>
<TooltipContent side="top">
<p className="text-xs">
{markupDisabledTooltip || m.markup_not_supported_for_this_file()}
</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
))}
</div>
)}
<button
onClick={handleSend}
disabled={!canSend || isUploading}
className={`p-2 rounded-full transition-all duration-200 flex items-center justify-center shrink-0 ${
canSend && !isUploading
? 'bg-blue-600 text-white shadow-md hover:bg-blue-700 hover:shadow-lg transform hover:-translate-y-0.5'
: 'bg-gray-400 cursor-not-allowed'
}`}
>
<ArrowUp className="w-4 h-4" />
</button>
{isStreaming ? (
<button
onClick={onAbort}
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 cursor-pointer"
title="Stop generation"
>
<div className="h-4 w-4 flex items-center justify-center">
<div className="h-3 w-3 rounded-[2px] bg-white" />
</div>
</button>
) : (
<button
onClick={handleSend}
disabled={!canSend || isUploading}
className={`p-2 rounded-full transition-all duration-200 flex items-center justify-center shrink-0 ${
canSend && !isUploading
? 'bg-blue-600 text-white shadow-md hover:bg-blue-700 hover:shadow-lg transform hover:-translate-y-0.5 cursor-pointer'
: 'bg-gray-400 cursor-not-allowed opacity-50'
}`}
>
<ArrowUp className="w-4 h-4" />
</button>
)}
</div>
{viewingFile && (
@@ -6,6 +6,7 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ChatbotSidebar } from './chatbot-sidebar'
import { useChatbotStore, AGENT_PREFERENCE_STORAGE_KEY } from '@/ui/stores/chatbot'
import { client } from '@/ui/api/client'
import type { ChatMessage } from '@shumai/dtos'
const localStorageMock = (() => {
let store: Record<string, string> = {}
@@ -190,4 +191,281 @@ describe('ChatbotSidebar - Agent Selection & Preference Persistence', () => {
expect(localStorage.getItem(AGENT_PREFERENCE_STORAGE_KEY)).toBeNull()
})
})
it('renders user messages with markup badge, timestamp, and attachments and triggers onSelectMessage on click when matched', async () => {
vi.mocked(client.api.projects[':projectId']['chat-agents'].$get).mockResolvedValue({
ok: true,
json: async () => [{ id: 'agent-1', name: 'Agent 1', type: 'chat', enabled: true }],
} as unknown as Awaited<
ReturnType<(typeof client.api.projects)[':projectId']['chat-agents']['$get']>
>)
const mockSelectMessage = vi.fn()
const mockMessage = {
id: 'msg-1',
role: 'custom',
customType: 'shumai_message',
content: 'Fix the color in this scene',
details: {
annotation: true,
annotations: [{ type: 'rectangle', x: 5, y: 10 }],
position: { type: 'time', seconds: 15 },
currentAsset: { id: 'file-123', name: 'video.mp4', type: 'file' },
attachedFiles: [
{ id: 'att-1', name: 'color-ref.png', type: 'image', mediaType: 'image/png' },
],
},
} as unknown as ChatMessage
useChatbotStore.setState({
messages: [mockMessage],
})
const { getByText } = render(
<QueryClientProvider client={queryClient}>
<ChatbotSidebar
projectId="proj-1"
contextAssetId="file-123"
onSelectMessage={mockSelectMessage}
/>
</QueryClientProvider>,
)
expect(getByText('Fix the color in this scene')).toBeTruthy()
expect(getByText('00:15')).toBeTruthy()
expect(getByText('color-ref.png')).toBeTruthy()
const msgCardInner = getByText('Fix the color in this scene').closest('.bg-primary')
expect(msgCardInner?.className).not.toContain('ring-2')
const msgCard = getByText('Fix the color in this scene').closest('div')
msgCard?.click()
expect(mockSelectMessage).toHaveBeenCalledWith(mockMessage)
})
it('renders selected message card with border-blue-500 instead of ring when selectedMessageId matches', async () => {
const mockMessage = {
id: 'msg-selected',
role: 'custom',
customType: 'shumai_message',
content: 'Selected message content',
details: {
position: { type: 'time', seconds: 10 },
currentAsset: { id: 'file-123', name: 'video.mp4', type: 'file' },
},
} as unknown as ChatMessage
useChatbotStore.setState({
messages: [mockMessage],
})
const { getByText } = render(
<QueryClientProvider client={queryClient}>
<ChatbotSidebar
projectId="proj-1"
contextAssetId="file-123"
selectedMessageId="msg-selected"
/>
</QueryClientProvider>,
)
const cardInner = getByText('Selected message content').closest('.bg-primary')
expect(cardInner?.className).toContain('border-blue-500')
expect(cardInner?.className).not.toContain('ring')
})
it('does not trigger onSelectMessage on click when currentAsset does not match', async () => {
vi.mocked(client.api.projects[':projectId']['chat-agents'].$get).mockResolvedValue({
ok: true,
json: async () => [{ id: 'agent-1', name: 'Agent 1', type: 'chat', enabled: true }],
} as unknown as Awaited<
ReturnType<(typeof client.api.projects)[':projectId']['chat-agents']['$get']>
>)
const mockSelectMessage = vi.fn()
const mockMessage = {
id: 'msg-2',
role: 'custom',
customType: 'shumai_message',
content: 'Other file comment',
details: {
annotation: true,
annotations: [{ type: 'rectangle', x: 5, y: 10 }],
position: { type: 'time', seconds: 20 },
currentAsset: { id: 'other-file-456', name: 'other.mp4', type: 'file' },
},
} as unknown as ChatMessage
useChatbotStore.setState({
messages: [mockMessage],
})
const { getByText } = render(
<QueryClientProvider client={queryClient}>
<ChatbotSidebar
projectId="proj-1"
contextAssetId="file-123"
onSelectMessage={mockSelectMessage}
/>
</QueryClientProvider>,
)
const msgCard = getByText('Other file comment').closest('div')
msgCard?.click()
expect(mockSelectMessage).not.toHaveBeenCalled()
})
it('renders full-width message card with inline badges, attachments and assets sections', async () => {
vi.mocked(client.api.projects[':projectId']['chat-agents'].$get).mockResolvedValue({
ok: true,
json: async () => [{ id: 'agent-1', name: 'Agent 1', type: 'chat', enabled: true }],
} as unknown as Awaited<
ReturnType<(typeof client.api.projects)[':projectId']['chat-agents']['$get']>
>)
const mockMessage = {
id: 'msg-full',
role: 'custom',
customType: 'shumai_message',
content: 'Review this scene carefully',
details: {
annotation: true,
annotations: [
{
type: 'box',
color: '#ff0000',
points: [
[0, 0],
[10, 10],
],
},
],
position: { type: 'time', seconds: 45 },
currentAsset: { id: 'file-123', name: 'clip.mp4', type: 'file' },
attachedFiles: [
{ id: 'att-1', name: 'reference-mood.jpg', type: 'image', mediaType: 'image/jpeg' },
],
referencedAssets: [
{ id: 'asset-ref-1', name: 'Storyboard.pdf', type: 'file' },
{ id: 'asset-ref-2', name: 'Audio Stems', type: 'folder' },
],
},
} as unknown as ChatMessage
useChatbotStore.setState({
messages: [mockMessage],
})
const { getByText } = render(
<QueryClientProvider client={queryClient}>
<ChatbotSidebar projectId="proj-1" contextAssetId="file-123" />
</QueryClientProvider>,
)
// Inline text and timestamp
expect(getByText('Review this scene carefully')).toBeTruthy()
expect(getByText('00:45')).toBeTruthy()
// Attachments section
expect(getByText('Attachments')).toBeTruthy()
expect(getByText('reference-mood.jpg')).toBeTruthy()
// Assets section
expect(getByText('Assets')).toBeTruthy()
expect(getByText('Storyboard.pdf')).toBeTruthy()
expect(getByText('Audio Stems')).toBeTruthy()
// Card should be full width (w-full)
const cardEl = getByText('Review this scene carefully').closest('.bg-primary')
expect(cardEl?.className).toContain('w-full')
})
it('asks for confirmation before deleting a session and deletes when confirmed', async () => {
const mockDeleteSession = vi.fn()
const originalDeleteSession = useChatbotStore.getState().deleteSession
useChatbotStore.setState({
isHistoryMode: true,
historySessions: [
{
id: 'sess-1',
agentId: 'agent-1',
userId: 'user-1',
assetId: 'asset-1',
userCommentId: null,
name: 'Important Chat',
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
},
],
deleteSession: mockDeleteSession,
})
const { useTeamContextStore } = await import('@/ui/stores/team-context')
useTeamContextStore.setState({ teamId: 'team-123' })
window.confirm = vi.fn()
const confirmSpy = vi.spyOn(window, 'confirm')
// 1. User cancels confirmation
confirmSpy.mockReturnValue(false)
const { getByTitle } = render(
<QueryClientProvider client={queryClient}>
<ChatbotSidebar projectId="proj-1" />
</QueryClientProvider>,
)
const deleteBtn = getByTitle('Delete Session')
deleteBtn.click()
expect(confirmSpy).toHaveBeenCalled()
expect(mockDeleteSession).not.toHaveBeenCalled()
// 2. User confirms deletion
confirmSpy.mockReturnValue(true)
deleteBtn.click()
expect(mockDeleteSession).toHaveBeenCalledWith('team-123', 'sess-1')
confirmSpy.mockRestore()
useChatbotStore.setState({ deleteSession: originalDeleteSession })
})
it('renders thinking_level_change messages properly in the chat log', () => {
const mockThinkingChangeMsg = {
id: 'think-1',
role: 'thinking_level_change',
content: 'Thinking level changed to high',
timestamp: Date.now(),
} as unknown as ChatMessage
useChatbotStore.setState({
messages: [mockThinkingChangeMsg],
})
const { getByText } = render(
<QueryClientProvider client={queryClient}>
<ChatbotSidebar projectId="proj-1" />
</QueryClientProvider>,
)
expect(getByText('Thinking level changed to high')).toBeTruthy()
})
it('disables message input when teamId is not available', async () => {
const { useTeamContextStore } = await import('@/ui/stores/team-context')
useTeamContextStore.setState({ teamId: null, projectTeamMap: {} })
useChatbotStore.setState({ selectedAgentId: 'agent-1' })
const { container } = render(
<QueryClientProvider client={queryClient}>
<ChatbotSidebar projectId="proj-1" />
</QueryClientProvider>,
)
const inputArea = container.querySelector('[contenteditable="false"]')
expect(inputArea).toBeTruthy()
expect(inputArea?.getAttribute('contenteditable')).toBe('false')
})
})
+388 -186
View File
@@ -11,31 +11,58 @@ 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 { useUiStore } from '@/ui/stores/ui'
import { useDroppable } from '@dnd-kit/react'
import type { ChatMessage } from '@shumai/dtos'
import type { AssetInfo, ChatMessage, ShumaiMessageContext } from '@shumai/dtos'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import {
ArrowLeft,
ArrowUp,
Bot,
Brain,
ChevronDown,
Download,
FileIcon,
Folder,
History,
Loader2,
Plus,
Trash2,
Wrench,
XIcon,
} from 'lucide-react'
import { useCallback, useEffect, useRef } from 'react'
import { useCallback, useEffect, useRef, useState } from 'react'
import Markdown from 'react-markdown'
import { formatTimeAgo } from '../lib/time'
import { ChatInput } from './chat/message-input'
import { DrawAnnotation } from './ui/icons'
import { formatTimecode } from './viewers/video/utils'
import { isImageFileName } from '@/ui/lib/media'
interface ChatbotSidebarProps {
projectId: string
contextAssetId?: string
file?: AssetInfo | null
currentTime?: number
frameRate?: number
startTimecode?: string
formatTimestamp?: (second: number) => string
onTyping?: () => void
onSelectMessage?: (message: ChatMessage) => void
selectedMessageId?: string | null
}
export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProps) {
export function ChatbotSidebar({
projectId,
contextAssetId,
file,
currentTime,
frameRate,
startTimecode,
formatTimestamp,
onTyping,
onSelectMessage,
selectedMessageId,
}: ChatbotSidebarProps) {
const {
isHistoryMode,
setIsHistoryMode,
@@ -58,6 +85,8 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
} = useChatbotStore()
const { teamId, ensureTeamIdForProject } = useTeamContextStore()
const { videoTimeDisplayMode } = useUiStore()
const [previewImage, setPreviewImage] = useState<{ url: string; name: string } | null>(null)
useEffect(() => {
if (projectId) {
@@ -187,21 +216,6 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
queryClient.invalidateQueries({ queryKey: ['projects'] })
}, [queryClient])
const handleSend = () => {
if (!inputText.trim() || isStreaming || !teamId) return
sendMessage(teamId, inputText, projectId, contextAssetId, handleAssetMutation)
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)) {
@@ -224,29 +238,221 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
const renderMessage = (msg: ChatMessage) => {
switch (msg.role as string) {
case 'user': {
case 'user':
case 'custom': {
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)}
if (msg.role === 'custom' && 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>
)
}
if (msg.role === 'user' || msgObj.customType === 'shumai_message') {
const details = msgObj.details as ShumaiMessageContext | undefined
const referencedAssets = details?.referencedAssets || []
const attachedFiles = details?.attachedFiles || []
const position = details?.position
const hasAnnotations =
details?.annotation || (details?.annotations && details.annotations.length > 0)
const isCurrentAssetMatch =
!details?.currentAsset?.id ||
(contextAssetId && details.currentAsset.id === contextAssetId) ||
(file?.id && details.currentAsset.id === file.id)
const isSelected = selectedMessageId === msg.id
let timeStr = ''
if (position?.type === 'time') {
if (formatTimestamp) {
timeStr = formatTimestamp(position.seconds)
} else if (frameRate) {
const frameIndex = Math.round(position.seconds * frameRate)
timeStr = formatTimecode(frameIndex, frameRate, videoTimeDisplayMode, startTimecode)
} else {
const mins = Math.floor(position.seconds / 60)
const secs = Math.floor(position.seconds % 60)
timeStr = `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
}
} else if (position?.type === 'page') {
timeStr = m.page_prefix({ page: position.page })
}
const messageText = getMessageText(msgObj.content)
const hasAttachmentsOrAssets = attachedFiles.length > 0 || referencedAssets.length > 0
const handleCardClick = () => {
if (isCurrentAssetMatch && onSelectMessage) {
onSelectMessage(msg)
}
}
return (
<div
key={msg.id}
onClick={handleCardClick}
className={cn(
'flex flex-col items-end w-full group transition-all duration-150',
isCurrentAssetMatch && (hasAnnotations || position) && 'cursor-pointer',
)}
>
<div
className={cn(
'bg-primary text-primary-foreground rounded-lg px-3 py-2 text-sm whitespace-pre-wrap shadow-xs break-words transition-all border',
hasAttachmentsOrAssets ? 'w-full' : 'max-w-[85%]',
isSelected ? 'border-blue-500 dark:border-blue-400' : 'border-transparent',
)}
>
<div className="text-sm leading-relaxed whitespace-pre-wrap break-words">
{hasAnnotations && (
<span
className="inline-flex items-center align-middle mr-1.5 p-0.5 rounded bg-primary-foreground/20 text-primary-foreground"
title={m.contains_drawing_annotations()}
>
<DrawAnnotation className="w-3.5 h-3.5" />
</span>
)}
{position && (
<span className="inline-flex items-center align-middle mr-1.5 px-1.5 py-0.5 rounded text-xs font-mono font-bold bg-primary-foreground/20 text-primary-foreground">
{timeStr}
</span>
)}
{messageText && <span>{messageText}</span>}
</div>
{attachedFiles.length > 0 && (
<div className="mt-2.5 w-full">
<div className="text-xs font-medium text-primary-foreground/70 mb-1">
{m.attachments()}
</div>
<div className="flex flex-col gap-1.5 w-full">
{attachedFiles.map((att) => {
const name = att.name || 'file'
const isImage =
isImageFileName(name) ||
att.mediaType === 'image' ||
att.mimeType?.startsWith('image/')
return (
<div
key={att.id}
className={cn(
'group/att relative flex items-center w-full max-w-full rounded-lg border border-primary-foreground/15 bg-primary-foreground/10 hover:bg-primary-foreground/15 transition-colors overflow-hidden',
isImage ? 'h-18 p-1.5 gap-2.5 cursor-pointer' : 'h-9 px-2.5 gap-2.5',
)}
onClick={(e) => {
e.stopPropagation()
if (att.url) {
if (isImage) {
setPreviewImage({ url: att.url, name })
} else {
window.open(att.url, '_blank', 'noreferrer')
}
}
}}
>
{isImage && att.url ? (
<div className="h-full aspect-square rounded-md overflow-hidden bg-black/20 shrink-0">
<img
src={att.url}
alt={name}
className="w-full h-full object-cover group-hover/att:scale-105 transition-transform duration-200"
/>
</div>
) : (
<FileIcon className="w-4 h-4 text-primary-foreground/70 shrink-0" />
)}
<div className="min-w-0 flex-1 overflow-hidden">
<p
className="text-xs font-medium text-primary-foreground truncate block w-full"
title={name}
>
{name}
</p>
</div>
{att.url && (
<a
href={att.url}
target="_blank"
rel="noreferrer"
download={name}
className="p-1 text-primary-foreground/70 hover:text-primary-foreground shrink-0 rounded-md hover:bg-primary-foreground/20 transition-colors opacity-0 group-hover/att:opacity-100"
onClick={(e) => e.stopPropagation()}
title={m.download()}
>
<Download className="w-3.5 h-3.5" />
</a>
)}
</div>
)
})}
</div>
</div>
)}
{referencedAssets.length > 0 && (
<div className="mt-2.5 w-full">
<div className="text-xs font-medium text-primary-foreground/70 mb-1">
{m.assets()}
</div>
<div className="flex flex-col gap-1.5 w-full">
{referencedAssets.map((asset) => (
<div
key={asset.id}
className="flex items-center justify-between w-full h-9 px-2.5 rounded-lg border border-primary-foreground/15 bg-primary-foreground/10 hover:bg-primary-foreground/15 transition-colors overflow-hidden"
>
<div className="flex items-center gap-2.5 min-w-0 flex-1">
{asset.type === 'folder' ? (
<Folder className="w-4 h-4 text-primary-foreground/70 shrink-0" />
) : (
<FileIcon className="w-4 h-4 text-primary-foreground/70 shrink-0" />
)}
<span
className="text-xs font-medium truncate text-primary-foreground"
title={asset.name}
>
{asset.name}
</span>
</div>
<span className="text-[10px] text-primary-foreground/60 uppercase shrink-0 font-mono ml-2">
{asset.type}
</span>
</div>
))}
</div>
</div>
)}
</div>
</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>
)
}
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
@@ -357,9 +563,7 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
}
}
})
if (renderedBlocks.length === 0) return null
let shouldShowFooter = false
if (content.length > 0) {
const lastBlock = content[content.length - 1]
@@ -372,7 +576,6 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
}
}
}
return (
<div key={msg.id} className="flex flex-col w-full space-y-1">
<div className="space-y-2 w-full">{renderedBlocks}</div>
@@ -384,7 +587,6 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
</div>
)
}
case 'toolResult': {
return (
<div
@@ -397,33 +599,8 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
</div>
)
}
case 'thinking_level_change':
case 'custom': {
case 'thinking_level_change': {
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
@@ -434,7 +611,6 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
</div>
)
}
default:
return null
}
@@ -452,92 +628,106 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
<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'}
{m.chatbot_drag_drop_hint()}
</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>
{/* Header */}
<div className="h-12 border-b border-border flex items-center justify-between px-4 flex-shrink-0 bg-background/50 backdrop-blur-sm">
<div className="flex items-center gap-2">
{isHistoryMode && (
<button
onClick={() => setIsHistoryMode(false)}
className="p-1.5 hover:bg-accent rounded-md text-muted-foreground hover:text-foreground transition-colors"
title="Close History"
className="p-1 rounded-md hover:bg-muted text-muted-foreground hover:text-foreground transition-colors cursor-pointer"
title={m.back_to_chat()}
>
<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">
)}
<Bot className="h-5 w-5 text-primary" />
<span className="font-semibold text-sm">
{isHistoryMode ? m.history() : m.shumai_agent()}
</span>
</div>
<div className="flex items-center gap-1">
{!isHistoryMode ? (
<>
<button
onClick={startNewSession}
className="p-1.5 hover:bg-accent rounded-md text-muted-foreground hover:text-foreground transition-colors"
className="p-1.5 rounded-md hover:bg-muted text-muted-foreground hover:text-foreground transition-colors cursor-pointer"
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"
className="p-1.5 rounded-md hover:bg-muted text-muted-foreground hover:text-foreground transition-colors cursor-pointer"
title={m.history()}
>
<History className="h-4 w-4" />
</button>
</div>
</>
)}
</>
) : (
<button
onClick={() => {
startNewSession()
setIsHistoryMode(false)
}}
className="flex items-center gap-1 text-xs bg-primary text-primary-foreground px-2.5 py-1 rounded-md hover:bg-primary/90 transition-colors shadow-xs cursor-pointer"
>
<Plus className="h-3.5 w-3.5" />
<span>{m.new_chat()}</span>
</button>
)}
</div>
</div>
{/* Main body view */}
{/* Body */}
{isHistoryMode ? (
<ScrollArea className="flex-1 p-4 min-h-0">
<ScrollArea className="flex-1 p-3">
{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 className="text-center text-xs text-muted-foreground py-8">
{m.no_history_sessions()}
</div>
) : (
<div className="space-y-2 pb-4">
{historySessions.map((sess) => {
const isActive = sess.id === currentSessionId
<div className="space-y-1">
{historySessions.map((session) => {
const isCurrent = session.id === currentSessionId
return (
<div
key={sess.id}
key={session.id}
onClick={() => {
if (teamId) {
loadSession(teamId, session.id)
setIsHistoryMode(false)
}
}}
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',
'group flex items-center justify-between p-2.5 rounded-lg text-xs cursor-pointer transition-all border border-transparent',
isCurrent
? 'bg-accent/80 text-accent-foreground font-medium border-border/40'
: 'hover:bg-muted/60 text-muted-foreground hover:text-foreground',
)}
>
<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 className="flex flex-col min-w-0 pr-2">
<span className="truncate text-foreground font-medium text-xs">
{session.name || m.untitled_session()}
</span>
<span className="text-[10px] text-muted-foreground">
{formatTimeAgo(session.updatedAt)}
</span>
</div>
<button
onClick={(e) => {
e.stopPropagation()
if (teamId && confirm(m.delete_session_confirm())) {
deleteSession(teamId, sess.id)
if (teamId && window.confirm(m.delete_session_confirm())) {
deleteSession(teamId, session.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"
className="opacity-0 group-hover:opacity-100 focus:opacity-100 transition-opacity text-muted-foreground hover:text-destructive p-1 rounded hover:bg-muted cursor-pointer"
title={m.delete_session()}
>
<Trash2 className="h-4 w-4" />
</button>
@@ -549,7 +739,6 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
</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">
@@ -561,10 +750,15 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
{messages.map(renderMessage)}
{isStreaming &&
messages.length > 0 &&
messages[messages.length - 1].role === 'user' && (
(messages[messages.length - 1].role === 'user' ||
(
messages[messages.length - 1] as unknown as {
customType?: string
}
).customType === 'shumai_message') && (
<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>
<span>{m.thinking_status()}</span>
</div>
)}
<div ref={messagesEndRef} />
@@ -572,62 +766,74 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
)}
</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%]">
<ChatInput
projectId={projectId}
value={inputText}
onChangeText={setInputText}
placeholder={m.type_a_message()}
disabled={!selectedAgentId || !teamId}
isStreaming={isStreaming}
onAbort={() => {
if (teamId) {
abortActiveSession(teamId)
}
}}
allowMentions={false}
allowAttachments={true}
workspaceAssets={chatAssets}
onRemoveWorkspaceAsset={removeAsset}
allowMarkup={true}
canMarkup={!!file && ['image', 'video', 'pdf'].includes(file.proxyType || '')}
markupDisabledTooltip={
!file ? m.open_file_to_add_markup() : m.markup_not_supported_for_this_file()
}
allowTimestamp={file?.proxyType === 'video' || file?.proxyType === 'pdf'}
currentTime={currentTime}
frameRate={frameRate || file?.media?.metadata?.frameRate || 30}
startTimecode={startTimecode || file?.media?.metadata?.startTimecode}
formatTimestamp={
formatTimestamp ||
(file?.proxyType === 'pdf'
? (sec: number) => m.page_prefix({ page: Math.round(sec) })
: undefined)
}
onTyping={onTyping}
onSendMessage={(
text,
attachmentIds,
annotations,
_replyToId,
second,
attachedFilesMeta,
) => {
if (!teamId) return
sendMessage(
teamId,
text,
projectId,
contextAssetId,
handleAssetMutation,
attachmentIds,
annotations,
second,
attachedFilesMeta,
)
}}
bottomLeftExtra={
<div className="flex items-center gap-1.5 min-w-0 max-w-full">
{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">
<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 cursor-pointer">
<SelectValue placeholder={m.select_agent()} />
</SelectTrigger>
<SelectContent>
{chatAgents.map((agent) => (
<SelectItem key={agent.id} value={agent.id}>
<SelectItem key={agent.id} value={agent.id} className="cursor-pointer">
<div className="flex items-center gap-2">
{agent.avatar ? (
<img
@@ -650,40 +856,36 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
</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>
</>
)}
{previewImage && (
<div
className="fixed inset-0 z-[100] bg-black/95 flex flex-col items-center justify-center p-4 animate-in fade-in duration-200"
onClick={() => setPreviewImage(null)}
>
<button className="absolute top-4 right-4 text-white/70 hover:text-white hover:bg-white/20 rounded-full p-2 transition-colors z-50 cursor-pointer">
<XIcon className="w-8 h-8" />
</button>
<div className="w-full h-full flex items-center justify-center p-2">
<img
src={previewImage.url}
alt={previewImage.name}
className="max-w-full max-h-[85vh] object-contain rounded shadow-2xl cursor-zoom-out"
onClick={(e) => {
e.stopPropagation()
setPreviewImage(null)
}}
/>
</div>
<div className="absolute bottom-6 text-white text-sm font-medium bg-black/50 px-4 py-2 rounded-full backdrop-blur-sm">
{previewImage.name}
</div>
</div>
)}
</div>
)
}
+1
View File
@@ -2,5 +2,6 @@ export interface DragState {
isActive: boolean
draggedIds: Set<string>
hasFolders: boolean
isSingleFile: boolean
itemCount: number
}
@@ -0,0 +1,83 @@
// @vitest-environment happy-dom
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import React from 'react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { FileBrowserContextMenu } from './context-menu'
import { ContextMenu, ContextMenuTrigger } from '@/ui/components/ui/context-menu'
import type { AssetInfo } from '@shumai/dtos'
describe('FileBrowserContextMenu', () => {
afterEach(() => {
cleanup()
})
const stackItem: AssetInfo = {
id: 'stack-123',
name: 'version_stack_test',
type: 'version_stack',
status: 'processed',
fileCount: 2,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
} as AssetInfo
const fileItem: AssetInfo = {
id: 'file-123',
name: 'regular_file.png',
type: 'file',
status: 'processed',
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
} as AssetInfo
const renderWithMenu = (
item: AssetInfo,
props: Partial<React.ComponentProps<typeof FileBrowserContextMenu>> = {},
) => {
return render(
<ContextMenu>
<ContextMenuTrigger data-testid="context-trigger">Trigger</ContextMenuTrigger>
<FileBrowserContextMenu
item={item}
selectedIds={new Set()}
folders={[]}
files={[item]}
onRename={vi.fn()}
onDelete={vi.fn()}
onDownload={vi.fn()}
onRestore={vi.fn()}
onNewFolder={vi.fn()}
onUploadFile={vi.fn()}
onUploadFolder={vi.fn()}
onNewVersion={vi.fn()}
onManageVersions={vi.fn()}
onMoveTo={vi.fn()}
onCopyTo={vi.fn()}
canEdit={true}
{...props}
/>
</ContextMenu>,
)
}
it('renders Manage versions for version_stack item when canEdit is true', () => {
const onManageVersions = vi.fn()
renderWithMenu(stackItem, { onManageVersions })
fireEvent.contextMenu(screen.getByTestId('context-trigger'))
const manageOption = screen.getByText(/Manage versions|管理版本/i)
expect(manageOption).toBeTruthy()
fireEvent.click(manageOption)
expect(onManageVersions).toHaveBeenCalledWith(stackItem)
})
it('does not render Manage versions for regular file item', () => {
renderWithMenu(fileItem)
fireEvent.contextMenu(screen.getByTestId('context-trigger'))
expect(screen.queryByText(/Manage versions|管理版本/i)).toBeNull()
})
})
@@ -22,6 +22,7 @@ import {
Plus,
ArrowRight,
Copy,
Layers,
} from 'lucide-react'
interface FileBrowserContextMenuProps {
@@ -36,6 +37,7 @@ interface FileBrowserContextMenuProps {
onUploadFile: () => void
onUploadFolder: () => void
onNewVersion: (item: AssetInfo) => void
onManageVersions?: (item: AssetInfo) => void
onMoveTo: (items: AssetInfo[]) => void
onCopyTo: (items: AssetInfo[]) => void
folders: AssetInfo[]
@@ -63,6 +65,7 @@ export function FileBrowserContextMenu({
onUploadFile,
onUploadFolder,
onNewVersion,
onManageVersions,
onMoveTo,
onCopyTo,
folders,
@@ -296,6 +299,13 @@ export function FileBrowserContextMenu({
</ContextMenuItem>
)}
{canEdit && item.type === 'version_stack' && (
<ContextMenuItem onSelect={() => onManageVersions?.(item)}>
<Layers className="mr-2 h-4 w-4" />
<span>{m.manage_versions()}</span>
</ContextMenuItem>
)}
{item.type === 'folder' && onOpenAgentsMd && (
<ContextMenuItem onSelect={() => onOpenAgentsMd(item)}>
<FileText className="mr-2 h-4 w-4" />

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