@draht/mom 2026.3.2-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.
Files changed (67) hide show
  1. package/CHANGELOG.md +448 -0
  2. package/README.md +490 -0
  3. package/dist/agent.d.ts +24 -0
  4. package/dist/agent.d.ts.map +1 -0
  5. package/dist/agent.js +758 -0
  6. package/dist/agent.js.map +1 -0
  7. package/dist/context.d.ts +70 -0
  8. package/dist/context.d.ts.map +1 -0
  9. package/dist/context.js +221 -0
  10. package/dist/context.js.map +1 -0
  11. package/dist/download.d.ts +2 -0
  12. package/dist/download.d.ts.map +1 -0
  13. package/dist/download.js +89 -0
  14. package/dist/download.js.map +1 -0
  15. package/dist/events.d.ts +57 -0
  16. package/dist/events.d.ts.map +1 -0
  17. package/dist/events.js +310 -0
  18. package/dist/events.js.map +1 -0
  19. package/dist/log.d.ts +39 -0
  20. package/dist/log.d.ts.map +1 -0
  21. package/dist/log.js +222 -0
  22. package/dist/log.js.map +1 -0
  23. package/dist/main.d.ts +3 -0
  24. package/dist/main.d.ts.map +1 -0
  25. package/dist/main.js +271 -0
  26. package/dist/main.js.map +1 -0
  27. package/dist/sandbox.d.ts +34 -0
  28. package/dist/sandbox.d.ts.map +1 -0
  29. package/dist/sandbox.js +183 -0
  30. package/dist/sandbox.js.map +1 -0
  31. package/dist/slack.d.ts +128 -0
  32. package/dist/slack.d.ts.map +1 -0
  33. package/dist/slack.js +455 -0
  34. package/dist/slack.js.map +1 -0
  35. package/dist/store.d.ts +60 -0
  36. package/dist/store.d.ts.map +1 -0
  37. package/dist/store.js +180 -0
  38. package/dist/store.js.map +1 -0
  39. package/dist/tools/attach.d.ts +10 -0
  40. package/dist/tools/attach.d.ts.map +1 -0
  41. package/dist/tools/attach.js +34 -0
  42. package/dist/tools/attach.js.map +1 -0
  43. package/dist/tools/bash.d.ts +10 -0
  44. package/dist/tools/bash.d.ts.map +1 -0
  45. package/dist/tools/bash.js +78 -0
  46. package/dist/tools/bash.js.map +1 -0
  47. package/dist/tools/edit.d.ts +11 -0
  48. package/dist/tools/edit.d.ts.map +1 -0
  49. package/dist/tools/edit.js +131 -0
  50. package/dist/tools/edit.js.map +1 -0
  51. package/dist/tools/index.d.ts +5 -0
  52. package/dist/tools/index.d.ts.map +1 -0
  53. package/dist/tools/index.js +16 -0
  54. package/dist/tools/index.js.map +1 -0
  55. package/dist/tools/read.d.ts +11 -0
  56. package/dist/tools/read.d.ts.map +1 -0
  57. package/dist/tools/read.js +134 -0
  58. package/dist/tools/read.js.map +1 -0
  59. package/dist/tools/truncate.d.ts +57 -0
  60. package/dist/tools/truncate.d.ts.map +1 -0
  61. package/dist/tools/truncate.js +184 -0
  62. package/dist/tools/truncate.js.map +1 -0
  63. package/dist/tools/write.d.ts +10 -0
  64. package/dist/tools/write.d.ts.map +1 -0
  65. package/dist/tools/write.js +33 -0
  66. package/dist/tools/write.js.map +1 -0
  67. package/package.json +54 -0
