@agenticmail/openclaw 0.5.46 → 0.5.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +99 -0
- package/dist/index.js +99 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -3078,6 +3078,105 @@ function activate(api) {
|
|
|
3078
3078
|
if (api?.registerService) {
|
|
3079
3079
|
api.registerService(createMailMonitorService(ctx));
|
|
3080
3080
|
}
|
|
3081
|
+
{
|
|
3082
|
+
const gatewayPort = process.env.OPENCLAW_PORT || process.env.PORT || "3000";
|
|
3083
|
+
const agentApiKey = ctx.config.apiKey;
|
|
3084
|
+
const sseUrl = `${ctx.config.apiUrl}/api/agenticmail/events`;
|
|
3085
|
+
let sseRetryMs = 5e3;
|
|
3086
|
+
let sseController = null;
|
|
3087
|
+
const startMainWatcher = () => {
|
|
3088
|
+
if (sseController) return;
|
|
3089
|
+
sseController = new AbortController();
|
|
3090
|
+
(async () => {
|
|
3091
|
+
try {
|
|
3092
|
+
const res = await fetch(sseUrl, {
|
|
3093
|
+
headers: { "Authorization": `Bearer ${agentApiKey}`, "Accept": "text/event-stream" },
|
|
3094
|
+
signal: sseController.signal
|
|
3095
|
+
});
|
|
3096
|
+
if (!res.ok || !res.body) return;
|
|
3097
|
+
sseRetryMs = 5e3;
|
|
3098
|
+
const reader = res.body.getReader();
|
|
3099
|
+
const decoder = new TextDecoder();
|
|
3100
|
+
let buffer = "";
|
|
3101
|
+
try {
|
|
3102
|
+
while (true) {
|
|
3103
|
+
const { done, value } = await reader.read();
|
|
3104
|
+
if (done) break;
|
|
3105
|
+
buffer += decoder.decode(value, { stream: true });
|
|
3106
|
+
let boundary;
|
|
3107
|
+
while ((boundary = buffer.indexOf("\n\n")) !== -1) {
|
|
3108
|
+
const frame = buffer.slice(0, boundary);
|
|
3109
|
+
buffer = buffer.slice(boundary + 2);
|
|
3110
|
+
for (const line of frame.split("\n")) {
|
|
3111
|
+
if (line.startsWith("data: ")) {
|
|
3112
|
+
try {
|
|
3113
|
+
const event = JSON.parse(line.slice(6));
|
|
3114
|
+
if (event.type === "new" && event.uid) {
|
|
3115
|
+
const from = event.from ?? "unknown";
|
|
3116
|
+
const subject = event.subject ?? "(no subject)";
|
|
3117
|
+
const wakeText = `New email received from ${from}: "${subject}". Read it with agenticmail_read(uid=${event.uid}), assess urgency, and decide: if urgent or time-sensitive, notify the user now. Otherwise, note it in memory and batch-notify later.`;
|
|
3118
|
+
const hooksToken = process.env.OPENCLAW_HOOKS_TOKEN;
|
|
3119
|
+
let wakeSuccess = false;
|
|
3120
|
+
if (hooksToken) {
|
|
3121
|
+
try {
|
|
3122
|
+
const hookUrl = `http://127.0.0.1:${gatewayPort}/hooks/agent`;
|
|
3123
|
+
const resp = await fetch(hookUrl, {
|
|
3124
|
+
method: "POST",
|
|
3125
|
+
headers: {
|
|
3126
|
+
"Content-Type": "application/json",
|
|
3127
|
+
"Authorization": `Bearer ${hooksToken}`
|
|
3128
|
+
},
|
|
3129
|
+
body: JSON.stringify({
|
|
3130
|
+
event: "system",
|
|
3131
|
+
text: wakeText,
|
|
3132
|
+
sessionTarget: "main"
|
|
3133
|
+
}),
|
|
3134
|
+
signal: AbortSignal.timeout(5e3)
|
|
3135
|
+
});
|
|
3136
|
+
if (resp.ok) {
|
|
3137
|
+
wakeSuccess = true;
|
|
3138
|
+
console.log(`[agenticmail] Wake event sent via webhook for new email from ${from}: "${subject}"`);
|
|
3139
|
+
}
|
|
3140
|
+
} catch {
|
|
3141
|
+
}
|
|
3142
|
+
}
|
|
3143
|
+
if (!wakeSuccess && api?.callTool) {
|
|
3144
|
+
try {
|
|
3145
|
+
await api.callTool("cron", { action: "wake", text: wakeText, mode: "now" });
|
|
3146
|
+
wakeSuccess = true;
|
|
3147
|
+
console.log(`[agenticmail] Wake event sent via cron tool for new email from ${from}: "${subject}"`);
|
|
3148
|
+
} catch {
|
|
3149
|
+
}
|
|
3150
|
+
}
|
|
3151
|
+
if (!wakeSuccess) {
|
|
3152
|
+
console.warn(`[agenticmail] Could not send wake event for email from ${from}: "${subject}"`);
|
|
3153
|
+
}
|
|
3154
|
+
}
|
|
3155
|
+
} catch {
|
|
3156
|
+
}
|
|
3157
|
+
}
|
|
3158
|
+
}
|
|
3159
|
+
}
|
|
3160
|
+
}
|
|
3161
|
+
} finally {
|
|
3162
|
+
try {
|
|
3163
|
+
reader.cancel();
|
|
3164
|
+
} catch {
|
|
3165
|
+
}
|
|
3166
|
+
}
|
|
3167
|
+
} catch (err) {
|
|
3168
|
+
if (err.name !== "AbortError") {
|
|
3169
|
+
console.warn(`[agenticmail] Main email watcher error: ${err.message}`);
|
|
3170
|
+
}
|
|
3171
|
+
} finally {
|
|
3172
|
+
sseController = null;
|
|
3173
|
+
sseRetryMs = Math.min(sseRetryMs * 1.5, 6e4);
|
|
3174
|
+
setTimeout(startMainWatcher, sseRetryMs);
|
|
3175
|
+
}
|
|
3176
|
+
})();
|
|
3177
|
+
};
|
|
3178
|
+
setTimeout(startMainWatcher, 3e3);
|
|
3179
|
+
}
|
|
3081
3180
|
if (api?.registerCommand) {
|
|
3082
3181
|
api.registerCommand({
|
|
3083
3182
|
name: "agenticmail",
|
package/dist/index.js
CHANGED
|
@@ -3042,6 +3042,105 @@ function activate(api) {
|
|
|
3042
3042
|
if (api?.registerService) {
|
|
3043
3043
|
api.registerService(createMailMonitorService(ctx));
|
|
3044
3044
|
}
|
|
3045
|
+
{
|
|
3046
|
+
const gatewayPort = process.env.OPENCLAW_PORT || process.env.PORT || "3000";
|
|
3047
|
+
const agentApiKey = ctx.config.apiKey;
|
|
3048
|
+
const sseUrl = `${ctx.config.apiUrl}/api/agenticmail/events`;
|
|
3049
|
+
let sseRetryMs = 5e3;
|
|
3050
|
+
let sseController = null;
|
|
3051
|
+
const startMainWatcher = () => {
|
|
3052
|
+
if (sseController) return;
|
|
3053
|
+
sseController = new AbortController();
|
|
3054
|
+
(async () => {
|
|
3055
|
+
try {
|
|
3056
|
+
const res = await fetch(sseUrl, {
|
|
3057
|
+
headers: { "Authorization": `Bearer ${agentApiKey}`, "Accept": "text/event-stream" },
|
|
3058
|
+
signal: sseController.signal
|
|
3059
|
+
});
|
|
3060
|
+
if (!res.ok || !res.body) return;
|
|
3061
|
+
sseRetryMs = 5e3;
|
|
3062
|
+
const reader = res.body.getReader();
|
|
3063
|
+
const decoder = new TextDecoder();
|
|
3064
|
+
let buffer = "";
|
|
3065
|
+
try {
|
|
3066
|
+
while (true) {
|
|
3067
|
+
const { done, value } = await reader.read();
|
|
3068
|
+
if (done) break;
|
|
3069
|
+
buffer += decoder.decode(value, { stream: true });
|
|
3070
|
+
let boundary;
|
|
3071
|
+
while ((boundary = buffer.indexOf("\n\n")) !== -1) {
|
|
3072
|
+
const frame = buffer.slice(0, boundary);
|
|
3073
|
+
buffer = buffer.slice(boundary + 2);
|
|
3074
|
+
for (const line of frame.split("\n")) {
|
|
3075
|
+
if (line.startsWith("data: ")) {
|
|
3076
|
+
try {
|
|
3077
|
+
const event = JSON.parse(line.slice(6));
|
|
3078
|
+
if (event.type === "new" && event.uid) {
|
|
3079
|
+
const from = event.from ?? "unknown";
|
|
3080
|
+
const subject = event.subject ?? "(no subject)";
|
|
3081
|
+
const wakeText = `New email received from ${from}: "${subject}". Read it with agenticmail_read(uid=${event.uid}), assess urgency, and decide: if urgent or time-sensitive, notify the user now. Otherwise, note it in memory and batch-notify later.`;
|
|
3082
|
+
const hooksToken = process.env.OPENCLAW_HOOKS_TOKEN;
|
|
3083
|
+
let wakeSuccess = false;
|
|
3084
|
+
if (hooksToken) {
|
|
3085
|
+
try {
|
|
3086
|
+
const hookUrl = `http://127.0.0.1:${gatewayPort}/hooks/agent`;
|
|
3087
|
+
const resp = await fetch(hookUrl, {
|
|
3088
|
+
method: "POST",
|
|
3089
|
+
headers: {
|
|
3090
|
+
"Content-Type": "application/json",
|
|
3091
|
+
"Authorization": `Bearer ${hooksToken}`
|
|
3092
|
+
},
|
|
3093
|
+
body: JSON.stringify({
|
|
3094
|
+
event: "system",
|
|
3095
|
+
text: wakeText,
|
|
3096
|
+
sessionTarget: "main"
|
|
3097
|
+
}),
|
|
3098
|
+
signal: AbortSignal.timeout(5e3)
|
|
3099
|
+
});
|
|
3100
|
+
if (resp.ok) {
|
|
3101
|
+
wakeSuccess = true;
|
|
3102
|
+
console.log(`[agenticmail] Wake event sent via webhook for new email from ${from}: "${subject}"`);
|
|
3103
|
+
}
|
|
3104
|
+
} catch {
|
|
3105
|
+
}
|
|
3106
|
+
}
|
|
3107
|
+
if (!wakeSuccess && api?.callTool) {
|
|
3108
|
+
try {
|
|
3109
|
+
await api.callTool("cron", { action: "wake", text: wakeText, mode: "now" });
|
|
3110
|
+
wakeSuccess = true;
|
|
3111
|
+
console.log(`[agenticmail] Wake event sent via cron tool for new email from ${from}: "${subject}"`);
|
|
3112
|
+
} catch {
|
|
3113
|
+
}
|
|
3114
|
+
}
|
|
3115
|
+
if (!wakeSuccess) {
|
|
3116
|
+
console.warn(`[agenticmail] Could not send wake event for email from ${from}: "${subject}"`);
|
|
3117
|
+
}
|
|
3118
|
+
}
|
|
3119
|
+
} catch {
|
|
3120
|
+
}
|
|
3121
|
+
}
|
|
3122
|
+
}
|
|
3123
|
+
}
|
|
3124
|
+
}
|
|
3125
|
+
} finally {
|
|
3126
|
+
try {
|
|
3127
|
+
reader.cancel();
|
|
3128
|
+
} catch {
|
|
3129
|
+
}
|
|
3130
|
+
}
|
|
3131
|
+
} catch (err) {
|
|
3132
|
+
if (err.name !== "AbortError") {
|
|
3133
|
+
console.warn(`[agenticmail] Main email watcher error: ${err.message}`);
|
|
3134
|
+
}
|
|
3135
|
+
} finally {
|
|
3136
|
+
sseController = null;
|
|
3137
|
+
sseRetryMs = Math.min(sseRetryMs * 1.5, 6e4);
|
|
3138
|
+
setTimeout(startMainWatcher, sseRetryMs);
|
|
3139
|
+
}
|
|
3140
|
+
})();
|
|
3141
|
+
};
|
|
3142
|
+
setTimeout(startMainWatcher, 3e3);
|
|
3143
|
+
}
|
|
3045
3144
|
if (api?.registerCommand) {
|
|
3046
3145
|
api.registerCommand({
|
|
3047
3146
|
name: "agenticmail",
|
package/package.json
CHANGED