@jcheesepkg/nanobot 0.7.7 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.mts","names":[],"sources":["../../src/agent/context.ts"],"mappings":";;;;;KA0CY,aAAA;EACV,IAAA;EACA,KAAA;EACA,QAAA;EACA,IAAA;AAAA;;;;cAsDW,cAAA;EAAA,QACH,SAAA;EAAA,SACC,MAAA,EAAQ,WAAA;EAAA,SACR,MAAA,EAAQ,YAAA;cAEL,SAAA;;EAOZ,iBAAA,CAAA;EAAA,QAyCQ,WAAA;EAAA,QAqEA,kBAAA;EAmBJ;EANJ,aAAA,CAAc,MAAA;IACZ,OAAA,EAAS,WAAA;IACT,cAAA;IACA,KAAA;IACA,OAAA;IACA,MAAA;EAAA,IACE,WAAA;EAAA,QAyBI,gBAAA;EApKC;EAyMT,aAAA,CACE,QAAA,EAAU,WAAA,IACV,UAAA,UACA,QAAA,UACA,MAAA,WACC,WAAA;EA7MM;EAwNT,mBAAA,CACE,QAAA,EAAU,WAAA,IACV,OAAA,iBACA,SAAA,GAAY,KAAA;IACV,EAAA;IACA,IAAA;IACA,QAAA;MAAY,IAAA;MAAc,SAAA;IAAA;EAAA,KAE3B,WAAA;AAAA"}
1
+ {"version":3,"file":"context.d.mts","names":[],"sources":["../../src/agent/context.ts"],"mappings":";;;;;KA0CY,aAAA;EACV,IAAA;EACA,KAAA;EACA,QAAA;EACA,IAAA;AAAA;;;;cAsDW,cAAA;EAAA,QACH,SAAA;EAAA,SACC,MAAA,EAAQ,WAAA;EAAA,SACR,MAAA,EAAQ,YAAA;cAEL,SAAA;;EAOZ,iBAAA,CAAA;EAAA,QAyCQ,WAAA;EAAA,QAsEA,kBAAA;EAmBJ;EANJ,aAAA,CAAc,MAAA;IACZ,OAAA,EAAS,WAAA;IACT,cAAA;IACA,KAAA;IACA,OAAA;IACA,MAAA;EAAA,IACE,WAAA;EAAA,QAyBI,gBAAA;EArKC;EA0MT,aAAA,CACE,QAAA,EAAU,WAAA,IACV,UAAA,UACA,QAAA,UACA,MAAA,WACC,WAAA;EA9MM;EAyNT,mBAAA,CACE,QAAA,EAAU,WAAA,IACV,OAAA,iBACA,SAAA,GAAY,KAAA;IACV,EAAA;IACA,IAAA;IACA,QAAA;MAAY,IAAA;MAAc,SAAA;IAAA;EAAA,KAE3B,WAAA;AAAA"}
@@ -133,8 +133,8 @@ Your workspace is at: ${this.workspace}
133
133
  - Persona: ${this.workspace}/SOUL.md
134
134
  - Instructions: ${this.workspace}/AGENTS.md
135
135
  - Heartbeat: ${this.workspace}/HEARTBEAT.md
136
- - Memory files: ${this.workspace}/memory/MEMORY.md
137
- - Daily notes: ${this.workspace}/memory/YYYY-MM-DD.md
136
+ - Long-term memory: ${this.workspace}/memory/MEMORY.md
137
+ - History log: ${this.workspace}/memory/HISTORY.md (grep-searchable)
138
138
  - Custom skills: ${this.workspace}/skills/{skill-name}/SKILL.md
139
139
 
140
140
  IMPORTANT: When responding to direct questions or conversations, reply directly with your text response.
@@ -166,7 +166,8 @@ Only do this ONCE — if both files already have real values, skip onboarding.
166
166
  If SOUL.md is present, embody its persona and tone. Avoid stiff, generic replies; follow its guidance unless higher-priority instructions override it.
167
167
 
168
168
  Always be helpful, accurate, and concise. When using tools, explain what you're doing.
169
- When remembering something, write to ${this.workspace}/memory/MEMORY.md`;
169
+ When remembering something important, write to ${this.workspace}/memory/MEMORY.md
170
+ To recall past events, grep ${this.workspace}/memory/HISTORY.md`;
170
171
  }
