mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-20 14:59:26 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d778f1b43 | ||
|
|
6027c2c21c | ||
|
|
d0931b7181 | ||
|
|
2220d35e40 | ||
|
|
bbf55e22da | ||
|
|
68d0830d05 | ||
|
|
2858274e52 | ||
|
|
45318d1eec | ||
|
|
e8e04edfee | ||
|
|
cf2546c861 | ||
|
|
1f640ede19 | ||
|
|
3b4751bcb2 | ||
|
|
d766edbbd0 | ||
|
|
673041e28a | ||
|
|
eabf49f6e3 | ||
|
|
f226bc74c6 | ||
|
|
2159ddfeb5 | ||
|
|
813c003a10 | ||
|
|
720710dbbf | ||
|
|
68a1714e7f | ||
|
|
d8aaed5212 | ||
|
|
60c499c153 | ||
|
|
a9c6686843 | ||
|
|
c2f6c85194 | ||
|
|
8e012f9353 | ||
|
|
c55abbf724 | ||
|
|
e9e096ac9e | ||
|
|
6a63e99044 | ||
|
|
9c3b808809 | ||
|
|
c6387dd4ef | ||
|
|
a1dfc8b4f9 | ||
|
|
443b099e12 | ||
|
|
e9e9be71f4 | ||
|
|
3ae74d6417 | ||
|
|
821de71719 | ||
|
|
cbc0a7960d | ||
|
|
0e2384627d | ||
|
|
0a0a8f973d | ||
|
|
fc3baede99 | ||
|
|
ca31ef02fd | ||
|
|
2824377600 | ||
|
|
f285b9e327 | ||
|
|
42d89eb88d | ||
|
|
8d8eeaaf47 | ||
|
|
8c55897d80 | ||
|
|
1790149986 | ||
|
|
88e5440d5c | ||
|
|
d687ea495b | ||
|
|
e437cdfeaf | ||
|
|
0294056ad9 | ||
|
|
5cf949abc0 | ||
|
|
e21d419bfe | ||
|
|
4159c0737c | ||
|
|
9a2bf1deaf | ||
|
|
1a43fad77b | ||
|
|
b5fb378cf8 | ||
|
|
782316ca97 | ||
|
|
e5084cc8b3 | ||
|
|
02b009fbe1 | ||
|
|
6dcd08ae3a | ||
|
|
d230a1fd4a | ||
|
|
210ed5f95a | ||
|
|
edd550b9f2 | ||
|
|
ea71954deb | ||
|
|
dc63ab3eaf | ||
|
|
d06dfc0ab3 | ||
|
|
84aa1662be | ||
|
|
91fedf1477 |
@@ -46,6 +46,3 @@ jobs:
|
||||
- name: Test Local Release
|
||||
run: |
|
||||
bun run release:local --skip-check --out /tmp/shumai-local-release --force
|
||||
cd /tmp/shumai-local-release/node
|
||||
npx shumai-agent
|
||||
npx shumai-transcode
|
||||
|
||||
+1
-1
@@ -70,4 +70,4 @@ COPY --chown=bun:bun --from=builder /app/prisma.config.ts.prod ./prisma.config.t
|
||||
EXPOSE 3000
|
||||
|
||||
# Default command: run migrations and start API server
|
||||
CMD ["sh", "-c", "bun prisma migrate deploy && bun apps/web/src/index.js"]
|
||||
CMD ["sh", "-c", "bun prisma migrate deploy && bun index.js"]
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
<p align="center">
|
||||
<img src="docs/logo-rounded.png" alt="Shumai Logo" width="65" />
|
||||
</p>
|
||||
|
||||
<h1 align="center">Shumai</h1>
|
||||
|
||||
<p align="center">
|
||||
<strong>An open-source platform for all your creative work.</strong>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a target="_blank" href="https://shumai.one/">Website</a> | <a target="_blank" href="https://docs.shumai.one/introduction">Docs</a> | <a target="_blank" href="https://staging.shumai.one">Demo</a>
|
||||
</p>
|
||||
|
||||

