@ynhcj/xiaoyi-channel 0.0.47-next → 0.0.48-next
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/dist/src/channel.js +5 -1
- package/dist/src/provider.js +20 -19
- package/dist/src/tools/device-tool-map.d.ts +1 -1
- package/dist/src/tools/device-tool-map.js +7 -2
- package/dist/src/tools/save-media-to-gallery-tool.d.ts +5 -0
- package/dist/src/tools/save-media-to-gallery-tool.js +178 -0
- package/dist/src/tools/upload-file-tool.js +2 -2
- package/dist/src/tools/xiaoyi-add-collection-tool.d.ts +4 -0
- package/dist/src/tools/xiaoyi-add-collection-tool.js +173 -0
- package/dist/src/tools/xiaoyi-collection-tool.js +42 -7
- package/dist/src/tools/xiaoyi-delete-collection-tool.d.ts +4 -0
- package/dist/src/tools/xiaoyi-delete-collection-tool.js +163 -0
- package/package.json +1 -1
package/dist/src/channel.js
CHANGED
|
@@ -25,6 +25,10 @@ import { viewPushResultTool } from "./tools/view-push-result-tool.js";
|
|
|
25
25
|
import { imageReadingTool } from "./tools/image-reading-tool.js";
|
|
26
26
|
import { timestampToUtc8Tool } from "./tools/timestamp-to-utc8-tool.js";
|
|
27
27
|
import { sendCommandToCarTool } from "./tools/send-command-to-car-tool.js";
|
|
28
|
+
import { xiaoyiCollectionTool } from "./tools/xiaoyi-collection-tool.js";
|
|
29
|
+
import { xiaoyiAddCollectionTool } from "./tools/xiaoyi-add-collection-tool.js";
|
|
30
|
+
import { xiaoyiDeleteCollectionTool } from "./tools/xiaoyi-delete-collection-tool.js";
|
|
31
|
+
import { saveMediaToGalleryTool } from "./tools/save-media-to-gallery-tool.js";
|
|
28
32
|
import { filterToolsByDevice } from "./tools/device-tool-map.js";
|
|
29
33
|
import { getCurrentSessionContext } from "./tools/session-manager.js";
|
|
30
34
|
import { logger } from "./utils/logger.js";
|
|
@@ -67,7 +71,7 @@ export const xyPlugin = {
|
|
|
67
71
|
},
|
|
68
72
|
outbound: xyOutbound,
|
|
69
73
|
agentTools: () => {
|
|
70
|
-
const allTools = [locationTool, noteTool, searchNoteTool, modifyNoteTool, calendarTool, searchCalendarTool, searchContactTool, searchPhotoGalleryTool, uploadPhotoTool, xiaoyiGuiTool, callPhoneTool, searchMessageTool, sendMessageTool, searchFileTool, uploadFileTool, createAlarmTool, searchAlarmTool, modifyAlarmTool, deleteAlarmTool, sendFileToUserTool, viewPushResultTool, imageReadingTool, timestampToUtc8Tool, sendCommandToCarTool];
|
|
74
|
+
const allTools = [locationTool, noteTool, searchNoteTool, modifyNoteTool, calendarTool, searchCalendarTool, searchContactTool, searchPhotoGalleryTool, uploadPhotoTool, xiaoyiGuiTool, callPhoneTool, searchMessageTool, sendMessageTool, searchFileTool, uploadFileTool, createAlarmTool, searchAlarmTool, modifyAlarmTool, deleteAlarmTool, sendFileToUserTool, viewPushResultTool, imageReadingTool, timestampToUtc8Tool, sendCommandToCarTool, xiaoyiCollectionTool, xiaoyiAddCollectionTool, xiaoyiDeleteCollectionTool, saveMediaToGalleryTool];
|
|
71
75
|
const ctx = getCurrentSessionContext();
|
|
72
76
|
const filtered = filterToolsByDevice(allTools, ctx?.deviceType);
|
|
73
77
|
logger.log(`[DEVICE-FILTER] deviceType=${ctx?.deviceType ?? "(none)"}, tools: ${allTools.length} → ${filtered.length} (${filtered.map(t => t.name).join(", ")})`);
|
package/dist/src/provider.js
CHANGED
|
@@ -4,12 +4,9 @@ import { getCurrentSessionContext } from "./tools/session-manager.js";
|
|
|
4
4
|
* Correspond to the three fields written to .xiaoyiruntime:
|
|
5
5
|
* TASK_ID, SESSION_ID, CONVERSATION_ID
|
|
6
6
|
*/
|
|
7
|
-
const
|
|
7
|
+
const HEADER_TRACE_ID = "x-hag-trace-id";
|
|
8
8
|
const HEADER_SESSION_ID = "x-session-id";
|
|
9
|
-
const
|
|
10
|
-
const EXTRA_PARAM_TASK_ID = "x-task-id";
|
|
11
|
-
const EXTRA_PARAM_SESSION_ID = "x-session-id";
|
|
12
|
-
const EXTRA_PARAM_CONVERSATION_ID = "x-conversation-id";
|
|
9
|
+
const HEADER_INTERACTION_ID = "x-interaction-id";
|
|
13
10
|
/**
|
|
14
11
|
* Encode uid to base64 and take first 32 chars.
|
|
15
12
|
*/
|
|
@@ -27,6 +24,7 @@ export const xiaoyiProvider = {
|
|
|
27
24
|
label: "Xiaoyi Provider",
|
|
28
25
|
docsPath: "/providers/models",
|
|
29
26
|
auth: [],
|
|
27
|
+
isCacheTtlEligible: () => true,
|
|
30
28
|
/**
|
|
31
29
|
* Inject dynamic session params into extraParams so they flow
|
|
32
30
|
* through to wrapStreamFn's ctx.extraParams.
|
|
@@ -39,11 +37,14 @@ export const xiaoyiProvider = {
|
|
|
39
37
|
prepareExtraParams: (ctx) => {
|
|
40
38
|
const sessionCtx = getCurrentSessionContext();
|
|
41
39
|
if (sessionCtx) {
|
|
40
|
+
const taskId = sessionCtx.taskId;
|
|
41
|
+
const sessionId = taskId.split("&")[0];
|
|
42
|
+
const interactionId = taskId.split("&")[1] || "";
|
|
42
43
|
return {
|
|
43
44
|
...ctx.extraParams,
|
|
44
|
-
[
|
|
45
|
-
[
|
|
46
|
-
[
|
|
45
|
+
[HEADER_TRACE_ID]: taskId,
|
|
46
|
+
[HEADER_SESSION_ID]: sessionId,
|
|
47
|
+
[HEADER_INTERACTION_ID]: interactionId,
|
|
47
48
|
};
|
|
48
49
|
}
|
|
49
50
|
// Fallback: uid-based values
|
|
@@ -55,9 +56,9 @@ export const xiaoyiProvider = {
|
|
|
55
56
|
const fallbackValue = `${prefix}_${ts}`;
|
|
56
57
|
return {
|
|
57
58
|
...ctx.extraParams,
|
|
58
|
-
[
|
|
59
|
-
[
|
|
60
|
-
[
|
|
59
|
+
[HEADER_TRACE_ID]: fallbackValue,
|
|
60
|
+
[HEADER_SESSION_ID]: fallbackValue,
|
|
61
|
+
[HEADER_INTERACTION_ID]: fallbackValue,
|
|
61
62
|
};
|
|
62
63
|
},
|
|
63
64
|
/**
|
|
@@ -73,21 +74,21 @@ export const xiaoyiProvider = {
|
|
|
73
74
|
return underlying;
|
|
74
75
|
const dynamicHeaders = {};
|
|
75
76
|
if (ctx.extraParams) {
|
|
76
|
-
const
|
|
77
|
-
const sessionId = ctx.extraParams[
|
|
78
|
-
const
|
|
79
|
-
if (typeof
|
|
80
|
-
dynamicHeaders[
|
|
77
|
+
const traceId = ctx.extraParams[HEADER_TRACE_ID];
|
|
78
|
+
const sessionId = ctx.extraParams[HEADER_SESSION_ID];
|
|
79
|
+
const interactionId = ctx.extraParams[HEADER_INTERACTION_ID];
|
|
80
|
+
if (typeof traceId === "string")
|
|
81
|
+
dynamicHeaders[HEADER_TRACE_ID] = traceId;
|
|
81
82
|
if (typeof sessionId === "string")
|
|
82
83
|
dynamicHeaders[HEADER_SESSION_ID] = sessionId;
|
|
83
|
-
if (typeof
|
|
84
|
-
dynamicHeaders[
|
|
84
|
+
if (typeof interactionId === "string")
|
|
85
|
+
dynamicHeaders[HEADER_INTERACTION_ID] = interactionId;
|
|
85
86
|
}
|
|
86
87
|
if (Object.keys(dynamicHeaders).length === 0)
|
|
87
88
|
return underlying;
|
|
88
89
|
return async (model, context, options) => {
|
|
89
90
|
// 记录输入
|
|
90
|
-
console.log(`[xiaoyiprovider] input messages
|
|
91
|
+
console.log(`[xiaoyiprovider] input messages: ${JSON.stringify(context.messages)}`);
|
|
91
92
|
if (context.systemPrompt) {
|
|
92
93
|
console.log(`[xiaoyiprovider] system prompt length: ${context.systemPrompt.length}`);
|
|
93
94
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/** Known device type enum. */
|
|
2
|
-
export declare const DEVICE_TYPES: readonly ["car", "
|
|
2
|
+
export declare const DEVICE_TYPES: readonly ["car", "2in1", "phone"];
|
|
3
3
|
export type DeviceType = (typeof DEVICE_TYPES)[number];
|
|
4
4
|
export declare function filterToolsByDevice(tools: any[], deviceType?: string): any[];
|
|
@@ -4,15 +4,20 @@
|
|
|
4
4
|
// - denylist: listed tools are blocked, everything else is available (used for permissive devices like pc)
|
|
5
5
|
// Tools NOT listed in any device entry → available to all devices (no restriction).
|
|
6
6
|
/** Known device type enum. */
|
|
7
|
-
export const DEVICE_TYPES = ["car", "
|
|
7
|
+
export const DEVICE_TYPES = ["car", "2in1", "phone"];
|
|
8
8
|
const DEVICE_TOOL_POLICY = {
|
|
9
|
-
|
|
9
|
+
"2in1": {
|
|
10
10
|
allowlist: false,
|
|
11
11
|
tools: [
|
|
12
12
|
"xiaoyi_gui_agent",
|
|
13
13
|
"call_phone",
|
|
14
14
|
"send_message",
|
|
15
|
+
"search_message",
|
|
16
|
+
"send_command_to_car",
|
|
15
17
|
"search_contact",
|
|
18
|
+
"QueryCollection",
|
|
19
|
+
"AddCollection",
|
|
20
|
+
"DeleteCollection",
|
|
16
21
|
],
|
|
17
22
|
},
|
|
18
23
|
};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { getXYWebSocketManager } from "../client.js";
|
|
2
|
+
import { sendCommand } from "../formatter.js";
|
|
3
|
+
import { getCurrentSessionContext } from "./session-manager.js";
|
|
4
|
+
import { XYFileUploadService } from "../file-upload.js";
|
|
5
|
+
/**
|
|
6
|
+
* Duck-typed ToolInputError: openclaw 按 .name 字段匹配,不用 instanceof。
|
|
7
|
+
* 抛出此错误会让 openclaw 返回 HTTP 400 而非 500,
|
|
8
|
+
* LLM 会将其识别为参数错误而非瞬时故障,不会触发重试。
|
|
9
|
+
*/
|
|
10
|
+
class ToolInputError extends Error {
|
|
11
|
+
status = 400;
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "ToolInputError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* XY save media to gallery tool - saves image or video files to user's device gallery.
|
|
19
|
+
* Supports local file paths (auto-uploaded to get public URL) and public URLs.
|
|
20
|
+
*/
|
|
21
|
+
export const saveMediaToGalleryTool = {
|
|
22
|
+
name: "SaveMediaToGallery",
|
|
23
|
+
label: "Save Media to Gallery",
|
|
24
|
+
description: `将图片文件或者视频文件保存到手机图库。
|
|
25
|
+
工具参数说明:
|
|
26
|
+
a. mediaType:非必填,string类型,不传端侧默认为pic。支持传 pic(图片) 或 video(视频)。
|
|
27
|
+
b. fileName:非必填,string类型,文件名称,不传手机侧默认生成随机uuid。
|
|
28
|
+
c. url:必填,string类型,支持本地路径或者公网url路径。如果是本地路径,会先上传获取公网url再保存到图库。
|
|
29
|
+
|
|
30
|
+
注意:
|
|
31
|
+
a. 操作超时时间为60秒,请勿重复调用此工具
|
|
32
|
+
b. 如果遇到各类调用失败场景,最多只能重试一次,不可以重复调用多次。
|
|
33
|
+
c. 调用工具前需认真检查调用参数是否满足工具要求
|
|
34
|
+
|
|
35
|
+
回复约束:如果工具返回没有授权或者其他报错,只需要完整描述没有授权或者其他报错内容即可,不需要主动给用户提供解决方案,例如告诉用户如何授权,如何解决报错等都是不需要的,请严格遵守。
|
|
36
|
+
`,
|
|
37
|
+
parameters: {
|
|
38
|
+
type: "object",
|
|
39
|
+
properties: {
|
|
40
|
+
mediaType: {
|
|
41
|
+
type: "string",
|
|
42
|
+
description: "非必填,不传默认为pic。支持 pic(图片) 或 video(视频)。",
|
|
43
|
+
},
|
|
44
|
+
fileName: {
|
|
45
|
+
type: "string",
|
|
46
|
+
description: "非必填,文件名称,不传手机侧默认生成随机uuid。",
|
|
47
|
+
},
|
|
48
|
+
url: {
|
|
49
|
+
type: "string",
|
|
50
|
+
description: "必填,支持本地路径或者公网url路径。如果是本地路径会先上传获取公网url。",
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
required: ["url"],
|
|
54
|
+
},
|
|
55
|
+
async execute(toolCallId, params) {
|
|
56
|
+
// Validate parameters
|
|
57
|
+
const { mediaType, fileName, url } = params;
|
|
58
|
+
if (!url || typeof url !== "string") {
|
|
59
|
+
throw new ToolInputError("缺少必填参数: url");
|
|
60
|
+
}
|
|
61
|
+
if (mediaType && !["pic", "video"].includes(mediaType)) {
|
|
62
|
+
throw new ToolInputError(`mediaType只支持 pic 或 video,当前值: ${mediaType}`);
|
|
63
|
+
}
|
|
64
|
+
// Strip file extension from fileName if present
|
|
65
|
+
let sanitizedFileName = fileName;
|
|
66
|
+
if (sanitizedFileName && typeof sanitizedFileName === "string") {
|
|
67
|
+
const lastDot = sanitizedFileName.lastIndexOf(".");
|
|
68
|
+
if (lastDot > 0) {
|
|
69
|
+
sanitizedFileName = sanitizedFileName.substring(0, lastDot);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Get session context
|
|
73
|
+
const sessionContext = getCurrentSessionContext();
|
|
74
|
+
if (!sessionContext) {
|
|
75
|
+
throw new Error("No active XY session found. SaveMediaToGallery tool can only be used during an active conversation.");
|
|
76
|
+
}
|
|
77
|
+
const { config, sessionId, taskId, messageId } = sessionContext;
|
|
78
|
+
// Get WebSocket manager
|
|
79
|
+
const wsManager = getXYWebSocketManager(config);
|
|
80
|
+
// Determine the URL: if it's a local path, upload first to get public URL
|
|
81
|
+
let publicUrl = url;
|
|
82
|
+
if (!url.startsWith("http://") && !url.startsWith("https://")) {
|
|
83
|
+
// Local file path - upload to get public URL
|
|
84
|
+
const uploadService = new XYFileUploadService(config.fileUploadUrl, config.apiKey, config.uid);
|
|
85
|
+
publicUrl = await uploadService.uploadFileAndGetUrl(url);
|
|
86
|
+
if (!publicUrl) {
|
|
87
|
+
throw new Error("本地文件上传失败,无法获取公网URL");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// Build intentParam
|
|
91
|
+
const intentParam = {
|
|
92
|
+
url: publicUrl,
|
|
93
|
+
};
|
|
94
|
+
if (mediaType) {
|
|
95
|
+
intentParam.mediaType = mediaType;
|
|
96
|
+
}
|
|
97
|
+
if (sanitizedFileName) {
|
|
98
|
+
intentParam.fileName = sanitizedFileName;
|
|
99
|
+
}
|
|
100
|
+
// Build SaveMediaToGallery command
|
|
101
|
+
const command = {
|
|
102
|
+
header: {
|
|
103
|
+
namespace: "Common",
|
|
104
|
+
name: "Action",
|
|
105
|
+
},
|
|
106
|
+
payload: {
|
|
107
|
+
cardParam: {},
|
|
108
|
+
executeParam: {
|
|
109
|
+
executeMode: "background",
|
|
110
|
+
intentName: "SaveMediaToGallery",
|
|
111
|
+
bundleName: "com.huawei.hmos.vassistant",
|
|
112
|
+
dimension: "",
|
|
113
|
+
needUnlock: true,
|
|
114
|
+
actionResponse: true,
|
|
115
|
+
appType: "OHOS_APP",
|
|
116
|
+
timeOut: 5,
|
|
117
|
+
intentParam,
|
|
118
|
+
permissionId: ["ohos.permission.WRITE_IMAGEVIDEO"],
|
|
119
|
+
achieveType: "INTENT",
|
|
120
|
+
},
|
|
121
|
+
responses: [
|
|
122
|
+
{
|
|
123
|
+
resultCode: "",
|
|
124
|
+
displayText: "",
|
|
125
|
+
ttsText: "",
|
|
126
|
+
},
|
|
127
|
+
],
|
|
128
|
+
needUploadResult: true,
|
|
129
|
+
noHalfPage: false,
|
|
130
|
+
pageControlRelated: false,
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
// Send command and wait for response (60 second timeout)
|
|
134
|
+
return new Promise((resolve, reject) => {
|
|
135
|
+
const timeout = setTimeout(() => {
|
|
136
|
+
wsManager.off("data-event", handler);
|
|
137
|
+
reject(new Error("保存媒体到图库超时(60秒)"));
|
|
138
|
+
}, 60000);
|
|
139
|
+
// Listen for data events from WebSocket
|
|
140
|
+
const handler = (event) => {
|
|
141
|
+
if (event.intentName === "SaveMediaToGallery") {
|
|
142
|
+
clearTimeout(timeout);
|
|
143
|
+
wsManager.off("data-event", handler);
|
|
144
|
+
if (event.status === "success" && event.outputs) {
|
|
145
|
+
resolve({
|
|
146
|
+
content: [
|
|
147
|
+
{
|
|
148
|
+
type: "text",
|
|
149
|
+
text: JSON.stringify(event.outputs),
|
|
150
|
+
}
|
|
151
|
+
]
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
reject(new Error(`保存媒体到图库失败: ${event.status}`));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
// Register event handler
|
|
160
|
+
wsManager.on("data-event", handler);
|
|
161
|
+
// Send the command
|
|
162
|
+
sendCommand({
|
|
163
|
+
config,
|
|
164
|
+
sessionId,
|
|
165
|
+
taskId,
|
|
166
|
+
messageId,
|
|
167
|
+
command,
|
|
168
|
+
})
|
|
169
|
+
.then(() => {
|
|
170
|
+
})
|
|
171
|
+
.catch((error) => {
|
|
172
|
+
clearTimeout(timeout);
|
|
173
|
+
wsManager.off("data-event", handler);
|
|
174
|
+
reject(error);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
};
|
|
@@ -18,10 +18,10 @@ export const uploadFileTool = {
|
|
|
18
18
|
label: "Upload File",
|
|
19
19
|
description: `工具能力描述:将手机本地文件上传并获取可公网访问的 URL。
|
|
20
20
|
|
|
21
|
-
前置工具调用:此工具使用前必须先调用 search_file 工具获取文件的 uri
|
|
21
|
+
前置工具调用:此工具使用前必须先调用 search_file 或者 QueryCollection 工具获取文件的 uri
|
|
22
22
|
|
|
23
23
|
工具参数说明:
|
|
24
|
-
a. 入参中的fileInfos数组,每个元素必须包含mediaUri字段(对应于search_file
|
|
24
|
+
a. 入参中的fileInfos数组,每个元素必须包含mediaUri字段(对应于search_file工具或者QueryCollection返回结果中的uri),必须与search_file或者QueryCollection结果中对应的uri完全保持一致,不要自行修改。
|
|
25
25
|
b. fileInfos中的timeout字段是可选的,表示上传文件超时时间,单位是毫秒,默认是20000(20秒)。
|
|
26
26
|
c. fileInfos 是文件在手机本地的信息数组(从 search_file 工具响应中获取)。限制:每次最多支持传入 5 条文件信息。
|
|
27
27
|
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { getXYWebSocketManager } from "../client.js";
|
|
2
|
+
import { sendCommand } from "../formatter.js";
|
|
3
|
+
import { getCurrentSessionContext } from "./session-manager.js";
|
|
4
|
+
import { XYFileUploadService } from "../file-upload.js";
|
|
5
|
+
/**
|
|
6
|
+
* Duck-typed ToolInputError: openclaw 按 .name 字段匹配,不用 instanceof。
|
|
7
|
+
* 抛出此错误会让 openclaw 返回 HTTP 400 而非 500,
|
|
8
|
+
* LLM 会将其识别为参数错误而非瞬时故障,不会触发重试。
|
|
9
|
+
*/
|
|
10
|
+
class ToolInputError extends Error {
|
|
11
|
+
status = 400;
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "ToolInputError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* XY add collection tool - adds data to user's XiaoYi collection.
|
|
19
|
+
*/
|
|
20
|
+
export const xiaoyiAddCollectionTool = {
|
|
21
|
+
name: "AddCollection",
|
|
22
|
+
label: "Add XiaoYi Collection",
|
|
23
|
+
description: `向小艺收藏中添加公共知识数据,可以给用户提供个性化体验。用户希望保存到个人化知识库中的数据都可以调用本技能。不同类型的数据对应的数据要求如下:
|
|
24
|
+
注意:
|
|
25
|
+
a. 操作超时时间为60秒,请勿重复调用此工具
|
|
26
|
+
b. 如果遇到各类调用失败场景,最多只能重试一次,不可以重复调用多次。
|
|
27
|
+
c. 调用工具前需认真检查调用参数是否满足工具要求
|
|
28
|
+
|
|
29
|
+
回复约束:如果工具返回没有授权或者其他报错,只需要完整描述没有授权或者其他报错内容即可,不需要主动给用户提供解决方案,例如告诉用户如何授权,如何解决报错等都是不需要的,请严格遵守。
|
|
30
|
+
`,
|
|
31
|
+
parameters: {
|
|
32
|
+
type: "object",
|
|
33
|
+
properties: {
|
|
34
|
+
content: {
|
|
35
|
+
type: "string",
|
|
36
|
+
description: "必填字段(HYPER_LINK/TEXT类型时)。用户添加收藏的链接url或文本原文。",
|
|
37
|
+
},
|
|
38
|
+
uri: {
|
|
39
|
+
type: "string",
|
|
40
|
+
description: "必填字段(IMAGE/FILE类型时)。图片或文件的地址链接。",
|
|
41
|
+
},
|
|
42
|
+
sourceAppBundleName: {
|
|
43
|
+
type: "string",
|
|
44
|
+
description: "非必填字段。标识该数据的来源应用。",
|
|
45
|
+
},
|
|
46
|
+
dataType: {
|
|
47
|
+
type: "string",
|
|
48
|
+
description: "必填字段。标识数据类型:HYPER_LINK表示网页,TEXT表示文本,IMAGE表示图片,FILE表示文件。",
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
required: ["dataType"],
|
|
52
|
+
},
|
|
53
|
+
async execute(toolCallId, params) {
|
|
54
|
+
// Validate parameters
|
|
55
|
+
const { content, uri, sourceAppBundleName, dataType } = params;
|
|
56
|
+
const validTypes = ["HYPER_LINK", "TEXT", "IMAGE", "FILE"];
|
|
57
|
+
if (!dataType || !validTypes.includes(dataType)) {
|
|
58
|
+
throw new ToolInputError(`dataType必填且必须为 HYPER_LINK、TEXT、IMAGE、FILE 之一,当前值: ${dataType}`);
|
|
59
|
+
}
|
|
60
|
+
if ((dataType === "HYPER_LINK" || dataType === "TEXT") && (!content || typeof content !== "string")) {
|
|
61
|
+
throw new ToolInputError(`dataType为${dataType}时,content字段必填且不能为空`);
|
|
62
|
+
}
|
|
63
|
+
if ((dataType === "IMAGE" || dataType === "FILE") && (!uri || typeof uri !== "string")) {
|
|
64
|
+
throw new ToolInputError(`dataType为${dataType}时,uri字段必填且不能为空`);
|
|
65
|
+
}
|
|
66
|
+
// Get session context
|
|
67
|
+
const sessionContext = getCurrentSessionContext();
|
|
68
|
+
if (!sessionContext) {
|
|
69
|
+
throw new Error("No active XY session found. AddCollection tool can only be used during an active conversation.");
|
|
70
|
+
}
|
|
71
|
+
const { config, sessionId, taskId, messageId } = sessionContext;
|
|
72
|
+
// Get WebSocket manager
|
|
73
|
+
const wsManager = getXYWebSocketManager(config);
|
|
74
|
+
// Handle uri: upload local paths to get public URL
|
|
75
|
+
let publicUri = uri;
|
|
76
|
+
if (uri && !uri.startsWith("http://") && !uri.startsWith("https://") && !uri.startsWith("file://")) {
|
|
77
|
+
const uploadService = new XYFileUploadService(config.fileUploadUrl, config.apiKey, config.uid);
|
|
78
|
+
publicUri = await uploadService.uploadFileAndGetUrl(uri);
|
|
79
|
+
if (!publicUri) {
|
|
80
|
+
throw new Error("本地文件上传失败,无法获取公网URL");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// Build intentParam
|
|
84
|
+
const intentParam = {
|
|
85
|
+
dataType,
|
|
86
|
+
};
|
|
87
|
+
if (content) {
|
|
88
|
+
intentParam.content = content;
|
|
89
|
+
}
|
|
90
|
+
if (publicUri) {
|
|
91
|
+
intentParam.uri = publicUri;
|
|
92
|
+
}
|
|
93
|
+
if (sourceAppBundleName) {
|
|
94
|
+
intentParam.sourceAppBundleName = sourceAppBundleName;
|
|
95
|
+
}
|
|
96
|
+
// Build AddCollection command
|
|
97
|
+
const command = {
|
|
98
|
+
header: {
|
|
99
|
+
namespace: "Common",
|
|
100
|
+
name: "Action",
|
|
101
|
+
},
|
|
102
|
+
payload: {
|
|
103
|
+
cardParam: {},
|
|
104
|
+
executeParam: {
|
|
105
|
+
executeMode: "background",
|
|
106
|
+
intentName: "AddCollection",
|
|
107
|
+
bundleName: "com.huawei.hmos.vassistant",
|
|
108
|
+
needUnlock: true,
|
|
109
|
+
actionResponse: true,
|
|
110
|
+
appType: "OHOS_APP",
|
|
111
|
+
timeOut: 5,
|
|
112
|
+
intentParam,
|
|
113
|
+
permissionId: [],
|
|
114
|
+
achieveType: "INTENT",
|
|
115
|
+
},
|
|
116
|
+
responses: [
|
|
117
|
+
{
|
|
118
|
+
resultCode: "",
|
|
119
|
+
displayText: "",
|
|
120
|
+
ttsText: "",
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
needUploadResult: true,
|
|
124
|
+
noHalfPage: false,
|
|
125
|
+
pageControlRelated: false,
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
// Send command and wait for response (60 second timeout)
|
|
129
|
+
return new Promise((resolve, reject) => {
|
|
130
|
+
const timeout = setTimeout(() => {
|
|
131
|
+
wsManager.off("data-event", handler);
|
|
132
|
+
reject(new Error("添加小艺收藏超时(60秒)"));
|
|
133
|
+
}, 60000);
|
|
134
|
+
// Listen for data events from WebSocket
|
|
135
|
+
const handler = (event) => {
|
|
136
|
+
if (event.intentName === "AddCollection") {
|
|
137
|
+
clearTimeout(timeout);
|
|
138
|
+
wsManager.off("data-event", handler);
|
|
139
|
+
if (event.status === "success" && event.outputs) {
|
|
140
|
+
resolve({
|
|
141
|
+
content: [
|
|
142
|
+
{
|
|
143
|
+
type: "text",
|
|
144
|
+
text: JSON.stringify(event.outputs),
|
|
145
|
+
}
|
|
146
|
+
]
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
reject(new Error(`添加小艺收藏失败: ${event.status}`));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
// Register event handler
|
|
155
|
+
wsManager.on("data-event", handler);
|
|
156
|
+
// Send the command
|
|
157
|
+
sendCommand({
|
|
158
|
+
config,
|
|
159
|
+
sessionId,
|
|
160
|
+
taskId,
|
|
161
|
+
messageId,
|
|
162
|
+
command,
|
|
163
|
+
})
|
|
164
|
+
.then(() => {
|
|
165
|
+
})
|
|
166
|
+
.catch((error) => {
|
|
167
|
+
clearTimeout(timeout);
|
|
168
|
+
wsManager.off("data-event", handler);
|
|
169
|
+
reject(error);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
},
|
|
173
|
+
};
|
|
@@ -1,26 +1,54 @@
|
|
|
1
1
|
import { getXYWebSocketManager } from "../client.js";
|
|
2
2
|
import { sendCommand } from "../formatter.js";
|
|
3
3
|
import { getCurrentSessionContext } from "./session-manager.js";
|
|
4
|
+
/**
|
|
5
|
+
* Duck-typed ToolInputError: openclaw 按 .name 字段匹配,不用 instanceof。
|
|
6
|
+
* 抛出此错误会让 openclaw 返回 HTTP 400 而非 500,
|
|
7
|
+
* LLM 会将其识别为参数错误而非瞬时故障,不会触发重试。
|
|
8
|
+
*/
|
|
9
|
+
class ToolInputError extends Error {
|
|
10
|
+
status = 400;
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "ToolInputError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
4
16
|
/**
|
|
5
17
|
* XY collection tool - retrieves user's collection data from XiaoYi.
|
|
6
18
|
* Returns personalized knowledge data saved in user's collection.
|
|
7
19
|
*/
|
|
8
20
|
export const xiaoyiCollectionTool = {
|
|
9
|
-
name: "
|
|
21
|
+
name: "QueryCollection",
|
|
10
22
|
label: "XiaoYi Collection",
|
|
11
|
-
description:
|
|
23
|
+
description: `检索用户在小艺收藏中记下来的公共知识数据,本技能支持查询用户收藏的公共知识数据,也可以根据特定语义化描述进行特定内容的检索,通过参数进行控制。本技能返回结果中,linkTitle是收藏内容的标题,description是对收藏内容的总结,label是收藏内容的标签,linkUrl是可以直接访问的原始内容链接。如果你认为某条数据对用户交互有用,可以通过linkUrl抓取更加丰富的原始数据。
|
|
24
|
+
注意:
|
|
25
|
+
a. 操作超时时间为60秒,请勿重复调用此工具
|
|
26
|
+
b. 如果遇到各类调用失败场景,最多只能重试一次,不可以重复调用多次。
|
|
27
|
+
c. 调用工具前需认真检查调用参数是否满足工具要求
|
|
28
|
+
|
|
29
|
+
回复约束:如果工具返回没有授权或者其他报错,只需要完整描述没有授权或者其他报错内容即可,不需要主动给用户提供解决方案,例如告诉用户如何授权,如何解决报错等都是不需要的,请严格遵守。
|
|
30
|
+
`,
|
|
12
31
|
parameters: {
|
|
13
32
|
type: "object",
|
|
14
33
|
properties: {
|
|
15
34
|
queryAll: {
|
|
16
35
|
type: "string",
|
|
17
|
-
description: "
|
|
18
|
-
|
|
36
|
+
description: "非必填参数,描述是否需要查询用户所有收藏数据。如果填入true则表示获取用户所有公共知识数据,其他参数无效。",
|
|
37
|
+
},
|
|
38
|
+
query: {
|
|
39
|
+
type: "string",
|
|
40
|
+
description: "非必填参数,queryAll不填或者为false则必填。用户的查询条件,可按照用户query进行检索。",
|
|
19
41
|
},
|
|
20
42
|
},
|
|
21
43
|
required: [],
|
|
22
44
|
},
|
|
23
45
|
async execute(toolCallId, params) {
|
|
46
|
+
// Validate parameters
|
|
47
|
+
const queryAll = params.queryAll;
|
|
48
|
+
const query = params.query;
|
|
49
|
+
if (queryAll !== "true" && (!query || typeof query !== "string")) {
|
|
50
|
+
throw new ToolInputError("queryAll不为true时,query参数必填");
|
|
51
|
+
}
|
|
24
52
|
// Get session context
|
|
25
53
|
const sessionContext = getCurrentSessionContext();
|
|
26
54
|
if (!sessionContext) {
|
|
@@ -29,6 +57,15 @@ export const xiaoyiCollectionTool = {
|
|
|
29
57
|
const { config, sessionId, taskId, messageId } = sessionContext;
|
|
30
58
|
// Get WebSocket manager
|
|
31
59
|
const wsManager = getXYWebSocketManager(config);
|
|
60
|
+
// Build intentParam
|
|
61
|
+
const intentParam = {};
|
|
62
|
+
if (queryAll === "true") {
|
|
63
|
+
intentParam.queryAll = "true";
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
intentParam.queryAll = "false";
|
|
67
|
+
intentParam.query = query;
|
|
68
|
+
}
|
|
32
69
|
// Build QueryCollection command
|
|
33
70
|
const command = {
|
|
34
71
|
header: {
|
|
@@ -45,9 +82,7 @@ export const xiaoyiCollectionTool = {
|
|
|
45
82
|
actionResponse: true,
|
|
46
83
|
appType: "OHOS_APP",
|
|
47
84
|
timeOut: 5,
|
|
48
|
-
intentParam
|
|
49
|
-
queryAll: params.queryAll || "true",
|
|
50
|
-
},
|
|
85
|
+
intentParam,
|
|
51
86
|
permissionId: [],
|
|
52
87
|
achieveType: "INTENT",
|
|
53
88
|
},
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { getXYWebSocketManager } from "../client.js";
|
|
2
|
+
import { sendCommand } from "../formatter.js";
|
|
3
|
+
import { getCurrentSessionContext } from "./session-manager.js";
|
|
4
|
+
/**
|
|
5
|
+
* Duck-typed ToolInputError: openclaw 按 .name 字段匹配,不用 instanceof。
|
|
6
|
+
* 抛出此错误会让 openclaw 返回 HTTP 400 而非 500,
|
|
7
|
+
* LLM 会将其识别为参数错误而非瞬时故障,不会触发重试。
|
|
8
|
+
*/
|
|
9
|
+
class ToolInputError extends Error {
|
|
10
|
+
status = 400;
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "ToolInputError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* XY delete collection tool - deletes data from user's XiaoYi collection.
|
|
18
|
+
*/
|
|
19
|
+
export const xiaoyiDeleteCollectionTool = {
|
|
20
|
+
name: "DeleteCollection",
|
|
21
|
+
label: "Delete XiaoYi Collection",
|
|
22
|
+
description: `从小艺收藏中删除之前已保存的公共知识数据。任何用户希望删除已保存到个人知识库的数据都可以调用本技能。如果用户想更新之前的收藏数据,需要先query获取itemId然后再delete,最后执行Add,按照这个步骤完成收藏数据更新。
|
|
23
|
+
注意:
|
|
24
|
+
a. 操作超时时间为60秒,请勿重复调用此工具
|
|
25
|
+
b. 如果遇到各类调用失败场景,最多只能重试一次,不可以重复调用多次。
|
|
26
|
+
c. 调用工具前需认真检查调用参数是否满足工具要求
|
|
27
|
+
|
|
28
|
+
回复约束:如果工具返回没有授权或者其他报错,只需要完整描述没有授权或者其他报错内容即可,不需要主动给用户提供解决方案,例如告诉用户如何授权,如何解决报错等都是不需要的,请严格遵守。
|
|
29
|
+
`,
|
|
30
|
+
parameters: {
|
|
31
|
+
type: "object",
|
|
32
|
+
properties: {
|
|
33
|
+
itemIds: {
|
|
34
|
+
// 不指定 type,允许传入数组或 JSON 字符串
|
|
35
|
+
// 具体的类型验证和转换在 execute 函数内部进行
|
|
36
|
+
description: "准备删除的数据的itemId合集。itemId可以由用户指定,也可以从之前检索回来的收藏数据项的itemId字段获取。",
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
required: ["itemIds"],
|
|
40
|
+
},
|
|
41
|
+
async execute(toolCallId, params) {
|
|
42
|
+
// ===== 参数规范化:兼容数组和 JSON 字符串 =====
|
|
43
|
+
let itemIds = null;
|
|
44
|
+
if (!params.itemIds) {
|
|
45
|
+
throw new ToolInputError("缺少必填参数: itemIds");
|
|
46
|
+
}
|
|
47
|
+
// 情况1: 已经是数组
|
|
48
|
+
if (Array.isArray(params.itemIds)) {
|
|
49
|
+
itemIds = params.itemIds;
|
|
50
|
+
}
|
|
51
|
+
// 情况2: 是字符串,尝试解析为 JSON 数组
|
|
52
|
+
else if (typeof params.itemIds === 'string') {
|
|
53
|
+
try {
|
|
54
|
+
const parsed = JSON.parse(params.itemIds);
|
|
55
|
+
if (Array.isArray(parsed)) {
|
|
56
|
+
itemIds = parsed;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
throw new ToolInputError("itemIds must be an array or a JSON string representing an array");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch (parseError) {
|
|
63
|
+
if (parseError instanceof ToolInputError)
|
|
64
|
+
throw parseError;
|
|
65
|
+
throw new ToolInputError(`itemIds must be a valid JSON array string. Parse error: ${parseError instanceof Error ? parseError.message : String(parseError)}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// 情况3: 其他类型,报错
|
|
69
|
+
else {
|
|
70
|
+
throw new ToolInputError(`itemIds must be an array or a JSON string, got ${typeof params.itemIds}`);
|
|
71
|
+
}
|
|
72
|
+
// 验证数组非空
|
|
73
|
+
if (!itemIds || itemIds.length === 0) {
|
|
74
|
+
throw new ToolInputError("itemIds array cannot be empty");
|
|
75
|
+
}
|
|
76
|
+
// Get session context
|
|
77
|
+
const sessionContext = getCurrentSessionContext();
|
|
78
|
+
if (!sessionContext) {
|
|
79
|
+
throw new Error("No active XY session found. DeleteCollection tool can only be used during an active conversation.");
|
|
80
|
+
}
|
|
81
|
+
const { config, sessionId, taskId, messageId } = sessionContext;
|
|
82
|
+
// Get WebSocket manager
|
|
83
|
+
const wsManager = getXYWebSocketManager(config);
|
|
84
|
+
// Build DeleteCollection command
|
|
85
|
+
const command = {
|
|
86
|
+
header: {
|
|
87
|
+
namespace: "Common",
|
|
88
|
+
name: "Action",
|
|
89
|
+
},
|
|
90
|
+
payload: {
|
|
91
|
+
cardParam: {},
|
|
92
|
+
executeParam: {
|
|
93
|
+
executeMode: "background",
|
|
94
|
+
intentName: "DeleteCollection",
|
|
95
|
+
bundleName: "com.huawei.hmos.vassistant",
|
|
96
|
+
needUnlock: true,
|
|
97
|
+
actionResponse: true,
|
|
98
|
+
appType: "OHOS_APP",
|
|
99
|
+
timeOut: 5,
|
|
100
|
+
intentParam: {
|
|
101
|
+
itemIds,
|
|
102
|
+
},
|
|
103
|
+
permissionId: [],
|
|
104
|
+
achieveType: "INTENT",
|
|
105
|
+
},
|
|
106
|
+
responses: [
|
|
107
|
+
{
|
|
108
|
+
resultCode: "",
|
|
109
|
+
displayText: "",
|
|
110
|
+
ttsText: "",
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
needUploadResult: true,
|
|
114
|
+
noHalfPage: false,
|
|
115
|
+
pageControlRelated: false,
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
// Send command and wait for response (60 second timeout)
|
|
119
|
+
return new Promise((resolve, reject) => {
|
|
120
|
+
const timeout = setTimeout(() => {
|
|
121
|
+
wsManager.off("data-event", handler);
|
|
122
|
+
reject(new Error("删除小艺收藏超时(60秒)"));
|
|
123
|
+
}, 60000);
|
|
124
|
+
// Listen for data events from WebSocket
|
|
125
|
+
const handler = (event) => {
|
|
126
|
+
if (event.intentName === "DeleteCollection") {
|
|
127
|
+
clearTimeout(timeout);
|
|
128
|
+
wsManager.off("data-event", handler);
|
|
129
|
+
if (event.status === "success" && event.outputs) {
|
|
130
|
+
resolve({
|
|
131
|
+
content: [
|
|
132
|
+
{
|
|
133
|
+
type: "text",
|
|
134
|
+
text: JSON.stringify(event.outputs),
|
|
135
|
+
}
|
|
136
|
+
]
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
reject(new Error(`删除小艺收藏失败: ${event.status}`));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
// Register event handler
|
|
145
|
+
wsManager.on("data-event", handler);
|
|
146
|
+
// Send the command
|
|
147
|
+
sendCommand({
|
|
148
|
+
config,
|
|
149
|
+
sessionId,
|
|
150
|
+
taskId,
|
|
151
|
+
messageId,
|
|
152
|
+
command,
|
|
153
|
+
})
|
|
154
|
+
.then(() => {
|
|
155
|
+
})
|
|
156
|
+
.catch((error) => {
|
|
157
|
+
clearTimeout(timeout);
|
|
158
|
+
wsManager.off("data-event", handler);
|
|
159
|
+
reject(error);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
},
|
|
163
|
+
};
|