@ynhcj/xiaoyi-channel 0.0.26-beta → 0.0.27-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/dist/src/bot.js +13 -3
  2. package/dist/src/channel.js +10 -1
  3. package/dist/src/tools/calendar-tool.js +2 -2
  4. package/dist/src/tools/call-phone-tool.d.ts +5 -0
  5. package/dist/src/tools/call-phone-tool.js +183 -0
  6. package/dist/src/tools/create-alarm-tool.d.ts +7 -0
  7. package/dist/src/tools/create-alarm-tool.js +395 -0
  8. package/dist/src/tools/delete-alarm-tool.d.ts +11 -0
  9. package/dist/src/tools/delete-alarm-tool.js +238 -0
  10. package/dist/src/tools/location-tool.js +2 -2
  11. package/dist/src/tools/modify-alarm-tool.d.ts +9 -0
  12. package/dist/src/tools/modify-alarm-tool.js +433 -0
  13. package/dist/src/tools/modify-note-tool.js +2 -2
  14. package/dist/src/tools/note-tool.js +2 -2
  15. package/dist/src/tools/search-alarm-tool.d.ts +8 -0
  16. package/dist/src/tools/search-alarm-tool.js +389 -0
  17. package/dist/src/tools/search-calendar-tool.js +2 -2
  18. package/dist/src/tools/search-contact-tool.js +2 -2
  19. package/dist/src/tools/search-file-tool.d.ts +5 -0
  20. package/dist/src/tools/search-file-tool.js +173 -0
  21. package/dist/src/tools/search-message-tool.d.ts +5 -0
  22. package/dist/src/tools/search-message-tool.js +173 -0
  23. package/dist/src/tools/search-note-tool.js +2 -2
  24. package/dist/src/tools/search-photo-gallery-tool.js +2 -2
  25. package/dist/src/tools/send-message-tool.d.ts +5 -0
  26. package/dist/src/tools/send-message-tool.js +189 -0
  27. package/dist/src/tools/session-manager.d.ts +12 -0
  28. package/dist/src/tools/session-manager.js +33 -0
  29. package/dist/src/tools/upload-file-tool.d.ts +13 -0
  30. package/dist/src/tools/upload-file-tool.js +265 -0
  31. package/dist/src/tools/upload-photo-tool.js +2 -2
  32. package/dist/src/tools/xiaoyi-gui-tool.js +2 -2
  33. package/package.json +1 -1
