chore: drop non-code junk (pi-mono-reference, runtime state, root duplicates)
Remove files that should never have been tracked: - pi-mono-reference/: 688-file copy of an upstream monorepo, zero imports, used only as offline human reference - build/electron-builder.runtime.json: environment-specific build artifact with hardcoded local paths - state/cancel-signal-state.json: runtime state written by the app - prompt.txt, logo.svg (root): exact duplicates of the canonical copies under .planning/archive/ and assets/ Add state/ to .gitignore so future runtime state stays out.
This commit is contained in:
1 parent
34ca5b77f1
commit
1f7230fe87
691 files changed
+3
-190434
No files matched your search
@@ -1,882 +0,0 @@
|
||||
import { Agent, type AgentEvent } from "@mariozechner/pi-agent-core";
|
||||
import { getModel, type ImageContent } from "@mariozechner/pi-ai";
|
||||
import {
|
||||
AgentSession,
|
||||
AuthStorage,
|
||||
convertToLlm,
|
||||
createExtensionRuntime,
|
||||
formatSkillsForPrompt,
|
||||
loadSkillsFromDir,
|
||||
ModelRegistry,
|
||||
type ResourceLoader,
|
||||
SessionManager,
|
||||
type Skill,
|
||||
} from "@mariozechner/pi-coding-agent";
|
||||
import { existsSync, readFileSync } from "fs";
|
||||
import { mkdir, writeFile } from "fs/promises";
|
||||
import { homedir } from "os";
|
||||
import { join } from "path";
|
||||
import { createMomSettingsManager, syncLogToSessionManager } from "./context.js";
|
||||
import * as log from "./log.js";
|
||||
import { createExecutor, type SandboxConfig } from "./sandbox.js";
|
||||
import type { ChannelInfo, SlackContext, UserInfo } from "./slack.js";
|
||||
import type { ChannelStore } from "./store.js";
|
||||
import { createMomTools, setUploadFunction } from "./tools/index.js";
|
||||
|
||||
// Hardcoded model for now - TODO: make configurable (issue #63)
|
||||
const model = getModel("anthropic", "claude-sonnet-4-5");
|
||||
|
||||
export interface PendingMessage {
|
||||
userName: string;
|
||||
text: string;
|
||||
attachments: { local: string }[];
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export interface AgentRunner {
|
||||
run(
|
||||
ctx: SlackContext,
|
||||
store: ChannelStore,
|
||||
pendingMessages?: PendingMessage[],
|
||||
): Promise<{ stopReason: string; errorMessage?: string }>;
|
||||
abort(): void;
|
||||
}
|
||||
|
||||
async function getAnthropicApiKey(authStorage: AuthStorage): Promise<string> {
|
||||
const key = await authStorage.getApiKey("anthropic");
|
||||
if (!key) {
|
||||
throw new Error(
|
||||
"No API key found for anthropic.\n\n" +
|
||||
"Set an API key environment variable, or use /login with Anthropic and link to auth.json from " +
|
||||
join(homedir(), ".pi", "mom", "auth.json"),
|
||||
);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
const IMAGE_MIME_TYPES: Record<string, string> = {
|
||||
jpg: "image/jpeg",
|
||||
jpeg: "image/jpeg",
|
||||
png: "image/png",
|
||||
gif: "image/gif",
|
||||
webp: "image/webp",
|
||||
};
|
||||
|
||||
function getImageMimeType(filename: string): string | undefined {
|
||||
return IMAGE_MIME_TYPES[filename.toLowerCase().split(".").pop() || ""];
|
||||
}
|
||||
|
||||
function getMemory(channelDir: string): string {
|
||||
const parts: string[] = [];
|
||||
|
||||
// Read workspace-level memory (shared across all channels)
|
||||
const workspaceMemoryPath = join(channelDir, "..", "MEMORY.md");
|
||||
if (existsSync(workspaceMemoryPath)) {
|
||||
try {
|
||||
const content = readFileSync(workspaceMemoryPath, "utf-8").trim();
|
||||
if (content) {
|
||||
parts.push(`### Global Workspace Memory\n${content}`);
|
||||
}
|
||||
} catch (error) {
|
||||
log.logWarning("Failed to read workspace memory", `${workspaceMemoryPath}: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Read channel-specific memory
|
||||
const channelMemoryPath = join(channelDir, "MEMORY.md");
|
||||
if (existsSync(channelMemoryPath)) {
|
||||
try {
|
||||
const content = readFileSync(channelMemoryPath, "utf-8").trim();
|
||||
if (content) {
|
||||
parts.push(`### Channel-Specific Memory\n${content}`);
|
||||
}
|
||||
} catch (error) {
|
||||
log.logWarning("Failed to read channel memory", `${channelMemoryPath}: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (parts.length === 0) {
|
||||
return "(no working memory yet)";
|
||||
}
|
||||
|
||||
return parts.join("\n\n");
|
||||
}
|
||||
|
||||
function loadMomSkills(channelDir: string, workspacePath: string): Skill[] {
|
||||
const skillMap = new Map<string, Skill>();
|
||||
|
||||
// channelDir is the host path (e.g., /Users/.../data/C0A34FL8PMH)
|
||||
// hostWorkspacePath is the parent directory on host
|
||||
// workspacePath is the container path (e.g., /workspace)
|
||||
const hostWorkspacePath = join(channelDir, "..");
|
||||
|
||||
// Helper to translate host paths to container paths
|
||||
const translatePath = (hostPath: string): string => {
|
||||
if (hostPath.startsWith(hostWorkspacePath)) {
|
||||
return workspacePath + hostPath.slice(hostWorkspacePath.length);
|
||||
}
|
||||
return hostPath;
|
||||
};
|
||||
|
||||
// Load workspace-level skills (global)
|
||||
const workspaceSkillsDir = join(hostWorkspacePath, "skills");
|
||||
for (const skill of loadSkillsFromDir({ dir: workspaceSkillsDir, source: "workspace" }).skills) {
|
||||
// Translate paths to container paths for system prompt
|
||||
skill.filePath = translatePath(skill.filePath);
|
||||
skill.baseDir = translatePath(skill.baseDir);
|
||||
skillMap.set(skill.name, skill);
|
||||
}
|
||||
|
||||
// Load channel-specific skills (override workspace skills on collision)
|
||||
const channelSkillsDir = join(channelDir, "skills");
|
||||
for (const skill of loadSkillsFromDir({ dir: channelSkillsDir, source: "channel" }).skills) {
|
||||
skill.filePath = translatePath(skill.filePath);
|
||||
skill.baseDir = translatePath(skill.baseDir);
|
||||
skillMap.set(skill.name, skill);
|
||||
}
|
||||
|
||||
return Array.from(skillMap.values());
|
||||
}
|
||||
|
||||
function buildSystemPrompt(
|
||||
workspacePath: string,
|
||||
channelId: string,
|
||||
memory: string,
|
||||
sandboxConfig: SandboxConfig,
|
||||
channels: ChannelInfo[],
|
||||
users: UserInfo[],
|
||||
skills: Skill[],
|
||||
): string {
|
||||
const channelPath = `${workspacePath}/${channelId}`;
|
||||
const isDocker = sandboxConfig.type === "docker";
|
||||
|
||||
// Format channel mappings
|
||||
const channelMappings =
|
||||
channels.length > 0 ? channels.map((c) => `${c.id}\t#${c.name}`).join("\n") : "(no channels loaded)";
|
||||
|
||||
// Format user mappings
|
||||
const userMappings =
|
||||
users.length > 0 ? users.map((u) => `${u.id}\t@${u.userName}\t${u.displayName}`).join("\n") : "(no users loaded)";
|
||||
|
||||
const envDescription = isDocker
|
||||
? `You are running inside a Docker container (Alpine Linux).
|
||||
- Bash working directory: / (use cd or absolute paths)
|
||||
- Install tools with: apk add <package>
|
||||
- Your changes persist across sessions`
|
||||
: `You are running directly on the host machine.
|
||||
- Bash working directory: ${process.cwd()}
|
||||
- Be careful with system modifications`;
|
||||
|
||||
return `You are mom, a Slack bot assistant. Be concise. No emojis.
|
||||
|
||||
## Context
|
||||
- For current date/time, use: date
|
||||
- You have access to previous conversation context including tool results from prior turns.
|
||||
- For older history beyond your context, search log.jsonl (contains user messages and your final responses, but not tool results).
|
||||
|
||||
## Slack Formatting (mrkdwn, NOT Markdown)
|
||||
Bold: *text*, Italic: _text_, Code: \`code\`, Block: \`\`\`code\`\`\`, Links: <url|text>
|
||||
Do NOT use **double asterisks** or [markdown](links).
|
||||
|
||||
## Slack IDs
|
||||
Channels: ${channelMappings}
|
||||
|
||||
Users: ${userMappings}
|
||||
|
||||
When mentioning users, use <@username> format (e.g., <@mario>).
|
||||
|
||||
## Environment
|
||||
${envDescription}
|
||||
|
||||
## Workspace Layout
|
||||
${workspacePath}/
|
||||
├── MEMORY.md # Global memory (all channels)
|
||||
├── skills/ # Global CLI tools you create
|
||||
└── ${channelId}/ # This channel
|
||||
├── MEMORY.md # Channel-specific memory
|
||||
├── log.jsonl # Message history (no tool results)
|
||||
├── attachments/ # User-shared files
|
||||
├── scratch/ # Your working directory
|
||||
└── skills/ # Channel-specific tools
|
||||
|
||||
## Skills (Custom CLI Tools)
|
||||
You can create reusable CLI tools for recurring tasks (email, APIs, data processing, etc.).
|
||||
|
||||
### Creating Skills
|
||||
Store in \`${workspacePath}/skills/<name>/\` (global) or \`${channelPath}/skills/<name>/\` (channel-specific).
|
||||
Each skill directory needs a \`SKILL.md\` with YAML frontmatter:
|
||||
|
||||
\`\`\`markdown
|
||||
---
|
||||
name: skill-name
|
||||
description: Short description of what this skill does
|
||||
---
|
||||
|
||||
# Skill Name
|
||||
|
||||
Usage instructions, examples, etc.
|
||||
Scripts are in: {baseDir}/
|
||||
\`\`\`
|
||||
|
||||
\`name\` and \`description\` are required. Use \`{baseDir}\` as placeholder for the skill's directory path.
|
||||
|
||||
### Available Skills
|
||||
${skills.length > 0 ? formatSkillsForPrompt(skills) : "(no skills installed yet)"}
|
||||
|
||||
## Events
|
||||
You can schedule events that wake you up at specific times or when external things happen. Events are JSON files in \`${workspacePath}/events/\`.
|
||||
|
||||
### Event Types
|
||||
|
||||
**Immediate** - Triggers as soon as harness sees the file. Use in scripts/webhooks to signal external events.
|
||||
\`\`\`json
|
||||
{"type": "immediate", "channelId": "${channelId}", "text": "New GitHub issue opened"}
|
||||
\`\`\`
|
||||
|
||||
**One-shot** - Triggers once at a specific time. Use for reminders.
|
||||
\`\`\`json
|
||||
{"type": "one-shot", "channelId": "${channelId}", "text": "Remind Mario about dentist", "at": "2025-12-15T09:00:00+01:00"}
|
||||
\`\`\`
|
||||
|
||||
**Periodic** - Triggers on a cron schedule. Use for recurring tasks.
|
||||
\`\`\`json
|
||||
{"type": "periodic", "channelId": "${channelId}", "text": "Check inbox and summarize", "schedule": "0 9 * * 1-5", "timezone": "${Intl.DateTimeFormat().resolvedOptions().timeZone}"}
|
||||
\`\`\`
|
||||
|
||||
### Cron Format
|
||||
\`minute hour day-of-month month day-of-week\`
|
||||
- \`0 9 * * *\` = daily at 9:00
|
||||
- \`0 9 * * 1-5\` = weekdays at 9:00
|
||||
- \`30 14 * * 1\` = Mondays at 14:30
|
||||
- \`0 0 1 * *\` = first of each month at midnight
|
||||
|
||||
### Timezones
|
||||
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}.
|
||||
|
||||
### Creating Events
|
||||
Use unique filenames to avoid overwriting existing events. Include a timestamp or random suffix:
|
||||
\`\`\`bash
|
||||
cat > ${workspacePath}/events/dentist-reminder-$(date +%s).json << 'EOF'
|
||||
{"type": "one-shot", "channelId": "${channelId}", "text": "Dentist tomorrow", "at": "2025-12-14T09:00:00+01:00"}
|
||||
EOF
|
||||
\`\`\`
|
||||
Or check if file exists first before creating.
|
||||
|
||||
### Managing Events
|
||||
- List: \`ls ${workspacePath}/events/\`
|
||||
- View: \`cat ${workspacePath}/events/foo.json\`
|
||||
- Delete/cancel: \`rm ${workspacePath}/events/foo.json\`
|
||||
|
||||
### When Events Trigger
|
||||
You receive a message like:
|
||||
\`\`\`
|
||||
[EVENT:dentist-reminder.json:one-shot:2025-12-14T09:00:00+01:00] Dentist tomorrow
|
||||
\`\`\`
|
||||
Immediate and one-shot events auto-delete after triggering. Periodic events persist until you delete them.
|
||||
|
||||
### Silent Completion
|
||||
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.
|
||||
|
||||
### Debouncing
|
||||
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.
|
||||
|
||||
### Limits
|
||||
Maximum 5 events can be queued. Don't create excessive immediate or periodic events.
|
||||
|
||||
## Memory
|
||||
Write to MEMORY.md files to persist context across conversations.
|
||||
- Global (${workspacePath}/MEMORY.md): skills, preferences, project info
|
||||
- Channel (${channelPath}/MEMORY.md): channel-specific decisions, ongoing work
|
||||
Update when you learn something important or when asked to remember something.
|
||||
|
||||
### Current Memory
|
||||
${memory}
|
||||
|
||||
## System Configuration Log
|
||||
Maintain ${workspacePath}/SYSTEM.md to log all environment modifications:
|
||||
- Installed packages (apk add, npm install, pip install)
|
||||
- Environment variables set
|
||||
- Config files modified (~/.gitconfig, cron jobs, etc.)
|
||||
- Skill dependencies installed
|
||||
|
||||
Update this file whenever you modify the environment. On fresh container, read it first to restore your setup.
|
||||
|
||||
## Log Queries (for older history)
|
||||
Format: \`{"date":"...","ts":"...","user":"...","userName":"...","text":"...","isBot":false}\`
|
||||
The log contains user messages and your final responses (not tool calls/results).
|
||||
${isDocker ? "Install jq: apk add jq" : ""}
|
||||
|
||||
\`\`\`bash
|
||||
# Recent messages
|
||||
tail -30 log.jsonl | jq -c '{date: .date[0:19], user: (.userName // .user), text}'
|
||||
|
||||
# Search for specific topic
|
||||
grep -i "topic" log.jsonl | jq -c '{date: .date[0:19], user: (.userName // .user), text}'
|
||||
|
||||
# Messages from specific user
|
||||
grep '"userName":"mario"' log.jsonl | tail -20 | jq -c '{date: .date[0:19], text}'
|
||||
\`\`\`
|
||||
|
||||
## Tools
|
||||
- bash: Run shell commands (primary tool). Install packages as needed.
|
||||
- read: Read files
|
||||
- write: Create/overwrite files
|
||||
- edit: Surgical file edits
|
||||
- attach: Share files to Slack
|
||||
|
||||
Each tool requires a "label" parameter (shown to user).
|
||||
`;
|
||||
}
|
||||
|
||||
function truncate(text: string, maxLen: number): string {
|
||||
if (text.length <= maxLen) return text;
|
||||
return `${text.substring(0, maxLen - 3)}...`;
|
||||
}
|
||||
|
||||
function extractToolResultText(result: unknown): string {
|
||||
if (typeof result === "string") {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (
|
||||
result &&
|
||||
typeof result === "object" &&
|
||||
"content" in result &&
|
||||
Array.isArray((result as { content: unknown }).content)
|
||||
) {
|
||||
const content = (result as { content: Array<{ type: string; text?: string }> }).content;
|
||||
const textParts: string[] = [];
|
||||
for (const part of content) {
|
||||
if (part.type === "text" && part.text) {
|
||||
textParts.push(part.text);
|
||||
}
|
||||
}
|
||||
if (textParts.length > 0) {
|
||||
return textParts.join("\n");
|
||||
}
|
||||
}
|
||||
|
||||
return JSON.stringify(result);
|
||||
}
|
||||
|
||||
function formatToolArgsForSlack(_toolName: string, args: Record<string, unknown>): string {
|
||||
const lines: string[] = [];
|
||||
|
||||
for (const [key, value] of Object.entries(args)) {
|
||||
if (key === "label") continue;
|
||||
|
||||
if (key === "path" && typeof value === "string") {
|
||||
const offset = args.offset as number | undefined;
|
||||
const limit = args.limit as number | undefined;
|
||||
if (offset !== undefined && limit !== undefined) {
|
||||
lines.push(`${value}:${offset}-${offset + limit}`);
|
||||
} else {
|
||||
lines.push(value);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (key === "offset" || key === "limit") continue;
|
||||
|
||||
if (typeof value === "string") {
|
||||
lines.push(value);
|
||||
} else {
|
||||
lines.push(JSON.stringify(value));
|
||||
}
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
// Cache runners per channel
|
||||
const channelRunners = new Map<string, AgentRunner>();
|
||||
|
||||
/**
|
||||
* Get or create an AgentRunner for a channel.
|
||||
* Runners are cached - one per channel, persistent across messages.
|
||||
*/
|
||||
export function getOrCreateRunner(sandboxConfig: SandboxConfig, channelId: string, channelDir: string): AgentRunner {
|
||||
const existing = channelRunners.get(channelId);
|
||||
if (existing) return existing;
|
||||
|
||||
const runner = createRunner(sandboxConfig, channelId, channelDir);
|
||||
channelRunners.set(channelId, runner);
|
||||
return runner;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new AgentRunner for a channel.
|
||||
* Sets up the session and subscribes to events once.
|
||||
*/
|
||||
function createRunner(sandboxConfig: SandboxConfig, channelId: string, channelDir: string): AgentRunner {
|
||||
const executor = createExecutor(sandboxConfig);
|
||||
const workspacePath = executor.getWorkspacePath(channelDir.replace(`/${channelId}`, ""));
|
||||
|
||||
// Create tools
|
||||
const tools = createMomTools(executor);
|
||||
|
||||
// Initial system prompt (will be updated each run with fresh memory/channels/users/skills)
|
||||
const memory = getMemory(channelDir);
|
||||
const skills = loadMomSkills(channelDir, workspacePath);
|
||||
const systemPrompt = buildSystemPrompt(workspacePath, channelId, memory, sandboxConfig, [], [], skills);
|
||||
|
||||
// Create session manager and settings manager
|
||||
// Use a fixed context.jsonl file per channel (not timestamped like coding-agent)
|
||||
const contextFile = join(channelDir, "context.jsonl");
|
||||
const sessionManager = SessionManager.open(contextFile, channelDir);
|
||||
const settingsManager = createMomSettingsManager(join(channelDir, ".."));
|
||||
|
||||
// Create AuthStorage and ModelRegistry
|
||||
// Auth stored outside workspace so agent can't access it
|
||||
const authStorage = AuthStorage.create(join(homedir(), ".pi", "mom", "auth.json"));
|
||||
const modelRegistry = ModelRegistry.create(authStorage);
|
||||
|
||||
// Create agent
|
||||
const agent = new Agent({
|
||||
initialState: {
|
||||
systemPrompt,
|
||||
model,
|
||||
thinkingLevel: "off",
|
||||
tools,
|
||||
},
|
||||
convertToLlm,
|
||||
getApiKey: async () => getAnthropicApiKey(authStorage),
|
||||
});
|
||||
|
||||
// Load existing messages
|
||||
const loadedSession = sessionManager.buildSessionContext();
|
||||
if (loadedSession.messages.length > 0) {
|
||||
agent.state.messages = loadedSession.messages;
|
||||
log.logInfo(`[${channelId}] Loaded ${loadedSession.messages.length} messages from context.jsonl`);
|
||||
}
|
||||
|
||||
const resourceLoader: ResourceLoader = {
|
||||
getExtensions: () => ({ extensions: [], errors: [], runtime: createExtensionRuntime() }),
|
||||
getSkills: () => ({ skills: [], diagnostics: [] }),
|
||||
getPrompts: () => ({ prompts: [], diagnostics: [] }),
|
||||
getThemes: () => ({ themes: [], diagnostics: [] }),
|
||||
getAgentsFiles: () => ({ agentsFiles: [] }),
|
||||
getSystemPrompt: () => systemPrompt,
|
||||
getAppendSystemPrompt: () => [],
|
||||
extendResources: () => {},
|
||||
reload: async () => {},
|
||||
};
|
||||
|
||||
const baseToolsOverride = Object.fromEntries(tools.map((tool) => [tool.name, tool]));
|
||||
|
||||
// Create AgentSession wrapper
|
||||
const session = new AgentSession({
|
||||
agent,
|
||||
sessionManager,
|
||||
settingsManager,
|
||||
cwd: process.cwd(),
|
||||
modelRegistry,
|
||||
resourceLoader,
|
||||
baseToolsOverride,
|
||||
});
|
||||
|
||||
// Mutable per-run state - event handler references this
|
||||
const runState = {
|
||||
ctx: null as SlackContext | null,
|
||||
logCtx: null as { channelId: string; userName?: string; channelName?: string } | null,
|
||||
queue: null as {
|
||||
enqueue(fn: () => Promise<void>, errorContext: string): void;
|
||||
enqueueMessage(text: string, target: "main" | "thread", errorContext: string, doLog?: boolean): void;
|
||||
} | null,
|
||||
pendingTools: new Map<string, { toolName: string; args: unknown; startTime: number }>(),
|
||||
totalUsage: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
||||
},
|
||||
stopReason: "stop",
|
||||
errorMessage: undefined as string | undefined,
|
||||
};
|
||||
|
||||
// Subscribe to events ONCE
|
||||
session.subscribe(async (event) => {
|
||||
// Skip if no active run
|
||||
if (!runState.ctx || !runState.logCtx || !runState.queue) return;
|
||||
|
||||
const { ctx, logCtx, queue, pendingTools } = runState;
|
||||
|
||||
if (event.type === "tool_execution_start") {
|
||||
const agentEvent = event as AgentEvent & { type: "tool_execution_start" };
|
||||
const args = agentEvent.args as { label?: string };
|
||||
const label = args.label || agentEvent.toolName;
|
||||
|
||||
pendingTools.set(agentEvent.toolCallId, {
|
||||
toolName: agentEvent.toolName,
|
||||
args: agentEvent.args,
|
||||
startTime: Date.now(),
|
||||
});
|
||||
|
||||
log.logToolStart(logCtx, agentEvent.toolName, label, agentEvent.args as Record<string, unknown>);
|
||||
queue.enqueue(() => ctx.respond(`_→ ${label}_`, false), "tool label");
|
||||
} else if (event.type === "tool_execution_end") {
|
||||
const agentEvent = event as AgentEvent & { type: "tool_execution_end" };
|
||||
const resultStr = extractToolResultText(agentEvent.result);
|
||||
const pending = pendingTools.get(agentEvent.toolCallId);
|
||||
pendingTools.delete(agentEvent.toolCallId);
|
||||
|
||||
const durationMs = pending ? Date.now() - pending.startTime : 0;
|
||||
|
||||
if (agentEvent.isError) {
|
||||
log.logToolError(logCtx, agentEvent.toolName, durationMs, resultStr);
|
||||
} else {
|
||||
log.logToolSuccess(logCtx, agentEvent.toolName, durationMs, resultStr);
|
||||
}
|
||||
|
||||
// Post args + result to thread
|
||||
const label = pending?.args ? (pending.args as { label?: string }).label : undefined;
|
||||
const argsFormatted = pending
|
||||
? formatToolArgsForSlack(agentEvent.toolName, pending.args as Record<string, unknown>)
|
||||
: "(args not found)";
|
||||
const duration = (durationMs / 1000).toFixed(1);
|
||||
let threadMessage = `*${agentEvent.isError ? "✗" : "✓"} ${agentEvent.toolName}*`;
|
||||
if (label) threadMessage += `: ${label}`;
|
||||
threadMessage += ` (${duration}s)\n`;
|
||||
if (argsFormatted) threadMessage += `\`\`\`\n${argsFormatted}\n\`\`\`\n`;
|
||||
threadMessage += `*Result:*\n\`\`\`\n${resultStr}\n\`\`\``;
|
||||
|
||||
queue.enqueueMessage(threadMessage, "thread", "tool result thread", false);
|
||||
|
||||
if (agentEvent.isError) {
|
||||
queue.enqueue(() => ctx.respond(`_Error: ${truncate(resultStr, 200)}_`, false), "tool error");
|
||||
}
|
||||
} else if (event.type === "message_start") {
|
||||
const agentEvent = event as AgentEvent & { type: "message_start" };
|
||||
if (agentEvent.message.role === "assistant") {
|
||||
log.logResponseStart(logCtx);
|
||||
}
|
||||
} else if (event.type === "message_end") {
|
||||
const agentEvent = event as AgentEvent & { type: "message_end" };
|
||||
if (agentEvent.message.role === "assistant") {
|
||||
const assistantMsg = agentEvent.message as any;
|
||||
|
||||
if (assistantMsg.stopReason) {
|
||||
runState.stopReason = assistantMsg.stopReason;
|
||||
}
|
||||
if (assistantMsg.errorMessage) {
|
||||
runState.errorMessage = assistantMsg.errorMessage;
|
||||
}
|
||||
|
||||
if (assistantMsg.usage) {
|
||||
runState.totalUsage.input += assistantMsg.usage.input;
|
||||
runState.totalUsage.output += assistantMsg.usage.output;
|
||||
runState.totalUsage.cacheRead += assistantMsg.usage.cacheRead;
|
||||
runState.totalUsage.cacheWrite += assistantMsg.usage.cacheWrite;
|
||||
runState.totalUsage.cost.input += assistantMsg.usage.cost.input;
|
||||
runState.totalUsage.cost.output += assistantMsg.usage.cost.output;
|
||||
runState.totalUsage.cost.cacheRead += assistantMsg.usage.cost.cacheRead;
|
||||
runState.totalUsage.cost.cacheWrite += assistantMsg.usage.cost.cacheWrite;
|
||||
runState.totalUsage.cost.total += assistantMsg.usage.cost.total;
|
||||
}
|
||||
|
||||
const content = agentEvent.message.content;
|
||||
const thinkingParts: string[] = [];
|
||||
const textParts: string[] = [];
|
||||
for (const part of content) {
|
||||
if (part.type === "thinking") {
|
||||
thinkingParts.push((part as any).thinking);
|
||||
} else if (part.type === "text") {
|
||||
textParts.push((part as any).text);
|
||||
}
|
||||
}
|
||||
|
||||
const text = textParts.join("\n");
|
||||
|
||||
for (const thinking of thinkingParts) {
|
||||
log.logThinking(logCtx, thinking);
|
||||
queue.enqueueMessage(`_${thinking}_`, "main", "thinking main");
|
||||
queue.enqueueMessage(`_${thinking}_`, "thread", "thinking thread", false);
|
||||
}
|
||||
|
||||
if (text.trim()) {
|
||||
log.logResponse(logCtx, text);
|
||||
queue.enqueueMessage(text, "main", "response main");
|
||||
queue.enqueueMessage(text, "thread", "response thread", false);
|
||||
}
|
||||
}
|
||||
} else if (event.type === "compaction_start") {
|
||||
log.logInfo(`Compaction started (reason: ${event.reason})`);
|
||||
queue.enqueue(() => ctx.respond("_Compacting context..._", false), "compaction start");
|
||||
} else if (event.type === "compaction_end") {
|
||||
if (event.result) {
|
||||
log.logInfo(`Compaction complete: ${event.result.tokensBefore} tokens compacted`);
|
||||
} else if (event.aborted) {
|
||||
log.logInfo("Compaction aborted");
|
||||
}
|
||||
} else if (event.type === "auto_retry_start") {
|
||||
const retryEvent = event as any;
|
||||
log.logWarning(`Retrying (${retryEvent.attempt}/${retryEvent.maxAttempts})`, retryEvent.errorMessage);
|
||||
queue.enqueue(
|
||||
() => ctx.respond(`_Retrying (${retryEvent.attempt}/${retryEvent.maxAttempts})..._`, false),
|
||||
"retry",
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// Slack message limit
|
||||
const SLACK_MAX_LENGTH = 40000;
|
||||
const splitForSlack = (text: string): string[] => {
|
||||
if (text.length <= SLACK_MAX_LENGTH) return [text];
|
||||
const parts: string[] = [];
|
||||
let remaining = text;
|
||||
let partNum = 1;
|
||||
while (remaining.length > 0) {
|
||||
const chunk = remaining.substring(0, SLACK_MAX_LENGTH - 50);
|
||||
remaining = remaining.substring(SLACK_MAX_LENGTH - 50);
|
||||
const suffix = remaining.length > 0 ? `\n_(continued ${partNum}...)_` : "";
|
||||
parts.push(chunk + suffix);
|
||||
partNum++;
|
||||
}
|
||||
return parts;
|
||||
};
|
||||
|
||||
return {
|
||||
async run(
|
||||
ctx: SlackContext,
|
||||
_store: ChannelStore,
|
||||
_pendingMessages?: PendingMessage[],
|
||||
): Promise<{ stopReason: string; errorMessage?: string }> {
|
||||
// Ensure channel directory exists
|
||||
await mkdir(channelDir, { recursive: true });
|
||||
|
||||
// Sync messages from log.jsonl that arrived while we were offline or busy
|
||||
// Exclude the current message (it will be added via prompt())
|
||||
const syncedCount = syncLogToSessionManager(sessionManager, channelDir, ctx.message.ts);
|
||||
if (syncedCount > 0) {
|
||||
log.logInfo(`[${channelId}] Synced ${syncedCount} messages from log.jsonl`);
|
||||
}
|
||||
|
||||
// Reload messages from context.jsonl
|
||||
// This picks up any messages synced above
|
||||
const reloadedSession = sessionManager.buildSessionContext();
|
||||
if (reloadedSession.messages.length > 0) {
|
||||
agent.state.messages = reloadedSession.messages;
|
||||
log.logInfo(`[${channelId}] Reloaded ${reloadedSession.messages.length} messages from context`);
|
||||
}
|
||||
|
||||
// Update system prompt with fresh memory, channel/user info, and skills
|
||||
const memory = getMemory(channelDir);
|
||||
const skills = loadMomSkills(channelDir, workspacePath);
|
||||
const systemPrompt = buildSystemPrompt(
|
||||
workspacePath,
|
||||
channelId,
|
||||
memory,
|
||||
sandboxConfig,
|
||||
ctx.channels,
|
||||
ctx.users,
|
||||
skills,
|
||||
);
|
||||
session.agent.state.systemPrompt = systemPrompt;
|
||||
|
||||
// Set up file upload function
|
||||
setUploadFunction(async (filePath: string, title?: string) => {
|
||||
const hostPath = translateToHostPath(filePath, channelDir, workspacePath, channelId);
|
||||
await ctx.uploadFile(hostPath, title);
|
||||
});
|
||||
|
||||
// Reset per-run state
|
||||
runState.ctx = ctx;
|
||||
runState.logCtx = {
|
||||
channelId: ctx.message.channel,
|
||||
userName: ctx.message.userName,
|
||||
channelName: ctx.channelName,
|
||||
};
|
||||
runState.pendingTools.clear();
|
||||
runState.totalUsage = {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
||||
};
|
||||
runState.stopReason = "stop";
|
||||
runState.errorMessage = undefined;
|
||||
|
||||
// Create queue for this run
|
||||
let queueChain = Promise.resolve();
|
||||
runState.queue = {
|
||||
enqueue(fn: () => Promise<void>, errorContext: string): void {
|
||||
queueChain = queueChain.then(async () => {
|
||||
try {
|
||||
await fn();
|
||||
} catch (err) {
|
||||
const errMsg = err instanceof Error ? err.message : String(err);
|
||||
log.logWarning(`Slack API error (${errorContext})`, errMsg);
|
||||
try {
|
||||
await ctx.respondInThread(`_Error: ${errMsg}_`);
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
enqueueMessage(text: string, target: "main" | "thread", errorContext: string, doLog = true): void {
|
||||
const parts = splitForSlack(text);
|
||||
for (const part of parts) {
|
||||
this.enqueue(
|
||||
() => (target === "main" ? ctx.respond(part, doLog) : ctx.respondInThread(part)),
|
||||
errorContext,
|
||||
);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// Log context info
|
||||
log.logInfo(`Context sizes - system: ${systemPrompt.length} chars, memory: ${memory.length} chars`);
|
||||
log.logInfo(`Channels: ${ctx.channels.length}, Users: ${ctx.users.length}`);
|
||||
|
||||
// Build user message with timestamp and username prefix
|
||||
// Format: "[YYYY-MM-DD HH:MM:SS+HH:MM] [username]: message" so LLM knows when and who
|
||||
const now = new Date();
|
||||
const pad = (n: number) => n.toString().padStart(2, "0");
|
||||
const offset = -now.getTimezoneOffset();
|
||||
const offsetSign = offset >= 0 ? "+" : "-";
|
||||
const offsetHours = pad(Math.floor(Math.abs(offset) / 60));
|
||||
const offsetMins = pad(Math.abs(offset) % 60);
|
||||
const timestamp = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}${offsetSign}${offsetHours}:${offsetMins}`;
|
||||
let userMessage = `[${timestamp}] [${ctx.message.userName || "unknown"}]: ${ctx.message.text}`;
|
||||
|
||||
const imageAttachments: ImageContent[] = [];
|
||||
const nonImagePaths: string[] = [];
|
||||
|
||||
for (const a of ctx.message.attachments || []) {
|
||||
const fullPath = `${workspacePath}/${a.local}`;
|
||||
const mimeType = getImageMimeType(a.local);
|
||||
|
||||
if (mimeType && existsSync(fullPath)) {
|
||||
try {
|
||||
imageAttachments.push({
|
||||
type: "image",
|
||||
mimeType,
|
||||
data: readFileSync(fullPath).toString("base64"),
|
||||
});
|
||||
} catch {
|
||||
nonImagePaths.push(fullPath);
|
||||
}
|
||||
} else {
|
||||
nonImagePaths.push(fullPath);
|
||||
}
|
||||
}
|
||||
|
||||
if (nonImagePaths.length > 0) {
|
||||
userMessage += `\n\n<slack_attachments>\n${nonImagePaths.join("\n")}\n</slack_attachments>`;
|
||||
}
|
||||
|
||||
// Debug: write context to last_prompt.jsonl
|
||||
const debugContext = {
|
||||
systemPrompt,
|
||||
messages: session.messages,
|
||||
newUserMessage: userMessage,
|
||||
imageAttachmentCount: imageAttachments.length,
|
||||
};
|
||||
await writeFile(join(channelDir, "last_prompt.jsonl"), JSON.stringify(debugContext, null, 2));
|
||||
|
||||
await session.prompt(userMessage, imageAttachments.length > 0 ? { images: imageAttachments } : undefined);
|
||||
|
||||
// Wait for queued messages
|
||||
await queueChain;
|
||||
|
||||
// Handle error case - update main message and post error to thread
|
||||
if (runState.stopReason === "error" && runState.errorMessage) {
|
||||
try {
|
||||
await ctx.replaceMessage("_Sorry, something went wrong_");
|
||||
await ctx.respondInThread(`_Error: ${runState.errorMessage}_`);
|
||||
} catch (err) {
|
||||
const errMsg = err instanceof Error ? err.message : String(err);
|
||||
log.logWarning("Failed to post error message", errMsg);
|
||||
}
|
||||
} else {
|
||||
// Final message update
|
||||
const messages = session.messages;
|
||||
const lastAssistant = messages.filter((m) => m.role === "assistant").pop();
|
||||
const finalText =
|
||||
lastAssistant?.content
|
||||
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
||||
.map((c) => c.text)
|
||||
.join("\n") || "";
|
||||
|
||||
// Check for [SILENT] marker - delete message and thread instead of posting
|
||||
if (finalText.trim() === "[SILENT]" || finalText.trim().startsWith("[SILENT]")) {
|
||||
try {
|
||||
await ctx.deleteMessage();
|
||||
log.logInfo("Silent response - deleted message and thread");
|
||||
} catch (err) {
|
||||
const errMsg = err instanceof Error ? err.message : String(err);
|
||||
log.logWarning("Failed to delete message for silent response", errMsg);
|
||||
}
|
||||
} else if (finalText.trim()) {
|
||||
try {
|
||||
const mainText =
|
||||
finalText.length > SLACK_MAX_LENGTH
|
||||
? `${finalText.substring(0, SLACK_MAX_LENGTH - 50)}\n\n_(see thread for full response)_`
|
||||
: finalText;
|
||||
await ctx.replaceMessage(mainText);
|
||||
} catch (err) {
|
||||
const errMsg = err instanceof Error ? err.message : String(err);
|
||||
log.logWarning("Failed to replace message with final text", errMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Log usage summary with context info
|
||||
if (runState.totalUsage.cost.total > 0) {
|
||||
// Get last non-aborted assistant message for context calculation
|
||||
const messages = session.messages;
|
||||
const lastAssistantMessage = messages
|
||||
.slice()
|
||||
.reverse()
|
||||
.find((m) => m.role === "assistant" && (m as any).stopReason !== "aborted") as any;
|
||||
|
||||
const contextTokens = lastAssistantMessage
|
||||
? lastAssistantMessage.usage.input +
|
||||
lastAssistantMessage.usage.output +
|
||||
lastAssistantMessage.usage.cacheRead +
|
||||
lastAssistantMessage.usage.cacheWrite
|
||||
: 0;
|
||||
const contextWindow = model.contextWindow || 200000;
|
||||
|
||||
const summary = log.logUsageSummary(runState.logCtx!, runState.totalUsage, contextTokens, contextWindow);
|
||||
runState.queue.enqueue(() => ctx.respondInThread(summary), "usage summary");
|
||||
await queueChain;
|
||||
}
|
||||
|
||||
// Clear run state
|
||||
runState.ctx = null;
|
||||
runState.logCtx = null;
|
||||
runState.queue = null;
|
||||
|
||||
return { stopReason: runState.stopReason, errorMessage: runState.errorMessage };
|
||||
},
|
||||
|
||||
abort(): void {
|
||||
session.abort();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate container path back to host path for file operations
|
||||
*/
|
||||
function translateToHostPath(
|
||||
containerPath: string,
|
||||
channelDir: string,
|
||||
workspacePath: string,
|
||||
channelId: string,
|
||||
): string {
|
||||
if (workspacePath === "/workspace") {
|
||||
const prefix = `/workspace/${channelId}/`;
|
||||
if (containerPath.startsWith(prefix)) {
|
||||
return join(channelDir, containerPath.slice(prefix.length));
|
||||
}
|
||||
if (containerPath.startsWith("/workspace/")) {
|
||||
return join(channelDir, "..", containerPath.slice("/workspace/".length));
|
||||
}
|
||||
}
|
||||
return containerPath;
|
||||
}
|
||||
@@ -1,180 +0,0 @@
|
||||
/**
|
||||
* Context management for mom.
|
||||
*
|
||||
* Mom uses two files per channel:
|
||||
* - context.jsonl: Structured API messages for LLM context (same format as coding-agent sessions)
|
||||
* - log.jsonl: Human-readable channel history for grep (no tool results)
|
||||
*
|
||||
* This module provides:
|
||||
* - syncLogToSessionManager: Syncs messages from log.jsonl to SessionManager
|
||||
* - createMomSettingsManager: Creates a SettingsManager backed by workspace settings.json
|
||||
*/
|
||||
|
||||
import type { UserMessage } from "@mariozechner/pi-ai";
|
||||
import { type SessionManager, type SessionMessageEntry, SettingsManager } from "@mariozechner/pi-coding-agent";
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
||||
import { dirname, join } from "path";
|
||||
|
||||
// ============================================================================
|
||||
// Sync log.jsonl to SessionManager
|
||||
// ============================================================================
|
||||
|
||||
interface LogMessage {
|
||||
date?: string;
|
||||
ts?: string;
|
||||
user?: string;
|
||||
userName?: string;
|
||||
text?: string;
|
||||
isBot?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync user messages from log.jsonl to SessionManager.
|
||||
*
|
||||
* This ensures that messages logged while mom wasn't running (channel chatter,
|
||||
* backfilled messages, messages while busy) are added to the LLM context.
|
||||
*
|
||||
* @param sessionManager - The SessionManager to sync to
|
||||
* @param channelDir - Path to channel directory containing log.jsonl
|
||||
* @param excludeSlackTs - Slack timestamp of current message (will be added via prompt(), not sync)
|
||||
* @returns Number of messages synced
|
||||
*/
|
||||
export function syncLogToSessionManager(
|
||||
sessionManager: SessionManager,
|
||||
channelDir: string,
|
||||
excludeSlackTs?: string,
|
||||
): number {
|
||||
const logFile = join(channelDir, "log.jsonl");
|
||||
|
||||
if (!existsSync(logFile)) return 0;
|
||||
|
||||
// Build set of existing message content from session
|
||||
const existingMessages = new Set<string>();
|
||||
for (const entry of sessionManager.getEntries()) {
|
||||
if (entry.type === "message") {
|
||||
const msgEntry = entry as SessionMessageEntry;
|
||||
const msg = msgEntry.message as { role: string; content?: unknown };
|
||||
if (msg.role === "user" && msg.content !== undefined) {
|
||||
const content = msg.content;
|
||||
if (typeof content === "string") {
|
||||
// Strip timestamp prefix for comparison (live messages have it, synced don't)
|
||||
// Format: [YYYY-MM-DD HH:MM:SS+HH:MM] [username]: text
|
||||
let normalized = content.replace(/^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}[+-]\d{2}:\d{2}\] /, "");
|
||||
// Strip attachments section
|
||||
const attachmentsIdx = normalized.indexOf("\n\n<slack_attachments>\n");
|
||||
if (attachmentsIdx !== -1) {
|
||||
normalized = normalized.substring(0, attachmentsIdx);
|
||||
}
|
||||
existingMessages.add(normalized);
|
||||
} else if (Array.isArray(content)) {
|
||||
for (const part of content) {
|
||||
if (
|
||||
typeof part === "object" &&
|
||||
part !== null &&
|
||||
"type" in part &&
|
||||
part.type === "text" &&
|
||||
"text" in part
|
||||
) {
|
||||
let normalized = (part as { type: "text"; text: string }).text;
|
||||
normalized = normalized.replace(/^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}[+-]\d{2}:\d{2}\] /, "");
|
||||
const attachmentsIdx = normalized.indexOf("\n\n<slack_attachments>\n");
|
||||
if (attachmentsIdx !== -1) {
|
||||
normalized = normalized.substring(0, attachmentsIdx);
|
||||
}
|
||||
existingMessages.add(normalized);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Read log.jsonl and find user messages not in context
|
||||
const logContent = readFileSync(logFile, "utf-8");
|
||||
const logLines = logContent.trim().split("\n").filter(Boolean);
|
||||
|
||||
const newMessages: Array<{ timestamp: number; message: UserMessage }> = [];
|
||||
|
||||
for (const line of logLines) {
|
||||
try {
|
||||
const logMsg: LogMessage = JSON.parse(line);
|
||||
|
||||
const slackTs = logMsg.ts;
|
||||
const date = logMsg.date;
|
||||
if (!slackTs || !date) continue;
|
||||
|
||||
// Skip the current message being processed (will be added via prompt())
|
||||
if (excludeSlackTs && slackTs === excludeSlackTs) continue;
|
||||
|
||||
// Skip bot messages - added through agent flow
|
||||
if (logMsg.isBot) continue;
|
||||
|
||||
// Build the message text as it would appear in context
|
||||
const messageText = `[${logMsg.userName || logMsg.user || "unknown"}]: ${logMsg.text || ""}`;
|
||||
|
||||
// Skip if this exact message text is already in context
|
||||
if (existingMessages.has(messageText)) continue;
|
||||
|
||||
const msgTime = new Date(date).getTime() || Date.now();
|
||||
const userMessage: UserMessage = {
|
||||
role: "user",
|
||||
content: [{ type: "text", text: messageText }],
|
||||
timestamp: msgTime,
|
||||
};
|
||||
|
||||
newMessages.push({ timestamp: msgTime, message: userMessage });
|
||||
existingMessages.add(messageText); // Track to avoid duplicates within this sync
|
||||
} catch {
|
||||
// Skip malformed lines
|
||||
}
|
||||
}
|
||||
|
||||
if (newMessages.length === 0) return 0;
|
||||
|
||||
// Sort by timestamp and add to session
|
||||
newMessages.sort((a, b) => a.timestamp - b.timestamp);
|
||||
|
||||
for (const { message } of newMessages) {
|
||||
sessionManager.appendMessage(message);
|
||||
}
|
||||
|
||||
return newMessages.length;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Settings manager for mom
|
||||
// ============================================================================
|
||||
|
||||
type MomSettingsStorage = Parameters<typeof SettingsManager.fromStorage>[0];
|
||||
|
||||
class WorkspaceSettingsStorage implements MomSettingsStorage {
|
||||
private settingsPath: string;
|
||||
|
||||
constructor(workspaceDir: string) {
|
||||
this.settingsPath = join(workspaceDir, "settings.json");
|
||||
}
|
||||
|
||||
withLock(scope: "global" | "project", fn: (current: string | undefined) => string | undefined): void {
|
||||
if (scope === "project") {
|
||||
// Mom stores all settings in a single workspace file.
|
||||
fn(undefined);
|
||||
return;
|
||||
}
|
||||
|
||||
const current = existsSync(this.settingsPath) ? readFileSync(this.settingsPath, "utf-8") : undefined;
|
||||
const next = fn(current);
|
||||
if (next === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const dir = dirname(this.settingsPath);
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
writeFileSync(this.settingsPath, next, "utf-8");
|
||||
}
|
||||
}
|
||||
|
||||
export function createMomSettingsManager(workspaceDir: string): SettingsManager {
|
||||
return SettingsManager.fromStorage(new WorkspaceSettingsStorage(workspaceDir));
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
import { LogLevel, WebClient } from "@slack/web-api";
|
||||
|
||||
interface Message {
|
||||
ts: string;
|
||||
user?: string;
|
||||
text?: string;
|
||||
thread_ts?: string;
|
||||
reply_count?: number;
|
||||
files?: Array<{ name: string; url_private?: string }>;
|
||||
}
|
||||
|
||||
function formatTs(ts: string): string {
|
||||
const date = new Date(parseFloat(ts) * 1000);
|
||||
return date
|
||||
.toISOString()
|
||||
.replace("T", " ")
|
||||
.replace(/\.\d+Z$/, "");
|
||||
}
|
||||
|
||||
function formatMessage(ts: string, user: string, text: string, indent = ""): string {
|
||||
const prefix = `[${formatTs(ts)}] ${user}: `;
|
||||
const lines = text.split("\n");
|
||||
const firstLine = `${indent}${prefix}${lines[0]}`;
|
||||
if (lines.length === 1) return firstLine;
|
||||
// All continuation lines get same indent as content start
|
||||
const contentIndent = indent + " ".repeat(prefix.length);
|
||||
return [firstLine, ...lines.slice(1).map((l) => contentIndent + l)].join("\n");
|
||||
}
|
||||
|
||||
export async function downloadChannel(channelId: string, botToken: string): Promise<void> {
|
||||
const client = new WebClient(botToken, { logLevel: LogLevel.ERROR });
|
||||
|
||||
console.error(`Fetching channel info for ${channelId}...`);
|
||||
|
||||
// Get channel info
|
||||
let channelName = channelId;
|
||||
try {
|
||||
const info = await client.conversations.info({ channel: channelId });
|
||||
channelName = (info.channel as any)?.name || channelId;
|
||||
} catch {
|
||||
// DM channels don't have names, that's fine
|
||||
}
|
||||
|
||||
console.error(`Downloading history for #${channelName} (${channelId})...`);
|
||||
|
||||
// Fetch all messages
|
||||
const messages: Message[] = [];
|
||||
let cursor: string | undefined;
|
||||
|
||||
do {
|
||||
const response = await client.conversations.history({
|
||||
channel: channelId,
|
||||
limit: 200,
|
||||
cursor,
|
||||
});
|
||||
|
||||
if (response.messages) {
|
||||
messages.push(...(response.messages as Message[]));
|
||||
}
|
||||
|
||||
cursor = response.response_metadata?.next_cursor;
|
||||
console.error(` Fetched ${messages.length} messages...`);
|
||||
} while (cursor);
|
||||
|
||||
// Reverse to chronological order
|
||||
messages.reverse();
|
||||
|
||||
// Build map of thread replies
|
||||
const threadReplies = new Map<string, Message[]>();
|
||||
const threadsToFetch = messages.filter((m) => m.reply_count && m.reply_count > 0);
|
||||
|
||||
console.error(`Fetching ${threadsToFetch.length} threads...`);
|
||||
|
||||
for (let i = 0; i < threadsToFetch.length; i++) {
|
||||
const parent = threadsToFetch[i];
|
||||
console.error(` Thread ${i + 1}/${threadsToFetch.length} (${parent.reply_count} replies)...`);
|
||||
|
||||
const replies: Message[] = [];
|
||||
let threadCursor: string | undefined;
|
||||
|
||||
do {
|
||||
const response = await client.conversations.replies({
|
||||
channel: channelId,
|
||||
ts: parent.ts,
|
||||
limit: 200,
|
||||
cursor: threadCursor,
|
||||
});
|
||||
|
||||
if (response.messages) {
|
||||
// Skip the first message (it's the parent)
|
||||
replies.push(...(response.messages as Message[]).slice(1));
|
||||
}
|
||||
|
||||
threadCursor = response.response_metadata?.next_cursor;
|
||||
} while (threadCursor);
|
||||
|
||||
threadReplies.set(parent.ts, replies);
|
||||
}
|
||||
|
||||
// Output messages with thread replies interleaved
|
||||
let totalReplies = 0;
|
||||
for (const msg of messages) {
|
||||
// Output the message
|
||||
console.log(formatMessage(msg.ts, msg.user || "unknown", msg.text || ""));
|
||||
|
||||
// Output thread replies right after parent (indented)
|
||||
const replies = threadReplies.get(msg.ts);
|
||||
if (replies) {
|
||||
for (const reply of replies) {
|
||||
console.log(formatMessage(reply.ts, reply.user || "unknown", reply.text || "", " "));
|
||||
totalReplies++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.error(`Done! ${messages.length} messages, ${totalReplies} thread replies`);
|
||||
}
|
||||
@@ -1,383 +0,0 @@
|
||||
import { Cron } from "croner";
|
||||
import { existsSync, type FSWatcher, mkdirSync, readdirSync, statSync, unlinkSync, watch } from "fs";
|
||||
import { readFile } from "fs/promises";
|
||||
import { join } from "path";
|
||||
import * as log from "./log.js";
|
||||
import type { SlackBot, SlackEvent } from "./slack.js";
|
||||
|
||||
// ============================================================================
|
||||
// Event Types
|
||||
// ============================================================================
|
||||
|
||||
export interface ImmediateEvent {
|
||||
type: "immediate";
|
||||
channelId: string;
|
||||
text: string;
|
||||
}
|
||||
|
||||
export interface OneShotEvent {
|
||||
type: "one-shot";
|
||||
channelId: string;
|
||||
text: string;
|
||||
at: string; // ISO 8601 with timezone offset
|
||||
}
|
||||
|
||||
export interface PeriodicEvent {
|
||||
type: "periodic";
|
||||
channelId: string;
|
||||
text: string;
|
||||
schedule: string; // cron syntax
|
||||
timezone: string; // IANA timezone
|
||||
}
|
||||
|
||||
export type MomEvent = ImmediateEvent | OneShotEvent | PeriodicEvent;
|
||||
|
||||
// ============================================================================
|
||||
// EventsWatcher
|
||||
// ============================================================================
|
||||
|
||||
const DEBOUNCE_MS = 100;
|
||||
const MAX_RETRIES = 3;
|
||||
const RETRY_BASE_MS = 100;
|
||||
|
||||
export class EventsWatcher {
|
||||
private timers: Map<string, NodeJS.Timeout> = new Map();
|
||||
private crons: Map<string, Cron> = new Map();
|
||||
private debounceTimers: Map<string, NodeJS.Timeout> = new Map();
|
||||
private startTime: number;
|
||||
private watcher: FSWatcher | null = null;
|
||||
private knownFiles: Set<string> = new Set();
|
||||
|
||||
constructor(
|
||||
private eventsDir: string,
|
||||
private slack: SlackBot,
|
||||
) {
|
||||
this.startTime = Date.now();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start watching for events. Call this after SlackBot is ready.
|
||||
*/
|
||||
start(): void {
|
||||
// Ensure events directory exists
|
||||
if (!existsSync(this.eventsDir)) {
|
||||
mkdirSync(this.eventsDir, { recursive: true });
|
||||
}
|
||||
|
||||
log.logInfo(`Events watcher starting, dir: ${this.eventsDir}`);
|
||||
|
||||
// Scan existing files
|
||||
this.scanExisting();
|
||||
|
||||
// Watch for changes
|
||||
this.watcher = watch(this.eventsDir, (_eventType, filename) => {
|
||||
if (!filename || !filename.endsWith(".json")) return;
|
||||
this.debounce(filename, () => this.handleFileChange(filename));
|
||||
});
|
||||
|
||||
log.logInfo(`Events watcher started, tracking ${this.knownFiles.size} files`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop watching and cancel all scheduled events.
|
||||
*/
|
||||
stop(): void {
|
||||
// Stop fs watcher
|
||||
if (this.watcher) {
|
||||
this.watcher.close();
|
||||
this.watcher = null;
|
||||
}
|
||||
|
||||
// Cancel all debounce timers
|
||||
for (const timer of this.debounceTimers.values()) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
this.debounceTimers.clear();
|
||||
|
||||
// Cancel all scheduled timers
|
||||
for (const timer of this.timers.values()) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
this.timers.clear();
|
||||
|
||||
// Cancel all cron jobs
|
||||
for (const cron of this.crons.values()) {
|
||||
cron.stop();
|
||||
}
|
||||
this.crons.clear();
|
||||
|
||||
this.knownFiles.clear();
|
||||
log.logInfo("Events watcher stopped");
|
||||
}
|
||||
|
||||
private debounce(filename: string, fn: () => void): void {
|
||||
const existing = this.debounceTimers.get(filename);
|
||||
if (existing) {
|
||||
clearTimeout(existing);
|
||||
}
|
||||
this.debounceTimers.set(
|
||||
filename,
|
||||
setTimeout(() => {
|
||||
this.debounceTimers.delete(filename);
|
||||
fn();
|
||||
}, DEBOUNCE_MS),
|
||||
);
|
||||
}
|
||||
|
||||
private scanExisting(): void {
|
||||
let files: string[];
|
||||
try {
|
||||
files = readdirSync(this.eventsDir).filter((f) => f.endsWith(".json"));
|
||||
} catch (err) {
|
||||
log.logWarning("Failed to read events directory", String(err));
|
||||
return;
|
||||
}
|
||||
|
||||
for (const filename of files) {
|
||||
this.handleFile(filename);
|
||||
}
|
||||
}
|
||||
|
||||
private handleFileChange(filename: string): void {
|
||||
const filePath = join(this.eventsDir, filename);
|
||||
|
||||
if (!existsSync(filePath)) {
|
||||
// File was deleted
|
||||
this.handleDelete(filename);
|
||||
} else if (this.knownFiles.has(filename)) {
|
||||
// File was modified - cancel existing and re-schedule
|
||||
this.cancelScheduled(filename);
|
||||
this.handleFile(filename);
|
||||
} else {
|
||||
// New file
|
||||
this.handleFile(filename);
|
||||
}
|
||||
}
|
||||
|
||||
private handleDelete(filename: string): void {
|
||||
if (!this.knownFiles.has(filename)) return;
|
||||
|
||||
log.logInfo(`Event file deleted: ${filename}`);
|
||||
this.cancelScheduled(filename);
|
||||
this.knownFiles.delete(filename);
|
||||
}
|
||||
|
||||
private cancelScheduled(filename: string): void {
|
||||
const timer = this.timers.get(filename);
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
this.timers.delete(filename);
|
||||
}
|
||||
|
||||
const cron = this.crons.get(filename);
|
||||
if (cron) {
|
||||
cron.stop();
|
||||
this.crons.delete(filename);
|
||||
}
|
||||
}
|
||||
|
||||
private async handleFile(filename: string): Promise<void> {
|
||||
const filePath = join(this.eventsDir, filename);
|
||||
|
||||
// Parse with retries
|
||||
let event: MomEvent | null = null;
|
||||
let lastError: Error | null = null;
|
||||
|
||||
for (let i = 0; i < MAX_RETRIES; i++) {
|
||||
try {
|
||||
const content = await readFile(filePath, "utf-8");
|
||||
event = this.parseEvent(content, filename);
|
||||
break;
|
||||
} catch (err) {
|
||||
lastError = err instanceof Error ? err : new Error(String(err));
|
||||
if (i < MAX_RETRIES - 1) {
|
||||
await this.sleep(RETRY_BASE_MS * 2 ** i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!event) {
|
||||
log.logWarning(`Failed to parse event file after ${MAX_RETRIES} retries: ${filename}`, lastError?.message);
|
||||
this.deleteFile(filename);
|
||||
return;
|
||||
}
|
||||
|
||||
this.knownFiles.add(filename);
|
||||
|
||||
// Schedule based on type
|
||||
switch (event.type) {
|
||||
case "immediate":
|
||||
this.handleImmediate(filename, event);
|
||||
break;
|
||||
case "one-shot":
|
||||
this.handleOneShot(filename, event);
|
||||
break;
|
||||
case "periodic":
|
||||
this.handlePeriodic(filename, event);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private parseEvent(content: string, filename: string): MomEvent | null {
|
||||
const data = JSON.parse(content);
|
||||
|
||||
if (!data.type || !data.channelId || !data.text) {
|
||||
throw new Error(`Missing required fields (type, channelId, text) in ${filename}`);
|
||||
}
|
||||
|
||||
switch (data.type) {
|
||||
case "immediate":
|
||||
return { type: "immediate", channelId: data.channelId, text: data.text };
|
||||
|
||||
case "one-shot":
|
||||
if (!data.at) {
|
||||
throw new Error(`Missing 'at' field for one-shot event in ${filename}`);
|
||||
}
|
||||
return { type: "one-shot", channelId: data.channelId, text: data.text, at: data.at };
|
||||
|
||||
case "periodic":
|
||||
if (!data.schedule) {
|
||||
throw new Error(`Missing 'schedule' field for periodic event in ${filename}`);
|
||||
}
|
||||
if (!data.timezone) {
|
||||
throw new Error(`Missing 'timezone' field for periodic event in ${filename}`);
|
||||
}
|
||||
return {
|
||||
type: "periodic",
|
||||
channelId: data.channelId,
|
||||
text: data.text,
|
||||
schedule: data.schedule,
|
||||
timezone: data.timezone,
|
||||
};
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown event type '${data.type}' in ${filename}`);
|
||||
}
|
||||
}
|
||||
|
||||
private handleImmediate(filename: string, event: ImmediateEvent): void {
|
||||
const filePath = join(this.eventsDir, filename);
|
||||
|
||||
// Check if stale (created before harness started)
|
||||
try {
|
||||
const stat = statSync(filePath);
|
||||
if (stat.mtimeMs < this.startTime) {
|
||||
log.logInfo(`Stale immediate event, deleting: ${filename}`);
|
||||
this.deleteFile(filename);
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// File may have been deleted
|
||||
return;
|
||||
}
|
||||
|
||||
log.logInfo(`Executing immediate event: ${filename}`);
|
||||
this.execute(filename, event);
|
||||
}
|
||||
|
||||
private handleOneShot(filename: string, event: OneShotEvent): void {
|
||||
const atTime = new Date(event.at).getTime();
|
||||
const now = Date.now();
|
||||
|
||||
if (atTime <= now) {
|
||||
// Past - delete without executing
|
||||
log.logInfo(`One-shot event in the past, deleting: ${filename}`);
|
||||
this.deleteFile(filename);
|
||||
return;
|
||||
}
|
||||
|
||||
const delay = atTime - now;
|
||||
log.logInfo(`Scheduling one-shot event: ${filename} in ${Math.round(delay / 1000)}s`);
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
this.timers.delete(filename);
|
||||
log.logInfo(`Executing one-shot event: ${filename}`);
|
||||
this.execute(filename, event);
|
||||
}, delay);
|
||||
|
||||
this.timers.set(filename, timer);
|
||||
}
|
||||
|
||||
private handlePeriodic(filename: string, event: PeriodicEvent): void {
|
||||
try {
|
||||
const cron = new Cron(event.schedule, { timezone: event.timezone }, () => {
|
||||
log.logInfo(`Executing periodic event: ${filename}`);
|
||||
this.execute(filename, event, false); // Don't delete periodic events
|
||||
});
|
||||
|
||||
this.crons.set(filename, cron);
|
||||
|
||||
const next = cron.nextRun();
|
||||
log.logInfo(`Scheduled periodic event: ${filename}, next run: ${next?.toISOString() ?? "unknown"}`);
|
||||
} catch (err) {
|
||||
log.logWarning(`Invalid cron schedule for ${filename}: ${event.schedule}`, String(err));
|
||||
this.deleteFile(filename);
|
||||
}
|
||||
}
|
||||
|
||||
private execute(filename: string, event: MomEvent, deleteAfter: boolean = true): void {
|
||||
// Format the message
|
||||
let scheduleInfo: string;
|
||||
switch (event.type) {
|
||||
case "immediate":
|
||||
scheduleInfo = "immediate";
|
||||
break;
|
||||
case "one-shot":
|
||||
scheduleInfo = event.at;
|
||||
break;
|
||||
case "periodic":
|
||||
scheduleInfo = event.schedule;
|
||||
break;
|
||||
}
|
||||
|
||||
const message = `[EVENT:${filename}:${event.type}:${scheduleInfo}] ${event.text}`;
|
||||
|
||||
// Create synthetic SlackEvent
|
||||
const syntheticEvent: SlackEvent = {
|
||||
type: "mention",
|
||||
channel: event.channelId,
|
||||
user: "EVENT",
|
||||
text: message,
|
||||
ts: Date.now().toString(),
|
||||
};
|
||||
|
||||
// Enqueue for processing
|
||||
const enqueued = this.slack.enqueueEvent(syntheticEvent);
|
||||
|
||||
if (enqueued && deleteAfter) {
|
||||
// Delete file after successful enqueue (immediate and one-shot)
|
||||
this.deleteFile(filename);
|
||||
} else if (!enqueued) {
|
||||
log.logWarning(`Event queue full, discarded: ${filename}`);
|
||||
// Still delete immediate/one-shot even if discarded
|
||||
if (deleteAfter) {
|
||||
this.deleteFile(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private deleteFile(filename: string): void {
|
||||
const filePath = join(this.eventsDir, filename);
|
||||
try {
|
||||
unlinkSync(filePath);
|
||||
} catch (err) {
|
||||
// ENOENT is fine (file already deleted), other errors are warnings
|
||||
if (err instanceof Error && "code" in err && err.code !== "ENOENT") {
|
||||
log.logWarning(`Failed to delete event file: ${filename}`, String(err));
|
||||
}
|
||||
}
|
||||
this.knownFiles.delete(filename);
|
||||
}
|
||||
|
||||
private sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and start an events watcher.
|
||||
*/
|
||||
export function createEventsWatcher(workspaceDir: string, slack: SlackBot): EventsWatcher {
|
||||
const eventsDir = join(workspaceDir, "events");
|
||||
return new EventsWatcher(eventsDir, slack);
|
||||
}
|
||||
@@ -1,271 +0,0 @@
|
||||
import chalk from "chalk";
|
||||
|
||||
export interface LogContext {
|
||||
channelId: string;
|
||||
userName?: string;
|
||||
channelName?: string; // For display like #dev-team vs C16HET4EQ
|
||||
}
|
||||
|
||||
function timestamp(): string {
|
||||
const now = new Date();
|
||||
const hh = String(now.getHours()).padStart(2, "0");
|
||||
const mm = String(now.getMinutes()).padStart(2, "0");
|
||||
const ss = String(now.getSeconds()).padStart(2, "0");
|
||||
return `[${hh}:${mm}:${ss}]`;
|
||||
}
|
||||
|
||||
function formatContext(ctx: LogContext): string {
|
||||
// DMs: [DM:username]
|
||||
// Channels: [#channel-name:username] or [C16HET4EQ:username] if no name
|
||||
if (ctx.channelId.startsWith("D")) {
|
||||
return `[DM:${ctx.userName || ctx.channelId}]`;
|
||||
}
|
||||
const channel = ctx.channelName || ctx.channelId;
|
||||
const user = ctx.userName || "unknown";
|
||||
return `[${channel.startsWith("#") ? channel : `#${channel}`}:${user}]`;
|
||||
}
|
||||
|
||||
function truncate(text: string, maxLen: number): string {
|
||||
if (text.length <= maxLen) return text;
|
||||
return `${text.substring(0, maxLen)}\n(truncated at ${maxLen} chars)`;
|
||||
}
|
||||
|
||||
function formatToolArgs(args: Record<string, unknown>): string {
|
||||
const lines: string[] = [];
|
||||
|
||||
for (const [key, value] of Object.entries(args)) {
|
||||
// Skip the label - it's already shown in the tool name
|
||||
if (key === "label") continue;
|
||||
|
||||
// For read tool, format path with offset/limit
|
||||
if (key === "path" && typeof value === "string") {
|
||||
const offset = args.offset as number | undefined;
|
||||
const limit = args.limit as number | undefined;
|
||||
if (offset !== undefined && limit !== undefined) {
|
||||
lines.push(`${value}:${offset}-${offset + limit}`);
|
||||
} else {
|
||||
lines.push(value);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip offset/limit since we already handled them
|
||||
if (key === "offset" || key === "limit") continue;
|
||||
|
||||
// For other values, format them
|
||||
if (typeof value === "string") {
|
||||
// Multi-line strings get indented
|
||||
if (value.includes("\n")) {
|
||||
lines.push(value);
|
||||
} else {
|
||||
lines.push(value);
|
||||
}
|
||||
} else {
|
||||
lines.push(JSON.stringify(value));
|
||||
}
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
// User messages
|
||||
export function logUserMessage(ctx: LogContext, text: string): void {
|
||||
console.log(chalk.green(`${timestamp()} ${formatContext(ctx)} ${text}`));
|
||||
}
|
||||
|
||||
// Tool execution
|
||||
export function logToolStart(ctx: LogContext, toolName: string, label: string, args: Record<string, unknown>): void {
|
||||
const formattedArgs = formatToolArgs(args);
|
||||
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ↳ ${toolName}: ${label}`));
|
||||
if (formattedArgs) {
|
||||
// Indent the args
|
||||
const indented = formattedArgs
|
||||
.split("\n")
|
||||
.map((line) => ` ${line}`)
|
||||
.join("\n");
|
||||
console.log(chalk.dim(indented));
|
||||
}
|
||||
}
|
||||
|
||||
export function logToolSuccess(ctx: LogContext, toolName: string, durationMs: number, result: string): void {
|
||||
const duration = (durationMs / 1000).toFixed(1);
|
||||
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ✓ ${toolName} (${duration}s)`));
|
||||
|
||||
const truncated = truncate(result, 1000);
|
||||
if (truncated) {
|
||||
const indented = truncated
|
||||
.split("\n")
|
||||
.map((line) => ` ${line}`)
|
||||
.join("\n");
|
||||
console.log(chalk.dim(indented));
|
||||
}
|
||||
}
|
||||
|
||||
export function logToolError(ctx: LogContext, toolName: string, durationMs: number, error: string): void {
|
||||
const duration = (durationMs / 1000).toFixed(1);
|
||||
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ✗ ${toolName} (${duration}s)`));
|
||||
|
||||
const truncated = truncate(error, 1000);
|
||||
const indented = truncated
|
||||
.split("\n")
|
||||
.map((line) => ` ${line}`)
|
||||
.join("\n");
|
||||
console.log(chalk.dim(indented));
|
||||
}
|
||||
|
||||
// Response streaming
|
||||
export function logResponseStart(ctx: LogContext): void {
|
||||
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} → Streaming response...`));
|
||||
}
|
||||
|
||||
export function logThinking(ctx: LogContext, thinking: string): void {
|
||||
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} 💭 Thinking`));
|
||||
const truncated = truncate(thinking, 1000);
|
||||
const indented = truncated
|
||||
.split("\n")
|
||||
.map((line) => ` ${line}`)
|
||||
.join("\n");
|
||||
console.log(chalk.dim(indented));
|
||||
}
|
||||
|
||||
export function logResponse(ctx: LogContext, text: string): void {
|
||||
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} 💬 Response`));
|
||||
const truncated = truncate(text, 1000);
|
||||
const indented = truncated
|
||||
.split("\n")
|
||||
.map((line) => ` ${line}`)
|
||||
.join("\n");
|
||||
console.log(chalk.dim(indented));
|
||||
}
|
||||
|
||||
// Attachments
|
||||
export function logDownloadStart(ctx: LogContext, filename: string, localPath: string): void {
|
||||
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ↓ Downloading attachment`));
|
||||
console.log(chalk.dim(` ${filename} → ${localPath}`));
|
||||
}
|
||||
|
||||
export function logDownloadSuccess(ctx: LogContext, sizeKB: number): void {
|
||||
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ✓ Downloaded (${sizeKB.toLocaleString()} KB)`));
|
||||
}
|
||||
|
||||
export function logDownloadError(ctx: LogContext, filename: string, error: string): void {
|
||||
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ✗ Download failed`));
|
||||
console.log(chalk.dim(` ${filename}: ${error}`));
|
||||
}
|
||||
|
||||
// Control
|
||||
export function logStopRequest(ctx: LogContext): void {
|
||||
console.log(chalk.green(`${timestamp()} ${formatContext(ctx)} stop`));
|
||||
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ⊗ Stop requested - aborting`));
|
||||
}
|
||||
|
||||
// System
|
||||
export function logInfo(message: string): void {
|
||||
console.log(chalk.blue(`${timestamp()} [system] ${message}`));
|
||||
}
|
||||
|
||||
export function logWarning(message: string, details?: string): void {
|
||||
console.log(chalk.yellow(`${timestamp()} [system] ⚠ ${message}`));
|
||||
if (details) {
|
||||
const indented = details
|
||||
.split("\n")
|
||||
.map((line) => ` ${line}`)
|
||||
.join("\n");
|
||||
console.log(chalk.dim(indented));
|
||||
}
|
||||
}
|
||||
|
||||
export function logAgentError(ctx: LogContext | "system", error: string): void {
|
||||
const context = ctx === "system" ? "[system]" : formatContext(ctx);
|
||||
console.log(chalk.yellow(`${timestamp()} ${context} ✗ Agent error`));
|
||||
const indented = error
|
||||
.split("\n")
|
||||
.map((line) => ` ${line}`)
|
||||
.join("\n");
|
||||
console.log(chalk.dim(indented));
|
||||
}
|
||||
|
||||
// Usage summary
|
||||
export function logUsageSummary(
|
||||
ctx: LogContext,
|
||||
usage: {
|
||||
input: number;
|
||||
output: number;
|
||||
cacheRead: number;
|
||||
cacheWrite: number;
|
||||
cost: { input: number; output: number; cacheRead: number; cacheWrite: number; total: number };
|
||||
},
|
||||
contextTokens?: number,
|
||||
contextWindow?: number,
|
||||
): string {
|
||||
const formatTokens = (count: number): string => {
|
||||
if (count < 1000) return count.toString();
|
||||
if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
|
||||
if (count < 1000000) return `${Math.round(count / 1000)}k`;
|
||||
return `${(count / 1000000).toFixed(1)}M`;
|
||||
};
|
||||
|
||||
const lines: string[] = [];
|
||||
lines.push("*Usage Summary*");
|
||||
lines.push(`Tokens: ${usage.input.toLocaleString()} in, ${usage.output.toLocaleString()} out`);
|
||||
if (usage.cacheRead > 0 || usage.cacheWrite > 0) {
|
||||
lines.push(`Cache: ${usage.cacheRead.toLocaleString()} read, ${usage.cacheWrite.toLocaleString()} write`);
|
||||
}
|
||||
if (contextTokens && contextWindow) {
|
||||
const contextPercent = ((contextTokens / contextWindow) * 100).toFixed(1);
|
||||
lines.push(`Context: ${formatTokens(contextTokens)} / ${formatTokens(contextWindow)} (${contextPercent}%)`);
|
||||
}
|
||||
lines.push(
|
||||
`Cost: $${usage.cost.input.toFixed(4)} in, $${usage.cost.output.toFixed(4)} out` +
|
||||
(usage.cacheRead > 0 || usage.cacheWrite > 0
|
||||
? `, $${usage.cost.cacheRead.toFixed(4)} cache read, $${usage.cost.cacheWrite.toFixed(4)} cache write`
|
||||
: ""),
|
||||
);
|
||||
lines.push(`*Total: $${usage.cost.total.toFixed(4)}*`);
|
||||
|
||||
const summary = lines.join("\n");
|
||||
|
||||
// Log to console
|
||||
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} 💰 Usage`));
|
||||
console.log(
|
||||
chalk.dim(
|
||||
` ${usage.input.toLocaleString()} in + ${usage.output.toLocaleString()} out` +
|
||||
(usage.cacheRead > 0 || usage.cacheWrite > 0
|
||||
? ` (${usage.cacheRead.toLocaleString()} cache read, ${usage.cacheWrite.toLocaleString()} cache write)`
|
||||
: "") +
|
||||
` = $${usage.cost.total.toFixed(4)}`,
|
||||
),
|
||||
);
|
||||
|
||||
return summary;
|
||||
}
|
||||
|
||||
// Startup (no context needed)
|
||||
export function logStartup(workingDir: string, sandbox: string): void {
|
||||
console.log("Starting mom bot...");
|
||||
console.log(` Working directory: ${workingDir}`);
|
||||
console.log(` Sandbox: ${sandbox}`);
|
||||
}
|
||||
|
||||
export function logConnected(): void {
|
||||
console.log("⚡️ Mom bot connected and listening!");
|
||||
console.log("");
|
||||
}
|
||||
|
||||
export function logDisconnected(): void {
|
||||
console.log("Mom bot disconnected.");
|
||||
}
|
||||
|
||||
// Backfill
|
||||
export function logBackfillStart(channelCount: number): void {
|
||||
console.log(chalk.blue(`${timestamp()} [system] Backfilling ${channelCount} channels...`));
|
||||
}
|
||||
|
||||
export function logBackfillChannel(channelName: string, messageCount: number): void {
|
||||
console.log(chalk.blue(`${timestamp()} [system] #${channelName}: ${messageCount} messages`));
|
||||
}
|
||||
|
||||
export function logBackfillComplete(totalMessages: number, durationMs: number): void {
|
||||
const duration = (durationMs / 1000).toFixed(1);
|
||||
console.log(chalk.blue(`${timestamp()} [system] Backfill complete: ${totalMessages} messages in ${duration}s`));
|
||||
}
|
||||
@@ -1,367 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { join, resolve } from "path";
|
||||
import { type AgentRunner, getOrCreateRunner } from "./agent.js";
|
||||
import { downloadChannel } from "./download.js";
|
||||
import { createEventsWatcher } from "./events.js";
|
||||
import * as log from "./log.js";
|
||||
import { parseSandboxArg, type SandboxConfig, validateSandbox } from "./sandbox.js";
|
||||
import { type MomHandler, type SlackBot, SlackBot as SlackBotClass, type SlackEvent } from "./slack.js";
|
||||
import { ChannelStore } from "./store.js";
|
||||
|
||||
// ============================================================================
|
||||
// Config
|
||||
// ============================================================================
|
||||
|
||||
const MOM_SLACK_APP_TOKEN = process.env.MOM_SLACK_APP_TOKEN;
|
||||
const MOM_SLACK_BOT_TOKEN = process.env.MOM_SLACK_BOT_TOKEN;
|
||||
|
||||
interface ParsedArgs {
|
||||
workingDir?: string;
|
||||
sandbox: SandboxConfig;
|
||||
downloadChannel?: string;
|
||||
}
|
||||
|
||||
function parseArgs(): ParsedArgs {
|
||||
const args = process.argv.slice(2);
|
||||
let sandbox: SandboxConfig = { type: "host" };
|
||||
let workingDir: string | undefined;
|
||||
let downloadChannelId: string | undefined;
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const arg = args[i];
|
||||
if (arg.startsWith("--sandbox=")) {
|
||||
sandbox = parseSandboxArg(arg.slice("--sandbox=".length));
|
||||
} else if (arg === "--sandbox") {
|
||||
sandbox = parseSandboxArg(args[++i] || "");
|
||||
} else if (arg.startsWith("--download=")) {
|
||||
downloadChannelId = arg.slice("--download=".length);
|
||||
} else if (arg === "--download") {
|
||||
downloadChannelId = args[++i];
|
||||
} else if (!arg.startsWith("-")) {
|
||||
workingDir = arg;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
workingDir: workingDir ? resolve(workingDir) : undefined,
|
||||
sandbox,
|
||||
downloadChannel: downloadChannelId,
|
||||
};
|
||||
}
|
||||
|
||||
const parsedArgs = parseArgs();
|
||||
|
||||
// Handle --download mode
|
||||
if (parsedArgs.downloadChannel) {
|
||||
if (!MOM_SLACK_BOT_TOKEN) {
|
||||
console.error("Missing env: MOM_SLACK_BOT_TOKEN");
|
||||
process.exit(1);
|
||||
}
|
||||
await downloadChannel(parsedArgs.downloadChannel, MOM_SLACK_BOT_TOKEN);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Normal bot mode - require working dir
|
||||
if (!parsedArgs.workingDir) {
|
||||
console.error("Usage: mom [--sandbox=host|docker:<name>] <working-directory>");
|
||||
console.error(" mom --download <channel-id>");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const { workingDir, sandbox } = { workingDir: parsedArgs.workingDir, sandbox: parsedArgs.sandbox };
|
||||
|
||||
if (!MOM_SLACK_APP_TOKEN || !MOM_SLACK_BOT_TOKEN) {
|
||||
console.error("Missing env: MOM_SLACK_APP_TOKEN, MOM_SLACK_BOT_TOKEN");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
await validateSandbox(sandbox);
|
||||
|
||||
// ============================================================================
|
||||
// State (per channel)
|
||||
// ============================================================================
|
||||
|
||||
interface ChannelState {
|
||||
running: boolean;
|
||||
runner: AgentRunner;
|
||||
store: ChannelStore;
|
||||
stopRequested: boolean;
|
||||
stopMessageTs?: string;
|
||||
}
|
||||
|
||||
const channelStates = new Map<string, ChannelState>();
|
||||
|
||||
function getState(channelId: string): ChannelState {
|
||||
let state = channelStates.get(channelId);
|
||||
if (!state) {
|
||||
const channelDir = join(workingDir, channelId);
|
||||
state = {
|
||||
running: false,
|
||||
runner: getOrCreateRunner(sandbox, channelId, channelDir),
|
||||
store: new ChannelStore({ workingDir, botToken: MOM_SLACK_BOT_TOKEN! }),
|
||||
stopRequested: false,
|
||||
};
|
||||
channelStates.set(channelId, state);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Create SlackContext adapter
|
||||
// ============================================================================
|
||||
|
||||
function createSlackContext(event: SlackEvent, slack: SlackBot, state: ChannelState, isEvent?: boolean) {
|
||||
let messageTs: string | null = null;
|
||||
const threadMessageTs: string[] = [];
|
||||
let accumulatedText = "";
|
||||
let isWorking = true;
|
||||
const workingIndicator = " ...";
|
||||
let updatePromise = Promise.resolve();
|
||||
|
||||
const user = slack.getUser(event.user);
|
||||
|
||||
// Extract event filename for status message
|
||||
const eventFilename = isEvent ? event.text.match(/^\[EVENT:([^:]+):/)?.[1] : undefined;
|
||||
|
||||
return {
|
||||
message: {
|
||||
text: event.text,
|
||||
rawText: event.text,
|
||||
user: event.user,
|
||||
userName: user?.userName,
|
||||
channel: event.channel,
|
||||
ts: event.ts,
|
||||
attachments: (event.attachments || []).map((a) => ({ local: a.local })),
|
||||
},
|
||||
channelName: slack.getChannel(event.channel)?.name,
|
||||
store: state.store,
|
||||
channels: slack.getAllChannels().map((c) => ({ id: c.id, name: c.name })),
|
||||
users: slack.getAllUsers().map((u) => ({ id: u.id, userName: u.userName, displayName: u.displayName })),
|
||||
|
||||
respond: async (text: string, shouldLog = true) => {
|
||||
updatePromise = updatePromise.then(async () => {
|
||||
try {
|
||||
accumulatedText = accumulatedText ? `${accumulatedText}\n${text}` : text;
|
||||
|
||||
// Truncate accumulated text if too long (Slack limit is 40K, we use 35K for safety)
|
||||
const MAX_MAIN_LENGTH = 35000;
|
||||
const truncationNote = "\n\n_(message truncated, ask me to elaborate on specific parts)_";
|
||||
if (accumulatedText.length > MAX_MAIN_LENGTH) {
|
||||
accumulatedText =
|
||||
accumulatedText.substring(0, MAX_MAIN_LENGTH - truncationNote.length) + truncationNote;
|
||||
}
|
||||
|
||||
const displayText = isWorking ? accumulatedText + workingIndicator : accumulatedText;
|
||||
|
||||
if (messageTs) {
|
||||
await slack.updateMessage(event.channel, messageTs, displayText);
|
||||
} else {
|
||||
messageTs = await slack.postMessage(event.channel, displayText);
|
||||
}
|
||||
|
||||
if (shouldLog && messageTs) {
|
||||
slack.logBotResponse(event.channel, text, messageTs);
|
||||
}
|
||||
} catch (err) {
|
||||
log.logWarning("Slack respond error", err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
});
|
||||
await updatePromise;
|
||||
},
|
||||
|
||||
replaceMessage: async (text: string) => {
|
||||
updatePromise = updatePromise.then(async () => {
|
||||
try {
|
||||
// Replace the accumulated text entirely, with truncation
|
||||
const MAX_MAIN_LENGTH = 35000;
|
||||
const truncationNote = "\n\n_(message truncated, ask me to elaborate on specific parts)_";
|
||||
if (text.length > MAX_MAIN_LENGTH) {
|
||||
accumulatedText = text.substring(0, MAX_MAIN_LENGTH - truncationNote.length) + truncationNote;
|
||||
} else {
|
||||
accumulatedText = text;
|
||||
}
|
||||
|
||||
const displayText = isWorking ? accumulatedText + workingIndicator : accumulatedText;
|
||||
|
||||
if (messageTs) {
|
||||
await slack.updateMessage(event.channel, messageTs, displayText);
|
||||
} else {
|
||||
messageTs = await slack.postMessage(event.channel, displayText);
|
||||
}
|
||||
} catch (err) {
|
||||
log.logWarning("Slack replaceMessage error", err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
});
|
||||
await updatePromise;
|
||||
},
|
||||
|
||||
respondInThread: async (text: string) => {
|
||||
updatePromise = updatePromise.then(async () => {
|
||||
try {
|
||||
if (messageTs) {
|
||||
// Truncate thread messages if too long (20K limit for safety)
|
||||
const MAX_THREAD_LENGTH = 20000;
|
||||
let threadText = text;
|
||||
if (threadText.length > MAX_THREAD_LENGTH) {
|
||||
threadText = `${threadText.substring(0, MAX_THREAD_LENGTH - 50)}\n\n_(truncated)_`;
|
||||
}
|
||||
|
||||
const ts = await slack.postInThread(event.channel, messageTs, threadText);
|
||||
threadMessageTs.push(ts);
|
||||
}
|
||||
} catch (err) {
|
||||
log.logWarning("Slack respondInThread error", err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
});
|
||||
await updatePromise;
|
||||
},
|
||||
|
||||
setTyping: async (isTyping: boolean) => {
|
||||
if (isTyping && !messageTs) {
|
||||
updatePromise = updatePromise.then(async () => {
|
||||
try {
|
||||
if (!messageTs) {
|
||||
accumulatedText = eventFilename ? `_Starting event: ${eventFilename}_` : "_Thinking_";
|
||||
messageTs = await slack.postMessage(event.channel, accumulatedText + workingIndicator);
|
||||
}
|
||||
} catch (err) {
|
||||
log.logWarning("Slack setTyping error", err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
});
|
||||
await updatePromise;
|
||||
}
|
||||
},
|
||||
|
||||
uploadFile: async (filePath: string, title?: string) => {
|
||||
await slack.uploadFile(event.channel, filePath, title);
|
||||
},
|
||||
|
||||
setWorking: async (working: boolean) => {
|
||||
updatePromise = updatePromise.then(async () => {
|
||||
try {
|
||||
isWorking = working;
|
||||
if (messageTs) {
|
||||
const displayText = isWorking ? accumulatedText + workingIndicator : accumulatedText;
|
||||
await slack.updateMessage(event.channel, messageTs, displayText);
|
||||
}
|
||||
} catch (err) {
|
||||
log.logWarning("Slack setWorking error", err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
});
|
||||
await updatePromise;
|
||||
},
|
||||
|
||||
deleteMessage: async () => {
|
||||
updatePromise = updatePromise.then(async () => {
|
||||
// Delete thread messages first (in reverse order)
|
||||
for (let i = threadMessageTs.length - 1; i >= 0; i--) {
|
||||
try {
|
||||
await slack.deleteMessage(event.channel, threadMessageTs[i]);
|
||||
} catch {
|
||||
// Ignore errors deleting thread messages
|
||||
}
|
||||
}
|
||||
threadMessageTs.length = 0;
|
||||
// Then delete main message
|
||||
if (messageTs) {
|
||||
await slack.deleteMessage(event.channel, messageTs);
|
||||
messageTs = null;
|
||||
}
|
||||
});
|
||||
await updatePromise;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Handler
|
||||
// ============================================================================
|
||||
|
||||
const handler: MomHandler = {
|
||||
isRunning(channelId: string): boolean {
|
||||
const state = channelStates.get(channelId);
|
||||
return state?.running ?? false;
|
||||
},
|
||||
|
||||
async handleStop(channelId: string, slack: SlackBot): Promise<void> {
|
||||
const state = channelStates.get(channelId);
|
||||
if (state?.running) {
|
||||
state.stopRequested = true;
|
||||
state.runner.abort();
|
||||
const ts = await slack.postMessage(channelId, "_Stopping..._");
|
||||
state.stopMessageTs = ts; // Save for updating later
|
||||
} else {
|
||||
await slack.postMessage(channelId, "_Nothing running_");
|
||||
}
|
||||
},
|
||||
|
||||
async handleEvent(event: SlackEvent, slack: SlackBot, isEvent?: boolean): Promise<void> {
|
||||
const state = getState(event.channel);
|
||||
|
||||
// Start run
|
||||
state.running = true;
|
||||
state.stopRequested = false;
|
||||
|
||||
log.logInfo(`[${event.channel}] Starting run: ${event.text.substring(0, 50)}`);
|
||||
|
||||
try {
|
||||
// Create context adapter
|
||||
const ctx = createSlackContext(event, slack, state, isEvent);
|
||||
|
||||
// Run the agent
|
||||
await ctx.setTyping(true);
|
||||
await ctx.setWorking(true);
|
||||
const result = await state.runner.run(ctx as any, state.store);
|
||||
await ctx.setWorking(false);
|
||||
|
||||
if (result.stopReason === "aborted" && state.stopRequested) {
|
||||
if (state.stopMessageTs) {
|
||||
await slack.updateMessage(event.channel, state.stopMessageTs, "_Stopped_");
|
||||
state.stopMessageTs = undefined;
|
||||
} else {
|
||||
await slack.postMessage(event.channel, "_Stopped_");
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
log.logWarning(`[${event.channel}] Run error`, err instanceof Error ? err.message : String(err));
|
||||
} finally {
|
||||
state.running = false;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Start
|
||||
// ============================================================================
|
||||
|
||||
log.logStartup(workingDir, sandbox.type === "host" ? "host" : `docker:${sandbox.container}`);
|
||||
|
||||
// Shared store for attachment downloads (also used per-channel in getState)
|
||||
const sharedStore = new ChannelStore({ workingDir, botToken: MOM_SLACK_BOT_TOKEN! });
|
||||
|
||||
const bot = new SlackBotClass(handler, {
|
||||
appToken: MOM_SLACK_APP_TOKEN,
|
||||
botToken: MOM_SLACK_BOT_TOKEN,
|
||||
workingDir,
|
||||
store: sharedStore,
|
||||
});
|
||||
|
||||
// Start events watcher
|
||||
const eventsWatcher = createEventsWatcher(workingDir, bot);
|
||||
eventsWatcher.start();
|
||||
|
||||
// Handle shutdown
|
||||
process.on("SIGINT", () => {
|
||||
log.logInfo("Shutting down...");
|
||||
eventsWatcher.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
process.on("SIGTERM", () => {
|
||||
log.logInfo("Shutting down...");
|
||||
eventsWatcher.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
bot.start();
|
||||
@@ -1,221 +0,0 @@
|
||||
import { spawn } from "child_process";
|
||||
|
||||
export type SandboxConfig = { type: "host" } | { type: "docker"; container: string };
|
||||
|
||||
export function parseSandboxArg(value: string): SandboxConfig {
|
||||
if (value === "host") {
|
||||
return { type: "host" };
|
||||
}
|
||||
if (value.startsWith("docker:")) {
|
||||
const container = value.slice("docker:".length);
|
||||
if (!container) {
|
||||
console.error("Error: docker sandbox requires container name (e.g., docker:mom-sandbox)");
|
||||
process.exit(1);
|
||||
}
|
||||
return { type: "docker", container };
|
||||
}
|
||||
console.error(`Error: Invalid sandbox type '${value}'. Use 'host' or 'docker:<container-name>'`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
export async function validateSandbox(config: SandboxConfig): Promise<void> {
|
||||
if (config.type === "host") {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if Docker is available
|
||||
try {
|
||||
await execSimple("docker", ["--version"]);
|
||||
} catch {
|
||||
console.error("Error: Docker is not installed or not in PATH");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Check if container exists and is running
|
||||
try {
|
||||
const result = await execSimple("docker", ["inspect", "-f", "{{.State.Running}}", config.container]);
|
||||
if (result.trim() !== "true") {
|
||||
console.error(`Error: Container '${config.container}' is not running.`);
|
||||
console.error(`Start it with: docker start ${config.container}`);
|
||||
process.exit(1);
|
||||
}
|
||||
} catch {
|
||||
console.error(`Error: Container '${config.container}' does not exist.`);
|
||||
console.error("Create it with: ./docker.sh create <data-dir>");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(` Docker container '${config.container}' is running.`);
|
||||
}
|
||||
|
||||
function execSimple(cmd: string, args: string[]): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(cmd, args, { stdio: ["ignore", "pipe", "pipe"] });
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
child.stdout?.on("data", (d) => {
|
||||
stdout += d;
|
||||
});
|
||||
child.stderr?.on("data", (d) => {
|
||||
stderr += d;
|
||||
});
|
||||
child.on("close", (code) => {
|
||||
if (code === 0) resolve(stdout);
|
||||
else reject(new Error(stderr || `Exit code ${code}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an executor that runs commands either on host or in Docker container
|
||||
*/
|
||||
export function createExecutor(config: SandboxConfig): Executor {
|
||||
if (config.type === "host") {
|
||||
return new HostExecutor();
|
||||
}
|
||||
return new DockerExecutor(config.container);
|
||||
}
|
||||
|
||||
export interface Executor {
|
||||
/**
|
||||
* Execute a bash command
|
||||
*/
|
||||
exec(command: string, options?: ExecOptions): Promise<ExecResult>;
|
||||
|
||||
/**
|
||||
* Get the workspace path prefix for this executor
|
||||
* Host: returns the actual path
|
||||
* Docker: returns /workspace
|
||||
*/
|
||||
getWorkspacePath(hostPath: string): string;
|
||||
}
|
||||
|
||||
export interface ExecOptions {
|
||||
timeout?: number;
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
export interface ExecResult {
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
code: number;
|
||||
}
|
||||
|
||||
class HostExecutor implements Executor {
|
||||
async exec(command: string, options?: ExecOptions): Promise<ExecResult> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const shell = process.platform === "win32" ? "cmd" : "sh";
|
||||
const shellArgs = process.platform === "win32" ? ["/c"] : ["-c"];
|
||||
|
||||
const child = spawn(shell, [...shellArgs, command], {
|
||||
detached: true,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
let timedOut = false;
|
||||
|
||||
const timeoutHandle =
|
||||
options?.timeout && options.timeout > 0
|
||||
? setTimeout(() => {
|
||||
timedOut = true;
|
||||
killProcessTree(child.pid!);
|
||||
}, options.timeout * 1000)
|
||||
: undefined;
|
||||
|
||||
const onAbort = () => {
|
||||
if (child.pid) killProcessTree(child.pid);
|
||||
};
|
||||
|
||||
if (options?.signal) {
|
||||
if (options.signal.aborted) {
|
||||
onAbort();
|
||||
} else {
|
||||
options.signal.addEventListener("abort", onAbort, { once: true });
|
||||
}
|
||||
}
|
||||
|
||||
child.stdout?.on("data", (data) => {
|
||||
stdout += data.toString();
|
||||
if (stdout.length > 10 * 1024 * 1024) {
|
||||
stdout = stdout.slice(0, 10 * 1024 * 1024);
|
||||
}
|
||||
});
|
||||
|
||||
child.stderr?.on("data", (data) => {
|
||||
stderr += data.toString();
|
||||
if (stderr.length > 10 * 1024 * 1024) {
|
||||
stderr = stderr.slice(0, 10 * 1024 * 1024);
|
||||
}
|
||||
});
|
||||
|
||||
child.on("close", (code) => {
|
||||
if (timeoutHandle) clearTimeout(timeoutHandle);
|
||||
if (options?.signal) {
|
||||
options.signal.removeEventListener("abort", onAbort);
|
||||
}
|
||||
|
||||
if (options?.signal?.aborted) {
|
||||
reject(new Error(`${stdout}\n${stderr}\nCommand aborted`.trim()));
|
||||
return;
|
||||
}
|
||||
|
||||
if (timedOut) {
|
||||
reject(new Error(`${stdout}\n${stderr}\nCommand timed out after ${options?.timeout} seconds`.trim()));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve({ stdout, stderr, code: code ?? 0 });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getWorkspacePath(hostPath: string): string {
|
||||
return hostPath;
|
||||
}
|
||||
}
|
||||
|
||||
class DockerExecutor implements Executor {
|
||||
constructor(private container: string) {}
|
||||
|
||||
async exec(command: string, options?: ExecOptions): Promise<ExecResult> {
|
||||
// Wrap command for docker exec
|
||||
const dockerCmd = `docker exec ${this.container} sh -c ${shellEscape(command)}`;
|
||||
const hostExecutor = new HostExecutor();
|
||||
return hostExecutor.exec(dockerCmd, options);
|
||||
}
|
||||
|
||||
getWorkspacePath(_hostPath: string): string {
|
||||
// Docker container sees /workspace
|
||||
return "/workspace";
|
||||
}
|
||||
}
|
||||
|
||||
function killProcessTree(pid: number): void {
|
||||
if (process.platform === "win32") {
|
||||
try {
|
||||
spawn("taskkill", ["/F", "/T", "/PID", String(pid)], {
|
||||
stdio: "ignore",
|
||||
detached: true,
|
||||
});
|
||||
} catch {
|
||||
// Ignore errors
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
process.kill(-pid, "SIGKILL");
|
||||
} catch {
|
||||
try {
|
||||
process.kill(pid, "SIGKILL");
|
||||
} catch {
|
||||
// Process already dead
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function shellEscape(s: string): string {
|
||||
// Escape for passing to sh -c
|
||||
return `'${s.replace(/'/g, "'\\''")}'`;
|
||||
}
|
||||
@@ -1,623 +0,0 @@
|
||||
import { SocketModeClient } from "@slack/socket-mode";
|
||||
import { WebClient } from "@slack/web-api";
|
||||
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "fs";
|
||||
import { basename, join } from "path";
|
||||
import * as log from "./log.js";
|
||||
import type { Attachment, ChannelStore } from "./store.js";
|
||||
|
||||
// ============================================================================
|
||||
// Types
|
||||
// ============================================================================
|
||||
|
||||
export interface SlackEvent {
|
||||
type: "mention" | "dm";
|
||||
channel: string;
|
||||
ts: string;
|
||||
user: string;
|
||||
text: string;
|
||||
files?: Array<{ name?: string; url_private_download?: string; url_private?: string }>;
|
||||
/** Processed attachments with local paths (populated after logUserMessage) */
|
||||
attachments?: Attachment[];
|
||||
}
|
||||
|
||||
export interface SlackUser {
|
||||
id: string;
|
||||
userName: string;
|
||||
displayName: string;
|
||||
}
|
||||
|
||||
export interface SlackChannel {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
// Types used by agent.ts
|
||||
export interface ChannelInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface UserInfo {
|
||||
id: string;
|
||||
userName: string;
|
||||
displayName: string;
|
||||
}
|
||||
|
||||
export interface SlackContext {
|
||||
message: {
|
||||
text: string;
|
||||
rawText: string;
|
||||
user: string;
|
||||
userName?: string;
|
||||
channel: string;
|
||||
ts: string;
|
||||
attachments: Array<{ local: string }>;
|
||||
};
|
||||
channelName?: string;
|
||||
channels: ChannelInfo[];
|
||||
users: UserInfo[];
|
||||
respond: (text: string, shouldLog?: boolean) => Promise<void>;
|
||||
replaceMessage: (text: string) => Promise<void>;
|
||||
respondInThread: (text: string) => Promise<void>;
|
||||
setTyping: (isTyping: boolean) => Promise<void>;
|
||||
uploadFile: (filePath: string, title?: string) => Promise<void>;
|
||||
setWorking: (working: boolean) => Promise<void>;
|
||||
deleteMessage: () => Promise<void>;
|
||||
}
|
||||
|
||||
export interface MomHandler {
|
||||
/**
|
||||
* Check if channel is currently running (SYNC)
|
||||
*/
|
||||
isRunning(channelId: string): boolean;
|
||||
|
||||
/**
|
||||
* Handle an event that triggers mom (ASYNC)
|
||||
* Called only when isRunning() returned false for user messages.
|
||||
* Events always queue and pass isEvent=true.
|
||||
*/
|
||||
handleEvent(event: SlackEvent, slack: SlackBot, isEvent?: boolean): Promise<void>;
|
||||
|
||||
/**
|
||||
* Handle stop command (ASYNC)
|
||||
* Called when user says "stop" while mom is running
|
||||
*/
|
||||
handleStop(channelId: string, slack: SlackBot): Promise<void>;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Per-channel queue for sequential processing
|
||||
// ============================================================================
|
||||
|
||||
type QueuedWork = () => Promise<void>;
|
||||
|
||||
class ChannelQueue {
|
||||
private queue: QueuedWork[] = [];
|
||||
private processing = false;
|
||||
|
||||
enqueue(work: QueuedWork): void {
|
||||
this.queue.push(work);
|
||||
this.processNext();
|
||||
}
|
||||
|
||||
size(): number {
|
||||
return this.queue.length;
|
||||
}
|
||||
|
||||
private async processNext(): Promise<void> {
|
||||
if (this.processing || this.queue.length === 0) return;
|
||||
this.processing = true;
|
||||
const work = this.queue.shift()!;
|
||||
try {
|
||||
await work();
|
||||
} catch (err) {
|
||||
log.logWarning("Queue error", err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
this.processing = false;
|
||||
this.processNext();
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// SlackBot
|
||||
// ============================================================================
|
||||
|
||||
export class SlackBot {
|
||||
private socketClient: SocketModeClient;
|
||||
private webClient: WebClient;
|
||||
private handler: MomHandler;
|
||||
private workingDir: string;
|
||||
private store: ChannelStore;
|
||||
private botUserId: string | null = null;
|
||||
private startupTs: string | null = null; // Messages older than this are just logged, not processed
|
||||
|
||||
private users = new Map<string, SlackUser>();
|
||||
private channels = new Map<string, SlackChannel>();
|
||||
private queues = new Map<string, ChannelQueue>();
|
||||
|
||||
constructor(
|
||||
handler: MomHandler,
|
||||
config: { appToken: string; botToken: string; workingDir: string; store: ChannelStore },
|
||||
) {
|
||||
this.handler = handler;
|
||||
this.workingDir = config.workingDir;
|
||||
this.store = config.store;
|
||||
this.socketClient = new SocketModeClient({ appToken: config.appToken });
|
||||
this.webClient = new WebClient(config.botToken);
|
||||
}
|
||||
|
||||
// ==========================================================================
|
||||
// Public API
|
||||
// ==========================================================================
|
||||
|
||||
async start(): Promise<void> {
|
||||
const auth = await this.webClient.auth.test();
|
||||
this.botUserId = auth.user_id as string;
|
||||
|
||||
await Promise.all([this.fetchUsers(), this.fetchChannels()]);
|
||||
log.logInfo(`Loaded ${this.channels.size} channels, ${this.users.size} users`);
|
||||
|
||||
await this.backfillAllChannels();
|
||||
|
||||
this.setupEventHandlers();
|
||||
await this.socketClient.start();
|
||||
|
||||
// Record startup time - messages older than this are just logged, not processed
|
||||
this.startupTs = (Date.now() / 1000).toFixed(6);
|
||||
|
||||
log.logConnected();
|
||||
}
|
||||
|
||||
getUser(userId: string): SlackUser | undefined {
|
||||
return this.users.get(userId);
|
||||
}
|
||||
|
||||
getChannel(channelId: string): SlackChannel | undefined {
|
||||
return this.channels.get(channelId);
|
||||
}
|
||||
|
||||
getAllUsers(): SlackUser[] {
|
||||
return Array.from(this.users.values());
|
||||
}
|
||||
|
||||
getAllChannels(): SlackChannel[] {
|
||||
return Array.from(this.channels.values());
|
||||
}
|
||||
|
||||
async postMessage(channel: string, text: string): Promise<string> {
|
||||
const result = await this.webClient.chat.postMessage({ channel, text });
|
||||
return result.ts as string;
|
||||
}
|
||||
|
||||
async updateMessage(channel: string, ts: string, text: string): Promise<void> {
|
||||
await this.webClient.chat.update({ channel, ts, text });
|
||||
}
|
||||
|
||||
async deleteMessage(channel: string, ts: string): Promise<void> {
|
||||
await this.webClient.chat.delete({ channel, ts });
|
||||
}
|
||||
|
||||
async postInThread(channel: string, threadTs: string, text: string): Promise<string> {
|
||||
const result = await this.webClient.chat.postMessage({ channel, thread_ts: threadTs, text });
|
||||
return result.ts as string;
|
||||
}
|
||||
|
||||
async uploadFile(channel: string, filePath: string, title?: string): Promise<void> {
|
||||
const fileName = title || basename(filePath);
|
||||
const fileContent = readFileSync(filePath);
|
||||
await this.webClient.files.uploadV2({
|
||||
channel_id: channel,
|
||||
file: fileContent,
|
||||
filename: fileName,
|
||||
title: fileName,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a message to log.jsonl (SYNC)
|
||||
* This is the ONLY place messages are written to log.jsonl
|
||||
*/
|
||||
logToFile(channel: string, entry: object): void {
|
||||
const dir = join(this.workingDir, channel);
|
||||
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
||||
appendFileSync(join(dir, "log.jsonl"), `${JSON.stringify(entry)}\n`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a bot response to log.jsonl
|
||||
*/
|
||||
logBotResponse(channel: string, text: string, ts: string): void {
|
||||
this.logToFile(channel, {
|
||||
date: new Date().toISOString(),
|
||||
ts,
|
||||
user: "bot",
|
||||
text,
|
||||
attachments: [],
|
||||
isBot: true,
|
||||
});
|
||||
}
|
||||
|
||||
// ==========================================================================
|
||||
// Events Integration
|
||||
// ==========================================================================
|
||||
|
||||
/**
|
||||
* Enqueue an event for processing. Always queues (no "already working" rejection).
|
||||
* Returns true if enqueued, false if queue is full (max 5).
|
||||
*/
|
||||
enqueueEvent(event: SlackEvent): boolean {
|
||||
const queue = this.getQueue(event.channel);
|
||||
if (queue.size() >= 5) {
|
||||
log.logWarning(`Event queue full for ${event.channel}, discarding: ${event.text.substring(0, 50)}`);
|
||||
return false;
|
||||
}
|
||||
log.logInfo(`Enqueueing event for ${event.channel}: ${event.text.substring(0, 50)}`);
|
||||
queue.enqueue(() => this.handler.handleEvent(event, this, true));
|
||||
return true;
|
||||
}
|
||||
|
||||
// ==========================================================================
|
||||
// Private - Event Handlers
|
||||
// ==========================================================================
|
||||
|
||||
private getQueue(channelId: string): ChannelQueue {
|
||||
let queue = this.queues.get(channelId);
|
||||
if (!queue) {
|
||||
queue = new ChannelQueue();
|
||||
this.queues.set(channelId, queue);
|
||||
}
|
||||
return queue;
|
||||
}
|
||||
|
||||
private setupEventHandlers(): void {
|
||||
// Channel @mentions
|
||||
this.socketClient.on("app_mention", ({ event, ack }) => {
|
||||
const e = event as {
|
||||
text: string;
|
||||
channel: string;
|
||||
user: string;
|
||||
ts: string;
|
||||
files?: Array<{ name: string; url_private_download?: string; url_private?: string }>;
|
||||
};
|
||||
|
||||
// Skip DMs (handled by message event)
|
||||
if (e.channel.startsWith("D")) {
|
||||
ack();
|
||||
return;
|
||||
}
|
||||
|
||||
const slackEvent: SlackEvent = {
|
||||
type: "mention",
|
||||
channel: e.channel,
|
||||
ts: e.ts,
|
||||
user: e.user,
|
||||
text: e.text.replace(/<@[A-Z0-9]+>/gi, "").trim(),
|
||||
files: e.files,
|
||||
};
|
||||
|
||||
// SYNC: Log to log.jsonl (ALWAYS, even for old messages)
|
||||
// Also downloads attachments in background and stores local paths
|
||||
slackEvent.attachments = this.logUserMessage(slackEvent);
|
||||
|
||||
// Only trigger processing for messages AFTER startup (not replayed old messages)
|
||||
if (this.startupTs && e.ts < this.startupTs) {
|
||||
log.logInfo(
|
||||
`[${e.channel}] Logged old message (pre-startup), not triggering: ${slackEvent.text.substring(0, 30)}`,
|
||||
);
|
||||
ack();
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for stop command - execute immediately, don't queue!
|
||||
if (slackEvent.text.toLowerCase().trim() === "stop") {
|
||||
if (this.handler.isRunning(e.channel)) {
|
||||
this.handler.handleStop(e.channel, this); // Don't await, don't queue
|
||||
} else {
|
||||
this.postMessage(e.channel, "_Nothing running_");
|
||||
}
|
||||
ack();
|
||||
return;
|
||||
}
|
||||
|
||||
// SYNC: Check if busy
|
||||
if (this.handler.isRunning(e.channel)) {
|
||||
this.postMessage(e.channel, "_Already working. Say `@mom stop` to cancel._");
|
||||
} else {
|
||||
this.getQueue(e.channel).enqueue(() => this.handler.handleEvent(slackEvent, this));
|
||||
}
|
||||
|
||||
ack();
|
||||
});
|
||||
|
||||
// All messages (for logging) + DMs (for triggering)
|
||||
this.socketClient.on("message", ({ event, ack }) => {
|
||||
const e = event as {
|
||||
text?: string;
|
||||
channel: string;
|
||||
user?: string;
|
||||
ts: string;
|
||||
channel_type?: string;
|
||||
subtype?: string;
|
||||
bot_id?: string;
|
||||
files?: Array<{ name: string; url_private_download?: string; url_private?: string }>;
|
||||
};
|
||||
|
||||
// Skip bot messages, edits, etc.
|
||||
if (e.bot_id || !e.user || e.user === this.botUserId) {
|
||||
ack();
|
||||
return;
|
||||
}
|
||||
if (e.subtype !== undefined && e.subtype !== "file_share") {
|
||||
ack();
|
||||
return;
|
||||
}
|
||||
if (!e.text && (!e.files || e.files.length === 0)) {
|
||||
ack();
|
||||
return;
|
||||
}
|
||||
|
||||
const isDM = e.channel_type === "im";
|
||||
const isBotMention = e.text?.includes(`<@${this.botUserId}>`);
|
||||
|
||||
// Skip channel @mentions - already handled by app_mention event
|
||||
if (!isDM && isBotMention) {
|
||||
ack();
|
||||
return;
|
||||
}
|
||||
|
||||
const slackEvent: SlackEvent = {
|
||||
type: isDM ? "dm" : "mention",
|
||||
channel: e.channel,
|
||||
ts: e.ts,
|
||||
user: e.user,
|
||||
text: (e.text || "").replace(/<@[A-Z0-9]+>/gi, "").trim(),
|
||||
files: e.files,
|
||||
};
|
||||
|
||||
// SYNC: Log to log.jsonl (ALL messages - channel chatter and DMs)
|
||||
// Also downloads attachments in background and stores local paths
|
||||
slackEvent.attachments = this.logUserMessage(slackEvent);
|
||||
|
||||
// Only trigger processing for messages AFTER startup (not replayed old messages)
|
||||
if (this.startupTs && e.ts < this.startupTs) {
|
||||
log.logInfo(`[${e.channel}] Skipping old message (pre-startup): ${slackEvent.text.substring(0, 30)}`);
|
||||
ack();
|
||||
return;
|
||||
}
|
||||
|
||||
// Only trigger handler for DMs
|
||||
if (isDM) {
|
||||
// Check for stop command - execute immediately, don't queue!
|
||||
if (slackEvent.text.toLowerCase().trim() === "stop") {
|
||||
if (this.handler.isRunning(e.channel)) {
|
||||
this.handler.handleStop(e.channel, this); // Don't await, don't queue
|
||||
} else {
|
||||
this.postMessage(e.channel, "_Nothing running_");
|
||||
}
|
||||
ack();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.handler.isRunning(e.channel)) {
|
||||
this.postMessage(e.channel, "_Already working. Say `stop` to cancel._");
|
||||
} else {
|
||||
this.getQueue(e.channel).enqueue(() => this.handler.handleEvent(slackEvent, this));
|
||||
}
|
||||
}
|
||||
|
||||
ack();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a user message to log.jsonl (SYNC)
|
||||
* Downloads attachments in background via store
|
||||
*/
|
||||
private logUserMessage(event: SlackEvent): Attachment[] {
|
||||
const user = this.users.get(event.user);
|
||||
// Process attachments - queues downloads in background
|
||||
const attachments = event.files ? this.store.processAttachments(event.channel, event.files, event.ts) : [];
|
||||
this.logToFile(event.channel, {
|
||||
date: new Date(parseFloat(event.ts) * 1000).toISOString(),
|
||||
ts: event.ts,
|
||||
user: event.user,
|
||||
userName: user?.userName,
|
||||
displayName: user?.displayName,
|
||||
text: event.text,
|
||||
attachments,
|
||||
isBot: false,
|
||||
});
|
||||
return attachments;
|
||||
}
|
||||
|
||||
// ==========================================================================
|
||||
// Private - Backfill
|
||||
// ==========================================================================
|
||||
|
||||
private getExistingTimestamps(channelId: string): Set<string> {
|
||||
const logPath = join(this.workingDir, channelId, "log.jsonl");
|
||||
const timestamps = new Set<string>();
|
||||
if (!existsSync(logPath)) return timestamps;
|
||||
|
||||
const content = readFileSync(logPath, "utf-8");
|
||||
const lines = content.trim().split("\n").filter(Boolean);
|
||||
for (const line of lines) {
|
||||
try {
|
||||
const entry = JSON.parse(line);
|
||||
if (entry.ts) timestamps.add(entry.ts);
|
||||
} catch {}
|
||||
}
|
||||
return timestamps;
|
||||
}
|
||||
|
||||
private async backfillChannel(channelId: string): Promise<number> {
|
||||
const existingTs = this.getExistingTimestamps(channelId);
|
||||
|
||||
// Find the biggest ts in log.jsonl
|
||||
let latestTs: string | undefined;
|
||||
for (const ts of existingTs) {
|
||||
if (!latestTs || parseFloat(ts) > parseFloat(latestTs)) latestTs = ts;
|
||||
}
|
||||
|
||||
type Message = {
|
||||
user?: string;
|
||||
bot_id?: string;
|
||||
text?: string;
|
||||
ts?: string;
|
||||
subtype?: string;
|
||||
files?: Array<{ name: string }>;
|
||||
};
|
||||
const allMessages: Message[] = [];
|
||||
|
||||
let cursor: string | undefined;
|
||||
let pageCount = 0;
|
||||
const maxPages = 3;
|
||||
|
||||
do {
|
||||
const result = await this.webClient.conversations.history({
|
||||
channel: channelId,
|
||||
oldest: latestTs, // Only fetch messages newer than what we have
|
||||
inclusive: false,
|
||||
limit: 1000,
|
||||
cursor,
|
||||
});
|
||||
if (result.messages) {
|
||||
allMessages.push(...(result.messages as Message[]));
|
||||
}
|
||||
cursor = result.response_metadata?.next_cursor;
|
||||
pageCount++;
|
||||
} while (cursor && pageCount < maxPages);
|
||||
|
||||
// Filter: include mom's messages, exclude other bots, skip already logged
|
||||
const relevantMessages = allMessages.filter((msg) => {
|
||||
if (!msg.ts || existingTs.has(msg.ts)) return false; // Skip duplicates
|
||||
if (msg.user === this.botUserId) return true;
|
||||
if (msg.bot_id) return false;
|
||||
if (msg.subtype !== undefined && msg.subtype !== "file_share") return false;
|
||||
if (!msg.user) return false;
|
||||
if (!msg.text && (!msg.files || msg.files.length === 0)) return false;
|
||||
return true;
|
||||
});
|
||||
|
||||
// Reverse to chronological order
|
||||
relevantMessages.reverse();
|
||||
|
||||
// Log each message to log.jsonl
|
||||
for (const msg of relevantMessages) {
|
||||
const isMomMessage = msg.user === this.botUserId;
|
||||
const user = this.users.get(msg.user!);
|
||||
// Strip @mentions from text (same as live messages)
|
||||
const text = (msg.text || "").replace(/<@[A-Z0-9]+>/gi, "").trim();
|
||||
// Process attachments - queues downloads in background
|
||||
const attachments = msg.files ? this.store.processAttachments(channelId, msg.files, msg.ts!) : [];
|
||||
|
||||
this.logToFile(channelId, {
|
||||
date: new Date(parseFloat(msg.ts!) * 1000).toISOString(),
|
||||
ts: msg.ts!,
|
||||
user: isMomMessage ? "bot" : msg.user!,
|
||||
userName: isMomMessage ? undefined : user?.userName,
|
||||
displayName: isMomMessage ? undefined : user?.displayName,
|
||||
text,
|
||||
attachments,
|
||||
isBot: isMomMessage,
|
||||
});
|
||||
}
|
||||
|
||||
return relevantMessages.length;
|
||||
}
|
||||
|
||||
private async backfillAllChannels(): Promise<void> {
|
||||
const startTime = Date.now();
|
||||
|
||||
// Only backfill channels that already have a log.jsonl (mom has interacted with them before)
|
||||
const channelsToBackfill: Array<[string, SlackChannel]> = [];
|
||||
for (const [channelId, channel] of this.channels) {
|
||||
const logPath = join(this.workingDir, channelId, "log.jsonl");
|
||||
if (existsSync(logPath)) {
|
||||
channelsToBackfill.push([channelId, channel]);
|
||||
}
|
||||
}
|
||||
|
||||
log.logBackfillStart(channelsToBackfill.length);
|
||||
|
||||
let totalMessages = 0;
|
||||
for (const [channelId, channel] of channelsToBackfill) {
|
||||
try {
|
||||
const count = await this.backfillChannel(channelId);
|
||||
if (count > 0) log.logBackfillChannel(channel.name, count);
|
||||
totalMessages += count;
|
||||
} catch (error) {
|
||||
log.logWarning(`Failed to backfill #${channel.name}`, String(error));
|
||||
}
|
||||
}
|
||||
|
||||
const durationMs = Date.now() - startTime;
|
||||
log.logBackfillComplete(totalMessages, durationMs);
|
||||
}
|
||||
|
||||
// ==========================================================================
|
||||
// Private - Fetch Users/Channels
|
||||
// ==========================================================================
|
||||
|
||||
private async fetchUsers(): Promise<void> {
|
||||
let cursor: string | undefined;
|
||||
do {
|
||||
const result = await this.webClient.users.list({ limit: 200, cursor });
|
||||
const members = result.members as
|
||||
| Array<{ id?: string; name?: string; real_name?: string; deleted?: boolean }>
|
||||
| undefined;
|
||||
if (members) {
|
||||
for (const u of members) {
|
||||
if (u.id && u.name && !u.deleted) {
|
||||
this.users.set(u.id, { id: u.id, userName: u.name, displayName: u.real_name || u.name });
|
||||
}
|
||||
}
|
||||
}
|
||||
cursor = result.response_metadata?.next_cursor;
|
||||
} while (cursor);
|
||||
}
|
||||
|
||||
private async fetchChannels(): Promise<void> {
|
||||
// Fetch public/private channels
|
||||
let cursor: string | undefined;
|
||||
do {
|
||||
const result = await this.webClient.conversations.list({
|
||||
types: "public_channel,private_channel",
|
||||
exclude_archived: true,
|
||||
limit: 200,
|
||||
cursor,
|
||||
});
|
||||
const channels = result.channels as Array<{ id?: string; name?: string; is_member?: boolean }> | undefined;
|
||||
if (channels) {
|
||||
for (const c of channels) {
|
||||
if (c.id && c.name && c.is_member) {
|
||||
this.channels.set(c.id, { id: c.id, name: c.name });
|
||||
}
|
||||
}
|
||||
}
|
||||
cursor = result.response_metadata?.next_cursor;
|
||||
} while (cursor);
|
||||
|
||||
// Also fetch DM channels (IMs)
|
||||
cursor = undefined;
|
||||
do {
|
||||
const result = await this.webClient.conversations.list({
|
||||
types: "im",
|
||||
limit: 200,
|
||||
cursor,
|
||||
});
|
||||
const ims = result.channels as Array<{ id?: string; user?: string }> | undefined;
|
||||
if (ims) {
|
||||
for (const im of ims) {
|
||||
if (im.id) {
|
||||
// Use user's name as channel name for DMs
|
||||
const user = im.user ? this.users.get(im.user) : undefined;
|
||||
const name = user ? `DM:${user.userName}` : `DM:${im.id}`;
|
||||
this.channels.set(im.id, { id: im.id, name });
|
||||
}
|
||||
}
|
||||
}
|
||||
cursor = result.response_metadata?.next_cursor;
|
||||
} while (cursor);
|
||||
}
|
||||
}
|
||||
@@ -1,234 +0,0 @@
|
||||
import { existsSync, mkdirSync, readFileSync } from "fs";
|
||||
import { appendFile, writeFile } from "fs/promises";
|
||||
import { join } from "path";
|
||||
import * as log from "./log.js";
|
||||
|
||||
export interface Attachment {
|
||||
original: string; // original filename from uploader
|
||||
local: string; // path relative to working dir (e.g., "C12345/attachments/1732531234567_file.png")
|
||||
}
|
||||
|
||||
export interface LoggedMessage {
|
||||
date: string; // ISO 8601 date (e.g., "2025-11-26T10:44:00.000Z") for easy grepping
|
||||
ts: string; // slack timestamp or epoch ms
|
||||
user: string; // user ID (or "bot" for bot responses)
|
||||
userName?: string; // handle (e.g., "mario")
|
||||
displayName?: string; // display name (e.g., "Mario Zechner")
|
||||
text: string;
|
||||
attachments: Attachment[];
|
||||
isBot: boolean;
|
||||
}
|
||||
|
||||
export interface ChannelStoreConfig {
|
||||
workingDir: string;
|
||||
botToken: string; // needed for authenticated file downloads
|
||||
}
|
||||
|
||||
interface PendingDownload {
|
||||
channelId: string;
|
||||
localPath: string; // relative path
|
||||
url: string;
|
||||
}
|
||||
|
||||
export class ChannelStore {
|
||||
private workingDir: string;
|
||||
private botToken: string;
|
||||
private pendingDownloads: PendingDownload[] = [];
|
||||
private isDownloading = false;
|
||||
// Track recently logged message timestamps to prevent duplicates
|
||||
// Key: "channelId:ts", automatically cleaned up after 60 seconds
|
||||
private recentlyLogged = new Map<string, number>();
|
||||
|
||||
constructor(config: ChannelStoreConfig) {
|
||||
this.workingDir = config.workingDir;
|
||||
this.botToken = config.botToken;
|
||||
|
||||
// Ensure working directory exists
|
||||
if (!existsSync(this.workingDir)) {
|
||||
mkdirSync(this.workingDir, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create the directory for a channel/DM
|
||||
*/
|
||||
getChannelDir(channelId: string): string {
|
||||
const dir = join(this.workingDir, channelId);
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique local filename for an attachment
|
||||
*/
|
||||
generateLocalFilename(originalName: string, timestamp: string): string {
|
||||
// Convert slack timestamp (1234567890.123456) to milliseconds
|
||||
const ts = Math.floor(parseFloat(timestamp) * 1000);
|
||||
// Sanitize original name (remove problematic characters)
|
||||
const sanitized = originalName.replace(/[^a-zA-Z0-9._-]/g, "_");
|
||||
return `${ts}_${sanitized}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process attachments from a Slack message event
|
||||
* Returns attachment metadata and queues downloads
|
||||
*/
|
||||
processAttachments(
|
||||
channelId: string,
|
||||
files: Array<{ name?: string; url_private_download?: string; url_private?: string }>,
|
||||
timestamp: string,
|
||||
): Attachment[] {
|
||||
const attachments: Attachment[] = [];
|
||||
|
||||
for (const file of files) {
|
||||
const url = file.url_private_download || file.url_private;
|
||||
if (!url) continue;
|
||||
if (!file.name) {
|
||||
log.logWarning("Attachment missing name, skipping", url);
|
||||
continue;
|
||||
}
|
||||
|
||||
const filename = this.generateLocalFilename(file.name, timestamp);
|
||||
const localPath = `${channelId}/attachments/${filename}`;
|
||||
|
||||
attachments.push({
|
||||
original: file.name,
|
||||
local: localPath,
|
||||
});
|
||||
|
||||
// Queue for background download
|
||||
this.pendingDownloads.push({ channelId, localPath, url });
|
||||
}
|
||||
|
||||
// Trigger background download
|
||||
this.processDownloadQueue();
|
||||
|
||||
return attachments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a message to the channel's log.jsonl
|
||||
* Returns false if message was already logged (duplicate)
|
||||
*/
|
||||
async logMessage(channelId: string, message: LoggedMessage): Promise<boolean> {
|
||||
// Check for duplicate (same channel + timestamp)
|
||||
const dedupeKey = `${channelId}:${message.ts}`;
|
||||
if (this.recentlyLogged.has(dedupeKey)) {
|
||||
return false; // Already logged
|
||||
}
|
||||
|
||||
// Mark as logged and schedule cleanup after 60 seconds
|
||||
this.recentlyLogged.set(dedupeKey, Date.now());
|
||||
setTimeout(() => this.recentlyLogged.delete(dedupeKey), 60000);
|
||||
|
||||
const logPath = join(this.getChannelDir(channelId), "log.jsonl");
|
||||
|
||||
// Ensure message has a date field
|
||||
if (!message.date) {
|
||||
// Parse timestamp to get date
|
||||
let date: Date;
|
||||
if (message.ts.includes(".")) {
|
||||
// Slack timestamp format (1234567890.123456)
|
||||
date = new Date(parseFloat(message.ts) * 1000);
|
||||
} else {
|
||||
// Epoch milliseconds
|
||||
date = new Date(parseInt(message.ts, 10));
|
||||
}
|
||||
message.date = date.toISOString();
|
||||
}
|
||||
|
||||
const line = `${JSON.stringify(message)}\n`;
|
||||
await appendFile(logPath, line, "utf-8");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a bot response
|
||||
*/
|
||||
async logBotResponse(channelId: string, text: string, ts: string): Promise<void> {
|
||||
await this.logMessage(channelId, {
|
||||
date: new Date().toISOString(),
|
||||
ts,
|
||||
user: "bot",
|
||||
text,
|
||||
attachments: [],
|
||||
isBot: true,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timestamp of the last logged message for a channel
|
||||
* Returns null if no log exists
|
||||
*/
|
||||
getLastTimestamp(channelId: string): string | null {
|
||||
const logPath = join(this.workingDir, channelId, "log.jsonl");
|
||||
if (!existsSync(logPath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const content = readFileSync(logPath, "utf-8");
|
||||
const lines = content.trim().split("\n");
|
||||
if (lines.length === 0 || lines[0] === "") {
|
||||
return null;
|
||||
}
|
||||
const lastLine = lines[lines.length - 1];
|
||||
const message = JSON.parse(lastLine) as LoggedMessage;
|
||||
return message.ts;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the download queue in the background
|
||||
*/
|
||||
private async processDownloadQueue(): Promise<void> {
|
||||
if (this.isDownloading || this.pendingDownloads.length === 0) return;
|
||||
|
||||
this.isDownloading = true;
|
||||
|
||||
while (this.pendingDownloads.length > 0) {
|
||||
const item = this.pendingDownloads.shift();
|
||||
if (!item) break;
|
||||
|
||||
try {
|
||||
await this.downloadAttachment(item.localPath, item.url);
|
||||
// Success - could add success logging here if we have context
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
log.logWarning(`Failed to download attachment`, `${item.localPath}: ${errorMsg}`);
|
||||
}
|
||||
}
|
||||
|
||||
this.isDownloading = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Download a single attachment
|
||||
*/
|
||||
private async downloadAttachment(localPath: string, url: string): Promise<void> {
|
||||
const filePath = join(this.workingDir, localPath);
|
||||
|
||||
// Ensure directory exists
|
||||
const dir = join(this.workingDir, localPath.substring(0, localPath.lastIndexOf("/")));
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.botToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const buffer = await response.arrayBuffer();
|
||||
await writeFile(filePath, Buffer.from(buffer));
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
import type { AgentTool } from "@mariozechner/pi-agent-core";
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { basename, resolve as resolvePath } from "path";
|
||||
|
||||
// This will be set by the agent before running
|
||||
let uploadFn: ((filePath: string, title?: string) => Promise<void>) | null = null;
|
||||
|
||||
export function setUploadFunction(fn: (filePath: string, title?: string) => Promise<void>): void {
|
||||
uploadFn = fn;
|
||||
}
|
||||
|
||||
const attachSchema = Type.Object({
|
||||
label: Type.String({ description: "Brief description of what you're sharing (shown to user)" }),
|
||||
path: Type.String({ description: "Path to the file to attach" }),
|
||||
title: Type.Optional(Type.String({ description: "Title for the file (defaults to filename)" })),
|
||||
});
|
||||
|
||||
export const attachTool: AgentTool<typeof attachSchema> = {
|
||||
name: "attach",
|
||||
label: "attach",
|
||||
description:
|
||||
"Attach a file to your response. Use this to share files, images, or documents with the user. Only files from /workspace/ can be attached.",
|
||||
parameters: attachSchema,
|
||||
execute: async (
|
||||
_toolCallId: string,
|
||||
{ path, title }: { label: string; path: string; title?: string },
|
||||
signal?: AbortSignal,
|
||||
) => {
|
||||
if (!uploadFn) {
|
||||
throw new Error("Upload function not configured");
|
||||
}
|
||||
|
||||
if (signal?.aborted) {
|
||||
throw new Error("Operation aborted");
|
||||
}
|
||||
|
||||
const absolutePath = resolvePath(path);
|
||||
const fileName = title || basename(absolutePath);
|
||||
|
||||
await uploadFn(absolutePath, fileName);
|
||||
|
||||
return {
|
||||
content: [{ type: "text" as const, text: `Attached file: ${fileName}` }],
|
||||
details: undefined,
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -1,97 +0,0 @@
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { createWriteStream } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { AgentTool } from "@mariozechner/pi-agent-core";
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import type { Executor } from "../sandbox.js";
|
||||
import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult, truncateTail } from "./truncate.js";
|
||||
|
||||
/**
|
||||
* Generate a unique temp file path for bash output
|
||||
*/
|
||||
function getTempFilePath(): string {
|
||||
const id = randomBytes(8).toString("hex");
|
||||
return join(tmpdir(), `mom-bash-${id}.log`);
|
||||
}
|
||||
|
||||
const bashSchema = Type.Object({
|
||||
label: Type.String({ description: "Brief description of what this command does (shown to user)" }),
|
||||
command: Type.String({ description: "Bash command to execute" }),
|
||||
timeout: Type.Optional(Type.Number({ description: "Timeout in seconds (optional, no default timeout)" })),
|
||||
});
|
||||
|
||||
interface BashToolDetails {
|
||||
truncation?: TruncationResult;
|
||||
fullOutputPath?: string;
|
||||
}
|
||||
|
||||
export function createBashTool(executor: Executor): AgentTool<typeof bashSchema> {
|
||||
return {
|
||||
name: "bash",
|
||||
label: "bash",
|
||||
description: `Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.`,
|
||||
parameters: bashSchema,
|
||||
execute: async (
|
||||
_toolCallId: string,
|
||||
{ command, timeout }: { label: string; command: string; timeout?: number },
|
||||
signal?: AbortSignal,
|
||||
) => {
|
||||
// Track output for potential temp file writing
|
||||
let tempFilePath: string | undefined;
|
||||
let tempFileStream: ReturnType<typeof createWriteStream> | undefined;
|
||||
|
||||
const result = await executor.exec(command, { timeout, signal });
|
||||
let output = "";
|
||||
if (result.stdout) output += result.stdout;
|
||||
if (result.stderr) {
|
||||
if (output) output += "\n";
|
||||
output += result.stderr;
|
||||
}
|
||||
|
||||
const totalBytes = Buffer.byteLength(output, "utf-8");
|
||||
|
||||
// Write to temp file if output exceeds limit
|
||||
if (totalBytes > DEFAULT_MAX_BYTES) {
|
||||
tempFilePath = getTempFilePath();
|
||||
tempFileStream = createWriteStream(tempFilePath);
|
||||
tempFileStream.write(output);
|
||||
tempFileStream.end();
|
||||
}
|
||||
|
||||
// Apply tail truncation
|
||||
const truncation = truncateTail(output);
|
||||
let outputText = truncation.content || "(no output)";
|
||||
|
||||
// Build details with truncation info
|
||||
let details: BashToolDetails | undefined;
|
||||
|
||||
if (truncation.truncated) {
|
||||
details = {
|
||||
truncation,
|
||||
fullOutputPath: tempFilePath,
|
||||
};
|
||||
|
||||
// Build actionable notice
|
||||
const startLine = truncation.totalLines - truncation.outputLines + 1;
|
||||
const endLine = truncation.totalLines;
|
||||
|
||||
if (truncation.lastLinePartial) {
|
||||
// Edge case: last line alone > 50KB
|
||||
const lastLineSize = formatSize(Buffer.byteLength(output.split("\n").pop() || "", "utf-8"));
|
||||
outputText += `\n\n[Showing last ${formatSize(truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${tempFilePath}]`;
|
||||
} else if (truncation.truncatedBy === "lines") {
|
||||
outputText += `\n\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines}. Full output: ${tempFilePath}]`;
|
||||
} else {
|
||||
outputText += `\n\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${tempFilePath}]`;
|
||||
}
|
||||
}
|
||||
|
||||
if (result.code !== 0) {
|
||||
throw new Error(`${outputText}\n\nCommand exited with code ${result.code}`.trim());
|
||||
}
|
||||
|
||||
return { content: [{ type: "text", text: outputText }], details };
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,165 +0,0 @@
|
||||
import type { AgentTool } from "@mariozechner/pi-agent-core";
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import * as Diff from "diff";
|
||||
import type { Executor } from "../sandbox.js";
|
||||
|
||||
/**
|
||||
* Generate a unified diff string with line numbers and context
|
||||
*/
|
||||
function generateDiffString(oldContent: string, newContent: string, contextLines = 4): string {
|
||||
const parts = Diff.diffLines(oldContent, newContent);
|
||||
const output: string[] = [];
|
||||
|
||||
const oldLines = oldContent.split("\n");
|
||||
const newLines = newContent.split("\n");
|
||||
const maxLineNum = Math.max(oldLines.length, newLines.length);
|
||||
const lineNumWidth = String(maxLineNum).length;
|
||||
|
||||
let oldLineNum = 1;
|
||||
let newLineNum = 1;
|
||||
let lastWasChange = false;
|
||||
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const part = parts[i];
|
||||
const raw = part.value.split("\n");
|
||||
if (raw[raw.length - 1] === "") {
|
||||
raw.pop();
|
||||
}
|
||||
|
||||
if (part.added || part.removed) {
|
||||
for (const line of raw) {
|
||||
if (part.added) {
|
||||
const lineNum = String(newLineNum).padStart(lineNumWidth, " ");
|
||||
output.push(`+${lineNum} ${line}`);
|
||||
newLineNum++;
|
||||
} else {
|
||||
const lineNum = String(oldLineNum).padStart(lineNumWidth, " ");
|
||||
output.push(`-${lineNum} ${line}`);
|
||||
oldLineNum++;
|
||||
}
|
||||
}
|
||||
lastWasChange = true;
|
||||
} else {
|
||||
const nextPartIsChange = i < parts.length - 1 && (parts[i + 1].added || parts[i + 1].removed);
|
||||
|
||||
if (lastWasChange || nextPartIsChange) {
|
||||
let linesToShow = raw;
|
||||
let skipStart = 0;
|
||||
let skipEnd = 0;
|
||||
|
||||
if (!lastWasChange) {
|
||||
skipStart = Math.max(0, raw.length - contextLines);
|
||||
linesToShow = raw.slice(skipStart);
|
||||
}
|
||||
|
||||
if (!nextPartIsChange && linesToShow.length > contextLines) {
|
||||
skipEnd = linesToShow.length - contextLines;
|
||||
linesToShow = linesToShow.slice(0, contextLines);
|
||||
}
|
||||
|
||||
if (skipStart > 0) {
|
||||
output.push(` ${"".padStart(lineNumWidth, " ")} ...`);
|
||||
}
|
||||
|
||||
for (const line of linesToShow) {
|
||||
const lineNum = String(oldLineNum).padStart(lineNumWidth, " ");
|
||||
output.push(` ${lineNum} ${line}`);
|
||||
oldLineNum++;
|
||||
newLineNum++;
|
||||
}
|
||||
|
||||
if (skipEnd > 0) {
|
||||
output.push(` ${"".padStart(lineNumWidth, " ")} ...`);
|
||||
}
|
||||
|
||||
oldLineNum += skipStart + skipEnd;
|
||||
newLineNum += skipStart + skipEnd;
|
||||
} else {
|
||||
oldLineNum += raw.length;
|
||||
newLineNum += raw.length;
|
||||
}
|
||||
|
||||
lastWasChange = false;
|
||||
}
|
||||
}
|
||||
|
||||
return output.join("\n");
|
||||
}
|
||||
|
||||
const editSchema = Type.Object({
|
||||
label: Type.String({ description: "Brief description of the edit you're making (shown to user)" }),
|
||||
path: Type.String({ description: "Path to the file to edit (relative or absolute)" }),
|
||||
oldText: Type.String({ description: "Exact text to find and replace (must match exactly)" }),
|
||||
newText: Type.String({ description: "New text to replace the old text with" }),
|
||||
});
|
||||
|
||||
export function createEditTool(executor: Executor): AgentTool<typeof editSchema> {
|
||||
return {
|
||||
name: "edit",
|
||||
label: "edit",
|
||||
description:
|
||||
"Edit a file by replacing exact text. The oldText must match exactly (including whitespace). Use this for precise, surgical edits.",
|
||||
parameters: editSchema,
|
||||
execute: async (
|
||||
_toolCallId: string,
|
||||
{ path, oldText, newText }: { label: string; path: string; oldText: string; newText: string },
|
||||
signal?: AbortSignal,
|
||||
) => {
|
||||
// Read the file
|
||||
const readResult = await executor.exec(`cat ${shellEscape(path)}`, { signal });
|
||||
if (readResult.code !== 0) {
|
||||
throw new Error(readResult.stderr || `File not found: ${path}`);
|
||||
}
|
||||
|
||||
const content = readResult.stdout;
|
||||
|
||||
// Check if old text exists
|
||||
if (!content.includes(oldText)) {
|
||||
throw new Error(
|
||||
`Could not find the exact text in ${path}. The old text must match exactly including all whitespace and newlines.`,
|
||||
);
|
||||
}
|
||||
|
||||
// Count occurrences
|
||||
const occurrences = content.split(oldText).length - 1;
|
||||
|
||||
if (occurrences > 1) {
|
||||
throw new Error(
|
||||
`Found ${occurrences} occurrences of the text in ${path}. The text must be unique. Please provide more context to make it unique.`,
|
||||
);
|
||||
}
|
||||
|
||||
// Perform replacement
|
||||
const index = content.indexOf(oldText);
|
||||
const newContent = content.substring(0, index) + newText + content.substring(index + oldText.length);
|
||||
|
||||
if (content === newContent) {
|
||||
throw new Error(
|
||||
`No changes made to ${path}. The replacement produced identical content. This might indicate an issue with special characters or the text not existing as expected.`,
|
||||
);
|
||||
}
|
||||
|
||||
// Write the file back
|
||||
const writeResult = await executor.exec(`printf '%s' ${shellEscape(newContent)} > ${shellEscape(path)}`, {
|
||||
signal,
|
||||
});
|
||||
if (writeResult.code !== 0) {
|
||||
throw new Error(writeResult.stderr || `Failed to write file: ${path}`);
|
||||
}
|
||||
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Successfully replaced text in ${path}. Changed ${oldText.length} characters to ${newText.length} characters.`,
|
||||
},
|
||||
],
|
||||
details: { diff: generateDiffString(content, newContent) },
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function shellEscape(s: string): string {
|
||||
return `'${s.replace(/'/g, "'\\''")}'`;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import type { AgentTool } from "@mariozechner/pi-agent-core";
|
||||
import type { Executor } from "../sandbox.js";
|
||||
import { attachTool } from "./attach.js";
|
||||
import { createBashTool } from "./bash.js";
|
||||
import { createEditTool } from "./edit.js";
|
||||
import { createReadTool } from "./read.js";
|
||||
import { createWriteTool } from "./write.js";
|
||||
|
||||
export { setUploadFunction } from "./attach.js";
|
||||
|
||||
export function createMomTools(executor: Executor): AgentTool<any>[] {
|
||||
return [
|
||||
createReadTool(executor),
|
||||
createBashTool(executor),
|
||||
createEditTool(executor),
|
||||
createWriteTool(executor),
|
||||
attachTool,
|
||||
];
|
||||
}
|
||||
@@ -1,159 +0,0 @@
|
||||
import type { AgentTool } from "@mariozechner/pi-agent-core";
|
||||
import type { ImageContent, TextContent } from "@mariozechner/pi-ai";
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { extname } from "path";
|
||||
import type { Executor } from "../sandbox.js";
|
||||
import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult, truncateHead } from "./truncate.js";
|
||||
|
||||
/**
|
||||
* Map of file extensions to MIME types for common image formats
|
||||
*/
|
||||
const IMAGE_MIME_TYPES: Record<string, string> = {
|
||||
".jpg": "image/jpeg",
|
||||
".jpeg": "image/jpeg",
|
||||
".png": "image/png",
|
||||
".gif": "image/gif",
|
||||
".webp": "image/webp",
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if a file is an image based on its extension
|
||||
*/
|
||||
function isImageFile(filePath: string): string | null {
|
||||
const ext = extname(filePath).toLowerCase();
|
||||
return IMAGE_MIME_TYPES[ext] || null;
|
||||
}
|
||||
|
||||
const readSchema = Type.Object({
|
||||
label: Type.String({ description: "Brief description of what you're reading and why (shown to user)" }),
|
||||
path: Type.String({ description: "Path to the file to read (relative or absolute)" }),
|
||||
offset: Type.Optional(Type.Number({ description: "Line number to start reading from (1-indexed)" })),
|
||||
limit: Type.Optional(Type.Number({ description: "Maximum number of lines to read" })),
|
||||
});
|
||||
|
||||
interface ReadToolDetails {
|
||||
truncation?: TruncationResult;
|
||||
}
|
||||
|
||||
export function createReadTool(executor: Executor): AgentTool<typeof readSchema> {
|
||||
return {
|
||||
name: "read",
|
||||
label: "read",
|
||||
description: `Read the contents of a file. Supports text files and images (jpg, png, gif, webp). Images are sent as attachments. For text files, output is truncated to ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Use offset/limit for large files.`,
|
||||
parameters: readSchema,
|
||||
execute: async (
|
||||
_toolCallId: string,
|
||||
{ path, offset, limit }: { label: string; path: string; offset?: number; limit?: number },
|
||||
signal?: AbortSignal,
|
||||
): Promise<{ content: (TextContent | ImageContent)[]; details: ReadToolDetails | undefined }> => {
|
||||
const mimeType = isImageFile(path);
|
||||
|
||||
if (mimeType) {
|
||||
// Read as image (binary) - use base64
|
||||
const result = await executor.exec(`base64 < ${shellEscape(path)}`, { signal });
|
||||
if (result.code !== 0) {
|
||||
throw new Error(result.stderr || `Failed to read file: ${path}`);
|
||||
}
|
||||
const base64 = result.stdout.replace(/\s/g, ""); // Remove whitespace from base64
|
||||
|
||||
return {
|
||||
content: [
|
||||
{ type: "text", text: `Read image file [${mimeType}]` },
|
||||
{ type: "image", data: base64, mimeType },
|
||||
],
|
||||
details: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
// Get total line count first
|
||||
const countResult = await executor.exec(`wc -l < ${shellEscape(path)}`, { signal });
|
||||
if (countResult.code !== 0) {
|
||||
throw new Error(countResult.stderr || `Failed to read file: ${path}`);
|
||||
}
|
||||
const totalFileLines = Number.parseInt(countResult.stdout.trim(), 10) + 1; // wc -l counts newlines, not lines
|
||||
|
||||
// Apply offset if specified (1-indexed)
|
||||
const startLine = offset ? Math.max(1, offset) : 1;
|
||||
const startLineDisplay = startLine;
|
||||
|
||||
// Check if offset is out of bounds
|
||||
if (startLine > totalFileLines) {
|
||||
throw new Error(`Offset ${offset} is beyond end of file (${totalFileLines} lines total)`);
|
||||
}
|
||||
|
||||
// Read content with offset
|
||||
let cmd: string;
|
||||
if (startLine === 1) {
|
||||
cmd = `cat ${shellEscape(path)}`;
|
||||
} else {
|
||||
cmd = `tail -n +${startLine} ${shellEscape(path)}`;
|
||||
}
|
||||
|
||||
const result = await executor.exec(cmd, { signal });
|
||||
if (result.code !== 0) {
|
||||
throw new Error(result.stderr || `Failed to read file: ${path}`);
|
||||
}
|
||||
|
||||
let selectedContent = result.stdout;
|
||||
let userLimitedLines: number | undefined;
|
||||
|
||||
// Apply user limit if specified
|
||||
if (limit !== undefined) {
|
||||
const lines = selectedContent.split("\n");
|
||||
const endLine = Math.min(limit, lines.length);
|
||||
selectedContent = lines.slice(0, endLine).join("\n");
|
||||
userLimitedLines = endLine;
|
||||
}
|
||||
|
||||
// Apply truncation (respects both line and byte limits)
|
||||
const truncation = truncateHead(selectedContent);
|
||||
|
||||
let outputText: string;
|
||||
let details: ReadToolDetails | undefined;
|
||||
|
||||
if (truncation.firstLineExceedsLimit) {
|
||||
// First line at offset exceeds 50KB - tell model to use bash
|
||||
const firstLineSize = formatSize(Buffer.byteLength(selectedContent.split("\n")[0], "utf-8"));
|
||||
outputText = `[Line ${startLineDisplay} is ${firstLineSize}, exceeds ${formatSize(DEFAULT_MAX_BYTES)} limit. Use bash: sed -n '${startLineDisplay}p' ${path} | head -c ${DEFAULT_MAX_BYTES}]`;
|
||||
details = { truncation };
|
||||
} else if (truncation.truncated) {
|
||||
// Truncation occurred - build actionable notice
|
||||
const endLineDisplay = startLineDisplay + truncation.outputLines - 1;
|
||||
const nextOffset = endLineDisplay + 1;
|
||||
|
||||
outputText = truncation.content;
|
||||
|
||||
if (truncation.truncatedBy === "lines") {
|
||||
outputText += `\n\n[Showing lines ${startLineDisplay}-${endLineDisplay} of ${totalFileLines}. Use offset=${nextOffset} to continue]`;
|
||||
} else {
|
||||
outputText += `\n\n[Showing lines ${startLineDisplay}-${endLineDisplay} of ${totalFileLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Use offset=${nextOffset} to continue]`;
|
||||
}
|
||||
details = { truncation };
|
||||
} else if (userLimitedLines !== undefined) {
|
||||
// User specified limit, check if there's more content
|
||||
const linesFromStart = startLine - 1 + userLimitedLines;
|
||||
if (linesFromStart < totalFileLines) {
|
||||
const remaining = totalFileLines - linesFromStart;
|
||||
const nextOffset = startLine + userLimitedLines;
|
||||
|
||||
outputText = truncation.content;
|
||||
outputText += `\n\n[${remaining} more lines in file. Use offset=${nextOffset} to continue]`;
|
||||
} else {
|
||||
outputText = truncation.content;
|
||||
}
|
||||
} else {
|
||||
// No truncation, no user limit exceeded
|
||||
outputText = truncation.content;
|
||||
}
|
||||
|
||||
return {
|
||||
content: [{ type: "text", text: outputText }],
|
||||
details,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function shellEscape(s: string): string {
|
||||
return `'${s.replace(/'/g, "'\\''")}'`;
|
||||
}
|
||||
@@ -1,236 +0,0 @@
|
||||
/**
|
||||
* Shared truncation utilities for tool outputs.
|
||||
*
|
||||
* Truncation is based on two independent limits - whichever is hit first wins:
|
||||
* - Line limit (default: 2000 lines)
|
||||
* - Byte limit (default: 50KB)
|
||||
*
|
||||
* Never returns partial lines (except bash tail truncation edge case).
|
||||
*/
|
||||
|
||||
export const DEFAULT_MAX_LINES = 2000;
|
||||
export const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB
|
||||
|
||||
export interface TruncationResult {
|
||||
/** The truncated content */
|
||||
content: string;
|
||||
/** Whether truncation occurred */
|
||||
truncated: boolean;
|
||||
/** Which limit was hit: "lines", "bytes", or null if not truncated */
|
||||
truncatedBy: "lines" | "bytes" | null;
|
||||
/** Total number of lines in the original content */
|
||||
totalLines: number;
|
||||
/** Total number of bytes in the original content */
|
||||
totalBytes: number;
|
||||
/** Number of complete lines in the truncated output */
|
||||
outputLines: number;
|
||||
/** Number of bytes in the truncated output */
|
||||
outputBytes: number;
|
||||
/** Whether the last line was partially truncated (only for tail truncation edge case) */
|
||||
lastLinePartial: boolean;
|
||||
/** Whether the first line exceeded the byte limit (for head truncation) */
|
||||
firstLineExceedsLimit: boolean;
|
||||
}
|
||||
|
||||
export interface TruncationOptions {
|
||||
/** Maximum number of lines (default: 2000) */
|
||||
maxLines?: number;
|
||||
/** Maximum number of bytes (default: 50KB) */
|
||||
maxBytes?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format bytes as human-readable size.
|
||||
*/
|
||||
export function formatSize(bytes: number): string {
|
||||
if (bytes < 1024) {
|
||||
return `${bytes}B`;
|
||||
} else if (bytes < 1024 * 1024) {
|
||||
return `${(bytes / 1024).toFixed(1)}KB`;
|
||||
} else {
|
||||
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate content from the head (keep first N lines/bytes).
|
||||
* Suitable for file reads where you want to see the beginning.
|
||||
*
|
||||
* Never returns partial lines. If first line exceeds byte limit,
|
||||
* returns empty content with firstLineExceedsLimit=true.
|
||||
*/
|
||||
export function truncateHead(content: string, options: TruncationOptions = {}): TruncationResult {
|
||||
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
|
||||
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
||||
|
||||
const totalBytes = Buffer.byteLength(content, "utf-8");
|
||||
const lines = content.split("\n");
|
||||
const totalLines = lines.length;
|
||||
|
||||
// Check if no truncation needed
|
||||
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
||||
return {
|
||||
content,
|
||||
truncated: false,
|
||||
truncatedBy: null,
|
||||
totalLines,
|
||||
totalBytes,
|
||||
outputLines: totalLines,
|
||||
outputBytes: totalBytes,
|
||||
lastLinePartial: false,
|
||||
firstLineExceedsLimit: false,
|
||||
};
|
||||
}
|
||||
|
||||
// Check if first line alone exceeds byte limit
|
||||
const firstLineBytes = Buffer.byteLength(lines[0], "utf-8");
|
||||
if (firstLineBytes > maxBytes) {
|
||||
return {
|
||||
content: "",
|
||||
truncated: true,
|
||||
truncatedBy: "bytes",
|
||||
totalLines,
|
||||
totalBytes,
|
||||
outputLines: 0,
|
||||
outputBytes: 0,
|
||||
lastLinePartial: false,
|
||||
firstLineExceedsLimit: true,
|
||||
};
|
||||
}
|
||||
|
||||
// Collect complete lines that fit
|
||||
const outputLinesArr: string[] = [];
|
||||
let outputBytesCount = 0;
|
||||
let truncatedBy: "lines" | "bytes" = "lines";
|
||||
|
||||
for (let i = 0; i < lines.length && i < maxLines; i++) {
|
||||
const line = lines[i];
|
||||
const lineBytes = Buffer.byteLength(line, "utf-8") + (i > 0 ? 1 : 0); // +1 for newline
|
||||
|
||||
if (outputBytesCount + lineBytes > maxBytes) {
|
||||
truncatedBy = "bytes";
|
||||
break;
|
||||
}
|
||||
|
||||
outputLinesArr.push(line);
|
||||
outputBytesCount += lineBytes;
|
||||
}
|
||||
|
||||
// If we exited due to line limit
|
||||
if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
|
||||
truncatedBy = "lines";
|
||||
}
|
||||
|
||||
const outputContent = outputLinesArr.join("\n");
|
||||
const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8");
|
||||
|
||||
return {
|
||||
content: outputContent,
|
||||
truncated: true,
|
||||
truncatedBy,
|
||||
totalLines,
|
||||
totalBytes,
|
||||
outputLines: outputLinesArr.length,
|
||||
outputBytes: finalOutputBytes,
|
||||
lastLinePartial: false,
|
||||
firstLineExceedsLimit: false,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate content from the tail (keep last N lines/bytes).
|
||||
* Suitable for bash output where you want to see the end (errors, final results).
|
||||
*
|
||||
* May return partial first line if the last line of original content exceeds byte limit.
|
||||
*/
|
||||
export function truncateTail(content: string, options: TruncationOptions = {}): TruncationResult {
|
||||
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
|
||||
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
||||
|
||||
const totalBytes = Buffer.byteLength(content, "utf-8");
|
||||
const lines = content.split("\n");
|
||||
const totalLines = lines.length;
|
||||
|
||||
// Check if no truncation needed
|
||||
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
||||
return {
|
||||
content,
|
||||
truncated: false,
|
||||
truncatedBy: null,
|
||||
totalLines,
|
||||
totalBytes,
|
||||
outputLines: totalLines,
|
||||
outputBytes: totalBytes,
|
||||
lastLinePartial: false,
|
||||
firstLineExceedsLimit: false,
|
||||
};
|
||||
}
|
||||
|
||||
// Work backwards from the end
|
||||
const outputLinesArr: string[] = [];
|
||||
let outputBytesCount = 0;
|
||||
let truncatedBy: "lines" | "bytes" = "lines";
|
||||
let lastLinePartial = false;
|
||||
|
||||
for (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) {
|
||||
const line = lines[i];
|
||||
const lineBytes = Buffer.byteLength(line, "utf-8") + (outputLinesArr.length > 0 ? 1 : 0); // +1 for newline
|
||||
|
||||
if (outputBytesCount + lineBytes > maxBytes) {
|
||||
truncatedBy = "bytes";
|
||||
// Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,
|
||||
// take the end of the line (partial)
|
||||
if (outputLinesArr.length === 0) {
|
||||
const truncatedLine = truncateStringToBytesFromEnd(line, maxBytes);
|
||||
outputLinesArr.unshift(truncatedLine);
|
||||
outputBytesCount = Buffer.byteLength(truncatedLine, "utf-8");
|
||||
lastLinePartial = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
outputLinesArr.unshift(line);
|
||||
outputBytesCount += lineBytes;
|
||||
}
|
||||
|
||||
// If we exited due to line limit
|
||||
if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
|
||||
truncatedBy = "lines";
|
||||
}
|
||||
|
||||
const outputContent = outputLinesArr.join("\n");
|
||||
const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8");
|
||||
|
||||
return {
|
||||
content: outputContent,
|
||||
truncated: true,
|
||||
truncatedBy,
|
||||
totalLines,
|
||||
totalBytes,
|
||||
outputLines: outputLinesArr.length,
|
||||
outputBytes: finalOutputBytes,
|
||||
lastLinePartial,
|
||||
firstLineExceedsLimit: false,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate a string to fit within a byte limit (from the end).
|
||||
* Handles multi-byte UTF-8 characters correctly.
|
||||
*/
|
||||
function truncateStringToBytesFromEnd(str: string, maxBytes: number): string {
|
||||
const buf = Buffer.from(str, "utf-8");
|
||||
if (buf.length <= maxBytes) {
|
||||
return str;
|
||||
}
|
||||
|
||||
// Start from the end, skip maxBytes back
|
||||
let start = buf.length - maxBytes;
|
||||
|
||||
// Find a valid UTF-8 boundary (start of a character)
|
||||
while (start < buf.length && (buf[start] & 0xc0) === 0x80) {
|
||||
start++;
|
||||
}
|
||||
|
||||
return buf.slice(start).toString("utf-8");
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
import type { AgentTool } from "@mariozechner/pi-agent-core";
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import type { Executor } from "../sandbox.js";
|
||||
|
||||
const writeSchema = Type.Object({
|
||||
label: Type.String({ description: "Brief description of what you're writing (shown to user)" }),
|
||||
path: Type.String({ description: "Path to the file to write (relative or absolute)" }),
|
||||
content: Type.String({ description: "Content to write to the file" }),
|
||||
});
|
||||
|
||||
export function createWriteTool(executor: Executor): AgentTool<typeof writeSchema> {
|
||||
return {
|
||||
name: "write",
|
||||
label: "write",
|
||||
description:
|
||||
"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.",
|
||||
parameters: writeSchema,
|
||||
execute: async (
|
||||
_toolCallId: string,
|
||||
{ path, content }: { label: string; path: string; content: string },
|
||||
signal?: AbortSignal,
|
||||
) => {
|
||||
// Create parent directories and write file using heredoc
|
||||
const dir = path.includes("/") ? path.substring(0, path.lastIndexOf("/")) : ".";
|
||||
|
||||
// Use printf to handle content with special characters, pipe to file
|
||||
// This avoids issues with heredoc and special characters
|
||||
const cmd = `mkdir -p ${shellEscape(dir)} && printf '%s' ${shellEscape(content)} > ${shellEscape(path)}`;
|
||||
|
||||
const result = await executor.exec(cmd, { signal });
|
||||
if (result.code !== 0) {
|
||||
throw new Error(result.stderr || `Failed to write file: ${path}`);
|
||||
}
|
||||
|
||||
return {
|
||||
content: [{ type: "text", text: `Successfully wrote ${content.length} bytes to ${path}` }],
|
||||
details: undefined,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function shellEscape(s: string): string {
|
||||
return `'${s.replace(/'/g, "'\\''")}'`;
|
||||
}
|
||||
Reference in new issue
Block a user