@ynhcj/xiaoyi-channel 0.0.22-beta → 0.0.22-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/index.js +42 -0
- package/dist/src/bot.js +123 -41
- package/dist/src/channel.js +17 -5
- package/dist/src/client.js +11 -24
- package/dist/src/config.js +2 -2
- package/dist/src/cspl/call-api.d.ts +3 -0
- package/dist/src/cspl/call-api.js +79 -0
- package/dist/src/cspl/config.d.ts +19 -0
- package/dist/src/cspl/config.js +50 -0
- package/dist/src/cspl/constants.d.ts +43 -0
- package/dist/src/cspl/constants.js +22 -0
- package/dist/src/cspl/utils.d.ts +10 -0
- package/dist/src/cspl/utils.js +57 -0
- package/dist/src/file-upload.d.ts +5 -0
- package/dist/src/file-upload.js +92 -0
- package/dist/src/formatter.d.ts +14 -0
- package/dist/src/formatter.js +46 -2
- package/dist/src/monitor.js +47 -5
- package/dist/src/outbound.js +52 -6
- package/dist/src/parser.d.ts +7 -0
- package/dist/src/parser.js +22 -0
- package/dist/src/push.d.ts +8 -1
- package/dist/src/push.js +30 -22
- package/dist/src/reply-dispatcher.d.ts +1 -0
- package/dist/src/reply-dispatcher.js +116 -101
- package/dist/src/steer-injector.d.ts +16 -0
- package/dist/src/steer-injector.js +74 -0
- package/dist/src/task-manager.d.ts +55 -0
- package/dist/src/task-manager.js +136 -0
- package/dist/src/tools/calendar-tool.js +5 -3
- package/dist/src/tools/call-phone-tool.d.ts +5 -0
- package/dist/src/tools/call-phone-tool.js +183 -0
- package/dist/src/tools/create-alarm-tool.d.ts +7 -0
- package/dist/src/tools/create-alarm-tool.js +420 -0
- package/dist/src/tools/delete-alarm-tool.d.ts +11 -0
- package/dist/src/tools/delete-alarm-tool.js +216 -0
- package/dist/src/tools/image-reading-tool.d.ts +5 -0
- package/dist/src/tools/image-reading-tool.js +375 -0
- package/dist/src/tools/location-tool.js +8 -11
- package/dist/src/tools/modify-alarm-tool.d.ts +9 -0
- package/dist/src/tools/modify-alarm-tool.js +439 -0
- package/dist/src/tools/modify-note-tool.js +4 -9
- package/dist/src/tools/note-tool.js +32 -21
- package/dist/src/tools/search-alarm-tool.d.ts +8 -0
- package/dist/src/tools/search-alarm-tool.js +343 -0
- package/dist/src/tools/search-calendar-tool.js +9 -46
- package/dist/src/tools/search-contact-tool.js +4 -27
- package/dist/src/tools/search-file-tool.d.ts +5 -0
- package/dist/src/tools/search-file-tool.js +161 -0
- package/dist/src/tools/search-message-tool.d.ts +5 -0
- package/dist/src/tools/search-message-tool.js +149 -0
- package/dist/src/tools/search-note-tool.js +29 -22
- package/dist/src/tools/search-photo-gallery-tool.js +51 -34
- package/dist/src/tools/send-file-to-user-tool.d.ts +5 -0
- package/dist/src/tools/send-file-to-user-tool.js +318 -0
- package/dist/src/tools/send-message-tool.d.ts +5 -0
- package/dist/src/tools/send-message-tool.js +176 -0
- package/dist/src/tools/session-manager.d.ts +15 -0
- package/dist/src/tools/session-manager.js +99 -18
- package/dist/src/tools/upload-file-tool.d.ts +13 -0
- package/dist/src/tools/upload-file-tool.js +265 -0
- package/dist/src/tools/upload-photo-tool.js +14 -4
- package/dist/src/tools/view-push-result-tool.d.ts +5 -0
- package/dist/src/tools/view-push-result-tool.js +118 -0
- package/dist/src/tools/xiaoyi-collection-tool.d.ts +5 -0
- package/dist/src/tools/xiaoyi-collection-tool.js +190 -0
- package/dist/src/tools/xiaoyi-gui-tool.js +8 -8
- package/dist/src/trigger-handler.d.ts +22 -0
- package/dist/src/trigger-handler.js +59 -0
- package/dist/src/types.d.ts +1 -8
- package/dist/src/types.js +4 -0
- package/dist/src/utils/pushdata-manager.d.ts +28 -0
- package/dist/src/utils/pushdata-manager.js +171 -0
- package/dist/src/utils/pushid-manager.d.ts +12 -0
- package/dist/src/utils/pushid-manager.js +105 -0
- package/dist/src/websocket.d.ts +25 -31
- package/dist/src/websocket.js +218 -270
- package/package.json +1 -1
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import { getXYWebSocketManager } from "../client.js";
|
|
2
|
+
import { sendCommand } from "../formatter.js";
|
|
3
|
+
import { getCurrentSessionContext } from "./session-manager.js";
|
|
4
|
+
import { logger } from "../utils/logger.js";
|
|
5
|
+
// Enum definitions for alarm parameters (same as create-alarm-tool)
|
|
6
|
+
const ALARM_SNOOZE_DURATION_VALUES = [5, 10, 15, 20, 25, 30];
|
|
7
|
+
const ALARM_SNOOZE_TOTAL_VALUES = [0, 1, 3, 5, 10];
|
|
8
|
+
const ALARM_RING_DURATION_VALUES = [1, 5, 10, 15, 20, 30];
|
|
9
|
+
const DAYS_OF_WAKE_TYPE_VALUES = [0, 1, 2, 3, 4];
|
|
10
|
+
const DAYS_OF_WEEK_VALUES = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
|
|
11
|
+
/**
|
|
12
|
+
* XY modify alarm tool - modifies an existing alarm on user's device.
|
|
13
|
+
* Requires entityId from search_alarm or create_alarm tool.
|
|
14
|
+
*
|
|
15
|
+
* Prerequisites:
|
|
16
|
+
* 1. Call search_alarm or create_alarm tool first to get entityId
|
|
17
|
+
* 2. Use the entityId to identify which alarm to modify
|
|
18
|
+
*/
|
|
19
|
+
export const modifyAlarmTool = {
|
|
20
|
+
name: "modify_alarm",
|
|
21
|
+
label: "Modify Alarm",
|
|
22
|
+
description: `修改用户设备上已存在的闹钟。
|
|
23
|
+
|
|
24
|
+
必需参数:
|
|
25
|
+
- entityId: 闹钟的唯一标识符,必须先通过 search_alarm 或 create_alarm 工具获取
|
|
26
|
+
|
|
27
|
+
可选参数(与创建闹钟的参数完全一致):
|
|
28
|
+
- alarmTime: 闹钟时间,格式必须为:YYYYMMDD hhmmss(例如:20240315 143000)
|
|
29
|
+
- alarmTitle: 闹钟名称/标题
|
|
30
|
+
- alarmState: 闹钟开启状态,0=关闭,1=开启
|
|
31
|
+
- alarmSnoozeDuration: 小睡间隔(分钟),枚举值:5,10,15,20,25,30
|
|
32
|
+
- alarmSnoozeTotal: 再响次数,枚举值:0,1,3,5,10
|
|
33
|
+
- alarmRingDuration: 响铃时长(分钟),枚举值:1,5,10,15,20,30
|
|
34
|
+
- daysOfWakeType: 闹钟响铃类型,枚举值:0=单次,1=法定节假日,2=每天,3=自定义,4=法定工作日
|
|
35
|
+
- daysOfWeek: 自定义响铃星期,仅当daysOfWakeType=3(自定义时间)时必需且有效,其他情况不要传递此参数。数组或JSON字符串,枚举值:Mon,Tue,Wed,Thu,Fri,Sat,Sun。注意:仅支持长度为1的数组,如果需要一周中不同的几天,需要多次调用此工具
|
|
36
|
+
|
|
37
|
+
使用流程:
|
|
38
|
+
1. 先调用 search_alarm 工具查询闹钟,获取 entityId,
|
|
39
|
+
2. 调用此工具修改闹钟,传入 entityId 和需要修改的参数
|
|
40
|
+
3. 其余不涉及需改的参数,如果search_alarm 或 create_alarm的结果中有相应的值,需要一并填上,需要与原有的保持一致,防止不填采用默认值
|
|
41
|
+
|
|
42
|
+
注意事项:操作超时时间为60秒,请勿重复调用此工具,如果超时或失败,最多重试一次。`,
|
|
43
|
+
parameters: {
|
|
44
|
+
type: "object",
|
|
45
|
+
properties: {
|
|
46
|
+
entityId: {
|
|
47
|
+
type: "string",
|
|
48
|
+
description: "闹钟的唯一标识符,必须先通过 search_alarm 或 create_alarm 工具获取",
|
|
49
|
+
},
|
|
50
|
+
alarmTime: {
|
|
51
|
+
type: "string",
|
|
52
|
+
description: "闹钟时间,格式必须为:YYYYMMDD hhmmss(例如:20240315 143000)",
|
|
53
|
+
},
|
|
54
|
+
alarmTitle: {
|
|
55
|
+
type: "string",
|
|
56
|
+
description: "闹钟名称/标题",
|
|
57
|
+
},
|
|
58
|
+
alarmState: {
|
|
59
|
+
type: "number",
|
|
60
|
+
description: "闹钟开启状态:0=关闭,1=开启",
|
|
61
|
+
},
|
|
62
|
+
alarmSnoozeDuration: {
|
|
63
|
+
type: "number",
|
|
64
|
+
description: "小睡间隔(分钟),枚举值:5,10,15,20,25,30",
|
|
65
|
+
},
|
|
66
|
+
alarmSnoozeTotal: {
|
|
67
|
+
type: "number",
|
|
68
|
+
description: "再响次数,枚举值:0,1,3,5,10",
|
|
69
|
+
},
|
|
70
|
+
alarmRingDuration: {
|
|
71
|
+
type: "number",
|
|
72
|
+
description: "响铃时长(分钟),枚举值:1,5,10,15,20,30",
|
|
73
|
+
},
|
|
74
|
+
daysOfWakeType: {
|
|
75
|
+
type: "number",
|
|
76
|
+
description: "闹钟响铃类型:0=单次,1=法定节假日,2=每天,3=自定义,4=法定工作日",
|
|
77
|
+
},
|
|
78
|
+
daysOfWeek: {
|
|
79
|
+
// 不指定 type,允许传入数组或 JSON 字符串
|
|
80
|
+
// 具体的类型验证和转换在 execute 函数内部进行
|
|
81
|
+
description: "自定义响铃星期(仅当daysOfWakeType=3时需要,其他情况不要传递),数组或JSON字符串,枚举值:Mon,Tue,Wed,Thu,Fri,Sat,Sun。注意:仅支持长度为1的数组,如果需要一周中不同的几天,需要多次调用此工具",
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
required: ["entityId"],
|
|
85
|
+
},
|
|
86
|
+
async execute(toolCallId, params) {
|
|
87
|
+
logger.log(`[MODIFY_ALARM_TOOL] 🚀 Starting execution`);
|
|
88
|
+
logger.log(`[MODIFY_ALARM_TOOL] - toolCallId: ${toolCallId}`);
|
|
89
|
+
logger.log(`[MODIFY_ALARM_TOOL] - params:`, JSON.stringify(params));
|
|
90
|
+
logger.log(`[MODIFY_ALARM_TOOL] - timestamp: ${new Date().toISOString()}`);
|
|
91
|
+
// ===== Validate required parameter: entityId =====
|
|
92
|
+
if (!params.entityId || typeof params.entityId !== "string") {
|
|
93
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Missing or invalid entityId`);
|
|
94
|
+
throw new Error("Missing required parameter: entityId must be a string obtained from search_alarm or create_alarm");
|
|
95
|
+
}
|
|
96
|
+
logger.log(`[MODIFY_ALARM_TOOL] - entityId: ${params.entityId}`);
|
|
97
|
+
// ===== Build intentParam with provided parameters =====
|
|
98
|
+
const intentParam = {
|
|
99
|
+
entityName: "Alarm",
|
|
100
|
+
entityId: params.entityId,
|
|
101
|
+
};
|
|
102
|
+
// Parse and convert alarmTime if provided
|
|
103
|
+
if (params.alarmTime !== undefined && params.alarmTime !== null) {
|
|
104
|
+
if (typeof params.alarmTime !== "string") {
|
|
105
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid alarmTime type`);
|
|
106
|
+
throw new Error("alarmTime must be a string in format YYYYMMDD hhmmss");
|
|
107
|
+
}
|
|
108
|
+
logger.log(`[MODIFY_ALARM_TOOL] 🕒 Parsing alarmTime: ${params.alarmTime}`);
|
|
109
|
+
const alarmTimeMs = parseAlarmTimeToTimestamp(params.alarmTime);
|
|
110
|
+
if (alarmTimeMs === null) {
|
|
111
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid alarmTime format`);
|
|
112
|
+
throw new Error("Invalid alarmTime format. Required format: YYYYMMDD hhmmss (e.g., 20240315 143000)");
|
|
113
|
+
}
|
|
114
|
+
intentParam.alarmTime = alarmTimeMs;
|
|
115
|
+
logger.log(`[MODIFY_ALARM_TOOL] ✅ alarmTime converted to timestamp: ${alarmTimeMs}`);
|
|
116
|
+
}
|
|
117
|
+
// Add alarmTitle if provided
|
|
118
|
+
if (params.alarmTitle !== undefined && params.alarmTitle !== null) {
|
|
119
|
+
if (typeof params.alarmTitle !== "string") {
|
|
120
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid alarmTitle type`);
|
|
121
|
+
throw new Error("alarmTitle must be a string");
|
|
122
|
+
}
|
|
123
|
+
intentParam.alarmTitle = params.alarmTitle;
|
|
124
|
+
logger.log(`[MODIFY_ALARM_TOOL] - alarmTitle: ${params.alarmTitle}`);
|
|
125
|
+
}
|
|
126
|
+
// Add alarmState if provided
|
|
127
|
+
if (params.alarmState !== undefined && params.alarmState !== null) {
|
|
128
|
+
if (typeof params.alarmState !== "number") {
|
|
129
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid alarmState type`);
|
|
130
|
+
throw new Error("alarmState must be a number");
|
|
131
|
+
}
|
|
132
|
+
if (!ALARM_STATE_VALUES.includes(params.alarmState)) {
|
|
133
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid alarmState value: ${params.alarmState}`);
|
|
134
|
+
throw new Error(`alarmState must be one of: ${ALARM_STATE_VALUES.join(", ")}`);
|
|
135
|
+
}
|
|
136
|
+
intentParam.alarmState = params.alarmState;
|
|
137
|
+
logger.log(`[MODIFY_ALARM_TOOL] - alarmState: ${params.alarmState}`);
|
|
138
|
+
}
|
|
139
|
+
// Add alarmSnoozeDuration if provided
|
|
140
|
+
if (params.alarmSnoozeDuration !== undefined && params.alarmSnoozeDuration !== null) {
|
|
141
|
+
if (typeof params.alarmSnoozeDuration !== "number") {
|
|
142
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid alarmSnoozeDuration type`);
|
|
143
|
+
throw new Error("alarmSnoozeDuration must be a number");
|
|
144
|
+
}
|
|
145
|
+
if (!ALARM_SNOOZE_DURATION_VALUES.includes(params.alarmSnoozeDuration)) {
|
|
146
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid alarmSnoozeDuration value: ${params.alarmSnoozeDuration}`);
|
|
147
|
+
throw new Error(`alarmSnoozeDuration must be one of: ${ALARM_SNOOZE_DURATION_VALUES.join(", ")}`);
|
|
148
|
+
}
|
|
149
|
+
intentParam.alarmSnoozeDuration = params.alarmSnoozeDuration;
|
|
150
|
+
logger.log(`[MODIFY_ALARM_TOOL] - alarmSnoozeDuration: ${params.alarmSnoozeDuration}`);
|
|
151
|
+
}
|
|
152
|
+
// Add alarmSnoozeTotal if provided
|
|
153
|
+
if (params.alarmSnoozeTotal !== undefined && params.alarmSnoozeTotal !== null) {
|
|
154
|
+
if (typeof params.alarmSnoozeTotal !== "number") {
|
|
155
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid alarmSnoozeTotal type`);
|
|
156
|
+
throw new Error("alarmSnoozeTotal must be a number");
|
|
157
|
+
}
|
|
158
|
+
if (!ALARM_SNOOZE_TOTAL_VALUES.includes(params.alarmSnoozeTotal)) {
|
|
159
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid alarmSnoozeTotal value: ${params.alarmSnoozeTotal}`);
|
|
160
|
+
throw new Error(`alarmSnoozeTotal must be one of: ${ALARM_SNOOZE_TOTAL_VALUES.join(", ")}`);
|
|
161
|
+
}
|
|
162
|
+
intentParam.alarmSnoozeTotal = params.alarmSnoozeTotal;
|
|
163
|
+
logger.log(`[MODIFY_ALARM_TOOL] - alarmSnoozeTotal: ${params.alarmSnoozeTotal}`);
|
|
164
|
+
}
|
|
165
|
+
// Add alarmRingDuration if provided
|
|
166
|
+
if (params.alarmRingDuration !== undefined && params.alarmRingDuration !== null) {
|
|
167
|
+
if (typeof params.alarmRingDuration !== "number") {
|
|
168
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid alarmRingDuration type`);
|
|
169
|
+
throw new Error("alarmRingDuration must be a number");
|
|
170
|
+
}
|
|
171
|
+
if (!ALARM_RING_DURATION_VALUES.includes(params.alarmRingDuration)) {
|
|
172
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid alarmRingDuration value: ${params.alarmRingDuration}`);
|
|
173
|
+
throw new Error(`alarmRingDuration must be one of: ${ALARM_RING_DURATION_VALUES.join(", ")}`);
|
|
174
|
+
}
|
|
175
|
+
intentParam.alarmRingDuration = params.alarmRingDuration;
|
|
176
|
+
logger.log(`[MODIFY_ALARM_TOOL] - alarmRingDuration: ${params.alarmRingDuration}`);
|
|
177
|
+
}
|
|
178
|
+
// Add daysOfWakeType if provided
|
|
179
|
+
if (params.daysOfWakeType !== undefined && params.daysOfWakeType !== null) {
|
|
180
|
+
if (typeof params.daysOfWakeType !== "number") {
|
|
181
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid daysOfWakeType type`);
|
|
182
|
+
throw new Error("daysOfWakeType must be a number");
|
|
183
|
+
}
|
|
184
|
+
if (!DAYS_OF_WAKE_TYPE_VALUES.includes(params.daysOfWakeType)) {
|
|
185
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid daysOfWakeType value: ${params.daysOfWakeType}`);
|
|
186
|
+
throw new Error(`daysOfWakeType must be one of: ${DAYS_OF_WAKE_TYPE_VALUES.join(", ")}`);
|
|
187
|
+
}
|
|
188
|
+
intentParam.daysOfWakeType = params.daysOfWakeType;
|
|
189
|
+
logger.log(`[MODIFY_ALARM_TOOL] - daysOfWakeType: ${params.daysOfWakeType}`);
|
|
190
|
+
}
|
|
191
|
+
// Add daysOfWeek if provided - only valid when daysOfWakeType is 3
|
|
192
|
+
if (params.daysOfWeek !== undefined && params.daysOfWeek !== null) {
|
|
193
|
+
// Check if daysOfWakeType is 3 or will be set to 3
|
|
194
|
+
const targetDaysOfWakeType = params.daysOfWakeType !== undefined ? params.daysOfWakeType : null;
|
|
195
|
+
if (targetDaysOfWakeType !== null && targetDaysOfWakeType !== 3) {
|
|
196
|
+
logger.warn(`[MODIFY_ALARM_TOOL] ⚠️ daysOfWeek parameter is ignored when daysOfWakeType is not 3 (current: ${targetDaysOfWakeType}). Please remove daysOfWeek parameter.`);
|
|
197
|
+
// Skip processing daysOfWeek when daysOfWakeType is not 3
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
// ===== 参数规范化:兼容数组和 JSON 字符串 =====
|
|
201
|
+
let normalizedDaysOfWeek = null;
|
|
202
|
+
// 情况1: 已经是数组
|
|
203
|
+
if (Array.isArray(params.daysOfWeek)) {
|
|
204
|
+
logger.log(`[MODIFY_ALARM_TOOL] ✅ daysOfWeek is already an array`);
|
|
205
|
+
normalizedDaysOfWeek = params.daysOfWeek;
|
|
206
|
+
}
|
|
207
|
+
// 情况2: 是字符串,尝试解析为 JSON 数组
|
|
208
|
+
else if (typeof params.daysOfWeek === 'string') {
|
|
209
|
+
logger.log(`[MODIFY_ALARM_TOOL] 🔄 daysOfWeek is a string, attempting to parse as JSON...`);
|
|
210
|
+
try {
|
|
211
|
+
const parsed = JSON.parse(params.daysOfWeek);
|
|
212
|
+
if (Array.isArray(parsed)) {
|
|
213
|
+
logger.log(`[MODIFY_ALARM_TOOL] ✅ Successfully parsed JSON string to array`);
|
|
214
|
+
normalizedDaysOfWeek = parsed;
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Parsed JSON is not an array:`, typeof parsed);
|
|
218
|
+
throw new Error("daysOfWeek must be an array or a JSON string representing an array");
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
catch (parseError) {
|
|
222
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Failed to parse daysOfWeek as JSON:`, parseError);
|
|
223
|
+
throw new Error(`daysOfWeek must be a valid JSON array string. Parse error: ${parseError instanceof Error ? parseError.message : String(parseError)}`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// 情况3: 其他类型,报错
|
|
227
|
+
else {
|
|
228
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid daysOfWeek type:`, typeof params.daysOfWeek);
|
|
229
|
+
throw new Error(`daysOfWeek must be an array or a JSON string, got ${typeof params.daysOfWeek}`);
|
|
230
|
+
}
|
|
231
|
+
// 验证数组非空
|
|
232
|
+
if (!normalizedDaysOfWeek || normalizedDaysOfWeek.length === 0) {
|
|
233
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ daysOfWeek array is empty`);
|
|
234
|
+
throw new Error("daysOfWeek array cannot be empty");
|
|
235
|
+
}
|
|
236
|
+
// 验证数组长度必须为1
|
|
237
|
+
if (normalizedDaysOfWeek.length !== 1) {
|
|
238
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ daysOfWeek array length must be 1, got ${normalizedDaysOfWeek.length}`);
|
|
239
|
+
throw new Error("daysOfWeek 仅支持长度为1的数组。如果需要一周中不同的几天,需要多次调用此工具");
|
|
240
|
+
}
|
|
241
|
+
// Validate each day
|
|
242
|
+
for (const day of normalizedDaysOfWeek) {
|
|
243
|
+
if (typeof day !== "string" || !DAYS_OF_WEEK_VALUES.includes(day)) {
|
|
244
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid day value: ${day}`);
|
|
245
|
+
throw new Error(`daysOfWeek must contain only: ${DAYS_OF_WEEK_VALUES.join(", ")}`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
intentParam.daysOfWeek = normalizedDaysOfWeek;
|
|
249
|
+
logger.log(`[MODIFY_ALARM_TOOL] - daysOfWeek: ${normalizedDaysOfWeek.join(", ")}`);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
// Get session context
|
|
253
|
+
logger.log(`[MODIFY_ALARM_TOOL] 🔍 Attempting to get session context...`);
|
|
254
|
+
const sessionContext = getCurrentSessionContext();
|
|
255
|
+
if (!sessionContext) {
|
|
256
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ FAILED: No active session found!`);
|
|
257
|
+
logger.error(`[MODIFY_ALARM_TOOL] - toolCallId: ${toolCallId}`);
|
|
258
|
+
throw new Error("No active XY session found. Modify alarm tool can only be used during an active conversation.");
|
|
259
|
+
}
|
|
260
|
+
logger.log(`[MODIFY_ALARM_TOOL] ✅ Session context found`);
|
|
261
|
+
logger.log(`[MODIFY_ALARM_TOOL] - sessionId: ${sessionContext.sessionId}`);
|
|
262
|
+
logger.log(`[MODIFY_ALARM_TOOL] - taskId: ${sessionContext.taskId}`);
|
|
263
|
+
logger.log(`[MODIFY_ALARM_TOOL] - messageId: ${sessionContext.messageId}`);
|
|
264
|
+
const { config, sessionId, taskId, messageId } = sessionContext;
|
|
265
|
+
// Get WebSocket manager
|
|
266
|
+
logger.log(`[MODIFY_ALARM_TOOL] 🔌 Getting WebSocket manager...`);
|
|
267
|
+
const wsManager = getXYWebSocketManager(config);
|
|
268
|
+
logger.log(`[MODIFY_ALARM_TOOL] ✅ WebSocket manager obtained`);
|
|
269
|
+
// Build ModifyAlarm command
|
|
270
|
+
logger.log(`[MODIFY_ALARM_TOOL] 📦 Building ModifyAlarm command...`);
|
|
271
|
+
const command = {
|
|
272
|
+
header: {
|
|
273
|
+
namespace: "Common",
|
|
274
|
+
name: "Action",
|
|
275
|
+
},
|
|
276
|
+
payload: {
|
|
277
|
+
cardParam: {},
|
|
278
|
+
executeParam: {
|
|
279
|
+
executeMode: "background",
|
|
280
|
+
intentName: "ModifyAlarm",
|
|
281
|
+
bundleName: "com.huawei.hmos.clock",
|
|
282
|
+
needUnlock: true,
|
|
283
|
+
actionResponse: true,
|
|
284
|
+
appType: "OHOS_APP",
|
|
285
|
+
timeOut: 5,
|
|
286
|
+
intentParam: intentParam,
|
|
287
|
+
permissionId: [],
|
|
288
|
+
achieveType: "INTENT",
|
|
289
|
+
},
|
|
290
|
+
responses: [
|
|
291
|
+
{
|
|
292
|
+
resultCode: "",
|
|
293
|
+
displayText: "",
|
|
294
|
+
ttsText: "",
|
|
295
|
+
},
|
|
296
|
+
],
|
|
297
|
+
needUploadResult: true,
|
|
298
|
+
noHalfPage: false,
|
|
299
|
+
pageControlRelated: false,
|
|
300
|
+
},
|
|
301
|
+
};
|
|
302
|
+
// Send command and wait for response (60 second timeout)
|
|
303
|
+
logger.log(`[MODIFY_ALARM_TOOL] ⏳ Setting up promise to wait for alarm modification response...`);
|
|
304
|
+
logger.log(`[MODIFY_ALARM_TOOL] - Timeout: 60 seconds`);
|
|
305
|
+
return new Promise((resolve, reject) => {
|
|
306
|
+
const timeout = setTimeout(() => {
|
|
307
|
+
logger.error(`[MODIFY_ALARM_TOOL] ⏰ Timeout: No response received within 60 seconds`);
|
|
308
|
+
wsManager.off("data-event", handler);
|
|
309
|
+
reject(new Error("修改闹钟超时(60秒)"));
|
|
310
|
+
}, 60000);
|
|
311
|
+
// Listen for data events from WebSocket
|
|
312
|
+
const handler = (event) => {
|
|
313
|
+
logger.log(`[MODIFY_ALARM_TOOL] 📨 Received data event:`, JSON.stringify(event));
|
|
314
|
+
if (event.intentName === "ModifyAlarm") {
|
|
315
|
+
logger.log(`[MODIFY_ALARM_TOOL] 🎯 ModifyAlarm event received`);
|
|
316
|
+
logger.log(`[MODIFY_ALARM_TOOL] - status: ${event.status}`);
|
|
317
|
+
clearTimeout(timeout);
|
|
318
|
+
wsManager.off("data-event", handler);
|
|
319
|
+
if (event.status === "success" && event.outputs) {
|
|
320
|
+
logger.log(`[MODIFY_ALARM_TOOL] ✅ Alarm modification completed successfully`);
|
|
321
|
+
logger.log(`[MODIFY_ALARM_TOOL] - outputs:`, JSON.stringify(event.outputs));
|
|
322
|
+
// 成功,直接返回完整的 event.outputs JSON 字符串
|
|
323
|
+
resolve({
|
|
324
|
+
content: [
|
|
325
|
+
{
|
|
326
|
+
type: "text",
|
|
327
|
+
text: JSON.stringify(event.outputs),
|
|
328
|
+
},
|
|
329
|
+
],
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Alarm modification failed`);
|
|
334
|
+
logger.error(`[MODIFY_ALARM_TOOL] - status: ${event.status}`);
|
|
335
|
+
logger.error(`[MODIFY_ALARM_TOOL] - outputs:`, JSON.stringify(event.outputs || {}));
|
|
336
|
+
reject(new Error(`修改闹钟失败: ${event.status}`));
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
// Register event handler
|
|
341
|
+
logger.log(`[MODIFY_ALARM_TOOL] 📡 Registering data-event handler on WebSocket manager`);
|
|
342
|
+
wsManager.on("data-event", handler);
|
|
343
|
+
// Send the command
|
|
344
|
+
logger.log(`[MODIFY_ALARM_TOOL] 📤 Sending ModifyAlarm command...`);
|
|
345
|
+
sendCommand({
|
|
346
|
+
config,
|
|
347
|
+
sessionId,
|
|
348
|
+
taskId,
|
|
349
|
+
messageId,
|
|
350
|
+
command,
|
|
351
|
+
})
|
|
352
|
+
.then(() => {
|
|
353
|
+
logger.log(`[MODIFY_ALARM_TOOL] ✅ Command sent successfully, waiting for response...`);
|
|
354
|
+
})
|
|
355
|
+
.catch((error) => {
|
|
356
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Failed to send command:`, error);
|
|
357
|
+
clearTimeout(timeout);
|
|
358
|
+
wsManager.off("data-event", handler);
|
|
359
|
+
reject(error);
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
},
|
|
363
|
+
};
|
|
364
|
+
// Enum for alarm state
|
|
365
|
+
const ALARM_STATE_VALUES = [0, 1];
|
|
366
|
+
/**
|
|
367
|
+
* Parse alarmTime string (YYYYMMDD hhmmss) to timestamp in milliseconds
|
|
368
|
+
* @param alarmTime - Time string in format "YYYYMMDD hhmmss" (e.g., "20240315 143000")
|
|
369
|
+
* @returns Timestamp in milliseconds, or null if parsing fails
|
|
370
|
+
*/
|
|
371
|
+
function parseAlarmTimeToTimestamp(alarmTime) {
|
|
372
|
+
try {
|
|
373
|
+
// Expected format: YYYYMMDD hhmmss
|
|
374
|
+
// Example: 20240315 143000
|
|
375
|
+
const trimmed = alarmTime.trim();
|
|
376
|
+
// Check basic format (should have at least 13 characters: YYYYMMDD hhmmss)
|
|
377
|
+
if (trimmed.length < 13) {
|
|
378
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ alarmTime too short: ${trimmed}`);
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
// Extract date and time parts
|
|
382
|
+
// Format: YYYYMMDD hhmmss
|
|
383
|
+
const datePart = trimmed.substring(0, 8); // YYYYMMDD
|
|
384
|
+
const timePart = trimmed.substring(8).trim(); // hhmmss (may have leading space)
|
|
385
|
+
logger.log(`[MODIFY_ALARM_TOOL] - datePart: ${datePart}, timePart: ${timePart}`);
|
|
386
|
+
// Validate lengths
|
|
387
|
+
if (datePart.length !== 8 || timePart.length !== 6) {
|
|
388
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid part lengths: datePart=${datePart.length}, timePart=${timePart.length}`);
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
// Parse components
|
|
392
|
+
const year = parseInt(datePart.substring(0, 4), 10);
|
|
393
|
+
const month = parseInt(datePart.substring(4, 6), 10);
|
|
394
|
+
const day = parseInt(datePart.substring(6, 8), 10);
|
|
395
|
+
const hour = parseInt(timePart.substring(0, 2), 10);
|
|
396
|
+
const minute = parseInt(timePart.substring(2, 4), 10);
|
|
397
|
+
const second = parseInt(timePart.substring(4, 6), 10);
|
|
398
|
+
logger.log(`[MODIFY_ALARM_TOOL] - Parsed: ${year}-${month}-${day} ${hour}:${minute}:${second}`);
|
|
399
|
+
// Validate values
|
|
400
|
+
if (isNaN(year) || isNaN(month) || isNaN(day) ||
|
|
401
|
+
isNaN(hour) || isNaN(minute) || isNaN(second)) {
|
|
402
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ NaN detected in parsed values`);
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
// Validate ranges
|
|
406
|
+
if (month < 1 || month > 12) {
|
|
407
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid month: ${month}`);
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
if (day < 1 || day > 31) {
|
|
411
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid day: ${day}`);
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
if (hour < 0 || hour > 23) {
|
|
415
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid hour: ${hour}`);
|
|
416
|
+
return null;
|
|
417
|
+
}
|
|
418
|
+
if (minute < 0 || minute > 59) {
|
|
419
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid minute: ${minute}`);
|
|
420
|
+
return null;
|
|
421
|
+
}
|
|
422
|
+
if (second < 0 || second > 59) {
|
|
423
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Invalid second: ${second}`);
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
// Create Date object and get timestamp
|
|
427
|
+
const date = new Date(year, month - 1, day, hour, minute, second);
|
|
428
|
+
const timestamp = date.getTime();
|
|
429
|
+
if (isNaN(timestamp)) {
|
|
430
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Generated timestamp is NaN`);
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
return timestamp;
|
|
434
|
+
}
|
|
435
|
+
catch (error) {
|
|
436
|
+
logger.error(`[MODIFY_ALARM_TOOL] ❌ Exception in parseAlarmTimeToTimestamp:`, error);
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getXYWebSocketManager } from "../client.js";
|
|
2
2
|
import { sendCommand } from "../formatter.js";
|
|
3
|
-
import {
|
|
3
|
+
import { getCurrentSessionContext } from "./session-manager.js";
|
|
4
4
|
import { logger } from "../utils/logger.js";
|
|
5
5
|
/**
|
|
6
6
|
* XY modify note tool - appends content to an existing note on user's device.
|
|
@@ -40,7 +40,7 @@ export const modifyNoteTool = {
|
|
|
40
40
|
}
|
|
41
41
|
// Get session context
|
|
42
42
|
logger.log(`[MODIFY_NOTE_TOOL] 🔍 Attempting to get session context...`);
|
|
43
|
-
const sessionContext =
|
|
43
|
+
const sessionContext = getCurrentSessionContext();
|
|
44
44
|
if (!sessionContext) {
|
|
45
45
|
logger.error(`[MODIFY_NOTE_TOOL] ❌ FAILED: No active session found!`);
|
|
46
46
|
logger.error(`[MODIFY_NOTE_TOOL] - toolCallId: ${toolCallId}`);
|
|
@@ -115,17 +115,12 @@ export const modifyNoteTool = {
|
|
|
115
115
|
if (event.status === "success" && event.outputs) {
|
|
116
116
|
logger.log(`[MODIFY_NOTE_TOOL] ✅ Note modified successfully`);
|
|
117
117
|
logger.log(`[MODIFY_NOTE_TOOL] - outputs:`, JSON.stringify(event.outputs));
|
|
118
|
-
//
|
|
119
|
-
const result = event.outputs.result;
|
|
120
|
-
logger.log(`[MODIFY_NOTE_TOOL] 📝 Note updated:`);
|
|
121
|
-
logger.log(`[MODIFY_NOTE_TOOL] - entityId: ${result?.entityId}`);
|
|
122
|
-
logger.log(`[MODIFY_NOTE_TOOL] - title: ${result?.title}`);
|
|
123
|
-
logger.log(`[MODIFY_NOTE_TOOL] - modifiedDate: ${result?.modifiedDate}`);
|
|
118
|
+
// 成功,直接返回完整的 event.outputs JSON 字符串
|
|
124
119
|
resolve({
|
|
125
120
|
content: [
|
|
126
121
|
{
|
|
127
122
|
type: "text",
|
|
128
|
-
text: JSON.stringify(
|
|
123
|
+
text: JSON.stringify(event.outputs),
|
|
129
124
|
},
|
|
130
125
|
],
|
|
131
126
|
});
|
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
import { getXYWebSocketManager } from "../client.js";
|
|
2
2
|
import { sendCommand } from "../formatter.js";
|
|
3
|
-
import {
|
|
3
|
+
import { getCurrentSessionContext } from "./session-manager.js";
|
|
4
4
|
import { logger } from "../utils/logger.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
|
+
}
|
|
5
17
|
/**
|
|
6
18
|
* XY note tool - creates a note on user's device.
|
|
7
19
|
* Requires title and content parameters.
|
|
@@ -9,29 +21,38 @@ import { logger } from "../utils/logger.js";
|
|
|
9
21
|
export const noteTool = {
|
|
10
22
|
name: "create_note",
|
|
11
23
|
label: "Create Note",
|
|
12
|
-
description:
|
|
24
|
+
description: `在用户设备上创建备忘录。需要提供备忘录标题和内容。
|
|
25
|
+
注意:
|
|
26
|
+
a. 操作超时时间为60秒,请勿重复调用此工具
|
|
27
|
+
b. 如果遇到各类调用失败场景,最多只能重试一次,不可以重复调用多次。
|
|
28
|
+
c. 调用工具前需认真检查调用参数是否满足工具要求
|
|
29
|
+
`,
|
|
13
30
|
parameters: {
|
|
14
31
|
type: "object",
|
|
15
32
|
properties: {
|
|
16
33
|
title: {
|
|
17
34
|
type: "string",
|
|
18
|
-
description: "
|
|
35
|
+
description: "备忘录标题,必填",
|
|
19
36
|
},
|
|
20
37
|
content: {
|
|
21
38
|
type: "string",
|
|
22
|
-
description: "
|
|
39
|
+
description: "备忘录内容,必填",
|
|
23
40
|
},
|
|
24
41
|
},
|
|
25
42
|
required: ["title", "content"],
|
|
26
43
|
},
|
|
27
44
|
async execute(toolCallId, params) {
|
|
28
45
|
logger.debug("Executing note tool, toolCallId:", toolCallId);
|
|
29
|
-
// Validate parameters
|
|
30
|
-
|
|
31
|
-
|
|
46
|
+
// Validate parameters — 抛 ToolInputError 而非普通 Error,
|
|
47
|
+
// 让 openclaw 返回 400 而非 500,明确告知 LLM 这是参数错误,不应重试。
|
|
48
|
+
if (typeof params.title !== "string" || !params.title) {
|
|
49
|
+
throw new ToolInputError("缺少必填参数 title(备忘录标题)");
|
|
50
|
+
}
|
|
51
|
+
if (typeof params.content !== "string" || !params.content) {
|
|
52
|
+
throw new ToolInputError("缺少必填参数 content(备忘录内容)");
|
|
32
53
|
}
|
|
33
54
|
// Get session context
|
|
34
|
-
const sessionContext =
|
|
55
|
+
const sessionContext = getCurrentSessionContext();
|
|
35
56
|
if (!sessionContext) {
|
|
36
57
|
throw new Error("No active XY session found. Note tool can only be used during an active conversation.");
|
|
37
58
|
}
|
|
@@ -85,23 +106,13 @@ export const noteTool = {
|
|
|
85
106
|
clearTimeout(timeout);
|
|
86
107
|
wsManager.off("data-event", handler);
|
|
87
108
|
if (event.status === "success" && event.outputs) {
|
|
88
|
-
|
|
89
|
-
|
|
109
|
+
logger.log(`Note created successfully, outputs:`, JSON.stringify(event.outputs));
|
|
110
|
+
// 成功,直接返回完整的 event.outputs JSON 字符串
|
|
90
111
|
resolve({
|
|
91
112
|
content: [
|
|
92
113
|
{
|
|
93
114
|
type: "text",
|
|
94
|
-
text: JSON.stringify(
|
|
95
|
-
success: true,
|
|
96
|
-
note: {
|
|
97
|
-
entityId: result?.entityId,
|
|
98
|
-
title: result?.title,
|
|
99
|
-
content: result?.content,
|
|
100
|
-
entityName: result?.entityName,
|
|
101
|
-
modifiedDate: result?.modifiedDate,
|
|
102
|
-
},
|
|
103
|
-
code,
|
|
104
|
-
}),
|
|
115
|
+
text: JSON.stringify(event.outputs),
|
|
105
116
|
},
|
|
106
117
|
],
|
|
107
118
|
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* XY search alarm tool - searches alarms on user's device.
|
|
3
|
+
* Returns matching alarms based on various filter criteria.
|
|
4
|
+
*
|
|
5
|
+
* At least one search criterion must be provided.
|
|
6
|
+
* Multiple criteria can be combined.
|
|
7
|
+
*/
|
|
8
|
+
export declare const searchAlarmTool: any;
|