mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-20 01:26:36 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6d4f62d6f6 | ||
|
|
43808216e6 | ||
|
|
f66bb7af73 | ||
|
|
ae86d56a7d | ||
|
|
1cee103b1c | ||
|
|
3c3b6880e4 | ||
|
|
0b84402c83 | ||
|
|
61a61abc7c | ||
|
|
fc878775c2 | ||
|
|
7acf736300 | ||
|
|
9d50301a43 | ||
|
|
7f185ca23c | ||
|
|
94582c9001 | ||
|
|
41b73feeab | ||
|
|
96493a5493 | ||
|
|
93c593545d | ||
|
|
11758c23c0 | ||
|
|
1cd94a77c6 | ||
|
|
5ad3fdbf69 | ||
|
|
851ae49c84 | ||
|
|
82aee9a291 | ||
|
|
1b5cbf936a | ||
|
|
1daeaf51ef | ||
|
|
49ef48a74a | ||
|
|
a680fccb12 | ||
|
|
e67552d8bd | ||
|
|
8080acfb2e | ||
|
|
dbd30a815f | ||
|
|
442568de00 | ||
|
|
d15fc5c6be | ||
|
|
aa6d5ed626 | ||
|
|
283d28bed9 | ||
|
|
315cacd474 | ||
|
|
44ab1e7023 | ||
|
|
beef4e9963 | ||
|
|
990f68fb3b | ||
|
|
ee4933a4f8 | ||
|
|
c91867b8eb | ||
|
|
8608ee027a | ||
|
|
394ddc8db4 | ||
|
|
83455aeaea | ||
|
|
707c091c9b | ||
|
|
d3f5892dd2 | ||
|
|
70b6c33204 | ||
|
|
624a3a6a54 | ||
|
|
03aea2cb36 |
@@ -54,5 +54,6 @@ jobs:
|
||||
platforms: linux/amd64,linux/arm64
|
||||
context: .
|
||||
push: true
|
||||
provenance: false
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
|
||||
@@ -90,7 +90,8 @@ Add any additional context, caveats, or follow-up work.
|
||||
- `bun run format`
|
||||
- `bun run typecheck`
|
||||
- `bun run test`
|
||||
- `bun run test:e2e`
|
||||
- `bun run test:e2e:app`
|
||||
- `bun run test:e2e:webui`
|
||||
- `bun run test:e2e:workflow`
|
||||
|
||||
- **Backend Testing Mandate**: Every backend feature, service method, workflow, and activity MUST be accompanied by comprehensive tests. Logic-heavy code without corresponding test coverage is considered incomplete.
|
||||
@@ -285,7 +286,7 @@ describe('Team API', () => {
|
||||
### Web App E2E Tests
|
||||
|
||||
- Located in `apps/web/e2e/**/*.spec.ts`, organized by domain under `tests/<domain>/` (e.g. `auth`, `project`).
|
||||
- Run via `bun run test:e2e`.
|
||||
- Run via `bun run test:e2e:app`.
|
||||
- **Fixtures**: Use the `owner`, `project`, and `file` fixtures for setup — they seed data through API calls/DB (no slow UI setup). You can add more fixtures if necessary.
|
||||
- **Documentation**: Every test case MUST be documented in [`apps/web/e2e/README.md`](apps/web/e2e/README.md). Add or update the corresponding entry in the README whenever you add, rename, or remove a spec.
|
||||
|
||||
@@ -321,7 +322,8 @@ We use `prisma-json-types-generator` to enforce strict type-safety for Prisma `J
|
||||
|
||||
- Development: Use `bun --bun run prisma migrate dev` to create and apply migrations during development.
|
||||
- Production: Use `bun --bun run prisma migrate deploy` to apply pending migrations in production environments.
|
||||
- **No Manual Migration Creation**: Never create migration SQL files or directories manually by hand. Always use Prisma CLI commands (e.g. `bun --bun run prisma migrate dev --create-only` to generate a migration template, or `bun --bun run prisma migrate dev`) so Prisma correctly tracks migration metadata and checksums.
|
||||
- **No Manual DDL Creation**: Never hand-write **schema (DDL) migration** files or directories. Always generate them with Prisma CLI (`bun --bun run prisma migrate dev --create-only` to create a template, then edit it if needed, or `bun --bun run prisma migrate dev` to apply) so Prisma correctly tracks migration metadata and checksums.
|
||||
- **Data Migrations Are Allowed**: Pure **data migration** SQL (no schema change) may be written by hand, but it MUST live inside a migration generated via `bun --bun run prisma migrate dev --create-only` so it is tracked and applied automatically.
|
||||
- **No Automatic Dev DB Reset**: Do not run `prisma migrate reset --force` or commands that force-reset the database automatically. If migrations become out of sync or a reset is required, stop executing, report the situation to the user, and present suggested manual cleanup/reset steps for the user to execute.
|
||||
|
||||
### Commands
|
||||
|
||||
@@ -8,6 +8,7 @@ WORKDIR /app
|
||||
COPY package.json bun.lock ./
|
||||
COPY --parents packages/*/package.json ./
|
||||
COPY --parents apps/*/package.json ./
|
||||
COPY patches/ ./patches/
|
||||
|
||||
# Install dependencies (including devDependencies for build tools)
|
||||
RUN bun install --frozen-lockfile
|
||||
|
||||
@@ -24,12 +24,16 @@ Shumai is an open-source Frame.io alternative built for creative teams to collab
|
||||
* **Granular Access Control**: Manage workspace permissions with team-level and project-level role-based access control.
|
||||
* **Distributed Transcoding via Temporal**: Offload resource-intensive video transcoding to a background worker pool orchestrated by Temporal.
|
||||
* **Custom Asset Metadata**: Define dynamic metadata fields tailored to your production pipeline.
|
||||
* **Share Link Watermarking**: Protect shared media with customizable text and image watermarks, and save watermark as reusable templates.
|
||||
* **Wide File Format Support**: Preview common creative file formats, including images (with PSD support), videos, audio, and documents, while allowing any file type to be uploaded and stored.
|
||||
* **Office & Document PDF Proxying via Gotenberg**: Convert Word, PowerPoint, Excel, Markdown, HTML, and CSV documents to high-quality PDF proxies for in-browser previewing using [Gotenberg](https://docs.shumai.one/using-shumai/gotenberg).
|
||||
|
||||
But Shumai goes beyond being a Frame.io alternative. With modern models like GPT-Image-2 and Seedance 2.5, AI can already handle a surprising amount of creative work. That means you can create or update videos and images from a simple prompt without having to jump back into Photoshop, Premiere or Houdini. That is why Shumai includes a powerful agent system:
|
||||
|
||||
* **Agent as a Team Member**: Mention a context-aware AI agent with `@mentions` directly in asset comments, just like a real teammate.
|
||||
* **Agent as a One-to-One Assistant**: Chat privately with the agent and ask it to help with tasks.
|
||||
* **Custom Skills & Tools**: Extend the agent by registering custom scripts, tools, and automation skills.
|
||||
* **Model Context Protocol (MCP)**: Connect agents to external tools and APIs through remote MCP servers, with OAuth authentication and per-agent MCP access.
|
||||
* **Isolated Sandbox Execution**: Run agent-submitted scripts safely in a sandboxed environment.
|
||||
* **AI Metadata Autofill**: Automatically fill in custom metadata for new assets.
|
||||
* **Semantic Search**: Find assets by visual or conceptual meaning using vector embeddings (Gemini Embedding 2).
|
||||
@@ -43,7 +47,7 @@ https://github.com/user-attachments/assets/1186067a-72c0-41b9-836f-987438f2332a
|
||||
|
||||
|
||||
> [!NOTE]
|
||||
> **Media Processing & Storage:** When uploading media, Shumai always preserves and stores your original raw file. Note that the file size shown in the system represents the original file size and does not include the size of any transcoded files. For all media types (both images and videos), Shumai creates a 480p WebP poster image to serve as the preview in the file list view. Additionally:
|
||||
> **Media Processing & Storage:** When uploading media, Shumai always preserves and stores your original raw file. Note that the file size shown in the system represents the original file size and does not include the size of any transcoded files. For all media types (both images and videos), Shumai creates a 300p WebP poster image to serve as the preview in the file list view. Additionally:
|
||||
> * **For Images:** Shumai generates a high-quality WebP proxy at the exact same resolution as your original file for optimized viewing.
|
||||
> * **For Videos:** It generates at least one proxy video (depending on your transcoding settings), alongside a sprite image and a small preview video (capped at 1600 frames) to enable fast timeline scrubbing and lightning-quick previews.
|
||||
|
||||
@@ -242,11 +246,14 @@ BETTER_AUTH_SECRET=ySxs7DxzHDZBbeeHNPEwBuspYwipBqz5Gk5XdBjNhWw=
|
||||
STORAGE_BACKEND=local
|
||||
SHUMAI_SERVER_PORT=3000
|
||||
AWS_ENDPOINT_URL_S3=http://localhost:3000
|
||||
# Optional: base URL for MCP OAuth redirects (defaults to BETTER_AUTH_URL)
|
||||
MCP_OAUTH_REDIRECT_BASE_URL=http://localhost:3000
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> `SHUMAI_SERVER_PORT` sets the port the server starts on, while `AWS_ENDPOINT_URL_S3` is used by the browser to build file upload URLs.
|
||||
> If deploying on a remote server (e.g. `http://123.456.7.8`) with a mapped port (e.g. `12345:3000` in docker-compose), set `AWS_ENDPOINT_URL_S3` to `http://123.456.7.8:12345`.
|
||||
> `MCP_OAUTH_REDIRECT_BASE_URL` is only needed when MCP servers use OAuth: the authorization redirect URI defaults to `{BETTER_AUTH_URL}/api/mcp/oauth/callback`; set it to an externally reachable URL if the app is served behind a proxy.
|
||||
|
||||
#### Step 6: Run Shumai
|
||||
|
||||
|
||||
+5
-1
@@ -25,12 +25,16 @@ Shumai 是一个开源的 Frame.io 替代方案,专为创意团队打造,让
|
||||
* **细粒度权限控制**:支持按团队、项目设置角色权限,方便管理不同成员的访问范围。
|
||||
* **基于 Temporal 的分布式转码**:把吃资源的视频转码任务交给后台 worker 池处理,由 Temporal 统一调度。
|
||||
* **自定义素材元数据**:可以按你的制作流程,自由定义动态元数据字段。
|
||||
* **分享链接水印**:通过可自定义的文字和图片水印保护共享媒体,并将水印配置保存为模板以便重复使用。
|
||||
* **丰富的文件格式支持**:支持预览常见创意文件格式,包括图片(支持 PSD)、视频、音频和文档,同时可上传和存储任意类型的文件。
|
||||
* **基于 Gotenberg 的文档 PDF 转码代理**:通过集成 [Gotenberg](https://docs.shumai.one/using-shumai/gotenberg) 支持将 Word、PowerPoint、Excel、Markdown、HTML 和 CSV 文档高质量转换为 PDF 代理文件进行在线预览。
|
||||
|
||||
但 Shumai 不仅仅只满足于 Frame.io 的开源替代。随着 GPT-Image-2、Seedance 2.5 这类强大模型的问世,AI已经生成产品级的图片/视频。很多时候,你只要给一句提示词,就能直接生成或修改图片和视频,而不用再打开 Photoshop、Premiere 或者 Houdini。也正因为这样,Shumai 才把 AI Agent 做成了核心能力:
|
||||
|
||||
* **Agent 就像团队成员**:你可以在素材评论区里直接用 `@` 提到 AI Agent,像拉一个真实同事进来一起看稿一样。
|
||||
* **Agent 也能当你的私聊助手**:你可以单独和它聊天,让它帮你查东西、做任务。
|
||||
* **自定义技能和工具**:你可以给 Agent 接上自己的脚本、工具和自动化能力。
|
||||
* **Model Context Protocol (MCP) 支持**:通过远程 MCP 服务端为 Agent 接入外部工具与 API,支持 OAuth 鉴权与按 Agent 独立分配访问权限。
|
||||
* **沙箱隔离执行**:Agent 生成的脚本会在隔离环境里运行,更安全。
|
||||
* **AI 自动补全元数据**:新素材上传后,系统可以自动帮你补一些元数据。
|
||||
* **语义搜索**:支持按画面内容或语义找素材,底层用的是向量 embedding(Gemini Embedding 2)。
|
||||
@@ -40,7 +44,7 @@ Shumai 是一个开源的 Frame.io 替代方案,专为创意团队打造,让
|
||||
https://github.com/user-attachments/assets/1186067a-72c0-41b9-836f-987438f2332a
|
||||
|
||||
> [!NOTE]
|
||||
> **媒体处理与存储机制:** 上传媒体文件后,Shumai 始终会保留并存储原始文件。请注意,系统中显示的文件大小为原始文件的大小,不包含转码后生成的代理文件大小。无论图片还是视频,都会额外生成一张 480p 的 WebP 封面图,作为文件列表中的预览图。此外:
|
||||
> **媒体处理与存储机制:** 上传媒体文件后,Shumai 始终会保留并存储原始文件。请注意,系统中显示的文件大小为原始文件的大小,不包含转码后生成的代理文件大小。无论图片还是视频,都会额外生成一张 300p 的 WebP 封面图,作为文件列表中的预览图。此外:
|
||||
>
|
||||
> * **图片:** 会生成一份与原图分辨率完全一致的高质量 WebP 转码版本,在保证画质的同时提升浏览性能。
|
||||
> * **视频:** 会根据转码配置生成至少一个代理视频,同时生成雪碧图(Sprite)和一段最长 1600 帧的低分辨率转码视频,用于实现快速拖动时间轴和即时视频预览。
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/agent-app",
|
||||
"version": "0.3.3",
|
||||
"version": "0.3.8",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"bin": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/cli",
|
||||
"version": "0.3.3",
|
||||
"version": "0.3.8",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"bin": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/transcode-app",
|
||||
"version": "0.3.3",
|
||||
"version": "0.3.8",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"bin": {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
Fullstack end-to-end tests for the Shumai web app. They run against a real backend
|
||||
(pgvector container + local S3 storage) served on `http://localhost:5200` and are
|
||||
executed with Playwright via `bun run test:e2e`.
|
||||
executed with Playwright via `bun run test:e2e:app`.
|
||||
|
||||
## Structure
|
||||
|
||||
@@ -68,7 +68,7 @@ apps/web/e2e/
|
||||
| Test | File |
|
||||
|---|---|
|
||||
| Owner creates a project with a cover image | `tests/project/create-project.spec.ts` |
|
||||
| Owner uploads a file via the "Upload File" context menu action | `tests/project/upload-file.spec.ts` |
|
||||
| Owner uploads a file or an unsupported file via the "Upload File" context menu action | `tests/project/upload-file.spec.ts` |
|
||||
| Owner deletes a file, finds it in recently deleted, and restores it | `tests/project/delete-restore.spec.ts` |
|
||||
| Owner creates a share link from a file context menu and lands on the share page | `tests/project/create-share-link.spec.ts` |
|
||||
| Owner protects a share with a password; guests must enter it to view | `tests/project/share-password.spec.ts` |
|
||||
@@ -80,10 +80,16 @@ apps/web/e2e/
|
||||
| Owner drags a file into a folder and a folder into another folder | `tests/project/move-folder-file.spec.ts` |
|
||||
| Owner drags a file onto a file to create a version stack | `tests/project/version-stack-create.spec.ts` |
|
||||
| Owner drags a file onto a version stack to add a new version | `tests/project/version-stack-add-version.spec.ts` |
|
||||
| Owner manages version stack: removes version from 3-version stack | `tests/project/version-stack-manage.spec.ts` |
|
||||
| Owner removes version from 2-version stack which dissolves into standalone files | `tests/project/version-stack-manage.spec.ts` |
|
||||
| Dissolving a shared version stack renders the remaining standalone file name on the share page | `tests/project/version-stack-manage.spec.ts` |
|
||||
| Moving a shared file into a shared version stack leaves only 1 stack card on the share page | `tests/project/version-stack-manage.spec.ts` |
|
||||
| Owner searches the file list by an English name keyword | `tests/project/search-file-list.spec.ts` |
|
||||
| Owner searches the file list by Chinese, Japanese, and Korean name keywords | `tests/project/search-file-list.spec.ts` |
|
||||
| Owner searches the file list with a keyword and a filter condition | `tests/project/search-file-list.spec.ts` |
|
||||
| Owner saves a search result as a collection | `tests/project/save-search-collection.spec.ts` |
|
||||
| Image share with watermark + download disabled: no download buttons on public list/detail pages, watermarked proxy served, download-url rejected | `tests/project/share-watermark-no-download.spec.ts` |
|
||||
| Video share with watermark + download disabled: no download buttons on public list/detail pages, watermarked proxy served, download-url rejected | `tests/project/share-watermark-no-download.spec.ts` |
|
||||
|
||||
### file
|
||||
| Test | File |
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { APIRequestContext, Locator, Page } from '@playwright/test'
|
||||
interface CreateShareResponse {
|
||||
id?: string
|
||||
name?: string
|
||||
rootFolderId?: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -23,7 +24,7 @@ export async function apiCreateShare(
|
||||
request: APIRequestContext,
|
||||
projectId: string,
|
||||
name: string,
|
||||
): Promise<{ id: string; name: string }> {
|
||||
): Promise<{ id: string; name: string; rootFolderId: string }> {
|
||||
const res = await request.post(`/api/projects/${projectId}/shares`, {
|
||||
data: { name },
|
||||
})
|
||||
@@ -31,10 +32,10 @@ export async function apiCreateShare(
|
||||
throw new Error(`Create share API failed (${res.status()}): ${await res.text()}`)
|
||||
}
|
||||
const body = (await res.json()) as CreateShareResponse
|
||||
if (!body.id || !body.name) {
|
||||
if (!body.id || !body.name || !body.rootFolderId) {
|
||||
throw new Error(`Create share returned an unexpected response: ${JSON.stringify(body)}`)
|
||||
}
|
||||
return { id: body.id, name: body.name }
|
||||
return { id: body.id, name: body.name, rootFolderId: body.rootFolderId }
|
||||
}
|
||||
|
||||
/** Adds assets to a share link through the API. */
|
||||
@@ -51,6 +52,66 @@ export async function apiAddAssetsToShare(
|
||||
}
|
||||
}
|
||||
|
||||
/** Updates share link settings (e.g. `allowDownload`) through the API. */
|
||||
export async function apiUpdateShare(
|
||||
request: APIRequestContext,
|
||||
shareId: string,
|
||||
patch: { allowDownload?: boolean },
|
||||
): Promise<void> {
|
||||
const res = await request.put(`/api/shares/${shareId}`, { data: patch })
|
||||
if (!res.ok()) {
|
||||
throw new Error(`Update share API failed (${res.status()}): ${await res.text()}`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables a text watermark on the share link through the API. Uses the same
|
||||
* default block shape the watermark editor produces (a "CONFIDENTIAL" text
|
||||
* block), so the transcode workflow runs against a realistic config.
|
||||
*/
|
||||
export async function apiEnableShareWatermark(
|
||||
request: APIRequestContext,
|
||||
shareId: string,
|
||||
): Promise<void> {
|
||||
const res = await request.put(`/api/shares/${shareId}/watermark`, {
|
||||
data: {
|
||||
enabled: true,
|
||||
config: {
|
||||
blocks: [
|
||||
{
|
||||
id: `wm-${Date.now()}`,
|
||||
type: 'text',
|
||||
text: 'CONFIDENTIAL',
|
||||
x: 0.5,
|
||||
y: 0.5,
|
||||
// size is a fraction (0..1) of the canvas width
|
||||
size: 0.05,
|
||||
color: '#999999',
|
||||
opacity: 0.5,
|
||||
rotation: -30,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
})
|
||||
if (!res.ok()) {
|
||||
throw new Error(`Enable share watermark API failed (${res.status()}): ${await res.text()}`)
|
||||
}
|
||||
}
|
||||
|
||||
/** Requests a public download URL. Returns status + body for assertions. */
|
||||
export async function apiPublicDownloadUrl(
|
||||
request: APIRequestContext,
|
||||
shareId: string,
|
||||
fileId: string,
|
||||
key: string,
|
||||
): Promise<{ status: number; body: unknown }> {
|
||||
const res = await request.post(`/api/shares/${shareId}/files/${fileId}/download-url`, {
|
||||
data: { key },
|
||||
})
|
||||
return { status: res.status(), body: await res.json().catch(() => null) }
|
||||
}
|
||||
|
||||
/** Uploads a file asset through the official API upload task endpoints. */
|
||||
export async function apiUploadFile(
|
||||
request: APIRequestContext,
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
import { expect, test } from '../../fixtures'
|
||||
import {
|
||||
apiAddAssetsToShare,
|
||||
apiCreateShare,
|
||||
apiEnableShareWatermark,
|
||||
apiPublicDownloadUrl,
|
||||
apiUpdateShare,
|
||||
fileCard,
|
||||
} from '../../helpers/files'
|
||||
|
||||
/**
|
||||
* The watermarked + download-disabled combination is frequently used together:
|
||||
* guests can preview the content but never download it, and any previewed proxy
|
||||
* carries a watermark. This covers both watermarkable media types (image and
|
||||
* video) and asserts, per type:
|
||||
* - the public share list page exposes no download affordance,
|
||||
* - the public share file detail page exposes no download affordance,
|
||||
* - the proxy loaded in the detail page is the watermarked version, and
|
||||
* - the download-url endpoint rejects guest download attempts with 403.
|
||||
*/
|
||||
for (const mediaType of ['image', 'video'] as const) {
|
||||
// NOTE: `test.use` must be scoped per describe block — a bare `test.use`
|
||||
// inside a `for` loop leaks the last iteration's value to every test.
|
||||
test.describe(`${mediaType} share`, () => {
|
||||
test.use({ fileOptions: { mediaType } })
|
||||
|
||||
// Transcoding (original + watermark) happens through the local workflow
|
||||
// executor, so this test needs a much larger budget than the 60s default.
|
||||
test.setTimeout(300_000)
|
||||
|
||||
test('with watermark + download disabled: no download buttons, watermarked proxy', async ({
|
||||
file,
|
||||
browser,
|
||||
prisma,
|
||||
}) => {
|
||||
const { context, projectId, fileName, fileId } = file
|
||||
|
||||
// The watermark transcode re-encodes from the existing transcode slots, so
|
||||
// the original transcode must finish first.
|
||||
await expect
|
||||
.poll(
|
||||
async () => {
|
||||
const asset = await prisma.asset.findUnique({ where: { id: fileId } })
|
||||
return asset?.status
|
||||
},
|
||||
{ timeout: 60_000 },
|
||||
)
|
||||
.toBe('processed')
|
||||
|
||||
// Seed the share through the API (setup, not the flow under test)
|
||||
const share = await apiCreateShare(context.request, projectId, 'Watermarked Share')
|
||||
await apiAddAssetsToShare(context.request, share.id, [fileId])
|
||||
await apiUpdateShare(context.request, share.id, { allowDownload: false })
|
||||
await apiEnableShareWatermark(context.request, share.id)
|
||||
|
||||
// Wait for the watermark transcode to complete and the share to be ready
|
||||
await expect
|
||||
.poll(
|
||||
async () => {
|
||||
const link = await prisma.shareLink.findUnique({ where: { id: share.id } })
|
||||
return link?.watermarkStatus
|
||||
},
|
||||
{ timeout: 120_000 },
|
||||
)
|
||||
.toBe('ready')
|
||||
|
||||
const saved = await prisma.shareLink.findUnique({ where: { id: share.id } })
|
||||
expect(saved?.allowDownload).toBe(false)
|
||||
expect(saved?.watermarkConfigId).not.toBeNull()
|
||||
|
||||
// The share root contains a symlink pointing at the real asset; the public
|
||||
// detail page navigates with the symlink id (mirrors the real double-click flow).
|
||||
const symlink = await prisma.asset.findFirst({
|
||||
where: { parentId: share.rootFolderId, targetId: fileId },
|
||||
})
|
||||
expect(symlink).not.toBeNull()
|
||||
|
||||
// Original (unwatermarked) transcode keys, to prove they are never served
|
||||
const asset = await prisma.asset.findUnique({ where: { id: fileId } })
|
||||
const media = asset?.media as {
|
||||
imageTranscodes?: Array<{ key: string }>
|
||||
videoTranscodes?: Array<{ key: string }>
|
||||
original?: { key: string } | null
|
||||
} | null
|
||||
const originalKeys =
|
||||
(mediaType === 'image' ? media?.imageTranscodes : media?.videoTranscodes)?.map(
|
||||
(t) => t.key,
|
||||
) ?? []
|
||||
|
||||
const guestContext = await browser.newContext()
|
||||
const guestPage = await guestContext.newPage()
|
||||
try {
|
||||
// ---------------- Public share list page ----------------
|
||||
await guestPage.goto(`/share/${share.id}`)
|
||||
await expect(fileCard(guestPage, fileName)).toBeVisible()
|
||||
|
||||
// Card context menu: no Download item
|
||||
const card = fileCard(guestPage, fileName)
|
||||
const kebab = card.locator('button', {
|
||||
has: guestPage.locator('svg.lucide-ellipsis'),
|
||||
})
|
||||
await kebab.click()
|
||||
await expect(guestPage.locator('[data-radix-menu-content]').first()).toBeAttached()
|
||||
await expect(guestPage.getByRole('menuitem', { name: 'Download' })).toHaveCount(0)
|
||||
await guestPage.keyboard.press('Escape')
|
||||
// Wait for the Radix menu to fully unmount before interacting again:
|
||||
// while its exit animation is running its dismiss layer can intercept
|
||||
// the outside pointerdown and swallow the next click.
|
||||
await guestPage
|
||||
.waitForFunction(() => !document.querySelector('[data-radix-menu-content]'), null, {
|
||||
timeout: 10_000,
|
||||
})
|
||||
.catch(() => {})
|
||||
|
||||
// Selection bar: selecting the file must not surface a Download button.
|
||||
// Click the card checkbox (selects directly) instead of the card body:
|
||||
// the preview area (video sprite scrubber) re-renders on hover and can
|
||||
// race with a body click.
|
||||
const checkbox = card.getByRole('checkbox')
|
||||
await checkbox.click()
|
||||
await expect(guestPage.getByText(/1 file selected/)).toBeVisible()
|
||||
await expect(guestPage.getByRole('button', { name: 'Download' })).toHaveCount(0)
|
||||
|
||||
// ---------------- Public share file detail page ----------------
|
||||
// Capture every network request so we can prove the loaded proxy is the
|
||||
// watermarked one and no original transcode key is ever requested.
|
||||
const watermarkedRequests: string[] = []
|
||||
const allRequests: string[] = []
|
||||
guestPage.on('request', (req) => {
|
||||
const url = req.url()
|
||||
allRequests.push(url)
|
||||
if (url.includes('-watermark-')) watermarkedRequests.push(url)
|
||||
})
|
||||
|
||||
const detailFileId = symlink!.id
|
||||
await guestPage.goto(`/share/${share.id}/files/${detailFileId}`)
|
||||
|
||||
// The viewer (image canvas / video element) renders the proxy
|
||||
if (mediaType === 'image') {
|
||||
await expect(guestPage.locator('canvas').first()).toBeVisible({ timeout: 30_000 })
|
||||
} else {
|
||||
await expect(guestPage.locator('video').first()).toBeVisible({ timeout: 30_000 })
|
||||
}
|
||||
|
||||
// The proxy actually loaded is the watermarked version, and no original
|
||||
// (unwatermarked) transcode key was requested
|
||||
await expect.poll(() => watermarkedRequests.length, { timeout: 30_000 }).toBeGreaterThan(0)
|
||||
const leaked = allRequests.filter((url) => originalKeys.some((key) => url.includes(key)))
|
||||
expect(leaked).toEqual([])
|
||||
|
||||
// No download affordance in the viewer control bar
|
||||
await expect(guestPage.getByTitle('Download')).toHaveCount(0)
|
||||
await expect(guestPage.getByTitle('Download original image')).toHaveCount(0)
|
||||
await expect(guestPage.getByRole('button', { name: 'Download' })).toHaveCount(0)
|
||||
|
||||
// Top-nav file dropdown: no Download item (video: also no resolution submenu)
|
||||
await guestPage.getByRole('button', { name: new RegExp(fileName) }).click()
|
||||
await expect(guestPage.locator('[data-radix-menu-content]').first()).toBeAttached()
|
||||
await expect(guestPage.getByRole('menuitem', { name: 'Download' })).toHaveCount(0)
|
||||
if (mediaType === 'video') {
|
||||
await expect(
|
||||
guestPage.locator('[data-radix-menu-content]').getByText('Original', { exact: true }),
|
||||
).toHaveCount(0)
|
||||
await expect(
|
||||
guestPage.locator('[data-radix-menu-content]').getByText('MP4', { exact: true }),
|
||||
).toHaveCount(0)
|
||||
}
|
||||
|
||||
// ---------------- Backend enforcement ----------------
|
||||
// Guests cannot obtain a download URL even when they know the storage key
|
||||
const originalKey = originalKeys[0] || media?.original?.key || ''
|
||||
const download = await apiPublicDownloadUrl(
|
||||
guestContext.request,
|
||||
share.id,
|
||||
detailFileId,
|
||||
originalKey,
|
||||
)
|
||||
expect(download.status).toBe(403)
|
||||
} finally {
|
||||
await guestContext.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -47,3 +47,39 @@ test('owner uploads a file via the "Upload File" context menu action', async ({
|
||||
const projectRow = await prisma.project.findUnique({ where: { id: projectId } })
|
||||
expect(asset!.parentId).toBe(projectRow?.rootFolderId)
|
||||
})
|
||||
|
||||
test('owner uploads an unsupported file via context menu action', async ({ project, prisma }) => {
|
||||
const { page, projectId } = project
|
||||
const fileName = `e2e-upload-${Date.now()}.bin`
|
||||
const binBuffer = Buffer.from([0x00, 0x01, 0x02, 0x03])
|
||||
|
||||
await page.goto(`/projects/${projectId}`)
|
||||
await expect(page.getByText('This folder is empty')).toBeVisible()
|
||||
|
||||
await page.getByText('This folder is empty').click({ button: 'right' })
|
||||
const [fileChooser] = await Promise.all([
|
||||
page.waitForEvent('filechooser'),
|
||||
page.getByRole('menuitem', { name: 'Upload File' }).click(),
|
||||
])
|
||||
await fileChooser.setFiles({
|
||||
name: fileName,
|
||||
mimeType: 'application/octet-stream',
|
||||
buffer: binBuffer,
|
||||
})
|
||||
|
||||
const card = fileCard(page, fileName)
|
||||
await expect(card).toBeVisible()
|
||||
|
||||
await expect
|
||||
.poll(
|
||||
async () => {
|
||||
const asset = await prisma.asset.findFirst({ where: { name: fileName } })
|
||||
return asset?.status
|
||||
},
|
||||
{ timeout: 30_000 },
|
||||
)
|
||||
.toBe('processed')
|
||||
|
||||
// Verify that the file card shows the generic file icon and is not stuck in uploading state
|
||||
await expect(card.locator('svg.lucide-file')).toBeVisible({ timeout: 10_000 })
|
||||
})
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
import { expect, test } from '../../fixtures'
|
||||
import { seedFile } from '../../helpers/assets'
|
||||
import { dragCard } from '../../helpers/dnd'
|
||||
import { apiAddAssetsToShare, apiCreateShare, fileCard } from '../../helpers/files'
|
||||
|
||||
test('owner manages versions: removes a version from a 3-version stack', async ({
|
||||
project,
|
||||
prisma,
|
||||
}) => {
|
||||
const { page, projectId, rootFolderId } = project
|
||||
|
||||
const fileA = await seedFile(prisma, projectId, rootFolderId, `e2e-mng-a-${Date.now()}`)
|
||||
const fileB = await seedFile(prisma, projectId, rootFolderId, `e2e-mng-b-${Date.now()}`)
|
||||
const fileC = await seedFile(prisma, projectId, rootFolderId, `e2e-mng-c-${Date.now()}`)
|
||||
|
||||
await page.goto(`/projects/${projectId}`)
|
||||
await expect(fileCard(page, fileA.name)).toBeVisible()
|
||||
await expect(fileCard(page, fileB.name)).toBeVisible()
|
||||
await expect(fileCard(page, fileC.name)).toBeVisible()
|
||||
|
||||
// 1. Create a 3-version stack: B on A -> stack (v2), then C on stack -> stack (v3)
|
||||
await dragCard(page, fileCard(page, fileB.name), fileCard(page, fileA.name))
|
||||
await expect(page.getByText('v2', { exact: true })).toBeVisible()
|
||||
|
||||
await dragCard(page, fileCard(page, fileC.name), fileCard(page, fileB.name))
|
||||
await expect(page.getByText('v3', { exact: true })).toBeVisible()
|
||||
|
||||
const stack = await prisma.asset.findFirstOrThrow({
|
||||
where: { projectId, parentId: rootFolderId, type: 'version_stack' },
|
||||
})
|
||||
expect(stack.fileCount).toBe(3)
|
||||
|
||||
// 2. Navigate to file viewer for the stack
|
||||
await page.goto(`/projects/${projectId}/files/${stack.id}`)
|
||||
|
||||
// 3. Open breadcrumb menu and click "Manage versions..."
|
||||
// Click on the asset name / badge dropdown trigger
|
||||
await page.getByRole('button', { name: new RegExp(fileC.name) }).click()
|
||||
// Hover or click on Versions sub-menu trigger
|
||||
await page.getByRole('menuitem', { name: 'Versions', exact: true }).hover()
|
||||
// Click "Manage versions..."
|
||||
await page.getByRole('button', { name: /manage versions$/i }).click()
|
||||
|
||||
// 4. Verify Manage versions dialog is visible with all 3 versions
|
||||
const dialog = page.getByRole('dialog')
|
||||
await expect(dialog).toBeVisible()
|
||||
await expect(dialog.getByText('Manage versions')).toBeVisible()
|
||||
await expect(dialog.getByText(fileA.name)).toBeVisible()
|
||||
await expect(dialog.getByText(fileB.name)).toBeVisible()
|
||||
await expect(dialog.getByText(fileC.name)).toBeVisible()
|
||||
|
||||
// 5. Remove fileB from stack: hover its row and click Remove from stack
|
||||
const rowB = dialog.locator('div.group', { hasText: fileB.name })
|
||||
await rowB.hover()
|
||||
const moreButton = rowB.getByRole('button', { name: 'More options' })
|
||||
await moreButton.click()
|
||||
await page.getByRole('menuitem', { name: /remove from stack/i }).click()
|
||||
|
||||
// 6. Verify toast and that fileB is removed from dialog
|
||||
await expect(page.getByText(/version removed from stack/i)).toBeVisible()
|
||||
await expect(dialog.getByText(fileB.name)).not.toBeVisible()
|
||||
await expect(dialog.getByText(fileA.name)).toBeVisible()
|
||||
await expect(dialog.getByText(fileC.name)).toBeVisible()
|
||||
|
||||
// DB: stack now has 2 versions (fileA and fileC), and fileB is standalone in root folder
|
||||
await expect
|
||||
.poll(async () => {
|
||||
const s = await prisma.asset.findUnique({ where: { id: stack.id } })
|
||||
return s?.fileCount
|
||||
})
|
||||
.toBe(2)
|
||||
|
||||
const updatedFileB = await prisma.asset.findUnique({ where: { id: fileB.id } })
|
||||
expect(updatedFileB?.parentId).toBe(rootFolderId)
|
||||
})
|
||||
|
||||
test('owner removes version from 2-version stack dissolving the stack into standalone files', async ({
|
||||
project,
|
||||
prisma,
|
||||
}) => {
|
||||
const { page, projectId, rootFolderId } = project
|
||||
|
||||
const fileA = await seedFile(prisma, projectId, rootFolderId, `e2e-dis-a-${Date.now()}`)
|
||||
const fileB = await seedFile(prisma, projectId, rootFolderId, `e2e-dis-b-${Date.now()}`)
|
||||
|
||||
await page.goto(`/projects/${projectId}`)
|
||||
await dragCard(page, fileCard(page, fileB.name), fileCard(page, fileA.name))
|
||||
await expect(page.getByText('v2', { exact: true })).toBeVisible()
|
||||
|
||||
const stack = await prisma.asset.findFirstOrThrow({
|
||||
where: { projectId, parentId: rootFolderId, type: 'version_stack' },
|
||||
})
|
||||
|
||||
// Open file view
|
||||
await page.goto(`/projects/${projectId}/files/${stack.id}`)
|
||||
|
||||
// Open breadcrumb menu and click "Manage versions..."
|
||||
await page.getByRole('button', { name: new RegExp(fileB.name) }).click()
|
||||
await page.getByRole('menuitem', { name: 'Versions', exact: true }).hover()
|
||||
await page.getByRole('button', { name: /manage versions$/i }).click()
|
||||
|
||||
const dialog = page.getByRole('dialog')
|
||||
await expect(dialog).toBeVisible()
|
||||
|
||||
// Remove fileB -> stack dissolves -> only fileA remains
|
||||
const rowB = dialog.locator('div.group', { hasText: fileB.name })
|
||||
await rowB.hover()
|
||||
const moreButton = rowB.getByRole('button', { name: 'More options' })
|
||||
await moreButton.click()
|
||||
await page.getByRole('menuitem', { name: /remove from stack/i }).click()
|
||||
|
||||
// Dialog should close automatically when stack dissolves
|
||||
await expect(dialog).not.toBeVisible()
|
||||
|
||||
// DB: stack asset is deleted
|
||||
await expect
|
||||
.poll(async () => {
|
||||
const s = await prisma.asset.findUnique({ where: { id: stack.id } })
|
||||
return s === null
|
||||
})
|
||||
.toBe(true)
|
||||
|
||||
// Both fileA and fileB are now standalone files in rootFolder
|
||||
const updatedFileA = await prisma.asset.findUnique({ where: { id: fileA.id } })
|
||||
const updatedFileB = await prisma.asset.findUnique({ where: { id: fileB.id } })
|
||||
expect(updatedFileA?.parentId).toBe(rootFolderId)
|
||||
expect(updatedFileB?.parentId).toBe(rootFolderId)
|
||||
})
|
||||
|
||||
test('dissolving a shared version stack renders the remaining standalone file name on the share page', async ({
|
||||
project,
|
||||
prisma,
|
||||
}) => {
|
||||
const { page, projectId, rootFolderId } = project
|
||||
|
||||
const fileA = await seedFile(prisma, projectId, rootFolderId, `e2e-shd-a-${Date.now()}`)
|
||||
const fileB = await seedFile(prisma, projectId, rootFolderId, `e2e-shd-b-${Date.now()}`)
|
||||
|
||||
await page.goto(`/projects/${projectId}`)
|
||||
await dragCard(page, fileCard(page, fileB.name), fileCard(page, fileA.name))
|
||||
await expect(page.getByText('v2', { exact: true })).toBeVisible()
|
||||
|
||||
const stack = await prisma.asset.findFirstOrThrow({
|
||||
where: { projectId, parentId: rootFolderId, type: 'version_stack' },
|
||||
})
|
||||
|
||||
// Share the version stack
|
||||
await fileCard(page, fileB.name).click({ button: 'right' })
|
||||
await page.getByRole('menuitem', { name: 'Create Share Link' }).click()
|
||||
const toast = page.locator('[data-sonner-toast]').filter({ hasText: 'Share link created' })
|
||||
await expect(toast).toBeVisible()
|
||||
|
||||
const share = await prisma.shareLink.findFirstOrThrow({
|
||||
where: { projectId },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
})
|
||||
|
||||
// Open file viewer for the stack
|
||||
await page.goto(`/projects/${projectId}/files/${stack.id}`)
|
||||
|
||||
// Open breadcrumb menu and click "Manage versions"
|
||||
await page.getByRole('button', { name: new RegExp(fileB.name) }).click()
|
||||
await page.getByRole('menuitem', { name: 'Versions', exact: true }).hover()
|
||||
await page.getByRole('button', { name: /manage versions$/i }).click()
|
||||
|
||||
const dialog = page.getByRole('dialog')
|
||||
await expect(dialog).toBeVisible()
|
||||
|
||||
// Remove fileB -> stack dissolves -> only fileA remains
|
||||
const rowB = dialog.locator('div.group', { hasText: fileB.name })
|
||||
await rowB.hover()
|
||||
const moreButton = rowB.getByRole('button', { name: 'More options' })
|
||||
await moreButton.click()
|
||||
await page.getByRole('menuitem', { name: /remove from stack/i }).click()
|
||||
|
||||
await expect(dialog).not.toBeVisible()
|
||||
|
||||
// Navigate to public share page: verify fileA is displayed with its non-empty name
|
||||
await page.goto(`/share/${share.id}`)
|
||||
await expect(fileCard(page, fileA.name)).toBeVisible()
|
||||
await expect(fileCard(page, fileB.name)).not.toBeVisible()
|
||||
})
|
||||
|
||||
test('moving a shared file into a shared version stack leaves only 1 stack card on the share page', async ({
|
||||
project,
|
||||
prisma,
|
||||
}) => {
|
||||
const { page, projectId, rootFolderId } = project
|
||||
|
||||
const fileA = await seedFile(prisma, projectId, rootFolderId, `e2e-mvsh-a-${Date.now()}`)
|
||||
const fileB = await seedFile(prisma, projectId, rootFolderId, `e2e-mvsh-b-${Date.now()}`)
|
||||
const fileC = await seedFile(prisma, projectId, rootFolderId, `e2e-mvsh-c-${Date.now()}`)
|
||||
|
||||
await page.goto(`/projects/${projectId}`)
|
||||
|
||||
// 1. Create a version stack from file B and file A (stack has v2, displays file B)
|
||||
await dragCard(page, fileCard(page, fileB.name), fileCard(page, fileA.name))
|
||||
await expect(page.getByText('v2', { exact: true })).toBeVisible()
|
||||
|
||||
const stack = await prisma.asset.findFirstOrThrow({
|
||||
where: { projectId, parentId: rootFolderId, type: 'version_stack' },
|
||||
})
|
||||
|
||||
// 2. Create a share link and add both stack AB and file C to it
|
||||
const share = await apiCreateShare(project.context.request, projectId, `Test Share ${Date.now()}`)
|
||||
await apiAddAssetsToShare(project.context.request, share.id, [stack.id, fileC.id])
|
||||
|
||||
// Verify initial share page has both stack and file C
|
||||
await page.goto(`/share/${share.id}`)
|
||||
await expect(fileCard(page, fileB.name)).toBeVisible()
|
||||
await expect(fileCard(page, fileC.name)).toBeVisible()
|
||||
|
||||
// 3. In project page, drop file C onto stack card (displays file B)
|
||||
await page.goto(`/projects/${projectId}`)
|
||||
await dragCard(page, fileCard(page, fileC.name), fileCard(page, fileB.name))
|
||||
await expect(page.getByText('v3', { exact: true })).toBeVisible()
|
||||
|
||||
// 4. Return to public share page: now only 1 card exists (the stack showing file C with v3)
|
||||
await page.goto(`/share/${share.id}`)
|
||||
await expect(page.getByText('v3', { exact: true })).toBeVisible()
|
||||
await expect(fileCard(page, fileC.name)).toBeVisible()
|
||||
await expect(fileCard(page, fileB.name)).not.toBeVisible()
|
||||
|
||||
// DB: share rootFolder fileCount is 1 and exactly 1 symlink exists
|
||||
const symlinks = await prisma.asset.findMany({
|
||||
where: { parentId: share.rootFolderId, type: 'symlink' },
|
||||
})
|
||||
expect(symlinks).toHaveLength(1)
|
||||
expect(symlinks[0].targetId).toBe(stack.id)
|
||||
|
||||
const updatedShareRoot = await prisma.asset.findUnique({ where: { id: share.rootFolderId } })
|
||||
expect(updatedShareRoot?.fileCount).toBe(1)
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/web-app",
|
||||
"version": "0.3.3",
|
||||
"version": "0.3.8",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
},
|
||||
"apps/agent": {
|
||||
"name": "@shumai/agent-app",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"bin": {
|
||||
"shumai-agent": "./src/index.ts",
|
||||
},
|
||||
@@ -45,7 +45,7 @@
|
||||
},
|
||||
"apps/cli": {
|
||||
"name": "@shumai/cli",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"bin": {
|
||||
"shumai-cli": "./src/index.ts",
|
||||
},
|
||||
@@ -60,7 +60,7 @@
|
||||
},
|
||||
"apps/transcode": {
|
||||
"name": "@shumai/transcode-app",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"bin": {
|
||||
"shumai-transcode": "./src/index.ts",
|
||||
},
|
||||
@@ -72,7 +72,7 @@
|
||||
},
|
||||
"apps/web": {
|
||||
"name": "@shumai/web-app",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"dependencies": {
|
||||
"@shumai/agent": "workspace:*",
|
||||
"@shumai/api": "workspace:*",
|
||||
@@ -92,7 +92,7 @@
|
||||
},
|
||||
"packages/agent": {
|
||||
"name": "@shumai/agent",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sandbox-runtime": "0.0.66",
|
||||
"@earendil-works/pi-agent-core": "0.82.1",
|
||||
@@ -102,12 +102,12 @@
|
||||
"@shumai/db": "workspace:*",
|
||||
"@shumai/dtos": "workspace:*",
|
||||
"@shumai/workflow-core": "workspace:*",
|
||||
"@sinclair/typebox": "0.34.49",
|
||||
"@temporalio/activity": "1.17.2",
|
||||
"@temporalio/worker": "1.17.2",
|
||||
"@temporalio/workflow": "1.17.2",
|
||||
"adm-zip": "0.5.17",
|
||||
"jittered-fractional-indexing": "1.0.1",
|
||||
"typebox": "1.1.38",
|
||||
"ulid": "3.0.2",
|
||||
"zod": "4.4.3",
|
||||
},
|
||||
@@ -117,7 +117,7 @@
|
||||
},
|
||||
"packages/api": {
|
||||
"name": "@shumai/api",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"dependencies": {
|
||||
"@hono/zod-validator": "0.7.6",
|
||||
"@shumai/core": "workspace:*",
|
||||
@@ -131,10 +131,11 @@
|
||||
},
|
||||
"packages/core": {
|
||||
"name": "@shumai/core",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "0.82.1",
|
||||
"@earendil-works/pi-ai": "0.82.1",
|
||||
"@modelcontextprotocol/client": "2.0.0",
|
||||
"@shumai/db": "workspace:*",
|
||||
"@shumai/dtos": "workspace:*",
|
||||
"@sinclair/typebox": "0.34.49",
|
||||
@@ -151,11 +152,13 @@
|
||||
"pino": "10.3.1",
|
||||
"sharp": "0.34.5",
|
||||
"tweetnacl": "1.0.3",
|
||||
"typebox": "1.1.38",
|
||||
"ulid": "3.0.2",
|
||||
"yaml": "2.9.0",
|
||||
"zod": "4.4.3",
|
||||
},
|
||||
"devDependencies": {
|
||||
"@modelcontextprotocol/server": "2.0.0",
|
||||
"@types/adm-zip": "0.5.8",
|
||||
"@types/extract-zip": "2.0.3",
|
||||
"@types/pdfkit": "0.17.6",
|
||||
@@ -163,7 +166,7 @@
|
||||
},
|
||||
"packages/db": {
|
||||
"name": "@shumai/db",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "0.82.1",
|
||||
"@prisma/adapter-pg": "7.8.0",
|
||||
@@ -179,7 +182,7 @@
|
||||
},
|
||||
"packages/dtos": {
|
||||
"name": "@shumai/dtos",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "0.82.1",
|
||||
"zod": "4.4.3",
|
||||
@@ -187,7 +190,7 @@
|
||||
},
|
||||
"packages/e2e": {
|
||||
"name": "@shumai/e2e",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "0.82.1",
|
||||
"@google/genai": "1.52.0",
|
||||
@@ -202,11 +205,11 @@
|
||||
},
|
||||
"packages/tableprinter": {
|
||||
"name": "@shumai/tableprinter",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
},
|
||||
"packages/transcode": {
|
||||
"name": "@shumai/transcode",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"dependencies": {
|
||||
"@shumai/core": "workspace:*",
|
||||
"@shumai/db": "workspace:*",
|
||||
@@ -220,7 +223,7 @@
|
||||
},
|
||||
"packages/webui": {
|
||||
"name": "@shumai/webui",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"dependencies": {
|
||||
"@base-ui/react": "1.5.0",
|
||||
"@dnd-kit/abstract": "0.4.0",
|
||||
@@ -318,7 +321,7 @@
|
||||
},
|
||||
"packages/workflow-core": {
|
||||
"name": "@shumai/workflow-core",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.7",
|
||||
"dependencies": {
|
||||
"@shumai/core": "workspace:*",
|
||||
"@shumai/db": "workspace:*",
|
||||
@@ -330,6 +333,9 @@
|
||||
},
|
||||
},
|
||||
},
|
||||
"patchedDependencies": {
|
||||
"@earendil-works/pi-ai@0.82.1": "patches/@earendil-works%2Fpi-ai@0.82.1.patch",
|
||||
},
|
||||
"packages": {
|
||||
"@alloc/quick-lru": ["@alloc/quick-lru@5.2.0", "", {}, "sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw=="],
|
||||
|
||||
@@ -735,6 +741,12 @@
|
||||
|
||||
"@mistralai/mistralai": ["@mistralai/mistralai@2.2.6", "", { "dependencies": { "@opentelemetry/semantic-conventions": "^1.40.0", "ws": "^8.18.0", "zod": "^3.25.0 || ^4.0.0", "zod-to-json-schema": "^3.25.0" }, "peerDependencies": { "@opentelemetry/api": "^1.9.0" }, "optionalPeers": ["@opentelemetry/api"] }, "sha512-W8pX7zHxjJvMIpw8JMxeJEleapXX0Q9NPszdNzqkM3MIEoIGPObdodujj+WHteXEvGfaP/AMwlNyRfEzSY6dQQ=="],
|
||||
|
||||
"@modelcontextprotocol/client": ["@modelcontextprotocol/client@2.0.0", "", { "dependencies": { "@modelcontextprotocol/core": "2.0.0", "cross-spawn": "^7.0.5", "eventsource": "^3.0.2", "eventsource-parser": "^3.0.0", "jose": "^6.1.3", "pkce-challenge": "^5.0.0", "zod": "^4.2.0" } }, "sha512-8f1OghQ2rjzIOfqgUCP+8GiUWqRs89njoWLNqAe8kWmDePv3s1fZXseej+QXemssEuuOvLLmLO/kqM3IQHtISw=="],
|
||||
|
||||
"@modelcontextprotocol/core": ["@modelcontextprotocol/core@2.0.0", "", { "dependencies": { "zod": "^4.2.0" } }, "sha512-pJCEwGG7Lfr/+PQp9ZTwKXNeO5wzbfKL7H3MYpCorM4oFBoQrdjnBgEoqG+RjhsvS1FKrDbKux+M1HhlnGWqcA=="],
|
||||
|
||||
"@modelcontextprotocol/server": ["@modelcontextprotocol/server@2.0.0", "", { "dependencies": { "@modelcontextprotocol/core": "2.0.0", "zod": "^4.2.0" } }, "sha512-YhHWdHfpFMQfd0prsEnxKeS3Qz3ytIGmsS0sth4KDjnacIT7hxk6hXHkJ9KysxlkvTM+WZAtQbbcUhdoP4Hvtw=="],
|
||||
|
||||
"@napi-rs/canvas": ["@napi-rs/canvas@1.0.2", "", { "optionalDependencies": { "@napi-rs/canvas-android-arm64": "1.0.2", "@napi-rs/canvas-darwin-arm64": "1.0.2", "@napi-rs/canvas-darwin-x64": "1.0.2", "@napi-rs/canvas-linux-arm-gnueabihf": "1.0.2", "@napi-rs/canvas-linux-arm64-gnu": "1.0.2", "@napi-rs/canvas-linux-arm64-musl": "1.0.2", "@napi-rs/canvas-linux-riscv64-gnu": "1.0.2", "@napi-rs/canvas-linux-x64-gnu": "1.0.2", "@napi-rs/canvas-linux-x64-musl": "1.0.2", "@napi-rs/canvas-win32-arm64-msvc": "1.0.2", "@napi-rs/canvas-win32-x64-msvc": "1.0.2" } }, "sha512-EYEqlMYaCbpZDz+IgDH5xp9MTd3ui4dmGqbQYryhMLnSRxrhHKq5KQWHHKxFUcEP4Hp8/BWgvqXocX4j7iSbOQ=="],
|
||||
|
||||
"@napi-rs/canvas-android-arm64": ["@napi-rs/canvas-android-arm64@1.0.2", "", { "os": "android", "cpu": "arm64" }, "sha512-IMXKVQod0ol4vt3gmClUfXz4JAgHYESGPCUqmH3lQxBoL0K/2greJaQE1HVBVxWWFKfLc4OLZVdxg7kXVyXv+g=="],
|
||||
@@ -1761,6 +1773,10 @@
|
||||
|
||||
"events-universal": ["events-universal@1.0.1", "", { "dependencies": { "bare-events": "^2.7.0" } }, "sha512-LUd5euvbMLpwOF8m6ivPCbhQeSiYVNb8Vs0fQ8QjXo0JTkEHpz8pxdQf0gStltaPpw0Cca8b39KxvK9cfKRiAw=="],
|
||||
|
||||
"eventsource": ["eventsource@3.0.7", "", { "dependencies": { "eventsource-parser": "^3.0.1" } }, "sha512-CRT1WTyuQoD771GW56XEZFQ/ZoSfWid1alKGDYMmkt2yl8UXrVR4pspqWNEcqKvVIzg6PAltWjxcSSPrboA4iA=="],
|
||||
|
||||
"eventsource-parser": ["eventsource-parser@3.1.0", "", {}, "sha512-kJezFj9YFAMLeORyi7aCLxLbD5/qWMQnoMVlVPyHIll7lgRJCc3JVln9Vgl9nwQi0YkMnhdGTMNn7CkRRAptMg=="],
|
||||
|
||||
"expect-type": ["expect-type@1.3.0", "", {}, "sha512-knvyeauYhqjOYvQ66MznSMs83wmHrCycNEN6Ao+2AeYEfxUIkuiVxdEa1qlGEPK+We3n0THiDciYSsCcgW/DoA=="],
|
||||
|
||||
"exsolve": ["exsolve@1.0.8", "", {}, "sha512-LmDxfWXwcTArk8fUEnOfSZpHOJ6zOMUJKOtFLFqJLoKJetuQG874Uc7/Kki7zFLzYybmZhp1M7+98pfMqeX8yA=="],
|
||||
@@ -2329,6 +2345,8 @@
|
||||
|
||||
"pino-std-serializers": ["pino-std-serializers@7.1.0", "", {}, "sha512-BndPH67/JxGExRgiX1dX0w1FvZck5Wa4aal9198SrRhZjH3GxKQUKIBnYJTdj2HDN3UQAS06HlfcSbQj2OHmaw=="],
|
||||
|
||||
"pkce-challenge": ["pkce-challenge@5.0.1", "", {}, "sha512-wQ0b/W4Fr01qtpHlqSqspcj3EhBvimsdh0KlHhH8HRZnMsEa0ea2fTULOXOS9ccQr3om+GcGRk4e+isrZWV8qQ=="],
|
||||
|
||||
"pkcs7": ["pkcs7@1.0.4", "", { "dependencies": { "@babel/runtime": "^7.5.5" }, "bin": { "pkcs7": "bin/cli.js" } }, "sha512-afRERtHn54AlwaF2/+LFszyAANTCggGilmcmILUzEjvs3XgFZT+xE6+QWQcAGmu4xajy+Xtj7acLOPdx5/eXWQ=="],
|
||||
|
||||
"pkg-types": ["pkg-types@2.3.1", "", { "dependencies": { "confbox": "^0.2.4", "exsolve": "^1.0.8", "pathe": "^2.0.3" } }, "sha512-y+ichcgc2LrADuhLNAx8DFjVfgz91pRxfZdI3UDhxHvcVEZsenLO+7XaU5vOp0u/7V/wZ+plyuQxtrDlZJ+yeg=="],
|
||||
|
||||
+4
-1
@@ -34,7 +34,8 @@
|
||||
"using-shumai/notifications",
|
||||
"using-shumai/metadata",
|
||||
"using-shumai/collections",
|
||||
"using-shumai/share-links"
|
||||
"using-shumai/share-links",
|
||||
"using-shumai/gotenberg"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -42,8 +43,10 @@
|
||||
"pages": [
|
||||
"using-shumai/agent-providers",
|
||||
"using-shumai/agents",
|
||||
"using-shumai/agents-md",
|
||||
"using-shumai/agent-skills",
|
||||
"using-shumai/agent-tools",
|
||||
"using-shumai/mcp-servers",
|
||||
"using-shumai/agent-sandbox"
|
||||
]
|
||||
},
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 82 KiB |
@@ -25,7 +25,7 @@ To protect your workspace environments and sensitive data, the sandbox enforces
|
||||
|
||||
## Domain Whitelisting
|
||||
|
||||
By default, sandboxed scripts cannot make network requests to the internet. Team Owners must explicitly authorize external APIs by managing domain access:
|
||||
By default, Network Sandbox is **disabled** (allowing all outbound network requests). Team Owners can toggle Network Sandbox **ON** in Sandbox Settings to enforce strict domain whitelisting:
|
||||
|
||||
### Allowed Domains
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
---
|
||||
title: AGENTS.md Context & Instructions
|
||||
description: Provide hierarchical, folder-specific instructions and context to AI agents.
|
||||
---
|
||||
|
||||
In addition to global agent system prompts and skills, Shumai allows you to define project-level and folder-level instructions using **`AGENTS.md`**.
|
||||
|
||||
When an AI agent (such as a Chat Agent or Autofill Agent) operates within a project or folder, Shumai automatically loads the corresponding `AGENTS.md` files to provide context, operational rules, and project-specific knowledge.
|
||||
|
||||
---
|
||||
|
||||
## Hierarchical Traversal & Inheritance
|
||||
|
||||
Shumai uses a hierarchical context model for `AGENTS.md`:
|
||||
|
||||
- **Upward Traversal**: When an agent works on a file or folder, Shumai traverses the folder tree upwards from the current folder all the way to the **project root level**.
|
||||
- **Combined Context**: All `AGENTS.md` files found along this path are loaded into the agent's context in order (from the project root down to the current folder).
|
||||
- **Layered Rules**: This lets you define broad, project-wide rules and guidelines at the project root `AGENTS.md`, while adding specific rules, vocabulary, or autofill hints in nested subfolder `AGENTS.md` files.
|
||||
|
||||
---
|
||||
|
||||
## What to Include in AGENTS.md
|
||||
|
||||
You can use `AGENTS.md` to define any instructions you want the agent to follow when working in that folder or project:
|
||||
|
||||
- **Project & Folder Overview**: Describe what assets are stored in the folder and how they relate to the broader project.
|
||||
- **Agent Behavioral Rules**: Specify guardrails, tone, required formats, or things the agent should and shouldn't do when answering queries or leaving comments.
|
||||
- **Terminology & Domain Knowledge**: Define specialized vocabulary, acronyms, brand naming conventions, or project-specific jargon.
|
||||
- **Metadata & Autofill Instructions**: Guide the **Autofill Agent** on how to summarize assets, generate tags, or populate specific custom metadata fields for files in this folder.
|
||||
|
||||
---
|
||||
|
||||
## Accessing and Editing AGENTS.md
|
||||
|
||||
### Opening the Editor
|
||||
|
||||
To view or edit `AGENTS.md`:
|
||||
|
||||
1. Open any project or folder in the Shumai web application.
|
||||
2. Click the **`AGENTS.md`** button in the top navigation toolbar, or right-click any folder in the file browser and choose **AGENTS.md**.
|
||||
3. A dedicated WYSIWYG Markdown editor opens with full formatting support (headings, lists, code blocks, bold/italics, and links).
|
||||
|
||||
### Auto-Save
|
||||
|
||||
As you make changes in the editor, Shumai automatically saves your edits in the background with visual save status indicators (`Saving...`, `Saved`).
|
||||
|
||||
---
|
||||
|
||||
## Permissions & Read-Only Mode
|
||||
|
||||
- **Viewing**: All team members who have read access to the project can view `AGENTS.md`.
|
||||
- **Editing**: **Only Team Owners** are permitted to create, edit, or delete `AGENTS.md` files.
|
||||
- **Read-Only State**: Non-owner members will see a clear read-only indicator in the toolbar explaining that only team owners can edit instructions for the file.
|
||||
@@ -0,0 +1,82 @@
|
||||
---
|
||||
title: Document PDF Conversion with Gotenberg
|
||||
description: Convert Office documents, HTML, Markdown, CSV, and TXT files into high-fidelity PDF proxies for in-browser previewing.
|
||||
---
|
||||
|
||||
Office documents, HTML pages, Markdown files, spreadsheets, and plain-text files cannot be rendered natively in web browsers. Shumai uses [Gotenberg](https://gotenberg.dev), an open-source, Docker powered conversion API — to turn these files into PDF proxy assets for seamless in-browser previewing.
|
||||
|
||||
Gotenberg performs the conversion using:
|
||||
|
||||
- **LibreOffice** for Office documents, spreadsheets, and plain text.
|
||||
- **Headless Chromium** for HTML and Markdown files.
|
||||
|
||||
---
|
||||
|
||||
## Supported File Types
|
||||
|
||||
When Gotenberg is configured, Shumai automatically generates a PDF proxy for the following uploads:
|
||||
|
||||
| Category | Extensions | Converter |
|
||||
| ------------------ | ---------------------- | ----------- |
|
||||
| Word | `.doc`, `.docx` | LibreOffice |
|
||||
| Excel | `.xls`, `.xlsx` | LibreOffice |
|
||||
| PowerPoint | `.ppt`, `.pptx` | LibreOffice |
|
||||
| OpenDocument | `.odt`, `.ods`, `.odp` | LibreOffice |
|
||||
| Rich Text | `.rtf` | LibreOffice |
|
||||
| HTML | `.html`, `.htm` | Chromium |
|
||||
| Markdown | `.md`, `.markdown` | Chromium |
|
||||
| Spreadsheets / CSV | `.csv` | LibreOffice |
|
||||
| Plain text | `.txt` | LibreOffice |
|
||||
|
||||
---
|
||||
|
||||
## Configuration
|
||||
|
||||
Gotenberg support is opt-in and controlled by the following environment variables on the machine running the transcode workers:
|
||||
|
||||
| Variable | Required | Description |
|
||||
| ------------------------------- | --------------- | ---------------------------------------------------------------- |
|
||||
| `GOTENBERG_URL` | Yes (to enable) | Base URL of the Gotenberg service, e.g. `http://gotenberg:3000`. |
|
||||
| `GOTENBERG_BASIC_AUTH_USERNAME` | No | Username when Basic Authentication is enabled on Gotenberg. |
|
||||
| `GOTENBERG_BASIC_AUTH_PASSWORD` | No | Password when Basic Authentication is enabled on Gotenberg. |
|
||||
|
||||
### Docker Quickstart
|
||||
|
||||
You can start a Gotenberg container with a single command:
|
||||
|
||||
```bash
|
||||
docker run -d --name gotenberg -p 3000:3000 gotenberg/gotenberg:8
|
||||
```
|
||||
|
||||
If you wish to enable Basic Authentication:
|
||||
|
||||
```bash
|
||||
docker run -d --name gotenberg -p 3000:3000 gotenberg/gotenberg:8 gotenberg --gotenberg-basic-auth-username=admin --gotenberg-basic-auth-password=secret
|
||||
```
|
||||
|
||||
### Docker Compose Setup
|
||||
|
||||
Add a Gotenberg service to your `docker-compose.yaml`:
|
||||
|
||||
```yaml
|
||||
gotenberg:
|
||||
image: gotenberg/gotenberg:8
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- '3000:3000'
|
||||
```
|
||||
|
||||
Then point Shumai at it:
|
||||
|
||||
```bash
|
||||
GOTENBERG_URL=http://localhost:3000
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Behavior Without Gotenberg
|
||||
|
||||
If Gotenberg is not configured or unreachable, Shumai degrades gracefully:
|
||||
|
||||
- **Office documents and HTML**: the upload completes as `processed` without a PDF proxy.
|
||||
- **CSV, Markdown, and TXT**: a lightweight built-in PDF generator produces the proxy instead, with reduced fidelity.
|
||||
@@ -0,0 +1,115 @@
|
||||
---
|
||||
title: MCP Servers
|
||||
description: Connect your agents to external tools and APIs using Model Context Protocol.
|
||||
---
|
||||
|
||||
Model Context Protocol (MCP) is an open standard that enables AI models to safely interface with external tools, microservices, and APIs. In Shumai, you can register remote MCP servers using standard HTTP transports (`Streamable HTTP` or `SSE`) and assign them to your AI agents.
|
||||
|
||||
---
|
||||
|
||||
## How MCP Works in Shumai
|
||||
|
||||
Rather than overloading model context windows with dozens of complex parameter schemas, Shumai connects to MCP servers efficiently:
|
||||
|
||||
- **Automatic Discovery & Metadata Caching**: When you add an MCP server, Shumai records it in your team's settings and connects to the endpoint to discover available tools, server-reported usage instructions, and description metadata. Discovery runs when you refresh the server, complete an OAuth flow, or the first time an agent calls one of its tools. These schemas are cached in your database for fast retrieval.
|
||||
- **Context-Efficient Proxy Tool (`mcp`)**: Assigned MCP servers are multiplexed through a single unified `mcp` proxy tool injected into your Chat Agents. The agent can dynamically inspect server status, search tools, view parameter schemas, and invoke functions as needed without wasting LLM tokens.
|
||||
- **Direct Tools Option**: For high-frequency actions, you can optionally configure specific tools as **Direct Tools** to expose them directly to the agent as top-level LLM functions alongside the `mcp` proxy.
|
||||
|
||||
---
|
||||
|
||||
## Adding an MCP Server
|
||||
|
||||
To register a new MCP server in Shumai:
|
||||
|
||||
1. Navigate to **Team Settings** and click the **MCP** tab.
|
||||
2. Click **Add MCP Server**.
|
||||
3. In the dialog box, enter your **Server Endpoint URL** (e.g. `https://mcp.example.com/sse` or `http://localhost:8080/mcp`).
|
||||
4. Click **Create**.
|
||||
|
||||
> **Note:** Only the **Server Endpoint URL** is required. Shumai derives a placeholder server name from the URL immediately; the server's actual name, status, instructions, and tool inventory are discovered the first time the server is connected (via the Refresh button in the edit dialog or the first tool call by an agent).
|
||||
|
||||
### Optional Server Configuration
|
||||
|
||||
After creating a server (the create dialog only requires a URL), open its edit dialog to configure advanced settings:
|
||||
|
||||
- **Transport**: Choose between `Streamable HTTP` (default) or `Server-Sent Events (SSE)`.
|
||||
- **Auth Strategy**: Select `Auto-detect` (default), `None`, `Bearer Token`, or `OAuth 2.0`.
|
||||
- **Minimum Permission Role**: Set the minimum team role required to invoke tools on this server (`Reviewer`, `Editor`, or `Owner`).
|
||||
- **Direct Tools**: Specify a list of tool names that should be exposed directly to agents as native LLM tools.
|
||||
|
||||
---
|
||||
|
||||
## Understanding Tool Configuration
|
||||
|
||||
In the server's edit dialog, the **Tools** section lists every discovered tool with a three-state toggle (plus **All Off** / **All Proxy** / **All Direct** bulk actions):
|
||||
|
||||
- **Off**: The tool is excluded and completely invisible to agents — it cannot be listed, searched, or called through the proxy, nor registered as a direct tool. Use this to hide noisy or dangerous tools.
|
||||
- **Proxy** (default): The tool is available through the single `mcp` proxy tool.
|
||||
- **Direct**: The tool is exposed to agents as a native top-level function with a `{server}_{tool}` prefixed name.
|
||||
|
||||
### The `mcp` Proxy Tool
|
||||
|
||||
The `mcp` proxy tool is a single function injected into an agent's context that multiplexes every MCP server assigned to that agent. Instead of loading each tool's full parameter schema up front, the agent interacts with servers on demand:
|
||||
|
||||
- `mcp({})` — connection and auth status of all assigned servers
|
||||
- `mcp({ server: "name" })` — list a server's tools
|
||||
- `mcp({ search: "query" })` — search for tools across servers (schemas included by default)
|
||||
- `mcp({ describe: "tool" })` — view one tool's full parameter schema
|
||||
- `mcp({ instructions: "name" })` — view a server's usage instructions
|
||||
- `mcp({ server: "name", tool: "tool_name", args: {...} })` — call a tool (pass either the `{server}_{tool}` prefixed name or the original name)
|
||||
- `mcp({ action: "auth-start", server: "name" })` — start OAuth for a server
|
||||
|
||||
Because only one compact tool (plus a short server/tool listing) occupies the context window, the proxy keeps token usage low even when a server exposes dozens of tools. The trade-off is an extra discovery round trip: the agent typically lists or searches tools first, then calls them.
|
||||
|
||||
### Direct Tools
|
||||
|
||||
Direct tools flip the trade-off in the other direction. A tool configured as **Direct** is registered as a native top-level function (e.g. `tavily_mcp_tavily_search`) that the agent can invoke immediately, exactly like any built-in tool.
|
||||
|
||||
Why use direct tools:
|
||||
|
||||
- **Faster, more reliable calls** for high-frequency actions: the full parameter schema is always in the context window, so the agent can call the tool in a single step without first discovering it through the proxy.
|
||||
- **Better tool discovery**: the model sees the tool and its description up front and is more likely to reach for it at the right moment.
|
||||
|
||||
The cost: every direct tool permanently consumes context tokens for its schema, so reserve **Direct** for frequently used or critical tools and keep large MCP servers on **Proxy** to avoid unnecessary context overhead. Direct tools are still subject to the same role-based permission checks as proxy calls, and the `mcp` proxy tool remains available for discovery, auth, and status.
|
||||
|
||||
---
|
||||
|
||||
## Authentication Modes
|
||||
|
||||
Shumai supports several authentication strategies for securing access to MCP endpoints:
|
||||
|
||||
### 1. No Auth (`none`)
|
||||
|
||||
Used for public or local MCP servers that do not require credentials.
|
||||
|
||||
### 2. Bearer Token (`bearer`)
|
||||
|
||||
Provide a static API token or key. Shumai attaches this token in the `Authorization: Bearer <token>` header for all transport requests.
|
||||
|
||||
### 3. OAuth 2.0 (`oauth`)
|
||||
|
||||
For MCP endpoints protected by OAuth identity providers:
|
||||
|
||||
- **Authorization Code + PKCE**: Click **Connect** on the server card in Team Settings. Shumai opens a secure OAuth popup window directing you to the identity provider. After approving permissions, the browser automatically redirects back to Shumai, completes token exchange, and refreshes server tools.
|
||||
- **Client Credentials**: Provide a `Client ID` and `Client Secret` for automated server-to-server token acquisition.
|
||||
|
||||
---
|
||||
|
||||
## Assigning MCP Servers to Agents
|
||||
|
||||
By default, newly added MCP servers are **not assigned to any agent**. To give an agent access to an MCP server:
|
||||
|
||||
1. Navigate to **Team Settings** > **Agents**.
|
||||
2. Click on the target agent to open its configuration dialog.
|
||||
3. Scroll down to the **MCP Servers** section.
|
||||
4. Toggle the switch next to the MCP server(s) you want to authorize for this agent.
|
||||
5. Click **Save Changes**.
|
||||
|
||||
---
|
||||
|
||||
## Role-Based Access Control
|
||||
|
||||
Each MCP server has a **Minimum Required Role** setting (`Reviewer`, `Editor`, or `Owner`):
|
||||
|
||||
- When an agent attempts to call a tool on an MCP server, Shumai verifies the role of the user interacting with the agent.
|
||||
- If the user's role does not meet the minimum requirement, tool execution is blocked with a permission error message, preventing unauthorized actions.
|
||||
@@ -14,6 +14,8 @@ Shumai allows you to collaborate with external stakeholders who do not have acco
|
||||
3. Configure the share link in the right sidebar. You can:
|
||||
- Add password protection
|
||||
- Set an expiration date
|
||||
- Allow or disallow downloads
|
||||
- Add a watermark to previews
|
||||
- Configure which metadata fields are visible to recipients
|
||||
4. Click **Copy Link** at the bottom of the sidebar, then send the link to your client or stakeholder.
|
||||
|
||||
@@ -41,6 +43,30 @@ Set an optional date and time after which the link becomes inactive. Once the da
|
||||
|
||||
Secure sensitive work by adding a password. External users visiting the link will be prompted to enter the correct password before they can view the assets.
|
||||
|
||||
### Allow Download
|
||||
|
||||
Control whether visitors can download the shared files. When **Allow download** is turned off:
|
||||
|
||||
- All download buttons are hidden on the public share page (file cards, selection bar, viewer control bars, and the top navigation).
|
||||
|
||||
Guests can still preview the files; only downloading is blocked. This is often combined with a watermark so previews remain viewable but protected.
|
||||
|
||||
### Watermark
|
||||
|
||||
Overlay a watermark (e.g. a "CONFIDENTIAL" text label or a logo image) on every preview shown through the share link. Open the watermark editor, arrange the blocks, and save.
|
||||
|
||||
Watermarking is **lazy and per-share-link** to keep storage costs low:
|
||||
|
||||
- The watermark transcode is only triggered when you **enable the watermark on a share link** — Shumai does **not** pre-generate watermark copies for every file in your workspace.
|
||||
- All video and image assets already in the share are transcoded at that moment (the sidebar shows a _processing_ status until it finishes).
|
||||
- Files and folders **added to the share link later are automatically transcoded too**, so the watermark always covers the full share without any manual steps.
|
||||
- Since watermark copies are only produced for assets that are actually shared through a watermarked link, files that are never shared (or shared through non-watermarked links) never consume extra storage for watermark proxies. This can save a very large amount of storage on big libraries.
|
||||
|
||||
Notes:
|
||||
|
||||
- Only **video and image** proxies are watermarked; other media types (PDF, audio, ...) are not affected.
|
||||
- While the watermark is still processing, previews are hidden rather than exposing the original, unwatermarked proxy.
|
||||
|
||||
### Default Sort Order
|
||||
|
||||
Set a default sorting rule for the shared page (e.g. sorting by name or version) to ensure the client views the files in the intended order.
|
||||
|
||||
+6
-4
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "shumai",
|
||||
"version": "0.3.3",
|
||||
"version": "0.3.8",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"repository": {
|
||||
@@ -18,11 +18,10 @@
|
||||
"typecheck": "tsc --noEmit",
|
||||
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
|
||||
"test": "bun --bun vitest run",
|
||||
"test:e2e": "playwright test --project=harness-chromium --project=app-chromium",
|
||||
"test:e2e:app": "playwright test --project=app-chromium",
|
||||
"test:e2e:webui": "E2E_SKIP_APP_SERVER=true playwright test --project=harness-chromium",
|
||||
"test:e2e:all": "playwright test",
|
||||
"test:e2e:workflow": "bun --bun vitest run -c packages/e2e/vitest.config.ts",
|
||||
"test:e2e:ui": "playwright test --ui --project=harness-chromium --project=app-chromium",
|
||||
"test:e2e:report": "playwright show-report",
|
||||
"test:e2e:install": "playwright install --with-deps",
|
||||
"harness:dev": "bun run packages/webui/e2e/harness/serve.ts",
|
||||
"format": "prettier --write .",
|
||||
@@ -66,5 +65,8 @@
|
||||
"typescript-eslint": "8.60.1",
|
||||
"vite-tsconfig-paths": "6.1.1",
|
||||
"vitest": "4.1.8"
|
||||
},
|
||||
"patchedDependencies": {
|
||||
"@earendil-works/pi-ai@0.82.1": "patches/@earendil-works%2Fpi-ai@0.82.1.patch"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/agent",
|
||||
"version": "0.3.3",
|
||||
"version": "0.3.8",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
@@ -14,12 +14,12 @@
|
||||
"@shumai/db": "workspace:*",
|
||||
"@shumai/dtos": "workspace:*",
|
||||
"@shumai/workflow-core": "workspace:*",
|
||||
"@sinclair/typebox": "0.34.49",
|
||||
"@temporalio/activity": "1.17.2",
|
||||
"@temporalio/worker": "1.17.2",
|
||||
"@temporalio/workflow": "1.17.2",
|
||||
"adm-zip": "0.5.17",
|
||||
"jittered-fractional-indexing": "1.0.1",
|
||||
"typebox": "1.1.38",
|
||||
"ulid": "3.0.2",
|
||||
"zod": "4.4.3"
|
||||
},
|
||||
|
||||
@@ -9,7 +9,6 @@ import {
|
||||
updateCommentActivity,
|
||||
getAgentChatContextActivity,
|
||||
getAgentAutofillContextActivity,
|
||||
getAssetAutofillContextActivity,
|
||||
getAssetActivity,
|
||||
getCommentActivity,
|
||||
getProjectAutofillFieldsActivity,
|
||||
@@ -19,6 +18,7 @@ import {
|
||||
generateSessionNameActivity,
|
||||
type GenerateSessionNameParams,
|
||||
getUserTeamInfoActivity,
|
||||
formatProjectContextPrompt,
|
||||
} from './agent'
|
||||
import * as piAgent from '../index'
|
||||
import { type AgentHarness, type Session } from '@earendil-works/pi-agent-core'
|
||||
@@ -460,58 +460,7 @@ describe('Agent Activities', () => {
|
||||
expect(res.usage.inputTokens).toBe(5)
|
||||
})
|
||||
|
||||
it('should include agentContext in the autofill prompt when provided', async () => {
|
||||
const mockHarness = {
|
||||
subscribe: vi.fn(),
|
||||
prompt: vi.fn().mockResolvedValue({
|
||||
content: [{ type: 'text', text: 'Captured' }],
|
||||
usage: { input: 5, output: 5 },
|
||||
}),
|
||||
}
|
||||
const mockSession = {
|
||||
getEntries: vi.fn().mockResolvedValue([]),
|
||||
getStorage: vi.fn().mockReturnValue({ sessionId: 'mock-session-id' }),
|
||||
}
|
||||
|
||||
vi.mocked(piAgent.createAgentSession).mockImplementation(async (config: unknown) => {
|
||||
const params = config as {
|
||||
customTools: Array<{
|
||||
name: string
|
||||
execute: (id: string, args: Record<string, unknown>) => Promise<unknown>
|
||||
}>
|
||||
}
|
||||
const tool = params.customTools.find((t) => t.name === 'autofill_metadata')
|
||||
if (tool) {
|
||||
await tool.execute('1', { f1: 'val' })
|
||||
}
|
||||
return {
|
||||
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: 'b1', provider: { name: 'google' }, modelRef: { modelId: 'gemini' } },
|
||||
dbProviders: [],
|
||||
teamSkills: [],
|
||||
allowedDomains: [],
|
||||
} as unknown as AgentExecutionContext
|
||||
|
||||
await autofillAiActivity({
|
||||
teamId: 't1',
|
||||
images: [],
|
||||
fields: [{ id: 'f1', config: { name: 'F1', type: 'text' } }],
|
||||
context,
|
||||
agentContext: 'Generated using gemini',
|
||||
})
|
||||
|
||||
const capturedPrompt = mockHarness.prompt.mock.calls[0]?.[0] ?? ''
|
||||
expect(capturedPrompt).toContain('Generated using gemini')
|
||||
expect(capturedPrompt).toContain('<context>')
|
||||
})
|
||||
|
||||
it('should not include agentContext in the autofill prompt when absent', async () => {
|
||||
it('should use the base autofill prompt without any creation-context injection', async () => {
|
||||
const mockHarness = {
|
||||
subscribe: vi.fn(),
|
||||
prompt: vi.fn().mockResolvedValue({
|
||||
@@ -557,6 +506,7 @@ describe('Agent Activities', () => {
|
||||
})
|
||||
|
||||
const capturedPrompt = mockHarness.prompt.mock.calls[0]?.[0] ?? ''
|
||||
expect(capturedPrompt).toContain('Analyze the provided images and extract metadata.')
|
||||
expect(capturedPrompt).not.toContain('<context>')
|
||||
})
|
||||
})
|
||||
@@ -1089,6 +1039,24 @@ describe('Agent Database Activities Integration', () => {
|
||||
},
|
||||
})
|
||||
|
||||
// Create two team skills, only one of which is enabled for the agent
|
||||
const enabledSkill = await prisma.skill.create({
|
||||
data: {
|
||||
name: 'Enabled Chat Skill',
|
||||
assetId: 'asset-enabled',
|
||||
hash: 'enabled-hash',
|
||||
teamId: team.id,
|
||||
},
|
||||
})
|
||||
const disabledSkill = await prisma.skill.create({
|
||||
data: {
|
||||
name: 'Disabled Chat Skill',
|
||||
assetId: 'asset-disabled',
|
||||
hash: 'disabled-hash',
|
||||
teamId: team.id,
|
||||
},
|
||||
})
|
||||
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: agentUser.id,
|
||||
@@ -1100,6 +1068,9 @@ describe('Agent Database Activities Integration', () => {
|
||||
provider: 'openai',
|
||||
model: 'gpt-4',
|
||||
},
|
||||
skills: {
|
||||
create: [{ skillId: enabledSkill.id }],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1110,6 +1081,10 @@ describe('Agent Database Activities Integration', () => {
|
||||
|
||||
expect(chatCtx.agent.id).toBe(agent.id)
|
||||
expect(chatCtx.dbProviders[0].name).toBe('openai')
|
||||
// Only the enabled skill must be advertised, and disabled team skills excluded
|
||||
expect(chatCtx.teamSkills.map((s) => s.id)).toEqual([enabledSkill.id])
|
||||
expect(chatCtx.enabledSkillIds).toEqual([enabledSkill.id])
|
||||
expect(chatCtx.teamSkills.map((s) => s.id)).not.toContain(disabledSkill.id)
|
||||
|
||||
// Create User for autofill agent
|
||||
const autofillUser = await prisma.user.create({
|
||||
@@ -1132,6 +1107,9 @@ describe('Agent Database Activities Integration', () => {
|
||||
provider: 'openai',
|
||||
model: 'gpt-4',
|
||||
},
|
||||
skills: {
|
||||
create: [{ skillId: enabledSkill.id }],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1140,6 +1118,77 @@ describe('Agent Database Activities Integration', () => {
|
||||
})
|
||||
|
||||
expect(autofillCtx.agent.id).toBe(autofillAgent.id)
|
||||
// Only the enabled skill must be advertised for the autofill agent as well
|
||||
expect(autofillCtx.teamSkills.map((s) => s.id)).toEqual([enabledSkill.id])
|
||||
expect(autofillCtx.enabledSkillIds).toEqual([enabledSkill.id])
|
||||
expect(autofillCtx.teamSkills.map((s) => s.id)).not.toContain(disabledSkill.id)
|
||||
})
|
||||
|
||||
it('should return an empty enabled skill list when the agent has no skills enabled', async () => {
|
||||
const provider = await prisma.provider.create({
|
||||
data: {
|
||||
teamId: team.id,
|
||||
name: 'openai-noskill',
|
||||
config: {
|
||||
api: 'openai-completions',
|
||||
apiKey: 'mock-key',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const model = await prisma.model.create({
|
||||
data: {
|
||||
providerId: provider.id,
|
||||
modelId: 'gpt-4',
|
||||
name: 'GPT 4',
|
||||
config: {
|
||||
reasoning: false,
|
||||
input: ['text'],
|
||||
contextWindow: 8192,
|
||||
maxTokens: 2048,
|
||||
cost: { input: 0.01, output: 0.03, cacheRead: 0, cacheWrite: 0 },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.skill.create({
|
||||
data: {
|
||||
name: 'Team Wide Skill',
|
||||
assetId: 'asset-team',
|
||||
hash: 'team-hash',
|
||||
teamId: team.id,
|
||||
},
|
||||
})
|
||||
|
||||
const agentUser = await prisma.user.create({
|
||||
data: {
|
||||
name: 'No Skill Agent User',
|
||||
email: 'noskillagent@shumai.ai',
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: agentUser.id,
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
providerId: provider.id,
|
||||
modelId: model.id,
|
||||
config: {
|
||||
provider: 'openai-noskill',
|
||||
model: 'gpt-4',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const chatCtx = await getAgentChatContextActivity({
|
||||
teamId: team.id,
|
||||
agentId: agent.id,
|
||||
})
|
||||
|
||||
// Team-wide skill exists but is not enabled for this agent
|
||||
expect(chatCtx.teamSkills).toEqual([])
|
||||
expect(chatCtx.enabledSkillIds).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1243,8 +1292,7 @@ describe('Agent Database Activities Integration', () => {
|
||||
key: 'field_key_1',
|
||||
scope: 'TEAM',
|
||||
teamId: team.id,
|
||||
aiAutofill: true,
|
||||
config: { name: 'Title Extractor', type: 'text' },
|
||||
config: { name: 'Title Extractor', type: 'text', autofillSource: 'CONTENT' },
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1264,37 +1312,6 @@ describe('Agent Database Activities Integration', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('getAssetAutofillContextActivity', () => {
|
||||
it('should return the stored autofill context for an asset', async () => {
|
||||
const withContext = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'ctx.png',
|
||||
type: AssetType.file,
|
||||
status: AssetStatus.uploaded,
|
||||
projectId: project.id,
|
||||
autofillContext: 'Generated using gemini',
|
||||
},
|
||||
})
|
||||
|
||||
const ctx = await getAssetAutofillContextActivity(withContext.id)
|
||||
expect(ctx).toBe('Generated using gemini')
|
||||
})
|
||||
|
||||
it('should return null when the asset has no autofill context', async () => {
|
||||
const noContext = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'plain.png',
|
||||
type: AssetType.file,
|
||||
status: AssetStatus.uploaded,
|
||||
projectId: project.id,
|
||||
},
|
||||
})
|
||||
|
||||
const ctx = await getAssetAutofillContextActivity(noContext.id)
|
||||
expect(ctx).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('executeAgentToolActivity', () => {
|
||||
beforeEach(() => {
|
||||
vi.spyOn(authzService, 'hasPermission').mockResolvedValue()
|
||||
@@ -1342,6 +1359,54 @@ describe('Agent Database Activities Integration', () => {
|
||||
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', () => {
|
||||
@@ -1406,7 +1471,17 @@ describe('Agent Database Activities Integration', () => {
|
||||
expect(uploadService.triggerPostUploadWorkflows).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should persist autofillContext on the created file when context is provided', async () => {
|
||||
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',
|
||||
@@ -1427,16 +1502,46 @@ describe('Agent Database Activities Integration', () => {
|
||||
name: 'ctx.txt',
|
||||
size: 100,
|
||||
contentType: 'text/plain',
|
||||
context: 'Generated using gemini',
|
||||
metadata: { prompt: 'Generated using gemini' },
|
||||
},
|
||||
userId: user.id,
|
||||
})
|
||||
|
||||
const created = await prisma.asset.findUnique({ where: { id: res.id } })
|
||||
expect(created?.autofillContext).toBe('Generated using gemini')
|
||||
const val = await prisma.assetMetadataValue.findFirst({
|
||||
where: { assetId: res.id, fieldKey: 'prompt' },
|
||||
})
|
||||
expect(val?.stringValue).toBe('Generated using gemini')
|
||||
})
|
||||
|
||||
it('should not set autofillContext when context is not provided', async () => {
|
||||
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',
|
||||
@@ -1453,16 +1558,80 @@ describe('Agent Database Activities Integration', () => {
|
||||
toolName: 'create_file',
|
||||
args: {
|
||||
parent: folder.id,
|
||||
s3Key: 'uploads/plain.txt',
|
||||
name: 'plain.txt',
|
||||
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,
|
||||
})
|
||||
|
||||
const created = await prisma.asset.findUnique({ where: { id: res.id } })
|
||||
expect(created?.autofillContext).toBeNull()
|
||||
// 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()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1573,7 +1742,17 @@ describe('Agent Database Activities Integration', () => {
|
||||
expect(Number(updatedStack?.sizeByte)).toBe(350)
|
||||
})
|
||||
|
||||
it('should persist autofillContext when creating a new version stack', async () => {
|
||||
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',
|
||||
@@ -1605,45 +1784,51 @@ describe('Agent Database Activities Integration', () => {
|
||||
name: 'v2.txt',
|
||||
size: 300,
|
||||
contentType: 'text/plain',
|
||||
context: 'Generated using seedance',
|
||||
metadata: { prompt: 'Generated using seedance' },
|
||||
},
|
||||
userId: user.id,
|
||||
})
|
||||
|
||||
const created = await prisma.asset.findUnique({ where: { id: res.id } })
|
||||
expect(created?.autofillContext).toBe('Generated using seedance')
|
||||
const val = await prisma.assetMetadataValue.findFirst({
|
||||
where: { assetId: res.id, fieldKey: 'prompt' },
|
||||
})
|
||||
expect(val?.stringValue).toBe('Generated using seedance')
|
||||
})
|
||||
|
||||
it('should persist autofillContext when adding to an existing version stack', async () => {
|
||||
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,
|
||||
},
|
||||
})
|
||||
|
||||
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,
|
||||
sizeByte: 200,
|
||||
},
|
||||
})
|
||||
|
||||
const existingFileVersion = await prisma.asset.create({
|
||||
const fileAsset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'v1.txt',
|
||||
type: AssetType.file,
|
||||
status: AssetStatus.uploaded,
|
||||
projectId: project.id,
|
||||
parentId: stack.id,
|
||||
sizeByte: 100,
|
||||
parentId: folder.id,
|
||||
sizeByte: 200,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1651,137 +1836,28 @@ describe('Agent Database Activities Integration', () => {
|
||||
taskId: 'task-1',
|
||||
toolName: 'create_version',
|
||||
args: {
|
||||
parent: existingFileVersion.id,
|
||||
parent: fileAsset.id,
|
||||
s3Key: 'uploads/v2.txt',
|
||||
name: 'v2.txt',
|
||||
size: 250,
|
||||
size: 300,
|
||||
contentType: 'text/plain',
|
||||
context: 'Generated using sora2',
|
||||
},
|
||||
userId: user.id,
|
||||
})
|
||||
|
||||
const created = await prisma.asset.findUnique({ where: { id: res.id } })
|
||||
expect(created?.autofillContext).toBe('Generated using sora2')
|
||||
})
|
||||
})
|
||||
|
||||
describe('list_autofill_fields', () => {
|
||||
it('should return only aiAutofill fields with their options', async () => {
|
||||
const folder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'WorkspaceFolder',
|
||||
type: AssetType.folder,
|
||||
status: AssetStatus.uploaded,
|
||||
projectId: project.id,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'source',
|
||||
scope: 'PROJECT',
|
||||
projectId: project.id,
|
||||
teamId: team.id,
|
||||
aiAutofill: true,
|
||||
config: {
|
||||
name: 'Source',
|
||||
type: 'select',
|
||||
select: {
|
||||
options: [
|
||||
{ id: 'gemini', displayName: 'Gemini', color: '#ffffff' },
|
||||
{ id: 'seedance', displayName: 'Seedance', color: '#ffffff' },
|
||||
],
|
||||
},
|
||||
metadata: {
|
||||
versionProvider: { newOption: { value: 'Midjourney' } },
|
||||
},
|
||||
description: 'Generation source',
|
||||
},
|
||||
})
|
||||
await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'title',
|
||||
scope: 'PROJECT',
|
||||
projectId: project.id,
|
||||
teamId: team.id,
|
||||
aiAutofill: true,
|
||||
config: { name: 'Title', type: 'text' },
|
||||
},
|
||||
})
|
||||
await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'manual_notes',
|
||||
scope: 'PROJECT',
|
||||
projectId: project.id,
|
||||
teamId: team.id,
|
||||
aiAutofill: false,
|
||||
config: { name: 'Manual Notes', type: 'text' },
|
||||
},
|
||||
})
|
||||
|
||||
const res = await executeAgentToolActivity({
|
||||
taskId: 'task-1',
|
||||
toolName: 'list_autofill_fields',
|
||||
args: { parent: folder.id },
|
||||
userId: user.id,
|
||||
})
|
||||
|
||||
expect(res.fields).toHaveLength(2)
|
||||
const source = res.fields.find((f: { name: string }) => f.name === 'Source')
|
||||
expect(source?.name).toBe('Source')
|
||||
expect(source?.type).toBe('select')
|
||||
expect(source?.description).toBe('Generation source')
|
||||
expect(source?.options).toEqual([{ displayName: 'Gemini' }, { displayName: 'Seedance' }])
|
||||
expect(res.fields.find((f: { name: string }) => f.name === 'Manual Notes')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('should resolve the project through the ancestor chain', async () => {
|
||||
const rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Root',
|
||||
type: AssetType.folder,
|
||||
status: AssetStatus.uploaded,
|
||||
projectId: project.id,
|
||||
},
|
||||
const val = await prisma.assetMetadataValue.findFirst({
|
||||
where: { assetId: res.id, fieldKey: selectField.key },
|
||||
})
|
||||
const subFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Sub',
|
||||
type: AssetType.folder,
|
||||
status: AssetStatus.uploaded,
|
||||
parentId: rootFolder.id,
|
||||
},
|
||||
expect(val?.stringValue).toBe('midjourney')
|
||||
|
||||
const updatedField = await prisma.metadataField.findUnique({
|
||||
where: { key: selectField.key },
|
||||
})
|
||||
|
||||
await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'title_2',
|
||||
scope: 'PROJECT',
|
||||
projectId: project.id,
|
||||
teamId: team.id,
|
||||
aiAutofill: true,
|
||||
config: { name: 'Title 2', type: 'text' },
|
||||
},
|
||||
})
|
||||
|
||||
const res = await executeAgentToolActivity({
|
||||
taskId: 'task-1',
|
||||
toolName: 'list_autofill_fields',
|
||||
args: { parent: subFolder.id },
|
||||
userId: user.id,
|
||||
})
|
||||
|
||||
expect(res.fields.length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('should fail when parent is missing', async () => {
|
||||
await expect(
|
||||
executeAgentToolActivity({
|
||||
taskId: 'task-1',
|
||||
toolName: 'list_autofill_fields',
|
||||
args: {},
|
||||
userId: user.id,
|
||||
}),
|
||||
).rejects.toThrow('parent parameter is required')
|
||||
const opts = (updatedField?.config as PrismaJson.FieldConfig)?.select?.options || []
|
||||
expect(opts.some((o) => o.displayName === 'Midjourney' && o.id === 'midjourney')).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1966,4 +2042,113 @@ describe('Agent Database Activities Integration', () => {
|
||||
expect(piAgent.createAgentSession).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('Nested AGENTS.md prompt injection', () => {
|
||||
it('formatProjectContextPrompt formats context files into XML project_context block', () => {
|
||||
expect(formatProjectContextPrompt([])).toBe('')
|
||||
|
||||
const formatted = formatProjectContextPrompt([
|
||||
{ path: '/AGENTS.md', content: 'Root rules' },
|
||||
{ path: '/subfolder/AGENTS.md', content: 'Subfolder rules' },
|
||||
])
|
||||
|
||||
expect(formatted).toBe(
|
||||
'\n\n<project_context>\n\nProject-specific instructions and guidelines:\n\n<project_instructions path="/AGENTS.md">\nRoot rules\n</project_instructions>\n\n<project_instructions path="/subfolder/AGENTS.md">\nSubfolder rules\n</project_instructions>\n\n</project_context>',
|
||||
)
|
||||
})
|
||||
|
||||
it('agentChatActivity injects nested AGENTS.md instructions into systemPrompt', async () => {
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'User_Chat_Md', email: `chat-md-${Date.now()}@example.com` },
|
||||
})
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Team_Chat_Md_' + Date.now() },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Project_Chat_Md', teamId: team.id },
|
||||
})
|
||||
const rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Root',
|
||||
type: AssetType.root,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
await prisma.assetAgentMd.create({
|
||||
data: {
|
||||
assetId: rootFolder.id,
|
||||
content: '# Root Policy',
|
||||
},
|
||||
})
|
||||
const subfolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'subfolder',
|
||||
type: AssetType.folder,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
await prisma.assetAgentMd.create({
|
||||
data: {
|
||||
assetId: subfolder.id,
|
||||
content: '# Subfolder Policy',
|
||||
},
|
||||
})
|
||||
|
||||
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: subfolder.id,
|
||||
assetId: subfolder.id,
|
||||
sessionId: 'mock-session-id',
|
||||
userId: user.id,
|
||||
context,
|
||||
})
|
||||
|
||||
expect(piAgent.createAgentSession).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
systemPrompt: expect.stringContaining('<project_context>'),
|
||||
}),
|
||||
)
|
||||
|
||||
const lastCall = vi.mocked(piAgent.createAgentSession).mock.calls.at(-1)
|
||||
const systemPromptArg = lastCall?.[0].systemPrompt as string
|
||||
expect(systemPromptArg).toContain(
|
||||
'<project_instructions path="/AGENTS.md">\n# Root Policy\n</project_instructions>',
|
||||
)
|
||||
expect(systemPromptArg).toContain(
|
||||
'<project_instructions path="/subfolder/AGENTS.md">\n# Subfolder Policy\n</project_instructions>',
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -7,6 +7,7 @@ import { type ImageContent } from '@earendil-works/pi-ai'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { authzService, Permission, ResourceType } 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'
|
||||
@@ -41,6 +42,11 @@ export type AgentWithProviderAndModel = Prisma.AgentGetPayload<{
|
||||
include: {
|
||||
provider: true
|
||||
modelRef: true
|
||||
skills: {
|
||||
include: {
|
||||
skill: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}>
|
||||
|
||||
@@ -54,9 +60,23 @@ export interface AgentExecutionContext {
|
||||
agent: AgentWithProviderAndModel
|
||||
dbProviders: DbProviderWithModels[]
|
||||
teamSkills: Skill[]
|
||||
mcpServers: Prisma.McpServerGetPayload<Record<string, never>>[]
|
||||
enabledSkillIds: string[]
|
||||
allowedDomains: string[]
|
||||
}
|
||||
|
||||
export function formatProjectContextPrompt(
|
||||
contextFiles: Array<{ path: string; content: string }>,
|
||||
): string {
|
||||
if (contextFiles.length === 0) return ''
|
||||
let prompt = '\n\n<project_context>\n\nProject-specific instructions and guidelines:\n\n'
|
||||
for (const { path: filePath, content } of contextFiles) {
|
||||
prompt += `<project_instructions path="${filePath}">\n${content}\n</project_instructions>\n\n`
|
||||
}
|
||||
prompt += '</project_context>'
|
||||
return prompt
|
||||
}
|
||||
|
||||
async function executeAgentPrompt(params: {
|
||||
taskId?: string
|
||||
teamId: string
|
||||
@@ -66,12 +86,14 @@ async function executeAgentPrompt(params: {
|
||||
agentsInstruction: string
|
||||
sessionId?: string
|
||||
userId?: string
|
||||
projectId?: string
|
||||
assetId?: string
|
||||
userCommentId?: string | null
|
||||
tools?: AgentTool[]
|
||||
context: AgentExecutionContext
|
||||
attachedAssets?: Array<{ id: string; name: string; type: string }>
|
||||
}): Promise<{ text: string; usage: Usage; sessionId: string }> {
|
||||
const { agent, dbProviders, teamSkills, allowedDomains } = params.context
|
||||
const { agent, dbProviders, teamSkills, enabledSkillIds, allowedDomains } = params.context
|
||||
|
||||
if (!agent.provider) {
|
||||
throw ApplicationFailure.create({
|
||||
@@ -96,7 +118,7 @@ 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, first use 'list_autofill_fields' to inspect the project's AI-autofillable metadata fields. If relevant metadata depends on information unavailable from the file content (for example, the AI model or tool used to generate it), include a brief context hint (maximum 50 words) in the 'context' field of 'create_file' or 'create_version' tool.`
|
||||
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.`
|
||||
|
||||
if (agent.soul) {
|
||||
systemPrompt = `${systemPrompt}\n\nAgent Personality and Core Instructions:\n${agent.soul}`
|
||||
@@ -105,6 +127,40 @@ When creating a file or version, first use 'list_autofill_fields' to inspect the
|
||||
// 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) {
|
||||
const comment = await prisma.assetComment.findUnique({
|
||||
where: { id: params.userCommentId },
|
||||
select: { assetId: true },
|
||||
})
|
||||
if (comment?.assetId) {
|
||||
targetAssetId = comment.assetId
|
||||
}
|
||||
} else if (params.taskId) {
|
||||
const task = await prisma.workflowTask.findUnique({
|
||||
where: { id: params.taskId },
|
||||
select: { assetId: true },
|
||||
})
|
||||
if (task?.assetId) {
|
||||
targetAssetId = task.assetId
|
||||
}
|
||||
} else if (params.projectId) {
|
||||
const project = await prisma.project.findUnique({
|
||||
where: { id: params.projectId },
|
||||
select: { rootFolderId: true },
|
||||
})
|
||||
if (project?.rootFolderId) {
|
||||
targetAssetId = project.rootFolderId
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const contextFiles = targetAssetId ? await assetService.getNestedAgentsMd(targetAssetId) : []
|
||||
if (contextFiles.length > 0) {
|
||||
systemPrompt += formatProjectContextPrompt(contextFiles)
|
||||
}
|
||||
|
||||
const modelConfig = agent.modelRef?.config
|
||||
if (modelConfig?.input) {
|
||||
systemPrompt += `\n\nYour current model supports the following input types: ${modelConfig.input.join(', ')}.`
|
||||
@@ -113,6 +169,13 @@ When creating a file or version, first use 'list_autofill_fields' to inspect the
|
||||
const agentConfig = agent.config as PrismaJson.AgentConfig | null | undefined
|
||||
const thinkingLevel = agentConfig?.thinkingLevel || 'off'
|
||||
|
||||
// MCP tools are only wired to chat agents (D6); autofill/embedding/naming
|
||||
// sessions never receive MCP tools even if a server is assigned.
|
||||
const mcpTools =
|
||||
agent.type === 'chat'
|
||||
? await mcpService.buildAgentTools(params.agentId, params.teamId, params.userId)
|
||||
: []
|
||||
|
||||
const { session, harness } = await createAgentSession({
|
||||
teamId: params.teamId,
|
||||
agentId: params.agentId,
|
||||
@@ -125,11 +188,13 @@ When creating a file or version, first use 'list_autofill_fields' to inspect the
|
||||
name: s.name,
|
||||
description: s.description,
|
||||
})),
|
||||
enabledSkillIds,
|
||||
allowedDomains,
|
||||
sessionId: params.sessionId,
|
||||
userId: params.userId,
|
||||
projectId: params.projectId,
|
||||
userCommentId: params.userCommentId,
|
||||
customTools: params.tools || [],
|
||||
customTools: [...(params.tools || []), ...mcpTools],
|
||||
providers: dbProviders.map((p) => ({
|
||||
name: p.name,
|
||||
config: p.config,
|
||||
@@ -331,6 +396,7 @@ export interface AgentChatParams {
|
||||
imageUrls: string[]
|
||||
projectId: string
|
||||
folderId: string
|
||||
assetId?: string
|
||||
agentsInstruction?: string
|
||||
sessionId: string
|
||||
userId?: string
|
||||
@@ -395,6 +461,8 @@ export async function agentChatActivity(params: AgentChatParams) {
|
||||
agentsInstruction: params.agentsInstruction || '',
|
||||
sessionId,
|
||||
userId: params.userId,
|
||||
projectId: params.projectId,
|
||||
assetId: params.assetId || params.folderId,
|
||||
userCommentId: params.userCommentId,
|
||||
context: params.context,
|
||||
attachedAssets: params.attachedAssets,
|
||||
@@ -406,17 +474,12 @@ export interface AutofillAiParams {
|
||||
images: string[]
|
||||
fields: AutofillField[]
|
||||
context: AgentExecutionContext
|
||||
agentContext?: string
|
||||
assetId?: string
|
||||
projectId?: string
|
||||
}
|
||||
|
||||
export async function autofillAiActivity(params: AutofillAiParams) {
|
||||
let prompt = 'Analyze the provided images and extract metadata.'
|
||||
if (params.agentContext && params.agentContext.trim()) {
|
||||
prompt +=
|
||||
'\n\nAdditional context about this file, provided during creation:\n' +
|
||||
`<context>\n${params.agentContext.trim()}\n</context>\n` +
|
||||
'Use this context to inform your answers, especially for fields that cannot be determined from the images alone (e.g. generation model/source).'
|
||||
}
|
||||
const prompt = 'Analyze the provided images and extract metadata.'
|
||||
const toolSchema = fieldsToTypeBoxSchema(params.fields)
|
||||
let capturedData: Record<string, unknown> | null = null
|
||||
|
||||
@@ -446,6 +509,8 @@ export async function autofillAiActivity(params: AutofillAiParams) {
|
||||
agentsInstruction: '',
|
||||
sessionId: undefined,
|
||||
userId: undefined,
|
||||
projectId: params.projectId,
|
||||
assetId: params.assetId,
|
||||
tools: [autofillTool],
|
||||
context: params.context,
|
||||
})
|
||||
@@ -834,6 +899,11 @@ export async function getAgentChatContextActivity(params: {
|
||||
include: {
|
||||
provider: true,
|
||||
modelRef: true,
|
||||
skills: {
|
||||
include: {
|
||||
skill: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if (!agent) {
|
||||
@@ -864,20 +934,29 @@ export async function getAgentChatContextActivity(params: {
|
||||
include: { models: true },
|
||||
})
|
||||
|
||||
// Fetch team skills
|
||||
const teamSkills = await prisma.skill.findMany({
|
||||
where: { teamId: params.teamId },
|
||||
// Only advertise skills that are explicitly enabled for this agent (agent_skills).
|
||||
// Disabled skills must not be listed in the system prompt nor loadable via read_skill.
|
||||
const enabledSkills = (agent.skills ?? []).map((as) => as.skill)
|
||||
const enabledSkillIds = enabledSkills.map((s) => s.id)
|
||||
|
||||
// Fetch the agent's assigned MCP servers
|
||||
const assignments = await prisma.agentMcpServer.findMany({
|
||||
where: { agentId: params.agentId },
|
||||
include: { mcpServer: true },
|
||||
})
|
||||
const mcpServers = assignments.map((a) => a.mcpServer)
|
||||
|
||||
const sandbox = await prisma.sandbox.findUnique({
|
||||
where: { teamId: params.teamId },
|
||||
})
|
||||
const allowedDomains = sandbox?.allowedDomains || []
|
||||
const allowedDomains = sandbox?.networkSandboxEnabled ? sandbox.allowedDomains : ['*']
|
||||
|
||||
return {
|
||||
agent,
|
||||
dbProviders,
|
||||
teamSkills,
|
||||
teamSkills: enabledSkills,
|
||||
mcpServers,
|
||||
enabledSkillIds,
|
||||
allowedDomains,
|
||||
}
|
||||
}
|
||||
@@ -912,6 +991,11 @@ export async function getAgentAutofillContextActivity(params: {
|
||||
include: {
|
||||
provider: true,
|
||||
modelRef: true,
|
||||
skills: {
|
||||
include: {
|
||||
skill: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if (!agentWithDetails) {
|
||||
@@ -938,20 +1022,25 @@ export async function getAgentAutofillContextActivity(params: {
|
||||
include: { models: true },
|
||||
})
|
||||
|
||||
// Fetch team skills
|
||||
const teamSkills = await prisma.skill.findMany({
|
||||
where: { teamId: params.teamId },
|
||||
})
|
||||
// Only advertise skills that are explicitly enabled for this agent (agent_skills).
|
||||
// Disabled skills must not be listed in the system prompt nor loadable via read_skill.
|
||||
const enabledSkills = (agentWithDetails.skills ?? []).map((as) => as.skill)
|
||||
const enabledSkillIds = enabledSkills.map((s) => s.id)
|
||||
|
||||
// Autofill agents currently do not use MCP tools.
|
||||
const mcpServers: Prisma.McpServerGetPayload<Record<string, never>>[] = []
|
||||
|
||||
const sandbox = await prisma.sandbox.findUnique({
|
||||
where: { teamId: params.teamId },
|
||||
})
|
||||
const allowedDomains = sandbox?.allowedDomains || []
|
||||
const allowedDomains = sandbox?.networkSandboxEnabled ? sandbox.allowedDomains : ['*']
|
||||
|
||||
return {
|
||||
agent: agentWithDetails,
|
||||
dbProviders,
|
||||
teamSkills,
|
||||
teamSkills: enabledSkills,
|
||||
mcpServers,
|
||||
enabledSkillIds,
|
||||
allowedDomains,
|
||||
}
|
||||
}
|
||||
@@ -1003,15 +1092,7 @@ export async function getCommentActivity(commentId: string) {
|
||||
|
||||
export async function getProjectAutofillFieldsActivity(projectId: string) {
|
||||
const fields = await metadataService.listProjectFields('', projectId)
|
||||
return fields.filter((f) => f.field.aiAutofill).map((f) => f.field)
|
||||
}
|
||||
|
||||
export async function getAssetAutofillContextActivity(assetId: string): Promise<string | null> {
|
||||
const asset = await prisma.asset.findUnique({
|
||||
where: { id: assetId },
|
||||
select: { autofillContext: true },
|
||||
})
|
||||
return asset?.autofillContext ?? null
|
||||
return fields.filter((f) => f.field.config?.autofillSource === 'CONTENT').map((f) => f.field)
|
||||
}
|
||||
|
||||
export async function updateAssetMetadataActivity(params: {
|
||||
@@ -1060,7 +1141,37 @@ export interface ExecuteAgentToolParams {
|
||||
userId?: string
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
/**
|
||||
* 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) {
|
||||
@@ -1134,13 +1245,35 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
||||
{ 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) => ({
|
||||
id: a.id,
|
||||
name: a.name,
|
||||
type: a.type,
|
||||
size: Number(a.sizeByte),
|
||||
}))
|
||||
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,
|
||||
@@ -1187,7 +1320,10 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
||||
const name = args.name as string
|
||||
const fileSize = args.size as number
|
||||
const mimeType = args.contentType as string
|
||||
const context = typeof args.context === 'string' ? args.context : undefined
|
||||
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',
|
||||
@@ -1221,6 +1357,9 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
||||
})
|
||||
}
|
||||
|
||||
// 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,
|
||||
@@ -1237,12 +1376,14 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
await assetService.updateAncestorsSize(tx as any, parent, fileSize)
|
||||
|
||||
// Persist autofill context provided by the agent (used by the autofill workflow)
|
||||
if (context) {
|
||||
await tx.asset.update({
|
||||
where: { id: newFile.id },
|
||||
data: { autofillContext: context },
|
||||
})
|
||||
// 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
|
||||
@@ -1268,7 +1409,10 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
||||
const name = args.name as string
|
||||
const fileSize = args.size as number
|
||||
const mimeType = args.contentType as string
|
||||
const context = typeof args.context === 'string' ? args.context : undefined
|
||||
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',
|
||||
@@ -1294,6 +1438,9 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
||||
})
|
||||
}
|
||||
|
||||
// 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,
|
||||
@@ -1333,12 +1480,14 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
||||
where: { id: newFile.id },
|
||||
data: { sortIndex: newSortIndex },
|
||||
})
|
||||
// Persist autofill context provided by the agent (used by the autofill workflow)
|
||||
if (context) {
|
||||
await tx.asset.update({
|
||||
where: { id: newFile.id },
|
||||
data: { autofillContext: context },
|
||||
})
|
||||
// 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({
|
||||
@@ -1398,12 +1547,14 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
||||
projectId: parentFile.projectId!,
|
||||
creatorId: userId,
|
||||
})
|
||||
// Persist autofill context provided by the agent (used by the autofill workflow)
|
||||
if (context) {
|
||||
await tx.asset.update({
|
||||
where: { id: newFile.id },
|
||||
data: { autofillContext: context },
|
||||
})
|
||||
// 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
|
||||
@@ -1427,49 +1578,6 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
||||
}
|
||||
}
|
||||
|
||||
case 'list_autofill_fields': {
|
||||
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,
|
||||
})
|
||||
|
||||
// Resolve projectId from the parent asset (climb ancestor chain if needed)
|
||||
const projectId = await resolveAssetProjectId(parent)
|
||||
if (!projectId) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `Could not resolve project for parent: ${parent}`,
|
||||
nonRetryable: true,
|
||||
})
|
||||
}
|
||||
|
||||
const fields = await metadataService.listProjectFields(userId, projectId)
|
||||
const autofillFields = fields
|
||||
.filter((f) => f.field.aiAutofill)
|
||||
.map((f) => ({
|
||||
name: f.field.config?.name,
|
||||
type: f.field.config?.type,
|
||||
description: f.field.description,
|
||||
options: (
|
||||
f.field.config?.select?.options ??
|
||||
f.field.config?.selectMulti?.options ??
|
||||
[]
|
||||
).map((o) => ({ displayName: o.displayName })),
|
||||
}))
|
||||
|
||||
return { fields: autofillFields }
|
||||
}
|
||||
|
||||
default:
|
||||
throw ApplicationFailure.create({
|
||||
message: `Unsupported tool name: ${toolName}`,
|
||||
@@ -1478,21 +1586,6 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveAssetProjectId(assetId: string): Promise<string | null> {
|
||||
let currentId: string | null = assetId
|
||||
while (currentId) {
|
||||
/* prettier-ignore */
|
||||
const assetNode = (await prisma.asset.findUnique({
|
||||
where: { id: currentId },
|
||||
select: { id: true, projectId: true, parentId: true },
|
||||
})) as { id: string; projectId: string | null; parentId: string | null } | null
|
||||
if (!assetNode) break
|
||||
if (assetNode.projectId) return assetNode.projectId
|
||||
currentId = assetNode.parentId
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export interface GenerateSessionNameParams {
|
||||
teamId: string
|
||||
agentId: string
|
||||
|
||||
@@ -10,6 +10,8 @@ import { createSandboxedBashTool } from './tools/sandboxed-bash'
|
||||
import { SandboxManager, type SandboxAskCallback } from '@anthropic-ai/sandbox-runtime'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { type TSchema } from 'typebox'
|
||||
import { Value } from 'typebox/value'
|
||||
import * as path from 'path'
|
||||
import * as childProcess from 'node:child_process'
|
||||
import * as fs from 'fs'
|
||||
@@ -89,10 +91,16 @@ describe('Sandbox Network isolation integration', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
it('should register callback that pushes blocked hosts to database and propagates to bash tool', async () => {
|
||||
// 1. Create a team
|
||||
// 1. Create a team and enabled sandbox
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Test Sandbox Team' },
|
||||
})
|
||||
await prisma.sandbox.create({
|
||||
data: {
|
||||
teamId: team.id,
|
||||
networkSandboxEnabled: true,
|
||||
},
|
||||
})
|
||||
|
||||
// Ensure agent user and agent exist in database to avoid foreign key violation
|
||||
await prisma.user.create({
|
||||
@@ -198,6 +206,12 @@ describe('Sandbox Network isolation integration', () => {
|
||||
const team2 = await prisma.team.create({
|
||||
data: { name: 'Team 2' },
|
||||
})
|
||||
await prisma.sandbox.create({
|
||||
data: { teamId: team1.id, networkSandboxEnabled: true },
|
||||
})
|
||||
await prisma.sandbox.create({
|
||||
data: { teamId: team2.id, networkSandboxEnabled: true },
|
||||
})
|
||||
|
||||
// Ensure agent user and agents exist in database
|
||||
await prisma.user.create({
|
||||
@@ -290,6 +304,64 @@ describe('Sandbox Network isolation integration', () => {
|
||||
}
|
||||
})
|
||||
|
||||
it('should bypass domain blocking and pending domain recording when networkSandboxEnabled is false', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Disabled Sandbox Team' },
|
||||
})
|
||||
await prisma.sandbox.create({
|
||||
data: { teamId: team.id, networkSandboxEnabled: false },
|
||||
})
|
||||
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: 'agent-disabled',
|
||||
name: 'Ai Agent Disabled',
|
||||
email: 'disabled@shumai.ai',
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: 'agent-disabled',
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'openai', model: 'gpt-4' },
|
||||
},
|
||||
})
|
||||
|
||||
const initializeSpy = vi.spyOn(SandboxManager, 'initialize').mockResolvedValue()
|
||||
vi.spyOn(SandboxManager, 'wrapWithSandbox').mockImplementation(async (cmd) => cmd)
|
||||
|
||||
await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: 'agent-disabled',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'prompt',
|
||||
teamSkills: [],
|
||||
allowedDomains: ['*'],
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
expect(initializeSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
network: expect.objectContaining({
|
||||
allowedDomains: ['*'],
|
||||
}),
|
||||
}),
|
||||
expect.any(Function),
|
||||
)
|
||||
|
||||
const callback = initializeSpy.mock.calls[0][1]
|
||||
const res = await callback!({ host: 'any-external-domain.com', port: 443 })
|
||||
expect(res).toBe(true)
|
||||
|
||||
const sandbox = await prisma.sandbox.findUnique({
|
||||
where: { teamId: team.id },
|
||||
})
|
||||
expect(sandbox?.pendingDomains).toEqual([])
|
||||
})
|
||||
|
||||
it('should pass thinkingLevel to AgentHarness and default to off', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Test Team' },
|
||||
@@ -813,3 +885,292 @@ describe('createAgentSession bash restriction for non-owner users', () => {
|
||||
expect(prompt).not.toContain('# Restricted User Context')
|
||||
})
|
||||
})
|
||||
|
||||
describe('createAgentSession typed metadata schema', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
async function setupSessionWithProject(opts: { withCreationFields: boolean }) {
|
||||
const team = await prisma.team.create({ data: { name: 'Typed Metadata Team' } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Typed Metadata Project', teamId: team.id },
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Typed Metadata User', email: 'typed-metadata@shumai.ai' },
|
||||
})
|
||||
await prisma.teamMember.create({
|
||||
data: { teamId: team.id, userId: user.id, role: 'owner' },
|
||||
})
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: 'typed-metadata-agent',
|
||||
name: 'Ai Agent Typed Metadata',
|
||||
email: 'typed-metadata-agent@shumai.ai',
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: 'typed-metadata-agent',
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'google', model: 'gemini' },
|
||||
},
|
||||
})
|
||||
|
||||
if (opts.withCreationFields) {
|
||||
await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'prompt',
|
||||
scope: 'PROJECT',
|
||||
projectId: project.id,
|
||||
teamId: team.id,
|
||||
config: { name: 'Prompt', type: 'text', autofillSource: 'CREATION_CONTEXT' },
|
||||
},
|
||||
})
|
||||
await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'source',
|
||||
scope: 'PROJECT',
|
||||
projectId: project.id,
|
||||
teamId: team.id,
|
||||
config: {
|
||||
name: 'Source',
|
||||
type: 'select',
|
||||
autofillSource: 'CREATION_CONTEXT',
|
||||
select: {
|
||||
options: [{ id: 'gemini', displayName: 'Gemini', color: '#ffffff' }],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'manualNotes',
|
||||
scope: 'PROJECT',
|
||||
projectId: project.id,
|
||||
teamId: team.id,
|
||||
config: { name: 'Manual Notes', type: 'text', autofillSource: 'NONE' },
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const initializeSpy = vi.spyOn(SandboxManager, 'initialize').mockResolvedValue()
|
||||
try {
|
||||
const { harness } = await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: 'typed-metadata-agent',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'prompt',
|
||||
teamSkills: [],
|
||||
allowedDomains: [],
|
||||
providers: mockProviders,
|
||||
userId: user.id,
|
||||
projectId: project.id,
|
||||
})
|
||||
return { harness }
|
||||
} finally {
|
||||
initializeSpy.mockRestore()
|
||||
}
|
||||
}
|
||||
|
||||
it('should build create_file/create_version with the project CREATION_CONTEXT fields', async () => {
|
||||
const { harness } = await setupSessionWithProject({ withCreationFields: true })
|
||||
const tools = harness.getTools()
|
||||
|
||||
expect(tools.some((t) => t.name === 'list_autofill_fields')).toBe(false)
|
||||
|
||||
const createFile = tools.find((t) => t.name === 'create_file')
|
||||
const createVersion = tools.find((t) => t.name === 'create_version')
|
||||
expect(createFile).toBeDefined()
|
||||
expect(createVersion).toBeDefined()
|
||||
|
||||
const checkFile = (args: Record<string, unknown>) =>
|
||||
Value.Check(createFile!.parameters as unknown as TSchema, {
|
||||
parent: 'f1',
|
||||
path: '/tmp/x.md',
|
||||
data: null,
|
||||
...args,
|
||||
})
|
||||
const checkVersion = (args: Record<string, unknown>) =>
|
||||
Value.Check(createVersion!.parameters as unknown as TSchema, {
|
||||
parent: 'f1',
|
||||
path: '/tmp/x.md',
|
||||
...args,
|
||||
})
|
||||
|
||||
for (const check of [checkFile, checkVersion]) {
|
||||
// Declared CREATION_CONTEXT fields (all-required, nullable) validate; other keys do not
|
||||
expect(check({ metadata: { prompt: 'hello', source: null } })).toBe(true)
|
||||
expect(check({ metadata: { prompt: null, source: 'gemini' } })).toBe(true)
|
||||
expect(check({ metadata: null })).toBe(true)
|
||||
expect(check({ metadata: { prompt: null, source: null, manualNotes: 'no' } })).toBe(false)
|
||||
expect(check({ metadata: { prompt: null, source: null, bogus: 1 } })).toBe(false)
|
||||
expect(check({ metadata: { prompt: 'hello' } })).toBe(false)
|
||||
}
|
||||
})
|
||||
|
||||
it('should omit the metadata parameter when the project has no CREATION_CONTEXT fields', async () => {
|
||||
const { harness } = await setupSessionWithProject({ withCreationFields: false })
|
||||
const createFile = harness.getTools().find((t) => t.name === 'create_file')
|
||||
const createVersion = harness.getTools().find((t) => t.name === 'create_version')
|
||||
expect(createFile).toBeDefined()
|
||||
expect(createVersion).toBeDefined()
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- inspecting the runtime schema shape of the dynamically-built tool
|
||||
const fileProps = (createFile!.parameters as any).properties
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- inspecting the runtime schema shape of the dynamically-built tool
|
||||
const versionProps = (createVersion!.parameters as any).properties
|
||||
expect('metadata' in fileProps).toBe(false)
|
||||
expect('metadata' in versionProps).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('createAgentSession enabled skill filtering', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('should only advertise and allow reading skills enabled for the agent', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Skill Filter Team' } })
|
||||
|
||||
const enabledSkill = await prisma.skill.create({
|
||||
data: {
|
||||
name: 'Enabled Skill',
|
||||
assetId: 'asset-enabled',
|
||||
hash: 'enabled-hash',
|
||||
teamId: team.id,
|
||||
},
|
||||
})
|
||||
const disabledSkill = await prisma.skill.create({
|
||||
data: {
|
||||
name: 'Disabled Skill',
|
||||
assetId: 'asset-disabled',
|
||||
hash: 'disabled-hash',
|
||||
teamId: team.id,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: 'skill-filter-agent',
|
||||
name: 'Ai Agent Skill Filter',
|
||||
email: 'skill-filter-agent@shumai.ai',
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: 'skill-filter-agent',
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'google', model: 'gemini' },
|
||||
skills: {
|
||||
create: [{ skillId: enabledSkill.id }],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const initializeSpy = vi.spyOn(SandboxManager, 'initialize').mockResolvedValue()
|
||||
try {
|
||||
const { harness } = await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: 'skill-filter-agent',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'prompt',
|
||||
teamSkills: [{ id: enabledSkill.id, name: enabledSkill.name }],
|
||||
enabledSkillIds: [enabledSkill.id],
|
||||
allowedDomains: [],
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
// The disabled skill must not be advertised in the available skills list
|
||||
// @ts-expect-error accessing private property for verification
|
||||
const prompt = await harness.systemPrompt()
|
||||
expect(prompt).toContain(enabledSkill.id)
|
||||
expect(prompt).not.toContain(disabledSkill.id)
|
||||
|
||||
// read_skill must reject the disabled skill id
|
||||
const readSkillTool = harness.getTools().find((t) => t.name === 'read_skill')
|
||||
expect(readSkillTool).toBeDefined()
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((p: any) => {
|
||||
if (p.toString().endsWith('.hash')) return 'enabled-hash'
|
||||
if (p.toString().endsWith('SKILL.md')) return '# Enabled Skill'
|
||||
return ''
|
||||
})
|
||||
|
||||
await expect(
|
||||
readSkillTool!.execute('1', { skillId: disabledSkill.id }, undefined, undefined, undefined),
|
||||
).rejects.toThrow('not enabled for this agent')
|
||||
|
||||
const result = await readSkillTool!.execute(
|
||||
'1',
|
||||
{ skillId: enabledSkill.id },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((result.content[0] as any).text).toBe('# Enabled Skill')
|
||||
} finally {
|
||||
initializeSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
|
||||
it('should reject all read_skill calls when no skills are enabled for the agent', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Skill None Team' } })
|
||||
const skill = await prisma.skill.create({
|
||||
data: { name: 'Unassigned Skill', assetId: 'asset-none', hash: 'none-hash', teamId: team.id },
|
||||
})
|
||||
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: 'skill-none-agent',
|
||||
name: 'Ai Agent Skill None',
|
||||
email: 'skill-none-agent@shumai.ai',
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: 'skill-none-agent',
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'google', model: 'gemini' },
|
||||
},
|
||||
})
|
||||
|
||||
const initializeSpy = vi.spyOn(SandboxManager, 'initialize').mockResolvedValue()
|
||||
try {
|
||||
const { harness } = await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: 'skill-none-agent',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'prompt',
|
||||
teamSkills: [],
|
||||
enabledSkillIds: [],
|
||||
allowedDomains: [],
|
||||
providers: mockProviders,
|
||||
})
|
||||
|
||||
const readSkillTool = harness.getTools().find((t) => t.name === 'read_skill')
|
||||
expect(readSkillTool).toBeDefined()
|
||||
|
||||
await expect(
|
||||
readSkillTool!.execute('1', { skillId: skill.id }, undefined, undefined, undefined),
|
||||
).rejects.toThrow('not enabled for this agent')
|
||||
} finally {
|
||||
initializeSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
+100
-30
@@ -10,14 +10,14 @@ import { createModels, InMemoryCredentialStore } from '@earendil-works/pi-ai'
|
||||
import { builtinProviders } from '@earendil-works/pi-ai/providers/all'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import { prisma, type TeamMemberRole } from '@shumai/db'
|
||||
import { Type, type TSchema } from '@sinclair/typebox'
|
||||
import { Type, type TSchema } from 'typebox'
|
||||
import * as fs from 'fs'
|
||||
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 { createListAutofillFieldsTool } from './tools/list-autofill-fields'
|
||||
import { createCreateFolderTool } from './tools/create-folder'
|
||||
import { createCreateVersionTool } from './tools/create-version'
|
||||
import { createDownloadAssetTool } from './tools/download-asset'
|
||||
@@ -98,9 +98,11 @@ export interface CreateAgentSessionParams {
|
||||
modelId: string
|
||||
systemPrompt: string
|
||||
teamSkills: Array<{ id: string; name: string; description?: string | null }>
|
||||
enabledSkillIds?: string[]
|
||||
allowedDomains: string[]
|
||||
sessionId?: string
|
||||
userId?: string
|
||||
projectId?: string
|
||||
userCommentId?: string | null
|
||||
customTools?: AgentTool[]
|
||||
thinkingLevel?: string
|
||||
@@ -145,9 +147,11 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
|
||||
modelId,
|
||||
systemPrompt,
|
||||
teamSkills,
|
||||
enabledSkillIds,
|
||||
allowedDomains,
|
||||
sessionId,
|
||||
userId,
|
||||
projectId,
|
||||
userCommentId: passedUserCommentId,
|
||||
customTools = [],
|
||||
thinkingLevel,
|
||||
@@ -186,6 +190,9 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
|
||||
const sandbox = await prisma.sandbox.findUnique({
|
||||
where: { teamId },
|
||||
})
|
||||
if (sandbox && !sandbox.networkSandboxEnabled) {
|
||||
return true
|
||||
}
|
||||
const pendingDomains = sandbox?.pendingDomains || []
|
||||
if (!pendingDomains.includes(host)) {
|
||||
await prisma.sandbox.upsert({
|
||||
@@ -313,17 +320,22 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
|
||||
next.map((t) => t.name),
|
||||
)
|
||||
}
|
||||
const readSkill = createReadSkillTool(userId, onEnvsAdded, restricted ? onSkillLoaded : undefined)
|
||||
const readSkill = createReadSkillTool(
|
||||
userId,
|
||||
onEnvsAdded,
|
||||
restricted ? onSkillLoaded : undefined,
|
||||
enabledSkillIds,
|
||||
)
|
||||
|
||||
const systemTools: AgentTool[] = []
|
||||
if (userId) {
|
||||
const metadataSchema = await buildProjectMetadataSchema(projectId)
|
||||
systemTools.push(
|
||||
createListAssetsTool(userId),
|
||||
createCreateFolderTool(userId),
|
||||
createCreateFileTool(userId),
|
||||
createCreateVersionTool(userId),
|
||||
createCreateFileTool(userId, metadataSchema),
|
||||
createCreateVersionTool(userId, metadataSchema),
|
||||
createDownloadAssetTool(userId),
|
||||
createListAutofillFieldsTool(userId),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -403,58 +415,116 @@ export interface AutofillField {
|
||||
}
|
||||
|
||||
export function fieldsToTypeBoxSchema(fields: AutofillField[]) {
|
||||
const newOptionSchema = Type.Object(
|
||||
{
|
||||
newOption: Type.Object(
|
||||
{
|
||||
value: Type.String({
|
||||
minLength: 1,
|
||||
description:
|
||||
'The new option display name/value to create and select if none of the existing options match.',
|
||||
}),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
)
|
||||
|
||||
const properties: Record<string, TSchema> = {}
|
||||
for (const f of fields) {
|
||||
let schema: TSchema
|
||||
const fieldName = f.config.name
|
||||
const fieldDesc = f.description || fieldName
|
||||
const baseDescription = `The field '${fieldName}' represents ${fieldDesc}.`
|
||||
switch (f.config.type) {
|
||||
case 'text':
|
||||
case 'longText':
|
||||
schema = Type.String()
|
||||
schema = Type.String({ description: baseDescription })
|
||||
break
|
||||
case 'number':
|
||||
case 'rating':
|
||||
schema = Type.Number()
|
||||
schema = Type.Number({ description: baseDescription })
|
||||
break
|
||||
case 'toggle':
|
||||
schema = Type.Boolean()
|
||||
schema = Type.Boolean({ description: baseDescription })
|
||||
break
|
||||
case 'select': {
|
||||
const options = f.config.select?.options || []
|
||||
schema = Type.String({
|
||||
enum: options.map((o) => o.id),
|
||||
})
|
||||
const description =
|
||||
options.length > 0
|
||||
? `${baseDescription}\nSelect one existing option ID or provide {"newOption": {"value": "..."}} to create a new option.\n\nAvailable options:\n${options.map((o) => `- ${o.displayName} => ${o.id}`).join('\n')}`
|
||||
: `${baseDescription}\nProvide {"newOption": {"value": "..."}} with the desired option name.`
|
||||
if (options.length > 0) {
|
||||
const optionLines = options.map((o) => `- ${o.displayName} => ${o.id}`).join('\n')
|
||||
schema.description = `The field '${fieldName}' represents ${fieldDesc}.\nSelect one option and return the option ID as the value.\n\nAvailable options:\n${optionLines}`
|
||||
schema = Type.Union(
|
||||
[
|
||||
Type.String({
|
||||
enum: options.map((o) => o.id),
|
||||
}),
|
||||
newOptionSchema,
|
||||
],
|
||||
{ description },
|
||||
)
|
||||
} else {
|
||||
schema = Type.Object(newOptionSchema.properties, {
|
||||
additionalProperties: false,
|
||||
description,
|
||||
})
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'selectMulti': {
|
||||
const options = f.config.selectMulti?.options || []
|
||||
schema = Type.Array(
|
||||
Type.String({
|
||||
enum: options.map((o) => o.id),
|
||||
}),
|
||||
)
|
||||
if (options.length > 0) {
|
||||
const optionLines = options.map((o) => `- ${o.displayName} => ${o.id}`).join('\n')
|
||||
schema.description = `The field '${fieldName}' represents ${fieldDesc}.\nSelect applicable options and return the option IDs as the value.\n\nAvailable options:\n${optionLines}`
|
||||
}
|
||||
const description =
|
||||
options.length > 0
|
||||
? `${baseDescription}\nSelect applicable option IDs or provide {"newOption": {"value": "..."}} objects for new options (e.g. ["opt1", {"newOption": {"value": "new_name"}}]).\n\nAvailable options:\n${options.map((o) => `- ${o.displayName} => ${o.id}`).join('\n')}`
|
||||
: `${baseDescription}\nProvide an array of {"newOption": {"value": "..."}} objects for new options.`
|
||||
const itemSchema =
|
||||
options.length > 0
|
||||
? Type.Union([
|
||||
Type.String({
|
||||
enum: options.map((o) => o.id),
|
||||
}),
|
||||
newOptionSchema,
|
||||
])
|
||||
: newOptionSchema
|
||||
schema = Type.Array(itemSchema, { description })
|
||||
break
|
||||
}
|
||||
default:
|
||||
schema = Type.String()
|
||||
schema = Type.String({ description: baseDescription })
|
||||
}
|
||||
|
||||
if (!schema.description) {
|
||||
schema.description = `The field '${fieldName}' represents ${fieldDesc}.`
|
||||
}
|
||||
properties[f.id] = schema
|
||||
// Every field is required but nullable: the agent must return each field key,
|
||||
// using `null` for values it does not know. Nulls are skipped when persisting.
|
||||
// This matches the OpenAI structured-outputs strict-mode convention (all
|
||||
// properties required, optionality expressed via `null`).
|
||||
properties[f.id] = Type.Union([schema, Type.Null()])
|
||||
}
|
||||
|
||||
return Type.Object(properties)
|
||||
return Type.Object(properties, { additionalProperties: false })
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the strict, all-optional metadata schema for the project's CREATION_CONTEXT
|
||||
* fields (the same field set the agent may attach when creating files/versions).
|
||||
* Returns undefined when no project is known or the project defines no fields, in
|
||||
* which case the `metadata` parameter is omitted from the tools entirely.
|
||||
*/
|
||||
async function buildProjectMetadataSchema(projectId?: string): Promise<TSchema | undefined> {
|
||||
if (!projectId) return undefined
|
||||
const fields = await metadataService.listProjectFields('', projectId)
|
||||
const creationFields: AutofillField[] = []
|
||||
for (const f of fields) {
|
||||
const config = f.field.config
|
||||
if (!config || config.autofillSource !== 'CREATION_CONTEXT') continue
|
||||
creationFields.push({
|
||||
id: f.field.key,
|
||||
config,
|
||||
description: f.field.description,
|
||||
})
|
||||
}
|
||||
if (creationFields.length === 0) return undefined
|
||||
return fieldsToTypeBoxSchema(creationFields)
|
||||
}
|
||||
|
||||
function escapeXml(str: string): string {
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
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'
|
||||
|
||||
vi.mock('@shumai/core/src/s3/s3', () => ({
|
||||
s3Service: {
|
||||
uploadFileToKey: vi.fn(),
|
||||
putObject: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('./tools/utils', () => ({
|
||||
executeAgentToolWorkflow: vi.fn().mockResolvedValue({ id: 'file-1', name: 'test', type: 'file' }),
|
||||
}))
|
||||
|
||||
/**
|
||||
* Reproduce: pi-ai <= 0.82.1 runs its lenient coercion on typebox v1 schemas and
|
||||
* converts `null` in a `[string, null]` union into `""` (see
|
||||
* coerceWithUnionSchema in @earendil-works/pi-ai/dist/utils/validation.js).
|
||||
*
|
||||
* When the agent calls create_file with `path: null, data: {...}`, the harness
|
||||
* validates first: the `null` path becomes `""`, so the tool's
|
||||
* assertExactlyOneSource believes BOTH path and data were provided and throws
|
||||
* "Provide exactly one of ..." even though the call is valid.
|
||||
*
|
||||
* Fixed upstream in pi-ai 0.84.0 (#7328): values that already match a union arm
|
||||
* are preserved before coercion. This test should pass once @earendil-works/pi-ai
|
||||
* is upgraded to >= 0.84.0.
|
||||
*/
|
||||
describe('reproduce null coercion in harness tool validation', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('keeps path: null when data is provided (no false "exactly one" error)', async () => {
|
||||
const tool = createCreateFileTool('user-1')
|
||||
|
||||
const validated = validateToolArguments(
|
||||
{
|
||||
name: 'create_file',
|
||||
description: '',
|
||||
parameters: tool.parameters,
|
||||
} as Parameters<typeof validateToolArguments>[0],
|
||||
{
|
||||
type: 'toolCall',
|
||||
id: 'call-1',
|
||||
name: 'create_file',
|
||||
arguments: {
|
||||
parent: '01KYKNP52YERGJ6Q4GTPF5FRQF',
|
||||
path: null,
|
||||
data: { name: 'random_text.txt', content: 'hello' },
|
||||
},
|
||||
} as Parameters<typeof validateToolArguments>[1],
|
||||
) as { path?: unknown; data?: unknown }
|
||||
|
||||
// The `null` path must survive validation untouched; coercion to "" makes
|
||||
// the tool think both path and data were provided.
|
||||
expect(validated.path).toBeNull()
|
||||
expect(validated.data).toEqual({ name: 'random_text.txt', content: 'hello' })
|
||||
|
||||
// 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(s3Service.putObject).toHaveBeenCalledTimes(1)
|
||||
expect(executeAgentToolWorkflow).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('keeps null metadata values (the "unknown" sentinel) through validation', () => {
|
||||
const tool = createCreateFileTool(
|
||||
'user-1',
|
||||
fieldsToTypeBoxSchema([{ id: 'prompt', config: { name: 'Prompt', type: 'text' } }]),
|
||||
)
|
||||
const validated = validateToolArguments(
|
||||
{
|
||||
name: 'create_file',
|
||||
description: '',
|
||||
parameters: tool.parameters,
|
||||
} as Parameters<typeof validateToolArguments>[0],
|
||||
{
|
||||
type: 'toolCall',
|
||||
id: 'call-1',
|
||||
name: 'create_file',
|
||||
arguments: {
|
||||
parent: 'folder-1',
|
||||
path: null,
|
||||
data: { name: 'a.md', content: 'x' },
|
||||
metadata: { prompt: null },
|
||||
},
|
||||
} as Parameters<typeof validateToolArguments>[1],
|
||||
) as { metadata?: Record<string, unknown> }
|
||||
|
||||
expect(validated.metadata).toEqual({ prompt: null })
|
||||
})
|
||||
})
|
||||
@@ -1,6 +1,22 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { type TSchema } from 'typebox'
|
||||
import { Value } from 'typebox/value'
|
||||
import { fieldsToTypeBoxSchema, type AutofillField } from './index'
|
||||
|
||||
interface AnyOfFieldSchema {
|
||||
type?: string
|
||||
enum?: string[]
|
||||
const?: string
|
||||
description?: string
|
||||
items?: { enum?: string[]; type?: string }
|
||||
anyOf?: AnyOfFieldSchema[]
|
||||
}
|
||||
|
||||
function propsOf(schema: TSchema): Record<string, { anyOf?: AnyOfFieldSchema[] }> {
|
||||
return (schema as unknown as { properties: Record<string, { anyOf?: AnyOfFieldSchema[] }> })
|
||||
.properties
|
||||
}
|
||||
|
||||
describe('fieldsToTypeBoxSchema', () => {
|
||||
it('converts text fields', () => {
|
||||
const fields: AutofillField[] = [
|
||||
@@ -8,10 +24,12 @@ describe('fieldsToTypeBoxSchema', () => {
|
||||
{ id: 'f2', config: { name: 'Desc', type: 'longText' } },
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(schema.properties.f1.type).toBe('string')
|
||||
expect(schema.properties.f1.description).toBe("The field 'Title' represents The title.")
|
||||
expect(schema.properties.f2.type).toBe('string')
|
||||
expect(schema.properties.f2.description).toBe("The field 'Desc' represents Desc.")
|
||||
const props = propsOf(schema)
|
||||
expect(props.f1.anyOf![0].type).toBe('string')
|
||||
expect(props.f1.anyOf![1].type).toBe('null')
|
||||
expect(props.f1.anyOf![0].description).toBe("The field 'Title' represents The title.")
|
||||
expect(props.f2.anyOf![0].type).toBe('string')
|
||||
expect(props.f2.anyOf![0].description).toBe("The field 'Desc' represents Desc.")
|
||||
})
|
||||
|
||||
it('converts numeric fields', () => {
|
||||
@@ -20,17 +38,18 @@ describe('fieldsToTypeBoxSchema', () => {
|
||||
{ id: 'f2', config: { name: 'Rating', type: 'rating' } },
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(schema.properties.f1.type).toBe('number')
|
||||
expect(schema.properties.f2.type).toBe('number')
|
||||
const props = propsOf(schema)
|
||||
expect(props.f1.anyOf![0].type).toBe('number')
|
||||
expect(props.f2.anyOf![0].type).toBe('number')
|
||||
})
|
||||
|
||||
it('converts toggle fields', () => {
|
||||
const fields: AutofillField[] = [{ id: 'f1', config: { name: 'Active', type: 'toggle' } }]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(schema.properties.f1.type).toBe('boolean')
|
||||
expect(propsOf(schema).f1.anyOf![0].type).toBe('boolean')
|
||||
})
|
||||
|
||||
it('converts select fields with enums and option descriptions', () => {
|
||||
it('converts select fields with enums, newOption support and option descriptions', () => {
|
||||
const fields: AutofillField[] = [
|
||||
{
|
||||
id: 'f1',
|
||||
@@ -48,14 +67,41 @@ describe('fieldsToTypeBoxSchema', () => {
|
||||
},
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(schema.properties.f1.type).toBe('string')
|
||||
expect(schema.properties.f1.enum).toEqual(['opt1', 'opt2'])
|
||||
expect(schema.properties.f1.description).toBe(
|
||||
"The field 'Species' represents Species of animal.\nSelect one option and return the option ID as the value.\n\nAvailable options:\n- Option 1 => opt1\n- Option 2 => opt2",
|
||||
const selectSchema = propsOf(schema).f1.anyOf![0]
|
||||
expect(selectSchema.anyOf![0].type).toBe('string')
|
||||
expect(selectSchema.anyOf![0].enum).toEqual(['opt1', 'opt2'])
|
||||
expect(selectSchema.anyOf![1].type).toBe('object')
|
||||
expect(selectSchema.description).toBe(
|
||||
'The field \'Species\' represents Species of animal.\nSelect one existing option ID or provide {"newOption": {"value": "..."}} to create a new option.\n\nAvailable options:\n- Option 1 => opt1\n- Option 2 => opt2',
|
||||
)
|
||||
// Declared option IDs, newOption objects, and null are valid
|
||||
expect(Value.Check(schema, { f1: 'opt1' })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: 'opt2' })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: { newOption: { value: 'kling' } } })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: null })).toBe(true)
|
||||
// Arbitrary strings or invalid objects are rejected
|
||||
expect(Value.Check(schema, { f1: 'not-an-option' })).toBe(false)
|
||||
expect(Value.Check(schema, { f1: { newOption: { foo: 'bar' } } })).toBe(false)
|
||||
})
|
||||
|
||||
it('converts selectMulti fields with array enums and option descriptions', () => {
|
||||
it('converts select fields with empty options to newOption schema only', () => {
|
||||
const fields: AutofillField[] = [
|
||||
{
|
||||
id: 'f1',
|
||||
config: {
|
||||
name: 'Provider',
|
||||
type: 'select',
|
||||
select: { options: [] },
|
||||
},
|
||||
},
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(Value.Check(schema, { f1: { newOption: { value: 'kling' } } })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: null })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: 'any-string' })).toBe(false)
|
||||
})
|
||||
|
||||
it('converts selectMulti fields with array enums, newOption support and option descriptions', () => {
|
||||
const fields: AutofillField[] = [
|
||||
{
|
||||
id: 'f1',
|
||||
@@ -72,11 +118,35 @@ describe('fieldsToTypeBoxSchema', () => {
|
||||
},
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(schema.properties.f1.type).toBe('array')
|
||||
expect(schema.properties.f1.items.enum).toEqual(['opt1', 'opt2'])
|
||||
expect(schema.properties.f1.description).toBe(
|
||||
"The field 'Tags' represents Tags.\nSelect applicable options and return the option IDs as the value.\n\nAvailable options:\n- Tag 1 => opt1\n- Tag 2 => opt2",
|
||||
const selectMultiSchema = propsOf(schema).f1.anyOf![0]
|
||||
expect(selectMultiSchema.type).toBe('array')
|
||||
expect(selectMultiSchema.description).toBe(
|
||||
'The field \'Tags\' represents Tags.\nSelect applicable option IDs or provide {"newOption": {"value": "..."}} objects for new options (e.g. ["opt1", {"newOption": {"value": "new_name"}}]).\n\nAvailable options:\n- Tag 1 => opt1\n- Tag 2 => opt2',
|
||||
)
|
||||
expect(Value.Check(schema, { f1: ['opt1', 'opt2'] })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: [{ newOption: { value: 'Tag 3' } }] })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: ['opt1', { newOption: { value: 'Tag 3' } }] })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: null })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: ['opt1', 'bad'] })).toBe(false)
|
||||
expect(Value.Check(schema, { f1: ['opt1', { newOption: { bad: 'value' } }] })).toBe(false)
|
||||
})
|
||||
|
||||
it('converts selectMulti fields with empty options to array of newOption schema', () => {
|
||||
const fields: AutofillField[] = [
|
||||
{
|
||||
id: 'f1',
|
||||
config: {
|
||||
name: 'Tags',
|
||||
type: 'selectMulti',
|
||||
selectMulti: { options: [] },
|
||||
},
|
||||
},
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(Value.Check(schema, { f1: [{ newOption: { value: 'Tag 1' } }] })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: [] })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: null })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: ['tag1'] })).toBe(false)
|
||||
})
|
||||
|
||||
it('defaults to string for unknown types', () => {
|
||||
@@ -85,6 +155,22 @@ describe('fieldsToTypeBoxSchema', () => {
|
||||
{ id: 'f1', config: { name: 'Unknown', type: 'invalid' } },
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(schema.properties.f1.type).toBe('string')
|
||||
expect(propsOf(schema).f1.anyOf![0].type).toBe('string')
|
||||
})
|
||||
|
||||
it('marks every property required (nullable) and rejects unknown keys', () => {
|
||||
const fields: AutofillField[] = [
|
||||
{ id: 'f1', config: { name: 'Title', type: 'text' } },
|
||||
{ id: 'f2', config: { name: 'Rating', type: 'rating' } },
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
const props = propsOf(schema)
|
||||
// typebox v1 objects are strict by default: unknown keys are rejected
|
||||
expect(props.f1.anyOf![0].type).toBe('string')
|
||||
expect(props.f1.anyOf![1].type).toBe('null')
|
||||
expect(Value.Check(schema, { f1: 'x', f2: null })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: null, f2: 3 })).toBe(true)
|
||||
expect(Value.Check(schema, { f1: 'x' })).toBe(false)
|
||||
expect(Value.Check(schema, { f1: 'x', f2: null, extra: 1 })).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
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'
|
||||
@@ -36,7 +36,7 @@ export function createAnalyzeImageTool(
|
||||
id: assetId,
|
||||
})
|
||||
|
||||
const asset = await prisma.asset.findUnique({
|
||||
let asset = await prisma.asset.findUnique({
|
||||
where: { id: assetId },
|
||||
include: { storageKey: true },
|
||||
})
|
||||
@@ -45,6 +45,17 @@ export function createAnalyzeImageTool(
|
||||
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) {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
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 { Value } from 'typebox/value'
|
||||
import * as fs from 'fs'
|
||||
import * as os from 'os'
|
||||
import * as path from 'path'
|
||||
@@ -44,7 +46,7 @@ describe('createCreateFileTool', () => {
|
||||
const filePath = createTempFile('# Hello from disk', 'create-file-test.md')
|
||||
|
||||
const tool = createCreateFileTool('user-1')
|
||||
const result = await tool.execute('call-1', { parent: 'folder-1', path: filePath })
|
||||
const result = await tool.execute('call-1', { parent: 'folder-1', path: filePath, data: null })
|
||||
|
||||
expect(s3Service.uploadFileToKey).toHaveBeenCalledTimes(1)
|
||||
expect(s3Service.putObject).not.toHaveBeenCalled()
|
||||
@@ -70,37 +72,93 @@ describe('createCreateFileTool', () => {
|
||||
expect(result.details).toEqual({ id: 'file-1', name: 'test', type: 'file' })
|
||||
})
|
||||
|
||||
it('should forward the optional context to the agent tool workflow when provided', async () => {
|
||||
const filePath = createTempFile('# Hello from disk', 'create-file-context.md')
|
||||
it('should forward the metadata to the agent tool workflow when provided', async () => {
|
||||
const filePath = createTempFile('# Hello from disk', 'create-file-metadata.md')
|
||||
const metadataSchema = fieldsToTypeBoxSchema([
|
||||
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
|
||||
])
|
||||
|
||||
const tool = createCreateFileTool('user-1')
|
||||
const tool = createCreateFileTool('user-1', metadataSchema)
|
||||
await tool.execute('call-1', {
|
||||
parent: 'folder-1',
|
||||
path: filePath,
|
||||
context: 'Generated using gemini',
|
||||
data: null,
|
||||
metadata: { prompt: 'Generated using gemini' },
|
||||
})
|
||||
|
||||
expect(executeAgentToolWorkflow).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
args: expect.objectContaining({ context: 'Generated using gemini' }),
|
||||
args: expect.objectContaining({ metadata: { prompt: 'Generated using gemini' } }),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should not include context in args when not provided', async () => {
|
||||
const filePath = createTempFile('# Hello from disk', 'create-file-no-context.md')
|
||||
it('should forward select and selectMulti newOption metadata', async () => {
|
||||
const filePath = createTempFile('# Hello from disk', 'create-file-new-option.md')
|
||||
const metadataSchema = fieldsToTypeBoxSchema([
|
||||
{
|
||||
id: 'provider',
|
||||
config: {
|
||||
name: 'Provider',
|
||||
type: 'select',
|
||||
select: { options: [{ id: 'openai', displayName: 'OpenAI', color: '#f43f5e' }] },
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'tags',
|
||||
config: {
|
||||
name: 'Tags',
|
||||
type: 'selectMulti',
|
||||
selectMulti: { options: [{ id: 'tag1', displayName: 'Tag 1', color: '#3b82f6' }] },
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const tool = createCreateFileTool('user-1')
|
||||
await tool.execute('call-1', { parent: 'folder-1', path: filePath })
|
||||
const tool = createCreateFileTool('user-1', metadataSchema)
|
||||
await tool.execute('call-1', {
|
||||
parent: 'folder-1',
|
||||
path: filePath,
|
||||
data: null,
|
||||
metadata: {
|
||||
provider: { newOption: { value: 'Kling' } },
|
||||
tags: ['tag1', { newOption: { value: 'Tag 2' } }],
|
||||
},
|
||||
})
|
||||
|
||||
expect(executeAgentToolWorkflow).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
args: expect.objectContaining({
|
||||
metadata: {
|
||||
provider: { newOption: { value: 'Kling' } },
|
||||
tags: ['tag1', { newOption: { value: 'Tag 2' } }],
|
||||
},
|
||||
}),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should not include metadata in args 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' } },
|
||||
])
|
||||
|
||||
const tool = createCreateFileTool('user-1', metadataSchema)
|
||||
await tool.execute('call-1', {
|
||||
parent: 'folder-1',
|
||||
path: filePath,
|
||||
data: null,
|
||||
metadata: null,
|
||||
})
|
||||
|
||||
const callArgs = vi.mocked(executeAgentToolWorkflow).mock.calls[0][0].args
|
||||
expect(callArgs).not.toHaveProperty('context')
|
||||
expect(callArgs).not.toHaveProperty('metadata')
|
||||
})
|
||||
|
||||
it('should throw when the local file does not exist', async () => {
|
||||
const tool = createCreateFileTool('user-1')
|
||||
await expect(
|
||||
tool.execute('call-1', { parent: 'folder-1', path: '/nonexistent/file.md' }),
|
||||
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()
|
||||
@@ -110,6 +168,7 @@ describe('createCreateFileTool', () => {
|
||||
const tool = createCreateFileTool('user-1')
|
||||
const result = await tool.execute('call-1', {
|
||||
parent: 'folder-1',
|
||||
path: null,
|
||||
data: { name: 'notes.md', content: '# Notes\n\nHello' },
|
||||
})
|
||||
|
||||
@@ -145,6 +204,7 @@ describe('createCreateFileTool', () => {
|
||||
const tool = createCreateFileTool('user-1')
|
||||
await tool.execute('call-1', {
|
||||
parent: 'folder-1',
|
||||
path: null,
|
||||
data: { name: 'script.ts', content: 'const x = 1' },
|
||||
})
|
||||
|
||||
@@ -162,6 +222,7 @@ describe('createCreateFileTool', () => {
|
||||
const tool = createCreateFileTool('user-1')
|
||||
await tool.execute('call-1', {
|
||||
parent: 'folder-1',
|
||||
path: null,
|
||||
data: { name: 'my/notes.md', content: 'hello' },
|
||||
})
|
||||
|
||||
@@ -183,9 +244,9 @@ describe('createCreateFileTool', () => {
|
||||
|
||||
it('should throw when neither "path" nor "data" is provided', async () => {
|
||||
const tool = createCreateFileTool('user-1')
|
||||
await expect(tool.execute('call-1', { parent: 'folder-1' })).rejects.toThrow(
|
||||
'Provide exactly one of "path" (a local file) or "data" (name and content).',
|
||||
)
|
||||
await expect(
|
||||
tool.execute('call-1', { parent: 'folder-1', path: null, data: null }),
|
||||
).rejects.toThrow('Provide exactly one of "path" (a local file) or "data" (name and content).')
|
||||
})
|
||||
|
||||
it('should throw when both "path" and "data" are provided', async () => {
|
||||
@@ -201,4 +262,58 @@ describe('createCreateFileTool', () => {
|
||||
expect(s3Service.putObject).not.toHaveBeenCalled()
|
||||
expect(executeAgentToolWorkflow).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should expose a strict, all-required (nullable) metadata schema when fields are provided', () => {
|
||||
const metadataSchema = fieldsToTypeBoxSchema([
|
||||
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
|
||||
{
|
||||
id: 'source',
|
||||
config: {
|
||||
name: 'Source',
|
||||
type: 'select',
|
||||
select: { options: [{ id: 'gemini', displayName: 'Gemini', color: '#ffffff' }] },
|
||||
},
|
||||
},
|
||||
{ id: 'rating', config: { name: 'Rating', type: 'rating' } },
|
||||
])
|
||||
|
||||
const tool = createCreateFileTool('user-1', metadataSchema)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- inspecting the runtime schema shape of the dynamically-built tool
|
||||
const meta = (tool.parameters as any).properties.metadata
|
||||
// `metadata` is a nullable union: [strict object, null]
|
||||
const metaObject = meta.anyOf[0]
|
||||
|
||||
expect(meta).toBeDefined()
|
||||
expect(metaObject.required).toEqual(['prompt', 'source', 'rating'])
|
||||
expect(metaObject.additionalProperties).toBe(false)
|
||||
expect(metaObject.properties.prompt.anyOf[0].type).toBe('string')
|
||||
expect(metaObject.properties.prompt.anyOf[1].type).toBe('null')
|
||||
expect(metaObject.properties.source.anyOf[0].anyOf[0].enum).toEqual(['gemini'])
|
||||
expect(metaObject.properties.rating.anyOf[0].type).toBe('number')
|
||||
|
||||
// Every field must be present (use null when unknown); unknown keys and bad enums are rejected
|
||||
expect(Value.Check(meta, { prompt: 'hello', source: 'gemini', rating: 5 })).toBe(true)
|
||||
expect(
|
||||
Value.Check(meta, {
|
||||
prompt: 'hello',
|
||||
source: { newOption: { value: 'kling' } },
|
||||
rating: 5,
|
||||
}),
|
||||
).toBe(true)
|
||||
expect(Value.Check(meta, { prompt: null, source: null, rating: null })).toBe(true)
|
||||
expect(Value.Check(meta, { prompt: 'hello', source: null, rating: null })).toBe(true)
|
||||
expect(Value.Check(meta, { prompt: 'hello' })).toBe(false)
|
||||
expect(
|
||||
Value.Check(meta, { prompt: 'hello', source: 'gemini', rating: 5, unknownKey: 'x' }),
|
||||
).toBe(false)
|
||||
expect(Value.Check(meta, { prompt: 'hello', source: 'not-an-option', rating: null })).toBe(
|
||||
false,
|
||||
)
|
||||
})
|
||||
|
||||
it('should omit the metadata parameter when no schema is provided', () => {
|
||||
const tool = createCreateFileTool('user-1')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- inspecting the runtime schema shape of the dynamically-built tool
|
||||
expect((tool.parameters as any).properties.metadata).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -2,68 +2,78 @@ import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
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 } from '@sinclair/typebox'
|
||||
import { Type, type TSchema } from 'typebox'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { ulid } from 'ulid'
|
||||
import { executeAgentToolWorkflow } from './utils'
|
||||
|
||||
const createFileSchema = Type.Object({
|
||||
parent: Type.String({
|
||||
description: 'The parent folder ID under which to create the file. This parameter is required.',
|
||||
}),
|
||||
path: Type.Optional(
|
||||
Type.String({
|
||||
description:
|
||||
'The absolute or relative local path to an existing file on disk. Provide either this or "data", but not both.',
|
||||
}),
|
||||
),
|
||||
data: Type.Optional(
|
||||
Type.Object({
|
||||
name: Type.String({
|
||||
minLength: 1,
|
||||
description:
|
||||
'The desired name for the new file, including its extension (e.g. "notes.md").',
|
||||
}),
|
||||
content: Type.String({
|
||||
description: 'The full content of the file as a plain string.',
|
||||
}),
|
||||
}),
|
||||
),
|
||||
context: Type.Optional(
|
||||
Type.String({
|
||||
description:
|
||||
'Optional short context about this file and how it was generated (max 50 words, e.g. "Generated using gemini"). ' +
|
||||
'Passed to the AI metadata autofill workflow to fill fields that cannot be determined from file content alone. ' +
|
||||
'Check autofillable fields with list_autofill_fields first.',
|
||||
}),
|
||||
),
|
||||
})
|
||||
interface CreateFileToolParams {
|
||||
parent: string
|
||||
path: string | null
|
||||
data: { name: string; content: string } | null
|
||||
metadata?: Record<string, unknown> | null
|
||||
}
|
||||
|
||||
function assertExactlyOneSource(params: {
|
||||
path?: string
|
||||
data?: { name: string; content: string }
|
||||
}) {
|
||||
const hasPath = params.path !== undefined
|
||||
const hasData = params.data !== undefined
|
||||
function assertExactlyOneSource(params: CreateFileToolParams) {
|
||||
const hasPath = params.path !== null
|
||||
const hasData = params.data !== null
|
||||
if (hasPath === hasData) {
|
||||
throw new Error('Provide exactly one of "path" (a local file) or "data" (name and content).')
|
||||
}
|
||||
return hasPath ? params.path! : params.data!
|
||||
}
|
||||
|
||||
export function createCreateFileTool(userId: string): AgentTool<typeof createFileSchema> {
|
||||
return {
|
||||
export function createCreateFileTool(userId: string, metadataSchema?: TSchema): AgentTool {
|
||||
const createFileSchema = Type.Object(
|
||||
{
|
||||
parent: Type.String({
|
||||
description:
|
||||
'The parent folder ID under which to create the file. This parameter is required.',
|
||||
}),
|
||||
path: Type.Union([
|
||||
Type.String({
|
||||
description:
|
||||
'The absolute or relative local path to an existing file on disk. Provide a value here OR in "data", but not both. Use null if you are providing "data".',
|
||||
}),
|
||||
Type.Null(),
|
||||
]),
|
||||
data: Type.Union([
|
||||
Type.Object(
|
||||
{
|
||||
name: Type.String({
|
||||
minLength: 1,
|
||||
description:
|
||||
'The desired name for the new file, including its extension (e.g. "notes.md").',
|
||||
}),
|
||||
content: Type.String({
|
||||
description: 'The full content of the file as a plain string.',
|
||||
}),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
),
|
||||
Type.Null(),
|
||||
]),
|
||||
...(metadataSchema ? { metadata: Type.Union([metadataSchema, Type.Null()]) } : {}),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
)
|
||||
|
||||
const tool: AgentTool = {
|
||||
name: 'create_file',
|
||||
label: 'Create File',
|
||||
description:
|
||||
'Create a new file under a folder. You MUST choose exactly ONE creation method: ' +
|
||||
'(1) provide "path" to upload an existing local file, OR ' +
|
||||
'(2) provide "data" with name and content to create a new text file. ' +
|
||||
'Never provide both "path" and "data" together. Never omit both.',
|
||||
'Set the unused parameter to null. ' +
|
||||
'The optional "metadata" parameter accepts only the CREATION_CONTEXT fields declared in its schema; never invent field keys, and use null for fields you do not know.',
|
||||
parameters: createFileSchema,
|
||||
execute: async (_toolCallId, params) => {
|
||||
const source = assertExactlyOneSource(params)
|
||||
// The harness validates tool args against `createFileSchema` before calling
|
||||
// execute, so `params` is guaranteed to match this shape.
|
||||
const p = params as CreateFileToolParams
|
||||
const source = assertExactlyOneSource(p)
|
||||
|
||||
let name: string
|
||||
let size: number
|
||||
@@ -98,15 +108,15 @@ export function createCreateFileTool(userId: string): AgentTool<typeof createFil
|
||||
const result = await executeAgentToolWorkflow({
|
||||
toolName: 'create_file',
|
||||
args: {
|
||||
parent: params.parent,
|
||||
parent: p.parent,
|
||||
s3Key,
|
||||
name,
|
||||
size,
|
||||
contentType: mimeType,
|
||||
...(params.context ? { context: params.context } : {}),
|
||||
...(p.metadata ? { metadata: p.metadata } : {}),
|
||||
},
|
||||
userId,
|
||||
assetId: params.parent,
|
||||
assetId: p.parent,
|
||||
})
|
||||
return {
|
||||
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
|
||||
@@ -114,4 +124,5 @@ export function createCreateFileTool(userId: string): AgentTool<typeof createFil
|
||||
}
|
||||
},
|
||||
}
|
||||
return tool
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { Type } from 'typebox'
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { executeAgentToolWorkflow } from './utils'
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
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 { Value } from 'typebox/value'
|
||||
import * as fs from 'fs'
|
||||
import * as os from 'os'
|
||||
import * as path from 'path'
|
||||
@@ -67,31 +69,80 @@ describe('createCreateVersionTool', () => {
|
||||
expect(result.details).toEqual({ id: 'file-1', name: 'v2', type: 'file' })
|
||||
})
|
||||
|
||||
it('should forward the optional context to the agent tool workflow when provided', async () => {
|
||||
const filePath = createTempFile('# Hello from disk', 'create-version-context.md')
|
||||
it('should forward the metadata to the agent tool workflow when provided', async () => {
|
||||
const filePath = createTempFile('# Hello from disk', 'create-version-metadata.md')
|
||||
const metadataSchema = fieldsToTypeBoxSchema([
|
||||
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
|
||||
])
|
||||
|
||||
const tool = createCreateVersionTool('user-1')
|
||||
const tool = createCreateVersionTool('user-1', metadataSchema)
|
||||
await tool.execute('call-1', {
|
||||
parent: 'file-1',
|
||||
path: filePath,
|
||||
context: 'Generated using gemini',
|
||||
metadata: { prompt: 'Generated using gemini' },
|
||||
})
|
||||
|
||||
expect(executeAgentToolWorkflow).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
args: expect.objectContaining({ context: 'Generated using gemini' }),
|
||||
args: expect.objectContaining({ metadata: { prompt: 'Generated using gemini' } }),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should not include context in args when not provided', async () => {
|
||||
const filePath = createTempFile('# Hello from disk', 'create-version-no-context.md')
|
||||
it('should forward select and selectMulti newOption metadata', async () => {
|
||||
const filePath = createTempFile('# Hello from disk', 'create-version-new-option.md')
|
||||
const metadataSchema = fieldsToTypeBoxSchema([
|
||||
{
|
||||
id: 'provider',
|
||||
config: {
|
||||
name: 'Provider',
|
||||
type: 'select',
|
||||
select: { options: [{ id: 'openai', displayName: 'OpenAI', color: '#f43f5e' }] },
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'tags',
|
||||
config: {
|
||||
name: 'Tags',
|
||||
type: 'selectMulti',
|
||||
selectMulti: { options: [{ id: 'tag1', displayName: 'Tag 1', color: '#3b82f6' }] },
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const tool = createCreateVersionTool('user-1')
|
||||
await tool.execute('call-1', { parent: 'file-1', path: filePath })
|
||||
const tool = createCreateVersionTool('user-1', metadataSchema)
|
||||
await tool.execute('call-1', {
|
||||
parent: 'file-1',
|
||||
path: filePath,
|
||||
metadata: {
|
||||
provider: { newOption: { value: 'Kling' } },
|
||||
tags: ['tag1', { newOption: { value: 'Tag 2' } }],
|
||||
},
|
||||
})
|
||||
|
||||
expect(executeAgentToolWorkflow).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
args: expect.objectContaining({
|
||||
metadata: {
|
||||
provider: { newOption: { value: 'Kling' } },
|
||||
tags: ['tag1', { newOption: { value: 'Tag 2' } }],
|
||||
},
|
||||
}),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should not include metadata in args 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' } },
|
||||
])
|
||||
|
||||
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('context')
|
||||
expect(callArgs).not.toHaveProperty('metadata')
|
||||
})
|
||||
|
||||
it('should throw when the local file does not exist', async () => {
|
||||
@@ -102,4 +153,34 @@ describe('createCreateVersionTool', () => {
|
||||
expect(s3Service.uploadFileToKey).not.toHaveBeenCalled()
|
||||
expect(executeAgentToolWorkflow).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should expose a strict, all-required (nullable) metadata schema when fields are provided', () => {
|
||||
const metadataSchema = fieldsToTypeBoxSchema([
|
||||
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
|
||||
{ id: 'toggle', config: { name: 'Active', type: 'toggle' } },
|
||||
])
|
||||
|
||||
const tool = createCreateVersionTool('user-1', metadataSchema)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- inspecting the runtime schema shape of the dynamically-built tool
|
||||
const meta = (tool.parameters as any).properties.metadata
|
||||
// `metadata` is a nullable union: [strict object, null]
|
||||
const metaObject = meta.anyOf[0]
|
||||
|
||||
expect(meta).toBeDefined()
|
||||
expect(metaObject.required).toEqual(['prompt', 'toggle'])
|
||||
expect(metaObject.additionalProperties).toBe(false)
|
||||
expect(metaObject.properties.prompt.anyOf[0].type).toBe('string')
|
||||
expect(metaObject.properties.prompt.anyOf[1].type).toBe('null')
|
||||
expect(metaObject.properties.toggle.anyOf[0].type).toBe('boolean')
|
||||
|
||||
expect(Value.Check(meta, { prompt: 'p', toggle: true })).toBe(true)
|
||||
expect(Value.Check(meta, { prompt: null, toggle: null })).toBe(true)
|
||||
expect(Value.Check(meta, { prompt: 'p', toggle: null, unknownKey: 'x' })).toBe(false)
|
||||
})
|
||||
|
||||
it('should omit the metadata parameter when no schema is provided', () => {
|
||||
const tool = createCreateVersionTool('user-1')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- inspecting the runtime schema shape of the dynamically-built tool
|
||||
expect((tool.parameters as any).properties.metadata).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { Type, type TSchema } from 'typebox'
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { executeAgentToolWorkflow } from './utils'
|
||||
import * as fs from 'fs'
|
||||
@@ -8,35 +8,42 @@ import { readFileMimeType } from '@shumai/core/src/utils/file-mime'
|
||||
import { ulid } from 'ulid'
|
||||
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
||||
|
||||
const createVersionSchema = Type.Object({
|
||||
parent: Type.String({
|
||||
description:
|
||||
'The parent file ID for which to create a new version. This parameter is required.',
|
||||
}),
|
||||
path: Type.String({
|
||||
description:
|
||||
'The absolute or relative local path to the file on disk. This parameter is required.',
|
||||
}),
|
||||
context: Type.Optional(
|
||||
Type.String({
|
||||
description:
|
||||
'Optional short context about this version and how it was generated (max 50 words, e.g. "Generated using gemini"). ' +
|
||||
'Passed to the AI metadata autofill workflow to fill fields that cannot be determined from file content alone. ' +
|
||||
'Check autofillable fields with list_autofill_fields first.',
|
||||
}),
|
||||
),
|
||||
})
|
||||
interface CreateVersionToolParams {
|
||||
parent: string
|
||||
path: string
|
||||
metadata?: Record<string, unknown> | null
|
||||
}
|
||||
|
||||
export function createCreateVersionTool(userId: string): AgentTool<typeof createVersionSchema> {
|
||||
return {
|
||||
export function createCreateVersionTool(userId: string, metadataSchema?: TSchema): AgentTool {
|
||||
const createVersionSchema = Type.Object(
|
||||
{
|
||||
parent: Type.String({
|
||||
description:
|
||||
'The parent file ID for which to create a new version. This parameter is required.',
|
||||
}),
|
||||
path: Type.String({
|
||||
description:
|
||||
'The absolute or relative local path to the file on disk. This parameter is required.',
|
||||
}),
|
||||
...(metadataSchema ? { metadata: Type.Union([metadataSchema, Type.Null()]) } : {}),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
)
|
||||
|
||||
const tool: AgentTool = {
|
||||
name: 'create_version',
|
||||
label: 'Create Version',
|
||||
description: 'Create a new version of an existing file from a local file path.',
|
||||
description:
|
||||
'Create a new version of an existing file from a local file path. ' +
|
||||
'The optional "metadata" parameter accepts only the CREATION_CONTEXT fields declared in its schema; never invent field keys, and use null for fields you do not know.',
|
||||
parameters: createVersionSchema,
|
||||
execute: async (_toolCallId, params) => {
|
||||
const absolutePath = path.resolve(process.cwd(), params.path)
|
||||
// The harness validates tool args against `createVersionSchema` before calling
|
||||
// execute, so `params` is guaranteed to match this shape.
|
||||
const p = params as CreateVersionToolParams
|
||||
const absolutePath = path.resolve(process.cwd(), p.path)
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
throw new Error(`Local file not found at path: ${params.path}`)
|
||||
throw new Error(`Local file not found at path: ${p.path}`)
|
||||
}
|
||||
|
||||
const fileSize = fs.statSync(absolutePath).size
|
||||
@@ -49,15 +56,15 @@ export function createCreateVersionTool(userId: string): AgentTool<typeof create
|
||||
const result = await executeAgentToolWorkflow({
|
||||
toolName: 'create_version',
|
||||
args: {
|
||||
parent: params.parent,
|
||||
parent: p.parent,
|
||||
s3Key,
|
||||
name: path.basename(absolutePath),
|
||||
size: fileSize,
|
||||
contentType: mimeType,
|
||||
...(params.context ? { context: params.context } : {}),
|
||||
...(p.metadata ? { metadata: p.metadata } : {}),
|
||||
},
|
||||
userId,
|
||||
assetId: params.parent,
|
||||
assetId: p.parent,
|
||||
})
|
||||
return {
|
||||
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
|
||||
@@ -65,4 +72,5 @@ export function createCreateVersionTool(userId: string): AgentTool<typeof create
|
||||
}
|
||||
},
|
||||
}
|
||||
return tool
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ vi.mock('@shumai/db', async (importOriginal) => {
|
||||
prisma: {
|
||||
asset: {
|
||||
findUnique: vi.fn(),
|
||||
findFirst: vi.fn(),
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -166,4 +167,48 @@ describe('downloadAssetTool', () => {
|
||||
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 },
|
||||
})
|
||||
|
||||
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')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { Type } from 'typebox'
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { prisma, type User } from '@shumai/db'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
@@ -35,7 +35,7 @@ export function createDownloadAssetTool(userId: string): AgentTool<typeof downlo
|
||||
id: assetId,
|
||||
})
|
||||
|
||||
const asset = await prisma.asset.findUnique({
|
||||
let asset = await prisma.asset.findUnique({
|
||||
where: { id: assetId },
|
||||
include: { storageKey: true },
|
||||
})
|
||||
@@ -44,6 +44,17 @@ export function createDownloadAssetTool(userId: string): AgentTool<typeof downlo
|
||||
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) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { Type } from 'typebox'
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { executeAgentToolWorkflow } from './utils'
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { createListAutofillFieldsTool } from './list-autofill-fields'
|
||||
import { executeAgentToolWorkflow } from './utils'
|
||||
|
||||
vi.mock('./utils', () => ({
|
||||
executeAgentToolWorkflow: vi.fn().mockResolvedValue({
|
||||
fields: [{ name: 'Source', type: 'select', options: [{ displayName: 'Gemini' }] }],
|
||||
}),
|
||||
}))
|
||||
|
||||
describe('createListAutofillFieldsTool', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('should list autofill fields for the parent folder via the agent tool workflow', async () => {
|
||||
const tool = createListAutofillFieldsTool('user-1')
|
||||
const result = await tool.execute('call-1', { parent: 'folder-1' })
|
||||
|
||||
expect(executeAgentToolWorkflow).toHaveBeenCalledWith({
|
||||
toolName: 'list_autofill_fields',
|
||||
args: { parent: 'folder-1' },
|
||||
userId: 'user-1',
|
||||
assetId: 'folder-1',
|
||||
})
|
||||
|
||||
expect(result.details).toEqual({
|
||||
fields: [{ name: 'Source', type: 'select', options: [{ displayName: 'Gemini' }] }],
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,35 +0,0 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { executeAgentToolWorkflow } from './utils'
|
||||
|
||||
const listAutofillFieldsSchema = Type.Object({
|
||||
parent: Type.String({
|
||||
description:
|
||||
'The parent folder ID whose project autofillable metadata fields to list. This parameter is required.',
|
||||
}),
|
||||
})
|
||||
|
||||
export function createListAutofillFieldsTool(
|
||||
userId: string,
|
||||
): AgentTool<typeof listAutofillFieldsSchema> {
|
||||
return {
|
||||
name: 'list_autofill_fields',
|
||||
label: 'List Autofill Fields',
|
||||
description:
|
||||
'List the AI-autofillable metadata fields defined for the project of a parent folder, including their types and available options. ' +
|
||||
'Use this before creating a file or version so you can pass relevant context (e.g. generation source) to create_file/create_version.',
|
||||
parameters: listAutofillFieldsSchema,
|
||||
execute: async (_toolCallId, params) => {
|
||||
const result = await executeAgentToolWorkflow({
|
||||
toolName: 'list_autofill_fields',
|
||||
args: params,
|
||||
userId,
|
||||
assetId: params.parent,
|
||||
})
|
||||
return {
|
||||
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
|
||||
details: result,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
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'
|
||||
@@ -56,7 +56,7 @@ export function createReadPdfPagesTool(
|
||||
id: targetAssetId,
|
||||
})
|
||||
|
||||
const asset = await prisma.asset.findUnique({
|
||||
let asset = await prisma.asset.findUnique({
|
||||
where: { id: targetAssetId },
|
||||
})
|
||||
|
||||
@@ -64,6 +64,16 @@ export function createReadPdfPagesTool(
|
||||
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.`)
|
||||
|
||||
@@ -363,4 +363,75 @@ describe('readSkillTool', () => {
|
||||
expect(onSkillLoaded).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('enabled skills enforcement', () => {
|
||||
it('should reject loading a skill that is not enabled for the agent', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Enabled Filter Team' } })
|
||||
const skill = await prisma.skill.create({
|
||||
data: {
|
||||
name: 'Disabled For Agent Skill',
|
||||
assetId: 'asset1',
|
||||
hash: 'disabled-agent-hash',
|
||||
teamId: team.id,
|
||||
},
|
||||
})
|
||||
|
||||
const readSkillTool = createReadSkillTool(undefined, () => {}, undefined, [
|
||||
'some-other-enabled-skill',
|
||||
])
|
||||
await expect(
|
||||
readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined),
|
||||
).rejects.toThrow('not enabled for this agent')
|
||||
})
|
||||
|
||||
it('should allow loading a skill that is enabled for the agent', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Enabled Allow Team' } })
|
||||
const skill = await prisma.skill.create({
|
||||
data: {
|
||||
name: 'Enabled For Agent Skill',
|
||||
assetId: 'asset1',
|
||||
hash: 'enabled-agent-hash',
|
||||
teamId: team.id,
|
||||
},
|
||||
})
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'enabled-agent-hash'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Enabled For Agent Skill'
|
||||
return ''
|
||||
})
|
||||
|
||||
const readSkillTool = createReadSkillTool(undefined, () => {}, undefined, [skill.id])
|
||||
const result = await readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((result.content[0] as any).text).toBe('# Enabled For Agent Skill')
|
||||
})
|
||||
|
||||
it('should allow loading any skill when enabledSkillIds is not provided (backward compatibility)', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'No Filter Team' } })
|
||||
const skill = await prisma.skill.create({
|
||||
data: {
|
||||
name: 'No Filter Skill',
|
||||
assetId: 'asset1',
|
||||
hash: 'no-filter-hash',
|
||||
teamId: team.id,
|
||||
},
|
||||
})
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'no-filter-hash'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# No Filter Skill'
|
||||
return ''
|
||||
})
|
||||
|
||||
const readSkillTool = createReadSkillTool(undefined, () => {})
|
||||
const result = await readSkillTool.execute('1', { skillId: skill.id }, undefined, undefined)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((result.content[0] as any).text).toBe('# No Filter Skill')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { Type } from 'typebox'
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
|
||||
@@ -17,6 +17,7 @@ export const createReadSkillTool = (
|
||||
userId: string | undefined,
|
||||
onEnvsAdded: (envs: Record<string, string>) => void,
|
||||
onSkillLoaded?: () => Promise<void> | void,
|
||||
enabledSkillIds?: string[],
|
||||
): AgentTool<typeof readSkillSchema, { skillId: string }> => ({
|
||||
name: 'read_skill',
|
||||
label: 'Read Agent Skill',
|
||||
@@ -31,6 +32,12 @@ export const createReadSkillTool = (
|
||||
throw new Error(`Skill with ID ${params.skillId} not found.`)
|
||||
}
|
||||
|
||||
// Skills disabled for this agent must not be loadable, even if the model
|
||||
// somehow knows their ID (e.g. from earlier conversation history).
|
||||
if (enabledSkillIds && !enabledSkillIds.includes(params.skillId)) {
|
||||
throw new Error(`Skill with ID ${params.skillId} is not enabled for this agent.`)
|
||||
}
|
||||
|
||||
const requiredLevel = ROLE_HIERARCHY[skill.permission] || 1
|
||||
|
||||
if (userId) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { Type } from 'typebox'
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
|
||||
const readThreadSchema = Type.Object({
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
|
||||
import { type AgentTool, type AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { Type } from 'typebox'
|
||||
import { spawn } from 'node:child_process'
|
||||
import { OutputAccumulator } from '../utils/output-accumulator'
|
||||
import {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
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'
|
||||
|
||||
@@ -64,6 +64,7 @@ describe('Agent Autofill Workflow', () => {
|
||||
mockActivities.createCommentActivity.mockResolvedValue({ id: 'comment-placeholder-id' })
|
||||
mockActivities.getAssetActivity.mockResolvedValue({
|
||||
id: 'a1',
|
||||
projectId: 'p1',
|
||||
storageKey: { key: 'asset-key' },
|
||||
project: { id: 'p1', teamId: 't1' },
|
||||
mediaType: 'image/png',
|
||||
@@ -78,7 +79,6 @@ describe('Agent Autofill Workflow', () => {
|
||||
{ key: 'title', config: { name: 'Title', type: 'text' }, description: 'The title' },
|
||||
])
|
||||
mockActivities.getAgentAutofillContextActivity.mockResolvedValue({ agent: { id: 'b1' } })
|
||||
mockActivities.getAssetAutofillContextActivity.mockResolvedValue('Generated using gemini')
|
||||
mockActivities.autofillAiActivity.mockResolvedValue({
|
||||
text: '{"title":"Extracted Title"}',
|
||||
sessionId: 'session-123',
|
||||
@@ -118,6 +118,9 @@ describe('Agent Autofill Workflow', () => {
|
||||
status: 'processing',
|
||||
})
|
||||
|
||||
// Verify asset fetched
|
||||
expect(mockActivities.getAssetActivity).toHaveBeenCalledWith('a1')
|
||||
|
||||
// Verify placeholder comment created
|
||||
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
||||
assetId: 'a1',
|
||||
@@ -140,11 +143,12 @@ describe('Agent Autofill Workflow', () => {
|
||||
expect(mockActivities.getAgentAutofillContextActivity).toHaveBeenCalledWith({
|
||||
teamId: 't1',
|
||||
})
|
||||
expect(mockActivities.getAssetAutofillContextActivity).toHaveBeenCalledWith('a1')
|
||||
|
||||
// Verify AI autofill called with mapped fields
|
||||
expect(mockActivities.autofillAiActivity).toHaveBeenCalledWith({
|
||||
teamId: 't1',
|
||||
assetId: 'a1',
|
||||
projectId: 'p1',
|
||||
images: ['/tmp/test-dir/1.webp'],
|
||||
fields: [
|
||||
{
|
||||
@@ -154,7 +158,6 @@ describe('Agent Autofill Workflow', () => {
|
||||
},
|
||||
],
|
||||
context: { agent: { id: 'b1' } },
|
||||
agentContext: 'Generated using gemini',
|
||||
})
|
||||
|
||||
// Verify task usage update
|
||||
@@ -195,42 +198,6 @@ describe('Agent Autofill Workflow', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('should run autofill without agentContext when the asset has no context', async () => {
|
||||
mockActivities.getAssetAutofillContextActivity.mockResolvedValue(null)
|
||||
|
||||
const task = await prisma.workflowTask.create({
|
||||
data: {
|
||||
type: 'ai_metadata_autofill',
|
||||
status: 'pending',
|
||||
assetId: 'a1',
|
||||
payload: {
|
||||
projectId: 'p1',
|
||||
agent: { sessionId: 's1', agentId: 'agent-1' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await agentAutofillMedia(task)
|
||||
|
||||
expect(mockActivities.getAssetAutofillContextActivity).toHaveBeenCalledWith('a1')
|
||||
expect(mockActivities.autofillAiActivity).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
teamId: 't1',
|
||||
images: ['/tmp/test-dir/1.webp'],
|
||||
fields: [
|
||||
{
|
||||
id: 'title',
|
||||
config: { name: 'Title', type: 'text' },
|
||||
description: 'The title',
|
||||
},
|
||||
],
|
||||
context: { agent: { id: 'b1' } },
|
||||
}),
|
||||
)
|
||||
const aiCallArgs = mockActivities.autofillAiActivity.mock.calls[0][0]
|
||||
expect(aiCallArgs).not.toHaveProperty('agentContext')
|
||||
})
|
||||
|
||||
it('should complete early if no image files could be extracted', async () => {
|
||||
mockActivities.extractAiMetadataActivity.mockResolvedValue([])
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
||||
extractAiMetadataActivity,
|
||||
getProjectAutofillFieldsActivity,
|
||||
getAgentAutofillContextActivity,
|
||||
getAssetAutofillContextActivity,
|
||||
autofillAiActivity,
|
||||
updateTaskUsageActivity,
|
||||
updateAssetMetadataActivity,
|
||||
@@ -132,17 +131,12 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
||||
teamId,
|
||||
})
|
||||
|
||||
// 3c. Fetch Agent-Provided Autofill Context (from create_file/create_version)
|
||||
const agentContext = await executeActivity(
|
||||
agentWorkerQueue,
|
||||
getAssetAutofillContextActivity,
|
||||
asset.id,
|
||||
)
|
||||
|
||||
// 4. Call AI Service
|
||||
const aiResult = await executeActivity(agentWorkerQueue, autofillAiActivity, {
|
||||
teamId,
|
||||
images: generatedFiles,
|
||||
assetId: asset.id,
|
||||
projectId: asset.projectId ?? undefined,
|
||||
fields: fields.map(
|
||||
(f: { key: string; config: Record<string, unknown>; description?: string | null }) => ({
|
||||
id: f.key,
|
||||
@@ -151,7 +145,6 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
||||
}),
|
||||
),
|
||||
context,
|
||||
...(agentContext ? { agentContext } : {}),
|
||||
})
|
||||
|
||||
if (aiResult.usage) {
|
||||
@@ -166,10 +159,12 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
||||
|
||||
// 6. Update Asset Metadata
|
||||
const result = JSON.parse(aiResult.text)
|
||||
const metadataUpdates = Object.entries(result).map(([key, value]) => ({
|
||||
key,
|
||||
value,
|
||||
}))
|
||||
const metadataUpdates = Object.entries(result)
|
||||
.filter(([, value]) => value !== null)
|
||||
.map(([key, value]) => ({
|
||||
key,
|
||||
value,
|
||||
}))
|
||||
|
||||
if (metadataUpdates.length > 0) {
|
||||
await executeActivity(agentWorkerQueue, updateAssetMetadataActivity, {
|
||||
|
||||
@@ -150,6 +150,7 @@ describe('Agent Chat Workflow', () => {
|
||||
agentsInstruction: expectedInstruction1,
|
||||
sessionId: 'session-123',
|
||||
folderId: 'parent-folder-id',
|
||||
assetId: 'a1',
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
@@ -202,6 +202,7 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
|
||||
imageUrls: attachmentImageUrls,
|
||||
projectId: payload.projectId,
|
||||
folderId,
|
||||
assetId: asset.id,
|
||||
agentsInstruction: instruction,
|
||||
sessionId,
|
||||
userId: payload.agent?.userId,
|
||||
|
||||
@@ -186,7 +186,7 @@ describe('Agent Embedding Workflow', () => {
|
||||
mediaType: 'image/png',
|
||||
storageKey: { key: 'test.png' },
|
||||
media: {
|
||||
imageTranscodes: [{ key: 'test-transcoded.webp', isRaw: false, format: 'webp' }],
|
||||
imageTranscodes: [{ key: 'test-transcoded.webp', format: 'webp' }],
|
||||
},
|
||||
},
|
||||
chunkDuration: 60.0,
|
||||
@@ -220,7 +220,7 @@ describe('Agent Embedding Workflow', () => {
|
||||
storageKey: { key: 'test.mp4' },
|
||||
media: {
|
||||
duration: 150.0,
|
||||
videoTranscodes: [{ key: 'test-transcoded.mp4', isRaw: false }],
|
||||
videoTranscodes: [{ key: 'test-transcoded.mp4' }],
|
||||
},
|
||||
},
|
||||
chunkDuration: 60.0,
|
||||
|
||||
@@ -91,20 +91,14 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
|
||||
const media = asset.media as any
|
||||
if (isVideo) {
|
||||
if (media.videoTranscodes && media.videoTranscodes.length > 0) {
|
||||
// Find first non-raw transcode or fallback to first transcode
|
||||
const transcode =
|
||||
media.videoTranscodes.find((t: PrismaJson.VideoTranscode) => !t.isRaw) ||
|
||||
media.videoTranscodes[0]
|
||||
const transcode = media.videoTranscodes[0] as PrismaJson.VideoTranscode
|
||||
if (transcode?.key) {
|
||||
embeddingKey = transcode.key
|
||||
}
|
||||
}
|
||||
} else if (isImage) {
|
||||
if (media.imageTranscodes && media.imageTranscodes.length > 0) {
|
||||
// Find first non-raw transcode or fallback to first transcode
|
||||
const transcode =
|
||||
media.imageTranscodes.find((t: PrismaJson.ImageTranscode) => !t.isRaw) ||
|
||||
media.imageTranscodes[0]
|
||||
const transcode = media.imageTranscodes[0] as PrismaJson.ImageTranscode
|
||||
if (transcode?.key) {
|
||||
embeddingKey = transcode.key
|
||||
if (transcode.format) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/api",
|
||||
"version": "0.3.3",
|
||||
"version": "0.3.8",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
|
||||
@@ -50,6 +50,7 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
skillId: s.skillId,
|
||||
skill: s.skill,
|
||||
})),
|
||||
mcpServerIds: (agent.mcpServers || []).map((m) => m.mcpServerId),
|
||||
deniedTools: config.deniedTools || [],
|
||||
}
|
||||
}),
|
||||
@@ -93,6 +94,7 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
skillId: s.skillId,
|
||||
skill: s.skill,
|
||||
})),
|
||||
mcpServerIds: (agent.mcpServers || []).map((m) => m.mcpServerId),
|
||||
deniedTools: config.deniedTools || [],
|
||||
}
|
||||
|
||||
@@ -146,6 +148,7 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
skillId: s.skillId,
|
||||
skill: s.skill,
|
||||
})),
|
||||
mcpServerIds: (agent.mcpServers || []).map((m) => m.mcpServerId),
|
||||
deniedTools: config.deniedTools || [],
|
||||
}
|
||||
|
||||
|
||||
@@ -351,7 +351,8 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
contentType,
|
||||
)
|
||||
|
||||
return c.json({ key })
|
||||
const url = await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET')
|
||||
return c.json({ key, url })
|
||||
})
|
||||
.post('/files/download-links', zValidator('json', getDownloadLinksRequestSchema), async (c) => {
|
||||
const user = c.get('user')
|
||||
|
||||
@@ -310,4 +310,59 @@ describe('folder api', () => {
|
||||
beforeIndex: 'index-1',
|
||||
})
|
||||
})
|
||||
|
||||
it('GET /folders/:folderId/agentsmd', async () => {
|
||||
vi.spyOn(assetService, 'getAgentsMd').mockResolvedValue('# Folder Guidelines')
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
|
||||
const res = await app.request('/folders/test-id/agentsmd')
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json()
|
||||
expect(json.content).toBe('# Folder Guidelines')
|
||||
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: { id: 'user1', name: 'Test User' },
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: 'test-id',
|
||||
})
|
||||
expect(assetService.getAgentsMd).toHaveBeenCalledWith('test-id')
|
||||
})
|
||||
|
||||
it('PATCH /folders/:folderId/agentsmd', async () => {
|
||||
vi.spyOn(assetService, 'updateAgentsMd').mockResolvedValue({ content: '# Updated Guidelines' })
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
|
||||
const res = await app.request('/folders/test-id/agentsmd', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ content: '# Updated Guidelines' }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json()
|
||||
expect(json.content).toBe('# Updated Guidelines')
|
||||
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: { id: 'user1', name: 'Test User' },
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Asset,
|
||||
id: 'test-id',
|
||||
})
|
||||
expect(assetService.updateAgentsMd).toHaveBeenCalledWith('test-id', '# Updated Guidelines')
|
||||
})
|
||||
|
||||
it('PATCH /folders/:folderId/agentsmd rejects oversized content', async () => {
|
||||
const mockUpdate = vi.spyOn(assetService, 'updateAgentsMd').mockResolvedValue({ content: '' })
|
||||
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
|
||||
const res = await app.request('/folders/test-id/agentsmd', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ content: 'x'.repeat(100_001) }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(400)
|
||||
expect(mockUpdate).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
updateFolderRequestSchema,
|
||||
deleteFoldersRequestSchema,
|
||||
restoreFoldersRequestSchema,
|
||||
updateAgentsMdRequestSchema,
|
||||
} from '@shumai/dtos'
|
||||
import { listChildrenRequestSchema, updateAssetOrderRequestSchema, AuditAction } from '@shumai/dtos'
|
||||
import { searchRequestSchema } from '@shumai/dtos'
|
||||
@@ -189,5 +190,48 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
const result = await searchService.search(folderId, req)
|
||||
return c.json(result)
|
||||
})
|
||||
.get('/folders/:folderId/agentsmd', async (c) => {
|
||||
const folderId = c.req.param('folderId')
|
||||
const user = c.get('user')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: folderId,
|
||||
})
|
||||
|
||||
const content = await assetService.getAgentsMd(folderId)
|
||||
return c.json({ content })
|
||||
})
|
||||
.patch(
|
||||
'/folders/:folderId/agentsmd',
|
||||
zValidator('json', updateAgentsMdRequestSchema),
|
||||
async (c) => {
|
||||
const folderId = c.req.param('folderId')
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Asset,
|
||||
id: folderId,
|
||||
})
|
||||
|
||||
const res = await assetService.updateAgentsMd(folderId, req.content)
|
||||
|
||||
const ctx = await assetService.getAssetContext(folderId)
|
||||
await auditLogService.logAction({
|
||||
action: AuditAction.folder_update,
|
||||
teamId: ctx.teamId,
|
||||
userId: user.id,
|
||||
projectId: ctx.projectId,
|
||||
itemId: folderId,
|
||||
})
|
||||
|
||||
return c.json(res)
|
||||
},
|
||||
)
|
||||
|
||||
export default route
|
||||
|
||||
@@ -9,6 +9,7 @@ import fileRoute from './file'
|
||||
import folderRoute from './folder'
|
||||
import inviteRoute from './invite'
|
||||
import metadataRoute from './metadata'
|
||||
import mcpRoute, { mcpOauthCallbackRoute } from './mcp'
|
||||
import { authMiddleware } from './middleware/auth'
|
||||
import { tokenAuthMiddleware } from './middleware/tokenAuth'
|
||||
import notificationRoute from './notification'
|
||||
@@ -19,6 +20,7 @@ import publicShareRoute from './public-share'
|
||||
import s3Route from './s3'
|
||||
import shareRoute from './share'
|
||||
import skillRoute from './skill'
|
||||
import watermarkTemplateRoute from './watermark-template'
|
||||
import teamRoute from './team'
|
||||
import uploadRoute, { localUploadRoute } from './upload'
|
||||
import versionStackRoute from './versionStack'
|
||||
@@ -33,6 +35,7 @@ const apiRoute = new Hono<{ Variables: { user: User } }>()
|
||||
.route('/', publicInviteRoute)
|
||||
.route('/', publicShareRoute)
|
||||
.route('/', localUploadRoute)
|
||||
.route('/', mcpOauthCallbackRoute)
|
||||
|
||||
// Selected routes supporting API token authentication
|
||||
.use('/projects', tokenAuthMiddleware)
|
||||
@@ -60,6 +63,8 @@ const apiRoute = new Hono<{ Variables: { user: User } }>()
|
||||
.route('/', uploadRoute)
|
||||
.route('/', shareRoute)
|
||||
.route('/', skillRoute)
|
||||
.route('/', mcpRoute)
|
||||
.route('/', watermarkTemplateRoute)
|
||||
|
||||
app.route('/files', s3Route)
|
||||
|
||||
|
||||
@@ -0,0 +1,351 @@
|
||||
import { describe, expect, test, vi, beforeEach } from 'vitest'
|
||||
import { Hono } from 'hono'
|
||||
import mcpRoute, { mcpOauthCallbackRoute } from './mcp'
|
||||
import { mcpService } from '@shumai/core/src/mcp/mcp-service'
|
||||
import { authMiddleware } from './middleware/auth'
|
||||
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
|
||||
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
|
||||
auditLogService: {
|
||||
logAction: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@shumai/core/src/mcp/mcp-service', () => ({
|
||||
mcpService: {
|
||||
listServers: vi.fn(),
|
||||
createServer: vi.fn(),
|
||||
getServer: vi.fn(),
|
||||
getServerRecord: vi.fn(),
|
||||
updateServer: vi.fn(),
|
||||
updateServerPermission: vi.fn(),
|
||||
deleteServer: vi.fn(),
|
||||
refreshServer: vi.fn(),
|
||||
startAuth: vi.fn(),
|
||||
completeAuth: vi.fn(),
|
||||
getAuthStatus: vi.fn(),
|
||||
removeAuth: vi.fn(),
|
||||
testServer: vi.fn(),
|
||||
handleOauthCallback: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('./middleware/auth', () => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
authMiddleware: async (c: any, next: any) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
c.set('user', { id: 'user1', name: 'Test User' } as any)
|
||||
await next()
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@shumai/core/src/authz/authz', () => ({
|
||||
authzService: {
|
||||
hasPermission: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
Permission: {
|
||||
Read: 'Read',
|
||||
Edit: 'Edit',
|
||||
Admin: 'Admin',
|
||||
},
|
||||
ResourceType: {
|
||||
Team: 'team',
|
||||
McpServer: 'mcpServer',
|
||||
},
|
||||
}))
|
||||
|
||||
import type { McpServerInfo } from '@shumai/dtos'
|
||||
|
||||
const serverInfo: McpServerInfo = {
|
||||
id: 'server1',
|
||||
name: 'github',
|
||||
description: 'GitHub MCP server',
|
||||
instructions: 'Always use the sandbox API and respect rate limits.',
|
||||
url: 'https://mcp.example.com/github',
|
||||
transport: 'streamable_http',
|
||||
authType: 'bearer',
|
||||
permission: 'reviewer',
|
||||
status: 'not_connected',
|
||||
toolCount: 0,
|
||||
hasCredential: false,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
}
|
||||
|
||||
describe('MCP API', () => {
|
||||
const app = new Hono().use('*', authMiddleware).route('/', mcpRoute)
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
// Clear call history so per-test call assertions are isolated.
|
||||
vi.clearAllMocks()
|
||||
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
|
||||
vi.mocked(mcpService.listServers).mockResolvedValue([serverInfo])
|
||||
vi.mocked(mcpService.getServer).mockResolvedValue(serverInfo)
|
||||
vi.mocked(mcpService.getServerRecord).mockResolvedValue({
|
||||
...serverInfo,
|
||||
lastError: null,
|
||||
lastConnectedAt: null,
|
||||
teamId: 'team1',
|
||||
tools: [],
|
||||
credential: null,
|
||||
config: null,
|
||||
authConfig: { type: 'bearer', bearerToken: 'secret' },
|
||||
} as never)
|
||||
})
|
||||
|
||||
test('lists servers with Admin permission on the team', async () => {
|
||||
const res = await app.request('/teams/team1/mcp/servers')
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({ servers: [serverInfo] })
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: expect.anything(),
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Team,
|
||||
id: 'team1',
|
||||
})
|
||||
})
|
||||
|
||||
test('includes server instructions in list and get responses', async () => {
|
||||
const list = await app.request('/teams/team1/mcp/servers')
|
||||
expect((await list.json()).servers[0].instructions).toBe(
|
||||
'Always use the sandbox API and respect rate limits.',
|
||||
)
|
||||
|
||||
const got = await app.request('/mcp/servers/server1')
|
||||
expect((await got.json()).instructions).toBe(
|
||||
'Always use the sandbox API and respect rate limits.',
|
||||
)
|
||||
})
|
||||
|
||||
test('creates a server and audits it', async () => {
|
||||
vi.mocked(mcpService.createServer).mockResolvedValue(serverInfo)
|
||||
const res = await app.request('/teams/team1/mcp/servers', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ url: 'https://mcp.example.com/github' }),
|
||||
})
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual(serverInfo)
|
||||
expect(auditLogService.logAction).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ action: 'mcp_server_create', teamId: 'team1' }),
|
||||
)
|
||||
})
|
||||
|
||||
test('rejects a server with an invalid URL', async () => {
|
||||
const res = await app.request('/teams/team1/mcp/servers', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ url: 'not-a-url' }),
|
||||
})
|
||||
expect(res.status).toBe(400)
|
||||
expect(mcpService.createServer).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('gets a single server with Admin on the McpServer resource', async () => {
|
||||
const res = await app.request('/mcp/servers/server1')
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual(serverInfo)
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: expect.anything(),
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.McpServer,
|
||||
id: 'server1',
|
||||
})
|
||||
})
|
||||
|
||||
test('returns 404 when the server is missing', async () => {
|
||||
vi.mocked(mcpService.getServer).mockResolvedValue(null)
|
||||
const res = await app.request('/mcp/servers/missing')
|
||||
expect(res.status).toBe(404)
|
||||
})
|
||||
|
||||
test('updates a server and audits it', async () => {
|
||||
vi.mocked(mcpService.updateServer).mockResolvedValue({
|
||||
...serverInfo,
|
||||
transport: 'sse',
|
||||
})
|
||||
const res = await app.request('/mcp/servers/server1', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ transport: 'sse' }),
|
||||
})
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({ ...serverInfo, transport: 'sse' })
|
||||
expect(auditLogService.logAction).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ action: 'mcp_server_update', itemId: 'server1' }),
|
||||
)
|
||||
})
|
||||
|
||||
test('updates the permission via the dedicated route', async () => {
|
||||
vi.mocked(mcpService.updateServerPermission).mockResolvedValue({
|
||||
...serverInfo,
|
||||
permission: 'editor',
|
||||
})
|
||||
const res = await app.request('/mcp/servers/server1/permission', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ permission: 'editor' }),
|
||||
})
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({ ...serverInfo, permission: 'editor' })
|
||||
expect(mcpService.updateServerPermission).toHaveBeenCalledWith('server1', 'editor')
|
||||
})
|
||||
|
||||
test('rejects an invalid permission value', async () => {
|
||||
const res = await app.request('/mcp/servers/server1/permission', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ permission: 'superadmin' }),
|
||||
})
|
||||
expect(res.status).toBe(400)
|
||||
})
|
||||
|
||||
test('deletes a server and audits it', async () => {
|
||||
const res = await app.request('/mcp/servers/server1', { method: 'DELETE' })
|
||||
expect(res.status).toBe(204)
|
||||
expect(auditLogService.logAction).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ action: 'mcp_server_delete', itemId: 'server1' }),
|
||||
)
|
||||
})
|
||||
|
||||
test('refreshes a server and returns the updated record', async () => {
|
||||
vi.mocked(mcpService.refreshServer).mockResolvedValue({
|
||||
...serverInfo,
|
||||
status: 'connected',
|
||||
toolCount: 3,
|
||||
instructions: 'Always use the sandbox API and respect rate limits.',
|
||||
})
|
||||
const res = await app.request('/mcp/servers/server1/refresh', { method: 'POST' })
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({
|
||||
...serverInfo,
|
||||
status: 'connected',
|
||||
toolCount: 3,
|
||||
instructions: 'Always use the sandbox API and respect rate limits.',
|
||||
})
|
||||
})
|
||||
|
||||
test('refresh failure returns 502 with the error message', async () => {
|
||||
vi.mocked(mcpService.refreshServer).mockRejectedValue(new Error('boom'))
|
||||
const res = await app.request('/mcp/servers/server1/refresh', { method: 'POST' })
|
||||
expect(res.status).toBe(502)
|
||||
expect(await res.json()).toEqual({ error: 'boom' })
|
||||
})
|
||||
|
||||
test('lists cached tools', async () => {
|
||||
vi.mocked(mcpService.getServerRecord).mockResolvedValue({
|
||||
...serverInfo,
|
||||
lastError: null,
|
||||
lastConnectedAt: null,
|
||||
teamId: 'team1',
|
||||
tools: [{ name: 'echo' }],
|
||||
credential: null,
|
||||
config: null,
|
||||
authConfig: null,
|
||||
} as never)
|
||||
const res = await app.request('/mcp/servers/server1/tools')
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({ tools: [{ name: 'echo' }] })
|
||||
})
|
||||
|
||||
test('starts OAuth auth', async () => {
|
||||
vi.mocked(mcpService.startAuth).mockResolvedValue({
|
||||
authorizationUrl: 'https://auth.example.com/authorize',
|
||||
})
|
||||
const res = await app.request('/mcp/servers/server1/auth/start', { method: 'POST' })
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({
|
||||
authorizationUrl: 'https://auth.example.com/authorize',
|
||||
status: 'in_progress',
|
||||
})
|
||||
})
|
||||
|
||||
test('completes OAuth auth', async () => {
|
||||
vi.mocked(mcpService.completeAuth).mockResolvedValue('authenticated')
|
||||
const res = await app.request('/mcp/servers/server1/auth/complete', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ code: 'auth-code', iss: 'https://issuer.example.com' }),
|
||||
})
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({ status: 'authenticated' })
|
||||
expect(mcpService.completeAuth).toHaveBeenCalledWith('server1', {
|
||||
code: 'auth-code',
|
||||
iss: 'https://issuer.example.com',
|
||||
})
|
||||
})
|
||||
|
||||
test('reports auth status', async () => {
|
||||
vi.mocked(mcpService.getAuthStatus).mockResolvedValue('not_authenticated')
|
||||
const res = await app.request('/mcp/servers/server1/auth/status')
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({ status: 'not_authenticated' })
|
||||
})
|
||||
|
||||
test('removes auth', async () => {
|
||||
const res = await app.request('/mcp/servers/server1/auth', { method: 'DELETE' })
|
||||
expect(res.status).toBe(204)
|
||||
expect(mcpService.removeAuth).toHaveBeenCalledWith('server1')
|
||||
})
|
||||
|
||||
test('tests the server connection', async () => {
|
||||
vi.mocked(mcpService.testServer).mockResolvedValue({ ok: true, toolCount: 3 })
|
||||
const res = await app.request('/mcp/servers/server1/test', { method: 'POST' })
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({ ok: true, toolCount: 3 })
|
||||
})
|
||||
})
|
||||
|
||||
describe('MCP OAuth callback route', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
test('completes a valid callback and renders an HTML page', async () => {
|
||||
vi.mocked(mcpService.handleOauthCallback).mockResolvedValue({ ok: true })
|
||||
const res = await mcpOauthCallbackRoute.request(
|
||||
'/mcp/oauth/callback?code=abc&state=xyz&iss=https%3A%2F%2Fissuer.example.com',
|
||||
)
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('content-type')).toContain('text/html')
|
||||
expect(mcpService.handleOauthCallback).toHaveBeenCalledWith(
|
||||
'abc',
|
||||
'xyz',
|
||||
'https://issuer.example.com',
|
||||
)
|
||||
const body = await res.text()
|
||||
expect(body).toContain('postMessage')
|
||||
expect(body).toContain('"ok":true')
|
||||
})
|
||||
|
||||
test('rejects a callback missing code or state', async () => {
|
||||
const res = await mcpOauthCallbackRoute.request('/mcp/oauth/callback?code=abc')
|
||||
expect(res.status).toBe(400)
|
||||
expect(mcpService.handleOauthCallback).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('surfaces callback failures in the HTML page', async () => {
|
||||
vi.mocked(mcpService.handleOauthCallback).mockResolvedValue({
|
||||
ok: false,
|
||||
message: 'Invalid or expired OAuth state',
|
||||
})
|
||||
const res = await mcpOauthCallbackRoute.request('/mcp/oauth/callback?code=abc&state=bad')
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.text()
|
||||
expect(body).toContain('"ok":false')
|
||||
expect(body).toContain('Invalid or expired OAuth state')
|
||||
})
|
||||
|
||||
test('accepts POST callbacks (form-encoded)', async () => {
|
||||
vi.mocked(mcpService.handleOauthCallback).mockResolvedValue({ ok: true })
|
||||
const res = await mcpOauthCallbackRoute.request('/mcp/oauth/callback', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
body: 'code=abc&state=xyz',
|
||||
})
|
||||
expect(res.status).toBe(200)
|
||||
expect(mcpService.handleOauthCallback).toHaveBeenCalledWith('abc', 'xyz', undefined)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,356 @@
|
||||
import { Hono } from 'hono'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { mcpService } from '@shumai/core/src/mcp/mcp-service'
|
||||
import {
|
||||
createMcpServerRequestSchema,
|
||||
updateMcpServerRequestSchema,
|
||||
updateMcpServerPermissionRequestSchema,
|
||||
mcpAuthCompleteRequestSchema,
|
||||
AuditAction,
|
||||
} from '@shumai/dtos'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
|
||||
const route = new Hono<{ Variables: { user: User } }>()
|
||||
.get('/teams/:teamId/mcp/servers', async (c) => {
|
||||
const user = c.get('user')
|
||||
const teamId = c.req.param('teamId')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Team,
|
||||
id: teamId,
|
||||
})
|
||||
|
||||
const servers = await mcpService.listServers(teamId)
|
||||
return c.json({ servers })
|
||||
})
|
||||
.post(
|
||||
'/teams/:teamId/mcp/servers',
|
||||
zValidator('json', createMcpServerRequestSchema),
|
||||
async (c) => {
|
||||
const user = c.get('user')
|
||||
const teamId = c.req.param('teamId')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Team,
|
||||
id: teamId,
|
||||
})
|
||||
|
||||
const server = await mcpService.createServer(teamId, req)
|
||||
|
||||
await auditLogService.logAction({
|
||||
action: AuditAction.mcp_server_create,
|
||||
teamId,
|
||||
userId: user?.id,
|
||||
itemId: server.id,
|
||||
})
|
||||
|
||||
return c.json(server)
|
||||
},
|
||||
)
|
||||
.get('/mcp/servers/:id', async (c) => {
|
||||
const user = c.get('user')
|
||||
const id = c.req.param('id')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.McpServer,
|
||||
id,
|
||||
})
|
||||
|
||||
const server = await mcpService.getServer(id)
|
||||
if (!server) return c.json({ error: 'MCP server not found' }, 404)
|
||||
return c.json(server)
|
||||
})
|
||||
.patch('/mcp/servers/:id', zValidator('json', updateMcpServerRequestSchema), async (c) => {
|
||||
const user = c.get('user')
|
||||
const id = c.req.param('id')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.McpServer,
|
||||
id,
|
||||
})
|
||||
|
||||
const existing = await mcpService.getServerRecord(id)
|
||||
if (!existing) return c.json({ error: 'MCP server not found' }, 404)
|
||||
|
||||
const server = await mcpService.updateServer(id, req)
|
||||
|
||||
await auditLogService.logAction({
|
||||
action: AuditAction.mcp_server_update,
|
||||
teamId: existing.teamId,
|
||||
userId: user?.id,
|
||||
itemId: id,
|
||||
})
|
||||
|
||||
return c.json(server)
|
||||
})
|
||||
.patch(
|
||||
'/mcp/servers/:id/permission',
|
||||
zValidator('json', updateMcpServerPermissionRequestSchema),
|
||||
async (c) => {
|
||||
const user = c.get('user')
|
||||
const id = c.req.param('id')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.McpServer,
|
||||
id,
|
||||
})
|
||||
|
||||
const existing = await mcpService.getServerRecord(id)
|
||||
if (!existing) return c.json({ error: 'MCP server not found' }, 404)
|
||||
|
||||
const server = await mcpService.updateServerPermission(id, req.permission)
|
||||
|
||||
await auditLogService.logAction({
|
||||
action: AuditAction.mcp_server_update,
|
||||
teamId: existing.teamId,
|
||||
userId: user?.id,
|
||||
itemId: id,
|
||||
})
|
||||
|
||||
return c.json(server)
|
||||
},
|
||||
)
|
||||
.delete('/mcp/servers/:id', async (c) => {
|
||||
const user = c.get('user')
|
||||
const id = c.req.param('id')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.McpServer,
|
||||
id,
|
||||
})
|
||||
|
||||
const existing = await mcpService.getServerRecord(id)
|
||||
|
||||
await mcpService.deleteServer(id)
|
||||
|
||||
if (existing) {
|
||||
await auditLogService.logAction({
|
||||
action: AuditAction.mcp_server_delete,
|
||||
teamId: existing.teamId,
|
||||
userId: user?.id,
|
||||
itemId: id,
|
||||
})
|
||||
}
|
||||
|
||||
return new Response(null, { status: 204 })
|
||||
})
|
||||
.post('/mcp/servers/:id/refresh', async (c) => {
|
||||
const user = c.get('user')
|
||||
const id = c.req.param('id')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.McpServer,
|
||||
id,
|
||||
})
|
||||
|
||||
try {
|
||||
const server = await mcpService.refreshServer(id)
|
||||
return c.json(server)
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
return c.json({ error: message }, 502)
|
||||
}
|
||||
})
|
||||
.get('/mcp/servers/:id/tools', async (c) => {
|
||||
const user = c.get('user')
|
||||
const id = c.req.param('id')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.McpServer,
|
||||
id,
|
||||
})
|
||||
|
||||
const server = await mcpService.getServer(id)
|
||||
if (!server) return c.json({ error: 'MCP server not found' }, 404)
|
||||
const record = await mcpService.getServerRecord(id)
|
||||
return c.json({ tools: record?.tools ?? [] })
|
||||
})
|
||||
.post('/mcp/servers/:id/auth/start', async (c) => {
|
||||
const user = c.get('user')
|
||||
const id = c.req.param('id')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.McpServer,
|
||||
id,
|
||||
})
|
||||
|
||||
try {
|
||||
const result = await mcpService.startAuth(id)
|
||||
if (result.alreadyAuthenticated) {
|
||||
return c.json({ status: 'authenticated' })
|
||||
}
|
||||
return c.json({ authorizationUrl: result.authorizationUrl, status: 'in_progress' })
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
return c.json({ error: message }, 502)
|
||||
}
|
||||
})
|
||||
.post(
|
||||
'/mcp/servers/:id/auth/complete',
|
||||
zValidator('json', mcpAuthCompleteRequestSchema),
|
||||
async (c) => {
|
||||
const user = c.get('user')
|
||||
const id = c.req.param('id')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.McpServer,
|
||||
id,
|
||||
})
|
||||
|
||||
try {
|
||||
const status = await mcpService.completeAuth(id, { code: req.code, iss: req.iss })
|
||||
return c.json({ status })
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
return c.json({ error: message }, 502)
|
||||
}
|
||||
},
|
||||
)
|
||||
.get('/mcp/servers/:id/auth/status', async (c) => {
|
||||
const user = c.get('user')
|
||||
const id = c.req.param('id')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.McpServer,
|
||||
id,
|
||||
})
|
||||
|
||||
try {
|
||||
const status = await mcpService.getAuthStatus(id)
|
||||
return c.json({ status })
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
return c.json({ error: message }, 404)
|
||||
}
|
||||
})
|
||||
.delete('/mcp/servers/:id/auth', async (c) => {
|
||||
const user = c.get('user')
|
||||
const id = c.req.param('id')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.McpServer,
|
||||
id,
|
||||
})
|
||||
|
||||
await mcpService.removeAuth(id)
|
||||
return new Response(null, { status: 204 })
|
||||
})
|
||||
.post('/mcp/servers/:id/test', async (c) => {
|
||||
const user = c.get('user')
|
||||
const id = c.req.param('id')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.McpServer,
|
||||
id,
|
||||
})
|
||||
|
||||
const result = await mcpService.testServer(id)
|
||||
return c.json(result)
|
||||
})
|
||||
|
||||
export default route
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public OAuth callback route (mounted BEFORE authMiddleware in index.ts).
|
||||
// The OAuth provider redirects the user's browser here after approval.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function renderCallbackPage(payload: { ok: boolean; message?: string }): string {
|
||||
const escaped = JSON.stringify(payload).replace(/</g, '\\u003c')
|
||||
return `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>MCP OAuth</title>
|
||||
</head>
|
||||
<body>
|
||||
<script>
|
||||
(function () {
|
||||
var payload = ${escaped};
|
||||
try {
|
||||
if (window.opener) {
|
||||
window.opener.postMessage({ source: 'shumai-mcp-oauth', payload: payload }, '*');
|
||||
}
|
||||
} catch (e) {
|
||||
/* ignore cross-origin postMessage failures */
|
||||
}
|
||||
setTimeout(function () {
|
||||
window.close();
|
||||
document.body.textContent = payload.ok ? 'Authentication complete. You can close this window.' : 'Authentication failed: ' + (payload.message || 'unknown error');
|
||||
}, 300);
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>`
|
||||
}
|
||||
|
||||
export const mcpOauthCallbackRoute = new Hono()
|
||||
.get('/mcp/oauth/callback', async (c) => {
|
||||
const code = c.req.query('code')
|
||||
const state = c.req.query('state')
|
||||
const iss = c.req.query('iss') || undefined
|
||||
|
||||
if (!code || !state) {
|
||||
return c.html(
|
||||
renderCallbackPage({ ok: false, message: 'Missing code or state parameter' }),
|
||||
400,
|
||||
)
|
||||
}
|
||||
|
||||
const result = await mcpService.handleOauthCallback(code, state, iss)
|
||||
return c.html(
|
||||
renderCallbackPage(result.ok ? { ok: true } : { ok: false, message: result.message }),
|
||||
)
|
||||
})
|
||||
.post('/mcp/oauth/callback', async (c) => {
|
||||
const body = await c.req.parseBody()
|
||||
const code = typeof body['code'] === 'string' ? body['code'] : undefined
|
||||
const state = typeof body['state'] === 'string' ? body['state'] : undefined
|
||||
const iss = typeof body['iss'] === 'string' ? body['iss'] : undefined
|
||||
|
||||
if (!code || !state) {
|
||||
return c.html(
|
||||
renderCallbackPage({ ok: false, message: 'Missing code or state parameter' }),
|
||||
400,
|
||||
)
|
||||
}
|
||||
|
||||
const result = await mcpService.handleOauthCallback(code, state, iss)
|
||||
return c.html(
|
||||
renderCallbackPage(result.ok ? { ok: true } : { ok: false, message: result.message }),
|
||||
)
|
||||
})
|
||||
@@ -46,10 +46,9 @@ describe('metadata api', () => {
|
||||
const mockField = {
|
||||
key: 'field1',
|
||||
scope: 'TEAM',
|
||||
config: { name: 'Test Field', type: 'text' },
|
||||
config: { name: 'Test Field', type: 'text', autofillSource: 'CONTENT' },
|
||||
readOnly: false,
|
||||
description: 'desc',
|
||||
aiAutofill: true,
|
||||
}
|
||||
|
||||
// Using any here because mocking complex service return types or Hono context is overly verbose for this test.
|
||||
@@ -62,7 +61,7 @@ describe('metadata api', () => {
|
||||
expect(json).toHaveLength(1)
|
||||
expect(json[0].id).toBe('field1')
|
||||
expect(json[0].config.name).toBe('Test Field')
|
||||
expect(json[0].aiAutofill).toBe(true)
|
||||
expect(json[0].config.autofillSource).toBe('CONTENT')
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: expect.anything(),
|
||||
permission: Permission.Read,
|
||||
@@ -75,10 +74,9 @@ describe('metadata api', () => {
|
||||
const mockField = {
|
||||
key: 'newfield',
|
||||
scope: 'TEAM',
|
||||
config: { name: 'New Field', type: 'text' },
|
||||
config: { name: 'New Field', type: 'text', autofillSource: 'CONTENT' },
|
||||
readOnly: false,
|
||||
description: 'desc',
|
||||
aiAutofill: true,
|
||||
}
|
||||
|
||||
// Using any here because mocking complex service return types or Hono context is overly verbose for this test.
|
||||
@@ -88,8 +86,7 @@ describe('metadata api', () => {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
config: { name: 'New Field', type: 'text' },
|
||||
aiAutofill: true,
|
||||
config: { name: 'New Field', type: 'text', autofillSource: 'CONTENT' },
|
||||
description: 'desc',
|
||||
}),
|
||||
})
|
||||
@@ -115,10 +112,9 @@ describe('metadata api', () => {
|
||||
const mockField = {
|
||||
key: 'field1',
|
||||
scope: 'PROJECT',
|
||||
config: { name: 'Project Field', type: 'text' },
|
||||
config: { name: 'Project Field', type: 'text', autofillSource: 'NONE' },
|
||||
readOnly: false,
|
||||
description: 'desc',
|
||||
aiAutofill: false,
|
||||
}
|
||||
|
||||
vi.mocked(metadataService.listProjectFields).mockResolvedValue([
|
||||
@@ -198,10 +194,9 @@ describe('metadata api', () => {
|
||||
const mockField = {
|
||||
key: 'f1',
|
||||
scope: 'TEAM',
|
||||
config: { name: 'Updated Field', type: 'text' },
|
||||
config: { name: 'Updated Field', type: 'text', autofillSource: 'CONTENT' },
|
||||
readOnly: false,
|
||||
description: 'desc',
|
||||
aiAutofill: true,
|
||||
teamId: 't1',
|
||||
}
|
||||
|
||||
@@ -214,8 +209,7 @@ describe('metadata api', () => {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
config: { name: 'Updated Field', type: 'text' },
|
||||
aiAutofill: true,
|
||||
config: { name: 'Updated Field', type: 'text', autofillSource: 'CONTENT' },
|
||||
description: 'desc',
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -27,7 +27,6 @@ function toFieldInfo(
|
||||
readOnly: f.readOnly,
|
||||
visible: visible,
|
||||
description: f.description,
|
||||
aiAutofill: f.aiAutofill,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { Context } from 'hono'
|
||||
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 { z } from 'zod'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
@@ -18,7 +19,65 @@ import {
|
||||
ShareLinkDisabledError,
|
||||
ShareLinkExpiredError,
|
||||
ShareLinkPasswordInvalidError,
|
||||
ShareLinkDownloadDisabledError,
|
||||
} from '@shumai/core/src/share/errors'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
|
||||
/**
|
||||
* Replaces the served media entries with the watermarked proxy entries produced
|
||||
* by the watermark transcode activity. When no completed watermark proxy exists
|
||||
* yet (still transcoding, or failed), the video/image transcode slots are emptied
|
||||
* so original, unwatermarked proxies are never exposed on the public share.
|
||||
*
|
||||
* Only video/image assets are subject to watermark protection; other media types
|
||||
* (pdf, audio, ...) never receive watermark transcodes and pass through untouched.
|
||||
* `original`, `thumbnail` and `videoPreview` are intentionally left untouched here.
|
||||
* S3 GET URLs are generated for all watermarked proxies.
|
||||
*/
|
||||
async function applyWatermarkToAssetMedia(
|
||||
asset: AssetInfo,
|
||||
watermarkMedia: PrismaJson.MediaInfo | null,
|
||||
): Promise<AssetInfo> {
|
||||
if (!asset.media) return asset
|
||||
if (asset.proxyType !== 'video' && asset.proxyType !== 'image') return asset
|
||||
|
||||
const updatedMedia = { ...asset.media }
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
|
||||
updatedMedia.videoTranscodes =
|
||||
watermarkMedia?.videoTranscodes && watermarkMedia.videoTranscodes.length > 0
|
||||
? await Promise.all(
|
||||
watermarkMedia.videoTranscodes.map(async (vt) => ({
|
||||
id: vt.key ?? '',
|
||||
url: vt.key ? await s3Service.presign(bucket, vt.key, 'GET') : '',
|
||||
key: vt.key ?? '',
|
||||
width: vt.width ?? 0,
|
||||
height: vt.height ?? 0,
|
||||
size: 0,
|
||||
})),
|
||||
)
|
||||
: []
|
||||
|
||||
updatedMedia.imageTranscodes =
|
||||
watermarkMedia?.imageTranscodes && watermarkMedia.imageTranscodes.length > 0
|
||||
? await Promise.all(
|
||||
watermarkMedia.imageTranscodes.map(async (it) => ({
|
||||
id: it.key ?? '',
|
||||
url: it.key ? await s3Service.presign(bucket, it.key, 'GET') : '',
|
||||
key: it.key ?? '',
|
||||
width: it.width ?? 0,
|
||||
height: it.height ?? 0,
|
||||
size: 0,
|
||||
})),
|
||||
)
|
||||
: []
|
||||
|
||||
return {
|
||||
...asset,
|
||||
media: updatedMedia,
|
||||
}
|
||||
}
|
||||
|
||||
const app = new Hono()
|
||||
|
||||
@@ -34,7 +93,6 @@ function toFieldInfo(
|
||||
readOnly: f.readOnly,
|
||||
visible: visible,
|
||||
description: f.description,
|
||||
aiAutofill: f.aiAutofill,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +104,10 @@ export function handlePublicShareError(c: Context, err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : String(err)
|
||||
return c.json({ error: msg }, 403)
|
||||
}
|
||||
if (err instanceof ShareLinkDownloadDisabledError) {
|
||||
const msg = err instanceof Error ? err.message : String(err)
|
||||
return c.json({ error: msg }, 403)
|
||||
}
|
||||
if (err instanceof ShareLinkNotFoundError) {
|
||||
const msg = err instanceof Error ? err.message : String(err)
|
||||
return c.json({ error: msg }, 404)
|
||||
@@ -70,6 +132,7 @@ const route = app
|
||||
name: shareLink.name,
|
||||
expireAt: shareLink.expireAt,
|
||||
isDisabled: shareLink.isDisabled,
|
||||
allowDownload: shareLink.allowDownload,
|
||||
isExpired: shareLink.isExpired,
|
||||
hasPassword: shareLink.hasPassword,
|
||||
rootFolderId: shareLink.rootFolderId,
|
||||
@@ -129,6 +192,24 @@ const route = app
|
||||
order,
|
||||
})
|
||||
|
||||
if (shareLink.watermarkConfigId && res.data.length > 0) {
|
||||
// Resolve symlink targets once and reuse for the media lookup, so each
|
||||
// item costs a single query instead of two.
|
||||
const targetIds = await Promise.all(
|
||||
res.data.map((item) => assetService.resolveTargetAssetId(item.id)),
|
||||
)
|
||||
const wfMap = await watermarkService.getCompletedWatermarkMediaMap(
|
||||
targetIds,
|
||||
shareLink.watermarkConfigId,
|
||||
)
|
||||
|
||||
res.data = await Promise.all(
|
||||
res.data.map(async (item, index) =>
|
||||
applyWatermarkToAssetMedia(item, wfMap.get(targetIds[index]) ?? null),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return c.json(res)
|
||||
} catch (err) {
|
||||
return handlePublicShareError(c, err)
|
||||
@@ -140,9 +221,24 @@ const route = app
|
||||
const password = c.req.header('x-share-password')
|
||||
|
||||
try {
|
||||
await shareService.verifyPublicAccess(fileId, password)
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
|
||||
let asset = await assetService.getAsset({ assetId: fileId })
|
||||
|
||||
// Only video/image assets are subject to watermark protection; skip the
|
||||
// watermark lookup entirely for other media types (pdf, audio, ...).
|
||||
if (
|
||||
shareLink.watermarkConfigId &&
|
||||
(asset.proxyType === 'video' || asset.proxyType === 'image')
|
||||
) {
|
||||
const targetAssetId = await assetService.resolveTargetAssetId(fileId)
|
||||
const wfMap = await watermarkService.getCompletedWatermarkMediaMap(
|
||||
[targetAssetId],
|
||||
shareLink.watermarkConfigId,
|
||||
)
|
||||
asset = await applyWatermarkToAssetMedia(asset, wfMap.get(targetAssetId) ?? null)
|
||||
}
|
||||
|
||||
const asset = await assetService.getAsset({ assetId: fileId })
|
||||
return c.json(asset)
|
||||
} catch (err) {
|
||||
return handlePublicShareError(c, err)
|
||||
@@ -157,7 +253,11 @@ const route = app
|
||||
const { key } = c.req.valid('json')
|
||||
|
||||
try {
|
||||
await shareService.verifyPublicAccess(fileId, password)
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
|
||||
if (!shareLink.allowDownload) {
|
||||
throw new ShareLinkDownloadDisabledError('Download is disabled for this share link')
|
||||
}
|
||||
|
||||
if (!key.startsWith('files/')) {
|
||||
return c.json({ error: 'Invalid key' }, 400)
|
||||
|
||||
@@ -39,7 +39,7 @@ describe('S3 API', () => {
|
||||
}
|
||||
})
|
||||
|
||||
it('GET /files/* sets Content-Disposition: attachment only when download=1 query param is present', async () => {
|
||||
it('GET /files/* sets Content-Disposition with filename when download=1 & filename query params are present', async () => {
|
||||
// We need to capture the onFound callback from serveStatic options
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let onFoundCallback: ((path: string, c: any) => void) | undefined
|
||||
@@ -54,19 +54,36 @@ describe('S3 API', () => {
|
||||
const { default: route } = await import('./s3')
|
||||
const app = new Hono().route('/files', route)
|
||||
|
||||
await app.request('/files/b1/test.webp?download=1')
|
||||
await app.request('/files/b1/test.webp?download=1&filename=foo.png')
|
||||
|
||||
expect(onFoundCallback).toBeDefined()
|
||||
|
||||
// Test with download=1
|
||||
// Test with download=1 and filename
|
||||
const mockContextWithDownload = {
|
||||
req: { query: (key: string) => (key === 'download' ? '1' : undefined) },
|
||||
req: {
|
||||
query: (key: string) =>
|
||||
key === 'download' ? '1' : key === 'filename' ? 'foo.png' : undefined,
|
||||
},
|
||||
header: vi.fn(),
|
||||
}
|
||||
|
||||
if (onFoundCallback) {
|
||||
onFoundCallback('/files/b1/test.webp', mockContextWithDownload)
|
||||
expect(mockContextWithDownload.header).toHaveBeenCalledWith(
|
||||
'Content-Disposition',
|
||||
'attachment; filename="foo.png"; filename*=UTF-8\'\'foo.png',
|
||||
)
|
||||
}
|
||||
|
||||
// Test with download=1 but no filename
|
||||
const mockContextDownloadNoFilename = {
|
||||
req: { query: (key: string) => (key === 'download' ? '1' : undefined) },
|
||||
header: vi.fn(),
|
||||
}
|
||||
|
||||
if (onFoundCallback) {
|
||||
onFoundCallback('/files/b1/test.webp', mockContextDownloadNoFilename)
|
||||
expect(mockContextDownloadNoFilename.header).toHaveBeenCalledWith(
|
||||
'Content-Disposition',
|
||||
'attachment',
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Hono } from 'hono'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { buildContentDisposition, s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { serveStatic } from 'hono/bun'
|
||||
|
||||
const route = new Hono()
|
||||
@@ -10,7 +10,7 @@ const route = new Hono()
|
||||
rewriteRequestPath: (path) => path.replace(/^\/files\//, ''),
|
||||
onFound: (_path, c) => {
|
||||
if (c.req.query('download') === '1') {
|
||||
c.header('Content-Disposition', 'attachment')
|
||||
c.header('Content-Disposition', buildContentDisposition(c.req.query('filename')))
|
||||
}
|
||||
},
|
||||
}),
|
||||
|
||||
@@ -9,6 +9,8 @@ import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/a
|
||||
import { ShareLinkPasswordInvalidError, ShareLinkExpiredError } from '@shumai/core/src/share/errors'
|
||||
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
import { watermarkService } from '@shumai/core/src/watermark/watermark'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
|
||||
vi.mock('./middleware/auth', () => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
@@ -47,6 +49,35 @@ vi.mock('@shumai/core/src/project/project', () => ({
|
||||
},
|
||||
}))
|
||||
|
||||
function watermarkMediaInfo(overrides: Partial<PrismaJson.MediaInfo> = {}): PrismaJson.MediaInfo {
|
||||
return {
|
||||
duration: 1,
|
||||
filesize: 100,
|
||||
frames: 30,
|
||||
proxyType: 'video',
|
||||
imageTranscodes: [],
|
||||
videoTranscodes: [],
|
||||
finishedAt: new Date().toISOString(),
|
||||
metadata: {
|
||||
originalHeight: 360,
|
||||
originalWidth: 640,
|
||||
hasAudio: false,
|
||||
duration: 1,
|
||||
bitRate: 0,
|
||||
frameRate: 30,
|
||||
totalFrames: 30,
|
||||
startTimecode: '00:00:00:00',
|
||||
format: {},
|
||||
},
|
||||
original: {
|
||||
key: 'files/orig.mp4',
|
||||
filesizeInBytes: 100,
|
||||
codec: '',
|
||||
},
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
describe('Share API', () => {
|
||||
const app = new Hono()
|
||||
.route('/', publicShareRoute)
|
||||
@@ -65,6 +96,7 @@ describe('Share API', () => {
|
||||
id: 'share1',
|
||||
name: 'Public Share',
|
||||
isDisabled: false,
|
||||
allowDownload: false,
|
||||
isExpired: false,
|
||||
hasPassword: true,
|
||||
rootFolderId: 'folder1',
|
||||
@@ -88,6 +120,7 @@ describe('Share API', () => {
|
||||
const body = await res.json()
|
||||
expect(body.name).toBe('Public Share')
|
||||
expect(body.hasPassword).toBe(true)
|
||||
expect(body.allowDownload).toBe(false)
|
||||
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('folder1', 'pass')
|
||||
})
|
||||
|
||||
@@ -96,6 +129,7 @@ describe('Share API', () => {
|
||||
id: 'share1',
|
||||
name: 'Public Share',
|
||||
isDisabled: false,
|
||||
allowDownload: false,
|
||||
isExpired: false,
|
||||
hasPassword: true,
|
||||
rootFolderId: 'folder1',
|
||||
@@ -122,6 +156,7 @@ describe('Share API', () => {
|
||||
id: 'share1',
|
||||
name: 'Public Share',
|
||||
isDisabled: false,
|
||||
allowDownload: false,
|
||||
isExpired: false,
|
||||
hasPassword: true,
|
||||
rootFolderId: 'folder1',
|
||||
@@ -149,6 +184,7 @@ describe('Share API', () => {
|
||||
id: 'share1',
|
||||
name: 'My Share',
|
||||
isDisabled: false,
|
||||
allowDownload: false,
|
||||
rootFolderId: 'folder1',
|
||||
projectId: 'project1',
|
||||
password: null,
|
||||
@@ -180,6 +216,291 @@ describe('Share API', () => {
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
test('applies per-file watermark proxies when watermark enabled', async () => {
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
|
||||
id: 'share1',
|
||||
name: 'My Share',
|
||||
isDisabled: false,
|
||||
allowDownload: false,
|
||||
rootFolderId: 'folder1',
|
||||
projectId: 'project1',
|
||||
password: null,
|
||||
expireAt: null,
|
||||
watermarkConfigId: 'cfg1',
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any)
|
||||
vi.spyOn(assetService, 'listChildren').mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
id: 'video-ready',
|
||||
name: 'a.mp4',
|
||||
sizeByte: 1,
|
||||
fileCount: 0,
|
||||
type: 'file',
|
||||
status: 'processed',
|
||||
proxyType: 'video',
|
||||
media: {
|
||||
proxyType: 'video',
|
||||
videoTranscodes: [
|
||||
{
|
||||
id: 'orig',
|
||||
url: 'u',
|
||||
key: 'files/orig.mp4',
|
||||
width: 640,
|
||||
height: 360,
|
||||
size: 1,
|
||||
},
|
||||
],
|
||||
imageTranscodes: [],
|
||||
original: { key: 'files/orig.mp4' },
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'video-pending',
|
||||
name: 'b.mp4',
|
||||
sizeByte: 1,
|
||||
fileCount: 0,
|
||||
type: 'file',
|
||||
status: 'processed',
|
||||
proxyType: 'video',
|
||||
media: {
|
||||
proxyType: 'video',
|
||||
videoTranscodes: [
|
||||
{
|
||||
id: 'orig',
|
||||
url: 'u',
|
||||
key: 'files/orig.mp4',
|
||||
width: 640,
|
||||
height: 360,
|
||||
size: 1,
|
||||
},
|
||||
],
|
||||
imageTranscodes: [],
|
||||
original: { key: 'files/orig.mp4' },
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'doc',
|
||||
name: 'c.pdf',
|
||||
sizeByte: 1,
|
||||
fileCount: 0,
|
||||
type: 'file',
|
||||
status: 'processed',
|
||||
proxyType: 'pdf',
|
||||
media: {
|
||||
proxyType: 'pdf',
|
||||
pdfTranscode: { url: 'u', key: 'files/doc.pdf' },
|
||||
videoTranscodes: [],
|
||||
imageTranscodes: [],
|
||||
original: { key: 'files/doc.pdf' },
|
||||
},
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
] as any,
|
||||
pageInfo: { total: 3, cursor: undefined },
|
||||
})
|
||||
vi.spyOn(assetService, 'resolveTargetAssetId').mockImplementation(async (id) => id)
|
||||
vi.spyOn(s3Service, 'presign').mockResolvedValue('http://s3/presigned')
|
||||
vi.spyOn(watermarkService, 'getCompletedWatermarkMediaMap').mockResolvedValue(
|
||||
new Map([
|
||||
[
|
||||
'video-ready',
|
||||
watermarkMediaInfo({
|
||||
videoTranscodes: [{ key: 'files/wm-a.mp4', width: 640, height: 360 }],
|
||||
}),
|
||||
],
|
||||
]),
|
||||
)
|
||||
|
||||
const res = await app.request('/shares/share1/folders/folder1/children?assetType=file', {
|
||||
method: 'GET',
|
||||
headers: { 'x-share-password': 'pass' },
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.json()
|
||||
// Ready item → watermarked proxy
|
||||
expect(body.data[0].media.videoTranscodes).toEqual([
|
||||
{
|
||||
id: 'files/wm-a.mp4',
|
||||
url: 'http://s3/presigned',
|
||||
key: 'files/wm-a.mp4',
|
||||
width: 640,
|
||||
height: 360,
|
||||
size: 0,
|
||||
},
|
||||
])
|
||||
// Pending item → original transcodes emptied
|
||||
expect(body.data[1].media.videoTranscodes).toEqual([])
|
||||
expect(body.data[1].media.imageTranscodes).toEqual([])
|
||||
// Non-media item → untouched
|
||||
expect(body.data[2].media.pdfTranscode).toEqual({ url: 'u', key: 'files/doc.pdf' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /shares/:shareId/files/:fileId', () => {
|
||||
const shareLinkWithWatermark = {
|
||||
id: 'share1',
|
||||
name: 'My Share',
|
||||
isDisabled: false,
|
||||
allowDownload: false,
|
||||
rootFolderId: 'folder1',
|
||||
projectId: 'project1',
|
||||
password: null,
|
||||
expireAt: null,
|
||||
watermarkConfigId: 'cfg1',
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any
|
||||
|
||||
const videoAsset = {
|
||||
id: 'file1',
|
||||
name: 'video.mp4',
|
||||
sizeByte: 1024,
|
||||
fileCount: 0,
|
||||
type: 'file',
|
||||
status: 'processed',
|
||||
proxyType: 'video',
|
||||
media: {
|
||||
proxyType: 'video',
|
||||
videoTranscodes: [
|
||||
{
|
||||
id: 'orig-360p',
|
||||
url: 'http://s3/original-360p',
|
||||
key: 'files/original-360p.mp4',
|
||||
width: 640,
|
||||
height: 360,
|
||||
size: 100,
|
||||
},
|
||||
],
|
||||
imageTranscodes: [],
|
||||
original: { key: 'files/original.mp4' },
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any
|
||||
|
||||
beforeEach(() => {
|
||||
vi.spyOn(assetService, 'getAsset').mockResolvedValue(videoAsset)
|
||||
vi.spyOn(assetService, 'resolveTargetAssetId').mockResolvedValue('file1')
|
||||
vi.spyOn(s3Service, 'presign').mockResolvedValue('http://s3/presigned')
|
||||
})
|
||||
|
||||
test('serves watermarked transcodes when watermark enabled and completed', async () => {
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue(shareLinkWithWatermark)
|
||||
vi.spyOn(watermarkService, 'getCompletedWatermarkMediaMap').mockResolvedValue(
|
||||
new Map([
|
||||
[
|
||||
'file1',
|
||||
watermarkMediaInfo({
|
||||
videoTranscodes: [{ key: 'files/watermarked-360p.mp4', width: 640, height: 360 }],
|
||||
}),
|
||||
],
|
||||
]),
|
||||
)
|
||||
|
||||
const res = await app.request('/shares/share1/files/file1', {
|
||||
method: 'GET',
|
||||
headers: { 'x-share-password': 'pass' },
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.json()
|
||||
expect(body.media.videoTranscodes).toEqual([
|
||||
{
|
||||
id: 'files/watermarked-360p.mp4',
|
||||
url: 'http://s3/presigned',
|
||||
key: 'files/watermarked-360p.mp4',
|
||||
width: 640,
|
||||
height: 360,
|
||||
size: 0,
|
||||
},
|
||||
])
|
||||
expect(body.media.imageTranscodes).toEqual([])
|
||||
})
|
||||
|
||||
test('serves empty transcode arrays while watermark transcoding is in flight', async () => {
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue(shareLinkWithWatermark)
|
||||
vi.spyOn(watermarkService, 'getCompletedWatermarkMediaMap').mockResolvedValue(new Map())
|
||||
|
||||
const res = await app.request('/shares/share1/files/file1', {
|
||||
method: 'GET',
|
||||
headers: { 'x-share-password': 'pass' },
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.json()
|
||||
expect(body.media.videoTranscodes).toEqual([])
|
||||
expect(body.media.imageTranscodes).toEqual([])
|
||||
})
|
||||
|
||||
test('leaves non-video/image media untouched when watermark enabled', async () => {
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue(shareLinkWithWatermark)
|
||||
vi.spyOn(assetService, 'getAsset').mockResolvedValue({
|
||||
id: 'file1',
|
||||
name: 'doc.pdf',
|
||||
sizeByte: 1024,
|
||||
fileCount: 0,
|
||||
type: 'file',
|
||||
status: 'processed',
|
||||
proxyType: 'pdf',
|
||||
media: {
|
||||
proxyType: 'pdf',
|
||||
pdfTranscode: { url: 'http://s3/doc', key: 'files/doc.pdf' },
|
||||
videoTranscodes: [],
|
||||
imageTranscodes: [],
|
||||
original: { key: 'files/doc.pdf' },
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any)
|
||||
const resolveSpy = vi.spyOn(assetService, 'resolveTargetAssetId')
|
||||
const wfSpy = vi.spyOn(watermarkService, 'getCompletedWatermarkMediaMap')
|
||||
|
||||
const res = await app.request('/shares/share1/files/file1', {
|
||||
method: 'GET',
|
||||
headers: { 'x-share-password': 'pass' },
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.json()
|
||||
expect(body.media.pdfTranscode).toEqual({ url: 'http://s3/doc', key: 'files/doc.pdf' })
|
||||
expect(resolveSpy).not.toHaveBeenCalled()
|
||||
expect(wfSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('serves original media unchanged when watermark disabled', async () => {
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
|
||||
id: 'share1',
|
||||
name: 'My Share',
|
||||
isDisabled: false,
|
||||
allowDownload: false,
|
||||
rootFolderId: 'folder1',
|
||||
projectId: 'project1',
|
||||
password: null,
|
||||
expireAt: null,
|
||||
watermarkConfigId: null,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any)
|
||||
const wfSpy = vi.spyOn(watermarkService, 'getCompletedWatermarkMediaMap')
|
||||
|
||||
const res = await app.request('/shares/share1/files/file1', {
|
||||
method: 'GET',
|
||||
headers: { 'x-share-password': 'pass' },
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.json()
|
||||
expect(body.media.videoTranscodes).toEqual([
|
||||
{
|
||||
id: 'orig-360p',
|
||||
url: 'http://s3/original-360p',
|
||||
key: 'files/original-360p.mp4',
|
||||
width: 640,
|
||||
height: 360,
|
||||
size: 100,
|
||||
},
|
||||
])
|
||||
expect(wfSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
// Protected Routes
|
||||
@@ -189,6 +510,7 @@ describe('Share API', () => {
|
||||
id: 'share1',
|
||||
name: 'test-share',
|
||||
isDisabled: false,
|
||||
allowDownload: false,
|
||||
rootFolderId: 'root1',
|
||||
projectId: 'project1',
|
||||
isExpired: false,
|
||||
@@ -226,6 +548,34 @@ describe('Share API', () => {
|
||||
itemId: 'share1',
|
||||
})
|
||||
})
|
||||
|
||||
test('Passes allowDownload to service', async () => {
|
||||
vi.spyOn(shareService, 'createShareLink').mockResolvedValue({
|
||||
id: 'share1',
|
||||
name: 'test-share',
|
||||
isDisabled: false,
|
||||
allowDownload: false,
|
||||
rootFolderId: 'root1',
|
||||
projectId: 'project1',
|
||||
isExpired: false,
|
||||
hasPassword: false,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
})
|
||||
|
||||
const res = await app.request('/projects/project1/shares', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ name: 'My Share', allowDownload: false }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
expect(shareService.createShareLink).toHaveBeenCalledWith(
|
||||
'project1',
|
||||
{ name: 'My Share', allowDownload: false },
|
||||
'user1',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /projects/:projectId/shares', () => {
|
||||
@@ -261,6 +611,7 @@ describe('Share API', () => {
|
||||
id: 'share1',
|
||||
name: 'test-share',
|
||||
isDisabled: false,
|
||||
allowDownload: false,
|
||||
rootFolderId: 'root1',
|
||||
projectId: 'project1',
|
||||
isExpired: false,
|
||||
@@ -291,6 +642,7 @@ describe('Share API', () => {
|
||||
id: 'share1',
|
||||
name: 'test-share',
|
||||
isDisabled: false,
|
||||
allowDownload: false,
|
||||
rootFolderId: 'root1',
|
||||
projectId: 'project1',
|
||||
isExpired: false,
|
||||
@@ -326,4 +678,147 @@ describe('Share API', () => {
|
||||
expect(shareService.addAssetToShare).toHaveBeenCalledWith('share1', { assetIds: ['asset1'] })
|
||||
})
|
||||
})
|
||||
|
||||
describe('Watermark API', () => {
|
||||
test('PUT /shares/:shareId/watermark success', async () => {
|
||||
const { watermarkService } = await import('@shumai/core/src/watermark/watermark')
|
||||
vi.spyOn(watermarkService, 'updateShareLinkWatermark').mockResolvedValue({
|
||||
id: 'share1',
|
||||
name: 'test-share',
|
||||
isDisabled: false,
|
||||
allowDownload: false,
|
||||
rootFolderId: 'root1',
|
||||
projectId: 'project1',
|
||||
isExpired: false,
|
||||
hasPassword: false,
|
||||
watermarkStatus: 'processing',
|
||||
watermarkConfigId: 'cfg1',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
})
|
||||
|
||||
const res = await app.request('/shares/share1/watermark', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
enabled: true,
|
||||
config: {
|
||||
blocks: [
|
||||
{
|
||||
id: 'b1',
|
||||
type: 'text',
|
||||
x: 0.5,
|
||||
y: 0.5,
|
||||
opacity: 0.5,
|
||||
rotation: 0,
|
||||
text: 'TEST',
|
||||
size: 0.1,
|
||||
color: '#FFF',
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.json()
|
||||
expect(body.watermarkStatus).toBe('processing')
|
||||
})
|
||||
|
||||
test('PUT /shares/:shareId/watermark conflict 409', async () => {
|
||||
const { watermarkService } = await import('@shumai/core/src/watermark/watermark')
|
||||
const { ShareLinkWatermarkProcessingError } = await import('@shumai/core/src/share/errors')
|
||||
vi.spyOn(watermarkService, 'updateShareLinkWatermark').mockRejectedValue(
|
||||
new ShareLinkWatermarkProcessingError(),
|
||||
)
|
||||
|
||||
const res = await app.request('/shares/share1/watermark', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ enabled: false }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(409)
|
||||
const body = await res.json()
|
||||
expect(body.error).toBe('Watermark transcoding is currently in progress')
|
||||
})
|
||||
|
||||
test('PUT /shares/:shareId/watermark without config when enabled returns 400', async () => {
|
||||
const res = await app.request('/shares/share1/watermark', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ enabled: true }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(400)
|
||||
})
|
||||
|
||||
test('GET /shares/:shareId/watermark success', async () => {
|
||||
const { watermarkService } = await import('@shumai/core/src/watermark/watermark')
|
||||
vi.spyOn(watermarkService, 'getShareLinkWatermark').mockResolvedValue({
|
||||
watermarkStatus: 'ready',
|
||||
watermarkConfig: {
|
||||
id: 'cfg1',
|
||||
config: { blocks: [] },
|
||||
hash: 'h1',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
})
|
||||
|
||||
const res = await app.request('/shares/share1/watermark', {
|
||||
method: 'GET',
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.json()
|
||||
expect(body.watermarkStatus).toBe('ready')
|
||||
expect(body.watermarkConfig.hash).toBe('h1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /shares/:shareId/files/:fileId/download-url', () => {
|
||||
test('Success for symlink asset in share', async () => {
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
|
||||
allowDownload: true,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any)
|
||||
vi.spyOn(assetService, 'getDownloadUrl').mockResolvedValue('https://s3.example.com/download')
|
||||
|
||||
const res = await app.request('/shares/share1/files/symlink1/download-url', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ key: 'files/target1/fly.png' }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.json()
|
||||
expect(body.url).toBe('https://s3.example.com/download')
|
||||
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('symlink1', undefined)
|
||||
expect(assetService.getDownloadUrl).toHaveBeenCalledWith('symlink1', 'files/target1/fly.png')
|
||||
})
|
||||
|
||||
test('Rejects when allowDownload is disabled', async () => {
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
|
||||
id: 'share1',
|
||||
name: 'No Download',
|
||||
allowDownload: false,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any)
|
||||
const getDownloadUrlSpy = vi
|
||||
.spyOn(assetService, 'getDownloadUrl')
|
||||
.mockResolvedValue('https://s3.example.com/download')
|
||||
|
||||
const res = await app.request('/shares/share1/files/symlink1/download-url', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ key: 'files/target1/fly.png' }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(403)
|
||||
const body = await res.json()
|
||||
expect(body.error).toBe('Download is disabled for this share link')
|
||||
expect(getDownloadUrlSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -8,10 +8,16 @@ import {
|
||||
updateShareLinkRequestSchema,
|
||||
listShareLinksRequestSchema,
|
||||
addAssetToShareRequestSchema,
|
||||
updateShareLinkWatermarkRequestSchema,
|
||||
AuditAction,
|
||||
} from '@shumai/dtos'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
import { watermarkService } from '@shumai/core/src/watermark/watermark'
|
||||
import {
|
||||
ShareLinkNotFoundError,
|
||||
ShareLinkWatermarkProcessingError,
|
||||
} from '@shumai/core/src/share/errors'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
|
||||
@@ -183,5 +189,52 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
await shareService.removeAssetFromShare(shareId, assetId)
|
||||
return c.body(null, 204)
|
||||
})
|
||||
.put(
|
||||
'/shares/:shareId/watermark',
|
||||
zValidator('json', updateShareLinkWatermarkRequestSchema),
|
||||
async (c) => {
|
||||
const shareId = c.req.param('shareId')
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Share,
|
||||
id: shareId,
|
||||
})
|
||||
|
||||
try {
|
||||
const updated = await watermarkService.updateShareLinkWatermark(
|
||||
shareId,
|
||||
req.enabled,
|
||||
req.config,
|
||||
)
|
||||
return c.json(updated)
|
||||
} catch (err) {
|
||||
if (err instanceof ShareLinkWatermarkProcessingError) {
|
||||
return c.json({ error: err.message }, 409)
|
||||
}
|
||||
if (err instanceof ShareLinkNotFoundError) {
|
||||
return c.json({ error: err.message }, 404)
|
||||
}
|
||||
throw err
|
||||
}
|
||||
},
|
||||
)
|
||||
.get('/shares/:shareId/watermark', async (c) => {
|
||||
const shareId = c.req.param('shareId')
|
||||
const user = c.get('user')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Share,
|
||||
id: shareId,
|
||||
})
|
||||
|
||||
const res = await watermarkService.getShareLinkWatermark(shareId)
|
||||
return c.json(res)
|
||||
})
|
||||
|
||||
export default route
|
||||
|
||||
@@ -292,6 +292,7 @@ describe('team api', () => {
|
||||
|
||||
it('GET /teams/:teamId/sandbox returns sandbox settings', async () => {
|
||||
mockGetSandboxSettings.mockResolvedValue({
|
||||
networkSandboxEnabled: false,
|
||||
allowedDomains: ['example.com'],
|
||||
pendingDomains: ['pending.com'],
|
||||
})
|
||||
@@ -300,6 +301,7 @@ describe('team api', () => {
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data.networkSandboxEnabled).toBe(false)
|
||||
expect(data.allowedDomains).toEqual(['example.com'])
|
||||
expect(data.pendingDomains).toEqual(['pending.com'])
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||
@@ -314,6 +316,7 @@ describe('team api', () => {
|
||||
|
||||
it('PUT /teams/:teamId/sandbox updates sandbox settings', async () => {
|
||||
mockUpdateSandboxSettings.mockResolvedValue({
|
||||
networkSandboxEnabled: true,
|
||||
allowedDomains: ['new.com'],
|
||||
pendingDomains: ['new-pending.com'],
|
||||
})
|
||||
@@ -321,11 +324,16 @@ describe('team api', () => {
|
||||
const res = await app.request('/teams/t1/sandbox', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ allowedDomains: ['new.com'], pendingDomains: ['new-pending.com'] }),
|
||||
body: JSON.stringify({
|
||||
networkSandboxEnabled: true,
|
||||
allowedDomains: ['new.com'],
|
||||
pendingDomains: ['new-pending.com'],
|
||||
}),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data.networkSandboxEnabled).toBe(true)
|
||||
expect(data.allowedDomains).toEqual(['new.com'])
|
||||
expect(data.pendingDomains).toEqual(['new-pending.com'])
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||
@@ -336,6 +344,7 @@ describe('team api', () => {
|
||||
}),
|
||||
)
|
||||
expect(mockUpdateSandboxSettings).toHaveBeenCalledWith('t1', {
|
||||
networkSandboxEnabled: true,
|
||||
allowedDomains: ['new.com'],
|
||||
pendingDomains: ['new-pending.com'],
|
||||
})
|
||||
|
||||
@@ -32,12 +32,14 @@ vi.mock('./middleware/auth', () => ({
|
||||
describe('versionStack api', () => {
|
||||
let mockCreateVersionStack: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockChangeStackFileVersion: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockRemoveVersionFromStack: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockGetAsset: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockGetStackVersions: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
|
||||
beforeEach(() => {
|
||||
mockCreateVersionStack = vi.spyOn(versionStackService, 'createVersionStack')
|
||||
mockChangeStackFileVersion = vi.spyOn(versionStackService, 'changeStackFileVersion')
|
||||
mockRemoveVersionFromStack = vi.spyOn(versionStackService, 'removeVersionFromStack')
|
||||
mockGetAsset = vi.spyOn(assetService, 'getAsset')
|
||||
mockGetStackVersions = vi.spyOn(assetService, 'getStackVersions')
|
||||
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
|
||||
@@ -146,4 +148,30 @@ describe('versionStack api', () => {
|
||||
expect(mockGetStackVersions).toHaveBeenCalledWith('stack1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('DELETE /version_stacks/:stackId/versions/:versionId', () => {
|
||||
it('should remove version from stack and return 200', async () => {
|
||||
mockRemoveVersionFromStack.mockResolvedValue()
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', versionStackRoute)
|
||||
|
||||
const res = await app.request('/version_stacks/stack1/versions/v1', {
|
||||
method: 'DELETE',
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: ResourceType.Asset,
|
||||
id: 'stack1',
|
||||
permission: Permission.Edit,
|
||||
}),
|
||||
)
|
||||
expect(mockRemoveVersionFromStack).toHaveBeenCalledWith({
|
||||
stackId: 'stack1',
|
||||
fileId: 'v1',
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -75,5 +75,24 @@ const route = app
|
||||
const versions = await assetService.getStackVersions(stackId)
|
||||
return c.json(versions)
|
||||
})
|
||||
.delete('/version_stacks/:stackId/versions/:versionId', async (c) => {
|
||||
const stackId = c.req.param('stackId')
|
||||
const versionId = c.req.param('versionId')
|
||||
const user = c.get('user')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Asset,
|
||||
id: stackId,
|
||||
})
|
||||
|
||||
await versionStackService.removeVersionFromStack({
|
||||
stackId,
|
||||
fileId: versionId,
|
||||
})
|
||||
|
||||
return new Response(null, { status: 200 })
|
||||
})
|
||||
|
||||
export default route
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
import { describe, expect, test, vi, beforeEach } from 'vitest'
|
||||
import { Hono } from 'hono'
|
||||
import watermarkTemplateRoute from './watermark-template'
|
||||
import { authMiddleware } from './middleware/auth'
|
||||
import { authzService } from '@shumai/core/src/authz/authz'
|
||||
import { watermarkService } from '@shumai/core/src/watermark/watermark'
|
||||
import { teamService } from '@shumai/core/src/team/team'
|
||||
|
||||
vi.mock('./middleware/auth', () => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
authMiddleware: async (c: any, next: any) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
c.set('user', { id: 'user1', name: 'Test User' } as any)
|
||||
await next()
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@shumai/core/src/authz/authz', () => ({
|
||||
authzService: {
|
||||
hasPermission: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
Permission: {
|
||||
Read: 'Read',
|
||||
Edit: 'Edit',
|
||||
Admin: 'Admin',
|
||||
},
|
||||
ResourceType: {
|
||||
Team: 'team',
|
||||
},
|
||||
}))
|
||||
|
||||
describe('Watermark Template API', () => {
|
||||
const app = new Hono().use('*', authMiddleware).route('/', watermarkTemplateRoute)
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
|
||||
})
|
||||
|
||||
test('GET /watermark-templates', async () => {
|
||||
vi.spyOn(watermarkService, 'listTemplates').mockResolvedValue([
|
||||
{
|
||||
id: 'tpl1',
|
||||
name: 'Preset 1',
|
||||
config: { blocks: [] },
|
||||
teamId: 't1',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
])
|
||||
|
||||
const res = await app.request('/watermark-templates?teamId=t1', {
|
||||
method: 'GET',
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.json()
|
||||
expect(body).toHaveLength(1)
|
||||
expect(body[0].name).toBe('Preset 1')
|
||||
})
|
||||
|
||||
test("GET /watermark-templates without teamId scopes to the caller's teams", async () => {
|
||||
vi.spyOn(teamService, 'getUserTeams').mockResolvedValue({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
data: [{ id: 't1', name: 'My Team' }] as any,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
pageInfo: {} as any,
|
||||
})
|
||||
vi.spyOn(watermarkService, 'listTemplates').mockResolvedValue([])
|
||||
|
||||
const res = await app.request('/watermark-templates', {
|
||||
method: 'GET',
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
expect(watermarkService.listTemplates).toHaveBeenCalledWith(['t1'])
|
||||
})
|
||||
|
||||
test('POST /watermark-templates', async () => {
|
||||
vi.spyOn(teamService, 'getUserTeams').mockResolvedValue({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
data: [{ id: 't1', name: 'My Team' }] as any,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
pageInfo: {} as any,
|
||||
})
|
||||
vi.spyOn(watermarkService, 'createTemplate').mockResolvedValue({
|
||||
id: 'tpl1',
|
||||
name: 'New Preset',
|
||||
config: { blocks: [] },
|
||||
teamId: 't1',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
})
|
||||
|
||||
const res = await app.request('/watermark-templates', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
name: 'New Preset',
|
||||
config: { blocks: [] },
|
||||
}),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(201)
|
||||
const body = await res.json()
|
||||
expect(body.name).toBe('New Preset')
|
||||
})
|
||||
|
||||
test('DELETE /watermark-templates/:templateId', async () => {
|
||||
vi.spyOn(watermarkService, 'getTemplate').mockResolvedValue({
|
||||
id: 'tpl1',
|
||||
name: 'New Preset',
|
||||
config: { blocks: [] },
|
||||
teamId: 't1',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
})
|
||||
vi.spyOn(watermarkService, 'deleteTemplate').mockResolvedValue(undefined)
|
||||
|
||||
const res = await app.request('/watermark-templates/tpl1', {
|
||||
method: 'DELETE',
|
||||
})
|
||||
|
||||
expect(res.status).toBe(204)
|
||||
expect(watermarkService.deleteTemplate).toHaveBeenCalledWith('tpl1')
|
||||
})
|
||||
|
||||
test('DELETE /watermark-templates/:templateId on a teamless template returns 403', async () => {
|
||||
vi.spyOn(watermarkService, 'getTemplate').mockResolvedValue({
|
||||
id: 'tpl-global',
|
||||
name: 'Platform Preset',
|
||||
config: { blocks: [] },
|
||||
teamId: null,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
})
|
||||
vi.spyOn(watermarkService, 'deleteTemplate').mockResolvedValue(undefined)
|
||||
|
||||
const res = await app.request('/watermark-templates/tpl-global', {
|
||||
method: 'DELETE',
|
||||
})
|
||||
|
||||
expect(res.status).toBe(403)
|
||||
expect(watermarkService.deleteTemplate).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,145 @@
|
||||
import { Hono } from 'hono'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { teamService } from '@shumai/core/src/team/team'
|
||||
import { watermarkService } from '@shumai/core/src/watermark/watermark'
|
||||
import {
|
||||
createWatermarkTemplateRequestSchema,
|
||||
updateWatermarkTemplateRequestSchema,
|
||||
} from '@shumai/dtos'
|
||||
import { z } from 'zod'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
|
||||
const listTemplatesQuerySchema = z.object({
|
||||
teamId: z.string().optional(),
|
||||
})
|
||||
|
||||
const route = new Hono<{ Variables: { user: User } }>()
|
||||
.get('/watermark-templates', zValidator('query', listTemplatesQuerySchema), async (c) => {
|
||||
const user = c.get('user')
|
||||
const { teamId } = c.req.valid('query')
|
||||
|
||||
let teamIds: string[]
|
||||
if (teamId) {
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Team,
|
||||
id: teamId,
|
||||
})
|
||||
teamIds = [teamId]
|
||||
} else {
|
||||
// No teamId: scope to the caller's own teams so templates are never
|
||||
// leaked across tenants.
|
||||
const userTeams = await teamService.getUserTeams({
|
||||
userId: user.id,
|
||||
pagination: { first: 20 },
|
||||
})
|
||||
teamIds = userTeams.data.map((t) => t.id)
|
||||
}
|
||||
|
||||
const templates = await watermarkService.listTemplates(teamIds)
|
||||
return c.json(templates)
|
||||
})
|
||||
.get('/watermark-templates/:templateId', async (c) => {
|
||||
const templateId = c.req.param('templateId')
|
||||
const template = await watermarkService.getTemplate(templateId)
|
||||
|
||||
if (template.teamId) {
|
||||
const user = c.get('user')
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Team,
|
||||
id: template.teamId,
|
||||
})
|
||||
}
|
||||
// Teamless templates are shared platform defaults: readable by any
|
||||
// authenticated user, but not writable (enforced on PUT/DELETE).
|
||||
|
||||
return c.json(template)
|
||||
})
|
||||
.post(
|
||||
'/watermark-templates',
|
||||
zValidator('json', createWatermarkTemplateRequestSchema),
|
||||
async (c) => {
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
let teamId: string | null
|
||||
if (req.teamId) {
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Team,
|
||||
id: req.teamId,
|
||||
})
|
||||
teamId = req.teamId
|
||||
} else {
|
||||
const userTeams = await teamService.getUserTeams({
|
||||
userId: user.id,
|
||||
pagination: { first: 20 },
|
||||
})
|
||||
teamId = userTeams.data[0]?.id || null
|
||||
if (!teamId) {
|
||||
return c.json({ error: 'A team is required to create a watermark template' }, 400)
|
||||
}
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Team,
|
||||
id: teamId,
|
||||
})
|
||||
}
|
||||
|
||||
const template = await watermarkService.createTemplate(teamId, req.name, req.config)
|
||||
return c.json(template, 201)
|
||||
},
|
||||
)
|
||||
.put(
|
||||
'/watermark-templates/:templateId',
|
||||
zValidator('json', updateWatermarkTemplateRequestSchema),
|
||||
async (c) => {
|
||||
const templateId = c.req.param('templateId')
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
const existing = await watermarkService.getTemplate(templateId)
|
||||
if (existing.teamId) {
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Team,
|
||||
id: existing.teamId,
|
||||
})
|
||||
} else {
|
||||
return c.json({ error: 'Platform templates are read-only' }, 403)
|
||||
}
|
||||
|
||||
const updated = await watermarkService.updateTemplate(templateId, req.name, req.config)
|
||||
return c.json(updated)
|
||||
},
|
||||
)
|
||||
.delete('/watermark-templates/:templateId', async (c) => {
|
||||
const templateId = c.req.param('templateId')
|
||||
const user = c.get('user')
|
||||
|
||||
const existing = await watermarkService.getTemplate(templateId)
|
||||
if (existing.teamId) {
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Team,
|
||||
id: existing.teamId,
|
||||
})
|
||||
} else {
|
||||
return c.json({ error: 'Platform templates are read-only' }, 403)
|
||||
}
|
||||
|
||||
await watermarkService.deleteTemplate(templateId)
|
||||
return c.body(null, 204)
|
||||
})
|
||||
|
||||
export default route
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/core",
|
||||
"version": "0.3.3",
|
||||
"version": "0.3.8",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
@@ -8,6 +8,7 @@
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "0.82.1",
|
||||
"@earendil-works/pi-ai": "0.82.1",
|
||||
"@modelcontextprotocol/client": "2.0.0",
|
||||
"@shumai/db": "workspace:*",
|
||||
"@shumai/dtos": "workspace:*",
|
||||
"@sinclair/typebox": "0.34.49",
|
||||
@@ -24,11 +25,13 @@
|
||||
"pino": "10.3.1",
|
||||
"sharp": "0.34.5",
|
||||
"tweetnacl": "1.0.3",
|
||||
"typebox": "1.1.38",
|
||||
"ulid": "3.0.2",
|
||||
"yaml": "2.9.0",
|
||||
"zod": "4.4.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@modelcontextprotocol/server": "2.0.0",
|
||||
"@types/adm-zip": "0.5.8",
|
||||
"@types/extract-zip": "2.0.3",
|
||||
"@types/pdfkit": "0.17.6"
|
||||
|
||||
@@ -121,6 +121,7 @@ export class AgentService {
|
||||
systemPrompt,
|
||||
soul,
|
||||
skills,
|
||||
mcpServerIds,
|
||||
deniedTools,
|
||||
} = params
|
||||
|
||||
@@ -189,6 +190,12 @@ export class AgentService {
|
||||
skillId,
|
||||
})) || [],
|
||||
},
|
||||
mcpServers: {
|
||||
create:
|
||||
mcpServerIds?.map((mcpServerId) => ({
|
||||
mcpServerId,
|
||||
})) || [],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -202,7 +209,11 @@ export class AgentService {
|
||||
|
||||
return tx.agent.findUnique({
|
||||
where: { id: agent.id },
|
||||
include: { user: true, skills: { include: { skill: true } } },
|
||||
include: {
|
||||
user: true,
|
||||
skills: { include: { skill: true } },
|
||||
mcpServers: { include: { mcpServer: true } },
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -220,6 +231,7 @@ export class AgentService {
|
||||
systemPrompt,
|
||||
soul,
|
||||
skills,
|
||||
mcpServerIds,
|
||||
deniedTools,
|
||||
} = params
|
||||
|
||||
@@ -278,6 +290,11 @@ export class AgentService {
|
||||
where: { agentId },
|
||||
})
|
||||
|
||||
// Update MCP server assignments: easier to delete all and recreate
|
||||
await tx.agentMcpServer.deleteMany({
|
||||
where: { agentId },
|
||||
})
|
||||
|
||||
return tx.agent.update({
|
||||
where: { id: agentId },
|
||||
data: {
|
||||
@@ -294,8 +311,18 @@ export class AgentService {
|
||||
skillId,
|
||||
})) || [],
|
||||
},
|
||||
mcpServers: {
|
||||
create:
|
||||
mcpServerIds?.map((mcpServerId) => ({
|
||||
mcpServerId,
|
||||
})) || [],
|
||||
},
|
||||
},
|
||||
include: {
|
||||
user: true,
|
||||
skills: { include: { skill: true } },
|
||||
mcpServers: { include: { mcpServer: true } },
|
||||
},
|
||||
include: { user: true, skills: { include: { skill: true } } },
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -338,6 +365,7 @@ export class AgentService {
|
||||
skill: true,
|
||||
},
|
||||
},
|
||||
mcpServers: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
|
||||
import { AssetType, AssetStatus, Prisma } from '@shumai/db'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { AssetService } from './asset'
|
||||
|
||||
vi.mock('@shumai/core/src/s3/s3', () => ({
|
||||
@@ -2229,6 +2230,18 @@ describe('AssetService — natural sort by name', () => {
|
||||
for (const item of result) {
|
||||
expect(item.url).toBe('http://mock-s3-url')
|
||||
}
|
||||
|
||||
// Renamed file names must be passed to presign so the download
|
||||
// Content-Disposition header uses the asset name, not the storage key
|
||||
for (const name of ['file1.txt', 'file2.txt', 'version2.txt']) {
|
||||
expect(s3Service.presign).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
expect.any(String),
|
||||
'GET',
|
||||
true,
|
||||
name,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it('resolves top-level symlinks to their targets when passed as direct starting IDs', async () => {
|
||||
@@ -2539,6 +2552,40 @@ describe('AssetService — natural sort by name', () => {
|
||||
)
|
||||
|
||||
expect(url).toBe('http://mock-s3-url')
|
||||
expect(s3Service.presign).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
'files/01TESTULID000000000000000/test-720p.mp4',
|
||||
'GET',
|
||||
true,
|
||||
'test.mp4',
|
||||
)
|
||||
})
|
||||
|
||||
it('should use the renamed asset name as the download filename', async () => {
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'foo.png',
|
||||
type: AssetType.file,
|
||||
status: AssetStatus.uploaded,
|
||||
storageKey: {
|
||||
create: { key: 'files/01RENAMEDULID0000000000000/test.png' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const url = await assetService.getDownloadUrl(
|
||||
asset.id,
|
||||
'files/01RENAMEDULID0000000000000/test.png',
|
||||
)
|
||||
|
||||
expect(url).toBe('http://mock-s3-url')
|
||||
expect(s3Service.presign).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
'files/01RENAMEDULID0000000000000/test.png',
|
||||
'GET',
|
||||
true,
|
||||
'foo.png',
|
||||
)
|
||||
})
|
||||
|
||||
it('should return a presigned download URL for the original key', async () => {
|
||||
@@ -2617,6 +2664,35 @@ describe('AssetService — natural sort by name', () => {
|
||||
assetService.getDownloadUrl(asset.id, 'files/01TESTULID000000000000000/test.mp4'),
|
||||
).rejects.toThrow('Asset not found or has no storage key')
|
||||
})
|
||||
|
||||
it('should return a presigned download URL when given a symlink asset ID', async () => {
|
||||
const realAsset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fly.png',
|
||||
type: AssetType.file,
|
||||
status: AssetStatus.uploaded,
|
||||
storageKey: {
|
||||
create: { key: 'files/01KZ8VH3QHKQCSQDC5JPXCYT0R/fly.png' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const symlinkAsset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fly.png',
|
||||
type: AssetType.symlink,
|
||||
status: AssetStatus.uploaded,
|
||||
targetId: realAsset.id,
|
||||
},
|
||||
})
|
||||
|
||||
const url = await assetService.getDownloadUrl(
|
||||
symlinkAsset.id,
|
||||
'files/01KZ8VH3QHKQCSQDC5JPXCYT0R/fly.png',
|
||||
)
|
||||
|
||||
expect(url).toBe('http://mock-s3-url')
|
||||
})
|
||||
})
|
||||
|
||||
describe('resolveLatestVersionId', () => {
|
||||
@@ -2754,4 +2830,615 @@ describe('AssetService — natural sort by name', () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('AGENTS.md methods', () => {
|
||||
it('getAgentsMd returns null when no agentmd is set', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Team_AgentsMd_' + Date.now() },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Project_AgentsMd', teamId: team.id },
|
||||
})
|
||||
const folder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Folder_No_Md',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
const content = await assetService.getAgentsMd(folder.id)
|
||||
expect(content).toBeNull()
|
||||
})
|
||||
|
||||
it('updateAgentsMd updates and getAgentsMd retrieves agentmd, and empty deletes', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Team_AgentsMd2_' + Date.now() },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Project_AgentsMd2', teamId: team.id },
|
||||
})
|
||||
const folder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Folder_With_Md',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
const updateResult = await assetService.updateAgentsMd(folder.id, '# Custom Guidelines')
|
||||
expect(updateResult).toEqual({ content: '# Custom Guidelines' })
|
||||
|
||||
const content = await assetService.getAgentsMd(folder.id)
|
||||
expect(content).toBe('# Custom Guidelines')
|
||||
|
||||
let folderInfo = await assetService.getAsset({ assetId: folder.id })
|
||||
expect(folderInfo?.hasAgentsMd).toBe(true)
|
||||
|
||||
// Empty string deletes the record
|
||||
const clearResult = await assetService.updateAgentsMd(folder.id, ' ')
|
||||
expect(clearResult).toEqual({ content: '' })
|
||||
|
||||
const emptyContent = await assetService.getAgentsMd(folder.id)
|
||||
expect(emptyContent).toBeNull()
|
||||
|
||||
folderInfo = await assetService.getAsset({ assetId: folder.id })
|
||||
expect(folderInfo?.hasAgentsMd).toBe(false)
|
||||
})
|
||||
|
||||
it('getNestedAgentsMd traverses hierarchy from root to leaf and formats virtual paths', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Team_NestedMd_' + Date.now() },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Project_NestedMd', teamId: team.id },
|
||||
})
|
||||
|
||||
// 1. Root folder
|
||||
const rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Root Folder',
|
||||
type: AssetType.root,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
await prisma.assetAgentMd.create({
|
||||
data: {
|
||||
assetId: rootFolder.id,
|
||||
content: '# Root Instructions\nFollow global rules.',
|
||||
},
|
||||
})
|
||||
|
||||
// 2. Subfolder 1: marketing (has agentmd)
|
||||
const marketingFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'marketing',
|
||||
type: AssetType.folder,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
await prisma.assetAgentMd.create({
|
||||
data: {
|
||||
assetId: marketingFolder.id,
|
||||
content: '# Marketing Instructions\nTone must be upbeat.',
|
||||
},
|
||||
})
|
||||
|
||||
// 3. Subfolder 2: campaigns (no agentmd)
|
||||
const campaignsFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'campaigns',
|
||||
type: AssetType.folder,
|
||||
parentId: marketingFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
// 4. Subfolder 3: video-ads (has agentmd)
|
||||
const videoAdsFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video-ads',
|
||||
type: AssetType.folder,
|
||||
parentId: campaignsFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
await prisma.assetAgentMd.create({
|
||||
data: {
|
||||
assetId: videoAdsFolder.id,
|
||||
content: '# Video Ads Instructions\nInclude resolution tags.',
|
||||
},
|
||||
})
|
||||
|
||||
// 5. File inside video-ads
|
||||
const fileAsset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'ad_clip.mp4',
|
||||
type: AssetType.file,
|
||||
parentId: videoAdsFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
const nestedFromLeaf = await assetService.getNestedAgentsMd(fileAsset.id)
|
||||
expect(nestedFromLeaf).toEqual([
|
||||
{
|
||||
path: '/AGENTS.md',
|
||||
content: '# Root Instructions\nFollow global rules.',
|
||||
},
|
||||
{
|
||||
path: '/marketing/AGENTS.md',
|
||||
content: '# Marketing Instructions\nTone must be upbeat.',
|
||||
},
|
||||
{
|
||||
path: '/marketing/campaigns/video-ads/AGENTS.md',
|
||||
content: '# Video Ads Instructions\nInclude resolution tags.',
|
||||
},
|
||||
])
|
||||
|
||||
const nestedFromMarketing = await assetService.getNestedAgentsMd(marketingFolder.id)
|
||||
expect(nestedFromMarketing).toEqual([
|
||||
{
|
||||
path: '/AGENTS.md',
|
||||
content: '# Root Instructions\nFollow global rules.',
|
||||
},
|
||||
{
|
||||
path: '/marketing/AGENTS.md',
|
||||
content: '# Marketing Instructions\nTone must be upbeat.',
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it('rejects get/update agentsmd on non-folder assets', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Team_AgentsMd_Type_' + Date.now() },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Project_AgentsMd_Type', teamId: team.id },
|
||||
})
|
||||
const rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Root',
|
||||
type: AssetType.root,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
const fileAsset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'doc.pdf',
|
||||
type: AssetType.file,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
await expect(assetService.getAgentsMd(fileAsset.id)).rejects.toThrow(
|
||||
'AGENTS.md can only be stored on folders',
|
||||
)
|
||||
await expect(assetService.updateAgentsMd(fileAsset.id, '# Nope')).rejects.toThrow(
|
||||
'AGENTS.md can only be stored on folders',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('reparentAssets into version_stack with sharelink symlinks', () => {
|
||||
it('deduplicates symlinks when both Stack A and File B are in the sharelink and File B is moved into Stack A', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
||||
})
|
||||
const rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Root',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
const shareRoot = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'ShareRoot',
|
||||
type: AssetType.share_root,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
fileCount: 2,
|
||||
},
|
||||
})
|
||||
|
||||
const fileA1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fileA1.txt',
|
||||
type: AssetType.file,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 100,
|
||||
sortIndex: 'a0',
|
||||
},
|
||||
})
|
||||
const fileA2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fileA2.txt',
|
||||
type: AssetType.file,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 100,
|
||||
sortIndex: 'a1',
|
||||
},
|
||||
})
|
||||
const fileB = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fileB.txt',
|
||||
type: AssetType.file,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 200,
|
||||
sortIndex: 'a2',
|
||||
},
|
||||
})
|
||||
|
||||
// Create Stack A from fileA1 and fileA2
|
||||
const stackA = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
fileCount: 2,
|
||||
sizeByte: 200,
|
||||
},
|
||||
})
|
||||
await prisma.asset.update({ where: { id: fileA1.id }, data: { parentId: stackA.id } })
|
||||
await prisma.asset.update({ where: { id: fileA2.id }, data: { parentId: stackA.id } })
|
||||
|
||||
// Create symlinks in shareRoot for both Stack A and File B
|
||||
const symlinkStackA = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: stackA.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
const symlinkFileB = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileB.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: fileB.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
// Move File B into Stack A
|
||||
await assetService.reparentAssets({
|
||||
newParentId: stackA.id,
|
||||
assetIds: [fileB.id],
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
// Verify: File B's symlink was deleted, only Stack A's symlink remains
|
||||
expect(await prisma.asset.findUnique({ where: { id: symlinkFileB.id } })).toBeNull()
|
||||
const remainingSymlinks = await prisma.asset.findMany({
|
||||
where: { parentId: shareRoot.id, type: AssetType.symlink },
|
||||
})
|
||||
expect(remainingSymlinks).toHaveLength(1)
|
||||
expect(remainingSymlinks[0].id).toBe(symlinkStackA.id)
|
||||
expect(remainingSymlinks[0].targetId).toBe(stackA.id)
|
||||
|
||||
// ShareRoot fileCount was decremented from 2 to 1
|
||||
const updatedShareRoot = await prisma.asset.findUnique({ where: { id: shareRoot.id } })
|
||||
expect(updatedShareRoot?.fileCount).toBe(1)
|
||||
})
|
||||
|
||||
it('repoints symlink to Stack A when only File B is in sharelink and File B is moved into Stack A', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
||||
})
|
||||
const rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Root',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
const shareRoot = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'ShareRoot',
|
||||
type: AssetType.share_root,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
fileCount: 1,
|
||||
},
|
||||
})
|
||||
|
||||
const fileA1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fileA1.txt',
|
||||
type: AssetType.file,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 100,
|
||||
sortIndex: 'a0',
|
||||
},
|
||||
})
|
||||
const stackA = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
fileCount: 1,
|
||||
sizeByte: 100,
|
||||
},
|
||||
})
|
||||
await prisma.asset.update({ where: { id: fileA1.id }, data: { parentId: stackA.id } })
|
||||
|
||||
const fileB = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fileB.txt',
|
||||
type: AssetType.file,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 200,
|
||||
sortIndex: 'a1',
|
||||
},
|
||||
})
|
||||
|
||||
// Create symlink for File B only
|
||||
const symlinkFileB = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileB.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: fileB.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
// Move File B into Stack A
|
||||
await assetService.reparentAssets({
|
||||
newParentId: stackA.id,
|
||||
assetIds: [fileB.id],
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
// Verify: symlinkFileB now points to stackA.id
|
||||
const updatedSymlink = await prisma.asset.findUnique({ where: { id: symlinkFileB.id } })
|
||||
expect(updatedSymlink?.targetId).toBe(stackA.id)
|
||||
|
||||
// Listing assets for shareRoot returns Stack A with latest version name
|
||||
const result = await assetService.listChildren({
|
||||
assetId: shareRoot.id,
|
||||
assetType: AssetType.file,
|
||||
})
|
||||
expect(result.data).toHaveLength(1)
|
||||
expect(result.data[0].name).toBe(fileB.name) // fileB is the latest version
|
||||
})
|
||||
})
|
||||
|
||||
describe('updateAssetName', () => {
|
||||
it('renames the latest version child file and returns updated AssetInfo when given a version stack ID', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Root',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
fileCount: 2,
|
||||
sizeByte: 300,
|
||||
},
|
||||
})
|
||||
|
||||
const fileV1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'v1.mp4',
|
||||
type: AssetType.file,
|
||||
parentId: stack.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 100,
|
||||
sortIndex: 'b0',
|
||||
},
|
||||
})
|
||||
|
||||
const fileV2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'v2.mp4',
|
||||
type: AssetType.file,
|
||||
parentId: stack.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 200,
|
||||
sortIndex: 'a0', // latest version
|
||||
},
|
||||
})
|
||||
|
||||
const updated = await assetService.updateAssetName({
|
||||
id: stack.id,
|
||||
name: 'v2-renamed.mp4',
|
||||
})
|
||||
|
||||
expect(updated.id).toBe(stack.id)
|
||||
expect(updated.name).toBe('v2-renamed.mp4')
|
||||
expect(updated.versionStack?.versions[0]?.name).toBe('v2-renamed.mp4')
|
||||
|
||||
const dbFileV2 = await prisma.asset.findUnique({ where: { id: fileV2.id } })
|
||||
expect(dbFileV2?.name).toBe('v2-renamed.mp4')
|
||||
|
||||
const dbFileV1 = await prisma.asset.findUnique({ where: { id: fileV1.id } })
|
||||
expect(dbFileV1?.name).toBe('v1.mp4')
|
||||
|
||||
const dbStack = await prisma.asset.findUnique({ where: { id: stack.id } })
|
||||
expect(dbStack?.name).toBe('')
|
||||
})
|
||||
|
||||
it('preserves the renamed filename when a version stack dissolves', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Root',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
fileCount: 2,
|
||||
sizeByte: 300,
|
||||
sortIndex: 's0',
|
||||
},
|
||||
})
|
||||
|
||||
const fileV1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'v1.mp4',
|
||||
type: AssetType.file,
|
||||
parentId: stack.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 100,
|
||||
sortIndex: 'b0',
|
||||
},
|
||||
})
|
||||
|
||||
const fileV2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'v2.mp4',
|
||||
type: AssetType.file,
|
||||
parentId: stack.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 200,
|
||||
sortIndex: 'a0',
|
||||
},
|
||||
})
|
||||
|
||||
await assetService.updateAssetName({
|
||||
id: stack.id,
|
||||
name: 'v2-renamed.mp4',
|
||||
})
|
||||
|
||||
// Remove v1 from stack -> stack dissolves and v2 becomes standalone file
|
||||
const { versionStackService } = await import('../versionStack/versionStack')
|
||||
await versionStackService.removeVersionFromStack({
|
||||
stackId: stack.id,
|
||||
fileId: fileV1.id,
|
||||
})
|
||||
|
||||
const remainingFile = await prisma.asset.findUnique({ where: { id: fileV2.id } })
|
||||
expect(remainingFile?.parentId).toBe(rootFolder.id)
|
||||
expect(remainingFile?.name).toBe('v2-renamed.mp4')
|
||||
})
|
||||
|
||||
it('throws an error if version stack has no active versions to rename', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const emptyStack = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
await expect(
|
||||
assetService.updateAssetName({
|
||||
id: emptyStack.id,
|
||||
name: 'new-name.mp4',
|
||||
}),
|
||||
).rejects.toThrow('No active version found in stack to rename')
|
||||
})
|
||||
|
||||
it('renames a regular file and folder correctly', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const folder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Original Folder',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
const file = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'original.png',
|
||||
type: AssetType.file,
|
||||
parentId: folder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
const updatedFolder = await assetService.updateAssetName({
|
||||
id: folder.id,
|
||||
name: 'Renamed Folder',
|
||||
})
|
||||
expect(updatedFolder.name).toBe('Renamed Folder')
|
||||
|
||||
const updatedFile = await assetService.updateAssetName({
|
||||
id: file.id,
|
||||
name: 'renamed.png',
|
||||
})
|
||||
expect(updatedFile.name).toBe('renamed.png')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -22,6 +22,8 @@ import { HTTPException } from 'hono/http-exception'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { PaginatedData, paginateQuery, PaginationParams } from '@shumai/core/src/pagination'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { dedupeSymlinksToTarget } from './symlink'
|
||||
import { watermarkService } from '@shumai/core/src/watermark/watermark'
|
||||
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
||||
import { getAvatarUrl } from '@shumai/core/src/user/avatar'
|
||||
|
||||
@@ -159,6 +161,9 @@ export class AssetService {
|
||||
const count = await tx.asset.count({ where: { parentId: stack.id } })
|
||||
|
||||
if (count === 0) {
|
||||
await tx.asset.deleteMany({
|
||||
where: { targetId: stack.id, type: AssetType.symlink },
|
||||
})
|
||||
if (stack.parentId) {
|
||||
await tx.asset.update({
|
||||
where: { id: stack.parentId },
|
||||
@@ -173,9 +178,14 @@ export class AssetService {
|
||||
if (!lastChild) return
|
||||
if (!stack.parentId) throw new Error('Stack has no parent')
|
||||
|
||||
await tx.asset.updateMany({
|
||||
where: { targetId: stack.id, type: AssetType.symlink },
|
||||
data: { targetId: lastChild.id, name: lastChild.name },
|
||||
})
|
||||
|
||||
await tx.asset.update({
|
||||
where: { id: lastChild.id },
|
||||
data: { parentId: stack.parentId, sortIndex: null },
|
||||
data: { parentId: stack.parentId, sortIndex: stack.sortIndex },
|
||||
})
|
||||
await tx.asset.delete({ where: { id: stack.id } })
|
||||
}
|
||||
@@ -272,6 +282,14 @@ export class AssetService {
|
||||
firstSortIndex = newSortIndex
|
||||
}
|
||||
|
||||
if (newParent.type === AssetType.version_stack) {
|
||||
await dedupeSymlinksToTarget(tx, {
|
||||
targetIds: req.assetIds,
|
||||
newTargetId: newParent.id,
|
||||
name: '',
|
||||
})
|
||||
}
|
||||
|
||||
const oldParent = await tx.asset.findUnique({
|
||||
where: { id: oldParentId },
|
||||
})
|
||||
@@ -578,6 +596,12 @@ export class AssetService {
|
||||
},
|
||||
})
|
||||
|
||||
// Update existing symlinks pointing to destFile or sourceAsset
|
||||
await dedupeSymlinksToTarget(tx, {
|
||||
targetIds: [destFile.id, sourceAsset.id],
|
||||
newTargetId: stack.id,
|
||||
})
|
||||
|
||||
const oldParent = await tx.asset.findUnique({ where: { id: oldParentId } })
|
||||
if (oldParent) {
|
||||
await this.dissolveStackIfEmpty(tx, oldParent)
|
||||
@@ -852,13 +876,6 @@ export class AssetService {
|
||||
t.url = await s3Service.presign(process.env.S3_BUCKET || 'shumai', t.key, 'GET')
|
||||
}
|
||||
}
|
||||
if (info.media.original?.key) {
|
||||
info.media.original.downloadUrl = await s3Service.presign(
|
||||
process.env.S3_BUCKET || 'shumai',
|
||||
info.media.original.key,
|
||||
'GET',
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return info
|
||||
@@ -938,9 +955,42 @@ export class AssetService {
|
||||
}
|
||||
|
||||
async updateAssetName(req: UpdateAssetNameRequest): Promise<AssetInfo> {
|
||||
const asset = await this.prismaClient.asset.update({
|
||||
const existing = await this.prismaClient.asset.findUnique({
|
||||
where: { id: req.id },
|
||||
include: { target: true },
|
||||
})
|
||||
if (!existing) throw new Error('Asset not found')
|
||||
|
||||
let targetType = existing.type
|
||||
let targetId = existing.id
|
||||
|
||||
if (existing.type === AssetType.symlink && existing.target) {
|
||||
targetType = existing.target.type
|
||||
targetId = existing.target.id
|
||||
}
|
||||
|
||||
if (targetType === AssetType.version_stack) {
|
||||
const latestChild = await this.prismaClient.asset.findFirst({
|
||||
where: { parentId: targetId, isDeleted: false },
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
select: { id: true },
|
||||
})
|
||||
if (!latestChild) {
|
||||
throw new Error('No active version found in stack to rename')
|
||||
}
|
||||
await this.prismaClient.asset.update({
|
||||
where: { id: latestChild.id },
|
||||
data: { name: req.name },
|
||||
})
|
||||
} else {
|
||||
await this.prismaClient.asset.update({
|
||||
where: { id: req.id },
|
||||
data: { name: req.name },
|
||||
})
|
||||
}
|
||||
|
||||
const asset = await this.prismaClient.asset.findUnique({
|
||||
where: { id: req.id },
|
||||
data: { name: req.name },
|
||||
include: {
|
||||
creator: true,
|
||||
metadataValues: true,
|
||||
@@ -966,6 +1016,8 @@ export class AssetService {
|
||||
storageKey: true,
|
||||
},
|
||||
})
|
||||
if (!asset) throw new Error('Asset not found')
|
||||
|
||||
const infos = await this.toAssetInfos([asset as unknown as AssetWithIncludes])
|
||||
return infos[0]
|
||||
}
|
||||
@@ -1104,6 +1156,7 @@ export class AssetService {
|
||||
await this.expireTrashedAssets()
|
||||
await this.purgePendingAssets()
|
||||
await this.purgeUnreferencedStorageKeys()
|
||||
await watermarkService.purgeOrphanWatermarkConfigs()
|
||||
} catch (e: unknown) {
|
||||
console.error('Error in asset cleanup job:', e)
|
||||
}
|
||||
@@ -1597,6 +1650,15 @@ export class AssetService {
|
||||
}
|
||||
}
|
||||
|
||||
const agentMdRecords =
|
||||
assets.length > 0
|
||||
? await this.prismaClient.assetAgentMd.findMany({
|
||||
where: { assetId: { in: assets.map((a) => a.id) } },
|
||||
select: { assetId: true },
|
||||
})
|
||||
: []
|
||||
const hasAgentMdSet = new Set(agentMdRecords.map((r) => r.assetId))
|
||||
|
||||
const result: AssetInfo[] = []
|
||||
for (const a of assets) {
|
||||
let latestVersion:
|
||||
@@ -1625,6 +1687,7 @@ export class AssetService {
|
||||
id: v.id,
|
||||
name: v.name,
|
||||
previewUrl: preview?.thumbnailUrl || null,
|
||||
createdAt: v.createdAt.toISOString(),
|
||||
creator: v.creator
|
||||
? {
|
||||
id: v.creator.id,
|
||||
@@ -1730,7 +1793,6 @@ export class AssetService {
|
||||
}
|
||||
media.original = {
|
||||
key,
|
||||
downloadUrl: await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET'),
|
||||
filesizeInBytes: Number(latestVersion.sizeByte),
|
||||
codec: '',
|
||||
}
|
||||
@@ -1741,9 +1803,12 @@ export class AssetService {
|
||||
result.push({
|
||||
id: a.id,
|
||||
name:
|
||||
a.type === AssetType.version_stack && (a.name === '' || !a.name)
|
||||
a.type === AssetType.version_stack ||
|
||||
(a.type === AssetType.symlink && a.target?.type === AssetType.version_stack)
|
||||
? latestVersion.name
|
||||
: a.name,
|
||||
: a.type === AssetType.symlink
|
||||
? a.name || latestVersion.name || a.target?.name || ''
|
||||
: a.name,
|
||||
sizeByte: Number(latestVersion.sizeByte),
|
||||
fileCount: latestVersion.fileCount,
|
||||
type: a.type,
|
||||
@@ -1759,6 +1824,7 @@ export class AssetService {
|
||||
creator,
|
||||
fieldValues,
|
||||
sortIndex: a.sortIndex,
|
||||
hasAgentsMd: hasAgentMdSet.has(a.id),
|
||||
media: media as unknown as AssetInfo['media'],
|
||||
versionStack,
|
||||
})
|
||||
@@ -1772,7 +1838,8 @@ export class AssetService {
|
||||
version: number
|
||||
name: string
|
||||
previewUrl: string | null
|
||||
creator: { id: string; name: string | null } | null
|
||||
createdAt: string
|
||||
creator: { id: string; name: string | null; image?: string | null } | null
|
||||
}>
|
||||
> {
|
||||
const versions = await this.prismaClient.asset.findMany({
|
||||
@@ -1793,6 +1860,7 @@ export class AssetService {
|
||||
version: versions.length - i,
|
||||
name: v.name,
|
||||
previewUrl: preview?.thumbnailUrl || null,
|
||||
createdAt: v.createdAt.toISOString(),
|
||||
creator: v.creator
|
||||
? {
|
||||
id: v.creator.id,
|
||||
@@ -1972,7 +2040,13 @@ export class AssetService {
|
||||
const key = file.storageKeyId ? storageKeyMap.get(file.storageKeyId) : null
|
||||
if (!key) return null
|
||||
|
||||
const url = await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET', true)
|
||||
const url = await s3Service.presign(
|
||||
process.env.S3_BUCKET || 'shumai',
|
||||
key,
|
||||
'GET',
|
||||
true,
|
||||
file.name,
|
||||
)
|
||||
|
||||
return {
|
||||
id: file.id,
|
||||
@@ -1991,23 +2065,50 @@ export class AssetService {
|
||||
* that belong to an asset they have access to.
|
||||
*/
|
||||
async getDownloadUrl(assetId: string, key: string): Promise<string> {
|
||||
const targetAssetId = await this.resolveLatestVersionId(assetId)
|
||||
const asset = await this.prismaClient.asset.findUnique({
|
||||
where: { id: assetId },
|
||||
select: { storageKey: { select: { key: true } } },
|
||||
where: { id: targetAssetId },
|
||||
select: { id: true, name: true, parentId: true, storageKey: { select: { key: true } } },
|
||||
})
|
||||
|
||||
if (!asset?.storageKey?.key) {
|
||||
throw new Error('Asset not found or has no storage key')
|
||||
}
|
||||
|
||||
// Verify the requested key shares the same parent directory as the asset's storage key
|
||||
const storageKey = asset.storageKey.key
|
||||
const assetDir = storageKey.substring(0, storageKey.lastIndexOf('/') + 1)
|
||||
if (key.includes('..') || !key.startsWith(assetDir)) {
|
||||
if (key.includes('..')) {
|
||||
throw new Error('Key does not belong to this asset')
|
||||
}
|
||||
|
||||
return s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET', true)
|
||||
let storageKey = asset.storageKey.key
|
||||
let assetDir = storageKey.substring(0, storageKey.lastIndexOf('/') + 1)
|
||||
|
||||
if (!key.startsWith(assetDir) && asset.parentId) {
|
||||
// Check if requested key belongs to another version in the same version stack
|
||||
const sibling = await this.prismaClient.asset.findFirst({
|
||||
where: {
|
||||
parentId: asset.parentId,
|
||||
isDeleted: false,
|
||||
storageKey: { key: { startsWith: key.substring(0, key.lastIndexOf('/') + 1) } },
|
||||
},
|
||||
select: { storageKey: { select: { key: true } } },
|
||||
})
|
||||
if (sibling?.storageKey?.key) {
|
||||
storageKey = sibling.storageKey.key
|
||||
assetDir = storageKey.substring(0, storageKey.lastIndexOf('/') + 1)
|
||||
}
|
||||
}
|
||||
|
||||
if (!key.startsWith(assetDir)) {
|
||||
throw new Error('Key does not belong to this asset')
|
||||
}
|
||||
|
||||
return s3Service.presign(
|
||||
process.env.S3_BUCKET || 'shumai',
|
||||
key,
|
||||
'GET',
|
||||
true,
|
||||
asset.name ?? undefined,
|
||||
)
|
||||
}
|
||||
|
||||
private async toPreviewInfo(asset: Asset | AssetWithIncludes): Promise<PreviewInfo | null> {
|
||||
@@ -2137,6 +2238,108 @@ export class AssetService {
|
||||
completionLastChangedBy,
|
||||
}
|
||||
}
|
||||
|
||||
async getAgentsMd(assetId: string): Promise<string | null> {
|
||||
const asset = await this.prismaClient.asset.findUnique({
|
||||
where: { id: assetId },
|
||||
select: { type: true },
|
||||
})
|
||||
if (!asset) throw new Error('Asset not found')
|
||||
if (asset.type !== AssetType.folder && asset.type !== AssetType.root) {
|
||||
throw new Error('AGENTS.md can only be stored on folders')
|
||||
}
|
||||
const record = await this.prismaClient.assetAgentMd.findUnique({
|
||||
where: { assetId },
|
||||
select: { content: true },
|
||||
})
|
||||
return record?.content ?? null
|
||||
}
|
||||
|
||||
async updateAgentsMd(assetId: string, content: string): Promise<{ content: string }> {
|
||||
const asset = await this.prismaClient.asset.findUnique({
|
||||
where: { id: assetId },
|
||||
select: { id: true, type: true },
|
||||
})
|
||||
if (!asset) throw new Error('Asset not found')
|
||||
if (asset.type !== AssetType.folder && asset.type !== AssetType.root) {
|
||||
throw new Error('AGENTS.md can only be stored on folders')
|
||||
}
|
||||
const trimmed = content.trim()
|
||||
if (trimmed.length === 0) {
|
||||
await this.prismaClient.assetAgentMd.deleteMany({
|
||||
where: { assetId },
|
||||
})
|
||||
return { content: '' }
|
||||
}
|
||||
const record = await this.prismaClient.assetAgentMd.upsert({
|
||||
where: { assetId },
|
||||
create: { assetId, content },
|
||||
update: { content },
|
||||
select: { content: true },
|
||||
})
|
||||
return { content: record.content }
|
||||
}
|
||||
|
||||
async getNestedAgentsMd(assetId: string): Promise<Array<{ path: string; content: string }>> {
|
||||
let currentId: string | null = assetId
|
||||
const nodes: Array<{
|
||||
id: string
|
||||
name: string
|
||||
type: AssetType
|
||||
parentId: string | null
|
||||
}> = []
|
||||
|
||||
while (currentId) {
|
||||
const node: {
|
||||
id: string
|
||||
name: string
|
||||
type: AssetType
|
||||
parentId: string | null
|
||||
} | null = await this.prismaClient.asset.findUnique({
|
||||
where: { id: currentId },
|
||||
select: { id: true, name: true, type: true, parentId: true },
|
||||
})
|
||||
if (!node) break
|
||||
nodes.unshift(node)
|
||||
currentId = node.parentId
|
||||
}
|
||||
|
||||
if (nodes.length === 0) return []
|
||||
|
||||
const agentMds = await this.prismaClient.assetAgentMd.findMany({
|
||||
where: { assetId: { in: nodes.map((n) => n.id) } },
|
||||
select: { assetId: true, content: true },
|
||||
})
|
||||
const agentMdMap = new Map(agentMds.map((m) => [m.assetId, m.content]))
|
||||
|
||||
const results: Array<{ path: string; content: string }> = []
|
||||
let currentFolderPath = ''
|
||||
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
const node = nodes[i]
|
||||
let virtualPath: string
|
||||
|
||||
if (node.type === AssetType.root || i === 0) {
|
||||
currentFolderPath = ''
|
||||
virtualPath = '/AGENTS.md'
|
||||
} else {
|
||||
currentFolderPath = currentFolderPath
|
||||
? `${currentFolderPath}/${node.name}`
|
||||
: `/${node.name}`
|
||||
virtualPath = `${currentFolderPath}/AGENTS.md`
|
||||
}
|
||||
|
||||
const content = agentMdMap.get(node.id)
|
||||
if (content && content.trim().length > 0) {
|
||||
results.push({
|
||||
path: virtualPath,
|
||||
content: content.trim(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
}
|
||||
|
||||
export const assetService = new AssetService()
|
||||
|
||||
@@ -0,0 +1,249 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { AssetType } from '@shumai/db'
|
||||
import { dedupeSymlinksToTarget } from './symlink'
|
||||
|
||||
async function setupShareFolder(projectId: string, fileCount: number) {
|
||||
const shareRoot = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'share-root',
|
||||
type: AssetType.share_root,
|
||||
projectId,
|
||||
status: 'uploaded',
|
||||
fileCount,
|
||||
},
|
||||
})
|
||||
return shareRoot
|
||||
}
|
||||
|
||||
describe('dedupeSymlinksToTarget', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
async function setupProject() {
|
||||
const team = await prisma.team.create({ data: { name: 'Team ' + Date.now() } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Project ' + Date.now(), teamId: team.id },
|
||||
})
|
||||
const root = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'root',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
const fileA = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'a.txt',
|
||||
type: AssetType.file,
|
||||
parentId: root.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 10,
|
||||
},
|
||||
})
|
||||
const fileB = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'b.txt',
|
||||
type: AssetType.file,
|
||||
parentId: root.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 20,
|
||||
},
|
||||
})
|
||||
return { project, root, fileA, fileB }
|
||||
}
|
||||
|
||||
it('repoints one symlink per parent and deletes duplicates, decrementing fileCount once', async () => {
|
||||
const { project, root, fileA, fileB } = await setupProject()
|
||||
const shareRoot = await setupShareFolder(project.id, 2)
|
||||
|
||||
const symA = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileA.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: fileA.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
const symB = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileB.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: fileB.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: root.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
fileCount: 2,
|
||||
sizeByte: 30,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.$transaction((tx) =>
|
||||
dedupeSymlinksToTarget(tx, { targetIds: [fileA.id, fileB.id], newTargetId: stack.id }),
|
||||
)
|
||||
|
||||
// First symlink repointed to the stack, second deleted
|
||||
const repointed = await prisma.asset.findUnique({ where: { id: symA.id } })
|
||||
expect(repointed?.targetId).toBe(stack.id)
|
||||
expect(await prisma.asset.findUnique({ where: { id: symB.id } })).toBeNull()
|
||||
|
||||
// fileCount decremented exactly once (2 -> 1)
|
||||
const updatedShareRoot = await prisma.asset.findUnique({ where: { id: shareRoot.id } })
|
||||
expect(updatedShareRoot?.fileCount).toBe(1)
|
||||
})
|
||||
|
||||
it('deletes a file symlink when the parent already shows the stack', async () => {
|
||||
const { project, root, fileA } = await setupProject()
|
||||
const shareRoot = await setupShareFolder(project.id, 2)
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: root.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
fileCount: 1,
|
||||
sizeByte: 10,
|
||||
},
|
||||
})
|
||||
// Share already contains a symlink to the stack
|
||||
const stackSymlink = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: stack.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
// And a symlink to the file being moved into the stack
|
||||
const fileSymlink = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileA.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: fileA.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.$transaction((tx) =>
|
||||
dedupeSymlinksToTarget(tx, { targetIds: [fileA.id], newTargetId: stack.id }),
|
||||
)
|
||||
|
||||
// File symlink deleted, stack symlink untouched
|
||||
expect(await prisma.asset.findUnique({ where: { id: fileSymlink.id } })).toBeNull()
|
||||
const updatedStackSymlink = await prisma.asset.findUnique({ where: { id: stackSymlink.id } })
|
||||
expect(updatedStackSymlink?.targetId).toBe(stack.id)
|
||||
|
||||
// fileCount decremented once (2 -> 1)
|
||||
const updatedShareRoot = await prisma.asset.findUnique({ where: { id: shareRoot.id } })
|
||||
expect(updatedShareRoot?.fileCount).toBe(1)
|
||||
})
|
||||
|
||||
it('repoints symlinks in different share folders without changing fileCounts', async () => {
|
||||
const { project, root, fileA, fileB } = await setupProject()
|
||||
const share1 = await setupShareFolder(project.id, 1)
|
||||
const share2 = await setupShareFolder(project.id, 1)
|
||||
|
||||
const sym1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileA.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: share1.id,
|
||||
targetId: fileA.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
const sym2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileB.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: share2.id,
|
||||
targetId: fileB.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: root.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
fileCount: 2,
|
||||
sizeByte: 30,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.$transaction((tx) =>
|
||||
dedupeSymlinksToTarget(tx, { targetIds: [fileA.id, fileB.id], newTargetId: stack.id }),
|
||||
)
|
||||
|
||||
expect((await prisma.asset.findUnique({ where: { id: sym1.id } }))?.targetId).toBe(stack.id)
|
||||
expect((await prisma.asset.findUnique({ where: { id: sym2.id } }))?.targetId).toBe(stack.id)
|
||||
expect((await prisma.asset.findUnique({ where: { id: share1.id } }))?.fileCount).toBe(1)
|
||||
expect((await prisma.asset.findUnique({ where: { id: share2.id } }))?.fileCount).toBe(1)
|
||||
})
|
||||
|
||||
it('sets the provided name on repointed symlinks', async () => {
|
||||
const { project, root, fileA } = await setupProject()
|
||||
const shareRoot = await setupShareFolder(project.id, 1)
|
||||
|
||||
const sym = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileA.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: fileA.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: root.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
fileCount: 1,
|
||||
sizeByte: 10,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.$transaction((tx) =>
|
||||
dedupeSymlinksToTarget(tx, {
|
||||
targetIds: [fileA.id],
|
||||
newTargetId: stack.id,
|
||||
name: '',
|
||||
}),
|
||||
)
|
||||
|
||||
const updatedSym = await prisma.asset.findUnique({ where: { id: sym.id } })
|
||||
expect(updatedSym?.targetId).toBe(stack.id)
|
||||
expect(updatedSym?.name).toBe('')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,58 @@
|
||||
import { Prisma } from '@shumai/db'
|
||||
import { AssetType } from '@shumai/db'
|
||||
|
||||
interface DedupeSymlinksParams {
|
||||
/** Files being merged into a version stack — symlinks pointing at them are the candidates. */
|
||||
targetIds: string[]
|
||||
/** The version stack the files are moving into. */
|
||||
newTargetId: string
|
||||
/** Optional name to set on repointed symlinks ('' for stacks — display name comes from the latest version). */
|
||||
name?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* When files are merged into a version stack, symlinks in share folders that
|
||||
* pointed at those files must either be repointed to the stack (one per parent)
|
||||
* or deleted as duplicates. Per parent folder: if the parent already shows the
|
||||
* stack (either before this call or after the first repoint), delete the
|
||||
* incoming symlink and decrement the parent's fileCount; otherwise repoint the
|
||||
* first symlink to the stack.
|
||||
*/
|
||||
export async function dedupeSymlinksToTarget(
|
||||
tx: Prisma.TransactionClient,
|
||||
{ targetIds, newTargetId, name }: DedupeSymlinksParams,
|
||||
): Promise<void> {
|
||||
const existingSymlinks = await tx.asset.findMany({
|
||||
where: { targetId: { in: targetIds }, type: AssetType.symlink },
|
||||
})
|
||||
if (existingSymlinks.length === 0) return
|
||||
|
||||
// Parents that already display the stack (or will after the first repoint).
|
||||
const stackSymlinkParents = new Set(
|
||||
(
|
||||
await tx.asset.findMany({
|
||||
where: { targetId: newTargetId, type: AssetType.symlink },
|
||||
select: { parentId: true },
|
||||
})
|
||||
)
|
||||
.map((s) => s.parentId)
|
||||
.filter((id): id is string => !!id),
|
||||
)
|
||||
|
||||
for (const symlink of existingSymlinks) {
|
||||
if (!symlink.parentId) continue
|
||||
if (stackSymlinkParents.has(symlink.parentId)) {
|
||||
await tx.asset.delete({ where: { id: symlink.id } })
|
||||
await tx.asset.update({
|
||||
where: { id: symlink.parentId },
|
||||
data: { fileCount: { decrement: 1 } },
|
||||
})
|
||||
} else {
|
||||
stackSymlinkParents.add(symlink.parentId)
|
||||
await tx.asset.update({
|
||||
where: { id: symlink.id },
|
||||
data: { targetId: newTargetId, ...(name !== undefined ? { name } : {}) },
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ export enum ResourceType {
|
||||
MetadataField = 'metadataField',
|
||||
Skill = 'skill',
|
||||
Provider = 'provider',
|
||||
McpServer = 'mcpServer',
|
||||
Invite = 'invite',
|
||||
Comment = 'comment',
|
||||
AgentSession = 'agentSession',
|
||||
@@ -187,6 +188,15 @@ export class AuthzService {
|
||||
return { teamId: skill.teamId }
|
||||
}
|
||||
|
||||
case ResourceType.McpServer: {
|
||||
const server = await prisma.mcpServer.findUnique({
|
||||
where: { id },
|
||||
select: { teamId: true },
|
||||
})
|
||||
if (!server) throw new HTTPException(404, { message: 'MCP server not found' })
|
||||
return { teamId: server.teamId }
|
||||
}
|
||||
|
||||
case ResourceType.Provider: {
|
||||
const provider = await prisma.provider.findUnique({
|
||||
where: { id },
|
||||
|
||||
@@ -0,0 +1,219 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import * as os from 'os'
|
||||
import { GotenbergService } from './gotenberg'
|
||||
|
||||
describe('GotenbergService', () => {
|
||||
const originalEnv = process.env
|
||||
let tmpDir: string
|
||||
let testFilePath: string
|
||||
|
||||
beforeEach(() => {
|
||||
process.env = { ...originalEnv }
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gotenberg-test-'))
|
||||
testFilePath = path.join(tmpDir, 'test.docx')
|
||||
fs.writeFileSync(testFilePath, 'fake docx content')
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
process.env = originalEnv
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true })
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('isAvailable', () => {
|
||||
it('should return false if GOTENBERG_URL is not set', async () => {
|
||||
delete process.env.GOTENBERG_URL
|
||||
const service = new GotenbergService()
|
||||
expect(await service.isAvailable()).toBe(false)
|
||||
})
|
||||
|
||||
it('should return false if GOTENBERG_URL is empty', async () => {
|
||||
process.env.GOTENBERG_URL = ' '
|
||||
const service = new GotenbergService()
|
||||
expect(await service.isAvailable()).toBe(false)
|
||||
})
|
||||
|
||||
it('should return true when health check returns 200 OK', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
} as Response)
|
||||
|
||||
const service = new GotenbergService()
|
||||
const available = await service.isAvailable()
|
||||
|
||||
expect(available).toBe(true)
|
||||
expect(fetchSpy).toHaveBeenCalledWith(
|
||||
'http://gotenberg:3000/health',
|
||||
expect.objectContaining({
|
||||
method: 'GET',
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should include Basic Auth header if username and password are provided', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
process.env.GOTENBERG_BASIC_AUTH_USERNAME = 'user'
|
||||
process.env.GOTENBERG_BASIC_AUTH_PASSWORD = 'pass'
|
||||
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
} as Response)
|
||||
|
||||
const service = new GotenbergService()
|
||||
await service.isAvailable()
|
||||
|
||||
const expectedBasic = Buffer.from('user:pass').toString('base64')
|
||||
expect(fetchSpy).toHaveBeenCalledWith(
|
||||
'http://gotenberg:3000/health',
|
||||
expect.objectContaining({
|
||||
headers: {
|
||||
Authorization: `Basic ${expectedBasic}`,
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should return false when fetch fails', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('Network error'))
|
||||
|
||||
const service = new GotenbergService()
|
||||
expect(await service.isAvailable()).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('convertDocumentToPdf', () => {
|
||||
it('should throw error if GOTENBERG_URL is not set', async () => {
|
||||
delete process.env.GOTENBERG_URL
|
||||
const service = new GotenbergService()
|
||||
await expect(service.convertDocumentToPdf(testFilePath, 'test.docx')).rejects.toThrow(
|
||||
'Gotenberg URL is not configured',
|
||||
)
|
||||
})
|
||||
|
||||
it('should call libreoffice convert endpoint and return buffer', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000/'
|
||||
const mockPdfBuffer = Buffer.from('%PDF-1.7 mock output')
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
arrayBuffer: async () => mockPdfBuffer.buffer,
|
||||
} as unknown as Response)
|
||||
|
||||
const service = new GotenbergService()
|
||||
const result = await service.convertDocumentToPdf(testFilePath, 'test.docx', {
|
||||
landscape: true,
|
||||
})
|
||||
|
||||
expect(result).toBeDefined()
|
||||
expect(result.toString()).toContain('%PDF-1.7')
|
||||
|
||||
expect(fetchSpy).toHaveBeenCalledWith(
|
||||
'http://gotenberg:3000/forms/libreoffice/convert',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
body: expect.any(FormData),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should throw error if gotenberg returns non-200', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
statusText: 'Internal Server Error',
|
||||
text: async () => 'Conversion failed',
|
||||
} as unknown as Response)
|
||||
|
||||
const service = new GotenbergService()
|
||||
await expect(service.convertDocumentToPdf(testFilePath, 'test.docx')).rejects.toThrow(
|
||||
'Gotenberg conversion failed with status 500: Conversion failed',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('convertHtmlToPdf', () => {
|
||||
it('should call chromium html convert endpoint and return buffer', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
const mockPdfBuffer = Buffer.from('%PDF-1.4 html output')
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
arrayBuffer: async () => mockPdfBuffer.buffer,
|
||||
} as unknown as Response)
|
||||
|
||||
const htmlPath = path.join(tmpDir, 'test.html')
|
||||
fs.writeFileSync(htmlPath, '<h1>Hello</h1>')
|
||||
|
||||
const service = new GotenbergService()
|
||||
const result = await service.convertHtmlToPdf(htmlPath, 'test.html')
|
||||
|
||||
expect(result).toBeDefined()
|
||||
expect(fetchSpy).toHaveBeenCalledWith(
|
||||
'http://gotenberg:3000/forms/chromium/convert/html',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('convertMarkdownToPdf', () => {
|
||||
it('should call chromium markdown convert endpoint with index.html wrapper and return buffer', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
const mockPdfBuffer = Buffer.from('%PDF-1.4 markdown output')
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
arrayBuffer: async () => mockPdfBuffer.buffer,
|
||||
} as unknown as Response)
|
||||
|
||||
const mdPath = path.join(tmpDir, 'test.md')
|
||||
fs.writeFileSync(mdPath, '# Hello Gotenberg')
|
||||
|
||||
const service = new GotenbergService()
|
||||
const result = await service.convertMarkdownToPdf(mdPath, 'test.md')
|
||||
|
||||
expect(result).toBeDefined()
|
||||
expect(fetchSpy).toHaveBeenCalledWith(
|
||||
'http://gotenberg:3000/forms/chromium/convert/markdown',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
body: expect.any(FormData),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should rename non-.md filenames like README.markdown to file.md for Gotenberg endpoint compatibility', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
const mockPdfBuffer = Buffer.from('%PDF-1.4 markdown output')
|
||||
let capturedFormData: FormData | null = null
|
||||
|
||||
vi.spyOn(globalThis, 'fetch').mockImplementation(async (_url, options) => {
|
||||
capturedFormData = options?.body as FormData
|
||||
return {
|
||||
ok: true,
|
||||
status: 200,
|
||||
arrayBuffer: async () => mockPdfBuffer.buffer,
|
||||
} as unknown as Response
|
||||
})
|
||||
|
||||
const mdPath = path.join(tmpDir, 'README.markdown')
|
||||
fs.writeFileSync(mdPath, '# Hello Gotenberg')
|
||||
|
||||
const service = new GotenbergService()
|
||||
await service.convertMarkdownToPdf(mdPath, 'README.markdown')
|
||||
|
||||
expect(capturedFormData).not.toBeNull()
|
||||
const indexBlob = (capturedFormData as unknown as FormData).get('files') as File
|
||||
const indexHtmlText = await indexBlob.text()
|
||||
expect(indexHtmlText).toContain('{{ toHTML "file.md" }}')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,178 @@
|
||||
import * as fs from 'fs'
|
||||
|
||||
export interface GotenbergConvertOptions {
|
||||
landscape?: boolean
|
||||
}
|
||||
|
||||
export class GotenbergService {
|
||||
private getUrl(): string | undefined {
|
||||
const url = process.env.GOTENBERG_URL?.trim()
|
||||
return url ? url.replace(/\/+$/, '') : undefined
|
||||
}
|
||||
|
||||
private getAuthHeader(): string | undefined {
|
||||
const username = process.env.GOTENBERG_BASIC_AUTH_USERNAME
|
||||
const password = process.env.GOTENBERG_BASIC_AUTH_PASSWORD
|
||||
|
||||
if (username && password) {
|
||||
const credentials = Buffer.from(`${username}:${password}`).toString('base64')
|
||||
return `Basic ${credentials}`
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
async isAvailable(): Promise<boolean> {
|
||||
const baseUrl = this.getUrl()
|
||||
if (!baseUrl) {
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
const headers: Record<string, string> = {}
|
||||
const authHeader = this.getAuthHeader()
|
||||
if (authHeader) {
|
||||
headers['Authorization'] = authHeader
|
||||
}
|
||||
|
||||
const response = await fetch(`${baseUrl}/health`, {
|
||||
method: 'GET',
|
||||
headers,
|
||||
signal: AbortSignal.timeout(3000),
|
||||
})
|
||||
|
||||
return response.ok
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async convertDocumentToPdf(
|
||||
filePath: string,
|
||||
filename: string,
|
||||
options: GotenbergConvertOptions = {},
|
||||
): Promise<Buffer> {
|
||||
const baseUrl = this.getUrl()
|
||||
if (!baseUrl) {
|
||||
throw new Error('Gotenberg URL is not configured.')
|
||||
}
|
||||
|
||||
const fileBytes = fs.readFileSync(filePath)
|
||||
const formData = new FormData()
|
||||
formData.append('files', new Blob([fileBytes]), filename)
|
||||
|
||||
if (options.landscape) {
|
||||
formData.append('landscape', 'true')
|
||||
}
|
||||
|
||||
const headers: Record<string, string> = {}
|
||||
const authHeader = this.getAuthHeader()
|
||||
if (authHeader) {
|
||||
headers['Authorization'] = authHeader
|
||||
}
|
||||
|
||||
const response = await fetch(`${baseUrl}/forms/libreoffice/convert`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: formData,
|
||||
signal: AbortSignal.timeout(60000),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text().catch(() => '')
|
||||
throw new Error(
|
||||
`Gotenberg conversion failed with status ${response.status}: ${errorText || response.statusText}`,
|
||||
)
|
||||
}
|
||||
|
||||
const arrayBuffer = await response.arrayBuffer()
|
||||
return Buffer.from(arrayBuffer)
|
||||
}
|
||||
|
||||
async convertHtmlToPdf(filePath: string, filename: string): Promise<Buffer> {
|
||||
const baseUrl = this.getUrl()
|
||||
if (!baseUrl) {
|
||||
throw new Error('Gotenberg URL is not configured.')
|
||||
}
|
||||
|
||||
const fileBytes = fs.readFileSync(filePath)
|
||||
const formData = new FormData()
|
||||
const targetFilename =
|
||||
filename.toLowerCase().endsWith('.html') || filename.toLowerCase().endsWith('.htm')
|
||||
? filename
|
||||
: 'index.html'
|
||||
formData.append('files', new Blob([fileBytes]), targetFilename)
|
||||
|
||||
const headers: Record<string, string> = {}
|
||||
const authHeader = this.getAuthHeader()
|
||||
if (authHeader) {
|
||||
headers['Authorization'] = authHeader
|
||||
}
|
||||
|
||||
const response = await fetch(`${baseUrl}/forms/chromium/convert/html`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: formData,
|
||||
signal: AbortSignal.timeout(60000),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text().catch(() => '')
|
||||
throw new Error(
|
||||
`Gotenberg HTML conversion failed with status ${response.status}: ${errorText || response.statusText}`,
|
||||
)
|
||||
}
|
||||
|
||||
const arrayBuffer = await response.arrayBuffer()
|
||||
return Buffer.from(arrayBuffer)
|
||||
}
|
||||
|
||||
async convertMarkdownToPdf(filePath: string, filename: string): Promise<Buffer> {
|
||||
const baseUrl = this.getUrl()
|
||||
if (!baseUrl) {
|
||||
throw new Error('Gotenberg URL is not configured.')
|
||||
}
|
||||
|
||||
const mdFilename = filename.toLowerCase().endsWith('.md') ? filename : 'file.md'
|
||||
|
||||
const htmlTemplate = `<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>PDF</title>
|
||||
</head>
|
||||
<body>
|
||||
{{ toHTML "${mdFilename}" }}
|
||||
</body>
|
||||
</html>`
|
||||
|
||||
const fileBytes = fs.readFileSync(filePath)
|
||||
const formData = new FormData()
|
||||
formData.append('files', new Blob([htmlTemplate], { type: 'text/html' }), 'index.html')
|
||||
formData.append('files', new Blob([fileBytes]), mdFilename)
|
||||
|
||||
const headers: Record<string, string> = {}
|
||||
const authHeader = this.getAuthHeader()
|
||||
if (authHeader) {
|
||||
headers['Authorization'] = authHeader
|
||||
}
|
||||
|
||||
const response = await fetch(`${baseUrl}/forms/chromium/convert/markdown`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: formData,
|
||||
signal: AbortSignal.timeout(60000),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text().catch(() => '')
|
||||
throw new Error(
|
||||
`Gotenberg Markdown conversion failed with status ${response.status}: ${errorText || response.statusText}`,
|
||||
)
|
||||
}
|
||||
|
||||
const arrayBuffer = await response.arrayBuffer()
|
||||
return Buffer.from(arrayBuffer)
|
||||
}
|
||||
}
|
||||
|
||||
export const gotenbergService = new GotenbergService()
|
||||
@@ -23,3 +23,6 @@ export * from './hyrumtoken'
|
||||
export * from './pagination'
|
||||
export * from './chat/chat'
|
||||
export * from './ai-usage/ai-usage'
|
||||
export * from './watermark/watermark'
|
||||
export * from './gotenberg/gotenberg'
|
||||
export * from './mcp/mcp-service'
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
import { describe, it, expect, beforeAll } from 'vitest'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { McpDbStore } from './mcp-db-store'
|
||||
|
||||
describe('McpDbStore', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
let store: McpDbStore
|
||||
let serverId: string
|
||||
|
||||
beforeAll(async () => {
|
||||
store = new McpDbStore()
|
||||
const team = await prisma.team.create({ data: { name: 'MCP DB Store Team' } })
|
||||
const server = await prisma.mcpServer.create({
|
||||
data: {
|
||||
name: 'github',
|
||||
url: 'https://mcp.example.com/github',
|
||||
teamId: team.id,
|
||||
},
|
||||
})
|
||||
serverId = server.id
|
||||
})
|
||||
|
||||
it('returns undefined when no credentials exist', async () => {
|
||||
expect(await store.getAuthForUrl(serverId, 'https://mcp.example.com/github')).toBeUndefined()
|
||||
expect(await store.hasStoredTokens(serverId)).toBe(false)
|
||||
expect(await store.isTokenExpired(serverId)).toBeNull()
|
||||
})
|
||||
|
||||
it('saves and reads tokens bound to the URL', async () => {
|
||||
await store.updateTokens(
|
||||
serverId,
|
||||
{ accessToken: 'tok-1', refreshToken: 'ref-1' },
|
||||
'https://mcp.example.com/github',
|
||||
)
|
||||
const entry = await store.getAuthForUrl(serverId, 'https://mcp.example.com/github')
|
||||
expect(entry?.tokens?.accessToken).toBe('tok-1')
|
||||
expect(entry?.tokens?.refreshToken).toBe('ref-1')
|
||||
expect(await store.hasStoredTokens(serverId)).toBe(true)
|
||||
})
|
||||
|
||||
it('invalidates credentials when the URL changes', async () => {
|
||||
expect(await store.getAuthForUrl(serverId, 'https://other.example.com')).toBeUndefined()
|
||||
expect(await store.hasStoredTokens(serverId)).toBe(false)
|
||||
})
|
||||
|
||||
it('saves and clears client info', async () => {
|
||||
await store.updateClientInfo(
|
||||
serverId,
|
||||
{ clientId: 'client-1', clientSecret: 'secret-1' },
|
||||
'https://mcp.example.com/github',
|
||||
)
|
||||
let entry = await store.getAuthForUrl(serverId, 'https://mcp.example.com/github')
|
||||
expect(entry?.clientInfo?.clientId).toBe('client-1')
|
||||
|
||||
await store.clearClientInfo(serverId)
|
||||
entry = await store.getAuthForUrl(serverId, 'https://mcp.example.com/github')
|
||||
expect(entry?.clientInfo).toBeUndefined()
|
||||
})
|
||||
|
||||
it('saves/clears the PKCE code verifier', async () => {
|
||||
await store.updateCodeVerifier(serverId, 'verifier-abc', 'https://mcp.example.com/github')
|
||||
let entry = await store.getAuthForUrl(serverId, 'https://mcp.example.com/github')
|
||||
expect(entry?.codeVerifier).toBe('verifier-abc')
|
||||
|
||||
await store.clearCodeVerifier(serverId)
|
||||
entry = await store.getAuthForUrl(serverId, 'https://mcp.example.com/github')
|
||||
expect(entry?.codeVerifier).toBeUndefined()
|
||||
})
|
||||
|
||||
it('saves/clears the CSRF state', async () => {
|
||||
await store.updateOauthState(serverId, 'state-xyz', 'https://mcp.example.com/github')
|
||||
expect(await store.getOauthState(serverId)).toBe('state-xyz')
|
||||
|
||||
await store.clearOauthState(serverId)
|
||||
expect(await store.getOauthState(serverId)).toBeUndefined()
|
||||
})
|
||||
|
||||
it('detects expired tokens', async () => {
|
||||
await store.updateTokens(
|
||||
serverId,
|
||||
{ accessToken: 'tok-expired', expiresAt: Math.floor(Date.now() / 1000) - 10 },
|
||||
'https://mcp.example.com/github',
|
||||
)
|
||||
expect(await store.isTokenExpired(serverId)).toBe(true)
|
||||
|
||||
await store.updateTokens(
|
||||
serverId,
|
||||
{ accessToken: 'tok-valid', expiresAt: Math.floor(Date.now() / 1000) + 3600 },
|
||||
'https://mcp.example.com/github',
|
||||
)
|
||||
expect(await store.isTokenExpired(serverId)).toBe(false)
|
||||
})
|
||||
|
||||
it('treats tokens without an expiry as never-expiring', async () => {
|
||||
await store.updateTokens(
|
||||
serverId,
|
||||
{ accessToken: 'tok-no-expiry' },
|
||||
'https://mcp.example.com/github',
|
||||
)
|
||||
expect(await store.isTokenExpired(serverId)).toBe(false)
|
||||
})
|
||||
|
||||
it('clears tokens only', async () => {
|
||||
await store.updateTokens(serverId, { accessToken: 'tok-x' }, 'https://mcp.example.com/github')
|
||||
await store.clearTokens(serverId)
|
||||
expect(await store.hasStoredTokens(serverId)).toBe(false)
|
||||
})
|
||||
|
||||
it('clears all credentials', async () => {
|
||||
await store.updateTokens(serverId, { accessToken: 'tok-y' }, 'https://mcp.example.com/github')
|
||||
await store.updateClientInfo(serverId, { clientId: 'c' }, 'https://mcp.example.com/github')
|
||||
await store.clearAllCredentials(serverId)
|
||||
expect(await store.getAuthForUrl(serverId, 'https://mcp.example.com/github')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('persists an in-flight pendingAuth with a TTL and expires it', async () => {
|
||||
const ttl = await store.getPendingAuthTtlSeconds()
|
||||
await store.savePendingAuth(serverId, {
|
||||
state: 'pending-state',
|
||||
authorizationUrl: 'https://auth.example.com/authorize?state=pending-state',
|
||||
discovery: { scope: 'read' },
|
||||
expiresAt: Math.floor(Date.now() / 1000) + ttl,
|
||||
})
|
||||
const pending = await store.getPendingAuth(serverId)
|
||||
expect(pending?.state).toBe('pending-state')
|
||||
expect(pending?.authorizationUrl).toContain('pending-state')
|
||||
|
||||
// Expired pendingAuth is cleared on read.
|
||||
await store.savePendingAuth(serverId, {
|
||||
state: 'stale',
|
||||
authorizationUrl: 'https://auth.example.com/authorize',
|
||||
discovery: {},
|
||||
expiresAt: Math.floor(Date.now() / 1000) - 10,
|
||||
})
|
||||
expect(await store.getPendingAuth(serverId)).toBeUndefined()
|
||||
|
||||
await store.clearPendingAuth(serverId)
|
||||
expect(await store.getPendingAuth(serverId)).toBeUndefined()
|
||||
})
|
||||
|
||||
it('savePendingAuth does not clobber an in-flight discovery snapshot', async () => {
|
||||
// Simulates the SDK flow: the provider's saveDiscoveryState() stores a
|
||||
// snapshot inside the pendingAuth column, then startAuth() calls
|
||||
// savePendingAuth(). The snapshot MUST survive so the callback leg can
|
||||
// satisfy the SEP-2352 issuer check.
|
||||
await store.saveDiscoverySnapshot(serverId, 'https://mcp.example.com/github', {
|
||||
authorizationServerUrl: 'https://kling.ai/auth',
|
||||
authorizationServerMetadata: { issuer: 'https://kling.ai/auth' },
|
||||
})
|
||||
|
||||
await store.savePendingAuth(serverId, {
|
||||
state: 'state-abc',
|
||||
authorizationUrl: 'https://kling.ai/authorize',
|
||||
discovery: {},
|
||||
expiresAt: Math.floor(Date.now() / 1000) + 300,
|
||||
})
|
||||
|
||||
const snapshot = await store.getDiscoverySnapshot(serverId)
|
||||
expect(snapshot).toBeDefined()
|
||||
expect(snapshot?.['authorizationServerUrl']).toBe('https://kling.ai/auth')
|
||||
|
||||
const pending = await store.getPendingAuth(serverId)
|
||||
expect(pending?.state).toBe('state-abc')
|
||||
})
|
||||
|
||||
it('persists and clears the discovery snapshot', async () => {
|
||||
await store.saveDiscoverySnapshot(serverId, 'https://mcp.example.com/github', {
|
||||
authorizationServerUrl: 'https://issuer.example.com',
|
||||
})
|
||||
const snapshot = await store.getDiscoverySnapshot(serverId)
|
||||
expect(snapshot?.authorizationServerUrl).toBe('https://issuer.example.com')
|
||||
|
||||
await store.clearDiscoverySnapshot(serverId)
|
||||
expect(await store.getDiscoverySnapshot(serverId)).toBeUndefined()
|
||||
})
|
||||
|
||||
it('deletes the credential row when the server is deleted (cascade)', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Cascade Team' } })
|
||||
const server = await prisma.mcpServer.create({
|
||||
data: { name: 'cascade-srv', url: 'https://mcp.example.com/cascade', teamId: team.id },
|
||||
})
|
||||
await store.updateTokens(
|
||||
server.id,
|
||||
{ accessToken: 'tok-cascade' },
|
||||
'https://mcp.example.com/cascade',
|
||||
)
|
||||
await prisma.mcpServer.delete({ where: { id: server.id } })
|
||||
const leftover = await prisma.mcpServerCredential.findUnique({ where: { serverId: server.id } })
|
||||
expect(leftover).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,388 @@
|
||||
import { prisma, Prisma } from '@shumai/db'
|
||||
|
||||
export interface StoredTokens {
|
||||
accessToken: string
|
||||
refreshToken?: string
|
||||
expiresAt?: number // Unix timestamp in seconds
|
||||
scope?: string
|
||||
/** SEP-2352 authorization-server issuer binding */
|
||||
issuer?: string
|
||||
}
|
||||
|
||||
export interface StoredClientInfo {
|
||||
clientId: string
|
||||
clientSecret?: string
|
||||
clientIdIssuedAt?: number
|
||||
clientSecretExpiresAt?: number
|
||||
redirectUris?: string[]
|
||||
issuer?: string
|
||||
configPreRegistered?: boolean
|
||||
}
|
||||
|
||||
export interface AuthEntry {
|
||||
tokens?: StoredTokens
|
||||
clientInfo?: StoredClientInfo
|
||||
codeVerifier?: string
|
||||
oauthState?: string
|
||||
serverUrl?: string // Track the URL these credentials are for
|
||||
}
|
||||
|
||||
export interface PendingAuth {
|
||||
state: string
|
||||
authorizationUrl: string
|
||||
discovery: Record<string, unknown>
|
||||
expiresAt: number // Unix timestamp in seconds
|
||||
grantType?: 'authorization_code' | 'client_credentials'
|
||||
redirectUri?: string
|
||||
}
|
||||
|
||||
/** How long an in-flight authorization_code flow is kept in the DB (5 minutes). */
|
||||
const PENDING_AUTH_TTL_SECONDS = 5 * 60
|
||||
|
||||
function rowToAuthEntry(row: {
|
||||
serverUrl: string
|
||||
tokens: unknown
|
||||
clientInfo: unknown
|
||||
codeVerifier: string | null
|
||||
oauthState: string | null
|
||||
pendingAuth: unknown
|
||||
}): {
|
||||
entry: AuthEntry
|
||||
pendingAuth?: PendingAuth
|
||||
} {
|
||||
const entry: AuthEntry = {
|
||||
serverUrl: row.serverUrl,
|
||||
...(row.tokens ? { tokens: row.tokens as StoredTokens } : {}),
|
||||
...(row.clientInfo ? { clientInfo: row.clientInfo as StoredClientInfo } : {}),
|
||||
...(row.codeVerifier ? { codeVerifier: row.codeVerifier } : {}),
|
||||
...(row.oauthState ? { oauthState: row.oauthState } : {}),
|
||||
}
|
||||
let pendingAuth: PendingAuth | undefined
|
||||
if (row.pendingAuth && typeof row.pendingAuth === 'object') {
|
||||
const raw = row.pendingAuth as Partial<PendingAuth>
|
||||
if (typeof raw.state === 'string' && typeof raw.expiresAt === 'number') {
|
||||
pendingAuth = {
|
||||
state: raw.state,
|
||||
authorizationUrl: raw.authorizationUrl ?? '',
|
||||
discovery: raw.discovery ?? {},
|
||||
expiresAt: raw.expiresAt,
|
||||
...(raw.grantType ? { grantType: raw.grantType } : {}),
|
||||
...(raw.redirectUri ? { redirectUri: raw.redirectUri } : {}),
|
||||
}
|
||||
}
|
||||
}
|
||||
return { entry, pendingAuth }
|
||||
}
|
||||
|
||||
/**
|
||||
* DB-backed credential store for MCP OAuth state (port of pi-mcp-adapter's
|
||||
* mcp-auth.ts storage layer, replacing the OS keyring/files with an
|
||||
* `McpServerCredential` row per server).
|
||||
*/
|
||||
export class McpDbStore {
|
||||
constructor(private readonly prismaClient: typeof prisma = prisma) {}
|
||||
|
||||
private async getRow(serverId: string) {
|
||||
return this.prismaClient.mcpServerCredential.findUnique({
|
||||
where: { serverId },
|
||||
})
|
||||
}
|
||||
|
||||
private async ensureRow(serverId: string, serverUrl?: string) {
|
||||
const existing = await this.getRow(serverId)
|
||||
if (existing) return existing
|
||||
if (!serverUrl) throw new Error(`No credential row for MCP server ${serverId}`)
|
||||
return this.prismaClient.mcpServerCredential.create({
|
||||
data: {
|
||||
serverId,
|
||||
serverUrl,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the auth entry for a server, validated against the current URL.
|
||||
* Returns undefined when no entry exists or the URL changed (credentials invalid).
|
||||
*/
|
||||
async getAuthForUrl(serverId: string, serverUrl: string): Promise<AuthEntry | undefined> {
|
||||
const row = await this.getRow(serverId)
|
||||
if (!row) return undefined
|
||||
// If no serverUrl is stored or it changed, credentials are invalid.
|
||||
if (!row.serverUrl || row.serverUrl !== serverUrl) return undefined
|
||||
return rowToAuthEntry(row).entry
|
||||
}
|
||||
|
||||
async getAuthEntry(serverId: string): Promise<AuthEntry | undefined> {
|
||||
const row = await this.getRow(serverId)
|
||||
if (!row) return undefined
|
||||
return rowToAuthEntry(row).entry
|
||||
}
|
||||
|
||||
async saveAuthEntry(serverId: string, entry: AuthEntry, serverUrl?: string): Promise<void> {
|
||||
const row = await this.getRow(serverId)
|
||||
const serverUrlValue = serverUrl ?? entry.serverUrl ?? row?.serverUrl
|
||||
// Do NOT merge the row's credential fields back in: callers build `entry`
|
||||
// from getAuthEntry() (all current fields) and delete fields to clear
|
||||
// them. Re-adding row fields would leak cleared values.
|
||||
const merged: AuthEntry = { ...entry }
|
||||
if (serverUrlValue) {
|
||||
merged.serverUrl = serverUrlValue
|
||||
if (row && serverUrlValue !== row.serverUrl) {
|
||||
// URL changed: stale sub-credentials are dropped.
|
||||
delete merged.tokens
|
||||
delete merged.clientInfo
|
||||
delete merged.codeVerifier
|
||||
delete merged.oauthState
|
||||
}
|
||||
}
|
||||
const data = {
|
||||
serverUrl: merged.serverUrl ?? serverUrlValue ?? '',
|
||||
tokens: merged.tokens ?? undefined,
|
||||
clientInfo: merged.clientInfo ?? undefined,
|
||||
codeVerifier: merged.codeVerifier ?? undefined,
|
||||
oauthState: merged.oauthState ?? undefined,
|
||||
}
|
||||
if (row) {
|
||||
await this.prismaClient.mcpServerCredential.update({
|
||||
where: { serverId },
|
||||
data: {
|
||||
serverUrl: data.serverUrl,
|
||||
tokens:
|
||||
data.tokens === undefined
|
||||
? Prisma.JsonNull
|
||||
: (data.tokens as PrismaJson.McpStoredTokens),
|
||||
clientInfo:
|
||||
data.clientInfo === undefined
|
||||
? Prisma.JsonNull
|
||||
: (data.clientInfo as PrismaJson.McpStoredClientInfo),
|
||||
codeVerifier: data.codeVerifier ?? null,
|
||||
oauthState: data.oauthState ?? null,
|
||||
},
|
||||
})
|
||||
} else if (serverUrlValue) {
|
||||
await this.prismaClient.mcpServerCredential.create({
|
||||
data: {
|
||||
serverId,
|
||||
serverUrl: serverUrlValue,
|
||||
tokens: data.tokens as PrismaJson.McpStoredTokens | undefined,
|
||||
clientInfo: data.clientInfo as PrismaJson.McpStoredClientInfo | undefined,
|
||||
codeVerifier: data.codeVerifier,
|
||||
oauthState: data.oauthState,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async updateTokens(serverId: string, tokens: StoredTokens, serverUrl?: string): Promise<void> {
|
||||
const entry = (await this.getAuthEntry(serverId)) ?? {}
|
||||
if (serverUrl && entry.serverUrl && entry.serverUrl !== serverUrl) {
|
||||
delete entry.clientInfo
|
||||
delete entry.codeVerifier
|
||||
delete entry.oauthState
|
||||
}
|
||||
entry.tokens = tokens
|
||||
await this.saveAuthEntry(serverId, entry, serverUrl)
|
||||
}
|
||||
|
||||
async updateClientInfo(
|
||||
serverId: string,
|
||||
clientInfo: StoredClientInfo,
|
||||
serverUrl?: string,
|
||||
): Promise<void> {
|
||||
const entry = (await this.getAuthEntry(serverId)) ?? {}
|
||||
if (serverUrl && entry.serverUrl && entry.serverUrl !== serverUrl) {
|
||||
delete entry.tokens
|
||||
delete entry.codeVerifier
|
||||
delete entry.oauthState
|
||||
}
|
||||
entry.clientInfo = clientInfo
|
||||
await this.saveAuthEntry(serverId, entry, serverUrl)
|
||||
}
|
||||
|
||||
async updateCodeVerifier(
|
||||
serverId: string,
|
||||
codeVerifier: string,
|
||||
serverUrl?: string,
|
||||
): Promise<void> {
|
||||
const entry = (await this.getAuthEntry(serverId)) ?? {}
|
||||
if (serverUrl && entry.serverUrl && entry.serverUrl !== serverUrl) {
|
||||
delete entry.tokens
|
||||
delete entry.clientInfo
|
||||
delete entry.oauthState
|
||||
}
|
||||
entry.codeVerifier = codeVerifier
|
||||
await this.saveAuthEntry(serverId, entry, serverUrl)
|
||||
}
|
||||
|
||||
async clearCodeVerifier(serverId: string): Promise<void> {
|
||||
const entry = await this.getAuthEntry(serverId)
|
||||
if (entry) {
|
||||
delete entry.codeVerifier
|
||||
await this.saveAuthEntry(serverId, entry)
|
||||
}
|
||||
}
|
||||
|
||||
async updateOauthState(serverId: string, state: string, serverUrl?: string): Promise<void> {
|
||||
const entry = (await this.getAuthEntry(serverId)) ?? {}
|
||||
if (serverUrl && entry.serverUrl && entry.serverUrl !== serverUrl) {
|
||||
delete entry.tokens
|
||||
delete entry.clientInfo
|
||||
delete entry.codeVerifier
|
||||
}
|
||||
entry.oauthState = state
|
||||
await this.saveAuthEntry(serverId, entry, serverUrl)
|
||||
}
|
||||
|
||||
async getOauthState(serverId: string): Promise<string | undefined> {
|
||||
const row = await this.getRow(serverId)
|
||||
return row?.oauthState ?? undefined
|
||||
}
|
||||
|
||||
async clearOauthState(serverId: string): Promise<void> {
|
||||
const entry = await this.getAuthEntry(serverId)
|
||||
if (entry) {
|
||||
delete entry.oauthState
|
||||
await this.saveAuthEntry(serverId, entry)
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns null when no tokens exist, false when valid, true when expired. */
|
||||
async isTokenExpired(serverId: string): Promise<boolean | null> {
|
||||
const row = await this.getRow(serverId)
|
||||
const tokens = row?.tokens as StoredTokens | null | undefined
|
||||
if (!tokens?.accessToken) return null
|
||||
if (!tokens.expiresAt) return false
|
||||
return tokens.expiresAt < Date.now() / 1000
|
||||
}
|
||||
|
||||
async hasStoredTokens(serverId: string): Promise<boolean> {
|
||||
const row = await this.getRow(serverId)
|
||||
const tokens = row?.tokens as StoredTokens | null | undefined
|
||||
return !!tokens?.accessToken
|
||||
}
|
||||
|
||||
async clearAllCredentials(serverId: string): Promise<void> {
|
||||
const row = await this.getRow(serverId)
|
||||
if (!row) return
|
||||
await this.prismaClient.mcpServerCredential.delete({ where: { serverId } })
|
||||
}
|
||||
|
||||
async clearClientInfo(serverId: string): Promise<void> {
|
||||
const entry = await this.getAuthEntry(serverId)
|
||||
if (entry) {
|
||||
delete entry.clientInfo
|
||||
await this.saveAuthEntry(serverId, entry)
|
||||
}
|
||||
}
|
||||
|
||||
async clearTokens(serverId: string): Promise<void> {
|
||||
const entry = await this.getAuthEntry(serverId)
|
||||
if (entry) {
|
||||
delete entry.tokens
|
||||
await this.saveAuthEntry(serverId, entry)
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// In-flight authorization_code flow (pendingAuth)
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
async savePendingAuth(serverId: string, pending: PendingAuth): Promise<void> {
|
||||
const row = await this.getRow(serverId)
|
||||
if (!row) {
|
||||
const server = await this.prismaClient.mcpServer.findUnique({
|
||||
where: { id: serverId },
|
||||
select: { url: true },
|
||||
})
|
||||
if (!server) throw new Error(`MCP server ${serverId} not found`)
|
||||
await this.ensureRow(serverId, server.url)
|
||||
}
|
||||
// Preserve an in-flight discovery snapshot: the OAuth provider's
|
||||
// saveDiscoveryState() lives in the same JSON column, and the callback
|
||||
// leg needs it for the SEP-2352 issuer check.
|
||||
const existing = (row?.pendingAuth as Record<string, unknown> | null | undefined) ?? {}
|
||||
const discoverySnapshot = existing['discoverySnapshot']
|
||||
const data = discoverySnapshot !== undefined ? { ...pending, discoverySnapshot } : pending
|
||||
await this.prismaClient.mcpServerCredential.update({
|
||||
where: { serverId },
|
||||
data: { pendingAuth: data as unknown as PrismaJson.McpPendingAuth },
|
||||
})
|
||||
}
|
||||
|
||||
async getPendingAuth(serverId: string): Promise<PendingAuth | undefined> {
|
||||
const row = await this.getRow(serverId)
|
||||
if (!row) return undefined
|
||||
const { pendingAuth } = rowToAuthEntry(row)
|
||||
if (!pendingAuth) return undefined
|
||||
if (pendingAuth.expiresAt < Date.now() / 1000) {
|
||||
await this.clearPendingAuth(serverId)
|
||||
return undefined
|
||||
}
|
||||
return pendingAuth
|
||||
}
|
||||
|
||||
async clearPendingAuth(serverId: string): Promise<void> {
|
||||
const row = await this.getRow(serverId)
|
||||
if (!row) return
|
||||
await this.prismaClient.mcpServerCredential.update({
|
||||
where: { serverId },
|
||||
data: { pendingAuth: Prisma.JsonNull },
|
||||
})
|
||||
}
|
||||
|
||||
async getPendingAuthTtlSeconds(): Promise<number> {
|
||||
return PENDING_AUTH_TTL_SECONDS
|
||||
}
|
||||
|
||||
/** Ensure a credential row exists for a server (used when persisting any auth state). */
|
||||
async ensureCredentialRow(serverId: string, serverUrl: string): Promise<void> {
|
||||
await this.ensureRow(serverId, serverUrl)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Discovery snapshot (survives from startAuth to the callback leg)
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
async saveDiscoverySnapshot(serverId: string, serverUrl: string, state: unknown): Promise<void> {
|
||||
await this.ensureRow(serverId, serverUrl)
|
||||
const row = await this.getRow(serverId)
|
||||
const existing = (row?.pendingAuth as Record<string, unknown> | null | undefined) ?? {}
|
||||
await this.prismaClient.mcpServerCredential.update({
|
||||
where: { serverId },
|
||||
data: {
|
||||
pendingAuth: {
|
||||
...existing,
|
||||
discoverySnapshot: state,
|
||||
} as unknown as PrismaJson.McpPendingAuth,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async getDiscoverySnapshot(serverId: string): Promise<Record<string, unknown> | undefined> {
|
||||
const row = await this.getRow(serverId)
|
||||
const pending = (row?.pendingAuth as Record<string, unknown> | null | undefined) ?? {}
|
||||
const snapshot = pending['discoverySnapshot']
|
||||
return snapshot && typeof snapshot === 'object'
|
||||
? (snapshot as Record<string, unknown>)
|
||||
: undefined
|
||||
}
|
||||
|
||||
async clearDiscoverySnapshot(serverId: string): Promise<void> {
|
||||
const row = await this.getRow(serverId)
|
||||
if (!row) return
|
||||
const pending = (row.pendingAuth as Record<string, unknown> | null | undefined) ?? {}
|
||||
const rest: Record<string, unknown> = { ...pending }
|
||||
delete rest['discoverySnapshot']
|
||||
await this.prismaClient.mcpServerCredential.update({
|
||||
where: { serverId },
|
||||
data: {
|
||||
pendingAuth:
|
||||
Object.keys(rest).length > 0
|
||||
? (rest as unknown as PrismaJson.McpPendingAuth)
|
||||
: Prisma.JsonNull,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const mcpDbStore = new McpDbStore()
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Direct MCP tools — per-server opt-in registration.
|
||||
*
|
||||
* When a server's config sets `directTools` to an array of tool names, its
|
||||
* allowed tools (matching the original or `{server}_{tool}` prefixed name,
|
||||
* after excludeTools filters) are registered as native AgentTools with
|
||||
* prefixed names `{server}_{tool}`. The `mcp` proxy tool stays registered for
|
||||
* discovery/auth/status and for tools not in direct mode.
|
||||
*/
|
||||
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { AgentTool, AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import '@shumai/db/src/prisma-json-types'
|
||||
import type { ToolMetadata } from './mcp-tool-registry'
|
||||
import { isToolAllowed } from './mcp-tool-registry'
|
||||
import { toToolParameters } from './mcp-tool-schema'
|
||||
|
||||
export interface DirectToolCallOptions {
|
||||
serverId: string
|
||||
serverName: string
|
||||
callTool(
|
||||
serverId: string,
|
||||
toolName: string,
|
||||
args: Record<string, unknown>,
|
||||
): Promise<{
|
||||
ok: boolean
|
||||
content: AgentToolResult<Record<string, unknown>>['content']
|
||||
}>
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide whether a tool is exposed as a native direct tool. Matches the
|
||||
* original MCP name or the `{server}_{tool}` prefixed name against the
|
||||
* configured directTools array. Shared by direct registration and the proxy
|
||||
* description counts so the two can never drift apart.
|
||||
*/
|
||||
export function isDirectTool(
|
||||
originalName: string,
|
||||
prefixedName: string,
|
||||
directTools?: string[],
|
||||
): boolean {
|
||||
if (!Array.isArray(directTools)) return false
|
||||
return directTools.includes(originalName) || directTools.includes(prefixedName)
|
||||
}
|
||||
|
||||
/**
|
||||
* Build direct AgentTools for a server in direct-tools mode.
|
||||
* Returns [] when directTools is not enabled or no tools pass the filters.
|
||||
*/
|
||||
export function buildDirectTools(
|
||||
metadata: ToolMetadata[],
|
||||
config: PrismaJson.McpServerConfig | undefined,
|
||||
opts: DirectToolCallOptions,
|
||||
): AgentTool[] {
|
||||
const directTools = config?.directTools
|
||||
if (!Array.isArray(directTools) || directTools.length === 0) return []
|
||||
|
||||
const tools: AgentTool[] = []
|
||||
for (const tool of metadata) {
|
||||
if (!isToolAllowed(tool.name, tool.originalName, config?.excludeTools)) continue
|
||||
|
||||
if (!isDirectTool(tool.originalName, tool.name, directTools)) continue
|
||||
const parameters = toToolParameters(tool.inputSchema)
|
||||
tools.push({
|
||||
name: tool.name,
|
||||
label: tool.name,
|
||||
description:
|
||||
tool.description || `Call ${tool.originalName} on MCP server ${opts.serverName}.`,
|
||||
parameters: parameters as unknown as import('typebox').TSchema,
|
||||
execute: async (_toolCallId, args): Promise<AgentToolResult<Record<string, unknown>>> => {
|
||||
const result = await opts.callTool(
|
||||
opts.serverId,
|
||||
tool.originalName,
|
||||
args as Record<string, unknown>,
|
||||
)
|
||||
return {
|
||||
content: result.content,
|
||||
details: { server: opts.serverName, tool: tool.originalName },
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
return tools
|
||||
}
|
||||
|
||||
// Re-exported for tests that need a fallback parameter schema.
|
||||
export { Type }
|
||||
@@ -0,0 +1,147 @@
|
||||
import { describe, it, expect, beforeAll } from 'vitest'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { McpDbStore } from './mcp-db-store'
|
||||
import { McpOauthProvider } from './mcp-oauth-provider'
|
||||
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
// The OAuth wire format (RFC 6749 / OAuth 2.1) uses snake_case field names
|
||||
// (client_id, access_token, grant_type, ...); the test fixtures must use the
|
||||
// protocol's exact key names because they are passed to the MCP SDK's
|
||||
// OAuthClientProvider interfaces.
|
||||
|
||||
const SERVER_URL = 'https://mcp.example.com/kling'
|
||||
const ISSUER = 'https://kling.ai/auth'
|
||||
|
||||
describe('McpOauthProvider SEP-2352 issuer stamping', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
let store: McpDbStore
|
||||
let serverId: string
|
||||
|
||||
const makeProvider = () =>
|
||||
new McpOauthProvider(
|
||||
serverId,
|
||||
SERVER_URL,
|
||||
{ redirectUri: 'https://shumai.ai/api/mcp/oauth/callback' },
|
||||
{ onRedirect: async () => {} },
|
||||
store,
|
||||
)
|
||||
|
||||
beforeAll(async () => {
|
||||
store = new McpDbStore()
|
||||
const team = await prisma.team.create({ data: { name: 'OAuth Provider Team' } })
|
||||
const server = await prisma.mcpServer.create({
|
||||
data: { name: 'kling', url: SERVER_URL, teamId: team.id },
|
||||
})
|
||||
serverId = server.id
|
||||
})
|
||||
|
||||
it('discoveryState() returns the snapshot saved by the start leg on the callback leg', async () => {
|
||||
const provider = makeProvider()
|
||||
// Start leg: the SDK saves the discovery state before redirecting.
|
||||
await provider.saveDiscoveryState({
|
||||
authorizationServerUrl: ISSUER,
|
||||
authorizationServerMetadata: { issuer: ISSUER },
|
||||
resourceMetadataUrl: `${SERVER_URL}/.well-known/oauth-protected-resource`,
|
||||
} as never)
|
||||
await provider.deactivate()
|
||||
|
||||
// startAuth() persists the pending flow (must not clobber the snapshot).
|
||||
await store.savePendingAuth(serverId, {
|
||||
state: 'state-xyz',
|
||||
authorizationUrl: 'https://kling.ai/authorize',
|
||||
discovery: {},
|
||||
expiresAt: Math.floor(Date.now() / 1000) + 300,
|
||||
})
|
||||
|
||||
// Callback leg: a fresh provider instance reads the same snapshot.
|
||||
const callbackProvider = makeProvider()
|
||||
const state = await callbackProvider.discoveryState()
|
||||
expect(state?.authorizationServerMetadata?.issuer).toBe(ISSUER)
|
||||
expect(state?.authorizationServerUrl).toBe(ISSUER)
|
||||
})
|
||||
|
||||
it('saveClientInformation stamps the issuer and clientInformation round-trips it', async () => {
|
||||
const provider = makeProvider()
|
||||
await provider.saveDiscoveryState({
|
||||
authorizationServerUrl: ISSUER,
|
||||
authorizationServerMetadata: { issuer: ISSUER },
|
||||
} as never)
|
||||
|
||||
await provider.saveClientInformation({
|
||||
client_id: 'dyn-client',
|
||||
client_secret: 'dyn-secret',
|
||||
} as never)
|
||||
|
||||
const info = await makeProvider().clientInformation()
|
||||
expect(info?.client_id).toBe('dyn-client')
|
||||
expect((info as { issuer?: string }).issuer).toBe(ISSUER)
|
||||
})
|
||||
|
||||
it('clientInformation() backfills a missing issuer stamp on stale client info', async () => {
|
||||
const provider = makeProvider()
|
||||
// Pre-existing client info stored WITHOUT an issuer (registered before the
|
||||
// stamping fix). The discovery snapshot is available for this read.
|
||||
await store.updateClientInfo(
|
||||
serverId,
|
||||
{ clientId: 'legacy-client', clientSecret: 'legacy-secret' },
|
||||
SERVER_URL,
|
||||
)
|
||||
await provider.saveDiscoveryState({
|
||||
authorizationServerUrl: ISSUER,
|
||||
authorizationServerMetadata: { issuer: ISSUER },
|
||||
} as never)
|
||||
|
||||
const info = await makeProvider().clientInformation()
|
||||
expect(info?.client_id).toBe('legacy-client')
|
||||
expect((info as { issuer?: string }).issuer).toBe(ISSUER)
|
||||
// The stamp is persisted, not just returned.
|
||||
const entry = await store.getAuthForUrl(serverId, SERVER_URL)
|
||||
expect(entry?.clientInfo?.issuer).toBe(ISSUER)
|
||||
})
|
||||
|
||||
it('saveTokens stamps the issuer and tokens round-trips it', async () => {
|
||||
const provider = makeProvider()
|
||||
await provider.saveDiscoveryState({
|
||||
authorizationServerUrl: ISSUER,
|
||||
authorizationServerMetadata: { issuer: ISSUER },
|
||||
} as never)
|
||||
|
||||
await provider.saveTokens({
|
||||
access_token: 'tok-1',
|
||||
token_type: 'Bearer',
|
||||
refresh_token: 'ref-1',
|
||||
expires_in: 3600,
|
||||
} as never)
|
||||
|
||||
const tokens = await makeProvider().tokens()
|
||||
expect(tokens?.access_token).toBe('tok-1')
|
||||
expect((tokens as { issuer?: string }).issuer).toBe(ISSUER)
|
||||
})
|
||||
|
||||
it('tokens() backfills a missing issuer stamp from the discovery snapshot', async () => {
|
||||
const provider = makeProvider()
|
||||
// Pre-existing tokens stored WITHOUT an issuer (pre-upgrade / buggy flow).
|
||||
await store.updateTokens(serverId, { accessToken: 'legacy-tok' }, SERVER_URL)
|
||||
// The discovery snapshot is present for the read.
|
||||
await provider.saveDiscoveryState({
|
||||
authorizationServerUrl: ISSUER,
|
||||
authorizationServerMetadata: { issuer: ISSUER },
|
||||
} as never)
|
||||
|
||||
const tokens = await provider.tokens()
|
||||
expect(tokens?.access_token).toBe('legacy-tok')
|
||||
expect((tokens as { issuer?: string }).issuer).toBe(ISSUER)
|
||||
// The stamp is persisted, not just returned.
|
||||
const entry = await store.getAuthForUrl(serverId, SERVER_URL)
|
||||
expect(entry?.tokens?.issuer).toBe(ISSUER)
|
||||
})
|
||||
|
||||
it('does not stamp an issuer when no discovery snapshot is available', async () => {
|
||||
const provider = makeProvider()
|
||||
await provider.saveClientInformation({ client_id: 'c2' } as never)
|
||||
const info = await makeProvider().clientInformation()
|
||||
expect((info as { issuer?: string }).issuer).toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,503 @@
|
||||
/**
|
||||
* MCP OAuth Provider (DB-backed)
|
||||
*
|
||||
* Implementation of the MCP SDK's OAuthClientProvider interface. Adapted from
|
||||
* pi-mcp-adapter's mcp-oauth-provider.ts, with two differences:
|
||||
* 1. All persisted state (tokens, client info, PKCE verifier, discovery
|
||||
* state, CSRF state) is stored in the DB via McpDbStore instead of the OS
|
||||
* keyring/files, so the flow survives across HTTP requests and processes.
|
||||
* 2. The redirect URI is shumai's own Hono callback endpoint
|
||||
* (`${MCP_OAUTH_REDIRECT_BASE_URL}/api/mcp/oauth/callback`) instead of a
|
||||
* localhost callback server.
|
||||
*/
|
||||
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
// The OAuth wire format (RFC 6749 / OAuth 2.1 client metadata) uses snake_case
|
||||
// field names (client_id, access_token, grant_type, ...). These are dictated by
|
||||
// the OAuth protocol and the MCP SDK's OAuthClientProvider interfaces, so they
|
||||
// cannot be renamed to camelCase.
|
||||
|
||||
import {
|
||||
type AddClientAuthentication,
|
||||
type OAuthClientProvider,
|
||||
type OAuthClientInformationMixed,
|
||||
type OAuthClientMetadata,
|
||||
type OAuthDiscoveryState,
|
||||
type OAuthTokens,
|
||||
} from '@modelcontextprotocol/client'
|
||||
import type { McpDbStore, AuthEntry, StoredClientInfo, StoredTokens } from './mcp-db-store'
|
||||
|
||||
type IssuerBoundClientInformation = OAuthClientInformationMixed & { issuer?: string }
|
||||
type IssuerBoundTokens = OAuthTokens & { issuer?: string }
|
||||
|
||||
function issuersMatch(first: string, second: string): boolean {
|
||||
return (
|
||||
first === second ||
|
||||
(first.endsWith('/') && first.slice(0, -1) === second) ||
|
||||
(second.endsWith('/') && second.slice(0, -1) === first)
|
||||
)
|
||||
}
|
||||
|
||||
export function getOauthRedirectBaseUrl(): string {
|
||||
return (
|
||||
process.env.MCP_OAUTH_REDIRECT_BASE_URL?.trim() || process.env.BETTER_AUTH_URL?.trim() || ''
|
||||
)
|
||||
}
|
||||
|
||||
/** The default redirect URI used when the server config does not set one. */
|
||||
export function getDefaultOauthRedirectUri(): string | undefined {
|
||||
const base = getOauthRedirectBaseUrl()
|
||||
if (!base) return undefined
|
||||
return `${base.replace(/\/$/, '')}/api/mcp/oauth/callback`
|
||||
}
|
||||
|
||||
/** Configuration options for OAuth. */
|
||||
export interface McpOauthConfig {
|
||||
grantType?: 'authorization_code' | 'client_credentials'
|
||||
clientId?: string
|
||||
clientSecret?: string
|
||||
scope?: string
|
||||
authorizationParams?: Record<string, string>
|
||||
redirectUri?: string
|
||||
clientName?: string
|
||||
clientUri?: string
|
||||
skipIssuerMetadataValidation?: boolean
|
||||
}
|
||||
|
||||
const reservedAuthorizationParams = new Set([
|
||||
'client_id',
|
||||
'code_challenge',
|
||||
'code_challenge_method',
|
||||
'redirect_uri',
|
||||
'resource',
|
||||
'response_type',
|
||||
'scope',
|
||||
'state',
|
||||
])
|
||||
|
||||
function addAuthorizationParams(
|
||||
authorizationUrl: URL,
|
||||
params: Record<string, string> | undefined,
|
||||
): URL {
|
||||
if (!params) return authorizationUrl
|
||||
const nextUrl = new URL(authorizationUrl.toString())
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
if (reservedAuthorizationParams.has(key) || nextUrl.searchParams.has(key)) {
|
||||
throw new Error(
|
||||
`OAuth authorizationParams.${key} cannot override an authorization flow parameter`,
|
||||
)
|
||||
}
|
||||
nextUrl.searchParams.set(key, value)
|
||||
}
|
||||
return nextUrl
|
||||
}
|
||||
|
||||
/** Callbacks for OAuth flow interactions. */
|
||||
export interface McpOauthCallbacks {
|
||||
onRedirect: (url: URL) => void | Promise<void>
|
||||
}
|
||||
|
||||
/**
|
||||
* OAuth provider implementation for MCP servers backed by the DB store.
|
||||
*/
|
||||
export class McpOauthProvider implements OAuthClientProvider {
|
||||
private readonly redirectUrlSnapshot: string | undefined
|
||||
private active = true
|
||||
private flowIssuerMismatch = false
|
||||
|
||||
constructor(
|
||||
private serverId: string,
|
||||
private serverUrl: string,
|
||||
private config: McpOauthConfig,
|
||||
private callbacks: McpOauthCallbacks,
|
||||
private store: McpDbStore,
|
||||
) {
|
||||
this.redirectUrlSnapshot =
|
||||
config.grantType === 'client_credentials'
|
||||
? undefined
|
||||
: (config.redirectUri ?? getDefaultOauthRedirectUri())
|
||||
}
|
||||
|
||||
private get usesClientCredentials(): boolean {
|
||||
return this.config.grantType === 'client_credentials'
|
||||
}
|
||||
|
||||
deactivate(): void {
|
||||
this.active = false
|
||||
}
|
||||
|
||||
private throwIfInactive(): void {
|
||||
if (!this.active) throw new Error('OAuth flow is no longer active')
|
||||
}
|
||||
|
||||
private assertStoredIssuerBindings(
|
||||
entry: AuthEntry | undefined,
|
||||
issuer: string | undefined,
|
||||
): void {
|
||||
if (this.flowIssuerMismatch) {
|
||||
throw new Error(
|
||||
`OAuth authorization server issuer changed; clear credentials before authenticating again`,
|
||||
)
|
||||
}
|
||||
if (!entry || !issuer) return
|
||||
|
||||
const storedIssuers = [entry.clientInfo?.issuer, entry.tokens?.issuer].filter(
|
||||
(storedIssuer): storedIssuer is string => storedIssuer !== undefined,
|
||||
)
|
||||
if (storedIssuers.some((storedIssuer) => !issuersMatch(storedIssuer, issuer))) {
|
||||
this.flowIssuerMismatch = true
|
||||
throw new Error(
|
||||
`OAuth authorization server issuer changed; clear credentials before authenticating again`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** The redirect URL for OAuth callbacks. */
|
||||
get redirectUrl(): string | undefined {
|
||||
return this.redirectUrlSnapshot
|
||||
}
|
||||
|
||||
/**
|
||||
* SEP-2352 issuer of the authorization server from the discovery snapshot
|
||||
* persisted by saveDiscoveryState (available on both the start and callback
|
||||
* legs thanks to the DB-backed store).
|
||||
*/
|
||||
private async getDiscoveredIssuer(): Promise<string | undefined> {
|
||||
const snapshot = await this.store.getDiscoverySnapshot(this.serverId)
|
||||
if (!snapshot) return undefined
|
||||
const metadata = snapshot['authorizationServerMetadata'] as { issuer?: string } | undefined
|
||||
const issuer = metadata?.issuer
|
||||
if (typeof issuer === 'string' && issuer.length > 0) return issuer
|
||||
const serverUrl = snapshot['authorizationServerUrl']
|
||||
return typeof serverUrl === 'string' && serverUrl.length > 0 ? serverUrl : undefined
|
||||
}
|
||||
|
||||
/** Client metadata for dynamic registration. */
|
||||
get clientMetadata(): OAuthClientMetadata {
|
||||
if (this.usesClientCredentials) {
|
||||
return {
|
||||
client_name: this.config.clientName ?? 'Shumai',
|
||||
client_uri: this.config.clientUri ?? 'https://shumai.ai',
|
||||
redirect_uris: [],
|
||||
grant_types: ['client_credentials'],
|
||||
token_endpoint_auth_method: this.config.clientSecret ? 'client_secret_post' : 'none',
|
||||
}
|
||||
}
|
||||
|
||||
const redirectUrl = this.redirectUrl
|
||||
if (!redirectUrl) {
|
||||
throw new Error(
|
||||
'redirectUrl is required for authorization_code flow. Set MCP_OAUTH_REDIRECT_BASE_URL or BETTER_AUTH_URL, or configure authConfig.oauth.redirectUri.',
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
redirect_uris: [redirectUrl],
|
||||
client_name: this.config.clientName ?? 'Shumai',
|
||||
client_uri: this.config.clientUri ?? 'https://shumai.ai',
|
||||
grant_types: ['authorization_code', 'refresh_token'],
|
||||
response_types: ['code'],
|
||||
token_endpoint_auth_method: this.config.clientSecret ? 'client_secret_post' : 'none',
|
||||
...(this.config.scope !== undefined ? { scope: this.config.scope } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
/** Get client information (pre-registered or dynamically registered). */
|
||||
async clientInformation(): Promise<OAuthClientInformationMixed | undefined> {
|
||||
const stored = await this.store.getAuthForUrl(this.serverId, this.serverUrl)
|
||||
this.assertStoredIssuerBindings(stored, undefined)
|
||||
|
||||
// Config-first (pre-registered client). The secret stays in config.
|
||||
if (this.config.clientId) {
|
||||
const storedClient =
|
||||
stored?.clientInfo?.clientId === this.config.clientId ? stored.clientInfo : undefined
|
||||
const issuer = await this.getDiscoveredIssuer()
|
||||
if (storedClient?.issuer === undefined || storedClient.configPreRegistered !== true) {
|
||||
await this.store.updateClientInfo(
|
||||
this.serverId,
|
||||
{
|
||||
clientId: this.config.clientId,
|
||||
configPreRegistered: true,
|
||||
...(issuer !== undefined ? { issuer } : {}),
|
||||
},
|
||||
this.serverUrl,
|
||||
)
|
||||
}
|
||||
return {
|
||||
client_id: this.config.clientId,
|
||||
client_secret: this.config.clientSecret,
|
||||
...(issuer !== undefined ? { issuer } : {}),
|
||||
} as IssuerBoundClientInformation
|
||||
}
|
||||
|
||||
const clientInfo = stored?.clientInfo
|
||||
if (clientInfo) {
|
||||
const isConfigStub =
|
||||
clientInfo.configPreRegistered === true ||
|
||||
(clientInfo.clientSecret === undefined &&
|
||||
clientInfo.clientIdIssuedAt === undefined &&
|
||||
clientInfo.clientSecretExpiresAt === undefined &&
|
||||
clientInfo.redirectUris === undefined)
|
||||
if (isConfigStub) return undefined
|
||||
if (
|
||||
clientInfo.clientSecretExpiresAt &&
|
||||
clientInfo.clientSecretExpiresAt < Date.now() / 1000
|
||||
) {
|
||||
return undefined
|
||||
}
|
||||
// SEP-2352: backfill a missing issuer stamp (pre-upgrade / stale storage)
|
||||
// from the discovery snapshot, persisting it so later reads are isolated.
|
||||
const issuer = await this.getDiscoveredIssuer()
|
||||
if (issuer !== undefined && clientInfo.issuer === undefined) {
|
||||
const stamped: StoredClientInfo = { ...clientInfo, issuer }
|
||||
await this.store.updateClientInfo(this.serverId, stamped, this.serverUrl)
|
||||
clientInfo.issuer = issuer
|
||||
}
|
||||
return {
|
||||
client_id: clientInfo.clientId,
|
||||
client_secret: clientInfo.clientSecret,
|
||||
...(clientInfo.clientIdIssuedAt !== undefined
|
||||
? { client_id_issued_at: clientInfo.clientIdIssuedAt }
|
||||
: {}),
|
||||
...(clientInfo.clientSecretExpiresAt !== undefined
|
||||
? { client_secret_expires_at: clientInfo.clientSecretExpiresAt }
|
||||
: {}),
|
||||
...(clientInfo.redirectUris !== undefined
|
||||
? { redirect_uris: clientInfo.redirectUris }
|
||||
: {}),
|
||||
...(clientInfo.issuer !== undefined ? { issuer: clientInfo.issuer } : {}),
|
||||
} as IssuerBoundClientInformation
|
||||
}
|
||||
|
||||
// No client info - triggers dynamic registration.
|
||||
return undefined
|
||||
}
|
||||
|
||||
/** Save client information from dynamic registration. */
|
||||
async saveClientInformation(info: OAuthClientInformationMixed): Promise<void> {
|
||||
this.throwIfInactive()
|
||||
if (this.config.clientId && info.client_id === this.config.clientId) {
|
||||
await this.store.updateClientInfo(
|
||||
this.serverId,
|
||||
{ clientId: info.client_id, configPreRegistered: true },
|
||||
this.serverUrl,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
const redirectUris =
|
||||
('redirect_uris' in info ? info.redirect_uris : undefined) ??
|
||||
(this.redirectUrl ? [this.redirectUrl] : undefined)
|
||||
const issuer = await this.getDiscoveredIssuer()
|
||||
const clientInfo: StoredClientInfo = {
|
||||
clientId: info.client_id,
|
||||
...(info.client_secret !== undefined ? { clientSecret: info.client_secret } : {}),
|
||||
...(info.client_id_issued_at !== undefined
|
||||
? { clientIdIssuedAt: info.client_id_issued_at }
|
||||
: {}),
|
||||
...(info.client_secret_expires_at !== undefined
|
||||
? { clientSecretExpiresAt: info.client_secret_expires_at }
|
||||
: {}),
|
||||
...(redirectUris !== undefined ? { redirectUris } : {}),
|
||||
...(issuer !== undefined ? { issuer } : {}),
|
||||
}
|
||||
await this.store.updateClientInfo(this.serverId, clientInfo, this.serverUrl)
|
||||
}
|
||||
|
||||
/** Get stored OAuth tokens. */
|
||||
async tokens(): Promise<OAuthTokens | undefined> {
|
||||
const entry = await this.store.getAuthForUrl(this.serverId, this.serverUrl)
|
||||
if (!entry?.tokens) return undefined
|
||||
this.assertStoredIssuerBindings(entry, undefined)
|
||||
|
||||
// SEP-2352: backfill a missing issuer stamp from the discovery snapshot so
|
||||
// isolation stays active for pre-existing credentials.
|
||||
const issuer = await this.getDiscoveredIssuer()
|
||||
if (issuer !== undefined && entry.tokens.issuer === undefined) {
|
||||
const stamped: StoredTokens = { ...entry.tokens, issuer }
|
||||
await this.store.updateTokens(this.serverId, stamped, this.serverUrl)
|
||||
entry.tokens = stamped
|
||||
}
|
||||
|
||||
return {
|
||||
access_token: entry.tokens.accessToken,
|
||||
token_type: 'Bearer',
|
||||
refresh_token: entry.tokens.refreshToken,
|
||||
expires_in: entry.tokens.expiresAt
|
||||
? Math.max(0, Math.floor(entry.tokens.expiresAt - Date.now() / 1000))
|
||||
: undefined,
|
||||
scope: entry.tokens.scope,
|
||||
...(entry.tokens.issuer !== undefined ? { issuer: entry.tokens.issuer } : {}),
|
||||
} as IssuerBoundTokens
|
||||
}
|
||||
|
||||
/** Save OAuth tokens. */
|
||||
async saveTokens(tokens: OAuthTokens): Promise<void> {
|
||||
const issuer = await this.getDiscoveredIssuer()
|
||||
const storedTokens: StoredTokens = {
|
||||
accessToken: tokens.access_token,
|
||||
...(tokens.refresh_token !== undefined ? { refreshToken: tokens.refresh_token } : {}),
|
||||
...(tokens.expires_in !== undefined
|
||||
? { expiresAt: Date.now() / 1000 + tokens.expires_in }
|
||||
: {}),
|
||||
...(tokens.scope !== undefined ? { scope: tokens.scope } : {}),
|
||||
...(issuer !== undefined ? { issuer } : {}),
|
||||
}
|
||||
this.throwIfInactive()
|
||||
await this.store.updateTokens(this.serverId, storedTokens, this.serverUrl)
|
||||
}
|
||||
|
||||
/** Redirect the user to the authorization URL. */
|
||||
async redirectToAuthorization(authorizationUrl: URL): Promise<void> {
|
||||
if (this.usesClientCredentials) {
|
||||
throw new Error('redirectToAuthorization is not used for client_credentials flow')
|
||||
}
|
||||
this.throwIfInactive()
|
||||
await this.callbacks.onRedirect(
|
||||
addAuthorizationParams(authorizationUrl, this.config.authorizationParams),
|
||||
)
|
||||
}
|
||||
|
||||
/** Save the PKCE code verifier (persisted so the callback leg can use it). */
|
||||
async saveCodeVerifier(codeVerifier: string): Promise<void> {
|
||||
this.throwIfInactive()
|
||||
await this.store.updateCodeVerifier(this.serverId, codeVerifier, this.serverUrl)
|
||||
}
|
||||
|
||||
async codeVerifier(): Promise<string> {
|
||||
if (this.usesClientCredentials) {
|
||||
throw new Error('codeVerifier is not used for client_credentials flow')
|
||||
}
|
||||
this.throwIfInactive()
|
||||
const entry = await this.store.getAuthForUrl(this.serverId, this.serverUrl)
|
||||
if (!entry?.codeVerifier) {
|
||||
throw new Error(`No code verifier saved for MCP server`)
|
||||
}
|
||||
return entry.codeVerifier
|
||||
}
|
||||
|
||||
/** Keep discovery with the in-flight PKCE verifier. */
|
||||
async saveDiscoveryState(state: OAuthDiscoveryState): Promise<void> {
|
||||
this.throwIfInactive()
|
||||
// Persisted inside the pendingAuth row by mcpService after startAuth; also
|
||||
// kept on the credential row so the callback leg can re-read it.
|
||||
await this.store.saveDiscoverySnapshot(this.serverId, this.serverUrl, state)
|
||||
}
|
||||
|
||||
async discoveryState(): Promise<OAuthDiscoveryState | undefined> {
|
||||
this.throwIfInactive()
|
||||
const snapshot = await this.store.getDiscoverySnapshot(this.serverId)
|
||||
return snapshot as unknown as OAuthDiscoveryState | undefined
|
||||
}
|
||||
|
||||
/** Save the OAuth state parameter for CSRF protection. */
|
||||
async saveState(state: string): Promise<void> {
|
||||
this.throwIfInactive()
|
||||
await this.store.updateOauthState(this.serverId, state, this.serverUrl)
|
||||
}
|
||||
|
||||
async state(): Promise<string> {
|
||||
if (this.usesClientCredentials) {
|
||||
throw new Error('state is not used for client_credentials flow')
|
||||
}
|
||||
this.throwIfInactive()
|
||||
let state = await this.store.getOauthState(this.serverId)
|
||||
if (!state) {
|
||||
state = Array.from(crypto.getRandomValues(new Uint8Array(32)))
|
||||
.map((b) => b.toString(16).padStart(2, '0'))
|
||||
.join('')
|
||||
await this.store.updateOauthState(this.serverId, state, this.serverUrl)
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
/** Invalidate credentials when authentication fails. */
|
||||
async invalidateCredentials(
|
||||
type: 'all' | 'client' | 'tokens' | 'verifier' | 'discovery',
|
||||
): Promise<void> {
|
||||
this.throwIfInactive()
|
||||
switch (type) {
|
||||
case 'all':
|
||||
this.flowIssuerMismatch = false
|
||||
await this.store.clearAllCredentials(this.serverId)
|
||||
break
|
||||
case 'client':
|
||||
await this.store.clearClientInfo(this.serverId)
|
||||
break
|
||||
case 'tokens':
|
||||
await this.store.clearTokens(this.serverId)
|
||||
break
|
||||
case 'verifier':
|
||||
await this.store.clearCodeVerifier(this.serverId)
|
||||
break
|
||||
case 'discovery':
|
||||
await this.store.clearDiscoverySnapshot(this.serverId)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
/** Adds configured authorization-code scope and client authentication. */
|
||||
addClientAuthentication: AddClientAuthentication = async (headers, params, _url, metadata) => {
|
||||
this.throwIfInactive()
|
||||
if (
|
||||
params.get('grant_type') === 'authorization_code' &&
|
||||
!params.has('scope') &&
|
||||
this.config.scope
|
||||
) {
|
||||
params.set('scope', this.config.scope)
|
||||
}
|
||||
|
||||
const clientInfo = await this.clientInformation()
|
||||
this.throwIfInactive()
|
||||
if (!clientInfo) return
|
||||
|
||||
const supportedMethods = metadata?.token_endpoint_auth_methods_supported ?? []
|
||||
const hasClientSecret = clientInfo.client_secret !== undefined
|
||||
let authMethod: 'client_secret_basic' | 'client_secret_post' | 'none'
|
||||
|
||||
if (supportedMethods.length === 0) {
|
||||
authMethod = hasClientSecret ? 'client_secret_post' : 'none'
|
||||
} else if (hasClientSecret && supportedMethods.includes('client_secret_basic')) {
|
||||
authMethod = 'client_secret_basic'
|
||||
} else if (hasClientSecret && supportedMethods.includes('client_secret_post')) {
|
||||
authMethod = 'client_secret_post'
|
||||
} else if (supportedMethods.includes('none')) {
|
||||
authMethod = 'none'
|
||||
} else {
|
||||
authMethod = hasClientSecret ? 'client_secret_post' : 'none'
|
||||
}
|
||||
|
||||
if (authMethod === 'client_secret_basic') {
|
||||
if (!clientInfo.client_secret) {
|
||||
throw new Error('client_secret_basic authentication requires a client_secret')
|
||||
}
|
||||
headers.set(
|
||||
'Authorization',
|
||||
`Basic ${Buffer.from(`${clientInfo.client_id}:${clientInfo.client_secret}`).toString('base64')}`,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if (!params.has('client_id')) {
|
||||
params.set('client_id', clientInfo.client_id)
|
||||
}
|
||||
if (
|
||||
authMethod === 'client_secret_post' &&
|
||||
clientInfo.client_secret &&
|
||||
!params.has('client_secret')
|
||||
) {
|
||||
params.set('client_secret', clientInfo.client_secret)
|
||||
}
|
||||
}
|
||||
|
||||
prepareTokenRequest(scope?: string): URLSearchParams | undefined {
|
||||
if (!this.usesClientCredentials) return undefined
|
||||
|
||||
const params = new URLSearchParams({ grant_type: 'client_credentials' })
|
||||
const requestedScope = scope ?? this.config.scope
|
||||
if (requestedScope) {
|
||||
params.set('scope', requestedScope)
|
||||
}
|
||||
return params
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
/**
|
||||
* MCP proxy tool — the single `mcp` AgentTool registered per agent.
|
||||
*
|
||||
* One tool in the LLM context multiplexes all of the agent's assigned MCP
|
||||
* servers via `{ server, tool, args, search, describe, connect, action }`.
|
||||
* Mode precedence: action > tool(call) > connect > describe > search >
|
||||
* server(list) > status.
|
||||
*/
|
||||
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { AgentTool, AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import type { McpToolRegistry, ToolMetadata } from './mcp-tool-registry'
|
||||
import { formatSchema } from './mcp-tool-schema'
|
||||
|
||||
type ProxyToolResult = AgentToolResult<Record<string, unknown>>
|
||||
|
||||
/** Truncated instructions head baked into the proxy tool description (L1). */
|
||||
const INSTRUCTIONS_SNIPPET_LENGTH = 150
|
||||
/** Longer instructions preview appended to server listings (L2). */
|
||||
const INSTRUCTIONS_PREVIEW_LENGTH = 300
|
||||
|
||||
/** Truncate at a word boundary (mirrors pi-mcp-adapter's truncateAtWord). */
|
||||
function truncateInstruction(text: string, target: number): string {
|
||||
if (!text || text.length <= target) return text
|
||||
const truncated = text.slice(0, target)
|
||||
const lastSpace = truncated.lastIndexOf(' ')
|
||||
if (lastSpace > target * 0.6) {
|
||||
return `${truncated.slice(0, lastSpace)}...`
|
||||
}
|
||||
return `${truncated}...`
|
||||
}
|
||||
|
||||
export interface ProxyServerInfo {
|
||||
id: string
|
||||
name: string
|
||||
toolCount: number
|
||||
directToolCount?: number
|
||||
status: string
|
||||
/** Self-reported server usage instructions (empty when the server provides none). */
|
||||
instructions?: string
|
||||
}
|
||||
|
||||
export type ConnectionOutcome =
|
||||
| { status: 'connected' }
|
||||
| { status: 'needs-auth' }
|
||||
| { status: 'error'; message: string }
|
||||
|
||||
export interface CallToolOutcome {
|
||||
ok: boolean
|
||||
content: AgentToolResult<Record<string, unknown>>['content']
|
||||
}
|
||||
|
||||
export interface McpProxyToolContext {
|
||||
teamId: string
|
||||
userId?: string
|
||||
servers: ProxyServerInfo[]
|
||||
serverNameById: Map<string, string>
|
||||
serverIdByName: Map<string, string>
|
||||
registry: McpToolRegistry
|
||||
/** Resolve the server id for a (possibly prefixed) tool name. */
|
||||
resolveServerForTool?(toolName: string): string | undefined
|
||||
ensureConnected(serverId: string): Promise<ConnectionOutcome>
|
||||
callTool(
|
||||
serverId: string,
|
||||
toolName: string,
|
||||
args: Record<string, unknown>,
|
||||
): Promise<CallToolOutcome>
|
||||
authStatus(serverId: string): Promise<string>
|
||||
startAuth(
|
||||
serverId: string,
|
||||
): Promise<{ authorizationUrl?: string; alreadyAuthenticated?: boolean }>
|
||||
/** Server-reported usage instructions (DB cache, works without a live connection). */
|
||||
getInstructions(serverId: string): Promise<string | null>
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a tool, lazily connecting the owning server first when the registry
|
||||
* has no cached metadata for it yet (first call in a fresh process).
|
||||
*/
|
||||
async function resolveToolWithLazyConnect(
|
||||
ctx: McpProxyToolContext,
|
||||
toolName: string,
|
||||
serverId?: string,
|
||||
): Promise<{ ok: true; serverId: string; tool: ToolMetadata } | { ok: false }> {
|
||||
const firstResolve = ctx.registry.findTool(toolName, serverId)
|
||||
if (firstResolve) return { ok: true, ...firstResolve }
|
||||
|
||||
const targetServerId = serverId ?? ctx.resolveServerForTool?.(toolName)
|
||||
if (!targetServerId) return { ok: false }
|
||||
|
||||
const outcome = await ctx.ensureConnected(targetServerId)
|
||||
if (outcome.status !== 'connected') return { ok: false }
|
||||
|
||||
const retried = ctx.registry.findTool(toolName, targetServerId)
|
||||
if (!retried) return { ok: false }
|
||||
return { ok: true, ...retried }
|
||||
}
|
||||
|
||||
const proxyToolSchema = Type.Object(
|
||||
{
|
||||
server: Type.Optional(
|
||||
Type.String({
|
||||
description:
|
||||
'The MCP server name. Required to call a tool, list a server, or start auth. Omit to search across all servers.',
|
||||
}),
|
||||
),
|
||||
tool: Type.Optional(
|
||||
Type.String({
|
||||
description:
|
||||
'The name of the tool to call on the server (prefixed "{server}_{tool}" or the original tool name).',
|
||||
}),
|
||||
),
|
||||
args: Type.Optional(
|
||||
Type.Unknown({
|
||||
description: 'Arguments for the tool call: either a JSON object or a JSON-encoded string.',
|
||||
}),
|
||||
),
|
||||
describe: Type.Optional(
|
||||
Type.String({
|
||||
description: 'Tool name to show its full parameter schema.',
|
||||
}),
|
||||
),
|
||||
instructions: Type.Optional(
|
||||
Type.String({
|
||||
description: 'Server name to show its full usage instructions.',
|
||||
}),
|
||||
),
|
||||
search: Type.Optional(
|
||||
Type.String({
|
||||
description: 'Search query to find tools across servers (camelCase/snake_case aware).',
|
||||
}),
|
||||
),
|
||||
includeSchemas: Type.Optional(
|
||||
Type.Boolean({
|
||||
description: 'Include parameter schemas in search results (default true).',
|
||||
}),
|
||||
),
|
||||
limit: Type.Optional(Type.Number({ description: 'Max results for search/list (default 12).' })),
|
||||
offset: Type.Optional(Type.Number({ description: 'Pagination offset for search/list.' })),
|
||||
action: Type.Optional(
|
||||
Type.Union([Type.Literal('status'), Type.Literal('auth-start')], {
|
||||
description:
|
||||
'"status" shows connection/auth state for all servers; "auth-start" begins OAuth for `server` (the user completes it in a browser, then you poll status).',
|
||||
}),
|
||||
),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
)
|
||||
|
||||
function parseArgs(raw: unknown): Record<string, unknown> {
|
||||
if (raw === undefined || raw === null) return {}
|
||||
if (typeof raw === 'string') {
|
||||
const trimmed = raw.trim()
|
||||
if (!trimmed) return {}
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed)
|
||||
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
||||
return parsed as Record<string, unknown>
|
||||
}
|
||||
return { value: parsed }
|
||||
} catch {
|
||||
return { value: raw }
|
||||
}
|
||||
}
|
||||
if (typeof raw === 'object' && !Array.isArray(raw)) {
|
||||
return raw as Record<string, unknown>
|
||||
}
|
||||
return { value: raw }
|
||||
}
|
||||
|
||||
function contentText(text: string): ProxyToolResult {
|
||||
return { content: [{ type: 'text', text }], details: {} }
|
||||
}
|
||||
|
||||
/** Transform MCP content blocks into model content blocks. */
|
||||
export function transformMcpContent(
|
||||
content: Array<{
|
||||
type: string
|
||||
text?: string
|
||||
data?: string
|
||||
mimeType?: string
|
||||
resource?: unknown
|
||||
uri?: string
|
||||
name?: string
|
||||
}>,
|
||||
): AgentToolResult<Record<string, unknown>>['content'] {
|
||||
return content.map((c) => {
|
||||
if (c.type === 'text') {
|
||||
return { type: 'text', text: c.text ?? '' }
|
||||
}
|
||||
if (c.type === 'image') {
|
||||
return { type: 'image', data: c.data ?? '', mimeType: c.mimeType ?? 'image/png' }
|
||||
}
|
||||
if (c.type === 'resource') {
|
||||
const res = (c.resource ?? {}) as { uri?: string; text?: string }
|
||||
const resourceUri = res.uri ?? '(no URI)'
|
||||
const resourceContent = res.text ?? (c.resource ? JSON.stringify(c.resource) : '(no content)')
|
||||
return { type: 'text', text: `[Resource: ${resourceUri}]\n${resourceContent}` }
|
||||
}
|
||||
if (c.type === 'resource_link') {
|
||||
const linkName = c.name ?? c.uri ?? 'unknown'
|
||||
const linkUri = c.uri ?? '(no URI)'
|
||||
return { type: 'text', text: `[Resource Link: ${linkName}]\nURI: ${linkUri}` }
|
||||
}
|
||||
if (c.type === 'audio') {
|
||||
return { type: 'text', text: `[Audio content: ${c.mimeType ?? 'audio/*'}]` }
|
||||
}
|
||||
return { type: 'text', text: JSON.stringify(c) }
|
||||
})
|
||||
}
|
||||
|
||||
function renderToolShape(tool: ToolMetadata, indent = ''): string {
|
||||
if (!tool.inputSchema) return 'No parameters defined.'
|
||||
const schemaText = formatSchema(tool.inputSchema, indent)
|
||||
return `Parameters:\n${schemaText}`
|
||||
}
|
||||
|
||||
function notFoundResult(
|
||||
toolName: string,
|
||||
suggestions: string[],
|
||||
serverName?: string,
|
||||
): ProxyToolResult {
|
||||
let msg = `Tool "${toolName}" not found.`
|
||||
if (serverName) {
|
||||
msg += ` Use mcp({ server: "${serverName}" }) to list its tools or mcp({ search: "..." }) to search.`
|
||||
} else {
|
||||
msg += ` Use mcp({ search: "..." }) to search.`
|
||||
}
|
||||
if (suggestions.length > 0) {
|
||||
msg += ` Did you mean: ${suggestions.join(', ')}`
|
||||
}
|
||||
return contentText(msg)
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the dynamic proxy tool description (server list + tool counts + usage).
|
||||
* Servers with instructions surface a truncated head so the model sees the
|
||||
* guidance without making a call; full text is available on demand.
|
||||
*/
|
||||
export function buildProxyDescription(servers: ProxyServerInfo[]): string {
|
||||
const lines: string[] = []
|
||||
lines.push('Call MCP (Model Context Protocol) tools exposed by the configured servers.')
|
||||
|
||||
const directSummaries: string[] = []
|
||||
for (const s of servers) {
|
||||
const directCount = s.directToolCount ?? 0
|
||||
if (directCount > 0) {
|
||||
directSummaries.push(`${s.name} (${directCount})`)
|
||||
}
|
||||
}
|
||||
|
||||
if (directSummaries.length > 0) {
|
||||
lines.push('')
|
||||
lines.push(`Direct tools available (call as normal tools): ${directSummaries.join(', ')}`)
|
||||
}
|
||||
|
||||
lines.push('')
|
||||
lines.push('Servers:')
|
||||
if (servers.length === 0) {
|
||||
lines.push(' (none assigned)')
|
||||
} else {
|
||||
let listedServerCount = 0
|
||||
for (const s of servers) {
|
||||
const directCount = s.directToolCount ?? 0
|
||||
const proxyCount = Math.max(0, s.toolCount - directCount)
|
||||
if (proxyCount > 0 || directCount === 0) {
|
||||
lines.push(` - ${s.name} (${proxyCount} tools, status: ${s.status})`)
|
||||
listedServerCount++
|
||||
}
|
||||
}
|
||||
if (listedServerCount === 0) {
|
||||
lines.push(' (all assigned tools are configured as direct tools)')
|
||||
}
|
||||
}
|
||||
lines.push('')
|
||||
lines.push('Usage:')
|
||||
lines.push(' - mcp({}) → connection/auth status of all servers')
|
||||
lines.push(' - mcp({ server: "name" }) → list that server\'s tools')
|
||||
lines.push(' - mcp({ search: "query" }) → search tools across servers')
|
||||
lines.push(' - mcp({ describe: "tool" }) → show one tool\'s parameter schema')
|
||||
lines.push(' - mcp({ server: "name", tool: "tool_name", args: {...} }) → call a tool')
|
||||
lines.push(' - mcp({ instructions: "name" }) → show full server usage instructions')
|
||||
lines.push(' - mcp({ action: "auth-start", server: "name" }) → start OAuth for a server')
|
||||
lines.push('')
|
||||
const instructionLines: string[] = []
|
||||
for (const s of servers) {
|
||||
if (!s.instructions || s.instructions.trim() === '') continue
|
||||
const snippet = truncateInstruction(
|
||||
s.instructions.replace(/\s+/g, ' ').trim(),
|
||||
INSTRUCTIONS_SNIPPET_LENGTH,
|
||||
)
|
||||
instructionLines.push(` - ${s.name}: ${snippet}`)
|
||||
}
|
||||
if (instructionLines.length > 0) {
|
||||
lines.push('Server instructions (truncated — full text via mcp({ instructions: "name" })):')
|
||||
lines.push(...instructionLines)
|
||||
lines.push('')
|
||||
}
|
||||
lines.push(
|
||||
'Tool names are prefixed "{server}_{tool}" (dots replaced by underscores). You may pass either the prefixed or the original name. ' +
|
||||
'If a call fails with "needs auth", tell the user to connect the server in Settings → MCP.',
|
||||
)
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a server's tool listing with the L2 instructions preview appended
|
||||
* (mirrors pi-mcp-adapter's mcp({ server }) output). Shared by the `server`
|
||||
* list mode and the `connect` mode.
|
||||
*/
|
||||
function renderServerListing(
|
||||
serverName: string,
|
||||
tools: ToolMetadata[],
|
||||
instructions: string | null,
|
||||
): string {
|
||||
const lines = [`${serverName} (${tools.length} tools):`, '']
|
||||
for (const t of tools) {
|
||||
lines.push(`- ${t.name}${t.description ? ` - ${t.description.slice(0, 60)}` : ''}`)
|
||||
}
|
||||
if (instructions && instructions.trim() !== '') {
|
||||
const preview = truncateInstruction(instructions, INSTRUCTIONS_PREVIEW_LENGTH)
|
||||
lines.push('', 'Server instructions:', preview)
|
||||
if (preview !== instructions) {
|
||||
lines.push(`Use mcp({ instructions: "${serverName}" }) for the full text.`)
|
||||
}
|
||||
}
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the single `mcp` proxy AgentTool for a team/session.
|
||||
*/
|
||||
export function buildProxyTool(ctx: McpProxyToolContext): AgentTool<typeof proxyToolSchema> {
|
||||
return {
|
||||
name: 'mcp',
|
||||
label: 'MCP Tools',
|
||||
description: buildProxyDescription(ctx.servers),
|
||||
parameters: proxyToolSchema,
|
||||
execute: async (_toolCallId, params): Promise<ProxyToolResult> => {
|
||||
const action = params.action
|
||||
const server = params.server
|
||||
const tool = params.tool
|
||||
const search = params.search
|
||||
const describe = params.describe
|
||||
const instructions = params.instructions
|
||||
const limit = params.limit ?? 12
|
||||
const offset = params.offset ?? 0
|
||||
const includeSchemas = params.includeSchemas !== false
|
||||
|
||||
// 1. action: status / auth-start
|
||||
if (action === 'status') {
|
||||
const lines: string[] = ['MCP status:']
|
||||
for (const s of ctx.servers) {
|
||||
const auth = await ctx.authStatus(s.id)
|
||||
lines.push(` - ${s.name}: ${s.status} (${s.toolCount} tools, auth: ${auth})`)
|
||||
}
|
||||
lines.push('')
|
||||
lines.push('mcp({ server: "name" }) to list tools, mcp({ search: "..." }) to search')
|
||||
return contentText(lines.join('\n'))
|
||||
}
|
||||
if (action === 'auth-start') {
|
||||
const serverId = server ? ctx.serverIdByName.get(server) : undefined
|
||||
if (!serverId) {
|
||||
return contentText(
|
||||
`Server "${server ?? ''}" not found. Use mcp({ action: "status" }) to see available servers.`,
|
||||
)
|
||||
}
|
||||
const result = await ctx.startAuth(serverId)
|
||||
if (result.alreadyAuthenticated) {
|
||||
return contentText(`OAuth authentication already complete for "${server}".`)
|
||||
}
|
||||
if (!result.authorizationUrl) {
|
||||
return contentText(
|
||||
`OAuth authentication could not be started for "${server}". Ask an admin to check Settings → MCP.`,
|
||||
)
|
||||
}
|
||||
return contentText(
|
||||
`OAuth authentication required for "${server}". An admin must open this URL in a browser and approve access:\n\n${result.authorizationUrl}\n\nAfter approving, the browser redirects back to shumai automatically. Then poll mcp({ action: "status", server: "${server}" }) until it shows connected.`,
|
||||
)
|
||||
}
|
||||
|
||||
// 2. tool call
|
||||
if (tool) {
|
||||
const serverId = server ? ctx.serverIdByName.get(server) : undefined
|
||||
if (server && !serverId) {
|
||||
return contentText(
|
||||
`Server "${server}" not found. Use mcp({ action: "status" }) to see available servers.`,
|
||||
)
|
||||
}
|
||||
const resolved = await resolveToolWithLazyConnect(ctx, tool, serverId)
|
||||
if (!resolved.ok) {
|
||||
const suggestions = ctx.registry.rankSuggestions(tool, 5, ctx.serverNameById).slice(0, 5)
|
||||
return notFoundResult(tool, suggestions, server)
|
||||
}
|
||||
const outcome = await ctx.callTool(
|
||||
resolved.serverId,
|
||||
resolved.tool.originalName,
|
||||
parseArgs(params.args),
|
||||
)
|
||||
if (outcome.ok) {
|
||||
return { content: outcome.content, details: {} }
|
||||
}
|
||||
return {
|
||||
content: outcome.content,
|
||||
details: { error: 'tool_call_failed', tool, server: resolved.serverId },
|
||||
}
|
||||
}
|
||||
|
||||
// 4. describe
|
||||
if (describe) {
|
||||
const resolved = ctx.registry.findTool(
|
||||
describe,
|
||||
server ? ctx.serverIdByName.get(server) : undefined,
|
||||
)
|
||||
if (!resolved) {
|
||||
const suggestions = ctx.registry
|
||||
.rankSuggestions(describe, 5, ctx.serverNameById)
|
||||
.slice(0, 5)
|
||||
return notFoundResult(describe, suggestions, server)
|
||||
}
|
||||
const serverName = ctx.serverNameById.get(resolved.serverId) ?? resolved.serverId
|
||||
let text = `${resolved.tool.name}\nServer: ${serverName}\n\n${resolved.tool.description || '(no description)'}\n\n`
|
||||
text += renderToolShape(resolved.tool)
|
||||
return contentText(text.trim())
|
||||
}
|
||||
|
||||
// 4.5 instructions
|
||||
if (instructions) {
|
||||
const serverId = ctx.serverIdByName.get(instructions)
|
||||
if (!serverId) {
|
||||
return contentText(
|
||||
`Server "${instructions}" not found. Use mcp({ action: "status" }) to see available servers.`,
|
||||
)
|
||||
}
|
||||
const text = await ctx.getInstructions(serverId)
|
||||
if (!text || text.trim() === '') {
|
||||
return contentText(`Server "${instructions}" does not provide instructions.`)
|
||||
}
|
||||
return contentText(`${instructions} instructions:\n\n${text}`)
|
||||
}
|
||||
|
||||
// 5. search
|
||||
if (search) {
|
||||
if (search.trim().length === 0 && !server) {
|
||||
return contentText('Search query cannot be empty')
|
||||
}
|
||||
const serverId = server ? ctx.serverIdByName.get(server) : undefined
|
||||
if (server && !serverId) {
|
||||
return contentText(
|
||||
`Server "${server}" not found. Use mcp({ action: "status" }) to see available servers.`,
|
||||
)
|
||||
}
|
||||
const { items, total, hasMore, nextOffset } = ctx.registry.searchTools(
|
||||
search,
|
||||
serverId,
|
||||
ctx.serverNameById,
|
||||
limit,
|
||||
offset,
|
||||
)
|
||||
if (total === 0) {
|
||||
return contentText(
|
||||
`No tools matching "${search}"${server ? ` in "${server}"` : ''}. Use mcp({ action: "status" }) to check connection state.`,
|
||||
)
|
||||
}
|
||||
let text = `Found ${total} tool${total === 1 ? '' : 's'} matching "${search}":\n\n`
|
||||
for (const match of items) {
|
||||
const serverName = ctx.serverNameById.get(match.server) ?? match.server
|
||||
text += `${match.tool.name} (server: ${serverName})\n`
|
||||
text += ` ${match.tool.description || '(no description)'}\n`
|
||||
if (includeSchemas && match.tool.inputSchema) {
|
||||
text += ` ${renderToolShape(match.tool, ' ')}\n`
|
||||
}
|
||||
text += '\n'
|
||||
}
|
||||
if (hasMore) {
|
||||
text += `${items.length} of ${total} — offset: ${nextOffset} for more\n`
|
||||
}
|
||||
return contentText(text.trim())
|
||||
}
|
||||
|
||||
// 6. server list
|
||||
if (server) {
|
||||
const serverId = ctx.serverIdByName.get(server)
|
||||
if (!serverId) {
|
||||
return contentText(
|
||||
`Server "${server}" not found. Use mcp({ action: "status" }) to see available servers.`,
|
||||
)
|
||||
}
|
||||
const tools = ctx.registry.getTools(serverId)
|
||||
if (tools.length === 0) {
|
||||
return contentText(
|
||||
`Server "${server}" has no cached tools yet. Call a tool on this server to connect automatically, or refresh it in Settings → MCP.`,
|
||||
)
|
||||
}
|
||||
const instructionsText = await ctx.getInstructions(serverId)
|
||||
return contentText(renderServerListing(server, tools, instructionsText))
|
||||
}
|
||||
|
||||
// 7. default: status
|
||||
const lines: string[] = ['MCP status:']
|
||||
for (const s of ctx.servers) {
|
||||
const auth = await ctx.authStatus(s.id)
|
||||
lines.push(` - ${s.name}: ${s.status} (${s.toolCount} tools, auth: ${auth})`)
|
||||
}
|
||||
lines.push('')
|
||||
lines.push('mcp({ server: "name" }) to list tools, mcp({ search: "..." }) to search')
|
||||
return contentText(lines.join('\n'))
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,443 @@
|
||||
/**
|
||||
* MCP Server Manager
|
||||
*
|
||||
* Manages MCP client connections to URL-based servers (Streamable HTTP with
|
||||
* SSE fallback, or pinned SSE). Simplified port of pi-mcp-adapter's
|
||||
* server-manager.ts: tools only, no stdio/sampling/elicitation/tracing.
|
||||
*/
|
||||
|
||||
import {
|
||||
Client,
|
||||
SdkHttpError,
|
||||
SSEClientTransport,
|
||||
StreamableHTTPClientTransport,
|
||||
UnauthorizedError,
|
||||
type Implementation,
|
||||
type ListToolsResult,
|
||||
type RequestOptions,
|
||||
type VersionNegotiationOptions,
|
||||
} from '@modelcontextprotocol/client'
|
||||
import '@shumai/db/src/prisma-json-types'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import type { McpDbStore } from './mcp-db-store'
|
||||
import { McpOauthProvider, type McpOauthConfig } from './mcp-oauth-provider'
|
||||
|
||||
export interface McpTool {
|
||||
name: string
|
||||
title?: string
|
||||
description?: string
|
||||
inputSchema?: unknown
|
||||
_meta?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export interface ServerConnection {
|
||||
client: Client
|
||||
definition: McpServerDefinition
|
||||
tools: McpTool[]
|
||||
status: 'connected' | 'needs-auth'
|
||||
/** Self-reported server name/title/version/description (from getServerVersion). */
|
||||
serverVersion?: Implementation
|
||||
/** Self-reported server usage instructions (from getInstructions). */
|
||||
instructions?: string
|
||||
lastUsedAt: number
|
||||
inFlight: number
|
||||
}
|
||||
|
||||
export interface McpServerDefinition {
|
||||
name: string
|
||||
url: string
|
||||
transport: 'streamable_http' | 'sse'
|
||||
authConfig?: PrismaJson.McpServerAuthConfig
|
||||
config?: PrismaJson.McpServerConfig
|
||||
}
|
||||
|
||||
function isUnauthorizedHttpError(error: unknown): boolean {
|
||||
return (
|
||||
error instanceof UnauthorizedError || (error instanceof SdkHttpError && error.status === 401)
|
||||
)
|
||||
}
|
||||
|
||||
function shouldFallbackToSse(error: unknown, definition: McpServerDefinition): boolean {
|
||||
if (definition.config?.protocolVersion === '2026-07-28') return false
|
||||
return error instanceof SdkHttpError && [404, 405, 406, 415].includes(error.status)
|
||||
}
|
||||
|
||||
function resolveVersionNegotiation(
|
||||
definition: McpServerDefinition,
|
||||
): VersionNegotiationOptions | undefined {
|
||||
switch (definition.config?.protocolVersion) {
|
||||
case undefined:
|
||||
case 'legacy':
|
||||
return undefined
|
||||
case 'auto':
|
||||
return { mode: 'auto' }
|
||||
case '2026-07-28':
|
||||
return { mode: { pin: '2026-07-28' } }
|
||||
default:
|
||||
throw new Error(`Invalid MCP protocolVersion: ${String(definition.config?.protocolVersion)}`)
|
||||
}
|
||||
}
|
||||
|
||||
export class McpServerManager {
|
||||
private connections = new Map<string, ServerConnection>()
|
||||
private connectPromises = new Map<string, Promise<ServerConnection>>()
|
||||
private stopped = false
|
||||
|
||||
constructor(private readonly dbStore: McpDbStore) {}
|
||||
|
||||
private getRequestTimeoutMs(definition: McpServerDefinition): number | undefined {
|
||||
const timeout = definition.config?.requestTimeoutMs
|
||||
return typeof timeout === 'number' && Number.isFinite(timeout) && timeout > 0
|
||||
? timeout
|
||||
: undefined
|
||||
}
|
||||
|
||||
private buildRequestOptions(definition: McpServerDefinition): RequestOptions | undefined {
|
||||
const timeout = this.getRequestTimeoutMs(definition)
|
||||
return timeout !== undefined ? { timeout } : undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to a server. Single-flight per server id. Returns a connection
|
||||
* with status 'connected' or 'needs-auth' (when the server returns 401 and
|
||||
* OAuth is configured / auto-detectable).
|
||||
*/
|
||||
async connect(serverId: string, definition: McpServerDefinition): Promise<ServerConnection> {
|
||||
if (this.stopped) throw new Error('MCP server manager is closed')
|
||||
|
||||
// Dedupe concurrent connection attempts.
|
||||
if (this.connectPromises.has(serverId)) {
|
||||
return this.connectPromises.get(serverId)!
|
||||
}
|
||||
|
||||
const existing = this.connections.get(serverId)
|
||||
if (existing?.status === 'connected') {
|
||||
existing.lastUsedAt = Date.now()
|
||||
return existing
|
||||
}
|
||||
|
||||
const promise = this.createConnection(serverId, definition).finally(() => {
|
||||
if (this.connectPromises.get(serverId) === promise) {
|
||||
this.connectPromises.delete(serverId)
|
||||
}
|
||||
})
|
||||
this.connectPromises.set(serverId, promise)
|
||||
|
||||
try {
|
||||
const connection = await promise
|
||||
this.connections.set(serverId, connection)
|
||||
return connection
|
||||
} catch (error) {
|
||||
this.connectPromises.delete(serverId)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
private async createConnection(
|
||||
serverId: string,
|
||||
definition: McpServerDefinition,
|
||||
): Promise<ServerConnection> {
|
||||
const url = new URL(definition.url)
|
||||
const requestInit = this.buildRequestInit(definition)
|
||||
const authProvider = this.buildAuthProvider(serverId, definition)
|
||||
|
||||
const attempt = async (
|
||||
kind: 'streamable-http' | 'sse',
|
||||
): Promise<{
|
||||
status: 'connected' | 'needs-auth'
|
||||
client: Client
|
||||
error?: unknown
|
||||
}> => {
|
||||
const transportOptions = {
|
||||
...(requestInit !== undefined ? { requestInit } : {}),
|
||||
...(authProvider !== undefined ? { authProvider } : {}),
|
||||
...(authProvider !== undefined &&
|
||||
definition.authConfig?.oauth?.skipIssuerMetadataValidation === true
|
||||
? { skipIssuerMetadataValidation: true }
|
||||
: {}),
|
||||
}
|
||||
const transport =
|
||||
kind === 'streamable-http'
|
||||
? new StreamableHTTPClientTransport(url, transportOptions)
|
||||
: new SSEClientTransport(url, transportOptions)
|
||||
const client = this.createClient(serverId, definition)
|
||||
|
||||
try {
|
||||
await client.connect(transport, this.buildRequestOptions(definition))
|
||||
return { status: 'connected', client }
|
||||
} catch (error) {
|
||||
try {
|
||||
await client.close()
|
||||
} catch {
|
||||
// Ignore cleanup errors on a failed connect.
|
||||
}
|
||||
return { status: 'needs-auth', client, error }
|
||||
}
|
||||
}
|
||||
|
||||
let kind: 'streamable-http' | 'sse' = definition.transport === 'sse' ? 'sse' : 'streamable-http'
|
||||
for (;;) {
|
||||
const result = await attempt(kind)
|
||||
if (result.status === 'connected') {
|
||||
const tools = await this.fetchAllTools(result.client, this.buildRequestOptions(definition))
|
||||
return {
|
||||
client: result.client,
|
||||
definition,
|
||||
tools,
|
||||
status: 'connected',
|
||||
serverVersion: result.client.getServerVersion(),
|
||||
instructions: result.client.getInstructions(),
|
||||
lastUsedAt: Date.now(),
|
||||
inFlight: 0,
|
||||
}
|
||||
}
|
||||
|
||||
if (isUnauthorizedHttpError(result.error)) {
|
||||
if (this.supportsOauth(definition)) {
|
||||
return {
|
||||
client: result.client,
|
||||
definition,
|
||||
tools: [],
|
||||
status: 'needs-auth',
|
||||
lastUsedAt: Date.now(),
|
||||
inFlight: 0,
|
||||
}
|
||||
}
|
||||
throw result.error
|
||||
}
|
||||
|
||||
if (
|
||||
kind === 'streamable-http' &&
|
||||
definition.transport !== 'sse' &&
|
||||
shouldFallbackToSse(result.error, definition)
|
||||
) {
|
||||
kind = 'sse'
|
||||
continue
|
||||
}
|
||||
throw result.error
|
||||
}
|
||||
}
|
||||
|
||||
private createClient(serverId: string, definition: McpServerDefinition): Client {
|
||||
const versionNegotiation = resolveVersionNegotiation(definition)
|
||||
return new Client(
|
||||
{ name: `shumai-mcp-${serverId}`, version: '1.0.0' },
|
||||
{
|
||||
...(versionNegotiation ? { versionNegotiation } : {}),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
private buildAuthProvider(
|
||||
serverId: string,
|
||||
definition: McpServerDefinition,
|
||||
): McpOauthProvider | undefined {
|
||||
if (this.supportsOauth(definition)) {
|
||||
return new McpOauthProvider(
|
||||
serverId,
|
||||
definition.url,
|
||||
this.extractOauthConfig(definition.authConfig ?? {}),
|
||||
{ onRedirect: async () => {} },
|
||||
this.dbStore,
|
||||
)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
private supportsOauth(definition: McpServerDefinition): boolean {
|
||||
const auth = definition.authConfig
|
||||
if (auth?.type === 'none') return false
|
||||
if (auth?.type === 'bearer') return false
|
||||
// Explicit oauth, or auto-detect when no auth configured and no custom headers.
|
||||
if (auth?.type === 'oauth') return true
|
||||
if (auth?.headers && Object.keys(auth.headers).length > 0) return false
|
||||
return auth === undefined || auth.type === undefined
|
||||
}
|
||||
|
||||
private extractOauthConfig(auth: PrismaJson.McpServerAuthConfig): McpOauthConfig {
|
||||
const oauth = auth.oauth
|
||||
if (!oauth) return {}
|
||||
return {
|
||||
...(oauth.grantType !== undefined ? { grantType: oauth.grantType } : {}),
|
||||
...(oauth.clientId !== undefined ? { clientId: oauth.clientId } : {}),
|
||||
...(oauth.clientSecret !== undefined ? { clientSecret: oauth.clientSecret } : {}),
|
||||
...(oauth.scope !== undefined ? { scope: oauth.scope } : {}),
|
||||
...(oauth.authorizationParams !== undefined
|
||||
? { authorizationParams: oauth.authorizationParams }
|
||||
: {}),
|
||||
...(oauth.redirectUri !== undefined ? { redirectUri: oauth.redirectUri } : {}),
|
||||
...(oauth.clientName !== undefined ? { clientName: oauth.clientName } : {}),
|
||||
...(oauth.clientUri !== undefined ? { clientUri: oauth.clientUri } : {}),
|
||||
...(oauth.skipIssuerMetadataValidation !== undefined
|
||||
? { skipIssuerMetadataValidation: oauth.skipIssuerMetadataValidation }
|
||||
: {}),
|
||||
}
|
||||
}
|
||||
|
||||
private buildRequestInit(
|
||||
definition: McpServerDefinition,
|
||||
): { headers: Record<string, string> } | undefined {
|
||||
const auth = definition.authConfig
|
||||
const headers: Record<string, string> = { ...(auth?.headers ?? {}) }
|
||||
if (auth?.type === 'bearer' && auth.bearerToken) {
|
||||
headers['Authorization'] = `Bearer ${auth.bearerToken}`
|
||||
}
|
||||
return Object.keys(headers).length > 0 ? { headers } : undefined
|
||||
}
|
||||
|
||||
private async fetchAllTools(client: Client, requestOptions?: RequestOptions): Promise<McpTool[]> {
|
||||
const allTools: McpTool[] = []
|
||||
let cursor: string | undefined
|
||||
|
||||
do {
|
||||
const result: ListToolsResult = await client.listTools(
|
||||
cursor ? { cursor } : undefined,
|
||||
requestOptions,
|
||||
)
|
||||
allTools.push(...(result.tools ?? []))
|
||||
cursor = result.nextCursor
|
||||
} while (cursor)
|
||||
|
||||
return allTools
|
||||
}
|
||||
|
||||
/** List tools for a connected server. */
|
||||
async listTools(serverId: string): Promise<McpTool[]> {
|
||||
const connection = this.connections.get(serverId)
|
||||
if (!connection || connection.status !== 'connected') {
|
||||
throw new Error(`Server is not connected`)
|
||||
}
|
||||
return connection.tools
|
||||
}
|
||||
|
||||
/** Call a tool on a connected server. */
|
||||
async callTool(
|
||||
serverId: string,
|
||||
toolName: string,
|
||||
args: Record<string, unknown>,
|
||||
): Promise<{
|
||||
content: McpContent[]
|
||||
structuredContent?: Record<string, unknown>
|
||||
isError?: boolean
|
||||
}> {
|
||||
const connection = this.connections.get(serverId)
|
||||
if (!connection || connection.status !== 'connected') {
|
||||
throw new Error(`Server is not connected`)
|
||||
}
|
||||
this.incrementInFlight(serverId)
|
||||
try {
|
||||
this.touch(serverId)
|
||||
const result = await connection.client.callTool(
|
||||
{ name: toolName, arguments: args },
|
||||
this.buildRequestOptions(connection.definition),
|
||||
)
|
||||
return {
|
||||
content: (result.content ?? []) as McpContent[],
|
||||
structuredContent: result.structuredContent as Record<string, unknown> | undefined,
|
||||
isError: result.isError,
|
||||
}
|
||||
} finally {
|
||||
this.decrementInFlight(serverId)
|
||||
this.touch(serverId)
|
||||
}
|
||||
}
|
||||
|
||||
incrementInFlight(serverId: string): void {
|
||||
const connection = this.connections.get(serverId)
|
||||
if (connection) {
|
||||
connection.inFlight = (connection.inFlight ?? 0) + 1
|
||||
}
|
||||
}
|
||||
|
||||
decrementInFlight(serverId: string): void {
|
||||
const connection = this.connections.get(serverId)
|
||||
if (connection && connection.inFlight > 0) {
|
||||
connection.inFlight--
|
||||
}
|
||||
}
|
||||
|
||||
isIdle(serverId: string, timeoutMs: number): boolean {
|
||||
const connection = this.connections.get(serverId)
|
||||
if (!connection || connection.status !== 'connected') return false
|
||||
if (connection.inFlight > 0) return false
|
||||
return Date.now() - connection.lastUsedAt > timeoutMs
|
||||
}
|
||||
|
||||
async closeIdleConnections(defaultTimeoutMs = 10 * 60 * 1000): Promise<string[]> {
|
||||
const closedIds: string[] = []
|
||||
for (const [serverId, connection] of this.connections) {
|
||||
if (connection.definition.config?.keepAlive) continue
|
||||
const timeoutMs = connection.definition.config?.idleTimeoutMs ?? defaultTimeoutMs
|
||||
if (this.isIdle(serverId, timeoutMs)) {
|
||||
await this.close(serverId)
|
||||
closedIds.push(serverId)
|
||||
}
|
||||
}
|
||||
return closedIds
|
||||
}
|
||||
|
||||
touch(serverId: string): void {
|
||||
const connection = this.connections.get(serverId)
|
||||
if (connection) {
|
||||
connection.lastUsedAt = Date.now()
|
||||
}
|
||||
}
|
||||
|
||||
getConnection(serverId: string): ServerConnection | undefined {
|
||||
return this.connections.get(serverId)
|
||||
}
|
||||
|
||||
getAllConnections(): Map<string, ServerConnection> {
|
||||
return new Map(this.connections)
|
||||
}
|
||||
|
||||
async close(serverId: string): Promise<void> {
|
||||
const connection = this.connections.get(serverId)
|
||||
if (!connection) {
|
||||
const pending = this.connectPromises.get(serverId)
|
||||
if (pending) {
|
||||
try {
|
||||
await pending
|
||||
} catch {
|
||||
// Ignore failed connect during close.
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
this.connections.delete(serverId)
|
||||
try {
|
||||
await connection.client.close()
|
||||
} catch (error) {
|
||||
logger.debug({ serverId, error }, 'MCP client close failed')
|
||||
}
|
||||
}
|
||||
|
||||
async closeAll(): Promise<void> {
|
||||
const ids = [...this.connections.keys()]
|
||||
await Promise.allSettled(ids.map((id) => this.close(id)))
|
||||
this.connections.clear()
|
||||
this.connectPromises.clear()
|
||||
// The manager stays usable afterwards (tests reset between suites);
|
||||
// `stopped` only guards against using a permanently-shut-down manager.
|
||||
this.stopped = false
|
||||
}
|
||||
|
||||
isConnecting(serverId: string): boolean {
|
||||
return this.connectPromises.has(serverId)
|
||||
}
|
||||
}
|
||||
|
||||
export interface McpContent {
|
||||
type: 'text' | 'image' | 'audio' | 'resource' | 'resource_link'
|
||||
text?: string
|
||||
data?: string
|
||||
mimeType?: string
|
||||
resource?: {
|
||||
uri: string
|
||||
text?: string
|
||||
blob?: string
|
||||
}
|
||||
uri?: string
|
||||
name?: string
|
||||
description?: string
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,965 @@
|
||||
/**
|
||||
* McpService — CRUD, discovery, OAuth, and agent integration for MCP servers.
|
||||
*/
|
||||
|
||||
import { auth } from '@modelcontextprotocol/client'
|
||||
import type { AgentTool, AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import { prisma } from '@shumai/db'
|
||||
import '@shumai/db/src/prisma-json-types'
|
||||
import type {
|
||||
CreateMcpServerRequest,
|
||||
McpAuthStatus,
|
||||
McpServerInfo,
|
||||
UpdateMcpServerRequest,
|
||||
} from '@shumai/dtos'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { mcpDbStore, type PendingAuth } from './mcp-db-store'
|
||||
import { buildDirectTools, isDirectTool } from './mcp-direct-tools'
|
||||
import { McpOauthProvider, type McpOauthConfig } from './mcp-oauth-provider'
|
||||
import { buildProxyTool, transformMcpContent, type McpProxyToolContext } from './mcp-proxy-tool'
|
||||
import {
|
||||
formatToolName,
|
||||
McpToolRegistry,
|
||||
sanitizeServerPrefix,
|
||||
type ToolMetadata,
|
||||
} from './mcp-tool-registry'
|
||||
import { McpServerManager, type McpServerDefinition } from './mcp-server-manager'
|
||||
|
||||
const ROLE_HIERARCHY: Record<string, number> = {
|
||||
owner: 3,
|
||||
editor: 2,
|
||||
reviewer: 1,
|
||||
}
|
||||
|
||||
export interface McpToolCallContext {
|
||||
teamId: string
|
||||
userId?: string
|
||||
}
|
||||
|
||||
export type McpToolCallContent = AgentToolResult<Record<string, unknown>>['content']
|
||||
|
||||
export interface McpToolCallResult {
|
||||
ok: boolean
|
||||
content: McpToolCallContent
|
||||
}
|
||||
|
||||
export interface McpServerRecord {
|
||||
id: string
|
||||
name: string
|
||||
description: string | null
|
||||
instructions: string | null
|
||||
url: string
|
||||
transport: 'streamable_http' | 'sse'
|
||||
authConfig: PrismaJson.McpServerAuthConfig | null
|
||||
config: PrismaJson.McpServerConfig | null
|
||||
permission: 'owner' | 'editor' | 'reviewer'
|
||||
tools: PrismaJson.McpToolInfo[] | null
|
||||
status: string
|
||||
lastError: string | null
|
||||
lastConnectedAt: Date | null
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
teamId: string
|
||||
credential: { id: string } | null
|
||||
}
|
||||
|
||||
/**
|
||||
* Derive a placeholder server name from its URL until the server self-reports
|
||||
* its name/title on first successful connection. Strips www/api/mcp prefixes
|
||||
* and sanitizes the remaining host label (used as the direct-tool prefix).
|
||||
*/
|
||||
function deriveServerName(url: string): string {
|
||||
try {
|
||||
const hostname = new URL(url).hostname.toLowerCase()
|
||||
const labels = hostname.split('.').filter(Boolean)
|
||||
let label = labels[0] ?? 'mcp-server'
|
||||
if (labels.length > 1 && ['www', 'api', 'mcp'].includes(label)) {
|
||||
label = labels[1]
|
||||
}
|
||||
const sanitized = label.replace(/[^a-z0-9_-]/g, '_').replace(/^[-_]+|[-_]+$/g, '')
|
||||
return sanitized || 'mcp-server'
|
||||
} catch {
|
||||
return 'mcp-server'
|
||||
}
|
||||
}
|
||||
|
||||
function generateState(): string {
|
||||
return Array.from(crypto.getRandomValues(new Uint8Array(32)))
|
||||
.map((b) => b.toString(16).padStart(2, '0'))
|
||||
.join('')
|
||||
}
|
||||
|
||||
function extractOauthConfig(authConfig?: PrismaJson.McpServerAuthConfig): McpOauthConfig {
|
||||
const oauth = authConfig?.oauth
|
||||
if (!oauth) return {}
|
||||
return {
|
||||
...(oauth.grantType !== undefined ? { grantType: oauth.grantType } : {}),
|
||||
...(oauth.clientId !== undefined ? { clientId: oauth.clientId } : {}),
|
||||
...(oauth.clientSecret !== undefined ? { clientSecret: oauth.clientSecret } : {}),
|
||||
...(oauth.scope !== undefined ? { scope: oauth.scope } : {}),
|
||||
...(oauth.authorizationParams !== undefined
|
||||
? { authorizationParams: oauth.authorizationParams }
|
||||
: {}),
|
||||
...(oauth.redirectUri !== undefined ? { redirectUri: oauth.redirectUri } : {}),
|
||||
...(oauth.clientName !== undefined ? { clientName: oauth.clientName } : {}),
|
||||
...(oauth.clientUri !== undefined ? { clientUri: oauth.clientUri } : {}),
|
||||
...(oauth.skipIssuerMetadataValidation !== undefined
|
||||
? { skipIssuerMetadataValidation: oauth.skipIssuerMetadataValidation }
|
||||
: {}),
|
||||
}
|
||||
}
|
||||
|
||||
export class McpService {
|
||||
private manager = new McpServerManager(mcpDbStore)
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// CRUD
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
async createServer(teamId: string, req: CreateMcpServerRequest): Promise<McpServerInfo> {
|
||||
const server = await prisma.mcpServer.create({
|
||||
data: {
|
||||
name: deriveServerName(req.url),
|
||||
url: req.url,
|
||||
teamId,
|
||||
transport: req.transport ?? 'streamable_http',
|
||||
authConfig: (req.authConfig ?? {}) as PrismaJson.McpServerAuthConfig,
|
||||
config: (req.config ?? {}) as PrismaJson.McpServerConfig,
|
||||
permission: req.permission ?? 'reviewer',
|
||||
},
|
||||
include: { credential: true },
|
||||
})
|
||||
return this.toServerInfo(this.mapRecord(server))
|
||||
}
|
||||
|
||||
async updateServer(id: string, req: UpdateMcpServerRequest): Promise<McpServerInfo> {
|
||||
const existing = await prisma.mcpServer.findUnique({ where: { id } })
|
||||
if (!existing) throw new Error('MCP server not found')
|
||||
|
||||
const authChanged =
|
||||
req.authConfig !== undefined &&
|
||||
JSON.stringify(req.authConfig) !== JSON.stringify(existing.authConfig ?? {})
|
||||
const transportChanged = req.transport !== undefined && req.transport !== existing.transport
|
||||
|
||||
// The endpoint URL is immutable (delete + re-add to change it).
|
||||
// Auth/transport changes invalidate credentials and cached tools.
|
||||
const invalidateAll = authChanged || transportChanged
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
if (invalidateAll) {
|
||||
await tx.mcpServerCredential.deleteMany({ where: { serverId: id } })
|
||||
}
|
||||
return tx.mcpServer.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...(req.transport !== undefined ? { transport: req.transport } : {}),
|
||||
...(req.authConfig !== undefined
|
||||
? { authConfig: req.authConfig as PrismaJson.McpServerAuthConfig }
|
||||
: {}),
|
||||
...(req.config !== undefined ? { config: req.config as PrismaJson.McpServerConfig } : {}),
|
||||
...(req.permission !== undefined ? { permission: req.permission } : {}),
|
||||
...(invalidateAll ? { tools: [], status: 'not_connected', lastError: null } : {}),
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
if (invalidateAll) {
|
||||
await this.manager.close(id)
|
||||
}
|
||||
|
||||
const record = await this.getServerRecord(id)
|
||||
if (!record) throw new Error('MCP server not found')
|
||||
return this.toServerInfo(record)
|
||||
}
|
||||
|
||||
async updateServerPermission(
|
||||
id: string,
|
||||
permission: 'owner' | 'editor' | 'reviewer',
|
||||
): Promise<McpServerInfo> {
|
||||
const server = await prisma.mcpServer.update({
|
||||
where: { id },
|
||||
data: { permission },
|
||||
include: { credential: true },
|
||||
})
|
||||
return this.toServerInfo(this.mapRecord(server))
|
||||
}
|
||||
|
||||
async deleteServer(id: string): Promise<void> {
|
||||
await this.manager.close(id)
|
||||
const server = await prisma.mcpServer.findUnique({ where: { id } })
|
||||
if (!server) return
|
||||
await prisma.mcpServer.delete({ where: { id } })
|
||||
}
|
||||
|
||||
async listServers(teamId: string): Promise<McpServerInfo[]> {
|
||||
const servers = await prisma.mcpServer.findMany({
|
||||
where: { teamId },
|
||||
orderBy: { id: 'desc' },
|
||||
include: { credential: true },
|
||||
})
|
||||
return servers.map((s) => this.toServerInfo(this.mapRecord(s)))
|
||||
}
|
||||
|
||||
async getServerRecord(id: string): Promise<McpServerRecord | null> {
|
||||
const server = await prisma.mcpServer.findUnique({
|
||||
where: { id },
|
||||
include: { credential: true },
|
||||
})
|
||||
if (!server) return null
|
||||
return this.mapRecord(server)
|
||||
}
|
||||
|
||||
async getServer(id: string): Promise<McpServerInfo | null> {
|
||||
const server = await this.getServerRecord(id)
|
||||
if (!server) return null
|
||||
return this.toServerInfo(server)
|
||||
}
|
||||
|
||||
private mapRecord(server: {
|
||||
id: string
|
||||
name: string
|
||||
description: string | null
|
||||
instructions: string | null
|
||||
url: string
|
||||
transport: 'streamable_http' | 'sse'
|
||||
authConfig: unknown
|
||||
config: unknown
|
||||
permission: 'owner' | 'editor' | 'reviewer'
|
||||
tools: unknown
|
||||
status: string
|
||||
lastError: string | null
|
||||
lastConnectedAt: Date | null
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
teamId: string
|
||||
credential: { id: string } | null
|
||||
}): McpServerRecord {
|
||||
return {
|
||||
id: server.id,
|
||||
name: server.name,
|
||||
description: server.description,
|
||||
instructions: server.instructions,
|
||||
url: server.url,
|
||||
transport: server.transport,
|
||||
authConfig: (server.authConfig ?? null) as PrismaJson.McpServerAuthConfig | null,
|
||||
config: (server.config ?? null) as PrismaJson.McpServerConfig | null,
|
||||
permission: server.permission,
|
||||
tools: (server.tools ?? null) as PrismaJson.McpToolInfo[] | null,
|
||||
status: server.status,
|
||||
lastError: server.lastError,
|
||||
lastConnectedAt: server.lastConnectedAt,
|
||||
createdAt: server.createdAt,
|
||||
updatedAt: server.updatedAt,
|
||||
teamId: server.teamId,
|
||||
credential: server.credential,
|
||||
}
|
||||
}
|
||||
|
||||
private toServerInfo(server: McpServerRecord): McpServerInfo {
|
||||
const tools = server.tools ?? []
|
||||
return {
|
||||
id: server.id,
|
||||
name: server.name,
|
||||
description: server.description ?? undefined,
|
||||
instructions: server.instructions ?? undefined,
|
||||
url: server.url,
|
||||
transport: server.transport,
|
||||
authType: server.authConfig?.type ?? 'auto',
|
||||
config: server.config ?? undefined,
|
||||
permission: server.permission,
|
||||
status: server.status,
|
||||
lastError: server.lastError ?? undefined,
|
||||
toolCount: Array.isArray(tools) ? tools.length : 0,
|
||||
hasCredential: server.credential !== null,
|
||||
createdAt: server.createdAt.toISOString(),
|
||||
updatedAt: server.updatedAt.toISOString(),
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Discovery
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
private toDefinition(server: McpServerRecord): McpServerDefinition {
|
||||
return {
|
||||
name: server.name,
|
||||
url: server.url,
|
||||
transport: server.transport,
|
||||
authConfig: server.authConfig ?? undefined,
|
||||
config: server.config ?? undefined,
|
||||
}
|
||||
}
|
||||
|
||||
private async updateStatus(
|
||||
serverId: string,
|
||||
status: string,
|
||||
lastError: string | null,
|
||||
extra?: {
|
||||
tools?: PrismaJson.McpToolInfo[]
|
||||
lastConnectedAt?: Date
|
||||
name?: string
|
||||
description?: string | null
|
||||
instructions?: string | null
|
||||
},
|
||||
): Promise<void> {
|
||||
await prisma.mcpServer.update({
|
||||
where: { id: serverId },
|
||||
data: {
|
||||
status,
|
||||
lastError,
|
||||
...(extra?.tools !== undefined ? { tools: extra.tools } : {}),
|
||||
...(extra?.lastConnectedAt !== undefined ? { lastConnectedAt: extra.lastConnectedAt } : {}),
|
||||
...(extra?.name !== undefined ? { name: extra.name } : {}),
|
||||
...(extra?.description !== undefined ? { description: extra.description } : {}),
|
||||
...(extra?.instructions !== undefined ? { instructions: extra.instructions } : {}),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
private toStoredTools(
|
||||
tools: Array<{ name: string; title?: string; description?: string; inputSchema?: unknown }>,
|
||||
): PrismaJson.McpToolInfo[] {
|
||||
return tools.map((t) => ({
|
||||
name: t.name,
|
||||
...(t.title !== undefined ? { title: t.title } : {}),
|
||||
...(t.description !== undefined ? { description: t.description } : {}),
|
||||
...(t.inputSchema !== undefined ? { inputSchema: t.inputSchema } : {}),
|
||||
}))
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist the server's self-reported metadata (name = title ?? name, plus
|
||||
* description and instructions) captured after a successful connect.
|
||||
* Description comes from getServerVersion(); instructions come from
|
||||
* getInstructions() (a sibling field of the initialize/discover result,
|
||||
* not part of Implementation).
|
||||
*/
|
||||
private serverMetadata(
|
||||
server: McpServerRecord,
|
||||
connection: { serverVersion?: unknown; instructions?: string },
|
||||
): {
|
||||
name?: string
|
||||
description?: string | null
|
||||
instructions?: string | null
|
||||
} {
|
||||
const metadata: {
|
||||
name?: string
|
||||
description?: string | null
|
||||
instructions?: string | null
|
||||
} = {}
|
||||
const impl = connection.serverVersion as
|
||||
| { name?: string; title?: string; description?: string }
|
||||
| undefined
|
||||
if (impl && typeof impl === 'object') {
|
||||
const reportedName = impl.title ?? impl.name
|
||||
if (typeof reportedName === 'string' && reportedName.trim() !== '') {
|
||||
metadata.name = reportedName.trim()
|
||||
metadata.description = typeof impl.description === 'string' ? impl.description : null
|
||||
}
|
||||
}
|
||||
if (typeof connection.instructions === 'string') {
|
||||
metadata.instructions = connection.instructions
|
||||
} else {
|
||||
// Sync-on-connect: a server that stops reporting instructions clears
|
||||
// the cached value (mirrors pi-mcp-adapter's set/delete behavior).
|
||||
metadata.instructions = null
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh a server: force a fresh connection (closing any existing one),
|
||||
* then re-discover and persist tools, instructions, name/description and
|
||||
* status. Returns the updated server record. Throws on connection failure
|
||||
* (after marking the server as failed). This is the single discovery
|
||||
* entry point: the refresh endpoint, Test Connection, and post-OAuth
|
||||
* completion all route through it.
|
||||
*/
|
||||
async refreshServer(serverId: string): Promise<McpServerInfo> {
|
||||
const server = await this.getServerRecord(serverId)
|
||||
if (!server) throw new Error('MCP server not found')
|
||||
await this.manager.close(serverId)
|
||||
|
||||
try {
|
||||
const connection = await this.manager.connect(serverId, this.toDefinition(server))
|
||||
if (connection.status === 'needs-auth') {
|
||||
await this.updateStatus(serverId, 'needs_auth', null)
|
||||
} else {
|
||||
const tools = this.toStoredTools(connection.tools)
|
||||
const metadata = this.serverMetadata(server, connection)
|
||||
await this.updateStatus(serverId, 'connected', null, {
|
||||
tools,
|
||||
lastConnectedAt: new Date(),
|
||||
...metadata,
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
logger.warn({ serverId, error }, 'MCP server refresh failed')
|
||||
await this.updateStatus(serverId, 'failed', message)
|
||||
throw error
|
||||
}
|
||||
|
||||
const record = await this.getServerRecord(serverId)
|
||||
if (!record) throw new Error('MCP server not found')
|
||||
return this.toServerInfo(record)
|
||||
}
|
||||
|
||||
/** Lazy connect used by the proxy/direct tools; never throws. */
|
||||
private async ensureConnected(
|
||||
serverId: string,
|
||||
registry?: McpToolRegistry,
|
||||
): Promise<
|
||||
{ status: 'connected' } | { status: 'needs-auth' } | { status: 'error'; message: string }
|
||||
> {
|
||||
const server = await this.getServerRecord(serverId)
|
||||
if (!server) return { status: 'error', message: 'server not found' }
|
||||
|
||||
const existing = this.manager.getConnection(serverId)
|
||||
if (existing?.status === 'connected') return { status: 'connected' }
|
||||
|
||||
try {
|
||||
const connection = await this.manager.connect(serverId, this.toDefinition(server))
|
||||
if (connection.status === 'needs-auth') {
|
||||
await this.updateStatus(serverId, 'needs_auth', null)
|
||||
return { status: 'needs-auth' }
|
||||
}
|
||||
const tools = this.toStoredTools(connection.tools)
|
||||
const metadata = this.serverMetadata(server, connection)
|
||||
const displayName = metadata.name ?? server.name
|
||||
registry?.setTools(serverId, displayName, connection.tools, {
|
||||
excludeTools: server.config?.excludeTools,
|
||||
})
|
||||
await this.updateStatus(serverId, 'connected', null, {
|
||||
tools,
|
||||
lastConnectedAt: new Date(),
|
||||
...metadata,
|
||||
})
|
||||
return { status: 'connected' }
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
await this.updateStatus(serverId, 'failed', message)
|
||||
return { status: 'error', message }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Server-reported usage instructions. Prefers the persisted value (the
|
||||
* cache, so it works without a live connection), falling back to the live
|
||||
* connection's getInstructions() for a just-connected server.
|
||||
*/
|
||||
private async getServerInstructions(serverId: string): Promise<string | null> {
|
||||
const server = await this.getServerRecord(serverId)
|
||||
if (!server) return null
|
||||
if (server.instructions && server.instructions.trim() !== '') return server.instructions
|
||||
const connection = this.manager.getConnection(serverId)
|
||||
const live = connection?.client.getInstructions?.()
|
||||
return typeof live === 'string' && live.trim() !== '' ? live : null
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Tool execution
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
private async checkPermission(
|
||||
server: McpServerRecord,
|
||||
ctx?: McpToolCallContext,
|
||||
): Promise<string | null> {
|
||||
const requiredLevel = ROLE_HIERARCHY[server.permission] || 1
|
||||
|
||||
if (ctx?.userId) {
|
||||
const member = await prisma.teamMember.findUnique({
|
||||
where: {
|
||||
teamIdUserId: {
|
||||
teamId: server.teamId,
|
||||
userId: ctx.userId,
|
||||
},
|
||||
},
|
||||
select: { role: true },
|
||||
})
|
||||
const userLevel = member ? ROLE_HIERARCHY[member.role] || 0 : 0
|
||||
if (userLevel < requiredLevel) {
|
||||
return `Permission denied: Insufficient role to use MCP server "${server.name}". Minimum required role is "${server.permission}".`
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
if (requiredLevel > 1) {
|
||||
return `Permission denied: User context required to use MCP server "${server.name}". Minimum required role is "${server.permission}".`
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private contentFromMcpResult(result: {
|
||||
content: unknown[]
|
||||
structuredContent?: Record<string, unknown>
|
||||
isError?: boolean
|
||||
}): McpToolCallContent {
|
||||
const blocks = transformMcpContent(
|
||||
(Array.isArray(result.content) ? result.content : []) as Parameters<
|
||||
typeof transformMcpContent
|
||||
>[0],
|
||||
)
|
||||
if (blocks.length > 0) return blocks
|
||||
if (result.structuredContent !== undefined && result.structuredContent !== null) {
|
||||
return [
|
||||
{
|
||||
type: 'text',
|
||||
text:
|
||||
JSON.stringify(result.structuredContent, null, 2) ?? String(result.structuredContent),
|
||||
},
|
||||
]
|
||||
}
|
||||
return [{ type: 'text', text: '(empty result)' }]
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute an MCP tool (shared by the proxy and direct tools).
|
||||
* Permission gate (D10) first; never throws — failures become error text.
|
||||
*/
|
||||
async callMcpTool(
|
||||
serverId: string,
|
||||
toolName: string,
|
||||
args: Record<string, unknown>,
|
||||
ctx?: McpToolCallContext,
|
||||
): Promise<McpToolCallResult> {
|
||||
const server = await this.getServerRecord(serverId)
|
||||
if (!server) {
|
||||
return { ok: false, content: [{ type: 'text', text: 'MCP server not found' }] }
|
||||
}
|
||||
|
||||
const denied = await this.checkPermission(server, ctx)
|
||||
if (denied) {
|
||||
return { ok: false, content: [{ type: 'text', text: denied }] }
|
||||
}
|
||||
|
||||
const outcome = await this.ensureConnected(serverId)
|
||||
if (outcome.status === 'needs-auth') {
|
||||
return {
|
||||
ok: false,
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `MCP server "${server.name}" requires authentication. Ask an admin to connect it in Settings → MCP.`,
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
if (outcome.status === 'error') {
|
||||
return {
|
||||
ok: false,
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Failed to connect to MCP server "${server.name}": ${outcome.message}`,
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await this.manager.callTool(serverId, toolName, args)
|
||||
if (result.isError) {
|
||||
const text = result.content.map((c) => ('text' in c ? c.text : '[image]')).join('\n')
|
||||
return {
|
||||
ok: true,
|
||||
content: [{ type: 'text', text: text ? `Error: ${text}` : 'Tool execution failed' }],
|
||||
}
|
||||
}
|
||||
return { ok: true, content: this.contentFromMcpResult(result) }
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
await this.updateStatus(serverId, 'failed', message)
|
||||
return {
|
||||
ok: false,
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Failed to call tool "${toolName}" on MCP server "${server.name}": ${message}`,
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// OAuth
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
async startAuth(serverId: string): Promise<{
|
||||
authorizationUrl?: string
|
||||
alreadyAuthenticated?: boolean
|
||||
}> {
|
||||
const server = await this.getServerRecord(serverId)
|
||||
if (!server) throw new Error('MCP server not found')
|
||||
|
||||
const config = extractOauthConfig(server.authConfig ?? undefined)
|
||||
if (config.grantType === 'client_credentials') {
|
||||
const provider = new McpOauthProvider(
|
||||
serverId,
|
||||
server.url,
|
||||
config,
|
||||
{
|
||||
onRedirect: async () => {
|
||||
throw new Error('Browser redirect is not used for client_credentials flow')
|
||||
},
|
||||
},
|
||||
mcpDbStore,
|
||||
)
|
||||
try {
|
||||
const result = await auth(provider, {
|
||||
serverUrl: server.url,
|
||||
...(config.scope !== undefined ? { scope: config.scope } : {}),
|
||||
...(config.skipIssuerMetadataValidation === true
|
||||
? { skipIssuerMetadataValidation: true }
|
||||
: {}),
|
||||
})
|
||||
if (result !== 'AUTHORIZED') {
|
||||
throw new Error('Failed to authorize')
|
||||
}
|
||||
await mcpDbStore.clearPendingAuth(serverId)
|
||||
return { alreadyAuthenticated: true }
|
||||
} finally {
|
||||
provider.deactivate()
|
||||
}
|
||||
}
|
||||
|
||||
let capturedUrl: URL | undefined
|
||||
const provider = new McpOauthProvider(
|
||||
serverId,
|
||||
server.url,
|
||||
config,
|
||||
{
|
||||
onRedirect: async (url) => {
|
||||
capturedUrl = url
|
||||
},
|
||||
},
|
||||
mcpDbStore,
|
||||
)
|
||||
|
||||
try {
|
||||
const result = await auth(provider, {
|
||||
serverUrl: server.url,
|
||||
...(config.scope !== undefined ? { scope: config.scope } : {}),
|
||||
...(config.skipIssuerMetadataValidation === true
|
||||
? { skipIssuerMetadataValidation: true }
|
||||
: {}),
|
||||
})
|
||||
if (result === 'AUTHORIZED') {
|
||||
await mcpDbStore.clearPendingAuth(serverId)
|
||||
return { alreadyAuthenticated: true }
|
||||
}
|
||||
if (!capturedUrl) {
|
||||
throw new Error('OAuth authorization URL was not provided')
|
||||
}
|
||||
|
||||
const state = (await mcpDbStore.getOauthState(serverId)) ?? generateState()
|
||||
const discoveryState = await provider.discoveryState()
|
||||
const pending: PendingAuth = {
|
||||
state,
|
||||
authorizationUrl: capturedUrl.toString(),
|
||||
discovery: (discoveryState ?? {}) as Record<string, unknown>,
|
||||
expiresAt: Math.floor(Date.now() / 1000) + (await mcpDbStore.getPendingAuthTtlSeconds()),
|
||||
grantType: 'authorization_code',
|
||||
...(provider.redirectUrl ? { redirectUri: provider.redirectUrl } : {}),
|
||||
}
|
||||
await mcpDbStore.savePendingAuth(serverId, pending)
|
||||
return { authorizationUrl: capturedUrl.toString() }
|
||||
} finally {
|
||||
provider.deactivate()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Complete an authorization_code flow. `state` is validated by the caller
|
||||
* (the API callback route) before delegating here.
|
||||
*/
|
||||
async completeAuth(
|
||||
serverId: string,
|
||||
input: { code: string; iss?: string },
|
||||
): Promise<McpAuthStatus> {
|
||||
const server = await this.getServerRecord(serverId)
|
||||
if (!server) throw new Error('MCP server not found')
|
||||
const pending = await mcpDbStore.getPendingAuth(serverId)
|
||||
if (!pending) throw new Error(`No pending OAuth flow for server: ${server.name}`)
|
||||
|
||||
const config = extractOauthConfig(server.authConfig ?? undefined)
|
||||
const provider = new McpOauthProvider(
|
||||
serverId,
|
||||
server.url,
|
||||
config,
|
||||
{ onRedirect: async () => {} },
|
||||
mcpDbStore,
|
||||
)
|
||||
|
||||
try {
|
||||
const result = await auth(provider, {
|
||||
serverUrl: server.url,
|
||||
authorizationCode: input.code,
|
||||
...(input.iss !== undefined ? { iss: input.iss } : {}),
|
||||
...(pending.discovery as Record<string, unknown>),
|
||||
...(config.skipIssuerMetadataValidation === true
|
||||
? { skipIssuerMetadataValidation: true }
|
||||
: {}),
|
||||
})
|
||||
if (result !== 'AUTHORIZED') {
|
||||
throw new Error('Failed to authorize')
|
||||
}
|
||||
await mcpDbStore.clearPendingAuth(serverId)
|
||||
await mcpDbStore.clearDiscoverySnapshot(serverId)
|
||||
try {
|
||||
await this.refreshServer(serverId)
|
||||
} catch (err) {
|
||||
logger.warn(
|
||||
{ serverId, err },
|
||||
'Post-auth server refresh failed, setting status to connected',
|
||||
)
|
||||
await this.updateStatus(serverId, 'connected', null)
|
||||
}
|
||||
return 'authenticated'
|
||||
} finally {
|
||||
provider.deactivate()
|
||||
}
|
||||
}
|
||||
|
||||
/** Find a pending flow by its CSRF state and complete it. */
|
||||
async handleOauthCallback(
|
||||
code: string,
|
||||
state: string,
|
||||
iss?: string,
|
||||
): Promise<{ ok: true } | { ok: false; message: string }> {
|
||||
const rows = await prisma.mcpServerCredential.findMany({
|
||||
select: { serverId: true, pendingAuth: true },
|
||||
})
|
||||
|
||||
let serverId: string | undefined
|
||||
for (const row of rows) {
|
||||
const raw = row.pendingAuth as Partial<PendingAuth> | null
|
||||
if (raw && typeof raw.state === 'string' && raw.state === state) {
|
||||
serverId = row.serverId
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!serverId) {
|
||||
return { ok: false, message: 'Invalid or expired OAuth state. Start authentication again.' }
|
||||
}
|
||||
|
||||
try {
|
||||
const status = await this.completeAuth(serverId, { code, iss })
|
||||
return status === 'authenticated'
|
||||
? { ok: true }
|
||||
: { ok: false, message: 'Authentication did not complete' }
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
logger.error({ serverId, error }, 'MCP OAuth callback completion failed')
|
||||
await mcpDbStore.clearPendingAuth(serverId)
|
||||
return { ok: false, message }
|
||||
}
|
||||
}
|
||||
|
||||
async getAuthStatus(serverId: string): Promise<McpAuthStatus> {
|
||||
const server = await this.getServerRecord(serverId)
|
||||
if (!server) throw new Error('MCP server not found')
|
||||
|
||||
const authType = server.authConfig?.type
|
||||
if (authType === 'none') return 'authenticated'
|
||||
if (authType === 'bearer') {
|
||||
return server.authConfig?.bearerToken ? 'authenticated' : 'not_authenticated'
|
||||
}
|
||||
|
||||
const pending = await mcpDbStore.getPendingAuth(serverId)
|
||||
if (pending) return 'in_progress'
|
||||
const hasTokens = await mcpDbStore.hasStoredTokens(serverId)
|
||||
if (!hasTokens) return 'not_authenticated'
|
||||
const expired = await mcpDbStore.isTokenExpired(serverId)
|
||||
return expired ? 'expired' : 'authenticated'
|
||||
}
|
||||
|
||||
async removeAuth(serverId: string): Promise<void> {
|
||||
await mcpDbStore.clearAllCredentials(serverId)
|
||||
await mcpDbStore.clearPendingAuth(serverId)
|
||||
await mcpDbStore.clearDiscoverySnapshot(serverId)
|
||||
await this.manager.close(serverId)
|
||||
await this.updateStatus(serverId, 'needs_auth', null)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Agent integration
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Build the agent's MCP AgentTools: the single `mcp` proxy tool plus direct
|
||||
* tools for assigned servers in direct-tools mode. Returns [] when the agent
|
||||
* has no assigned servers (no proxy tool — don't waste LLM context).
|
||||
*/
|
||||
async buildAgentTools(agentId: string, teamId: string, userId?: string): Promise<AgentTool[]> {
|
||||
const assignments = await prisma.agentMcpServer.findMany({
|
||||
where: { agentId },
|
||||
include: { mcpServer: true },
|
||||
})
|
||||
if (assignments.length === 0) return []
|
||||
|
||||
// Per-agent tool registry: built fresh for this agent from the persisted
|
||||
// discovery cache (McpServer.tools). It only ever contains the agent's
|
||||
// assigned servers, so unassigned/disabled servers can neither be searched
|
||||
// nor called — no global mutable state to leak through.
|
||||
const registry = new McpToolRegistry()
|
||||
|
||||
const servers = assignments
|
||||
.map((a) => a.mcpServer)
|
||||
.map((s) => {
|
||||
const storedTools = (s.tools ?? []) as PrismaJson.McpToolInfo[]
|
||||
if (storedTools.length > 0) {
|
||||
registry.setTools(
|
||||
s.id,
|
||||
s.name,
|
||||
storedTools.map((t) => ({
|
||||
name: t.name,
|
||||
description: t.description ?? '',
|
||||
...(t.inputSchema !== undefined ? { inputSchema: t.inputSchema } : {}),
|
||||
})),
|
||||
{ excludeTools: s.config?.excludeTools },
|
||||
)
|
||||
}
|
||||
return {
|
||||
id: s.id,
|
||||
name: s.name,
|
||||
instructions: s.instructions ?? null,
|
||||
url: s.url,
|
||||
transport: s.transport,
|
||||
authConfig: (s.authConfig ?? {}) as PrismaJson.McpServerAuthConfig,
|
||||
config: (s.config ?? {}) as PrismaJson.McpServerConfig,
|
||||
status: s.status,
|
||||
}
|
||||
})
|
||||
|
||||
const serverNameById = new Map<string, string>()
|
||||
const serverIdByName = new Map<string, string>()
|
||||
const proxyServers = servers.map((s) => {
|
||||
serverNameById.set(s.id, s.name)
|
||||
serverIdByName.set(s.name, s.id)
|
||||
// Count direct tools from the same filtered registry metadata that
|
||||
// buildDirectTools consumes, so the proxy description can never drift
|
||||
// from what is actually registered as native tools.
|
||||
const metadata = registry.getTools(s.id)
|
||||
const directToolCount = metadata.filter((t) =>
|
||||
isDirectTool(t.originalName, t.name, s.config?.directTools),
|
||||
).length
|
||||
|
||||
return {
|
||||
id: s.id,
|
||||
name: s.name,
|
||||
toolCount: metadata.length,
|
||||
directToolCount,
|
||||
status: s.status,
|
||||
...(s.instructions !== null && s.instructions !== undefined
|
||||
? { instructions: s.instructions }
|
||||
: {}),
|
||||
}
|
||||
})
|
||||
|
||||
const ctx: McpProxyToolContext = {
|
||||
teamId,
|
||||
userId,
|
||||
servers: proxyServers,
|
||||
serverNameById,
|
||||
serverIdByName,
|
||||
registry,
|
||||
resolveServerForTool: (toolName) => {
|
||||
// Longest {server}_ prefix match (dots/hyphens sanitized like the registry).
|
||||
let best: string | undefined
|
||||
let bestLen = 0
|
||||
for (const [sid, name] of serverNameById.entries()) {
|
||||
const prefix = sanitizeServerPrefix(name)
|
||||
if (toolName.startsWith(`${prefix}_`) && prefix.length > bestLen) {
|
||||
best = sid
|
||||
bestLen = prefix.length
|
||||
}
|
||||
}
|
||||
return best
|
||||
},
|
||||
ensureConnected: (serverId) => this.ensureConnected(serverId, registry),
|
||||
callTool: (serverId, toolName, args) =>
|
||||
this.callMcpTool(serverId, toolName, args, { teamId, userId }),
|
||||
authStatus: (serverId) => this.getAuthStatus(serverId),
|
||||
startAuth: (serverId) => this.startAuth(serverId),
|
||||
getInstructions: (serverId) => this.getServerInstructions(serverId),
|
||||
}
|
||||
|
||||
const tools: AgentTool[] = [buildProxyTool(ctx)]
|
||||
|
||||
for (const server of servers) {
|
||||
if (!Array.isArray(server.config?.directTools) || server.config.directTools.length === 0) {
|
||||
continue
|
||||
}
|
||||
const metadata: ToolMetadata[] = registry.getTools(server.id)
|
||||
tools.push(
|
||||
...buildDirectTools(metadata, server.config, {
|
||||
serverId: server.id,
|
||||
serverName: server.name,
|
||||
callTool: (serverId, toolName, args) =>
|
||||
this.callMcpTool(serverId, toolName, args, { teamId, userId }),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
return tools
|
||||
}
|
||||
|
||||
/** Test a server by refreshing it and reporting the tool count (UI validation). */
|
||||
async testServer(
|
||||
serverId: string,
|
||||
): Promise<{ ok: boolean; toolCount: number; message?: string }> {
|
||||
try {
|
||||
const server = await this.refreshServer(serverId)
|
||||
return { ok: true, toolCount: server.toolCount }
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
return { ok: false, toolCount: 0, message }
|
||||
}
|
||||
}
|
||||
|
||||
/** Tool-name formatter ({server}_{tool}); exposed for tests. */
|
||||
getToolName(serverName: string, toolName: string): string {
|
||||
return formatToolName(serverName, toolName)
|
||||
}
|
||||
|
||||
private idleCleanupInterval?: ReturnType<typeof setInterval>
|
||||
|
||||
/** Start a periodic background timer to prune idle connections. Default check interval is 30 seconds. */
|
||||
startIdleCleanupTimer(intervalMs = 30 * 1000, defaultTimeoutMs?: number): void {
|
||||
if (this.idleCleanupInterval) return
|
||||
this.idleCleanupInterval = setInterval(() => {
|
||||
this.closeIdleConnections(defaultTimeoutMs).catch((error) => {
|
||||
logger.debug({ error }, 'Failed to close idle connections in background timer')
|
||||
})
|
||||
}, intervalMs)
|
||||
this.idleCleanupInterval.unref?.()
|
||||
}
|
||||
|
||||
/** Stop the background idle cleanup timer. */
|
||||
stopIdleCleanupTimer(): void {
|
||||
if (this.idleCleanupInterval) {
|
||||
clearInterval(this.idleCleanupInterval)
|
||||
this.idleCleanupInterval = undefined
|
||||
}
|
||||
}
|
||||
|
||||
/** Close all in-process MCP connections (used by tests/shutdown). */
|
||||
async closeAllConnections(): Promise<void> {
|
||||
this.stopIdleCleanupTimer()
|
||||
await this.manager.closeAll()
|
||||
}
|
||||
|
||||
/** Close idle connections across all active server connections. */
|
||||
async closeIdleConnections(defaultTimeoutMs?: number): Promise<string[]> {
|
||||
return this.manager.closeIdleConnections(defaultTimeoutMs)
|
||||
}
|
||||
|
||||
/** Number of live in-process connections (used by tests). */
|
||||
getConnectionCount(serverId: string): number {
|
||||
return this.manager.getConnection(serverId) ? 1 : 0
|
||||
}
|
||||
}
|
||||
|
||||
export const mcpService = new McpService()
|
||||
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* In-process MCP test server for integration tests.
|
||||
*
|
||||
* Spins up a real Streamable HTTP MCP server on an ephemeral port using
|
||||
* `@modelcontextprotocol/server` (v2, stateless mode) and serves it over
|
||||
* Bun's web-standard fetch. Tests connect to it through the real MCP client
|
||||
* stack (`@modelcontextprotocol/client` v2), so connection lifecycle, tool
|
||||
* discovery, argument validation, and call round-trips are all exercised for
|
||||
* real — no mocks.
|
||||
*/
|
||||
|
||||
import { McpServer, WebStandardStreamableHTTPServerTransport } from '@modelcontextprotocol/server'
|
||||
import { z, type ZodRawShape } from 'zod'
|
||||
|
||||
export interface TestMcpToolDef {
|
||||
name: string
|
||||
description?: string
|
||||
inputSchema?: ZodRawShape
|
||||
handler: (args: Record<string, unknown>) => Promise<unknown>
|
||||
/** When set, the tool returns an isError result with this text. */
|
||||
errorText?: string
|
||||
}
|
||||
|
||||
export interface TestMcpServerOptions {
|
||||
tools: TestMcpToolDef[]
|
||||
/** If set, respond with 401 to any request that lacks this bearer token. */
|
||||
bearerToken?: string
|
||||
/** Port to bind; defaults to an ephemeral port. */
|
||||
port?: number
|
||||
/** Override the server's self-reported identity (getServerVersion result). */
|
||||
serverInfo?: { name: string; title?: string; description?: string; instructions?: string }
|
||||
}
|
||||
|
||||
export interface RunningTestMcpServer {
|
||||
url: string
|
||||
port: number
|
||||
stop(): Promise<void>
|
||||
/** Requests received, in order, with their headers. */
|
||||
requests: Array<{ url: string; headers: Record<string, string> }>
|
||||
}
|
||||
|
||||
export async function startTestMcpServer(
|
||||
options: TestMcpServerOptions,
|
||||
): Promise<RunningTestMcpServer> {
|
||||
const { serverInfo } = options
|
||||
const server = new McpServer(
|
||||
{
|
||||
name: serverInfo?.name ?? 'test-mcp-server',
|
||||
version: '1.0.0',
|
||||
...(serverInfo?.title !== undefined ? { title: serverInfo.title } : {}),
|
||||
...(serverInfo?.description !== undefined ? { description: serverInfo.description } : {}),
|
||||
},
|
||||
serverInfo?.instructions !== undefined ? { instructions: serverInfo.instructions } : undefined,
|
||||
)
|
||||
// registerTool has overloads with incompatible zod type resolutions; bind a
|
||||
// narrow local signature so tool registration stays type-safe and simple.
|
||||
const registerTool = server.registerTool.bind(server) as unknown as (
|
||||
name: string,
|
||||
config: { description?: string; inputSchema?: ZodRawShape },
|
||||
cb: (args: Record<string, unknown>) => Promise<unknown> | unknown,
|
||||
) => unknown
|
||||
for (const tool of options.tools) {
|
||||
registerTool(
|
||||
tool.name,
|
||||
{
|
||||
description: tool.description ?? `Test tool ${tool.name}`,
|
||||
inputSchema: tool.inputSchema ?? {},
|
||||
},
|
||||
async (args) => {
|
||||
if (tool.errorText) {
|
||||
return {
|
||||
content: [{ type: 'text', text: tool.errorText }],
|
||||
isError: true,
|
||||
}
|
||||
}
|
||||
const result = await tool.handler(args as Record<string, unknown>)
|
||||
if (typeof result === 'string') {
|
||||
return { content: [{ type: 'text', text: result }] }
|
||||
}
|
||||
if (
|
||||
result &&
|
||||
typeof result === 'object' &&
|
||||
'structuredContent' in (result as Record<string, unknown>)
|
||||
) {
|
||||
const structuredContent = (result as Record<string, unknown>).structuredContent
|
||||
return {
|
||||
content: [{ type: 'text', text: JSON.stringify(structuredContent) }],
|
||||
structuredContent,
|
||||
}
|
||||
}
|
||||
return { content: [{ type: 'text', text: JSON.stringify(result) }] }
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// Stateless mode: one transport instance, no session IDs.
|
||||
const transport = new WebStandardStreamableHTTPServerTransport({
|
||||
sessionIdGenerator: undefined,
|
||||
})
|
||||
await server.connect(transport)
|
||||
|
||||
const requests: RunningTestMcpServer['requests'] = []
|
||||
|
||||
const httpServer = Bun.serve({
|
||||
port: options.port ?? 0,
|
||||
idleTimeout: 30,
|
||||
async fetch(req) {
|
||||
requests.push({
|
||||
url: req.url,
|
||||
headers: Object.fromEntries(req.headers.entries()),
|
||||
})
|
||||
|
||||
if (options.bearerToken) {
|
||||
const auth = req.headers.get('authorization')
|
||||
if (auth !== `Bearer ${options.bearerToken}`) {
|
||||
return new Response('Unauthorized', { status: 401 })
|
||||
}
|
||||
}
|
||||
|
||||
const url = new URL(req.url)
|
||||
if (url.pathname === '/mcp') {
|
||||
return transport.handleRequest(req)
|
||||
}
|
||||
return new Response('not found', { status: 404 })
|
||||
},
|
||||
})
|
||||
|
||||
const port = httpServer.port as number
|
||||
return {
|
||||
url: `http://localhost:${port}/mcp`,
|
||||
port,
|
||||
requests,
|
||||
async stop() {
|
||||
await server.close().catch(() => {})
|
||||
httpServer.stop(true)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** Common test tools: echo (returns string), add (structured math), list_sims. */
|
||||
export function standardTestTools(): TestMcpToolDef[] {
|
||||
return [
|
||||
{
|
||||
name: 'echo',
|
||||
description: 'Echoes the provided text back',
|
||||
inputSchema: { text: z.string() },
|
||||
handler: async (args) => `echo:${String(args.text)}`,
|
||||
},
|
||||
{
|
||||
name: 'add',
|
||||
description: 'Adds two numbers',
|
||||
inputSchema: { a: z.number(), b: z.number() },
|
||||
handler: async (args) => ({
|
||||
structuredContent: { sum: Number(args.a) + Number(args.b) },
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: 'list_sims',
|
||||
description: 'Lists simulator devices',
|
||||
handler: async () => ['iPhone 15 Pro', 'iPad Pro'],
|
||||
},
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
McpToolRegistry,
|
||||
formatToolName,
|
||||
normalizeSearchText,
|
||||
sanitizeServerPrefix,
|
||||
tokenize,
|
||||
scoreToolMatch,
|
||||
paginate,
|
||||
} from './mcp-tool-registry'
|
||||
|
||||
function makeRegistry() {
|
||||
const registry = new McpToolRegistry()
|
||||
registry.setTools('server-1', 'xcodebuild', [
|
||||
{ name: 'list_sims', description: 'Lists available simulator devices' },
|
||||
{ name: 'build', description: 'Builds an Xcode project' },
|
||||
{ name: 'showBuildSettings', description: 'Shows build settings for a target' },
|
||||
])
|
||||
registry.setTools('server-2', 'github', [
|
||||
{ name: 'create_issue', description: 'Creates a GitHub issue' },
|
||||
{ name: 'list_issues', description: 'Lists issues in a repository' },
|
||||
{ name: 'search_code', description: 'Searches code across repositories' },
|
||||
])
|
||||
return registry
|
||||
}
|
||||
|
||||
describe('formatToolName', () => {
|
||||
it('prefixes server name and sanitizes dots', () => {
|
||||
expect(formatToolName('xcodebuild', 'list_sims')).toBe('xcodebuild_list_sims')
|
||||
expect(formatToolName('my-server', 'tool.name')).toBe('my_server_tool_name')
|
||||
})
|
||||
|
||||
it('sanitizes spaces and punctuation in the server prefix', () => {
|
||||
expect(formatToolName('Meta Server', 'echo')).toBe('Meta_Server_echo')
|
||||
expect(formatToolName('my.server/v1', 'list')).toBe('my_server_v1_list')
|
||||
expect(formatToolName('!!!', 'list')).toBe('list')
|
||||
})
|
||||
})
|
||||
|
||||
describe('sanitizeServerPrefix', () => {
|
||||
it('collapses non-alphanumeric runs into a single underscore', () => {
|
||||
expect(sanitizeServerPrefix('Meta Server')).toBe('Meta_Server')
|
||||
expect(sanitizeServerPrefix('github.com')).toBe('github_com')
|
||||
expect(sanitizeServerPrefix(' a b ')).toBe('a_b')
|
||||
})
|
||||
})
|
||||
|
||||
describe('normalizeSearchText / tokenize', () => {
|
||||
it('splits camelCase, snake_case, dots and hyphens', () => {
|
||||
expect(tokenize('showBuildSettings')).toEqual(['show', 'build', 'settings'])
|
||||
expect(tokenize('list_sims')).toEqual(['list', 'sims'])
|
||||
expect(tokenize('search.code-v2')).toEqual(['search', 'code', 'v2'])
|
||||
})
|
||||
|
||||
it('lowercases input', () => {
|
||||
expect(normalizeSearchText('ListSims')).toBe('list sims')
|
||||
})
|
||||
})
|
||||
|
||||
describe('scoreToolMatch', () => {
|
||||
const tool = {
|
||||
name: 'xcodebuild_list_sims',
|
||||
originalName: 'list_sims',
|
||||
description: 'Lists available simulator devices',
|
||||
}
|
||||
|
||||
it('returns null for empty queries', () => {
|
||||
expect(scoreToolMatch(tool, 'xcodebuild', '')).toBeNull()
|
||||
expect(scoreToolMatch(tool, 'xcodebuild', ' ')).toBeNull()
|
||||
})
|
||||
|
||||
it('scores exact name matches highest', () => {
|
||||
const exact = scoreToolMatch(tool, 'xcodebuild', 'list_sims')
|
||||
const partial = scoreToolMatch(tool, 'xcodebuild', 'sims')
|
||||
const unrelated = scoreToolMatch(tool, 'xcodebuild', 'zzz')
|
||||
expect(exact).not.toBeNull()
|
||||
expect(partial).not.toBeNull()
|
||||
expect(unrelated).toBeNull()
|
||||
expect(exact!).toBeGreaterThan(partial!)
|
||||
})
|
||||
|
||||
it('matches camelCase queries against camelCase original names', () => {
|
||||
const camel = scoreToolMatch(
|
||||
{
|
||||
name: 'xcodebuild_showBuildSettings',
|
||||
originalName: 'showBuildSettings',
|
||||
description: 'Shows build settings',
|
||||
},
|
||||
'xcodebuild',
|
||||
'showBuildSettings',
|
||||
)
|
||||
expect(camel).not.toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('registry searchTools', () => {
|
||||
it('finds tools across servers with ranking', () => {
|
||||
const registry = makeRegistry()
|
||||
const names = new Map([
|
||||
['server-1', 'xcodebuild'],
|
||||
['server-2', 'github'],
|
||||
])
|
||||
const { items, total } = registry.searchTools('list', undefined, names)
|
||||
expect(total).toBeGreaterThanOrEqual(2)
|
||||
// Both list_sims and list_issues should be found.
|
||||
const found = items.map((m) => m.tool.originalName)
|
||||
expect(found).toContain('list_sims')
|
||||
expect(found).toContain('list_issues')
|
||||
// Sorting is by score descending.
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
expect(items[i - 1].score).toBeGreaterThanOrEqual(items[i].score)
|
||||
}
|
||||
})
|
||||
|
||||
it('scopes search to one server', () => {
|
||||
const registry = makeRegistry()
|
||||
const names = new Map([
|
||||
['server-1', 'xcodebuild'],
|
||||
['server-2', 'github'],
|
||||
])
|
||||
const { items, total } = registry.searchTools('list', 'server-1', names)
|
||||
expect(total).toBe(1)
|
||||
expect(items[0].tool.originalName).toBe('list_sims')
|
||||
})
|
||||
|
||||
it('honors limit/offset pagination', () => {
|
||||
const registry = makeRegistry()
|
||||
const names = new Map([
|
||||
['server-1', 'xcodebuild'],
|
||||
['server-2', 'github'],
|
||||
])
|
||||
const first = registry.searchTools('list', undefined, names, 1, 0)
|
||||
expect(first.items.length).toBe(1)
|
||||
expect(first.hasMore).toBe(true)
|
||||
const second = registry.searchTools('list', undefined, names, 1, 1)
|
||||
expect(second.items.length).toBe(1)
|
||||
expect(second.nextOffset).toBeNull()
|
||||
})
|
||||
|
||||
it('coverage gate rejects weak matches', () => {
|
||||
const registry = makeRegistry()
|
||||
const names = new Map([['server-1', 'xcodebuild']])
|
||||
const { total } = registry.searchTools('building skyscrapers', 'server-1', names)
|
||||
expect(total).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('registry findTool', () => {
|
||||
it('finds by prefixed or original name', () => {
|
||||
const registry = makeRegistry()
|
||||
expect(registry.findTool('xcodebuild_list_sims')?.tool.originalName).toBe('list_sims')
|
||||
expect(registry.findTool('list_sims')?.tool.originalName).toBe('list_sims')
|
||||
expect(registry.findTool('list_sims', 'server-1')?.tool.originalName).toBe('list_sims')
|
||||
expect(registry.findTool('list_sims', 'server-2')).toBeUndefined()
|
||||
expect(registry.findTool('nope')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns did-you-mean suggestions', () => {
|
||||
const registry = makeRegistry()
|
||||
const suggestions = registry.rankSuggestions(
|
||||
'xcodebuild_list_sim',
|
||||
5,
|
||||
new Map([
|
||||
['server-1', 'xcodebuild'],
|
||||
['server-2', 'github'],
|
||||
]),
|
||||
)
|
||||
expect(suggestions).toContain('xcodebuild_list_sims')
|
||||
})
|
||||
})
|
||||
|
||||
describe('paginate', () => {
|
||||
it('handles empty input and offsets beyond length', () => {
|
||||
expect(paginate([], 0, 10)).toEqual({ items: [], total: 0, hasMore: false, nextOffset: null })
|
||||
expect(paginate([1, 2, 3], 100, 10)).toEqual({
|
||||
items: [],
|
||||
total: 3,
|
||||
hasMore: false,
|
||||
nextOffset: null,
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,275 @@
|
||||
/**
|
||||
* MCP tool registry — in-memory tool metadata + search
|
||||
*
|
||||
* Per-agent registry of discovered tool metadata keyed by server id, created
|
||||
* fresh by `McpService.buildAgentTools` and warmed from the persisted DB
|
||||
* cache (`McpServer.tools`). Scoping discovery to the agent's assigned
|
||||
* servers means unassigned/disabled servers can never be searched, suggested,
|
||||
* or called. (Adapted from pi-mcp-adapter's ToolMetadata + search-ranking.ts.)
|
||||
*/
|
||||
|
||||
import type { McpTool } from './mcp-server-manager'
|
||||
|
||||
export interface ToolMetadata {
|
||||
/** Prefixed tool name (e.g. "xcodebuild_list_sims"). */
|
||||
name: string
|
||||
/** Original MCP tool name (e.g. "list_sims"). */
|
||||
originalName: string
|
||||
description: string
|
||||
inputSchema?: unknown
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize a server name into a safe tool-name prefix: any run of
|
||||
* non-alphanumeric characters becomes a single underscore. Server-reported
|
||||
* names/titles may contain spaces, dots or other punctuation.
|
||||
*/
|
||||
export function sanitizeServerPrefix(serverName: string): string {
|
||||
return serverName.replace(/[^a-zA-Z0-9_]+/g, '_').replace(/^_+|_+$/g, '')
|
||||
}
|
||||
|
||||
/** Format a tool name with the "{server}_{tool}" prefix (dots sanitized). */
|
||||
export function formatToolName(serverName: string, toolName: string): string {
|
||||
const serverPart = sanitizeServerPrefix(serverName)
|
||||
const sanitized = toolName.replace(/\./g, '_')
|
||||
return serverPart ? `${serverPart}_${sanitized}` : sanitized
|
||||
}
|
||||
|
||||
function globToRegExp(pattern: string): RegExp {
|
||||
const escaped = pattern
|
||||
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
||||
.replace(/\*/g, '.*')
|
||||
.replace(/\?/g, '.')
|
||||
return new RegExp(`^${escaped}$`)
|
||||
}
|
||||
|
||||
function matchesToolPattern(name: string, patterns?: string[]): boolean {
|
||||
if (!Array.isArray(patterns) || patterns.length === 0) return false
|
||||
for (const pattern of patterns) {
|
||||
if (!pattern) continue
|
||||
if (!pattern.includes('*') && !pattern.includes('?') && name === pattern) return true
|
||||
if ((pattern.includes('*') || pattern.includes('?')) && globToRegExp(pattern).test(name)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide whether a tool is allowed by excludeTools (matches prefixed or
|
||||
* original name). Applied when building the per-agent registry so excluded
|
||||
* tools are invisible to both the proxy tool and direct registration.
|
||||
*/
|
||||
export function isToolAllowed(
|
||||
prefixedName: string,
|
||||
originalName: string,
|
||||
excludeTools?: string[],
|
||||
): boolean {
|
||||
if (
|
||||
matchesToolPattern(prefixedName, excludeTools) ||
|
||||
matchesToolPattern(originalName, excludeTools)
|
||||
) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
const MIN_STEM_LENGTH = 4
|
||||
|
||||
const FIELD_WEIGHTS = {
|
||||
name: 12,
|
||||
originalName: 10,
|
||||
server: 8,
|
||||
description: 5,
|
||||
} as const
|
||||
|
||||
export interface RankedToolMatch {
|
||||
server: string
|
||||
tool: ToolMetadata
|
||||
score: number
|
||||
}
|
||||
|
||||
export function normalizeSearchText(value: string): string {
|
||||
return value
|
||||
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
||||
.replace(/[_./:-]+/g, ' ')
|
||||
.toLowerCase()
|
||||
}
|
||||
|
||||
export function tokenize(value: string): string[] {
|
||||
return normalizeSearchText(value)
|
||||
.split(/[^a-z0-9]+/)
|
||||
.filter(Boolean)
|
||||
}
|
||||
|
||||
export function scoreToolMatch(tool: ToolMetadata, server: string, query: string): number | null {
|
||||
const normalizedQuery = normalizeSearchText(query).trim()
|
||||
const queryTokens = tokenize(query)
|
||||
if (queryTokens.length === 0) return null
|
||||
|
||||
const fields = {
|
||||
name: normalizeSearchText(tool.name),
|
||||
originalName: normalizeSearchText(tool.originalName),
|
||||
server: normalizeSearchText(server),
|
||||
description: normalizeSearchText(tool.description),
|
||||
}
|
||||
let score = 0
|
||||
let phraseMatched = false
|
||||
let wholeFieldExact = false
|
||||
const matchedTokens = new Set<string>()
|
||||
|
||||
for (const [field, value] of Object.entries(fields) as Array<
|
||||
[keyof typeof FIELD_WEIGHTS, string]
|
||||
>) {
|
||||
const weight = FIELD_WEIGHTS[field]
|
||||
const fieldTokens = tokenize(value)
|
||||
if (value === normalizedQuery) {
|
||||
score += weight * 14
|
||||
phraseMatched = true
|
||||
wholeFieldExact = true
|
||||
} else if (value.startsWith(normalizedQuery)) {
|
||||
score += weight * 9
|
||||
phraseMatched = true
|
||||
} else if (value.includes(normalizedQuery)) {
|
||||
score += weight * 6
|
||||
phraseMatched = true
|
||||
}
|
||||
|
||||
for (const token of queryTokens) {
|
||||
if (fieldTokens.includes(token)) {
|
||||
score += weight * 4
|
||||
matchedTokens.add(token)
|
||||
} else if (
|
||||
fieldTokens.some(
|
||||
(fieldToken) =>
|
||||
fieldToken.startsWith(token) ||
|
||||
(fieldToken.length >= MIN_STEM_LENGTH && token.startsWith(fieldToken)),
|
||||
)
|
||||
) {
|
||||
score += weight * 2
|
||||
matchedTokens.add(token)
|
||||
} else if (value.includes(token)) {
|
||||
score += weight
|
||||
matchedTokens.add(token)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const coverage = matchedTokens.size / queryTokens.length
|
||||
if (!phraseMatched && (queryTokens.length <= 2 ? coverage !== 1 : coverage < 0.6)) return null
|
||||
|
||||
score += coverage === 1 ? 25 : Math.round(coverage * 10)
|
||||
const firstQueryToken = queryTokens[0]
|
||||
if (firstQueryToken !== undefined && tokenize(fields.name).includes(firstQueryToken)) score += 8
|
||||
if (wholeFieldExact) score += 20
|
||||
return score
|
||||
}
|
||||
|
||||
export function paginate<T>(
|
||||
items: T[],
|
||||
offset: number,
|
||||
limit: number,
|
||||
): { items: T[]; total: number; hasMore: boolean; nextOffset: number | null } {
|
||||
const safeOffset = Number.isFinite(offset) ? Math.max(0, Math.trunc(offset)) : 0
|
||||
const safeLimit = Number.isFinite(limit) ? Math.max(1, Math.trunc(limit)) : 1
|
||||
const total = items.length
|
||||
const page = items.slice(safeOffset, safeOffset + safeLimit)
|
||||
const nextOffset = safeOffset + page.length
|
||||
return {
|
||||
items: page,
|
||||
total,
|
||||
hasMore: nextOffset < total,
|
||||
nextOffset: nextOffset < total ? nextOffset : null,
|
||||
}
|
||||
}
|
||||
|
||||
export class McpToolRegistry {
|
||||
private readonly metadataByServer = new Map<string, ToolMetadata[]>()
|
||||
|
||||
/**
|
||||
* Rebuild the metadata for one server from its discovered tools, skipping
|
||||
* tools excluded via excludeTools so they are unreachable through the proxy.
|
||||
*/
|
||||
setTools(
|
||||
serverId: string,
|
||||
serverName: string,
|
||||
tools: McpTool[],
|
||||
opts?: { excludeTools?: string[] },
|
||||
): void {
|
||||
const metadata: ToolMetadata[] = []
|
||||
for (const tool of tools) {
|
||||
const name = formatToolName(serverName, tool.name)
|
||||
if (!isToolAllowed(name, tool.name, opts?.excludeTools)) continue
|
||||
metadata.push({
|
||||
name,
|
||||
originalName: tool.name,
|
||||
description: tool.description ?? '',
|
||||
...(tool.inputSchema !== undefined ? { inputSchema: tool.inputSchema } : {}),
|
||||
})
|
||||
}
|
||||
metadata.sort((a, b) => a.name.localeCompare(b.name))
|
||||
this.metadataByServer.set(serverId, metadata)
|
||||
}
|
||||
|
||||
getTools(serverId: string): ToolMetadata[] {
|
||||
return this.metadataByServer.get(serverId) ?? []
|
||||
}
|
||||
|
||||
getAllTools(): Array<{ serverId: string; serverName: string; tool: ToolMetadata }> {
|
||||
const result: Array<{ serverId: string; serverName: string; tool: ToolMetadata }> = []
|
||||
for (const [serverId, metadata] of this.metadataByServer.entries()) {
|
||||
for (const tool of metadata) {
|
||||
result.push({ serverId, serverName: serverId, tool })
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
clear(serverId: string): void {
|
||||
this.metadataByServer.delete(serverId)
|
||||
}
|
||||
|
||||
clearAll(): void {
|
||||
this.metadataByServer.clear()
|
||||
}
|
||||
|
||||
/** Find a tool by prefixed or original name, optionally within one server. */
|
||||
findTool(name: string, serverId?: string): { serverId: string; tool: ToolMetadata } | undefined {
|
||||
for (const [sid, metadata] of this.metadataByServer.entries()) {
|
||||
if (serverId && sid !== serverId) continue
|
||||
const found = metadata.find((tool) => tool.name === name || tool.originalName === name)
|
||||
if (found) return { serverId: sid, tool: found }
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
/** Ranked search across all (or one) server's tools. */
|
||||
searchTools(
|
||||
query: string,
|
||||
serverId?: string,
|
||||
serverNames: Map<string, string> = new Map(),
|
||||
limit = 12,
|
||||
offset = 0,
|
||||
): { items: RankedToolMatch[]; total: number; hasMore: boolean; nextOffset: number | null } {
|
||||
const matches: RankedToolMatch[] = []
|
||||
for (const [sid, metadata] of this.metadataByServer.entries()) {
|
||||
if (serverId && sid !== serverId) continue
|
||||
const serverName = serverNames.get(sid) ?? sid
|
||||
for (const tool of metadata) {
|
||||
const score = scoreToolMatch(tool, serverName, query)
|
||||
if (score !== null) matches.push({ server: sid, tool, score })
|
||||
}
|
||||
}
|
||||
matches.sort((a, b) => b.score - a.score || a.tool.name.localeCompare(b.tool.name))
|
||||
return paginate(matches, offset, limit)
|
||||
}
|
||||
|
||||
/** "Did you mean" suggestions for a tool name. */
|
||||
rankSuggestions(
|
||||
name: string,
|
||||
limit: number,
|
||||
serverNames: Map<string, string> = new Map(),
|
||||
): string[] {
|
||||
return this.searchTools(name, undefined, serverNames, limit, 0).items.map((m) => m.tool.name)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { toToolParameters, formatSchema } from './mcp-tool-schema'
|
||||
import { Compile } from 'typebox/compile'
|
||||
|
||||
describe('toToolParameters', () => {
|
||||
it('passes through a plain object schema', () => {
|
||||
const schema = {
|
||||
type: 'object',
|
||||
properties: { query: { type: 'string' } },
|
||||
required: ['query'],
|
||||
}
|
||||
const params = toToolParameters(schema)
|
||||
expect(params).toMatchObject({ type: 'object', properties: { query: { type: 'string' } } })
|
||||
// The resulting schema must compile with the same validator pi-ai uses.
|
||||
expect(() => Compile(params)).not.toThrow()
|
||||
})
|
||||
|
||||
it('strips $schema / $id / $comment keywords', () => {
|
||||
const params = toToolParameters({
|
||||
$schema: 'http://json-schema.org/draft-07/schema#',
|
||||
$id: 'https://example.com/tool.json',
|
||||
$comment: 'strip me',
|
||||
type: 'object',
|
||||
properties: { a: { type: 'string' } },
|
||||
})
|
||||
expect(JSON.stringify(params)).not.toContain('$schema')
|
||||
expect(JSON.stringify(params)).not.toContain('$id')
|
||||
expect(JSON.stringify(params)).not.toContain('$comment')
|
||||
})
|
||||
|
||||
it('supports $defs / $ref schemas', () => {
|
||||
const params = toToolParameters({
|
||||
type: 'object',
|
||||
properties: { q: { $ref: '#/$defs/query' } },
|
||||
$defs: { query: { type: 'string' } },
|
||||
required: ['q'],
|
||||
})
|
||||
const compiled = Compile(params)
|
||||
expect(compiled.Check({ q: 'hello' })).toBe(true)
|
||||
})
|
||||
|
||||
it('supports anyOf and enums', () => {
|
||||
const params = toToolParameters({
|
||||
type: 'object',
|
||||
properties: {
|
||||
value: { anyOf: [{ type: 'string' }, { type: 'number' }] },
|
||||
color: { type: 'string', enum: ['red', 'blue'] },
|
||||
},
|
||||
})
|
||||
const compiled = Compile(params)
|
||||
expect(compiled.Check({ value: 42, color: 'red' })).toBe(true)
|
||||
expect(compiled.Check({ value: 'x', color: 'green' })).toBe(false)
|
||||
})
|
||||
|
||||
it('supports additionalProperties and patternProperties', () => {
|
||||
const params = toToolParameters({
|
||||
type: 'object',
|
||||
properties: { a: { type: 'string' } },
|
||||
additionalProperties: false,
|
||||
})
|
||||
const compiled = Compile(params)
|
||||
expect(compiled.Check({ a: 'x' })).toBe(true)
|
||||
|
||||
const patterned = toToolParameters({
|
||||
type: 'object',
|
||||
patternProperties: { '^x-': { type: 'string' } },
|
||||
})
|
||||
expect(() => Compile(patterned)).not.toThrow()
|
||||
})
|
||||
|
||||
it('normalizes non-object roots to an object', () => {
|
||||
expect(toToolParameters({ type: 'string' })).toMatchObject({ type: 'object' })
|
||||
})
|
||||
|
||||
it('falls back to a permissive record for invalid schemas', () => {
|
||||
const params = toToolParameters({ type: 'object', properties: { bad: { type: 'not-a-type' } } })
|
||||
const compiled = Compile(params)
|
||||
// Fallback must accept arbitrary arguments.
|
||||
expect(compiled.Check({ anything: 123, nested: { x: 1 } })).toBe(true)
|
||||
})
|
||||
|
||||
it('handles undefined / empty input', () => {
|
||||
expect(toToolParameters(undefined)).toMatchObject({ type: 'object' })
|
||||
expect(toToolParameters(null)).toMatchObject({ type: 'object' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatSchema', () => {
|
||||
it('formats JSON Schema as indented text', () => {
|
||||
const text = formatSchema({ type: 'object', properties: { a: { type: 'string' } } })
|
||||
expect(text).toContain('"type": "object"')
|
||||
expect(text).toContain('"properties"')
|
||||
})
|
||||
|
||||
it('returns empty string for non-objects', () => {
|
||||
expect(formatSchema(undefined)).toBe('')
|
||||
expect(formatSchema(42)).toBe('')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* MCP tool schema conversion
|
||||
*
|
||||
* Normalizes an MCP tool's JSON Schema `inputSchema` into a TypeBox-compatible
|
||||
* `TSchema` that the pi-ai harness can validate and providers can serialize.
|
||||
*
|
||||
* - Ensures `type: 'object'` (MCP tool arguments are always objects).
|
||||
* - Strips keywords the harness does not understand (`$schema`, `$id`, `$comment`).
|
||||
* - Verifies the schema compiles with `typebox/compile`; if it does not, falls
|
||||
* back to a permissive `Record<string, unknown>` so the tool still works
|
||||
* (arguments pass through to `callTool`).
|
||||
*/
|
||||
|
||||
import { Compile } from 'typebox/compile'
|
||||
import { Type, type TSchema } from '@sinclair/typebox'
|
||||
|
||||
const STRIPPED_KEYS = new Set(['$schema', '$id', '$comment'])
|
||||
|
||||
function normalizeInputSchema(schema: unknown): Record<string, unknown> {
|
||||
if (!schema || typeof schema !== 'object' || Array.isArray(schema)) {
|
||||
return { type: 'object' }
|
||||
}
|
||||
const source = schema as Record<string, unknown>
|
||||
const result: Record<string, unknown> = {}
|
||||
for (const [key, value] of Object.entries(source)) {
|
||||
if (STRIPPED_KEYS.has(key)) continue
|
||||
if (value === undefined) continue
|
||||
result[key] = value
|
||||
}
|
||||
// MCP tool parameters are always a JSON object.
|
||||
if (result.type !== 'object' && result.type !== undefined) {
|
||||
result.type = 'object'
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an MCP `inputSchema` into an AgentTool `parameters` schema.
|
||||
*/
|
||||
export function toToolParameters(inputSchema: unknown): TSchema {
|
||||
const normalized = normalizeInputSchema(inputSchema)
|
||||
try {
|
||||
Compile(normalized)
|
||||
return normalized as TSchema
|
||||
} catch {
|
||||
// Fall back to a permissive object schema so the tool remains usable.
|
||||
return Type.Record(Type.String(), Type.Unknown())
|
||||
}
|
||||
}
|
||||
|
||||
/** Format a JSON Schema as indented text (used by proxy describe/search). */
|
||||
export function formatSchema(schema: unknown, indent = ''): string {
|
||||
if (!schema || typeof schema !== 'object') return ''
|
||||
try {
|
||||
return JSON.stringify(schema, null, 2)
|
||||
.split('\n')
|
||||
.map((line) => `${indent}${line}`)
|
||||
.join('\n')
|
||||
} catch {
|
||||
return String(schema)
|
||||
}
|
||||
}
|
||||
@@ -87,7 +87,7 @@ describe('MetadataService', () => {
|
||||
expect(projectFields[2].visible).toBe(false)
|
||||
})
|
||||
|
||||
it('AIAutofillAndDescription', async () => {
|
||||
it('AutofillSourceAndDescription', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'test-team' },
|
||||
})
|
||||
@@ -103,24 +103,22 @@ describe('MetadataService', () => {
|
||||
key: 'teamfield1',
|
||||
scope: 'TEAM',
|
||||
team: { connect: { id: team.id } },
|
||||
config: { name: 'Test Team Field', type: 'text' },
|
||||
aiAutofill: true,
|
||||
config: { name: 'Test Team Field', type: 'text', autofillSource: 'CONTENT' },
|
||||
description: desc,
|
||||
},
|
||||
})
|
||||
|
||||
const newDesc = 'Updated description'
|
||||
const updatedTeamField = await metadataService.updateTeamField(team.id, teamField.key, {
|
||||
config: { name: 'Test Team Field', type: 'text' },
|
||||
aiAutofill: false,
|
||||
config: { name: 'Test Team Field', type: 'text', autofillSource: 'NONE' },
|
||||
description: newDesc,
|
||||
})
|
||||
expect(updatedTeamField.aiAutofill).toBe(false)
|
||||
expect(updatedTeamField.config?.autofillSource).toBe('NONE')
|
||||
expect(updatedTeamField.description).toBe(newDesc)
|
||||
// IDOR test
|
||||
await expect(
|
||||
metadataService.updateTeamField('wrong-team-id', teamField.key, {
|
||||
config: { name: 'Hack', type: 'text' },
|
||||
config: { name: 'Hack', type: 'text', autofillSource: 'NONE' },
|
||||
}),
|
||||
).rejects.toThrow('Field does not belong to this team')
|
||||
})
|
||||
@@ -261,4 +259,294 @@ describe('MetadataService', () => {
|
||||
const multiVal = values.find((v) => v.fieldKey === userMultiField.key)
|
||||
expect(multiVal?.jsonValue).toEqual(multiUserIds)
|
||||
})
|
||||
|
||||
it('should dynamically create new options for select fields and store option id', async () => {
|
||||
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: 'test-asset',
|
||||
project: { connect: { id: project.id } },
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
},
|
||||
})
|
||||
|
||||
const selectField = await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'ai_provider',
|
||||
scope: 'PROJECT',
|
||||
project: { connect: { id: project.id } },
|
||||
config: {
|
||||
name: 'AI Provider',
|
||||
type: 'select',
|
||||
select: {
|
||||
options: [
|
||||
{ id: 'openai', displayName: 'OpenAI', color: '#f43f5e' },
|
||||
{ id: 'google', displayName: 'Google', color: '#3b82f6' },
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// 1. Add new option 'Kling AI'
|
||||
await metadataService.updateAssetMetadata(asset.id, [
|
||||
{ key: selectField.key, value: { newOption: { value: 'Kling AI' } } },
|
||||
])
|
||||
|
||||
// Verify option added to field config
|
||||
const updatedField = await prisma.metadataField.findUnique({
|
||||
where: { key: selectField.key },
|
||||
})
|
||||
const options = (updatedField?.config as PrismaJson.FieldConfig)?.select?.options || []
|
||||
expect(options).toHaveLength(3)
|
||||
const klingOpt = options.find((o) => o.displayName === 'Kling AI')
|
||||
expect(klingOpt).toBeDefined()
|
||||
expect(klingOpt?.id).toBe('kling-ai')
|
||||
expect(klingOpt?.color).toBeDefined()
|
||||
|
||||
// Verify asset metadata value stores the option ID
|
||||
const val = await prisma.assetMetadataValue.findUnique({
|
||||
where: {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
assetId_fieldKey: {
|
||||
assetId: asset.id,
|
||||
fieldKey: selectField.key,
|
||||
},
|
||||
},
|
||||
})
|
||||
expect(val?.stringValue).toBe('kling-ai')
|
||||
|
||||
// 2. Deduplication: passing 'kling ai' case-insensitively should reuse existing 'kling-ai'
|
||||
await metadataService.updateAssetMetadata(asset.id, [
|
||||
{ key: selectField.key, value: { newOption: { value: 'kling ai' } } },
|
||||
])
|
||||
const updatedField2 = await prisma.metadataField.findUnique({
|
||||
where: { key: selectField.key },
|
||||
})
|
||||
const options2 = (updatedField2?.config as PrismaJson.FieldConfig)?.select?.options || []
|
||||
expect(options2).toHaveLength(3) // Not 4!
|
||||
})
|
||||
|
||||
it('should dynamically create new options for selectMulti fields and store array of option ids', async () => {
|
||||
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: 'test-asset',
|
||||
project: { connect: { id: project.id } },
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
},
|
||||
})
|
||||
|
||||
const selectMultiField = await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'tags',
|
||||
scope: 'PROJECT',
|
||||
project: { connect: { id: project.id } },
|
||||
config: {
|
||||
name: 'Tags',
|
||||
type: 'selectMulti',
|
||||
selectMulti: {
|
||||
options: [{ id: 'tag1', displayName: 'Tag 1', color: '#f43f5e' }],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// Pass mixed existing and new option
|
||||
await metadataService.updateAssetMetadata(asset.id, [
|
||||
{
|
||||
key: selectMultiField.key,
|
||||
value: ['tag1', { newOption: { value: 'Tag 2' } }],
|
||||
},
|
||||
])
|
||||
|
||||
// Verify option added to field config
|
||||
const updatedField = await prisma.metadataField.findUnique({
|
||||
where: { key: selectMultiField.key },
|
||||
})
|
||||
const options = (updatedField?.config as PrismaJson.FieldConfig)?.selectMulti?.options || []
|
||||
expect(options).toHaveLength(2)
|
||||
const tag2Opt = options.find((o) => o.displayName === 'Tag 2')
|
||||
expect(tag2Opt).toBeDefined()
|
||||
expect(tag2Opt?.id).toBe('tag-2')
|
||||
|
||||
// Verify asset metadata value stores array of IDs
|
||||
const val = await prisma.assetMetadataValue.findUnique({
|
||||
where: {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
assetId_fieldKey: {
|
||||
assetId: asset.id,
|
||||
fieldKey: selectMultiField.key,
|
||||
},
|
||||
},
|
||||
})
|
||||
expect(val?.jsonValue).toEqual(['tag1', 'tag-2'])
|
||||
})
|
||||
|
||||
it('should reject invalid newOption value format', async () => {
|
||||
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: 'test-asset',
|
||||
project: { connect: { id: project.id } },
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
},
|
||||
})
|
||||
|
||||
const selectField = await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'field_opt',
|
||||
scope: 'PROJECT',
|
||||
project: { connect: { id: project.id } },
|
||||
config: { name: 'Field Opt', type: 'select' },
|
||||
},
|
||||
})
|
||||
|
||||
await expect(
|
||||
metadataService.updateAssetMetadata(asset.id, [
|
||||
{ key: selectField.key, value: { newOption: { value: ' ' } } },
|
||||
]),
|
||||
).rejects.toThrow('value must be a non-empty string')
|
||||
})
|
||||
|
||||
it('should handle ID collisions by appending numeric suffix', async () => {
|
||||
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: 'test-asset',
|
||||
project: { connect: { id: project.id } },
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
},
|
||||
})
|
||||
|
||||
const selectField = await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'collision_test',
|
||||
scope: 'PROJECT',
|
||||
project: { connect: { id: project.id } },
|
||||
config: {
|
||||
name: 'Collision Test',
|
||||
type: 'select',
|
||||
select: {
|
||||
options: [
|
||||
{ id: 'kling-ai', displayName: 'Old Kling AI', color: '#f43f5e' },
|
||||
{ id: 'kling-ai-1', displayName: 'Old Kling AI Suffix 1', color: '#3b82f6' },
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// Now adding a new option with different displayName whose slug collides with 'kling-ai'
|
||||
await metadataService.updateAssetMetadata(asset.id, [
|
||||
{ key: selectField.key, value: { newOption: { value: 'Kling AI Unique' } } },
|
||||
])
|
||||
|
||||
const updatedField = await prisma.metadataField.findUnique({
|
||||
where: { key: selectField.key },
|
||||
})
|
||||
const options = (updatedField?.config as PrismaJson.FieldConfig)?.select?.options || []
|
||||
const newOpt = options.find((o) => o.displayName === 'Kling AI Unique')
|
||||
expect(newOpt).toBeDefined()
|
||||
expect(newOpt?.id).toBe('kling-ai-unique')
|
||||
|
||||
// Test when baseId and candidateId-1 exist, next should be candidateId-2
|
||||
const collisionField = await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'collision_base',
|
||||
scope: 'PROJECT',
|
||||
project: { connect: { id: project.id } },
|
||||
config: {
|
||||
name: 'Collision Base',
|
||||
type: 'select',
|
||||
select: {
|
||||
options: [
|
||||
{ id: 'custom-tool', displayName: 'First Tool', color: '#f43f5e' },
|
||||
{ id: 'custom-tool-1', displayName: 'Second Tool', color: '#3b82f6' },
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// displayName is 'Custom Tool' which has baseId 'custom-tool' (not matching display names 'First Tool' / 'Second Tool')
|
||||
await metadataService.updateAssetMetadata(asset.id, [
|
||||
{ key: collisionField.key, value: { newOption: { value: 'Custom Tool' } } },
|
||||
])
|
||||
|
||||
const updatedCollisionField = await prisma.metadataField.findUnique({
|
||||
where: { key: collisionField.key },
|
||||
})
|
||||
const collisionOpts =
|
||||
(updatedCollisionField?.config as PrismaJson.FieldConfig)?.select?.options || []
|
||||
const resolvedNewOpt = collisionOpts.find((o) => o.displayName === 'Custom Tool')
|
||||
expect(resolvedNewOpt).toBeDefined()
|
||||
expect(resolvedNewOpt?.id).toBe('custom-tool-2')
|
||||
})
|
||||
|
||||
it('should reject non-string and non-newOption items in selectMulti array', async () => {
|
||||
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: 'test-asset',
|
||||
project: { connect: { id: project.id } },
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
},
|
||||
})
|
||||
|
||||
const selectMultiField = await prisma.metadataField.create({
|
||||
data: {
|
||||
key: 'tags_invalid_items',
|
||||
scope: 'PROJECT',
|
||||
project: { connect: { id: project.id } },
|
||||
config: {
|
||||
name: 'Tags',
|
||||
type: 'selectMulti',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// Passing invalid items like number or null
|
||||
await expect(
|
||||
metadataService.updateAssetMetadata(asset.id, [
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
{ key: selectMultiField.key, value: ['tag1', 123 as any] },
|
||||
]),
|
||||
).rejects.toThrow('expected string or {newOption}')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -10,6 +10,36 @@ import { ulid } from 'ulid'
|
||||
import { assetService } from '../asset/asset'
|
||||
import { systemFields } from './system_fields'
|
||||
|
||||
export const FIELD_COLORS = [
|
||||
'#f43f5e', // rose
|
||||
'#ec4899', // pink
|
||||
'#d946ef', // fuchsia
|
||||
'#a855f7', // purple
|
||||
'#8b5cf6', // violet
|
||||
'#6366f1', // indigo
|
||||
'#3b82f6', // blue
|
||||
'#0ea5e9', // sky
|
||||
'#06b6d4', // cyan
|
||||
'#14b8a6', // teal
|
||||
'#10b981', // emerald
|
||||
'#22c55e', // green
|
||||
'#84cc16', // lime
|
||||
'#eab308', // yellow
|
||||
'#f59e0b', // amber
|
||||
'#f97316', // orange
|
||||
'#71717a', // zinc
|
||||
'#64748b', // slate
|
||||
]
|
||||
|
||||
export function getNextOptionColor(existingOptions: PrismaJson.SelectOption[] = []): string {
|
||||
const usedColors = new Set(existingOptions.map((o) => o.color))
|
||||
const available = FIELD_COLORS.filter((c) => !usedColors.has(c))
|
||||
if (available.length > 0) {
|
||||
return available[0]
|
||||
}
|
||||
return FIELD_COLORS[existingOptions.length % FIELD_COLORS.length]
|
||||
}
|
||||
|
||||
export class MetadataService {
|
||||
constructor(private client: typeof prisma = prisma) {}
|
||||
async syncSystemFields(): Promise<void> {
|
||||
@@ -43,7 +73,6 @@ export class MetadataService {
|
||||
scope: 'TEAM',
|
||||
teamId: teamId,
|
||||
config: req.config,
|
||||
aiAutofill: req.aiAutofill,
|
||||
description: req.description,
|
||||
},
|
||||
})
|
||||
@@ -89,7 +118,6 @@ export class MetadataService {
|
||||
where: { id: field.id },
|
||||
data: {
|
||||
config: req.config,
|
||||
aiAutofill: req.aiAutofill ?? field.aiAutofill,
|
||||
description: req.description ?? field.description,
|
||||
},
|
||||
})
|
||||
@@ -125,7 +153,6 @@ export class MetadataService {
|
||||
projectId: projectId,
|
||||
teamId: p.teamId,
|
||||
config: req.config,
|
||||
aiAutofill: req.aiAutofill,
|
||||
description: req.description,
|
||||
},
|
||||
})
|
||||
@@ -239,7 +266,6 @@ export class MetadataService {
|
||||
where: { id: field.id },
|
||||
data: {
|
||||
config: req.config,
|
||||
aiAutofill: req.aiAutofill ?? field.aiAutofill,
|
||||
description: req.description ?? field.description,
|
||||
},
|
||||
})
|
||||
@@ -262,84 +288,211 @@ export class MetadataService {
|
||||
assetId: string,
|
||||
reqs: UpdateAssetMetadataRequest[],
|
||||
allowReadOnly = false,
|
||||
): Promise<void> {
|
||||
await this.client.$transaction(async (tx) => {
|
||||
await this.updateAssetMetadataInternal(tx, assetId, reqs, allowReadOnly)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link updateAssetMetadata} but runs inside the caller's transaction,
|
||||
* keeping the metadata write atomic with the surrounding changes.
|
||||
*/
|
||||
async updateAssetMetadataInTx(
|
||||
tx: Prisma.TransactionClient,
|
||||
assetId: string,
|
||||
reqs: UpdateAssetMetadataRequest[],
|
||||
allowReadOnly = false,
|
||||
): Promise<void> {
|
||||
await this.updateAssetMetadataInternal(tx, assetId, reqs, allowReadOnly)
|
||||
}
|
||||
|
||||
private async resolveOrCreateOption(
|
||||
client: Prisma.TransactionClient | typeof prisma,
|
||||
field: Prisma.MetadataFieldGetPayload<Record<string, never>>,
|
||||
rawOption: unknown,
|
||||
): Promise<string> {
|
||||
if (
|
||||
typeof rawOption !== 'object' ||
|
||||
rawOption === null ||
|
||||
!('newOption' in rawOption) ||
|
||||
typeof (rawOption as { newOption?: unknown }).newOption !== 'object' ||
|
||||
(rawOption as { newOption?: unknown }).newOption === null
|
||||
) {
|
||||
throw new Error(`Invalid newOption object for field ${field.key}`)
|
||||
}
|
||||
|
||||
const value = (rawOption as { newOption: { value?: unknown } }).newOption.value
|
||||
if (typeof value !== 'string' || !value.trim()) {
|
||||
throw new Error(
|
||||
`Invalid newOption value for field ${field.key}: value must be a non-empty string`,
|
||||
)
|
||||
}
|
||||
|
||||
const trimmedValue = value.trim()
|
||||
const config = (field.config ?? {}) as PrismaJson.FieldConfig
|
||||
const isMulti = config.type === 'selectMulti'
|
||||
const existingOptions: PrismaJson.SelectOption[] =
|
||||
(isMulti ? config.selectMulti?.options : config.select?.options) ?? []
|
||||
|
||||
// 1. Check for case-insensitive match against existing options (id or displayName)
|
||||
const existing = existingOptions.find(
|
||||
(opt) =>
|
||||
opt.id.toLowerCase() === trimmedValue.toLowerCase() ||
|
||||
opt.displayName.toLowerCase() === trimmedValue.toLowerCase(),
|
||||
)
|
||||
if (existing) {
|
||||
return existing.id
|
||||
}
|
||||
|
||||
// 2. Generate slug id
|
||||
let baseId = trimmedValue.toLowerCase().replace(/\s+/g, '-')
|
||||
if (!baseId) {
|
||||
baseId = ulid().toLowerCase()
|
||||
}
|
||||
|
||||
let candidateId = baseId
|
||||
let suffix = 1
|
||||
while (existingOptions.some((opt) => opt.id === candidateId)) {
|
||||
candidateId = `${baseId}-${suffix}`
|
||||
suffix++
|
||||
}
|
||||
|
||||
const newOption: PrismaJson.SelectOption = {
|
||||
id: candidateId,
|
||||
displayName: trimmedValue,
|
||||
color: getNextOptionColor(existingOptions),
|
||||
}
|
||||
|
||||
const updatedOptions = [...existingOptions, newOption]
|
||||
const updatedConfig: PrismaJson.FieldConfig = {
|
||||
...config,
|
||||
name: config.name ?? field.key,
|
||||
type: config.type ?? (isMulti ? 'selectMulti' : 'select'),
|
||||
...(isMulti
|
||||
? { selectMulti: { ...(config.selectMulti ?? {}), options: updatedOptions } }
|
||||
: { select: { ...(config.select ?? {}), options: updatedOptions } }),
|
||||
}
|
||||
|
||||
await client.metadataField.update({
|
||||
where: { key: field.key },
|
||||
data: { config: updatedConfig },
|
||||
})
|
||||
|
||||
// Update in-memory field.config for subsequent iterations in the same transaction
|
||||
field.config = updatedConfig
|
||||
|
||||
return candidateId
|
||||
}
|
||||
|
||||
private async updateAssetMetadataInternal(
|
||||
client: Prisma.TransactionClient | typeof prisma,
|
||||
assetId: string,
|
||||
reqs: UpdateAssetMetadataRequest[],
|
||||
allowReadOnly: boolean,
|
||||
): Promise<void> {
|
||||
const resolvedAssetId = await assetService.resolveLatestVersionId(assetId)
|
||||
|
||||
await this.client.$transaction(async (tx) => {
|
||||
for (const req of reqs) {
|
||||
const field = await tx.metadataField.findUnique({
|
||||
where: { key: req.key },
|
||||
})
|
||||
if (field?.readOnly && !allowReadOnly) {
|
||||
throw new Error(`Field ${field.key} is read-only`)
|
||||
}
|
||||
|
||||
const value = req.value
|
||||
let stringValue: string | null = null
|
||||
let numberValue: number | null = null
|
||||
let booleanValue: boolean | null = null
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let jsonValue: any = null
|
||||
let dateValue: Date | null = null
|
||||
|
||||
if (typeof value === 'string') {
|
||||
// Check if it's a valid ISO date string
|
||||
const date = new Date(value)
|
||||
if (!isNaN(date.getTime()) && value.includes('T') && value.includes('-')) {
|
||||
dateValue = date
|
||||
} else {
|
||||
stringValue = value
|
||||
}
|
||||
} else if (typeof value === 'number') {
|
||||
numberValue = value
|
||||
} else if (typeof value === 'boolean') {
|
||||
booleanValue = value
|
||||
} else if (value instanceof Date) {
|
||||
dateValue = value
|
||||
} else if (Array.isArray(value)) {
|
||||
jsonValue = value
|
||||
} else if (value === null) {
|
||||
// all null
|
||||
} else if (typeof value === 'object') {
|
||||
jsonValue = value
|
||||
} else {
|
||||
stringValue = String(value)
|
||||
}
|
||||
|
||||
await tx.assetMetadataValue.upsert({
|
||||
where: {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
assetId_fieldKey: {
|
||||
assetId: resolvedAssetId,
|
||||
fieldKey: req.key,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
assetId: resolvedAssetId,
|
||||
fieldKey: req.key,
|
||||
stringValue,
|
||||
numberValue,
|
||||
booleanValue,
|
||||
jsonValue,
|
||||
dateValue,
|
||||
},
|
||||
update: {
|
||||
stringValue,
|
||||
numberValue,
|
||||
booleanValue,
|
||||
jsonValue,
|
||||
dateValue,
|
||||
},
|
||||
})
|
||||
for (const req of reqs) {
|
||||
const field = await client.metadataField.findUnique({
|
||||
where: { key: req.key },
|
||||
})
|
||||
if (field?.readOnly && !allowReadOnly) {
|
||||
throw new Error(`Field ${field.key} is read-only`)
|
||||
}
|
||||
|
||||
// Touch asset
|
||||
await tx.asset.update({
|
||||
where: { id: resolvedAssetId },
|
||||
data: {
|
||||
updatedAt: new Date(),
|
||||
let value = req.value
|
||||
const config = field?.config as PrismaJson.FieldConfig | null
|
||||
|
||||
if (
|
||||
field &&
|
||||
config?.type === 'select' &&
|
||||
typeof value === 'object' &&
|
||||
value !== null &&
|
||||
'newOption' in value
|
||||
) {
|
||||
value = await this.resolveOrCreateOption(client, field, value)
|
||||
} else if (field && config?.type === 'selectMulti' && Array.isArray(value)) {
|
||||
const resolvedList: string[] = []
|
||||
for (const item of value) {
|
||||
if (typeof item === 'object' && item !== null && 'newOption' in item) {
|
||||
const resolvedId = await this.resolveOrCreateOption(client, field, item)
|
||||
resolvedList.push(resolvedId)
|
||||
} else if (typeof item === 'string') {
|
||||
resolvedList.push(item)
|
||||
} else {
|
||||
throw new Error(
|
||||
`Invalid option value for field ${field.key}: expected string or {newOption}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
value = resolvedList
|
||||
}
|
||||
|
||||
let stringValue: string | null = null
|
||||
let numberValue: number | null = null
|
||||
let booleanValue: boolean | null = null
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let jsonValue: any = null
|
||||
let dateValue: Date | null = null
|
||||
|
||||
if (typeof value === 'string') {
|
||||
// Check if it's a valid ISO date string
|
||||
const date = new Date(value)
|
||||
if (!isNaN(date.getTime()) && value.includes('T') && value.includes('-')) {
|
||||
dateValue = date
|
||||
} else {
|
||||
stringValue = value
|
||||
}
|
||||
} else if (typeof value === 'number') {
|
||||
numberValue = value
|
||||
} else if (typeof value === 'boolean') {
|
||||
booleanValue = value
|
||||
} else if (value instanceof Date) {
|
||||
dateValue = value
|
||||
} else if (Array.isArray(value)) {
|
||||
jsonValue = value
|
||||
} else if (value === null) {
|
||||
// all null
|
||||
} else if (typeof value === 'object') {
|
||||
jsonValue = value
|
||||
} else {
|
||||
stringValue = String(value)
|
||||
}
|
||||
|
||||
await client.assetMetadataValue.upsert({
|
||||
where: {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
assetId_fieldKey: {
|
||||
assetId: resolvedAssetId,
|
||||
fieldKey: req.key,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
assetId: resolvedAssetId,
|
||||
fieldKey: req.key,
|
||||
stringValue,
|
||||
numberValue,
|
||||
booleanValue,
|
||||
jsonValue,
|
||||
dateValue,
|
||||
},
|
||||
update: {
|
||||
stringValue,
|
||||
numberValue,
|
||||
booleanValue,
|
||||
jsonValue,
|
||||
dateValue,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Touch asset
|
||||
await client.asset.update({
|
||||
where: { id: resolvedAssetId },
|
||||
data: {
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { LocalStorageService, S3StorageService } from './s3'
|
||||
import { buildContentDisposition, LocalStorageService, S3StorageService } from './s3'
|
||||
|
||||
// Mock Bun S3Client
|
||||
const s3ClientConstructorSpy = vi.fn()
|
||||
@@ -140,6 +140,32 @@ describe('S3Service implementations', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('should set contentDisposition with the filename when download filename is provided', async () => {
|
||||
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
||||
await s3.presign('bucket', 'key', 'GET', true, 'foo.png')
|
||||
|
||||
expect(s3PresignSpy).toHaveBeenCalledWith(
|
||||
'key',
|
||||
expect.objectContaining({
|
||||
contentDisposition: 'attachment; filename="foo.png"; filename*=UTF-8\'\'foo.png',
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should include an RFC 5987 filename* for non-ASCII filenames', async () => {
|
||||
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
||||
await s3.presign('bucket', 'key', 'GET', true, '报告.png')
|
||||
|
||||
expect(s3PresignSpy).toHaveBeenCalledWith(
|
||||
'key',
|
||||
expect.objectContaining({
|
||||
// Non-ASCII names are conveyed only via filename* (percent-encoded),
|
||||
// keeping the header value pure ASCII (RFC 7230).
|
||||
contentDisposition: "attachment; filename*=UTF-8''%E6%8A%A5%E5%91%8A.png",
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should not cache download presign URLs', async () => {
|
||||
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
||||
|
||||
@@ -248,6 +274,18 @@ describe('S3Service implementations', () => {
|
||||
expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt?download=1')
|
||||
})
|
||||
|
||||
it('should include an encoded filename in the local download URL', async () => {
|
||||
const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', true, 'foo bar.png')
|
||||
expect(url).toBe(
|
||||
'http://localhost:3000/files/my-bucket/dir/file.txt?download=1&filename=foo%20bar.png',
|
||||
)
|
||||
})
|
||||
|
||||
it('should not append a filename param when none is provided', async () => {
|
||||
const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', true)
|
||||
expect(url).not.toContain('filename=')
|
||||
})
|
||||
|
||||
it('should throw an error for unsupported presign methods', async () => {
|
||||
await expect(localS3.presign('b', 'k', 'POST')).rejects.toThrow()
|
||||
})
|
||||
@@ -287,4 +325,46 @@ describe('S3Service implementations', () => {
|
||||
expect(url).not.toContain('4567')
|
||||
})
|
||||
})
|
||||
|
||||
describe('buildContentDisposition', () => {
|
||||
it('returns plain attachment when no filename is provided', () => {
|
||||
expect(buildContentDisposition()).toBe('attachment')
|
||||
expect(buildContentDisposition('')).toBe('attachment')
|
||||
expect(buildContentDisposition(null)).toBe('attachment')
|
||||
})
|
||||
|
||||
it('builds a disposition with quoted filename and RFC 5987 filename*', () => {
|
||||
expect(buildContentDisposition('foo.png')).toBe(
|
||||
'attachment; filename="foo.png"; filename*=UTF-8\'\'foo.png',
|
||||
)
|
||||
})
|
||||
|
||||
it('strips control characters and quotes to prevent header injection', () => {
|
||||
expect(buildContentDisposition('a\r\nb"c\\d')).toBe(
|
||||
'attachment; filename="abcd"; filename*=UTF-8\'\'abcd',
|
||||
)
|
||||
})
|
||||
|
||||
it('percent-encodes reserved RFC 5987 characters', () => {
|
||||
expect(buildContentDisposition("it's (final).png")).toBe(
|
||||
"attachment; filename=\"it's (final).png\"; filename*=UTF-8''it%27s%20%28final%29.png",
|
||||
)
|
||||
})
|
||||
|
||||
it('percent-encodes the * character in filename*', () => {
|
||||
expect(buildContentDisposition('foo*bar.png')).toBe(
|
||||
'attachment; filename="foo*bar.png"; filename*=UTF-8\'\'foo%2Abar.png',
|
||||
)
|
||||
})
|
||||
|
||||
it('omits the quoted filename fallback for non-ASCII names to keep the header ASCII', () => {
|
||||
expect(buildContentDisposition('幻境边界.pptx')).toBe(
|
||||
"attachment; filename*=UTF-8''%E5%B9%BB%E5%A2%83%E8%BE%B9%E7%95%8C.pptx",
|
||||
)
|
||||
// The whole header value must remain pure ASCII so Bun/Node Headers accept it
|
||||
expect(
|
||||
[...buildContentDisposition('幻境边界.pptx')].every((ch) => ch.charCodeAt(0) <= 0x7f),
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user