@dreb/coding-agent 2.1.0 → 2.2.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":"buddy-manager.d.ts","sourceRoot":"","sources":["../../../src/core/buddy/buddy-manager.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAW,KAAK,EAAE,MAAM,UAAU,CAAC;AAQ/C,OAAO,KAAK,EAAE,UAAU,EAAmC,MAAM,kBAAkB,CAAC;AA4DpF,MAAM,WAAW,YAAY;IAC5B,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;GAGG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,YAAY,CAAC,CAezD;AAaD;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAIvE;AAiID,qBAAa,YAAY;IACxB,OAAO,CAAC,KAAK,CAA2B;IACxC,OAAO,CAAC,YAAY,CAA6B;IAEjD,mDAAmD;IACnD,QAAQ,IAAI,UAAU,GAAG,IAAI,CAE5B;IAED,oCAAoC;IACpC,cAAc,IAAI,OAAO,CAExB;IAED;;;;OAIG;IACH,IAAI,IAAI,UAAU,GAAG,IAAI,CAOxB;IAED;;;OAGG;IACG,KAAK,CAAC,WAAW,EAAE,KAAK,CAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAmBzF;IAED;;OAEG;IACG,MAAM,CAAC,WAAW,EAAE,KAAK,CAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAmB1F;IAED,iDAAiD;IACjD,OAAO,IAAI,MAAM,GAAG,IAAI,CAEvB;YAMa,UAAU;IAmDxB;;;OAGG;IACG,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAejD;IAED;;;OAGG;IACG,iBAAiB,CAAC,WAAW,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAgB1F;IAED,+DAA+D;IAC/D,cAAc,IAAI,MAAM,GAAG,IAAI,CAE9B;IAED,kEAAkE;IAClE,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAWtC;IAED,2EAA2E;IAC3E,gBAAgB,IAAI,IAAI,CAEvB;IAED,kDAAkD;IAClD,SAAS,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI,CAU/B;CACD","sourcesContent":["/**\n * BuddyManager — Core state machine for the buddy companion.\n *\n * Handles: bone rolling, soul generation, persistence, Ollama availability checks.\n * Bones are deterministic from hash(username + hostname + salt + rerollCount).\n * Soul is LLM-generated once on first hatch and persisted to buddy.json.\n */\n\nimport type { Context, Model } from \"@dreb/ai\";\nimport { completeSimple } from \"@dreb/ai\";\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"fs\";\nimport { hostname } from \"os\";\nimport { join } from \"path\";\nimport { getAgentDir } from \"../../config.js\";\nimport { createBuddyRng } from \"./buddy-prng.js\";\nimport { rollEyes, rollHat, rollSpecies, rollStats } from \"./buddy-species.js\";\nimport type { BuddyState, CompanionBones, StoredCompanion } from \"./buddy-types.js\";\nimport { STAT_NAMES } from \"./buddy-types.js\";\n\nconst BUDDY_SALT = \"dreb-buddy-v1\";\nconst BUDDY_FILENAME = \"buddy.json\";\nconst DEFAULT_BACKSTORY = \"A mysterious past shrouded in legend.\";\n\n/** Base Ollama model config — id/name are set dynamically from available models */\nconst OLLAMA_MODEL_BASE: Omit<Model<\"openai-completions\">, \"id\" | \"name\"> = {\n\tapi: \"openai-completions\",\n\tprovider: \"ollama\",\n\tbaseUrl: \"http://localhost:11434/v1\",\n\treasoning: false,\n\tinput: [\"text\"],\n\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },\n\tcontextWindow: 128000,\n\tmaxTokens: 2048,\n\tcompat: {\n\t\tsupportsDeveloperRole: false,\n\t\tsupportsReasoningEffort: false,\n\t},\n};\n\n/** Max words for buddy response before truncation */\nconst MAX_RESPONSE_WORDS = 300;\n\n/** Prompt for soul generation (uses parent LLM, not Ollama) */\nconst SOUL_GENERATION_PROMPT = `You are generating a companion character for a coding assistant terminal app. Based on the species, rarity, and stats below, generate a creative name, a one-sentence personality description, and a funny fictional backstory.\n\nSpecies: {species}\nRarity: {rarity}\nStats: {stats}\nShiny: {shiny}\n\nThe name must NOT be a common English word, programming keyword, tool name, or command. It should be unique and distinctive — a proper noun that won't appear in normal conversation. The name must be 4-8 characters and easy to type on a QWERTY keyboard — use only common letters (a-z, avoid q, x, z, j). Do not use species name as the name.\n\nRespond in EXACTLY this format:\nNAME: <name>\nPERSONALITY: <one sentence personality>\nBACKSTORY: <2-3 sentence elaborate fictional backstory — funny, absurd, or dramatic. Include specific events, places, former occupations>`;\n\n/** Prompt for buddy reactions via Ollama */\nconst REACTION_PROMPT = `You are {name}, a {species} companion in a terminal coding app. You are {personality}. Your backstory: {backstory}\n\nSomething just happened. React with a short, in-character quip based on the context below. Be specific — reference what actually happened, not just that something happened. Max 20 words. No quotes, no prefixes, just the quip.\n\nContext:\n{event}`;\n\nconst NAME_CALL_PROMPT = `You are {name}, a {species} companion in a terminal coding app. You are {personality}. Your backstory: {backstory}\n\nThe user just said: \"{message}\"\nRecent context: {context}\n\nRespond to what the user said directly. Be in-character, reference your backstory occasionally. Max 30 words. No quotes, no prefixes, just your response.`;\n\n// =============================================================================\n// Ollama availability\n// =============================================================================\n\nexport interface OllamaStatus {\n\tavailable: boolean;\n\tmodels: string[];\n\terror?: string;\n}\n\n/**\n * Check if Ollama is running and has models available.\n * Uses the /api/tags endpoint.\n */\nexport async function checkOllama(): Promise<OllamaStatus> {\n\ttry {\n\t\tconst res = await fetch(\"http://localhost:11434/api/tags\", { signal: AbortSignal.timeout(3000) });\n\t\tif (!res.ok) {\n\t\t\treturn { available: false, models: [], error: `Ollama returned ${res.status}` };\n\t\t}\n\t\tconst data = (await res.json()) as { models?: { name: string }[] };\n\t\tconst models = (data.models ?? []).map((m) => m.name);\n\t\tif (models.length === 0) {\n\t\t\treturn { available: false, models: [], error: \"No models installed. Run: ollama pull llama3.2\" };\n\t\t}\n\t\treturn { available: true, models };\n\t} catch {\n\t\treturn { available: false, models: [], error: \"Ollama is not running. Start it with: ollama serve\" };\n\t}\n}\n\n/**\n * Pick the Ollama model for the buddy.\n * Returns the stored model name if it's available, otherwise null.\n */\nfunction pickOllamaModel(storedModel: string | undefined, availableModels: string[]): string | null {\n\tif (!storedModel) return null;\n\t// Check if the stored model is installed (exact match or prefix match without tag)\n\tconst match = availableModels.find((m) => m === storedModel || m.startsWith(`${storedModel}:`));\n\treturn match ?? null;\n}\n\n/**\n * Truncate response to a maximum word count, appending \"...[truncated]\" if exceeded.\n */\nexport function truncateResponse(text: string, maxWords: number): string {\n\tconst words = text.split(/\\s+/);\n\tif (words.length <= maxWords) return text;\n\treturn `${words.slice(0, maxWords).join(\" \")} ...[truncated]`;\n}\n\n// =============================================================================\n// Persistence\n// =============================================================================\n\nfunction getBuddyPath(): string {\n\treturn join(getAgentDir(), BUDDY_FILENAME);\n}\n\nfunction loadStored(): StoredCompanion | null {\n\tconst path = getBuddyPath();\n\tif (!existsSync(path)) return null;\n\ttry {\n\t\tconst data = JSON.parse(readFileSync(path, \"utf-8\"));\n\t\t// Validate required fields\n\t\tif (\n\t\t\ttypeof data.rerollCount === \"number\" &&\n\t\t\ttypeof data.name === \"string\" &&\n\t\t\ttypeof data.personality === \"string\"\n\t\t) {\n\t\t\treturn {\n\t\t\t\trerollCount: data.rerollCount,\n\t\t\t\tname: data.name,\n\t\t\t\tpersonality: data.personality,\n\t\t\t\tbackstory: typeof data.backstory === \"string\" ? data.backstory : DEFAULT_BACKSTORY,\n\t\t\t\thatchedAt: data.hatchedAt ?? new Date().toISOString(),\n\t\t\t\t...(data.hidden !== undefined ? { hidden: data.hidden } : {}),\n\t\t\t\t...(typeof data.ollamaModel === \"string\" ? { ollamaModel: data.ollamaModel } : {}),\n\t\t\t};\n\t\t}\n\t\treturn null;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nfunction saveStored(stored: StoredCompanion): void {\n\tconst path = getBuddyPath();\n\tconst dir = join(path, \"..\");\n\tif (!existsSync(dir)) {\n\t\tmkdirSync(dir, { recursive: true });\n\t}\n\twriteFileSync(path, JSON.stringify(stored, null, 2));\n}\n\n// =============================================================================\n// Bone rolling\n// =============================================================================\n\nfunction rollBones(rerollCount: number): CompanionBones {\n\tconst username = process.env.USER ?? process.env.LOGNAME ?? \"user\";\n\tconst host = hostname();\n\tconst rng = createBuddyRng(username, host, BUDDY_SALT, rerollCount);\n\n\t// Roll species + rarity\n\tconst { species, rarity } = rollSpecies(rng);\n\n\t// Roll shiny (1% chance)\n\tconst shiny = rng() < 0.01;\n\n\t// Roll eyes and hat\n\tconst eyes = rollEyes(rng);\n\tconst hat = rollHat(rng);\n\n\t// Roll stats\n\tconst stats = rollStats(rng, rarity);\n\n\treturn { species, rarity, shiny, stats, eyeStyle: eyes, hat };\n}\n\n// =============================================================================\n// Soul generation\n// =============================================================================\n\n/**\n * Generate a soul (name + personality + backstory) using the parent LLM.\n * Only called on first hatch or reroll.\n */\nasync function generateSoul(\n\tbones: CompanionBones,\n\tparentModel: Model<\"openai-completions\">,\n\tapiKey: string,\n): Promise<{ name: string; personality: string; backstory: string }> {\n\tconst statsStr = STAT_NAMES.map((s) => `${s}: ${bones.stats[s]}`).join(\", \");\n\tconst prompt = SOUL_GENERATION_PROMPT.replace(\"{species}\", bones.species)\n\t\t.replace(\"{rarity}\", bones.rarity)\n\t\t.replace(\"{stats}\", statsStr)\n\t\t.replace(\"{shiny}\", bones.shiny ? \"YES ✨\" : \"no\");\n\n\tconst context: Context = {\n\t\tsystemPrompt: \"Generate a companion character. Respond in the exact format requested.\",\n\t\tmessages: [{ role: \"user\", content: prompt, timestamp: Date.now() }],\n\t};\n\n\ttry {\n\t\tconst response = await completeSimple(parentModel, context, { apiKey });\n\t\tconst text = response.content\n\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t.map((c) => c.text)\n\t\t\t.join(\"\");\n\n\t\t// Parse NAME: ... and PERSONALITY: ... and BACKSTORY: ...\n\t\tconst nameMatch = text.match(/NAME:\\s*(.+)/i);\n\t\tconst personalityMatch = text.match(/PERSONALITY:\\s*(.+)/i);\n\t\tconst backstoryMatch = text.match(/BACKSTORY:\\s*([\\s\\S]+)/i);\n\n\t\tlet name = nameMatch?.[1]?.trim() ?? bones.species;\n\t\tconst personality = personalityMatch?.[1]?.trim() ?? `A ${bones.rarity} ${bones.species} companion.`;\n\t\tconst backstory = backstoryMatch?.[1]?.trim() ?? DEFAULT_BACKSTORY;\n\n\t\t// Enforce name length\n\t\tif (name.length > 8) name = name.slice(0, 8);\n\n\t\treturn { name, personality, backstory };\n\t} catch {\n\t\t// Fallback if LLM fails\n\t\treturn {\n\t\t\tname: bones.species,\n\t\t\tpersonality: `A ${bones.rarity} ${bones.species} companion.`,\n\t\t\tbackstory: DEFAULT_BACKSTORY,\n\t\t};\n\t}\n}\n\n// =============================================================================\n// BuddyManager\n// =============================================================================\n\nexport class BuddyManager {\n\tprivate state: BuddyState | null = null;\n\tprivate ollamaStatus: OllamaStatus | null = null;\n\n\t/** Get current buddy state (null if not loaded) */\n\tgetState(): BuddyState | null {\n\t\treturn this.state;\n\t}\n\n\t/** Check if buddy exists on disk */\n\thasStoredBuddy(): boolean {\n\t\treturn loadStored() !== null;\n\t}\n\n\t/**\n\t * Load or create buddy state.\n\t * If stored buddy exists, loads soul and re-rolls bones.\n\t * If no stored buddy, returns null (need to hatch first).\n\t */\n\tload(): BuddyState | null {\n\t\tconst stored = loadStored();\n\t\tif (!stored) return null;\n\n\t\tconst bones = rollBones(stored.rerollCount);\n\t\tthis.state = { ...bones, ...stored };\n\t\treturn this.state;\n\t}\n\n\t/**\n\t * Hatch a new buddy. Generates bones, then uses parent LLM for soul.\n\t * Returns the new state.\n\t */\n\tasync hatch(parentModel: Model<\"openai-completions\">, apiKey: string): Promise<BuddyState> {\n\t\tconst stored = loadStored();\n\t\tconst rerollCount = stored?.rerollCount ?? 0;\n\n\t\tconst bones = rollBones(rerollCount);\n\t\tconst { name, personality, backstory } = await generateSoul(bones, parentModel, apiKey);\n\n\t\tconst newStored: StoredCompanion = {\n\t\t\trerollCount,\n\t\t\tname,\n\t\t\tpersonality,\n\t\t\tbackstory,\n\t\t\thatchedAt: new Date().toISOString(),\n\t\t\t...(stored?.ollamaModel ? { ollamaModel: stored.ollamaModel } : {}),\n\t\t};\n\n\t\tsaveStored(newStored);\n\t\tthis.state = { ...bones, ...newStored };\n\t\treturn this.state;\n\t}\n\n\t/**\n\t * Reroll the buddy — new bones + new soul.\n\t */\n\tasync reroll(parentModel: Model<\"openai-completions\">, apiKey: string): Promise<BuddyState> {\n\t\tconst stored = loadStored();\n\t\tconst newRerollCount = (stored?.rerollCount ?? 0) + 1;\n\n\t\tconst bones = rollBones(newRerollCount);\n\t\tconst { name, personality, backstory } = await generateSoul(bones, parentModel, apiKey);\n\n\t\tconst newStored: StoredCompanion = {\n\t\t\trerollCount: newRerollCount,\n\t\t\tname,\n\t\t\tpersonality,\n\t\t\tbackstory,\n\t\t\thatchedAt: new Date().toISOString(),\n\t\t\t...(stored?.ollamaModel ? { ollamaModel: stored.ollamaModel } : {}),\n\t\t};\n\n\t\tsaveStored(newStored);\n\t\tthis.state = { ...bones, ...newStored };\n\t\treturn this.state;\n\t}\n\n\t/** Get buddy's name (for name-call detection) */\n\tgetName(): string | null {\n\t\treturn this.state?.name ?? loadStored()?.name ?? null;\n\t}\n\n\t/**\n\t * Shared Ollama chat helper. Checks availability, picks model, runs completion.\n\t * Returns the response text, or null if Ollama is unavailable or no model configured.\n\t */\n\tprivate async ollamaChat(context: Context): Promise<string | null> {\n\t\t// Check Ollama lazily, retry if previously unavailable\n\t\tif (!this.ollamaStatus || !this.ollamaStatus.available) {\n\t\t\tthis.ollamaStatus = await checkOllama();\n\t\t}\n\t\tif (!this.ollamaStatus.available) return null;\n\n\t\tconst modelName = pickOllamaModel(this.state?.ollamaModel, this.ollamaStatus.models);\n\t\tif (!modelName) return null;\n\t\tconst model: Model<\"openai-completions\"> = {\n\t\t\t...OLLAMA_MODEL_BASE,\n\t\t\tid: modelName,\n\t\t\tname: `${modelName} (Ollama)`,\n\t\t};\n\n\t\tlet response: import(\"@dreb/ai\").AssistantMessage;\n\t\ttry {\n\t\t\tresponse = await completeSimple(model, context, {\n\t\t\t\tapiKey: \"ollama\",\n\t\t\t\tsignal: AbortSignal.timeout(120000),\n\t\t\t});\n\t\t} catch {\n\t\t\t// Safety net for unexpected sync errors (e.g. provider not found).\n\t\t\t// Normal runtime errors (timeout, connection) are handled via stopReason below.\n\t\t\tthis.ollamaStatus = null;\n\t\t\treturn null;\n\t\t}\n\n\t\t// Connection error — invalidate cache so next attempt re-checks Ollama\n\t\tif (response.stopReason === \"error\") {\n\t\t\tthis.ollamaStatus = null;\n\t\t\treturn null;\n\t\t}\n\n\t\t// Timeout or abort — preserve cache (model is just slow, Ollama is fine)\n\t\tif (response.stopReason === \"aborted\") {\n\t\t\treturn null;\n\t\t}\n\n\t\tlet text = response.content\n\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t.map((c) => c.text)\n\t\t\t.join(\"\")\n\t\t\t.trim();\n\n\t\t// Truncate overly long responses\n\t\ttext = truncateResponse(text, MAX_RESPONSE_WORDS);\n\n\t\treturn text || null;\n\t}\n\n\t/**\n\t * Generate a reaction to an event using Ollama.\n\t * Returns null if Ollama is unavailable.\n\t */\n\tasync react(event: string): Promise<string | null> {\n\t\tif (!this.state) return null;\n\n\t\tconst prompt = REACTION_PROMPT.replace(\"{name}\", this.state.name)\n\t\t\t.replace(\"{species}\", this.state.species)\n\t\t\t.replace(\"{personality}\", this.state.personality)\n\t\t\t.replace(\"{backstory}\", this.state.backstory)\n\t\t\t.replace(\"{event}\", event);\n\n\t\tconst context: Context = {\n\t\t\tsystemPrompt: \"Respond with a short in-character quip. Max 20 words.\",\n\t\t\tmessages: [{ role: \"user\", content: prompt, timestamp: Date.now() }],\n\t\t};\n\n\t\treturn this.ollamaChat(context);\n\t}\n\n\t/**\n\t * Respond to the user calling the buddy's name.\n\t * Uses Ollama for the response.\n\t */\n\tasync respondToNameCall(userMessage: string, recentContext: string): Promise<string | null> {\n\t\tif (!this.state) return null;\n\n\t\tconst prompt = NAME_CALL_PROMPT.replace(\"{name}\", this.state.name)\n\t\t\t.replace(\"{species}\", this.state.species)\n\t\t\t.replace(\"{personality}\", this.state.personality)\n\t\t\t.replace(\"{backstory}\", this.state.backstory)\n\t\t\t.replace(\"{message}\", userMessage)\n\t\t\t.replace(\"{context}\", recentContext);\n\n\t\tconst context: Context = {\n\t\t\tsystemPrompt: \"Respond with a short friendly greeting. Max 30 words.\",\n\t\t\tmessages: [{ role: \"user\", content: prompt, timestamp: Date.now() }],\n\t\t};\n\n\t\treturn this.ollamaChat(context);\n\t}\n\n\t/** Get the configured Ollama model name, or null if not set */\n\tgetOllamaModel(): string | null {\n\t\treturn this.state?.ollamaModel ?? loadStored()?.ollamaModel ?? null;\n\t}\n\n\t/** Set the Ollama model for buddy reactions. Persists to disk. */\n\tsetOllamaModel(modelName: string): void {\n\t\tconst stored = loadStored();\n\t\tif (stored) {\n\t\t\tstored.ollamaModel = modelName;\n\t\t\tsaveStored(stored);\n\t\t}\n\t\tif (this.state) {\n\t\t\tthis.state.ollamaModel = modelName;\n\t\t}\n\t\t// Invalidate Ollama status cache so next call picks up the new model\n\t\tthis.ollamaStatus = null;\n\t}\n\n\t/** Reset Ollama status cache (e.g. after detecting it became available) */\n\tresetOllamaCache(): void {\n\t\tthis.ollamaStatus = null;\n\t}\n\n\t/** Update the hidden flag in persisted storage */\n\tsetHidden(hidden: boolean): void {\n\t\tconst stored = loadStored();\n\t\tif (stored) {\n\t\t\tstored.hidden = hidden;\n\t\t\tsaveStored(stored);\n\t\t}\n\t\t// Keep in-memory state in sync so reset() reads current hidden flag\n\t\tif (this.state) {\n\t\t\tthis.state.hidden = hidden;\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"buddy-manager.d.ts","sourceRoot":"","sources":["../../../src/core/buddy/buddy-manager.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAW,KAAK,EAAE,MAAM,UAAU,CAAC;AAQ/C,OAAO,KAAK,EAAE,UAAU,EAAmC,MAAM,kBAAkB,CAAC;AA4DpF,MAAM,WAAW,YAAY;IAC5B,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;GAGG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,YAAY,CAAC,CAezD;AAaD;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAIvE;AAiID,qBAAa,YAAY;IACxB,OAAO,CAAC,KAAK,CAA2B;IACxC,OAAO,CAAC,YAAY,CAA6B;IAEjD,mDAAmD;IACnD,QAAQ,IAAI,UAAU,GAAG,IAAI,CAE5B;IAED,oCAAoC;IACpC,cAAc,IAAI,OAAO,CAExB;IAED;;;;OAIG;IACH,IAAI,IAAI,UAAU,GAAG,IAAI,CAOxB;IAED;;;OAGG;IACG,KAAK,CAAC,WAAW,EAAE,KAAK,CAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAmBzF;IAED;;OAEG;IACG,MAAM,CAAC,WAAW,EAAE,KAAK,CAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAmB1F;IAED,iDAAiD;IACjD,OAAO,IAAI,MAAM,GAAG,IAAI,CAEvB;YAMa,UAAU;IAmDxB;;;OAGG;IACG,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAejD;IAED;;;OAGG;IACG,iBAAiB,CAAC,WAAW,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAgB1F;IAED,+DAA+D;IAC/D,cAAc,IAAI,MAAM,GAAG,IAAI,CAE9B;IAED,kEAAkE;IAClE,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAWtC;IAED,2EAA2E;IAC3E,gBAAgB,IAAI,IAAI,CAEvB;IAED,kDAAkD;IAClD,SAAS,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI,CAU/B;CACD","sourcesContent":["/**\n * BuddyManager — Core state machine for the buddy companion.\n *\n * Handles: bone rolling, soul generation, persistence, Ollama availability checks.\n * Bones are deterministic from hash(username + hostname + salt + rerollCount).\n * Soul is LLM-generated once on first hatch and persisted to buddy.json.\n */\n\nimport type { Context, Model } from \"@dreb/ai\";\nimport { completeSimple } from \"@dreb/ai\";\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"fs\";\nimport { hostname } from \"os\";\nimport { join } from \"path\";\nimport { getAgentDir } from \"../../config.js\";\nimport { createBuddyRng } from \"./buddy-prng.js\";\nimport { rollEyes, rollHat, rollSpecies, rollStats } from \"./buddy-species.js\";\nimport type { BuddyState, CompanionBones, StoredCompanion } from \"./buddy-types.js\";\nimport { STAT_NAMES } from \"./buddy-types.js\";\n\nconst BUDDY_SALT = \"dreb-buddy-v1\";\nconst BUDDY_FILENAME = \"buddy.json\";\nconst DEFAULT_BACKSTORY = \"A mysterious past shrouded in legend.\";\n\n/** Base Ollama model config — id/name are set dynamically from available models */\nconst OLLAMA_MODEL_BASE: Omit<Model<\"openai-completions\">, \"id\" | \"name\"> = {\n\tapi: \"openai-completions\",\n\tprovider: \"ollama\",\n\tbaseUrl: \"http://localhost:11434/v1\",\n\treasoning: false,\n\tinput: [\"text\"],\n\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },\n\tcontextWindow: 128000,\n\tmaxTokens: 2048,\n\tcompat: {\n\t\tsupportsDeveloperRole: false,\n\t\tsupportsReasoningEffort: false,\n\t},\n};\n\n/** Max words for buddy response before truncation */\nconst MAX_RESPONSE_WORDS = 60;\n\n/** Prompt for soul generation (uses parent LLM, not Ollama) */\nconst SOUL_GENERATION_PROMPT = `You are generating a companion character for a coding assistant terminal app. Based on the species, rarity, and stats below, generate a creative name, a one-sentence personality description, and a funny fictional backstory.\n\nSpecies: {species}\nRarity: {rarity}\nStats: {stats}\nShiny: {shiny}\n\nThe name must NOT be a common English word, programming keyword, tool name, or command. It should be unique and distinctive — a proper noun that won't appear in normal conversation. The name must be 4-8 characters and easy to type on a QWERTY keyboard — use only common letters (a-z, avoid q, x, z, j). Do not use species name as the name.\n\nRespond in EXACTLY this format:\nNAME: <name>\nPERSONALITY: <one sentence personality>\nBACKSTORY: <2-3 sentence elaborate fictional backstory — funny, absurd, or dramatic. Include specific events, places, former occupations>`;\n\n/** Prompt for buddy reactions via Ollama */\nconst REACTION_PROMPT = `You are {name}, a {species} companion in a terminal coding app. You are {personality}. Your backstory: {backstory}\n\nSomething just happened. React with a short, in-character quip based on the context below. Be specific — reference what actually happened, not just that something happened. Max 20 words. No quotes, no prefixes, just the quip.\n\nContext:\n{event}`;\n\nconst NAME_CALL_PROMPT = `You are {name}, a {species} companion in a terminal coding app. You are {personality}. Your backstory: {backstory}\n\nThe user just said: \"{message}\"\nRecent context: {context}\n\nRespond to what the user said directly. Be in-character, reference your backstory occasionally. Max 30 words. No quotes, no prefixes, just your response.`;\n\n// =============================================================================\n// Ollama availability\n// =============================================================================\n\nexport interface OllamaStatus {\n\tavailable: boolean;\n\tmodels: string[];\n\terror?: string;\n}\n\n/**\n * Check if Ollama is running and has models available.\n * Uses the /api/tags endpoint.\n */\nexport async function checkOllama(): Promise<OllamaStatus> {\n\ttry {\n\t\tconst res = await fetch(\"http://localhost:11434/api/tags\", { signal: AbortSignal.timeout(3000) });\n\t\tif (!res.ok) {\n\t\t\treturn { available: false, models: [], error: `Ollama returned ${res.status}` };\n\t\t}\n\t\tconst data = (await res.json()) as { models?: { name: string }[] };\n\t\tconst models = (data.models ?? []).map((m) => m.name);\n\t\tif (models.length === 0) {\n\t\t\treturn { available: false, models: [], error: \"No models installed. Run: ollama pull llama3.2\" };\n\t\t}\n\t\treturn { available: true, models };\n\t} catch {\n\t\treturn { available: false, models: [], error: \"Ollama is not running. Start it with: ollama serve\" };\n\t}\n}\n\n/**\n * Pick the Ollama model for the buddy.\n * Returns the stored model name if it's available, otherwise null.\n */\nfunction pickOllamaModel(storedModel: string | undefined, availableModels: string[]): string | null {\n\tif (!storedModel) return null;\n\t// Check if the stored model is installed (exact match or prefix match without tag)\n\tconst match = availableModels.find((m) => m === storedModel || m.startsWith(`${storedModel}:`));\n\treturn match ?? null;\n}\n\n/**\n * Truncate response to a maximum word count, appending \"...[truncated]\" if exceeded.\n */\nexport function truncateResponse(text: string, maxWords: number): string {\n\tconst words = text.split(/\\s+/);\n\tif (words.length <= maxWords) return text;\n\treturn `${words.slice(0, maxWords).join(\" \")} ...[truncated]`;\n}\n\n// =============================================================================\n// Persistence\n// =============================================================================\n\nfunction getBuddyPath(): string {\n\treturn join(getAgentDir(), BUDDY_FILENAME);\n}\n\nfunction loadStored(): StoredCompanion | null {\n\tconst path = getBuddyPath();\n\tif (!existsSync(path)) return null;\n\ttry {\n\t\tconst data = JSON.parse(readFileSync(path, \"utf-8\"));\n\t\t// Validate required fields\n\t\tif (\n\t\t\ttypeof data.rerollCount === \"number\" &&\n\t\t\ttypeof data.name === \"string\" &&\n\t\t\ttypeof data.personality === \"string\"\n\t\t) {\n\t\t\treturn {\n\t\t\t\trerollCount: data.rerollCount,\n\t\t\t\tname: data.name,\n\t\t\t\tpersonality: data.personality,\n\t\t\t\tbackstory: typeof data.backstory === \"string\" ? data.backstory : DEFAULT_BACKSTORY,\n\t\t\t\thatchedAt: data.hatchedAt ?? new Date().toISOString(),\n\t\t\t\t...(data.hidden !== undefined ? { hidden: data.hidden } : {}),\n\t\t\t\t...(typeof data.ollamaModel === \"string\" ? { ollamaModel: data.ollamaModel } : {}),\n\t\t\t};\n\t\t}\n\t\treturn null;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nfunction saveStored(stored: StoredCompanion): void {\n\tconst path = getBuddyPath();\n\tconst dir = join(path, \"..\");\n\tif (!existsSync(dir)) {\n\t\tmkdirSync(dir, { recursive: true });\n\t}\n\twriteFileSync(path, JSON.stringify(stored, null, 2));\n}\n\n// =============================================================================\n// Bone rolling\n// =============================================================================\n\nfunction rollBones(rerollCount: number): CompanionBones {\n\tconst username = process.env.USER ?? process.env.LOGNAME ?? \"user\";\n\tconst host = hostname();\n\tconst rng = createBuddyRng(username, host, BUDDY_SALT, rerollCount);\n\n\t// Roll species + rarity\n\tconst { species, rarity } = rollSpecies(rng);\n\n\t// Roll shiny (1% chance)\n\tconst shiny = rng() < 0.01;\n\n\t// Roll eyes and hat\n\tconst eyes = rollEyes(rng);\n\tconst hat = rollHat(rng);\n\n\t// Roll stats\n\tconst stats = rollStats(rng, rarity);\n\n\treturn { species, rarity, shiny, stats, eyeStyle: eyes, hat };\n}\n\n// =============================================================================\n// Soul generation\n// =============================================================================\n\n/**\n * Generate a soul (name + personality + backstory) using the parent LLM.\n * Only called on first hatch or reroll.\n */\nasync function generateSoul(\n\tbones: CompanionBones,\n\tparentModel: Model<\"openai-completions\">,\n\tapiKey: string,\n): Promise<{ name: string; personality: string; backstory: string }> {\n\tconst statsStr = STAT_NAMES.map((s) => `${s}: ${bones.stats[s]}`).join(\", \");\n\tconst prompt = SOUL_GENERATION_PROMPT.replace(\"{species}\", bones.species)\n\t\t.replace(\"{rarity}\", bones.rarity)\n\t\t.replace(\"{stats}\", statsStr)\n\t\t.replace(\"{shiny}\", bones.shiny ? \"YES ✨\" : \"no\");\n\n\tconst context: Context = {\n\t\tsystemPrompt: \"Generate a companion character. Respond in the exact format requested.\",\n\t\tmessages: [{ role: \"user\", content: prompt, timestamp: Date.now() }],\n\t};\n\n\ttry {\n\t\tconst response = await completeSimple(parentModel, context, { apiKey });\n\t\tconst text = response.content\n\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t.map((c) => c.text)\n\t\t\t.join(\"\");\n\n\t\t// Parse NAME: ... and PERSONALITY: ... and BACKSTORY: ...\n\t\tconst nameMatch = text.match(/NAME:\\s*(.+)/i);\n\t\tconst personalityMatch = text.match(/PERSONALITY:\\s*(.+)/i);\n\t\tconst backstoryMatch = text.match(/BACKSTORY:\\s*([\\s\\S]+)/i);\n\n\t\tlet name = nameMatch?.[1]?.trim() ?? bones.species;\n\t\tconst personality = personalityMatch?.[1]?.trim() ?? `A ${bones.rarity} ${bones.species} companion.`;\n\t\tconst backstory = backstoryMatch?.[1]?.trim() ?? DEFAULT_BACKSTORY;\n\n\t\t// Enforce name length\n\t\tif (name.length > 8) name = name.slice(0, 8);\n\n\t\treturn { name, personality, backstory };\n\t} catch {\n\t\t// Fallback if LLM fails\n\t\treturn {\n\t\t\tname: bones.species,\n\t\t\tpersonality: `A ${bones.rarity} ${bones.species} companion.`,\n\t\t\tbackstory: DEFAULT_BACKSTORY,\n\t\t};\n\t}\n}\n\n// =============================================================================\n// BuddyManager\n// =============================================================================\n\nexport class BuddyManager {\n\tprivate state: BuddyState | null = null;\n\tprivate ollamaStatus: OllamaStatus | null = null;\n\n\t/** Get current buddy state (null if not loaded) */\n\tgetState(): BuddyState | null {\n\t\treturn this.state;\n\t}\n\n\t/** Check if buddy exists on disk */\n\thasStoredBuddy(): boolean {\n\t\treturn loadStored() !== null;\n\t}\n\n\t/**\n\t * Load or create buddy state.\n\t * If stored buddy exists, loads soul and re-rolls bones.\n\t * If no stored buddy, returns null (need to hatch first).\n\t */\n\tload(): BuddyState | null {\n\t\tconst stored = loadStored();\n\t\tif (!stored) return null;\n\n\t\tconst bones = rollBones(stored.rerollCount);\n\t\tthis.state = { ...bones, ...stored };\n\t\treturn this.state;\n\t}\n\n\t/**\n\t * Hatch a new buddy. Generates bones, then uses parent LLM for soul.\n\t * Returns the new state.\n\t */\n\tasync hatch(parentModel: Model<\"openai-completions\">, apiKey: string): Promise<BuddyState> {\n\t\tconst stored = loadStored();\n\t\tconst rerollCount = stored?.rerollCount ?? 0;\n\n\t\tconst bones = rollBones(rerollCount);\n\t\tconst { name, personality, backstory } = await generateSoul(bones, parentModel, apiKey);\n\n\t\tconst newStored: StoredCompanion = {\n\t\t\trerollCount,\n\t\t\tname,\n\t\t\tpersonality,\n\t\t\tbackstory,\n\t\t\thatchedAt: new Date().toISOString(),\n\t\t\t...(stored?.ollamaModel ? { ollamaModel: stored.ollamaModel } : {}),\n\t\t};\n\n\t\tsaveStored(newStored);\n\t\tthis.state = { ...bones, ...newStored };\n\t\treturn this.state;\n\t}\n\n\t/**\n\t * Reroll the buddy — new bones + new soul.\n\t */\n\tasync reroll(parentModel: Model<\"openai-completions\">, apiKey: string): Promise<BuddyState> {\n\t\tconst stored = loadStored();\n\t\tconst newRerollCount = (stored?.rerollCount ?? 0) + 1;\n\n\t\tconst bones = rollBones(newRerollCount);\n\t\tconst { name, personality, backstory } = await generateSoul(bones, parentModel, apiKey);\n\n\t\tconst newStored: StoredCompanion = {\n\t\t\trerollCount: newRerollCount,\n\t\t\tname,\n\t\t\tpersonality,\n\t\t\tbackstory,\n\t\t\thatchedAt: new Date().toISOString(),\n\t\t\t...(stored?.ollamaModel ? { ollamaModel: stored.ollamaModel } : {}),\n\t\t};\n\n\t\tsaveStored(newStored);\n\t\tthis.state = { ...bones, ...newStored };\n\t\treturn this.state;\n\t}\n\n\t/** Get buddy's name (for name-call detection) */\n\tgetName(): string | null {\n\t\treturn this.state?.name ?? loadStored()?.name ?? null;\n\t}\n\n\t/**\n\t * Shared Ollama chat helper. Checks availability, picks model, runs completion.\n\t * Returns the response text, or null if Ollama is unavailable or no model configured.\n\t */\n\tprivate async ollamaChat(context: Context): Promise<string | null> {\n\t\t// Check Ollama lazily, retry if previously unavailable\n\t\tif (!this.ollamaStatus || !this.ollamaStatus.available) {\n\t\t\tthis.ollamaStatus = await checkOllama();\n\t\t}\n\t\tif (!this.ollamaStatus.available) return null;\n\n\t\tconst modelName = pickOllamaModel(this.state?.ollamaModel, this.ollamaStatus.models);\n\t\tif (!modelName) return null;\n\t\tconst model: Model<\"openai-completions\"> = {\n\t\t\t...OLLAMA_MODEL_BASE,\n\t\t\tid: modelName,\n\t\t\tname: `${modelName} (Ollama)`,\n\t\t};\n\n\t\tlet response: import(\"@dreb/ai\").AssistantMessage;\n\t\ttry {\n\t\t\tresponse = await completeSimple(model, context, {\n\t\t\t\tapiKey: \"ollama\",\n\t\t\t\tsignal: AbortSignal.timeout(120000),\n\t\t\t});\n\t\t} catch {\n\t\t\t// Safety net for unexpected sync errors (e.g. provider not found).\n\t\t\t// Normal runtime errors (timeout, connection) are handled via stopReason below.\n\t\t\tthis.ollamaStatus = null;\n\t\t\treturn null;\n\t\t}\n\n\t\t// Connection error — invalidate cache so next attempt re-checks Ollama\n\t\tif (response.stopReason === \"error\") {\n\t\t\tthis.ollamaStatus = null;\n\t\t\treturn null;\n\t\t}\n\n\t\t// Timeout or abort — preserve cache (model is just slow, Ollama is fine)\n\t\tif (response.stopReason === \"aborted\") {\n\t\t\treturn null;\n\t\t}\n\n\t\tlet text = response.content\n\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t.map((c) => c.text)\n\t\t\t.join(\"\")\n\t\t\t.trim();\n\n\t\t// Truncate overly long responses\n\t\ttext = truncateResponse(text, MAX_RESPONSE_WORDS);\n\n\t\treturn text || null;\n\t}\n\n\t/**\n\t * Generate a reaction to an event using Ollama.\n\t * Returns null if Ollama is unavailable.\n\t */\n\tasync react(event: string): Promise<string | null> {\n\t\tif (!this.state) return null;\n\n\t\tconst prompt = REACTION_PROMPT.replace(\"{name}\", this.state.name)\n\t\t\t.replace(\"{species}\", this.state.species)\n\t\t\t.replace(\"{personality}\", this.state.personality)\n\t\t\t.replace(\"{backstory}\", this.state.backstory)\n\t\t\t.replace(\"{event}\", event);\n\n\t\tconst context: Context = {\n\t\t\tsystemPrompt: \"Respond with a short in-character quip. Max 20 words.\",\n\t\t\tmessages: [{ role: \"user\", content: prompt, timestamp: Date.now() }],\n\t\t};\n\n\t\treturn this.ollamaChat(context);\n\t}\n\n\t/**\n\t * Respond to the user calling the buddy's name.\n\t * Uses Ollama for the response.\n\t */\n\tasync respondToNameCall(userMessage: string, recentContext: string): Promise<string | null> {\n\t\tif (!this.state) return null;\n\n\t\tconst prompt = NAME_CALL_PROMPT.replace(\"{name}\", this.state.name)\n\t\t\t.replace(\"{species}\", this.state.species)\n\t\t\t.replace(\"{personality}\", this.state.personality)\n\t\t\t.replace(\"{backstory}\", this.state.backstory)\n\t\t\t.replace(\"{message}\", userMessage)\n\t\t\t.replace(\"{context}\", recentContext);\n\n\t\tconst context: Context = {\n\t\t\tsystemPrompt: \"Respond with a short friendly greeting. Max 30 words.\",\n\t\t\tmessages: [{ role: \"user\", content: prompt, timestamp: Date.now() }],\n\t\t};\n\n\t\treturn this.ollamaChat(context);\n\t}\n\n\t/** Get the configured Ollama model name, or null if not set */\n\tgetOllamaModel(): string | null {\n\t\treturn this.state?.ollamaModel ?? loadStored()?.ollamaModel ?? null;\n\t}\n\n\t/** Set the Ollama model for buddy reactions. Persists to disk. */\n\tsetOllamaModel(modelName: string): void {\n\t\tconst stored = loadStored();\n\t\tif (stored) {\n\t\t\tstored.ollamaModel = modelName;\n\t\t\tsaveStored(stored);\n\t\t}\n\t\tif (this.state) {\n\t\t\tthis.state.ollamaModel = modelName;\n\t\t}\n\t\t// Invalidate Ollama status cache so next call picks up the new model\n\t\tthis.ollamaStatus = null;\n\t}\n\n\t/** Reset Ollama status cache (e.g. after detecting it became available) */\n\tresetOllamaCache(): void {\n\t\tthis.ollamaStatus = null;\n\t}\n\n\t/** Update the hidden flag in persisted storage */\n\tsetHidden(hidden: boolean): void {\n\t\tconst stored = loadStored();\n\t\tif (stored) {\n\t\t\tstored.hidden = hidden;\n\t\t\tsaveStored(stored);\n\t\t}\n\t\t// Keep in-memory state in sync so reset() reads current hidden flag\n\t\tif (this.state) {\n\t\t\tthis.state.hidden = hidden;\n\t\t}\n\t}\n}\n"]}
@@ -32,7 +32,7 @@ const OLLAMA_MODEL_BASE = {
32
32
  },
