@oh-my-pi/pi-mom 0.1.0

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