package/dist/agent.js ADDED
@@ -0,0 +1,758 @@
1
+ import { Agent } from "@draht/agent-core";
2
+ import { getModel } from "@draht/ai";
3
+ import { AgentSession, AuthStorage, convertToLlm, createExtensionRuntime, formatSkillsForPrompt, loadSkillsFromDir, ModelRegistry, SessionManager, } from "@draht/coding-agent";
4
+ import { existsSync, readFileSync } from "fs";
5
+ import { mkdir, writeFile } from "fs/promises";
6
+ import { homedir } from "os";
7
+ import { join } from "path";
8
+ import { MomSettingsManager, syncLogToSessionManager } from "./context.js";
9
+ import * as log from "./log.js";
10
+ import { createExecutor } from "./sandbox.js";
11
+ import { createMomTools, setUploadFunction } from "./tools/index.js";
12
+ // Hardcoded model for now - TODO: make configurable (issue #63)
13
+ const model = getModel("anthropic", "claude-sonnet-4-5");
14
+ async function getAnthropicApiKey(authStorage) {
15
+ const key = await authStorage.getApiKey("anthropic");
16
+ if (!key) {
17
+ throw new Error("No API key found for anthropic.\n\n" +
18
+ "Set an API key environment variable, or use /login with Anthropic and link to auth.json from " +
19
+ join(homedir(), ".pi", "mom", "auth.json"));
20
+ }
21
+ return key;
22
+ }
23
+ const IMAGE_MIME_TYPES = {
24
+ jpg: "image/jpeg",
25
+ jpeg: "image/jpeg",
26
+ png: "image/png",
27
+ gif: "image/gif",
28
+ webp: "image/webp",
29
+ };
30
+ function getImageMimeType(filename) {
31
+ return IMAGE_MIME_TYPES[filename.toLowerCase().split(".").pop() || ""];
32
+ }
33
+ function getMemory(channelDir) {
34
+ const parts = [];
35
+ // Read workspace-level memory (shared across all channels)
36
+ const workspaceMemoryPath = join(channelDir, "..", "MEMORY.md");
37
+ if (existsSync(workspaceMemoryPath)) {
38
+ try {
39
+ const content = readFileSync(workspaceMemoryPath, "utf-8").trim();
40
+ if (content) {
41
+ parts.push(`### Global Workspace Memory\n${content}`);
42
+ }
43
+ }
44
+ catch (error) {
45
+ log.logWarning("Failed to read workspace memory", `${workspaceMemoryPath}: ${error}`);
46
+ }
47
+ }
48
+ // Read channel-specific memory
49
+ const channelMemoryPath = join(channelDir, "MEMORY.md");
50
+ if (existsSync(channelMemoryPath)) {
51
+ try {
52
+ const content = readFileSync(channelMemoryPath, "utf-8").trim();
53
+ if (content) {
54
+ parts.push(`### Channel-Specific Memory\n${content}`);
55
+ }
56
+ }
57
+ catch (error) {
58
+ log.logWarning("Failed to read channel memory", `${channelMemoryPath}: ${error}`);
59
+ }
60
+ }
61
+ if (parts.length === 0) {
62
+ return "(no working memory yet)";
63
+ }
64
+ return parts.join("\n\n");
65
+ }
66
+ function loadMomSkills(channelDir, workspacePath) {
67
+ const skillMap = new Map();
68
+ // channelDir is the host path (e.g., /Users/.../data/C0A34FL8PMH)
69
+ // hostWorkspacePath is the parent directory on host
70
+ // workspacePath is the container path (e.g., /workspace)
71
+ const hostWorkspacePath = join(channelDir, "..");
72
+ // Helper to translate host paths to container paths
73
+ const translatePath = (hostPath) => {
74
+ if (hostPath.startsWith(hostWorkspacePath)) {
75
+ return workspacePath + hostPath.slice(hostWorkspacePath.length);
76
+ }
77
+ return hostPath;
78
+ };
79
+ // Load workspace-level skills (global)
80
+ const workspaceSkillsDir = join(hostWorkspacePath, "skills");
81
+ for (const skill of loadSkillsFromDir({ dir: workspaceSkillsDir, source: "workspace" }).skills) {
82
+ // Translate paths to container paths for system prompt
83
+ skill.filePath = translatePath(skill.filePath);
84
+ skill.baseDir = translatePath(skill.baseDir);
85
+ skillMap.set(skill.name, skill);
86
+ }
87
+ // Load channel-specific skills (override workspace skills on collision)
88
+ const channelSkillsDir = join(channelDir, "skills");
89
+ for (const skill of loadSkillsFromDir({ dir: channelSkillsDir, source: "channel" }).skills) {
90
+ skill.filePath = translatePath(skill.filePath);
91
+ skill.baseDir = translatePath(skill.baseDir);
92
+ skillMap.set(skill.name, skill);
93
+ }
94
+ return Array.from(skillMap.values());
95
+ }
96
+ function buildSystemPrompt(workspacePath, channelId, memory, sandboxConfig, channels, users, skills) {
97
+ const channelPath = `${workspacePath}/${channelId}`;
98
+ const isDocker = sandboxConfig.type === "docker";
99
+ // Format channel mappings
100
+ const channelMappings = channels.length > 0 ? channels.map((c) => `${c.id}\t#${c.name}`).join("\n") : "(no channels loaded)";
101
+ // Format user mappings
102
+ const userMappings = users.length > 0 ? users.map((u) => `${u.id}\t@${u.userName}\t${u.displayName}`).join("\n") : "(no users loaded)";
103
+ const envDescription = isDocker
104
+ ? `You are running inside a Docker container (Alpine Linux).
105
+ - Bash working directory: / (use cd or absolute paths)
106
+ - Install tools with: apk add <package>
107
+ - Your changes persist across sessions`
108
+ : `You are running directly on the host machine.
109
+ - Bash working directory: ${process.cwd()}
110
+ - Be careful with system modifications`;
111
+ return `You are mom, a Slack bot assistant. Be concise. No emojis.
112
+
113
+ ## Context
114
+ - For current date/time, use: date
115
+ - You have access to previous conversation context including tool results from prior turns.
116
+ - For older history beyond your context, search log.jsonl (contains user messages and your final responses, but not tool results).
117
+
118
+ ## Slack Formatting (mrkdwn, NOT Markdown)
119
+ Bold: *text*, Italic: _text_, Code: \`code\`, Block: \`\`\`code\`\`\`, Links: <url|text>
120
+ Do NOT use **double asterisks** or [markdown](links).
121
+
122
+ ## Slack IDs
123
+ Channels: ${channelMappings}
124
+
125
+ Users: ${userMappings}
126
+
127
+ When mentioning users, use <@username> format (e.g., <@mario>).
128
+
129
+ ## Environment
130
+ ${envDescription}
131
+
132
+ ## Workspace Layout
133
+ ${workspacePath}/
134
+ ├── MEMORY.md # Global memory (all channels)
135
+ ├── skills/ # Global CLI tools you create
136
+ └── ${channelId}/ # This channel
137
+ ├── MEMORY.md # Channel-specific memory
138
+ ├── log.jsonl # Message history (no tool results)
139
+ ├── attachments/ # User-shared files
140
+ ├── scratch/ # Your working directory
141
+ └── skills/ # Channel-specific tools
142
+
143
+ ## Skills (Custom CLI Tools)
144
+ You can create reusable CLI tools for recurring tasks (email, APIs, data processing, etc.).
145
+
146
+ ### Creating Skills
147
+ Store in \`${workspacePath}/skills/<name>/\` (global) or \`${channelPath}/skills/<name>/\` (channel-specific).
148
+ Each skill directory needs a \`SKILL.md\` with YAML frontmatter:
149
+
150
+ \`\`\`markdown
151
+ ---
152
+ name: skill-name
153
+ description: Short description of what this skill does
154
+ ---
155
+
156
+ # Skill Name
157
+
158
+ Usage instructions, examples, etc.
159
+ Scripts are in: {baseDir}/
160
+ \`\`\`
161
+
162
+ \`name\` and \`description\` are required. Use \`{baseDir}\` as placeholder for the skill's directory path.
163
+
164
+ ### Available Skills
165
+ ${skills.length > 0 ? formatSkillsForPrompt(skills) : "(no skills installed yet)"}
166
+
167
+ ## Events
168
+ You can schedule events that wake you up at specific times or when external things happen. Events are JSON files in \`${workspacePath}/events/\`.
169
+
170
+ ### Event Types
171
+
172
+ **Immediate** - Triggers as soon as harness sees the file. Use in scripts/webhooks to signal external events.
173
+ \`\`\`json
174
+ {"type": "immediate", "channelId": "${channelId}", "text": "New GitHub issue opened"}
175
+ \`\`\`
176
+
177
+ **One-shot** - Triggers once at a specific time. Use for reminders.
178
+ \`\`\`json
179
+ {"type": "one-shot", "channelId": "${channelId}", "text": "Remind Mario about dentist", "at": "2025-12-15T09:00:00+01:00"}
180
+ \`\`\`
181
+
182
+ **Periodic** - Triggers on a cron schedule. Use for recurring tasks.
183
+ \`\`\`json
184
+ {"type": "periodic", "channelId": "${channelId}", "text": "Check inbox and summarize", "schedule": "0 9 * * 1-5", "timezone": "${Intl.DateTimeFormat().resolvedOptions().timeZone}"}
185
+ \`\`\`
186
+
187
+ ### Cron Format
188
+ \`minute hour day-of-month month day-of-week\`
189
+ - \`0 9 * * *\` = daily at 9:00
190
+ - \`0 9 * * 1-5\` = weekdays at 9:00
191
+ - \`30 14 * * 1\` = Mondays at 14:30
192
+ - \`0 0 1 * *\` = first of each month at midnight
193
+
194
+ ### Timezones
195
+ All \`at\` timestamps must include offset (e.g., \`+01:00\`). Periodic events use IANA timezone names. The harness runs in ${Intl.DateTimeFormat().resolvedOptions().timeZone}. When users mention times without timezone, assume ${Intl.DateTimeFormat().resolvedOptions().timeZone}.
196
+
197
+ ### Creating Events
198
+ Use unique filenames to avoid overwriting existing events. Include a timestamp or random suffix:
199
+ \`\`\`bash
200
+ cat > ${workspacePath}/events/dentist-reminder-$(date +%s).json << 'EOF'
201
+ {"type": "one-shot", "channelId": "${channelId}", "text": "Dentist tomorrow", "at": "2025-12-14T09:00:00+01:00"}
202
+ EOF
203
+ \`\`\`
204
+ Or check if file exists first before creating.
205
+
206
+ ### Managing Events
207
+ - List: \`ls ${workspacePath}/events/\`
208
+ - View: \`cat ${workspacePath}/events/foo.json\`
209
+ - Delete/cancel: \`rm ${workspacePath}/events/foo.json\`
210
+
211
+ ### When Events Trigger
212
+ You receive a message like:
213
+ \`\`\`
214
+ [EVENT:dentist-reminder.json:one-shot:2025-12-14T09:00:00+01:00] Dentist tomorrow
215
+ \`\`\`
216
+ Immediate and one-shot events auto-delete after triggering. Periodic events persist until you delete them.
217
+
218
+ ### Silent Completion
219
+ For periodic events where there's nothing to report, respond with just \`[SILENT]\` (no other text). This deletes the status message and posts nothing to Slack. Use this to avoid spamming the channel when periodic checks find nothing actionable.
220
+
221
+ ### Debouncing
222
+ When writing programs that create immediate events (email watchers, webhook handlers, etc.), always debounce. If 50 emails arrive in a minute, don't create 50 immediate events. Instead collect events over a window and create ONE immediate event summarizing what happened, or just signal "new activity, check inbox" rather than per-item events. Or simpler: use a periodic event to check for new items every N minutes instead of immediate events.
223
+
224
+ ### Limits
225
+ Maximum 5 events can be queued. Don't create excessive immediate or periodic events.
226
+
227
+ ## Memory
228
+ Write to MEMORY.md files to persist context across conversations.
229
+ - Global (${workspacePath}/MEMORY.md): skills, preferences, project info
230
+ - Channel (${channelPath}/MEMORY.md): channel-specific decisions, ongoing work
231
+ Update when you learn something important or when asked to remember something.
232
+
233
+ ### Current Memory
234
+ ${memory}
235
+
236
+ ## System Configuration Log
237
+ Maintain ${workspacePath}/SYSTEM.md to log all environment modifications:
238
+ - Installed packages (apk add, npm install, pip install)
239
+ - Environment variables set
240
+ - Config files modified (~/.gitconfig, cron jobs, etc.)
241
+ - Skill dependencies installed
242
+
243
+ Update this file whenever you modify the environment. On fresh container, read it first to restore your setup.
244
+
245
+ ## Log Queries (for older history)
246
+ Format: \`{"date":"...","ts":"...","user":"...","userName":"...","text":"...","isBot":false}\`
247
+ The log contains user messages and your final responses (not tool calls/results).
248
+ ${isDocker ? "Install jq: apk add jq" : ""}
249
+
250
+ \`\`\`bash
251
+ # Recent messages
252
+ tail -30 log.jsonl | jq -c '{date: .date[0:19], user: (.userName // .user), text}'
253
+
254
+ # Search for specific topic
255
+ grep -i "topic" log.jsonl | jq -c '{date: .date[0:19], user: (.userName // .user), text}'
256
+
257
+ # Messages from specific user
258
+ grep '"userName":"mario"' log.jsonl | tail -20 | jq -c '{date: .date[0:19], text}'
259
+ \`\`\`
260
+
261
+ ## Tools
262
+ - bash: Run shell commands (primary tool). Install packages as needed.
263
+ - read: Read files
264
+ - write: Create/overwrite files
265
+ - edit: Surgical file edits
266
+ - attach: Share files to Slack
267
+
268
+ Each tool requires a "label" parameter (shown to user).
269
+ `;
270
+ }
271
+ function truncate(text, maxLen) {
272
+ if (text.length <= maxLen)
273
+ return text;
274
+ return `${text.substring(0, maxLen - 3)}...`;
275
+ }
276
+ function extractToolResultText(result) {
277
+ if (typeof result === "string") {
278
+ return result;
279
+ }
280
+ if (result &&
281
+ typeof result === "object" &&
282
+ "content" in result &&
283
+ Array.isArray(result.content)) {
284
+ const content = result.content;
285
+ const textParts = [];
286
+ for (const part of content) {
287
+ if (part.type === "text" && part.text) {
288
+ textParts.push(part.text);
289
+ }
290
+ }
291
+ if (textParts.length > 0) {
292
+ return textParts.join("\n");
293
+ }
294
+ }
295
+ return JSON.stringify(result);
296
+ }
297
+ function formatToolArgsForSlack(_toolName, args) {
298
+ const lines = [];
299
+ for (const [key, value] of Object.entries(args)) {
300
+ if (key === "label")
301
+ continue;
302
+ if (key === "path" && typeof value === "string") {
303
+ const offset = args.offset;
304
+ const limit = args.limit;
305
+ if (offset !== undefined && limit !== undefined) {
306
+ lines.push(`${value}:${offset}-${offset + limit}`);
307
+ }
308
+ else {
309
+ lines.push(value);
310
+ }
311
+ continue;
312
+ }
313
+ if (key === "offset" || key === "limit")
314
+ continue;
315
+ if (typeof value === "string") {
316
+ lines.push(value);
317
+ }
318
+ else {
319
+ lines.push(JSON.stringify(value));
320
+ }
321
+ }
322
+ return lines.join("\n");
323
+ }
324
+ // Cache runners per channel
325
+ const channelRunners = new Map();
326
+ /**
327
+ * Get or create an AgentRunner for a channel.
328
+ * Runners are cached - one per channel, persistent across messages.
329
+ */
330
+ export function getOrCreateRunner(sandboxConfig, channelId, channelDir) {
331
+ const existing = channelRunners.get(channelId);
332
+ if (existing)
333
+ return existing;
334
+ const runner = createRunner(sandboxConfig, channelId, channelDir);
335
+ channelRunners.set(channelId, runner);
336
+ return runner;
337
+ }
338
+ /**
339
+ * Create a new AgentRunner for a channel.
340
+ * Sets up the session and subscribes to events once.
341
+ */
342
+ function createRunner(sandboxConfig, channelId, channelDir) {
343
+ const executor = createExecutor(sandboxConfig);
344
+ const workspacePath = executor.getWorkspacePath(channelDir.replace(`/${channelId}`, ""));
345
+ // Create tools
346
+ const tools = createMomTools(executor);
347
+ // Initial system prompt (will be updated each run with fresh memory/channels/users/skills)
348
+ const memory = getMemory(channelDir);
349
+ const skills = loadMomSkills(channelDir, workspacePath);
350
+ const systemPrompt = buildSystemPrompt(workspacePath, channelId, memory, sandboxConfig, [], [], skills);
351
+ // Create session manager and settings manager
352
+ // Use a fixed context.jsonl file per channel (not timestamped like coding-agent)
353
+ const contextFile = join(channelDir, "context.jsonl");
354
+ const sessionManager = SessionManager.open(contextFile, channelDir);
355
+ const settingsManager = new MomSettingsManager(join(channelDir, ".."));
356
+ // Create AuthStorage and ModelRegistry
357
+ // Auth stored outside workspace so agent can't access it
358
+ const authStorage = AuthStorage.create(join(homedir(), ".pi", "mom", "auth.json"));
359
+ const modelRegistry = new ModelRegistry(authStorage);
360
+ // Create agent
361
+ const agent = new Agent({
362
+ initialState: {
363
+ systemPrompt,
364
+ model,
365
+ thinkingLevel: "off",
366
+ tools,
367
+ },
368
+ convertToLlm,
369
+ getApiKey: async () => getAnthropicApiKey(authStorage),
370
+ });
371
+ // Load existing messages
372
+ const loadedSession = sessionManager.buildSessionContext();
373
+ if (loadedSession.messages.length > 0) {
374
+ agent.replaceMessages(loadedSession.messages);
375
+ log.logInfo(`[${channelId}] Loaded ${loadedSession.messages.length} messages from context.jsonl`);
376
+ }
377
+ const resourceLoader = {
378
+ getExtensions: () => ({ extensions: [], errors: [], runtime: createExtensionRuntime() }),
379
+ getSkills: () => ({ skills: [], diagnostics: [] }),
380
+ getPrompts: () => ({ prompts: [], diagnostics: [] }),
381
+ getThemes: () => ({ themes: [], diagnostics: [] }),
382
+ getAgentsFiles: () => ({ agentsFiles: [] }),
383
+ getSystemPrompt: () => systemPrompt,
384
+ getAppendSystemPrompt: () => [],
385
+ getPathMetadata: () => new Map(),
386
+ extendResources: () => { },
387
+ reload: async () => { },
388
+ };
389
+ const baseToolsOverride = Object.fromEntries(tools.map((tool) => [tool.name, tool]));
390
+ // Create AgentSession wrapper
391
+ const session = new AgentSession({
392
+ agent,
393
+ sessionManager,
394
+ settingsManager: settingsManager,
395
+ cwd: process.cwd(),
396
+ modelRegistry,
397
+ resourceLoader,
398
+ baseToolsOverride,
399
+ });
400
+ // Mutable per-run state - event handler references this
401
+ const runState = {
402
+ ctx: null,
403
+ logCtx: null,
404
+ queue: null,
405
+ pendingTools: new Map(),
406
+ totalUsage: {
407
+ input: 0,
408
+ output: 0,
409
+ cacheRead: 0,
410
+ cacheWrite: 0,
411
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
412
+ },
413
+ stopReason: "stop",
414
+ errorMessage: undefined,
415
+ };
416
+ // Subscribe to events ONCE
417
+ session.subscribe(async (event) => {
418
+ // Skip if no active run
419
+ if (!runState.ctx || !runState.logCtx || !runState.queue)
420
+ return;
421
+ const { ctx, logCtx, queue, pendingTools } = runState;
422
+ if (event.type === "tool_execution_start") {
423
+ const agentEvent = event;
424
+ const args = agentEvent.args;
425
+ const label = args.label || agentEvent.toolName;
426
+ pendingTools.set(agentEvent.toolCallId, {
427
+ toolName: agentEvent.toolName,
428
+ args: agentEvent.args,
429
+ startTime: Date.now(),
430
+ });
431
+ log.logToolStart(logCtx, agentEvent.toolName, label, agentEvent.args);
432
+ queue.enqueue(() => ctx.respond(`_→ ${label}_`, false), "tool label");
433
+ }
434
+ else if (event.type === "tool_execution_end") {
435
+ const agentEvent = event;
436
+ const resultStr = extractToolResultText(agentEvent.result);
437
+ const pending = pendingTools.get(agentEvent.toolCallId);
438
+ pendingTools.delete(agentEvent.toolCallId);
439
+ const durationMs = pending ? Date.now() - pending.startTime : 0;
440
+ if (agentEvent.isError) {
441
+ log.logToolError(logCtx, agentEvent.toolName, durationMs, resultStr);
442
+ }
443
+ else {
444
+ log.logToolSuccess(logCtx, agentEvent.toolName, durationMs, resultStr);
445
+ }
446
+ // Post args + result to thread
447
+ const label = pending?.args ? pending.args.label : undefined;
448
+ const argsFormatted = pending
449
+ ? formatToolArgsForSlack(agentEvent.toolName, pending.args)
450
+ : "(args not found)";
451
+ const duration = (durationMs / 1000).toFixed(1);
452
+ let threadMessage = `*${agentEvent.isError ? "✗" : "✓"} ${agentEvent.toolName}*`;
453
+ if (label)
454
+ threadMessage += `: ${label}`;
455
+ threadMessage += ` (${duration}s)\n`;
456
+ if (argsFormatted)
457
+ threadMessage += `\`\`\`\n${argsFormatted}\n\`\`\`\n`;
458
+ threadMessage += `*Result:*\n\`\`\`\n${resultStr}\n\`\`\``;
459
+ queue.enqueueMessage(threadMessage, "thread", "tool result thread", false);
460
+ if (agentEvent.isError) {
461
+ queue.enqueue(() => ctx.respond(`_Error: ${truncate(resultStr, 200)}_`, false), "tool error");
462
+ }
463
+ }
464
+ else if (event.type === "message_start") {
465
+ const agentEvent = event;
466
+ if (agentEvent.message.role === "assistant") {
467
+ log.logResponseStart(logCtx);
468
+ }
469
+ }
470
+ else if (event.type === "message_end") {
471
+ const agentEvent = event;
472
+ if (agentEvent.message.role === "assistant") {
473
+ const assistantMsg = agentEvent.message;
474
+ if (assistantMsg.stopReason) {
475
+ runState.stopReason = assistantMsg.stopReason;
476
+ }
477
+ if (assistantMsg.errorMessage) {
478
+ runState.errorMessage = assistantMsg.errorMessage;
479
+ }
480
+ if (assistantMsg.usage) {
481
+ runState.totalUsage.input += assistantMsg.usage.input;
482
+ runState.totalUsage.output += assistantMsg.usage.output;
483
+ runState.totalUsage.cacheRead += assistantMsg.usage.cacheRead;
484
+ runState.totalUsage.cacheWrite += assistantMsg.usage.cacheWrite;
485
+ runState.totalUsage.cost.input += assistantMsg.usage.cost.input;
486
+ runState.totalUsage.cost.output += assistantMsg.usage.cost.output;
487
+ runState.totalUsage.cost.cacheRead += assistantMsg.usage.cost.cacheRead;
488
+ runState.totalUsage.cost.cacheWrite += assistantMsg.usage.cost.cacheWrite;
489
+ runState.totalUsage.cost.total += assistantMsg.usage.cost.total;
490
+ }
491
+ const content = agentEvent.message.content;
492
+ const thinkingParts = [];
493
+ const textParts = [];
494
+ for (const part of content) {
495
+ if (part.type === "thinking") {
496
+ thinkingParts.push(part.thinking);
497
+ }
498
+ else if (part.type === "text") {
499
+ textParts.push(part.text);
500
+ }
501
+ }
502
+ const text = textParts.join("\n");
503
+ for (const thinking of thinkingParts) {
504
+ log.logThinking(logCtx, thinking);
505
+ queue.enqueueMessage(`_${thinking}_`, "main", "thinking main");
506
+ queue.enqueueMessage(`_${thinking}_`, "thread", "thinking thread", false);
507
+ }
508
+ if (text.trim()) {
509
+ log.logResponse(logCtx, text);
510
+ queue.enqueueMessage(text, "main", "response main");
511
+ queue.enqueueMessage(text, "thread", "response thread", false);
512
+ }
513
+ }
514
+ }
515
+ else if (event.type === "auto_compaction_start") {
516
+ log.logInfo(`Auto-compaction started (reason: ${event.reason})`);
517
+ queue.enqueue(() => ctx.respond("_Compacting context..._", false), "compaction start");
518
+ }
519
+ else if (event.type === "auto_compaction_end") {
520
+ const compEvent = event;
521
+ if (compEvent.result) {
522
+ log.logInfo(`Auto-compaction complete: ${compEvent.result.tokensBefore} tokens compacted`);
523
+ }
524
+ else if (compEvent.aborted) {
525
+ log.logInfo("Auto-compaction aborted");
526
+ }
527
+ }
528
+ else if (event.type === "auto_retry_start") {
529
+ const retryEvent = event;
530
+ log.logWarning(`Retrying (${retryEvent.attempt}/${retryEvent.maxAttempts})`, retryEvent.errorMessage);
531
+ queue.enqueue(() => ctx.respond(`_Retrying (${retryEvent.attempt}/${retryEvent.maxAttempts})..._`, false), "retry");
532
+ }
533
+ });
534
+ // Slack message limit
535
+ const SLACK_MAX_LENGTH = 40000;
536
+ const splitForSlack = (text) => {
537
+ if (text.length <= SLACK_MAX_LENGTH)
538
+ return [text];
539
+ const parts = [];
540
+ let remaining = text;
541
+ let partNum = 1;
542
+ while (remaining.length > 0) {
543
+ const chunk = remaining.substring(0, SLACK_MAX_LENGTH - 50);
544
+ remaining = remaining.substring(SLACK_MAX_LENGTH - 50);
545
+ const suffix = remaining.length > 0 ? `\n_(continued ${partNum}...)_` : "";
546
+ parts.push(chunk + suffix);
547
+ partNum++;
548
+ }
549
+ return parts;
550
+ };
551
+ return {
552
+ async run(ctx, _store, _pendingMessages) {
553
+ // Ensure channel directory exists
554
+ await mkdir(channelDir, { recursive: true });
555
+ // Sync messages from log.jsonl that arrived while we were offline or busy
556
+ // Exclude the current message (it will be added via prompt())
557
+ const syncedCount = syncLogToSessionManager(sessionManager, channelDir, ctx.message.ts);
558
+ if (syncedCount > 0) {
559
+ log.logInfo(`[${channelId}] Synced ${syncedCount} messages from log.jsonl`);
560
+ }
561
+ // Reload messages from context.jsonl
562
+ // This picks up any messages synced above
563
+ const reloadedSession = sessionManager.buildSessionContext();
564
+ if (reloadedSession.messages.length > 0) {
565
+ agent.replaceMessages(reloadedSession.messages);
566
+ log.logInfo(`[${channelId}] Reloaded ${reloadedSession.messages.length} messages from context`);
567
+ }
568
+ // Update system prompt with fresh memory, channel/user info, and skills
569
+ const memory = getMemory(channelDir);
570
+ const skills = loadMomSkills(channelDir, workspacePath);
571
+ const systemPrompt = buildSystemPrompt(workspacePath, channelId, memory, sandboxConfig, ctx.channels, ctx.users, skills);
572
+ session.agent.setSystemPrompt(systemPrompt);
573
+ // Set up file upload function
574
+ setUploadFunction(async (filePath, title) => {
575
+ const hostPath = translateToHostPath(filePath, channelDir, workspacePath, channelId);
576
+ await ctx.uploadFile(hostPath, title);
577
+ });
578
+ // Reset per-run state
579
+ runState.ctx = ctx;
580
+ runState.logCtx = {
581
+ channelId: ctx.message.channel,
582
+ userName: ctx.message.userName,
583
+ channelName: ctx.channelName,
584
+ };
585
+ runState.pendingTools.clear();
586
+ runState.totalUsage = {
587
+ input: 0,
588
+ output: 0,
589
+ cacheRead: 0,
590
+ cacheWrite: 0,
591
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
592
+ };
593
+ runState.stopReason = "stop";
594
+ runState.errorMessage = undefined;
595
+ // Create queue for this run
596
+ let queueChain = Promise.resolve();
597
+ runState.queue = {
598
+ enqueue(fn, errorContext) {
599
+ queueChain = queueChain.then(async () => {
600
+ try {
601
+ await fn();
602
+ }
603
+ catch (err) {
604
+ const errMsg = err instanceof Error ? err.message : String(err);
605
+ log.logWarning(`Slack API error (${errorContext})`, errMsg);
606
+ try {
607
+ await ctx.respondInThread(`_Error: ${errMsg}_`);
608
+ }
609
+ catch {
610
+ // Ignore
611
+ }
612
+ }
613
+ });
614
+ },
615
+ enqueueMessage(text, target, errorContext, doLog = true) {
616
+ const parts = splitForSlack(text);
617
+ for (const part of parts) {
618
+ this.enqueue(() => (target === "main" ? ctx.respond(part, doLog) : ctx.respondInThread(part)), errorContext);
619
+ }
620
+ },
621
+ };
622
+ // Log context info
623
+ log.logInfo(`Context sizes - system: ${systemPrompt.length} chars, memory: ${memory.length} chars`);
624
+ log.logInfo(`Channels: ${ctx.channels.length}, Users: ${ctx.users.length}`);
625
+ // Build user message with timestamp and username prefix
626
+ // Format: "[YYYY-MM-DD HH:MM:SS+HH:MM] [username]: message" so LLM knows when and who
627
+ const now = new Date();
628
+ const pad = (n) => n.toString().padStart(2, "0");
629
+ const offset = -now.getTimezoneOffset();
630
+ const offsetSign = offset >= 0 ? "+" : "-";
631
+ const offsetHours = pad(Math.floor(Math.abs(offset) / 60));
632
+ const offsetMins = pad(Math.abs(offset) % 60);
633
+ const timestamp = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}${offsetSign}${offsetHours}:${offsetMins}`;
634
+ let userMessage = `[${timestamp}] [${ctx.message.userName || "unknown"}]: ${ctx.message.text}`;
635
+ const imageAttachments = [];
636
+ const nonImagePaths = [];
637
+ for (const a of ctx.message.attachments || []) {
638
+ const fullPath = `${workspacePath}/${a.local}`;
639
+ const mimeType = getImageMimeType(a.local);
640
+ if (mimeType && existsSync(fullPath)) {
641
+ try {
642
+ imageAttachments.push({
643
+ type: "image",
644
+ mimeType,
645
+ data: readFileSync(fullPath).toString("base64"),
646
+ });
647
+ }
648
+ catch {
649
+ nonImagePaths.push(fullPath);
650
+ }
651
+ }
652
+ else {
653
+ nonImagePaths.push(fullPath);
654
+ }
655
+ }
656
+ if (nonImagePaths.length > 0) {
657
+ userMessage += `\n\n<slack_attachments>\n${nonImagePaths.join("\n")}\n</slack_attachments>`;
658
+ }
659
+ // Debug: write context to last_prompt.jsonl
660
+ const debugContext = {
661
+ systemPrompt,
662
+ messages: session.messages,
663
+ newUserMessage: userMessage,
664
+ imageAttachmentCount: imageAttachments.length,
665
+ };
666
+ await writeFile(join(channelDir, "last_prompt.jsonl"), JSON.stringify(debugContext, null, 2));
667
+ await session.prompt(userMessage, imageAttachments.length > 0 ? { images: imageAttachments } : undefined);
668
+ // Wait for queued messages
669
+ await queueChain;
670
+ // Handle error case - update main message and post error to thread
671
+ if (runState.stopReason === "error" && runState.errorMessage) {
672
+ try {
673
+ await ctx.replaceMessage("_Sorry, something went wrong_");
674
+ await ctx.respondInThread(`_Error: ${runState.errorMessage}_`);
675
+ }
676
+ catch (err) {
677
+ const errMsg = err instanceof Error ? err.message : String(err);
678
+ log.logWarning("Failed to post error message", errMsg);
679
+ }
680
+ }
681
+ else {
682
+ // Final message update
683
+ const messages = session.messages;
684
+ const lastAssistant = messages.filter((m) => m.role === "assistant").pop();
685
+ const finalText = lastAssistant?.content
686
+ .filter((c) => c.type === "text")
687
+ .map((c) => c.text)
688
+ .join("\n") || "";
689
+ // Check for [SILENT] marker - delete message and thread instead of posting
690
+ if (finalText.trim() === "[SILENT]" || finalText.trim().startsWith("[SILENT]")) {
691
+ try {
692
+ await ctx.deleteMessage();
693
+ log.logInfo("Silent response - deleted message and thread");
694
+ }
695
+ catch (err) {
696
+ const errMsg = err instanceof Error ? err.message : String(err);
697
+ log.logWarning("Failed to delete message for silent response", errMsg);
698
+ }
699
+ }
700
+ else if (finalText.trim()) {
701
+ try {
702
+ const mainText = finalText.length > SLACK_MAX_LENGTH
703
+ ? `${finalText.substring(0, SLACK_MAX_LENGTH - 50)}\n\n_(see thread for full response)_`
704
+ : finalText;
705
+ await ctx.replaceMessage(mainText);
706
+ }
707
+ catch (err) {
708
+ const errMsg = err instanceof Error ? err.message : String(err);
709
+ log.logWarning("Failed to replace message with final text", errMsg);
710
+ }
711
+ }
712
+ }
713
+ // Log usage summary with context info
714
+ if (runState.totalUsage.cost.total > 0) {
715
+ // Get last non-aborted assistant message for context calculation
716
+ const messages = session.messages;
717
+ const lastAssistantMessage = messages
718
+ .slice()
719
+ .reverse()
720
+ .find((m) => m.role === "assistant" && m.stopReason !== "aborted");
721
+ const contextTokens = lastAssistantMessage
722
+ ? lastAssistantMessage.usage.input +
723
+ lastAssistantMessage.usage.output +
724
+ lastAssistantMessage.usage.cacheRead +
725
+ lastAssistantMessage.usage.cacheWrite
726
+ : 0;
727
+ const contextWindow = model.contextWindow || 200000;
728
+ const summary = log.logUsageSummary(runState.logCtx, runState.totalUsage, contextTokens, contextWindow);
729
+ runState.queue.enqueue(() => ctx.respondInThread(summary), "usage summary");
730
+ await queueChain;
731
+ }
732
+ // Clear run state
733
+ runState.ctx = null;
734
+ runState.logCtx = null;
735
+ runState.queue = null;
736
+ return { stopReason: runState.stopReason, errorMessage: runState.errorMessage };
737
+ },
738
+ abort() {
739
+ session.abort();
740
+ },
741
+ };
742
+ }
743
+ /**
744
+ * Translate container path back to host path for file operations
745
+ */
746
+ function translateToHostPath(containerPath, channelDir, workspacePath, channelId) {
747
+ if (workspacePath === "/workspace") {
748
+ const prefix = `/workspace/${channelId}/`;
749
+ if (containerPath.startsWith(prefix)) {
750
+ return join(channelDir, containerPath.slice(prefix.length));
751
+ }
752
+ if (containerPath.startsWith("/workspace/")) {
753
+ return join(channelDir, "..", containerPath.slice("/workspace/".length));
754
+ }
755
+ }
756
+ return containerPath;
757
+ }
758
+ //# sourceMappingURL=agent.js.map