Compare commits

...
25 Commits
Author SHA1 Message Date
yiling f0bdccf713 Release v0.6.2 2026-10-29 20:29:50 +08:00
yiling 5b7fc04b02 docs: document MCP OAuth support and simplify unreleased changelog 2026-10-29 17:52:45 +08:00
Yiling-J 687b5a79d8 fix(core): strip trailing slashes when normalizing auth and MCP resource URLs (#506)
* fix(core): strip trailing slashes when normalizing auth and MCP resource URLs

* docs: document BETTER_AUTH_URL in README and quickstart guides

* docs: prioritize AWS_ENDPOINT_URL_S3 and clarify applicability to local storage

* docs: refine AWS_ENDPOINT_URL_S3 guidance for local storage deployments
2026-10-29 17:24:31 +08:00
Yiling-J baf30730af feat(mcp): add OAuth 2.1 support (#505)
* feat(mcp): add OAuth 2.1 support with dynamic client registration

* feat(api): alias bare oauth-authorization-server discovery endpoint

* fix(auth): preserve user session on OAuth sign-in redirect to consent
2026-10-29 15:39:37 +08:00
yiling 96539207d0 update readme 2026-10-28 11:41:28 +08:00
yiling 43f95a2fa2 update cn compose 2026-10-28 11:38:07 +08:00
yiling 0f92a5377f update readme 2026-10-28 10:30:13 +08:00
Yiling-J 4e3e17a547 docs: simplify AGENTS.md developer guide (#501) 2026-10-27 15:26:42 +08:00
Yiling-J b3cae4f2b0 feat: move MCP to /mcp and add teammate operations (#500)
* feat(operations): move MCP to /mcp and add search, comment, metadata, version, and upload operations

- Serve the MCP server at /mcp instead of /api/mcp
- Add search_assets, copy_assets, get_folder_instructions, list_comments,
  create_comment, resolve_comment, list_metadata_fields,
  update_asset_metadata, create_version_stack, list_versions, and
  remove_version on the agent, MCP, and CLI
- Add create_file and create_version (agent, MCP) with Creation Context
  metadata, and complete_upload (MCP) for presigned two-step uploads
- Replace the native agent create_file/create_version tools with
  Operation-backed ones
- Route the comment-complete, metadata, copy, version stack, and
  AGENTS.md read REST endpoints through their Operations

* docs(readme): add built-in MCP server and CLI to features list
2026-10-27 14:37:26 +08:00
Yiling-J 50fbd97953 feat: expose read_asset and download_asset on MCP and the CLI (#499)
* feat(asset-read): expose read_asset on MCP and the CLI

Enable the mcp and cli surfaces for read_asset and get_asset_render.

- MCP: add per-Operation tool extensions. The read presenter attaches up
  to 8 images (2 MB each) inline, lists image links, supports
  inlineImages: false, and tells the model to call get_asset_render when
  a render is pending. Storage keys are stripped from the result.
- CLI: generated commands can flatten object inputs into prefixed flags
  (cli.flatten) and omit fields (cli.omit); add per-command extensions.
  New `read` and `render` commands print image links, download them with
  --out, and support --no-wait with a printed `render <taskId>` hint.
- Round frame timestamps in labels to 3 decimals.

* fix(mcp): make tool input schemas portable to single-type dialects

Nullable inputs were emitted as `type: ["number", "null"]` (or a `null`
anyOf branch), which clients that map tool schemas onto an OpenAPI-style
subset (e.g. Gemini function declarations) may reject.

- Make read_asset's startPage, endPage, and degree optional instead of
  nullable.
- Move null handling into a shared dropNullOptionals helper in
  @shumai/operations that also strips nulls inside nested config
  objects. The agent uses it, and the MCP server applies it before the
  SDK validates arguments, so models that send null keep working.
- Test that no Operation input schema (and no listed MCP tool) uses a
  nullable type.

* feat(asset-download): share download_asset across agent, MCP and CLI

Turn the agent-only download_asset tool into an Operation on all three
surfaces.

- download_asset takes up to 50 file or version stack IDs and returns
  presigned links. proxy (default true) picks the web preview when there
  is one, as the agent always did; proxy: false returns the original.
  Folders are rejected. Read is checked on every ID before anything runs.
- Agent: an extension saves the files into the .pi sandbox and keeps the
  agent-only storage key mode (e.g. frames from read_asset). Agent tool
  extensions gain description, parameterOverrides and intercept.
- CLI: `shumai-cli download <assetId...> [--out <dir>] [--original]`
  streams files to disk without overwriting existing names.
- read_asset's unsupported-type error now names the right way to
  download on each surface (download_asset, or `shumai-cli download`).
- Add the Download Asset toggle to agent settings.
2026-10-27 08:09:11 +08:00
Yiling-J 155fb95a0e feat(asset-read): rewrite read_asset as a reader registry and Operations (#498)
Replace the monolithic agent read_asset tool with a reader registry in
@shumai/core/src/asset-read (model3d, image, video, document) behind a
shared pipeline that handles access checks, version stacks, annotations,
render tasks, and waiting.

- Add read_asset and get_asset_render Operations (agent surface only).
  Renders that outlast the caller's wait budget return "pending" with the
  workflow task ID; the read request is stored on the task payload so
  get_asset_render works across restarts and instances (requester only).
- Add workflowService.waitFor and presign expiry support.
- Keep agent output compatible via an agent tool extension (s3KeyOnly,
  image blocks, session image details); autofill keeps its team-scoped tool.
- Cap video frames at 20 per call; add the get_asset_render agent toggle.
2026-10-27 08:09:10 +08:00
Yiling-J e44938cb8f feat(cli): generate shumai-cli commands from Operations with commander (#496)
Rebuild the CLI on commander. Commands for every cli-surface Operation
(project ls, ls, mkdir, rename, move/mv, delete/rm) are generated from
@shumai/operations: positionals, options, choices, numeric parsing and
help text come from the Zod input schema. They call POST /api/ops/:name
with x-shumai-client: cli.

Existing invocations keep working. Adds --json, ls --type/--first/
--after, typo suggestions, and generated help. upload and create-version
stay hand-written. Drops the unused @shumai/core dependency.
2026-10-27 08:09:10 +08:00
Yiling-J 4d61786799 feat(mcp): serve Operations over MCP and a generic /api/ops endpoint (#495)
Add a stateless Streamable HTTP MCP server at /api/mcp that exposes every
mcp-surface Operation with its input/output schemas and annotations, and
POST /api/ops/:name for the CLI. Both require an API token (session
cookies are rejected), run Operations as the token's user, honor demo
read-only mode, and record via mcp/cli/api in audit logs.

Also make list_projects toggleable in agent settings and document MCP
client setup.
2026-10-27 08:09:09 +08:00
Yiling-J 76057ccd21 feat(operations): add shared Operation layer for agent capabilities (#494)
Introduce @shumai/operations (pure definitions: Zod schemas, metadata,
CLI hints) and @shumai/core/src/operations (implementOperation and the
runOperation pipeline: validate, authorize, prepare, execute, validate
output, audit).

Migrate list_assets, create_folder, rename_asset, move_assets and
delete_asset to Operations, and add list_projects. The agent now builds
these tools from the registry, and the folder create/rename, file
rename and reparent routes call the same Operations.

Audit logs gain a nullable via column recording the surface. A lint
rule keeps the Prisma client out of Operation implementations.
2026-10-27 08:09:09 +08:00
Yiling-J 47ee794ae4 fix(agent): enforce access checks in read_thread and before file uploads (#493)
read_thread returned any comment thread by ID. It now requires Read on
the thread's asset for the acting user, and confines system agents
without a user to their own team.

create_file and create_version uploaded to storage before checking Edit
permission, leaving orphaned objects on denied calls. The permission
check now runs first.
2026-10-26 11:14:37 +08:00
Yiling-J 22486b893b feat(api-token): store API tokens as SHA-256 hashes (#492)
New tokens are 32 random bytes with a shumai_ prefix and are returned
only once at creation. The list endpoint returns a 4-character hint
instead of the plaintext. A migration hashes existing tokens in place so
they keep authenticating.
2026-10-26 11:08:46 +08:00
yiling c2af7494ec Release v0.6.1 2026-10-24 13:21:50 +08:00
yiling b7d2748b00 docs(changelog): add bun 1.4.4 upgrade to unreleased changes 2026-10-24 12:02:22 +08:00
Yiling-J 3467c0d92b chore(docker): update Aliyun compose images and document transcode worker scaling in README_CN (#491) 2026-10-24 11:51:37 +08:00
Yiling-J 22fede0290 fix(webui): prevent clipping of long text overlay and date picker in cards (#490)
* fix(webui): prevent clipping of long text overlay and date picker in cards

* docs: update DeepWiki badge url in README and README_CN

* fix(webui): preserve expanded overlay height and textarea style in LongTextField

* fix(webui): add bottom padding to file list to prevent screen clipping of expanded fields

* fix(webui): expand directly into edit mode on single click for LongTextField

* fix(webui): prevent file selection when interacting with metadata fields in cards and list rows
2026-10-24 11:40:47 +08:00
Yiling-J d87fe43b0f feat(dashboard): add workflow tasks monitoring and stopping to team dashboard (#489)
* feat(dashboard): add workflow tasks monitoring and stopping to team dashboard

* fix(asset): retain workflow tasks on asset purge and mark active tasks as failed

* style(webui): add edge padding to audit logs and workflow tasks tables

* fix(workflow): atomically update asset status to failed when task is stopped

* fix(workflow): propagate cancellation errors, guard concurrent task stop, and localize UI

* docs(changelog): keep only added log for workflow tasks dashboard

* test(provider): mock upstream catalog api calls using saved fixtures

* feat(dashboard): show file name, size, and project name in workflow tasks list
2026-10-23 16:44:52 +08:00
Yiling-J 7a0ffca629 Add feedback invitation to README
Added a feedback section to encourage user engagement.
2026-10-22 20:39:56 +08:00
Yiling-J 5cf6d31794 chore(deps): upgrade bun to 1.4.4 and migrate to bun fmt (#488)
- Upgrade Bun to 1.4.4 across Dockerfiles, CI workflows, and release configurations.
- Update @types/bun and bun-types.
- Remove eslint-plugin-prettier and update format script to use native bun fmt.
- Configure .prettierignore to exclude packages/webui/public and minified files.
2026-10-22 20:25:54 +08:00
Yiling-J 0d5fe48a3a docs(docker): add guide for scaling transcoding workers horizontally (#486) 2026-10-21 12:45:09 +08:00
Yiling-J 19eed521ae fix(webui): prevent unwanted redirects to login on reset-password page (#487)
* fix(webui): allow reset-password route with token when stale auth exists

* fix(webui): prevent 401 redirect to login on reset-password page
2026-10-21 12:12:25 +08:00
303 changed files with 38546 additions and 6862 deletions
+4 -4
View File
@@ -24,7 +24,7 @@ jobs:
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.4.2
bun-version: 1.4.4
- name: Install dependencies
run: bun install
@@ -81,7 +81,7 @@ jobs:
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.4.2
bun-version: 1.4.4
- name: Install dependencies
run: bun install
@@ -126,7 +126,7 @@ jobs:
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.4.2
bun-version: 1.4.4
- name: Install dependencies
run: bun install
@@ -163,7 +163,7 @@ jobs:
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.4.2
bun-version: 1.4.4
- name: Install dependencies
run: bun install
+1 -1
View File
@@ -36,7 +36,7 @@ jobs:
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.4.2
bun-version: 1.4.4
- name: Setup Node.js
uses: actions/setup-node@v6
+3
View File
@@ -9,3 +9,6 @@ packages/webui/paraglide
*.md
**/*.md
docs/changelog.mdx
packages/webui/public
**/*.min.*
+67 -424
View File
@@ -10,14 +10,11 @@ Before starting any task, create and switch to a new feature branch:
git checkout -b <branch-name>
```
Complete the task on that branch.
After finishing the work and verifying that all checks pass:
1. Update `CHANGELOG.md` under `## [Unreleased]` for user-facing changes (see [Changelog Guidelines](#changelog-guidelines)).
2. Stage and commit your changes.
3. Push the branch to `origin`.
4. Open a pull request using the GitHub CLI.
2. Stage and commit your changes following [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/#summary) (`<type>[optional scope]: <description>` in imperative mood, e.g., `feat(auth): add OAuth login support`, `fix:`, `docs:`, `refactor:`, `test:`, `chore:`, and `!` or `BREAKING CHANGE:` for breaking changes).
3. Push the branch to `origin` and open a pull request using the GitHub CLI:
```bash
git add .
@@ -26,55 +23,15 @@ git push origin <branch-name>
gh pr create --title "<pull-request-title>" --body "<pull-request-body>"
```
---
## Commit Messages
We follow the Conventional Commits specification.
Commit messages MUST be formatted as:
```text
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
```
Example:
```text
feat(auth): add OAuth login support
```
The commit contains the following structural elements, to communicate intent to the consumers of your library:
1. `fix:` a commit of the type `fix` patches a bug in your codebase.
2. `feat:` a commit of the type `feat` introduces a new feature to the codebase.
3. `BREAKING CHANGE:` a footer or `!` after the type/scope introduces a breaking API change.
4. Additional types are allowed (for example: `docs:`, `refactor:`, `test:`, `chore:`).
Write commit messages in the imperative mood and keep descriptions concise and specific.
Reference:
https://www.conventionalcommits.org/en/v1.0.0/#summary
---
## Changelog Guidelines
We maintain `CHANGELOG.md` following [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
We maintain `CHANGELOG.md` following [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). Before opening a pull request, add an entry under `## [Unreleased]` for any user-facing changes:
Before opening a pull request, add an entry under `## [Unreleased]` for any user-facing changes:
- **End-User Friendly**: Make entries easy to understand for end users. For new features, explain what they actually do in plain language without heavy technical jargon. For bug fixes, describe what the bug was in a friendly, relatable way rather than focusing on internal code mechanics, file paths, or private symbols.
- **End-User Friendly**: Write in plain language for end users without heavy technical jargon, internal file paths, or private symbols.
- **Formatting**: `- **<scope>**: <Description>` (e.g. `- **asset**: Fix an issue where moving a video to trash while it was still transcoding could cause it to lose its trashed status`).
- **No PR Numbers**: Do not include PR numbers or links; the release pipeline automatically appends the detailed PR list.
- **No PR Numbers**: Do not include PR numbers or links; the release pipeline appends them automatically.
- **Internal Changes**: Routine refactors, test additions, or internal chores without external behavior changes may omit changelog updates.
---
## Pull Request Template
```md
@@ -106,7 +63,7 @@ Add any additional context, caveats, or follow-up work.
## Submission Rules
- **Strict Requirement**: A submission is considered complete **only** when there is a single final code state in which **all** of the following pass **simultaneously**:
- **Strict Requirement**: A submission is complete **only** when there is a single final code state in which **all** of the following pass **simultaneously** (do not submit intermediate states where some checks fail):
- `bun run lint`
- `bun run format`
- `bun run typecheck`
@@ -114,403 +71,89 @@ Add any additional context, caveats, or follow-up work.
- `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.
- Fixes must be iterated until **no check causes any other check to fail**.
- **Type Safety**: The use of explicit `any` is strictly forbidden and will result in lint errors. You should use `unknown` instead when the type is not known. If you absolutely must use `any` due to a limitation (e.g. interacting with an untyped 3rd party library), you must add an eslint-disable comment (e.g., `// eslint-disable-next-line @typescript-eslint/no-explicit-any`) and include a comment directly above it clearly explaining _why_ we cannot be type-safe here.
- Do **not** submit intermediate states where some checks pass and others fail, even temporarily.
- **Cleanup Requirement**: Remove all verification related files (scripts, screenshots, `verification/` folder) before submit.
## Backend Architecture
The backend is built with:
- **Runtime**: Bun
- **Framework**: Hono
- **ORM**: Prisma (with Pgvector18)
- **Database**: Pgvector18
- **Backend Testing Mandate**: Every backend feature, service method, workflow, and activity MUST have comprehensive tests.
- **Type Safety**: Explicit `any` is strictly forbidden. Use `unknown` when the type is unknown. If `any` is unavoidable (e.g., untyped 3rd-party library), add `// eslint-disable-next-line @typescript-eslint/no-explicit-any` with a comment directly above explaining why.
- **Cleanup Requirement**: Remove all temporary verification files, scripts, screenshots, and the `verification/` folder before submitting.
## Workspace Architecture
The project is a monorepo managed by **Bun Workspaces**. It follows a strictly decoupled architecture where each domain or layer is its own package:
The project is a Bun Workspaces monorepo (Bun runtime, Hono API, Prisma + Pgvector18 DB, React + Tailwind CSS + Shadcn UI frontend):
- **WebUI (`packages/webui`)**: React-based frontend.
- **API (`packages/api`)**: Hono-based HTTP entry point. Handles requests and calls Core services.
- **Core (`packages/core`)**: Business logic, services, and infrastructure utilities.
- **Database (`packages/db`)**: Prisma client, schema, and migrations.
- **DTOs (`packages/dtos`)**: Shared type definitions and Zod schemas used by both API and WebUI.
- **Workers**: Specialized packages for background task execution:
- `@shumai/workflow-core`: Common workflow engine logic.
- `@shumai/agent`: AI agent workflows and activities.
- `@shumai/transcode`: Media processing workflows and activities.
- **`packages/webui`**: React frontend (`bun run dev` runs on port 3000).
- **`packages/api`**: Hono HTTP entry point and MCP server.
- **`packages/core`**: Business logic, services, operations, and asset readers.
- **`packages/db`**: Prisma client, schema, and migrations.
- **`packages/dtos`**: Shared Zod schemas and TypeScript types (do not append `Dto` suffix to type names).
- **`packages/operations`**: Shared Operation definitions (`@shumai/operations`) used by agent tools, MCP, and `apps/cli`.
- **Workers**: `@shumai/workflow-core` (DBOS engine), `@shumai/agent` (AI agent workflows/activities), and `@shumai/transcode` (media workflows/activities).
### Layered Communication Rules
### Layered Communication & Code Patterns
1. **API Layer** calls **Core Layer**. Do not access the database directly in the API layer.
2. **Core Layer** calls **Database Layer** and other Core services.
3. **DTO Layer** is imported by all layers to ensure end-to-end type safety.
4. **No Direct DB Leak**: Do not return Prisma objects directly from the API; always map them to DTOs.
1. **API → Core → DB**: API routes (`packages/api/src/`) call Core services (`packages/core/src/`), which call `@shumai/db`. Never access `prisma` directly in the API layer, and never return raw Prisma objects from API routes (always map to DTOs).
2. **Services**: Define service logic as instance methods on a class and export a singleton instance (check existing services in `packages/core/src/`).
3. **Hono Routes**: Always chain `.get()` / `.post()` on a Hono instance using `zValidator` and export the chained route as default to preserve Hono RPC type inference (check `packages/api/src/`).
4. **Operations First**: If an Operation exists for an action, API routes must call it via `runOperation` instead of re-implementing permission checks and audit logging. UI-only routes keep calling services directly.
5. **Dependencies & Imports**:
- Declare runtime `dependencies` in each package's local `package.json` (`workspace:*` for internal packages). Keep root `package.json` strictly for shared `devDependencies` and scripts.
- Never edit `package.json` manually to add dependencies; run `bun add <package> --filter <workspace>` from the root without an explicit version.
- Use absolute workspace imports across packages (e.g., `@shumai/db`, `@shumai/core/src/...`). When adding a new package, add its path mappings to the root `tsconfig.json`.
## Dependency Management
## Operations & Asset Readers
We follow Bun's monorepo conventions for dependency management:
### Operations
1. **Self-Contained Packages**: Every workspace package MUST declare its own runtime `dependencies` in its local `package.json`. Do not rely on dependencies being available via the root.
2. **Shared DevDependencies**: Common development tools (e.g., `typescript`, `eslint`, `vitest`, `prettier`, `prisma`) MUST be declared in the root `package.json` to ensure version consistency across the workspace.
3. **Local DevDependencies**: Tools specific to a single package (e.g., `@vitejs/plugin-react` for `webui`) should be declared in that package's local `package.json`.
4. **Workspace Imports**: Use `workspace:*` for internal package dependencies.
5. **Clean Root**: The root `package.json` must not contain runtime `dependencies`. It is reserved for shared `devDependencies` and workspace-wide `scripts`.
6. **Adding Packages**: Never edit `package.json` manually to add new dependencies. Always use `bun add` (e.g., `bun add <package> --filter <workspace>`) from the root without specifying an explicit version, letting Bun resolve the correct version.
An **Operation** is defined once and exposed across built-in agent tools, the MCP server, and `shumai-cli`. Check existing definitions in `packages/operations/src/` and implementations in `packages/core/src/operations/`.
## TypeScript Configuration
- **Definition (`packages/operations/src/**`)**: Keep free of server dependencies (no `@shumai/core` or `@shumai/db`). Every Zod input/output field must have `.describe()`. Object input fields must be listed in `cli.flatten` or `cli.omit`.
- **Implementation (`packages/core/src/operations/**`)**: Use `implementOperation(definition, { authz, prepare?, execute, audit? })`.
- `authz` returns all required permissions (checked before any side effect).
- `execute` calls Core services only (**importing `prisma` in Operations is forbidden by lint**).
- `audit` resolves team/project from the affected asset, never trusting caller input.
- Outputs must be DTOs matching the output schema.
- **Register & Run**: Register in both `packages/operations/src/registry.ts` and `packages/core/src/operations/registry.ts`. Execute only via `runOperation(op, { userId, via, agentId? }, input)`.
- **Surface Extensions**: For surface-specific parameters or rendering, check `packages/agent/src/operation-extensions/`, `packages/api/src/mcp-extensions/`, and `apps/cli/src/operations/extensions/`.
We use a root-level `tsconfig.json` to manage path mappings for the entire workspace. This ensures that imports across packages resolve correctly during typechecking and in the IDE.
### Asset Readers
1. **Path Mappings**: All workspace packages MUST have an entry in the `paths` object in the root `tsconfig.json`.
```json
"paths": {
"@shumai/core": ["packages/core/src/index.ts"],
"@shumai/core/src/*": ["packages/core/src/*"]
}
```
2. **Package tsconfig**: Each package should have its own `tsconfig.json` that extends the root configuration and defines its specific `include`/`exclude` rules.
3. **Cross-Package Resolution**: When adding a new package, you MUST update the root `tsconfig.json` path mappings to ensure it can be imported by other packages.
`read_asset` is backed by readers in `packages/core/src/asset-read/readers/` registered in `packages/core/src/asset-read/registry.ts` (order matters: first matching reader wins). To add or modify a reader, check existing readers in `packages/core/src/asset-read/readers/` and option schemas in `packages/operations/src/asset/read-asset.ts`. Use `badRequest(...)` for invalid input and return storage keys in `ReadResult`.
### Service Layer Patterns
## Frontend Rules (`packages/webui`)
- **Instance Methods**: Define service logic as instance methods on a class, not static methods.
- **Singleton Export**: Export a singleton instance of the service class.
```typescript
export class MyService {
async doSomething() { ... }
}
export const myService = new MyService()
```
- **Usage**: Import and use the exported instance.
```typescript
import { myService } from '@shumai/core/src/services/myService'
await myService.doSomething()
```
- **System Colors**: Prefer semantic design tokens (`bg-background`, `text-foreground`, `text-muted-foreground`, etc.) over hardcoded Tailwind colors for light/dark theme support.
- **Route Code-Splitting**: Split all routes into `[route].tsx` (route definition and loaders) and `[route].lazy.tsx` (UI component and heavy imports).
- **Radix / Shadcn UI**: Set `modal={false}` on `<DropdownMenu>` when triggering a `Dialog` or `AlertDialog` from inside it to prevent focus-trap UI freezes. Use `ScrollArea` (`@/ui/components/ui/scroll-area`) for scrollable containers.
- **Pagination & Infinite Scroll**: Use `useInfiniteQuery` + `react-intersection-observer` with default `limit: 20` and the API's `total` field. Backend list APIs use `paginateQuery` from `packages/core/src/pagination.ts` (default `limit: 20`).
- **Internationalization (i18n)**: Hardcoded UI text is strictly forbidden. Add `snake_case` keys to both `packages/webui/messages/en.json` and `packages/webui/messages/zh.json`, run `bun run i18n:compile` (or `bun run i18n:watch`), and import `m` from `@/ui/paraglide/messages.js`.
### Hono API Definitions
## Database & Prisma (`packages/db`)
- **Route Chaining**: Always define API routes using method chaining on a Hono instance. This ensures that the type definitions for inputs and outputs are correctly inferred and preserved in the exported type.
- **Export Pattern**: Export the chained route instance as the default export.
```typescript
const app = new Hono()
const route = app
.get('/', ...)
.post('/', ...)
export default route
```
- **Type Safety**: Use `zValidator` for request validation (query, json, form) to ensure full end-to-end type safety with the Hono RPC client.
- **Schema & Config**: Schema at `packages/db/prisma/schema.prisma`, config at `prisma.config.ts`, generated client at `packages/db/src/generated/prisma`.
- **ULIDs & Sorting**: All models (except key-value stores like `Setting`) must use `@default(ulid())`. Do not manually generate DB IDs in application code. Order all list queries by `orderBy: { id: 'desc' }` (do not sort by unindexed `createdAt`).
- **Strict JSON Typing**: Annotate `Json` columns with `/// [TypeName]` in `schema.prisma` and define the type under the global `PrismaJson` namespace in `packages/db/src/prisma-json-types.ts`. Use type guards/narrowing instead of `as any`.
- **Migrations**:
- Commands: `bun --bun run prisma migrate dev`, `bun --bun run prisma migrate deploy`, `bun --bun run prisma generate`.
- Never hand-write DDL migration files; always generate them via `prisma migrate dev` (use `--create-only` for custom data migration SQL).
- **Never run `prisma migrate reset --force`** automatically. If migrations are out of sync or require a reset, stop and ask the user.
## Frontend Architecture
## DBOS Workflows & Activities
The frontend is built with:
Check existing workflows and activities in `packages/agent/src/{workflows,activities}`, `packages/transcode/src/{workflows,activities}`, and `packages/workflow-core/src/`.
- **Runtime**: Bun (Bundler & Runner)
- **Framework**: React
- **Styling**: Tailwind CSS + Shadcn UI (Components)
### UI Rules
- **Prefer System Colors**: Always prefer using semantic system color design tokens (e.g., `bg-background`, `text-foreground`, `text-sidebar-primary`, `text-muted-foreground`, etc.) over hardcoded color utilities (e.g., `bg-blue-50`, `text-emerald-600`, `bg-emerald-500`). This ensures seamless compatibility with light and dark themes.
### Route Code-Splitting
To optimize initial bundle size, all routes must be code-split into `[route].tsx` (lightweight route definition and loaders) and `[route].lazy.tsx` (UI component and heavy imports).
### Development
- **Run Dev**: `bun run dev` (Runs on port 3000)
## Testing
We use **Vitest** for testing (via `bun run test`). **Do not use `bun test`** as it uses Bun's native test runner which is not compatible with this project's test suite.
### Bug Fixes
- **Mandatory Reproduction**: When fixing a backend bug, you MUST first write a test case that reproduces the bug (demonstrates the failure) before applying the fix. This ensures the bug is truly understood and prevents future regressions.
### Test Maintenance Rule
- **Do not delete tests** unless the feature or code being tested is permanently removed from the codebase.
- When refactoring code, you must also refactor the corresponding tests to ensure they pass with the new implementation.
- This ensures we maintain regression coverage and do not silently break features.
### Service Tests
- Located in `packages/core/src/**/*.test.ts`.
- For comprehensive testing against a real database instance, we use `Testcontainers` alongside a `PrismaTestingHelper` which wraps each test within an automatic PostgreSQL transaction rollback boundary.
- All service tests must use `setupTestDbHooks()` from `@shumai/db` to initialize the database boundaries for each test suite inside the `describe` block. This automatically registers `beforeEach` and `afterEach` hooks to manage transaction state.
- Import `prisma` from `@shumai/db` directly to interact with the database in tests. The testing helper automatically proxies it.
```typescript
import { describe, it, expect } from 'vitest'
import { prisma, setupTestDbHooks } from '@shumai/db'
import { teamService } from '@shumai/core/src/team/team'
describe('TeamService', () => {
setupTestDbHooks()
it('works with db', async () => {
const user = await prisma.user.create({
data: { name: 'Test User', password: 'pw' },
})
const team = await teamService.ensureDefaultTeam()
expect(team).toBeDefined()
expect(team.name).toBe('Default Team')
})
})
```
The `bunfig.toml` is configured to run `setup-tests.ts` automatically as a preload step. This initializes the `Testcontainers` PostgreSQL instance and applies migrations before the tests begin running.
### API Tests
- Located in `packages/api/src/**/*.test.ts`.
- Mock the Service layer using `vi.spyOn(service, 'method')` or `vi.mock('@shumai/core/src/team/team')`.
- Verify that the API calls the Service methods correctly.
```typescript
import { describe, it, expect, vi } from 'vitest'
import { teamService } from '@shumai/core/src/team/team'
import app from './team'
describe('Team API', () => {
it('calls service', async () => {
const mockEnsureDefaultTeam = vi
.spyOn(teamService, 'ensureDefaultTeam')
.mockResolvedValue({ id: '1', name: 'Default Team' } as any)
// ... test logic ...
expect(mockEnsureDefaultTeam).toHaveBeenCalled()
mockEnsureDefaultTeam.mockRestore()
})
})
```
### Workflow E2E Tests
- Located in `packages/e2e/workflow/**/*.test.ts`.
- Run via `bun run test:e2e:workflow`.
- **Mandatory test coverage**: When creating or updating workflows or activities, you MUST create or update the corresponding E2E tests.
- **Mocking**: Mock ONLY AI API calls. Do not mock S3, databases, or media/transcode extraction services.
- Mock the AI response by spying on `AgentHarness.prototype.prompt`:
### 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: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.
## Prisma Configuration & Migrations
- Schema: `packages/db/prisma/schema.prisma`
- Generated Client: `packages/db/src/generated/prisma`
- Config: `prisma.config.ts` (Required for Prisma 7+)
### Strict JSON Typing
We use `prisma-json-types-generator` to enforce strict type-safety for Prisma `Json` columns, rather than typing them as generic `JsonValue`.
- **Defining Types**: In `packages/db/prisma/schema.prisma`, annotate the JSON column with a triple-slash comment specifying the type name from `packages/db/src/prisma-json-types.ts`:
```prisma
model WorkflowTask {
/// [WorkflowTaskPayload]
payload Json?
}
```
- **Declaring Types**: All typed JSON shapes must be defined in `packages/db/src/prisma-json-types.ts` under the global `PrismaJson` namespace:
```typescript
declare global {
namespace PrismaJson {
export type WorkflowTaskPayload = Record<string, unknown> | TaskSpec | AiTaskPayload
}
}
```
- **Usage**: When accessing these columns on the Prisma client (e.g. `task.payload`), the field will automatically be typed as the declared shape (e.g., `PrismaJson.WorkflowTaskPayload | null`).
- **Accessing Properties**: When working with union types or custom structures in the JSON payload, use proper narrowing or type guards instead of casting to `any`. Direct `as any` casting should be avoided.
### Migration Workflow
- 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 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
- Generate Client: `bun --bun run prisma generate`
- Apply Migrations (Dev): `bun --bun run prisma migrate dev`
- Deploy Migrations (Prod): `bun --bun run prisma migrate deploy`
## ID Generation & Sorting
- **ULID Default**: All models (except key-value stores like `Setting`) must use `@default(ulid())` for the `id` field in `schema.prisma`.
```prisma
model Example {
id String @id @default(ulid())
}
```
- **No Manual Assignment**: Do not manually generate or assign ULIDs in application code (e.g., `packages/core/src/*.ts`) for creating database records. Let Prisma handle it via the schema default.
- Exception: File names generated by `FileService` may still use `ulid()` internally, but this is separate from the database ID.
- **Sorting**: All list APIs must order results by `id` descending (`orderBy: { id: 'desc' }`) to ensure consistent, time-based sorting (since ULIDs are sortable). Do not sort by `createdAt` as it is not indexed.
## Infinite Scroll & Pagination
- **Frontend**: For paginated APIs (which contain `pageInfo` and `total` fields), the UI MUST use an auto infinite scroll style design using `useInfiniteQuery` from `@tanstack/react-query` and `react-intersection-observer`. Use the `total` count from the API response instead of counting the results locally.
- The default `limit` should be **20**.
- **Backend**:
- Most list APIs support cursor pagination using opaque tokens (via `hyrumtoken` logic) using the `paginateQuery` helper function.
- Pass `PaginationParams` containing `first` (limit) and `after` (cursor).
- The default `limit` should be **20**.
- Always use the `paginateQuery` helper function from `packages/core/src/pagination.ts` for consistent cursor pagination.
## DBOS Workflow & Activities
We use **DBOS Transact** (`@dbos-inc/dbos-sdk`) as our unified workflow engine for reliable, distributed background execution.
### Architecture
1. **Workflows**: Orchestrate multiple steps/activities reliably. Defined in worker packages (e.g., `@shumai/agent/src/workflows`, `@shumai/transcode/src/workflows`).
2. **Activities (Steps)**: Perform the actual work (DB updates, API calls, media processing). Grouped by domain in worker packages (e.g., `@shumai/agent/src/activities`, `@shumai/transcode/src/activities`).
3. **Executors**:
- `DbosExecutor` (in `@shumai/workflow-core`): Coordinates workflow and step dispatching via DBOS queues against PostgreSQL.
4. **Automatic Submission**: New `WorkflowTask` records are automatically enqueued to `WorkflowService` via a Prisma Client Extension defined in `@shumai/db`.
### Dynamic Registration
To avoid circular dependencies, workflows are dynamically registered onto the workflow registry at bootstrap time via initializer functions (e.g., `initAgentWorkflows()`, `initTranscodeWorkflows()`).
### Non-Retryable Error Handling
To prevent the engine from indefinitely retrying fatal, expected business validation failures (e.g., missing records, invalid configurations, missing parameters), **never throw standard `Error` objects from workflow or activity logic.** Instead, throw a non-retryable `ApplicationFailure` (alias of `WorkflowFailure`).
**Mandatory Requirement**: You **MUST** set `nonRetryable: true` in the options object passed to `ApplicationFailure.create`. Standard errors or failure objects without `nonRetryable: true` will cause DBOS to retry the activity or workflow according to its retry policy.
- **Import**: Import `ApplicationFailure` from `@shumai/workflow-core`:
```typescript
import { ApplicationFailure } from '@shumai/workflow-core'
throw ApplicationFailure.create({ message: 'agentId missing in payload', nonRetryable: true })
```
### Workflow Patterns
- **Definition**: Define workflows as exported async functions.
```typescript
import { activityA, activityB } from '../activities/my-activities'
export async function myWorkflow(task: WorkflowTask): Promise<void> {
await activityA({ ... })
await activityB({ ... })
}
```
- **Activity Invocation**: Workflows import and call activities directly as standard async functions.
- **Queues**: Workflows are dispatched to dedicated task queues (e.g. `TaskQueueTranscode`, `TaskQueueAgent`) managed by DBOS.
### Activity Patterns
- **Definition**: Wrap exported activity functions with `defineStep` from `@shumai/workflow-core`.
```typescript
export const myActivity = defineStep(
async function myActivity(params: MyActivityParams): Promise<MyActivityResult> {
...
},
{ runOnCancel: false }, // optional: set runOnCancel: true for cleanup/status handlers
)
```
- **Implementation**: Activities _can_ and _should_ call other services (e.g., `aiService`, `transcodeService`) or interact with the database directly. Database (Prisma) queries or updates are allowed in all activity types (e.g., agent or transcode) for simplicity.
### Worker Identity & Multi-Instance Deployments
In multi-replica deployments (e.g., multiple transcode or agent worker instances), each worker instance MUST have a unique executor ID to prevent startup recovery from re-enqueueing workflows being actively processed by other live workers.
- **Identity Derivation**: `DbosExecutor` automatically derives a unique per-instance ID:
- If `SHUMAI_WORKER_ID` or `DBOS__VMID` is set, it is used as the instance identifier.
- In Kubernetes, set `POD_NAME` (via Downward API: `fieldRef: { fieldPath: metadata.name }`) to provide stable, per-pod identity across StatefulSet restarts.
- In Docker/container environments, `HOSTNAME` (container ID) is automatically used.
- On bare metal/multi-process hosts, `process.pid` and an instance suffix are appended to guarantee uniqueness.
### Durable Step File Exchange
Intermediate files passed across steps must be saved to durable storage (S3) by the producing step, consuming steps must use `ensureLocalMediaFile` (or S3 fetch) rather than assuming local `/tmp` presence, and any purely temporary S3 scratch files must be cleaned up in a `finally` block.
## Import conventions
- When importing across workspace packages, always use absolute workspace imports (e.g. `import { prisma } from '@shumai/db'`, `import { teamService } from '@shumai/core/src/team/team'`).
- **DTO Naming**: Do not append the `Dto` suffix to types or interfaces (e.g. use `JoinRequest` instead of `JoinRequestDto`).
## Radix UI / Shadcn UI Patterns
- **Modal Overlays**: When triggering a dialog (e.g., `Dialog`, `AlertDialog`) from inside a `DropdownMenu`, you must set `modal={false}` on the `<DropdownMenu>` component. Failing to do so will cause the UI to freeze due to conflicting focus management between the two modal components.
- **Scrollable Content**: Always use the Shadcn `ScrollArea` component (`@/ui/components/ui/scroll-area`) for scrollable content containers to ensure consistent custom scrollbar UI across the application.
- **Patterns**: Workflows are exported async functions registered at bootstrap (`initAgentWorkflows`, `initTranscodeWorkflows`) and auto-enqueued when `WorkflowTask` records are created. Wrap activity functions with `defineStep` from `@shumai/workflow-core` (activities may call services or query `prisma` directly).
- **Non-Retryable Errors**: Never throw standard `Error` objects for fatal validation or business failures in workflows/activities. Always throw `ApplicationFailure.create({ message: '...', nonRetryable: true })` from `@shumai/workflow-core`.
- **Durable Step File Exchange**: Save intermediate files passed across steps to S3, fetch them in consuming steps via `ensureLocalMediaFile` (never assume local `/tmp` persists across steps), and clean up temporary S3 files in a `finally` block.
## Logging
We use **pino** for logging. Always use **structured logging** to ensure logs are easily searchable and machine-readable.
Use structured **pino** logging via `import { logger } from '@shumai/core/src/logger'`. Pass metadata object first and message string second: `logger.info({ userId, projectId }, 'Project deleted')`.
- **Import**: `import { logger } from '@shumai/core/src/logger'`
- **Usage**: Pass an object as the first argument containing the metadata, and a string as the second argument for the descriptive message.
```typescript
logger.info({ userId: user.id, projectId }, 'Project deleted successfully')
```
- **Levels**: Use appropriate levels: `debug`, `info`, `warn`, `error`.
## Testing
## Internationalization (i18n)
Use **Vitest** via `bun run test` (**never** use `bun test`).
All user-facing text in the WebUI (`packages/webui`) MUST be internationalized using **Paraglide JS**. Hardcoded text in UI components is strictly forbidden.
### Message Definition
1. Define your English message strings in [packages/webui/messages/en.json](packages/webui/messages/en.json).
2. Define the corresponding Chinese translation strings in [packages/webui/messages/zh.json](packages/webui/messages/zh.json).
3. Keys should use `snake_case`.
Example (`en.json`):
```json
{
"my_key": "My English Text"
}
```
### Compilation
When you add or update translation keys, you must compile them to regenerate the type-safe functions:
```bash
# Compile once (generates type-safe TS/JS functions on disk)
bun run i18n:compile
# Watch and recompile on change during development
bun run i18n:watch
```
### Usage in Components
Import the compiled message namespace `m` and call it as a function:
```typescript
import { m } from '@/ui/paraglide/messages.js'
export function MyComponent() {
return (
<div>
<h1>{m.my_key()}</h1>
</div>
)
}
```
Always use the absolute workspace alias path `@/ui/paraglide/messages.js` (or `runtime.js` for runtime features) when importing Paraglide outputs.
- **Bug Fixes**: Always write a test case that reproduces the bug (demonstrates failure) before applying a backend fix.
- **Test Maintenance**: Never delete tests unless the tested feature is permanently removed; update existing tests when refactoring.
- **Service & Operation Tests (`packages/core/src/**/*.test.ts`)**: Call `setupTestDbHooks()` from `@shumai/db` inside `describe` for per-test PostgreSQL transaction rollback, and use `prisma` from `@shumai/db`. Every Operation requires real-DB tests covering success, permission denial (no side effects), and audit entries.
- **API Tests (`packages/api/src/**/*.test.ts`)**: Mock Core services using `vi.spyOn` or `vi.mock`.
- **Workflow E2E Tests (`packages/e2e/workflow/**/*.test.ts`)**: Run via `bun run test:e2e:workflow`. Mandatory when adding/updating workflows or activities. Mock **only** AI API calls (spy on `AgentHarness.prototype.prompt`); do not mock S3, DB, or media/transcode services.
- **Web App E2E Tests (`apps/web/e2e/**/*.spec.ts`)**: Run via `bun run test:e2e:app`. Use `owner`, `project`, and `file` fixtures for setup, and document every added, renamed, or removed spec in `apps/web/e2e/README.md`.
+28
View File
@@ -12,6 +12,34 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Changed
## [0.6.2] - 2026-10-29
### Added
* **mcp**: Add a built-in MCP server supporting OAuth 2.1 and API tokens, allowing external AI agents (such as Claude Code, Cursor, or Kiro) to browse projects, manage and upload files, inspect media contents (images, video frames, PDF pages, 3D models, and text), search assets, manage review comments, update metadata, and organize version stacks
* **cli**: Add `shumai-cli` commands to download files (`download`), view and render media contents (`read`, `render`), search and copy files (`search`, `copy`), manage review comments (`comment`), view and update metadata (`field ls`, `meta set`), organize version stacks (`version`), and view folder instructions (`instructions`)
* **agent**: The built-in agent can now list accessible projects, search files by name, meaning, or metadata, read and post review comments, update metadata, copy files, stack and unstack versions, and read folder instructions (`AGENTS.md`), with configurable permissions per agent
### Fixed
### Changed
## [0.6.1] - 2026-10-24
### Added
* **dashboard**: Add Workflow Tasks section to the Team Dashboard allowing team owners to monitor background workflow jobs, inspect execution payloads and outputs, and stop in-flight tasks
### Fixed
* **metadata**: Fix an issue where clicking to edit a metadata field on a card or list row inadvertently selected the file, and prevent expanded text overlays or date pickers from being clipped by container borders
### Changed
* **deps**: Upgrade Bun to 1.4.4
## [0.6.0] - 2026-10-20
### Added
+2 -2
View File
@@ -1,6 +1,6 @@
#syntax=docker/dockerfile:1.7-labs
# Stage 1: Builder
FROM oven/bun:1.4.2 AS builder
FROM oven/bun:1.4.4 AS builder
WORKDIR /app
@@ -28,7 +28,7 @@ RUN bun run build.ts
RUN bun run scripts/extract-runtime-deps.ts
# Stage 2: Runner
FROM oven/bun:1.4.2 AS runner
FROM oven/bun:1.4.4 AS runner
# Install system dependencies and Jellyfin-FFmpeg (with VA-API support)
RUN apt-get update && apt-get install -y --no-install-recommends \
+2 -2
View File
@@ -1,6 +1,6 @@
#syntax=docker/dockerfile:1.7-labs
# Stage 1: Builder
FROM oven/bun:1.4.2 AS builder
FROM oven/bun:1.4.4 AS builder
WORKDIR /app
@@ -23,7 +23,7 @@ RUN bun run build.ts transcode
RUN bun run scripts/extract-runtime-deps.ts
# Stage 2: Runner
FROM oven/bun:1.4.2 AS runner
FROM oven/bun:1.4.4 AS runner
# Install transcoding system dependencies and Jellyfin-FFmpeg (with VA-API/hardware acceleration support)
RUN apt-get update && apt-get install -y --no-install-recommends \
+64 -17
View File
@@ -12,7 +12,7 @@
<a href="https://hub.docker.com/r/shumaione/shumai"><img alt="Docker pulls" src="https://img.shields.io/docker/pulls/shumaione/shumai" /></a>
<a href="https://github.com/shumaiOne/shumai/releases"><img alt="GitHub latest release" src="https://img.shields.io/github/v/release/shumaiOne/shumai" /></a>
<a href="LICENSE"><img alt="License" src="https://img.shields.io/badge/license-MIT-blue" /></a>
<a target="_blank" href="https://deepwiki.com/shumaiOne/shumai"><img alt="Ask DeepWiki" src="https://deepwiki.com/badge.svg" /></a>
<a target="_blank" href="https://deepwiki.com/shumaiOne/shumai"><img alt="Ask DeepWiki" src="https://img.shields.io/badge/Ask-DeepWiki-6E56CF" /></a>
</p>
<p align="center">
@@ -30,7 +30,7 @@ Shumai is an open-source Frame.io alternative built for creative teams to collab
* **Secure Sharing & Collections**: Create private share links and curated media collections for clients, stakeholders, and collaborators.
* **Granular Access Control**: Manage workspace permissions with team-level and project-level role-based access control.
* **Durable Workflows via DBOS**: Run background tasks and AI agent workflows orchestrated reliably by DBOS on PostgreSQL with automatic recovery and fault tolerance.
* **Scalable Deployment on Kubernetes**: Horizontally autoscale video transcoding on multiple instances. See [EKS Deployment Example](eks/README.md).
* **Scalable Deployment on Docker or Kubernetes**: Horizontally autoscale video transcoding on multiple instances. See [EKS Deployment Example](eks/README.md).
* **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.
@@ -38,6 +38,7 @@ Shumai is an open-source Frame.io alternative built for creative teams to collab
* **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).
* **Semantic Search**: Find assets by visual or conceptual meaning using vector embeddings. See [documentation](https://docs.shumai.one/using-shumai/semantic-search).
* **NLE Export & Premiere Pro Plugin (Experimental)**: Export timestamped review comments to your favorite non-linear editor (Premiere Pro XML, DaVinci Resolve EDL, Final Cut Pro, Avid Media Composer) or use the experimental Premiere Pro UXP plugin to import media assets and auto-sync comments directly to sequence timeline markers. See [documentation](https://docs.shumai.one/using-shumai/nle-and-premiere).
* **Built-in MCP Server & CLI**: Connect external AI agents to Shumai via `/mcp` or manage projects, assets, comments, and metadata from the terminal with `shumai-cli`.
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:
@@ -70,7 +71,9 @@ The ultimate goal is an **agentic Kanban**, where agents and team members can wo
> * **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.
---
Have feedback or ideas? [Share them with us](https://github.com/shumaiOne/shumai/issues/328), and if you find Shumai useful, please give us a ⭐ or recommend Shumai to someone who might find it useful, it really helps!
## Installation
@@ -103,10 +106,20 @@ Docker Compose is the fastest way to get Shumai running. You do not need to clon
> [!NOTE]
> By default, the Docker Compose setups use CPU software transcoding. If your server has a GPU (NVIDIA NVENC, Intel QuickSync, AMD/Intel VA-API, or Rockchip RKMPP) and you want to enable GPU hardware acceleration, check the [Hardware Transcoding Guide](https://docs.shumai.one/configuration/hardware-transcoding).
3. Configure environment variables (optional):
3. Configure environment variables:
* `SHUMAI_SERVER_PORT` controls the port the Shumai server listens on. The default is `3000`.
* By default, Shumai uses the bundled local storage service in this Docker Compose setup. To use an external S3-compatible storage instead, configure the environment variables for your S3 provider. Here are examples for AWS S3 and Cloudflare R2:
* `BETTER_AUTH_URL`: The public base URL of your Shumai server (e.g., `https://shumai.example.com` or `http://12.34.56.78:3000`). This is required for remote servers and production deployments.
* `AWS_ENDPOINT_URL_S3`: By default, `AWS_ENDPOINT_URL_S3` is set to `http://localhost:{SHUMAI_SERVER_PORT}`. When you use local storage (the default storage backend with `STORAGE_BACKEND: local`) and expose Shumai on a remote server, custom domain, or custom host/port combination, set `AWS_ENDPOINT_URL_S3` to the external URL that client browsers will use to upload and download files.
For example, if you change the port mapping in `docker-compose.yaml` from `3000:3000` to `12345:3000` and deploy on a server with IP address `12.34.56.78`, set:
```yaml
AWS_ENDPOINT_URL_S3: http://12.34.56.78:12345
```
This value must be reachable from client browsers and should include the externally exposed port.
* `SHUMAI_SERVER_PORT`: Controls the port the Shumai server listens on. The default is `3000`.
* **External S3 Storage**: By default, Shumai uses the bundled local storage service in this Docker Compose setup. To use an external S3-compatible storage instead, configure the environment variables for your S3 provider. Here are examples for AWS S3 and Cloudflare R2:
**AWS S3 Example:**
```yaml
@@ -126,21 +139,44 @@ Docker Compose is the fastest way to get Shumai running. You do not need to clon
S3_SECRET_ACCESS_KEY: your-secret-access-key
AWS_ENDPOINT_URL_S3: https://<account-id>.r2.cloudflarestorage.com
```
* By default, `AWS_ENDPOINT_URL_S3` is set to: `http://localhost:{SHUMAI_SERVER_PORT}`, if you use local storage and expose Shumai on a custom host/port combination, set `AWS_ENDPOINT_URL_S3` to the external URL that browsers will use to upload files.
For example, if you change the port mapping in `docker-compose.yaml` from `3000:3000` to `12345:3000` and deploy on a server with IP address `12.34.56.78`, set:
```
AWS_ENDPOINT_URL_S3: http://12.34.56.78:12345
```
This value must be reachable from client browsers and should include the externally exposed port.
5. Start the services in detached mode:
4. Start the services in detached mode:
```bash
docker compose up -d
```
6. Open your browser and access Shumai at `http://localhost:3000` (or `http://<your-server-ip>:3000` for remote deployments).
5. Open your browser and access Shumai at `http://localhost:3000` (or `http://<your-server-ip>:3000` for remote deployments).
#### **Scaling Transcode Workers (Parallel Processing)**
By default, the Docker Compose deployment transcodes videos one-by-one. This is by design, as video transcoding is compute-intensive and can saturate available CPU or GPU resources.
However, when uploading large batches (e.g., 100+ videos at once) or massive raw camera files that take hours to process and block subsequent uploads, you can scale transcoding throughput across additional machines. Each machine still processes tasks sequentially, but adding more worker instances increases overall parallel capacity.
Shumai makes this seamless with a dedicated [`shumaione/shumai-transcode`](https://hub.docker.com/r/shumaione/shumai-transcode) Docker image:
1. **Connect**: Launch a temporary machine and ensure network access to your PostgreSQL database and storage backend (e.g., S3 or shared storage).
2. **Start the worker**: Run the container with matching database and storage credentials:
```bash
docker run -d --name shumai_transcode_worker \
-e DATABASE_URL="postgres://shumai:shumai_password@<postgres-host>:5432/shumai_db?sslmode=disable" \
-e STORAGE_BACKEND="s3" \
-e S3_BUCKET="your-bucket-name" \
-e AWS_ENDPOINT_URL_S3="https://s3.us-east-1.amazonaws.com" \
-e S3_ACCESS_KEY_ID="your-access-key-id" \
-e S3_SECRET_ACCESS_KEY="your-secret-access-key" \
shumaione/shumai-transcode:latest
```
The worker will automatically attach to the task queue and begin processing pending transcode jobs in parallel with your primary instance.
3. **Stop anytime**: Once the transcoding backlog finishes (or whenever you no longer need the extra compute), simply stop or remove the worker:
```bash
docker stop shumai_transcode_worker
```
There is no downtime or impact on the main Shumai application.
### Option 2: Run from Source (Development)
@@ -165,6 +201,7 @@ To set up Shumai locally for development:
```env
DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public
BETTER_AUTH_SECRET=ySxs7DxzHDZBbeeHNPEwBuspYwipBqz5Gk5XdBjNhWw=
BETTER_AUTH_URL=http://localhost:3000
STORAGE_BACKEND=local
SHUMAI_SERVER_PORT=3000
AWS_ENDPOINT_URL_S3=http://localhost:3000
@@ -194,3 +231,13 @@ To set up Shumai locally for development:
Shumai provides a Command Line Interface (CLI) tool to manage projects, folders, and assets, upload files/folders, and create new versions directly from your terminal.
For more details on installation and usage, see the [CLI Readme](apps/cli/README.md).
## MCP Server
Shumai includes a built-in MCP server at `/mcp`, so external agents such as Claude Code, Cursor, or Kiro can browse and organize your projects with OAuth or an API token. See the [MCP server guide](docs/developer/mcp-server.mdx) for setup.
---
## Community
Thanks to [LINUX DO](https://linux.do/) for providing a community for open-source discussions and feedback.
+107 -60
View File
@@ -5,48 +5,50 @@
<h1 align="center">Shumai</h1>
<p align="center">
<strong>为创意工作打造的开源协作平台。</strong>
<strong>开源版 阅流 / 分秒帧 - 专为创意与视频团队打造的审片与管理平台。</strong>
</p>
<p align="center">
<a href="https://hub.docker.com/r/shumaione/shumai"><img alt="Docker pulls" src="https://img.shields.io/docker/pulls/shumaione/shumai" /></a>
<a href="https://github.com/shumaiOne/shumai/releases"><img alt="GitHub latest release" src="https://img.shields.io/github/v/release/shumaiOne/shumai" /></a>
<a href="LICENSE"><img alt="License" src="https://img.shields.io/badge/license-MIT-blue" /></a>
<a target="_blank" href="https://deepwiki.com/shumaiOne/shumai"><img alt="Ask DeepWiki" src="https://deepwiki.com/badge.svg" /></a>
<a target="_blank" href="https://deepwiki.com/shumaiOne/shumai"><img alt="Ask DeepWiki" src="https://img.shields.io/badge/Ask-DeepWiki-6E56CF" /></a>
</p>
<p align="center">
<a target="_blank" href="https://shumai.one/">官方网站</a> | <a target="_blank" href="https://docs.shumai.one/getting-started/overview">文档</a> | <a target="_blank" href="https://staging.shumai.one">在线演示</a> | <a target="_blank" href="README.md">English</a>
<a target="_blank" href="https://shumai.one/">官方网站</a> | <a target="_blank" href="https://docs.shumai.one/getting-started/overview">文档(英文)</a> | <a target="_blank" href="https://staging.shumai.one">在线演示</a> | <a target="_blank" href="README.md">English</a>
</p>
![Shumai 应用截图](docs/screenshot.webp)
## 功能
Shumai 是一个开源的 Frame.io 替代方案,专为创意团队打造,让大家能更轻松地协作、审片和分享素材:
Shumai 是一个专为广告,短剧,漫剧等各类创意团队打造的开源协作管理平台,功能上类似阅流和分秒帧,提供了协作、审片和分享等各种功能:
* **支持 S3 和本地存储**:你可以把创意素材安全地存到本地文件系统,或者任意兼容 S3 的存储服务里,比如 AWS S3、Cloudflare R2、MinIO 等。
* **逐帧标注和评论**:支持按帧进行精确反馈,可以直接在视频和图片素材上画重点、写评论,还能留下时间戳。
* **安全分享和合集管理**:可以生成私密分享链接,也能整理成精选合集,方便给客户、同事和合作方查看。
* **支持 S3 和本地存储**:你可以把创意素材安全地存放在本地硬盘或者任意兼容 S3 的存储服务里,比如 阿里云 OSS 或 MinIO 等。
* **帧级别标注和评论**:支持按帧进行精确反馈,同时支持直接在视频上进行标注,让团队成员能更精确地Get到你的想法。
* **安全的分享**:可以生成私密分享链接,同时还支持给分享的图片/视频添加自定义水印,并将水印配置保存为模板以便重复使用。方便给客户、同事和合作方查看。
* **细粒度权限控制**:支持按团队、项目设置角色权限,方便管理不同成员的访问范围。
* **基于 DBOS 的持久化工作流**:后台任务与 AI 工作流基于 PostgreSQL 由 DBOS 统一持久化编排与调度,具备自动恢复和容错能力。
* **Kubernetes 弹性扩缩容部署**:支持在多个实例上横向自动扩缩容视频转码工作负载。详见 [EKS 部署示例](eks/README.md)。
* **基于 DBOS 的持久化工作流**:视频转码任务由 DBOS 统一持久化编排与调度,具备自动恢复和容错能力。
* **Docker/K8s 弹性扩缩容部署**:支持在多个实例上横向自动扩缩容视频转码工作负载。详见 [EKS 部署示例](eks/README.md)。
* **自定义素材元数据**:可以按你的制作流程,自由定义动态元数据字段。
* **分享链接水印**:通过可自定义的文字和图片水印保护共享媒体,并将水印配置保存为模板以便重复使用。
* **丰富的文件格式支持**:支持预览常见创意文件格式,包括图片(支持 PSD)、视频、音频和文档,同时可上传和存储任意类型的文件。
* **基于转盘渲染服务的 3D 资产预览**:通过集成 [turntable-renderer](https://docs.shumai.one/using-shumai/3d-asset-support) 支持为 GLB、FBX、OBJ、STL、USD 和 DAE 等 3D 模型生成高质量的交互式转盘预览与视频。
* **基于 Gotenberg 的文档 PDF 转码代理**:通过集成 [Gotenberg](https://docs.shumai.one/using-shumai/gotenberg) 支持将 Word、PowerPoint、Excel、Markdown、HTML 和 CSV 文档高质量转换为 PDF 代理文件进行在线预览。
* **语义搜索**:支持按画面内容或概念含义搜索素材,底层基于向量嵌入技术。详见[相关文档](https://docs.shumai.one/using-shumai/semantic-search)。
* **非编剪辑导出与 Premiere 插件(实验性)**:支持将时间戳审阅评论导出至常用非编软件(Premiere Pro XML、DaVinci Resolve EDL、Final Cut Pro、Avid Media Composer),或使用实验性的 Premiere Pro 原生 UXP 插件导入媒体素材并将评论自动同步为时间轴标记。详见[相关文档](https://docs.shumai.one/using-shumai/nle-and-premiere)。
* **3D 素材预览**:通过集成 [turntable-renderer](https://docs.shumai.one/using-shumai/3d-asset-support) 支持给 GLB、FBX、OBJ、STL、USD 和 DAE 等 3D 模型生成高质量的交互式转盘预览。
* **Office 与文档在线预览**:通过集成 [Gotenberg](https://docs.shumai.one/using-shumai/gotenberg),自动将 Word、PPT、Excel、Markdown、HTML 和 CSV 高质量转换为 PDF,直接在浏览器里丝滑预览。
* **语义搜索**:支持按画面内容或概念含义搜索素材,详见[相关文档](https://docs.shumai.one/using-shumai/semantic-search)。
* **非编剪辑导出与 Premiere 插件(测试版)**:支持将时间戳审阅评论导出至常用非编软件(Premiere Pro XML、DaVinci Resolve EDL、Final Cut Pro、Avid Media Composer),或使用实验性的 Premiere Pro 原生 UXP 插件导入媒体素材并将评论自动同步为时间轴标记。详见[相关文档](https://docs.shumai.one/using-shumai/nle-and-premiere)。
* **内置 MCP 服务端与 CLI 工具**:支持通过 `/mcp` 将外部 AI Agent 接入 Shumai,或使用 `shumai-cli` 在终端中管理项目、素材、评论与元数据。
但 Shumai 不仅仅只满足于 Frame.io 的开源替代。随着 GPT-Image-2、Seedance 2.5 这类强大模型的问世,AI已经生成产品级的图片/视频。很多时候,你只要给一句提示词,就能直接生成或修改图片和视频,而不用再打开 Photoshop、Premiere 或者 Houdini。也正因为这样,Shumai 才把 AI Agent 做成了核心能力:
Shumai 不仅仅只满足于「阅流」和「分秒帧」的开源替代。随着 GPT-Image-2、Seedance 2.5 这类强大视频/图片生成模型的问世,AI已经能够生成产品级的创意素材。只需要一句提示词,AI就能直接生成或修改图片和视频,而不用再打开 Photoshop、Premiere 或者 Houdini。因此Shumai也添加了Agent相关功能:
* **Agent 就像团队成员**:你可以在素材评论区里直接用 `@` 提到 AI Agent,像拉一个真实同事进来一起看稿一样。
> **AI 功能完全可选。** 你完全可以把 Shumai 当作纯粹的开源版「阅流 / 分秒帧」来用;如果团队不需要 AI,直接忽略即可。
* **Agent 是你的团队成员**:你可以在素材评论区里直接用 `@` 提到 AI Agent,像拉一个真实同事进来一起看稿一样。
* **Agent 也能当你的私聊助手**:你可以单独和它聊天,让它帮你查东西、做任务。
* **自定义技能和工具**:你可以给 Agent 接上自己的脚本、工具和自动化能力。
* **Model Context Protocol (MCP) 支持**:通过远程 MCP 服务端为 Agent 接入外部工具与 API,支持 OAuth 鉴权与按 Agent 独立分配访问权限。
* **自定义技能和工具**:你可以给 Agent 添加自己的脚本、工具和自动化能力。
* **接入外部 MCP 工具 (Model Context Protocol)**:通过远程 MCP 服务端为 Agent 接入外部工具与 API,支持 OAuth 鉴权与按 Agent 独立分配访问权限。
* **沙箱隔离执行**:Agent 生成的脚本会在隔离环境里运行,更安全。
* **AI 自动补全元数据**:新素材上传后,系统可以自动帮你补一些元数据。
* **AI 自动打标与补全元数据**:新素材上传后,AI 可根据画面内容自动识别并填写自定义元数据字段,省去手动归档打标的麻烦。
* **资源配额**:支持按成员、角色或全团队对 AI Token、成本和工具调用设置使用上限与实时用量监控。
下面是一个简短的 demo 视频,展示了 Shumai Agent 能做什么:
@@ -66,15 +68,17 @@ Shumai 也将项目管理融入了创意工作流程。 内置的看板可以帮
> * **图片:** 会生成一份与原图分辨率完全一致的高质量 WebP 转码版本,在保证画质的同时提升浏览性能。
> * **视频:** 会根据转码配置生成至少一个代理视频,同时生成雪碧图(Sprite)和一段最长 1600 帧的低分辨率转码视频,用于实现快速拖动时间轴和即时视频预览。
如果你有任何反馈或想法,欢迎在 [Issue #329](https://github.com/shumaiOne/shumai/issues/329) 中和我们交流。如果 Shumai 对你有帮助,希望能给我们点个 ⭐,或者安利给身边做视频/创意的朋友。
---
## 安装指南
以下是使用本地存储运行 Shumai 的快速上手指南。如需了解高级配置(包括 S3 兼容存储配置、分布式 worker 编排等),请参阅我们的[官方文档](https://docs.shumai.one/getting-started/overview)。
以下是使用本地存储的快速安装指南。如需了解高级配置(包括 S3 兼容存储配置、分布式 worker 编排等),请参阅我们的[官方文档](https://docs.shumai.one/getting-started/overview)。
### 方案一:使用 Docker Compose(推荐)
<details>
<summary>复制这个提示词,让agent帮你安装</summary>
```
@@ -92,68 +96,67 @@ https://gh-proxy.com/https://raw.githubusercontent.com/shumaiOne/shumai/main/REA
```
</details>
使用 Docker Compose 是运行 Shumai 最快的方式。您无需克隆仓库,也不需要手动安装依赖包。请确保电脑上已安装 [Docker](https://docs.docker.com/get-docker/) 和 [Docker Compose](https://docs.docker.com/compose/install/),然后按照以下步骤操作:
使用 Docker Compose 是运行 Shumai 最快的方式。你无需克隆仓库,也不需要手动安装依赖包。请确保电脑上已安装 [Docker](https://docs.docker.com/get-docker/) 和 [Docker Compose](https://docs.docker.com/compose/install/),然后按照以下步骤操作:
1. 创建并进入一个新目录,用于存放配置文件和数据卷:
1. 创建一个新文件夹,用于存放配置文件和数据:
```bash
mkdir shumai && cd shumai
```
2. 根据您的需求和机器可用内存,下载对应的 `docker-compose.yaml` 配置文件:
2. 根据你的需求和机器可用内存,下载对应的 `docker-compose.yaml` 配置文件(注意所有镜像都是阿里云源,想要使用dockerhub源请参考不带aliyun的compose文件):
* **精简版(建议内存:2 GB+)**:包含 Shumai 核心应用与 PostgreSQL 数据库。
* **轻量版(建议内存:2 GB+)**:包含 Shumai 核心应用与 PostgreSQL 数据库。
```bash
curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose-aliyun.yaml
```
* **含嵌入向量服务版(建议内存:4 GB+)**:新增基于 `shumaiOne/embedding-server` (`EmbeddingGemma 2 740M`) 的本地多模态语义搜索能力。
* **集成语义搜索版(建议内存:4 GB+)**:新增基于 `shumaiOne/embedding-server` (`EmbeddingGemma 2 740M`) 的本地多模态语义搜索能力。
```bash
curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose-aliyun.embedding.yaml
```
* **完整版(建议内存:8 GB+)**:一站式全功能套件,包含 `embedding-server`、`turntable-renderer`(3D 模型交互转盘预览)与 `Gotenberg`(文档与表格 PDF 代理转码)。
* **完整版(建议内存:8 GB+)**:一站式全功能版本,包含 `embedding-server`、`turntable-renderer`(3D 模型交互转盘预览)与 `Gotenberg`(文档与表格 PDF 代理转码)。
```bash
curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose-aliyun.full.yaml
```
> [!NOTE]
> 默认的 Docker Compose 配置使用基于 CPU 的软件转码。如果您的服务器配备了支持的显卡(NVIDIA NVENC、Intel QuickSync、AMD/Intel VA-API 或 Rockchip RKMPP)并需要开启 GPU 硬件加速转码,请参考 [硬件加速转码配置文档](https://docs.shumai.one/configuration/hardware-transcoding)。
> 默认的 Docker Compose 配置使用基于 CPU 的软件转码。如果你的服务器支持硬件加速(NVIDIA NVENC、Intel QuickSync、AMD/Intel VA-API 或 Rockchip RKMPP),请参考 [硬件加速转码配置文档](https://docs.shumai.one/configuration/hardware-transcoding)。
3. 配置环境变量(可选):
* `SHUMAI_SERVER_PORT` 用于控制 Shumai 服务监听的端口,默认值为 `3000`。
* 默认情况下,该 Docker Compose 部署会自动启用集成的本地存储。若想切换至外部 S3 兼容存储,请在 `environment` 中配置 S3 提供商的环境变量。以下是 AWS S3 和 Cloudflare R2 的配置示例:
3. 配置环境变量:
* `BETTER_AUTH_URL`:Shumai 服务的外部公开访问地址(例如 `https://shumai.example.com` 或 `http://12.34.56.78:3000`)。在远程服务器或生产环境中部署时必须配置该变量。
* `AWS_ENDPOINT_URL_S3`:默认情况下指向 `http://localhost:{SHUMAI_SERVER_PORT}`。当你使用本地存储(即默认配置 `STORAGE_BACKEND: local`)并在远程服务器、自定义域名或自定义端口上公开访问 Shumai 时,请将 `AWS_ENDPOINT_URL_S3` 设置为客户端浏览器上传和下载文件所使用的外部 URL。
**AWS S3 示例:**
```yaml
STORAGE_BACKEND: s3
S3_BUCKET: your-bucket-name
S3_ACCESS_KEY_ID: your-access-key-id
S3_SECRET_ACCESS_KEY: your-secret-access-key
AWS_ENDPOINT_URL_S3: https://s3.us-east-1.amazonaws.com
```
例如,如果你将 `docker-compose.yaml` 中的端口映射从 `3000:3000` 修改为了 `12345:3000`,且服务部署在 IP 为 `12.34.56.78` 的服务器上,请进行如下设置:
**Cloudflare R2 示例:**
```yaml
STORAGE_BACKEND: s3
S3_BUCKET: your-bucket-name
S3_REGION: auto
S3_ACCESS_KEY_ID: your-access-key-id
S3_SECRET_ACCESS_KEY: your-secret-access-key
AWS_ENDPOINT_URL_S3: https://<account-id>.r2.cloudflarestorage.com
```
```yaml
AWS_ENDPOINT_URL_S3: http://12.34.56.78:12345
```
此地址必须能够从客户端浏览器正常访问,且必须包含对外暴露的实际端口号。
* `SHUMAI_SERVER_PORT` 用于控制 Shumai 服务监听的端口,默认值为 `3000`。
* **外部 S3 存储**:默认情况下,该 Docker Compose 部署会自动启用集成的本地存储。若想切换至外部 S3 兼容存储,请在 `environment` 中配置 S3 提供商的环境变量。以下是 AWS S3 和 Cloudflare R2 的配置示例:
* 默认情况下 `AWS_ENDPOINT_URL_S3` 指向 `http://localhost:{SHUMAI_SERVER_PORT}`。如果您使用本地存储,但希望在自定义的主机名或端口上公开访问 Shumai,请务必将 `AWS_ENDPOINT_URL_S3` 修改为浏览器访问该服务时所使用的外部 URL。
例如,如果您将 `docker-compose.yaml` 中的端口映射从 `3000:3000` 修改为了 `12345:3000`,且服务部署在 IP 为 `12.34.56.78` 的服务器上,请进行如下设置:
```
AWS_ENDPOINT_URL_S3: http://12.34.56.78:12345
**AWS S3 示例:**
```yaml
STORAGE_BACKEND: s3
S3_BUCKET: your-bucket-name
S3_ACCESS_KEY_ID: your-access-key-id
S3_SECRET_ACCESS_KEY: your-secret-access-key
AWS_ENDPOINT_URL_S3: https://s3.us-east-1.amazonaws.com
```
```
此地址必须能够从客户端浏览器正常访问,且必须包含对外暴露的实际端口号。
**Cloudflare R2 示例:**
```yaml
STORAGE_BACKEND: s3
S3_BUCKET: your-bucket-name
S3_REGION: auto
S3_ACCESS_KEY_ID: your-access-key-id
S3_SECRET_ACCESS_KEY: your-secret-access-key
AWS_ENDPOINT_URL_S3: https://<account-id>.r2.cloudflarestorage.com
```
4. 在后台启动所有服务:
@@ -163,11 +166,44 @@ https://gh-proxy.com/https://raw.githubusercontent.com/shumaiOne/shumai/main/REA
```
5. 打开浏览器,访问 `http://localhost:3000` 即可开始使用(若是远程部署,请访问 `http://<您的服务器IP>:3000`)。
5. 打开浏览器,访问 `http://localhost:3000` 即可开始使用(若是远程部署,请访问 `http://<你的服务器IP>:3000`)。
#### **扩展转码 Worker(并行处理)**
默认情况下,Docker Compose 部署会按顺序逐个处理视频转码任务。这是出于系统设计考虑,因为视频转码属于计算密集型任务,容易耗尽可用的 CPU 或 GPU 资源。
但在批量上传(例如一次性上传 100+ 个视频)或处理需要耗费数小时的大型原始视频素材时,长时间的转码任务可能会阻塞后续文件的处理。此时,你可以通过增加额外的机器来横向扩展转码吞吐量。每台机器依然按顺序执行任务,但增加 worker 实例能够显著提升整体的并行处理能力。
Shumai 提供了独立的转码 Worker 镜像(支持 Docker Hub 与阿里云镜像源):
1. **连接准备**:启动一台临时机器,确保其能够正常访问你的 PostgreSQL 数据库和存储。
2. **启动 Worker**:配置相同的数据库与存储连接凭据并运行容器:
**阿里云镜像(国内推荐):**
```bash
docker run -d --name shumai_transcode_worker \
-e DATABASE_URL="postgres://shumai:shumai_password@<postgres-host>:5432/shumai_db?sslmode=disable" \
-e STORAGE_BACKEND="s3" \
-e S3_BUCKET="your-bucket-name" \
-e AWS_ENDPOINT_URL_S3="https://s3.us-east-1.amazonaws.com" \
-e S3_ACCESS_KEY_ID="your-access-key-id" \
-e S3_SECRET_ACCESS_KEY="your-secret-access-key" \
crpi-000nkafzu6xvpwsc.cn-hongkong.personal.cr.aliyuncs.com/shumai-one/shumai-transcode:latest
```
Worker 启动后会自动接入任务队列,并与主实例并行消费并处理积压的转码任务。
3. **随时停止**:当转码队列处理完毕(或不再需要额外的算力)时,只需停止或移除 Worker 容器:
```bash
docker stop shumai_transcode_worker
```
主 Shumai 服务的运行不会受到任何影响,不需要停机。
### 方案二:源码编译运行(开发环境)
如果您希望在本地参与 Shumai 的开发,请按以下步骤配置开发环境:
如果你希望在本地参与 Shumai 的开发,请按以下步骤配置开发环境:
1. 克隆代码仓库并安装依赖项:
```bash
@@ -190,6 +226,7 @@ https://gh-proxy.com/https://raw.githubusercontent.com/shumaiOne/shumai/main/REA
```env
DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public
BETTER_AUTH_SECRET=ySxs7DxzHDZBbeeHNPEwBuspYwipBqz5Gk5XdBjNhWw=
BETTER_AUTH_URL=http://localhost:3000
STORAGE_BACKEND=local
SHUMAI_SERVER_PORT=3000
AWS_ENDPOINT_URL_S3=http://localhost:3000
@@ -221,6 +258,16 @@ https://gh-proxy.com/https://raw.githubusercontent.com/shumaiOne/shumai/main/REA
## 命令行工具 (CLI)
Shumai 自带一个功能完备的命令行工具(CLI),支持您直接在终端里高效管理项目、文件夹、资产,批量上传文件/目录,以及快速创建新版本。
Shumai 自带一个功能完备的命令行工具(CLI),支持你直接在终端里高效管理项目、文件夹、素材,批量上传文件/目录,以及快速创建新版本。
关于 CLI 的安装与详细使用指南,请参阅 [CLI Readme](https://www.google.com/search?q=apps/cli/README.md)。
关于 CLI 的安装与详细使用指南,请参阅 [CLI Readme](apps/cli/README.md)。
## MCP 服务器
Shumai 内置 MCP 服务器(地址为 `/mcp`),Claude Code、Cursor、Kiro 等外部智能体可以使用 API 令牌连接,浏览并整理你的项目。配置方法请参阅 [MCP 服务器指南](docs/developer/mcp-server.mdx)。
---
## 社区
感谢 [LINUX DO](https://linux.do/) 提供开源交流社区,欢迎佬友交流使用体验与改进建议。
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/agent-app",
"version": "0.6.0",
"version": "0.6.2",
"private": true,
"type": "module",
"bin": {
+130 -4
View File
@@ -2,13 +2,13 @@
A Command Line Interface (CLI) tool for interacting with your Shumai instance.
The CLI allows you to view projects, list folder contents, create folders, upload files and directories, and create new versions of existing files directly from the terminal.
The CLI allows you to view projects, list folder contents, create folders, upload files and directories, create new versions of existing files, and look at images, video frames, and document pages directly from the terminal.
---
## Installation
The CLI package is bundled inside the Shumai workspace.
The CLI package is bundled inside the Shumai workspace. The published npm package (`@shumai-one/shumai-cli`) requires Node.js 22.12 or later.
### Environment Setup
@@ -18,7 +18,8 @@ Before using the CLI, you must set the following environment variables to authen
# The endpoint of your Shumai API server (e.g. http://localhost:3000)
export SHUMAI_API_SERVER="http://localhost:3000"
# Your personal API Token (Generate this in the WebUI via Settings -> Developer tab)
# Your personal API Token (Generate this in the WebUI via Settings -> API Tokens tab).
# The token is shown only once when it is created, so copy it right away.
export SHUMAI_API_KEY="your-developer-api-token"
# Optional: Enable asset deletion capability (disabled by default for safety)
@@ -38,6 +39,14 @@ Run `shumai-cli` or query specific commands:
shumai-cli <command> [arguments] [options]
```
Run `shumai-cli --help` to list all commands, and `shumai-cli <command> --help` for a command's arguments and options.
### Global Options
- `--json`: Print the raw JSON result instead of human-friendly text, for scripts and tools like `jq`. Example: `shumai-cli ls -p <parentId> --json | jq '.data[].id'`.
When the output is piped instead of shown in a terminal, tables are printed as tab-separated values without headers.
### Commands
#### 1. List Projects
@@ -50,7 +59,7 @@ shumai-cli project ls
#### 2. List Directory Contents
List direct children (folders and files) of a parent folder.
List direct children (folders and files) of a parent folder. Folders are listed first.
```bash
shumai-cli ls -p <parentId>
@@ -58,6 +67,9 @@ shumai-cli ls -p <parentId>
- **Options:**
- `-p`, `--parent <id>`: The parent folder ID (required).
- `--type <type>`: Only list `file`, `folder`, or `all` (default).
- `--first <n>`: Maximum number of items to list (default and maximum: 200).
- `--after <cursor>`: Continue a long listing. When more items exist, `ls` prints the cursor to pass here.
#### 3. Create a New Folder
@@ -123,6 +135,116 @@ shumai-cli delete <assetId>
shumai-cli rm <assetId>
```
#### 9. Read an Asset
Look at a file: an image, video frames, PDF pages, a 3D model view, or the text of a text file. Prints a summary and a link to each image (links are valid for about an hour), or the file's text.
```bash
# An image, or the 4-view overview of a 3D model
shumai-cli read <assetId>
# 5 evenly spaced frames between 0s and 30s of a video, downloaded into ./frames
shumai-cli read <assetId> --video-start 0 --video-end 30 --video-count 5 --out frames
# Pages 1 to 3 of a PDF, or the text of a text file
shumai-cli read <assetId> --doc-mode pages --doc-start-page 1 --doc-end-page 3
shumai-cli read <assetId> --doc-mode text
# A 3D model at 90°
shumai-cli read <assetId> --degree 90
```
- **Options:**
- `--video-start <s>`, `--video-end <s>`, `--video-count <n>`: Video frames to extract (up to 20).
- `--doc-mode <pages|text>`, `--doc-start-page <n>`, `--doc-end-page <n>`: PDF pages to render (up to 20), or `text` to read a text file.
- `--degree <deg>`: 3D model angle (0, 15, 30, ..., 345).
- `--annotation-id <id>`: Draw a comment's markup on the image, frame, or page.
- `-o`, `--out <dir>`: Download the images into a folder and print their paths. With `--json`, each image also gets a `path`.
- `--max-wait-seconds <s>`, `--no-wait`: Rendering frames or pages can take a while. `read` waits up to 2 minutes; use these to wait less or return right away. If the render isn't done, `read` prints the `render` command to run next.
#### 10. Get a Render
Get the result of a `read` that was still rendering. Takes the same `--out`, `--max-wait-seconds`, and `--no-wait` options as `read`.
```bash
shumai-cli render <taskId> --out frames
```
#### 11. Download Files
Download up to 50 files at once. By default it saves the web preview version when there is one (for example the transcoded video or the web image), which is smaller; use `--original` for the uploaded file. A version stack downloads its latest version. Folders aren't supported: download the files inside them. Files keep their names; if a name is taken, the new file gets a number, like `clip (2).mp4`.
```bash
shumai-cli download <assetId...> --out ./downloads
shumai-cli download <assetId> --original
```
- **Options:**
- `-o`, `--out <dir>`: Folder to save the files in (default: the current folder). With `--json`, each file also gets a `path`.
- `--original`: Download the original uploaded files instead of the web previews.
#### 12. Search Files
Search a folder and all of its subfolders by name, or by meaning with `--semantic` (when AI search is enabled for the team).
```bash
shumai-cli search <query> -p <folderId>
shumai-cli search "a calm beach at sunset" -p <folderId> --semantic
```
- **Options:**
- `-p`, `--parent <id>`: The folder to search in (required).
- `--type <file|folder>`: Search files (default) or folders.
- `--first <n>`, `--after <cursor>`: Page through many results.
#### 13. Copy Assets
Copy files or folders into another folder of the same team. Prints the IDs of the copies.
```bash
shumai-cli copy <assetId...> -p <parentId> [--with-comments]
# or using the short alias:
shumai-cli cp <assetId...> -p <parentId>
```
#### 14. Comments
```bash
# List the comments on a file, with their replies
shumai-cli comment ls <assetId>
# Comment on a file, optionally at a timecode (seconds), or reply to a comment
shumai-cli comment add <assetId> "The logo is too small" --at 12.5
shumai-cli comment add <assetId> "Fixed in v2" --reply-to <commentId>
# Mark a comment as resolved, or unresolved again
shumai-cli comment resolve <commentId>
shumai-cli comment resolve <commentId> --reopen
```
#### 15. Metadata
```bash
# List a project's metadata fields and their choices
shumai-cli field ls <projectId>
# Set metadata values on a file, as JSON keyed by field key (select fields take an option id)
shumai-cli meta set <assetId> --values '{"status": "approved", "rating": 4}'
```
#### 16. Version Stacks
```bash
# Stack files in the same folder as versions; the first file is the latest
shumai-cli version stack <fileId...>
# List the versions in a stack, latest first
shumai-cli version ls <stackId>
# Take a version out of its stack (the file is kept)
shumai-cli version unstack <versionId>
```
#### 17. Folder Instructions
Show a folder's instructions for AI agents (its AGENTS.md).
```bash
shumai-cli instructions <folderId>
```
---
## Development
@@ -132,3 +254,7 @@ To run the CLI in development mode inside the repository source code:
```bash
bun run apps/cli/src/index.ts project ls
```
### Adding Commands
Most commands are generated from the shared Operation definitions in `packages/operations` (see "Operations" in `AGENTS.md`). An Operation with the `cli` surface and a `cli` spec becomes a command automatically, with arguments, options, and help text derived from its input schema (key/value inputs such as `meta set --values` take a JSON object). It is also available to Shumai's built-in agent and MCP server. Behavior that needs the local machine, such as downloading files, goes in a CLI extension in `src/operations/extensions/` (extra options, input mapping, and output). Only commands that stream local files (`upload`, `create-version`) are written by hand in `src/program.ts`; the `create_file`, `create_version`, and `complete_upload` Operations are for the agent and MCP only.
+5 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/cli",
"version": "0.6.0",
"version": "0.6.2",
"private": true,
"type": "module",
"bin": {
@@ -8,10 +8,12 @@
},
"dependencies": {
"@shumai/api": "workspace:*",
"@shumai/core": "workspace:*",
"@shumai/dtos": "workspace:*",
"@shumai/operations": "workspace:*",
"@shumai/tableprinter": "workspace:*",
"commander": "15.0.0",
"hono": "4.13.5",
"ulid": "3.0.2"
"ulid": "3.0.2",
"zod": "4.5.4"
}
}
+14 -2
View File
@@ -1,7 +1,13 @@
import { hc } from 'hono/client'
import type { AppType } from '@shumai/api'
export function getClient() {
export interface CliConfig {
apiServer: string
apiKey: string
}
/** Reads the server URL and API token from the environment, exiting with guidance if missing. */
export function getCliConfig(): CliConfig {
const apiKey = process.env.SHUMAI_API_KEY
const apiServer = process.env.SHUMAI_API_SERVER
@@ -12,11 +18,17 @@ export function getClient() {
if (!apiKey) {
console.error(
'Error: SHUMAI_API_KEY environment variable is not set. Please generate a token in the Settings -> Developer tab.',
'Error: SHUMAI_API_KEY environment variable is not set. Please generate a token in the Settings -> API Tokens tab.',
)
process.exit(1)
}
return { apiServer: apiServer.replace(/\/+$/, ''), apiKey }
}
/** Typed REST client, used by commands that are not Operations yet (upload, create-version). */
export function getClient() {
const { apiServer, apiKey } = getCliConfig()
return hc<AppType>(apiServer, {
headers: {
Authorization: `Bearer ${apiKey}`,
+1 -261
View File
@@ -1,13 +1,7 @@
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
import { projectLs } from './project'
import { ls } from './ls'
import { mkdir } from './mkdir'
import { getClient } from '../client'
import { upload } from './upload'
import { createVersion } from './create-version'
import { rename } from './rename'
import { move } from './move'
import { deleteAsset } from './delete'
import fs from 'node:fs'
import http from 'node:http'
@@ -21,7 +15,7 @@ vi.mock('node:http', () => ({
},
}))
describe('CLI Commands', () => {
describe('CLI upload commands', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let logSpy: any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -103,72 +97,6 @@ describe('CLI Commands', () => {
}
})
describe('project ls', () => {
it('prints list of projects correctly', async () => {
mockClient.api.projects.$get.mockResolvedValue({
ok: true,
json: async () => ({
data: [
{ id: 'p1', name: 'Proj 1', rootFolder: 'root1' },
{ id: 'p2', name: 'Proj 2', rootFolder: 'root2' },
],
}),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
await projectLs()
expect(mockClient.api.projects.$get).toHaveBeenCalledWith({
query: { first: '200' },
})
expect(writeSpy).toHaveBeenCalledWith(expect.stringContaining('Proj 1'))
expect(writeSpy).toHaveBeenCalledWith(expect.stringContaining('Proj 2'))
})
})
describe('ls', () => {
it('queries folders and files and displays table', async () => {
mockClient.api.folders[':folderId'].children.$get
.mockResolvedValueOnce({
ok: true,
json: async () => ({
data: [{ id: 'subfolder1', name: 'Sub 1', type: 'folder', sizeByte: 0 }],
}),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
.mockResolvedValueOnce({
ok: true,
json: async () => ({
data: [{ id: 'file1', name: 'doc.pdf', type: 'file', sizeByte: 1024 }],
}),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
await ls('parentId123')
expect(mockClient.api.folders[':folderId'].children.$get).toHaveBeenCalledTimes(2)
expect(writeSpy).toHaveBeenCalledWith(expect.stringContaining('Sub 1'))
expect(writeSpy).toHaveBeenCalledWith(expect.stringContaining('doc.pdf'))
})
})
describe('mkdir', () => {
it('calls POST /folders and prints the folder ID', async () => {
mockClient.api.folders.$post.mockResolvedValue({
ok: true,
json: async () => ({ id: 'new_folder_ulid' }),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
await mkdir('NewFolder', 'parent_ulid')
expect(mockClient.api.folders.$post).toHaveBeenCalledWith({
json: { name: 'NewFolder', parentId: 'parent_ulid' },
})
expect(logSpy).toHaveBeenCalledWith('Created folder with ID: new_folder_ulid')
})
})
describe('upload', () => {
it('uploads a file successfully and confirms the task', async () => {
const tempFile = 'temp-test-file.txt'
@@ -383,192 +311,4 @@ describe('CLI Commands', () => {
}
})
})
describe('rename', () => {
it('exits with error when assetId or newName is missing', async () => {
await expect(rename('', 'new-name')).rejects.toThrow('process.exit(1)')
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Asset ID is required'))
await expect(rename('asset-1', '')).rejects.toThrow('process.exit(1)')
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('New name is required'))
})
it('renames a folder when asset type is folder', async () => {
mockClient.api.folders[':folderId'].$get.mockResolvedValue({
ok: true,
json: async () => ({ id: 'f-1', type: 'folder' }),
} as unknown as Response)
mockClient.api.folders[':folderId'].$put.mockResolvedValue({
ok: true,
json: async () => ({ id: 'f-1', name: 'Renamed Folder' }),
} as unknown as Response)
await rename('f-1', 'Renamed Folder')
expect(mockClient.api.folders[':folderId'].$put).toHaveBeenCalledWith({
param: { folderId: 'f-1' },
json: { name: 'Renamed Folder' },
})
expect(logSpy).toHaveBeenCalledWith('Renamed asset f-1 to "Renamed Folder"')
})
it('renames a file when asset type is file', async () => {
mockClient.api.folders[':folderId'].$get.mockResolvedValue({
ok: true,
json: async () => ({ id: 'file-1', type: 'file' }),
} as unknown as Response)
mockClient.api.files[':fileId'].$put.mockResolvedValue({
ok: true,
json: async () => ({ id: 'file-1', name: 'new.mp4' }),
} as unknown as Response)
await rename('file-1', 'new.mp4')
expect(mockClient.api.files[':fileId'].$put).toHaveBeenCalledWith({
param: { fileId: 'file-1' },
json: { name: 'new.mp4' },
})
expect(logSpy).toHaveBeenCalledWith('Renamed asset file-1 to "new.mp4"')
})
it('exits with error when asset fetch fails', async () => {
mockClient.api.folders[':folderId'].$get.mockResolvedValue({
ok: false,
json: async () => ({ error: 'Asset not found' }),
} as unknown as Response)
await expect(rename('unknown-id', 'name')).rejects.toThrow('process.exit(1)')
expect(errorSpy).toHaveBeenCalledWith('Error: Asset not found')
})
})
describe('move', () => {
it('exits with error when assetIds or parentId is missing', async () => {
await expect(move([], 'parent-1')).rejects.toThrow('process.exit(1)')
expect(errorSpy).toHaveBeenCalledWith(
expect.stringContaining('At least one asset ID is required'),
)
await expect(move(['a-1'], '')).rejects.toThrow('process.exit(1)')
expect(errorSpy).toHaveBeenCalledWith(
expect.stringContaining('Option -p/--parent <parentId> is required'),
)
})
it('successfully moves assets', async () => {
mockClient.api.folders[':folderId'].$get.mockResolvedValue({
ok: true,
json: async () => ({ id: 'parent-1', projectId: 'proj-1' }),
} as unknown as Response)
mockClient.api.projects[':projectId'].reparent.$post.mockResolvedValue({
ok: true,
} as unknown as Response)
await move(['a-1', 'a-2'], 'parent-1')
expect(mockClient.api.projects[':projectId'].reparent.$post).toHaveBeenCalledWith({
param: { projectId: 'proj-1' },
json: {
assetIds: ['a-1', 'a-2'],
newParentId: 'parent-1',
},
})
expect(logSpy).toHaveBeenCalledWith('Moved 2 asset(s) to parent parent-1')
})
it('exits with error when destination parent is not found', async () => {
mockClient.api.folders[':folderId'].$get.mockResolvedValue({
ok: false,
json: async () => ({ error: 'Folder not found' }),
} as unknown as Response)
await expect(move(['a-1'], 'missing-parent')).rejects.toThrow('process.exit(1)')
expect(errorSpy).toHaveBeenCalledWith('Error: Folder not found')
})
})
describe('deleteAsset', () => {
it('exits with error when SHUMAI_ALLOW_DELETE is not set or not true', async () => {
delete process.env.SHUMAI_ALLOW_DELETE
await expect(deleteAsset(['a-1'])).rejects.toThrow('process.exit(1)')
expect(errorSpy).toHaveBeenCalledWith(
'Error: Asset deletion is disabled in this environment.',
)
})
it('exits with error when assetIds is empty', async () => {
process.env.SHUMAI_ALLOW_DELETE = 'true'
await expect(deleteAsset([])).rejects.toThrow('process.exit(1)')
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Asset ID is required'))
})
it('exits with error when more than one asset ID is passed', async () => {
process.env.SHUMAI_ALLOW_DELETE = 'true'
await expect(deleteAsset(['a-1', 'a-2'])).rejects.toThrow('process.exit(1)')
expect(errorSpy).toHaveBeenCalledWith('Error: Can only delete one asset at a time.')
})
it('exits with error when asset is not found', async () => {
process.env.SHUMAI_ALLOW_DELETE = 'true'
mockClient.api.folders[':folderId'].$get.mockResolvedValue({
ok: false,
json: async () => ({ error: 'Asset not found' }),
} as unknown as Response)
await expect(deleteAsset(['a-1'])).rejects.toThrow('process.exit(1)')
expect(errorSpy).toHaveBeenCalledWith('Error: Asset not found')
})
it('successfully deletes a file when SHUMAI_ALLOW_DELETE is true', async () => {
process.env.SHUMAI_ALLOW_DELETE = 'true'
mockClient.api.folders[':folderId'].$get.mockResolvedValue({
ok: true,
json: async () => ({ id: 'a-1', type: 'file' }),
} as unknown as Response)
mockClient.api.files.$delete.mockResolvedValue({
ok: true,
} as unknown as Response)
await deleteAsset(['a-1'])
expect(mockClient.api.files.$delete).toHaveBeenCalledWith({
json: { ids: ['a-1'] },
})
expect(mockClient.api.folders.$delete).not.toHaveBeenCalled()
expect(logSpy).toHaveBeenCalledWith('Deleted asset a-1')
})
it('successfully deletes a folder when SHUMAI_ALLOW_DELETE is 1', async () => {
process.env.SHUMAI_ALLOW_DELETE = '1'
mockClient.api.folders[':folderId'].$get.mockResolvedValue({
ok: true,
json: async () => ({ id: 'folder-1', type: 'folder' }),
} as unknown as Response)
mockClient.api.folders.$delete.mockResolvedValue({
ok: true,
} as unknown as Response)
await deleteAsset(['folder-1'])
expect(mockClient.api.folders.$delete).toHaveBeenCalledWith({
json: { ids: ['folder-1'] },
})
expect(logSpy).toHaveBeenCalledWith('Deleted asset folder-1')
})
it('exits with error when delete API returns an error', async () => {
process.env.SHUMAI_ALLOW_DELETE = 'true'
mockClient.api.folders[':folderId'].$get.mockResolvedValue({
ok: true,
json: async () => ({ id: 'a-1', type: 'file' }),
} as unknown as Response)
mockClient.api.files.$delete.mockResolvedValue({
ok: false,
json: async () => ({ error: 'Failed to delete' }),
} as unknown as Response)
await expect(deleteAsset(['a-1'])).rejects.toThrow('process.exit(1)')
expect(errorSpy).toHaveBeenCalledWith('Error: Failed to delete')
})
})
})
-62
View File
@@ -1,62 +0,0 @@
import { getClient } from '../client'
export async function deleteAsset(assetIds: string[]) {
const isAllowed =
process.env.SHUMAI_ALLOW_DELETE === 'true' || process.env.SHUMAI_ALLOW_DELETE === '1'
if (!isAllowed) {
console.error('Error: Asset deletion is disabled in this environment.')
process.exit(1)
}
if (!assetIds || assetIds.length === 0) {
console.error('Error: Asset ID is required. Usage: shumai-cli delete <assetId>')
process.exit(1)
}
if (assetIds.length > 1) {
console.error('Error: Can only delete one asset at a time.')
process.exit(1)
}
const assetId = assetIds[0]
const client = getClient()
try {
// Inspect asset to determine whether it is a folder or file
const getRes = await client.api.folders[':folderId'].$get({
param: { folderId: assetId },
})
if (!getRes.ok) {
const err = (await getRes.json().catch(() => ({ error: 'Asset not found' }))) as {
error?: string
}
console.error(`Error: ${err.error || 'Asset not found'}`)
process.exit(1)
}
const asset = await getRes.json()
const isFolder = asset.type === 'folder'
const res = isFolder
? await client.api.folders.$delete({
json: { ids: [assetId] },
})
: await client.api.files.$delete({
json: { ids: [assetId] },
})
if (!res.ok) {
const err = (await res.json().catch(() => ({ error: 'Failed to delete asset' }))) as {
error?: string
}
console.error(`Error: ${err.error || 'Failed to delete asset'}`)
process.exit(1)
}
console.log(`Deleted asset ${assetId}`)
} catch (err) {
console.error('Error connecting to API server:', err instanceof Error ? err.message : err)
process.exit(1)
}
}
-87
View File
@@ -1,87 +0,0 @@
import { newTablePrinter, withTruncate } from '@shumai/tableprinter'
import type { AssetInfo } from '@shumai/dtos'
import { getClient } from '../client'
function formatSize(bytes: number): string {
if (bytes === 0) return '-'
const k = 1024
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}
export async function ls(parentId: string) {
if (!parentId) {
console.error('Error: Parent ID (-p/--parent) is required.')
process.exit(1)
}
const client = getClient()
try {
const [foldersRes, filesRes] = await Promise.all([
client.api.folders[':folderId'].children.$get({
param: { folderId: parentId },
query: { assetType: 'folder', first: '200' },
}),
client.api.folders[':folderId'].children.$get({
param: { folderId: parentId },
query: { assetType: 'file', first: '200' },
}),
])
if (!foldersRes.ok) {
const err = (await foldersRes.json().catch(() => ({ error: 'Unknown error' }))) as {
error?: string
}
console.error(`Error: ${err.error || 'Failed to list folders'}`)
process.exit(1)
}
if (!filesRes.ok) {
const err = (await filesRes.json().catch(() => ({ error: 'Unknown error' }))) as {
error?: string
}
console.error(`Error: ${err.error || 'Failed to list files'}`)
process.exit(1)
}
const foldersData = await foldersRes.json()
const filesData = await filesRes.json()
const folders = foldersData.data || []
const files = filesData.data || []
const items = [
...folders.map((f: AssetInfo) => ({ ...f, typeDisplay: 'folder' as const })),
...files.map((f: AssetInfo) => ({ ...f, typeDisplay: 'file' as const })),
]
if (items.length === 0) {
console.log('No assets found in this folder.')
return
}
const isTty = process.stdout.isTTY
const maxWidth = process.stdout.columns || 80
const tp = newTablePrinter(process.stdout, isTty, maxWidth)
tp.addHeader(['ID', 'Name', 'Type', 'Size'])
for (const item of items) {
tp.addField(item.id || '', withTruncate(null))
tp.addField(item.name || '')
tp.addField(item.typeDisplay, withTruncate(null))
tp.addField(
item.typeDisplay === 'folder' ? '-' : formatSize(item.sizeByte || 0),
withTruncate(null),
)
tp.endRow()
}
tp.render()
} catch (err) {
console.error('Error connecting to API server:', err instanceof Error ? err.message : err)
process.exit(1)
}
}
-32
View File
@@ -1,32 +0,0 @@
import { getClient } from '../client'
export async function mkdir(name: string, parentId: string) {
if (!name) {
console.error('Error: Folder name is required.')
process.exit(1)
}
if (!parentId) {
console.error('Error: Parent ID (-p/--parent) is required.')
process.exit(1)
}
const client = getClient()
try {
const res = await client.api.folders.$post({
json: { name, parentId },
})
if (!res.ok) {
const err = (await res.json().catch(() => ({ error: 'Unknown error' }))) as { error?: string }
console.error(`Error: ${err.error || 'Failed to create folder'}`)
process.exit(1)
}
const created = await res.json()
console.log(`Created folder with ID: ${created.id}`)
} catch (err) {
console.error('Error connecting to API server:', err instanceof Error ? err.message : err)
process.exit(1)
}
}
-59
View File
@@ -1,59 +0,0 @@
import { getClient } from '../client'
export async function move(assetIds: string[], parentId: string) {
if (!assetIds || assetIds.length === 0) {
console.error(
'Error: At least one asset ID is required. Usage: shumai-cli move <assetId...> -p <parentId>',
)
process.exit(1)
}
if (!parentId) {
console.error('Error: Option -p/--parent <parentId> is required.')
process.exit(1)
}
const client = getClient()
try {
// Fetch destination folder to get its projectId
const parentRes = await client.api.folders[':folderId'].$get({
param: { folderId: parentId },
})
if (!parentRes.ok) {
const err = (await parentRes.json().catch(() => ({ error: 'Parent folder not found' }))) as {
error?: string
}
console.error(`Error: ${err.error || 'Parent folder not found'}`)
process.exit(1)
}
const parentInfo = await parentRes.json()
const projectId = parentInfo.projectId
if (!projectId) {
console.error('Error: Destination parent folder is not associated with a project.')
process.exit(1)
}
const res = await client.api.projects[':projectId'].reparent.$post({
param: { projectId },
json: {
assetIds,
newParentId: parentId,
},
})
if (!res.ok) {
const err = (await res.json().catch(() => ({ error: 'Failed to move assets' }))) as {
error?: string
}
console.error(`Error: ${err.error || 'Failed to move assets'}`)
process.exit(1)
}
console.log(`Moved ${assetIds.length} asset(s) to parent ${parentId}`)
} catch (err) {
console.error('Error connecting to API server:', err instanceof Error ? err.message : err)
process.exit(1)
}
}
-42
View File
@@ -1,42 +0,0 @@
import { newTablePrinter } from '@shumai/tableprinter'
import { getClient } from '../client'
export async function projectLs() {
const client = getClient()
try {
const res = await client.api.projects.$get({
query: { first: '200' },
})
if (!res.ok) {
const err = (await res.json().catch(() => ({ error: 'Unknown error' }))) as { error?: string }
console.error(`Error: ${err.error || 'Failed to list projects'}`)
process.exit(1)
}
const { data: projects } = await res.json()
if (projects.length === 0) {
console.log('No projects found.')
return
}
const isTty = process.stdout.isTTY
const maxWidth = process.stdout.columns || 80
const tp = newTablePrinter(process.stdout, isTty, maxWidth)
tp.addHeader(['ID', 'Name', 'Root Folder ID'])
for (const p of projects) {
tp.addField(p.id || '')
tp.addField(p.name || '')
tp.addField(p.rootFolder || '')
tp.endRow()
}
tp.render()
} catch (err) {
console.error('Error connecting to API server:', err instanceof Error ? err.message : err)
process.exit(1)
}
}
-55
View File
@@ -1,55 +0,0 @@
import { getClient } from '../client'
export async function rename(assetId: string, newName: string) {
if (!assetId) {
console.error('Error: Asset ID is required. Usage: shumai-cli rename <assetId> <newName>')
process.exit(1)
}
if (!newName) {
console.error('Error: New name is required. Usage: shumai-cli rename <assetId> <newName>')
process.exit(1)
}
const client = getClient()
try {
// Determine whether asset is a folder or a file
const getRes = await client.api.folders[':folderId'].$get({
param: { folderId: assetId },
})
if (!getRes.ok) {
const err = (await getRes.json().catch(() => ({ error: 'Asset not found' }))) as {
error?: string
}
console.error(`Error: ${err.error || 'Asset not found'}`)
process.exit(1)
}
const asset = await getRes.json()
const isFolder = asset.type === 'folder'
const updateRes = isFolder
? await client.api.folders[':folderId'].$put({
param: { folderId: assetId },
json: { name: newName },
})
: await client.api.files[':fileId'].$put({
param: { fileId: assetId },
json: { name: newName },
})
if (!updateRes.ok) {
const err = (await updateRes.json().catch(() => ({ error: 'Failed to rename asset' }))) as {
error?: string
}
console.error(`Error: ${err.error || 'Failed to rename asset'}`)
process.exit(1)
}
console.log(`Renamed asset ${assetId} to "${newName}"`)
} catch (err) {
console.error('Error connecting to API server:', err instanceof Error ? err.message : err)
process.exit(1)
}
}
+7 -138
View File
@@ -1,140 +1,9 @@
import { projectLs } from './commands/project'
import { ls } from './commands/ls'
import { mkdir } from './commands/mkdir'
import { upload } from './commands/upload'
import { createVersion } from './commands/create-version'
import { rename } from './commands/rename'
import { move } from './commands/move'
import { deleteAsset } from './commands/delete'
import { formatCliError } from './operations/errors'
import { createProgram } from './program'
function printHelp() {
console.log(`
Usage: shumai-cli <command> [arguments] [options]
Commands:
project ls List all projects (returns id, name, root folder id)
ls -p <parentId> List direct children of a parent folder (returns id, name, type, size)
mkdir <name> -p <parentId> Create a new folder in a parent folder
upload <path> -p <parentId> Upload a local file or folder to a parent folder
create-version <path> -p <id> Create a new version of an existing file or version stack
rename <assetId> <newName> Rename an existing file or folder
move <assetIds...> -p <target> Move one or more assets to a destination parent folder (alias: mv)
delete <assetId> Delete a single file or folder (alias: rm)
Options:
-p, --parent <id> The parent folder/asset ID (required for ls, mkdir, upload, create-version, move)
-h, --help Show help details
`)
}
async function main() {
const args = process.argv.slice(2)
if (args.length === 0 || args.includes('-h') || args.includes('--help')) {
printHelp()
process.exit(0)
}
const cmd = args[0]
let parentId = ''
const parentIdx = args.findIndex((a) => a === '-p' || a === '--parent')
if (parentIdx !== -1 && parentIdx + 1 < args.length) {
parentId = args[parentIdx + 1]
}
const cleanArgs: string[] = []
for (let i = 0; i < args.length; i++) {
if (args[i] === '-p' || args[i] === '--parent') {
i++
} else {
cleanArgs.push(args[i])
}
}
if (cmd === 'project' && cleanArgs[1] === 'ls') {
await projectLs()
} else if (cmd === 'ls') {
if (!parentId) {
console.error('Error: Option -p/--parent <parentId> is required.')
process.exit(1)
}
await ls(parentId)
} else if (cmd === 'mkdir') {
const name = cleanArgs[1]
if (!name) {
console.error(
'Error: Folder name is required. Usage: shumai-cli mkdir <folderName> -p <parentId>',
)
process.exit(1)
}
if (!parentId) {
console.error('Error: Option -p/--parent <parentId> is required.')
process.exit(1)
}
await mkdir(name, parentId)
} else if (cmd === 'upload') {
const localPath = cleanArgs[1]
if (!localPath) {
console.error(
'Error: File/folder path is required. Usage: shumai-cli upload <localPath> -p <parentId>',
)
process.exit(1)
}
if (!parentId) {
console.error('Error: Option -p/--parent <parentId> is required.')
process.exit(1)
}
await upload(localPath, parentId)
} else if (cmd === 'create-version') {
const localPath = cleanArgs[1]
if (!localPath) {
console.error(
'Error: Local file path is required. Usage: shumai-cli create-version <localPath> -p <parentAssetId>',
)
process.exit(1)
}
if (!parentId) {
console.error('Error: Option -p/--parent <parentAssetId> is required.')
process.exit(1)
}
await createVersion(localPath, parentId)
} else if (cmd === 'rename') {
const assetId = cleanArgs[1]
const newName = cleanArgs[2]
if (!assetId || !newName) {
console.error(
'Error: Asset ID and new name are required. Usage: shumai-cli rename <assetId> <newName>',
)
process.exit(1)
}
await rename(assetId, newName)
} else if (cmd === 'move' || cmd === 'mv') {
const assetIds = cleanArgs.slice(1)
if (assetIds.length === 0) {
console.error(
'Error: At least one asset ID is required. Usage: shumai-cli move <assetId...> -p <parentId>',
)
process.exit(1)
}
if (!parentId) {
console.error('Error: Option -p/--parent <parentId> is required.')
process.exit(1)
}
await move(assetIds, parentId)
} else if (cmd === 'delete' || cmd === 'rm') {
const assetIds = cleanArgs.slice(1)
if (assetIds.length === 0) {
console.error('Error: Asset ID is required. Usage: shumai-cli delete <assetId>')
process.exit(1)
}
await deleteAsset(assetIds)
} else {
console.error(`Error: Unknown command "${cmd}". Run "shumai-cli --help" for usage.`)
createProgram()
.parseAsync(process.argv)
.catch((err: unknown) => {
console.error(formatCliError(err))
process.exit(1)
}
}
main().catch((err) => {
console.error('Unexpected error:', err)
process.exit(1)
})
})
@@ -0,0 +1,78 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createFolder, listProjects } from '@shumai/operations'
import { callOperation } from './call-operation'
import { CliError } from './errors'
describe('callOperation', () => {
const env = { ...process.env }
const fetchMock = vi.fn<typeof fetch>()
beforeEach(() => {
process.env.SHUMAI_API_SERVER = 'http://shumai.test/'
process.env.SHUMAI_API_KEY = 'shumai_token'
vi.stubGlobal('fetch', fetchMock)
})
afterEach(() => {
process.env = { ...env }
vi.unstubAllGlobals()
fetchMock.mockReset()
})
const json = (body: unknown, status = 200) =>
new Response(JSON.stringify(body), {
status,
headers: { 'Content-Type': 'application/json' },
})
it('posts the input to /api/ops/:name with the token and CLI attribution', async () => {
const folder = { id: 'f1', name: 'Renders', type: 'folder', size: 0, projectId: 'p1' }
fetchMock.mockResolvedValue(json(folder))
const output = await callOperation(createFolder, { parent: 'root', name: 'Renders' })
expect(output).toEqual(folder)
const [url, init] = fetchMock.mock.calls[0]
expect(url).toBe('http://shumai.test/api/ops/create_folder')
expect(init).toMatchObject({
method: 'POST',
headers: {
Authorization: 'Bearer shumai_token',
'Content-Type': 'application/json',
'x-shumai-client': 'cli',
},
body: JSON.stringify({ parent: 'root', name: 'Renders' }),
})
})
it('throws the server error message', async () => {
fetchMock.mockResolvedValue(json({ error: 'User is not a member of the team' }, 403))
await expect(callOperation(listProjects, {})).rejects.toEqual(
new CliError('User is not a member of the team'),
)
})
it('falls back to the status when the error body is not JSON', async () => {
fetchMock.mockResolvedValue(new Response('Bad gateway', { status: 502 }))
await expect(callOperation(listProjects, {})).rejects.toThrow('Request failed with status 502')
})
it('reports connection failures', async () => {
fetchMock.mockRejectedValue(new TypeError('fetch failed'))
await expect(callOperation(listProjects, {})).rejects.toMatchObject({
message: 'Error connecting to API server: fetch failed',
prefixed: true,
})
})
it('rejects responses that do not match the output schema', async () => {
fetchMock.mockResolvedValue(json({ unexpected: true }))
await expect(callOperation(listProjects, {})).rejects.toThrow(
'Unexpected response from server for list_projects',
)
})
})
+55
View File
@@ -0,0 +1,55 @@
import type { z } from 'zod'
import type { AnyOperationDefinition } from '@shumai/operations'
import { getCliConfig } from '../client'
import { CliError } from './errors'
/** Header that attributes invocations to the CLI (`via: cli`) in audit logs. */
const CLIENT_HEADER = 'x-shumai-client'
/** Signature used by generated commands, which only see type-erased definitions. */
export type CallOperation = (
definition: AnyOperationDefinition,
input: Record<string, unknown>,
) => Promise<unknown>
/** Runs an Operation on the server via `POST /api/ops/:name`. */
export async function callOperation<TDef extends AnyOperationDefinition>(
definition: TDef,
input: z.output<TDef['input']>,
): Promise<z.output<TDef['output']>> {
const { apiServer, apiKey } = getCliConfig()
let res: Response
try {
res = await fetch(`${apiServer}/api/ops/${definition.name}`, {
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
[CLIENT_HEADER]: 'cli',
},
body: JSON.stringify(input),
})
} catch (err) {
throw new CliError(
`Error connecting to API server: ${err instanceof Error ? err.message : String(err)}`,
{ prefixed: true },
)
}
const body: unknown = await res.json().catch(() => undefined)
if (!res.ok) {
const message =
body && typeof body === 'object' && 'error' in body && typeof body.error === 'string'
? body.error
: `Request failed with status ${res.status}`
throw new CliError(message)
}
const parsed = definition.output.safeParse(body)
if (!parsed.success) {
throw new CliError(`Unexpected response from server for ${definition.name}`)
}
// safeParse on the generic schema type loses the concrete shape; the schema guarantees it.
return parsed.data as z.output<TDef['output']>
}
+18
View File
@@ -0,0 +1,18 @@
/** An expected failure, printed as `Error: <message>` with exit code 1. */
export class CliError extends Error {
/** When true, the message already carries its own prefix and is printed as-is. */
readonly prefixed: boolean
constructor(message: string, options: { prefixed?: boolean } = {}) {
super(message)
this.name = 'CliError'
this.prefixed = options.prefixed ?? false
}
}
export function formatCliError(err: unknown): string {
if (err instanceof CliError) {
return err.prefixed ? err.message : `Error: ${err.message}`
}
return `Unexpected error: ${err instanceof Error ? err.message : String(err)}`
}
@@ -0,0 +1,103 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import * as path from 'node:path'
import type { DownloadAssetOutput } from '@shumai/operations'
import { CliError } from '../errors'
import { downloadAssetExtension, safeFileName, uniquePath } from './download-asset'
const output: DownloadAssetOutput = {
files: [
{
assetId: 'a-1',
name: 'clip.mp4',
kind: 'proxy',
size: 2048,
mimeType: 'video/mp4',
url: 'https://files.example/a-1',
},
{
assetId: 'a-2',
name: 'clip.mp4',
kind: 'original',
size: 10,
mimeType: 'video/mp4',
url: 'https://files.example/a-2',
},
],
}
describe('download CLI extension', () => {
let dir: string
let written: string
beforeEach(async () => {
dir = await mkdtemp(path.join(tmpdir(), 'shumai-cli-download-'))
written = ''
vi.stubGlobal(
'fetch',
vi.fn(async (url: string) => new Response(`bytes of ${url}`, { status: 200 })),
)
})
afterEach(async () => {
vi.unstubAllGlobals()
await rm(dir, { recursive: true, force: true })
})
const o = () => ({ out: { write: (s: string) => void (written += s) }, isTty: true, columns: 80 })
it('saves every file, numbering names that are already taken', async () => {
await writeFile(path.join(dir, 'clip.mp4'), 'existing')
await downloadAssetExtension.present(output, { opts: { out: dir }, json: false, output: o() })
expect((await readdir(dir)).sort()).toEqual(['clip (2).mp4', 'clip (3).mp4', 'clip.mp4'])
expect(await readFile(path.join(dir, 'clip.mp4'), 'utf8')).toBe('existing')
expect(await readFile(path.join(dir, 'clip (2).mp4'), 'utf8')).toBe(
'bytes of https://files.example/a-1',
)
expect(written).toBe(
`Saved ${path.join(dir, 'clip (2).mp4')} (2 KB, proxy)\nSaved ${path.join(dir, 'clip (3).mp4')} (10 B, original)\n`,
)
})
it('creates the folder and prints JSON with the saved paths', async () => {
const out = path.join(dir, 'nested', 'files')
await downloadAssetExtension.present(output, { opts: { out }, json: true, output: o() })
const printed = JSON.parse(written) as { files: Array<{ path: string; url: string }> }
expect(printed.files.map((f) => f.path)).toEqual([
path.join(out, 'clip.mp4'),
path.join(out, 'clip (2).mp4'),
])
expect(printed.files[0].url).toBe('https://files.example/a-1')
})
it('fails clearly when a download fails', async () => {
vi.stubGlobal(
'fetch',
vi.fn(async () => new Response('gone', { status: 403 })),
)
await expect(
downloadAssetExtension.present(output, { opts: { out: dir }, json: false, output: o() }),
).rejects.toThrow(new CliError('Failed to download "clip.mp4" (status 403).'))
})
it('keeps file names inside the target folder', () => {
expect(safeFileName('../../etc/passwd')).toBe('_.._etc_passwd')
expect(safeFileName('a\\b.txt')).toBe('a_b.txt')
expect(safeFileName('...')).toBe('file')
const taken = new Set<string>()
expect(uniquePath(dir, 'notes', taken)).toBe(path.join(dir, 'notes'))
expect(uniquePath(dir, 'notes', taken)).toBe(path.join(dir, 'notes (2)'))
})
it('maps --original to proxy false', () => {
expect(downloadAssetExtension.toInput!({ assetIds: ['a'] }, { original: true })).toEqual({
assetIds: ['a'],
proxy: false,
})
expect(downloadAssetExtension.toInput!({ assetIds: ['a'] }, {})).toEqual({ assetIds: ['a'] })
})
})
@@ -0,0 +1,58 @@
import { existsSync } from 'node:fs'
import { mkdir } from 'node:fs/promises'
import * as path from 'node:path'
import { Option } from 'commander'
import type { DownloadAssetOutput } from '@shumai/operations'
import type { CliCommandExtension } from '../generate'
import { formatSize } from '../render'
import { saveUrlToFile } from './save-url'
/** A file name that is safe to create inside the target folder (no paths or control chars). */
export function safeFileName(name: string): string {
const safe = name
// eslint-disable-next-line no-control-regex
.replace(/[\x00-\x1f\x7f/\\]/g, '_')
.replace(/^[\s.]+|[\s.]+$/g, '')
return safe || 'file'
}
/** `name.ext`, or `name (2).ext`, `name (3).ext`, ... if taken on disk or in this download. */
export function uniquePath(dir: string, name: string, taken: Set<string>): string {
const ext = path.extname(name)
const stem = ext ? name.slice(0, -ext.length) : name
for (let n = 1; ; n++) {
const candidate = path.join(dir, n === 1 ? name : `${stem} (${n})${ext}`)
if (!taken.has(candidate) && !existsSync(candidate)) {
taken.add(candidate)
return candidate
}
}
}
/** `download`: saves each file into `--out` (the current folder by default). */
export const downloadAssetExtension: CliCommandExtension = {
options: () => [
new Option('-o, --out <dir>', 'Folder to save the files in.').default('.'),
new Option('--original', 'Download the original uploaded files instead of the web previews.'),
],
toInput: (input, opts) => (opts.original === true ? { ...input, proxy: false } : input),
async present(raw, { opts, json, output: o }) {
const output = raw as DownloadAssetOutput
const dir = typeof opts.out === 'string' ? opts.out : '.'
await mkdir(dir, { recursive: true })
const taken = new Set<string>()
const saved: string[] = []
for (const file of output.files) {
const filePath = uniquePath(dir, safeFileName(file.name), taken)
await saveUrlToFile(file.url, filePath, file.name)
saved.push(filePath)
if (!json) {
o.out.write(`Saved ${filePath} (${formatSize(file.size)}, ${file.kind})\n`)
}
}
if (json) {
const files = output.files.map((file, i) => ({ ...file, path: saved[i] }))
o.out.write(`${JSON.stringify({ files }, null, 2)}\n`)
}
},
}
@@ -0,0 +1,10 @@
import type { CliCommandExtension } from '../generate'
import { downloadAssetExtension } from './download-asset'
import { getAssetRenderExtension, readAssetExtension } from './read-asset'
/** CLI-specific behavior for generated commands, by Operation name. */
export const cliExtensions: ReadonlyMap<string, CliCommandExtension> = new Map([
['read_asset', readAssetExtension],
['get_asset_render', getAssetRenderExtension],
['download_asset', downloadAssetExtension],
])
@@ -0,0 +1,98 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { mkdtemp, readFile, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import * as path from 'node:path'
import type { AssetReadOutput } from '@shumai/operations'
import { CliError } from '../errors'
import { downloadImages, imageFileName, readAssetExtension } from './read-asset'
const output: AssetReadOutput = {
status: 'ready',
summary: 'Rendered 2 page(s) of "deck.pdf" (Pages 1 to 2):',
asset: { id: 'doc-1', name: 'deck.pdf', kind: 'document', mediaType: 'application/pdf' },
details: [],
images: [
{ label: 'Page 1', mimeType: 'image/webp', url: 'https://files.example/p1' },
{ label: 'Page 2', mimeType: 'image/png', url: 'https://files.example/p2' },
],
}
describe('read / render CLI extension', () => {
let dir: string
let written: string
beforeEach(async () => {
dir = await mkdtemp(path.join(tmpdir(), 'shumai-cli-read-'))
written = ''
vi.stubGlobal(
'fetch',
vi.fn(async (url: string) => new Response(`bytes of ${url}`, { status: 200 })),
)
})
afterEach(async () => {
vi.unstubAllGlobals()
await rm(dir, { recursive: true, force: true })
})
const o = () => ({ out: { write: (s: string) => void (written += s) }, isTty: true, columns: 80 })
it('names files after the asset, position, and label', () => {
expect(imageFileName('vid-1', 0, { label: 'Frame at 2.5s', mimeType: 'image/webp' })).toBe(
'vid-1-1-frame-at-2.5s.webp',
)
expect(imageFileName('m-1', 3, { label: 'View at 90°', mimeType: 'image/jpeg' })).toBe(
'm-1-4-view-at-90.jpg',
)
})
it('downloads every image with --out and prints the saved paths', async () => {
const out = path.join(dir, 'pages')
await readAssetExtension.present(output, { opts: { out }, json: false, output: o() })
const first = path.join(out, 'doc-1-1-page-1.webp')
const second = path.join(out, 'doc-1-2-page-2.png')
expect(await readFile(first, 'utf8')).toBe('bytes of https://files.example/p1')
expect(await readFile(second, 'utf8')).toBe('bytes of https://files.example/p2')
expect(written).toBe(`${output.summary}\n- Page 1: ${first}\n- Page 2: ${second}\n`)
})
it('adds the saved path to each image in --json mode', async () => {
await readAssetExtension.present(output, { opts: { out: dir }, json: true, output: o() })
const printed = JSON.parse(written) as { images: Array<{ path: string }> }
expect(printed.images.map((i) => i.path)).toEqual([
path.join(dir, 'doc-1-1-page-1.webp'),
path.join(dir, 'doc-1-2-page-2.png'),
])
})
it('fails clearly when a download fails', async () => {
vi.stubGlobal(
'fetch',
vi.fn(async () => new Response('gone', { status: 403 })),
)
await expect(downloadImages(output, dir)).rejects.toThrow(
new CliError('Failed to download "Page 1" (status 403).'),
)
})
it('does not download anything for a pending render', async () => {
await readAssetExtension.present(
{ ...output, status: 'pending', images: [], taskId: 't-1', retryAfterSeconds: 5 },
{ opts: { out: dir }, json: false, output: o() },
)
expect(fetch).not.toHaveBeenCalled()
expect(written).toContain(`shumai-cli render t-1 --out ${JSON.stringify(dir)}`)
})
it('maps --no-wait to maxWaitSeconds 0', () => {
expect(readAssetExtension.toInput!({ assetId: 'a' }, { wait: false })).toEqual({
assetId: 'a',
maxWaitSeconds: 0,
})
expect(readAssetExtension.toInput!({ assetId: 'a' }, { wait: true })).toEqual({
assetId: 'a',
})
})
})
@@ -0,0 +1,116 @@
import { mkdir } from 'node:fs/promises'
import * as path from 'node:path'
import { Option } from 'commander'
import type { AssetReadOutput } from '@shumai/operations'
import { CliError } from '../errors'
import { saveUrlToFile } from './save-url'
import type { CliCommandExtension } from '../generate'
import type { Output } from '../render'
const EXTENSIONS: Record<string, string> = {
'image/webp': 'webp',
'image/png': 'png',
'image/jpeg': 'jpg',
'image/gif': 'gif',
'image/avif': 'avif',
}
function slug(value: string): string {
return (
value
.toLowerCase()
.replace(/[^a-z0-9.]+/g, '-')
.replace(/^-+|-+$/g, '') || 'image'
)
}
/** File name for the n-th image of a read, e.g. `01ABC-1-frame-at-2s.webp`. */
export function imageFileName(
assetId: string,
index: number,
image: AssetReadOutput['images'][number],
): string {
const ext = EXTENSIONS[image.mimeType] ?? image.mimeType.split('/')[1] ?? 'bin'
return `${assetId}-${index + 1}-${slug(image.label).replace(/\.+$/, '')}.${ext}`
}
/** Downloads every image of a ready read into `dir`; returns the saved paths in order. */
export async function downloadImages(output: AssetReadOutput, dir: string): Promise<string[]> {
await mkdir(dir, { recursive: true })
const saved: string[] = []
for (const [index, image] of output.images.entries()) {
if (!image.url) {
throw new CliError(`No download link was returned for "${image.label}".`)
}
const filePath = path.join(dir, imageFileName(output.asset.id, index, image))
await saveUrlToFile(image.url, filePath, image.label)
saved.push(filePath)
}
return saved
}
function renderCommand(taskId: string, out: unknown): string {
const outArg = typeof out === 'string' ? ` --out ${JSON.stringify(out)}` : ''
return `shumai-cli render ${taskId}${outArg}`
}
function writeReady(output: AssetReadOutput, paths: string[] | null, o: Output): void {
const lines = [output.summary, ...output.details]
output.images.forEach((image, i) => {
lines.push(`- ${image.label}: ${paths ? paths[i] : (image.url ?? '')}`)
})
o.out.write(`${lines.join('\n')}\n`)
if (output.text) {
o.out.write(`\n${output.text.content}`)
if (!output.text.content.endsWith('\n')) o.out.write('\n')
if (output.text.truncated) {
o.out.write(
`\n[Content truncated: showing the first part of a ${output.text.size}-byte file. Run \`shumai-cli download ${output.asset.id}\` to get the whole file.]\n`,
)
}
}
}
/**
* `read` and `render`: images are printed as links (valid for about an hour) or downloaded
* with `--out`; a render that takes longer than the wait prints the `render` command to run.
*/
function readExtension(noWaitHelp: string): CliCommandExtension {
return {
options: () => [
new Option('-o, --out <dir>', 'Download the images into this folder and print their paths.'),
new Option('--no-wait', noWaitHelp),
],
toInput: (input, opts) => (opts.wait === false ? { ...input, maxWaitSeconds: 0 } : input),
async present(raw, { opts, json, output: o }) {
const output = raw as AssetReadOutput
if (output.status === 'pending') {
if (json) {
o.out.write(`${JSON.stringify(output, null, 2)}\n`)
return
}
o.out.write(
`${output.summary}\nRun this to get the result: ${renderCommand(output.taskId ?? '', opts.out)}\n`,
)
return
}
const paths = typeof opts.out === 'string' ? await downloadImages(output, opts.out) : null
if (json) {
const images = output.images.map((image, i) =>
paths ? { ...image, path: paths[i] } : image,
)
o.out.write(`${JSON.stringify({ ...output, images }, null, 2)}\n`)
return
}
writeReady(output, paths, o)
},
}
}
export const readAssetExtension = readExtension(
'Return right away with a task ID if the asset needs rendering.',
)
export const getAssetRenderExtension = readExtension(
'Check once and return right away if the render is still running.',
)
@@ -0,0 +1,32 @@
import { createWriteStream } from 'node:fs'
import { rm } from 'node:fs/promises'
import { Readable } from 'node:stream'
import { pipeline } from 'node:stream/promises'
import type { ReadableStream as WebReadableStream } from 'node:stream/web'
import { CliError } from '../errors'
/** Streams a download link to `filePath` (large files never sit in memory). */
export async function saveUrlToFile(url: string, filePath: string, label: string): Promise<void> {
let res: Response
try {
res = await fetch(url)
} catch (err) {
throw new CliError(
`Failed to download "${label}": ${err instanceof Error ? err.message : String(err)}`,
)
}
if (!res.ok || !res.body) {
throw new CliError(`Failed to download "${label}" (status ${res.status}).`)
}
try {
await pipeline(
Readable.fromWeb(res.body as unknown as WebReadableStream<Uint8Array>),
createWriteStream(filePath),
)
} catch (err) {
await rm(filePath, { force: true })
throw new CliError(
`Failed to save "${label}" to ${filePath}: ${err instanceof Error ? err.message : String(err)}`,
)
}
}
+269
View File
@@ -0,0 +1,269 @@
import { Command, InvalidArgumentError, Option } from 'commander'
import { z } from 'zod'
import type { AnyOperationDefinition } from '@shumai/operations'
import type { CallOperation } from './call-operation'
import { CliError } from './errors'
import { renderOutput, type Output } from './render'
/**
* CLI-specific behavior for one Operation's command, for things a definition can't describe
* (it must stay free of I/O): extra options, input mapping, and output that downloads files.
*/
export interface CliCommandExtension {
/** Extra options only the CLI has; their values are passed to `toInput` and `present`. */
options?(): Option[]
/** Adjust the Operation input using the command's options. */
toInput?(input: Record<string, unknown>, opts: Record<string, unknown>): Record<string, unknown>
/** Writes the output, including in `--json` mode. Replaces the default rendering. */
present(
output: unknown,
ctx: { opts: Record<string, unknown>; json: boolean; output: Output },
): Promise<void>
}
export interface GenerateDeps {
callOperation: CallOperation
output: Output
/** Whether destructive Operations are allowed (SHUMAI_ALLOW_DELETE). */
allowDestructive: () => boolean
/** CLI extensions by Operation name. */
extensions?: ReadonlyMap<string, CliCommandExtension>
}
type FieldKind =
| { type: 'string' }
| { type: 'number' }
| { type: 'boolean' }
| { type: 'enum'; values: string[] }
| { type: 'array' }
/** A key/value object (`z.record`), passed as a JSON object. */
| { type: 'json' }
interface FieldInfo {
name: string
kind: FieldKind
required: boolean
description: string
}
/** Strip optional/nullable/default wrappers to find the field's base schema. */
function unwrap(schema: z.ZodType): z.ZodType {
let current = schema
while (
current instanceof z.ZodOptional ||
current instanceof z.ZodNullable ||
current instanceof z.ZodDefault
) {
current = current.unwrap() as z.ZodType
}
return current
}
function describeField(definition: AnyOperationDefinition, name: string): FieldInfo {
return describeSchema(definition, name, definition.input.shape[name] as z.ZodType)
}
function describeSchema(
definition: AnyOperationDefinition,
name: string,
schema: z.ZodType,
): FieldInfo {
const base = unwrap(schema)
let kind: FieldKind
if (base instanceof z.ZodNumber) kind = { type: 'number' }
else if (base instanceof z.ZodBoolean) kind = { type: 'boolean' }
else if (base instanceof z.ZodEnum) kind = { type: 'enum', values: base.options.map(String) }
else if (base instanceof z.ZodArray) kind = { type: 'array' }
else if (base instanceof z.ZodString) kind = { type: 'string' }
else if (base instanceof z.ZodRecord) kind = { type: 'json' }
else {
throw new Error(`${definition.name}.${name}: unsupported input type for the CLI`)
}
return {
name,
kind,
required: !schema.safeParse(undefined).success,
description: schema.description ?? base.description ?? '',
}
}
/**
* Input errors name the input path (e.g. `videoConfig.end`). For flattened fields, name the
* flag the user typed instead (e.g. `--video-end`).
*/
function formatInputError(
error: z.ZodError,
options: ReadonlyArray<{ path: [string] | [string, string]; option: Option }>,
): string {
const flagFor = (path: PropertyKey[]): string | undefined =>
options.find((o) => o.path.length === 2 && o.path[0] === path[0] && o.path[1] === path[1])
?.option.long
if (!error.issues.some((issue) => flagFor(issue.path))) {
return z.prettifyError(error)
}
return error.issues
.map((issue) => {
const flag = flagFor(issue.path)
return flag ? `✖ ${flag}: ${issue.message}` : z.prettifyError(new z.ZodError([issue]))
})
.join('\n')
}
function parseNumber(value: string): number {
const parsed = Number(value)
if (value.trim() === '' || Number.isNaN(parsed)) {
throw new InvalidArgumentError('Not a number.')
}
return parsed
}
function parseJsonObject(value: string): Record<string, unknown> {
let parsed: unknown
try {
parsed = JSON.parse(value)
} catch {
throw new InvalidArgumentError('Not valid JSON.')
}
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new InvalidArgumentError('Expected a JSON object, e.g. \'{"key": "value"}\'.')
}
return parsed as Record<string, unknown>
}
function kebab(name: string): string {
return name.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`)
}
/** Find or create the subcommand path, e.g. `project` for `project ls`. */
function ensureParent(program: Command, path: readonly string[]): Command {
let parent = program
for (const segment of path) {
parent =
parent.commands.find((c) => c.name() === segment) ??
parent.command(segment).description(`Manage ${segment}s.`)
}
return parent
}
/** Registers one command for each definition with a `cli` spec. */
export function registerOperationCommands(
program: Command,
definitions: readonly AnyOperationDefinition[],
deps: GenerateDeps,
): void {
for (const definition of definitions) {
const cli = definition.cli
if (!cli || !definition.surfaces.includes('cli')) continue
const parent = ensureParent(program, cli.command.slice(0, -1))
const cmd = parent
.command(cli.command[cli.command.length - 1])
.description(cli.description ?? definition.description)
if (cli.aliases) cmd.aliases([...cli.aliases])
const positional = (cli.positional ?? []).map((name) => describeField(definition, name))
for (const field of positional) {
const placeholder = field.kind.type === 'array' ? `${field.name}...` : field.name
cmd.argument(field.required ? `<${placeholder}>` : `[${placeholder}]`, field.description)
}
const positionalNames = new Set(positional.map((f) => f.name))
const omitted = new Set<string>(cli.omit ?? [])
const flatten = (cli.flatten ?? {}) as Record<string, string | undefined>
// `path` is where the value goes in the input: `[field]` or `[field, subField]`.
const options: Array<{ path: [string] | [string, string]; option: Option }> = []
const addOption = (
path: [string] | [string, string],
field: FieldInfo,
spellings: readonly string[],
) => {
const valueHint =
field.kind.type === 'boolean'
? ''
: field.kind.type === 'array'
? ` <${field.name}...>`
: field.kind.type === 'json'
? ` <json>`
: ` <${field.name}>`
const option = new Option(`${spellings.join(', ')}${valueHint}`, field.description)
if (field.kind.type === 'number') option.argParser(parseNumber)
if (field.kind.type === 'json') option.argParser(parseJsonObject)
if (field.kind.type === 'enum') option.choices(field.kind.values)
if (field.required) option.makeOptionMandatory()
cmd.addOption(option)
options.push({ path, option })
return option
}
for (const name of Object.keys(definition.input.shape)) {
if (positionalNames.has(name) || omitted.has(name)) continue
const prefix = flatten[name]
if (prefix !== undefined) {
const schema = definition.input.shape[name] as z.ZodType
const base = unwrap(schema)
if (!(base instanceof z.ZodObject)) {
throw new Error(`${definition.name}.${name}: only object fields can be flattened`)
}
const parentRequired = !schema.safeParse(undefined).success
for (const [subName, subSchema] of Object.entries(base.shape)) {
const field = describeSchema(definition, `${name}.${subName}`, subSchema as z.ZodType)
field.name = subName
// Sub-fields are only mandatory when the object itself is.
field.required = parentRequired && field.required
const flag = prefix ? `--${prefix}-${kebab(subName)}` : `--${kebab(subName)}`
addOption([name, subName], field, [flag])
}
continue
}
const field = describeField(definition, name)
const option = addOption([name], field, cli.flags?.[name] ?? [`--${kebab(name)}`])
const cliDefault = (cli.defaults as Record<string, unknown> | undefined)?.[name]
if (cliDefault !== undefined) option.default(cliDefault)
}
const extension = deps.extensions?.get(definition.name)
for (const option of extension?.options?.() ?? []) cmd.addOption(option)
cmd.action(async (...args: unknown[]) => {
const command = args[args.length - 1] as Command
const input: Record<string, unknown> = { ...cli.defaults }
positional.forEach((field, i) => {
if (args[i] !== undefined) input[field.name] = args[i]
})
const opts = command.opts()
for (const { path, option } of options) {
const value = opts[option.attributeName()]
if (value === undefined) continue
if (path.length === 1) {
input[path[0]] = value
} else {
const parent = (input[path[0]] ?? {}) as Record<string, unknown>
input[path[0]] = { ...parent, [path[1]]: value }
}
}
if (definition.annotations.destructive && !deps.allowDestructive()) {
throw new CliError(
`${definition.title} is disabled in this environment. Set SHUMAI_ALLOW_DELETE=true to enable it.`,
)
}
const parsed = definition.input.safeParse(
extension?.toInput ? extension.toInput(input, opts) : input,
)
if (!parsed.success) {
throw new CliError(formatInputError(parsed.error, options))
}
const output = await deps.callOperation(definition, parsed.data)
const json = Boolean(command.optsWithGlobals().json)
if (extension) {
await extension.present(output, { opts, json, output: deps.output })
} else if (json) {
deps.output.out.write(`${JSON.stringify(output, null, 2)}\n`)
} else {
renderOutput(definition, output, deps.output)
}
})
}
}
+68
View File
@@ -0,0 +1,68 @@
import { describe, expect, it } from 'vitest'
import { z } from 'zod'
import { defineOperation, listAssets, renameAsset } from '@shumai/operations'
import { formatSize, renderOutput, type Output } from './render'
function capture(isTty = true): Output & { text: () => string } {
let text = ''
return { out: { write: (s) => void (text += s) }, isTty, columns: 120, text: () => text }
}
const page = {
data: [{ id: 'd1', name: 'Renders', type: 'folder', size: 0, projectId: 'p' }],
pageInfo: { hasNextPage: true, endCursor: 'CUR' },
total: 2,
}
describe('renderOutput', () => {
it('shows the next-page cursor only on a terminal', () => {
const tty = capture(true)
renderOutput(listAssets, page, tty)
expect(tty.text()).toContain('Next page: --after CUR')
const piped = capture(false)
renderOutput(listAssets, page, piped)
expect(piped.text()).toBe('d1\tRenders\tfolder\t-\n')
})
it('uses the definition formatter when there is no table renderer', () => {
const o = capture()
renderOutput(renameAsset, { id: 'a1', name: 'new', type: 'file', size: 1, projectId: null }, o)
expect(o.text()).toBe('Renamed asset a1 to "new"\n')
})
it('falls back to tables for lists and key/value lines for objects', () => {
const generic = defineOperation({
name: 'generic_thing',
title: 'Generic',
description: 'Generic output.',
input: z.object({}),
output: z.unknown(),
surfaces: ['agent'],
annotations: { readOnly: true, destructive: false },
})
const list = capture(false)
renderOutput(generic, { data: [{ id: 'x', tags: ['a'] }] }, list)
expect(list.text()).toBe('x\t["a"]\n')
const empty = capture()
renderOutput(generic, [], empty)
expect(empty.text()).toBe('No results.\n')
const object = capture()
renderOutput(generic, { id: 'x', count: 2, note: null }, object)
expect(object.text()).toBe('id: x\ncount: 2\nnote: \n')
})
})
describe('formatSize', () => {
it.each([
[0, '-'],
[512, '512 B'],
[1536, '1.5 KB'],
[5 * 1024 * 1024, '5 MB'],
])('formats %d bytes as %s', (bytes, expected) => {
expect(formatSize(bytes)).toBe(expected)
})
})
+121
View File
@@ -0,0 +1,121 @@
import { newTablePrinter, withTruncate, type Writer } from '@shumai/tableprinter'
import type {
AnyOperationDefinition,
AssetSummary,
PageInfo,
ProjectSummary,
} from '@shumai/operations'
export interface Output {
out: Writer
isTty: boolean
columns: number
}
export function formatSize(bytes: number): string {
if (bytes === 0) return '-'
const k = 1024
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}
function renderAssets(output: { data: AssetSummary[]; pageInfo: PageInfo }, o: Output): void {
if (output.data.length === 0) {
o.out.write('No assets found in this folder.\n')
return
}
// Folders first, as in the file browser's default listing
const items = [...output.data].sort(
(a, b) => Number(b.type === 'folder') - Number(a.type === 'folder'),
)
const tp = newTablePrinter(o.out, o.isTty, o.columns)
tp.addHeader(['ID', 'Name', 'Type', 'Size'])
for (const item of items) {
const isFolder = item.type === 'folder'
tp.addField(item.id, withTruncate(null))
tp.addField(item.name)
tp.addField(isFolder ? 'folder' : 'file', withTruncate(null))
tp.addField(isFolder ? '-' : formatSize(item.size), withTruncate(null))
tp.endRow()
}
tp.render()
// Piped output is TSV for scripts (use --json for pagination metadata); keep it clean.
if (o.isTty && output.pageInfo.hasNextPage && output.pageInfo.endCursor) {
o.out.write(`\nMore assets available. Next page: --after ${output.pageInfo.endCursor}\n`)
}
}
function renderProjects(output: { data: ProjectSummary[] }, o: Output): void {
if (output.data.length === 0) {
o.out.write('No projects found.\n')
return
}
const tp = newTablePrinter(o.out, o.isTty, o.columns)
tp.addHeader(['ID', 'Name', 'Root Folder ID'])
for (const p of output.data) {
tp.addField(p.id)
tp.addField(p.name)
tp.addField(p.rootFolderId ?? '')
tp.endRow()
}
tp.render()
}
/** Table renderers for list Operations; other Operations use their `cli.format` or the fallback. */
const tableRenderers = new Map<string, (output: never, o: Output) => void>([
['list_assets', renderAssets],
['list_projects', renderProjects],
])
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
function cell(value: unknown): string {
if (value === null || value === undefined) return ''
return typeof value === 'object' ? JSON.stringify(value) : String(value)
}
/** Generic rendering: `{ data: [...] }` and arrays as tables, objects as `key: value` lines. */
function renderFallback(output: unknown, o: Output): void {
const rows = Array.isArray(output)
? output
: isRecord(output) && Array.isArray(output.data)
? output.data
: null
if (rows) {
if (rows.length === 0) {
o.out.write('No results.\n')
return
}
const keys = Object.keys(isRecord(rows[0]) ? rows[0] : { value: rows[0] })
const tp = newTablePrinter(o.out, o.isTty, o.columns)
tp.addHeader(keys)
for (const row of rows) {
for (const key of keys) tp.addField(cell(isRecord(row) ? row[key] : row))
tp.endRow()
}
tp.render()
return
}
if (isRecord(output)) {
for (const [key, value] of Object.entries(output)) o.out.write(`${key}: ${cell(value)}\n`)
return
}
o.out.write(`${cell(output)}\n`)
}
/** Writes an Operation's output in human-friendly form. */
export function renderOutput(definition: AnyOperationDefinition, output: unknown, o: Output): void {
const table = tableRenderers.get(definition.name)
if (table) {
table(output as never, o)
return
}
if (definition.cli?.format) {
o.out.write(`${definition.cli.format(output)}\n`)
return
}
renderFallback(output, o)
}
+844
View File
@@ -0,0 +1,844 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { Command } from 'commander'
import {
copyAssets,
createComment,
createFolder,
createVersionStack,
deleteAsset,
downloadAsset,
getAssetRender,
getFolderInstructions,
readAsset,
listAssets,
listComments,
listMetadataFields,
listProjects,
listVersions,
moveAssets,
removeVersion,
renameAsset,
resolveComment,
searchAssets,
updateAssetMetadata,
} from '@shumai/operations'
import type { CallOperation } from './operations/call-operation'
import { CliError, formatCliError } from './operations/errors'
import { createProgram } from './program'
/** Runs the CLI with mocked Operation calls and captures stdout/stderr. */
function setup() {
let stdout = ''
let stderr = ''
const callOperation = vi.fn<CallOperation>()
const program = createProgram({
callOperation,
output: { out: { write: (s) => void (stdout += s) }, isTty: true, columns: 120 },
})
const configure = (cmd: Command) => {
cmd.exitOverride().configureOutput({
writeOut: (s) => void (stdout += s),
writeErr: (s) => void (stderr += s),
})
cmd.commands.forEach(configure)
}
configure(program)
return {
callOperation,
run: (...argv: string[]) => program.parseAsync(['node', 'shumai-cli', ...argv]),
stdout: () => stdout,
stderr: () => stderr,
}
}
const folder = { id: 'f-1', name: 'Renders', type: 'folder', size: 0, projectId: 'p-1' }
const frames = {
status: 'ready',
summary: 'Extracted 2 frame(s) from "clip.mp4" (0s - 4s):',
asset: { id: 'vid-1', name: 'clip.mp4', kind: 'video', mediaType: 'video/mp4' },
details: [],
images: [
{ label: 'Frame at 0s', mimeType: 'image/webp', url: 'https://files.example/0.webp' },
{ label: 'Frame at 4s', mimeType: 'image/webp', url: 'https://files.example/4.webp' },
],
}
const pendingRead = {
status: 'pending',
summary: 'Rendering is still in progress for "clip.mp4".',
asset: frames.asset,
details: [],
images: [],
taskId: 'task-1',
retryAfterSeconds: 5,
}
describe('shumai-cli', () => {
const originalAllowDelete = process.env.SHUMAI_ALLOW_DELETE
afterEach(() => {
if (originalAllowDelete === undefined) delete process.env.SHUMAI_ALLOW_DELETE
else process.env.SHUMAI_ALLOW_DELETE = originalAllowDelete
})
describe('project ls', () => {
it('lists projects with their root folder', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({
data: [
{ id: 'p1', name: 'Proj 1', teamId: 't1', rootFolderId: 'root1' },
{ id: 'p2', name: 'Proj 2', teamId: 't1', rootFolderId: 'root2' },
],
})
await cli.run('project', 'ls')
expect(cli.callOperation).toHaveBeenCalledWith(listProjects, {})
expect(cli.stdout()).toContain('Root Folder ID')
expect(cli.stdout()).toMatch(/p1\s+Proj 1\s+root1/)
expect(cli.stdout()).toContain('Proj 2')
})
it('reports when there are no projects', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({ data: [] })
await cli.run('project', 'ls')
expect(cli.stdout()).toBe('No projects found.\n')
})
})
describe('ls', () => {
it('lists a folder with the maximum page size, folders first', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({
data: [
{ id: 'file1', name: 'doc.pdf', type: 'file', size: 1024, projectId: 'p' },
{ id: 'sub1', name: 'Sub 1', type: 'folder', size: 0, projectId: 'p' },
],
pageInfo: { hasNextPage: false, endCursor: null },
total: 2,
})
await cli.run('ls', '-p', 'parentId123')
expect(cli.callOperation).toHaveBeenCalledWith(listAssets, {
parent: 'parentId123',
first: 200,
})
const out = cli.stdout()
expect(out.indexOf('Sub 1')).toBeLessThan(out.indexOf('doc.pdf'))
expect(out).toMatch(/file1\s+doc\.pdf\s+file\s+1 KB/)
expect(out).not.toContain('More assets available')
})
it('passes pagination and type flags and prints the next-page cursor', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({
data: [{ id: 'sub1', name: 'Sub 1', type: 'folder', size: 0, projectId: 'p' }],
pageInfo: { hasNextPage: true, endCursor: 'cur-2' },
total: 3,
})
await cli.run('ls', '--parent', 'p1', '--type', 'folder', '--first', '1', '--after', 'cur-1')
expect(cli.callOperation).toHaveBeenCalledWith(listAssets, {
parent: 'p1',
type: 'folder',
first: 1,
after: 'cur-1',
})
expect(cli.stdout()).toContain('Next page: --after cur-2')
})
it('requires the parent option', async () => {
const cli = setup()
await expect(cli.run('ls')).rejects.toThrow()
expect(cli.stderr()).toContain("required option '-p, --parent <parent>' not specified")
expect(cli.callOperation).not.toHaveBeenCalled()
})
it('rejects an invalid type and a non-numeric page size', async () => {
const cli = setup()
await expect(cli.run('ls', '-p', 'x', '--type', 'video')).rejects.toThrow()
expect(cli.stderr()).toContain('Allowed choices are file, folder, all')
await expect(cli.run('ls', '-p', 'x', '--first', 'many')).rejects.toThrow()
expect(cli.stderr()).toContain('Not a number')
expect(cli.callOperation).not.toHaveBeenCalled()
})
})
describe('mkdir', () => {
it('creates a folder and prints its ID', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({ ...folder, id: 'new_folder_ulid' })
await cli.run('mkdir', 'NewFolder', '-p', 'parent_ulid')
expect(cli.callOperation).toHaveBeenCalledWith(createFolder, {
name: 'NewFolder',
parent: 'parent_ulid',
})
expect(cli.stdout()).toBe('Created folder with ID: new_folder_ulid\n')
})
it('requires a folder name', async () => {
const cli = setup()
await expect(cli.run('mkdir', '-p', 'parent_ulid')).rejects.toThrow()
expect(cli.stderr()).toContain("missing required argument 'name'")
})
})
describe('rename', () => {
it('renames an asset', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({ ...folder, name: 'Renamed Folder' })
await cli.run('rename', 'f-1', 'Renamed Folder')
expect(cli.callOperation).toHaveBeenCalledWith(renameAsset, {
assetId: 'f-1',
name: 'Renamed Folder',
})
expect(cli.stdout()).toBe('Renamed asset f-1 to "Renamed Folder"\n')
})
it('requires both the asset ID and the new name', async () => {
const cli = setup()
await expect(cli.run('rename', 'f-1')).rejects.toThrow()
expect(cli.stderr()).toContain("missing required argument 'name'")
})
})
describe('move', () => {
it.each(['move', 'mv'])('moves assets with %s', async (command) => {
const cli = setup()
cli.callOperation.mockResolvedValue({
movedAssetIds: ['a-1', 'a-2'],
newParentId: 'parent-1',
})
await cli.run(command, 'a-1', 'a-2', '-p', 'parent-1')
expect(cli.callOperation).toHaveBeenCalledWith(moveAssets, {
assetIds: ['a-1', 'a-2'],
newParentId: 'parent-1',
})
expect(cli.stdout()).toBe('Moved 2 asset(s) to parent parent-1\n')
})
it('requires at least one asset and a destination', async () => {
const cli = setup()
await expect(cli.run('move', '-p', 'parent-1')).rejects.toThrow()
expect(cli.stderr()).toContain("missing required argument 'assetIds'")
await expect(cli.run('move', 'a-1')).rejects.toThrow()
expect(cli.stderr()).toContain("required option '-p, --parent <newParentId>' not specified")
})
})
describe('delete', () => {
it.each([undefined, 'false'])(
'refuses to delete when SHUMAI_ALLOW_DELETE is %s',
async (value) => {
if (value === undefined) delete process.env.SHUMAI_ALLOW_DELETE
else process.env.SHUMAI_ALLOW_DELETE = value
const cli = setup()
const error = await cli.run('delete', 'a-1').catch((err: unknown) => err)
expect(formatCliError(error)).toBe(
'Error: Delete Asset is disabled in this environment. Set SHUMAI_ALLOW_DELETE=true to enable it.',
)
expect(cli.callOperation).not.toHaveBeenCalled()
},
)
it.each([
['delete', 'true'],
['rm', '1'],
])('deletes with %s when SHUMAI_ALLOW_DELETE=%s', async (command, value) => {
process.env.SHUMAI_ALLOW_DELETE = value
const cli = setup()
cli.callOperation.mockResolvedValue({ deletedAssetId: 'a-1' })
await cli.run(command, 'a-1')
expect(cli.callOperation).toHaveBeenCalledWith(deleteAsset, { assetId: 'a-1' })
expect(cli.stdout()).toBe('Deleted asset a-1\n')
})
it('deletes only one asset at a time', async () => {
process.env.SHUMAI_ALLOW_DELETE = 'true'
const cli = setup()
await expect(cli.run('delete', 'a-1', 'a-2')).rejects.toThrow()
expect(cli.stderr()).toContain('too many arguments')
expect(cli.callOperation).not.toHaveBeenCalled()
})
})
describe('read', () => {
it('maps flattened reader flags onto the config objects and prints image links', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue(frames)
await cli.run(
'read',
'vid-1',
'--video-start',
'0',
'--video-end',
'4',
'--video-count',
'2',
'--annotation-id',
'c-1',
)
expect(cli.callOperation).toHaveBeenCalledWith(readAsset, {
assetId: 'vid-1',
annotationId: 'c-1',
videoConfig: { start: 0, end: 4, count: 2 },
})
expect(cli.stdout()).toBe(
[
'Extracted 2 frame(s) from "clip.mp4" (0s - 4s):',
'- Frame at 0s: https://files.example/0.webp',
'- Frame at 4s: https://files.example/4.webp',
'',
].join('\n'),
)
})
it.each([
[
['--doc-mode', 'pages', '--doc-start-page', '1', '--doc-end-page', '3'],
{ docConfig: { mode: 'pages', startPage: 1, endPage: 3 } },
],
[['--doc-mode', 'text'], { docConfig: { mode: 'text' } }],
[['--degree', '90'], { config3d: { degree: 90 } }],
[[], {}],
])('reads with %j', async (flags, config) => {
const cli = setup()
cli.callOperation.mockResolvedValue({ ...frames, images: [] })
await cli.run('read', 'a-1', ...flags)
expect(cli.callOperation).toHaveBeenCalledWith(readAsset, { assetId: 'a-1', ...config })
})
it('names the flag that is missing from a reader config', async () => {
const cli = setup()
const error = await cli
.run('read', 'vid-1', '--video-start', '0', '--video-count', '2')
.catch((err: unknown) => err)
expect(formatCliError(error)).toContain('--video-end')
expect(cli.callOperation).not.toHaveBeenCalled()
})
it('prints text content after the summary', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({
...frames,
summary: 'Document "notes.md" (ID: d-1):',
images: [],
text: {
content: '# Notes\nhello',
truncated: true,
size: 90000,
mimeType: 'text/markdown',
},
})
await cli.run('read', 'd-1', '--doc-mode', 'text')
expect(cli.stdout()).toBe(
'Document "notes.md" (ID: d-1):\n\n# Notes\nhello\n\n[Content truncated: showing the first part of a 90000-byte file. Run `shumai-cli download vid-1` to get the whole file.]\n',
)
})
it('returns right away with --no-wait and prints the render command', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue(pendingRead)
await cli.run(
'read',
'vid-1',
'--video-start',
'0',
'--video-end',
'4',
'--video-count',
'2',
'--no-wait',
'--out',
'frames',
)
expect(cli.callOperation).toHaveBeenCalledWith(readAsset, {
assetId: 'vid-1',
videoConfig: { start: 0, end: 4, count: 2 },
maxWaitSeconds: 0,
})
expect(cli.stdout()).toBe(
'Rendering is still in progress for "clip.mp4".\nRun this to get the result: shumai-cli render task-1 --out "frames"\n',
)
})
it('prints the raw output with --json', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue(frames)
await cli.run('--json', 'read', 'vid-1')
expect(JSON.parse(cli.stdout())).toEqual(frames)
})
})
describe('render', () => {
it('fetches a pending render by task ID', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue(frames)
await cli.run('render', 'task-1')
expect(cli.callOperation).toHaveBeenCalledWith(getAssetRender, { taskId: 'task-1' })
expect(cli.stdout()).toContain('- Frame at 4s: https://files.example/4.webp')
})
it('checks once with --no-wait', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue(pendingRead)
await cli.run('render', 'task-1', '--no-wait')
expect(cli.callOperation).toHaveBeenCalledWith(getAssetRender, {
taskId: 'task-1',
maxWaitSeconds: 0,
})
expect(cli.stdout()).toContain('Run this to get the result: shumai-cli render task-1\n')
})
})
describe('download', () => {
it('downloads the web previews by default, or the originals with --original', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({ files: [] })
await cli.run('download', 'a-1', 'a-2')
expect(cli.callOperation).toHaveBeenLastCalledWith(downloadAsset, {
assetIds: ['a-1', 'a-2'],
})
await cli.run('download', 'a-1', '--original')
expect(cli.callOperation).toHaveBeenLastCalledWith(downloadAsset, {
assetIds: ['a-1'],
proxy: false,
})
})
it('requires at least one asset ID and documents its options', async () => {
const cli = setup()
await expect(cli.run('download')).rejects.toThrow()
expect(cli.stderr()).toContain("missing required argument 'assetIds'")
await expect(cli.run('download', '--help')).rejects.toThrow()
expect(cli.stdout()).toContain('-o, --out <dir>')
expect(cli.stdout()).toContain('--original')
expect(cli.stdout()).not.toContain('--proxy')
})
})
describe('--json', () => {
it('prints the raw Operation output', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue(folder)
await cli.run('mkdir', 'Renders', '-p', 'root', '--json')
expect(JSON.parse(cli.stdout())).toEqual(folder)
})
it('works before the command too', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({ data: [] })
await cli.run('--json', 'project', 'ls')
expect(JSON.parse(cli.stdout())).toEqual({ data: [] })
})
})
describe('errors', () => {
it('surfaces server errors for the entry point to print', async () => {
const cli = setup()
cli.callOperation.mockRejectedValue(new CliError('User is not a member of the team'))
const error = await cli.run('mkdir', 'x', '-p', 'root').catch((err: unknown) => err)
expect(formatCliError(error)).toBe('Error: User is not a member of the team')
})
it('validates input against the Operation schema before calling the server', async () => {
const cli = setup()
const error = await cli.run('mkdir', '', '-p', 'root').catch((err: unknown) => err)
expect(error).toBeInstanceOf(CliError)
expect(cli.callOperation).not.toHaveBeenCalled()
})
it('suggests similar commands for typos', async () => {
const cli = setup()
await expect(cli.run('mkidr', 'x')).rejects.toThrow()
expect(cli.stderr()).toContain('Did you mean mkdir?')
})
})
describe('help', () => {
it('lists every command, including generated ones', async () => {
const cli = setup()
await expect(cli.run('--help')).rejects.toThrow()
const help = cli.stdout()
for (const command of [
'project',
'ls',
'mkdir',
'rename',
'move|mv',
'delete|rm',
'read',
'render',
'download',
'upload',
'create-version',
]) {
expect(help).toContain(command)
}
expect(help).toContain('--json')
})
it('documents generated options from the Operation schema', async () => {
const cli = setup()
await expect(cli.run('ls', '--help')).rejects.toThrow()
const help = cli.stdout()
expect(help).toContain('-p, --parent <parent>')
expect(help).toContain('The parent folder ID to list assets from.')
expect(help).toContain('--type <type>')
expect(help).toContain('choices: "file", "folder", "all"')
expect(help).toContain('(default: 200)')
expect(help).toContain('List the files and folders inside a parent folder.')
})
it('documents flattened reader options for read', async () => {
const cli = setup()
await expect(cli.run('read', '--help')).rejects.toThrow()
const help = cli.stdout()
for (const flag of [
'--video-start <start>',
'--video-end <end>',
'--video-count <count>',
'--doc-mode <mode>',
'--doc-start-page <startPage>',
'--doc-end-page <endPage>',
'--degree <degree>',
'--annotation-id <annotationId>',
'-o, --out <dir>',
'--no-wait',
]) {
expect(help).toContain(flag)
}
expect(help).not.toContain('--include-content')
expect(help).not.toContain('--image')
})
})
describe('search', () => {
it('searches a folder by name with the maximum page size', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({
data: [{ id: 'f1', name: 'sunset.png', type: 'file', size: 10, projectId: 'p' }],
pageInfo: { hasNextPage: false, endCursor: null },
total: 1,
})
await cli.run('search', 'sunset', '-p', 'root-1')
expect(cli.callOperation).toHaveBeenCalledWith(searchAssets, {
parent: 'root-1',
query: 'sunset',
first: 200,
})
expect(cli.stdout()).toContain('sunset.png')
})
it('searches by meaning with --semantic', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({
data: [],
pageInfo: { hasNextPage: false, endCursor: null },
total: 0,
})
await cli.run('search', 'a calm beach', '-p', 'root-1', '--semantic')
expect(cli.callOperation).toHaveBeenCalledWith(
searchAssets,
expect.objectContaining({ query: 'a calm beach', semantic: true }),
)
})
})
describe('copy', () => {
it.each(['copy', 'cp'])('copies assets with %s', async (command) => {
const cli = setup()
cli.callOperation.mockResolvedValue({ copiedAssetIds: ['c-1', 'c-2'], newParentId: 'f-2' })
await cli.run(command, 'a-1', 'a-2', '-p', 'f-2', '--with-comments')
expect(cli.callOperation).toHaveBeenCalledWith(copyAssets, {
assetIds: ['a-1', 'a-2'],
newParentId: 'f-2',
withComments: true,
})
expect(cli.stdout()).toBe('Copied 2 asset(s) to parent f-2: c-1, c-2\n')
})
})
describe('comment', () => {
const thread = {
id: 'c-1',
message: 'Too dark here',
author: { id: 'u-1', name: 'Ana' },
second: 12.5,
hasMarkup: false,
attachmentCount: 0,
createdAt: '2026-01-01T00:00:00.000Z',
resolved: true,
replies: [
{
id: 'c-2',
message: 'Fixed in v2',
author: { id: 'u-2', name: 'Bo' },
second: null,
hasMarkup: false,
attachmentCount: 0,
createdAt: '2026-01-02T00:00:00.000Z',
},
],
}
it('lists threads with their replies', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({
data: [thread],
pageInfo: { hasNextPage: false, endCursor: null },
total: 1,
})
await cli.run('comment', 'ls', 'vid-1')
expect(cli.callOperation).toHaveBeenCalledWith(listComments, {
assetId: 'vid-1',
first: 200,
})
expect(cli.stdout()).toBe(
'c-1 @12.5s [resolved] Ana: Too dark here\n ↳ c-2 Bo: Fixed in v2\n',
)
})
it('posts a reply at a timecode', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({ id: 'c-3', assetId: 'vid-1', replyToId: 'c-1' })
await cli.run('comment', 'add', 'vid-1', 'Looks good now', '--reply-to', 'c-1', '--at', '3')
expect(cli.callOperation).toHaveBeenCalledWith(createComment, {
assetId: 'vid-1',
message: 'Looks good now',
replyToId: 'c-1',
second: 3,
})
expect(cli.stdout()).toBe('Posted comment c-3\n')
})
it('resolves and reopens comments', async () => {
const cli = setup()
cli.callOperation.mockResolvedValueOnce({ commentId: 'c-1', resolved: true })
cli.callOperation.mockResolvedValueOnce({ commentId: 'c-1', resolved: false })
await cli.run('comment', 'resolve', 'c-1')
await cli.run('comment', 'resolve', 'c-1', '--reopen')
expect(cli.callOperation).toHaveBeenNthCalledWith(1, resolveComment, { commentId: 'c-1' })
expect(cli.callOperation).toHaveBeenNthCalledWith(2, resolveComment, {
commentId: 'c-1',
reopen: true,
})
expect(cli.stdout()).toBe('Comment c-1 is resolved\nComment c-1 is unresolved\n')
})
})
describe('meta set', () => {
it('passes --values as a JSON object', async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({ assetId: 'a-1', updatedKeys: ['status', 'rating'] })
await cli.run('meta', 'set', 'a-1', '--values', '{"status":"approved","rating":4}')
expect(cli.callOperation).toHaveBeenCalledWith(updateAssetMetadata, {
assetId: 'a-1',
values: { status: 'approved', rating: 4 },
})
expect(cli.stdout()).toBe('Updated status, rating on a-1\n')
})
it.each([
['{not json', 'Not valid JSON.'],
['["status"]', 'Expected a JSON object'],
])('rejects --values %s', async (value, message) => {
const cli = setup()
await expect(cli.run('meta', 'set', 'a-1', '--values', value)).rejects.toThrow()
expect(cli.stderr()).toContain(message)
expect(cli.callOperation).not.toHaveBeenCalled()
})
})
describe('field ls', () => {
it("lists a project's fields with their choices", async () => {
const cli = setup()
cli.callOperation.mockResolvedValue({
fields: [
{
key: 'status',
name: 'Status',
type: 'select',
description: '',
readOnly: false,
creationContext: false,
options: [{ id: 'approved', name: 'Approved' }],
},
{
key: 'prompt',
name: 'Prompt',
type: 'text',
description: '',
readOnly: false,
creationContext: true,
},
],
})
await cli.run('field', 'ls', 'p-1')
expect(cli.callOperation).toHaveBeenCalledWith(listMetadataFields, { projectId: 'p-1' })
expect(cli.stdout()).toBe(
'status Status (select) [approved=Approved]\nprompt Prompt (text, creation)\n',
)
})
})
describe('version', () => {
it('stacks files, lists versions, and unstacks one', async () => {
const cli = setup()
cli.callOperation.mockResolvedValueOnce({
id: 's-1',
name: 'cut_v2.mp4',
type: 'version_stack',
size: 20,
projectId: 'p-1',
})
cli.callOperation.mockResolvedValueOnce({
versions: [
{
id: 'v-2',
version: 2,
name: 'cut_v2.mp4',
current: true,
createdAt: '2026-01-02T00:00:00.000Z',
creator: null,
agent: null,
},
{
id: 'v-1',
version: 1,
name: 'cut_v1.mp4',
current: false,
createdAt: '2026-01-01T00:00:00.000Z',
creator: null,
agent: null,
},
],
})
cli.callOperation.mockResolvedValueOnce({ versionId: 'v-1', stackId: 's-1' })
await cli.run('version', 'stack', 'v-2', 'v-1')
await cli.run('version', 'ls', 's-1')
await cli.run('version', 'unstack', 'v-1')
expect(cli.callOperation).toHaveBeenNthCalledWith(1, createVersionStack, {
fileIds: ['v-2', 'v-1'],
})
expect(cli.callOperation).toHaveBeenNthCalledWith(2, listVersions, { stackId: 's-1' })
expect(cli.callOperation).toHaveBeenNthCalledWith(3, removeVersion, { versionId: 'v-1' })
expect(cli.stdout()).toBe(
'Created version stack s-1\n' +
'v2 (current) v-2 cut_v2.mp4\nv1 v-1 cut_v1.mp4\n' +
'Removed v-1 from version stack s-1\n',
)
})
})
describe('instructions', () => {
it("prints a folder's instructions, or says it has none", async () => {
const cli = setup()
cli.callOperation.mockResolvedValueOnce({ folderId: 'f-1', content: '# Rules' })
cli.callOperation.mockResolvedValueOnce({ folderId: 'f-2', content: null })
await cli.run('instructions', 'f-1')
await cli.run('instructions', 'f-2')
expect(cli.callOperation).toHaveBeenCalledWith(getFolderInstructions, { folderId: 'f-1' })
expect(cli.stdout()).toBe('# Rules\n(This folder has no instructions.)\n')
})
})
it('has no upload Operation commands: upload and create-version stream local files', () => {
const program = createProgram({ callOperation: vi.fn<CallOperation>() })
const names = program.commands.map((c) => c.name())
expect(names).not.toContain('complete-upload')
expect(names.filter((n) => n === 'create-version')).toHaveLength(1)
})
})
describe('formatCliError', () => {
beforeEach(() => vi.restoreAllMocks())
it('formats expected and unexpected errors', () => {
expect(formatCliError(new CliError('boom'))).toBe('Error: boom')
expect(
formatCliError(new CliError('Error connecting to API server: x', { prefixed: true })),
).toBe('Error connecting to API server: x')
expect(formatCliError(new TypeError('bad'))).toBe('Unexpected error: bad')
})
})
+59
View File
@@ -0,0 +1,59 @@
import { Command } from 'commander'
import { operationDefinitions } from '@shumai/operations'
import { createVersion } from './commands/create-version'
import { upload } from './commands/upload'
import { callOperation, type CallOperation } from './operations/call-operation'
import { cliExtensions } from './operations/extensions'
import { registerOperationCommands } from './operations/generate'
import type { Output } from './operations/render'
export interface ProgramOptions {
callOperation?: CallOperation
output?: Output
}
function isDeleteAllowed(): boolean {
const value = process.env.SHUMAI_ALLOW_DELETE
return value === 'true' || value === '1'
}
/**
* Builds the `shumai-cli` program. Operation commands (ls, mkdir, rename, move, delete,
* read, render, project ls, ...) are generated from `@shumai/operations`; upload and create-version stream
* local files and are defined here.
*/
export function createProgram(options: ProgramOptions = {}): Command {
const program = new Command()
.name('shumai-cli')
.description('Command line interface for the Shumai media workspace.')
.option('--json', 'Print raw JSON output instead of human-friendly text')
.showHelpAfterError()
.showSuggestionAfterError()
registerOperationCommands(program, operationDefinitions, {
callOperation: options.callOperation ?? callOperation,
output: options.output ?? {
out: process.stdout,
isTty: Boolean(process.stdout.isTTY),
columns: process.stdout.columns || 80,
},
allowDestructive: isDeleteAllowed,
extensions: cliExtensions,
})
program
.command('upload')
.description('Upload a local file or folder to a parent folder.')
.argument('<path>', 'Local file or folder to upload.')
.requiredOption('-p, --parent <parentId>', 'The parent folder ID to upload into.')
.action((localPath: string, opts: { parent: string }) => upload(localPath, opts.parent))
program
.command('create-version')
.description('Upload a local file as a new version of an existing file or version stack.')
.argument('<path>', 'Local file to upload as the new version.')
.requiredOption('-p, --parent <parentId>', 'The file or version stack ID to add a version to.')
.action((localPath: string, opts: { parent: string }) => createVersion(localPath, opts.parent))
return program
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/premiere-uxp",
"version": "0.6.0",
"version": "0.6.2",
"private": true,
"type": "commonjs",
"scripts": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/transcode-app",
"version": "0.6.0",
"version": "0.6.2",
"private": true,
"type": "module",
"bin": {
+1
View File
@@ -66,6 +66,7 @@ apps/web/e2e/
| Owner removes a member via the members dialog; the membership is deleted from the DB | `tests/team/remove-member.spec.ts` |
| Owner changes a team member role in the members dialog and permissions update | `tests/team/change-member-role.spec.ts` |
| Owner manages resource quotas in settings and resets usage from the dashboard | `tests/team/quotas-settings.spec.ts` |
| Owner creates an API token that is shown once, listed by hint, and authenticates API calls | `tests/team/api-tokens.spec.ts` |
### project
| Test | File |
@@ -0,0 +1,61 @@
import { createHash } from 'node:crypto'
import { expect, test } from '../../fixtures'
import { E2E_APP_URL } from '../../helpers/auth'
test('owner creates an API token that is shown once, listed by hint, and authenticates API calls', async ({
owner,
prisma,
playwright,
}) => {
const { page, teamId } = owner
// 1. Open the API Tokens settings tab
await page.goto(`/teams/${teamId}/settings`)
await page.getByRole('button', { name: /^(API Tokens|API 令牌)$/i }).click()
await expect(page.getByText(/No API tokens found|未找到 API 令牌/i)).toBeVisible()
// 2. Generate a token
await page.fill('#tokenName', 'e2e-cli')
await page.getByRole('button', { name: /^(Generate|生成)$/i }).click()
// 3. The plaintext is shown once in a dialog
const dialog = page.getByRole('dialog')
await expect(dialog.getByText(/Save your API token|保存您的 API 令牌/i)).toBeVisible()
const tokenInput = dialog.getByRole('textbox')
await expect(tokenInput).toHaveValue(/^shumai_[A-Za-z0-9_-]{43}$/)
const token = await tokenInput.inputValue()
await dialog.getByRole('button', { name: /^(Done|完成)$/i }).click()
await expect(dialog).toBeHidden()
// 4. The list identifies the token by name and hint only
const hint = token.slice(-4)
const row = page.getByRole('row', { name: /e2e-cli/ })
await expect(row).toBeVisible()
await expect(row.getByText(`…${hint}`)).toBeVisible()
await expect(page.getByText(token)).toHaveCount(0)
// 5. Only the hash is stored
const stored = await prisma.apiToken.findFirstOrThrow({ where: { name: 'e2e-cli' } })
expect(stored.tokenHash).toBe(createHash('sha256').update(token, 'utf8').digest('hex'))
expect(stored.tokenHint).toBe(hint)
// 6. The token authenticates a request that carries no session cookie
const tokenClient = await playwright.request.newContext({
// eslint-disable-next-line @typescript-eslint/naming-convention
baseURL: E2E_APP_URL,
})
try {
const ok = await tokenClient.get('/api/projects', {
headers: { Authorization: `Bearer ${token}` },
})
expect(ok.status()).toBe(200)
const rejected = await tokenClient.get('/api/projects', {
headers: { Authorization: `Bearer ${stored.tokenHash}` },
})
expect(rejected.status()).toBe(401)
} finally {
await tokenClient.dispose()
}
})
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/web-app",
"version": "0.6.0",
"version": "0.6.2",
"private": true,
"type": "module",
"dependencies": {
+3
View File
@@ -134,6 +134,9 @@ async function run() {
const routes: Record<string, any> = {
'/api/*': app.fetch,
'/files/*': app.fetch,
'/.well-known/*': app.fetch,
// Shumai's MCP server; without this entry the SPA catch-all (`/*`) would serve it HTML.
'/mcp': app.fetch,
}
if (isProd) {
+63 -48
View File
@@ -11,14 +11,13 @@
"@testcontainers/postgresql": "12.1.0",
"@testing-library/dom": "10.4.1",
"@testing-library/react": "16.3.3",
"@types/bun": "1.4.0",
"@types/bun": "1.4.3",
"@types/node": "25.9.1",
"bun-plugin-pino": "1.5.0",
"bun-plugin-tailwind": "0.1.2",
"bun-types": "1.4.0",
"bun-types": "1.4.3",
"eslint": "10.9.1",
"eslint-config-prettier": "10.1.8",
"eslint-plugin-prettier": "5.5.6",
"eslint-plugin-react": "7.37.5",
"globals": "17.11.0",
"happy-dom": "20.12.0",
@@ -33,7 +32,7 @@
},
"apps/agent": {
"name": "@shumai/agent-app",
"version": "0.6.0",
"version": "0.6.2",
"bin": {
"shumai-agent": "./src/index.ts",
},
@@ -45,22 +44,24 @@
},
"apps/cli": {
"name": "@shumai/cli",
"version": "0.6.0",
"version": "0.6.2",
"bin": {
"shumai-cli": "./src/index.ts",
},
"dependencies": {
"@shumai/api": "workspace:*",
"@shumai/core": "workspace:*",
"@shumai/dtos": "workspace:*",
"@shumai/operations": "workspace:*",
"@shumai/tableprinter": "workspace:*",
"commander": "15.0.0",
"hono": "4.13.5",
"ulid": "3.0.2",
"zod": "4.5.4",
},
},
"apps/premiere-uxp": {
"name": "@shumai/premiere-uxp",
"version": "0.6.0",
"version": "0.6.2",
"dependencies": {
"@shumai/api": "workspace:*",
"@shumai/dtos": "workspace:*",
@@ -114,7 +115,7 @@
},
"apps/transcode": {
"name": "@shumai/transcode-app",
"version": "0.6.0",
"version": "0.6.2",
"bin": {
"shumai-transcode": "./src/index.ts",
},
@@ -126,7 +127,7 @@
},
"apps/web": {
"name": "@shumai/web-app",
"version": "0.6.0",
"version": "0.6.2",
"dependencies": {
"@shumai/agent": "workspace:*",
"@shumai/api": "workspace:*",
@@ -146,7 +147,7 @@
},
"packages/agent": {
"name": "@shumai/agent",
"version": "0.6.0",
"version": "0.6.2",
"dependencies": {
"@anthropic-ai/sandbox-runtime": "0.0.74",
"@earendil-works/pi-agent-core": "0.82.1",
@@ -155,6 +156,7 @@
"@shumai/core": "workspace:*",
"@shumai/db": "workspace:*",
"@shumai/dtos": "workspace:*",
"@shumai/operations": "workspace:*",
"@shumai/workflow-core": "workspace:*",
"adm-zip": "0.6.0",
"jittered-fractional-indexing": "1.0.1",
@@ -169,21 +171,27 @@
},
"packages/api": {
"name": "@shumai/api",
"version": "0.6.0",
"version": "0.6.2",
"dependencies": {
"@better-auth/mcp": "1.7.12",
"@hono/zod-validator": "0.7.6",
"@modelcontextprotocol/server": "2.0.0",
"@shumai/core": "workspace:*",
"@shumai/db": "workspace:*",
"@shumai/dtos": "workspace:*",
"@shumai/operations": "workspace:*",
"hono": "4.13.5",
"pino": "10.3.1",
"ulid": "3.0.2",
"zod": "4.5.4",
},
"devDependencies": {
"@modelcontextprotocol/client": "2.0.0",
},
},
"packages/core": {
"name": "@shumai/core",
"version": "0.6.0",
"version": "0.6.2",
"dependencies": {
"@ai-sdk/alibaba": "2.0.38",
"@ai-sdk/amazon-bedrock": "5.0.67",
@@ -206,16 +214,19 @@
"@anthropic-ai/sandbox-runtime": "0.0.74",
"@aws-sdk/client-s3": "3.1121.0",
"@aws-sdk/s3-request-presigner": "3.1121.0",
"@better-auth/mcp": "1.7.12",
"@better-auth/oauth-provider": "1.7.12",
"@earendil-works/pi-agent-core": "0.82.1",
"@earendil-works/pi-ai": "0.82.1",
"@google/genai": "2.28.0",
"@modelcontextprotocol/client": "2.0.0",
"@shumai/db": "workspace:*",
"@shumai/dtos": "workspace:*",
"@shumai/operations": "workspace:*",
"@sinclair/typebox": "0.34.52",
"adm-zip": "0.6.0",
"ai": "7.0.84",
"better-auth": "1.6.11",
"better-auth": "1.7.12",
"csv-parse": "7.0.2",
"date-fns": "4.4.0",
"dotenv": "17.4.2",
@@ -244,7 +255,7 @@
},
"packages/db": {
"name": "@shumai/db",
"version": "0.6.0",
"version": "0.6.2",
"dependencies": {
"@earendil-works/pi-agent-core": "0.82.1",
"@prisma/adapter-pg": "7.10.0",
@@ -260,7 +271,7 @@
},
"packages/dtos": {
"name": "@shumai/dtos",
"version": "0.6.0",
"version": "0.6.2",
"dependencies": {
"@earendil-works/pi-agent-core": "0.82.1",
"zod": "4.5.4",
@@ -268,7 +279,7 @@
},
"packages/e2e": {
"name": "@shumai/e2e",
"version": "0.6.0",
"version": "0.6.2",
"dependencies": {
"@earendil-works/pi-agent-core": "0.82.1",
"@google/genai": "2.28.0",
@@ -281,13 +292,20 @@
"sharp": "0.35.4",
},
},
"packages/operations": {
"name": "@shumai/operations",
"version": "0.6.2",
"dependencies": {
"zod": "4.5.4",
},
},
"packages/tableprinter": {
"name": "@shumai/tableprinter",
"version": "0.6.0",
"version": "0.6.2",
},
"packages/transcode": {
"name": "@shumai/transcode",
"version": "0.6.0",
"version": "0.6.2",
"dependencies": {
"@shumai/core": "workspace:*",
"@shumai/db": "workspace:*",
@@ -298,9 +316,10 @@
},
"packages/webui": {
"name": "@shumai/webui",
"version": "0.6.0",
"version": "0.6.2",
"dependencies": {
"@base-ui/react": "1.7.0",
"@better-auth/oauth-provider": "1.7.12",
"@dnd-kit/abstract": "0.5.0",
"@dnd-kit/dom": "0.5.0",
"@dnd-kit/react": "0.5.0",
@@ -352,7 +371,7 @@
"@uppy/aws-s3": "6.0.0",
"@uppy/core": "6.0.0",
"@uppy/xhr-upload": "6.0.0",
"better-auth": "1.6.11",
"better-auth": "1.7.12",
"class-variance-authority": "0.7.1",
"clsx": "2.1.1",
"cmdk": "1.1.1",
@@ -400,7 +419,7 @@
},
"packages/workflow-core": {
"name": "@shumai/workflow-core",
"version": "0.6.0",
"version": "0.6.2",
"dependencies": {
"@dbos-inc/dbos-sdk": "5.2.11",
"@shumai/core": "workspace:*",
@@ -572,23 +591,27 @@
"@base-ui/utils": ["@base-ui/utils@0.3.2", "", { "dependencies": { "@babel/runtime": "^7.29.2", "@floating-ui/utils": "^0.2.12", "reselect": "^5.2.0", "use-sync-external-store": "^1.6.0" }, "peerDependencies": { "@types/react": "^17 || ^18 || ^19", "react": "^17 || ^18 || ^19", "react-dom": "^17 || ^18 || ^19" }, "optionalPeers": ["@types/react"] }, "sha512-oWy1aq/I2GmYjpl4PhEAhzflF8VPGKgZeq0xAWTbfD5KBWyxcN0ZP2+WHSUm/5Z6lVMBDLReLcoXwSYoRc/zNQ=="],
"@better-auth/core": ["@better-auth/core@1.6.11", "", { "dependencies": { "@opentelemetry/semantic-conventions": "^1.39.0", "@standard-schema/spec": "^1.1.0", "zod": "^4.3.6" }, "peerDependencies": { "@better-auth/utils": "0.4.0", "@better-fetch/fetch": "1.1.21", "@cloudflare/workers-types": ">=4", "@opentelemetry/api": "^1.9.0", "better-call": "1.3.5", "jose": "^6.1.0", "kysely": "^0.28.5", "nanostores": "^1.0.1" }, "optionalPeers": ["@cloudflare/workers-types", "@opentelemetry/api"] }, "sha512-LrwidLCV8azdMGjvtwp30nj9tIv1BwI3VhtC0UaGSjQkAVWw4bN42I8qwbxRziPeSQoj+zUVkOpxZzAWBDARtQ=="],
"@better-auth/core": ["@better-auth/core@1.7.12", "", { "dependencies": { "@standard-schema/spec": "^1.1.0", "zod": "^4.5.4" }, "peerDependencies": { "@better-auth/utils": "^0.4.2 || ^0.5.0", "@better-fetch/fetch": "1.3.2", "@opentelemetry/api": "^1.9.0", "better-call": "1.4.4", "jose": "^6.1.0", "kysely": "^0.28.5 || ^0.29.0", "nanostores": "^1.0.1" }, "optionalPeers": ["@opentelemetry/api"] }, "sha512-SUStBGHJ13KnSO6+7U/1DSlfuOtln4jtOrFZqzQtxZVHZIrAoMjwHNGitlF+s7hvKbuC3Z14xjnqfLLqIqGfxw=="],
"@better-auth/drizzle-adapter": ["@better-auth/drizzle-adapter@1.6.11", "", { "peerDependencies": { "@better-auth/core": "^1.6.11", "@better-auth/utils": "0.4.0", "drizzle-orm": "^0.45.2" }, "optionalPeers": ["drizzle-orm"] }, "sha512-4jpkETIGZOHCf7BK4jnu22fdN6jjomH0/HhEzkaWy3+Eppi5PYlHTF/460jrTmA3Xc+Vqwp9t282ymHiEPypGw=="],
"@better-auth/drizzle-adapter": ["@better-auth/drizzle-adapter@1.7.12", "", { "peerDependencies": { "@better-auth/core": "^1.7.12", "@better-auth/utils": "^0.4.2 || ^0.5.0", "drizzle-orm": "^0.45.2 || >=1.0.0-rc.1 <2.0.0" }, "optionalPeers": ["drizzle-orm"] }, "sha512-Qzoyuf05aK01Pc0k3EXv8Xj3XCT2m2dCWc5HoB7x+xX6N3Pm2eQsca+UIArsJcbuNEWWP3L51oIebnBoHJ5DTw=="],
"@better-auth/kysely-adapter": ["@better-auth/kysely-adapter@1.6.11", "", { "peerDependencies": { "@better-auth/core": "^1.6.11", "@better-auth/utils": "0.4.0", "kysely": "^0.28.17" }, "optionalPeers": ["kysely"] }, "sha512-/g8M9RfIjdcZDnbstSUvQiINkvdNlCeZr248zwqx2/PVksQI1MhQofbzUn3RnQnbPKp0EPwpX/dR3oudRFenUg=="],
"@better-auth/kysely-adapter": ["@better-auth/kysely-adapter@1.7.12", "", { "peerDependencies": { "@better-auth/core": "^1.7.12", "@better-auth/utils": "^0.4.2 || ^0.5.0", "kysely": "^0.28.17 || ^0.29.0" }, "optionalPeers": ["kysely"] }, "sha512-1PaFXktLv4n3DmrGbPaCj5SsvcT40NnWw7NLua5/J1+8/A051N5ZzqOwJifXEIQX5k6NdwvmymwDQxKytduEAQ=="],
"@better-auth/memory-adapter": ["@better-auth/memory-adapter@1.6.11", "", { "peerDependencies": { "@better-auth/core": "^1.6.11", "@better-auth/utils": "0.4.0" } }, "sha512-hpdfw0BBf8MuzLkIdmbcUZICbY9r/bhLO2RxSnkzT5+/O+0I0u2I8+m0YUP7vNllP/ZCKASHOYgXPLO75Z0f9Q=="],
"@better-auth/mcp": ["@better-auth/mcp@1.7.12", "", { "dependencies": { "@better-auth/oauth-provider": "^1.7.12", "jose": "^6.1.3" }, "peerDependencies": { "@better-auth/core": "^1.7.12", "better-auth": "^1.7.12", "better-call": "1.4.4" } }, "sha512-iiD6qW/aSBznMJ/t9v9/a3Obd1T2QeiBbbHZTLwusCLm+tzeH6fLHIgB7631TYQBuB/iEA+xpEs9mCy14MpQ8Q=="],
"@better-auth/mongo-adapter": ["@better-auth/mongo-adapter@1.6.11", "", { "peerDependencies": { "@better-auth/core": "^1.6.11", "@better-auth/utils": "0.4.0", "mongodb": "^6.0.0 || ^7.0.0" }, "optionalPeers": ["mongodb"] }, "sha512-3Tor8rSv8vSEIMEaV2PFpPEuVhqc1gNoZ6eGvoh3LwExXXuj8madew6ob+H1pH7Aphn3Ar5PQ08AguT8TbwFAA=="],
"@better-auth/memory-adapter": ["@better-auth/memory-adapter@1.7.12", "", { "peerDependencies": { "@better-auth/core": "^1.7.12", "@better-auth/utils": "^0.4.2 || ^0.5.0" } }, "sha512-glaDwXzDptnddHqRuu7fj47FAssUcOzLNgdlTGJoEPrspkX69x2vnXwgAzNuApjZI21WLtPH/rQ6CN+DQGybPA=="],
"@better-auth/prisma-adapter": ["@better-auth/prisma-adapter@1.6.11", "", { "peerDependencies": { "@better-auth/core": "^1.6.11", "@better-auth/utils": "0.4.0", "@prisma/client": "^5.0.0 || ^6.0.0 || ^7.0.0", "prisma": "^5.0.0 || ^6.0.0 || ^7.0.0" }, "optionalPeers": ["@prisma/client", "prisma"] }, "sha512-Pw+7q7zTp+VSci1V+CYMvuxIbAeVMZLe4lRo46LJoAKMHfjFl5T/ycsyFvWs/DkWC7n9gZZzRDEbHp0I5FiKKw=="],
"@better-auth/mongo-adapter": ["@better-auth/mongo-adapter@1.7.12", "", { "peerDependencies": { "@better-auth/core": "^1.7.12", "@better-auth/utils": "^0.4.2 || ^0.5.0", "mongodb": "^6.0.0 || ^7.0.0" }, "optionalPeers": ["mongodb"] }, "sha512-HKV4sgYSw/Gdt9AuHlESMkiKL72SWdGweDy1Wsyxoh06BpG7aNu4aSJ7marNpMdKZteyRbxWWqQkA9ediA8qFw=="],
"@better-auth/telemetry": ["@better-auth/telemetry@1.6.11", "", { "peerDependencies": { "@better-auth/core": "^1.6.11", "@better-auth/utils": "0.4.0", "@better-fetch/fetch": "1.1.21" } }, "sha512-hsjDHc8MZbm6/AHeNdtywrWedXevnBjmdvnHTcZub+rTVjOv+Td0roI8USKuC6uUibmrl//2rJfVCsGbopihNA=="],
"@better-auth/oauth-provider": ["@better-auth/oauth-provider@1.7.12", "", { "dependencies": { "jose": "^6.2.3", "zod": "^4.5.4" }, "peerDependencies": { "@better-auth/core": "^1.7.12", "@better-auth/utils": "^0.4.2 || ^0.5.0", "@better-fetch/fetch": "1.3.2", "better-auth": "^1.7.12", "better-call": "1.4.4" } }, "sha512-XzjlApbWtuuoEK63CUNHbgs8zpr3AjlQeGbT1vWEPqvOvI2z4lOaIvIsogomfqFAgj/jgB0zMGYjt0MRyW0T7w=="],
"@better-auth/utils": ["@better-auth/utils@0.4.0", "", { "dependencies": { "@noble/hashes": "^2.0.1" } }, "sha512-RpMtLUIQAEWMgdPLNVbIF5ON2mm+CH0U3rCdUCU1VyeAUui4m38DyK7/aXMLZov2YDjG684pS1D0MBllrmgjQA=="],
"@better-auth/prisma-adapter": ["@better-auth/prisma-adapter@1.7.12", "", { "peerDependencies": { "@better-auth/core": "^1.7.12", "@better-auth/utils": "^0.4.2 || ^0.5.0", "@prisma/client": "^5.0.0 || ^6.0.0 || ^7.0.0", "prisma": "^5.0.0 || ^6.0.0 || ^7.0.0" }, "optionalPeers": ["@prisma/client", "prisma"] }, "sha512-FZRX6ORbwhDmeYE1Tl0IPtUn8v3BlVFgbJxDnm/DCNHxuFRf+0t4HfddUgYeYehHFR8aSDsdYX44fEFWM9877w=="],
"@better-fetch/fetch": ["@better-fetch/fetch@1.1.21", "", {}, "sha512-/ImESw0sskqlVR94jB+5+Pxjf+xBwDZF/N5+y2/q4EqD7IARUTSpPfIo8uf39SYpCxyOCtbyYpUrZ3F/k0zT4A=="],
"@better-auth/telemetry": ["@better-auth/telemetry@1.7.12", "", { "peerDependencies": { "@better-auth/core": "^1.7.12", "@better-auth/utils": "^0.4.2 || ^0.5.0", "@better-fetch/fetch": "1.3.2" } }, "sha512-DaDkKNnI99vKGGbWq9KJg2uYeaOjsK9nIBw9T2m2KG/RkxpvQRjYkZ00++up2bZQkR5pB1nukGbplza3UJG/Xg=="],
"@better-auth/utils": ["@better-auth/utils@0.5.0", "", { "dependencies": { "@noble/hashes": "^2.0.1" } }, "sha512-BL8W4EfIZFwlu0r54m3v1ztjDhu6dDe/amLTm0xybmbZaNgYUqhD3SjpAsnq0q8YD6/ki4iwIgxJNLP/N3TxiA=="],
"@better-fetch/fetch": ["@better-fetch/fetch@1.3.2", "", {}, "sha512-Gs7n99b5tqUC6cQAPbV0uED3IraHB6xQbHLQ/C3l7ZFafHScOx9pQ+DYmP5blbLFShVWLqxNUlI9wi4xU/X+ow=="],
"@date-fns/tz": ["@date-fns/tz@1.5.0", "", {}, "sha512-lwYN/vDPeNRULcepoE/LO2Pgx+7/RV+S9ARfbc9lr2DtGkOD7pAiruHvbR1RX3Qyf6ja47EWJDMsNK5vK08DJg=="],
@@ -882,8 +905,6 @@
"@pkgjs/parseargs": ["@pkgjs/parseargs@0.11.0", "", {}, "sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg=="],
"@pkgr/core": ["@pkgr/core@0.3.6", "", {}, "sha512-SEeaJLb3qBNF/OaXnaR1NmmBbFYk1zC0ZH/52fATcRPLFg/p791YrcyFFy44Bo9sLaGuSuLp5Q6axbb/O+v/RA=="],
"@playwright/test": ["@playwright/test@1.61.1", "", { "dependencies": { "playwright": "1.61.1" }, "bin": { "playwright": "cli.js" } }, "sha512-8nKv6+0RJSL9FE4jYOEGXnPeM/Hg12qZpmqzZjRh3qM0Y7c3z1mrOTfFLids72RDQYVh9WpLEfR5WdpNX4fkig=="],
"@pondwader/socks5-server": ["@pondwader/socks5-server@1.0.10", "", {}, "sha512-bQY06wzzR8D2+vVCUoBsr5QS2U6UgPUQRmErNwtsuI6vLcyRKkafjkr3KxbtGFf9aBBIV2mcvlsKD1UYaIV+sg=="],
@@ -1112,6 +1133,8 @@
"@shumai/e2e": ["@shumai/e2e@workspace:packages/e2e"],
"@shumai/operations": ["@shumai/operations@workspace:packages/operations"],
"@shumai/premiere-uxp": ["@shumai/premiere-uxp@workspace:apps/premiere-uxp"],
"@shumai/tableprinter": ["@shumai/tableprinter@workspace:packages/tableprinter"],
@@ -1428,7 +1451,7 @@
"@types/aria-query": ["@types/aria-query@5.0.4", "", {}, "sha512-rfT93uj5s0PRL7EzccGMs3brplhcrghnDoV26NqKhCAS1hVo+WdNsPvE/yb6ilfr5hi2MEk6d5EWJTKdxg8jVw=="],
"@types/bun": ["@types/bun@1.4.0", "", { "dependencies": { "bun-types": "1.4.0" } }, "sha512-K+lZULY23vRgK/CfTjFIV+tyifaNdSMlPh9j+6mQ/cLfpOznLyAuzgV/JQysyECpkBQLVMSyvjlr2fBUSA9wFQ=="],
"@types/bun": ["@types/bun@1.4.3", "", { "dependencies": { "bun-types": "1.4.3" } }, "sha512-N67Pqy8JXbAY/j0tNraS3V5mubGvc6rrrNDmDCtZLlFzccbUJPDb0tvgUBC5q4m8ybkP93Jcn1QB+RYQ6PvBhw=="],
"@types/chai": ["@types/chai@5.2.3", "", { "dependencies": { "@types/deep-eql": "*", "assertion-error": "^2.0.1" } }, "sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA=="],
@@ -1674,9 +1697,9 @@
"bcrypt-pbkdf": ["bcrypt-pbkdf@1.0.2", "", { "dependencies": { "tweetnacl": "^0.14.3" } }, "sha512-qeFIXtP4MSoi6NLqO12WfqARWWuCKi2Rn/9hJLEmtB5yTNr9DqFWkJRCf2qShWzPeAMRnOgCrq0sg/KLv5ES9w=="],
"better-auth": ["better-auth@1.6.11", "", { "dependencies": { "@better-auth/core": "1.6.11", "@better-auth/drizzle-adapter": "1.6.11", "@better-auth/kysely-adapter": "1.6.11", "@better-auth/memory-adapter": "1.6.11", "@better-auth/mongo-adapter": "1.6.11", "@better-auth/prisma-adapter": "1.6.11", "@better-auth/telemetry": "1.6.11", "@better-auth/utils": "0.4.0", "@better-fetch/fetch": "1.1.21", "@noble/ciphers": "^2.1.1", "@noble/hashes": "^2.0.1", "better-call": "1.3.5", "defu": "^6.1.4", "jose": "^6.1.3", "kysely": "^0.28.17", "nanostores": "^1.1.1", "zod": "^4.3.6" }, "peerDependencies": { "@lynx-js/react": "*", "@prisma/client": "^5.0.0 || ^6.0.0 || ^7.0.0", "@sveltejs/kit": "^2.0.0", "@tanstack/react-start": "^1.0.0", "@tanstack/solid-start": "^1.0.0", "better-sqlite3": "^12.0.0", "drizzle-kit": ">=0.31.4", "drizzle-orm": "^0.45.2", "mongodb": "^6.0.0 || ^7.0.0", "mysql2": "^3.0.0", "next": "^14.0.0 || ^15.0.0 || ^16.0.0", "pg": "^8.0.0", "prisma": "^5.0.0 || ^6.0.0 || ^7.0.0", "react": "^18.0.0 || ^19.0.0", "react-dom": "^18.0.0 || ^19.0.0", "solid-js": "^1.0.0", "svelte": "^4.0.0 || ^5.0.0", "vitest": "^2.0.0 || ^3.0.0 || ^4.0.0", "vue": "^3.0.0" }, "optionalPeers": ["@lynx-js/react", "@prisma/client", "@sveltejs/kit", "@tanstack/react-start", "@tanstack/solid-start", "better-sqlite3", "drizzle-kit", "drizzle-orm", "mongodb", "mysql2", "next", "pg", "prisma", "react", "react-dom", "solid-js", "svelte", "vitest", "vue"] }, "sha512-Wwt6+q07dwIhsp6XiM7L1qSXVUWBEtNl+eZvwM778CguFqDZFBN9Pt6LtFaHl55t8Z+Zc//5kxcbgDY8/79vFQ=="],
"better-auth": ["better-auth@1.7.12", "", { "dependencies": { "@better-auth/core": "1.7.12", "@better-auth/drizzle-adapter": "1.7.12", "@better-auth/kysely-adapter": "1.7.12", "@better-auth/memory-adapter": "1.7.12", "@better-auth/mongo-adapter": "1.7.12", "@better-auth/prisma-adapter": "1.7.12", "@better-auth/telemetry": "1.7.12", "@better-auth/utils": "0.5.0", "@better-fetch/fetch": "1.3.2", "@noble/ciphers": "^2.2.0", "@noble/hashes": "^2.2.0", "better-call": "1.4.4", "defu": "^6.1.4", "jose": "^6.2.3", "kysely": "^0.28.17 || ^0.29.0", "nanostores": "^1.3.0", "zod": "^4.5.4" }, "peerDependencies": { "@lynx-js/react": "*", "@prisma/client": "^5.0.0 || ^6.0.0 || ^7.0.0", "@sveltejs/kit": "^2.0.0 || ^3.0.0", "@tanstack/react-start": "^1.0.0", "@tanstack/solid-start": "^1.0.0", "drizzle-kit": ">=0.31.4 || >=1.0.0-beta.1", "drizzle-orm": "^0.45.2 || >=1.0.0-rc.1 <2.0.0", "mongodb": "^6.0.0 || ^7.0.0", "mysql2": "^3.0.0", "next": "^14.0.0 || ^15.0.0 || ^16.0.0", "pg": "^8.0.0", "prisma": "^5.0.0 || ^6.0.0 || ^7.0.0", "react": "^18.0.0 || ^19.0.0", "react-dom": "^18.0.0 || ^19.0.0", "solid-js": "^1.0.0", "svelte": "^4.0.0 || ^5.0.0", "vitest": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0", "vue": "^3.0.0" }, "optionalPeers": ["@lynx-js/react", "@prisma/client", "@sveltejs/kit", "@tanstack/react-start", "@tanstack/solid-start", "drizzle-kit", "drizzle-orm", "mongodb", "mysql2", "next", "pg", "prisma", "react", "react-dom", "solid-js", "svelte", "vitest", "vue"] }, "sha512-f1fEG0QdTslzbmFA+vOGFFW/XXCWkG4kBxLeH9n1vp1otTH9+ZzV6EJ6797GStWeiYYgS5Vnt9xqE12KwSyAAw=="],
"better-call": ["better-call@1.3.5", "", { "dependencies": { "@better-auth/utils": "^0.4.0", "@better-fetch/fetch": "^1.1.21", "rou3": "^0.7.12", "set-cookie-parser": "^3.0.1" }, "peerDependencies": { "zod": "^4.0.0" }, "optionalPeers": ["zod"] }, "sha512-kOFJkBP7utAQLEYrobZm3vkTH8mXq5GNgvjc5/XEST1ilVHaxXUXfeDeFlqoETMtyqS4+3/h4ONX2i++ebZrvA=="],
"better-call": ["better-call@1.4.4", "", { "dependencies": { "@better-auth/utils": "^0.5.0", "@better-fetch/fetch": "^1.3.1", "rou3": "^0.9.1", "set-cookie-parser": "^3.1.2" }, "peerDependencies": { "zod": "^4.0.0" }, "optionalPeers": ["zod"] }, "sha512-Gq4XdGFy4dmUxFEAX2nj0ycFcFbD9ZWh8YROouZoyXeF1M11TpYbplk0pDByTFF8Wz+MYa6W9052c9m7dD+mfw=="],
"better-result": ["better-result@2.10.0", "", {}, "sha512-oQhh0y1qo2/ZKdAAEvHZAqKKiHOFU5k/bW96fE2ScgQOVkJRiHwB+nOS1SgFsYqRlxMDWvefXi9Q3px7QvgNDw=="],
@@ -1708,7 +1731,7 @@
"bun-plugin-tailwind": ["bun-plugin-tailwind@0.1.2", "", { "peerDependencies": { "bun": ">=1.0.0" } }, "sha512-41jNC1tZRSK3s1o7pTNrLuQG8kL/0vR/JgiTmZAJ1eHwe0w5j6HFPKeqEk0WAD13jfrUC7+ULuewFBBCoADPpg=="],
"bun-types": ["bun-types@1.4.0", "", { "dependencies": { "@types/node": "*" } }, "sha512-iIKw23BspnQQYd3prITOBxeUsxBHnwzX6YJfGMuNOZzeNcMmVqzIIVGRm1l69ogaPQmb4wB6BN8mA5bE9YuC5Q=="],
"bun-types": ["bun-types@1.4.3", "", { "dependencies": { "@types/node": "*", "undici-types": "*" } }, "sha512-tinRJNGOmUN+tEAsn2y2e942n9JHRHR42iUYzDH75NmhJqVBokGSQkkfSFdNWwsANIdrK6ACH9t01M452YyTwg=="],
"byline": ["byline@5.0.0", "", {}, "sha512-s6webAy+R4SR8XVuJWt2V2rGvhnrhxN+9S15GNuTK3wKPOXFF6RNc+8ug2XhH+2s4f+uudG4kUVYmYOQWL2g0Q=="],
@@ -1762,7 +1785,7 @@
"comma-separated-tokens": ["comma-separated-tokens@2.0.3", "", {}, "sha512-Fu4hJdvzeylCfQPp9SGWidpzrMs7tTrlu6Vb8XGaRGck8QSNZJJp538Wrb60Lax4fPwR64ViY468OIUTbRlGZg=="],
"commander": ["commander@11.1.0", "", {}, "sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ=="],
"commander": ["commander@15.0.0", "", {}, "sha512-z67u4ZhzCL/Tydu1lJARtEZYWbWaN7oYLHbsuzocr6y4N6WZAagG3RQ4FW61V1/0+jImpj293XfrcYnd1qxtPg=="],
"comment-json": ["comment-json@4.6.2", "", { "dependencies": { "array-timsort": "^1.0.3", "esprima": "^4.0.1" } }, "sha512-R2rze/hDX30uul4NZoIZ76ImSJLFxn/1/ZxtKC1L77y2X1k+yYu1joKbAtMA2Fg3hZrTOiw0I5mwVMo0cf250w=="],
@@ -1942,8 +1965,6 @@
"eslint-config-prettier": ["eslint-config-prettier@10.1.8", "", { "peerDependencies": { "eslint": ">=7.0.0" }, "bin": { "eslint-config-prettier": "bin/cli.js" } }, "sha512-82GZUjRS0p/jganf6q1rEO25VSoHH0hKPCTrgillPjdI/3bgBhAE1QzHrHTizjpRvy6pGAvKjDJtk2pF9NDq8w=="],
"eslint-plugin-prettier": ["eslint-plugin-prettier@5.5.6", "", { "dependencies": { "prettier-linter-helpers": "^1.0.1", "synckit": "^0.11.13" }, "peerDependencies": { "@types/eslint": ">=8.0.0", "eslint": ">=8.0.0", "eslint-config-prettier": ">= 7.0.0 <10.0.0 || >=10.1.0", "prettier": ">=3.0.0" }, "optionalPeers": ["@types/eslint", "eslint-config-prettier"] }, "sha512-ifetmTcxWfz+4qRW3pH/ujdTq2jQIj59AxJMIN26K5avYgU8dxycUETQonWiW+wPrYXA0j3Try0l1CnwVQtDqQ=="],
"eslint-plugin-react": ["eslint-plugin-react@7.37.5", "", { "dependencies": { "array-includes": "^3.1.8", "array.prototype.findlast": "^1.2.5", "array.prototype.flatmap": "^1.3.3", "array.prototype.tosorted": "^1.1.4", "doctrine": "^2.1.0", "es-iterator-helpers": "^1.2.1", "estraverse": "^5.3.0", "hasown": "^2.0.2", "jsx-ast-utils": "^2.4.1 || ^3.0.0", "minimatch": "^3.1.2", "object.entries": "^1.1.9", "object.fromentries": "^2.0.8", "object.values": "^1.2.1", "prop-types": "^15.8.1", "resolve": "^2.0.0-next.5", "semver": "^6.3.1", "string.prototype.matchall": "^4.0.12", "string.prototype.repeat": "^1.0.0" }, "peerDependencies": { "eslint": "^3 || ^4 || ^5 || ^6 || ^7 || ^8 || ^9.7" } }, "sha512-Qteup0SqU15kdocexFNAJMvCJEfa2xUKNV4CC1xsVMrIIqEy3SQ/rqyxCWNzfrd3/ldy6HMlD2e0JDVpDg2qIA=="],
"eslint-scope": ["eslint-scope@9.1.2", "", { "dependencies": { "@types/esrecurse": "^4.3.1", "@types/estree": "^1.0.8", "esrecurse": "^4.3.0", "estraverse": "^5.2.0" } }, "sha512-xS90H51cKw0jltxmvmHy2Iai1LIqrfbw57b79w/J7MfvDfkIkFZ+kj6zC3BjtUwh150HsSSdxXZcsuv72miDFQ=="],
@@ -2000,8 +2021,6 @@
"fast-deep-equal": ["fast-deep-equal@3.1.3", "", {}, "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="],
"fast-diff": ["fast-diff@1.3.0", "", {}, "sha512-VxPP4NqbUjj6MaAOafWeUn2cXWLcCtljklUtZf0Ind4XQ+QPtmA0b18zZy0jIQx+ExRVCR/ZQpBmik5lXshNsw=="],
"fast-fifo": ["fast-fifo@1.3.2", "", {}, "sha512-/d9sfos4yxzpwkDkuN7k2SqFKtYNmCTzgfEpz82x34IM9/zc8KGxQoXg1liNC/izpRM/MBdt44Nmx41ZWqk+FQ=="],
"fast-json-stable-stringify": ["fast-json-stable-stringify@2.1.0", "", {}, "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw=="],
@@ -2576,8 +2595,6 @@
"prettier": ["prettier@3.9.6", "", { "bin": { "prettier": "bin/prettier.cjs" } }, "sha512-OpN0zzVdiaiAhxpuuj5efpIS4sY9j7bY6uR5mnj5yPzGkdkjNKSJeUThPb60Jw29QuAZgA4o+/iB49kFiaBX6g=="],
"prettier-linter-helpers": ["prettier-linter-helpers@1.0.1", "", { "dependencies": { "fast-diff": "^1.1.2" } }, "sha512-SxToR7P8Y2lWmv/kTzVLC1t/GDI2WGjMwNhLLE9qtH8Q13C+aEmuRlzDst4Up4s0Wc8sF2M+J57iB3cMLqftfg=="],
"pretty-format": ["pretty-format@27.5.1", "", { "dependencies": { "ansi-regex": "^5.0.1", "ansi-styles": "^5.0.0", "react-is": "^17.0.1" } }, "sha512-Qb1gy5OrP5+zDf2Bvnzdl3jsTf1qXVMazbvCoKhtKqVs4/YK4ozX4gKQJJVyNe+cajNPn0KoC0MC3FUmaHWEmQ=="],
"prisma": ["prisma@7.10.0", "", { "dependencies": { "@prisma/config": "7.10.0", "@prisma/dev": "0.24.17", "@prisma/engines": "7.10.0", "@prisma/studio-core": "0.33.0", "mysql2": "3.15.3", "postgres": "3.4.7" }, "peerDependencies": { "better-sqlite3": ">=9.0.0", "typescript": ">=5.4.0" }, "optionalPeers": ["better-sqlite3", "typescript"], "bin": { "prisma": "build/index.js" } }, "sha512-o0ornyJOWgygVAzGCpr8PdXV8EJLHyVGDDUr/voBQt8Azzw8cYTByzzPGcA/m4tCkPcnJA8raEOv2CslsKhPEw=="],
@@ -2684,7 +2701,7 @@
"rolldown": ["rolldown@1.2.6", "", { "dependencies": { "@oxc-project/types": "=0.147.0", "@rolldown/pluginutils": "^1.0.0" }, "optionalDependencies": { "@rolldown/binding-android-arm-eabi": "1.2.6", "@rolldown/binding-android-arm64": "1.2.6", "@rolldown/binding-darwin-arm64": "1.2.6", "@rolldown/binding-darwin-x64": "1.2.6", "@rolldown/binding-freebsd-x64": "1.2.6", "@rolldown/binding-linux-arm-gnueabihf": "1.2.6", "@rolldown/binding-linux-arm64-gnu": "1.2.6", "@rolldown/binding-linux-arm64-musl": "1.2.6", "@rolldown/binding-linux-ppc64-gnu": "1.2.6", "@rolldown/binding-linux-s390x-gnu": "1.2.6", "@rolldown/binding-linux-x64-gnu": "1.2.6", "@rolldown/binding-linux-x64-musl": "1.2.6", "@rolldown/binding-openharmony-arm64": "1.2.6", "@rolldown/binding-win32-arm64-msvc": "1.2.6", "@rolldown/binding-win32-x64-msvc": "1.2.6" }, "bin": { "rolldown": "./bin/cli.mjs" } }, "sha512-vMM4q3aixf46GiF1Kok8jDPFsEpXgFWGjUHXNkNHNm+Y2adXAG2dbX91jkti3i0ZRsOlcmbuzAz1poObSHCmUA=="],
"rou3": ["rou3@0.7.12", "", {}, "sha512-iFE4hLDuloSWcD7mjdCDhx2bKcIsYbtOTpfH5MHHLSKMOUyjqQXTeZVa289uuwEGEKFoE/BAPbhaU4B774nceg=="],
"rou3": ["rou3@0.9.2", "", {}, "sha512-3SOzvaAg8rkHrXtRjpCvCvbyO5to9oOO27Z/XqHEYXfMRVSw/qMIVdmaOk9W2lcRLtR6dlqTjo9hDeJk70QBYQ=="],
"safe-array-concat": ["safe-array-concat@1.1.4", "", { "dependencies": { "call-bind": "^1.0.9", "call-bound": "^1.0.4", "get-intrinsic": "^1.3.0", "has-symbols": "^1.1.0", "isarray": "^2.0.5" } }, "sha512-wtZlHyOje6OZTGqAoaDKxFkgRtkF9CnHAVnCHKfuj200wAgL+bSJhdsCD2l0Qx/2ekEXjPWcyKkfGb5CPboslg=="],
@@ -2802,8 +2819,6 @@
"supports-preserve-symlinks-flag": ["supports-preserve-symlinks-flag@1.0.0", "", {}, "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w=="],
"synckit": ["synckit@0.11.13", "", { "dependencies": { "@pkgr/core": "^0.3.6" } }, "sha512-eNRKgb3z66Yp3D2CixVujOUvXLFUTij/zVnV8KRyvFdQwpz7I5DS8UfRkTeLzb64u+dkzDSdelE24izu+zSSUg=="],
"tabbable": ["tabbable@6.5.0", "", {}, "sha512-wieBHXygIm7OyQOu5hQlkk62/WyCFYGlWg7L6/ZCUZwx0o398Zkn4pVmMyfYhfMG8kGrj/Krt8eIk6UKC6VzwA=="],
"tailwind-merge": ["tailwind-merge@3.6.0", "", {}, "sha512-uxL7qAVQriqRQPAyK3pj66VqskWqoZ37PW94jwOTwNfq/z9oyu1V+eqrZqtR2+fCiXdYOZe/Modt8GtvqNzu+w=="],
@@ -3036,6 +3051,8 @@
"@grpc/grpc-js/@grpc/proto-loader": ["@grpc/proto-loader@0.8.1", "", { "dependencies": { "lodash.camelcase": "^4.3.0", "long": "^5.0.0", "protobufjs": "^7.5.5", "yargs": "^17.7.2" }, "bin": { "proto-loader-gen-types": "build/bin/proto-loader-gen-types.js" } }, "sha512-wtF6h+DY6M3YaDBPAmvuuA6jV8Sif9MjtOI5euKFWRgCDl5PeDpPsHR9u2l6St5ceY8AZgoNDww5+HvEsXFsGg=="],
"@inlang/paraglide-js/commander": ["commander@11.1.0", "", {}, "sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ=="],
"@inlang/sdk/@sinclair/typebox": ["@sinclair/typebox@0.31.30", "", {}, "sha512-MGsM7bVmHg3sUKCphlu3SGQ+T+5JTbygZWYArfKQCW5anJeACHeqLhcnf+R7XlCWZ+QR3C8JTBx3kCKJTN69ow=="],
"@isaacs/cliui/string-width": ["string-width@5.1.2", "", { "dependencies": { "eastasianwidth": "^0.2.0", "emoji-regex": "^9.2.2", "strip-ansi": "^7.0.1" } }, "sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA=="],
@@ -3204,8 +3221,6 @@
"bcrypt-pbkdf/tweetnacl": ["tweetnacl@0.14.5", "", {}, "sha512-KXXFFdAbFXY4geFIwoyNK+f5Z1b7swfXABfL7HXCmoIWMKU3dmS26672A4EeQtDzLKy7SXmfBu51JolvEKwtGA=="],
"better-call/@better-auth/utils": ["@better-auth/utils@0.4.2", "", { "dependencies": { "@noble/hashes": "^2.0.1" } }, "sha512-AUxrvu+HaaODsUyzDxFgwd/8RZ1yZaYo42LXKSrU2oGgR38pS1ij8nqQKNgtTWoYGpNevNXtCfgTy6loHveW9A=="],
"better-call/@better-fetch/fetch": ["@better-fetch/fetch@1.3.1", "", {}, "sha512-ABkD1WhyfPZprKRQI3bhATjeiFuNWC9PXhfGWqL+sg/gKrM977oFrYkdb4msM3hgUGonr7KlOsOFT5TU2rht9g=="],
"bl/buffer": ["buffer@5.7.1", "", { "dependencies": { "base64-js": "^1.3.1", "ieee754": "^1.1.13" } }, "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ=="],
@@ -15,7 +15,7 @@ services:
retries: 5
embedding-server:
image: shumaione/embedding-server:latest
image: crpi-000nkafzu6xvpwsc.cn-hongkong.personal.cr.aliyuncs.com/shumai-one/embedding-server:latest
container_name: shumai_embedding
restart: unless-stopped
@@ -49,6 +49,7 @@ services:
# Better Auth
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
BETTER_AUTH_URL: http://localhost:3000
# Sandbox Configuration
ENABLE_WEAKER_NESTED_SANDBOX: 'true'
@@ -15,17 +15,17 @@ services:
retries: 5
embedding-server:
image: shumaione/embedding-server:latest
image: crpi-000nkafzu6xvpwsc.cn-hongkong.personal.cr.aliyuncs.com/shumai-one/embedding-server:latest
container_name: shumai_embedding
restart: unless-stopped
turntable-renderer:
image: shumaione/turntable-renderer:latest
image: crpi-000nkafzu6xvpwsc.cn-hongkong.personal.cr.aliyuncs.com/shumai-one/turntable-renderer:latest
container_name: shumai_turntable
restart: unless-stopped
gotenberg:
image: gotenberg/gotenberg:8
image: docker.m.daocloud.io/gotenberg/gotenberg:8
container_name: shumai_gotenberg
restart: unless-stopped
@@ -59,6 +59,7 @@ services:
# Better Auth
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
BETTER_AUTH_URL: http://localhost:3000
# Sandbox Configuration
ENABLE_WEAKER_NESTED_SANDBOX: 'true'
@@ -44,6 +44,7 @@ services:
# Better Auth
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
BETTER_AUTH_URL: http://localhost:3000
# Sandbox Configuration
ENABLE_WEAKER_NESTED_SANDBOX: 'true'
@@ -49,6 +49,7 @@ services:
# Better Auth
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
BETTER_AUTH_URL: http://localhost:3000
# Sandbox Configuration
ENABLE_WEAKER_NESTED_SANDBOX: 'true'
@@ -59,6 +59,7 @@ services:
# Better Auth
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
BETTER_AUTH_URL: http://localhost:3000
# Sandbox Configuration
ENABLE_WEAKER_NESTED_SANDBOX: 'true'
+1
View File
@@ -44,6 +44,7 @@ services:
# Better Auth
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
BETTER_AUTH_URL: http://localhost:3000
# Sandbox Configuration
ENABLE_WEAKER_NESTED_SANDBOX: 'true'
+31
View File
@@ -3,6 +3,37 @@ title: "Changelog"
description: "Recent updates, new features, and improvements in Shumai"
---
<Update label="v0.6.2" description="October 29, 2026">
### Added
* **mcp**: Add a built-in MCP server supporting OAuth 2.1 and API tokens, allowing external AI agents (such as Claude Code, Cursor, or Kiro) to browse projects, manage and upload files, inspect media contents (images, video frames, PDF pages, 3D models, and text), search assets, manage review comments, update metadata, and organize version stacks
* **cli**: Add `shumai-cli` commands to download files (`download`), view and render media contents (`read`, `render`), search and copy files (`search`, `copy`), manage review comments (`comment`), view and update metadata (`field ls`, `meta set`), organize version stacks (`version`), and view folder instructions (`instructions`)
* **agent**: The built-in agent can now list accessible projects, search files by name, meaning, or metadata, read and post review comments, update metadata, copy files, stack and unstack versions, and read folder instructions (`AGENTS.md`), with configurable permissions per agent
### Fixed
### Changed
</Update>
<Update label="v0.6.1" description="October 24, 2026">
### Added
* **dashboard**: Add Workflow Tasks section to the Team Dashboard allowing team owners to monitor background workflow jobs, inspect execution payloads and outputs, and stop in-flight tasks
### Fixed
* **metadata**: Fix an issue where clicking to edit a metadata field on a card or list row inadvertently selected the file, and prevent expanded text overlays or date pickers from being clipped by container borders
### Changed
* **deps**: Upgrade Bun to 1.4.4
</Update>
<Update label="v0.6.0" description="October 20, 2026">
### Added
+5 -5
View File
@@ -26,11 +26,11 @@ Shumai is configured via environment variables. You can define these variables i
## Authentication & Application Config
| Variable | Description | Example / Default |
| :------------------- | :--------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------- |
| `BETTER_AUTH_SECRET` | A secure, random 32-byte secret used to sign session cookies and JWTs. | Generate with `openssl rand -base64 32` |
| `BETTER_AUTH_URL` | The public URL of your Hono backend API. Used for redirect flows and notification email links fallback. | `http://localhost:3000` (respects `SHUMAI_SERVER_PORT` fallback) |
| `APP_URL` | The public URL of the web application. Used for notification email deep links (falls back to `BETTER_AUTH_URL`). | `https://shumai.example.com` |
| Variable | Description | Example / Default |
| :------------------- | :------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------- |
| `BETTER_AUTH_SECRET` | A secure, random 32-byte secret used to sign session cookies and JWTs. | Generate with `openssl rand -base64 32` |
| `BETTER_AUTH_URL` | The public URL of your Shumai server. Used for MCP OAuth 2.1 discovery, redirect flows, password reset links, and email notifications. | `http://localhost:3000` (respects `SHUMAI_SERVER_PORT` fallback) |
| `APP_URL` | The public URL of the web application. Used for notification email deep links (falls back to `BETTER_AUTH_URL`). | `https://shumai.example.com` |
---
+222
View File
@@ -0,0 +1,222 @@
---
title: Shumai MCP Server
description: Let external AI agents such as Claude Code, Cursor, or Kiro work with your Shumai projects.
---
Shumai includes a built-in [Model Context Protocol](https://modelcontextprotocol.io) (MCP) server. Any MCP-compatible agent can connect to it and browse projects, look at images, videos, and documents, create folders, and organize assets on your behalf, using the same tools as Shumai's built-in agent.
<Note>
This page is about connecting **external agents to Shumai**. To connect Shumai's own agents to
other tools, see [MCP Servers](/using-shumai/mcp-servers).
</Note>
---
## Connecting a Client
The MCP server uses the Streamable HTTP transport at:
```
https://<your-shumai-host>/mcp
```
Shumai supports **OAuth 2.1** (recommended for seamless browser-based authentication) and **API tokens** (for headless environments or clients that do not support OAuth).
Pick your client below to get started:
### Claude Code
[Claude Code](https://docs.anthropic.com/en/docs/claude-code) natively supports remote MCP servers with OAuth authentication. Add Shumai to your Claude Code configuration by running:
```bash
claude mcp add shumai --transport http https://<your-shumai-host>/mcp
```
When you start a new conversation, Claude Code will open a browser window for you to sign in and authorize access to your Shumai account. No manual API token is required.
Alternatively, you can manually configure `.claude/settings.json`:
```json
{
"mcpServers": {
"shumai": {
"type": "http",
"url": "https://<your-shumai-host>/mcp"
}
}
}
```
Or connect stdio via `mcp-remote`:
```bash
claude mcp add shumai -- npx -y mcp-remote https://<your-shumai-host>/mcp
```
### Cursor
Add Shumai to your Cursor MCP configuration (`~/.cursor/mcp.json` or project `.cursor/mcp.json`):
```json
{
"mcpServers": {
"shumai": {
"url": "https://<your-shumai-host>/mcp"
}
}
}
```
When Cursor connects to the server, it will prompt you in the browser to complete the OAuth sign-in and authorize access.
To authenticate with an API token instead, pass the `Authorization` header:
```json
{
"mcpServers": {
"shumai": {
"url": "https://<your-shumai-host>/mcp",
"headers": {
"Authorization": "Bearer <your-api-token>"
}
}
}
}
```
### Codex
```bash
codex mcp add shumai --url https://<your-shumai-host>/mcp
codex mcp login shumai
```
### VS Code
```bash
code --add-mcp '{"name":"shumai","type":"http","url":"https://<your-shumai-host>/mcp"}'
```
Start the server from your **MCP Servers** list in VS Code and sign in to Shumai when prompted.
### Other Clients
**Remote (HTTP)**
If your client natively supports remote HTTP MCP servers:
```json
{
"mcpServers": {
"shumai": {
"url": "https://<your-shumai-host>/mcp"
}
}
}
```
**Local Bridge (`mcp-remote` / stdio)**
For clients that only support local (stdio) MCP servers, use `mcp-remote` to connect to Shumai's remote HTTP endpoint:
```json
{
"mcpServers": {
"shumai": {
"command": "npx",
"args": ["-y", "mcp-remote", "https://<your-shumai-host>/mcp"]
}
}
}
```
---
## Authentication
Shumai's MCP server supports two authentication methods:
### OAuth 2.1 (Recommended)
Shumai implements OAuth 2.1 with Dynamic Client Registration ([RFC 7591](https://datatracker.ietf.org/doc/html/rfc7591)) and OAuth Protected Resource Metadata ([RFC 9728](https://datatracker.ietf.org/doc/html/rfc9728) / [RFC 8414](https://datatracker.ietf.org/doc/html/rfc8414)).
- **Seamless Sign-in**: Supported MCP clients automatically discover authorization endpoints and complete the OAuth handshake in your browser.
- **Access Control**: External agents act with your permissions in Shumai. Actions are attributed to your user account and logged in the team audit log with `via: mcp`.
- **Clearing OAuth Cache**: If you need to re-authenticate or clear stored credentials in CLI tools such as `mcp-remote` or Claude Code, remove the local authentication cache:
```bash
rm -rf ~/.mcp-auth
```
### API Tokens
API tokens are ideal for automated CI/CD pipelines, headless environments, or clients that do not support interactive OAuth.
1. In Shumai, open **Team Settings** → **API Tokens**.
2. Enter a name (for example, `ci-agent` or `cursor-token`) and click **Generate**.
3. Copy the token from the dialog. It is only shown once.
4. Pass the token in the `Authorization: Bearer <your-api-token>` header (or `x-api-key` header).
<Warning>
An API token acts as you: the agent can do anything your account can do in Shumai. Store it like a
password, use a separate token per client, and revoke tokens you no longer use from **Team
Settings** → **API Tokens**.
</Warning>
---
## Available Tools
| Tool | What it does |
| ------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| `list_projects` | Lists the projects you can access, with each project's root folder. |
| `list_assets` | Lists files and folders in a folder, page by page. |
| `search_assets` | Searches a folder and its subfolders by name, by meaning (when AI search is enabled), or by metadata. |
| `create_folder` | Creates a folder. |
| `rename_asset` | Renames a file or folder. |
| `move_assets` | Moves files or folders into another folder. |
| `copy_assets` | Copies files or folders into another folder of the same team. |
| `delete_asset` | Moves a file or folder to the project trash. Marked as destructive so clients can ask you to confirm. |
| `read_asset` | Looks at a file: an image, video frames, PDF pages, a 3D model view, or the text of a text file. |
| `get_asset_render` | Gets the result of a `read_asset` call that was still rendering. |
| `download_asset` | Gets download links for up to 50 files: the web preview version by default, or the original with `proxy: false`. |
| `get_folder_instructions` | Reads a folder's instructions for AI agents (its AGENTS.md). |
| `create_file` | Starts uploading a new file into a folder (see [Uploading Files](#uploading-files)). |
| `create_version` | Starts uploading a new version of a file. |
| `complete_upload` | Finishes an upload started with `create_file` or `create_version`. |
| `list_comments` | Lists the review comments on a file, with their replies. |
| `create_comment` | Comments on a file (optionally at a timecode) or replies to a comment. |
| `resolve_comment` | Marks a comment as resolved, or unresolved again. |
| `list_metadata_fields` | Lists a project's metadata fields, such as status, rating, and custom fields, with their choices. |
| `update_asset_metadata` | Sets metadata values on a file. |
| `create_version_stack` | Stacks files in the same folder as versions of one asset. |
| `list_versions` | Lists the versions in a version stack, latest first. |
| `remove_version` | Takes a version out of its stack. The file is kept. |
### Reading Files
`read_asset` attaches the images to the result so the AI model can see them: up to 8 images per call, each up to 2 MB. Any other images are listed as links only. Every result also includes a link to each image, valid for about an hour. Pass `inlineImages: false` to get links only, for example when your client can't show images.
Rendering video frames or PDF pages can take a while. If it takes longer than about 20 seconds, `read_asset` returns `status: "pending"` with a `taskId`, and the model calls `get_asset_render` with that `taskId` to get the result. Only the user who started the read can get its result.
### Uploading Files
Uploading takes three steps, so large files never pass through the AI model:
1. `create_file` (or `create_version`) returns an `uploadUrl` and an `uploadId`.
2. The agent uploads the file's bytes to `uploadUrl` with an HTTP `PUT`, for example `curl -T video.mp4 "<uploadUrl>"`.
3. `complete_upload` with the `uploadId` creates the file and starts processing it (previews, transcoding).
The upload URL and ID are valid for about an hour, and only you can complete your upload. If the project has **Creation Context** metadata fields (for example, the prompt or model used to make a file), pass their values as `metadata` to `create_file` or `create_version`; `list_metadata_fields` shows which fields they are.
The agent must be able to reach the upload URL. With Shumai's local storage, it points to your Shumai server; with S3 storage, to your S3 endpoint.
Every call runs with your permissions: for example, a **Reviewer** can list assets but cannot create or rename them. Actions are recorded in the team audit log and attributed to you, marked as coming from MCP.
---
## Troubleshooting
- **OAuth Authorization Expired or Invalid**: Clear your client's local OAuth session cache (`rm -rf ~/.mcp-auth`) and restart your MCP client to re-authenticate.
- **401 Unauthorized**: The API token or OAuth access token is missing, expired, mistyped, or revoked. For API tokens, generate a new token under **Team Settings** → **API Tokens** and update your client configuration.
- **Permission errors on a tool call**: Your role in that team or project does not allow the action.
+4
View File
@@ -71,6 +71,10 @@
"using-shumai/password-reset"
]
},
{
"group": "Developer",
"pages": ["developer/mcp-server"]
},
{
"group": "Configuration",
"pages": ["configuration/env-variables", "configuration/hardware-transcoding"]
+102 -19
View File
@@ -9,23 +9,84 @@ We support three installation options. For most administrators, **Docker Compose
## Option 1: Docker Compose (Recommended)
Docker Compose automatically configures and spins up all required services (the web application, a database, and local storage).
Docker Compose is the fastest way to get Shumai running. You do not need to clone the repository or install packages manually. Ensure you have [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/) installed, then follow these steps:
1. **Download the Configuration**:
1. **Create Directory**:
Create and navigate to a new directory for your configuration and data volumes:
```bash
curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose.yaml
mkdir shumai && cd shumai
```
> [!TIP]
> **Hardware Acceleration:** By default, the standard Compose stack uses CPU-based software transcoding. If your server has a dedicated or integrated GPU (NVIDIA, Intel, AMD, or Rockchip), see the [Hardware Transcoding Guide](/configuration/hardware-transcoding) to enable GPU acceleration via the companion `hwaccel.transcoding.yaml`.
2. **Download the Configuration**:
Download your preferred `docker-compose.yaml` configuration based on your needs and available RAM:
- **Lite (Suggested RAM: 2 GB+)**: Minimal deployment containing Shumai and PostgreSQL database.
```bash
curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose.yaml
```
- **With Embedding Server (Suggested RAM: 4 GB+)**: Adds local multimodal semantic search powered by `shumaiOne/embedding-server` (`EmbeddingGemma 2 740M`).
```bash
curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose.embedding.yaml
```
- **Full (Suggested RAM: 8 GB+)**: All-in-one setup including `embedding-server`, `turntable-renderer` (3D previews), and `Gotenberg` (document PDF proxying).
```bash
curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose.full.yaml
```
> [!NOTE]
> By default, the Docker Compose setups use CPU software transcoding. If your server has a GPU (NVIDIA NVENC, Intel QuickSync, AMD/Intel VA-API, or Rockchip RKMPP) and you want to enable GPU hardware acceleration, check the [Hardware Transcoding Guide](/configuration/hardware-transcoding).
3. **Configure Environment Variables**:
Open `docker-compose.yaml` to adjust environment variables for your deployment:
- `BETTER_AUTH_URL`: The public base URL of your Shumai server (e.g., `https://shumai.example.com` or `http://12.34.56.78:3000`). This is required for remote servers and production deployments.
- `AWS_ENDPOINT_URL_S3`: By default, `AWS_ENDPOINT_URL_S3` is set to `http://localhost:{SHUMAI_SERVER_PORT}`. When you use local storage (the default storage backend with `STORAGE_BACKEND: local`) and expose Shumai on a remote server, custom domain, or custom host/port combination, set `AWS_ENDPOINT_URL_S3` to the external URL that client browsers will use to upload and download files.
For example, if you change the port mapping in `docker-compose.yaml` from `3000:3000` to `12345:3000` and deploy on a server with IP address `12.34.56.78`, set:
```yaml
AWS_ENDPOINT_URL_S3: http://12.34.56.78:12345
```
This value must be reachable from client browsers and should include the externally exposed port.
- `SHUMAI_SERVER_PORT`: Controls the port the Shumai server listens on. The default is `3000`.
- **External S3 Storage**: By default, Shumai uses the bundled local storage service in this Docker Compose setup. To use an external S3-compatible storage instead, configure the environment variables for your S3 provider. Here are examples for AWS S3 and Cloudflare R2:
**AWS S3 Example:**
```yaml
STORAGE_BACKEND: s3
S3_BUCKET: your-bucket-name
S3_ACCESS_KEY_ID: your-access-key-id
S3_SECRET_ACCESS_KEY: your-secret-access-key
AWS_ENDPOINT_URL_S3: https://s3.us-east-1.amazonaws.com
```
**Cloudflare R2 Example:**
```yaml
STORAGE_BACKEND: s3
S3_BUCKET: your-bucket-name
S3_REGION: auto
S3_ACCESS_KEY_ID: your-access-key-id
S3_SECRET_ACCESS_KEY: your-secret-access-key
AWS_ENDPOINT_URL_S3: https://<account-id>.r2.cloudflarestorage.com
```
4. **Start the Services**:
2. **Start the Services**:
```bash
docker compose up -d
```
3. **Access the Dashboard**:
Open your browser and navigate to **`http://localhost:3000`** to access the Shumai Web UI.
5. **Access the Dashboard**:
Open your browser and navigate to **`http://localhost:3000`** (or `http://<your-server-ip>:3000` for remote deployments) to access the Shumai Web UI.
### Production Deployments (Distributed Workers)
@@ -79,12 +140,13 @@ To handle media processing and security isolation, the host machine must have th
```env
DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public
BETTER_AUTH_SECRET=your-random-32-character-secret-key
BETTER_AUTH_URL=http://localhost:3000
STORAGE_BACKEND=local
AWS_ENDPOINT_URL_S3=http://localhost:3000
```
> [!TIP]
> `AWS_ENDPOINT_URL_S3` must include the scheme and port (e.g. `http://localhost:3000`) where Shumai is publicly accessible. This URL is used directly by client browsers for file uploads.
> `BETTER_AUTH_URL` should be set to your public server address for remote or production deployments. `AWS_ENDPOINT_URL_S3` must include the scheme and port (e.g. `http://localhost:3000` or `http://12.34.56.78:3000`) where Shumai is publicly accessible, as client browsers use this URL directly for file uploads.
4. **Start Shumai**:
@@ -101,25 +163,46 @@ To handle media processing and security isolation, the host machine must have th
## Option 3: Run from Source (For Developers)
If you wish to run Shumai locally to customize the platform or contribute:
To set up Shumai locally for development:
1. **Clone the Repository**:
1. **Clone the Repository and Install Dependencies**:
```bash
git clone https://github.com/shumaiOne/shumai.git
cd shumai
```
2. **Install Dependencies**:
```bash
bun install
```
3. **Start a Database**:
Launch a local database container (as described in the NPM section above).
4. **Run Migrations**:
Set up your `.env` file at the root, then apply database migrations:
2. **Start a Database**:
Launch a local database container (as described in the NPM section above):
```bash
docker run --name shumai_postgres \
-e POSTGRES_USER=shumai \
-e POSTGRES_PASSWORD=shumai_password \
-e POSTGRES_DB=shumai_db \
-p 5432:5432 \
-d pgvector/pgvector:pg18
```
3. **Create Environment File**:
Create a `.env` file at the root of the workspace:
```env
DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public
BETTER_AUTH_SECRET=ySxs7DxzHDZBbeeHNPEwBuspYwipBqz5Gk5XdBjNhWw=
BETTER_AUTH_URL=http://localhost:3000
STORAGE_BACKEND=local
SHUMAI_SERVER_PORT=3000
AWS_ENDPOINT_URL_S3=http://localhost:3000
```
4. **Generate i18n Translations**:
```bash
bun run i18n:compile
```
> [!TIP]
> During frontend development, you can run `bun run i18n:watch` in a separate terminal to automatically recompile translation files whenever you edit strings in `packages/webui/messages/`.
5. **Run Migrations**:
Apply database schema migrations:
```bash
bun run db:migrate
```
5. **Start the Dev Server**:
6. **Start the Dev Server**:
```bash
bun run dev
```
+78 -12
View File
@@ -34,59 +34,125 @@ The following tools can be individually toggled in the agent configuration dialo
- **Description:** Allows the agent to inspect images, extract still frames from video files, render PDF document pages, and read text/markdown documents.
- **Why it matters:** Enables multimodal agents to view visual media assets uploaded to Shumai, extract frame-by-frame video screenshots, inspect document layouts, and answer questions about uploaded creative assets.
- **Limits:** Up to 20 video frames or PDF pages per call. If rendering takes longer than about a minute, the tool tells the agent the render is still in progress instead of failing.
### 4. Browse Shumai (`list_assets`)
### 4. Get Asset Render (`get_asset_render`)
- **Description:** Allows the agent to list folders and browse files in Shumai.
- **Description:** Allows the agent to pick up video frames or document pages that were still rendering when `read_asset` returned.
- **Why it matters:** Long videos and large documents can take a while to render. With this tool the agent can come back for the result instead of starting over. It only returns renders that `read_asset` started for the same user.
### 5. Browse Shumai (`list_assets`)
- **Description:** Allows the agent to list folders and browse files in Shumai. Large folders are returned page by page.
- **Why it matters:** Gives the agent visual mapping of the project structure so it can search for other files or locate related assets.
### 5. Create Folder (`create_folder`)
### 6. List Projects (`list_projects`)
- **Description:** Allows the agent to list the projects you have access to across your teams, including each project's root folder.
- **Why it matters:** Lets the agent find the right project and its root folder on its own before browsing or creating files.
### 7. Create Folder (`create_folder`)
- **Description:** Allows the agent to create new folders in Shumai.
- **Why it matters:** Needed if the agent organizes outputs or places files in custom subdirectory folders.
### 6. Create File (`create_file`)
### 8. Create File (`create_file`)
- **Description:** Allows the agent to create and upload new files to Shumai.
- **Why it matters:** Allows the agent to generate new assets (e.g. text logs, transcoded clips, reports) and save them directly back into your project.
### 7. Create Version (`create_version`)
### 9. Create Version (`create_version`)
- **Description:** Allows the agent to upload and stack a new version of an existing file in Shumai.
- **Why it matters:** Allows the agent to update files in a clean, non-destructive way by stacking revisions rather than creating duplicate files.
### 8. Generate Image (`generate_image`)
### 10. Generate Image (`generate_image`)
- **Description:** Allows the agent to generate still images from text prompts and reference images using configured AI image models.
- **Why it matters:** Enables agents to create concept art, mockups, textures, and illustrations directly inside Shumai.
- **Prerequisite:** This tool requires at least one image model to be configured and enabled in [Image / Video Generation](/using-shumai/image-video-generation). If no image models are enabled under a configured provider, this tool is omitted from the agent session even if enabled in the agent settings dialog.
### 9. Generate Video (`generate_video`)
### 11. Generate Video (`generate_video`)
- **Description:** Allows the agent to generate video clips from text prompts, image animation, or transition keyframes using configured AI video models.
- **Why it matters:** Enables agents to produce short animated clips, camera motion shots, and video transitions.
- **Prerequisite:** This tool requires at least one video model to be configured and enabled in [Image / Video Generation](/using-shumai/image-video-generation). If no video models are enabled under a configured provider, this tool is omitted from the agent session even if enabled in the agent settings dialog.
### 10. Download Asset (`download_asset`)
### 12. Download Asset (`download_asset`)
- **Description:** Allows the agent to download a workspace asset or specific storage S3 key to the local `.pi` directory for processing with local scripts or bash commands.
- **Description:** Allows the agent to download up to 50 workspace files, or a specific storage S3 key (such as a frame from `read_asset`), to the local `.pi` directory for processing with local scripts or bash commands. By default it downloads the web preview version when there is one (for example the transcoded video or the web image); the agent can ask for the original uploaded file instead. Folders are not supported.
- **Why it matters:** Enables agents executing custom skill scripts to fetch files or intermediate transcode artifacts to local disk.
### 11. Rename Asset (`rename_asset`)
### 13. Rename Asset (`rename_asset`)
- **Description:** Allows the agent to rename an existing file or folder in Shumai.
- **Why it matters:** Enables agents to keep project files neatly named and organized according to workspace naming conventions. Enabled by default.
### 12. Move Assets (`move_assets`)
### 14. Move Assets (`move_assets`)
- **Description:** Allows the agent to move one or more assets to a destination parent folder.
- **Why it matters:** Enables agents to reorganize workspace structures, relocate generated outputs, or tidy up project folders. Enabled by default.
### 13. Delete Asset (`delete_asset`)
### 15. Delete Asset (`delete_asset`)
- **Description:** Allows the agent to delete a single file or folder by moving it to the Trash bin.
- **Why it matters:** Provides agents with the ability to clean up outdated or redundant assets. To prevent accidental data loss, this tool is **disabled by default** and strictly limited to deleting one asset at a time. Team Owners must explicitly enable this tool for an agent.
### 16. Search Assets (`search_assets`)
- **Description:** Allows the agent to search a folder and its subfolders by name, by meaning (when AI search is enabled for the team), or by metadata such as status.
- **Why it matters:** Lets the agent find the files a request is about without browsing folder by folder.
### 17. Copy Assets (`copy_assets`)
- **Description:** Allows the agent to copy files or folders, with everything inside them, into another folder of the same team.
- **Why it matters:** Lets the agent reuse assets across projects or set up working copies while keeping the originals unchanged.
### 18. Read Folder Instructions (`get_folder_instructions`)
- **Description:** Allows the agent to read a folder's instructions for AI agents (its AGENTS.md).
- **Why it matters:** Lets the agent follow the team's conventions, such as naming rules or deliverable specs, before adding files to a folder.
### 19. Read Comments (`list_comments`)
- **Description:** Allows the agent to read the review comments and replies on a file, including their timecodes.
- **Why it matters:** Lets the agent act on review feedback, for example to summarize open notes or address them in a new version.
### 20. Post Comment (`create_comment`)
- **Description:** Allows the agent to comment on a file, optionally at a timecode, or reply to a comment. Comments are posted as the user the agent works for; the audit log records that the agent wrote them.
- **Why it matters:** Lets the agent take part in reviews like a teammate. Mentions in the agent's comments don't start other agents.
### 21. Resolve Comment (`resolve_comment`)
- **Description:** Allows the agent to mark comments as resolved, or reopen them.
- **Why it matters:** Lets the agent close the feedback it has addressed.
### 22. List Metadata Fields (`list_metadata_fields`)
- **Description:** Allows the agent to see a project's metadata fields, such as status, rating, and custom fields, with their choices.
- **Why it matters:** Tells the agent which values it can search by or set.
### 23. Update Metadata (`update_asset_metadata`)
- **Description:** Allows the agent to set metadata values on a file, such as its status or tags. Read-only fields can't be changed.
- **Why it matters:** Lets the agent keep a file's review status and other fields up to date.
### 24. Create Version Stack (`create_version_stack`)
- **Description:** Allows the agent to stack existing files in the same folder as versions of one asset.
- **Why it matters:** Lets the agent tidy up loose revisions (for example `cut_v1.mp4` and `cut_v2.mp4`) into one versioned asset.
### 25. List Versions (`list_versions`)
- **Description:** Allows the agent to list the versions in a version stack, latest first.
- **Why it matters:** Lets the agent compare a file with earlier versions.
### 26. Remove Version (`remove_version`)
- **Description:** Allows the agent to take a version out of its stack. The file isn't deleted; it goes back into the folder as a separate file.
- **Why it matters:** Lets the agent fix a version that was stacked by mistake.
---
## Restricting Tools
+20
View File
@@ -92,6 +92,26 @@ export default tseslint.config(
'@typescript-eslint/naming-convention': baseNamingConvention,
},
},
{
// Operations hold policy and orchestration only; data access belongs in core services.
files: ['packages/core/src/operations/**/*.ts'],
ignores: ['**/*.test.ts'],
rules: {
'@typescript-eslint/no-restricted-imports': [
'error',
{
paths: [
{
name: '@shumai/db',
importNames: ['prisma'],
message: 'Operations must call core services instead of the Prisma client.',
allowTypeImports: true,
},
],
},
],
},
},
{
files: ['**/*.test.ts', '**/*.test.tsx'],
rules: {
+4 -5
View File
@@ -1,6 +1,6 @@
{
"name": "shumai",
"version": "0.6.0",
"version": "0.6.2",
"private": true,
"type": "module",
"repository": {
@@ -24,7 +24,7 @@
"test:e2e:workflow": "bun --bun vitest run -c packages/e2e/vitest.config.ts",
"test:e2e:install": "playwright install --with-deps",
"harness:dev": "bun run packages/webui/e2e/harness/serve.ts",
"format": "prettier --write .",
"format": "bun fmt",
"db:migrate": "bun run prisma migrate dev",
"db:deploy": "bun run prisma migrate deploy",
"db:generate": "cd packages/db && bunx prisma generate",
@@ -50,14 +50,13 @@
"@testcontainers/postgresql": "12.1.0",
"@testing-library/dom": "10.4.1",
"@testing-library/react": "16.3.3",
"@types/bun": "1.4.0",
"@types/bun": "1.4.3",
"@types/node": "25.9.1",
"bun-plugin-pino": "1.5.0",
"bun-plugin-tailwind": "0.1.2",
"bun-types": "1.4.0",
"bun-types": "1.4.3",
"eslint": "10.9.1",
"eslint-config-prettier": "10.1.8",
"eslint-plugin-prettier": "5.5.6",
"eslint-plugin-react": "7.37.5",
"globals": "17.11.0",
"happy-dom": "20.12.0",
+2 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/agent",
"version": "0.6.0",
"version": "0.6.2",
"private": true,
"type": "module",
"main": "./src/index.ts",
@@ -13,6 +13,7 @@
"@shumai/core": "workspace:*",
"@shumai/db": "workspace:*",
"@shumai/dtos": "workspace:*",
"@shumai/operations": "workspace:*",
"@shumai/workflow-core": "workspace:*",
"adm-zip": "0.6.0",
"jittered-fractional-indexing": "1.0.1",
+1 -7
View File
@@ -859,13 +859,7 @@ export const initializeAgentSessionActivity = defineStep(
name: att.asset.name,
type: att.asset.type,
mediaType: attProxyType as
| 'image'
| 'video'
| 'pdf'
| 'audio'
| '3d'
| 'other'
| undefined,
'image' | 'video' | 'pdf' | 'audio' | '3d' | 'other' | undefined,
mimeType: att.asset.mediaType || undefined,
})
}
@@ -1,8 +1,8 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { resolveAnnotationsById } from './annotation-resolver'
import { DatabaseSessionStorage } from '../database-session-storage'
import { resolveAnnotationsById } from '@shumai/core/src/asset-read/annotations'
import { DatabaseSessionStorage } from './database-session-storage'
import type { SessionTreeEntry } from '@earendil-works/pi-agent-core'
import { degreeToSecond } from '@shumai/core/src/utils/model3d'
@@ -7,7 +7,7 @@ import { DatabaseSessionStorage } from './database-session-storage'
import { agentService } from '@shumai/core/src/agent/agent'
import { createAgentSession, type DbProviderInfo } from './index'
import * as sandboxedBashModule from './tools/sandboxed-bash'
import * as readAssetModule from './tools/read-asset'
import * as operationToolsModule from './operation-tools'
describe('DatabaseSessionStorage', () => {
setupTestDbHooks()
@@ -450,8 +450,8 @@ describe('DatabaseSessionStorage', () => {
})
const sessionId = initialStorage.sessionId
// 2. Spy on media tool constructor
const createReadAssetToolSpy = vi.spyOn(readAssetModule, 'createReadAssetTool')
// 2. Spy on the Operation tools builder (read_asset comes from the Operation registry)
const buildOperationToolsSpy = vi.spyOn(operationToolsModule, 'buildOperationTools')
// 3. Resume the session via createAgentSession with a NEW comment ID
await createAgentSession({
@@ -468,8 +468,11 @@ describe('DatabaseSessionStorage', () => {
providers: mockProviders,
})
// 4. Assert that the tool was instantiated using user ID
expect(createReadAssetToolSpy).toHaveBeenCalledWith(user.id)
// 4. Assert that the tools were built for the user
expect(buildOperationToolsSpy).toHaveBeenCalledWith(
expect.objectContaining({ userId: user.id }),
expect.objectContaining({}),
)
})
it('should instantiate media tools with user ID in subsequent turns (video)', async () => {
@@ -502,8 +505,8 @@ describe('DatabaseSessionStorage', () => {
})
const sessionId = initialStorage.sessionId
// 2. Spy on media tool constructor
const createReadAssetToolSpy = vi.spyOn(readAssetModule, 'createReadAssetTool')
// 2. Spy on the Operation tools builder (read_asset comes from the Operation registry)
const buildOperationToolsSpy = vi.spyOn(operationToolsModule, 'buildOperationTools')
// 3. Resume the session via createAgentSession with a NEW comment ID
await createAgentSession({
@@ -520,8 +523,11 @@ describe('DatabaseSessionStorage', () => {
providers: mockProviders,
})
// 4. Assert that the tool was instantiated using user ID
expect(createReadAssetToolSpy).toHaveBeenCalledWith(user.id)
// 4. Assert that the tools were built for the user
expect(buildOperationToolsSpy).toHaveBeenCalledWith(
expect.objectContaining({ userId: user.id }),
expect.objectContaining({}),
)
})
describe('P1 Bug Reproductions', () => {
+12 -28
View File
@@ -18,20 +18,13 @@ import * as fs from 'fs'
import * as path from 'path'
import { DatabaseSessionStorage } from './database-session-storage'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { createCreateFileTool } from './tools/create-file'
import { createCreateFolderTool } from './tools/create-folder'
import { createCreateVersionTool } from './tools/create-version'
import { createDownloadAssetTool } from './tools/download-asset'
import { createListAssetsTool } from './tools/list-assets'
import { createReadAssetTool } from './tools/read-asset'
import { createReadSkillTool } from './tools/read-skill'
import { createReadThreadTool } from './tools/read-thread'
import { createSandboxedBashTool } from './tools/sandboxed-bash'
import { createGenerateImageTool } from './tools/generate-image'
import { createGenerateVideoTool } from './tools/generate-video'
import { createRenameAssetTool } from './tools/rename-asset'
import { createMoveAssetsTool } from './tools/move-assets'
import { createDeleteAssetTool } from './tools/delete-asset'
import { buildOperationTools } from './operation-tools'
import { createAutofillReadAssetTool } from './operation-extensions'
import { mediaGenerationService } from '@shumai/core/src/media-generation/media-generation'
import { serializeContextToXml } from './context/serialize-context'
import { DEFAULT_DENIED_TOOLS, type ShumaiMessageContext } from '@shumai/dtos'
@@ -271,13 +264,12 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
const deniedTools = agentConfig?.deniedTools ?? [...DEFAULT_DENIED_TOOLS]
const mediaTools: AgentTool[] = []
if (userId) {
mediaTools.push(createReadAssetTool(userId))
} else if (agent?.type === 'autofill' && assetId) {
// Agents acting for a user get read_asset / get_asset_render from the Operation registry
// (see systemTools below); the autofill system agent has its own team-scoped read_asset.
if (!userId && agent?.type === 'autofill' && assetId) {
mediaTools.push(
createReadAssetTool({
createAutofillReadAssetTool({
teamId,
agentType: 'autofill',
targetAssetId: assetId,
}),
)
@@ -338,21 +330,15 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
const systemTools: AgentTool[] = []
if (userId) {
const metadataSchema = await buildProjectMetadataSchema(projectId)
const agentContext = { teamId, agentId }
const creationMetadataSchema = await buildProjectMetadataSchema(projectId)
systemTools.push(
createListAssetsTool(userId),
createCreateFolderTool(userId, agentContext),
createCreateFileTool(userId, metadataSchema, agentContext),
createCreateVersionTool(userId, metadataSchema, agentContext),
createDownloadAssetTool(userId),
createRenameAssetTool(userId, agentContext),
createMoveAssetsTool(userId, agentContext),
createDeleteAssetTool(userId, agentContext),
// Shared Operations (also exposed via MCP and the CLI): browsing, searching, organizing,
// reading and downloading assets, creating files and versions, comments and metadata
...buildOperationTools({ userId, agentId, teamId, projectId }, { creationMetadataSchema }),
)
}
const readThread = createReadThreadTool()
const readThread = createReadThreadTool({ userId, teamId })
const rawTools = [
...mediaTools,
readSkill,
@@ -635,6 +621,4 @@ export * from './workflows/agent-chat'
export * from './workflows/agent-embedding'
export * from './tools/generate-image'
export * from './tools/generate-video'
export * from './tools/rename-asset'
export * from './tools/move-assets'
export * from './tools/delete-asset'
export * from './operation-tools'
@@ -0,0 +1,294 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import * as fs from 'fs'
import * as path from 'path'
import { prisma, type Asset, type StorageKey } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { authzService } from '@shumai/core/src/authz/authz'
import { getOperation } from '@shumai/core/src/operations'
import { toAgentTool } from '../operation-tools'
import './download-asset'
vi.mock('@shumai/db', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shumai/db')>()
return {
...actual,
prisma: {
asset: { findUnique: vi.fn(), findFirst: vi.fn() },
storageKey: { findFirst: vi.fn(), findUnique: vi.fn() },
},
}
})
vi.mock('@shumai/core/src/s3/s3', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shumai/core/src/s3/s3')>()
return {
...actual,
s3Service: {
headObject: vi.fn(),
downloadToFile: vi.fn(),
presign: vi.fn().mockResolvedValue('https://signed.example/file'),
},
}
})
vi.mock('@shumai/core/src/authz/authz', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shumai/core/src/authz/authz')>()
return { ...actual, authzService: { hasPermission: vi.fn() } }
})
type Head = Awaited<ReturnType<typeof s3Service.headObject>>
type Saved = { filePath: string; absolutePath: string; contentType: string; size: number }
const piDir = path.join(process.cwd(), '.pi')
function downloadTool(userId = 'user-1') {
return toAgentTool(getOperation('download_asset')!, { userId })
}
function textOf(result: { content: unknown[] }): string {
return (result.content[0] as { text: string }).text
}
function mockAssets(...assets: Array<Record<string, unknown>>) {
vi.mocked(prisma.asset.findUnique).mockImplementation(
(async (args: { where: { id: string } }) =>
assets.find((a) => a.id === args.where.id) ?? null) as never,
)
}
function mockStorage(contentType: string, write = 'bytes') {
vi.mocked(s3Service.headObject).mockResolvedValue({ size: 1234, contentType } as Head)
vi.mocked(s3Service.downloadToFile).mockImplementation(async (_bucket, _key, dest) => {
await fs.promises.writeFile(dest, write)
})
}
const photo = {
id: 'img-1',
name: 'sample_photo.jpg',
type: 'file',
isDeleted: false,
storageKey: { key: 'files/up-1/sample_photo.jpg' },
media: { proxyType: 'image', imageTranscodes: [{ key: 'files/img-1/proxy.webp' }] },
}
describe('download_asset agent tool', () => {
const created: string[] = []
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(authzService.hasPermission).mockResolvedValue()
})
afterEach(() => {
for (const file of created.splice(0)) fs.rmSync(file, { force: true })
})
it('offers assetIds, proxy, and the agent-only key parameter', () => {
const tool = downloadTool()
const schema = tool.parameters as { properties: Record<string, unknown>; required?: string[] }
expect(Object.keys(schema.properties)).toEqual(['assetIds', 'proxy', 'key'])
expect(schema.required ?? []).not.toContain('assetIds')
expect(tool.description).toContain('.pi directory')
})
it('saves the web preview into .pi by default, named after the asset', async () => {
mockAssets(photo)
mockStorage('image/webp', 'proxy-bytes')
const result = await downloadTool().execute('call-1', { assetIds: ['img-1'] })
const expectedPath = path.join(piDir, 'img-1_sample_photo.webp')
created.push(expectedPath)
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'img-1',
})
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
'shumai',
'files/img-1/proxy.webp',
expectedPath,
)
expect(fs.readFileSync(expectedPath, 'utf-8')).toBe('proxy-bytes')
expect(textOf(result)).toBe(
[
`Downloaded "sample_photo.webp" (ID: img-1, proxy, image/webp, 11 B) to "${path.join('.pi', 'img-1_sample_photo.webp')}".`,
'Remember to delete these temporary files when finished.',
].join('\n'),
)
const details = result.details as { files: Saved[] }
expect(details.files[0]).toMatchObject({
filePath: path.join('.pi', 'img-1_sample_photo.webp'),
contentType: 'image/webp',
size: 11,
})
})
it('saves the original with proxy false', async () => {
mockAssets(photo)
mockStorage('image/jpeg')
await downloadTool().execute('call-1', { assetIds: ['img-1'], proxy: false })
const expectedPath = path.join(piDir, 'img-1_sample_photo.jpg')
created.push(expectedPath)
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
'shumai',
'files/up-1/sample_photo.jpg',
expectedPath,
)
})
it('downloads several files and the latest version of a version stack', async () => {
mockAssets(photo, { id: 'stack-1', name: 'banana', type: 'version_stack', isDeleted: false })
vi.mocked(prisma.asset.findFirst).mockResolvedValue({
id: 'file-v2',
name: 'banana_pig.png',
type: 'file',
isDeleted: false,
storageKey: { key: 'files/up-2/banana_pig.png' },
media: { proxyType: 'image', imageTranscodes: [{ key: 'files/file-v2/proxy.webp' }] },
} as unknown as Asset)
mockStorage('image/webp')
const result = await downloadTool().execute('call-1', {
assetIds: ['img-1', 'stack-1'],
key: null,
})
created.push(
path.join(piDir, 'img-1_sample_photo.webp'),
path.join(piDir, 'file-v2_banana_pig.webp'),
)
expect(prisma.asset.findFirst).toHaveBeenCalledWith({
where: { parentId: 'stack-1', isDeleted: false },
orderBy: { sortIndex: 'asc' },
include: { storageKey: true },
})
expect(s3Service.downloadToFile).toHaveBeenCalledTimes(2)
expect(textOf(result)).toContain('Downloaded "banana_pig.webp" (ID: file-v2, proxy')
})
it('rejects folders', async () => {
mockAssets({ id: 'folder-1', name: 'Renders', type: 'folder', isDeleted: false })
await expect(downloadTool().execute('call-1', { assetIds: ['folder-1'] })).rejects.toThrow(
'Asset folder-1 ("Renders") is a folder. Download the files inside it instead.',
)
expect(s3Service.downloadToFile).not.toHaveBeenCalled()
})
it('checks Read on every asset before downloading anything', async () => {
mockAssets(photo)
vi.mocked(authzService.hasPermission).mockImplementation(async ({ id }) => {
if (id === 'secret') throw new Error('Forbidden')
})
await expect(
downloadTool().execute('call-1', { assetIds: ['img-1', 'secret'] }),
).rejects.toThrow('Forbidden')
expect(s3Service.downloadToFile).not.toHaveBeenCalled()
})
it('requires assetIds or key', async () => {
await expect(downloadTool().execute('call-1', { assetIds: null, key: null })).rejects.toThrow(
'Invalid input for download_asset',
)
})
it('rejects assetIds and key together', async () => {
await expect(
downloadTool().execute('call-1', { assetIds: ['img-1'], key: 'files/img-1/a.webp' }),
).rejects.toThrow('Provide either "assetIds" or "key", not both.')
})
describe('by storage key', () => {
it('requires a user', async () => {
await expect(
downloadTool('').execute('call-1', { key: 'files/ast-1/shot.webp' }),
).rejects.toThrow('User ID is required for authorization.')
})
it('rejects keys outside files/ and path traversal', async () => {
await expect(downloadTool().execute('call-1', { key: 'avatars/user-1.png' })).rejects.toThrow(
'Invalid storage key format: key must start with "files/"',
)
await expect(
downloadTool().execute('call-1', { key: 'files/../../etc/passwd' }),
).rejects.toThrow('cannot contain ".."')
})
it('rejects keys that belong to no asset', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findFirst).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findUnique).mockResolvedValue(null)
await expect(
downloadTool().execute('call-1', { key: 'files/nope/shot.webp' }),
).rejects.toThrow('does not belong to any valid asset or the asset has been deleted')
})
it('checks Read on the owning asset', async () => {
mockAssets({ id: 'other-project-asset' })
vi.mocked(authzService.hasPermission).mockRejectedValue(new Error('Forbidden'))
await expect(
downloadTool().execute('call-1', { key: 'files/other-project-asset/shots/a.webp' }),
).rejects.toThrow('Forbidden')
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'other-project-asset',
})
expect(s3Service.downloadToFile).not.toHaveBeenCalled()
})
it('downloads a read_asset artifact into .pi', async () => {
mockAssets({ id: 'ast-123' })
mockStorage('image/webp', 'frame-bytes')
const result = await downloadTool().execute('call-1', {
key: 'files/ast-123/screenshots/shot_5.0s.webp',
})
const expectedPath = path.join(piDir, 'shot_5.0s.webp')
created.push(expectedPath)
expect(fs.readFileSync(expectedPath, 'utf-8')).toBe('frame-bytes')
expect(textOf(result)).toBe(
`Downloaded storage key "files/ast-123/screenshots/shot_5.0s.webp" to "${path.join('.pi', 'shot_5.0s.webp')}". Remember to delete this temporary file when finished.`,
)
expect(result.details).toMatchObject({
key: 'files/ast-123/screenshots/shot_5.0s.webp',
assetId: 'ast-123',
size: Buffer.from('frame-bytes').length,
})
})
it('resolves original upload keys through the storage key', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findFirst).mockResolvedValue({
id: 'sk-1',
key: 'files/upload-ulid-456/photo.png',
assets: [{ id: 'asset-photo-1' }],
} as unknown as StorageKey & { assets: Asset[] })
const pngHeader = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
vi.mocked(s3Service.headObject).mockResolvedValue({
size: 8,
contentType: 'application/octet-stream',
} as Head)
vi.mocked(s3Service.downloadToFile).mockImplementation(async (_b, _k, dest) => {
await fs.promises.writeFile(dest, pngHeader)
})
const result = await downloadTool().execute('call-1', {
key: 'files/upload-ulid-456/photo.png',
})
created.push(path.join(piDir, 'photo.png'))
expect(authzService.hasPermission).toHaveBeenCalledWith(
expect.objectContaining({ id: 'asset-photo-1' }),
)
// Falls back to detecting the type from the bytes
expect(result.details).toMatchObject({ assetId: 'asset-photo-1', contentType: 'image/png' })
})
})
})
@@ -0,0 +1,124 @@
import * as fs from 'fs'
import * as path from 'path'
import { z } from 'zod'
import type { AgentToolResult } from '@earendil-works/pi-agent-core'
import { downloadAsset, MAX_DOWNLOAD_ASSETS, type DownloadAssetOutput } from '@shumai/operations'
import type { User } from '@shumai/db'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { storageBucket } from '@shumai/core/src/asset-read'
import { s3Service } from '@shumai/core/src/s3/s3'
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
import { readFileMimeType } from '@shumai/core/src/utils/file-mime'
import { resolveAssetIdFromKey } from '../tools/download-asset'
import { registerAgentToolExtension, type AgentToolExtension } from '../operation-tools'
const CLEANUP_REMINDER = 'Remember to delete these temporary files when finished.'
/** The agent's sandbox, where downloaded files are saved for local scripts and bash. */
function sandboxDir(): string {
const dir = path.join(process.cwd(), '.pi')
fs.mkdirSync(dir, { recursive: true })
return dir
}
function formatSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
}
/** Saves a storage object into the sandbox; returns the paths and detected content type. */
async function saveToSandbox(key: string, filename: string) {
const dir = sandboxDir()
const absolutePath = path.join(dir, filename)
const bucket = storageBucket()
const info = await s3Service.headObject(bucket, key).catch(() => null)
await s3Service.downloadToFile(bucket, key, absolutePath)
const size = await fs.promises
.stat(absolutePath)
.then((s) => s.size)
.catch(() => info?.size ?? 0)
const contentType =
info?.contentType && info.contentType !== 'application/octet-stream'
? info.contentType
: readFileMimeType(absolutePath)
return { absolutePath, filePath: path.join('.pi', filename), size, contentType }
}
/** Downloads one storage key (e.g. a frame from read_asset) after checking Read on its asset. */
async function downloadKey(key: string, userId: string): Promise<AgentToolResult<unknown>> {
const owningAssetId = await resolveAssetIdFromKey(key)
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: owningAssetId,
})
const filename = sanitizeFilename(path.basename(key)) || 'downloaded_file'
const saved = await saveToSandbox(key, filename)
return {
content: [
{
type: 'text',
text: `Downloaded storage key "${key}" to "${saved.filePath}". Remember to delete this temporary file when finished.`,
},
],
details: { key, assetId: owningAssetId, ...saved },
}
}
/** Saves each file of a download_asset result into the sandbox. */
export async function presentDownloadOutput(
output: DownloadAssetOutput,
): Promise<AgentToolResult<unknown>> {
const lines: string[] = []
const files = []
for (const file of output.files) {
if (!file.key) throw new Error(`download_asset returned no storage key for ${file.assetId}`)
const saved = await saveToSandbox(
file.key,
`${file.assetId}_${sanitizeFilename(file.name) || 'file'}`,
)
lines.push(
`Downloaded "${file.name}" (ID: ${file.assetId}, ${file.kind}, ${saved.contentType}, ${formatSize(saved.size)}) to "${saved.filePath}".`,
)
files.push({ assetId: file.assetId, name: file.name, kind: file.kind, ...saved })
}
lines.push(CLEANUP_REMINDER)
return { content: [{ type: 'text', text: lines.join('\n') }], details: { files } }
}
const downloadAssetExtension: AgentToolExtension = {
description:
`Downloads up to ${MAX_DOWNLOAD_ASSETS} workspace files into the local .pi directory, for processing with local scripts or bash commands. ` +
'By default it saves the web preview version (proxy) when there is one, e.g. the transcoded video or the web image; set proxy to false for the original uploaded file. ' +
'A version stack downloads its latest version; folders are not supported. ' +
'To download a specific artifact from read_asset (e.g. a frame or page), pass its storage key as "key" instead of assetIds. ' +
'Remember to delete the downloaded files when finished.',
parameterOverrides: z.object({
assetIds: downloadAsset.input.shape.assetIds
.optional()
.describe(
`IDs of the files or version stacks to download (1 to ${MAX_DOWNLOAD_ASSETS}). Omit when passing "key".`,
),
}),
extraParameters: z.object({
key: z
.string()
.optional()
.describe(
'Storage S3 key of a specific media artifact to download instead of assetIds, e.g. a screenshot or page key from read_asset.',
),
}),
async intercept(input, extra, ctx) {
if (typeof extra.key !== 'string') return undefined
if (Array.isArray(input.assetIds) && input.assetIds.length > 0) {
throw new Error('Provide either "assetIds" or "key", not both.')
}
if (!ctx.userId) throw new Error('User ID is required for authorization.')
return downloadKey(extra.key, ctx.userId)
},
present: (output) => presentDownloadOutput(output as DownloadAssetOutput),
}
registerAgentToolExtension('download_asset', downloadAssetExtension)
@@ -0,0 +1,4 @@
// Registers agent-specific presentation for Operations. Import once before building tools.
export { createAutofillReadAssetTool, presentReadOutput } from './read-asset'
export { presentDownloadOutput } from './download-asset'
import './upload'
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,155 @@
import { z } from 'zod'
import { type AgentTool, type AgentToolResult } from '@earendil-works/pi-agent-core'
import { type ImageContent, type TextContent } from '@earendil-works/pi-ai'
import { readAsset, type AssetReadOutput } from '@shumai/operations'
import { assetReadService, toAssetReadOutput } from '@shumai/core/src/asset-read'
import { getOperation, READ_WAIT_BUDGET_MS } from '@shumai/core/src/operations'
import { s3Service } from '@shumai/core/src/s3/s3'
import { getFileMimeType } from '@shumai/core/src/utils/file-mime'
import { MAX_TEXT_BYTES, MAX_TEXT_LINES } from '@shumai/core/src/asset-read'
import {
registerAgentToolExtension,
splitAgentParams,
toToolParameters,
type AgentToolExtension,
} from '../operation-tools'
const extraParameters = z.object({
s3KeyOnly: z
.boolean()
.optional()
.describe(
'When true, returns only the storage S3 key(s) and MIME types of the asset/frames without attaching base64 image or text content to the agent context. Use this to obtain S3 keys for generate_image or generate_video references without consuming context tokens.',
),
})
/** Details kept compatible with the previous tool; `sourceKeys` drives session image storage. */
interface ReadAssetDetails {
assetId: string
name: string
mediaType: string | null
key?: string
keys: string[]
downloadUrl?: string
results: Array<{ key: string; label: string; downloadUrl?: string }>
sourceKeys: string[]
size?: number
isTruncated?: boolean
}
/** Render a read_asset / get_asset_render output as agent content (text plus image blocks). */
export async function presentReadOutput(
output: AssetReadOutput,
opts: { s3KeyOnly: boolean },
): Promise<AgentToolResult<ReadAssetDetails | { taskId?: string; status: 'pending' }>> {
if (output.status === 'pending') {
const retry = output.retryAfterSeconds ?? 5
return {
content: [
{
type: 'text',
text: `${output.summary} Call get_asset_render with taskId "${output.taskId}" in about ${retry} seconds to get the result.`,
},
],
details: { taskId: output.taskId, status: 'pending' },
}
}
const bucket = process.env.S3_BUCKET || 'shumai'
const imageBlocks: ImageContent[] = []
const imageLines: string[] = []
for (const image of output.images) {
const key = image.key ?? ''
let mimeType = image.mimeType
if (!opts.s3KeyOnly && key) {
const { buffer, contentType } = await s3Service.getObject(bucket, key)
mimeType =
contentType && contentType !== 'application/octet-stream'
? contentType
: getFileMimeType(buffer, key, image.mimeType)
imageBlocks.push({ type: 'image', data: buffer.toString('base64'), mimeType })
}
imageLines.push(`- ${image.label} (S3 Key: "${key}", MIME: ${mimeType})`)
}
const lines = [output.summary, ...output.details, ...imageLines]
if (output.text) {
lines.push(`- Source file (S3 Key: "${output.text.key ?? ''}", MIME: ${output.text.mimeType})`)
}
let text = lines.join('\n')
if (output.text && !opts.s3KeyOnly) {
text += `\n\n${output.text.content}`
if (output.text.truncated) {
text += `\n\n[Content truncated to ${MAX_TEXT_LINES} lines / ${MAX_TEXT_BYTES / 1024}KB limit. Use download_asset to process the full file.]`
}
}
const imageKeys = output.images.map((i) => i.key ?? '')
const content: Array<TextContent | ImageContent> = [{ type: 'text', text }, ...imageBlocks]
return {
content,
details: {
assetId: output.asset.id,
name: output.asset.name,
mediaType: output.asset.mediaType,
key: imageKeys[0] ?? output.text?.key,
keys: imageKeys,
downloadUrl: output.images[0]?.url ?? output.text?.url,
results: output.images.map((i) => ({ key: i.key ?? '', label: i.label, downloadUrl: i.url })),
sourceKeys: output.text?.key && imageKeys.length === 0 ? [output.text.key] : imageKeys,
...(output.text ? { size: output.text.size, isTruncated: output.text.truncated } : {}),
},
}
}
const readAssetExtension: AgentToolExtension = {
extraParameters,
// s3KeyOnly also means "don't read text content", as before.
toInput: (input, extra) => (extra.s3KeyOnly ? { ...input, includeContent: false } : input),
present: (output, extra) =>
presentReadOutput(output as AssetReadOutput, { s3KeyOnly: extra.s3KeyOnly === true }),
}
const getAssetRenderExtension: AgentToolExtension = {
present: (output) => presentReadOutput(output as AssetReadOutput, { s3KeyOnly: false }),
}
registerAgentToolExtension('read_asset', readAssetExtension)
registerAgentToolExtension('get_asset_render', getAssetRenderExtension)
/** Longer than the chat budget: autofill can't poll get_asset_render (it has no user). */
const AUTOFILL_WAIT_BUDGET_MS = 2 * READ_WAIT_BUDGET_MS.agent
/**
* `read_asset` for the autofill system agent, which runs without a user. It uses the same
* pipeline and presentation, but access is confined to the agent's team and target asset.
*/
export function createAutofillReadAssetTool(access: {
teamId?: string
targetAssetId?: string
}): AgentTool {
const op = getOperation(readAsset.name)!
return {
name: readAsset.name,
label: readAsset.title,
description: readAsset.description,
parameters: toToolParameters(op, readAssetExtension),
execute: async (_toolCallId, params) => {
const { input, extra } = splitAgentParams(op, readAssetExtension, params)
const parsed = readAsset.input.safeParse(input)
if (!parsed.success) {
throw new Error(`Invalid input for read_asset: ${z.prettifyError(parsed.error)}`)
}
const { maxWaitSeconds, ...request } = parsed.data
const budget =
maxWaitSeconds === undefined
? AUTOFILL_WAIT_BUDGET_MS
: Math.min(AUTOFILL_WAIT_BUDGET_MS, maxWaitSeconds * 1000)
const outcome = await assetReadService.read({ kind: 'autofill', ...access }, request, {
waitBudgetMs: budget,
})
const output = await toAssetReadOutput(outcome, { includeKeys: true })
return presentReadOutput(output, { s3KeyOnly: extra.s3KeyOnly === true })
},
}
}
@@ -0,0 +1,447 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { Value } from 'typebox/value'
import { validateToolArguments } from '@earendil-works/pi-ai'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import { AuditAction, type AssetInfo } from '@shumai/dtos'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { getOperation } from '@shumai/core/src/operations'
import { s3Service } from '@shumai/core/src/s3/s3'
import { fieldsToTypeBoxSchema } from '../index'
import { toAgentTool, type AgentToolOptions } from '../operation-tools'
import './upload'
const ctx = { userId: 'user-1', agentId: 'agent-1', teamId: 'team-1', projectId: 'proj-1' }
const bucket = process.env.S3_BUCKET || 'shumai'
function tool(name: 'create_file' | 'create_version', options: AgentToolOptions = {}) {
return toAgentTool(getOperation(name)!, ctx, options)
}
function assetInfo(overrides: Partial<AssetInfo>): AssetInfo {
return {
id: 'file-1',
name: 'test',
sizeByte: 100,
fileCount: 0,
type: 'file',
status: 'uploaded',
createdAt: '2026-01-01T00:00:00.000Z',
updatedAt: '2026-01-01T00:00:00.000Z',
projectId: 'proj-1',
...overrides,
}
}
function outputOf(result: { content: unknown[] }) {
return JSON.parse((result.content[0] as { text: string }).text) as unknown
}
const promptField = {
field: {
id: 'f1',
key: 'prompt',
scope: 'PROJECT',
readOnly: false,
description: '',
config: { name: 'Prompt', type: 'text', autofillSource: 'CREATION_CONTEXT' },
},
visible: true,
}
describe('create_file and create_version agent tools', () => {
const tempFiles: string[] = []
const tempFile = (content: string, filename: string): string => {
const filePath = path.join(os.tmpdir(), filename)
fs.writeFileSync(filePath, content)
tempFiles.push(filePath)
return filePath
}
beforeEach(() => {
vi.spyOn(authzService, 'hasPermission').mockResolvedValue(undefined)
vi.spyOn(auditLogService, 'logAction').mockResolvedValue(null)
vi.spyOn(assetService, 'getAssetContext').mockResolvedValue({
teamId: 'team-1',
projectId: 'proj-1',
})
vi.spyOn(assetService, 'getAssetMeta').mockImplementation(async (id) => ({
id,
name: id,
type: id.startsWith('folder') ? 'folder' : 'file',
parentId: 'root',
teamId: 'team-1',
projectId: 'proj-1',
}))
vi.spyOn(assetService, 'findAssetByStorageKey').mockResolvedValue(null)
vi.spyOn(assetService, 'createFile').mockResolvedValue(assetInfo({}))
vi.spyOn(assetService, 'createVersion').mockResolvedValue(assetInfo({ name: 'v2' }))
vi.spyOn(metadataService, 'listProjectFields').mockResolvedValue([promptField] as never)
vi.spyOn(s3Service, 'presign').mockResolvedValue('https://storage.example/upload')
// Read file streams to the end, as real storage would.
vi.spyOn(s3Service, 'putObject').mockImplementation(async (_bucket, _key, body) => {
if (body && typeof body === 'object' && Symbol.asyncIterator in body) {
for await (const chunk of body as AsyncIterable<unknown>) void chunk
}
})
vi.spyOn(s3Service, 'headObject').mockImplementation(async (_bucket, key) => ({
key,
size: 17,
lastModified: new Date(),
contentType: 'text/markdown',
eTag: '',
}))
})
afterEach(() => {
vi.restoreAllMocks()
for (const f of tempFiles.splice(0)) fs.rmSync(f, { force: true })
})
describe('create_file', () => {
it('uploads a local file to storage and creates the file as the agent', async () => {
const filePath = tempFile('# Hello from disk', 'agent-create-file.md')
const result = await tool('create_file').execute('call-1', {
parent: 'folder-1',
path: filePath,
} as never)
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: Permission.Edit,
type: ResourceType.Asset,
id: 'folder-1',
})
const [putBucket, key, , size, contentType] = vi.mocked(s3Service.putObject).mock.calls[0]
expect(putBucket).toBe(bucket)
expect(key).toMatch(/^files\/.+\/agent-create-file\.md$/)
expect(size).toBe(Buffer.byteLength('# Hello from disk'))
expect(contentType).toBe('text/markdown')
expect(assetService.createFile).toHaveBeenCalledWith({
parentId: 'folder-1',
name: 'agent-create-file.md',
key,
sizeByte: 17,
contentType: 'text/markdown',
creatorId: 'user-1',
agentId: 'agent-1',
metadata: undefined,
})
expect(auditLogService.logAction).toHaveBeenCalledWith(
expect.objectContaining({
action: AuditAction.file_create,
teamId: 'team-1',
projectId: 'proj-1',
userId: 'user-1',
agentId: 'agent-1',
itemId: 'file-1',
via: 'agent',
}),
)
expect(result.details).toEqual({})
expect(outputOf(result)).toEqual({
id: 'file-1',
name: 'test',
type: 'file',
size: 100,
projectId: 'proj-1',
})
})
it('creates a text file from name and content', async () => {
await tool('create_file').execute('call-1', {
parent: 'folder-1',
name: 'notes.md',
content: '# Notes\n\nHello',
} as never)
const [, key, body, size, contentType] = vi.mocked(s3Service.putObject).mock.calls[0]
expect(key).toMatch(/^files\/.+\/notes\.md$/)
expect((body as Buffer).toString('utf-8')).toBe('# Notes\n\nHello')
expect(size).toBe(Buffer.byteLength('# Notes\n\nHello'))
expect(contentType).toBe('text/markdown')
expect(assetService.createFile).toHaveBeenCalledWith(
expect.objectContaining({ name: 'notes.md', contentType: 'text/markdown' }),
)
})
it('defaults unknown extensions to text/plain when creating from content', async () => {
await tool('create_file').execute('call-1', {
parent: 'folder-1',
name: 'script.ts',
content: 'const x = 1',
} as never)
expect(vi.mocked(s3Service.putObject).mock.calls[0][4]).toBe('text/plain')
expect(assetService.createFile).toHaveBeenCalledWith(
expect.objectContaining({ contentType: 'text/plain' }),
)
})
it('sanitizes the name', async () => {
await tool('create_file').execute('call-1', {
parent: 'folder-1',
name: 'my/notes.md',
content: 'hello',
} as never)
expect(vi.mocked(s3Service.putObject).mock.calls[0][1]).toContain('my_notes.md')
expect(assetService.createFile).toHaveBeenCalledWith(
expect.objectContaining({ name: 'my_notes.md' }),
)
})
it('forwards Creation Context metadata, including new select options', async () => {
const filePath = tempFile('# Hello', 'agent-create-file-meta.md')
await tool('create_file').execute('call-1', {
parent: 'folder-1',
path: filePath,
metadata: { prompt: { newOption: { value: 'Kling' } } },
} as never)
expect(assetService.createFile).toHaveBeenCalledWith(
expect.objectContaining({ metadata: { prompt: { newOption: { value: 'Kling' } } } }),
)
})
it('treats null metadata as omitted', async () => {
const filePath = tempFile('# Hello', 'agent-create-file-null.md')
await tool('create_file').execute('call-1', {
parent: 'folder-1',
path: filePath,
metadata: null,
} as never)
expect(vi.mocked(assetService.createFile).mock.calls[0][0].metadata).toBeUndefined()
})
it('rejects metadata keys that are not Creation Context fields before uploading', async () => {
const filePath = tempFile('# Hello', 'agent-create-file-bad-meta.md')
await expect(
tool('create_file').execute('call-1', {
parent: 'folder-1',
path: filePath,
metadata: { status: 'approved' },
} as never),
).rejects.toThrow(/not allowed: status/)
expect(s3Service.putObject).not.toHaveBeenCalled()
expect(assetService.createFile).not.toHaveBeenCalled()
})
it('rejects a call with neither or both of "path" and "content"', async () => {
await expect(
tool('create_file').execute('call-1', { parent: 'folder-1' } as never),
).rejects.toThrow('Provide exactly one of "path" (a local file) or "content" (with "name").')
await expect(
tool('create_file').execute('call-1', {
parent: 'folder-1',
path: '/some/file.md',
name: 'notes.md',
content: 'hello',
} as never),
).rejects.toThrow('Provide exactly one of')
expect(s3Service.putObject).not.toHaveBeenCalled()
})
it('requires a name with "content"', async () => {
await expect(
tool('create_file').execute('call-1', { parent: 'folder-1', content: 'x' } as never),
).rejects.toThrow('"name" is required')
})
it('fails when the local file does not exist, without touching storage', async () => {
await expect(
tool('create_file').execute('call-1', {
parent: 'folder-1',
path: '/nonexistent/file.md',
} as never),
).rejects.toThrow('Local file not found at path: /nonexistent/file.md')
expect(s3Service.putObject).not.toHaveBeenCalled()
expect(assetService.createFile).not.toHaveBeenCalled()
})
it('does not write to storage when the user lacks Edit permission', async () => {
vi.mocked(authzService.hasPermission).mockRejectedValue(
new Error('User is not a member of the team'),
)
const filePath = tempFile('# Not allowed', 'agent-create-file-denied.md')
await expect(
tool('create_file').execute('call-1', { parent: 'folder-1', path: filePath } as never),
).rejects.toThrow('User is not a member of the team')
await expect(
tool('create_file').execute('call-1', {
parent: 'folder-1',
name: 'notes.md',
content: 'x',
} as never),
).rejects.toThrow('User is not a member of the team')
expect(s3Service.putObject).not.toHaveBeenCalled()
expect(s3Service.presign).not.toHaveBeenCalled()
expect(assetService.createFile).not.toHaveBeenCalled()
})
it('passes harness validation with only one source set', async () => {
const t = tool('create_file')
const validated = validateToolArguments(
{ name: t.name, description: '', parameters: t.parameters } as Parameters<
typeof validateToolArguments
>[0],
{
type: 'toolCall',
id: 'call-1',
name: t.name,
arguments: { parent: 'folder-1', name: 'random.txt', content: 'hello' },
} as Parameters<typeof validateToolArguments>[1],
)
await t.execute('call-1', validated as never)
expect(assetService.createFile).toHaveBeenCalledWith(
expect.objectContaining({ name: 'random.txt' }),
)
})
it('shows the agent its own parameters', () => {
const properties = Object.keys(
(tool('create_file').parameters as unknown as { properties: object }).properties,
)
expect(properties.sort()).toEqual(['content', 'name', 'parent', 'path'])
})
})
describe('create_version', () => {
it('uploads a local file and creates a version as the agent', async () => {
const filePath = tempFile('# Hello from disk', 'agent-create-version.md')
const result = await tool('create_version').execute('call-1', {
parent: 'file-1',
path: filePath,
} as never)
const key = vi.mocked(s3Service.putObject).mock.calls[0][1]
expect(assetService.createVersion).toHaveBeenCalledWith({
parentId: 'file-1',
name: 'agent-create-version.md',
key,
sizeByte: 17,
contentType: 'text/markdown',
creatorId: 'user-1',
agentId: 'agent-1',
metadata: undefined,
})
expect(outputOf(result)).toMatchObject({ id: 'file-1', name: 'v2' })
expect(auditLogService.logAction).toHaveBeenCalledWith(
expect.objectContaining({ action: AuditAction.file_create, agentId: 'agent-1' }),
)
})
it('forwards Creation Context metadata', async () => {
const filePath = tempFile('# Hello', 'agent-create-version-meta.md')
await tool('create_version').execute('call-1', {
parent: 'file-1',
path: filePath,
metadata: { prompt: 'Generated using gemini' },
} as never)
expect(assetService.createVersion).toHaveBeenCalledWith(
expect.objectContaining({ metadata: { prompt: 'Generated using gemini' } }),
)
})
it('does not upload when the user lacks Edit permission', async () => {
vi.mocked(authzService.hasPermission).mockRejectedValueOnce(
new Error('User is not a member of the team'),
)
const filePath = tempFile('# Not allowed', 'agent-create-version-denied.md')
await expect(
tool('create_version').execute('call-1', { parent: 'file-1', path: filePath } as never),
).rejects.toThrow('User is not a member of the team')
expect(s3Service.putObject).not.toHaveBeenCalled()
expect(assetService.createVersion).not.toHaveBeenCalled()
})
it('fails when the local file does not exist', async () => {
await expect(
tool('create_version').execute('call-1', {
parent: 'file-1',
path: '/nonexistent/file.md',
} as never),
).rejects.toThrow('Local file not found at path: /nonexistent/file.md')
expect(s3Service.putObject).not.toHaveBeenCalled()
})
it('shows the agent its own parameters', () => {
const properties = Object.keys(
(tool('create_version').parameters as unknown as { properties: object }).properties,
)
expect(properties.sort()).toEqual(['parent', 'path'])
})
})
describe('Creation Context metadata parameter', () => {
const creationMetadataSchema = fieldsToTypeBoxSchema([
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
{
id: 'source',
config: {
name: 'Source',
type: 'select',
select: { options: [{ id: 'gemini', displayName: 'Gemini', color: '#ffffff' }] },
},
},
])
it.each(['create_file', 'create_version'] as const)(
'%s exposes the project schema as a strict, nullable metadata parameter',
(name) => {
const parameters = tool(name, { creationMetadataSchema }).parameters as unknown as {
properties: { metadata: { anyOf: [{ required: string[] }, { type: string }] } }
}
const meta = parameters.properties.metadata
expect(meta.anyOf[0].required).toEqual(['prompt', 'source'])
expect(meta.anyOf[1].type).toBe('null')
expect(Value.Check(meta, { prompt: 'hello', source: 'gemini' })).toBe(true)
expect(Value.Check(meta, { prompt: null, source: null })).toBe(true)
expect(Value.Check(meta, null)).toBe(true)
expect(Value.Check(meta, { prompt: 'hello' })).toBe(false)
expect(Value.Check(meta, { prompt: 'x', source: 'gemini', other: 1 })).toBe(false)
expect(Value.Check(meta, { prompt: 'x', source: 'not-an-option' })).toBe(false)
},
)
it('keeps null values inside metadata through harness validation', () => {
const t = tool('create_file', {
creationMetadataSchema: fieldsToTypeBoxSchema([
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
]),
})
const validated = validateToolArguments(
{ name: t.name, description: '', parameters: t.parameters } as Parameters<
typeof validateToolArguments
>[0],
{
type: 'toolCall',
id: 'call-1',
name: t.name,
arguments: { parent: 'folder-1', name: 'a.md', content: 'x', metadata: { prompt: null } },
} as Parameters<typeof validateToolArguments>[1],
) as { metadata?: unknown }
expect(validated.metadata).toEqual({ prompt: null })
})
})
})
@@ -0,0 +1,158 @@
import * as fs from 'fs'
import * as path from 'path'
import { z } from 'zod'
import { Type } from 'typebox'
import type { AgentToolResult } from '@earendil-works/pi-agent-core'
import { createFile, type StartedUploadOutput } from '@shumai/operations'
import { storageBucket } from '@shumai/core/src/asset-read'
import { completeUploadOp, runOperation } from '@shumai/core/src/operations'
import { s3Service } from '@shumai/core/src/s3/s3'
import { getFileMimeType, readFileMimeType } from '@shumai/core/src/utils/file-mime'
import {
registerAgentToolExtension,
type AgentOperationContext,
type AgentToolExtension,
type AgentToolOptions,
type ToolParametersSchema,
} from '../operation-tools'
const METADATA_HINT =
'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.'
/**
* The agent gets the conversation project's Creation Context fields as a strict schema
* (so it knows exactly what to fill in) instead of the open record MCP clients see. Without
* such fields the parameter is left out.
*/
function setCreationMetadata(schema: ToolParametersSchema, options: AgentToolOptions): void {
if (options.creationMetadataSchema) {
schema.properties.metadata = Type.Union([options.creationMetadataSchema, Type.Null()])
} else {
delete schema.properties.metadata
}
}
function resolveLocalFile(localPath: string): string {
const absolutePath = path.resolve(process.cwd(), localPath)
if (!fs.existsSync(absolutePath) || !fs.statSync(absolutePath).isFile()) {
throw new Error(`Local file not found at path: ${localPath}`)
}
return absolutePath
}
/**
* Writes the file straight to the upload's storage key (the agent runs next to storage, so
* it doesn't need the upload URL), then completes the upload as the agent.
*/
async function uploadAndComplete(
raw: unknown,
extra: Record<string, unknown>,
ctx: AgentOperationContext,
): Promise<AgentToolResult<unknown>> {
const started = raw as StartedUploadOutput
if (!started.key) throw new Error('The upload did not return a storage key')
if (typeof extra.path === 'string') {
const absolutePath = resolveLocalFile(extra.path)
await s3Service.putObject(
storageBucket(),
started.key,
fs.createReadStream(absolutePath),
fs.statSync(absolutePath).size,
readFileMimeType(absolutePath),
)
} else {
const content = Buffer.from(typeof extra.content === 'string' ? extra.content : '', 'utf-8')
// The key ends with the file name, which gives the same type as the upload's.
const contentType = getFileMimeType(null, started.key, 'text/plain')
await s3Service.putObject(
storageBucket(),
started.key,
content,
content.byteLength,
contentType,
)
}
const asset = await runOperation(
completeUploadOp,
{ ...ctx, via: 'agent' },
{ uploadId: started.uploadId },
)
return { content: [{ type: 'text', text: JSON.stringify(asset, null, 2) }], details: {} }
}
const createFileExtension: AgentToolExtension = {
description:
'Create a new file in a folder. Choose exactly ONE source: ' +
'(1) "path" to upload a local file (e.g. one you made in .pi), OR ' +
'(2) "name" and "content" to create a text file with that content. ' +
METADATA_HINT,
parameterOverrides: z.object({
name: createFile.input.shape.name
.optional()
.describe(
'File name including its extension (e.g. "notes.md"). Required with "content"; defaults to the local file\'s name with "path".',
),
}),
extraParameters: z.object({
path: z
.string()
.min(1)
.optional()
.describe('Absolute or relative path of a local file to upload. Omit when using "content".'),
content: z
.string()
.optional()
.describe('Full text content of a new text file. Requires "name"; omit when using "path".'),
}),
hiddenParameters: ['contentType'],
adjustParameters: setCreationMetadata,
toInput(input, extra) {
const hasPath = typeof extra.path === 'string'
const hasContent = typeof extra.content === 'string'
if (hasPath === hasContent) {
throw new Error('Provide exactly one of "path" (a local file) or "content" (with "name").')
}
if (hasPath) {
const absolutePath = resolveLocalFile(extra.path as string)
return {
...input,
name: input.name ?? path.basename(absolutePath),
contentType: readFileMimeType(absolutePath),
}
}
if (typeof input.name !== 'string' || !input.name.trim()) {
throw new Error('"name" is required when creating a file from "content".')
}
return { ...input, contentType: getFileMimeType(null, input.name, 'text/plain') }
},
present: uploadAndComplete,
}
const createVersionExtension: AgentToolExtension = {
description:
'Create a new version of an existing file (or version stack) from a local file path. ' +
'The new version becomes the current one. ' +
METADATA_HINT,
extraParameters: z.object({
path: z
.string()
.min(1)
.describe('Absolute or relative path of the local file to upload as the new version.'),
}),
hiddenParameters: ['name', 'contentType'],
adjustParameters: setCreationMetadata,
toInput(input, extra) {
const absolutePath = resolveLocalFile(extra.path as string)
return {
...input,
name: path.basename(absolutePath),
contentType: readFileMimeType(absolutePath),
}
},
present: uploadAndComplete,
}
registerAgentToolExtension('create_file', createFileExtension)
registerAgentToolExtension('create_version', createVersionExtension)
+429
View File
@@ -0,0 +1,429 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { Value } from 'typebox/value'
import { validateToolArguments } from '@earendil-works/pi-ai'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { AuditAction, type AssetInfo, type ProjectInfo } from '@shumai/dtos'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { projectService } from '@shumai/core/src/project/project'
import { buildOperationTools } from './operation-tools'
const ctx = { userId: 'user-1', agentId: 'agent-1', teamId: 'team-1', projectId: 'proj-1' }
function assetInfo(overrides: Partial<AssetInfo>): AssetInfo {
return {
id: 'asset-1',
name: 'Asset',
sizeByte: 0,
fileCount: 0,
type: 'file',
status: 'uploaded',
createdAt: '2026-01-01T00:00:00.000Z',
updatedAt: '2026-01-01T00:00:00.000Z',
projectId: 'proj-1',
...overrides,
}
}
function getTool(name: string): AgentTool {
const tool = buildOperationTools(ctx).find((t) => t.name === name)
if (!tool) throw new Error(`tool ${name} not built`)
return tool
}
async function run(name: string, params: Record<string, unknown>) {
const result = await getTool(name).execute('call-1', params as never)
return { result, output: JSON.parse((result.content[0] as { text: string }).text) as unknown }
}
function editCheck(id: string) {
return {
user: { id: 'user-1' },
permission: Permission.Edit,
type: ResourceType.Asset,
id,
}
}
describe('operation-backed agent tools', () => {
beforeEach(() => {
vi.spyOn(authzService, 'hasPermission').mockResolvedValue(undefined)
vi.spyOn(auditLogService, 'logAction').mockResolvedValue(null)
vi.spyOn(assetService, 'getAssetContext').mockResolvedValue({
teamId: 'team-1',
projectId: 'proj-1',
})
})
afterEach(() => {
vi.restoreAllMocks()
})
describe('tool definitions', () => {
it('keeps the existing tool names and adds the newer Operations', () => {
expect(
buildOperationTools(ctx)
.map((t) => t.name)
.sort(),
).toEqual([
'copy_assets',
'create_comment',
'create_file',
'create_folder',
'create_version',
'create_version_stack',
'delete_asset',
'download_asset',
'get_asset_render',
'get_folder_instructions',
'list_assets',
'list_comments',
'list_metadata_fields',
'list_projects',
'list_versions',
'move_assets',
'read_asset',
'remove_version',
'rename_asset',
'resolve_comment',
'search_assets',
'update_asset_metadata',
])
})
it('does not give the agent complete_upload: its create tools finish uploads themselves', () => {
expect(buildOperationTools(ctx).map((t) => t.name)).not.toContain('complete_upload')
})
it('keeps the existing parameter names', () => {
const params = (name: string) =>
Object.keys((getTool(name).parameters as unknown as { properties: object }).properties)
expect(params('create_folder')).toEqual(['parent', 'name'])
expect(params('rename_asset')).toEqual(['assetId', 'name'])
expect(params('move_assets')).toEqual(['assetIds', 'newParentId'])
expect(params('delete_asset')).toEqual(['assetId'])
expect(params('list_assets')).toEqual(['parent', 'type', 'first', 'after'])
})
it('exposes JSON Schema parameters the harness can validate', () => {
const tool = getTool('move_assets')
expect(tool.parameters).not.toHaveProperty('$schema')
expect(Value.Check(tool.parameters, { assetIds: ['a'], newParentId: 'b' })).toBe(true)
expect(Value.Check(tool.parameters, { assetIds: 'a', newParentId: 'b' })).toBe(false)
const description = (
tool.parameters as unknown as { properties: { assetIds: { description: string } } }
).properties.assetIds.description
expect(description).toContain('same parent folder')
})
it('passes harness validation with null optional parameters', async () => {
vi.spyOn(assetService, 'listChildren').mockResolvedValue({
data: [],
pageInfo: { total: 0 },
})
const tool = getTool('list_assets')
const validated = validateToolArguments(
{ name: tool.name, description: '', parameters: tool.parameters } as Parameters<
typeof validateToolArguments
>[0],
{
type: 'toolCall',
id: 'call-1',
name: tool.name,
arguments: { parent: 'folder-1' },
} as Parameters<typeof validateToolArguments>[1],
)
await expect(tool.execute('call-1', validated as never)).resolves.toBeDefined()
})
})
describe('list_assets', () => {
beforeEach(() => {
vi.spyOn(assetService, 'listChildren').mockResolvedValue({
data: [
assetInfo({ id: '01M2FILE2', name: 'file2.txt', sizeByte: 200 }),
assetInfo({ id: '01M1FILE1', name: 'file1.txt', sizeByte: 100 }),
],
pageInfo: { cursor: 'next-cursor', total: 5 },
})
})
it('checks read permission and lists all types with the default page size', async () => {
const { result, output } = await run('list_assets', { parent: 'folder-1' })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: Permission.Read,
type: ResourceType.Asset,
id: 'folder-1',
})
expect(assetService.listChildren).toHaveBeenCalledWith({
assetId: 'folder-1',
assetType: 'all',
first: 20,
after: undefined,
})
expect(result.details).toEqual({})
expect(output).toEqual({
data: [
{ id: '01M2FILE2', name: 'file2.txt', type: 'file', size: 200, projectId: 'proj-1' },
{ id: '01M1FILE1', name: 'file1.txt', type: 'file', size: 100, projectId: 'proj-1' },
],
pageInfo: { hasNextPage: true, endCursor: 'next-cursor' },
total: 5,
})
})
it('passes cursor pagination and the type filter through', async () => {
await run('list_assets', { parent: 'folder-1', first: 10, after: 'cur', type: 'folder' })
expect(assetService.listChildren).toHaveBeenCalledWith({
assetId: 'folder-1',
assetType: 'folder',
first: 10,
after: 'cur',
})
})
it('treats null optional parameters as omitted', async () => {
await run('list_assets', { parent: 'folder-1', type: null, first: null, after: null })
expect(assetService.listChildren).toHaveBeenCalledWith({
assetId: 'folder-1',
assetType: 'all',
first: 20,
after: undefined,
})
})
it('rejects a null required parameter', async () => {
await expect(run('list_assets', { parent: null })).rejects.toMatchObject({ status: 400 })
expect(assetService.listChildren).not.toHaveBeenCalled()
})
})
describe('create_folder', () => {
beforeEach(() => {
vi.spyOn(assetService, 'createAsset').mockResolvedValue(
assetInfo({ id: 'folder-1', name: 'New Folder', type: 'folder' }),
)
})
it('checks edit permission and creates a folder attributed to the user and agent', async () => {
const { result, output } = await run('create_folder', {
parent: 'root-folder',
name: 'New Folder',
})
expect(authzService.hasPermission).toHaveBeenCalledWith(editCheck('root-folder'))
expect(assetService.createAsset).toHaveBeenCalledWith({
name: 'New Folder',
parentId: 'root-folder',
type: 'folder',
creatorId: 'user-1',
agentId: 'agent-1',
})
expect(result.details).toEqual({})
expect(output).toEqual({
id: 'folder-1',
name: 'New Folder',
type: 'folder',
size: 0,
projectId: 'proj-1',
})
})
it('logs folder_create with the agent and via agent', async () => {
await run('create_folder', { parent: 'root-folder', name: 'New Folder' })
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: AuditAction.folder_create,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: 'proj-1',
itemId: 'folder-1',
via: 'agent',
})
})
})
describe('rename_asset', () => {
it('checks edit permission and renames', async () => {
vi.spyOn(assetService, 'updateAssetName').mockResolvedValue(
assetInfo({ id: 'asset-1', name: 'Renamed Asset', sizeByte: 1024 }),
)
const { output } = await run('rename_asset', { assetId: 'asset-1', name: 'Renamed Asset' })
expect(authzService.hasPermission).toHaveBeenCalledWith(editCheck('asset-1'))
expect(assetService.updateAssetName).toHaveBeenCalledWith({
id: 'asset-1',
name: 'Renamed Asset',
})
expect(output).toEqual({
id: 'asset-1',
name: 'Renamed Asset',
type: 'file',
size: 1024,
projectId: 'proj-1',
})
expect(auditLogService.logAction).toHaveBeenCalledWith(
expect.objectContaining({ action: AuditAction.file_update, itemId: 'asset-1' }),
)
})
it('logs folder_update when renaming a folder', async () => {
vi.spyOn(assetService, 'updateAssetName').mockResolvedValue(
assetInfo({ id: 'folder-1', name: 'Renamed Folder', type: 'folder' }),
)
await run('rename_asset', { assetId: 'folder-1', name: 'Renamed Folder' })
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: AuditAction.folder_update,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: 'proj-1',
itemId: 'folder-1',
via: 'agent',
})
})
it('fails without renaming when the permission check rejects', async () => {
vi.mocked(authzService.hasPermission).mockRejectedValueOnce(new Error('Permission denied'))
const update = vi.spyOn(assetService, 'updateAssetName')
await expect(run('rename_asset', { assetId: 'asset-1', name: 'New' })).rejects.toThrow(
'Permission denied',
)
expect(update).not.toHaveBeenCalled()
})
})
describe('move_assets', () => {
beforeEach(() => {
vi.spyOn(assetService, 'reparentAssets').mockResolvedValue(undefined)
})
it('checks permissions on the destination and every asset, then reparents', async () => {
const { output } = await run('move_assets', {
assetIds: ['asset-1', 'asset-2'],
newParentId: 'folder-dest',
})
expect(vi.mocked(authzService.hasPermission).mock.calls.map(([req]) => req)).toEqual([
editCheck('folder-dest'),
editCheck('asset-1'),
editCheck('asset-2'),
])
expect(assetService.reparentAssets).toHaveBeenCalledWith({
assetIds: ['asset-1', 'asset-2'],
newParentId: 'folder-dest',
creatorId: 'user-1',
})
expect(output).toEqual({ movedAssetIds: ['asset-1', 'asset-2'], newParentId: 'folder-dest' })
})
it('logs asset_reparent for each asset', async () => {
await run('move_assets', { assetIds: ['asset-1', 'asset-2'], newParentId: 'folder-dest' })
expect(assetService.getAssetContext).toHaveBeenCalledWith('folder-dest')
expect(auditLogService.logAction).toHaveBeenCalledTimes(2)
for (const itemId of ['asset-1', 'asset-2']) {
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: AuditAction.asset_reparent,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: 'proj-1',
itemId,
via: 'agent',
})
}
})
it('fails without moving when the destination permission check rejects', async () => {
vi.mocked(authzService.hasPermission).mockRejectedValueOnce(
new Error('Access denied to destination'),
)
await expect(
run('move_assets', { assetIds: ['asset-1'], newParentId: 'folder-dest' }),
).rejects.toThrow('Access denied to destination')
expect(assetService.reparentAssets).not.toHaveBeenCalled()
})
})
describe('delete_asset', () => {
beforeEach(() => {
vi.spyOn(assetService, 'deleteAssets').mockResolvedValue(undefined)
})
it.each([
['file', AuditAction.file_delete],
['folder', AuditAction.folder_delete],
] as const)('deletes a %s and logs %s', async (type, action) => {
vi.spyOn(assetService, 'getAssetMeta').mockResolvedValue({
id: 'asset-1',
name: 'x',
type,
parentId: null,
teamId: 'team-1',
projectId: 'proj-1',
})
const { output } = await run('delete_asset', { assetId: 'asset-1' })
expect(authzService.hasPermission).toHaveBeenCalledWith(editCheck('asset-1'))
expect(assetService.deleteAssets).toHaveBeenCalledWith(['asset-1'])
expect(output).toEqual({ deletedAssetId: 'asset-1' })
expect(auditLogService.logAction).toHaveBeenCalledWith({
action,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: 'proj-1',
itemId: 'asset-1',
via: 'agent',
})
})
it('fails without deleting when the asset is not found', async () => {
vi.spyOn(assetService, 'getAssetMeta').mockRejectedValue(new Error('Asset not found'))
await expect(run('delete_asset', { assetId: 'missing' })).rejects.toThrow('Asset not found')
expect(assetService.deleteAssets).not.toHaveBeenCalled()
})
it('fails without deleting when the permission check rejects', async () => {
vi.mocked(authzService.hasPermission).mockRejectedValueOnce(new Error('Permission denied'))
await expect(run('delete_asset', { assetId: 'asset-1' })).rejects.toThrow('Permission denied')
expect(assetService.deleteAssets).not.toHaveBeenCalled()
})
})
describe('list_projects', () => {
it("lists the user's projects", async () => {
const project: ProjectInfo = {
id: 'proj-1',
name: 'Project',
teamId: 'team-1',
rootFolder: 'root-1',
enableNotification: true,
updatedAt: '2026-01-01T00:00:00.000Z',
}
vi.spyOn(projectService, 'getUserProjects').mockResolvedValue([project])
const { output } = await run('list_projects', {})
expect(projectService.getUserProjects).toHaveBeenCalledWith('user-1', 200)
expect(output).toEqual({
data: [{ id: 'proj-1', name: 'Project', teamId: 'team-1', rootFolderId: 'root-1' }],
})
})
})
})
+165
View File
@@ -0,0 +1,165 @@
import { z } from 'zod'
import type { TSchema } from 'typebox'
import { Compile } from 'typebox/compile'
import { type AgentTool, type AgentToolResult } from '@earendil-works/pi-agent-core'
import { dropNullOptionals } from '@shumai/operations'
import {
getOperationsForSurface,
runOperation,
type Operation,
type OperationContext,
} from '@shumai/core/src/operations'
/** Context for agent-invoked Operations; `via` is always `agent`. */
export type AgentOperationContext = Omit<OperationContext, 'via'>
/** Per-session settings for building tools, e.g. schemas that depend on the project. */
export interface AgentToolOptions {
/**
* Schema of the conversation project's Creation Context fields, for the `metadata`
* parameter of tools that create files. Undefined when the project has none.
*/
creationMetadataSchema?: TSchema
}
/** The JSON Schema of a tool's parameters (an object schema). */
export interface ToolParametersSchema {
type?: string
properties: Record<string, unknown>
required?: string[]
[key: string]: unknown
}
/**
* Agent-specific presentation for an Operation. By default a tool returns the Operation output
* as JSON text; an extension can add agent-only parameters, map them into the Operation input,
* and render the output (e.g. as image blocks).
*/
export interface AgentToolExtension {
/** Replaces the Operation description for the agent (e.g. where files end up). */
description?: string
/** Extra parameters only the agent sees; they are removed before the Operation runs. */
extraParameters?: z.ZodObject
/**
* Agent-facing versions of Operation input fields, e.g. a required field made optional
* because an extra parameter can be used instead. The Operation still validates its own
* input when it runs.
*/
parameterOverrides?: z.ZodObject
/** Operation input fields the agent doesn't see because `toInput` fills them in. */
hiddenParameters?: readonly string[]
/** Adjust the parameters' JSON Schema for this session (e.g. a project-specific field). */
adjustParameters?(schema: ToolParametersSchema, options: AgentToolOptions): void
/** Adjust the Operation input using the extra parameters. */
toInput?(input: Record<string, unknown>, extra: Record<string, unknown>): Record<string, unknown>
/**
* Handle an agent-only mode without running the Operation (e.g. a storage key instead of
* an asset). Return undefined to run the Operation.
*/
intercept?(
input: Record<string, unknown>,
extra: Record<string, unknown>,
ctx: AgentOperationContext,
): Promise<AgentToolResult<unknown> | undefined>
present(
output: unknown,
extra: Record<string, unknown>,
ctx: AgentOperationContext,
): Promise<AgentToolResult<unknown>>
}
const extensions = new Map<string, AgentToolExtension>()
/** Register agent presentation for an Operation (see `./operation-extensions`). */
export function registerAgentToolExtension(name: string, extension: AgentToolExtension): void {
extensions.set(name, extension)
}
function toolInputSchema(op: Operation, extension?: AgentToolExtension): z.ZodObject {
let schema = op.definition.input
if (extension?.parameterOverrides) schema = schema.extend(extension.parameterOverrides.shape)
if (extension?.extraParameters) schema = schema.extend(extension.extraParameters.shape)
return schema
}
/**
* Convert an Operation's Zod input schema into the JSON Schema the agent harness expects.
* TypeBox schemas are plain JSON Schema at runtime, so the converted schema is used as-is.
* Throws when the schema cannot be compiled, so a bad definition fails loudly in tests.
*/
export function toToolParameters(
op: Operation,
extension?: AgentToolExtension,
options: AgentToolOptions = {},
): TSchema {
const schema = z.toJSONSchema(toolInputSchema(op, extension), {
io: 'input',
}) as unknown as ToolParametersSchema
// `$schema` is not understood by the harness validator or every model provider.
delete schema.$schema
for (const name of extension?.hiddenParameters ?? []) {
delete schema.properties[name]
schema.required = schema.required?.filter((field) => field !== name)
}
extension?.adjustParameters?.(schema, options)
Compile(schema)
return schema as unknown as TSchema
}
/** Split agent-only parameters from the Operation input. */
export function splitAgentParams(
op: Operation,
extension: AgentToolExtension | undefined,
params: unknown,
): { input: unknown; extra: Record<string, unknown> } {
const cleaned = dropNullOptionals(toolInputSchema(op, extension), params)
if (!extension?.extraParameters || !cleaned || typeof cleaned !== 'object') {
return { input: cleaned, extra: {} }
}
const extraKeys = new Set(Object.keys(extension.extraParameters.shape))
const input: Record<string, unknown> = {}
const extra: Record<string, unknown> = {}
for (const [key, value] of Object.entries(cleaned)) {
;(extraKeys.has(key) ? extra : input)[key] = value
}
return { input: extension.toInput ? extension.toInput(input, extra) : input, extra }
}
/** Expose an Operation as a native agent tool that runs through `runOperation`. */
export function toAgentTool(
op: Operation,
ctx: AgentOperationContext,
options: AgentToolOptions = {},
): AgentTool {
const { definition } = op
const extension = extensions.get(definition.name)
return {
name: definition.name,
label: definition.title,
description: extension?.description ?? definition.description,
parameters: toToolParameters(op, extension, options),
execute: async (_toolCallId, params) => {
const { input, extra } = splitAgentParams(op, extension, params)
if (extension?.intercept) {
const handled = await extension.intercept(input as Record<string, unknown>, extra, ctx)
if (handled) return handled
}
const output = await runOperation(op, { ...ctx, via: 'agent' }, input)
if (extension) {
return extension.present(output, extra, ctx)
}
return {
content: [{ type: 'text', text: JSON.stringify(output, null, 2) }],
details: {},
}
},
}
}
/** Agent tools for every Operation with the `agent` surface. */
export function buildOperationTools(
ctx: AgentOperationContext,
options: AgentToolOptions = {},
): AgentTool[] {
return getOperationsForSurface('agent').map((op) => toAgentTool(op, ctx, options))
}
@@ -1,117 +0,0 @@
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 { assetService } from '@shumai/core/src/asset/asset'
import { authzService } from '@shumai/core/src/authz/authz'
vi.mock('@shumai/core/src/s3/s3', () => ({
s3Service: {
uploadFileToKey: vi.fn(),
putObject: vi.fn(),
},
}))
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
createFile: vi
.fn()
.mockResolvedValue({ id: 'file-1', name: 'test', type: 'file', sizeByte: 100 }),
},
}))
/**
* 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({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
id: 'file-1',
name: 'test',
type: 'file',
size: 100,
})
expect(s3Service.putObject).toHaveBeenCalledTimes(1)
expect(authzService.hasPermission).toHaveBeenCalledTimes(1)
expect(assetService.createFile).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,372 +0,0 @@
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 { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService } from '@shumai/core/src/authz/authz'
import { AuditAction } from '@shumai/dtos'
import { Value } from 'typebox/value'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
vi.mock('@shumai/core/src/s3/s3', () => ({
s3Service: {
uploadFileToKey: vi.fn(),
putObject: vi.fn(),
},
}))
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
createFile: vi
.fn()
.mockResolvedValue({ id: 'file-1', name: 'test', type: 'file', sizeByte: 100 }),
},
}))
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
auditLogService: {
logAction: vi.fn().mockResolvedValue(undefined),
},
}))
describe('createCreateFileTool', () => {
const createdFiles: string[] = []
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
for (const f of createdFiles) {
if (fs.existsSync(f)) {
fs.unlinkSync(f)
}
}
createdFiles.length = 0
})
const createTempFile = (content: string | Buffer, filename: string): string => {
const filePath = path.join(os.tmpdir(), filename)
fs.writeFileSync(filePath, content)
createdFiles.push(filePath)
return filePath
}
it('should upload a local file when "path" is provided', async () => {
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, data: null })
expect(s3Service.uploadFileToKey).toHaveBeenCalledTimes(1)
expect(s3Service.putObject).not.toHaveBeenCalled()
const uploadArgs = vi.mocked(s3Service.uploadFileToKey).mock.calls[0]
const s3Key = uploadArgs[1]
expect(s3Key).toMatch(/^files\/.+\.md$/)
expect(uploadArgs[2]).toBe('text/markdown')
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'edit',
type: 'asset',
id: 'folder-1',
})
expect(assetService.createFile).toHaveBeenCalledWith({
parentId: 'folder-1',
name: 'create-file-test.md',
key: s3Key,
sizeByte: Buffer.byteLength('# Hello from disk', 'utf-8'),
contentType: 'text/markdown',
creatorId: 'user-1',
metadata: undefined,
})
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
id: 'file-1',
name: 'test',
type: 'file',
size: 100,
})
})
it('should forward the metadata to assetService.createFile when provided', async () => {
const filePath = createTempFile('# Hello from disk', 'create-file-metadata.md')
const metadataSchema = fieldsToTypeBoxSchema([
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
])
const tool = createCreateFileTool('user-1', metadataSchema)
await tool.execute('call-1', {
parent: 'folder-1',
path: filePath,
data: null,
metadata: { prompt: 'Generated using gemini' },
})
expect(assetService.createFile).toHaveBeenCalledWith(
expect.objectContaining({
metadata: { prompt: 'Generated using gemini' },
}),
)
})
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', 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(assetService.createFile).toHaveBeenCalledWith(
expect.objectContaining({
metadata: {
provider: { newOption: { value: 'Kling' } },
tags: ['tag1', { newOption: { value: 'Tag 2' } }],
},
}),
)
})
it('should not include metadata when metadata is null', async () => {
const filePath = createTempFile('# Hello from disk', 'create-file-no-metadata.md')
const metadataSchema = fieldsToTypeBoxSchema([
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
])
const tool = createCreateFileTool('user-1', metadataSchema)
await tool.execute('call-1', {
parent: 'folder-1',
path: filePath,
data: null,
metadata: null,
})
const callArgs = vi.mocked(assetService.createFile).mock.calls[0][0]
expect(callArgs.metadata).toBeUndefined()
})
it('should throw when the local file does not exist', async () => {
const tool = createCreateFileTool('user-1')
await expect(
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(assetService.createFile).not.toHaveBeenCalled()
})
it('should create a file directly from name and content', async () => {
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' },
})
expect(s3Service.putObject).toHaveBeenCalledTimes(1)
expect(s3Service.uploadFileToKey).not.toHaveBeenCalled()
const [bucket, s3Key, body, size, contentType] = vi.mocked(s3Service.putObject).mock
.calls[0] as unknown as [string, string, Buffer, number, string]
expect(bucket).toBe(process.env.S3_BUCKET || 'shumai')
expect(s3Key).toMatch(/^files\/.+notes\.md$/)
expect(body.toString('utf-8')).toBe('# Notes\n\nHello')
expect(size).toBe(Buffer.byteLength('# Notes\n\nHello', 'utf-8'))
expect(contentType).toBe('text/markdown')
expect(assetService.createFile).toHaveBeenCalledWith({
parentId: 'folder-1',
name: 'notes.md',
key: s3Key,
sizeByte: Buffer.byteLength('# Notes\n\nHello', 'utf-8'),
contentType: 'text/markdown',
creatorId: 'user-1',
metadata: undefined,
})
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
id: 'file-1',
name: 'test',
type: 'file',
size: 100,
})
})
it('should default unknown extensions to text/plain when creating from content', async () => {
const tool = createCreateFileTool('user-1')
await tool.execute('call-1', {
parent: 'folder-1',
path: null,
data: { name: 'script.ts', content: 'const x = 1' },
})
const [, , , , contentType] = vi.mocked(s3Service.putObject).mock.calls[0] as unknown as [
string,
string,
Buffer,
number,
string,
]
expect(contentType).toBe('text/plain')
})
it('should sanitize the name when creating from content', async () => {
const tool = createCreateFileTool('user-1')
await tool.execute('call-1', {
parent: 'folder-1',
path: null,
data: { name: 'my/notes.md', content: 'hello' },
})
const [, s3Key] = vi.mocked(s3Service.putObject).mock.calls[0] as unknown as [
string,
string,
Buffer,
number,
string,
]
expect(s3Key).toContain('my_notes.md')
expect(assetService.createFile).toHaveBeenCalledWith(
expect.objectContaining({ name: 'my_notes.md' }),
)
})
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', 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 () => {
const tool = createCreateFileTool('user-1')
await expect(
tool.execute('call-1', {
parent: 'folder-1',
path: '/some/file.md',
data: { name: 'notes.md', content: 'hello' },
}),
).rejects.toThrow('Provide exactly one of "path" (a local file) or "data" (name and content).')
expect(s3Service.uploadFileToKey).not.toHaveBeenCalled()
expect(s3Service.putObject).not.toHaveBeenCalled()
expect(assetService.createFile).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()
})
it('should pass agentId and record audit log when agentContext is provided', async () => {
const filePath = createTempFile('# Agent file', 'agent-file.md')
const tool = createCreateFileTool('user-1', undefined, { teamId: 'team-1', agentId: 'agent-1' })
await tool.execute('call-1', { parent: 'folder-1', path: filePath, data: null })
expect(assetService.createFile).toHaveBeenCalledWith(
expect.objectContaining({
creatorId: 'user-1',
agentId: 'agent-1',
}),
)
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: AuditAction.file_create,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: undefined,
itemId: 'file-1',
})
})
})
-159
View File
@@ -1,159 +0,0 @@
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { s3Service } from '@shumai/core/src/s3/s3'
import { getFileMimeType, readFileMimeType } from '@shumai/core/src/utils/file-mime'
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
import { type User } from '@shumai/db'
import { AuditAction } from '@shumai/dtos'
import { Type, type TSchema } from 'typebox'
import * as fs from 'fs'
import * as path from 'path'
import { ulid } from 'ulid'
interface CreateFileToolParams {
parent: string
path: string | null
data: { name: string; content: string } | null
metadata?: Record<string, unknown> | null
}
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,
metadataSchema?: TSchema,
agentContext?: { teamId?: string; agentId?: string },
): 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. ' +
'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) => {
// 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
let mimeType: string
let s3Key: string
if (typeof source === 'string') {
// Branch A: upload an existing local file
const absolutePath = path.resolve(process.cwd(), source)
if (!fs.existsSync(absolutePath)) {
throw new Error(`Local file not found at path: ${source}`)
}
name = path.basename(absolutePath)
size = fs.statSync(absolutePath).size
mimeType = readFileMimeType(absolutePath)
// Generate compliant S3 key matching normal file upload format
s3Key = `files/${ulid()}/${sanitizeFilename(name)}`
await s3Service.uploadFileToKey(absolutePath, s3Key, mimeType)
} else {
// Branch B: create the file directly from provided name and content
name = sanitizeFilename(source.name)
const content = Buffer.from(source.content, 'utf-8')
size = content.byteLength
mimeType = getFileMimeType(null, source.name, 'text/plain')
s3Key = `files/${ulid()}/${name}`
await s3Service.putObject(process.env.S3_BUCKET || 'shumai', s3Key, content, size, mimeType)
}
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Edit,
type: ResourceType.Asset,
id: p.parent,
})
const asset = await assetService.createFile({
parentId: p.parent,
name,
key: s3Key,
sizeByte: size,
contentType: mimeType,
creatorId: userId,
agentId: agentContext?.agentId,
metadata: p.metadata ?? undefined,
})
if (agentContext?.teamId) {
await auditLogService.logAction({
action: AuditAction.file_create,
teamId: agentContext.teamId,
userId,
agentId: agentContext.agentId,
projectId: asset.projectId ?? undefined,
itemId: asset.id,
})
}
const result = {
id: asset.id,
name: asset.name,
type: asset.type,
size: Number(asset.sizeByte),
}
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
details: {},
}
},
}
return tool
}
@@ -1,87 +0,0 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createCreateFolderTool } from './create-folder'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService } from '@shumai/core/src/authz/authz'
import { AuditAction } from '@shumai/dtos'
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
createAsset: vi.fn().mockResolvedValue({
id: 'folder-1',
name: 'New Folder',
type: 'folder',
sizeByte: 0,
}),
},
}))
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
auditLogService: {
logAction: vi.fn().mockResolvedValue(undefined),
},
}))
describe('createCreateFolderTool', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('checks edit permission and calls assetService.createAsset with folder type', async () => {
const tool = createCreateFolderTool('user-1')
const result = await tool.execute('call-1', { parent: 'root-folder', name: 'New Folder' })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'edit',
type: 'asset',
id: 'root-folder',
})
expect(assetService.createAsset).toHaveBeenCalledWith({
name: 'New Folder',
parentId: 'root-folder',
type: 'folder',
creatorId: 'user-1',
agentId: undefined,
})
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
id: 'folder-1',
name: 'New Folder',
type: 'folder',
size: 0,
})
})
it('passes agentId and logs audit action when agentContext is provided', async () => {
const tool = createCreateFolderTool('user-1', { teamId: 'team-1', agentId: 'agent-1' })
await tool.execute('call-1', { parent: 'root-folder', name: 'New Folder' })
expect(assetService.createAsset).toHaveBeenCalledWith({
name: 'New Folder',
parentId: 'root-folder',
type: 'folder',
creatorId: 'user-1',
agentId: 'agent-1',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: AuditAction.folder_create,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: undefined,
itemId: 'folder-1',
})
})
})
-68
View File
@@ -1,68 +0,0 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { type User } from '@shumai/db'
import { AuditAction } from '@shumai/dtos'
const createFolderSchema = Type.Object({
parent: Type.String({
description:
'The parent folder ID under which to create the new folder. This parameter is required.',
}),
name: Type.String({
description: 'The name of the new folder to create. This parameter is required.',
}),
})
export function createCreateFolderTool(
userId: string,
agentContext?: { teamId?: string; agentId?: string },
): AgentTool<typeof createFolderSchema> {
return {
name: 'create_folder',
label: 'Create Folder',
description: 'Create a new folder in a specified parent folder.',
parameters: createFolderSchema,
execute: async (_toolCallId, params) => {
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Edit,
type: ResourceType.Asset,
id: params.parent,
})
const newFolder = await assetService.createAsset({
name: params.name,
parentId: params.parent,
type: 'folder',
creatorId: userId,
agentId: agentContext?.agentId,
})
if (agentContext?.teamId) {
await auditLogService.logAction({
action: AuditAction.folder_create,
teamId: agentContext.teamId,
userId,
agentId: agentContext.agentId,
projectId: newFolder.projectId ?? undefined,
itemId: newFolder.id,
})
}
const result = {
id: newFolder.id,
name: newFolder.name,
type: newFolder.type,
size: Number(newFolder.sizeByte),
}
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
details: {},
}
},
}
}
@@ -1,241 +0,0 @@
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 { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService } from '@shumai/core/src/authz/authz'
import { AuditAction } from '@shumai/dtos'
import { Value } from 'typebox/value'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
vi.mock('@shumai/core/src/s3/s3', () => ({
s3Service: {
uploadFileToKey: vi.fn(),
},
}))
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
createVersion: vi
.fn()
.mockResolvedValue({ id: 'file-1', name: 'v2', type: 'file', sizeByte: 100 }),
},
}))
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
auditLogService: {
logAction: vi.fn().mockResolvedValue(undefined),
},
}))
describe('createCreateVersionTool', () => {
const createdFiles: string[] = []
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
for (const f of createdFiles) {
if (fs.existsSync(f)) {
fs.unlinkSync(f)
}
}
createdFiles.length = 0
})
const createTempFile = (content: string | Buffer, filename: string): string => {
const filePath = path.join(os.tmpdir(), filename)
fs.writeFileSync(filePath, content)
createdFiles.push(filePath)
return filePath
}
it('should upload a local file and create a version via assetService.createVersion', async () => {
const filePath = createTempFile('# Hello from disk', 'create-version-test.md')
const tool = createCreateVersionTool('user-1')
const result = await tool.execute('call-1', { parent: 'file-1', path: filePath })
expect(s3Service.uploadFileToKey).toHaveBeenCalledTimes(1)
const uploadArgs = vi.mocked(s3Service.uploadFileToKey).mock.calls[0]
const s3Key = uploadArgs[1]
expect(s3Key).toMatch(/^files\/.+\.md$/)
expect(uploadArgs[2]).toBe('text/markdown')
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'edit',
type: 'asset',
id: 'file-1',
})
expect(assetService.createVersion).toHaveBeenCalledWith({
parentId: 'file-1',
name: 'create-version-test.md',
key: s3Key,
sizeByte: Buffer.byteLength('# Hello from disk', 'utf-8'),
contentType: 'text/markdown',
creatorId: 'user-1',
metadata: undefined,
})
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
id: 'file-1',
name: 'v2',
type: 'file',
size: 100,
})
})
it('should forward the metadata to assetService.createVersion when provided', async () => {
const filePath = createTempFile('# Hello from disk', 'create-version-metadata.md')
const metadataSchema = fieldsToTypeBoxSchema([
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
])
const tool = createCreateVersionTool('user-1', metadataSchema)
await tool.execute('call-1', {
parent: 'file-1',
path: filePath,
metadata: { prompt: 'Generated using gemini' },
})
expect(assetService.createVersion).toHaveBeenCalledWith(
expect.objectContaining({
metadata: { prompt: 'Generated using gemini' },
}),
)
})
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', metadataSchema)
await tool.execute('call-1', {
parent: 'file-1',
path: filePath,
metadata: {
provider: { newOption: { value: 'Kling' } },
tags: ['tag1', { newOption: { value: 'Tag 2' } }],
},
})
expect(assetService.createVersion).toHaveBeenCalledWith(
expect.objectContaining({
metadata: {
provider: { newOption: { value: 'Kling' } },
tags: ['tag1', { newOption: { value: 'Tag 2' } }],
},
}),
)
})
it('should not include metadata when metadata is null', async () => {
const filePath = createTempFile('# Hello from disk', 'create-version-no-metadata.md')
const metadataSchema = fieldsToTypeBoxSchema([
{ id: 'prompt', config: { name: 'Prompt', type: 'text' } },
])
const tool = createCreateVersionTool('user-1', metadataSchema)
await tool.execute('call-1', { parent: 'file-1', path: filePath, metadata: null })
const callArgs = vi.mocked(assetService.createVersion).mock.calls[0][0]
expect(callArgs.metadata).toBeUndefined()
})
it('should throw when the local file does not exist', async () => {
const tool = createCreateVersionTool('user-1')
await expect(
tool.execute('call-1', { parent: 'file-1', path: '/nonexistent/file.md' }),
).rejects.toThrow('Local file not found at path: /nonexistent/file.md')
expect(s3Service.uploadFileToKey).not.toHaveBeenCalled()
expect(assetService.createVersion).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()
})
it('should pass agentId and record audit log when agentContext is provided', async () => {
const filePath = createTempFile('# Agent version file', 'agent-ver-file.md')
const tool = createCreateVersionTool('user-1', undefined, {
teamId: 'team-1',
agentId: 'agent-1',
})
await tool.execute('call-1', { parent: 'file-1', path: filePath })
expect(assetService.createVersion).toHaveBeenCalledWith(
expect.objectContaining({
creatorId: 'user-1',
agentId: 'agent-1',
}),
)
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: AuditAction.file_create,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: undefined,
itemId: 'file-1',
})
})
})
-108
View File
@@ -1,108 +0,0 @@
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { s3Service } from '@shumai/core/src/s3/s3'
import { readFileMimeType } from '@shumai/core/src/utils/file-mime'
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
import { type User } from '@shumai/db'
import { AuditAction } from '@shumai/dtos'
import { Type, type TSchema } from 'typebox'
import * as fs from 'fs'
import * as path from 'path'
import { ulid } from 'ulid'
interface CreateVersionToolParams {
parent: string
path: string
metadata?: Record<string, unknown> | null
}
export function createCreateVersionTool(
userId: string,
metadataSchema?: TSchema,
agentContext?: { teamId?: string; agentId?: string },
): 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. ' +
'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) => {
// 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: ${p.path}`)
}
const fileSize = fs.statSync(absolutePath).size
const mimeType = readFileMimeType(absolutePath)
const name = path.basename(absolutePath)
// Generate compliant S3 key matching normal file upload format
const s3Key = `files/${ulid()}/${sanitizeFilename(name)}`
await s3Service.uploadFileToKey(absolutePath, s3Key, mimeType)
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Edit,
type: ResourceType.Asset,
id: p.parent,
})
const asset = await assetService.createVersion({
parentId: p.parent,
name,
key: s3Key,
sizeByte: fileSize,
contentType: mimeType,
creatorId: userId,
agentId: agentContext?.agentId,
metadata: p.metadata ?? undefined,
})
if (agentContext?.teamId) {
await auditLogService.logAction({
action: AuditAction.file_create,
teamId: agentContext.teamId,
userId,
agentId: agentContext.agentId,
projectId: asset.projectId ?? undefined,
itemId: asset.id,
})
}
const result = {
id: asset.id,
name: asset.name,
type: asset.type,
size: Number(asset.sizeByte),
}
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
details: {},
}
},
}
return tool
}
@@ -1,128 +0,0 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createDeleteAssetTool } from './delete-asset'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService } from '@shumai/core/src/authz/authz'
import { prisma } from '@shumai/db'
import { AuditAction } from '@shumai/dtos'
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
deleteAssets: vi.fn().mockResolvedValue(undefined),
getAssetContext: vi.fn().mockResolvedValue({
teamId: 'team-1',
projectId: 'proj-1',
}),
},
}))
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
auditLogService: {
logAction: vi.fn().mockResolvedValue(undefined),
},
}))
vi.mock('@shumai/db', () => ({
prisma: {
asset: {
findUnique: vi.fn(),
},
},
}))
describe('createDeleteAssetTool', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('checks edit permission, deletes asset, and returns success for a file', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-1',
type: 'file',
projectId: 'proj-1',
} as unknown as never)
const tool = createDeleteAssetTool('user-1')
const result = await tool.execute('call-1', { assetId: 'asset-1' })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'edit',
type: 'asset',
id: 'asset-1',
})
expect(assetService.deleteAssets).toHaveBeenCalledWith(['asset-1'])
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
success: true,
deletedAssetId: 'asset-1',
})
})
it('logs file_delete audit action when deleting a file with agentContext', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-1',
type: 'file',
projectId: 'proj-1',
} as unknown as never)
const tool = createDeleteAssetTool('user-1', { teamId: 'team-1', agentId: 'agent-1' })
await tool.execute('call-1', { assetId: 'asset-1' })
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: AuditAction.file_delete,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: 'proj-1',
itemId: 'asset-1',
})
})
it('logs folder_delete audit action when deleting a folder with agentContext', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'folder-1',
type: 'folder',
projectId: 'proj-1',
} as unknown as never)
const tool = createDeleteAssetTool('user-1', { teamId: 'team-1', agentId: 'agent-1' })
await tool.execute('call-1', { assetId: 'folder-1' })
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: AuditAction.folder_delete,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: 'proj-1',
itemId: 'folder-1',
})
})
it('throws when asset is not found', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
const tool = createDeleteAssetTool('user-1')
await expect(tool.execute('call-1', { assetId: 'missing-asset' })).rejects.toThrow(
'Asset not found',
)
})
it('fails when permission check fails', async () => {
vi.mocked(authzService.hasPermission).mockRejectedValueOnce(new Error('Permission denied'))
const tool = createDeleteAssetTool('user-1')
await expect(tool.execute('call-1', { assetId: 'asset-1' })).rejects.toThrow(
'Permission denied',
)
})
})
-74
View File
@@ -1,74 +0,0 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { prisma, type User } from '@shumai/db'
import { AuditAction } from '@shumai/dtos'
const deleteAssetSchema = Type.Object(
{
assetId: Type.String({
description:
'The ID of the single asset (file or folder) to delete. This parameter is required.',
}),
},
{ additionalProperties: false },
)
export function createDeleteAssetTool(
userId: string,
agentContext?: { teamId?: string; agentId?: string },
): AgentTool<typeof deleteAssetSchema> {
return {
name: 'delete_asset',
label: 'Delete Asset',
description:
'Delete a single asset (file or folder) by its ID. Can only delete one asset at a time.',
parameters: deleteAssetSchema,
execute: async (_toolCallId, params) => {
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Edit,
type: ResourceType.Asset,
id: params.assetId,
})
const asset = await prisma.asset.findUnique({
where: { id: params.assetId },
select: { id: true, type: true, projectId: true },
})
if (!asset) {
throw new Error('Asset not found')
}
const ctx = await assetService.getAssetContext(params.assetId).catch(() => ({
teamId: agentContext?.teamId || '',
projectId: asset.projectId ?? undefined,
}))
await assetService.deleteAssets([params.assetId])
if (agentContext?.teamId) {
await auditLogService.logAction({
action: asset.type === 'folder' ? AuditAction.folder_delete : AuditAction.file_delete,
teamId: agentContext.teamId || ctx.teamId,
userId,
agentId: agentContext.agentId,
projectId: ctx.projectId ?? asset.projectId ?? undefined,
itemId: params.assetId,
})
}
const result = {
success: true,
deletedAssetId: params.assetId,
}
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
details: {},
}
},
}
}
+3 -331
View File
@@ -1,14 +1,8 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import {
createDownloadAssetTool,
resolveAssetIdFromKey,
resolveS3MediaBuffer,
} from './download-asset'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { resolveAssetIdFromKey, resolveS3MediaBuffer } from './download-asset'
import { prisma, type Asset, type StorageKey } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { authzService } from '@shumai/core/src/authz/authz'
import * as fs from 'fs'
import * as path from 'path'
vi.mock('@shumai/db', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shumai/db')>()
@@ -43,333 +37,11 @@ vi.mock('@shumai/core/src/authz/authz', () => ({
ResourceType: { Asset: 'asset' },
}))
describe('downloadAssetTool', () => {
const piDir = path.join(process.cwd(), '.pi')
const createdFiles: string[] = []
describe('download-asset storage key helpers', () => {
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
for (const f of createdFiles) {
if (fs.existsSync(f)) {
fs.unlinkSync(f)
}
}
createdFiles.length = 0
})
it('should throw authorization error if user ID is empty', async () => {
const tool = createDownloadAssetTool('')
await expect(tool.execute('call-1', { assetId: 'asset-1', key: null })).rejects.toThrow(
'User ID is required for authorization.',
)
})
it('should throw error if both assetId and key are null', async () => {
const tool = createDownloadAssetTool('user-1')
await expect(tool.execute('call-1', { assetId: null, key: null })).rejects.toThrow(
'Provide exactly one of "assetId" (to download a workspace asset) or "key" (to download a specific S3 storage key). Set the unused parameter to null.',
)
})
it('should throw error if both assetId and key are provided', async () => {
const tool = createDownloadAssetTool('user-1')
await expect(
tool.execute('call-1', { assetId: 'asset-1', key: 'files/path/file.png' }),
).rejects.toThrow(
'Provide exactly one of "assetId" (to download a workspace asset) or "key" (to download a specific S3 storage key). Set the unused parameter to null.',
)
})
describe('Download by assetId', () => {
it('should throw error if asset not found', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
const tool = createDownloadAssetTool('user-1')
await expect(tool.execute('call-1', { assetId: 'asset-1', key: null })).rejects.toThrow(
'Asset with ID asset-1 not found.',
)
})
it('should download proxy image to .pi directory when downloading by assetId', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-img-1',
name: 'sample_photo.jpg',
storageKey: { key: 'raw/sample_photo.jpg' },
media: {
proxyType: 'image',
imageTranscodes: [{ key: 'proxy/sample_photo.webp' }],
},
} as unknown as Asset)
vi.mocked(s3Service.downloadToFile).mockImplementation(async (_b, _k, dest) => {
await fs.promises.writeFile(dest, 'fake-proxy-image-bytes')
})
vi.mocked(s3Service.headObject).mockResolvedValue({
contentType: 'image/webp',
} as unknown as Awaited<ReturnType<typeof s3Service.headObject>>)
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', { assetId: 'asset-img-1', key: null })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'asset-img-1',
})
const expectedPath = path.join(piDir, 'asset-img-1_sample_photo.jpg')
createdFiles.push(expectedPath)
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
'shumai',
'proxy/sample_photo.webp',
expectedPath,
)
expect(fs.existsSync(expectedPath)).toBe(true)
expect(fs.readFileSync(expectedPath, 'utf-8')).toBe('fake-proxy-image-bytes')
expect(result.details.filePath).toBe(path.join('.pi', 'asset-img-1_sample_photo.jpg'))
expect(result.details.contentType).toBe('image/webp')
expect((result.content[0] as { type: 'text'; text: string }).text).toContain(
'Remember to delete this temporary file when finished.',
)
})
it('should resolve version_stack asset to its latest version file', async () => {
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'stack-1',
name: '',
type: 'version_stack',
storageKey: null,
media: null,
} as unknown as Asset)
vi.mocked(prisma.asset.findFirst).mockResolvedValue({
id: 'file-ver-2',
name: 'banana_pig.png',
type: 'file',
storageKey: { key: 'raw/banana_pig.png' },
media: {
proxyType: 'image',
imageTranscodes: [{ key: 'proxy/banana_pig.webp' }],
},
} as unknown as Asset)
vi.mocked(s3Service.downloadToFile).mockImplementation(async (_b, _k, dest) => {
await fs.promises.writeFile(dest, 'fake-version-bytes')
})
vi.mocked(s3Service.headObject).mockResolvedValue({
contentType: 'image/webp',
} as unknown as Awaited<ReturnType<typeof s3Service.headObject>>)
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', { assetId: 'stack-1', key: null })
expect(prisma.asset.findFirst).toHaveBeenCalledWith({
where: { parentId: 'stack-1', isDeleted: false },
orderBy: { sortIndex: 'asc' },
include: { storageKey: true },
})
const expectedPath = path.join(piDir, 'file-ver-2_banana_pig.png')
createdFiles.push(expectedPath)
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
'shumai',
'proxy/banana_pig.webp',
expectedPath,
)
expect(fs.existsSync(expectedPath)).toBe(true)
expect(result.details.name).toBe('banana_pig.png')
})
})
describe('Download by S3 key and Authorization', () => {
it('should reject keys that do not start with files/', async () => {
const tool = createDownloadAssetTool('user-1')
await expect(
tool.execute('call-1', { assetId: null, key: 'avatars/user-1.png' }),
).rejects.toThrow('Invalid storage key format: key must start with "files/"')
})
it('should reject keys with path traversal (..)', async () => {
const tool = createDownloadAssetTool('user-1')
await expect(
tool.execute('call-1', { assetId: null, key: 'files/../../etc/passwd' }),
).rejects.toThrow('cannot contain ".."')
})
it('should reject keys with insufficient path segments', async () => {
const tool = createDownloadAssetTool('user-1')
await expect(tool.execute('call-1', { assetId: null, key: 'files/invalid' })).rejects.toThrow(
'Invalid storage key format',
)
})
it('should reject keys that do not belong to any valid asset in database', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findFirst).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findUnique).mockResolvedValue(null)
const tool = createDownloadAssetTool('user-1')
await expect(
tool.execute('call-1', { assetId: null, key: 'files/nonexistent-id/shot.webp' }),
).rejects.toThrow('does not belong to any valid asset or the asset has been deleted')
})
it('should reject download when user lacks read permission on the owning asset (cross-project protection)', async () => {
// Key belongs to asset-in-other-project
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'asset-in-other-project',
} as unknown as Asset)
// Authz rejects because user is in a different project/team
vi.mocked(authzService.hasPermission).mockRejectedValue(
new Error('User does not have Read permission on asset asset-in-other-project'),
)
const tool = createDownloadAssetTool('user-1')
await expect(
tool.execute('call-1', {
assetId: null,
key: 'files/asset-in-other-project/screenshots/secret_frame.webp',
}),
).rejects.toThrow('User does not have Read permission on asset asset-in-other-project')
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'asset-in-other-project',
})
expect(s3Service.downloadToFile).not.toHaveBeenCalled()
})
it('should authorize and download derived artifact key (files/<assetId>/...)', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
id: 'ast-123',
} as unknown as Asset)
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(s3Service.downloadToFile).mockImplementation(async (_b, _k, dest) => {
await fs.promises.writeFile(dest, 'fake-screenshot-bytes')
})
vi.mocked(s3Service.headObject).mockResolvedValue({
contentType: 'image/webp',
} as unknown as Awaited<ReturnType<typeof s3Service.headObject>>)
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', {
assetId: null,
key: 'files/ast-123/screenshots/shot_5.0s.webp',
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'ast-123',
})
const expectedPath = path.join(piDir, 'shot_5.0s.webp')
createdFiles.push(expectedPath)
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
'shumai',
'files/ast-123/screenshots/shot_5.0s.webp',
expectedPath,
)
expect(fs.existsSync(expectedPath)).toBe(true)
expect(fs.readFileSync(expectedPath, 'utf-8')).toBe('fake-screenshot-bytes')
expect(result.details.filePath).toBe(path.join('.pi', 'shot_5.0s.webp'))
expect(result.details.key).toBe('files/ast-123/screenshots/shot_5.0s.webp')
expect(result.details.assetId).toBe('ast-123')
expect(result.details.size).toBe(Buffer.from('fake-screenshot-bytes').length)
})
it('should authorize and download original upload key (files/<uploadUlid>/...) via StorageKey', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findFirst).mockResolvedValue({
id: 'sk-1',
key: 'files/upload-ulid-456/photo.png',
assets: [{ id: 'asset-photo-1' }],
} as unknown as StorageKey & { assets: Asset[] })
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(s3Service.downloadToFile).mockImplementation(async (_b, _k, dest) => {
await fs.promises.writeFile(dest, 'fake-original-photo-bytes')
})
vi.mocked(s3Service.headObject).mockResolvedValue({
contentType: 'image/png',
} as unknown as Awaited<ReturnType<typeof s3Service.headObject>>)
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', {
assetId: null,
key: 'files/upload-ulid-456/photo.png',
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'Read',
type: 'asset',
id: 'asset-photo-1',
})
const expectedPath = path.join(piDir, 'photo.png')
createdFiles.push(expectedPath)
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
'shumai',
'files/upload-ulid-456/photo.png',
expectedPath,
)
expect(fs.existsSync(expectedPath)).toBe(true)
expect(result.details.assetId).toBe('asset-photo-1')
})
it('should fall back to file MIME detection when headObject returns generic application/octet-stream', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
vi.mocked(prisma.storageKey.findFirst).mockResolvedValue({
id: 'sk-2',
key: 'files/upload-ulid-789/graphic.png',
assets: [{ id: 'asset-graphic-1' }],
} as unknown as StorageKey & { assets: Asset[] })
vi.mocked(authzService.hasPermission).mockResolvedValue()
vi.mocked(s3Service.downloadToFile).mockImplementation(async (_b, _k, dest) => {
const pngHeader = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
await fs.promises.writeFile(dest, pngHeader)
})
vi.mocked(s3Service.headObject).mockResolvedValue({
contentType: 'application/octet-stream',
} as unknown as Awaited<ReturnType<typeof s3Service.headObject>>)
const tool = createDownloadAssetTool('user-1')
const result = await tool.execute('call-1', {
assetId: null,
key: 'files/upload-ulid-789/graphic.png',
})
const expectedPath = path.join(piDir, 'graphic.png')
createdFiles.push(expectedPath)
expect(result.details.contentType).toBe('image/png')
})
})
describe('resolveAssetIdFromKey helper unit tests', () => {
it('resolves directly by asset ID', async () => {
vi.mocked(prisma.asset.findUnique).mockResolvedValue({ id: 'ast-999' } as unknown as Asset)
+2 -200
View File
@@ -1,32 +1,8 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
// Storage-key helpers shared by download_asset (operation-extensions) and the media generation tools.
import { prisma, type User } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
import { getFileMimeType, readFileMimeType } from '@shumai/core/src/utils/file-mime'
import * as fs from 'fs'
import * as path from 'path'
export const downloadAssetSchema = Type.Object(
{
assetId: Type.Union([
Type.String({
description:
'The asset ID of the workspace asset to download to local disk, or null if providing "key".',
}),
Type.Null(),
]),
key: Type.Union([
Type.String({
description:
'The storage S3 key of a specific media artifact (e.g. a screenshot or transcode key from read_asset) to download to local disk, or null if providing "assetId".',
}),
Type.Null(),
]),
},
{ additionalProperties: false },
)
import { getFileMimeType } from '@shumai/core/src/utils/file-mime'
export async function resolveAssetIdFromKey(key: string): Promise<string> {
if (!key.startsWith('files/') || key.includes('..')) {
@@ -118,177 +94,3 @@ export async function resolveS3MediaBuffer(
return { buffer, mimeType }
}
export function createDownloadAssetTool(userId: string): AgentTool<typeof downloadAssetSchema> {
return {
name: 'download_asset',
label: 'Download Asset',
description:
'Downloads a workspace asset or specific storage S3 key to the local .pi directory for processing with local scripts or bash commands. ' +
'Provide exactly one of "assetId" (for full assets) or "key" (for intermediate artifacts like screenshots from read_asset). ' +
'Remember to delete the temporary downloaded file when finished.',
parameters: downloadAssetSchema,
execute: async (_toolCallId, params) => {
const { assetId, key } = params
if (!userId) {
throw new Error('User ID is required for authorization.')
}
const hasAssetId = assetId !== null
const hasKey = key !== null
if (hasAssetId === hasKey) {
throw new Error(
'Provide exactly one of "assetId" (to download a workspace asset) or "key" (to download a specific S3 storage key). Set the unused parameter to null.',
)
}
const piDir = path.join(process.cwd(), '.pi')
if (!fs.existsSync(piDir)) {
fs.mkdirSync(piDir, { recursive: true })
}
const bucket = process.env.S3_BUCKET || 'shumai'
// Branch A: Download by assetId
if (assetId) {
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: assetId,
})
let asset = await prisma.asset.findUnique({
where: { id: assetId },
include: { storageKey: true },
})
if (!asset) {
throw new Error(`Asset with ID ${assetId} not found.`)
}
if (asset.type === 'version_stack') {
const latestVersion = await prisma.asset.findFirst({
where: { parentId: asset.id, isDeleted: false },
orderBy: { sortIndex: 'asc' },
include: { storageKey: true },
})
if (latestVersion) {
asset = latestVersion
}
}
let mediaKey = asset.storageKey?.key
if (asset.media) {
const mediaInfo = asset.media as unknown as PrismaJson.MediaInfo
if (
mediaInfo.proxyType === 'image' ||
(mediaInfo.imageTranscodes && mediaInfo.imageTranscodes.length > 0)
) {
if (mediaInfo.imageTranscodes && mediaInfo.imageTranscodes.length > 0) {
mediaKey = mediaInfo.imageTranscodes[0].key || mediaKey
}
} else if (
mediaInfo.proxyType === 'video' ||
(mediaInfo.videoTranscodes && mediaInfo.videoTranscodes.length > 0) ||
mediaInfo.videoPreview?.key
) {
if (mediaInfo.videoTranscodes && mediaInfo.videoTranscodes.length > 0) {
mediaKey = mediaInfo.videoTranscodes[0].key || mediaKey
} else if (mediaInfo.videoPreview?.key) {
mediaKey = mediaInfo.videoPreview.key
}
} else if (mediaInfo.original?.key) {
mediaKey = mediaInfo.original.key || mediaKey
}
}
if (!mediaKey) {
throw new Error(`No media content found for asset ${assetId}.`)
}
const safeName = sanitizeFilename(asset.name || 'file')
const filename = `${asset.id}_${safeName}`
const targetFilePath = path.join(piDir, filename)
const relativePath = path.join('.pi', filename)
const objectInfo = await s3Service.headObject(bucket, mediaKey).catch(() => null)
await s3Service.downloadToFile(bucket, mediaKey, targetFilePath)
const stat = await fs.promises
.stat(targetFilePath)
.catch(() => ({ size: objectInfo?.size ?? 0 }))
const contentType =
objectInfo?.contentType && objectInfo.contentType !== 'application/octet-stream'
? objectInfo.contentType
: readFileMimeType(targetFilePath)
return {
content: [
{
type: 'text',
text: `Downloaded asset "${asset.name}" (ID: ${assetId}) to "${relativePath}". Remember to delete this temporary file when finished.`,
},
],
details: {
assetId,
name: asset.name,
filePath: relativePath,
absolutePath: targetFilePath,
contentType,
size: stat.size,
},
}
}
// Branch B: Download by S3 storage key
if (key) {
const owningAssetId = await resolveAssetIdFromKey(key)
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: owningAssetId,
})
const rawBasename = path.basename(key)
const safeBasename = sanitizeFilename(rawBasename) || 'downloaded_file'
const filename = safeBasename
const targetFilePath = path.join(piDir, filename)
const relativePath = path.join('.pi', filename)
const objectInfo = await s3Service.headObject(bucket, key).catch(() => null)
await s3Service.downloadToFile(bucket, key, targetFilePath)
const stat = await fs.promises
.stat(targetFilePath)
.catch(() => ({ size: objectInfo?.size ?? 0 }))
const contentType =
objectInfo?.contentType && objectInfo.contentType !== 'application/octet-stream'
? objectInfo.contentType
: readFileMimeType(targetFilePath)
return {
content: [
{
type: 'text',
text: `Downloaded storage key "${key}" to "${relativePath}". Remember to delete this temporary file when finished.`,
},
],
details: {
key,
assetId: owningAssetId,
filePath: relativePath,
absolutePath: targetFilePath,
contentType,
size: stat.size,
},
}
}
throw new Error('Invalid download_asset parameters.')
},
}
}
@@ -1,79 +0,0 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createListAssetsTool } from './list-assets'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService } from '@shumai/core/src/authz/authz'
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
listChildren: vi.fn().mockResolvedValue({
data: [
{ id: '01M2FILE2', name: 'file2.txt', type: 'file', sizeByte: 200 },
{ id: '01M1FILE1', name: 'file1.txt', type: 'file', sizeByte: 100 },
],
pageInfo: { hasNextPage: false, hasPreviousPage: false, cursor: 'abc' },
}),
},
}))
describe('createListAssetsTool', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('checks read permissions and invokes assetService.listChildren with default all and id desc ordering', async () => {
const tool = createListAssetsTool('user-1')
const result = await tool.execute('call-1', { parent: 'folder-1' })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'read',
type: 'asset',
id: 'folder-1',
})
expect(assetService.listChildren).toHaveBeenCalledWith({
assetId: 'folder-1',
assetType: 'all',
sort: 'createdAt',
order: 'desc',
first: 20,
after: undefined,
})
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
assets: [
{ id: '01M2FILE2', name: 'file2.txt', type: 'file', size: 200 },
{ id: '01M1FILE1', name: 'file1.txt', type: 'file', size: 100 },
],
pageInfo: { hasNextPage: false, hasPreviousPage: false, cursor: 'abc' },
})
})
it('passes explicit pagination and type filter options to assetService.listChildren', async () => {
const tool = createListAssetsTool('user-1')
await tool.execute('call-1', {
parent: 'folder-1',
page: 2,
pageSize: 10,
type: 'folder',
})
expect(assetService.listChildren).toHaveBeenCalledWith({
assetId: 'folder-1',
assetType: 'folder',
sort: 'createdAt',
order: 'desc',
first: 10,
after: expect.any(String),
})
})
})
-77
View File
@@ -1,77 +0,0 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { encodeCursor } from '@shumai/core/src/pagination'
import { type User } from '@shumai/db'
const listAssetsSchema = Type.Object({
parent: Type.String({
description: 'The parent folder ID to list assets from. This parameter is required.',
}),
page: Type.Optional(
Type.Number({
description: 'The page number to retrieve, default is 1.',
default: 1,
}),
),
pageSize: Type.Optional(
Type.Number({
description: 'The number of assets per page, default is 20.',
default: 20,
}),
),
type: Type.Optional(
Type.Union([Type.Literal('file'), Type.Literal('folder'), Type.Literal('all')], {
description: "Filter assets by type: 'file', 'folder', or 'all'. Default is 'all'.",
default: 'all',
}),
),
})
export function createListAssetsTool(userId: string): AgentTool<typeof listAssetsSchema> {
return {
name: 'list_assets',
label: 'List Assets',
description: 'List the assets (files and folders) inside a parent folder with pagination.',
parameters: listAssetsSchema,
execute: async (_toolCallId, params) => {
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: params.parent,
})
const page = params.page || 1
const pageSize = params.pageSize || 20
const type = params.type || 'all'
const result = await assetService.listChildren({
assetId: params.parent,
assetType: type,
sort: 'createdAt',
order: 'desc',
first: pageSize,
after: page > 1 ? encodeCursor((page - 1) * pageSize) : undefined,
})
const assets = result.data.map((a) => ({
id: a.id,
name: a.name,
type: a.type,
size: Number(a.sizeByte),
}))
const output = {
assets,
pageInfo: result.pageInfo,
}
return {
content: [{ type: 'text', text: JSON.stringify(output, null, 2) }],
details: {},
}
},
}
}
@@ -1,118 +0,0 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createMoveAssetsTool } from './move-assets'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService } from '@shumai/core/src/authz/authz'
import { AuditAction } from '@shumai/dtos'
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
reparentAssets: vi.fn().mockResolvedValue(undefined),
getAssetContext: vi.fn().mockResolvedValue({
teamId: 'team-1',
projectId: 'proj-1',
}),
},
}))
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
auditLogService: {
logAction: vi.fn().mockResolvedValue(undefined),
},
}))
describe('createMoveAssetsTool', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('checks permissions on destination and all assets, and calls reparentAssets', async () => {
const tool = createMoveAssetsTool('user-1')
const result = await tool.execute('call-1', {
assetIds: ['asset-1', 'asset-2'],
newParentId: 'folder-dest',
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'edit',
type: 'asset',
id: 'folder-dest',
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'edit',
type: 'asset',
id: 'asset-1',
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'edit',
type: 'asset',
id: 'asset-2',
})
expect(assetService.reparentAssets).toHaveBeenCalledWith({
assetIds: ['asset-1', 'asset-2'],
newParentId: 'folder-dest',
creatorId: 'user-1',
})
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
success: true,
movedAssetIds: ['asset-1', 'asset-2'],
newParentId: 'folder-dest',
})
})
it('logs asset_reparent for each asset when agentContext is provided', async () => {
const tool = createMoveAssetsTool('user-1', { teamId: 'team-1', agentId: 'agent-1' })
await tool.execute('call-1', {
assetIds: ['asset-1', 'asset-2'],
newParentId: 'folder-dest',
})
expect(auditLogService.logAction).toHaveBeenCalledTimes(2)
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: AuditAction.asset_reparent,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: 'proj-1',
itemId: 'asset-1',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: AuditAction.asset_reparent,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: 'proj-1',
itemId: 'asset-2',
})
})
it('fails when permission check on target folder fails', async () => {
vi.mocked(authzService.hasPermission).mockRejectedValueOnce(
new Error('Access denied to destination'),
)
const tool = createMoveAssetsTool('user-1')
await expect(
tool.execute('call-1', {
assetIds: ['asset-1'],
newParentId: 'folder-dest',
}),
).rejects.toThrow('Access denied to destination')
})
})
-84
View File
@@ -1,84 +0,0 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { type User } from '@shumai/db'
import { AuditAction } from '@shumai/dtos'
const moveAssetsSchema = Type.Object({
assetIds: Type.Array(Type.String(), {
description:
'The IDs of the assets to move. All assets must belong to the same parent folder. At least one asset ID is required.',
minItems: 1,
}),
newParentId: Type.String({
description:
'The destination parent folder ID to move the assets into. This parameter is required.',
}),
})
export function createMoveAssetsTool(
userId: string,
agentContext?: { teamId?: string; agentId?: string },
): AgentTool<typeof moveAssetsSchema> {
return {
name: 'move_assets',
label: 'Move Assets',
description:
'Move one or more assets (files or folders) to a specified parent folder. Note: All assets to be moved must belong to the same current parent folder.',
parameters: moveAssetsSchema,
execute: async (_toolCallId, params) => {
// Check Edit permission on destination folder
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Edit,
type: ResourceType.Asset,
id: params.newParentId,
})
// Check Edit permission on all source assets
for (const assetId of params.assetIds) {
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Edit,
type: ResourceType.Asset,
id: assetId,
})
}
await assetService.reparentAssets({
assetIds: params.assetIds,
newParentId: params.newParentId,
creatorId: userId,
})
if (agentContext?.teamId) {
const ctx = await assetService
.getAssetContext(params.newParentId)
.catch(() => ({ teamId: agentContext.teamId!, projectId: undefined }))
for (const assetId of params.assetIds) {
await auditLogService.logAction({
action: AuditAction.asset_reparent,
teamId: agentContext.teamId || ctx.teamId,
userId,
agentId: agentContext.agentId,
projectId: ctx.projectId,
itemId: assetId,
})
}
}
const result = {
success: true,
movedAssetIds: params.assetIds,
newParentId: params.newParentId,
}
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
details: {},
}
},
}
}
File diff suppressed because it is too large Load Diff
-924
View File
@@ -1,924 +0,0 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { type ImageContent, type TextContent } from '@earendil-works/pi-ai'
import { prisma, WorkflowTaskType, WorkflowTaskStatus, type User } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { workflowService } from '@shumai/workflow-core'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { resolveAnnotationsById } from './annotation-resolver'
import { getFileMimeType } from '@shumai/core/src/utils/file-mime'
import { is3dModel } from '@shumai/core/src/utils/mime'
import {
isValidTurntableDegree,
secondToDegree,
degreeToSecond,
} from '@shumai/core/src/utils/model3d'
export const readAssetSchema = Type.Object(
{
assetId: Type.String({
description: 'The asset ID of the workspace asset to inspect. Required.',
}),
annotationId: Type.Union([
Type.String({
description:
'The ID of the comment or message entry from <annotation id="..." /> whose visual markup should be overlaid, or null.',
}),
Type.Null(),
]),
s3KeyOnly: Type.Optional(
Type.Boolean({
description:
'When true, returns only the storage S3 key(s) and MIME types of the asset/frames without attaching base64 image or text content to the agent context. Use this to obtain S3 keys for generate_image or generate_video references without consuming context tokens.',
default: false,
}),
),
imageConfig: Type.Union([
Type.Object(
{},
{
additionalProperties: false,
description: 'Optional configuration for image assets, or null.',
},
),
Type.Null(),
]),
config3d: Type.Optional(
Type.Union([
Type.Object(
{
degree: Type.Optional(
Type.Union([
Type.Integer({
minimum: 0,
maximum: 345,
description:
'Turntable rotation degree (integer: 0, 15, 30, ..., 345 in 15° increments) to inspect a specific angle of a 3D model. Leave null/omitted to view the 4-view composite overview.',
}),
Type.Null(),
]),
),
},
{
additionalProperties: false,
description:
'Configuration for 3D model assets. Pass null if inspecting another asset type. Optionally pass degree to inspect a specific turntable angle, or leave null/omitted to view the 4-view composite overview.',
},
),
Type.Null(),
]),
),
videoConfig: Type.Union([
Type.Object(
{
start: Type.Number({ description: 'Start time in seconds.' }),
end: Type.Number({ description: 'End time in seconds.' }),
count: Type.Number({ description: 'Number of frames to extract (e.g. 1 to 10).' }),
},
{
additionalProperties: false,
description:
'Configuration for video assets. Required when inspecting videos, or null for other asset types.',
},
),
Type.Null(),
]),
docConfig: Type.Union([
Type.Object(
{
mode: Type.Union([
Type.Literal('pages', {
description:
'Render specific page range as images for visual layout, charts, or visual annotations (required for binary PDFs).',
}),
Type.Literal('text', {
description:
'Read raw text content directly (ideal for .md, .txt, .csv, code files).',
}),
]),
startPage: Type.Union([
Type.Number({
description:
'Start page number (1-based index, required when mode="pages"), or null when mode="text".',
}),
Type.Null(),
]),
endPage: Type.Union([
Type.Number({
description:
'End page number (1-based index, max 20 pages per call, required when mode="pages"), or null when mode="text".',
}),
Type.Null(),
]),
},
{
additionalProperties: false,
description:
'Configuration for document/PDF/text assets, or null if inspecting another asset type.',
},
),
Type.Null(),
]),
},
{ additionalProperties: false },
)
export function isPlainTextAsset(mediaType: string, filename: string): boolean {
if (
mediaType.startsWith('text/') ||
mediaType === 'application/json' ||
mediaType === 'application/javascript' ||
mediaType === 'application/typescript' ||
mediaType === 'application/xml' ||
mediaType === 'application/x-yaml'
) {
return true
}
const textExtensions = [
'.txt',
'.md',
'.markdown',
'.json',
'.csv',
'.tsv',
'.js',
'.ts',
'.tsx',
'.jsx',
'.html',
'.htm',
'.css',
'.scss',
'.yaml',
'.yml',
'.xml',
'.py',
'.rb',
'.go',
'.rs',
'.c',
'.cpp',
'.h',
'.hpp',
'.java',
'.sh',
'.bash',
'.zsh',
'.sql',
]
return textExtensions.some((ext) => filename.endsWith(ext))
}
const MAX_TEXT_BYTES = 50 * 1024
const MAX_TEXT_LINES = 1000
export type ReadAssetAuthContext =
| {
userId?: string
teamId?: string
agentType?: 'chat' | 'embedding'
targetAssetId?: string
}
| {
userId?: undefined
teamId: string
agentType: 'autofill'
targetAssetId: string
}
export function createReadAssetTool(
auth: string | ReadAssetAuthContext,
): AgentTool<typeof readAssetSchema> {
const authContext: ReadAssetAuthContext =
typeof auth === 'string' ? { userId: auth, agentType: 'chat' } : auth
const { userId, teamId, agentType = 'chat', targetAssetId } = authContext
return {
name: 'read_asset',
label: 'Read Asset',
description:
'Inspects and reads the content of an asset in the workspace. ' +
'Supports visual analysis for images, 3D models (overview or specific degree angle), video frame extraction, PDF page rendering, and direct text reading for text/markdown files. ' +
'Pass assetId, and provide the specific config (imageConfig, config3d, videoConfig, or docConfig) while setting unused configs to null. ' +
'Optionally provide annotationId to view visual markups drawn on the asset.',
parameters: readAssetSchema,
execute: async (_toolCallId, params) => {
const { assetId, annotationId, s3KeyOnly, config3d, videoConfig, docConfig } = params
if (agentType === 'autofill' && !userId) {
if (!targetAssetId) {
throw new Error('Target asset ID is required for autofill agent authorization.')
}
if (!teamId) {
throw new Error('Team ID is required for autofill agent authorization.')
}
// System background execution for Autofill Agent
const assetRecord = await prisma.asset.findUnique({
where: { id: assetId },
include: { project: true, teamRootFolder: true },
})
if (!assetRecord) {
throw new Error(`Asset with ID ${assetId} not found.`)
}
// Strict team boundary check
const assetTeamId = assetRecord.project?.teamId || assetRecord.teamRootFolder?.id
if (!assetTeamId || assetTeamId !== teamId) {
throw new Error(`Access denied: asset ${assetId} does not belong to team ${teamId}.`)
}
// Strict target asset boundary check (allows the target asset or its version stack children)
if (assetRecord.id !== targetAssetId && assetRecord.parentId !== targetAssetId) {
throw new Error(
`Access denied: autofill agent can only read target asset ${targetAssetId}.`,
)
}
} else {
// Chat agents or user-driven executions strictly require a userId and full ACL validation
if (!userId) {
throw new Error('User ID is required for authorization.')
}
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: assetId,
})
}
let asset = await prisma.asset.findUnique({
where: { id: assetId },
include: { storageKey: true },
})
if (!asset) {
throw new Error(`Asset with ID ${assetId} not found.`)
}
if (asset.type === 'version_stack') {
const latestVersion = await prisma.asset.findFirst({
where: { parentId: asset.id, isDeleted: false },
orderBy: { sortIndex: 'asc' },
include: { storageKey: true },
})
if (latestVersion) {
asset = latestVersion
}
}
const mediaInfo = asset.media as unknown as PrismaJson.MediaInfo | null
const proxyType = mediaInfo?.proxyType
const mediaType = asset.mediaType?.toLowerCase() || ''
const filename = asset.name?.toLowerCase() || ''
const bucket = process.env.S3_BUCKET || 'shumai'
// ----------------------------------------------------------------------
// Branch 0: 3D Assets (Orthogonal 4-View Composite Image or Degree Angle)
// ----------------------------------------------------------------------
const is3d = proxyType === '3d' || is3dModel(mediaType, filename)
if (is3d) {
if (videoConfig) {
throw new Error(
`Asset ${asset.id} is a 3D model. To inspect specific angles pass 'config3d: { degree: ... }'; do not pass videoConfig.`,
)
}
if (config3d?.degree !== undefined && config3d?.degree !== null) {
if (!isValidTurntableDegree(config3d.degree)) {
throw new Error(
`Invalid turntable degree: ${config3d.degree}. Degree must be an integer multiple of 15 between 0 and 345 (e.g. 0, 15, 30, 45, ..., 345).`,
)
}
}
let annotations: PrismaJson.AnnotationList | null = null
let commentTimestamp: number | null = null
if (annotationId) {
const resolved = await resolveAnnotationsById(asset.id, annotationId)
annotations = resolved.annotations
commentTimestamp = resolved.timestamp
if (commentTimestamp !== null && commentTimestamp !== undefined) {
const commentDegree = secondToDegree(commentTimestamp)
if (
config3d?.degree !== undefined &&
config3d?.degree !== null &&
config3d.degree !== commentDegree
) {
throw new Error(
`Annotation "${annotationId}" was drawn at ${commentDegree}°, but degree ${config3d.degree}° was specified. Do not pass a conflicting degree with annotationId.`,
)
}
}
}
if ((config3d?.degree !== undefined && config3d?.degree !== null) || annotationId) {
if (!mediaInfo?.videoTranscodes || mediaInfo.videoTranscodes.length === 0) {
throw new Error(
`Asset ${asset.id} is a 3D model, but its turntable video (turntable.mp4) is missing.`,
)
}
let targetDegree: number
let targetSecond: number
if (config3d?.degree !== undefined && config3d?.degree !== null) {
targetDegree = config3d.degree
targetSecond = degreeToSecond(targetDegree)
} else {
targetSecond =
commentTimestamp !== null && commentTimestamp !== undefined ? commentTimestamp : 0
targetDegree = secondToDegree(targetSecond)
}
const task = await prisma.workflowTask.create({
data: {
assetId: asset.id,
projectId: asset.projectId || 'none',
type: WorkflowTaskType.transcode_screenshot,
status: WorkflowTaskStatus.pending,
payload: {
projectId: asset.projectId || 'none',
screenshot: {
start: targetSecond,
end: targetSecond,
count: 1,
commentTimestamp: targetSecond,
annotations: annotations ?? undefined,
},
},
},
})
const completedTask = await workflowService.executeWait(task)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- transcode screenshot output
const output = completedTask.output as any
const screenshots = output?.screenshots as
| Array<{ key: string; timestamp: number }>
| undefined
if (!screenshots || screenshots.length === 0) {
throw new Error(
`Failed to extract 3D turntable frame for asset ${asset.id} at ${targetDegree}°.`,
)
}
const shotKey = screenshots[0].key
const downloadUrl = await s3Service.presign(
bucket,
shotKey,
'GET',
true,
asset.name || undefined,
)
const content: Array<TextContent | ImageContent> = []
const hasMarkup = Boolean(annotations && annotations.length > 0)
const textDescription = hasMarkup
? `3D asset "${asset.name}" (ID: ${asset.id}, S3 Key: "${shotKey}") view at ${targetDegree}° with visual markups overlaid:`
: `3D asset "${asset.name}" (ID: ${asset.id}, S3 Key: "${shotKey}") view at ${targetDegree}°:`
if (s3KeyOnly) {
content.push({
type: 'text',
text: textDescription,
})
} else {
const { buffer, contentType } = await s3Service.getObject(bucket, shotKey)
const actualMimeType =
contentType && contentType !== 'application/octet-stream' ? contentType : 'image/webp'
content.push({
type: 'text',
text: textDescription,
})
content.push({
type: 'image',
data: buffer.toString('base64'),
mimeType: actualMimeType,
})
}
return {
content,
details: {
assetId: asset.id,
name: asset.name,
mediaType: asset.mediaType,
key: shotKey,
downloadUrl,
sourceKeys: [shotKey],
},
}
}
if (!mediaInfo?.imageTranscodes || mediaInfo.imageTranscodes.length === 0) {
throw new Error(
`Asset ${asset.id} is a 3D model, but its 4-views image (views.webp) is missing.`,
)
}
const mediaKey = mediaInfo.imageTranscodes[0].key
if (!mediaKey) {
throw new Error(`Asset ${asset.id} is a 3D model, but its 4-views image key is missing.`)
}
const keyToUse = mediaKey
const mimeType = 'image/webp'
const downloadUrl = await s3Service.presign(
bucket,
keyToUse,
'GET',
true,
asset.name || undefined,
)
const content: Array<TextContent | ImageContent> = []
const textDescription = [
`3D asset "${asset.name}" (ID: ${asset.id}, MIME: ${mimeType}, S3 Key: "${keyToUse}"):`,
'Orthogonal 4-view composite image (2x2 grid across 360° rotation):',
'- Top-Left quadrant: Front view (0°)',
'- Top-Right quadrant: Right side view (90°)',
'- Bottom-Left quadrant: Back view (180°)',
'- Bottom-Right quadrant: Left side view (270°)',
].join('\n')
if (s3KeyOnly) {
content.push({
type: 'text',
text: textDescription,
})
} else {
const { buffer, contentType } = await s3Service.getObject(bucket, keyToUse)
const actualMimeType =
contentType && contentType !== 'application/octet-stream' ? contentType : mimeType
content.push({
type: 'text',
text: textDescription,
})
content.push({
type: 'image',
data: buffer.toString('base64'),
mimeType: actualMimeType,
})
}
return {
content,
details: {
assetId: asset.id,
name: asset.name,
mediaType: asset.mediaType,
key: keyToUse,
downloadUrl,
sourceKeys: [keyToUse],
},
}
}
// ----------------------------------------------------------------------
// Branch 1: Image Assets
// ----------------------------------------------------------------------
if (proxyType === 'image' || mediaType.startsWith('image/') || filename.endsWith('.psd')) {
let mediaKey = asset.storageKey?.key
if (mediaInfo?.imageTranscodes && mediaInfo.imageTranscodes.length > 0) {
mediaKey = mediaInfo.imageTranscodes[0].key || mediaKey
}
if (!mediaKey) {
throw new Error('No media content found for this asset.')
}
let keyToUse = mediaKey
let mimeType = getFileMimeType(null, keyToUse, asset.mediaType || 'image/png')
if (annotationId) {
const { annotations } = await resolveAnnotationsById(asset.id, annotationId)
if (annotations && annotations.length > 0) {
const task = await prisma.workflowTask.create({
data: {
assetId: asset.id,
projectId: asset.projectId || 'none',
type: WorkflowTaskType.transcode_image_annotation,
status: WorkflowTaskStatus.pending,
payload: {
projectId: asset.projectId || 'none',
imageAnnotation: {
annotations,
},
},
},
})
const completedTask = await workflowService.executeWait(task)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- transcode image annotation task output
const output = completedTask.output as any
if (output?.key) {
keyToUse = output.key
mimeType = 'image/webp'
} else {
throw new Error('Image annotation transcode workflow failed to output S3 key.')
}
}
}
const downloadUrl = await s3Service.presign(
bucket,
keyToUse,
'GET',
true,
asset.name || undefined,
)
const content: Array<TextContent | ImageContent> = []
if (s3KeyOnly) {
content.push({
type: 'text',
text: `Image asset "${asset.name}" (ID: ${asset.id}, MIME: ${mimeType}, S3 Key: "${keyToUse}")`,
})
} else {
const { buffer, contentType } = await s3Service.getObject(bucket, keyToUse)
const actualMimeType =
contentType && contentType !== 'application/octet-stream' ? contentType : mimeType
content.push({
type: 'text',
text: `Image asset "${asset.name}" (ID: ${asset.id}, MIME: ${actualMimeType}, S3 Key: "${keyToUse}")`,
})
content.push({
type: 'image',
data: buffer.toString('base64'),
mimeType: actualMimeType,
})
}
return {
content,
details: {
assetId: asset.id,
name: asset.name,
mediaType: asset.mediaType,
key: keyToUse,
downloadUrl,
sourceKeys: [keyToUse],
},
}
}
// ----------------------------------------------------------------------
// Branch 2: Video Assets
// ----------------------------------------------------------------------
if (proxyType === 'video' || mediaType.startsWith('video/')) {
if (!videoConfig) {
throw new Error(
'videoConfig with start, end, and count is required when inspecting video assets.',
)
}
const { start, end, count } = videoConfig
if (start < 0 || end < 0) {
throw new Error('Invalid video time range: start and end must be non-negative numbers.')
}
if (start > end) {
throw new Error(
`Invalid video time range: start (${start}) must be less than or equal to end (${end}).`,
)
}
if (count < 1) {
throw new Error(`Invalid frame count: count (${count}) must be at least 1.`)
}
const { annotations, timestamp: commentTimestamp } = await resolveAnnotationsById(
asset.id,
annotationId,
)
const task = await prisma.workflowTask.create({
data: {
assetId: asset.id,
projectId: asset.projectId || 'none',
type: WorkflowTaskType.transcode_screenshot,
status: WorkflowTaskStatus.pending,
payload: {
projectId: asset.projectId || 'none',
screenshot: {
start,
end,
count,
commentTimestamp,
annotations,
},
},
},
})
const completedTask = await workflowService.executeWait(task)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- transcode screenshot output
const output = completedTask.output as any
const screenshots = output?.screenshots as
| Array<{ key: string; timestamp: number }>
| undefined
if (!screenshots || screenshots.length === 0) {
throw new Error('No screenshots were generated.')
}
const content: Array<TextContent | ImageContent> = []
const sourceKeys: string[] = []
const results: Array<{ key: string; timestamp: number; downloadUrl: string }> = []
const textLines: string[] = [
`Extracted ${screenshots.length} frame(s) from "${asset.name}" (${start}s - ${end}s):`,
]
for (const shot of screenshots) {
const downloadUrl = await s3Service.presign(
bucket,
shot.key,
'GET',
true,
asset.name || undefined,
)
sourceKeys.push(shot.key)
results.push({
key: shot.key,
timestamp: shot.timestamp,
downloadUrl,
})
if (s3KeyOnly) {
const mimeType = getFileMimeType(null, shot.key, 'image/webp')
textLines.push(
`- Frame at ${shot.timestamp}s (S3 Key: "${shot.key}", MIME: ${mimeType})`,
)
} else {
const { buffer, contentType } = await s3Service.getObject(bucket, shot.key)
const mimeType =
contentType && contentType !== 'application/octet-stream'
? contentType
: getFileMimeType(buffer, shot.key, 'image/webp')
textLines.push(
`- Frame at ${shot.timestamp}s (S3 Key: "${shot.key}", MIME: ${mimeType})`,
)
content.push({
type: 'image',
data: buffer.toString('base64'),
mimeType,
})
}
}
content.unshift({
type: 'text',
text: textLines.join('\n'),
})
return {
content,
details: {
assetId: asset.id,
name: asset.name,
mediaType: asset.mediaType,
keys: sourceKeys,
results,
sourceKeys,
},
}
}
// ----------------------------------------------------------------------
// Branch 3: Documents & PDF Assets
// ----------------------------------------------------------------------
if (
proxyType === 'pdf' ||
mediaType === 'application/pdf' ||
isPlainTextAsset(mediaType, filename)
) {
if (!docConfig) {
throw new Error(
'docConfig with mode ("pages" or "text") is required when inspecting document/PDF assets.',
)
}
if (docConfig.mode === 'text') {
if (annotationId !== null) {
throw new Error(
'annotationId cannot be used with docConfig mode "text" because visual markups can only be rendered on visual pages. Use docConfig: { mode: "pages", startPage: ..., endPage: ... } instead.',
)
}
if (docConfig.startPage !== null || docConfig.endPage !== null) {
throw new Error(
'startPage and endPage must be null when docConfig mode is "text". To inspect specific page numbers, use mode: "pages".',
)
}
if (!isPlainTextAsset(mediaType, filename)) {
throw new Error(
`Asset "${asset.name}" is a binary PDF/document and cannot be read as raw text. Please call read_asset with docConfig: { mode: "pages", startPage: 1, endPage: ... } to view its visual pages.`,
)
}
const mediaKey = asset.storageKey?.key
if (!mediaKey) {
throw new Error(`No media content found for asset ${asset.id}.`)
}
const docMimeType =
asset.mediaType || getFileMimeType(null, asset.name || mediaKey, 'text/plain')
const downloadUrl = await s3Service.presign(
bucket,
mediaKey,
'GET',
true,
asset.name || undefined,
)
if (s3KeyOnly) {
return {
content: [
{
type: 'text',
text: `Document "${asset.name}" (ID: ${asset.id}, MIME: ${docMimeType}, S3 Key: "${mediaKey}")`,
},
],
details: {
assetId: asset.id,
name: asset.name,
mediaType: asset.mediaType,
key: mediaKey,
downloadUrl,
sourceKeys: [mediaKey],
size: 0,
isTruncated: false,
},
}
}
const { buffer } = await s3Service.getObject(bucket, mediaKey)
const rawText = buffer.toString('utf-8')
let text = rawText
let isTruncated = false
const lines = text.split('\n')
if (lines.length > MAX_TEXT_LINES) {
text = lines.slice(0, MAX_TEXT_LINES).join('\n')
isTruncated = true
}
if (Buffer.byteLength(text, 'utf-8') > MAX_TEXT_BYTES) {
text = Buffer.from(text, 'utf-8').subarray(0, MAX_TEXT_BYTES).toString('utf-8')
isTruncated = true
}
if (isTruncated) {
text += `\n\n[Content truncated to ${MAX_TEXT_LINES} lines / ${MAX_TEXT_BYTES / 1024}KB limit. Use download_asset to process the full file.]`
}
return {
content: [
{
type: 'text',
text: `Document "${asset.name}" (ID: ${asset.id}, MIME: ${docMimeType}, S3 Key: "${mediaKey}"):\n\n${text}`,
},
],
details: {
assetId: asset.id,
name: asset.name,
mediaType: asset.mediaType,
key: mediaKey,
downloadUrl,
sourceKeys: [mediaKey],
size: buffer.length,
isTruncated,
},
}
}
// docConfig.mode === 'pages'
if (docConfig.startPage === null || docConfig.endPage === null) {
throw new Error('startPage and endPage are required when docConfig mode is "pages".')
}
const start = docConfig.startPage
const end = docConfig.endPage
if (start < 1) {
throw new Error(`Invalid page range: start page (${start}) must be at least 1.`)
}
if (start > end) {
throw new Error(
`Invalid page range: start page (${start}) must be less than or equal to end page (${end}).`,
)
}
const MAX_PAGE_RANGE = 20
const requestedPages = end - start + 1
if (requestedPages > MAX_PAGE_RANGE) {
throw new Error(
`Page range (${requestedPages} pages requested) exceeds the maximum limit of ${MAX_PAGE_RANGE} pages per request.`,
)
}
const { annotations, timestamp: commentTimestamp } = await resolveAnnotationsById(
asset.id,
annotationId,
)
const task = await prisma.workflowTask.create({
data: {
assetId: asset.id,
projectId: asset.projectId || 'none',
type: WorkflowTaskType.transcode_pdf_pages,
status: WorkflowTaskStatus.pending,
payload: {
projectId: asset.projectId || 'none',
pdfPages: {
start,
end,
commentTimestamp,
annotations,
},
},
},
})
const completedTask = await workflowService.executeWait(task)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- transcode pdf pages output
const output = completedTask.output as any
const pages = output?.pages as Array<{ key: string; page: number }> | undefined
if (!pages || pages.length === 0) {
throw new Error('No PDF page images were generated.')
}
const content: Array<TextContent | ImageContent> = []
const sourceKeys: string[] = []
const results: Array<{ key: string; page: number; downloadUrl: string }> = []
const textLines: string[] = [
`Rendered ${pages.length} page(s) of "${asset.name}" (Pages ${start} to ${end}):`,
]
for (const pageItem of pages) {
const downloadUrl = await s3Service.presign(
bucket,
pageItem.key,
'GET',
true,
asset.name || undefined,
)
sourceKeys.push(pageItem.key)
results.push({
key: pageItem.key,
page: pageItem.page,
downloadUrl,
})
if (s3KeyOnly) {
const mimeType = getFileMimeType(null, pageItem.key, 'image/webp')
textLines.push(`- Page ${pageItem.page} (S3 Key: "${pageItem.key}", MIME: ${mimeType})`)
} else {
const { buffer, contentType } = await s3Service.getObject(bucket, pageItem.key)
const mimeType =
contentType && contentType !== 'application/octet-stream'
? contentType
: getFileMimeType(buffer, pageItem.key, 'image/webp')
textLines.push(`- Page ${pageItem.page} (S3 Key: "${pageItem.key}", MIME: ${mimeType})`)
content.push({
type: 'image',
data: buffer.toString('base64'),
mimeType,
})
}
}
content.unshift({
type: 'text',
text: textLines.join('\n'),
})
return {
content,
details: {
assetId: asset.id,
name: asset.name,
mediaType: asset.mediaType,
keys: sourceKeys,
results,
sourceKeys,
},
}
}
throw new Error(
`Unsupported asset type for read_asset. Asset ${asset.id} has media type "${mediaType || 'unknown'}". Use download_asset to download this file to disk.`,
)
},
}
}
@@ -0,0 +1,91 @@
import { describe, it, expect } from 'vitest'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { createReadThreadTool } from './read-thread'
/**
* Real-database authorization tests for `read_thread`.
*
* Reproduces a bug where the tool returned any comment thread by ID without
* checking that the invoking user (or, for system agents, the agent's team)
* has access to the asset the thread belongs to.
*/
describe('read_thread authorization', () => {
setupTestDbHooks()
async function seedTeamWithThread(label: string) {
const team = await prisma.team.create({ data: { name: `Team ${label}` } })
const project = await prisma.project.create({
data: { name: `Project ${label}`, teamId: team.id },
})
const asset = await prisma.asset.create({
data: { name: `${label}.png`, type: 'file', status: 'uploaded', projectId: project.id },
})
const author = await prisma.user.create({
data: { name: `Author ${label}`, email: `author-${label}@test.com`, password: 'pw' },
})
await prisma.teamMember.create({
data: { teamId: team.id, userId: author.id, role: 'owner' },
})
const thread = await prisma.assetComment.create({
data: { assetId: asset.id, creatorId: author.id, message: `secret message ${label}` },
})
await prisma.assetComment.create({
data: {
assetId: asset.id,
creatorId: author.id,
replyToId: thread.id,
message: `secret reply ${label}`,
},
})
return { team, project, asset, author, thread }
}
it('lets a team member read a thread in their team', async () => {
const a = await seedTeamWithThread('a')
const tool = createReadThreadTool({ userId: a.author.id, teamId: a.team.id })
const result = await tool.execute('call-1', { threadId: a.thread.id })
const text = (result.content[0] as { text: string }).text
expect(text).toContain('secret message a')
expect(text).toContain('secret reply a')
})
it('rejects a user reading a thread from another team', async () => {
const a = await seedTeamWithThread('a')
const b = await seedTeamWithThread('b')
const tool = createReadThreadTool({ userId: a.author.id, teamId: a.team.id })
await expect(tool.execute('call-1', { threadId: b.thread.id })).rejects.toThrow(
'User is not a member of the team',
)
})
it('rejects a project-scoped member reading a thread in a project they cannot access', async () => {
const b = await seedTeamWithThread('b')
const outsider = await prisma.user.create({
data: { name: 'Project scoped', email: 'scoped@test.com', password: 'pw' },
})
await prisma.teamMember.create({
data: { teamId: b.team.id, userId: outsider.id, role: 'editor', scope: 'project' },
})
const tool = createReadThreadTool({ userId: outsider.id, teamId: b.team.id })
await expect(tool.execute('call-1', { threadId: b.thread.id })).rejects.toThrow()
})
it('confines a system agent (no user) to threads in its own team', async () => {
const a = await seedTeamWithThread('a')
const b = await seedTeamWithThread('b')
const tool = createReadThreadTool({ teamId: a.team.id })
const own = await tool.execute('call-1', { threadId: a.thread.id })
expect((own.content[0] as { text: string }).text).toContain('secret message a')
await expect(tool.execute('call-2', { threadId: b.thread.id })).rejects.toThrow('Access denied')
})
})
+23 -7
View File
@@ -1,6 +1,15 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createReadThreadTool } from './read-thread'
import { prisma } from '@shumai/db'
import { authzService } from '@shumai/core/src/authz/authz'
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/db', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shumai/db')>()
@@ -27,7 +36,7 @@ describe('createReadThreadTool', () => {
vi.mocked(prisma.assetComment.findUnique).mockResolvedValue(null)
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
const tool = createReadThreadTool()
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
const result = await tool.execute('call-1', { threadId: 'non-existent' })
const textContent = (result.content[0] as { text: string }).text
@@ -39,7 +48,7 @@ describe('createReadThreadTool', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mock return
vi.mocked(prisma.asset.findUnique).mockResolvedValue({ id: 'asset-1' } as any)
const tool = createReadThreadTool()
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
const result = await tool.execute('call-asset', { threadId: 'asset-1' })
const textContent = (result.content[0] as { text: string }).text
@@ -49,6 +58,7 @@ describe('createReadThreadTool', () => {
it('should return thread root message and notice when there are no replies', async () => {
vi.mocked(prisma.assetComment.findUnique).mockResolvedValue({
id: 'root-1',
assetId: 'asset-1',
message: 'Initial top-level question',
creator: { name: 'Alice' },
second: 12.5,
@@ -57,10 +67,16 @@ describe('createReadThreadTool', () => {
} as any)
vi.mocked(prisma.assetComment.findMany).mockResolvedValue([])
const tool = createReadThreadTool()
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
const result = await tool.execute('call-2', { threadId: 'root-1' })
const textContent = (result.content[0] as { text: string }).text
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'read',
type: 'asset',
id: 'asset-1',
})
expect(textContent).toContain(
'Thread Root [Alice] (id: root-1, time: 12.5s, has_markup: true): Initial top-level question',
)
@@ -90,7 +106,7 @@ describe('createReadThreadTool', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
] as any)
const tool = createReadThreadTool()
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
const result = await tool.execute('call-3', { threadId: 'root-1' })
const textContent = (result.content[0] as { text: string }).text
@@ -122,7 +138,7 @@ describe('createReadThreadTool', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
] as any)
const tool = createReadThreadTool()
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
const result = await tool.execute('call-agent', { threadId: 'root-1' })
const textContent = (result.content[0] as { text: string }).text
@@ -153,7 +169,7 @@ describe('createReadThreadTool', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
] as any)
const tool = createReadThreadTool()
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
const result = await tool.execute('call-3d', { threadId: 'root-3d' })
const textContent = (result.content[0] as { text: string }).text
@@ -166,7 +182,7 @@ describe('createReadThreadTool', () => {
})
it('should describe threadId parameter as referencing <thread id="..." /> in <context>', () => {
const tool = createReadThreadTool()
const tool = createReadThreadTool({ userId: 'user-1', teamId: 'team-1' })
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const description = (tool.parameters as any).properties.threadId.description
expect(description).toContain('<thread id="..." />')
+37 -5
View File
@@ -1,6 +1,8 @@
import { prisma } from '@shumai/db'
import { prisma, type User } from '@shumai/db'
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { secondToDegree } from '@shumai/core/src/utils/model3d'
import { getProxyType } from '@shumai/core/src/utils/mime'
@@ -32,10 +34,38 @@ function formatCommentMeta(
return `(${parts.join(', ')})`
}
export const createReadThreadTool = (): AgentTool<
typeof readThreadSchema,
{ threadId: string }
> => ({
export interface ReadThreadToolContext {
/** The user the agent acts for. When set, the user must be able to read the thread's asset. */
userId?: string
/** The agent's team. System agents without a user may only read threads in this team. */
teamId: string
}
/**
* Ensure the caller may read a thread on `assetId` before any thread content is
* returned: the acting user needs Read on the asset; a system agent (no user)
* is confined to its own team.
*/
async function assertCanReadThread(assetId: string, ctx: ReadThreadToolContext): Promise<void> {
if (ctx.userId) {
await authzService.hasPermission({
user: { id: ctx.userId } as User,
permission: Permission.Read,
type: ResourceType.Asset,
id: assetId,
})
return
}
const assetCtx = await assetService.getAssetContext(assetId).catch(() => null)
if (!assetCtx || assetCtx.teamId !== ctx.teamId) {
throw new Error('Access denied: this comment thread does not belong to your team.')
}
}
export const createReadThreadTool = (
ctx: ReadThreadToolContext,
): AgentTool<typeof readThreadSchema, { threadId: string }> => ({
name: 'read_thread',
label: 'Read Comment Thread',
description: 'Reads all comments and replies inside a specific comment thread.',
@@ -64,6 +94,8 @@ export const createReadThreadTool = (): AgentTool<
}
}
await assertCanReadThread(rootComment.assetId, ctx)
const replies = await prisma.assetComment.findMany({
where: {
replyToId: rootComment.id,
@@ -1,112 +0,0 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createRenameAssetTool } from './rename-asset'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService } from '@shumai/core/src/authz/authz'
import { AuditAction, type AssetInfo } from '@shumai/dtos'
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: { Edit: 'edit', Read: 'read' },
ResourceType: { Asset: 'asset' },
}))
vi.mock('@shumai/core/src/asset/asset', () => ({
assetService: {
updateAssetName: vi.fn().mockResolvedValue({
id: 'asset-1',
name: 'Renamed Asset',
type: 'file',
sizeByte: 1024,
projectId: 'proj-1',
}),
getAssetContext: vi.fn().mockResolvedValue({
teamId: 'team-1',
projectId: 'proj-1',
}),
},
}))
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
auditLogService: {
logAction: vi.fn().mockResolvedValue(undefined),
},
}))
describe('createRenameAssetTool', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('checks edit permission and calls assetService.updateAssetName', async () => {
const tool = createRenameAssetTool('user-1')
const result = await tool.execute('call-1', { assetId: 'asset-1', name: 'Renamed Asset' })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user-1' },
permission: 'edit',
type: 'asset',
id: 'asset-1',
})
expect(assetService.updateAssetName).toHaveBeenCalledWith({
id: 'asset-1',
name: 'Renamed Asset',
})
expect(result.details).toEqual({})
expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({
id: 'asset-1',
name: 'Renamed Asset',
type: 'file',
size: 1024,
})
})
it('logs file_update audit action when renaming a file with agentContext', async () => {
const tool = createRenameAssetTool('user-1', { teamId: 'team-1', agentId: 'agent-1' })
await tool.execute('call-1', { assetId: 'asset-1', name: 'Renamed File' })
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: AuditAction.file_update,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: 'proj-1',
itemId: 'asset-1',
})
})
it('logs folder_update audit action when renaming a folder with agentContext', async () => {
vi.mocked(assetService.updateAssetName).mockResolvedValueOnce({
id: 'folder-1',
name: 'Renamed Folder',
type: 'folder',
sizeByte: 0,
projectId: 'proj-1',
} as unknown as AssetInfo)
const tool = createRenameAssetTool('user-1', { teamId: 'team-1', agentId: 'agent-1' })
await tool.execute('call-1', { assetId: 'folder-1', name: 'Renamed Folder' })
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: AuditAction.folder_update,
teamId: 'team-1',
userId: 'user-1',
agentId: 'agent-1',
projectId: 'proj-1',
itemId: 'folder-1',
})
})
it('fails when permission check rejects', async () => {
vi.mocked(authzService.hasPermission).mockRejectedValueOnce(new Error('Permission denied'))
const tool = createRenameAssetTool('user-1')
await expect(tool.execute('call-1', { assetId: 'asset-1', name: 'New' })).rejects.toThrow(
'Permission denied',
)
})
})
-69
View File
@@ -1,69 +0,0 @@
import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { type User } from '@shumai/db'
import { AuditAction } from '@shumai/dtos'
const renameAssetSchema = Type.Object({
assetId: Type.String({
description: 'The ID of the asset (file or folder) to rename. This parameter is required.',
}),
name: Type.String({
description: 'The new name for the asset. This parameter is required.',
}),
})
export function createRenameAssetTool(
userId: string,
agentContext?: { teamId?: string; agentId?: string },
): AgentTool<typeof renameAssetSchema> {
return {
name: 'rename_asset',
label: 'Rename Asset',
description: 'Rename an existing asset (file or folder).',
parameters: renameAssetSchema,
execute: async (_toolCallId, params) => {
await authzService.hasPermission({
user: { id: userId } as User,
permission: Permission.Edit,
type: ResourceType.Asset,
id: params.assetId,
})
const updatedAsset = await assetService.updateAssetName({
id: params.assetId,
name: params.name,
})
if (agentContext?.teamId) {
const ctx = await assetService.getAssetContext(params.assetId).catch(() => ({
teamId: agentContext.teamId!,
projectId: updatedAsset.projectId ?? undefined,
}))
await auditLogService.logAction({
action:
updatedAsset.type === 'folder' ? AuditAction.folder_update : AuditAction.file_update,
teamId: agentContext.teamId || ctx.teamId,
userId,
agentId: agentContext.agentId,
projectId: ctx.projectId ?? updatedAsset.projectId ?? undefined,
itemId: params.assetId,
})
}
const result = {
id: updatedAsset.id,
name: updatedAsset.name,
type: updatedAsset.type,
size: Number(updatedAsset.sizeByte),
}
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
details: {},
}
},
}
}
+7 -1
View File
@@ -1,18 +1,24 @@
{
"name": "@shumai/api",
"version": "0.6.0",
"version": "0.6.2",
"private": true,
"type": "module",
"main": "./src/index.ts",
"types": "./src/index.ts",
"dependencies": {
"@better-auth/mcp": "1.7.12",
"@hono/zod-validator": "0.7.6",
"@modelcontextprotocol/server": "2.0.0",
"@shumai/core": "workspace:*",
"@shumai/db": "workspace:*",
"@shumai/dtos": "workspace:*",
"@shumai/operations": "workspace:*",
"hono": "4.13.5",
"pino": "10.3.1",
"ulid": "3.0.2",
"zod": "4.5.4"
},
"devDependencies": {
"@modelcontextprotocol/client": "2.0.0"
}
}
+72 -55
View File
@@ -1,5 +1,6 @@
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
import { Hono, type Context, type Next } from 'hono'
import { HTTPException } from 'hono/http-exception'
import fileRoute from './file'
import { assetService } from '@shumai/core/src/asset/asset'
import { metadataService } from '@shumai/core/src/metadata/metadata'
@@ -8,6 +9,12 @@ import { s3Service } from '@shumai/core/src/s3/s3'
import { transcodeService } from '@shumai/core'
import { authMiddleware } from './middleware/auth'
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
import {
renameAssetOp,
resolveCommentOp,
runOperation,
updateAssetMetadataOp,
} from '@shumai/core/src/operations'
vi.mock('fs', async (importOriginal) => {
const actual = (await importOriginal()) as Record<string, unknown>
@@ -55,6 +62,11 @@ vi.mock('./middleware/auth', () => ({
},
}))
vi.mock('@shumai/core/src/operations', async (importOriginal) => ({
...(await importOriginal<typeof import('@shumai/core/src/operations')>()),
runOperation: vi.fn(),
}))
describe('file api', () => {
beforeEach(() => {
vi.mocked(authzService.hasPermission).mockClear()
@@ -114,18 +126,9 @@ describe('file api', () => {
})
})
it('PUT /files/:fileId', async () => {
vi.mocked(assetService.updateAssetName).mockResolvedValue({
id: 'test-id',
name: 'new-name',
sizeByte: 10,
fileCount: 1,
type: 'file',
status: 'active',
proxyType: 'image',
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
})
it('PUT /files/:fileId runs rename_asset', async () => {
const summary = { id: 'test-id', name: 'new-name', type: 'file', size: 10, projectId: 'p1' }
vi.mocked(runOperation).mockResolvedValue(summary)
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/test-id', {
@@ -135,22 +138,12 @@ describe('file api', () => {
})
expect(res.status).toBe(200)
const json = await res.json()
expect(json.name).toBe('new-name')
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user1', name: 'Test User' },
permission: Permission.Edit,
type: ResourceType.Asset,
id: 'test-id',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'file_update',
teamId: 'test-team',
userId: 'user1',
projectId: undefined,
itemId: 'test-id',
})
expect(await res.json()).toEqual(summary)
expect(runOperation).toHaveBeenCalledWith(
renameAssetOp,
{ userId: 'user1', via: 'api' },
{ assetId: 'test-id', name: 'new-name' },
)
})
it('DELETE /files', async () => {
@@ -463,8 +456,9 @@ describe('file api', () => {
expect(resInvalidTz.status).toBe(400)
})
it('POST /comments/:commentId/complete', async () => {
vi.mocked(assetService.completeComment).mockResolvedValue({
it('POST /comments/:commentId/complete runs resolve_comment and returns the comment', async () => {
vi.mocked(runOperation).mockResolvedValue({ commentId: 'comment-id', resolved: true })
vi.mocked(assetService.getComment).mockResolvedValue({
id: 'comment-id',
assetId: 'file-id',
message: 'hello',
@@ -496,12 +490,33 @@ describe('file api', () => {
expect(json.isCompleted).toBe(true)
expect(json.completionLastChangedBy.id).toBe('user1')
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user1', name: 'Test User' },
permission: Permission.Read,
type: ResourceType.Comment,
expect(runOperation).toHaveBeenCalledWith(
resolveCommentOp,
{ userId: 'user1', via: 'api' },
{ commentId: 'comment-id', reopen: false },
)
expect(assetService.getComment).toHaveBeenCalledWith('comment-id', 'user1')
})
it('POST /comments/:commentId/complete reopens with isCompleted false', async () => {
vi.mocked(runOperation).mockResolvedValue({ commentId: 'comment-id', resolved: false })
vi.mocked(assetService.getComment).mockResolvedValue({
id: 'comment-id',
isCompleted: false,
} as any) // eslint-disable-line @typescript-eslint/no-explicit-any
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
await app.request('/comments/comment-id/complete', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ isCompleted: false }),
})
expect(runOperation).toHaveBeenCalledWith(
resolveCommentOp,
{ userId: 'user1', via: 'api' },
{ commentId: 'comment-id', reopen: true },
)
})
it('DELETE /comments/:commentId', async () => {
@@ -638,41 +653,43 @@ describe('file api', () => {
)
})
it('PATCH /files/:fileId/metadata', async () => {
vi.mocked(metadataService.updateAssetMetadata).mockResolvedValue(undefined)
// Mock assetService.getAsset to return teamId for notification
vi.mocked(assetService.getAsset).mockResolvedValue({
id: 'test-id',
project: { teamId: 'test-team' },
} as any) // eslint-disable-line @typescript-eslint/no-explicit-any
it('PATCH /files/:fileId/metadata runs update_asset_metadata', async () => {
vi.mocked(runOperation).mockResolvedValue({ assetId: 'test-id', updatedKeys: ['status'] })
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/test-id/metadata', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify([{ key: 'status', value: 'approved' }]),
body: JSON.stringify([
{ key: 'status', value: 'approved' },
{ key: 'rating', value: 4 },
]),
})
expect(res.status).toBe(200)
expect(runOperation).toHaveBeenCalledWith(
updateAssetMetadataOp,
{ userId: 'user1', via: 'api' },
{ assetId: 'test-id', values: { status: 'approved', rating: 4 } },
)
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user1', name: 'Test User' },
permission: Permission.Edit,
type: ResourceType.Asset,
id: 'test-id',
it('PATCH /files/:fileId/metadata does nothing for an empty list', async () => {
vi.mocked(runOperation).mockClear()
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/test-id/metadata', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify([]),
})
expect(notificationService.create).toHaveBeenCalledWith({
type: 'metadata_field_updated_status',
teamId: 'test-team',
creatorId: 'user1',
assetId: 'test-id',
})
expect(res.status).toBe(200)
expect(runOperation).not.toHaveBeenCalled()
})
it('PATCH /files/:fileId/metadata rejects readonly fields with 422', async () => {
vi.mocked(metadataService.updateAssetMetadata).mockRejectedValue(
new Error('Field some-key is read-only'),
vi.mocked(runOperation).mockRejectedValue(
new HTTPException(422, { message: 'Field some-key is read-only' }),
)
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
+34 -95
View File
@@ -1,11 +1,16 @@
import { zValidator } from '@hono/zod-validator'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { notificationService } from '@shumai/core/src/notification/notification'
import { commentService } from '@shumai/core/src/comment/comment'
import { s3Service } from '@shumai/core/src/s3/s3'
import type { Prisma } from '@shumai/db'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import {
renameAssetOp,
resolveCommentOp,
runOperation,
updateAssetMetadataOp,
} from '@shumai/core/src/operations'
import {
createCommentRequestSchema,
deleteFilesRequestSchema,
@@ -25,6 +30,7 @@ import {
import { transcodeService, buildContentDisposition } from '@shumai/core'
import fs from 'fs'
import { Hono } from 'hono'
import { HTTPException } from 'hono/http-exception'
import os from 'os'
import path from 'path'
import { ulid } from 'ulid'
@@ -63,32 +69,13 @@ const route = new Hono<{ Variables: { user: User } }>()
return c.json(updated)
})
.put('/files/:fileId', zValidator('json', updateFileRequestSchema), async (c) => {
const fileId = c.req.param('fileId')
const user = c.get('user')
const req = c.req.valid('json')
await authzService.hasPermission({
user,
permission: Permission.Edit,
type: ResourceType.Asset,
id: fileId,
})
const updatedAsset = await assetService.updateAssetName({
id: fileId,
name: req.name,
})
const context = await assetService.getAssetContext(fileId)
await auditLogService.logAction({
action: AuditAction.file_update,
teamId: context.teamId,
userId: user.id,
projectId: context.projectId,
itemId: fileId,
})
return c.json(updatedAsset)
const file = await runOperation(
renameAssetOp,
{ userId: c.get('user').id, via: 'api' },
{ assetId: c.req.param('fileId'), name: req.name },
)
return c.json(file)
})
.delete('/files', zValidator('json', deleteFilesRequestSchema), async (c) => {
const user = c.get('user')
@@ -124,37 +111,23 @@ const route = new Hono<{ Variables: { user: User } }>()
'/files/:fileId/metadata',
zValidator('json', z.array(updateAssetMetadataRequestSchema)),
async (c) => {
const fileId = c.req.param('fileId')
const user = c.get('user')
const req = c.req.valid('json')
await authzService.hasPermission({
user,
permission: Permission.Edit,
type: ResourceType.Asset,
id: fileId,
})
if (req.length === 0) return c.json('')
try {
await metadataService.updateAssetMetadata(fileId, req)
await runOperation(
updateAssetMetadataOp,
{ userId: c.get('user').id, via: 'api' },
{
assetId: c.req.param('fileId'),
values: Object.fromEntries(req.map((m) => [m.key, m.value])),
},
)
} catch (err) {
if (err instanceof Error && err.message.includes('is read-only')) {
if (err instanceof HTTPException && err.status === 422) {
return c.json({ error: err.message }, 422)
}
throw err
}
const hasStatus = req.some((m) => m.key === 'status')
if (hasStatus) {
const context = await assetService.getAssetContext(fileId)
notificationService.create({
type: 'metadata_field_updated_status',
teamId: context.teamId,
creatorId: user.id,
assetId: fileId,
})
}
return c.json('')
},
)
@@ -173,7 +146,9 @@ const route = new Hono<{ Variables: { user: User } }>()
id: fileId,
})
const comment = await assetService.createComment({
// The web app can attach markup and files, which the create_comment Operation doesn't
// expose; both post through the same service, so notifications are the same.
const comment = await commentService.postComment({
assetId: fileId,
userId: user.id,
replyToId: req.replyToId,
@@ -183,27 +158,7 @@ const route = new Hono<{ Variables: { user: User } }>()
attachmentIds: req.attachmentIds,
})
const notifType = req.replyToId ? 'reply_created' : 'comment_created'
const context = await assetService.getAssetContext(fileId)
let targetUserId: string | undefined
if (req.replyToId) {
const parentComment = await assetService.getComment(req.replyToId).catch(() => null)
if (parentComment?.creator?.id) {
targetUserId = parentComment.creator.id
}
}
notificationService.create({
type: notifType,
teamId: context.teamId,
creatorId: user.id,
assetId: fileId,
userId: targetUserId,
commentMessage: req.message,
})
await auditLogService.logAction({
action: AuditAction.comment_create,
teamId: context.teamId,
@@ -300,29 +255,13 @@ const route = new Hono<{ Variables: { user: User } }>()
const user = c.get('user')
const req = c.req.valid('json')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Comment,
id: commentId,
})
const commentData = await assetService.getComment(commentId).catch(() => null)
const updated = await assetService.completeComment(commentId, req.isCompleted, user.id)
if (commentData?.assetId) {
const context = await assetService.getAssetContext(commentData.assetId)
await auditLogService.logAction({
action: AuditAction.comment_complete,
teamId: context.teamId,
userId: user.id,
projectId: context.projectId,
itemId: commentId,
})
}
return c.json(updated)
await runOperation(
resolveCommentOp,
{ userId: user.id, via: 'api' },
{ commentId, reopen: !req.isCompleted },
)
// The web app shows the whole comment, so respond with it rather than the summary.
return c.json(await assetService.getComment(commentId, user.id))
},
)
.delete('/comments/:commentId', async (c) => {
+52 -64
View File
@@ -5,6 +5,13 @@ import { assetService } from '@shumai/core/src/asset/asset'
import { searchService } from '@shumai/core/src/search/search'
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
import { authMiddleware } from './middleware/auth'
import { HTTPException } from 'hono/http-exception'
import {
createFolderOp,
getFolderInstructionsOp,
renameAssetOp,
runOperation,
} from '@shumai/core/src/operations'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
@@ -36,6 +43,11 @@ vi.mock('./middleware/auth', () => ({
},
}))
vi.mock('@shumai/core/src/operations', async (importOriginal) => ({
...(await importOriginal<typeof import('@shumai/core/src/operations')>()),
runOperation: vi.fn(),
}))
describe('folder api', () => {
beforeEach(() => {
vi.mocked(authzService.hasPermission).mockClear()
@@ -81,17 +93,9 @@ describe('folder api', () => {
})
})
it('PUT /folders/:folderId', async () => {
vi.mocked(assetService.updateAssetName).mockResolvedValue({
id: 'test-id',
name: 'new-name',
sizeByte: 10,
fileCount: 1,
type: 'folder',
status: 'active',
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
})
it('PUT /folders/:folderId runs rename_asset', async () => {
const summary = { id: 'test-id', name: 'new-name', type: 'folder', size: 10, projectId: 'p1' }
vi.mocked(runOperation).mockResolvedValue(summary)
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
const res = await app.request('/folders/test-id', {
@@ -101,35 +105,17 @@ describe('folder api', () => {
})
expect(res.status).toBe(200)
const json = await res.json()
expect(json.name).toBe('new-name')
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user1', name: 'Test User' },
permission: Permission.Edit,
type: ResourceType.Asset,
id: 'test-id',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'folder_update',
teamId: 'test-team',
userId: 'user1',
projectId: undefined,
itemId: 'test-id',
})
expect(await res.json()).toEqual(summary)
expect(runOperation).toHaveBeenCalledWith(
renameAssetOp,
{ userId: 'user1', via: 'api' },
{ assetId: 'test-id', name: 'new-name' },
)
})
it('POST /folders', async () => {
vi.mocked(assetService.createAsset).mockResolvedValue({
id: 'new-id',
name: 'new-folder',
sizeByte: 0,
fileCount: 0,
type: 'folder',
status: 'active',
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
})
it('POST /folders runs create_folder', async () => {
const summary = { id: 'new-id', name: 'new-folder', type: 'folder', size: 0, projectId: 'p1' }
vi.mocked(runOperation).mockResolvedValue(summary)
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
const res = await app.request('/folders', {
@@ -139,22 +125,25 @@ describe('folder api', () => {
})
expect(res.status).toBe(200)
const json = await res.json()
expect(json.name).toBe('new-folder')
expect(await res.json()).toEqual(summary)
expect(runOperation).toHaveBeenCalledWith(
createFolderOp,
{ userId: 'user1', via: 'api' },
{ parent: 'parent-id', name: 'new-folder' },
)
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user1', name: 'Test User' },
permission: Permission.Edit,
type: ResourceType.Asset,
id: 'parent-id',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'folder_create',
teamId: 'test-team',
userId: 'user1',
projectId: undefined,
itemId: 'new-id',
it('POST /folders surfaces Operation errors with their status', async () => {
vi.mocked(runOperation).mockRejectedValue(new HTTPException(403, { message: 'Forbidden' }))
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
const res = await app.request('/folders', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'new-folder', parentId: 'parent-id' }),
})
expect(res.status).toBe(403)
})
it('GET /folders/:folderId/children', async () => {
@@ -373,23 +362,22 @@ describe('folder api', () => {
})
})
it('GET /folders/:folderId/agentsmd', async () => {
vi.spyOn(assetService, 'getAgentsMd').mockResolvedValue('# Folder Guidelines')
it('GET /folders/:folderId/agentsmd runs get_folder_instructions', async () => {
vi.mocked(runOperation).mockResolvedValue({
folderId: 'test-id',
content: '# 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')
expect(await res.json()).toEqual({ content: '# Folder Guidelines' })
expect(runOperation).toHaveBeenCalledWith(
getFolderInstructionsOp,
{ userId: 'user1', via: 'api' },
{ folderId: 'test-id' },
)
})
it('PATCH /folders/:folderId/agentsmd', async () => {
+23 -61
View File
@@ -14,37 +14,24 @@ import { listChildrenRequestSchema, updateAssetOrderRequestSchema, AuditAction }
import { searchRequestSchema } from '@shumai/dtos'
import type { Prisma } from '@shumai/db'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import {
createFolderOp,
getFolderInstructionsOp,
renameAssetOp,
runOperation,
} from '@shumai/core/src/operations'
type User = Prisma.UserGetPayload<Record<string, never>>
const route = new Hono<{ Variables: { user: User } }>()
.post('/folders', zValidator('json', createFolderRequestSchema), async (c) => {
const user = c.get('user')
const req = c.req.valid('json')
await authzService.hasPermission({
user,
permission: Permission.Edit,
type: ResourceType.Asset,
id: req.parentId,
})
const newAsset = await assetService.createAsset({
name: req.name,
parentId: req.parentId,
type: 'folder',
})
const parentCtx = await assetService.getAssetContext(req.parentId)
await auditLogService.logAction({
action: AuditAction.folder_create,
teamId: parentCtx.teamId,
userId: user.id,
projectId: parentCtx.projectId,
itemId: newAsset.id,
})
return c.json(newAsset)
const folder = await runOperation(
createFolderOp,
{ userId: c.get('user').id, via: 'api' },
{ parent: req.parentId, name: req.name },
)
return c.json(folder)
})
.patch(
'/folders/:folderId/order',
@@ -66,32 +53,13 @@ const route = new Hono<{ Variables: { user: User } }>()
},
)
.put('/folders/:folderId', zValidator('json', updateFolderRequestSchema), 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.Edit,
type: ResourceType.Asset,
id: folderId,
})
const updatedAsset = await assetService.updateAssetName({
id: folderId,
name: req.name,
})
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(updatedAsset)
const folder = await runOperation(
renameAssetOp,
{ userId: c.get('user').id, via: 'api' },
{ assetId: c.req.param('folderId'), name: req.name },
)
return c.json(folder)
})
.get('/folders/:folderId', async (c) => {
const folderId = c.req.param('folderId')
@@ -187,17 +155,11 @@ const route = new Hono<{ Variables: { user: User } }>()
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)
const { content } = await runOperation(
getFolderInstructionsOp,
{ userId: c.get('user').id, via: 'api' },
{ folderId: c.req.param('folderId') },
)
return c.json({ content })
})
.patch(
+22 -1
View File
@@ -1,4 +1,5 @@
import type { Prisma } from '@shumai/db'
import { auth } from '@shumai/core/src/auth/auth'
import { Hono } from 'hono'
import agentRoute from './agent'
import chatRoute from './chat'
@@ -11,8 +12,10 @@ import inviteRoute from './invite'
import metadataRoute from './metadata'
import mcpRoute, { mcpOauthCallbackRoute } from './mcp'
import { authMiddleware } from './middleware/auth'
import { tokenAuthMiddleware } from './middleware/tokenAuth'
import { requireApiTokenMiddleware, tokenAuthMiddleware } from './middleware/tokenAuth'
import mcpServerRoute from './mcp-server'
import notificationRoute from './notification'
import operationsRoute from './operations'
import projectRoute from './project'
import providerRoute from './provider'
import publicInviteRoute from './public-invite'
@@ -40,6 +43,10 @@ const apiRoute = new Hono<{ Variables: { user: User } }>()
.route('/', localUploadRoute)
.route('/', mcpOauthCallbackRoute)
// Programmatic endpoint for the CLI: API token required, session cookies not accepted
.use('/ops/*', requireApiTokenMiddleware)
.route('/', operationsRoute)
// Selected routes supporting API token authentication
.use('/projects', tokenAuthMiddleware)
.use('/projects/*', tokenAuthMiddleware)
@@ -74,6 +81,20 @@ const apiRoute = new Hono<{ Variables: { user: User } }>()
.route('/', kanbanRoute)
.route('/', mediaGenerationRoute)
// RFC 8414 & RFC 9728 OAuth discovery endpoints
app.on(['GET', 'HEAD', 'OPTIONS'], '/.well-known/oauth-authorization-server', (c) => {
const url = new URL(c.req.raw.url)
url.pathname = '/.well-known/oauth-authorization-server/api/auth'
return auth.handler(new Request(url.toString(), c.req.raw))
})
app.on(['GET', 'HEAD', 'OPTIONS'], '/.well-known/*', (c) => auth.handler(c.req.raw))
// Shumai's MCP server lives at the root (`/mcp`), next to the API rather than inside it, so
// it can also be served from a dedicated host (e.g. `https://mcp.example.com/mcp`). It supports
// both static API tokens and OAuth 2.1 authentication. The `/api/mcp/servers` routes are a separate
// feature: external MCP servers that Shumai's own agent connects to.
app.route('/', mcpServerRoute)
app.route('/files', s3Route)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
+10
View File
@@ -0,0 +1,10 @@
import { getAssetRenderMcpExtension, readAssetMcpExtension } from './read-asset'
import type { McpToolExtension } from './types'
export type { McpToolExtension } from './types'
/** MCP-specific presentation for Operations, by Operation name. */
export const mcpExtensions: ReadonlyMap<string, McpToolExtension> = new Map([
['read_asset', readAssetMcpExtension],
['get_asset_render', getAssetRenderMcpExtension],
])
@@ -0,0 +1,116 @@
import { z } from 'zod'
import type { CallToolResult, ImageContent } from '@modelcontextprotocol/server'
import type { AssetReadOutput } from '@shumai/operations'
import { storageBucket } from '@shumai/core/src/asset-read'
import { logger } from '@shumai/core/src/logger'
import { s3Service } from '@shumai/core/src/s3/s3'
import { getFileMimeType } from '@shumai/core/src/utils/file-mime'
import type { McpToolExtension } from './types'
/** Most images attached inline to one result; the rest are links only. */
export const MAX_INLINE_IMAGES = 8
/** Largest image attached inline; bigger ones are links only. */
export const MAX_INLINE_IMAGE_BYTES = 2 * 1024 * 1024
const extraInput = z.object({
inlineImages: z
.boolean()
.optional()
.describe(
`Attach images to the result so you can see them (default true, at most ${MAX_INLINE_IMAGES} images of up to 2 MB). Set to false to only get links.`,
),
})
/**
* Removes storage keys and the source file link: the Operation returns keys to server-side
* presenters so they can attach image bytes, but MCP clients only get image links.
*/
export function toClientOutput(output: AssetReadOutput): AssetReadOutput {
const images = output.images.map(({ label, mimeType, url }) => ({ label, mimeType, url }))
if (!output.text) return { ...output, images }
const { content, truncated, size, mimeType } = output.text
return { ...output, images, text: { content, truncated, size, mimeType } }
}
async function loadImage(key: string, fallbackMime: string): Promise<ImageContent | 'too_large'> {
const bucket = storageBucket()
const size = await s3Service.getObjectSize(bucket, key)
if (size > MAX_INLINE_IMAGE_BYTES) return 'too_large'
const { buffer, contentType } = await s3Service.getObject(bucket, key)
const mimeType =
contentType && contentType !== 'application/octet-stream'
? contentType
: getFileMimeType(buffer, key, fallbackMime)
return { type: 'image', data: buffer.toString('base64'), mimeType }
}
/** Renders a `read_asset` / `get_asset_render` output as MCP content: text, then images. */
export async function presentReadForMcp(
raw: unknown,
extra: Record<string, unknown>,
): Promise<CallToolResult> {
const output = raw as AssetReadOutput
const client = toClientOutput(output)
const structuredContent = client as unknown as Record<string, unknown>
if (output.status === 'pending') {
const retry = output.retryAfterSeconds ?? 5
return {
content: [
{
type: 'text',
text: `${output.summary} Call get_asset_render with taskId "${output.taskId}" in about ${retry} seconds to get the result.`,
},
],
structuredContent,
}
}
const inline = extra.inlineImages !== false
const images: ImageContent[] = []
const lines = [output.summary, ...output.details]
for (const [index, image] of output.images.entries()) {
let note = ''
if (inline) {
if (index >= MAX_INLINE_IMAGES) {
note = ` (not attached: only the first ${MAX_INLINE_IMAGES} images are attached)`
} else if (image.key) {
try {
const loaded = await loadImage(image.key, image.mimeType)
if (loaded === 'too_large') {
note = ' (not attached: larger than 2 MB)'
} else {
images.push(loaded)
note = ` (attached as image ${images.length})`
}
} catch (err) {
logger.warn({ err, key: image.key }, 'Failed to attach image to MCP read result')
note = ' (not attached: could not load the image)'
}
}
}
lines.push(`- ${image.label}${note}: ${image.url ?? ''}`)
}
if (output.images.length > 0) {
lines.push('Image links expire in about an hour.')
}
let text = lines.join('\n')
if (output.text) {
text += `\n\n${output.text.content}`
if (output.text.truncated) {
text += `\n\n[Content truncated: showing the first part of a ${output.text.size}-byte file. Use download_asset to get the whole file.]`
}
}
return { content: [{ type: 'text', text }, ...images], structuredContent }
}
export const readAssetMcpExtension: McpToolExtension = {
extraInput,
present: presentReadForMcp,
}
export const getAssetRenderMcpExtension: McpToolExtension = {
extraInput,
present: presentReadForMcp,
}

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