@@ -0,0 +1,395 @@
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
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 create alarm tool - creates an alarm on user's device.
13
+ * Requires alarmTime parameter. Other parameters are optional with sensible defaults.
14
+ *
15
+ * Time format: YYYYMMDD hhmmss (e.g., 20240315 143000)
16
+ */
17
+ export const createAlarmTool = {
18
+ name: "create_alarm",
19
+ label: "Create Alarm",
20
+ description: `在用户设备上创建闹钟。
21
+
22
+ 必需参数:
23
+ - alarmTime: 闹钟时间,格式必须为:YYYYMMDD hhmmss(例如:20240315 143000,表示2024年3月15日14:30:00)
24
+
25
+ 可选参数:
26
+ - alarmTitle: 闹钟名称/标题,默认为"闹钟"
27
+ - alarmSnoozeDuration: 小睡间隔(分钟),枚举值:5,10,15,20,25,30,默认10
28
+ - alarmSnoozeTotal: 再响次数,枚举值:0,1,3,5,10,默认0(表示不再响)
29
+ - alarmRingDuration: 响铃时长(分钟),枚举值:1,5,10,15,20,30,默认20
30
+ - daysOfWakeType: 闹钟响铃类型,枚举值:0=单次响铃,1=法定节假日,2=每天,3=自定义时间,4=法定工作日,默认0
31
+ - daysOfWeek: 自定义响铃星期,当daysOfWakeType=3时需要,数组,枚举值:Mon,Tue,Wed,Thu,Fri,Sat,Sun
32
+
33
+ 注意事项:操作超时时间为60秒,请勿重复调用此工具,如果超时或失败,最多重试一次。`,
34
+ parameters: {
35
+ type: "object",
36
+ properties: {
37
+ alarmTime: {
38
+ type: "string",
39
+ description: "闹钟时间,格式必须为:YYYYMMDD hhmmss(例如:20240315 143000)",
40
+ },
41
+ alarmTitle: {
42
+ type: "string",
43
+ description: "闹钟名称/标题,默认为'闹钟'",
44
+ },
45
+ alarmSnoozeDuration: {
46
+ type: "number",
47
+ description: "小睡间隔(分钟),枚举值:5,10,15,20,25,30,默认10",
48
+ },
49
+ alarmSnoozeTotal: {
50
+ type: "number",
51
+ description: "再响次数,枚举值:0,1,3,5,10,默认0",
52
+ },
53
+ alarmRingDuration: {
54
+ type: "number",
55
+ description: "响铃时长(分钟),枚举值:1,5,10,15,20,30,默认20",
56
+ },
57
+ daysOfWakeType: {
58
+ type: "number",
59
+ description: "闹钟响铃类型:0=单次,1=法定节假日,2=每天,3=自定义,4=法定工作日,默认0",
60
+ },
61
+ daysOfWeek: {
62
+ type: "array",
63
+ items: {
64
+ type: "string",
65
+ },
66
+ description: "自定义响铃星期(当daysOfWakeType=3时需要),枚举值:Mon,Tue,Wed,Thu,Fri,Sat,Sun",
67
+ },
68
+ },
69
+ required: ["alarmTime"],
70
+ },
71
+ async execute(toolCallId, params) {
72
+ logger.log(`[CREATE_ALARM_TOOL] 🚀 Starting execution`);
73
+ logger.log(`[CREATE_ALARM_TOOL] - toolCallId: ${toolCallId}`);
74
+ logger.log(`[CREATE_ALARM_TOOL] - params:`, JSON.stringify(params));
75
+ logger.log(`[CREATE_ALARM_TOOL] - timestamp: ${new Date().toISOString()}`);
76
+ // ===== Validate required parameter: alarmTime =====
77
+ if (!params.alarmTime || typeof params.alarmTime !== "string") {
78
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Missing or invalid alarmTime`);
79
+ throw new Error("Missing required parameter: alarmTime must be a string in format YYYYMMDD hhmmss");
80
+ }
81
+ // Parse and convert alarmTime to timestamp
82
+ logger.log(`[CREATE_ALARM_TOOL] 🕒 Parsing alarmTime: ${params.alarmTime}`);
83
+ const alarmTimeMs = parseAlarmTimeToTimestamp(params.alarmTime);
84
+ if (alarmTimeMs === null) {
85
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid alarmTime format`);
86
+ throw new Error("Invalid alarmTime format. Required format: YYYYMMDD hhmmss (e.g., 20240315 143000)");
87
+ }
88
+ logger.log(`[CREATE_ALARM_TOOL] ✅ alarmTime converted to timestamp: ${alarmTimeMs}`);
89
+ // ===== Validate and set optional parameters with defaults =====
90
+ // alarmTitle - default to "闹钟"
91
+ const alarmTitle = params.alarmTitle && typeof params.alarmTitle === "string"
92
+ ? params.alarmTitle
93
+ : "闹钟";
94
+ logger.log(`[CREATE_ALARM_TOOL] - alarmTitle: ${alarmTitle}`);
95
+ // alarmSnoozeDuration - default 10
96
+ let alarmSnoozeDuration = 10;
97
+ if (params.alarmSnoozeDuration !== undefined && params.alarmSnoozeDuration !== null) {
98
+ if (typeof params.alarmSnoozeDuration !== "number") {
99
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid alarmSnoozeDuration type`);
100
+ throw new Error("alarmSnoozeDuration must be a number");
101
+ }
102
+ if (!ALARM_SNOOZE_DURATION_VALUES.includes(params.alarmSnoozeDuration)) {
103
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid alarmSnoozeDuration value: ${params.alarmSnoozeDuration}`);
104
+ throw new Error(`alarmSnoozeDuration must be one of: ${ALARM_SNOOZE_DURATION_VALUES.join(", ")}`);
105
+ }
106
+ alarmSnoozeDuration = params.alarmSnoozeDuration;
107
+ }
108
+ logger.log(`[CREATE_ALARM_TOOL] - alarmSnoozeDuration: ${alarmSnoozeDuration}`);
109
+ // alarmSnoozeTotal - default 0
110
+ let alarmSnoozeTotal = 0;
111
+ if (params.alarmSnoozeTotal !== undefined && params.alarmSnoozeTotal !== null) {
112
+ if (typeof params.alarmSnoozeTotal !== "number") {
113
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid alarmSnoozeTotal type`);
114
+ throw new Error("alarmSnoozeTotal must be a number");
115
+ }
116
+ if (!ALARM_SNOOZE_TOTAL_VALUES.includes(params.alarmSnoozeTotal)) {
117
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid alarmSnoozeTotal value: ${params.alarmSnoozeTotal}`);
118
+ throw new Error(`alarmSnoozeTotal must be one of: ${ALARM_SNOOZE_TOTAL_VALUES.join(", ")}`);
119
+ }
120
+ alarmSnoozeTotal = params.alarmSnoozeTotal;
121
+ }
122
+ logger.log(`[CREATE_ALARM_TOOL] - alarmSnoozeTotal: ${alarmSnoozeTotal}`);
123
+ // alarmRingDuration - default 20
124
+ let alarmRingDuration = 20;
125
+ if (params.alarmRingDuration !== undefined && params.alarmRingDuration !== null) {
126
+ if (typeof params.alarmRingDuration !== "number") {
127
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid alarmRingDuration type`);
128
+ throw new Error("alarmRingDuration must be a number");
129
+ }
130
+ if (!ALARM_RING_DURATION_VALUES.includes(params.alarmRingDuration)) {
131
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid alarmRingDuration value: ${params.alarmRingDuration}`);
132
+ throw new Error(`alarmRingDuration must be one of: ${ALARM_RING_DURATION_VALUES.join(", ")}`);
133
+ }
134
+ alarmRingDuration = params.alarmRingDuration;
135
+ }
136
+ logger.log(`[CREATE_ALARM_TOOL] - alarmRingDuration: ${alarmRingDuration}`);
137
+ // daysOfWakeType - default 0
138
+ let daysOfWakeType = 0;
139
+ if (params.daysOfWakeType !== undefined && params.daysOfWakeType !== null) {
140
+ if (typeof params.daysOfWakeType !== "number") {
141
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid daysOfWakeType type`);
142
+ throw new Error("daysOfWakeType must be a number");
143
+ }
144
+ if (!DAYS_OF_WAKE_TYPE_VALUES.includes(params.daysOfWakeType)) {
145
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid daysOfWakeType value: ${params.daysOfWakeType}`);
146
+ throw new Error(`daysOfWakeType must be one of: ${DAYS_OF_WAKE_TYPE_VALUES.join(", ")}`);
147
+ }
148
+ daysOfWakeType = params.daysOfWakeType;
149
+ }
150
+ logger.log(`[CREATE_ALARM_TOOL] - daysOfWakeType: ${daysOfWakeType}`);
151
+ // daysOfWeek - only required when daysOfWakeType is 3
152
+ let daysOfWeek = [];
153
+ if (daysOfWakeType === 3) {
154
+ if (!params.daysOfWeek || !Array.isArray(params.daysOfWeek)) {
155
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Missing daysOfWeek when daysOfWakeType=3`);
156
+ throw new Error("daysOfWeek is required when daysOfWakeType is 3 (custom)");
157
+ }
158
+ if (!Array.isArray(params.daysOfWeek)) {
159
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid daysOfWeek type`);
160
+ throw new Error("daysOfWeek must be an array");
161
+ }
162
+ // Validate each day
163
+ for (const day of params.daysOfWeek) {
164
+ if (typeof day !== "string" || !DAYS_OF_WEEK_VALUES.includes(day)) {
165
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid day value: ${day}`);
166
+ throw new Error(`daysOfWeek must contain only: ${DAYS_OF_WEEK_VALUES.join(", ")}`);
167
+ }
168
+ }
169
+ daysOfWeek = params.daysOfWeek;
170
+ logger.log(`[CREATE_ALARM_TOOL] - daysOfWeek: ${daysOfWeek.join(", ")}`);
171
+ }
172
+ else if (params.daysOfWeek && params.daysOfWeek.length > 0) {
173
+ logger.warn(`[CREATE_ALARM_TOOL] ⚠️ daysOfWeek is ignored when daysOfWakeType is not 3`);
174
+ }
175
+ // Get session context
176
+ logger.log(`[CREATE_ALARM_TOOL] 🔍 Attempting to get session context...`);
177
+ const sessionContext = getCurrentSessionContext();
178
+ if (!sessionContext) {
179
+ logger.error(`[CREATE_ALARM_TOOL] ❌ FAILED: No active session found!`);
180
+ logger.error(`[CREATE_ALARM_TOOL] - toolCallId: ${toolCallId}`);
181
+ throw new Error("No active XY session found. Create alarm tool can only be used during an active conversation.");
182
+ }
183
+ logger.log(`[CREATE_ALARM_TOOL] ✅ Session context found`);
184
+ logger.log(`[CREATE_ALARM_TOOL] - sessionId: ${sessionContext.sessionId}`);
185
+ logger.log(`[CREATE_ALARM_TOOL] - taskId: ${sessionContext.taskId}`);
186
+ logger.log(`[CREATE_ALARM_TOOL] - messageId: ${sessionContext.messageId}`);
187
+ const { config, sessionId, taskId, messageId } = sessionContext;
188
+ // Get WebSocket manager
189
+ logger.log(`[CREATE_ALARM_TOOL] 🔌 Getting WebSocket manager...`);
190
+ const wsManager = getXYWebSocketManager(config);
191
+ logger.log(`[CREATE_ALARM_TOOL] ✅ WebSocket manager obtained`);
192
+ // Build CreateAlarm command
193
+ logger.log(`[CREATE_ALARM_TOOL] 📦 Building CreateAlarm command...`);
194
+ const command = {
195
+ header: {
196
+ namespace: "Common",
197
+ name: "Action",
198
+ },
199
+ payload: {
200
+ cardParam: {},
201
+ executeParam: {
202
+ executeMode: "background",
203
+ intentName: "CreateAlarm",
204
+ bundleName: "com.huawei.hmos.clock",
205
+ needUnlock: true,
206
+ actionResponse: true,
207
+ appType: "OHOS_APP",
208
+ timeOut: 5,
209
+ intentParam: {
210
+ entityName: "Alarm",
211
+ alarmTime: alarmTimeMs,
212
+ alarmTitle: alarmTitle,
213
+ alarmSnoozeDuration: alarmSnoozeDuration,
214
+ alarmSnoozeTotal: alarmSnoozeTotal,
215
+ alarmRingDuration: alarmRingDuration,
216
+ daysOfWakeType: daysOfWakeType,
217
+ daysOfWeek: daysOfWeek,
218
+ },
219
+ permissionId: [],
220
+ achieveType: "INTENT",
221
+ },
222
+ responses: [
223
+ {
224
+ resultCode: "",
225
+ displayText: "",
226
+ ttsText: "",
227
+ },
228
+ ],
229
+ needUploadResult: true,
230
+ noHalfPage: false,
231
+ pageControlRelated: false,
232
+ },
233
+ };
234
+ // Send command and wait for response (60 second timeout)
235
+ logger.log(`[CREATE_ALARM_TOOL] ⏳ Setting up promise to wait for alarm creation response...`);
236
+ logger.log(`[CREATE_ALARM_TOOL] - Timeout: 60 seconds`);
237
+ return new Promise((resolve, reject) => {
238
+ const timeout = setTimeout(() => {
239
+ logger.error(`[CREATE_ALARM_TOOL] ⏰ Timeout: No response received within 60 seconds`);
240
+ wsManager.off("data-event", handler);
241
+ reject(new Error("创建闹钟超时(60秒)"));
242
+ }, 60000);
243
+ // Listen for data events from WebSocket
244
+ const handler = (event) => {
245
+ logger.log(`[CREATE_ALARM_TOOL] 📨 Received data event:`, JSON.stringify(event));
246
+ if (event.intentName === "CreateAlarm") {
247
+ logger.log(`[CREATE_ALARM_TOOL] 🎯 CreateAlarm event received`);
248
+ logger.log(`[CREATE_ALARM_TOOL] - status: ${event.status}`);
249
+ clearTimeout(timeout);
250
+ wsManager.off("data-event", handler);
251
+ if (event.status === "success" && event.outputs) {
252
+ logger.log(`[CREATE_ALARM_TOOL] ✅ Alarm creation completed successfully`);
253
+ logger.log(`[CREATE_ALARM_TOOL] - outputs:`, JSON.stringify(event.outputs));
254
+ // Check for error code in outputs
255
+ const code = event.outputs.code !== undefined ? event.outputs.code : null;
256
+ if (code !== null && code !== 0) {
257
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Device returned error`);
258
+ logger.error(`[CREATE_ALARM_TOOL] - code: ${code}`);
259
+ const errorMsg = event.outputs.errorMsg || event.outputs.errMsg || "未知错误";
260
+ logger.error(`[CREATE_ALARM_TOOL] - errorMsg: ${errorMsg}`);
261
+ reject(new Error(`创建闹钟失败: ${errorMsg} (错误代码: ${code})`));
262
+ return;
263
+ }
264
+ // Extract result with safe navigation
265
+ const result = event.outputs.result || {};
266
+ logger.log(`[CREATE_ALARM_TOOL] 📋 Alarm result:`, JSON.stringify(result));
267
+ resolve({
268
+ content: [
269
+ {
270
+ type: "text",
271
+ text: JSON.stringify({
272
+ success: true,
273
+ alarm: {
274
+ entityId: result.entityId,
275
+ entityName: result.entityName,
276
+ alarmTitle: result.alarmTitle,
277
+ alarmTime: result.alarmTime,
278
+ alarmState: result.alarmState,
279
+ alarmRingDuration: result.alarmRingDuration,
280
+ alarmSnoozeDuration: result.alarmSnoozeDuration,
281
+ alarmSnoozeTotal: result.alarmSnoozeTotal,
282
+ daysOfWakeType: result.daysOfWakeType,
283
+ },
284
+ code,
285
+ }),
286
+ },
287
+ ],
288
+ });
289
+ }
290
+ else {
291
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Alarm creation failed`);
292
+ logger.error(`[CREATE_ALARM_TOOL] - status: ${event.status}`);
293
+ logger.error(`[CREATE_ALARM_TOOL] - outputs:`, JSON.stringify(event.outputs || {}));
294
+ reject(new Error(`创建闹钟失败: ${event.status}`));
295
+ }
296
+ }
297
+ };
298
+ // Register event handler
299
+ logger.log(`[CREATE_ALARM_TOOL] 📡 Registering data-event handler on WebSocket manager`);
300
+ wsManager.on("data-event", handler);
301
+ // Send the command
302
+ logger.log(`[CREATE_ALARM_TOOL] 📤 Sending CreateAlarm command...`);
303
+ sendCommand({
304
+ config,
305
+ sessionId,
306
+ taskId,
307
+ messageId,
308
+ command,
309
+ })
310
+ .then(() => {
311
+ logger.log(`[CREATE_ALARM_TOOL] ✅ Command sent successfully, waiting for response...`);
312
+ })
313
+ .catch((error) => {
314
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Failed to send command:`, error);
315
+ clearTimeout(timeout);
316
+ wsManager.off("data-event", handler);
317
+ reject(error);
318
+ });
319
+ });
320
+ },
321
+ };
322
+ /**
323
+ * Parse alarmTime string (YYYYMMDD hhmmss) to timestamp in milliseconds
324
+ * @param alarmTime - Time string in format "YYYYMMDD hhmmss" (e.g., "20240315 143000")
325
+ * @returns Timestamp in milliseconds, or null if parsing fails
326
+ */
327
+ function parseAlarmTimeToTimestamp(alarmTime) {
328
+ try {
329
+ // Expected format: YYYYMMDD hhmmss
330
+ // Example: 20240315 143000
331
+ const trimmed = alarmTime.trim();
332
+ // Check basic format (should have at least 13 characters: YYYYMMDD hhmmss)
333
+ if (trimmed.length < 13) {
334
+ logger.error(`[CREATE_ALARM_TOOL] ❌ alarmTime too short: ${trimmed}`);
335
+ return null;
336
+ }
337
+ // Extract date and time parts
338
+ // Format: YYYYMMDD hhmmss
339
+ const datePart = trimmed.substring(0, 8); // YYYYMMDD
340
+ const timePart = trimmed.substring(8).trim(); // hhmmss (may have leading space)
341
+ logger.log(`[CREATE_ALARM_TOOL] - datePart: ${datePart}, timePart: ${timePart}`);
342
+ // Validate lengths
343
+ if (datePart.length !== 8 || timePart.length !== 6) {
344
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid part lengths: datePart=${datePart.length}, timePart=${timePart.length}`);
345
+ return null;
346
+ }
347
+ // Parse components
348
+ const year = parseInt(datePart.substring(0, 4), 10);
349
+ const month = parseInt(datePart.substring(4, 6), 10);
350
+ const day = parseInt(datePart.substring(6, 8), 10);
351
+ const hour = parseInt(timePart.substring(0, 2), 10);
352
+ const minute = parseInt(timePart.substring(2, 4), 10);
353
+ const second = parseInt(timePart.substring(4, 6), 10);
354
+ logger.log(`[CREATE_ALARM_TOOL] - Parsed: ${year}-${month}-${day} ${hour}:${minute}:${second}`);
355
+ // Validate values
356
+ if (isNaN(year) || isNaN(month) || isNaN(day) ||
357
+ isNaN(hour) || isNaN(minute) || isNaN(second)) {
358
+ logger.error(`[CREATE_ALARM_TOOL] ❌ NaN detected in parsed values`);
359
+ return null;
360
+ }
361
+ // Validate ranges
362
+ if (month < 1 || month > 12) {
363
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid month: ${month}`);
364
+ return null;
365
+ }
366
+ if (day < 1 || day > 31) {
367
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid day: ${day}`);
368
+ return null;
369
+ }
370
+ if (hour < 0 || hour > 23) {
371
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid hour: ${hour}`);
372
+ return null;
373
+ }
374
+ if (minute < 0 || minute > 59) {
375
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid minute: ${minute}`);
376
+ return null;
377
+ }
378
+ if (second < 0 || second > 59) {
379
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Invalid second: ${second}`);
380
+ return null;
381
+ }
382
+ // Create Date object and get timestamp
383
+ const date = new Date(year, month - 1, day, hour, minute, second);
384
+ const timestamp = date.getTime();
385
+ if (isNaN(timestamp)) {
386
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Generated timestamp is NaN`);
387
+ return null;
388
+ }
389
+ return timestamp;
390
+ }
391
+ catch (error) {
392
+ logger.error(`[CREATE_ALARM_TOOL] ❌ Exception in parseAlarmTimeToTimestamp:`, error);
393
+ return null;
394
+ }
395
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * XY delete alarm tool - deletes existing alarms on user's device.
3
+ * Requires entityId(s) from search_alarm or create_alarm tool as prerequisite.
4
+ *
5
+ * Prerequisites:
6
+ * 1. Call search_alarm or create_alarm tool first to get entityId(s) of alarms
7
+ * 2. Use the entityId(s) to delete those alarms
8
+ *
9
+ * Supports deleting single or multiple alarms in one call.
10
+ */
11
+ export declare const deleteAlarmTool: any;
@@ -0,0 +1,238 @@
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
+ /**
6
+ * XY delete alarm tool - deletes existing alarms on user's device.
7
+ * Requires entityId(s) from search_alarm or create_alarm tool as prerequisite.
8
+ *
9
+ * Prerequisites:
10
+ * 1. Call search_alarm or create_alarm tool first to get entityId(s) of alarms
11
+ * 2. Use the entityId(s) to delete those alarms
12
+ *
13
+ * Supports deleting single or multiple alarms in one call.
14
+ */
15
+ export const deleteAlarmTool = {
16
+ name: "delete_alarm",
17
+ label: "Delete Alarm",
18
+ description: `删除用户设备上的闹钟。使用前必须先调用 search_alarm 或 create_alarm 工具获取闹钟的 entityId。
19
+
20
+ 工具参数:
21
+ - items: 要删除的闹钟列表,每个元素包含 entityId 字段。支持数组或 JSON 字符串格式。entityId 是闹钟的唯一标识符(从 search_alarm 或 create_alarm 工具获取)。
22
+
23
+ 使用示例:
24
+ - 删除单个闹钟:{"items": [{"entityId": "6"}]}
25
+ - 删除多个闹钟:{"items": [{"entityId": "6"}, {"entityId": "8"}]}
26
+
27
+ 注意事项:
28
+ 1. 操作超时时间为60秒,请勿重复调用此工具,如果超时或失败,最多重试一次。
29
+ 2. 删除操作不可撤销,请确认 entityId 正确后再删除。
30
+ 3. items 参数支持数组或 JSON 字符串格式,代码会自动解析。`,
31
+ parameters: {
32
+ type: "object",
33
+ properties: {
34
+ items: {
35
+ // 不指定 type,允许传入数组或 JSON 字符串
36
+ // 具体的类型验证和转换在 execute 函数内部进行
37
+ description: "要删除的闹钟列表,每个元素包含 entityId 字段。支持数组或 JSON 字符串格式。",
38
+ },
39
+ },
40
+ required: ["items"],
41
+ },
42
+ async execute(toolCallId, params) {
43
+ logger.log(`[DELETE_ALARM_TOOL] 🚀 Starting execution`);
44
+ logger.log(`[DELETE_ALARM_TOOL] - toolCallId: ${toolCallId}`);
45
+ logger.log(`[DELETE_ALARM_TOOL] - params (raw):`, JSON.stringify(params));
46
+ logger.log(`[DELETE_ALARM_TOOL] - params.items type:`, typeof params.items);
47
+ logger.log(`[DELETE_ALARM_TOOL] - timestamp: ${new Date().toISOString()}`);
48
+ // ===== 参数规范化:兼容数组和 JSON 字符串 =====
49
+ let items = null;
50
+ if (!params.items) {
51
+ logger.error(`[DELETE_ALARM_TOOL] ❌ Missing parameter: items`);
52
+ throw new Error("Missing required parameter: items");
53
+ }
54
+ // 情况1: 已经是数组
55
+ if (Array.isArray(params.items)) {
56
+ logger.log(`[DELETE_ALARM_TOOL] ✅ items is already an array`);
57
+ items = params.items;
58
+ }
59
+ // 情况2: 是字符串,尝试解析为 JSON 数组
60
+ else if (typeof params.items === 'string') {
61
+ logger.log(`[DELETE_ALARM_TOOL] 🔄 items is a string, attempting to parse as JSON...`);
62
+ try {
63
+ const parsed = JSON.parse(params.items);
64
+ if (Array.isArray(parsed)) {
65
+ logger.log(`[DELETE_ALARM_TOOL] ✅ Successfully parsed JSON string to array`);
66
+ items = parsed;
67
+ }
68
+ else {
69
+ logger.error(`[DELETE_ALARM_TOOL] ❌ Parsed JSON is not an array:`, typeof parsed);
70
+ throw new Error("items must be an array or a JSON string representing an array");
71
+ }
72
+ }
73
+ catch (parseError) {
74
+ logger.error(`[DELETE_ALARM_TOOL] ❌ Failed to parse items as JSON:`, parseError);
75
+ throw new Error(`items must be a valid JSON array string. Parse error: ${parseError instanceof Error ? parseError.message : String(parseError)}`);
76
+ }
77
+ }
78
+ // 情况3: 其他类型,报错
79
+ else {
80
+ logger.error(`[DELETE_ALARM_TOOL] ❌ Invalid items type:`, typeof params.items);
81
+ throw new Error(`items must be an array or a JSON string, got ${typeof params.items}`);
82
+ }
83
+ // 验证数组非空
84
+ if (!items || items.length === 0) {
85
+ logger.error(`[DELETE_ALARM_TOOL] ❌ items array is empty`);
86
+ throw new Error("items array cannot be empty");
87
+ }
88
+ logger.log(`[DELETE_ALARM_TOOL] ✅ Normalized items:`, JSON.stringify(items));
89
+ // 验证每个 item 是否有有效的 entityId
90
+ for (let i = 0; i < items.length; i++) {
91
+ const item = items[i];
92
+ if (!item || typeof item !== 'object') {
93
+ logger.error(`[DELETE_ALARM_TOOL] ❌ Item at index ${i} is not an object`);
94
+ throw new Error(`items[${i}] must be an object with entityId property`);
95
+ }
96
+ if (!item.entityId || typeof item.entityId !== 'string') {
97
+ logger.error(`[DELETE_ALARM_TOOL] ❌ Item at index ${i} missing or invalid entityId`);
98
+ throw new Error(`items[${i}] must have a valid entityId string property`);
99
+ }
100
+ }
101
+ logger.log(`[DELETE_ALARM_TOOL] - items count: ${items.length}`);
102
+ logger.log(`[DELETE_ALARM_TOOL] - entityIds:`, items.map(item => item.entityId).join(", "));
103
+ // Get session context
104
+ logger.log(`[DELETE_ALARM_TOOL] 🔍 Attempting to get session context...`);
105
+ const sessionContext = getCurrentSessionContext();
106
+ if (!sessionContext) {
107
+ logger.error(`[DELETE_ALARM_TOOL] ❌ FAILED: No active session found!`);
108
+ logger.error(`[DELETE_ALARM_TOOL] - toolCallId: ${toolCallId}`);
109
+ throw new Error("No active XY session found. Delete alarm tool can only be used during an active conversation.");
110
+ }
111
+ logger.log(`[DELETE_ALARM_TOOL] ✅ Session context found`);
112
+ logger.log(`[DELETE_ALARM_TOOL] - sessionId: ${sessionContext.sessionId}`);
113
+ logger.log(`[DELETE_ALARM_TOOL] - taskId: ${sessionContext.taskId}`);
114
+ logger.log(`[DELETE_ALARM_TOOL] - messageId: ${sessionContext.messageId}`);
115
+ const { config, sessionId, taskId, messageId } = sessionContext;
116
+ // Get WebSocket manager
117
+ logger.log(`[DELETE_ALARM_TOOL] 🔌 Getting WebSocket manager...`);
118
+ const wsManager = getXYWebSocketManager(config);
119
+ logger.log(`[DELETE_ALARM_TOOL] ✅ WebSocket manager obtained`);
120
+ // Build DeleteAlarm command
121
+ logger.log(`[DELETE_ALARM_TOOL] 📦 Building DeleteAlarm command...`);
122
+ const command = {
123
+ header: {
124
+ namespace: "Common",
125
+ name: "Action",
126
+ },
127
+ payload: {
128
+ cardParam: {},
129
+ executeParam: {
130
+ executeMode: "background",
131
+ intentName: "DeleteAlarm",
132
+ bundleName: "com.huawei.hmos.clock",
133
+ needUnlock: true,
134
+ actionResponse: true,
135
+ appType: "OHOS_APP",
136
+ timeOut: 5,
137
+ intentParam: {
138
+ items: items,
139
+ },
140
+ permissionId: [],
141
+ achieveType: "INTENT",
142
+ },
143
+ responses: [
144
+ {
145
+ resultCode: "",
146
+ displayText: "",
147
+ ttsText: "",
148
+ },
149
+ ],
150
+ needUploadResult: true,
151
+ noHalfPage: false,
152
+ pageControlRelated: false,
153
+ },
154
+ };
155
+ // Send command and wait for response (60 second timeout)
156
+ logger.log(`[DELETE_ALARM_TOOL] ⏳ Setting up promise to wait for alarm deletion response...`);
157
+ logger.log(`[DELETE_ALARM_TOOL] - Timeout: 60 seconds`);
158
+ return new Promise((resolve, reject) => {
159
+ const timeout = setTimeout(() => {
160
+ logger.error(`[DELETE_ALARM_TOOL] ⏰ Timeout: No response received within 60 seconds`);
161
+ wsManager.off("data-event", handler);
162
+ reject(new Error("删除闹钟超时(60秒)"));
163
+ }, 60000);
164
+ // Listen for data events from WebSocket
165
+ const handler = (event) => {
166
+ logger.log(`[DELETE_ALARM_TOOL] 📨 Received data event:`, JSON.stringify(event));
167
+ if (event.intentName === "DeleteAlarm") {
168
+ logger.log(`[DELETE_ALARM_TOOL] 🎯 DeleteAlarm event received`);
169
+ logger.log(`[DELETE_ALARM_TOOL] - status: ${event.status}`);
170
+ clearTimeout(timeout);
171
+ wsManager.off("data-event", handler);
172
+ if (event.status === "success" && event.outputs) {
173
+ logger.log(`[DELETE_ALARM_TOOL] ✅ Alarm deletion completed successfully`);
174
+ logger.log(`[DELETE_ALARM_TOOL] - outputs:`, JSON.stringify(event.outputs));
175
+ // Check for error code in outputs
176
+ const code = event.outputs.code !== undefined ? event.outputs.code : null;
177
+ if (code !== null && code !== 0) {
178
+ logger.error(`[DELETE_ALARM_TOOL] ❌ Device returned error`);
179
+ logger.error(`[DELETE_ALARM_TOOL] - code: ${code}`);
180
+ const errorMsg = event.outputs.errorMsg || event.outputs.errMsg || "未知错误";
181
+ logger.error(`[DELETE_ALARM_TOOL] - errorMsg: ${errorMsg}`);
182
+ reject(new Error(`删除闹钟失败: ${errorMsg} (错误代码: ${code})`));
183
+ return;
184
+ }
185
+ // Extract result with safe navigation
186
+ const result = event.outputs.result || {};
187
+ logger.log(`[DELETE_ALARM_TOOL] 📋 Deletion result:`, JSON.stringify(result));
188
+ // Build response with safe navigation
189
+ const response = {
190
+ success: true,
191
+ entityName: result.entityName || "Alarm",
192
+ message: result.message || "Alarm deleted successfully",
193
+ deletedCount: items.length,
194
+ };
195
+ // Add entityIds from request for reference
196
+ response.deletedIds = items.map(item => item.entityId);
197
+ logger.log(`[DELETE_ALARM_TOOL] 🎉 Successfully deleted ${items.length} alarm(s)`);
198
+ resolve({
199
+ content: [
200
+ {
201
+ type: "text",
202
+ text: JSON.stringify(response),
203
+ },
204
+ ],
205
+ });
206
+ }
207
+ else {
208
+ logger.error(`[DELETE_ALARM_TOOL] ❌ Alarm deletion failed`);
209
+ logger.error(`[DELETE_ALARM_TOOL] - status: ${event.status}`);
210
+ logger.error(`[DELETE_ALARM_TOOL] - outputs:`, JSON.stringify(event.outputs || {}));
211
+ reject(new Error(`删除闹钟失败: ${event.status}`));
212
+ }
213
+ }
214
+ };
215
+ // Register event handler
216
+ logger.log(`[DELETE_ALARM_TOOL] 📡 Registering data-event handler on WebSocket manager`);
217
+ wsManager.on("data-event", handler);
218
+ // Send the command
219
+ logger.log(`[DELETE_ALARM_TOOL] 📤 Sending DeleteAlarm command...`);
220
+ sendCommand({
221
+ config,
222
+ sessionId,
223
+ taskId,
224
+ messageId,
225
+ command,
226
+ })
227
+ .then(() => {
228
+ logger.log(`[DELETE_ALARM_TOOL] ✅ Command sent successfully, waiting for response...`);
229
+ })
230
+ .catch((error) => {
231
+ logger.error(`[DELETE_ALARM_TOOL] ❌ Failed to send command:`, error);
232
+ clearTimeout(timeout);
233
+ wsManager.off("data-event", handler);
234
+ reject(error);
235
+ });
236
+ });
237
+ },
238
+ };
@@ -1,6 +1,6 @@
1
1
  import { getXYWebSocketManager } from "../client.js";
2
2
  import { sendCommand } from "../formatter.js";
3
- import { getLatestSessionContext } from "./session-manager.js";
3
+ import { getCurrentSessionContext } from "./session-manager.js";
4
4
  import { logger } from "../utils/logger.js";
5
5
  /**
6
6
  * XY location tool - gets user's current location.
@@ -22,7 +22,7 @@ export const locationTool = {
22
22
  logger.log(`[LOCATION_TOOL] - timestamp: ${new Date().toISOString()}`);
23
23
  // Get session context
24
24
  logger.log(`[LOCATION_TOOL] 🔍 Attempting to get session context...`);
25
- const sessionContext = getLatestSessionContext();
25
+ const sessionContext = getCurrentSessionContext();
26
26
  if (!sessionContext) {
27
27
  logger.error(`[LOCATION_TOOL] ❌ FAILED: No active session found!`);
28
28
  logger.error(`[LOCATION_TOOL] - toolCallId: ${toolCallId}`);
@@ -0,0 +1,9 @@
1
+ /**
2
+ * XY modify alarm tool - modifies an existing alarm on user's device.
3
+ * Requires entityId from search_alarm or create_alarm tool.
4
+ *
5
+ * Prerequisites:
6
+ * 1. Call search_alarm or create_alarm tool first to get entityId
7
+ * 2. Use the entityId to identify which alarm to modify
8
+ */
9
+ export declare const modifyAlarmTool: any;