@vectorx/xhs-cloud-cli 0.0.0-beta-20260108133839 → 0.0.0-beta-20260320063305
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/bin/rcb.js +47 -157
- package/lib/commands/agent/dev.js +42 -7
- package/lib/commands/agent/index.js +2 -0
- package/lib/commands/agent/info.js +107 -0
- package/lib/commands/agent/list.js +95 -0
- package/lib/commands/agent/new.js +32 -6
- package/lib/commands/auth/login.js +63 -35
- package/lib/commands/env/create.js +156 -0
- package/lib/commands/env/index.js +20 -0
- package/lib/commands/env/info.js +108 -0
- package/lib/commands/env/list.js +93 -0
- package/lib/commands/env/set.js +131 -0
- package/lib/commands/fun/deploy.js +184 -0
- package/lib/commands/fun/dev.js +189 -0
- package/lib/commands/fun/index.js +20 -0
- package/lib/commands/fun/list.js +77 -0
- package/lib/commands/fun/new.js +151 -0
- package/lib/commands/index.js +2 -0
- package/lib/constants/cmd.js +9 -9
- package/lib/core/base.js +83 -1
- package/lib/decorators/auth.js +8 -1
- package/lib/decorators/captureError.js +1 -0
- package/lib/main.js +8 -0
- package/lib/utils/agent-check.js +158 -0
- package/lib/utils/templates.js +75 -0
- package/package.json +12 -9
- package/templates/cloud-agents/agent-base-template/project.config.json +6 -0
- package/templates/{chatbox-agent → cloud-agents/agent-base-template}/src/index.js +0 -1
- package/templates/cloud-agents/agent-doc-template/.env.template +1 -0
- package/templates/cloud-agents/agent-doc-template/agent-cloudbase-functions.json +11 -0
- package/templates/cloud-agents/agent-doc-template/package.json +11 -0
- package/templates/cloud-agents/agent-doc-template/project.config.json +6 -0
- package/templates/cloud-agents/agent-doc-template/src/index.js +68 -0
- package/templates/cloud-agents/agent-image-template/.env.template +1 -0
- package/templates/cloud-agents/agent-image-template/agent-cloudbase-functions.json +11 -0
- package/templates/cloud-agents/agent-image-template/package.json +11 -0
- package/templates/cloud-agents/agent-image-template/project.config.json +6 -0
- package/templates/cloud-agents/agent-image-template/src/index.js +56 -0
- package/templates/cloud-agents/agent-text-template/.env.template +1 -0
- package/templates/cloud-agents/agent-text-template/agent-cloudbase-functions.json +11 -0
- package/templates/cloud-agents/agent-text-template/package.json +11 -0
- package/templates/cloud-agents/agent-text-template/project.config.json +6 -0
- package/templates/cloud-agents/agent-text-template/src/index.js +38 -0
- package/templates/cloud-agents/agent-voice-template/.env.template +1 -0
- package/templates/cloud-agents/agent-voice-template/agent-cloudbase-functions.json +11 -0
- package/templates/cloud-agents/agent-voice-template/package.json +11 -0
- package/templates/cloud-agents/agent-voice-template/project.config.json +6 -0
- package/templates/cloud-agents/agent-voice-template/src/index.js +81 -0
- package/templates/cloud-agents/templates.json +9 -0
- package/templates/cloud-dev/cloudfunction-route-template/.env.template +1 -0
- package/templates/cloud-dev/cloudfunction-route-template/README.md +277 -0
- package/templates/cloud-dev/cloudfunction-route-template/agent-cloudbase-functions.json +83 -0
- package/templates/cloud-dev/cloudfunction-route-template/package.json +10 -0
- package/templates/cloud-dev/cloudfunction-route-template/src/binary/index.js +207 -0
- package/templates/cloud-dev/cloudfunction-route-template/src/context/context-service.js +94 -0
- package/templates/cloud-dev/cloudfunction-route-template/src/context/index.js +57 -0
- package/templates/cloud-dev/cloudfunction-route-template/src/env/index.js +264 -0
- package/templates/cloud-dev/cloudfunction-route-template/src/form/index.js +138 -0
- package/templates/cloud-dev/cloudfunction-route-template/src/index.js +0 -0
- package/templates/cloud-dev/cloudfunction-route-template/src/json/index.js +194 -0
- package/templates/cloud-dev/cloudfunction-route-template/src/multipart/index.js +189 -0
- package/templates/cloud-dev/cloudfunction-route-template/src/text/index.js +319 -0
- package/templates/cloud-dev/cloudfunction-route-template/src/user/index.js +82 -0
- package/templates/cloud-dev/cloudfunction-template/.env.template +2 -0
- package/templates/cloud-dev/cloudfunction-template/agent-cloudbase-functions.json +17 -0
- package/templates/cloud-dev/cloudfunction-template/package.json +11 -0
- package/templates/cloud-dev/cloudfunction-template/src/echo.js +27 -0
- package/templates/cloud-dev/cloudfunction-template/src/index.js +34 -0
- package/templates/cloud-dev/templates.json +6 -0
- package/types/commands/agent/index.d.ts +2 -0
- package/types/commands/agent/info.d.ts +14 -0
- package/types/commands/agent/list.d.ts +14 -0
- package/types/commands/auth/login.d.ts +2 -0
- package/types/commands/env/create.d.ts +19 -0
- package/types/commands/env/index.d.ts +4 -0
- package/types/commands/env/info.d.ts +14 -0
- package/types/commands/env/list.d.ts +11 -0
- package/types/commands/env/set.d.ts +14 -0
- package/types/commands/fun/deploy.d.ts +14 -0
- package/types/commands/fun/dev.d.ts +14 -0
- package/types/commands/fun/index.d.ts +4 -0
- package/types/commands/fun/list.d.ts +14 -0
- package/types/commands/fun/new.d.ts +16 -0
- package/types/commands/index.d.ts +2 -0
- package/types/utils/agent-check.d.ts +1 -0
- package/types/utils/templates.d.ts +17 -0
- package/templates/weather-agent/index.js +0 -92
- package/templates/weather-agent/project.config.json +0 -4
- /package/templates/{chatbox-agent → cloud-agents/agent-base-template}/.env.template +0 -0
- /package/templates/{chatbox-agent → cloud-agents/agent-base-template}/agent-cloudbase-functions.json +0 -0
- /package/templates/{chatbox-agent → cloud-agents/agent-base-template}/package.json +0 -0
- /package/templates/{chatbox-agent → cloud-dev/cloudfunction-template}/project.config.json +0 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const { AgentRuntime, AgentDriver } = require("@vectorx/agent-runtime");
|
|
2
|
+
|
|
3
|
+
class DocAgent extends AgentRuntime {
|
|
4
|
+
async sendMessage(message) {
|
|
5
|
+
try {
|
|
6
|
+
const userContent = [];
|
|
7
|
+
|
|
8
|
+
// 1. 添加文档 URL(标准上传的文件)
|
|
9
|
+
if (Array.isArray(message?.files) && message.files.length > 0) {
|
|
10
|
+
message.files.forEach((file) => {
|
|
11
|
+
if ((file.fileSource === "standard" || !file.fileSource) && file.url) {
|
|
12
|
+
userContent.push({
|
|
13
|
+
type: "doc_url",
|
|
14
|
+
doc_url: [file.url],
|
|
15
|
+
file_parsing_strategy: "auto",
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 2. 添加用户文本提示
|
|
22
|
+
const userPrompt = message?.msg || "请分析这个文档";
|
|
23
|
+
if (userPrompt.trim()) {
|
|
24
|
+
userContent.push({ type: "text", text: userPrompt.trim() });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (userContent.length === 0) {
|
|
28
|
+
this.sseSender.send({
|
|
29
|
+
data: {
|
|
30
|
+
choices: [{
|
|
31
|
+
message: { role: "assistant", type: "error", content: "请上传文档或输入分析提示" },
|
|
32
|
+
finish_reason: "error",
|
|
33
|
+
}],
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
this.sseSender.end();
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const model = this.createModel("qwen-doc-turbo");
|
|
41
|
+
const modelResponse = await model.streamText({
|
|
42
|
+
messages: [{ role: "user", content: userContent }],
|
|
43
|
+
parameters: { temperature: 0.3, max_tokens: 2000 },
|
|
44
|
+
enable_thinking: true,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
for await (const chunk of modelResponse) {
|
|
48
|
+
this.sseSender.send({ data: chunk });
|
|
49
|
+
}
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error("DocAgent error:", error);
|
|
52
|
+
this.sseSender.send({
|
|
53
|
+
data: {
|
|
54
|
+
choices: [{
|
|
55
|
+
message: { role: "assistant", type: "error", content: "文档分析出错:" + (error?.message || "Unknown") },
|
|
56
|
+
finish_reason: "error",
|
|
57
|
+
}],
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
} finally {
|
|
61
|
+
this.sseSender.end();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
exports.main = (event, context) => {
|
|
67
|
+
return AgentDriver.run(event, context, new DocAgent(context));
|
|
68
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# 环境变量模板,复制为 .env 后填写
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const { AgentRuntime, AgentDriver } = require("@vectorx/agent-runtime");
|
|
2
|
+
|
|
3
|
+
class ImageAgent extends AgentRuntime {
|
|
4
|
+
async sendMessage(message) {
|
|
5
|
+
try {
|
|
6
|
+
if (!message?.msg) {
|
|
7
|
+
this.sseSender.send({
|
|
8
|
+
data: {
|
|
9
|
+
choices: [{
|
|
10
|
+
message: { role: "assistant", type: "error", content: "请输入图片描述" },
|
|
11
|
+
finish_reason: "error",
|
|
12
|
+
}],
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
this.sseSender.end();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const model = this.createModel("qwen-image");
|
|
20
|
+
const modelResponse = await model.streamText({
|
|
21
|
+
messages: [
|
|
22
|
+
{
|
|
23
|
+
role: "user",
|
|
24
|
+
content: [{ type: "text", text: message.msg }],
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
parameters: {
|
|
28
|
+
negative_prompt: "",
|
|
29
|
+
prompt_extend: true,
|
|
30
|
+
watermark: true,
|
|
31
|
+
size: "1328*1328",
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
for await (const chunk of modelResponse) {
|
|
36
|
+
this.sseSender.send({ data: chunk });
|
|
37
|
+
}
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.error("ImageAgent error:", error);
|
|
40
|
+
this.sseSender.send({
|
|
41
|
+
data: {
|
|
42
|
+
choices: [{
|
|
43
|
+
message: { role: "assistant", type: "error", content: "图片生成出错:" + (error?.message || "Unknown") },
|
|
44
|
+
finish_reason: "error",
|
|
45
|
+
}],
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
} finally {
|
|
49
|
+
this.sseSender.end();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
exports.main = (event, context) => {
|
|
55
|
+
return AgentDriver.run(event, context, new ImageAgent(context));
|
|
56
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# 环境变量模板,复制为 .env 后填写
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const { AgentRuntime, AgentDriver } = require("@vectorx/agent-runtime");
|
|
2
|
+
|
|
3
|
+
const systemPrompt = `你是一个友好的 AI 助手,用简洁清晰的语言回答用户问题。`;
|
|
4
|
+
|
|
5
|
+
class TextAgent extends AgentRuntime {
|
|
6
|
+
async sendMessage(input) {
|
|
7
|
+
try {
|
|
8
|
+
const userMsg = typeof input.msg === "string" ? input.msg : JSON.stringify(input.msg || "");
|
|
9
|
+
const model = this.createModel("qwen-plus");
|
|
10
|
+
const textStream = await model.streamText({
|
|
11
|
+
messages: [
|
|
12
|
+
{ role: "system", content: systemPrompt },
|
|
13
|
+
{ role: "user", content: [{ type: "text", text: userMsg }] },
|
|
14
|
+
],
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
for await (const chunk of textStream) {
|
|
18
|
+
this.sseSender.send({ data: chunk });
|
|
19
|
+
}
|
|
20
|
+
} catch (e) {
|
|
21
|
+
console.error("TextAgent error:", e);
|
|
22
|
+
this.sseSender.send({
|
|
23
|
+
data: {
|
|
24
|
+
choices: [{
|
|
25
|
+
message: { role: "assistant", type: "error", content: "处理出错:" + (e?.message || "Unknown") },
|
|
26
|
+
finish_reason: "error",
|
|
27
|
+
}],
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
} finally {
|
|
31
|
+
this.sseSender.end();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
exports.main = (event, context) => {
|
|
37
|
+
return AgentDriver.run(event, context, new TextAgent(context));
|
|
38
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# 环境变量模板,复制为 .env 后填写
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const { AgentRuntime, AgentDriver } = require("@vectorx/agent-runtime");
|
|
2
|
+
|
|
3
|
+
const SYSTEM_PROMPT = `你是一个语音助手,用简洁清晰的语言回答用户问题。回答控制在 100 字以内,适合语音朗读。不要使用 Markdown 格式、代码块或特殊符号。`;
|
|
4
|
+
|
|
5
|
+
const TTS_VOICE = "longhouge_v3";
|
|
6
|
+
const TTS_MODEL = "cosyvoice-v3-flash";
|
|
7
|
+
const TTS_FORMAT = "mp3";
|
|
8
|
+
|
|
9
|
+
class VoiceAgent extends AgentRuntime {
|
|
10
|
+
async sendMessage(input) {
|
|
11
|
+
const userMsg = typeof input.msg === "string" ? input.msg : JSON.stringify(input.msg || "");
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
// 1. 流式生成文本
|
|
15
|
+
const fullText = await this.streamTextResponse(userMsg);
|
|
16
|
+
if (!fullText) return;
|
|
17
|
+
|
|
18
|
+
// 2. TTS 合成语音
|
|
19
|
+
const ttsModel = this.createModel(TTS_MODEL);
|
|
20
|
+
const result = await ttsModel.generateSpeech({
|
|
21
|
+
text: fullText,
|
|
22
|
+
voice: TTS_VOICE,
|
|
23
|
+
format: TTS_FORMAT,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// 3. 发送音频 SSE 消息
|
|
27
|
+
const audioBase64 = result.audio.toString("base64");
|
|
28
|
+
this.sseSender.send({
|
|
29
|
+
data: {
|
|
30
|
+
choices: [{
|
|
31
|
+
message: {
|
|
32
|
+
role: "assistant",
|
|
33
|
+
type: "audio",
|
|
34
|
+
content: `data:audio/${TTS_FORMAT};base64,${audioBase64}`,
|
|
35
|
+
metadata: { model: TTS_MODEL, voice: TTS_VOICE },
|
|
36
|
+
},
|
|
37
|
+
finish_reason: "stop",
|
|
38
|
+
}],
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error("[VoiceAgent] Error:", error);
|
|
43
|
+
this.sseSender.send({
|
|
44
|
+
data: {
|
|
45
|
+
choices: [{
|
|
46
|
+
message: {
|
|
47
|
+
role: "assistant",
|
|
48
|
+
type: "error",
|
|
49
|
+
content: "语音合成出错:" + (error?.message || "Unknown"),
|
|
50
|
+
},
|
|
51
|
+
finish_reason: "error",
|
|
52
|
+
}],
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
} finally {
|
|
56
|
+
this.sseSender.end();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async streamTextResponse(userMsg) {
|
|
61
|
+
const textModel = this.createModel("qwen-plus");
|
|
62
|
+
const textStream = await textModel.streamText({
|
|
63
|
+
messages: [
|
|
64
|
+
{ role: "system", content: SYSTEM_PROMPT },
|
|
65
|
+
{ role: "user", content: [{ type: "text", text: userMsg }] },
|
|
66
|
+
],
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
let fullText = "";
|
|
70
|
+
for await (const chunk of textStream) {
|
|
71
|
+
this.sseSender.send({ data: chunk });
|
|
72
|
+
const delta = chunk.choices?.[0]?.delta?.content || chunk.choices?.[0]?.message?.content || "";
|
|
73
|
+
fullText += delta;
|
|
74
|
+
}
|
|
75
|
+
return fullText;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
exports.main = (event, context) => {
|
|
80
|
+
return AgentDriver.run(event, context, new VoiceAgent(context));
|
|
81
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"templates": [
|
|
3
|
+
{ "id": "agent-base-template", "name": "基础智能体", "desc": "默认智能体模板" },
|
|
4
|
+
{ "id": "agent-text-template", "name": "文本模型", "desc": "文本模型智能体(最简版)" },
|
|
5
|
+
{ "id": "agent-voice-template", "name": "语音合成", "desc": "语音合成智能体(最简版)" },
|
|
6
|
+
{ "id": "agent-image-template", "name": "文生图", "desc": "文生图智能体(最简版)" },
|
|
7
|
+
{ "id": "agent-doc-template", "name": "文档理解", "desc": "文档理解智能体(最简版)" }
|
|
8
|
+
]
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# key
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
# 云函数测试项目
|
|
2
|
+
|
|
3
|
+
这是一个云函数服务端的测试项目,用于测试和演示云函数在不同 Content-Type 和 HTTP 方法下的处理能力。
|
|
4
|
+
|
|
5
|
+
## 项目结构
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
src/
|
|
9
|
+
├── json/ # JSON 请求处理函数
|
|
10
|
+
├── form/ # 表单提交处理函数
|
|
11
|
+
├── multipart/ # 文件上传处理函数
|
|
12
|
+
├── binary/ # 二进制数据处理函数
|
|
13
|
+
├── text/ # 文本数据处理函数
|
|
14
|
+
└── user/ # 用户相关函数
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 函数目录说明
|
|
18
|
+
|
|
19
|
+
### 1. JSON 请求处理 (`src/json/`)
|
|
20
|
+
|
|
21
|
+
**覆盖场景:**
|
|
22
|
+
- `Content-Type: application/json`
|
|
23
|
+
- `Content-Type: application/vnd.api+json` (带 +json 后缀)
|
|
24
|
+
|
|
25
|
+
**业务场景:** 用户数据查询和创建(模拟数据库操作)
|
|
26
|
+
|
|
27
|
+
#### 函数列表
|
|
28
|
+
|
|
29
|
+
| 函数名 | 路由路径 | HTTP 方法 | 功能说明 |
|
|
30
|
+
|--------|---------|----------|---------|
|
|
31
|
+
| `jsonUsersList` | `/json/users` | GET | 查询用户列表(支持分页) |
|
|
32
|
+
| `jsonUsersCreate` | `/json/users/create` | POST | 创建用户(JSON Body) |
|
|
33
|
+
| `jsonUsersBatch` | `/json/users/batch` | POST | 批量查询用户(支持 +json content-type) |
|
|
34
|
+
|
|
35
|
+
**示例请求:**
|
|
36
|
+
```bash
|
|
37
|
+
# GET 查询用户列表
|
|
38
|
+
GET /json/users?page=1&limit=10
|
|
39
|
+
|
|
40
|
+
# POST 创建用户
|
|
41
|
+
POST /json/users/create
|
|
42
|
+
Content-Type: application/json
|
|
43
|
+
Body: { "name": "王五", "email": "wangwu@example.com", "age": 28 }
|
|
44
|
+
|
|
45
|
+
# POST 批量查询
|
|
46
|
+
POST /json/users/batch
|
|
47
|
+
Content-Type: application/vnd.api+json
|
|
48
|
+
Body: { "userIds": ["u001", "u002"] }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
### 2. 表单提交处理 (`src/form/`)
|
|
54
|
+
|
|
55
|
+
**覆盖场景:**
|
|
56
|
+
- `Content-Type: application/x-www-form-urlencoded`
|
|
57
|
+
|
|
58
|
+
**业务场景:** 表单数据提交和处理(模拟表单提交到数据库)
|
|
59
|
+
|
|
60
|
+
#### 函数列表
|
|
61
|
+
|
|
62
|
+
| 函数名 | 路由路径 | HTTP 方法 | 功能说明 |
|
|
63
|
+
|--------|---------|----------|---------|
|
|
64
|
+
| `formSubmit` | `/form/submit` | POST | 提交表单数据 |
|
|
65
|
+
| `formSubmissions` | `/form/submissions` | GET | 查询表单提交记录 |
|
|
66
|
+
|
|
67
|
+
**示例请求:**
|
|
68
|
+
```bash
|
|
69
|
+
# POST 提交表单
|
|
70
|
+
POST /form/submit
|
|
71
|
+
Content-Type: application/x-www-form-urlencoded
|
|
72
|
+
Body: name=张三&email=zhangsan@example.com&message=这是一条留言
|
|
73
|
+
|
|
74
|
+
# GET 查询提交记录
|
|
75
|
+
GET /form/submissions?email=zhangsan@example.com
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
### 3. 文件上传处理 (`src/multipart/`)
|
|
81
|
+
|
|
82
|
+
**覆盖场景:**
|
|
83
|
+
- `Content-Type: multipart/form-data`
|
|
84
|
+
|
|
85
|
+
**业务场景:** 文件上传,模拟保存到数据库或 OSS
|
|
86
|
+
|
|
87
|
+
#### 函数列表
|
|
88
|
+
|
|
89
|
+
| 函数名 | 路由路径 | HTTP 方法 | 功能说明 |
|
|
90
|
+
|--------|---------|----------|---------|
|
|
91
|
+
| `multipartUpload` | `/multipart/upload` | POST | 上传文件(multipart/form-data) |
|
|
92
|
+
| `multipartFiles` | `/multipart/files` | GET | 查询文件列表 |
|
|
93
|
+
|
|
94
|
+
**功能特性:**
|
|
95
|
+
- 支持多种文件格式(Buffer、base64 字符串、包装对象)
|
|
96
|
+
- 模拟保存文件元数据到数据库
|
|
97
|
+
- 模拟上传文件到 OSS
|
|
98
|
+
- 返回文件访问 URL
|
|
99
|
+
|
|
100
|
+
**示例请求:**
|
|
101
|
+
```bash
|
|
102
|
+
# POST 上传文件
|
|
103
|
+
POST /multipart/upload
|
|
104
|
+
Content-Type: multipart/form-data
|
|
105
|
+
Body: FormData with file field
|
|
106
|
+
|
|
107
|
+
# GET 查询文件列表
|
|
108
|
+
GET /multipart/files?fileType=image/png
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
### 4. 二进制数据处理 (`src/binary/`)
|
|
114
|
+
|
|
115
|
+
**覆盖场景:**
|
|
116
|
+
- `Content-Type: application/octet-stream`
|
|
117
|
+
|
|
118
|
+
**业务场景:** 处理二进制文件(图片、文档等),模拟保存到数据库或 OSS
|
|
119
|
+
|
|
120
|
+
#### 函数列表
|
|
121
|
+
|
|
122
|
+
| 函数名 | 路由路径 | HTTP 方法 | 功能说明 |
|
|
123
|
+
|--------|---------|----------|---------|
|
|
124
|
+
| `binaryUpload` | `/binary/upload` | POST | 上传二进制文件 |
|
|
125
|
+
| `binaryFiles` | `/binary/files` | GET | 查询二进制文件信息 |
|
|
126
|
+
|
|
127
|
+
**功能特性:**
|
|
128
|
+
- 自动检测文件类型(JPEG、PNG、PDF 等)
|
|
129
|
+
- 通过文件头(Magic Number)识别文件格式
|
|
130
|
+
- 模拟保存到 OSS
|
|
131
|
+
- 图片文件自动生成缩略图(模拟)
|
|
132
|
+
- 保存文件元数据到数据库
|
|
133
|
+
|
|
134
|
+
**示例请求:**
|
|
135
|
+
```bash
|
|
136
|
+
# POST 上传二进制文件
|
|
137
|
+
POST /binary/upload
|
|
138
|
+
Content-Type: application/octet-stream
|
|
139
|
+
Body: <binary data>
|
|
140
|
+
|
|
141
|
+
# GET 查询文件信息
|
|
142
|
+
GET /binary/files?fileType=image/jpeg
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
### 5. 文本数据处理 (`src/text/`)
|
|
148
|
+
|
|
149
|
+
**覆盖场景:**
|
|
150
|
+
- `Content-Type: text/plain`
|
|
151
|
+
- `Content-Type: text/html`
|
|
152
|
+
- `Content-Type: text/xml`
|
|
153
|
+
- 默认 text(未指定 content-type)
|
|
154
|
+
|
|
155
|
+
**业务场景:** 处理文本数据,模拟日志记录、文本分析等
|
|
156
|
+
|
|
157
|
+
#### 函数列表
|
|
158
|
+
|
|
159
|
+
| 函数名 | 路由路径 | HTTP 方法 | 功能说明 |
|
|
160
|
+
|--------|---------|----------|---------|
|
|
161
|
+
| `textSubmit` | `/text/submit` | POST | 提交文本内容 |
|
|
162
|
+
| `textLogs` | `/text/logs` | GET | 查询文本日志 |
|
|
163
|
+
| `textHtml` | `/text/html` | POST | 处理 HTML 内容 |
|
|
164
|
+
|
|
165
|
+
**功能特性:**
|
|
166
|
+
- 文本分析(字数统计、字符统计、行数统计)
|
|
167
|
+
- 关键词提取
|
|
168
|
+
- HTML 解析(提取标题、链接)
|
|
169
|
+
- 日志记录和查询
|
|
170
|
+
|
|
171
|
+
**示例请求:**
|
|
172
|
+
```bash
|
|
173
|
+
# POST 提交文本
|
|
174
|
+
POST /text/submit
|
|
175
|
+
Content-Type: text/plain
|
|
176
|
+
Body: "这是一段文本内容"
|
|
177
|
+
|
|
178
|
+
# GET 查询日志
|
|
179
|
+
GET /text/logs?contentType=text/plain&limit=10
|
|
180
|
+
|
|
181
|
+
# POST 处理 HTML
|
|
182
|
+
POST /text/html
|
|
183
|
+
Content-Type: text/html
|
|
184
|
+
Body: "<html><body><h1>Hello</h1></body></html>"
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
### 6. 用户相关 (`src/user/`)
|
|
190
|
+
|
|
191
|
+
**业务场景:** 用户基本信息查询
|
|
192
|
+
|
|
193
|
+
#### 函数列表
|
|
194
|
+
|
|
195
|
+
| 函数名 | 路由路径 | HTTP 方法 | 功能说明 |
|
|
196
|
+
|--------|---------|----------|---------|
|
|
197
|
+
| `userProfile` | `/user/profile` | POST | 查询用户基本信息 |
|
|
198
|
+
|
|
199
|
+
**示例请求:**
|
|
200
|
+
```bash
|
|
201
|
+
# POST 查询用户信息
|
|
202
|
+
POST /user/profile
|
|
203
|
+
Body: { "userId": "u10001" }
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## Content-Type 覆盖情况
|
|
209
|
+
|
|
210
|
+
本项目完整覆盖了以下 Content-Type 场景:
|
|
211
|
+
|
|
212
|
+
| Content-Type | 处理函数目录 | 说明 |
|
|
213
|
+
|-------------|------------|------|
|
|
214
|
+
| `application/json` | `json/` | JSON 数据请求 |
|
|
215
|
+
| `application/vnd.api+json` | `json/` | 带 +json 后缀的 JSON |
|
|
216
|
+
| `application/x-www-form-urlencoded` | `form/` | URL 编码表单数据 |
|
|
217
|
+
| `multipart/form-data` | `multipart/` | 多部分表单数据(文件上传) |
|
|
218
|
+
| `application/octet-stream` | `binary/` | 二进制数据流 |
|
|
219
|
+
| `text/plain` | `text/` | 纯文本 |
|
|
220
|
+
| `text/html` | `text/` | HTML 文本 |
|
|
221
|
+
| `text/xml` | `text/` | XML 文本 |
|
|
222
|
+
| 默认(未指定) | `text/` | 默认按 text/plain 处理 |
|
|
223
|
+
|
|
224
|
+
## HTTP 方法覆盖
|
|
225
|
+
|
|
226
|
+
- **GET**: 用于查询操作(列表查询、详情查询)
|
|
227
|
+
- **POST**: 用于创建操作(数据提交、文件上传)
|
|
228
|
+
|
|
229
|
+
## 日志功能
|
|
230
|
+
|
|
231
|
+
所有函数都包含详细的日志输出,包括:
|
|
232
|
+
|
|
233
|
+
- **函数调用开始日志**:记录函数调用时间、上下文 ID、事件 ID
|
|
234
|
+
- **参数日志**:记录请求参数和查询条件
|
|
235
|
+
- **处理过程日志**:记录数据处理的关键步骤
|
|
236
|
+
- **结果日志**:记录处理结果和返回数据
|
|
237
|
+
- **错误日志**:记录错误信息和失败原因
|
|
238
|
+
|
|
239
|
+
日志格式统一使用 `[函数名]` 前缀,便于日志过滤和追踪。
|
|
240
|
+
|
|
241
|
+
## 业务逻辑模拟
|
|
242
|
+
|
|
243
|
+
所有函数都模拟了真实的业务场景:
|
|
244
|
+
|
|
245
|
+
1. **数据库操作**:使用内存数组模拟数据库的增删改查
|
|
246
|
+
2. **文件存储**:模拟文件上传到 OSS,生成访问 URL
|
|
247
|
+
3. **数据验证**:参数校验、格式检查、业务规则验证
|
|
248
|
+
4. **错误处理**:统一的错误响应格式
|
|
249
|
+
5. **数据分析**:文本分析、文件类型检测等
|
|
250
|
+
|
|
251
|
+
## 配置说明
|
|
252
|
+
|
|
253
|
+
函数配置在 `agent-cloudbase-functions.json` 中定义,遵循以下规则:
|
|
254
|
+
|
|
255
|
+
- 每个函数一个目录
|
|
256
|
+
- 使用 `source: "index.js"` 映射函数入口
|
|
257
|
+
- 通过 `triggerPath` 定义路由路径
|
|
258
|
+
- 支持单实例多函数框架
|
|
259
|
+
|
|
260
|
+
## 使用说明
|
|
261
|
+
|
|
262
|
+
1. **启动开发服务器**:
|
|
263
|
+
```bash
|
|
264
|
+
npm run dev
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
2. **测试函数**:
|
|
268
|
+
使用 HTTP 客户端(如 Postman、curl)或云函数调试器调用各个路由
|
|
269
|
+
|
|
270
|
+
3. **查看日志**:
|
|
271
|
+
函数执行时会输出详细的日志信息,便于调试和追踪
|
|
272
|
+
|
|
273
|
+
## 注意事项
|
|
274
|
+
|
|
275
|
+
- 本项目仅用于测试和演示,所有数据存储在内存中,重启后会丢失
|
|
276
|
+
- 文件上传功能仅模拟保存元数据,实际文件内容不会持久化存储
|
|
277
|
+
- 所有业务逻辑都是模拟实现,不包含真实的数据库或 OSS 操作
|