33
33
  };
34
34
  /** Max words for buddy response before truncation */
35
- const MAX_RESPONSE_WORDS = 300;
35
+ const MAX_RESPONSE_WORDS = 60;
36
36
  /** Prompt for soul generation (uses parent LLM, not Ollama) */
37
37
  const SOUL_GENERATION_PROMPT = `You are generating a companion character for a coding assistant terminal app. Based on the species, rarity, and stats below, generate a creative name, a one-sentence personality description, and a funny fictional backstory.
38
38
 
@@ -1 +1 @@
1
- {"version":3,"file":"buddy-manager.js","sourceRoot":"","sources":["../../../src/core/buddy/buddy-manager.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AACxE,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAC9B,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/E,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,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AE,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;SACpE,CAAC;QAEF,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAAA,CAChC;IAED,+DAA+D;IAC/D,cAAc,GAAkB;QAC/B,OAAO,IAAI,CAAC,KAAK,EAAE,WAAW,IAAI,UAAU,EAAE,EAAE,WAAW,IAAI,IAAI,CAAC;IAAA,CACpE;IAED,kEAAkE;IAClE,cAAc,CAAC,SAAiB,EAAQ;QACvC,MAAM,MAAM,GAAG,UAAU,EAAE,CAAC;QAC5B,IAAI,MAAM,EAAE,CAAC;YACZ,MAAM,CAAC,WAAW,GAAG,SAAS,CAAC;YAC/B,UAAU,CAAC,MAAM,CAAC,CAAC;QACpB,CAAC;QACD,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YAChB,IAAI,CAAC,KAAK,CAAC,WAAW,GAAG,SAAS,CAAC;QACpC,CAAC;QACD,qEAAqE;QACrE,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;IAAA,CACzB;IAED,2EAA2E;IAC3E,gBAAgB,GAAS;QACxB,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;IAAA,CACzB;IAED,kDAAkD;IAClD,SAAS,CAAC,MAAe,EAAQ;QAChC,MAAM,MAAM,GAAG,UAAU,EAAE,CAAC;QAC5B,IAAI,MAAM,EAAE,CAAC;YACZ,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;YACvB,UAAU,CAAC,MAAM,CAAC,CAAC;QACpB,CAAC;QACD,oEAAoE;QACpE,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YAChB,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC;QAC5B,CAAC;IAAA,CACD;CACD","sourcesContent":["/**\n * BuddyManager — Core state machine for the buddy companion.\n *\n * Handles: bone rolling, soul generation, persistence, Ollama availability checks.\n * Bones are deterministic from hash(username + hostname + salt + rerollCount).\n * Soul is LLM-generated once on first hatch and persisted to buddy.json.\n */\n\nimport type { Context, Model } from \"@dreb/ai\";\nimport { completeSimple } from \"@dreb/ai\";\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"fs\";\nimport { hostname } from \"os\";\nimport { join } from \"path\";\nimport { getAgentDir } from \"../../config.js\";\nimport { createBuddyRng } from \"./buddy-prng.js\";\nimport { rollEyes, rollHat, rollSpecies, rollStats } from \"./buddy-species.js\";\nimport type { BuddyState, CompanionBones, StoredCompanion } from \"./buddy-types.js\";\nimport { STAT_NAMES } from \"./buddy-types.js\";\n\nconst BUDDY_SALT = \"dreb-buddy-v1\";\nconst BUDDY_FILENAME = \"buddy.json\";\nconst DEFAULT_BACKSTORY = \"A mysterious past shrouded in legend.\";\n\n/** Base Ollama model config — id/name are set dynamically from available models */\nconst OLLAMA_MODEL_BASE: Omit<Model<\"openai-completions\">, \"id\" | \"name\"> = {\n\tapi: \"openai-completions\",\n\tprovider: \"ollama\",\n\tbaseUrl: \"http://localhost:11434/v1\",\n\treasoning: false,\n\tinput: [\"text\"],\n\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },\n\tcontextWindow: 128000,\n\tmaxTokens: 2048,\n\tcompat: {\n\t\tsupportsDeveloperRole: false,\n\t\tsupportsReasoningEffort: false,\n\t},\n};\n\n/** Max words for buddy response before truncation */\nconst MAX_RESPONSE_WORDS = 300;\n\n/** Prompt for soul generation (uses parent LLM, not Ollama) */\nconst SOUL_GENERATION_PROMPT = `You are generating a companion character for a coding assistant terminal app. Based on the species, rarity, and stats below, generate a creative name, a one-sentence personality description, and a funny fictional backstory.\n\nSpecies: {species}\nRarity: {rarity}\nStats: {stats}\nShiny: {shiny}\n\nThe name must NOT be a common English word, programming keyword, tool name, or command. It should be unique and distinctive — a proper noun that won't appear in normal conversation. The name must be 4-8 characters and easy to type on a QWERTY keyboard — use only common letters (a-z, avoid q, x, z, j). Do not use species name as the name.\n\nRespond in EXACTLY this format:\nNAME: <name>\nPERSONALITY: <one sentence personality>\nBACKSTORY: <2-3 sentence elaborate fictional backstory — funny, absurd, or dramatic. Include specific events, places, former occupations>`;\n\n/** Prompt for buddy reactions via Ollama */\nconst REACTION_PROMPT = `You are {name}, a {species} companion in a terminal coding app. You are {personality}. Your backstory: {backstory}\n\nSomething just happened. React with a short, in-character quip based on the context below. Be specific — reference what actually happened, not just that something happened. Max 20 words. No quotes, no prefixes, just the quip.\n\nContext:\n{event}`;\n\nconst NAME_CALL_PROMPT = `You are {name}, a {species} companion in a terminal coding app. You are {personality}. Your backstory: {backstory}\n\nThe user just said: \"{message}\"\nRecent context: {context}\n\nRespond to what the user said directly. Be in-character, reference your backstory occasionally. Max 30 words. No quotes, no prefixes, just your response.`;\n\n// =============================================================================\n// Ollama availability\n// =============================================================================\n\nexport interface OllamaStatus {\n\tavailable: boolean;\n\tmodels: string[];\n\terror?: string;\n}\n\n/**\n * Check if Ollama is running and has models available.\n * Uses the /api/tags endpoint.\n */\nexport async function checkOllama(): Promise<OllamaStatus> {\n\ttry {\n\t\tconst res = await fetch(\"http://localhost:11434/api/tags\", { signal: AbortSignal.timeout(3000) });\n\t\tif (!res.ok) {\n\t\t\treturn { available: false, models: [], error: `Ollama returned ${res.status}` };\n\t\t}\n\t\tconst data = (await res.json()) as { models?: { name: string }[] };\n\t\tconst models = (data.models ?? []).map((m) => m.name);\n\t\tif (models.length === 0) {\n\t\t\treturn { available: false, models: [], error: \"No models installed. Run: ollama pull llama3.2\" };\n\t\t}\n\t\treturn { available: true, models };\n\t} catch {\n\t\treturn { available: false, models: [], error: \"Ollama is not running. Start it with: ollama serve\" };\n\t}\n}\n\n/**\n * Pick the Ollama model for the buddy.\n * Returns the stored model name if it's available, otherwise null.\n */\nfunction pickOllamaModel(storedModel: string | undefined, availableModels: string[]): string | null {\n\tif (!storedModel) return null;\n\t// Check if the stored model is installed (exact match or prefix match without tag)\n\tconst match = availableModels.find((m) => m === storedModel || m.startsWith(`${storedModel}:`));\n\treturn match ?? null;\n}\n\n/**\n * Truncate response to a maximum word count, appending \"...[truncated]\" if exceeded.\n */\nexport function truncateResponse(text: string, maxWords: number): string {\n\tconst words = text.split(/\\s+/);\n\tif (words.length <= maxWords) return text;\n\treturn `${words.slice(0, maxWords).join(\" \")} ...[truncated]`;\n}\n\n// =============================================================================\n// Persistence\n// =============================================================================\n\nfunction getBuddyPath(): string {\n\treturn join(getAgentDir(), BUDDY_FILENAME);\n}\n\nfunction loadStored(): StoredCompanion | null {\n\tconst path = getBuddyPath();\n\tif (!existsSync(path)) return null;\n\ttry {\n\t\tconst data = JSON.parse(readFileSync(path, \"utf-8\"));\n\t\t// Validate required fields\n\t\tif (\n\t\t\ttypeof data.rerollCount === \"number\" &&\n\t\t\ttypeof data.name === \"string\" &&\n\t\t\ttypeof data.personality === \"string\"\n\t\t) {\n\t\t\treturn {\n\t\t\t\trerollCount: data.rerollCount,\n\t\t\t\tname: data.name,\n\t\t\t\tpersonality: data.personality,\n\t\t\t\tbackstory: typeof data.backstory === \"string\" ? data.backstory : DEFAULT_BACKSTORY,\n\t\t\t\thatchedAt: data.hatchedAt ?? new Date().toISOString(),\n\t\t\t\t...(data.hidden !== undefined ? { hidden: data.hidden } : {}),\n\t\t\t\t...(typeof data.ollamaModel === \"string\" ? { ollamaModel: data.ollamaModel } : {}),\n\t\t\t};\n\t\t}\n\t\treturn null;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nfunction saveStored(stored: StoredCompanion): void {\n\tconst path = getBuddyPath();\n\tconst dir = join(path, \"..\");\n\tif (!existsSync(dir)) {\n\t\tmkdirSync(dir, { recursive: true });\n\t}\n\twriteFileSync(path, JSON.stringify(stored, null, 2));\n}\n\n// =============================================================================\n// Bone rolling\n// =============================================================================\n\nfunction rollBones(rerollCount: number): CompanionBones {\n\tconst username = process.env.USER ?? process.env.LOGNAME ?? \"user\";\n\tconst host = hostname();\n\tconst rng = createBuddyRng(username, host, BUDDY_SALT, rerollCount);\n\n\t// Roll species + rarity\n\tconst { species, rarity } = rollSpecies(rng);\n\n\t// Roll shiny (1% chance)\n\tconst shiny = rng() < 0.01;\n\n\t// Roll eyes and hat\n\tconst eyes = rollEyes(rng);\n\tconst hat = rollHat(rng);\n\n\t// Roll stats\n\tconst stats = rollStats(rng, rarity);\n\n\treturn { species, rarity, shiny, stats, eyeStyle: eyes, hat };\n}\n\n// =============================================================================\n// Soul generation\n// =============================================================================\n\n/**\n * Generate a soul (name + personality + backstory) using the parent LLM.\n * Only called on first hatch or reroll.\n */\nasync function generateSoul(\n\tbones: CompanionBones,\n\tparentModel: Model<\"openai-completions\">,\n\tapiKey: string,\n): Promise<{ name: string; personality: string; backstory: string }> {\n\tconst statsStr = STAT_NAMES.map((s) => `${s}: ${bones.stats[s]}`).join(\", \");\n\tconst prompt = SOUL_GENERATION_PROMPT.replace(\"{species}\", bones.species)\n\t\t.replace(\"{rarity}\", bones.rarity)\n\t\t.replace(\"{stats}\", statsStr)\n\t\t.replace(\"{shiny}\", bones.shiny ? \"YES ✨\" : \"no\");\n\n\tconst context: Context = {\n\t\tsystemPrompt: \"Generate a companion character. Respond in the exact format requested.\",\n\t\tmessages: [{ role: \"user\", content: prompt, timestamp: Date.now() }],\n\t};\n\n\ttry {\n\t\tconst response = await completeSimple(parentModel, context, { apiKey });\n\t\tconst text = response.content\n\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t.map((c) => c.text)\n\t\t\t.join(\"\");\n\n\t\t// Parse NAME: ... and PERSONALITY: ... and BACKSTORY: ...\n\t\tconst nameMatch = text.match(/NAME:\\s*(.+)/i);\n\t\tconst personalityMatch = text.match(/PERSONALITY:\\s*(.+)/i);\n\t\tconst backstoryMatch = text.match(/BACKSTORY:\\s*([\\s\\S]+)/i);\n\n\t\tlet name = nameMatch?.[1]?.trim() ?? bones.species;\n\t\tconst personality = personalityMatch?.[1]?.trim() ?? `A ${bones.rarity} ${bones.species} companion.`;\n\t\tconst backstory = backstoryMatch?.[1]?.trim() ?? DEFAULT_BACKSTORY;\n\n\t\t// Enforce name length\n\t\tif (name.length > 8) name = name.slice(0, 8);\n\n\t\treturn { name, personality, backstory };\n\t} catch {\n\t\t// Fallback if LLM fails\n\t\treturn {\n\t\t\tname: bones.species,\n\t\t\tpersonality: `A ${bones.rarity} ${bones.species} companion.`,\n\t\t\tbackstory: DEFAULT_BACKSTORY,\n\t\t};\n\t}\n}\n\n// =============================================================================\n// BuddyManager\n// =============================================================================\n\nexport class BuddyManager {\n\tprivate state: BuddyState | null = null;\n\tprivate ollamaStatus: OllamaStatus | null = null;\n\n\t/** Get current buddy state (null if not loaded) */\n\tgetState(): BuddyState | null {\n\t\treturn this.state;\n\t}\n\n\t/** Check if buddy exists on disk */\n\thasStoredBuddy(): boolean {\n\t\treturn loadStored() !== null;\n\t}\n\n\t/**\n\t * Load or create buddy state.\n\t * If stored buddy exists, loads soul and re-rolls bones.\n\t * If no stored buddy, returns null (need to hatch first).\n\t */\n\tload(): BuddyState | null {\n\t\tconst stored = loadStored();\n\t\tif (!stored) return null;\n\n\t\tconst bones = rollBones(stored.rerollCount);\n\t\tthis.state = { ...bones, ...stored };\n\t\treturn this.state;\n\t}\n\n\t/**\n\t * Hatch a new buddy. Generates bones, then uses parent LLM for soul.\n\t * Returns the new state.\n\t */\n\tasync hatch(parentModel: Model<\"openai-completions\">, apiKey: string): Promise<BuddyState> {\n\t\tconst stored = loadStored();\n\t\tconst rerollCount = stored?.rerollCount ?? 0;\n\n\t\tconst bones = rollBones(rerollCount);\n\t\tconst { name, personality, backstory } = await generateSoul(bones, parentModel, apiKey);\n\n\t\tconst newStored: StoredCompanion = {\n\t\t\trerollCount,\n\t\t\tname,\n\t\t\tpersonality,\n\t\t\tbackstory,\n\t\t\thatchedAt: new Date().toISOString(),\n\t\t\t...(stored?.ollamaModel ? { ollamaModel: stored.ollamaModel } : {}),\n\t\t};\n\n\t\tsaveStored(newStored);\n\t\tthis.state = { ...bones, ...newStored };\n\t\treturn this.state;\n\t}\n\n\t/**\n\t * Reroll the buddy — new bones + new soul.\n\t */\n\tasync reroll(parentModel: Model<\"openai-completions\">, apiKey: string): Promise<BuddyState> {\n\t\tconst stored = loadStored();\n\t\tconst newRerollCount = (stored?.rerollCount ?? 0) + 1;\n\n\t\tconst bones = rollBones(newRerollCount);\n\t\tconst { name, personality, backstory } = await generateSoul(bones, parentModel, apiKey);\n\n\t\tconst newStored: StoredCompanion = {\n\t\t\trerollCount: newRerollCount,\n\t\t\tname,\n\t\t\tpersonality,\n\t\t\tbackstory,\n\t\t\thatchedAt: new Date().toISOString(),\n\t\t\t...(stored?.ollamaModel ? { ollamaModel: stored.ollamaModel } : {}),\n\t\t};\n\n\t\tsaveStored(newStored);\n\t\tthis.state = { ...bones, ...newStored };\n\t\treturn this.state;\n\t}\n\n\t/** Get buddy's name (for name-call detection) */\n\tgetName(): string | null {\n\t\treturn this.state?.name ?? loadStored()?.name ?? null;\n\t}\n\n\t/**\n\t * Shared Ollama chat helper. Checks availability, picks model, runs completion.\n\t * Returns the response text, or null if Ollama is unavailable or no model configured.\n\t */\n\tprivate async ollamaChat(context: Context): Promise<string | null> {\n\t\t// Check Ollama lazily, retry if previously unavailable\n\t\tif (!this.ollamaStatus || !this.ollamaStatus.available) {\n\t\t\tthis.ollamaStatus = await checkOllama();\n\t\t}\n\t\tif (!this.ollamaStatus.available) return null;\n\n\t\tconst modelName = pickOllamaModel(this.state?.ollamaModel, this.ollamaStatus.models);\n\t\tif (!modelName) return null;\n\t\tconst model: Model<\"openai-completions\"> = {\n\t\t\t...OLLAMA_MODEL_BASE,\n\t\t\tid: modelName,\n\t\t\tname: `${modelName} (Ollama)`,\n\t\t};\n\n\t\tlet response: import(\"@dreb/ai\").AssistantMessage;\n\t\ttry {\n\t\t\tresponse = await completeSimple(model, context, {\n\t\t\t\tapiKey: \"ollama\",\n\t\t\t\tsignal: AbortSignal.timeout(120000),\n\t\t\t});\n\t\t} catch {\n\t\t\t// Safety net for unexpected sync errors (e.g. provider not found).\n\t\t\t// Normal runtime errors (timeout, connection) are handled via stopReason below.\n\t\t\tthis.ollamaStatus = null;\n\t\t\treturn null;\n\t\t}\n\n\t\t// Connection error — invalidate cache so next attempt re-checks Ollama\n\t\tif (response.stopReason === \"error\") {\n\t\t\tthis.ollamaStatus = null;\n\t\t\treturn null;\n\t\t}\n\n\t\t// Timeout or abort — preserve cache (model is just slow, Ollama is fine)\n\t\tif (response.stopReason === \"aborted\") {\n\t\t\treturn null;\n\t\t}\n\n\t\tlet text = response.content\n\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t.map((c) => c.text)\n\t\t\t.join(\"\")\n\t\t\t.trim();\n\n\t\t// Truncate overly long responses\n\t\ttext = truncateResponse(text, MAX_RESPONSE_WORDS);\n\n\t\treturn text || null;\n\t}\n\n\t/**\n\t * Generate a reaction to an event using Ollama.\n\t * Returns null if Ollama is unavailable.\n\t */\n\tasync react(event: string): Promise<string | null> {\n\t\tif (!this.state) return null;\n\n\t\tconst prompt = REACTION_PROMPT.replace(\"{name}\", this.state.name)\n\t\t\t.replace(\"{species}\", this.state.species)\n\t\t\t.replace(\"{personality}\", this.state.personality)\n\t\t\t.replace(\"{backstory}\", this.state.backstory)\n\t\t\t.replace(\"{event}\", event);\n\n\t\tconst context: Context = {\n\t\t\tsystemPrompt: \"Respond with a short in-character quip. Max 20 words.\",\n\t\t\tmessages: [{ role: \"user\", content: prompt, timestamp: Date.now() }],\n\t\t};\n\n\t\treturn this.ollamaChat(context);\n\t}\n\n\t/**\n\t * Respond to the user calling the buddy's name.\n\t * Uses Ollama for the response.\n\t */\n\tasync respondToNameCall(userMessage: string, recentContext: string): Promise<string | null> {\n\t\tif (!this.state) return null;\n\n\t\tconst prompt = NAME_CALL_PROMPT.replace(\"{name}\", this.state.name)\n\t\t\t.replace(\"{species}\", this.state.species)\n\t\t\t.replace(\"{personality}\", this.state.personality)\n\t\t\t.replace(\"{backstory}\", this.state.backstory)\n\t\t\t.replace(\"{message}\", userMessage)\n\t\t\t.replace(\"{context}\", recentContext);\n\n\t\tconst context: Context = {\n\t\t\tsystemPrompt: \"Respond with a short friendly greeting. Max 30 words.\",\n\t\t\tmessages: [{ role: \"user\", content: prompt, timestamp: Date.now() }],\n\t\t};\n\n\t\treturn this.ollamaChat(context);\n\t}\n\n\t/** Get the configured Ollama model name, or null if not set */\n\tgetOllamaModel(): string | null {\n\t\treturn this.state?.ollamaModel ?? loadStored()?.ollamaModel ?? null;\n\t}\n\n\t/** Set the Ollama model for buddy reactions. Persists to disk. */\n\tsetOllamaModel(modelName: string): void {\n\t\tconst stored = loadStored();\n\t\tif (stored) {\n\t\t\tstored.ollamaModel = modelName;\n\t\t\tsaveStored(stored);\n\t\t}\n\t\tif (this.state) {\n\t\t\tthis.state.ollamaModel = modelName;\n\t\t}\n\t\t// Invalidate Ollama status cache so next call picks up the new model\n\t\tthis.ollamaStatus = null;\n\t}\n\n\t/** Reset Ollama status cache (e.g. after detecting it became available) */\n\tresetOllamaCache(): void {\n\t\tthis.ollamaStatus = null;\n\t}\n\n\t/** Update the hidden flag in persisted storage */\n\tsetHidden(hidden: boolean): void {\n\t\tconst stored = loadStored();\n\t\tif (stored) {\n\t\t\tstored.hidden = hidden;\n\t\t\tsaveStored(stored);\n\t\t}\n\t\t// Keep in-memory state in sync so reset() reads current hidden flag\n\t\tif (this.state) {\n\t\t\tthis.state.hidden = hidden;\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"buddy-manager.js","sourceRoot":"","sources":["../../../src/core/buddy/buddy-manager.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AACxE,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAC9B,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CA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AC;QAC5B,CAAC;IAAA,CACD;CACD","sourcesContent":["/**\n * BuddyManager — Core state machine for the buddy companion.\n *\n * Handles: bone rolling, soul generation, persistence, Ollama availability checks.\n * Bones are deterministic from hash(username + hostname + salt + rerollCount).\n * Soul is LLM-generated once on first hatch and persisted to buddy.json.\n */\n\nimport type { Context, Model } from \"@dreb/ai\";\nimport { completeSimple } from \"@dreb/ai\";\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"fs\";\nimport { hostname } from \"os\";\nimport { join } from \"path\";\nimport { getAgentDir } from \"../../config.js\";\nimport { createBuddyRng } from \"./buddy-prng.js\";\nimport { rollEyes, rollHat, rollSpecies, rollStats } from \"./buddy-species.js\";\nimport type { BuddyState, CompanionBones, StoredCompanion } from \"./buddy-types.js\";\nimport { STAT_NAMES } from \"./buddy-types.js\";\n\nconst BUDDY_SALT = \"dreb-buddy-v1\";\nconst BUDDY_FILENAME = \"buddy.json\";\nconst DEFAULT_BACKSTORY = \"A mysterious past shrouded in legend.\";\n\n/** Base Ollama model config — id/name are set dynamically from available models */\nconst OLLAMA_MODEL_BASE: Omit<Model<\"openai-completions\">, \"id\" | \"name\"> = {\n\tapi: \"openai-completions\",\n\tprovider: \"ollama\",\n\tbaseUrl: \"http://localhost:11434/v1\",\n\treasoning: false,\n\tinput: [\"text\"],\n\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },\n\tcontextWindow: 128000,\n\tmaxTokens: 2048,\n\tcompat: {\n\t\tsupportsDeveloperRole: false,\n\t\tsupportsReasoningEffort: false,\n\t},\n};\n\n/** Max words for buddy response before truncation */\nconst MAX_RESPONSE_WORDS = 60;\n\n/** Prompt for soul generation (uses parent LLM, not Ollama) */\nconst SOUL_GENERATION_PROMPT = `You are generating a companion character for a coding assistant terminal app. Based on the species, rarity, and stats below, generate a creative name, a one-sentence personality description, and a funny fictional backstory.\n\nSpecies: {species}\nRarity: {rarity}\nStats: {stats}\nShiny: {shiny}\n\nThe name must NOT be a common English word, programming keyword, tool name, or command. It should be unique and distinctive — a proper noun that won't appear in normal conversation. The name must be 4-8 characters and easy to type on a QWERTY keyboard — use only common letters (a-z, avoid q, x, z, j). Do not use species name as the name.\n\nRespond in EXACTLY this format:\nNAME: <name>\nPERSONALITY: <one sentence personality>\nBACKSTORY: <2-3 sentence elaborate fictional backstory — funny, absurd, or dramatic. Include specific events, places, former occupations>`;\n\n/** Prompt for buddy reactions via Ollama */\nconst REACTION_PROMPT = `You are {name}, a {species} companion in a terminal coding app. You are {personality}. Your backstory: {backstory}\n\nSomething just happened. React with a short, in-character quip based on the context below. Be specific — reference what actually happened, not just that something happened. Max 20 words. No quotes, no prefixes, just the quip.\n\nContext:\n{event}`;\n\nconst NAME_CALL_PROMPT = `You are {name}, a {species} companion in a terminal coding app. You are {personality}. Your backstory: {backstory}\n\nThe user just said: \"{message}\"\nRecent context: {context}\n\nRespond to what the user said directly. Be in-character, reference your backstory occasionally. Max 30 words. No quotes, no prefixes, just your response.`;\n\n// =============================================================================\n// Ollama availability\n// =============================================================================\n\nexport interface OllamaStatus {\n\tavailable: boolean;\n\tmodels: string[];\n\terror?: string;\n}\n\n/**\n * Check if Ollama is running and has models available.\n * Uses the /api/tags endpoint.\n */\nexport async function checkOllama(): Promise<OllamaStatus> {\n\ttry {\n\t\tconst res = await fetch(\"http://localhost:11434/api/tags\", { signal: AbortSignal.timeout(3000) });\n\t\tif (!res.ok) {\n\t\t\treturn { available: false, models: [], error: `Ollama returned ${res.status}` };\n\t\t}\n\t\tconst data = (await res.json()) as { models?: { name: string }[] };\n\t\tconst models = (data.models ?? []).map((m) => m.name);\n\t\tif (models.length === 0) {\n\t\t\treturn { available: false, models: [], error: \"No models installed. Run: ollama pull llama3.2\" };\n\t\t}\n\t\treturn { available: true, models };\n\t} catch {\n\t\treturn { available: false, models: [], error: \"Ollama is not running. Start it with: ollama serve\" };\n\t}\n}\n\n/**\n * Pick the Ollama model for the buddy.\n * Returns the stored model name if it's available, otherwise null.\n */\nfunction pickOllamaModel(storedModel: string | undefined, availableModels: string[]): string | null {\n\tif (!storedModel) return null;\n\t// Check if the stored model is installed (exact match or prefix match without tag)\n\tconst match = availableModels.find((m) => m === storedModel || m.startsWith(`${storedModel}:`));\n\treturn match ?? null;\n}\n\n/**\n * Truncate response to a maximum word count, appending \"...[truncated]\" if exceeded.\n */\nexport function truncateResponse(text: string, maxWords: number): string {\n\tconst words = text.split(/\\s+/);\n\tif (words.length <= maxWords) return text;\n\treturn `${words.slice(0, maxWords).join(\" \")} ...[truncated]`;\n}\n\n// =============================================================================\n// Persistence\n// =============================================================================\n\nfunction getBuddyPath(): string {\n\treturn join(getAgentDir(), BUDDY_FILENAME);\n}\n\nfunction loadStored(): StoredCompanion | null {\n\tconst path = getBuddyPath();\n\tif (!existsSync(path)) return null;\n\ttry {\n\t\tconst data = JSON.parse(readFileSync(path, \"utf-8\"));\n\t\t// Validate required fields\n\t\tif (\n\t\t\ttypeof data.rerollCount === \"number\" &&\n\t\t\ttypeof data.name === \"string\" &&\n\t\t\ttypeof data.personality === \"string\"\n\t\t) {\n\t\t\treturn {\n\t\t\t\trerollCount: data.rerollCount,\n\t\t\t\tname: data.name,\n\t\t\t\tpersonality: data.personality,\n\t\t\t\tbackstory: typeof data.backstory === \"string\" ? data.backstory : DEFAULT_BACKSTORY,\n\t\t\t\thatchedAt: data.hatchedAt ?? new Date().toISOString(),\n\t\t\t\t...(data.hidden !== undefined ? { hidden: data.hidden } : {}),\n\t\t\t\t...(typeof data.ollamaModel === \"string\" ? { ollamaModel: data.ollamaModel } : {}),\n\t\t\t};\n\t\t}\n\t\treturn null;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nfunction saveStored(stored: StoredCompanion): void {\n\tconst path = getBuddyPath();\n\tconst dir = join(path, \"..\");\n\tif (!existsSync(dir)) {\n\t\tmkdirSync(dir, { recursive: true });\n\t}\n\twriteFileSync(path, JSON.stringify(stored, null, 2));\n}\n\n// =============================================================================\n// Bone rolling\n// =============================================================================\n\nfunction rollBones(rerollCount: number): CompanionBones {\n\tconst username = process.env.USER ?? process.env.LOGNAME ?? \"user\";\n\tconst host = hostname();\n\tconst rng = createBuddyRng(username, host, BUDDY_SALT, rerollCount);\n\n\t// Roll species + rarity\n\tconst { species, rarity } = rollSpecies(rng);\n\n\t// Roll shiny (1% chance)\n\tconst shiny = rng() < 0.01;\n\n\t// Roll eyes and hat\n\tconst eyes = rollEyes(rng);\n\tconst hat = rollHat(rng);\n\n\t// Roll stats\n\tconst stats = rollStats(rng, rarity);\n\n\treturn { species, rarity, shiny, stats, eyeStyle: eyes, hat };\n}\n\n// =============================================================================\n// Soul generation\n// =============================================================================\n\n/**\n * Generate a soul (name + personality + backstory) using the parent LLM.\n * Only called on first hatch or reroll.\n */\nasync function generateSoul(\n\tbones: CompanionBones,\n\tparentModel: Model<\"openai-completions\">,\n\tapiKey: string,\n): Promise<{ name: string; personality: string; backstory: string }> {\n\tconst statsStr = STAT_NAMES.map((s) => `${s}: ${bones.stats[s]}`).join(\", \");\n\tconst prompt = SOUL_GENERATION_PROMPT.replace(\"{species}\", bones.species)\n\t\t.replace(\"{rarity}\", bones.rarity)\n\t\t.replace(\"{stats}\", statsStr)\n\t\t.replace(\"{shiny}\", bones.shiny ? \"YES ✨\" : \"no\");\n\n\tconst context: Context = {\n\t\tsystemPrompt: \"Generate a companion character. Respond in the exact format requested.\",\n\t\tmessages: [{ role: \"user\", content: prompt, timestamp: Date.now() }],\n\t};\n\n\ttry {\n\t\tconst response = await completeSimple(parentModel, context, { apiKey });\n\t\tconst text = response.content\n\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t.map((c) => c.text)\n\t\t\t.join(\"\");\n\n\t\t// Parse NAME: ... and PERSONALITY: ... and BACKSTORY: ...\n\t\tconst nameMatch = text.match(/NAME:\\s*(.+)/i);\n\t\tconst personalityMatch = text.match(/PERSONALITY:\\s*(.+)/i);\n\t\tconst backstoryMatch = text.match(/BACKSTORY:\\s*([\\s\\S]+)/i);\n\n\t\tlet name = nameMatch?.[1]?.trim() ?? bones.species;\n\t\tconst personality = personalityMatch?.[1]?.trim() ?? `A ${bones.rarity} ${bones.species} companion.`;\n\t\tconst backstory = backstoryMatch?.[1]?.trim() ?? DEFAULT_BACKSTORY;\n\n\t\t// Enforce name length\n\t\tif (name.length > 8) name = name.slice(0, 8);\n\n\t\treturn { name, personality, backstory };\n\t} catch {\n\t\t// Fallback if LLM fails\n\t\treturn {\n\t\t\tname: bones.species,\n\t\t\tpersonality: `A ${bones.rarity} ${bones.species} companion.`,\n\t\t\tbackstory: DEFAULT_BACKSTORY,\n\t\t};\n\t}\n}\n\n// =============================================================================\n// BuddyManager\n// =============================================================================\n\nexport class BuddyManager {\n\tprivate state: BuddyState | null = null;\n\tprivate ollamaStatus: OllamaStatus | null = null;\n\n\t/** Get current buddy state (null if not loaded) */\n\tgetState(): BuddyState | null {\n\t\treturn this.state;\n\t}\n\n\t/** Check if buddy exists on disk */\n\thasStoredBuddy(): boolean {\n\t\treturn loadStored() !== null;\n\t}\n\n\t/**\n\t * Load or create buddy state.\n\t * If stored buddy exists, loads soul and re-rolls bones.\n\t * If no stored buddy, returns null (need to hatch first).\n\t */\n\tload(): BuddyState | null {\n\t\tconst stored = loadStored();\n\t\tif (!stored) return null;\n\n\t\tconst bones = rollBones(stored.rerollCount);\n\t\tthis.state = { ...bones, ...stored };\n\t\treturn this.state;\n\t}\n\n\t/**\n\t * Hatch a new buddy. Generates bones, then uses parent LLM for soul.\n\t * Returns the new state.\n\t */\n\tasync hatch(parentModel: Model<\"openai-completions\">, apiKey: string): Promise<BuddyState> {\n\t\tconst stored = loadStored();\n\t\tconst rerollCount = stored?.rerollCount ?? 0;\n\n\t\tconst bones = rollBones(rerollCount);\n\t\tconst { name, personality, backstory } = await generateSoul(bones, parentModel, apiKey);\n\n\t\tconst newStored: StoredCompanion = {\n\t\t\trerollCount,\n\t\t\tname,\n\t\t\tpersonality,\n\t\t\tbackstory,\n\t\t\thatchedAt: new Date().toISOString(),\n\t\t\t...(stored?.ollamaModel ? { ollamaModel: stored.ollamaModel } : {}),\n\t\t};\n\n\t\tsaveStored(newStored);\n\t\tthis.state = { ...bones, ...newStored };\n\t\treturn this.state;\n\t}\n\n\t/**\n\t * Reroll the buddy — new bones + new soul.\n\t */\n\tasync reroll(parentModel: Model<\"openai-completions\">, apiKey: string): Promise<BuddyState> {\n\t\tconst stored = loadStored();\n\t\tconst newRerollCount = (stored?.rerollCount ?? 0) + 1;\n\n\t\tconst bones = rollBones(newRerollCount);\n\t\tconst { name, personality, backstory } = await generateSoul(bones, parentModel, apiKey);\n\n\t\tconst newStored: StoredCompanion = {\n\t\t\trerollCount: newRerollCount,\n\t\t\tname,\n\t\t\tpersonality,\n\t\t\tbackstory,\n\t\t\thatchedAt: new Date().toISOString(),\n\t\t\t...(stored?.ollamaModel ? { ollamaModel: stored.ollamaModel } : {}),\n\t\t};\n\n\t\tsaveStored(newStored);\n\t\tthis.state = { ...bones, ...newStored };\n\t\treturn this.state;\n\t}\n\n\t/** Get buddy's name (for name-call detection) */\n\tgetName(): string | null {\n\t\treturn this.state?.name ?? loadStored()?.name ?? null;\n\t}\n\n\t/**\n\t * Shared Ollama chat helper. Checks availability, picks model, runs completion.\n\t * Returns the response text, or null if Ollama is unavailable or no model configured.\n\t */\n\tprivate async ollamaChat(context: Context): Promise<string | null> {\n\t\t// Check Ollama lazily, retry if previously unavailable\n\t\tif (!this.ollamaStatus || !this.ollamaStatus.available) {\n\t\t\tthis.ollamaStatus = await checkOllama();\n\t\t}\n\t\tif (!this.ollamaStatus.available) return null;\n\n\t\tconst modelName = pickOllamaModel(this.state?.ollamaModel, this.ollamaStatus.models);\n\t\tif (!modelName) return null;\n\t\tconst model: Model<\"openai-completions\"> = {\n\t\t\t...OLLAMA_MODEL_BASE,\n\t\t\tid: modelName,\n\t\t\tname: `${modelName} (Ollama)`,\n\t\t};\n\n\t\tlet response: import(\"@dreb/ai\").AssistantMessage;\n\t\ttry {\n\t\t\tresponse = await completeSimple(model, context, {\n\t\t\t\tapiKey: \"ollama\",\n\t\t\t\tsignal: AbortSignal.timeout(120000),\n\t\t\t});\n\t\t} catch {\n\t\t\t// Safety net for unexpected sync errors (e.g. provider not found).\n\t\t\t// Normal runtime errors (timeout, connection) are handled via stopReason below.\n\t\t\tthis.ollamaStatus = null;\n\t\t\treturn null;\n\t\t}\n\n\t\t// Connection error — invalidate cache so next attempt re-checks Ollama\n\t\tif (response.stopReason === \"error\") {\n\t\t\tthis.ollamaStatus = null;\n\t\t\treturn null;\n\t\t}\n\n\t\t// Timeout or abort — preserve cache (model is just slow, Ollama is fine)\n\t\tif (response.stopReason === \"aborted\") {\n\t\t\treturn null;\n\t\t}\n\n\t\tlet text = response.content\n\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t.map((c) => c.text)\n\t\t\t.join(\"\")\n\t\t\t.trim();\n\n\t\t// Truncate overly long responses\n\t\ttext = truncateResponse(text, MAX_RESPONSE_WORDS);\n\n\t\treturn text || null;\n\t}\n\n\t/**\n\t * Generate a reaction to an event using Ollama.\n\t * Returns null if Ollama is unavailable.\n\t */\n\tasync react(event: string): Promise<string | null> {\n\t\tif (!this.state) return null;\n\n\t\tconst prompt = REACTION_PROMPT.replace(\"{name}\", this.state.name)\n\t\t\t.replace(\"{species}\", this.state.species)\n\t\t\t.replace(\"{personality}\", this.state.personality)\n\t\t\t.replace(\"{backstory}\", this.state.backstory)\n\t\t\t.replace(\"{event}\", event);\n\n\t\tconst context: Context = {\n\t\t\tsystemPrompt: \"Respond with a short in-character quip. Max 20 words.\",\n\t\t\tmessages: [{ role: \"user\", content: prompt, timestamp: Date.now() }],\n\t\t};\n\n\t\treturn this.ollamaChat(context);\n\t}\n\n\t/**\n\t * Respond to the user calling the buddy's name.\n\t * Uses Ollama for the response.\n\t */\n\tasync respondToNameCall(userMessage: string, recentContext: string): Promise<string | null> {\n\t\tif (!this.state) return null;\n\n\t\tconst prompt = NAME_CALL_PROMPT.replace(\"{name}\", this.state.name)\n\t\t\t.replace(\"{species}\", this.state.species)\n\t\t\t.replace(\"{personality}\", this.state.personality)\n\t\t\t.replace(\"{backstory}\", this.state.backstory)\n\t\t\t.replace(\"{message}\", userMessage)\n\t\t\t.replace(\"{context}\", recentContext);\n\n\t\tconst context: Context = {\n\t\t\tsystemPrompt: \"Respond with a short friendly greeting. Max 30 words.\",\n\t\t\tmessages: [{ role: \"user\", content: prompt, timestamp: Date.now() }],\n\t\t};\n\n\t\treturn this.ollamaChat(context);\n\t}\n\n\t/** Get the configured Ollama model name, or null if not set */\n\tgetOllamaModel(): string | null {\n\t\treturn this.state?.ollamaModel ?? loadStored()?.ollamaModel ?? null;\n\t}\n\n\t/** Set the Ollama model for buddy reactions. Persists to disk. */\n\tsetOllamaModel(modelName: string): void {\n\t\tconst stored = loadStored();\n\t\tif (stored) {\n\t\t\tstored.ollamaModel = modelName;\n\t\t\tsaveStored(stored);\n\t\t}\n\t\tif (this.state) {\n\t\t\tthis.state.ollamaModel = modelName;\n\t\t}\n\t\t// Invalidate Ollama status cache so next call picks up the new model\n\t\tthis.ollamaStatus = null;\n\t}\n\n\t/** Reset Ollama status cache (e.g. after detecting it became available) */\n\tresetOllamaCache(): void {\n\t\tthis.ollamaStatus = null;\n\t}\n\n\t/** Update the hidden flag in persisted storage */\n\tsetHidden(hidden: boolean): void {\n\t\tconst stored = loadStored();\n\t\tif (stored) {\n\t\t\tstored.hidden = hidden;\n\t\t\tsaveStored(stored);\n\t\t}\n\t\t// Keep in-memory state in sync so reset() reads current hidden flag\n\t\tif (this.state) {\n\t\t\tthis.state.hidden = hidden;\n\t\t}\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"buddy-component.d.ts","sourceRoot":"","sources":["../../../../src/modes/interactive/components/buddy-component.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAuB,GAAG,EAAE,MAAM,WAAW,CAAC;AA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AAa;IAOrB,OAAO,CAAC,WAAW;IAQnB,OAAO,CAAC,OAAO;IAQf,OAAO,CAAC,WAAW;IAenB,OAAO,CAAC,kBAAkB;IAwC1B,6EAA6E;IAC7E,OAAO,CAAC,oBAAoB;IAY5B,qEAAqE;IACrE,OAAO,CAAC,kBAAkB;CA0B1B","sourcesContent":["/**\n * BuddyComponent — TUI component rendering the buddy companion.\n *\n * Renders below the editor with:\n * - 3-frame idle animation cycling at 500ms\n * - Speech bubbles for reactions and name-call responses\n * - Pet hearts animation (2.5s)\n * - Narrow terminal fallback (<100 cols)\n * - Stat display and rarity badge\n */\n\nimport type { Component, MarkdownTheme as MT, TUI } from \"@dreb/tui\";\nimport { truncateToWidth, visibleWidth } from \"@dreb/tui\";\nimport { marked } from \"marked\";\nimport { applyEyes, getSpeciesFrames, getSpeciesWidth } from \"../../../core/buddy/buddy-species.js\";\nimport type { BuddyState } from \"../../../core/buddy/buddy-types.js\";\nimport { Rarity, Stat } from \"../../../core/buddy/buddy-types.js\";\nimport { getMarkdownTheme, theme } from \"../theme/theme.js\";\n\nconst IDLE_INTERVAL_MS = 500;\nconst SPEECH_BUBBLE_DURATION_MS = 10000;\nconst PET_DURATION_MS = 2500;\nconst HEART_CHARS = [\"❤️\", \"💕\", \"💖\", \"💗\", \"✨\"];\nconst NARROW_THRESHOLD = 100;\n\nexport class BuddyComponent implements Component {\n\tprivate ui: TUI;\n\tprivate state: BuddyState;\n\tprivate interval: ReturnType<typeof setInterval> | null = null;\n\n\t// Animation state\n\tprivate currentFrame = 0;\n\tprivate totalFrames = 3;\n\n\t// Speech bubble\n\tprivate speechText: string | null = null;\n\tprivate speechTimeout: ReturnType<typeof setTimeout> | null = null;\n\n\t// Pet animation\n\tprivate isPetting = false;\n\tprivate petTimeout: ReturnType<typeof setTimeout> | null = null;\n\tprivate hearts: Array<{ x: number; y: number; char: string; life: number }> = [];\n\n\t// Thinking indicator\n\tprivate thinkingLabel: string | null = null;\n\tprivate thinkingDots = 0;\n\tprivate static readonly THINKING_DOT_COUNT = 4; // cycles 0,1,2,3 → \".\", \"..\", \"...\", \"....\"\n\n\t// Cached render\n\tprivate cachedLines: string[] = [];\n\tprivate cachedWidth = 0;\n\tprivate cachedVersion = -1;\n\tprivate renderVersion = 0;\n\n\tconstructor(ui: TUI, state: BuddyState) {\n\t\tthis.ui = ui;\n\t\tthis.state = state;\n\t\tthis.totalFrames = getSpeciesFrames(state.species).length;\n\t\tthis.startAnimation();\n\t}\n\n\t/** Update buddy state (e.g. after reroll) */\n\tupdateState(state: BuddyState): void {\n\t\tthis.state = state;\n\t\tthis.totalFrames = getSpeciesFrames(state.species).length;\n\t\tthis.currentFrame = 0;\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Show a speech bubble with text */\n\tshowSpeech(text: string): void {\n\t\tthis.speechText = text;\n\t\tif (this.speechTimeout) clearTimeout(this.speechTimeout);\n\t\tthis.speechTimeout = setTimeout(() => {\n\t\t\tthis.speechText = null;\n\t\t\tthis.speechTimeout = null;\n\t\t\tthis.bumpVersion();\n\t\t\tthis.ui.requestRender();\n\t\t}, SPEECH_BUBBLE_DURATION_MS);\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Trigger pet animation */\n\tpet(): void {\n\t\tthis.isPetting = true;\n\t\t// Spawn hearts\n\t\tconst spriteWidth = getSpeciesWidth(this.state.species);\n\t\tfor (let i = 0; i < 5; i++) {\n\t\t\tthis.hearts.push({\n\t\t\t\tx: Math.floor(Math.random() * spriteWidth),\n\t\t\t\ty: -1 - Math.floor(Math.random() * 3),\n\t\t\t\tchar: HEART_CHARS[Math.floor(Math.random() * HEART_CHARS.length)],\n\t\t\t\tlife: 5 + Math.floor(Math.random() * 5),\n\t\t\t});\n\t\t}\n\t\tif (this.petTimeout) clearTimeout(this.petTimeout);\n\t\tthis.petTimeout = setTimeout(() => {\n\t\t\tthis.isPetting = false;\n\t\t\tthis.hearts = [];\n\t\t\tthis.petTimeout = null;\n\t\t\tthis.bumpVersion();\n\t\t\tthis.ui.requestRender();\n\t\t}, PET_DURATION_MS);\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Show a pulsing thinking indicator with optional label */\n\tshowThinking(label?: string): void {\n\t\tthis.thinkingLabel = label ?? \"thinking\";\n\t\tthis.thinkingDots = 0;\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Hide the thinking indicator */\n\thideThinking(): void {\n\t\tthis.thinkingLabel = null;\n\t\tthis.thinkingDots = 0;\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\tinvalidate(): void {\n\t\tthis.cachedWidth = 0;\n\t}\n\n\trender(width: number): string[] {\n\t\tif (width === this.cachedWidth && this.cachedVersion === this.renderVersion) {\n\t\t\treturn this.cachedLines;\n\t\t}\n\n\t\tif (width < NARROW_THRESHOLD) {\n\t\t\tthis.cachedLines = this.renderNarrow(width);\n\t\t} else {\n\t\t\tthis.cachedLines = this.renderFull(width);\n\t\t}\n\n\t\tthis.cachedWidth = width;\n\t\tthis.cachedVersion = this.renderVersion;\n\t\treturn this.cachedLines;\n\t}\n\n\tdispose(): void {\n\t\tthis.stopAnimation();\n\t\tthis.thinkingLabel = null;\n\t\tif (this.speechTimeout) {\n\t\t\tclearTimeout(this.speechTimeout);\n\t\t\tthis.speechTimeout = null;\n\t\t}\n\t\tif (this.petTimeout) {\n\t\t\tclearTimeout(this.petTimeout);\n\t\t\tthis.petTimeout = null;\n\t\t}\n\t}\n\n\t// =============================================================================\n\t// Full rendering (wide terminal)\n\t// =============================================================================\n\n\tprivate renderFull(width: number): string[] {\n\t\tconst lines: string[] = [];\n\t\tconst frames = getSpeciesFrames(this.state.species);\n\t\tconst frame = frames[this.currentFrame % this.totalFrames];\n\t\tconst rendered = applyEyes(frame, this.state.eyeStyle);\n\n\t\t// Hat line (if present)\n\t\tif (this.state.hat) {\n\t\t\tconst hatPad = Math.max(0, Math.floor((getSpeciesWidth(this.state.species) - 1) / 2) - 1);\n\t\t\tlines.push(\" \".repeat(hatPad) + this.state.hat);\n\t\t}\n\n\t\t// Sprite lines\n\t\tconst spriteWidth = getSpeciesWidth(this.state.species);\n\t\tlines.push(...rendered);\n\n\t\t// Heart animation line above sprite\n\t\tif (this.isPetting && this.hearts.length > 0) {\n\t\t\tconst heartLine = \" \".repeat(spriteWidth);\n\t\t\tconst chars = heartLine.split(\"\");\n\t\t\tfor (const heart of this.hearts) {\n\t\t\t\tconst x = Math.min(heart.x, chars.length - 2);\n\t\t\t\tif (x >= 0 && heart.life > 0) {\n\t\t\t\t\tconst hChar = heart.char;\n\t\t\t\t\tchars.splice(x, hChar.length, ...hChar.split(\"\"));\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Insert heart line before sprite\n\t\t\tlines.splice(this.state.hat ? 1 : 0, 0, chars.join(\"\"));\n\t\t}\n\n\t\t// Name + rarity line\n\t\tconst shinyMark = this.state.shiny ? \" ✨\" : \"\";\n\t\tconst rarityColor = this.rarityColor(this.state.rarity);\n\t\tconst nameLine = ` ${theme.bold(this.state.name)}${shinyMark} ${theme.fg(rarityColor, `[${this.state.rarity}]`)} ${theme.fg(\"muted\", this.state.species)}`;\n\t\tlines.push(nameLine);\n\n\t\t// Stats line\n\t\tconst statParts = (Object.values(Stat) as Stat[]).map((s) => {\n\t\t\tconst val = this.state.stats[s];\n\t\t\tconst bar = this.statBar(val);\n\t\t\treturn `${theme.fg(\"muted\", s[0])}:${bar}`;\n\t\t});\n\t\tlines.push(` ${statParts.join(\" \")}`);\n\n\t\t// Thinking indicator\n\t\tif (this.thinkingLabel !== null) {\n\t\t\tconst dots = \".\".repeat((this.thinkingDots % BuddyComponent.THINKING_DOT_COUNT) + 1);\n\t\t\tconst label = this.thinkingLabel || \"thinking\";\n\t\t\tlines.push(` ${theme.fg(\"muted\", `💭 ${label}${dots}`)}`);\n\t\t}\n\n\t\t// Speech bubble (beside or below sprite)\n\t\tif (this.speechText) {\n\t\t\tconst bubbleLines = this.formatSpeechBubble(this.speechText, Math.min(width - 4, 60));\n\t\t\tlines.push(...bubbleLines);\n\t\t}\n\n\t\treturn lines;\n\t}\n\n\t// =============================================================================\n\t// Narrow rendering (< 100 cols)\n\t// =============================================================================\n\n\tprivate renderNarrow(width: number): string[] {\n\t\tconst lines: string[] = [];\n\n\t\t// Single-line face\n\t\tconst eyes = this.state.eyeStyle;\n\t\tconst mouth = this.isPetting ? \"♥\" : \">\";\n\t\tconst face = `${this.state.hat}${eyes}${mouth}${eyes}`;\n\n\t\t// Name + truncated quip\n\t\tconst shinyMark = this.state.shiny ? \"✨\" : \"\";\n\t\tconst name = `${theme.bold(this.state.name)}${shinyMark}`;\n\n\t\tif (this.speechText) {\n\t\t\tconst maxQuip = Math.max(10, width - face.length - this.state.name.length - 6);\n\t\t\tconst styledQuip = this.renderInlineMarkdown(this.speechText);\n\t\t\tconst quip = visibleWidth(styledQuip) > maxQuip ? `${truncateToWidth(styledQuip, maxQuip - 1)}…` : styledQuip;\n\t\t\tlines.push(` ${face} ${name}: ${theme.fg(\"accent\", quip)}`);\n\t\t} else if (this.thinkingLabel !== null) {\n\t\t\tconst dots = \".\".repeat((this.thinkingDots % BuddyComponent.THINKING_DOT_COUNT) + 1);\n\t\t\tconst label = this.thinkingLabel || \"thinking\";\n\t\t\tlines.push(` ${face} ${name} ${theme.fg(\"muted\", `💭 ${label}${dots}`)}`);\n\t\t} else {\n\t\t\tlines.push(` ${face} ${name}`);\n\t\t}\n\n\t\treturn lines;\n\t}\n\n\t// =============================================================================\n\t// Animation\n\t// =============================================================================\n\n\tprivate startAnimation(): void {\n\t\tthis.interval = setInterval(() => {\n\t\t\tthis.currentFrame = (this.currentFrame + 1) % this.totalFrames;\n\n\t\t\t// Tick thinking dots\n\t\t\tif (this.thinkingLabel !== null) {\n\t\t\t\tthis.thinkingDots = (this.thinkingDots + 1) % BuddyComponent.THINKING_DOT_COUNT;\n\t\t\t}\n\n\t\t\t// Tick hearts\n\t\t\tif (this.isPetting) {\n\t\t\t\tfor (const heart of this.hearts) {\n\t\t\t\t\theart.life--;\n\t\t\t\t\theart.y++;\n\t\t\t\t}\n\t\t\t\tthis.hearts = this.hearts.filter((h) => h.life > 0);\n\t\t\t}\n\n\t\t\tthis.bumpVersion();\n\t\t\tthis.ui.requestRender();\n\t\t}, IDLE_INTERVAL_MS);\n\t}\n\n\tprivate stopAnimation(): void {\n\t\tif (this.interval) {\n\t\t\tclearInterval(this.interval);\n\t\t\tthis.interval = null;\n\t\t}\n\t}\n\n\tprivate bumpVersion(): void {\n\t\tthis.renderVersion++;\n\t}\n\n\t// =============================================================================\n\t// Formatting helpers\n\t// =============================================================================\n\n\tprivate statBar(value: number): string {\n\t\tconst filled = Math.round(value / 10);\n\t\tconst empty = 10 - filled;\n\t\tconst bar = \"█\".repeat(filled) + \"░\".repeat(empty);\n\t\tconst color: \"success\" | \"warning\" | \"error\" = value >= 70 ? \"success\" : value >= 40 ? \"warning\" : \"error\";\n\t\treturn theme.fg(color, bar);\n\t}\n\n\tprivate rarityColor(rarity: Rarity): \"muted\" | \"success\" | \"accent\" | \"warning\" | \"error\" {\n\t\tswitch (rarity) {\n\t\t\tcase Rarity.COMMON:\n\t\t\t\treturn \"muted\";\n\t\t\tcase Rarity.UNCOMMON:\n\t\t\t\treturn \"success\";\n\t\t\tcase Rarity.RARE:\n\t\t\t\treturn \"accent\";\n\t\t\tcase Rarity.EPIC:\n\t\t\t\treturn \"warning\";\n\t\t\tcase Rarity.LEGENDARY:\n\t\t\t\treturn \"error\";\n\t\t}\n\t}\n\n\tprivate formatSpeechBubble(text: string, maxWidth: number): string[] {\n\t\tif (!text.trim()) return [];\n\n\t\t// Render inline markdown (bold, italic, code) to styled text with ANSI codes\n\t\tconst styledText = this.renderInlineMarkdown(text);\n\n\t\t// Word-wrap using visible width (ANSI-aware)\n\t\tconst lines: string[] = [];\n\t\tconst words = styledText.split(\" \");\n\t\tlet currentLine = \"\";\n\n\t\tfor (const word of words) {\n\t\t\tconst test = currentLine ? `${currentLine} ${word}` : word;\n\t\t\tif (visibleWidth(test) > maxWidth) {\n\t\t\t\tif (currentLine) lines.push(currentLine);\n\t\t\t\tcurrentLine = word;\n\t\t\t} else {\n\t\t\t\tcurrentLine = test;\n\t\t\t}\n\t\t}\n\t\tif (currentLine) lines.push(currentLine);\n\n\t\t// Wrap in bubble border using visible width for measurement\n\t\tconst maxLineWidth = Math.max(...lines.map((l) => visibleWidth(l)));\n\t\tconst bubbleWidth = Math.max(6, Math.min(maxLineWidth + 4, maxWidth + 4));\n\t\tconst top = `╭${\"─\".repeat(bubbleWidth - 2)}╮`;\n\t\tconst bottom = `╰${\"─\".repeat(bubbleWidth - 2)}╯`;\n\n\t\tconst result: string[] = [];\n\t\tresult.push(` ${theme.fg(\"accent\", top)}`);\n\t\tfor (const line of lines) {\n\t\t\tconst padding = Math.max(0, bubbleWidth - 4 - visibleWidth(line));\n\t\t\tconst padded = line + \" \".repeat(padding);\n\t\t\tresult.push(` ${theme.fg(\"accent\", \"│\")} ${padded} ${theme.fg(\"accent\", \"│\")}`);\n\t\t}\n\t\tresult.push(` ${theme.fg(\"accent\", bottom)}`);\n\n\t\treturn result;\n\t}\n\n\t/** Render inline markdown tokens (bold, italic, code) to ANSI-styled text */\n\tprivate renderInlineMarkdown(text: string): string {\n\t\tconst mdTheme = getMarkdownTheme();\n\t\tconst tokens = marked.lexer(text);\n\n\t\t// Flatten: we expect a paragraph containing inline tokens\n\t\tconst inlineTokens = tokens.flatMap((t: any) =>\n\t\t\tt.type === \"paragraph\" ? (t.tokens ?? []) : t.type === \"text\" ? t : [],\n\t\t);\n\n\t\treturn this.renderInlineTokens(inlineTokens, mdTheme);\n\t}\n\n\t/** Recursively render marked inline tokens to ANSI-styled strings */\n\tprivate renderInlineTokens(tokens: any[], mdTheme: MT): string {\n\t\tlet result = \"\";\n\t\tfor (const token of tokens) {\n\t\t\tswitch (token.type) {\n\t\t\t\tcase \"text\":\n\t\t\t\t\tresult += token.text ?? this.renderInlineTokens(token.tokens ?? [], mdTheme);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"strong\":\n\t\t\t\t\tresult += mdTheme.bold(this.renderInlineTokens(token.tokens ?? [], mdTheme));\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"em\":\n\t\t\t\t\tresult += mdTheme.italic(this.renderInlineTokens(token.tokens ?? [], mdTheme));\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"codespan\":\n\t\t\t\t\tresult += mdTheme.code(token.text);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"escape\":\n\t\t\t\t\tresult += token.text;\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\tresult += token.text ?? token.raw ?? \"\";\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n}\n"]}
1
+ {"version":3,"file":"buddy-component.d.ts","sourceRoot":"","sources":["../../../../src/modes/interactive/components/buddy-component.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAuB,GAAG,EAAE,MAAM,WAAW,CAAC;AAIrE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oCAAoC,CAAC;AAYrE,qBAAa,cAAe,YAAW,SAAS;IAC/C,OAAO,CAAC,EAAE,CAAM;IAChB,OAAO,CAAC,KAAK,CAAa;IAC1B,OAAO,CAAC,QAAQ,CAA+C;IAG/D,OAAO,CAAC,YAAY,CAAK;IACzB,OAAO,CAAC,WAAW,CAAK;IAGxB,OAAO,CAAC,UAAU,CAAuB;IACzC,OAAO,CAAC,aAAa,CAA8C;IAGnE,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,UAAU,CAA8C;IAChE,OAAO,CAAC,MAAM,CAAmE;IAGjF,OAAO,CAAC,aAAa,CAAuB;IAC5C,OAAO,CAAC,YAAY,CAAK;IACzB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,CAAK;IAG/C,OAAO,CAAC,WAAW,CAAgB;IACnC,OAAO,CAAC,WAAW,CAAK;IACxB,OAAO,CAAC,aAAa,CAAM;IAC3B,OAAO,CAAC,aAAa,CAAK;IAE1B,YAAY,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,UAAU,EAKrC;IAED,6CAA6C;IAC7C,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAMnC;IAED,qCAAqC;IACrC,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAW7B;IAED,4BAA4B;IAC5B,GAAG,IAAI,IAAI,CAsBV;IAED,4DAA4D;IAC5D,YAAY,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAKjC;IAED,kCAAkC;IAClC,YAAY,IAAI,IAAI,CAKnB;IAED,UAAU,IAAI,IAAI,CAEjB;IAED,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAc9B;IAED,OAAO,IAAI,IAAI,CAWd;IAMD,OAAO,CAAC,UAAU;IA0ElB,OAAO,CAAC,YAAY;IAgCpB,OAAO,CAAC,cAAc;IAuBtB,OAAO,CAAC,aAAa;IAOrB,OAAO,CAAC,WAAW;IAQnB,OAAO,CAAC,OAAO;IAQf,OAAO,CAAC,WAAW;IAenB,OAAO,CAAC,kBAAkB;IAsC1B,6EAA6E;IAC7E,OAAO,CAAC,oBAAoB;IAY5B,qEAAqE;IACrE,OAAO,CAAC,kBAAkB;CA0B1B","sourcesContent":["/**\n * BuddyComponent — TUI component rendering the buddy companion.\n *\n * Renders below the editor with:\n * - 3-frame idle animation cycling at 500ms\n * - Speech bubbles for reactions and name-call responses\n * - Pet hearts animation (2.5s)\n * - Narrow terminal fallback (<100 cols)\n * - Stat display and rarity badge\n */\n\nimport type { Component, MarkdownTheme as MT, TUI } from \"@dreb/tui\";\nimport { joinColumns, truncateToWidth, visibleWidth, wrapTextWithAnsi } from \"@dreb/tui\";\nimport { marked } from \"marked\";\nimport { applyEyes, getSpeciesFrames, getSpeciesWidth } from \"../../../core/buddy/buddy-species.js\";\nimport type { BuddyState } from \"../../../core/buddy/buddy-types.js\";\nimport { Rarity, Stat } from \"../../../core/buddy/buddy-types.js\";\nimport { getMarkdownTheme, theme } from \"../theme/theme.js\";\n\nconst IDLE_INTERVAL_MS = 500;\nconst SPEECH_BUBBLE_DURATION_MS = 10000;\nconst PET_DURATION_MS = 2500;\nconst HEART_CHARS = [\"❤️\", \"💕\", \"💖\", \"💗\", \"✨\"];\nconst NARROW_THRESHOLD = 100;\nconst SPEECH_MAX_CONTENT_LINES = 3;\nconst SIDE_PANEL_GAP = 2;\n\nexport class BuddyComponent implements Component {\n\tprivate ui: TUI;\n\tprivate state: BuddyState;\n\tprivate interval: ReturnType<typeof setInterval> | null = null;\n\n\t// Animation state\n\tprivate currentFrame = 0;\n\tprivate totalFrames = 3;\n\n\t// Speech bubble\n\tprivate speechText: string | null = null;\n\tprivate speechTimeout: ReturnType<typeof setTimeout> | null = null;\n\n\t// Pet animation\n\tprivate isPetting = false;\n\tprivate petTimeout: ReturnType<typeof setTimeout> | null = null;\n\tprivate hearts: Array<{ x: number; y: number; char: string; life: number }> = [];\n\n\t// Thinking indicator\n\tprivate thinkingLabel: string | null = null;\n\tprivate thinkingDots = 0;\n\tprivate static readonly THINKING_DOT_COUNT = 4; // cycles 0,1,2,3 → \".\", \"..\", \"...\", \"....\"\n\n\t// Cached render\n\tprivate cachedLines: string[] = [];\n\tprivate cachedWidth = 0;\n\tprivate cachedVersion = -1;\n\tprivate renderVersion = 0;\n\n\tconstructor(ui: TUI, state: BuddyState) {\n\t\tthis.ui = ui;\n\t\tthis.state = state;\n\t\tthis.totalFrames = getSpeciesFrames(state.species).length;\n\t\tthis.startAnimation();\n\t}\n\n\t/** Update buddy state (e.g. after reroll) */\n\tupdateState(state: BuddyState): void {\n\t\tthis.state = state;\n\t\tthis.totalFrames = getSpeciesFrames(state.species).length;\n\t\tthis.currentFrame = 0;\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Show a speech bubble with text */\n\tshowSpeech(text: string): void {\n\t\tthis.speechText = text;\n\t\tif (this.speechTimeout) clearTimeout(this.speechTimeout);\n\t\tthis.speechTimeout = setTimeout(() => {\n\t\t\tthis.speechText = null;\n\t\t\tthis.speechTimeout = null;\n\t\t\tthis.bumpVersion();\n\t\t\tthis.ui.requestRender();\n\t\t}, SPEECH_BUBBLE_DURATION_MS);\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Trigger pet animation */\n\tpet(): void {\n\t\tthis.isPetting = true;\n\t\t// Spawn hearts\n\t\tconst spriteWidth = getSpeciesWidth(this.state.species);\n\t\tfor (let i = 0; i < 5; i++) {\n\t\t\tthis.hearts.push({\n\t\t\t\tx: Math.floor(Math.random() * spriteWidth),\n\t\t\t\ty: -1 - Math.floor(Math.random() * 3),\n\t\t\t\tchar: HEART_CHARS[Math.floor(Math.random() * HEART_CHARS.length)],\n\t\t\t\tlife: 5 + Math.floor(Math.random() * 5),\n\t\t\t});\n\t\t}\n\t\tif (this.petTimeout) clearTimeout(this.petTimeout);\n\t\tthis.petTimeout = setTimeout(() => {\n\t\t\tthis.isPetting = false;\n\t\t\tthis.hearts = [];\n\t\t\tthis.petTimeout = null;\n\t\t\tthis.bumpVersion();\n\t\t\tthis.ui.requestRender();\n\t\t}, PET_DURATION_MS);\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Show a pulsing thinking indicator with optional label */\n\tshowThinking(label?: string): void {\n\t\tthis.thinkingLabel = label ?? \"thinking\";\n\t\tthis.thinkingDots = 0;\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Hide the thinking indicator */\n\thideThinking(): void {\n\t\tthis.thinkingLabel = null;\n\t\tthis.thinkingDots = 0;\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\tinvalidate(): void {\n\t\tthis.cachedWidth = 0;\n\t}\n\n\trender(width: number): string[] {\n\t\tif (width === this.cachedWidth && this.cachedVersion === this.renderVersion) {\n\t\t\treturn this.cachedLines;\n\t\t}\n\n\t\tif (width < NARROW_THRESHOLD) {\n\t\t\tthis.cachedLines = this.renderNarrow(width);\n\t\t} else {\n\t\t\tthis.cachedLines = this.renderFull(width);\n\t\t}\n\n\t\tthis.cachedWidth = width;\n\t\tthis.cachedVersion = this.renderVersion;\n\t\treturn this.cachedLines;\n\t}\n\n\tdispose(): void {\n\t\tthis.stopAnimation();\n\t\tthis.thinkingLabel = null;\n\t\tif (this.speechTimeout) {\n\t\t\tclearTimeout(this.speechTimeout);\n\t\t\tthis.speechTimeout = null;\n\t\t}\n\t\tif (this.petTimeout) {\n\t\t\tclearTimeout(this.petTimeout);\n\t\t\tthis.petTimeout = null;\n\t\t}\n\t}\n\n\t// =============================================================================\n\t// Full rendering (wide terminal)\n\t// =============================================================================\n\n\tprivate renderFull(width: number): string[] {\n\t\tconst lines: string[] = [];\n\t\tconst frames = getSpeciesFrames(this.state.species);\n\t\tconst frame = frames[this.currentFrame % this.totalFrames];\n\t\tconst rendered = applyEyes(frame, this.state.eyeStyle);\n\t\tconst spriteWidth = getSpeciesWidth(this.state.species);\n\n\t\t// Build LEFT block: hat + heart animation + sprite lines\n\t\tconst leftLines: string[] = [];\n\n\t\t// Hat line\n\t\tif (this.state.hat) {\n\t\t\tconst hatPad = Math.max(0, Math.floor((spriteWidth - 1) / 2) - 1);\n\t\t\tleftLines.push(\" \".repeat(hatPad) + this.state.hat);\n\t\t}\n\n\t\t// Heart animation line (above sprite, inserted at top)\n\t\tif (this.isPetting && this.hearts.length > 0) {\n\t\t\tconst heartLine = \" \".repeat(spriteWidth);\n\t\t\tconst chars = heartLine.split(\"\");\n\t\t\tfor (const heart of this.hearts) {\n\t\t\t\tconst x = Math.min(heart.x, chars.length - 2);\n\t\t\t\tif (x >= 0 && heart.life > 0) {\n\t\t\t\t\tconst hChar = heart.char;\n\t\t\t\t\tchars.splice(x, hChar.length, ...hChar.split(\"\"));\n\t\t\t\t}\n\t\t\t}\n\t\t\tleftLines.push(chars.join(\"\"));\n\t\t}\n\n\t\t// Sprite lines\n\t\tleftLines.push(...rendered);\n\n\t\t// Build RIGHT block: speech bubble or thinking indicator\n\t\tlet rightLines: string[] = [];\n\t\tif (this.speechText) {\n\t\t\tconst availableWidth = width - spriteWidth - SIDE_PANEL_GAP - 5; // 5 for leading space + bubble borders + padding\n\t\t\tconst bubbleMaxWidth = Math.max(20, availableWidth);\n\t\t\trightLines = this.formatSpeechBubble(this.speechText, bubbleMaxWidth);\n\t\t} else if (this.thinkingLabel !== null) {\n\t\t\tconst dots = \".\".repeat((this.thinkingDots % BuddyComponent.THINKING_DOT_COUNT) + 1);\n\t\t\tconst label = this.thinkingLabel || \"thinking\";\n\t\t\trightLines = [` ${theme.fg(\"muted\", `💭 ${label}${dots}`)}`];\n\t\t}\n\n\t\t// Merge left and right side-by-side\n\t\tif (rightLines.length > 0) {\n\t\t\tconst merged = joinColumns(leftLines, rightLines, SIDE_PANEL_GAP, width);\n\t\t\tlines.push(...merged);\n\t\t} else {\n\t\t\tlines.push(...leftLines);\n\t\t}\n\n\t\t// Name + rarity line (full width, below the sprite+panel area)\n\t\tconst shinyMark = this.state.shiny ? \" ✨\" : \"\";\n\t\tconst rarityColor = this.rarityColor(this.state.rarity);\n\t\tconst nameLine = ` ${theme.bold(this.state.name)}${shinyMark} ${theme.fg(rarityColor, `[${this.state.rarity}]`)} ${theme.fg(\"muted\", this.state.species)}`;\n\t\tlines.push(nameLine);\n\n\t\t// Stats line (full width)\n\t\tconst statParts = (Object.values(Stat) as Stat[]).map((s) => {\n\t\t\tconst val = this.state.stats[s];\n\t\t\tconst bar = this.statBar(val);\n\t\t\treturn `${theme.fg(\"muted\", s[0])}:${bar}`;\n\t\t});\n\t\tlines.push(` ${statParts.join(\" \")}`);\n\n\t\treturn lines;\n\t}\n\n\t// =============================================================================\n\t// Narrow rendering (< 100 cols)\n\t// =============================================================================\n\n\tprivate renderNarrow(width: number): string[] {\n\t\tconst lines: string[] = [];\n\n\t\t// Single-line face\n\t\tconst eyes = this.state.eyeStyle;\n\t\tconst mouth = this.isPetting ? \"♥\" : \">\";\n\t\tconst face = `${this.state.hat}${eyes}${mouth}${eyes}`;\n\n\t\t// Name + truncated quip\n\t\tconst shinyMark = this.state.shiny ? \"✨\" : \"\";\n\t\tconst name = `${theme.bold(this.state.name)}${shinyMark}`;\n\n\t\tif (this.speechText) {\n\t\t\tconst maxQuip = Math.max(10, width - face.length - this.state.name.length - 6);\n\t\t\tconst styledQuip = this.renderInlineMarkdown(this.speechText);\n\t\t\tconst quip = visibleWidth(styledQuip) > maxQuip ? `${truncateToWidth(styledQuip, maxQuip - 1)}…` : styledQuip;\n\t\t\tlines.push(` ${face} ${name}: ${theme.fg(\"accent\", quip)}`);\n\t\t} else if (this.thinkingLabel !== null) {\n\t\t\tconst dots = \".\".repeat((this.thinkingDots % BuddyComponent.THINKING_DOT_COUNT) + 1);\n\t\t\tconst label = this.thinkingLabel || \"thinking\";\n\t\t\tlines.push(` ${face} ${name} ${theme.fg(\"muted\", `💭 ${label}${dots}`)}`);\n\t\t} else {\n\t\t\tlines.push(` ${face} ${name}`);\n\t\t}\n\n\t\treturn lines;\n\t}\n\n\t// =============================================================================\n\t// Animation\n\t// =============================================================================\n\n\tprivate startAnimation(): void {\n\t\tthis.interval = setInterval(() => {\n\t\t\tthis.currentFrame = (this.currentFrame + 1) % this.totalFrames;\n\n\t\t\t// Tick thinking dots\n\t\t\tif (this.thinkingLabel !== null) {\n\t\t\t\tthis.thinkingDots = (this.thinkingDots + 1) % BuddyComponent.THINKING_DOT_COUNT;\n\t\t\t}\n\n\t\t\t// Tick hearts\n\t\t\tif (this.isPetting) {\n\t\t\t\tfor (const heart of this.hearts) {\n\t\t\t\t\theart.life--;\n\t\t\t\t\theart.y++;\n\t\t\t\t}\n\t\t\t\tthis.hearts = this.hearts.filter((h) => h.life > 0);\n\t\t\t}\n\n\t\t\tthis.bumpVersion();\n\t\t\tthis.ui.requestRender();\n\t\t}, IDLE_INTERVAL_MS);\n\t}\n\n\tprivate stopAnimation(): void {\n\t\tif (this.interval) {\n\t\t\tclearInterval(this.interval);\n\t\t\tthis.interval = null;\n\t\t}\n\t}\n\n\tprivate bumpVersion(): void {\n\t\tthis.renderVersion++;\n\t}\n\n\t// =============================================================================\n\t// Formatting helpers\n\t// =============================================================================\n\n\tprivate statBar(value: number): string {\n\t\tconst filled = Math.round(value / 10);\n\t\tconst empty = 10 - filled;\n\t\tconst bar = \"█\".repeat(filled) + \"░\".repeat(empty);\n\t\tconst color: \"success\" | \"warning\" | \"error\" = value >= 70 ? \"success\" : value >= 40 ? \"warning\" : \"error\";\n\t\treturn theme.fg(color, bar);\n\t}\n\n\tprivate rarityColor(rarity: Rarity): \"muted\" | \"success\" | \"accent\" | \"warning\" | \"error\" {\n\t\tswitch (rarity) {\n\t\t\tcase Rarity.COMMON:\n\t\t\t\treturn \"muted\";\n\t\t\tcase Rarity.UNCOMMON:\n\t\t\t\treturn \"success\";\n\t\t\tcase Rarity.RARE:\n\t\t\t\treturn \"accent\";\n\t\t\tcase Rarity.EPIC:\n\t\t\t\treturn \"warning\";\n\t\t\tcase Rarity.LEGENDARY:\n\t\t\t\treturn \"error\";\n\t\t}\n\t}\n\n\tprivate formatSpeechBubble(text: string, maxWidth: number): string[] {\n\t\tif (!text.trim()) return [];\n\n\t\t// Render inline markdown (bold, italic, code) to styled text with ANSI codes\n\t\tconst styledText = this.renderInlineMarkdown(text);\n\n\t\t// Word-wrap using visible width (ANSI-aware, handles long words)\n\t\tconst lines = wrapTextWithAnsi(styledText, maxWidth);\n\n\t\t// Enforce hard line cap\n\t\tif (lines.length > SPEECH_MAX_CONTENT_LINES) {\n\t\t\tconst kept = lines.slice(0, SPEECH_MAX_CONTENT_LINES - 1);\n\t\t\tconst lastLine = lines[SPEECH_MAX_CONTENT_LINES - 1];\n\t\t\t// Truncate the last kept line with ellipsis\n\t\t\tconst truncated = truncateToWidth(lastLine, maxWidth - 1, \"…\");\n\t\t\tkept.push(truncated);\n\t\t\tlines.length = 0;\n\t\t\tlines.push(...kept);\n\t\t}\n\n\t\t// Wrap in bubble border using visible width for measurement\n\t\tconst maxLineWidth = Math.max(...lines.map((l) => visibleWidth(l)));\n\t\tconst bubbleWidth = Math.max(6, Math.min(maxLineWidth + 4, maxWidth + 4));\n\t\tconst top = `╭${\"─\".repeat(bubbleWidth - 2)}╮`;\n\t\tconst bottom = `╰${\"─\".repeat(bubbleWidth - 2)}╯`;\n\n\t\tconst result: string[] = [];\n\t\tresult.push(` ${theme.fg(\"accent\", top)}`);\n\t\tfor (const line of lines) {\n\t\t\tconst padding = Math.max(0, bubbleWidth - 4 - visibleWidth(line));\n\t\t\tconst padded = line + \" \".repeat(padding);\n\t\t\tresult.push(` ${theme.fg(\"accent\", \"│\")} ${padded} ${theme.fg(\"accent\", \"│\")}`);\n\t\t}\n\t\tresult.push(` ${theme.fg(\"accent\", bottom)}`);\n\n\t\treturn result;\n\t}\n\n\t/** Render inline markdown tokens (bold, italic, code) to ANSI-styled text */\n\tprivate renderInlineMarkdown(text: string): string {\n\t\tconst mdTheme = getMarkdownTheme();\n\t\tconst tokens = marked.lexer(text);\n\n\t\t// Flatten: we expect a paragraph containing inline tokens\n\t\tconst inlineTokens = tokens.flatMap((t: any) =>\n\t\t\tt.type === \"paragraph\" ? (t.tokens ?? []) : t.type === \"text\" ? t : [],\n\t\t);\n\n\t\treturn this.renderInlineTokens(inlineTokens, mdTheme);\n\t}\n\n\t/** Recursively render marked inline tokens to ANSI-styled strings */\n\tprivate renderInlineTokens(tokens: any[], mdTheme: MT): string {\n\t\tlet result = \"\";\n\t\tfor (const token of tokens) {\n\t\t\tswitch (token.type) {\n\t\t\t\tcase \"text\":\n\t\t\t\t\tresult += token.text ?? this.renderInlineTokens(token.tokens ?? [], mdTheme);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"strong\":\n\t\t\t\t\tresult += mdTheme.bold(this.renderInlineTokens(token.tokens ?? [], mdTheme));\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"em\":\n\t\t\t\t\tresult += mdTheme.italic(this.renderInlineTokens(token.tokens ?? [], mdTheme));\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"codespan\":\n\t\t\t\t\tresult += mdTheme.code(token.text);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"escape\":\n\t\t\t\t\tresult += token.text;\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\tresult += token.text ?? token.raw ?? \"\";\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n}\n"]}
@@ -8,7 +8,7 @@
8
8
  * - Narrow terminal fallback (<100 cols)
9
9
  * - Stat display and rarity badge
10
10
  */
11
- import { truncateToWidth, visibleWidth } from "@dreb/tui";
11
+ import { joinColumns, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@dreb/tui";
12
12
  import { marked } from "marked";
13
13
  import { applyEyes, getSpeciesFrames, getSpeciesWidth } from "../../../core/buddy/buddy-species.js";
14
14
  import { Rarity, Stat } from "../../../core/buddy/buddy-types.js";
@@ -18,6 +18,8 @@ const SPEECH_BUBBLE_DURATION_MS = 10000;
18
18
  const PET_DURATION_MS = 2500;
19
19
  const HEART_CHARS = ["❤️", "💕", "💖", "💗", "✨"];
20
20
  const NARROW_THRESHOLD = 100;
21
+ const SPEECH_MAX_CONTENT_LINES = 3;
22
+ const SIDE_PANEL_GAP = 2;
21
23
  export class BuddyComponent {
22
24
  ui;
23
25
  state;
@@ -145,15 +147,15 @@ export class BuddyComponent {
145
147
  const frames = getSpeciesFrames(this.state.species);
146
148
  const frame = frames[this.currentFrame % this.totalFrames];
147
149
  const rendered = applyEyes(frame, this.state.eyeStyle);
148
- // Hat line (if present)
150
+ const spriteWidth = getSpeciesWidth(this.state.species);
151
+ // Build LEFT block: hat + heart animation + sprite lines
152
+ const leftLines = [];
153
+ // Hat line
149
154
  if (this.state.hat) {
150
- const hatPad = Math.max(0, Math.floor((getSpeciesWidth(this.state.species) - 1) / 2) - 1);
151
- lines.push(" ".repeat(hatPad) + this.state.hat);
155
+ const hatPad = Math.max(0, Math.floor((spriteWidth - 1) / 2) - 1);
156
+ leftLines.push(" ".repeat(hatPad) + this.state.hat);
152
157
  }
153
- // Sprite lines
154
- const spriteWidth = getSpeciesWidth(this.state.species);
155
- lines.push(...rendered);
156
- // Heart animation line above sprite
158
+ // Heart animation line (above sprite, inserted at top)
157
159
  if (this.isPetting && this.hearts.length > 0) {
158
160
  const heartLine = " ".repeat(spriteWidth);
159
161
  const chars = heartLine.split("");
@@ -164,32 +166,42 @@ export class BuddyComponent {
164
166
  chars.splice(x, hChar.length, ...hChar.split(""));
165
167
  }
166
168
  }
167
- // Insert heart line before sprite
168
- lines.splice(this.state.hat ? 1 : 0, 0, chars.join(""));
169
+ leftLines.push(chars.join(""));
170
+ }
171
+ // Sprite lines
172
+ leftLines.push(...rendered);
173
+ // Build RIGHT block: speech bubble or thinking indicator
174
+ let rightLines = [];
175
+ if (this.speechText) {
176
+ const availableWidth = width - spriteWidth - SIDE_PANEL_GAP - 5; // 5 for leading space + bubble borders + padding
177
+ const bubbleMaxWidth = Math.max(20, availableWidth);
178
+ rightLines = this.formatSpeechBubble(this.speechText, bubbleMaxWidth);
179
+ }
180
+ else if (this.thinkingLabel !== null) {
181
+ const dots = ".".repeat((this.thinkingDots % BuddyComponent.THINKING_DOT_COUNT) + 1);
182
+ const label = this.thinkingLabel || "thinking";
183
+ rightLines = [` ${theme.fg("muted", `💭 ${label}${dots}`)}`];
184
+ }
185
+ // Merge left and right side-by-side
186
+ if (rightLines.length > 0) {
187
+ const merged = joinColumns(leftLines, rightLines, SIDE_PANEL_GAP, width);
188
+ lines.push(...merged);
169
189
  }
170
- // Name + rarity line
190
+ else {
191
+ lines.push(...leftLines);
192
+ }
193
+ // Name + rarity line (full width, below the sprite+panel area)
171
194
  const shinyMark = this.state.shiny ? " ✨" : "";
172
195
  const rarityColor = this.rarityColor(this.state.rarity);
173
196
  const nameLine = ` ${theme.bold(this.state.name)}${shinyMark} ${theme.fg(rarityColor, `[${this.state.rarity}]`)} ${theme.fg("muted", this.state.species)}`;
174
197
  lines.push(nameLine);
175
- // Stats line
198
+ // Stats line (full width)
176
199
  const statParts = Object.values(Stat).map((s) => {
177
200
  const val = this.state.stats[s];
178
201
  const bar = this.statBar(val);
179
202
  return `${theme.fg("muted", s[0])}:${bar}`;
180
203
  });
181
204
  lines.push(` ${statParts.join(" ")}`);
182
- // Thinking indicator
183
- if (this.thinkingLabel !== null) {
184
- const dots = ".".repeat((this.thinkingDots % BuddyComponent.THINKING_DOT_COUNT) + 1);
185
- const label = this.thinkingLabel || "thinking";
186
- lines.push(` ${theme.fg("muted", `💭 ${label}${dots}`)}`);
187
- }
188
- // Speech bubble (beside or below sprite)
189
- if (this.speechText) {
190
- const bubbleLines = this.formatSpeechBubble(this.speechText, Math.min(width - 4, 60));
191
- lines.push(...bubbleLines);
192
- }
193
205
  return lines;
194
206
  }
195
207
  // =============================================================================
@@ -280,23 +292,18 @@ export class BuddyComponent {
280
292
  return [];
281
293
  // Render inline markdown (bold, italic, code) to styled text with ANSI codes
282
294
  const styledText = this.renderInlineMarkdown(text);
283
- // Word-wrap using visible width (ANSI-aware)
284
- const lines = [];
285
- const words = styledText.split(" ");
286
- let currentLine = "";
287
- for (const word of words) {
288
- const test = currentLine ? `${currentLine} ${word}` : word;
289
- if (visibleWidth(test) > maxWidth) {
290
- if (currentLine)
291
- lines.push(currentLine);
292
- currentLine = word;
293
- }
294
- else {
295
- currentLine = test;
296
- }
295
+ // Word-wrap using visible width (ANSI-aware, handles long words)
296
+ const lines = wrapTextWithAnsi(styledText, maxWidth);
297
+ // Enforce hard line cap
298
+ if (lines.length > SPEECH_MAX_CONTENT_LINES) {
299
+ const kept = lines.slice(0, SPEECH_MAX_CONTENT_LINES - 1);
300
+ const lastLine = lines[SPEECH_MAX_CONTENT_LINES - 1];
301
+ // Truncate the last kept line with ellipsis
302
+ const truncated = truncateToWidth(lastLine, maxWidth - 1, "…");
303
+ kept.push(truncated);
304
+ lines.length = 0;
305
+ lines.push(...kept);
297
306
  }
298
- if (currentLine)
299
- lines.push(currentLine);
300
307
  // Wrap in bubble border using visible width for measurement
301
308
  const maxLineWidth = Math.max(...lines.map((l) => visibleWidth(l)));
302
309
  const bubbleWidth = Math.max(6, Math.min(maxLineWidth + 4, maxWidth + 4));
@@ -1 +1 @@
1
- {"version":3,"file":"buddy-component.js","sourceRoot":"","sources":["../../../../src/modes/interactive/components/buddy-component.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC1D,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,sCAAsC,CAAC;AAEpG,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE5D,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAC7B,MAAM,yBAAyB,GAAG,KAAK,CAAC;AACxC,MAAM,eAAe,GAAG,IAAI,CAAC;AAC7B,MAAM,WAAW,GAAG,CAAC,QAAI,EAAE,MAAG,EAAE,MAAG,EAAE,MAAG,EAAE,KAAG,CAAC,CAAC;AAC/C,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAE7B,MAAM,OAAO,cAAc;IAClB,EAAE,CAAM;IACR,KAAK,CAAa;IAClB,QAAQ,GAA0C,IAAI,CAAC;IAE/D,kBAAkB;IACV,YAAY,GAAG,CAAC,CAAC;IACjB,WAAW,GAAG,CAAC,CAAC;IAExB,gBAAgB;IACR,UAAU,GAAkB,IAAI,CAAC;IACjC,aAA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UAAU;oBACd,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;oBACnC,MAAM;gBACP,KAAK,QAAQ;oBACZ,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC;oBACrB,MAAM;gBACP;oBACC,MAAM,IAAI,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,GAAG,IAAI,EAAE,CAAC;oBACxC,MAAM;YACR,CAAC;QACF,CAAC;QACD,OAAO,MAAM,CAAC;IAAA,CACd;CACD","sourcesContent":["/**\n * BuddyComponent — TUI component rendering the buddy companion.\n *\n * Renders below the editor with:\n * - 3-frame idle animation cycling at 500ms\n * - Speech bubbles for reactions and name-call responses\n * - Pet hearts animation (2.5s)\n * - Narrow terminal fallback (<100 cols)\n * - Stat display and rarity badge\n */\n\nimport type { Component, MarkdownTheme as MT, TUI } from \"@dreb/tui\";\nimport { truncateToWidth, visibleWidth } from \"@dreb/tui\";\nimport { marked } from \"marked\";\nimport { applyEyes, getSpeciesFrames, getSpeciesWidth } from \"../../../core/buddy/buddy-species.js\";\nimport type { BuddyState } from \"../../../core/buddy/buddy-types.js\";\nimport { Rarity, Stat } from \"../../../core/buddy/buddy-types.js\";\nimport { getMarkdownTheme, theme } from \"../theme/theme.js\";\n\nconst IDLE_INTERVAL_MS = 500;\nconst SPEECH_BUBBLE_DURATION_MS = 10000;\nconst PET_DURATION_MS = 2500;\nconst HEART_CHARS = [\"❤️\", \"💕\", \"💖\", \"💗\", \"✨\"];\nconst NARROW_THRESHOLD = 100;\n\nexport class BuddyComponent implements Component {\n\tprivate ui: TUI;\n\tprivate state: BuddyState;\n\tprivate interval: ReturnType<typeof setInterval> | null = null;\n\n\t// Animation state\n\tprivate currentFrame = 0;\n\tprivate totalFrames = 3;\n\n\t// Speech bubble\n\tprivate speechText: string | null = null;\n\tprivate speechTimeout: ReturnType<typeof setTimeout> | null = null;\n\n\t// Pet animation\n\tprivate isPetting = false;\n\tprivate petTimeout: ReturnType<typeof setTimeout> | null = null;\n\tprivate hearts: Array<{ x: number; y: number; char: string; life: number }> = [];\n\n\t// Thinking indicator\n\tprivate thinkingLabel: string | null = null;\n\tprivate thinkingDots = 0;\n\tprivate static readonly THINKING_DOT_COUNT = 4; // cycles 0,1,2,3 → \".\", \"..\", \"...\", \"....\"\n\n\t// Cached render\n\tprivate cachedLines: string[] = [];\n\tprivate cachedWidth = 0;\n\tprivate cachedVersion = -1;\n\tprivate renderVersion = 0;\n\n\tconstructor(ui: TUI, state: BuddyState) {\n\t\tthis.ui = ui;\n\t\tthis.state = state;\n\t\tthis.totalFrames = getSpeciesFrames(state.species).length;\n\t\tthis.startAnimation();\n\t}\n\n\t/** Update buddy state (e.g. after reroll) */\n\tupdateState(state: BuddyState): void {\n\t\tthis.state = state;\n\t\tthis.totalFrames = getSpeciesFrames(state.species).length;\n\t\tthis.currentFrame = 0;\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Show a speech bubble with text */\n\tshowSpeech(text: string): void {\n\t\tthis.speechText = text;\n\t\tif (this.speechTimeout) clearTimeout(this.speechTimeout);\n\t\tthis.speechTimeout = setTimeout(() => {\n\t\t\tthis.speechText = null;\n\t\t\tthis.speechTimeout = null;\n\t\t\tthis.bumpVersion();\n\t\t\tthis.ui.requestRender();\n\t\t}, SPEECH_BUBBLE_DURATION_MS);\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Trigger pet animation */\n\tpet(): void {\n\t\tthis.isPetting = true;\n\t\t// Spawn hearts\n\t\tconst spriteWidth = getSpeciesWidth(this.state.species);\n\t\tfor (let i = 0; i < 5; i++) {\n\t\t\tthis.hearts.push({\n\t\t\t\tx: Math.floor(Math.random() * spriteWidth),\n\t\t\t\ty: -1 - Math.floor(Math.random() * 3),\n\t\t\t\tchar: HEART_CHARS[Math.floor(Math.random() * HEART_CHARS.length)],\n\t\t\t\tlife: 5 + Math.floor(Math.random() * 5),\n\t\t\t});\n\t\t}\n\t\tif (this.petTimeout) clearTimeout(this.petTimeout);\n\t\tthis.petTimeout = setTimeout(() => {\n\t\t\tthis.isPetting = false;\n\t\t\tthis.hearts = [];\n\t\t\tthis.petTimeout = null;\n\t\t\tthis.bumpVersion();\n\t\t\tthis.ui.requestRender();\n\t\t}, PET_DURATION_MS);\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Show a pulsing thinking indicator with optional label */\n\tshowThinking(label?: string): void {\n\t\tthis.thinkingLabel = label ?? \"thinking\";\n\t\tthis.thinkingDots = 0;\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Hide the thinking indicator */\n\thideThinking(): void {\n\t\tthis.thinkingLabel = null;\n\t\tthis.thinkingDots = 0;\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\tinvalidate(): void {\n\t\tthis.cachedWidth = 0;\n\t}\n\n\trender(width: number): string[] {\n\t\tif (width === this.cachedWidth && this.cachedVersion === this.renderVersion) {\n\t\t\treturn this.cachedLines;\n\t\t}\n\n\t\tif (width < NARROW_THRESHOLD) {\n\t\t\tthis.cachedLines = this.renderNarrow(width);\n\t\t} else {\n\t\t\tthis.cachedLines = this.renderFull(width);\n\t\t}\n\n\t\tthis.cachedWidth = width;\n\t\tthis.cachedVersion = this.renderVersion;\n\t\treturn this.cachedLines;\n\t}\n\n\tdispose(): void {\n\t\tthis.stopAnimation();\n\t\tthis.thinkingLabel = null;\n\t\tif (this.speechTimeout) {\n\t\t\tclearTimeout(this.speechTimeout);\n\t\t\tthis.speechTimeout = null;\n\t\t}\n\t\tif (this.petTimeout) {\n\t\t\tclearTimeout(this.petTimeout);\n\t\t\tthis.petTimeout = null;\n\t\t}\n\t}\n\n\t// =============================================================================\n\t// Full rendering (wide terminal)\n\t// =============================================================================\n\n\tprivate renderFull(width: number): string[] {\n\t\tconst lines: string[] = [];\n\t\tconst frames = getSpeciesFrames(this.state.species);\n\t\tconst frame = frames[this.currentFrame % this.totalFrames];\n\t\tconst rendered = applyEyes(frame, this.state.eyeStyle);\n\n\t\t// Hat line (if present)\n\t\tif (this.state.hat) {\n\t\t\tconst hatPad = Math.max(0, Math.floor((getSpeciesWidth(this.state.species) - 1) / 2) - 1);\n\t\t\tlines.push(\" \".repeat(hatPad) + this.state.hat);\n\t\t}\n\n\t\t// Sprite lines\n\t\tconst spriteWidth = getSpeciesWidth(this.state.species);\n\t\tlines.push(...rendered);\n\n\t\t// Heart animation line above sprite\n\t\tif (this.isPetting && this.hearts.length > 0) {\n\t\t\tconst heartLine = \" \".repeat(spriteWidth);\n\t\t\tconst chars = heartLine.split(\"\");\n\t\t\tfor (const heart of this.hearts) {\n\t\t\t\tconst x = Math.min(heart.x, chars.length - 2);\n\t\t\t\tif (x >= 0 && heart.life > 0) {\n\t\t\t\t\tconst hChar = heart.char;\n\t\t\t\t\tchars.splice(x, hChar.length, ...hChar.split(\"\"));\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Insert heart line before sprite\n\t\t\tlines.splice(this.state.hat ? 1 : 0, 0, chars.join(\"\"));\n\t\t}\n\n\t\t// Name + rarity line\n\t\tconst shinyMark = this.state.shiny ? \" ✨\" : \"\";\n\t\tconst rarityColor = this.rarityColor(this.state.rarity);\n\t\tconst nameLine = ` ${theme.bold(this.state.name)}${shinyMark} ${theme.fg(rarityColor, `[${this.state.rarity}]`)} ${theme.fg(\"muted\", this.state.species)}`;\n\t\tlines.push(nameLine);\n\n\t\t// Stats line\n\t\tconst statParts = (Object.values(Stat) as Stat[]).map((s) => {\n\t\t\tconst val = this.state.stats[s];\n\t\t\tconst bar = this.statBar(val);\n\t\t\treturn `${theme.fg(\"muted\", s[0])}:${bar}`;\n\t\t});\n\t\tlines.push(` ${statParts.join(\" \")}`);\n\n\t\t// Thinking indicator\n\t\tif (this.thinkingLabel !== null) {\n\t\t\tconst dots = \".\".repeat((this.thinkingDots % BuddyComponent.THINKING_DOT_COUNT) + 1);\n\t\t\tconst label = this.thinkingLabel || \"thinking\";\n\t\t\tlines.push(` ${theme.fg(\"muted\", `💭 ${label}${dots}`)}`);\n\t\t}\n\n\t\t// Speech bubble (beside or below sprite)\n\t\tif (this.speechText) {\n\t\t\tconst bubbleLines = this.formatSpeechBubble(this.speechText, Math.min(width - 4, 60));\n\t\t\tlines.push(...bubbleLines);\n\t\t}\n\n\t\treturn lines;\n\t}\n\n\t// =============================================================================\n\t// Narrow rendering (< 100 cols)\n\t// =============================================================================\n\n\tprivate renderNarrow(width: number): string[] {\n\t\tconst lines: string[] = [];\n\n\t\t// Single-line face\n\t\tconst eyes = this.state.eyeStyle;\n\t\tconst mouth = this.isPetting ? \"♥\" : \">\";\n\t\tconst face = `${this.state.hat}${eyes}${mouth}${eyes}`;\n\n\t\t// Name + truncated quip\n\t\tconst shinyMark = this.state.shiny ? \"✨\" : \"\";\n\t\tconst name = `${theme.bold(this.state.name)}${shinyMark}`;\n\n\t\tif (this.speechText) {\n\t\t\tconst maxQuip = Math.max(10, width - face.length - this.state.name.length - 6);\n\t\t\tconst styledQuip = this.renderInlineMarkdown(this.speechText);\n\t\t\tconst quip = visibleWidth(styledQuip) > maxQuip ? `${truncateToWidth(styledQuip, maxQuip - 1)}…` : styledQuip;\n\t\t\tlines.push(` ${face} ${name}: ${theme.fg(\"accent\", quip)}`);\n\t\t} else if (this.thinkingLabel !== null) {\n\t\t\tconst dots = \".\".repeat((this.thinkingDots % BuddyComponent.THINKING_DOT_COUNT) + 1);\n\t\t\tconst label = this.thinkingLabel || \"thinking\";\n\t\t\tlines.push(` ${face} ${name} ${theme.fg(\"muted\", `💭 ${label}${dots}`)}`);\n\t\t} else {\n\t\t\tlines.push(` ${face} ${name}`);\n\t\t}\n\n\t\treturn lines;\n\t}\n\n\t// =============================================================================\n\t// Animation\n\t// =============================================================================\n\n\tprivate startAnimation(): void {\n\t\tthis.interval = setInterval(() => {\n\t\t\tthis.currentFrame = (this.currentFrame + 1) % this.totalFrames;\n\n\t\t\t// Tick thinking dots\n\t\t\tif (this.thinkingLabel !== null) {\n\t\t\t\tthis.thinkingDots = (this.thinkingDots + 1) % BuddyComponent.THINKING_DOT_COUNT;\n\t\t\t}\n\n\t\t\t// Tick hearts\n\t\t\tif (this.isPetting) {\n\t\t\t\tfor (const heart of this.hearts) {\n\t\t\t\t\theart.life--;\n\t\t\t\t\theart.y++;\n\t\t\t\t}\n\t\t\t\tthis.hearts = this.hearts.filter((h) => h.life > 0);\n\t\t\t}\n\n\t\t\tthis.bumpVersion();\n\t\t\tthis.ui.requestRender();\n\t\t}, IDLE_INTERVAL_MS);\n\t}\n\n\tprivate stopAnimation(): void {\n\t\tif (this.interval) {\n\t\t\tclearInterval(this.interval);\n\t\t\tthis.interval = null;\n\t\t}\n\t}\n\n\tprivate bumpVersion(): void {\n\t\tthis.renderVersion++;\n\t}\n\n\t// =============================================================================\n\t// Formatting helpers\n\t// =============================================================================\n\n\tprivate statBar(value: number): string {\n\t\tconst filled = Math.round(value / 10);\n\t\tconst empty = 10 - filled;\n\t\tconst bar = \"█\".repeat(filled) + \"░\".repeat(empty);\n\t\tconst color: \"success\" | \"warning\" | \"error\" = value >= 70 ? \"success\" : value >= 40 ? \"warning\" : \"error\";\n\t\treturn theme.fg(color, bar);\n\t}\n\n\tprivate rarityColor(rarity: Rarity): \"muted\" | \"success\" | \"accent\" | \"warning\" | \"error\" {\n\t\tswitch (rarity) {\n\t\t\tcase Rarity.COMMON:\n\t\t\t\treturn \"muted\";\n\t\t\tcase Rarity.UNCOMMON:\n\t\t\t\treturn \"success\";\n\t\t\tcase Rarity.RARE:\n\t\t\t\treturn \"accent\";\n\t\t\tcase Rarity.EPIC:\n\t\t\t\treturn \"warning\";\n\t\t\tcase Rarity.LEGENDARY:\n\t\t\t\treturn \"error\";\n\t\t}\n\t}\n\n\tprivate formatSpeechBubble(text: string, maxWidth: number): string[] {\n\t\tif (!text.trim()) return [];\n\n\t\t// Render inline markdown (bold, italic, code) to styled text with ANSI codes\n\t\tconst styledText = this.renderInlineMarkdown(text);\n\n\t\t// Word-wrap using visible width (ANSI-aware)\n\t\tconst lines: string[] = [];\n\t\tconst words = styledText.split(\" \");\n\t\tlet currentLine = \"\";\n\n\t\tfor (const word of words) {\n\t\t\tconst test = currentLine ? `${currentLine} ${word}` : word;\n\t\t\tif (visibleWidth(test) > maxWidth) {\n\t\t\t\tif (currentLine) lines.push(currentLine);\n\t\t\t\tcurrentLine = word;\n\t\t\t} else {\n\t\t\t\tcurrentLine = test;\n\t\t\t}\n\t\t}\n\t\tif (currentLine) lines.push(currentLine);\n\n\t\t// Wrap in bubble border using visible width for measurement\n\t\tconst maxLineWidth = Math.max(...lines.map((l) => visibleWidth(l)));\n\t\tconst bubbleWidth = Math.max(6, Math.min(maxLineWidth + 4, maxWidth + 4));\n\t\tconst top = `╭${\"─\".repeat(bubbleWidth - 2)}╮`;\n\t\tconst bottom = `╰${\"─\".repeat(bubbleWidth - 2)}╯`;\n\n\t\tconst result: string[] = [];\n\t\tresult.push(` ${theme.fg(\"accent\", top)}`);\n\t\tfor (const line of lines) {\n\t\t\tconst padding = Math.max(0, bubbleWidth - 4 - visibleWidth(line));\n\t\t\tconst padded = line + \" \".repeat(padding);\n\t\t\tresult.push(` ${theme.fg(\"accent\", \"│\")} ${padded} ${theme.fg(\"accent\", \"│\")}`);\n\t\t}\n\t\tresult.push(` ${theme.fg(\"accent\", bottom)}`);\n\n\t\treturn result;\n\t}\n\n\t/** Render inline markdown tokens (bold, italic, code) to ANSI-styled text */\n\tprivate renderInlineMarkdown(text: string): string {\n\t\tconst mdTheme = getMarkdownTheme();\n\t\tconst tokens = marked.lexer(text);\n\n\t\t// Flatten: we expect a paragraph containing inline tokens\n\t\tconst inlineTokens = tokens.flatMap((t: any) =>\n\t\t\tt.type === \"paragraph\" ? (t.tokens ?? []) : t.type === \"text\" ? t : [],\n\t\t);\n\n\t\treturn this.renderInlineTokens(inlineTokens, mdTheme);\n\t}\n\n\t/** Recursively render marked inline tokens to ANSI-styled strings */\n\tprivate renderInlineTokens(tokens: any[], mdTheme: MT): string {\n\t\tlet result = \"\";\n\t\tfor (const token of tokens) {\n\t\t\tswitch (token.type) {\n\t\t\t\tcase \"text\":\n\t\t\t\t\tresult += token.text ?? this.renderInlineTokens(token.tokens ?? [], mdTheme);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"strong\":\n\t\t\t\t\tresult += mdTheme.bold(this.renderInlineTokens(token.tokens ?? [], mdTheme));\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"em\":\n\t\t\t\t\tresult += mdTheme.italic(this.renderInlineTokens(token.tokens ?? [], mdTheme));\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"codespan\":\n\t\t\t\t\tresult += mdTheme.code(token.text);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"escape\":\n\t\t\t\t\tresult += token.text;\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\tresult += token.text ?? token.raw ?? \"\";\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n}\n"]}
1
+ {"version":3,"file":"buddy-component.js","sourceRoot":"","sources":["../../../../src/modes/interactive/components/buddy-component.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AACzF,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,sCAAsC,CAAC;AAEpG,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE5D,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAC7B,MAAM,yBAAyB,GAAG,KAAK,CAAC;AACxC,MAAM,eAAe,GAAG,IAAI,CAAC;AAC7B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D,kBAAkB,CAAC,MAAa,EAAE,OAAW,EAAU;QAC9D,IAAI,MAAM,GAAG,EAAE,CAAC;QAChB,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC5B,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;gBACpB,KAAK,MAAM;oBACV,MAAM,IAAI,KAAK,CAAC,IAAI,IAAI,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,MAAM,IAAI,EAAE,EAAE,OAAO,CAAC,CAAC;oBAC7E,MAAM;gBACP,KAAK,QAAQ;oBACZ,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,MAAM,IAAI,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC;oBAC7E,MAAM;gBACP,KAAK,IAAI;oBACR,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,MAAM,IAAI,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC;oBAC/E,MAAM;gBACP,KAAK,UAAU;oBACd,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;oBACnC,MAAM;gBACP,KAAK,QAAQ;oBACZ,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC;oBACrB,MAAM;gBACP;oBACC,MAAM,IAAI,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,GAAG,IAAI,EAAE,CAAC;oBACxC,MAAM;YACR,CAAC;QACF,CAAC;QACD,OAAO,MAAM,CAAC;IAAA,CACd;CACD","sourcesContent":["/**\n * BuddyComponent — TUI component rendering the buddy companion.\n *\n * Renders below the editor with:\n * - 3-frame idle animation cycling at 500ms\n * - Speech bubbles for reactions and name-call responses\n * - Pet hearts animation (2.5s)\n * - Narrow terminal fallback (<100 cols)\n * - Stat display and rarity badge\n */\n\nimport type { Component, MarkdownTheme as MT, TUI } from \"@dreb/tui\";\nimport { joinColumns, truncateToWidth, visibleWidth, wrapTextWithAnsi } from \"@dreb/tui\";\nimport { marked } from \"marked\";\nimport { applyEyes, getSpeciesFrames, getSpeciesWidth } from \"../../../core/buddy/buddy-species.js\";\nimport type { BuddyState } from \"../../../core/buddy/buddy-types.js\";\nimport { Rarity, Stat } from \"../../../core/buddy/buddy-types.js\";\nimport { getMarkdownTheme, theme } from \"../theme/theme.js\";\n\nconst IDLE_INTERVAL_MS = 500;\nconst SPEECH_BUBBLE_DURATION_MS = 10000;\nconst PET_DURATION_MS = 2500;\nconst HEART_CHARS = [\"❤️\", \"💕\", \"💖\", \"💗\", \"✨\"];\nconst NARROW_THRESHOLD = 100;\nconst SPEECH_MAX_CONTENT_LINES = 3;\nconst SIDE_PANEL_GAP = 2;\n\nexport class BuddyComponent implements Component {\n\tprivate ui: TUI;\n\tprivate state: BuddyState;\n\tprivate interval: ReturnType<typeof setInterval> | null = null;\n\n\t// Animation state\n\tprivate currentFrame = 0;\n\tprivate totalFrames = 3;\n\n\t// Speech bubble\n\tprivate speechText: string | null = null;\n\tprivate speechTimeout: ReturnType<typeof setTimeout> | null = null;\n\n\t// Pet animation\n\tprivate isPetting = false;\n\tprivate petTimeout: ReturnType<typeof setTimeout> | null = null;\n\tprivate hearts: Array<{ x: number; y: number; char: string; life: number }> = [];\n\n\t// Thinking indicator\n\tprivate thinkingLabel: string | null = null;\n\tprivate thinkingDots = 0;\n\tprivate static readonly THINKING_DOT_COUNT = 4; // cycles 0,1,2,3 → \".\", \"..\", \"...\", \"....\"\n\n\t// Cached render\n\tprivate cachedLines: string[] = [];\n\tprivate cachedWidth = 0;\n\tprivate cachedVersion = -1;\n\tprivate renderVersion = 0;\n\n\tconstructor(ui: TUI, state: BuddyState) {\n\t\tthis.ui = ui;\n\t\tthis.state = state;\n\t\tthis.totalFrames = getSpeciesFrames(state.species).length;\n\t\tthis.startAnimation();\n\t}\n\n\t/** Update buddy state (e.g. after reroll) */\n\tupdateState(state: BuddyState): void {\n\t\tthis.state = state;\n\t\tthis.totalFrames = getSpeciesFrames(state.species).length;\n\t\tthis.currentFrame = 0;\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Show a speech bubble with text */\n\tshowSpeech(text: string): void {\n\t\tthis.speechText = text;\n\t\tif (this.speechTimeout) clearTimeout(this.speechTimeout);\n\t\tthis.speechTimeout = setTimeout(() => {\n\t\t\tthis.speechText = null;\n\t\t\tthis.speechTimeout = null;\n\t\t\tthis.bumpVersion();\n\t\t\tthis.ui.requestRender();\n\t\t}, SPEECH_BUBBLE_DURATION_MS);\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Trigger pet animation */\n\tpet(): void {\n\t\tthis.isPetting = true;\n\t\t// Spawn hearts\n\t\tconst spriteWidth = getSpeciesWidth(this.state.species);\n\t\tfor (let i = 0; i < 5; i++) {\n\t\t\tthis.hearts.push({\n\t\t\t\tx: Math.floor(Math.random() * spriteWidth),\n\t\t\t\ty: -1 - Math.floor(Math.random() * 3),\n\t\t\t\tchar: HEART_CHARS[Math.floor(Math.random() * HEART_CHARS.length)],\n\t\t\t\tlife: 5 + Math.floor(Math.random() * 5),\n\t\t\t});\n\t\t}\n\t\tif (this.petTimeout) clearTimeout(this.petTimeout);\n\t\tthis.petTimeout = setTimeout(() => {\n\t\t\tthis.isPetting = false;\n\t\t\tthis.hearts = [];\n\t\t\tthis.petTimeout = null;\n\t\t\tthis.bumpVersion();\n\t\t\tthis.ui.requestRender();\n\t\t}, PET_DURATION_MS);\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Show a pulsing thinking indicator with optional label */\n\tshowThinking(label?: string): void {\n\t\tthis.thinkingLabel = label ?? \"thinking\";\n\t\tthis.thinkingDots = 0;\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\t/** Hide the thinking indicator */\n\thideThinking(): void {\n\t\tthis.thinkingLabel = null;\n\t\tthis.thinkingDots = 0;\n\t\tthis.bumpVersion();\n\t\tthis.ui.requestRender();\n\t}\n\n\tinvalidate(): void {\n\t\tthis.cachedWidth = 0;\n\t}\n\n\trender(width: number): string[] {\n\t\tif (width === this.cachedWidth && this.cachedVersion === this.renderVersion) {\n\t\t\treturn this.cachedLines;\n\t\t}\n\n\t\tif (width < NARROW_THRESHOLD) {\n\t\t\tthis.cachedLines = this.renderNarrow(width);\n\t\t} else {\n\t\t\tthis.cachedLines = this.renderFull(width);\n\t\t}\n\n\t\tthis.cachedWidth = width;\n\t\tthis.cachedVersion = this.renderVersion;\n\t\treturn this.cachedLines;\n\t}\n\n\tdispose(): void {\n\t\tthis.stopAnimation();\n\t\tthis.thinkingLabel = null;\n\t\tif (this.speechTimeout) {\n\t\t\tclearTimeout(this.speechTimeout);\n\t\t\tthis.speechTimeout = null;\n\t\t}\n\t\tif (this.petTimeout) {\n\t\t\tclearTimeout(this.petTimeout);\n\t\t\tthis.petTimeout = null;\n\t\t}\n\t}\n\n\t// =============================================================================\n\t// Full rendering (wide terminal)\n\t// =============================================================================\n\n\tprivate renderFull(width: number): string[] {\n\t\tconst lines: string[] = [];\n\t\tconst frames = getSpeciesFrames(this.state.species);\n\t\tconst frame = frames[this.currentFrame % this.totalFrames];\n\t\tconst rendered = applyEyes(frame, this.state.eyeStyle);\n\t\tconst spriteWidth = getSpeciesWidth(this.state.species);\n\n\t\t// Build LEFT block: hat + heart animation + sprite lines\n\t\tconst leftLines: string[] = [];\n\n\t\t// Hat line\n\t\tif (this.state.hat) {\n\t\t\tconst hatPad = Math.max(0, Math.floor((spriteWidth - 1) / 2) - 1);\n\t\t\tleftLines.push(\" \".repeat(hatPad) + this.state.hat);\n\t\t}\n\n\t\t// Heart animation line (above sprite, inserted at top)\n\t\tif (this.isPetting && this.hearts.length > 0) {\n\t\t\tconst heartLine = \" \".repeat(spriteWidth);\n\t\t\tconst chars = heartLine.split(\"\");\n\t\t\tfor (const heart of this.hearts) {\n\t\t\t\tconst x = Math.min(heart.x, chars.length - 2);\n\t\t\t\tif (x >= 0 && heart.life > 0) {\n\t\t\t\t\tconst hChar = heart.char;\n\t\t\t\t\tchars.splice(x, hChar.length, ...hChar.split(\"\"));\n\t\t\t\t}\n\t\t\t}\n\t\t\tleftLines.push(chars.join(\"\"));\n\t\t}\n\n\t\t// Sprite lines\n\t\tleftLines.push(...rendered);\n\n\t\t// Build RIGHT block: speech bubble or thinking indicator\n\t\tlet rightLines: string[] = [];\n\t\tif (this.speechText) {\n\t\t\tconst availableWidth = width - spriteWidth - SIDE_PANEL_GAP - 5; // 5 for leading space + bubble borders + padding\n\t\t\tconst bubbleMaxWidth = Math.max(20, availableWidth);\n\t\t\trightLines = this.formatSpeechBubble(this.speechText, bubbleMaxWidth);\n\t\t} else if (this.thinkingLabel !== null) {\n\t\t\tconst dots = \".\".repeat((this.thinkingDots % BuddyComponent.THINKING_DOT_COUNT) + 1);\n\t\t\tconst label = this.thinkingLabel || \"thinking\";\n\t\t\trightLines = [` ${theme.fg(\"muted\", `💭 ${label}${dots}`)}`];\n\t\t}\n\n\t\t// Merge left and right side-by-side\n\t\tif (rightLines.length > 0) {\n\t\t\tconst merged = joinColumns(leftLines, rightLines, SIDE_PANEL_GAP, width);\n\t\t\tlines.push(...merged);\n\t\t} else {\n\t\t\tlines.push(...leftLines);\n\t\t}\n\n\t\t// Name + rarity line (full width, below the sprite+panel area)\n\t\tconst shinyMark = this.state.shiny ? \" ✨\" : \"\";\n\t\tconst rarityColor = this.rarityColor(this.state.rarity);\n\t\tconst nameLine = ` ${theme.bold(this.state.name)}${shinyMark} ${theme.fg(rarityColor, `[${this.state.rarity}]`)} ${theme.fg(\"muted\", this.state.species)}`;\n\t\tlines.push(nameLine);\n\n\t\t// Stats line (full width)\n\t\tconst statParts = (Object.values(Stat) as Stat[]).map((s) => {\n\t\t\tconst val = this.state.stats[s];\n\t\t\tconst bar = this.statBar(val);\n\t\t\treturn `${theme.fg(\"muted\", s[0])}:${bar}`;\n\t\t});\n\t\tlines.push(` ${statParts.join(\" \")}`);\n\n\t\treturn lines;\n\t}\n\n\t// =============================================================================\n\t// Narrow rendering (< 100 cols)\n\t// =============================================================================\n\n\tprivate renderNarrow(width: number): string[] {\n\t\tconst lines: string[] = [];\n\n\t\t// Single-line face\n\t\tconst eyes = this.state.eyeStyle;\n\t\tconst mouth = this.isPetting ? \"♥\" : \">\";\n\t\tconst face = `${this.state.hat}${eyes}${mouth}${eyes}`;\n\n\t\t// Name + truncated quip\n\t\tconst shinyMark = this.state.shiny ? \"✨\" : \"\";\n\t\tconst name = `${theme.bold(this.state.name)}${shinyMark}`;\n\n\t\tif (this.speechText) {\n\t\t\tconst maxQuip = Math.max(10, width - face.length - this.state.name.length - 6);\n\t\t\tconst styledQuip = this.renderInlineMarkdown(this.speechText);\n\t\t\tconst quip = visibleWidth(styledQuip) > maxQuip ? `${truncateToWidth(styledQuip, maxQuip - 1)}…` : styledQuip;\n\t\t\tlines.push(` ${face} ${name}: ${theme.fg(\"accent\", quip)}`);\n\t\t} else if (this.thinkingLabel !== null) {\n\t\t\tconst dots = \".\".repeat((this.thinkingDots % BuddyComponent.THINKING_DOT_COUNT) + 1);\n\t\t\tconst label = this.thinkingLabel || \"thinking\";\n\t\t\tlines.push(` ${face} ${name} ${theme.fg(\"muted\", `💭 ${label}${dots}`)}`);\n\t\t} else {\n\t\t\tlines.push(` ${face} ${name}`);\n\t\t}\n\n\t\treturn lines;\n\t}\n\n\t// =============================================================================\n\t// Animation\n\t// =============================================================================\n\n\tprivate startAnimation(): void {\n\t\tthis.interval = setInterval(() => {\n\t\t\tthis.currentFrame = (this.currentFrame + 1) % this.totalFrames;\n\n\t\t\t// Tick thinking dots\n\t\t\tif (this.thinkingLabel !== null) {\n\t\t\t\tthis.thinkingDots = (this.thinkingDots + 1) % BuddyComponent.THINKING_DOT_COUNT;\n\t\t\t}\n\n\t\t\t// Tick hearts\n\t\t\tif (this.isPetting) {\n\t\t\t\tfor (const heart of this.hearts) {\n\t\t\t\t\theart.life--;\n\t\t\t\t\theart.y++;\n\t\t\t\t}\n\t\t\t\tthis.hearts = this.hearts.filter((h) => h.life > 0);\n\t\t\t}\n\n\t\t\tthis.bumpVersion();\n\t\t\tthis.ui.requestRender();\n\t\t}, IDLE_INTERVAL_MS);\n\t}\n\n\tprivate stopAnimation(): void {\n\t\tif (this.interval) {\n\t\t\tclearInterval(this.interval);\n\t\t\tthis.interval = null;\n\t\t}\n\t}\n\n\tprivate bumpVersion(): void {\n\t\tthis.renderVersion++;\n\t}\n\n\t// =============================================================================\n\t// Formatting helpers\n\t// =============================================================================\n\n\tprivate statBar(value: number): string {\n\t\tconst filled = Math.round(value / 10);\n\t\tconst empty = 10 - filled;\n\t\tconst bar = \"█\".repeat(filled) + \"░\".repeat(empty);\n\t\tconst color: \"success\" | \"warning\" | \"error\" = value >= 70 ? \"success\" : value >= 40 ? \"warning\" : \"error\";\n\t\treturn theme.fg(color, bar);\n\t}\n\n\tprivate rarityColor(rarity: Rarity): \"muted\" | \"success\" | \"accent\" | \"warning\" | \"error\" {\n\t\tswitch (rarity) {\n\t\t\tcase Rarity.COMMON:\n\t\t\t\treturn \"muted\";\n\t\t\tcase Rarity.UNCOMMON:\n\t\t\t\treturn \"success\";\n\t\t\tcase Rarity.RARE:\n\t\t\t\treturn \"accent\";\n\t\t\tcase Rarity.EPIC:\n\t\t\t\treturn \"warning\";\n\t\t\tcase Rarity.LEGENDARY:\n\t\t\t\treturn \"error\";\n\t\t}\n\t}\n\n\tprivate formatSpeechBubble(text: string, maxWidth: number): string[] {\n\t\tif (!text.trim()) return [];\n\n\t\t// Render inline markdown (bold, italic, code) to styled text with ANSI codes\n\t\tconst styledText = this.renderInlineMarkdown(text);\n\n\t\t// Word-wrap using visible width (ANSI-aware, handles long words)\n\t\tconst lines = wrapTextWithAnsi(styledText, maxWidth);\n\n\t\t// Enforce hard line cap\n\t\tif (lines.length > SPEECH_MAX_CONTENT_LINES) {\n\t\t\tconst kept = lines.slice(0, SPEECH_MAX_CONTENT_LINES - 1);\n\t\t\tconst lastLine = lines[SPEECH_MAX_CONTENT_LINES - 1];\n\t\t\t// Truncate the last kept line with ellipsis\n\t\t\tconst truncated = truncateToWidth(lastLine, maxWidth - 1, \"…\");\n\t\t\tkept.push(truncated);\n\t\t\tlines.length = 0;\n\t\t\tlines.push(...kept);\n\t\t}\n\n\t\t// Wrap in bubble border using visible width for measurement\n\t\tconst maxLineWidth = Math.max(...lines.map((l) => visibleWidth(l)));\n\t\tconst bubbleWidth = Math.max(6, Math.min(maxLineWidth + 4, maxWidth + 4));\n\t\tconst top = `╭${\"─\".repeat(bubbleWidth - 2)}╮`;\n\t\tconst bottom = `╰${\"─\".repeat(bubbleWidth - 2)}╯`;\n\n\t\tconst result: string[] = [];\n\t\tresult.push(` ${theme.fg(\"accent\", top)}`);\n\t\tfor (const line of lines) {\n\t\t\tconst padding = Math.max(0, bubbleWidth - 4 - visibleWidth(line));\n\t\t\tconst padded = line + \" \".repeat(padding);\n\t\t\tresult.push(` ${theme.fg(\"accent\", \"│\")} ${padded} ${theme.fg(\"accent\", \"│\")}`);\n\t\t}\n\t\tresult.push(` ${theme.fg(\"accent\", bottom)}`);\n\n\t\treturn result;\n\t}\n\n\t/** Render inline markdown tokens (bold, italic, code) to ANSI-styled text */\n\tprivate renderInlineMarkdown(text: string): string {\n\t\tconst mdTheme = getMarkdownTheme();\n\t\tconst tokens = marked.lexer(text);\n\n\t\t// Flatten: we expect a paragraph containing inline tokens\n\t\tconst inlineTokens = tokens.flatMap((t: any) =>\n\t\t\tt.type === \"paragraph\" ? (t.tokens ?? []) : t.type === \"text\" ? t : [],\n\t\t);\n\n\t\treturn this.renderInlineTokens(inlineTokens, mdTheme);\n\t}\n\n\t/** Recursively render marked inline tokens to ANSI-styled strings */\n\tprivate renderInlineTokens(tokens: any[], mdTheme: MT): string {\n\t\tlet result = \"\";\n\t\tfor (const token of tokens) {\n\t\t\tswitch (token.type) {\n\t\t\t\tcase \"text\":\n\t\t\t\t\tresult += token.text ?? this.renderInlineTokens(token.tokens ?? [], mdTheme);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"strong\":\n\t\t\t\t\tresult += mdTheme.bold(this.renderInlineTokens(token.tokens ?? [], mdTheme));\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"em\":\n\t\t\t\t\tresult += mdTheme.italic(this.renderInlineTokens(token.tokens ?? [], mdTheme));\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"codespan\":\n\t\t\t\t\tresult += mdTheme.code(token.text);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"escape\":\n\t\t\t\t\tresult += token.text;\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\tresult += token.text ?? token.raw ?? \"\";\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dreb/coding-agent",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Coding agent CLI with read, bash, edit, write tools and session management",
5
5
  "type": "module",
6
6
  "drebConfig": {
@@ -52,10 +52,10 @@
52
52
  "prepublishOnly": "npm run clean && npm run build"
53
53
  },
54
54
  "dependencies": {
55
- "@dreb/agent-core": "^1.0.0",
56
- "@dreb/ai": "^1.0.0",
57
- "@dreb/semantic-search": "^1.0.0",
58
- "@dreb/tui": "^1.0.0",
55
+ "@dreb/agent-core": "^2.0.0",
56
+ "@dreb/ai": "^2.0.0",
57
+ "@dreb/semantic-search": "^2.0.0",
58
+ "@dreb/tui": "^2.0.0",
59
59
  "@huggingface/transformers": "^4.0.1",
60
60
  "@mariozechner/jiti": "^2.6.2",
61
61
  "@silvia-odwyer/photon-node": "^0.3.4",