|
||||
|
||||
## Features
|
||||
|
||||
- **S3-Compatible & Local FS Storage**: Securely store and serve your creative assets using local filesystems or any S3-compatible cloud storage (AWS S3, Cloudflare R2, MinIO, etc.).
|
||||
- **Comments with Draw Annotations & Timestamps**: Give precise feedback with frame-specific drawing tools and exact timestamps directly on video and image assets.
|
||||
- **Easy Share Links & Collections**: Create secure external share links and curated media collections to collaborate with external clients and stakeholders.
|
||||
- **Team & Project Member Management**: Manage granular workspace permissions using team-level and project-level role-based access control.
|
||||
- **Scalable Transcode Workflows via Temporal**: Offload resource-heavy video transcoding to a reliable background worker pool orchestrated by Temporal.
|
||||
- **Flexible Asset Metadata System**: Customize your workspace metadata with dynamic, user-defined fields tailored to your production pipeline.
|
||||
|
||||
#### Shumai Agent
|
||||
|
||||
- **Chat with Agent as a Collaborator**: Converse with a context-aware AI agent directly within your project workspace.
|
||||
- **Extend Agent with Skills**: Easily register new tools and custom skills for the agent to run and automate workflows.
|
||||
- **Secure Sandboxed Execution**: Execute agent scripts and tasks in a secure, isolated sandbox environment.
|
||||
- **AI-Powered Metadata Autofill**: Automatically generate tags, descriptions, and custom metadata for new assets using Google Gemini.
|
||||
- **Semantic Asset Search**: Find assets instantly based on their visual and textual content using vector embeddings.
|
||||
|
||||
---
|
||||
|
||||
## Installation
|
||||
|
||||
### Option 1: Docker Compose (Quick Start)
|
||||
|
||||
The easiest way to run Shumai is using Docker Compose. You do not need to clone the repository:
|
||||
|
||||
1. Create a `docker-compose.yaml` file by copying the configuration from [GitHub](https://github.com/shumaiOne/shumai/blob/main/docker-compose/local/docker-compose.yaml) or download it directly:
|
||||
```bash
|
||||
curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose.yaml
|
||||
```
|
||||
2. Configure environment variables (if deploying remotely):
|
||||
If you are deploying Shumai to a remote server (e.g., AWS ECS, EC2, VPS), you must edit `docker-compose.yaml`, set `AWS_ENDPOINT_URL_S3` to your server's public IP or domain name (e.g., `http://12.345.567.789:3000`).
|
||||
3. Start the services:
|
||||
```bash
|
||||
docker compose up -d
|
||||
```
|
||||
4. Access the application in your browser at `http://localhost:3000` (or `http://<your-server-ip>:3000` for remote deployments).
|
||||
|
||||
### Option 2: Install from NPM
|
||||
|
||||
You can run Shumai using the npm package `@shumai-one/shumai`. This option allows you to install and run Shumai globally or locally via your favorite package manager.
|
||||
|
||||
#### Step 1: Start PostgreSQL with pgvector
|
||||
|
||||
Shumai requires PostgreSQL with the `pgvector` extension. You can spin up a pre-configured database container using Docker:
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
#### Step 2: Install Shumai globally
|
||||
|
||||
Install the main Shumai package using `npm`, `pnpm`, `bun`, or `yarn`:
|
||||
|
||||
> [!IMPORTANT]
|
||||
> **Prerequisites**:
|
||||
>
|
||||
> - **Agent Sandbox**: Because Shumai uses a secure sandboxed execution environment for its AI agents, your machine must meet the platform requirements for the sandbox. Please verify you have installed the required dependencies for your OS as detailed in the [Agent Sandbox Platform Support](#agent-sandbox-platform-support) section.
|
||||
> - **FFmpeg & FFprobe**: Shumai requires `ffmpeg` and `ffprobe` to be installed on your system path for handling media transcoding and metadata extraction.
|
||||
> - macOS: `brew install ffmpeg`
|
||||
> - Ubuntu/Debian: `sudo apt-get install ffmpeg`
|
||||
> - Fedora: `sudo dnf install ffmpeg`
|
||||
|
||||
```bash
|
||||
npm install -g @shumai-one/shumai
|
||||
# or: pnpm add -g @shumai-one/shumai
|
||||
# or: bun add -g @shumai-one/shumai
|
||||
```
|
||||
|
||||
#### Step 3: Configure Environment Variables
|
||||
|
||||
Create a new directory for your Shumai configuration and navigate into it:
|
||||
|
||||
```bash
|
||||
mkdir shumai
|
||||
cd shumai
|
||||
```
|
||||
|
||||
Create a `.env` file in this folder and add the following configuration (which aligns with the Docker database command in Step 1):
|
||||
|
||||
```env
|
||||
DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public
|
||||
BETTER_AUTH_SECRET=ySxs7DxzHDZBbeeHNPEwBuspYwipBqz5Gk5XdBjNhWw=
|
||||
STORAGE_BACKEND=local
|
||||
AWS_ENDPOINT_URL_S3=http://localhost:3000
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> If you are deploying on a remote server, make sure to change `AWS_ENDPOINT_URL_S3` from `http://localhost:3000` to your server's public IP address or domain name.
|
||||
|
||||
#### Step 4: Run Shumai
|
||||
|
||||
Start the application from your configuration folder:
|
||||
|
||||
```bash
|
||||
shumai
|
||||
```
|
||||
|
||||
On startup, Shumai will automatically run the necessary database migrations and start the web server at `http://localhost:3000`.
|
||||
|
||||
---
|
||||
|
||||
### Option 3: Run from Source
|
||||
|
||||
To run Shumai locally for development:
|
||||
|
||||
1. Clone the repository and install dependencies:
|
||||
```bash
|
||||
git clone https://github.com/shumaiOne/shumai.git
|
||||
cd shumai
|
||||
bun install
|
||||
```
|
||||
2. Start the `pgvector` database container (as described in Option 2, Step 1).
|
||||
3. Create a `.env` file at the root of the workspace using the example configuration from Option 2, Step 3.
|
||||
4. Run the database migrations:
|
||||
```bash
|
||||
bun run db:migrate
|
||||
```
|
||||
5. Start the development server:
|
||||
```bash
|
||||
bun run dev
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Advanced: Distributed Execution with Temporal
|
||||
|
||||
For production environments or workloads with heavy media processing and AI agent requirements, Shumai can delegate background activities to **Temporal**. This provides distributed queue management, task retries, and high availability.
|
||||
|
||||
### How it works
|
||||
|
||||
By setting the environment variable `WORKFLOW_EXECUTOR=temporal`, the main Shumai application will delegate background workflows to a Temporal cluster instead of using the local polling-based engine. This allows you to run independent worker processes (`shumai-agent` and `shumai-transcode`) on separate specialized machines (e.g., transcoding workers on GPU-accelerated instances, and agents on sandboxed environments).
|
||||
|
||||
> [!IMPORTANT]
|
||||
> **Storage Requirement**: Because the main Shumai application and the background workers run on different machines in a distributed setup, **you must use S3-compatible storage** (e.g., AWS S3, Cloudflare R2, MinIO). Local storage (`STORAGE_BACKEND=local`) will not work because the workers and the main application do not share a common local filesystem.
|
||||
|
||||
### 1. Set up the Core Stack
|
||||
|
||||
Spin up the database, Temporal server, and the main Shumai web application on your primary host machine using Docker Compose:
|
||||
|
||||
1. Download the Temporal-enabled `docker-compose.yaml` file:
|
||||
```bash
|
||||
curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/temporal/docker-compose.yaml
|
||||
```
|
||||
2. Configure the environment variables in `docker-compose.yaml`:
|
||||
- Set the S3 storage environment variables (`AWS_ENDPOINT_URL_S3`, `S3_REGION`, `S3_BUCKET`, etc.).
|
||||
- If deploying to a remote host (e.g., AWS ECS, EC2, VPS), update `AWS_ENDPOINT_URL_S3` under the `shumai` service to your server's public IP address or domain name.
|
||||
- Note: The `TEMPORAL_ADDRESS` environment variable for the main Shumai application is pre-configured as `temporal:7233` (referencing the Temporal container within the same Docker network) and does not need to be changed.
|
||||
3. Start the services:
|
||||
```bash
|
||||
docker compose up -d
|
||||
```
|
||||
This exposes the Shumai Web UI at `http://<host-ip>:3000`, the database at port `5432`, the Temporal gRPC service at port `7233`, and the Temporal dashboard at `http://<host-ip>:8080`. Ensure that port `7233` is exposed and accessible by your background worker machines.
|
||||
|
||||
### 2. Run the Background Workers
|
||||
|
||||
When running in Temporal mode, the main Shumai application only submits tasks to the Temporal queue. To process these tasks, you must run at least one agent worker and one transcoding worker on their respective machines.
|
||||
|
||||
> [!NOTE]
|
||||
> **Network & Access Requirements**:
|
||||
>
|
||||
> - Both the transcode worker and the agent worker must have network access to the PostgreSQL database and the S3-compatible storage.
|
||||
> - Because the transcode worker needs to download and upload large media files for transcoding, it is highly recommended to deploy the transcode worker in the same cloud provider and region as your S3 storage to minimize transfer latency and data egress fees.
|
||||
|
||||
Configure a `.env` file on each worker machine. It must contain the following variables:
|
||||
|
||||
```env
|
||||
DATABASE_URL=postgresql://shumai:shumai_password@<host-ip>:5432/shumai_db?schema=public
|
||||
WORKFLOW_EXECUTOR=temporal
|
||||
TEMPORAL_ADDRESS=<host-ip>:7233
|
||||
STORAGE_BACKEND=s3
|
||||
AWS_ENDPOINT_URL_S3=http://<host-ip>:3000 # Or your public S3/R2 endpoint URL
|
||||
S3_REGION=us-east-1
|
||||
S3_BUCKET=shumai
|
||||
S3_ACCESS_KEY_ID=your-access-key-id
|
||||
S3_SECRET_ACCESS_KEY=your-secret-access-key
|
||||
```
|
||||
|
||||
Run the workers:
|
||||
|
||||
- **Transcoding Worker**:
|
||||
Install `@shumai-one/shumai-transcode` and run it on a machine with sufficient transcoding resources:
|
||||
|
||||
> [!IMPORTANT]
|
||||
> **FFmpeg Requirement**: The transcoding worker requires `ffmpeg` and `ffprobe` to be installed and available on the machine's system path.
|
||||
>
|
||||
> - macOS: `brew install ffmpeg`
|
||||
> - Ubuntu/Debian: `sudo apt-get install ffmpeg`
|
||||
|
||||
```bash
|
||||
npm install -g @shumai-one/shumai-transcode
|
||||
# Run worker with the configured .env
|
||||
shumai-transcode
|
||||
```
|
||||
|
||||
- **AI Agent Worker**:
|
||||
Install `@shumai-one/shumai-agent` and run it on a machine configured for Agent tasks:
|
||||
|
||||
> [!IMPORTANT]
|
||||
> Because the AI Agent worker runs sandboxed agent scripts, the host machine must have the necessary dependencies installed. Please refer to [Agent Sandbox Platform Support](#agent-sandbox-platform-support) for setup details.
|
||||
|
||||
```bash
|
||||
npm install -g @shumai-one/shumai-agent
|
||||
# Run worker with the configured .env (must include GEMINI_API_KEY)
|
||||
shumai-agent
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Agent Sandbox Platform Support
|
||||
|
||||
Shumai uses `anthropic-experimental/sandbox-runtime` to execute AI agent scripts and tasks in a secure, isolated sandbox environment. When installing Shumai or the AI Agent worker from NPM, ensure your host platform meets the following requirements:
|
||||
|
||||
### Platform Support
|
||||
|
||||
- **macOS**: Uses `sandbox-exec` with custom profiles (no additional OS-level containerization dependencies).
|
||||
- **Linux**: Uses `bubblewrap` (bwrap) for containerization.
|
||||
- **Windows**: Not yet supported.
|
||||
|
||||
### Platform-Specific Dependencies
|
||||
|
||||
#### Linux
|
||||
|
||||
The following system packages must be installed on your Linux host:
|
||||
|
||||
- `bubblewrap` - Container runtime
|
||||
- Ubuntu/Debian: `sudo apt-get install bubblewrap`
|
||||
- Fedora: `sudo dnf install bubblewrap`
|
||||
- Arch: `sudo pacman -S bubblewrap`
|
||||
- `socat` - Socket relay for proxy bridging
|
||||
- Ubuntu/Debian: `sudo apt-get install socat`
|
||||
- Fedora: `sudo dnf install socat`
|
||||
- Arch: `sudo pacman -S socat`
|
||||
- `ripgrep` - Fast search tool for deny path detection
|
||||
- Ubuntu/Debian: `sudo apt-get install ripgrep`
|
||||
- Fedora: `sudo dnf install ripgrep`
|
||||
- Arch: `sudo pacman -S ripgrep`
|
||||
|
||||
**Ubuntu 24.04+ Note**: These releases restrict unprivileged user namespaces by default. Disable this restriction so that `bubblewrap` and the seccomp isolation layer can function:
|
||||
|
||||
```bash
|
||||
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
|
||||
```
|
||||
|
||||
**Optional Linux Dependencies (for seccomp fallback)**:
|
||||
Pre-generated seccomp filters are provided for x86-64 and arm architectures. If you run on a different architecture, you will need a C compiler and development files:
|
||||
|
||||
- `gcc` or `clang`
|
||||
- `libseccomp` development headers
|
||||
- Ubuntu/Debian: `sudo apt-get install gcc libseccomp-dev`
|
||||
- Fedora: `sudo dnf install gcc libseccomp-devel`
|
||||
- Arch: `sudo pacman -S gcc libseccomp`
|
||||
|
||||
#### macOS
|
||||
|
||||
The following dependencies are required on macOS:
|
||||
|
||||
- `ripgrep` - Fast search tool for deny path detection
|
||||
- Install via Homebrew: `brew install ripgrep`
|
||||
- Or download from: https://github.com/BurntSushi/ripgrep/releases
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/agent-app",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"bin": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/transcode-app",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"bin": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/web-app",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
|
||||
+64
-17
@@ -1,3 +1,4 @@
|
||||
import { join } from 'node:path'
|
||||
import { loadEnvConfig } from '@shumai/core/src/env-loader'
|
||||
loadEnvConfig(process.cwd(), process.env.NODE_ENV !== 'production')
|
||||
|
||||
@@ -41,25 +42,71 @@ if (process.env.WORKFLOW_EXECUTOR === 'temporal') {
|
||||
Promise.all(queuesToStart.map((q) => workflowService.startWorkers(q))).catch(console.error)
|
||||
}
|
||||
|
||||
const server = Bun.serve({
|
||||
port: 3000,
|
||||
fetch: app.fetch,
|
||||
maxRequestBodySize: process.env.MAX_REQUEST_BODY_SIZE
|
||||
? parseInt(process.env.MAX_REQUEST_BODY_SIZE)
|
||||
: 1024 * 1024 * 1024 * 10, // Default 10GB
|
||||
routes: {
|
||||
// Serve index.html for root
|
||||
'/': index,
|
||||
const isProd = process.env.NODE_ENV === 'production'
|
||||
|
||||
// Proxy API requests to Hono
|
||||
'/api/*': app.fetch,
|
||||
'/files/*': app.fetch,
|
||||
const server = Bun.serve(
|
||||
isProd
|
||||
? {
|
||||
port: 3000,
|
||||
maxRequestBodySize: process.env.MAX_REQUEST_BODY_SIZE
|
||||
? parseInt(process.env.MAX_REQUEST_BODY_SIZE)
|
||||
: 1024 * 1024 * 1024 * 10, // Default 10GB
|
||||
development: false,
|
||||
async fetch(req) {
|
||||
const url = new URL(req.url)
|
||||
|
||||
// Catch-all for SPA routing (fallback to index.html)
|
||||
'/*': index,
|
||||
},
|
||||
development: process.env.NODE_ENV !== 'production',
|
||||
})
|
||||
if (!url.pathname.startsWith('/api/') && !url.pathname.startsWith('/files/')) {
|
||||
const filepath = join(import.meta.dir, url.pathname)
|
||||
const file = Bun.file(filepath)
|
||||
if (await file.exists()) {
|
||||
try {
|
||||
const stat = await file.stat()
|
||||
if (stat.isFile()) {
|
||||
return new Response(file)
|
||||
}
|
||||
} catch {
|
||||
// ignore stat errors
|
||||
}
|
||||
}
|
||||
|
||||
const htmlFile = Bun.file(join(import.meta.dir, 'index.html'))
|
||||
if (await htmlFile.exists()) {
|
||||
return new Response(htmlFile, {
|
||||
headers: { 'Content-Type': 'text/html' },
|
||||
})
|
||||
}
|
||||
|
||||
const htmlFileNpm = Bun.file(join(import.meta.dir, 'shumai-app.html'))
|
||||
if (await htmlFileNpm.exists()) {
|
||||
return new Response(htmlFileNpm, {
|
||||
headers: { 'Content-Type': 'text/html' },
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return app.fetch(req)
|
||||
},
|
||||
}
|
||||
: {
|
||||
port: 3000,
|
||||
maxRequestBodySize: process.env.MAX_REQUEST_BODY_SIZE
|
||||
? parseInt(process.env.MAX_REQUEST_BODY_SIZE)
|
||||
: 1024 * 1024 * 1024 * 10, // Default 10GB
|
||||
development: true,
|
||||
fetch: app.fetch,
|
||||
routes: {
|
||||
// Serve index.html for root
|
||||
'/': index,
|
||||
|
||||
// Proxy API requests to Hono
|
||||
'/api/*': app.fetch,
|
||||
'/files/*': app.fetch,
|
||||
|
||||
// Catch-all for SPA routing (fallback to index.html)
|
||||
'/*': index,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
console.log(`🚀 Server running at ${server.url}`)
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ await Bun.build({
|
||||
target: 'bun',
|
||||
outdir: outdir,
|
||||
minify: false,
|
||||
naming: '[name].[ext]',
|
||||
plugins: [
|
||||
temporalWorkflow({
|
||||
bundleOptions: {},
|
||||
|
||||
@@ -32,7 +32,6 @@ services:
|
||||
|
||||
# Better Auth
|
||||
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
|
||||
BETTER_AUTH_URL: https://staging.shumai.one/
|
||||
|
||||
depends_on:
|
||||
postgres:
|
||||
|
||||
@@ -33,7 +33,6 @@ services:
|
||||
S3_ACCESS_KEY_ID: ID
|
||||
S3_SECRET_ACCESS_KEY: KEY
|
||||
BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw=
|
||||
BETTER_AUTH_URL: http://localhost:3000
|
||||
WORKFLOW_EXECUTOR: temporal
|
||||
TEMPORAL_ADDRESS: temporal:7233
|
||||
GEMINI_API_KEY: KEY
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 26 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 309 KiB |
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "shumai",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"repository": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/agent",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
|
||||
@@ -98,6 +98,7 @@ If you need to create files in the local filesystem (for example, a temporary fi
|
||||
}
|
||||
|
||||
const { session, harness } = await createAgentSession({
|
||||
teamId: params.teamId,
|
||||
agentId: params.agentId,
|
||||
providerName,
|
||||
modelId,
|
||||
|
||||
@@ -316,6 +316,7 @@ describe('DatabaseSessionStorage', () => {
|
||||
|
||||
// 6. Resume the session via createAgentSession
|
||||
await createAgentSession({
|
||||
teamId: 't1',
|
||||
agentId: agent.id,
|
||||
providerName: 'test-provider',
|
||||
modelId: 'test-model',
|
||||
@@ -334,6 +335,7 @@ describe('DatabaseSessionStorage', () => {
|
||||
expect.objectContaining({
|
||||
MOCK_ENV_VAR: 'mocked-value',
|
||||
}),
|
||||
expect.any(Object),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -424,6 +426,7 @@ describe('DatabaseSessionStorage', () => {
|
||||
|
||||
// 3. Resume the session via createAgentSession with a NEW comment ID
|
||||
await createAgentSession({
|
||||
teamId: 't1',
|
||||
agentId: agent.id,
|
||||
providerName: 'test-provider',
|
||||
modelId: 'test-model',
|
||||
|
||||
@@ -1,6 +1,27 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { formatSkillsForPrompt } from './index'
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { formatSkillsForPrompt, createAgentSession } from './index'
|
||||
import { createSandboxedBashTool } from './tools/sandboxed-bash'
|
||||
import { SandboxManager, type SandboxAskCallback } from '@anthropic-ai/sandbox-runtime'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import * as path from 'path'
|
||||
import * as childProcess from 'node:child_process'
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let mockSpawn: any = null
|
||||
|
||||
vi.mock('node:child_process', async () => {
|
||||
const actual = (await vi.importActual('node:child_process')) as typeof childProcess
|
||||
return {
|
||||
...actual,
|
||||
spawn: (...args: Parameters<typeof childProcess.spawn>) => {
|
||||
if (mockSpawn) {
|
||||
return mockSpawn(...args)
|
||||
}
|
||||
return actual.spawn(...args)
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
describe('formatSkillsForPrompt', () => {
|
||||
it('should return empty string if skills array is empty', () => {
|
||||
@@ -31,3 +52,209 @@ describe('formatSkillsForPrompt', () => {
|
||||
expect(result).toContain('</available_skills>')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Sandbox Network isolation integration', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
it('should register callback that pushes blocked hosts to database and propagates to bash tool', async () => {
|
||||
// 1. Create a team
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Test Sandbox Team' },
|
||||
})
|
||||
|
||||
// Ensure agent user and agent exist in database to avoid foreign key violation
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: 'test-agent',
|
||||
name: 'Ai Agent',
|
||||
email: 'test-agent@shumai.ai',
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: 'test-agent',
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: {
|
||||
provider: 'openai',
|
||||
model: 'gpt-4',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// 2. Mock / spy SandboxManager
|
||||
const initializeSpy = vi.spyOn(SandboxManager, 'initialize').mockResolvedValue()
|
||||
const wrapSpy = vi
|
||||
.spyOn(SandboxManager, 'wrapWithSandbox')
|
||||
.mockImplementation(async (cmd) => cmd)
|
||||
|
||||
// 3. Call createAgentSession
|
||||
await createAgentSession({
|
||||
teamId: team.id,
|
||||
agentId: 'test-agent',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'prompt',
|
||||
teamSkills: [],
|
||||
allowedDomains: [],
|
||||
providers: [],
|
||||
})
|
||||
|
||||
// 4. Verify SandboxManager.initialize was called and extract the callback
|
||||
expect(initializeSpy).toHaveBeenCalled()
|
||||
const callback = initializeSpy.mock.calls[0][1]
|
||||
expect(callback).toBeDefined()
|
||||
|
||||
// 5. Trigger the callback with a blocked host
|
||||
const result = await callback!({ host: 'blocked-api.com', port: 443 })
|
||||
expect(result).toBe(false) // should always return false to block
|
||||
|
||||
// 6. Verify it was pushed to database
|
||||
const sandbox = await prisma.sandbox.findUnique({
|
||||
where: { teamId: team.id },
|
||||
})
|
||||
expect(sandbox?.pendingDomains).toContain('blocked-api.com')
|
||||
|
||||
// 7. Verify that if the bash tool is run while a domain is blocked, it rejects with correct error
|
||||
const sandboxState = { blockedHost: '' }
|
||||
const bashTool = createSandboxedBashTool(
|
||||
process.cwd(),
|
||||
{},
|
||||
{
|
||||
getBlockedHost: () => sandboxState.blockedHost,
|
||||
clearBlockedHost: () => {
|
||||
sandboxState.blockedHost = ''
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
// Mock spawn to simulate process exit and trigger network block
|
||||
mockSpawn = () => {
|
||||
sandboxState.blockedHost = 'blocked-api.com'
|
||||
const mockChild = {
|
||||
pid: 123,
|
||||
stdout: { on: vi.fn() },
|
||||
stderr: { on: vi.fn() },
|
||||
on: vi.fn((event: string, cb: (code: number) => void) => {
|
||||
if (event === 'close') {
|
||||
setTimeout(() => cb(1), 10)
|
||||
}
|
||||
}),
|
||||
} as unknown as childProcess.ChildProcess
|
||||
return mockChild
|
||||
}
|
||||
|
||||
const bashPromise = bashTool.execute('1', { command: 'curl blocked-api.com' })
|
||||
|
||||
try {
|
||||
await expect(bashPromise).rejects.toThrow(
|
||||
'Network request to blocked-api.com is blocked, please ask admin to allow it in sandbox settings.',
|
||||
)
|
||||
} finally {
|
||||
mockSpawn = null
|
||||
initializeSpy.mockRestore()
|
||||
wrapSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
|
||||
it('should correctly route sandbox callback to the active session when multiple sessions are created sequentially', async () => {
|
||||
// 1. Create two teams
|
||||
const team1 = await prisma.team.create({
|
||||
data: { name: 'Team 1' },
|
||||
})
|
||||
const team2 = await prisma.team.create({
|
||||
data: { name: 'Team 2' },
|
||||
})
|
||||
|
||||
// Ensure agent user and agents exist in database
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: 'agent-1',
|
||||
name: 'Ai Agent 1',
|
||||
email: 'agent1@shumai.ai',
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: 'agent-2',
|
||||
name: 'Ai Agent 2',
|
||||
email: 'agent2@shumai.ai',
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: 'agent-1',
|
||||
teamId: team1.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'openai', model: 'gpt-4' },
|
||||
},
|
||||
})
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: 'agent-2',
|
||||
teamId: team2.id,
|
||||
type: 'chat',
|
||||
config: { provider: 'openai', model: 'gpt-4' },
|
||||
},
|
||||
})
|
||||
|
||||
// Mock SandboxManager.initialize to behave like a process-global singleton
|
||||
let registeredCallback: SandboxAskCallback | null = null
|
||||
const initializeSpy = vi
|
||||
.spyOn(SandboxManager, 'initialize')
|
||||
.mockImplementation(async (config, cb) => {
|
||||
if (!registeredCallback) {
|
||||
registeredCallback = cb || null
|
||||
}
|
||||
})
|
||||
const resetSpy = vi.spyOn(SandboxManager, 'reset').mockImplementation(async () => {
|
||||
// In the real SandboxManager, reset clears the global initialization promise and servers,
|
||||
// which allows subsequent initialize calls to register a new callback.
|
||||
registeredCallback = null
|
||||
})
|
||||
|
||||
try {
|
||||
// 2. Call createAgentSession for Session 1
|
||||
await createAgentSession({
|
||||
teamId: team1.id,
|
||||
agentId: 'agent-1',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'prompt',
|
||||
teamSkills: [],
|
||||
allowedDomains: [],
|
||||
providers: [],
|
||||
})
|
||||
|
||||
// 3. Call createAgentSession for Session 2
|
||||
await createAgentSession({
|
||||
teamId: team2.id,
|
||||
agentId: 'agent-2',
|
||||
providerName: 'google',
|
||||
modelId: 'gemini',
|
||||
systemPrompt: 'prompt',
|
||||
teamSkills: [],
|
||||
allowedDomains: [],
|
||||
providers: [],
|
||||
})
|
||||
|
||||
expect(registeredCallback).toBeDefined()
|
||||
|
||||
// 4. Trigger the globally registered callback (which is from Session 1)
|
||||
const result = await registeredCallback!({ host: 'blocked-for-session-2.com', port: 443 })
|
||||
expect(result).toBe(false)
|
||||
|
||||
// 5. Verify that the pending domain was pushed to the DB record of Team 2
|
||||
const sandbox2 = await prisma.sandbox.findUnique({
|
||||
where: { teamId: team2.id },
|
||||
})
|
||||
expect(sandbox2?.pendingDomains).toContain('blocked-for-session-2.com')
|
||||
} finally {
|
||||
initializeSpy.mockRestore()
|
||||
resetSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
+58
-11
@@ -5,6 +5,7 @@ import { getModel } from '@earendil-works/pi-ai'
|
||||
import { Type, type TSchema } from '@sinclair/typebox'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import { DatabaseSessionStorage } from './database-session-storage'
|
||||
import { createAnalyzeImageTool } from './tools/analyze-image'
|
||||
@@ -17,6 +18,7 @@ import { createReadSkillTool } from './tools/read-skill'
|
||||
import { createSandboxedBashTool } from './tools/sandboxed-bash'
|
||||
|
||||
export interface CreateAgentSessionParams {
|
||||
teamId: string
|
||||
agentId: string
|
||||
providerName: string
|
||||
modelId: string
|
||||
@@ -40,6 +42,7 @@ export interface CreateAgentSessionParams {
|
||||
|
||||
export async function createAgentSession(params: CreateAgentSessionParams) {
|
||||
const {
|
||||
teamId,
|
||||
agentId,
|
||||
providerName,
|
||||
modelId,
|
||||
@@ -115,18 +118,57 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
|
||||
|
||||
const allowWrite = [piDir]
|
||||
|
||||
const sandboxState = {
|
||||
blockedHost: '',
|
||||
}
|
||||
|
||||
const sandboxAskCallback = async ({ host }: { host: string; port?: number }) => {
|
||||
try {
|
||||
const sandbox = await prisma.sandbox.findUnique({
|
||||
where: { teamId },
|
||||
})
|
||||
const pendingDomains = sandbox?.pendingDomains || []
|
||||
if (!pendingDomains.includes(host)) {
|
||||
await prisma.sandbox.upsert({
|
||||
where: { teamId },
|
||||
create: {
|
||||
teamId,
|
||||
pendingDomains: [host],
|
||||
},
|
||||
update: {
|
||||
pendingDomains: {
|
||||
push: host,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to update sandbox pending domains:', err)
|
||||
}
|
||||
|
||||
sandboxState.blockedHost = host
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// SandboxManager.initialize is a global operation that applies to the entire process.
|
||||
await SandboxManager.initialize({
|
||||
network: {
|
||||
allowedDomains,
|
||||
deniedDomains: [],
|
||||
// We reset it first to ensure any previous process-global SOCKS/HTTP proxy servers
|
||||
// and callbacks are cleaned up, allowing the new callback and allowedDomains to take effect.
|
||||
await SandboxManager.reset()
|
||||
await SandboxManager.initialize(
|
||||
{
|
||||
network: {
|
||||
allowedDomains,
|
||||
deniedDomains: [],
|
||||
},
|
||||
filesystem: {
|
||||
denyRead: ['.env', '.env.*', '*.pem', '*.key'],
|
||||
allowWrite,
|
||||
denyWrite: ['.env', '.env.*', '*.pem', '*.key'],
|
||||
},
|
||||
},
|
||||
filesystem: {
|
||||
denyRead: ['.env', '.env.*', '*.pem', '*.key'],
|
||||
allowWrite,
|
||||
denyWrite: ['.env', '.env.*', '*.pem', '*.key'],
|
||||
},
|
||||
})
|
||||
sandboxAskCallback,
|
||||
)
|
||||
|
||||
const skillEnvs: Record<string, string> = {}
|
||||
const onEnvsAdded = (envs: Record<string, string>) => {
|
||||
@@ -169,7 +211,12 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
|
||||
)
|
||||
}
|
||||
|
||||
const sandboxedBash = createSandboxedBashTool(process.cwd(), skillEnvs)
|
||||
const sandboxedBash = createSandboxedBashTool(process.cwd(), skillEnvs, {
|
||||
getBlockedHost: () => sandboxState.blockedHost,
|
||||
clearBlockedHost: () => {
|
||||
sandboxState.blockedHost = ''
|
||||
},
|
||||
})
|
||||
const readSkill = createReadSkillTool(onEnvsAdded)
|
||||
|
||||
const systemTools: AgentTool[] = []
|
||||
|
||||
@@ -15,9 +15,15 @@ type BashDetails = {
|
||||
stderr: string
|
||||
}
|
||||
|
||||
export interface SandboxedBashOptions {
|
||||
getBlockedHost?: () => string
|
||||
clearBlockedHost?: () => void
|
||||
}
|
||||
|
||||
export const createSandboxedBashTool = (
|
||||
cwd: string,
|
||||
skillEnvs: Record<string, string> = {},
|
||||
options?: SandboxedBashOptions,
|
||||
): AgentTool<typeof BashParameters, BashDetails> => {
|
||||
return {
|
||||
name: 'bash',
|
||||
@@ -29,6 +35,7 @@ export const createSandboxedBashTool = (
|
||||
{ command, timeout },
|
||||
signal,
|
||||
): Promise<AgentToolResult<BashDetails>> => {
|
||||
options?.clearBlockedHost?.()
|
||||
const wrappedCommand = await SandboxManager.wrapWithSandbox(command)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -94,12 +101,20 @@ export const createSandboxedBashTool = (
|
||||
if (timeoutHandle) clearTimeout(timeoutHandle)
|
||||
signal?.removeEventListener('abort', onAbort)
|
||||
|
||||
const blocked = options?.getBlockedHost?.()
|
||||
|
||||
if (signal?.aborted) {
|
||||
logger.error({ command, stdout, stderr }, 'Sandboxed bash command aborted')
|
||||
reject(new Error('aborted'))
|
||||
} else if (timedOut) {
|
||||
logger.error({ timeout, command, stdout, stderr }, 'Sandboxed bash command timed out')
|
||||
reject(new Error(`bash command timed out after ${timeout} seconds`))
|
||||
} else if (blocked) {
|
||||
reject(
|
||||
new Error(
|
||||
`Network request to ${blocked} is blocked, please ask admin to allow it in sandbox settings.`,
|
||||
),
|
||||
)
|
||||
} else if (code !== 0 && code !== null) {
|
||||
logger.error(
|
||||
{ exitCode: code, command, stdout, stderr },
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/api",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
|
||||
@@ -268,13 +268,17 @@ describe('team api', () => {
|
||||
})
|
||||
|
||||
it('GET /teams/:teamId/sandbox returns sandbox settings', async () => {
|
||||
mockGetSandboxSettings.mockResolvedValue({ allowedDomains: ['example.com'] })
|
||||
mockGetSandboxSettings.mockResolvedValue({
|
||||
allowedDomains: ['example.com'],
|
||||
pendingDomains: ['pending.com'],
|
||||
})
|
||||
|
||||
const res = await app.request('/teams/t1/sandbox')
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data.allowedDomains).toEqual(['example.com'])
|
||||
expect(data.pendingDomains).toEqual(['pending.com'])
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: ResourceType.Team,
|
||||
@@ -286,17 +290,21 @@ describe('team api', () => {
|
||||
})
|
||||
|
||||
it('PUT /teams/:teamId/sandbox updates sandbox settings', async () => {
|
||||
mockUpdateSandboxSettings.mockResolvedValue({ allowedDomains: ['new.com'] })
|
||||
mockUpdateSandboxSettings.mockResolvedValue({
|
||||
allowedDomains: ['new.com'],
|
||||
pendingDomains: ['new-pending.com'],
|
||||
})
|
||||
|
||||
const res = await app.request('/teams/t1/sandbox', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ allowedDomains: ['new.com'] }),
|
||||
body: JSON.stringify({ allowedDomains: ['new.com'], pendingDomains: ['new-pending.com'] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data.allowedDomains).toEqual(['new.com'])
|
||||
expect(data.pendingDomains).toEqual(['new-pending.com'])
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: ResourceType.Team,
|
||||
@@ -304,6 +312,9 @@ describe('team api', () => {
|
||||
permission: Permission.Admin,
|
||||
}),
|
||||
)
|
||||
expect(mockUpdateSandboxSettings).toHaveBeenCalledWith('t1', { allowedDomains: ['new.com'] })
|
||||
expect(mockUpdateSandboxSettings).toHaveBeenCalledWith('t1', {
|
||||
allowedDomains: ['new.com'],
|
||||
pendingDomains: ['new-pending.com'],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/core",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
|
||||
@@ -1152,6 +1152,134 @@ describe('AssetService', () => {
|
||||
expect(updatedA?.sortIndex && updatedA.sortIndex > 'a1').toBe(true)
|
||||
})
|
||||
|
||||
it('verifies that sortIndex uses C collation to prevent fractional indexing mismatch', async () => {
|
||||
const { user, project } = await setupBasicAssets()
|
||||
|
||||
// Create a parent folder to contain our assets
|
||||
const folder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Parent Folder',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// Create Asset A: In ASCII, "YjnsD0vZ" > "YjNuyGpV" because "n" (110) > "N" (78).
|
||||
// In default Postgres locale collation, "YjnsD0vZ" < "YjNuyGpV".
|
||||
const assetA = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Asset A',
|
||||
type: AssetType.file,
|
||||
parentId: folder.id,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sortIndex: 'YjnsD0vZ',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// Create Asset B
|
||||
const assetB = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Asset B',
|
||||
type: AssetType.file,
|
||||
parentId: folder.id,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sortIndex: 'YjNuyGpV',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// Create Asset C
|
||||
const assetC = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Asset C',
|
||||
type: AssetType.file,
|
||||
parentId: folder.id,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sortIndex: 'Yjzaaa',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// Verify ASCII ordering in JavaScript: assetA.sortIndex > assetB.sortIndex
|
||||
expect(assetA.sortIndex! > assetB.sortIndex!).toBe(true)
|
||||
|
||||
// Verify Postgres sorting behavior:
|
||||
// With C collation, it must follow ASCII ordering. Therefore, searching for
|
||||
// sortIndex < assetB.sortIndex must NOT find assetA.
|
||||
const prevAsset = await prisma.asset.findFirst({
|
||||
where: {
|
||||
parentId: folder.id,
|
||||
sortIndex: { lt: assetB.sortIndex! },
|
||||
id: { not: assetC.id },
|
||||
},
|
||||
orderBy: { sortIndex: 'desc' },
|
||||
})
|
||||
|
||||
// Assert that the database collation is correctly "C" (returns null for prevAsset)
|
||||
expect(prevAsset).toBeNull()
|
||||
|
||||
// Assert that updateAssetOrder succeeds without throwing a collation error
|
||||
const updatedC = await assetService.updateAssetOrder(assetC.id, {
|
||||
beforeIndex: assetB.sortIndex!,
|
||||
})
|
||||
expect(updatedC.sortIndex).toBeDefined()
|
||||
expect(updatedC.sortIndex! < assetB.sortIndex!).toBe(true)
|
||||
})
|
||||
|
||||
it('correctly bounds the new sortIndex when moving an asset after another asset in the middle of the list', async () => {
|
||||
const { user, project } = await setupBasicAssets()
|
||||
|
||||
// Create three assets: A, B, C with specific sort indexes
|
||||
const assetA = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Asset A',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sortIndex: 'a0',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
const assetB = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Asset B',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sortIndex: 'a1',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
const assetC = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Asset C',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sortIndex: 'a11',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// Move Asset A to be after Asset B (it should be placed between B ("a1") and C ("a11"))
|
||||
const updatedA = await assetService.updateAssetOrder(assetA.id, {
|
||||
afterIndex: assetB.sortIndex!,
|
||||
})
|
||||
|
||||
// Verify it is placed after B
|
||||
expect(updatedA.sortIndex! > assetB.sortIndex!).toBe(true)
|
||||
// Verify it is placed before C (this fails under the buggy code because C's bound is ignored, generating a key greater than "a11")
|
||||
expect(updatedA.sortIndex! < assetC.sortIndex!).toBe(true)
|
||||
})
|
||||
|
||||
describe('Symlinks', () => {
|
||||
it('toAssetInfo resolves symlink target metadata', async () => {
|
||||
const { project, user } = await setupBasicAssets()
|
||||
@@ -1266,6 +1394,7 @@ describe('AssetService', () => {
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
sortIndex: 'abc',
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1297,6 +1426,7 @@ describe('AssetService', () => {
|
||||
expect(res.data).toHaveLength(1)
|
||||
const stackAsset = res.data[0]
|
||||
expect(stackAsset.type).toBe(AssetType.version_stack)
|
||||
expect(stackAsset.sortIndex).toBe('abc')
|
||||
expect(stackAsset.versionStack).toBeDefined()
|
||||
expect(stackAsset.versionStack?.versions).toHaveLength(2)
|
||||
const versions = stackAsset.versionStack!.versions
|
||||
|
||||
@@ -533,6 +533,7 @@ export class AssetService {
|
||||
parentId: stackParentId,
|
||||
creatorId: creatorId,
|
||||
name: '',
|
||||
sortIndex: destFile.sortIndex,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -973,7 +974,15 @@ export class AssetService {
|
||||
afterSortIndex = prevAsset?.sortIndex || null
|
||||
} else if (req.afterIndex) {
|
||||
afterSortIndex = req.afterIndex
|
||||
beforeSortIndex = null
|
||||
const nextAsset = await this.prismaClient.asset.findFirst({
|
||||
where: {
|
||||
parentId: asset.parentId,
|
||||
sortIndex: { gt: req.afterIndex },
|
||||
id: { not: id },
|
||||
},
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
})
|
||||
beforeSortIndex = nextAsset?.sortIndex || null
|
||||
}
|
||||
|
||||
const newSortIndex = generateKeyBetween(afterSortIndex, beforeSortIndex)
|
||||
|
||||
@@ -15,7 +15,7 @@ export const auth = betterAuth({
|
||||
},
|
||||
secret: process.env.BETTER_AUTH_SECRET,
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
baseURL: process.env.BETTER_AUTH_URL,
|
||||
baseURL: process.env.BETTER_AUTH_URL || 'http://localhost:3000',
|
||||
basePath: '/api/auth',
|
||||
trustedOrigins: ['http://localhost:3000', 'http://127.0.0.1:3000'],
|
||||
advanced: {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -290,16 +290,20 @@ describe('TeamService', () => {
|
||||
// Should have default settings
|
||||
const settings = await teamService.getSandboxSettings(team.id)
|
||||
expect(settings.allowedDomains).toContain('github.com')
|
||||
expect(settings.pendingDomains).toEqual([])
|
||||
|
||||
// Should update settings
|
||||
const updated = await teamService.updateSandboxSettings(team.id, {
|
||||
allowedDomains: ['example.com'],
|
||||
pendingDomains: ['pending.com'],
|
||||
})
|
||||
expect(updated.allowedDomains).toEqual(['example.com'])
|
||||
expect(updated.pendingDomains).toEqual(['pending.com'])
|
||||
|
||||
// Should persist to DB
|
||||
const persisted = await teamService.getSandboxSettings(team.id)
|
||||
expect(persisted.allowedDomains).toEqual(['example.com'])
|
||||
expect(persisted.pendingDomains).toEqual(['pending.com'])
|
||||
})
|
||||
|
||||
it('should return empty allowedDomains if sandbox is missing for old team', async () => {
|
||||
@@ -309,5 +313,6 @@ describe('TeamService', () => {
|
||||
|
||||
const settings = await teamService.getSandboxSettings(team.id)
|
||||
expect(settings.allowedDomains).toEqual([])
|
||||
expect(settings.pendingDomains).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -322,6 +322,7 @@ export class TeamService {
|
||||
})
|
||||
return {
|
||||
allowedDomains: sandbox?.allowedDomains || [],
|
||||
pendingDomains: sandbox?.pendingDomains || [],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -331,13 +332,16 @@ export class TeamService {
|
||||
create: {
|
||||
teamId,
|
||||
allowedDomains: settings.allowedDomains,
|
||||
pendingDomains: settings.pendingDomains,
|
||||
},
|
||||
update: {
|
||||
allowedDomains: settings.allowedDomains,
|
||||
pendingDomains: settings.pendingDomains,
|
||||
},
|
||||
})
|
||||
return {
|
||||
allowedDomains: sandbox.allowedDomains,
|
||||
pendingDomains: sandbox.pendingDomains,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@ describe('VersionStackService', () => {
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
sortIndex: 'abc',
|
||||
},
|
||||
})
|
||||
|
||||
@@ -85,6 +86,7 @@ describe('VersionStackService', () => {
|
||||
expect(stack.fileCount).toBe(2)
|
||||
expect(stack.sizeByte).toBe(300)
|
||||
expect(stack.parentId).toBe(parent.id)
|
||||
expect(stack.sortIndex).toBe('abc')
|
||||
|
||||
// Verify Parent file count
|
||||
const updatedParent = await prisma.asset.findUnique({ where: { id: parent.id } })
|
||||
|
||||
@@ -58,6 +58,9 @@ export class VersionStackService {
|
||||
throw new Error('parent not found')
|
||||
}
|
||||
|
||||
const firstFile = files[0]
|
||||
const sortIndex = firstFile?.sortIndex || null
|
||||
|
||||
const stack = await tx.asset.create({
|
||||
data: {
|
||||
type: AssetType.version_stack,
|
||||
@@ -67,6 +70,7 @@ export class VersionStackService {
|
||||
projectId: projectId,
|
||||
creatorId: creatorId,
|
||||
parentId: parentId,
|
||||
sortIndex: sortIndex,
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/db",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "sandboxes" ADD COLUMN "pending_domains" TEXT[] DEFAULT ARRAY[]::TEXT[];
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
-- AlterTable: Change sort_index column collation to "C"
|
||||
ALTER TABLE "assets" ALTER COLUMN "sort_index" TYPE TEXT COLLATE "C";
|
||||
|
||||
-- Recreate index on sort_index with "C" collation
|
||||
DROP INDEX IF EXISTS "assets_sort_index_idx";
|
||||
CREATE INDEX "assets_sort_index_idx" ON "assets" ("sort_index" COLLATE "C");
|
||||
@@ -236,6 +236,7 @@ model Team {
|
||||
model Sandbox {
|
||||
id String @id @default(ulid())
|
||||
allowedDomains String[] @default(["npmjs.org", "*.npmjs.org", "registry.npmjs.org", "registry.yarnpkg.com", "pypi.org", "*.pypi.org", "github.com", "*.github.com", "api.github.com", "raw.githubusercontent.com"])
|
||||
pendingDomains String[] @default([]) @map("pending_domains")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -2970,6 +2970,7 @@ export type TeamScalarFieldEnum = (typeof TeamScalarFieldEnum)[keyof typeof Team
|
||||
export const SandboxScalarFieldEnum = {
|
||||
id: 'id',
|
||||
allowedDomains: 'allowedDomains',
|
||||
pendingDomains: 'pendingDomains',
|
||||
createdAt: 'createdAt',
|
||||
updatedAt: 'updatedAt',
|
||||
teamId: 'teamId',
|
||||
|
||||
@@ -185,6 +185,7 @@ export type TeamScalarFieldEnum = (typeof TeamScalarFieldEnum)[keyof typeof Team
|
||||
export const SandboxScalarFieldEnum = {
|
||||
id: 'id',
|
||||
allowedDomains: 'allowedDomains',
|
||||
pendingDomains: 'pendingDomains',
|
||||
createdAt: 'createdAt',
|
||||
updatedAt: 'updatedAt',
|
||||
teamId: 'teamId',
|
||||
|
||||
@@ -42,6 +42,7 @@ export type SandboxMaxAggregateOutputType = {
|
||||
export type SandboxCountAggregateOutputType = {
|
||||
id: number
|
||||
allowedDomains: number
|
||||
pendingDomains: number
|
||||
createdAt: number
|
||||
updatedAt: number
|
||||
teamId: number
|
||||
@@ -65,6 +66,7 @@ export type SandboxMaxAggregateInputType = {
|
||||
export type SandboxCountAggregateInputType = {
|
||||
id?: true
|
||||
allowedDomains?: true
|
||||
pendingDomains?: true
|
||||
createdAt?: true
|
||||
updatedAt?: true
|
||||
teamId?: true
|
||||
@@ -147,6 +149,7 @@ export type SandboxGroupByArgs<
|
||||
export type SandboxGroupByOutputType = {
|
||||
id: string
|
||||
allowedDomains: string[]
|
||||
pendingDomains: string[]
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
teamId: string
|
||||
@@ -173,6 +176,7 @@ export type SandboxWhereInput = {
|
||||
NOT?: Prisma.SandboxWhereInput | Prisma.SandboxWhereInput[]
|
||||
id?: Prisma.StringFilter<'Sandbox'> | string
|
||||
allowedDomains?: Prisma.StringNullableListFilter<'Sandbox'>
|
||||
pendingDomains?: Prisma.StringNullableListFilter<'Sandbox'>
|
||||
createdAt?: Prisma.DateTimeFilter<'Sandbox'> | Date | string
|
||||
updatedAt?: Prisma.DateTimeFilter<'Sandbox'> | Date | string
|
||||
teamId?: Prisma.StringFilter<'Sandbox'> | string
|
||||
@@ -182,6 +186,7 @@ export type SandboxWhereInput = {
|
||||
export type SandboxOrderByWithRelationInput = {
|
||||
id?: Prisma.SortOrder
|
||||
allowedDomains?: Prisma.SortOrder
|
||||
pendingDomains?: Prisma.SortOrder
|
||||
createdAt?: Prisma.SortOrder
|
||||
updatedAt?: Prisma.SortOrder
|
||||
teamId?: Prisma.SortOrder
|
||||
@@ -196,6 +201,7 @@ export type SandboxWhereUniqueInput = Prisma.AtLeast<
|
||||
OR?: Prisma.SandboxWhereInput[]
|
||||
NOT?: Prisma.SandboxWhereInput | Prisma.SandboxWhereInput[]
|
||||
allowedDomains?: Prisma.StringNullableListFilter<'Sandbox'>
|
||||
pendingDomains?: Prisma.StringNullableListFilter<'Sandbox'>
|
||||
createdAt?: Prisma.DateTimeFilter<'Sandbox'> | Date | string
|
||||
updatedAt?: Prisma.DateTimeFilter<'Sandbox'> | Date | string
|
||||
team?: Prisma.XOR<Prisma.TeamScalarRelationFilter, Prisma.TeamWhereInput>
|
||||
@@ -206,6 +212,7 @@ export type SandboxWhereUniqueInput = Prisma.AtLeast<
|
||||
export type SandboxOrderByWithAggregationInput = {
|
||||
id?: Prisma.SortOrder
|
||||
allowedDomains?: Prisma.SortOrder
|
||||
pendingDomains?: Prisma.SortOrder
|
||||
createdAt?: Prisma.SortOrder
|
||||
updatedAt?: Prisma.SortOrder
|
||||
teamId?: Prisma.SortOrder
|
||||
@@ -224,6 +231,7 @@ export type SandboxScalarWhereWithAggregatesInput = {
|
||||
| Prisma.SandboxScalarWhereWithAggregatesInput[]
|
||||
id?: Prisma.StringWithAggregatesFilter<'Sandbox'> | string
|
||||
allowedDomains?: Prisma.StringNullableListFilter<'Sandbox'>
|
||||
pendingDomains?: Prisma.StringNullableListFilter<'Sandbox'>
|
||||
createdAt?: Prisma.DateTimeWithAggregatesFilter<'Sandbox'> | Date | string
|
||||
updatedAt?: Prisma.DateTimeWithAggregatesFilter<'Sandbox'> | Date | string
|
||||
teamId?: Prisma.StringWithAggregatesFilter<'Sandbox'> | string
|
||||
@@ -232,6 +240,7 @@ export type SandboxScalarWhereWithAggregatesInput = {
|
||||
export type SandboxCreateInput = {
|
||||
id?: string
|
||||
allowedDomains?: Prisma.SandboxCreateallowedDomainsInput | string[]
|
||||
pendingDomains?: Prisma.SandboxCreatependingDomainsInput | string[]
|
||||
createdAt?: Date | string
|
||||
updatedAt?: Date | string
|
||||
team: Prisma.TeamCreateNestedOneWithoutSandboxInput
|
||||
@@ -240,6 +249,7 @@ export type SandboxCreateInput = {
|
||||
export type SandboxUncheckedCreateInput = {
|
||||
id?: string
|
||||
allowedDomains?: Prisma.SandboxCreateallowedDomainsInput | string[]
|
||||
pendingDomains?: Prisma.SandboxCreatependingDomainsInput | string[]
|
||||
createdAt?: Date | string
|
||||
updatedAt?: Date | string
|
||||
teamId: string
|
||||
@@ -248,6 +258,7 @@ export type SandboxUncheckedCreateInput = {
|
||||
export type SandboxUpdateInput = {
|
||||
id?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
allowedDomains?: Prisma.SandboxUpdateallowedDomainsInput | string[]
|
||||
pendingDomains?: Prisma.SandboxUpdatependingDomainsInput | string[]
|
||||
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
|
||||
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
|
||||
team?: Prisma.TeamUpdateOneRequiredWithoutSandboxNestedInput
|
||||
@@ -256,6 +267,7 @@ export type SandboxUpdateInput = {
|
||||
export type SandboxUncheckedUpdateInput = {
|
||||
id?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
allowedDomains?: Prisma.SandboxUpdateallowedDomainsInput | string[]
|
||||
pendingDomains?: Prisma.SandboxUpdatependingDomainsInput | string[]
|
||||
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
|
||||
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
|
||||
teamId?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
@@ -264,6 +276,7 @@ export type SandboxUncheckedUpdateInput = {
|
||||
export type SandboxCreateManyInput = {
|
||||
id?: string
|
||||
allowedDomains?: Prisma.SandboxCreateallowedDomainsInput | string[]
|
||||
pendingDomains?: Prisma.SandboxCreatependingDomainsInput | string[]
|
||||
createdAt?: Date | string
|
||||
updatedAt?: Date | string
|
||||
teamId: string
|
||||
@@ -272,6 +285,7 @@ export type SandboxCreateManyInput = {
|
||||
export type SandboxUpdateManyMutationInput = {
|
||||
id?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
allowedDomains?: Prisma.SandboxUpdateallowedDomainsInput | string[]
|
||||
pendingDomains?: Prisma.SandboxUpdatependingDomainsInput | string[]
|
||||
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
|
||||
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
|
||||
}
|
||||
@@ -279,6 +293,7 @@ export type SandboxUpdateManyMutationInput = {
|
||||
export type SandboxUncheckedUpdateManyInput = {
|
||||
id?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
allowedDomains?: Prisma.SandboxUpdateallowedDomainsInput | string[]
|
||||
pendingDomains?: Prisma.SandboxUpdatependingDomainsInput | string[]
|
||||
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
|
||||
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
|
||||
teamId?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
@@ -300,6 +315,7 @@ export type StringNullableListFilter<$PrismaModel = never> = {
|
||||
export type SandboxCountOrderByAggregateInput = {
|
||||
id?: Prisma.SortOrder
|
||||
allowedDomains?: Prisma.SortOrder
|
||||
pendingDomains?: Prisma.SortOrder
|
||||
createdAt?: Prisma.SortOrder
|
||||
updatedAt?: Prisma.SortOrder
|
||||
teamId?: Prisma.SortOrder
|
||||
@@ -379,14 +395,24 @@ export type SandboxCreateallowedDomainsInput = {
|
||||
set: string[]
|
||||
}
|
||||
|
||||
export type SandboxCreatependingDomainsInput = {
|
||||
set: string[]
|
||||
}
|
||||
|
||||
export type SandboxUpdateallowedDomainsInput = {
|
||||
set?: string[]
|
||||
push?: string | string[]
|
||||
}
|
||||
|
||||
export type SandboxUpdatependingDomainsInput = {
|
||||
set?: string[]
|
||||
push?: string | string[]
|
||||
}
|
||||
|
||||
export type SandboxCreateWithoutTeamInput = {
|
||||
id?: string
|
||||
allowedDomains?: Prisma.SandboxCreateallowedDomainsInput | string[]
|
||||
pendingDomains?: Prisma.SandboxCreatependingDomainsInput | string[]
|
||||
createdAt?: Date | string
|
||||
updatedAt?: Date | string
|
||||
}
|
||||
@@ -394,6 +420,7 @@ export type SandboxCreateWithoutTeamInput = {
|
||||
export type SandboxUncheckedCreateWithoutTeamInput = {
|
||||
id?: string
|
||||
allowedDomains?: Prisma.SandboxCreateallowedDomainsInput | string[]
|
||||
pendingDomains?: Prisma.SandboxCreatependingDomainsInput | string[]
|
||||
createdAt?: Date | string
|
||||
updatedAt?: Date | string
|
||||
}
|
||||
@@ -429,6 +456,7 @@ export type SandboxUpdateToOneWithWhereWithoutTeamInput = {
|
||||
export type SandboxUpdateWithoutTeamInput = {
|
||||
id?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
allowedDomains?: Prisma.SandboxUpdateallowedDomainsInput | string[]
|
||||
pendingDomains?: Prisma.SandboxUpdatependingDomainsInput | string[]
|
||||
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
|
||||
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
|
||||
}
|
||||
@@ -436,6 +464,7 @@ export type SandboxUpdateWithoutTeamInput = {
|
||||
export type SandboxUncheckedUpdateWithoutTeamInput = {
|
||||
id?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
allowedDomains?: Prisma.SandboxUpdateallowedDomainsInput | string[]
|
||||
pendingDomains?: Prisma.SandboxUpdatependingDomainsInput | string[]
|
||||
createdAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
|
||||
updatedAt?: Prisma.DateTimeFieldUpdateOperationsInput | Date | string
|
||||
}
|
||||
@@ -446,6 +475,7 @@ export type SandboxSelect<
|
||||
{
|
||||
id?: boolean
|
||||
allowedDomains?: boolean
|
||||
pendingDomains?: boolean
|
||||
createdAt?: boolean
|
||||
updatedAt?: boolean
|
||||
teamId?: boolean
|
||||
@@ -460,6 +490,7 @@ export type SandboxSelectCreateManyAndReturn<
|
||||
{
|
||||
id?: boolean
|
||||
allowedDomains?: boolean
|
||||
pendingDomains?: boolean
|
||||
createdAt?: boolean
|
||||
updatedAt?: boolean
|
||||
teamId?: boolean
|
||||
@@ -474,6 +505,7 @@ export type SandboxSelectUpdateManyAndReturn<
|
||||
{
|
||||
id?: boolean
|
||||
allowedDomains?: boolean
|
||||
pendingDomains?: boolean
|
||||
createdAt?: boolean
|
||||
updatedAt?: boolean
|
||||
teamId?: boolean
|
||||
@@ -485,6 +517,7 @@ export type SandboxSelectUpdateManyAndReturn<
|
||||
export type SandboxSelectScalar = {
|
||||
id?: boolean
|
||||
allowedDomains?: boolean
|
||||
pendingDomains?: boolean
|
||||
createdAt?: boolean
|
||||
updatedAt?: boolean
|
||||
teamId?: boolean
|
||||
@@ -493,7 +526,7 @@ export type SandboxSelectScalar = {
|
||||
export type SandboxOmit<
|
||||
ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs,
|
||||
> = runtime.Types.Extensions.GetOmit<
|
||||
'id' | 'allowedDomains' | 'createdAt' | 'updatedAt' | 'teamId',
|
||||
'id' | 'allowedDomains' | 'pendingDomains' | 'createdAt' | 'updatedAt' | 'teamId',
|
||||
ExtArgs['result']['sandbox']
|
||||
>
|
||||
export type SandboxInclude<
|
||||
@@ -523,6 +556,7 @@ export type $SandboxPayload<
|
||||
{
|
||||
id: string
|
||||
allowedDomains: string[]
|
||||
pendingDomains: string[]
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
teamId: string
|
||||
@@ -1088,6 +1122,7 @@ export interface Prisma__SandboxClient<
|
||||
export interface SandboxFieldRefs {
|
||||
readonly id: Prisma.FieldRef<'Sandbox', 'String'>
|
||||
readonly allowedDomains: Prisma.FieldRef<'Sandbox', 'String[]'>
|
||||
readonly pendingDomains: Prisma.FieldRef<'Sandbox', 'String[]'>
|
||||
readonly createdAt: Prisma.FieldRef<'Sandbox', 'DateTime'>
|
||||
readonly updatedAt: Prisma.FieldRef<'Sandbox', 'DateTime'>
|
||||
readonly teamId: Prisma.FieldRef<'Sandbox', 'String'>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/dtos",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
|
||||
@@ -79,6 +79,7 @@ export type VideoTranscodeStrategy =
|
||||
|
||||
export const sandboxSettingsSchema = z.object({
|
||||
allowedDomains: z.array(z.string()),
|
||||
pendingDomains: z.array(z.string()).default([]),
|
||||
})
|
||||
export type SandboxSettings = z.infer<typeof sandboxSettingsSchema>
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/transcode",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
|
||||
@@ -10,6 +10,9 @@ import {
|
||||
takeScreenshotsActivity,
|
||||
transcodeVideoActivity,
|
||||
updateAssetMediaActivity,
|
||||
downloadMediaToTmpActivity,
|
||||
transcodeImageActivity,
|
||||
generateSpriteActivity,
|
||||
} from './transcode'
|
||||
|
||||
vi.mock('@shumai/core/src/s3/s3', () => ({
|
||||
@@ -27,6 +30,8 @@ vi.mock('../transcode', () => ({
|
||||
getVideoInfo: vi.fn(),
|
||||
getImageInfo: vi.fn(),
|
||||
transcodeVideo: vi.fn(),
|
||||
transcodeImage: vi.fn(),
|
||||
generateSprite: vi.fn(),
|
||||
createTempDir: vi.fn().mockReturnValue('/tmp'),
|
||||
removeDir: vi.fn(),
|
||||
takeScreenshots: vi.fn(),
|
||||
@@ -258,4 +263,106 @@ describe('Transcode Activities', () => {
|
||||
})
|
||||
expect(result).toBe('annotations/ann.webp')
|
||||
})
|
||||
|
||||
describe('Non-retryable Error Handling', () => {
|
||||
it('should throw non-retryable ApplicationFailure when downloadMediaToTmpActivity fails with ENOENT', async () => {
|
||||
vi.mocked(s3Service.downloadToFile).mockRejectedValue({
|
||||
code: 'ENOENT',
|
||||
message: 'ENOENT: no such file or directory',
|
||||
})
|
||||
|
||||
await expect(downloadMediaToTmpActivity({ assetKey: 'missing.mp4' })).rejects.toThrowError(
|
||||
/Failed to download media to tmp/,
|
||||
)
|
||||
})
|
||||
|
||||
it('should throw non-retryable ApplicationFailure when getMediaInfoActivity fails with ENOENT', async () => {
|
||||
const asset = await prisma.asset.create({
|
||||
data: { name: 'missing.mp4', type: 'file', status: 'uploaded' },
|
||||
})
|
||||
vi.mocked(transcodeService.getVideoInfo).mockRejectedValue({
|
||||
code: 'ENOENT',
|
||||
message: 'ENOENT: no such file or directory',
|
||||
})
|
||||
|
||||
await expect(
|
||||
getMediaInfoActivity({
|
||||
assetId: asset.id,
|
||||
filePath: '/tmp/missing.mp4',
|
||||
mediaType: 'video/mp4',
|
||||
}),
|
||||
).rejects.toThrowError(/Failed to get media info/)
|
||||
})
|
||||
|
||||
it('should throw non-retryable ApplicationFailure when transcodeVideoActivity fails with ffmpeg error', async () => {
|
||||
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
||||
vi.mocked(transcodeService.transcodeVideo).mockRejectedValue(new Error('FFmpeg failed'))
|
||||
|
||||
await expect(
|
||||
transcodeVideoActivity({
|
||||
assetKey: 'v.mp4',
|
||||
filePath: '/tmp/v.mp4',
|
||||
videoSpec: { resolution: '720p', width: 1280, height: 720 },
|
||||
duration: 10,
|
||||
originalFps: 30,
|
||||
}),
|
||||
).rejects.toThrowError(/Video transcoding failed/)
|
||||
})
|
||||
|
||||
it('should throw non-retryable ApplicationFailure when transcodeImageActivity fails with sharp error', async () => {
|
||||
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
||||
vi.mocked(transcodeService.transcodeImage).mockRejectedValue(new Error('sharp failed'))
|
||||
|
||||
await expect(
|
||||
transcodeImageActivity({
|
||||
assetKey: 'i.png',
|
||||
filePath: '/tmp/i.png',
|
||||
imageSpec: { width: 800, height: 600, quality: 90, format: 'webp' },
|
||||
}),
|
||||
).rejects.toThrowError(/Image transcoding failed/)
|
||||
})
|
||||
|
||||
it('should throw non-retryable ApplicationFailure when generateSpriteActivity fails with error', async () => {
|
||||
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
||||
vi.mocked(transcodeService.generateSprite).mockRejectedValue(new Error('FFmpeg failed'))
|
||||
|
||||
await expect(
|
||||
generateSpriteActivity({
|
||||
assetKey: 'v.mp4',
|
||||
filePath: '/tmp/v.mp4',
|
||||
mediaInfo: { duration: 10 } as unknown as PrismaJson.MediaInfo,
|
||||
spriteSpec: { key: 'sprite.webp', frames: 100, tileX: 10, tileY: 10 },
|
||||
posterSpec: { key: 'poster.webp' },
|
||||
}),
|
||||
).rejects.toThrowError(/Sprite\/Poster generation failed/)
|
||||
})
|
||||
|
||||
it('should throw non-retryable ApplicationFailure when takeScreenshotsActivity fails with error', async () => {
|
||||
vi.mocked(transcodeService.takeScreenshots).mockRejectedValue(new Error('FFmpeg failed'))
|
||||
|
||||
await expect(
|
||||
takeScreenshotsActivity({
|
||||
assetKey: 'v.mp4',
|
||||
assetId: 'asset-1',
|
||||
start: 0,
|
||||
end: 10,
|
||||
count: 1,
|
||||
commentTimestamp: 1.0,
|
||||
annotations: [],
|
||||
}),
|
||||
).rejects.toThrowError(/Screenshot extraction failed/)
|
||||
})
|
||||
|
||||
it('should throw non-retryable ApplicationFailure when overlayAnnotationsActivity fails with error', async () => {
|
||||
vi.mocked(transcodeService.overlayAnnotations).mockRejectedValue(new Error('sharp failed'))
|
||||
|
||||
await expect(
|
||||
overlayAnnotationsActivity({
|
||||
assetKey: 'i.png',
|
||||
assetId: 'asset-2',
|
||||
annotations: [],
|
||||
}),
|
||||
).rejects.toThrowError(/Overlay annotations failed/)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -12,107 +12,133 @@ export interface GetMediaInfoActivityParams {
|
||||
mediaType: string
|
||||
}
|
||||
|
||||
function getErrorDetails(err: unknown): { code?: string; message: string; name?: string } {
|
||||
if (err && typeof err === 'object') {
|
||||
const record = err as Record<string, unknown>
|
||||
return {
|
||||
code: typeof record.code === 'string' ? record.code : undefined,
|
||||
message: typeof record.message === 'string' ? record.message : String(err),
|
||||
name: typeof record.name === 'string' ? record.name : undefined,
|
||||
}
|
||||
}
|
||||
return {
|
||||
message: String(err),
|
||||
}
|
||||
}
|
||||
|
||||
export async function getMediaInfoActivity(params: {
|
||||
filePath: string
|
||||
assetId: string
|
||||
mediaType: string
|
||||
}): Promise<PrismaJson.MediaInfo> {
|
||||
const isVideo = params.mediaType.startsWith('video/')
|
||||
const isImage = params.mediaType.startsWith('image/')
|
||||
const isAudio = params.mediaType.startsWith('audio/')
|
||||
const isDocument =
|
||||
params.mediaType.startsWith('text/') ||
|
||||
params.mediaType === 'application/pdf' ||
|
||||
params.mediaType.includes('msword') ||
|
||||
params.mediaType.includes('officedocument') ||
|
||||
params.mediaType.includes('vnd.ms-')
|
||||
try {
|
||||
const isVideo = params.mediaType.startsWith('video/')
|
||||
const isImage = params.mediaType.startsWith('image/')
|
||||
const isAudio = params.mediaType.startsWith('audio/')
|
||||
const isDocument =
|
||||
params.mediaType.startsWith('text/') ||
|
||||
params.mediaType === 'application/pdf' ||
|
||||
params.mediaType.includes('msword') ||
|
||||
params.mediaType.includes('officedocument') ||
|
||||
params.mediaType.includes('vnd.ms-')
|
||||
|
||||
const fileType = isVideo
|
||||
? 'video'
|
||||
: isAudio
|
||||
? 'audio'
|
||||
: isImage
|
||||
? 'image'
|
||||
: isDocument
|
||||
? 'document'
|
||||
: 'file'
|
||||
const fileType = isVideo
|
||||
? 'video'
|
||||
: isAudio
|
||||
? 'audio'
|
||||
: isImage
|
||||
? 'image'
|
||||
: isDocument
|
||||
? 'document'
|
||||
: 'file'
|
||||
|
||||
const mediaInfo: PrismaJson.MediaInfo = {
|
||||
duration: 0,
|
||||
filesize: 0,
|
||||
fps: 0,
|
||||
frames: 0,
|
||||
imageTranscodes: [],
|
||||
videoTranscodes: [],
|
||||
videoPreview: { width: 0, height: 0 },
|
||||
finishedAt: new Date().toISOString(),
|
||||
metadata: null,
|
||||
mimeType: params.mediaType,
|
||||
original: {
|
||||
key: '', // Will be filled by caller or updated later
|
||||
downloadUrl: '',
|
||||
filesizeInBytes: 0,
|
||||
codec: '',
|
||||
},
|
||||
}
|
||||
|
||||
const metadataUpdates: { key: string; value: string | number }[] = [
|
||||
{ key: 'file_type', value: fileType },
|
||||
]
|
||||
|
||||
if (isVideo) {
|
||||
const info = await transcodeService.getVideoInfo(params.filePath)
|
||||
mediaInfo.duration = info.duration
|
||||
mediaInfo.fps = info.frameRate
|
||||
mediaInfo.metadata = {
|
||||
originalWidth: info.originalWidth,
|
||||
originalHeight: info.originalHeight,
|
||||
duration: info.duration,
|
||||
bitRate: info.bitRate,
|
||||
frameRate: info.frameRate,
|
||||
hasAudio: info.hasAudio,
|
||||
videoCodec: info.videoCodec,
|
||||
audioCodec: info.audioCodec,
|
||||
audioChannels: info.audioChannels,
|
||||
audioSampleRate: info.audioSampleRate,
|
||||
audioBitDepth: info.audioBitDepth,
|
||||
format: {},
|
||||
}
|
||||
metadataUpdates.push(
|
||||
{ key: 'resolution_width', value: info.originalWidth },
|
||||
{ key: 'resolution_height', value: info.originalHeight },
|
||||
{ key: 'duration', value: info.duration },
|
||||
{ key: 'bitRate', value: info.bitRate / 1000 },
|
||||
{ key: 'frame_rate', value: info.frameRate },
|
||||
)
|
||||
if (info.videoCodec) metadataUpdates.push({ key: 'video_codec', value: info.videoCodec })
|
||||
if (info.audioCodec) metadataUpdates.push({ key: 'audio_codec', value: info.audioCodec })
|
||||
if (info.audioChannels !== undefined)
|
||||
metadataUpdates.push({ key: 'audio_channels', value: info.audioChannels })
|
||||
if (info.audioSampleRate !== undefined)
|
||||
metadataUpdates.push({ key: 'audio_sample_rate', value: info.audioSampleRate })
|
||||
if (info.audioBitDepth !== undefined)
|
||||
metadataUpdates.push({ key: 'audio_bit_depth', value: info.audioBitDepth })
|
||||
} else if (isImage) {
|
||||
const info = await transcodeService.getImageInfo(params.filePath)
|
||||
mediaInfo.metadata = {
|
||||
originalWidth: info.originalWidth,
|
||||
originalHeight: info.originalHeight,
|
||||
const mediaInfo: PrismaJson.MediaInfo = {
|
||||
duration: 0,
|
||||
bitRate: 0,
|
||||
frameRate: 0,
|
||||
hasAudio: false,
|
||||
format: {},
|
||||
filesize: 0,
|
||||
fps: 0,
|
||||
frames: 0,
|
||||
imageTranscodes: [],
|
||||
videoTranscodes: [],
|
||||
videoPreview: { width: 0, height: 0 },
|
||||
finishedAt: new Date().toISOString(),
|
||||
metadata: null,
|
||||
mimeType: params.mediaType,
|
||||
original: {
|
||||
key: '', // Will be filled by caller or updated later
|
||||
downloadUrl: '',
|
||||
filesizeInBytes: 0,
|
||||
codec: '',
|
||||
},
|
||||
}
|
||||
metadataUpdates.push(
|
||||
{ key: 'resolution_width', value: info.originalWidth },
|
||||
{ key: 'resolution_height', value: info.originalHeight },
|
||||
)
|
||||
|
||||
const metadataUpdates: { key: string; value: string | number }[] = [
|
||||
{ key: 'file_type', value: fileType },
|
||||
]
|
||||
|
||||
if (isVideo) {
|
||||
const info = await transcodeService.getVideoInfo(params.filePath)
|
||||
mediaInfo.duration = info.duration
|
||||
mediaInfo.fps = info.frameRate
|
||||
mediaInfo.metadata = {
|
||||
originalWidth: info.originalWidth,
|
||||
originalHeight: info.originalHeight,
|
||||
duration: info.duration,
|
||||
bitRate: info.bitRate,
|
||||
frameRate: info.frameRate,
|
||||
hasAudio: info.hasAudio,
|
||||
videoCodec: info.videoCodec,
|
||||
audioCodec: info.audioCodec,
|
||||
audioChannels: info.audioChannels,
|
||||
audioSampleRate: info.audioSampleRate,
|
||||
audioBitDepth: info.audioBitDepth,
|
||||
format: {},
|
||||
}
|
||||
metadataUpdates.push(
|
||||
{ key: 'resolution_width', value: info.originalWidth },
|
||||
{ key: 'resolution_height', value: info.originalHeight },
|
||||
{ key: 'duration', value: info.duration },
|
||||
{ key: 'bitRate', value: info.bitRate / 1000 },
|
||||
{ key: 'frame_rate', value: info.frameRate },
|
||||
)
|
||||
if (info.videoCodec) metadataUpdates.push({ key: 'video_codec', value: info.videoCodec })
|
||||
if (info.audioCodec) metadataUpdates.push({ key: 'audio_codec', value: info.audioCodec })
|
||||
if (info.audioChannels !== undefined)
|
||||
metadataUpdates.push({ key: 'audio_channels', value: info.audioChannels })
|
||||
if (info.audioSampleRate !== undefined)
|
||||
metadataUpdates.push({ key: 'audio_sample_rate', value: info.audioSampleRate })
|
||||
if (info.audioBitDepth !== undefined)
|
||||
metadataUpdates.push({ key: 'audio_bit_depth', value: info.audioBitDepth })
|
||||
} else if (isImage) {
|
||||
const info = await transcodeService.getImageInfo(params.filePath)
|
||||
mediaInfo.metadata = {
|
||||
originalWidth: info.originalWidth,
|
||||
originalHeight: info.originalHeight,
|
||||
duration: 0,
|
||||
bitRate: 0,
|
||||
frameRate: 0,
|
||||
hasAudio: false,
|
||||
format: {},
|
||||
}
|
||||
metadataUpdates.push(
|
||||
{ key: 'resolution_width', value: info.originalWidth },
|
||||
{ key: 'resolution_height', value: info.originalHeight },
|
||||
)
|
||||
}
|
||||
|
||||
await metadataService.updateAssetMetadata(params.assetId, metadataUpdates, true)
|
||||
|
||||
return mediaInfo
|
||||
} catch (err) {
|
||||
const { name, message } = getErrorDetails(err)
|
||||
if (name?.includes('Prisma') || message.includes('Prisma')) {
|
||||
throw err
|
||||
}
|
||||
throw ApplicationFailure.create({
|
||||
message: `Failed to get media info: ${message}`,
|
||||
nonRetryable: true,
|
||||
cause: err instanceof Error ? err : undefined,
|
||||
})
|
||||
}
|
||||
|
||||
await metadataService.updateAssetMetadata(params.assetId, metadataUpdates, true)
|
||||
|
||||
return mediaInfo
|
||||
}
|
||||
|
||||
export interface VideoActivityParams {
|
||||
@@ -167,6 +193,26 @@ export async function transcodeVideoActivity(
|
||||
await s3Service.putObject(bucket, key, buffer, buffer.length, 'video/mp4')
|
||||
|
||||
return { ...params.videoSpec, key }
|
||||
} catch (err) {
|
||||
const { code, message } = getErrorDetails(err)
|
||||
const lowerMsg = message.toLowerCase()
|
||||
|
||||
if (
|
||||
code === 'ENOENT' ||
|
||||
lowerMsg.includes('enoent') ||
|
||||
lowerMsg.includes('ffmpeg') ||
|
||||
lowerMsg.includes('ffprobe') ||
|
||||
lowerMsg.includes('spawn') ||
|
||||
lowerMsg.includes('format') ||
|
||||
lowerMsg.includes('no video stream found')
|
||||
) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `Video transcoding failed: ${message}`,
|
||||
nonRetryable: true,
|
||||
cause: err instanceof Error ? err : undefined,
|
||||
})
|
||||
}
|
||||
throw err
|
||||
} finally {
|
||||
if (fs.existsSync(outputFile)) fs.unlinkSync(outputFile)
|
||||
}
|
||||
@@ -205,6 +251,24 @@ export async function transcodeImageActivity(
|
||||
await s3Service.putObject(bucket, key, buffer, buffer.length, 'image/webp')
|
||||
|
||||
return { ...params.imageSpec, key }
|
||||
} catch (err) {
|
||||
const { code, message } = getErrorDetails(err)
|
||||
const lowerMsg = message.toLowerCase()
|
||||
|
||||
if (
|
||||
code === 'ENOENT' ||
|
||||
lowerMsg.includes('enoent') ||
|
||||
lowerMsg.includes('sharp') ||
|
||||
lowerMsg.includes('spawn') ||
|
||||
lowerMsg.includes('format')
|
||||
) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `Image transcoding failed: ${message}`,
|
||||
nonRetryable: true,
|
||||
cause: err instanceof Error ? err : undefined,
|
||||
})
|
||||
}
|
||||
throw err
|
||||
} finally {
|
||||
if (fs.existsSync(outputFile)) fs.unlinkSync(outputFile)
|
||||
}
|
||||
@@ -259,6 +323,25 @@ export async function generateSpriteActivity(params: GenerateSpriteActivityParam
|
||||
)
|
||||
|
||||
return { sprite: params.spriteSpec, poster: params.posterSpec }
|
||||
} catch (err) {
|
||||
const { code, message } = getErrorDetails(err)
|
||||
const lowerMsg = message.toLowerCase()
|
||||
|
||||
if (
|
||||
code === 'ENOENT' ||
|
||||
lowerMsg.includes('enoent') ||
|
||||
lowerMsg.includes('ffmpeg') ||
|
||||
lowerMsg.includes('ffprobe') ||
|
||||
lowerMsg.includes('spawn') ||
|
||||
lowerMsg.includes('format')
|
||||
) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `Sprite/Poster generation failed: ${message}`,
|
||||
nonRetryable: true,
|
||||
cause: err instanceof Error ? err : undefined,
|
||||
})
|
||||
}
|
||||
throw err
|
||||
} finally {
|
||||
if (fs.existsSync(spriteFile)) fs.unlinkSync(spriteFile)
|
||||
if (fs.existsSync(posterFile)) fs.unlinkSync(posterFile)
|
||||
@@ -276,7 +359,20 @@ export async function downloadMediaToTmpActivity(params: {
|
||||
fs.mkdirSync(tmpDir, { recursive: true })
|
||||
}
|
||||
|
||||
await s3Service.downloadToFile(bucket, params.assetKey, filePath)
|
||||
try {
|
||||
await s3Service.downloadToFile(bucket, params.assetKey, filePath)
|
||||
} catch (err) {
|
||||
const { code, message } = getErrorDetails(err)
|
||||
const lowerMsg = message.toLowerCase()
|
||||
if (code === 'ENOENT' || lowerMsg.includes('enoent') || lowerMsg.includes('nosuchkey')) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `Failed to download media to tmp: ${message}`,
|
||||
nonRetryable: true,
|
||||
cause: err instanceof Error ? err : undefined,
|
||||
})
|
||||
}
|
||||
throw err
|
||||
}
|
||||
|
||||
return { filePath, tmpDir }
|
||||
}
|
||||
@@ -324,7 +420,26 @@ export async function takeScreenshotsActivity(params: {
|
||||
commentTimestamp?: number | null
|
||||
annotations?: PrismaJson.AnnotationList | null
|
||||
}): Promise<Array<{ key: string; timestamp: number }>> {
|
||||
return transcodeService.takeScreenshots(params)
|
||||
try {
|
||||
return await transcodeService.takeScreenshots(params)
|
||||
} catch (err) {
|
||||
const { code, message } = getErrorDetails(err)
|
||||
const lowerMsg = message.toLowerCase()
|
||||
if (
|
||||
code === 'ENOENT' ||
|
||||
lowerMsg.includes('enoent') ||
|
||||
lowerMsg.includes('nosuchkey') ||
|
||||
lowerMsg.includes('ffmpeg') ||
|
||||
lowerMsg.includes('sharp')
|
||||
) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `Screenshot extraction failed: ${message}`,
|
||||
nonRetryable: true,
|
||||
cause: err instanceof Error ? err : undefined,
|
||||
})
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
export async function overlayAnnotationsActivity(params: {
|
||||
@@ -332,5 +447,24 @@ export async function overlayAnnotationsActivity(params: {
|
||||
assetId: string
|
||||
annotations: PrismaJson.AnnotationList
|
||||
}): Promise<string> {
|
||||
return transcodeService.overlayAnnotations(params)
|
||||
try {
|
||||
return await transcodeService.overlayAnnotations(params)
|
||||
} catch (err) {
|
||||
const { code, message } = getErrorDetails(err)
|
||||
const lowerMsg = message.toLowerCase()
|
||||
if (
|
||||
code === 'ENOENT' ||
|
||||
lowerMsg.includes('enoent') ||
|
||||
lowerMsg.includes('nosuchkey') ||
|
||||
lowerMsg.includes('ffmpeg') ||
|
||||
lowerMsg.includes('sharp')
|
||||
) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `Overlay annotations failed: ${message}`,
|
||||
nonRetryable: true,
|
||||
cause: err instanceof Error ? err : undefined,
|
||||
})
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,65 +240,67 @@ export function BreadcrumbNav({
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex flex-shrink-0 items-center gap-1 rounded-md border border-border bg-background p-1">
|
||||
{/* Left Sidebar Toggle */}
|
||||
{!isPublic && onLeftSidebarToggle && (
|
||||
{!fileId && (
|
||||
<div className="flex flex-shrink-0 items-center gap-1 rounded-md border border-border bg-background p-1">
|
||||
{/* Left Sidebar Toggle */}
|
||||
{!isPublic && onLeftSidebarToggle && (
|
||||
<button
|
||||
onClick={onLeftSidebarToggle}
|
||||
className="rounded p-1.5 text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground"
|
||||
title={isLeftSidebarCollapsed ? 'Show Left Sidebar' : 'Hide Left Sidebar'}
|
||||
>
|
||||
{isLeftSidebarCollapsed ? (
|
||||
<DockToLeft className="h-4 w-4" />
|
||||
) : (
|
||||
<DockToLeftFilled className="h-4 w-4 fill-primary stroke-0" />
|
||||
)}
|
||||
</button>
|
||||
)}
|
||||
|
||||
{/* Card View */}
|
||||
{!isPublic && displayStyle && onDisplayStyleChange && (
|
||||
<>
|
||||
<button
|
||||
onClick={() => onDisplayStyleChange('card')}
|
||||
className={`rounded p-1.5 transition-colors ${
|
||||
displayStyle === 'card'
|
||||
? 'bg-accent text-accent-foreground'
|
||||
: 'text-muted-foreground hover:bg-accent/50 hover:text-foreground'
|
||||
}`}
|
||||
title="Card View"
|
||||
>
|
||||
<LayoutGrid className="h-4 w-4" />
|
||||
</button>
|
||||
|
||||
{/* List View */}
|
||||
<button
|
||||
onClick={() => onDisplayStyleChange('list')}
|
||||
className={`rounded p-1.5 transition-colors ${
|
||||
displayStyle === 'list'
|
||||
? 'bg-accent text-accent-foreground'
|
||||
: 'text-muted-foreground hover:bg-accent/50 hover:text-foreground'
|
||||
}`}
|
||||
title="List View"
|
||||
>
|
||||
<List className="h-4 w-4" />
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* Right Sidebar Toggle */}
|
||||
<button
|
||||
onClick={onLeftSidebarToggle}
|
||||
onClick={onRightSidebarToggle}
|
||||
className="rounded p-1.5 text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground"
|
||||
title={isLeftSidebarCollapsed ? 'Show Left Sidebar' : 'Hide Left Sidebar'}
|
||||
title={isRightSidebarCollapsed ? 'Show Right Sidebar' : 'Hide Right Sidebar'}
|
||||
>
|
||||
{isLeftSidebarCollapsed ? (
|
||||
<DockToLeft className="h-4 w-4" />
|
||||
{isRightSidebarCollapsed ? (
|
||||
<DockToRight className="h-4 w-4" />
|
||||
) : (
|
||||
<DockToLeftFilled className="h-4 w-4 fill-primary stroke-0" />
|
||||
<DockToRightFilled className="h-4 w-4 fill-primary stroke-0" />
|
||||
)}
|
||||
</button>
|
||||
)}
|
||||
|
||||
{/* Card View */}
|
||||
{!isPublic && displayStyle && onDisplayStyleChange && (
|
||||
<>
|
||||
<button
|
||||
onClick={() => onDisplayStyleChange('card')}
|
||||
className={`rounded p-1.5 transition-colors ${
|
||||
displayStyle === 'card'
|
||||
? 'bg-accent text-accent-foreground'
|
||||
: 'text-muted-foreground hover:bg-accent/50 hover:text-foreground'
|
||||
}`}
|
||||
title="Card View"
|
||||
>
|
||||
<LayoutGrid className="h-4 w-4" />
|
||||
</button>
|
||||
|
||||
{/* List View */}
|
||||
<button
|
||||
onClick={() => onDisplayStyleChange('list')}
|
||||
className={`rounded p-1.5 transition-colors ${
|
||||
displayStyle === 'list'
|
||||
? 'bg-accent text-accent-foreground'
|
||||
: 'text-muted-foreground hover:bg-accent/50 hover:text-foreground'
|
||||
}`}
|
||||
title="List View"
|
||||
>
|
||||
<List className="h-4 w-4" />
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* Right Sidebar Toggle */}
|
||||
<button
|
||||
onClick={onRightSidebarToggle}
|
||||
className="rounded p-1.5 text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground"
|
||||
title={isRightSidebarCollapsed ? 'Show Right Sidebar' : 'Hide Right Sidebar'}
|
||||
>
|
||||
{isRightSidebarCollapsed ? (
|
||||
<DockToRight className="h-4 w-4" />
|
||||
) : (
|
||||
<DockToRightFilled className="h-4 w-4 fill-primary stroke-0" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import type { AttachmentInfo, CommentInfo } from '@shumai/dtos'
|
||||
import type { UserInfo } from '@shumai/dtos'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import {
|
||||
@@ -11,6 +9,7 @@ import {
|
||||
} from '@/ui/components/ui/dialog'
|
||||
import { Separator } from '@/ui/components/ui/separator'
|
||||
import { formatTimeAgo } from '@/ui/lib/time'
|
||||
import type { AttachmentInfo, CommentInfo, UserInfo } from '@shumai/dtos'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { Download, File, Terminal } from 'lucide-react'
|
||||
import React from 'react'
|
||||
@@ -191,11 +190,11 @@ export const MessageCard: React.FC<MessageCardProps> = ({
|
||||
<span className="text-xs text-muted-foreground animate-pulse">{loadingText}</span>
|
||||
</div>
|
||||
) : message.sessionId ? (
|
||||
<div className="text-sm leading-relaxed prose prose-sm dark:prose-invert max-w-none break-words">
|
||||
<div className="text-sm leading-[1.8] prose prose-sm dark:prose-invert max-w-none break-words">
|
||||
<Markdown>{preprocessMarkdown(message.message!)}</Markdown>
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-sm leading-relaxed whitespace-pre-wrap break-words text-wrap break-all">
|
||||
<div className="text-sm leading-[1.8] whitespace-pre-wrap break-words text-wrap break-all">
|
||||
{renderFormattedMessage(message.message!)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -5,6 +5,9 @@ import { client } from '@/ui/api/client'
|
||||
import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
||||
import type { Annotation } from '@/ui/types'
|
||||
import { useMutation } from '@tanstack/react-query'
|
||||
import { Skeleton } from '../ui/skeleton'
|
||||
import { useMemberStore } from '@/ui/stores/members'
|
||||
import { useTeamContextStore } from '@/ui/stores/team-context'
|
||||
import {
|
||||
ArrowLeft,
|
||||
ArrowUp,
|
||||
@@ -45,7 +48,6 @@ interface ChatInputProps {
|
||||
) => void
|
||||
replyingTo?: CommentInfo | null
|
||||
onCancelReply?: () => void
|
||||
users?: UserInfo[]
|
||||
bots?: BotInfo[]
|
||||
initialText?: string
|
||||
hideAnnotationControl?: boolean
|
||||
@@ -76,7 +78,6 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
|
||||
onSendMessage,
|
||||
replyingTo,
|
||||
onCancelReply,
|
||||
users = [],
|
||||
bots = [],
|
||||
initialText = '',
|
||||
hideAnnotationControl = false,
|
||||
@@ -91,6 +92,15 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
|
||||
const [viewingFile, setViewingFile] = useState<File | null>(null)
|
||||
const [isTimestampEnabled, setIsTimestampEnabled] = useState(true)
|
||||
|
||||
const { teamId, ensureTeamIdForProject } = useTeamContextStore()
|
||||
const { members: storeMembers, loading: membersLoading, fetchMembers } = useMemberStore()
|
||||
|
||||
useEffect(() => {
|
||||
if (projectId && !teamId) {
|
||||
ensureTeamIdForProject(projectId)
|
||||
}
|
||||
}, [projectId, teamId, ensureTeamIdForProject])
|
||||
|
||||
// Annotation Store
|
||||
const {
|
||||
isDrawing,
|
||||
@@ -171,7 +181,7 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
|
||||
}, [setIsDrawing])
|
||||
|
||||
const filteredAgents = [
|
||||
...users
|
||||
...storeMembers
|
||||
.filter(
|
||||
(u) => u.type === 'agent' && u.name?.toLowerCase().startsWith(mentionQuery.toLowerCase()),
|
||||
)
|
||||
@@ -181,7 +191,7 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
|
||||
.map((b) => ({ type: 'bot' as const, data: b })),
|
||||
]
|
||||
|
||||
const filteredHumans = users
|
||||
const filteredHumans = storeMembers
|
||||
.filter(
|
||||
(u) => u.type !== 'agent' && u.name?.toLowerCase().startsWith(mentionQuery.toLowerCase()),
|
||||
)
|
||||
@@ -230,6 +240,10 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
|
||||
setMentionQuery(query)
|
||||
setShowMentionList(true)
|
||||
|
||||
if (teamId) {
|
||||
fetchMembers(teamId, true, true)
|
||||
}
|
||||
|
||||
// Save the range for later insertion
|
||||
const mentionRange = document.createRange()
|
||||
mentionRange.setStart(node, lastAtPos)
|
||||
@@ -443,90 +457,126 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
|
||||
cursor: text;
|
||||
}
|
||||
`}</style>
|
||||
{showMentionList && (filteredAgents.length > 0 || filteredHumans.length > 0) && (
|
||||
<div className="absolute bottom-full left-4 mb-2 w-64 rounded-xl shadow-lg border border-border overflow-hidden z-20 bg-popover text-popover-foreground animate-in fade-in slide-in-from-bottom-2 duration-200">
|
||||
<div className="h-64 overflow-y-auto">
|
||||
{filteredAgents.length > 0 && (
|
||||
<>
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
setCollapsedSections((prev) => ({ ...prev, bots: !prev.bots }))
|
||||
}}
|
||||
className="w-full text-xs font-semibold text-muted-foreground px-3 py-2 bg-muted/50 border-b border-border sticky top-0 flex items-center justify-between hover:bg-muted transition-colors"
|
||||
>
|
||||
<span>Agents</span>
|
||||
<ChevronDown
|
||||
className={`w-3 h-3 transition-transform ${collapsedSections.bots ? '-rotate-90' : ''}`}
|
||||
/>
|
||||
</button>
|
||||
{!collapsedSections.bots &&
|
||||
filteredAgents.map((item, index) => {
|
||||
const actualIndex = index
|
||||
return (
|
||||
{showMentionList &&
|
||||
(membersLoading || filteredAgents.length > 0 || filteredHumans.length > 0) && (
|
||||
<div className="absolute bottom-full left-4 mb-2 w-64 rounded-xl shadow-lg border border-border overflow-hidden z-20 bg-popover text-popover-foreground animate-in fade-in slide-in-from-bottom-2 duration-200">
|
||||
<div className="h-64 overflow-y-auto">
|
||||
{membersLoading ? (
|
||||
<div className="p-2 space-y-4 animate-pulse">
|
||||
{/* Agents Skeleton */}
|
||||
<div className="space-y-2">
|
||||
<div className="px-3 py-1.5 bg-muted/50 border-b border-border">
|
||||
<Skeleton className="h-3 w-16" />
|
||||
</div>
|
||||
<div className="flex items-center gap-2 px-3 py-2">
|
||||
<Skeleton className="w-6 h-6 rounded-full" />
|
||||
<Skeleton className="h-4 w-28" />
|
||||
</div>
|
||||
<div className="flex items-center gap-2 px-3 py-2">
|
||||
<Skeleton className="w-6 h-6 rounded-full" />
|
||||
<Skeleton className="h-4 w-20" />
|
||||
</div>
|
||||
</div>
|
||||
{/* Members Skeleton */}
|
||||
<div className="space-y-2">
|
||||
<div className="px-3 py-1.5 bg-muted/50 border-b border-border">
|
||||
<Skeleton className="h-3 w-16" />
|
||||
</div>
|
||||
<div className="flex items-center gap-2 px-3 py-2">
|
||||
<Skeleton className="w-6 h-6 rounded-full" />
|
||||
<Skeleton className="h-4 w-32" />
|
||||
</div>
|
||||
<div className="flex items-center gap-2 px-3 py-2">
|
||||
<Skeleton className="w-6 h-6 rounded-full" />
|
||||
<Skeleton className="h-4 w-24" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{filteredAgents.length > 0 && (
|
||||
<>
|
||||
<button
|
||||
key={item.data.id}
|
||||
onClick={() => handleSelectEntity(item)}
|
||||
onMouseEnter={() => setHighlightedIndex(actualIndex)}
|
||||
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors text-left ${
|
||||
actualIndex === highlightedIndex
|
||||
? 'bg-accent text-accent-foreground'
|
||||
: 'hover:bg-accent/50'
|
||||
}`}
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
setCollapsedSections((prev) => ({ ...prev, bots: !prev.bots }))
|
||||
}}
|
||||
className="w-full text-xs font-semibold text-muted-foreground px-3 py-2 bg-muted/50 border-b border-border sticky top-0 flex items-center justify-between hover:bg-muted transition-colors"
|
||||
>
|
||||
<div className="w-6 h-6 rounded-full bg-purple-100 dark:bg-purple-900/40 flex items-center justify-center text-xs font-bold text-purple-600 dark:text-purple-300">
|
||||
{item.data.name?.[0]}
|
||||
</div>
|
||||
<span className="text-sm">{item.data.name}</span>
|
||||
<span>Agents</span>
|
||||
<ChevronDown
|
||||
className={`w-3 h-3 transition-transform ${collapsedSections.bots ? '-rotate-90' : ''}`}
|
||||
/>
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</>
|
||||
)}
|
||||
{!collapsedSections.bots &&
|
||||
filteredAgents.map((item, index) => {
|
||||
const actualIndex = index
|
||||
return (
|
||||
<button
|
||||
key={item.data.id}
|
||||
onClick={() => handleSelectEntity(item)}
|
||||
onMouseEnter={() => setHighlightedIndex(actualIndex)}
|
||||
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors text-left ${
|
||||
actualIndex === highlightedIndex
|
||||
? 'bg-accent text-accent-foreground'
|
||||
: 'hover:bg-accent/50'
|
||||
}`}
|
||||
>
|
||||
<div className="w-6 h-6 rounded-full bg-purple-100 dark:bg-purple-900/40 flex items-center justify-center text-xs font-bold text-purple-600 dark:text-purple-300">
|
||||
{item.data.name?.[0]}
|
||||
</div>
|
||||
<span className="text-sm">{item.data.name}</span>
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</>
|
||||
)}
|
||||
|
||||
{filteredHumans.length > 0 && (
|
||||
<>
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
setCollapsedSections((prev) => ({ ...prev, users: !prev.users }))
|
||||
}}
|
||||
className="w-full text-xs font-semibold text-muted-foreground px-3 py-2 bg-muted/50 border-b border-border sticky top-0 flex items-center justify-between hover:bg-muted transition-colors"
|
||||
>
|
||||
<span>Members</span>
|
||||
<ChevronDown
|
||||
className={`w-3 h-3 transition-transform ${collapsedSections.users ? '-rotate-90' : ''}`}
|
||||
/>
|
||||
</button>
|
||||
{!collapsedSections.users &&
|
||||
filteredHumans.map((item, index) => {
|
||||
const actualIndex =
|
||||
(collapsedSections.bots ? 0 : filteredAgents.length) + index
|
||||
return (
|
||||
{filteredHumans.length > 0 && (
|
||||
<>
|
||||
<button
|
||||
key={item.data.id}
|
||||
onClick={() => handleSelectEntity(item)}
|
||||
onMouseEnter={() => setHighlightedIndex(actualIndex)}
|
||||
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors text-left ${
|
||||
actualIndex === highlightedIndex
|
||||
? 'bg-accent text-accent-foreground'
|
||||
: 'hover:bg-accent/50'
|
||||
}`}
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
setCollapsedSections((prev) => ({ ...prev, users: !prev.users }))
|
||||
}}
|
||||
className="w-full text-xs font-semibold text-muted-foreground px-3 py-2 bg-muted/50 border-b border-border sticky top-0 flex items-center justify-between hover:bg-muted transition-colors"
|
||||
>
|
||||
<div className="w-6 h-6 rounded-full bg-blue-100 dark:bg-blue-900/40 flex items-center justify-center text-xs font-bold text-blue-600 dark:text-blue-300">
|
||||
{item.data.name?.[0]}
|
||||
</div>
|
||||
<span className="text-sm">{item.data.name}</span>
|
||||
<span>Members</span>
|
||||
<ChevronDown
|
||||
className={`w-3 h-3 transition-transform ${collapsedSections.users ? '-rotate-90' : ''}`}
|
||||
/>
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</>
|
||||
)}
|
||||
{!collapsedSections.users &&
|
||||
filteredHumans.map((item, index) => {
|
||||
const actualIndex =
|
||||
(collapsedSections.bots ? 0 : filteredAgents.length) + index
|
||||
return (
|
||||
<button
|
||||
key={item.data.id}
|
||||
onClick={() => handleSelectEntity(item)}
|
||||
onMouseEnter={() => setHighlightedIndex(actualIndex)}
|
||||
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors text-left ${
|
||||
actualIndex === highlightedIndex
|
||||
? 'bg-accent text-accent-foreground'
|
||||
: 'hover:bg-accent/50'
|
||||
}`}
|
||||
>
|
||||
<div className="w-6 h-6 rounded-full bg-blue-100 dark:bg-blue-900/40 flex items-center justify-center text-xs font-bold text-blue-600 dark:text-blue-300">
|
||||
{item.data.name?.[0]}
|
||||
</div>
|
||||
<span className="text-sm">{item.data.name}</span>
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
)}
|
||||
|
||||
{showUpperPart && (
|
||||
<div className="flex flex-col gap-2 p-3 pb-0 animate-in fade-in slide-in-from-bottom-2 duration-200">
|
||||
|
||||
@@ -853,6 +853,7 @@ export function FileBrowser({
|
||||
<div
|
||||
ref={scrollContainerRef}
|
||||
className="flex-1 overflow-y-auto min-h-0 relative flex flex-col"
|
||||
style={{ overflowAnchor: 'none' }}
|
||||
>
|
||||
{displayStyle === 'list' ? (
|
||||
<FileBrowserListView
|
||||
|
||||
@@ -12,7 +12,7 @@ import { useMemberStore } from '@/ui/stores/members'
|
||||
import { useTopNavStore } from '@/ui/stores/top-nav'
|
||||
import { useUiStore } from '@/ui/stores/ui'
|
||||
import { useUserMetadataStore } from '@/ui/stores/user-metadata'
|
||||
import { PointerActivationConstraints } from '@dnd-kit/dom'
|
||||
import { Feedback, PointerActivationConstraints } from '@dnd-kit/dom'
|
||||
import { DragDropProvider, DragOverlay, KeyboardSensor, PointerSensor } from '@dnd-kit/react'
|
||||
import { useInfiniteQuery, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { useNavigate } from '@tanstack/react-router'
|
||||
@@ -400,6 +400,17 @@ export default function FileSystemManager({
|
||||
return (
|
||||
<DragDropProvider
|
||||
modifiers={[SnapToPointer.configure({ anchor: { x: 0, y: 0 } })]}
|
||||
plugins={(defaults) =>
|
||||
defaults.map((p) => {
|
||||
if (p === Feedback) {
|
||||
return Feedback.configure({ dropAnimation: null })
|
||||
}
|
||||
if (typeof p === 'object' && p !== null && 'plugin' in p && p.plugin === Feedback) {
|
||||
return Feedback.configure({ dropAnimation: null })
|
||||
}
|
||||
return p
|
||||
})
|
||||
}
|
||||
sensors={
|
||||
isRecentlyDeleted || !canEdit
|
||||
? []
|
||||
|
||||
@@ -1,26 +1,40 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import { useInfiniteQuery } from '@tanstack/react-query'
|
||||
import { Loader2, PanelLeftClose } from 'lucide-react'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import type { AssetInfo, AssetInfoPaginatedList } from '@shumai/dtos'
|
||||
import { Link } from '@tanstack/react-router'
|
||||
import { File, Loader2 } from 'lucide-react'
|
||||
import type { FC } from 'react'
|
||||
import type { AssetInfoPaginatedList } from '@shumai/dtos'
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { useInView } from 'react-intersection-observer'
|
||||
import { SidebarFileCard } from './sidebar-file-card'
|
||||
import { Button } from './ui/button'
|
||||
import { ScrollArea } from './ui/scroll-area'
|
||||
|
||||
interface FileViewerLeftSidebarProps {
|
||||
projectId: string
|
||||
currentAssetId: string
|
||||
parentFolderId: string
|
||||
onCollapse: () => void
|
||||
initialFiles: AssetInfo[]
|
||||
initialNextCursor: string | undefined
|
||||
}
|
||||
|
||||
export const CarouselFilePreview = ({ item }: { item: AssetInfo }) => {
|
||||
if (item.preview?.thumbnailUrl) {
|
||||
return (
|
||||
<img src={item.preview.thumbnailUrl} alt="Preview" className="w-full h-full object-cover" />
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-full h-full flex items-center justify-center bg-muted">
|
||||
<File className="w-5 h-5 text-muted-foreground" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export const FileViewerLeftSidebar: FC<FileViewerLeftSidebarProps> = ({
|
||||
projectId,
|
||||
currentAssetId,
|
||||
parentFolderId,
|
||||
onCollapse,
|
||||
initialFiles,
|
||||
initialNextCursor,
|
||||
}) => {
|
||||
const { ref, inView } = useInView({
|
||||
threshold: 0,
|
||||
@@ -29,38 +43,51 @@ export const FileViewerLeftSidebar: FC<FileViewerLeftSidebarProps> = ({
|
||||
const activeItemRef = useRef<HTMLDivElement>(null)
|
||||
const hasScrolledRef = useRef(false)
|
||||
|
||||
const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } = useInfiniteQuery({
|
||||
queryKey: [
|
||||
'folders',
|
||||
parentFolderId,
|
||||
'children',
|
||||
{
|
||||
assetType: 'file',
|
||||
},
|
||||
],
|
||||
queryFn: async ({ pageParam }) => {
|
||||
const res = await client.api.folders[':folderId'].children.$get({
|
||||
param: { folderId: parentFolderId },
|
||||
query: {
|
||||
assetType: 'file',
|
||||
after: pageParam,
|
||||
first: '20',
|
||||
},
|
||||
})
|
||||
if (!res.ok) throw new Error('failed to fetch folder children')
|
||||
return (await res.json()) as unknown as AssetInfoPaginatedList
|
||||
},
|
||||
initialPageParam: '',
|
||||
getNextPageParam: (lastPage) => lastPage.pageInfo?.cursor || undefined,
|
||||
})
|
||||
|
||||
const files = data?.pages.flatMap((page) => page.data) ?? []
|
||||
const [files, setFiles] = useState<AssetInfo[]>(initialFiles)
|
||||
const [nextCursor, setNextCursor] = useState<string | undefined>(initialNextCursor)
|
||||
const [hasMore, setHasMore] = useState<boolean>(!!initialNextCursor)
|
||||
const [isFetchingNext, setIsFetchingNext] = useState<boolean>(false)
|
||||
|
||||
// Sync state if initial files / cursor changes (due to parent folder change)
|
||||
useEffect(() => {
|
||||
if (inView && hasNextPage && !isFetchingNextPage) {
|
||||
fetchNextPage()
|
||||
setFiles(initialFiles)
|
||||
setNextCursor(initialNextCursor)
|
||||
setHasMore(!!initialNextCursor)
|
||||
hasScrolledRef.current = false
|
||||
}, [initialFiles, initialNextCursor])
|
||||
|
||||
// Fetch next page when scrolling near bottom
|
||||
useEffect(() => {
|
||||
const fetchNextPage = async () => {
|
||||
if (!nextCursor || isFetchingNext || !inView || !hasMore) return
|
||||
setIsFetchingNext(true)
|
||||
try {
|
||||
const res = await client.api.folders[':folderId'].search.$post({
|
||||
param: { folderId: parentFolderId },
|
||||
json: {
|
||||
assetType: 'file',
|
||||
after: nextCursor,
|
||||
first: 200,
|
||||
recursively: false,
|
||||
conditions: [],
|
||||
},
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to fetch next page')
|
||||
const result = (await res.json()) as unknown as AssetInfoPaginatedList
|
||||
const batchFiles = (result.data || []) as AssetInfo[]
|
||||
|
||||
setFiles((prev) => [...prev, ...batchFiles])
|
||||
setNextCursor(result.pageInfo?.cursor || undefined)
|
||||
setHasMore(!!result.pageInfo?.cursor && batchFiles.length > 0)
|
||||
} catch (err) {
|
||||
console.error('Error fetching next page:', err)
|
||||
} finally {
|
||||
setIsFetchingNext(false)
|
||||
}
|
||||
}
|
||||
}, [inView, hasNextPage, isFetchingNextPage, fetchNextPage])
|
||||
|
||||
fetchNextPage()
|
||||
}, [inView, nextCursor, hasMore, isFetchingNext, parentFolderId])
|
||||
|
||||
// Reset scroll lock when current asset changes
|
||||
useEffect(() => {
|
||||
@@ -73,47 +100,52 @@ export const FileViewerLeftSidebar: FC<FileViewerLeftSidebarProps> = ({
|
||||
if (activeFileInView && activeItemRef.current && !hasScrolledRef.current) {
|
||||
activeItemRef.current.scrollIntoView({
|
||||
block: 'center',
|
||||
inline: 'center',
|
||||
behavior: 'smooth',
|
||||
})
|
||||
hasScrolledRef.current = true
|
||||
}
|
||||
}, [files, currentAssetId])
|
||||
|
||||
return (
|
||||
<div className="h-full w-full bg-card border-r border-border flex flex-col">
|
||||
<div className="p-2 border-b flex items-center justify-between">
|
||||
<h3 className="text-sm font-semibold px-2">Current Folder</h3>
|
||||
<Button variant="ghost" size="icon-sm" onClick={onCollapse}>
|
||||
<PanelLeftClose className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
<ScrollArea className="flex-1 [&>div>div]:block! px-2">
|
||||
<div className="p-2 space-y-2">
|
||||
{isLoading && (
|
||||
<div className="flex justify-center items-center p-4">
|
||||
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
|
||||
<div className="h-24 md:h-full w-full md:w-24 bg-gray-100 dark:bg-gray-950 flex flex-row md:flex-col flex-shrink-0 select-none">
|
||||
<div className="flex-1 overflow-x-auto overflow-y-hidden md:overflow-y-auto md:overflow-x-hidden px-4 md:px-0 py-2 md:py-4 flex flex-row md:flex-col items-center gap-3 no-scrollbar">
|
||||
{files.map((file) => {
|
||||
if (!file) return null
|
||||
const isActive = file.id === currentAssetId
|
||||
return (
|
||||
<div
|
||||
key={file.id}
|
||||
ref={file.id === currentAssetId ? activeItemRef : null}
|
||||
className="flex-shrink-0"
|
||||
>
|
||||
<Link
|
||||
to="/projects/$projectId/files/$fileId"
|
||||
params={{ projectId, fileId: file.id }}
|
||||
search={{ version: undefined }}
|
||||
className={cn(
|
||||
'rounded-lg border overflow-hidden flex items-center justify-center transition-all bg-muted/30 block',
|
||||
isActive
|
||||
? 'w-[62px] h-[62px] opacity-100'
|
||||
: 'w-[52px] h-[52px] opacity-60 hover:opacity-100',
|
||||
)}
|
||||
>
|
||||
<CarouselFilePreview item={file} />
|
||||
</Link>
|
||||
</div>
|
||||
)}
|
||||
{files.map(
|
||||
(file) =>
|
||||
file && (
|
||||
<SidebarFileCard
|
||||
ref={file.id === currentAssetId ? activeItemRef : null}
|
||||
key={file.id}
|
||||
projectId={projectId}
|
||||
item={file}
|
||||
isActive={file.id === currentAssetId}
|
||||
/>
|
||||
),
|
||||
)}
|
||||
<div ref={ref}>
|
||||
{isFetchingNextPage && (
|
||||
<div className="flex justify-center items-center p-4">
|
||||
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
)}
|
||||
)
|
||||
})}
|
||||
|
||||
{/* Infinite Scroll Trigger */}
|
||||
{hasMore && (
|
||||
<div
|
||||
ref={ref}
|
||||
className="px-2 md:px-0 py-2 md:py-4 flex justify-center items-center h-full md:w-full flex-shrink-0"
|
||||
>
|
||||
{isFetchingNext && <Loader2 className="h-5 w-5 animate-spin text-muted-foreground" />}
|
||||
</div>
|
||||
</div>
|
||||
</ScrollArea>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -404,7 +404,6 @@ export function FileViewerRightSidebar({
|
||||
onSendMessage={handleSendMessage}
|
||||
replyingTo={replyingTo}
|
||||
onCancelReply={() => setReplyingTo(null)}
|
||||
users={members}
|
||||
bots={enabledBots}
|
||||
hideAnnotationControl={hideAnnotationControl}
|
||||
disableMentions={isPublic}
|
||||
|
||||
@@ -2,8 +2,8 @@ import { useScreenSize } from '@/ui/hooks/useScreenSize'
|
||||
import { getBestTranscode } from '@/ui/lib/media'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import { Check, Copy, Download, Minus, Plus } from 'lucide-react'
|
||||
import type { RefObject } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import type { ReactNode, RefObject } from 'react'
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import type Player from 'video.js/dist/types/player'
|
||||
import DrawingCanvas from './drawing-canvas'
|
||||
import VideoPlayer from './viewers/video-player'
|
||||
@@ -18,6 +18,7 @@ type FileViewerProps = {
|
||||
onPause?: () => void
|
||||
onTimeUpdate?: (time: number) => void
|
||||
annotations?: Annotation[]
|
||||
children?: ReactNode
|
||||
}
|
||||
|
||||
export function FileViewer({
|
||||
@@ -27,6 +28,7 @@ export function FileViewer({
|
||||
onPause,
|
||||
onTimeUpdate,
|
||||
annotations,
|
||||
children,
|
||||
}: FileViewerProps) {
|
||||
const { width: screenWidth } = useScreenSize()
|
||||
const isImage = file.mediaType?.startsWith('image/')
|
||||
@@ -49,11 +51,33 @@ export function FileViewer({
|
||||
|
||||
// State for Image Viewer
|
||||
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const [zoom, setZoom] = useState(1)
|
||||
const [pan, setPan] = useState({ x: 0, y: 0 })
|
||||
const [copied, setCopied] = useState(false)
|
||||
|
||||
const observerRef = useRef<ResizeObserver | null>(null)
|
||||
|
||||
const containerRef = useCallback((node: HTMLDivElement | null) => {
|
||||
if (observerRef.current) {
|
||||
observerRef.current.disconnect()
|
||||
observerRef.current = null
|
||||
}
|
||||
|
||||
if (node !== null) {
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
const entry = entries[0]
|
||||
if (entry) {
|
||||
setContainerSize({
|
||||
width: entry.contentRect.width,
|
||||
height: entry.contentRect.height,
|
||||
})
|
||||
}
|
||||
})
|
||||
observer.observe(node)
|
||||
observerRef.current = observer
|
||||
}
|
||||
}, [])
|
||||
|
||||
const imgW = file.media?.metadata?.originalWidth ?? 1920
|
||||
const imgH = file.media?.metadata?.originalHeight ?? 1080
|
||||
const conW = containerSize.width
|
||||
@@ -76,22 +100,6 @@ export function FileViewer({
|
||||
}
|
||||
}, [file.id, conW, conH, isImage, baseScale, imgW, imgH])
|
||||
|
||||
// Resize Observer
|
||||
useEffect(() => {
|
||||
if (!containerRef.current) return
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
const entry = entries[0]
|
||||
if (entry) {
|
||||
setContainerSize({
|
||||
width: entry.contentRect.width,
|
||||
height: entry.contentRect.height,
|
||||
})
|
||||
}
|
||||
})
|
||||
observer.observe(containerRef.current)
|
||||
return () => observer.disconnect()
|
||||
}, [])
|
||||
|
||||
const handleZoom = (factor: number) => {
|
||||
const cx = conW / 2
|
||||
const cy = conH / 2
|
||||
@@ -186,10 +194,28 @@ export function FileViewer({
|
||||
}
|
||||
}
|
||||
|
||||
if (!bestUrl) {
|
||||
const isUnsupported = !bestUrl || (!isImage && !isVideo)
|
||||
|
||||
if (isUnsupported) {
|
||||
return (
|
||||
<div className="flex flex-1 items-center justify-center">
|
||||
<p className="text-muted-foreground">Preview unavailable</p>
|
||||
<div className="flex flex-col flex-1 h-full overflow-hidden bg-gray-100 dark:bg-gray-950 relative">
|
||||
<div className="flex-1 flex flex-col-reverse md:flex-row min-h-0 relative">
|
||||
{children}
|
||||
<div className="flex-1 flex items-center justify-center">
|
||||
<p className="text-muted-foreground">Preview unavailable</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="relative px-4 py-3 bg-card border-t border-gray-200 dark:border-gray-700 z-10 flex items-center justify-end gap-2 transition-colors duration-200">
|
||||
<button
|
||||
onClick={handleDownload}
|
||||
disabled={!file.media?.original?.downloadUrl}
|
||||
className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50 animate-in fade-in zoom-in-95 duration-200"
|
||||
title="Download original file"
|
||||
>
|
||||
<Download size={14} />
|
||||
Download
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -201,26 +227,29 @@ export function FileViewer({
|
||||
<div className="flex flex-col flex-1 h-full overflow-hidden bg-gray-100 dark:bg-gray-950 relative">
|
||||
{isImage && (
|
||||
<>
|
||||
<div ref={containerRef} className="flex-1 relative overflow-hidden">
|
||||
<DrawingCanvas
|
||||
width={conW}
|
||||
height={conH}
|
||||
mediaDimensions={{
|
||||
width: imgW,
|
||||
height: imgH,
|
||||
}}
|
||||
imageUrl={bestUrl}
|
||||
annotations={displayAnnotations}
|
||||
scale={zoom}
|
||||
offset={pan}
|
||||
onPan={setPan}
|
||||
className="absolute inset-0 z-0"
|
||||
// Drawing Props
|
||||
isDrawing={isDrawing}
|
||||
currentTool={currentTool}
|
||||
currentColor={currentColor}
|
||||
onAddAnnotation={addAnnotation}
|
||||
/>
|
||||
<div className="flex-1 flex flex-col-reverse md:flex-row min-h-0 relative">
|
||||
{children}
|
||||
<div ref={containerRef} className="flex-1 relative overflow-hidden">
|
||||
<DrawingCanvas
|
||||
width={conW}
|
||||
height={conH}
|
||||
mediaDimensions={{
|
||||
width: imgW,
|
||||
height: imgH,
|
||||
}}
|
||||
imageUrl={bestUrl}
|
||||
annotations={displayAnnotations}
|
||||
scale={zoom}
|
||||
offset={pan}
|
||||
onPan={setPan}
|
||||
className="absolute inset-0 z-0"
|
||||
// Drawing Props
|
||||
isDrawing={isDrawing}
|
||||
currentTool={currentTool}
|
||||
currentColor={currentColor}
|
||||
onAddAnnotation={addAnnotation}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{/* Zoom Toolbar */}
|
||||
<div className="relative px-4 py-3 bg-card border-t border-gray-200 dark:border-gray-700 z-10 flex items-center justify-end gap-2 transition-colors duration-200">
|
||||
@@ -278,7 +307,9 @@ export function FileViewer({
|
||||
onPause={onPause}
|
||||
onTimeUpdate={onTimeUpdate}
|
||||
annotations={displayAnnotations}
|
||||
/>
|
||||
>
|
||||
{children}
|
||||
</VideoPlayer>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -21,7 +21,6 @@ export function GuestIdentityPopup({
|
||||
onSuccess,
|
||||
children,
|
||||
}: GuestIdentityPopupProps) {
|
||||
console.log('[GuestIdentityPopup] isOpen:', isOpen)
|
||||
const [name, setName] = useState('')
|
||||
const [email, setEmail] = useState('')
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
|
||||
@@ -4,7 +4,7 @@ import { client } from '@/ui/api/client'
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/ui/components/ui/card'
|
||||
import { Input } from '@/ui/components/ui/input'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import { Loader2, Plus, X, Globe } from 'lucide-react'
|
||||
import { Loader2, Plus, X, Globe, Check, Trash2, ShieldAlert } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
export function SandboxSettings({ teamId }: { teamId: string }) {
|
||||
@@ -23,10 +23,10 @@ export function SandboxSettings({ teamId }: { teamId: string }) {
|
||||
})
|
||||
|
||||
const { mutate: updateSandbox, isPending: isUpdating } = useMutation({
|
||||
mutationFn: async (allowedDomains: string[]) => {
|
||||
mutationFn: async (params: { allowedDomains: string[]; pendingDomains: string[] }) => {
|
||||
const res = await client.api.teams[':teamId'].sandbox.$put({
|
||||
param: { teamId },
|
||||
json: { allowedDomains },
|
||||
json: params,
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to update sandbox settings')
|
||||
return await res.json()
|
||||
@@ -47,14 +47,27 @@ export function SandboxSettings({ teamId }: { teamId: string }) {
|
||||
toast.error('Domain already exists')
|
||||
return
|
||||
}
|
||||
const updated = [...(sandbox?.allowedDomains || []), newDomain.trim()]
|
||||
updateSandbox(updated)
|
||||
const updatedAllowed = [...(sandbox?.allowedDomains || []), newDomain.trim()]
|
||||
const updatedPending = sandbox?.pendingDomains || []
|
||||
updateSandbox({ allowedDomains: updatedAllowed, pendingDomains: updatedPending })
|
||||
setNewDomain('')
|
||||
}
|
||||
|
||||
const handleRemoveDomain = (domain: string) => {
|
||||
const updated = (sandbox?.allowedDomains || []).filter((d) => d !== domain)
|
||||
updateSandbox(updated)
|
||||
const updatedAllowed = (sandbox?.allowedDomains || []).filter((d) => d !== domain)
|
||||
const updatedPending = sandbox?.pendingDomains || []
|
||||
updateSandbox({ allowedDomains: updatedAllowed, pendingDomains: updatedPending })
|
||||
}
|
||||
|
||||
const handleApproveDomain = (domain: string) => {
|
||||
const updatedAllowed = [...(sandbox?.allowedDomains || []), domain]
|
||||
const updatedPending = (sandbox?.pendingDomains || []).filter((d) => d !== domain)
|
||||
updateSandbox({ allowedDomains: updatedAllowed, pendingDomains: updatedPending })
|
||||
}
|
||||
|
||||
const handleDeletePendingDomain = (domain: string) => {
|
||||
const updatedPending = (sandbox?.pendingDomains || []).filter((d) => d !== domain)
|
||||
updateSandbox({ allowedDomains: sandbox?.allowedDomains || [], pendingDomains: updatedPending })
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
@@ -122,6 +135,64 @@ export function SandboxSettings({ teamId }: { teamId: string }) {
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Pending Domains Section */}
|
||||
<Card className="border-amber-200 dark:border-amber-900 bg-amber-50/10 dark:bg-amber-950/5">
|
||||
<CardHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<ShieldAlert className="w-5 h-5 text-amber-500" />
|
||||
<CardTitle>Pending Domain Approvals</CardTitle>
|
||||
</div>
|
||||
<CardDescription>
|
||||
These domains were blocked during agent execution. You can approve them to allow future
|
||||
network requests, or delete them from this list.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-3">
|
||||
{sandbox?.pendingDomains && sandbox.pendingDomains.length > 0 ? (
|
||||
<div className="divide-y divide-slate-100 dark:divide-slate-800">
|
||||
{sandbox.pendingDomains.map((domain) => (
|
||||
<div
|
||||
key={domain}
|
||||
className="flex items-center justify-between py-2.5 first:pt-0 last:pb-0"
|
||||
>
|
||||
<span className="font-mono text-sm text-slate-700 dark:text-slate-300 bg-slate-100 dark:bg-slate-800 px-2 py-1 rounded break-all">
|
||||
{domain}
|
||||
</span>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="h-8 border-emerald-200 text-emerald-600 hover:bg-emerald-50 hover:text-emerald-700 dark:border-emerald-900 dark:text-emerald-400 dark:hover:bg-emerald-950/20"
|
||||
onClick={() => handleApproveDomain(domain)}
|
||||
disabled={isUpdating}
|
||||
>
|
||||
<Check className="w-4 h-4 mr-1.5" />
|
||||
Approve
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="h-8 border-red-200 text-red-600 hover:bg-red-50 hover:text-red-700 dark:border-red-900 dark:text-red-400 dark:hover:bg-red-950/20"
|
||||
onClick={() => handleDeletePendingDomain(domain)}
|
||||
disabled={isUpdating}
|
||||
>
|
||||
<Trash2 className="w-4 h-4 mr-1.5" />
|
||||
Delete
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-sm text-slate-400 italic py-2">
|
||||
No pending domains requiring approval
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Filesystem Restriction</CardTitle>
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
|
||||
import { client } from '@/ui/api/client'
|
||||
import type { InferRequestType, InferResponseType } from 'hono/client'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import type { AssetInfo, AssetInfoPaginatedList, SearchSort } from '@shumai/dtos'
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import type { InfiniteData } from '@tanstack/react-query'
|
||||
import { useState } from 'react'
|
||||
import { toast } from 'sonner'
|
||||
import type { DragEndEvent, DragStartEvent } from '@dnd-kit/react'
|
||||
@@ -31,6 +32,71 @@ export function useFileSystemDnd({
|
||||
const queryClient = useQueryClient()
|
||||
const [dragState, setDragState] = useState<DragState | undefined>(undefined)
|
||||
|
||||
const updateLocalCache = (isFolder: boolean, updatedItem: AssetInfo) => {
|
||||
const assetType = isFolder ? 'folder' : 'file'
|
||||
const queryKeyPrefix = ['search', teamId, assetId, assetType]
|
||||
|
||||
const queries = queryClient.getQueriesData<InfiniteData<AssetInfoPaginatedList>>({
|
||||
queryKey: queryKeyPrefix,
|
||||
})
|
||||
|
||||
const isSearchSort = (val: unknown): val is SearchSort => {
|
||||
if (typeof val !== 'object' || val === null) return false
|
||||
const obj = val as Record<string, unknown>
|
||||
return (
|
||||
'field' in obj &&
|
||||
'order' in obj &&
|
||||
typeof obj.field === 'string' &&
|
||||
(obj.order === 'asc' || obj.order === 'desc')
|
||||
)
|
||||
}
|
||||
|
||||
for (const [queryKey, oldData] of queries) {
|
||||
if (!oldData || !Array.isArray(oldData.pages)) continue
|
||||
|
||||
const sort = queryKey.find(isSearchSort)
|
||||
|
||||
const allItems = oldData.pages.flatMap((page) => page.data ?? [])
|
||||
|
||||
const itemIndex = allItems.findIndex((item) => item.id === updatedItem.id)
|
||||
if (itemIndex === -1) {
|
||||
continue
|
||||
}
|
||||
|
||||
allItems[itemIndex] = { ...allItems[itemIndex], ...updatedItem }
|
||||
|
||||
if (sort?.field === 'custom') {
|
||||
const isDesc = sort.order === 'desc'
|
||||
allItems.sort((a, b) => {
|
||||
const indexA = a.sortIndex ?? ''
|
||||
const indexB = b.sortIndex ?? ''
|
||||
if (indexA === indexB) return 0
|
||||
if (isDesc) {
|
||||
return indexA < indexB ? 1 : -1
|
||||
} else {
|
||||
return indexA < indexB ? -1 : 1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
let pointer = 0
|
||||
const newPages = oldData.pages.map((page) => {
|
||||
const pageSize = page.data ? page.data.length : 0
|
||||
const pageData = allItems.slice(pointer, pointer + pageSize)
|
||||
pointer += pageSize
|
||||
return {
|
||||
...page,
|
||||
data: pageData,
|
||||
}
|
||||
})
|
||||
|
||||
queryClient.setQueryData<InfiniteData<AssetInfoPaginatedList>>(queryKey, {
|
||||
...oldData,
|
||||
pages: newPages,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const $reparent = client.api.projects[':projectId'].reparent.$post
|
||||
const { mutate: reparentAssets } = useMutation<
|
||||
InferResponseType<typeof $reparent, 204>,
|
||||
@@ -185,11 +251,9 @@ export function useFileSystemDnd({
|
||||
: { afterIndex: targetItem.sortIndex ?? undefined },
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
onSuccess: (updatedFolder) => {
|
||||
toast.success('Folder reordered')
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['search', teamId, assetId],
|
||||
})
|
||||
updateLocalCache(true, updatedFolder)
|
||||
onClearSelection()
|
||||
},
|
||||
onError: (err) => {
|
||||
@@ -207,11 +271,9 @@ export function useFileSystemDnd({
|
||||
: { afterIndex: targetItem.sortIndex ?? undefined },
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
onSuccess: (updatedFile) => {
|
||||
toast.success('File reordered')
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['search', teamId, assetId],
|
||||
})
|
||||
updateLocalCache(false, updatedFile)
|
||||
onClearSelection()
|
||||
},
|
||||
onError: (err) => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import type { MediaMetadata } from '@shumai/dtos'
|
||||
import React, { useCallback, useEffect, useRef, useState } from 'react'
|
||||
|
||||
@@ -34,6 +35,12 @@ const ProgressBar: React.FC<ProgressBarProps> = ({
|
||||
const previewVideoRef = useRef<HTMLVideoElement>(null)
|
||||
const [hoverPosition, setHoverPosition] = useState<number | null>(null) // 0 to 1
|
||||
const [isHovering, setIsHovering] = useState(false)
|
||||
const [isDragging, setIsDragging] = useState(false)
|
||||
|
||||
const onSeekRef = useRef(onSeek)
|
||||
useEffect(() => {
|
||||
onSeekRef.current = onSeek
|
||||
}, [onSeek])
|
||||
|
||||
// Load the preview video in background
|
||||
useEffect(() => {
|
||||
@@ -42,8 +49,16 @@ const ProgressBar: React.FC<ProgressBarProps> = ({
|
||||
}
|
||||
}, [previewUrl])
|
||||
|
||||
const getPercentageFromEvent = useCallback((clientX: number) => {
|
||||
if (!containerRef.current) return 0
|
||||
const rect = containerRef.current.getBoundingClientRect()
|
||||
const x = Math.max(0, Math.min(clientX - rect.left, rect.width))
|
||||
return x / rect.width
|
||||
}, [])
|
||||
|
||||
const handleMouseMove = useCallback(
|
||||
(e: React.MouseEvent<HTMLDivElement>) => {
|
||||
if (isDragging) return
|
||||
if (!containerRef.current) return
|
||||
|
||||
const rect = containerRef.current.getBoundingClientRect()
|
||||
@@ -62,23 +77,138 @@ const ProgressBar: React.FC<ProgressBarProps> = ({
|
||||
}
|
||||
}
|
||||
},
|
||||
[duration],
|
||||
[duration, isDragging],
|
||||
)
|
||||
|
||||
const handleMouseLeave = () => {
|
||||
setIsHovering(false)
|
||||
setHoverPosition(null)
|
||||
if (!isDragging) {
|
||||
setIsHovering(false)
|
||||
setHoverPosition(null)
|
||||
}
|
||||
}
|
||||
|
||||
const handleClick = (e: React.MouseEvent<HTMLDivElement>) => {
|
||||
if (!containerRef.current || !duration) return
|
||||
const rect = containerRef.current.getBoundingClientRect()
|
||||
const x = e.clientX - rect.left
|
||||
const percentage = x / rect.width
|
||||
onSeek(percentage * duration)
|
||||
const handleMouseDown = (e: React.MouseEvent<HTMLDivElement>) => {
|
||||
if (e.button !== 0) return
|
||||
e.preventDefault()
|
||||
setIsDragging(true)
|
||||
const percentage = getPercentageFromEvent(e.clientX)
|
||||
setHoverPosition(percentage)
|
||||
setIsHovering(true)
|
||||
onSeekRef.current(percentage * duration)
|
||||
}
|
||||
|
||||
const progressPercent = duration > 0 ? (currentTime / duration) * 100 : 0
|
||||
const handleTouchStart = (e: React.TouchEvent<HTMLDivElement>) => {
|
||||
setIsDragging(true)
|
||||
const percentage = getPercentageFromEvent(e.touches[0].clientX)
|
||||
setHoverPosition(percentage)
|
||||
setIsHovering(true)
|
||||
onSeekRef.current(percentage * duration)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!isDragging) return
|
||||
|
||||
const handleWindowMouseMove = (e: MouseEvent) => {
|
||||
const percentage = getPercentageFromEvent(e.clientX)
|
||||
setHoverPosition(percentage)
|
||||
setIsHovering(true)
|
||||
|
||||
if (previewVideoRef.current && duration) {
|
||||
const seekTime = percentage * duration
|
||||
const vid = previewVideoRef.current
|
||||
if (Number.isFinite(seekTime)) {
|
||||
vid.currentTime = seekTime
|
||||
}
|
||||
}
|
||||
|
||||
onSeekRef.current(percentage * duration)
|
||||
}
|
||||
|
||||
const handleWindowMouseUp = (e: MouseEvent) => {
|
||||
setIsDragging(false)
|
||||
|
||||
let stillHovering = false
|
||||
if (containerRef.current) {
|
||||
const rect = containerRef.current.getBoundingClientRect()
|
||||
stillHovering =
|
||||
e.clientX >= rect.left &&
|
||||
e.clientX <= rect.right &&
|
||||
e.clientY >= rect.top &&
|
||||
e.clientY <= rect.bottom
|
||||
}
|
||||
|
||||
if (!stillHovering) {
|
||||
setIsHovering(false)
|
||||
setHoverPosition(null)
|
||||
}
|
||||
|
||||
const percentage = getPercentageFromEvent(e.clientX)
|
||||
onSeekRef.current(percentage * duration)
|
||||
}
|
||||
|
||||
const handleWindowTouchMove = (e: TouchEvent) => {
|
||||
if (e.touches.length === 0) return
|
||||
const percentage = getPercentageFromEvent(e.touches[0].clientX)
|
||||
setHoverPosition(percentage)
|
||||
setIsHovering(true)
|
||||
|
||||
if (previewVideoRef.current && duration) {
|
||||
const seekTime = percentage * duration
|
||||
const vid = previewVideoRef.current
|
||||
if (Number.isFinite(seekTime)) {
|
||||
vid.currentTime = seekTime
|
||||
}
|
||||
}
|
||||
|
||||
onSeekRef.current(percentage * duration)
|
||||
}
|
||||
|
||||
const handleWindowTouchEnd = (e: TouchEvent) => {
|
||||
setIsDragging(false)
|
||||
|
||||
const clientX = e.changedTouches.length > 0 ? e.changedTouches[0].clientX : null
|
||||
const clientY = e.changedTouches.length > 0 ? e.changedTouches[0].clientY : null
|
||||
|
||||
let stillHovering = false
|
||||
if (clientX !== null && clientY !== null && containerRef.current) {
|
||||
const rect = containerRef.current.getBoundingClientRect()
|
||||
stillHovering =
|
||||
clientX >= rect.left &&
|
||||
clientX <= rect.right &&
|
||||
clientY >= rect.top &&
|
||||
clientY <= rect.bottom
|
||||
}
|
||||
|
||||
if (!stillHovering) {
|
||||
setIsHovering(false)
|
||||
setHoverPosition(null)
|
||||
}
|
||||
|
||||
if (clientX !== null) {
|
||||
const percentage = getPercentageFromEvent(clientX)
|
||||
onSeekRef.current(percentage * duration)
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('mousemove', handleWindowMouseMove)
|
||||
window.addEventListener('mouseup', handleWindowMouseUp)
|
||||
window.addEventListener('touchmove', handleWindowTouchMove, { passive: true })
|
||||
window.addEventListener('touchend', handleWindowTouchEnd)
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('mousemove', handleWindowMouseMove)
|
||||
window.removeEventListener('mouseup', handleWindowMouseUp)
|
||||
window.removeEventListener('touchmove', handleWindowTouchMove)
|
||||
window.removeEventListener('touchend', handleWindowTouchEnd)
|
||||
}
|
||||
}, [isDragging, duration, getPercentageFromEvent])
|
||||
|
||||
const progressPercent =
|
||||
isDragging && hoverPosition !== null
|
||||
? hoverPosition * 100
|
||||
: duration > 0
|
||||
? (currentTime / duration) * 100
|
||||
: 0
|
||||
const hoverPercent = hoverPosition !== null ? hoverPosition * 100 : 0
|
||||
|
||||
// Calculate preview tooltip position
|
||||
@@ -124,10 +254,11 @@ const ProgressBar: React.FC<ProgressBarProps> = ({
|
||||
return (
|
||||
<div
|
||||
ref={containerRef}
|
||||
className="group relative h-1.5 w-full cursor-pointer touch-none rounded-full bg-gray-300 transition-colors duration-200 dark:bg-white/20"
|
||||
className="group relative h-1.5 w-full cursor-pointer touch-none rounded-full bg-muted transition-colors duration-200"
|
||||
onMouseMove={handleMouseMove}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
onClick={handleClick}
|
||||
onMouseDown={handleMouseDown}
|
||||
onTouchStart={handleTouchStart}
|
||||
>
|
||||
{/* Hidden Preview Video Element */}
|
||||
<video
|
||||
@@ -145,7 +276,7 @@ const ProgressBar: React.FC<ProgressBarProps> = ({
|
||||
className="pointer-events-none absolute bottom-full z-50 mb-3 flex w-max flex-col"
|
||||
style={getTooltipStyle()}
|
||||
>
|
||||
<div className="relative overflow-hidden rounded-lg border-2 border-gray-300 bg-white shadow-2xl dark:border-white/50 dark:bg-black">
|
||||
<div className="relative overflow-hidden rounded-lg border-2 border-border bg-popover shadow-2xl">
|
||||
<video
|
||||
src={previewUrl}
|
||||
className="bg-black object-cover"
|
||||
@@ -157,7 +288,7 @@ const ProgressBar: React.FC<ProgressBarProps> = ({
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<div className="absolute bottom-0 w-full bg-white/80 py-0.5 text-center text-xs font-medium text-gray-900 dark:bg-black/60 dark:text-white">
|
||||
<div className="absolute bottom-0 w-full bg-popover/80 py-0.5 text-center text-xs font-medium text-popover-foreground">
|
||||
{formatTime(hoverPosition! * duration)}
|
||||
</div>
|
||||
</div>
|
||||
@@ -166,25 +297,30 @@ const ProgressBar: React.FC<ProgressBarProps> = ({
|
||||
|
||||
{/* Buffered Bar */}
|
||||
<div
|
||||
className="absolute top-0 left-0 h-full rounded-full bg-gray-400 transition-all duration-200 dark:bg-white/30"
|
||||
className="absolute top-0 left-0 h-full rounded-full bg-muted-foreground/30 transition-all duration-200"
|
||||
style={{ width: `${buffered}%` }}
|
||||
/>
|
||||
|
||||
{/* Hover Highlight Bar */}
|
||||
{isHovering && (
|
||||
<div
|
||||
className="absolute top-0 left-0 h-full rounded-full bg-gray-500/40 dark:bg-white/40"
|
||||
className="absolute top-0 left-0 h-full rounded-full bg-primary/30"
|
||||
style={{ width: `${hoverPercent}%` }}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Play Progress Bar */}
|
||||
<div
|
||||
className="relative absolute top-0 left-0 h-full rounded-full bg-blue-600 dark:bg-blue-500"
|
||||
className="relative absolute top-0 left-0 h-full rounded-full bg-primary"
|
||||
style={{ width: `${progressPercent}%` }}
|
||||
>
|
||||
{/* Thumb (Playhead) - visible on group hover or active */}
|
||||
<div className="absolute top-1/2 right-0 h-3.5 w-3.5 -translate-y-1/2 translate-x-1/2 scale-0 rounded-full border border-gray-200 bg-white shadow-md transition-transform duration-100 group-hover:scale-100 dark:border-none" />
|
||||
<div
|
||||
className={cn(
|
||||
'absolute top-1/2 right-0 h-3.5 w-3.5 -translate-y-1/2 translate-x-1/2 rounded-full border border-border bg-primary shadow-md transition-transform duration-100',
|
||||
isHovering || isDragging ? 'scale-100' : 'scale-0 group-hover:scale-100',
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -53,6 +53,7 @@ interface VideoPlayerProps {
|
||||
onPause?: () => void
|
||||
onTimeUpdate?: (time: number) => void
|
||||
annotations?: Annotation[]
|
||||
children?: React.ReactNode
|
||||
}
|
||||
|
||||
type DisplayTranscode = VideoTranscode & { resolution: string }
|
||||
@@ -108,7 +109,7 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
'transition-all duration-300 ease-in-out z-20',
|
||||
state.isFullScreen
|
||||
? 'absolute bottom-0 left-0 right-0 px-4 py-4 bg-black/60 backdrop-blur-md text-white'
|
||||
: 'relative w-full bg-gray-50 dark:bg-gray-800 border-t border-gray-200 dark:border-gray-700 px-4 py-3 z-10 transition-colors duration-200',
|
||||
: 'relative w-full bg-card border-t border-border px-4 py-3 z-10 transition-colors duration-200',
|
||||
isControlsVisible
|
||||
? 'opacity-100 translate-y-0'
|
||||
: 'opacity-0 translate-y-4 pointer-events-none',
|
||||
@@ -133,12 +134,12 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
<div
|
||||
className={cn(
|
||||
'flex items-center justify-between',
|
||||
state.isFullScreen ? 'text-white/90' : 'text-gray-700 dark:text-white/90',
|
||||
state.isFullScreen ? 'text-white/90' : 'text-foreground',
|
||||
)}
|
||||
>
|
||||
{/* Left Side: Play, Vol, Time */}
|
||||
<div className="flex items-center gap-4">
|
||||
<button onClick={togglePlay} className="hover:text-blue-500 transition-colors">
|
||||
<button onClick={togglePlay} className="hover:text-primary transition-colors">
|
||||
{state.isPlaying ? (
|
||||
<Pause className="w-6 h-6 fill-current" />
|
||||
) : (
|
||||
@@ -148,14 +149,17 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
|
||||
<button
|
||||
onClick={toggleLoop}
|
||||
className={cn('transition-colors', state.isLooping ? 'text-blue-500' : 'text-gray-400')}
|
||||
className={cn(
|
||||
'transition-colors',
|
||||
state.isLooping ? 'text-primary' : 'text-muted-foreground',
|
||||
)}
|
||||
title="Loop"
|
||||
>
|
||||
<Repeat className="w-5 h-5" />
|
||||
</button>
|
||||
|
||||
<div className="flex items-center gap-3 group/vol">
|
||||
<button onClick={toggleMute} className="hover:text-blue-500">
|
||||
<button onClick={toggleMute} className="hover:text-primary">
|
||||
{state.isMuted || state.volume === 0 ? (
|
||||
<VolumeX className="w-6 h-6" />
|
||||
) : (
|
||||
@@ -176,31 +180,33 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
</div>
|
||||
|
||||
<div
|
||||
className="min-w-[100px] cursor-pointer text-sm font-medium tabular-nums hover:text-blue-500"
|
||||
className="min-w-[100px] cursor-pointer text-sm font-medium tabular-nums hover:text-primary"
|
||||
onClick={() => setState((p) => ({ ...p, showFrames: !p.showFrames }))}
|
||||
title="Click to toggle Time/Frames"
|
||||
>
|
||||
{state.showFrames
|
||||
? `${formatFrame(state.currentTime, data.media.metadata.frameRate ?? 30)} / ${formatFrame(state.duration, data.media.metadata.frameRate ?? 30)}`
|
||||
: `${formatTime(state.currentTime)} / ${formatTime(state.duration)}`}
|
||||
<span className="ml-1 text-xs text-gray-400">{state.showFrames ? 'fr' : ''}</span>
|
||||
<span className="ml-1 text-xs text-muted-foreground">
|
||||
{state.showFrames ? 'fr' : ''}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Right Side: Zoom, Speed, Res, Download, Fullscreen */}
|
||||
<div className="relative flex items-center gap-3">
|
||||
{/* Zoom Controls */}
|
||||
<div className="flex items-center gap-1 bg-gray-200/50 dark:bg-white/10 rounded-md p-0.5">
|
||||
<div className="flex items-center gap-1 bg-muted rounded-md p-0.5">
|
||||
<button
|
||||
onClick={() => onZoomChange(zoom * 0.8)}
|
||||
className="p-1 hover:text-blue-500 rounded"
|
||||
className="p-1 hover:text-primary rounded"
|
||||
>
|
||||
<Minus size={14} />
|
||||
</button>
|
||||
<span className="text-xs w-8 text-center tabular-nums">{Math.round(zoom * 100)}%</span>
|
||||
<button
|
||||
onClick={() => onZoomChange(zoom * 1.2)}
|
||||
className="p-1 hover:text-blue-500 rounded"
|
||||
className="p-1 hover:text-primary rounded"
|
||||
>
|
||||
<Plus size={14} />
|
||||
</button>
|
||||
@@ -209,7 +215,7 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
{/* Playback Rate */}
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<button className="w-8 text-sm font-bold hover:text-blue-500">
|
||||
<button className="w-8 text-sm font-bold hover:text-primary">
|
||||
{state.playbackRate}x
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
@@ -221,8 +227,8 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
className={cn(
|
||||
'rounded py-1 px-2 text-left text-xs',
|
||||
state.playbackRate === rate
|
||||
? 'bg-blue-600 text-white'
|
||||
: 'text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-800',
|
||||
? 'bg-primary text-primary-foreground focus:bg-primary focus:text-primary-foreground'
|
||||
: 'text-foreground',
|
||||
)}
|
||||
>
|
||||
{rate}x
|
||||
@@ -234,7 +240,7 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
{/* Resolution Selector */}
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<button className="flex items-center gap-1 rounded border border-gray-300 px-2 py-0.5 text-sm font-semibold hover:bg-gray-200 dark:border-white/20 dark:hover:bg-white/10">
|
||||
<button className="flex items-center gap-1 rounded border border-border px-2 py-0.5 text-sm font-semibold hover:bg-muted transition-colors">
|
||||
<Settings className="h-3.5 w-3.5" />
|
||||
<span>{state.currentResolution}</span>
|
||||
</button>
|
||||
@@ -249,12 +255,12 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
className={cn(
|
||||
'flex w-full items-center justify-between',
|
||||
state.currentResolution === res.resolution
|
||||
? 'text-blue-500'
|
||||
: 'text-gray-700 dark:text-gray-300',
|
||||
? 'text-primary font-medium'
|
||||
: 'text-foreground',
|
||||
)}
|
||||
>
|
||||
<span>{res.resolution}</span>
|
||||
<span className="pl-5 text-xs text-gray-400 dark:text-gray-500">
|
||||
<span className="pl-5 text-xs text-muted-foreground">
|
||||
{res.width}x{res.height}
|
||||
</span>
|
||||
</DropdownMenuItem>
|
||||
@@ -265,7 +271,7 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
{/* Download */}
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<button className="transition-colors hover:text-blue-500" title="Download">
|
||||
<button className="transition-colors hover:text-primary" title="Download">
|
||||
<Download className="h-5 w-5" />
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
@@ -275,17 +281,17 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
<DropdownMenuItem
|
||||
key={res.resolution}
|
||||
onClick={() => handleDownload(res.url ?? '', res.resolution)}
|
||||
className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-800"
|
||||
className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-foreground"
|
||||
>
|
||||
<span>{res.resolution}</span>
|
||||
<span className="text-xs text-gray-400 dark:text-gray-500">MP4</span>
|
||||
<span className="text-xs text-muted-foreground">MP4</span>
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
|
||||
{/* Fullscreen */}
|
||||
<button onClick={toggleFullScreen} className="transition-colors hover:text-blue-500">
|
||||
<button onClick={toggleFullScreen} className="transition-colors hover:text-primary">
|
||||
{state.isFullScreen ? (
|
||||
<Minimize className="h-6 w-6" />
|
||||
) : (
|
||||
@@ -305,6 +311,7 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
|
||||
onPause,
|
||||
onTimeUpdate,
|
||||
annotations,
|
||||
children,
|
||||
}) => {
|
||||
const localPlayerRef = useRef<Player | null>(null)
|
||||
const playerRef = passedPlayerRef || localPlayerRef
|
||||
@@ -463,7 +470,7 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
|
||||
sources: [
|
||||
{
|
||||
src: state.currentSrc,
|
||||
type: data.media?.mimeType || 'video/mp4',
|
||||
type: 'video/mp4',
|
||||
},
|
||||
],
|
||||
}))
|
||||
@@ -500,12 +507,19 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
|
||||
setBuffered((bufferedEnd / duration) * 100)
|
||||
}
|
||||
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
currentTime: current,
|
||||
duration: duration || prev.duration,
|
||||
progress: duration > 0 ? (current / duration) * 100 : 0,
|
||||
}))
|
||||
// Only update state here if the player is paused.
|
||||
// When playing, requestAnimationFrame handles smooth updates.
|
||||
if (player.paused()) {
|
||||
setState((prev) => {
|
||||
if (prev.currentTime === current) return prev
|
||||
return {
|
||||
...prev,
|
||||
currentTime: current,
|
||||
duration: duration || prev.duration,
|
||||
progress: duration > 0 ? (current / duration) * 100 : 0,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (onTimeUpdate) {
|
||||
onTimeUpdate(current)
|
||||
@@ -545,6 +559,41 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
|
||||
}
|
||||
}, [data])
|
||||
|
||||
// Smooth progress updates using requestAnimationFrame when playing
|
||||
useEffect(() => {
|
||||
let animationFrameId: number
|
||||
|
||||
const updateProgress = () => {
|
||||
const player = playerRef.current
|
||||
if (player && !player.paused()) {
|
||||
const current = player.currentTime() || 0
|
||||
const duration = player.duration() || data.media?.metadata?.duration || 0
|
||||
|
||||
setState((prev) => {
|
||||
if (prev.currentTime === current) return prev
|
||||
return {
|
||||
...prev,
|
||||
currentTime: current,
|
||||
duration: duration || prev.duration,
|
||||
progress: duration > 0 ? (current / duration) * 100 : 0,
|
||||
}
|
||||
})
|
||||
|
||||
animationFrameId = requestAnimationFrame(updateProgress)
|
||||
}
|
||||
}
|
||||
|
||||
if (state.isPlaying) {
|
||||
animationFrameId = requestAnimationFrame(updateProgress)
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (animationFrameId) {
|
||||
cancelAnimationFrame(animationFrameId)
|
||||
}
|
||||
}
|
||||
}, [state.isPlaying, playerRef, data.media?.metadata?.duration])
|
||||
|
||||
const handleMouseMove = useCallback(() => {
|
||||
// Always show controls on movement
|
||||
setIsControlsVisible(true)
|
||||
@@ -683,7 +732,7 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
|
||||
currentSrc: res.url,
|
||||
}))
|
||||
|
||||
player.src({ type: data.media?.mimeType || 'video/mp4', src: res.url })
|
||||
player.src({ type: 'video/mp4', src: res.url })
|
||||
|
||||
player.one('loadedmetadata', () => {
|
||||
player.currentTime(currentTime)
|
||||
@@ -750,49 +799,54 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
|
||||
onMouseMove={handleMouseMove}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
>
|
||||
{/* Video Area */}
|
||||
<div
|
||||
ref={containerRef}
|
||||
className={cn(
|
||||
'w-full bg-black cursor-pointer relative flex items-center justify-center overflow-hidden flex-1 min-h-0',
|
||||
)}
|
||||
onClick={togglePlay}
|
||||
data-vjs-player
|
||||
>
|
||||
{/* Hidden VideoJS container */}
|
||||
<div ref={videoContainerRef} className="absolute inset-0 z-[-1]" />
|
||||
<div className="flex-1 flex flex-col-reverse md:flex-row min-h-0 relative">
|
||||
{/* Render Carousel/Sidebar here if not fullscreen */}
|
||||
{!state.isFullScreen && children}
|
||||
|
||||
{/* Drawing Canvas (Visible) */}
|
||||
{videoHtmlEl && containerSize.width > 0 && (
|
||||
<DrawingCanvas
|
||||
width={containerSize.width}
|
||||
height={containerSize.height}
|
||||
mediaDimensions={{
|
||||
width: vidW,
|
||||
height: vidH,
|
||||
}}
|
||||
videoElement={videoHtmlEl}
|
||||
annotations={displayAnnotations}
|
||||
scale={scale}
|
||||
offset={{ x: panX, y: panY }}
|
||||
className="absolute inset-0"
|
||||
onClick={togglePlay}
|
||||
// Drawing Props
|
||||
isDrawing={isDrawing}
|
||||
currentTool={currentTool}
|
||||
currentColor={currentColor}
|
||||
onAddAnnotation={addAnnotation}
|
||||
/>
|
||||
)}
|
||||
{/* Video Area */}
|
||||
<div
|
||||
ref={containerRef}
|
||||
className={cn(
|
||||
'flex-1 bg-black cursor-pointer relative flex items-center justify-center overflow-hidden min-h-0',
|
||||
)}
|
||||
onClick={togglePlay}
|
||||
data-vjs-player
|
||||
>
|
||||
{/* Hidden VideoJS container */}
|
||||
<div ref={videoContainerRef} className="absolute inset-0 z-[-1]" />
|
||||
|
||||
{/* Big Play Button Overlay (when paused) */}
|
||||
{!state.isPlaying && !isDrawing && (
|
||||
<div className="absolute inset-0 flex items-center justify-center bg-black/20 pointer-events-none z-10">
|
||||
<div className="w-20 h-20 bg-white/10 backdrop-blur-sm rounded-full flex items-center justify-center border-2 border-white/30 animate-pulse">
|
||||
<Play className="w-10 h-10 text-white ml-1 fill-white" />
|
||||
{/* Drawing Canvas (Visible) */}
|
||||
{videoHtmlEl && containerSize.width > 0 && (
|
||||
<DrawingCanvas
|
||||
width={containerSize.width}
|
||||
height={containerSize.height}
|
||||
mediaDimensions={{
|
||||
width: vidW,
|
||||
height: vidH,
|
||||
}}
|
||||
videoElement={videoHtmlEl}
|
||||
annotations={displayAnnotations}
|
||||
scale={scale}
|
||||
offset={{ x: panX, y: panY }}
|
||||
className="absolute inset-0"
|
||||
onClick={togglePlay}
|
||||
// Drawing Props
|
||||
isDrawing={isDrawing}
|
||||
currentTool={currentTool}
|
||||
currentColor={currentColor}
|
||||
onAddAnnotation={addAnnotation}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Big Play Button Overlay (when paused) */}
|
||||
{!state.isPlaying && !isDrawing && (
|
||||
<div className="absolute inset-0 flex items-center justify-center bg-black/20 pointer-events-none z-10">
|
||||
<div className="w-20 h-20 bg-white/10 backdrop-blur-sm rounded-full flex items-center justify-center border-2 border-white/30 animate-pulse">
|
||||
<Play className="w-10 h-10 text-white ml-1 fill-white" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<ControlBar
|
||||
|
||||
@@ -159,3 +159,11 @@
|
||||
@apply bg-background text-foreground;
|
||||
}
|
||||
}
|
||||
|
||||
.no-scrollbar {
|
||||
-ms-overflow-style: none; /* IE and Edge */
|
||||
scrollbar-width: none; /* Firefox */
|
||||
}
|
||||
.no-scrollbar::-webkit-scrollbar {
|
||||
display: none; /* Chrome, Safari and Opera */
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/webui",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./index.html",
|
||||
|
||||
@@ -7,12 +7,11 @@ import { FileDetailSkeleton } from '@/ui/components/loading-skeletons'
|
||||
import { useMemberStore } from '@/ui/stores/members'
|
||||
import { useTeamContextStore } from '@/ui/stores/team-context'
|
||||
import { useTopNavStore } from '@/ui/stores/top-nav'
|
||||
import { useUiStore } from '@/ui/stores/ui'
|
||||
import { type Annotation } from '@/ui/types'
|
||||
import { useMutation } from '@tanstack/react-query'
|
||||
import { InferRequestType, InferResponseType } from 'hono/client'
|
||||
|
||||
import type { AssetInfo, CommentInfo } from '@shumai/dtos'
|
||||
import type { AssetInfo, AssetInfoPaginatedList, CommentInfo } from '@shumai/dtos'
|
||||
import { keepPreviousData, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { createFileRoute, useNavigate } from '@tanstack/react-router'
|
||||
import { Loader2 } from 'lucide-react'
|
||||
@@ -26,17 +25,17 @@ function FileViewPage() {
|
||||
const activeFileId = versionAssetId || fileId
|
||||
|
||||
const videoRef = useRef<Player | null>(null)
|
||||
const {
|
||||
fileViewRightSidebarCollapsed: isRightSidebarCollapsed,
|
||||
fileViewLeftSidebarCollapsed: isLeftSidebarCollapsed,
|
||||
setFileViewLeftSidebarCollapsed: setIsLeftSidebarCollapsed,
|
||||
} = useUiStore()
|
||||
const { teamId, ensureTeamIdForProject } = useTeamContextStore()
|
||||
const [leftSidebarWidth, setLeftSidebarWidth] = useState(280)
|
||||
const [rightSidebarWidth, setRightSidebarWidth] = useState(360)
|
||||
const [annotations, setAnnotations] = useState<Annotation[]>([])
|
||||
const [currentTime, setCurrentTime] = useState(0)
|
||||
const [selectedCommentId, setSelectedCommentId] = useState<string | null>(null)
|
||||
const [carouselState, setCarouselState] = useState<{
|
||||
show: boolean
|
||||
files: AssetInfo[]
|
||||
nextCursor?: string
|
||||
folderId?: string
|
||||
}>({ show: false, files: [], nextCursor: undefined, folderId: undefined })
|
||||
const $patchMetadata = client.api.files[':fileId'].metadata.$patch
|
||||
const { mutate: patchMetadata } = useMutation<
|
||||
InferResponseType<typeof $patchMetadata>,
|
||||
@@ -171,6 +170,92 @@ function FileViewPage() {
|
||||
navigate,
|
||||
])
|
||||
|
||||
const parentFolderId =
|
||||
fileData?.ancestorFolders?.[fileData.ancestorFolders.length - 1]?.id ??
|
||||
projectInfo?.rootFolder ??
|
||||
''
|
||||
|
||||
useEffect(() => {
|
||||
if (!parentFolderId || !activeFileId) return
|
||||
if (carouselState.folderId === parentFolderId) {
|
||||
return
|
||||
}
|
||||
|
||||
let isMounted = true
|
||||
|
||||
const fetchCarouselData = async () => {
|
||||
try {
|
||||
let cursor = ''
|
||||
let found = false
|
||||
const allFiles: AssetInfo[] = []
|
||||
let lastCursor: string | undefined = undefined
|
||||
|
||||
for (let batch = 0; batch < 5; batch++) {
|
||||
const res = await client.api.folders[':folderId'].search.$post({
|
||||
param: { folderId: parentFolderId },
|
||||
json: {
|
||||
assetType: 'file',
|
||||
after: cursor,
|
||||
first: 200,
|
||||
recursively: false,
|
||||
conditions: [],
|
||||
},
|
||||
})
|
||||
if (!res.ok) {
|
||||
throw new Error('Failed to search parent folder for carousel')
|
||||
}
|
||||
const result = (await res.json()) as unknown as AssetInfoPaginatedList
|
||||
const batchFiles = (result.data || []) as AssetInfo[]
|
||||
allFiles.push(...batchFiles)
|
||||
|
||||
if (batchFiles.some((f) => f.id === activeFileId)) {
|
||||
found = true
|
||||
lastCursor = result.pageInfo?.cursor || undefined
|
||||
break
|
||||
}
|
||||
if (!result.pageInfo?.cursor || batchFiles.length < 200) {
|
||||
break
|
||||
}
|
||||
cursor = result.pageInfo.cursor
|
||||
}
|
||||
|
||||
if (isMounted) {
|
||||
if (found) {
|
||||
setCarouselState({
|
||||
show: true,
|
||||
files: allFiles,
|
||||
nextCursor: lastCursor,
|
||||
folderId: parentFolderId,
|
||||
})
|
||||
} else {
|
||||
setCarouselState({
|
||||
show: false,
|
||||
files: [],
|
||||
nextCursor: undefined,
|
||||
folderId: parentFolderId,
|
||||
})
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
if (isMounted) {
|
||||
setCarouselState({
|
||||
show: false,
|
||||
files: [],
|
||||
nextCursor: undefined,
|
||||
folderId: parentFolderId,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fetchCarouselData()
|
||||
|
||||
return () => {
|
||||
isMounted = false
|
||||
}
|
||||
}, [parentFolderId, activeFileId, carouselState.folderId])
|
||||
|
||||
if (isLoading && !fileData) {
|
||||
return <FileDetailSkeleton />
|
||||
}
|
||||
@@ -225,28 +310,6 @@ function FileViewPage() {
|
||||
return (
|
||||
<div className="h-full flex flex-1 flex-col bg-background">
|
||||
<div className="h-full flex flex-1 overflow-hidden">
|
||||
{!isLeftSidebarCollapsed && (
|
||||
<>
|
||||
<div style={{ width: leftSidebarWidth }} className="flex-shrink-0">
|
||||
<FileViewerLeftSidebar
|
||||
projectId={projectId}
|
||||
currentAssetId={activeFileId}
|
||||
parentFolderId={
|
||||
fileData.ancestorFolders?.[fileData.ancestorFolders.length - 1]?.id ??
|
||||
projectInfo.rootFolder ??
|
||||
''
|
||||
}
|
||||
onCollapse={() => setIsLeftSidebarCollapsed(true)}
|
||||
/>
|
||||
</div>
|
||||
<ResizeHandle
|
||||
onResize={(delta) => {
|
||||
setLeftSidebarWidth((prev) => Math.max(200, Math.min(500, prev + delta)))
|
||||
}}
|
||||
className="hidden md:block"
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
<div className="flex-1 relative">
|
||||
{isFetching && (
|
||||
<div className="absolute inset-0 z-10 flex items-center justify-center bg-background/50">
|
||||
@@ -259,35 +322,41 @@ function FileViewPage() {
|
||||
onPlay={handlePlay}
|
||||
onTimeUpdate={setCurrentTime}
|
||||
annotations={annotations}
|
||||
>
|
||||
{carouselState.show && (
|
||||
<FileViewerLeftSidebar
|
||||
projectId={projectId}
|
||||
currentAssetId={activeFileId}
|
||||
parentFolderId={parentFolderId}
|
||||
initialFiles={carouselState.files}
|
||||
initialNextCursor={carouselState.nextCursor}
|
||||
/>
|
||||
)}
|
||||
</FileViewer>
|
||||
</div>
|
||||
<ResizeHandle
|
||||
onResize={(delta) => {
|
||||
setRightSidebarWidth((prev) => Math.max(300, Math.min(600, prev - delta)))
|
||||
}}
|
||||
className="hidden md:block"
|
||||
/>
|
||||
<div style={{ width: rightSidebarWidth }} className="flex-shrink-0">
|
||||
<FileViewerRightSidebar
|
||||
teamId={teamId}
|
||||
projectId={projectId}
|
||||
file={fileData as unknown as AssetInfo}
|
||||
onSaveField={handleSaveField}
|
||||
members={members}
|
||||
onCommentSelect={handleCommentSelect}
|
||||
currentTime={currentTime}
|
||||
onTyping={() => {
|
||||
if (videoRef.current) {
|
||||
videoRef.current.pause()
|
||||
}
|
||||
}}
|
||||
selectedCommentId={selectedCommentId}
|
||||
/>
|
||||
</div>
|
||||
{!isRightSidebarCollapsed && (
|
||||
<>
|
||||
<ResizeHandle
|
||||
onResize={(delta) => {
|
||||
setRightSidebarWidth((prev) => Math.max(300, Math.min(600, prev - delta)))
|
||||
}}
|
||||
className="hidden md:block"
|
||||
/>
|
||||
<div style={{ width: rightSidebarWidth }} className="flex-shrink-0">
|
||||
<FileViewerRightSidebar
|
||||
teamId={teamId}
|
||||
projectId={projectId}
|
||||
file={fileData as unknown as AssetInfo}
|
||||
onSaveField={handleSaveField}
|
||||
members={members}
|
||||
onCommentSelect={handleCommentSelect}
|
||||
currentTime={currentTime}
|
||||
onTyping={() => {
|
||||
if (videoRef.current) {
|
||||
videoRef.current.pause()
|
||||
}
|
||||
}}
|
||||
selectedCommentId={selectedCommentId}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -9,7 +9,7 @@ interface MemberState {
|
||||
loadedTeamId?: string
|
||||
loadedWithAgents?: boolean
|
||||
loading: boolean
|
||||
fetchMembers: (teamId: string, includeAgents?: boolean) => Promise<void>
|
||||
fetchMembers: (teamId: string, includeAgents?: boolean, force?: boolean) => Promise<void>
|
||||
}
|
||||
|
||||
export const useMemberStore = create<MemberState>()(
|
||||
@@ -17,8 +17,8 @@ export const useMemberStore = create<MemberState>()(
|
||||
immer((set, get) => ({
|
||||
members: [],
|
||||
loading: false,
|
||||
fetchMembers: async (teamId, includeAgents) => {
|
||||
if (get().loadedTeamId === teamId && get().loadedWithAgents === includeAgents) {
|
||||
fetchMembers: async (teamId, includeAgents, force) => {
|
||||
if (!force && get().loadedTeamId === teamId && get().loadedWithAgents === includeAgents) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shumai/workflow-core",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
|
||||
@@ -1,6 +1,27 @@
|
||||
import { describe, it, expect, vi, beforeAll } from 'vitest'
|
||||
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest'
|
||||
import * as wf from '@temporalio/workflow'
|
||||
import { getActivities, executeActivity } from './workflow-utils'
|
||||
|
||||
const mockWorkflowInfo = vi.fn().mockImplementation(() => {
|
||||
throw new Error('Not in workflow context')
|
||||
})
|
||||
const mockProxyActivities = vi.fn()
|
||||
|
||||
vi.mock('@temporalio/workflow', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@temporalio/workflow')>()
|
||||
return {
|
||||
...actual,
|
||||
workflowInfo: () => mockWorkflowInfo(),
|
||||
proxyActivities: (...args: unknown[]) => mockProxyActivities(...args),
|
||||
log: {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
describe('workflow-utils executeActivity', () => {
|
||||
beforeAll(() => {
|
||||
// Inject mock activities for local execution tests
|
||||
@@ -39,4 +60,37 @@ describe('workflow-utils executeActivity', () => {
|
||||
'executeActivity must be called with an activity function obtained from getActivities()',
|
||||
)
|
||||
})
|
||||
|
||||
describe('Temporal mode', () => {
|
||||
beforeAll(() => {
|
||||
// Mock workflowInfo to simulate running in Temporal by returning a fake object
|
||||
mockWorkflowInfo.mockReturnValue({} as unknown as wf.WorkflowInfo)
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
mockWorkflowInfo.mockImplementation(() => {
|
||||
throw new Error('Not in workflow context')
|
||||
})
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('should configure proxyActivities with retry options', async () => {
|
||||
mockProxyActivities.mockReturnValue({
|
||||
mockActivity: vi.fn().mockResolvedValue('temporal-success'),
|
||||
})
|
||||
|
||||
const { mockActivity } = getActivities()
|
||||
const result = await executeActivity('test-queue', mockActivity, 'arg1')
|
||||
|
||||
expect(mockProxyActivities).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
retry: {
|
||||
maximumAttempts: 5,
|
||||
initialInterval: '10s',
|
||||
},
|
||||
}),
|
||||
)
|
||||
expect(result).toBe('temporal-success')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -71,6 +71,10 @@ export function getActivities<T = any>(): T {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
baseProxy = wf.proxyActivities<any>({
|
||||
startToCloseTimeout: '10 minutes',
|
||||
retry: {
|
||||
maximumAttempts: 5,
|
||||
initialInterval: '10s',
|
||||
},
|
||||
})
|
||||
} else {
|
||||
baseProxy = localActivitiesProxy()
|
||||
@@ -163,6 +167,10 @@ export async function executeActivity(
|
||||
const handle = wf.proxyActivities<any>({
|
||||
startToCloseTimeout: '10 minutes',
|
||||
taskQueue: queue,
|
||||
retry: {
|
||||
maximumAttempts: 5,
|
||||
initialInterval: '10s',
|
||||
},
|
||||
})
|
||||
result = await handle[activityName](...args)
|
||||
} else {
|
||||
|
||||
+220
-46
@@ -1,7 +1,7 @@
|
||||
import { rm, mkdir, cp, writeFile } from 'node:fs/promises'
|
||||
import { existsSync } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import tailwindPlugin from 'bun-plugin-tailwind'
|
||||
import { existsSync } from 'node:fs'
|
||||
import { cp, mkdir, readdir, rm, writeFile } from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import { temporalWorkflow } from '../packages/workflow-core/src/bun-temporal-plugin'
|
||||
|
||||
console.log('\n🚀 Starting NPM packaging build process for all applications...\n')
|
||||
@@ -16,6 +16,9 @@ if (existsSync(distDir)) {
|
||||
}
|
||||
await mkdir(distDir, { recursive: true })
|
||||
|
||||
const dummyRunnerPath = path.join(distDir, 'dummy-runner.ts')
|
||||
await writeFile(dummyRunnerPath, '// Shumai Runner Stub\n', 'utf-8')
|
||||
|
||||
const rootPackageJson = JSON.parse(await Bun.file('package.json').text())
|
||||
const version = rootPackageJson.version || '0.1.0'
|
||||
|
||||
@@ -112,21 +115,18 @@ async function compileApp({
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// Rename generated binary (usually Bun names it 'src' or the folder name of the entrypoint)
|
||||
const generatedFile = path.join(outdir, 'src')
|
||||
const generatedFileExe = path.join(outdir, 'src.exe')
|
||||
// Rename generated binary dynamically by finding the output file in outdir
|
||||
const finalFile = path.join(outdir, binaryName)
|
||||
const files = await readdir(outdir)
|
||||
const generatedFilename = files.find((f) => f !== binaryName)
|
||||
|
||||
if (existsSync(generatedFile)) {
|
||||
if (generatedFilename) {
|
||||
const generatedPath = path.join(outdir, generatedFilename)
|
||||
console.log(`➡️ Moving binary to final location: ${finalFile}`)
|
||||
await cp(generatedFile, finalFile)
|
||||
await rm(generatedFile)
|
||||
} else if (existsSync(generatedFileExe)) {
|
||||
console.log(`➡️ Moving binary to final location: ${finalFile}`)
|
||||
await cp(generatedFileExe, finalFile)
|
||||
await rm(generatedFileExe)
|
||||
} else {
|
||||
console.error(`❌ Could not find compiled binary at ${generatedFile} or ${generatedFileExe}`)
|
||||
await cp(generatedPath, finalFile)
|
||||
await rm(generatedPath)
|
||||
} else if (!existsSync(finalFile)) {
|
||||
console.error(`❌ Could not find compiled binary in ${outdir}`)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
@@ -173,23 +173,68 @@ async function buildPlatformPackages(
|
||||
}
|
||||
|
||||
// Function to build main wrapper package
|
||||
async function buildMainPackage(
|
||||
appName: string,
|
||||
description: string,
|
||||
hasPrisma: boolean,
|
||||
extraDependencies: Record<string, string> = {},
|
||||
) {
|
||||
// Function to build main wrapper package
|
||||
async function buildMainPackage({
|
||||
appName,
|
||||
description,
|
||||
hasPrisma,
|
||||
entrypoint,
|
||||
plugins = [],
|
||||
extraDependencies = {},
|
||||
}: {
|
||||
appName: string
|
||||
description: string
|
||||
hasPrisma: boolean
|
||||
entrypoint: string
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
plugins?: any[]
|
||||
extraDependencies?: Record<string, string>
|
||||
}) {
|
||||
console.log(`\n📄 Packaging wrapper app ${appName}...`)
|
||||
const mainOutDir = path.join(distDir, appName)
|
||||
const binOutDir = path.join(mainOutDir, 'bin')
|
||||
await mkdir(binOutDir, { recursive: true })
|
||||
|
||||
// 1. Bundle the JS application code for wrapper
|
||||
console.log(`📦 Bundling JS application code for wrapper ${appName}...`)
|
||||
const appJsName = `${appName}-app.js`
|
||||
|
||||
const buildResult = await Bun.build({
|
||||
entrypoints: [entrypoint],
|
||||
root: '.',
|
||||
target: 'bun',
|
||||
outdir: binOutDir,
|
||||
minify: true,
|
||||
naming: `${appName}-app.[ext]`,
|
||||
plugins,
|
||||
external: commonExternal,
|
||||
define: {
|
||||
'process.env.NODE_ENV': JSON.stringify('production'),
|
||||
},
|
||||
publicPath: '/',
|
||||
})
|
||||
|
||||
if (!buildResult.success) {
|
||||
console.error(`❌ JS bundling failed for ${appName}!`)
|
||||
for (const log of buildResult.logs) {
|
||||
console.error(log)
|
||||
}
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// Confirm that the bundle exists
|
||||
const finalJsFile = path.join(binOutDir, appJsName)
|
||||
if (!existsSync(finalJsFile)) {
|
||||
console.error(`❌ Could not find compiled JS bundle at ${finalJsFile}`)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// Write JS wrapper script
|
||||
const wrapperContent = `#!/usr/bin/env node
|
||||
import { spawn } from 'node:child_process'
|
||||
import { join, dirname } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { existsSync } from 'node:fs'
|
||||
import { existsSync, readFileSync } from 'node:fs'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const platform = process.platform
|
||||
@@ -239,18 +284,111 @@ if (!binaryPath) {
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const child = spawn(binaryPath, process.argv.slice(2), {
|
||||
stdio: 'inherit',
|
||||
})
|
||||
const appJsPath = join(__dirname, '${appName}-app.js')
|
||||
|
||||
child.on('close', (code) => {
|
||||
process.exit(code ?? 0)
|
||||
})
|
||||
let activeProcess = null
|
||||
|
||||
function startApp() {
|
||||
activeProcess = spawn(binaryPath, ['run', appJsPath, ...process.argv.slice(2)], {
|
||||
stdio: 'inherit',
|
||||
env: {
|
||||
...process.env,
|
||||
BUN_BE_BUN: '1',
|
||||
},
|
||||
})
|
||||
|
||||
activeProcess.on('close', (code) => {
|
||||
process.exit(code ?? 0)
|
||||
})
|
||||
|
||||
activeProcess.on('error', (err) => {
|
||||
console.error(\`Failed to start child process:\`, err)
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
|
||||
// Simple .env parser to load env files from process.cwd()
|
||||
function loadEnv() {
|
||||
const envFiles = ['.env.production.local', '.env.local', '.env.production', '.env']
|
||||
for (const file of envFiles) {
|
||||
const envPath = join(process.cwd(), file)
|
||||
if (existsSync(envPath)) {
|
||||
try {
|
||||
const content = readFileSync(envPath, 'utf8')
|
||||
const lines = content.split(/\\r?\\n/)
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim()
|
||||
if (!trimmed || trimmed.startsWith('#')) continue
|
||||
const match = trimmed.match(/^([^=]+)=(.*)$/)
|
||||
if (match) {
|
||||
const key = match[1].trim()
|
||||
let value = match[2].trim()
|
||||
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
||||
value = value.substring(1, value.length - 1)
|
||||
}
|
||||
if (process.env[key] === undefined) {
|
||||
process.env[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('⚠️ [shumai] Error reading env file:', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Load env files on startup
|
||||
loadEnv()
|
||||
|
||||
console.log(\`ℹ️ [${appName}] Running from CWD: \${process.cwd()}\`)
|
||||
console.log(\`ℹ️ [${appName}] DATABASE_URL is: \${process.env.DATABASE_URL ? '(defined)' : '(undefined)'}\`)
|
||||
|
||||
const prismaSchemaPath = join(__dirname, '..', 'prisma', 'schema.prisma')
|
||||
if (existsSync(prismaSchemaPath)) {
|
||||
console.log('🔄 Running database migrations...')
|
||||
activeProcess = spawn(
|
||||
binaryPath,
|
||||
['run', 'prisma', 'migrate', 'deploy', '--schema', './prisma/schema.prisma'],
|
||||
{
|
||||
stdio: 'inherit',
|
||||
cwd: join(__dirname, '..'),
|
||||
env: {
|
||||
...process.env,
|
||||
BUN_BE_BUN: '1',
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
activeProcess.on('close', (code) => {
|
||||
if (code !== 0) {
|
||||
console.error(\`❌ Database migration failed with code \${code}. Exiting.\`)
|
||||
process.exit(code ?? 1)
|
||||
}
|
||||
startApp()
|
||||
})
|
||||
|
||||
activeProcess.on('error', (err) => {
|
||||
console.error('❌ Failed to run database migrations:', err)
|
||||
process.exit(1)
|
||||
})
|
||||
} else {
|
||||
startApp()
|
||||
}
|
||||
|
||||
// Forward kill signals to the active child process
|
||||
const cleanupAndExit = (signal) => {
|
||||
if (activeProcess && !activeProcess.killed) {
|
||||
activeProcess.kill(signal)
|
||||
} else {
|
||||
process.exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
process.on('SIGINT', () => cleanupAndExit('SIGINT'))
|
||||
process.on('SIGTERM', () => cleanupAndExit('SIGTERM'))
|
||||
process.on('SIGQUIT', () => cleanupAndExit('SIGQUIT'))
|
||||
|
||||
child.on('error', (err) => {
|
||||
console.error(\`Failed to start child process:\`, err)
|
||||
process.exit(1)
|
||||
})
|
||||
`
|
||||
|
||||
const wrapperPath = path.join(binOutDir, `${appName}.js`)
|
||||
@@ -267,6 +405,21 @@ child.on('error', (err) => {
|
||||
recursive: true,
|
||||
})
|
||||
}
|
||||
|
||||
// Generate prisma.config.ts in the package root
|
||||
const localConfigContent = `import { defineConfig } from 'prisma/config'
|
||||
|
||||
export default defineConfig({
|
||||
schema: './prisma/schema.prisma',
|
||||
migrations: {
|
||||
path: './prisma/migrations',
|
||||
},
|
||||
datasource: {
|
||||
url: process.env.DATABASE_URL,
|
||||
},
|
||||
})
|
||||
`
|
||||
await writeFile(path.join(mainOutDir, 'prisma.config.ts'), localConfigContent, 'utf-8')
|
||||
}
|
||||
|
||||
// Generate package.json
|
||||
@@ -287,7 +440,7 @@ child.on('error', (err) => {
|
||||
bin: {
|
||||
[appName]: `./bin/${appName}.js`,
|
||||
},
|
||||
files: ['bin', ...(hasPrisma ? ['prisma'] : [])],
|
||||
files: ['bin', ...(hasPrisma ? ['prisma', 'prisma.config.ts'] : [])],
|
||||
dependencies,
|
||||
optionalDependencies,
|
||||
repository: rootPackageJson.repository,
|
||||
@@ -301,32 +454,53 @@ child.on('error', (err) => {
|
||||
}
|
||||
|
||||
// 2. Build shumai (Main app & platform binaries)
|
||||
await buildPlatformPackages('shumai', './apps/web/src/index.ts', [
|
||||
const shumaiPlugins = [
|
||||
temporalWorkflow({
|
||||
bundleOptions: {},
|
||||
}),
|
||||
tailwindPlugin,
|
||||
])
|
||||
await buildMainPackage('shumai', 'A fullstack AI-powered media workspace and workflow engine', true)
|
||||
]
|
||||
await buildPlatformPackages('shumai', dummyRunnerPath)
|
||||
await buildMainPackage({
|
||||
appName: 'shumai',
|
||||
description: 'A fullstack AI-powered media workspace and workflow engine',
|
||||
hasPrisma: true,
|
||||
entrypoint: './apps/web/src/index.ts',
|
||||
plugins: shumaiPlugins,
|
||||
})
|
||||
|
||||
// 3. Build shumai-agent (Agent app & platform binaries)
|
||||
await buildPlatformPackages('shumai-agent', './apps/agent/src/index.ts', [
|
||||
const agentPlugins = [
|
||||
temporalWorkflow({
|
||||
bundleOptions: {},
|
||||
}),
|
||||
])
|
||||
await buildMainPackage('shumai-agent', 'AI worker agent for the shumai media workspace', false)
|
||||
]
|
||||
await buildPlatformPackages('shumai-agent', dummyRunnerPath)
|
||||
await buildMainPackage({
|
||||
appName: 'shumai-agent',
|
||||
description: 'AI worker agent for the shumai media workspace',
|
||||
hasPrisma: true,
|
||||
entrypoint: './apps/agent/src/index.ts',
|
||||
plugins: agentPlugins,
|
||||
})
|
||||
|
||||
// 4. Build shumai-transcode (Transcode app & platform binaries)
|
||||
await buildPlatformPackages('shumai-transcode', './apps/transcode/src/index.ts', [
|
||||
const transcodePlugins = [
|
||||
temporalWorkflow({
|
||||
bundleOptions: {},
|
||||
}),
|
||||
])
|
||||
await buildMainPackage(
|
||||
'shumai-transcode',
|
||||
'Media transcoding worker for the shumai media workspace',
|
||||
false,
|
||||
)
|
||||
]
|
||||
await buildPlatformPackages('shumai-transcode', dummyRunnerPath)
|
||||
await buildMainPackage({
|
||||
appName: 'shumai-transcode',
|
||||
description: 'Media transcoding worker for the shumai media workspace',
|
||||
hasPrisma: true,
|
||||
entrypoint: './apps/transcode/src/index.ts',
|
||||
plugins: transcodePlugins,
|
||||
})
|
||||
|
||||
if (existsSync(dummyRunnerPath)) {
|
||||
await rm(dummyRunnerPath)
|
||||
}
|
||||
|
||||
console.log('\n✅ All applications and platform binaries built and packaged successfully!')
|
||||
|
||||
Reference in New Issue
Block a user