@lark-apaas/coding-steering 0.1.52 → 0.1.53-alpha.20260916135943
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.
- package/package.json +6 -6
- package/steering/nestjs-react-fullstack/skills/app-init-feasibility-guide/SKILL.md +1 -2
- package/steering/nestjs-react-fullstack/skills/client-builtins-file-storage-service/SKILL.md +0 -1
- package/steering/nestjs-react-fullstack/skills/client-builtins-user-service/SKILL.md +0 -2
- package/steering/nestjs-react-fullstack/skills/coding-guide/SKILL.md +14 -61
- package/steering/nestjs-react-fullstack/skills/plugin-guide/SKILL.md +1 -4
- package/steering/nestjs-react-fullstack/skills/plugin-guide/references/plugin-coding-guide.md +5 -37
- package/steering/nestjs-react-fullstack/skills_common/mcp-guide/SKILL.md +376 -0
- package/steering/nestjs-react-fullstack/skills_local/client-builtins-user-service/SKILL.md +0 -2
- package/steering/nestjs-react-fullstack/skills_local/coding-guide/SKILL.md +13 -44
- package/steering/nestjs-react-fullstack/skills_local/plugin-guide/SKILL.md +1 -1
- package/steering/nestjs-react-fullstack/skills_local/plugin-guide/references/plugin-coding-guide.md +5 -37
- package/steering/vite-react/skills/plugin-guide/SKILL.md +5 -2
- package/steering/nestjs-react-fullstack/skills/design-guide/SKILL.md +0 -188
- package/steering/nestjs-react-fullstack/skills/design-guide/references/broadsheet.md +0 -311
- package/steering/nestjs-react-fullstack/skills/design-guide/references/claude-editorial-research.md +0 -375
- package/steering/nestjs-react-fullstack/skills/design-guide/references/corporate-blueprint.md +0 -252
- package/steering/nestjs-react-fullstack/skills/design-guide/references/crimson-frosted-glass.md +0 -221
- package/steering/nestjs-react-fullstack/skills/design-guide/references/cybernetic-vault-terminal.md +0 -74
- package/steering/nestjs-react-fullstack/skills/design-guide/references/dashboard.md +0 -233
- package/steering/nestjs-react-fullstack/skills/design-guide/references/digital-e-guide.md +0 -349
- package/steering/nestjs-react-fullstack/skills/design-guide/references/flowbite.md +0 -179
- package/steering/nestjs-react-fullstack/skills/design-guide/references/frontend-design.md +0 -66
- package/steering/nestjs-react-fullstack/skills/design-guide/references/handwritten-sketch.md +0 -211
- package/steering/nestjs-react-fullstack/skills/design-guide/references/industry.md +0 -346
- package/steering/nestjs-react-fullstack/skills/design-guide/references/metabase.md +0 -220
- package/steering/nestjs-react-fullstack/skills/design-guide/references/minimal-jade.md +0 -205
- package/steering/nestjs-react-fullstack/skills/design-guide/references/nebula-crimson.md +0 -306
- package/steering/nestjs-react-fullstack/skills/design-guide/references/nexuscore-analytics.md +0 -74
- package/steering/nestjs-react-fullstack/skills/design-guide/references/organic-minimalism.md +0 -251
- package/steering/nestjs-react-fullstack/skills/design-guide/references/phosphor-hud.md +0 -214
- package/steering/nestjs-react-fullstack/skills/design-guide/references/pm-spec.md +0 -233
- package/steering/nestjs-react-fullstack/skills/design-guide/references/pop-art.md +0 -264
- package/steering/nestjs-react-fullstack/skills/design-guide/references/token-mapping.md +0 -68
- package/steering/nestjs-react-fullstack/skills/design-guide/references/warm-elegance.md +0 -215
- package/steering/nestjs-react-fullstack/skills/design-guide/references/wild-orange.md +0 -203
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mcp-guide
|
|
3
|
+
description: "Use when 用户要把应用能力开放给外部 Agent / 工作伙伴 / MCP 客户端调用,或提到 MCP Server、MCP 工具、MCP Apps、tools/call、@McpTools、@McpTool、manifest.json。覆盖工具类目录与命名、装饰器用法、zod schema、身份读取、错误处理、MCP Apps 界面、清单校验与调试方式。触发词:MCP, MCP Server, MCP 工具, MCP Apps, 应用 Skill, 多 Skill, server/mcp/skills, server/mcp/SKILL.md, resources/read, prompts/list, prompts/get, mcp tool, tools/list, tools/call, @McpTools, @McpTool, @McpUiResource, miaoda-mcp, manifest.json, 开放给 Agent, 工作伙伴调用, Agent 调用应用"
|
|
4
|
+
steering: true
|
|
5
|
+
steering-topic: mcp_guide
|
|
6
|
+
match-template-name: nestjs-react-fullstack
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# MCP Server 开发指引
|
|
10
|
+
|
|
11
|
+
应用通过 MCP(Model Context Protocol)把业务能力开放给外部 Agent。平台 SDK(`@lark-apaas/fullstack-nestjs-core`)已内置 MCP Server:**只需要用装饰器把业务方法声明为工具**,协议、装配、入参校验、身份读取与清单导出全部由 SDK 完成。
|
|
12
|
+
|
|
13
|
+
## 一、边界(先读)
|
|
14
|
+
|
|
15
|
+
| 由谁负责 | 内容 |
|
|
16
|
+
|---|---|
|
|
17
|
+
| **你(业务代码)** | 在 `server/mcp/tools/*.tools.ts` 里用 `@McpTools()` / `@McpTool()` 声明工具,调用现有 Service |
|
|
18
|
+
| **SDK(已内置)** | `POST /__innerapi__/mcp` 端点、Streamable HTTP 协议、zod 校验、`ctx.user` 身份、在线 manifest 与资源读取、离线清单导出 |
|
|
19
|
+
| **平台网关** | 凭证校验、OAuth、把当前用户身份注入请求头 |
|
|
20
|
+
|
|
21
|
+
**严禁**:自行实现 MCP 协议或 JSON-RPC、自行挂载 `/mcp` 路由、在应用内做凭证校验、把 `@modelcontextprotocol/sdk` 加进 `package.json`(SDK 已带)。
|
|
22
|
+
|
|
23
|
+
## 二、目录与命名
|
|
24
|
+
|
|
25
|
+
```text
|
|
26
|
+
server
|
|
27
|
+
└── mcp
|
|
28
|
+
├── SKILL.md # 可选:兼容旧应用的 legacy 单文件
|
|
29
|
+
├── skills # 按使用场景组织,不要求与 Tool 一一对应
|
|
30
|
+
│ ├── order-query/SKILL.md
|
|
31
|
+
│ └── order-cancel/SKILL.md
|
|
32
|
+
└── tools
|
|
33
|
+
├── order.tools.ts # 一个业务域一个文件:{域名}.tools.ts
|
|
34
|
+
└── inventory.tools.ts
|
|
35
|
+
client
|
|
36
|
+
└── mcp-ui # 仅 MCP Apps 需要(见第六节)
|
|
37
|
+
└── order-detail
|
|
38
|
+
├── index.html
|
|
39
|
+
└── main.tsx
|
|
40
|
+
.spark/mcp/manifest.json # SDK 自动生成,不要手改
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
- 工具类命名 `XxxMcpTools`,注册为**所属业务模块**的 provider;SDK 启动时自动发现,**不需要改 `app.module.ts`**。
|
|
44
|
+
- 工具名 = 类前缀 + 方法名(或 `name` 覆盖),只允许 `A-Z a-z 0-9 _ . -`,全应用唯一。用 `{域}_{动作}` 风格:`order_get`、`order_list`、`order_cancel`。
|
|
45
|
+
|
|
46
|
+
## 三、编写工具
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// server/mcp/tools/order.tools.ts
|
|
50
|
+
import { z } from 'zod';
|
|
51
|
+
import {
|
|
52
|
+
McpTools,
|
|
53
|
+
McpTool,
|
|
54
|
+
McpToolError,
|
|
55
|
+
type Infer,
|
|
56
|
+
type McpContext,
|
|
57
|
+
type McpToolResult,
|
|
58
|
+
} from '@lark-apaas/fullstack-nestjs-core';
|
|
59
|
+
import { OrderService } from '@server/modules/order/order.service';
|
|
60
|
+
|
|
61
|
+
// schema 用 const 声明并导出,供类型推导与清单导出
|
|
62
|
+
export const GetOrderInput = { orderId: z.string().describe('订单 ID') };
|
|
63
|
+
export const GetOrderOutput = {
|
|
64
|
+
orderId: z.string(),
|
|
65
|
+
amount: z.number().describe('金额,单位元'),
|
|
66
|
+
status: z.enum(['pending', 'paid', 'cancelled']),
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
@McpTools({ prefix: 'order_' })
|
|
70
|
+
export class OrderMcpTools {
|
|
71
|
+
constructor(private readonly orders: OrderService) {}
|
|
72
|
+
|
|
73
|
+
@McpTool({
|
|
74
|
+
title: '查询订单',
|
|
75
|
+
description: '按订单 ID 查询金额与状态。用户询问某个订单的情况时调用;找不到订单会返回 ORDER_NOT_FOUND。',
|
|
76
|
+
inputSchema: GetOrderInput,
|
|
77
|
+
outputSchema: GetOrderOutput,
|
|
78
|
+
annotations: { readOnlyHint: true },
|
|
79
|
+
})
|
|
80
|
+
async get(input: Infer<typeof GetOrderInput>, ctx: McpContext): Promise<McpToolResult<typeof GetOrderOutput>> {
|
|
81
|
+
const order = await this.orders.findVisibleTo(input.orderId, ctx.user.userId);
|
|
82
|
+
if (!order) throw new McpToolError('订单不存在', { code: 'ORDER_NOT_FOUND' });
|
|
83
|
+
return { structuredContent: { orderId: order.id, amount: order.amount, status: order.status } };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@McpTool({
|
|
87
|
+
description: '取消订单。仅在用户明确要求取消且订单处于 pending 时调用。',
|
|
88
|
+
inputSchema: { orderId: z.string() },
|
|
89
|
+
annotations: { destructiveHint: true, idempotentHint: true },
|
|
90
|
+
})
|
|
91
|
+
async cancel(input: { orderId: string }, ctx: McpContext): Promise<McpToolResult> {
|
|
92
|
+
await this.orders.cancel(input.orderId, ctx.user.userId);
|
|
93
|
+
return { content: [{ type: 'text', text: `订单 ${input.orderId} 已取消` }] };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
// server/modules/order/order.module.ts —— 工具类和 Service 一起注册
|
|
100
|
+
@Module({ providers: [OrderService, OrderMcpTools], controllers: [OrderController] })
|
|
101
|
+
export class OrderModule {}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### 规则
|
|
105
|
+
|
|
106
|
+
1. **`description` 必填**且面向模型:说清用途、何时调用、返回什么、失败会怎样。`title` 面向人。
|
|
107
|
+
2. **方法签名固定** `(input, ctx)`:`input` 用 `Infer<typeof InputSchema>` 标注;`ctx.user` 与 REST 里的 `req.userContext` 完全一致(`userId` / `tenantId` / `roles` …)。TypeScript 装饰器不能改写方法类型,所以入参与返回值类型必须自己写。
|
|
108
|
+
3. **返回值**:声明了 `outputSchema` → 返回 `{ structuredContent }`(SDK 自动补一份 JSON 文本);没声明 → 返回 `{ content: [{ type: 'text', text }] }`。
|
|
109
|
+
4. **业务错误抛 `McpToolError(message, { code?, data? })`**,Agent 会原样收到并据此调整;其他异常会被记日志并替换为通用失败提示,不要用它传业务信息。
|
|
110
|
+
5. **注解**如实填写:只读 `readOnlyHint`,删除/不可逆 `destructiveHint`,重复调用无副作用 `idempotentHint`。
|
|
111
|
+
6. **权限与数据隔离复用业务层**:工具只负责把 `ctx.user` 传给 Service,不在工具里拼 SQL 或绕过行级权限。
|
|
112
|
+
7. 工具方法保持薄:参数校验交给 zod,业务逻辑放 Service;一个工具只做一件事。
|
|
113
|
+
8. 工具类及依赖必须为单例,逐请求身份通过 ctx.user 传递;Nest HTTP Controller 上的 Guard/Pipe/Interceptor 不会自动应用于工具方法,业务权限检查放共享 Service。
|
|
114
|
+
9. 未携带用户身份的调用默认被 SDK 拒绝(`MCP_USER_REQUIRED`),不需要自己判空。
|
|
115
|
+
|
|
116
|
+
## 四、zod 用法约束
|
|
117
|
+
|
|
118
|
+
- 入参 / 出参都用**对象**:原始形状 `{ a: z.string() }` 或 `z.object({...})`,不要用数组 / 基础类型作为顶层。
|
|
119
|
+
- 每个字段尽量 `.describe()`,这是 Agent 理解参数的唯一依据。
|
|
120
|
+
- 可选字段用 `.optional()`;枚举用 `z.enum([...])`;不要用 `z.any()` / `z.unknown()` 作为入参。
|
|
121
|
+
- 项目里已安装 zod,`import { z } from 'zod'` 即可,不要额外安装。
|
|
122
|
+
|
|
123
|
+
## 五、校验与调试
|
|
124
|
+
|
|
125
|
+
**每次新增或修改工具后必须运行:**
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
npx miaoda-mcp validate
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
它会加载 `server/mcp` 下的源码,检查工具名格式 / 重名 / 引用错误,并写入 `.spark/mcp/manifest.json`(version 3 离线目录,含工具 Schema、Skill 元信息及路径引用,不复制 Skill 正文)。校验失败要修到通过;`.spark/mcp/manifest.json` 由命令生成,不要手改,随代码一起提交。
|
|
132
|
+
|
|
133
|
+
**类型检查**:`npm run type:check:server`(工具方法签名错误会在这里暴露)。
|
|
134
|
+
|
|
135
|
+
**调试端点**:应用启动后(`npm run dev`),MCP 端点在开发服务入口的 `/__innerapi__/mcp`。
|
|
136
|
+
标准 MCP 客户端配置为 Streamable HTTP、URL 填 `<应用开发地址>/__innerapi__/mcp` 即可;本地开发与云端沙箱使用同一协议路径。本地代理从 SUDA_WEBUSER 注入身份;沙箱入口需由平台提供可信身份,不要伪造用户头。
|
|
137
|
+
也可以用 curl 快速验证(`Accept` 必须同时含两种类型):
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
curl -s -X POST '<应用开发地址>/__innerapi__/mcp' \
|
|
141
|
+
-H 'content-type: application/json' \
|
|
142
|
+
-H 'accept: application/json, text/event-stream' \
|
|
143
|
+
-d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
- 返回 `404`:检查 MCP 模块是否启用、访问路径及应用前缀;工具缺失时检查所属模块的 `providers`。
|
|
147
|
+
- 返回 `405`:用了 GET/DELETE,端点只接受 POST。
|
|
148
|
+
- 工具结果 `isError` 且 code 为 `MCP_USER_REQUIRED`:请求没经过开发服务入口 / 网关,缺少用户身份。
|
|
149
|
+
|
|
150
|
+
## 六、MCP Apps(可选:工具结果带界面)
|
|
151
|
+
|
|
152
|
+
只有当结果**需要可视化交互**(详情卡片、图表、审批表单)时才做。做法:
|
|
153
|
+
|
|
154
|
+
1. 界面源码放 `server/mcp/ui/<entry>/index.html`(+ `main.tsx`,可用 React),构建预设会自动打成单文件 `dist/mcp-ui/<entry>.html`,开发态监听变更自动重建。界面内如需与宿主通信,用 `@modelcontextprotocol/ext-apps` 的 `App` / React hooks(SDK 已带)。
|
|
155
|
+
2. 在工具类里声明资源并关联工具:
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
import { McpUiResource, readMcpUiTemplate } from '@lark-apaas/fullstack-nestjs-core';
|
|
159
|
+
|
|
160
|
+
@McpTools({ prefix: 'order_' })
|
|
161
|
+
export class OrderMcpTools {
|
|
162
|
+
@McpTool({ description: '…', inputSchema: GetOrderInput, outputSchema: GetOrderOutput, ui: { resourceUri: 'ui://order/detail' } })
|
|
163
|
+
async get(/* … */) { /* … */ }
|
|
164
|
+
|
|
165
|
+
@McpUiResource({ uri: 'ui://order/detail', title: '订单详情' })
|
|
166
|
+
detailView() {
|
|
167
|
+
return readMcpUiTemplate('order-detail'); // 对应 server/mcp/ui/order-detail
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
- `uri` 必须以 `ui://` 开头,`ui.resourceUri` 必须能找到对应的 `@McpUiResource()`(`miaoda-mcp validate` 会检查)。
|
|
173
|
+
- 界面代码统一放 `server/mcp/ui/`。它是独立浏览器程序:开通时在 `tsconfig.node.json` 原有 `exclude` 中追加 `server/mcp/ui`,不要覆盖其他排除项;生成 UI 独立 tsconfig(见下),分别执行服务端和 UI 类型检查。
|
|
174
|
+
- UI 只可引用 UI 目录、shared 的独立展示代码/类型及浏览器依赖。禁止引入 client 业务组件、应用 Router/Provider/登录态、MCP 工具或服务端实现;通过 shared 间接引入也不允许。构建预设会检查实际依赖。
|
|
175
|
+
- 子构建不读取主应用 Vite/PostCSS 配置、环境文件或 public 目录;资源放 UI/shared 并通过 import 引入。通过 MCP Apps 宿主通信获取结果、调用工具,不直接假设应用同源 API 可用。
|
|
176
|
+
|
|
177
|
+
## 七、常见错误
|
|
178
|
+
|
|
179
|
+
| 现象 | 原因 | 处理 |
|
|
180
|
+
|---|---|---|
|
|
181
|
+
| 启动报「工具名重复」 | 两个方法算出同名 | 调整 `prefix` 或 `name` |
|
|
182
|
+
| 启动报「未找到对应的 @McpUiResource」 | `ui.resourceUri` 拼错 | 与 `@McpUiResource({ uri })` 保持一致 |
|
|
183
|
+
| `validate` 报 Cannot find module | 工具文件 import 了未安装的包或错误路径 | 修正 import;路径别名用 `@server/*` |
|
|
184
|
+
| 工具能列出但调用总是失败 | 抛了非 `McpToolError` 的异常 | 看服务端日志;业务失败改抛 `McpToolError` |
|
|
185
|
+
| Agent 不会调用工具 | `description` 太简略或缺 `.describe()` | 补充何时调用、参数含义与返回内容 |
|
|
186
|
+
|
|
187
|
+
## 八、平台目录与验收
|
|
188
|
+
|
|
189
|
+
平台通过 `GET <应用开发地址>/__innerapi__/mcp/manifest` 获取当前运行能力。工具和资源定义来自运行注册表,Skill 正文读取 `server/mcp/skills/<name>/SKILL.md`,同时兼容 `server/mcp/SKILL.md`;不扫描或回退到 `.spark/mcp/manifest.json`。用户无需实现该接口。示例(省略具体工具和 UI 资源):
|
|
190
|
+
|
|
191
|
+
```json
|
|
192
|
+
{
|
|
193
|
+
"version": 3,
|
|
194
|
+
"generatedAt": "2026-09-14T08:00:00.000Z",
|
|
195
|
+
"endpoint": "/__innerapi__/mcp",
|
|
196
|
+
"tools": [],
|
|
197
|
+
"resources": [],
|
|
198
|
+
"skills": [
|
|
199
|
+
{
|
|
200
|
+
"name": "order-query",
|
|
201
|
+
"description": "查询当前用户可见的订单详情",
|
|
202
|
+
"path": "server/mcp/skills/order-query/SKILL.md",
|
|
203
|
+
"content": "---\nname: order-query\ndescription: 查询当前用户可见的订单详情\n---\n# 查询订单\n取得 orderId 后调用 order_get;ORDER_NOT_FOUND 时请用户核对编号。\n"
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
"name": "order-cancel",
|
|
207
|
+
"description": "用户确认后取消待处理订单",
|
|
208
|
+
"path": "server/mcp/skills/order-cancel/SKILL.md",
|
|
209
|
+
"content": "---\nname: order-cancel\ndescription: 用户确认后取消待处理订单\n---\n# 取消订单\n先调用 order_get 检查状态;仅在用户明确确认且状态为 pending 时调用 order_cancel。\n"
|
|
210
|
+
}
|
|
211
|
+
],
|
|
212
|
+
"sources": {
|
|
213
|
+
"tools": {},
|
|
214
|
+
"resources": {},
|
|
215
|
+
"prompts": {
|
|
216
|
+
"order-query": {
|
|
217
|
+
"path": "server/mcp/skills/order-query/SKILL.md"
|
|
218
|
+
},
|
|
219
|
+
"order-cancel": {
|
|
220
|
+
"path": "server/mcp/skills/order-cancel/SKILL.md"
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
"prompts": [
|
|
225
|
+
{
|
|
226
|
+
"name": "order-query",
|
|
227
|
+
"description": "查询当前用户可见的订单详情"
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
"name": "order-cancel",
|
|
231
|
+
"description": "用户确认后取消待处理订单"
|
|
232
|
+
}
|
|
233
|
+
]
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
`tools`、`resources`、`prompts` 完整复用标准 MCP 列表定义;UI 关联仍为工具 `_meta.ui.resourceUri`。平台目录 v3 的 `skills` 保存 `{ name, description, path, content }`,content 是含 frontmatter 的完整源文件,供面板展示;标准 Prompt 获取正文时去掉前言。`sources.tools` 按工具名、`sources.resources` 按 UI URI、`sources.prompts` 按 Prompt 名提供源码定位,Skill 也可直接使用 `skills[].path`。未配置时数组为空,读取失败不能伪装为空。`generatedAt` 是响应时间,不代表源码版本;平台负责工程版本匹配。
|
|
238
|
+
|
|
239
|
+
删除所有 MCP 工具、UI 资源声明及应用 Skill 文件后再次运行 `miaoda-mcp validate` 清理旧清单。标准 MCP 协议端点仍为 POST,不要把 GET 目录接口当成工具调用接口。
|
|
240
|
+
|
|
241
|
+
Vite 子构建生成 `dist/mcp-ui/<entry>.html`,并同步到上述两个交付根各自的 `dist/mcp-ui`。开发态监听 UI/shared 变更并重建。MCP Apps 使用 NRF 默认 Vite;Rspack 负责身份代理和 Skill 交付,不提供 UI 子构建。 界面可以使用普通 HTML/TypeScript,不强制 React。最小宿主通信示例:
|
|
242
|
+
|
|
243
|
+
```html
|
|
244
|
+
<!-- server/mcp/ui/order-detail/index.html -->
|
|
245
|
+
<!doctype html>
|
|
246
|
+
<html><head><meta charset="utf-8"></head><body>
|
|
247
|
+
<pre id="result">等待工具结果</pre>
|
|
248
|
+
<script type="module" src="./main.ts"></script>
|
|
249
|
+
</body></html>
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
// server/mcp/ui/order-detail/main.ts
|
|
254
|
+
import { App } from '@modelcontextprotocol/ext-apps';
|
|
255
|
+
const app = new App({ name: 'order-detail', version: '1.0.0' }, {});
|
|
256
|
+
app.ontoolresult = (result) => {
|
|
257
|
+
document.getElementById('result')!.textContent = JSON.stringify(result.structuredContent ?? result.content, null, 2);
|
|
258
|
+
};
|
|
259
|
+
await app.connect();
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
界面通过宿主 SDK 接收结果,不要自行处理宿主 postMessage 协议;展示文本用 textContent,避免将业务返回值直接写成 HTML。
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
## 九、应用 Skill(开放 MCP 能力时一起维护)
|
|
266
|
+
|
|
267
|
+
创建或修改应用 MCP 能力时,按使用场景生成并维护 `server/mcp/skills/<name>/SKILL.md`。不强制 Tool 与 Skill 一一对应:一个 Skill 可以编排多个工具,多个 Skill 可以复用同一工具。当前 `mcp-guide/SKILL.md` 是指导编码 AI 的 steering,不要复制为应用 Skill,也不要给应用 Skill 加 `steering: true`。
|
|
268
|
+
|
|
269
|
+
每份新 Skill 的 YAML frontmatter **必须有非空 name / description,name 必须等于所在目录名**。使用清楚的场景名,例如 `order-query`、`order-cancel`。正文写适用场景、实际工具名与参数、调用顺序、用户确认边界、身份权限、失败处理和 UI URI(如有);只记录已实现能力,不写凭证或用户数据。以下两份示例以应用已实现 order_get / order_cancel 为前提。
|
|
270
|
+
|
|
271
|
+
`server/mcp/skills/order-query/SKILL.md`:
|
|
272
|
+
|
|
273
|
+
```markdown
|
|
274
|
+
---
|
|
275
|
+
name: order-query
|
|
276
|
+
description: 查询当前用户可见的订单详情
|
|
277
|
+
---
|
|
278
|
+
# 查询订单
|
|
279
|
+
需要可信用户身份;取得 orderId 后调用 order_get,只访问当前用户可见的订单。
|
|
280
|
+
ORDER_NOT_FOUND 时请用户核对编号,不查询其他用户数据;宿主可展示 ui://order/detail。
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
`server/mcp/skills/order-cancel/SKILL.md`:
|
|
284
|
+
|
|
285
|
+
```markdown
|
|
286
|
+
---
|
|
287
|
+
name: order-cancel
|
|
288
|
+
description: 用户确认后取消待处理订单
|
|
289
|
+
---
|
|
290
|
+
# 取消订单
|
|
291
|
+
先用 order_get 检查 orderId 对应订单;仅在用户明确确认且状态为 pending 时调用 order_cancel。
|
|
292
|
+
沿用当前用户权限;失败时说明原因并重新查询状态,不盲目重试写操作。
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
新目录名与 frontmatter name 必须一致,名称为 1–64 位小写字母、数字及单连字符,不允许首尾或连续连字符;description 非空,最多 1024 个 UTF-16 代码单元。名称不能重复。已有 `server/mcp/SKILL.md` 仍可作为一份 Prompt 读取;无有效前言时使用 `app-skill` / `应用使用说明`。对外只提供 `skills` 数组,不返回旧单 `skill` 字段。
|
|
296
|
+
|
|
297
|
+
每份 Skill 自动注册为标准 MCP Prompt:`prompts/list` 返回 `name`、`description`;`prompts/get` 按 `name` 返回 `description` 和 `messages: [{ role: "user", content: { type: "text", text: "Markdown 正文" } }]`。正文去除 YAML frontmatter,当前不声明参数。Skill 不再注册为 Resource,也不使用 `skill://` URI;不新增 `skills/list` 或实验性扩展。仅有 Skills 的应用也可连接。
|
|
298
|
+
|
|
299
|
+
```json
|
|
300
|
+
{
|
|
301
|
+
"jsonrpc": "2.0",
|
|
302
|
+
"id": 2,
|
|
303
|
+
"method": "prompts/get",
|
|
304
|
+
"params": {
|
|
305
|
+
"name": "order-query"
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
默认 `requireUser` 下,`tools/call`、`prompts/get`、`resources/read` 需要可信用户身份;三类列表查询不要求身份。沿用平台网关注入规则,不伪造请求头。标准协议使用 Tools、Prompts、Resources,不增加自定义 MCP 方法。
|
|
311
|
+
|
|
312
|
+
仅交付和读取 SKILL.md;附属脚本、references、图片等不自动开放、复制或执行,读取 Markdown 也不会执行其中命令。最多 100 份,每份最多 1 MiB,正文总量最多 8 MiB;限制包含 legacy,大小按完整 UTF-8 文件字节数(含 frontmatter)计算。缺失与删除按未配置处理,新目录 Skill 的非法 frontmatter、目录名不符、非法 UTF-8、超限或越界链接必须修复,不能用空列表掩盖。源文件及产物路径不使用符号链接。
|
|
313
|
+
|
|
314
|
+
生产运行时固定从 `cwd/server/mcp` 读取 Skills、从 `cwd/dist/mcp-ui` 读取 UI。现存 NRF 有 `dist`(启动 `node server/main.js`)和 `dist/server`(启动 `node main.js`)两种交付根;预设分别把 Skills 同步到 `dist/server/mcp`、`dist/server/server/mcp`,把 UI 同步到 `dist/dist/mcp-ui`、`dist/server/dist/mcp-ui`。只复制 SKILL.md,清理删除项时保留相邻工具编译产物;不复制附件。无需 UI 也交付 Skills。自定义构建须满足相同的运行目录约定;开发修改需重新构建发布才影响生产。
|
|
315
|
+
|
|
316
|
+
让 AI 修改工程文件,然后运行 `npx miaoda-mcp validate`、构建并按应用流程发布;不实现 GUI 保存或运行时写源码。CLI 的 `--dir` 只选择工具源码目录,Skill 固定从工程根的上述路径读取;纯 Skill 应用也生成离线目录。离线 `prompts` 保留标准定义,`skills` 保留 name/description/path,不复制 content 或返回单 skill;`--check` 只校验不写入,不生成或覆盖业务 SKILL.md。删除全部能力与 Skill 后再 validate 清理旧清单。
|
|
317
|
+
|
|
318
|
+
验收覆盖两份 Skill、legacy 共存与独立删除、身份读取、无 UI 打包、生产 cwd 读取、正文修改与旧产物清理、frontmatter/容量错误。来源跳转用每项 path;工具未重新编译时的位置匹配由平台负责,生产没有 TypeScript 可以省略工具/UI 定位。
|
|
319
|
+
|
|
320
|
+
### MCP UI 独立类型检查
|
|
321
|
+
|
|
322
|
+
在 `server/mcp/ui/tsconfig.json` 创建独立配置(不继承服务端配置):
|
|
323
|
+
|
|
324
|
+
```json
|
|
325
|
+
{
|
|
326
|
+
"compilerOptions": {
|
|
327
|
+
"target": "ES2022",
|
|
328
|
+
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
329
|
+
"module": "ESNext",
|
|
330
|
+
"moduleResolution": "Bundler",
|
|
331
|
+
"jsx": "react-jsx",
|
|
332
|
+
"strict": true,
|
|
333
|
+
"allowJs": true,
|
|
334
|
+
"checkJs": true,
|
|
335
|
+
"types": ["react", "react-dom"],
|
|
336
|
+
"skipLibCheck": true,
|
|
337
|
+
"noEmit": true,
|
|
338
|
+
"esModuleInterop": true,
|
|
339
|
+
"baseUrl": "../../..",
|
|
340
|
+
"paths": { "@shared/*": ["shared/*"] }
|
|
341
|
+
},
|
|
342
|
+
"include": ["./**/*.ts", "./**/*.tsx", "./**/*.js", "./**/*.jsx", "./**/*.mts", "./**/*.cts", "./**/*.mjs", "./**/*.cjs"]
|
|
343
|
+
}
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
在应用根目录创建 `eslint.mcp-ui.config.cjs`,独立检查 UI,不继承主应用的 ESLint 配置:
|
|
347
|
+
|
|
348
|
+
```js
|
|
349
|
+
const tseslint = require('typescript-eslint');
|
|
350
|
+
const globals = require('globals');
|
|
351
|
+
const hooks = require('eslint-plugin-react-hooks');
|
|
352
|
+
const { builtinModules } = require('node:module');
|
|
353
|
+
|
|
354
|
+
module.exports = tseslint.config({
|
|
355
|
+
files: ['server/mcp/ui/**/*.{ts,tsx,js,jsx,mts,cts,mjs,cjs}'],
|
|
356
|
+
extends: [...tseslint.configs.recommended],
|
|
357
|
+
languageOptions: { globals: globals.browser },
|
|
358
|
+
plugins: { 'react-hooks': hooks },
|
|
359
|
+
rules: {
|
|
360
|
+
...hooks.configs.recommended.rules,
|
|
361
|
+
'no-restricted-globals': ['error', 'process', 'Buffer', '__dirname', '__filename'],
|
|
362
|
+
'no-restricted-imports': ['error', {
|
|
363
|
+
paths: [...new Set(builtinModules.flatMap(name => [name, `node:${name.replace(/^node:/, '')}`]))],
|
|
364
|
+
patterns: ['@client/*', '@server/*', '@/*', '**/client/**'],
|
|
365
|
+
}],
|
|
366
|
+
},
|
|
367
|
+
});
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
以上配置由 Agent 在创建 UI 时生成,CLI 不自动创建或覆盖。主应用 ESLint 需在顶层 `ignores` 保留 `server/mcp/ui/**`,已有自定义脚本不要整份覆盖。UI 的间接导入和相对路径越界仍由构建预设检查。
|
|
371
|
+
|
|
372
|
+
升级后的 `scripts/lint.js` 提供 `--typecheck`(服务端、客户端、UI)、`--typecheck-mcp-ui`、`--eslint-mcp-ui`。`npm run lint` 包含独立 UI 检查,`--files` 按影响范围检查,shared 变更会触发 UI 类型检查。新模板的 `type:check` 指向 `node ./scripts/lint.js --typecheck`;旧工程通过 `fullstack-cli sync` 更新已知模板脚本,自定义聚合命令由 Agent 增量接入。CLI 必须使用包含这些模式的新版本,不能只更新 npm scripts。
|
|
373
|
+
|
|
374
|
+
只有外部 JS/TS/TSX 等源码会触发 UI 静态检查;纯 HTML/CSS 仍走 UI 构建。没有 UI 源码正常跳过;有源码缺配置、配置未覆盖源码或检查失败均报错。类型检查不能替代真实宿主内的渲染验收。
|
|
375
|
+
|
|
376
|
+
执行 `npx tsc --noEmit -p server/mcp/ui/tsconfig.json`,同时执行应用原有服务端类型检查。Skill 与 Tool 不要求一一对应,UI 也可以复用多个工具。
|
|
@@ -14,8 +14,6 @@ match-template-name: nestjs-react-fullstack
|
|
|
14
14
|
|
|
15
15
|
> **边界说明**:`authClient.session` 仅用于用户登录/登出/获取用户信息等鉴权操作。**插件调用(capability)不属于账户 SDK**,须使用独立的 `capabilityClient`(参见 plugin-guide)。
|
|
16
16
|
>
|
|
17
|
-
> **本地 dev 限制**:本 skill 的能力底层打 `/app/<appId>/__runtime__/api/v1/account/*`。这类端点只有在 `npm run dev:local` 成功 `env pull` 到 `MIAODA_DEV_PLATFORM_BASE`(或 `SANDBOX_PUBLIC_URL`)时才由 dev-proxy 反代到远端沙箱;**env pull 失败或未装 lark-cli 时该反代不挂**(启动脚本只 warn 不中断),表现为 POST 404 / GET 返回 200 + index.html。`useCurrentUserProfile()` 打的是 `account/login/user`;UserSelect / DepartmentSelect / UserDisplay 打 `search_user` / `list_users` / `user_profile` / `search_department`。先确认 `.env.local` 里有 `MIAODA_DEV_PLATFORM_BASE`;确实缺失时禁止改 `business-ui` 组件或换自研控件绕过,改由服务端出数据(`req.userContext` / `AuthNPaasService`)。
|
|
18
|
-
>
|
|
19
17
|
> **运行时边界**:本 skill 所有能力(`authClient`、`useCurrentUserProfile`、UserSelect/UserDisplay 等)仅限前端代码使用,**严禁在 `server/**` 中 import**。服务端获取用户身份用 `req.userContext` / `AuthNPaasService`(见 `user-identity` skill),完整边界规则见 coding-guide。
|
|
20
18
|
|
|
21
19
|
## 怎么选(决策指引)
|
|
@@ -112,11 +112,6 @@ shared/ # 前后端共享的目录
|
|
|
112
112
|
- **import 类型前必须确认实际导出**:先读取文件确认类型名称存在且拼写一致,禁止臆造类型名
|
|
113
113
|
- 属性统一 camelCase(禁止 snake_case),server 端 schema 的 snake_case 不应泄漏到接口类型
|
|
114
114
|
- shared 目录是前后端共享文件,**禁止反向引用** `@server/*`、`@client/*` 等路径别名
|
|
115
|
-
- **请求 DTO 中的 shared 字符串联合别名**:`shared/api.interface.ts` 是前后端契约权威。带 `class-validator` 的 NestJS 请求 DTO 字段若对应 shared 字符串字面量联合 `type` alias,不把 imported alias 直接写在 DTO 属性上;改用等价 inline literal union + `@IsString()` + `@IsIn([...])`,DTO class 仍 `implements` shared 请求接口。
|
|
116
|
-
- ❌ `type!: ExpressionElementType`
|
|
117
|
-
- ✅ `@IsString()` + `@IsIn(['tile', 'operator'])` + `type!: 'tile' | 'operator'`
|
|
118
|
-
- **机械核对**:先数 shared alias 字面量个数 N,inline union 和 `@IsIn` 数组都必须恰好 N 个,且每个字面量完全一致;数量或名称不等就是错,不能只写“不得窄于”。
|
|
119
|
-
- 边界:shared 真实 `enum` 用 `@IsEnum(EnumName)`;嵌套 DTO 用 `@ValidateNested()` + `@Type(() => XxxDto)`;普通对象字段用 `@IsObject()`;Date 转换按 `@Type(() => Date)` 处理。禁止用 `@Type(() => String)` 只为绕过 lint。
|
|
120
115
|
|
|
121
116
|
## 代码质量约束
|
|
122
117
|
|
|
@@ -134,7 +129,7 @@ shared/ # 前后端共享的目录
|
|
|
134
129
|
|
|
135
130
|
1. 优先使用项目已有依赖,仅在无法实现时安装新依赖
|
|
136
131
|
2. 使用前先查看 `package.json` 确保依赖已存在
|
|
137
|
-
3. **子包完整性检查**:部分库有多个子包(如 `@
|
|
132
|
+
3. **子包完整性检查**:部分库有多个子包(如 `@dnd-kit/core` + `@dnd-kit/sortable` + `@dnd-kit/utilities`),添加 import 后必须确认 package.json 中包含所有需要的子包
|
|
138
133
|
4. 用法不清时查看 readme,可进一步搜索或网页访问获取信息
|
|
139
134
|
|
|
140
135
|
## 文件命名约定
|
|
@@ -152,39 +147,6 @@ shared/ # 前后端共享的目录
|
|
|
152
147
|
- `/api` 和 `/openapi` 代理已配置,前端 TS 严格模式已禁用,后端已启用
|
|
153
148
|
- Rspack 用于构建,已配置好,**禁止修改**
|
|
154
149
|
|
|
155
|
-
### 平台 runtime 接口(`__runtime__`)在本地要靠 env pull 才通
|
|
156
|
-
|
|
157
|
-
dev server 自己只代理 `/api`、`/openapi`、`/__innerapi__`。`/app/<appId>/__runtime__/*` 这一整类平台 runtime 接口走的是另一条路:`npm run dev:local` 会先跑 `lark-cli apps +env-pull` 把沙箱身份/凭证拉进 `.env.local`,其中的 `MIAODA_DEV_PLATFORM_BASE`(或 legacy `SANDBOX_PUBLIC_URL`)才让 dev-proxy 把 `__runtime__` 反代到远端沙箱。
|
|
158
|
-
|
|
159
|
-
**env pull 失败、未装 lark-cli、或 `.spark/meta.json` 缺 app_id 时,启动脚本只 warn 不中断**,反代就不会挂上。此时这些端点的症状是:**POST 返回 404;GET 落到 Vite 的 SPA fallback,返回 200 + `index.html`**(业务侧表现为「返回一坨 HTML,JSON 解析失败」)。
|
|
160
|
-
|
|
161
|
-
常踩到的端点:
|
|
162
|
-
|
|
163
|
-
| 端点 | 方法 | 谁会打到它 |
|
|
164
|
-
|------|------|-----------|
|
|
165
|
-
| `api/v1/permissions/roles` | GET | `AppContainer` 每次加载应用自动打 |
|
|
166
|
-
| `api/v1/observability/{logs,traces,metrics}/collect`、`current_server_timestamp` | POST/GET | 平台埋点上报,自动打 |
|
|
167
|
-
| `api/v1/account/login/user` | POST | `useCurrentUserProfile()` → `authClient.session.getUserInfo()` |
|
|
168
|
-
| `api/v1/account/search_user`、`list_users`、`user_profile`、`search_department`、`convert_lark_user` | POST / `user_profile` 是 GET | `business-ui` 的 UserSelect / DepartmentSelect / UserDisplay |
|
|
169
|
-
| `api/v1/account/search`、`api/v1/account/chat/list_chats` | POST | ChatSelect 选群组件 |
|
|
170
|
-
| `api/v1/studio/user/profile` | GET | `getUserProfile()` |
|
|
171
|
-
| `api/v1/storage/object/<bucket>/pre_upload` | POST | `dataloom.storage.uploadFile()` 前端上传 |
|
|
172
|
-
| `api/v1/studio/plugins/tmp_files/acquire_upload_url`、`acquire_download_url` | POST | `capabilityClient` 的文件类入参 |
|
|
173
|
-
|
|
174
|
-
> `useCurrentUserProfile()` 打的是 `account/login/user`,**不是** `user_profile`/`search_user` 那一排;它另外还打一个 `GET /api/authnpaas/lark-user-id`,那条走 `/api`、被代理、正常。
|
|
175
|
-
|
|
176
|
-
排查顺序:先看 `.env.local` 里有没有 `MIAODA_DEV_PLATFORM_BASE`,没有就重跑 `env pull`。确认拉不到时:
|
|
177
|
-
|
|
178
|
-
- **禁止**改 `client/src/components/business-ui/**`(平台保护文件)、**禁止**给组件加兜底请求或改用自研选人控件绕过——发布后这些端点是正常的,绕过写法反而是错的
|
|
179
|
-
- 需要验证「按当前用户过滤」「展示用户姓名」这类逻辑:改由服务端出数据。服务端的 `req.userContext` 和 `AuthNPaasService` 由网关注入,不受此限制(见 `user-identity` / `contacts-service` skill)。`useCurrentUserProfile()` 长期停在加载态就是这个原因
|
|
180
|
-
- 需要验证上传链路:改用接口测试直接打后端接口,或复用库里已有的文件 URL
|
|
181
|
-
|
|
182
|
-
另外,`UserSelect` 弹层是**搜索驱动**的:不输关键词时列表为空属预期行为,不是接口挂了。
|
|
183
|
-
|
|
184
|
-
### 服务端启动即崩、`/api` 全部 502
|
|
185
|
-
|
|
186
|
-
报错形如 `UnknownDependenciesException: Nest can't resolve dependencies of the XxxService (?)... argument Function at index [0]` 时,先查构造函数有没有空参 `@Inject()`(见 plugin-guide),不要先去改 Module 的 `imports` / `providers`。
|
|
187
|
-
|
|
188
150
|
## 质量保障流程
|
|
189
151
|
|
|
190
152
|
修改代码后:
|
|
@@ -243,8 +205,6 @@ dev server 自己只代理 `/api`、`/openapi`、`/__innerapi__`。`/app/<appId>
|
|
|
243
205
|
- **三方集成**:调用第三方 API 需在后端实现,使用 @nestjs/axios
|
|
244
206
|
- **文件上传**:前端用 dataloom SDK 上传,服务端仅保存元信息(不要在服务端处理 multipart/form-data)
|
|
245
207
|
- **环境判断**:`process.env.NODE_ENV === "production"` 表示生产环境
|
|
246
|
-
- **运行时文件系统(CRITICAL)**:生产 FaaS 中 `process.cwd()` 指向只读部署目录 `/opt/bytefaas`;禁止在该目录或项目目录下创建或写入 `uploads`、`logs`、缓存等,也禁止“先写工作目录,失败再回退 `/tmp`”。临时文件、下载内容和处理中间产物必须直接写入 `/tmp` 下的独立目录
|
|
247
|
-
- **启动生命周期**:`bootstrap`、`constructor`、`onModuleInit` 和 `onApplicationBootstrap` 都位于服务启动或就绪关键路径,应快速完成,只做必要且确定的装配、内存初始化和配置校验;不得在其中执行或直接发起 DDL、下载大文件、全量同步、批量迁移、长轮询或无界外部请求。仅将钩子声明为 `async`,或在钩子内用 `void Promise`、异步 IIFE、定时器等 fire-and-forget 写法启动任务,都不算移出启动流程;耗时工作必须由平台支持的独立异步任务或外部触发机制执行
|
|
248
208
|
|
|
249
209
|
## 日志约定
|
|
250
210
|
|
|
@@ -498,7 +458,8 @@ NestJS 自己不读 env。直连 NestJS 端口 → header 缺失 → `req.userCo
|
|
|
498
458
|
- **图标**: Lucide React(唯一图标库,禁止 Emoji 和其他图标库)
|
|
499
459
|
- **表格/表单/图表**: 见下方"组件 Skill 召回规则",开发前必须先调用对应 Skill
|
|
500
460
|
- **用户**: 用户信息展示/选择必须用 `business-ui` 组件(阅读 README.md),禁止直接展示 userId
|
|
501
|
-
-
|
|
461
|
+
- **富文本**: `business-ui/tiptap-editor`(阅读 README.md)
|
|
462
|
+
- **Markdown 渲染**: `components/ui/streamdown`(内置 prose 排版)
|
|
502
463
|
|
|
503
464
|
## API 请求
|
|
504
465
|
|
|
@@ -531,7 +492,8 @@ import { axiosForBackend } from '@lark-apaas/client-toolkit/utils/getAxiosForBac
|
|
|
531
492
|
| --------------------------------------------------- | --------------------------------------- | --------------------------------- |
|
|
532
493
|
| Table | `@lark-apaas/client-toolkit/antd-table` | 数据表格,**先调 `/table-skill`** |
|
|
533
494
|
| UserSelect/UserDisplay/UserProfile/DepartmentSelect | `business-ui/*` | 用户/部门选择展示 |
|
|
534
|
-
|
|
|
495
|
+
| TiptapEditorComplete | `business-ui/tiptap-editor` | 富文本编辑器 |
|
|
496
|
+
| Streamdown | `components/ui/streamdown` | Markdown/流式渲染 |
|
|
535
497
|
|
|
536
498
|
### 组件 Skill 召回规则(强制执行)
|
|
537
499
|
|
|
@@ -676,8 +638,15 @@ return <h1>{data?.title || '未知标题'}</h1>;
|
|
|
676
638
|
| 验证 | zod |
|
|
677
639
|
| 工具函数 | lodash |
|
|
678
640
|
| 样式 | clsx |
|
|
641
|
+
| Excel | xlsx(**仅前端实现,禁止服务端实现**。解析后将结构化数据传到服务端保存) |
|
|
642
|
+
| PDF 导出 | jspdf + html2canvas(**仅前端实现,禁止服务端实现**) |
|
|
643
|
+
| 文件上传 | react-dropzone |
|
|
644
|
+
| 二维码 | qrcode.react |
|
|
679
645
|
| 用户反馈 | sonner |
|
|
680
|
-
|
|
646
|
+
| 拖拽 | @dnd-kit/core |
|
|
647
|
+
| 数字动画 | react-countup |
|
|
648
|
+
| Base64 | js-base64 — `import { encode, decode } from 'js-base64'` |
|
|
649
|
+
| 3D 场景 | cobe |
|
|
681
650
|
|
|
682
651
|
## 滚动分页最佳实践
|
|
683
652
|
|
|
@@ -38,7 +38,7 @@ lark-cli apps --help 2>&1 | grep -q '+plugin-install' && echo "READY" || echo "M
|
|
|
38
38
|
| 长耗时 AI 结果 | 大体量/多字段/多份/多语言/文件或多模态串联等结构信号命中时,优先前端 `callStream` 渐进展示;需保存则流式结束后复用 CRUD 落库;仅在 Client 侧无法满足时使用后端任务记录 + 状态查询 + 结果读取;禁止单个 HTTP 请求等待完整结果后才返回 |
|
|
39
39
|
| capabilityClient 导入 | `import { capabilityClient } from '@lark-apaas/client-toolkit'` |
|
|
40
40
|
| CapabilityService 导入 | `import { CapabilityService } from '@lark-apaas/fullstack-nestjs-core';` |
|
|
41
|
-
| CapabilityService 注入 | `@Inject(
|
|
41
|
+
| CapabilityService 注入 | `@Inject() private readonly capabilityService: CapabilityService` |
|
|
42
42
|
| 插件实例配置位置 | `server/capabilities/<instance_id>.json`(默认全栈应用)。见「配置目录」段 |
|
|
43
43
|
|
|
44
44
|
> **capabilityClient 导入警告**:`capabilityClient` 是从 `@lark-apaas/client-toolkit` 直接导入的独立对象,**不是**从 `getDataloom()` 上获取的。正确且唯一的方式:`import { capabilityClient } from '@lark-apaas/client-toolkit'`。
|
package/steering/nestjs-react-fullstack/skills_local/plugin-guide/references/plugin-coding-guide.md
CHANGED
|
@@ -76,18 +76,12 @@
|
|
|
76
76
|
#### call / callStream 函数签名
|
|
77
77
|
|
|
78
78
|
```typescript
|
|
79
|
-
//
|
|
80
|
-
.
|
|
81
|
-
.callStream<T = unknown>(actionKey: string, params?: Record<string, unknown>): AsyncIterable<T> // 流式
|
|
79
|
+
.call(actionKey: string, input: object) // 非流式,返回 Promise<output>
|
|
80
|
+
.callStream(actionKey: string, input: object) // 流式,返回 AsyncIterable<chunk>
|
|
82
81
|
```
|
|
83
82
|
|
|
84
83
|
- **第一个参数 `actionKey`**:必须是字符串,值来自 `get_plugin_ai_json` 返回的 `actions[].key`(如 `'sendFeishuMessage'`、`'textGenerate'`)
|
|
85
|
-
- **第二个参数 `
|
|
86
|
-
- 上面是**前端**签名,带泛型。**服务端 `CapabilityService` 的 `call` / `callStream` / `callStreamWithEvents` 都没有泛型**,两者不通用,详见「Server 侧调用方式」
|
|
87
|
-
|
|
88
|
-
> **`Record<string, unknown>` 不接 `interface` 声明的对象**(interface 没有隐式索引签名),直接传会撞
|
|
89
|
-
> `Argument of type 'XxxInput' is not assignable to parameter of type 'Record<string, unknown>'. Index signature for type 'string' is missing in type 'XxxInput'.`
|
|
90
|
-
> 三种正确写法:① 直接传内联对象字面量;② 入参类型用 `type XxxInput = { ... }` 而非 `interface`(type 别名有隐式索引签名);③ 已有 interface 时在调用点显式 `input as unknown as Record<string, unknown>`。**不要为此改用 `as any`。**
|
|
84
|
+
- **第二个参数 `input`**:必须是对象,结构符合 `actions[].inputSchema`
|
|
91
85
|
|
|
92
86
|
```typescript
|
|
93
87
|
// ❌ 错误:把参数 JSON.stringify 后当作 actionKey
|
|
@@ -274,49 +268,23 @@ import { CapabilityService } from '@lark-apaas/fullstack-nestjs-core';
|
|
|
274
268
|
export class XxxService {
|
|
275
269
|
private readonly logger = new Logger(XxxService.name);
|
|
276
270
|
|
|
277
|
-
// 注入 token 必须显式写成 @Inject(CapabilityService)
|
|
278
271
|
constructor(
|
|
279
|
-
@Inject(
|
|
272
|
+
@Inject() private readonly capabilityService: CapabilityService,
|
|
280
273
|
) {}
|
|
281
274
|
}
|
|
282
275
|
```
|
|
283
276
|
|
|
284
|
-
**禁止空参 `@Inject()`**。本 stack 的 `nest-cli.json` 用 `builder: swc`,空参 `@Inject()` 会把注入 token 置为 undefined,Nest 回退读 `design:type` 拿到 `Function`,服务端启动即崩溃、所有 `/api` 请求返回 502:
|
|
285
|
-
|
|
286
|
-
```
|
|
287
|
-
UnknownDependenciesException: Nest can't resolve dependencies of the XxxService (?).
|
|
288
|
-
Please make sure that the argument Function at index [0] is available in the XxxModule context.
|
|
289
|
-
```
|
|
290
|
-
|
|
291
|
-
见到这段报错先查构造函数有没有空参 `@Inject()`,不要去改 Module 的 `imports` / `providers`。
|
|
292
|
-
|
|
293
|
-
**业务 Module 不需要注册 CapabilityModule**。`PlatformModule` 是 `@Global()` 且已把 `CapabilityModule` 放进 `exports`,`CapabilityService` 全局可注入。不要在业务 Module 里写 `imports: [CapabilityModule.forRoot(...)]` 或 `imports: [PlatformModule.forRoot()]`。
|
|
294
|
-
|
|
295
|
-
同一规则适用于从 `@lark-apaas/fullstack-nestjs-core` 注入的其他平台服务(`AuthNPaasService`、`FileService` 等)。
|
|
296
|
-
|
|
297
277
|
#### 3. 调用示例
|
|
298
278
|
|
|
299
|
-
服务端 `CapabilityExecutor` 的**三个方法全都没有泛型参数**(`@lark-apaas/nestjs-capability`):
|
|
300
|
-
|
|
301
|
-
```typescript
|
|
302
|
-
call(actionName: string, input: unknown, context?: Partial<PluginActionContext>): Promise<unknown>;
|
|
303
|
-
callStream(actionName: string, input: unknown, context?): AsyncIterable<unknown>;
|
|
304
|
-
callStreamWithEvents(actionName: string, input: unknown, context?): AsyncIterable<StreamEvent<unknown>>;
|
|
305
|
-
```
|
|
306
|
-
|
|
307
|
-
写 `.call<T>(...)` / `.callStream<T>(...)` / `.callStreamWithEvents<T>(...)` 都会报「应有 0 个类型参数,但获得 1 个」。泛型只存在于前端 `capabilityClient`。注意服务端 `input` 是**必传**(没有 `?`),且类型是 `unknown` 而非前端的 `Record<string, unknown>`。服务端接 `unknown` 后在运行时收窄,禁止 `as any`:
|
|
308
|
-
|
|
309
279
|
```typescript
|
|
310
280
|
const inputParams = {
|
|
311
281
|
// 严格按 get_plugin_ai_json.actions[].inputSchema 构造
|
|
312
282
|
};
|
|
313
283
|
|
|
314
284
|
try {
|
|
315
|
-
const output
|
|
285
|
+
const output = await this.capabilityService
|
|
316
286
|
.load('')
|
|
317
287
|
.call('', inputParams);
|
|
318
|
-
// 运行时收窄后再取字段,例如:
|
|
319
|
-
// if (typeof output === 'object' && output !== null && 'content' in output) { ... }
|
|
320
288
|
return output;
|
|
321
289
|
} catch (error) {
|
|
322
290
|
this.logger.error('pluginInstance call failed', {
|
|
@@ -167,9 +167,12 @@ const structured = await (jsonExtractor as any).call('textToJson', { text: rawRe
|
|
|
167
167
|
|
|
168
168
|
### 创建结构化提取 PluginInstance 的关键要求
|
|
169
169
|
|
|
170
|
-
创建 `ai-text-to-json`
|
|
170
|
+
创建 `ai-text-to-json` 或 `ai-image-to-json` 类型的 PluginInstance 时:
|
|
171
171
|
|
|
172
|
-
|
|
172
|
+
1. **必须一次性定义所有需要提取的字段**(参考数据库 schema / 表单定义 / UI 设计),宁多勿漏
|
|
173
|
+
2. 字段类型仅支持 String / Number / Boolean,最多 20 个字段
|
|
174
|
+
3. 先调用 `get_plugin_ai_json` 确认上游插件的 `outputSchema`,确保输入格式正确
|
|
175
|
+
4. 图片→结构化数据场景,优先使用 `ai-image-to-json`(单步),避免不必要的链式调用
|
|
173
176
|
|
|
174
177
|
## 核心概念
|
|
175
178
|
|