@friendlyrobot/discord-pi-agent 0.19.3 → 0.19.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +75 -14
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -308,6 +308,7 @@ 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,31 @@ async function runAgentTurn(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
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
325
|
+
toolInputsByCallId.set(event.toolCallId, input);
|
|
326
|
+
if (event.toolName === "bash") {
|
|
327
|
+
logger4.debug({
|
|
328
|
+
toolName: event.toolName
|
|
329
|
+
}, `agent tool start: [${event.toolName}]`);
|
|
330
|
+
debugPrint(input, "CMD");
|
|
331
|
+
} else {
|
|
332
|
+
logger4.debug({
|
|
333
|
+
toolName: event.toolName,
|
|
334
|
+
input
|
|
335
|
+
}, `agent tool start: [${event.toolName}]`);
|
|
336
|
+
}
|
|
328
337
|
}
|
|
329
338
|
if (event.type === "tool_execution_end") {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
339
|
+
const input = toolInputsByCallId.get(event.toolCallId);
|
|
340
|
+
toolInputsByCallId.delete(event.toolCallId);
|
|
341
|
+
if (event.toolName === "bash") {
|
|
342
|
+
debugPrint(extractToolOutput(event.result), event.isError ? "BASH TOOL ERROR OUTPUT" : "BASH TOOL OUTPUT");
|
|
343
|
+
} else {
|
|
344
|
+
logger4.debug({
|
|
345
|
+
toolName: event.toolName,
|
|
346
|
+
input: truncateForLog(extractToolOutput(input)),
|
|
347
|
+
isError: event.isError
|
|
348
|
+
}, `agent tool end: [${event.toolName}]`);
|
|
349
|
+
}
|
|
334
350
|
}
|
|
335
351
|
});
|
|
336
352
|
try {
|
|
@@ -352,6 +368,56 @@ async function runAgentTurn(session, prompt, options = {}) {
|
|
|
352
368
|
}
|
|
353
369
|
return "No response generated.";
|
|
354
370
|
}
|
|
371
|
+
function truncateForLog(value, maxLength = 400) {
|
|
372
|
+
if (value.length <= maxLength) {
|
|
373
|
+
return value;
|
|
374
|
+
}
|
|
375
|
+
return `${value.slice(0, maxLength)}...`;
|
|
376
|
+
}
|
|
377
|
+
function extractToolOutput(output) {
|
|
378
|
+
if (typeof output === "string") {
|
|
379
|
+
const parsed = tryParseJson(output);
|
|
380
|
+
if (parsed !== undefined && typeof parsed === "object" && parsed !== null) {
|
|
381
|
+
return extractTextFromObject(parsed) ?? output;
|
|
382
|
+
}
|
|
383
|
+
return output;
|
|
384
|
+
}
|
|
385
|
+
if (typeof output === "object" && output !== null) {
|
|
386
|
+
return extractTextFromObject(output) ?? JSON.stringify(output);
|
|
387
|
+
}
|
|
388
|
+
return String(output);
|
|
389
|
+
}
|
|
390
|
+
function tryParseJson(value) {
|
|
391
|
+
try {
|
|
392
|
+
return JSON.parse(value);
|
|
393
|
+
} catch {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
var TEXT_BEARING_FIELDS = [
|
|
398
|
+
"text",
|
|
399
|
+
"content",
|
|
400
|
+
"message",
|
|
401
|
+
"output",
|
|
402
|
+
"result"
|
|
403
|
+
];
|
|
404
|
+
function extractTextFromObject(obj) {
|
|
405
|
+
for (const field of TEXT_BEARING_FIELDS) {
|
|
406
|
+
if (field in obj) {
|
|
407
|
+
const value = obj[field];
|
|
408
|
+
if (typeof value === "string" && value.trim().length > 0) {
|
|
409
|
+
return value;
|
|
410
|
+
}
|
|
411
|
+
if (typeof value === "object" && value !== null) {
|
|
412
|
+
const nested = extractTextFromObject(value);
|
|
413
|
+
if (nested !== null) {
|
|
414
|
+
return nested;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
355
421
|
function getLatestAssistantText(messages) {
|
|
356
422
|
const latestAssistantMessage = [...messages].reverse().find((message) => {
|
|
357
423
|
return message.role === "assistant";
|
|
@@ -628,12 +694,7 @@ function parseStringArrayFromEnv(key) {
|
|
|
628
694
|
}
|
|
629
695
|
|
|
630
696
|
// src/discord-gateway-client.ts
|
|
631
|
-
import {
|
|
632
|
-
Client,
|
|
633
|
-
Events,
|
|
634
|
-
GatewayIntentBits,
|
|
635
|
-
Partials
|
|
636
|
-
} from "discord.js";
|
|
697
|
+
import { Client, Events, GatewayIntentBits, Partials } from "discord.js";
|
|
637
698
|
|
|
638
699
|
// src/session-commands.ts
|
|
639
700
|
function getSessionStatusText(session, promptQueue, extras) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friendlyrobot/discord-pi-agent",
|
|
3
|
-
"version": "0.19.
|
|
3
|
+
"version": "0.19.5",
|
|
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",
|