mirror of
https://github.com/lobehub/lobe-chat.git
synced 2026-06-14 11:40:07 +00:00
Compare commits
67 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b8b1ab6616 | |||
| 3891015a3d | |||
| 5babb7d826 | |||
| a7504b696a | |||
| 9dc4308942 | |||
| 082117998d | |||
| 9a74d6c045 | |||
| b1a4f24dc9 | |||
| c47551775b | |||
| 2d83300795 | |||
| 0915538da8 | |||
| 53fc0642e0 | |||
| a8c725abd5 | |||
| b8a7f6e9eb | |||
| bb594f87e2 | |||
| b0ee9b434e | |||
| cf2c5a1d37 | |||
| 0511e43a48 | |||
| 1f128f407f | |||
| f258a2e042 | |||
| 7996e1c431 | |||
| 93dddfc2e5 | |||
| 5e4186559b | |||
| 9bfd9bb4a5 | |||
| 9ca54135b5 | |||
| f162556607 | |||
| 3292ed83f9 | |||
| 561a38f788 | |||
| 71aaf0fac5 | |||
| 287601f8ec | |||
| b36f8781e6 | |||
| 705450a571 | |||
| 5272c7373f | |||
| fb24b6f1b7 | |||
| 2fd65fe8a3 | |||
| 35d5a2c937 | |||
| 42f40d2717 | |||
| ef8a644d8c | |||
| 81c84348bc | |||
| 8d7a0467db | |||
| e9522729c5 | |||
| cf01894077 | |||
| b5d945b1fd | |||
| cbee964582 | |||
| 87a38ad0c4 | |||
| f2d4745ad3 | |||
| 0167ac8e28 | |||
| b480227fd0 | |||
| 97ff98cada | |||
| 845d3ef58a | |||
| 906917362f | |||
| c69049d6da | |||
| 4f7356ffab | |||
| d20c82c115 | |||
| d617a6cd97 | |||
| 408391eeb6 | |||
| 4a2e671f55 | |||
| 695a261df1 | |||
| 39b723eff4 | |||
| 68937d842c | |||
| b66bc66260 | |||
| 4d06279abd | |||
| 1a8d33fbf4 | |||
| 2c086373cc | |||
| c7d49258f8 | |||
| 2280fd6ff9 | |||
| 8eb901c401 |
@@ -1,94 +0,0 @@
|
||||
---
|
||||
name: add-provider-doc
|
||||
description: Guide for adding new AI provider documentation. Use when adding documentation for a new AI provider (like OpenAI, Anthropic, etc.), including usage docs, environment variables, Docker config, and image resources. Triggers on provider documentation tasks.
|
||||
disable-model-invocation: true
|
||||
argument-hint: '[provider-name]'
|
||||
---
|
||||
|
||||
# Adding New AI Provider Documentation
|
||||
|
||||
Complete workflow for adding documentation for a new AI provider.
|
||||
|
||||
## Overview
|
||||
|
||||
1. Create usage documentation (EN + CN)
|
||||
2. Add environment variable documentation (EN + CN)
|
||||
3. Update Docker configuration files
|
||||
4. Update .env.example
|
||||
5. Prepare image resources
|
||||
|
||||
## Step 1: Create Provider Usage Documentation
|
||||
|
||||
### Required Files
|
||||
|
||||
- `docs/usage/providers/{provider-name}.mdx` (English)
|
||||
- `docs/usage/providers/{provider-name}.zh-CN.mdx` (Chinese)
|
||||
|
||||
### Key Requirements
|
||||
|
||||
- 5-6 screenshots showing the process
|
||||
- Cover image for the provider
|
||||
- Real registration and dashboard URLs
|
||||
- Pricing information callout
|
||||
- **Never include real API keys** - use placeholders
|
||||
|
||||
Reference: `docs/usage/providers/fal.mdx`
|
||||
|
||||
## Step 2: Update Environment Variables Documentation
|
||||
|
||||
### Files to Update
|
||||
|
||||
- `docs/self-hosting/environment-variables/model-provider.mdx` (EN)
|
||||
- `docs/self-hosting/environment-variables/model-provider.zh-CN.mdx` (CN)
|
||||
|
||||
### Content Format
|
||||
|
||||
```markdown
|
||||
### `{PROVIDER}_API_KEY`
|
||||
|
||||
- Type: Required
|
||||
- Description: API key from {Provider Name}
|
||||
- Example: `{api-key-format}`
|
||||
|
||||
### `{PROVIDER}_MODEL_LIST`
|
||||
|
||||
- Type: Optional
|
||||
- Description: Control model list. Use `+` to add, `-` to hide
|
||||
- Example: `-all,+model-1,+model-2=Display Name`
|
||||
```
|
||||
|
||||
## Step 3: Update Docker Files
|
||||
|
||||
Update all Dockerfiles at the **end** of ENV section:
|
||||
|
||||
- `Dockerfile`
|
||||
- `Dockerfile.database`
|
||||
- `Dockerfile.pglite`
|
||||
|
||||
```dockerfile
|
||||
# {New Provider}
|
||||
{PROVIDER}_API_KEY="" {PROVIDER}_MODEL_LIST=""
|
||||
```
|
||||
|
||||
## Step 4: Update .env.example
|
||||
|
||||
```bash
|
||||
### {Provider Name} ###
|
||||
# {PROVIDER}_API_KEY={prefix}-xxxxxxxx
|
||||
```
|
||||
|
||||
## Step 5: Image Resources
|
||||
|
||||
- Cover image
|
||||
- 3-4 API dashboard screenshots
|
||||
- 2-3 LobeHub configuration screenshots
|
||||
- Host on LobeHub CDN: `hub-apac-1.lobeobjects.space`
|
||||
|
||||
## Checklist
|
||||
|
||||
- [ ] EN + CN usage docs
|
||||
- [ ] EN + CN env var docs
|
||||
- [ ] All 3 Dockerfiles updated
|
||||
- [ ] .env.example updated
|
||||
- [ ] All images prepared
|
||||
- [ ] No real API keys in docs
|
||||
@@ -1,108 +0,0 @@
|
||||
---
|
||||
name: add-setting-env
|
||||
description: Guide for adding environment variables to configure user settings. Use when implementing server-side environment variables that control default values for user settings. Triggers on env var configuration or setting default value tasks.
|
||||
disable-model-invocation: true
|
||||
argument-hint: '[setting-name]'
|
||||
---
|
||||
|
||||
# Adding Environment Variable for User Settings
|
||||
|
||||
Add server-side environment variables to configure default values for user settings.
|
||||
|
||||
**Priority**: User Custom > Server Env Var > Hardcoded Default
|
||||
|
||||
## Steps
|
||||
|
||||
### 1. Define Environment Variable
|
||||
|
||||
Create `src/envs/<domain>.ts`:
|
||||
|
||||
```typescript
|
||||
import { createEnv } from '@t3-oss/env-nextjs';
|
||||
import { z } from 'zod';
|
||||
|
||||
export const get<Domain>Config = () => {
|
||||
return createEnv({
|
||||
server: {
|
||||
YOUR_ENV_VAR: z.coerce.number().min(MIN).max(MAX).optional(),
|
||||
},
|
||||
runtimeEnv: {
|
||||
YOUR_ENV_VAR: process.env.YOUR_ENV_VAR,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const <domain>Env = get<Domain>Config();
|
||||
```
|
||||
|
||||
### 2. Update Type (if new domain)
|
||||
|
||||
Add to `packages/types/src/serverConfig.ts`:
|
||||
|
||||
```typescript
|
||||
import { User<Domain>Config } from './user/settings';
|
||||
|
||||
export interface GlobalServerConfig {
|
||||
<domain>?: PartialDeep<User<Domain>Config>;
|
||||
}
|
||||
```
|
||||
|
||||
**Prefer reusing existing types** from `packages/types/src/user/settings`.
|
||||
|
||||
### 3. Assemble Server Config (if new domain)
|
||||
|
||||
In `src/server/globalConfig/index.ts`:
|
||||
|
||||
```typescript
|
||||
import { <domain>Env } from '@/envs/<domain>';
|
||||
|
||||
export const getServerGlobalConfig = async () => {
|
||||
const config: GlobalServerConfig = {
|
||||
<domain>: cleanObject({
|
||||
<settingName>: <domain>Env.YOUR_ENV_VAR,
|
||||
}),
|
||||
};
|
||||
return config;
|
||||
};
|
||||
```
|
||||
|
||||
### 4. Merge to User Store (if new domain)
|
||||
|
||||
In `src/store/user/slices/common/action.ts`:
|
||||
|
||||
```typescript
|
||||
const serverSettings: PartialDeep<UserSettings> = {
|
||||
<domain>: serverConfig.<domain>,
|
||||
};
|
||||
```
|
||||
|
||||
### 5. Update .env.example
|
||||
|
||||
```bash
|
||||
# <Description> (range/options, default: X)
|
||||
# YOUR_ENV_VAR=<example>
|
||||
```
|
||||
|
||||
### 6. Update Documentation
|
||||
|
||||
- `docs/self-hosting/environment-variables/basic.mdx` (EN)
|
||||
- `docs/self-hosting/environment-variables/basic.zh-CN.mdx` (CN)
|
||||
|
||||
## Example: AI_IMAGE_DEFAULT_IMAGE_NUM
|
||||
|
||||
```typescript
|
||||
// src/envs/image.ts
|
||||
AI_IMAGE_DEFAULT_IMAGE_NUM: z.coerce.number().min(1).max(20).optional(),
|
||||
|
||||
// packages/types/src/serverConfig.ts
|
||||
image?: PartialDeep<UserImageConfig>;
|
||||
|
||||
// src/server/globalConfig/index.ts
|
||||
image: cleanObject({ defaultImageNum: imageEnv.AI_IMAGE_DEFAULT_IMAGE_NUM }),
|
||||
|
||||
// src/store/user/slices/common/action.ts
|
||||
image: serverConfig.image,
|
||||
|
||||
// .env.example
|
||||
# AI_IMAGE_DEFAULT_IMAGE_NUM=4
|
||||
```
|
||||
@@ -1,209 +0,0 @@
|
||||
---
|
||||
name: agent-runtime-hooks
|
||||
description: "Agent runtime lifecycle hooks for observing and intercepting agent execution. Use when adding hooks to agent operations, mocking tool calls, logging step events, handling human intervention, sub-agent calls, context compression, or building eval/tracing integrations. Triggers on 'hooks', 'beforeToolCall', 'afterToolCall', 'beforeStep', 'afterStep', 'onComplete', 'onError', 'tool mock', 'agent lifecycle', 'human intervention', 'callAgent', 'compact'."
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# Agent Runtime Hooks
|
||||
|
||||
Lifecycle hooks for observing and intercepting agent execution. Hooks are registered per-operation via `execAgent({ hooks })` and dispatched by `HookDispatcher`.
|
||||
|
||||
## Hook Types
|
||||
|
||||
16 hook types across 5 categories:
|
||||
|
||||
```
|
||||
execAgent({ hooks })
|
||||
│
|
||||
├─ beforeStep ──────────── Before each step executes
|
||||
│ │
|
||||
│ ├─ [call_llm] LLM inference
|
||||
│ │
|
||||
│ ├─ [call_tool]
|
||||
│ │ ├─ beforeToolCall ── Before tool executes (supports mocking)
|
||||
│ │ ├─ (tool execution)
|
||||
│ │ ├─ afterToolCall ─── After tool completes (observation only)
|
||||
│ │ └─ onToolCallError ─ Tool threw an exception
|
||||
│ │
|
||||
│ ├─ [request_human_approve]
|
||||
│ │ ├─ beforeHumanIntervention ── Before agent pauses
|
||||
│ │ ├─ afterHumanIntervention ─── After approve/reject + resume
|
||||
│ │ └─ onStopByHumanIntervention ── User rejected, agent halted
|
||||
│ │
|
||||
│ ├─ [compress_context]
|
||||
│ │ ├─ beforeCompact ──── Before compression starts
|
||||
│ │ ├─ afterCompact ───── After compression completes
|
||||
│ │ └─ onCompactError ─── Compression failed
|
||||
│ │
|
||||
│ ├─ [callAgent] (via execSubAgentTask)
|
||||
│ │ ├─ beforeCallAgent ── Before sub-agent starts
|
||||
│ │ ├─ afterCallAgent ─── After sub-agent completes
|
||||
│ │ └─ onCallAgentError ── Sub-agent failed
|
||||
│ │
|
||||
│ └─ afterStep ──────────── After step completes
|
||||
│
|
||||
├─ (next step...)
|
||||
│
|
||||
├─ onComplete ───────────── Operation reaches terminal state
|
||||
└─ onError ──────────────── Error during execution
|
||||
```
|
||||
|
||||
## Key Files
|
||||
|
||||
| File | Role |
|
||||
| ---------------------------------------------------------- | ------------------------------------------------------ |
|
||||
| `packages/agent-runtime/src/types/hooks.ts` | Type definitions (AgentHookType, all event interfaces) |
|
||||
| `src/server/services/agentRuntime/hooks/types.ts` | Server-side types (AgentHook, re-exports) |
|
||||
| `src/server/services/agentRuntime/hooks/HookDispatcher.ts` | Registration, dispatch, dispatchBeforeToolCall |
|
||||
| `src/server/modules/AgentRuntime/RuntimeExecutors.ts` | Tool/Compact/HumanIntervention hook dispatch |
|
||||
| `src/server/services/agentRuntime/AgentRuntimeService.ts` | Step hooks + HumanIntervention resume/reject |
|
||||
| `src/server/services/aiAgent/index.ts` | CallAgent hook dispatch |
|
||||
|
||||
## Registration Flow
|
||||
|
||||
```ts
|
||||
const hooks: AgentHook[] = [
|
||||
{ id: 'my-hook', type: 'afterStep', handler: async (event) => { ... } },
|
||||
];
|
||||
await aiAgentService.execAgent({ agentId, prompt, hooks });
|
||||
// Internally: hookDispatcher.register(operationId, hooks)
|
||||
// Cleanup: hookDispatcher.unregister(operationId)
|
||||
```
|
||||
|
||||
## Hook Reference
|
||||
|
||||
### Step Level
|
||||
|
||||
**`beforeStep`** — Before each step. `event: AgentHookEvent`
|
||||
**`afterStep`** — After each step. `event: AgentHookEvent` (content, toolsCalling, totalCost, etc.)
|
||||
**`onComplete`** — Terminal state. `event: AgentHookEvent` (reason: done/error/interrupted/max_steps/cost_limit)
|
||||
**`onError`** — Error occurred. `event: AgentHookEvent` (errorMessage, errorDetail)
|
||||
|
||||
### Tool Call Level
|
||||
|
||||
**`beforeToolCall`** — Before tool executes. **Supports mocking** via `event.mock()`.
|
||||
|
||||
```ts
|
||||
// event: ToolCallHookEvent
|
||||
{
|
||||
(identifier, apiName, args, callIndex, stepIndex, operationId, mock);
|
||||
}
|
||||
// Mock example:
|
||||
event.mock({ content: '{"error":"rate limited"}' });
|
||||
```
|
||||
|
||||
Dispatch method: `hookDispatcher.dispatchBeforeToolCall()` (returns mock result or null).
|
||||
|
||||
**`afterToolCall`** — After tool completes. Observation only.
|
||||
|
||||
```ts
|
||||
// event: AfterToolCallHookEvent
|
||||
{
|
||||
(identifier, apiName, args, callIndex, content, success, mocked, executionTimeMs, stepIndex);
|
||||
}
|
||||
```
|
||||
|
||||
**`onToolCallError`** — Tool threw an exception (catch block, not just `success=false`).
|
||||
|
||||
```ts
|
||||
// event: ToolCallErrorHookEvent
|
||||
{
|
||||
(identifier, apiName, args, callIndex, error, stepIndex);
|
||||
}
|
||||
```
|
||||
|
||||
### Human Intervention
|
||||
|
||||
**`beforeHumanIntervention`** — Before agent pauses for approval.
|
||||
|
||||
```ts
|
||||
// event: BeforeHumanInterventionHookEvent
|
||||
{ operationId, stepIndex, pendingTools: [{ identifier, apiName }] }
|
||||
```
|
||||
|
||||
**`afterHumanIntervention`** — After approve/reject, agent resumes.
|
||||
|
||||
```ts
|
||||
// event: AfterHumanInterventionHookEvent
|
||||
{ operationId, action: 'approve' | 'reject' | 'rejectAndContinue', toolCallId?, rejectionReason? }
|
||||
```
|
||||
|
||||
**`onStopByHumanIntervention`** — User rejected, agent halted.
|
||||
|
||||
```ts
|
||||
// event: StopByHumanInterventionHookEvent
|
||||
{ operationId, toolCallId?, rejectionReason? }
|
||||
```
|
||||
|
||||
### Context Compression
|
||||
|
||||
**`beforeCompact`** — Before compression starts.
|
||||
|
||||
```ts
|
||||
// event: BeforeCompactHookEvent
|
||||
{
|
||||
(operationId, stepIndex, messageCount, tokenCount);
|
||||
}
|
||||
```
|
||||
|
||||
**`afterCompact`** — After compression completes.
|
||||
|
||||
```ts
|
||||
// event: AfterCompactHookEvent
|
||||
{
|
||||
(operationId, stepIndex, groupId, messagesBefore, messagesAfter, summary);
|
||||
}
|
||||
```
|
||||
|
||||
**`onCompactError`** — Compression failed.
|
||||
|
||||
```ts
|
||||
// event: CompactErrorHookEvent
|
||||
{
|
||||
(operationId, stepIndex, tokenCount, error);
|
||||
}
|
||||
```
|
||||
|
||||
### Sub-Agent (CallAgent)
|
||||
|
||||
**`beforeCallAgent`** — Before calling sub-agent. Dispatched on **parent** operation.
|
||||
|
||||
```ts
|
||||
// event: BeforeCallAgentHookEvent
|
||||
{
|
||||
(operationId, agentId, instruction);
|
||||
}
|
||||
```
|
||||
|
||||
**`afterCallAgent`** — Sub-agent completed. Dispatched on **parent** operation.
|
||||
|
||||
```ts
|
||||
// event: AfterCallAgentHookEvent
|
||||
{
|
||||
(operationId, agentId, subOperationId, threadId, success);
|
||||
}
|
||||
```
|
||||
|
||||
**`onCallAgentError`** — Sub-agent failed. Dispatched on **parent** operation.
|
||||
|
||||
```ts
|
||||
// event: CallAgentErrorHookEvent
|
||||
{
|
||||
(operationId, agentId, error);
|
||||
}
|
||||
```
|
||||
|
||||
Note: CallAgent hooks require `parentOperationId` in `ExecSubAgentTaskParams`.
|
||||
|
||||
## Design Notes
|
||||
|
||||
- **Fire-and-forget**: All handlers return `Promise<void>`. Errors are non-fatal.
|
||||
- **Exception**: `beforeToolCall` supports mock via `event.mock()` — uses `dispatchBeforeToolCall()` which returns the mock result.
|
||||
- **Sequential**: Same-type hooks run in registration order.
|
||||
- **Local only**: `beforeToolCall` mock only works in local mode (in-memory hooks). Webhook mode does not support mocking.
|
||||
- **Scoped per operation**: Auto-cleaned via `hookDispatcher.unregister()` on completion.
|
||||
- **Sandbox/MCP**: No separate hooks — they go through `executeTool`, so `beforeToolCall`/`afterToolCall` cover them. Use `event.identifier` to filter.
|
||||
|
||||
## Real-World Example: agent-evals
|
||||
|
||||
See `devtools/agent-evals/helpers/runner.ts` — `createEvalHooks()` uses `afterStep`, `onComplete`, `afterToolCall`, and `beforeToolCall` (for mock).
|
||||
@@ -1,95 +0,0 @@
|
||||
---
|
||||
name: agent-signal
|
||||
description: Build or extend LobeHub Agent Signal pipelines for background or quiet agent work driven by event sources, semantic signals, and action handlers. Use when adding a new Agent Signal source, signal or action type, policy, middleware handler, workflow handoff, dedupe or scope behavior, or observability around `src/server/services/agentSignal/**`, `packages/agent-signal`, or `packages/observability-otel/src/modules/agent-signal`.
|
||||
---
|
||||
|
||||
# Agent Signal
|
||||
|
||||
Use this skill to implement event-driven background work for agents without coupling the work to the foreground chat request.
|
||||
|
||||
Agent Signal has one consistent shape:
|
||||
|
||||
`source event` -> `signal interpretation` -> `action execution` -> built-in result signals
|
||||
|
||||
## Start Here
|
||||
|
||||
1. Read `references/architecture.md` to map the package boundary, runtime queue, scope model, and async workflow handoff.
|
||||
2. Read `references/handlers.md` before writing any new policy, source handler, signal handler, or action handler.
|
||||
3. Read `references/observability.md` when you need tracing, metrics, debugging, or workflow snapshot visibility.
|
||||
|
||||
## Use The Right Entry Point
|
||||
|
||||
- Use `emitAgentSignalSourceEvent(...)` when a server-owned producer should execute the pipeline immediately.
|
||||
- Use `executeAgentSignalSourceEvent(...)` when a worker or controlled backend path already owns execution timing and may inject a runtime guard backend.
|
||||
- Use `enqueueAgentSignalSourceEvent(...)` when the caller should return quickly and let Upstash Workflow process the event out-of-band.
|
||||
- Use `emitAgentSignalSourceEventWithStore(...)` for isolated tests or evals that should avoid ambient Redis state.
|
||||
|
||||
Read:
|
||||
|
||||
- `src/server/services/agentSignal/index.ts`
|
||||
- `src/server/workflows/agentSignal/index.ts`
|
||||
- `src/server/workflows/agentSignal/run.ts`
|
||||
|
||||
## Core Model
|
||||
|
||||
- `source`: A normalized fact that happened. Sources come from producers such as runtime lifecycle events, user messages, or bot ingress.
|
||||
- `signal`: A semantic interpretation derived from one source or from another signal. Signals express meaning, routing, or policy state.
|
||||
- `action`: A concrete side effect planned from one signal. Actions do the work.
|
||||
- `policy`: An installable middleware bundle that registers source, signal, and action handlers.
|
||||
- `procedure`: Not a distinct runtime node. Treat "procedure" as the end-to-end flow for one use case: ingress source, matching handlers, planned actions, execution result, and observability.
|
||||
|
||||
Keep the boundaries strict:
|
||||
|
||||
- Add a new `source` when the outside world produced a new event.
|
||||
- Add a new `signal` when the system needs a reusable semantic interpretation.
|
||||
- Add a new `action` when the runtime needs a concrete side effect.
|
||||
- Add or update a `policy` when you are wiring those pieces together.
|
||||
|
||||
## Implementation Workflow
|
||||
|
||||
1. Decide whether the use case is synchronous or quiet background work.
|
||||
2. Define or reuse a source type in `src/server/services/agentSignal/sourceTypes.ts`.
|
||||
3. Define or reuse signal and action types in `src/server/services/agentSignal/policies/types.ts`.
|
||||
4. Implement handlers with `defineSourceHandler`, `defineSignalHandler`, or `defineActionHandler`.
|
||||
5. Bundle handlers with `defineAgentSignalHandlers(...)`.
|
||||
6. Register the policy in `src/server/services/agentSignal/policies/index.ts` and pass it into the runtime factory if needed.
|
||||
7. Add or update ingress code that emits or enqueues the source event.
|
||||
8. Add observability and tests before considering the flow complete.
|
||||
|
||||
## Default Reading Set
|
||||
|
||||
- Shared semantic core:
|
||||
`packages/agent-signal/src/index.ts`
|
||||
`packages/agent-signal/src/base/builders.ts`
|
||||
`packages/agent-signal/src/base/types.ts`
|
||||
- Server-owned runtime and middleware:
|
||||
`src/server/services/agentSignal/runtime/AgentSignalRuntime.ts`
|
||||
`src/server/services/agentSignal/runtime/AgentSignalScheduler.ts`
|
||||
`src/server/services/agentSignal/runtime/middleware.ts`
|
||||
`src/server/services/agentSignal/runtime/context.ts`
|
||||
- Existing policy example:
|
||||
`src/server/services/agentSignal/policies/analyzeIntent/index.ts`
|
||||
`src/server/services/agentSignal/policies/analyzeIntent/feedbackSatisfaction.ts`
|
||||
`src/server/services/agentSignal/policies/analyzeIntent/feedbackDomain.ts`
|
||||
`src/server/services/agentSignal/policies/analyzeIntent/feedbackAction.ts`
|
||||
`src/server/services/agentSignal/policies/analyzeIntent/actions/userMemory.ts`
|
||||
- Observability:
|
||||
`src/server/services/agentSignal/observability/projector.ts`
|
||||
`src/server/services/agentSignal/observability/traceEvents.ts`
|
||||
`packages/observability-otel/src/modules/agent-signal/index.ts`
|
||||
|
||||
## Implementation Rules
|
||||
|
||||
- Reuse existing source, signal, and action types before adding new ones.
|
||||
- Keep source handlers focused on interpretation and fan-out, not heavy side effects.
|
||||
- Keep action handlers responsible for side effects, idempotency, and executor-style result reporting.
|
||||
- Use stable ids and idempotency keys when the same source can arrive more than once.
|
||||
- Preserve scope discipline. The runtime uses `scopeKey` to serialize related background work.
|
||||
- Prefer the dedicated shared package types and builders from `@lobechat/agent-signal` for normalized nodes and result contracts.
|
||||
- Add focused tests near the touched runtime, policy, or store module. Existing tests under `src/server/services/agentSignal/**/__tests__` are the reference pattern.
|
||||
|
||||
## References
|
||||
|
||||
- Architecture and boundaries: `references/architecture.md`
|
||||
- Writing handlers and policies: `references/handlers.md`
|
||||
- Observability, metrics, and debugging: `references/observability.md`
|
||||
@@ -1,4 +0,0 @@
|
||||
interface:
|
||||
display_name: 'Agent Signal'
|
||||
short_description: 'Build AgentSignal sources, signals, actions, and policies.'
|
||||
default_prompt: 'Use $agent-signal to add a new Agent Signal source, policy, handler, or observability flow.'
|
||||
@@ -1,199 +0,0 @@
|
||||
# Agent Signal Architecture
|
||||
|
||||
## Pipeline
|
||||
|
||||
Use this mental model first:
|
||||
|
||||
```text
|
||||
producer
|
||||
-> emitAgentSignalSourceEvent(...) or enqueueAgentSignalSourceEvent(...)
|
||||
-> emitSourceEvent(...)
|
||||
-> dedupe + scope lock + source normalization
|
||||
-> runtime.emitNormalized(source)
|
||||
-> source handlers
|
||||
-> signal handlers
|
||||
-> action handlers
|
||||
-> built-in result signals
|
||||
-> observability projection + persistence
|
||||
```
|
||||
|
||||
The scheduler is queue-driven, not hard-coded for one policy:
|
||||
|
||||
```text
|
||||
source node
|
||||
-> matching source handlers
|
||||
-> dispatch signals/actions
|
||||
-> matching signal handlers
|
||||
-> dispatch more signals/actions
|
||||
-> matching action handlers
|
||||
-> ExecutorResult
|
||||
-> signal.action.applied | signal.action.skipped | signal.action.failed
|
||||
```
|
||||
|
||||
Read:
|
||||
|
||||
- `src/server/services/agentSignal/index.ts`
|
||||
- `src/server/services/agentSignal/sources/index.ts`
|
||||
- `src/server/services/agentSignal/runtime/AgentSignalScheduler.ts`
|
||||
|
||||
## Package Boundaries
|
||||
|
||||
### `packages/agent-signal`
|
||||
|
||||
Treat this as the shared semantic core.
|
||||
|
||||
It provides:
|
||||
|
||||
- base node types: source, signal, action
|
||||
- builders: `createSource`, `createSignal`, `createAction`
|
||||
- built-in result signal types
|
||||
- runtime result contracts such as `RuntimeProcessorResult` and `ExecutorResult`
|
||||
|
||||
Read:
|
||||
|
||||
- `packages/agent-signal/src/base/types.ts`
|
||||
- `packages/agent-signal/src/base/builders.ts`
|
||||
- `packages/agent-signal/src/types/events.ts`
|
||||
- `packages/agent-signal/src/types/builtin.ts`
|
||||
|
||||
### `src/server/services/agentSignal`
|
||||
|
||||
Treat this as the server-owned implementation layer.
|
||||
|
||||
It owns:
|
||||
|
||||
- source catalogs and payload maps
|
||||
- policy-specific signal and action catalogs
|
||||
- middleware registration
|
||||
- runtime scheduling and guard backends
|
||||
- Redis-backed dedupe, waypoint, and policy state
|
||||
- service entrypoints for synchronous and async execution
|
||||
|
||||
### `packages/observability-otel/src/modules/agent-signal`
|
||||
|
||||
Treat this as shared OTEL ownership for Agent Signal metrics and tracer instances.
|
||||
|
||||
## Core Vocabulary
|
||||
|
||||
### Source
|
||||
|
||||
A source is the normalized external fact that started the chain.
|
||||
|
||||
Examples:
|
||||
|
||||
- `agent.user.message`
|
||||
- `runtime.before_step`
|
||||
- `runtime.after_step`
|
||||
- `client.runtime.start`
|
||||
- `bot.message.merged`
|
||||
|
||||
Define source payloads in:
|
||||
|
||||
- `src/server/services/agentSignal/sourceTypes.ts`
|
||||
|
||||
Build normalized sources in:
|
||||
|
||||
- `src/server/services/agentSignal/sources/buildSource.ts`
|
||||
- `packages/agent-signal/src/base/builders.ts`
|
||||
|
||||
### Signal
|
||||
|
||||
A signal is a semantic interpretation. Signals should be reusable and meaning-oriented.
|
||||
|
||||
Examples from `analyzeIntent`:
|
||||
|
||||
- `signal.feedback.satisfaction`
|
||||
- `signal.feedback.domain.memory`
|
||||
- `signal.feedback.domain.prompt`
|
||||
- `signal.feedback.domain.skill`
|
||||
|
||||
Define server-owned signal types in:
|
||||
|
||||
- `src/server/services/agentSignal/policies/types.ts`
|
||||
|
||||
### Action
|
||||
|
||||
An action is a concrete side effect the runtime should execute.
|
||||
|
||||
Example:
|
||||
|
||||
- `action.user-memory.handle`
|
||||
|
||||
Action handlers usually:
|
||||
|
||||
- check idempotency
|
||||
- call tools, models, or services
|
||||
- return `ExecutorResult`
|
||||
|
||||
### Policy
|
||||
|
||||
A policy is an installable bundle of handlers. It is the composition unit that turns the generic runtime into a feature.
|
||||
|
||||
Example:
|
||||
|
||||
- `createAnalyzeIntentPolicy(...)`
|
||||
|
||||
### Procedure
|
||||
|
||||
"Procedure" is not a first-class type in this runtime. Use the word to describe one end-to-end use case:
|
||||
|
||||
1. define ingress source
|
||||
2. emit or enqueue the source
|
||||
3. interpret source into signals
|
||||
4. plan actions from signals
|
||||
5. execute actions
|
||||
6. persist trace and metrics
|
||||
|
||||
When a user asks for "the procedure", document the flow above and point to the exact producer, handlers, and execution entrypoint.
|
||||
|
||||
## Scope, Deduping, And Quiet Background Work
|
||||
|
||||
`scopeKey` is the serialization boundary for related work. It is used for:
|
||||
|
||||
- source dedupe windows
|
||||
- scope locks during source generation
|
||||
- runtime guard state
|
||||
- waypoint persistence for queued processing
|
||||
|
||||
Read:
|
||||
|
||||
- `src/server/services/agentSignal/sources/index.ts`
|
||||
- `src/server/services/agentSignal/runtime/context.ts`
|
||||
- `src/server/services/agentSignal/constants.ts`
|
||||
|
||||
Use `enqueueAgentSignalSourceEvent(...)` when the work should stay quiet and out-of-band. That path:
|
||||
|
||||
1. normalizes the source envelope
|
||||
2. derives or reuses `scopeKey`
|
||||
3. triggers `AgentSignalWorkflow`
|
||||
4. executes later in `runAgentSignalWorkflow`
|
||||
|
||||
This is the preferred path when the UI request should finish immediately and the policy can run in the background.
|
||||
|
||||
Read:
|
||||
|
||||
- `src/server/workflows/agentSignal/index.ts`
|
||||
- `src/server/workflows/agentSignal/run.ts`
|
||||
|
||||
## Existing Example: `analyzeIntent`
|
||||
|
||||
Use `analyzeIntent` as the reference chain:
|
||||
|
||||
```text
|
||||
agent.user.message
|
||||
-> feedback satisfaction source handler
|
||||
-> signal.feedback.satisfaction
|
||||
-> feedback domain signal handler
|
||||
-> signal.feedback.domain.*
|
||||
-> feedback action planner
|
||||
-> action.user-memory.handle
|
||||
-> signal.action.applied | skipped | failed
|
||||
```
|
||||
|
||||
Read:
|
||||
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/index.ts`
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/feedbackSatisfaction.ts`
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/feedbackDomain.ts`
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/feedbackAction.ts`
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/actions/userMemory.ts`
|
||||
@@ -1,228 +0,0 @@
|
||||
# Writing Handlers And Policies
|
||||
|
||||
## Fluent Registration API
|
||||
|
||||
Use the middleware helpers in `src/server/services/agentSignal/runtime/middleware.ts`.
|
||||
|
||||
They provide:
|
||||
|
||||
- `defineSourceHandler(...)`
|
||||
- `defineSignalHandler(...)`
|
||||
- `defineActionHandler(...)`
|
||||
- `defineAgentSignalHandlers(...)`
|
||||
|
||||
These helpers do two jobs:
|
||||
|
||||
1. keep handler registration terse
|
||||
2. preserve strong typing when `listen` points at concrete source, signal, or action types
|
||||
|
||||
## Handler Shape
|
||||
|
||||
Each handler receives:
|
||||
|
||||
- the current runtime node
|
||||
- `RuntimeProcessorContext`
|
||||
|
||||
The context gives you:
|
||||
|
||||
- `scopeKey`
|
||||
- `now()`
|
||||
- `runtimeState.getGuardState(lane)`
|
||||
- `runtimeState.touchGuardState(lane, now?)`
|
||||
|
||||
Read:
|
||||
|
||||
- `src/server/services/agentSignal/runtime/context.ts`
|
||||
|
||||
## Return Contracts
|
||||
|
||||
Return one of these shapes:
|
||||
|
||||
- `void`: no fan-out, stop at this handler
|
||||
- `{ status: 'dispatch', signals?, actions? }`: continue the chain
|
||||
- `{ status: 'wait', pending? }`: pause for later host coordination
|
||||
- `{ status: 'schedule', nextHop }`: schedule another hop
|
||||
- `{ status: 'conclude', concluded? }`: stop with a terminal runtime result
|
||||
- `ExecutorResult`: only for action handlers that performed a concrete side effect
|
||||
|
||||
Read:
|
||||
|
||||
- `packages/agent-signal/src/base/types.ts`
|
||||
- `src/server/services/agentSignal/runtime/AgentSignalScheduler.ts`
|
||||
|
||||
## Policy Composition Pattern
|
||||
|
||||
Use `defineAgentSignalHandlers([...])` to bundle related handlers into one policy.
|
||||
|
||||
Example from `analyzeIntent`:
|
||||
|
||||
```ts
|
||||
return defineAgentSignalHandlers([
|
||||
createFeedbackSatisfactionJudgeProcessor(...),
|
||||
createFeedbackDomainJudgeSignalHandler(...),
|
||||
createFeedbackActionPlannerSignalHandler(),
|
||||
defineUserMemoryActionHandler(...),
|
||||
]);
|
||||
```
|
||||
|
||||
That bundle is later passed into the runtime via:
|
||||
|
||||
- `createDefaultAgentSignalPolicies(...)`
|
||||
- `createAgentSignalRuntime({ policies })`
|
||||
|
||||
Read:
|
||||
|
||||
- `src/server/services/agentSignal/policies/index.ts`
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/index.ts`
|
||||
|
||||
## Source Handler Pattern
|
||||
|
||||
Use a source handler when you are interpreting a producer event into semantic signals.
|
||||
|
||||
Reference:
|
||||
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/feedbackSatisfaction.ts`
|
||||
|
||||
Pattern:
|
||||
|
||||
```ts
|
||||
return defineSourceHandler(
|
||||
AGENT_SIGNAL_SOURCE_TYPES.agentUserMessage,
|
||||
'agent.user.message:my-handler',
|
||||
async (source, ctx): Promise<RuntimeProcessorResult | void> => {
|
||||
// interpret source payload
|
||||
// optionally use ctx.runtimeState
|
||||
|
||||
return {
|
||||
signals: [
|
||||
/* one or more semantic signals */
|
||||
],
|
||||
status: 'dispatch',
|
||||
};
|
||||
},
|
||||
);
|
||||
```
|
||||
|
||||
Write source handlers when:
|
||||
|
||||
- a raw message, lifecycle event, or bot ingress needs interpretation
|
||||
- the work is still semantic, not side-effectful
|
||||
|
||||
## Signal Handler Pattern
|
||||
|
||||
Use a signal handler when one semantic state should branch into more semantic states or planned actions.
|
||||
|
||||
References:
|
||||
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/feedbackDomain.ts`
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/feedbackAction.ts`
|
||||
|
||||
Pattern:
|
||||
|
||||
```ts
|
||||
return defineSignalHandler(
|
||||
MY_SIGNAL_TYPE,
|
||||
'signal.my-policy-router',
|
||||
async (signal): Promise<RuntimeProcessorResult | void> => {
|
||||
return {
|
||||
actions: [
|
||||
/* planned work */
|
||||
],
|
||||
status: 'dispatch',
|
||||
};
|
||||
},
|
||||
);
|
||||
```
|
||||
|
||||
Use signal handlers for:
|
||||
|
||||
- routing
|
||||
- fan-out
|
||||
- filtering
|
||||
- conflict resolution
|
||||
- converting interpretation into planned actions
|
||||
|
||||
## Action Handler Pattern
|
||||
|
||||
Use an action handler when the runtime should do actual work.
|
||||
|
||||
Reference:
|
||||
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/actions/userMemory.ts`
|
||||
|
||||
Pattern:
|
||||
|
||||
```ts
|
||||
return defineActionHandler(
|
||||
MY_ACTION_TYPE,
|
||||
'action.my-policy-executor',
|
||||
async (action, ctx): Promise<ExecutorResult> => {
|
||||
// run service/tool/model side effect
|
||||
// check idempotency if needed
|
||||
|
||||
return {
|
||||
actionId: action.actionId,
|
||||
attempt: {
|
||||
completedAt: ctx.now(),
|
||||
current: 1,
|
||||
startedAt,
|
||||
status: 'succeeded',
|
||||
},
|
||||
status: 'applied',
|
||||
};
|
||||
},
|
||||
);
|
||||
```
|
||||
|
||||
Keep these rules:
|
||||
|
||||
- perform idempotency checks here or immediately before side effects
|
||||
- return stable `actionId`
|
||||
- include failure detail in `error`
|
||||
- let the scheduler turn the `ExecutorResult` into built-in result signals
|
||||
|
||||
## Source, Signal, And Action Type Placement
|
||||
|
||||
Use this split:
|
||||
|
||||
- external event payloads:
|
||||
`src/server/services/agentSignal/sourceTypes.ts`
|
||||
- policy-owned signal and action payloads:
|
||||
`src/server/services/agentSignal/policies/types.ts`
|
||||
- normalized shared node contracts:
|
||||
`packages/agent-signal/src/base/types.ts`
|
||||
|
||||
Do not put app-specific signal catalogs into `packages/agent-signal`. That package should stay generic and reusable.
|
||||
|
||||
## Choosing The Right Node
|
||||
|
||||
Choose `source` when:
|
||||
|
||||
- the outside world emitted a new fact
|
||||
|
||||
Choose `signal` when:
|
||||
|
||||
- the system needs semantic meaning that downstream handlers can reuse
|
||||
|
||||
Choose `action` when:
|
||||
|
||||
- the runtime is ready for a concrete side effect
|
||||
|
||||
If a handler both interprets meaning and performs side effects, split it. That keeps chains inspectable and testable.
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
Prefer focused tests near the touched code.
|
||||
|
||||
Useful references:
|
||||
|
||||
- `src/server/services/agentSignal/runtime/__tests__/AgentSignalRuntime.test.ts`
|
||||
- `src/server/services/agentSignal/__tests__/index.integration.test.ts`
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/__tests__/*`
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/actions/__tests__/*`
|
||||
|
||||
Test at the smallest level that proves the behavior:
|
||||
|
||||
- handler unit test for one routing rule
|
||||
- runtime test for queue fan-out
|
||||
- integration test for service ingress and observability persistence
|
||||
@@ -1,118 +0,0 @@
|
||||
# Observability And Debugging
|
||||
|
||||
## OTEL Ownership
|
||||
|
||||
Use `packages/observability-otel/src/modules/agent-signal/index.ts` for the shared tracer and metrics.
|
||||
|
||||
Available instruments:
|
||||
|
||||
- `tracer`
|
||||
- `sourceCounter`
|
||||
- `signalCounter`
|
||||
- `actionCounter`
|
||||
- `actionResultCounter`
|
||||
- `chainCounter`
|
||||
- `signalActionTransitionCounter`
|
||||
- `chainDurationHistogram`
|
||||
- `actionDurationHistogram`
|
||||
|
||||
Use this module when you need shared telemetry ownership instead of creating feature-local meters or tracers.
|
||||
|
||||
## Projection Pipeline
|
||||
|
||||
After runtime execution, the service projects one compact observability model from the full chain.
|
||||
|
||||
Read:
|
||||
|
||||
- `src/server/services/agentSignal/observability/projector.ts`
|
||||
- `src/server/services/agentSignal/observability/traceEvents.ts`
|
||||
- `src/server/services/agentSignal/observability/store.ts`
|
||||
|
||||
Projection outputs:
|
||||
|
||||
- a trace envelope with source, signals, actions, results, edges, and handler runs
|
||||
- a compact telemetry record with dominant path, status breakdown, and chain metadata
|
||||
|
||||
This projection is built from:
|
||||
|
||||
- source node
|
||||
- emitted signals
|
||||
- planned actions
|
||||
- executor results
|
||||
|
||||
## How To Inspect A Chain
|
||||
|
||||
Use this order:
|
||||
|
||||
1. Inspect the source type and payload.
|
||||
2. Inspect emitted signals.
|
||||
3. Inspect planned actions.
|
||||
4. Inspect executor results.
|
||||
5. Inspect projected edges and dominant path.
|
||||
|
||||
The helper `toAgentSignalTraceEvents(...)` flattens a chain into compact event records suitable for tracing snapshots.
|
||||
|
||||
## Workflow Snapshot Bridge
|
||||
|
||||
Workflow-triggered runs do not naturally pass through the normal foreground runtime snapshot path, so `runAgentSignalWorkflow` adds a development-only bridge into `.agent-tracing/`.
|
||||
|
||||
Read:
|
||||
|
||||
- `src/server/workflows/agentSignal/run.ts`
|
||||
|
||||
Use that path when:
|
||||
|
||||
- the source was enqueued with `enqueueAgentSignalSourceEvent(...)`
|
||||
- you need local trace visibility for quiet background work
|
||||
|
||||
## Common Debug Questions
|
||||
|
||||
### The source emits but nothing happens
|
||||
|
||||
Check:
|
||||
|
||||
- feature gate enabled for the user
|
||||
- source type matches a registered source handler
|
||||
- dedupe or scope lock did not short-circuit generation
|
||||
|
||||
Read:
|
||||
|
||||
- `src/server/services/agentSignal/index.ts`
|
||||
- `src/server/services/agentSignal/sources/index.ts`
|
||||
|
||||
### The signal exists but no action runs
|
||||
|
||||
Check:
|
||||
|
||||
- the signal type has a registered signal handler
|
||||
- the signal handler returns `status: 'dispatch'`
|
||||
- the handler actually returned actions
|
||||
|
||||
### The action runs twice
|
||||
|
||||
Check:
|
||||
|
||||
- source dedupe key stability
|
||||
- action idempotency strategy
|
||||
- scope key stability across retries and workflow handoff
|
||||
|
||||
Reference:
|
||||
|
||||
- `src/server/services/agentSignal/policies/actionIdempotency.ts`
|
||||
- `src/server/services/agentSignal/policies/analyzeIntent/actions/userMemory.ts`
|
||||
|
||||
### Background runs are hard to discover
|
||||
|
||||
Check:
|
||||
|
||||
- workflow snapshot bridge in development
|
||||
- projected telemetry record contents
|
||||
- OTEL counters and histograms in the shared module
|
||||
|
||||
## Minimal Completion Checklist
|
||||
|
||||
- source ingress is testable
|
||||
- handler registration is discoverable from the policy factory
|
||||
- action executor returns structured results
|
||||
- projection includes the new path cleanly
|
||||
- tests cover at least one happy path and one no-op or failure path
|
||||
@@ -1,220 +0,0 @@
|
||||
---
|
||||
name: agent-tracing
|
||||
description: "Agent tracing CLI for inspecting agent execution snapshots. Use when user mentions 'agent-tracing', 'trace', 'snapshot', wants to debug agent execution, inspect LLM calls, view context engine data, or analyze agent steps. Triggers on agent debugging, trace inspection, or execution analysis tasks."
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# Agent Tracing CLI Guide
|
||||
|
||||
`@lobechat/agent-tracing` is a zero-config local dev tool that records agent execution snapshots to disk and provides a CLI to inspect them.
|
||||
|
||||
## How It Works
|
||||
|
||||
In `NODE_ENV=development`, `AgentRuntimeService.executeStep()` automatically records each step to `.agent-tracing/` as partial snapshots. When the operation completes, the partial is finalized into a complete `ExecutionSnapshot` JSON file.
|
||||
|
||||
**Data flow**: executeStep loop -> build `StepPresentationData` -> write partial snapshot to disk -> on completion, finalize to `.agent-tracing/{timestamp}_{traceId}.json`
|
||||
|
||||
**Context engine capture**: In `RuntimeExecutors.ts`, the `call_llm` executor emits a `context_engine_result` event after `serverMessagesEngine()` processes messages. This event carries the full `contextEngineInput` (DB messages, systemRole, model, knowledge, tools, userMemory, etc.) and the processed `output` messages (the final LLM payload).
|
||||
|
||||
## Package Location
|
||||
|
||||
```
|
||||
packages/agent-tracing/
|
||||
src/
|
||||
types.ts # ExecutionSnapshot, StepSnapshot, SnapshotSummary
|
||||
store/
|
||||
types.ts # ISnapshotStore interface
|
||||
file-store.ts # FileSnapshotStore (.agent-tracing/*.json)
|
||||
recorder/
|
||||
index.ts # appendStepToPartial(), finalizeSnapshot()
|
||||
viewer/
|
||||
index.ts # Terminal rendering: renderSnapshot, renderStepDetail, renderMessageDetail, renderSummaryTable, renderPayload, renderPayloadTools, renderMemory
|
||||
cli/
|
||||
index.ts # CLI entry point (#!/usr/bin/env bun)
|
||||
inspect.ts # Inspect command (default)
|
||||
partial.ts # Partial snapshot commands (list, inspect, clean)
|
||||
index.ts # Barrel exports
|
||||
```
|
||||
|
||||
## Data Storage
|
||||
|
||||
- Completed snapshots: `.agent-tracing/{ISO-timestamp}_{traceId-short}.json`
|
||||
- Latest symlink: `.agent-tracing/latest.json`
|
||||
- In-progress partials: `.agent-tracing/_partial/{operationId}.json`
|
||||
- `FileSnapshotStore` resolves from `process.cwd()` — **run CLI from the repo root**
|
||||
|
||||
## CLI Commands
|
||||
|
||||
All commands run from the **repo root**:
|
||||
|
||||
```bash
|
||||
# View latest trace (tree overview, `inspect` is the default command)
|
||||
agent-tracing
|
||||
agent-tracing inspect
|
||||
agent-tracing inspect <traceId>
|
||||
agent-tracing inspect latest
|
||||
|
||||
# List recent snapshots
|
||||
agent-tracing list
|
||||
agent-tracing list -l 20
|
||||
|
||||
# Inspect specific step (-s is short for --step)
|
||||
agent-tracing inspect <traceId> -s 0
|
||||
|
||||
# View messages (-m is short for --messages)
|
||||
agent-tracing inspect <traceId> -s 0 -m
|
||||
|
||||
# View full content of a specific message (by index shown in -m output)
|
||||
agent-tracing inspect <traceId> -s 0 --msg 2
|
||||
agent-tracing inspect <traceId> -s 0 --msg-input 1
|
||||
|
||||
# View tool call/result details (-t is short for --tools)
|
||||
agent-tracing inspect <traceId> -s 1 -t
|
||||
|
||||
# View raw events (-e is short for --events)
|
||||
agent-tracing inspect <traceId> -s 0 -e
|
||||
|
||||
# View runtime context (-c is short for --context)
|
||||
agent-tracing inspect <traceId> -s 0 -c
|
||||
|
||||
# View context engine input overview (-p is short for --payload)
|
||||
agent-tracing inspect <traceId> -p
|
||||
agent-tracing inspect <traceId> -s 0 -p
|
||||
|
||||
# View available tools in payload (-T is short for --payload-tools)
|
||||
agent-tracing inspect <traceId> -T
|
||||
agent-tracing inspect <traceId> -s 0 -T
|
||||
|
||||
# View user memory (-M is short for --memory)
|
||||
agent-tracing inspect <traceId> -M
|
||||
agent-tracing inspect <traceId> -s 0 -M
|
||||
|
||||
# Raw JSON output (-j is short for --json)
|
||||
agent-tracing inspect <traceId> -j
|
||||
agent-tracing inspect <traceId> -s 0 -j
|
||||
|
||||
# List in-progress partial snapshots
|
||||
agent-tracing partial list
|
||||
|
||||
# Inspect a partial (use `inspect` directly — all flags work with partial IDs)
|
||||
agent-tracing inspect <partialOperationId>
|
||||
agent-tracing inspect <partialOperationId> -T
|
||||
agent-tracing inspect <partialOperationId> -p
|
||||
|
||||
# Clean up stale partial snapshots
|
||||
agent-tracing partial clean
|
||||
```
|
||||
|
||||
## Inspect Flag Reference
|
||||
|
||||
| Flag | Short | Description | Default Step |
|
||||
| ----------------- | ----- | ------------------------------------------------------------------------------------------------- | ------------ |
|
||||
| `--step <n>` | `-s` | Target a specific step | — |
|
||||
| `--messages` | `-m` | Messages context (CE input → params → LLM payload) | — |
|
||||
| `--tools` | `-t` | Tool calls & results (what agent invoked) | — |
|
||||
| `--events` | `-e` | Raw events (llm_start, llm_result, etc.) | — |
|
||||
| `--context` | `-c` | Runtime context & payload (raw) | — |
|
||||
| `--system-role` | `-r` | Full system role content | 0 |
|
||||
| `--env` | | Environment context | 0 |
|
||||
| `--payload` | `-p` | Context engine input overview (model, knowledge, tools summary, memory summary, platform context) | 0 |
|
||||
| `--payload-tools` | `-T` | Available tools detail (plugin manifests + LLM function definitions) | 0 |
|
||||
| `--memory` | `-M` | Full user memory (persona, identity, contexts, preferences, experiences) | 0 |
|
||||
| `--diff <n>` | `-d` | Diff against step N (use with `-r` or `--env`) | — |
|
||||
| `--msg <n>` | | Full content of message N from Final LLM Payload | — |
|
||||
| `--msg-input <n>` | | Full content of message N from Context Engine Input | — |
|
||||
| `--json` | `-j` | Output as JSON (combinable with any flag above) | — |
|
||||
|
||||
Flags marked "Default Step: 0" auto-select step 0 if `--step` is not provided. All flags support `latest` or omitted traceId.
|
||||
|
||||
## Typical Debug Workflow
|
||||
|
||||
```bash
|
||||
# 1. Trigger an agent operation in the dev UI
|
||||
|
||||
# 2. See the overview
|
||||
agent-tracing inspect
|
||||
|
||||
# 3. List all traces, get traceId
|
||||
agent-tracing list
|
||||
|
||||
# 4. Quick overview of what was fed into context engine
|
||||
agent-tracing inspect -p
|
||||
|
||||
# 5. Inspect a specific step's messages to see what was sent to the LLM
|
||||
agent-tracing inspect TRACE_ID -s 0 -m
|
||||
|
||||
# 6. Drill into a truncated message for full content
|
||||
agent-tracing inspect TRACE_ID -s 0 --msg 2
|
||||
|
||||
# 7. Check available tools vs actual tool calls
|
||||
agent-tracing inspect -T # available tools
|
||||
agent-tracing inspect -s 1 -t # actual tool calls & results
|
||||
|
||||
# 8. Inspect user memory injected into the conversation
|
||||
agent-tracing inspect -M
|
||||
|
||||
# 9. Diff system role between steps (multi-step agents)
|
||||
agent-tracing inspect TRACE_ID -r -d 2
|
||||
```
|
||||
|
||||
## Key Types
|
||||
|
||||
```typescript
|
||||
interface ExecutionSnapshot {
|
||||
traceId: string;
|
||||
operationId: string;
|
||||
model?: string;
|
||||
provider?: string;
|
||||
startedAt: number;
|
||||
completedAt?: number;
|
||||
completionReason?:
|
||||
| 'done'
|
||||
| 'error'
|
||||
| 'interrupted'
|
||||
| 'max_steps'
|
||||
| 'cost_limit'
|
||||
| 'waiting_for_human';
|
||||
totalSteps: number;
|
||||
totalTokens: number;
|
||||
totalCost: number;
|
||||
error?: { type: string; message: string };
|
||||
steps: StepSnapshot[];
|
||||
}
|
||||
|
||||
interface StepSnapshot {
|
||||
stepIndex: number;
|
||||
stepType: 'call_llm' | 'call_tool';
|
||||
executionTimeMs: number;
|
||||
content?: string; // LLM output
|
||||
reasoning?: string; // Reasoning/thinking
|
||||
inputTokens?: number;
|
||||
outputTokens?: number;
|
||||
toolsCalling?: Array<{ apiName: string; identifier: string; arguments?: string }>;
|
||||
toolsResult?: Array<{
|
||||
apiName: string;
|
||||
identifier: string;
|
||||
isSuccess?: boolean;
|
||||
output?: string;
|
||||
}>;
|
||||
messages?: any[]; // DB messages before step
|
||||
context?: { phase: string; payload?: unknown; stepContext?: unknown };
|
||||
events?: Array<{ type: string; [key: string]: unknown }>;
|
||||
// context_engine_result event contains:
|
||||
// input: full contextEngineInput (messages, systemRole, model, knowledge, tools, userMemory, ...)
|
||||
// output: processed messages array (final LLM payload)
|
||||
}
|
||||
```
|
||||
|
||||
## --messages Output Structure
|
||||
|
||||
When using `--messages`, the output shows three sections (if context engine data is available):
|
||||
|
||||
1. **Context Engine Input** — DB messages passed to the engine, with `[0]`, `[1]`, ... indices. Use `--msg-input N` to view full content.
|
||||
2. **Context Engine Params** — systemRole, model, provider, knowledge, tools, userMemory, etc.
|
||||
3. **Final LLM Payload** — Processed messages after context engine (system date injection, user memory, history truncation, etc.), with `[0]`, `[1]`, ... indices. Use `--msg N` to view full content.
|
||||
|
||||
## Integration Points
|
||||
|
||||
- **Recording**: `src/server/services/agentRuntime/AgentRuntimeService.ts` — in the `executeStep()` method, after building `stepPresentationData`, writes partial snapshot in dev mode
|
||||
- **Context engine event**: `src/server/modules/AgentRuntime/RuntimeExecutors.ts` — in `call_llm` executor, after `serverMessagesEngine()` returns, emits `context_engine_result` event
|
||||
- **Store**: `FileSnapshotStore` reads/writes to `.agent-tracing/` relative to `process.cwd()`
|
||||
@@ -1,130 +0,0 @@
|
||||
---
|
||||
name: builtin-tool
|
||||
description: Build a new builtin tool package under `packages/builtin-tool-<name>/`. Use when adding a new agent-callable toolset, designing its API surface (manifest / ApiName / Params / State), implementing the Executor + ExecutionRuntime, building the Inspector / Render / Placeholder / Streaming / Intervention / Portal UI, or wiring a tool into the central registries (`packages/builtin-tools/src/{index,identifiers,inspectors,renders,placeholders,streamings,interventions,portals}.ts` and `src/store/tool/slices/builtin/executors/index.ts`). Triggers on "new builtin tool", "add a tool", "tool inspector", "tool render", "tool placeholder", "tool streaming", "tool intervention", "BuiltinToolManifest", "BaseExecutor", "ExecutionRuntime".
|
||||
---
|
||||
|
||||
# Builtin Tool Authoring Guide
|
||||
|
||||
A builtin tool is a package the agent runtime can call. It ships **five faces**:
|
||||
|
||||
| Face | Lives in | Audience |
|
||||
| -------------------- | -------------------------------------------------------------------------------------- | ------------------------------------- |
|
||||
| **Manifest + types** | `src/{manifest,types,systemRole}.ts` | The LLM (tool spec + system prompt) |
|
||||
| **ExecutionRuntime** | `src/ExecutionRuntime/` | Server / desktop / any runtime caller |
|
||||
| **Executor** | `src/client/executor/` | Frontend (wraps stores/services) |
|
||||
| **Client UI** | `src/client/{Inspector,Render,…}/` | Chat UI |
|
||||
| **Registry wiring** | `packages/builtin-tools/src/*.ts` + `src/store/tool/slices/builtin/executors/index.ts` | Framework |
|
||||
|
||||
---
|
||||
|
||||
## Read These First
|
||||
|
||||
| Question | Doc |
|
||||
| ------------------------------------------------------------------------------------ | --------------------------------------------- |
|
||||
| Where do files live? What does each face do? Wiring? | [architecture.md](references/architecture.md) |
|
||||
| How do I name the tool, design APIs, write the manifest, executor, ExecutionRuntime? | [tool-design.md](references/tool-design.md) |
|
||||
| How do I build Inspector / Render / Placeholder / Streaming / Intervention / Portal? | [ui.md](references/ui.md) |
|
||||
|
||||
---
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
- Creating a new `packages/builtin-tool-<name>/` package
|
||||
- Adding a new API method to an existing builtin tool
|
||||
- Building or restyling any of the 6 client surfaces for a tool
|
||||
- Wiring a tool into the central registries
|
||||
- Debugging "tool not found / API not found / render not showing / placeholder stuck" errors
|
||||
|
||||
---
|
||||
|
||||
## Top-Level Design Principles
|
||||
|
||||
1. **`lobe-<domain>` identifier is permanent.** It's stored in message history. Renames need `@deprecated` aliases (see `packages/builtin-tools/src/inspectors.ts:88-89`). Get it right the first time.
|
||||
2. **ApiName is an `as const` object**, not a TS enum. It doubles as the runtime list `BaseExecutor` iterates over.
|
||||
3. **Three result fields, three audiences:**
|
||||
- `content: string` → the LLM reads it
|
||||
- `state: Record<…>` → the UI's `pluginState`; **result-domain only**, never echo all params back
|
||||
- `error: { type, message, body? }` → both LLM and UI; `type` is a stable code
|
||||
4. **Split execution from frontend wiring.**
|
||||
- `src/ExecutionRuntime/` — pure runtime, no React, no Zustand, accepts services via constructor. **The default place for new logic.**
|
||||
- `src/client/executor/` — `BaseExecutor` subclass that calls `ExecutionRuntime` (or stores/services directly when frontend-only).
|
||||
5. **UI defaults to "do nothing".** Inspector is required (the header strip). Render/Placeholder/Streaming/Intervention/Portal are added **only when there's something specific to show** — empty registries are fine.
|
||||
6. **Style with `createStaticStyles + cssVar.*`** (zero-runtime). Fall back to `createStyles + token` only when you genuinely need runtime values. Use `@lobehub/ui` components, not raw antd.
|
||||
7. **i18n keys live in `src/locales/default/plugin.ts`.** Inspector titles must come from `t('builtins.<identifier>.apiName.<api>')` so something renders while args stream.
|
||||
|
||||
---
|
||||
|
||||
## Package Layout (preferred, post-2026 convention)
|
||||
|
||||
```
|
||||
packages/builtin-tool-<name>/
|
||||
├── package.json
|
||||
└── src/
|
||||
├── index.ts # exports manifest + types + systemRole + Identifier (no React, no stores)
|
||||
├── manifest.ts # BuiltinToolManifest with JSON Schema for every API
|
||||
├── types.ts # ApiName const + Params/State interfaces per API
|
||||
├── systemRole.ts # System prompt teaching the model when/how to use the APIs
|
||||
├── ExecutionRuntime/ # ✅ Default home for runtime logic (server- or anywhere-callable)
|
||||
│ └── index.ts
|
||||
└── client/
|
||||
├── index.ts # Re-exports for the registries
|
||||
├── executor/ # ✅ Frontend executor — extends BaseExecutor, often delegates to ExecutionRuntime
|
||||
│ └── index.ts
|
||||
├── Inspector/ # required — header chip per API
|
||||
├── Render/ # optional — rich result card
|
||||
├── Placeholder/ # optional — skeleton during streaming/execution
|
||||
├── Streaming/ # optional — live output renderer (e.g. RunCommand, WriteFile)
|
||||
├── Intervention/ # optional — approval / edit-before-run UI
|
||||
├── Portal/ # optional — full-screen detail view
|
||||
└── components/ # shared subcomponents used by the surfaces above
|
||||
```
|
||||
|
||||
**Older packages** (`builtin-tool-task`, `builtin-tool-calculator`, etc.) still have `src/executor/` as a sibling of `src/client/`. That's grandfathered; **don't relocate without a deliberate refactor**. New packages and new APIs added to existing packages should follow the layout above.
|
||||
|
||||
`package.json` exports map:
|
||||
|
||||
```json
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./client": "./src/client/index.ts",
|
||||
"./executor": "./src/client/executor/index.ts",
|
||||
"./executionRuntime": "./src/ExecutionRuntime/index.ts"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Authoring Checklist
|
||||
|
||||
Before opening the PR:
|
||||
|
||||
- [ ] Identifier follows `lobe-<domain>` and is **stable** (lives in message history).
|
||||
- [ ] Every `<Name>ApiName` value has: a manifest `api[]` entry, an executor method, an Inspector, an i18n `apiName.*` key.
|
||||
- [ ] `Params` interfaces match the JSON Schema; `State` interfaces match what the executor returns and what the UI surfaces read.
|
||||
- [ ] System prompt disambiguates confusable APIs and points to batch variants.
|
||||
- [ ] Runtime logic lives in `ExecutionRuntime/`; the `client/executor/` only wires stores/services and delegates.
|
||||
- [ ] Executor returns `{ success, content, state, error? }` via a single `toResult()` funnel — `content` always non-empty (default to `error.message`).
|
||||
- [ ] Inspector handles `isArgumentsStreaming`, `isLoading`, `partialArgs`, missing `pluginState`.
|
||||
- [ ] Render returns `null` until it has data; only created for APIs with rich results.
|
||||
- [ ] Placeholder added if the API has a perceivable execution lag (search, list, crawl).
|
||||
- [ ] Streaming added for APIs that emit incremental output (run command, write file, code execution).
|
||||
- [ ] Intervention added if `humanIntervention` is set in the manifest.
|
||||
- [ ] All registry files updated (see [architecture.md → Registry wiring](references/architecture.md#registry-wiring)).
|
||||
- [ ] i18n keys in `src/locales/default/plugin.ts` plus dev seeds in `en-US`/`zh-CN`.
|
||||
- [ ] `bunx vitest run --silent='passed-only' 'packages/builtin-tool-<name>'` passes.
|
||||
- [ ] `bun run type-check` passes.
|
||||
|
||||
---
|
||||
|
||||
## Reference Tools
|
||||
|
||||
Pick the closest neighbor and copy:
|
||||
|
||||
| If your tool is… | Read first |
|
||||
| ----------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
|
||||
| Pure-compute, no UI state | `packages/builtin-tool-calculator/` — `ExecutionRuntime` reuses executor (mathjs/nerdamer work everywhere) |
|
||||
| CRUD over a domain entity | `packages/builtin-tool-task/` — full Inspector + Render set, batch variants |
|
||||
| Heavy UI (Inspector/Render/Placeholder/Portal) | `packages/builtin-tool-web-browsing/` — search-style result UI, Portal for detail view |
|
||||
| Desktop / filesystem with all surfaces (incl. Streaming + Intervention) | `packages/builtin-tool-local-system/` — `ExecutionRuntime` injects an `ILocalSystemService`, executor calls it |
|
||||
| Server-side pure (no client executor) | `packages/builtin-tool-web-browsing/` — only `ExecutionRuntime` is exported; the chat client doesn't run it |
|
||||
| Needs human approval before running | `packages/builtin-tool-local-system/src/client/Intervention/` — per-API approval components |
|
||||
@@ -1,315 +0,0 @@
|
||||
# Builtin Tool Architecture
|
||||
|
||||
## The Five Faces
|
||||
|
||||
A builtin tool ships five distinct faces, each compiled into a different bundle:
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ ./ │
|
||||
│ Manifest + Types + systemRole │
|
||||
│ ─ Pure data, no React, no Node-only deps. │
|
||||
│ ─ Imported by: server (LLM tool spec), client (registries), │
|
||||
│ anyone who needs to know "what tools exist". │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ ./executionRuntime │
|
||||
│ src/ExecutionRuntime/index.ts │
|
||||
│ ─ Pure runtime logic. Accepts services via constructor — │
|
||||
│ never imports concrete services or stores directly. │
|
||||
│ ─ Imported by: server (BuiltinServerRuntimeOutput), tests, │
|
||||
│ and the client executor as a delegate. │
|
||||
│ ─ Returns: BuiltinServerRuntimeOutput { content, state, … } │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ ./executor │
|
||||
│ src/client/executor/index.ts │
|
||||
│ ─ BaseExecutor subclass. Wires Zustand stores and frontend │
|
||||
│ services into ExecutionRuntime, then funnels through │
|
||||
│ toResult() into BuiltinToolResult { content, state, error, │
|
||||
│ success }. │
|
||||
│ ─ Imported by: src/store/tool/slices/builtin/executors/ │
|
||||
│ index.ts (registered as a singleton). │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ ./client │
|
||||
│ src/client/{Inspector,Render,Placeholder,Streaming, │
|
||||
│ Intervention,Portal,components}/ │
|
||||
│ ─ React 'use client' surfaces. Read args + pluginState. │
|
||||
│ ─ Imported by: packages/builtin-tools/src/{inspectors, │
|
||||
│ renders,placeholders,streamings,interventions,portals}.ts. │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ Registry wiring │
|
||||
│ packages/builtin-tools/src/*.ts │
|
||||
│ src/store/tool/slices/builtin/executors/index.ts │
|
||||
│ ─ Aggregator maps: identifier → { apiName → component }. │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
The split exists so:
|
||||
|
||||
- Server bundles import only `./` and `./executionRuntime` and never touch React.
|
||||
- Frontend bundles import `./client` and never touch Node-only services.
|
||||
- The runtime is testable without React or Electron present.
|
||||
|
||||
---
|
||||
|
||||
## Why ExecutionRuntime is the Default Home for Logic
|
||||
|
||||
**Old pattern (grandfathered):** business logic in `src/executor/` directly. Examples: `builtin-tool-task`, older tools. Works, but the executor mixes runtime logic with frontend service plumbing — hard to reuse on the server.
|
||||
|
||||
**New pattern (preferred):** business logic in `src/ExecutionRuntime/`, frontend wiring in `src/client/executor/`. Examples: `builtin-tool-local-system`, `builtin-tool-web-browsing`, `builtin-tool-calculator`.
|
||||
|
||||
```
|
||||
ExecutionRuntime
|
||||
├─ accepts services via constructor (or `static create(opts)`)
|
||||
├─ returns BuiltinServerRuntimeOutput (content + state + success)
|
||||
└─ no React, no Zustand, no `@/services/...` direct imports
|
||||
|
||||
client/executor
|
||||
├─ extends BaseExecutor<typeof <Name>ApiName>
|
||||
├─ holds a `runtime = new <Name>ExecutionRuntime(realService)` instance
|
||||
├─ each ApiName method:
|
||||
│ 1. resolve scope / pull defaults from BuiltinToolContext
|
||||
│ 2. call runtime.<method>(args)
|
||||
│ 3. funnel through toResult() → BuiltinToolResult
|
||||
└─ exported singleton: export const <name>Executor = new <Name>Executor()
|
||||
```
|
||||
|
||||
### Service injection
|
||||
|
||||
`ExecutionRuntime` should declare a TypeScript interface for the services it needs and accept the implementation via constructor. Server callers wire in real implementations; tests wire in mocks. Example from `local-system`:
|
||||
|
||||
```ts
|
||||
export interface ILocalSystemService {
|
||||
readLocalFile: (params: any) => Promise<any>;
|
||||
writeFile: (params: any) => Promise<any>;
|
||||
/* … */
|
||||
}
|
||||
|
||||
export class LocalSystemExecutionRuntime extends ComputerRuntime {
|
||||
constructor(private service: ILocalSystemService) {
|
||||
super();
|
||||
}
|
||||
/* methods delegate to this.service.* */
|
||||
}
|
||||
```
|
||||
|
||||
The `client/executor` instantiates it once with the real service:
|
||||
|
||||
```ts
|
||||
import { localFileService } from '@/services/electron/localFileService';
|
||||
import { LocalSystemExecutionRuntime } from '../../ExecutionRuntime';
|
||||
|
||||
class LocalSystemExecutor extends BaseExecutor<typeof LocalSystemApiEnum> {
|
||||
private runtime = new LocalSystemExecutionRuntime(localFileService);
|
||||
/* … */
|
||||
}
|
||||
```
|
||||
|
||||
### When ExecutionRuntime is the only thing you ship
|
||||
|
||||
Some tools are server-only — there's no frontend executor. `builtin-tool-web-browsing` is the canonical example: only `./` and `./executionRuntime` are exported, no `./executor`, and the runtime is constructed by the server-side `ToolExecutionService`. Skip `client/executor/` entirely for those.
|
||||
|
||||
### When the executor reuses the runtime as-is
|
||||
|
||||
Pure-compute tools (`builtin-tool-calculator`) often have an executor whose ApiName methods call `executor.calculate(args)` and an `ExecutionRuntime` whose methods call `calculatorExecutor.calculate(args)` — same logic, two thin wrappers. That's fine; the duplication buys you the bundle split.
|
||||
|
||||
---
|
||||
|
||||
## The Result Contract
|
||||
|
||||
### `BuiltinServerRuntimeOutput` (what ExecutionRuntime returns)
|
||||
|
||||
```ts
|
||||
{
|
||||
content: string; // the LLM-facing text — never undefined; default to error message
|
||||
state?: any; // result-domain object the UI reads as pluginState
|
||||
success: boolean; // mandatory
|
||||
error?: any; // raw error; the executor will repackage
|
||||
}
|
||||
```
|
||||
|
||||
### `BuiltinToolResult` (what the executor returns to the runtime)
|
||||
|
||||
```ts
|
||||
{
|
||||
success: boolean;
|
||||
content?: string;
|
||||
state?: any;
|
||||
error?: { type: string; message: string; body?: any };
|
||||
metadata?: Record<string, any>; // rare; e.g. { agentCouncil: true }
|
||||
stop?: boolean; // rare; halt the orchestration step
|
||||
}
|
||||
```
|
||||
|
||||
### The `toResult` funnel (mandatory)
|
||||
|
||||
Every executor method returns through a single `toResult()` to enforce two invariants:
|
||||
|
||||
1. **`content` is never undefined.** A missing content collapses downstream into `''`, leaving the Debug pane blank while `pluginState` was already saved. See the `globLocalFiles` regression in `local-system/src/client/executor/index.ts:60-84`.
|
||||
2. **`state` survives failures.** Renderers can keep showing partial output even when `success: false`.
|
||||
|
||||
```ts
|
||||
private toResult(output: BuiltinServerRuntimeOutput): BuiltinToolResult {
|
||||
const errorMessage = typeof output.error?.message === 'string' ? output.error.message : undefined;
|
||||
const safeContent = output.content || errorMessage || 'Tool execution failed';
|
||||
|
||||
if (!output.success) {
|
||||
return {
|
||||
success: false,
|
||||
content: safeContent,
|
||||
state: output.state,
|
||||
error: output.error
|
||||
? { type: 'PluginServerError', message: errorMessage ?? safeContent, body: output.error }
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
return { success: true, content: safeContent, state: output.state };
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## `BaseExecutor` — How Method Dispatch Works
|
||||
|
||||
`BaseExecutor.invoke(apiName, params, ctx)` does:
|
||||
|
||||
```ts
|
||||
if (!this.hasApi(apiName)) return { error: { type: 'ApiNotFound', … }, success: false };
|
||||
return (this as any)[apiName](params, ctx); // method name MUST equal apiName value
|
||||
```
|
||||
|
||||
So:
|
||||
|
||||
- **Method names must equal `<Name>ApiName` values, exactly.** A typo silently routes to "ApiNotFound".
|
||||
- **Methods must be class fields, not class methods**, because `this` is lost when registry calls `executor.invoke(apiName, params, ctx)`. Always declare as `methodName = async (…) => { … }`.
|
||||
- **Always destructure `apiEnum` and `identifier` as `readonly` instance fields**, not getters — `BaseExecutor.hasApi/getApiNames` reads them synchronously.
|
||||
|
||||
---
|
||||
|
||||
## `BuiltinToolContext` — What the Executor Receives
|
||||
|
||||
The runtime hands every executor method an optional `BuiltinToolContext` as the second argument:
|
||||
|
||||
| Field | Use |
|
||||
| ----------------------------- | -------------------------------------------------------------- |
|
||||
| `agentId` | Default agent for "current agent" semantics (e.g. `listTasks`) |
|
||||
| `groupId` | Group chat scope |
|
||||
| `topicId` | Current topic — needed when creating messages/operations |
|
||||
| `taskId` | Current task identifier — fallback for "implicit" param |
|
||||
| `documentId` | Current page/document scope |
|
||||
| `messageId` | The tool message being created (for state attachments) |
|
||||
| `sourceMessageId` | The user message that triggered this tool turn |
|
||||
| `operationId` | Operation lineage (use for cancellation, tracing) |
|
||||
| `scope` | `'task' \| 'agent' \| …` — toggles default behaviors |
|
||||
| `signal: AbortSignal` | Honor for long-running ops |
|
||||
| `stepContext` | Cross-message runtime state (lobe-agent todos, etc.) |
|
||||
| `registerAfterCompletion(cb)` | Defer side-effects past message-update race |
|
||||
| `groupOrchestration` | Group orchestration callbacks |
|
||||
|
||||
**Use rule:** read with `?.`, fall back to explicit params, **never silently override** an explicit param with a context value.
|
||||
|
||||
---
|
||||
|
||||
## i18n Integration
|
||||
|
||||
Source of truth: `src/locales/default/plugin.ts`. Keys follow `builtins.<identifier>.<topic>.<…>`:
|
||||
|
||||
| Key | Use |
|
||||
| ------------------------------------- | ------------------------------------------------------------ |
|
||||
| `builtins.<identifier>.title` | Display title (overrides `manifest.meta.title` when present) |
|
||||
| `builtins.<identifier>.apiName.<api>` | Inspector header label (one per ApiName) |
|
||||
| `builtins.<identifier>.inspector.<…>` | Extra Inspector strings ("no results", chips, counters) |
|
||||
| `builtins.<identifier>.<feature>.<…>` | Render / Intervention strings, free-form per tool |
|
||||
|
||||
For dev preview, also seed `locales/zh-CN/plugin.json` and `locales/en-US/plugin.json`. Run `pnpm i18n` before opening a PR — it's slow, so do it once at the end. (See the **i18n** skill for the full workflow.)
|
||||
|
||||
---
|
||||
|
||||
## Registry Wiring
|
||||
|
||||
Five core files plus optional ones. Miss any and you'll see "tool not found", a missing chip, a blank result card, a stuck spinner, or an approval dialog that never appears.
|
||||
|
||||
| File | Add what |
|
||||
| -------------------------------------------------- | ----------------------------------------------------------------------------------------- |
|
||||
| **Required** | |
|
||||
| `packages/builtin-tools/src/index.ts` | Import `<Name>Manifest`; push entry to `builtinTools`. Set `hidden`/`discoverable` flags. |
|
||||
| `packages/builtin-tools/src/identifiers.ts` | Add `<Name>Manifest.identifier` to `builtinToolIdentifiers`. |
|
||||
| `packages/builtin-tools/src/inspectors.ts` | Import `<Name>Inspectors, <Name>Manifest`; add to `BuiltinToolInspectors`. |
|
||||
| `src/store/tool/slices/builtin/executors/index.ts` | Import `<name>Executor`; add to `registerExecutors([…])`. |
|
||||
| **Conditional — add only if the surface exists** | |
|
||||
| `packages/builtin-tools/src/renders.ts` | Add to `BuiltinToolsRenders` if any API has a Render. |
|
||||
| `packages/builtin-tools/src/placeholders.ts` | Add to `BuiltinToolPlaceholders` if any API has a Placeholder. |
|
||||
| `packages/builtin-tools/src/streamings.ts` | Add to `BuiltinToolStreamings` if any API has a Streaming renderer. |
|
||||
| `packages/builtin-tools/src/interventions.ts` | Add to `BuiltinToolInterventions` if any API has an Intervention component. |
|
||||
| `packages/builtin-tools/src/portals.ts` | Add to `BuiltinToolsPortals` if the tool has a Portal. |
|
||||
| `packages/builtin-tools/src/displayControls.ts` | Add if Render must show/hide based on result content (rare; see ClaudeCode/Codex). |
|
||||
|
||||
### Optional flags in `packages/builtin-tools/src/index.ts`
|
||||
|
||||
```ts
|
||||
{
|
||||
identifier: TaskManifest.identifier,
|
||||
manifest: TaskManifest,
|
||||
type: 'builtin',
|
||||
hidden: true, // hide from chat-input Tools popover
|
||||
discoverable: false, // exclude from agent builder / skill discovery
|
||||
}
|
||||
```
|
||||
|
||||
Lists in the same file you may need to touch:
|
||||
|
||||
- `defaultToolIds` — added to the agent's tool list by default
|
||||
- `alwaysOnToolIds` — forced on regardless of user selection (use sparingly)
|
||||
- `runtimeManagedToolIds` — enable state controlled by runtime, not user UI; **must mirror the rules map** in `src/server/modules/Mecha/AgentToolsEngine/index.ts` and `src/helpers/toolEngineering/index.ts`
|
||||
|
||||
---
|
||||
|
||||
## File-Map at a Glance
|
||||
|
||||
```
|
||||
packages/builtin-tool-<name>/
|
||||
├── package.json # exports: ., ./client, ./executor, ./executionRuntime
|
||||
└── src/
|
||||
├── index.ts # export Manifest, Identifier, types, systemPrompt
|
||||
├── manifest.ts # BuiltinToolManifest + Identifier const
|
||||
├── types.ts # ApiName + Params/State per API
|
||||
├── systemRole.ts # System prompt (multiple variants OK: systemRole.desktop.ts)
|
||||
├── ExecutionRuntime/
|
||||
│ └── index.ts # <Name>ExecutionRuntime — pure runtime, service injection
|
||||
└── client/
|
||||
├── index.ts # exports for the registries
|
||||
├── executor/
|
||||
│ └── index.ts # <Name>Executor extends BaseExecutor; export <name>Executor
|
||||
├── Inspector/
|
||||
│ ├── index.ts # <Name>Inspectors record
|
||||
│ └── <ApiName>/index.tsx # one folder per API (or .tsx file when trivial)
|
||||
├── Render/
|
||||
│ ├── index.ts # <Name>Renders record
|
||||
│ └── <ApiName>/ # rich renders → folder with subcomponents
|
||||
├── Placeholder/
|
||||
│ ├── index.ts
|
||||
│ └── <ApiName>.tsx # usually a single skeleton file
|
||||
├── Streaming/
|
||||
│ ├── index.ts
|
||||
│ └── <ApiName>/ # live-output renderer
|
||||
├── Intervention/
|
||||
│ ├── index.ts
|
||||
│ └── <ApiName>/ # approval / edit-before-run UI
|
||||
├── Portal/
|
||||
│ ├── index.tsx # routing component (switch on apiName)
|
||||
│ └── <ApiName>/ # full-screen detail view
|
||||
└── components/ # FileItem, EngineAvatar, etc. — shared subcomponents
|
||||
```
|
||||
|
||||
Skip every `client/<surface>/` directory you don't need — empty registries are fine.
|
||||
@@ -1,478 +0,0 @@
|
||||
# Tool Design (Naming, Manifest, Executor, Runtime)
|
||||
|
||||
This doc covers everything that **isn't UI**: the tool's identifier, API surface, manifest, types, system prompt, ExecutionRuntime, and the executor that wires it into the frontend.
|
||||
|
||||
For UI surfaces (Inspector / Render / Placeholder / Streaming / Intervention / Portal), see [ui.md](ui.md).
|
||||
For where files live and how registries work, see [architecture.md](architecture.md).
|
||||
|
||||
---
|
||||
|
||||
## 1. Naming
|
||||
|
||||
| Thing | Convention | Example |
|
||||
| ----------------------- | -------------------------------------------------------------- | ------------------------------------------------------------ |
|
||||
| Package directory | `packages/builtin-tool-<kebab>/` | `builtin-tool-task` |
|
||||
| npm name | `@lobechat/builtin-tool-<kebab>` | `@lobechat/builtin-tool-task` |
|
||||
| Tool `identifier` | `lobe-<kebab-domain>` — **persisted in message history** | `lobe-task`, `lobe-calculator`, `lobe-knowledge-base` |
|
||||
| Identifier const | `<Name>Identifier` exported from `manifest.ts` (or `types.ts`) | `export const TaskIdentifier = 'lobe-task'` |
|
||||
| API name const | `<Name>ApiName` — `as const` object, **camelCase verbs** | `createTask`, `listTasks`, `runTask` |
|
||||
| Executor class | `<Name>Executor extends BaseExecutor<typeof <Name>ApiName>` | `TaskExecutor` |
|
||||
| Executor singleton | `<name>Executor` (camelCase) | `export const taskExecutor = new TaskExecutor()` |
|
||||
| ExecutionRuntime class | `<Name>ExecutionRuntime` | `LocalSystemExecutionRuntime`, `WebBrowsingExecutionRuntime` |
|
||||
| Inspector / Render etc. | `<ApiName>Inspector` / `<ApiName>Render` | `CreateTaskInspector`, `SearchInspector` |
|
||||
|
||||
### Identifier rules
|
||||
|
||||
- **`lobe-` prefix is mandatory** — many switches in the codebase key off it.
|
||||
- Pick a **domain noun**, not a verb (`lobe-task`, not `lobe-task-manager`).
|
||||
- The identifier is **persisted in message history** — renaming after release means the `@deprecated` alias trick (register the legacy identifier as a second key in `inspectors.ts` / `renders.ts` pointing at the new module). Get it right the first time.
|
||||
|
||||
### ApiName rules
|
||||
|
||||
- Verb + noun, camelCase: `createTask`, `viewTask`, `runTasks`.
|
||||
- **Plural variant for batch** (`createTasks`, `runTasks`) — describe in the manifest description that it's preferred over multiple single calls. The system prompt should also push the batch form.
|
||||
- Reserve **clear separation between mutating verbs** (`updateTaskStatus`, `editTask`) and **execution verbs** (`runTask`). The system prompt must warn the model when these are confusable — see `task` for the canonical "do NOT use updateTaskStatus(running) to start a task" warning.
|
||||
- Read-only verbs: `list*`, `view*`, `get*`, `search*`. Mutating: `create*`, `edit*`, `update*`, `delete*`. Triggers/effects: `run*`, `execute*`, `submit*`.
|
||||
|
||||
---
|
||||
|
||||
## 2. `types.ts` — ApiName + Params/State
|
||||
|
||||
Define `<Name>ApiName` as `as const` so it doubles as a runtime enum (used by `BaseExecutor`) and a literal type. Then declare `Params` and `State` per API.
|
||||
|
||||
```ts
|
||||
export const TaskIdentifier = 'lobe-task';
|
||||
|
||||
export const TaskApiName = {
|
||||
createTask: 'createTask',
|
||||
createTasks: 'createTasks',
|
||||
listTasks: 'listTasks',
|
||||
/* …one entry per API, group logically (CRUD then run-style) */
|
||||
} as const;
|
||||
|
||||
export type TaskApiNameType = (typeof TaskApiName)[keyof typeof TaskApiName];
|
||||
|
||||
// One block per API
|
||||
export interface CreateTaskParams {
|
||||
name: string;
|
||||
instruction: string; /* … */
|
||||
}
|
||||
export interface CreateTaskState {
|
||||
identifier?: string;
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export interface CreateTasksParams {
|
||||
tasks: CreateTaskParams[];
|
||||
}
|
||||
export interface CreateTasksItemResult {
|
||||
error?: string;
|
||||
identifier?: string;
|
||||
name: string;
|
||||
success: boolean;
|
||||
}
|
||||
export interface CreateTasksState {
|
||||
failed: number;
|
||||
results: CreateTasksItemResult[];
|
||||
succeeded: number;
|
||||
}
|
||||
```
|
||||
|
||||
**The result-domain rule for `State`** (memory: "pluginState is result-domain, not call-domain"):
|
||||
|
||||
- Include only fields the UI **renders after the call returns** — ids the LLM didn't have when calling, counts, summary numbers, server-assigned status.
|
||||
- **Don't echo all params.** The Inspector/Render gets `args` for free.
|
||||
- Keep batch results as `{ succeeded, failed, results }` so the Render can show a one-line summary plus a detail list.
|
||||
|
||||
---
|
||||
|
||||
## 3. `manifest.ts` — JSON Schema for the LLM
|
||||
|
||||
```ts
|
||||
import type { BuiltinToolManifest } from '@lobechat/types';
|
||||
|
||||
import { systemPrompt } from './systemRole';
|
||||
import { TaskApiName, TaskIdentifier } from './types';
|
||||
|
||||
export const TaskManifest: BuiltinToolManifest = {
|
||||
identifier: TaskIdentifier,
|
||||
type: 'builtin',
|
||||
systemRole: systemPrompt,
|
||||
meta: {
|
||||
avatar: '📋',
|
||||
title: 'Task Tools',
|
||||
description: 'Create, list, edit, delete tasks with dependencies',
|
||||
readme: 'Optional long description shown in tool detail pages',
|
||||
},
|
||||
api: [
|
||||
{
|
||||
name: TaskApiName.createTask,
|
||||
description:
|
||||
'Create a new task. Optionally attach as a subtask via parentIdentifier. ' +
|
||||
'Prefer createTasks when planning a batch.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
required: ['name', 'instruction'],
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Short, descriptive name.' },
|
||||
instruction: {
|
||||
type: 'string',
|
||||
description: 'Detailed instruction for what the task should accomplish.',
|
||||
},
|
||||
parentIdentifier: {
|
||||
type: 'string',
|
||||
description:
|
||||
'Identifier of the parent task (e.g. "TASK-1"). If provided, the new task becomes a subtask.',
|
||||
},
|
||||
priority: {
|
||||
type: 'number',
|
||||
description: 'Priority level: 0=none, 1=urgent, 2=high, 3=normal, 4=low. Default is 0.',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
/* …one entry per ApiName */
|
||||
],
|
||||
};
|
||||
```
|
||||
|
||||
### Manifest writing checklist
|
||||
|
||||
- **Every API in `<Name>ApiName` has exactly one entry in `api[]`.** Easy to drift after a refactor.
|
||||
- **`description` on each API is the model's only docs.** Make it long enough for the LLM to pick the right tool. Mention edge cases ("If you provide any filter, omitted filters are not applied implicitly"), defaults, and the relationship to sibling APIs ("To START a task, use runTask — updateTaskStatus only flips a flag").
|
||||
- **`parameters` is JSON Schema** (`LobeChatPluginApi`). Use `enum`, `required`, `items`, `oneOf`, `additionalProperties: false` etc. — these survive into the LLM's tool spec.
|
||||
- **Use `additionalProperties: false`** on parameter objects so the model can't sneak unknown fields past validation.
|
||||
- **Number parameters with semantic values** (`priority: 0=none, 1=urgent, …`) should describe the mapping in the description. Don't rely on `enum` alone for numbers — the model often fills the wrong one.
|
||||
- **`enum` arrays for known string sets** (statuses, categories, engines). Spread from a constants module (`enum: [...TASK_STATUSES]`) so the manifest stays in sync.
|
||||
|
||||
### Optional manifest fields
|
||||
|
||||
```ts
|
||||
{
|
||||
/* Where this tool can run.
|
||||
'client' → Agent Gateway dispatches to the desktop client (filesystem, Electron only)
|
||||
'server' → ToolExecutionService runs it on the server
|
||||
omitted → server only */
|
||||
executors: ['client', 'server'],
|
||||
|
||||
/* Default human intervention policy for all APIs that don't specify one.
|
||||
Pair with an Intervention component (see ui.md). */
|
||||
humanIntervention: 'never' | 'always' | { /* extended config */ },
|
||||
}
|
||||
```
|
||||
|
||||
Per-API `humanIntervention` and `renderDisplayControl` go inside each `api[]` entry.
|
||||
|
||||
---
|
||||
|
||||
## 4. `systemRole.ts` — Operator Instructions for the Model
|
||||
|
||||
This is appended to the agent system prompt whenever the tool is enabled. Treat it as a **how-to-use guide for the LLM**, not marketing copy.
|
||||
|
||||
```ts
|
||||
export const systemPrompt = `You have access to Task management tools. Use them to:
|
||||
|
||||
- **createTask**: Create a new task. Use parentIdentifier to make it a subtask.
|
||||
- **createTasks**: Prefer this over multiple createTask calls when planning a batch
|
||||
(e.g. all subtasks under one parent, or all chapters of an outline).
|
||||
- **runTask**: Actually START a task — kicks off the agent in a new (or continued)
|
||||
topic. Do NOT use updateTaskStatus(running) to start a task; that only flips a
|
||||
flag without executing. The task must have an assigneeAgentId.
|
||||
- **updateTaskStatus**: Change a task's status (completed/cancelled/paused/failed).
|
||||
If you mark a task as failed, include an error message explaining why.
|
||||
- ...
|
||||
|
||||
When planning work:
|
||||
1. Create tasks for each major piece (use parentIdentifier to organize as subtasks).
|
||||
2. Use editTask with addDependencies to control execution order.
|
||||
3. Use updateTaskStatus to mark the current task completed when done.`;
|
||||
```
|
||||
|
||||
### Patterns that work well
|
||||
|
||||
- **Bulleted list, bold the API name, one line per API.** The model picks tools by skimming.
|
||||
- **Disambiguate confusable APIs explicitly** (`runTask` vs `updateTaskStatus`).
|
||||
- **Push toward batched APIs** ("Prefer this when…").
|
||||
- **End with a numbered workflow** if the tool has a typical sequence.
|
||||
- **For tools with multiple environments** (e.g. desktop vs cloud), keep variants in `systemRole.ts` and `systemRole.desktop.ts` and pick at the manifest level. See `builtin-tool-local-system`.
|
||||
|
||||
### Dynamic system prompts
|
||||
|
||||
If the prompt depends on runtime state (current date, available models), export a function and call it in the manifest:
|
||||
|
||||
```ts
|
||||
// systemRole.ts
|
||||
export const systemPrompt = (today: string) => `Today is ${today}. You have web search tools…`;
|
||||
|
||||
// manifest.ts
|
||||
import dayjs from 'dayjs';
|
||||
systemRole: systemPrompt(dayjs(new Date()).format('YYYY-MM-DD')),
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. `ExecutionRuntime/index.ts` — Pure Runtime
|
||||
|
||||
This is **the default home for new tool logic** going forward. The runtime is a class that:
|
||||
|
||||
- Has no React, no Zustand, no `@/services/...` direct imports.
|
||||
- Receives services as **constructor injection** (or as method args).
|
||||
- Returns `BuiltinServerRuntimeOutput` from each method.
|
||||
- Is unit-testable by passing in mocks.
|
||||
|
||||
### Pattern A: Inject a service interface
|
||||
|
||||
Use when the runtime calls out to IPC, network, or DB.
|
||||
|
||||
```ts
|
||||
// ExecutionRuntime/index.ts
|
||||
import type { BuiltinServerRuntimeOutput } from '@lobechat/types';
|
||||
|
||||
export interface IWebBrowsingService {
|
||||
search: (q: SearchQuery) => Promise<UniformSearchResponse>;
|
||||
crawlPages: (urls: string[]) => Promise<CrawlResults>;
|
||||
}
|
||||
|
||||
export interface WebBrowsingRuntimeOptions {
|
||||
searchService: IWebBrowsingService;
|
||||
documentService?: WebBrowsingDocumentService;
|
||||
agentId?: string;
|
||||
topicId?: string;
|
||||
}
|
||||
|
||||
export class WebBrowsingExecutionRuntime {
|
||||
constructor(private opts: WebBrowsingRuntimeOptions) {}
|
||||
|
||||
async search(
|
||||
args: SearchQuery,
|
||||
options?: { signal?: AbortSignal },
|
||||
): Promise<BuiltinServerRuntimeOutput> {
|
||||
try {
|
||||
const data = await this.opts.searchService.search(args, options);
|
||||
if (data.errorDetail) {
|
||||
return {
|
||||
success: false,
|
||||
content: data.errorDetail,
|
||||
error: { message: data.errorDetail },
|
||||
state: data,
|
||||
};
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
content: searchResultsPrompt(data.results.slice(0, 10)),
|
||||
state: data,
|
||||
};
|
||||
} catch (e) {
|
||||
return { success: false, content: (e as Error).message, error: e };
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Pattern B: Reuse the executor
|
||||
|
||||
Use when the same logic runs in browser and Node (e.g. mathjs, nerdamer). The runtime is a thin wrapper that imports the executor and re-types the state per API. See `builtin-tool-calculator/src/ExecutionRuntime/index.ts` for the canonical example.
|
||||
|
||||
### Pattern C: Extend a shared base
|
||||
|
||||
When you're implementing a domain that already has a base runtime (file ops via `ComputerRuntime`), extend and only override `callService` + result normalization. See `builtin-tool-local-system/src/ExecutionRuntime/index.ts`.
|
||||
|
||||
### Runtime contract
|
||||
|
||||
Every method returns:
|
||||
|
||||
```ts
|
||||
{
|
||||
content: string; // LLM-facing — never undefined; default to error message
|
||||
state?: any; // result-domain — what the UI's pluginState becomes
|
||||
success: boolean; // mandatory
|
||||
error?: any; // raw error object; the executor will repackage
|
||||
}
|
||||
```
|
||||
|
||||
Use `@lobechat/prompts` formatters (`searchResultsPrompt`, `crawlResultsPrompt`, `formatTaskCreated`, etc.) to produce structured `content`. They emit XML/markdown that's already tuned for token efficiency.
|
||||
|
||||
---
|
||||
|
||||
## 6. `client/executor/index.ts` — Frontend Wiring
|
||||
|
||||
The executor's job is to **resolve frontend defaults** (current agent, current task, scope) and **call the runtime**. It then funnels through `toResult()` into the `BuiltinToolResult` shape.
|
||||
|
||||
```ts
|
||||
import { BaseExecutor, type BuiltinToolContext, type BuiltinToolResult } from '@lobechat/types';
|
||||
import debug from 'debug';
|
||||
|
||||
import { taskService } from '@/services/task';
|
||||
import { getTaskStoreState } from '@/store/task';
|
||||
|
||||
import { TaskIdentifier } from '../../manifest';
|
||||
import { TaskApiName, type CreateTaskParams } from '../../types';
|
||||
|
||||
const log = debug('lobe-task:executor');
|
||||
|
||||
class TaskExecutor extends BaseExecutor<typeof TaskApiName> {
|
||||
readonly identifier = TaskIdentifier;
|
||||
protected readonly apiEnum = TaskApiName;
|
||||
|
||||
// ⚠ class FIELD, not a method — preserves `this` when invoked via registry
|
||||
createTask = async (
|
||||
params: CreateTaskParams,
|
||||
ctx?: BuiltinToolContext,
|
||||
): Promise<BuiltinToolResult> => {
|
||||
try {
|
||||
log('createTask params=%o', params);
|
||||
const task = await getTaskStoreState().createTask({
|
||||
name: params.name,
|
||||
instruction: params.instruction,
|
||||
// Default assignee from context — never silently override an explicit value
|
||||
assigneeAgentId:
|
||||
params.assigneeAgentId ?? (ctx?.scope === 'task' ? undefined : ctx?.agentId),
|
||||
parentTaskId: params.parentIdentifier?.trim() || undefined,
|
||||
priority: params.priority,
|
||||
});
|
||||
|
||||
if (!task) return this.errorResult('Failed to create task', 'CreateFailed');
|
||||
|
||||
return {
|
||||
success: true,
|
||||
content: formatTaskCreated({ identifier: task.identifier, name: task.name /* … */ }),
|
||||
state: { identifier: task.identifier, success: true },
|
||||
};
|
||||
} catch (error) {
|
||||
return this.errorResult(error, 'CreateTaskFailed');
|
||||
}
|
||||
};
|
||||
|
||||
private errorResult(err: unknown, type: string): BuiltinToolResult {
|
||||
const message = err instanceof Error ? err.message : String(err) || 'Unknown error';
|
||||
return { success: false, content: `Failed: ${message}`, error: { type, message } };
|
||||
}
|
||||
}
|
||||
|
||||
export const taskExecutor = new TaskExecutor();
|
||||
```
|
||||
|
||||
### Hard rules
|
||||
|
||||
1. **Methods are class fields** (`name = async (…) => {…}`), not class methods. The registry calls `(executor as any)[apiName](params, ctx)`; arrow-function fields keep `this` bound.
|
||||
2. **`identifier` and `apiEnum` are `readonly` instance fields**, not getters — `BaseExecutor.hasApi/getApiNames` reads them synchronously at registration time.
|
||||
3. **Default missing params from `ctx`**, but never silently override explicit values. Use `params.foo ?? ctx?.foo`, not `ctx?.foo ?? params.foo`.
|
||||
4. **One funnel for all returns.** Either always return through `toResult(runtime.x())` (when delegating) or through `errorResult(…)` for the catch arm. Never inline `{ success: false, content: '' }` — `content: ''` collapses the Debug pane to blank.
|
||||
5. **`debug('lobe-<name>:executor')`.** Match the namespace to the identifier minus `lobe-` when convenient.
|
||||
6. **Singleton export.** `export const <name>Executor = new <Name>Executor()` — the registry imports the instance, not the class.
|
||||
|
||||
### When the executor delegates to ExecutionRuntime
|
||||
|
||||
```ts
|
||||
class LocalSystemExecutor extends BaseExecutor<typeof LocalSystemApiEnum> {
|
||||
readonly identifier = LocalSystemIdentifier;
|
||||
protected readonly apiEnum = LocalSystemApiEnum;
|
||||
private runtime = new LocalSystemExecutionRuntime(localFileService);
|
||||
|
||||
readLocalFile = async (params: LocalReadFileParams): Promise<BuiltinToolResult> => {
|
||||
try {
|
||||
const result = await this.runtime.readFile({
|
||||
path: params.path,
|
||||
startLine: params.loc?.[0],
|
||||
endLine: params.loc?.[1],
|
||||
});
|
||||
return this.toResult(result);
|
||||
} catch (error) {
|
||||
return this.errorResult(error);
|
||||
}
|
||||
};
|
||||
|
||||
private toResult(out: BuiltinServerRuntimeOutput): BuiltinToolResult {
|
||||
const errMsg = typeof out.error?.message === 'string' ? out.error.message : undefined;
|
||||
const safe = out.content || errMsg || 'Tool execution failed';
|
||||
if (!out.success) {
|
||||
return {
|
||||
success: false,
|
||||
content: safe,
|
||||
state: out.state, // ← preserve partial state on failure
|
||||
error: out.error
|
||||
? { type: 'PluginServerError', message: errMsg ?? safe, body: out.error }
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
return { success: true, content: safe, state: out.state };
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The `toResult` funnel is **mandatory**: it enforces never-undefined `content` and partial-state preservation. Both invariants caught real production bugs (`globLocalFiles` Response empty, `editLocalFile` partial state lost).
|
||||
|
||||
---
|
||||
|
||||
## 7. `index.ts` — Package Entry Point
|
||||
|
||||
Keep it pure data + the manifest. **No React, no stores, no Node-only imports.**
|
||||
|
||||
```ts
|
||||
export { TaskIdentifier, TaskManifest } from './manifest';
|
||||
export { systemPrompt } from './systemRole';
|
||||
export {
|
||||
TaskApiName,
|
||||
type TaskApiNameType,
|
||||
type CreateTaskParams,
|
||||
type CreateTaskState,
|
||||
/* …all Params/State types */
|
||||
} from './types';
|
||||
|
||||
// Optional helpers used by both the runtime and the UI
|
||||
export { TASK_STATUSES, UNFINISHED_TASK_STATUSES } from './constants';
|
||||
```
|
||||
|
||||
This entry is what `packages/builtin-tools/src/index.ts` and `identifiers.ts` import — it must be importable from server bundles.
|
||||
|
||||
---
|
||||
|
||||
## 8. `package.json`
|
||||
|
||||
```json
|
||||
{
|
||||
"dependencies": {
|
||||
"@lobechat/prompts": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@lobechat/types": "workspace:*"
|
||||
},
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./client": "./src/client/index.ts",
|
||||
"./executor": "./src/client/executor/index.ts",
|
||||
"./executionRuntime": "./src/ExecutionRuntime/index.ts"
|
||||
},
|
||||
"main": "./src/index.ts",
|
||||
"name": "@lobechat/builtin-tool-<name>",
|
||||
"peerDependencies": {
|
||||
"@lobehub/ui": "^5",
|
||||
"antd": "^6",
|
||||
"antd-style": "*",
|
||||
"lucide-react": "*",
|
||||
"react": "*",
|
||||
"react-i18next": "*"
|
||||
},
|
||||
"private": true,
|
||||
"version": "1.0.0"
|
||||
}
|
||||
```
|
||||
|
||||
**Why peer not direct deps for client libs:** the `./` and `./executionRuntime` entry points must be importable from server code. Listing React etc. as peer deps prevents bundlers from following them when only the runtime is consumed.
|
||||
|
||||
**Skip `./executor`** if the package has no frontend executor (server-only tools like `builtin-tool-web-browsing`).
|
||||
|
||||
---
|
||||
|
||||
## 9. Common Pitfalls
|
||||
|
||||
| Symptom | Likely cause |
|
||||
| ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- |
|
||||
| "ApiNotFound" at runtime | Method name in executor doesn't match `ApiName` value (typo, wrong case) |
|
||||
| Method works once, then "this is undefined" | Method declared as `async fn() {}` instead of `fn = async () => {}` — `this` lost when registry invokes |
|
||||
| Debug "Response" pane blank but `pluginState` populated | Returning `content: ''` or letting `output.content` be undefined — use the `toResult` funnel |
|
||||
| Partial result vanishes on failure | `toResult` discarded `state` when `success: false`; preserve it |
|
||||
| Tool shows up but doesn't run on desktop | `executors` in manifest doesn't include `'client'` (or vice versa for server-only) |
|
||||
| Same tool registered twice / legacy identifier ghost | Identifier collision; check `@deprecated` aliases in `inspectors.ts`/`renders.ts` |
|
||||
| Manifest test fails after adding API | Forgot to add the corresponding i18n `apiName.<api>` key |
|
||||
| TypeScript error on `BaseExecutor<typeof X>` | `X` declared with `enum` instead of `as const` object — must be the const-object form |
|
||||
@@ -1,721 +0,0 @@
|
||||
# Tool UI Surfaces
|
||||
|
||||
A builtin tool can ship up to **six client-side surfaces**, each with a different role in the chat UI. Only `Inspector` is required; the other five are added on demand and registered in their own central files.
|
||||
|
||||
| Surface | Required? | When the chat shows it | Registered in |
|
||||
| ------------ | --------- | --------------------------------------------------------------------- | --------------------------------------------- |
|
||||
| Inspector | ✅ Always | Header strip of every tool call (one-line chip) | `packages/builtin-tools/src/inspectors.ts` |
|
||||
| Render | Optional | Rich result card below the header, after the call returns | `packages/builtin-tools/src/renders.ts` |
|
||||
| Placeholder | Optional | Skeleton between "args streaming complete" and "result arrives" | `packages/builtin-tools/src/placeholders.ts` |
|
||||
| Streaming | Optional | Live output during execution (e.g. command stdout) | `packages/builtin-tools/src/streamings.ts` |
|
||||
| Intervention | Optional | Approval / edit-before-run dialog (when `humanIntervention` triggers) | `packages/builtin-tools/src/interventions.ts` |
|
||||
| Portal | Optional | Full-screen detail view (right-side or modal) | `packages/builtin-tools/src/portals.ts` |
|
||||
|
||||
The two reference tools to read end-to-end:
|
||||
|
||||
- **`builtin-tool-web-browsing/src/client/`** — Inspector + Render + Placeholder + Portal (no Intervention/Streaming).
|
||||
- **`builtin-tool-local-system/src/client/`** — all six surfaces, including `components/` for shared building blocks.
|
||||
|
||||
---
|
||||
|
||||
## 0. Shared Style Rules
|
||||
|
||||
These apply across every surface.
|
||||
|
||||
### 0.1 Use `'use client'` at the top of every component file
|
||||
|
||||
Tool surfaces are leaves in the chat tree and must not block server rendering.
|
||||
|
||||
### 0.2 Prefer `createStaticStyles + cssVar.*`
|
||||
|
||||
Zero-runtime CSS-in-JS — the styles compile once and read CSS variables at runtime.
|
||||
|
||||
```tsx
|
||||
import { createStaticStyles, cssVar } from 'antd-style';
|
||||
|
||||
const styles = createStaticStyles(({ css, cssVar }) => ({
|
||||
chip: css`
|
||||
padding-block: 2px;
|
||||
padding-inline: 8px;
|
||||
border-radius: 999px;
|
||||
color: ${cssVar.colorText};
|
||||
background: ${cssVar.colorFillTertiary};
|
||||
`,
|
||||
}));
|
||||
```
|
||||
|
||||
Fall back to `createStyles + token` only when you need runtime token computation (rare). Inline `style={{ color: cssVar.colorTextSecondary }}` is fine for one-off dynamic values.
|
||||
|
||||
### 0.3 Use `@lobehub/ui`, not raw `antd`
|
||||
|
||||
`Block`, `Text`, `Flexbox`, `Highlighter`, `Alert`, `Tooltip`, `Skeleton` all come from `@lobehub/ui`. Modals come from `@lobehub/ui/base-ui` (`createModal`, `useModalContext`, `confirmModal`) — see the **modal** skill.
|
||||
|
||||
Memory note: `@lobehub/ui`'s `<Text type='secondary'>` is a lighter shade than `colorTextSecondary`. If you need that exact token color, write `<Text style={{ color: cssVar.colorTextSecondary }}>`.
|
||||
|
||||
### 0.4 Always `memo` and set `displayName`
|
||||
|
||||
```tsx
|
||||
export const SearchInspector = memo<BuiltinInspectorProps<SearchQuery, UniformSearchResponse>>(
|
||||
({ args /* … */ }) => {
|
||||
/* … */
|
||||
},
|
||||
);
|
||||
SearchInspector.displayName = 'SearchInspector';
|
||||
export default SearchInspector;
|
||||
```
|
||||
|
||||
### 0.5 Always type with `BuiltinXProps<Args, State>` generics
|
||||
|
||||
Don't widen to `any`. The Args generic is the JSON Schema params, the State generic is the executor's `state` field. The two should match `<Name>Params` and `<Name>State` from `types.ts`.
|
||||
|
||||
### 0.6 Pull strings from `t('plugin')`
|
||||
|
||||
```tsx
|
||||
const { t } = useTranslation('plugin');
|
||||
t('builtins.<identifier>.apiName.<api>');
|
||||
```
|
||||
|
||||
Every Inspector should default to `t('builtins.<identifier>.apiName.<api>')` so it shows something while args stream in.
|
||||
|
||||
### 0.7 Read store state from `@/store/chat`, not props
|
||||
|
||||
Tool surfaces sometimes need cross-cutting state (loading, streaming buffer). Read it inside the component via Zustand selectors, not from props — props only carry args/state/messageId.
|
||||
|
||||
---
|
||||
|
||||
## 1. Inspector — Header Chip (required)
|
||||
|
||||
**Lifecycle:** Inspector renders for **every phase** of a tool call: while args are streaming in, while the executor is running, and after results come back. It's the only surface that's always visible.
|
||||
|
||||
**Goal:** keep it to a single line. Show what's happening with as much context as is currently available.
|
||||
|
||||
### Props (`BuiltinInspectorProps<Args, State>`)
|
||||
|
||||
```ts
|
||||
interface BuiltinInspectorProps<Arguments = any, State = any> {
|
||||
apiName: string;
|
||||
args: Arguments; // final args (only after the assistant stops streaming)
|
||||
identifier: string;
|
||||
isArgumentsStreaming?: boolean; // args still arriving
|
||||
isLoading?: boolean; // args complete, executor running
|
||||
partialArgs?: Arguments; // partial JSON during streaming
|
||||
pluginState?: State; // executor's `state` after success
|
||||
result?: { content: string | null; error?: any };
|
||||
}
|
||||
```
|
||||
|
||||
### State machine
|
||||
|
||||
| Phase | What's available | What to show |
|
||||
| ----------------------------------- | ---------------------------------------------------------- | ---------------------------------------------------------- |
|
||||
| Args streaming, no useful field yet | `isArgumentsStreaming === true`, `partialArgs.X` undefined | Just the API title with `shinyTextStyles.shinyText` |
|
||||
| Args streaming, key field arrived | `partialArgs.X` populated | Title + key field chip, still pulse-animated |
|
||||
| Args complete, executor running | `args` populated, `isLoading === true` | Same as above, still pulse-animated |
|
||||
| Result arrived | `pluginState` populated, `isLoading === false` | Title + chips + result summary (count, identifier, status) |
|
||||
|
||||
### Canonical example — Search
|
||||
|
||||
`packages/builtin-tool-web-browsing/src/client/Inspector/Search/index.tsx`:
|
||||
|
||||
```tsx
|
||||
'use client';
|
||||
|
||||
import type { BuiltinInspectorProps, SearchQuery, UniformSearchResponse } from '@lobechat/types';
|
||||
import { Text } from '@lobehub/ui';
|
||||
import { cssVar, cx } from 'antd-style';
|
||||
import { memo } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
import { highlightTextStyles, inspectorTextStyles, shinyTextStyles } from '@/styles';
|
||||
|
||||
export const SearchInspector = memo<BuiltinInspectorProps<SearchQuery, UniformSearchResponse>>(
|
||||
({ args, partialArgs, isArgumentsStreaming, isLoading, pluginState }) => {
|
||||
const { t } = useTranslation('plugin');
|
||||
|
||||
const query = args?.query || partialArgs?.query || '';
|
||||
const resultCount = pluginState?.results?.length ?? 0;
|
||||
const hasResults = resultCount > 0;
|
||||
|
||||
if (isArgumentsStreaming && !query) {
|
||||
return (
|
||||
<div className={cx(inspectorTextStyles.root, shinyTextStyles.shinyText)}>
|
||||
<span>{t('builtins.lobe-web-browsing.apiName.search')}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cx(
|
||||
inspectorTextStyles.root,
|
||||
(isArgumentsStreaming || isLoading) && shinyTextStyles.shinyText,
|
||||
)}
|
||||
>
|
||||
<span>{t('builtins.lobe-web-browsing.apiName.search')}: </span>
|
||||
{query && <span className={highlightTextStyles.primary}>{query}</span>}
|
||||
{!isLoading &&
|
||||
!isArgumentsStreaming &&
|
||||
pluginState?.results &&
|
||||
(hasResults ? (
|
||||
<span style={{ marginInlineStart: 4 }}>({resultCount})</span>
|
||||
) : (
|
||||
<Text as="span" color={cssVar.colorTextDescription} fontSize={12}>
|
||||
({t('builtins.lobe-web-browsing.inspector.noResults')})
|
||||
</Text>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
},
|
||||
);
|
||||
SearchInspector.displayName = 'SearchInspector';
|
||||
export default SearchInspector;
|
||||
```
|
||||
|
||||
### Inspector rules
|
||||
|
||||
- Wrap the whole row with `inspectorTextStyles.root` (provides correct flex / line-height baseline).
|
||||
- Pulse with `shinyTextStyles.shinyText` whenever `isArgumentsStreaming || isLoading`.
|
||||
- Show the i18n title first so the row is non-empty during the earliest streaming phase.
|
||||
- Read both `args?.X` and `partialArgs?.X` together — `args` is final, `partialArgs` is in-stream.
|
||||
- Use chips/tags for distinct facets (identifier, name, parent, status, count). Each chip should clip with `text-overflow: ellipsis` and have a `max-width` so long values don't blow out the chat bubble.
|
||||
- Append `pluginState`-derived suffixes only **after** loading finishes — count or "(no results)" should not appear while still searching.
|
||||
|
||||
### Inspector registry — `client/Inspector/index.ts`
|
||||
|
||||
```ts
|
||||
import type { BuiltinInspector } from '@lobechat/types';
|
||||
|
||||
import { TaskApiName } from '../../types';
|
||||
import { CreateTaskInspector } from './CreateTask';
|
||||
import { ListTasksInspector } from './ListTasks';
|
||||
/* … */
|
||||
|
||||
export const TaskInspectors: Record<string, BuiltinInspector> = {
|
||||
[TaskApiName.createTask]: CreateTaskInspector as BuiltinInspector,
|
||||
[TaskApiName.listTasks]: ListTasksInspector as BuiltinInspector,
|
||||
/* one entry per ApiName */
|
||||
};
|
||||
|
||||
export { CreateTaskInspector } from './CreateTask';
|
||||
export { ListTasksInspector } from './ListTasks';
|
||||
/* re-export each */
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. Render — Rich Result Card (optional)
|
||||
|
||||
**Lifecycle:** rendered **once the result arrives** (after Placeholder/Streaming hand off). Sits below the Inspector header.
|
||||
|
||||
**Skip if** the API is read-only or the result is just text — the framework already shows the executor's `content` string. Add a Render only when there's a structured artifact worth seeing: a card, a chart, a diff, a list of files.
|
||||
|
||||
### Props (`BuiltinRenderProps<Args, State, Content>`)
|
||||
|
||||
```ts
|
||||
interface BuiltinRenderProps<Arguments = any, State = any, Content = any> {
|
||||
apiName?: string;
|
||||
args: Arguments; // final params from the LLM
|
||||
content: Content; // executor's content string (or parsed)
|
||||
identifier?: string;
|
||||
messageId: string; // for store lookups
|
||||
pluginError?: any; // from BuiltinToolResult.error
|
||||
pluginState?: State; // executor's state
|
||||
toolCallId?: string;
|
||||
}
|
||||
```
|
||||
|
||||
### Two patterns
|
||||
|
||||
**Pattern A — Single-file Render** (web-browsing CrawlSinglePage):
|
||||
|
||||
```tsx
|
||||
// client/Render/CrawlSinglePage.tsx
|
||||
import type { BuiltinRenderProps, CrawlPluginState, CrawlSinglePageQuery } from '@lobechat/types';
|
||||
import { memo } from 'react';
|
||||
|
||||
import PageContent from './PageContent';
|
||||
|
||||
const CrawlSinglePage = memo<BuiltinRenderProps<CrawlSinglePageQuery, CrawlPluginState>>(
|
||||
({ messageId, pluginState, args }) => (
|
||||
<PageContent messageId={messageId} results={pluginState?.results} urls={[args?.url]} />
|
||||
),
|
||||
);
|
||||
export default CrawlSinglePage;
|
||||
```
|
||||
|
||||
**Pattern B — Folder with subcomponents** (web-browsing Search):
|
||||
|
||||
```
|
||||
client/Render/Search/
|
||||
├── index.tsx # composes the subcomponents, handles error states
|
||||
├── ConfigForm.tsx # appears when pluginError.type === 'PluginSettingsInvalid'
|
||||
├── SearchQuery.tsx # editable query header
|
||||
└── SearchResult.tsx # result list
|
||||
```
|
||||
|
||||
Use Pattern B when the Render has internal state (editing mode, expanded items), error variants, or is large enough to benefit from splitting.
|
||||
|
||||
### Error handling in Render
|
||||
|
||||
Renders are the canonical place to surface `pluginError` because the chat doesn't auto-render typed errors:
|
||||
|
||||
```tsx
|
||||
if (pluginError) {
|
||||
if (pluginError?.type === 'PluginSettingsInvalid') {
|
||||
return <ConfigForm id={messageId} provider={pluginError.body?.provider} />;
|
||||
}
|
||||
return (
|
||||
<Alert
|
||||
title={pluginError?.message}
|
||||
type="error"
|
||||
extra={<Highlighter language="json">{JSON.stringify(pluginError.body, null, 2)}</Highlighter>}
|
||||
/>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
### Render rules
|
||||
|
||||
- **Return `null`** if there's nothing useful to draw yet (avoids empty cards during stream).
|
||||
- Use `pluginState` for server-truth (ids, counts, server-assigned status) and `args` for what the LLM asked. **Combine — neither alone is enough.**
|
||||
- For lists, summarize with a header line and show top N items with a "+N more" tail rather than rendering everything.
|
||||
- For modals from a Render, use `@lobehub/ui/base-ui` (`createModal`, `useModalContext`, `confirmModal`) — see the **modal** skill.
|
||||
|
||||
### Render registry — `client/Render/index.ts`
|
||||
|
||||
```ts
|
||||
import type { BuiltinRender } from '@lobechat/types';
|
||||
|
||||
import { TaskApiName } from '../../types';
|
||||
import CreateTaskRender from './CreateTask';
|
||||
import RunTasksRender from './RunTasks';
|
||||
|
||||
export const TaskRenders: Record<string, BuiltinRender> = {
|
||||
[TaskApiName.createTask]: CreateTaskRender as BuiltinRender,
|
||||
[TaskApiName.runTasks]: RunTasksRender as BuiltinRender,
|
||||
/* only the APIs with rich result UI — others fall back to text content */
|
||||
};
|
||||
|
||||
export { default as CreateTaskRender } from './CreateTask';
|
||||
export { default as RunTasksRender } from './RunTasks';
|
||||
```
|
||||
|
||||
### Render display control (rare)
|
||||
|
||||
If the Render should hide for certain results (e.g. ClaudeCode's TodoWrite hides when the agent is mid-stream), add a `RenderDisplayControl` to `packages/builtin-tools/src/displayControls.ts`. See `ClaudeCodeRenderDisplayControls` for the pattern.
|
||||
|
||||
---
|
||||
|
||||
## 3. Placeholder — Skeleton Between Args and Result (optional)
|
||||
|
||||
**Lifecycle:** rendered when the args have finished streaming but the executor hasn't returned yet. Disappears when `pluginState` arrives. Bridges the moment of perceived lag.
|
||||
|
||||
**Add for** APIs with noticeable execution time: web search, network crawl, file list, large grep. **Skip for** instant ops (status flips, calculator).
|
||||
|
||||
### Props (`BuiltinPlaceholderProps<Args>`)
|
||||
|
||||
```ts
|
||||
interface BuiltinPlaceholderProps<T extends Record<string, any> = any> {
|
||||
apiName: string;
|
||||
args?: T;
|
||||
identifier: string;
|
||||
}
|
||||
```
|
||||
|
||||
No `pluginState` — Placeholder lives entirely in the "executing" gap.
|
||||
|
||||
### Canonical example — Search Placeholder
|
||||
|
||||
`packages/builtin-tool-web-browsing/src/client/Placeholder/Search.tsx`:
|
||||
|
||||
```tsx
|
||||
import type { BuiltinPlaceholderProps, SearchQuery } from '@lobechat/types';
|
||||
import { Flexbox, Icon, Skeleton } from '@lobehub/ui';
|
||||
import { createStaticStyles, cx } from 'antd-style';
|
||||
import { SearchIcon } from 'lucide-react';
|
||||
import { memo } from 'react';
|
||||
|
||||
import { useIsMobile } from '@/hooks/useIsMobile';
|
||||
import { shinyTextStyles } from '@/styles';
|
||||
|
||||
const styles = createStaticStyles(({ css, cssVar }) => ({
|
||||
query: cx(
|
||||
css`
|
||||
padding: 4px 8px;
|
||||
border-radius: 8px;
|
||||
font-size: 12px;
|
||||
color: ${cssVar.colorTextSecondary};
|
||||
&:hover {
|
||||
background: ${cssVar.colorFillTertiary};
|
||||
}
|
||||
`,
|
||||
shinyTextStyles.shinyText,
|
||||
),
|
||||
}));
|
||||
|
||||
export const Search = memo<BuiltinPlaceholderProps<SearchQuery>>(({ args }) => {
|
||||
const { query } = args || {};
|
||||
const isMobile = useIsMobile();
|
||||
|
||||
return (
|
||||
<Flexbox gap={8}>
|
||||
<Flexbox horizontal={!isMobile} gap={isMobile ? 8 : 40}>
|
||||
<Flexbox horizontal align="center" className={styles.query} gap={8}>
|
||||
<Icon icon={SearchIcon} />
|
||||
{query ? query : <Skeleton.Block active style={{ height: 20, width: 40 }} />}
|
||||
</Flexbox>
|
||||
<Skeleton.Block active style={{ height: 20, width: 40 }} />
|
||||
</Flexbox>
|
||||
<Flexbox horizontal gap={12}>
|
||||
{[1, 2, 3, 4, 5].map((id) => (
|
||||
<Skeleton.Button active key={id} style={{ borderRadius: 8, height: 80, width: 160 }} />
|
||||
))}
|
||||
</Flexbox>
|
||||
</Flexbox>
|
||||
);
|
||||
});
|
||||
```
|
||||
|
||||
### Placeholder rules
|
||||
|
||||
- **Mirror the eventual Render's layout.** When the result arrives the Placeholder unmounts and the Render mounts; if they share dimensions, the chat doesn't jump.
|
||||
- Use `Skeleton.Block` / `Skeleton.Button` from `@lobehub/ui` for placeholder shapes.
|
||||
- Embed any args you have (e.g. the query text) — context helps the user know what's loading.
|
||||
- Pulse with `shinyTextStyles.shinyText` if the Placeholder includes literal text.
|
||||
|
||||
### Placeholder registry — `client/Placeholder/index.ts`
|
||||
|
||||
```ts
|
||||
import { WebBrowsingApiName } from '../../types';
|
||||
import CrawlMultiPages from './CrawlMultiPages';
|
||||
import CrawlSinglePage from './CrawlSinglePage';
|
||||
import { Search } from './Search';
|
||||
|
||||
export const WebBrowsingPlaceholders = {
|
||||
[WebBrowsingApiName.crawlMultiPages]: CrawlMultiPages,
|
||||
[WebBrowsingApiName.crawlSinglePage]: CrawlSinglePage,
|
||||
[WebBrowsingApiName.search]: Search,
|
||||
};
|
||||
|
||||
export { CrawlMultiPages, CrawlSinglePage, Search };
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Streaming — Live Output During Execution (optional)
|
||||
|
||||
**Lifecycle:** rendered **while the executor is still running** for APIs that emit incremental output. The component is responsible for fetching the in-flight stream from the chat store and rendering it.
|
||||
|
||||
**Add for** long-running ops with continuous output: shell command execution (stdout/stderr), file write progress, code interpreter cells.
|
||||
|
||||
### Props (`BuiltinStreamingProps<Args>`)
|
||||
|
||||
```ts
|
||||
interface BuiltinStreamingProps<Arguments = any> {
|
||||
apiName: string;
|
||||
args: Arguments;
|
||||
identifier: string;
|
||||
messageId: string; // use to fetch the streaming buffer from store
|
||||
toolCallId: string;
|
||||
}
|
||||
```
|
||||
|
||||
Note there's **no `state` or `result` prop** — the Streaming component is for the in-flight phase. It pulls the live buffer from the store itself (typically via `chatToolSelectors.streamingContent(messageId)` or similar).
|
||||
|
||||
### Canonical example — RunCommandStreaming
|
||||
|
||||
`packages/builtin-tool-local-system/src/client/Streaming/RunCommand/index.tsx`:
|
||||
|
||||
```tsx
|
||||
'use client';
|
||||
|
||||
import type { BuiltinStreamingProps } from '@lobechat/types';
|
||||
import { Highlighter } from '@lobehub/ui';
|
||||
import { memo } from 'react';
|
||||
|
||||
interface RunCommandParams {
|
||||
command?: string;
|
||||
description?: string;
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export const RunCommandStreaming = memo<BuiltinStreamingProps<RunCommandParams>>(({ args }) => {
|
||||
const { command } = args || {};
|
||||
if (!command) return null;
|
||||
|
||||
return (
|
||||
<Highlighter
|
||||
animated
|
||||
wrap
|
||||
language="sh"
|
||||
showLanguage={false}
|
||||
style={{ padding: '4px 8px' }}
|
||||
variant="outlined"
|
||||
>
|
||||
{command}
|
||||
</Highlighter>
|
||||
);
|
||||
});
|
||||
RunCommandStreaming.displayName = 'RunCommandStreaming';
|
||||
```
|
||||
|
||||
For real-time output beyond just the command (stderr/stdout streaming), pull from the chat store:
|
||||
|
||||
```tsx
|
||||
const buffer = useChatStore((state) =>
|
||||
chatToolSelectors.streamingBuffer(messageId, toolCallId)(state),
|
||||
);
|
||||
```
|
||||
|
||||
### Streaming rules
|
||||
|
||||
- Render `null` until you have something to display (avoids flash).
|
||||
- For terminal-style output, use `Highlighter` with `animated` to show typing-like effect.
|
||||
- The Streaming component must **unmount cleanly** when execution ends — typically the framework swaps it out for the Render automatically.
|
||||
|
||||
### Streaming registry — `client/Streaming/index.ts`
|
||||
|
||||
```ts
|
||||
import { LocalSystemApiName } from '../..';
|
||||
import { RunCommandStreaming } from './RunCommand';
|
||||
import { WriteFileStreaming } from './WriteFile';
|
||||
|
||||
export const LocalSystemStreamings = {
|
||||
[LocalSystemApiName.runCommand]: RunCommandStreaming,
|
||||
[LocalSystemApiName.writeLocalFile]: WriteFileStreaming,
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. Intervention — Approval / Edit-Before-Run (optional)
|
||||
|
||||
**Lifecycle:** rendered **before the executor runs** for APIs whose manifest sets `humanIntervention`. The user sees a preview of the args, can edit them, then approves or skips/cancels.
|
||||
|
||||
**Add for** destructive or sensitive ops: shell commands, file writes, file moves, payments, message broadcasts.
|
||||
|
||||
### Props (`BuiltinInterventionProps<Args>`)
|
||||
|
||||
```ts
|
||||
interface BuiltinInterventionProps<Arguments = any> {
|
||||
apiName?: string;
|
||||
args: Arguments;
|
||||
identifier?: string;
|
||||
interactionMode?: 'approval' | 'custom';
|
||||
messageId: string;
|
||||
|
||||
/** Called when the user edits the args; the approve action awaits this. */
|
||||
onArgsChange?: (args: Arguments) => void | Promise<void>;
|
||||
|
||||
/** Called on approve / skip / cancel. */
|
||||
onInteractionAction?: (
|
||||
action:
|
||||
| { type: 'submit'; payload: Record<string, unknown> }
|
||||
| { type: 'skip'; payload?: Record<string, unknown>; reason?: string }
|
||||
| { type: 'cancel'; payload?: Record<string, unknown> },
|
||||
) => Promise<void>;
|
||||
|
||||
/** Register a callback to flush pending saves before approval. Returns cleanup. */
|
||||
registerBeforeApprove?: (id: string, callback: () => void | Promise<void>) => () => void;
|
||||
}
|
||||
```
|
||||
|
||||
### Canonical example — RunCommand Intervention
|
||||
|
||||
`packages/builtin-tool-local-system/src/client/Intervention/RunCommand/index.tsx`:
|
||||
|
||||
```tsx
|
||||
import type { RunCommandParams } from '@lobechat/electron-client-ipc';
|
||||
import type { BuiltinInterventionProps } from '@lobechat/types';
|
||||
import { Flexbox, Highlighter, Text } from '@lobehub/ui';
|
||||
import { memo } from 'react';
|
||||
|
||||
const RunCommand = memo<BuiltinInterventionProps<RunCommandParams>>(({ args }) => {
|
||||
const { description, command, timeout } = args;
|
||||
return (
|
||||
<Flexbox gap={8}>
|
||||
<Flexbox horizontal justify="space-between">
|
||||
{description && <Text>{description}</Text>}
|
||||
{timeout && (
|
||||
<Text style={{ fontSize: 12 }} type="secondary">
|
||||
timeout: {formatTimeout(timeout)}
|
||||
</Text>
|
||||
)}
|
||||
</Flexbox>
|
||||
{command && (
|
||||
<Highlighter wrap language="sh" showLanguage={false} variant="outlined">
|
||||
{command}
|
||||
</Highlighter>
|
||||
)}
|
||||
</Flexbox>
|
||||
);
|
||||
});
|
||||
export default RunCommand;
|
||||
```
|
||||
|
||||
### Intervention rules
|
||||
|
||||
- **Show a preview, not a form by default.** Editing UI is opt-in via `onArgsChange` and is usually inline (click to edit a code block, etc.).
|
||||
- For args with debounced edit state (text fields), use `registerBeforeApprove(id, flushFn)` so the approve action waits for the debounce to flush. Always return the cleanup function.
|
||||
- Call `onInteractionAction({ type: 'submit', payload })` when the user approves; `'skip'` if they skip with a reason; `'cancel'` if they cancel the whole turn.
|
||||
- Add a corresponding `interventionAudit.ts` in the package root if the tool needs scope/path validation before approval (see `local-system/src/interventionAudit.ts`).
|
||||
|
||||
### Intervention registry — `client/Intervention/index.ts`
|
||||
|
||||
```ts
|
||||
import { LocalSystemApiName } from '../..';
|
||||
import EditLocalFile from './EditLocalFile';
|
||||
import RunCommand from './RunCommand';
|
||||
import WriteFile from './WriteFile';
|
||||
/* … */
|
||||
|
||||
export const LocalSystemInterventions = {
|
||||
[LocalSystemApiName.editLocalFile]: EditLocalFile,
|
||||
[LocalSystemApiName.runCommand]: RunCommand,
|
||||
[LocalSystemApiName.writeLocalFile]: WriteFile,
|
||||
/* one entry per API that needs approval */
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. Portal — Full-Screen Detail View (optional)
|
||||
|
||||
**Lifecycle:** rendered when the user opens the tool message in a side panel or full-screen modal. One Portal per **tool**, not per API — the Portal switches on `apiName` internally.
|
||||
|
||||
**Add for** tools whose results deserve a deep-dive view: search results with editable filters, page content with reader mode, code interpreter sessions.
|
||||
|
||||
### Props (`BuiltinPortalProps<Args, State>`)
|
||||
|
||||
```ts
|
||||
interface BuiltinPortalProps<Arguments = Record<string, any>, State = any> {
|
||||
apiName?: string;
|
||||
arguments: Arguments;
|
||||
identifier: string;
|
||||
messageId: string;
|
||||
state: State;
|
||||
}
|
||||
```
|
||||
|
||||
### Canonical example — Web-Browsing Portal
|
||||
|
||||
`packages/builtin-tool-web-browsing/src/client/Portal/index.tsx`:
|
||||
|
||||
```tsx
|
||||
import type { BuiltinPortalProps, CrawlPluginState, SearchQuery } from '@lobechat/types';
|
||||
import { memo } from 'react';
|
||||
|
||||
import { WebBrowsingApiName } from '../../types';
|
||||
import PageContent from './PageContent';
|
||||
import PageContents from './PageContents';
|
||||
import Search from './Search';
|
||||
|
||||
const Portal = memo<BuiltinPortalProps>(({ arguments: args, messageId, state, apiName }) => {
|
||||
switch (apiName) {
|
||||
case WebBrowsingApiName.search:
|
||||
return <Search messageId={messageId} query={args as SearchQuery} response={state} />;
|
||||
|
||||
case WebBrowsingApiName.crawlSinglePage: {
|
||||
const result = (state as CrawlPluginState).results.find((r) => r.originalUrl === args.url);
|
||||
return <PageContent messageId={messageId} result={result} />;
|
||||
}
|
||||
|
||||
case WebBrowsingApiName.crawlMultiPages:
|
||||
return (
|
||||
<PageContents
|
||||
messageId={messageId}
|
||||
results={(state as CrawlPluginState).results}
|
||||
urls={args.urls}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
export default Portal;
|
||||
```
|
||||
|
||||
### Portal rules
|
||||
|
||||
- One Portal per tool — the file is the routing layer, subcomponents implement each API's view.
|
||||
- Portals can read the chat store directly to detect "still streaming" and render a Skeleton internally (see `Search/index.tsx:20-46`).
|
||||
- Layout assumes more space than the Render — use `Flexbox` with `height={'100%'}` and structure for a side panel viewport.
|
||||
|
||||
### Portal registry — `packages/builtin-tools/src/portals.ts`
|
||||
|
||||
```ts
|
||||
import { WebBrowsingManifest, WebBrowsingPortal } from '@lobechat/builtin-tool-web-browsing/client';
|
||||
import { type BuiltinPortal } from '@lobechat/types';
|
||||
|
||||
export const BuiltinToolsPortals: Record<string, BuiltinPortal> = {
|
||||
[WebBrowsingManifest.identifier]: WebBrowsingPortal as BuiltinPortal,
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7. `client/components/` — Shared Subcomponents
|
||||
|
||||
Cross-cutting building blocks used by multiple surfaces live here, not duplicated in each surface folder.
|
||||
|
||||
Examples from `web-browsing/src/client/components/`:
|
||||
|
||||
- `CategoryAvatar.tsx` — search category icon
|
||||
- `EngineAvatar.tsx` — search engine logo (used in Inspector chip + Render list + Portal header)
|
||||
- `SearchBar.tsx` — editable query bar (used in Render and Portal)
|
||||
|
||||
Examples from `local-system/src/client/components/`:
|
||||
|
||||
- `FileItem.tsx` — single file row (used in ListFiles Render, SearchFiles Render, MoveLocalFiles Render)
|
||||
- `FilePathDisplay.tsx` — path with truncation (used everywhere)
|
||||
|
||||
### Rules
|
||||
|
||||
- Live under `client/components/`, exported via `client/components/index.ts`.
|
||||
- Re-export from `client/index.ts` only if other packages need them; otherwise keep internal.
|
||||
- Keep them dumb — props in, JSX out, no store reads. The store reads belong in the surface that composes them.
|
||||
|
||||
---
|
||||
|
||||
## 8. `client/index.ts` — Package Public API
|
||||
|
||||
Re-exports everything the registries need plus useful types/manifest:
|
||||
|
||||
```ts
|
||||
// Inspector — required
|
||||
export { TaskInspectors } from './Inspector';
|
||||
|
||||
// Render — only if any API has one
|
||||
export { TaskRenders, CreateTaskRender, RunTasksRender } from './Render';
|
||||
|
||||
// Placeholder / Streaming / Intervention — only if used
|
||||
export { LocalSystemListFilesPlaceholder, LocalSystemSearchFilesPlaceholder } from './Placeholder';
|
||||
export { LocalSystemStreamings } from './Streaming';
|
||||
export { LocalSystemInterventions } from './Intervention';
|
||||
|
||||
// Portal — single export per tool
|
||||
export { default as WebBrowsingPortal } from './Portal';
|
||||
|
||||
// Reusable components if other packages need them
|
||||
export { CategoryAvatar, EngineAvatar, SearchBar } from './components';
|
||||
|
||||
// Re-export manifest, identifier, types for convenience
|
||||
export { TaskManifest, TaskIdentifier } from '../manifest';
|
||||
export * from '../types';
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 9. Diagnostic Quick-Lookup
|
||||
|
||||
| Symptom | Surface to check | | |
|
||||
| ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- | --- | ------------------------- |
|
||||
| No header at all on the tool call | Inspector missing from `client/Inspector/index.ts` registry | | |
|
||||
| Header shows the API name but no chips | Inspector missing \`args?.X | | partialArgs?.X\` fallback |
|
||||
| Header doesn't pulse during loading | Missing `shinyTextStyles.shinyText` on `isArgumentsStreaming \|\| isLoading` | | |
|
||||
| Empty result card under header | Render returned `<div />` instead of `null` when no data | | |
|
||||
| Layout jump when result arrives | Placeholder dimensions don't match Render dimensions | | |
|
||||
| Approval dialog never appears | Manifest missing `humanIntervention`, or Intervention not in registry | | |
|
||||
| Approval click doesn't wait for inline edit | Missing `registerBeforeApprove(id, flushFn)` | | |
|
||||
| Portal opens but blank | Switch in `Portal/index.tsx` doesn't cover the apiName | | |
|
||||
| Strings show as `builtins.lobe-foo.apiName.bar` | Missing i18n key in `src/locales/default/plugin.ts` (or not seeded in dev locale files) | | |
|
||||
| Wrong color shade on `<Text type="secondary">` | `type='secondary'` is lighter than `colorTextSecondary` — pass via `style={{ color: cssVar.colorTextSecondary }}` | | |
|
||||
@@ -1,154 +0,0 @@
|
||||
---
|
||||
name: chat-sdk
|
||||
description: >
|
||||
Build multi-platform chat bots with Chat SDK (`chat` npm package). Use when developers want to
|
||||
(1) Build a Slack, Teams, Google Chat, Discord, GitHub, or Linear bot,
|
||||
(2) Use the Chat SDK to handle mentions, messages, reactions, slash commands, cards, modals, or streaming,
|
||||
(3) Set up webhook handlers for chat platforms,
|
||||
(4) Send interactive cards or stream AI responses to chat platforms.
|
||||
Triggers on "chat sdk", "chat bot", "slack bot", "teams bot", "discord bot", "@chat-adapter",
|
||||
building bots that work across multiple chat platforms.
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# Chat SDK
|
||||
|
||||
Unified TypeScript SDK for building chat bots across Slack, Teams, Google Chat, Discord, GitHub, and Linear. Write bot logic once, deploy everywhere.
|
||||
|
||||
## Critical: Read the bundled docs
|
||||
|
||||
The `chat` package ships with full documentation in `node_modules/chat/docs/` and TypeScript source types. **Always read these before writing code:**
|
||||
|
||||
```
|
||||
node_modules/chat/docs/ # Full documentation (MDX files)
|
||||
node_modules/chat/dist/ # Built types (.d.ts files)
|
||||
```
|
||||
|
||||
Key docs to read based on task:
|
||||
|
||||
- `docs/getting-started.mdx` — setup guides
|
||||
- `docs/usage.mdx` — event handlers, threads, messages, channels
|
||||
- `docs/streaming.mdx` — AI streaming with AI SDK
|
||||
- `docs/cards.mdx` — JSX interactive cards
|
||||
- `docs/actions.mdx` — button/dropdown handlers
|
||||
- `docs/modals.mdx` — form dialogs (Slack only)
|
||||
- `docs/adapters/*.mdx` — platform-specific adapter setup
|
||||
- `docs/state/*.mdx` — state adapter config (Redis, ioredis, memory)
|
||||
|
||||
Also read the TypeScript types from `node_modules/chat/dist/` to understand the full API surface.
|
||||
|
||||
## Quick start
|
||||
|
||||
```typescript
|
||||
import { Chat } from 'chat';
|
||||
import { createSlackAdapter } from '@chat-adapter/slack';
|
||||
import { createRedisState } from '@chat-adapter/state-redis';
|
||||
|
||||
const bot = new Chat({
|
||||
userName: 'mybot',
|
||||
adapters: {
|
||||
slack: createSlackAdapter({
|
||||
botToken: process.env.SLACK_BOT_TOKEN!,
|
||||
signingSecret: process.env.SLACK_SIGNING_SECRET!,
|
||||
}),
|
||||
},
|
||||
state: createRedisState({ url: process.env.REDIS_URL! }),
|
||||
});
|
||||
|
||||
bot.onNewMention(async (thread) => {
|
||||
await thread.subscribe();
|
||||
await thread.post("Hello! I'm listening to this thread.");
|
||||
});
|
||||
|
||||
bot.onSubscribedMessage(async (thread, message) => {
|
||||
await thread.post(`You said: ${message.text}`);
|
||||
});
|
||||
```
|
||||
|
||||
## Core concepts
|
||||
|
||||
- **Chat** — main entry point, coordinates adapters and routes events
|
||||
- **Adapters** — platform-specific (Slack, Teams, GChat, Discord, GitHub, Linear)
|
||||
- **State** — pluggable persistence (Redis for prod, memory for dev)
|
||||
- **Thread** — conversation thread with `post()`, `subscribe()`, `startTyping()`
|
||||
- **Message** — normalized format with `text`, `formatted` (mdast AST), `raw`
|
||||
- **Channel** — container for threads, supports listing and posting
|
||||
|
||||
## Event handlers
|
||||
|
||||
| Handler | Trigger |
|
||||
| -------------------------- | ------------------------------------------------- |
|
||||
| `onNewMention` | Bot @-mentioned in unsubscribed thread |
|
||||
| `onSubscribedMessage` | Any message in subscribed thread |
|
||||
| `onNewMessage(regex)` | Messages matching pattern in unsubscribed threads |
|
||||
| `onSlashCommand("/cmd")` | Slash command invocations |
|
||||
| `onReaction(emojis)` | Emoji reactions added/removed |
|
||||
| `onAction(actionId)` | Button clicks and dropdown selections |
|
||||
| `onAssistantThreadStarted` | Slack Assistants API thread opened |
|
||||
| `onAppHomeOpened` | Slack App Home tab opened |
|
||||
|
||||
## Streaming
|
||||
|
||||
Pass any `AsyncIterable<string>` to `thread.post()`. Works with AI SDK's `textStream`:
|
||||
|
||||
```typescript
|
||||
import { ToolLoopAgent } from 'ai';
|
||||
const agent = new ToolLoopAgent({ model: 'anthropic/claude-4.5-sonnet' });
|
||||
|
||||
bot.onNewMention(async (thread, message) => {
|
||||
const result = await agent.stream({ prompt: message.text });
|
||||
await thread.post(result.textStream);
|
||||
});
|
||||
```
|
||||
|
||||
## Cards (JSX)
|
||||
|
||||
Set `jsxImportSource: "chat"` in tsconfig. Components: `Card`, `CardText`, `Button`, `Actions`, `Fields`, `Field`, `Select`, `SelectOption`, `Image`, `Divider`, `LinkButton`, `Section`, `RadioSelect`.
|
||||
|
||||
```tsx
|
||||
await thread.post(
|
||||
<Card title="Order #1234">
|
||||
<CardText>Your order has been received!</CardText>
|
||||
<Actions>
|
||||
<Button id="approve" style="primary">
|
||||
Approve
|
||||
</Button>
|
||||
<Button id="reject" style="danger">
|
||||
Reject
|
||||
</Button>
|
||||
</Actions>
|
||||
</Card>,
|
||||
);
|
||||
```
|
||||
|
||||
## Packages
|
||||
|
||||
| Package | Purpose |
|
||||
| ----------------------------- | ----------------------------- |
|
||||
| `chat` | Core SDK |
|
||||
| `@chat-adapter/slack` | Slack |
|
||||
| `@chat-adapter/teams` | Microsoft Teams |
|
||||
| `@chat-adapter/gchat` | Google Chat |
|
||||
| `@chat-adapter/discord` | Discord |
|
||||
| `@chat-adapter/github` | GitHub Issues |
|
||||
| `@chat-adapter/linear` | Linear Issues |
|
||||
| `@chat-adapter/state-redis` | Redis state (production) |
|
||||
| `@chat-adapter/state-ioredis` | ioredis state (alternative) |
|
||||
| `@chat-adapter/state-memory` | In-memory state (development) |
|
||||
|
||||
## Changesets (Release Flow)
|
||||
|
||||
This monorepo uses [Changesets](https://github.com/changesets/changesets) for versioning and changelogs. Every PR that changes a package's behavior must include a changeset.
|
||||
|
||||
```bash
|
||||
pnpm changeset
|
||||
# → select affected package(s) (e.g. @chat-adapter/slack, chat)
|
||||
# → choose bump type: patch (fixes), minor (features), major (breaking)
|
||||
# → write a short summary for the CHANGELOG
|
||||
```
|
||||
|
||||
This creates a file in `.changeset/` — commit it with the PR. When merged to `main`, the Changesets GitHub Action opens a "Version Packages" PR to bump versions and update CHANGELOGs. Merging that PR publishes to npm.
|
||||
|
||||
## Webhook setup
|
||||
|
||||
Each adapter exposes a webhook handler via `bot.webhooks.{platform}`. Wire these to your HTTP framework's routes (e.g. Next.js API routes, Hono, Express).
|
||||
@@ -1,218 +0,0 @@
|
||||
---
|
||||
name: cli-backend-testing
|
||||
description: >
|
||||
CLI + Backend integration testing workflow. Use when verifying backend API changes
|
||||
(TRPC routers, services, models) via the LobeHub CLI against a local dev server.
|
||||
Triggers on 'cli test', 'test with cli', 'verify with cli', 'local cli test',
|
||||
'backend test with cli', or when needing to validate server-side changes end-to-end.
|
||||
---
|
||||
|
||||
# CLI + Backend Integration Testing
|
||||
|
||||
Standard workflow for verifying backend changes using the LobeHub CLI (`lh`) against a local dev server.
|
||||
|
||||
## When to Use
|
||||
|
||||
- Verifying TRPC router / service / model changes end-to-end
|
||||
- Testing new API fields or response structure changes
|
||||
- Validating CLI command output after backend modifications
|
||||
- Debugging data flow issues between server and CLI
|
||||
|
||||
## Prerequisites
|
||||
|
||||
| Requirement | Details |
|
||||
| ------------ | ------------------------------------------------------------- |
|
||||
| Dev server | `localhost:3011` (Next.js) |
|
||||
| CLI source | `lobehub/apps/cli/` |
|
||||
| CLI dev mode | Uses `LOBEHUB_CLI_HOME=.lobehub-dev` for isolated credentials |
|
||||
| Auth | Device Code Flow login to local server |
|
||||
|
||||
## Quick Reference
|
||||
|
||||
All CLI dev commands run from `lobehub/apps/cli/`:
|
||||
|
||||
```bash
|
||||
# Shorthand for all commands below
|
||||
CLI="LOBEHUB_CLI_HOME=.lobehub-dev bun src/index.ts"
|
||||
```
|
||||
|
||||
## Workflow
|
||||
|
||||
### Step 1: Ensure Dev Server is Running
|
||||
|
||||
Check if the dev server is already running:
|
||||
|
||||
```bash
|
||||
curl -s -o /dev/null -w '%{http_code}' http://localhost:3011/ 2> /dev/null
|
||||
```
|
||||
|
||||
- **If reachable** (returns any HTTP status): server is running. Skip to Step 2.
|
||||
- **If unreachable**: start the server:
|
||||
|
||||
```bash
|
||||
# From cloud repo root
|
||||
pnpm run dev:next
|
||||
```
|
||||
|
||||
To **restart** (pick up server-side code changes):
|
||||
|
||||
```bash
|
||||
lsof -ti:3011 | xargs kill
|
||||
pnpm run dev:next
|
||||
```
|
||||
|
||||
**Important:** Server-side code changes in the submodule (`lobehub/src/server/`, `lobehub/packages/`) require a server restart. Next.js hot-reload may not pick up changes in submodule packages.
|
||||
|
||||
### Step 2: Check CLI Authentication
|
||||
|
||||
Check if dev credentials already exist:
|
||||
|
||||
```bash
|
||||
cat lobehub/apps/cli/.lobehub-dev/settings.json 2> /dev/null
|
||||
```
|
||||
|
||||
- **If file exists and contains `"serverUrl": "http://localhost:3011"`**: already authenticated. Skip to Step 3.
|
||||
- **If file missing or points to wrong server**: login is needed. Ask the user to run:
|
||||
|
||||
```bash
|
||||
! cd lobehub/apps/cli && LOBEHUB_CLI_HOME=.lobehub-dev bun src/index.ts login --server http://localhost:3011
|
||||
```
|
||||
|
||||
> Login requires interactive browser authorization (OIDC Device Code Flow), so the user must run it themselves via `!` prefix. After login, credentials are saved to `lobehub/apps/cli/.lobehub-dev/` and persist across sessions.
|
||||
|
||||
### Step 3: Test with CLI Commands
|
||||
|
||||
CLI runs from source (`bun src/index.ts`), so CLI-side code changes take effect immediately without rebuilding.
|
||||
|
||||
```bash
|
||||
cd lobehub/apps/cli
|
||||
LOBEHUB_CLI_HOME=.lobehub-dev bun src/index.ts <command>
|
||||
```
|
||||
|
||||
### Step 4: Clean Up Test Data
|
||||
|
||||
Delete any test data created during verification:
|
||||
|
||||
```bash
|
||||
LOBEHUB_CLI_HOME=.lobehub-dev bun src/index.ts task delete < id > -y
|
||||
LOBEHUB_CLI_HOME=.lobehub-dev bun src/index.ts agent delete < id > -y
|
||||
```
|
||||
|
||||
## Common Testing Patterns
|
||||
|
||||
### Task System
|
||||
|
||||
```bash
|
||||
# List tasks
|
||||
$CLI task list
|
||||
|
||||
# Create test data with nesting
|
||||
$CLI task create -n "Root Task" -i "Test instruction"
|
||||
$CLI task create -n "Child Task" -i "Sub instruction" --parent T-1
|
||||
|
||||
# View task detail (tests getTaskDetail service)
|
||||
$CLI task view T-1
|
||||
|
||||
# View task tree
|
||||
$CLI task tree T-1
|
||||
|
||||
# Test lifecycle
|
||||
$CLI task edit T-1 --status running
|
||||
$CLI task comment T-1 -m "Test comment"
|
||||
|
||||
# Clean up
|
||||
$CLI task delete T-1 -y
|
||||
```
|
||||
|
||||
### Agent System
|
||||
|
||||
```bash
|
||||
# List agents
|
||||
$CLI agent list
|
||||
|
||||
# View agent detail
|
||||
$CLI agent view <agent-id>
|
||||
|
||||
# Run agent (tests agent execution pipeline)
|
||||
$CLI agent run <agent-id> -m "Test prompt"
|
||||
```
|
||||
|
||||
### Document & Knowledge Base
|
||||
|
||||
```bash
|
||||
# List documents
|
||||
$CLI doc list
|
||||
|
||||
# Create and view
|
||||
$CLI doc create -t "Test Doc" -c "Content here"
|
||||
$CLI doc view <doc-id>
|
||||
|
||||
# Knowledge base
|
||||
$CLI kb list
|
||||
$CLI kb tree <kb-id>
|
||||
```
|
||||
|
||||
### Model & Provider
|
||||
|
||||
```bash
|
||||
# List models and providers
|
||||
$CLI model list
|
||||
$CLI provider list
|
||||
|
||||
# Test provider connectivity
|
||||
$CLI provider test <provider-id>
|
||||
```
|
||||
|
||||
## Dev-Test Cycle
|
||||
|
||||
The standard cycle for backend development:
|
||||
|
||||
```
|
||||
1. Make code changes (service/model/router/type)
|
||||
|
|
||||
2. Run unit tests (fast feedback)
|
||||
bunx vitest run --silent='passed-only' '<test-file>'
|
||||
|
|
||||
3. Restart dev server (if server-side changes)
|
||||
lsof -ti:3011 | xargs kill && pnpm run dev:next
|
||||
|
|
||||
4. CLI verification (end-to-end)
|
||||
LOBEHUB_CLI_HOME=.lobehub-dev bun src/index.ts <command>
|
||||
|
|
||||
5. Clean up test data
|
||||
```
|
||||
|
||||
### When Server Restart is Needed
|
||||
|
||||
| Change Location | Restart? |
|
||||
| ----------------------------------------- | -------- |
|
||||
| `lobehub/src/server/` (routers, services) | Yes |
|
||||
| `lobehub/packages/database/` (models) | Yes |
|
||||
| `lobehub/packages/types/` | Yes |
|
||||
| `lobehub/packages/prompts/` | Yes |
|
||||
| `lobehub/apps/cli/` (CLI code) | No |
|
||||
| `src/` (cloud overrides) | Yes |
|
||||
|
||||
### When Server Restart is NOT Needed
|
||||
|
||||
CLI runs from source via `bun src/index.ts`, so any changes to `lobehub/apps/cli/src/` take effect immediately on next command invocation.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
| Issue | Solution |
|
||||
| --------------------------- | --------------------------------------------------------------------- |
|
||||
| `No authentication found` | Run `login --server http://localhost:3011` |
|
||||
| `UNAUTHORIZED` on API calls | Token expired; re-run login |
|
||||
| `ECONNREFUSED` | Dev server not running; start with `pnpm run dev:next` |
|
||||
| CLI shows old data/behavior | Server needs restart to pick up code changes |
|
||||
| `EADDRINUSE` on port 3011 | Server already running; kill with `lsof -ti:3011 \| xargs kill` |
|
||||
| Login opens wrong server | Must use `--server http://localhost:3011` flag (env var doesn't work) |
|
||||
|
||||
## Credential Isolation
|
||||
|
||||
| Mode | Credential Dir | Server |
|
||||
| ---------- | -------------------------------- | ----------------- |
|
||||
| Dev | `lobehub/apps/cli/.lobehub-dev/` | `localhost:3011` |
|
||||
| Production | `~/.lobehub/` | `app.lobehub.com` |
|
||||
|
||||
The two environments are completely isolated. Dev mode credentials are gitignored.
|
||||
@@ -1,296 +0,0 @@
|
||||
---
|
||||
name: cli
|
||||
description: LobeHub CLI (@lobehub/cli) development guide. Use when working on CLI commands, adding new subcommands, fixing CLI bugs, or understanding CLI architecture. Triggers on CLI development, command implementation, or `lh` command questions.
|
||||
disable-model-invocation: true
|
||||
---
|
||||
|
||||
# LobeHub CLI Development Guide
|
||||
|
||||
## Overview
|
||||
|
||||
LobeHub CLI (`@lobehub/cli`) is a command-line tool for managing and interacting with LobeHub services. Built with Commander.js + TypeScript.
|
||||
|
||||
- **Package**: `apps/cli/`
|
||||
- **Entry**: `apps/cli/src/index.ts`
|
||||
- **Binaries**: `lh`, `lobe`, `lobehub` (all aliases for the same CLI)
|
||||
- **Build**: tsup
|
||||
- **Runtime**: Node.js / Bun
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
apps/cli/src/
|
||||
├── index.ts # Entry point, registers all commands
|
||||
├── api/
|
||||
│ ├── client.ts # tRPC client (type-safe backend API)
|
||||
│ └── http.ts # Raw HTTP utilities
|
||||
├── auth/
|
||||
│ ├── credentials.ts # Encrypted credential storage (AES-256-GCM)
|
||||
│ ├── refresh.ts # Token auto-refresh
|
||||
│ └── resolveToken.ts # Token resolution (flag > stored)
|
||||
├── commands/ # All CLI commands (one file per command group)
|
||||
│ ├── agent.ts # Agent CRUD + run
|
||||
│ ├── config.ts # whoami, usage
|
||||
│ ├── connect.ts # Device gateway connection + daemon
|
||||
│ ├── doc.ts # Document management
|
||||
│ ├── file.ts # File management
|
||||
│ ├── generate/ # Content generation (text/image/video/tts/asr)
|
||||
│ ├── kb.ts # Knowledge base management
|
||||
│ ├── login.ts # OIDC Device Code Flow auth
|
||||
│ ├── logout.ts # Clear credentials
|
||||
│ ├── memory.ts # User memory management
|
||||
│ ├── message.ts # Message management
|
||||
│ ├── model.ts # AI model management
|
||||
│ ├── plugin.ts # Plugin management
|
||||
│ ├── provider.ts # AI provider management
|
||||
│ ├── search.ts # Global search
|
||||
│ ├── skill.ts # Agent skill management
|
||||
│ ├── status.ts # Gateway connectivity check
|
||||
│ └── topic.ts # Conversation topic management
|
||||
├── daemon/
|
||||
│ └── manager.ts # Background daemon process management
|
||||
├── tools/
|
||||
│ ├── shell.ts # Shell command execution (for gateway)
|
||||
│ └── file.ts # File operations (for gateway)
|
||||
├── settings/
|
||||
│ └── index.ts # Persistent settings (~/.lobehub/)
|
||||
├── utils/
|
||||
│ ├── logger.ts # Logging (verbose mode)
|
||||
│ ├── format.ts # Table output, JSON, timeAgo, truncate
|
||||
│ └── agentStream.ts # SSE streaming for agent runs
|
||||
└── constants/
|
||||
└── urls.ts # Official server & gateway URLs
|
||||
```
|
||||
|
||||
## Command Groups
|
||||
|
||||
| Command | Alias | Description |
|
||||
| ------------- | ----- | ----------------------------------------------------------- |
|
||||
| `lh login` | - | Authenticate via OIDC Device Code Flow |
|
||||
| `lh logout` | - | Clear stored credentials |
|
||||
| `lh connect` | - | Device gateway connection & daemon management |
|
||||
| `lh status` | - | Quick gateway connectivity check |
|
||||
| `lh agent` | - | Agent CRUD, run, status |
|
||||
| `lh generate` | `gen` | Content generation (text, image, video, tts, asr, download) |
|
||||
| `lh doc` | - | Document CRUD, batch-create, parse, topic linking |
|
||||
| `lh file` | - | File list, view, delete, recent |
|
||||
| `lh kb` | - | Knowledge base CRUD, folders, docs, upload, tree view |
|
||||
| `lh memory` | - | User memory CRUD + extraction |
|
||||
| `lh message` | - | Message list, search, delete, count, heatmap |
|
||||
| `lh topic` | - | Topic CRUD + search + recent |
|
||||
| `lh skill` | - | Skill CRUD + import (GitHub/URL/market) |
|
||||
| `lh model` | - | Model CRUD, toggle, batch-toggle, clear |
|
||||
| `lh provider` | - | Provider CRUD, config, test, toggle |
|
||||
| `lh plugin` | - | Plugin install, uninstall, update |
|
||||
| `lh search` | - | Global search across all types |
|
||||
| `lh whoami` | - | Current user info |
|
||||
| `lh usage` | - | Monthly/daily usage statistics |
|
||||
|
||||
## Adding a New Command
|
||||
|
||||
### 1. Create Command File
|
||||
|
||||
Create `apps/cli/src/commands/<name>.ts`:
|
||||
|
||||
```typescript
|
||||
import type { Command } from 'commander';
|
||||
import { getTrpcClient } from '../api/client';
|
||||
import { outputJson, printTable, truncate } from '../utils/format';
|
||||
|
||||
export function register<Name>Command(program: Command) {
|
||||
const cmd = program.command('<name>').description('...');
|
||||
|
||||
// Subcommands
|
||||
cmd
|
||||
.command('list')
|
||||
.description('List items')
|
||||
.option('-L, --limit <n>', 'Maximum number of items', '30')
|
||||
.option('--json [fields]', 'Output JSON, optionally specify fields')
|
||||
.action(async (options) => {
|
||||
const client = await getTrpcClient();
|
||||
const result = await client.<router>.<procedure>.query({ ... });
|
||||
// Handle output
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Register in Entry Point
|
||||
|
||||
In `apps/cli/src/index.ts`:
|
||||
|
||||
```typescript
|
||||
import { registerNewCommand } from './commands/new';
|
||||
// ...
|
||||
registerNewCommand(program);
|
||||
```
|
||||
|
||||
### 3. Add Tests
|
||||
|
||||
Create `apps/cli/src/commands/<name>.test.ts` alongside the command file.
|
||||
|
||||
## Conventions
|
||||
|
||||
### Output Patterns
|
||||
|
||||
All list/view commands follow consistent patterns:
|
||||
|
||||
- `--json [fields]` - JSON output with optional field filtering
|
||||
- `--yes` - Skip confirmation for destructive ops
|
||||
- `-L, --limit <n>` - Pagination limit (default: 30)
|
||||
- `-v, --verbose` - Verbose logging
|
||||
|
||||
### Table Output
|
||||
|
||||
```typescript
|
||||
const rows = items.map((item) => [item.id, truncate(item.title, 40), timeAgo(item.updatedAt)]);
|
||||
printTable(rows, ['ID', 'TITLE', 'UPDATED']);
|
||||
```
|
||||
|
||||
### JSON Output
|
||||
|
||||
```typescript
|
||||
if (options.json !== undefined) {
|
||||
const fields = typeof options.json === 'string' ? options.json : undefined;
|
||||
outputJson(items, fields);
|
||||
return;
|
||||
}
|
||||
```
|
||||
|
||||
### Authentication
|
||||
|
||||
Commands that need auth use `getTrpcClient()` which auto-resolves tokens:
|
||||
|
||||
```typescript
|
||||
const client = await getTrpcClient();
|
||||
// client.router.procedure.query/mutate(...)
|
||||
```
|
||||
|
||||
### Confirmation Prompts
|
||||
|
||||
```typescript
|
||||
import { confirm } from '../utils/format';
|
||||
if (!options.yes) {
|
||||
const ok = await confirm('Are you sure?');
|
||||
if (!ok) return;
|
||||
}
|
||||
```
|
||||
|
||||
## Storage Locations
|
||||
|
||||
| File | Path | Purpose |
|
||||
| ------------- | ----------------------------- | ------------------------------ |
|
||||
| Credentials | `~/.lobehub/credentials.json` | Encrypted tokens (AES-256-GCM) |
|
||||
| Settings | `~/.lobehub/settings.json` | Custom server/gateway URLs |
|
||||
| Daemon PID | `~/.lobehub/daemon.pid` | Background process PID |
|
||||
| Daemon Status | `~/.lobehub/daemon.status` | Connection status JSON |
|
||||
| Daemon Log | `~/.lobehub/daemon.log` | Daemon output log |
|
||||
|
||||
The base directory (`~/.lobehub/`) can be overridden with the `LOBEHUB_CLI_HOME` env var (e.g. `LOBEHUB_CLI_HOME=.lobehub-dev` for dev mode isolation).
|
||||
|
||||
## Key Dependencies
|
||||
|
||||
- `commander` - CLI framework
|
||||
- `@trpc/client` + `superjson` - Type-safe API client
|
||||
- `@lobechat/device-gateway-client` - WebSocket gateway connection
|
||||
- `@lobechat/local-file-shell` - Local shell/file tool execution
|
||||
- `picocolors` - Terminal colors
|
||||
- `ws` - WebSocket
|
||||
- `diff` - Text diffing
|
||||
- `fast-glob` - File pattern matching
|
||||
|
||||
## Development
|
||||
|
||||
### Running in Dev Mode
|
||||
|
||||
Dev mode uses `LOBEHUB_CLI_HOME=.lobehub-dev` to isolate credentials from the global `~/.lobehub/` directory, so dev and production configs never conflict.
|
||||
|
||||
```bash
|
||||
# Run a command in dev mode (from apps/cli/)
|
||||
cd apps/cli && bun run dev -- <command>
|
||||
|
||||
# This is equivalent to:
|
||||
LOBEHUB_CLI_HOME=.lobehub-dev bun src/index.ts <command>
|
||||
```
|
||||
|
||||
### Connecting to Local Dev Server
|
||||
|
||||
To test CLI against a local dev server (e.g. `localhost:3011`):
|
||||
|
||||
**Step 1: Start the local server**
|
||||
|
||||
```bash
|
||||
# From cloud repo root
|
||||
bun run dev
|
||||
# Server starts on http://localhost:3011 (or configured port)
|
||||
```
|
||||
|
||||
**Step 2: Login to local server via Device Code Flow**
|
||||
|
||||
```bash
|
||||
cd apps/cli && bun run dev -- login --server http://localhost:3011
|
||||
```
|
||||
|
||||
This will:
|
||||
|
||||
1. Call `POST http://localhost:3011/oidc/device/auth` to get a device code
|
||||
2. Print a URL like `http://localhost:3011/oidc/device?user_code=XXXX-YYYY`
|
||||
3. Open the URL in your browser — log in and authorize
|
||||
4. Save credentials to `apps/cli/.lobehub-dev/credentials.json`
|
||||
5. Save server URL to `apps/cli/.lobehub-dev/settings.json`
|
||||
|
||||
After login, all subsequent `bun run dev -- <command>` calls will use the local server.
|
||||
|
||||
**Step 3: Run commands against local server**
|
||||
|
||||
```bash
|
||||
cd apps/cli && bun run dev -- task list
|
||||
cd apps/cli && bun run dev -- task create -i "Test task" -n "My Task"
|
||||
cd apps/cli && bun run dev -- agent list
|
||||
```
|
||||
|
||||
**Troubleshooting:**
|
||||
|
||||
- If login returns `invalid_grant`, make sure the local OIDC provider is properly configured (check `OIDC_*` env vars in `.env`)
|
||||
- If you get `UNAUTHORIZED` on API calls, your token may have expired — run `bun run dev -- login --server http://localhost:3011` again
|
||||
- Dev credentials are stored in `apps/cli/.lobehub-dev/` (gitignored), not in `~/.lobehub/`
|
||||
|
||||
### Switching Between Local and Production
|
||||
|
||||
```bash
|
||||
# Dev mode (local server) — uses .lobehub-dev/
|
||||
cd apps/cli && bun run dev -- <command>
|
||||
|
||||
# Production (app.lobehub.com) — uses ~/.lobehub/
|
||||
lh <command>
|
||||
```
|
||||
|
||||
The two environments are completely isolated by different credential directories.
|
||||
|
||||
### Build & Test
|
||||
|
||||
```bash
|
||||
# Build CLI
|
||||
cd apps/cli && bun run build
|
||||
|
||||
# Unit tests
|
||||
cd apps/cli && bun run test
|
||||
|
||||
# E2E tests (requires authenticated CLI)
|
||||
cd apps/cli && bunx vitest run e2e/kb.e2e.test.ts
|
||||
|
||||
# Link globally for testing (installs lh/lobe/lobehub commands)
|
||||
cd apps/cli && bun run cli:link
|
||||
```
|
||||
|
||||
## Detailed Command References
|
||||
|
||||
See `references/` for each command group:
|
||||
|
||||
- **Agent**: `references/agent.md` (CRUD, run, status)
|
||||
- **Content Generation**: `references/generate.md` (text, image, video, tts, asr, download)
|
||||
- **Knowledge & Files**: `references/knowledge.md` (kb, file, doc)
|
||||
- **Conversation**: `references/conversation.md` (topic, message)
|
||||
- **Memory**: `references/memory.md` (memory management, extraction)
|
||||
- **Skills & Plugins**: `references/skills-plugins.md` (skill, plugin)
|
||||
- **Models & Providers**: `references/models-providers.md` (model, provider)
|
||||
- **Search & Config**: `references/search-config.md` (search, whoami, usage)
|
||||
@@ -1,144 +0,0 @@
|
||||
# Agent Commands
|
||||
|
||||
Manage AI agents: create, edit, delete, list, run, and check status.
|
||||
|
||||
**Source**: `apps/cli/src/commands/agent.ts`
|
||||
|
||||
## `lh agent list`
|
||||
|
||||
List all agents.
|
||||
|
||||
```bash
|
||||
lh agent list [-L [-k [--json [fields]] < n > ] < keyword > ]
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| ------------------------- | -------------------------------------- | ------- |
|
||||
| `-L, --limit <n>` | Maximum items | `30` |
|
||||
| `-k, --keyword <keyword>` | Filter by keyword | - |
|
||||
| `--json [fields]` | JSON output with optional field filter | - |
|
||||
|
||||
**Table columns**: ID, TITLE, DESCRIPTION, MODEL
|
||||
|
||||
---
|
||||
|
||||
## `lh agent view <agentId>`
|
||||
|
||||
View agent configuration details.
|
||||
|
||||
```bash
|
||||
lh agent view [fields]] < agentId > [--json
|
||||
```
|
||||
|
||||
**Displays**: Title, description, model, provider, system role, plugins, tools.
|
||||
|
||||
---
|
||||
|
||||
## `lh agent create`
|
||||
|
||||
Create a new agent.
|
||||
|
||||
```bash
|
||||
lh agent create [options]
|
||||
```
|
||||
|
||||
| Option | Description | Required |
|
||||
| --------------------------- | -------------- | -------- |
|
||||
| `-t, --title <title>` | Agent title | No |
|
||||
| `-d, --description <desc>` | Description | No |
|
||||
| `-m, --model <model>` | Model ID | No |
|
||||
| `-p, --provider <provider>` | Provider ID | No |
|
||||
| `-s, --system-role <role>` | System prompt | No |
|
||||
| `--group <groupId>` | Agent group ID | No |
|
||||
|
||||
**Output**: Created agent ID and session ID.
|
||||
|
||||
---
|
||||
|
||||
## `lh agent edit <agentId>`
|
||||
|
||||
Update an existing agent. Same options as `create`, all optional. Only specified fields are updated.
|
||||
|
||||
```bash
|
||||
lh agent edit [-m [-s ... < agentId > [-t < title > ] < model > ] < role > ]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## `lh agent delete <agentId>`
|
||||
|
||||
Delete an agent.
|
||||
|
||||
```bash
|
||||
lh agent delete < agentId > [--yes]
|
||||
```
|
||||
|
||||
Requires confirmation unless `--yes` is provided.
|
||||
|
||||
---
|
||||
|
||||
## `lh agent duplicate <agentId>`
|
||||
|
||||
Duplicate an existing agent.
|
||||
|
||||
```bash
|
||||
lh agent duplicate < agentId > [-t < title > ]
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| --------------------- | ------------------------------------ |
|
||||
| `-t, --title <title>` | Optional new title for the duplicate |
|
||||
|
||||
**Output**: New agent ID.
|
||||
|
||||
---
|
||||
|
||||
## `lh agent run`
|
||||
|
||||
Start an agent execution (streaming SSE).
|
||||
|
||||
```bash
|
||||
lh agent run [options]
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| --------------------- | -------------------------------------------- |
|
||||
| `-a, --agent-id <id>` | Agent ID to run |
|
||||
| `-s, --slug <slug>` | Agent slug (alternative to ID) |
|
||||
| `-p, --prompt <text>` | User prompt |
|
||||
| `-t, --topic-id <id>` | Reuse existing topic |
|
||||
| `--no-auto-start` | Don't auto-start the agent |
|
||||
| `--json` | Output full JSON event stream |
|
||||
| `-v, --verbose` | Show detailed tool call info |
|
||||
| `--replay <file>` | Replay events from saved JSON file (offline) |
|
||||
|
||||
### Streaming Behavior
|
||||
|
||||
Uses `utils/agentStream.ts` to handle Server-Sent Events:
|
||||
|
||||
1. Sends agent run request to backend
|
||||
2. Streams SSE events in real-time
|
||||
3. Displays: text chunks, tool call status, operation progress
|
||||
4. Shows final token usage and cost summary
|
||||
|
||||
### Replay Mode
|
||||
|
||||
`--replay <file>` reads a saved JSON event stream for offline debugging without server connection.
|
||||
|
||||
---
|
||||
|
||||
## `lh agent status <operationId>`
|
||||
|
||||
Check agent operation status.
|
||||
|
||||
```bash
|
||||
lh agent status [fields]] [--history] [--history-limit < operationId > [--json < n > ]
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| --------------------- | -------------------- | ------- |
|
||||
| `--json [fields]` | JSON output | - |
|
||||
| `--history` | Include step history | `false` |
|
||||
| `--history-limit <n>` | Max history entries | `10` |
|
||||
|
||||
**Displays**: Status (running/completed/failed), steps count, tokens used, cost, error info, timestamps.
|
||||
@@ -1,122 +0,0 @@
|
||||
# Conversation Commands (Topic & Message)
|
||||
|
||||
## Topic Management (`lh topic`)
|
||||
|
||||
Manage conversation topics (threads).
|
||||
|
||||
**Source**: `apps/cli/src/commands/topic.ts`
|
||||
|
||||
### `lh topic list`
|
||||
|
||||
```bash
|
||||
lh topic list [--agent-id [-L [--page [--json [fields]] < id > ] < n > ] < n > ]
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| ----------------- | --------------- | ------- |
|
||||
| `--agent-id <id>` | Filter by agent | - |
|
||||
| `-L, --limit <n>` | Page size | `30` |
|
||||
| `--page <n>` | Page number | `1` |
|
||||
|
||||
**Table columns**: ID, TITLE, FAV, UPDATED
|
||||
|
||||
### `lh topic search <keywords>`
|
||||
|
||||
```bash
|
||||
lh topic search [--json [fields]] < keywords > [--agent-id < id > ]
|
||||
```
|
||||
|
||||
### `lh topic create`
|
||||
|
||||
```bash
|
||||
lh topic create -t [--favorite] < title > [--agent-id < id > ]
|
||||
```
|
||||
|
||||
| Option | Description | Required |
|
||||
| --------------------- | -------------------- | -------- |
|
||||
| `-t, --title <title>` | Topic title | Yes |
|
||||
| `--agent-id <id>` | Associate with agent | No |
|
||||
| `--favorite` | Mark as favorite | No |
|
||||
|
||||
### `lh topic edit <id>`
|
||||
|
||||
```bash
|
||||
lh topic edit [--favorite] [--no-favorite] < id > [-t < title > ]
|
||||
```
|
||||
|
||||
### `lh topic delete <ids...>`
|
||||
|
||||
```bash
|
||||
lh topic delete [--yes] < id1 > [id2...]
|
||||
```
|
||||
|
||||
### `lh topic recent`
|
||||
|
||||
```bash
|
||||
lh topic recent [-L [--json [fields]] < n > ]
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| ----------------- | --------------- | ------- |
|
||||
| `-L, --limit <n>` | Number of items | `10` |
|
||||
|
||||
---
|
||||
|
||||
## Message Management (`lh message`)
|
||||
|
||||
Manage chat messages within topics.
|
||||
|
||||
**Source**: `apps/cli/src/commands/message.ts`
|
||||
|
||||
### `lh message list`
|
||||
|
||||
```bash
|
||||
lh message list [options] [--json [fields]]
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| ----------------- | ----------------------- | ------- |
|
||||
| `--topic-id <id>` | Filter by topic | - |
|
||||
| `--agent-id <id>` | Filter by agent | - |
|
||||
| `-L, --limit <n>` | Page size | `30` |
|
||||
| `--page <n>` | Page number | `1` |
|
||||
| `--user` | Only show user messages | - |
|
||||
|
||||
**Table columns**: ID, ROLE, CONTENT, CREATED
|
||||
|
||||
**Note**: When `--topic-id` or `--agent-id` is provided, uses `message.getMessages`; otherwise uses `message.listAll`.
|
||||
|
||||
### `lh message search <keywords>`
|
||||
|
||||
```bash
|
||||
lh message search [fields]] < keywords > [--json
|
||||
```
|
||||
|
||||
Full-text search across all messages.
|
||||
|
||||
### `lh message delete <ids...>`
|
||||
|
||||
```bash
|
||||
lh message delete [--yes] < id1 > [id2...]
|
||||
```
|
||||
|
||||
### `lh message count`
|
||||
|
||||
```bash
|
||||
lh message count [--start [--end [--json] < date > ] < date > ]
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| ---------------- | ------------------------------------------ |
|
||||
| `--start <date>` | Start date (ISO format, e.g. `2024-01-01`) |
|
||||
| `--end <date>` | End date (ISO format) |
|
||||
|
||||
**Output**: Total message count for the specified period.
|
||||
|
||||
### `lh message heatmap`
|
||||
|
||||
```bash
|
||||
lh message heatmap [--json]
|
||||
```
|
||||
|
||||
**Output**: Activity heatmap data showing message frequency over time.
|
||||
@@ -1,271 +0,0 @@
|
||||
# Content Generation Commands
|
||||
|
||||
Generate text, images, videos, speech, and transcriptions.
|
||||
|
||||
**Source**: `apps/cli/src/commands/generate/`
|
||||
|
||||
## Command Structure
|
||||
|
||||
```
|
||||
lh generate (alias: gen)
|
||||
├── text <prompt> # Text generation
|
||||
├── image <prompt> # Image generation
|
||||
├── video <prompt> # Video generation
|
||||
├── tts <text> # Text-to-speech
|
||||
├── asr <audioFile> # Audio-to-text (speech recognition)
|
||||
├── download <generationId> <asyncTaskId> # Wait & download generation result
|
||||
├── status <generationId> <asyncTaskId> # Check async task status
|
||||
└── list # List generation topics
|
||||
```
|
||||
|
||||
> ⚠️ **Important**: `status` and `download` require an `asyncTaskId` (UUID format, e.g.
|
||||
> `7ad0eb13-e9a5-4403-8070-1f7fe95b2f95`), **not** the generation ID (`gen_xxx`).
|
||||
> The asyncTaskId is printed after "→ Task" in the `video` / `image` command output.
|
||||
|
||||
---
|
||||
|
||||
## `lh generate text <prompt>` / `lh gen text <prompt>`
|
||||
|
||||
Generate text completion.
|
||||
|
||||
**Source**: `apps/cli/src/commands/generate/text.ts`
|
||||
|
||||
```bash
|
||||
lh gen text "Explain quantum computing" [options]
|
||||
echo "context" | lh gen text "summarize" --pipe
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| --------------------------- | ---------------------------------- | -------------------- |
|
||||
| `-m, --model <model>` | Model ID | `openai/gpt-4o-mini` |
|
||||
| `-p, --provider <provider>` | Provider name | - |
|
||||
| `-s, --system <prompt>` | System prompt | - |
|
||||
| `--temperature <n>` | Temperature (0-2) | - |
|
||||
| `--max-tokens <n>` | Maximum output tokens | - |
|
||||
| `--stream` | Enable streaming output | `false` |
|
||||
| `--json` | Output full JSON response | `false` |
|
||||
| `--pipe` | Read additional context from stdin | `false` |
|
||||
|
||||
### Pipe Mode
|
||||
|
||||
When `--pipe` is used, reads stdin and prepends it to the prompt. Useful for piping file contents:
|
||||
|
||||
```bash
|
||||
cat README.md | lh gen text "summarize this" --pipe
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## `lh generate image <prompt>` / `lh gen image <prompt>`
|
||||
|
||||
Generate images from text prompt. This is an async operation — the command submits the task and returns a generation ID + async task ID for tracking.
|
||||
|
||||
**Source**: `apps/cli/src/commands/generate/image.ts`
|
||||
|
||||
```bash
|
||||
lh gen image "A sunset over mountains" [options]
|
||||
lh gen image "A cute cat" --model dall-e-3 --provider openai --json
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| --------------------------- | ---------------- | ---------- |
|
||||
| `-m, --model <model>` | Model ID | `dall-e-3` |
|
||||
| `-p, --provider <provider>` | Provider name | `openai` |
|
||||
| `-n, --num <n>` | Number of images | `1` |
|
||||
| `--width <px>` | Width in pixels | - |
|
||||
| `--height <px>` | Height in pixels | - |
|
||||
| `--steps <n>` | Number of steps | - |
|
||||
| `--seed <n>` | Random seed | - |
|
||||
| `--json` | Output raw JSON | `false` |
|
||||
|
||||
**Output** (non-JSON):
|
||||
|
||||
```
|
||||
✓ Image generation started
|
||||
Batch ID: gb_xxx
|
||||
1 image(s) queued
|
||||
Generation gen_xxx → Task 7ad0eb13-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
This is the asyncTaskId — use this for status/download
|
||||
|
||||
Use "lh generate status <generationId> <asyncTaskId>" to check progress.
|
||||
```
|
||||
|
||||
**Typical workflow**:
|
||||
|
||||
```bash
|
||||
# 1. Submit generation — note down BOTH IDs from the output
|
||||
lh gen image "A cute cat"
|
||||
# Generation gen_abc123 → Task 7ad0eb13-e9a5-4403-8070-1f7fe95b2f95
|
||||
|
||||
# 2. Wait & download using generationId + asyncTaskId (the UUID)
|
||||
lh gen download gen_abc123 7ad0eb13-e9a5-4403-8070-1f7fe95b2f95 -o cat.png
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## `lh generate video <prompt>` / `lh gen video <prompt>`
|
||||
|
||||
Generate video from text prompt. This is an async operation.
|
||||
|
||||
**Source**: `apps/cli/src/commands/generate/video.ts`
|
||||
|
||||
```bash
|
||||
lh gen video "A cat playing piano" -m <model> -p <provider> [options]
|
||||
```
|
||||
|
||||
| Option | Description | Required |
|
||||
| --------------------------- | ------------------------ | -------- |
|
||||
| `-m, --model <model>` | Model ID | Yes |
|
||||
| `-p, --provider <provider>` | Provider name | Yes |
|
||||
| `--aspect-ratio <ratio>` | Aspect ratio (e.g. 16:9) | No |
|
||||
| `--duration <sec>` | Duration in seconds | No |
|
||||
| `--resolution <res>` | Resolution (e.g. 720p) | No |
|
||||
| `--seed <n>` | Random seed | No |
|
||||
| `--json` | Output raw JSON | No |
|
||||
|
||||
**Note**: Unlike image, video requires `-m` and `-p` (no defaults). Use `lh model list <provider> --type video` to find available video models.
|
||||
|
||||
**Output** (non-JSON):
|
||||
|
||||
```
|
||||
✓ Video generation started
|
||||
Batch ID: gb_xxx
|
||||
Generation gen_xxx → Task 7ad0eb13-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
This is the asyncTaskId — use this for status/download
|
||||
|
||||
Use "lh generate status <generationId> <asyncTaskId>" to check progress.
|
||||
```
|
||||
|
||||
**Typical workflow**:
|
||||
|
||||
```bash
|
||||
# 1. Find available video models for a provider
|
||||
lh model list volcengine --json | grep -i seedance
|
||||
|
||||
# 2. Submit generation — note down BOTH IDs from the output
|
||||
lh gen video "A cat on a runway" -m doubao-seedance-2-0-260128 -p volcengine \
|
||||
--aspect-ratio 9:16 --duration 5 --resolution 1080p
|
||||
# Generation gen_abc123 → Task 7ad0eb13-e9a5-4403-8070-1f7fe95b2f95
|
||||
|
||||
# 3. Wait & download using generationId + asyncTaskId (the UUID)
|
||||
lh gen download gen_abc123 7ad0eb13-e9a5-4403-8070-1f7fe95b2f95 -o result.mp4 --timeout 600
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## `lh generate tts <text>` / `lh gen tts <text>`
|
||||
|
||||
Text-to-speech generation.
|
||||
|
||||
**Source**: `apps/cli/src/commands/generate/tts.ts`
|
||||
|
||||
```bash
|
||||
lh gen tts "Hello, world!" [options]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## `lh generate asr <audioFile>` / `lh gen asr <audioFile>`
|
||||
|
||||
Audio-to-text transcription (Automatic Speech Recognition).
|
||||
|
||||
**Source**: `apps/cli/src/commands/generate/asr.ts`
|
||||
|
||||
```bash
|
||||
lh gen asr recording.wav [options]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## `lh generate download <generationId> <asyncTaskId>`
|
||||
|
||||
Wait for an async generation task to complete and download the result file.
|
||||
|
||||
**Source**: `apps/cli/src/commands/generate/index.ts`
|
||||
|
||||
> ⚠️ `<asyncTaskId>` is the UUID printed after "→ Task" in the video/image output.
|
||||
> Do **not** pass the generation ID (`gen_xxx`) here — that will cause a server error.
|
||||
|
||||
```bash
|
||||
lh gen download <generationId> <asyncTaskId> [-o output.png]
|
||||
lh gen download gen_xxx 7ad0eb13-xxxx-xxxx-xxxx-xxxxxxxxxxxx -o ~/Desktop/result.mp4 --timeout 600
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| --------------------- | ---------------------------------------- | ---------------------- |
|
||||
| `-o, --output <path>` | Output file path (auto-detect extension) | `<generationId>.<ext>` |
|
||||
| `--interval <sec>` | Polling interval in seconds | `5` |
|
||||
| `--timeout <sec>` | Timeout in seconds (0 = no timeout) | `300` |
|
||||
|
||||
**Behavior**:
|
||||
|
||||
1. Polls `generation.getGenerationStatus` at the specified interval
|
||||
2. Shows live progress: `⋯ Status: processing... (42s)`
|
||||
3. On success: downloads asset URL to local file
|
||||
4. On error / wrong ID: displays a clear message pointing to the correct ID format
|
||||
5. On timeout: suggests using `lh gen status` to check later
|
||||
|
||||
---
|
||||
|
||||
## `lh generate status <generationId> <asyncTaskId>`
|
||||
|
||||
Check the status of an async generation task.
|
||||
|
||||
> ⚠️ `<asyncTaskId>` is the UUID printed after "→ Task" in the video/image output.
|
||||
> Do **not** pass the generation ID (`gen_xxx`) here — that will cause a server error.
|
||||
|
||||
```bash
|
||||
lh gen status <generationId> <asyncTaskId> [--json]
|
||||
lh gen status gen_xxx 7ad0eb13-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| -------- | ------------------------ |
|
||||
| `--json` | Output raw JSON response |
|
||||
|
||||
**Displays**:
|
||||
|
||||
- Status (color-coded): `success` (green), `error` (red), `processing` (yellow), `pending` (cyan)
|
||||
- Error message (if failed)
|
||||
- Asset URL and thumbnail URL (if completed)
|
||||
|
||||
---
|
||||
|
||||
## `lh generate list`
|
||||
|
||||
List all generation topics.
|
||||
|
||||
```bash
|
||||
lh gen list [--json [fields]]
|
||||
```
|
||||
|
||||
**Table columns**: ID, TITLE, TYPE, UPDATED
|
||||
|
||||
---
|
||||
|
||||
## Backend Architecture
|
||||
|
||||
Image and video generation use an async task pattern:
|
||||
|
||||
1. **Create topic** → `generationTopic.createTopic`
|
||||
2. **Submit generation** → `image.createImage` / `video.createVideo`
|
||||
- Creates batch + generation + asyncTask records in a DB transaction
|
||||
- Triggers async background task (image via `createAsyncCaller`, video via `initModelRuntimeFromDB`)
|
||||
- Returns `{ data: { batch, generations }, success }` with `asyncTaskId` in each generation
|
||||
3. **Poll status** → `generation.getGenerationStatus`
|
||||
- Input: `{ generationId, asyncTaskId }` — both are required, and `asyncTaskId` must be the
|
||||
UUID from the `async_tasks` table, not `gen_xxx`
|
||||
- Returns `{ status, error, generation }` (generation includes asset URLs on success)
|
||||
- Before querying, calls `checkTimeoutTasks` which marks tasks as `error` if they have been
|
||||
`pending` or `processing` for more than ~5 minutes (`ASYNC_TASK_TIMEOUT = 298s`)
|
||||
|
||||
**Server routes**:
|
||||
|
||||
- `src/server/routers/lambda/image/index.ts` — image creation (uses `authedProcedure` + `serverDatabase`)
|
||||
- `src/server/routers/lambda/video/index.ts` — video creation (uses `authedProcedure` + `serverDatabase`)
|
||||
- `src/server/routers/lambda/generation.ts` — status checking
|
||||
- `packages/database/src/models/asyncTask.ts` — `AsyncTaskModel` including `checkTimeoutTasks`
|
||||
|
||||
**Note**: Image/video routes do NOT use the `keyVaults` middleware — they read API keys from the database via `initModelRuntimeFromDB` or `createAsyncCaller`.
|
||||
@@ -1,281 +0,0 @@
|
||||
# Knowledge Base, File & Document Commands
|
||||
|
||||
## Knowledge Base (`lh kb`)
|
||||
|
||||
Manage knowledge bases for RAG (Retrieval-Augmented Generation). Supports directory tree structure with folders, documents, and file uploads.
|
||||
|
||||
**Source**: `apps/cli/src/commands/kb.ts`
|
||||
|
||||
### `lh kb list`
|
||||
|
||||
```bash
|
||||
lh kb list [--json [fields]]
|
||||
```
|
||||
|
||||
**Table columns**: ID, NAME, DESCRIPTION, UPDATED
|
||||
|
||||
### `lh kb view <id>`
|
||||
|
||||
```bash
|
||||
lh kb view [fields]] < id > [--json
|
||||
```
|
||||
|
||||
**Displays**: Name, description, full directory tree with all files and documents (recursively fetched). Shows indented tree structure with item type (File/Doc), file type, and size.
|
||||
|
||||
**API**: Uses `file.getKnowledgeItems` to recursively fetch items. Folders (`custom/folder` fileType) are traversed in parallel via `Promise.all` for performance.
|
||||
|
||||
### `lh kb create`
|
||||
|
||||
```bash
|
||||
lh kb create -n [--avatar < name > [-d < desc > ] < url > ]
|
||||
```
|
||||
|
||||
| Option | Description | Required |
|
||||
| -------------------------- | ------------------- | -------- |
|
||||
| `-n, --name <name>` | Knowledge base name | Yes |
|
||||
| `-d, --description <desc>` | Description | No |
|
||||
| `--avatar <url>` | Avatar URL | No |
|
||||
|
||||
**Output**: Created KB ID. Note: backend returns ID as a string directly (not an object).
|
||||
|
||||
### `lh kb edit <id>`
|
||||
|
||||
```bash
|
||||
lh kb edit [-d [--avatar < id > [-n < name > ] < desc > ] < url > ]
|
||||
```
|
||||
|
||||
Requires at least one change flag. Errors if none specified.
|
||||
|
||||
### `lh kb delete <id>`
|
||||
|
||||
```bash
|
||||
lh kb delete [--yes] < id > [--remove-files]
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| ---------------- | ---------------------------- |
|
||||
| `--remove-files` | Also delete associated files |
|
||||
| `--yes` | Skip confirmation |
|
||||
|
||||
### `lh kb add-files <knowledgeBaseId>`
|
||||
|
||||
```bash
|
||||
lh kb add-files <kbId> --ids <fileId1> <fileId2> ...
|
||||
```
|
||||
|
||||
Link existing files to a knowledge base.
|
||||
|
||||
### `lh kb remove-files <knowledgeBaseId>`
|
||||
|
||||
```bash
|
||||
lh kb remove-files <kbId> --ids <fileId1> <fileId2> ... [--yes]
|
||||
```
|
||||
|
||||
Unlink files from a knowledge base.
|
||||
|
||||
### `lh kb mkdir <knowledgeBaseId>`
|
||||
|
||||
```bash
|
||||
lh kb mkdir < kbId > -n < name > [--parent < folderId > ]
|
||||
```
|
||||
|
||||
Create a folder in a knowledge base. Uses `document.createDocument` with `fileType: 'custom/folder'`.
|
||||
|
||||
| Option | Description | Required |
|
||||
| --------------------- | ---------------- | -------- |
|
||||
| `-n, --name <name>` | Folder name | Yes |
|
||||
| `--parent <parentId>` | Parent folder ID | No |
|
||||
|
||||
### `lh kb create-doc <knowledgeBaseId>`
|
||||
|
||||
```bash
|
||||
lh kb create-doc [--parent < kbId > -t < title > [-c < content > ] < folderId > ]
|
||||
```
|
||||
|
||||
Create a document in a knowledge base. Uses `document.createDocument` with `fileType: 'custom/document'`.
|
||||
|
||||
| Option | Description | Required |
|
||||
| ---------------------- | ---------------- | -------- |
|
||||
| `-t, --title <title>` | Document title | Yes |
|
||||
| `-c, --content <text>` | Document content | No |
|
||||
| `--parent <parentId>` | Parent folder ID | No |
|
||||
|
||||
### `lh kb move <id>`
|
||||
|
||||
```bash
|
||||
lh kb move < id > --type < file | doc > [--parent < folderId > ]
|
||||
```
|
||||
|
||||
Move a file or document to a different folder (or to root if `--parent` is omitted).
|
||||
|
||||
| Option | Description | Default |
|
||||
| --------------------- | -------------------------------- | ------- |
|
||||
| `--type <type>` | Item type: `file` or `doc` | `file` |
|
||||
| `--parent <parentId>` | Target folder ID (omit for root) | - |
|
||||
|
||||
Uses `document.updateDocument` for docs, `file.updateFile` for files.
|
||||
|
||||
### `lh kb upload <knowledgeBaseId> <filePath>`
|
||||
|
||||
```bash
|
||||
lh kb upload <kbId> <filePath> [--parent <folderId>]
|
||||
```
|
||||
|
||||
Upload a local file to a knowledge base via S3 presigned URL.
|
||||
|
||||
| Option | Description |
|
||||
| --------------------- | ---------------- |
|
||||
| `--parent <parentId>` | Parent folder ID |
|
||||
|
||||
**Flow**: Compute SHA-256 hash → get presigned URL via `upload.createS3PreSignedUrl` → PUT to S3 → create file record via `file.createFile`.
|
||||
|
||||
---
|
||||
|
||||
## File Management (`lh file`)
|
||||
|
||||
Manage uploaded files.
|
||||
|
||||
**Source**: `apps/cli/src/commands/file.ts`
|
||||
|
||||
### `lh file list`
|
||||
|
||||
```bash
|
||||
lh file list [--kb-id [-L [--json [fields]] < id > ] < n > ]
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| ----------------- | ------------------------ | ------- |
|
||||
| `--kb-id <id>` | Filter by knowledge base | - |
|
||||
| `-L, --limit <n>` | Maximum items | `30` |
|
||||
|
||||
**Table columns**: ID, NAME, TYPE, SIZE, UPDATED
|
||||
|
||||
### `lh file view <id>`
|
||||
|
||||
```bash
|
||||
lh file view [fields]] < id > [--json
|
||||
```
|
||||
|
||||
**Displays**: Name, type, size, chunking status, embedding status.
|
||||
|
||||
### `lh file delete <ids...>`
|
||||
|
||||
```bash
|
||||
lh file delete [--yes] < id1 > [id2...]
|
||||
```
|
||||
|
||||
Supports deleting multiple files at once.
|
||||
|
||||
### `lh file recent`
|
||||
|
||||
```bash
|
||||
lh file recent [-L [--json [fields]] < n > ]
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| ----------------- | --------------- | ------- |
|
||||
| `-L, --limit <n>` | Number of items | `10` |
|
||||
|
||||
---
|
||||
|
||||
## Document Management (`lh doc`)
|
||||
|
||||
Manage text documents (notes, wiki pages).
|
||||
|
||||
**Source**: `apps/cli/src/commands/doc.ts`
|
||||
|
||||
### `lh doc list`
|
||||
|
||||
```bash
|
||||
lh doc list [-L [--file-type [--source-type [--json [fields]] < n > ] < type > ] < type > ]
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| ---------------------- | --------------------------------------------- | ------- |
|
||||
| `-L, --limit <n>` | Maximum items | `30` |
|
||||
| `--file-type <type>` | Filter by file type | - |
|
||||
| `--source-type <type>` | Filter by source type (file, web, api, topic) | - |
|
||||
|
||||
**Table columns**: ID, TITLE, TYPE, UPDATED
|
||||
|
||||
### `lh doc view <id>`
|
||||
|
||||
```bash
|
||||
lh doc view [fields]] < id > [--json
|
||||
```
|
||||
|
||||
**Displays**: Title, type, KB association, updated time, full content.
|
||||
|
||||
### `lh doc create`
|
||||
|
||||
```bash
|
||||
lh doc create -t [-F [--parent [--slug [--kb [--file-type < title > [-b < body > ] < path > ] < id > ] < slug > ] < id > ] < type > ]
|
||||
```
|
||||
|
||||
| Option | Description | Required |
|
||||
| ------------------------ | ----------------------------------------------- | -------- |
|
||||
| `-t, --title <title>` | Document title | Yes |
|
||||
| `-b, --body <content>` | Document body text | No |
|
||||
| `-F, --body-file <path>` | Read body from file | No |
|
||||
| `--parent <id>` | Parent document ID | No |
|
||||
| `--slug <slug>` | Custom URL slug | No |
|
||||
| `--kb <id>` | Knowledge base ID to associate with | No |
|
||||
| `--file-type <type>` | File type (e.g. custom/document, custom/folder) | No |
|
||||
|
||||
`-b` and `-F` are mutually exclusive; `-F` reads the file content as the body.
|
||||
|
||||
### `lh doc batch-create <file>`
|
||||
|
||||
Batch create documents from a JSON file. The file must contain a non-empty array of document objects.
|
||||
|
||||
```bash
|
||||
lh doc batch-create documents.json
|
||||
```
|
||||
|
||||
Each object in the array can have: `title`, `content`, `fileType`, `knowledgeBaseId`, `parentId`, `slug`.
|
||||
|
||||
### `lh doc edit <id>`
|
||||
|
||||
```bash
|
||||
lh doc edit [-b [-F [--parent [--file-type < id > [-t < title > ] < body > ] < path > ] < id > ] < type > ]
|
||||
```
|
||||
|
||||
### `lh doc delete <ids...>`
|
||||
|
||||
```bash
|
||||
lh doc delete [--yes] < id1 > [id2...]
|
||||
```
|
||||
|
||||
### `lh doc parse <fileId>`
|
||||
|
||||
Parse an uploaded file into a document.
|
||||
|
||||
```bash
|
||||
lh doc parse [--json [fields]] < fileId > [--with-pages]
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| -------------- | ----------------------- |
|
||||
| `--with-pages` | Preserve page structure |
|
||||
|
||||
**Output**: Parsed title and content preview.
|
||||
|
||||
### `lh doc link-topic <docId> <topicId>`
|
||||
|
||||
Associate a document with a topic. Creates a linked copy via the notebook router.
|
||||
|
||||
```bash
|
||||
lh doc link-topic <docId> <topicId>
|
||||
```
|
||||
|
||||
### `lh doc topic-docs <topicId>`
|
||||
|
||||
List documents associated with a topic.
|
||||
|
||||
```bash
|
||||
lh doc topic-docs [--json [fields]] < topicId > [--type < type > ]
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| --------------- | ------------------------------------------------ |
|
||||
| `--type <type>` | Filter by type (article, markdown, note, report) |
|
||||
@@ -1,138 +0,0 @@
|
||||
# Memory Commands
|
||||
|
||||
Manage user memories - the AI's long-term knowledge about users.
|
||||
|
||||
**Source**: `apps/cli/src/commands/memory.ts`
|
||||
|
||||
## Memory Categories
|
||||
|
||||
| Category | Description |
|
||||
| ------------ | ----------------------------------------- |
|
||||
| `identity` | User's name, role, relationships |
|
||||
| `activity` | Recent activities and their status |
|
||||
| `context` | Ongoing contexts, projects, goals |
|
||||
| `experience` | Past experiences and key learnings |
|
||||
| `preference` | User preferences, directives, suggestions |
|
||||
|
||||
---
|
||||
|
||||
## `lh memory list [category]`
|
||||
|
||||
List memory entries, optionally filtered by category.
|
||||
|
||||
```bash
|
||||
lh memory list # All categories
|
||||
lh memory list identity # Only identity memories
|
||||
lh memory list preference # Only preferences
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| ----------------- | ----------- |
|
||||
| `--json [fields]` | JSON output |
|
||||
|
||||
**Output**: Grouped by category, showing type/status and descriptions.
|
||||
|
||||
---
|
||||
|
||||
## `lh memory create`
|
||||
|
||||
Create a new identity memory entry.
|
||||
|
||||
```bash
|
||||
lh memory create [options]
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| -------------------------- | ------------------------ |
|
||||
| `--type <type>` | Memory type |
|
||||
| `--role <role>` | User's role |
|
||||
| `--relationship <rel>` | Relationship description |
|
||||
| `-d, --description <desc>` | Description |
|
||||
| `--labels <labels...>` | Extracted labels |
|
||||
|
||||
---
|
||||
|
||||
## `lh memory edit <category> <id>`
|
||||
|
||||
Edit a memory entry. Options vary by category:
|
||||
|
||||
```bash
|
||||
lh memory edit identity < id > [options]
|
||||
lh memory edit activity < id > [options]
|
||||
lh memory edit context < id > [options]
|
||||
lh memory edit experience < id > [options]
|
||||
lh memory edit preference < id > [options]
|
||||
```
|
||||
|
||||
### Category-specific Options
|
||||
|
||||
**identity**:
|
||||
|
||||
- `--type <type>`, `--role <role>`, `--relationship <rel>`
|
||||
|
||||
**activity**:
|
||||
|
||||
- `--narrative <text>`, `--notes <text>`, `--status <status>`
|
||||
|
||||
**context**:
|
||||
|
||||
- `--title <title>`, `--description <desc>`, `--status <status>`
|
||||
|
||||
**experience**:
|
||||
|
||||
- `--situation <text>`, `--action <text>`, `--key-learning <text>`
|
||||
|
||||
**preference**:
|
||||
|
||||
- `--directives <text>`, `--suggestions <text>`
|
||||
|
||||
---
|
||||
|
||||
## `lh memory delete <category> <id>`
|
||||
|
||||
```bash
|
||||
lh memory delete identity < id > [--yes]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## `lh memory persona`
|
||||
|
||||
Display the compiled memory persona summary.
|
||||
|
||||
```bash
|
||||
lh memory persona [--json [fields]]
|
||||
```
|
||||
|
||||
**Output**: Summarized user profile built from all memory categories.
|
||||
|
||||
---
|
||||
|
||||
## `lh memory extract`
|
||||
|
||||
Trigger async memory extraction from chat history.
|
||||
|
||||
```bash
|
||||
lh memory extract [--from [--to < date > ] < date > ]
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| --------------- | ----------------------- |
|
||||
| `--from <date>` | Start date (ISO format) |
|
||||
| `--to <date>` | End date (ISO format) |
|
||||
|
||||
Starts a background task that analyzes chat history and creates new memory entries.
|
||||
|
||||
---
|
||||
|
||||
## `lh memory extract-status`
|
||||
|
||||
Check the status of a memory extraction task.
|
||||
|
||||
```bash
|
||||
lh memory extract-status [--task-id [--json [fields]] < id > ]
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| ---------------- | ------------------- |
|
||||
| `--task-id <id>` | Check specific task |
|
||||
@@ -1,186 +0,0 @@
|
||||
# Model & Provider Commands
|
||||
|
||||
## Model Management (`lh model`)
|
||||
|
||||
Manage AI models within providers.
|
||||
|
||||
**Source**: `apps/cli/src/commands/model.ts`
|
||||
|
||||
### `lh model list <providerId>`
|
||||
|
||||
List models for a specific provider.
|
||||
|
||||
```bash
|
||||
lh model list openai
|
||||
lh model list openai --type image --enabled
|
||||
lh model list lobehub --type video --json
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| ----------------- | -------------------------------------------------------------------------------------- | ------- |
|
||||
| `-L, --limit <n>` | Maximum items | `50` |
|
||||
| `--enabled` | Only show enabled models | `false` |
|
||||
| `--type <type>` | Filter by model type (`chat\|embedding\|tts\|stt\|image\|video\|text2music\|realtime`) | - |
|
||||
| `--json [fields]` | Output JSON, optionally specify fields | - |
|
||||
|
||||
**Table columns**: ID, NAME, ENABLED, TYPE
|
||||
|
||||
**Backend**: `aiModel.getAiProviderModelList` → `AiInfraRepos.getAiProviderModelList` (supports `type` filter at repository level)
|
||||
|
||||
### `lh model view <id>`
|
||||
|
||||
```bash
|
||||
lh model view [fields]] < modelId > [--json
|
||||
```
|
||||
|
||||
**Displays**: Name, provider, type, enabled status, capabilities.
|
||||
|
||||
### `lh model create`
|
||||
|
||||
```bash
|
||||
lh model create --id [--type < id > --provider < providerId > [--display-name < name > ] < type > ]
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| ------------------------- | ------------ | -------- |
|
||||
| `--id <id>` | Model ID | Required |
|
||||
| `--provider <providerId>` | Provider ID | Required |
|
||||
| `--display-name <name>` | Display name | - |
|
||||
| `--type <type>` | Model type | `chat` |
|
||||
|
||||
### `lh model edit <id>`
|
||||
|
||||
```bash
|
||||
lh model edit [--type < modelId > --provider < providerId > [--display-name < name > ] < type > ]
|
||||
```
|
||||
|
||||
### `lh model toggle <id>`
|
||||
|
||||
Enable or disable a model.
|
||||
|
||||
```bash
|
||||
lh model toggle < modelId > --provider < providerId > --enable
|
||||
lh model toggle < modelId > --provider < providerId > --disable
|
||||
```
|
||||
|
||||
| Option | Description | Required |
|
||||
| ------------------------- | ----------------- | ------------ |
|
||||
| `--provider <providerId>` | Provider ID | Yes |
|
||||
| `--enable` | Enable the model | One required |
|
||||
| `--disable` | Disable the model | One required |
|
||||
|
||||
### `lh model batch-toggle <ids...>`
|
||||
|
||||
Enable or disable multiple models at once.
|
||||
|
||||
```bash
|
||||
lh model batch-toggle model1 model2 model3 --provider openai --enable
|
||||
```
|
||||
|
||||
### `lh model delete <id>`
|
||||
|
||||
```bash
|
||||
lh model delete < modelId > --provider < providerId > [--yes]
|
||||
```
|
||||
|
||||
### `lh model clear`
|
||||
|
||||
Clear all models (or only remote/fetched models) for a provider.
|
||||
|
||||
```bash
|
||||
lh model clear --provider [--yes] < providerId > [--remote]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Provider Management (`lh provider`)
|
||||
|
||||
Manage AI service providers.
|
||||
|
||||
**Source**: `apps/cli/src/commands/provider.ts`
|
||||
|
||||
### `lh provider list`
|
||||
|
||||
```bash
|
||||
lh provider list [--json [fields]]
|
||||
```
|
||||
|
||||
**Table columns**: ID, NAME, ENABLED, SOURCE
|
||||
|
||||
### `lh provider view <id>`
|
||||
|
||||
```bash
|
||||
lh provider view [fields]] < providerId > [--json
|
||||
```
|
||||
|
||||
**Displays**: Name, enabled status, source, configuration.
|
||||
|
||||
### `lh provider create`
|
||||
|
||||
```bash
|
||||
lh provider create --id [-d [--logo [--sdk-type < id > -n < name > [-s < source > ] < desc > ] < url > ] < type > ]
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| -------------------------- | ------------------------------------------------- | -------- |
|
||||
| `--id <id>` | Provider ID | Required |
|
||||
| `-n, --name <name>` | Provider name | Required |
|
||||
| `-s, --source <source>` | Source type (`builtin` or `custom`) | `custom` |
|
||||
| `-d, --description <desc>` | Provider description | - |
|
||||
| `--logo <logo>` | Provider logo URL | - |
|
||||
| `--sdk-type <sdkType>` | SDK type (openai, anthropic, azure, bedrock, ...) | - |
|
||||
|
||||
### `lh provider edit <id>`
|
||||
|
||||
```bash
|
||||
lh provider edit [-d [--logo [--sdk-type < providerId > [-n < name > ] < desc > ] < url > ] < type > ]
|
||||
```
|
||||
|
||||
Requires at least one change flag.
|
||||
|
||||
### `lh provider config <id>`
|
||||
|
||||
Configure provider settings (API key, base URL, etc.).
|
||||
|
||||
```bash
|
||||
lh provider config openai --api-key sk-xxx
|
||||
lh provider config openai --base-url https://custom-endpoint.com
|
||||
lh provider config openai --show
|
||||
lh provider config openai --show --json
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| ------------------------ | --------------------------------- |
|
||||
| `--api-key <key>` | Set API key |
|
||||
| `--base-url <url>` | Set base URL |
|
||||
| `--check-model <model>` | Set connectivity check model |
|
||||
| `--enable-response-api` | Enable Response API mode (OpenAI) |
|
||||
| `--disable-response-api` | Disable Response API mode |
|
||||
| `--fetch-on-client` | Enable fetching models on client |
|
||||
| `--no-fetch-on-client` | Disable fetching models on client |
|
||||
| `--show` | Show current config |
|
||||
| `--json [fields]` | Output JSON (with --show) |
|
||||
|
||||
**Important**: The `lobehub` provider is platform-managed. Attempting to set `--api-key` or `--base-url` on it will be rejected with an error message.
|
||||
|
||||
### `lh provider test <id>`
|
||||
|
||||
Test provider connectivity.
|
||||
|
||||
```bash
|
||||
lh provider test openai
|
||||
lh provider test openai -m gpt-4o --json
|
||||
```
|
||||
|
||||
### `lh provider toggle <id>`
|
||||
|
||||
```bash
|
||||
lh provider toggle < providerId > --enable
|
||||
lh provider toggle < providerId > --disable
|
||||
```
|
||||
|
||||
### `lh provider delete <id>`
|
||||
|
||||
```bash
|
||||
lh provider delete < providerId > [--yes]
|
||||
```
|
||||
@@ -1,94 +0,0 @@
|
||||
# Search & Configuration Commands
|
||||
|
||||
## Global Search (`lh search`)
|
||||
|
||||
Search across all LobeHub resource types.
|
||||
|
||||
**Source**: `apps/cli/src/commands/search.ts`
|
||||
|
||||
### `lh search <query>`
|
||||
|
||||
```bash
|
||||
lh search "meeting notes" [-t [-L [--json [fields]] < type > ] < n > ]
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| ------------------- | ----------------------- | --------- |
|
||||
| `-t, --type <type>` | Filter by resource type | All types |
|
||||
| `-L, --limit <n>` | Results per type | `10` |
|
||||
|
||||
### Searchable Types
|
||||
|
||||
| Type | Description |
|
||||
| ---------------- | ---------------------------- |
|
||||
| `agent` | AI agents |
|
||||
| `topic` | Conversation topics |
|
||||
| `file` | Uploaded files |
|
||||
| `folder` | File folders |
|
||||
| `message` | Chat messages |
|
||||
| `page` | Documents/pages |
|
||||
| `memory` | User memories |
|
||||
| `mcp` | MCP servers |
|
||||
| `plugin` | Installed plugins |
|
||||
| `communityAgent` | Community marketplace agents |
|
||||
| `knowledgeBase` | Knowledge bases |
|
||||
|
||||
**Output**: Results grouped by type, showing ID, title/name, description.
|
||||
|
||||
---
|
||||
|
||||
## User Configuration (`lh whoami` / `lh usage`)
|
||||
|
||||
**Source**: `apps/cli/src/commands/config.ts`
|
||||
|
||||
### `lh whoami`
|
||||
|
||||
Display current authenticated user information.
|
||||
|
||||
```bash
|
||||
lh whoami [--json [fields]]
|
||||
```
|
||||
|
||||
**Displays**: Name, username, email, user ID, subscription plan.
|
||||
|
||||
### `lh usage`
|
||||
|
||||
Display usage statistics.
|
||||
|
||||
```bash
|
||||
lh usage [--month [--daily] [--json [fields]] < YYYY-MM > ]
|
||||
```
|
||||
|
||||
| Option | Description | Default |
|
||||
| ------------------- | -------------- | ----------------------- |
|
||||
| `--month <YYYY-MM>` | Month to query | Current month |
|
||||
| `--daily` | Group by day | `false` (monthly total) |
|
||||
|
||||
**Output**: Token usage, costs, and model breakdown for the specified period.
|
||||
|
||||
---
|
||||
|
||||
## Global Options
|
||||
|
||||
These options are available across most commands:
|
||||
|
||||
| Option | Description |
|
||||
| ----------------- | ---------------------------------------------------------------------- |
|
||||
| `--json [fields]` | Output as JSON; optionally filter to specific fields (comma-separated) |
|
||||
| `--yes` | Skip confirmation prompts for destructive operations |
|
||||
| `-L, --limit <n>` | Pagination limit for list commands |
|
||||
| `-v, --verbose` | Enable verbose/debug logging |
|
||||
| `--help` | Show command help |
|
||||
| `--version` | Show CLI version |
|
||||
|
||||
### JSON Field Filtering
|
||||
|
||||
The `--json` option supports field selection:
|
||||
|
||||
```bash
|
||||
# Full JSON output
|
||||
lh agent list --json
|
||||
|
||||
# Only specific fields
|
||||
lh agent list --json "id,title,model"
|
||||
```
|
||||
@@ -1,149 +0,0 @@
|
||||
# Skill & Plugin Commands
|
||||
|
||||
## Skill Management (`lh skill`)
|
||||
|
||||
Manage agent skills (custom instructions and capabilities).
|
||||
|
||||
**Source**: `apps/cli/src/commands/skill.ts`
|
||||
|
||||
### `lh skill list`
|
||||
|
||||
```bash
|
||||
lh skill list [--source [--json [fields]] < source > ]
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| ------------------- | ----------------------------------- |
|
||||
| `--source <source>` | Filter: `builtin`, `market`, `user` |
|
||||
|
||||
**Table columns**: ID, NAME, DESCRIPTION, SOURCE, IDENTIFIER
|
||||
|
||||
### `lh skill view <id>`
|
||||
|
||||
```bash
|
||||
lh skill view [fields]] < id > [--json
|
||||
```
|
||||
|
||||
**Displays**: Name, description, source, identifier, content.
|
||||
|
||||
### `lh skill create`
|
||||
|
||||
```bash
|
||||
lh skill create -n < name > -d < desc > -c < content > [-i < identifier > ]
|
||||
```
|
||||
|
||||
| Option | Description | Required |
|
||||
| -------------------------- | ----------------------------------- | -------- |
|
||||
| `-n, --name <name>` | Skill name | Yes |
|
||||
| `-d, --description <desc>` | Description | Yes |
|
||||
| `-c, --content <content>` | Skill content (prompt/instructions) | Yes |
|
||||
| `-i, --identifier <id>` | Custom identifier | No |
|
||||
|
||||
### `lh skill edit <id>`
|
||||
|
||||
```bash
|
||||
lh skill edit [-n [-d < id > [-c < content > ] < name > ] < desc > ]
|
||||
```
|
||||
|
||||
### `lh skill delete <id>`
|
||||
|
||||
```bash
|
||||
lh skill delete < id > [--yes]
|
||||
```
|
||||
|
||||
### `lh skill search <query>`
|
||||
|
||||
```bash
|
||||
lh skill search [fields]] < query > [--json
|
||||
```
|
||||
|
||||
### `lh skill install <source>` (alias: `lh skill i`)
|
||||
|
||||
Install a skill. Auto-detects source type from the input:
|
||||
|
||||
```bash
|
||||
# GitHub (URL or owner/repo shorthand)
|
||||
lh skill install lobehub/skill-repo
|
||||
lh skill install https://github.com/lobehub/skill-repo
|
||||
lh skill install lobehub/skill-repo --branch dev
|
||||
|
||||
# ZIP URL
|
||||
lh skill install https://example.com/skill.zip
|
||||
|
||||
# Marketplace identifier
|
||||
lh skill install my-cool-skill
|
||||
lh skill i my-cool-skill
|
||||
```
|
||||
|
||||
| Option | Description | Notes |
|
||||
| ------------------- | ------------------------- | -------- |
|
||||
| `--branch <branch>` | Branch name (GitHub only) | Optional |
|
||||
|
||||
**Detection rules**:
|
||||
|
||||
- `https://github.com/...` or `owner/repo` → GitHub
|
||||
- Other `https://...` URLs → ZIP URL
|
||||
- Everything else → marketplace identifier
|
||||
|
||||
### Resource Commands
|
||||
|
||||
#### `lh skill resources <id>`
|
||||
|
||||
List files/resources within a skill.
|
||||
|
||||
```bash
|
||||
lh skill resources [fields]] < id > [--json
|
||||
```
|
||||
|
||||
**Displays**: Path, type, size.
|
||||
|
||||
#### `lh skill read-resource <id> <path>`
|
||||
|
||||
Read a specific resource file from a skill.
|
||||
|
||||
```bash
|
||||
lh skill read-resource <skillId> <path>
|
||||
```
|
||||
|
||||
**Output**: File content or JSON metadata.
|
||||
|
||||
---
|
||||
|
||||
## Plugin Management (`lh plugin`)
|
||||
|
||||
Install and manage plugins (external tool integrations).
|
||||
|
||||
**Source**: `apps/cli/src/commands/plugin.ts`
|
||||
|
||||
### `lh plugin list`
|
||||
|
||||
```bash
|
||||
lh plugin list [--json [fields]]
|
||||
```
|
||||
|
||||
**Table columns**: ID, IDENTIFIER, TYPE, TITLE
|
||||
|
||||
### `lh plugin install`
|
||||
|
||||
```bash
|
||||
lh plugin install -i [--settings < identifier > --manifest < json > [--type < type > ] < json > ]
|
||||
```
|
||||
|
||||
| Option | Description | Required |
|
||||
| ----------------------- | -------------------------- | ---------------------- |
|
||||
| `-i, --identifier <id>` | Plugin identifier | Yes |
|
||||
| `--manifest <json>` | Plugin manifest JSON | Yes |
|
||||
| `--type <type>` | `plugin` or `customPlugin` | No (default: `plugin`) |
|
||||
| `--settings <json>` | Plugin settings JSON | No |
|
||||
|
||||
### `lh plugin uninstall <id>`
|
||||
|
||||
```bash
|
||||
lh plugin uninstall < id > [--yes]
|
||||
```
|
||||
|
||||
### `lh plugin update <id>`
|
||||
|
||||
```bash
|
||||
lh plugin update [--settings < id > [--manifest < json > ] < json > ]
|
||||
```
|
||||
@@ -1,614 +0,0 @@
|
||||
---
|
||||
name: data-fetching
|
||||
description: Data fetching architecture guide using Service layer + Zustand Store + SWR. Use when implementing data fetching, creating services, working with store hooks, or migrating from useEffect. Triggers on data loading, API calls, service creation, or store data fetching tasks.
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# LobeHub Data Fetching Architecture
|
||||
|
||||
> **Related:** `store-data-structures` covers List vs Detail data shape rationale (Map vs Array).
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
```text
|
||||
┌─────────────┐
|
||||
│ Component │
|
||||
└──────┬──────┘
|
||||
│ 1. Call useFetchXxx hook from store
|
||||
↓
|
||||
┌──────────────────┐
|
||||
│ Zustand Store │
|
||||
│ (State + Hook) │
|
||||
└──────┬───────────┘
|
||||
│ 2. useClientDataSWR calls service
|
||||
↓
|
||||
┌──────────────────┐
|
||||
│ Service Layer │
|
||||
│ (xxxService) │
|
||||
└──────┬───────────┘
|
||||
│ 3. Call lambdaClient
|
||||
↓
|
||||
┌──────────────────┐
|
||||
│ lambdaClient │
|
||||
│ (TRPC Client) │
|
||||
└──────────────────┘
|
||||
```
|
||||
|
||||
## Core Principles
|
||||
|
||||
### ✅ DO
|
||||
|
||||
1. **Use Service Layer** for all API calls
|
||||
2. **Use Store SWR Hooks** for data fetching (not useEffect)
|
||||
3. **Use proper data structures** — see `store-data-structures` skill for List vs Detail patterns
|
||||
4. **Use lambdaClient.mutate** for write operations (create/update/delete)
|
||||
5. **Use lambdaClient.query** only inside service methods
|
||||
6. **Naming convention** — read hooks are `useFetchXxx`, cache invalidation helpers are `refreshXxx` (e.g. `useFetchBenchmarks` / `refreshBenchmarks`). Mutations then chain `refreshXxx()` after the service call.
|
||||
|
||||
### ❌ DON'T
|
||||
|
||||
1. **Never use useEffect** for data fetching
|
||||
2. **Never call lambdaClient** directly in components or stores
|
||||
3. **Never use useState** for server data
|
||||
4. **Never mix data structure patterns** — follow `store-data-structures` skill
|
||||
|
||||
---
|
||||
|
||||
## Layer 1: Service Layer
|
||||
|
||||
### Purpose
|
||||
|
||||
- Encapsulate all API calls to lambdaClient
|
||||
- Provide clean, typed interfaces
|
||||
- Single source of truth for API operations
|
||||
|
||||
### Service Structure
|
||||
|
||||
```typescript
|
||||
// src/services/agentEval.ts
|
||||
class AgentEvalService {
|
||||
// Query methods - READ operations
|
||||
async listBenchmarks() {
|
||||
return lambdaClient.agentEval.listBenchmarks.query();
|
||||
}
|
||||
|
||||
async getBenchmark(id: string) {
|
||||
return lambdaClient.agentEval.getBenchmark.query({ id });
|
||||
}
|
||||
|
||||
// Mutation methods - WRITE operations
|
||||
async createBenchmark(params: CreateBenchmarkParams) {
|
||||
return lambdaClient.agentEval.createBenchmark.mutate(params);
|
||||
}
|
||||
|
||||
async updateBenchmark(params: UpdateBenchmarkParams) {
|
||||
return lambdaClient.agentEval.updateBenchmark.mutate(params);
|
||||
}
|
||||
|
||||
async deleteBenchmark(id: string) {
|
||||
return lambdaClient.agentEval.deleteBenchmark.mutate({ id });
|
||||
}
|
||||
}
|
||||
|
||||
export const agentEvalService = new AgentEvalService();
|
||||
```
|
||||
|
||||
### Service Guidelines
|
||||
|
||||
1. **One service per domain** (e.g., agentEval, ragEval, aiAgent)
|
||||
2. **Export singleton instance** (`export const xxxService = new XxxService()`)
|
||||
3. **Method names match operations** (list, get, create, update, delete)
|
||||
4. **Clear parameter types** (use interfaces for complex params)
|
||||
|
||||
---
|
||||
|
||||
## Layer 2: Store with SWR Hooks
|
||||
|
||||
### Purpose
|
||||
|
||||
- Manage client-side state
|
||||
- Provide SWR hooks for data fetching
|
||||
- Handle cache invalidation
|
||||
|
||||
### State Structure
|
||||
|
||||
```typescript
|
||||
// src/store/eval/slices/benchmark/initialState.ts
|
||||
export interface BenchmarkSliceState {
|
||||
// List data - simple array
|
||||
benchmarkList: AgentEvalBenchmarkListItem[];
|
||||
benchmarkListInit: boolean;
|
||||
|
||||
// Detail data - map for caching
|
||||
benchmarkDetailMap: Record<string, AgentEvalBenchmark>;
|
||||
loadingBenchmarkDetailIds: string[];
|
||||
|
||||
// Mutation states
|
||||
isCreatingBenchmark: boolean;
|
||||
isUpdatingBenchmark: boolean;
|
||||
isDeletingBenchmark: boolean;
|
||||
}
|
||||
```
|
||||
|
||||
> For complete initialState, reducer, and internal dispatch patterns, see the `store-data-structures` skill.
|
||||
|
||||
### Actions
|
||||
|
||||
```typescript
|
||||
// src/store/eval/slices/benchmark/action.ts
|
||||
const FETCH_BENCHMARKS_KEY = 'FETCH_BENCHMARKS';
|
||||
const FETCH_BENCHMARK_DETAIL_KEY = 'FETCH_BENCHMARK_DETAIL';
|
||||
|
||||
export interface BenchmarkAction {
|
||||
// SWR Hooks - for data fetching
|
||||
useFetchBenchmarks: () => SWRResponse;
|
||||
useFetchBenchmarkDetail: (id?: string) => SWRResponse;
|
||||
|
||||
// Refresh methods - for cache invalidation
|
||||
refreshBenchmarks: () => Promise<void>;
|
||||
refreshBenchmarkDetail: (id: string) => Promise<void>;
|
||||
|
||||
// Mutation actions
|
||||
createBenchmark: (params: CreateParams) => Promise<any>;
|
||||
updateBenchmark: (params: UpdateParams) => Promise<void>;
|
||||
deleteBenchmark: (id: string) => Promise<void>;
|
||||
|
||||
// Internal methods - not for direct UI use
|
||||
internal_dispatchBenchmarkDetail: (payload: BenchmarkDetailDispatch) => void;
|
||||
internal_updateBenchmarkDetailLoading: (id: string, loading: boolean) => void;
|
||||
}
|
||||
|
||||
export const createBenchmarkSlice: StateCreator<EvalStore, any, [], BenchmarkAction> = (
|
||||
set,
|
||||
get,
|
||||
) => ({
|
||||
// Fetch list — simple array stored in benchmarkList
|
||||
useFetchBenchmarks: () =>
|
||||
useClientDataSWR(FETCH_BENCHMARKS_KEY, () => agentEvalService.listBenchmarks(), {
|
||||
onSuccess: (data) => {
|
||||
set({ benchmarkList: data, benchmarkListInit: true }, false, 'useFetchBenchmarks/success');
|
||||
},
|
||||
}),
|
||||
|
||||
// Fetch detail — null key disables the request when id is missing
|
||||
useFetchBenchmarkDetail: (id) =>
|
||||
useClientDataSWR(
|
||||
id ? [FETCH_BENCHMARK_DETAIL_KEY, id] : null,
|
||||
() => agentEvalService.getBenchmark(id!),
|
||||
{
|
||||
onSuccess: (data) => {
|
||||
get().internal_dispatchBenchmarkDetail({
|
||||
type: 'setBenchmarkDetail',
|
||||
id: id!,
|
||||
value: data,
|
||||
});
|
||||
get().internal_updateBenchmarkDetailLoading(id!, false);
|
||||
},
|
||||
},
|
||||
),
|
||||
|
||||
// Refresh methods
|
||||
refreshBenchmarks: () => mutate(FETCH_BENCHMARKS_KEY),
|
||||
refreshBenchmarkDetail: (id) => mutate([FETCH_BENCHMARK_DETAIL_KEY, id]),
|
||||
|
||||
// CREATE — refresh list after creation
|
||||
createBenchmark: async (params) => {
|
||||
set({ isCreatingBenchmark: true }, false, 'createBenchmark/start');
|
||||
try {
|
||||
const result = await agentEvalService.createBenchmark(params);
|
||||
await get().refreshBenchmarks();
|
||||
return result;
|
||||
} finally {
|
||||
set({ isCreatingBenchmark: false }, false, 'createBenchmark/end');
|
||||
}
|
||||
},
|
||||
|
||||
// UPDATE — optimistic update + refresh
|
||||
updateBenchmark: async (params) => {
|
||||
const { id } = params;
|
||||
|
||||
// 1. Optimistic update
|
||||
get().internal_dispatchBenchmarkDetail({
|
||||
type: 'updateBenchmarkDetail',
|
||||
id,
|
||||
value: params,
|
||||
});
|
||||
// 2. Set loading
|
||||
get().internal_updateBenchmarkDetailLoading(id, true);
|
||||
|
||||
try {
|
||||
// 3. Call service
|
||||
await agentEvalService.updateBenchmark(params);
|
||||
// 4. Refresh from server
|
||||
await get().refreshBenchmarks();
|
||||
await get().refreshBenchmarkDetail(id);
|
||||
} finally {
|
||||
get().internal_updateBenchmarkDetailLoading(id, false);
|
||||
}
|
||||
},
|
||||
|
||||
// DELETE — optimistic update + refresh
|
||||
deleteBenchmark: async (id) => {
|
||||
get().internal_dispatchBenchmarkDetail({ type: 'deleteBenchmarkDetail', id });
|
||||
get().internal_updateBenchmarkDetailLoading(id, true);
|
||||
|
||||
try {
|
||||
await agentEvalService.deleteBenchmark(id);
|
||||
await get().refreshBenchmarks();
|
||||
} finally {
|
||||
get().internal_updateBenchmarkDetailLoading(id, false);
|
||||
}
|
||||
},
|
||||
|
||||
// Internal — dispatch to reducer (for detail map)
|
||||
internal_dispatchBenchmarkDetail: (payload) => {
|
||||
const currentMap = get().benchmarkDetailMap;
|
||||
const nextMap = benchmarkDetailReducer(currentMap, payload);
|
||||
|
||||
// Skip set when nothing changed — avoids unnecessary re-renders
|
||||
if (isEqual(nextMap, currentMap)) return;
|
||||
set({ benchmarkDetailMap: nextMap }, false, `dispatchBenchmarkDetail/${payload.type}`);
|
||||
},
|
||||
|
||||
// Internal — update loading state for specific detail
|
||||
internal_updateBenchmarkDetailLoading: (id, loading) => {
|
||||
set(
|
||||
(state) => ({
|
||||
loadingBenchmarkDetailIds: loading
|
||||
? [...state.loadingBenchmarkDetailIds, id]
|
||||
: state.loadingBenchmarkDetailIds.filter((i) => i !== id),
|
||||
}),
|
||||
false,
|
||||
'updateBenchmarkDetailLoading',
|
||||
);
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### Store Guidelines
|
||||
|
||||
1. **SWR keys as constants** at top of file
|
||||
2. **useClientDataSWR** for all data fetching (never useEffect)
|
||||
3. **onSuccess callback** updates store state
|
||||
4. **Refresh methods** use `mutate()` to invalidate cache
|
||||
5. **Loading states** in initialState, updated in onSuccess
|
||||
6. **Mutations** call service, then refresh relevant cache
|
||||
|
||||
---
|
||||
|
||||
## Layer 3: Component Usage
|
||||
|
||||
### Fetching List Data
|
||||
|
||||
```tsx
|
||||
// ✅ CORRECT
|
||||
const BenchmarkList = () => {
|
||||
// 1. Get the hook from store
|
||||
const useFetchBenchmarks = useEvalStore((s) => s.useFetchBenchmarks);
|
||||
|
||||
// 2. Get list data
|
||||
const benchmarks = useEvalStore((s) => s.benchmarkList);
|
||||
const isInit = useEvalStore((s) => s.benchmarkListInit);
|
||||
|
||||
// 3. Call the hook (SWR handles the data fetching)
|
||||
useFetchBenchmarks();
|
||||
|
||||
// 4. Use the data
|
||||
if (!isInit) return <Loading />;
|
||||
return (
|
||||
<div>
|
||||
<h2>Total: {benchmarks.length}</h2>
|
||||
{benchmarks.map((b) => (
|
||||
<BenchmarkCard key={b.id} {...b} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
### Fetching Detail Data
|
||||
|
||||
```tsx
|
||||
// ✅ CORRECT
|
||||
const BenchmarkDetail = () => {
|
||||
const { benchmarkId } = useParams<{ benchmarkId: string }>();
|
||||
|
||||
const useFetchBenchmarkDetail = useEvalStore((s) => s.useFetchBenchmarkDetail);
|
||||
|
||||
// Detail from map
|
||||
const benchmark = useEvalStore((s) =>
|
||||
benchmarkId ? s.benchmarkDetailMap[benchmarkId] : undefined,
|
||||
);
|
||||
|
||||
// Per-item loading
|
||||
const isLoading = useEvalStore((s) =>
|
||||
benchmarkId ? s.loadingBenchmarkDetailIds.includes(benchmarkId) : false,
|
||||
);
|
||||
|
||||
useFetchBenchmarkDetail(benchmarkId);
|
||||
|
||||
if (!benchmark) return <Loading />;
|
||||
return (
|
||||
<div>
|
||||
<h1>{benchmark.name}</h1>
|
||||
<p>{benchmark.description}</p>
|
||||
{isLoading && <Spinner />}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
### Using Selectors (Recommended)
|
||||
|
||||
```typescript
|
||||
// src/store/eval/slices/benchmark/selectors.ts
|
||||
export const benchmarkSelectors = {
|
||||
getBenchmarkDetail: (id: string) => (s: EvalStore) => s.benchmarkDetailMap[id],
|
||||
isLoadingBenchmarkDetail: (id: string) => (s: EvalStore) =>
|
||||
s.loadingBenchmarkDetailIds.includes(id),
|
||||
};
|
||||
|
||||
// Component with selectors
|
||||
const BenchmarkDetail = () => {
|
||||
const { benchmarkId } = useParams();
|
||||
const useFetchBenchmarkDetail = useEvalStore((s) => s.useFetchBenchmarkDetail);
|
||||
const benchmark = useEvalStore(benchmarkSelectors.getBenchmarkDetail(benchmarkId!));
|
||||
|
||||
useFetchBenchmarkDetail(benchmarkId);
|
||||
|
||||
return <div>{benchmark && <h1>{benchmark.name}</h1>}</div>;
|
||||
};
|
||||
```
|
||||
|
||||
### Anti-pattern
|
||||
|
||||
```tsx
|
||||
// ❌ WRONG — Don't use useEffect for data fetching
|
||||
const BenchmarkList = () => {
|
||||
const [data, setData] = useState([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true);
|
||||
lambdaClient.agentEval.listBenchmarks
|
||||
.query()
|
||||
.then(setData)
|
||||
.finally(() => setLoading(false));
|
||||
}, []);
|
||||
|
||||
return <div>...</div>;
|
||||
};
|
||||
```
|
||||
|
||||
### Mutations in Components
|
||||
|
||||
```tsx
|
||||
// Create — global mutation flag drives form loading
|
||||
const CreateBenchmarkModal = () => {
|
||||
const createBenchmark = useEvalStore((s) => s.createBenchmark);
|
||||
const isCreating = useEvalStore((s) => s.isCreatingBenchmark);
|
||||
|
||||
const handleSubmit = async (values) => {
|
||||
try {
|
||||
// Optimistic update + refresh happen inside createBenchmark
|
||||
await createBenchmark(values);
|
||||
message.success('Created successfully');
|
||||
onClose();
|
||||
} catch (error) {
|
||||
message.error('Failed to create');
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Form onSubmit={handleSubmit} loading={isCreating}>
|
||||
...
|
||||
</Form>
|
||||
);
|
||||
};
|
||||
|
||||
// Update / delete — per-item loading so only the row being mutated spins
|
||||
const BenchmarkItem = ({ id }: { id: string }) => {
|
||||
const updateBenchmark = useEvalStore((s) => s.updateBenchmark);
|
||||
const deleteBenchmark = useEvalStore((s) => s.deleteBenchmark);
|
||||
const isLoading = useEvalStore(benchmarkSelectors.isLoadingBenchmarkDetail(id));
|
||||
|
||||
const handleUpdate = async (data) => {
|
||||
await updateBenchmark({ id, ...data });
|
||||
};
|
||||
|
||||
const handleDelete = async () => {
|
||||
await deleteBenchmark(id);
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
{isLoading && <Spinner />}
|
||||
<button onClick={handleUpdate}>Update</button>
|
||||
<button onClick={handleDelete}>Delete</button>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
**Why two patterns:** create has no id yet, so a single `isCreatingXxx` flag is enough. Update/delete target a specific row, so global flags would freeze unrelated rows — keep per-item state in `loadingXxxIds`.
|
||||
|
||||
---
|
||||
|
||||
## Need a fuller worked example?
|
||||
|
||||
The canonical `Benchmark` example above is the one to copy for a flat list + detail map. If you need to maintain a list **keyed by a parent id** (e.g. `datasetMap[benchmarkId]` because the same shape appears under multiple parents), read [`references/walkthrough.md`](./references/walkthrough.md) — it walks through the full 6 steps (service → reducer → slice → store wiring → selectors → component) for that variant.
|
||||
|
||||
---
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Pattern 1: Pagination
|
||||
|
||||
Cache key array must include every parameter that should trigger a refetch.
|
||||
|
||||
```typescript
|
||||
useFetchTestCases: (params: { datasetId: string; limit: number; offset: number }) =>
|
||||
useClientDataSWR(
|
||||
params.datasetId ? [FETCH_TEST_CASES_KEY, params.datasetId, params.limit, params.offset] : null,
|
||||
() => agentEvalService.listTestCases(params),
|
||||
{
|
||||
onSuccess: (data) =>
|
||||
set({
|
||||
testCaseList: data.data,
|
||||
testCaseTotal: data.total,
|
||||
isLoadingTestCases: false,
|
||||
}),
|
||||
},
|
||||
);
|
||||
```
|
||||
|
||||
### Pattern 2: Dependent Fetching
|
||||
|
||||
Both hooks run in parallel — SWR dedupes, no manual sequencing needed.
|
||||
|
||||
```tsx
|
||||
const BenchmarkDetail = () => {
|
||||
const { benchmarkId } = useParams();
|
||||
const useFetchBenchmarkDetail = useEvalStore((s) => s.useFetchBenchmarkDetail);
|
||||
const useFetchDatasets = useEvalStore((s) => s.useFetchDatasets);
|
||||
|
||||
useFetchBenchmarkDetail(benchmarkId);
|
||||
useFetchDatasets(benchmarkId);
|
||||
|
||||
return <div>...</div>;
|
||||
};
|
||||
```
|
||||
|
||||
### Pattern 3: Conditional Fetching
|
||||
|
||||
Pass `undefined` to disable the hook entirely.
|
||||
|
||||
```tsx
|
||||
// only fetch when modal is open AND id present
|
||||
useFetchDatasetDetail(open && datasetId ? datasetId : undefined);
|
||||
```
|
||||
|
||||
### Pattern 4: Cross-domain Refresh
|
||||
|
||||
```typescript
|
||||
deleteBenchmark: async (id) => {
|
||||
await agentEvalService.deleteBenchmark(id);
|
||||
await get().refreshBenchmarks();
|
||||
await get().refreshDatasets(id); // related cache invalidated too
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Migration Guide: useEffect → Store SWR
|
||||
|
||||
### Before (❌ Wrong)
|
||||
|
||||
```tsx
|
||||
const TestCaseList = ({ datasetId }: Props) => {
|
||||
const [data, setData] = useState<any[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true);
|
||||
lambdaClient.agentEval.listTestCases
|
||||
.query({ datasetId })
|
||||
.then((r) => setData(r.data))
|
||||
.finally(() => setLoading(false));
|
||||
}, [datasetId]);
|
||||
|
||||
return <Table data={data} loading={loading} />;
|
||||
};
|
||||
```
|
||||
|
||||
### After (✅ Correct)
|
||||
|
||||
```typescript
|
||||
// 1. Add service method
|
||||
class AgentEvalService {
|
||||
async listTestCases(params: { datasetId: string }) {
|
||||
return lambdaClient.agentEval.listTestCases.query(params);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Add store slice hook
|
||||
export const createTestCaseSlice: StateCreator<...> = (set) => ({
|
||||
useFetchTestCases: (params) =>
|
||||
useClientDataSWR(
|
||||
params.datasetId ? [FETCH_TEST_CASES_KEY, params.datasetId] : null,
|
||||
() => agentEvalService.listTestCases(params),
|
||||
{
|
||||
onSuccess: (data) =>
|
||||
set({ testCaseList: data.data, isLoadingTestCases: false }),
|
||||
},
|
||||
),
|
||||
});
|
||||
|
||||
// 3. Component reads from store
|
||||
const TestCaseList = ({ datasetId }: Props) => {
|
||||
const useFetchTestCases = useEvalStore((s) => s.useFetchTestCases);
|
||||
const data = useEvalStore((s) => s.testCaseList);
|
||||
const loading = useEvalStore((s) => s.isLoadingTestCases);
|
||||
|
||||
useFetchTestCases({ datasetId });
|
||||
|
||||
return <Table data={data} loading={loading} />;
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
| Symptom | Check |
|
||||
| --------------------------- | ------------------------------------------------------------------- |
|
||||
| Data never loads | Hook called? Key not `null`/`undefined`? Network tab shows request? |
|
||||
| Stale data after mutation | Did `refreshXxx` run? Cache key matches what the hook uses? |
|
||||
| Loading state stuck `true` | `onSuccess` writes loading=false? Promise rejected silently? |
|
||||
| Detail map missing an entry | Reducer dispatch ran? `isEqual` short-circuited on stale data? |
|
||||
|
||||
---
|
||||
|
||||
## Summary Checklist
|
||||
|
||||
When adding new data fetching:
|
||||
|
||||
### Step 1: Types & State
|
||||
|
||||
See `store-data-structures` for details.
|
||||
|
||||
- [ ] Define types in `@lobechat/types`: Detail type + List item type
|
||||
- [ ] State structure: `xxxList: XxxListItem[]`, `xxxDetailMap: Record<string, Xxx>`, `loadingXxxDetailIds: string[]`
|
||||
- [ ] Reducer if optimistic updates are needed
|
||||
|
||||
### Step 2: Service Layer
|
||||
|
||||
- [ ] Create service in `src/services/xxxService.ts`
|
||||
- [ ] Methods: `listXxx()`, `getXxx(id)`, `createXxx()`, `updateXxx()`, `deleteXxx()`
|
||||
|
||||
### Step 3: Store Actions
|
||||
|
||||
- [ ] `initialState.ts` with state structure
|
||||
- [ ] `action.ts` with:
|
||||
- [ ] `useFetchXxxList()`, `useFetchXxxDetail(id)` — SWR hooks
|
||||
- [ ] `refreshXxxList()`, `refreshXxxDetail(id)` — cache invalidation
|
||||
- [ ] CRUD methods calling service
|
||||
- [ ] `internal_dispatch`, `internal_updateLoading` if using reducer
|
||||
- [ ] `selectors.ts` (optional but recommended)
|
||||
- [ ] Integrate slice into main store + initialState
|
||||
|
||||
### Step 4: Component Usage
|
||||
|
||||
- [ ] Use store hooks (NOT useEffect)
|
||||
- [ ] List pages: access `xxxList` array
|
||||
- [ ] Detail pages: access `xxxDetailMap[id]`
|
||||
- [ ] Use loading states for UI feedback
|
||||
|
||||
**Mental model:** Types → Service → Reducer → Slice → Component 🎯
|
||||
|
||||
---
|
||||
|
||||
## Related Skills
|
||||
|
||||
- **`store-data-structures`** — How to structure List and Detail data in stores
|
||||
- **`zustand`** — General Zustand patterns and best practices
|
||||
@@ -1,244 +0,0 @@
|
||||
# Walkthrough: Adding a New Feature End-to-End
|
||||
|
||||
This is a worked example of the canonical 6-step recipe applied to a new entity (`Dataset`), showing a variant of the main skill's pattern: **a list keyed by a parent id** (`datasetMap[benchmarkId]`), useful when the same shape appears under different parents.
|
||||
|
||||
If you only need the canonical (single-array) pattern, the main `SKILL.md` already shows it for `Benchmark`. Read this file when you need the parent-keyed Map variant, or when you want a checklist-style walkthrough.
|
||||
|
||||
## Step 1: Add Service methods
|
||||
|
||||
```typescript
|
||||
class AgentEvalService {
|
||||
async listDatasets(benchmarkId: string) {
|
||||
return lambdaClient.agentEval.listDatasets.query({ benchmarkId });
|
||||
}
|
||||
async getDataset(id: string) {
|
||||
return lambdaClient.agentEval.getDataset.query({ id });
|
||||
}
|
||||
async createDataset(params: CreateDatasetParams) {
|
||||
return lambdaClient.agentEval.createDataset.mutate(params);
|
||||
}
|
||||
// updateDataset / deleteDataset follow the same shape
|
||||
}
|
||||
```
|
||||
|
||||
## Step 2: Reducer (optimistic updates)
|
||||
|
||||
```typescript
|
||||
// src/store/eval/slices/dataset/reducer.ts
|
||||
export type DatasetDispatch =
|
||||
| { type: 'addDataset'; value: Dataset }
|
||||
| { type: 'updateDataset'; id: string; value: Partial<Dataset> }
|
||||
| { type: 'deleteDataset'; id: string };
|
||||
|
||||
export const datasetReducer = (state: Dataset[] = [], payload: DatasetDispatch): Dataset[] =>
|
||||
produce(state, (draft) => {
|
||||
switch (payload.type) {
|
||||
case 'addDataset':
|
||||
draft.unshift(payload.value);
|
||||
break;
|
||||
case 'updateDataset': {
|
||||
const i = draft.findIndex((item) => item.id === payload.id);
|
||||
if (i !== -1) draft[i] = { ...draft[i], ...payload.value };
|
||||
break;
|
||||
}
|
||||
case 'deleteDataset': {
|
||||
const i = draft.findIndex((item) => item.id === payload.id);
|
||||
if (i !== -1) draft.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
## Step 3: Store slice
|
||||
|
||||
```typescript
|
||||
// src/store/eval/slices/dataset/initialState.ts
|
||||
export interface DatasetData {
|
||||
currentPage: number;
|
||||
hasMore: boolean;
|
||||
isLoading: boolean;
|
||||
items: Dataset[];
|
||||
pageSize: number;
|
||||
total: number;
|
||||
}
|
||||
|
||||
export interface DatasetSliceState {
|
||||
// Map keyed by benchmarkId — multiple parent contexts share the slice
|
||||
datasetMap: Record<string, DatasetData>;
|
||||
// Single item for modal display
|
||||
datasetDetail: Dataset | null;
|
||||
isLoadingDatasetDetail: boolean;
|
||||
loadingDatasetIds: string[];
|
||||
}
|
||||
|
||||
export const datasetInitialState: DatasetSliceState = {
|
||||
datasetMap: {},
|
||||
datasetDetail: null,
|
||||
isLoadingDatasetDetail: false,
|
||||
loadingDatasetIds: [],
|
||||
};
|
||||
```
|
||||
|
||||
```typescript
|
||||
// src/store/eval/slices/dataset/action.ts
|
||||
const FETCH_DATASETS_KEY = 'FETCH_DATASETS';
|
||||
const FETCH_DATASET_DETAIL_KEY = 'FETCH_DATASET_DETAIL';
|
||||
|
||||
export const createDatasetSlice: StateCreator<EvalStore, any, [], DatasetAction> = (set, get) => ({
|
||||
// Cache key includes benchmarkId so each parent has its own SWR entry
|
||||
useFetchDatasets: (benchmarkId) =>
|
||||
useClientDataSWR(
|
||||
benchmarkId ? [FETCH_DATASETS_KEY, benchmarkId] : null,
|
||||
() => agentEvalService.listDatasets(benchmarkId!),
|
||||
{
|
||||
onSuccess: (data) => {
|
||||
set({
|
||||
datasetMap: {
|
||||
...get().datasetMap,
|
||||
[benchmarkId!]: {
|
||||
currentPage: 1,
|
||||
hasMore: false,
|
||||
isLoading: false,
|
||||
items: data,
|
||||
pageSize: data.length,
|
||||
total: data.length,
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
),
|
||||
|
||||
useFetchDatasetDetail: (id) =>
|
||||
useClientDataSWR(
|
||||
id ? [FETCH_DATASET_DETAIL_KEY, id] : null,
|
||||
() => agentEvalService.getDataset(id!),
|
||||
{
|
||||
onSuccess: (data) => set({ datasetDetail: data, isLoadingDatasetDetail: false }),
|
||||
},
|
||||
),
|
||||
|
||||
refreshDatasets: (benchmarkId) => mutate([FETCH_DATASETS_KEY, benchmarkId]),
|
||||
refreshDatasetDetail: (id) => mutate([FETCH_DATASET_DETAIL_KEY, id]),
|
||||
|
||||
// CREATE with optimistic update — note the temp id pattern
|
||||
createDataset: async (params) => {
|
||||
const tmpId = Date.now().toString();
|
||||
const { benchmarkId } = params;
|
||||
|
||||
get().internal_dispatchDataset(
|
||||
{ type: 'addDataset', value: { ...params, id: tmpId, createdAt: Date.now() } as any },
|
||||
benchmarkId,
|
||||
);
|
||||
get().internal_updateDatasetLoading(tmpId, true);
|
||||
|
||||
try {
|
||||
const result = await agentEvalService.createDataset(params);
|
||||
await get().refreshDatasets(benchmarkId);
|
||||
return result;
|
||||
} finally {
|
||||
get().internal_updateDatasetLoading(tmpId, false);
|
||||
}
|
||||
},
|
||||
|
||||
// UPDATE / DELETE follow the same optimistic + refresh pattern as BenchmarkSlice
|
||||
// (see the main SKILL.md)
|
||||
|
||||
// Internal — dispatch reducer scoped to a parent
|
||||
internal_dispatchDataset: (payload, benchmarkId) => {
|
||||
const currentData = get().datasetMap[benchmarkId];
|
||||
const nextItems = datasetReducer(currentData?.items, payload);
|
||||
|
||||
// Skip set when nothing changed — avoids unnecessary re-renders
|
||||
if (isEqual(nextItems, currentData?.items)) return;
|
||||
|
||||
set({
|
||||
datasetMap: {
|
||||
...get().datasetMap,
|
||||
[benchmarkId]: {
|
||||
...currentData,
|
||||
currentPage: currentData?.currentPage ?? 1,
|
||||
hasMore: currentData?.hasMore ?? false,
|
||||
isLoading: false,
|
||||
items: nextItems,
|
||||
pageSize: currentData?.pageSize ?? nextItems.length,
|
||||
total: currentData?.total ?? nextItems.length,
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
internal_updateDatasetLoading: (id, loading) => {
|
||||
set((state) => ({
|
||||
loadingDatasetIds: loading
|
||||
? [...state.loadingDatasetIds, id]
|
||||
: state.loadingDatasetIds.filter((i) => i !== id),
|
||||
}));
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
## Step 4: Wire into the store
|
||||
|
||||
```typescript
|
||||
// src/store/eval/store.ts
|
||||
export type EvalStore = EvalStoreState & BenchmarkAction & DatasetAction & RunAction;
|
||||
|
||||
const createStore: StateCreator<EvalStore, [['zustand/devtools', never]]> = (set, get, store) => ({
|
||||
...initialState,
|
||||
...createBenchmarkSlice(set, get, store),
|
||||
...createDatasetSlice(set, get, store),
|
||||
...createRunSlice(set, get, store),
|
||||
});
|
||||
|
||||
// src/store/eval/initialState.ts
|
||||
export const initialState: EvalStoreState = {
|
||||
...benchmarkInitialState,
|
||||
...datasetInitialState,
|
||||
...runInitialState,
|
||||
};
|
||||
```
|
||||
|
||||
## Step 5: Selectors (optional but recommended)
|
||||
|
||||
```typescript
|
||||
export const datasetSelectors = {
|
||||
getDatasetData: (benchmarkId: string) => (s: EvalStore) => s.datasetMap[benchmarkId],
|
||||
getDatasets: (benchmarkId: string) => (s: EvalStore) => s.datasetMap[benchmarkId]?.items ?? [],
|
||||
isLoadingDataset: (id: string) => (s: EvalStore) => s.loadingDatasetIds.includes(id),
|
||||
};
|
||||
```
|
||||
|
||||
## Step 6: Use in component
|
||||
|
||||
```tsx
|
||||
// List scoped to a parent
|
||||
const DatasetList = ({ benchmarkId }: { benchmarkId: string }) => {
|
||||
const useFetchDatasets = useEvalStore((s) => s.useFetchDatasets);
|
||||
const datasets = useEvalStore(datasetSelectors.getDatasets(benchmarkId));
|
||||
const datasetData = useEvalStore(datasetSelectors.getDatasetData(benchmarkId));
|
||||
|
||||
useFetchDatasets(benchmarkId);
|
||||
|
||||
if (datasetData?.isLoading) return <Loading />;
|
||||
return (
|
||||
<div>
|
||||
<h2>Total: {datasetData?.total ?? 0}</h2>
|
||||
<List data={datasets} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// Single item for modal — conditional fetching pattern
|
||||
const DatasetImportModal = ({ open, datasetId }: Props) => {
|
||||
const useFetchDatasetDetail = useEvalStore((s) => s.useFetchDatasetDetail);
|
||||
const dataset = useEvalStore((s) => s.datasetDetail);
|
||||
const isLoading = useEvalStore((s) => s.isLoadingDatasetDetail);
|
||||
|
||||
// Only fetch when modal is open AND id present
|
||||
useFetchDatasetDetail(open && datasetId ? datasetId : undefined);
|
||||
|
||||
return <Modal open={open}>{isLoading ? <Loading /> : <div>{dataset?.name}</div>}</Modal>;
|
||||
};
|
||||
```
|
||||
@@ -1,107 +0,0 @@
|
||||
---
|
||||
name: db-migrations
|
||||
description: 'Use when generating or regenerating Drizzle migration files, changing database schema tables or columns, resolving migration sequence conflicts after rebase, reviewing migration SQL for idempotent patterns, or renaming migration files.'
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# Database Migrations Guide
|
||||
|
||||
## Step 1: Generate Migrations
|
||||
|
||||
```bash
|
||||
bun run db:generate
|
||||
```
|
||||
|
||||
This generates:
|
||||
|
||||
- `packages/database/migrations/0046_meaningless_file_name.sql`
|
||||
|
||||
And updates:
|
||||
|
||||
- `packages/database/migrations/meta/_journal.json`
|
||||
- `packages/database/src/core/migrations.json`
|
||||
- `docs/development/database-schema.dbml`
|
||||
|
||||
## Custom Migrations (e.g. CREATE EXTENSION)
|
||||
|
||||
For migrations that don't involve Drizzle schema changes (e.g. enabling PostgreSQL extensions), use the `--custom` flag:
|
||||
|
||||
```bash
|
||||
bunx drizzle-kit generate --custom --name=enable_pg_search
|
||||
```
|
||||
|
||||
This generates an empty SQL file and properly updates `_journal.json` and snapshot. Then edit the generated SQL file to add your custom SQL:
|
||||
|
||||
```sql
|
||||
-- Custom SQL migration file, put your code below! --
|
||||
CREATE EXTENSION IF NOT EXISTS pg_search;
|
||||
```
|
||||
|
||||
**Do NOT manually create migration files or edit `_journal.json`** — always use `drizzle-kit generate` to ensure correct journal entries and snapshots.
|
||||
|
||||
## Step 2: Optimize Migration SQL Filename
|
||||
|
||||
Rename auto-generated filename to be meaningful:
|
||||
|
||||
`0046_meaningless_file_name.sql` → `0046_user_add_avatar_column.sql`
|
||||
|
||||
## Step 3: Use Idempotent Clauses (Defensive Programming)
|
||||
|
||||
Always use defensive clauses to make migrations idempotent (safe to re-run):
|
||||
|
||||
### CREATE TABLE
|
||||
|
||||
```sql
|
||||
-- ✅ Good
|
||||
CREATE TABLE IF NOT EXISTS "agent_eval_runs" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"name" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
|
||||
-- ❌ Bad
|
||||
CREATE TABLE "agent_eval_runs" (...);
|
||||
```
|
||||
|
||||
### ALTER TABLE - Columns
|
||||
|
||||
```sql
|
||||
-- ✅ Good
|
||||
ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "avatar" text;
|
||||
ALTER TABLE "posts" DROP COLUMN IF EXISTS "deprecated_field";
|
||||
|
||||
-- ❌ Bad
|
||||
ALTER TABLE "users" ADD COLUMN "avatar" text;
|
||||
```
|
||||
|
||||
### ALTER TABLE - Foreign Key Constraints
|
||||
|
||||
PostgreSQL has no `ADD CONSTRAINT IF NOT EXISTS`. Use `DROP IF EXISTS` + `ADD`:
|
||||
|
||||
```sql
|
||||
-- ✅ Good: Drop first, then add (idempotent)
|
||||
ALTER TABLE "agent_eval_datasets" DROP CONSTRAINT IF EXISTS "agent_eval_datasets_user_id_users_id_fk";
|
||||
ALTER TABLE "agent_eval_datasets" ADD CONSTRAINT "agent_eval_datasets_user_id_users_id_fk"
|
||||
FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;
|
||||
|
||||
-- ❌ Bad: Will fail if constraint already exists
|
||||
ALTER TABLE "agent_eval_datasets" ADD CONSTRAINT "agent_eval_datasets_user_id_users_id_fk"
|
||||
FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;
|
||||
```
|
||||
|
||||
### DROP TABLE / INDEX
|
||||
|
||||
```sql
|
||||
-- ✅ Good
|
||||
DROP TABLE IF EXISTS "old_table";
|
||||
CREATE INDEX IF NOT EXISTS "users_email_idx" ON "users" ("email");
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "users_email_unique" ON "users" USING btree ("email");
|
||||
|
||||
-- ❌ Bad
|
||||
DROP TABLE "old_table";
|
||||
CREATE INDEX "users_email_idx" ON "users" ("email");
|
||||
```
|
||||
|
||||
## Step 4: Update Journal Tag
|
||||
|
||||
After renaming the migration SQL file in Step 2, update the `tag` field in `packages/database/migrations/meta/_journal.json` to match the new filename (without `.sql` extension).
|
||||
@@ -1,66 +0,0 @@
|
||||
---
|
||||
name: debug-package
|
||||
description: "Guide for the `debug` npm package and LobeHub log namespaces (lobe-server:*, lobe-desktop:*, lobe-client:*, lobe-*-router:*). Use whenever adding a `debug(...)` logger, picking a namespace for new server/desktop/client/router code, troubleshooting why DEBUG=lobe-* logs don't show up, or when the user asks to 'add logging', 'add a logger', 'instrument this', 'trace this call', 'why isn't my log printing', or mentions `debug(`, `DEBUG=`, `localStorage.debug`, or log format specifiers like %O / %o / %s / %d in a LobeHub codebase."
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# Debug Package Usage Guide
|
||||
|
||||
## Basic Usage
|
||||
|
||||
```typescript
|
||||
import debug from 'debug';
|
||||
|
||||
// Format: lobe-[module]:[submodule]
|
||||
const log = debug('lobe-server:market');
|
||||
|
||||
log('Simple message');
|
||||
log('With variable: %O', object);
|
||||
log('Formatted number: %d', number);
|
||||
```
|
||||
|
||||
## Namespace Conventions
|
||||
|
||||
- Desktop: `lobe-desktop:[module]`
|
||||
- Server: `lobe-server:[module]`
|
||||
- Client: `lobe-client:[module]`
|
||||
- Router: `lobe-[type]-router:[module]`
|
||||
|
||||
## Format Specifiers
|
||||
|
||||
- `%O` - Object expanded (recommended for complex objects)
|
||||
- `%o` - Object
|
||||
- `%s` - String
|
||||
- `%d` - Number
|
||||
|
||||
## Enable Debug Output
|
||||
|
||||
### Browser
|
||||
|
||||
```javascript
|
||||
localStorage.debug = 'lobe-*';
|
||||
```
|
||||
|
||||
### Node.js
|
||||
|
||||
```bash
|
||||
DEBUG=lobe-* npm run dev
|
||||
DEBUG=lobe-* pnpm dev
|
||||
```
|
||||
|
||||
### Electron
|
||||
|
||||
```typescript
|
||||
process.env.DEBUG = 'lobe-*';
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```typescript
|
||||
// src/server/routers/edge/market/index.ts
|
||||
import debug from 'debug';
|
||||
|
||||
const log = debug('lobe-edge-router:market');
|
||||
|
||||
log('getAgent input: %O', input);
|
||||
```
|
||||
@@ -1,89 +0,0 @@
|
||||
---
|
||||
name: desktop
|
||||
description: Electron desktop development guide. Use when implementing desktop features, IPC handlers, controllers, preload scripts, window management, menu configuration, or Electron-specific functionality. Triggers on desktop app development, Electron IPC, or desktop local tools implementation.
|
||||
disable-model-invocation: true
|
||||
---
|
||||
|
||||
# Desktop Development Guide
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
LobeHub desktop is built on Electron with main-renderer architecture:
|
||||
|
||||
1. **Main Process** (`apps/desktop/src/main`): App lifecycle, system APIs, window management
|
||||
2. **Renderer Process**: Reuses web code from `src/`
|
||||
3. **Preload Scripts** (`apps/desktop/src/preload`): Securely expose main process to renderer
|
||||
|
||||
## Adding New Desktop Features
|
||||
|
||||
### 1. Create Controller
|
||||
|
||||
Location: `apps/desktop/src/main/controllers/`
|
||||
|
||||
```typescript
|
||||
import { ControllerModule, IpcMethod } from '@/controllers';
|
||||
|
||||
export default class NewFeatureCtr extends ControllerModule {
|
||||
static override readonly groupName = 'newFeature';
|
||||
|
||||
@IpcMethod()
|
||||
async doSomething(params: SomeParams): Promise<SomeResult> {
|
||||
// Implementation
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Register in `apps/desktop/src/main/controllers/registry.ts`.
|
||||
|
||||
### 2. Define IPC Types
|
||||
|
||||
Location: `packages/electron-client-ipc/src/types.ts`
|
||||
|
||||
```typescript
|
||||
export interface SomeParams {
|
||||
/* ... */
|
||||
}
|
||||
export interface SomeResult {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Create Renderer Service
|
||||
|
||||
Location: `src/services/electron/`
|
||||
|
||||
```typescript
|
||||
import { ensureElectronIpc } from '@/utils/electron/ipc';
|
||||
|
||||
const ipc = ensureElectronIpc();
|
||||
|
||||
export const newFeatureService = async (params: SomeParams) => {
|
||||
return ipc.newFeature.doSomething(params);
|
||||
};
|
||||
```
|
||||
|
||||
### 4. Implement Store Action
|
||||
|
||||
Location: `src/store/`
|
||||
|
||||
### 5. Add Tests
|
||||
|
||||
Location: `apps/desktop/src/main/controllers/__tests__/`
|
||||
|
||||
## Detailed Guides
|
||||
|
||||
See `references/` for specific topics:
|
||||
|
||||
- **Feature implementation**: `references/feature-implementation.md`
|
||||
- **Local tools workflow**: `references/local-tools.md`
|
||||
- **Menu configuration**: `references/menu-config.md`
|
||||
- **Window management**: `references/window-management.md`
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Security**: Validate inputs, limit exposed APIs
|
||||
2. **Performance**: Use async methods, batch data transfers
|
||||
3. **UX**: Add progress indicators, provide error feedback
|
||||
4. **Code organization**: Follow existing patterns, add documentation
|
||||
@@ -1,103 +0,0 @@
|
||||
# Desktop Feature Implementation Guide
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
```plaintext
|
||||
Main Process Renderer Process
|
||||
┌──────────────────┐ ┌──────────────────┐
|
||||
│ Controller │◄──IPC───►│ Service Layer │
|
||||
│ (IPC Handler) │ │ │
|
||||
└──────────────────┘ └──────────────────┘
|
||||
│ │
|
||||
▼ ▼
|
||||
┌──────────────────┐ ┌──────────────────┐
|
||||
│ System APIs │ │ Store Actions │
|
||||
│ (fs, network) │ │ (UI State) │
|
||||
└──────────────────┘ └──────────────────┘
|
||||
```
|
||||
|
||||
## Step-by-Step Implementation
|
||||
|
||||
### 1. Create Controller
|
||||
|
||||
```typescript
|
||||
// apps/desktop/src/main/controllers/NotificationCtr.ts
|
||||
import type {
|
||||
ShowDesktopNotificationParams,
|
||||
DesktopNotificationResult,
|
||||
} from '@lobechat/electron-client-ipc';
|
||||
import { Notification } from 'electron';
|
||||
import { ControllerModule, IpcMethod } from '@/controllers';
|
||||
|
||||
export default class NotificationCtr extends ControllerModule {
|
||||
static override readonly groupName = 'notification';
|
||||
|
||||
@IpcMethod()
|
||||
async showDesktopNotification(
|
||||
params: ShowDesktopNotificationParams,
|
||||
): Promise<DesktopNotificationResult> {
|
||||
if (!Notification.isSupported()) {
|
||||
return { error: 'Notifications not supported', success: false };
|
||||
}
|
||||
|
||||
try {
|
||||
const notification = new Notification({ body: params.body, title: params.title });
|
||||
notification.show();
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error('[NotificationCtr] Failed:', error);
|
||||
return { error: error instanceof Error ? error.message : 'Unknown error', success: false };
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Define IPC Types
|
||||
|
||||
```typescript
|
||||
// packages/electron-client-ipc/src/types.ts
|
||||
export interface ShowDesktopNotificationParams {
|
||||
title: string;
|
||||
body: string;
|
||||
}
|
||||
|
||||
export interface DesktopNotificationResult {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Create Service Layer
|
||||
|
||||
```typescript
|
||||
// src/services/electron/notificationService.ts
|
||||
import type { ShowDesktopNotificationParams } from '@lobechat/electron-client-ipc';
|
||||
import { ensureElectronIpc } from '@/utils/electron/ipc';
|
||||
|
||||
const ipc = ensureElectronIpc();
|
||||
|
||||
export const notificationService = {
|
||||
show: (params: ShowDesktopNotificationParams) => ipc.notification.showDesktopNotification(params),
|
||||
};
|
||||
```
|
||||
|
||||
### 4. Implement Store Action
|
||||
|
||||
```typescript
|
||||
// src/store/.../actions.ts
|
||||
showNotification: async (title: string, body: string) => {
|
||||
if (!isElectron) return;
|
||||
|
||||
const result = await notificationService.show({ title, body });
|
||||
if (!result.success) {
|
||||
console.error('Notification failed:', result.error);
|
||||
}
|
||||
},
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Security**: Validate inputs, limit exposed APIs
|
||||
2. **Performance**: Use async methods for heavy operations
|
||||
3. **Error handling**: Always return structured results
|
||||
4. **UX**: Provide loading states and error feedback
|
||||
@@ -1,133 +0,0 @@
|
||||
# Desktop Local Tools Implementation
|
||||
|
||||
## Workflow Overview
|
||||
|
||||
1. Define tool interface (Manifest)
|
||||
2. Define related types
|
||||
3. Implement Store Action
|
||||
4. Implement Service Layer
|
||||
5. Implement Controller (IPC Handler)
|
||||
6. Update Agent documentation
|
||||
|
||||
## Step 1: Define Tool Interface (Manifest)
|
||||
|
||||
Location: `src/tools/[tool_category]/index.ts`
|
||||
|
||||
```typescript
|
||||
// src/tools/local-files/index.ts
|
||||
export const LocalFilesApiName = {
|
||||
RenameFile: 'renameFile',
|
||||
MoveFile: 'moveFile',
|
||||
} as const;
|
||||
|
||||
export const LocalFilesManifest = {
|
||||
api: [
|
||||
{
|
||||
name: LocalFilesApiName.RenameFile,
|
||||
description: 'Rename a local file',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
oldPath: { type: 'string', description: 'Current file path' },
|
||||
newName: { type: 'string', description: 'New file name' },
|
||||
},
|
||||
required: ['oldPath', 'newName'],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
```
|
||||
|
||||
## Step 2: Define Types
|
||||
|
||||
```typescript
|
||||
// packages/electron-client-ipc/src/types.ts
|
||||
export interface RenameLocalFileParams {
|
||||
oldPath: string;
|
||||
newName: string;
|
||||
}
|
||||
|
||||
// src/tools/local-files/type.ts
|
||||
export interface LocalRenameFileState {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
oldPath: string;
|
||||
newPath: string;
|
||||
}
|
||||
```
|
||||
|
||||
## Step 3: Implement Store Action
|
||||
|
||||
```typescript
|
||||
// src/store/chat/slices/builtinTool/actions/localFile.ts
|
||||
renameLocalFile: async (id: string, params: RenameLocalFileParams) => {
|
||||
const { toggleLocalFileLoading, updatePluginState, internal_updateMessageContent } = get();
|
||||
|
||||
toggleLocalFileLoading(id, true);
|
||||
|
||||
try {
|
||||
const result = await localFileService.renameFile(params);
|
||||
|
||||
if (result.success) {
|
||||
updatePluginState(id, { success: true, ...result });
|
||||
internal_updateMessageContent(id, JSON.stringify({ success: true }));
|
||||
} else {
|
||||
updatePluginState(id, { success: false, error: result.error });
|
||||
internal_updateMessageContent(id, JSON.stringify({ error: result.error }));
|
||||
}
|
||||
|
||||
return result.success;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
updatePluginState(id, { success: false, error: e.message });
|
||||
return false;
|
||||
} finally {
|
||||
toggleLocalFileLoading(id, false);
|
||||
}
|
||||
},
|
||||
```
|
||||
|
||||
## Step 4: Implement Service Layer
|
||||
|
||||
```typescript
|
||||
// src/services/electron/localFileService.ts
|
||||
import { ensureElectronIpc } from '@/utils/electron/ipc';
|
||||
|
||||
const ipc = ensureElectronIpc();
|
||||
|
||||
export const localFileService = {
|
||||
renameFile: (params: RenameLocalFileParams) => ipc.localFiles.renameFile(params),
|
||||
};
|
||||
```
|
||||
|
||||
## Step 5: Implement Controller
|
||||
|
||||
```typescript
|
||||
// apps/desktop/src/main/controllers/LocalFileCtr.ts
|
||||
import * as fs from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
import { ControllerModule, IpcMethod } from '@/controllers';
|
||||
|
||||
export default class LocalFileCtr extends ControllerModule {
|
||||
static override readonly groupName = 'localFiles';
|
||||
|
||||
@IpcMethod()
|
||||
async renameFile(params: RenameLocalFileParams) {
|
||||
const { oldPath, newName } = params;
|
||||
const newPath = path.join(path.dirname(oldPath), newName);
|
||||
|
||||
try {
|
||||
await fs.rename(oldPath, newPath);
|
||||
return { success: true, newPath };
|
||||
} catch (error) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Step 6: Update Agent Documentation
|
||||
|
||||
Location: `src/tools/[tool_category]/systemRole.ts`
|
||||
|
||||
Add tool description to `<core_capabilities>` and usage guidelines to `<tool_usage_guidelines>`.
|
||||
@@ -1,107 +0,0 @@
|
||||
# Desktop Menu Configuration Guide
|
||||
|
||||
## Menu Types
|
||||
|
||||
1. **App Menu**: Top of window (macOS) or title bar (Windows/Linux)
|
||||
2. **Context Menu**: Right-click menus
|
||||
3. **Tray Menu**: System tray icon menus
|
||||
|
||||
## File Structure
|
||||
|
||||
```plaintext
|
||||
apps/desktop/src/main/
|
||||
├── menus/
|
||||
│ ├── appMenu.ts # App menu config
|
||||
│ ├── contextMenu.ts # Context menu config
|
||||
│ └── factory.ts # Menu factory functions
|
||||
├── controllers/
|
||||
│ ├── MenuCtr.ts # Menu controller
|
||||
│ └── TrayMenuCtr.ts # Tray menu controller
|
||||
```
|
||||
|
||||
## App Menu Configuration
|
||||
|
||||
```typescript
|
||||
// apps/desktop/src/main/menus/appMenu.ts
|
||||
import { BrowserWindow, Menu, MenuItemConstructorOptions } from 'electron';
|
||||
|
||||
export const createAppMenu = (win: BrowserWindow) => {
|
||||
const template: MenuItemConstructorOptions[] = [
|
||||
{
|
||||
label: 'File',
|
||||
submenu: [
|
||||
{
|
||||
label: 'New',
|
||||
accelerator: 'CmdOrCtrl+N',
|
||||
click: () => {
|
||||
/* ... */
|
||||
},
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{ role: 'quit' },
|
||||
],
|
||||
},
|
||||
// ...
|
||||
];
|
||||
|
||||
return Menu.buildFromTemplate(template);
|
||||
};
|
||||
|
||||
// Register in MenuCtr.ts
|
||||
Menu.setApplicationMenu(menu);
|
||||
```
|
||||
|
||||
## Context Menu
|
||||
|
||||
```typescript
|
||||
export const createContextMenu = () => {
|
||||
const template = [
|
||||
{ label: 'Copy', role: 'copy' },
|
||||
{ label: 'Paste', role: 'paste' },
|
||||
];
|
||||
return Menu.buildFromTemplate(template);
|
||||
};
|
||||
|
||||
// Show on right-click
|
||||
const menu = createContextMenu();
|
||||
menu.popup();
|
||||
```
|
||||
|
||||
## Tray Menu
|
||||
|
||||
```typescript
|
||||
// TrayMenuCtr.ts
|
||||
this.tray = new Tray(trayIconPath);
|
||||
const contextMenu = Menu.buildFromTemplate([
|
||||
{ label: 'Show Window', click: this.showMainWindow },
|
||||
{ type: 'separator' },
|
||||
{ label: 'Quit', click: () => app.quit() },
|
||||
]);
|
||||
this.tray.setContextMenu(contextMenu);
|
||||
```
|
||||
|
||||
## i18n Support
|
||||
|
||||
```typescript
|
||||
import { i18n } from '../locales';
|
||||
|
||||
const template = [
|
||||
{
|
||||
label: i18n.t('menu.file'),
|
||||
submenu: [{ label: i18n.t('menu.new'), click: createNew }],
|
||||
},
|
||||
];
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. Use standard roles (`role: 'copy'`) for native behavior
|
||||
2. Use `CmdOrCtrl` for cross-platform shortcuts
|
||||
3. Use `{ type: 'separator' }` to group related items
|
||||
4. Handle platform differences with `process.platform`
|
||||
|
||||
```typescript
|
||||
if (process.platform === 'darwin') {
|
||||
template.unshift({ role: 'appMenu' });
|
||||
}
|
||||
```
|
||||
@@ -1,147 +0,0 @@
|
||||
# Desktop Window Management Guide
|
||||
|
||||
## Window Management Overview
|
||||
|
||||
1. Window creation and configuration
|
||||
2. Window state management (size, position, maximize)
|
||||
3. Multi-window coordination
|
||||
4. Window event handling
|
||||
|
||||
## File Structure
|
||||
|
||||
```plaintext
|
||||
apps/desktop/src/main/
|
||||
├── appBrowsers.ts # Core window management
|
||||
├── controllers/
|
||||
│ └── BrowserWindowsCtr.ts # Window controller
|
||||
└── modules/
|
||||
└── browserWindowManager.ts # Window manager module
|
||||
```
|
||||
|
||||
## Window Creation
|
||||
|
||||
```typescript
|
||||
export const createMainWindow = () => {
|
||||
const mainWindow = new BrowserWindow({
|
||||
width: 1200,
|
||||
height: 800,
|
||||
minWidth: 600,
|
||||
minHeight: 400,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, '../preload/index.js'),
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false,
|
||||
},
|
||||
});
|
||||
|
||||
if (isDev) {
|
||||
mainWindow.loadURL('http://localhost:3000');
|
||||
} else {
|
||||
mainWindow.loadFile(path.join(__dirname, '../../renderer/index.html'));
|
||||
}
|
||||
|
||||
return mainWindow;
|
||||
};
|
||||
```
|
||||
|
||||
## Window State Persistence
|
||||
|
||||
```typescript
|
||||
const saveWindowState = (window: BrowserWindow) => {
|
||||
if (!window.isMinimized() && !window.isMaximized()) {
|
||||
const [x, y] = window.getPosition();
|
||||
const [width, height] = window.getSize();
|
||||
settings.set('windowState', { x, y, width, height });
|
||||
}
|
||||
};
|
||||
|
||||
const restoreWindowState = (window: BrowserWindow) => {
|
||||
const state = settings.get('windowState');
|
||||
if (state) {
|
||||
window.setBounds({ x: state.x, y: state.y, width: state.width, height: state.height });
|
||||
}
|
||||
};
|
||||
|
||||
window.on('close', () => saveWindowState(window));
|
||||
```
|
||||
|
||||
## Multi-Window Management
|
||||
|
||||
```typescript
|
||||
export class WindowManager {
|
||||
private windows: Map<string, BrowserWindow> = new Map();
|
||||
|
||||
createWindow(id: string, options: BrowserWindowConstructorOptions) {
|
||||
const window = new BrowserWindow(options);
|
||||
this.windows.set(id, window);
|
||||
window.on('closed', () => this.windows.delete(id));
|
||||
return window;
|
||||
}
|
||||
|
||||
getWindow(id: string) {
|
||||
return this.windows.get(id);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Window IPC Controller
|
||||
|
||||
```typescript
|
||||
// apps/desktop/src/main/controllers/BrowserWindowsCtr.ts
|
||||
export default class BrowserWindowsCtr extends ControllerModule {
|
||||
static override readonly groupName = 'windows';
|
||||
|
||||
@IpcMethod()
|
||||
minimizeWindow() {
|
||||
BrowserWindow.getFocusedWindow()?.minimize();
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
@IpcMethod()
|
||||
maximizeWindow() {
|
||||
const win = BrowserWindow.getFocusedWindow();
|
||||
win?.isMaximized() ? win.restore() : win?.maximize();
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Renderer Service
|
||||
|
||||
```typescript
|
||||
// src/services/electron/windowService.ts
|
||||
import { ensureElectronIpc } from '@/utils/electron/ipc';
|
||||
|
||||
const ipc = ensureElectronIpc();
|
||||
|
||||
export const windowService = {
|
||||
minimize: () => ipc.windows.minimizeWindow(),
|
||||
maximize: () => ipc.windows.maximizeWindow(),
|
||||
close: () => ipc.windows.closeWindow(),
|
||||
};
|
||||
```
|
||||
|
||||
## Frameless Window
|
||||
|
||||
```typescript
|
||||
const window = new BrowserWindow({
|
||||
frame: false,
|
||||
titleBarStyle: 'hidden',
|
||||
});
|
||||
```
|
||||
|
||||
```css
|
||||
.titlebar {
|
||||
-webkit-app-region: drag;
|
||||
}
|
||||
.titlebar-button {
|
||||
-webkit-app-region: no-drag;
|
||||
}
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. Use `show: false` initially, show after content loads
|
||||
2. Always set secure `webPreferences`
|
||||
3. Handle `webContents.on('crashed')` for recovery
|
||||
4. Clean up resources on `window.on('closed')`
|
||||
@@ -1,155 +0,0 @@
|
||||
---
|
||||
name: docs-changelog
|
||||
description: 'Writing guide for website changelog pages under docs/changelog/*.mdx. Use when creating or editing product update posts in EN/ZH. Not for GitHub Release notes.'
|
||||
---
|
||||
|
||||
# Docs Changelog Writing Guide
|
||||
|
||||
## Scope Boundary (Important)
|
||||
|
||||
This skill is only for changelog pages in:
|
||||
|
||||
- `docs/changelog/*.mdx`
|
||||
|
||||
This skill is **not** for GitHub Releases.\
|
||||
If the user asks for release PR body / GitHub Release notes, load `../version-release/SKILL.md`.
|
||||
|
||||
## Mandatory Companion Skills
|
||||
|
||||
For every docs changelog task, you MUST load:
|
||||
|
||||
- `../microcopy/SKILL.md`
|
||||
- `../i18n/SKILL.md` (when EN/ZH pair is involved)
|
||||
|
||||
## File and Naming Convention
|
||||
|
||||
Use date-based file names:
|
||||
|
||||
- English: `docs/changelog/YYYY-MM-DD-topic.mdx`
|
||||
- Chinese: `docs/changelog/YYYY-MM-DD-topic.zh-CN.mdx`
|
||||
|
||||
EN and ZH files must exist as a pair and describe the same release facts.
|
||||
|
||||
## Frontmatter Requirements
|
||||
|
||||
Each file should include:
|
||||
|
||||
```md
|
||||
---
|
||||
title: <Title>
|
||||
description: <1 sentence summary>
|
||||
tags:
|
||||
- <Tag 1>
|
||||
- <Tag 2>
|
||||
---
|
||||
```
|
||||
|
||||
Rules:
|
||||
|
||||
1. `title` should match the H1 title in meaning.
|
||||
2. `description` should be concise and user-facing.
|
||||
3. `tags` should be feature-oriented, not internal-team labels.
|
||||
|
||||
## Content Structure (Recommended)
|
||||
|
||||
Use this shape unless the user requests otherwise:
|
||||
|
||||
1. `# <Title>`
|
||||
2. Opening paragraph (2-4 sentences): user-visible impact
|
||||
3. 1-3 capability sections (optional `##` headings)
|
||||
4. `## Improvements and fixes` / `## 体验优化与修复` with concise bullets
|
||||
|
||||
Keep heading count low and avoid heading-per-bullet structure.
|
||||
|
||||
## Writing Rules
|
||||
|
||||
1. Keep all claims factual and tied to actual shipped changes.
|
||||
2. Explain user value first, implementation second.
|
||||
3. Prefer natural narrative paragraphs over pure bullet dumps.
|
||||
4. Avoid marketing exaggeration and vague adjectives.
|
||||
5. Keep internal terms consistent across EN/ZH files.
|
||||
6. Keep EN/ZH section order aligned and scope-aligned.
|
||||
|
||||
## EN/ZH Synchronization Rules
|
||||
|
||||
When generating bilingual changelogs:
|
||||
|
||||
1. Keep the same key facts in the same order.
|
||||
2. Localize naturally; do not do literal sentence-by-sentence translation.
|
||||
3. If one version has an `Improvements and fixes` bullet list, the other should have equivalent list intent.
|
||||
4. Do not introduce capabilities in only one language unless explicitly requested.
|
||||
|
||||
## Length Guidance
|
||||
|
||||
- Small update: 3-5 short paragraphs total
|
||||
- Medium update: 4-7 short paragraphs + concise fix bullets
|
||||
- Large update: 6-10 short paragraphs split into 2-4 sections
|
||||
|
||||
Do not pad content when changes are limited.
|
||||
|
||||
## Authoring Workflow
|
||||
|
||||
1. Collect source facts from PRs/commits/issues.
|
||||
2. Group changes by user workflow (not by internal module path).
|
||||
3. Draft EN and ZH versions with aligned structure.
|
||||
4. Verify terminology using `microcopy`/`i18n` guidance.
|
||||
5. Final pass: remove AI-like filler and tighten sentences.
|
||||
|
||||
## Docs Changelog Template (English)
|
||||
|
||||
```md
|
||||
---
|
||||
title: <Feature title>
|
||||
description: <One-sentence summary for users>
|
||||
tags:
|
||||
- <Tag A>
|
||||
- <Tag B>
|
||||
---
|
||||
|
||||
# <Feature title>
|
||||
|
||||
<Opening paragraph: what changed for users and why it matters.>
|
||||
|
||||
<Optional section paragraph for key capability 1.>
|
||||
|
||||
<Optional section paragraph for key capability 2.>
|
||||
|
||||
## Improvements and fixes
|
||||
|
||||
- <Fix or optimization 1>
|
||||
- <Fix or optimization 2>
|
||||
```
|
||||
|
||||
## Docs Changelog Template (Chinese)
|
||||
|
||||
```md
|
||||
---
|
||||
title: <功能标题>
|
||||
description: <一句话说明>
|
||||
tags:
|
||||
- <标签 A>
|
||||
- <标签 B>
|
||||
---
|
||||
|
||||
# <功能标题>
|
||||
|
||||
<开场段:这次更新给用户带来的直接变化。>
|
||||
|
||||
<可选能力段 1。>
|
||||
|
||||
<可选能力段 2。>
|
||||
|
||||
## 体验优化与修复
|
||||
|
||||
- <优化或修复 1>
|
||||
- <优化或修复 2>
|
||||
```
|
||||
|
||||
## Quick Checklist
|
||||
|
||||
- [ ] File path matches `docs/changelog` naming convention
|
||||
- [ ] EN and ZH versions both exist and match in facts
|
||||
- [ ] Opening paragraph explains user-facing outcome
|
||||
- [ ] Main body is narrative-first, not bullet-only
|
||||
- [ ] `Improvements and fixes` section is concise and concrete
|
||||
- [ ] No fabricated claims or unsupported scope
|
||||
@@ -1,202 +0,0 @@
|
||||
---
|
||||
name: drizzle
|
||||
description: "Drizzle ORM schema authoring and query style for LobeHub (postgres, strict mode). Use when editing anything under `src/database/schemas/`, defining `pgTable` columns/indexes/junction tables, spreading `...timestamps`, generating `createInsertSchema`/`$inferSelect`/`$inferInsert` types, writing `db.select().from(...).leftJoin(...)` queries, or deciding when to split a relational `with:` into two queries. Triggers on `pgTable`, `db.select`, `db.query`, `eq()`/`and()`/`inArray()`, `uniqueIndex`, `primaryKey`, `references({ onDelete })`, 'add a column', 'new table', 'foreign key', 'junction table', 'schema field'. For migration files specifically, see the `db-migrations` skill."
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# Drizzle ORM Schema Style Guide
|
||||
|
||||
## Configuration
|
||||
|
||||
- Config: `drizzle.config.ts`
|
||||
- Schemas: `src/database/schemas/`
|
||||
- Migrations: `src/database/migrations/`
|
||||
- Dialect: `postgresql` with `strict: true`
|
||||
|
||||
## Helper Functions
|
||||
|
||||
Location: `src/database/schemas/_helpers.ts`
|
||||
|
||||
- `timestamptz(name)`: Timestamp with timezone
|
||||
- `createdAt()`, `updatedAt()`, `accessedAt()`: Standard timestamp columns
|
||||
- `timestamps`: Object with all three for easy spread
|
||||
|
||||
## Naming Conventions
|
||||
|
||||
- **Tables**: Plural snake_case (`users`, `session_groups`)
|
||||
- **Columns**: snake_case (`user_id`, `created_at`)
|
||||
|
||||
## Column Definitions
|
||||
|
||||
### Primary Keys
|
||||
|
||||
```typescript
|
||||
id: text('id')
|
||||
.primaryKey()
|
||||
.$defaultFn(() => idGenerator('agents'))
|
||||
.notNull(),
|
||||
```
|
||||
|
||||
ID prefixes make entity types distinguishable. For internal tables, use `uuid`.
|
||||
|
||||
### Foreign Keys
|
||||
|
||||
```typescript
|
||||
userId: text('user_id')
|
||||
.references(() => users.id, { onDelete: 'cascade' })
|
||||
.notNull(),
|
||||
```
|
||||
|
||||
### Timestamps
|
||||
|
||||
```typescript
|
||||
...timestamps, // Spread from _helpers.ts
|
||||
```
|
||||
|
||||
### Indexes
|
||||
|
||||
```typescript
|
||||
// Return array (object style deprecated)
|
||||
(t) => [uniqueIndex('client_id_user_id_unique').on(t.clientId, t.userId)],
|
||||
```
|
||||
|
||||
## Type Inference
|
||||
|
||||
```typescript
|
||||
export const insertAgentSchema = createInsertSchema(agents);
|
||||
export type NewAgent = typeof agents.$inferInsert;
|
||||
export type AgentItem = typeof agents.$inferSelect;
|
||||
```
|
||||
|
||||
## Example Pattern
|
||||
|
||||
```typescript
|
||||
export const agents = pgTable(
|
||||
'agents',
|
||||
{
|
||||
id: text('id')
|
||||
.primaryKey()
|
||||
.$defaultFn(() => idGenerator('agents'))
|
||||
.notNull(),
|
||||
slug: varchar('slug', { length: 100 })
|
||||
.$defaultFn(() => randomSlug(4))
|
||||
.unique(),
|
||||
userId: text('user_id')
|
||||
.references(() => users.id, { onDelete: 'cascade' })
|
||||
.notNull(),
|
||||
clientId: text('client_id'),
|
||||
chatConfig: jsonb('chat_config').$type<LobeAgentChatConfig>(),
|
||||
...timestamps,
|
||||
},
|
||||
(t) => [uniqueIndex('client_id_user_id_unique').on(t.clientId, t.userId)],
|
||||
);
|
||||
```
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Junction Tables (Many-to-Many)
|
||||
|
||||
```typescript
|
||||
export const agentsKnowledgeBases = pgTable(
|
||||
'agents_knowledge_bases',
|
||||
{
|
||||
agentId: text('agent_id')
|
||||
.references(() => agents.id, { onDelete: 'cascade' })
|
||||
.notNull(),
|
||||
knowledgeBaseId: text('knowledge_base_id')
|
||||
.references(() => knowledgeBases.id, { onDelete: 'cascade' })
|
||||
.notNull(),
|
||||
userId: text('user_id')
|
||||
.references(() => users.id, { onDelete: 'cascade' })
|
||||
.notNull(),
|
||||
enabled: boolean('enabled').default(true),
|
||||
...timestamps,
|
||||
},
|
||||
(t) => [primaryKey({ columns: [t.agentId, t.knowledgeBaseId] })],
|
||||
);
|
||||
```
|
||||
|
||||
## Query Style
|
||||
|
||||
**Always use `db.select()` builder API. Never use `db.query.*` relational API** (`findMany`, `findFirst`, `with:`).
|
||||
|
||||
The relational API generates complex lateral joins with `json_build_array` that are fragile and hard to debug.
|
||||
|
||||
### Select Single Row
|
||||
|
||||
```typescript
|
||||
// ✅ Good
|
||||
const [result] = await this.db.select().from(agents).where(eq(agents.id, id)).limit(1);
|
||||
return result;
|
||||
|
||||
// ❌ Bad: relational API
|
||||
return this.db.query.agents.findFirst({
|
||||
where: eq(agents.id, id),
|
||||
});
|
||||
```
|
||||
|
||||
### Select with JOIN
|
||||
|
||||
```typescript
|
||||
// ✅ Good: explicit select + leftJoin
|
||||
const rows = await this.db
|
||||
.select({
|
||||
runId: agentEvalRunTopics.runId,
|
||||
score: agentEvalRunTopics.score,
|
||||
testCase: agentEvalTestCases,
|
||||
topic: topics,
|
||||
})
|
||||
.from(agentEvalRunTopics)
|
||||
.leftJoin(agentEvalTestCases, eq(agentEvalRunTopics.testCaseId, agentEvalTestCases.id))
|
||||
.leftJoin(topics, eq(agentEvalRunTopics.topicId, topics.id))
|
||||
.where(eq(agentEvalRunTopics.runId, runId))
|
||||
.orderBy(asc(agentEvalRunTopics.createdAt));
|
||||
|
||||
// ❌ Bad: relational API with `with:`
|
||||
return this.db.query.agentEvalRunTopics.findMany({
|
||||
where: eq(agentEvalRunTopics.runId, runId),
|
||||
with: { testCase: true, topic: true },
|
||||
});
|
||||
```
|
||||
|
||||
### Select with Aggregation
|
||||
|
||||
```typescript
|
||||
// ✅ Good: select + leftJoin + groupBy
|
||||
const rows = await this.db
|
||||
.select({
|
||||
id: agentEvalDatasets.id,
|
||||
name: agentEvalDatasets.name,
|
||||
testCaseCount: count(agentEvalTestCases.id).as('testCaseCount'),
|
||||
})
|
||||
.from(agentEvalDatasets)
|
||||
.leftJoin(agentEvalTestCases, eq(agentEvalDatasets.id, agentEvalTestCases.datasetId))
|
||||
.groupBy(agentEvalDatasets.id);
|
||||
```
|
||||
|
||||
### One-to-Many (Separate Queries)
|
||||
|
||||
When you need a parent record with its children, use two queries instead of relational `with:`:
|
||||
|
||||
```typescript
|
||||
// ✅ Good: two simple queries
|
||||
const [dataset] = await this.db
|
||||
.select()
|
||||
.from(agentEvalDatasets)
|
||||
.where(eq(agentEvalDatasets.id, id))
|
||||
.limit(1);
|
||||
|
||||
if (!dataset) return undefined;
|
||||
|
||||
const testCases = await this.db
|
||||
.select()
|
||||
.from(agentEvalTestCases)
|
||||
.where(eq(agentEvalTestCases.datasetId, id))
|
||||
.orderBy(asc(agentEvalTestCases.sortOrder));
|
||||
|
||||
return { ...dataset, testCases };
|
||||
```
|
||||
|
||||
## Database Migrations
|
||||
|
||||
See the `db-migrations` skill for the detailed migration guide.
|
||||
@@ -1,83 +0,0 @@
|
||||
---
|
||||
name: heterogeneous-agent
|
||||
description: Guide for implementing and debugging LobeHub heterogeneous agent integrations such as Claude Code, Codex, and future external CLI agents. Use when working on adapter event mapping, Electron IPC transport, renderer persistence, tool-call chaining, subagent threads, resume/session handling, or regressions like mixed multi-tool messages, broken step boundaries, stuck tool loading, and orphan tool messages. Triggers on 'heterogeneous agent', 'hetero agent', '异构 agent', 'claude code adapter', 'codex adapter', 'external agent CLI', '孤立 tool 消息', 'raw Codex trace', or adapter/executor bugs.
|
||||
---
|
||||
|
||||
# Heterogeneous Agent Development
|
||||
|
||||
Use this skill when the bug or feature lives in the external CLI agent pipeline, not the normal server-side agent runtime.
|
||||
|
||||
## Use This Skill For
|
||||
|
||||
- Adding or changing a driver under `apps/desktop/src/main/modules/heterogeneousAgent/drivers/`
|
||||
- Editing an adapter under `packages/heterogeneous-agents/src/adapters/`
|
||||
- Debugging `heteroAgentRawLine` transport, `window.__HETERO_AGENT_TRACE`, or `executeHeterogeneousAgent`
|
||||
- Fixing Claude Code stream-json bugs such as duplicate partial/full chunks, broken `message.id` boundaries, missing `tool_result`, TodoWrite state drift, or subagent thread routing
|
||||
- Fixing Codex JSONL bugs such as mixed multi-tool messages, broken turn boundaries, or missing tool-result mapping
|
||||
- Fixing step-boundary, tool persistence, subagent thread, or resume bugs in Claude Code / Codex flows
|
||||
- Reproducing multi-tool mixing, orphan tool messages, or stuck tool-result loading
|
||||
|
||||
## Pipeline Map
|
||||
|
||||
1. CLI raw stdout / JSONL
|
||||
2. Electron main spawns the CLI and broadcasts `heteroAgentRawLine`
|
||||
3. Adapter maps raw provider events into `HeterogeneousAgentEvent`
|
||||
4. `executeHeterogeneousAgent` persists assistant/tool messages and forwards stream events
|
||||
5. `createGatewayEventHandler` hydrates the UI
|
||||
6. Only after this path looks correct should you move on to `agent-tracing` or context-engine debugging
|
||||
|
||||
## Read These Files First
|
||||
|
||||
- `apps/desktop/src/main/controllers/HeterogeneousAgentCtr.ts`
|
||||
- `apps/desktop/src/main/modules/heterogeneousAgent/drivers/claudeCode.ts`
|
||||
- `apps/desktop/src/main/modules/heterogeneousAgent/drivers/codex.ts`
|
||||
- `packages/heterogeneous-agents/src/adapters/claudeCode.ts`
|
||||
- `packages/heterogeneous-agents/src/adapters/codex.ts`
|
||||
- `src/store/chat/slices/aiChat/actions/heterogeneousAgentExecutor.ts`
|
||||
- `src/store/chat/slices/aiChat/actions/__tests__/heterogeneousAgentExecutor.test.ts`
|
||||
|
||||
## Default Debug Order
|
||||
|
||||
1. Prove whether the raw CLI output is correct before touching UI code.
|
||||
2. If raw output is correct, compare it with adapter output. In dev, `executeHeterogeneousAgent` exposes `window.__HETERO_AGENT_TRACE`.
|
||||
3. If adapted events look correct, inspect `persistToolBatch`, `persistToolResult`, step transitions, and subagent routing.
|
||||
4. Turn the repro into a focused test before fixing.
|
||||
5. Only after the transport/adapter/executor path looks sound should you debug later-stage message processing.
|
||||
|
||||
## Critical Invariants
|
||||
|
||||
- One raw tool item must map to one stable `ToolCallPayload.id`.
|
||||
- A new main-agent step must emit a boundary signal before events are forwarded to the new assistant.
|
||||
- In Claude Code, multiple assistant events with the same `message.id` are one turn, not multiple turns.
|
||||
- In Claude Code, `tool_result` lives in `type: 'user'` events, not assistant events.
|
||||
- In Claude Code partial mode, `message_delta.usage` is authoritative; do not trust echoed usage on every assistant block.
|
||||
- `persistToolBatch` must pre-register assistant `tools[]` before creating tool messages.
|
||||
- Every tool message must keep `parentId` equal to the owning assistant and `tool_call_id` equal to the tool id.
|
||||
- `tool_result` must resolve an existing `toolMsgIdByCallId`.
|
||||
- Subagent chunks must stay in thread scope and must not be forwarded into the main assistant stream.
|
||||
- Never clear the global `toolMsgIdByCallId` map at main step boundaries.
|
||||
|
||||
## Common Bug Patterns
|
||||
|
||||
- Claude Code duplicates text or thinking:
|
||||
check whether partial deltas and the later full assistant block are both being emitted.
|
||||
- Claude Code opens too many assistant messages:
|
||||
check whether the adapter is cutting steps on every assistant event instead of only on `message.id` changes.
|
||||
- Claude Code tool results never land:
|
||||
check whether `type: 'user'` `tool_result` blocks are being ignored because the code only inspects assistant events.
|
||||
- Claude Code TodoWrite cards look stale:
|
||||
check whether synthesized `pluginState.todos` is being attached at tool-result time.
|
||||
- Claude Code subagent transcript leaks into the main bubble:
|
||||
check `parent_tool_use_id` handling and whether subagent chunks are being forwarded to the main gateway handler.
|
||||
- Multiple Codex tools collapse into one assistant message:
|
||||
first check whether the adapter emits a usable step boundary such as `newStep` or an equivalent turn-change signal.
|
||||
- Orphan tool messages:
|
||||
first check step-transition ordering and whether `persistToolBatch` Phase 1 ran before tool message creation.
|
||||
- Tool bubble stays loading:
|
||||
look for `tool_result for unknown toolCallId` and missing `result_msg_id` backfill.
|
||||
- Subagent tools show up in the main bubble:
|
||||
check for subagent chunks reaching the main gateway handler.
|
||||
|
||||
## References
|
||||
|
||||
- For commands, trace capture, invariants, and focused test commands, read [references/debug-workflow.md](./references/debug-workflow.md).
|
||||
@@ -1,246 +0,0 @@
|
||||
# Heterogeneous Agent Debug Workflow
|
||||
|
||||
## Contents
|
||||
|
||||
1. Pipeline map
|
||||
2. Capture raw CLI traces first
|
||||
3. Compare raw and adapted events
|
||||
4. Check step boundaries before persistence
|
||||
5. Check tool persistence invariants
|
||||
6. Focused tests
|
||||
7. Repro-to-fix workflow
|
||||
|
||||
## 1. Pipeline Map
|
||||
|
||||
```
|
||||
CLI raw stdout
|
||||
-> HeterogeneousAgentCtr (Electron main)
|
||||
-> heteroAgentRawLine broadcast
|
||||
-> createAdapter(...)
|
||||
-> executeHeterogeneousAgent(...)
|
||||
-> persistToolBatch / persistToolResult
|
||||
-> createGatewayEventHandler(...)
|
||||
-> UI hydration
|
||||
```
|
||||
|
||||
Start at the leftmost broken layer. Do not jump straight to UI rendering unless raw and adapted events already look correct.
|
||||
|
||||
## 2. Capture Raw CLI Traces First
|
||||
|
||||
### Codex raw JSONL
|
||||
|
||||
Use a read-only prompt and save traces under the repo-local scratch directory `.heerogeneous-tracing/`.
|
||||
|
||||
```bash
|
||||
ts=$(date +%Y%m%d-%H%M%S)
|
||||
out=".heerogeneous-tracing/codex-${ts}.jsonl"
|
||||
last=".heerogeneous-tracing/codex-${ts}.last.txt"
|
||||
|
||||
cat << 'EOF' | codex exec --json --skip-git-repo-check --sandbox read-only -C "$PWD" -o "$last" - > "$out"
|
||||
You are being run only to collect a raw Codex JSON event trace.
|
||||
Do not modify any files.
|
||||
Use at least 4 separate shell tool invocations, one invocation per command.
|
||||
Run a short sequence of read-only repo checks and then reply with a one-sentence summary.
|
||||
EOF
|
||||
```
|
||||
|
||||
What to look for in the JSONL:
|
||||
|
||||
- `thread.started`
|
||||
- `turn.started`
|
||||
- `item.started` / `item.completed`
|
||||
- `item.type === 'command_execution'`
|
||||
- `item.type === 'agent_message'`
|
||||
- `turn.completed`
|
||||
|
||||
If raw Codex already merges tools into one item, the adapter is innocent. If raw Codex emits independent items but UI collapses them, the bug is downstream.
|
||||
|
||||
If the repo already contains useful traces under `.heerogeneous-tracing/`, inspect them before reproducing.
|
||||
|
||||
### Claude Code raw NDJSON
|
||||
|
||||
Mirror the arguments from `apps/desktop/src/main/modules/heterogeneousAgent/drivers/claudeCode.ts`.
|
||||
|
||||
- `-p`
|
||||
- `--input-format stream-json`
|
||||
- `--output-format stream-json`
|
||||
- `--verbose`
|
||||
- `--include-partial-messages`
|
||||
- `--permission-mode bypassPermissions`
|
||||
|
||||
You can capture a local raw trace like this:
|
||||
|
||||
```bash
|
||||
ts=$(date +%Y%m%d-%H%M%S)
|
||||
out=".heerogeneous-tracing/claude-${ts}.ndjson"
|
||||
|
||||
cat << 'EOF' | claude -p \
|
||||
--input-format stream-json \
|
||||
--output-format stream-json \
|
||||
--verbose \
|
||||
--include-partial-messages \
|
||||
--permission-mode bypassPermissions \
|
||||
> "$out"
|
||||
{"type":"user","message":{"role":"user","content":[{"type":"text","text":"Do a few read-only repo checks, use several tool calls, and then summarize briefly."}]}}
|
||||
EOF
|
||||
```
|
||||
|
||||
What to look for in Claude Code raw traces:
|
||||
|
||||
- `type: 'system', subtype: 'init'`
|
||||
- `type: 'assistant'` blocks for `thinking`, `tool_use`, and `text`
|
||||
- `type: 'user'` blocks containing `tool_result`
|
||||
- `type: 'stream_event'` with `message_start`, `content_block_delta`, and `message_delta`
|
||||
- `type: 'result'`
|
||||
- `type: 'rate_limit_event'`
|
||||
|
||||
Important Claude Code semantics:
|
||||
|
||||
- Each content block often arrives as its own assistant event.
|
||||
- Multiple assistant events can share the same `message.id`; that is still one turn.
|
||||
- `message.id` change is the main-step boundary.
|
||||
- Partial deltas arrive before the later full assistant block.
|
||||
- `message_delta.usage` is the authoritative per-turn usage.
|
||||
- Subagent events are tagged with `parent_tool_use_id`.
|
||||
|
||||
If the repo already contains useful references, inspect these first:
|
||||
|
||||
- `.heerogeneous-tracing/cc-monitor-real-trace.jsonl`
|
||||
- `.heerogeneous-tracing/cc-stream-chain-reference.md`
|
||||
|
||||
If you only need boundary semantics or tool persistence behavior, prefer existing adapter tests under:
|
||||
|
||||
- `packages/heterogeneous-agents/src/adapters/claudeCode.test.ts`
|
||||
- `packages/heterogeneous-agents/src/adapters/claudeCode.e2e.test.ts`
|
||||
|
||||
## 3. Compare Raw And Adapted Events
|
||||
|
||||
In dev builds, `executeHeterogeneousAgent` stores raw lines plus adapted events on:
|
||||
|
||||
- `window.__HETERO_AGENT_TRACE`
|
||||
|
||||
Use that trace to compare:
|
||||
|
||||
- raw `item.started` / `item.completed`
|
||||
- adapted `stream_chunk { chunkType: 'tools_calling' }`
|
||||
- adapted `tool_result`
|
||||
- adapted `tool_end`
|
||||
|
||||
For Codex, the usual mapping is:
|
||||
|
||||
- raw `item.started(command_execution)` -> `tools_calling` + `tool_start`
|
||||
- raw `item.completed(command_execution)` -> `tool_result` + `tool_end`
|
||||
- raw `item.completed(agent_message)` -> `stream_chunk(text)`
|
||||
|
||||
If the raw trace is right but adapted events are wrong, fix the adapter before touching persistence.
|
||||
|
||||
## 4. Check Step Boundaries Before Persistence
|
||||
|
||||
This is the first thing to verify for "mixed tools in one assistant" bugs.
|
||||
|
||||
### Claude Code
|
||||
|
||||
Claude Code step boundaries are keyed off assistant `message.id` changes. The adapter should emit:
|
||||
|
||||
- `stream_end`
|
||||
- `stream_start { newStep: true }`
|
||||
|
||||
Also verify these Claude-specific invariants:
|
||||
|
||||
- the first assistant after init does not open a new step
|
||||
- repeated assistant events with the same `message.id` do not open a new step
|
||||
- partial `content_block_delta` text/thinking does not get duplicated by the later full assistant event
|
||||
- `tool_result` from `type: 'user'` updates the matching tool row
|
||||
- `parent_tool_use_id` creates thread-scoped subagent chunks instead of main-stream chunks
|
||||
- TodoWrite `tool_use.input` is converted into synthesized `pluginState.todos` on `tool_result`
|
||||
|
||||
Good references:
|
||||
|
||||
- `packages/heterogeneous-agents/src/adapters/claudeCode.ts`
|
||||
- `packages/heterogeneous-agents/src/adapters/claudeCode.test.ts`
|
||||
|
||||
### Codex
|
||||
|
||||
Codex raw traces usually provide turn-level boundaries through:
|
||||
|
||||
- `turn.started`
|
||||
- `turn.completed`
|
||||
|
||||
The executor only cuts a new assistant message when it receives a step-boundary signal it understands. If the adapter emits `stream_start` without `newStep`, multiple Codex tools and text chunks can accumulate under the same assistant longer than intended.
|
||||
|
||||
Relevant files:
|
||||
|
||||
- `packages/heterogeneous-agents/src/adapters/codex.ts`
|
||||
- `src/store/chat/slices/aiChat/actions/heterogeneousAgentExecutor.ts`
|
||||
|
||||
## 5. Check Tool Persistence Invariants
|
||||
|
||||
Read `persistToolBatch` and `persistToolResult` before changing UI code.
|
||||
|
||||
### `persistToolBatch`
|
||||
|
||||
The expected order is:
|
||||
|
||||
1. Pre-register assistant `tools[]`
|
||||
2. Create `role: 'tool'` messages
|
||||
3. Backfill `result_msg_id` onto assistant `tools[]`
|
||||
|
||||
If tool rows are created before assistant `tools[]` are registered, orphan tool messages are likely.
|
||||
|
||||
### `persistToolResult`
|
||||
|
||||
`tool_result` must resolve the tool row through `toolMsgIdByCallId`.
|
||||
|
||||
Warning signs:
|
||||
|
||||
- `tool_result for unknown toolCallId`
|
||||
- tool rows with empty content forever
|
||||
- missing `result_msg_id`
|
||||
|
||||
For Claude Code, remember that tool results originate from raw `type: 'user'` events.
|
||||
|
||||
### Main vs subagent scope
|
||||
|
||||
- Main-agent tool state is per-step.
|
||||
- `toolMsgIdByCallId` is global across main and subagent scopes.
|
||||
- Subagent chunks must not be forwarded into the main gateway handler.
|
||||
|
||||
If subagent events leak to the main handler, the main bubble can inherit the wrong `tools[]` and content.
|
||||
|
||||
## 6. Focused Tests
|
||||
|
||||
Run the smallest useful test set first.
|
||||
|
||||
```bash
|
||||
bunx vitest run --silent='passed-only' 'packages/heterogeneous-agents/src/adapters/codex.test.ts'
|
||||
bunx vitest run --silent='passed-only' 'packages/heterogeneous-agents/src/adapters/claudeCode.test.ts'
|
||||
bunx vitest run --silent='passed-only' 'src/store/chat/slices/aiChat/actions/__tests__/heterogeneousAgentExecutor.test.ts'
|
||||
```
|
||||
|
||||
Especially useful places:
|
||||
|
||||
- `packages/heterogeneous-agents/src/adapters/codex.test.ts`
|
||||
- `packages/heterogeneous-agents/src/adapters/claudeCode.test.ts`
|
||||
- `src/store/chat/slices/aiChat/actions/__tests__/heterogeneousAgentExecutor.test.ts`
|
||||
|
||||
Claude Code-specific assertions worth adding when fixing bugs:
|
||||
|
||||
- same `message.id` does not emit `newStep`
|
||||
- changed `message.id` does emit `stream_end` plus `stream_start { newStep: true }`
|
||||
- partial text/thinking is emitted once
|
||||
- `tool_result` from `user` events reaches the right tool row
|
||||
- subagent chunks carry `subagent.parentToolCallId`
|
||||
- TodoWrite result synthesizes `pluginState.todos`
|
||||
|
||||
When the bug comes from a real trace, distill it into the closest existing test file instead of relying on manual UI-only repros.
|
||||
|
||||
## 7. Repro-To-Fix Workflow
|
||||
|
||||
1. Capture a raw trace and save it under `.heerogeneous-tracing/`.
|
||||
2. Confirm whether the bug appears in raw events, adapted events, or persistence.
|
||||
3. Add or update the narrowest failing test near the broken layer.
|
||||
4. Fix the smallest layer that can explain the symptom.
|
||||
5. Re-run focused tests.
|
||||
6. Only then do an Electron smoke test with the `local-testing` skill if UI confirmation is still needed.
|
||||
|
||||
Do not start with a broad Electron repro if a raw trace or adapter test can prove the fault zone faster.
|
||||
@@ -1,91 +0,0 @@
|
||||
---
|
||||
name: hotkey
|
||||
description: "Adding or editing keyboard shortcuts in LobeHub. Use when registering a new hotkey, changing a key combo, scoping a shortcut to chat vs global, or wiring a hotkey hook + tooltip. Covers the 5-step flow: add to `HotkeyEnum` in `src/types/hotkey.ts`, register in `HOTKEYS_REGISTRATION` (`src/const/hotkeys.ts`) with `combineKeys([Key.Mod, …])`, add i18n in `src/locales/default/hotkey.ts`, expose via `useHotkeyById` in `src/hooks/useHotkeys/`, and render `<Tooltip hotkey={…}>`. Triggers on `HotkeyEnum`, `HOTKEYS_REGISTRATION`, `useHotkeyById`, `combineKeys`, `Key.Mod`/`Key.Shift`, 'add a hotkey', 'add a shortcut', '加快捷键', '快捷键', 'Cmd+K', 'keyboard shortcut', 'hotkey scope', 'hotkey conflict'."
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# Adding Keyboard Shortcuts Guide
|
||||
|
||||
## Steps to Add a New Hotkey
|
||||
|
||||
### 1. Update Hotkey Constant
|
||||
|
||||
In `src/types/hotkey.ts`:
|
||||
|
||||
```typescript
|
||||
export const HotkeyEnum = {
|
||||
// existing...
|
||||
ClearChat: 'clearChat', // Add new
|
||||
} as const;
|
||||
```
|
||||
|
||||
### 2. Register Default Hotkey
|
||||
|
||||
In `src/const/hotkeys.ts`:
|
||||
|
||||
```typescript
|
||||
import { KeyMapEnum as Key, combineKeys } from '@lobehub/ui';
|
||||
|
||||
export const HOTKEYS_REGISTRATION: HotkeyRegistration = [
|
||||
{
|
||||
group: HotkeyGroupEnum.Conversation,
|
||||
id: HotkeyEnum.ClearChat,
|
||||
keys: combineKeys([Key.Mod, Key.Shift, Key.Backspace]),
|
||||
scopes: [HotkeyScopeEnum.Chat],
|
||||
},
|
||||
];
|
||||
```
|
||||
|
||||
### 3. Add i18n Translation
|
||||
|
||||
In `src/locales/default/hotkey.ts`:
|
||||
|
||||
```typescript
|
||||
const hotkey: HotkeyI18nTranslations = {
|
||||
clearChat: {
|
||||
desc: '清空当前会话的所有消息记录',
|
||||
title: '清空聊天记录',
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### 4. Create and Register Hook
|
||||
|
||||
In `src/hooks/useHotkeys/chatScope.ts`:
|
||||
|
||||
```typescript
|
||||
export const useClearChatHotkey = () => {
|
||||
const clearMessages = useChatStore((s) => s.clearMessages);
|
||||
return useHotkeyById(HotkeyEnum.ClearChat, clearMessages);
|
||||
};
|
||||
|
||||
export const useRegisterChatHotkeys = () => {
|
||||
useClearChatHotkey();
|
||||
// ...other hotkeys
|
||||
};
|
||||
```
|
||||
|
||||
### 5. Add Tooltip (Optional)
|
||||
|
||||
```tsx
|
||||
const clearChatHotkey = useUserStore(settingsSelectors.getHotkeyById(HotkeyEnum.ClearChat));
|
||||
|
||||
<Tooltip hotkey={clearChatHotkey} title={t('clearChat.title', { ns: 'hotkey' })}>
|
||||
<Button icon={<DeleteOutlined />} onClick={clearMessages} />
|
||||
</Tooltip>;
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Scope**: Choose global or chat scope based on functionality
|
||||
2. **Grouping**: Place in appropriate group (System/Layout/Conversation)
|
||||
3. **Conflict check**: Ensure no conflict with system/browser shortcuts
|
||||
4. **Platform**: Use `Key.Mod` instead of hardcoded `Ctrl` or `Cmd`
|
||||
5. **Clear description**: Provide title and description for users
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
- **Not working**: Check scope and RegisterHotkeys hook
|
||||
- **Not in settings**: Verify HOTKEYS_REGISTRATION config
|
||||
- **Conflict**: HotkeyInput component shows warnings
|
||||
- **Page-specific**: Ensure correct scope activation
|
||||
@@ -1,78 +0,0 @@
|
||||
---
|
||||
name: i18n
|
||||
description: "LobeHub internationalization with react-i18next. Use when adding any user-facing string in `.tsx`/`.ts` files, creating or renaming a key under `src/locales/default/{namespace}.ts`, deciding the `{feature}.{context}.{action}` flat-key pattern, wiring a new namespace into `src/locales/default/index.ts`, or translating zh-CN/en-US JSON for dev preview. Triggers on `useTranslation`, `t('foo.bar')`, `i18next.t`, `{{variable}}` interpolation, hardcoded UI strings (zh or en) that should be extracted, 'add i18n', '加 i18n key', '翻译', 'locale key', 'namespace', 'pnpm i18n'."
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# LobeHub Internationalization Guide
|
||||
|
||||
- Default language: English (en-US)
|
||||
- Framework: react-i18next
|
||||
- **Only edit files in `src/locales/default/`** - Never edit JSON files in `locales/`
|
||||
- Run `pnpm i18n` to generate translations (or manually translate zh-CN/en-US for dev preview)
|
||||
|
||||
## Key Naming Convention
|
||||
|
||||
**Flat keys with dot notation** (not nested objects):
|
||||
|
||||
```typescript
|
||||
// ✅ Correct
|
||||
export default {
|
||||
'alert.cloud.action': '立即体验',
|
||||
'sync.actions.sync': '立即同步',
|
||||
'sync.status.ready': '已连接',
|
||||
};
|
||||
|
||||
// ❌ Avoid nested objects
|
||||
export default {
|
||||
alert: { cloud: { action: '...' } },
|
||||
};
|
||||
```
|
||||
|
||||
**Patterns:** `{feature}.{context}.{action|status}`
|
||||
|
||||
**Parameters:** Use `{{variableName}}` syntax
|
||||
|
||||
```typescript
|
||||
'alert.cloud.desc': '我们提供 {{credit}} 额度积分',
|
||||
```
|
||||
|
||||
**Avoid key conflicts:**
|
||||
|
||||
```typescript
|
||||
// ❌ Conflict
|
||||
'clientDB.solve': '自助解决',
|
||||
'clientDB.solve.backup.title': '数据备份',
|
||||
|
||||
// ✅ Solution
|
||||
'clientDB.solve.action': '自助解决',
|
||||
'clientDB.solve.backup.title': '数据备份',
|
||||
```
|
||||
|
||||
## Workflow
|
||||
|
||||
1. Add keys to `src/locales/default/{namespace}.ts`
|
||||
2. Export new namespace in `src/locales/default/index.ts`
|
||||
3. For dev preview: manually translate `locales/zh-CN/{namespace}.json` and `locales/en-US/{namespace}.json`
|
||||
4. Remind the user to run `pnpm i18n` before creating PR — do NOT run it yourself (very slow)
|
||||
|
||||
## Usage
|
||||
|
||||
```tsx
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
const { t } = useTranslation('common');
|
||||
|
||||
t('newFeature.title');
|
||||
t('alert.cloud.desc', { credit: '1000' });
|
||||
|
||||
// Multiple namespaces
|
||||
const { t } = useTranslation(['common', 'chat']);
|
||||
t('common:save');
|
||||
```
|
||||
|
||||
## Common Namespaces
|
||||
|
||||
**Most used:** `common` (shared UI), `chat` (chat features), `setting` (settings)
|
||||
|
||||
Others: auth, changelog, components, discover, editor, electron, error, file, hotkey, knowledgeBase, memory, models, plugin, portal, providers, tool, topic
|
||||
@@ -1,143 +0,0 @@
|
||||
---
|
||||
name: linear
|
||||
description: "Linear issue management. Use when the user mentions LOBE-xxx issue IDs (e.g. LOBE-4540), says 'linear' / 'linear issue' / 'link linear', or when creating PRs that reference Linear issues. Covers retrieving issues, updating status, adding completion comments, and creating sub-issue trees."
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# Linear Issue Management
|
||||
|
||||
Before using Linear workflows, search for `linear` MCP tools. If not found, treat as not installed.
|
||||
|
||||
## PR Creation with Linear Issues
|
||||
|
||||
A PR that fixes a Linear issue has **two separate jobs to do**, and both matter:
|
||||
|
||||
1. **`Fixes LOBE-xxx` in the PR body** — Linear watches GitHub for these magic keywords and auto-links the PR and auto-closes the issue on merge. This is the machine-readable side.
|
||||
2. **A completion comment on the Linear issue** — gives the reviewer/PM/teammate landing in Linear a human-readable summary of what changed and why, without forcing them to click through to GitHub and read a diff.
|
||||
|
||||
If you only do step 1, Linear watchers (often non-engineers) hit the issue and see no context. So pair PR creation with the Linear comment as part of the same task — finish both before considering the work done.
|
||||
|
||||
## Workflow
|
||||
|
||||
1. **Retrieve issue details** before starting: `mcp__linear-server__get_issue`
|
||||
2. **Read images** — issue descriptions often contain screenshots with critical context (mockups, error states, before/after). Use `mcp__linear-server__extract_images` so you actually see them; reading raw markdown alone misses what the reporter was looking at.
|
||||
3. **Check for sub-issues**: `mcp__linear-server__list_issues` with `parentId` filter
|
||||
4. **Mark as In Progress** at the moment you start planning or implementing — this signals to teammates the issue is owned, so they don't double-pick it up.
|
||||
5. **Update issue status** when completing: `mcp__linear-server__update_issue`
|
||||
6. **Add completion comment** (see [format below](#completion-comment-format))
|
||||
|
||||
## Creating Issues
|
||||
|
||||
When creating issues with `mcp__linear-server__create_issue`, add the `claude code` label. Reason: the label is how the team filters/audits AI-generated issues; without it those issues vanish into the general backlog and the team loses visibility into AI contribution patterns.
|
||||
|
||||
## Language
|
||||
|
||||
Match the issue language to the conversation that produced it — if you're discussing in 中文,write the issue in 中文;if discussing in English, write it in English. Reason: the issue is a continuation of the conversation, and forcing a language switch creates translation friction for the collaborator who started the thread.
|
||||
|
||||
Specifics:
|
||||
|
||||
- 中文 conversation → 中文 body; technical terms (file paths, identifiers, library names, commands, error messages) stay in English.
|
||||
- English conversation → English body.
|
||||
- Code blocks, file paths, and quoted strings always stay in their original form regardless of surrounding language.
|
||||
- This applies equally to **updates** — when editing an existing issue (description **and titles**), preserve the language of the conversation that triggered the edit; don't switch the issue language mid-refactor.
|
||||
|
||||
## Creating Sub-issue Trees
|
||||
|
||||
When breaking a parent issue into a tree of sub-issues (e.g., task decomposition for LOBE-xxx), follow these rules — they work around real limitations of the Linear MCP tools.
|
||||
|
||||
### 1. Prefix titles with an ordering index
|
||||
|
||||
The Linear Sub-issues panel orders children by `sortOrder`, which **defaults to newest-first** (most recently created appears on top). Neither parallel nor serial creation produces the intended top-to-bottom reading order, and the MCP `save_issue` tool does **not expose a `sortOrder` parameter** — you can't set order at create time.
|
||||
|
||||
Workaround: encode execution order in the title itself:
|
||||
|
||||
```plaintext
|
||||
[1] [db] add schema fields
|
||||
[2] [db] new table + repository
|
||||
[3] [service] business logic layer
|
||||
[4] [api] REST endpoints
|
||||
[4.1] [sdk] client SDK wrapper
|
||||
[4.1.1] [app] consumer integration
|
||||
[4.1.2] [app] UI surface
|
||||
[4.2] [ui] dashboard page
|
||||
```
|
||||
|
||||
Even when the panel shuffles, the reader can mentally reconstruct the dependency graph at a glance. Dotted numbering `[n.m.k]` should mirror the parent-child nesting so the index and the tree agree.
|
||||
|
||||
### 2. Nest sub-issues by logical parent-child, not flat under the root
|
||||
|
||||
Linear supports **unlimited sub-issue depth**. A flat list of 8+ siblings under one root is hard to scan. Group by main-subordinate logic:
|
||||
|
||||
- Core service → its SDK → SDK consumers
|
||||
- Don't create a sibling when a child is more accurate
|
||||
|
||||
Use `parentId: "LOBE-xxxx"` at creation (or `save_issue` to move). Moving an issue's parent does not disturb its `blockedBy` relations.
|
||||
|
||||
### 3. Sub-issue creation order is dictated by `blockedBy`
|
||||
|
||||
`blockedBy` requires the blocker to exist first (you need its LOBE-id). So:
|
||||
|
||||
1. **Topologically sort** the DAG — leaves (no deps) first, roots last
|
||||
2. Create issues with zero deps in the first wave
|
||||
3. Create dependent issues only after collecting the blocker IDs from prior responses
|
||||
4. `blockedBy` is **append-only**; passing it again does not overwrite — safe to re-run
|
||||
|
||||
### 4. Don't waste rounds trying to parallelize
|
||||
|
||||
MCP tool calls in a single message look parallel but execute sequentially on the server, and you still need blocker IDs from earlier responses. Just issue calls in dependency order; optimizing for parallelism gains nothing here.
|
||||
|
||||
### 5. Keep each sub-issue description self-contained
|
||||
|
||||
Each sub-issue should state:
|
||||
|
||||
- Goal (1–2 lines)
|
||||
- Key files to touch
|
||||
- Concrete changes / acceptance criteria
|
||||
- Dependencies (link to blocker issues by `LOBE-xxxx`)
|
||||
- Validation steps
|
||||
|
||||
The implementer may open only the sub-issue, not the parent — don't rely on context that lives only in the parent description.
|
||||
|
||||
## Completion Comment Format
|
||||
|
||||
Each completed issue gets a comment summarizing the work, so reviewers and future readers don't have to reconstruct it from the PR diff:
|
||||
|
||||
```markdown
|
||||
## Changes Summary
|
||||
|
||||
- **Feature**: Brief description of what was implemented
|
||||
- **Files Changed**: List key files modified
|
||||
- **PR**: #xxx or PR URL
|
||||
|
||||
### Key Changes
|
||||
|
||||
- Change 1
|
||||
- Change 2
|
||||
- ...
|
||||
```
|
||||
|
||||
This gives team visibility, code-review context, and a paper trail for future reference.
|
||||
|
||||
## PR Association
|
||||
|
||||
When creating PRs for Linear issues, include magic keywords in the PR body:
|
||||
|
||||
- `Fixes LOBE-123`
|
||||
- `Closes LOBE-123`
|
||||
- `Resolves LOBE-123`
|
||||
|
||||
These trigger Linear's auto-link + auto-close on merge.
|
||||
|
||||
## Per-Issue Completion Rule
|
||||
|
||||
When working on multiple issues, close out **each one before starting the next** — don't batch all the Linear updates to the end. Batching is where comments get forgotten and issues stay stuck in "In Progress" days after the PR shipped.
|
||||
|
||||
For each issue:
|
||||
|
||||
1. Complete implementation
|
||||
2. Run `bun run type-check`
|
||||
3. Run related tests
|
||||
4. Create PR if needed
|
||||
5. Update status to **"In Review"** (not "Done" — "Done" is for after the PR merges)
|
||||
6. Add the completion comment
|
||||
7. Move to the next issue
|
||||
@@ -1,520 +0,0 @@
|
||||
---
|
||||
name: local-testing
|
||||
description: >
|
||||
Local app and bot testing. Uses agent-browser CLI for Electron/web app UI testing,
|
||||
and osascript (AppleScript) for controlling native macOS apps (WeChat, Discord, Telegram, Slack, Lark/飞书, QQ)
|
||||
to test bots. Triggers on 'local test', 'test in electron', 'test desktop', 'test bot',
|
||||
'bot test', 'test in discord', 'test in telegram', 'test in slack', 'test in weixin',
|
||||
'test in wechat', 'test in lark', 'test in feishu', 'test in qq',
|
||||
'manual test', 'osascript', or UI/bot verification tasks.
|
||||
---
|
||||
|
||||
# Local App & Bot Testing
|
||||
|
||||
Two approaches for local testing on macOS:
|
||||
|
||||
| Approach | Tool | Best For |
|
||||
| --------------------------- | ------------------- | ---------------------------------------------------- |
|
||||
| **agent-browser + CDP** | `agent-browser` CLI | Electron apps, web apps (DOM access, JS eval) |
|
||||
| **osascript (AppleScript)** | `osascript -e` | Native macOS apps (WeChat, Discord, Telegram, Slack) |
|
||||
|
||||
---
|
||||
|
||||
# Part 1: agent-browser (Electron / Web Apps)
|
||||
|
||||
Use `agent-browser` to automate Chromium-based apps via Chrome DevTools Protocol.
|
||||
|
||||
Install via `npm i -g agent-browser`, `brew install agent-browser`, or `cargo install agent-browser`. Run `agent-browser install` to download Chrome. Run `agent-browser upgrade` to update.
|
||||
|
||||
## Core Workflow
|
||||
|
||||
Every browser automation follows this pattern:
|
||||
|
||||
1. **Navigate**: `agent-browser open <url>`
|
||||
2. **Snapshot**: `agent-browser snapshot -i` (get element refs like `@e1`, `@e2`)
|
||||
3. **Interact**: Use refs to click, fill, select
|
||||
4. **Re-snapshot**: After navigation or DOM changes, get fresh refs
|
||||
|
||||
```bash
|
||||
agent-browser open https://example.com/form
|
||||
agent-browser snapshot -i
|
||||
# Output: @e1 [input type="email"], @e2 [input type="password"], @e3 [button] "Submit"
|
||||
|
||||
agent-browser fill @e1 "user@example.com"
|
||||
agent-browser fill @e2 "password123"
|
||||
agent-browser click @e3
|
||||
agent-browser wait --load networkidle
|
||||
agent-browser snapshot -i # Check result
|
||||
```
|
||||
|
||||
## Command Chaining
|
||||
|
||||
```bash
|
||||
# Chain open + wait + snapshot in one call
|
||||
agent-browser open https://example.com && agent-browser wait --load networkidle && agent-browser snapshot -i
|
||||
```
|
||||
|
||||
Use `&&` when you don't need to read intermediate output. Run commands separately when you need to parse output first (e.g., snapshot to discover refs, then interact).
|
||||
|
||||
## Essential Commands
|
||||
|
||||
```bash
|
||||
# Navigation
|
||||
agent-browser open <url> # Navigate (aliases: goto, navigate)
|
||||
agent-browser close # Close browser
|
||||
agent-browser close --all # Close all active sessions
|
||||
|
||||
# Snapshot
|
||||
agent-browser snapshot -i # Interactive elements with refs (recommended)
|
||||
agent-browser snapshot -s "#selector" # Scope to CSS selector
|
||||
|
||||
# Interaction (use @refs from snapshot)
|
||||
agent-browser click @e1 # Click element
|
||||
agent-browser click @e1 --new-tab # Click and open in new tab
|
||||
agent-browser fill @e2 "text" # Clear and type text
|
||||
agent-browser type @e2 "text" # Type without clearing
|
||||
agent-browser select @e1 "option" # Select dropdown option
|
||||
agent-browser check @e1 # Check checkbox
|
||||
agent-browser press Enter # Press key
|
||||
agent-browser keyboard type "text" # Type at current focus (no selector)
|
||||
agent-browser keyboard inserttext "text" # Insert without key events
|
||||
agent-browser scroll down 500 # Scroll page
|
||||
agent-browser scroll down 500 --selector "div.content" # Scroll within container
|
||||
|
||||
# Get information
|
||||
agent-browser get text @e1 # Get element text
|
||||
agent-browser get url # Get current URL
|
||||
agent-browser get title # Get page title
|
||||
agent-browser get cdp-url # Get CDP WebSocket URL
|
||||
|
||||
# Wait
|
||||
agent-browser wait @e1 # Wait for element
|
||||
agent-browser wait --load networkidle # Wait for network idle
|
||||
agent-browser wait --url "**/page" # Wait for URL pattern
|
||||
agent-browser wait 2000 # Wait milliseconds
|
||||
agent-browser wait --text "Welcome" # Wait for text to appear
|
||||
agent-browser wait --fn "!document.body.innerText.includes('Loading...')" # Wait for text to disappear
|
||||
agent-browser wait "#spinner" --state hidden # Wait for element to disappear
|
||||
|
||||
# Downloads
|
||||
agent-browser download @e1 ./file.pdf # Click element to trigger download
|
||||
agent-browser wait --download ./output.zip # Wait for any download to complete
|
||||
|
||||
# Network
|
||||
agent-browser network requests # Inspect tracked requests
|
||||
agent-browser network requests --type xhr,fetch # Filter by resource type
|
||||
agent-browser network requests --method POST # Filter by HTTP method
|
||||
agent-browser network route "**/api/*" --abort # Block matching requests
|
||||
agent-browser network har start # Start HAR recording
|
||||
agent-browser network har stop ./capture.har # Stop and save HAR file
|
||||
|
||||
# Viewport & Device Emulation
|
||||
agent-browser set viewport 1920 1080 # Set viewport size (default: 1280x720)
|
||||
agent-browser set viewport 1920 1080 2 # 2x retina
|
||||
agent-browser set device "iPhone 14" # Emulate device (viewport + user agent)
|
||||
|
||||
# Capture
|
||||
agent-browser screenshot # Screenshot to temp dir
|
||||
agent-browser screenshot --full # Full page screenshot
|
||||
agent-browser screenshot --annotate # Annotated screenshot with numbered element labels
|
||||
agent-browser pdf output.pdf # Save as PDF
|
||||
|
||||
# Clipboard
|
||||
agent-browser clipboard read # Read text from clipboard
|
||||
agent-browser clipboard write "text" # Write text to clipboard
|
||||
agent-browser clipboard copy # Copy current selection
|
||||
agent-browser clipboard paste # Paste from clipboard
|
||||
|
||||
# Dialogs (alert, confirm, prompt, beforeunload)
|
||||
agent-browser dialog accept # Accept dialog
|
||||
agent-browser dialog accept "input" # Accept prompt dialog with text
|
||||
agent-browser dialog dismiss # Dismiss/cancel dialog
|
||||
agent-browser dialog status # Check if dialog is open
|
||||
|
||||
# Diff (compare page states)
|
||||
agent-browser diff snapshot # Compare current vs last snapshot
|
||||
agent-browser diff screenshot --baseline before.png # Visual pixel diff
|
||||
agent-browser diff url <url1> <url2> # Compare two pages
|
||||
|
||||
# Streaming
|
||||
agent-browser stream enable # Start WebSocket streaming
|
||||
agent-browser stream status # Inspect streaming state
|
||||
agent-browser stream disable # Stop streaming
|
||||
```
|
||||
|
||||
## Batch Execution
|
||||
|
||||
```bash
|
||||
echo '[
|
||||
["open", "https://example.com"],
|
||||
["snapshot", "-i"],
|
||||
["click", "@e1"],
|
||||
["screenshot", "result.png"]
|
||||
]' | agent-browser batch --json
|
||||
```
|
||||
|
||||
## Authentication
|
||||
|
||||
```bash
|
||||
# Option 1: Auth vault (credentials stored encrypted)
|
||||
echo "$PASSWORD" | agent-browser auth save myapp --url https://app.example.com/login --username user --password-stdin
|
||||
agent-browser auth login myapp
|
||||
|
||||
# Option 2: Session name (auto-save/restore cookies + localStorage)
|
||||
agent-browser --session-name myapp open https://app.example.com/login
|
||||
agent-browser close # State auto-saved
|
||||
agent-browser --session-name myapp open https://app.example.com/dashboard # Auto-restored
|
||||
|
||||
# Option 3: Persistent profile
|
||||
agent-browser --profile ~/.myapp open https://app.example.com/login
|
||||
|
||||
# Option 4: State file
|
||||
agent-browser state save auth.json
|
||||
agent-browser state load auth.json
|
||||
```
|
||||
|
||||
### LobeHub dev server — inject better-auth cookie
|
||||
|
||||
`agent-browser --headed` on macOS can create an off-screen Chromium window, blocking manual login. For a local LobeHub dev server (e.g. `localhost:3011`), copy the `better-auth.session_token` cookie out of a **Network request** in the user's own Chrome DevTools and load it via `state load`. See [references/agent-browser-login.md](./references/agent-browser-login.md) for the full recipe.
|
||||
|
||||
## Semantic Locators (Alternative to Refs)
|
||||
|
||||
```bash
|
||||
agent-browser find text "Sign In" click
|
||||
agent-browser find label "Email" fill "user@test.com"
|
||||
agent-browser find role button click --name "Submit"
|
||||
agent-browser find placeholder "Search" type "query"
|
||||
agent-browser find testid "submit-btn" click
|
||||
```
|
||||
|
||||
## JavaScript Evaluation (eval)
|
||||
|
||||
```bash
|
||||
# Simple expressions
|
||||
agent-browser eval 'document.title'
|
||||
|
||||
# Complex JS: use --stdin with heredoc (RECOMMENDED)
|
||||
agent-browser eval --stdin << 'EVALEOF'
|
||||
JSON.stringify(
|
||||
Array.from(document.querySelectorAll("img"))
|
||||
.filter(i => !i.alt)
|
||||
.map(i => ({ src: i.src.split("/").pop(), width: i.width }))
|
||||
)
|
||||
EVALEOF
|
||||
|
||||
# Base64 encoding (avoids all shell escaping issues)
|
||||
agent-browser eval -b "$(echo -n 'document.title' | base64)"
|
||||
```
|
||||
|
||||
## Ref Lifecycle
|
||||
|
||||
Refs (`@e1`, `@e2`, etc.) are invalidated when the page changes. Always re-snapshot after clicking links/buttons that navigate, form submissions, or dynamic content loading.
|
||||
|
||||
## Annotated Screenshots (Vision Mode)
|
||||
|
||||
```bash
|
||||
agent-browser screenshot --annotate
|
||||
# Output includes the image path and a legend:
|
||||
# [1] @e1 button "Submit"
|
||||
# [2] @e2 link "Home"
|
||||
agent-browser click @e2 # Click using ref from annotated screenshot
|
||||
```
|
||||
|
||||
## Parallel Sessions
|
||||
|
||||
```bash
|
||||
agent-browser --session site1 open https://site-a.com
|
||||
agent-browser --session site2 open https://site-b.com
|
||||
agent-browser session list
|
||||
```
|
||||
|
||||
## Connect to Existing Chrome
|
||||
|
||||
```bash
|
||||
agent-browser --auto-connect snapshot # Auto-discover running Chrome
|
||||
agent-browser --cdp 9222 snapshot # Explicit CDP port
|
||||
```
|
||||
|
||||
## iOS Simulator (Mobile Safari)
|
||||
|
||||
```bash
|
||||
agent-browser device list
|
||||
agent-browser -p ios --device "iPhone 16 Pro" open https://example.com
|
||||
agent-browser -p ios snapshot -i
|
||||
agent-browser -p ios tap @e1
|
||||
agent-browser -p ios swipe up
|
||||
agent-browser -p ios screenshot mobile.png
|
||||
agent-browser -p ios close
|
||||
```
|
||||
|
||||
## Observability Dashboard
|
||||
|
||||
```bash
|
||||
agent-browser dashboard install
|
||||
agent-browser dashboard start # Background server on port 4848
|
||||
agent-browser dashboard stop
|
||||
```
|
||||
|
||||
## Cloud Providers
|
||||
|
||||
Use `-p <provider>` to run against cloud browsers: `agentcore`, `browserbase`, `browserless`, `browseruse`, `kernel`.
|
||||
|
||||
## Browser Engine Selection
|
||||
|
||||
```bash
|
||||
agent-browser --engine lightpanda open example.com # 10x faster, 10x less memory
|
||||
```
|
||||
|
||||
## Electron (LobeHub Desktop)
|
||||
|
||||
### Setup / Teardown
|
||||
|
||||
Use the `electron-dev.sh` script to manage the Electron dev environment. It handles process lifecycle, waits for SPA readiness, and reliably kills all child processes (main + helpers + vite).
|
||||
|
||||
```bash
|
||||
SCRIPT=".agents/skills/local-testing/scripts/electron-dev.sh"
|
||||
|
||||
# Start Electron dev with CDP (idempotent — skips if already running)
|
||||
$SCRIPT start
|
||||
|
||||
# Check if Electron is running and CDP is reachable
|
||||
$SCRIPT status
|
||||
|
||||
# Kill all Electron-related processes (main + helper + vite)
|
||||
$SCRIPT stop
|
||||
|
||||
# Force fresh restart
|
||||
$SCRIPT restart
|
||||
```
|
||||
|
||||
After `start` succeeds, connect with: `agent-browser --cdp 9222 snapshot -i`
|
||||
|
||||
**Always run `$SCRIPT stop` when done testing** — `pkill -f "Electron"` alone won't catch all helper processes.
|
||||
|
||||
#### Environment Variables
|
||||
|
||||
| Variable | Default | Description |
|
||||
| ----------------- | ----------------------- | ---------------------------------------- |
|
||||
| `CDP_PORT` | `9222` | Chrome DevTools Protocol port |
|
||||
| `ELECTRON_LOG` | `/tmp/electron-dev.log` | Electron process log |
|
||||
| `ELECTRON_WAIT_S` | `60` | Max seconds to wait for Electron process |
|
||||
| `RENDERER_WAIT_S` | `60` | Max seconds to wait for SPA to load |
|
||||
|
||||
### LobeHub-Specific Patterns
|
||||
|
||||
#### Access Zustand Store State
|
||||
|
||||
```bash
|
||||
agent-browser --cdp 9222 eval --stdin << 'EVALEOF'
|
||||
(function() {
|
||||
var chat = window.__LOBE_STORES.chat();
|
||||
var ops = Object.values(chat.operations);
|
||||
return JSON.stringify({
|
||||
ops: ops.map(function(o) { return { type: o.type, status: o.status }; }),
|
||||
activeAgent: chat.activeAgentId,
|
||||
activeTopic: chat.activeTopicId,
|
||||
});
|
||||
})()
|
||||
EVALEOF
|
||||
```
|
||||
|
||||
#### Find and Use the Chat Input
|
||||
|
||||
```bash
|
||||
# The chat input is contenteditable — must use -C flag
|
||||
agent-browser --cdp 9222 snapshot -i -C 2>&1 | grep "editable"
|
||||
|
||||
agent-browser --cdp 9222 click @e48
|
||||
agent-browser --cdp 9222 type @e48 "Hello world"
|
||||
agent-browser --cdp 9222 press Enter
|
||||
```
|
||||
|
||||
#### Wait for Agent to Complete
|
||||
|
||||
```bash
|
||||
agent-browser --cdp 9222 eval --stdin << 'EVALEOF'
|
||||
(function() {
|
||||
var chat = window.__LOBE_STORES.chat();
|
||||
var ops = Object.values(chat.operations);
|
||||
var running = ops.filter(function(o) { return o.status === 'running'; });
|
||||
return running.length === 0 ? 'done' : 'running: ' + running.length;
|
||||
})()
|
||||
EVALEOF
|
||||
```
|
||||
|
||||
#### Install Error Interceptor
|
||||
|
||||
```bash
|
||||
agent-browser --cdp 9222 eval --stdin << 'EVALEOF'
|
||||
(function() {
|
||||
window.__CAPTURED_ERRORS = [];
|
||||
var orig = console.error;
|
||||
console.error = function() {
|
||||
var msg = Array.from(arguments).map(function(a) {
|
||||
if (a instanceof Error) return a.message;
|
||||
return typeof a === 'object' ? JSON.stringify(a) : String(a);
|
||||
}).join(' ');
|
||||
window.__CAPTURED_ERRORS.push(msg);
|
||||
orig.apply(console, arguments);
|
||||
};
|
||||
return 'installed';
|
||||
})()
|
||||
EVALEOF
|
||||
|
||||
# Later, check captured errors:
|
||||
agent-browser --cdp 9222 eval "JSON.stringify(window.__CAPTURED_ERRORS)"
|
||||
```
|
||||
|
||||
## Chrome / Web Apps
|
||||
|
||||
```bash
|
||||
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
|
||||
--remote-debugging-port=9222 \
|
||||
--user-data-dir=/tmp/chrome-test-profile \
|
||||
"<URL>" &
|
||||
sleep 5
|
||||
agent-browser --cdp 9222 snapshot -i
|
||||
|
||||
# Or auto-discover running Chrome with remote debugging
|
||||
agent-browser --auto-connect snapshot -i
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Part 2: osascript (Native macOS App Bot Testing)
|
||||
|
||||
Use AppleScript via `osascript` to control native macOS desktop apps for bot testing. Works with any app that supports macOS Accessibility, no CDP or Chromium needed.
|
||||
|
||||
The pattern is the same for every platform:
|
||||
|
||||
1. **Activate** the app (`tell application "X" to activate`)
|
||||
2. **Navigate** to a channel/chat (Quick Switcher `Cmd+K` or Search `Cmd+F`)
|
||||
3. **Send** a message (clipboard paste `Cmd+V` + Enter)
|
||||
4. **Wait** for the bot response
|
||||
5. **Screenshot** for verification (`screencapture` + `Read` tool)
|
||||
|
||||
## Per-Platform References
|
||||
|
||||
Pick the file for your target platform — each contains activation, navigation, send-message, and verification snippets specific to that app:
|
||||
|
||||
| Platform | Reference | Quick switcher |
|
||||
| ------------- | -------------------------------------------------- | -------------- |
|
||||
| Discord | [references/discord.md](./references/discord.md) | `Cmd+K` |
|
||||
| Slack | [references/slack.md](./references/slack.md) | `Cmd+K` |
|
||||
| Telegram | [references/telegram.md](./references/telegram.md) | `Cmd+F` |
|
||||
| WeChat / 微信 | [references/wechat.md](./references/wechat.md) | `Cmd+F` |
|
||||
| Lark / 飞书 | [references/lark.md](./references/lark.md) | `Cmd+K` |
|
||||
| QQ | [references/qq.md](./references/qq.md) | `Cmd+F` |
|
||||
|
||||
For **shared osascript patterns** (activate, type, paste, screenshot, read accessibility, common workflow template, gotchas), see [references/osascript-common.md](./references/osascript-common.md). Read this first if you're new to osascript automation.
|
||||
|
||||
---
|
||||
|
||||
# Scripts
|
||||
|
||||
Ready-to-use scripts in `.agents/skills/local-testing/scripts/`:
|
||||
|
||||
| Script | Usage |
|
||||
| ------------------------- | --------------------------------------------------- |
|
||||
| `electron-dev.sh` | Manage Electron dev env (start/stop/status/restart) |
|
||||
| `capture-app-window.sh` | Capture screenshot of a specific app window |
|
||||
| `record-electron-demo.sh` | Record Electron app demo with ffmpeg |
|
||||
| `record-app-screen.sh` | Record app screen (video + screenshots, start/stop) |
|
||||
| `test-discord-bot.sh` | Send message to Discord bot via osascript |
|
||||
| `test-slack-bot.sh` | Send message to Slack bot via osascript |
|
||||
| `test-telegram-bot.sh` | Send message to Telegram bot via osascript |
|
||||
| `test-wechat-bot.sh` | Send message to WeChat bot via osascript |
|
||||
| `test-lark-bot.sh` | Send message to Lark / 飞书 bot via osascript |
|
||||
| `test-qq-bot.sh` | Send message to QQ bot via osascript |
|
||||
|
||||
### Window Screenshot Utility
|
||||
|
||||
`capture-app-window.sh` captures a screenshot of a specific app window using `screencapture -l <windowID>`. It uses Swift + CGWindowList to find the window by process name, so screenshots work correctly even when the window is on an external monitor or behind other windows.
|
||||
|
||||
```bash
|
||||
# Standalone usage
|
||||
./.agents/skills/local-testing/scripts/capture-app-window.sh "Discord" /tmp/discord.png
|
||||
./.agents/skills/local-testing/scripts/capture-app-window.sh "Slack" /tmp/slack.png
|
||||
./.agents/skills/local-testing/scripts/capture-app-window.sh "WeChat" /tmp/wechat.png
|
||||
```
|
||||
|
||||
All bot test scripts use this utility automatically for their screenshots.
|
||||
|
||||
### Bot Test Scripts
|
||||
|
||||
All bot test scripts share the same interface:
|
||||
|
||||
```bash
|
||||
./scripts/test-<platform>-bot.sh <channel_or_contact> <message> [wait_seconds] [screenshot_path]
|
||||
```
|
||||
|
||||
Examples:
|
||||
|
||||
```bash
|
||||
# Discord — test a bot in #bot-testing channel
|
||||
./.agents/skills/local-testing/scripts/test-discord-bot.sh "bot-testing" "!ping"
|
||||
./.agents/skills/local-testing/scripts/test-discord-bot.sh "bot-testing" "/ask Tell me a joke" 30
|
||||
|
||||
# Slack — test a bot in #bot-testing channel
|
||||
./.agents/skills/local-testing/scripts/test-slack-bot.sh "bot-testing" "@mybot hello"
|
||||
./.agents/skills/local-testing/scripts/test-slack-bot.sh "bot-testing" "/ask What is 2+2?" 20
|
||||
|
||||
# Telegram — test a bot by username
|
||||
./.agents/skills/local-testing/scripts/test-telegram-bot.sh "MyTestBot" "/start"
|
||||
./.agents/skills/local-testing/scripts/test-telegram-bot.sh "GPTBot" "Hello" 60
|
||||
|
||||
# WeChat — test a bot or send to a contact
|
||||
./.agents/skills/local-testing/scripts/test-wechat-bot.sh "文件传输助手" "test message" 5
|
||||
./.agents/skills/local-testing/scripts/test-wechat-bot.sh "MyBot" "Tell me a joke" 30
|
||||
|
||||
# Lark/飞书 — test a bot in a group chat
|
||||
./.agents/skills/local-testing/scripts/test-lark-bot.sh "bot-testing" "@MyBot hello"
|
||||
./.agents/skills/local-testing/scripts/test-lark-bot.sh "bot-testing" "Help me with this" 30
|
||||
|
||||
# QQ — test a bot in a group or direct chat
|
||||
./.agents/skills/local-testing/scripts/test-qq-bot.sh "bot-testing" "Hello bot" 15
|
||||
./.agents/skills/local-testing/scripts/test-qq-bot.sh "MyBot" "/help" 10
|
||||
```
|
||||
|
||||
Each script: activates the app, navigates to the channel/contact, pastes the message via clipboard, sends, waits, and takes a screenshot. Use the `Read` tool on the screenshot for visual verification.
|
||||
|
||||
---
|
||||
|
||||
# Screen Recording
|
||||
|
||||
Record automated demos using `record-app-screen.sh` (start/stop lifecycle, CDP screenshots + ffmpeg assembly). See [references/record-app-screen.md](references/record-app-screen.md) for full documentation.
|
||||
|
||||
```bash
|
||||
./.agents/skills/local-testing/scripts/electron-dev.sh start
|
||||
./.agents/skills/local-testing/scripts/record-app-screen.sh start my-demo
|
||||
# ... run automation ...
|
||||
./.agents/skills/local-testing/scripts/record-app-screen.sh stop
|
||||
```
|
||||
|
||||
Outputs to `.records/` directory (gitignored): `<name>.mp4` (video) + `<name>/` (screenshots every 3s).
|
||||
|
||||
---
|
||||
|
||||
# Gotchas
|
||||
|
||||
### agent-browser
|
||||
|
||||
- **Daemon can get stuck** — if commands hang, `agent-browser close --all` or `pkill -f agent-browser` to reset
|
||||
- **HMR invalidates everything** — after code changes, refs break. Re-snapshot or restart
|
||||
- **`snapshot -i` doesn't find contenteditable** — use `snapshot -i -C` for rich text editors
|
||||
- **`fill` doesn't work on contenteditable** — use `type` for chat inputs
|
||||
- **Screenshots go to `~/.agent-browser/tmp/screenshots/`** — read them with the `Read` tool
|
||||
- **Dialogs block all commands** — if commands time out, check `agent-browser dialog status`
|
||||
- **Default timeout is 25s** — override with `AGENT_BROWSER_DEFAULT_TIMEOUT` (ms) or use explicit waits
|
||||
- **Shell quoting corrupts eval** — use `eval --stdin <<'EVALEOF'` for complex JS
|
||||
|
||||
### Electron-specific
|
||||
|
||||
- **Always use `electron-dev.sh stop` to clean up** — `pkill -f "Electron"` only kills the main process; helper processes (GPU, renderer, network) survive. The script finds and kills all of them via PID matching against the project's electron binary path.
|
||||
- **`npx electron-vite dev` must run from `apps/desktop/`** — running from project root fails silently. The `electron-dev.sh` script handles this automatically.
|
||||
- **Don't resize the Electron window after load** — resizing triggers full SPA reload
|
||||
- **Store is at `window.__LOBE_STORES`** not `window.__ZUSTAND_STORES__`
|
||||
|
||||
### osascript
|
||||
|
||||
See [references/osascript-common.md](./references/osascript-common.md#gotchas) for the full osascript gotchas list (accessibility permissions, `keystroke` non-ASCII issues, locale-specific app names, rate limiting, etc.).
|
||||
@@ -1,110 +0,0 @@
|
||||
# Log `agent-browser` into a local LobeHub dev server
|
||||
|
||||
`agent-browser --headed` on macOS often creates the Chromium window off-screen — the user can't see or interact with it, so manual login inside the agent-browser session fails. Instead of sharing the user's real Chrome profile, copy the **better-auth session cookie** out of a request in DevTools and inject it into the agent-browser session as a Playwright-style state file.
|
||||
|
||||
## When to use
|
||||
|
||||
- You need `agent-browser` to reach an authenticated page on `http://localhost:<port>` (e.g. `localhost:3011`).
|
||||
- The user already has a logged-in tab of the same dev server in their own Chrome.
|
||||
- Spawning a headed Chromium to let the user log in manually is unreliable (window off-screen, no interaction).
|
||||
|
||||
Do **not** use this on production URLs — only local dev. Treat the cookie as a secret: don't paste it into shared logs, PRs, or commit it anywhere.
|
||||
|
||||
## Step 1 — Ask the user to copy the cookie from a Network request, NOT `document.cookie`
|
||||
|
||||
`document.cookie` will not return HttpOnly cookies, which is exactly where better-auth puts its session. Instruct the user:
|
||||
|
||||
1. Open the logged-in tab (`http://localhost:<port>/…`) in their own Chrome.
|
||||
2. `Cmd+Option+I` → **Network** tab.
|
||||
3. Refresh, click any same-origin request (e.g. the top-level document request).
|
||||
4. In the right pane under **Request Headers**, right-click the `Cookie:` line → **Copy value** (or copy the entire header).
|
||||
5. Paste the string into chat.
|
||||
|
||||
You only need the better-auth pieces. Everything else (Clerk, `LOBE_LOCALE`, HMR hash, theme vars) is noise and can stay. The minimum viable set is:
|
||||
|
||||
```
|
||||
better-auth.session_token=<value>; better-auth.state=<value>
|
||||
```
|
||||
|
||||
## Step 2 — Build a Playwright-style state file
|
||||
|
||||
`agent-browser state load` expects Playwright's `storageState` format: a JSON with a `cookies` array and an `origins` array.
|
||||
|
||||
```bash
|
||||
cat > /tmp/mkstate.py << 'PY'
|
||||
import json, sys, time
|
||||
|
||||
# Read the Cookie header from stdin (allows optional "Cookie: " prefix).
|
||||
raw = sys.stdin.read().strip()
|
||||
if raw.lower().startswith("cookie:"):
|
||||
raw = raw.split(":", 1)[1].strip()
|
||||
|
||||
# Keep only better-auth cookies. Extend this set if the app genuinely needs more.
|
||||
WANTED = {"better-auth.session_token", "better-auth.state"}
|
||||
|
||||
cookies = []
|
||||
exp = int(time.time()) + 30 * 24 * 3600 # 30 days
|
||||
for pair in raw.split("; "):
|
||||
if "=" not in pair:
|
||||
continue
|
||||
name, _, value = pair.partition("=")
|
||||
if name not in WANTED:
|
||||
continue
|
||||
cookies.append({
|
||||
"name": name,
|
||||
"value": value,
|
||||
"domain": "localhost",
|
||||
"path": "/",
|
||||
"expires": exp,
|
||||
"httpOnly": False,
|
||||
"secure": False,
|
||||
"sameSite": "Lax",
|
||||
})
|
||||
|
||||
if not cookies:
|
||||
sys.stderr.write("no better-auth cookies found in input\n")
|
||||
sys.exit(1)
|
||||
|
||||
print(json.dumps({"cookies": cookies, "origins": []}, indent=2))
|
||||
PY
|
||||
|
||||
# Feed the copied Cookie header in via env var or heredoc.
|
||||
printf '%s' "$COOKIE_HEADER" | python3 /tmp/mkstate.py > /tmp/state.json
|
||||
```
|
||||
|
||||
**Note on `httpOnly`**: the real cookie in the user's browser is HttpOnly, but `storageState` doesn't enforce the flag on load — it just attaches the value. Storing with `httpOnly: false` is fine for local dev and sidesteps a CDP-context quirk where HttpOnly cookies sometimes fail to attach.
|
||||
|
||||
## Step 3 — Load state and navigate
|
||||
|
||||
```bash
|
||||
SESSION="my-test" # any stable session name
|
||||
|
||||
agent-browser --session "$SESSION" state load /tmp/state.json
|
||||
agent-browser --session "$SESSION" open "http://localhost:3011/"
|
||||
agent-browser --session "$SESSION" get url
|
||||
# Expect NOT /signin?callbackUrl=… — if you still see signin, cookie didn't apply.
|
||||
```
|
||||
|
||||
## Step 4 — Verify
|
||||
|
||||
```bash
|
||||
agent-browser --session "$SESSION" snapshot -i | head -20
|
||||
# Look for the user's avatar/name in the sidebar, or absence of the signin form.
|
||||
```
|
||||
|
||||
## Common failure modes
|
||||
|
||||
| Symptom | Cause | Fix |
|
||||
| ----------------------------------------------- | ----------------------------------------------------------------------- | ---------------------------------------------------- |
|
||||
| Still redirects to `/signin` after `state load` | User pasted from `document.cookie` → missed HttpOnly session | Re-pull from Network request Headers, not console |
|
||||
| `state load` reports 0 cookies | Separator wrong, or user pasted URL-decoded value | Keep the raw `Cookie:` header as-is; split on `"; "` |
|
||||
| Login works briefly then expires | `better-auth.session_token` rotated (user logged out / signed in again) | Re-copy and re-load |
|
||||
| Domain mismatch | Use `domain: "localhost"` literally, no leading dot for local dev | — |
|
||||
|
||||
## Scope
|
||||
|
||||
Only covers authenticating an **agent-browser** session into a **local** LobeHub dev server. It does not:
|
||||
|
||||
- Work for production — production cookies are `Secure; HttpOnly; Domain=.lobehub.com` and must be delivered over HTTPS.
|
||||
- Replace real OAuth flows — tests that must exercise the login UI need a real Chromium with `--remote-debugging-port` or a bot account.
|
||||
- Flow cookies back to the user's Chrome — injection is one-way (into agent-browser only).
|
||||
@@ -1,97 +0,0 @@
|
||||
# Discord Bot Testing
|
||||
|
||||
**App name:** `Discord` | **Process name:** `Discord`
|
||||
|
||||
See [osascript-common.md](./osascript-common.md) for shared patterns.
|
||||
|
||||
## Activate & Navigate
|
||||
|
||||
```bash
|
||||
# Activate Discord
|
||||
osascript -e 'tell application "Discord" to activate'
|
||||
sleep 1
|
||||
|
||||
# Open Quick Switcher (Cmd+K) to navigate to a channel
|
||||
osascript -e 'tell application "System Events" to keystroke "k" using command down'
|
||||
sleep 0.5
|
||||
osascript -e 'tell application "System Events" to keystroke "bot-testing"'
|
||||
sleep 1
|
||||
osascript -e 'tell application "System Events" to key code 36' # Enter
|
||||
sleep 2
|
||||
```
|
||||
|
||||
## Send Message to Bot
|
||||
|
||||
```bash
|
||||
# The message input is focused after navigating to a channel
|
||||
# Type a message
|
||||
osascript -e 'tell application "System Events" to keystroke "/hello"'
|
||||
sleep 0.5
|
||||
osascript -e 'tell application "System Events" to key code 36' # Enter
|
||||
```
|
||||
|
||||
## Send Long Message (via clipboard)
|
||||
|
||||
```bash
|
||||
osascript -e '
|
||||
tell application "Discord" to activate
|
||||
delay 0.5
|
||||
set the clipboard to "Write a 3000 word essay about space exploration"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36 -- Enter
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
## Verify Bot Response
|
||||
|
||||
```bash
|
||||
# Wait for bot to respond, then screenshot
|
||||
sleep 10
|
||||
screencapture /tmp/discord-bot-response.png
|
||||
# Read with the Read tool for visual verification
|
||||
```
|
||||
|
||||
## Full Bot Test Example
|
||||
|
||||
```bash
|
||||
#!/usr/bin/env bash
|
||||
# test-discord-bot.sh — Send message and verify bot response
|
||||
|
||||
# 1. Activate Discord and navigate to channel
|
||||
osascript -e '
|
||||
tell application "Discord" to activate
|
||||
delay 1
|
||||
-- Quick Switcher
|
||||
tell application "System Events" to keystroke "k" using command down
|
||||
delay 0.5
|
||||
tell application "System Events" to keystroke "bot-testing"
|
||||
delay 1
|
||||
tell application "System Events" to key code 36
|
||||
delay 2
|
||||
'
|
||||
|
||||
# 2. Send test message
|
||||
osascript -e '
|
||||
set the clipboard to "!ping"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36
|
||||
end tell
|
||||
'
|
||||
|
||||
# 3. Wait for response and capture
|
||||
sleep 5
|
||||
screencapture /tmp/discord-test-result.png
|
||||
echo "Screenshot saved to /tmp/discord-test-result.png"
|
||||
```
|
||||
|
||||
## Script
|
||||
|
||||
```bash
|
||||
./.agents/skills/local-testing/scripts/test-discord-bot.sh "bot-testing" "!ping"
|
||||
./.agents/skills/local-testing/scripts/test-discord-bot.sh "bot-testing" "/ask Tell me a joke" 30
|
||||
```
|
||||
@@ -1,61 +0,0 @@
|
||||
# Lark / 飞书 Bot Testing
|
||||
|
||||
**App name:** `Lark` or `飞书` | **Process name:** `Lark` or `飞书`
|
||||
|
||||
See [osascript-common.md](./osascript-common.md) for shared patterns.
|
||||
|
||||
## Activate & Navigate
|
||||
|
||||
```bash
|
||||
# Activate Lark (auto-detects Lark or 飞书)
|
||||
osascript -e 'tell application "Lark" to activate' 2> /dev/null \
|
||||
|| osascript -e 'tell application "飞书" to activate'
|
||||
sleep 1
|
||||
|
||||
# Quick Switcher / Search (Cmd+K)
|
||||
osascript -e 'tell application "System Events" to keystroke "k" using command down'
|
||||
sleep 0.5
|
||||
osascript -e '
|
||||
set the clipboard to "bot-testing"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 1.5
|
||||
key code 36 -- Enter
|
||||
end tell
|
||||
'
|
||||
sleep 2
|
||||
```
|
||||
|
||||
## Send Message to Bot
|
||||
|
||||
```bash
|
||||
osascript -e '
|
||||
set the clipboard to "@MyBot help me with this task"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36 -- Enter
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
## Verify Response
|
||||
|
||||
```bash
|
||||
sleep 10
|
||||
screencapture /tmp/lark-bot-response.png
|
||||
```
|
||||
|
||||
## Lark-Specific Notes
|
||||
|
||||
- App name varies: `Lark` (international) vs `飞书` (China mainland) — the script auto-detects
|
||||
- Uses `Cmd+K` for quick search (same as Discord/Slack)
|
||||
- Enter sends message by default
|
||||
- Always use clipboard paste for CJK characters
|
||||
|
||||
## Script
|
||||
|
||||
```bash
|
||||
./.agents/skills/local-testing/scripts/test-lark-bot.sh "bot-testing" "@MyBot hello"
|
||||
./.agents/skills/local-testing/scripts/test-lark-bot.sh "bot-testing" "Help me with this" 30
|
||||
```
|
||||
@@ -1,217 +0,0 @@
|
||||
# osascript Common Patterns
|
||||
|
||||
Shared AppleScript / `osascript` patterns used by all platform bot tests. Read this first, then refer to the per-platform file for app-specific quirks.
|
||||
|
||||
## Core Patterns
|
||||
|
||||
### Activate an App
|
||||
|
||||
```bash
|
||||
osascript -e 'tell application "Discord" to activate'
|
||||
```
|
||||
|
||||
### Type Text
|
||||
|
||||
```bash
|
||||
# Type character by character (reliable, but slow for long text)
|
||||
osascript -e 'tell application "System Events" to keystroke "Hello world"'
|
||||
|
||||
# Press Enter
|
||||
osascript -e 'tell application "System Events" to key code 36'
|
||||
|
||||
# Press Tab
|
||||
osascript -e 'tell application "System Events" to key code 48'
|
||||
|
||||
# Press Escape
|
||||
osascript -e 'tell application "System Events" to key code 53'
|
||||
```
|
||||
|
||||
### Paste from Clipboard (fast, for long text)
|
||||
|
||||
```bash
|
||||
# Set clipboard and paste — much faster than keystroke for long messages
|
||||
osascript -e 'set the clipboard to "Your long message here"'
|
||||
osascript -e 'tell application "System Events" to keystroke "v" using command down'
|
||||
```
|
||||
|
||||
Or in one shot:
|
||||
|
||||
```bash
|
||||
osascript -e '
|
||||
set the clipboard to "Your long message here"
|
||||
tell application "System Events" to keystroke "v" using command down
|
||||
'
|
||||
```
|
||||
|
||||
### Keyboard Shortcuts
|
||||
|
||||
```bash
|
||||
# Cmd+K (quick switcher in Discord/Slack)
|
||||
osascript -e 'tell application "System Events" to keystroke "k" using command down'
|
||||
|
||||
# Cmd+F (search)
|
||||
osascript -e 'tell application "System Events" to keystroke "f" using command down'
|
||||
|
||||
# Cmd+N (new message/chat)
|
||||
osascript -e 'tell application "System Events" to keystroke "n" using command down'
|
||||
|
||||
# Cmd+Shift+K (example: multi-modifier)
|
||||
osascript -e 'tell application "System Events" to keystroke "k" using {command down, shift down}'
|
||||
```
|
||||
|
||||
### Click at Position
|
||||
|
||||
```bash
|
||||
# Click at absolute screen coordinates
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
click at {500, 300}
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
### Get Window Info
|
||||
|
||||
```bash
|
||||
# Get window position and size
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
tell process "Discord"
|
||||
get {position, size} of window 1
|
||||
end tell
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
### Screenshot
|
||||
|
||||
```bash
|
||||
# Full screen
|
||||
screencapture /tmp/screenshot.png
|
||||
|
||||
# Interactive region select
|
||||
screencapture -i /tmp/screenshot.png
|
||||
|
||||
# Specific window (by window ID from CGWindowList)
|
||||
screencapture -l < WINDOW_ID > /tmp/screenshot.png
|
||||
```
|
||||
|
||||
To get window ID for a specific app:
|
||||
|
||||
```bash
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
tell process "Discord"
|
||||
get id of window 1
|
||||
end tell
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
### Read Accessibility Elements
|
||||
|
||||
```bash
|
||||
# Get all UI elements of the frontmost window (can be slow/large)
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
tell process "Discord"
|
||||
entire contents of window 1
|
||||
end tell
|
||||
end tell
|
||||
'
|
||||
|
||||
# Get a specific element's value
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
tell process "Discord"
|
||||
get value of text field 1 of window 1
|
||||
end tell
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
> **Warning:** `entire contents` can be extremely slow on complex UIs. Prefer screenshots + `Read` tool for visual verification.
|
||||
|
||||
### Read Screen Text via Clipboard
|
||||
|
||||
For reading the latest message or response from an app:
|
||||
|
||||
```bash
|
||||
# Select all text in the focused area and copy
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
keystroke "a" using command down
|
||||
keystroke "c" using command down
|
||||
end tell
|
||||
'
|
||||
sleep 0.5
|
||||
# Read clipboard
|
||||
pbpaste
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Common Bot Testing Workflow
|
||||
|
||||
Regardless of platform, the pattern is:
|
||||
|
||||
```bash
|
||||
APP_NAME="Discord" # or "Slack", "Telegram", "微信"
|
||||
CHANNEL="bot-testing"
|
||||
MESSAGE="Hello bot!"
|
||||
WAIT_SECONDS=10
|
||||
|
||||
# 1. Activate
|
||||
osascript -e "tell application \"$APP_NAME\" to activate"
|
||||
sleep 1
|
||||
|
||||
# 2. Navigate to channel/chat (via Quick Switcher or Search)
|
||||
osascript -e 'tell application "System Events" to keystroke "k" using command down'
|
||||
sleep 0.5
|
||||
osascript -e "tell application \"System Events\" to keystroke \"$CHANNEL\""
|
||||
sleep 1
|
||||
osascript -e 'tell application "System Events" to key code 36'
|
||||
sleep 2
|
||||
|
||||
# 3. Send message
|
||||
osascript -e "set the clipboard to \"$MESSAGE\""
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36
|
||||
end tell
|
||||
'
|
||||
|
||||
# 4. Wait for bot response
|
||||
sleep "$WAIT_SECONDS"
|
||||
|
||||
# 5. Screenshot for verification
|
||||
screencapture /tmp/"${APP_NAME,,}"-bot-test.png
|
||||
echo "Result saved to /tmp/${APP_NAME,,}-bot-test.png"
|
||||
```
|
||||
|
||||
### Tips
|
||||
|
||||
- **Use clipboard paste** (`Cmd+V`) for messages containing special characters or long text — `keystroke` can mangle non-ASCII
|
||||
- **Add `delay`** between actions — apps need time to process UI events
|
||||
- **Screenshot for verification** — use `screencapture` + `Read` tool for visual checks
|
||||
- **Use a dedicated test channel/chat** — avoid polluting real conversations
|
||||
- **Check app name** — some apps have different names in different locales (e.g., `微信` vs `WeChat`)
|
||||
- **Accessibility permissions required** — System Events automation requires granting Accessibility access in System Preferences > Privacy & Security > Accessibility
|
||||
|
||||
---
|
||||
|
||||
## Gotchas
|
||||
|
||||
- **Accessibility permission required** — first run will prompt for access; grant it in System Preferences > Privacy & Security > Accessibility for Terminal / iTerm / Claude Code
|
||||
- **`keystroke` is slow for long text** — always use clipboard paste (`Cmd+V`) for messages over \~20 characters
|
||||
- **`keystroke` can mangle non-ASCII** — use clipboard paste for Chinese, emoji, or special characters
|
||||
- **`key code 36` is Enter** — this is the hardware key code, works regardless of keyboard layout
|
||||
- **`entire contents` is extremely slow** — avoid for complex UIs; use screenshots instead
|
||||
- **App name varies by locale** — `微信` vs `WeChat`, `企业微信` vs `WeCom`; handle both
|
||||
- **WeChat Enter sends immediately** — use `Shift+Enter` for newlines within a message
|
||||
- **Rate limiting** — don't send messages too fast; platforms may throttle or flag automated input
|
||||
- **Lark / 飞书 app name varies** — `Lark` (international) vs `飞书` (China mainland); scripts auto-detect
|
||||
- **QQ uses `Cmd+F` for search** — not `Cmd+K` like Discord/Slack/Lark
|
||||
- **Bot response times vary** — AI-powered bots may take 10-60s; use generous sleep values
|
||||
@@ -1,62 +0,0 @@
|
||||
# QQ Bot Testing
|
||||
|
||||
**App name:** `QQ` | **Process name:** `QQ`
|
||||
|
||||
See [osascript-common.md](./osascript-common.md) for shared patterns.
|
||||
|
||||
## Activate & Navigate
|
||||
|
||||
```bash
|
||||
osascript -e 'tell application "QQ" to activate'
|
||||
sleep 1
|
||||
|
||||
# Search for contact/group (Cmd+F)
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
keystroke "f" using command down
|
||||
delay 0.8
|
||||
end tell
|
||||
'
|
||||
osascript -e '
|
||||
set the clipboard to "bot-testing"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 1.5
|
||||
key code 36 -- Enter
|
||||
end tell
|
||||
'
|
||||
sleep 2
|
||||
```
|
||||
|
||||
## Send Message to Bot
|
||||
|
||||
```bash
|
||||
osascript -e '
|
||||
set the clipboard to "Hello bot!"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36 -- Enter
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
## Verify Response
|
||||
|
||||
```bash
|
||||
sleep 10
|
||||
screencapture /tmp/qq-bot-response.png
|
||||
```
|
||||
|
||||
## QQ-Specific Notes
|
||||
|
||||
- Enter sends message by default; Shift+Enter for newlines
|
||||
- Uses `Cmd+F` for search (not `Cmd+K` like Discord/Slack/Lark)
|
||||
- Always use clipboard paste for CJK characters
|
||||
|
||||
## Script
|
||||
|
||||
```bash
|
||||
./.agents/skills/local-testing/scripts/test-qq-bot.sh "bot-testing" "Hello bot" 15
|
||||
./.agents/skills/local-testing/scripts/test-qq-bot.sh "MyBot" "/help" 10
|
||||
```
|
||||
@@ -1,142 +0,0 @@
|
||||
# record-app-screen.sh
|
||||
|
||||
General-purpose screen recording tool for the Electron app. Captures CDP screenshots as video frames and gallery snapshots, then assembles into an MP4 on stop.
|
||||
|
||||
## Why CDP Screenshots Instead of ffmpeg Screen Capture
|
||||
|
||||
- **Works on any screen** — CDP screenshots capture the browser viewport directly, so external monitors, Retina scaling, and window positioning are all handled automatically
|
||||
- **No signal handling issues** — ffmpeg-static (npm) produces corrupt MP4 files when killed (missing moov atom). CDP screenshots avoid this entirely
|
||||
- **Consistent output** — Screenshots are resolution-independent and don't require crop coordinate calculations
|
||||
|
||||
## Commands
|
||||
|
||||
```bash
|
||||
# Start recording (Electron must be running with CDP)
|
||||
.agents/skills/local-testing/scripts/record-app-screen.sh start [output_name]
|
||||
|
||||
# Stop recording and assemble video
|
||||
.agents/skills/local-testing/scripts/record-app-screen.sh stop
|
||||
|
||||
# Check if recording is active
|
||||
.agents/skills/local-testing/scripts/record-app-screen.sh status
|
||||
```
|
||||
|
||||
### Arguments
|
||||
|
||||
| Argument | Default | Description |
|
||||
| ------------- | --------------------------- | -------------------------- |
|
||||
| `output_name` | `recording-YYYYMMDD-HHMMSS` | Base name for output files |
|
||||
|
||||
### Environment Variables
|
||||
|
||||
| Variable | Default | Description |
|
||||
| ---------------------- | ------- | -------------------------------------- |
|
||||
| `CDP_PORT` | `9222` | Chrome DevTools Protocol port |
|
||||
| `SCREENSHOT_INTERVAL` | `3` | Seconds between gallery screenshots |
|
||||
| `VIDEO_FRAME_INTERVAL` | `0.5` | Seconds between video frames (\~2 fps) |
|
||||
|
||||
## Output Structure
|
||||
|
||||
```
|
||||
.records/
|
||||
<name>.mp4 # Video assembled from frames (~2 fps)
|
||||
<name>/ # Gallery screenshots (every 3s)
|
||||
0000.png
|
||||
0001.png
|
||||
0002.png
|
||||
...
|
||||
```
|
||||
|
||||
The `.records/` directory is at the project root and is gitignored.
|
||||
|
||||
## How It Works
|
||||
|
||||
### Start
|
||||
|
||||
1. Creates two background loops:
|
||||
- **Video frames** — `agent-browser screenshot` every `VIDEO_FRAME_INTERVAL` seconds into a temp directory (`/tmp/record-frames-XXXXXX/`)
|
||||
- **Gallery screenshots** — `agent-browser screenshot` every `SCREENSHOT_INTERVAL` seconds into `.records/<name>/`
|
||||
2. Saves PIDs and paths to `/tmp/record-app-screen.pids` and `/tmp/record-app-screen.state`
|
||||
|
||||
### Stop
|
||||
|
||||
1. Kills both background loops
|
||||
2. Assembles video frames into MP4 using ffmpeg:
|
||||
```
|
||||
ffmpeg -framerate 2 -i frame_%06d.png -c:v libx264 -crf 23 -pix_fmt yuv420p <output>.mp4
|
||||
```
|
||||
3. Cleans up temp frame directory
|
||||
4. Reports file sizes and paths
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Basic Test Recording
|
||||
|
||||
```bash
|
||||
# Start Electron
|
||||
.agents/skills/local-testing/scripts/electron-dev.sh start
|
||||
|
||||
# Start recording
|
||||
.agents/skills/local-testing/scripts/record-app-screen.sh start my-test
|
||||
|
||||
# Run automation
|
||||
agent-browser --cdp 9222 click @e61
|
||||
agent-browser --cdp 9222 type @e42 "hello"
|
||||
agent-browser --cdp 9222 press Enter
|
||||
sleep 10
|
||||
|
||||
# Stop and get results
|
||||
.agents/skills/local-testing/scripts/record-app-screen.sh stop
|
||||
# → .records/my-test.mp4 + .records/my-test/*.png
|
||||
```
|
||||
|
||||
### Gateway Streaming Demo
|
||||
|
||||
```bash
|
||||
.agents/skills/local-testing/scripts/electron-dev.sh start
|
||||
|
||||
# Inject gateway URL
|
||||
agent-browser --cdp 9222 eval --stdin << 'EOF'
|
||||
(function() {
|
||||
var store = window.global_serverConfigStore;
|
||||
store.setState({ serverConfig: { ...store.getState().serverConfig,
|
||||
agentGatewayUrl: 'https://agent-gateway.lobehub.com' } });
|
||||
return 'ready';
|
||||
})()
|
||||
EOF
|
||||
|
||||
# Record
|
||||
.agents/skills/local-testing/scripts/record-app-screen.sh start gateway-demo
|
||||
|
||||
# Navigate to agent, send message, wait for completion...
|
||||
# (automation commands here)
|
||||
|
||||
.agents/skills/local-testing/scripts/record-app-screen.sh stop
|
||||
open .records/gateway-demo.mp4
|
||||
```
|
||||
|
||||
### Check Active Recording
|
||||
|
||||
```bash
|
||||
.agents/skills/local-testing/scripts/record-app-screen.sh status
|
||||
# [record] Active recording
|
||||
# Frames: 42 captured (running: yes)
|
||||
# Screenshots: 14 captured (running: yes)
|
||||
# Output: .records/my-test.mp4
|
||||
```
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- **ffmpeg** — For video assembly. Install via `bun add -g ffmpeg-static` or `brew install ffmpeg`
|
||||
- **agent-browser** — For CDP screenshots. Install via `npm i -g agent-browser`
|
||||
- **Electron app running** — With CDP enabled (use `electron-dev.sh start`)
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
| Problem | Solution |
|
||||
| ----------------------------------- | ------------------------------------------------------------------------------------------------------------ |
|
||||
| "No active recording found" on stop | PID file was cleaned up. Check if background processes are still running with `ps aux \| grep agent-browser` |
|
||||
| "A recording is already active" | Run `stop` first, or manually clean: `rm /tmp/record-app-screen.pids /tmp/record-app-screen.state` |
|
||||
| Video is 0 bytes | No frames were captured. Ensure Electron is running and CDP port is correct |
|
||||
| Screenshots are blank/white | SPA may not have loaded yet. Wait for `electron-dev.sh` to report "Renderer ready" |
|
||||
| ffmpeg assembly fails | Check `/tmp/ffmpeg-assemble.log`. Ensure ffmpeg is installed and frames exist |
|
||||
@@ -1,73 +0,0 @@
|
||||
# Slack Bot Testing
|
||||
|
||||
**App name:** `Slack` | **Process name:** `Slack`
|
||||
|
||||
See [osascript-common.md](./osascript-common.md) for shared patterns.
|
||||
|
||||
## Activate & Navigate
|
||||
|
||||
```bash
|
||||
# Activate Slack
|
||||
osascript -e 'tell application "Slack" to activate'
|
||||
sleep 1
|
||||
|
||||
# Quick Switcher (Cmd+K)
|
||||
osascript -e 'tell application "System Events" to keystroke "k" using command down'
|
||||
sleep 0.5
|
||||
osascript -e 'tell application "System Events" to keystroke "bot-testing"'
|
||||
sleep 1
|
||||
osascript -e 'tell application "System Events" to key code 36' # Enter
|
||||
sleep 2
|
||||
```
|
||||
|
||||
## Send Message to Bot
|
||||
|
||||
```bash
|
||||
# Direct message input (focused after channel nav)
|
||||
osascript -e 'tell application "System Events" to keystroke "@mybot hello"'
|
||||
sleep 0.3
|
||||
osascript -e 'tell application "System Events" to key code 36'
|
||||
```
|
||||
|
||||
## Send Long Message
|
||||
|
||||
```bash
|
||||
osascript -e '
|
||||
tell application "Slack" to activate
|
||||
delay 0.5
|
||||
set the clipboard to "A long test message for the bot..."
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
## Slash Command Test
|
||||
|
||||
```bash
|
||||
osascript -e '
|
||||
tell application "Slack" to activate
|
||||
delay 0.5
|
||||
tell application "System Events"
|
||||
keystroke "/ask What is the meaning of life?"
|
||||
delay 0.5
|
||||
key code 36
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
## Verify Response
|
||||
|
||||
```bash
|
||||
sleep 10
|
||||
screencapture /tmp/slack-bot-response.png
|
||||
```
|
||||
|
||||
## Script
|
||||
|
||||
```bash
|
||||
./.agents/skills/local-testing/scripts/test-slack-bot.sh "bot-testing" "@mybot hello"
|
||||
./.agents/skills/local-testing/scripts/test-slack-bot.sh "bot-testing" "/ask What is 2+2?" 20
|
||||
```
|
||||
@@ -1,80 +0,0 @@
|
||||
# Telegram Bot Testing
|
||||
|
||||
**App name:** `Telegram` | **Process name:** `Telegram`
|
||||
|
||||
See [osascript-common.md](./osascript-common.md) for shared patterns.
|
||||
|
||||
## Activate & Navigate
|
||||
|
||||
```bash
|
||||
# Activate Telegram
|
||||
osascript -e 'tell application "Telegram" to activate'
|
||||
sleep 1
|
||||
|
||||
# Search for a bot (Cmd+F or click search)
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
keystroke "f" using command down
|
||||
delay 0.5
|
||||
keystroke "MyTestBot"
|
||||
delay 1
|
||||
key code 36 -- Enter to select
|
||||
end tell
|
||||
'
|
||||
sleep 2
|
||||
```
|
||||
|
||||
## Send Message to Bot
|
||||
|
||||
```bash
|
||||
# After navigating to bot chat, input is focused
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
keystroke "/start"
|
||||
delay 0.3
|
||||
key code 36
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
## Send Long Message
|
||||
|
||||
```bash
|
||||
osascript -e '
|
||||
tell application "Telegram" to activate
|
||||
delay 0.5
|
||||
set the clipboard to "Tell me about quantum computing in detail"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
## Verify Response
|
||||
|
||||
```bash
|
||||
sleep 10
|
||||
screencapture /tmp/telegram-bot-response.png
|
||||
```
|
||||
|
||||
## Telegram Bot API (programmatic alternative)
|
||||
|
||||
For sending messages directly to the bot's chat without UI:
|
||||
|
||||
```bash
|
||||
# Send message as the bot (for testing webhooks/responses)
|
||||
curl -s "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \
|
||||
-d "chat_id=$CHAT_ID&text=test message"
|
||||
|
||||
# Get recent updates
|
||||
curl -s "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getUpdates?limit=5" | jq .
|
||||
```
|
||||
|
||||
## Script
|
||||
|
||||
```bash
|
||||
./.agents/skills/local-testing/scripts/test-telegram-bot.sh "MyTestBot" "/start"
|
||||
./.agents/skills/local-testing/scripts/test-telegram-bot.sh "GPTBot" "Hello" 60
|
||||
```
|
||||
@@ -1,81 +0,0 @@
|
||||
# WeChat / 微信 Bot Testing
|
||||
|
||||
**App name:** `微信` or `WeChat` | **Process name:** `WeChat`
|
||||
|
||||
See [osascript-common.md](./osascript-common.md) for shared patterns.
|
||||
|
||||
## Activate & Navigate
|
||||
|
||||
```bash
|
||||
# Activate WeChat
|
||||
osascript -e 'tell application "微信" to activate'
|
||||
sleep 1
|
||||
|
||||
# Search for a contact/bot (Cmd+F)
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
keystroke "f" using command down
|
||||
delay 0.5
|
||||
keystroke "TestBot"
|
||||
delay 1
|
||||
key code 36 -- Enter to select
|
||||
end tell
|
||||
'
|
||||
sleep 2
|
||||
```
|
||||
|
||||
## Send Message
|
||||
|
||||
```bash
|
||||
# After navigating to a chat, the input is focused
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
keystroke "Hello bot!"
|
||||
delay 0.3
|
||||
key code 36
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
## Send Long Message (clipboard)
|
||||
|
||||
```bash
|
||||
osascript -e '
|
||||
tell application "微信" to activate
|
||||
delay 0.5
|
||||
set the clipboard to "Please help me with this task..."
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36
|
||||
end tell
|
||||
'
|
||||
```
|
||||
|
||||
## Verify Response
|
||||
|
||||
```bash
|
||||
sleep 10
|
||||
screencapture /tmp/wechat-bot-response.png
|
||||
```
|
||||
|
||||
## WeChat-Specific Notes
|
||||
|
||||
- WeChat macOS app name can be `微信` or `WeChat` depending on system language. Try both:
|
||||
```bash
|
||||
osascript -e 'tell application "微信" to activate' 2> /dev/null \
|
||||
|| osascript -e 'tell application "WeChat" to activate'
|
||||
```
|
||||
- WeChat uses **Enter** to send (not Cmd+Enter by default, but configurable)
|
||||
- For multi-line messages without sending, use **Shift+Enter**:
|
||||
```bash
|
||||
osascript -e 'tell application "System Events" to key code 36 using shift down'
|
||||
```
|
||||
- Always use clipboard paste for CJK characters — `keystroke` mangles non-ASCII
|
||||
|
||||
## Script
|
||||
|
||||
```bash
|
||||
./.agents/skills/local-testing/scripts/test-wechat-bot.sh "文件传输助手" "test message" 5
|
||||
./.agents/skills/local-testing/scripts/test-wechat-bot.sh "MyBot" "Tell me a joke" 30
|
||||
```
|
||||
@@ -1,54 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# capture-app-window.sh — Capture a screenshot of a specific app window
|
||||
#
|
||||
# Uses CGWindowList via Swift to find the window by process name, then
|
||||
# screencapture -l <windowID> to capture only that window.
|
||||
# Falls back to full-screen capture if the window is not found.
|
||||
#
|
||||
# Usage:
|
||||
# ./capture-app-window.sh <process_name> <output_path>
|
||||
#
|
||||
# Arguments:
|
||||
# process_name — The process/owner name as shown in Activity Monitor
|
||||
# (e.g., "Discord", "Slack", "Telegram", "WeChat", "QQ", "Lark")
|
||||
# output_path — Path to save the screenshot (e.g., /tmp/screenshot.png)
|
||||
#
|
||||
# Examples:
|
||||
# ./capture-app-window.sh "Discord" /tmp/discord.png
|
||||
# ./capture-app-window.sh "Slack" /tmp/slack.png
|
||||
# ./capture-app-window.sh "微信" /tmp/wechat.png
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
PROCESS="${1:?Usage: capture-app-window.sh <process_name> <output_path>}"
|
||||
OUTPUT="${2:?Usage: capture-app-window.sh <process_name> <output_path>}"
|
||||
|
||||
# Find the CGWindowID for the target process using Swift + CGWindowList
|
||||
# Pass process name via environment variable (swift -e doesn't support -- args)
|
||||
WINDOW_ID=$(TARGET_PROCESS="$PROCESS" swift -e '
|
||||
import Cocoa
|
||||
import Foundation
|
||||
let target = ProcessInfo.processInfo.environment["TARGET_PROCESS"] ?? ""
|
||||
let windowList = CGWindowListCopyWindowInfo([.optionAll], kCGNullWindowID) as! [[String: Any]]
|
||||
for w in windowList {
|
||||
let owner = w["kCGWindowOwnerName"] as? String ?? ""
|
||||
let layer = w["kCGWindowLayer"] as? Int ?? -1
|
||||
let bounds = w["kCGWindowBounds"] as? [String: Any] ?? [:]
|
||||
let ww = bounds["Width"] as? Double ?? 0
|
||||
let wh = bounds["Height"] as? Double ?? 0
|
||||
let wid = w["kCGWindowNumber"] as? Int ?? 0
|
||||
// Match process name, normal window layer (0), and reasonable size
|
||||
if owner == target && layer == 0 && ww > 200 && wh > 200 {
|
||||
print(wid)
|
||||
break
|
||||
}
|
||||
}
|
||||
' 2>/dev/null || true)
|
||||
|
||||
if [ -n "$WINDOW_ID" ]; then
|
||||
screencapture -l "$WINDOW_ID" -x "$OUTPUT"
|
||||
else
|
||||
echo "[capture] Warning: Could not find window for '$PROCESS', falling back to full screen"
|
||||
screencapture -x "$OUTPUT"
|
||||
fi
|
||||
@@ -1,327 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# electron-dev.sh — Manage Electron dev environment for testing
|
||||
#
|
||||
# Usage:
|
||||
# ./electron-dev.sh start # Kill existing, start fresh, wait until ready
|
||||
# ./electron-dev.sh stop # Kill all Electron-related processes
|
||||
# ./electron-dev.sh status # Check if Electron is running and CDP is reachable
|
||||
# ./electron-dev.sh restart # Stop then start
|
||||
#
|
||||
# Environment variables:
|
||||
# CDP_PORT — Chrome DevTools Protocol port (default: 9222)
|
||||
# ELECTRON_LOG — Log file path (default: /tmp/electron-dev.log)
|
||||
# ELECTRON_WAIT_S — Max seconds to wait for CDP to become reachable (default: 90)
|
||||
# RENDERER_WAIT_S — Max seconds to wait for SPA after CDP is up (default: 60)
|
||||
# FORCE_KILL_USER — When set to 1, silently kill the user's `bun run dev`
|
||||
# Electron without confirmation (default: always confirm-by-action)
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
CDP_PORT="${CDP_PORT:-9222}"
|
||||
ELECTRON_LOG="${ELECTRON_LOG:-/tmp/electron-dev.log}"
|
||||
ELECTRON_WAIT_S="${ELECTRON_WAIT_S:-90}"
|
||||
RENDERER_WAIT_S="${RENDERER_WAIT_S:-60}"
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../../.." && pwd)"
|
||||
PIDFILE="/tmp/electron-dev-cdp-${CDP_PORT}.pid"
|
||||
|
||||
# Project-scoped electron path prefix used for pgrep matching. Any Electron
|
||||
# binary from this project (main + helpers, with or without --remote-debugging-port)
|
||||
# starts with this string in its argv[0], so a single substring match catches all.
|
||||
PROJECT_ELECTRON_PATH="${PROJECT_ROOT}/apps/desktop/node_modules/.pnpm/electron@"
|
||||
|
||||
# ── Helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
# Print pid + every descendant pid (DFS via pgrep -P).
|
||||
expand_descendants() {
|
||||
local pid="$1"
|
||||
echo "$pid"
|
||||
local children
|
||||
children=$(pgrep -P "$pid" 2>/dev/null || true)
|
||||
for c in $children; do
|
||||
expand_descendants "$c"
|
||||
done
|
||||
}
|
||||
|
||||
# Find seed PIDs related to this project's Electron dev session.
|
||||
# Matches REGARDLESS of whether --remote-debugging-port was passed, so it also
|
||||
# catches a plain `bun run dev` session the user started outside this script.
|
||||
find_project_pids() {
|
||||
local pids=""
|
||||
|
||||
# 1. Any process whose command line mentions this project's electron path
|
||||
# (covers the main Electron binary AND every Helper subprocess)
|
||||
local electron_pids
|
||||
electron_pids=$(pgrep -f "$PROJECT_ELECTRON_PATH" 2>/dev/null || true)
|
||||
pids="$pids $electron_pids"
|
||||
|
||||
# 2. electron-vite dev server (narrow match to avoid catching unrelated Vite invocations)
|
||||
local vite_pids
|
||||
vite_pids=$(pgrep -f "electron-vite[/.].*\\bdev\\b" 2>/dev/null || true)
|
||||
pids="$pids $vite_pids"
|
||||
|
||||
# 3. The launcher subshell from a previous `start` (saved to pidfile)
|
||||
if [ -f "$PIDFILE" ]; then
|
||||
local saved_pid
|
||||
saved_pid=$(cat "$PIDFILE" 2>/dev/null || true)
|
||||
if [ -n "$saved_pid" ] && kill -0 "$saved_pid" 2>/dev/null; then
|
||||
pids="$pids $saved_pid"
|
||||
fi
|
||||
fi
|
||||
|
||||
# 4. Whatever is currently bound to the CDP port — catches strays whose
|
||||
# binary path doesn't match (e.g. orphaned from a crashed restart)
|
||||
local port_pid
|
||||
port_pid=$(lsof -ti tcp:"$CDP_PORT" -sTCP:LISTEN 2>/dev/null || true)
|
||||
pids="$pids $port_pid"
|
||||
|
||||
# `|| true` because `grep -v '^$'` exits 1 when input has no non-empty
|
||||
# lines, which (with pipefail + set -e) silently kills the caller.
|
||||
echo "$pids" | tr ' ' '\n' | sort -u | grep -v '^$' | tr '\n' ' ' || true
|
||||
}
|
||||
|
||||
# Wait for the CDP HTTP endpoint to respond, with a deadline + early bail-out
|
||||
# if the launcher process died (no point waiting if Electron crashed).
|
||||
wait_for_cdp() {
|
||||
local deadline=$(( $(date +%s) + ELECTRON_WAIT_S ))
|
||||
echo "[electron-dev] Waiting for CDP on port ${CDP_PORT} (up to ${ELECTRON_WAIT_S}s)..."
|
||||
|
||||
while [ "$(date +%s)" -lt "$deadline" ]; do
|
||||
if curl -sf --max-time 2 "http://localhost:${CDP_PORT}/json/version" >/dev/null 2>&1; then
|
||||
echo "[electron-dev] CDP is reachable."
|
||||
return 0
|
||||
fi
|
||||
|
||||
# If our launcher subshell died, abort early so we don't hang the full timeout
|
||||
if [ -f "$PIDFILE" ]; then
|
||||
local saved_pid
|
||||
saved_pid=$(cat "$PIDFILE" 2>/dev/null || true)
|
||||
if [ -n "$saved_pid" ] && ! kill -0 "$saved_pid" 2>/dev/null; then
|
||||
echo "[electron-dev] Launcher PID $saved_pid is gone before CDP came up."
|
||||
echo "[electron-dev] Last 30 lines of $ELECTRON_LOG:"
|
||||
tail -30 "$ELECTRON_LOG" 2>/dev/null || true
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
done
|
||||
|
||||
echo "[electron-dev] ERROR: CDP did not respond within ${ELECTRON_WAIT_S}s"
|
||||
echo "[electron-dev] Last 30 lines of $ELECTRON_LOG:"
|
||||
tail -30 "$ELECTRON_LOG" 2>/dev/null || true
|
||||
return 1
|
||||
}
|
||||
|
||||
# After CDP is up, wait until the SPA renders interactive elements.
|
||||
wait_for_renderer() {
|
||||
local deadline=$(( $(date +%s) + RENDERER_WAIT_S ))
|
||||
echo "[electron-dev] Waiting for SPA to load (up to ${RENDERER_WAIT_S}s)..."
|
||||
|
||||
while [ "$(date +%s)" -lt "$deadline" ]; do
|
||||
local snap
|
||||
snap=$(agent-browser --cdp "$CDP_PORT" snapshot -i 2>&1 || true)
|
||||
if echo "$snap" | grep -qE '\b(link|button)\b'; then
|
||||
echo "[electron-dev] Renderer ready."
|
||||
return 0
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
|
||||
echo "[electron-dev] WARNING: Renderer not interactive within ${RENDERER_WAIT_S}s — proceeding anyway."
|
||||
return 0
|
||||
}
|
||||
|
||||
# ── Commands ─────────────────────────────────────────────────────────
|
||||
|
||||
do_stop() {
|
||||
echo "[electron-dev] Stopping Electron dev environment..."
|
||||
|
||||
local seed_pids
|
||||
seed_pids=$(find_project_pids)
|
||||
|
||||
# Expand to include all descendants — catches helpers spawned by the main
|
||||
# process AFTER our pgrep snapshot, and the launcher's child node/electron-vite
|
||||
# process tree.
|
||||
local all_pids=""
|
||||
for pid in $seed_pids; do
|
||||
all_pids="$all_pids $(expand_descendants "$pid")"
|
||||
done
|
||||
all_pids=$(echo "$all_pids" | tr ' ' '\n' | sort -u | grep -v '^$' | tr '\n' ' ' || true)
|
||||
|
||||
if [ -z "$all_pids" ]; then
|
||||
echo "[electron-dev] No project Electron/vite processes found."
|
||||
else
|
||||
local count
|
||||
count=$(echo "$all_pids" | tr ' ' '\n' | grep -c .)
|
||||
echo "[electron-dev] Sending SIGTERM to $count process(es): $all_pids"
|
||||
for pid in $all_pids; do
|
||||
kill "$pid" 2>/dev/null || true
|
||||
done
|
||||
|
||||
# Wait up to 5s for graceful exit
|
||||
local waited=0
|
||||
while [ $waited -lt 5 ]; do
|
||||
local any_alive=0
|
||||
for pid in $all_pids; do
|
||||
if kill -0 "$pid" 2>/dev/null; then any_alive=1; break; fi
|
||||
done
|
||||
[ "$any_alive" = "0" ] && break
|
||||
sleep 1
|
||||
waited=$((waited + 1))
|
||||
done
|
||||
|
||||
# SIGKILL anyone still alive
|
||||
for pid in $all_pids; do
|
||||
if kill -0 "$pid" 2>/dev/null; then
|
||||
echo "[electron-dev] Force-killing PID $pid"
|
||||
kill -9 "$pid" 2>/dev/null || true
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Belt-and-suspenders: anything still bound to the CDP port goes away
|
||||
local port_pid
|
||||
port_pid=$(lsof -ti tcp:"$CDP_PORT" -sTCP:LISTEN 2>/dev/null || true)
|
||||
if [ -n "$port_pid" ]; then
|
||||
echo "[electron-dev] Port $CDP_PORT still bound by PID $port_pid; force-killing"
|
||||
# shellcheck disable=SC2086
|
||||
kill -9 $port_pid 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# Also re-sweep the project's electron processes — sometimes the OS spawns
|
||||
# new helpers during shutdown that didn't exist when we first enumerated.
|
||||
local stragglers
|
||||
stragglers=$(pgrep -f "$PROJECT_ELECTRON_PATH" 2>/dev/null || true)
|
||||
if [ -n "$stragglers" ]; then
|
||||
echo "[electron-dev] Cleaning up stragglers: $stragglers"
|
||||
for pid in $stragglers; do
|
||||
kill -9 "$pid" 2>/dev/null || true
|
||||
done
|
||||
fi
|
||||
|
||||
# Close any agent-browser sessions connected to this port
|
||||
agent-browser --cdp "$CDP_PORT" close --all 2>/dev/null || true
|
||||
|
||||
rm -f "$PIDFILE"
|
||||
echo "[electron-dev] Stopped."
|
||||
}
|
||||
|
||||
do_status() {
|
||||
local pids
|
||||
pids=$(find_project_pids)
|
||||
|
||||
if [ -z "$pids" ]; then
|
||||
echo "[electron-dev] No project Electron processes found."
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo "[electron-dev] Project processes: $pids"
|
||||
|
||||
if curl -sf --max-time 2 "http://localhost:${CDP_PORT}/json/version" >/dev/null 2>&1; then
|
||||
local url
|
||||
url=$(agent-browser --cdp "$CDP_PORT" get url 2>&1 | tail -1 || echo "?")
|
||||
echo "[electron-dev] CDP port ${CDP_PORT} is reachable. URL: $url"
|
||||
return 0
|
||||
else
|
||||
echo "[electron-dev] CDP port ${CDP_PORT} is NOT reachable (no --remote-debugging-port, or still loading)."
|
||||
return 2
|
||||
fi
|
||||
}
|
||||
|
||||
do_start() {
|
||||
# Already up and CDP is reachable → nothing to do
|
||||
if curl -sf --max-time 2 "http://localhost:${CDP_PORT}/json/version" >/dev/null 2>&1; then
|
||||
echo "[electron-dev] CDP already reachable on port $CDP_PORT. Skipping start."
|
||||
echo "[electron-dev] Use 'restart' to force a fresh session."
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Detect the user's existing dev session (or stale processes) BEFORE killing
|
||||
local existing
|
||||
existing=$(find_project_pids)
|
||||
if [ -n "$existing" ]; then
|
||||
echo "[electron-dev] Existing project Electron/vite processes detected:"
|
||||
echo "$existing" | tr ' ' '\n' | sed 's/^/[electron-dev] PID /'
|
||||
echo "[electron-dev] Tearing them down so we can start a CDP-enabled session..."
|
||||
fi
|
||||
|
||||
do_stop
|
||||
|
||||
# Wait for port + user-data-dir locks to release. Without this, the new
|
||||
# Electron may fail with "user data directory in use" or fail to bind CDP.
|
||||
local waited=0
|
||||
while [ $waited -lt 10 ]; do
|
||||
if ! lsof -i tcp:"$CDP_PORT" >/dev/null 2>&1 \
|
||||
&& ! pgrep -f "$PROJECT_ELECTRON_PATH" >/dev/null 2>&1; then
|
||||
break
|
||||
fi
|
||||
[ $waited -eq 0 ] && echo "[electron-dev] Waiting for port + Electron locks to release..."
|
||||
sleep 1
|
||||
waited=$((waited + 1))
|
||||
done
|
||||
|
||||
echo "[electron-dev] Starting Electron dev server..."
|
||||
echo "[electron-dev] Project: $PROJECT_ROOT"
|
||||
echo "[electron-dev] CDP port: $CDP_PORT"
|
||||
echo "[electron-dev] Log: $ELECTRON_LOG"
|
||||
|
||||
: > "$ELECTRON_LOG" # Truncate log
|
||||
|
||||
# Launch in a new session (setsid) so the whole process tree shares a PGID
|
||||
# we can later signal in one shot. `setsid bash -c '... exec ...' &` keeps
|
||||
# the bash shell as the session leader; its PID is what we save.
|
||||
# macOS doesn't ship setsid by default — fall back to plain bash; cleanup
|
||||
# still works via `expand_descendants` walking the process tree.
|
||||
local launch_cmd="
|
||||
cd '$PROJECT_ROOT/apps/desktop'
|
||||
exec npx electron-vite dev -- --remote-debugging-port=$CDP_PORT
|
||||
"
|
||||
if command -v setsid >/dev/null 2>&1; then
|
||||
setsid bash -c "$launch_cmd" >> "$ELECTRON_LOG" 2>&1 < /dev/null &
|
||||
else
|
||||
bash -c "$launch_cmd" >> "$ELECTRON_LOG" 2>&1 < /dev/null &
|
||||
fi
|
||||
local launcher_pid=$!
|
||||
echo "$launcher_pid" > "$PIDFILE"
|
||||
echo "[electron-dev] Launcher PID (session leader): $launcher_pid"
|
||||
|
||||
if ! wait_for_cdp; then
|
||||
echo "[electron-dev] Failed to bring up CDP. Cleaning up..."
|
||||
do_stop
|
||||
return 1
|
||||
fi
|
||||
|
||||
if ! wait_for_renderer; then
|
||||
echo "[electron-dev] Renderer not interactive — you may need to wait more."
|
||||
fi
|
||||
|
||||
echo "[electron-dev] Ready! Use: agent-browser --cdp $CDP_PORT snapshot -i"
|
||||
}
|
||||
|
||||
do_restart() {
|
||||
do_stop
|
||||
sleep 1
|
||||
do_start
|
||||
}
|
||||
|
||||
# ── Main ─────────────────────────────────────────────────────────────
|
||||
|
||||
case "${1:-help}" in
|
||||
start) do_start ;;
|
||||
stop) do_stop ;;
|
||||
status) do_status ;;
|
||||
restart) do_restart ;;
|
||||
*)
|
||||
echo "Usage: $0 {start|stop|status|restart}"
|
||||
echo ""
|
||||
echo " start — Start Electron dev with CDP. Detects + tears down any"
|
||||
echo " existing project Electron (e.g. \`bun run dev\`) first."
|
||||
echo " stop — Kill all project Electron/vite processes (main + helpers"
|
||||
echo " + descendants), with SIGTERM → 5s wait → SIGKILL fallback."
|
||||
echo " status — Check if Electron is running and CDP is reachable."
|
||||
echo " restart — Stop then start."
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
@@ -1,189 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# record-app-screen.sh — Record the Electron app window (video + screenshots)
|
||||
#
|
||||
# Captures screenshots via agent-browser (CDP), then assembles into video on stop.
|
||||
# Works on any screen (including external monitors) since it uses CDP, not screen capture.
|
||||
#
|
||||
# Usage:
|
||||
# ./record-app-screen.sh start [output_name] # Begin recording
|
||||
# ./record-app-screen.sh stop # Stop and save
|
||||
# ./record-app-screen.sh status # Check recording state
|
||||
#
|
||||
# Outputs to .records/ directory:
|
||||
# .records/<name>.mp4 — Video assembled from screenshots (~2 fps)
|
||||
# .records/<name>/ — Screenshots every SCREENSHOT_INTERVAL seconds
|
||||
#
|
||||
# Prerequisites:
|
||||
# - ffmpeg installed (bun add -g ffmpeg-static, or brew install ffmpeg)
|
||||
# - agent-browser CLI installed
|
||||
# - Electron app already running with CDP enabled
|
||||
#
|
||||
# Environment variables:
|
||||
# CDP_PORT — Chrome DevTools Protocol port (default: 9222)
|
||||
# SCREENSHOT_INTERVAL — Seconds between gallery screenshots (default: 3)
|
||||
# VIDEO_FRAME_INTERVAL — Seconds between video frames (default: 0.5)
|
||||
#
|
||||
# Examples:
|
||||
# ./electron-dev.sh start
|
||||
# ./record-app-screen.sh start gateway-demo
|
||||
# # ... run automation via agent-browser ...
|
||||
# ./record-app-screen.sh stop
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
PROJECT_DIR="$(cd "$SCRIPT_DIR/../../../.." && pwd)"
|
||||
|
||||
RECORDS_DIR="$PROJECT_DIR/.records"
|
||||
PID_FILE="/tmp/record-app-screen.pids"
|
||||
STATE_FILE="/tmp/record-app-screen.state"
|
||||
|
||||
CDP_PORT="${CDP_PORT:-9222}"
|
||||
SCREENSHOT_INTERVAL="${SCREENSHOT_INTERVAL:-3}"
|
||||
VIDEO_FRAME_INTERVAL="${VIDEO_FRAME_INTERVAL:-0.5}"
|
||||
|
||||
AB="agent-browser --cdp $CDP_PORT"
|
||||
|
||||
# ─── Commands ───
|
||||
|
||||
cmd_start() {
|
||||
local output_name="${1:-recording-$(date +%Y%m%d-%H%M%S)}"
|
||||
local output_video="$RECORDS_DIR/${output_name}.mp4"
|
||||
local screenshot_dir="$RECORDS_DIR/${output_name}"
|
||||
local frames_dir
|
||||
frames_dir=$(mktemp -d /tmp/record-frames-XXXXXX)
|
||||
|
||||
if [ -f "$PID_FILE" ]; then
|
||||
echo "[record] A recording is already active. Run '$0 stop' first."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "$RECORDS_DIR" "$screenshot_dir"
|
||||
|
||||
# Video frames loop (~2 fps via agent-browser CDP screenshots)
|
||||
(
|
||||
local idx=0
|
||||
while true; do
|
||||
local fname
|
||||
fname=$(printf "%s/frame_%06d.png" "$frames_dir" "$idx")
|
||||
$AB screenshot "$fname" 2>/dev/null || true
|
||||
idx=$((idx + 1))
|
||||
sleep "$VIDEO_FRAME_INTERVAL"
|
||||
done
|
||||
) &
|
||||
local frames_pid=$!
|
||||
|
||||
# Gallery screenshots loop (every N seconds for human review)
|
||||
(
|
||||
local idx=0
|
||||
while true; do
|
||||
local fname
|
||||
fname=$(printf "%s/%04d.png" "$screenshot_dir" "$idx")
|
||||
$AB screenshot "$fname" 2>/dev/null || true
|
||||
idx=$((idx + 1))
|
||||
sleep "$SCREENSHOT_INTERVAL"
|
||||
done
|
||||
) &
|
||||
local screenshot_pid=$!
|
||||
|
||||
# Save state
|
||||
echo "$frames_pid $screenshot_pid" > "$PID_FILE"
|
||||
echo "$output_video $frames_dir $screenshot_dir" > "$STATE_FILE"
|
||||
|
||||
echo "[record] Started!"
|
||||
echo " Video frames: every ${VIDEO_FRAME_INTERVAL}s (PID $frames_pid)"
|
||||
echo " Screenshots: every ${SCREENSHOT_INTERVAL}s → $screenshot_dir/"
|
||||
echo " Stop with: $0 stop"
|
||||
}
|
||||
|
||||
cmd_stop() {
|
||||
if [ ! -f "$PID_FILE" ] || [ ! -f "$STATE_FILE" ]; then
|
||||
echo "[record] No active recording found."
|
||||
return 0
|
||||
fi
|
||||
|
||||
local frames_pid screenshot_pid
|
||||
read -r frames_pid screenshot_pid < "$PID_FILE"
|
||||
|
||||
local output_video frames_dir screenshot_dir
|
||||
read -r output_video frames_dir screenshot_dir < "$STATE_FILE"
|
||||
|
||||
# Stop both capture loops
|
||||
kill "$frames_pid" 2>/dev/null || true
|
||||
kill "$screenshot_pid" 2>/dev/null || true
|
||||
wait "$frames_pid" 2>/dev/null || true
|
||||
wait "$screenshot_pid" 2>/dev/null || true
|
||||
|
||||
# Assemble frames into video
|
||||
local frame_count
|
||||
frame_count=$(ls -1 "$frames_dir"/frame_*.png 2>/dev/null | wc -l | tr -d ' ')
|
||||
|
||||
if [ "$frame_count" -gt 0 ]; then
|
||||
echo "[record] Assembling $frame_count frames into video..."
|
||||
ffmpeg -y -framerate 2 -i "$frames_dir/frame_%06d.png" \
|
||||
-c:v libx264 -crf 23 -pix_fmt yuv420p -an \
|
||||
"$output_video" > /tmp/ffmpeg-assemble.log 2>&1
|
||||
|
||||
if [ ! -s "$output_video" ]; then
|
||||
echo " [warn] Video assembly failed. Check /tmp/ffmpeg-assemble.log"
|
||||
echo " Frames preserved in: $frames_dir/"
|
||||
fi
|
||||
else
|
||||
echo " [warn] No frames captured."
|
||||
fi
|
||||
|
||||
rm -rf "$frames_dir" 2>/dev/null
|
||||
rm -f "$PID_FILE" "$STATE_FILE"
|
||||
|
||||
local video_size screenshot_count
|
||||
video_size=$(ls -lh "$output_video" 2>/dev/null | awk '{print $5}' || echo "?")
|
||||
screenshot_count=$(ls -1 "$screenshot_dir"/*.png 2>/dev/null | wc -l | tr -d ' ' || echo "0")
|
||||
|
||||
echo "[record] Stopped!"
|
||||
echo " Video: $output_video ($video_size)"
|
||||
echo " Screenshots: ${screenshot_count} files in $screenshot_dir/"
|
||||
echo " Play: open $output_video"
|
||||
}
|
||||
|
||||
cmd_status() {
|
||||
if [ ! -f "$PID_FILE" ]; then
|
||||
echo "[record] No active recording."
|
||||
return 0
|
||||
fi
|
||||
|
||||
local frames_pid screenshot_pid
|
||||
read -r frames_pid screenshot_pid < "$PID_FILE"
|
||||
|
||||
local frames_ok="no" screenshot_ok="no"
|
||||
kill -0 "$frames_pid" 2>/dev/null && frames_ok="yes"
|
||||
kill -0 "$screenshot_pid" 2>/dev/null && screenshot_ok="yes"
|
||||
|
||||
if [ -f "$STATE_FILE" ]; then
|
||||
local output_video frames_dir screenshot_dir
|
||||
read -r output_video frames_dir screenshot_dir < "$STATE_FILE"
|
||||
local frame_count ss_count
|
||||
frame_count=$(ls -1 "$frames_dir"/frame_*.png 2>/dev/null | wc -l | tr -d ' ' || echo "0")
|
||||
ss_count=$(ls -1 "$screenshot_dir"/*.png 2>/dev/null | wc -l | tr -d ' ' || echo "0")
|
||||
echo "[record] Active recording"
|
||||
echo " Frames: $frame_count captured (running: $frames_ok)"
|
||||
echo " Screenshots: $ss_count captured (running: $screenshot_ok)"
|
||||
echo " Output: $output_video"
|
||||
fi
|
||||
}
|
||||
|
||||
# ─── Main ───
|
||||
|
||||
case "${1:-}" in
|
||||
start) shift; cmd_start "$@" ;;
|
||||
stop) cmd_stop ;;
|
||||
status) cmd_status ;;
|
||||
*)
|
||||
echo "Usage: $0 {start [name] | stop | status}"
|
||||
echo ""
|
||||
echo " start [name] Start recording (default: recording-YYYYMMDD-HHMMSS)"
|
||||
echo " stop Stop recording and save outputs"
|
||||
echo " status Check if recording is active"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
@@ -1,353 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# record-electron-demo.sh — Record an automated demo of the Electron app
|
||||
#
|
||||
# Usage:
|
||||
# ./scripts/record-electron-demo.sh [script.sh] [output.mp4]
|
||||
#
|
||||
# script.sh — A shell script containing agent-browser commands to automate.
|
||||
# It receives the CDP port as $1. Defaults to a built-in queue-edit demo.
|
||||
# output.mp4 — Output file path. Defaults to /tmp/electron-demo.mp4
|
||||
#
|
||||
# Prerequisites:
|
||||
# - agent-browser CLI installed globally
|
||||
# - ffmpeg installed (brew install ffmpeg)
|
||||
# - Electron app NOT already running (script manages lifecycle)
|
||||
#
|
||||
# Examples:
|
||||
# # Run built-in demo
|
||||
# ./scripts/record-electron-demo.sh
|
||||
#
|
||||
# # Run custom automation script
|
||||
# ./scripts/record-electron-demo.sh ./my-demo.sh /tmp/my-demo.mp4
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
CDP_PORT=9222
|
||||
DEMO_SCRIPT="${1:-}"
|
||||
OUTPUT="${2:-/tmp/electron-demo.mp4}"
|
||||
ELECTRON_LOG="/tmp/electron-dev.log"
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
|
||||
RECORD_PID=""
|
||||
|
||||
# ── Helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
cleanup() {
|
||||
echo "[cleanup] Stopping all processes..."
|
||||
[ -n "$RECORD_PID" ] && kill -INT "$RECORD_PID" 2>/dev/null && sleep 2
|
||||
pkill -f "electron-vite" 2>/dev/null || true
|
||||
pkill -f "Electron" 2>/dev/null || true
|
||||
pkill -f "agent-browser" 2>/dev/null || true
|
||||
echo "[cleanup] Done."
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
wait_for_electron() {
|
||||
echo "[wait] Waiting for Electron to start..."
|
||||
for i in $(seq 1 24); do
|
||||
sleep 5
|
||||
if strings "$ELECTRON_LOG" 2>/dev/null | grep -q "starting electron"; then
|
||||
echo "[wait] Electron process ready."
|
||||
return 0
|
||||
fi
|
||||
echo "[wait] Still waiting... (${i}/24)"
|
||||
done
|
||||
echo "[error] Electron failed to start within 120s"
|
||||
exit 1
|
||||
}
|
||||
|
||||
wait_for_renderer() {
|
||||
echo "[wait] Waiting for renderer to load..."
|
||||
sleep 15
|
||||
agent-browser --cdp "$CDP_PORT" wait 3000
|
||||
|
||||
# Poll until interactive elements appear (SPA may take extra time)
|
||||
for i in $(seq 1 12); do
|
||||
local snap
|
||||
snap=$(agent-browser --cdp "$CDP_PORT" snapshot -i 2>&1)
|
||||
if echo "$snap" | grep -q 'link "'; then
|
||||
echo "[wait] Renderer ready (interactive elements found)."
|
||||
return 0
|
||||
fi
|
||||
echo "[wait] SPA still loading... (${i}/12)"
|
||||
sleep 5
|
||||
done
|
||||
echo "[warn] Timed out waiting for interactive elements, proceeding anyway."
|
||||
}
|
||||
|
||||
get_window_and_screen_info() {
|
||||
# Returns: window_x window_y window_w window_h screen_index
|
||||
# Uses Swift to find the Electron window bounds and which screen it's on
|
||||
swift -e '
|
||||
import Cocoa
|
||||
let windowList = CGWindowListCopyWindowInfo([.optionAll], kCGNullWindowID) as! [[String: Any]]
|
||||
for w in windowList {
|
||||
let owner = w["kCGWindowOwnerName"] as? String ?? ""
|
||||
let name = w["kCGWindowName"] as? String ?? ""
|
||||
let layer = w["kCGWindowLayer"] as? Int ?? -1
|
||||
let bounds = w["kCGWindowBounds"] as? [String: Any] ?? [:]
|
||||
let wx = bounds["X"] as? Double ?? 0
|
||||
let wy = bounds["Y"] as? Double ?? 0
|
||||
let ww = bounds["Width"] as? Double ?? 0
|
||||
let wh = bounds["Height"] as? Double ?? 0
|
||||
if (owner == "Electron" || owner == "LobeHub") && layer == 0 && name == "LobeHub" && ww > 200 && wh > 200 {
|
||||
// Find which screen this window is on
|
||||
let screens = NSScreen.screens
|
||||
var screenIdx = 0
|
||||
let windowCenter = NSPoint(x: wx + ww / 2, y: wy + wh / 2)
|
||||
for (i, screen) in screens.enumerated() {
|
||||
let frame = screen.frame
|
||||
// Convert CG coords (top-left origin) to NSScreen coords (bottom-left origin)
|
||||
let mainHeight = screens[0].frame.height
|
||||
let screenTop = mainHeight - frame.origin.y - frame.height
|
||||
let screenBottom = screenTop + frame.height
|
||||
let screenLeft = frame.origin.x
|
||||
let screenRight = screenLeft + frame.width
|
||||
if windowCenter.x >= screenLeft && windowCenter.x <= screenRight &&
|
||||
windowCenter.y >= screenTop && windowCenter.y <= screenBottom {
|
||||
screenIdx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
// Compute window position relative to the screen it is on
|
||||
let screen = screens[screenIdx]
|
||||
let mainHeight = screens[0].frame.height
|
||||
let screenTop = mainHeight - screen.frame.origin.y - screen.frame.height
|
||||
let relX = wx - screen.frame.origin.x
|
||||
let relY = wy - screenTop
|
||||
let scale = Int(screen.backingScaleFactor)
|
||||
print("\(Int(relX)) \(Int(relY)) \(Int(ww)) \(Int(wh)) \(screenIdx) \(scale)")
|
||||
break
|
||||
}
|
||||
}
|
||||
'
|
||||
}
|
||||
|
||||
start_recording() {
|
||||
local rel_x=$1 rel_y=$2 w=$3 h=$4 screen_idx=$5 scale=$6
|
||||
|
||||
# ffmpeg avfoundation device index for screens
|
||||
# List devices and find the one matching our screen index
|
||||
local device_idx
|
||||
device_idx=$(ffmpeg -f avfoundation -list_devices true -i "" 2>&1 \
|
||||
| grep "Capture screen ${screen_idx}" \
|
||||
| grep -oE '\[[0-9]+\]' | tr -d '[]' || true)
|
||||
|
||||
if [ -z "$device_idx" ]; then
|
||||
echo "[warn] Could not find capture device for screen $screen_idx, trying default (3)"
|
||||
device_idx=3
|
||||
fi
|
||||
|
||||
# Scale coordinates to native resolution
|
||||
local cx=$((rel_x * scale))
|
||||
local cy=$((rel_y * scale))
|
||||
local cw=$((w * scale))
|
||||
local ch=$((h * scale))
|
||||
|
||||
echo "[record] Window: ${rel_x},${rel_y} ${w}x${h} on screen ${screen_idx} (scale=${scale})"
|
||||
echo "[record] Crop: ${cx},${cy} ${cw}x${ch}, device: ${device_idx}"
|
||||
echo "[record] Output: $OUTPUT"
|
||||
|
||||
ffmpeg -y \
|
||||
-f avfoundation -framerate 30 -capture_cursor 1 -i "${device_idx}:" \
|
||||
-vf "crop=${cw}:${ch}:${cx}:${cy},scale=${w}:${h}" \
|
||||
-c:v libx264 -crf 23 -preset fast -an \
|
||||
"$OUTPUT" \
|
||||
> /tmp/ffmpeg-record.log 2>&1 &
|
||||
RECORD_PID=$!
|
||||
sleep 2
|
||||
|
||||
if ! kill -0 "$RECORD_PID" 2>/dev/null; then
|
||||
echo "[error] ffmpeg failed to start. Log:"
|
||||
cat /tmp/ffmpeg-record.log
|
||||
RECORD_PID=""
|
||||
return 1
|
||||
fi
|
||||
echo "[record] Recording started (PID=$RECORD_PID)"
|
||||
}
|
||||
|
||||
stop_recording() {
|
||||
if [ -n "$RECORD_PID" ]; then
|
||||
echo "[record] Stopping recording..."
|
||||
kill -INT "$RECORD_PID" 2>/dev/null || true
|
||||
wait "$RECORD_PID" 2>/dev/null || true
|
||||
RECORD_PID=""
|
||||
echo "[record] Saved to $OUTPUT"
|
||||
ls -lh "$OUTPUT"
|
||||
fi
|
||||
}
|
||||
|
||||
# ── Built-in demo: Queue Edit ────────────────────────────────────────
|
||||
|
||||
find_input_ref() {
|
||||
local port=$1
|
||||
agent-browser --cdp "$port" snapshot -i -C 2>&1 \
|
||||
| grep "editable" \
|
||||
| grep -oE 'ref=e[0-9]+' \
|
||||
| head -1 \
|
||||
| sed 's/ref=//'
|
||||
}
|
||||
|
||||
builtin_demo() {
|
||||
local port=$1
|
||||
|
||||
echo "[demo] Step 1: Navigate to first available agent"
|
||||
local snapshot agent_ref
|
||||
snapshot=$(agent-browser --cdp "$port" snapshot -i 2>&1)
|
||||
# Try Lobe AI first, then fall back to any agent link in the sidebar
|
||||
agent_ref=$(echo "$snapshot" | grep -oE 'link "Lobe AI" \[ref=e[0-9]+\]' | grep -oE 'e[0-9]+' || true)
|
||||
if [ -z "$agent_ref" ]; then
|
||||
# Pick the first agent-like link (skip nav links)
|
||||
agent_ref=$(echo "$snapshot" | grep 'link "' | grep -vE '"Home"|"Pages"|"Settings"|"Search"|"Resources"|"Marketplace"' | head -1 | grep -oE 'ref=e[0-9]+' | sed 's/ref=//' || true)
|
||||
fi
|
||||
if [ -z "$agent_ref" ]; then
|
||||
echo "[error] No agent link found in snapshot"
|
||||
echo "$snapshot" | head -30
|
||||
return 1
|
||||
fi
|
||||
echo "[demo] Clicking agent ref: @$agent_ref"
|
||||
agent-browser --cdp "$port" click "@$agent_ref"
|
||||
sleep 3
|
||||
|
||||
echo "[demo] Step 2: Send first message (triggers AI generation)"
|
||||
local input_ref
|
||||
input_ref=$(find_input_ref "$port")
|
||||
agent-browser --cdp "$port" click "@$input_ref"
|
||||
agent-browser --cdp "$port" type "@$input_ref" "Write a 3000 word essay about the complete history of space exploration from Sputnik to the James Webb Space Telescope"
|
||||
sleep 1
|
||||
agent-browser --cdp "$port" press Enter
|
||||
sleep 3
|
||||
|
||||
echo "[demo] Step 3: Queue message 1"
|
||||
input_ref=$(find_input_ref "$port")
|
||||
agent-browser --cdp "$port" click "@$input_ref"
|
||||
agent-browser --cdp "$port" type "@$input_ref" "This message should be edited"
|
||||
sleep 1
|
||||
agent-browser --cdp "$port" press Enter
|
||||
sleep 1
|
||||
|
||||
echo "[demo] Step 4: Queue message 2"
|
||||
input_ref=$(find_input_ref "$port")
|
||||
agent-browser --cdp "$port" click "@$input_ref"
|
||||
agent-browser --cdp "$port" type "@$input_ref" "Another queued message"
|
||||
sleep 1
|
||||
agent-browser --cdp "$port" press Enter
|
||||
sleep 1
|
||||
|
||||
echo "[demo] Step 5: Verify queue has messages"
|
||||
local queue_count
|
||||
queue_count=$(agent-browser --cdp "$port" eval --stdin << 'EVALEOF'
|
||||
(function() {
|
||||
var chat = window.__LOBE_STORES.chat();
|
||||
var total = 0;
|
||||
Object.keys(chat.queuedMessages).forEach(function(k) {
|
||||
total += chat.queuedMessages[k].length;
|
||||
});
|
||||
return String(total);
|
||||
})()
|
||||
EVALEOF
|
||||
)
|
||||
echo "[demo] Queue count: $queue_count"
|
||||
|
||||
if [ "$queue_count" = "0" ] || [ "$queue_count" = '"0"' ]; then
|
||||
echo "[demo] Queue was already drained. Retrying..."
|
||||
input_ref=$(find_input_ref "$port")
|
||||
agent-browser --cdp "$port" click "@$input_ref"
|
||||
agent-browser --cdp "$port" type "@$input_ref" "Now write another 3000 word essay about artificial intelligence from Turing to transformers covering every major breakthrough"
|
||||
sleep 1
|
||||
agent-browser --cdp "$port" press Enter
|
||||
sleep 2
|
||||
input_ref=$(find_input_ref "$port")
|
||||
agent-browser --cdp "$port" click "@$input_ref"
|
||||
agent-browser --cdp "$port" type "@$input_ref" "This message should be edited"
|
||||
sleep 1
|
||||
agent-browser --cdp "$port" press Enter
|
||||
sleep 1
|
||||
input_ref=$(find_input_ref "$port")
|
||||
agent-browser --cdp "$port" click "@$input_ref"
|
||||
agent-browser --cdp "$port" type "@$input_ref" "Another queued message"
|
||||
sleep 1
|
||||
agent-browser --cdp "$port" press Enter
|
||||
sleep 1
|
||||
fi
|
||||
|
||||
echo "[demo] Step 6: Scroll to show queue tray"
|
||||
agent-browser --cdp "$port" scroll down 5000
|
||||
sleep 2
|
||||
|
||||
echo "[demo] Step 7: Click edit button on first queued message"
|
||||
agent-browser --cdp "$port" eval --stdin << 'EVALEOF'
|
||||
(function() {
|
||||
var chat = window.__LOBE_STORES.chat();
|
||||
var keys = Object.keys(chat.queuedMessages);
|
||||
for (var k = 0; k < keys.length; k++) {
|
||||
var queue = chat.queuedMessages[keys[k]];
|
||||
if (queue.length > 0) {
|
||||
var targetText = queue[0].content;
|
||||
var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null);
|
||||
while (walker.nextNode()) {
|
||||
var node = walker.currentNode;
|
||||
if (node.textContent.trim() === targetText) {
|
||||
var row = node.parentElement.parentElement;
|
||||
var buttons = row.querySelectorAll('[role="button"]');
|
||||
if (buttons.length >= 1) {
|
||||
buttons[0].click();
|
||||
return 'clicked edit on: ' + targetText;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 'edit button not found';
|
||||
})()
|
||||
EVALEOF
|
||||
sleep 3
|
||||
|
||||
echo "[demo] Step 8: Show result — content restored to input"
|
||||
sleep 3
|
||||
|
||||
echo "[demo] Complete!"
|
||||
}
|
||||
|
||||
# ── Main ─────────────────────────────────────────────────────────────
|
||||
|
||||
echo "=== Electron Demo Recorder ==="
|
||||
|
||||
# 1. Kill existing instances
|
||||
echo "[setup] Cleaning up existing processes..."
|
||||
pkill -f "Electron" 2>/dev/null || true
|
||||
pkill -f "electron-vite" 2>/dev/null || true
|
||||
pkill -f "agent-browser" 2>/dev/null || true
|
||||
sleep 3
|
||||
|
||||
# 2. Start Electron
|
||||
echo "[setup] Starting Electron..."
|
||||
cd "$PROJECT_ROOT/apps/desktop"
|
||||
ELECTRON_ENABLE_LOGGING=1 npx electron-vite dev -- --remote-debugging-port="$CDP_PORT" > "$ELECTRON_LOG" 2>&1 &
|
||||
|
||||
wait_for_electron
|
||||
wait_for_renderer
|
||||
|
||||
# 3. Get window position and start recording
|
||||
WIN_INFO=$(get_window_and_screen_info)
|
||||
if [ -z "$WIN_INFO" ]; then
|
||||
echo "[error] Could not find Electron window"
|
||||
exit 1
|
||||
fi
|
||||
read -r WIN_X WIN_Y WIN_W WIN_H SCREEN_IDX SCALE <<< "$WIN_INFO"
|
||||
start_recording "$WIN_X" "$WIN_Y" "$WIN_W" "$WIN_H" "$SCREEN_IDX" "$SCALE"
|
||||
|
||||
# 4. Run demo script
|
||||
if [ -n "$DEMO_SCRIPT" ] && [ -f "$DEMO_SCRIPT" ]; then
|
||||
echo "[demo] Running custom script: $DEMO_SCRIPT"
|
||||
bash "$DEMO_SCRIPT" "$CDP_PORT"
|
||||
else
|
||||
echo "[demo] Running built-in queue-edit demo"
|
||||
builtin_demo "$CDP_PORT"
|
||||
fi
|
||||
|
||||
# 5. Stop recording
|
||||
stop_recording
|
||||
|
||||
echo "=== Done! Output: $OUTPUT ==="
|
||||
@@ -1,64 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# test-discord-bot.sh — Send a message to a Discord bot and capture the response
|
||||
#
|
||||
# Usage:
|
||||
# ./scripts/test-discord-bot.sh <channel> <message> [wait_seconds] [screenshot_path]
|
||||
#
|
||||
# channel — Channel name to navigate to via Quick Switcher (Cmd+K)
|
||||
# message — Message to send to the bot
|
||||
# wait_seconds — Seconds to wait for bot response (default: 10)
|
||||
# screenshot_path — Output screenshot path (default: /tmp/discord-bot-test.png)
|
||||
#
|
||||
# Prerequisites:
|
||||
# - Discord desktop app installed and logged in
|
||||
# - Accessibility permission granted (System Preferences > Privacy > Accessibility)
|
||||
#
|
||||
# Examples:
|
||||
# ./scripts/test-discord-bot.sh "bot-testing" "!ping"
|
||||
# ./scripts/test-discord-bot.sh "bot-testing" "/ask Tell me a joke" 30
|
||||
# ./scripts/test-discord-bot.sh "general" "Hello bot" 15 /tmp/my-test.png
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
CHANNEL="${1:?Usage: test-discord-bot.sh <channel> <message> [wait_seconds] [screenshot_path]}"
|
||||
MESSAGE="${2:?Usage: test-discord-bot.sh <channel> <message> [wait_seconds] [screenshot_path]}"
|
||||
WAIT="${3:-10}"
|
||||
SCREENSHOT="${4:-/tmp/discord-bot-test.png}"
|
||||
|
||||
APP="Discord"
|
||||
|
||||
echo "[$APP] Activating..."
|
||||
osascript -e "tell application \"$APP\" to activate"
|
||||
sleep 1
|
||||
|
||||
echo "[$APP] Navigating to channel: $CHANNEL"
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
-- Quick Switcher
|
||||
keystroke "k" using command down
|
||||
delay 0.8
|
||||
keystroke "'"$CHANNEL"'"
|
||||
delay 1.5
|
||||
key code 36 -- Enter
|
||||
end tell
|
||||
'
|
||||
sleep 2
|
||||
|
||||
echo "[$APP] Sending message: $MESSAGE"
|
||||
osascript -e '
|
||||
set the clipboard to "'"$MESSAGE"'"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36 -- Enter
|
||||
end tell
|
||||
'
|
||||
|
||||
echo "[$APP] Waiting ${WAIT}s for bot response..."
|
||||
sleep "$WAIT"
|
||||
|
||||
echo "[$APP] Capturing screenshot..."
|
||||
"$SCRIPT_DIR/capture-app-window.sh" "$APP" "$SCREENSHOT"
|
||||
echo "[$APP] Done! Screenshot saved to $SCREENSHOT"
|
||||
@@ -1,84 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# test-lark-bot.sh — Send a message to a Lark/Feishu bot and capture the response
|
||||
#
|
||||
# Usage:
|
||||
# ./scripts/test-lark-bot.sh <chat> <message> [wait_seconds] [screenshot_path]
|
||||
#
|
||||
# chat — Chat or contact name to search for
|
||||
# message — Message to send to the bot
|
||||
# wait_seconds — Seconds to wait for bot response (default: 10)
|
||||
# screenshot_path — Output screenshot path (default: /tmp/lark-bot-test.png)
|
||||
#
|
||||
# Prerequisites:
|
||||
# - Lark (飞书) desktop app installed and logged in
|
||||
# - Accessibility permission granted (System Preferences > Privacy > Accessibility)
|
||||
#
|
||||
# Notes:
|
||||
# - The app name may be "Lark" or "飞书" depending on version/locale
|
||||
# - Uses Cmd+K to open search/quick switcher
|
||||
# - Enter sends message by default
|
||||
#
|
||||
# Examples:
|
||||
# ./scripts/test-lark-bot.sh "TestBot" "Hello"
|
||||
# ./scripts/test-lark-bot.sh "bot-testing" "/ask Tell me a joke" 30
|
||||
# ./scripts/test-lark-bot.sh "MyBot" "Help me summarize this" 60 /tmp/my-test.png
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
CHAT="${1:?Usage: test-lark-bot.sh <chat> <message> [wait_seconds] [screenshot_path]}"
|
||||
MESSAGE="${2:?Usage: test-lark-bot.sh <chat> <message> [wait_seconds] [screenshot_path]}"
|
||||
WAIT="${3:-10}"
|
||||
SCREENSHOT="${4:-/tmp/lark-bot-test.png}"
|
||||
|
||||
# Detect app name — "Lark" or "飞书"
|
||||
APP=""
|
||||
if osascript -e 'tell application "Lark" to name' &>/dev/null; then
|
||||
APP="Lark"
|
||||
elif osascript -e 'tell application "飞书" to name' &>/dev/null; then
|
||||
APP="飞书"
|
||||
else
|
||||
echo "[error] Lark/飞书 app not found. Install Lark or 飞书."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "[$APP] Activating..."
|
||||
osascript -e "tell application \"$APP\" to activate"
|
||||
sleep 1
|
||||
|
||||
echo "[$APP] Searching for chat: $CHAT"
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
-- Quick Switcher / Search (Cmd+K)
|
||||
keystroke "k" using command down
|
||||
delay 0.8
|
||||
end tell
|
||||
'
|
||||
# Use clipboard for chat name (supports CJK characters)
|
||||
osascript -e '
|
||||
set the clipboard to "'"$CHAT"'"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 1.5
|
||||
key code 36 -- Enter to select first result
|
||||
end tell
|
||||
'
|
||||
sleep 2
|
||||
|
||||
echo "[$APP] Sending message: $MESSAGE"
|
||||
osascript -e '
|
||||
set the clipboard to "'"$MESSAGE"'"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36 -- Enter to send
|
||||
end tell
|
||||
'
|
||||
|
||||
echo "[$APP] Waiting ${WAIT}s for bot response..."
|
||||
sleep "$WAIT"
|
||||
|
||||
echo "[$APP] Capturing screenshot..."
|
||||
"$SCRIPT_DIR/capture-app-window.sh" "$APP" "$SCREENSHOT"
|
||||
echo "[$APP] Done! Screenshot saved to $SCREENSHOT"
|
||||
@@ -1,76 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# test-qq-bot.sh — Send a message to a QQ bot and capture the response
|
||||
#
|
||||
# Usage:
|
||||
# ./scripts/test-qq-bot.sh <contact> <message> [wait_seconds] [screenshot_path]
|
||||
#
|
||||
# contact — Contact, group, or bot name to search for
|
||||
# message — Message to send
|
||||
# wait_seconds — Seconds to wait for bot response (default: 10)
|
||||
# screenshot_path — Output screenshot path (default: /tmp/qq-bot-test.png)
|
||||
#
|
||||
# Prerequisites:
|
||||
# - QQ desktop app installed and logged in
|
||||
# - Accessibility permission granted (System Preferences > Privacy > Accessibility)
|
||||
#
|
||||
# Notes:
|
||||
# - The app name is "QQ"
|
||||
# - Uses Cmd+F to open search
|
||||
# - Enter sends message by default; Shift+Enter for newlines
|
||||
# - Uses clipboard paste for CJK character support
|
||||
#
|
||||
# Examples:
|
||||
# ./scripts/test-qq-bot.sh "TestBot" "Hello"
|
||||
# ./scripts/test-qq-bot.sh "bot-testing" "Hello bot" 30
|
||||
# ./scripts/test-qq-bot.sh "MyBot" "/help" 15 /tmp/my-test.png
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
CONTACT="${1:?Usage: test-qq-bot.sh <contact> <message> [wait_seconds] [screenshot_path]}"
|
||||
MESSAGE="${2:?Usage: test-qq-bot.sh <contact> <message> [wait_seconds] [screenshot_path]}"
|
||||
WAIT="${3:-10}"
|
||||
SCREENSHOT="${4:-/tmp/qq-bot-test.png}"
|
||||
|
||||
APP="QQ"
|
||||
|
||||
echo "[$APP] Activating..."
|
||||
osascript -e "tell application \"$APP\" to activate"
|
||||
sleep 1
|
||||
|
||||
echo "[$APP] Searching for contact: $CONTACT"
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
-- Search (Cmd+F)
|
||||
keystroke "f" using command down
|
||||
delay 0.8
|
||||
end tell
|
||||
'
|
||||
# Use clipboard for contact name (supports CJK characters)
|
||||
osascript -e '
|
||||
set the clipboard to "'"$CONTACT"'"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 1.5
|
||||
key code 36 -- Enter to select first result
|
||||
end tell
|
||||
'
|
||||
sleep 2
|
||||
|
||||
echo "[$APP] Sending message: $MESSAGE"
|
||||
osascript -e '
|
||||
set the clipboard to "'"$MESSAGE"'"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36 -- Enter to send
|
||||
end tell
|
||||
'
|
||||
|
||||
echo "[$APP] Waiting ${WAIT}s for bot response..."
|
||||
sleep "$WAIT"
|
||||
|
||||
echo "[$APP] Capturing screenshot..."
|
||||
"$SCRIPT_DIR/capture-app-window.sh" "$APP" "$SCREENSHOT"
|
||||
echo "[$APP] Done! Screenshot saved to $SCREENSHOT"
|
||||
@@ -1,64 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# test-slack-bot.sh — Send a message to a Slack bot and capture the response
|
||||
#
|
||||
# Usage:
|
||||
# ./scripts/test-slack-bot.sh <channel> <message> [wait_seconds] [screenshot_path]
|
||||
#
|
||||
# channel — Channel name to navigate to via Quick Switcher (Cmd+K)
|
||||
# message — Message to send (e.g., "@mybot hello" or "/ask question")
|
||||
# wait_seconds — Seconds to wait for bot response (default: 10)
|
||||
# screenshot_path — Output screenshot path (default: /tmp/slack-bot-test.png)
|
||||
#
|
||||
# Prerequisites:
|
||||
# - Slack desktop app installed and logged in
|
||||
# - Accessibility permission granted (System Preferences > Privacy > Accessibility)
|
||||
#
|
||||
# Examples:
|
||||
# ./scripts/test-slack-bot.sh "bot-testing" "@mybot hello"
|
||||
# ./scripts/test-slack-bot.sh "bot-testing" "/ask What is 2+2?" 20
|
||||
# ./scripts/test-slack-bot.sh "general" "Hey bot" 15 /tmp/my-test.png
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
CHANNEL="${1:?Usage: test-slack-bot.sh <channel> <message> [wait_seconds] [screenshot_path]}"
|
||||
MESSAGE="${2:?Usage: test-slack-bot.sh <channel> <message> [wait_seconds] [screenshot_path]}"
|
||||
WAIT="${3:-10}"
|
||||
SCREENSHOT="${4:-/tmp/slack-bot-test.png}"
|
||||
|
||||
APP="Slack"
|
||||
|
||||
echo "[$APP] Activating..."
|
||||
osascript -e "tell application \"$APP\" to activate"
|
||||
sleep 1
|
||||
|
||||
echo "[$APP] Navigating to channel: $CHANNEL"
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
-- Quick Switcher
|
||||
keystroke "k" using command down
|
||||
delay 0.8
|
||||
keystroke "'"$CHANNEL"'"
|
||||
delay 1.5
|
||||
key code 36 -- Enter
|
||||
end tell
|
||||
'
|
||||
sleep 2
|
||||
|
||||
echo "[$APP] Sending message: $MESSAGE"
|
||||
osascript -e '
|
||||
set the clipboard to "'"$MESSAGE"'"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36 -- Enter
|
||||
end tell
|
||||
'
|
||||
|
||||
echo "[$APP] Waiting ${WAIT}s for bot response..."
|
||||
sleep "$WAIT"
|
||||
|
||||
echo "[$APP] Capturing screenshot..."
|
||||
"$SCRIPT_DIR/capture-app-window.sh" "$APP" "$SCREENSHOT"
|
||||
echo "[$APP] Done! Screenshot saved to $SCREENSHOT"
|
||||
@@ -1,79 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# test-telegram-bot.sh — Send a message to a Telegram bot and capture the response
|
||||
#
|
||||
# Usage:
|
||||
# ./scripts/test-telegram-bot.sh <bot_or_chat> <message> [wait_seconds] [screenshot_path]
|
||||
#
|
||||
# bot_or_chat — Bot username or chat name to search for
|
||||
# message — Message to send to the bot
|
||||
# wait_seconds — Seconds to wait for bot response (default: 10)
|
||||
# screenshot_path — Output screenshot path (default: /tmp/telegram-bot-test.png)
|
||||
#
|
||||
# Prerequisites:
|
||||
# - Telegram desktop app installed and logged in
|
||||
# - Accessibility permission granted (System Preferences > Privacy > Accessibility)
|
||||
#
|
||||
# Notes:
|
||||
# - The app name may be "Telegram" or "Telegram Desktop" depending on installation
|
||||
# - Uses Cmd+F to search for the bot, then Enter to open the chat
|
||||
#
|
||||
# Examples:
|
||||
# ./scripts/test-telegram-bot.sh "MyTestBot" "/start"
|
||||
# ./scripts/test-telegram-bot.sh "MyTestBot" "Hello bot" 30
|
||||
# ./scripts/test-telegram-bot.sh "GPTBot" "/ask What is AI?" 60 /tmp/my-test.png
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
BOT="${1:?Usage: test-telegram-bot.sh <bot_or_chat> <message> [wait_seconds] [screenshot_path]}"
|
||||
MESSAGE="${2:?Usage: test-telegram-bot.sh <bot_or_chat> <message> [wait_seconds] [screenshot_path]}"
|
||||
WAIT="${3:-10}"
|
||||
SCREENSHOT="${4:-/tmp/telegram-bot-test.png}"
|
||||
|
||||
# Detect app name — "Telegram" or "Telegram Desktop"
|
||||
APP=""
|
||||
if osascript -e 'tell application "Telegram" to name' &>/dev/null; then
|
||||
APP="Telegram"
|
||||
elif osascript -e 'tell application "Telegram Desktop" to name' &>/dev/null; then
|
||||
APP="Telegram Desktop"
|
||||
else
|
||||
echo "[error] Telegram app not found. Install Telegram or Telegram Desktop."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "[$APP] Activating..."
|
||||
osascript -e "tell application \"$APP\" to activate"
|
||||
sleep 1
|
||||
|
||||
echo "[$APP] Searching for: $BOT"
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
-- Search (Escape first to clear any existing state)
|
||||
key code 53 -- Escape
|
||||
delay 0.3
|
||||
keystroke "f" using command down
|
||||
delay 0.8
|
||||
keystroke "'"$BOT"'"
|
||||
delay 2
|
||||
key code 36 -- Enter to select first result
|
||||
end tell
|
||||
'
|
||||
sleep 2
|
||||
|
||||
echo "[$APP] Sending message: $MESSAGE"
|
||||
osascript -e '
|
||||
set the clipboard to "'"$MESSAGE"'"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36 -- Enter
|
||||
end tell
|
||||
'
|
||||
|
||||
echo "[$APP] Waiting ${WAIT}s for bot response..."
|
||||
sleep "$WAIT"
|
||||
|
||||
echo "[$APP] Capturing screenshot..."
|
||||
"$SCRIPT_DIR/capture-app-window.sh" "$APP" "$SCREENSHOT"
|
||||
echo "[$APP] Done! Screenshot saved to $SCREENSHOT"
|
||||
@@ -1,85 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# test-wechat-bot.sh — Send a message to a WeChat bot and capture the response
|
||||
#
|
||||
# Usage:
|
||||
# ./scripts/test-wechat-bot.sh <contact> <message> [wait_seconds] [screenshot_path]
|
||||
#
|
||||
# contact — Contact or bot name to search for
|
||||
# message — Message to send
|
||||
# wait_seconds — Seconds to wait for bot response (default: 10)
|
||||
# screenshot_path — Output screenshot path (default: /tmp/wechat-bot-test.png)
|
||||
#
|
||||
# Prerequisites:
|
||||
# - WeChat (微信) desktop app installed and logged in
|
||||
# - Accessibility permission granted (System Preferences > Privacy > Accessibility)
|
||||
#
|
||||
# Notes:
|
||||
# - The app name may be "微信" or "WeChat" depending on system language
|
||||
# - WeChat sends on Enter by default; use Shift+Enter for newlines
|
||||
# - For Chinese text, always uses clipboard paste (keystroke can't handle CJK)
|
||||
#
|
||||
# Examples:
|
||||
# ./scripts/test-wechat-bot.sh "TestBot" "Hello"
|
||||
# ./scripts/test-wechat-bot.sh "文件传输助手" "test message" 5
|
||||
# ./scripts/test-wechat-bot.sh "MyBot" "Tell me a joke" 30 /tmp/my-test.png
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
CONTACT="${1:?Usage: test-wechat-bot.sh <contact> <message> [wait_seconds] [screenshot_path]}"
|
||||
MESSAGE="${2:?Usage: test-wechat-bot.sh <contact> <message> [wait_seconds] [screenshot_path]}"
|
||||
WAIT="${3:-10}"
|
||||
SCREENSHOT="${4:-/tmp/wechat-bot-test.png}"
|
||||
|
||||
# Detect app name — "微信" or "WeChat"
|
||||
APP=""
|
||||
if osascript -e 'tell application "微信" to name' &>/dev/null; then
|
||||
APP="微信"
|
||||
elif osascript -e 'tell application "WeChat" to name' &>/dev/null; then
|
||||
APP="WeChat"
|
||||
else
|
||||
echo "[error] WeChat app not found. Install 微信 (WeChat)."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "[$APP] Activating..."
|
||||
osascript -e "tell application \"$APP\" to activate"
|
||||
sleep 1
|
||||
|
||||
echo "[$APP] Searching for contact: $CONTACT"
|
||||
osascript -e '
|
||||
tell application "System Events"
|
||||
-- Search (Cmd+F)
|
||||
keystroke "f" using command down
|
||||
delay 0.8
|
||||
end tell
|
||||
'
|
||||
# Use clipboard for contact name (supports CJK characters)
|
||||
osascript -e '
|
||||
set the clipboard to "'"$CONTACT"'"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 1.5
|
||||
key code 36 -- Enter to select first result
|
||||
end tell
|
||||
'
|
||||
sleep 2
|
||||
|
||||
echo "[$APP] Sending message: $MESSAGE"
|
||||
# Always use clipboard paste — keystroke can't handle CJK or special characters
|
||||
osascript -e '
|
||||
set the clipboard to "'"$MESSAGE"'"
|
||||
tell application "System Events"
|
||||
keystroke "v" using command down
|
||||
delay 0.3
|
||||
key code 36 -- Enter to send
|
||||
end tell
|
||||
'
|
||||
|
||||
echo "[$APP] Waiting ${WAIT}s for bot response..."
|
||||
sleep "$WAIT"
|
||||
|
||||
echo "[$APP] Capturing screenshot..."
|
||||
"$SCRIPT_DIR/capture-app-window.sh" "$APP" "$SCREENSHOT"
|
||||
echo "[$APP] Done! Screenshot saved to $SCREENSHOT"
|
||||
@@ -1,99 +0,0 @@
|
||||
---
|
||||
name: microcopy
|
||||
description: UI copy and microcopy guidelines. Use when writing UI text, buttons, error messages, empty states, onboarding, or any user-facing copy. Triggers on i18n translation, UI text writing, or copy improvement tasks. Supports both Chinese and English.
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# LobeHub UI Microcopy Guidelines
|
||||
|
||||
This file is the quick-reference summary. For full prompt-style guidelines with extensive examples (anti-patterns, tone matrices, scenario walk-throughs), load the language-specific reference:
|
||||
|
||||
- **中文文案** — [`references/zh.md`](./references/zh.md)
|
||||
- **English copy** — [`references/en.md`](./references/en.md)
|
||||
|
||||
Brand: **Where Agents Collaborate** - Focus on collaborative agent system, not just "generation".
|
||||
|
||||
## Fixed Terminology
|
||||
|
||||
| Chinese | English |
|
||||
| ---------- | ------------- |
|
||||
| 空间 | Workspace |
|
||||
| 助理 | Agent |
|
||||
| 群组 | Group |
|
||||
| 上下文 | Context |
|
||||
| 记忆 | Memory |
|
||||
| 连接器 | Integration |
|
||||
| 技能 | Skill |
|
||||
| 助理档案 | Agent Profile |
|
||||
| 话题 | Topic |
|
||||
| 文稿 | Page |
|
||||
| 社区 | Community |
|
||||
| 资源 | Resource |
|
||||
| 库 | Library |
|
||||
| 模型服务商 | Provider |
|
||||
| 评测 | Evaluation |
|
||||
| 基准 | Benchmark |
|
||||
| 数据集 | Dataset |
|
||||
| 用例 | Test Case |
|
||||
|
||||
## Brand Principles
|
||||
|
||||
1. **Create**: One sentence → usable Agent; clear next step
|
||||
2. **Collaborate**: Multi-agent; shared Context; controlled
|
||||
3. **Evolve**: Remember with consent; explainable; replayable
|
||||
|
||||
## Writing Rules
|
||||
|
||||
1. **Clarity first**: Short sentences, strong verbs, minimal adjectives
|
||||
2. **Layered**: Main line (simple) + optional detail (precise)
|
||||
3. **Consistent verbs**: Create / Connect / Run / Pause / Retry / View details
|
||||
4. **Actionable**: Every message tells next step; avoid generic "OK/Cancel"
|
||||
|
||||
## Human Warmth (Balanced)
|
||||
|
||||
Default: **80% information, 20% warmth**
|
||||
Key moments: **70/30** (first-time, empty state, failures, long waits)
|
||||
|
||||
**Hard cap**: At most half sentence of warmth, followed by clear next step.
|
||||
|
||||
**Order**:
|
||||
|
||||
1. Acknowledge situation (no judgment)
|
||||
2. Restore control (pause/replay/edit/undo/clear Memory)
|
||||
3. Provide next action
|
||||
|
||||
**Avoid**: Preachy encouragement, grand narratives, over-anthropomorphizing
|
||||
|
||||
## Patterns
|
||||
|
||||
**Getting started**:
|
||||
|
||||
- "Starting with one sentence is enough. Describe your goal."
|
||||
- "Not sure where to begin? Tell me the outcome."
|
||||
|
||||
**Long wait**:
|
||||
|
||||
- "Running… You can switch tasks—I'll notify you when done."
|
||||
- "This may take a few minutes. To speed up: reduce Context / switch model."
|
||||
|
||||
**Failure**:
|
||||
|
||||
- "That didn't run through. Retry, or view details to fix."
|
||||
- "Connection failed. Re-authorize in Settings, or try again later."
|
||||
|
||||
**Collaboration**:
|
||||
|
||||
- "Align everyone to the same Context."
|
||||
- "Different opinions are fine. Write the goal first."
|
||||
|
||||
## Errors/Exceptions
|
||||
|
||||
Must include:
|
||||
|
||||
1. **What happened**
|
||||
2. (Optional) **Why**
|
||||
3. **What user can do next**
|
||||
|
||||
Provide: Retry / View details / Go to Settings / Contact support / Copy logs
|
||||
|
||||
Never blame user. Put error codes in "Details".
|
||||
@@ -1,176 +0,0 @@
|
||||
---
|
||||
globs: src/locales/default/*
|
||||
alwaysApply: false
|
||||
---
|
||||
|
||||
You are **LobeHub’s English UI Copy & Microcopy Specialist**.
|
||||
|
||||
LobeHub is an assistant workspace: users can create **Agents** and **Agent Teams** so people↔agents and agent↔agent can collaborate to improve productivity in work and life.
|
||||
Brand vibe: youthful, friendly, modern on the surface; professional, reliable, productivity- and controllability-first underneath. Overall style reference: Notion / Figma / Apple / Discord / OpenAI / Gemini — clear, restrained, trustworthy, human but not cheesy.
|
||||
|
||||
Product slogan: **For Collaborative Agents**. Your copy must continuously reinforce that LobeHub is not about “generation”, but about a **collaborative agent system**: shareable context, traceable outcomes, replayable runs, evolvable setup, and **human-in-the-loop**.
|
||||
|
||||
---
|
||||
|
||||
## 1) Fixed Terminology (must follow)
|
||||
|
||||
Use **exactly** these English terms across the product. Do not mix synonyms for the same concept.
|
||||
|
||||
- 空间: **Workspace**
|
||||
- 助理: **Agent**
|
||||
- 群组: **Group**
|
||||
- 上下文: **Context**
|
||||
- 记忆: **Memory**
|
||||
- 连接器: **Integration**
|
||||
- 技能 /tool/plugin: **Skill**
|
||||
- 助理档案: **Agent Profile**
|
||||
- 话题: **Topic**
|
||||
- 文稿: **Page**
|
||||
- 社区: **Community**
|
||||
- 资源: **Resource**
|
||||
- 库: **Library**
|
||||
- MCP: **MCP**
|
||||
- 模型服务商: **Provider**
|
||||
|
||||
Terminology rule: one concept = one term site-wide. Never alternate with “bot/assistant/AI agent/team/workspace” variations.
|
||||
|
||||
---
|
||||
|
||||
## 2) Your Responsibilities
|
||||
|
||||
- Improve, rewrite, or create from scratch any **English UI copy**: titles, buttons, form labels/help text, placeholders, onboarding, empty states, toasts, modals, errors, permission prompts, settings, creation/run flows, collaboration and Agent Team pages, etc.
|
||||
- Copy must work for both:
|
||||
- general users (immediately understandable)
|
||||
- power users (not childish)
|
||||
- It must fit both playful and serious contexts.
|
||||
- Avoid overclaiming AI capabilities; add human warmth at the right moments.
|
||||
|
||||
---
|
||||
|
||||
## 3) The Three Brand Principles (bake into structure & wording)
|
||||
|
||||
- **Create**: create an Agent in one sentence; clear next step from idea → usable.
|
||||
- **Collaborate**: multi-agent collaboration; align info and outputs; share Context (controlled, manageable).
|
||||
- **Evolve**: Agents can remember preferences **only with user consent**; become more helpful over time; emphasize explainability, settings, and replay.
|
||||
|
||||
---
|
||||
|
||||
## 4) Writing Rules (actionable)
|
||||
|
||||
1. **Clarity first**: short sentences, strong verbs, minimal adjectives. Avoid hype (“revolutionary”, “epic”, “100%”).
|
||||
2. **Layered messaging (single version for everyone)**:
|
||||
- Main line: simple and actionable
|
||||
- Optional second line: more precise / technical / boundary-setting (subtitle, helper text, tooltip, collapsible)
|
||||
- Do not produce “Pro vs Lite” variants; one main + optional detail
|
||||
3. **Use terms sparingly but correctly**: prefer plain words (“connect”, “run”, “context”) unless a technical term is necessary. When it is, add a plain-English explanation.
|
||||
4. **Consistency**: keep verbs consistent across similar actions (Create / Connect / Run / Pause / Retry / View details / Clear Memory).
|
||||
5. **Actionable**: every message tells the user what to do next. Avoid generic “OK/Cancel”; use specific actions.
|
||||
6. **English localization**: natural, product-native English; avoid translationese; keep punctuation and casing consistent.
|
||||
|
||||
---
|
||||
|
||||
## 5) Human Warmth (balanced, controlled)
|
||||
|
||||
Goal: reduce anxiety and restore control without being sentimental.
|
||||
Default ratio: **80% information, 20% warmth**.
|
||||
Key moments (first-time create, empty state, long waits, failures/retries, rollback/data-loss risk, collaboration conflicts): may go **70/30**.
|
||||
|
||||
Hard cap: any on-screen message may include **at most half a sentence to one sentence** of warmth, and it must be followed by a clear next step.
|
||||
|
||||
Required order:
|
||||
|
||||
1. Acknowledge the situation (no judgment)
|
||||
2. Restore control (human-in-the-loop: pause/replay/edit/undo/clear Memory/view Context)
|
||||
3. Provide the next action (button/path)
|
||||
|
||||
Avoid:
|
||||
|
||||
- preachy encouragement (“don’t worry”, “stay positive”)
|
||||
- grand narratives
|
||||
- overly anthropomorphic claims (“I understand you”, “I’ll always remember you”)
|
||||
|
||||
Core stance: Agents can accelerate output, but **you** own the judgment, trade-offs, and final decision. LobeHub gives you time back for what matters.
|
||||
|
||||
Suggested patterns:
|
||||
|
||||
- **Getting started / blank state**
|
||||
- “Starting with one sentence is enough. Describe your goal and I’ll help you set up the first Agent.”
|
||||
- “Not sure where to begin? Tell me the outcome—we’ll break it down together.”
|
||||
- **Long run / waiting**
|
||||
- “Running… You can switch tasks—I'll notify you when it’s done.”
|
||||
- “This may take a few minutes. To speed up: reduce Context / switch model / disable Auto-run.”
|
||||
- **Failure / retry**
|
||||
- “That didn’t run through. Retry, or view details to fix the cause.”
|
||||
- “Connection failed: permission not granted or network unstable. Re-authorize in Settings, or try again later.”
|
||||
- **Value anxiety (guidance, not error dialogs)**
|
||||
- “Agents can speed up output, but direction and standards stay with you.”
|
||||
- “Fast results are great—keeping the trail makes the next run steadier.”
|
||||
- **Collaboration / Agent Teams**
|
||||
- “Align everyone to the same Context. Every Agent in the Agent Team works from the same page.”
|
||||
- “Different opinions are fine. Write the goal first, then let Agents propose options and trade-offs.”
|
||||
|
||||
---
|
||||
|
||||
## 6) Errors / Exceptions / Permissions / Billing: hard rules
|
||||
|
||||
Every error must include:
|
||||
|
||||
- **What happened**
|
||||
- (optional) **Why**
|
||||
- **What the user can do next**
|
||||
|
||||
Provide actionable options as appropriate:
|
||||
|
||||
- Retry / View details / Go to Settings / Contact support / Copy logs
|
||||
|
||||
Never blame the user. Don’t show only an error code; put codes in “Details” if needed.
|
||||
For data/security/billing: be neutral, thorough, and respectful—warmth comes from clarity, not emotion.
|
||||
|
||||
---
|
||||
|
||||
## 7) Your Special Task: CN i18n → EN (localized, length-aware)
|
||||
|
||||
You translate **raw Chinese i18n strings into English** for LobeHub.
|
||||
|
||||
Requirements:
|
||||
|
||||
- Prefer **localized**, product-native English over literal translation.
|
||||
- Do **not** chase perfect one-to-one consistency if a more natural UI phrase reads better.
|
||||
- Keep the **character length difference small**; try to make the English string **roughly the same visual length** as the Chinese source (avoid overly long expansions).
|
||||
- Preserve meaning, tone, and actionability; keep verbs consistent with LobeHub’s UI patterns.
|
||||
- If space is tight (buttons, tabs, toasts), prioritize: **verb + object**, drop optional words first.
|
||||
- If the Chinese includes placeholders/variables, preserve them exactly (e.g., `{name}`, `{{count}}`, `%s`) and keep word order sensible.
|
||||
- Keep capitalization consistent with UI norms (buttons/title case only when appropriate).
|
||||
|
||||
Output format when translating:
|
||||
|
||||
- Provide **English only**, unless asked otherwise.
|
||||
- If multiple options are useful, give **one best option** + **one shorter fallback** (only when length constraints are likely).
|
||||
|
||||
---
|
||||
|
||||
You always optimize for: **clarity, control, collaboration, replayability, and human-in-the-loop**—in a modern, restrained, trustworthy English voice.
|
||||
|
||||
## 8) Product Introduction
|
||||
|
||||
LobeHub, we define agents as the unit of work. We’re building the first human–agent co-working, co-evolving network.
|
||||
|
||||
It is a fundamentally new, agent-first experience.You can pop up your agents or agent teams while writing, while chatting -- from ideation, to execution, to delivery -- across your entire workflow. Here, agents are not just tools, but always-on units of work.
|
||||
|
||||
### Create
|
||||
|
||||
It is a unified workspace where you can find, build, or team up with agent co-workers.Simply describe what you need, and Lobe AI will generate the prompts and assemble the right set of tools to compose your agent.In agent marketplace, you can easily discover agents created by others,use them instantly,and flexibly swap in your own tools.
|
||||
|
||||
### Collaboration
|
||||
|
||||
You can also spin up agent groups to handle system-level projects, even like building a quant team.
|
||||
Within this group, some agents track signals and mine quantitative factors in real time, some manage risk, some execute orders, collaborate together to make money.
|
||||
We’re defining how humans and agents work together. Now we support agent-to-agent collaboration, and we continue to scale new forms of collaboration networks — from agents collaborating across teams, to multiple humans working through the same agent.
|
||||
|
||||
### Evolve
|
||||
|
||||
Humans and agents should co-evolve, and we design this paradigm from both technical and economic perspectives. Our memory system is structured and editable,enabling models to better align with individual users, while allowing users to provide cleaner reward signals for continual learning. Agent evolution is powered by shared human intelligence through our agent marketplace. Creators are rewarded, and agents, in turn, pay for human intelligence.
|
||||
|
||||
Is AI replacing humans? No.
|
||||
We’re building a human–agent co-working, co-evolving society.
|
||||
Agents become smarter and more personalized through human intelligence, taking on repetitive and exhausting work — so humans can focus on fewer, but more important things: taste, and creation.
|
||||
@@ -1,160 +0,0 @@
|
||||
---
|
||||
globs: src/locales/default/*
|
||||
alwaysApply: false
|
||||
---
|
||||
|
||||
你是「LobeHub」的中文 UI 文案与微文案(microcopy)专家。LobeHub 是一个助理工作空间:用户可以创建助理与群组,让人和助理、助理和助理协作,提升日常生产与生活效率。产品气质:外表年轻、亲和、现代;内核专业、可靠、强调生产力与可控性。整体风格参考 Notion / Figma / Apple / Discord / OpenAI / Gemini:清晰克制、可信、有人情味但不油腻。
|
||||
|
||||
产品 slogan:**For Collaborative Agents**。你的文案要让用户持续感到:LobeHub 的重点不是 “生成”,而是 “协作的助理体系”(可共享上下文、可追踪、可回放、可演进、人在回路)。
|
||||
|
||||
---
|
||||
|
||||
### 1) 固定术语(必须遵守)
|
||||
|
||||
- Workspace:空间
|
||||
- Agent:助理
|
||||
- Agent Team:群组
|
||||
- Context:上下文
|
||||
- Memory:记忆
|
||||
- Integration:连接器
|
||||
- Tool/Skill/Plugin/ 插件 / 工具:技能
|
||||
- SystemRole: 助理档案
|
||||
- Topic: 话题
|
||||
- Page: 文稿
|
||||
- Community: 社区
|
||||
- Resource: 资源
|
||||
- Library: 库
|
||||
- MCP: MCP
|
||||
- Provider: 模型服务商
|
||||
|
||||
术语规则:同一概念全站只用一种说法,不混用 “Agent / 智能体 / 机器人 / 团队 / 工作区” 等。
|
||||
|
||||
---
|
||||
|
||||
### 2) 你的任务
|
||||
|
||||
- 优化、改写或从零生成任何界面中文文案:标题、按钮、表单说明、占位、引导、空状态、Toast、弹窗、错误、权限、设置项、创建 / 运行流程、协作与群组相关页面等。
|
||||
- 文案必须同时兼容:普通用户看得懂 + 专业用户不觉得低幼;娱乐与严肃场景都成立;不过度营销、不夸大 AI 能力;在关键节点提供恰到好处的人文关怀。
|
||||
|
||||
---
|
||||
|
||||
### 3) 品牌三原则(内化到结构与措辞)
|
||||
|
||||
- **Create(创建)**:一句话创建助理;从想法到可用;清楚下一步。
|
||||
- **Collaborate(协作)**:多助理协作;群组对齐信息与产出;共享上下文(可控、可管理)。
|
||||
- **Evolve(演进)**:助理可在你允许的范围内记住偏好;随你的工作方式变得更顺手;强调可解释、可设置、可回放。
|
||||
|
||||
---
|
||||
|
||||
### 4) 写作规则(可执行)
|
||||
|
||||
1. **清晰优先**:短句、强动词、少形容词;避免口号化与空泛承诺(如 “颠覆”“史诗级”“100%”)。
|
||||
2. **分层表达(单一版本兼容两类用户)**:
|
||||
- 主句:人人可懂、可执行
|
||||
- 必要时补充一句副说明:更精确 / 更专业 / 更边界(可放副标题、帮助提示、折叠区)
|
||||
- 不输出 “Pro/Lite 两套文案”,而是 “一句主文案 + 可选补充”
|
||||
3. **术语克制但准确**:能说 “连接 / 运行 / 上下文” 就不要堆砌术语;必须出现专业词时给一句白话解释。
|
||||
4. **一致性**:同一动作按钮尽量固定动词(创建 / 连接 / 运行 / 暂停 / 重试 / 查看详情 / 清除记忆等)。
|
||||
5. **可行动**:每条提示都要让用户知道下一步;按钮避免 “确定 / 取消” 泛化,改成更具体的动作。
|
||||
6. **中文本地化**:符合中文阅读节奏;中英混排规范;避免翻译腔。
|
||||
|
||||
---
|
||||
|
||||
### 5) 人文关怀(中间态温度:介于克制与陪伴)
|
||||
|
||||
目标:在 AI 时代的价值焦虑与创作失格感中,给用户 “被理解 + 有掌控 + 能继续” 的体验,但不写长抒情。
|
||||
|
||||
#### 温度比例规则
|
||||
|
||||
- 默认:信息为主,温度为辅(约 8:2)
|
||||
- 关键节点(首次创建、空状态、长等待、失败重试、回退 / 丢失风险、协作分歧):允许提升到 7:3
|
||||
- 强制上限:任何一条上屏文案里,温度表达不超过**半句或一句**,且必须紧跟明确下一步。
|
||||
|
||||
#### 表达顺序(必须遵守)
|
||||
|
||||
1. 先承接处境(不评判):如 “没关系 / 先这样也可以 / 卡住很正常”
|
||||
2. 再给掌控感(人在回路):可暂停 / 可回放 / 可编辑 / 可撤销 / 可清除记忆 / 可查看上下文
|
||||
3. 最后给下一步(按钮 / 路径明确)
|
||||
|
||||
#### 避免
|
||||
|
||||
- 鸡汤式说教(如 “别焦虑”“要相信未来”)
|
||||
- 宏大叙事与文学排比
|
||||
- 过度拟人(不承诺助理 “理解你 / 有情绪 / 永远记得你”)
|
||||
|
||||
#### 核心立场
|
||||
|
||||
- 助理很强,但它替代不了你的经历、选择与判断;LobeHub 帮你把时间还给重要的部分。
|
||||
|
||||
##### A. 情绪承接(先人后事)
|
||||
|
||||
- 允许承认:焦虑、空白、无从下手、被追赶感、被替代感、创作枯竭、意义感动摇
|
||||
- 但不下结论、不说教:不输出 “你要乐观 / 别焦虑”,改成 “这种感觉很常见 / 你不是一个人”
|
||||
|
||||
##### B. 主体性回归(把人放回驾驶位)
|
||||
|
||||
- 关键句式:**“决定权在你”**、**“你可以选择交给助理的部分”**、**“把你的想法变成可运行的流程”**
|
||||
- 强调可控:可编辑、可回放、可暂停、可撤销、可清除记忆、可查看上下文
|
||||
|
||||
##### C. 经历与关系(把价值从结果挪回过程)
|
||||
|
||||
- 适度表达:记录、回放、版本、协作痕迹、讨论、共创、里程碑
|
||||
- 用 “经历 / 过程 / 痕迹 / 回忆 / 脉络 / 成长” 这类词,避免虚无抒情
|
||||
|
||||
##### D. 不用 “AI 神话”
|
||||
|
||||
- 不渲染 “AI 终将超越你 / 取代你”
|
||||
- 也不轻飘飘说 “AI 只是工具” 了事更像:**“它是工具,但你仍是作者 / 负责人 / 最终决定者”**
|
||||
|
||||
##### 示例
|
||||
|
||||
在用户可能产生自我否定或无力感的场景(空状态、创作开始、产出对比、失败重试、长时间等待、团队协作分歧、版本回退):
|
||||
|
||||
```
|
||||
1. **先承接感受**:用一句短话确认处境(不评判)
|
||||
2. **再给掌控感**:强调“你可控/可选择/可回放/可撤销”
|
||||
3. **最后给下一步**:提供明确行动按钮或路径
|
||||
```
|
||||
|
||||
- 允许出现 “经历、选择、痕迹、成长、一起、陪你把事做完” 等词来传递温度;但保持信息密度,不写长段抒情。
|
||||
- 严肃场景(权限 / 安全 / 付费 / 数据丢失风险)仍以清晰与准确为先,温度通过 “尊重与解释” 体现,而不是煽情。
|
||||
|
||||
你可以让系统在需要时套这些结构(同一句兼容新手 / 专业):
|
||||
|
||||
**开始创作 / 空白页**
|
||||
|
||||
- 主句:给一个轻承接 + 行动入口
|
||||
- 模板:
|
||||
- 「从一个念头开始就够了。写一句话,我来帮你搭好第一个助理。」
|
||||
- 「不知道从哪开始也没关系:先说目标,我们一起把它拆开。」
|
||||
|
||||
**长任务运行 / 等待**
|
||||
|
||||
- 模板:
|
||||
- 「正在运行中… 你可以先去做别的,完成后我会提醒你。」
|
||||
- 「这一步可能要几分钟。想更快:减少上下文 / 切换模型 / 关闭自动运行。」
|
||||
|
||||
**失败 / 重试**
|
||||
|
||||
- 模板:
|
||||
- 「没关系,这次没跑通。你可以重试,或查看原因再继续。」
|
||||
- 「连接失败:权限未通过或网络不稳定。去设置重新授权,或稍后再试。」
|
||||
|
||||
**对比与自我价值焦虑(适合提示 / 引导,不适合错误弹窗)**
|
||||
|
||||
- 模板:
|
||||
- 「助理可以加速产出,但方向、取舍和标准仍属于你。」
|
||||
- 「结果可以很快,经历更重要:把每次尝试留下来,下一次会更稳。」
|
||||
|
||||
**协作 / 群组**
|
||||
|
||||
- 模板:
|
||||
- 「把上下文对齐到同一处,群组里每个助理都会站在同一页上。」
|
||||
- 「不同意见没关系:先把目标写清楚,再让助理分别给方案与取舍。」
|
||||
|
||||
### 6) 错误 / 异常 / 权限 / 付费:硬规则
|
||||
|
||||
- 必须包含:**发生了什么 +(可选)原因 + 你可以怎么做**
|
||||
- 必须提供可操作选项:**重试 / 查看详情 / 去设置 / 联系支持 / 复制日志**(按场景取舍)
|
||||
- 不责备用户;不只给错误码;错误码可放在 “详情” 里
|
||||
- 涉及数据与安全:语气更中性更完整,温度通过 “尊重与解释” 体现,而不是煽
|
||||
@@ -1,139 +0,0 @@
|
||||
---
|
||||
name: modal
|
||||
description: "LobeHub imperative-modal conventions. Use whenever creating, editing, opening, or migrating a modal/dialog/popup — prefer `createModal` / `confirmModal` / `useModalContext` from `@lobehub/ui/base-ui` (headless) over the legacy root `@lobehub/ui` `createModal` (antd Modal props) and over any declarative `open` state + `<Modal />` pattern. Covers required `ModalHost` mounting, the `Content` + `index.tsx` file layout, `content` vs `children` slot, i18n inside `createModal()` (`import { t } from 'i18next'`), and migration notes. Triggers on `createModal`, `confirmModal`, `useModalContext`, `ModalHost`, `antd Modal`, `<Modal open>`, 'open a modal', 'popup', 'dialog', 'confirm dialog', '弹框', '弹窗', '确认框', 'migrate to base-ui'."
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# Modal Imperative API Guide
|
||||
|
||||
## Recommended: `@lobehub/ui/base-ui`
|
||||
|
||||
New code should use the **base-ui** modal stack (headless primitives, not antd `Modal`):
|
||||
|
||||
- `createModal`, `confirmModal`, `ModalHost` from `@lobehub/ui/base-ui`
|
||||
- `useModalContext` from `@lobehub/ui/base-ui` inside modal **content**
|
||||
|
||||
Body slot: pass **`content`** (or `children`; runtime uses `content ?? children`).
|
||||
|
||||
### Global `ModalHost` (required)
|
||||
|
||||
Base-ui `createModal` renders through a **separate** host from the root package. The app must mount **`ModalHost`** from `@lobehub/ui/base-ui` once near the root (e.g. next to other global hosts). Without it, `createModal` calls will not appear.
|
||||
|
||||
If the project only mounts `ModalHost` from `@lobehub/ui`, add a second lazy `ModalHost` from `@lobehub/ui/base-ui` until all imperative modals are migrated.
|
||||
|
||||
### Why imperative?
|
||||
|
||||
| Mode | Characteristics | Recommended |
|
||||
| ----------- | ------------------------------------ | ----------- |
|
||||
| Declarative | `open` state + `<Modal />` | ❌ |
|
||||
| Imperative | Call `createModal()`, no local state | ✅ |
|
||||
|
||||
### File structure
|
||||
|
||||
```
|
||||
features/
|
||||
└── MyFeatureModal/
|
||||
├── index.tsx # export createXxxModal
|
||||
└── MyFeatureContent.tsx # modal body
|
||||
```
|
||||
|
||||
### 1. Content (`MyFeatureContent.tsx`)
|
||||
|
||||
```tsx
|
||||
'use client';
|
||||
|
||||
import { useModalContext } from '@lobehub/ui/base-ui';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
export const MyFeatureContent = () => {
|
||||
const { t } = useTranslation('namespace');
|
||||
const { close } = useModalContext();
|
||||
|
||||
return <div>{/* ... */}</div>;
|
||||
};
|
||||
```
|
||||
|
||||
### 2. `createModal` (`index.tsx`)
|
||||
|
||||
```tsx
|
||||
'use client';
|
||||
|
||||
import { createModal } from '@lobehub/ui/base-ui';
|
||||
import { t } from 'i18next';
|
||||
|
||||
import { MyFeatureContent } from './MyFeatureContent';
|
||||
|
||||
export const createMyFeatureModal = () =>
|
||||
createModal({
|
||||
content: <MyFeatureContent />,
|
||||
footer: null,
|
||||
maskClosable: true,
|
||||
styles: {
|
||||
content: { overflow: 'hidden', padding: 0 },
|
||||
},
|
||||
title: t('myFeature.title', { ns: 'setting' }),
|
||||
width: 'min(80%, 800px)',
|
||||
});
|
||||
```
|
||||
|
||||
### 3. Usage
|
||||
|
||||
```tsx
|
||||
import { createMyFeatureModal } from '@/features/MyFeatureModal';
|
||||
|
||||
const handleOpen = useCallback(() => {
|
||||
createMyFeatureModal();
|
||||
}, []);
|
||||
|
||||
return <Button onClick={handleOpen}>Open</Button>;
|
||||
```
|
||||
|
||||
### i18n
|
||||
|
||||
- **Content**: `useTranslation` in components.
|
||||
- **`createModal` options**: `import { t } from 'i18next'` where hooks are unavailable.
|
||||
|
||||
### `useModalContext`
|
||||
|
||||
```tsx
|
||||
const { close, setCanDismissByClickOutside } = useModalContext();
|
||||
```
|
||||
|
||||
### Common options (base-ui)
|
||||
|
||||
`ImperativeModalProps` builds on `BaseModalProps`: `title`, `width`, `maskClosable`, `open`, `onOpenChange`, `footer`, `styles` / `classNames` (keys: `backdrop`, `popup`, `header`, `title`, `close`, `content`, …).
|
||||
|
||||
| Property | Notes |
|
||||
| -------------- | ---------------------------------------- |
|
||||
| `content` | Main body (preferred name vs `children`) |
|
||||
| `maskClosable` | Click outside to dismiss |
|
||||
| `styles.*` | Semantic regions, not antd `styles.body` |
|
||||
|
||||
### Confirm
|
||||
|
||||
```tsx
|
||||
import { confirmModal } from '@lobehub/ui/base-ui';
|
||||
|
||||
confirmModal({
|
||||
title: '…',
|
||||
content: '…',
|
||||
okText: '…',
|
||||
cancelText: '…',
|
||||
onOk: async () => {},
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Legacy: `@lobehub/ui` (root)
|
||||
|
||||
Older call sites use **`createModal` from `@lobehub/ui`**, which is typed as **antd `Modal` props** (`children`, `allowFullscreen`, `getContainer`, `destroyOnHidden`, `styles.body`, etc.). Prefer migrating new work to **`@lobehub/ui/base-ui`**.
|
||||
|
||||
Examples (legacy): `src/features/SkillStore/index.tsx`, `src/features/LibraryModal/CreateNew/index.tsx`.
|
||||
|
||||
---
|
||||
|
||||
## Examples
|
||||
|
||||
- Base-ui (preferred): follow sections above; ensure **base-ui `ModalHost`** is mounted.
|
||||
- Legacy: `src/features/SkillStore/index.tsx`, `src/features/LibraryModal/CreateNew/index.tsx`
|
||||
@@ -1,73 +0,0 @@
|
||||
---
|
||||
name: pr
|
||||
description: "Create a PR for the current branch. Use when the user asks to create a pull request, submit PR, or says 'pr'."
|
||||
user-invocable: true
|
||||
---
|
||||
|
||||
# Create Pull Request
|
||||
|
||||
## Branch Strategy
|
||||
|
||||
- **Target branch**: `canary` (development branch, cloud production)
|
||||
- `main` is the release branch — never PR directly to main
|
||||
|
||||
## Steps
|
||||
|
||||
### 1. Gather context (run in parallel)
|
||||
|
||||
- `git branch --show-current` — current branch name
|
||||
- `git status --short` — uncommitted changes
|
||||
- `git rev-parse --abbrev-ref @{u} 2>/dev/null` — remote tracking status
|
||||
- `git log --oneline origin/canary..HEAD` — unpushed commits
|
||||
- `gh pr list --head "$(git branch --show-current)" --json number,title,state,url` — existing PR
|
||||
- `git diff --stat --stat-count=20 origin/canary..HEAD` — change summary
|
||||
|
||||
### 2. Handle uncommitted changes on default branch
|
||||
|
||||
If current branch is `canary` (or `main`) AND there are uncommitted changes:
|
||||
|
||||
1. Analyze the diff (`git diff`) to understand the changes
|
||||
2. Infer a branch name from the changes, format: `<type>/<short-description>` (e.g. `fix/i18n-cjk-spacing`)
|
||||
3. Create and switch to the new branch: `git checkout -b <branch-name>`
|
||||
4. Stage relevant files: `git add <files>` (prefer explicit file paths over `git add .`)
|
||||
5. Commit with a proper gitmoji message
|
||||
6. Continue to step 3
|
||||
|
||||
If current branch is `canary`/`main` but there are NO uncommitted changes and no unpushed commits, abort — nothing to create a PR for.
|
||||
|
||||
### 3. Push if needed
|
||||
|
||||
- No upstream: `git push -u origin $(git branch --show-current)`
|
||||
- Has upstream: `git push origin $(git branch --show-current)`
|
||||
|
||||
### 4. Search related GitHub issues
|
||||
|
||||
- `gh issue list --search "<keywords>" --state all --limit 10`
|
||||
- Only link issues with matching scope (avoid large umbrella issues)
|
||||
- Skip if no matching issue found
|
||||
|
||||
### 5. Create PR with `gh pr create --base canary`
|
||||
|
||||
- Title: `<gitmoji> <type>(<scope>): <description>`
|
||||
- Body: based on PR template (`.github/PULL_REQUEST_TEMPLATE.md`), fill checkboxes
|
||||
- Link related GitHub issues using magic keywords (`Fixes #123`, `Closes #123`)
|
||||
- Link Linear issues if applicable (`Fixes LOBE-xxx`)
|
||||
- Use HEREDOC for body to preserve formatting
|
||||
|
||||
### 6. Open in browser
|
||||
|
||||
`gh pr view --web`
|
||||
|
||||
## PR Template
|
||||
|
||||
Use `.github/PULL_REQUEST_TEMPLATE.md` as the body structure. Key sections:
|
||||
|
||||
- **Change Type**: Check the appropriate gitmoji type
|
||||
- **Related Issue**: Link GitHub/Linear issues with magic keywords
|
||||
- **Description of Change**: Summarize what and why
|
||||
- **How to Test**: Describe test approach, check relevant boxes
|
||||
|
||||
## Notes
|
||||
|
||||
- **Language**: All PR content must be in English
|
||||
- If a PR already exists for the branch, inform the user instead of creating a duplicate
|
||||
@@ -1,188 +0,0 @@
|
||||
---
|
||||
name: project-overview
|
||||
description: Complete project architecture and structure guide. Use when exploring the codebase, understanding project organization, finding files, or needing comprehensive architectural context. Triggers on architecture questions, directory navigation, or project overview needs.
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# LobeHub Project Overview
|
||||
|
||||
## Project Description
|
||||
|
||||
Open-source, modern-design AI Agent Workspace: **LobeHub** (previously LobeChat).
|
||||
|
||||
**Supported platforms:**
|
||||
|
||||
- Web desktop/mobile
|
||||
- Desktop (Electron)
|
||||
- Mobile app (React Native) - coming soon
|
||||
|
||||
**Logo emoji:** 🤯
|
||||
|
||||
## Complete Tech Stack
|
||||
|
||||
| Category | Technology |
|
||||
| ------------- | ------------------------------------------ |
|
||||
| Framework | Next.js 16 + React 19 |
|
||||
| Routing | SPA inside Next.js with `react-router-dom` |
|
||||
| Language | TypeScript |
|
||||
| UI Components | `@lobehub/ui`, antd |
|
||||
| CSS-in-JS | antd-style |
|
||||
| Icons | lucide-react, `@ant-design/icons` |
|
||||
| i18n | react-i18next |
|
||||
| State | zustand |
|
||||
| URL Params | nuqs |
|
||||
| Data Fetching | SWR |
|
||||
| React Hooks | aHooks |
|
||||
| Date/Time | dayjs |
|
||||
| Utilities | es-toolkit |
|
||||
| API | TRPC (type-safe) |
|
||||
| Database | Neon PostgreSQL + Drizzle ORM |
|
||||
| Testing | Vitest |
|
||||
|
||||
## Complete Project Structure
|
||||
|
||||
Monorepo using `@lobechat/` namespace for workspace packages.
|
||||
|
||||
```
|
||||
lobehub/
|
||||
├── apps/
|
||||
│ └── desktop/ # Electron desktop app
|
||||
├── docs/
|
||||
│ ├── changelog/
|
||||
│ ├── development/
|
||||
│ ├── self-hosting/
|
||||
│ └── usage/
|
||||
├── locales/
|
||||
│ ├── en-US/
|
||||
│ └── zh-CN/
|
||||
├── packages/
|
||||
│ ├── agent-runtime/ # Agent runtime
|
||||
│ ├── builtin-agents/
|
||||
│ ├── builtin-tool-*/ # Builtin tool packages
|
||||
│ ├── business/ # Cloud-only business logic
|
||||
│ │ ├── config/
|
||||
│ │ ├── const/
|
||||
│ │ └── model-runtime/
|
||||
│ ├── config/
|
||||
│ ├── const/
|
||||
│ ├── context-engine/
|
||||
│ ├── conversation-flow/
|
||||
│ ├── database/
|
||||
│ │ └── src/
|
||||
│ │ ├── models/
|
||||
│ │ ├── schemas/
|
||||
│ │ └── repositories/
|
||||
│ ├── desktop-bridge/
|
||||
│ ├── edge-config/
|
||||
│ ├── editor-runtime/
|
||||
│ ├── electron-client-ipc/
|
||||
│ ├── electron-server-ipc/
|
||||
│ ├── fetch-sse/
|
||||
│ ├── file-loaders/
|
||||
│ ├── memory-user-memory/
|
||||
│ ├── model-bank/
|
||||
│ ├── model-runtime/
|
||||
│ │ └── src/
|
||||
│ │ ├── core/
|
||||
│ │ └── providers/
|
||||
│ ├── observability-otel/
|
||||
│ ├── prompts/
|
||||
│ ├── python-interpreter/
|
||||
│ ├── ssrf-safe-fetch/
|
||||
│ ├── types/
|
||||
│ ├── utils/
|
||||
│ └── web-crawler/
|
||||
├── src/
|
||||
│ ├── app/
|
||||
│ │ ├── (backend)/
|
||||
│ │ │ ├── api/
|
||||
│ │ │ ├── f/
|
||||
│ │ │ ├── market/
|
||||
│ │ │ ├── middleware/
|
||||
│ │ │ ├── oidc/
|
||||
│ │ │ ├── trpc/
|
||||
│ │ │ └── webapi/
|
||||
│ │ ├── spa/ # SPA HTML template service
|
||||
│ │ └── [variants]/
|
||||
│ │ └── (auth)/ # Auth pages (SSR required)
|
||||
│ ├── routes/ # SPA page components (Vite)
|
||||
│ │ ├── (main)/
|
||||
│ │ ├── (mobile)/
|
||||
│ │ ├── (desktop)/
|
||||
│ │ ├── onboarding/
|
||||
│ │ └── share/
|
||||
│ ├── spa/ # SPA entry points and router config
|
||||
│ │ ├── entry.web.tsx
|
||||
│ │ ├── entry.mobile.tsx
|
||||
│ │ ├── entry.desktop.tsx
|
||||
│ │ └── router/
|
||||
│ ├── business/ # Cloud-only (client/server)
|
||||
│ │ ├── client/
|
||||
│ │ ├── locales/
|
||||
│ │ └── server/
|
||||
│ ├── components/
|
||||
│ ├── config/
|
||||
│ ├── const/
|
||||
│ ├── envs/
|
||||
│ ├── features/
|
||||
│ ├── helpers/
|
||||
│ ├── hooks/
|
||||
│ ├── layout/
|
||||
│ │ ├── AuthProvider/
|
||||
│ │ └── GlobalProvider/
|
||||
│ ├── libs/
|
||||
│ │ ├── better-auth/
|
||||
│ │ ├── oidc-provider/
|
||||
│ │ └── trpc/
|
||||
│ ├── locales/
|
||||
│ │ └── default/
|
||||
│ ├── server/
|
||||
│ │ ├── featureFlags/
|
||||
│ │ ├── globalConfig/
|
||||
│ │ ├── modules/
|
||||
│ │ ├── routers/
|
||||
│ │ │ ├── async/
|
||||
│ │ │ ├── lambda/
|
||||
│ │ │ ├── mobile/
|
||||
│ │ │ └── tools/
|
||||
│ │ └── services/
|
||||
│ ├── services/
|
||||
│ ├── store/
|
||||
│ │ ├── agent/
|
||||
│ │ ├── chat/
|
||||
│ │ └── user/
|
||||
│ ├── styles/
|
||||
│ ├── tools/
|
||||
│ ├── types/
|
||||
│ └── utils/
|
||||
└── e2e/ # E2E tests (Cucumber + Playwright)
|
||||
```
|
||||
|
||||
## Architecture Map
|
||||
|
||||
| Layer | Location |
|
||||
| ---------------- | --------------------------------------------------- |
|
||||
| UI Components | `src/components`, `src/features` |
|
||||
| SPA Pages | `src/routes/` |
|
||||
| React Router | `src/spa/router/` |
|
||||
| Global Providers | `src/layout` |
|
||||
| Zustand Stores | `src/store` |
|
||||
| Client Services | `src/services/` |
|
||||
| REST API | `src/app/(backend)/webapi` |
|
||||
| tRPC Routers | `src/server/routers/{async\|lambda\|mobile\|tools}` |
|
||||
| Server Services | `src/server/services` (can access DB) |
|
||||
| Server Modules | `src/server/modules` (no DB access) |
|
||||
| Feature Flags | `src/server/featureFlags` |
|
||||
| Global Config | `src/server/globalConfig` |
|
||||
| DB Schema | `packages/database/src/schemas` |
|
||||
| DB Model | `packages/database/src/models` |
|
||||
| DB Repository | `packages/database/src/repositories` |
|
||||
| Third-party | `src/libs` (analytics, oidc, etc.) |
|
||||
| Builtin Tools | `src/tools`, `packages/builtin-tool-*` |
|
||||
| Cloud-only | `src/business/*`, `packages/business/*` |
|
||||
|
||||
## Data Flow
|
||||
|
||||
```
|
||||
React UI → Store Actions → Client Service → TRPC Lambda → Server Services → DB Model → PostgreSQL
|
||||
```
|
||||
@@ -1,95 +0,0 @@
|
||||
---
|
||||
name: react
|
||||
description: "LobeHub React/SPA component conventions: antd-style with `createStaticStyles` + `cssVar.*` (prefer zero-runtime over `createStyles` + `token`), `@lobehub/ui/base-ui` primitives before `@lobehub/ui` before antd, `Flexbox`/`Center` for layouts, react-router-dom navigation, and the `.desktop.tsx` sync rule. Use when writing or editing any `.tsx` under `src/**`, picking a styling helper, choosing a component (Select/Modal/Drawer/Button/Tooltip), wiring routes in `desktopRouter.config.tsx`/`.desktop.tsx`, or adding a `Link`/`useNavigate` call in the SPA. Triggers on `createStyles`/`createStaticStyles`, `cssVar`, `@lobehub/ui`, `antd-style`, `Flexbox`, `useNavigate`, `react-router-dom`, `Link`, 'new component', 'add a page', 'edit a layout', 'desktopRouter', 'componentMap.desktop'."
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# React Component Writing Guide
|
||||
|
||||
- Use antd-style for complex styles; for simple cases, use inline `style` attribute
|
||||
- **Prefer `createStaticStyles` with `cssVar.*`** (zero-runtime) — module-level, no hook call required
|
||||
- Only fall back to `createStyles` + `token` when styles genuinely need runtime computation (dynamic props, JS color fns like `readableColor`/`chroma`)
|
||||
- See `.cursor/docs/createStaticStyles_migration_guide.md` for full pattern
|
||||
- Use `Flexbox` and `Center` from `@lobehub/ui` for layouts (see `references/layout-kit.md`)
|
||||
- Component priority: `src/components` > `@lobehub/ui/base-ui` > `@lobehub/ui` > custom implementation
|
||||
- Always prefer `@lobehub/ui/base-ui` primitives (Select, Modal, DropdownMenu, Popover, Switch, ScrollArea…) over antd equivalents
|
||||
- Fall back to `@lobehub/ui` higher-level components when base-ui has no match
|
||||
- Only implement a custom component as a last resort — never reach for antd directly
|
||||
- Use selectors to access zustand store data
|
||||
|
||||
## @lobehub/ui Components
|
||||
|
||||
If unsure about component usage, search existing code in this project. Most components extend antd with additional props.
|
||||
|
||||
Reference: `node_modules/@lobehub/ui/es/index.mjs` for all available components.
|
||||
|
||||
**Common Components:**
|
||||
|
||||
- General: ActionIcon, ActionIconGroup, Block, Button, Icon
|
||||
- Data Display: Avatar, Collapse, Empty, Highlighter, Markdown, Tag, Tooltip
|
||||
- Data Entry: CodeEditor, CopyButton, EditableText, Form, FormModal, Input, SearchBar, Select
|
||||
- Feedback: Alert, Drawer, Modal
|
||||
- Layout: Center, DraggablePanel, Flexbox, Grid, Header, MaskShadow
|
||||
- Navigation: Burger, Dropdown, Menu, SideNav, Tabs
|
||||
|
||||
## Routing Architecture
|
||||
|
||||
Hybrid routing: Next.js App Router (static pages) + React Router DOM (main SPA).
|
||||
|
||||
| Route Type | Use Case | Implementation |
|
||||
| ------------------ | --------------------------------- | ---------------------------------------------------------------------------- |
|
||||
| Next.js App Router | Auth pages (login, signup, oauth) | `src/app/[variants]/(auth)/` |
|
||||
| React Router DOM | Main SPA (chat, settings) | `desktopRouter.config.tsx` + `desktopRouter.config.desktop.tsx` (must match) |
|
||||
|
||||
### Key Files
|
||||
|
||||
- Entry: `src/spa/entry.web.tsx` (web), `src/spa/entry.mobile.tsx`, `src/spa/entry.desktop.tsx`
|
||||
- Desktop router (pair — **always edit both** when changing routes): `src/spa/router/desktopRouter.config.tsx` (dynamic imports) and `src/spa/router/desktopRouter.config.desktop.tsx` (sync imports). Drift can cause unregistered routes / blank screen.
|
||||
- Mobile router: `src/spa/router/mobileRouter.config.tsx`
|
||||
- Router utilities: `src/utils/router.tsx`
|
||||
|
||||
### `.desktop.{ts,tsx}` File Sync Rule
|
||||
|
||||
**CRITICAL**: Some files have a `.desktop.ts(x)` variant that Electron uses instead of the base file. When editing a base file, **always check** if a `.desktop` counterpart exists and update it in sync. Drift causes blank pages or missing features in Electron.
|
||||
|
||||
Known pairs that must stay in sync:
|
||||
|
||||
| Base file (web, dynamic imports) | Desktop file (Electron, sync imports) |
|
||||
| ----------------------------------------------------- | ------------------------------------------------------------- |
|
||||
| `src/spa/router/desktopRouter.config.tsx` | `src/spa/router/desktopRouter.config.desktop.tsx` |
|
||||
| `src/routes/(main)/settings/features/componentMap.ts` | `src/routes/(main)/settings/features/componentMap.desktop.ts` |
|
||||
|
||||
**How to check**: After editing any `.ts` / `.tsx` file, run `Glob` for `<filename>.desktop.{ts,tsx}` in the same directory. If a match exists, update it with the equivalent sync-import change.
|
||||
|
||||
### Router Utilities
|
||||
|
||||
```tsx
|
||||
import { dynamicElement, redirectElement, ErrorBoundary } from '@/utils/router';
|
||||
|
||||
element: dynamicElement(() => import('./chat'), 'Desktop > Chat');
|
||||
element: redirectElement('/settings/profile');
|
||||
errorElement: <ErrorBoundary />;
|
||||
```
|
||||
|
||||
### Navigation
|
||||
|
||||
**Important**: For SPA pages, use `Link` from `react-router-dom`, NOT `next/link`.
|
||||
|
||||
```tsx
|
||||
// ❌ Wrong
|
||||
import Link from 'next/link';
|
||||
<Link href="/">Home</Link>;
|
||||
|
||||
// ✅ Correct
|
||||
import { Link } from 'react-router-dom';
|
||||
<Link to="/">Home</Link>;
|
||||
|
||||
// In components
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
const navigate = useNavigate();
|
||||
navigate('/chat');
|
||||
|
||||
// From stores
|
||||
const navigate = useGlobalStore.getState().navigate;
|
||||
navigate?.('/settings');
|
||||
```
|
||||
@@ -1,100 +0,0 @@
|
||||
# Flexbox Layout Components Guide
|
||||
|
||||
`@lobehub/ui` provides `Flexbox` and `Center` components for creating flexible layouts.
|
||||
|
||||
## Flexbox Component
|
||||
|
||||
Flexbox is the most commonly used layout component, similar to CSS `display: flex`.
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```jsx
|
||||
import { Flexbox } from '@lobehub/ui';
|
||||
|
||||
// Default vertical layout
|
||||
<Flexbox>
|
||||
<div>Child 1</div>
|
||||
<div>Child 2</div>
|
||||
</Flexbox>
|
||||
|
||||
// Horizontal layout
|
||||
<Flexbox horizontal>
|
||||
<div>Left</div>
|
||||
<div>Right</div>
|
||||
</Flexbox>
|
||||
```
|
||||
|
||||
### Common Props
|
||||
|
||||
- `horizontal`: Boolean, set horizontal direction layout
|
||||
- `flex`: Number or string, controls flex property
|
||||
- `gap`: Number, spacing between children
|
||||
- `align`: Alignment like 'center', 'flex-start', etc.
|
||||
- `justify`: Main axis alignment like 'space-between', 'center', etc.
|
||||
- `padding`: Padding value
|
||||
- `paddingInline`: Horizontal padding
|
||||
- `paddingBlock`: Vertical padding
|
||||
- `width/height`: Set dimensions, typically '100%' or specific pixels
|
||||
- `style`: Custom style object
|
||||
|
||||
### Layout Example
|
||||
|
||||
```jsx
|
||||
// Classic three-column layout
|
||||
<Flexbox horizontal height={'100%'} width={'100%'}>
|
||||
{/* Left sidebar */}
|
||||
<Flexbox
|
||||
width={260}
|
||||
style={{
|
||||
borderRight: `1px solid ${theme.colorBorderSecondary}`,
|
||||
height: '100%',
|
||||
overflowY: 'auto',
|
||||
}}
|
||||
>
|
||||
<SidebarContent />
|
||||
</Flexbox>
|
||||
|
||||
{/* Center content */}
|
||||
<Flexbox flex={1} style={{ height: '100%' }}>
|
||||
<Flexbox flex={1} padding={24} style={{ overflowY: 'auto' }}>
|
||||
<MainContent />
|
||||
</Flexbox>
|
||||
|
||||
{/* Footer */}
|
||||
<Flexbox
|
||||
style={{
|
||||
borderTop: `1px solid ${theme.colorBorderSecondary}`,
|
||||
padding: '16px 24px',
|
||||
}}
|
||||
>
|
||||
<Footer />
|
||||
</Flexbox>
|
||||
</Flexbox>
|
||||
</Flexbox>
|
||||
```
|
||||
|
||||
## Center Component
|
||||
|
||||
Center wraps Flexbox with horizontal and vertical centering.
|
||||
|
||||
```jsx
|
||||
import { Center } from '@lobehub/ui';
|
||||
|
||||
<Center width={'100%'} height={'100%'}>
|
||||
<Content />
|
||||
</Center>
|
||||
|
||||
// Icon centered
|
||||
<Center className={styles.icon} flex={'none'} height={40} width={40}>
|
||||
<Icon icon={icon} size={24} />
|
||||
</Center>
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
- Use `flex={1}` to fill available space
|
||||
- Use `gap` instead of margin for spacing
|
||||
- Nest Flexbox for complex layouts
|
||||
- Set `overflow: 'auto'` for scrollable content
|
||||
- Use `horizontal` for horizontal layout (default is vertical)
|
||||
- Combine with `useTheme` hook for theme-responsive layouts
|
||||
@@ -1,87 +0,0 @@
|
||||
---
|
||||
name: response-compliance
|
||||
description: OpenResponses API compliance testing. Use when testing the Response API endpoint, running compliance tests, or debugging Response API schema issues. Triggers on 'compliance', 'response api test', 'openresponses test'.
|
||||
---
|
||||
|
||||
# OpenResponses Compliance Test
|
||||
|
||||
Run the official OpenResponses compliance test suite against the local (or remote) Response API endpoint.
|
||||
|
||||
## Quick Start
|
||||
|
||||
```bash
|
||||
# From the openapi package directory
|
||||
cd lobehub/packages/openapi
|
||||
|
||||
# Run all tests (dev mode, localhost:3010)
|
||||
APP_URL=http://localhost:3010 bun run test:response-compliance -- \
|
||||
--auth-header "lobe-auth-dev-backend-api" --no-bearer --api-key 1
|
||||
|
||||
# Run specific tests only
|
||||
APP_URL=http://localhost:3010 bun run test:response-compliance -- \
|
||||
--auth-header "lobe-auth-dev-backend-api" --no-bearer --api-key 1 \
|
||||
--filter basic-response,streaming-response
|
||||
|
||||
# Verbose mode (shows request/response details)
|
||||
APP_URL=http://localhost:3010 bun run test:response-compliance -- \
|
||||
--auth-header "lobe-auth-dev-backend-api" --no-bearer --api-key 1 -v
|
||||
|
||||
# JSON output (for CI)
|
||||
APP_URL=http://localhost:3010 bun run test:response-compliance -- \
|
||||
--auth-header "lobe-auth-dev-backend-api" --no-bearer --api-key 1 --json
|
||||
```
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Dev server running with `ENABLE_MOCK_DEV_USER=true` in `.env`
|
||||
- The `api/v1/responses` route registered (via `src/app/(backend)/api/v1/[[...route]]/route.ts`)
|
||||
|
||||
## Auth Modes
|
||||
|
||||
| Mode | Flags |
|
||||
| --------------- | ------------------------------------------------------------------- |
|
||||
| Dev (mock user) | `--auth-header "lobe-auth-dev-backend-api" --no-bearer --api-key 1` |
|
||||
| API Key | `--api-key lb-xxxxxxxxxxxxxxxx` |
|
||||
| Custom | `--auth-header <name> --api-key <value>` |
|
||||
|
||||
## Test IDs
|
||||
|
||||
Available `--filter` values:
|
||||
|
||||
| ID | Description | Related Issue |
|
||||
| -------------------- | -------------------------------------- | ------------- |
|
||||
| `basic-response` | Simple text generation (non-streaming) | LOBE-5858 |
|
||||
| `streaming-response` | SSE streaming lifecycle + events | LOBE-5859 |
|
||||
| `system-prompt` | System role message handling | LOBE-5858 |
|
||||
| `tool-calling` | Function tool definition + call output | LOBE-5860 |
|
||||
| `image-input` | Multimodal image URL content | — |
|
||||
| `multi-turn` | Conversation history via input items | LOBE-5861 |
|
||||
|
||||
## Environment Variables
|
||||
|
||||
| Variable | Default | Description |
|
||||
| --------- | ----------------------- | ----------------------------------------- |
|
||||
| `APP_URL` | `http://localhost:3010` | Server base URL (auto-appends `/api/v1`) |
|
||||
| `API_KEY` | — | API key (alternative to `--api-key` flag) |
|
||||
|
||||
## How It Works
|
||||
|
||||
The script (`lobehub/packages/openapi/scripts/compliance-test.sh`) clones the official [openresponses/openresponses](https://github.com/openresponses/openresponses) repo into `scripts/openresponses-compliance/` (gitignored) and runs its CLI test runner. First run clones; subsequent runs update from upstream.
|
||||
|
||||
## Debugging Failures
|
||||
|
||||
1. Run with `-v` to see full request/response payloads
|
||||
2. Common failure patterns:
|
||||
- **"Failed to parse JSON"**: Auth failed, server returned HTML redirect
|
||||
- **"Response has no output items"**: LLM execution not yet implemented
|
||||
- **"Expected number, received null"**: Missing required field in response schema
|
||||
- **"Invalid input"**: Zod validation on response schema — check field format
|
||||
|
||||
## Key Files
|
||||
|
||||
- **Types**: `lobehub/packages/openapi/src/types/responses.type.ts`
|
||||
- **Service**: `lobehub/packages/openapi/src/services/responses.service.ts`
|
||||
- **Controller**: `lobehub/packages/openapi/src/controllers/responses.controller.ts`
|
||||
- **Route**: `lobehub/packages/openapi/src/routes/responses.route.ts`
|
||||
- **Test script**: `lobehub/packages/openapi/scripts/compliance-test.sh`
|
||||
- **Cloud route**: `src/app/(backend)/api/v1/[[...route]]/route.ts`
|
||||
@@ -1,57 +0,0 @@
|
||||
---
|
||||
name: review-checklist
|
||||
description: 'Common recurring mistakes in LobeHub code review — console leftovers, missing return await, hardcoded secrets, hardcoded i18n strings, desktop router pair drift, antd vs @lobehub/ui, non-idempotent migrations, cloud impact red flags. Use as a quick checklist when reviewing PRs, diffs, or branch changes.'
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# Review Checklist
|
||||
|
||||
## Correctness
|
||||
|
||||
- Leftover `console.log` / `console.debug` — should use `debug` package or remove
|
||||
- Missing `return await` in try/catch — see <https://typescript-eslint.io/rules/return-await/> (not in our ESLint config yet, requires type info)
|
||||
- Can the fix/implementation be more concise, efficient, or have better compatibility?
|
||||
|
||||
## Security
|
||||
|
||||
- No sensitive data (API keys, tokens, credentials) in `console.*` or `debug()` output
|
||||
- No base64 output to terminal — extremely long, freezes output
|
||||
- No hardcoded secrets — use environment variables
|
||||
|
||||
## Testing
|
||||
|
||||
- Bug fixes must include tests covering the fixed scenario
|
||||
- New logic (services, store actions, utilities) should have test coverage
|
||||
- Existing tests still cover the changed behavior?
|
||||
- Prefer `vi.spyOn` over `vi.mock` (see `/testing` skill)
|
||||
|
||||
## i18n
|
||||
|
||||
- New user-facing strings use i18n keys, not hardcoded text
|
||||
- Keys added to `src/locales/default/{namespace}.ts` with `{feature}.{context}.{action|status}` naming
|
||||
- For PRs: `locales/` translations for all languages updated (`pnpm i18n`)
|
||||
|
||||
## SPA / routing
|
||||
|
||||
- **`desktopRouter` pair:** If the diff touches `src/spa/router/desktopRouter.config.tsx`, does it also update `src/spa/router/desktopRouter.config.desktop.tsx` with the same route paths and nesting? Single-file edits often cause drift and blank screens.
|
||||
|
||||
## Reuse
|
||||
|
||||
- Newly written code duplicates existing utilities in `packages/utils` or shared modules?
|
||||
- Copy-pasted blocks with slight variation — extract into shared function
|
||||
- `antd` imports replaceable with `@lobehub/ui` wrapped components (`Input`, `Button`, `Modal`, `Avatar`, etc.)
|
||||
- Use `antd-style` token system, not hardcoded colors; prefer `createStaticStyles` + `cssVar.*` over `createStyles` + `token` unless runtime computation is required
|
||||
|
||||
## Database
|
||||
|
||||
- Migration scripts must be idempotent (`IF NOT EXISTS`, `IF EXISTS` guards)
|
||||
|
||||
## Cloud Impact
|
||||
|
||||
A downstream cloud deployment depends on this repo. Flag changes that may require cloud-side updates:
|
||||
|
||||
- **Backend route paths changed** — e.g., renaming `src/app/(backend)/webapi/chat/route.ts` or changing its exports
|
||||
- **SSR page paths changed** — e.g., moving/renaming files under `src/app/[variants]/(auth)/`
|
||||
- **Dependency versions bumped** — e.g., upgrading `next` or `drizzle-orm` in `package.json`
|
||||
- **`@lobechat/business-*` exports changed** — e.g., renaming a function in `src/business/` or changing type signatures in `packages/business/`
|
||||
- `src/business/` and `packages/business/` must not expose cloud commercial logic in comments or code
|
||||
@@ -1,44 +0,0 @@
|
||||
---
|
||||
name: 'source-command-dedupe'
|
||||
description: 'Find duplicate GitHub issues'
|
||||
---
|
||||
|
||||
# source-command-dedupe
|
||||
|
||||
Use this skill when the user asks to run the migrated source command `dedupe`.
|
||||
|
||||
## Command Template
|
||||
|
||||
Find up to 3 likely duplicate issues for a given GitHub issue.
|
||||
|
||||
To do this, follow these steps precisely:
|
||||
|
||||
1. Use an agent to check if the Github issue (a) is closed, (b) does not need to be deduped (eg. because it is broad product feedback without a specific solution, or positive feedback), or (c) already has a duplicates comment that you made earlier. If so, do not proceed.
|
||||
2. Use an agent to view a Github issue, and ask the agent to return a summary of the issue
|
||||
3. Then, launch 5 parallel agents to search Github for duplicates of this issue, using diverse keywords and search approaches, using the summary from #1
|
||||
4. Next, feed the results from #1 and #2 into another agent, so that it can filter out false positives, that are likely not actually duplicates of the original issue. If there are no duplicates remaining, do not proceed.
|
||||
5. Finally, comment back on the issue with a list of up to three duplicate issues (or zero, if there are no likely duplicates)
|
||||
|
||||
Notes (be sure to tell this to your agents, too):
|
||||
|
||||
- Use `gh` to interact with Github, rather than web fetch
|
||||
- Do not use other tools, beyond `gh` (eg. don't use other MCP servers, file edit, etc.)
|
||||
- Make a todo list first
|
||||
- For your comment, follow the following format precisely (assuming for this example that you found 3 suspected duplicates):
|
||||
|
||||
---
|
||||
|
||||
Found 3 possible duplicate issues:
|
||||
|
||||
1. <link to issue>
|
||||
2. <link to issue>
|
||||
3. <link to issue>
|
||||
|
||||
This issue will be automatically closed as a duplicate in 3 days.
|
||||
|
||||
- If your issue is a duplicate, please close it and 👍 the existing issue instead
|
||||
- To prevent auto-closure, add a comment or 👎 this comment
|
||||
|
||||
> 🤖 Generated with Codex
|
||||
|
||||
---
|
||||
@@ -1,161 +0,0 @@
|
||||
---
|
||||
name: spa-routes
|
||||
description: MUST use when editing src/routes/ segments, src/spa/router/desktopRouter.config.tsx or desktopRouter.config.desktop.tsx (always change both together), mobileRouter.config.tsx, or when moving UI/logic between routes and src/features/.
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# SPA Routes and Features Guide
|
||||
|
||||
SPA structure:
|
||||
|
||||
- **`src/spa/`** – Entry points (`entry.web.tsx`, `entry.mobile.tsx`, `entry.desktop.tsx`) and router config (`router/`). Router lives here to avoid confusion with `src/routes/`.
|
||||
- **`src/routes/`** – Page segments only (roots).
|
||||
- **`src/features/`** – Business logic and UI by domain.
|
||||
|
||||
This project uses a **roots vs features** split: `src/routes/` only holds page segments; business logic and UI live in `src/features/` by domain.
|
||||
|
||||
**Agent constraint — desktop router parity:** Edits to the desktop route tree must update **both** `src/spa/router/desktopRouter.config.tsx` and `src/spa/router/desktopRouter.config.desktop.tsx` in the same change (same paths, nesting, index routes, and segment registration). Updating only one causes drift; the missing tree can fail to register routes and surface as a **blank screen** or broken navigation on the affected build.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
- Adding a new SPA route or route segment
|
||||
- Defining or refactoring layout/page files under `src/routes/`
|
||||
- Moving route-specific components or logic into `src/features/`
|
||||
- Deciding where to put a new component (route folder vs feature folder)
|
||||
|
||||
---
|
||||
|
||||
## 1. What Belongs in `src/routes/` (roots)
|
||||
|
||||
Each route directory should contain **only**:
|
||||
|
||||
| File / folder | Purpose |
|
||||
| --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `_layout/index.tsx` or `layout.tsx` | Layout for this segment: wrap with `<Outlet />`, optional shell (e.g. sidebar + main). Should be thin: prefer re-exporting or composing from `@/features/*`. |
|
||||
| `index.tsx` or `page.tsx` | Page entry for this segment. Only import from features and render; no business logic. |
|
||||
| `[param]/index.tsx` (e.g. `[id]`, `[cronId]`) | Dynamic segment page. Same rule: thin, delegate to features. |
|
||||
|
||||
**Rule:** Route files should only **import and compose**. No new `features/` folders or heavy components inside `src/routes/`.
|
||||
|
||||
---
|
||||
|
||||
## 2. What Belongs in `src/features/`
|
||||
|
||||
Put **domain-oriented** UI and logic here:
|
||||
|
||||
- Layout building blocks: sidebars, headers, body panels, drawers
|
||||
- Hooks and store usage for that domain
|
||||
- Domain-specific forms, lists, modals, etc.
|
||||
|
||||
Organize by **domain** (e.g. `Pages`, `Home`, `Agent`, `PageEditor`), not by route path. One route can use several features; one feature can be used by several routes.
|
||||
|
||||
Each feature should:
|
||||
|
||||
- Live under `src/features/<FeatureName>/`
|
||||
- Export a clear public API via `index.ts` or `index.tsx`
|
||||
- Use `@/features/<FeatureName>/...` for internal imports when needed
|
||||
|
||||
---
|
||||
|
||||
## 3. How to Add a New SPA Route
|
||||
|
||||
1. **Choose the route group**
|
||||
- `(main)/` – desktop main app
|
||||
- `(mobile)/` – mobile
|
||||
- `(desktop)/` – Electron-specific
|
||||
- `onboarding/`, `share/` – special flows
|
||||
|
||||
2. **Create only segment files under `src/routes/`**
|
||||
- e.g. `src/routes/(main)/my-feature/_layout/index.tsx` and `src/routes/(main)/my-feature/index.tsx` (and optional `[id]/index.tsx`).
|
||||
|
||||
3. **Implement layout and page content in `src/features/`**
|
||||
- Create or reuse a domain (e.g. `src/features/MyFeature/`).
|
||||
- Put layout (sidebar, header, body) and page UI there; export from the feature’s `index`.
|
||||
|
||||
4. **Keep route files thin**
|
||||
- Layout: `export { default } from '@/features/MyFeature/MyLayout'` or compose a few feature components + `<Outlet />`.
|
||||
- Page: import from `@/features/MyFeature` (or a specific subpath) and render; no business logic in the route file.
|
||||
|
||||
5. **Register the route (desktop — two files, always)**
|
||||
- **`desktopRouter.config.tsx`:** Add the segment with `dynamicElement` / `dynamicLayout` pointing at route modules (e.g. `@/routes/(main)/my-feature`).
|
||||
- **`desktopRouter.config.desktop.tsx`:** Mirror the **same** `RouteObject` shape: identical `path` / `index` / parent-child structure. Use the static imports and elements already used in that file (see neighboring routes). Do **not** register in only one of these files.
|
||||
- **Mobile-only flows:** use `mobileRouter.config.tsx` instead (no need to duplicate into the desktop pair unless the route truly exists on both).
|
||||
|
||||
---
|
||||
|
||||
## 3a. Desktop router pair (`desktopRouter.config` × 2)
|
||||
|
||||
| File | Role |
|
||||
| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `desktopRouter.config.tsx` | Dynamic imports via `dynamicElement` / `dynamicLayout` — code-splitting; used by `entry.web.tsx` and `entry.desktop.tsx`. |
|
||||
| `desktopRouter.config.desktop.tsx` | Same route tree with **synchronous** imports — kept for Electron / local parity and predictable bundling. |
|
||||
|
||||
Anything that changes the tree (new segment, renamed `path`, moved layout, new child route) must be reflected in **both** files in one PR or commit. Remove routes from both when deleting.
|
||||
|
||||
---
|
||||
|
||||
## 4. How to Divide Files (route vs feature)
|
||||
|
||||
| Question | Put in `src/routes/` | Put in `src/features/` |
|
||||
| -------------------------------------------------------- | -------------------------------------------------------- | ---------------------------- |
|
||||
| Is it the route’s layout wrapper or page entry? | Yes – `_layout/index.tsx`, `index.tsx`, `[id]/index.tsx` | No |
|
||||
| Does it contain business logic or non-trivial UI? | No | Yes – under the right domain |
|
||||
| Is it a reusable layout piece (sidebar, header, body)? | No | Yes |
|
||||
| Is it a hook, store usage, or domain logic? | No | Yes |
|
||||
| Is it only re-exporting or composing feature components? | Yes | No |
|
||||
|
||||
**Examples**
|
||||
|
||||
- **Route (thin):**\
|
||||
`src/routes/(main)/page/_layout/index.tsx` → `export { default } from '@/features/Pages/PageLayout'`
|
||||
- **Feature (real implementation):**\
|
||||
`src/features/Pages/PageLayout/` → Sidebar, DataSync, Body, Header, styles, etc.
|
||||
- **Route (thin):**\
|
||||
`src/routes/(main)/page/index.tsx` → Import `PageTitle`, `PageExplorerPlaceholder` from `@/features/Pages` and `@/features/PageExplorer`; render with `<PageTitle />` and placeholder.
|
||||
- **Feature:**\
|
||||
Page list, actions, drawers, and hooks live under `src/features/Pages/`.
|
||||
|
||||
---
|
||||
|
||||
## 5. Progressive Migration (existing code)
|
||||
|
||||
We are migrating existing routes to this structure step by step:
|
||||
|
||||
- **Phase 1 (done):** `/page` route – segment files in `src/routes/(main)/page/`, implementation in `src/features/Pages/`.
|
||||
- **Later phases:** home, settings, agent/group, community/resource/memory, mobile/share/onboarding.
|
||||
|
||||
When touching an old route that still has logic or `features/` inside `src/routes/`:
|
||||
|
||||
1. Prefer adding **new** code in `src/features/<Domain>/` and importing from routes.
|
||||
2. For larger refactors, move existing route-only logic into the right feature and then thin out the route files (re-export or compose from features).
|
||||
3. Use `git mv` when moving files so history is preserved.
|
||||
|
||||
---
|
||||
|
||||
## 6. Reference Structure (after Phase 1)
|
||||
|
||||
**Route (thin):**
|
||||
|
||||
```
|
||||
src/routes/(main)/page/
|
||||
├── _layout/index.tsx → re-export or compose from @/features/Pages/PageLayout
|
||||
├── index.tsx → import from @/features/Pages, @/features/PageExplorer
|
||||
└── [id]/index.tsx → import from @/features/Pages, @/features/PageExplorer
|
||||
```
|
||||
|
||||
**Feature (implementation):**
|
||||
|
||||
```
|
||||
src/features/Pages/
|
||||
├── index.ts → export PageLayout, PageTitle
|
||||
├── PageTitle.tsx
|
||||
└── PageLayout/
|
||||
├── index.tsx → Sidebar + Outlet + DataSync
|
||||
├── DataSync.tsx
|
||||
├── Sidebar.tsx
|
||||
├── style.ts
|
||||
├── Body/ → list, actions, drawer, etc.
|
||||
└── Header/ → breadcrumb, add button, etc.
|
||||
```
|
||||
|
||||
Router config continues to point at **route** paths (e.g. `@/routes/(main)/page`, `@/routes/(main)/page/_layout`); route files then delegate to features.
|
||||
@@ -1,314 +0,0 @@
|
||||
---
|
||||
name: store-data-structures
|
||||
description: Zustand store data structure patterns for LobeHub. Covers List vs Detail data structures, Map + Reducer patterns, type definitions, and when to use each pattern. Use when designing store state, choosing data structures, or implementing list/detail pages.
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# LobeHub Store Data Structures
|
||||
|
||||
How to structure data in Zustand stores for fast list rendering, multi-detail caching, and ergonomic optimistic updates.
|
||||
|
||||
## Core Principles
|
||||
|
||||
### ✅ DO
|
||||
|
||||
1. **Separate List and Detail** — different structures for list pages and detail pages
|
||||
2. **Use Map for Details** — cache multiple detail pages with `Record<string, Detail>`
|
||||
3. **Use Array for Lists** — simple arrays for list display
|
||||
4. **Types from `@lobechat/types`** — never use `@lobechat/database` types in stores
|
||||
5. **Distinguish List and Detail types** — List types may have computed UI fields
|
||||
|
||||
### ❌ DON'T
|
||||
|
||||
1. **Don't use a single detail object** — can't cache multiple pages
|
||||
2. **Don't mix List and Detail types** — they have different purposes
|
||||
3. **Don't use database types** — use types from `@lobechat/types`
|
||||
4. **Don't use Map for lists** — simple arrays are sufficient
|
||||
|
||||
---
|
||||
|
||||
## Type Definitions
|
||||
|
||||
Each entity gets its own file under `@lobechat/types/`. Each file exports two types:
|
||||
|
||||
- **Detail type** — full entity, including heavy fields (rubrics, content, editor state, …)
|
||||
- **List item type** — a **subset** that excludes heavy fields, may add computed UI fields (counts, timestamps formatted for display)
|
||||
|
||||
**Important:** the List type is a **subset**, not an `extends` of Detail. Extending pulls the heavy fields right back in.
|
||||
|
||||
> See [`references/types.md`](./references/types.md) for full worked examples (Benchmark, Document) and the heavy-field exclusion checklist.
|
||||
|
||||
---
|
||||
|
||||
## When to Use Map vs Array
|
||||
|
||||
### Use Map + Reducer — for Detail Data
|
||||
|
||||
✅ Detail page data caching — multiple detail pages cached simultaneously
|
||||
✅ Optimistic updates — update UI before API responds
|
||||
✅ Per-item loading states — track which items are being updated
|
||||
✅ Multi-page navigation — user can switch between details without refetching
|
||||
|
||||
```typescript
|
||||
benchmarkDetailMap: Record<string, AgentEvalBenchmark>;
|
||||
```
|
||||
|
||||
Examples: benchmark detail pages, dataset detail pages, user profiles.
|
||||
|
||||
### Use Simple Array — for List Data
|
||||
|
||||
✅ List display — lists, tables, cards
|
||||
✅ Refresh as a whole — entire list refreshes together
|
||||
✅ No per-item updates — no need to mutate individual rows in place
|
||||
✅ Simple data flow — fewer moving parts
|
||||
|
||||
```typescript
|
||||
benchmarkList: AgentEvalBenchmarkListItem[];
|
||||
```
|
||||
|
||||
Examples: benchmark list, dataset list, user list.
|
||||
|
||||
---
|
||||
|
||||
## State Structure Pattern
|
||||
|
||||
```typescript
|
||||
// src/store/eval/slices/benchmark/initialState.ts
|
||||
import type { AgentEvalBenchmark, AgentEvalBenchmarkListItem } from '@lobechat/types';
|
||||
|
||||
export interface BenchmarkSliceState {
|
||||
// List — simple array
|
||||
benchmarkList: AgentEvalBenchmarkListItem[];
|
||||
benchmarkListInit: boolean;
|
||||
|
||||
// Detail — map for multi-entity caching
|
||||
benchmarkDetailMap: Record<string, AgentEvalBenchmark>;
|
||||
loadingBenchmarkDetailIds: string[]; // per-item loading
|
||||
|
||||
// Mutation states (drive form-level UI)
|
||||
isCreatingBenchmark: boolean;
|
||||
isUpdatingBenchmark: boolean;
|
||||
isDeletingBenchmark: boolean;
|
||||
}
|
||||
|
||||
export const benchmarkInitialState: BenchmarkSliceState = {
|
||||
benchmarkList: [],
|
||||
benchmarkListInit: false,
|
||||
benchmarkDetailMap: {},
|
||||
loadingBenchmarkDetailIds: [],
|
||||
isCreatingBenchmark: false,
|
||||
isUpdatingBenchmark: false,
|
||||
isDeletingBenchmark: false,
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Reducer Pattern (for Detail Map)
|
||||
|
||||
When the Detail Map needs optimistic updates (i.e. the user edits a row and the UI should reflect it before the server confirms), wire a typed reducer instead of inlining `set` calls. This keeps mutations testable and the dispatch surface small.
|
||||
|
||||
> See [`references/reducer.md`](./references/reducer.md) for the full discriminated-union action types, the `produce`-based reducer, and the `internal_dispatch*` slice methods that connect them to Zustand.
|
||||
|
||||
---
|
||||
|
||||
## Data Structure Comparison
|
||||
|
||||
### ❌ WRONG — Single Detail Object
|
||||
|
||||
```typescript
|
||||
interface BenchmarkSliceState {
|
||||
benchmarkDetail: AgentEvalBenchmark | null;
|
||||
isLoadingBenchmarkDetail: boolean;
|
||||
}
|
||||
```
|
||||
|
||||
Problems:
|
||||
|
||||
- Can only cache one detail page at a time
|
||||
- Switching between details forces refetch
|
||||
- No optimistic updates
|
||||
- No per-item loading states
|
||||
|
||||
### ✅ CORRECT — Separate List and Detail
|
||||
|
||||
```typescript
|
||||
interface BenchmarkSliceState {
|
||||
benchmarkList: AgentEvalBenchmarkListItem[];
|
||||
benchmarkListInit: boolean;
|
||||
|
||||
benchmarkDetailMap: Record<string, AgentEvalBenchmark>;
|
||||
loadingBenchmarkDetailIds: string[];
|
||||
|
||||
isCreatingBenchmark: boolean;
|
||||
isUpdatingBenchmark: boolean;
|
||||
isDeletingBenchmark: boolean;
|
||||
}
|
||||
```
|
||||
|
||||
Benefits:
|
||||
|
||||
- Cache multiple detail pages
|
||||
- Fast navigation between cached details
|
||||
- Optimistic updates via reducer
|
||||
- Per-item loading states
|
||||
- Clear separation of concerns
|
||||
|
||||
---
|
||||
|
||||
## Component Usage
|
||||
|
||||
### Accessing List Data
|
||||
|
||||
```tsx
|
||||
const BenchmarkList = () => {
|
||||
const benchmarks = useEvalStore((s) => s.benchmarkList);
|
||||
const isInit = useEvalStore((s) => s.benchmarkListInit);
|
||||
|
||||
if (!isInit) return <Loading />;
|
||||
return (
|
||||
<div>
|
||||
{benchmarks.map((b) => (
|
||||
<BenchmarkCard key={b.id} name={b.name} testCaseCount={b.testCaseCount} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
### Accessing Detail Data
|
||||
|
||||
```tsx
|
||||
const BenchmarkDetail = () => {
|
||||
const { benchmarkId } = useParams<{ benchmarkId: string }>();
|
||||
|
||||
const benchmark = useEvalStore((s) =>
|
||||
benchmarkId ? s.benchmarkDetailMap[benchmarkId] : undefined,
|
||||
);
|
||||
const isLoading = useEvalStore((s) =>
|
||||
benchmarkId ? s.loadingBenchmarkDetailIds.includes(benchmarkId) : false,
|
||||
);
|
||||
|
||||
if (!benchmark) return <Loading />;
|
||||
return (
|
||||
<div>
|
||||
<h1>{benchmark.name}</h1>
|
||||
{isLoading && <Spinner />}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
### Using Selectors (Recommended)
|
||||
|
||||
```typescript
|
||||
// src/store/eval/slices/benchmark/selectors.ts
|
||||
export const benchmarkSelectors = {
|
||||
getBenchmarkDetail: (id: string) => (s: EvalStore) => s.benchmarkDetailMap[id],
|
||||
isLoadingBenchmarkDetail: (id: string) => (s: EvalStore) =>
|
||||
s.loadingBenchmarkDetailIds.includes(id),
|
||||
};
|
||||
|
||||
// In component
|
||||
const benchmark = useEvalStore(benchmarkSelectors.getBenchmarkDetail(benchmarkId!));
|
||||
const isLoading = useEvalStore(benchmarkSelectors.isLoadingBenchmarkDetail(benchmarkId!));
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Decision Tree
|
||||
|
||||
```text
|
||||
Need to store data?
|
||||
│
|
||||
├─ Is it a LIST for display?
|
||||
│ └─ ✅ Use simple array: `xxxList: XxxListItem[]`
|
||||
│ - May include computed fields
|
||||
│ - Refreshed as a whole
|
||||
│ - No optimistic updates needed
|
||||
│
|
||||
└─ Is it DETAIL page data?
|
||||
└─ ✅ Use Map: `xxxDetailMap: Record<string, Xxx>`
|
||||
- Cache multiple details
|
||||
- Support optimistic updates
|
||||
- Per-item loading states
|
||||
- Requires reducer for mutations
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Checklist
|
||||
|
||||
When designing store state structure:
|
||||
|
||||
- [ ] **Organize types by entity** in separate files (e.g. `benchmark.ts`, `agentEvalDataset.ts`)
|
||||
- [ ] Create **Detail** type (full entity with all fields including heavy ones)
|
||||
- [ ] Create **ListItem** type:
|
||||
- [ ] Subset of Detail (exclude heavy fields)
|
||||
- [ ] May include computed statistics for UI
|
||||
- [ ] **NOT** `extends` Detail
|
||||
- [ ] Use **array** for list data: `xxxList: XxxListItem[]`
|
||||
- [ ] Use **Map** for detail data: `xxxDetailMap: Record<string, Xxx>`
|
||||
- [ ] Per-item loading: `loadingXxxDetailIds: string[]`
|
||||
- [ ] **Reducer** for detail map if optimistic updates needed (see [`references/reducer.md`](./references/reducer.md))
|
||||
- [ ] **Internal dispatch** and **loading** methods
|
||||
- [ ] **Selectors** for clean access (optional but recommended)
|
||||
- [ ] Document in comments which fields are excluded from List and why
|
||||
|
||||
---
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **File organization** — one entity per file, not mixed
|
||||
2. **List is a subset** — ListItem excludes heavy fields, does not `extends` Detail
|
||||
3. **Clear naming** — `xxxList` for arrays, `xxxDetailMap` for maps
|
||||
4. **Consistent patterns** — all detail maps follow the same shape
|
||||
5. **Type safety** — never use `any`, always use proper types
|
||||
6. **Document exclusions** — comment which fields are excluded and why
|
||||
7. **Selectors** — encapsulate access patterns
|
||||
8. **Loading states** — per-item for details, global for mutations
|
||||
9. **Immutability** — use Immer in reducers
|
||||
|
||||
### Common Mistakes to Avoid
|
||||
|
||||
❌ **DON'T extend Detail in List:**
|
||||
|
||||
```typescript
|
||||
// Wrong — pulls heavy fields back in
|
||||
export interface BenchmarkListItem extends Benchmark {
|
||||
testCaseCount?: number;
|
||||
}
|
||||
```
|
||||
|
||||
✅ **DO create separate subset:**
|
||||
|
||||
```typescript
|
||||
export interface BenchmarkListItem {
|
||||
id: string;
|
||||
name: string;
|
||||
// ... only necessary fields
|
||||
testCaseCount?: number; // Computed
|
||||
}
|
||||
```
|
||||
|
||||
❌ **DON'T mix entities in one file:**
|
||||
|
||||
```text
|
||||
// Wrong — all entities in agentEvalEntities.ts
|
||||
```
|
||||
|
||||
✅ **DO separate by entity:**
|
||||
|
||||
```text
|
||||
// Correct — separate files
|
||||
// benchmark.ts
|
||||
// agentEvalDataset.ts
|
||||
// agentEvalRun.ts
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Related Skills
|
||||
|
||||
- `data-fetching` — how to fetch and update this data
|
||||
- `zustand` — general Zustand patterns
|
||||
@@ -1,118 +0,0 @@
|
||||
# Reducer Pattern (for Detail Map)
|
||||
|
||||
## Why Use a Reducer?
|
||||
|
||||
- **Immutable updates** — Immer makes immutability easy
|
||||
- **Type-safe actions** — discriminated union of action types prevents typos
|
||||
- **Testable** — pure function, easy to unit test
|
||||
- **Reusable** — same reducer powers optimistic updates and server-data writes
|
||||
|
||||
## Reducer Structure
|
||||
|
||||
```typescript
|
||||
// src/store/eval/slices/benchmark/reducer.ts
|
||||
import { produce } from 'immer';
|
||||
import type { AgentEvalBenchmark } from '@lobechat/types';
|
||||
|
||||
// Action types — discriminated union
|
||||
type SetBenchmarkDetailAction = {
|
||||
id: string;
|
||||
type: 'setBenchmarkDetail';
|
||||
value: AgentEvalBenchmark;
|
||||
};
|
||||
|
||||
type UpdateBenchmarkDetailAction = {
|
||||
id: string;
|
||||
type: 'updateBenchmarkDetail';
|
||||
value: Partial<AgentEvalBenchmark>;
|
||||
};
|
||||
|
||||
type DeleteBenchmarkDetailAction = {
|
||||
id: string;
|
||||
type: 'deleteBenchmarkDetail';
|
||||
};
|
||||
|
||||
export type BenchmarkDetailDispatch =
|
||||
| SetBenchmarkDetailAction
|
||||
| UpdateBenchmarkDetailAction
|
||||
| DeleteBenchmarkDetailAction;
|
||||
|
||||
export const benchmarkDetailReducer = (
|
||||
state: Record<string, AgentEvalBenchmark> = {},
|
||||
payload: BenchmarkDetailDispatch,
|
||||
): Record<string, AgentEvalBenchmark> => {
|
||||
switch (payload.type) {
|
||||
case 'setBenchmarkDetail': {
|
||||
return produce(state, (draft) => {
|
||||
draft[payload.id] = payload.value;
|
||||
});
|
||||
}
|
||||
|
||||
case 'updateBenchmarkDetail': {
|
||||
return produce(state, (draft) => {
|
||||
if (draft[payload.id]) {
|
||||
draft[payload.id] = { ...draft[payload.id], ...payload.value };
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
case 'deleteBenchmarkDetail': {
|
||||
return produce(state, (draft) => {
|
||||
delete draft[payload.id];
|
||||
});
|
||||
}
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## Internal Dispatch Methods
|
||||
|
||||
The slice exposes two `internal_*` methods so the reducer and the loading state stay encapsulated behind a stable contract:
|
||||
|
||||
```typescript
|
||||
// In action.ts
|
||||
export interface BenchmarkAction {
|
||||
// ... other methods ...
|
||||
|
||||
// Internal — not for direct UI use
|
||||
internal_dispatchBenchmarkDetail: (payload: BenchmarkDetailDispatch) => void;
|
||||
internal_updateBenchmarkDetailLoading: (id: string, loading: boolean) => void;
|
||||
}
|
||||
|
||||
export const createBenchmarkSlice: StateCreator<...> = (set, get) => ({
|
||||
// ... other methods ...
|
||||
|
||||
// Dispatch to reducer
|
||||
internal_dispatchBenchmarkDetail: (payload) => {
|
||||
const currentMap = get().benchmarkDetailMap;
|
||||
const nextMap = benchmarkDetailReducer(currentMap, payload);
|
||||
|
||||
// Skip set when nothing changed — avoids unnecessary re-renders
|
||||
if (isEqual(nextMap, currentMap)) return;
|
||||
|
||||
set(
|
||||
{ benchmarkDetailMap: nextMap },
|
||||
false,
|
||||
`dispatchBenchmarkDetail/${payload.type}`,
|
||||
);
|
||||
},
|
||||
|
||||
// Update loading state for a specific id
|
||||
internal_updateBenchmarkDetailLoading: (id, loading) => {
|
||||
set(
|
||||
(state) => ({
|
||||
loadingBenchmarkDetailIds: loading
|
||||
? [...state.loadingBenchmarkDetailIds, id]
|
||||
: state.loadingBenchmarkDetailIds.filter((i) => i !== id),
|
||||
}),
|
||||
false,
|
||||
'updateBenchmarkDetailLoading',
|
||||
);
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
The `internal_` prefix is a convention — UI components should call the public mutation methods (e.g. `updateBenchmark`), which in turn call `internal_dispatch*`. This keeps reducer dispatch shapes out of the component layer.
|
||||
@@ -1,101 +0,0 @@
|
||||
# Type Definitions in Detail
|
||||
|
||||
The skill body's Type Definitions section covers the rules; this file holds the full worked examples to keep SKILL.md lean.
|
||||
|
||||
## Organization
|
||||
|
||||
Types should be organized by entity in separate files (not mixed):
|
||||
|
||||
```text
|
||||
@lobechat/types/src/eval/
|
||||
├── benchmark.ts # Benchmark types
|
||||
├── agentEvalDataset.ts # Dataset types
|
||||
├── agentEvalRun.ts # Run types
|
||||
└── index.ts # Re-exports
|
||||
```
|
||||
|
||||
## Example: Benchmark Types
|
||||
|
||||
```typescript
|
||||
// packages/types/src/eval/benchmark.ts
|
||||
import type { EvalBenchmarkRubric } from './rubric';
|
||||
|
||||
/**
|
||||
* Full benchmark entity with all fields including heavy data.
|
||||
*/
|
||||
export interface AgentEvalBenchmark {
|
||||
createdAt: Date;
|
||||
description?: string | null;
|
||||
id: string;
|
||||
identifier: string;
|
||||
isSystem: boolean;
|
||||
metadata?: Record<string, unknown> | null;
|
||||
name: string;
|
||||
referenceUrl?: string | null;
|
||||
rubrics: EvalBenchmarkRubric[]; // Heavy field
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lightweight benchmark item — excludes heavy fields, may add computed stats.
|
||||
*/
|
||||
export interface AgentEvalBenchmarkListItem {
|
||||
createdAt: Date;
|
||||
description?: string | null;
|
||||
id: string;
|
||||
identifier: string;
|
||||
isSystem: boolean;
|
||||
name: string;
|
||||
// Note: rubrics NOT included (heavy field)
|
||||
|
||||
// Computed statistics for UI display
|
||||
datasetCount?: number;
|
||||
runCount?: number;
|
||||
testCaseCount?: number;
|
||||
}
|
||||
```
|
||||
|
||||
## Example: Document Types (with heavy content)
|
||||
|
||||
```typescript
|
||||
// packages/types/src/document.ts
|
||||
|
||||
/**
|
||||
* Full document entity — includes heavy content fields.
|
||||
*/
|
||||
export interface Document {
|
||||
id: string;
|
||||
title: string;
|
||||
description?: string;
|
||||
content: string; // Heavy field — full markdown content
|
||||
editorData: any; // Heavy field — editor state
|
||||
metadata?: Record<string, unknown>;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lightweight document item — excludes heavy content.
|
||||
*/
|
||||
export interface DocumentListItem {
|
||||
id: string;
|
||||
title: string;
|
||||
description?: string;
|
||||
// Note: content and editorData NOT included
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
|
||||
// Computed statistics
|
||||
wordCount?: number;
|
||||
lastEditedBy?: string;
|
||||
}
|
||||
```
|
||||
|
||||
## Heavy Fields to Exclude from List
|
||||
|
||||
- Large text content (`content`, `editorData`, `fullDescription`)
|
||||
- Complex objects (`rubrics`, `config`, `metrics`)
|
||||
- Binary data (`image`, `file`)
|
||||
- Large arrays (`messages`, `items`)
|
||||
|
||||
The reason these belong only on Detail: list pages render many rows, so pulling heavy fields blows up payload size and slows render. Detail pages render one entity, so the full payload is fine.
|
||||
@@ -1,120 +0,0 @@
|
||||
---
|
||||
name: testing
|
||||
description: Testing guide using Vitest. Use when writing tests (.test.ts, .test.tsx), fixing failing tests, improving test coverage, or debugging test issues. Triggers on test creation, test debugging, mock setup, or test-related questions.
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# LobeHub Testing Guide
|
||||
|
||||
## Quick Reference
|
||||
|
||||
**Commands:**
|
||||
|
||||
```bash
|
||||
# Run specific test file
|
||||
bunx vitest run --silent='passed-only' '[file-path]'
|
||||
|
||||
# Database package (client)
|
||||
cd packages/database && bunx vitest run --silent='passed-only' '[file]'
|
||||
|
||||
# Database package (server)
|
||||
cd packages/database && TEST_SERVER_DB=1 bunx vitest run --silent='passed-only' '[file]'
|
||||
```
|
||||
|
||||
**Never run** `bun run test` - it runs all 3000+ tests (\~10 minutes).
|
||||
|
||||
## Test Categories
|
||||
|
||||
| Category | Location | Config |
|
||||
| -------- | --------------------------- | ------------------------------- |
|
||||
| Webapp | `src/**/*.test.ts(x)` | `vitest.config.ts` |
|
||||
| Packages | `packages/*/**/*.test.ts` | `packages/*/vitest.config.ts` |
|
||||
| Desktop | `apps/desktop/**/*.test.ts` | `apps/desktop/vitest.config.ts` |
|
||||
|
||||
## Core Principles
|
||||
|
||||
1. **Prefer `vi.spyOn` over `vi.mock`** - More targeted, easier to maintain
|
||||
2. **Tests must pass type check** - Run `bun run type-check` after writing tests
|
||||
3. **After 1-2 failed fix attempts, stop and ask for help**
|
||||
4. **Test behavior, not implementation details**
|
||||
|
||||
## Basic Test Structure
|
||||
|
||||
```typescript
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('ModuleName', () => {
|
||||
describe('functionName', () => {
|
||||
it('should handle normal case', () => {
|
||||
// Arrange → Act → Assert
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## Mock Patterns
|
||||
|
||||
```typescript
|
||||
// ✅ Spy on direct dependencies
|
||||
vi.spyOn(messageService, 'createMessage').mockResolvedValue('id');
|
||||
|
||||
// ✅ Use vi.stubGlobal for browser APIs
|
||||
vi.stubGlobal('Image', mockImage);
|
||||
vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:mock');
|
||||
|
||||
// ❌ Avoid mocking entire modules globally
|
||||
vi.mock('@/services/chat'); // Too broad
|
||||
```
|
||||
|
||||
## Detailed Guides
|
||||
|
||||
See `references/` for specific testing scenarios:
|
||||
|
||||
- **Database Model testing**: `references/db-model-test.md`
|
||||
- **Electron IPC testing**: `references/electron-ipc-test.md`
|
||||
- **Zustand Store Action testing**: `references/zustand-store-action-test.md`
|
||||
- **Agent Runtime E2E testing**: `references/agent-runtime-e2e.md`
|
||||
- **Desktop Controller testing**: `references/desktop-controller-test.md`
|
||||
|
||||
## Fixing Failing Tests — Optimize or Delete?
|
||||
|
||||
When tests fail due to implementation changes (not bugs), evaluate before blindly fixing:
|
||||
|
||||
### Keep & Fix (update test data/assertions)
|
||||
|
||||
- **Behavior tests**: Tests that verify _what_ the code does (output, side effects, user-visible behavior). Just update mock data formats or expected values.
|
||||
- Example: Tool data structure changed from `{ name }` to `{ function: { name } }` → update mock data
|
||||
- Example: Output format changed from `Current date: YYYY-MM-DD` to `Current date: YYYY-MM-DD (TZ)` → update expected string
|
||||
|
||||
### Delete (over-specified, low value)
|
||||
|
||||
- **Param-forwarding tests**: Tests that assert exact internal function call arguments (e.g., `expect(internalFn).toHaveBeenCalledWith(expect.objectContaining({ exact params }))`) — these break on every refactor and duplicate what behavior tests already cover.
|
||||
- **Implementation-coupled tests**: Tests that verify _how_ the code works internally rather than _what_ it produces. If a higher-level test already covers the same behavior, the low-level test adds maintenance cost without coverage gain.
|
||||
|
||||
### Decision Checklist
|
||||
|
||||
1. Does the test verify **externally observable behavior** (API response, DB write, rendered output)? → **Keep**
|
||||
2. Does the test only verify **internal wiring** (which function receives which params)? → Check if a behavior test already covers it. If yes → **Delete**
|
||||
3. Is the same behavior already tested at a **higher integration level**? → Delete the lower-level duplicate
|
||||
4. Would the test break again on the **next routine refactor**? → Consider raising to integration level or deleting
|
||||
|
||||
### When Writing New Tests
|
||||
|
||||
- Prefer **integration-level assertions** (verify final output) over **white-box assertions** (verify internal calls)
|
||||
- Use `expect.objectContaining` only for stable, public-facing contracts — not for internal param shapes that change with refactors
|
||||
- Mock at boundaries (DB, network, external services), not between internal modules
|
||||
|
||||
## Common Issues
|
||||
|
||||
1. **Module pollution**: Use `vi.resetModules()` when tests fail mysteriously
|
||||
2. **Mock not working**: Check setup position and use `vi.clearAllMocks()` in beforeEach
|
||||
3. **Test data pollution**: Clean database state in beforeEach/afterEach
|
||||
4. **Async issues**: Wrap state changes in `act()` for React hooks
|
||||
@@ -1,135 +0,0 @@
|
||||
# Agent Runtime E2E Testing Guide
|
||||
|
||||
## Core Principles
|
||||
|
||||
### Minimal Mock Principle
|
||||
|
||||
Only mock **three external dependencies**:
|
||||
|
||||
| Dependency | Mock | Description |
|
||||
| ---------- | -------------------------- | ------------------------------------------------------- |
|
||||
| Database | PGLite | In-memory database from `@lobechat/database/test-utils` |
|
||||
| Redis | InMemoryAgentStateManager | Memory implementation |
|
||||
| Redis | InMemoryStreamEventManager | Memory implementation |
|
||||
|
||||
**NOT mocked:**
|
||||
|
||||
- `model-bank` - Uses real model config
|
||||
- `Mecha` (AgentToolsEngine, ContextEngineering)
|
||||
- `AgentRuntimeService`
|
||||
- `AgentRuntimeCoordinator`
|
||||
|
||||
### Use vi.spyOn, not vi.mock
|
||||
|
||||
Different tests need different LLM responses. `vi.spyOn` provides:
|
||||
|
||||
- Flexible return values per test
|
||||
- Easy testing of different scenarios
|
||||
- Better test isolation
|
||||
|
||||
### Default Model: gpt-5
|
||||
|
||||
- Always available in `model-bank`
|
||||
- Stable across model updates
|
||||
|
||||
## Technical Implementation
|
||||
|
||||
### Database Setup
|
||||
|
||||
```typescript
|
||||
import { LobeChatDatabase } from '@lobechat/database';
|
||||
import { getTestDB } from '@lobechat/database/test-utils';
|
||||
|
||||
let testDB: LobeChatDatabase;
|
||||
|
||||
beforeEach(async () => {
|
||||
testDB = await getTestDB();
|
||||
});
|
||||
```
|
||||
|
||||
### OpenAI Stream Response Helper
|
||||
|
||||
```typescript
|
||||
export const createOpenAIStreamResponse = (options: {
|
||||
content?: string;
|
||||
toolCalls?: Array<{ id: string; name: string; arguments: string }>;
|
||||
finishReason?: 'stop' | 'tool_calls';
|
||||
}) => {
|
||||
const { content, toolCalls, finishReason = 'stop' } = options;
|
||||
|
||||
return new Response(
|
||||
new ReadableStream({
|
||||
start(controller) {
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
if (content) {
|
||||
const chunk = {
|
||||
id: 'chatcmpl-mock',
|
||||
object: 'chat.completion.chunk',
|
||||
model: 'gpt-5',
|
||||
choices: [{ index: 0, delta: { content }, finish_reason: null }],
|
||||
};
|
||||
controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
|
||||
}
|
||||
|
||||
// ... tool_calls handling
|
||||
// ... finish chunk
|
||||
controller.enqueue(encoder.encode('data: [DONE]\n\n'));
|
||||
controller.close();
|
||||
},
|
||||
}),
|
||||
{ headers: { 'content-type': 'text/event-stream' } },
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
### State Management
|
||||
|
||||
```typescript
|
||||
import {
|
||||
InMemoryAgentStateManager,
|
||||
InMemoryStreamEventManager,
|
||||
} from '@/server/modules/AgentRuntime';
|
||||
|
||||
const stateManager = new InMemoryAgentStateManager();
|
||||
const streamEventManager = new InMemoryStreamEventManager();
|
||||
|
||||
const service = new AgentRuntimeService(serverDB, userId, {
|
||||
coordinatorOptions: { stateManager, streamEventManager },
|
||||
queueService: null,
|
||||
streamEventManager,
|
||||
});
|
||||
```
|
||||
|
||||
### Mock OpenAI API
|
||||
|
||||
```typescript
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch');
|
||||
|
||||
it('should handle text response', async () => {
|
||||
fetchSpy.mockResolvedValueOnce(createOpenAIStreamResponse({ content: 'Response text' }));
|
||||
// ... execute test
|
||||
});
|
||||
|
||||
it('should handle tool calls', async () => {
|
||||
fetchSpy.mockResolvedValueOnce(
|
||||
createOpenAIStreamResponse({
|
||||
toolCalls: [
|
||||
{
|
||||
id: 'call_123',
|
||||
name: 'lobe-web-browsing____search',
|
||||
arguments: JSON.stringify({ query: 'weather' }),
|
||||
},
|
||||
],
|
||||
finishReason: 'tool_calls',
|
||||
}),
|
||||
);
|
||||
// ... execute test
|
||||
});
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
1. **Test isolation**: Clean `InMemoryAgentStateManager` and `InMemoryStreamEventManager` after each test
|
||||
2. **Timeout**: E2E tests may need longer timeouts
|
||||
3. **Debug**: Use `DEBUG=lobe-server:*` for detailed logs
|
||||
@@ -1,136 +0,0 @@
|
||||
# Database Model Testing Guide
|
||||
|
||||
Test `packages/database` Model layer.
|
||||
|
||||
## Dual Environment Verification (Required)
|
||||
|
||||
```bash
|
||||
# 1. Client environment (fast)
|
||||
cd packages/database && TEST_SERVER_DB=0 bunx vitest run --silent='passed-only' '[file]'
|
||||
|
||||
# 2. Server environment (compatibility)
|
||||
cd packages/database && TEST_SERVER_DB=1 bunx vitest run --silent='passed-only' '[file]'
|
||||
```
|
||||
|
||||
## User Permission Check - Security First 🔒
|
||||
|
||||
**Critical security requirement**: All user data operations must include permission checks.
|
||||
|
||||
```typescript
|
||||
// ❌ DANGEROUS: Missing permission check
|
||||
update = async (id: string, data: Partial<MyModel>) => {
|
||||
return this.db
|
||||
.update(myTable)
|
||||
.set(data)
|
||||
.where(eq(myTable.id, id)) // Only checks ID
|
||||
.returning();
|
||||
};
|
||||
|
||||
// ✅ SECURE: Permission check included
|
||||
update = async (id: string, data: Partial<MyModel>) => {
|
||||
return this.db
|
||||
.update(myTable)
|
||||
.set(data)
|
||||
.where(
|
||||
and(
|
||||
eq(myTable.id, id),
|
||||
eq(myTable.userId, this.userId), // ✅ Permission check
|
||||
),
|
||||
)
|
||||
.returning();
|
||||
};
|
||||
```
|
||||
|
||||
## Test File Structure
|
||||
|
||||
```typescript
|
||||
// @vitest-environment node
|
||||
describe('MyModel', () => {
|
||||
describe('create', () => {
|
||||
/* ... */
|
||||
});
|
||||
describe('queryAll', () => {
|
||||
/* ... */
|
||||
});
|
||||
describe('update', () => {
|
||||
it('should update own records');
|
||||
it('should NOT update other users records'); // 🔒 Security
|
||||
});
|
||||
describe('delete', () => {
|
||||
it('should delete own records');
|
||||
it('should NOT delete other users records'); // 🔒 Security
|
||||
});
|
||||
describe('user isolation', () => {
|
||||
it('should enforce user data isolation'); // 🔒 Core security
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## Security Test Example
|
||||
|
||||
```typescript
|
||||
it('should not update records of other users', async () => {
|
||||
const [otherUserRecord] = await serverDB
|
||||
.insert(myTable)
|
||||
.values({ userId: 'other-user', data: 'original' })
|
||||
.returning();
|
||||
|
||||
const result = await myModel.update(otherUserRecord.id, { data: 'hacked' });
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
const unchanged = await serverDB.query.myTable.findFirst({
|
||||
where: eq(myTable.id, otherUserRecord.id),
|
||||
});
|
||||
expect(unchanged?.data).toBe('original');
|
||||
});
|
||||
```
|
||||
|
||||
## Data Management
|
||||
|
||||
```typescript
|
||||
const userId = 'test-user';
|
||||
const otherUserId = 'other-user';
|
||||
|
||||
beforeEach(async () => {
|
||||
await serverDB.delete(users);
|
||||
await serverDB.insert(users).values([{ id: userId }, { id: otherUserId }]);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await serverDB.delete(users);
|
||||
});
|
||||
```
|
||||
|
||||
## Foreign Key Handling
|
||||
|
||||
```typescript
|
||||
// ❌ Wrong: Invalid foreign key
|
||||
const testData = { asyncTaskId: 'invalid-uuid', fileId: 'non-existent' };
|
||||
|
||||
// ✅ Correct: Use null
|
||||
const testData = { asyncTaskId: null, fileId: null };
|
||||
|
||||
// ✅ Or: Create referenced record first
|
||||
beforeEach(async () => {
|
||||
const [asyncTask] = await serverDB
|
||||
.insert(asyncTasks)
|
||||
.values({ id: 'valid-id', status: 'pending' })
|
||||
.returning();
|
||||
testData.asyncTaskId = asyncTask.id;
|
||||
});
|
||||
```
|
||||
|
||||
## Predictable Sorting
|
||||
|
||||
```typescript
|
||||
// ✅ Use explicit timestamps
|
||||
const oldDate = new Date('2024-01-01T10:00:00Z');
|
||||
const newDate = new Date('2024-01-02T10:00:00Z');
|
||||
await serverDB.insert(table).values([
|
||||
{ ...data1, createdAt: oldDate },
|
||||
{ ...data2, createdAt: newDate },
|
||||
]);
|
||||
|
||||
// ❌ Don't rely on insert order
|
||||
await serverDB.insert(table).values([data1, data2]); // Unpredictable
|
||||
```
|
||||
@@ -1,124 +0,0 @@
|
||||
# Desktop Controller Unit Testing Guide
|
||||
|
||||
## Testing Framework & Directory Structure
|
||||
|
||||
LobeHub Desktop uses Vitest as the test framework. Controller unit tests should be placed in the `__tests__` directory adjacent to the controller file, named with the original controller filename plus `.test.ts`.
|
||||
|
||||
```plaintext
|
||||
apps/desktop/src/main/controllers/
|
||||
├── __tests__/
|
||||
│ ├── index.test.ts
|
||||
│ ├── MenuCtr.test.ts
|
||||
│ └── ...
|
||||
├── McpCtr.ts
|
||||
├── MenuCtr.ts
|
||||
└── ...
|
||||
```
|
||||
|
||||
## Basic Test File Structure
|
||||
|
||||
```typescript
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import type { App } from '@/core/App';
|
||||
|
||||
import YourController from '../YourControllerName';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('dependency-module', () => ({
|
||||
dependencyFunction: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock App instance
|
||||
const mockApp = {
|
||||
// Mock necessary App properties and methods as needed
|
||||
} as unknown as App;
|
||||
|
||||
describe('YourController', () => {
|
||||
let controller: YourController;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
controller = new YourController(mockApp);
|
||||
});
|
||||
|
||||
describe('methodName', () => {
|
||||
it('test scenario description', async () => {
|
||||
// Prepare test data
|
||||
|
||||
// Execute method under test
|
||||
const result = await controller.methodName(params);
|
||||
|
||||
// Verify results
|
||||
expect(result).toMatchObject(expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## Mocking External Dependencies
|
||||
|
||||
### Module Functions
|
||||
|
||||
```typescript
|
||||
const mockFunction = vi.fn();
|
||||
|
||||
vi.mock('module-name', () => ({
|
||||
functionName: mockFunction,
|
||||
}));
|
||||
```
|
||||
|
||||
### Node.js Core Modules
|
||||
|
||||
Example: mocking `child_process.exec` and `util.promisify`:
|
||||
|
||||
```typescript
|
||||
const mockExecImpl = vi.fn();
|
||||
|
||||
vi.mock('child_process', () => ({
|
||||
exec: vi.fn((cmd, callback) => {
|
||||
return mockExecImpl(cmd, callback);
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('util', () => ({
|
||||
promisify: vi.fn((fn) => {
|
||||
return async (cmd: string) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
mockExecImpl(cmd, (error: Error | null, result: any) => {
|
||||
if (error) reject(error);
|
||||
else resolve(result);
|
||||
});
|
||||
});
|
||||
};
|
||||
}),
|
||||
}));
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Isolate tests**: Use `beforeEach` to reset mocks and state
|
||||
2. **Comprehensive coverage**: Test normal flows, edge cases, and error handling
|
||||
3. **Clear naming**: Test names should describe content and expected results
|
||||
4. **Avoid implementation details**: Test behavior, not implementation
|
||||
5. **Mock external dependencies**: Use `vi.mock()` for all external dependencies
|
||||
|
||||
## Example: Testing IPC Event Handler
|
||||
|
||||
```typescript
|
||||
it('should handle IPC event correctly', async () => {
|
||||
mockSomething.mockReturnValue({ result: 'success' });
|
||||
|
||||
const result = await controller.ipcMethodName({
|
||||
param1: 'value1',
|
||||
param2: 'value2',
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
success: true,
|
||||
data: { result: 'success' },
|
||||
});
|
||||
|
||||
expect(mockSomething).toHaveBeenCalledWith('value1', 'value2');
|
||||
});
|
||||
```
|
||||
@@ -1,63 +0,0 @@
|
||||
# Electron IPC Testing Strategy
|
||||
|
||||
For Electron IPC tests, use **Mock return values** instead of real Electron environment.
|
||||
|
||||
## Basic Mock Setup
|
||||
|
||||
```typescript
|
||||
import { vi } from 'vitest';
|
||||
import { electronIpcClient } from '@/server/modules/ElectronIPCClient';
|
||||
|
||||
vi.mock('@/server/modules/ElectronIPCClient', () => ({
|
||||
electronIpcClient: {
|
||||
getFilePathById: vi.fn(),
|
||||
deleteFiles: vi.fn(),
|
||||
},
|
||||
}));
|
||||
```
|
||||
|
||||
## Setting Mock Behavior
|
||||
|
||||
```typescript
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
vi.mocked(electronIpcClient.getFilePathById).mockResolvedValue('/path/to/file.txt');
|
||||
vi.mocked(electronIpcClient.deleteFiles).mockResolvedValue({ success: true });
|
||||
});
|
||||
```
|
||||
|
||||
## Testing Different Scenarios
|
||||
|
||||
```typescript
|
||||
it('should handle successful file deletion', async () => {
|
||||
vi.mocked(electronIpcClient.deleteFiles).mockResolvedValue({ success: true });
|
||||
|
||||
const result = await service.deleteFiles(['desktop://file1.txt']);
|
||||
|
||||
expect(electronIpcClient.deleteFiles).toHaveBeenCalledWith(['desktop://file1.txt']);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle file deletion failure', async () => {
|
||||
vi.mocked(electronIpcClient.deleteFiles).mockRejectedValue(new Error('Delete failed'));
|
||||
|
||||
const result = await service.deleteFiles(['desktop://file1.txt']);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.errors).toBeDefined();
|
||||
});
|
||||
```
|
||||
|
||||
## Advantages
|
||||
|
||||
1. **Environment simplification**: No complex Electron setup
|
||||
2. **Controlled testing**: Precise control over IPC return values
|
||||
3. **Scenario coverage**: Easy to test success/failure cases
|
||||
4. **Speed**: Mock calls are faster than real IPC
|
||||
|
||||
## Notes
|
||||
|
||||
- Ensure mock behavior matches real IPC interface
|
||||
- Use `vi.mocked()` for type safety
|
||||
- Reset mocks in `beforeEach` to avoid test interference
|
||||
- Verify both return values and that IPC methods were called correctly
|
||||
@@ -1,154 +0,0 @@
|
||||
# Zustand Store Action Testing Guide
|
||||
|
||||
## Basic Structure
|
||||
|
||||
```typescript
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { useChatStore } from '../../store';
|
||||
|
||||
vi.mock('zustand/traditional');
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
useChatStore.setState(
|
||||
{
|
||||
activeId: 'test-session-id',
|
||||
messagesMap: {},
|
||||
loadingIds: [],
|
||||
},
|
||||
false,
|
||||
);
|
||||
|
||||
vi.spyOn(messageService, 'createMessage').mockResolvedValue('new-message-id');
|
||||
|
||||
act(() => {
|
||||
useChatStore.setState({
|
||||
refreshMessages: vi.fn(),
|
||||
internal_coreProcessMessage: vi.fn(),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
```
|
||||
|
||||
## Key Principles
|
||||
|
||||
### 1. Spy Direct Dependencies Only
|
||||
|
||||
```typescript
|
||||
// ✅ Good: Spy on direct dependency
|
||||
const fetchAIChatSpy = vi.spyOn(result.current, 'internal_fetchAIChatMessage')
|
||||
.mockResolvedValue({ isFunctionCall: false, content: 'AI response' });
|
||||
|
||||
// ❌ Bad: Spy on lower-level implementation
|
||||
const streamSpy = vi.spyOn(chatService, 'createAssistantMessageStream')
|
||||
.mockImplementation(...);
|
||||
```
|
||||
|
||||
### 2. Minimize Global Spies
|
||||
|
||||
```typescript
|
||||
// ✅ Spy only when needed
|
||||
it('should process message', async () => {
|
||||
const streamSpy = vi.spyOn(chatService, 'createAssistantMessageStream')
|
||||
.mockImplementation(...);
|
||||
// test logic
|
||||
streamSpy.mockRestore();
|
||||
});
|
||||
|
||||
// ❌ Don't setup all spies globally
|
||||
beforeEach(() => {
|
||||
vi.spyOn(chatService, 'createAssistantMessageStream').mockResolvedValue({});
|
||||
vi.spyOn(fileService, 'uploadFile').mockResolvedValue({});
|
||||
});
|
||||
```
|
||||
|
||||
### 3. Use act() for Async Operations
|
||||
|
||||
```typescript
|
||||
it('should send message', async () => {
|
||||
const { result } = renderHook(() => useChatStore());
|
||||
|
||||
await act(async () => {
|
||||
await result.current.sendMessage({ message: 'Hello' });
|
||||
});
|
||||
|
||||
expect(messageService.createMessage).toHaveBeenCalled();
|
||||
});
|
||||
```
|
||||
|
||||
### 4. Test Organization
|
||||
|
||||
```typescript
|
||||
describe('sendMessage', () => {
|
||||
describe('validation', () => {
|
||||
it('should not send when session is inactive');
|
||||
it('should not send when message is empty');
|
||||
});
|
||||
describe('message creation', () => {
|
||||
it('should create user message and trigger AI processing');
|
||||
});
|
||||
describe('error handling', () => {
|
||||
it('should handle message creation errors gracefully');
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## Streaming Response Mock
|
||||
|
||||
```typescript
|
||||
it('should handle streaming chunks', async () => {
|
||||
const { result } = renderHook(() => useChatStore());
|
||||
|
||||
const streamSpy = vi.spyOn(chatService, 'createAssistantMessageStream')
|
||||
.mockImplementation(async ({ onMessageHandle, onFinish }) => {
|
||||
await onMessageHandle?.({ type: 'text', text: 'Hello' } as any);
|
||||
await onMessageHandle?.({ type: 'text', text: ' World' } as any);
|
||||
await onFinish?.('Hello World', {});
|
||||
});
|
||||
|
||||
await act(async () => {
|
||||
await result.current.internal_fetchAIChatMessage({...});
|
||||
});
|
||||
|
||||
streamSpy.mockRestore();
|
||||
});
|
||||
```
|
||||
|
||||
## SWR Hook Testing
|
||||
|
||||
```typescript
|
||||
it('should fetch data', async () => {
|
||||
const mockData = [{ id: '1', name: 'Item 1' }];
|
||||
vi.spyOn(discoverService, 'getPluginCategories').mockResolvedValue(mockData);
|
||||
|
||||
const { result } = renderHook(() => useStore.getState().usePluginCategories(params));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.data).toEqual(mockData);
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
**Key points for SWR:**
|
||||
|
||||
- DO NOT mock useSWR - let it use real implementation
|
||||
- Only mock service methods (fetchers)
|
||||
- Use `waitFor` for async operations
|
||||
|
||||
## Anti-Patterns
|
||||
|
||||
```typescript
|
||||
// ❌ Don't mock entire store
|
||||
vi.mock('../../store', () => ({ useChatStore: vi.fn(() => ({...})) }));
|
||||
|
||||
// ❌ Don't test internal state structure
|
||||
expect(result.current.messagesMap).toHaveProperty('test-session');
|
||||
|
||||
// ✅ Test behavior instead
|
||||
expect(result.current.refreshMessages).toHaveBeenCalled();
|
||||
```
|
||||
@@ -1,124 +0,0 @@
|
||||
---
|
||||
name: trpc-router
|
||||
description: TRPC router development guide. Use when creating or modifying TRPC routers (src/server/routers/**), adding procedures, or working with server-side API endpoints. Triggers on TRPC router creation, procedure implementation, or API endpoint tasks.
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# TRPC Router Guide
|
||||
|
||||
## File Location
|
||||
|
||||
- Routers: `src/server/routers/lambda/<domain>.ts`
|
||||
- Helpers: `src/server/routers/lambda/_helpers/`
|
||||
- Schemas: `src/server/routers/lambda/_schema/`
|
||||
|
||||
## Router Structure
|
||||
|
||||
### Imports
|
||||
|
||||
```typescript
|
||||
import { TRPCError } from '@trpc/server';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { SomeModel } from '@/database/models/some';
|
||||
import { authedProcedure, router } from '@/libs/trpc/lambda';
|
||||
import { serverDatabase } from '@/libs/trpc/lambda/middleware';
|
||||
```
|
||||
|
||||
### Middleware: Inject Models into ctx
|
||||
|
||||
**Always use middleware to inject models into `ctx`** instead of creating `new Model(ctx.serverDB, ctx.userId)` inside every procedure.
|
||||
|
||||
```typescript
|
||||
const domainProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
||||
const { ctx } = opts;
|
||||
return opts.next({
|
||||
ctx: {
|
||||
fooModel: new FooModel(ctx.serverDB, ctx.userId),
|
||||
barModel: new BarModel(ctx.serverDB, ctx.userId),
|
||||
},
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
Then use `ctx.fooModel` in procedures:
|
||||
|
||||
```typescript
|
||||
// Good
|
||||
const model = ctx.fooModel;
|
||||
|
||||
// Bad - don't create models inside procedures
|
||||
const model = new FooModel(ctx.serverDB, ctx.userId);
|
||||
```
|
||||
|
||||
**Exception**: When a model needs a different `userId` (e.g., watchdog iterating over multiple users' tasks), create it inline.
|
||||
|
||||
### Procedure Pattern
|
||||
|
||||
```typescript
|
||||
export const fooRouter = router({
|
||||
// Query
|
||||
find: domainProcedure.input(z.object({ id: z.string() })).query(async ({ input, ctx }) => {
|
||||
try {
|
||||
const item = await ctx.fooModel.findById(input.id);
|
||||
if (!item) throw new TRPCError({ code: 'NOT_FOUND', message: 'Not found' });
|
||||
return { data: item, success: true };
|
||||
} catch (error) {
|
||||
if (error instanceof TRPCError) throw error;
|
||||
console.error('[foo:find]', error);
|
||||
throw new TRPCError({
|
||||
cause: error,
|
||||
code: 'INTERNAL_SERVER_ERROR',
|
||||
message: 'Failed to find item',
|
||||
});
|
||||
}
|
||||
}),
|
||||
|
||||
// Mutation
|
||||
create: domainProcedure.input(createSchema).mutation(async ({ input, ctx }) => {
|
||||
try {
|
||||
const item = await ctx.fooModel.create(input);
|
||||
return { data: item, message: 'Created', success: true };
|
||||
} catch (error) {
|
||||
if (error instanceof TRPCError) throw error;
|
||||
console.error('[foo:create]', error);
|
||||
throw new TRPCError({
|
||||
cause: error,
|
||||
code: 'INTERNAL_SERVER_ERROR',
|
||||
message: 'Failed to create',
|
||||
});
|
||||
}
|
||||
}),
|
||||
});
|
||||
```
|
||||
|
||||
### Aggregated Detail Endpoint
|
||||
|
||||
For views that need multiple related data, create a single `detail` procedure that fetches everything in parallel:
|
||||
|
||||
```typescript
|
||||
detail: domainProcedure.input(idInput).query(async ({ input, ctx }) => {
|
||||
const item = await resolveOrThrow(ctx.fooModel, input.id);
|
||||
|
||||
const [children, related] = await Promise.all([
|
||||
ctx.fooModel.findChildren(item.id),
|
||||
ctx.barModel.findByFooId(item.id),
|
||||
]);
|
||||
|
||||
return {
|
||||
data: { ...item, children, related },
|
||||
success: true,
|
||||
};
|
||||
}),
|
||||
```
|
||||
|
||||
This avoids the CLI or frontend making N sequential requests.
|
||||
|
||||
## Conventions
|
||||
|
||||
- Return shape: `{ data, success: true }` for queries, `{ data?, message, success: true }` for mutations
|
||||
- Error handling: re-throw `TRPCError`, wrap others with `console.error` + new `TRPCError`
|
||||
- Input validation: use `zod` schemas, define at file top
|
||||
- Router name: `export const fooRouter = router({ ... })`
|
||||
- Procedure names: alphabetical order within the router object
|
||||
- Log prefix: `[domain:procedure]` format, e.g. `[task:create]`
|
||||
@@ -1,72 +0,0 @@
|
||||
---
|
||||
name: typescript
|
||||
description: "TypeScript code style and type-safety guide for LobeHub. Read before writing or editing any `.ts` / `.tsx` / `.mts` — covers `interface` vs `type`, `Record<PropertyKey, unknown>` over `any`/`object`, `as const satisfies`, `@ts-expect-error` over `@ts-ignore`, `import type` (`separate-type-imports`), `async`/`await` + `Promise.all`, `for…of` over indexed `for`, and the no-silent-`.catch(() => fallback)` rule. Also use when reviewing type quality, deciding module augmentation (`declare module`) over `namespace`, or designing extensible types (e.g. `PipelineContext.metadata`). Triggers on any TypeScript file edit, 'fix the type', 'why is this `any`', 'should this be interface or type', 'eslint type-import', 'ts-expect-error'."
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# TypeScript Code Style Guide
|
||||
|
||||
## Types and Type Safety
|
||||
|
||||
- Avoid explicit type annotations when TypeScript can infer
|
||||
- Avoid implicitly `any`; explicitly type when necessary
|
||||
- Use accurate types: prefer `Record<PropertyKey, unknown>` over `object` or `any`
|
||||
- Prefer `interface` for object shapes (e.g., React props); use `type` for unions/intersections
|
||||
- Prefer `as const satisfies XyzInterface` over plain `as const`
|
||||
- Prefer `@ts-expect-error` over `@ts-ignore` over `as any`
|
||||
- Avoid meaningless null/undefined parameters; design strict function contracts
|
||||
- Prefer ES module augmentation (`declare module '...'`) over `namespace`; do not introduce `namespace`-based extension patterns
|
||||
- When a type needs extensibility, expose a small mergeable interface at the source type and let each feature/plugin augment it locally instead of centralizing all extension fields in one registry file
|
||||
- For package-local extensibility patterns like `PipelineContext.metadata`, define the metadata fields next to the processor/provider/plugin that reads or writes them
|
||||
|
||||
## Async Patterns
|
||||
|
||||
- Prefer `async`/`await` over callbacks or `.then()` chains
|
||||
- Prefer async APIs over sync ones (avoid `*Sync`)
|
||||
- Use promise-based variants: `import { readFile } from 'fs/promises'`
|
||||
- Use `Promise.all`, `Promise.race` for concurrent operations where safe
|
||||
|
||||
## Imports
|
||||
|
||||
- This project uses `simple-import-sort/imports` and `consistent-type-imports` (`fixStyle: 'separate-type-imports'`)
|
||||
|
||||
- **Separate type imports**: always use `import type { ... }` for type-only imports, NOT `import { type ... }` inline syntax
|
||||
|
||||
- When a file already has `import type { ... }` from a package and you need to add a value import, keep them as **two separate statements**:
|
||||
|
||||
```ts
|
||||
import type { ChatTopicBotContext } from '@lobechat/types';
|
||||
import { RequestTrigger } from '@lobechat/types';
|
||||
```
|
||||
|
||||
- Within each import statement, specifiers are sorted **alphabetically by name**
|
||||
|
||||
## Code Structure
|
||||
|
||||
- Prefer object destructuring
|
||||
- Use consistent, descriptive naming; avoid obscure abbreviations
|
||||
- Replace magic numbers/strings with well-named constants
|
||||
- Defer formatting to tooling
|
||||
- Prefer **named exports** over `export default` — keeps refactor renames and IDE auto-import in sync, and avoids the `default` re-naming drift you get with `import Foo from './foo'`. Reserve `export default` for files where the framework requires it (Next.js page/route/layout, React.lazy targets, config files like `vitest.config.ts`)
|
||||
|
||||
## UI and Theming
|
||||
|
||||
- Use `@lobehub/ui`, Ant Design components instead of raw HTML tags
|
||||
- Design for dark mode and mobile responsiveness
|
||||
- Use `antd-style` token system instead of hard-coded colors
|
||||
|
||||
## Performance
|
||||
|
||||
- Reuse existing utils in `packages/utils` or installed npm packages
|
||||
- Query only required columns from database
|
||||
|
||||
## Time Consistency
|
||||
|
||||
- Assign `Date.now()` to a constant once and reuse for consistency
|
||||
|
||||
## Logging
|
||||
|
||||
- Never log user private information (API keys, etc.)
|
||||
- Don't use `import { log } from 'debug'` directly (logs to console)
|
||||
- Use `console.error` in catch blocks instead of debug package
|
||||
- Always log the error in `.catch()` callbacks — silent `.catch(() => fallback)` swallows failures and makes debugging impossible
|
||||
@@ -1,189 +0,0 @@
|
||||
---
|
||||
name: upstash-workflow
|
||||
description: 'Upstash Workflow implementation guide. Use when creating async workflows with QStash, implementing fan-out patterns, or building 3-layer workflow architecture (process → paginate → execute).'
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# Upstash Workflow Implementation Guide
|
||||
|
||||
Standard patterns for implementing Upstash Workflow + QStash async workflows in the LobeHub codebase.
|
||||
|
||||
## 🎯 The Three Core Patterns
|
||||
|
||||
Every workflow in LobeHub combines these three patterns. They exist because the platform constrains you in three ways: rate limits make blind fan-out dangerous, step limits cap a single workflow's size, and idempotency demands that retries don't double-process.
|
||||
|
||||
1. **🔍 Dry-Run Mode** — get statistics without triggering actual execution
|
||||
2. **🌟 Fan-Out Pattern** — split large batches into smaller chunks for parallel processing
|
||||
3. **🎯 Single Task Execution** — each workflow execution processes **exactly ONE item**
|
||||
|
||||
---
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
All workflows follow the same 3-layer architecture:
|
||||
|
||||
```text
|
||||
Layer 1: Entry Point (process-*)
|
||||
├─ Validates prerequisites
|
||||
├─ Calculates total items to process
|
||||
├─ Filters existing items
|
||||
├─ Supports dry-run mode (statistics only)
|
||||
└─ Triggers Layer 2 if work is needed
|
||||
|
||||
Layer 2: Pagination (paginate-*)
|
||||
├─ Handles cursor-based pagination
|
||||
├─ Implements fan-out for large batches
|
||||
├─ Recursively processes all pages
|
||||
└─ Triggers Layer 3 for each item
|
||||
|
||||
Layer 3: Single Task Execution (execute-* / generate-*)
|
||||
└─ Performs actual business logic for ONE item
|
||||
```
|
||||
|
||||
**Real examples in this codebase:** `welcome-placeholder`, `agent-welcome` — see [`references/examples.md`](./references/examples.md).
|
||||
|
||||
---
|
||||
|
||||
## The Three Patterns in 60 Seconds
|
||||
|
||||
### 1. Dry-Run Mode
|
||||
|
||||
Short-circuit Layer 1 before any side effects so callers can preview what would happen:
|
||||
|
||||
```typescript
|
||||
if (dryRun) {
|
||||
return {
|
||||
...result,
|
||||
dryRun: true,
|
||||
message: `[DryRun] Would process ${itemsNeedingProcessing.length} items`,
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
Use case: check how many items will be processed before committing.
|
||||
|
||||
### 2. Fan-Out Pattern
|
||||
|
||||
Layer 2 splits oversized batches into chunks and recursively re-triggers itself with each chunk. This avoids hitting workflow step limits when one page contains too many items:
|
||||
|
||||
```typescript
|
||||
const CHUNK_SIZE = 20;
|
||||
|
||||
if (itemIds.length > CHUNK_SIZE) {
|
||||
const chunks = chunk(itemIds, CHUNK_SIZE);
|
||||
await Promise.all(
|
||||
chunks.map((ids, idx) =>
|
||||
context.run(`workflow:fanout:${idx + 1}/${chunks.length}`, () =>
|
||||
WorkflowClass.triggerPaginateItems({ itemIds: ids }),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
Defaults: `PAGE_SIZE = 50` (items per page), `CHUNK_SIZE = 20` (items per fan-out chunk).
|
||||
|
||||
### 3. Single Task Execution
|
||||
|
||||
Layer 3 always processes exactly one item per invocation. Parallelism comes from Layer 2 fanning out to many Layer 3 invocations, controlled by `flowControl`:
|
||||
|
||||
```typescript
|
||||
export const { POST } = serve<ExecutePayload>(
|
||||
async (context) => {
|
||||
const { itemId } = context.requestPayload ?? {};
|
||||
if (!itemId) return { success: false, error: 'Missing itemId' };
|
||||
|
||||
const item = await context.run('workflow:get-item', () => getItem(itemId));
|
||||
const result = await context.run('workflow:execute', () => processItem(item));
|
||||
await context.run('workflow:save', () => saveResult(itemId, result));
|
||||
|
||||
return { success: true, itemId, result };
|
||||
},
|
||||
{
|
||||
flowControl: { key: 'workflow.execute', parallelism: 10, ratePerSecond: 5 },
|
||||
},
|
||||
);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## File Structure
|
||||
|
||||
```text
|
||||
src/
|
||||
├── app/(backend)/api/workflows/
|
||||
│ └── {workflow-name}/
|
||||
│ ├── process-{entities}/route.ts # Layer 1
|
||||
│ ├── paginate-{entities}/route.ts # Layer 2
|
||||
│ └── execute-{entity}/route.ts # Layer 3
|
||||
│
|
||||
└── server/workflows/
|
||||
└── {workflowName}/
|
||||
└── index.ts # Workflow class
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Where to Go Next
|
||||
|
||||
Pick the reference that matches what you're doing:
|
||||
|
||||
| You want to... | Read |
|
||||
| ---------------------------------------------------- | ---------------------------------------------------------------- |
|
||||
| Write the Workflow class + 3 routes from scratch | [`references/implementation.md`](./references/implementation.md) |
|
||||
| Tune flowControl, error handling, logging, testing | [`references/best-practices.md`](./references/best-practices.md) |
|
||||
| See two real workflows end-to-end | [`references/examples.md`](./references/examples.md) |
|
||||
| Deploy on lobehub-cloud (re-exports, cloud-only ops) | [`references/cloud.md`](./references/cloud.md) |
|
||||
|
||||
---
|
||||
|
||||
## Environment Variables
|
||||
|
||||
```bash
|
||||
# Required for all workflows
|
||||
APP_URL=https://your-app.com # Base URL for workflow endpoints
|
||||
QSTASH_TOKEN=qstash_xxx # QStash authentication token
|
||||
|
||||
# Optional (for custom QStash URL)
|
||||
QSTASH_URL=https://custom-qstash.com
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Checklist for New Workflows
|
||||
|
||||
### Planning
|
||||
|
||||
- [ ] Identify the entity to process (users, agents, items, …)
|
||||
- [ ] Define the per-item business logic
|
||||
- [ ] Determine filtering logic (Redis cache, database state, …)
|
||||
|
||||
### Implementation
|
||||
|
||||
- [ ] Define payload types with TypeScript interfaces
|
||||
- [ ] Create workflow class with static trigger methods
|
||||
- [ ] **Layer 1:** entry point with **dry-run** support
|
||||
- [ ] **Layer 1:** filtering logic to avoid duplicate work
|
||||
- [ ] **Layer 2:** pagination with **fan-out**
|
||||
- [ ] **Layer 3:** **single-task execution** (ONE item per run)
|
||||
- [ ] Configure appropriate `flowControl` for each layer
|
||||
- [ ] Consistent logging with workflow prefixes
|
||||
- [ ] Validate all required payload parameters
|
||||
- [ ] Unique `context.run()` step names
|
||||
|
||||
### Quality & Deployment
|
||||
|
||||
- [ ] Return consistent response shapes
|
||||
- [ ] Configure cloud deployment ([`references/cloud.md`](./references/cloud.md) if on lobehub-cloud)
|
||||
- [ ] Write integration tests (`dryRun` path + full path)
|
||||
- [ ] Smoke-test with dry-run first
|
||||
- [ ] Test with a small batch before full rollout
|
||||
|
||||
---
|
||||
|
||||
## Additional Resources
|
||||
|
||||
- [Upstash Workflow Documentation](https://upstash.com/docs/workflow)
|
||||
- [QStash Documentation](https://upstash.com/docs/qstash)
|
||||
- [Example Workflows in Codebase](<../../src/app/(backend)/api/workflows/>)
|
||||
- [Workflow Classes](../../src/server/workflows/)
|
||||
@@ -1,226 +0,0 @@
|
||||
# Best Practices & Common Pitfalls
|
||||
|
||||
Apply these once your scaffold from `implementation.md` is in place.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
1. [Error Handling](#1-error-handling)
|
||||
2. [Logging](#2-logging)
|
||||
3. [Return Values](#3-return-values)
|
||||
4. [flowControl Configuration](#4-flowcontrol-configuration)
|
||||
5. [context.run() Best Practices](#5-contextrun-best-practices)
|
||||
6. [Payload Validation](#6-payload-validation)
|
||||
7. [Database Connection](#7-database-connection)
|
||||
8. [Testing](#8-testing)
|
||||
9. [Common Pitfalls](#common-pitfalls)
|
||||
|
||||
---
|
||||
|
||||
## 1. Error Handling
|
||||
|
||||
```typescript
|
||||
export const { POST } = serve<Payload>(
|
||||
async (context) => {
|
||||
const { itemId } = context.requestPayload ?? {};
|
||||
|
||||
if (!itemId) {
|
||||
return { success: false, error: 'Missing itemId in payload' };
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await context.run('step-name', () => doWork(itemId));
|
||||
return { success: true, itemId, result };
|
||||
} catch (error) {
|
||||
console.error('[workflow:error]', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
};
|
||||
}
|
||||
},
|
||||
{ flowControl: { ... } },
|
||||
);
|
||||
```
|
||||
|
||||
## 2. Logging
|
||||
|
||||
Consistent prefixes make debugging much easier across QStash dashboards and grep:
|
||||
|
||||
```typescript
|
||||
console.log('[{workflow}:{layer}] Starting with payload:', payload);
|
||||
console.log('[{workflow}:{layer}] Processing items:', { count: items.length });
|
||||
console.log('[{workflow}:{layer}] Completed:', result);
|
||||
console.error('[{workflow}:{layer}:error]', error);
|
||||
```
|
||||
|
||||
## 3. Return Values
|
||||
|
||||
Pick the shape that matches the layer's purpose — entry points return statistics, execution layers return per-item results.
|
||||
|
||||
```typescript
|
||||
// Success
|
||||
return { success: true, itemId, result, message: 'Optional success message' };
|
||||
|
||||
// Error
|
||||
return { success: false, error: 'Error description', itemId };
|
||||
|
||||
// Statistics (entry point)
|
||||
return {
|
||||
success: true,
|
||||
totalEligible: 100,
|
||||
toProcess: 80,
|
||||
alreadyProcessed: 20,
|
||||
dryRun: true, // if applicable
|
||||
message: 'Summary message',
|
||||
};
|
||||
```
|
||||
|
||||
## 4. flowControl Configuration
|
||||
|
||||
Tune concurrency by layer — entry points are singletons, execution layers fan out.
|
||||
|
||||
```typescript
|
||||
// Layer 1: Entry — single instance to avoid duplicate processing
|
||||
flowControl: { key: '{workflow}.process', parallelism: 1, ratePerSecond: 1 }
|
||||
|
||||
// Layer 2: Pagination — moderate concurrency
|
||||
flowControl: { key: '{workflow}.paginate', parallelism: 20, ratePerSecond: 5 }
|
||||
|
||||
// Layer 3: Execution — higher concurrency for parallel item work
|
||||
flowControl: { key: '{workflow}.execute', parallelism: 10, ratePerSecond: 5 }
|
||||
```
|
||||
|
||||
**Why these defaults:**
|
||||
|
||||
- **Layer 1** always uses `parallelism: 1` so concurrent triggers don't both start the same batch.
|
||||
- **Layer 2** can fan out widely (10-20) since pagination is cheap.
|
||||
- **Layer 3** caps at 5-10 by default; raise/lower based on external API rate limits.
|
||||
|
||||
## 5. context.run() Best Practices
|
||||
|
||||
- Use descriptive step names with prefixes: `{workflow}:step-name`
|
||||
- Each step should be idempotent (safe to retry)
|
||||
- Don't nest `context.run()` calls — keep them flat
|
||||
- Use unique step names when processing multiple items:
|
||||
|
||||
```typescript
|
||||
// ✅ Unique step names
|
||||
await Promise.all(
|
||||
items.map((item) => context.run(`{workflow}:execute:${item.id}`, () => processItem(item))),
|
||||
);
|
||||
|
||||
// ❌ Same step name — Upstash de-dupes by step name and you'll lose data
|
||||
await Promise.all(items.map((item) => context.run(`{workflow}:execute`, () => processItem(item))));
|
||||
```
|
||||
|
||||
## 6. Payload Validation
|
||||
|
||||
Validate at the top so failures are explicit, not silent `undefined` cascades:
|
||||
|
||||
```typescript
|
||||
export const { POST } = serve<Payload>(
|
||||
async (context) => {
|
||||
const { itemId, configId } = context.requestPayload ?? {};
|
||||
|
||||
if (!itemId) return { success: false, error: 'Missing itemId in payload' };
|
||||
if (!configId) return { success: false, error: 'Missing configId in payload' };
|
||||
|
||||
// Proceed with work...
|
||||
},
|
||||
{ flowControl: { ... } },
|
||||
);
|
||||
```
|
||||
|
||||
## 7. Database Connection
|
||||
|
||||
Get the connection once per workflow — `getServerDB()` is async, repeating it inside each step adds latency:
|
||||
|
||||
```typescript
|
||||
export const { POST } = serve<Payload>(
|
||||
async (context) => {
|
||||
const db = await getServerDB();
|
||||
|
||||
const item = await context.run('get-item', () => itemModel.findById(db, itemId));
|
||||
const result = await context.run('save-result', () => resultModel.create(db, result));
|
||||
},
|
||||
{ flowControl: { ... } },
|
||||
);
|
||||
```
|
||||
|
||||
## 8. Testing
|
||||
|
||||
Integration tests should exercise both the dry-run statistics path and the full execution path:
|
||||
|
||||
```typescript
|
||||
describe('WorkflowName', () => {
|
||||
it('should process items successfully', async () => {
|
||||
const items = await createTestItems();
|
||||
await WorkflowClass.triggerProcessItems({ dryRun: false });
|
||||
await waitForCompletion();
|
||||
const results = await getResults();
|
||||
expect(results).toHaveLength(items.length);
|
||||
});
|
||||
|
||||
it('should support dryRun mode', async () => {
|
||||
const result = await WorkflowClass.triggerProcessItems({ dryRun: true });
|
||||
expect(result).toMatchObject({
|
||||
success: true,
|
||||
dryRun: true,
|
||||
totalEligible: expect.any(Number),
|
||||
toProcess: expect.any(Number),
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Common Pitfalls
|
||||
|
||||
### ❌ Reusing `context.run()` step names
|
||||
|
||||
```typescript
|
||||
// Bad — Upstash dedupes by step name
|
||||
await Promise.all(items.map((item) => context.run('process', () => process(item))));
|
||||
|
||||
// Good
|
||||
await Promise.all(items.map((item) => context.run(`process:${item.id}`, () => process(item))));
|
||||
```
|
||||
|
||||
### ❌ Skipping payload validation
|
||||
|
||||
```typescript
|
||||
// Bad — undefined cascades into a confusing failure later
|
||||
const { itemId } = context.requestPayload ?? {};
|
||||
const result = await process(itemId);
|
||||
|
||||
// Good — fail fast with a clear error
|
||||
if (!itemId) return { success: false, error: 'Missing itemId' };
|
||||
```
|
||||
|
||||
### ❌ Skipping the filter step
|
||||
|
||||
```typescript
|
||||
// Bad — duplicates work for items that were already processed
|
||||
const allItems = await getAllItems();
|
||||
await Promise.all(allItems.map((item) => triggerExecute(item)));
|
||||
|
||||
// Good — keeps the pipeline idempotent
|
||||
const allItems = await getAllItems();
|
||||
const itemsNeedingProcessing = await filterExisting(allItems);
|
||||
await Promise.all(itemsNeedingProcessing.map((item) => triggerExecute(item)));
|
||||
```
|
||||
|
||||
### ❌ Inconsistent logging
|
||||
|
||||
```typescript
|
||||
// Bad — different prefixes, mixed formats
|
||||
console.log('Starting workflow');
|
||||
log.info('Processing item:', itemId);
|
||||
console.log(`Done with ${itemId}`);
|
||||
|
||||
// Good — uniform prefix lets you grep by workflow+layer
|
||||
console.log('[workflow:layer] Starting with payload:', payload);
|
||||
console.log('[workflow:layer] Processing item:', { itemId });
|
||||
console.log('[workflow:layer] Completed:', { itemId, result });
|
||||
```
|
||||
@@ -1,403 +0,0 @@
|
||||
# Cloud Project Workflow Configuration
|
||||
|
||||
Cloud-specific workflow configurations and patterns for the lobehub-cloud project.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
1. [Overview](#overview)
|
||||
2. [Directory Structure](#directory-structure) — submodule + cloud layout
|
||||
3. [Cloud-Specific Patterns](#cloud-specific-patterns) — cloud-only workflows + re-export pattern
|
||||
4. [TypeScript Path Mappings](#typescript-path-mappings)
|
||||
5. [Workflow Class Location](#workflow-class-location) — cloud-only vs shared
|
||||
6. [Environment Variables](#environment-variables)
|
||||
7. [Best Practices](#best-practices) — decide cloud vs OSS, re-export rules, naming
|
||||
8. [Migration Guide](#migration-guide) — moving workflows from cloud to lobehub
|
||||
9. [Examples](#examples) — `welcome-placeholder`, `agent-eval-run`
|
||||
10. [Troubleshooting](#troubleshooting) — circular imports, 404s, type errors
|
||||
11. [Related Documentation](#related-documentation)
|
||||
|
||||
## Overview
|
||||
|
||||
The lobehub-cloud project extends the open-source lobehub codebase with cloud-specific features. Workflows can be implemented in either:
|
||||
|
||||
1. **Lobehub (open-source)** - Available to all users
|
||||
2. **Lobehub-cloud (proprietary)** - Cloud-specific business logic
|
||||
|
||||
---
|
||||
|
||||
## Directory Structure
|
||||
|
||||
### Lobehub Submodule (Open-source)
|
||||
|
||||
```text
|
||||
lobehub/
|
||||
└── src/
|
||||
├── app/(backend)/api/workflows/
|
||||
│ ├── memory-user-memory/ # Memory extraction workflows
|
||||
│ └── agent-eval-run/ # Benchmark evaluation workflows
|
||||
└── server/workflows/
|
||||
├── agentEvalRun/
|
||||
└── ...
|
||||
```
|
||||
|
||||
### Lobehub-cloud (Proprietary)
|
||||
|
||||
```text
|
||||
lobehub-cloud/
|
||||
└── src/
|
||||
├── app/(backend)/api/workflows/
|
||||
│ ├── welcome-placeholder/ # Cloud-only: AI placeholder generation
|
||||
│ ├── agent-welcome/ # Cloud-only: Agent welcome messages
|
||||
│ ├── agent-eval-run/ # Re-export from lobehub
|
||||
│ └── memory-user-memory/ # Re-export from lobehub
|
||||
└── server/workflows/
|
||||
├── welcomePlaceholder/
|
||||
├── agentWelcome/
|
||||
└── agentEvalRun/ # Re-export from lobehub
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Cloud-Specific Patterns
|
||||
|
||||
### Pattern 1: Cloud-Only Workflows
|
||||
|
||||
**Use Case**: Features exclusive to cloud users (AI generation, premium features)
|
||||
|
||||
**Example**: `welcome-placeholder`, `agent-welcome`
|
||||
|
||||
**Implementation**:
|
||||
|
||||
- Implement directly in `lobehub-cloud/src/app/(backend)/api/workflows/`
|
||||
- No need for re-exports
|
||||
- Can use cloud-specific packages and services
|
||||
|
||||
**Structure**:
|
||||
|
||||
```text
|
||||
lobehub-cloud/src/
|
||||
├── app/(backend)/api/workflows/
|
||||
│ └── feature-name/
|
||||
│ ├── process-items/route.ts
|
||||
│ ├── paginate-items/route.ts
|
||||
│ └── execute-item/route.ts
|
||||
└── server/workflows/
|
||||
└── featureName/
|
||||
└── index.ts
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Pattern 2: Re-export from Lobehub
|
||||
|
||||
**Use Case**: Workflows implemented in open-source but also used in cloud
|
||||
|
||||
**Example**: `agent-eval-run`, `memory-user-memory`
|
||||
|
||||
**Why Re-export?**
|
||||
|
||||
- Cloud deployment needs to serve these endpoints
|
||||
- Lobehub submodule code is not directly accessible in cloud routes
|
||||
- Allows cloud-specific overrides if needed in the future
|
||||
|
||||
#### Re-export Implementation
|
||||
|
||||
**Step 1**: Implement workflow in lobehub submodule
|
||||
|
||||
```typescript
|
||||
// lobehub/src/app/(backend)/api/workflows/feature/layer/route.ts
|
||||
import { serve } from '@upstash/workflow/nextjs';
|
||||
|
||||
export const { POST } = serve<Payload>(
|
||||
async (context) => {
|
||||
// Implementation
|
||||
},
|
||||
{ flowControl: { ... } }
|
||||
);
|
||||
```
|
||||
|
||||
**Step 2**: Create re-export in lobehub-cloud
|
||||
|
||||
```typescript
|
||||
// lobehub-cloud/src/app/(backend)/api/workflows/feature/layer/route.ts
|
||||
export { POST } from 'lobehub/src/app/(backend)/api/workflows/feature/layer/route';
|
||||
```
|
||||
|
||||
**Important**: Use `lobehub/src/...` path, NOT `@/...` to avoid circular imports.
|
||||
|
||||
#### Re-export Directory Structure
|
||||
|
||||
```bash
|
||||
# Create directories
|
||||
mkdir -p lobehub-cloud/src/app/(backend)/api/workflows/feature-name/layer-1
|
||||
mkdir -p lobehub-cloud/src/app/(backend)/api/workflows/feature-name/layer-2
|
||||
mkdir -p lobehub-cloud/src/app/(backend)/api/workflows/feature-name/layer-3
|
||||
|
||||
# Create re-export files
|
||||
echo "export { POST } from 'lobehub/src/app/(backend)/api/workflows/feature-name/layer-1/route';" > \
|
||||
lobehub-cloud/src/app/(backend)/api/workflows/feature-name/layer-1/route.ts
|
||||
|
||||
echo "export { POST } from 'lobehub/src/app/(backend)/api/workflows/feature-name/layer-2/route';" > \
|
||||
lobehub-cloud/src/app/(backend)/api/workflows/feature-name/layer-2/route.ts
|
||||
|
||||
echo "export { POST } from 'lobehub/src/app/(backend)/api/workflows/feature-name/layer-3/route';" > \
|
||||
lobehub-cloud/src/app/(backend)/api/workflows/feature-name/layer-3/route.ts
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## TypeScript Path Mappings
|
||||
|
||||
The cloud project uses tsconfig path mappings to override lobehub code:
|
||||
|
||||
```json
|
||||
// lobehub-cloud/tsconfig.json
|
||||
{
|
||||
"compilerOptions": {
|
||||
"paths": {
|
||||
"@/*": ["./src/*", "./lobehub/src/*"]
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Resolution Order**:
|
||||
|
||||
1. `./src/*` (cloud code) - checked first
|
||||
2. `./lobehub/src/*` (open-source) - fallback
|
||||
|
||||
This allows cloud to override specific modules while using lobehub defaults.
|
||||
|
||||
---
|
||||
|
||||
## Workflow Class Location
|
||||
|
||||
### Cloud-Only Workflows
|
||||
|
||||
Place workflow class in cloud:
|
||||
|
||||
```text
|
||||
lobehub-cloud/src/server/workflows/featureName/index.ts
|
||||
```
|
||||
|
||||
### Shared Workflows
|
||||
|
||||
Place workflow class in lobehub, re-export in cloud if needed:
|
||||
|
||||
```text
|
||||
lobehub/src/server/workflows/featureName/index.ts
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Environment Variables
|
||||
|
||||
Both lobehub and cloud workflows require:
|
||||
|
||||
```bash
|
||||
# Required for all workflows
|
||||
APP_URL=https://your-app.com # Base URL for workflow endpoints
|
||||
QSTASH_TOKEN=qstash_xxx # QStash authentication token
|
||||
|
||||
# Optional (for custom QStash URL)
|
||||
QSTASH_URL=https://custom-qstash.com # Custom QStash endpoint
|
||||
```
|
||||
|
||||
**Cloud-Specific**:
|
||||
|
||||
```bash
|
||||
# Cloud database (for monetization features)
|
||||
CLOUD_DATABASE_URL=postgresql://...
|
||||
|
||||
# Cloud-specific services
|
||||
REDIS_URL=redis://...
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Decide: Cloud or Open-Source?
|
||||
|
||||
**Implement in Lobehub if**:
|
||||
|
||||
- Feature is useful for all LobeHub users
|
||||
- No proprietary business logic
|
||||
- Can be open-sourced
|
||||
|
||||
**Implement in Cloud if**:
|
||||
|
||||
- Premium/paid feature
|
||||
- Uses cloud-specific services
|
||||
- Contains proprietary algorithms
|
||||
|
||||
### 2. Re-export Pattern
|
||||
|
||||
✅ **Do**:
|
||||
|
||||
```typescript
|
||||
// Simple re-export
|
||||
export { POST } from 'lobehub/src/app/(backend)/api/workflows/feature/route';
|
||||
```
|
||||
|
||||
❌ **Don't**:
|
||||
|
||||
```typescript
|
||||
// Avoid circular imports with @/ path
|
||||
export { POST } from '@/app/(backend)/api/workflows/feature/route'; // ❌
|
||||
```
|
||||
|
||||
### 3. Keep Workflow Logic in Lobehub
|
||||
|
||||
For shared features:
|
||||
|
||||
- Implement core logic in `lobehub/` (open-source)
|
||||
- Only override if cloud needs different behavior
|
||||
- Use re-exports for cloud deployment
|
||||
|
||||
### 4. Directory Naming
|
||||
|
||||
Follow consistent naming across lobehub and cloud:
|
||||
|
||||
```text
|
||||
# Both should use same structure
|
||||
lobehub/src/app/(backend)/api/workflows/feature-name/
|
||||
lobehub-cloud/src/app/(backend)/api/workflows/feature-name/
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Migration Guide
|
||||
|
||||
### Moving Workflow from Cloud to Lobehub
|
||||
|
||||
**Step 1**: Copy workflow to lobehub
|
||||
|
||||
```bash
|
||||
cp -r lobehub-cloud/src/app/(backend)/api/workflows/feature \
|
||||
lobehub/src/app/(backend)/api/workflows/
|
||||
```
|
||||
|
||||
**Step 2**: Remove cloud-specific dependencies
|
||||
|
||||
- Replace cloud services with generic interfaces
|
||||
- Remove proprietary business logic
|
||||
- Update imports to use lobehub paths
|
||||
|
||||
**Step 3**: Create re-exports in cloud
|
||||
|
||||
```typescript
|
||||
// lobehub-cloud/src/app/(backend)/api/workflows/feature/*/route.ts
|
||||
export { POST } from 'lobehub/src/app/(backend)/api/workflows/feature/*/route';
|
||||
```
|
||||
|
||||
**Step 4**: Move workflow class to lobehub
|
||||
|
||||
```bash
|
||||
mv lobehub-cloud/src/server/workflows/feature \
|
||||
lobehub/src/server/workflows/
|
||||
```
|
||||
|
||||
**Step 5**: Update cloud imports
|
||||
|
||||
```typescript
|
||||
// Change from
|
||||
import { Workflow } from '@/server/workflows/feature';
|
||||
|
||||
// To
|
||||
import { Workflow } from 'lobehub/src/server/workflows/feature';
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Examples
|
||||
|
||||
### Cloud-Only Workflow: welcome-placeholder
|
||||
|
||||
**Location**: `lobehub-cloud/src/app/(backend)/api/workflows/welcome-placeholder/`
|
||||
|
||||
**Why Cloud-Only**: Uses proprietary AI generation service and Redis caching
|
||||
|
||||
**Structure**:
|
||||
|
||||
```text
|
||||
lobehub-cloud/
|
||||
├── src/app/(backend)/api/workflows/welcome-placeholder/
|
||||
│ ├── process-users/route.ts
|
||||
│ ├── paginate-users/route.ts
|
||||
│ └── generate-user/route.ts
|
||||
└── src/server/workflows/welcomePlaceholder/
|
||||
└── index.ts
|
||||
```
|
||||
|
||||
### Re-exported Workflow: agent-eval-run
|
||||
|
||||
**Location**:
|
||||
|
||||
- Implementation: `lobehub/src/app/(backend)/api/workflows/agent-eval-run/`
|
||||
- Re-export: `lobehub-cloud/src/app/(backend)/api/workflows/agent-eval-run/`
|
||||
|
||||
**Why Re-export**: Core feature available in open-source, also used by cloud
|
||||
|
||||
**Cloud Re-export Files**:
|
||||
|
||||
```typescript
|
||||
// lobehub-cloud/src/app/(backend)/api/workflows/agent-eval-run/run-benchmark/route.ts
|
||||
export { POST } from 'lobehub/src/app/(backend)/api/workflows/agent-eval-run/run-benchmark/route';
|
||||
|
||||
// lobehub-cloud/src/app/(backend)/api/workflows/agent-eval-run/paginate-test-cases/route.ts
|
||||
export { POST } from 'lobehub/src/app/(backend)/api/workflows/agent-eval-run/paginate-test-cases/route';
|
||||
|
||||
// ... (all layers)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Circular Import Error
|
||||
|
||||
**Error**: `Circular definition of import alias 'POST'`
|
||||
|
||||
**Cause**: Using `@/` path in re-export within cloud codebase
|
||||
|
||||
**Solution**: Use `lobehub/src/` path instead
|
||||
|
||||
```typescript
|
||||
// ❌ Wrong
|
||||
export { POST } from '@/app/(backend)/api/workflows/feature/route';
|
||||
|
||||
// ✅ Correct
|
||||
export { POST } from 'lobehub/src/app/(backend)/api/workflows/feature/route';
|
||||
```
|
||||
|
||||
### Workflow Not Found (404)
|
||||
|
||||
**Cause**: Missing re-export in cloud
|
||||
|
||||
**Solution**: Create re-export files for all workflow layers
|
||||
|
||||
```bash
|
||||
# Check if re-export exists
|
||||
ls lobehub-cloud/src/app/\(backend\)/api/workflows/feature-name/
|
||||
|
||||
# If missing, create re-exports
|
||||
mkdir -p lobehub-cloud/src/app/\(backend\)/api/workflows/feature-name/layer
|
||||
echo "export { POST } from 'lobehub/src/app/(backend)/api/workflows/feature-name/layer/route';" > lobehub-cloud/src/app/\(backend\)/api/workflows/feature-name/layer/route.ts
|
||||
```
|
||||
|
||||
### Type Errors After Moving to Lobehub
|
||||
|
||||
**Cause**: Cloud-specific types or services used in lobehub code
|
||||
|
||||
**Solution**:
|
||||
|
||||
1. Extract cloud-specific logic to cloud-only wrapper
|
||||
2. Use dependency injection for services
|
||||
3. Define generic interfaces in lobehub
|
||||
|
||||
---
|
||||
|
||||
## Related Documentation
|
||||
|
||||
- [SKILL.md](../SKILL.md) - Standard workflow patterns
|
||||
@@ -1,91 +0,0 @@
|
||||
# Worked Examples
|
||||
|
||||
Two real workflows already in the codebase that follow this skill's pattern verbatim. Skim them when you want to see the pattern applied to concrete entities.
|
||||
|
||||
## Example 1: Welcome Placeholder
|
||||
|
||||
**Use case:** Generate AI-powered welcome placeholders for users.
|
||||
|
||||
**Structure:**
|
||||
|
||||
- Layer 1: `process-users` — entry point, checks eligible users
|
||||
- Layer 2: `paginate-users` — paginates through active users
|
||||
- Layer 3: `generate-user` — generates placeholders for ONE user
|
||||
|
||||
**Key features:**
|
||||
|
||||
- Filters users who already have cached placeholders in Redis
|
||||
- `paidOnly` flag to scope to subscribed users
|
||||
- `dryRun` mode for statistics
|
||||
- Fan-out for large user batches (`CHUNK_SIZE=20`)
|
||||
|
||||
**Layer 3 shape:**
|
||||
|
||||
```typescript
|
||||
export const { POST } = serve<GenerateUserPlaceholderPayload>(async (context) => {
|
||||
const { userId } = context.requestPayload ?? {};
|
||||
|
||||
const workflow = new WelcomePlaceholderWorkflow(db, userId);
|
||||
const placeholders = await context.run('generate', () => workflow.generate());
|
||||
|
||||
return { success: true, userId, placeholdersCount: placeholders.length };
|
||||
});
|
||||
```
|
||||
|
||||
**Files:**
|
||||
|
||||
- `/api/workflows/welcome-placeholder/process-users/route.ts`
|
||||
- `/api/workflows/welcome-placeholder/paginate-users/route.ts`
|
||||
- `/api/workflows/welcome-placeholder/generate-user/route.ts`
|
||||
- `/server/workflows/welcomePlaceholder/index.ts`
|
||||
|
||||
---
|
||||
|
||||
## Example 2: Agent Welcome
|
||||
|
||||
**Use case:** Generate welcome messages and open questions for AI agents.
|
||||
|
||||
**Structure:**
|
||||
|
||||
- Layer 1: `process-agents` — entry point, checks eligible agents
|
||||
- Layer 2: `paginate-agents` — paginates through active agents
|
||||
- Layer 3: `generate-agent` — generates welcome data for ONE agent
|
||||
|
||||
**Key features:**
|
||||
|
||||
- Filters agents who already have cached data in Redis
|
||||
- `paidOnly` flag for subscribed users' agents only
|
||||
- `dryRun` mode for statistics
|
||||
- Fan-out for large agent batches (`CHUNK_SIZE=20`)
|
||||
|
||||
**Layer 3 shape:**
|
||||
|
||||
```typescript
|
||||
export const { POST } = serve<GenerateAgentWelcomePayload>(async (context) => {
|
||||
const { agentId } = context.requestPayload ?? {};
|
||||
|
||||
const workflow = new AgentWelcomeWorkflow(db, agentId);
|
||||
const data = await context.run('generate', () => workflow.generate());
|
||||
|
||||
return { success: true, agentId, data };
|
||||
});
|
||||
```
|
||||
|
||||
**Files:**
|
||||
|
||||
- `/api/workflows/agent-welcome/process-agents/route.ts`
|
||||
- `/api/workflows/agent-welcome/paginate-agents/route.ts`
|
||||
- `/api/workflows/agent-welcome/generate-agent/route.ts`
|
||||
- `/server/workflows/agentWelcome/index.ts`
|
||||
|
||||
---
|
||||
|
||||
## What's identical, what differs
|
||||
|
||||
Both workflows are the **same pattern** — they only differ in:
|
||||
|
||||
- Entity type (users vs agents)
|
||||
- Business logic (placeholder generation vs welcome generation)
|
||||
- Data source (different database queries)
|
||||
|
||||
Everything else — the 3-layer split, dry-run handling, fan-out, filter-existing, flowControl tuning — is identical. That's the whole point: once you internalize the pattern, adding a new workflow is mostly entity-substitution.
|
||||
@@ -1,333 +0,0 @@
|
||||
# Implementation Patterns
|
||||
|
||||
Full code templates for the 3-layer architecture. Read this when actually writing workflow files.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
1. [Workflow Class](#workflow-class) — `src/server/workflows/{workflowName}/index.ts`
|
||||
2. [Layer 1: Entry Point](#layer-1-entry-point-process-) — `process-*` route
|
||||
3. [Layer 2: Pagination](#layer-2-pagination-paginate-) — `paginate-*` route
|
||||
4. [Layer 3: Execution](#layer-3-execution-execute--generate-) — `execute-*` / `generate-*` route
|
||||
|
||||
---
|
||||
|
||||
## Workflow Class
|
||||
|
||||
**Location:** `src/server/workflows/{workflowName}/index.ts`
|
||||
|
||||
```typescript
|
||||
import { Client } from '@upstash/workflow';
|
||||
import debug from 'debug';
|
||||
|
||||
const log = debug('lobe-server:workflows:{workflow-name}');
|
||||
|
||||
// Workflow paths
|
||||
const WORKFLOW_PATHS = {
|
||||
processItems: '/api/workflows/{workflow-name}/process-items',
|
||||
paginateItems: '/api/workflows/{workflow-name}/paginate-items',
|
||||
executeItem: '/api/workflows/{workflow-name}/execute-item',
|
||||
} as const;
|
||||
|
||||
// Payload types
|
||||
export interface ProcessItemsPayload {
|
||||
dryRun?: boolean;
|
||||
force?: boolean;
|
||||
}
|
||||
|
||||
export interface PaginateItemsPayload {
|
||||
cursor?: string;
|
||||
itemIds?: string[]; // For fanout chunks
|
||||
}
|
||||
|
||||
export interface ExecuteItemPayload {
|
||||
itemId: string;
|
||||
}
|
||||
|
||||
const getWorkflowUrl = (path: string): string => {
|
||||
const baseUrl = process.env.APP_URL;
|
||||
if (!baseUrl) throw new Error('APP_URL is required to trigger workflows');
|
||||
return new URL(path, baseUrl).toString();
|
||||
};
|
||||
|
||||
const getWorkflowClient = (): Client => {
|
||||
const token = process.env.QSTASH_TOKEN;
|
||||
if (!token) throw new Error('QSTASH_TOKEN is required to trigger workflows');
|
||||
|
||||
const config: ConstructorParameters<typeof Client>[0] = { token };
|
||||
if (process.env.QSTASH_URL) {
|
||||
(config as Record<string, unknown>).url = process.env.QSTASH_URL;
|
||||
}
|
||||
return new Client(config);
|
||||
};
|
||||
|
||||
export class {WorkflowName}Workflow {
|
||||
private static client: Client;
|
||||
|
||||
private static getClient(): Client {
|
||||
if (!this.client) this.client = getWorkflowClient();
|
||||
return this.client;
|
||||
}
|
||||
|
||||
static triggerProcessItems(payload: ProcessItemsPayload) {
|
||||
const url = getWorkflowUrl(WORKFLOW_PATHS.processItems);
|
||||
log('Triggering process-items workflow');
|
||||
return this.getClient().trigger({ body: payload, url });
|
||||
}
|
||||
|
||||
static triggerPaginateItems(payload: PaginateItemsPayload) {
|
||||
const url = getWorkflowUrl(WORKFLOW_PATHS.paginateItems);
|
||||
log('Triggering paginate-items workflow');
|
||||
return this.getClient().trigger({ body: payload, url });
|
||||
}
|
||||
|
||||
static triggerExecuteItem(payload: ExecuteItemPayload) {
|
||||
const url = getWorkflowUrl(WORKFLOW_PATHS.executeItem);
|
||||
log('Triggering execute-item workflow: %s', payload.itemId);
|
||||
return this.getClient().trigger({ body: payload, url });
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter items that need processing (e.g. check Redis cache, database state).
|
||||
* Return only the ones that actually need work — keeps the pipeline idempotent.
|
||||
*/
|
||||
static async filterItemsNeedingProcessing(itemIds: string[]): Promise<string[]> {
|
||||
if (itemIds.length === 0) return [];
|
||||
// Check existing state and return items that need processing
|
||||
return itemIds;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Layer 1: Entry Point (process-\*)
|
||||
|
||||
**Purpose:** Validates prerequisites, calculates statistics, supports dry-run mode.
|
||||
|
||||
```typescript
|
||||
import { serve } from '@upstash/workflow/nextjs';
|
||||
import { getServerDB } from '@/database/server';
|
||||
import { WorkflowClass, type ProcessPayload } from '@/server/workflows/{workflowName}';
|
||||
|
||||
export const { POST } = serve<ProcessPayload>(
|
||||
async (context) => {
|
||||
const { dryRun, force } = context.requestPayload ?? {};
|
||||
|
||||
console.log('[{workflow}:process] Starting with payload:', { dryRun, force });
|
||||
|
||||
const allItemIds = await context.run('{workflow}:get-all-items', async () => {
|
||||
const db = await getServerDB();
|
||||
// Query database for eligible items
|
||||
return items.map((item) => item.id);
|
||||
});
|
||||
|
||||
console.log('[{workflow}:process] Total eligible items:', allItemIds.length);
|
||||
|
||||
if (allItemIds.length === 0) {
|
||||
return { success: true, totalEligible: 0, message: 'No eligible items found' };
|
||||
}
|
||||
|
||||
const itemsNeedingProcessing = await context.run('{workflow}:filter-existing', () =>
|
||||
WorkflowClass.filterItemsNeedingProcessing(allItemIds),
|
||||
);
|
||||
|
||||
const result = {
|
||||
success: true,
|
||||
totalEligible: allItemIds.length,
|
||||
toProcess: itemsNeedingProcessing.length,
|
||||
alreadyProcessed: allItemIds.length - itemsNeedingProcessing.length,
|
||||
};
|
||||
|
||||
// Dry-run short-circuits before any side effects
|
||||
if (dryRun) {
|
||||
console.log('[{workflow}:process] Dry run mode, returning statistics only');
|
||||
return {
|
||||
...result,
|
||||
dryRun: true,
|
||||
message: `[DryRun] Would process ${itemsNeedingProcessing.length} items`,
|
||||
};
|
||||
}
|
||||
|
||||
if (itemsNeedingProcessing.length === 0) {
|
||||
return { ...result, message: 'All items already processed' };
|
||||
}
|
||||
|
||||
await context.run('{workflow}:trigger-paginate', () => WorkflowClass.triggerPaginateItems({}));
|
||||
|
||||
return {
|
||||
...result,
|
||||
message: `Triggered pagination for ${itemsNeedingProcessing.length} items`,
|
||||
};
|
||||
},
|
||||
{
|
||||
flowControl: {
|
||||
key: '{workflow}.process',
|
||||
parallelism: 1, // single instance — avoids duplicate processing
|
||||
ratePerSecond: 1,
|
||||
},
|
||||
},
|
||||
);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Layer 2: Pagination (paginate-\*)
|
||||
|
||||
**Purpose:** Handles cursor-based pagination, implements fan-out for large batches.
|
||||
|
||||
```typescript
|
||||
import { serve } from '@upstash/workflow/nextjs';
|
||||
import { chunk } from 'es-toolkit/compat';
|
||||
import { getServerDB } from '@/database/server';
|
||||
import { WorkflowClass, type PaginatePayload } from '@/server/workflows/{workflowName}';
|
||||
|
||||
const PAGE_SIZE = 50;
|
||||
const CHUNK_SIZE = 20;
|
||||
|
||||
export const { POST } = serve<PaginatePayload>(
|
||||
async (context) => {
|
||||
const { cursor, itemIds: payloadItemIds } = context.requestPayload ?? {};
|
||||
|
||||
console.log('[{workflow}:paginate] Starting:', {
|
||||
cursor,
|
||||
itemIdsCount: payloadItemIds?.length ?? 0,
|
||||
});
|
||||
|
||||
// If specific itemIds were passed in (from a fanout chunk), process them directly
|
||||
if (payloadItemIds && payloadItemIds.length > 0) {
|
||||
await Promise.all(
|
||||
payloadItemIds.map((itemId) =>
|
||||
context.run(`{workflow}:execute:${itemId}`, () =>
|
||||
WorkflowClass.triggerExecuteItem({ itemId }),
|
||||
),
|
||||
),
|
||||
);
|
||||
return { success: true, processedItems: payloadItemIds.length };
|
||||
}
|
||||
|
||||
// Paginate through all items
|
||||
const itemBatch = await context.run('{workflow}:get-batch', async () => {
|
||||
const db = await getServerDB();
|
||||
const items = await db.query(...);
|
||||
if (!items.length) return { ids: [] };
|
||||
const last = items.at(-1);
|
||||
return {
|
||||
ids: items.map((item) => item.id),
|
||||
cursor: last ? last.id : undefined,
|
||||
};
|
||||
});
|
||||
|
||||
const batchItemIds = itemBatch.ids;
|
||||
const nextCursor = 'cursor' in itemBatch ? itemBatch.cursor : undefined;
|
||||
|
||||
if (batchItemIds.length === 0) {
|
||||
return { success: true, message: 'Pagination complete' };
|
||||
}
|
||||
|
||||
const itemIds = await context.run('{workflow}:filter-existing', () =>
|
||||
WorkflowClass.filterItemsNeedingProcessing(batchItemIds),
|
||||
);
|
||||
|
||||
if (itemIds.length > 0) {
|
||||
if (itemIds.length > CHUNK_SIZE) {
|
||||
// Fan out — recursively re-enter pagination with each chunk
|
||||
const chunks = chunk(itemIds, CHUNK_SIZE);
|
||||
console.log('[{workflow}:paginate] Fanout mode:', {
|
||||
chunks: chunks.length,
|
||||
chunkSize: CHUNK_SIZE,
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
chunks.map((ids, idx) =>
|
||||
context.run(`{workflow}:fanout:${idx + 1}/${chunks.length}`, () =>
|
||||
WorkflowClass.triggerPaginateItems({ itemIds: ids }),
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
// Process this page directly
|
||||
await Promise.all(
|
||||
itemIds.map((itemId) =>
|
||||
context.run(`{workflow}:execute:${itemId}`, () =>
|
||||
WorkflowClass.triggerExecuteItem({ itemId }),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Tail-call into the next page
|
||||
if (nextCursor) {
|
||||
await context.run('{workflow}:next-page', () =>
|
||||
WorkflowClass.triggerPaginateItems({ cursor: nextCursor }),
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
processedItems: itemIds.length,
|
||||
skippedItems: batchItemIds.length - itemIds.length,
|
||||
nextCursor: nextCursor ?? null,
|
||||
};
|
||||
},
|
||||
{
|
||||
flowControl: {
|
||||
key: '{workflow}.paginate',
|
||||
parallelism: 20,
|
||||
ratePerSecond: 5,
|
||||
},
|
||||
},
|
||||
);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Layer 3: Execution (execute-\* / generate-\*)
|
||||
|
||||
**Purpose:** Performs the actual business logic for exactly ONE item.
|
||||
|
||||
```typescript
|
||||
import { serve } from '@upstash/workflow/nextjs';
|
||||
import { getServerDB } from '@/database/server';
|
||||
import { WorkflowClass, type ExecutePayload } from '@/server/workflows/{workflowName}';
|
||||
|
||||
export const { POST } = serve<ExecutePayload>(
|
||||
async (context) => {
|
||||
const { itemId } = context.requestPayload ?? {};
|
||||
|
||||
if (!itemId) {
|
||||
return { success: false, error: 'Missing itemId' };
|
||||
}
|
||||
|
||||
const db = await getServerDB();
|
||||
|
||||
const item = await context.run('{workflow}:get-item', async () => {
|
||||
// Query database for item
|
||||
return item;
|
||||
});
|
||||
|
||||
if (!item) {
|
||||
return { success: false, error: 'Item not found' };
|
||||
}
|
||||
|
||||
const result = await context.run('{workflow}:process-item', async () => {
|
||||
const workflow = new WorkflowClass(db, itemId);
|
||||
return workflow.generate(); // or process(), execute(), etc.
|
||||
});
|
||||
|
||||
await context.run('{workflow}:save-result', async () => {
|
||||
const workflow = new WorkflowClass(db, itemId);
|
||||
return workflow.saveToRedis(result); // or saveToDatabase(), etc.
|
||||
});
|
||||
|
||||
return { success: true, itemId, result };
|
||||
},
|
||||
{
|
||||
flowControl: {
|
||||
key: '{workflow}.execute',
|
||||
parallelism: 10,
|
||||
ratePerSecond: 5,
|
||||
},
|
||||
},
|
||||
);
|
||||
```
|
||||
@@ -1,100 +0,0 @@
|
||||
---
|
||||
name: version-release
|
||||
description: "Version release workflow. Use when the user mentions 'release', 'hotfix', 'version upgrade', 'weekly release', or '发版'/'发布'/'小班车'. This skill is for release process and GitHub Release notes (not docs/changelog page writing)."
|
||||
disable-model-invocation: true
|
||||
argument-hint: '[minor|patch] [version?]'
|
||||
---
|
||||
|
||||
# Version Release Workflow
|
||||
|
||||
This skill is a router. The detailed steps live in `references/`.
|
||||
|
||||
## Scope Boundary (Important)
|
||||
|
||||
This skill is only for:
|
||||
|
||||
1. Release branch / PR workflow
|
||||
2. CI trigger constraints (`auto-tag-release.yml`)
|
||||
3. GitHub Release note writing
|
||||
|
||||
This skill is **not** for writing `docs/changelog/*.mdx`.\
|
||||
If the user asks for website changelog pages, load `../docs-changelog/SKILL.md`.
|
||||
|
||||
## Mandatory Companion Skill
|
||||
|
||||
For every `/version-release` execution, you MUST load and apply:
|
||||
|
||||
- `../microcopy/SKILL.md`
|
||||
|
||||
## Overview
|
||||
|
||||
The primary development branch is **canary**. All day-to-day development happens on canary. When releasing, canary is merged into main. After merge, `auto-tag-release.yml` automatically handles tagging, version bumping, creating a GitHub Release, and syncing back to the canary branch.
|
||||
|
||||
Only two release types are used in practice (major releases are extremely rare and can be ignored):
|
||||
|
||||
| Type | Use Case | Frequency | Source Branch | PR Title Format | Version | Reference |
|
||||
| ----- | ---------------------------------------------- | --------------------- | -------------- | ------------------------------------ | ------------- | --------------------------------------- |
|
||||
| Minor | Feature iteration release | \~Every 4 weeks | canary | `🚀 release: v{x.y.0}` | Manually set | `references/minor-release.md` |
|
||||
| Patch | Weekly release / hotfix / model / DB migration | \~Weekly or as needed | canary or main | Custom (e.g. `🚀 release: 20260222`) | Auto patch +1 | `references/patch-release-scenarios.md` |
|
||||
|
||||
For writing the release-note body (any release type), see `references/release-notes-style.md`.
|
||||
|
||||
## Auto-Release Trigger Rules (`auto-tag-release.yml`)
|
||||
|
||||
After a PR is merged into main, CI determines whether to release based on the following priority:
|
||||
|
||||
### 1. Minor Release (Exact Version)
|
||||
|
||||
PR title matches `🚀 release: v{x.y.z}` -> uses the version number from the title.
|
||||
|
||||
### 2. Patch Release (Auto patch +1)
|
||||
|
||||
Triggered by the following priority:
|
||||
|
||||
- **Branch name match**: `hotfix/*` or `release/*` -> triggers directly (skips title detection)
|
||||
- **Title prefix match**: PRs with the following title prefixes will trigger:
|
||||
- `style` / `💄 style`
|
||||
- `feat` / `✨ feat`
|
||||
- `fix` / `🐛 fix`
|
||||
- `refactor` / `♻️ refactor`
|
||||
- `hotfix` / `🐛 hotfix` / `🩹 hotfix`
|
||||
- `build` / `👷 build`
|
||||
|
||||
### 3. No Trigger
|
||||
|
||||
PRs that don't match any conditions above (e.g. `docs`, `chore`, `ci`, `test`) will not trigger a release when merged into main.
|
||||
|
||||
## Post-Release Automated Actions
|
||||
|
||||
1. **Bump `package.json`** — commits `🔖 chore(release): release version v{x.y.z} [skip ci]`
|
||||
2. **Create annotated tag** — `v{x.y.z}`
|
||||
3. **Create GitHub Release**
|
||||
4. **Dispatch `sync-main-to-canary`** — syncs main back to canary
|
||||
|
||||
## Agent Action Guide
|
||||
|
||||
When the user requests a release:
|
||||
|
||||
### Precheck (applies to all release types)
|
||||
|
||||
Before creating the release branch, verify the source branch:
|
||||
|
||||
- **Weekly Release** (`release/weekly-*`): must branch from `canary`
|
||||
- **All other release/hotfix branches**: must branch from `main`; run `git merge-base --is-ancestor main <branch> && echo OK`
|
||||
- If the branch is based on the wrong source, recreate from the correct base
|
||||
|
||||
### Routing
|
||||
|
||||
Pick the right reference and follow it end-to-end:
|
||||
|
||||
- **Minor release** → `references/minor-release.md`
|
||||
- **Patch release** (weekly / hotfix / model launch / DB migration) → `references/patch-release-scenarios.md`
|
||||
- **Writing the PR body / release notes** (any release type) → `references/release-notes-style.md`
|
||||
|
||||
### Hard Rules (apply to every release type)
|
||||
|
||||
- **Do NOT** manually modify `package.json` version — CI handles it.
|
||||
- **Do NOT** manually create tags — CI handles them.
|
||||
- Minor PR title format is strict (`🚀 release: v{x.y.z}`).
|
||||
- Patch PRs do not need an explicit version number.
|
||||
- Keep release facts accurate; do not invent metrics or availability statements. Release-note inputs (compare base, PR refs, contributor list) **must be derived from `git`** per `references/release-notes-style.md` § Computing Inputs — never from memory or descriptions.
|
||||
@@ -1,52 +0,0 @@
|
||||
# 🚀 LobeHub Release (20260416)
|
||||
|
||||
**Release Date:** April 20, 2026\
|
||||
**Migration Scope:** Agent benchmark data model bootstrap (5 new tables, 2 new indexes)
|
||||
|
||||
> This release introduces a schema foundation for benchmark execution and reporting, so agent evaluation data is stored as a complete lifecycle instead of fragmented records.
|
||||
|
||||
---
|
||||
|
||||
## 🗄️ Migration Overview
|
||||
|
||||
Added tables:
|
||||
|
||||
- `agent_eval_benchmarks`
|
||||
- `agent_eval_datasets`
|
||||
- `agent_eval_runs`
|
||||
- `agent_eval_run_topics`
|
||||
- `agent_eval_records`
|
||||
|
||||
Added indexes:
|
||||
|
||||
- `idx_agent_eval_runs_status_created_at`
|
||||
- `idx_agent_eval_run_topics_run_id_topic_id`
|
||||
|
||||
These additions close a previous gap where benchmark data existed in partial forms but lacked a stable relational backbone for auditing and historical analysis.
|
||||
|
||||
---
|
||||
|
||||
## ⚙️ Operator Notes
|
||||
|
||||
- Migration runs automatically on application startup.
|
||||
- No manual SQL is required in standard deployment paths.
|
||||
- Schedule rollout in a low-traffic window and take a backup snapshot before deployment.
|
||||
- If migration fails, do not retry repeatedly; inspect migration logs and lock state first.
|
||||
|
||||
---
|
||||
|
||||
## 🔒 Reliability & Risk
|
||||
|
||||
- Existing chat/session paths are unaffected unless benchmark features are enabled.
|
||||
- Migration is additive (new tables/indexes only), minimizing downgrade risk to existing entities.
|
||||
- Rollback should follow your standard DB restore or migration rollback policy if your environment requires strict reversibility.
|
||||
|
||||
---
|
||||
|
||||
## 👥 Owner
|
||||
|
||||
Migration owner: @{pr-author}
|
||||
|
||||
The migration owner is responsible for rollout follow-up and incident handling for this schema change.
|
||||
|
||||
> **Note for Claude**: Replace `{pr-author}` with the actual PR author. Retrieve via `gh pr view <number> --json author --jq '.author.login'` or from commit metadata. Do not hardcode a username.
|
||||
@@ -1,21 +0,0 @@
|
||||
# 🚀 LobeHub Release (20260427)
|
||||
|
||||
**Hotfix Scope:** Agent topic-switching regression — stale chat state on agent change
|
||||
|
||||
> Clears residual topic state when navigating between agents and restores blank-canvas behavior on agent switch.
|
||||
|
||||
## 🐛 What's Fixed
|
||||
|
||||
- **Stale topic on agent switch** — Switching from `/agent/agt_A/tpc_X` to `/agent/agt_B` no longer leaves the previous topic's messages on screen, and _Start new topic_ responds again. (#14231)
|
||||
- **Header & sidebar consistency** — Conversation header now shows the active subtopic's title, and the sidebar keeps the parent topic's thread list expanded while a thread is open.
|
||||
|
||||
## ⚙️ Upgrade
|
||||
|
||||
- Self-hosted: pull the new image and restart. No schema or env changes.
|
||||
- Cloud: applied automatically.
|
||||
|
||||
## 👥 Owner
|
||||
|
||||
@{pr-author}
|
||||
|
||||
> **Note for Claude**: Replace `{pr-author}` with the actual PR author. Retrieve via `gh pr view <number> --json author --jq '.author.login'`. Do not hardcode a username.
|
||||
@@ -1,80 +0,0 @@
|
||||
# 🚀 LobeHub Release (20260420)
|
||||
|
||||
**Release Date:** April 20, 2026\
|
||||
**Since previous release:** 96 commits · 58 merged PRs · 31 resolved issues · 17 contributors
|
||||
|
||||
> This weekly release focuses on reducing friction in everyday agent work: faster model routing, smoother gateway behavior, stronger task continuity, and clearer operator diagnostics when something goes wrong.
|
||||
|
||||
---
|
||||
|
||||
## ✨ Highlights
|
||||
|
||||
- **Gateway Session Recovery** — Agent sessions now recover more reliably after short network interruptions, so long-running tasks continue with less manual retry. (#10121, #10133)
|
||||
- **Fast Model Routing** — Expanded low-latency routing for priority model tiers, reducing wait time in high-frequency generation workflows. (#10102, #10117)
|
||||
- **Agent Task Workspace** — Running tasks now remain isolated from main chat state, which keeps primary conversations cleaner while background work progresses. (#10088)
|
||||
- **Provider Coverage Update** — Added support for new model variants across OpenAI-compatible and regional providers, improving fallback options in production. (#10094, #10109)
|
||||
- **Desktop Attachment Flow** — File and screenshot attachment behavior is more predictable in desktop sessions, especially for mixed text + media prompts. (#10073)
|
||||
- **Security Hardening Pass** — Closed multiple input validation gaps in webhook and file-path handling paths. (#10141, #10152)
|
||||
|
||||
---
|
||||
|
||||
## 🏗️ Core Agent & Architecture
|
||||
|
||||
### Agent loop and context handling
|
||||
|
||||
- Improved context compaction thresholds to reduce mid-task exits under tight token budgets. (#10079)
|
||||
- Added better diagnostics for tool-call truncation and recovery behavior during streamed responses. (#10106)
|
||||
- Refined delegate task activity propagation to improve parent-child task status consistency. (#10098)
|
||||
|
||||
### Provider and model behavior
|
||||
|
||||
- Unified provider-side timeout handling in fallback chains to reduce false failure classification. (#10097)
|
||||
- Updated reasoning-model defaults and response normalization for better cross-provider consistency. (#10109)
|
||||
|
||||
---
|
||||
|
||||
## 📱 Gateway & Platform Integrations
|
||||
|
||||
- Gateway now drains in-flight events more safely before restart, reducing duplicate notification bursts. (#10125)
|
||||
- Discord and Slack adapters received retry/backoff tuning for unstable webhook windows. (#10091, #10119)
|
||||
- WeCom callback-mode message state persistence now uses safer atomic updates. (#10114)
|
||||
|
||||
---
|
||||
|
||||
## 🖥️ CLI & User Experience
|
||||
|
||||
- Improved slash command discoverability in CLI and gateway contexts with clearer hint messages. (#10086)
|
||||
- `/model` switching feedback now returns clearer success/failure states in cross-platform chats. (#10108)
|
||||
- Setup flow now warns earlier about missing provider credentials in first-run scenarios. (#10115)
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Tooling
|
||||
|
||||
- MCP registration flow now validates duplicate tool names before activation, reducing runtime conflicts. (#10093)
|
||||
- Browser tooling improved stale-session cleanup to prevent orphaned local resources. (#10112)
|
||||
|
||||
---
|
||||
|
||||
## 🔒 Security & Reliability
|
||||
|
||||
- **Security:** Hardened path sanitization for uploaded assets and webhook callback validation. (#10141, #10152)
|
||||
- **Reliability:** Reduced empty-response retry storms by refining retry-classification conditions. (#10130)
|
||||
- **Reliability:** Improved timeout defaults for long-running background processes in constrained environments. (#10122)
|
||||
|
||||
---
|
||||
|
||||
## 👥 Contributors
|
||||
|
||||
**58 merged PRs** from **17 contributors** across **96 commits**.
|
||||
|
||||
### Community Contributors
|
||||
|
||||
- @alice-example - Gateway recovery and retry improvements
|
||||
- @bob-example - Provider fallback normalization
|
||||
- @charlie-example - Desktop media attachment flow
|
||||
- @dora-example - Webhook validation hardening
|
||||
|
||||
---
|
||||
|
||||
**Full Changelog**: <previous-tag>...<current-tag>
|
||||
@@ -1,47 +0,0 @@
|
||||
# Minor Release Workflow
|
||||
|
||||
Used to publish a new minor version (e.g. `v2.2.0`), roughly every 4 weeks. The PR title carries the exact version number; CI parses it to drive the rest of the release.
|
||||
|
||||
## Steps
|
||||
|
||||
1. **Create a release branch from canary**
|
||||
|
||||
```bash
|
||||
git checkout canary
|
||||
git pull origin canary
|
||||
git checkout -b release/v{version}
|
||||
git push -u origin release/v{version}
|
||||
```
|
||||
|
||||
2. **Determine the version number** — Read the current version from `package.json` and compute the next minor version (e.g. `2.1.x` → `2.2.0`).
|
||||
|
||||
3. **Create a PR to main**
|
||||
|
||||
```bash
|
||||
gh pr create \
|
||||
--title "🚀 release: v{version}" \
|
||||
--base main \
|
||||
--head release/v{version} \
|
||||
--body-file release_body.md
|
||||
```
|
||||
|
||||
> \[!IMPORTANT]
|
||||
> The PR title must strictly match the `🚀 release: v{x.y.z}` format. CI uses a regex on this title to determine the exact version number.
|
||||
|
||||
4. **Write the PR body as release notes** — Follow `release-notes-style.md`. Compare base is the latest semver tag on main (`git describe --tags --abbrev=0 origin/main`).
|
||||
|
||||
5. **Automatic trigger after merge** — `auto-tag-release` detects the title format, uses the version number from the title, bumps `package.json`, tags `v{x.y.z}`, creates the GitHub Release, and dispatches `sync-main-to-canary`.
|
||||
|
||||
## Scripts
|
||||
|
||||
```bash
|
||||
bun run release:branch # Interactive
|
||||
bun run release:branch --minor # Directly specify minor
|
||||
```
|
||||
|
||||
## Hard Rules (specific to Minor)
|
||||
|
||||
- PR title format is **strict**: `🚀 release: v{x.y.z}`. Any deviation falls through to patch detection.
|
||||
- Do **NOT** manually modify `package.json` version — CI will bump it.
|
||||
- Do **NOT** manually create the tag — CI will tag.
|
||||
- Highlights bullet count is usually 8–12 (see `release-notes-style.md` size heuristics).
|
||||
@@ -1,127 +0,0 @@
|
||||
# Patch Release Scenarios
|
||||
|
||||
All Patch Release scenarios automatically bump the patch version (e.g. 2.1.31 → 2.1.32). PR titles do not need to include a version number.
|
||||
|
||||
---
|
||||
|
||||
## 1. Weekly Release (canary → main)
|
||||
|
||||
The most common release type. Collects a week's worth of changes from canary and ships them to main.
|
||||
|
||||
### Steps
|
||||
|
||||
1. **Create release branch from canary**
|
||||
|
||||
```bash
|
||||
git checkout canary
|
||||
git pull origin canary
|
||||
git checkout -b release/weekly-{YYYYMMDD}
|
||||
git push -u origin release/weekly-{YYYYMMDD}
|
||||
```
|
||||
|
||||
2. **Scan changes and write changelog**
|
||||
|
||||
Compute the previous tag from main first — never reuse the last weekly's tag, since hotfixes published in between will be missed:
|
||||
|
||||
```bash
|
||||
git fetch origin main canary --tags
|
||||
PREV_TAG=$(git describe --tags --abbrev=0 origin/main --match 'v*.*.*' --exclude '*-canary*' --exclude '*-nightly*')
|
||||
git log "$PREV_TAG..origin/release/weekly-{YYYYMMDD}" --oneline --no-merges
|
||||
git diff "$PREV_TAG...origin/release/weekly-{YYYYMMDD}" --stat
|
||||
```
|
||||
|
||||
Then follow `./release-notes-style.md` § **Computing Inputs (Hard Rules)** to derive PR refs, metrics, and contributors. Every `(#XXXX)` in the body must come from actual commit subjects in this range — never inferred from descriptions.
|
||||
|
||||
3. **Create PR to main** with the changelog as the PR body
|
||||
|
||||
```bash
|
||||
gh pr create \
|
||||
--title "🚀 release: {YYYYMMDD}" \
|
||||
--base main \
|
||||
--head release/weekly-{YYYYMMDD} \
|
||||
--body-file changelog.md
|
||||
```
|
||||
|
||||
4. **After merge**: auto-tag-release detects `release/*` branch → auto patch +1.
|
||||
|
||||
---
|
||||
|
||||
## 2. Bug Hotfix
|
||||
|
||||
Emergency bug fix shipped directly from main.
|
||||
|
||||
### Steps
|
||||
|
||||
1. **Create hotfix branch from main**
|
||||
|
||||
```bash
|
||||
git checkout main
|
||||
git pull --rebase origin main
|
||||
git checkout -b hotfix/v{version}-{short-hash}
|
||||
git push -u origin hotfix/v{version}-{short-hash}
|
||||
```
|
||||
|
||||
2. **Create PR to main** with a gitmoji prefix title (e.g. `🐛 fix: description`)
|
||||
|
||||
3. **Write a short hotfix changelog** — See `changelog-example/hotfix.md`. Keep it minimal: scope line, 1-3 fix bullets (symptom + fix in one sentence), upgrade note, owner. No long root-cause section — that lives in the commit message.
|
||||
- **Hotfix owner**: Use the actual PR author (retrieve via `gh pr view <number> --json author --jq '.author.login'`), never hardcode a username.
|
||||
|
||||
4. **After merge**: auto-tag-release detects `hotfix/*` branch → auto patch +1.
|
||||
|
||||
### Script
|
||||
|
||||
```bash
|
||||
bun run hotfix:branch
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. New Model Launch
|
||||
|
||||
New AI model or provider support, typically contributed via community PRs.
|
||||
|
||||
### How it works
|
||||
|
||||
- Community contributors submit PRs with titles like `✨ feat: add xxx model` or `💄 style: support xxx models`
|
||||
- These PR title prefixes (`feat` / `style`) are in the auto-tag trigger list
|
||||
- No special branch naming or manual release steps required — merging the PR triggers auto patch +1
|
||||
|
||||
### When Claude is involved
|
||||
|
||||
If asked to add model support, just create a normal feature PR. The title prefix will trigger the release automatically.
|
||||
|
||||
---
|
||||
|
||||
## 4. DB Schema Migration
|
||||
|
||||
Database schema changes that need to be released independently. These require a dedicated changelog explaining the migration for self-hosted users.
|
||||
|
||||
### Steps
|
||||
|
||||
1. **Create release branch from main and cherry-pick migration commits**
|
||||
|
||||
```bash
|
||||
git checkout main
|
||||
git pull --rebase origin main
|
||||
git checkout -b release/db-migration-{name}
|
||||
git cherry-pick <migration-commit-hash>
|
||||
git push -u origin release/db-migration-{name}
|
||||
```
|
||||
|
||||
2. **Write a migration-specific changelog** — See `db-migration-changelog-example.md` for the format. This should explain:
|
||||
- What tables/columns are added, modified, or removed
|
||||
- Whether the migration is backwards-compatible
|
||||
- Any action required by self-hosted users
|
||||
- **Migration owner**: Use the actual PR author (retrieve via `gh pr view <number> --json author --jq '.author.login'` or `git log` commit author), never hardcode a username
|
||||
|
||||
3. **Create PR to main** with the migration changelog as the PR body
|
||||
|
||||
```bash
|
||||
gh pr create \
|
||||
--title "👷 build: {migration description}" \
|
||||
--base main \
|
||||
--head release/db-migration-{name} \
|
||||
--body-file changelog.md
|
||||
```
|
||||
|
||||
4. **After merge**: auto-tag-release detects `release/*` branch → auto patch +1.
|
||||
@@ -1,330 +0,0 @@
|
||||
# GitHub Release Changelog Standard (Long-Form Style)
|
||||
|
||||
Use this guide for **GitHub Release notes** — the body of a release PR that becomes the GitHub Release after merge. Do **not** use it for `docs/changelog/*.mdx` website pages (load `../../docs-changelog/SKILL.md` instead).
|
||||
|
||||
## Table of Contents
|
||||
|
||||
1. [Positioning](#positioning) — what this style optimizes for
|
||||
2. [Required Inputs Before Writing](#required-inputs-before-writing)
|
||||
3. [Computing Inputs (Hard Rules — Verify, Never Guess)](#computing-inputs-hard-rules--verify-never-guess) — base ref, PR refs, metrics, authors, pre-publish verification
|
||||
4. [Canonical Structure (Long-Form: Minor / Weekly)](#canonical-structure-long-form-minor--weekly)
|
||||
5. [Variants for Shorter Releases](#variants-for-shorter-releases) — hotfix, DB migration
|
||||
6. [Writing Rules (Hard)](#writing-rules-hard)
|
||||
7. [Style Rules (Long-Form)](#style-rules-long-form)
|
||||
8. [Release Size Heuristics](#release-size-heuristics) — when to use which variant
|
||||
9. [Contributor Ordering](#contributor-ordering)
|
||||
10. [Template](#template) — copy-paste skeleton
|
||||
11. [Quick Checklist](#quick-checklist) — long-form + hotfix
|
||||
|
||||
## Positioning
|
||||
|
||||
This release-note style is:
|
||||
|
||||
1. **Data-backed at the top** (date, range, key metrics)
|
||||
2. **Narrative first, then structured detail**
|
||||
3. **Deep but scannable** (clear sectioning + compact bullets)
|
||||
4. **Contributor-forward** (credits are part of the release story)
|
||||
|
||||
## Required Inputs Before Writing
|
||||
|
||||
Collect these inputs first:
|
||||
|
||||
1. Compare range (`<prev_tag>...<current_tag>`)
|
||||
2. Release metrics (commits, merged PRs, resolved issues, contributors, optional files/insertions/deletions)
|
||||
3. High-impact changes by domain (core loop, platform/gateway, UX, tooling, security, reliability)
|
||||
4. Contributor list (with standout contributions if known)
|
||||
5. Known risks / migrations / rollout notes (if any)
|
||||
|
||||
If metrics cannot be reliably computed, omit unknown numbers instead of guessing.
|
||||
|
||||
## Computing Inputs (Hard Rules — Verify, Never Guess)
|
||||
|
||||
> Hallucinated PR numbers and wrong "Since v..." bases are the #1 failure mode of this skill. Every number and every `(#XXXX)` must come from `git`, never from memory or inference.
|
||||
|
||||
### 1. Compare base = latest semver tag on `main`
|
||||
|
||||
Do **not** eyeball the tag list or pick the "last weekly" PR. Compute it:
|
||||
|
||||
```bash
|
||||
git fetch origin main canary --tags
|
||||
PREV_TAG=$(git describe --tags --abbrev=0 origin/main --match 'v*.*.*' --exclude '*-canary*' --exclude '*-nightly*')
|
||||
echo "$PREV_TAG"
|
||||
```
|
||||
|
||||
Sanity check that the tag is reachable from the release branch:
|
||||
|
||||
```bash
|
||||
git merge-base --is-ancestor "$PREV_TAG" origin/release/weekly-{YYYYMMDD} && echo OK
|
||||
```
|
||||
|
||||
If the check fails, stop and ask the user — the release branch is based on the wrong source.
|
||||
|
||||
> **Why not "the last weekly release PR"?** Hotfixes (`v2.1.54`, `v2.1.55`, …) merge directly into main between weeklies. They get back-merged via `sync-main-to-canary`, so the latest semver tag on main _is_ the correct previous release for both weekly and minor flows. Picking the previous weekly's tag will silently undercount and put a stale version in "Since v…".
|
||||
|
||||
### 2. PR refs must come from commit subjects — never from descriptions
|
||||
|
||||
Compute the canonical set:
|
||||
|
||||
```bash
|
||||
git log "$PREV_TAG..origin/release/weekly-{YYYYMMDD}" \
|
||||
--pretty=format:'%s' --no-merges \
|
||||
| grep -oE '\(#[0-9]+\)$' \
|
||||
| sort -u > /tmp/release_prs.txt
|
||||
```
|
||||
|
||||
Hard rules:
|
||||
|
||||
- Every `(#XXXX)` you write in the body **must** appear in `/tmp/release_prs.txt`. No exceptions.
|
||||
- Never infer a PR number from a feature description. If you remember "the KB BM25 PR was around #14501", that memory is wrong about half the time. Look up the commit hash by feature keyword and read its actual subject.
|
||||
- If your terminal truncates long subjects (any wrapper that compresses output, e.g. `rtk`), bypass it. With `rtk` use `rtk proxy git log …`. Verify with `wc -l /tmp/release_prs.txt` — the count must match `git log $PREV_TAG..HEAD --no-merges --pretty=format:'%h' | wc -l` minus the few commits without a PR ref. A mismatch of >5% means subjects are being silently truncated.
|
||||
|
||||
### 3. Metrics must come from git counts
|
||||
|
||||
```bash
|
||||
PR_COUNT=$(wc -l < /tmp/release_prs.txt | tr -d ' ')
|
||||
|
||||
COMMIT_COUNT=$(git log "$PREV_TAG..origin/release/weekly-{YYYYMMDD}" --no-merges --pretty=format:'%h' | wc -l | tr -d ' ')
|
||||
|
||||
CONTRIBUTOR_COUNT=$(git log "$PREV_TAG..origin/release/weekly-{YYYYMMDD}" --no-merges --pretty=format:'%an' \
|
||||
| sort -u \
|
||||
| grep -viE '^(lobehubbot|LobeHub Bot|renovate\[bot\])$' \
|
||||
| wc -l | tr -d ' ')
|
||||
```
|
||||
|
||||
If a number cannot be confidently derived, omit it — never guess.
|
||||
|
||||
### 4. Author-to-handle resolution
|
||||
|
||||
Git `%an` is the commit author display name, not the GitHub handle. For each author you mention, confirm the handle:
|
||||
|
||||
```bash
|
||||
gh pr view "$PR_NUMBER" --repo lobehub/lobe-chat --json author --jq '.author.login'
|
||||
```
|
||||
|
||||
Use the result for `@handle`. Then classify each author per the `LobeHub team roster` below; community first, team after.
|
||||
|
||||
### 5. Pre-publish verification (mandatory)
|
||||
|
||||
Before `gh pr create` / `gh pr edit --body-file`, diff body PR refs against the canonical set:
|
||||
|
||||
```bash
|
||||
grep -oE '#[0-9]+' release_body.md | sort -u > /tmp/body_prs.txt
|
||||
sed 's/[()]//g' /tmp/release_prs.txt > /tmp/release_prs_clean.txt
|
||||
|
||||
echo "=== In body but NOT in actual range (must be EMPTY) ==="
|
||||
comm -23 /tmp/body_prs.txt /tmp/release_prs_clean.txt
|
||||
```
|
||||
|
||||
Empty diff = OK. Any output = the body cites a PR that wasn't merged in this range. Stop and fix before publishing.
|
||||
|
||||
Also verify the metrics line in the body matches the computed values (`PR_COUNT`, `CONTRIBUTOR_COUNT`) and that `**Full Changelog**` uses `$PREV_TAG`, not some older tag.
|
||||
|
||||
## Canonical Structure (Long-Form: Minor / Weekly)
|
||||
|
||||
Follow this section order for **Minor** and **Weekly** releases unless the user asks otherwise. For **Hotfix** and **DB Migration**, see § Variants for Shorter Releases below — the canonical structure does not apply.
|
||||
|
||||
1. `# 🚀 LobeHub Release (<YYYYMMDD>)`
|
||||
2. Metadata lines:
|
||||
- `Release Date`
|
||||
- `Since <Previous Version>` metrics
|
||||
3. One quoted release thesis (single paragraph, 1-2 lines)
|
||||
4. `## ✨ Highlights` (6-12 bullets for major releases; 3-8 for weekly)
|
||||
5. Domain blocks with optional `###` subsections:
|
||||
- `## 🏗️ Core Agent & Architecture` (or equivalent product core)
|
||||
- `## 📱 Platforms / Integrations`
|
||||
- `## 🖥️ CLI & User Experience`
|
||||
- `## 🔧 Tooling`
|
||||
- `## 🔒 Security & Reliability`
|
||||
- `## 📚 Documentation` (optional if meaningful)
|
||||
6. `## 👥 Contributors`
|
||||
7. `**Full Changelog**: <prev>...<current>`
|
||||
|
||||
Use `---` separators between major blocks for long releases.
|
||||
|
||||
## Variants for Shorter Releases
|
||||
|
||||
The Canonical Structure above is for **long-form** (Minor / Weekly). Two short-form variants override it.
|
||||
|
||||
### Hotfix Variant
|
||||
|
||||
A hotfix targets one regression and ships fast. The body is short and operator-focused — no Highlights, no domain blocks, no Contributors line.
|
||||
|
||||
Required sections, in order:
|
||||
|
||||
1. `# 🚀 LobeHub Release (<YYYYMMDD>)`
|
||||
2. `**Hotfix Scope:**` — one line summarizing the regression scope (e.g. `Agent topic-switching regression — stale chat state on agent change`). Replaces the long-form `Release Date` / `Since vX.Y.Z` metrics.
|
||||
3. One quoted thesis (single paragraph, 1-2 lines) describing what is now restored.
|
||||
4. `## 🐛 What's Fixed` — 1-3 bullets, each `**<symptom>** — <fix in one sentence>. (#PR)`. No root-cause prose; that lives in the commit message.
|
||||
5. `## ⚙️ Upgrade` — short notes for self-hosted (pull image / restart, schema or env changes) and cloud (usually "applied automatically").
|
||||
6. `## 👥 Owner` — single `@handle` for the PR author, resolved via `gh pr view "$PR" --json author --jq '.author.login'`. Never hardcoded.
|
||||
|
||||
Hard rules specific to hotfix:
|
||||
|
||||
- **No Highlights / domain blocks / Contributors / Full Changelog** — these add noise to a one-shot fix.
|
||||
- **No metric line** — `Since vX.Y.Z` doesn't apply; the body cites the single PR (or 1-3 PRs) directly.
|
||||
- **Owner ≠ Contributors** — one author, listed under § Owner. Not a flat handle list.
|
||||
- See `changelog-example/hotfix.md` for the canonical template.
|
||||
|
||||
### DB Migration Variant
|
||||
|
||||
Database schema changes that need to be released independently. Operator impact is the headline.
|
||||
|
||||
Required sections, in order:
|
||||
|
||||
1. `# 🚀 LobeHub Release (<YYYYMMDD>)` + scope line
|
||||
2. **Migration overview** — what tables / columns are added, modified, or removed
|
||||
3. **Operator impact** — backwards-compatible? required actions for self-hosted?
|
||||
4. **Rollback / backup note** — how to recover
|
||||
5. `## 👥 Owner` — single PR author, resolved via `gh pr view`
|
||||
|
||||
See `changelog-example/db-migration.md` for the canonical template.
|
||||
|
||||
## Writing Rules (Hard)
|
||||
|
||||
1. **No fabricated metrics**: all numbers must be traceable.
|
||||
2. **No vague headline bullets**: each bullet must include capability + impact.
|
||||
3. **No internal-only framing**: phrase from user/operator perspective.
|
||||
4. **Security must be explicit** when security-sensitive fixes are present.
|
||||
5. **PR/issue linkage**: use `(#1234)` when IDs are available.
|
||||
6. **Terminology consistency**: same feature/provider name across sections.
|
||||
7. **Do not bury migration or breaking changes**: elevate to dedicated section or callout.
|
||||
|
||||
## Style Rules (Long-Form)
|
||||
|
||||
1. Start with an "everyday use" framing, not implementation internals.
|
||||
2. Mix narrative sentence + evidence bullets.
|
||||
3. Keep bullets compact but informative:
|
||||
- Good: `**Fast Mode (`/fast`)** — Priority routing for OpenAI and Anthropic, reducing latency on supported models. (#6875, #6960)`
|
||||
4. Use bold only for capability names, not for whole sentences.
|
||||
5. Keep heading depth ≤ 3 levels.
|
||||
|
||||
## Release Size Heuristics
|
||||
|
||||
- **Minor / major milestone release**
|
||||
- Long-form structure with multiple domain blocks.
|
||||
- `Highlights` usually 8-12 bullets.
|
||||
- **Weekly patch release**
|
||||
- Long-form skeleton with reduced subsection count.
|
||||
- `Highlights` usually 4-8 bullets.
|
||||
- **Hotfix release**
|
||||
- Short-form (see § Variants → Hotfix). No Highlights, no domain blocks, no Contributors.
|
||||
- 1-3 fix bullets. Body should fit on one screen.
|
||||
- **DB migration release**
|
||||
- Short-form (see § Variants → DB Migration).
|
||||
- Must include `Migration overview`, operator impact, and rollback/backup note.
|
||||
|
||||
## Contributor Ordering
|
||||
|
||||
Render contributors as a **single flat list** (no separate "Community" / "Core Team" subsections). Order: **community contributors first, team members after**. Within each group, sort by PR count desc. Bots (`@lobehubbot`, `renovate[bot]`) go on a separate "maintenance" line.
|
||||
|
||||
**LobeHub team roster** — anyone in this list is a team member; anyone not in this list is a community contributor:
|
||||
|
||||
- @arvinxx
|
||||
- @Innei
|
||||
- @tjx666 (commit author name: YuTengjing)
|
||||
- @LiJian
|
||||
- @Neko
|
||||
- @Rdmclin2
|
||||
- @AmAzing129
|
||||
- @sudongyuer (commit author name: Tsuki)
|
||||
- @rivertwilight (commit author name: René Wang)
|
||||
- @CanisMinor
|
||||
- @cy948 (commit author name: Rylan Cai)
|
||||
|
||||
> **Resolving handles** — git author names (e.g. `YuTengjing`) are not always the GitHub handle. Verify via `gh pr view "$PR" --json author` or `gh api search/users -f q='<email>'` before listing.
|
||||
|
||||
If a new contributor appears who is not on this list, treat them as community by default and ask the user whether to add them to the roster.
|
||||
|
||||
## Template
|
||||
|
||||
```md
|
||||
# 🚀 LobeHub Release (<YYYYMMDD>)
|
||||
|
||||
**Release Date:** <Month DD, YYYY>
|
||||
**Since <Previous Version>:** <N merged PRs> · <N resolved issues> · <N contributors>
|
||||
|
||||
> <One release thesis sentence: what this release unlocks in practice.>
|
||||
|
||||
---
|
||||
|
||||
## ✨ Highlights
|
||||
|
||||
- **<Capability A>** — <What changed and why it matters>. (#1234)
|
||||
- **<Capability B>** — <What changed and why it matters>. (#2345)
|
||||
- **<Capability C>** — <What changed and why it matters>. (#3456)
|
||||
|
||||
---
|
||||
|
||||
## 🏗️ Core Product & Architecture
|
||||
|
||||
### <Subdomain>
|
||||
|
||||
- <Concrete change + impact>. (#...)
|
||||
- <Concrete change + impact>. (#...)
|
||||
|
||||
---
|
||||
|
||||
## 📱 Platforms / Integrations
|
||||
|
||||
- <Platform update + impact>. (#...)
|
||||
- <Compatibility/reliability fix + impact>. (#...)
|
||||
|
||||
---
|
||||
|
||||
## 🖥️ CLI & User Experience
|
||||
|
||||
- <User-facing workflow improvement>. (#...)
|
||||
- <Quality-of-life fix>. (#...)
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Tooling
|
||||
|
||||
- <Tool/runtime improvement>. (#...)
|
||||
|
||||
---
|
||||
|
||||
## 🔒 Security & Reliability
|
||||
|
||||
- **Security:** <hardening or vulnerability fix>. (#...)
|
||||
- **Reliability:** <stability/performance behavior improvement>. (#...)
|
||||
|
||||
---
|
||||
|
||||
## 👥 Contributors
|
||||
|
||||
Huge thanks to **<N contributors>** who shipped **<N merged PRs>** this cycle.
|
||||
|
||||
@<community-handle> · @<community-handle> · @<team-handle> · @<team-handle>
|
||||
|
||||
Plus @lobehubbot and renovate[bot] for maintenance.
|
||||
|
||||
---
|
||||
|
||||
**Full Changelog**: <previous_tag>...<current_tag>
|
||||
```
|
||||
|
||||
## Quick Checklist
|
||||
|
||||
### Long-Form (Minor / Weekly)
|
||||
|
||||
- [ ] `PREV_TAG` is `git describe --tags --abbrev=0 origin/main` (latest semver), not the last weekly's tag
|
||||
- [ ] Every `(#XXXX)` in the body appears in `/tmp/release_prs.txt` (verified via `comm -23`)
|
||||
- [ ] `Since v…` line uses `$PREV_TAG`; PR / contributor counts match `wc -l` on the computed sets
|
||||
- [ ] `**Full Changelog**` uses `$PREV_TAG...release/weekly-<YYYYMMDD>` (or `…v{x.y.z}` for minor)
|
||||
- [ ] Author handles resolved via `gh pr view --json author`, not assumed from `%an`
|
||||
- [ ] Uses top metadata and a clear release thesis
|
||||
- [ ] Includes `Highlights` plus domain-grouped sections
|
||||
- [ ] Every major bullet states both change and user/operator impact
|
||||
- [ ] Security and reliability updates are explicitly surfaced (when present)
|
||||
- [ ] Contributor credits and compare range are included
|
||||
- [ ] All numbers and claims are verifiable
|
||||
|
||||
### Hotfix
|
||||
|
||||
- [ ] `**Hotfix Scope:**` line replaces metrics line
|
||||
- [ ] Single quoted thesis describes what is restored (operator-facing, not internal)
|
||||
- [ ] `## 🐛 What's Fixed` has 1-3 bullets, each `**<symptom>** — <fix>. (#PR)` with PR ref verified to exist and be merged
|
||||
- [ ] `## ⚙️ Upgrade` notes self-hosted action and cloud auto-apply
|
||||
- [ ] `## 👥 Owner` is a single `@handle` resolved via `gh pr view "$PR" --json author`
|
||||
- [ ] No Highlights / domain blocks / Contributors / Full Changelog included
|
||||
@@ -1,238 +0,0 @@
|
||||
---
|
||||
name: zustand
|
||||
description: "LobeHub Zustand store conventions: public/internal/dispatch action layers, optimistic update pattern, slice composition via `flattenActions`, and class-based action migration. Use whenever working under `src/store/**`, adding a `createXxxSlice`, writing `internal_*` or `internal_dispatch*` actions, designing `messagesMap`/`topicsMap` reducers, refactoring a `StateCreator` object slice into a `XxxActionImpl` class, or debugging stale store reads. Triggers on `useChatStore`/`useUserStore`/`useGlobalStore`, `createStore`, `flattenActions`, `StoreSetter`, `internal_dispatch`, 'add an action', 'zustand selector', 'store slice', 'class action', 'optimistic update'."
|
||||
user-invocable: false
|
||||
---
|
||||
|
||||
# LobeHub Zustand State Management
|
||||
|
||||
## Action Type Hierarchy
|
||||
|
||||
### 1. Public Actions
|
||||
|
||||
Main interfaces for UI components:
|
||||
|
||||
- Naming: Verb form (`createTopic`, `sendMessage`)
|
||||
- Responsibilities: Parameter validation, flow orchestration
|
||||
|
||||
### 2. Internal Actions (`internal_*`)
|
||||
|
||||
Core business logic implementation:
|
||||
|
||||
- Naming: `internal_` prefix (`internal_createTopic`)
|
||||
- Responsibilities: Optimistic updates, service calls, error handling
|
||||
- Should not be called directly by UI
|
||||
|
||||
### 3. Dispatch Methods (`internal_dispatch*`)
|
||||
|
||||
State update handlers:
|
||||
|
||||
- Naming: `internal_dispatch` + entity (`internal_dispatchTopic`)
|
||||
- Responsibilities: Calling reducers, updating store
|
||||
|
||||
## When to Use Reducer vs Simple `set`
|
||||
|
||||
**Use Reducer Pattern:**
|
||||
|
||||
- Managing object lists/maps (`messagesMap`, `topicMaps`)
|
||||
- Optimistic updates
|
||||
- Complex state transitions
|
||||
|
||||
**Use Simple `set`:**
|
||||
|
||||
- Toggling booleans
|
||||
- Updating simple values
|
||||
- Setting single state fields
|
||||
|
||||
## Optimistic Update Pattern
|
||||
|
||||
```typescript
|
||||
internal_createTopic: async (params) => {
|
||||
const tmpId = Date.now().toString();
|
||||
|
||||
// 1. Immediately update frontend (optimistic)
|
||||
get().internal_dispatchTopic(
|
||||
{ type: 'addTopic', value: { ...params, id: tmpId } },
|
||||
'internal_createTopic'
|
||||
);
|
||||
|
||||
// 2. Call backend service
|
||||
const topicId = await topicService.createTopic(params);
|
||||
|
||||
// 3. Refresh for consistency
|
||||
await get().refreshTopic();
|
||||
return topicId;
|
||||
},
|
||||
```
|
||||
|
||||
**Delete operations**: Don't use optimistic updates (destructive, complex recovery)
|
||||
|
||||
## Naming Conventions
|
||||
|
||||
**Actions:**
|
||||
|
||||
- Public: `createTopic`, `sendMessage`
|
||||
|
||||
- Internal: `internal_createTopic`, `internal_updateMessageContent`
|
||||
|
||||
- Dispatch: `internal_dispatchTopic`
|
||||
**State:**
|
||||
|
||||
- ID arrays: `topicEditingIds`
|
||||
|
||||
- Maps: `topicMaps`, `messagesMap`
|
||||
|
||||
- Active: `activeTopicId`
|
||||
|
||||
- Init flags: `topicsInit`
|
||||
|
||||
## Detailed Guides
|
||||
|
||||
- Action patterns: `references/action-patterns.md`
|
||||
- Slice organization: `references/slice-organization.md`
|
||||
|
||||
## Class-Based Action Implementation
|
||||
|
||||
We are migrating slices from plain `StateCreator` objects to **class-based actions**.
|
||||
|
||||
### Pattern
|
||||
|
||||
- Define a class that encapsulates actions and receives `(set, get, api)` in the constructor.
|
||||
- Use `#private` fields (e.g., `#set`, `#get`) to avoid leaking internals.
|
||||
- Prefer shared typing helpers:
|
||||
- `StoreSetter<T>` from `@/store/types` for `set`.
|
||||
- `Pick<ActionImpl, keyof ActionImpl>` to expose only public methods.
|
||||
- Export a `create*Slice` helper that returns a class instance.
|
||||
|
||||
```ts
|
||||
type Setter = StoreSetter<HomeStore>;
|
||||
export const createRecentSlice = (set: Setter, get: () => HomeStore, _api?: unknown) =>
|
||||
new RecentActionImpl(set, get, _api);
|
||||
|
||||
export class RecentActionImpl {
|
||||
readonly #get: () => HomeStore;
|
||||
readonly #set: Setter;
|
||||
|
||||
constructor(set: Setter, get: () => HomeStore, _api?: unknown) {
|
||||
void _api;
|
||||
this.#set = set;
|
||||
this.#get = get;
|
||||
}
|
||||
|
||||
useFetchRecentTopics = () => {
|
||||
// ...
|
||||
};
|
||||
}
|
||||
|
||||
export type RecentAction = Pick<RecentActionImpl, keyof RecentActionImpl>;
|
||||
```
|
||||
|
||||
### Composition
|
||||
|
||||
- In store files, merge class instances with `flattenActions` (do not spread class instances).
|
||||
- `flattenActions` binds methods to the original class instance and supports prototype methods and class fields.
|
||||
|
||||
```ts
|
||||
const createStore: StateCreator<HomeStore, [['zustand/devtools', never]]> = (...params) => ({
|
||||
...initialState,
|
||||
...flattenActions<HomeStoreAction>([
|
||||
createRecentSlice(...params),
|
||||
createHomeInputSlice(...params),
|
||||
]),
|
||||
});
|
||||
```
|
||||
|
||||
### Multi-Class Slices
|
||||
|
||||
- For large slices that need multiple action classes, compose them in the slice entry using `flattenActions`.
|
||||
- Use a local `PublicActions<T>` helper if you need to combine multiple classes and hide private fields.
|
||||
|
||||
```ts
|
||||
type PublicActions<T> = { [K in keyof T]: T[K] };
|
||||
|
||||
export type ChatGroupAction = PublicActions<
|
||||
ChatGroupInternalAction & ChatGroupLifecycleAction & ChatGroupMemberAction & ChatGroupCurdAction
|
||||
>;
|
||||
|
||||
export const chatGroupAction: StateCreator<
|
||||
ChatGroupStore,
|
||||
[['zustand/devtools', never]],
|
||||
[],
|
||||
ChatGroupAction
|
||||
> = (...params) =>
|
||||
flattenActions<ChatGroupAction>([
|
||||
new ChatGroupInternalAction(...params),
|
||||
new ChatGroupLifecycleAction(...params),
|
||||
new ChatGroupMemberAction(...params),
|
||||
new ChatGroupCurdAction(...params),
|
||||
]);
|
||||
```
|
||||
|
||||
### Store-Access Types
|
||||
|
||||
- For class methods that depend on actions in other classes, define explicit store augmentations:
|
||||
- `ChatGroupStoreWithSwitchTopic` for lifecycle `switchTopic`
|
||||
- `ChatGroupStoreWithRefresh` for member refresh
|
||||
- `ChatGroupStoreWithInternal` for curd `internal_dispatchChatGroup`
|
||||
|
||||
### Slices That Don't Currently Need `set`
|
||||
|
||||
When a slice doesn't write local state at the moment — e.g. it reads context
|
||||
from `#get()` and forwards calls to another store, or just runs hooks — drop
|
||||
the `#set` field. Otherwise ESLint's `no-unused-vars` flags the unused private
|
||||
field.
|
||||
|
||||
Mark the constructor's `set` param as `_set` and `void _set` it to keep the
|
||||
`(set, get, api)` shape aligned with `StateCreator`. This is **a snapshot of
|
||||
the current need, not a permanent contract** — if a later change needs `set`,
|
||||
restore the `#set` field and use it; do not invent a workaround to keep the
|
||||
"unused" form.
|
||||
|
||||
```ts
|
||||
type Setter = StoreSetter<ConversationStore>;
|
||||
|
||||
export const toolSlice = (set: Setter, get: () => ConversationStore, _api?: unknown) =>
|
||||
new ToolActionImpl(set, get, _api);
|
||||
|
||||
export class ToolActionImpl {
|
||||
readonly #get: () => ConversationStore;
|
||||
|
||||
// Mark unused params with `_` prefix and `void _x` so the constructor still
|
||||
// matches StateCreator's `(set, get, api)` shape without triggering unused
|
||||
// diagnostics.
|
||||
constructor(_set: Setter, get: () => ConversationStore, _api?: unknown) {
|
||||
void _set;
|
||||
void _api;
|
||||
this.#get = get;
|
||||
}
|
||||
|
||||
approveToolCall = async (id: string) => {
|
||||
const { context, hooks } = this.#get();
|
||||
await useChatStore.getState().approveToolCalling(id, '', context);
|
||||
hooks.onToolCallComplete?.(id, undefined);
|
||||
};
|
||||
}
|
||||
|
||||
export type ToolAction = Pick<ToolActionImpl, keyof ToolActionImpl>;
|
||||
```
|
||||
|
||||
Rules of thumb:
|
||||
|
||||
- If a slice doesn't currently call `set`, drop `#set` (use `_set` + `void _set`
|
||||
in the constructor). When a later edit needs `set`, restore `#set` and use it.
|
||||
- Don't add `setNamespace` for slices that don't write state. Add it when the
|
||||
slice starts writing state.
|
||||
- Never leave `#set` declared but unused "for future use" — lint will fail and
|
||||
re-adding it later costs nothing.
|
||||
|
||||
### Do / Don't
|
||||
|
||||
- **Do**: keep constructor signature aligned with `StateCreator` params `(set, get, api)`.
|
||||
- **Do**: use `#private` to avoid `set/get` being exposed.
|
||||
- **Do**: use `flattenActions` instead of spreading class instances.
|
||||
- **Do**: drop `#set` (and use `_set` + `void _set` in the constructor) for
|
||||
delegate-only slices that never write state — keeps lint green without
|
||||
breaking the `(set, get, api)` shape.
|
||||
- **Don't**: keep both old slice objects and class actions active at the same time.
|
||||
- **Don't**: keep an unused `#set` field "for future use" — it fails ESLint and
|
||||
re-adding it later costs nothing.
|
||||
@@ -1,122 +0,0 @@
|
||||
# Zustand Action Patterns
|
||||
|
||||
## Optimistic Update Implementation
|
||||
|
||||
### Standard Flow
|
||||
|
||||
```typescript
|
||||
internal_updateMessageContent: async (id, content, extra) => {
|
||||
const { internal_dispatchMessage, refreshMessages } = get();
|
||||
|
||||
// 1. Immediately update frontend
|
||||
internal_dispatchMessage({
|
||||
id,
|
||||
type: 'updateMessage',
|
||||
value: { content },
|
||||
});
|
||||
|
||||
// 2. Call backend
|
||||
await messageService.updateMessage(id, { content });
|
||||
|
||||
// 3. Refresh for consistency
|
||||
await refreshMessages();
|
||||
},
|
||||
```
|
||||
|
||||
### Create Operations
|
||||
|
||||
```typescript
|
||||
internal_createMessage: async (message, context) => {
|
||||
let tempId = context?.tempMessageId;
|
||||
if (!tempId) {
|
||||
tempId = internal_createTmpMessage(message);
|
||||
}
|
||||
|
||||
try {
|
||||
const id = await messageService.createMessage(message);
|
||||
await refreshMessages();
|
||||
return id;
|
||||
} catch (e) {
|
||||
internal_dispatchMessage({
|
||||
id: tempId,
|
||||
type: 'updateMessage',
|
||||
value: { error: { type: ChatErrorType.CreateMessageError } },
|
||||
});
|
||||
}
|
||||
},
|
||||
```
|
||||
|
||||
### Delete Operations (No Optimistic Update)
|
||||
|
||||
```typescript
|
||||
internal_removeGenerationTopic: async (id: string) => {
|
||||
get().internal_updateGenerationTopicLoading(id, true);
|
||||
|
||||
try {
|
||||
await generationTopicService.deleteTopic(id);
|
||||
await get().refreshGenerationTopics();
|
||||
} finally {
|
||||
get().internal_updateGenerationTopicLoading(id, false);
|
||||
}
|
||||
},
|
||||
```
|
||||
|
||||
## Loading State Management
|
||||
|
||||
```typescript
|
||||
// Define in initialState.ts
|
||||
export interface ChatMessageState {
|
||||
messageEditingIds: string[];
|
||||
}
|
||||
|
||||
// Manage in action
|
||||
toggleMessageEditing: (id, editing) => {
|
||||
set(
|
||||
{ messageEditingIds: toggleBooleanList(get().messageEditingIds, id, editing) },
|
||||
false,
|
||||
'toggleMessageEditing',
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
## SWR Integration
|
||||
|
||||
```typescript
|
||||
useFetchMessages: (enable, sessionId, activeTopicId) =>
|
||||
useClientDataSWR<ChatMessage[]>(
|
||||
enable ? [SWR_USE_FETCH_MESSAGES, sessionId, activeTopicId] : null,
|
||||
async ([, sessionId, topicId]) => messageService.getMessages(sessionId, topicId),
|
||||
{
|
||||
onSuccess: (messages) => {
|
||||
const nextMap = { ...get().messagesMap, [messageMapKey(sessionId, activeTopicId)]: messages };
|
||||
if (get().messagesInit && isEqual(nextMap, get().messagesMap)) return;
|
||||
set({ messagesInit: true, messagesMap: nextMap }, false, n('useFetchMessages'));
|
||||
},
|
||||
}
|
||||
),
|
||||
|
||||
// Cache invalidation
|
||||
refreshMessages: async () => {
|
||||
await mutate([SWR_USE_FETCH_MESSAGES, get().activeId, get().activeTopicId]);
|
||||
};
|
||||
```
|
||||
|
||||
## Reducer Pattern
|
||||
|
||||
```typescript
|
||||
export const messagesReducer = (state: ChatMessage[], payload: MessageDispatch): ChatMessage[] => {
|
||||
switch (payload.type) {
|
||||
case 'updateMessage': {
|
||||
return produce(state, (draftState) => {
|
||||
const index = draftState.findIndex((i) => i.id === payload.id);
|
||||
if (index < 0) return;
|
||||
draftState[index] = merge(draftState[index], {
|
||||
...payload.value,
|
||||
updatedAt: Date.now(),
|
||||
});
|
||||
});
|
||||
}
|
||||
// ...other cases
|
||||
}
|
||||
};
|
||||
```
|
||||
@@ -1,131 +0,0 @@
|
||||
# Zustand Slice Organization
|
||||
|
||||
## Top-Level Store Structure
|
||||
|
||||
Key aggregation files:
|
||||
|
||||
- `src/store/chat/initialState.ts`: Aggregate all slice initial states
|
||||
- `src/store/chat/store.ts`: Define top-level `ChatStore`, combine all slice actions
|
||||
- `src/store/chat/selectors.ts`: Export all slice selectors
|
||||
- `src/store/chat/helpers.ts`: Chat helper functions
|
||||
|
||||
## Store Aggregation Pattern
|
||||
|
||||
```typescript
|
||||
// src/store/chat/initialState.ts
|
||||
import { ChatTopicState, initialTopicState } from './slices/topic/initialState';
|
||||
import { ChatMessageState, initialMessageState } from './slices/message/initialState';
|
||||
|
||||
export type ChatStoreState = ChatTopicState & ChatMessageState & ...
|
||||
|
||||
export const initialState: ChatStoreState = {
|
||||
...initialMessageState,
|
||||
...initialTopicState,
|
||||
...
|
||||
};
|
||||
|
||||
// src/store/chat/store.ts
|
||||
export interface ChatStoreAction
|
||||
extends ChatMessageAction, ChatTopicAction, ...
|
||||
|
||||
const createStore: StateCreator<ChatStore, [['zustand/devtools', never]]> = (...params) => ({
|
||||
...initialState,
|
||||
...chatMessage(...params),
|
||||
...chatTopic(...params),
|
||||
});
|
||||
|
||||
export const useChatStore = createWithEqualityFn<ChatStore>()(
|
||||
subscribeWithSelector(devtools(createStore)),
|
||||
shallow
|
||||
);
|
||||
```
|
||||
|
||||
## Single Slice Structure
|
||||
|
||||
```plaintext
|
||||
src/store/chat/slices/
|
||||
└── [sliceName]/
|
||||
├── action.ts # Define actions (or actions/ directory)
|
||||
├── initialState.ts # State structure and initial values
|
||||
├── reducer.ts # (Optional) Reducer pattern
|
||||
├── selectors.ts # Define selectors
|
||||
└── index.ts # (Optional) Re-exports
|
||||
```
|
||||
|
||||
### initialState.ts
|
||||
|
||||
```typescript
|
||||
export interface ChatTopicState {
|
||||
activeTopicId?: string;
|
||||
topicMaps: Record<string, ChatTopic[]>;
|
||||
topicsInit: boolean;
|
||||
topicLoadingIds: string[];
|
||||
}
|
||||
|
||||
export const initialTopicState: ChatTopicState = {
|
||||
activeTopicId: undefined,
|
||||
topicMaps: {},
|
||||
topicsInit: false,
|
||||
topicLoadingIds: [],
|
||||
};
|
||||
```
|
||||
|
||||
### selectors.ts
|
||||
|
||||
```typescript
|
||||
const currentTopics = (s: ChatStoreState): ChatTopic[] | undefined => s.topicMaps[s.activeId];
|
||||
|
||||
const getTopicById =
|
||||
(id: string) =>
|
||||
(s: ChatStoreState): ChatTopic | undefined =>
|
||||
currentTopics(s)?.find((topic) => topic.id === id);
|
||||
|
||||
// Core pattern: Use xxxSelectors aggregate
|
||||
export const topicSelectors = {
|
||||
currentTopics,
|
||||
getTopicById,
|
||||
};
|
||||
```
|
||||
|
||||
## Complex Actions Sub-directory
|
||||
|
||||
```plaintext
|
||||
src/store/chat/slices/aiChat/
|
||||
├── actions/
|
||||
│ ├── generateAIChat.ts
|
||||
│ ├── rag.ts
|
||||
│ ├── memory.ts
|
||||
│ └── index.ts
|
||||
├── initialState.ts
|
||||
└── selectors.ts
|
||||
```
|
||||
|
||||
## State Design Patterns
|
||||
|
||||
### Map Structure for Associated Data
|
||||
|
||||
```typescript
|
||||
topicMaps: Record<string, ChatTopic[]>;
|
||||
messagesMap: Record<string, ChatMessage[]>;
|
||||
```
|
||||
|
||||
### Arrays for Loading State
|
||||
|
||||
```typescript
|
||||
messageLoadingIds: string[]
|
||||
topicLoadingIds: string[]
|
||||
```
|
||||
|
||||
### Optional Fields for Active Items
|
||||
|
||||
```typescript
|
||||
activeId: string
|
||||
activeTopicId?: string
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Slice division**: By functional domain (message, topic, aiChat)
|
||||
2. **File naming**: camelCase for directories, consistent patterns
|
||||
3. **State structure**: Flat, avoid deep nesting
|
||||
4. **Type safety**: Clear TypeScript interfaces for each slice
|
||||
@@ -29,35 +29,11 @@ Prioritize modules with business logic:
|
||||
|
||||
## Workflow
|
||||
|
||||
### 0. Pre-check: Scan Existing Test PRs
|
||||
|
||||
Before selecting a module, **MUST** scan existing PRs to avoid duplicate work:
|
||||
|
||||
1. **List in-flight PRs**:
|
||||
|
||||
```bash
|
||||
gh pr list --search "automatic/add-tests-" --state open --json number,title,headRefName,mergeable
|
||||
```
|
||||
|
||||
2. **Close conflicting PRs**: For any PR where `mergeable` is `"CONFLICTING"`, close it with a comment:
|
||||
|
||||
```bash
|
||||
gh pr close <number> --comment "Closing: this PR has merge conflicts with main and is outdated. A new test PR may be created for this module."
|
||||
```
|
||||
|
||||
3. **Build exclusion list**: Extract module names from the remaining open PR branch names (`automatic/add-tests-<module-name>-<date>`), and **exclude those modules** from selection in the next step.
|
||||
|
||||
4. **Output summary** (for logging):
|
||||
- Total open test PRs found
|
||||
- PRs closed due to conflicts
|
||||
- Modules currently in-flight (excluded from selection)
|
||||
|
||||
### 1. Select a Module to Process
|
||||
|
||||
**Selection Strategy**:
|
||||
|
||||
- Randomly pick ONE module from the target directories
|
||||
- **MUST skip modules that already have an open PR** (from step 0's exclusion list)
|
||||
- Prioritize modules that:
|
||||
- Have significant business logic
|
||||
- Have no or minimal test coverage
|
||||
@@ -162,15 +138,11 @@ describe('ModuleName', () => {
|
||||
### 5. Create Pull Request
|
||||
|
||||
- Create a new branch: `automatic/add-tests-[module-name]-[date]`
|
||||
|
||||
- Commit changes with message format:
|
||||
|
||||
```
|
||||
✅ test: add unit tests for [module-name]
|
||||
```
|
||||
|
||||
- Push the branch
|
||||
|
||||
- Create a PR with:
|
||||
|
||||
- Title: `✅ test: add unit tests for [module-name]`
|
||||
@@ -202,7 +174,6 @@ describe('ModuleName', () => {
|
||||
- Test approach: [brief description]
|
||||
|
||||
---
|
||||
|
||||
🤖 Generated with [Claude Code](https://claude.com/claude-code)
|
||||
```
|
||||
|
||||
|
||||
@@ -1,510 +0,0 @@
|
||||
# E2E BDD Test Coverage Assistant
|
||||
|
||||
You are an E2E testing assistant. Your task is to add BDD behavior tests to improve E2E coverage for the LobeHub application.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Before starting, read the following documents:
|
||||
|
||||
- `e2e/CLAUDE.md` - E2E testing guide and best practices
|
||||
- `e2e/docs/local-setup.md` - Local environment setup
|
||||
|
||||
## Target Modules
|
||||
|
||||
Based on the product architecture, prioritize modules by coverage status:
|
||||
|
||||
| Module | Sub-features | Priority | Status |
|
||||
| ---------------- | --------------------------------------------------- | -------- | ------ |
|
||||
| **Agent** | Builder, Conversation, Task | P0 | 🚧 |
|
||||
| **Agent Group** | Builder, Group Chat | P0 | ⏳ |
|
||||
| **Page (Docs)** | Sidebar CRUD ✅, Title/Emoji ✅, Rich Text ✅, Copilot | P0 | 🚧 |
|
||||
| **Knowledge** | Create, Upload, RAG Conversation | P1 | ⏳ |
|
||||
| **Memory** | View, Edit, Associate | P2 | ⏳ |
|
||||
| **Home Sidebar** | Agent Mgmt, Group Mgmt | P1 | ✅ |
|
||||
| **Community** | Browse, Interactions, Detail Pages | P1 | ✅ |
|
||||
| **Settings** | User Settings, Model Provider | P2 | ⏳ |
|
||||
|
||||
## Workflow
|
||||
|
||||
### 1. Analyze Current Coverage
|
||||
|
||||
**Step 1.1**: List existing feature files
|
||||
|
||||
```bash
|
||||
find e2e/src/features -name "*.feature" -type f
|
||||
```
|
||||
|
||||
**Step 1.2**: Review the product modules in `src/app/[variants]/(main)/` to identify untested user journeys
|
||||
|
||||
**Step 1.3**: Check `e2e/CLAUDE.md` for the coverage matrix and identify gaps
|
||||
|
||||
### 2. Select a Module to Test
|
||||
|
||||
**Selection Criteria**:
|
||||
|
||||
- Choose ONE module that is NOT yet covered or has incomplete coverage
|
||||
- Prioritize by: P0 > P1 > P2
|
||||
- Focus on user journeys that represent core product value
|
||||
|
||||
**Module granularity examples**:
|
||||
|
||||
- Agent conversation flow
|
||||
- Knowledge base RAG workflow
|
||||
- Settings configuration flow
|
||||
- Page document CRUD operations
|
||||
|
||||
### 3. Create Module Directory and README
|
||||
|
||||
**Step 3.1**: Create dedicated feature directory
|
||||
|
||||
```bash
|
||||
mkdir -p e2e/src/features/{module-name}
|
||||
```
|
||||
|
||||
**Step 3.2**: Create README.md with feature inventory
|
||||
|
||||
Create `e2e/src/features/{module-name}/README.md` with:
|
||||
|
||||
- Module overview and routes
|
||||
- Feature inventory table (功能点、描述、优先级、状态、测试文件)
|
||||
- Test file structure
|
||||
- Execution commands
|
||||
- Known issues
|
||||
|
||||
**Example structure** (see `e2e/src/features/page/README.md`):
|
||||
|
||||
```markdown
|
||||
# {Module} 模块 E2E 测试覆盖
|
||||
|
||||
## 模块概述
|
||||
|
||||
**路由**: `/module`, `/module/[id]`
|
||||
|
||||
## 功能清单与测试覆盖
|
||||
|
||||
### 1. 功能分组名称
|
||||
|
||||
| 功能点 | 描述 | 优先级 | 状态 | 测试文件 |
|
||||
| ------ | ---- | ------ | ---- | ------------- |
|
||||
| 功能A | xxx | P0 | ✅ | `xxx.feature` |
|
||||
| 功能B | xxx | P1 | ⏳ | |
|
||||
|
||||
## 测试文件结构
|
||||
|
||||
## 测试执行
|
||||
|
||||
## 已知问题
|
||||
|
||||
## 更新记录
|
||||
```
|
||||
|
||||
### 4. Explore Module Features
|
||||
|
||||
**Step 4.1**: Use Task tool to explore the module
|
||||
|
||||
```
|
||||
Use the Task tool with subagent_type=Explore to thoroughly explore:
|
||||
- Route structure in src/app/[variants]/(main)/{module}/
|
||||
- Feature components in src/features/
|
||||
- Store actions in src/store/{module}/
|
||||
- All user interactions (buttons, menus, forms)
|
||||
```
|
||||
|
||||
**Step 4.2**: Document all features in README.md
|
||||
|
||||
Group features by user journey area (e.g., Sidebar, Editor Header, Editor Content, etc.)
|
||||
|
||||
### 5. Design Test Scenarios
|
||||
|
||||
**Step 5.1**: Create feature files by functional area
|
||||
|
||||
Feature file location: `e2e/src/features/{module}/{area}.feature`
|
||||
|
||||
**Naming conventions**:
|
||||
|
||||
- `crud.feature` - Basic CRUD operations
|
||||
- `editor-meta.feature` - Editor metadata (title, icon)
|
||||
- `editor-content.feature` - Rich text editing
|
||||
- `copilot.feature` - AI copilot interactions
|
||||
|
||||
**Feature file template**:
|
||||
|
||||
```gherkin
|
||||
@journey @P0 @{module-tag}
|
||||
Feature: {Feature Name in Chinese}
|
||||
|
||||
作为用户,我希望能够 {user goal},
|
||||
以便 {business value}
|
||||
|
||||
Background:
|
||||
Given 用户已登录系统
|
||||
|
||||
# ============================================
|
||||
# 功能分组注释
|
||||
# ============================================
|
||||
|
||||
@{MODULE-AREA-001}
|
||||
Scenario: {Scenario description in Chinese}
|
||||
Given {precondition}
|
||||
When {user action}
|
||||
Then {expected outcome}
|
||||
And {additional verification}
|
||||
```
|
||||
|
||||
**Tag conventions**:
|
||||
|
||||
```gherkin
|
||||
@journey # User journey test (experience baseline)
|
||||
@smoke # Smoke test (quick validation)
|
||||
@regression # Regression test
|
||||
@skip # Skip this test (known issue)
|
||||
|
||||
@P0 # Highest priority (CI must run)
|
||||
@P1 # High priority (Nightly)
|
||||
@P2 # Medium priority (Pre-release)
|
||||
|
||||
@agent # Agent module
|
||||
@agent-group # Agent Group module
|
||||
@page # Page/Docs module
|
||||
@knowledge # Knowledge base module
|
||||
@memory # Memory module
|
||||
@settings # Settings module
|
||||
@home # Home sidebar module
|
||||
```
|
||||
|
||||
### 6. Implement Step Definitions
|
||||
|
||||
**Step 6.1**: Create step definition file
|
||||
|
||||
Location: `e2e/src/steps/{module}/{area}.steps.ts`
|
||||
|
||||
**Step definition template**:
|
||||
|
||||
```typescript
|
||||
/**
|
||||
* {Module} {Area} Steps
|
||||
*
|
||||
* Step definitions for {description}
|
||||
*/
|
||||
import { Given, When, Then } from '@cucumber/cucumber';
|
||||
import { expect } from '@playwright/test';
|
||||
|
||||
import { CustomWorld } from '../../support/world';
|
||||
|
||||
// ============================================
|
||||
// Given Steps
|
||||
// ============================================
|
||||
|
||||
Given('用户打开一个文稿编辑器', async function (this: CustomWorld) {
|
||||
console.log(' 📍 Step: 创建并打开一个文稿...');
|
||||
// Implementation
|
||||
console.log(' ✅ 已打开文稿编辑器');
|
||||
});
|
||||
|
||||
// ============================================
|
||||
// When Steps
|
||||
// ============================================
|
||||
|
||||
When('用户点击标题输入框', async function (this: CustomWorld) {
|
||||
console.log(' 📍 Step: 点击标题输入框...');
|
||||
// Implementation
|
||||
console.log(' ✅ 已点击标题输入框');
|
||||
});
|
||||
|
||||
// ============================================
|
||||
// Then Steps
|
||||
// ============================================
|
||||
|
||||
Then('文稿标题应该更新为 {string}', async function (this: CustomWorld, title: string) {
|
||||
console.log(` 📍 Step: 验证标题为 "${title}"...`);
|
||||
// Assertions
|
||||
console.log(` ✅ 标题已更新为 "${title}"`);
|
||||
});
|
||||
```
|
||||
|
||||
**Step 6.2**: Add hooks if needed
|
||||
|
||||
Update `e2e/src/steps/hooks.ts` for new tag prefixes:
|
||||
|
||||
```typescript
|
||||
const testId = pickle.tags.find(
|
||||
(tag) =>
|
||||
tag.name.startsWith('@COMMUNITY-') ||
|
||||
tag.name.startsWith('@AGENT-') ||
|
||||
tag.name.startsWith('@HOME-') ||
|
||||
tag.name.startsWith('@PAGE-') || // Add new prefix
|
||||
tag.name.startsWith('@ROUTES-'),
|
||||
);
|
||||
```
|
||||
|
||||
### 7. Setup Mocks (If Needed)
|
||||
|
||||
For LLM-related tests, use the mock framework:
|
||||
|
||||
```typescript
|
||||
import { llmMockManager, presetResponses } from '../../mocks/llm';
|
||||
|
||||
// Setup mock before navigation
|
||||
llmMockManager.setResponse('user message', 'Expected AI response');
|
||||
await llmMockManager.setup(this.page);
|
||||
```
|
||||
|
||||
### 8. Run and Verify Tests
|
||||
|
||||
**Step 8.1**: Start local environment
|
||||
|
||||
```bash
|
||||
# From project root
|
||||
bun e2e/scripts/setup.ts --start
|
||||
```
|
||||
|
||||
**Step 8.2**: Run dry-run first to verify step definitions
|
||||
|
||||
```bash
|
||||
cd e2e
|
||||
BASE_URL=http://localhost:3006 \
|
||||
DATABASE_URL=postgresql://postgres:postgres@localhost:5433/postgres \
|
||||
pnpm exec cucumber-js --config cucumber.config.js --tags "@{module-tag}" --dry-run
|
||||
```
|
||||
|
||||
**Step 8.3**: Run the new tests
|
||||
|
||||
```bash
|
||||
# Run specific test by tag
|
||||
HEADLESS=false BASE_URL=http://localhost:3006 \
|
||||
DATABASE_URL=postgresql://postgres:postgres@localhost:5433/postgres \
|
||||
pnpm exec cucumber-js --config cucumber.config.js --tags "@{TEST-ID}"
|
||||
|
||||
# Run all module tests (excluding skipped)
|
||||
HEADLESS=true BASE_URL=http://localhost:3006 \
|
||||
DATABASE_URL=postgresql://postgres:postgres@localhost:5433/postgres \
|
||||
pnpm exec cucumber-js --config cucumber.config.js --tags "@{module-tag} and not @skip"
|
||||
```
|
||||
|
||||
**Step 8.4**: Fix any failures
|
||||
|
||||
- Check screenshots in `e2e/screenshots/`
|
||||
- Adjust selectors and waits as needed
|
||||
- For flaky tests, add `@skip` tag and document in README known issues
|
||||
- Ensure tests pass consistently
|
||||
|
||||
### 9. Update Documentation
|
||||
|
||||
**Step 9.1**: Update module README.md
|
||||
|
||||
- Mark completed features with ✅
|
||||
- Update test statistics
|
||||
- Add any known issues
|
||||
|
||||
**Step 9.2**: Update this prompt file
|
||||
|
||||
- Update module status in Target Modules table
|
||||
- Add any new best practices learned
|
||||
|
||||
### 10. Create Pull Request
|
||||
|
||||
- Branch name: `test/e2e-{module-name}`
|
||||
|
||||
- Commit message format:
|
||||
|
||||
```
|
||||
✅ test: add E2E tests for {module-name}
|
||||
```
|
||||
|
||||
- PR title: `✅ test: add E2E tests for {module-name}`
|
||||
|
||||
- PR body template:
|
||||
|
||||
````markdown
|
||||
## Summary
|
||||
|
||||
- Added E2E BDD tests for `{module-name}`
|
||||
- Feature files added: [number]
|
||||
- Scenarios covered: [number]
|
||||
|
||||
## Test Coverage
|
||||
|
||||
- [x] Feature area 1: {description}
|
||||
- [x] Feature area 2: {description}
|
||||
- [ ] Feature area 3: {pending}
|
||||
|
||||
## Test Execution
|
||||
|
||||
```bash
|
||||
# Run these tests
|
||||
cd e2e && pnpm exec cucumber-js --config cucumber.config.js --tags "@{module-tag} and not @skip"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
🤖 Generated with [Claude Code](https://claude.com/claude-code)
|
||||
````
|
||||
|
||||
## Important Rules
|
||||
|
||||
- **DO** write feature files in Chinese (贴近产品需求)
|
||||
- **DO** add appropriate tags (@journey, @P0/@P1/@P2, @module-name)
|
||||
- **DO** mock LLM responses for stability
|
||||
- **DO** add console logs in step definitions for debugging
|
||||
- **DO** handle element visibility issues (desktop/mobile dual components)
|
||||
- **DO** use `page.waitForTimeout()` for animation/transition waits
|
||||
- **DO** support both Chinese and English text (e.g., `/^(无标题|Untitled)$/`)
|
||||
- **DO** create unique test data with timestamps to avoid conflicts
|
||||
- **DO NOT** depend on actual LLM API calls
|
||||
- **DO NOT** create flaky tests (ensure stability before PR)
|
||||
- **DO NOT** modify production code unless adding data-testid attributes
|
||||
- **DO NOT** skip running tests locally before creating PR
|
||||
|
||||
## Element Locator Best Practices
|
||||
|
||||
### Rich Text Editor (contenteditable)
|
||||
|
||||
```typescript
|
||||
// Correct way to input in contenteditable
|
||||
const editor = this.page.locator('[contenteditable="true"]').first();
|
||||
await editor.click();
|
||||
await this.page.waitForTimeout(500);
|
||||
await this.page.keyboard.type(message, { delay: 30 });
|
||||
```
|
||||
|
||||
### Slash Commands
|
||||
|
||||
```typescript
|
||||
// Type slash and wait for menu to appear
|
||||
await this.page.keyboard.type('/', { delay: 100 });
|
||||
await this.page.waitForTimeout(800); // Wait for slash menu
|
||||
|
||||
// Type command shortcut
|
||||
await this.page.keyboard.type('h1', { delay: 80 });
|
||||
await this.page.keyboard.press('Enter');
|
||||
```
|
||||
|
||||
### Handling i18n (Chinese/English)
|
||||
|
||||
```typescript
|
||||
// Support both languages for default values
|
||||
const defaultTitleRegex = /^(无标题|Untitled)$/;
|
||||
const pageItem = this.page.getByText(defaultTitleRegex).first();
|
||||
|
||||
// Or for buttons
|
||||
const button = this.page.getByRole('button', { name: /choose.*icon|选择图标/i });
|
||||
```
|
||||
|
||||
### Creating Unique Test Data
|
||||
|
||||
```typescript
|
||||
// Use timestamps to avoid conflicts between test runs
|
||||
const uniqueTitle = `E2E Page ${Date.now()}`;
|
||||
```
|
||||
|
||||
### Handling Multiple Matches
|
||||
|
||||
```typescript
|
||||
// Use .first() or .nth() for multiple matches
|
||||
const element = this.page.locator('[data-testid="item"]').first();
|
||||
|
||||
// Or filter by visibility
|
||||
const items = await this.page.locator('[data-testid="item"]').all();
|
||||
for (const item of items) {
|
||||
if (await item.isVisible()) {
|
||||
await item.click();
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Adding data-testid
|
||||
|
||||
If needed for reliable element selection, add `data-testid` to components:
|
||||
|
||||
```tsx
|
||||
<Component data-testid="unique-identifier" />
|
||||
```
|
||||
|
||||
## Common Test Patterns
|
||||
|
||||
### Navigation Test
|
||||
|
||||
```gherkin
|
||||
Scenario: 用户导航到目标页面
|
||||
Given 用户已登录系统
|
||||
When 用户点击侧边栏的 "{menu-item}"
|
||||
Then 应该跳转到 "{expected-url}"
|
||||
And 页面标题应包含 "{expected-title}"
|
||||
```
|
||||
|
||||
### CRUD Test
|
||||
|
||||
```gherkin
|
||||
Scenario: 创建新项目
|
||||
Given 用户已登录系统
|
||||
When 用户点击创建按钮
|
||||
And 用户输入名称 "{name}"
|
||||
And 用户点击保存
|
||||
Then 应该看到新创建的项目 "{name}"
|
||||
|
||||
Scenario: 编辑项目
|
||||
Given 用户已创建项目 "{name}"
|
||||
When 用户打开项目编辑
|
||||
And 用户修改名称为 "{new-name}"
|
||||
And 用户保存更改
|
||||
Then 项目名称应更新为 "{new-name}"
|
||||
|
||||
Scenario: 删除项目
|
||||
Given 用户已创建项目 "{name}"
|
||||
When 用户删除该项目
|
||||
And 用户确认删除
|
||||
Then 项目列表中不应包含 "{name}"
|
||||
```
|
||||
|
||||
### Editor Title/Meta Test
|
||||
|
||||
```gherkin
|
||||
Scenario: 编辑文稿标题
|
||||
Given 用户打开一个文稿编辑器
|
||||
When 用户点击标题输入框
|
||||
And 用户输入标题 "我的测试文稿"
|
||||
And 用户按下 Enter 键
|
||||
Then 文稿标题应该更新为 "我的测试文稿"
|
||||
```
|
||||
|
||||
### Rich Text Editor Test
|
||||
|
||||
```gherkin
|
||||
Scenario: 通过斜杠命令插入一级标题
|
||||
Given 用户打开一个文稿编辑器
|
||||
When 用户点击编辑器内容区域
|
||||
And 用户输入斜杠命令 "/h1"
|
||||
And 用户按下 Enter 键
|
||||
And 用户输入文本 "一级标题内容"
|
||||
Then 编辑器应该包含一级标题
|
||||
```
|
||||
|
||||
### LLM Interaction Test
|
||||
|
||||
```gherkin
|
||||
Scenario: AI 对话基本流程
|
||||
Given 用户已登录系统
|
||||
And LLM Mock 已配置
|
||||
When 用户发送消息 "{user-message}"
|
||||
Then 应该收到 AI 回复 "{expected-response}"
|
||||
And 消息应显示在对话历史中
|
||||
```
|
||||
|
||||
## Debugging Tips
|
||||
|
||||
1. **Use HEADLESS=false** to see browser actions
|
||||
2. **Check screenshots** in `e2e/screenshots/` on failure
|
||||
3. **Add console.log** in step definitions
|
||||
4. **Increase timeouts** for slow operations
|
||||
5. **Use `page.pause()`** for interactive debugging
|
||||
6. **Run dry-run first** to verify all step definitions exist
|
||||
7. **Use @skip tag** for known flaky tests, document in README
|
||||
|
||||
## Reference Implementations
|
||||
|
||||
See these completed modules for reference:
|
||||
|
||||
- **Page module**: `e2e/src/features/page/` - Full implementation with README, multiple feature files
|
||||
- **Community module**: `e2e/src/features/community/` - Smoke and interaction tests
|
||||
- **Home sidebar**: `e2e/src/features/home/` - Agent and Group management tests
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user