@ramarivera/coding-buddy 0.4.0-alpha.5 → 0.4.0-alpha.6

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.
@@ -5,14 +5,14 @@
5
5
  },
6
6
  "metadata": {
7
7
  "description": "Permanent coding companion for Claude Code",
8
- "version": "0.4.0-alpha.5"
8
+ "version": "0.4.0-alpha.6"
9
9
  },
10
10
  "plugins": [
11
11
  {
12
12
  "name": "claude-buddy",
13
13
  "source": "./",
14
14
  "description": "Permanent coding companion for Claude Code \u2014 survives any update. MCP-based terminal pet with ASCII art, stats, reactions, and personality.",
15
- "version": "0.4.0-alpha.5",
15
+ "version": "0.4.0-alpha.6",
16
16
  "author": {
17
17
  "name": "1270011"
18
18
  },
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-buddy",
3
- "version": "0.4.0-alpha.5",
3
+ "version": "0.4.0-alpha.6"
4
4
  "description": "Permanent coding companion for Claude Code \u2014 survives any update. MCP-based terminal pet with ASCII art, stats, reactions, and personality.",
5
5
  "author": {
6
6
  "name": "1270011"
@@ -89,7 +89,7 @@ const buddyService = new BuddyCommandService({
89
89
  const server = new McpServer(
90
90
  {
91
91
  name: "claude-buddy",
92
- version: "0.4.0-alpha.5",
92
+ version: "0.4.0-alpha.6",
93
93
  },
94
94
  {
95
95
  instructions: getInstructions(buddyService.loadActiveCompanion()),
@@ -9,12 +9,13 @@ import type {
9
9
  } from "@mariozechner/pi-coding-agent";
10
10
  import { isBashToolResult } from "@mariozechner/pi-coding-agent";
11
11
  import type { AgentMessage } from "@mariozechner/pi-agent-core";
12
- import type { AssistantMessage, TextContent } from "@mariozechner/pi-ai";
12
+ import { complete, type AssistantMessage, type TextContent, type UserMessage } from "@mariozechner/pi-ai";
13
13
  import { BuddyCommandService } from "../../core/command-service.ts";
14
14
  import type { Achievement } from "../../core/achievements.ts";
15
15
  import type { Companion } from "../../core/model.ts";
16
16
  import { getNameReaction, getSuccessReaction } from "../../core/reactions.ts";
17
17
  import { PiBuddyStorage } from "./storage.ts";
18
+ import { buildBuddyReactionPrompt, normalizeBuddyComment, stripBuddyComments } from "./prompt.ts";
18
19
  import { PiBuddyUI } from "./ui.ts";
19
20
 
20
21
  interface RegisterBuddyEventsDeps {
@@ -93,7 +94,7 @@ export function registerBuddyEvents(pi: ExtensionAPI, deps: RegisterBuddyEventsD
93
94
  return;
94
95
  }
95
96
 
96
- const comment = deriveTurnComment(progress.companion, event.message);
97
+ const comment = await generateTurnComment(ctx, progress.companion, event);
97
98
  if (!comment) {
98
99
  deps.ui.refresh(ctx, progress.companion, deps.storage.loadLatest(), progress.achievements);
99
100
  return;
@@ -177,10 +178,66 @@ function isAssistantMessage(message: AgentMessage): message is AssistantMessage
177
178
  function getAssistantText(message: AssistantMessage): string {
178
179
  return message.content
179
180
  .filter((block): block is TextContent => block.type === "text")
180
- .map((block) => block.text)
181
+ .map((block) => stripBuddyComments(block.text))
181
182
  .join("\n");
182
183
  }
183
184
 
185
+ async function generateTurnComment(
186
+ ctx: ExtensionContext,
187
+ companion: Companion,
188
+ event: TurnEndEvent,
189
+ ): Promise<string | null> {
190
+ const assistantText = isAssistantMessage(event.message) ? getAssistantText(event.message) : "";
191
+ if (!assistantText.trim()) return null;
192
+
193
+ if (ctx.model) {
194
+ const auth = await ctx.modelRegistry.getApiKeyAndHeaders(ctx.model);
195
+ if (auth.ok && auth.apiKey) {
196
+ const userMessage: UserMessage = {
197
+ role: "user",
198
+ content: [{ type: "text", text: buildBuddyReactionPrompt(companion, assistantText, getToolResultsText(event)) }],
199
+ timestamp: Date.now(),
200
+ };
201
+
202
+ try {
203
+ const response = await complete(
204
+ ctx.model,
205
+ { messages: [userMessage] },
206
+ {
207
+ apiKey: auth.apiKey,
208
+ headers: auth.headers,
209
+ signal: ctx.signal,
210
+ },
211
+ );
212
+
213
+ if (response.stopReason !== "aborted") {
214
+ const text = response.content
215
+ .filter((block): block is TextContent => block.type === "text")
216
+ .map((block) => block.text)
217
+ .join("\n");
218
+ const normalized = normalizeBuddyComment(text);
219
+ if (normalized) return normalized;
220
+ }
221
+ } catch {
222
+ // Fall through to heuristic fallback.
223
+ }
224
+ }
225
+ }
226
+
227
+ return deriveTurnComment(companion, event.message);
228
+ }
229
+
230
+ function getToolResultsText(event: TurnEndEvent): string {
231
+ return event.toolResults
232
+ .map((result) => result.content
233
+ .filter((block): block is TextContent => block.type === "text")
234
+ .map((block) => block.text)
235
+ .join("\n"))
236
+ .filter(Boolean)
237
+ .join("\n\n")
238
+ .slice(0, 4000);
239
+ }
240
+
184
241
  export function deriveTurnComment(companion: Companion, message: AgentMessage): string | null {
185
242
  if (!isAssistantMessage(message)) return null;
186
243
 
@@ -0,0 +1,42 @@
1
+ import type { Companion } from "../../core/model.ts";
2
+
3
+ export function buildBuddyReactionPrompt(
4
+ companion: Companion,
5
+ assistantText: string,
6
+ toolText: string,
7
+ ): string {
8
+ return [
9
+ `You are generating a single end-of-turn buddy reaction for ${companion.name}, a ${companion.bones.rarity} ${companion.bones.species}.`,
10
+ `Buddy personality: ${companion.personality}`,
11
+ `Peak stat: ${companion.bones.peak} (${companion.bones.stats[companion.bones.peak]}). Dump stat: ${companion.bones.dump} (${companion.bones.stats[companion.bones.dump]}).`,
12
+ "",
13
+ "Write exactly one short in-character reaction sentence as the buddy.",
14
+ "Rules:",
15
+ `- Write as ${companion.name}, not as the assistant.`,
16
+ "- Reference something specific from the turn.",
17
+ "- Max 150 characters.",
18
+ "- Use *asterisks* for physical actions when useful.",
19
+ "- Output only the reaction line, with no quotes, labels, markdown fences, or explanation.",
20
+ "",
21
+ "Assistant response:",
22
+ assistantText || "(empty)",
23
+ "",
24
+ "Tool result summary:",
25
+ toolText || "(none)",
26
+ ].join("\n");
27
+ }
28
+
29
+ export function stripBuddyComments(text: string): string {
30
+ return text.replace(/<!--\s*buddy:\s*[\s\S]*?-->/gi, "").trimEnd();
31
+ }
32
+
33
+ export function normalizeBuddyComment(text: string): string {
34
+ return text
35
+ .replace(/<!--\s*buddy:\s*([\s\S]*?)\s*-->/gi, "$1")
36
+ .replace(/^buddy\s*:\s*/i, "")
37
+ .replace(/^['"`]+|['"`]+$/g, "")
38
+ .replace(/\s+/g, " ")
39
+ .trim()
40
+ .slice(0, 150)
41
+ .trim();
42
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ramarivera/coding-buddy",
3
- "version": "0.4.0-alpha.5",
3
+ "version": "0.4.0-alpha.6",
4
4
  "description": "Persistent coding companion for Claude Code and pi",
5
5
  "type": "module",
6
6
  "bin": {