@friendlyrobot/discord-pi-agent 0.19.2 → 0.19.4

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.
@@ -3,5 +3,5 @@ import type { ImageContent } from "@earendil-works/pi-ai";
3
3
  type CollectReplyOptions = {
4
4
  images?: ImageContent[];
5
5
  };
6
- export declare function runPromptAndCollectReply(session: AgentSession, prompt: string, options?: CollectReplyOptions): Promise<string>;
6
+ export declare function runAgentTurn(session: AgentSession, prompt: string, options?: CollectReplyOptions): Promise<string>;
7
7
  export {};
package/dist/index.js CHANGED
@@ -302,12 +302,13 @@ async function formatWithPrettier(text) {
302
302
  }
303
303
  }
304
304
 
305
- // src/reply-buffer.ts
306
- var logger4 = createModuleLogger("reply-buffer");
307
- async function runPromptAndCollectReply(session, prompt, options = {}) {
305
+ // src/agent-turn-runner.ts
306
+ var logger4 = createModuleLogger("agent-turn-runner");
307
+ async function runAgentTurn(session, prompt, options = {}) {
308
308
  let streamedText = "";
309
309
  let eventCount = 0;
310
310
  let toolCount = 0;
311
+ const toolInputsByCallId = new Map;
311
312
  const model = session.model ? `${session.model.provider}/${session.model.id}` : "none";
312
313
  debugPrint(prompt, "Full Prompt");
313
314
  const unsubscribe = session.subscribe((event) => {
@@ -321,16 +322,34 @@ async function runPromptAndCollectReply(session, prompt, options = {}) {
321
322
  if (event.type === "tool_execution_start") {
322
323
  toolCount += 1;
323
324
  const input = event.toolName === "bash" ? event.args.command : event.args;
324
- logger4.debug({
325
- toolName: event.toolName,
326
- input
327
- }, `agent tool start: [${event.toolName}] `);
325
+ toolInputsByCallId.set(event.toolCallId, input);
326
+ if (event.toolName === "bash") {
327
+ logger4.debug({
328
+ toolName: event.toolName
329
+ }, `agent tool start: [${event.toolName}] ${String(input)}`);
330
+ } else {
331
+ logger4.debug({
332
+ toolName: event.toolName,
333
+ input
334
+ }, `agent tool start: [${event.toolName}]`);
335
+ }
328
336
  }
329
337
  if (event.type === "tool_execution_end") {
330
- logger4.debug({
331
- toolName: event.toolName,
332
- isError: event.isError
333
- }, `agent tool end: [${event.toolName}]`);
338
+ const input = toolInputsByCallId.get(event.toolCallId);
339
+ toolInputsByCallId.delete(event.toolCallId);
340
+ if (event.toolName === "bash") {
341
+ logger4.debug({
342
+ toolName: event.toolName,
343
+ isError: event.isError
344
+ }, `agent tool end: [${event.toolName}] ${truncateForLog(typeof input === "string" ? input : "")}`);
345
+ debugPrint(extractToolOutput(event.result), event.isError ? "BASH TOOL ERROR OUTPUT" : "BASH TOOL OUTPUT");
346
+ } else {
347
+ logger4.debug({
348
+ toolName: event.toolName,
349
+ input: truncateForLog(extractToolOutput(input)),
350
+ isError: event.isError
351
+ }, `agent tool end: [${event.toolName}]`);
352
+ }
334
353
  }
335
354
  });
336
355
  try {
@@ -352,6 +371,22 @@ async function runPromptAndCollectReply(session, prompt, options = {}) {
352
371
  }
353
372
  return "No response generated.";
354
373
  }
374
+ function truncateForLog(value, maxLength = 400) {
375
+ if (value.length <= maxLength) {
376
+ return value;
377
+ }
378
+ return `${value.slice(0, maxLength)}...`;
379
+ }
380
+ function extractToolOutput(output) {
381
+ if (typeof output === "string") {
382
+ return output;
383
+ }
384
+ try {
385
+ return JSON.stringify(output);
386
+ } catch {
387
+ return String(output);
388
+ }
389
+ }
355
390
  function getLatestAssistantText(messages) {
356
391
  const latestAssistantMessage = [...messages].reverse().find((message) => {
357
392
  return message.role === "assistant";
@@ -454,7 +489,7 @@ class AgentService {
454
489
  async prompt(text) {
455
490
  const session = this.requireSession();
456
491
  const transformedPrompt = await this.config.promptTransform(text);
457
- return runPromptAndCollectReply(session, transformedPrompt);
492
+ return runAgentTurn(session, transformedPrompt);
458
493
  }
459
494
  async compact() {
460
495
  const session = this.requireSession();
@@ -1552,7 +1587,7 @@ ${attachment.content}`;
1552
1587
  }
1553
1588
  const wrappedContent = buildDiscordPromptContent(message, scope, promptContent, config);
1554
1589
  const transformedPrompt = await config.promptTransform(wrappedContent);
1555
- return runPromptAndCollectReply(session, transformedPrompt, {
1590
+ return runAgentTurn(session, transformedPrompt, {
1556
1591
  images: promptImages
1557
1592
  });
1558
1593
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@friendlyrobot/discord-pi-agent",
3
- "version": "0.19.2",
3
+ "version": "0.19.4",
4
4
  "description": "Reusable Discord gateway for persistent pi agent sessions",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -25,9 +25,9 @@
25
25
  "access": "public"
26
26
  },
27
27
  "scripts": {
28
- "test:watch": "vitest",
29
- "test": "vitest run",
30
- "test:coverage": "vitest run --coverage",
28
+ "test:watch": "LOG_LEVEL=debug vitest",
29
+ "test": "LOG_LEVEL=silent vitest run",
30
+ "test:coverage": "LOG_LEVEL=silent vitest run --coverage",
31
31
  "format": "prettier --write .",
32
32
  "build:01-clean": "rm -rf dist",
33
33
  "build:02-tsgo": "tsgo -p tsconfig.build.json --emitDeclarationOnly --declaration --declarationMap false",