171
172
  loadBootstrapFiles() {
172
173
  const parts = [];
@@ -1 +1 @@
1
- {"version":3,"file":"context.mjs","names":[],"sources":["../../src/agent/context.ts"],"sourcesContent":["import { readFileSync, existsSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport type { ChatMessage, ContentPart } from \"../providers/base.js\";\nimport { MemoryStore } from \"./memory.js\";\nimport { SkillsLoader } from \"./skills.js\";\n\n/**\n * Resolve user timezone from USER.md.\n * Looks for a line like: Timezone: Asia/Tokyo\n * Falls back to host detection / UTC.\n */\nfunction resolveTimezone(workspace: string): string {\n const userMd = join(workspace, \"USER.md\");\n if (existsSync(userMd)) {\n try {\n const content = readFileSync(userMd, \"utf-8\");\n const tzMatch = content.match(/^timezone:\\s*(.+)/im);\n if (tzMatch) {\n const tz = tzMatch[1].trim();\n if (tz && !tz.includes(\"not set\")) {\n // Validate IANA timezone\n new Intl.DateTimeFormat(\"en-US\", { timeZone: tz }).format(new Date());\n return tz;\n }\n }\n } catch {\n // Invalid value or read error, fall through\n }\n }\n\n const host = Intl.DateTimeFormat().resolvedOptions().timeZone;\n return host?.trim() || \"UTC\";\n}\n\n/** Placeholder values in IDENTITY.md that mean \"not yet filled in\". */\nconst IDENTITY_PLACEHOLDERS = new Set([\n \"(not set)\",\n \"(pick something you like)\",\n \"(your signature)\",\n \"(how do you come across?)\",\n]);\n\nexport type AgentIdentity = {\n name?: string;\n emoji?: string;\n creature?: string;\n vibe?: string;\n};\n\n/**\n * Parse IDENTITY.md from the workspace to resolve the agent's chosen identity.\n * Returns fields that have been filled in (skips placeholders).\n */\nfunction resolveIdentity(workspace: string): AgentIdentity {\n const identity: AgentIdentity = {};\n const identityMd = join(workspace, \"IDENTITY.md\");\n if (!existsSync(identityMd)) return identity;\n\n try {\n const content = readFileSync(identityMd, \"utf-8\");\n const lines = content.split(/\\r?\\n/);\n for (const line of lines) {\n // Strip list markers and bold markers\n const cleaned = line.trim().replace(/^\\s*-\\s*/, \"\");\n const colonIndex = cleaned.indexOf(\":\");\n if (colonIndex === -1) continue;\n\n const label = cleaned.slice(0, colonIndex).replace(/[*_]/g, \"\").trim().toLowerCase();\n const value = cleaned.slice(colonIndex + 1).replace(/^[*_]+|[*_]+$/g, \"\").trim();\n if (!value) continue;\n\n // Check if it's a placeholder\n const normalized = value.toLowerCase().replace(/[\\u2013\\u2014]/g, \"-\").replace(/\\s+/g, \" \");\n if (IDENTITY_PLACEHOLDERS.has(normalized)) continue;\n // Also check if it looks like a template hint wrapped in parens or italics\n if (/^\\(.*\\)$/.test(value) || /^_.*_$/.test(value)) continue;\n\n if (label === \"name\") identity.name = value;\n if (label === \"emoji\") identity.emoji = value;\n if (label === \"creature\") identity.creature = value;\n if (label === \"vibe\") identity.vibe = value;\n }\n } catch {\n // Read error, return empty\n }\n\n return identity;\n}\n\nconst BOOTSTRAP_FILES = [\n \"AGENTS.md\",\n \"SOUL.md\",\n \"USER.md\",\n \"TOOLS.md\",\n \"IDENTITY.md\",\n];\n\n/**\n * Builds the context (system prompt + messages) for the agent.\n */\nexport class ContextBuilder {\n private workspace: string;\n readonly memory: MemoryStore;\n readonly skills: SkillsLoader;\n\n constructor(workspace: string) {\n this.workspace = workspace;\n this.memory = new MemoryStore(workspace);\n this.skills = new SkillsLoader(workspace);\n }\n\n /** Build the system prompt from bootstrap files, memory, and skills. */\n buildSystemPrompt(): string {\n const parts: string[] = [];\n\n // Core identity\n parts.push(this.getIdentity());\n\n // Bootstrap files\n const bootstrap = this.loadBootstrapFiles();\n if (bootstrap) parts.push(bootstrap);\n\n // Memory context\n const memory = this.memory.getMemoryContext();\n if (memory) parts.push(`# Memory\\n\\n${memory}`);\n\n // Always-loaded skills\n const alwaysSkills = this.skills.getAlwaysSkills();\n if (alwaysSkills.length > 0) {\n console.log(`Skills always-loaded: ${alwaysSkills.join(\", \")}`);\n const alwaysContent = this.skills.loadSkillsForContext(alwaysSkills);\n if (alwaysContent) {\n parts.push(`# Active Skills\\n\\n${alwaysContent}`);\n }\n }\n\n // Available skills summary\n const skillsSummary = this.skills.buildSkillsSummary();\n if (skillsSummary) {\n parts.push(\n `# Skills (mandatory)\\n\\n` +\n `Before replying, scan the <skills> entries below.\\n` +\n `- If a skill clearly applies to the user's request: read its SKILL.md at the <location> path using read_file, then follow its instructions.\\n` +\n `- If multiple skills could apply: choose the most specific one, then read and follow it.\\n` +\n `- If no skill applies: respond normally without reading any SKILL.md.\\n` +\n `- Never improvise when a matching skill exists. Always read and follow the skill first.\\n\\n` +\n skillsSummary,\n );\n }\n\n return parts.join(\"\\n\\n---\\n\\n\");\n }\n\n private getIdentity(): string {\n const now = new Date();\n const tz = resolveTimezone(this.workspace);\n const identity = resolveIdentity(this.workspace);\n const dateStr = now.toLocaleString(\"en-US\", { timeZone: tz, year: \"numeric\", month: \"2-digit\", day: \"2-digit\", hour: \"2-digit\", minute: \"2-digit\", hour12: false }).replace(/\\//g, \"-\");\n const dayName = now.toLocaleDateString(\"en-US\", { timeZone: tz, weekday: \"long\" });\n\n // Use identity name if set, otherwise generic\n const agentName = identity.name || \"kodama\";\n const identityLine = identity.name\n ? `You are ${agentName}, a personal AI assistant.`\n : \"You are a personal AI assistant running inside kodamabot.\";\n\n return `# ${agentName}\n\n${identityLine} You have access to tools that allow you to:\n- Read, write, and edit files\n- Execute shell commands\n- Search the web and fetch web pages\n- Send messages to users on chat channels\n- Spawn subagents for complex background tasks\n\n## Current Time\n${dateStr} (${dayName})\nTimezone: ${tz}${tz === \"UTC\" ? \"\\nNote: Timezone is not yet configured. Do NOT assume the user's local time or make time-of-day references (morning, night, etc.). Ask the user where they are so you can set their timezone.\" : \"\"}\n\n## Workspace\nYour workspace is at: ${this.workspace}\n- Identity: ${this.workspace}/IDENTITY.md\n- User info: ${this.workspace}/USER.md\n- Persona: ${this.workspace}/SOUL.md\n- Instructions: ${this.workspace}/AGENTS.md\n- Heartbeat: ${this.workspace}/HEARTBEAT.md\n- Memory files: ${this.workspace}/memory/MEMORY.md\n- Daily notes: ${this.workspace}/memory/YYYY-MM-DD.md\n- Custom skills: ${this.workspace}/skills/{skill-name}/SKILL.md\n\nIMPORTANT: When responding to direct questions or conversations, reply directly with your text response.\nOnly use the 'message' tool when you need to send a message to a specific chat channel.\nFor normal conversation, just respond with text - do not call the message tool.\n\nIMPORTANT: When you decide to use tools, ALWAYS include a brief text message alongside your tool calls.\nThis text is sent to the user immediately so they know you're working. Keep it natural and conversational.\nNever mention internal file names (IDENTITY.md, USER.md, SOUL.md, MEMORY.md, etc.) to the user. These are implementation details. Just act naturally — save things silently, don't narrate file operations.\n\n## Installing Skills\nWhen asked to install a skill (from a URL, file, or any source):\n1. Fetch/read the skill content\n2. ALWAYS install it to: ${this.workspace}/skills/{skill-name}/SKILL.md\n3. If the skill references a different workspace path (e.g. .moltbot/, .otherbot/, etc.), rewrite ALL paths to use ${this.workspace}/ instead\n4. Preserve the skill's frontmatter, instructions, scripts, references, and assets — only change the workspace paths\n\n## Identity & Onboarding\nIf IDENTITY.md has no Name set, and USER.md contains \"(not set)\" values, this is a new user.\nOn their FIRST message, greet them warmly and ask:\n1. What they'd like to call you (your name — you're becoming someone, not just a chatbot)\n2. What they'd like to be called\n3. Where they're located (to determine timezone, e.g. Asia/Tokyo, America/New_York)\n\nThen update IDENTITY.md and USER.md with the real values using the edit_file tool. Use IANA timezone format.\nOnly do this ONCE — if both files already have real values, skip onboarding.\n\n## Persona\nIf SOUL.md is present, embody its persona and tone. Avoid stiff, generic replies; follow its guidance unless higher-priority instructions override it.\n\nAlways be helpful, accurate, and concise. When using tools, explain what you're doing.\nWhen remembering something, write to ${this.workspace}/memory/MEMORY.md`;\n }\n\n private loadBootstrapFiles(): string {\n const parts: string[] = [];\n for (const filename of BOOTSTRAP_FILES) {\n const filePath = join(this.workspace, filename);\n if (existsSync(filePath)) {\n const content = readFileSync(filePath, \"utf-8\");\n parts.push(`## ${filename}\\n\\n${content}`);\n }\n }\n return parts.join(\"\\n\\n\");\n }\n\n /** Build the complete message list for an LLM call. */\n buildMessages(params: {\n history: ChatMessage[];\n currentMessage: string;\n media?: string[];\n channel?: string;\n chatId?: string;\n }): ChatMessage[] {\n const messages: ChatMessage[] = [];\n\n // System prompt\n let systemPrompt = this.buildSystemPrompt();\n if (params.channel && params.chatId) {\n systemPrompt += `\\n\\n## Current Session\\nChannel: ${params.channel}\\nChat ID: ${params.chatId}`;\n }\n messages.push({ role: \"system\", content: systemPrompt });\n\n // History — replay full rich messages (user, assistant w/ tool_calls, tool results, etc.)\n for (const msg of params.history) {\n messages.push(msg);\n }\n\n // Current message (with optional image attachments)\n const userContent = this.buildUserContent(\n params.currentMessage,\n params.media,\n );\n messages.push({ role: \"user\", content: userContent });\n\n return messages;\n }\n\n private buildUserContent(\n text: string,\n media?: string[],\n ): string | ContentPart[] {\n if (!media || media.length === 0) return text;\n\n const images: ContentPart[] = [];\n for (const filePath of media) {\n if (!existsSync(filePath)) continue;\n try {\n const data = readFileSync(filePath);\n const ext = filePath.split(\".\").pop()?.toLowerCase() ?? \"\";\n const mimeMap: 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 const mime = mimeMap[ext];\n if (!mime) continue;\n\n const b64 = data.toString(\"base64\");\n images.push({\n type: \"image_url\",\n image_url: { url: `data:${mime};base64,${b64}` },\n });\n } catch {\n // skip unreadable files\n }\n }\n\n if (images.length === 0) return text;\n return [...images, { type: \"text\", text }];\n }\n\n /** Add a tool result to the message list. */\n addToolResult(\n messages: ChatMessage[],\n toolCallId: string,\n toolName: string,\n result: string,\n ): ChatMessage[] {\n messages.push({\n role: \"tool\",\n tool_call_id: toolCallId,\n name: toolName,\n content: result,\n });\n return messages;\n }\n\n /** Add an assistant message to the message list. */\n addAssistantMessage(\n messages: ChatMessage[],\n content: string | null,\n toolCalls?: Array<{\n id: string;\n type: \"function\";\n function: { name: string; arguments: string };\n }>,\n ): ChatMessage[] {\n const msg: ChatMessage = {\n role: \"assistant\",\n content: content ?? \"\",\n };\n if (toolCalls) {\n msg.tool_calls = toolCalls;\n }\n messages.push(msg);\n return messages;\n }\n}\n"],"mappings":";;;;;;;;;;;AAWA,SAAS,gBAAgB,WAA2B;CAClD,MAAM,SAAS,KAAK,WAAW,UAAU;AACzC,KAAI,WAAW,OAAO,CACpB,KAAI;EAEF,MAAM,UADU,aAAa,QAAQ,QAAQ,CACrB,MAAM,sBAAsB;AACpD,MAAI,SA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;IACF,MAAM,OAAO,aAAa,SAAS;IASnC,MAAM,OAPkC;KACtC,KAAK;KACL,MAAM;KACN,KAAK;KACL,KAAK;KACL,MAAM;KACP,CAPW,SAAS,MAAM,IAAI,CAAC,KAAK,EAAE,aAAa,IAAI;AASxD,QAAI,CAAC,KAAM;IAEX,MAAM,MAAM,KAAK,SAAS,SAAS;AACnC,WAAO,KAAK;KACV,MAAM;KACN,WAAW,EAAE,KAAK,QAAQ,KAAK,UAAU,OAAO;KACjD,CAAC;WACI;;AAKV,MAAI,OAAO,WAAW,EAAG,QAAO;AAChC,SAAO,CAAC,GAAG,QAAQ;GAAE,MAAM;GAAQ;GAAM,CAAC;;;CAI5C,cACE,UACA,YACA,UACA,QACe;AACf,WAAS,KAAK;GACZ,MAAM;GACN,cAAc;GACd,MAAM;GACN,SAAS;GACV,CAAC;AACF,SAAO;;;CAIT,oBACE,UACA,SACA,WAKe;EACf,MAAM,MAAmB;GACvB,MAAM;GACN,SAAS,WAAW;GACrB;AACD,MAAI,UACF,KAAI,aAAa;AAEnB,WAAS,KAAK,IAAI;AAClB,SAAO"}
1
+ {"version":3,"file":"context.mjs","names":[],"sources":["../../src/agent/context.ts"],"sourcesContent":["import { readFileSync, existsSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport type { ChatMessage, ContentPart } from \"../providers/base.js\";\nimport { MemoryStore } from \"./memory.js\";\nimport { SkillsLoader } from \"./skills.js\";\n\n/**\n * Resolve user timezone from USER.md.\n * Looks for a line like: Timezone: Asia/Tokyo\n * Falls back to host detection / UTC.\n */\nfunction resolveTimezone(workspace: string): string {\n const userMd = join(workspace, \"USER.md\");\n if (existsSync(userMd)) {\n try {\n const content = readFileSync(userMd, \"utf-8\");\n const tzMatch = content.match(/^timezone:\\s*(.+)/im);\n if (tzMatch) {\n const tz = tzMatch[1].trim();\n if (tz && !tz.includes(\"not set\")) {\n // Validate IANA timezone\n new Intl.DateTimeFormat(\"en-US\", { timeZone: tz }).format(new Date());\n return tz;\n }\n }\n } catch {\n // Invalid value or read error, fall through\n }\n }\n\n const host = Intl.DateTimeFormat().resolvedOptions().timeZone;\n return host?.trim() || \"UTC\";\n}\n\n/** Placeholder values in IDENTITY.md that mean \"not yet filled in\". */\nconst IDENTITY_PLACEHOLDERS = new Set([\n \"(not set)\",\n \"(pick something you like)\",\n \"(your signature)\",\n \"(how do you come across?)\",\n]);\n\nexport type AgentIdentity = {\n name?: string;\n emoji?: string;\n creature?: string;\n vibe?: string;\n};\n\n/**\n * Parse IDENTITY.md from the workspace to resolve the agent's chosen identity.\n * Returns fields that have been filled in (skips placeholders).\n */\nfunction resolveIdentity(workspace: string): AgentIdentity {\n const identity: AgentIdentity = {};\n const identityMd = join(workspace, \"IDENTITY.md\");\n if (!existsSync(identityMd)) return identity;\n\n try {\n const content = readFileSync(identityMd, \"utf-8\");\n const lines = content.split(/\\r?\\n/);\n for (const line of lines) {\n // Strip list markers and bold markers\n const cleaned = line.trim().replace(/^\\s*-\\s*/, \"\");\n const colonIndex = cleaned.indexOf(\":\");\n if (colonIndex === -1) continue;\n\n const label = cleaned.slice(0, colonIndex).replace(/[*_]/g, \"\").trim().toLowerCase();\n const value = cleaned.slice(colonIndex + 1).replace(/^[*_]+|[*_]+$/g, \"\").trim();\n if (!value) continue;\n\n // Check if it's a placeholder\n const normalized = value.toLowerCase().replace(/[\\u2013\\u2014]/g, \"-\").replace(/\\s+/g, \" \");\n if (IDENTITY_PLACEHOLDERS.has(normalized)) continue;\n // Also check if it looks like a template hint wrapped in parens or italics\n if (/^\\(.*\\)$/.test(value) || /^_.*_$/.test(value)) continue;\n\n if (label === \"name\") identity.name = value;\n if (label === \"emoji\") identity.emoji = value;\n if (label === \"creature\") identity.creature = value;\n if (label === \"vibe\") identity.vibe = value;\n }\n } catch {\n // Read error, return empty\n }\n\n return identity;\n}\n\nconst BOOTSTRAP_FILES = [\n \"AGENTS.md\",\n \"SOUL.md\",\n \"USER.md\",\n \"TOOLS.md\",\n \"IDENTITY.md\",\n];\n\n/**\n * Builds the context (system prompt + messages) for the agent.\n */\nexport class ContextBuilder {\n private workspace: string;\n readonly memory: MemoryStore;\n readonly skills: SkillsLoader;\n\n constructor(workspace: string) {\n this.workspace = workspace;\n this.memory = new MemoryStore(workspace);\n this.skills = new SkillsLoader(workspace);\n }\n\n /** Build the system prompt from bootstrap files, memory, and skills. */\n buildSystemPrompt(): string {\n const parts: string[] = [];\n\n // Core identity\n parts.push(this.getIdentity());\n\n // Bootstrap files\n const bootstrap = this.loadBootstrapFiles();\n if (bootstrap) parts.push(bootstrap);\n\n // Memory context\n const memory = this.memory.getMemoryContext();\n if (memory) parts.push(`# Memory\\n\\n${memory}`);\n\n // Always-loaded skills\n const alwaysSkills = this.skills.getAlwaysSkills();\n if (alwaysSkills.length > 0) {\n console.log(`Skills always-loaded: ${alwaysSkills.join(\", \")}`);\n const alwaysContent = this.skills.loadSkillsForContext(alwaysSkills);\n if (alwaysContent) {\n parts.push(`# Active Skills\\n\\n${alwaysContent}`);\n }\n }\n\n // Available skills summary\n const skillsSummary = this.skills.buildSkillsSummary();\n if (skillsSummary) {\n parts.push(\n `# Skills (mandatory)\\n\\n` +\n `Before replying, scan the <skills> entries below.\\n` +\n `- If a skill clearly applies to the user's request: read its SKILL.md at the <location> path using read_file, then follow its instructions.\\n` +\n `- If multiple skills could apply: choose the most specific one, then read and follow it.\\n` +\n `- If no skill applies: respond normally without reading any SKILL.md.\\n` +\n `- Never improvise when a matching skill exists. Always read and follow the skill first.\\n\\n` +\n skillsSummary,\n );\n }\n\n return parts.join(\"\\n\\n---\\n\\n\");\n }\n\n private getIdentity(): string {\n const now = new Date();\n const tz = resolveTimezone(this.workspace);\n const identity = resolveIdentity(this.workspace);\n const dateStr = now.toLocaleString(\"en-US\", { timeZone: tz, year: \"numeric\", month: \"2-digit\", day: \"2-digit\", hour: \"2-digit\", minute: \"2-digit\", hour12: false }).replace(/\\//g, \"-\");\n const dayName = now.toLocaleDateString(\"en-US\", { timeZone: tz, weekday: \"long\" });\n\n // Use identity name if set, otherwise generic\n const agentName = identity.name || \"kodama\";\n const identityLine = identity.name\n ? `You are ${agentName}, a personal AI assistant.`\n : \"You are a personal AI assistant running inside kodamabot.\";\n\n return `# ${agentName}\n\n${identityLine} You have access to tools that allow you to:\n- Read, write, and edit files\n- Execute shell commands\n- Search the web and fetch web pages\n- Send messages to users on chat channels\n- Spawn subagents for complex background tasks\n\n## Current Time\n${dateStr} (${dayName})\nTimezone: ${tz}${tz === \"UTC\" ? \"\\nNote: Timezone is not yet configured. Do NOT assume the user's local time or make time-of-day references (morning, night, etc.). Ask the user where they are so you can set their timezone.\" : \"\"}\n\n## Workspace\nYour workspace is at: ${this.workspace}\n- Identity: ${this.workspace}/IDENTITY.md\n- User info: ${this.workspace}/USER.md\n- Persona: ${this.workspace}/SOUL.md\n- Instructions: ${this.workspace}/AGENTS.md\n- Heartbeat: ${this.workspace}/HEARTBEAT.md\n- Long-term memory: ${this.workspace}/memory/MEMORY.md\n- History log: ${this.workspace}/memory/HISTORY.md (grep-searchable)\n- Custom skills: ${this.workspace}/skills/{skill-name}/SKILL.md\n\nIMPORTANT: When responding to direct questions or conversations, reply directly with your text response.\nOnly use the 'message' tool when you need to send a message to a specific chat channel.\nFor normal conversation, just respond with text - do not call the message tool.\n\nIMPORTANT: When you decide to use tools, ALWAYS include a brief text message alongside your tool calls.\nThis text is sent to the user immediately so they know you're working. Keep it natural and conversational.\nNever mention internal file names (IDENTITY.md, USER.md, SOUL.md, MEMORY.md, etc.) to the user. These are implementation details. Just act naturally — save things silently, don't narrate file operations.\n\n## Installing Skills\nWhen asked to install a skill (from a URL, file, or any source):\n1. Fetch/read the skill content\n2. ALWAYS install it to: ${this.workspace}/skills/{skill-name}/SKILL.md\n3. If the skill references a different workspace path (e.g. .moltbot/, .otherbot/, etc.), rewrite ALL paths to use ${this.workspace}/ instead\n4. Preserve the skill's frontmatter, instructions, scripts, references, and assets — only change the workspace paths\n\n## Identity & Onboarding\nIf IDENTITY.md has no Name set, and USER.md contains \"(not set)\" values, this is a new user.\nOn their FIRST message, greet them warmly and ask:\n1. What they'd like to call you (your name — you're becoming someone, not just a chatbot)\n2. What they'd like to be called\n3. Where they're located (to determine timezone, e.g. Asia/Tokyo, America/New_York)\n\nThen update IDENTITY.md and USER.md with the real values using the edit_file tool. Use IANA timezone format.\nOnly do this ONCE — if both files already have real values, skip onboarding.\n\n## Persona\nIf SOUL.md is present, embody its persona and tone. Avoid stiff, generic replies; follow its guidance unless higher-priority instructions override it.\n\nAlways be helpful, accurate, and concise. When using tools, explain what you're doing.\nWhen remembering something important, write to ${this.workspace}/memory/MEMORY.md\nTo recall past events, grep ${this.workspace}/memory/HISTORY.md`;\n }\n\n private loadBootstrapFiles(): string {\n const parts: string[] = [];\n for (const filename of BOOTSTRAP_FILES) {\n const filePath = join(this.workspace, filename);\n if (existsSync(filePath)) {\n const content = readFileSync(filePath, \"utf-8\");\n parts.push(`## ${filename}\\n\\n${content}`);\n }\n }\n return parts.join(\"\\n\\n\");\n }\n\n /** Build the complete message list for an LLM call. */\n buildMessages(params: {\n history: ChatMessage[];\n currentMessage: string;\n media?: string[];\n channel?: string;\n chatId?: string;\n }): ChatMessage[] {\n const messages: ChatMessage[] = [];\n\n // System prompt\n let systemPrompt = this.buildSystemPrompt();\n if (params.channel && params.chatId) {\n systemPrompt += `\\n\\n## Current Session\\nChannel: ${params.channel}\\nChat ID: ${params.chatId}`;\n }\n messages.push({ role: \"system\", content: systemPrompt });\n\n // History — replay full rich messages (user, assistant w/ tool_calls, tool results, etc.)\n for (const msg of params.history) {\n messages.push(msg);\n }\n\n // Current message (with optional image attachments)\n const userContent = this.buildUserContent(\n params.currentMessage,\n params.media,\n );\n messages.push({ role: \"user\", content: userContent });\n\n return messages;\n }\n\n private buildUserContent(\n text: string,\n media?: string[],\n ): string | ContentPart[] {\n if (!media || media.length === 0) return text;\n\n const images: ContentPart[] = [];\n for (const filePath of media) {\n if (!existsSync(filePath)) continue;\n try {\n const data = readFileSync(filePath);\n const ext = filePath.split(\".\").pop()?.toLowerCase() ?? \"\";\n const mimeMap: 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 const mime = mimeMap[ext];\n if (!mime) continue;\n\n const b64 = data.toString(\"base64\");\n images.push({\n type: \"image_url\",\n image_url: { url: `data:${mime};base64,${b64}` },\n });\n } catch {\n // skip unreadable files\n }\n }\n\n if (images.length === 0) return text;\n return [...images, { type: \"text\", text }];\n }\n\n /** Add a tool result to the message list. */\n addToolResult(\n messages: ChatMessage[],\n toolCallId: string,\n toolName: string,\n result: string,\n ): ChatMessage[] {\n messages.push({\n role: \"tool\",\n tool_call_id: toolCallId,\n name: toolName,\n content: result,\n });\n return messages;\n }\n\n /** Add an assistant message to the message list. */\n addAssistantMessage(\n messages: ChatMessage[],\n content: string | null,\n toolCalls?: Array<{\n id: string;\n type: \"function\";\n function: { name: string; arguments: string };\n }>,\n ): ChatMessage[] {\n const msg: ChatMessage = {\n role: \"assistant\",\n content: content ?? \"\",\n };\n if (toolCalls) {\n msg.tool_calls = toolCalls;\n }\n messages.push(msg);\n return messages;\n }\n}\n"],"mappings":";;;;;;;;;;;AAWA,SAAS,gBAAgB,WAA2B;CAClD,MAAM,SAAS,KAAK,WAAW,UAAU;AACzC,KAAI,WAAW,OAAO,CACpB,KAAI;EAEF,MAAM,UADU,aAAa,QAAQ,QAAQ,CACrB,MAAM,sBAAsB;AACpD,MAAI,SAAS;GACX,MAAM,KAAK,QAAQ,GAAG,MAAM;AAC5B,OAAI,MAAM,CAAC,GAAG,SAAS,UAAU,EAAE;AAEjC,QAAI,KAAK,eAAe,SAAS,EAAE,UAAU,IAAI,CAAC,CAAC,uBAAO,IAAI,MAAM,CAAC;AACrE,WAAO;;;SAGL;AAMV,QADa,KAAK,gBAAgB,CAAC,iBAAiB,CAAC,UACxC,MAAM,IAAI;;;AAIzB,MAAM,wBAAwB,IAAI,I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K,QAAQ,KAAK,UAAU,OAAO;KACjD,CAAC;WACI;;AAKV,MAAI,OAAO,WAAW,EAAG,QAAO;AAChC,SAAO,CAAC,GAAG,QAAQ;GAAE,MAAM;GAAQ;GAAM,CAAC;;;CAI5C,cACE,UACA,YACA,UACA,QACe;AACf,WAAS,KAAK;GACZ,MAAM;GACN,cAAc;GACd,MAAM;GACN,SAAS;GACV,CAAC;AACF,SAAO;;;CAIT,oBACE,UACA,SACA,WAKe;EACf,MAAM,MAAmB;GACvB,MAAM;GACN,SAAS,WAAW;GACrB;AACD,MAAI,UACF,KAAI,aAAa;AAEnB,WAAS,KAAK,IAAI;AAClB,SAAO"}
@@ -22,8 +22,10 @@ declare class AgentLoop {
22
22
  private provider;
23
23
  private workspace;
24
24
  private model;
25
+ private consolidationModel;
25
26
  private maxTokens;
26
27
  private maxIterations;
28
+ private memoryWindow;
27
29
  readonly context: ContextBuilder;
28
30
  readonly sessions: SessionManager;
29
31
  readonly tools: ToolRegistry;
@@ -36,8 +38,10 @@ declare class AgentLoop {
36
38
  provider: LLMProvider;
37
39
  workspace: string;
38
40
  model?: string;
41
+ consolidationModel?: string;
39
42
  maxTokens?: number;
40
43
  maxIterations?: number;
44
+ memoryWindow?: number;
41
45
  braveApiKey?: string;
42
46
  execConfig?: ExecToolConfig;
43
47
  restrictToWorkspace?: boolean;
@@ -59,6 +63,8 @@ declare class AgentLoop {
59
63
  private updateToolContexts;
60
64
  /** Process a message directly (for CLI or cron usage). */
61
65
  processDirect(content: string, sessionKey?: string, channel?: string, chatId?: string): Promise<string>;
66
+ /** Consolidate old messages into MEMORY.md + HISTORY.md, then trim session. */
67
+ private consolidateMemory;
62
68
  }
63
69
  //#endregion
64
70
  export { AgentLoop };
@@ -1 +1 @@
1
- {"version":3,"file":"loop.d.mts","names":[],"sources":["../../src/agent/loop.ts"],"mappings":";;;;;;;;;;;;AAmCA;;;;;;;cAAa,SAAA;EAAA,QACH,GAAA;EAAA,QACA,QAAA;EAAA,QACA,SA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UAAA,GAAa,cAAA;IACb,mBAAA;IACA,YAAA;IACA,aAAA;IACA,WAAA,GAAc,IAAA;EAAA;EAAA,QAqCR,oBAAA;EA9CN;EA4GI,GAAA,CAAA,GAAO,OAAA;EA1GX;EA2IF,IAAA,CAAA;EAzIE;EAAA,QA+IY,cAAA;EAAA,QA4FA,oBAAA;EAAA,QA4CA,YAAA;EApRZ;EAAA,QAkXM,uBAAA;EAAA,QAiBA,kBAAA;EAjYQ;EAmZV,aAAA,CACJ,OAAA,UACA,UAAA,WACA,OAAA,WACA,MAAA,YACC,OAAA;EAnXK;EAAA,QA2YM,iBAAA;AAAA"}
@@ -1,4 +1,5 @@
1
1
  import { createOutboundMessage } from "../bus/events.mjs";
2
+ import { MemoryStore } from "./memory.mjs";
2
3
  import { getBuiltinSkillsDir } from "./skills.mjs";
3
4
  import { ContextBuilder } from "./context.mjs";
4
5
  import { ToolRegistry } from "./tools/registry.mjs";
@@ -26,8 +27,10 @@ var AgentLoop = class {
26
27
  provider;
27
28
  workspace;
28
29
  model;
30
+ consolidationModel;
29
31
  maxTokens;
30
32
  maxIterations;
33
+ memoryWindow;
31
34
  context;
32
35
  sessions;
33
36
  tools;
@@ -40,8 +43,10 @@ var AgentLoop = class {
40
43
  this.provider = params.provider;
41
44
  this.workspace = params.workspace;
42
45
  this.model = params.model ?? params.provider.getDefaultModel();
46
+ this.consolidationModel = params.consolidationModel ?? this.model;
43
47
  this.maxTokens = params.maxTokens ?? 8192;
44
48
  this.maxIterations = params.maxIterations ?? 20;
49
+ this.memoryWindow = params.memoryWindow ?? 50;
45
50
  const execConfig = params.execConfig ?? { timeout: 60 };
46
51
  const restrictToWorkspace = params.restrictToWorkspace ?? false;
47
52
  this.context = new ContextBuilder(params.workspace);
@@ -127,6 +132,7 @@ var AgentLoop = class {
127
132
  const controller = new AbortController();
128
133
  this.inflight.set(sessionKey, controller);
129
134
  const session = this.sessions.getOrCreate(sessionKey);
135
+ if (session.history.length > this.memoryWindow) await this.consolidateMemory(session);
130
136
  this.updateToolContexts(msg.channel, msg.chatId);
131
137
  const messages = this.context.buildMessages({
132
138
  history: session.getHistory(),
@@ -137,19 +143,19 @@ var AgentLoop = class {
137
143
  });
138
144
  const newMsgStart = 1 + session.getHistory().length;
139
145
  try {
140
- const finalContent = await this.runAgentLoop(messages, controller.signal, (text) => {
146
+ const result = await this.runAgentLoop(messages, controller.signal, (text) => {
141
147
  if (text && text.trim().length > 0) this.bus.publishOutbound(createOutboundMessage({
142
148
  channel: msg.channel,
143
149
  chatId: msg.chatId,
144
150
  content: text
145
151
  }));
146
152
  });
147
- session.addTurnMessages(messages.slice(newMsgStart));
153
+ session.addTurnMessages(messages.slice(newMsgStart), result.toolsUsed);
148
154
  this.sessions.save(session);
149
155
  return createOutboundMessage({
150
156
  channel: msg.channel,
151
157
  chatId: msg.chatId,
152
- content: finalContent
158
+ content: result.content
153
159
  });
154
160
  } catch (err) {
155
161
  if (isAbortError(err)) {
@@ -188,18 +194,19 @@ var AgentLoop = class {
188
194
  chatId: originChatId
189
195
  });
190
196
  const newMsgStart = 1 + session.getHistory().length;
191
- const finalContent = await this.runAgentLoop(messages);
192
- session.addTurnMessages(messages.slice(newMsgStart));
197
+ const result = await this.runAgentLoop(messages);
198
+ session.addTurnMessages(messages.slice(newMsgStart), result.toolsUsed);
193
199
  this.sessions.save(session);
194
200
  return createOutboundMessage({
195
201
  channel: originChannel,
196
202
  chatId: originChatId,
197
- content: finalContent
203
+ content: result.content
198
204
  });
199
205
  }
200
206
  async runAgentLoop(messages, signal, onToolCallText) {
201
207
  let finalContent = null;
202
208
  let sentToolCallNotice = false;
209
+ const toolsUsed = [];
203
210
  for (let i = 0; i < this.maxIterations; i++) {
204
211
  const response = await this.provider.chat({
205
212
  messages,
@@ -224,6 +231,7 @@ var AgentLoop = class {
224
231
  }));
225
232
  this.context.addAssistantMessage(messages, response.content, toolCallDicts);
226
233
  for (const tc of response.toolCalls) {
234
+ toolsUsed.push(tc.name);
227
235
  console.log(`Tool: ${tc.name}(${JSON.stringify(tc.arguments)})`);
228
236
  if (tc.name === "read_file") {
229
237
  const skillMatch = String(tc.arguments?.path ?? "").match(/skills\/([^/]+)\/SKILL\.md$/);
@@ -257,7 +265,10 @@ var AgentLoop = class {
257
265
  role: "assistant",
258
266
  content: finalContent
259
267
  });
260
- return finalContent;
268
+ return {
269
+ content: finalContent,
270
+ toolsUsed
271
+ };
261
272
  }
262
273
  /** Generate a fallback interim message based on which tools are being called. */
263
274
  getToolCallFallbackText(toolCalls) {
@@ -293,10 +304,65 @@ var AgentLoop = class {
293
304
  chatId
294
305
  });
295
306
  const newMsgStart = 1 + session.getHistory().length;
296
- const finalContent = await this.runAgentLoop(messages);
297
- session.addTurnMessages(messages.slice(newMsgStart));
307
+ const result = await this.runAgentLoop(messages);
308
+ session.addTurnMessages(messages.slice(newMsgStart), result.toolsUsed);
309
+ this.sessions.save(session);
310
+ return result.content;
311
+ }
312
+ /** Consolidate old messages into MEMORY.md + HISTORY.md, then trim session. */
313
+ async consolidateMemory(session) {
314
+ const memory = new MemoryStore(this.workspace);
315
+ const keepCount = Math.min(10, Math.max(2, Math.floor(this.memoryWindow / 2)));
316
+ const oldMessages = session.history.slice(0, -keepCount);
317
+ if (oldMessages.length === 0) return;
318
+ console.log(`Memory consolidation: ${session.history.length} messages, archiving ${oldMessages.length}, keeping ${keepCount}`);
319
+ const lines = [];
320
+ for (const m of oldMessages) {
321
+ const content = typeof m.content === "string" ? m.content : "";
322
+ if (!content) continue;
323
+ const role = m.role.toUpperCase();
324
+ const tools = m.toolsUsed ? ` [tools: ${m.toolsUsed.join(", ")}]` : "";
325
+ const ts = m.timestamp ? `[${m.timestamp.slice(0, 16)}] ` : "";
326
+ lines.push(`${ts}${role}${tools}: ${content}`);
327
+ }
328
+ const conversation = lines.join("\n");
329
+ const currentMemory = memory.readLongTerm();
330
+ const prompt = `You are a memory consolidation agent. Process this conversation and return a JSON object with exactly two keys:
331
+
332
+ 1. "history_entry": A paragraph (2-5 sentences) summarizing the key events/decisions/topics. Start with a timestamp like [YYYY-MM-DD HH:MM]. Include enough detail to be useful when found by grep search later.
333
+
334
+ 2. "memory_update": The updated long-term memory content. Add any new facts: user location, preferences, personal info, habits, project context, technical decisions, tools/services used. If nothing new, return the existing content unchanged.
335
+
336
+ ## Current Long-term Memory
337
+ ${currentMemory || "(empty)"}
338
+
339
+ ## Conversation to Process
340
+ ${conversation}
341
+
342
+ Respond with ONLY valid JSON, no markdown fences.`;
343
+ try {
344
+ let text = ((await this.provider.chat({
345
+ messages: [{
346
+ role: "system",
347
+ content: "You are a memory consolidation agent. Respond only with valid JSON."
348
+ }, {
349
+ role: "user",
350
+ content: prompt
351
+ }],
352
+ model: this.consolidationModel
353
+ })).content || "").trim();
354
+ if (text.startsWith("```")) text = text.split("\n").slice(1).join("\n").split("```")[0].trim();
355
+ const result = JSON.parse(text);
356
+ if (result.history_entry) memory.appendHistory(result.history_entry);
357
+ if (result.memory_update && result.memory_update !== currentMemory) memory.writeLongTerm(result.memory_update);
358
+ } catch (err) {
359
+ console.error("Memory consolidation failed:", err);
360
+ const fallbackEntry = `[${(/* @__PURE__ */ new Date()).toISOString().slice(0, 16)}] Consolidation failed, archiving raw messages:\n${conversation.slice(0, 2e3)}`;
361
+ memory.appendHistory(fallbackEntry);
362
+ }
363
+ session.trimHistory(keepCount);
298
364
  this.sessions.save(session);
299
- return finalContent;
365
+ console.log(`Memory consolidation done, session trimmed to ${session.history.length} messages`);
300
366
  }
301
367
  };
302
368
  /** Check if an error is an abort/cancellation error. */
@@ -1 +1 @@
1
- {"version":3,"file":"loop.mjs","names":[],"sources":["../../src/agent/loop.ts"],"sourcesContent":["import type { LLMProvider, ChatMessage, ToolCallRequest } from \"../providers/base.js\";\nimport type { MessageBus } from \"../bus/queue.js\";\nimport type {\n InboundMessage,\n OutboundMessage,\n} from \"../bus/events.js\";\nimport { createOutboundMessage } from \"../bus/events.js\";\nimport { ContextBuilder } from \"./context.js\";\nimport { ToolRegistry } from \"./tools/registry.js\";\nimport {\n ReadFileTool,\n WriteFileTool,\n EditFileTool,\n ListDirTool,\n} from \"./tools/filesystem.js\";\nimport { ExecTool } from \"./tools/shell.js\";\nimport { WebSearchTool, WebFetchTool } from \"./tools/web.js\";\nimport { MessageTool } from \"./tools/message.js\";\nimport { SpawnTool } from \"./tools/spawn.js\";\nimport { CronTool } from \"./tools/cron.js\";\nimport { SubagentManager } from \"./subagent.js\";\nimport { getBuiltinSkillsDir } from \"./skills.js\";\nimport { SessionManager } from \"../session/manager.js\";\nimport type { ExecToolConfig } from \"../config/schema.js\";\nimport type { Tool } from \"./tools/base.js\";\n\n/**\n * The agent loop: core processing engine.\n *\n * 1. Receives messages from the bus\n * 2. Builds context with history, memory, skills\n * 3. Calls the LLM\n * 4. Executes tool calls\n * 5. Sends responses back\n */\nexport class AgentLoop {\n private bus: MessageBus;\n private provider: LLMProvider;\n private workspace: string;\n private model: string;\n private maxTokens: number;\n private maxIterations: number;\n\n readonly context: ContextBuilder;\n readonly sessions: SessionManager;\n readonly tools: ToolRegistry;\n readonly subagents: SubagentManager;\n\n private _running = false;\n\n /** In-flight AbortControllers keyed by session key. */\n private inflight = new Map<string, AbortController>();\n\n constructor(params: {\n bus: MessageBus;\n provider: LLMProvider;\n workspace: string;\n model?: string;\n maxTokens?: number;\n maxIterations?: number;\n braveApiKey?: string;\n execConfig?: ExecToolConfig;\n restrictToWorkspace?: boolean;\n toolsEnabled?: string[];\n toolsDisabled?: string[];\n customTools?: Tool[];\n }) {\n this.bus = params.bus;\n this.provider = params.provider;\n this.workspace = params.workspace;\n this.model = params.model ?? params.provider.getDefaultModel();\n this.maxTokens = params.maxTokens ?? 8192;\n this.maxIterations = params.maxIterations ?? 20;\n\n const execConfig = params.execConfig ?? { timeout: 60 };\n const restrictToWorkspace = params.restrictToWorkspace ?? false;\n\n this.context = new ContextBuilder(params.workspace);\n this.sessions = new SessionManager(params.workspace);\n this.tools = new ToolRegistry();\n this.subagents = new SubagentManager({\n provider: params.provider,\n workspace: params.workspace,\n bus: params.bus,\n model: this.model,\n braveApiKey: params.braveApiKey,\n execConfig,\n restrictToWorkspace,\n });\n\n this.registerDefaultTools(\n execConfig,\n restrictToWorkspace,\n params.braveApiKey,\n params.toolsEnabled,\n params.toolsDisabled,\n params.customTools,\n );\n }\n\n private registerDefaultTools(\n execConfig: ExecToolConfig,\n restrictToWorkspace: boolean,\n braveApiKey?: string,\n toolsEnabled?: string[],\n toolsDisabled?: string[],\n customTools?: Tool[],\n ): void {\n const enabled = new Set(toolsEnabled ?? []);\n const disabled = new Set(toolsDisabled ?? []);\n const hasAllowlist = enabled.size > 0;\n const shouldRegister = (name: string): boolean =>\n (hasAllowlist ? enabled.has(name) : true) && !disabled.has(name);\n\n const registerIfEnabled = (tool: Tool): void => {\n if (shouldRegister(tool.name)) {\n this.tools.register(tool);\n }\n };\n\n // File tools — pass allowedDir when restrictToWorkspace is enabled\n const allowedDir = restrictToWorkspace ? this.workspace : undefined;\n const readOnlyPaths = restrictToWorkspace ? [getBuiltinSkillsDir()] : undefined;\n registerIfEnabled(new ReadFileTool({ allowedDir, readOnlyPaths }));\n registerIfEnabled(new WriteFileTool({ allowedDir }));\n registerIfEnabled(new EditFileTool({ allowedDir }));\n registerIfEnabled(new ListDirTool({ allowedDir, readOnlyPaths }));\n\n // Shell tool\n registerIfEnabled(\n new ExecTool({\n workingDir: this.workspace,\n timeout: execConfig.timeout,\n restrictToWorkspace,\n }),\n );\n\n // Web tools\n registerIfEnabled(new WebSearchTool({ apiKey: braveApiKey }));\n registerIfEnabled(new WebFetchTool());\n\n // Message tool\n const messageTool = new MessageTool({\n sendCallback: (msg) => this.bus.publishOutbound(msg),\n });\n registerIfEnabled(messageTool);\n\n // Spawn tool\n const spawnTool = new SpawnTool(this.subagents);\n registerIfEnabled(spawnTool);\n\n // Cron tool — always registered, uses DO scheduling via worker API\n registerIfEnabled(new CronTool());\n\n if (customTools && customTools.length > 0) {\n for (const tool of customTools) {\n registerIfEnabled(tool);\n }\n }\n }\n\n /** Run the agent loop, processing messages from the bus. */\n async run(): Promise<void> {\n this._running = true;\n console.log(\"Agent loop started\");\n\n while (this._running) {\n try {\n const msg = await this.bus.consumeInboundTimeout(1000);\n\n // Process concurrently so new messages can abort in-flight ones\n this.processMessage(msg)\n .then(async (response) => {\n if (response) {\n await this.bus.publishOutbound(response);\n }\n })\n .catch(async (err) => {\n if (isAbortError(err)) return; // Already handled\n console.error(\"Error processing message:\", err);\n await this.bus.publishOutbound(\n createOutboundMessage({\n channel: msg.channel,\n chatId: msg.chatId,\n content: `Sorry, I encountered an error: ${err instanceof Error ? err.message : err}`,\n }),\n );\n });\n } catch {\n // timeout, continue\n }\n }\n }\n\n /** Stop the agent loop. */\n stop(): void {\n this._running = false;\n console.log(\"Agent loop stopping\");\n }\n\n /** Process a single inbound message. */\n private async processMessage(\n msg: InboundMessage,\n ): Promise<OutboundMessage | null> {\n // Handle system messages (subagent announces)\n if (msg.channel === \"system\") {\n return this.processSystemMessage(msg);\n }\n\n console.log(`Processing message from ${msg.channel}:${msg.senderId}`);\n\n const sessionKey = `${msg.channel}:${msg.chatId}`;\n\n // Abort any in-flight request for this session\n const existing = this.inflight.get(sessionKey);\n if (existing) {\n console.log(`Aborting in-flight request for ${sessionKey}`);\n existing.abort();\n }\n\n // Create a new AbortController for this request\n const controller = new AbortController();\n this.inflight.set(sessionKey, controller);\n\n const session = this.sessions.getOrCreate(sessionKey);\n\n // Update tool contexts\n this.updateToolContexts(msg.channel, msg.chatId);\n\n // Build initial messages\n const messages = this.context.buildMessages({\n history: session.getHistory(),\n currentMessage: msg.content,\n media: msg.media.length > 0 ? msg.media : undefined,\n channel: msg.channel,\n chatId: msg.chatId,\n });\n\n // The messages array is: [system, ...history, currentUser]\n // We want to save from the current user message onward (skip system + old history).\n const savedHistoryLen = session.getHistory().length;\n const newMsgStart = 1 + savedHistoryLen; // 1 for system prompt\n\n try {\n // Agent loop (mutates messages by appending assistant/tool messages)\n const finalContent = await this.runAgentLoop(messages, controller.signal, (text) => {\n if (text && text.trim().length > 0) {\n this.bus.publishOutbound(\n createOutboundMessage({\n channel: msg.channel,\n chatId: msg.chatId,\n content: text,\n }),\n );\n }\n });\n\n // Save the new messages from this turn (user + all agent loop messages)\n session.addTurnMessages(messages.slice(newMsgStart));\n this.sessions.save(session);\n\n return createOutboundMessage({\n channel: msg.channel,\n chatId: msg.chatId,\n content: finalContent,\n });\n } catch (err) {\n if (isAbortError(err)) {\n // Request was aborted because a new message arrived.\n // Save the user message to history so the next request has context,\n // but don't save any assistant response.\n const userMessages = messages.slice(newMsgStart).filter((m) => m.role === \"user\");\n if (userMessages.length > 0) {\n session.addTurnMessages(userMessages);\n this.sessions.save(session);\n }\n console.log(`Request aborted for ${sessionKey}, user message saved to history`);\n return null; // No response -- the new message will handle it\n }\n throw err; // Re-throw non-abort errors\n } finally {\n // Clean up if this is still our controller\n if (this.inflight.get(sessionKey) === controller) {\n this.inflight.delete(sessionKey);\n }\n }\n }\n\n private async processSystemMessage(\n msg: InboundMessage,\n ): Promise<OutboundMessage | null> {\n console.log(`Processing system message from ${msg.senderId}`);\n\n let originChannel: string;\n let originChatId: string;\n\n if (msg.chatId.includes(\":\")) {\n const [ch, id] = msg.chatId.split(\":\", 2);\n originChannel = ch;\n originChatId = id;\n } else {\n originChannel = \"cli\";\n originChatId = msg.chatId;\n }\n\n const sessionKey = `${originChannel}:${originChatId}`;\n const session = this.sessions.getOrCreate(sessionKey);\n\n this.updateToolContexts(originChannel, originChatId);\n\n const messages = this.context.buildMessages({\n history: session.getHistory(),\n currentMessage: msg.content,\n channel: originChannel,\n chatId: originChatId,\n });\n\n const savedHistoryLen = session.getHistory().length;\n const newMsgStart = 1 + savedHistoryLen;\n\n const finalContent = await this.runAgentLoop(messages);\n\n session.addTurnMessages(messages.slice(newMsgStart));\n this.sessions.save(session);\n\n return createOutboundMessage({\n channel: originChannel,\n chatId: originChatId,\n content: finalContent,\n });\n }\n\n private async runAgentLoop(\n messages: ChatMessage[],\n signal?: AbortSignal,\n onToolCallText?: (text: string) => void,\n ): Promise<string> {\n let finalContent: string | null = null;\n let sentToolCallNotice = false;\n\n for (let i = 0; i < this.maxIterations; i++) {\n const response = await this.provider.chat({\n messages,\n tools: this.tools.getDefinitions(),\n model: this.model,\n maxTokens: this.maxTokens,\n signal,\n });\n\n if (response.hasToolCalls) {\n // Send an interim message so the user knows we're working\n if (!sentToolCallNotice && onToolCallText) {\n const interimText = response.content?.trim()\n || this.getToolCallFallbackText(response.toolCalls);\n if (interimText) {\n onToolCallText(interimText);\n }\n sentToolCallNotice = true;\n }\n const toolCallDicts = response.toolCalls.map((tc) => ({\n id: tc.id,\n type: \"function\" as const,\n function: {\n name: tc.name,\n arguments: JSON.stringify(tc.arguments),\n },\n }));\n\n this.context.addAssistantMessage(\n messages,\n response.content,\n toolCallDicts,\n );\n\n for (const tc of response.toolCalls) {\n console.log(`Tool: ${tc.name}(${JSON.stringify(tc.arguments)})`);\n\n // Detect skill reads\n if (tc.name === \"read_file\") {\n const path = String(tc.arguments?.path ?? \"\");\n const skillMatch = path.match(/skills\\/([^/]+)\\/SKILL\\.md$/);\n if (skillMatch) {\n console.log(`Skill activated: ${skillMatch[1]}`);\n }\n }\n\n const result = await this.tools.execute(tc.name, tc.arguments);\n this.context.addToolResult(messages, tc.id, tc.name, result);\n }\n } else {\n finalContent = response.content;\n\n // If the LLM returned empty content after tool use, nudge it to respond\n if ((!finalContent || finalContent.trim().length === 0) && i > 0) {\n messages.push({\n role: \"assistant\",\n content: \"\",\n });\n messages.push({\n role: \"user\",\n content: \"(You used tools but didn't respond to the user. Please provide a brief response summarizing what you did.)\",\n });\n continue;\n }\n\n // Push the final assistant message so it gets persisted with the turn\n messages.push({ role: \"assistant\", content: finalContent ?? \"\" });\n break;\n }\n }\n\n if (!finalContent || finalContent.trim().length === 0) {\n finalContent = \"I've completed processing but have no response to give.\";\n }\n\n // If we exhausted iterations without a non-tool-call response, still persist the final text\n if (messages[messages.length - 1]?.role !== \"assistant\" || messages[messages.length - 1]?.content !== finalContent) {\n messages.push({ role: \"assistant\", content: finalContent });\n }\n\n return finalContent;\n }\n\n /** Generate a fallback interim message based on which tools are being called. */\n private getToolCallFallbackText(toolCalls: ToolCallRequest[]): string {\n const toolNames = toolCalls.map((tc) => tc.name);\n const fallbacks: Record<string, string> = {\n web_search: \"検索中...\",\n web_fetch: \"ページを読み込み中...\",\n read_file: \"ファイルを確認中...\",\n write_file: \"ファイルを書き込み中...\",\n edit_file: \"ファイルを編集中...\",\n exec: \"コマンドを実行中...\",\n spawn: \"サブエージェントを起動中...\",\n };\n for (const name of toolNames) {\n if (fallbacks[name]) return fallbacks[name];\n }\n return \"ちょっと待ってね...\";\n }\n\n private updateToolContexts(channel: string, chatId: string): void {\n const messageTool = this.tools.get(\"message\");\n if (messageTool instanceof MessageTool) {\n messageTool.setContext(channel, chatId);\n }\n\n const spawnTool = this.tools.get(\"spawn\");\n if (spawnTool instanceof SpawnTool) {\n spawnTool.setContext(channel, chatId);\n }\n\n const cronTool = this.tools.get(\"cron\");\n if (cronTool instanceof CronTool) {\n cronTool.setContext(channel, chatId);\n }\n }\n\n /** Process a message directly (for CLI or cron usage). */\n async processDirect(\n content: string,\n sessionKey = \"cli:direct\",\n channel = \"cli\",\n chatId = \"direct\",\n ): Promise<string> {\n // Use inline version of processMessage for direct calls\n const session = this.sessions.getOrCreate(sessionKey);\n this.updateToolContexts(channel, chatId);\n\n const messages = this.context.buildMessages({\n history: session.getHistory(),\n currentMessage: content,\n channel,\n chatId,\n });\n\n const savedHistoryLen = session.getHistory().length;\n const newMsgStart = 1 + savedHistoryLen;\n\n const finalContent = await this.runAgentLoop(messages);\n\n session.addTurnMessages(messages.slice(newMsgStart));\n this.sessions.save(session);\n\n return finalContent;\n }\n}\n\n/** Check if an error is an abort/cancellation error. */\nfunction isAbortError(err: unknown): boolean {\n if (err instanceof DOMException && err.name === \"AbortError\") return true;\n if (err instanceof Error) {\n if (err.name === \"AbortError\") return true;\n // OpenAI SDK wraps abort as APIUserAbortError\n if (err.name === \"APIUserAbortError\") return true;\n if (err.message.includes(\"abort\")) return true;\n }\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAmCA,IAAa,YAAb,MAAuB;CACrB,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CAER,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CAET,AAAQ,WAAW;;CAGnB,AAAQ,2BAAW,IAAI,KAA8B;CAErD,YAAY,QAaT;AACD,OAAK,MAAM,OAAO;AAClB,OAAK,WAAW,OAAO;AACvB,OAAK,YAAY,OAAO;AACxB,OAAK,QAAQ,OAAO,SAAS,OAAO,SAAS,iBAAiB;AAC9D,OAAK,YAAY,OAAO,aAAa;AACrC,OAAK,gBAAgB,OAAO,iBAAiB;EAE7C,MAAM,aAAa,OAAO,cAAc,EAAE,SAAS,IAAI;EACvD,MAAM,sBAAsB,OAAO,uBAAuB;AAE1D,OAAK,UAAU,IAAI,eAAe,OAAO,UAAU;AACnD,OAAK,WAAW,IAAI,eAAe,OAAO,UAAU;AACpD,OAAK,QAAQ,IAAI,cAAc;AAC/B,OAAK,YAAY,IAAI,gBAAgB;GACnC,UAAU,OAAO;GACjB,WAAW,OAAO;GAClB,KAAK,OAAO;GACZ,OAAO,KAAK;GACZ,aAAa,OAAO;GACpB;GACA;GACD,CAAC;AAEF,OAAK,qBACH,YACA,qBACA,OAAO,aACP,OAAO,cACP,OAAO,eACP,OAAO,YACR;;CAGH,AAAQ,qBACN,YACA,qBACA,aACA,cACA,eACA,aACM;EACN,MAAM,UAAU,IAAI,IAAI,gBAAgB,EAAE,CAAC;EAC3C,MAAM,WAAW,IAAI,IAAI,iBAAiB,EAAE,CAAC;EAC7C,MAAM,eAAe,QAAQ,OAAO;EACpC,MAAM,kBAAkB,UACrB,eAAe,QAAQ,IAAI,KAAK,GAAG,SAAS,CAAC,SAAS,IAAI,KAAK;EAElE,MA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I,SAAS,aAAc,QAAO;AAEtC,MAAI,IAAI,SAAS,oBAAqB,QAAO;AAC7C,MAAI,IAAI,QAAQ,SAAS,QAAQ,CAAE,QAAO;;AAE5C,QAAO"}
1
+ {"version":3,"file":"loop.mjs","names":[],"sources":["../../src/agent/loop.ts"],"sourcesContent":["import type { LLMProvider, ChatMessage, ToolCallRequest } from \"../providers/base.js\";\nimport type { MessageBus } from \"../bus/queue.js\";\nimport type {\n InboundMessage,\n OutboundMessage,\n} from \"../bus/events.js\";\nimport { createOutboundMessage } from \"../bus/events.js\";\nimport { ContextBuilder } from \"./context.js\";\nimport { ToolRegistry } from \"./tools/registry.js\";\nimport {\n ReadFileTool,\n WriteFileTool,\n EditFileTool,\n ListDirTool,\n} from \"./tools/filesystem.js\";\nimport { ExecTool } from \"./tools/shell.js\";\nimport { WebSearchTool, WebFetchTool } from \"./tools/web.js\";\nimport { MessageTool } from \"./tools/message.js\";\nimport { SpawnTool } from \"./tools/spawn.js\";\nimport { CronTool } from \"./tools/cron.js\";\nimport { SubagentManager } from \"./subagent.js\";\nimport { getBuiltinSkillsDir } from \"./skills.js\";\nimport { SessionManager, Session } from \"../session/manager.js\";\nimport { MemoryStore } from \"./memory.js\";\nimport type { ExecToolConfig } from \"../config/schema.js\";\nimport type { Tool } from \"./tools/base.js\";\n\n/**\n * The agent loop: core processing engine.\n *\n * 1. Receives messages from the bus\n * 2. Builds context with history, memory, skills\n * 3. Calls the LLM\n * 4. Executes tool calls\n * 5. Sends responses back\n */\nexport class AgentLoop {\n private bus: MessageBus;\n private provider: LLMProvider;\n private workspace: string;\n private model: string;\n private consolidationModel: string;\n private maxTokens: number;\n private maxIterations: number;\n private memoryWindow: number;\n\n readonly context: ContextBuilder;\n readonly sessions: SessionManager;\n readonly tools: ToolRegistry;\n readonly subagents: SubagentManager;\n\n private _running = false;\n\n /** In-flight AbortControllers keyed by session key. */\n private inflight = new Map<string, AbortController>();\n\n constructor(params: {\n bus: MessageBus;\n provider: LLMProvider;\n workspace: string;\n model?: string;\n consolidationModel?: string;\n maxTokens?: number;\n maxIterations?: number;\n memoryWindow?: number;\n braveApiKey?: string;\n execConfig?: ExecToolConfig;\n restrictToWorkspace?: boolean;\n toolsEnabled?: string[];\n toolsDisabled?: string[];\n customTools?: Tool[];\n }) {\n this.bus = params.bus;\n this.provider = params.provider;\n this.workspace = params.workspace;\n this.model = params.model ?? params.provider.getDefaultModel();\n this.consolidationModel = params.consolidationModel ?? this.model;\n this.maxTokens = params.maxTokens ?? 8192;\n this.maxIterations = params.maxIterations ?? 20;\n this.memoryWindow = params.memoryWindow ?? 50;\n\n const execConfig = params.execConfig ?? { timeout: 60 };\n const restrictToWorkspace = params.restrictToWorkspace ?? false;\n\n this.context = new ContextBuilder(params.workspace);\n this.sessions = new SessionManager(params.workspace);\n this.tools = new ToolRegistry();\n this.subagents = new SubagentManager({\n provider: params.provider,\n workspace: params.workspace,\n bus: params.bus,\n model: this.model,\n braveApiKey: params.braveApiKey,\n execConfig,\n restrictToWorkspace,\n });\n\n this.registerDefaultTools(\n execConfig,\n restrictToWorkspace,\n params.braveApiKey,\n params.toolsEnabled,\n params.toolsDisabled,\n params.customTools,\n );\n }\n\n private registerDefaultTools(\n execConfig: ExecToolConfig,\n restrictToWorkspace: boolean,\n braveApiKey?: string,\n toolsEnabled?: string[],\n toolsDisabled?: string[],\n customTools?: Tool[],\n ): void {\n const enabled = new Set(toolsEnabled ?? []);\n const disabled = new Set(toolsDisabled ?? []);\n const hasAllowlist = enabled.size > 0;\n const shouldRegister = (name: string): boolean =>\n (hasAllowlist ? enabled.has(name) : true) && !disabled.has(name);\n\n const registerIfEnabled = (tool: Tool): void => {\n if (shouldRegister(tool.name)) {\n this.tools.register(tool);\n }\n };\n\n // File tools — pass allowedDir when restrictToWorkspace is enabled\n const allowedDir = restrictToWorkspace ? this.workspace : undefined;\n const readOnlyPaths = restrictToWorkspace ? [getBuiltinSkillsDir()] : undefined;\n registerIfEnabled(new ReadFileTool({ allowedDir, readOnlyPaths }));\n registerIfEnabled(new WriteFileTool({ allowedDir }));\n registerIfEnabled(new EditFileTool({ allowedDir }));\n registerIfEnabled(new ListDirTool({ allowedDir, readOnlyPaths }));\n\n // Shell tool\n registerIfEnabled(\n new ExecTool({\n workingDir: this.workspace,\n timeout: execConfig.timeout,\n restrictToWorkspace,\n }),\n );\n\n // Web tools\n registerIfEnabled(new WebSearchTool({ apiKey: braveApiKey }));\n registerIfEnabled(new WebFetchTool());\n\n // Message tool\n const messageTool = new MessageTool({\n sendCallback: (msg) => this.bus.publishOutbound(msg),\n });\n registerIfEnabled(messageTool);\n\n // Spawn tool\n const spawnTool = new SpawnTool(this.subagents);\n registerIfEnabled(spawnTool);\n\n // Cron tool — always registered, uses DO scheduling via worker API\n registerIfEnabled(new CronTool());\n\n if (customTools && customTools.length > 0) {\n for (const tool of customTools) {\n registerIfEnabled(tool);\n }\n }\n }\n\n /** Run the agent loop, processing messages from the bus. */\n async run(): Promise<void> {\n this._running = true;\n console.log(\"Agent loop started\");\n\n while (this._running) {\n try {\n const msg = await this.bus.consumeInboundTimeout(1000);\n\n // Process concurrently so new messages can abort in-flight ones\n this.processMessage(msg)\n .then(async (response) => {\n if (response) {\n await this.bus.publishOutbound(response);\n }\n })\n .catch(async (err) => {\n if (isAbortError(err)) return; // Already handled\n console.error(\"Error processing message:\", err);\n await this.bus.publishOutbound(\n createOutboundMessage({\n channel: msg.channel,\n chatId: msg.chatId,\n content: `Sorry, I encountered an error: ${err instanceof Error ? err.message : err}`,\n }),\n );\n });\n } catch {\n // timeout, continue\n }\n }\n }\n\n /** Stop the agent loop. */\n stop(): void {\n this._running = false;\n console.log(\"Agent loop stopping\");\n }\n\n /** Process a single inbound message. */\n private async processMessage(\n msg: InboundMessage,\n ): Promise<OutboundMessage | null> {\n // Handle system messages (subagent announces)\n if (msg.channel === \"system\") {\n return this.processSystemMessage(msg);\n }\n\n console.log(`Processing message from ${msg.channel}:${msg.senderId}`);\n\n const sessionKey = `${msg.channel}:${msg.chatId}`;\n\n // Abort any in-flight request for this session\n const existing = this.inflight.get(sessionKey);\n if (existing) {\n console.log(`Aborting in-flight request for ${sessionKey}`);\n existing.abort();\n }\n\n // Create a new AbortController for this request\n const controller = new AbortController();\n this.inflight.set(sessionKey, controller);\n\n const session = this.sessions.getOrCreate(sessionKey);\n\n // Consolidate memory if session is too large\n if (session.history.length > this.memoryWindow) {\n await this.consolidateMemory(session);\n }\n\n // Update tool contexts\n this.updateToolContexts(msg.channel, msg.chatId);\n\n // Build initial messages\n const messages = this.context.buildMessages({\n history: session.getHistory(),\n currentMessage: msg.content,\n media: msg.media.length > 0 ? msg.media : undefined,\n channel: msg.channel,\n chatId: msg.chatId,\n });\n\n // The messages array is: [system, ...history, currentUser]\n // We want to save from the current user message onward (skip system + old history).\n const savedHistoryLen = session.getHistory().length;\n const newMsgStart = 1 + savedHistoryLen; // 1 for system prompt\n\n try {\n // Agent loop (mutates messages by appending assistant/tool messages)\n const result = await this.runAgentLoop(messages, controller.signal, (text) => {\n if (text && text.trim().length > 0) {\n this.bus.publishOutbound(\n createOutboundMessage({\n channel: msg.channel,\n chatId: msg.chatId,\n content: text,\n }),\n );\n }\n });\n\n // Save the new messages from this turn (user + all agent loop messages)\n session.addTurnMessages(messages.slice(newMsgStart), result.toolsUsed);\n this.sessions.save(session);\n\n return createOutboundMessage({\n channel: msg.channel,\n chatId: msg.chatId,\n content: result.content,\n });\n } catch (err) {\n if (isAbortError(err)) {\n // Request was aborted because a new message arrived.\n // Save the user message to history so the next request has context,\n // but don't save any assistant response.\n const userMessages = messages.slice(newMsgStart).filter((m) => m.role === \"user\");\n if (userMessages.length > 0) {\n session.addTurnMessages(userMessages);\n this.sessions.save(session);\n }\n console.log(`Request aborted for ${sessionKey}, user message saved to history`);\n return null; // No response -- the new message will handle it\n }\n throw err; // Re-throw non-abort errors\n } finally {\n // Clean up if this is still our controller\n if (this.inflight.get(sessionKey) === controller) {\n this.inflight.delete(sessionKey);\n }\n }\n }\n\n private async processSystemMessage(\n msg: InboundMessage,\n ): Promise<OutboundMessage | null> {\n console.log(`Processing system message from ${msg.senderId}`);\n\n let originChannel: string;\n let originChatId: string;\n\n if (msg.chatId.includes(\":\")) {\n const [ch, id] = msg.chatId.split(\":\", 2);\n originChannel = ch;\n originChatId = id;\n } else {\n originChannel = \"cli\";\n originChatId = msg.chatId;\n }\n\n const sessionKey = `${originChannel}:${originChatId}`;\n const session = this.sessions.getOrCreate(sessionKey);\n\n this.updateToolContexts(originChannel, originChatId);\n\n const messages = this.context.buildMessages({\n history: session.getHistory(),\n currentMessage: msg.content,\n channel: originChannel,\n chatId: originChatId,\n });\n\n const savedHistoryLen = session.getHistory().length;\n const newMsgStart = 1 + savedHistoryLen;\n\n const result = await this.runAgentLoop(messages);\n\n session.addTurnMessages(messages.slice(newMsgStart), result.toolsUsed);\n this.sessions.save(session);\n\n return createOutboundMessage({\n channel: originChannel,\n chatId: originChatId,\n content: result.content,\n });\n }\n\n private async runAgentLoop(\n messages: ChatMessage[],\n signal?: AbortSignal,\n onToolCallText?: (text: string) => void,\n ): Promise<{ content: string; toolsUsed: string[] }> {\n let finalContent: string | null = null;\n let sentToolCallNotice = false;\n const toolsUsed: string[] = [];\n\n for (let i = 0; i < this.maxIterations; i++) {\n const response = await this.provider.chat({\n messages,\n tools: this.tools.getDefinitions(),\n model: this.model,\n maxTokens: this.maxTokens,\n signal,\n });\n\n if (response.hasToolCalls) {\n // Send an interim message so the user knows we're working\n if (!sentToolCallNotice && onToolCallText) {\n const interimText = response.content?.trim()\n || this.getToolCallFallbackText(response.toolCalls);\n if (interimText) {\n onToolCallText(interimText);\n }\n sentToolCallNotice = true;\n }\n const toolCallDicts = response.toolCalls.map((tc) => ({\n id: tc.id,\n type: \"function\" as const,\n function: {\n name: tc.name,\n arguments: JSON.stringify(tc.arguments),\n },\n }));\n\n this.context.addAssistantMessage(\n messages,\n response.content,\n toolCallDicts,\n );\n\n for (const tc of response.toolCalls) {\n toolsUsed.push(tc.name);\n console.log(`Tool: ${tc.name}(${JSON.stringify(tc.arguments)})`);\n\n // Detect skill reads\n if (tc.name === \"read_file\") {\n const path = String(tc.arguments?.path ?? \"\");\n const skillMatch = path.match(/skills\\/([^/]+)\\/SKILL\\.md$/);\n if (skillMatch) {\n console.log(`Skill activated: ${skillMatch[1]}`);\n }\n }\n\n const result = await this.tools.execute(tc.name, tc.arguments);\n this.context.addToolResult(messages, tc.id, tc.name, result);\n }\n } else {\n finalContent = response.content;\n\n // If the LLM returned empty content after tool use, nudge it to respond\n if ((!finalContent || finalContent.trim().length === 0) && i > 0) {\n messages.push({\n role: \"assistant\",\n content: \"\",\n });\n messages.push({\n role: \"user\",\n content: \"(You used tools but didn't respond to the user. Please provide a brief response summarizing what you did.)\",\n });\n continue;\n }\n\n // Push the final assistant message so it gets persisted with the turn\n messages.push({ role: \"assistant\", content: finalContent ?? \"\" });\n break;\n }\n }\n\n if (!finalContent || finalContent.trim().length === 0) {\n finalContent = \"I've completed processing but have no response to give.\";\n }\n\n // If we exhausted iterations without a non-tool-call response, still persist the final text\n if (messages[messages.length - 1]?.role !== \"assistant\" || messages[messages.length - 1]?.content !== finalContent) {\n messages.push({ role: \"assistant\", content: finalContent });\n }\n\n return { content: finalContent, toolsUsed };\n }\n\n /** Generate a fallback interim message based on which tools are being called. */\n private getToolCallFallbackText(toolCalls: ToolCallRequest[]): string {\n const toolNames = toolCalls.map((tc) => tc.name);\n const fallbacks: Record<string, string> = {\n web_search: \"検索中...\",\n web_fetch: \"ページを読み込み中...\",\n read_file: \"ファイルを確認中...\",\n write_file: \"ファイルを書き込み中...\",\n edit_file: \"ファイルを編集中...\",\n exec: \"コマンドを実行中...\",\n spawn: \"サブエージェントを起動中...\",\n };\n for (const name of toolNames) {\n if (fallbacks[name]) return fallbacks[name];\n }\n return \"ちょっと待ってね...\";\n }\n\n private updateToolContexts(channel: string, chatId: string): void {\n const messageTool = this.tools.get(\"message\");\n if (messageTool instanceof MessageTool) {\n messageTool.setContext(channel, chatId);\n }\n\n const spawnTool = this.tools.get(\"spawn\");\n if (spawnTool instanceof SpawnTool) {\n spawnTool.setContext(channel, chatId);\n }\n\n const cronTool = this.tools.get(\"cron\");\n if (cronTool instanceof CronTool) {\n cronTool.setContext(channel, chatId);\n }\n }\n\n /** Process a message directly (for CLI or cron usage). */\n async processDirect(\n content: string,\n sessionKey = \"cli:direct\",\n channel = \"cli\",\n chatId = \"direct\",\n ): Promise<string> {\n // Use inline version of processMessage for direct calls\n const session = this.sessions.getOrCreate(sessionKey);\n this.updateToolContexts(channel, chatId);\n\n const messages = this.context.buildMessages({\n history: session.getHistory(),\n currentMessage: content,\n channel,\n chatId,\n });\n\n const savedHistoryLen = session.getHistory().length;\n const newMsgStart = 1 + savedHistoryLen;\n\n const result = await this.runAgentLoop(messages);\n\n session.addTurnMessages(messages.slice(newMsgStart), result.toolsUsed);\n this.sessions.save(session);\n\n return result.content;\n }\n\n /** Consolidate old messages into MEMORY.md + HISTORY.md, then trim session. */\n private async consolidateMemory(session: Session): Promise<void> {\n const memory = new MemoryStore(this.workspace);\n const keepCount = Math.min(10, Math.max(2, Math.floor(this.memoryWindow / 2)));\n const oldMessages = session.history.slice(0, -keepCount);\n if (oldMessages.length === 0) return;\n\n console.log(`Memory consolidation: ${session.history.length} messages, archiving ${oldMessages.length}, keeping ${keepCount}`);\n\n // Format messages for LLM\n const lines: string[] = [];\n for (const m of oldMessages) {\n const content = typeof m.content === \"string\" ? m.content : \"\";\n if (!content) continue;\n const role = m.role.toUpperCase();\n const tools = m.toolsUsed ? ` [tools: ${m.toolsUsed.join(\", \")}]` : \"\";\n const ts = m.timestamp ? `[${m.timestamp.slice(0, 16)}] ` : \"\";\n lines.push(`${ts}${role}${tools}: ${content}`);\n }\n const conversation = lines.join(\"\\n\");\n const currentMemory = memory.readLongTerm();\n\n const prompt = `You are a memory consolidation agent. Process this conversation and return a JSON object with exactly two keys:\n\n1. \"history_entry\": A paragraph (2-5 sentences) summarizing the key events/decisions/topics. Start with a timestamp like [YYYY-MM-DD HH:MM]. Include enough detail to be useful when found by grep search later.\n\n2. \"memory_update\": The updated long-term memory content. Add any new facts: user location, preferences, personal info, habits, project context, technical decisions, tools/services used. If nothing new, return the existing content unchanged.\n\n## Current Long-term Memory\n${currentMemory || \"(empty)\"}\n\n## Conversation to Process\n${conversation}\n\nRespond with ONLY valid JSON, no markdown fences.`;\n\n try {\n const response = await this.provider.chat({\n messages: [\n { role: \"system\", content: \"You are a memory consolidation agent. Respond only with valid JSON.\" },\n { role: \"user\", content: prompt },\n ],\n model: this.consolidationModel,\n });\n\n let text = (response.content || \"\").trim();\n if (text.startsWith(\"```\")) {\n text = text.split(\"\\n\").slice(1).join(\"\\n\").split(\"```\")[0].trim();\n }\n\n const result = JSON.parse(text);\n\n if (result.history_entry) {\n memory.appendHistory(result.history_entry);\n }\n if (result.memory_update && result.memory_update !== currentMemory) {\n memory.writeLongTerm(result.memory_update);\n }\n } catch (err) {\n console.error(\"Memory consolidation failed:\", err);\n // Fallback: append raw conversation to history so it's not lost\n const fallbackEntry = `[${new Date().toISOString().slice(0, 16)}] Consolidation failed, archiving raw messages:\\n${conversation.slice(0, 2000)}`;\n memory.appendHistory(fallbackEntry);\n }\n\n // Always trim session to prevent unbounded growth\n session.trimHistory(keepCount);\n this.sessions.save(session);\n console.log(`Memory consolidation done, session trimmed to ${session.history.length} messages`);\n }\n}\n\n/** Check if an error is an abort/cancellation error. */\nfunction isAbortError(err: unknown): boolean {\n if (err instanceof DOMException && err.name === \"AbortError\") return true;\n if (err instanceof Error) {\n if (err.name === \"AbortError\") return true;\n // OpenAI SDK wraps abort as APIUserAbortError\n if (err.name === \"APIUserAbortError\") return true;\n if (err.message.includes(\"abort\")) return true;\n }\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAoCA,IAAa,YAAb,MAAuB;CACrB,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,A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,OAAO,iBAAiB,OAAO,kBAAkB,cACnD,QAAO,cAAc,OAAO,cAAc;WAErC,KAAK;AACZ,WAAQ,MAAM,gCAAgC,IAAI;GAElD,MAAM,gBAAgB,qBAAI,IAAI,MAAM,EAAC,aAAa,CAAC,MAAM,GAAG,GAAG,CAAC,mDAAmD,aAAa,MAAM,GAAG,IAAK;AAC9I,UAAO,cAAc,cAAc;;AAIrC,UAAQ,YAAY,UAAU;AAC9B,OAAK,SAAS,KAAK,QAAQ;AAC3B,UAAQ,IAAI,iDAAiD,QAAQ,QAAQ,OAAO,WAAW;;;;AAKnG,SAAS,aAAa,KAAuB;AAC3C,KAAI,eAAe,gBAAgB,IAAI,SAAS,aAAc,QAAO;AACrE,KAAI,eAAe,OAAO;AACxB,MAAI,IAAI,SAAS,aAAc,QAAO;AAEtC,MAAI,IAAI,SAAS,oBAAqB,QAAO;AAC7C,MAAI,IAAI,QAAQ,SAAS,QAAQ,CAAE,QAAO;;AAE5C,QAAO"}
@@ -1,28 +1,13 @@
1
1
  //#region src/agent/memory.d.ts
2
- /**
3
- * Memory system for the agent.
4
- * Supports daily notes (memory/YYYY-MM-DD.md) and long-term memory (MEMORY.md).
5
- */
2
+ /** Two-layer memory: MEMORY.md (long-term facts) + HISTORY.md (grep-searchable log). */
6
3
  declare class MemoryStore {
7
- private workspace;
8
4
  private memoryDir;
9
5
  private memoryFile;
6
+ private historyFile;
10
7
  constructor(workspace: string);
11
- /** Get path to today's memory file. */
12
- getTodayFile(): string;
13
- /** Read today's memory notes. */
14
- readToday(): string;
15
- /** Append content to today's memory notes. */
16
- appendToday(content: string): void;
17
- /** Read long-term memory (MEMORY.md). */
18
8
  readLongTerm(): string;
19
- /** Write to long-term memory (MEMORY.md). */
20
9
  writeLongTerm(content: string): void;
21
- /** Get memories from the last N days. */
22
- getRecentMemories(days?: number): string;
23
- /** List all memory files sorted by date (newest first). */
24
- listMemoryFiles(): string[];
25
- /** Get memory context for the agent prompt. */
10
+ appendHistory(entry: string): void;
26
11
  getMemoryContext(): string;
27
12
  }
28
13
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"memory.d.mts","names":[],"sources":["../../src/agent/memory.ts"],"mappings":";;AAQA;;;cAAa,WAAA;EAAA,QACH,SAAA;EAAA,QACA,SAAA;EAAA,QACA,UAAA;cAEI,SAAA;EAAA;EAOZ,YAAA,CAAA;EAKA;EAAA,SAAA,CAAA;EASY;EAAZ,WAAA,CAAY,OAAA;EAuBZ;EARA,YAAA,CAAA;EAaA;EALA,aAAA,CAAc,OAAA;EAwBd;EAnBA,iBAAA,CAAkB,IAAA;EA8BF;EAXhB,eAAA,CAAA;;EAWA,gBAAA,CAAA;AAAA"}
1
+ {"version":3,"file":"memory.d.mts","names":[],"sources":["../../src/agent/memory.ts"],"mappings":";;cAKa,WAAA;EAAA,QACH,SAAA;EAAA,QACA,UAAA;EAAA,QACA,WAAA;cAEI,SAAA;EAMZ,YAAA,CAAA;EAOA,aAAA,CAAc,OAAA;EAId,aAAA,CAAc,KAAA;EAId,gBAAA,CAAA;AAAA"}
@@ -1,74 +1,31 @@
1
- import { ensureDir, todayDate } from "../utils/helpers.mjs";
2
- import { existsSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
1
+ import { ensureDir } from "../utils/helpers.mjs";
2
+ import { appendFileSync, existsSync, readFileSync, writeFileSync } from "node:fs";
3
3
  import { join } from "node:path";
4
4
 
5
5
  //#region src/agent/memory.ts
6
- /**
7
- * Memory system for the agent.
8
- * Supports daily notes (memory/YYYY-MM-DD.md) and long-term memory (MEMORY.md).
9
- */
6
+ /** Two-layer memory: MEMORY.md (long-term facts) + HISTORY.md (grep-searchable log). */
10
7
  var MemoryStore = class {
11
- workspace;
12
8
  memoryDir;
13
9
  memoryFile;
10
+ historyFile;
14
11
  constructor(workspace) {
15
- this.workspace = workspace;
16
12
  this.memoryDir = ensureDir(join(workspace, "memory"));
17
13
  this.memoryFile = join(this.memoryDir, "MEMORY.md");
14
+ this.historyFile = join(this.memoryDir, "HISTORY.md");
18
15
  }
19
- /** Get path to today's memory file. */
20
- getTodayFile() {
21
- return join(this.memoryDir, `${todayDate()}.md`);
22
- }
23
- /** Read today's memory notes. */
24
- readToday() {
25
- const todayFile = this.getTodayFile();
26
- if (existsSync(todayFile)) return readFileSync(todayFile, "utf-8");
27
- return "";
28
- }
29
- /** Append content to today's memory notes. */
30
- appendToday(content) {
31
- const todayFile = this.getTodayFile();
32
- let finalContent;
33
- if (existsSync(todayFile)) finalContent = readFileSync(todayFile, "utf-8") + "\n" + content;
34
- else finalContent = `# ${todayDate()}\n\n${content}`;
35
- writeFileSync(todayFile, finalContent, "utf-8");
36
- }
37
- /** Read long-term memory (MEMORY.md). */
38
16
  readLongTerm() {
39
17
  if (existsSync(this.memoryFile)) return readFileSync(this.memoryFile, "utf-8");
40
18
  return "";
41
19
  }
42
- /** Write to long-term memory (MEMORY.md). */
43
20
  writeLongTerm(content) {
44
21
  writeFileSync(this.memoryFile, content, "utf-8");
45
22
  }
46
- /** Get memories from the last N days. */
47
- getRecentMemories(days = 7) {
48
- const memories = [];
49
- const today = /* @__PURE__ */ new Date();
50
- for (let i = 0; i < days; i++) {
51
- const date = new Date(today);
52
- date.setDate(date.getDate() - i);
53
- const dateStr = date.toISOString().slice(0, 10);
54
- const filePath = join(this.memoryDir, `${dateStr}.md`);
55
- if (existsSync(filePath)) memories.push(readFileSync(filePath, "utf-8"));
56
- }
57
- return memories.join("\n\n---\n\n");
58
- }
59
- /** List all memory files sorted by date (newest first). */
60
- listMemoryFiles() {
61
- if (!existsSync(this.memoryDir)) return [];
62
- return readdirSync(this.memoryDir).filter((f) => /^\d{4}-\d{2}-\d{2}\.md$/.test(f)).sort().reverse().map((f) => join(this.memoryDir, f));
23
+ appendHistory(entry) {
24
+ appendFileSync(this.historyFile, entry.trimEnd() + "\n\n", "utf-8");
63
25
  }
64
- /** Get memory context for the agent prompt. */
65
26
  getMemoryContext() {
66
- const parts = [];
67
27
  const longTerm = this.readLongTerm();
68
- if (longTerm) parts.push("## Long-term Memory\n" + longTerm);
69
- const today = this.readToday();
70
- if (today) parts.push("## Today's Notes\n" + today);
71
- return parts.join("\n\n");
28
+ return longTerm ? `## Long-term Memory\n${longTerm}` : "";
72
29
  }
73
30
  };
74
31
 
@@ -1 +1 @@
1
- {"version":3,"file":"memory.mjs","names":[],"sources":["../../src/agent/memory.ts"],"sourcesContent":["import { readFileSync, writeFileSync, existsSync, readdirSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { ensureDir, todayDate } from \"../utils/helpers.js\";\n\n/**\n * Memory system for the agent.\n * Supports daily notes (memory/YYYY-MM-DD.md) and long-term memory (MEMORY.md).\n */\nexport class MemoryStore {\n private workspace: string;\n private memoryDir: string;\n private memoryFile: string;\n\n constructor(workspace: string) {\n this.workspace = workspace;\n this.memoryDir = ensureDir(join(workspace, \"memory\"));\n this.memoryFile = join(this.memoryDir, \"MEMORY.md\");\n }\n\n /** Get path to today's memory file. */\n getTodayFile(): string {\n return join(this.memoryDir, `${todayDate()}.md`);\n }\n\n /** Read today's memory notes. */\n readToday(): string {\n const todayFile = this.getTodayFile();\n if (existsSync(todayFile)) {\n return readFileSync(todayFile, \"utf-8\");\n }\n return \"\";\n }\n\n /** Append content to today's memory notes. */\n appendToday(content: string): void {\n const todayFile = this.getTodayFile();\n let finalContent: string;\n\n if (existsSync(todayFile)) {\n const existing = readFileSync(todayFile, \"utf-8\");\n finalContent = existing + \"\\n\" + content;\n } else {\n finalContent = `# ${todayDate()}\\n\\n${content}`;\n }\n\n writeFileSync(todayFile, finalContent, \"utf-8\");\n }\n\n /** Read long-term memory (MEMORY.md). */\n readLongTerm(): string {\n if (existsSync(this.memoryFile)) {\n return readFileSync(this.memoryFile, \"utf-8\");\n }\n return \"\";\n }\n\n /** Write to long-term memory (MEMORY.md). */\n writeLongTerm(content: string): void {\n writeFileSync(this.memoryFile, content, \"utf-8\");\n }\n\n /** Get memories from the last N days. */\n getRecentMemories(days = 7): string {\n const memories: string[] = [];\n const today = new Date();\n\n for (let i = 0; i < days; i++) {\n const date = new Date(today);\n date.setDate(date.getDate() - i);\n const dateStr = date.toISOString().slice(0, 10);\n const filePath = join(this.memoryDir, `${dateStr}.md`);\n\n if (existsSync(filePath)) {\n memories.push(readFileSync(filePath, \"utf-8\"));\n }\n }\n\n return memories.join(\"\\n\\n---\\n\\n\");\n }\n\n /** List all memory files sorted by date (newest first). */\n listMemoryFiles(): string[] {\n if (!existsSync(this.memoryDir)) return [];\n\n return readdirSync(this.memoryDir)\n .filter((f) => /^\\d{4}-\\d{2}-\\d{2}\\.md$/.test(f))\n .sort()\n .reverse()\n .map((f) => join(this.memoryDir, f));\n }\n\n /** Get memory context for the agent prompt. */\n getMemoryContext(): string {\n const parts: string[] = [];\n\n const longTerm = this.readLongTerm();\n if (longTerm) {\n parts.push(\"## Long-term Memory\\n\" + longTerm);\n }\n\n const today = this.readToday();\n if (today) {\n parts.push(\"## Today's Notes\\n\" + today);\n }\n\n return parts.join(\"\\n\\n\");\n }\n}\n"],"mappings":";;;;;;;;;AAQA,IAAa,cAAb,MAAyB;CACvB,AAAQ;CACR,AAAQ;CACR,AAAQ;CAER,YAAY,WAAmB;AAC7B,OAAK,YAAY;AACjB,OAAK,YAAY,UAAU,KAAK,WAAW,SAAS,CAAC;AACrD,OAAK,aAAa,KAAK,KAAK,WAAW,YAAY;;;CAIrD,eAAuB;AACrB,SAAO,KAAK,KAAK,WAAW,GAAG,WAAW,CAAC,KAAK;;;CAIlD,YAAoB;EAClB,MAAM,YAAY,KAAK,cAAc;AACrC,MAAI,WAAW,UAAU,CACvB,QAAO,aAAa,WAAW,QAAQ;AAEzC,SAAO;;;CAIT,YAAY,SAAuB;EACjC,MAAM,YAAY,KAAK,cAAc;EACrC,IAAI;AAEJ,MAAI,WAAW,UAAU,CAEvB,gBADiB,aAAa,WAAW,QAAQ,GACvB,OAAO;MAEjC,gBAAe,KAAK,WAAW,CAAC,MAAM;AAGxC,gBAAc,WAAW,cAAc,QAAQ;;;CAIjD,eAAuB;AACrB,MAAI,WAAW,KAAK,WAAW,CAC7B,QAAO,aAAa,KAAK,YAAY,QAAQ;AAE/C,SAAO;;;CAIT,cAAc,SAAuB;AACnC,gBAAc,KAAK,YAAY,SAAS,QAAQ;;;CAIlD,kBAAkB,OAAO,GAAW;EAClC,MAAM,WAAqB,EAAE;EAC7B,MAAM,wBAAQ,IAAI,MAAM;AAExB,OAAK,IAAI,IAAI,GAAG,IAAI,MAAM,KAAK;GAC7B,MAAM,OAAO,IAAI,KAAK,MAAM;AAC5B,QAAK,QAAQ,KAAK,SAAS,GAAG,EAAE;GAChC,MAAM,UAAU,KAAK,aAAa,CAAC,MAAM,GAAG,GAAG;GAC/C,MAAM,WAAW,KAAK,KAAK,WAAW,GAAG,QAAQ,KAAK;AAEtD,OAAI,WAAW,SAAS,CACtB,UAAS,KAAK,aAAa,UAAU,QAAQ,CAAC;;AAIlD,SAAO,SAAS,KAAK,cAAc;;;CAIrC,kBAA4B;AAC1B,MAAI,CAAC,WAAW,KAAK,UAAU,CAAE,QAAO,EAAE;AAE1C,SAAO,YAAY,KAAK,UAAU,CAC/B,QAAQ,MAAM,0BAA0B,KAAK,EAAE,CAAC,CAChD,MAAM,CACN,SAAS,CACT,KAAK,MAAM,KAAK,KAAK,WAAW,EAAE,CAAC;;;CAIxC,mBAA2B;EACzB,MAAM,QAAkB,EAAE;EAE1B,MAAM,WAAW,KAAK,cAAc;AACpC,MAAI,SACF,OAAM,KAAK,0BAA0B,SAAS;EAGhD,MAAM,QAAQ,KAAK,WAAW;AAC9B,MAAI,MACF,OAAM,KAAK,uBAAuB,MAAM;AAG1C,SAAO,MAAM,KAAK,OAAO"}
1
+ {"version":3,"file":"memory.mjs","names":[],"sources":["../../src/agent/memory.ts"],"sourcesContent":["import { readFileSync, writeFileSync, existsSync, appendFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { ensureDir } from \"../utils/helpers.js\";\n\n/** Two-layer memory: MEMORY.md (long-term facts) + HISTORY.md (grep-searchable log). */\nexport class MemoryStore {\n private memoryDir: string;\n private memoryFile: string;\n private historyFile: string;\n\n constructor(workspace: string) {\n this.memoryDir = ensureDir(join(workspace, \"memory\"));\n this.memoryFile = join(this.memoryDir, \"MEMORY.md\");\n this.historyFile = join(this.memoryDir, \"HISTORY.md\");\n }\n\n readLongTerm(): string {\n if (existsSync(this.memoryFile)) {\n return readFileSync(this.memoryFile, \"utf-8\");\n }\n return \"\";\n }\n\n writeLongTerm(content: string): void {\n writeFileSync(this.memoryFile, content, \"utf-8\");\n }\n\n appendHistory(entry: string): void {\n appendFileSync(this.historyFile, entry.trimEnd() + \"\\n\\n\", \"utf-8\");\n }\n\n getMemoryContext(): string {\n const longTerm = this.readLongTerm();\n return longTerm ? `## Long-term Memory\\n${longTerm}` : \"\";\n }\n}\n"],"mappings":";;;;;;AAKA,IAAa,cAAb,MAAyB;CACvB,AAAQ;CACR,AAAQ;CACR,AAAQ;CAER,YAAY,WAAmB;AAC7B,OAAK,YAAY,UAAU,KAAK,WAAW,SAAS,CAAC;AACrD,OAAK,aAAa,KAAK,KAAK,WAAW,YAAY;AACnD,OAAK,cAAc,KAAK,KAAK,WAAW,aAAa;;CAGvD,eAAuB;AACrB,MAAI,WAAW,KAAK,WAAW,CAC7B,QAAO,aAAa,KAAK,YAAY,QAAQ;AAE/C,SAAO;;CAGT,cAAc,SAAuB;AACnC,gBAAc,KAAK,YAAY,SAAS,QAAQ;;CAGlD,cAAc,OAAqB;AACjC,iBAAe,KAAK,aAAa,MAAM,SAAS,GAAG,QAAQ,QAAQ;;CAGrE,mBAA2B;EACzB,MAAM,WAAW,KAAK,cAAc;AACpC,SAAO,WAAW,wBAAwB,aAAa"}