@loom-framework/core 0.1.0-alpha.180 → 0.1.0-alpha.181

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/builtin-skills/app-skill/SKILL.md +4 -5
  2. package/builtin-skills/app-skill/references/auth.md +79 -79
  3. package/builtin-skills/app-skill/references/events.md +237 -19
  4. package/builtin-skills/app-skill/references/evolution.md +59 -59
  5. package/builtin-skills/app-skill/references/model.md +48 -48
  6. package/builtin-skills/app-skill/references/notification.md +47 -47
  7. package/builtin-skills/app-skill/references/process-builder.md +173 -197
  8. package/builtin-skills/app-skill/references/process.md +274 -163
  9. package/builtin-skills/loom/SKILL.md +17 -10
  10. package/builtin-skills/loom/references/data-model.md +19 -3
  11. package/dist/backend/auth/rbac.js +1 -1
  12. package/dist/backend/auth/rbac.js.map +1 -1
  13. package/dist/backend/events/subscription-service.d.ts +46 -0
  14. package/dist/backend/events/subscription-service.d.ts.map +1 -0
  15. package/dist/backend/events/subscription-service.js +209 -0
  16. package/dist/backend/events/subscription-service.js.map +1 -0
  17. package/dist/backend/index.d.ts +1 -0
  18. package/dist/backend/index.d.ts.map +1 -1
  19. package/dist/backend/index.js +27 -13
  20. package/dist/backend/index.js.map +1 -1
  21. package/dist/backend/lifecycle/manager.d.ts +4 -2
  22. package/dist/backend/lifecycle/manager.d.ts.map +1 -1
  23. package/dist/backend/lifecycle/manager.js +9 -3
  24. package/dist/backend/lifecycle/manager.js.map +1 -1
  25. package/dist/backend/loom-paths.d.ts +3 -1
  26. package/dist/backend/loom-paths.d.ts.map +1 -1
  27. package/dist/backend/loom-paths.js +5 -1
  28. package/dist/backend/loom-paths.js.map +1 -1
  29. package/dist/backend/process/engine.d.ts +15 -1
  30. package/dist/backend/process/engine.d.ts.map +1 -1
  31. package/dist/backend/process/engine.js +79 -23
  32. package/dist/backend/process/engine.js.map +1 -1
  33. package/dist/backend/process/registry.d.ts +44 -9
  34. package/dist/backend/process/registry.d.ts.map +1 -1
  35. package/dist/backend/process/registry.js +208 -40
  36. package/dist/backend/process/registry.js.map +1 -1
  37. package/dist/backend/process/trigger.d.ts +18 -8
  38. package/dist/backend/process/trigger.d.ts.map +1 -1
  39. package/dist/backend/process/trigger.js +86 -32
  40. package/dist/backend/process/trigger.js.map +1 -1
  41. package/dist/backend/routes/ai-config.d.ts.map +1 -1
  42. package/dist/backend/routes/ai-config.js +1 -69
  43. package/dist/backend/routes/ai-config.js.map +1 -1
  44. package/dist/backend/routes/config-writer.d.ts +94 -0
  45. package/dist/backend/routes/config-writer.d.ts.map +1 -0
  46. package/dist/backend/routes/config-writer.js +522 -0
  47. package/dist/backend/routes/config-writer.js.map +1 -0
  48. package/dist/backend/routes/event-routes.d.ts +9 -2
  49. package/dist/backend/routes/event-routes.d.ts.map +1 -1
  50. package/dist/backend/routes/event-routes.js +101 -7
  51. package/dist/backend/routes/event-routes.js.map +1 -1
  52. package/dist/backend/routes/process-routes.d.ts +3 -0
  53. package/dist/backend/routes/process-routes.d.ts.map +1 -1
  54. package/dist/backend/routes/process-routes.js +120 -49
  55. package/dist/backend/routes/process-routes.js.map +1 -1
  56. package/dist/cli/commands/event.d.ts +12 -0
  57. package/dist/cli/commands/event.d.ts.map +1 -0
  58. package/dist/cli/commands/event.js +332 -0
  59. package/dist/cli/commands/event.js.map +1 -0
  60. package/dist/cli/commands/process.d.ts.map +1 -1
  61. package/dist/cli/commands/process.js +82 -44
  62. package/dist/cli/commands/process.js.map +1 -1
  63. package/dist/cli/generators/capability-generator.d.ts.map +1 -1
  64. package/dist/cli/generators/capability-generator.js +28 -32
  65. package/dist/cli/generators/capability-generator.js.map +1 -1
  66. package/dist/cli/helpers/system-page-config.d.ts +1 -1
  67. package/dist/cli/helpers/system-page-config.d.ts.map +1 -1
  68. package/dist/cli/helpers/system-page-config.js +9 -0
  69. package/dist/cli/helpers/system-page-config.js.map +1 -1
  70. package/dist/cli/index.d.ts.map +1 -1
  71. package/dist/cli/index.js +2 -0
  72. package/dist/cli/index.js.map +1 -1
  73. package/dist/cli/templates/event-management-page.d.ts +8 -0
  74. package/dist/cli/templates/event-management-page.d.ts.map +1 -0
  75. package/dist/cli/templates/event-management-page.js +598 -0
  76. package/dist/cli/templates/event-management-page.js.map +1 -0
  77. package/dist/cli/templates/login-page.d.ts +1 -1
  78. package/dist/cli/templates/login-page.js +1 -1
  79. package/dist/cli/templates/model-management-page.d.ts +1 -1
  80. package/dist/cli/templates/model-management-page.js +1 -1
  81. package/dist/cli/templates/notification-center-page.d.ts +1 -1
  82. package/dist/cli/templates/notification-center-page.js +1 -1
  83. package/dist/cli/templates/notification-detail-page.d.ts +1 -1
  84. package/dist/cli/templates/notification-detail-page.js +1 -1
  85. package/dist/cli/templates/process-management-page.d.ts +1 -1
  86. package/dist/cli/templates/process-management-page.js +1 -1
  87. package/dist/cli/templates/skill-management-page.d.ts +1 -1
  88. package/dist/cli/templates/skill-management-page.js +1 -1
  89. package/dist/cli/templates/user-management-page.d.ts +1 -1
  90. package/dist/cli/templates/user-management-page.js +1 -1
  91. package/dist/config.d.ts +327 -11
  92. package/dist/config.d.ts.map +1 -1
  93. package/dist/config.js +26 -7
  94. package/dist/config.js.map +1 -1
  95. package/dist/index.d.ts +1 -0
  96. package/dist/index.d.ts.map +1 -1
  97. package/dist/index.js.map +1 -1
  98. package/dist/types/event.d.ts +6 -0
  99. package/dist/types/event.d.ts.map +1 -1
  100. package/dist/types/index.d.ts +1 -1
  101. package/dist/types/index.d.ts.map +1 -1
  102. package/dist/types/process.d.ts +45 -7
  103. package/dist/types/process.d.ts.map +1 -1
  104. package/package.json +1 -1
  105. package/builtin-skills/app-skill/references/process-metrics.md +0 -93
