qwen-audio-agent 1.4.2 → 1.5.0
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/README.md +10 -0
- package/README_EN.md +12 -0
- package/cli/package.json +1 -1
- package/config/frontend-agent/PROMPT.md +16 -0
- package/docs/configuration.md +11 -2
- package/package.json +7 -2
- package/server/package.json +4 -1
- package/server/src/agent/agent-client.mjs +2 -6
- package/server/src/app/bootstrap.mjs +67 -0
- package/server/src/core/config.mjs +43 -0
- package/server/src/task/reminder-scheduler.mjs +107 -0
- package/server/src/task/task-manager.mjs +214 -2
- package/server/src/voice/frontend-tools.mjs +36 -1
- package/server/src/voice/realtime-gateway.mjs +247 -6
- package/server/src/voice/realtime-provider.mjs +1 -0
- package/server/src/voice/sleep-controller.mjs +79 -0
- package/server/src/voice/tools/tool-call-handler.mjs +64 -0
- package/server/src/voice/wake-word/model-manager.mjs +121 -0
- package/server/src/voice/wake-word/sherpa-detector.mjs +85 -0
- package/shared/realtime-events.mjs +5 -0
- package/tui/package.json +1 -1
- package/web/dist/assets/index-CAteXyT1.js +38 -0
- package/web/dist/index.html +1 -1
- package/web/dist/assets/index-DnfPPt23.js +0 -38
package/README.md
CHANGED
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
|
|
21
21
|
## News
|
|
22
22
|
|
|
23
|
+
- **2026-08-05 · [v1.5.0](https://github.com/QwenAudio/qwen-audio-agent/releases/tag/v1.5.0)**
|
|
24
|
+
⏰ 新增定时提醒与进度查询,任务完成或待授权时自动唤醒;🗣️ 新增语音唤醒词“你好千问”,休眠后说出唤醒词即可激活;🐧 桌面版支持 Linux 打包(AppImage + deb);桌面版数据目录与 CLI 隔离,两者可同时使用互不干扰。
|
|
23
25
|
- **2026-08-05 · [v1.4.2](https://github.com/QwenAudio/qwen-audio-agent/releases/tag/v1.4.2)**
|
|
24
26
|
🔧 优化桌面端后台 Agent 的安装、登录与状态检测,完善长期记忆行为。
|
|
25
27
|
- **2026-08-04 · [v1.4.1](https://github.com/QwenAudio/qwen-audio-agent/releases/tag/v1.4.1)**
|
|
@@ -266,6 +268,14 @@ qwenaudio tui --audio-mode full
|
|
|
266
268
|
npm run desktop:build:local
|
|
267
269
|
```
|
|
268
270
|
|
|
271
|
+
官方 Release 只发布 macOS 安装包。需要 Linux 版时,请在 Linux 机器上自行打包:
|
|
272
|
+
|
|
273
|
+
```bash
|
|
274
|
+
npm run desktop:build:linux
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
产物位于 `dist/desktop/`(AppImage 与 deb),无需代码签名证书。
|
|
278
|
+
|
|
269
279
|
## 后台常驻
|
|
270
280
|
|
|
271
281
|
希望个人助理长期在线时,可以安装为用户后台服务:
|
package/README_EN.md
CHANGED
|
@@ -25,6 +25,8 @@ tells you:
|
|
|
25
25
|
|
|
26
26
|
## News
|
|
27
27
|
|
|
28
|
+
- **2026-08-05 · [v1.5.0](https://github.com/QwenAudio/qwen-audio-agent/releases/tag/v1.5.0)**
|
|
29
|
+
⏰ Adds scheduled reminders and progress reporting, with automatic wake-up when a task completes or needs approval; 🗣️ adds the voice wake word (“你好千问”) — speak it while the assistant is asleep to activate it; 🐧 desktop build support for Linux (AppImage + deb); the desktop app now uses a data directory isolated from the CLI, so both can run side by side without interfering.
|
|
28
30
|
- **2026-08-05 · [v1.4.2](https://github.com/QwenAudio/qwen-audio-agent/releases/tag/v1.4.2)**
|
|
29
31
|
🔧 Refines desktop backend Agent installation, login, and status detection, with more reliable long-term memory behavior.
|
|
30
32
|
- **2026-08-04 · [v1.4.1](https://github.com/QwenAudio/qwen-audio-agent/releases/tag/v1.4.1)**
|
|
@@ -296,6 +298,16 @@ Build a local test package from source:
|
|
|
296
298
|
npm run desktop:build:local
|
|
297
299
|
```
|
|
298
300
|
|
|
301
|
+
Official releases ship macOS installers only. To build for Linux, run on a
|
|
302
|
+
Linux machine:
|
|
303
|
+
|
|
304
|
+
```bash
|
|
305
|
+
npm run desktop:build:linux
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
Artifacts land in `dist/desktop/` (AppImage and deb). No signing certificate
|
|
309
|
+
is required.
|
|
310
|
+
|
|
299
311
|
## Run the Gateway in the Background
|
|
300
312
|
|
|
301
313
|
To keep your personal assistant available, install the Gateway as a user
|
package/cli/package.json
CHANGED
|
@@ -133,3 +133,19 @@ User Memory 中的 `rules` 是用户亲自教你的长期约定:说话方式
|
|
|
133
133
|
|
|
134
134
|
表达应简洁、自然、适合听觉。不要朗读协议字段、工作 ID、路径、URL、端口、
|
|
135
135
|
哈希、时间戳或长数字,除非用户明确要求准确值。
|
|
136
|
+
|
|
137
|
+
# Scheduled reminders
|
|
138
|
+
|
|
139
|
+
用户说“X点提醒我”“明天三点帮我查某事然后告诉我”等时间驱动的提醒或任务时,
|
|
140
|
+
先调用 get_current_time 获取当前时间,计算目标时间后调用 schedule_reminder。
|
|
141
|
+
|
|
142
|
+
- type=reminder:到点直接播报 reminder 内容。用户只要求提醒时用此类型。
|
|
143
|
+
- type=task:到点执行 reminder 描述的任务,执行完播报结果。用户要求“帮我做某事然后告诉我”时用此类型。
|
|
144
|
+
- execute_at 用 ISO 8601 格式,基于 get_current_time 返回的时区计算。
|
|
145
|
+
- 重复提醒用 recurrence:每天 daily、每周 weekly、工作日 weekdays。
|
|
146
|
+
|
|
147
|
+
用户询问有哪些待提醒时,调 get_agent_task_status(不传 work_id)可列出全部工作,
|
|
148
|
+
其中 status=scheduled 的就是未到点的提醒。取消时调 cancel_agent_task 并传入 reminder_id。
|
|
149
|
+
|
|
150
|
+
定时任务(type=task)执行期间,系统会在 5 分钟后开始定期播报真实进度
|
|
151
|
+
(如“正在执行 npm test”),不需要你主动查询。任务完成后自动播报最终结果。
|
package/docs/configuration.md
CHANGED
|
@@ -9,6 +9,13 @@
|
|
|
9
9
|
设置 `QWAUDIO_CONFIG_DIR` 或 `XDG_CONFIG_HOME` 可以更改配置目录。开发仓库中的
|
|
10
10
|
`.env.local` 和 `.env` 仍然支持,并优先于用户配置文件。
|
|
11
11
|
|
|
12
|
+
桌面版与 CLI 使用相互独立的数据目录:CLI 使用 `~/.config/qwaudio`,桌面版使用
|
|
13
|
+
系统标准应用数据目录(macOS 为 `~/Library/Application Support/Qwen Audio Agent`,
|
|
14
|
+
Linux 为 `~/.config/Qwen Audio Agent`,Windows 为 `%APPDATA%\Qwen Audio Agent`)。
|
|
15
|
+
两者的 Gateway、锁、日志与设置互不干扰,可以同时运行。桌面版首次启动时会从 CLI
|
|
16
|
+
目录复制 `config.env` 等用户配置(CLI 保留原件);`gateway.lock` 等运行时状态各自
|
|
17
|
+
重建。显式设置 `QWAUDIO_CONFIG_DIR` 时桌面版也遵循该覆盖。
|
|
18
|
+
|
|
12
19
|
配置优先级固定为:
|
|
13
20
|
|
|
14
21
|
```text
|
|
@@ -395,8 +402,10 @@ QWEN_AUDIO_AGENT_ALLOWED_ORIGINS=https://voice.example.com
|
|
|
395
402
|
|
|
396
403
|
## Gateway 运行方式
|
|
397
404
|
|
|
398
|
-
|
|
399
|
-
|
|
405
|
+
同一数据目录在任意时刻只允许一个本地 Gateway。CLI、TUI 和 WebUI 共用
|
|
406
|
+
`~/.config/qwaudio`,会优先复用同一个实例;桌面版使用独立目录,只复用或管理
|
|
407
|
+
自己目录下的 Gateway。同一目录内的多个客户端可以同时连接,但不会各自启动一套
|
|
408
|
+
后台 Agent。实例身份记录在
|
|
400
409
|
用户配置目录下的临时 `gateway.lock` 中,Gateway 正常退出时会删除,异常退出留下的
|
|
401
410
|
锁会在确认原进程已经结束后自动回收。若现有 Gateway 的 Realtime、后台 Agent 或
|
|
402
411
|
权限配置与当前请求不一致,启动会明确报错,而不会静默另开随机端口。远程 Gateway
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qwen-audio-agent",
|
|
3
|
-
"
|
|
3
|
+
"desktopName": "qwen-audio-agent.desktop",
|
|
4
|
+
"version": "1.5.0",
|
|
4
5
|
"description": "A realtime voice frontend for mainstream AI agents.",
|
|
5
6
|
"author": "qwen-audio-agent contributors",
|
|
6
7
|
"license": "Apache-2.0",
|
|
@@ -98,6 +99,7 @@
|
|
|
98
99
|
"test:desktop-smoke": "node scripts/electron-smoke.mjs",
|
|
99
100
|
"desktop:build": "node scripts/check-desktop-release-env.mjs && npm run build && electron-builder --config desktop/electron-builder.yml --mac dmg zip",
|
|
100
101
|
"desktop:build:local": "npm run build && electron-builder --config desktop/electron-builder.yml --config.mac.identity=- --config.mac.hardenedRuntime=false --config.mac.notarize=false --mac dmg",
|
|
102
|
+
"desktop:build:linux": "npm run build && electron-builder --config desktop/electron-builder.yml --linux AppImage deb",
|
|
101
103
|
"cli": "npm run start --workspace @qwen-audio-agent/cli --",
|
|
102
104
|
"tui": "npm run start --workspace tui --",
|
|
103
105
|
"backend": "node scripts/start.mjs",
|
|
@@ -118,7 +120,10 @@
|
|
|
118
120
|
"electron-updater": "6.8.9",
|
|
119
121
|
"express": "^4.22.2",
|
|
120
122
|
"ws": "^8.21.1",
|
|
121
|
-
"zod": "^4.4.3"
|
|
123
|
+
"zod": "^4.4.3",
|
|
124
|
+
"sherpa-onnx": "1.13.4",
|
|
125
|
+
"tar-stream": "3.1.7",
|
|
126
|
+
"unbzip2-stream": "1.4.3"
|
|
122
127
|
},
|
|
123
128
|
"devDependencies": {
|
|
124
129
|
"concurrently": "^9.2.0",
|
package/server/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qwen-audio-agent/server",
|
|
3
3
|
"private": true,
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.5.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "node --watch src/index.mjs",
|
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
"@agentclientprotocol/sdk": "1.3.0",
|
|
13
13
|
"@modelcontextprotocol/sdk": "1.30.0",
|
|
14
14
|
"express": "^4.22.2",
|
|
15
|
+
"sherpa-onnx": "1.13.4",
|
|
16
|
+
"tar-stream": "3.1.7",
|
|
17
|
+
"unbzip2-stream": "1.4.3",
|
|
15
18
|
"ws": "^8.21.1",
|
|
16
19
|
"zod": "^4.4.3"
|
|
17
20
|
}
|
|
@@ -25,17 +25,13 @@ export class AgentClient {
|
|
|
25
25
|
// backends only need a config.backends entry; nothing to thread here.
|
|
26
26
|
const backend = {
|
|
27
27
|
...(config.backends?.[driver.id] || {}),
|
|
28
|
-
...(backends[driver.id] || {}),
|
|
28
|
+
...(backends?.[driver.id] || {}),
|
|
29
29
|
}
|
|
30
30
|
const options = {
|
|
31
31
|
baseUrl: '',
|
|
32
32
|
...backend,
|
|
33
33
|
model: model ?? backend.model,
|
|
34
|
-
coordinatorAgent:
|
|
35
|
-
coordinatorAgent
|
|
36
|
-
?? backend.coordinatorAgent
|
|
37
|
-
?? config.backends?.opencode?.coordinatorAgent
|
|
38
|
-
),
|
|
34
|
+
coordinatorAgent: coordinatorAgent ?? backend.coordinatorAgent,
|
|
39
35
|
}
|
|
40
36
|
const profile = driver.createProfile({
|
|
41
37
|
protocol,
|
|
@@ -17,6 +17,7 @@ import { attachRealtimeGateway } from '../voice/realtime-gateway.mjs'
|
|
|
17
17
|
import { describeActiveRealtime } from '../voice/realtime-provider.mjs'
|
|
18
18
|
import { SessionPermissionPolicy } from '../voice/session-permission-policy.mjs'
|
|
19
19
|
import { taskManager, taskStore } from '../task/task-manager.mjs'
|
|
20
|
+
import { ReminderScheduler } from '../task/reminder-scheduler.mjs'
|
|
20
21
|
import { webDistributionPath } from '../core/install-paths.mjs'
|
|
21
22
|
|
|
22
23
|
const identityManager = new IdentityManager({
|
|
@@ -41,6 +42,72 @@ taskManager.recoverDelegated({
|
|
|
41
42
|
return result
|
|
42
43
|
},
|
|
43
44
|
})
|
|
45
|
+
// Provide a fallback runner for restored scheduled_task entries whose
|
|
46
|
+
// runner (a closure) was lost during serialisation. Uses the shared
|
|
47
|
+
// coordinator singleton — same path as handleScheduleReminder.
|
|
48
|
+
taskManager.configureScheduledTaskRunner(
|
|
49
|
+
async (objective, { onEvent, signal }) => coordinator.run({
|
|
50
|
+
originalRequest: objective,
|
|
51
|
+
objective,
|
|
52
|
+
conversationContext: [],
|
|
53
|
+
userMemories: [],
|
|
54
|
+
}, { signal, onEvent }),
|
|
55
|
+
)
|
|
56
|
+
taskManager.configureCoordinatorQuery(
|
|
57
|
+
(workId, question, options) => coordinator.queryDelegatedWork(workId, question, options),
|
|
58
|
+
)
|
|
59
|
+
// ReminderScheduler: setTimeout-driven, no polling. Handles overdue
|
|
60
|
+
// stagger on restart and re-arming after each fire.
|
|
61
|
+
if (config.reminderSchedulerEnabled) {
|
|
62
|
+
const reminderScheduler = new ReminderScheduler({
|
|
63
|
+
taskManager,
|
|
64
|
+
staggerMs: config.reminderStaggerMs,
|
|
65
|
+
logger,
|
|
66
|
+
})
|
|
67
|
+
reminderScheduler.start()
|
|
68
|
+
}
|
|
69
|
+
// Offline notification subscriber: if a voice session does not claim a
|
|
70
|
+
// pending notification within the delay window, deliver via desktop
|
|
71
|
+
// notification (Electron) and WebSocket push.
|
|
72
|
+
taskManager.subscribe(event => {
|
|
73
|
+
if (event.type === 'task.progress.check') {
|
|
74
|
+
const timer = setTimeout(() => {
|
|
75
|
+
if (process.parentPort) {
|
|
76
|
+
process.parentPort.postMessage({
|
|
77
|
+
type: 'qwen-audio-agent:offline-notification',
|
|
78
|
+
task: {
|
|
79
|
+
id: event.task.id,
|
|
80
|
+
objective: event.task.objective,
|
|
81
|
+
result: event.message,
|
|
82
|
+
status: 'progress',
|
|
83
|
+
},
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
}, config.offlineNotificationDelayMs)
|
|
87
|
+
timer.unref?.()
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
if (event.type !== 'task.notification.pending') return
|
|
91
|
+
const task = event.task
|
|
92
|
+
const timer = setTimeout(() => {
|
|
93
|
+
const current = taskManager.get(task.id, { ownerId: event.ownerId })
|
|
94
|
+
if (current && current.notificationStatus === 'pending') {
|
|
95
|
+
if (process.parentPort) {
|
|
96
|
+
process.parentPort.postMessage({
|
|
97
|
+
type: 'qwen-audio-agent:offline-notification',
|
|
98
|
+
task: {
|
|
99
|
+
id: current.id,
|
|
100
|
+
objective: current.objective,
|
|
101
|
+
result: current.result,
|
|
102
|
+
error: current.error,
|
|
103
|
+
status: current.status,
|
|
104
|
+
},
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}, config.offlineNotificationDelayMs)
|
|
109
|
+
timer.unref?.()
|
|
110
|
+
})
|
|
44
111
|
conversationSync.configureRetention({
|
|
45
112
|
sessionTtlMs: config.conversationSessionTtlMs,
|
|
46
113
|
maxSessions: config.maxConversationSessions,
|
|
@@ -452,6 +452,49 @@ export const config = {
|
|
|
452
452
|
1000,
|
|
453
453
|
{ min: 10 },
|
|
454
454
|
),
|
|
455
|
+
reminderSchedulerEnabled: String(
|
|
456
|
+
process.env.QWEN_AUDIO_AGENT_REMINDER_SCHEDULER || 'true'
|
|
457
|
+
).toLowerCase() === 'true',
|
|
458
|
+
reminderMaxPerOwner: numberSetting(
|
|
459
|
+
process.env.QWEN_AUDIO_AGENT_REMINDER_MAX_PER_OWNER,
|
|
460
|
+
50,
|
|
461
|
+
{ min: 1, max: 500 },
|
|
462
|
+
),
|
|
463
|
+
scheduledTaskTimeoutMs: numberSetting(
|
|
464
|
+
process.env.QWEN_AUDIO_AGENT_SCHEDULED_TASK_TIMEOUT_MS,
|
|
465
|
+
1_800_000,
|
|
466
|
+
{ min: 60_000 },
|
|
467
|
+
),
|
|
468
|
+
scheduledTaskProgressCheckMs: numberSetting(
|
|
469
|
+
process.env.QWEN_AUDIO_AGENT_SCHEDULED_TASK_PROGRESS_CHECK_MS,
|
|
470
|
+
300_000,
|
|
471
|
+
{ min: 30_000 },
|
|
472
|
+
),
|
|
473
|
+
offlineNotificationDelayMs: numberSetting(
|
|
474
|
+
process.env.QWEN_AUDIO_AGENT_OFFLINE_NOTIFICATION_DELAY_MS,
|
|
475
|
+
5_000,
|
|
476
|
+
{ min: 1_000, max: 120_000 },
|
|
477
|
+
),
|
|
478
|
+
reminderStaggerMs: numberSetting(
|
|
479
|
+
process.env.QWEN_AUDIO_AGENT_REMINDER_STAGGER_MS,
|
|
480
|
+
30_000,
|
|
481
|
+
{ min: 0, max: 300_000 },
|
|
482
|
+
),
|
|
483
|
+
// The sleep timeout mirrors the desktop auto-hide timeout: the orb hides
|
|
484
|
+
// and the gateway enters sleep mode at the same threshold. The legacy
|
|
485
|
+
// QWEN_AUDIO_SLEEP_TIMEOUT_SECONDS is ignored to avoid divergence.
|
|
486
|
+
sleepTimeoutMs: numberSetting(
|
|
487
|
+
process.env.QWEN_AUDIO_DESKTOP_AUTO_HIDE_SECONDS,
|
|
488
|
+
0,
|
|
489
|
+
{ min: 0, max: 86_400 },
|
|
490
|
+
) * 1000,
|
|
491
|
+
wakeWordEnabled: String(
|
|
492
|
+
process.env.QWEN_AUDIO_WAKE_WORD_ENABLED || '',
|
|
493
|
+
).toLowerCase() === 'true',
|
|
494
|
+
wakeWord: '你好千问',
|
|
495
|
+
wakeWordModelDirectory: process.env.QWEN_AUDIO_WAKE_WORD_MODEL_DIR
|
|
496
|
+
? resolve(process.env.QWEN_AUDIO_WAKE_WORD_MODEL_DIR)
|
|
497
|
+
: resolve(runtimeEnvironment.configDirectory, 'models/wake-word'),
|
|
455
498
|
}
|
|
456
499
|
|
|
457
500
|
export function realtimeUrl(baseUrl, model) {
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ReminderScheduler — setTimeout-driven scheduler for scheduled tasks.
|
|
3
|
+
*
|
|
4
|
+
* Does not poll. Uses a single setTimeout for the next due task, re-arming
|
|
5
|
+
* after each fire. On restart, overdue tasks are staggered (not replayed
|
|
6
|
+
* simultaneously) to avoid swamping the backend agent.
|
|
7
|
+
*
|
|
8
|
+
* Borrowed from OpenClaw's automation system:
|
|
9
|
+
* - Overdue rescheduling on restart (stagger instead of replay)
|
|
10
|
+
* - Per-run wall-clock budget (handled in TaskManager.start)
|
|
11
|
+
* - Cleanup window before force-fail (handled in TaskManager.start)
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export class ReminderScheduler {
|
|
15
|
+
constructor({
|
|
16
|
+
taskManager,
|
|
17
|
+
staggerMs = 30_000,
|
|
18
|
+
logger = null,
|
|
19
|
+
} = {}) {
|
|
20
|
+
this.taskManager = taskManager
|
|
21
|
+
this.staggerMs = staggerMs
|
|
22
|
+
this.logger = logger
|
|
23
|
+
this.timer = null
|
|
24
|
+
|
|
25
|
+
// Re-arm whenever a new scheduled task is created or cancelled.
|
|
26
|
+
this.taskManager.subscribe(event => {
|
|
27
|
+
if (event.type === 'task.scheduled' || event.type === 'task.cancelled') {
|
|
28
|
+
this.reschedule()
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
start() {
|
|
34
|
+
this.restoreOverdue()
|
|
35
|
+
this.reschedule()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* On restart, overdue tasks are staggered — each fired after an increasing
|
|
40
|
+
* delay rather than all at once. This keeps model/tool bootstrap work out of
|
|
41
|
+
* a single burst and avoids overwhelming the backend agent.
|
|
42
|
+
*/
|
|
43
|
+
restoreOverdue() {
|
|
44
|
+
const now = Date.now()
|
|
45
|
+
const overdue = [...this.taskManager.tasks.values()]
|
|
46
|
+
.filter(t => t.status === 'scheduled' && t.schedule?.at <= now)
|
|
47
|
+
.sort((a, b) => (a.schedule?.at || 0) - (b.schedule?.at || 0))
|
|
48
|
+
|
|
49
|
+
if (!overdue.length) return
|
|
50
|
+
this.logger?.info?.('reminder.restore_overdue', {
|
|
51
|
+
count: overdue.length,
|
|
52
|
+
staggerMs: this.staggerMs,
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
overdue.forEach((task, index) => {
|
|
56
|
+
const delay = index * this.staggerMs
|
|
57
|
+
const timer = setTimeout(() => {
|
|
58
|
+
if (task.status !== 'scheduled') return
|
|
59
|
+
task.status = 'queued'
|
|
60
|
+
this.taskManager.emit('task.scheduled.fired', task)
|
|
61
|
+
this.taskManager.persistDeferred()
|
|
62
|
+
this.taskManager.drain()
|
|
63
|
+
}, delay)
|
|
64
|
+
timer.unref?.()
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Register a single setTimeout for the next due scheduled task.
|
|
70
|
+
* Called after every create, fire, or cancel.
|
|
71
|
+
*/
|
|
72
|
+
reschedule() {
|
|
73
|
+
if (this.timer) {
|
|
74
|
+
clearTimeout(this.timer)
|
|
75
|
+
this.timer = null
|
|
76
|
+
}
|
|
77
|
+
const now = Date.now()
|
|
78
|
+
const next = [...this.taskManager.tasks.values()]
|
|
79
|
+
.filter(t => t.status === 'scheduled' && t.schedule?.at > now)
|
|
80
|
+
.map(t => t.schedule.at)
|
|
81
|
+
.sort((a, b) => a - b)[0]
|
|
82
|
+
if (!next) return
|
|
83
|
+
const delay = next - now
|
|
84
|
+
this.timer = setTimeout(() => this.fire(), delay)
|
|
85
|
+
this.timer.unref?.()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Fire all due scheduled tasks: status scheduled → queued, then drain.
|
|
90
|
+
*/
|
|
91
|
+
fire() {
|
|
92
|
+
const now = Date.now()
|
|
93
|
+
let fired = 0
|
|
94
|
+
for (const task of this.taskManager.tasks.values()) {
|
|
95
|
+
if (task.status === 'scheduled' && task.schedule?.at <= now) {
|
|
96
|
+
task.status = 'queued'
|
|
97
|
+
// Phase 3: recurrence handling (create next cycle's new scheduled task)
|
|
98
|
+
fired += 1
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (fired) {
|
|
102
|
+
this.taskManager.persistDeferred()
|
|
103
|
+
this.taskManager.drain()
|
|
104
|
+
}
|
|
105
|
+
this.reschedule()
|
|
106
|
+
}
|
|
107
|
+
}
|