@geminixiang/mama 0.2.0-beta.2 → 0.2.0-beta.3
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 +67 -39
- package/dist/adapter.d.ts +14 -4
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js.map +1 -1
- package/dist/adapters/discord/bot.d.ts +8 -5
- package/dist/adapters/discord/bot.d.ts.map +1 -1
- package/dist/adapters/discord/bot.js +210 -42
- package/dist/adapters/discord/bot.js.map +1 -1
- package/dist/adapters/discord/context.d.ts.map +1 -1
- package/dist/adapters/discord/context.js +83 -21
- package/dist/adapters/discord/context.js.map +1 -1
- package/dist/adapters/shared.d.ts +23 -0
- package/dist/adapters/shared.d.ts.map +1 -0
- package/dist/adapters/shared.js +57 -0
- package/dist/adapters/shared.js.map +1 -0
- package/dist/adapters/slack/bot.d.ts +8 -7
- package/dist/adapters/slack/bot.d.ts.map +1 -1
- package/dist/adapters/slack/bot.js +161 -27
- package/dist/adapters/slack/bot.js.map +1 -1
- package/dist/adapters/slack/branch-manager.d.ts +21 -0
- package/dist/adapters/slack/branch-manager.d.ts.map +1 -0
- package/dist/adapters/slack/branch-manager.js +96 -0
- package/dist/adapters/slack/branch-manager.js.map +1 -0
- package/dist/adapters/slack/context.d.ts.map +1 -1
- package/dist/adapters/slack/context.js +92 -56
- package/dist/adapters/slack/context.js.map +1 -1
- package/dist/adapters/slack/session.d.ts +3 -0
- package/dist/adapters/slack/session.d.ts.map +1 -0
- package/dist/adapters/slack/session.js +16 -0
- package/dist/adapters/slack/session.js.map +1 -0
- package/dist/adapters/telegram/bot.d.ts.map +1 -1
- package/dist/adapters/telegram/bot.js +11 -3
- package/dist/adapters/telegram/bot.js.map +1 -1
- package/dist/adapters/telegram/context.d.ts.map +1 -1
- package/dist/adapters/telegram/context.js +40 -14
- package/dist/adapters/telegram/context.js.map +1 -1
- package/dist/agent.d.ts +2 -1
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +69 -142
- package/dist/agent.js.map +1 -1
- package/dist/config.d.ts +2 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +13 -1
- package/dist/config.js.map +1 -1
- package/dist/context.d.ts +11 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +100 -16
- package/dist/context.js.map +1 -1
- package/dist/events.d.ts +7 -0
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +61 -30
- package/dist/events.js.map +1 -1
- package/dist/{login.d.ts → login/index.d.ts} +1 -1
- package/dist/login/index.d.ts.map +1 -0
- package/dist/{login.js → login/index.js} +1 -1
- package/dist/login/index.js.map +1 -0
- package/dist/{link-server.d.ts → login/portal.d.ts} +5 -4
- package/dist/login/portal.d.ts.map +1 -0
- package/dist/login/portal.js +1453 -0
- package/dist/login/portal.js.map +1 -0
- package/dist/{link-token.d.ts → login/session.d.ts} +1 -1
- package/dist/login/session.d.ts.map +1 -0
- package/dist/{link-token.js → login/session.js} +1 -1
- package/dist/login/session.js.map +1 -0
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +87 -15
- package/dist/main.js.map +1 -1
- package/dist/provisioner.d.ts +17 -2
- package/dist/provisioner.d.ts.map +1 -1
- package/dist/provisioner.js +84 -5
- package/dist/provisioner.js.map +1 -1
- package/dist/session-policy.d.ts +13 -0
- package/dist/session-policy.d.ts.map +1 -0
- package/dist/session-policy.js +23 -0
- package/dist/session-policy.js.map +1 -0
- package/dist/session-store.d.ts +26 -0
- package/dist/session-store.d.ts.map +1 -1
- package/dist/session-store.js +157 -0
- package/dist/session-store.js.map +1 -1
- package/dist/session-view/command.d.ts +5 -0
- package/dist/session-view/command.d.ts.map +1 -0
- package/dist/session-view/command.js +11 -0
- package/dist/session-view/command.js.map +1 -0
- package/dist/session-view/portal.d.ts +9 -0
- package/dist/session-view/portal.d.ts.map +1 -0
- package/dist/session-view/portal.js +766 -0
- package/dist/session-view/portal.js.map +1 -0
- package/dist/session-view/service.d.ts +34 -0
- package/dist/session-view/service.d.ts.map +1 -0
- package/dist/session-view/service.js +380 -0
- package/dist/session-view/service.js.map +1 -0
- package/dist/session-view/store.d.ts +16 -0
- package/dist/session-view/store.d.ts.map +1 -0
- package/dist/session-view/store.js +38 -0
- package/dist/session-view/store.js.map +1 -0
- package/dist/store.d.ts +3 -6
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +15 -35
- package/dist/store.js.map +1 -1
- package/dist/tools/event.d.ts +2 -0
- package/dist/tools/event.d.ts.map +1 -1
- package/dist/tools/event.js +21 -3
- package/dist/tools/event.js.map +1 -1
- package/dist/tools/index.d.ts +2 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/link-server.d.ts.map +0 -1
- package/dist/link-server.js +0 -899
- package/dist/link-server.js.map +0 -1
- package/dist/link-token.d.ts.map +0 -1
- package/dist/link-token.js.map +0 -1
- package/dist/login.d.ts.map +0 -1
- package/dist/login.js.map +0 -1
package/dist/context.js
CHANGED
|
@@ -25,7 +25,7 @@ const DEFAULT_SYNC_DAYS = 10;
|
|
|
25
25
|
*
|
|
26
26
|
* @param sessionManager - The SessionManager to sync to
|
|
27
27
|
* @param conversationDir - Path to the conversation directory containing log.jsonl
|
|
28
|
-
* @param excludeSlackTs -
|
|
28
|
+
* @param excludeSlackTs - Current platform message ID/timestamp (will be added via prompt(), not sync)
|
|
29
29
|
* @param timeRange - Optional time range to filter log entries (defaults to last 10 days)
|
|
30
30
|
* @param threadFilter - Optional thread filter to scope sync to a specific thread
|
|
31
31
|
* @returns Number of messages synced
|
|
@@ -38,16 +38,33 @@ export async function syncLogToSessionManager(sessionManager, conversationDir, e
|
|
|
38
38
|
const logFile = join(conversationDir, "log.jsonl");
|
|
39
39
|
if (!existsSync(logFile))
|
|
40
40
|
return 0;
|
|
41
|
-
// Build
|
|
42
|
-
//
|
|
43
|
-
|
|
41
|
+
// Build a list of existing session messages for dedupe.
|
|
42
|
+
// Live user prompts carry a formatted timestamp in the text and use Date.now(),
|
|
43
|
+
// while log.jsonl uses the platform event timestamp. We therefore need a small
|
|
44
|
+
// fuzzy match window in addition to the exact timestamp/content match used for
|
|
45
|
+
// already-synced log entries.
|
|
46
|
+
const existingMessages = [];
|
|
47
|
+
const existingMessageKeys = new Set();
|
|
44
48
|
for (const entry of sessionManager.getEntries()) {
|
|
45
|
-
if (entry.type
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
if (entry.type !== "message")
|
|
50
|
+
continue;
|
|
51
|
+
const msgEntry = entry;
|
|
52
|
+
const message = msgEntry.message;
|
|
53
|
+
const contentText = Array.isArray(message.content)
|
|
54
|
+
? message.content
|
|
55
|
+
.filter((part) => part.type === "text")
|
|
56
|
+
.map((part) => part.text)
|
|
57
|
+
.join("\n\n")
|
|
58
|
+
: typeof message.content === "string"
|
|
59
|
+
? message.content
|
|
60
|
+
: "";
|
|
61
|
+
existingMessages.push({
|
|
62
|
+
timestamp: typeof message.timestamp === "number" ? message.timestamp : undefined,
|
|
63
|
+
rawText: contentText,
|
|
64
|
+
normalizedText: normalizeComparableUserText(contentText),
|
|
65
|
+
});
|
|
66
|
+
if (typeof message.timestamp === "number") {
|
|
67
|
+
existingMessageKeys.add(`${message.timestamp}:${contentText}`);
|
|
51
68
|
}
|
|
52
69
|
}
|
|
53
70
|
// Read log.jsonl and find user messages not in context
|
|
@@ -64,6 +81,11 @@ export async function syncLogToSessionManager(sessionManager, conversationDir, e
|
|
|
64
81
|
// Skip the current message being processed (will be added via prompt())
|
|
65
82
|
if (excludeSlackTs && slackTs === excludeSlackTs)
|
|
66
83
|
continue;
|
|
84
|
+
// While queued messages are being processed, newer messages may already be present
|
|
85
|
+
// in log.jsonl. Do not look ahead into those future messages when building the
|
|
86
|
+
// current turn's context.
|
|
87
|
+
if (!isMessageAtOrBeforeCurrent(slackTs, excludeSlackTs))
|
|
88
|
+
continue;
|
|
67
89
|
// Skip bot messages - added through agent flow
|
|
68
90
|
if (logMsg.isBot)
|
|
69
91
|
continue;
|
|
@@ -92,15 +114,15 @@ export async function syncLogToSessionManager(sessionManager, conversationDir, e
|
|
|
92
114
|
}
|
|
93
115
|
}
|
|
94
116
|
}
|
|
95
|
-
// Skip if this Slack timestamp is already in the session (dedupe by ts, not content)
|
|
96
|
-
// Convert Slack ts (e.g., "1234567890.123456") to Unix ms for comparison
|
|
97
|
-
const slackTsMs = Math.floor(parseFloat(slackTs) * 1000).toString();
|
|
98
|
-
if (existingTimestamps.has(slackTsMs))
|
|
99
|
-
continue;
|
|
100
117
|
// Build the message text as it would appear in context
|
|
101
118
|
const threadContext = logMsg.threadTs ? ` [in-thread:${logMsg.threadTs}]` : "";
|
|
102
119
|
const messageText = `[${logMsg.userName || logMsg.user || "unknown"}]${threadContext}: ${logMsg.text || ""}`;
|
|
103
120
|
const msgTime = new Date(date).getTime() || Date.now();
|
|
121
|
+
const messageKey = `${msgTime}:${messageText}`;
|
|
122
|
+
if (existingMessageKeys.has(messageKey))
|
|
123
|
+
continue;
|
|
124
|
+
if (hasExistingSessionMessage(existingMessages, msgTime, messageText))
|
|
125
|
+
continue;
|
|
104
126
|
// Skip messages outside the time range
|
|
105
127
|
if (msgTime < range.start || msgTime > range.end)
|
|
106
128
|
continue;
|
|
@@ -110,7 +132,12 @@ export async function syncLogToSessionManager(sessionManager, conversationDir, e
|
|
|
110
132
|
timestamp: msgTime,
|
|
111
133
|
};
|
|
112
134
|
newMessages.push({ timestamp: msgTime, message: userMessage });
|
|
113
|
-
|
|
135
|
+
existingMessages.push({
|
|
136
|
+
timestamp: msgTime,
|
|
137
|
+
rawText: messageText,
|
|
138
|
+
normalizedText: normalizeComparableUserText(messageText),
|
|
139
|
+
});
|
|
140
|
+
existingMessageKeys.add(messageKey); // Track to avoid duplicates within this sync
|
|
114
141
|
}
|
|
115
142
|
catch {
|
|
116
143
|
// Skip malformed lines
|
|
@@ -134,4 +161,61 @@ export async function syncLogToSessionManager(sessionManager, conversationDir, e
|
|
|
134
161
|
export function createMamaSettingsManager(_workspaceDir) {
|
|
135
162
|
return SettingsManager.inMemory();
|
|
136
163
|
}
|
|
164
|
+
export async function findLogMessageById(conversationDir, messageId) {
|
|
165
|
+
const logFile = join(conversationDir, "log.jsonl");
|
|
166
|
+
if (!existsSync(logFile))
|
|
167
|
+
return null;
|
|
168
|
+
const logContent = await readFile(logFile, "utf-8");
|
|
169
|
+
const logLines = logContent.trim().split("\n").filter(Boolean);
|
|
170
|
+
for (let i = logLines.length - 1; i >= 0; i--) {
|
|
171
|
+
try {
|
|
172
|
+
const entry = JSON.parse(logLines[i]);
|
|
173
|
+
if (entry.ts === messageId) {
|
|
174
|
+
return entry;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
// Skip malformed lines
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
function stripSlackAttachmentBlock(text) {
|
|
184
|
+
return text.replace(/\n*<slack_attachments>\n[\s\S]*?\n<\/slack_attachments>\s*$/g, "");
|
|
185
|
+
}
|
|
186
|
+
function normalizeComparableUserText(text) {
|
|
187
|
+
const withoutTimestamp = text.replace(/^\[[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}[+-][0-9]{2}:[0-9]{2}\]\s+(?=\[[^\]]+\](?:\s+\[in-thread:[^\]]+\])?:\s)/, "");
|
|
188
|
+
return stripSlackAttachmentBlock(withoutTimestamp).trim();
|
|
189
|
+
}
|
|
190
|
+
function hasExistingSessionMessage(existingMessages, timestamp, text) {
|
|
191
|
+
const normalizedText = normalizeComparableUserText(text);
|
|
192
|
+
return existingMessages.some((existing) => {
|
|
193
|
+
if (existing.timestamp === timestamp && existing.rawText === text) {
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
if (existing.normalizedText !== normalizedText || existing.timestamp === undefined) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
return existing.timestamp >= timestamp;
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
function isMessageAtOrBeforeCurrent(messageId, currentMessageId) {
|
|
203
|
+
if (!currentMessageId)
|
|
204
|
+
return true;
|
|
205
|
+
const comparison = compareMessageIds(messageId, currentMessageId);
|
|
206
|
+
return comparison === null || comparison <= 0;
|
|
207
|
+
}
|
|
208
|
+
function compareMessageIds(a, b) {
|
|
209
|
+
if (/^\d+$/.test(a) && /^\d+$/.test(b)) {
|
|
210
|
+
const left = BigInt(a);
|
|
211
|
+
const right = BigInt(b);
|
|
212
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
213
|
+
}
|
|
214
|
+
const left = Number(a);
|
|
215
|
+
const right = Number(b);
|
|
216
|
+
if (Number.isFinite(left) && Number.isFinite(right)) {
|
|
217
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
218
|
+
}
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
137
221
|
//# sourceMappingURL=context.js.map
|
package/dist/context.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAGL,eAAe,GAChB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAChC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAc5B;;GAEG;AACH,MAAM,iBAAiB,GAAG,EAAE,CAAC;AA0B7B;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,cAA8B,EAC9B,eAAuB,EACvB,cAAuB,EACvB,SAAqB,EACrB,YAA2B;IAE3B,8DAA8D;IAC9D,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACvB,MAAM,YAAY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two data sources per conversation:\n * - sessions/*.jsonl: Structured session history for agent context\n * - log.jsonl: Human-readable conversation history for grep (no tool results)\n *\n * This module provides:\n * - syncLogToSessionManager: Syncs messages from log.jsonl to SessionManager\n * - createMamaSettingsManager: Creates an in-memory SettingsManager for AgentSession\n */\n\nimport type { UserMessage } from \"@mariozechner/pi-ai\";\nimport {\n type SessionManager,\n type SessionMessageEntry,\n SettingsManager,\n} from \"@mariozechner/pi-coding-agent\";\nimport { existsSync } from \"fs\";\nimport { readFile } from \"fs/promises\";\nimport { join } from \"path\";\n\n// ============================================================================\n// Sync log.jsonl to SessionManager\n// ============================================================================\n\n/**\n * Time range for filtering log messages\n */\nexport interface TimeRange {\n start: number; // Unix timestamp in ms\n end: number;\n}\n\n/**\n * Default number of days to sync when no time range is specified\n */\nconst DEFAULT_SYNC_DAYS = 10;\n\ninterface LogMessage {\n date?: string;\n ts?: string;\n threadTs?: string;\n user?: string;\n userName?: string;\n text?: string;\n isBot?: boolean;\n}\n\n/**\n * Thread filter for scoping log sync to a specific thread session.\n * When provided, only messages belonging to this thread are synced,\n * preventing cross-thread context contamination.\n */\nexport interface ThreadFilter {\n /** Filter mode: a specific thread, or top-level messages only for persistent channel/chat sessions */\n scope?: \"thread\" | \"top-level\";\n /** The root message timestamp (user's original message ts, derived from sessionKey) */\n rootTs: string;\n /** The thread anchor timestamp (bot's first reply ts, used as thread_ts by Slack replies) */\n threadTs?: string;\n}\n\n/**\n * Sync user messages from log.jsonl to SessionManager.\n *\n * This ensures that messages logged while mama wasn't running (conversation chatter,\n * backfilled messages, messages while busy) are added to the LLM context.\n *\n * @param sessionManager - The SessionManager to sync to\n * @param conversationDir - Path to the conversation directory containing log.jsonl\n * @param excludeSlackTs - Slack timestamp of current message (will be added via prompt(), not sync)\n * @param timeRange - Optional time range to filter log entries (defaults to last 10 days)\n * @param threadFilter - Optional thread filter to scope sync to a specific thread\n * @returns Number of messages synced\n */\nexport async function syncLogToSessionManager(\n sessionManager: SessionManager,\n conversationDir: string,\n excludeSlackTs?: string,\n timeRange?: TimeRange,\n threadFilter?: ThreadFilter,\n): Promise<number> {\n // Calculate default time range (last 10 days) if not provided\n const now = Date.now();\n const defaultStart = now - DEFAULT_SYNC_DAYS * 24 * 60 * 60 * 1000;\n const range = timeRange ?? { start: defaultStart, end: now };\n const logFile = join(conversationDir, \"log.jsonl\");\n\n if (!existsSync(logFile)) return 0;\n\n // Build set of existing timestamps from session entries\n // We use ts (Slack timestamp) as the unique key instead of message content\n const existingTimestamps = new Set<string>();\n for (const entry of sessionManager.getEntries()) {\n if (entry.type === \"message\") {\n const msgEntry = entry as SessionMessageEntry;\n // SessionMessageEntry has a timestamp field (number, Unix ms)\n if (msgEntry.timestamp) {\n existingTimestamps.add(msgEntry.timestamp.toString());\n }\n }\n }\n\n // Read log.jsonl and find user messages not in context\n const logContent = await readFile(logFile, \"utf-8\");\n const logLines = logContent.trim().split(\"\\n\").filter(Boolean);\n\n const newMessages: Array<{ timestamp: number; message: UserMessage }> = [];\n\n for (const line of logLines) {\n try {\n const logMsg: LogMessage = JSON.parse(line);\n\n const slackTs = logMsg.ts;\n const date = logMsg.date;\n if (!slackTs || !date) continue;\n\n // Skip the current message being processed (will be added via prompt())\n if (excludeSlackTs && slackTs === excludeSlackTs) continue;\n\n // Skip bot messages - added through agent flow\n if (logMsg.isBot) continue;\n\n // Thread filtering: only sync messages belonging to this session's thread\n if (threadFilter) {\n if (threadFilter.scope === \"top-level\") {\n // Persistent top-level sessions should only ingest top-level messages.\n // This avoids pulling in unrelated replies from other threads.\n if (logMsg.threadTs) {\n continue;\n }\n } else {\n if (logMsg.threadTs) {\n // Thread reply: only include if threadTs matches our thread anchor or rootTs\n if (\n logMsg.threadTs !== threadFilter.threadTs &&\n logMsg.threadTs !== threadFilter.rootTs\n ) {\n continue;\n }\n } else {\n // Top-level message: only include if it's this session's root message\n if (slackTs !== threadFilter.rootTs) {\n continue;\n }\n }\n }\n }\n\n // Skip if this Slack timestamp is already in the session (dedupe by ts, not content)\n // Convert Slack ts (e.g., \"1234567890.123456\") to Unix ms for comparison\n const slackTsMs = Math.floor(parseFloat(slackTs) * 1000).toString();\n if (existingTimestamps.has(slackTsMs)) continue;\n\n // Build the message text as it would appear in context\n const threadContext = logMsg.threadTs ? ` [in-thread:${logMsg.threadTs}]` : \"\";\n const messageText = `[${logMsg.userName || logMsg.user || \"unknown\"}]${threadContext}: ${logMsg.text || \"\"}`;\n\n const msgTime = new Date(date).getTime() || Date.now();\n\n // Skip messages outside the time range\n if (msgTime < range.start || msgTime > range.end) continue;\n\n const userMessage: UserMessage = {\n role: \"user\",\n content: [{ type: \"text\", text: messageText }],\n timestamp: msgTime,\n };\n\n newMessages.push({ timestamp: msgTime, message: userMessage });\n existingTimestamps.add(slackTsMs); // Track to avoid duplicates within this sync\n } catch {\n // Skip malformed lines\n }\n }\n\n if (newMessages.length === 0) return 0;\n\n // Sort by timestamp and add to session\n newMessages.sort((a, b) => a.timestamp - b.timestamp);\n\n for (const { message } of newMessages) {\n sessionManager.appendMessage(message);\n }\n\n return newMessages.length;\n}\n\n// ============================================================================\n// Settings manager for mama\n// ============================================================================\n\n// Mama manages model/provider config through its own config.ts / settings.json.\n// We use an in-memory SettingsManager so AgentSession has valid defaults\n// without interfering with coding-agent's global settings files.\nexport function createMamaSettingsManager(_workspaceDir: string): SettingsManager {\n return SettingsManager.inMemory();\n}\n"]}
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAGL,eAAe,GAChB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAChC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAc5B;;GAEG;AACH,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAgC7B;;;;;;;;;;;;GAYG;AACH,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OAAO,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAClD,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["/**\n * Context management for mama.\n *\n * Mama uses two data sources per conversation:\n * - sessions/*.jsonl: Structured session history for agent context\n * - log.jsonl: Human-readable conversation history for grep (no tool results)\n *\n * This module provides:\n * - syncLogToSessionManager: Syncs messages from log.jsonl to SessionManager\n * - createMamaSettingsManager: Creates an in-memory SettingsManager for AgentSession\n */\n\nimport type { Message, UserMessage } from \"@mariozechner/pi-ai\";\nimport {\n type SessionManager,\n type SessionMessageEntry,\n SettingsManager,\n} from \"@mariozechner/pi-coding-agent\";\nimport { existsSync } from \"fs\";\nimport { readFile } from \"fs/promises\";\nimport { join } from \"path\";\n\n// ============================================================================\n// Sync log.jsonl to SessionManager\n// ============================================================================\n\n/**\n * Time range for filtering log messages\n */\nexport interface TimeRange {\n start: number; // Unix timestamp in ms\n end: number;\n}\n\n/**\n * Default number of days to sync when no time range is specified\n */\nconst DEFAULT_SYNC_DAYS = 10;\n\nexport interface ConversationLogMessage {\n date?: string;\n ts?: string;\n threadTs?: string;\n user?: string;\n userName?: string;\n text?: string;\n isBot?: boolean;\n}\n\ninterface ExistingSessionMessage {\n timestamp?: number;\n rawText: string;\n normalizedText: string;\n}\n\n/**\n * Thread filter for scoping log sync to a specific thread session.\n * When provided, only messages belonging to this thread are synced,\n * preventing cross-thread context contamination.\n */\nexport interface ThreadFilter {\n /** Filter mode: a specific thread, or top-level messages only for persistent channel/chat sessions */\n scope?: \"thread\" | \"top-level\";\n /** The root message timestamp (user's original message ts, derived from sessionKey) */\n rootTs: string;\n /** The thread anchor timestamp (bot's first reply ts, used as thread_ts by Slack replies) */\n threadTs?: string;\n}\n\n/**\n * Sync user messages from log.jsonl to SessionManager.\n *\n * This ensures that messages logged while mama wasn't running (conversation chatter,\n * backfilled messages, messages while busy) are added to the LLM context.\n *\n * @param sessionManager - The SessionManager to sync to\n * @param conversationDir - Path to the conversation directory containing log.jsonl\n * @param excludeSlackTs - Current platform message ID/timestamp (will be added via prompt(), not sync)\n * @param timeRange - Optional time range to filter log entries (defaults to last 10 days)\n * @param threadFilter - Optional thread filter to scope sync to a specific thread\n * @returns Number of messages synced\n */\nexport async function syncLogToSessionManager(\n sessionManager: SessionManager,\n conversationDir: string,\n excludeSlackTs?: string,\n timeRange?: TimeRange,\n threadFilter?: ThreadFilter,\n): Promise<number> {\n // Calculate default time range (last 10 days) if not provided\n const now = Date.now();\n const defaultStart = now - DEFAULT_SYNC_DAYS * 24 * 60 * 60 * 1000;\n const range = timeRange ?? { start: defaultStart, end: now };\n const logFile = join(conversationDir, \"log.jsonl\");\n\n if (!existsSync(logFile)) return 0;\n\n // Build a list of existing session messages for dedupe.\n // Live user prompts carry a formatted timestamp in the text and use Date.now(),\n // while log.jsonl uses the platform event timestamp. We therefore need a small\n // fuzzy match window in addition to the exact timestamp/content match used for\n // already-synced log entries.\n const existingMessages: ExistingSessionMessage[] = [];\n const existingMessageKeys = new Set<string>();\n for (const entry of sessionManager.getEntries()) {\n if (entry.type !== \"message\") continue;\n const msgEntry = entry as SessionMessageEntry;\n const message = msgEntry.message as Message;\n const contentText = Array.isArray(message.content)\n ? message.content\n .filter((part): part is { type: \"text\"; text: string } => part.type === \"text\")\n .map((part) => part.text)\n .join(\"\\n\\n\")\n : typeof message.content === \"string\"\n ? message.content\n : \"\";\n existingMessages.push({\n timestamp: typeof message.timestamp === \"number\" ? message.timestamp : undefined,\n rawText: contentText,\n normalizedText: normalizeComparableUserText(contentText),\n });\n if (typeof message.timestamp === \"number\") {\n existingMessageKeys.add(`${message.timestamp}:${contentText}`);\n }\n }\n\n // Read log.jsonl and find user messages not in context\n const logContent = await readFile(logFile, \"utf-8\");\n const logLines = logContent.trim().split(\"\\n\").filter(Boolean);\n\n const newMessages: Array<{ timestamp: number; message: UserMessage }> = [];\n\n for (const line of logLines) {\n try {\n const logMsg: ConversationLogMessage = JSON.parse(line);\n\n const slackTs = logMsg.ts;\n const date = logMsg.date;\n if (!slackTs || !date) continue;\n\n // Skip the current message being processed (will be added via prompt())\n if (excludeSlackTs && slackTs === excludeSlackTs) continue;\n\n // While queued messages are being processed, newer messages may already be present\n // in log.jsonl. Do not look ahead into those future messages when building the\n // current turn's context.\n if (!isMessageAtOrBeforeCurrent(slackTs, excludeSlackTs)) continue;\n\n // Skip bot messages - added through agent flow\n if (logMsg.isBot) continue;\n\n // Thread filtering: only sync messages belonging to this session's thread\n if (threadFilter) {\n if (threadFilter.scope === \"top-level\") {\n // Persistent top-level sessions should only ingest top-level messages.\n // This avoids pulling in unrelated replies from other threads.\n if (logMsg.threadTs) {\n continue;\n }\n } else {\n if (logMsg.threadTs) {\n // Thread reply: only include if threadTs matches our thread anchor or rootTs\n if (\n logMsg.threadTs !== threadFilter.threadTs &&\n logMsg.threadTs !== threadFilter.rootTs\n ) {\n continue;\n }\n } else {\n // Top-level message: only include if it's this session's root message\n if (slackTs !== threadFilter.rootTs) {\n continue;\n }\n }\n }\n }\n\n // Build the message text as it would appear in context\n const threadContext = logMsg.threadTs ? ` [in-thread:${logMsg.threadTs}]` : \"\";\n const messageText = `[${logMsg.userName || logMsg.user || \"unknown\"}]${threadContext}: ${logMsg.text || \"\"}`;\n\n const msgTime = new Date(date).getTime() || Date.now();\n const messageKey = `${msgTime}:${messageText}`;\n if (existingMessageKeys.has(messageKey)) continue;\n if (hasExistingSessionMessage(existingMessages, msgTime, messageText)) continue;\n\n // Skip messages outside the time range\n if (msgTime < range.start || msgTime > range.end) continue;\n\n const userMessage: UserMessage = {\n role: \"user\",\n content: [{ type: \"text\", text: messageText }],\n timestamp: msgTime,\n };\n\n newMessages.push({ timestamp: msgTime, message: userMessage });\n existingMessages.push({\n timestamp: msgTime,\n rawText: messageText,\n normalizedText: normalizeComparableUserText(messageText),\n });\n existingMessageKeys.add(messageKey); // Track to avoid duplicates within this sync\n } catch {\n // Skip malformed lines\n }\n }\n\n if (newMessages.length === 0) return 0;\n\n // Sort by timestamp and add to session\n newMessages.sort((a, b) => a.timestamp - b.timestamp);\n\n for (const { message } of newMessages) {\n sessionManager.appendMessage(message);\n }\n\n return newMessages.length;\n}\n\n// ============================================================================\n// Settings manager for mama\n// ============================================================================\n\n// Mama manages model/provider config through its own config.ts / settings.json.\n// We use an in-memory SettingsManager so AgentSession has valid defaults\n// without interfering with coding-agent's global settings files.\nexport function createMamaSettingsManager(_workspaceDir: string): SettingsManager {\n return SettingsManager.inMemory();\n}\n\nexport async function findLogMessageById(\n conversationDir: string,\n messageId: string,\n): Promise<ConversationLogMessage | null> {\n const logFile = join(conversationDir, \"log.jsonl\");\n if (!existsSync(logFile)) return null;\n\n const logContent = await readFile(logFile, \"utf-8\");\n const logLines = logContent.trim().split(\"\\n\").filter(Boolean);\n\n for (let i = logLines.length - 1; i >= 0; i--) {\n try {\n const entry = JSON.parse(logLines[i]) as ConversationLogMessage;\n if (entry.ts === messageId) {\n return entry;\n }\n } catch {\n // Skip malformed lines\n }\n }\n\n return null;\n}\n\nfunction stripSlackAttachmentBlock(text: string): string {\n return text.replace(/\\n*<slack_attachments>\\n[\\s\\S]*?\\n<\\/slack_attachments>\\s*$/g, \"\");\n}\n\nfunction normalizeComparableUserText(text: string): string {\n const withoutTimestamp = text.replace(\n /^\\[[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}[+-][0-9]{2}:[0-9]{2}\\]\\s+(?=\\[[^\\]]+\\](?:\\s+\\[in-thread:[^\\]]+\\])?:\\s)/,\n \"\",\n );\n return stripSlackAttachmentBlock(withoutTimestamp).trim();\n}\n\nfunction hasExistingSessionMessage(\n existingMessages: ExistingSessionMessage[],\n timestamp: number,\n text: string,\n): boolean {\n const normalizedText = normalizeComparableUserText(text);\n return existingMessages.some((existing) => {\n if (existing.timestamp === timestamp && existing.rawText === text) {\n return true;\n }\n if (existing.normalizedText !== normalizedText || existing.timestamp === undefined) {\n return false;\n }\n return existing.timestamp >= timestamp;\n });\n}\n\nfunction isMessageAtOrBeforeCurrent(messageId: string, currentMessageId?: string): boolean {\n if (!currentMessageId) return true;\n const comparison = compareMessageIds(messageId, currentMessageId);\n return comparison === null || comparison <= 0;\n}\n\nfunction compareMessageIds(a: string, b: string): number | null {\n if (/^\\d+$/.test(a) && /^\\d+$/.test(b)) {\n const left = BigInt(a);\n const right = BigInt(b);\n return left < right ? -1 : left > right ? 1 : 0;\n }\n\n const left = Number(a);\n const right = Number(b);\n if (Number.isFinite(left) && Number.isFinite(right)) {\n return left < right ? -1 : left > right ? 1 : 0;\n }\n\n return null;\n}\n"]}
|
package/dist/events.d.ts
CHANGED
|
@@ -7,6 +7,10 @@ export interface ImmediateEvent {
|
|
|
7
7
|
/** Creator userId — routes tool execution to that user's vault selection when fired. */
|
|
8
8
|
userId?: string;
|
|
9
9
|
text: string;
|
|
10
|
+
/** Determines which AgentRunner handles the event. */
|
|
11
|
+
sessionKey?: string;
|
|
12
|
+
/** Sub-conversation target (Slack thread ts, Discord thread id, Telegram reply-to id). */
|
|
13
|
+
threadTs?: string;
|
|
10
14
|
}
|
|
11
15
|
export interface OneShotEvent {
|
|
12
16
|
type: "one-shot";
|
|
@@ -26,6 +30,8 @@ export interface PeriodicEvent {
|
|
|
26
30
|
text: string;
|
|
27
31
|
schedule: string;
|
|
28
32
|
timezone: string;
|
|
33
|
+
/** Determines which AgentRunner handles the event. */
|
|
34
|
+
sessionKey?: string;
|
|
29
35
|
}
|
|
30
36
|
export type MamaEvent = ImmediateEvent | OneShotEvent | PeriodicEvent;
|
|
31
37
|
export interface PeriodicEventInfo {
|
|
@@ -42,6 +48,7 @@ export declare class EventsWatcher {
|
|
|
42
48
|
private eventsDir;
|
|
43
49
|
private botsByPlatform;
|
|
44
50
|
private timers;
|
|
51
|
+
private timerEventTypes;
|
|
45
52
|
private crons;
|
|
46
53
|
private debounceTimers;
|
|
47
54
|
private startTime;
|
package/dist/events.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,GAAG,EAAY,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAOpE,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,WAAW,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,wFAAwF;IACxF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,MAAM,SAAS,GAAG,cAAc,GAAG,YAAY,GAAG,aAAa,CAAC;AAEtE,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AAUD,qBAAa,aAAa;IAStB,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,cAAc;IATxB,OAAO,CAAC,MAAM,CAA0C;IACxD,OAAO,CAAC,KAAK,CAAgC;IAC7C,OAAO,CAAC,cAAc,CAA0C;IAChE,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAA0B;IACzC,OAAO,CAAC,UAAU,CAA0B;IAE5C,YACU,SAAS,EAAE,MAAM,EACjB,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAG5C;IAED;;OAEG;IACH,KAAK,IAAI,IAAI,CAkBZ;IAED;;OAEG;IACH,IAAI,IAAI,IAAI,CA2BX;IAED;;OAEG;IACH,iBAAiB,IAAI,iBAAiB,EAAE,CA0BvC;IAED,OAAO,CAAC,QAAQ;IAchB,OAAO,CAAC,YAAY;IAcpB,OAAO,CAAC,gBAAgB;IAgBxB,OAAO,CAAC,YAAY;IAQpB,OAAO,CAAC,eAAe;YAcT,UAAU;IA6CxB,OAAO,CAAC,UAAU;IAqElB,OAAO,CAAC,eAAe;IAsBvB,OAAO,CAAC,uBAAuB;IAoB/B,OAAO,CAAC,eAAe;IAoBvB,OAAO,CAAC,aAAa;IAuBrB,OAAO,CAAC,cAAc;IAmBtB,OAAO,CAAC,OAAO;IAsDf,OAAO,CAAC,UAAU;IAalB,OAAO,CAAC,KAAK;CAGd;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE,MAAM,EACpB,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAClC,aAAa,CAGf","sourcesContent":["import { Cron } from \"croner\";\nimport {\n existsSync,\n type FSWatcher,\n mkdirSync,\n readdirSync,\n readFileSync,\n statSync,\n unlinkSync,\n watch,\n} from \"fs\";\nimport { readFile } from \"fs/promises\";\nimport { join } from \"path\";\nimport type { Bot, BotEvent, ConversationKind } from \"./adapter.js\";\nimport * as log from \"./log.js\";\n\n// ============================================================================\n// Event Types\n// ============================================================================\n\nexport interface ImmediateEvent {\n type: \"immediate\";\n platform: string;\n conversationId: string;\n conversationKind: ConversationKind;\n /** Creator userId — routes tool execution to that user's vault selection when fired. */\n userId?: string;\n text: string;\n}\n\nexport interface OneShotEvent {\n type: \"one-shot\";\n platform: string;\n conversationId: string;\n conversationKind: ConversationKind;\n userId?: string;\n text: string;\n at: string; // ISO 8601 with timezone offset\n}\n\nexport interface PeriodicEvent {\n type: \"periodic\";\n platform: string;\n conversationId: string;\n conversationKind: ConversationKind;\n userId?: string;\n text: string;\n schedule: string; // cron syntax\n timezone: string; // IANA timezone\n}\n\nexport type MamaEvent = ImmediateEvent | OneShotEvent | PeriodicEvent;\n\nexport interface PeriodicEventInfo {\n filename: string;\n platform: string;\n conversationId: string;\n conversationKind: ConversationKind;\n text: string;\n schedule: string;\n timezone: string;\n nextRun: string | null; // ISO 8601\n}\n\n// ============================================================================\n// EventsWatcher\n// ============================================================================\n\nconst DEBOUNCE_MS = 100;\nconst MAX_RETRIES = 3;\nconst RETRY_BASE_MS = 100;\n\nexport class EventsWatcher {\n private timers: Map<string, NodeJS.Timeout> = new Map();\n private crons: Map<string, Cron> = new Map();\n private debounceTimers: Map<string, NodeJS.Timeout> = new Map();\n private startTime: number;\n private watcher: FSWatcher | null = null;\n private knownFiles: Set<string> = new Set();\n\n constructor(\n private eventsDir: string,\n private botsByPlatform: Record<string, Bot>,\n ) {\n this.startTime = Date.now();\n }\n\n /**\n * Start watching for events. Call this after platform bots are initialized.\n */\n start(): void {\n // Ensure events directory exists\n if (!existsSync(this.eventsDir)) {\n mkdirSync(this.eventsDir, { recursive: true });\n }\n\n log.logInfo(`Events watcher starting, dir: ${this.eventsDir}`);\n\n // Scan existing files\n this.scanExisting();\n\n // Watch for changes\n this.watcher = watch(this.eventsDir, (_eventType, filename) => {\n if (!filename || !filename.endsWith(\".json\")) return;\n this.debounce(filename, () => this.handleFileChange(filename));\n });\n\n log.logInfo(`Events watcher started, tracking ${this.knownFiles.size} files`);\n }\n\n /**\n * Stop watching and cancel all scheduled events.\n */\n stop(): void {\n // Stop fs watcher\n if (this.watcher) {\n this.watcher.close();\n this.watcher = null;\n }\n\n // Cancel all debounce timers\n for (const timer of this.debounceTimers.values()) {\n clearTimeout(timer);\n }\n this.debounceTimers.clear();\n\n // Cancel all scheduled timers\n for (const timer of this.timers.values()) {\n clearTimeout(timer);\n }\n this.timers.clear();\n\n // Cancel all cron jobs\n for (const cron of this.crons.values()) {\n cron.stop();\n }\n this.crons.clear();\n\n this.knownFiles.clear();\n log.logInfo(\"Events watcher stopped\");\n }\n\n /**\n * Return all active periodic (cron) events with their next run time.\n */\n getPeriodicEvents(): PeriodicEventInfo[] {\n const results: PeriodicEventInfo[] = [];\n for (const [filename, cron] of this.crons) {\n const filePath = join(this.eventsDir, filename);\n try {\n const content = readFileSync(filePath, \"utf-8\");\n const data = this.parseEvent(content, filename);\n if (!data || data.type !== \"periodic\") {\n continue;\n }\n const next = cron.nextRun();\n results.push({\n filename,\n platform: data.platform,\n conversationId: data.conversationId,\n conversationKind: data.conversationKind,\n text: data.text,\n schedule: data.schedule,\n timezone: data.timezone,\n nextRun: next?.toISOString() ?? null,\n });\n } catch {\n // File may have been deleted or corrupted, skip\n }\n }\n return results;\n }\n\n private debounce(filename: string, fn: () => void): void {\n const existing = this.debounceTimers.get(filename);\n if (existing) {\n clearTimeout(existing);\n }\n this.debounceTimers.set(\n filename,\n setTimeout(() => {\n this.debounceTimers.delete(filename);\n fn();\n }, DEBOUNCE_MS),\n );\n }\n\n private scanExisting(): void {\n let files: string[];\n try {\n files = readdirSync(this.eventsDir).filter((f) => f.endsWith(\".json\"));\n } catch (err) {\n log.logWarning(\"Failed to read events directory\", String(err));\n return;\n }\n\n for (const filename of files) {\n this.handleFile(filename);\n }\n }\n\n private handleFileChange(filename: string): void {\n const filePath = join(this.eventsDir, filename);\n\n if (!existsSync(filePath)) {\n // File was deleted\n this.handleDelete(filename);\n } else if (this.knownFiles.has(filename)) {\n // File was modified - cancel existing and re-schedule\n this.cancelScheduled(filename);\n this.handleFile(filename);\n } else {\n // New file\n this.handleFile(filename);\n }\n }\n\n private handleDelete(filename: string): void {\n if (!this.knownFiles.has(filename)) return;\n\n log.logInfo(`Event file deleted: ${filename}`);\n this.cancelScheduled(filename);\n this.knownFiles.delete(filename);\n }\n\n private cancelScheduled(filename: string): void {\n const timer = this.timers.get(filename);\n if (timer) {\n clearTimeout(timer);\n this.timers.delete(filename);\n }\n\n const cron = this.crons.get(filename);\n if (cron) {\n cron.stop();\n this.crons.delete(filename);\n }\n }\n\n private async handleFile(filename: string): Promise<void> {\n const filePath = join(this.eventsDir, filename);\n\n // Parse with retries\n let event: MamaEvent | null = null;\n let lastError: Error | null = null;\n\n for (let i = 0; i < MAX_RETRIES; i++) {\n try {\n const content = await readFile(filePath, \"utf-8\");\n event = this.parseEvent(content, filename);\n break;\n } catch (err) {\n lastError = err instanceof Error ? err : new Error(String(err));\n if (i < MAX_RETRIES - 1) {\n await this.sleep(RETRY_BASE_MS * 2 ** i);\n }\n }\n }\n\n if (!event) {\n log.logWarning(\n `Failed to parse event file after ${MAX_RETRIES} retries: ${filename}`,\n lastError?.message,\n );\n this.deleteFile(filename);\n return;\n }\n\n this.knownFiles.add(filename);\n\n // Schedule based on type\n switch (event.type) {\n case \"immediate\":\n this.handleImmediate(filename, event);\n break;\n case \"one-shot\":\n this.handleOneShot(filename, event);\n break;\n case \"periodic\":\n this.handlePeriodic(filename, event);\n break;\n }\n }\n\n private parseEvent(content: string, filename: string): MamaEvent | null {\n const data = JSON.parse(content);\n const conversationId =\n typeof data.conversationId === \"string\"\n ? data.conversationId\n : typeof data.channelId === \"string\"\n ? data.channelId\n : undefined;\n\n if (!data.type || !conversationId || !data.text) {\n throw new Error(`Missing required fields (type, conversationId, text) in ${filename}`);\n }\n\n const platform = this.resolvePlatform(data.platform, filename);\n const conversationKind = this.resolveConversationKind(\n platform,\n conversationId,\n data.conversationKind,\n );\n const userId = typeof data.userId === \"string\" ? data.userId : undefined;\n\n switch (data.type) {\n case \"immediate\":\n return {\n type: \"immediate\",\n platform,\n conversationId,\n conversationKind,\n userId,\n text: data.text,\n };\n\n case \"one-shot\":\n if (!data.at) {\n throw new Error(`Missing 'at' field for one-shot event in ${filename}`);\n }\n return {\n type: \"one-shot\",\n platform,\n conversationId,\n conversationKind,\n userId,\n text: data.text,\n at: data.at,\n };\n\n case \"periodic\":\n if (!data.schedule) {\n throw new Error(`Missing 'schedule' field for periodic event in ${filename}`);\n }\n if (!data.timezone) {\n throw new Error(`Missing 'timezone' field for periodic event in ${filename}`);\n }\n return {\n type: \"periodic\",\n platform,\n conversationId,\n conversationKind,\n userId,\n text: data.text,\n schedule: data.schedule,\n timezone: data.timezone,\n };\n\n default:\n throw new Error(`Unknown event type '${data.type}' in ${filename}`);\n }\n }\n\n private resolvePlatform(platformValue: unknown, filename: string): string {\n const availablePlatforms = Object.keys(this.botsByPlatform);\n\n if (typeof platformValue === \"string\" && platformValue.trim().length > 0) {\n const platform = platformValue.trim().toLowerCase();\n if (!this.botsByPlatform[platform]) {\n throw new Error(\n `Unknown platform '${platformValue}' in ${filename}. Expected one of: ${availablePlatforms.join(\", \")}`,\n );\n }\n return platform;\n }\n\n if (availablePlatforms.length === 1) {\n return availablePlatforms[0];\n }\n\n throw new Error(\n `Missing required field 'platform' in ${filename}. Available platforms: ${availablePlatforms.join(\", \")}`,\n );\n }\n\n private resolveConversationKind(\n platform: string,\n conversationId: string,\n conversationKindValue: unknown,\n ): ConversationKind {\n if (conversationKindValue === \"direct\" || conversationKindValue === \"shared\") {\n return conversationKindValue;\n }\n\n if (platform === \"slack\") {\n return conversationId.startsWith(\"D\") ? \"direct\" : \"shared\";\n }\n\n if (platform === \"telegram\") {\n return conversationId.startsWith(\"-\") ? \"shared\" : \"direct\";\n }\n\n return \"shared\";\n }\n\n private handleImmediate(filename: string, event: ImmediateEvent): void {\n const filePath = join(this.eventsDir, filename);\n\n // Check if stale (created before harness started)\n try {\n const stat = statSync(filePath);\n if (stat.mtimeMs < this.startTime) {\n log.logInfo(`Stale immediate event, deleting: ${filename}`);\n this.deleteFile(filename);\n return;\n }\n } catch {\n // File may have been deleted\n return;\n }\n\n log.logInfo(`Executing immediate event: ${filename}`);\n this.execute(filename, event);\n }\n\n private handleOneShot(filename: string, event: OneShotEvent): void {\n const atTime = new Date(event.at).getTime();\n const now = Date.now();\n\n if (atTime <= now) {\n // Past - delete without executing\n log.logInfo(`One-shot event in the past, deleting: ${filename}`);\n this.deleteFile(filename);\n return;\n }\n\n const delay = atTime - now;\n log.logInfo(`Scheduling one-shot event: ${filename} in ${Math.round(delay / 1000)}s`);\n\n const timer = setTimeout(() => {\n this.timers.delete(filename);\n log.logInfo(`Executing one-shot event: ${filename}`);\n this.execute(filename, event);\n }, delay);\n\n this.timers.set(filename, timer);\n }\n\n private handlePeriodic(filename: string, event: PeriodicEvent): void {\n try {\n const cron = new Cron(event.schedule, { timezone: event.timezone }, () => {\n log.logInfo(`Executing periodic event: ${filename}`);\n this.execute(filename, event, false); // Don't delete periodic events\n });\n\n this.crons.set(filename, cron);\n\n const next = cron.nextRun();\n log.logInfo(\n `Scheduled periodic event: ${filename}, next run: ${next?.toISOString() ?? \"unknown\"}`,\n );\n } catch (err) {\n log.logWarning(`Invalid cron schedule for ${filename}: ${event.schedule}`, String(err));\n this.deleteFile(filename);\n }\n }\n\n private execute(filename: string, event: MamaEvent, deleteAfter: boolean = true): void {\n // Format the message\n let scheduleInfo: string;\n switch (event.type) {\n case \"immediate\":\n scheduleInfo = \"immediate\";\n break;\n case \"one-shot\":\n scheduleInfo = event.at;\n break;\n case \"periodic\":\n scheduleInfo = event.schedule;\n break;\n }\n\n const message = `[EVENT:${filename}:${event.type}:${scheduleInfo}] ${event.text}`;\n const bot = this.botsByPlatform[event.platform];\n\n if (!bot) {\n log.logWarning(`No bot configured for event platform '${event.platform}'`, filename);\n if (deleteAfter) {\n this.deleteFile(filename);\n }\n return;\n }\n\n // Create synthetic BotEvent. Keep a stable conversation session key so recurring\n // reminders share context, but use a unique synthetic message id because\n // some adapters treat ts/message id as a reply target.\n const syntheticEvent: BotEvent = {\n type: \"mention\",\n conversationId: event.conversationId,\n conversationKind: event.conversationKind,\n user: event.userId ?? \"EVENT\",\n text: message,\n ts: `event:${filename}`,\n sessionKey: event.conversationId,\n };\n\n // Enqueue for processing\n const enqueued = bot.enqueueEvent(syntheticEvent);\n\n if (enqueued && deleteAfter) {\n // Delete file after successful enqueue (immediate and one-shot)\n this.deleteFile(filename);\n } else if (!enqueued) {\n log.logWarning(`Event queue full, discarded: ${filename}`);\n // Still delete immediate/one-shot even if discarded\n if (deleteAfter) {\n this.deleteFile(filename);\n }\n }\n }\n\n private deleteFile(filename: string): void {\n const filePath = join(this.eventsDir, filename);\n try {\n unlinkSync(filePath);\n } catch (err) {\n // ENOENT is fine (file already deleted), other errors are warnings\n if (err instanceof Error && \"code\" in err && err.code !== \"ENOENT\") {\n log.logWarning(`Failed to delete event file: ${filename}`, String(err));\n }\n }\n this.knownFiles.delete(filename);\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n}\n\n/**\n * Create an events watcher for all configured platforms.\n */\nexport function createEventsWatcher(\n workspaceDir: string,\n botsByPlatform: Record<string, Bot>,\n): EventsWatcher {\n const eventsDir = join(workspaceDir, \"events\");\n return new EventsWatcher(eventsDir, botsByPlatform);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,GAAG,EAAY,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAQpE,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,WAAW,CAAC;IAClB,QAAQ,EA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Aa;IAUtB,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,cAAc;IAVxB,OAAO,CAAC,MAAM,CAA0C;IACxD,OAAO,CAAC,eAAe,CAAsC;IAC7D,OAAO,CAAC,KAAK,CAAgC;IAC7C,OAAO,CAAC,cAAc,CAA0C;IAChE,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAA0B;IACzC,OAAO,CAAC,UAAU,CAA0B;IAE5C,YACU,SAAS,EAAE,MAAM,EACjB,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAG5C;IAED;;OAEG;IACH,KAAK,IAAI,IAAI,CAqBZ;IAED;;OAEG;IACH,IAAI,IAAI,IAAI,CA4BX;IAED;;OAEG;IACH,iBAAiB,IAAI,iBAAiB,EAAE,CA0BvC;IAED,OAAO,CAAC,QAAQ;IAchB,OAAO,CAAC,YAAY;IAcpB,OAAO,CAAC,gBAAgB;YAoBV,YAAY;IA0B1B,OAAO,CAAC,eAAe;YAkBT,UAAU;IAiDxB,OAAO,CAAC,UAAU;IA0ElB,OAAO,CAAC,eAAe;IAsBvB,OAAO,CAAC,uBAAuB;IAY/B,OAAO,CAAC,eAAe;IAoBvB,OAAO,CAAC,aAAa;IA4BrB,OAAO,CAAC,cAAc;IAmBtB,OAAO,CAAC,OAAO;IAwDf,OAAO,CAAC,UAAU;IAclB,OAAO,CAAC,KAAK;CAGd;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE,MAAM,EACpB,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAClC,aAAa,CAGf","sourcesContent":["import { Cron } from \"croner\";\nimport {\n existsSync,\n type FSWatcher,\n mkdirSync,\n readdirSync,\n readFileSync,\n statSync,\n unlinkSync,\n watch,\n} from \"fs\";\nimport { readFile } from \"fs/promises\";\nimport { join } from \"path\";\nimport type { Bot, BotEvent, ConversationKind } from \"./adapter.js\";\nimport * as log from \"./log.js\";\nimport { inferConversationKind } from \"./session-policy.js\";\n\n// ============================================================================\n// Event Types\n// ============================================================================\n\nexport interface ImmediateEvent {\n type: \"immediate\";\n platform: string;\n conversationId: string;\n conversationKind: ConversationKind;\n /** Creator userId — routes tool execution to that user's vault selection when fired. */\n userId?: string;\n text: string;\n /** Determines which AgentRunner handles the event. */\n sessionKey?: string;\n /** Sub-conversation target (Slack thread ts, Discord thread id, Telegram reply-to id). */\n threadTs?: string;\n}\n\nexport interface OneShotEvent {\n type: \"one-shot\";\n platform: string;\n conversationId: string;\n conversationKind: ConversationKind;\n userId?: string;\n text: string;\n at: string; // ISO 8601 with timezone offset\n // No sessionKey or threadTs: reminders fire as top-level messages regardless of where they were created.\n}\n\nexport interface PeriodicEvent {\n type: \"periodic\";\n platform: string;\n conversationId: string;\n conversationKind: ConversationKind;\n userId?: string;\n text: string;\n schedule: string; // cron syntax\n timezone: string; // IANA timezone\n /** Determines which AgentRunner handles the event. */\n sessionKey?: string;\n // No threadTs: recurring events always fire as top-level messages.\n}\n\nexport type MamaEvent = ImmediateEvent | OneShotEvent | PeriodicEvent;\n\nexport interface PeriodicEventInfo {\n filename: string;\n platform: string;\n conversationId: string;\n conversationKind: ConversationKind;\n text: string;\n schedule: string;\n timezone: string;\n nextRun: string | null; // ISO 8601\n}\n\n// ============================================================================\n// EventsWatcher\n// ============================================================================\n\nconst DEBOUNCE_MS = 100;\nconst MAX_RETRIES = 3;\nconst RETRY_BASE_MS = 100;\n\nexport class EventsWatcher {\n private timers: Map<string, NodeJS.Timeout> = new Map();\n private timerEventTypes: Map<string, \"one-shot\"> = new Map();\n private crons: Map<string, Cron> = new Map();\n private debounceTimers: Map<string, NodeJS.Timeout> = new Map();\n private startTime: number;\n private watcher: FSWatcher | null = null;\n private knownFiles: Set<string> = new Set();\n\n constructor(\n private eventsDir: string,\n private botsByPlatform: Record<string, Bot>,\n ) {\n this.startTime = Date.now();\n }\n\n /**\n * Start watching for events. Call this after platform bots are initialized.\n */\n start(): void {\n // Ensure events directory exists\n if (!existsSync(this.eventsDir)) {\n mkdirSync(this.eventsDir, { recursive: true });\n }\n\n log.logInfo(`Events watcher starting, dir: ${this.eventsDir}`);\n\n // Scan existing files\n this.scanExisting();\n\n // Watch for changes\n this.watcher = watch(this.eventsDir, (eventType, filename) => {\n if (!filename || !filename.endsWith(\".json\")) return;\n log.logInfo(\n `Events watcher fs event: ${String(eventType)} ${filename} (exists=${existsSync(join(this.eventsDir, filename))})`,\n );\n this.debounce(filename, () => this.handleFileChange(filename));\n });\n\n log.logInfo(`Events watcher started, tracking ${this.knownFiles.size} files`);\n }\n\n /**\n * Stop watching and cancel all scheduled events.\n */\n stop(): void {\n // Stop fs watcher\n if (this.watcher) {\n this.watcher.close();\n this.watcher = null;\n }\n\n // Cancel all debounce timers\n for (const timer of this.debounceTimers.values()) {\n clearTimeout(timer);\n }\n this.debounceTimers.clear();\n\n // Cancel all scheduled timers\n for (const timer of this.timers.values()) {\n clearTimeout(timer);\n }\n this.timers.clear();\n this.timerEventTypes.clear();\n\n // Cancel all cron jobs\n for (const cron of this.crons.values()) {\n cron.stop();\n }\n this.crons.clear();\n\n this.knownFiles.clear();\n log.logInfo(\"Events watcher stopped\");\n }\n\n /**\n * Return all active periodic (cron) events with their next run time.\n */\n getPeriodicEvents(): PeriodicEventInfo[] {\n const results: PeriodicEventInfo[] = [];\n for (const [filename, cron] of this.crons) {\n const filePath = join(this.eventsDir, filename);\n try {\n const content = readFileSync(filePath, \"utf-8\");\n const data = this.parseEvent(content, filename);\n if (!data || data.type !== \"periodic\") {\n continue;\n }\n const next = cron.nextRun();\n results.push({\n filename,\n platform: data.platform,\n conversationId: data.conversationId,\n conversationKind: data.conversationKind,\n text: data.text,\n schedule: data.schedule,\n timezone: data.timezone,\n nextRun: next?.toISOString() ?? null,\n });\n } catch {\n // File may have been deleted or corrupted, skip\n }\n }\n return results;\n }\n\n private debounce(filename: string, fn: () => void): void {\n const existing = this.debounceTimers.get(filename);\n if (existing) {\n clearTimeout(existing);\n }\n this.debounceTimers.set(\n filename,\n setTimeout(() => {\n this.debounceTimers.delete(filename);\n fn();\n }, DEBOUNCE_MS),\n );\n }\n\n private scanExisting(): void {\n let files: string[];\n try {\n files = readdirSync(this.eventsDir).filter((f) => f.endsWith(\".json\"));\n } catch (err) {\n log.logWarning(\"Failed to read events directory\", String(err));\n return;\n }\n\n for (const filename of files) {\n this.handleFile(filename);\n }\n }\n\n private handleFileChange(filename: string): void {\n const filePath = join(this.eventsDir, filename);\n const exists = existsSync(filePath);\n const known = this.knownFiles.has(filename);\n log.logInfo(`Handling event file change: ${filename} (exists=${exists}, known=${known})`);\n\n if (!exists) {\n // fs.watch can briefly report a file as missing during create/rename churn.\n // Confirm deletion before canceling scheduled events.\n void this.handleDelete(filename);\n } else if (known) {\n // File was modified - cancel existing and re-schedule\n this.cancelScheduled(filename, \"file-modified\");\n void this.handleFile(filename);\n } else {\n // New file\n void this.handleFile(filename);\n }\n }\n\n private async handleDelete(filename: string): Promise<void> {\n if (!this.knownFiles.has(filename)) return;\n\n const filePath = join(this.eventsDir, filename);\n for (let i = 0; i < MAX_RETRIES; i++) {\n const delay = RETRY_BASE_MS * 2 ** i;\n await this.sleep(delay);\n const exists = existsSync(filePath);\n log.logInfo(`Confirming event deletion: ${filename} after ${delay}ms (exists=${exists})`);\n if (exists) {\n return;\n }\n }\n\n if (this.timerEventTypes.get(filename) === \"one-shot\" && this.timers.has(filename)) {\n log.logInfo(\n `Ignoring deleted one-shot file after scheduling: ${filename} (timer remains active)`,\n );\n return;\n }\n\n log.logInfo(`Event file deleted: ${filename}`);\n this.cancelScheduled(filename, \"confirmed-delete\");\n this.knownFiles.delete(filename);\n }\n\n private cancelScheduled(filename: string, reason = \"unspecified\"): void {\n const timer = this.timers.get(filename);\n const cron = this.crons.get(filename);\n log.logInfo(\n `Canceling scheduled event: ${filename} (reason=${reason}, timer=${Boolean(timer)}, cron=${Boolean(cron)})`,\n );\n if (timer) {\n clearTimeout(timer);\n this.timers.delete(filename);\n this.timerEventTypes.delete(filename);\n }\n\n if (cron) {\n cron.stop();\n this.crons.delete(filename);\n }\n }\n\n private async handleFile(filename: string): Promise<void> {\n const filePath = join(this.eventsDir, filename);\n log.logInfo(`Loading event file: ${filename} from ${filePath}`);\n\n // Parse with retries\n let event: MamaEvent | null = null;\n let lastError: Error | null = null;\n\n for (let i = 0; i < MAX_RETRIES; i++) {\n try {\n const content = await readFile(filePath, \"utf-8\");\n event = this.parseEvent(content, filename);\n break;\n } catch (err) {\n lastError = err instanceof Error ? err : new Error(String(err));\n if (i < MAX_RETRIES - 1) {\n await this.sleep(RETRY_BASE_MS * 2 ** i);\n }\n }\n }\n\n if (!event) {\n log.logWarning(\n `Failed to parse event file after ${MAX_RETRIES} retries: ${filename}`,\n lastError?.message,\n );\n this.deleteFile(filename, \"parse-failed\");\n return;\n }\n\n this.knownFiles.add(filename);\n log.logInfo(\n `Parsed event file: ${filename} (${event.type} for ${event.platform}/${event.conversationId})`,\n );\n\n // Schedule based on type\n switch (event.type) {\n case \"immediate\":\n this.handleImmediate(filename, event);\n break;\n case \"one-shot\":\n this.handleOneShot(filename, event);\n break;\n case \"periodic\":\n this.handlePeriodic(filename, event);\n break;\n }\n }\n\n private parseEvent(content: string, filename: string): MamaEvent | null {\n const data = JSON.parse(content);\n const conversationId =\n typeof data.conversationId === \"string\"\n ? data.conversationId\n : typeof data.channelId === \"string\"\n ? data.channelId\n : undefined;\n\n if (!data.type || !conversationId || !data.text) {\n throw new Error(`Missing required fields (type, conversationId, text) in ${filename}`);\n }\n\n const platform = this.resolvePlatform(data.platform, filename);\n const conversationKind = this.resolveConversationKind(\n platform,\n conversationId,\n data.conversationKind,\n );\n const userId = typeof data.userId === \"string\" ? data.userId : undefined;\n const sessionKey = typeof data.sessionKey === \"string\" ? data.sessionKey : undefined;\n const threadTs = typeof data.threadTs === \"string\" ? data.threadTs : undefined;\n\n switch (data.type) {\n case \"immediate\":\n return {\n type: \"immediate\",\n platform,\n conversationId,\n conversationKind,\n userId,\n text: data.text,\n sessionKey,\n threadTs,\n };\n\n case \"one-shot\":\n if (!data.at) {\n throw new Error(`Missing 'at' field for one-shot event in ${filename}`);\n }\n return {\n type: \"one-shot\",\n platform,\n conversationId,\n conversationKind,\n userId,\n text: data.text,\n at: data.at,\n };\n\n case \"periodic\":\n if (!data.schedule) {\n throw new Error(`Missing 'schedule' field for periodic event in ${filename}`);\n }\n if (!data.timezone) {\n throw new Error(`Missing 'timezone' field for periodic event in ${filename}`);\n }\n return {\n type: \"periodic\",\n platform,\n conversationId,\n conversationKind,\n userId,\n text: data.text,\n schedule: data.schedule,\n timezone: data.timezone,\n sessionKey,\n };\n\n default:\n throw new Error(`Unknown event type '${data.type}' in ${filename}`);\n }\n }\n\n private resolvePlatform(platformValue: unknown, filename: string): string {\n const availablePlatforms = Object.keys(this.botsByPlatform);\n\n if (typeof platformValue === \"string\" && platformValue.trim().length > 0) {\n const platform = platformValue.trim().toLowerCase();\n if (!this.botsByPlatform[platform]) {\n throw new Error(\n `Unknown platform '${platformValue}' in ${filename}. Expected one of: ${availablePlatforms.join(\", \")}`,\n );\n }\n return platform;\n }\n\n if (availablePlatforms.length === 1) {\n return availablePlatforms[0];\n }\n\n throw new Error(\n `Missing required field 'platform' in ${filename}. Available platforms: ${availablePlatforms.join(\", \")}`,\n );\n }\n\n private resolveConversationKind(\n platform: string,\n conversationId: string,\n conversationKindValue: unknown,\n ): ConversationKind {\n if (conversationKindValue === \"direct\" || conversationKindValue === \"shared\") {\n return conversationKindValue;\n }\n\n return inferConversationKind(platform, conversationId);\n }\n\n private handleImmediate(filename: string, event: ImmediateEvent): void {\n const filePath = join(this.eventsDir, filename);\n\n // Check if stale (created before harness started)\n try {\n const stat = statSync(filePath);\n if (stat.mtimeMs < this.startTime) {\n log.logInfo(`Stale immediate event, deleting: ${filename}`);\n this.deleteFile(filename, \"stale-immediate\");\n return;\n }\n } catch {\n // File may have been deleted\n return;\n }\n\n log.logInfo(`Executing immediate event: ${filename}`);\n this.execute(filename, event);\n }\n\n private handleOneShot(filename: string, event: OneShotEvent): void {\n const atTime = new Date(event.at).getTime();\n const now = Date.now();\n\n if (atTime <= now) {\n // Past - delete without executing\n log.logInfo(`One-shot event in the past, deleting: ${filename}`);\n this.deleteFile(filename, \"one-shot-in-past\");\n return;\n }\n\n const delay = atTime - now;\n log.logInfo(\n `Scheduling one-shot event: ${filename} in ${Math.round(delay / 1000)}s (at=${event.at}, now=${new Date(now).toISOString()})`,\n );\n\n const timer = setTimeout(() => {\n this.timers.delete(filename);\n this.timerEventTypes.delete(filename);\n log.logInfo(`Executing one-shot event: ${filename}`);\n this.execute(filename, event);\n }, delay);\n\n this.timers.set(filename, timer);\n this.timerEventTypes.set(filename, \"one-shot\");\n log.logInfo(`Stored one-shot timer: ${filename} (active timers=${this.timers.size})`);\n }\n\n private handlePeriodic(filename: string, event: PeriodicEvent): void {\n try {\n const cron = new Cron(event.schedule, { timezone: event.timezone }, () => {\n log.logInfo(`Executing periodic event: ${filename}`);\n this.execute(filename, event, false); // Don't delete periodic events\n });\n\n this.crons.set(filename, cron);\n\n const next = cron.nextRun();\n log.logInfo(\n `Scheduled periodic event: ${filename}, next run: ${next?.toISOString() ?? \"unknown\"}`,\n );\n } catch (err) {\n log.logWarning(`Invalid cron schedule for ${filename}: ${event.schedule}`, String(err));\n this.deleteFile(filename, \"invalid-cron\");\n }\n }\n\n private execute(filename: string, event: MamaEvent, deleteAfter: boolean = true): void {\n // Format the message\n let scheduleInfo: string;\n switch (event.type) {\n case \"immediate\":\n scheduleInfo = \"immediate\";\n break;\n case \"one-shot\":\n scheduleInfo = event.at;\n break;\n case \"periodic\":\n scheduleInfo = event.schedule;\n break;\n }\n\n const message = `[EVENT:${filename}:${event.type}:${scheduleInfo}] ${event.text}`;\n const bot = this.botsByPlatform[event.platform];\n\n if (!bot) {\n log.logWarning(`No bot configured for event platform '${event.platform}'`, filename);\n if (deleteAfter) {\n this.deleteFile(filename, \"missing-bot\");\n }\n return;\n }\n\n // Create synthetic BotEvent. Keep a stable conversation session key so recurring\n // reminders share context, but use a unique synthetic message id because\n // some adapters treat ts/message id as a reply target.\n const scopedEvent = event as { sessionKey?: string; threadTs?: string };\n const syntheticEvent: BotEvent = {\n type: \"mention\",\n conversationId: event.conversationId,\n conversationKind: event.conversationKind,\n user: event.userId ?? \"EVENT\",\n text: message,\n ts: `event:${filename}`,\n thread_ts: scopedEvent.threadTs,\n sessionKey: scopedEvent.sessionKey ?? event.conversationId,\n };\n\n // Enqueue for processing\n const enqueued = bot.enqueueEvent(syntheticEvent);\n\n if (enqueued && deleteAfter) {\n // Delete file after successful enqueue (immediate and one-shot)\n this.deleteFile(filename, \"executed-and-enqueued\");\n } else if (!enqueued) {\n log.logWarning(`Event queue full, discarded: ${filename}`);\n // Still delete immediate/one-shot even if discarded\n if (deleteAfter) {\n this.deleteFile(filename, \"queue-full-discarded\");\n }\n }\n }\n\n private deleteFile(filename: string, reason = \"unspecified\"): void {\n const filePath = join(this.eventsDir, filename);\n log.logInfo(`Deleting event file: ${filename} (reason=${reason})`);\n try {\n unlinkSync(filePath);\n } catch (err) {\n // ENOENT is fine (file already deleted), other errors are warnings\n if (err instanceof Error && \"code\" in err && err.code !== \"ENOENT\") {\n log.logWarning(`Failed to delete event file: ${filename}`, String(err));\n }\n }\n this.knownFiles.delete(filename);\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n}\n\n/**\n * Create an events watcher for all configured platforms.\n */\nexport function createEventsWatcher(\n workspaceDir: string,\n botsByPlatform: Record<string, Bot>,\n): EventsWatcher {\n const eventsDir = join(workspaceDir, \"events\");\n return new EventsWatcher(eventsDir, botsByPlatform);\n}\n"]}
|
package/dist/events.js
CHANGED
|
@@ -3,6 +3,7 @@ import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, unlinkSync,
|
|
|
3
3
|
import { readFile } from "fs/promises";
|
|
4
4
|
import { join } from "path";
|
|
5
5
|
import * as log from "./log.js";
|
|
6
|
+
import { inferConversationKind } from "./session-policy.js";
|
|
6
7
|
// ============================================================================
|
|
7
8
|
// EventsWatcher
|
|
8
9
|
// ============================================================================
|
|
@@ -14,6 +15,7 @@ export class EventsWatcher {
|
|
|
14
15
|
this.eventsDir = eventsDir;
|
|
15
16
|
this.botsByPlatform = botsByPlatform;
|
|
16
17
|
this.timers = new Map();
|
|
18
|
+
this.timerEventTypes = new Map();
|
|
17
19
|
this.crons = new Map();
|
|
18
20
|
this.debounceTimers = new Map();
|
|
19
21
|
this.watcher = null;
|
|
@@ -32,9 +34,10 @@ export class EventsWatcher {
|
|
|
32
34
|
// Scan existing files
|
|
33
35
|
this.scanExisting();
|
|
34
36
|
// Watch for changes
|
|
35
|
-
this.watcher = watch(this.eventsDir, (
|
|
37
|
+
this.watcher = watch(this.eventsDir, (eventType, filename) => {
|
|
36
38
|
if (!filename || !filename.endsWith(".json"))
|
|
37
39
|
return;
|
|
40
|
+
log.logInfo(`Events watcher fs event: ${String(eventType)} ${filename} (exists=${existsSync(join(this.eventsDir, filename))})`);
|
|
38
41
|
this.debounce(filename, () => this.handleFileChange(filename));
|
|
39
42
|
});
|
|
40
43
|
log.logInfo(`Events watcher started, tracking ${this.knownFiles.size} files`);
|
|
@@ -58,6 +61,7 @@ export class EventsWatcher {
|
|
|
58
61
|
clearTimeout(timer);
|
|
59
62
|
}
|
|
60
63
|
this.timers.clear();
|
|
64
|
+
this.timerEventTypes.clear();
|
|
61
65
|
// Cancel all cron jobs
|
|
62
66
|
for (const cron of this.crons.values()) {
|
|
63
67
|
cron.stop();
|
|
@@ -122,34 +126,54 @@ export class EventsWatcher {
|
|
|
122
126
|
}
|
|
123
127
|
handleFileChange(filename) {
|
|
124
128
|
const filePath = join(this.eventsDir, filename);
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
129
|
+
const exists = existsSync(filePath);
|
|
130
|
+
const known = this.knownFiles.has(filename);
|
|
131
|
+
log.logInfo(`Handling event file change: ${filename} (exists=${exists}, known=${known})`);
|
|
132
|
+
if (!exists) {
|
|
133
|
+
// fs.watch can briefly report a file as missing during create/rename churn.
|
|
134
|
+
// Confirm deletion before canceling scheduled events.
|
|
135
|
+
void this.handleDelete(filename);
|
|
136
|
+
}
|
|
137
|
+
else if (known) {
|
|
130
138
|
// File was modified - cancel existing and re-schedule
|
|
131
|
-
this.cancelScheduled(filename);
|
|
132
|
-
this.handleFile(filename);
|
|
139
|
+
this.cancelScheduled(filename, "file-modified");
|
|
140
|
+
void this.handleFile(filename);
|
|
133
141
|
}
|
|
134
142
|
else {
|
|
135
143
|
// New file
|
|
136
|
-
this.handleFile(filename);
|
|
144
|
+
void this.handleFile(filename);
|
|
137
145
|
}
|
|
138
146
|
}
|
|
139
|
-
handleDelete(filename) {
|
|
147
|
+
async handleDelete(filename) {
|
|
140
148
|
if (!this.knownFiles.has(filename))
|
|
141
149
|
return;
|
|
150
|
+
const filePath = join(this.eventsDir, filename);
|
|
151
|
+
for (let i = 0; i < MAX_RETRIES; i++) {
|
|
152
|
+
const delay = RETRY_BASE_MS * 2 ** i;
|
|
153
|
+
await this.sleep(delay);
|
|
154
|
+
const exists = existsSync(filePath);
|
|
155
|
+
log.logInfo(`Confirming event deletion: ${filename} after ${delay}ms (exists=${exists})`);
|
|
156
|
+
if (exists) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (this.timerEventTypes.get(filename) === "one-shot" && this.timers.has(filename)) {
|
|
161
|
+
log.logInfo(`Ignoring deleted one-shot file after scheduling: ${filename} (timer remains active)`);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
142
164
|
log.logInfo(`Event file deleted: ${filename}`);
|
|
143
|
-
this.cancelScheduled(filename);
|
|
165
|
+
this.cancelScheduled(filename, "confirmed-delete");
|
|
144
166
|
this.knownFiles.delete(filename);
|
|
145
167
|
}
|
|
146
|
-
cancelScheduled(filename) {
|
|
168
|
+
cancelScheduled(filename, reason = "unspecified") {
|
|
147
169
|
const timer = this.timers.get(filename);
|
|
170
|
+
const cron = this.crons.get(filename);
|
|
171
|
+
log.logInfo(`Canceling scheduled event: ${filename} (reason=${reason}, timer=${Boolean(timer)}, cron=${Boolean(cron)})`);
|
|
148
172
|
if (timer) {
|
|
149
173
|
clearTimeout(timer);
|
|
150
174
|
this.timers.delete(filename);
|
|
175
|
+
this.timerEventTypes.delete(filename);
|
|
151
176
|
}
|
|
152
|
-
const cron = this.crons.get(filename);
|
|
153
177
|
if (cron) {
|
|
154
178
|
cron.stop();
|
|
155
179
|
this.crons.delete(filename);
|
|
@@ -157,6 +181,7 @@ export class EventsWatcher {
|
|
|
157
181
|
}
|
|
158
182
|
async handleFile(filename) {
|
|
159
183
|
const filePath = join(this.eventsDir, filename);
|
|
184
|
+
log.logInfo(`Loading event file: ${filename} from ${filePath}`);
|
|
160
185
|
// Parse with retries
|
|
161
186
|
let event = null;
|
|
162
187
|
let lastError = null;
|
|
@@ -175,10 +200,11 @@ export class EventsWatcher {
|
|
|
175
200
|
}
|
|
176
201
|
if (!event) {
|
|
177
202
|
log.logWarning(`Failed to parse event file after ${MAX_RETRIES} retries: ${filename}`, lastError?.message);
|
|
178
|
-
this.deleteFile(filename);
|
|
203
|
+
this.deleteFile(filename, "parse-failed");
|
|
179
204
|
return;
|
|
180
205
|
}
|
|
181
206
|
this.knownFiles.add(filename);
|
|
207
|
+
log.logInfo(`Parsed event file: ${filename} (${event.type} for ${event.platform}/${event.conversationId})`);
|
|
182
208
|
// Schedule based on type
|
|
183
209
|
switch (event.type) {
|
|
184
210
|
case "immediate":
|
|
@@ -205,6 +231,8 @@ export class EventsWatcher {
|
|
|
205
231
|
const platform = this.resolvePlatform(data.platform, filename);
|
|
206
232
|
const conversationKind = this.resolveConversationKind(platform, conversationId, data.conversationKind);
|
|
207
233
|
const userId = typeof data.userId === "string" ? data.userId : undefined;
|
|
234
|
+
const sessionKey = typeof data.sessionKey === "string" ? data.sessionKey : undefined;
|
|
235
|
+
const threadTs = typeof data.threadTs === "string" ? data.threadTs : undefined;
|
|
208
236
|
switch (data.type) {
|
|
209
237
|
case "immediate":
|
|
210
238
|
return {
|
|
@@ -214,6 +242,8 @@ export class EventsWatcher {
|
|
|
214
242
|
conversationKind,
|
|
215
243
|
userId,
|
|
216
244
|
text: data.text,
|
|
245
|
+
sessionKey,
|
|
246
|
+
threadTs,
|
|
217
247
|
};
|
|
218
248
|
case "one-shot":
|
|
219
249
|
if (!data.at) {
|
|
@@ -244,6 +274,7 @@ export class EventsWatcher {
|
|
|
244
274
|
text: data.text,
|
|
245
275
|
schedule: data.schedule,
|
|
246
276
|
timezone: data.timezone,
|
|
277
|
+
sessionKey,
|
|
247
278
|
};
|
|
248
279
|
default:
|
|
249
280
|
throw new Error(`Unknown event type '${data.type}' in ${filename}`);
|
|
@@ -267,13 +298,7 @@ export class EventsWatcher {
|
|
|
267
298
|
if (conversationKindValue === "direct" || conversationKindValue === "shared") {
|
|
268
299
|
return conversationKindValue;
|
|
269
300
|
}
|
|
270
|
-
|
|
271
|
-
return conversationId.startsWith("D") ? "direct" : "shared";
|
|
272
|
-
}
|
|
273
|
-
if (platform === "telegram") {
|
|
274
|
-
return conversationId.startsWith("-") ? "shared" : "direct";
|
|
275
|
-
}
|
|
276
|
-
return "shared";
|
|
301
|
+
return inferConversationKind(platform, conversationId);
|
|
277
302
|
}
|
|
278
303
|
handleImmediate(filename, event) {
|
|
279
304
|
const filePath = join(this.eventsDir, filename);
|
|
@@ -282,7 +307,7 @@ export class EventsWatcher {
|
|
|
282
307
|
const stat = statSync(filePath);
|
|
283
308
|
if (stat.mtimeMs < this.startTime) {
|
|
284
309
|
log.logInfo(`Stale immediate event, deleting: ${filename}`);
|
|
285
|
-
this.deleteFile(filename);
|
|
310
|
+
this.deleteFile(filename, "stale-immediate");
|
|
286
311
|
return;
|
|
287
312
|
}
|
|
288
313
|
}
|
|
@@ -299,17 +324,20 @@ export class EventsWatcher {
|
|
|
299
324
|
if (atTime <= now) {
|
|
300
325
|
// Past - delete without executing
|
|
301
326
|
log.logInfo(`One-shot event in the past, deleting: ${filename}`);
|
|
302
|
-
this.deleteFile(filename);
|
|
327
|
+
this.deleteFile(filename, "one-shot-in-past");
|
|
303
328
|
return;
|
|
304
329
|
}
|
|
305
330
|
const delay = atTime - now;
|
|
306
|
-
log.logInfo(`Scheduling one-shot event: ${filename} in ${Math.round(delay / 1000)}s`);
|
|
331
|
+
log.logInfo(`Scheduling one-shot event: ${filename} in ${Math.round(delay / 1000)}s (at=${event.at}, now=${new Date(now).toISOString()})`);
|
|
307
332
|
const timer = setTimeout(() => {
|
|
308
333
|
this.timers.delete(filename);
|
|
334
|
+
this.timerEventTypes.delete(filename);
|
|
309
335
|
log.logInfo(`Executing one-shot event: ${filename}`);
|
|
310
336
|
this.execute(filename, event);
|
|
311
337
|
}, delay);
|
|
312
338
|
this.timers.set(filename, timer);
|
|
339
|
+
this.timerEventTypes.set(filename, "one-shot");
|
|
340
|
+
log.logInfo(`Stored one-shot timer: ${filename} (active timers=${this.timers.size})`);
|
|
313
341
|
}
|
|
314
342
|
handlePeriodic(filename, event) {
|
|
315
343
|
try {
|
|
@@ -323,7 +351,7 @@ export class EventsWatcher {
|
|
|
323
351
|
}
|
|
324
352
|
catch (err) {
|
|
325
353
|
log.logWarning(`Invalid cron schedule for ${filename}: ${event.schedule}`, String(err));
|
|
326
|
-
this.deleteFile(filename);
|
|
354
|
+
this.deleteFile(filename, "invalid-cron");
|
|
327
355
|
}
|
|
328
356
|
}
|
|
329
357
|
execute(filename, event, deleteAfter = true) {
|
|
@@ -345,13 +373,14 @@ export class EventsWatcher {
|
|
|
345
373
|
if (!bot) {
|
|
346
374
|
log.logWarning(`No bot configured for event platform '${event.platform}'`, filename);
|
|
347
375
|
if (deleteAfter) {
|
|
348
|
-
this.deleteFile(filename);
|
|
376
|
+
this.deleteFile(filename, "missing-bot");
|
|
349
377
|
}
|
|
350
378
|
return;
|
|
351
379
|
}
|
|
352
380
|
// Create synthetic BotEvent. Keep a stable conversation session key so recurring
|
|
353
381
|
// reminders share context, but use a unique synthetic message id because
|
|
354
382
|
// some adapters treat ts/message id as a reply target.
|
|
383
|
+
const scopedEvent = event;
|
|
355
384
|
const syntheticEvent = {
|
|
356
385
|
type: "mention",
|
|
357
386
|
conversationId: event.conversationId,
|
|
@@ -359,24 +388,26 @@ export class EventsWatcher {
|
|
|
359
388
|
user: event.userId ?? "EVENT",
|
|
360
389
|
text: message,
|
|
361
390
|
ts: `event:${filename}`,
|
|
362
|
-
|
|
391
|
+
thread_ts: scopedEvent.threadTs,
|
|
392
|
+
sessionKey: scopedEvent.sessionKey ?? event.conversationId,
|
|
363
393
|
};
|
|
364
394
|
// Enqueue for processing
|
|
365
395
|
const enqueued = bot.enqueueEvent(syntheticEvent);
|
|
366
396
|
if (enqueued && deleteAfter) {
|
|
367
397
|
// Delete file after successful enqueue (immediate and one-shot)
|
|
368
|
-
this.deleteFile(filename);
|
|
398
|
+
this.deleteFile(filename, "executed-and-enqueued");
|
|
369
399
|
}
|
|
370
400
|
else if (!enqueued) {
|
|
371
401
|
log.logWarning(`Event queue full, discarded: ${filename}`);
|
|
372
402
|
// Still delete immediate/one-shot even if discarded
|
|
373
403
|
if (deleteAfter) {
|
|
374
|
-
this.deleteFile(filename);
|
|
404
|
+
this.deleteFile(filename, "queue-full-discarded");
|
|
375
405
|
}
|
|
376
406
|
}
|
|
377
407
|
}
|
|
378
|
-
deleteFile(filename) {
|
|
408
|
+
deleteFile(filename, reason = "unspecified") {
|
|
379
409
|
const filePath = join(this.eventsDir, filename);
|
|
410
|
+
log.logInfo(`Deleting event file: ${filename} (reason=${reason})`);
|
|
380
411
|
try {
|
|
381
412
|
unlinkSync(filePath);
|
|
382
413
|
}
|