@@ -1,10 +1,8 @@
1
1
  ---
2
2
  name: {{projectName}}
3
3
  description: |
4
- ⚠️ TODO: 根据下方 [TODO] 标记补全触发短语。阅读 references/models.md 中的数据模型和 AI 按钮定义,
5
- 编写触发短语,包含:用户语言中的常见增删改查动词、各数据模型相关的操作、AI 按钮的标签。另外后续是一些系统操作短语,修改时需保留:<!-- AUTH_DESC --><!-- EVENTS_DESC --><!-- PROCESS_DESC --><!-- NOTIFICATION_DESC -->
6
- 格式:"This skill should be used when the user asks to '短语1', '短语2',
7
- or any request about <此项目管理的内容> in the {{projectName}} project."
4
+ This skill should be used when the user asks to "[TODO: 补全数据模型相关短语,如 '查看告警', '添加任务']"<!-- AUTH_DESC --><!-- EVENTS_DESC --><!-- PROCESS_DESC --><!-- NOTIFICATION_DESC -->,
5
+ or any request about managing data, processes, events, or notifications in the {{projectName}} project.
8
6
  version: 1.0.0
9
7
  ---
10
8
 
@@ -27,6 +25,7 @@ version: 1.0.0
27
25
  - models.md — 当前项目的数据模型字段表(字段名、类型、enum 取值、默认值)、AI 按钮定义(id、label、prompt、placement)、CLI 命令语法(read/write/update/delete,不要使用 create/list 等不存在的命令)。
28
26
  - data-semantics.md — 当前项目的数据模型名称列表、write 的 --data JSON 构造规则(字段类型格式、enum 取值约束、贴合业务的真实示例值)、read 的 --filter JSON 构造规则(优先用 enum/boolean 字段筛选、跨模型关联查询)。
