@cortex-agent/server 2026.5.19-3 → 2026.5.22
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 +5 -1
- package/defaults/config/thread-templates.json +65 -39
- package/defaults/plugins/cortex-system/skills/schedule/SKILL.md +4 -4
- package/defaults/prompts/directives/coder-reviewer.md +9 -4
- package/defaults/prompts/directives/coder.md +10 -2
- package/dist/core/log.js +61 -12
- package/dist/core/status-format.js +1 -1
- package/dist/core/task-parser.d.ts +1 -1
- package/dist/core/task-parser.js +13 -6
- package/dist/core/types/thread-types.d.ts +4 -2
- package/dist/domain/costs/codex-usage-monitor.d.ts +1 -2
- package/dist/domain/costs/codex-usage-monitor.js +2 -2
- package/dist/domain/costs/cost-tracker.d.ts +2 -0
- package/dist/domain/costs/cost-tracker.js +4 -20
- package/dist/domain/costs/rate-limit-throttle.js +1 -4
- package/dist/domain/mcp/tools/context.d.ts +4 -2
- package/dist/domain/mcp/tools/context.js +7 -5
- package/dist/domain/mcp/tools/schedule.d.ts +1 -1
- package/dist/domain/mcp/tools/schedule.js +24 -20
- package/dist/domain/memory/index-regen.js +8 -5
- package/dist/domain/monitor/disk-monitor.js +1 -6
- package/dist/domain/projects/index.d.ts +2 -0
- package/dist/domain/projects/index.js +3 -0
- package/dist/domain/projects/project-store.d.ts +54 -0
- package/dist/domain/projects/project-store.js +194 -0
- package/dist/domain/projects/project-types.d.ts +11 -0
- package/dist/domain/projects/project-types.js +5 -0
- package/dist/domain/remote/client-hot-reload.d.ts +1 -0
- package/dist/domain/remote/client-hot-reload.js +284 -53
- package/dist/domain/scheduling/jobs/_shared.d.ts +1 -1
- package/dist/domain/scheduling/jobs/_shared.js +6 -5
- package/dist/domain/scheduling/jobs/memory-index-regen.js +2 -2
- package/dist/domain/scheduling/jobs/scheduled-task.d.ts +2 -2
- package/dist/domain/scheduling/jobs/scheduled-task.js +38 -36
- package/dist/domain/scheduling/jobs/sync-public.d.ts +1 -0
- package/dist/domain/scheduling/jobs/sync-public.js +34 -0
- package/dist/domain/scheduling/jobs/target-dispatch.d.ts +3 -7
- package/dist/domain/scheduling/jobs/target-dispatch.js +7 -9
- package/dist/domain/scheduling/jobs/task-archive.js +2 -2
- package/dist/domain/scheduling/jobs/task-dispatch.js +18 -16
- package/dist/domain/scheduling/runner.d.ts +1 -0
- package/dist/domain/scheduling/runner.js +2 -0
- package/dist/domain/scheduling/schedule-cli.d.ts +1 -0
- package/dist/domain/scheduling/schedule-cli.js +14 -14
- package/dist/domain/scheduling/schedule-command.js +45 -36
- package/dist/domain/scheduling/scheduler.d.ts +1 -1
- package/dist/domain/scheduling/scheduler.js +12 -11
- package/dist/domain/sessions/session-hooks.d.ts +10 -11
- package/dist/domain/sessions/session-hooks.js +26 -27
- package/dist/domain/sessions/session-registry.d.ts +10 -5
- package/dist/domain/sessions/session-registry.js +24 -9
- package/dist/domain/tasks/pending-tracker.js +8 -9
- package/dist/domain/tasks/system/task-cli.d.ts +2 -0
- package/dist/domain/tasks/system/task-cli.js +189 -29
- package/dist/domain/tasks/system/task-lifecycle-edit.js +3 -3
- package/dist/domain/threads/hook-runner.js +12 -7
- package/dist/domain/threads/runner.d.ts +3 -3
- package/dist/domain/threads/runner.js +30 -28
- package/dist/domain/threads/state-machine.d.ts +3 -0
- package/dist/domain/threads/state-machine.js +4 -0
- package/dist/entry/app.js +30 -9
- package/dist/entry/cli.js +19 -4
- package/dist/entry/daemon.js +28 -2
- package/dist/entry/startup-notify.js +8 -6
- package/dist/orchestration/agent-runner.js +19 -13
- package/dist/orchestration/durable-helpers.js +3 -2
- package/dist/orchestration/interactions/ask-user-question.d.ts +2 -3
- package/dist/orchestration/interactions/ask-user-question.js +5 -4
- package/dist/orchestration/interactions/interaction-handlers.js +8 -5
- package/dist/orchestration/interactions/plan-handler.js +3 -3
- package/dist/orchestration/lifecycle.js +43 -38
- package/dist/orchestration/routing/commands/cancel.js +9 -8
- package/dist/orchestration/routing/commands/channel.js +32 -26
- package/dist/orchestration/routing/commands/cost.js +5 -3
- package/dist/orchestration/routing/commands/device.js +2 -1
- package/dist/orchestration/routing/commands/dispatch.js +8 -7
- package/dist/orchestration/routing/commands/index.js +7 -4
- package/dist/orchestration/routing/commands/mode.js +24 -18
- package/dist/orchestration/routing/commands/nvtop.js +14 -12
- package/dist/orchestration/routing/commands/orient.js +2 -1
- package/dist/orchestration/routing/commands/schedule.js +2 -1
- package/dist/orchestration/routing/commands/sendfile.js +12 -9
- package/dist/orchestration/routing/commands/session.js +12 -10
- package/dist/orchestration/routing/commands/status.js +2 -2
- package/dist/orchestration/routing/commands/tail.js +10 -6
- package/dist/orchestration/routing/commands/task.js +5 -4
- package/dist/orchestration/routing/commands/thread-handlers.js +19 -12
- package/dist/orchestration/routing/hook-bridge-subscribers.js +15 -13
- package/dist/orchestration/status-helpers.d.ts +4 -5
- package/dist/orchestration/status-helpers.js +7 -8
- package/dist/orchestration/thread-executor.js +21 -12
- package/dist/platform/adapter.d.ts +13 -9
- package/dist/platform/adapter.js +1 -1
- package/dist/platform/adapters/feishu-output-stream.d.ts +27 -0
- package/dist/platform/adapters/feishu-output-stream.js +114 -0
- package/dist/platform/adapters/feishu.d.ts +16 -7
- package/dist/platform/adapters/feishu.js +64 -18
- package/dist/platform/adapters/index.d.ts +4 -1
- package/dist/platform/adapters/index.js +1 -1
- package/dist/platform/adapters/slack-output-stream.d.ts +55 -0
- package/dist/platform/{virtual-message.js → adapters/slack-output-stream.js} +98 -165
- package/dist/platform/adapters/slack-project-conduits.d.ts +9 -0
- package/dist/platform/adapters/slack-project-conduits.js +40 -0
- package/dist/platform/adapters/slack.d.ts +18 -8
- package/dist/platform/adapters/slack.js +82 -24
- package/dist/platform/index.d.ts +3 -2
- package/dist/platform/index.js +1 -1
- package/dist/platform/output-stream-chunk.d.ts +6 -0
- package/dist/platform/output-stream-chunk.js +45 -0
- package/dist/platform/output-stream-helpers.d.ts +6 -0
- package/dist/platform/output-stream-helpers.js +11 -0
- package/dist/platform/output-stream.d.ts +28 -0
- package/dist/platform/output-stream.js +4 -0
- package/dist/platform/testing.d.ts +41 -8
- package/dist/platform/testing.js +93 -11
- package/dist/platform/tool-trace.d.ts +9 -8
- package/dist/platform/tool-trace.js +19 -17
- package/dist/platform/types.d.ts +21 -1
- package/dist/platform/types.js +17 -1
- package/dist/store/channel-repo.d.ts +6 -0
- package/dist/store/channel-repo.js +9 -0
- package/dist/store/json-repository.js +12 -1
- package/dist/store/outbound-queue.d.ts +5 -4
- package/dist/store/outbound-queue.js +9 -6
- package/dist/store/schedule-repo.d.ts +9 -5
- package/dist/store/schedule-repo.js +59 -14
- package/dist/store/session-registry-repo.d.ts +36 -7
- package/dist/store/session-registry-repo.js +186 -25
- package/dist/store/thread-repo.d.ts +1 -0
- package/dist/store/thread-repo.js +8 -0
- package/package.json +1 -1
- package/dist/platform/virtual-message.d.ts +0 -68
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// input: sync-public script + PlatformAdapter
|
|
2
|
+
// output: sync-public job runner — registers as 'sync-public'
|
|
3
|
+
// pos: periodic fetch + cherry-pick from public/main to main (non-LLM programmatic work)
|
|
4
|
+
import { register, ctx } from '../job-registry.js';
|
|
5
|
+
import { execSync } from 'child_process';
|
|
6
|
+
const SYNC_SCRIPT = '/home/fangxin/Cortex/scripts/sync-pull-from-public.sh';
|
|
7
|
+
// Self-register
|
|
8
|
+
register('sync-public', async (payload) => {
|
|
9
|
+
const { channel } = payload;
|
|
10
|
+
const adapter = ctx.adapter;
|
|
11
|
+
try {
|
|
12
|
+
const output = execSync(`bash "${SYNC_SCRIPT}"`, {
|
|
13
|
+
timeout: 30_000,
|
|
14
|
+
encoding: 'utf-8',
|
|
15
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
16
|
+
});
|
|
17
|
+
const lines = output.trim().split('\n');
|
|
18
|
+
const lastLine = lines[lines.length - 1] || '';
|
|
19
|
+
if (lastLine.includes('0 failed') || lastLine.includes('everything in sync') || lastLine.includes('OK:') || lastLine.includes('SKIP:')) {
|
|
20
|
+
// Quiet success — no message needed unless there were actual syncs
|
|
21
|
+
const countLine = lines.find(l => l.includes('cherry-picked'));
|
|
22
|
+
if (countLine && !countLine.includes('0 cherry-picked')) {
|
|
23
|
+
await adapter.postMessage({ type: 'interactive-reply', conduit: channel }, { text: `:arrows_counterclockwise: Public sync: ${countLine.trim()}` });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
await adapter.postMessage({ type: 'interactive-reply', conduit: channel }, { text: `:warning: Public sync issue:\n\`\`\`\n${output.slice(-500)}\n\`\`\`` });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
const msg = err?.stderr || err?.message || String(err);
|
|
32
|
+
await adapter.postMessage({ type: 'interactive-reply', conduit: channel }, { text: `:warning: Public sync error: ${msg.slice(0, 500)}` });
|
|
33
|
+
}
|
|
34
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ScheduleTarget, ScheduleTask } from '../../../store/schedule-repo.js';
|
|
2
2
|
import type { ThreadRecord } from '../../../core/types/thread-types.js';
|
|
3
|
-
import type {
|
|
3
|
+
import type { Session } from '../../../store/session-registry-repo.js';
|
|
4
4
|
/** What scheduled-task.ts should do at fire time. Each kind maps to one runtime branch:
|
|
5
5
|
* - fresh / default-thread → createDefaultThread + runThreadExec (default-path)
|
|
6
6
|
* - continue-thread → continueThread (resumes the existing thread's slot session)
|
|
@@ -22,19 +22,15 @@ export type DispatchPlan = {
|
|
|
22
22
|
reason: string;
|
|
23
23
|
};
|
|
24
24
|
export interface DispatchLookups {
|
|
25
|
-
/** Active thread (status running|waiting) for the channel, or null. */
|
|
26
|
-
findActiveThread(channel: string): ThreadRecord | null;
|
|
27
|
-
/** Channel-default sessionId (the one a fresh user message would resume), or undefined. */
|
|
28
|
-
getChannelSession(channel: string): Promise<string | undefined>;
|
|
29
25
|
/** Look up a cortex-XXXX session record. Null if it's been GC'd or never registered. */
|
|
30
|
-
lookupSession(sessionName: string): Promise<
|
|
26
|
+
lookupSession(sessionName: string): Promise<Session | null>;
|
|
31
27
|
/** Look up a thread record by id. Null if missing. */
|
|
32
28
|
getThread(threadId: string): ThreadRecord | null;
|
|
33
29
|
}
|
|
34
30
|
export interface DispatchPlanInput {
|
|
35
31
|
target: ScheduleTarget | undefined;
|
|
36
32
|
fallback: ScheduleTask['fallback'];
|
|
37
|
-
/** Channel to use when falling back to fresh — typically the schedule's own
|
|
33
|
+
/** Channel to use when falling back to fresh — typically the schedule's own resolved channel. */
|
|
38
34
|
fallbackChannel: string;
|
|
39
35
|
lookups: DispatchLookups;
|
|
40
36
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
// input: ScheduleTarget + lookup callbacks (
|
|
1
|
+
// input: ScheduleTarget + lookup callbacks (session/thread stores)
|
|
2
2
|
// output: planScheduledDispatch — pure decision tree picking how to land a fired schedule
|
|
3
|
-
// pos: extracted from scheduled-task.ts so the
|
|
3
|
+
// pos: extracted from scheduled-task.ts so the target dispatch is unit-testable
|
|
4
4
|
// without spinning up the platform adapter, execution registry, or thread runner.
|
|
5
|
+
// Removed channel variant in M4; project replaces it as the default target kind.
|
|
5
6
|
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
6
7
|
function applyFallback(input, reason) {
|
|
7
8
|
const policy = input.fallback ?? 'fresh';
|
|
@@ -16,13 +17,10 @@ export async function planScheduledDispatch(input) {
|
|
|
16
17
|
if (target.kind === 'fresh') {
|
|
17
18
|
return { kind: 'fresh', channel: input.fallbackChannel };
|
|
18
19
|
}
|
|
19
|
-
if (target.kind === '
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
const sessionId = (await input.lookups.getChannelSession(target.channel)) ?? null;
|
|
25
|
-
return { kind: 'default-thread', channel: target.channel, existingSessionId: sessionId };
|
|
20
|
+
if (target.kind === 'project') {
|
|
21
|
+
// Always spawn a fresh session in the project channel.
|
|
22
|
+
// The scheduler resolves projectId → channel before calling runScheduledTask.
|
|
23
|
+
return { kind: 'fresh', channel: input.fallbackChannel };
|
|
26
24
|
}
|
|
27
25
|
if (target.kind === 'session') {
|
|
28
26
|
const record = await input.lookups.lookupSession(target.sessionName);
|
|
@@ -10,10 +10,10 @@ register('task-archive', async (payload) => {
|
|
|
10
10
|
const adapter = ctx.adapter;
|
|
11
11
|
if (results.archived.length > 0) {
|
|
12
12
|
const summary = results.archived.map((r) => `*${r.project}*: archived ${r.ids.length} tasks`).join('\n');
|
|
13
|
-
await adapter.postMessage(channel, { text: `:file_folder: Task auto-archive:\n${summary}` });
|
|
13
|
+
await adapter.postMessage({ type: 'interactive-reply', conduit: channel }, { text: `:file_folder: Task auto-archive:\n${summary}` });
|
|
14
14
|
}
|
|
15
15
|
if (results.errors.length > 0) {
|
|
16
16
|
const errSummary = results.errors.join('\n');
|
|
17
|
-
await adapter.postMessage(channel, { text: `:warning: Task archiver errors:\n${errSummary}` });
|
|
17
|
+
await adapter.postMessage({ type: 'interactive-reply', conduit: channel }, { text: `:warning: Task archiver errors:\n${errSummary}` });
|
|
18
18
|
}
|
|
19
19
|
});
|
|
@@ -6,10 +6,9 @@ import { createLogger } from '../../../core/log.js';
|
|
|
6
6
|
import * as executionRegistry from '../../executions/registry.js';
|
|
7
7
|
const log = createLogger('task-dispatch');
|
|
8
8
|
import * as pendingTaskTracker from '../../tasks/pending-tracker.js';
|
|
9
|
-
import {
|
|
9
|
+
import { sessionStore } from '../../../store/session-registry-repo.js';
|
|
10
10
|
import { allConfigsRateLimited } from '../../agents/facade.js';
|
|
11
11
|
import { selectAndClaimTask, computeNextInterval, updateScheduleInterval } from '../../tasks/dispatcher.js';
|
|
12
|
-
import { channelRepo } from '../../../store/channel-repo.js';
|
|
13
12
|
import { taskMutator } from '../../tasks/mutator.js';
|
|
14
13
|
import { createThread } from '../../threads/index.js';
|
|
15
14
|
import { runThread as runThreadExec } from '../../threads/runner.js';
|
|
@@ -83,12 +82,11 @@ async function runDispatchAsync({ channel, scheduleTaskId, profileName }) {
|
|
|
83
82
|
}
|
|
84
83
|
async function executeDispatchTask({ selected, selectedTask, channel, scheduleTaskId, profileName, startTime }) {
|
|
85
84
|
const adapter = ctx.adapter;
|
|
86
|
-
const sessionName = await
|
|
85
|
+
const sessionName = await sessionStore.generateSessionName();
|
|
87
86
|
const effectiveProfile = profileName;
|
|
88
|
-
const effectiveChannel = (selectedTask.project && await channelRepo.getProjectChannel(selectedTask.project)) || channel;
|
|
89
87
|
let statusMsg = null;
|
|
90
88
|
try {
|
|
91
|
-
statusMsg = await adapter.postMessage(
|
|
89
|
+
statusMsg = await adapter.postMessage({ type: 'project-report', projectId: selectedTask.project || channel, trigger: 'task-dispatch', sessionId: '' }, {
|
|
92
90
|
text: `:satellite: Dispatching: [${selectedTask.project}] ${selectedTask.text.substring(0, 80)}... | ${sessionName} | ${effectiveProfile}`,
|
|
93
91
|
});
|
|
94
92
|
}
|
|
@@ -98,14 +96,16 @@ async function executeDispatchTask({ selected, selectedTask, channel, scheduleTa
|
|
|
98
96
|
await taskMutator.unclaim(selectedTask.id);
|
|
99
97
|
return { success: false, skipped: true, note: 'Task missing required [template:] tag' };
|
|
100
98
|
}
|
|
101
|
-
const thread = createThread(
|
|
99
|
+
const thread = createThread(channel, {
|
|
102
100
|
templateName: selected.template, userMessage: selected.prompt, userMessageTs: `dispatch_${Date.now()}`,
|
|
103
101
|
platformThreadId: statusMsg?.messageId ?? null,
|
|
104
|
-
|
|
102
|
+
projectId: selectedTask.project,
|
|
103
|
+
metadata: { scheduleTaskId, trigger: 'task-dispatch', profileOverride: effectiveProfile },
|
|
105
104
|
});
|
|
106
|
-
const icb = ctx.buildInteractiveCallbacks?.(
|
|
105
|
+
const icb = ctx.buildInteractiveCallbacks?.(channel, null);
|
|
107
106
|
const threadResult = await runThreadExec(thread.id, {
|
|
108
|
-
adapter, channel:
|
|
107
|
+
adapter, channel: channel, threadTs: statusMsg?.messageId || null, statusMsg, startTime, existingSessionId: null,
|
|
108
|
+
destination: { type: 'project-report', projectId: selectedTask.project || channel, trigger: 'task-dispatch', sessionId: '' },
|
|
109
109
|
onToolUse: icb?.onToolUse ?? null, onPlanWritten: icb?.onPlanWritten ?? null, onAskUserQuestion: icb?.onAskUserQuestion ?? null,
|
|
110
110
|
extraHooks: {
|
|
111
111
|
onEnd: {
|
|
@@ -120,7 +120,7 @@ async function executeDispatchTask({ selected, selectedTask, channel, scheduleTa
|
|
|
120
120
|
const { elapsedStr } = computeElapsed(startTime);
|
|
121
121
|
await taskMutator.unclaim(selectedTask.id);
|
|
122
122
|
if (statusMsg) {
|
|
123
|
-
const text = `:warning: [${selectedTask.project}] ${selectedTask.text.substring(0, 80)} | ${buildSessionTag(sessionName, result?.sessionId)}Rate limited — all fallbacks exhausted (${elapsedStr}
|
|
123
|
+
const text = `:warning: [${selectedTask.project}] ${selectedTask.text.substring(0, 80)} | ${buildSessionTag(sessionName, result?.sessionId)}Rate limited — all fallbacks exhausted (${elapsedStr})`;
|
|
124
124
|
const queue = getOutboundQueue();
|
|
125
125
|
if (queue) {
|
|
126
126
|
await durableUpdate(queue, adapter, statusMsg, { text });
|
|
@@ -131,7 +131,7 @@ async function executeDispatchTask({ selected, selectedTask, channel, scheduleTa
|
|
|
131
131
|
}
|
|
132
132
|
return { success: false, skipped: false, note: 'Rate limited — all fallbacks exhausted' };
|
|
133
133
|
}
|
|
134
|
-
await finalizeThreadSuccess(adapter,
|
|
134
|
+
await finalizeThreadSuccess(adapter, channel, statusMsg, {
|
|
135
135
|
startTime, sessionName, result, threadResult, project: selectedTask.project, trigger: 'task-dispatch',
|
|
136
136
|
label: selectedTask.text?.substring(0, 60) || null, sessionKind: 'scheduled',
|
|
137
137
|
statusPrefix: `Done: [${selectedTask.project}] ${selectedTask.text.substring(0, 80)}`,
|
|
@@ -151,7 +151,7 @@ async function handleDispatchError(error, selectedTask, channel) {
|
|
|
151
151
|
log.error(`Failed to unclaim task: ${e.message}`);
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
|
-
const errChannel =
|
|
154
|
+
const errChannel = channel;
|
|
155
155
|
let blocked = false;
|
|
156
156
|
let blockReason = null;
|
|
157
157
|
if (selectedTask?.task_hash) {
|
|
@@ -175,20 +175,22 @@ async function handleDispatchError(error, selectedTask, channel) {
|
|
|
175
175
|
const queue = getOutboundQueue();
|
|
176
176
|
if (blocked && selectedTask) {
|
|
177
177
|
const text = `:no_entry: Auto-blocked after ${DISPATCH_FAILURE_QUARANTINE_THRESHOLD} consecutive dispatch failures. Reason recorded in TASKS.yaml. Task: [${selectedTask.project}] ${String(selectedTask.text).substring(0, 80)}. Last error: ${error.message}. Unblock with \`cortex-task unblock --task-id ${selectedTask.id}\`.`;
|
|
178
|
+
const projDest = { type: 'project-report', projectId: selectedTask.project || errChannel, trigger: 'task-dispatch', sessionId: '' };
|
|
178
179
|
if (queue) {
|
|
179
|
-
await durablePost(queue, adapter,
|
|
180
|
+
await durablePost(queue, adapter, projDest, { text });
|
|
180
181
|
}
|
|
181
182
|
else {
|
|
182
|
-
await adapter.postMessage(
|
|
183
|
+
await adapter.postMessage(projDest, { text });
|
|
183
184
|
}
|
|
184
185
|
}
|
|
185
186
|
else {
|
|
186
187
|
const text = `:x: Task dispatch error: ${error.message}`;
|
|
188
|
+
const projDest = { type: 'project-report', projectId: selectedTask?.project || errChannel, trigger: 'task-dispatch', sessionId: '' };
|
|
187
189
|
if (queue) {
|
|
188
|
-
await durablePost(queue, adapter,
|
|
190
|
+
await durablePost(queue, adapter, projDest, { text });
|
|
189
191
|
}
|
|
190
192
|
else {
|
|
191
|
-
await adapter.postMessage(
|
|
193
|
+
await adapter.postMessage(projDest, { text });
|
|
192
194
|
}
|
|
193
195
|
}
|
|
194
196
|
}
|
|
@@ -2,6 +2,7 @@ import './jobs/scheduled-task.js';
|
|
|
2
2
|
import './jobs/task-dispatch.js';
|
|
3
3
|
import './jobs/memory-index-regen.js';
|
|
4
4
|
import './jobs/task-archive.js';
|
|
5
|
+
import './jobs/sync-public.js';
|
|
5
6
|
import { Scheduler } from './scheduler.js';
|
|
6
7
|
import type { EventBus } from '../../events/index.js';
|
|
7
8
|
import type { PlatformAdapter } from '../../platform/index.js';
|
|
@@ -8,6 +8,7 @@ import './jobs/scheduled-task.js';
|
|
|
8
8
|
import './jobs/task-dispatch.js';
|
|
9
9
|
import './jobs/memory-index-regen.js';
|
|
10
10
|
import './jobs/task-archive.js';
|
|
11
|
+
import './jobs/sync-public.js';
|
|
11
12
|
import { Scheduler } from './scheduler.js';
|
|
12
13
|
export function initScheduledRunner(adapter) { ctx.adapter = adapter; }
|
|
13
14
|
export function setSchedulerRef(s) { ctx.schedulerRef = s; }
|
|
@@ -17,6 +18,7 @@ export function createScheduler() {
|
|
|
17
18
|
const sched = new Scheduler(async (params) => { dispatch('scheduled-task', params); }, async (params) => { dispatch('task-dispatch', params); }, {
|
|
18
19
|
'memory-index-regen': async (params) => { dispatch('memory-index-regen', params); },
|
|
19
20
|
'task-archive': async (params) => { dispatch('task-archive', params); },
|
|
21
|
+
'sync-public': async (params) => { dispatch('sync-public', params); },
|
|
20
22
|
});
|
|
21
23
|
ctx.schedulerRef = sched;
|
|
22
24
|
return sched;
|
|
@@ -34,8 +34,8 @@ function parseUpdateArgs(args) {
|
|
|
34
34
|
patch.message = value;
|
|
35
35
|
else if (flag === '--profile')
|
|
36
36
|
patch.profile = value;
|
|
37
|
-
else if (flag === '--
|
|
38
|
-
patch.
|
|
37
|
+
else if (flag === '--project-id')
|
|
38
|
+
patch.projectId = value;
|
|
39
39
|
else if (flag === '--dispatch-type')
|
|
40
40
|
patch.dispatchType = value;
|
|
41
41
|
else if (flag === '--time')
|
|
@@ -70,7 +70,7 @@ function getScheduleHelp() {
|
|
|
70
70
|
options: [
|
|
71
71
|
{ flag: '--message <text>', description: 'Schedule message (for edit)' },
|
|
72
72
|
{ flag: '--profile <name>', description: 'Agent profile (for edit)' },
|
|
73
|
-
{ flag: '--
|
|
73
|
+
{ flag: '--project-id <id>', description: 'Project id (for edit)' },
|
|
74
74
|
{ flag: '--dispatch-type <type>', description: 'Dispatch type (for edit)' },
|
|
75
75
|
{ flag: '--time <HH:MM>', description: 'Time of day (for edit daily/weekly)' },
|
|
76
76
|
{ flag: '--day <day>', description: 'Day of week: sun-sat or 0-6 (for edit weekly)' },
|
|
@@ -152,7 +152,7 @@ async function runScheduleCli(args, options = {}) {
|
|
|
152
152
|
// Detect flag mode vs legacy positional mode
|
|
153
153
|
if (rest[0]?.startsWith('--')) {
|
|
154
154
|
// Flag mode: add --type interval --interval 30m --message "msg"
|
|
155
|
-
const addArgs = { type: null, interval: null, time: null, day: null, delay: null, message: null,
|
|
155
|
+
const addArgs = { type: null, interval: null, time: null, day: null, delay: null, message: null, projectId: options.projectId || 'general', profile: null };
|
|
156
156
|
for (let i = 0; i < rest.length; i += 2) {
|
|
157
157
|
const flag = rest[i], value = rest[i + 1];
|
|
158
158
|
if (!flag?.startsWith('--') || value == null)
|
|
@@ -169,8 +169,8 @@ async function runScheduleCli(args, options = {}) {
|
|
|
169
169
|
addArgs.delay = value;
|
|
170
170
|
else if (flag === '--message')
|
|
171
171
|
addArgs.message = value;
|
|
172
|
-
else if (flag === '--
|
|
173
|
-
addArgs.
|
|
172
|
+
else if (flag === '--project-id')
|
|
173
|
+
addArgs.projectId = value;
|
|
174
174
|
else if (flag === '--profile')
|
|
175
175
|
addArgs.profile = value;
|
|
176
176
|
else
|
|
@@ -186,12 +186,12 @@ async function runScheduleCli(args, options = {}) {
|
|
|
186
186
|
if (addArgs.type === 'interval') {
|
|
187
187
|
if (!addArgs.interval)
|
|
188
188
|
return fail('--interval is required for add --type interval');
|
|
189
|
-
return ok({ task: await scheduler.add('interval', { intervalMs: parseMaybeDuration(addArgs.interval), message: addArgs.message,
|
|
189
|
+
return ok({ task: await scheduler.add('interval', { intervalMs: parseMaybeDuration(addArgs.interval), message: addArgs.message, projectId: addArgs.projectId, profile: addArgs.profile }) });
|
|
190
190
|
}
|
|
191
191
|
if (addArgs.type === 'daily') {
|
|
192
192
|
if (!addArgs.time)
|
|
193
193
|
return fail('--time is required for add --type daily');
|
|
194
|
-
return ok({ task: await scheduler.add('daily', { time: addArgs.time, message: addArgs.message,
|
|
194
|
+
return ok({ task: await scheduler.add('daily', { time: addArgs.time, message: addArgs.message, projectId: addArgs.projectId, profile: addArgs.profile }) });
|
|
195
195
|
}
|
|
196
196
|
if (addArgs.type === 'weekly') {
|
|
197
197
|
if (!addArgs.day)
|
|
@@ -199,12 +199,12 @@ async function runScheduleCli(args, options = {}) {
|
|
|
199
199
|
if (!addArgs.time)
|
|
200
200
|
return fail('--time is required for add --type weekly');
|
|
201
201
|
const dayOfWeek = Number.isInteger(Number(addArgs.day)) ? Number(addArgs.day) : DAY_MAP[addArgs.day.toLowerCase()];
|
|
202
|
-
return ok({ task: await scheduler.add('weekly', { dayOfWeek, time: addArgs.time, message: addArgs.message,
|
|
202
|
+
return ok({ task: await scheduler.add('weekly', { dayOfWeek, time: addArgs.time, message: addArgs.message, projectId: addArgs.projectId, profile: addArgs.profile }) });
|
|
203
203
|
}
|
|
204
204
|
if (addArgs.type === 'once') {
|
|
205
205
|
if (!addArgs.delay)
|
|
206
206
|
return fail('--delay is required for add --type once');
|
|
207
|
-
return ok({ task: await scheduler.add('once', { delay: parseMaybeDuration(addArgs.delay), message: addArgs.message,
|
|
207
|
+
return ok({ task: await scheduler.add('once', { delay: parseMaybeDuration(addArgs.delay), message: addArgs.message, projectId: addArgs.projectId, profile: addArgs.profile }) });
|
|
208
208
|
}
|
|
209
209
|
return fail(`Unknown add type: '${addArgs.type}'. Valid types: interval, daily, weekly, once`);
|
|
210
210
|
}
|
|
@@ -216,7 +216,7 @@ async function runScheduleCli(args, options = {}) {
|
|
|
216
216
|
const err = assertValidMessage(message);
|
|
217
217
|
if (err)
|
|
218
218
|
return fail(err);
|
|
219
|
-
return ok({ task: await scheduler.add('interval', { intervalMs, message,
|
|
219
|
+
return ok({ task: await scheduler.add('interval', { intervalMs, message, projectId: options.projectId || 'general', profile: null }) });
|
|
220
220
|
}
|
|
221
221
|
if (type === 'daily') {
|
|
222
222
|
const time = rest[1];
|
|
@@ -224,7 +224,7 @@ async function runScheduleCli(args, options = {}) {
|
|
|
224
224
|
const err = assertValidMessage(message);
|
|
225
225
|
if (err)
|
|
226
226
|
return fail(err);
|
|
227
|
-
return ok({ task: await scheduler.add('daily', { time, message,
|
|
227
|
+
return ok({ task: await scheduler.add('daily', { time, message, projectId: options.projectId || 'general', profile: null }) });
|
|
228
228
|
}
|
|
229
229
|
if (type === 'weekly') {
|
|
230
230
|
const dayValue = rest[1];
|
|
@@ -234,7 +234,7 @@ async function runScheduleCli(args, options = {}) {
|
|
|
234
234
|
const err = assertValidMessage(message);
|
|
235
235
|
if (err)
|
|
236
236
|
return fail(err);
|
|
237
|
-
return ok({ task: await scheduler.add('weekly', { dayOfWeek, time, message,
|
|
237
|
+
return ok({ task: await scheduler.add('weekly', { dayOfWeek, time, message, projectId: options.projectId || 'general', profile: null }) });
|
|
238
238
|
}
|
|
239
239
|
if (type === 'once') {
|
|
240
240
|
const delay = parseMaybeDuration(rest[1]);
|
|
@@ -242,7 +242,7 @@ async function runScheduleCli(args, options = {}) {
|
|
|
242
242
|
const err = assertValidMessage(message);
|
|
243
243
|
if (err)
|
|
244
244
|
return fail(err);
|
|
245
|
-
return ok({ task: await scheduler.add('once', { delay, message,
|
|
245
|
+
return ok({ task: await scheduler.add('once', { delay, message, projectId: options.projectId || 'general', profile: null }) });
|
|
246
246
|
}
|
|
247
247
|
return fail(`Unknown add type: '${type}'. Valid types: interval, daily, weekly, once`);
|
|
248
248
|
}
|
|
@@ -3,7 +3,12 @@
|
|
|
3
3
|
// pos: !schedule subcommand routing and profile pinning
|
|
4
4
|
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
5
|
import { formatDuration, formatTimeUntil, parseDuration } from './scheduler.js';
|
|
6
|
+
import { channelToProjectId } from '../../store/schedule-repo.js';
|
|
6
7
|
import { listProfiles, resolveProfile, getDefaultProfileName } from '../agents/profile-manager.js';
|
|
8
|
+
/** Convert a raw channel string to a Destination for postMessage calls. */
|
|
9
|
+
function toDest(channel) {
|
|
10
|
+
return { type: 'interactive-reply', conduit: channel };
|
|
11
|
+
}
|
|
7
12
|
function scheduleHelp() {
|
|
8
13
|
const profileNames = listProfiles().map(p => `\`${p.name}\``).join(', ');
|
|
9
14
|
return [
|
|
@@ -25,12 +30,12 @@ const fmtTime = (ts) => new Date(ts).toLocaleString('en-US', FMT_OPTS);
|
|
|
25
30
|
async function handleList(parts, channel, adapter, scheduler) {
|
|
26
31
|
const tasks = await scheduler.list();
|
|
27
32
|
if (tasks.length === 0) {
|
|
28
|
-
await adapter.postMessage(channel, { text: 'No scheduled tasks.' });
|
|
33
|
+
await adapter.postMessage(toDest(channel), { text: 'No scheduled tasks.' });
|
|
29
34
|
return;
|
|
30
35
|
}
|
|
31
36
|
const now = Date.now();
|
|
32
37
|
const lines = tasks.map((t) => formatTaskLine(t, now));
|
|
33
|
-
await adapter.postMessage(channel, { text: `*Scheduled tasks (${tasks.length}):*\n${lines.join('\n')}` });
|
|
38
|
+
await adapter.postMessage(toDest(channel), { text: `*Scheduled tasks (${tasks.length}):*\n${lines.join('\n')}` });
|
|
34
39
|
}
|
|
35
40
|
function formatTaskLine(t, now) {
|
|
36
41
|
const id = `\`${t.id}\``;
|
|
@@ -69,11 +74,11 @@ async function handleRemove(parts, channel, adapter, scheduler) {
|
|
|
69
74
|
const rawId = parts[2];
|
|
70
75
|
const id = normalizeTaskId(rawId);
|
|
71
76
|
if (!id) {
|
|
72
|
-
await adapter.postMessage(channel, { text: 'Usage: `!schedule remove <id>`' });
|
|
77
|
+
await adapter.postMessage(toDest(channel), { text: 'Usage: `!schedule remove <id>`' });
|
|
73
78
|
return;
|
|
74
79
|
}
|
|
75
80
|
const removed = await scheduler.remove(id);
|
|
76
|
-
await adapter.postMessage(channel, {
|
|
81
|
+
await adapter.postMessage(toDest(channel), {
|
|
77
82
|
text: removed ? `:white_check_mark: Removed task \`${id}\`.` : `:x: Task not found: \`${id}\``,
|
|
78
83
|
});
|
|
79
84
|
}
|
|
@@ -81,39 +86,39 @@ async function handlePause(parts, channel, adapter, scheduler) {
|
|
|
81
86
|
const rawId = parts[2];
|
|
82
87
|
const id = normalizeTaskId(rawId);
|
|
83
88
|
if (!id) {
|
|
84
|
-
await adapter.postMessage(channel, { text: 'Usage: `!schedule pause <id>`' });
|
|
89
|
+
await adapter.postMessage(toDest(channel), { text: 'Usage: `!schedule pause <id>`' });
|
|
85
90
|
return;
|
|
86
91
|
}
|
|
87
92
|
try {
|
|
88
93
|
const task = await scheduler.pause(id);
|
|
89
94
|
if (!task) {
|
|
90
|
-
await adapter.postMessage(channel, { text: `:x: Task not found: \`${id}\`` });
|
|
95
|
+
await adapter.postMessage(toDest(channel), { text: `:x: Task not found: \`${id}\`` });
|
|
91
96
|
return;
|
|
92
97
|
}
|
|
93
|
-
await adapter.postMessage(channel, { text: `:double_vertical_bar: Paused task \`${id}\`.` });
|
|
98
|
+
await adapter.postMessage(toDest(channel), { text: `:double_vertical_bar: Paused task \`${id}\`.` });
|
|
94
99
|
}
|
|
95
100
|
catch (error) {
|
|
96
|
-
await adapter.postMessage(channel, { text: `:x: ${error.message}` });
|
|
101
|
+
await adapter.postMessage(toDest(channel), { text: `:x: ${error.message}` });
|
|
97
102
|
}
|
|
98
103
|
}
|
|
99
104
|
async function handleResume(parts, channel, adapter, scheduler) {
|
|
100
105
|
const rawId = parts[2];
|
|
101
106
|
const id = normalizeTaskId(rawId);
|
|
102
107
|
if (!id) {
|
|
103
|
-
await adapter.postMessage(channel, { text: 'Usage: `!schedule resume <id>`' });
|
|
108
|
+
await adapter.postMessage(toDest(channel), { text: 'Usage: `!schedule resume <id>`' });
|
|
104
109
|
return;
|
|
105
110
|
}
|
|
106
111
|
try {
|
|
107
112
|
const task = await scheduler.resume(id);
|
|
108
113
|
if (!task) {
|
|
109
|
-
await adapter.postMessage(channel, { text: `:x: Task not found: \`${id}\`` });
|
|
114
|
+
await adapter.postMessage(toDest(channel), { text: `:x: Task not found: \`${id}\`` });
|
|
110
115
|
return;
|
|
111
116
|
}
|
|
112
117
|
const nextAt = task.nextRun ? ` Next run: ${fmtTime(task.nextRun)}.` : '';
|
|
113
|
-
await adapter.postMessage(channel, { text: `:arrow_forward: Resumed task \`${id}\`.${nextAt}` });
|
|
118
|
+
await adapter.postMessage(toDest(channel), { text: `:arrow_forward: Resumed task \`${id}\`.${nextAt}` });
|
|
114
119
|
}
|
|
115
120
|
catch (error) {
|
|
116
|
-
await adapter.postMessage(channel, { text: `:x: ${error.message}` });
|
|
121
|
+
await adapter.postMessage(toDest(channel), { text: `:x: ${error.message}` });
|
|
117
122
|
}
|
|
118
123
|
}
|
|
119
124
|
function parseProfileArgs(args, usage) {
|
|
@@ -145,22 +150,23 @@ async function handleAddInterval(parts, channel, adapter, scheduler) {
|
|
|
145
150
|
parsed = parseProfileArgs(parts.slice(4), '`!schedule add interval <duration> [--profile <name>] <message>`');
|
|
146
151
|
}
|
|
147
152
|
catch (error) {
|
|
148
|
-
await adapter.postMessage(channel, { text: `:x: ${error.message}` });
|
|
153
|
+
await adapter.postMessage(toDest(channel), { text: `:x: ${error.message}` });
|
|
149
154
|
return;
|
|
150
155
|
}
|
|
151
156
|
const msg = parsed.rest.join(' ');
|
|
152
157
|
const ms = parseDuration(durationStr);
|
|
153
158
|
if (!ms) {
|
|
154
|
-
await adapter.postMessage(channel, { text: `Invalid duration: \`${durationStr}\`. Use e.g. \`30s\`, \`5m\`, \`2h\`, \`1d\`` });
|
|
159
|
+
await adapter.postMessage(toDest(channel), { text: `Invalid duration: \`${durationStr}\`. Use e.g. \`30s\`, \`5m\`, \`2h\`, \`1d\`` });
|
|
155
160
|
return;
|
|
156
161
|
}
|
|
157
162
|
if (!msg) {
|
|
158
|
-
await adapter.postMessage(channel, { text: 'Usage: `!schedule add interval <duration> [--profile <name>] <message>`' });
|
|
163
|
+
await adapter.postMessage(toDest(channel), { text: 'Usage: `!schedule add interval <duration> [--profile <name>] <message>`' });
|
|
159
164
|
return;
|
|
160
165
|
}
|
|
161
|
-
const
|
|
166
|
+
const resolvedProjectId = channelToProjectId(channel) ?? 'general';
|
|
167
|
+
const task = await scheduler.add('interval', { intervalMs: ms, message: msg, projectId: resolvedProjectId, profile: parsed.profileName });
|
|
162
168
|
const nextAt = task.nextRun ? ` | first run: ${fmtTime(task.nextRun)}` : '';
|
|
163
|
-
await adapter.postMessage(channel, { text: buildScheduledConfirmation(`:white_check_mark: Scheduled every *${formatDuration(ms)}*: "${msg}"`, task, nextAt) });
|
|
169
|
+
await adapter.postMessage(toDest(channel), { text: buildScheduledConfirmation(`:white_check_mark: Scheduled every *${formatDuration(ms)}*: "${msg}"`, task, nextAt) });
|
|
164
170
|
}
|
|
165
171
|
async function handleAddDaily(parts, channel, adapter, scheduler) {
|
|
166
172
|
const time = parts[3];
|
|
@@ -169,21 +175,22 @@ async function handleAddDaily(parts, channel, adapter, scheduler) {
|
|
|
169
175
|
parsed = parseProfileArgs(parts.slice(4), '`!schedule add daily <HH:MM> [--profile <name>] <message>`');
|
|
170
176
|
}
|
|
171
177
|
catch (error) {
|
|
172
|
-
await adapter.postMessage(channel, { text: `:x: ${error.message}` });
|
|
178
|
+
await adapter.postMessage(toDest(channel), { text: `:x: ${error.message}` });
|
|
173
179
|
return;
|
|
174
180
|
}
|
|
175
181
|
const msg = parsed.rest.join(' ');
|
|
176
182
|
if (!/^\d{2}:\d{2}$/.test(time)) {
|
|
177
|
-
await adapter.postMessage(channel, { text: `Invalid time: \`${time}\`. Use 24-hour HH:MM, e.g. \`09:00\`` });
|
|
183
|
+
await adapter.postMessage(toDest(channel), { text: `Invalid time: \`${time}\`. Use 24-hour HH:MM, e.g. \`09:00\`` });
|
|
178
184
|
return;
|
|
179
185
|
}
|
|
180
186
|
if (!msg) {
|
|
181
|
-
await adapter.postMessage(channel, { text: 'Usage: `!schedule add daily <HH:MM> [--profile <name>] <message>`' });
|
|
187
|
+
await adapter.postMessage(toDest(channel), { text: 'Usage: `!schedule add daily <HH:MM> [--profile <name>] <message>`' });
|
|
182
188
|
return;
|
|
183
189
|
}
|
|
184
|
-
const
|
|
190
|
+
const resolvedProjectId = channelToProjectId(channel) ?? 'general';
|
|
191
|
+
const task = await scheduler.add('daily', { time, message: msg, projectId: resolvedProjectId, profile: parsed.profileName });
|
|
185
192
|
const nextAt = task.nextRun ? ` | first run: ${fmtTime(task.nextRun)}` : '';
|
|
186
|
-
await adapter.postMessage(channel, { text: buildScheduledConfirmation(`:white_check_mark: Scheduled daily at *${time}*: "${msg}"`, task, nextAt) });
|
|
193
|
+
await adapter.postMessage(toDest(channel), { text: buildScheduledConfirmation(`:white_check_mark: Scheduled daily at *${time}*: "${msg}"`, task, nextAt) });
|
|
187
194
|
}
|
|
188
195
|
async function handleAddWeekly(parts, channel, adapter, scheduler) {
|
|
189
196
|
const dayStr = (parts[3] || '').toLowerCase();
|
|
@@ -193,27 +200,28 @@ async function handleAddWeekly(parts, channel, adapter, scheduler) {
|
|
|
193
200
|
parsed = parseProfileArgs(parts.slice(5), '`!schedule add weekly <day> <HH:MM> [--profile <name>] <message>`');
|
|
194
201
|
}
|
|
195
202
|
catch (error) {
|
|
196
|
-
await adapter.postMessage(channel, { text: `:x: ${error.message}` });
|
|
203
|
+
await adapter.postMessage(toDest(channel), { text: `:x: ${error.message}` });
|
|
197
204
|
return;
|
|
198
205
|
}
|
|
199
206
|
const msg = parsed.rest.join(' ');
|
|
200
207
|
const dayOfWeek = DAY_MAP[dayStr];
|
|
201
208
|
if (dayOfWeek == null) {
|
|
202
|
-
await adapter.postMessage(channel, { text: `Invalid day: \`${parts[3]}\`. Use: mon, tue, wed, thu, fri, sat, sun` });
|
|
209
|
+
await adapter.postMessage(toDest(channel), { text: `Invalid day: \`${parts[3]}\`. Use: mon, tue, wed, thu, fri, sat, sun` });
|
|
203
210
|
return;
|
|
204
211
|
}
|
|
205
212
|
if (!/^\d{2}:\d{2}$/.test(time)) {
|
|
206
|
-
await adapter.postMessage(channel, { text: `Invalid time: \`${time}\`. Use 24-hour HH:MM, e.g. \`21:00\`` });
|
|
213
|
+
await adapter.postMessage(toDest(channel), { text: `Invalid time: \`${time}\`. Use 24-hour HH:MM, e.g. \`21:00\`` });
|
|
207
214
|
return;
|
|
208
215
|
}
|
|
209
216
|
if (!msg) {
|
|
210
|
-
await adapter.postMessage(channel, { text: 'Usage: `!schedule add weekly <day> <HH:MM> [--profile <name>] <message>`' });
|
|
217
|
+
await adapter.postMessage(toDest(channel), { text: 'Usage: `!schedule add weekly <day> <HH:MM> [--profile <name>] <message>`' });
|
|
211
218
|
return;
|
|
212
219
|
}
|
|
213
220
|
const dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
214
|
-
const
|
|
221
|
+
const resolvedProjectId = channelToProjectId(channel) ?? 'general';
|
|
222
|
+
const task = await scheduler.add('weekly', { dayOfWeek, time, message: msg, projectId: resolvedProjectId, profile: parsed.profileName });
|
|
215
223
|
const nextAt = task.nextRun ? ` | first run: ${fmtTime(task.nextRun)}` : '';
|
|
216
|
-
await adapter.postMessage(channel, { text: buildScheduledConfirmation(`:white_check_mark: Scheduled weekly *${dayNames[dayOfWeek]} ${time}*: "${msg}"`, task, nextAt) });
|
|
224
|
+
await adapter.postMessage(toDest(channel), { text: buildScheduledConfirmation(`:white_check_mark: Scheduled weekly *${dayNames[dayOfWeek]} ${time}*: "${msg}"`, task, nextAt) });
|
|
217
225
|
}
|
|
218
226
|
async function handleAddOnce(parts, channel, adapter, scheduler) {
|
|
219
227
|
const durationStr = parts[3];
|
|
@@ -222,22 +230,23 @@ async function handleAddOnce(parts, channel, adapter, scheduler) {
|
|
|
222
230
|
parsed = parseProfileArgs(parts.slice(4), '`!schedule add once <duration> [--profile <name>] <message>`');
|
|
223
231
|
}
|
|
224
232
|
catch (error) {
|
|
225
|
-
await adapter.postMessage(channel, { text: `:x: ${error.message}` });
|
|
233
|
+
await adapter.postMessage(toDest(channel), { text: `:x: ${error.message}` });
|
|
226
234
|
return;
|
|
227
235
|
}
|
|
228
236
|
const msg = parsed.rest.join(' ');
|
|
229
237
|
const ms = parseDuration(durationStr);
|
|
230
238
|
if (!ms) {
|
|
231
|
-
await adapter.postMessage(channel, { text: `Invalid duration: \`${durationStr}\`. Use e.g. \`30s\`, \`5m\`, \`2h\`, \`1d\`` });
|
|
239
|
+
await adapter.postMessage(toDest(channel), { text: `Invalid duration: \`${durationStr}\`. Use e.g. \`30s\`, \`5m\`, \`2h\`, \`1d\`` });
|
|
232
240
|
return;
|
|
233
241
|
}
|
|
234
242
|
if (!msg) {
|
|
235
|
-
await adapter.postMessage(channel, { text: 'Usage: `!schedule add once <duration> [--profile <name>] <message>`' });
|
|
243
|
+
await adapter.postMessage(toDest(channel), { text: 'Usage: `!schedule add once <duration> [--profile <name>] <message>`' });
|
|
236
244
|
return;
|
|
237
245
|
}
|
|
238
|
-
const
|
|
246
|
+
const resolvedProjectId = channelToProjectId(channel) ?? 'general';
|
|
247
|
+
const task = await scheduler.add('once', { delay: ms, message: msg, projectId: resolvedProjectId, profile: parsed.profileName });
|
|
239
248
|
const runAt = task.runAt ? ` | runs at: ${fmtTime(task.runAt)}` : '';
|
|
240
|
-
await adapter.postMessage(channel, { text: buildScheduledConfirmation(`:white_check_mark: Scheduled once in *${formatDuration(ms)}*: "${msg}"`, task, runAt) });
|
|
249
|
+
await adapter.postMessage(toDest(channel), { text: buildScheduledConfirmation(`:white_check_mark: Scheduled once in *${formatDuration(ms)}*: "${msg}"`, task, runAt) });
|
|
241
250
|
}
|
|
242
251
|
const ADD_TYPE_HANDLERS = {
|
|
243
252
|
interval: handleAddInterval,
|
|
@@ -251,10 +260,10 @@ async function handleAdd(parts, channel, adapter, scheduler) {
|
|
|
251
260
|
if (handler)
|
|
252
261
|
return handler(parts, channel, adapter, scheduler);
|
|
253
262
|
if (type) {
|
|
254
|
-
await adapter.postMessage(channel, { text: `Unknown type: \`${type}\`. Use \`interval\`, \`daily\`, \`weekly\`, or \`once\`` });
|
|
263
|
+
await adapter.postMessage(toDest(channel), { text: `Unknown type: \`${type}\`. Use \`interval\`, \`daily\`, \`weekly\`, or \`once\`` });
|
|
255
264
|
return;
|
|
256
265
|
}
|
|
257
|
-
await adapter.postMessage(channel, { text: scheduleHelp() });
|
|
266
|
+
await adapter.postMessage(toDest(channel), { text: scheduleHelp() });
|
|
258
267
|
}
|
|
259
268
|
const SUB_HANDLERS = {
|
|
260
269
|
list: handleList,
|
|
@@ -269,6 +278,6 @@ async function handleScheduleCommand(text, channel, adapter, scheduler) {
|
|
|
269
278
|
const handler = SUB_HANDLERS[sub];
|
|
270
279
|
if (handler)
|
|
271
280
|
return handler(parts, channel, adapter, scheduler);
|
|
272
|
-
await adapter.postMessage(channel, { text: scheduleHelp() });
|
|
281
|
+
await adapter.postMessage(toDest(channel), { text: scheduleHelp() });
|
|
273
282
|
}
|
|
274
283
|
export { handleScheduleCommand };
|
|
@@ -6,7 +6,7 @@ declare function formatDuration(ms: number): string;
|
|
|
6
6
|
declare function formatTimeUntil(ms: number): string;
|
|
7
7
|
interface RunnerParams {
|
|
8
8
|
message: string;
|
|
9
|
-
|
|
9
|
+
projectId: string;
|
|
10
10
|
scheduleTaskId: string;
|
|
11
11
|
profileName: string;
|
|
12
12
|
/** Where the fired task should land — passed through so scheduled-task.ts can pick its dispatch branch. */
|