@geminixiang/mama 0.2.0-beta.1 → 0.2.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -58
- package/dist/adapter.d.ts +8 -6
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js.map +1 -1
- package/dist/adapters/discord/bot.d.ts +2 -2
- package/dist/adapters/discord/bot.d.ts.map +1 -1
- package/dist/adapters/discord/bot.js +20 -29
- package/dist/adapters/discord/bot.js.map +1 -1
- package/dist/adapters/discord/context.d.ts.map +1 -1
- package/dist/adapters/discord/context.js +16 -20
- package/dist/adapters/discord/context.js.map +1 -1
- package/dist/adapters/slack/bot.d.ts +11 -4
- package/dist/adapters/slack/bot.d.ts.map +1 -1
- package/dist/adapters/slack/bot.js +199 -73
- package/dist/adapters/slack/bot.js.map +1 -1
- package/dist/adapters/slack/context.d.ts.map +1 -1
- package/dist/adapters/slack/context.js +27 -30
- package/dist/adapters/slack/context.js.map +1 -1
- package/dist/adapters/telegram/bot.d.ts +4 -2
- package/dist/adapters/telegram/bot.d.ts.map +1 -1
- package/dist/adapters/telegram/bot.js +130 -71
- package/dist/adapters/telegram/bot.js.map +1 -1
- package/dist/adapters/telegram/context.d.ts.map +1 -1
- package/dist/adapters/telegram/context.js +9 -95
- package/dist/adapters/telegram/context.js.map +1 -1
- package/dist/adapters/telegram/html.d.ts +3 -0
- package/dist/adapters/telegram/html.d.ts.map +1 -0
- package/dist/adapters/telegram/html.js +98 -0
- package/dist/adapters/telegram/html.js.map +1 -0
- package/dist/agent.d.ts +3 -11
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +63 -70
- package/dist/agent.js.map +1 -1
- package/dist/bindings.d.ts +1 -20
- package/dist/bindings.d.ts.map +1 -1
- package/dist/bindings.js +1 -21
- package/dist/bindings.js.map +1 -1
- package/dist/config.d.ts +7 -27
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +77 -63
- package/dist/config.js.map +1 -1
- package/dist/context.d.ts +2 -2
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +2 -2
- package/dist/context.js.map +1 -1
- package/dist/events.d.ts +11 -6
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +33 -13
- package/dist/events.js.map +1 -1
- package/dist/execution-resolver.d.ts.map +1 -1
- package/dist/execution-resolver.js +1 -3
- package/dist/execution-resolver.js.map +1 -1
- package/dist/instrument.d.ts.map +1 -1
- package/dist/instrument.js +5 -11
- package/dist/instrument.js.map +1 -1
- package/dist/link-server.d.ts +2 -1
- package/dist/link-server.d.ts.map +1 -1
- package/dist/link-server.js +62 -2
- package/dist/link-server.js.map +1 -1
- package/dist/login.d.ts +1 -1
- package/dist/login.d.ts.map +1 -1
- package/dist/login.js +1 -1
- package/dist/login.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +96 -112
- package/dist/main.js.map +1 -1
- package/dist/provisioner.d.ts +0 -41
- package/dist/provisioner.d.ts.map +1 -1
- package/dist/provisioner.js +0 -45
- package/dist/provisioner.js.map +1 -1
- package/dist/sandbox/host.d.ts +0 -2
- package/dist/sandbox/host.d.ts.map +1 -1
- package/dist/sandbox/host.js +1 -5
- package/dist/sandbox/host.js.map +1 -1
- package/dist/sentry.d.ts.map +1 -1
- package/dist/sentry.js +2 -0
- package/dist/sentry.js.map +1 -1
- package/dist/session-store.d.ts +1 -1
- package/dist/session-store.d.ts.map +1 -1
- package/dist/session-store.js +5 -9
- package/dist/session-store.js.map +1 -1
- package/dist/tools/event.d.ts +1 -0
- package/dist/tools/event.d.ts.map +1 -1
- package/dist/tools/event.js +6 -5
- package/dist/tools/event.js.map +1 -1
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +2 -2
- package/dist/tools/index.js.map +1 -1
- package/dist/ui-copy.d.ts +1 -0
- package/dist/ui-copy.d.ts.map +1 -1
- package/dist/ui-copy.js +3 -0
- package/dist/ui-copy.js.map +1 -1
- package/dist/vault-routing.d.ts +1 -2
- package/dist/vault-routing.d.ts.map +1 -1
- package/dist/vault-routing.js +1 -7
- package/dist/vault-routing.js.map +1 -1
- package/package.json +1 -1
- package/dist/vault.test.d.ts +0 -2
- package/dist/vault.test.d.ts.map +0 -1
- package/dist/vault.test.js +0 -67
- package/dist/vault.test.js.map +0 -1
package/dist/agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,WAAW,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAKnF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAiC,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AACjF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAiB/C,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IACrC,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,WAAW;IAC1B,GAAG,CACD,OAAO,EAAE,WAAW,EACpB,WAAW,EAAE,mBAAmB,EAChC,QAAQ,EAAE,YAAY,GACrB,OAAO,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC1D,KAAK,IAAI,IAAI,CAAC;IACd,6DAA6D;IAC7D,cAAc,IAAI;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC;CACrE;AA4WD;;;;;;GAMG;AACH,wBAAsB,YAAY,CAChC,aAAa,EAAE,aAAa,EAC5B,UAAU,EAAE,MAAM,EAClB,cAAc,EAAE,MAAM,EACtB,eAAe,EAAE,MAAM,EACvB,YAAY,EAAE,MAAM,EACpB,YAAY,CAAC,EAAE,YAAY,EAC3B,YAAY,CAAC,EAAE,gBAAgB,EAC/B,WAAW,CAAC,EAAE,sBAAsB,EACpC,QAAQ,CAAC,EAAE,MAAM,GAChB,OAAO,CAAC,WAAW,CAAC,CAyxBtB","sourcesContent":["import { Agent, type AgentEvent } from \"@mariozechner/pi-agent-core\";\nimport { getModel, type ImageContent } from \"@mariozechner/pi-ai\";\nimport {\n AgentSession,\n AuthStorage,\n convertToLlm,\n DefaultResourceLoader,\n formatSkillsForPrompt,\n getAgentDir,\n loadSkillsFromDir,\n ModelRegistry,\n SessionManager,\n type Skill,\n} from \"@mariozechner/pi-coding-agent\";\nimport { existsSync, readFileSync } from \"fs\";\nimport { mkdir, readFile, writeFile } from \"fs/promises\";\nimport { homedir } from \"os\";\nimport { join } from \"path\";\nimport type { ChatMessage, ChatResponseContext, PlatformInfo } from \"./adapter.js\";\nimport { loadAgentConfig } from \"./config.js\";\nimport { createMamaSettingsManager, syncLogToSessionManager } from \"./context.js\";\nimport { ActorExecutionResolver } from \"./execution-resolver.js\";\nimport * as log from \"./log.js\";\nimport type { UserBindingStore } from \"./bindings.js\";\nimport type { DockerContainerManager } from \"./provisioner.js\";\nimport { createExecutor, type Executor, type SandboxConfig } from \"./sandbox.js\";\nimport type { VaultManager } from \"./vault.js\";\nimport { addLifecycleBreadcrumb, metricAttributes } from \"./sentry.js\";\nimport {\n createManagedSessionFileAtPath,\n extractSessionSuffix,\n extractSessionUuid,\n forkThreadSessionFile,\n getChannelSessionDir,\n getThreadSessionFile,\n openManagedSession,\n resolveChannelSessionFile,\n resolveManagedSessionFile,\n tryResolveThreadSession,\n} from \"./session-store.js\";\nimport { createMamaTools } from \"./tools/index.js\";\nimport * as Sentry from \"@sentry/node\";\n\nexport interface PendingMessage {\n userName: string;\n text: string;\n attachments: { localPath: string }[];\n timestamp: number;\n}\n\nexport interface AgentRunner {\n run(\n message: ChatMessage,\n responseCtx: ChatResponseContext,\n platform: PlatformInfo,\n ): Promise<{ stopReason: string; errorMessage?: string }>;\n abort(): void;\n /** Get current step info (tool name, label) for debugging */\n getCurrentStep(): { toolName?: string; label?: string } | undefined;\n}\n\nconst IMAGE_MIME_TYPES: Record<string, string> = {\n jpg: \"image/jpeg\",\n jpeg: \"image/jpeg\",\n png: \"image/png\",\n gif: \"image/gif\",\n webp: \"image/webp\",\n};\n\nfunction getImageMimeType(filename: string): string | undefined {\n return IMAGE_MIME_TYPES[filename.toLowerCase().split(\".\").pop() || \"\"];\n}\n\nasync function getMemory(conversationDir: string): Promise<string> {\n const parts: string[] = [];\n\n // Read workspace-level memory (shared across all conversations)\n const workspaceMemoryPath = join(conversationDir, \"..\", \"MEMORY.md\");\n if (existsSync(workspaceMemoryPath)) {\n try {\n const content = (await readFile(workspaceMemoryPath, \"utf-8\")).trim();\n if (content) {\n parts.push(`### Global Workspace Memory\\n${content}`);\n }\n } catch (error) {\n log.logWarning(\"Failed to read workspace memory\", `${workspaceMemoryPath}: ${error}`);\n }\n }\n\n // Read conversation-specific memory\n const conversationMemoryPath = join(conversationDir, \"MEMORY.md\");\n if (existsSync(conversationMemoryPath)) {\n try {\n const content = (await readFile(conversationMemoryPath, \"utf-8\")).trim();\n if (content) {\n parts.push(`### Conversation-Specific Memory\\n${content}`);\n }\n } catch (error) {\n log.logWarning(\"Failed to read conversation memory\", `${conversationMemoryPath}: ${error}`);\n }\n }\n\n if (parts.length === 0) {\n return \"(no working memory yet)\";\n }\n\n return parts.join(\"\\n\\n\");\n}\n\nfunction loadMamaSkills(conversationDir: string, workspacePath: string): Skill[] {\n const skillMap = new Map<string, Skill>();\n\n // conversationDir is the host path (e.g., /Users/.../data/C0A34FL8PMH)\n // hostWorkspacePath is the parent directory on host\n // workspacePath is the container path (e.g., /workspace)\n const hostWorkspacePath = join(conversationDir, \"..\");\n\n // Helper to translate host paths to container paths\n const translatePath = (hostPath: string): string => {\n if (hostPath.startsWith(hostWorkspacePath)) {\n return workspacePath + hostPath.slice(hostWorkspacePath.length);\n }\n return hostPath;\n };\n\n // Load workspace-level skills (global)\n const workspaceSkillsDir = join(hostWorkspacePath, \"skills\");\n for (const skill of loadSkillsFromDir({ dir: workspaceSkillsDir, source: \"workspace\" }).skills) {\n // Translate paths to container paths for system prompt\n skill.filePath = translatePath(skill.filePath);\n skill.baseDir = translatePath(skill.baseDir);\n skillMap.set(skill.name, skill);\n }\n\n // Load conversation-specific skills (override workspace skills on collision)\n const conversationSkillsDir = join(conversationDir, \"skills\");\n for (const skill of loadSkillsFromDir({ dir: conversationSkillsDir, source: \"channel\" }).skills) {\n skill.filePath = translatePath(skill.filePath);\n skill.baseDir = translatePath(skill.baseDir);\n skillMap.set(skill.name, skill);\n }\n\n return Array.from(skillMap.values());\n}\n\nfunction buildSystemPrompt(\n workspacePath: string,\n conversationId: string,\n currentUserId: string | undefined,\n memory: string,\n sandboxConfig: SandboxConfig,\n platform: PlatformInfo,\n skills: Skill[],\n): string {\n const conversationPath = `${workspacePath}/${conversationId}`;\n const isContainer = sandboxConfig.type === \"container\" || sandboxConfig.type === \"image\";\n const isFirecracker = sandboxConfig.type === \"firecracker\";\n\n // Format platform conversation mappings\n const channelMappings =\n platform.channels.length > 0\n ? platform.channels.map((c) => `${c.id}\\t#${c.name}`).join(\"\\n\")\n : \"(no channels loaded)\";\n\n // Format user mappings\n const userMappings =\n platform.users.length > 0\n ? platform.users.map((u) => `${u.id}\\t@${u.userName}\\t${u.displayName}`).join(\"\\n\")\n : \"(no users loaded)\";\n\n const envDescription = isContainer\n ? `You are running inside a container (Docker runtime, Alpine Linux).\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with: apk add <package>\n- Your changes persist across sessions`\n : isFirecracker\n ? `You are running inside a Firecracker microVM.\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with: apt-get install <package> (Debian-based)\n- Your changes persist across sessions`\n : `You are running directly on the host machine.\n- Bash working directory: ${process.cwd()}\n- Be careful with system modifications`;\n\n return `You are mama, a ${platform.name} bot assistant. Be concise. No emojis.\n\n## Context\n- For current date/time, use: date\n- You have access to previous conversation context including tool results from prior turns.\n- For older history beyond your context, search log.jsonl (contains user messages and your final responses, but not tool results).\n- User messages include a \\`[in-thread:TS]\\` marker when sent from within a Slack thread (TS is the root message timestamp). Without this marker, the message is a top-level channel message.\n\n${platform.formattingGuide}\n\n## Platform IDs\nChannels: ${channelMappings}\n\nUsers: ${userMappings}\n\nWhen mentioning users, use <@username> format (e.g., <@mario>).\n\n## Environment\n${envDescription}\n\n## Workspace Layout\n${workspacePath}/\n├── MEMORY.md # Global memory (all channels)\n├── skills/ # Global CLI tools you create\n└── ${conversationId}/ # This conversation\n ├── MEMORY.md # Conversation-specific memory\n ├── log.jsonl # Message history (no tool results)\n ├── attachments/ # User-shared files\n ├── scratch/ # Your working directory\n └── skills/ # Conversation-specific tools\n\n## Skills (Custom CLI Tools)\nYou can create reusable CLI tools for recurring tasks (email, APIs, data processing, etc.).\n\n### Creating Skills\nStore in \\`${workspacePath}/skills/<name>/\\` (global) or \\`${conversationPath}/skills/<name>/\\` (conversation-specific).\nEach skill directory needs a \\`SKILL.md\\` with YAML frontmatter:\n\n\\`\\`\\`markdown\n---\nname: skill-name\ndescription: Short description of what this skill does\n---\n\n# Skill Name\n\nUsage instructions, examples, etc.\nScripts are in: {baseDir}/\n\\`\\`\\`\n\n\\`name\\` and \\`description\\` are required. Use \\`{baseDir}\\` as placeholder for the skill's directory path.\n\n### Available Skills\n${skills.length > 0 ? formatSkillsForPrompt(skills) : \"(no skills installed yet)\"}\n\n## Events\nYou can schedule events that wake you up at specific times or when external things happen. Events are JSON files in \\`${workspacePath}/events/\\`.\n\n### Event Types\n\n**Immediate** - Triggers as soon as harness sees the file. Use in scripts/webhooks to signal external events.\n\\`\\`\\`json\n{\"type\": \"immediate\", \"platform\": \"${platform.name}\", \"channelId\": \"${conversationId}\", \"userId\": \"<requester userId>\", \"text\": \"New GitHub issue opened\"}\n\\`\\`\\`\n\n**One-shot** - Triggers once at a specific time. Use for reminders.\n\\`\\`\\`json\n{\"type\": \"one-shot\", \"platform\": \"${platform.name}\", \"channelId\": \"${conversationId}\", \"userId\": \"<requester userId>\", \"text\": \"Remind Mario about dentist\", \"at\": \"2025-12-15T09:00:00+01:00\"}\n\\`\\`\\`\n\n**Periodic** - Triggers on a cron schedule. Use for recurring tasks.\n\\`\\`\\`json\n{\"type\": \"periodic\", \"platform\": \"${platform.name}\", \"channelId\": \"${conversationId}\", \"userId\": \"<requester userId>\", \"text\": \"Check inbox and summarize\", \"schedule\": \"0 9 * * 1-5\", \"timezone\": \"${Intl.DateTimeFormat().resolvedOptions().timeZone}\"}\n\\`\\`\\`\n\nSet \\`userId\\` to the platform userId of whoever asked for the event (look it up in the user mappings above). When the event fires, tool execution will route to the sandbox vault selection for that user so the right credentials are available. In shared container mode, all events use the container's single shared vault.\n\n### Cron Format\n\\`minute hour day-of-month month day-of-week\\`\n- \\`0 9 * * *\\` = daily at 9:00\n- \\`0 9 * * 1-5\\` = weekdays at 9:00\n- \\`30 14 * * 1\\` = Mondays at 14:30\n- \\`0 0 1 * *\\` = first of each month at midnight\n\n### Timezones\nAll \\`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}.\n\n### Platform Routing\nSet \\`platform\\` to the target bot platform (\\`${platform.name}\\` for this conversation). When only one platform is running, omitting \\`platform\\` is allowed for backward compatibility, but include it by default to avoid ambiguity.\n\n### Creating Events\nPrefer the \\`event\\` tool. It automatically writes to the correct events directory and fills the current \\`platform\\`, \\`channelId\\`, and requester \\`userId\\`.\nDo not use \\`bash\\` or \\`write\\` to hand-create JSON files in \\`/events\\` unless the user explicitly asks for manual file editing.\n\nCurrent conversation defaults:\n- \\`platform\\`: \\`${platform.name}\\`\n- \\`channelId\\`: \\`${conversationId}\\`\n- \\`userId\\`: \\`${currentUserId ?? \"unknown\"}\\`\n\nManual file creation is fallback only:\nUse unique filenames to avoid overwriting existing events. Include a timestamp or random suffix:\n\\`\\`\\`bash\ncat > ${workspacePath}/events/dentist-reminder-$(date +%s).json << 'EOF'\n{\"type\": \"one-shot\", \"platform\": \"${platform.name}\", \"channelId\": \"${conversationId}\", \"userId\": \"<requester userId>\", \"text\": \"Dentist tomorrow\", \"at\": \"2025-12-14T09:00:00+01:00\"}\nEOF\n\\`\\`\\`\nOr check if file exists first before creating.\n\n### Managing Events\n- List: \\`ls ${workspacePath}/events/\\`\n- View: \\`cat ${workspacePath}/events/foo.json\\`\n- Delete/cancel: \\`rm ${workspacePath}/events/foo.json\\`\n\n### When Events Trigger\nYou receive a message like:\n\\`\\`\\`\n[EVENT:dentist-reminder.json:one-shot:2025-12-14T09:00:00+01:00] Dentist tomorrow\n\\`\\`\\`\nImmediate and one-shot events auto-delete after triggering. Periodic events persist until you delete them.\n\n### Silent Completion\nFor periodic events where there's nothing to report, respond with just \\`[SILENT]\\` (no other text). This deletes the status message and posts nothing to the platform. Use this to avoid spamming the channel when periodic checks find nothing actionable.\n\n### Debouncing\nWhen 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.\n\n### Limits\nMaximum 5 events can be queued. Don't create excessive immediate or periodic events.\n\n## Memory\nWrite to MEMORY.md files to persist context across conversations.\n- Global (${workspacePath}/MEMORY.md): skills, preferences, project info\n- Conversation (${conversationPath}/MEMORY.md): conversation-specific decisions, ongoing work\nUpdate when you learn something important or when asked to remember something.\n\n### Current Memory\n${memory}\n\n## System Configuration Log\nMaintain ${workspacePath}/SYSTEM.md to log all environment modifications:\n- Installed packages (apk add, npm install, pip install)\n- Environment variables set\n- Config files modified (~/.gitconfig, cron jobs, etc.)\n- Skill dependencies installed\n\nUpdate this file whenever you modify the environment. On fresh container, read it first to restore your setup.\n\n## Log Queries (for older history)\nFormat: \\`{\"date\":\"...\",\"ts\":\"...\",\"user\":\"...\",\"userName\":\"...\",\"text\":\"...\",\"isBot\":false}\\`\nThe log contains user messages and your final responses (not tool calls/results).\n${isContainer ? \"Install jq: apk add jq\" : \"\"}\n${isFirecracker ? \"Install jq: apt-get install jq\" : \"\"}\n\n\\`\\`\\`bash\n# Recent messages\ntail -30 log.jsonl | jq -c '{date: .date[0:19], user: (.userName // .user), text}'\n\n# Search for specific topic\ngrep -i \"topic\" log.jsonl | jq -c '{date: .date[0:19], user: (.userName // .user), text}'\n\n# Messages from specific user\ngrep '\"userName\":\"mario\"' log.jsonl | tail -20 | jq -c '{date: .date[0:19], text}'\n\\`\\`\\`\n\n## Tools\n- bash: Run shell commands (primary tool). Install packages as needed.\n- read: Read files\n- write: Create/overwrite files\n- edit: Surgical file edits\n- attach: Share files to the platform\n\nEach tool requires a \"label\" parameter (shown to user).\n`;\n}\n\nfunction truncate(text: string, maxLen: number): string {\n if (text.length <= maxLen) return text;\n return `${text.substring(0, maxLen - 3)}...`;\n}\n\nfunction extractToolResultText(result: unknown): string {\n if (typeof result === \"string\") {\n return result;\n }\n\n if (\n result &&\n typeof result === \"object\" &&\n \"content\" in result &&\n Array.isArray((result as { content: unknown }).content)\n ) {\n const content = (result as { content: Array<{ type: string; text?: string }> }).content;\n const textParts: string[] = [];\n for (const part of content) {\n if (part.type === \"text\" && part.text) {\n textParts.push(part.text);\n }\n }\n if (textParts.length > 0) {\n return textParts.join(\"\\n\");\n }\n }\n\n return JSON.stringify(result);\n}\n\nfunction formatToolArgsForSlack(_toolName: string, args: Record<string, unknown>): string {\n const lines: string[] = [];\n\n for (const [key, value] of Object.entries(args)) {\n if (key === \"label\") continue;\n\n if (key === \"path\" && typeof value === \"string\") {\n const offset = args.offset as number | undefined;\n const limit = args.limit as number | undefined;\n if (offset !== undefined && limit !== undefined) {\n lines.push(`${value}:${offset}-${offset + limit}`);\n } else {\n lines.push(value);\n }\n continue;\n }\n\n if (key === \"offset\" || key === \"limit\") continue;\n\n if (typeof value === \"string\") {\n lines.push(value);\n } else {\n lines.push(JSON.stringify(value));\n }\n }\n\n return lines.join(\"\\n\");\n}\n\n// ============================================================================\n// Agent runner\n// ============================================================================\n\n/**\n * Create a new AgentRunner for a conversation.\n * Sets up the session and subscribes to events once.\n *\n * Runner caching is handled by the caller (conversationStates in main.ts).\n * This is a stateless factory function.\n */\nexport async function createRunner(\n sandboxConfig: SandboxConfig,\n sessionKey: string,\n conversationId: string,\n conversationDir: string,\n workspaceDir: string,\n vaultManager?: VaultManager,\n bindingStore?: UserBindingStore,\n provisioner?: DockerContainerManager,\n stateDir?: string,\n): Promise<AgentRunner> {\n const agentConfig = loadAgentConfig(stateDir ?? workspaceDir);\n\n // Initialize logger with settings from config\n log.initLogger({\n logFormat: agentConfig.logFormat,\n logLevel: agentConfig.logLevel,\n });\n\n const executionResolver =\n vaultManager &&\n (vaultManager.isEnabled() ||\n !!bindingStore ||\n sandboxConfig.type === \"image\" ||\n sandboxConfig.type === \"container\")\n ? new ActorExecutionResolver(sandboxConfig, vaultManager, bindingStore, provisioner)\n : undefined;\n let activeExecutor: Executor =\n executionResolver !== undefined\n ? createExecutor({ type: \"host\" })\n : createExecutor(sandboxConfig);\n const executor: Executor = {\n exec(command, options) {\n return activeExecutor.exec(command, options);\n },\n getWorkspacePath(hostPath) {\n return activeExecutor.getWorkspacePath(hostPath);\n },\n getSandboxConfig() {\n return activeExecutor.getSandboxConfig();\n },\n };\n const workspaceBase = conversationDir.replace(`/${conversationId}`, \"\");\n // Compute workspace path from the current executor. This may change per run.\n const getWorkspacePath = () => executor.getWorkspacePath(workspaceBase);\n let workspacePath = getWorkspacePath();\n\n // Create tools (per-runner, with per-runner upload function setter)\n const { tools, setUploadFunction, setEventContext } = createMamaTools(executor, workspaceDir);\n\n // Resolve model from config\n // Use 'as any' cast because agentConfig.provider/model are plain strings,\n // while getModel() has constrained generic types for known providers.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const model = (getModel as any)(agentConfig.provider, agentConfig.model);\n\n // Initial system prompt (will be updated each run with fresh memory/channels/users/skills)\n const memory = await getMemory(conversationDir);\n const skills = loadMamaSkills(conversationDir, workspacePath);\n const emptyPlatform: PlatformInfo = {\n name: \"slack\",\n formattingGuide: \"\",\n channels: [],\n users: [],\n };\n const systemPrompt = buildSystemPrompt(\n workspacePath,\n conversationId,\n undefined,\n memory,\n sandboxConfig,\n emptyPlatform,\n skills,\n );\n\n // Create session manager and settings manager.\n // Top-level conversation sessions use {conversationDir}/sessions/current.\n // Thread sessions use fixed files: {conversationDir}/sessions/{threadTs}.jsonl.\n const sessionDir = getChannelSessionDir(conversationDir);\n const isThread = sessionKey.includes(\":\");\n\n let sessionManager!: SessionManager;\n let sessionFile!: string;\n\n if (isThread) {\n const threadFile = getThreadSessionFile(conversationDir, sessionKey);\n const existing = tryResolveThreadSession(threadFile);\n if (existing) {\n sessionFile = existing;\n sessionManager = openManagedSession(sessionFile, sessionDir, conversationDir);\n } else {\n const conversationSource = resolveChannelSessionFile(conversationDir);\n if (conversationSource) {\n try {\n sessionFile = forkThreadSessionFile(conversationSource, threadFile, conversationDir);\n sessionManager = openManagedSession(sessionFile, sessionDir, conversationDir);\n } catch {\n sessionFile = createManagedSessionFileAtPath(threadFile, conversationDir);\n sessionManager = openManagedSession(sessionFile, sessionDir, conversationDir);\n }\n } else {\n sessionFile = createManagedSessionFileAtPath(threadFile, conversationDir);\n sessionManager = openManagedSession(sessionFile, sessionDir, conversationDir);\n }\n }\n } else {\n // Top-level conversation session: resolve the current session file.\n sessionFile = resolveManagedSessionFile(sessionDir, conversationDir);\n sessionManager = openManagedSession(sessionFile, sessionDir, conversationDir);\n }\n const sessionUuid = extractSessionUuid(sessionFile);\n // Used for Slack thread filtering — for non-Slack platforms this is effectively a no-op\n const rootTs = extractSessionSuffix(sessionKey);\n const settingsManager = createMamaSettingsManager(join(conversationDir, \"..\"));\n\n // Create AuthStorage and ModelRegistry\n // Auth stored outside workspace so agent can't access it\n const authStorage = AuthStorage.create(join(homedir(), \".pi\", \"mama\", \"auth.json\"));\n const modelRegistry = ModelRegistry.create(authStorage);\n\n // Create agent\n const agent = new Agent({\n initialState: {\n systemPrompt,\n model,\n thinkingLevel:\n (agentConfig.thinkingLevel as \"off\" | \"low\" | \"medium\" | \"high\" | undefined) ?? \"off\",\n tools,\n },\n convertToLlm,\n getApiKey: async () => {\n const key = await modelRegistry.getApiKeyForProvider(model.provider);\n if (!key)\n throw new Error(\n `No API key for provider \"${model.provider}\". Set the appropriate environment variable or configure via auth.json`,\n );\n return key;\n },\n });\n\n // Load existing messages\n const loadedSession = sessionManager.buildSessionContext();\n if (loadedSession.messages.length > 0) {\n agent.state.messages = loadedSession.messages;\n log.logInfo(\n `[${conversationId}] Loaded ${loadedSession.messages.length} messages from session file`,\n );\n }\n\n // Load extensions, skills, prompts, themes via DefaultResourceLoader\n // This reads ~/.pi/agent/settings.json (packages, extensions enable/disable)\n // and discovers resources from standard locations + npm/git packages.\n const resourceLoader = new DefaultResourceLoader({\n cwd: workspaceDir,\n agentDir: getAgentDir(),\n systemPrompt,\n });\n try {\n await resourceLoader.reload();\n const extResult = resourceLoader.getExtensions();\n if (extResult.errors.length > 0) {\n for (const err of extResult.errors) {\n log.logWarning(`[${conversationId}] Extension load error: ${err.path}`, err.error);\n }\n }\n log.logInfo(\n `[${conversationId}] Loaded ${extResult.extensions.length} extension(s): ${extResult.extensions.map((e) => e.path).join(\", \")}`,\n );\n } catch (error) {\n log.logWarning(`[${conversationId}] Failed to load resources`, String(error));\n }\n\n const baseToolsOverride = Object.fromEntries(tools.map((tool) => [tool.name, tool]));\n\n // Create AgentSession wrapper\n const session = new AgentSession({\n agent,\n sessionManager,\n settingsManager,\n cwd: workspaceDir,\n modelRegistry,\n resourceLoader,\n baseToolsOverride,\n });\n\n // Mutable per-run state - event handler references this\n const runState = {\n responseCtx: null as ChatResponseContext | null,\n logCtx: null as {\n conversationId: string;\n userName?: string;\n conversationName?: string;\n sessionId?: string;\n } | null,\n queue: null as {\n enqueue(fn: () => Promise<void>, errorContext: string): void;\n enqueueMessage(\n text: string,\n target: \"main\" | \"thread\",\n errorContext: string,\n doLog?: boolean,\n ): void;\n } | null,\n pendingTools: new Map<string, { toolName: string; args: unknown; startTime: number }>(),\n totalUsage: {\n input: 0,\n output: 0,\n cacheRead: 0,\n cacheWrite: 0,\n cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n },\n llmCallCount: 0,\n stopReason: \"stop\",\n errorMessage: undefined as string | undefined,\n };\n\n // Subscribe to events ONCE\n session.subscribe(async (event) => {\n // Skip if no active run\n if (!runState.responseCtx || !runState.logCtx || !runState.queue) return;\n\n const { responseCtx, logCtx, queue, pendingTools } = runState;\n const baseAttrs = { channel_id: logCtx.conversationId, session_id: logCtx.sessionId };\n\n if (event.type === \"tool_execution_start\") {\n const agentEvent = event as AgentEvent & { type: \"tool_execution_start\" };\n const args = agentEvent.args as { label?: string };\n const label = args.label || agentEvent.toolName;\n\n pendingTools.set(agentEvent.toolCallId, {\n toolName: agentEvent.toolName,\n args: agentEvent.args,\n startTime: Date.now(),\n });\n addLifecycleBreadcrumb(\"agent.tool.started\", {\n tool: agentEvent.toolName,\n ...baseAttrs,\n });\n\n log.logToolStart(\n logCtx,\n agentEvent.toolName,\n label,\n agentEvent.args as Record<string, unknown>,\n );\n // Tool labels are omitted from the main message to reduce Slack noise.\n // Tool execution details are still posted to the thread (see tool_execution_end).\n } else if (event.type === \"tool_execution_end\") {\n const agentEvent = event as AgentEvent & { type: \"tool_execution_end\" };\n const resultStr = extractToolResultText(agentEvent.result);\n const pending = pendingTools.get(agentEvent.toolCallId);\n pendingTools.delete(agentEvent.toolCallId);\n\n const durationMs = pending ? Date.now() - pending.startTime : 0;\n\n Sentry.metrics.count(\"agent.tool.calls\", 1, {\n attributes: metricAttributes({\n tool: agentEvent.toolName,\n error: String(agentEvent.isError),\n ...baseAttrs,\n }),\n });\n Sentry.metrics.distribution(\"agent.tool.duration\", durationMs, {\n unit: \"millisecond\",\n attributes: metricAttributes({\n tool: agentEvent.toolName,\n ...baseAttrs,\n }),\n });\n addLifecycleBreadcrumb(\"agent.tool.completed\", {\n tool: agentEvent.toolName,\n error: agentEvent.isError,\n duration_ms: durationMs,\n ...baseAttrs,\n });\n\n if (agentEvent.isError) {\n log.logToolError(logCtx, agentEvent.toolName, durationMs, resultStr);\n } else {\n log.logToolSuccess(logCtx, agentEvent.toolName, durationMs, resultStr);\n }\n\n // Post args + result to thread\n const label = pending?.args ? (pending.args as { label?: string }).label : undefined;\n const argsFormatted = pending\n ? formatToolArgsForSlack(agentEvent.toolName, pending.args as Record<string, unknown>)\n : \"(args not found)\";\n const duration = (durationMs / 1000).toFixed(1);\n let threadMessage = `*${agentEvent.isError ? \"✗\" : \"✓\"} ${agentEvent.toolName}*`;\n if (label) threadMessage += `: ${label}`;\n threadMessage += ` (${duration}s)\\n`;\n if (argsFormatted) threadMessage += `\\`\\`\\`\\n${argsFormatted}\\n\\`\\`\\`\\n`;\n threadMessage += `*Result:*\\n\\`\\`\\`\\n${resultStr}\\n\\`\\`\\``;\n\n // Only post thread details for tools with meaningful output (bash, attach).\n // Skip read/write/edit to reduce Slack noise — their results are in the log.\n const quietTools = new Set([\"read\", \"write\", \"edit\"]);\n if (!quietTools.has(agentEvent.toolName)) {\n queue.enqueueMessage(threadMessage, \"thread\", \"tool result thread\", false);\n }\n\n if (agentEvent.isError) {\n queue.enqueue(\n () => responseCtx.respond(`_Error: ${truncate(resultStr, 200)}_`),\n \"tool error\",\n );\n }\n } else if (event.type === \"message_start\") {\n const agentEvent = event as AgentEvent & { type: \"message_start\" };\n if (agentEvent.message.role === \"assistant\") {\n runState.llmCallCount += 1;\n addLifecycleBreadcrumb(\"agent.llm.call.started\", {\n call_index: runState.llmCallCount,\n provider: model.provider,\n model: agentConfig.model,\n ...baseAttrs,\n });\n log.logResponseStart(logCtx);\n }\n } else if (event.type === \"message_end\") {\n const agentEvent = event as AgentEvent & { type: \"message_end\" };\n if (agentEvent.message.role === \"assistant\") {\n const assistantMsg = agentEvent.message as any;\n\n if (assistantMsg.stopReason) {\n runState.stopReason = assistantMsg.stopReason;\n }\n if (assistantMsg.errorMessage) {\n runState.errorMessage = assistantMsg.errorMessage;\n }\n\n if (assistantMsg.usage) {\n runState.totalUsage.input += assistantMsg.usage.input;\n runState.totalUsage.output += assistantMsg.usage.output;\n runState.totalUsage.cacheRead += assistantMsg.usage.cacheRead;\n runState.totalUsage.cacheWrite += assistantMsg.usage.cacheWrite;\n runState.totalUsage.cost.input += assistantMsg.usage.cost.input;\n runState.totalUsage.cost.output += assistantMsg.usage.cost.output;\n runState.totalUsage.cost.cacheRead += assistantMsg.usage.cost.cacheRead;\n runState.totalUsage.cost.cacheWrite += assistantMsg.usage.cost.cacheWrite;\n runState.totalUsage.cost.total += assistantMsg.usage.cost.total;\n\n // Per-turn LLM metrics\n const llmAttributes = metricAttributes({\n provider: model.provider,\n model: agentConfig.model,\n ...baseAttrs,\n stop_reason: assistantMsg.stopReason,\n error: Boolean(assistantMsg.errorMessage),\n });\n Sentry.metrics.count(\"agent.llm.calls\", 1, { attributes: llmAttributes });\n Sentry.metrics.distribution(\"agent.llm.tokens_in\", assistantMsg.usage.input, {\n attributes: llmAttributes,\n });\n Sentry.metrics.distribution(\"agent.llm.tokens_out\", assistantMsg.usage.output, {\n attributes: llmAttributes,\n });\n if (assistantMsg.usage.cacheRead > 0) {\n Sentry.metrics.distribution(\"agent.llm.cache_read\", assistantMsg.usage.cacheRead, {\n attributes: llmAttributes,\n });\n }\n if (assistantMsg.usage.cacheWrite > 0) {\n Sentry.metrics.distribution(\"agent.llm.cache_write\", assistantMsg.usage.cacheWrite, {\n attributes: llmAttributes,\n });\n }\n Sentry.metrics.distribution(\"agent.llm.cost_per_turn\", assistantMsg.usage.cost.total, {\n attributes: llmAttributes,\n });\n addLifecycleBreadcrumb(\"agent.llm.call.completed\", {\n call_index: runState.llmCallCount,\n provider: model.provider,\n model: agentConfig.model,\n stop_reason: assistantMsg.stopReason,\n error: Boolean(assistantMsg.errorMessage),\n input_tokens: assistantMsg.usage.input,\n output_tokens: assistantMsg.usage.output,\n cost_total_usd: assistantMsg.usage.cost.total,\n });\n }\n\n const content = agentEvent.message.content;\n const thinkingParts: string[] = [];\n const textParts: string[] = [];\n for (const part of content) {\n if (part.type === \"thinking\") {\n thinkingParts.push((part as any).thinking);\n } else if (part.type === \"text\") {\n textParts.push((part as any).text);\n }\n }\n\n const text = textParts.join(\"\\n\");\n\n for (const thinking of thinkingParts) {\n log.logThinking(logCtx, thinking);\n queue.enqueueMessage(`_${thinking}_`, \"main\", \"thinking main\");\n queue.enqueueMessage(`_${thinking}_`, \"thread\", \"thinking thread\", false);\n }\n\n if (text.trim()) {\n log.logResponse(logCtx, text);\n queue.enqueueMessage(text, \"main\", \"response main\");\n // Only overflow to thread for texts that will be truncated in main\n if (text.length > SLACK_MAX_LENGTH) {\n queue.enqueueMessage(text, \"thread\", \"response thread\", false);\n }\n }\n }\n } else if (event.type === \"compaction_start\") {\n log.logInfo(`Auto-compaction started (reason: ${(event as any).reason})`);\n queue.enqueue(() => responseCtx.respond(\"_Compacting context..._\"), \"compaction start\");\n } else if (event.type === \"compaction_end\") {\n const compEvent = event as any;\n if (compEvent.result) {\n log.logInfo(`Auto-compaction complete: ${compEvent.result.tokensBefore} tokens compacted`);\n } else if (compEvent.aborted) {\n log.logInfo(\"Auto-compaction aborted\");\n }\n } else if (event.type === \"auto_retry_start\") {\n const retryEvent = event as any;\n log.logWarning(\n `Retrying (${retryEvent.attempt}/${retryEvent.maxAttempts})`,\n retryEvent.errorMessage,\n );\n queue.enqueue(\n () =>\n responseCtx.respond(`_Retrying (${retryEvent.attempt}/${retryEvent.maxAttempts})..._`),\n \"retry\",\n );\n }\n });\n\n // Message limit constant\n const SLACK_MAX_LENGTH = 40000;\n const splitForSlack = (text: string): string[] => {\n if (text.length <= SLACK_MAX_LENGTH) return [text];\n const parts: string[] = [];\n let remaining = text;\n let partNum = 1;\n while (remaining.length > 0) {\n const chunk = remaining.substring(0, SLACK_MAX_LENGTH - 50);\n remaining = remaining.substring(SLACK_MAX_LENGTH - 50);\n const suffix = remaining.length > 0 ? `\\n_(continued ${partNum}...)_` : \"\";\n parts.push(chunk + suffix);\n partNum++;\n }\n return parts;\n };\n\n return {\n async run(\n message: ChatMessage,\n responseCtx: ChatResponseContext,\n platform: PlatformInfo,\n ): Promise<{ stopReason: string; errorMessage?: string }> {\n // Extract conversationId from sessionKey (format: \"conversationId:rootTs\" or just \"conversationId\")\n const sessionConversationId = message.sessionKey.split(\":\")[0];\n\n // Ensure the conversation directory exists\n await mkdir(conversationDir, { recursive: true });\n\n // Refresh vault config and clear executor cache so credential changes\n // (env file updates, vault.json edits, token rotations) take effect.\n // Then set the active actor BEFORE building system prompt, so workspacePath\n // reflects the actor's sandbox type.\n if (executionResolver) {\n executionResolver.refresh();\n activeExecutor = await executionResolver.resolve({\n platform: platform.name,\n userId: message.userId,\n });\n workspacePath = getWorkspacePath();\n }\n\n // Sync messages from log.jsonl that arrived while we were offline or busy\n // Exclude the current message (it will be added via prompt())\n // Default sync range is 10 days (handled by syncLogToSessionManager)\n // Thread filter ensures only messages from this session's thread are synced\n const threadFilter = message.sessionKey.includes(\":\")\n ? { scope: \"thread\" as const, rootTs, threadTs: message.threadTs }\n : { scope: \"top-level\" as const, rootTs };\n const syncedCount = await syncLogToSessionManager(\n sessionManager,\n conversationDir,\n message.id,\n undefined,\n threadFilter,\n );\n if (syncedCount > 0) {\n log.logInfo(`[${conversationId}] Synced ${syncedCount} messages from log.jsonl`);\n }\n\n // Reload messages from the session file.\n // This picks up any messages synced above.\n const reloadedSession = sessionManager.buildSessionContext();\n if (reloadedSession.messages.length > 0) {\n agent.state.messages = reloadedSession.messages;\n log.logInfo(\n `[${conversationId}] Reloaded ${reloadedSession.messages.length} messages from context`,\n );\n }\n\n // Update system prompt with fresh memory, channel/user info, and skills\n // Use the actual executor's sandbox config, not the initial config,\n // to ensure accurate environment description for the model\n const memory = await getMemory(conversationDir);\n const skills = loadMamaSkills(conversationDir, workspacePath);\n const actualSandboxConfig = executor.getSandboxConfig();\n const systemPrompt = buildSystemPrompt(\n workspacePath,\n conversationId,\n message.userId,\n memory,\n actualSandboxConfig,\n platform,\n skills,\n );\n session.agent.state.systemPrompt = systemPrompt;\n\n // Set up file upload function\n setUploadFunction(async (filePath: string, title?: string) => {\n const hostPath = translateToHostPath(\n filePath,\n conversationDir,\n workspacePath,\n conversationId,\n );\n await responseCtx.uploadFile(hostPath, title);\n });\n setEventContext({\n platform: platform.name,\n conversationId,\n userId: message.userId,\n });\n\n // Reset per-run state\n runState.responseCtx = responseCtx;\n runState.logCtx = {\n conversationId: sessionConversationId,\n userName: message.userName,\n conversationName: undefined,\n sessionId: sessionUuid,\n };\n runState.pendingTools.clear();\n runState.totalUsage = {\n input: 0,\n output: 0,\n cacheRead: 0,\n cacheWrite: 0,\n cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n };\n runState.llmCallCount = 0;\n runState.stopReason = \"stop\";\n runState.errorMessage = undefined;\n\n // Create queue for this run\n let queueChain = Promise.resolve();\n runState.queue = {\n enqueue(fn: () => Promise<void>, errorContext: string): void {\n queueChain = queueChain.then(async () => {\n try {\n await fn();\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(`API error (${errorContext})`, errMsg);\n try {\n // Split long error messages to avoid msg_too_long\n const errParts = splitForSlack(`_Error: ${errMsg}_`);\n for (const part of errParts) {\n await responseCtx.respondInThread(part);\n }\n } catch {\n // Ignore\n }\n }\n });\n },\n enqueueMessage(\n text: string,\n target: \"main\" | \"thread\",\n errorContext: string,\n _doLog = true,\n ): void {\n const parts = splitForSlack(text);\n for (const part of parts) {\n this.enqueue(\n () =>\n target === \"main\" ? responseCtx.respond(part) : responseCtx.respondInThread(part),\n errorContext,\n );\n }\n },\n };\n\n // Log context info\n log.logInfo(\n `Context sizes - system: ${systemPrompt.length} chars, memory: ${memory.length} chars`,\n );\n log.logInfo(`Channels: ${platform.channels.length}, Users: ${platform.users.length}`);\n\n // Build user message with timestamp and username prefix\n // Format: \"[YYYY-MM-DD HH:MM:SS+HH:MM] [username]: message\" so LLM knows when and who\n const now = new Date();\n const pad = (n: number) => n.toString().padStart(2, \"0\");\n const offset = -now.getTimezoneOffset();\n const offsetSign = offset >= 0 ? \"+\" : \"-\";\n const offsetHours = pad(Math.floor(Math.abs(offset) / 60));\n const offsetMins = pad(Math.abs(offset) % 60);\n const timestamp = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}${offsetSign}${offsetHours}:${offsetMins}`;\n const threadContext = message.threadTs ? ` [in-thread:${message.threadTs}]` : \"\";\n let userMessage = `[${timestamp}] [${message.userName || \"unknown\"}]${threadContext}: ${message.text}`;\n\n const imageAttachments: ImageContent[] = [];\n const nonImagePaths: string[] = [];\n\n for (const a of message.attachments || []) {\n // a.localPath is the path relative to the workspace\n const fullPath = `${workspacePath}/${a.localPath}`;\n const mimeType = getImageMimeType(a.localPath);\n\n if (mimeType && existsSync(fullPath)) {\n try {\n imageAttachments.push({\n type: \"image\",\n mimeType,\n data: readFileSync(fullPath).toString(\"base64\"),\n });\n } catch {\n nonImagePaths.push(fullPath);\n }\n } else {\n nonImagePaths.push(fullPath);\n }\n }\n\n if (nonImagePaths.length > 0) {\n userMessage += `\\n\\n<slack_attachments>\\n${nonImagePaths.join(\"\\n\")}\\n</slack_attachments>`;\n }\n\n // Debug: write context to last_prompt.jsonl\n const debugContext = {\n systemPrompt,\n messages: session.messages,\n newUserMessage: userMessage,\n imageAttachmentCount: imageAttachments.length,\n };\n await writeFile(\n join(conversationDir, \"last_prompt.jsonl\"),\n JSON.stringify(debugContext, null, 2),\n );\n addLifecycleBreadcrumb(\"agent.prompt.sent\", {\n provider: model.provider,\n model: agentConfig.model,\n channel_id: sessionConversationId,\n session_id: sessionUuid,\n attachment_count: message.attachments?.length ?? 0,\n image_attachment_count: imageAttachments.length,\n });\n\n await session.prompt(\n userMessage,\n imageAttachments.length > 0 ? { images: imageAttachments } : undefined,\n );\n\n // Wait for queued messages\n await queueChain;\n\n // Handle error case - update main message and post error to thread\n if (runState.stopReason === \"error\" && runState.errorMessage) {\n try {\n await responseCtx.replaceResponse(\"_Sorry, something went wrong_\");\n // Split long error messages to avoid msg_too_long\n const errorParts = splitForSlack(`_Error: ${runState.errorMessage}_`);\n for (const part of errorParts) {\n await responseCtx.respondInThread(part);\n }\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(\"Failed to post error message\", errMsg);\n }\n } else {\n // Final message update\n const messages = session.messages;\n const lastAssistant = messages.filter((m) => m.role === \"assistant\").pop();\n const finalText =\n lastAssistant?.content\n .filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n .map((c) => c.text)\n .join(\"\\n\") || \"\";\n\n // Check for [SILENT] marker - delete message and thread instead of posting\n if (finalText.trim() === \"[SILENT]\" || finalText.trim().startsWith(\"[SILENT]\")) {\n try {\n await responseCtx.deleteResponse();\n log.logInfo(\"Silent response - deleted message and thread\");\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(\"Failed to delete message for silent response\", errMsg);\n }\n } else if (finalText.trim()) {\n try {\n const mainText =\n finalText.length > SLACK_MAX_LENGTH\n ? `${finalText.substring(0, SLACK_MAX_LENGTH - 50)}\\n\\n_(see thread for full response)_`\n : finalText;\n await responseCtx.replaceResponse(mainText);\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(\"Failed to replace message with final text\", errMsg);\n }\n }\n }\n\n // Log usage summary with context info\n if (runState.totalUsage.cost.total > 0) {\n // Get last non-aborted assistant message for context calculation\n const messages = session.messages;\n const lastAssistantMessage = messages\n .slice()\n .reverse()\n .find((m) => m.role === \"assistant\" && (m as any).stopReason !== \"aborted\") as any;\n\n const contextTokens = lastAssistantMessage\n ? lastAssistantMessage.usage.input +\n lastAssistantMessage.usage.output +\n lastAssistantMessage.usage.cacheRead +\n lastAssistantMessage.usage.cacheWrite\n : 0;\n const contextWindow = model.contextWindow || 200000;\n\n // Run-level Sentry metrics\n const { totalUsage } = runState;\n const runMetricAttributes = metricAttributes({\n provider: model.provider,\n model: agentConfig.model,\n channel_id: sessionConversationId,\n session_id: sessionUuid,\n stop_reason: runState.stopReason,\n llm_calls: runState.llmCallCount,\n });\n Sentry.metrics.distribution(\"agent.run.tokens_in\", totalUsage.input, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.distribution(\"agent.run.tokens_out\", totalUsage.output, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.distribution(\"agent.run.cache_read\", totalUsage.cacheRead, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.distribution(\"agent.run.cache_write\", totalUsage.cacheWrite, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.distribution(\"agent.run.cost\", totalUsage.cost.total, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.gauge(\"agent.context.utilization\", contextTokens / contextWindow, {\n unit: \"ratio\",\n attributes: runMetricAttributes,\n });\n\n const summary = log.logUsageSummary(\n runState.logCtx!,\n runState.totalUsage,\n contextTokens,\n contextWindow,\n );\n // Split long summaries to avoid msg_too_long\n const summaryParts = splitForSlack(summary);\n for (const part of summaryParts) {\n runState.queue!.enqueue(\n () => responseCtx.respondInThread(part, { style: \"muted\" }),\n \"usage summary\",\n );\n }\n await queueChain;\n }\n\n // Clear run state\n runState.responseCtx = null;\n runState.logCtx = null;\n runState.queue = null;\n\n return { stopReason: runState.stopReason, errorMessage: runState.errorMessage };\n },\n\n abort(): void {\n session.abort();\n },\n\n getCurrentStep(): { toolName?: string; label?: string } | undefined {\n const pending = runState.pendingTools;\n if (pending.size === 0) return undefined;\n // Get the first pending tool\n const first = pending.values().next().value;\n if (!first) return undefined;\n return {\n toolName: first.toolName,\n label: (first.args as { label?: string })?.label,\n };\n },\n };\n}\n\n/**\n * Translate container path back to host path for file operations\n */\nfunction translateToHostPath(\n containerPath: string,\n conversationDir: string,\n workspacePath: string,\n conversationId: string,\n): string {\n if (workspacePath === \"/workspace\") {\n const prefix = `/workspace/${conversationId}/`;\n if (containerPath.startsWith(prefix)) {\n return join(conversationDir, containerPath.slice(prefix.length));\n }\n if (containerPath.startsWith(\"/workspace/\")) {\n return join(conversationDir, \"..\", containerPath.slice(\"/workspace/\".length));\n }\n }\n return containerPath;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EACV,WAAW,EACX,mBAAmB,EAEnB,YAAY,EACb,MAAM,cAAc,CAAC;AAKtB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAiC,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAEjF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAgB/C,MAAM,WAAW,WAAW;IAC1B,GAAG,CACD,OAAO,EAAE,WAAW,EACpB,WAAW,EAAE,mBAAmB,EAChC,QAAQ,EAAE,YAAY,GACrB,OAAO,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC1D,KAAK,IAAI,IAAI,CAAC;IACd,6DAA6D;IAC7D,cAAc,IAAI;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC;CACrE;AA4WD;;;;;;GAMG;AACH,wBAAsB,YAAY,CAChC,aAAa,EAAE,aAAa,EAC5B,UAAU,EAAE,MAAM,EAClB,cAAc,EAAE,MAAM,EACtB,eAAe,EAAE,MAAM,EACvB,YAAY,EAAE,MAAM,EACpB,YAAY,CAAC,EAAE,YAAY,EAC3B,YAAY,CAAC,EAAE,gBAAgB,EAC/B,WAAW,CAAC,EAAE,sBAAsB,GACnC,OAAO,CAAC,WAAW,CAAC,CAsxBtB","sourcesContent":["import { Agent, type AgentEvent } from \"@mariozechner/pi-agent-core\";\nimport { getModel, type ImageContent } from \"@mariozechner/pi-ai\";\nimport {\n AgentSession,\n AuthStorage,\n convertToLlm,\n DefaultResourceLoader,\n formatSkillsForPrompt,\n getAgentDir,\n loadSkillsFromDir,\n ModelRegistry,\n SessionManager,\n type Skill,\n} from \"@mariozechner/pi-coding-agent\";\nimport { existsSync, readFileSync } from \"fs\";\nimport { mkdir, readFile, writeFile } from \"fs/promises\";\nimport { homedir } from \"os\";\nimport { join } from \"path\";\nimport type {\n ChatMessage,\n ChatResponseContext,\n ConversationKind,\n PlatformInfo,\n} from \"./adapter.js\";\nimport { loadAgentConfig } from \"./config.js\";\nimport { createMamaSettingsManager, syncLogToSessionManager } from \"./context.js\";\nimport { ActorExecutionResolver } from \"./execution-resolver.js\";\nimport * as log from \"./log.js\";\nimport type { UserBindingStore } from \"./bindings.js\";\nimport type { DockerContainerManager } from \"./provisioner.js\";\nimport { createExecutor, type Executor, type SandboxConfig } from \"./sandbox.js\";\nimport { addLifecycleBreadcrumb, metricAttributes } from \"./sentry.js\";\nimport type { VaultManager } from \"./vault.js\";\nimport {\n createManagedSessionFileAtPath,\n extractSessionSuffix,\n extractSessionUuid,\n forkThreadSessionFile,\n getChannelSessionDir,\n getThreadSessionFile,\n openManagedSession,\n resolveChannelSessionFile,\n resolveManagedSessionFile,\n tryResolveThreadSession,\n} from \"./session-store.js\";\nimport { createMamaTools } from \"./tools/index.js\";\nimport * as Sentry from \"@sentry/node\";\n\nexport interface AgentRunner {\n run(\n message: ChatMessage,\n responseCtx: ChatResponseContext,\n platform: PlatformInfo,\n ): Promise<{ stopReason: string; errorMessage?: string }>;\n abort(): void;\n /** Get current step info (tool name, label) for debugging */\n getCurrentStep(): { toolName?: string; label?: string } | undefined;\n}\n\nconst IMAGE_MIME_TYPES: Record<string, string> = {\n jpg: \"image/jpeg\",\n jpeg: \"image/jpeg\",\n png: \"image/png\",\n gif: \"image/gif\",\n webp: \"image/webp\",\n};\n\nfunction getImageMimeType(filename: string): string | undefined {\n return IMAGE_MIME_TYPES[filename.toLowerCase().split(\".\").pop() || \"\"];\n}\n\nasync function getMemory(conversationDir: string): Promise<string> {\n const parts: string[] = [];\n\n // Read workspace-level memory (shared across all conversations)\n const workspaceMemoryPath = join(conversationDir, \"..\", \"MEMORY.md\");\n if (existsSync(workspaceMemoryPath)) {\n try {\n const content = (await readFile(workspaceMemoryPath, \"utf-8\")).trim();\n if (content) {\n parts.push(`### Global Workspace Memory\\n${content}`);\n }\n } catch (error) {\n log.logWarning(\"Failed to read workspace memory\", `${workspaceMemoryPath}: ${error}`);\n }\n }\n\n // Read conversation-specific memory\n const conversationMemoryPath = join(conversationDir, \"MEMORY.md\");\n if (existsSync(conversationMemoryPath)) {\n try {\n const content = (await readFile(conversationMemoryPath, \"utf-8\")).trim();\n if (content) {\n parts.push(`### Conversation-Specific Memory\\n${content}`);\n }\n } catch (error) {\n log.logWarning(\"Failed to read conversation memory\", `${conversationMemoryPath}: ${error}`);\n }\n }\n\n if (parts.length === 0) {\n return \"(no working memory yet)\";\n }\n\n return parts.join(\"\\n\\n\");\n}\n\nfunction loadMamaSkills(conversationDir: string, workspacePath: string): Skill[] {\n const skillMap = new Map<string, Skill>();\n\n // conversationDir is the host path (e.g., /Users/.../data/C0A34FL8PMH)\n // hostWorkspacePath is the parent directory on host\n // workspacePath is the container path (e.g., /workspace)\n const hostWorkspacePath = join(conversationDir, \"..\");\n\n // Helper to translate host paths to container paths\n const translatePath = (hostPath: string): string => {\n if (hostPath.startsWith(hostWorkspacePath)) {\n return workspacePath + hostPath.slice(hostWorkspacePath.length);\n }\n return hostPath;\n };\n\n // Load workspace-level skills (global)\n const workspaceSkillsDir = join(hostWorkspacePath, \"skills\");\n for (const skill of loadSkillsFromDir({ dir: workspaceSkillsDir, source: \"workspace\" }).skills) {\n // Translate paths to container paths for system prompt\n skill.filePath = translatePath(skill.filePath);\n skill.baseDir = translatePath(skill.baseDir);\n skillMap.set(skill.name, skill);\n }\n\n // Load conversation-specific skills (override workspace skills on collision)\n const conversationSkillsDir = join(conversationDir, \"skills\");\n for (const skill of loadSkillsFromDir({ dir: conversationSkillsDir, source: \"channel\" }).skills) {\n skill.filePath = translatePath(skill.filePath);\n skill.baseDir = translatePath(skill.baseDir);\n skillMap.set(skill.name, skill);\n }\n\n return Array.from(skillMap.values());\n}\n\nfunction buildSystemPrompt(\n workspacePath: string,\n conversationId: string,\n conversationKind: ConversationKind,\n currentUserId: string | undefined,\n memory: string,\n sandboxConfig: SandboxConfig,\n platform: PlatformInfo,\n skills: Skill[],\n): string {\n const conversationPath = `${workspacePath}/${conversationId}`;\n const isContainer = sandboxConfig.type === \"container\" || sandboxConfig.type === \"image\";\n const isImageSandbox = sandboxConfig.type === \"image\";\n const isFirecracker = sandboxConfig.type === \"firecracker\";\n\n // Format channel mappings\n const channelMappings =\n platform.channels.length > 0\n ? platform.channels.map((c) => `${c.id}\\t#${c.name}`).join(\"\\n\")\n : \"(no channels loaded)\";\n\n // Format user mappings\n const userMappings =\n platform.users.length > 0\n ? platform.users.map((u) => `${u.id}\\t@${u.userName}\\t${u.displayName}`).join(\"\\n\")\n : \"(no users loaded)\";\n\n const envDescription = isImageSandbox\n ? `You are running inside a managed per-user container.\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with the image's package manager\n- Your changes persist for this user's container until it is recreated`\n : isContainer\n ? `You are running inside a shared container.\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with the container's package manager\n- Your changes persist across sessions`\n : isFirecracker\n ? `You are running inside a Firecracker microVM.\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with: apt-get install <package> (Debian-based)\n- Your changes persist across sessions`\n : `You are running directly on the host machine.\n- Bash working directory: ${process.cwd()}\n- Be careful with system modifications`;\n\n return `You are mama, a ${platform.name} bot assistant. Be concise. No emojis.\n\n## Context\n- For current date/time, use: date\n- You have access to previous conversation context including tool results from prior turns.\n- For older history beyond your context, search log.jsonl (contains user messages and your final responses, but not tool results).\n- User messages include a \\`[in-thread:TS]\\` marker when sent from within a Slack thread (TS is the root message timestamp). Without this marker, the message is a top-level channel message.\n\n${platform.formattingGuide}\n\n## Platform IDs\nChannels: ${channelMappings}\n\nUsers: ${userMappings}\n\nWhen mentioning users, use <@username> format (e.g., <@mario>).\n\n## Environment\n${envDescription}\n\n## Workspace Layout\n${workspacePath}/\n├── MEMORY.md # Global memory (all conversations)\n├── skills/ # Global CLI tools you create\n└── ${conversationId}/ # This conversation\n ├── MEMORY.md # Conversation-specific memory\n ├── log.jsonl # Message history (no tool results)\n ├── attachments/ # User-shared files\n ├── scratch/ # Your working directory\n └── skills/ # Conversation-specific tools\n\n## Skills (Custom CLI Tools)\nYou can create reusable CLI tools for recurring tasks (email, APIs, data processing, etc.).\n\n### Creating Skills\nStore in \\`${workspacePath}/skills/<name>/\\` (global) or \\`${conversationPath}/skills/<name>/\\` (conversation-specific).\nEach skill directory needs a \\`SKILL.md\\` with YAML frontmatter:\n\n\\`\\`\\`markdown\n---\nname: skill-name\ndescription: Short description of what this skill does\n---\n\n# Skill Name\n\nUsage instructions, examples, etc.\nScripts are in: {baseDir}/\n\\`\\`\\`\n\n\\`name\\` and \\`description\\` are required. Use \\`{baseDir}\\` as placeholder for the skill's directory path.\n\n### Available Skills\n${skills.length > 0 ? formatSkillsForPrompt(skills) : \"(no skills installed yet)\"}\n\n## Events\nYou can schedule events that wake you up at specific times or when external things happen. Events are JSON files in \\`${workspacePath}/events/\\`.\n\n### Event Types\n\n**Immediate** - Triggers as soon as harness sees the file. Use in scripts/webhooks to signal external events.\n\\`\\`\\`json\n{\"type\": \"immediate\", \"platform\": \"${platform.name}\", \"conversationId\": \"${conversationId}\", \"conversationKind\": \"${conversationKind}\", \"userId\": \"${currentUserId ?? \"<requester userId>\"}\", \"text\": \"New GitHub issue opened\"}\n\\`\\`\\`\n\n**One-shot** - Triggers once at a specific time. Use for reminders.\n\\`\\`\\`json\n{\"type\": \"one-shot\", \"platform\": \"${platform.name}\", \"conversationId\": \"${conversationId}\", \"conversationKind\": \"${conversationKind}\", \"userId\": \"${currentUserId ?? \"<requester userId>\"}\", \"text\": \"Remind Mario about dentist\", \"at\": \"2025-12-15T09:00:00+01:00\"}\n\\`\\`\\`\n\n**Periodic** - Triggers on a cron schedule. Use for recurring tasks.\n\\`\\`\\`json\n{\"type\": \"periodic\", \"platform\": \"${platform.name}\", \"conversationId\": \"${conversationId}\", \"conversationKind\": \"${conversationKind}\", \"userId\": \"${currentUserId ?? \"<requester userId>\"}\", \"text\": \"Check inbox and summarize\", \"schedule\": \"0 9 * * 1-5\", \"timezone\": \"${Intl.DateTimeFormat().resolvedOptions().timeZone}\"}\n\\`\\`\\`\n\n### Cron Format\n\\`minute hour day-of-month month day-of-week\\`\n- \\`0 9 * * *\\` = daily at 9:00\n- \\`0 9 * * 1-5\\` = weekdays at 9:00\n- \\`30 14 * * 1\\` = Mondays at 14:30\n- \\`0 0 1 * *\\` = first of each month at midnight\n\n### Timezones\nAll \\`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}.\n\n### Platform and Credential Routing\nSet \\`platform\\` to the target bot platform (\\`${platform.name}\\` for this conversation). When only one platform is running, omitting \\`platform\\` is allowed for backward compatibility, but include it by default to avoid ambiguity.\n\nSet \\`userId\\` to the platform userId of whoever asked for the event. When the event fires, tool execution routes using that user's vault selection in per-user modes. In \\`container:<name>\\`, events use the container's single shared vault.\n\nPrefer the \\`event\\` tool over manually writing JSON files; it fills \\`platform\\`, \\`conversationId\\`, \\`conversationKind\\`, and \\`userId\\` for the current conversation automatically.\n\n### Creating Events\nUse unique filenames to avoid overwriting existing events. Include a timestamp or random suffix:\n\\`\\`\\`bash\ncat > ${workspacePath}/events/dentist-reminder-$(date +%s).json << 'EOF'\n{\"type\": \"one-shot\", \"platform\": \"${platform.name}\", \"conversationId\": \"${conversationId}\", \"conversationKind\": \"${conversationKind}\", \"userId\": \"${currentUserId ?? \"<requester userId>\"}\", \"text\": \"Dentist tomorrow\", \"at\": \"2025-12-14T09:00:00+01:00\"}\nEOF\n\\`\\`\\`\nOr check if file exists first before creating.\n\n### Managing Events\n- List: \\`ls ${workspacePath}/events/\\`\n- View: \\`cat ${workspacePath}/events/foo.json\\`\n- Delete/cancel: \\`rm ${workspacePath}/events/foo.json\\`\n\n### When Events Trigger\nYou receive a message like:\n\\`\\`\\`\n[EVENT:dentist-reminder.json:one-shot:2025-12-14T09:00:00+01:00] Dentist tomorrow\n\\`\\`\\`\nImmediate and one-shot events auto-delete after triggering. Periodic events persist until you delete them.\n\n### Silent Completion\nFor periodic events where there's nothing to report, respond with just \\`[SILENT]\\` (no other text). This deletes the status message and posts nothing to the platform. Use this to avoid spamming the channel when periodic checks find nothing actionable.\n\n### Debouncing\nWhen 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.\n\n### Limits\nMaximum 5 events can be queued. Don't create excessive immediate or periodic events.\n\n## Memory\nWrite to MEMORY.md files to persist context across conversations.\n- Global (${workspacePath}/MEMORY.md): skills, preferences, project info\n- Conversation (${conversationPath}/MEMORY.md): conversation-specific decisions, ongoing work\nUpdate when you learn something important or when asked to remember something.\n\n### Current Memory\n${memory}\n\n## System Configuration Log\nMaintain ${workspacePath}/SYSTEM.md to log all environment modifications:\n- Installed packages (apk add, npm install, pip install)\n- Environment variables set\n- Config files modified (~/.gitconfig, cron jobs, etc.)\n- Skill dependencies installed\n\nUpdate this file whenever you modify the environment. On fresh container, read it first to restore your setup.\n\n## Log Queries (for older history)\nFormat: \\`{\"date\":\"...\",\"ts\":\"...\",\"user\":\"...\",\"userName\":\"...\",\"text\":\"...\",\"isBot\":false}\\`\nThe log contains user messages and your final responses (not tool calls/results).\n${isContainer ? \"Install jq: apk add jq\" : \"\"}\n${isFirecracker ? \"Install jq: apt-get install jq\" : \"\"}\n\n\\`\\`\\`bash\n# Recent messages\ntail -30 log.jsonl | jq -c '{date: .date[0:19], user: (.userName // .user), text}'\n\n# Search for specific topic\ngrep -i \"topic\" log.jsonl | jq -c '{date: .date[0:19], user: (.userName // .user), text}'\n\n# Messages from specific user\ngrep '\"userName\":\"mario\"' log.jsonl | tail -20 | jq -c '{date: .date[0:19], text}'\n\\`\\`\\`\n\n## Tools\n- bash: Run shell commands (primary tool). Install packages as needed.\n- read: Read files\n- write: Create/overwrite files\n- edit: Surgical file edits\n- attach: Share files to the platform\n\nEach tool requires a \"label\" parameter (shown to user).\n`;\n}\n\nfunction truncate(text: string, maxLen: number): string {\n if (text.length <= maxLen) return text;\n return `${text.substring(0, maxLen - 3)}...`;\n}\n\nfunction extractToolResultText(result: unknown): string {\n if (typeof result === \"string\") {\n return result;\n }\n\n if (\n result &&\n typeof result === \"object\" &&\n \"content\" in result &&\n Array.isArray((result as { content: unknown }).content)\n ) {\n const content = (result as { content: Array<{ type: string; text?: string }> }).content;\n const textParts: string[] = [];\n for (const part of content) {\n if (part.type === \"text\" && part.text) {\n textParts.push(part.text);\n }\n }\n if (textParts.length > 0) {\n return textParts.join(\"\\n\");\n }\n }\n\n return JSON.stringify(result);\n}\n\nfunction formatToolArgsForSlack(_toolName: string, args: Record<string, unknown>): string {\n const lines: string[] = [];\n\n for (const [key, value] of Object.entries(args)) {\n if (key === \"label\") continue;\n\n if (key === \"path\" && typeof value === \"string\") {\n const offset = args.offset as number | undefined;\n const limit = args.limit as number | undefined;\n if (offset !== undefined && limit !== undefined) {\n lines.push(`${value}:${offset}-${offset + limit}`);\n } else {\n lines.push(value);\n }\n continue;\n }\n\n if (key === \"offset\" || key === \"limit\") continue;\n\n if (typeof value === \"string\") {\n lines.push(value);\n } else {\n lines.push(JSON.stringify(value));\n }\n }\n\n return lines.join(\"\\n\");\n}\n\n// ============================================================================\n// Agent runner\n// ============================================================================\n\n/**\n * Create a new AgentRunner for a channel.\n * Sets up the session and subscribes to events once.\n *\n * Runner caching is handled by the caller (channelStates in main.ts).\n * This is a stateless factory function.\n */\nexport async function createRunner(\n sandboxConfig: SandboxConfig,\n sessionKey: string,\n conversationId: string,\n conversationDir: string,\n workspaceDir: string,\n vaultManager?: VaultManager,\n bindingStore?: UserBindingStore,\n provisioner?: DockerContainerManager,\n): Promise<AgentRunner> {\n const agentConfig = loadAgentConfig(workspaceDir);\n\n // Initialize logger with settings from config\n log.initLogger({\n logFormat: agentConfig.logFormat,\n logLevel: agentConfig.logLevel,\n });\n\n const executionResolver =\n vaultManager &&\n sandboxConfig.type !== \"host\" &&\n (vaultManager.isEnabled() ||\n !!bindingStore ||\n sandboxConfig.type === \"container\" ||\n sandboxConfig.type === \"image\")\n ? new ActorExecutionResolver(sandboxConfig, vaultManager, bindingStore, provisioner)\n : undefined;\n let activeExecutor: Executor =\n executionResolver !== undefined\n ? createExecutor({ type: \"host\" })\n : createExecutor(sandboxConfig);\n const executor: Executor = {\n exec(command, options) {\n return activeExecutor.exec(command, options);\n },\n getWorkspacePath(hostPath) {\n return activeExecutor.getWorkspacePath(hostPath);\n },\n getSandboxConfig() {\n return activeExecutor.getSandboxConfig();\n },\n };\n const workspaceBase = conversationDir.replace(`/${conversationId}`, \"\");\n const getWorkspacePath = () => executor.getWorkspacePath(workspaceBase);\n let workspacePath = getWorkspacePath();\n\n // Create tools (per-runner, with per-runner upload function setter)\n const { tools, setUploadFunction, setEventContext } = createMamaTools(executor, workspaceDir);\n\n // Resolve model from config\n // Use 'as any' cast because agentConfig.provider/model are plain strings,\n // while getModel() has constrained generic types for known providers.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const model = (getModel as any)(agentConfig.provider, agentConfig.model);\n\n // Initial system prompt (will be updated each run with fresh memory/channels/users/skills)\n const memory = await getMemory(conversationDir);\n const skills = loadMamaSkills(conversationDir, workspacePath);\n const emptyPlatform: PlatformInfo = {\n name: \"slack\",\n formattingGuide: \"\",\n channels: [],\n users: [],\n };\n const systemPrompt = buildSystemPrompt(\n workspacePath,\n conversationId,\n \"shared\",\n undefined,\n memory,\n sandboxConfig,\n emptyPlatform,\n skills,\n );\n\n // Create session manager and settings manager\n // Conversation sessions use {conversationDir}/sessions/current.\n // Thread sessions use fixed files: {conversationDir}/sessions/{threadTs}.jsonl\n const sessionDir = getChannelSessionDir(conversationDir);\n const isThread = sessionKey.includes(\":\");\n\n let sessionManager!: SessionManager;\n let contextFile!: string;\n\n if (isThread) {\n const threadFile = getThreadSessionFile(conversationDir, sessionKey);\n const existing = tryResolveThreadSession(threadFile);\n if (existing) {\n contextFile = existing;\n sessionManager = openManagedSession(contextFile, sessionDir, conversationDir);\n } else {\n const conversationSource = resolveChannelSessionFile(conversationDir);\n if (conversationSource) {\n try {\n contextFile = forkThreadSessionFile(conversationSource, threadFile, conversationDir);\n sessionManager = openManagedSession(contextFile, sessionDir, conversationDir);\n } catch {\n contextFile = createManagedSessionFileAtPath(threadFile, conversationDir);\n sessionManager = openManagedSession(contextFile, sessionDir, conversationDir);\n }\n } else {\n contextFile = createManagedSessionFileAtPath(threadFile, conversationDir);\n sessionManager = openManagedSession(contextFile, sessionDir, conversationDir);\n }\n }\n } else {\n // Direct/shared session: normal resolve\n contextFile = resolveManagedSessionFile(sessionDir, conversationDir);\n sessionManager = openManagedSession(contextFile, sessionDir, conversationDir);\n }\n const sessionUuid = extractSessionUuid(contextFile);\n // Used for Slack thread filtering — for non-Slack platforms this is effectively a no-op\n const rootTs = extractSessionSuffix(sessionKey);\n const settingsManager = createMamaSettingsManager(join(conversationDir, \"..\"));\n\n // Create AuthStorage and ModelRegistry\n // Auth stored outside workspace so agent can't access it\n const authStorage = AuthStorage.create(join(homedir(), \".pi\", \"mama\", \"auth.json\"));\n const modelRegistry = ModelRegistry.create(authStorage);\n\n // Create agent\n const agent = new Agent({\n initialState: {\n systemPrompt,\n model,\n thinkingLevel:\n (agentConfig.thinkingLevel as \"off\" | \"low\" | \"medium\" | \"high\" | undefined) ?? \"off\",\n tools,\n },\n convertToLlm,\n getApiKey: async () => {\n const key = await modelRegistry.getApiKeyForProvider(model.provider);\n if (!key)\n throw new Error(\n `No API key for provider \"${model.provider}\". Set the appropriate environment variable or configure via auth.json`,\n );\n return key;\n },\n });\n\n // Load existing messages\n const loadedSession = sessionManager.buildSessionContext();\n if (loadedSession.messages.length > 0) {\n agent.state.messages = loadedSession.messages;\n log.logInfo(\n `[${conversationId}] Loaded ${loadedSession.messages.length} messages from context.jsonl`,\n );\n }\n\n // Load extensions, skills, prompts, themes via DefaultResourceLoader\n // This reads ~/.pi/agent/settings.json (packages, extensions enable/disable)\n // and discovers resources from standard locations + npm/git packages.\n const resourceLoader = new DefaultResourceLoader({\n cwd: workspaceDir,\n agentDir: getAgentDir(),\n systemPrompt,\n });\n try {\n await resourceLoader.reload();\n const extResult = resourceLoader.getExtensions();\n if (extResult.errors.length > 0) {\n for (const err of extResult.errors) {\n log.logWarning(`[${conversationId}] Extension load error: ${err.path}`, err.error);\n }\n }\n log.logInfo(\n `[${conversationId}] Loaded ${extResult.extensions.length} extension(s): ${extResult.extensions.map((e) => e.path).join(\", \")}`,\n );\n } catch (error) {\n log.logWarning(`[${conversationId}] Failed to load resources`, String(error));\n }\n\n const baseToolsOverride = Object.fromEntries(tools.map((tool) => [tool.name, tool]));\n\n // Create AgentSession wrapper\n const session = new AgentSession({\n agent,\n sessionManager,\n settingsManager,\n cwd: workspaceDir,\n modelRegistry,\n resourceLoader,\n baseToolsOverride,\n });\n\n // Mutable per-run state - event handler references this\n const runState = {\n responseCtx: null as ChatResponseContext | null,\n logCtx: null as {\n conversationId: string;\n userName?: string;\n conversationName?: string;\n sessionId?: string;\n } | null,\n queue: null as {\n enqueue(fn: () => Promise<void>, errorContext: string): void;\n enqueueMessage(\n text: string,\n target: \"main\" | \"thread\",\n errorContext: string,\n doLog?: boolean,\n ): void;\n } | null,\n pendingTools: new Map<string, { toolName: string; args: unknown; startTime: number }>(),\n totalUsage: {\n input: 0,\n output: 0,\n cacheRead: 0,\n cacheWrite: 0,\n cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n },\n llmCallCount: 0,\n stopReason: \"stop\",\n errorMessage: undefined as string | undefined,\n };\n\n // Subscribe to events ONCE\n session.subscribe(async (event) => {\n // Skip if no active run\n if (!runState.responseCtx || !runState.logCtx || !runState.queue) return;\n\n const { responseCtx, logCtx, queue, pendingTools } = runState;\n const baseAttrs = { channel_id: logCtx.conversationId, session_id: logCtx.sessionId };\n\n if (event.type === \"tool_execution_start\") {\n const agentEvent = event as AgentEvent & { type: \"tool_execution_start\" };\n const args = agentEvent.args as { label?: string };\n const label = args.label || agentEvent.toolName;\n\n pendingTools.set(agentEvent.toolCallId, {\n toolName: agentEvent.toolName,\n args: agentEvent.args,\n startTime: Date.now(),\n });\n addLifecycleBreadcrumb(\"agent.tool.started\", {\n tool: agentEvent.toolName,\n ...baseAttrs,\n });\n\n log.logToolStart(\n logCtx,\n agentEvent.toolName,\n label,\n agentEvent.args as Record<string, unknown>,\n );\n // Tool labels are omitted from the main message to reduce Slack noise.\n // Tool execution details are still posted to the thread (see tool_execution_end).\n } else if (event.type === \"tool_execution_end\") {\n const agentEvent = event as AgentEvent & { type: \"tool_execution_end\" };\n const resultStr = extractToolResultText(agentEvent.result);\n const pending = pendingTools.get(agentEvent.toolCallId);\n pendingTools.delete(agentEvent.toolCallId);\n\n const durationMs = pending ? Date.now() - pending.startTime : 0;\n\n Sentry.metrics.count(\"agent.tool.calls\", 1, {\n attributes: metricAttributes({\n tool: agentEvent.toolName,\n error: String(agentEvent.isError),\n ...baseAttrs,\n }),\n });\n Sentry.metrics.distribution(\"agent.tool.duration\", durationMs, {\n unit: \"millisecond\",\n attributes: metricAttributes({\n tool: agentEvent.toolName,\n ...baseAttrs,\n }),\n });\n addLifecycleBreadcrumb(\"agent.tool.completed\", {\n tool: agentEvent.toolName,\n error: agentEvent.isError,\n duration_ms: durationMs,\n ...baseAttrs,\n });\n\n if (agentEvent.isError) {\n log.logToolError(logCtx, agentEvent.toolName, durationMs, resultStr);\n } else {\n log.logToolSuccess(logCtx, agentEvent.toolName, durationMs, resultStr);\n }\n\n // Post args + result to thread\n const label = pending?.args ? (pending.args as { label?: string }).label : undefined;\n const argsFormatted = pending\n ? formatToolArgsForSlack(agentEvent.toolName, pending.args as Record<string, unknown>)\n : \"(args not found)\";\n const duration = (durationMs / 1000).toFixed(1);\n let threadMessage = `*${agentEvent.isError ? \"✗\" : \"✓\"} ${agentEvent.toolName}*`;\n if (label) threadMessage += `: ${label}`;\n threadMessage += ` (${duration}s)\\n`;\n if (argsFormatted) threadMessage += `\\`\\`\\`\\n${argsFormatted}\\n\\`\\`\\`\\n`;\n threadMessage += `*Result:*\\n\\`\\`\\`\\n${resultStr}\\n\\`\\`\\``;\n\n // Only post thread details for tools with meaningful output (bash, attach).\n // Skip read/write/edit to reduce Slack noise — their results are in the log.\n const quietTools = new Set([\"read\", \"write\", \"edit\"]);\n if (!quietTools.has(agentEvent.toolName)) {\n queue.enqueueMessage(threadMessage, \"thread\", \"tool result thread\", false);\n }\n\n if (agentEvent.isError) {\n queue.enqueue(\n () => responseCtx.respond(`_Error: ${truncate(resultStr, 200)}_`),\n \"tool error\",\n );\n }\n } else if (event.type === \"message_start\") {\n const agentEvent = event as AgentEvent & { type: \"message_start\" };\n if (agentEvent.message.role === \"assistant\") {\n runState.llmCallCount += 1;\n addLifecycleBreadcrumb(\"agent.llm.call.started\", {\n call_index: runState.llmCallCount,\n provider: model.provider,\n model: agentConfig.model,\n ...baseAttrs,\n });\n log.logResponseStart(logCtx);\n }\n } else if (event.type === \"message_end\") {\n const agentEvent = event as AgentEvent & { type: \"message_end\" };\n if (agentEvent.message.role === \"assistant\") {\n const assistantMsg = agentEvent.message as any;\n\n if (assistantMsg.stopReason) {\n runState.stopReason = assistantMsg.stopReason;\n }\n if (assistantMsg.errorMessage) {\n runState.errorMessage = assistantMsg.errorMessage;\n }\n\n if (assistantMsg.usage) {\n runState.totalUsage.input += assistantMsg.usage.input;\n runState.totalUsage.output += assistantMsg.usage.output;\n runState.totalUsage.cacheRead += assistantMsg.usage.cacheRead;\n runState.totalUsage.cacheWrite += assistantMsg.usage.cacheWrite;\n runState.totalUsage.cost.input += assistantMsg.usage.cost.input;\n runState.totalUsage.cost.output += assistantMsg.usage.cost.output;\n runState.totalUsage.cost.cacheRead += assistantMsg.usage.cost.cacheRead;\n runState.totalUsage.cost.cacheWrite += assistantMsg.usage.cost.cacheWrite;\n runState.totalUsage.cost.total += assistantMsg.usage.cost.total;\n\n // Per-turn LLM metrics\n const llmAttributes = metricAttributes({\n provider: model.provider,\n model: agentConfig.model,\n ...baseAttrs,\n stop_reason: assistantMsg.stopReason,\n error: Boolean(assistantMsg.errorMessage),\n });\n Sentry.metrics.count(\"agent.llm.calls\", 1, { attributes: llmAttributes });\n Sentry.metrics.distribution(\"agent.llm.tokens_in\", assistantMsg.usage.input, {\n attributes: llmAttributes,\n });\n Sentry.metrics.distribution(\"agent.llm.tokens_out\", assistantMsg.usage.output, {\n attributes: llmAttributes,\n });\n if (assistantMsg.usage.cacheRead > 0) {\n Sentry.metrics.distribution(\"agent.llm.cache_read\", assistantMsg.usage.cacheRead, {\n attributes: llmAttributes,\n });\n }\n if (assistantMsg.usage.cacheWrite > 0) {\n Sentry.metrics.distribution(\"agent.llm.cache_write\", assistantMsg.usage.cacheWrite, {\n attributes: llmAttributes,\n });\n }\n Sentry.metrics.distribution(\"agent.llm.cost_per_turn\", assistantMsg.usage.cost.total, {\n attributes: llmAttributes,\n });\n addLifecycleBreadcrumb(\"agent.llm.call.completed\", {\n call_index: runState.llmCallCount,\n provider: model.provider,\n model: agentConfig.model,\n stop_reason: assistantMsg.stopReason,\n error: Boolean(assistantMsg.errorMessage),\n input_tokens: assistantMsg.usage.input,\n output_tokens: assistantMsg.usage.output,\n cost_total_usd: assistantMsg.usage.cost.total,\n });\n }\n\n const content = agentEvent.message.content;\n const thinkingParts: string[] = [];\n const textParts: string[] = [];\n for (const part of content) {\n if (part.type === \"thinking\") {\n thinkingParts.push((part as any).thinking);\n } else if (part.type === \"text\") {\n textParts.push((part as any).text);\n }\n }\n\n const text = textParts.join(\"\\n\");\n\n for (const thinking of thinkingParts) {\n log.logThinking(logCtx, thinking);\n queue.enqueueMessage(`_${thinking}_`, \"main\", \"thinking main\");\n queue.enqueueMessage(`_${thinking}_`, \"thread\", \"thinking thread\", false);\n }\n\n if (text.trim()) {\n log.logResponse(logCtx, text);\n queue.enqueueMessage(text, \"main\", \"response main\");\n // Only overflow to thread for texts that will be truncated in main\n if (text.length > SLACK_MAX_LENGTH) {\n queue.enqueueMessage(text, \"thread\", \"response thread\", false);\n }\n }\n }\n } else if (event.type === \"compaction_start\") {\n log.logInfo(`Auto-compaction started (reason: ${(event as any).reason})`);\n queue.enqueue(() => responseCtx.respond(\"_Compacting context..._\"), \"compaction start\");\n } else if (event.type === \"compaction_end\") {\n const compEvent = event as any;\n if (compEvent.result) {\n log.logInfo(`Auto-compaction complete: ${compEvent.result.tokensBefore} tokens compacted`);\n } else if (compEvent.aborted) {\n log.logInfo(\"Auto-compaction aborted\");\n }\n } else if (event.type === \"auto_retry_start\") {\n const retryEvent = event as any;\n log.logWarning(\n `Retrying (${retryEvent.attempt}/${retryEvent.maxAttempts})`,\n retryEvent.errorMessage,\n );\n queue.enqueue(\n () =>\n responseCtx.respond(`_Retrying (${retryEvent.attempt}/${retryEvent.maxAttempts})..._`),\n \"retry\",\n );\n }\n });\n\n // Message limit constant\n const SLACK_MAX_LENGTH = 40000;\n const splitForSlack = (text: string): string[] => {\n if (text.length <= SLACK_MAX_LENGTH) return [text];\n const parts: string[] = [];\n let remaining = text;\n let partNum = 1;\n while (remaining.length > 0) {\n const chunk = remaining.substring(0, SLACK_MAX_LENGTH - 50);\n remaining = remaining.substring(SLACK_MAX_LENGTH - 50);\n const suffix = remaining.length > 0 ? `\\n_(continued ${partNum}...)_` : \"\";\n parts.push(chunk + suffix);\n partNum++;\n }\n return parts;\n };\n\n return {\n async run(\n message: ChatMessage,\n responseCtx: ChatResponseContext,\n platform: PlatformInfo,\n ): Promise<{ stopReason: string; errorMessage?: string }> {\n // Extract conversationId from sessionKey (format: \"conversationId:rootTs\" or just \"conversationId\")\n const sessionConversation = message.sessionKey.split(\":\")[0];\n\n // Ensure conversation directory exists\n await mkdir(conversationDir, { recursive: true });\n\n if (executionResolver) {\n executionResolver.refresh();\n activeExecutor = await executionResolver.resolve({\n platform: platform.name,\n userId: message.userId,\n });\n workspacePath = getWorkspacePath();\n }\n\n // Sync messages from log.jsonl that arrived while we were offline or busy\n // Exclude the current message (it will be added via prompt())\n // Default sync range is 10 days (handled by syncLogToSessionManager)\n // Thread filter ensures only messages from this session's thread are synced\n const threadFilter = message.sessionKey.includes(\":\")\n ? { scope: \"thread\" as const, rootTs, threadTs: message.threadTs }\n : { scope: \"top-level\" as const, rootTs };\n const syncedCount = await syncLogToSessionManager(\n sessionManager,\n conversationDir,\n message.id,\n undefined,\n threadFilter,\n );\n if (syncedCount > 0) {\n log.logInfo(`[${conversationId}] Synced ${syncedCount} messages from log.jsonl`);\n }\n\n // Reload messages from context.jsonl\n // This picks up any messages synced above\n const reloadedSession = sessionManager.buildSessionContext();\n if (reloadedSession.messages.length > 0) {\n agent.state.messages = reloadedSession.messages;\n log.logInfo(\n `[${conversationId}] Reloaded ${reloadedSession.messages.length} messages from context`,\n );\n }\n\n // Update system prompt with fresh memory, channel/user info, and skills\n const memory = await getMemory(conversationDir);\n const skills = loadMamaSkills(conversationDir, workspacePath);\n const systemPrompt = buildSystemPrompt(\n workspacePath,\n conversationId,\n message.conversationKind,\n message.userId,\n memory,\n executor.getSandboxConfig(),\n platform,\n skills,\n );\n session.agent.state.systemPrompt = systemPrompt;\n\n setEventContext({\n platform: platform.name,\n conversationId,\n conversationKind: message.conversationKind,\n userId: message.userId,\n });\n\n // Set up file upload function\n setUploadFunction(async (filePath: string, title?: string) => {\n const hostPath = translateToHostPath(\n filePath,\n conversationDir,\n workspacePath,\n conversationId,\n );\n await responseCtx.uploadFile(hostPath, title);\n });\n\n // Reset per-run state\n runState.responseCtx = responseCtx;\n runState.logCtx = {\n conversationId: sessionConversation,\n userName: message.userName,\n conversationName: undefined,\n sessionId: sessionUuid,\n };\n runState.pendingTools.clear();\n runState.totalUsage = {\n input: 0,\n output: 0,\n cacheRead: 0,\n cacheWrite: 0,\n cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n };\n runState.llmCallCount = 0;\n runState.stopReason = \"stop\";\n runState.errorMessage = undefined;\n\n // Create queue for this run\n let queueChain = Promise.resolve();\n runState.queue = {\n enqueue(fn: () => Promise<void>, errorContext: string): void {\n queueChain = queueChain.then(async () => {\n try {\n await fn();\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(`API error (${errorContext})`, errMsg);\n try {\n // Split long error messages to avoid msg_too_long\n const errParts = splitForSlack(`_Error: ${errMsg}_`);\n for (const part of errParts) {\n await responseCtx.respondInThread(part);\n }\n } catch {\n // Ignore\n }\n }\n });\n },\n enqueueMessage(\n text: string,\n target: \"main\" | \"thread\",\n errorContext: string,\n _doLog = true,\n ): void {\n const parts = splitForSlack(text);\n for (const part of parts) {\n this.enqueue(\n () =>\n target === \"main\" ? responseCtx.respond(part) : responseCtx.respondInThread(part),\n errorContext,\n );\n }\n },\n };\n\n // Log context info\n log.logInfo(\n `Context sizes - system: ${systemPrompt.length} chars, memory: ${memory.length} chars`,\n );\n log.logInfo(`Channels: ${platform.channels.length}, Users: ${platform.users.length}`);\n\n // Build user message with timestamp and username prefix\n // Format: \"[YYYY-MM-DD HH:MM:SS+HH:MM] [username]: message\" so LLM knows when and who\n const now = new Date();\n const pad = (n: number) => n.toString().padStart(2, \"0\");\n const offset = -now.getTimezoneOffset();\n const offsetSign = offset >= 0 ? \"+\" : \"-\";\n const offsetHours = pad(Math.floor(Math.abs(offset) / 60));\n const offsetMins = pad(Math.abs(offset) % 60);\n const timestamp = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}${offsetSign}${offsetHours}:${offsetMins}`;\n const threadContext = message.threadTs ? ` [in-thread:${message.threadTs}]` : \"\";\n let userMessage = `[${timestamp}] [${message.userName || \"unknown\"}]${threadContext}: ${message.text}`;\n\n const imageAttachments: ImageContent[] = [];\n const nonImagePaths: string[] = [];\n\n for (const a of message.attachments || []) {\n // a.localPath is the path relative to the workspace.\n const fullPath = `${workspacePath}/${a.localPath}`;\n const mimeType = getImageMimeType(a.localPath);\n\n if (mimeType && existsSync(fullPath)) {\n try {\n imageAttachments.push({\n type: \"image\",\n mimeType,\n data: readFileSync(fullPath).toString(\"base64\"),\n });\n } catch {\n nonImagePaths.push(fullPath);\n }\n } else {\n nonImagePaths.push(fullPath);\n }\n }\n\n if (nonImagePaths.length > 0) {\n userMessage += `\\n\\n<slack_attachments>\\n${nonImagePaths.join(\"\\n\")}\\n</slack_attachments>`;\n }\n\n // Debug: write context to last_prompt.jsonl\n const debugContext = {\n systemPrompt,\n messages: session.messages,\n newUserMessage: userMessage,\n imageAttachmentCount: imageAttachments.length,\n };\n await writeFile(\n join(conversationDir, \"last_prompt.jsonl\"),\n JSON.stringify(debugContext, null, 2),\n );\n addLifecycleBreadcrumb(\"agent.prompt.sent\", {\n provider: model.provider,\n model: agentConfig.model,\n channel_id: sessionConversation,\n session_id: sessionUuid,\n attachment_count: message.attachments?.length ?? 0,\n image_attachment_count: imageAttachments.length,\n });\n\n await session.prompt(\n userMessage,\n imageAttachments.length > 0 ? { images: imageAttachments } : undefined,\n );\n\n // Wait for queued messages\n await queueChain;\n\n // Handle error case - update main message and post error to thread\n if (runState.stopReason === \"error\" && runState.errorMessage) {\n try {\n await responseCtx.replaceResponse(\"_Sorry, something went wrong_\");\n // Split long error messages to avoid msg_too_long\n const errorParts = splitForSlack(`_Error: ${runState.errorMessage}_`);\n for (const part of errorParts) {\n await responseCtx.respondInThread(part);\n }\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(\"Failed to post error message\", errMsg);\n }\n } else {\n // Final message update\n const messages = session.messages;\n const lastAssistant = messages.filter((m) => m.role === \"assistant\").pop();\n const finalText =\n lastAssistant?.content\n .filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n .map((c) => c.text)\n .join(\"\\n\") || \"\";\n\n // Check for [SILENT] marker - delete message and thread instead of posting\n if (finalText.trim() === \"[SILENT]\" || finalText.trim().startsWith(\"[SILENT]\")) {\n try {\n await responseCtx.deleteResponse();\n log.logInfo(\"Silent response - deleted message and thread\");\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(\"Failed to delete message for silent response\", errMsg);\n }\n } else if (finalText.trim()) {\n try {\n const mainText =\n finalText.length > SLACK_MAX_LENGTH\n ? `${finalText.substring(0, SLACK_MAX_LENGTH - 50)}\\n\\n_(see thread for full response)_`\n : finalText;\n await responseCtx.replaceResponse(mainText);\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(\"Failed to replace message with final text\", errMsg);\n }\n }\n }\n\n // Log usage summary with context info\n if (runState.totalUsage.cost.total > 0) {\n // Get last non-aborted assistant message for context calculation\n const messages = session.messages;\n const lastAssistantMessage = messages\n .slice()\n .reverse()\n .find((m) => m.role === \"assistant\" && (m as any).stopReason !== \"aborted\") as any;\n\n const contextTokens = lastAssistantMessage\n ? lastAssistantMessage.usage.input +\n lastAssistantMessage.usage.output +\n lastAssistantMessage.usage.cacheRead +\n lastAssistantMessage.usage.cacheWrite\n : 0;\n const contextWindow = model.contextWindow || 200000;\n\n // Run-level Sentry metrics\n const { totalUsage } = runState;\n const runMetricAttributes = metricAttributes({\n provider: model.provider,\n model: agentConfig.model,\n channel_id: sessionConversation,\n session_id: sessionUuid,\n stop_reason: runState.stopReason,\n llm_calls: runState.llmCallCount,\n });\n Sentry.metrics.distribution(\"agent.run.tokens_in\", totalUsage.input, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.distribution(\"agent.run.tokens_out\", totalUsage.output, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.distribution(\"agent.run.cache_read\", totalUsage.cacheRead, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.distribution(\"agent.run.cache_write\", totalUsage.cacheWrite, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.distribution(\"agent.run.cost\", totalUsage.cost.total, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.gauge(\"agent.context.utilization\", contextTokens / contextWindow, {\n unit: \"ratio\",\n attributes: runMetricAttributes,\n });\n\n const summary = log.logUsageSummary(\n runState.logCtx!,\n runState.totalUsage,\n contextTokens,\n contextWindow,\n );\n // Split long summaries to avoid msg_too_long\n const summaryParts = splitForSlack(summary);\n for (const part of summaryParts) {\n runState.queue!.enqueue(\n () => responseCtx.respondInThread(part, { style: \"muted\" }),\n \"usage summary\",\n );\n }\n await queueChain;\n }\n\n // Clear run state\n runState.responseCtx = null;\n runState.logCtx = null;\n runState.queue = null;\n\n return { stopReason: runState.stopReason, errorMessage: runState.errorMessage };\n },\n\n abort(): void {\n session.abort();\n },\n\n getCurrentStep(): { toolName?: string; label?: string } | undefined {\n const pending = runState.pendingTools;\n if (pending.size === 0) return undefined;\n // Get the first pending tool\n const first = pending.values().next().value;\n if (!first) return undefined;\n return {\n toolName: first.toolName,\n label: (first.args as { label?: string })?.label,\n };\n },\n };\n}\n\n/**\n * Translate container path back to host path for file operations\n */\nfunction translateToHostPath(\n containerPath: string,\n conversationDir: string,\n workspacePath: string,\n conversationId: string,\n): string {\n if (workspacePath === \"/workspace\") {\n const prefix = `/workspace/${conversationId}/`;\n if (containerPath.startsWith(prefix)) {\n return join(conversationDir, containerPath.slice(prefix.length));\n }\n if (containerPath.startsWith(\"/workspace/\")) {\n return join(conversationDir, \"..\", containerPath.slice(\"/workspace/\".length));\n }\n }\n return containerPath;\n}\n"]}
|
package/dist/agent.js
CHANGED
|
@@ -87,11 +87,12 @@ function loadMamaSkills(conversationDir, workspacePath) {
|
|
|
87
87
|
}
|
|
88
88
|
return Array.from(skillMap.values());
|
|
89
89
|
}
|
|
90
|
-
function buildSystemPrompt(workspacePath, conversationId, currentUserId, memory, sandboxConfig, platform, skills) {
|
|
90
|
+
function buildSystemPrompt(workspacePath, conversationId, conversationKind, currentUserId, memory, sandboxConfig, platform, skills) {
|
|
91
91
|
const conversationPath = `${workspacePath}/${conversationId}`;
|
|
92
92
|
const isContainer = sandboxConfig.type === "container" || sandboxConfig.type === "image";
|
|
93
|
+
const isImageSandbox = sandboxConfig.type === "image";
|
|
93
94
|
const isFirecracker = sandboxConfig.type === "firecracker";
|
|
94
|
-
// Format
|
|
95
|
+
// Format channel mappings
|
|
95
96
|
const channelMappings = platform.channels.length > 0
|
|
96
97
|
? platform.channels.map((c) => `${c.id}\t#${c.name}`).join("\n")
|
|
97
98
|
: "(no channels loaded)";
|
|
@@ -99,17 +100,22 @@ function buildSystemPrompt(workspacePath, conversationId, currentUserId, memory,
|
|
|
99
100
|
const userMappings = platform.users.length > 0
|
|
100
101
|
? platform.users.map((u) => `${u.id}\t@${u.userName}\t${u.displayName}`).join("\n")
|
|
101
102
|
: "(no users loaded)";
|
|
102
|
-
const envDescription =
|
|
103
|
-
? `You are running inside a
|
|
103
|
+
const envDescription = isImageSandbox
|
|
104
|
+
? `You are running inside a managed per-user container.
|
|
104
105
|
- Bash working directory: / (use cd or absolute paths)
|
|
105
|
-
- Install tools with
|
|
106
|
+
- Install tools with the image's package manager
|
|
107
|
+
- Your changes persist for this user's container until it is recreated`
|
|
108
|
+
: isContainer
|
|
109
|
+
? `You are running inside a shared container.
|
|
110
|
+
- Bash working directory: / (use cd or absolute paths)
|
|
111
|
+
- Install tools with the container's package manager
|
|
106
112
|
- Your changes persist across sessions`
|
|
107
|
-
|
|
108
|
-
|
|
113
|
+
: isFirecracker
|
|
114
|
+
? `You are running inside a Firecracker microVM.
|
|
109
115
|
- Bash working directory: / (use cd or absolute paths)
|
|
110
116
|
- Install tools with: apt-get install <package> (Debian-based)
|
|
111
117
|
- Your changes persist across sessions`
|
|
112
|
-
|
|
118
|
+
: `You are running directly on the host machine.
|
|
113
119
|
- Bash working directory: ${process.cwd()}
|
|
114
120
|
- Be careful with system modifications`;
|
|
115
121
|
return `You are mama, a ${platform.name} bot assistant. Be concise. No emojis.
|
|
@@ -134,7 +140,7 @@ ${envDescription}
|
|
|
134
140
|
|
|
135
141
|
## Workspace Layout
|
|
136
142
|
${workspacePath}/
|
|
137
|
-
├── MEMORY.md # Global memory (all
|
|
143
|
+
├── MEMORY.md # Global memory (all conversations)
|
|
138
144
|
├── skills/ # Global CLI tools you create
|
|
139
145
|
└── ${conversationId}/ # This conversation
|
|
140
146
|
├── MEMORY.md # Conversation-specific memory
|
|
@@ -174,21 +180,19 @@ You can schedule events that wake you up at specific times or when external thin
|
|
|
174
180
|
|
|
175
181
|
**Immediate** - Triggers as soon as harness sees the file. Use in scripts/webhooks to signal external events.
|
|
176
182
|
\`\`\`json
|
|
177
|
-
{"type": "immediate", "platform": "${platform.name}", "
|
|
183
|
+
{"type": "immediate", "platform": "${platform.name}", "conversationId": "${conversationId}", "conversationKind": "${conversationKind}", "userId": "${currentUserId ?? "<requester userId>"}", "text": "New GitHub issue opened"}
|
|
178
184
|
\`\`\`
|
|
179
185
|
|
|
180
186
|
**One-shot** - Triggers once at a specific time. Use for reminders.
|
|
181
187
|
\`\`\`json
|
|
182
|
-
{"type": "one-shot", "platform": "${platform.name}", "
|
|
188
|
+
{"type": "one-shot", "platform": "${platform.name}", "conversationId": "${conversationId}", "conversationKind": "${conversationKind}", "userId": "${currentUserId ?? "<requester userId>"}", "text": "Remind Mario about dentist", "at": "2025-12-15T09:00:00+01:00"}
|
|
183
189
|
\`\`\`
|
|
184
190
|
|
|
185
191
|
**Periodic** - Triggers on a cron schedule. Use for recurring tasks.
|
|
186
192
|
\`\`\`json
|
|
187
|
-
{"type": "periodic", "platform": "${platform.name}", "
|
|
193
|
+
{"type": "periodic", "platform": "${platform.name}", "conversationId": "${conversationId}", "conversationKind": "${conversationKind}", "userId": "${currentUserId ?? "<requester userId>"}", "text": "Check inbox and summarize", "schedule": "0 9 * * 1-5", "timezone": "${Intl.DateTimeFormat().resolvedOptions().timeZone}"}
|
|
188
194
|
\`\`\`
|
|
189
195
|
|
|
190
|
-
Set \`userId\` to the platform userId of whoever asked for the event (look it up in the user mappings above). When the event fires, tool execution will route to the sandbox vault selection for that user so the right credentials are available. In shared container mode, all events use the container's single shared vault.
|
|
191
|
-
|
|
192
196
|
### Cron Format
|
|
193
197
|
\`minute hour day-of-month month day-of-week\`
|
|
194
198
|
- \`0 9 * * *\` = daily at 9:00
|
|
@@ -199,23 +203,18 @@ Set \`userId\` to the platform userId of whoever asked for the event (look it up
|
|
|
199
203
|
### Timezones
|
|
200
204
|
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}.
|
|
201
205
|
|
|
202
|
-
### Platform Routing
|
|
206
|
+
### Platform and Credential Routing
|
|
203
207
|
Set \`platform\` to the target bot platform (\`${platform.name}\` for this conversation). When only one platform is running, omitting \`platform\` is allowed for backward compatibility, but include it by default to avoid ambiguity.
|
|
204
208
|
|
|
205
|
-
|
|
206
|
-
Prefer the \`event\` tool. It automatically writes to the correct events directory and fills the current \`platform\`, \`channelId\`, and requester \`userId\`.
|
|
207
|
-
Do not use \`bash\` or \`write\` to hand-create JSON files in \`/events\` unless the user explicitly asks for manual file editing.
|
|
209
|
+
Set \`userId\` to the platform userId of whoever asked for the event. When the event fires, tool execution routes using that user's vault selection in per-user modes. In \`container:<name>\`, events use the container's single shared vault.
|
|
208
210
|
|
|
209
|
-
|
|
210
|
-
- \`platform\`: \`${platform.name}\`
|
|
211
|
-
- \`channelId\`: \`${conversationId}\`
|
|
212
|
-
- \`userId\`: \`${currentUserId ?? "unknown"}\`
|
|
211
|
+
Prefer the \`event\` tool over manually writing JSON files; it fills \`platform\`, \`conversationId\`, \`conversationKind\`, and \`userId\` for the current conversation automatically.
|
|
213
212
|
|
|
214
|
-
|
|
213
|
+
### Creating Events
|
|
215
214
|
Use unique filenames to avoid overwriting existing events. Include a timestamp or random suffix:
|
|
216
215
|
\`\`\`bash
|
|
217
216
|
cat > ${workspacePath}/events/dentist-reminder-$(date +%s).json << 'EOF'
|
|
218
|
-
{"type": "one-shot", "platform": "${platform.name}", "
|
|
217
|
+
{"type": "one-shot", "platform": "${platform.name}", "conversationId": "${conversationId}", "conversationKind": "${conversationKind}", "userId": "${currentUserId ?? "<requester userId>"}", "text": "Dentist tomorrow", "at": "2025-12-14T09:00:00+01:00"}
|
|
219
218
|
EOF
|
|
220
219
|
\`\`\`
|
|
221
220
|
Or check if file exists first before creating.
|
|
@@ -343,24 +342,25 @@ function formatToolArgsForSlack(_toolName, args) {
|
|
|
343
342
|
// Agent runner
|
|
344
343
|
// ============================================================================
|
|
345
344
|
/**
|
|
346
|
-
* Create a new AgentRunner for a
|
|
345
|
+
* Create a new AgentRunner for a channel.
|
|
347
346
|
* Sets up the session and subscribes to events once.
|
|
348
347
|
*
|
|
349
|
-
* Runner caching is handled by the caller (
|
|
348
|
+
* Runner caching is handled by the caller (channelStates in main.ts).
|
|
350
349
|
* This is a stateless factory function.
|
|
351
350
|
*/
|
|
352
|
-
export async function createRunner(sandboxConfig, sessionKey, conversationId, conversationDir, workspaceDir, vaultManager, bindingStore, provisioner
|
|
353
|
-
const agentConfig = loadAgentConfig(
|
|
351
|
+
export async function createRunner(sandboxConfig, sessionKey, conversationId, conversationDir, workspaceDir, vaultManager, bindingStore, provisioner) {
|
|
352
|
+
const agentConfig = loadAgentConfig(workspaceDir);
|
|
354
353
|
// Initialize logger with settings from config
|
|
355
354
|
log.initLogger({
|
|
356
355
|
logFormat: agentConfig.logFormat,
|
|
357
356
|
logLevel: agentConfig.logLevel,
|
|
358
357
|
});
|
|
359
358
|
const executionResolver = vaultManager &&
|
|
359
|
+
sandboxConfig.type !== "host" &&
|
|
360
360
|
(vaultManager.isEnabled() ||
|
|
361
361
|
!!bindingStore ||
|
|
362
|
-
sandboxConfig.type === "
|
|
363
|
-
sandboxConfig.type === "
|
|
362
|
+
sandboxConfig.type === "container" ||
|
|
363
|
+
sandboxConfig.type === "image")
|
|
364
364
|
? new ActorExecutionResolver(sandboxConfig, vaultManager, bindingStore, provisioner)
|
|
365
365
|
: undefined;
|
|
366
366
|
let activeExecutor = executionResolver !== undefined
|
|
@@ -378,7 +378,6 @@ export async function createRunner(sandboxConfig, sessionKey, conversationId, co
|
|
|
378
378
|
},
|
|
379
379
|
};
|
|
380
380
|
const workspaceBase = conversationDir.replace(`/${conversationId}`, "");
|
|
381
|
-
// Compute workspace path from the current executor. This may change per run.
|
|
382
381
|
const getWorkspacePath = () => executor.getWorkspacePath(workspaceBase);
|
|
383
382
|
let workspacePath = getWorkspacePath();
|
|
384
383
|
// Create tools (per-runner, with per-runner upload function setter)
|
|
@@ -397,45 +396,45 @@ export async function createRunner(sandboxConfig, sessionKey, conversationId, co
|
|
|
397
396
|
channels: [],
|
|
398
397
|
users: [],
|
|
399
398
|
};
|
|
400
|
-
const systemPrompt = buildSystemPrompt(workspacePath, conversationId, undefined, memory, sandboxConfig, emptyPlatform, skills);
|
|
401
|
-
// Create session manager and settings manager
|
|
402
|
-
//
|
|
403
|
-
// Thread sessions use fixed files: {conversationDir}/sessions/{threadTs}.jsonl
|
|
399
|
+
const systemPrompt = buildSystemPrompt(workspacePath, conversationId, "shared", undefined, memory, sandboxConfig, emptyPlatform, skills);
|
|
400
|
+
// Create session manager and settings manager
|
|
401
|
+
// Conversation sessions use {conversationDir}/sessions/current.
|
|
402
|
+
// Thread sessions use fixed files: {conversationDir}/sessions/{threadTs}.jsonl
|
|
404
403
|
const sessionDir = getChannelSessionDir(conversationDir);
|
|
405
404
|
const isThread = sessionKey.includes(":");
|
|
406
405
|
let sessionManager;
|
|
407
|
-
let
|
|
406
|
+
let contextFile;
|
|
408
407
|
if (isThread) {
|
|
409
408
|
const threadFile = getThreadSessionFile(conversationDir, sessionKey);
|
|
410
409
|
const existing = tryResolveThreadSession(threadFile);
|
|
411
410
|
if (existing) {
|
|
412
|
-
|
|
413
|
-
sessionManager = openManagedSession(
|
|
411
|
+
contextFile = existing;
|
|
412
|
+
sessionManager = openManagedSession(contextFile, sessionDir, conversationDir);
|
|
414
413
|
}
|
|
415
414
|
else {
|
|
416
415
|
const conversationSource = resolveChannelSessionFile(conversationDir);
|
|
417
416
|
if (conversationSource) {
|
|
418
417
|
try {
|
|
419
|
-
|
|
420
|
-
sessionManager = openManagedSession(
|
|
418
|
+
contextFile = forkThreadSessionFile(conversationSource, threadFile, conversationDir);
|
|
419
|
+
sessionManager = openManagedSession(contextFile, sessionDir, conversationDir);
|
|
421
420
|
}
|
|
422
421
|
catch {
|
|
423
|
-
|
|
424
|
-
sessionManager = openManagedSession(
|
|
422
|
+
contextFile = createManagedSessionFileAtPath(threadFile, conversationDir);
|
|
423
|
+
sessionManager = openManagedSession(contextFile, sessionDir, conversationDir);
|
|
425
424
|
}
|
|
426
425
|
}
|
|
427
426
|
else {
|
|
428
|
-
|
|
429
|
-
sessionManager = openManagedSession(
|
|
427
|
+
contextFile = createManagedSessionFileAtPath(threadFile, conversationDir);
|
|
428
|
+
sessionManager = openManagedSession(contextFile, sessionDir, conversationDir);
|
|
430
429
|
}
|
|
431
430
|
}
|
|
432
431
|
}
|
|
433
432
|
else {
|
|
434
|
-
//
|
|
435
|
-
|
|
436
|
-
sessionManager = openManagedSession(
|
|
433
|
+
// Direct/shared session: normal resolve
|
|
434
|
+
contextFile = resolveManagedSessionFile(sessionDir, conversationDir);
|
|
435
|
+
sessionManager = openManagedSession(contextFile, sessionDir, conversationDir);
|
|
437
436
|
}
|
|
438
|
-
const sessionUuid = extractSessionUuid(
|
|
437
|
+
const sessionUuid = extractSessionUuid(contextFile);
|
|
439
438
|
// Used for Slack thread filtering — for non-Slack platforms this is effectively a no-op
|
|
440
439
|
const rootTs = extractSessionSuffix(sessionKey);
|
|
441
440
|
const settingsManager = createMamaSettingsManager(join(conversationDir, ".."));
|
|
@@ -463,7 +462,7 @@ export async function createRunner(sandboxConfig, sessionKey, conversationId, co
|
|
|
463
462
|
const loadedSession = sessionManager.buildSessionContext();
|
|
464
463
|
if (loadedSession.messages.length > 0) {
|
|
465
464
|
agent.state.messages = loadedSession.messages;
|
|
466
|
-
log.logInfo(`[${conversationId}] Loaded ${loadedSession.messages.length} messages from
|
|
465
|
+
log.logInfo(`[${conversationId}] Loaded ${loadedSession.messages.length} messages from context.jsonl`);
|
|
467
466
|
}
|
|
468
467
|
// Load extensions, skills, prompts, themes via DefaultResourceLoader
|
|
469
468
|
// This reads ~/.pi/agent/settings.json (packages, extensions enable/disable)
|
|
@@ -731,13 +730,9 @@ export async function createRunner(sandboxConfig, sessionKey, conversationId, co
|
|
|
731
730
|
return {
|
|
732
731
|
async run(message, responseCtx, platform) {
|
|
733
732
|
// Extract conversationId from sessionKey (format: "conversationId:rootTs" or just "conversationId")
|
|
734
|
-
const
|
|
735
|
-
// Ensure
|
|
733
|
+
const sessionConversation = message.sessionKey.split(":")[0];
|
|
734
|
+
// Ensure conversation directory exists
|
|
736
735
|
await mkdir(conversationDir, { recursive: true });
|
|
737
|
-
// Refresh vault config and clear executor cache so credential changes
|
|
738
|
-
// (env file updates, vault.json edits, token rotations) take effect.
|
|
739
|
-
// Then set the active actor BEFORE building system prompt, so workspacePath
|
|
740
|
-
// reflects the actor's sandbox type.
|
|
741
736
|
if (executionResolver) {
|
|
742
737
|
executionResolver.refresh();
|
|
743
738
|
activeExecutor = await executionResolver.resolve({
|
|
@@ -757,35 +752,33 @@ export async function createRunner(sandboxConfig, sessionKey, conversationId, co
|
|
|
757
752
|
if (syncedCount > 0) {
|
|
758
753
|
log.logInfo(`[${conversationId}] Synced ${syncedCount} messages from log.jsonl`);
|
|
759
754
|
}
|
|
760
|
-
// Reload messages from
|
|
761
|
-
// This picks up any messages synced above
|
|
755
|
+
// Reload messages from context.jsonl
|
|
756
|
+
// This picks up any messages synced above
|
|
762
757
|
const reloadedSession = sessionManager.buildSessionContext();
|
|
763
758
|
if (reloadedSession.messages.length > 0) {
|
|
764
759
|
agent.state.messages = reloadedSession.messages;
|
|
765
760
|
log.logInfo(`[${conversationId}] Reloaded ${reloadedSession.messages.length} messages from context`);
|
|
766
761
|
}
|
|
767
762
|
// Update system prompt with fresh memory, channel/user info, and skills
|
|
768
|
-
// Use the actual executor's sandbox config, not the initial config,
|
|
769
|
-
// to ensure accurate environment description for the model
|
|
770
763
|
const memory = await getMemory(conversationDir);
|
|
771
764
|
const skills = loadMamaSkills(conversationDir, workspacePath);
|
|
772
|
-
const
|
|
773
|
-
const systemPrompt = buildSystemPrompt(workspacePath, conversationId, message.userId, memory, actualSandboxConfig, platform, skills);
|
|
765
|
+
const systemPrompt = buildSystemPrompt(workspacePath, conversationId, message.conversationKind, message.userId, memory, executor.getSandboxConfig(), platform, skills);
|
|
774
766
|
session.agent.state.systemPrompt = systemPrompt;
|
|
775
|
-
// Set up file upload function
|
|
776
|
-
setUploadFunction(async (filePath, title) => {
|
|
777
|
-
const hostPath = translateToHostPath(filePath, conversationDir, workspacePath, conversationId);
|
|
778
|
-
await responseCtx.uploadFile(hostPath, title);
|
|
779
|
-
});
|
|
780
767
|
setEventContext({
|
|
781
768
|
platform: platform.name,
|
|
782
769
|
conversationId,
|
|
770
|
+
conversationKind: message.conversationKind,
|
|
783
771
|
userId: message.userId,
|
|
784
772
|
});
|
|
773
|
+
// Set up file upload function
|
|
774
|
+
setUploadFunction(async (filePath, title) => {
|
|
775
|
+
const hostPath = translateToHostPath(filePath, conversationDir, workspacePath, conversationId);
|
|
776
|
+
await responseCtx.uploadFile(hostPath, title);
|
|
777
|
+
});
|
|
785
778
|
// Reset per-run state
|
|
786
779
|
runState.responseCtx = responseCtx;
|
|
787
780
|
runState.logCtx = {
|
|
788
|
-
conversationId:
|
|
781
|
+
conversationId: sessionConversation,
|
|
789
782
|
userName: message.userName,
|
|
790
783
|
conversationName: undefined,
|
|
791
784
|
sessionId: sessionUuid,
|
|
@@ -849,7 +842,7 @@ export async function createRunner(sandboxConfig, sessionKey, conversationId, co
|
|
|
849
842
|
const imageAttachments = [];
|
|
850
843
|
const nonImagePaths = [];
|
|
851
844
|
for (const a of message.attachments || []) {
|
|
852
|
-
// a.localPath is the path relative to the workspace
|
|
845
|
+
// a.localPath is the path relative to the workspace.
|
|
853
846
|
const fullPath = `${workspacePath}/${a.localPath}`;
|
|
854
847
|
const mimeType = getImageMimeType(a.localPath);
|
|
855
848
|
if (mimeType && existsSync(fullPath)) {
|
|
@@ -882,7 +875,7 @@ export async function createRunner(sandboxConfig, sessionKey, conversationId, co
|
|
|
882
875
|
addLifecycleBreadcrumb("agent.prompt.sent", {
|
|
883
876
|
provider: model.provider,
|
|
884
877
|
model: agentConfig.model,
|
|
885
|
-
channel_id:
|
|
878
|
+
channel_id: sessionConversation,
|
|
886
879
|
session_id: sessionUuid,
|
|
887
880
|
attachment_count: message.attachments?.length ?? 0,
|
|
888
881
|
image_attachment_count: imageAttachments.length,
|
|
@@ -957,7 +950,7 @@ export async function createRunner(sandboxConfig, sessionKey, conversationId, co
|
|
|
957
950
|
const runMetricAttributes = metricAttributes({
|
|
958
951
|
provider: model.provider,
|
|
959
952
|
model: agentConfig.model,
|
|
960
|
-
channel_id:
|
|
953
|
+
channel_id: sessionConversation,
|
|
961
954
|
session_id: sessionUuid,
|
|
962
955
|
stop_reason: runState.stopReason,
|
|
963
956
|
llm_calls: runState.llmCallCount,
|