29
27
  <!-- AUTH_SCENARIOS_LOAD -->
28
+ <!-- EVENTS_SCENARIOS_LOAD -->
30
29
  <!-- PROCESS_SCENARIOS_LOAD -->
31
30
  <!-- NOTIFICATION_SCENARIOS_LOAD -->
32
31
 
@@ -41,7 +40,7 @@ version: 1.0.0
41
40
  <!-- CLI_AUTH -->
42
41
  - 数据模型结构、CLI 语法和操作规范:**references/models.md**
43
42
  - 语义引导(示例数据生成、筛选选择、关系推断):**references/data-semantics.md**
44
- - 鉴权与 Token 注入:**references/auth.md** — Web 对话和 Process 执行时 `LOOM_TOKEN` 将自动注入,`loom data` 命令无需添加 `--token`;本地终端执行时才需显式传 `--token`。
43
+ - AI 模型管理(model list/add/update/remove、能力层级、Process 模型绑定):**references/model.md**
45
44
  <!-- AUTH_REF -->
46
45
  <!-- EVENTS_REF -->
47
46
  <!-- PROCESS_REF -->
@@ -1,89 +1,89 @@
1
- # Auth Commands Reference
1
+ # 认证命令参考
2
2
 
3
- ## Authentication Commands
3
+ ## 认证命令
4
4
 
5
- ### Login
5
+ ### 登录
6
6
  ```bash
7
7
  loom auth login -u <username> -p <password>
8
- loom auth login # interactive prompt
8
+ loom auth login # 交互式提示
9
9
  ```
10
10
 
11
- ### Register (admin token required)
11
+ ### 注册(需要 admin 令牌)
12
12
  ```bash
13
13
  loom auth register -u <username> -p <password> [--role <role>] --token <admin-jwt>
14
14
  ```
15
15
 
16
- ### Logout
16
+ ### 登出
17
17
  ```bash
18
18
  loom auth logout --refresh-token <token> --token <jwt>
19
19
  ```
20
20
 
21
- ### Whoami
21
+ ### 查看当前用户
22
22
  ```bash
23
23
  loom auth whoami --token <jwt>
24
24
  ```
25
25
 
26
- ## User Management (Admin Only)
26
+ ## 用户管理(仅管理员)
27
27
 
28
- ### Add User
28
+ ### 添加用户
29
29
  ```bash
30
30
  loom user add <username> -p <password> -r <role> --token <admin-jwt>
31
31
  ```
32
32
 
33
- ### List Users
33
+ ### 列出用户
34
34
  ```bash
35
35
  loom user list --token <jwt>
36
36
  ```
37
37
 
38
- ### Update User
38
+ ### 更新用户
39
39
  ```bash
40
40
  loom user update <id> -r <role> --token <jwt>
41
41
  loom user update <id> -p <newpassword> --token <jwt>
42
42
  ```
43
43
 
44
- ### Delete User
44
+ ### 删除用户
45
45
  ```bash
46
46
  loom user delete <id> --token <admin-jwt>
47
47
  ```
48
48
 
49
- ### Change Role
49
+ ### 修改角色
50
50
  ```bash
51
51
  loom user role <id> <role> --token <admin-jwt>
52
52
  ```
53
53
 
54
- ## Role Management
54
+ ## 角色管理
55
55
 
56
- ### List Roles
56
+ ### 列出角色
57
57
  ```bash
58
58
  loom role list [--token <jwt>]
59
59
  ```
60
- Note: `role list` does not require authentication (the endpoint is public).
60
+ 注意:`role list` 不需要认证(该端点是公开的)。
61
61
 
62
- ### Create Role (admin required)
62
+ ### 创建角色(需要管理员权限)
63
63
  ```bash
64
64
  loom role create <name> -p '[{"model":"*","level":"read"}]' --token <admin-jwt>
65
65
  ```
66
66
 
67
- ### Update Role (admin required)
67
+ ### 更新角色(需要管理员权限)
68
68
  ```bash
69
69
  loom role update <name> -n <new-name> --token <admin-jwt>
70
70
  loom role update <name> -p '[{"model":"items","level":"write"}]' --token <admin-jwt>
71
71
  ```
72
72
 
73
- ### Delete Role (admin required)
73
+ ### 删除角色(需要管理员权限)
74
74
  ```bash
75
75
  loom role delete <name> --token <admin-jwt>
76
76
  ```
