morpheus-cli 0.5.1 → 0.5.2
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.
|
@@ -162,6 +162,10 @@ export const startCommand = new Command('start')
|
|
|
162
162
|
taskWorker.start();
|
|
163
163
|
taskNotifier.start();
|
|
164
164
|
}
|
|
165
|
+
// Recover webhook notifications stuck in 'pending' from previous runs
|
|
166
|
+
WebhookDispatcher.recoverStale().catch((err) => {
|
|
167
|
+
display.log(`Webhook recovery error: ${err.message}`, { source: 'Webhooks', level: 'error' });
|
|
168
|
+
});
|
|
165
169
|
// Handle graceful shutdown
|
|
166
170
|
const shutdown = async (signal) => {
|
|
167
171
|
display.stopSpinner();
|
|
@@ -148,6 +148,10 @@ export class TaskRepository {
|
|
|
148
148
|
const row = this.db.prepare('SELECT * FROM tasks WHERE id = ?').get(id);
|
|
149
149
|
return row ? this.deserializeTask(row) : null;
|
|
150
150
|
}
|
|
151
|
+
findTaskByOriginMessageId(originMessageId) {
|
|
152
|
+
const row = this.db.prepare('SELECT * FROM tasks WHERE origin_message_id = ? LIMIT 1').get(originMessageId);
|
|
153
|
+
return row ? this.deserializeTask(row) : null;
|
|
154
|
+
}
|
|
151
155
|
listTasks(filters) {
|
|
152
156
|
const params = [];
|
|
153
157
|
let query = 'SELECT * FROM tasks WHERE 1=1';
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { WebhookRepository } from './repository.js';
|
|
2
|
+
import { TaskRepository } from '../tasks/repository.js';
|
|
2
3
|
import { DisplayManager } from '../display.js';
|
|
4
|
+
const STALE_NOTIFICATION_THRESHOLD_MS = 2 * 60 * 1000; // 2 minutes
|
|
3
5
|
export class WebhookDispatcher {
|
|
4
6
|
static telegramAdapter = null;
|
|
5
7
|
static oracle = null;
|
|
@@ -35,17 +37,35 @@ export class WebhookDispatcher {
|
|
|
35
37
|
}
|
|
36
38
|
const message = this.buildPrompt(webhook.prompt, payload);
|
|
37
39
|
try {
|
|
38
|
-
await oracle.chat(message, undefined, false, {
|
|
40
|
+
const response = await oracle.chat(message, undefined, false, {
|
|
39
41
|
origin_channel: 'webhook',
|
|
40
42
|
session_id: `webhook-${webhook.id}`,
|
|
41
43
|
origin_message_id: notificationId,
|
|
42
44
|
});
|
|
43
|
-
|
|
45
|
+
// Check whether Oracle delegated a task for this notification.
|
|
46
|
+
// If a task exists with this origin_message_id, TaskNotifier will update
|
|
47
|
+
// the notification when the task completes. If not (Oracle answered
|
|
48
|
+
// directly), mark it completed now with the direct response.
|
|
49
|
+
const taskRepo = TaskRepository.getInstance();
|
|
50
|
+
const delegatedTask = taskRepo.findTaskByOriginMessageId(notificationId);
|
|
51
|
+
if (delegatedTask) {
|
|
52
|
+
this.display.log(`Webhook "${webhook.name}" accepted and queued (notification: ${notificationId})`, { source: 'Webhooks', level: 'success' });
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
repo.updateNotificationResult(notificationId, 'completed', response);
|
|
56
|
+
this.display.log(`Webhook "${webhook.name}" completed with direct response (notification: ${notificationId})`, { source: 'Webhooks', level: 'success' });
|
|
57
|
+
if (webhook.notification_channels.includes('telegram')) {
|
|
58
|
+
await this.sendTelegram(webhook.name, response, 'completed');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
44
61
|
}
|
|
45
62
|
catch (err) {
|
|
46
63
|
const result = `Execution error: ${err.message}`;
|
|
47
64
|
this.display.log(`Webhook "${webhook.name}" failed: ${err.message}`, { source: 'Webhooks', level: 'error' });
|
|
48
65
|
repo.updateNotificationResult(notificationId, 'failed', result);
|
|
66
|
+
if (webhook.notification_channels.includes('telegram')) {
|
|
67
|
+
await this.sendTelegram(webhook.name, result, 'failed');
|
|
68
|
+
}
|
|
49
69
|
}
|
|
50
70
|
}
|
|
51
71
|
/**
|
|
@@ -63,6 +83,57 @@ ${payloadStr}
|
|
|
63
83
|
|
|
64
84
|
Analyze the payload above and follow the instructions provided. Be concise and actionable in your response.`;
|
|
65
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Called at startup to re-dispatch webhook notifications that got stuck in
|
|
88
|
+
* 'pending' status (e.g. from a previous crash or from the direct-response
|
|
89
|
+
* bug). Skips notifications that already have an active task running.
|
|
90
|
+
*/
|
|
91
|
+
static async recoverStale() {
|
|
92
|
+
const display = DisplayManager.getInstance();
|
|
93
|
+
const oracle = WebhookDispatcher.oracle;
|
|
94
|
+
if (!oracle) {
|
|
95
|
+
display.log('Webhook recovery skipped — Oracle not available.', {
|
|
96
|
+
source: 'Webhooks',
|
|
97
|
+
level: 'warning',
|
|
98
|
+
});
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const repo = WebhookRepository.getInstance();
|
|
102
|
+
const taskRepo = TaskRepository.getInstance();
|
|
103
|
+
const stale = repo.findStaleNotifications(STALE_NOTIFICATION_THRESHOLD_MS);
|
|
104
|
+
if (stale.length === 0)
|
|
105
|
+
return;
|
|
106
|
+
display.log(`Recovering ${stale.length} stale webhook notification(s)...`, {
|
|
107
|
+
source: 'Webhooks',
|
|
108
|
+
level: 'warning',
|
|
109
|
+
});
|
|
110
|
+
for (const notification of stale) {
|
|
111
|
+
// Skip if a task is still active for this notification
|
|
112
|
+
const activeTask = taskRepo.findTaskByOriginMessageId(notification.id);
|
|
113
|
+
if (activeTask && (activeTask.status === 'pending' || activeTask.status === 'running')) {
|
|
114
|
+
display.log(`Webhook notification ${notification.id} has active task ${activeTask.id} — skipping recovery.`, { source: 'Webhooks' });
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const webhook = repo.getWebhookById(notification.webhook_id);
|
|
118
|
+
if (!webhook || !webhook.enabled) {
|
|
119
|
+
repo.updateNotificationResult(notification.id, 'failed', webhook ? 'Webhook was disabled.' : 'Webhook no longer exists.');
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
let payload;
|
|
123
|
+
try {
|
|
124
|
+
payload = JSON.parse(notification.payload);
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
payload = {};
|
|
128
|
+
}
|
|
129
|
+
display.log(`Re-dispatching stale notification ${notification.id} for webhook "${webhook.name}".`, { source: 'Webhooks' });
|
|
130
|
+
// Fire-and-forget re-dispatch (same pattern as the trigger endpoint)
|
|
131
|
+
const dispatcher = new WebhookDispatcher();
|
|
132
|
+
dispatcher.dispatch(webhook, payload, notification.id).catch((err) => {
|
|
133
|
+
display.log(`Recovery dispatch error for notification ${notification.id}: ${err.message}`, { source: 'Webhooks', level: 'error' });
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
66
137
|
/**
|
|
67
138
|
* Sends a formatted Telegram message to all allowed users.
|
|
68
139
|
* Silently skips if the adapter is not connected.
|
|
@@ -180,6 +180,13 @@ export class WebhookRepository {
|
|
|
180
180
|
const row = this.db.prepare('SELECT COUNT(*) as cnt FROM webhook_notifications WHERE read = 0').get();
|
|
181
181
|
return row?.cnt ?? 0;
|
|
182
182
|
}
|
|
183
|
+
findStaleNotifications(olderThanMs) {
|
|
184
|
+
const cutoff = Date.now() - olderThanMs;
|
|
185
|
+
const rows = this.db.prepare(`SELECT * FROM webhook_notifications
|
|
186
|
+
WHERE status = 'pending' AND created_at < ?
|
|
187
|
+
ORDER BY created_at ASC`).all(cutoff);
|
|
188
|
+
return rows.map(this.deserializeNotification);
|
|
189
|
+
}
|
|
183
190
|
deserializeNotification(row) {
|
|
184
191
|
return {
|
|
185
192
|
id: row.id,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "morpheus-cli",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Morpheus is a local AI agent for developers, running as a CLI daemon that connects to LLMs, local tools, and MCPs, enabling interaction via Terminal, Telegram, and Discord. Inspired by the character Morpheus from *The Matrix*, the project acts as an intelligent orchestrator, bridging the gap between the developer and complex systems.",
|
|
5
5
|
"bin": {
|
|
6
6
|
"morpheus": "./bin/morpheus.js"
|