77
- Cannot delete a role that still has assigned users.
77
+ 无法删除仍有关联用户的角色。
78
78
 
79
- ### Permission Levels
79
+ ### 权限等级
80
80
  ```bash
81
81
  loom role permissions
82
82
  ```
83
83
 
84
- ## Data Commands with Auth
84
+ ## 带认证的数据命令
85
85
 
86
- When auth is enabled, data commands route through HTTP API and require token:
86
+ 启用认证后,数据命令通过 HTTP API 路由,需要令牌:
87
87
 
88
88
  ```bash
89
89
  loom data read <model> --token <jwt>
@@ -93,33 +93,33 @@ loom data delete <model> --id <id> --token <jwt>
93
93
  loom data schema <model> --token <jwt>
94
94
  ```
95
95
 
96
- ## Automatic Token Injection
96
+ ## 自动令牌注入
97
97
 
98
- In Web chat and Process execution scenarios, `LOOM_TOKEN` is automatically injected into the environment — `--token` is not needed:
98
+ Web 聊天和 Process 执行场景中,`LOOM_TOKEN` 会自动注入到环境变量中,无需 `--token`:
99
99
 
100
- | Scenario | Token Source | Need --token? |
101
- |----------|-------------|---------------|
102
- | Web chat (ChatDrawer → Claude Code) | Framework injects user's JWT as `LOOM_TOKEN` env var when spawning `claude -p` | No |
103
- | Process automation (cron/event) | Framework injects owner's JWT as `LOOM_TOKEN` env var | No (use `$LOOM_TOKEN` shell variable reference in prompts) |
104
- | Local terminal `loom` commands | No automatic injection | Yes |
100
+ | 场景 | 令牌来源 | 需要 --token |
101
+ |------|---------|---------------|
102
+ | Web 聊天(ChatDrawer → Claude Code | 框架在启动 `claude -p` 时将用户 JWT 注入为 `LOOM_TOKEN` 环境变量 | |
103
+ | Process 自动化(cron/事件) | 框架将所有者的 JWT 注入为 `LOOM_TOKEN` 环境变量 | 否(在 prompt 中使用 `$LOOM_TOKEN` shell 变量引用) |
104
+ | 本地终端 `loom` 命令 | 无自动注入 | |
105
105
 
106
- Priority: `--token` flag > `LOOM_TOKEN` env var. The `--token` flag acts as a fallback/override — safe to always include, but redundant in Web/Process contexts.
106
+ 优先级:`--token` 参数 > `LOOM_TOKEN` 环境变量。`--token` 参数作为后备/覆盖使用——始终包含是安全的,但在 Web/Process 场景中是冗余的。
107
107
 
108
- ## Permission Model
108
+ ## 权限模型
109
109
 
110
- ### Levels (hierarchical)
111
- | Level | Access |
112
- |-------|--------|
113
- | `none` | No access |
114
- | `read` | GET (read-only) |
115
- | `write` | GET, POST, PUT |
116
- | `admin` | GET, POST, PUT, DELETE |
110
+ ### 等级(层级关系)
111
+ | Level | 访问权限 |
112
+ |-------|---------|
113
+ | `none` | 无访问权限 |
114
+ | `read` | GET(只读) |
115
+ | `write` | GETPOSTPUT |
116
+ | `admin` | GETPOSTPUTDELETE |
117
117
 
118
- ### Configuration in loom.config.ts
118
+ ### loom.config.ts 中配置
119
119
  ```typescript
120
120
  auth: {
121
121
  provider: 'builtin',
122
- secret: 'env:JWT_SECRET', // or plain string (not recommended)
122
+ secret: 'env:JWT_SECRET', // 或明文字符串(不推荐)
123
123
  roles: [
124
124
  { role: 'admin', permissions: [{ model: '*', level: 'admin' }] },
125
125
  { role: 'editor', permissions: [{ model: 'items', level: 'write' }] },
@@ -129,52 +129,52 @@ auth: {
129
129
  defaults: {
130
130
  read: 'read',
131
131
  write: 'write',
132
- writeExcludesDelete: false, // writer can delete?
132
+ writeExcludesDelete: false, // write 角色是否可以删除?
133
133
  },
134
134
  },
135
135
  }
136
136
  ```
137
137
 
138
- ### Wildcard Permissions
139
- Use `model: '*'` to apply a permission level to all models.
140
- Explicit model permissions override wildcards.
138
+ ### 通配符权限
139
+ 使用 `model: '*'` 将权限等级应用到所有模型。
140
+ 显式模型权限覆盖通配符。
141
141
 
142
- ### Default Admin User
143
- When auth is first enabled, a default admin user is created:
144
- - Username: `admin`
145
- - Password: `admin123`
146
- - **Change this immediately after first login!**
142
+ ### 默认管理员用户
143
+ 首次启用认证时,会创建默认管理员用户:
144
+ - 用户名:`admin`
145
+ - 密码:`admin123`
146
+ - **首次登录后请立即修改密码!**
147
147
 
148
- ### Service Token
149
- Each user gets a service token at registration time (long-lived JWT, 30-day TTL).
150
- Returned in the API response when creating a user — the admin should save it securely.
151
- Used by Process automation to execute as the user.
148
+ ### 服务令牌
149
+ 每个用户在注册时会获得一个服务令牌(长效 JWT30 天有效期)。
150
+ 创建用户时在 API 响应中返回——管理员应安全保存。
151
+ Process 自动化使用它来以该用户身份执行操作。
152
152
 
153
- ## API Endpoints
153
+ ## API 端点
154
154
 
155
155
  | Method | Path | Auth | Description |
156
156
  |--------|------|------|-------------|
157
- | POST | /api/v1/auth/login | No | Login |
158
- | POST | /api/v1/auth/register | Admin | Register new user |
159
- | POST | /api/v1/auth/logout | Yes | Logout |
160
- | POST | /api/v1/auth/refresh | No* | Refresh token |
161
- | GET | /api/v1/auth/whoami | Yes | Current user |
162
- | GET | /api/v1/auth/roles | No | List roles |
163
- | POST | /api/v1/auth/roles | Admin | Create role |
164
- | PUT | /api/v1/auth/roles/:role | Admin | Update role |
165
- | DELETE | /api/v1/auth/roles/:role | Admin | Delete role |
166
- | GET | /api/v1/auth/users | Admin | List users |
167
- | POST | /api/v1/auth/users | Admin | Create user |
168
- | GET | /api/v1/auth/users/:id | Self/Admin | Get user |
169
- | PUT | /api/v1/auth/users/:id | Self/Admin | Update user |
170
- | DELETE | /api/v1/auth/users/:id | Admin | Delete user |
171
-
172
- *Refresh requires valid refresh token in store
173
-
174
- ## Audit Fields
175
-
176
- When auth is enabled, write operations automatically inject:
177
- - `createdBy` — username of the user who created the record (POST)
178
- - `updatedBy` — username of the user who last updated the record (PUT)
179
-
180
- These fields are optional and don't affect existing data.
157
+ | POST | /api/v1/auth/login | No | 登录 |
158
+ | POST | /api/v1/auth/register | Admin | 注册新用户 |
159
+ | POST | /api/v1/auth/logout | Yes | 登出 |
160
+ | POST | /api/v1/auth/refresh | No* | 刷新令牌 |
161
+ | GET | /api/v1/auth/whoami | Yes | 当前用户信息 |
162
+ | GET | /api/v1/auth/roles | No | 列出角色 |
163
+ | POST | /api/v1/auth/roles | Admin | 创建角色 |
164
+ | PUT | /api/v1/auth/roles/:role | Admin | 更新角色 |
165
+ | DELETE | /api/v1/auth/roles/:role | Admin | 删除角色 |
166
+ | GET | /api/v1/auth/users | Admin | 列出用户 |
167
+ | POST | /api/v1/auth/users | Admin | 创建用户 |
168
+ | GET | /api/v1/auth/users/:id | Self/Admin | 获取用户信息 |
169
+ | PUT | /api/v1/auth/users/:id | Self/Admin | 更新用户 |
170
+ | DELETE | /api/v1/auth/users/:id | Admin | 删除用户 |
171
+
172
+ *刷新令牌需要存储中有效的 refresh token
173
+
174
+ ## 审计字段
175
+
176
+ 启用认证后,写操作会自动注入:
177
+ - `createdBy` — 创建该记录的用户名(POST
178
+ - `updatedBy` — 最后更新该记录的用户名(PUT
179
+
180
+ 这些字段是可选的,不会影响现有数据。
@@ -1,5 +1,7 @@
1
1
  # 事件系统参考
2
2
 
3
+ > **配置 Schema 参考**:事件订阅的声明式配置 (`events.subscriptions`) 见 Loom Skill (`references/data-model.md` → EventsConfig)。
4
+
3
5
  Loom 事件系统在数据变更和系统操作时自动触发事件,可用于通知、审计和自动化。
4
6
 
5
7
  ## 事件模式格式
@@ -69,38 +71,167 @@ Loom 事件系统在数据变更和系统操作时自动触发事件,可用于
69
71
  |----------|----------|-------------|
70
72
  | `file:uploaded` | 文件上传成功 | filename, sessionId, path, size |
71
73
 
74
+ ## 订阅可用处理器
75
+
76
+ | handler | 说明 | config 字段 |
77
+ |---------|------|------------|
78
+ | `process` | 触发 Process 执行 | `process`(必填), `filter`, `debounceMs` |
79
+ | `webhook` | 发送 HTTP 请求 | `url`(必填), `method`, `headers` |
80
+ | `log` | 写入服务器日志 | `level`: info/debug/warn |
81
+ | `notify` | 创建站内通知 | `title`, `description` |
82
+
72
83
  ## 配置声明式订阅
73
84
 
74
- 在 `loom.config.ts` 中声明事件处理器:
85
+ 在 `loom.config.ts` 中声明事件订阅(服务启动时自动注册):
75
86
 
76
87
  ```typescript
77
88
  export default defineConfig({
78
89
  // ...
79
90
  events: {
80
91
  subscriptions: [
81
- {
82
- pattern: 'data:create:orders',
83
- handler: 'webhook',
84
- config: { url: 'https://example.com/webhook' },
85
- },
86
- {
87
- pattern: 'user:login',
88
- handler: 'log',
89
- config: { level: 'info' },
90
- },
92
+ { pattern: 'data:create:orders', handler: 'process', config: { process: 'order-processor' } },
93
+ { pattern: 'data:create:orders', handler: 'webhook', config: { url: 'https://example.com/webhook' } },
94
+ { pattern: 'user:login', handler: 'log', config: { level: 'info' } },
95
+ { pattern: 'alert:critical', handler: 'notify', config: { title: 'Critical Alert' } },
91
96
  ],
92
97
  },
93
98
  });
94
99
  ```
95
100
 
101
+ ## CLI 管理
102
+
103
+ ### `loom event list`
104
+
105
+ 列出所有事件订阅(显示稳定 ID)。
106
+
107
+ ```bash
108
+ loom event list
109
+ ```
110
+
111
+ 输出示例:
112
+ ```
113
+ Event Subscriptions:
114
+ sub_1747301234_ab data:create:tasks → process (on-task-create) ●
115
+ sub_1747301235_cd user:login → log (info)
116
+ sub_1747301236_ef alert:critical → webhook → https://example.com/hook
117
+ ```
118
+
119
+ `●` 表示订阅活跃(process 类型需目标进程已注册事件监听;webhook/log/notify 类型在 EventBus 运行时始终活跃),`○` 表示未激活。
120
+
121
+ ### `loom event subscribe <pattern>`
122
+
123
+ 添加事件订阅,返回稳定 ID。
124
+
125
+ ```bash
126
+ # 触发 Process 执行
127
+ loom event subscribe "data:create:tasks" \
128
+ --handler process \
129
+ --process "on-task-create"
130
+
131
+ # 带过滤和防抖
132
+ loom event subscribe "data:update:*" \
133
+ --handler process \
134
+ --process "on-data-update" \
135
+ --filter '{"model":"tasks"}' \
136
+ --debounce 2000
137
+
138
+ # Webhook
139
+ loom event subscribe "alert:critical" \
140
+ --handler webhook \
141
+ --url "https://example.com/hook"
142
+
143
+ # 日志
144
+ loom event subscribe "data:*" \
145
+ --handler log \
146
+ --level debug
147
+
148
+ # 通知
149
+ loom event subscribe "incident:created" \
150
+ --handler notify \
151
+ --title "New Incident"
152
+ ```
153
+
154
+ **Options:**
155
+ | Flag | Required | Description |
156
+ |------|----------|-------------|
157
+ | `--handler` | Yes | Handler type: `process` \| `webhook` \| `log` \| `notify` |
158
+ | `--process` | process | 目标 Process 名称(handler=process 时必填) |
159
+ | `--url` | webhook | Webhook URL(handler=webhook 时必填) |
160
+ | `--method` | No | HTTP 方法,默认 POST(handler=webhook) |
161
+ | `--headers` | No | Webhook 请求头 JSON |
162
+ | `--level` | No | 日志级别 info/debug/warn(handler=log) |
163
+ | `--title` | No | 通知标题模板(handler=notify) |
164
+ | `--description` | No | 通知描述模板(handler=notify) |
165
+ | `--filter` | No | 事件载荷过滤 JSON(handler=process) |
166
+ | `--debounce` | No | 防抖窗口 ms,默认 1000(handler=process) |
167
+ | `--token` | No | Auth token(或设置 LOOM_TOKEN) |
168
+ | `--server` | No | Server URL(或设置 LOOM_SERVER) |
169
+
170
+ ### `loom event update <id>`
171
+
172
+ 按 ID 部分更新事件订阅(只需指定要修改的字段)。
173
+
174
+ ```bash
175
+ # 修改防抖值
176
+ loom event update sub_1747301234_ab --debounce 2000
177
+
178
+ # 修改 pattern
179
+ loom event update sub_1747301234_ab --pattern "data:update:tasks"
180
+
181
+ # 修改 process 配置
182
+ loom event update sub_1747301234_ab --process "new-processor" --filter '{"status":"critical"}'
183
+
184
+ # 修改 webhook URL
185
+ loom event update sub_1747301235_cd --url "https://new-hook.example.com"
186
+ ```
187
+
188
+ **Options:**
189
+ | Flag | Required | Description |
190
+ |------|----------|-------------|
191
+ | `--pattern` | No | 新的事件模式 |
192
+ | `--handler` | No | 新的处理器类型 |
193
+ | `--process` | No | Process 名称(handler=process) |
194
+ | `--filter` | No | 事件载荷过滤 JSON(handler=process) |
195
+ | `--debounce` | No | 防抖窗口 ms(handler=process) |
196
+ | `--url` | No | Webhook URL(handler=webhook) |
197
+ | `--method` | No | HTTP 方法(handler=webhook) |
198
+ | `--headers` | No | Webhook 请求头 JSON(handler=webhook) |
199
+ | `--level` | No | 日志级别(handler=log) |
200
+ | `--title` | No | 通知标题(handler=notify) |
201
+ | `--description` | No | 通知描述(handler=notify) |
202
+ | `--token` | No | Auth token |
203
+ | `--server` | No | Server URL |
204
+
205
+ ### `loom event unsubscribe <id>`
206
+
207
+ 按 ID 删除事件订阅。
208
+
209
+ ```bash
210
+ loom event unsubscribe sub_1747301234_ab
211
+ ```
212
+
213
+ ### `loom event test <pattern>`
214
+
215
+ 发送测试事件(调试用)。
216
+
217
+ ```bash
218
+ loom event test "data:create:tasks"
219
+ loom event test "data:update:items" --payload '{"id":"test_123"}'
220
+ ```
221
+
96
222
  ## 事件 API
97
223
 
98
224
  | 端点 | 方法 | 说明 |
99
225
  |------|------|------|
100
- | `/api/v1/events/notify` | POST | 外部事件注入(CLI 使用) |
226
+ | `/api/v1/events/emit` | POST | 外部事件注入(CLI 使用) |
101
227
  | `/api/v1/events/patterns` | GET | 查看已注册的事件模式 |
228
+ | `/api/v1/events/subscriptions` | GET | 列出所有事件订阅 |
229
+ | `/api/v1/events/subscriptions` | POST | 添加事件订阅 |
230
+ | `/api/v1/events/subscriptions/:id` | PUT | 部分更新事件订阅 |
231
+ | `/api/v1/events/subscriptions/:id` | DELETE | 删除事件订阅 |
232
+ | `/api/v1/events/test` | POST | 发送测试事件 |
102
233
 
103
- ### POST /api/v1/events/notify
234
+ ### POST /api/v1/events/emit
104
235
 
105
236
  ```json
106
237
  {
@@ -110,10 +241,97 @@ export default defineConfig({
110
241
  }
111
242
  ```
112
243
 
113
- ## 订阅可用处理器
244
+ ### POST /api/v1/events/subscriptions
114
245
 
115
- | handler | 说明 | config 字段 |
116
- |---------|------|------------|
117
- | `log` | 写入服务器日志 | `level`: info/debug/warn |
118
- | `webhook` | 发送 HTTP 请求 | `url`, `method`, `headers` |
119
- | `process` | 启动进程(未来) | `process`, `filter` |
246
+ 返回值包含自动生成的稳定 `id`:
247
+
248
+ ```json
249
+ {
250
+ "pattern": "data:create:orders",
251
+ "handler": "process",
252
+ "config": { "process": "order-processor", "debounceMs": 1000 }
253
+ }
254
+ ```
255
+
256
+ 响应:
257
+ ```json
258
+ { "ok": true, "id": "sub_1747301234_ab", "subscription": { "id": "sub_1747301234_ab", "pattern": "...", "handler": "...", "config": {...} } }
259
+ ```
260
+
261
+ ### PUT /api/v1/events/subscriptions/:id
262
+
263
+ 部分更新(只发送要修改的字段):
264
+
265
+ ```json
266
+ { "config": { "debounceMs": 2000 } }
267
+ ```
268
+
269
+ ## Process 事件绑定
270
+
271
+ Process 通过事件订阅响应事件。三种绑定方式:
272
+
273
+ 1. **创建 Process 时绑定**(原子操作):`loom process add --subscribe "pattern"` — 同时创建 process 和 subscription,保证 process 存在时订阅立即生效
274
+ 2. **给已有 Process 添加订阅**:`loom process update <name> --subscribe-add "pattern"` 或 `loom event subscribe "pattern" --handler process --process <name>`
275
+ 3. **移除订阅**:`loom process update <name> --subscribe-remove <id>` 或 `loom event unsubscribe <id>`
276
+
277
+ 注意:避免对同一 pattern + process 重复声明(声明式配置 + CLI/API),否则会双重触发。
278
+
279
+ ### `loom process add --subscribe`
280
+
281
+ 创建 Process 时同时绑定事件订阅(原子操作):
282
+
283
+ ```bash
284
+ loom process add --name "on-task-create" \
285
+ --prompt "自动处理新任务" \
286
+ --subscribe "data:create:tasks" \
287
+ --event-filter '{"priority":"high"}' \
288
+ --event-debounce 2000 \
289
+ --owner admin
290
+ ```
291
+
292
+ **Options:**
293
+ | Flag | Description |
294
+ |------|-------------|
295
+ | `--subscribe <pattern>` | 事件模式(如 `data:create:tasks`)|
296
+ | `--event-filter <json>` | 事件载荷过滤(可选)|
297
+ | `--event-debounce <ms>` | 防抖窗口,默认 1000ms(可选)|
298
+ | `--source <command>` | Feeder 命令,必须同时指定 `--subscribe`(可选)|
299
+
300
+ ### `loom process update --subscribe-add / --subscribe-remove`
301
+
302
+ 给已有 Process 动态添加或移除事件订阅:
303
+
304
+ ```bash
305
+ # 添加订阅
306
+ loom process update my-process \
307
+ --subscribe-add "data:update:orders" \
308
+ --subscribe-filter '{"status":"urgent"}' \
309
+ --subscribe-debounce 1500
310
+
311
+ # 移除订阅
312
+ loom process update my-process --subscribe-remove sub_1747301234_ab
313
+ ```
314
+
315
+ ## 声明式配置
316
+
317
+ 事件订阅也可以在 `loom.config.ts` 的 `events.subscriptions` 中声明式定义(完整 Schema 见 Loom Skill `references/data-model.md` → EventsConfig)。服务启动时自动注册,修改后需重启生效。适合固定不变的订阅(如 webhook 推送、日志记录)。
318
+
319
+ CLI/API 与声明式配置不要对同一 pattern + handler 重复声明,否则会双重触发。
320
+
321
+ ## 事件管理页面
322
+
323
+ 系统管理 → 事件管理 页面提供可视化操作:
324
+
325
+ - **查看所有订阅**:表格展示 pattern、handler、config 详情、活跃状态
326
+ - **添加/编辑/删除订阅**:按 handler 类型动态渲染配置表单
327
+ - **测试事件**:输入 pattern + payload 发送测试事件调试
328
+
329
+ 通过 `loom generate system-settings event` 接入 App.tsx,或 `loom eject system-settings event` 定制。
330
+
331
+ ## 校验行为
332
+
333
+ | 操作 | 校验规则 |
334
+ |------|---------|
335
+ | `handler: 'process'` | 目标 process 必须存在,否则返回 `Process "xxx" not found` |
336
+ | `handler: 'webhook'` | `config.url` 必填,否则返回 `webhook handler requires config.url` |
337
+ | `handler: 'process'` + 无 `--process` | 返回 `--process is required when handler=process` |